ncal.pas 93 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This file implements the node for sub procedure calling.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit ncal;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cutils,cclasses,
  23. globtype,cpuinfo,
  24. paramgr,
  25. node,nbas,nutils,
  26. {$ifdef state_tracking}
  27. nstate,
  28. {$endif state_tracking}
  29. symbase,symtype,symsym,symdef,symtable;
  30. type
  31. tcallnodeflag = (
  32. cnf_restypeset,
  33. cnf_return_value_used,
  34. cnf_inherited,
  35. cnf_anon_inherited,
  36. cnf_new_call,
  37. cnf_dispose_call,
  38. cnf_member_call { called with implicit methodpointer tree }
  39. );
  40. tcallnodeflags = set of tcallnodeflag;
  41. tcallnode = class(tbinarynode)
  42. private
  43. {$ifndef VER1_0}
  44. { info for inlining }
  45. inlinelocals: array of tnode;
  46. {$endif VER1_0}
  47. { number of parameters passed from the source, this does not include the hidden parameters }
  48. paralength : smallint;
  49. function gen_self_tree_methodpointer:tnode;
  50. function gen_self_tree:tnode;
  51. function gen_vmt_tree:tnode;
  52. procedure bind_paraitem;
  53. { function return node, this is used to pass the data for a
  54. ret_in_param return value }
  55. _funcretnode : tnode;
  56. procedure setfuncretnode(const returnnode: tnode);
  57. procedure convert_carg_array_of_const;
  58. procedure order_parameters;
  59. procedure createinlineparas(var createstatement, deletestatement: tstatementnode);
  60. function replaceparaload(var n: tnode; arg: pointer): foreachnoderesult;
  61. {$ifndef VER1_0}
  62. procedure createlocaltemps(p:TNamedIndexItem;arg:pointer);
  63. {$endif VER1_0}
  64. protected
  65. pushedparasize : longint;
  66. public
  67. { the symbol containing the definition of the procedure }
  68. { to call }
  69. symtableprocentry : tprocsym;
  70. symtableprocentryderef : tderef;
  71. { symtable where the entry was found, needed for with support }
  72. symtableproc : tsymtable;
  73. { the definition of the procedure to call }
  74. procdefinition : tabstractprocdef;
  75. procdefinitionderef : tderef;
  76. { tree that contains the pointer to the object for this method }
  77. methodpointerinit,
  78. methodpointerdone,
  79. methodpointer : tnode;
  80. { inline function body }
  81. inlinecode : tnode;
  82. { varargs tparaitems }
  83. varargsparas : tvarargspara;
  84. { node that specifies where the result should be put for calls }
  85. { that return their result in a parameter }
  86. property funcretnode: tnode read _funcretnode write setfuncretnode;
  87. { separately specified resulttype for some compilerprocs (e.g. }
  88. { you can't have a function with an "array of char" resulttype }
  89. { the RTL) (JM) }
  90. restype: ttype;
  91. callnodeflags : tcallnodeflags;
  92. { only the processor specific nodes need to override this }
  93. { constructor }
  94. constructor create(l:tnode; v : tprocsym;st : tsymtable; mp: tnode; callflags:tcallnodeflags);virtual;
  95. constructor create_procvar(l,r:tnode);
  96. constructor createintern(const name: string; params: tnode);
  97. constructor createinternres(const name: string; params: tnode; const res: ttype);
  98. constructor createinternreturn(const name: string; params: tnode; returnnode : tnode);
  99. destructor destroy;override;
  100. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  101. procedure ppuwrite(ppufile:tcompilerppufile);override;
  102. procedure buildderefimpl;override;
  103. procedure derefimpl;override;
  104. function getcopy : tnode;override;
  105. { Goes through all symbols in a class and subclasses and calls
  106. verify abstract for each .
  107. }
  108. procedure verifyabstractcalls;
  109. { called for each definition in a class and verifies if a method
  110. is abstract or not, if it is abstract, give out a warning
  111. }
  112. procedure verifyabstract(p : tnamedindexitem;arg:pointer);
  113. procedure insertintolist(l : tnodelist);override;
  114. function pass_1 : tnode;override;
  115. function det_resulttype:tnode;override;
  116. {$ifdef state_tracking}
  117. function track_state_pass(exec_known:boolean):boolean;override;
  118. {$endif state_tracking}
  119. function docompare(p: tnode): boolean; override;
  120. procedure printnodedata(var t:text);override;
  121. function para_count:longint;
  122. private
  123. AbstractMethodsList : TStringList;
  124. end;
  125. tcallnodeclass = class of tcallnode;
  126. tcallparaflag = (
  127. cpf_is_colon_para,
  128. cpf_varargs_para { belongs this para to varargs }
  129. );
  130. tcallparaflags = set of tcallparaflag;
  131. tcallparanode = class(tbinarynode)
  132. public
  133. callparaflags : tcallparaflags;
  134. paraitem : tparaitem;
  135. used_by_callnode : boolean;
  136. { only the processor specific nodes need to override this }
  137. { constructor }
  138. constructor create(expr,next : tnode);virtual;
  139. destructor destroy;override;
  140. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  141. procedure ppuwrite(ppufile:tcompilerppufile);override;
  142. function getcopy : tnode;override;
  143. procedure insertintolist(l : tnodelist);override;
  144. procedure get_paratype;
  145. procedure insert_typeconv(do_count : boolean);
  146. procedure det_registers;
  147. procedure firstcallparan;
  148. procedure secondcallparan;virtual;abstract;
  149. function docompare(p: tnode): boolean; override;
  150. procedure printnodetree(var t:text);override;
  151. end;
  152. tcallparanodeclass = class of tcallparanode;
  153. function reverseparameters(p: tcallparanode): tcallparanode;
  154. var
  155. ccallnode : tcallnodeclass;
  156. ccallparanode : tcallparanodeclass;
  157. { Current callnode, this is needed for having a link
  158. between the callparanodes and the callnode they belong to }
  159. aktcallnode : tcallnode;
  160. implementation
  161. uses
  162. systems,
  163. verbose,globals,
  164. symconst,defutil,defcmp,
  165. htypechk,pass_1,
  166. ncnv,nld,ninl,nadd,ncon,nmem,
  167. procinfo,
  168. cgbase
  169. ;
  170. type
  171. tobjectinfoitem = class(tlinkedlistitem)
  172. objinfo : tobjectdef;
  173. constructor create(def : tobjectdef);
  174. end;
  175. {****************************************************************************
  176. HELPERS
  177. ****************************************************************************}
  178. function reverseparameters(p: tcallparanode): tcallparanode;
  179. var
  180. hp1, hp2: tcallparanode;
  181. begin
  182. hp1:=nil;
  183. while assigned(p) do
  184. begin
  185. { pull out }
  186. hp2:=p;
  187. p:=tcallparanode(p.right);
  188. { pull in }
  189. hp2.right:=hp1;
  190. hp1:=hp2;
  191. end;
  192. reverseparameters:=hp1;
  193. end;
  194. function gen_high_tree(p:tnode;paradef:tdef):tnode;
  195. var
  196. temp: tnode;
  197. len : integer;
  198. loadconst : boolean;
  199. hightree : tnode;
  200. begin
  201. len:=-1;
  202. loadconst:=true;
  203. hightree:=nil;
  204. case p.resulttype.def.deftype of
  205. arraydef :
  206. begin
  207. if (paradef.deftype<>arraydef) then
  208. internalerror(200405241);
  209. { handle special case of passing an single array to an array of array }
  210. if compare_defs(tarraydef(paradef).elementtype.def,p.resulttype.def,nothingn)>=te_equal then
  211. len:=0
  212. else
  213. begin
  214. { handle via a normal inline in_high_x node }
  215. loadconst := false;
  216. hightree := geninlinenode(in_high_x,false,p.getcopy);
  217. resulttypepass(hightree);
  218. { only substract low(array) if it's <> 0 }
  219. temp := geninlinenode(in_low_x,false,p.getcopy);
  220. resulttypepass(temp);
  221. if (temp.nodetype <> ordconstn) or
  222. (tordconstnode(temp).value <> 0) then
  223. hightree := caddnode.create(subn,hightree,temp)
  224. else
  225. temp.free;
  226. end;
  227. end;
  228. stringdef :
  229. begin
  230. if is_open_string(paradef) then
  231. begin
  232. { handle via a normal inline in_high_x node }
  233. loadconst := false;
  234. hightree := geninlinenode(in_high_x,false,p.getcopy);
  235. end
  236. else
  237. begin
  238. { passing a string to an array of char }
  239. if (p.nodetype=stringconstn) then
  240. begin
  241. len:=str_length(p);
  242. if len>0 then
  243. dec(len);
  244. end
  245. else
  246. begin
  247. hightree:=caddnode.create(subn,geninlinenode(in_length_x,false,p.getcopy),
  248. cordconstnode.create(1,s32inttype,false));
  249. loadconst:=false;
  250. end;
  251. end;
  252. end;
  253. else
  254. len:=0;
  255. end;
  256. if loadconst then
  257. hightree:=cordconstnode.create(len,s32inttype,true)
  258. else
  259. begin
  260. if not assigned(hightree) then
  261. internalerror(200304071);
  262. { Need to use explicit, because it can also be a enum }
  263. hightree:=ctypeconvnode.create_explicit(hightree,s32inttype);
  264. end;
  265. result:=hightree;
  266. end;
  267. {****************************************************************************
  268. TOBJECTINFOITEM
  269. ****************************************************************************}
  270. constructor tobjectinfoitem.create(def : tobjectdef);
  271. begin
  272. inherited create;
  273. objinfo := def;
  274. end;
  275. {****************************************************************************
  276. TCALLPARANODE
  277. ****************************************************************************}
  278. constructor tcallparanode.create(expr,next : tnode);
  279. begin
  280. inherited create(callparan,expr,next);
  281. if not assigned(expr) then
  282. internalerror(200305091);
  283. expr.set_file_line(self);
  284. callparaflags:=[];
  285. end;
  286. destructor tcallparanode.destroy;
  287. begin
  288. { When the node is used by callnode then
  289. we don't destroy left, the callnode takes care of it }
  290. if used_by_callnode then
  291. left:=nil;
  292. inherited destroy;
  293. end;
  294. constructor tcallparanode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  295. begin
  296. inherited ppuload(t,ppufile);
  297. ppufile.getsmallset(callparaflags);
  298. end;
  299. procedure tcallparanode.ppuwrite(ppufile:tcompilerppufile);
  300. begin
  301. inherited ppuwrite(ppufile);
  302. ppufile.putsmallset(callparaflags);
  303. end;
  304. function tcallparanode.getcopy : tnode;
  305. var
  306. n : tcallparanode;
  307. begin
  308. n:=tcallparanode(inherited getcopy);
  309. n.callparaflags:=callparaflags;
  310. n.paraitem:=paraitem;
  311. result:=n;
  312. end;
  313. procedure tcallparanode.insertintolist(l : tnodelist);
  314. begin
  315. end;
  316. procedure tcallparanode.get_paratype;
  317. var
  318. old_array_constructor : boolean;
  319. begin
  320. inc(parsing_para_level);
  321. if assigned(right) then
  322. tcallparanode(right).get_paratype;
  323. old_array_constructor:=allow_array_constructor;
  324. allow_array_constructor:=true;
  325. resulttypepass(left);
  326. allow_array_constructor:=old_array_constructor;
  327. if codegenerror then
  328. resulttype:=generrortype
  329. else
  330. resulttype:=left.resulttype;
  331. dec(parsing_para_level);
  332. end;
  333. procedure tcallparanode.insert_typeconv(do_count : boolean);
  334. var
  335. oldtype : ttype;
  336. {$ifdef extdebug}
  337. store_count_ref : boolean;
  338. {$endif def extdebug}
  339. begin
  340. inc(parsing_para_level);
  341. {$ifdef extdebug}
  342. if do_count then
  343. begin
  344. store_count_ref:=count_ref;
  345. count_ref:=true;
  346. end;
  347. {$endif def extdebug}
  348. { Be sure to have the resulttype }
  349. if not assigned(left.resulttype.def) then
  350. resulttypepass(left);
  351. if (left.nodetype<>nothingn) then
  352. begin
  353. { Convert tp procvars, this is needs to be done
  354. here to make the change permanent. in the overload
  355. choosing the changes are only made temporary }
  356. if (left.resulttype.def.deftype=procvardef) and
  357. (paraitem.paratype.def.deftype<>procvardef) then
  358. begin
  359. if maybe_call_procvar(left,true) then
  360. resulttype:=left.resulttype;
  361. end;
  362. { Handle varargs and hidden paras directly, no typeconvs or }
  363. { typechecking needed }
  364. if (cpf_varargs_para in callparaflags) then
  365. begin
  366. { convert pascal to C types }
  367. case left.resulttype.def.deftype of
  368. stringdef :
  369. inserttypeconv(left,charpointertype);
  370. floatdef :
  371. inserttypeconv(left,s64floattype);
  372. end;
  373. set_varstate(left,vs_used,true);
  374. resulttype:=left.resulttype;
  375. { also update paraitem type to get the correct parameter location
  376. for the new types }
  377. paraitem.paratype:=left.resulttype;
  378. end
  379. else
  380. if (paraitem.is_hidden) then
  381. begin
  382. set_varstate(left,vs_used,true);
  383. resulttype:=left.resulttype;
  384. end
  385. else
  386. begin
  387. { Do we need arrayconstructor -> set conversion, then insert
  388. it here before the arrayconstructor node breaks the tree
  389. with its conversions of enum->ord }
  390. if (left.nodetype=arrayconstructorn) and
  391. (paraitem.paratype.def.deftype=setdef) then
  392. inserttypeconv(left,paraitem.paratype);
  393. { set some settings needed for arrayconstructor }
  394. if is_array_constructor(left.resulttype.def) then
  395. begin
  396. if is_array_of_const(paraitem.paratype.def) then
  397. begin
  398. { force variant array }
  399. include(left.flags,nf_forcevaria);
  400. end
  401. else
  402. begin
  403. include(left.flags,nf_novariaallowed);
  404. { now that the resultting type is know we can insert the required
  405. typeconvs for the array constructor }
  406. tarrayconstructornode(left).force_type(tarraydef(paraitem.paratype.def).elementtype);
  407. end;
  408. end;
  409. { check if local proc/func is assigned to procvar }
  410. if left.resulttype.def.deftype=procvardef then
  411. test_local_to_procvar(tprocvardef(left.resulttype.def),paraitem.paratype.def);
  412. { test conversions }
  413. if not(is_shortstring(left.resulttype.def) and
  414. is_shortstring(paraitem.paratype.def)) and
  415. (paraitem.paratype.def.deftype<>formaldef) then
  416. begin
  417. { Process open parameters }
  418. if paramanager.push_high_param(paraitem.paratyp,paraitem.paratype.def,aktcallnode.procdefinition.proccalloption) then
  419. begin
  420. { insert type conv but hold the ranges of the array }
  421. oldtype:=left.resulttype;
  422. inserttypeconv(left,paraitem.paratype);
  423. left.resulttype:=oldtype;
  424. end
  425. else
  426. begin
  427. { for ordinals, floats and enums, verify if we might cause
  428. some range-check errors. }
  429. if (paraitem.paratype.def.deftype in [enumdef,orddef,floatdef]) and
  430. (left.resulttype.def.deftype in [enumdef,orddef,floatdef]) and
  431. (left.nodetype in [vecn,loadn,calln]) then
  432. begin
  433. if (left.resulttype.def.size>paraitem.paratype.def.size) then
  434. begin
  435. if (cs_check_range in aktlocalswitches) then
  436. Message(type_w_smaller_possible_range_check)
  437. else
  438. Message(type_h_smaller_possible_range_check);
  439. end;
  440. end;
  441. inserttypeconv(left,paraitem.paratype);
  442. end;
  443. if codegenerror then
  444. begin
  445. dec(parsing_para_level);
  446. exit;
  447. end;
  448. end;
  449. { check var strings }
  450. if (cs_strict_var_strings in aktlocalswitches) and
  451. is_shortstring(left.resulttype.def) and
  452. is_shortstring(paraitem.paratype.def) and
  453. (paraitem.paratyp in [vs_out,vs_var]) and
  454. not(is_open_string(paraitem.paratype.def)) and
  455. not(equal_defs(left.resulttype.def,paraitem.paratype.def)) then
  456. begin
  457. aktfilepos:=left.fileinfo;
  458. CGMessage(type_e_strict_var_string_violation);
  459. end;
  460. { Handle formal parameters separate }
  461. if (paraitem.paratype.def.deftype=formaldef) then
  462. begin
  463. { load procvar if a procedure is passed }
  464. if (m_tp_procvar in aktmodeswitches) and
  465. (left.nodetype=calln) and
  466. (is_void(left.resulttype.def)) then
  467. load_procvar_from_calln(left);
  468. case paraitem.paratyp of
  469. vs_var,
  470. vs_out :
  471. begin
  472. if not valid_for_formal_var(left) then
  473. CGMessagePos(left.fileinfo,parser_e_illegal_parameter_list);
  474. end;
  475. vs_const :
  476. begin
  477. if not valid_for_formal_const(left) then
  478. CGMessagePos(left.fileinfo,parser_e_illegal_parameter_list);
  479. end;
  480. end;
  481. end
  482. else
  483. begin
  484. { check if the argument is allowed }
  485. if (paraitem.paratyp in [vs_out,vs_var]) then
  486. valid_for_var(left);
  487. end;
  488. if paraitem.paratyp in [vs_var,vs_const] then
  489. begin
  490. { Causes problems with const ansistrings if also }
  491. { done for vs_const (JM) }
  492. if paraitem.paratyp = vs_var then
  493. set_unique(left);
  494. make_not_regable(left);
  495. end;
  496. { ansistrings out paramaters doesn't need to be }
  497. { unique, they are finalized }
  498. if paraitem.paratyp=vs_out then
  499. make_not_regable(left);
  500. if do_count then
  501. begin
  502. if paraitem.paratyp in [vs_var,vs_out] then
  503. set_varstate(left,vs_used,false)
  504. else
  505. set_varstate(left,vs_used,true);
  506. end;
  507. { must only be done after typeconv PM }
  508. resulttype:=paraitem.paratype;
  509. end;
  510. end;
  511. { process next node }
  512. if assigned(right) then
  513. tcallparanode(right).insert_typeconv(do_count);
  514. dec(parsing_para_level);
  515. {$ifdef extdebug}
  516. if do_count then
  517. count_ref:=store_count_ref;
  518. {$endif def extdebug}
  519. end;
  520. procedure tcallparanode.det_registers;
  521. begin
  522. if assigned(right) then
  523. begin
  524. tcallparanode(right).det_registers;
  525. registersint:=right.registersint;
  526. registersfpu:=right.registersfpu;
  527. {$ifdef SUPPORT_MMX}
  528. registersmmx:=right.registersmmx;
  529. {$endif}
  530. end;
  531. firstpass(left);
  532. if left.registersint>registersint then
  533. registersint:=left.registersint;
  534. if left.registersfpu>registersfpu then
  535. registersfpu:=left.registersfpu;
  536. {$ifdef SUPPORT_MMX}
  537. if left.registersmmx>registersmmx then
  538. registersmmx:=left.registersmmx;
  539. {$endif SUPPORT_MMX}
  540. end;
  541. procedure tcallparanode.firstcallparan;
  542. begin
  543. if not assigned(left.resulttype.def) then
  544. get_paratype;
  545. det_registers;
  546. end;
  547. function tcallparanode.docompare(p: tnode): boolean;
  548. begin
  549. docompare :=
  550. inherited docompare(p) and
  551. (callparaflags = tcallparanode(p).callparaflags)
  552. ;
  553. end;
  554. procedure tcallparanode.printnodetree(var t:text);
  555. begin
  556. printnodelist(t);
  557. end;
  558. {****************************************************************************
  559. TCALLNODE
  560. ****************************************************************************}
  561. constructor tcallnode.create(l:tnode;v : tprocsym;st : tsymtable; mp: tnode; callflags:tcallnodeflags);
  562. begin
  563. inherited create(calln,l,nil);
  564. symtableprocentry:=v;
  565. symtableproc:=st;
  566. callnodeflags:=callflags+[cnf_return_value_used];
  567. methodpointer:=mp;
  568. methodpointerinit:=nil;
  569. methodpointerdone:=nil;
  570. procdefinition:=nil;
  571. _funcretnode:=nil;
  572. inlinecode:=nil;
  573. paralength:=-1;
  574. varargsparas:=nil;
  575. end;
  576. constructor tcallnode.create_procvar(l,r:tnode);
  577. begin
  578. inherited create(calln,l,r);
  579. symtableprocentry:=nil;
  580. symtableproc:=nil;
  581. methodpointer:=nil;
  582. methodpointerinit:=nil;
  583. methodpointerdone:=nil;
  584. procdefinition:=nil;
  585. callnodeflags:=[cnf_return_value_used];
  586. _funcretnode:=nil;
  587. inlinecode:=nil;
  588. paralength:=-1;
  589. varargsparas:=nil;
  590. end;
  591. constructor tcallnode.createintern(const name: string; params: tnode);
  592. var
  593. srsym: tsym;
  594. symowner: tsymtable;
  595. begin
  596. if not (cs_compilesystem in aktmoduleswitches) then
  597. begin
  598. srsym := searchsymonlyin(systemunit,name);
  599. symowner := systemunit;
  600. end
  601. else
  602. begin
  603. searchsym(name,srsym,symowner);
  604. if not assigned(srsym) then
  605. searchsym(upper(name),srsym,symowner);
  606. end;
  607. if not assigned(srsym) or
  608. (srsym.typ <> procsym) then
  609. begin
  610. {$ifdef EXTDEBUG}
  611. Comment(V_Error,'unknown compilerproc '+name);
  612. {$endif EXTDEBUG}
  613. internalerror(200107271);
  614. end;
  615. self.create(params,tprocsym(srsym),symowner,nil,[]);
  616. end;
  617. constructor tcallnode.createinternres(const name: string; params: tnode; const res: ttype);
  618. begin
  619. self.createintern(name,params);
  620. restype := res;
  621. include(callnodeflags,cnf_restypeset);
  622. { both the normal and specified resulttype either have to be returned via a }
  623. { parameter or not, but no mixing (JM) }
  624. if paramanager.ret_in_param(restype.def,pocall_compilerproc) xor
  625. paramanager.ret_in_param(symtableprocentry.first_procdef.rettype.def,symtableprocentry.first_procdef.proccalloption) then
  626. internalerror(200108291);
  627. end;
  628. constructor tcallnode.createinternreturn(const name: string; params: tnode; returnnode : tnode);
  629. begin
  630. self.createintern(name,params);
  631. _funcretnode:=returnnode;
  632. if not paramanager.ret_in_param(symtableprocentry.first_procdef.rettype.def,symtableprocentry.first_procdef.proccalloption) then
  633. internalerror(200204247);
  634. end;
  635. procedure tcallnode.setfuncretnode(const returnnode: tnode);
  636. var
  637. para: tcallparanode;
  638. begin
  639. if assigned(_funcretnode) then
  640. _funcretnode.free;
  641. _funcretnode := returnnode;
  642. { if the resulttype pass hasn't occurred yet, that one will do }
  643. { everything }
  644. if assigned(resulttype.def) then
  645. begin
  646. { these are returned as values, but we can optimize their loading }
  647. { as well }
  648. if is_ansistring(resulttype.def) or
  649. is_widestring(resulttype.def) then
  650. exit;
  651. para := tcallparanode(left);
  652. while assigned(para) do
  653. begin
  654. if para.paraitem.is_hidden and
  655. (vo_is_funcret in tvarsym(para.paraitem.parasym).varoptions) then
  656. begin
  657. para.left.free;
  658. para.left := _funcretnode.getcopy;
  659. exit;
  660. end;
  661. para := tcallparanode(para.right);
  662. end;
  663. { no hidden resultpara found, error! }
  664. if not(procdefinition.proccalloption = pocall_inline) then
  665. internalerror(200306087);
  666. end;
  667. end;
  668. destructor tcallnode.destroy;
  669. begin
  670. methodpointer.free;
  671. methodpointerinit.free;
  672. methodpointerdone.free;
  673. _funcretnode.free;
  674. inlinecode.free;
  675. if assigned(varargsparas) then
  676. varargsparas.free;
  677. inherited destroy;
  678. end;
  679. constructor tcallnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  680. begin
  681. inherited ppuload(t,ppufile);
  682. ppufile.getderef(symtableprocentryderef);
  683. {$ifdef fpc}
  684. {$warning FIXME: No withsymtable support}
  685. {$endif}
  686. symtableproc:=nil;
  687. ppufile.getderef(procdefinitionderef);
  688. ppufile.getsmallset(callnodeflags);
  689. methodpointer:=ppuloadnode(ppufile);
  690. methodpointerinit:=ppuloadnode(ppufile);
  691. methodpointerdone:=ppuloadnode(ppufile);
  692. _funcretnode:=ppuloadnode(ppufile);
  693. inlinecode:=ppuloadnode(ppufile);
  694. end;
  695. procedure tcallnode.ppuwrite(ppufile:tcompilerppufile);
  696. begin
  697. inherited ppuwrite(ppufile);
  698. ppufile.putderef(symtableprocentryderef);
  699. ppufile.putderef(procdefinitionderef);
  700. ppufile.putsmallset(callnodeflags);
  701. ppuwritenode(ppufile,methodpointer);
  702. ppuwritenode(ppufile,methodpointerinit);
  703. ppuwritenode(ppufile,methodpointerdone);
  704. ppuwritenode(ppufile,_funcretnode);
  705. ppuwritenode(ppufile,inlinecode);
  706. end;
  707. procedure tcallnode.buildderefimpl;
  708. begin
  709. inherited buildderefimpl;
  710. symtableprocentryderef.build(symtableprocentry);
  711. procdefinitionderef.build(procdefinition);
  712. if assigned(methodpointer) then
  713. methodpointer.buildderefimpl;
  714. if assigned(methodpointerinit) then
  715. methodpointerinit.buildderefimpl;
  716. if assigned(methodpointerdone) then
  717. methodpointerdone.buildderefimpl;
  718. if assigned(_funcretnode) then
  719. _funcretnode.buildderefimpl;
  720. if assigned(inlinecode) then
  721. inlinecode.buildderefimpl;
  722. end;
  723. procedure tcallnode.derefimpl;
  724. var
  725. pt : tcallparanode;
  726. currpara : tparaitem;
  727. begin
  728. inherited derefimpl;
  729. symtableprocentry:=tprocsym(symtableprocentryderef.resolve);
  730. symtableproc:=symtableprocentry.owner;
  731. procdefinition:=tprocdef(procdefinitionderef.resolve);
  732. if assigned(methodpointer) then
  733. methodpointer.derefimpl;
  734. if assigned(methodpointerinit) then
  735. methodpointerinit.derefimpl;
  736. if assigned(methodpointerdone) then
  737. methodpointerdone.derefimpl;
  738. if assigned(_funcretnode) then
  739. _funcretnode.derefimpl;
  740. if assigned(inlinecode) then
  741. inlinecode.derefimpl;
  742. { Connect paraitems }
  743. pt:=tcallparanode(left);
  744. while assigned(pt) and
  745. (cpf_varargs_para in pt.callparaflags) do
  746. pt:=tcallparanode(pt.right);
  747. currpara:=tparaitem(procdefinition.Para.last);
  748. while assigned(currpara) do
  749. begin
  750. if not assigned(pt) then
  751. internalerror(200311077);
  752. pt.paraitem:=currpara;
  753. pt:=tcallparanode(pt.right);
  754. currpara:=tparaitem(currpara.previous);
  755. end;
  756. if assigned(currpara) or assigned(pt) then
  757. internalerror(200311078);
  758. end;
  759. function tcallnode.getcopy : tnode;
  760. var
  761. n : tcallnode;
  762. hp : tparaitem;
  763. oldleft : tnode;
  764. begin
  765. { Need to use a hack here to prevent the parameters from being copied.
  766. The parameters must be copied between methodpointerinit/methodpointerdone because
  767. the can reference methodpointer }
  768. oldleft:=left;
  769. left:=nil;
  770. n:=tcallnode(inherited getcopy);
  771. left:=oldleft;
  772. n.symtableprocentry:=symtableprocentry;
  773. n.symtableproc:=symtableproc;
  774. n.procdefinition:=procdefinition;
  775. n.restype := restype;
  776. n.callnodeflags := callnodeflags;
  777. if assigned(methodpointerinit) then
  778. n.methodpointerinit:=methodpointerinit.getcopy
  779. else
  780. n.methodpointerinit:=nil;
  781. { methodpointerinit is copied, now references to the temp will also be copied
  782. correctly. We can now copy the parameters and methodpointer }
  783. if assigned(left) then
  784. n.left:=left.getcopy
  785. else
  786. n.left:=nil;
  787. if assigned(methodpointer) then
  788. n.methodpointer:=methodpointer.getcopy
  789. else
  790. n.methodpointer:=nil;
  791. if assigned(methodpointerdone) then
  792. n.methodpointerdone:=methodpointerdone.getcopy
  793. else
  794. n.methodpointerdone:=nil;
  795. if assigned(_funcretnode) then
  796. n._funcretnode:=_funcretnode.getcopy
  797. else
  798. n._funcretnode:=nil;
  799. if assigned(inlinecode) then
  800. n.inlinecode:=inlinecode.getcopy
  801. else
  802. n.inlinecode:=nil;
  803. if assigned(varargsparas) then
  804. begin
  805. n.varargsparas:=tvarargspara.create;
  806. hp:=tparaitem(varargsparas.first);
  807. while assigned(hp) do
  808. begin
  809. n.varargsparas.concat(hp.getcopy);
  810. hp:=tparaitem(hp.next);
  811. end;
  812. end
  813. else
  814. n.varargsparas:=nil;
  815. result:=n;
  816. end;
  817. procedure tcallnode.insertintolist(l : tnodelist);
  818. begin
  819. end;
  820. procedure tcallnode.convert_carg_array_of_const;
  821. var
  822. hp : tarrayconstructornode;
  823. oldleft : tcallparanode;
  824. begin
  825. oldleft:=tcallparanode(left);
  826. if oldleft.left.nodetype<>arrayconstructorn then
  827. begin
  828. CGMessage1(type_e_wrong_type_in_array_constructor,oldleft.left.resulttype.def.typename);
  829. exit;
  830. end;
  831. { Get arrayconstructor node and insert typeconvs }
  832. hp:=tarrayconstructornode(oldleft.left);
  833. hp.insert_typeconvs;
  834. { Add c args parameters }
  835. { It could be an empty set }
  836. if assigned(hp) and
  837. assigned(hp.left) then
  838. begin
  839. while assigned(hp) do
  840. begin
  841. left:=ccallparanode.create(hp.left,left);
  842. { set callparanode resulttype and flags }
  843. left.resulttype:=hp.left.resulttype;
  844. include(tcallparanode(left).callparaflags,cpf_varargs_para);
  845. hp.left:=nil;
  846. hp:=tarrayconstructornode(hp.right);
  847. end;
  848. end;
  849. { Remove value of old array of const parameter, but keep it
  850. in the list because it is required for bind_paraitem.
  851. Generate a nothign to keep callparanoed.left valid }
  852. oldleft.left.free;
  853. oldleft.left:=cnothingnode.create;
  854. end;
  855. procedure tcallnode.verifyabstract(p : tnamedindexitem;arg:pointer);
  856. var
  857. hp : tprocdef;
  858. j: integer;
  859. begin
  860. if (tsym(p).typ=procsym) then
  861. begin
  862. for j:=1 to tprocsym(p).procdef_count do
  863. begin
  864. { index starts at 1 }
  865. hp:=tprocsym(p).procdef[j];
  866. { If this is an abstract method insert into the list }
  867. if (po_abstractmethod in hp.procoptions) then
  868. AbstractMethodsList.Insert(hp.procsym.name)
  869. else
  870. { If this symbol is already in the list, and it is
  871. an overriding method or dynamic, then remove it from the list
  872. }
  873. begin
  874. { symbol was found }
  875. if AbstractMethodsList.Find(hp.procsym.name) <> nil then
  876. begin
  877. if po_overridingmethod in hp.procoptions then
  878. AbstractMethodsList.Remove(hp.procsym.name);
  879. end;
  880. end;
  881. end;
  882. end;
  883. end;
  884. procedure tcallnode.verifyabstractcalls;
  885. var
  886. objectdf : tobjectdef;
  887. parents : tlinkedlist;
  888. objectinfo : tobjectinfoitem;
  889. stritem : tstringlistitem;
  890. begin
  891. objectdf := nil;
  892. { verify if trying to create an instance of a class which contains
  893. non-implemented abstract methods }
  894. { first verify this class type, no class than exit }
  895. { also, this checking can only be done if the constructor is directly
  896. called, indirect constructor calls cannot be checked.
  897. }
  898. if assigned(methodpointer) then
  899. begin
  900. if (methodpointer.resulttype.def.deftype = objectdef) then
  901. objectdf:=tobjectdef(methodpointer.resulttype.def)
  902. else
  903. if (methodpointer.resulttype.def.deftype = classrefdef) and
  904. (tclassrefdef(methodpointer.resulttype.def).pointertype.def.deftype = objectdef) and
  905. (methodpointer.nodetype in [typen,loadvmtaddrn]) then
  906. objectdf:=tobjectdef(tclassrefdef(methodpointer.resulttype.def).pointertype.def);
  907. end;
  908. if not assigned(objectdf) then
  909. exit;
  910. parents := tlinkedlist.create;
  911. AbstractMethodsList := tstringlist.create;
  912. { insert all parents in this class : the first item in the
  913. list will be the base parent of the class .
  914. }
  915. while assigned(objectdf) do
  916. begin
  917. objectinfo:=tobjectinfoitem.create(objectdf);
  918. parents.insert(objectinfo);
  919. objectdf := objectdf.childof;
  920. end;
  921. { now all parents are in the correct order
  922. insert all abstract methods in the list, and remove
  923. those which are overriden by parent classes.
  924. }
  925. objectinfo:=tobjectinfoitem(parents.first);
  926. while assigned(objectinfo) do
  927. begin
  928. objectdf := objectinfo.objinfo;
  929. if assigned(objectdf.symtable) then
  930. objectdf.symtable.foreach({$ifdef FPCPROCVAR}@{$endif}verifyabstract,nil);
  931. objectinfo:=tobjectinfoitem(objectinfo.next);
  932. end;
  933. if assigned(parents) then
  934. parents.free;
  935. { Finally give out a warning for each abstract method still in the list }
  936. stritem := tstringlistitem(AbstractMethodsList.first);
  937. if assigned(stritem) then
  938. Message1(type_w_instance_with_abstract,objectdf.objname^);
  939. while assigned(stritem) do
  940. begin
  941. if assigned(stritem.fpstr) then
  942. Message1(sym_h_param_list,stritem.str);
  943. stritem := tstringlistitem(stritem.next);
  944. end;
  945. if assigned(AbstractMethodsList) then
  946. AbstractMethodsList.Free;
  947. end;
  948. function tcallnode.gen_self_tree_methodpointer:tnode;
  949. var
  950. hsym : tvarsym;
  951. begin
  952. { find self field in methodpointer record }
  953. hsym:=tvarsym(trecorddef(methodpointertype.def).symtable.search('self'));
  954. if not assigned(hsym) then
  955. internalerror(200305251);
  956. { Load tmehodpointer(right).self }
  957. result:=csubscriptnode.create(
  958. hsym,
  959. ctypeconvnode.create_explicit(right.getcopy,methodpointertype));
  960. end;
  961. function tcallnode.gen_self_tree:tnode;
  962. var
  963. selftree : tnode;
  964. begin
  965. selftree:=nil;
  966. { inherited }
  967. if (cnf_inherited in callnodeflags) then
  968. selftree:=load_self_node
  969. else
  970. { constructors }
  971. if (procdefinition.proctypeoption=potype_constructor) then
  972. begin
  973. { push 0 as self when allocation is needed }
  974. if (methodpointer.resulttype.def.deftype=classrefdef) or
  975. (cnf_new_call in callnodeflags) then
  976. selftree:=cpointerconstnode.create(0,voidpointertype)
  977. else
  978. begin
  979. if methodpointer.nodetype=typen then
  980. selftree:=load_self_node
  981. else
  982. selftree:=methodpointer.getcopy;
  983. end;
  984. end
  985. else
  986. { Calling a static/class method }
  987. if (po_classmethod in procdefinition.procoptions) or
  988. (po_staticmethod in procdefinition.procoptions) then
  989. begin
  990. if (procdefinition.deftype<>procdef) then
  991. internalerror(200305062);
  992. if (oo_has_vmt in tprocdef(procdefinition)._class.objectoptions) then
  993. begin
  994. { we only need the vmt, loading self is not required and there is no
  995. need to check for typen, because that will always get the
  996. loadvmtaddrnode added }
  997. selftree:=methodpointer.getcopy;
  998. if methodpointer.resulttype.def.deftype<>classrefdef then
  999. selftree:=cloadvmtaddrnode.create(selftree);
  1000. end
  1001. else
  1002. selftree:=cpointerconstnode.create(0,voidpointertype);
  1003. end
  1004. else
  1005. begin
  1006. if methodpointer.nodetype=typen then
  1007. selftree:=load_self_node
  1008. else
  1009. selftree:=methodpointer.getcopy;
  1010. end;
  1011. result:=selftree;
  1012. end;
  1013. function tcallnode.gen_vmt_tree:tnode;
  1014. var
  1015. vmttree : tnode;
  1016. begin
  1017. vmttree:=nil;
  1018. if not(procdefinition.proctypeoption in [potype_constructor,potype_destructor]) then
  1019. internalerror(200305051);
  1020. { inherited call, no create/destroy }
  1021. if (cnf_inherited in callnodeflags) then
  1022. vmttree:=cpointerconstnode.create(0,voidpointertype)
  1023. else
  1024. { do not create/destroy when called from member function
  1025. without specifying self explicit }
  1026. if (cnf_member_call in callnodeflags) then
  1027. begin
  1028. if (methodpointer.resulttype.def.deftype=classrefdef) and
  1029. (procdefinition.proctypeoption=potype_constructor) then
  1030. vmttree:=methodpointer.getcopy
  1031. else
  1032. vmttree:=cpointerconstnode.create(0,voidpointertype);
  1033. end
  1034. else
  1035. { constructor with extended syntax called from new }
  1036. if (cnf_new_call in callnodeflags) then
  1037. vmttree:=cloadvmtaddrnode.create(ctypenode.create(methodpointer.resulttype))
  1038. else
  1039. { destructor with extended syntax called from dispose }
  1040. if (cnf_dispose_call in callnodeflags) then
  1041. vmttree:=cloadvmtaddrnode.create(methodpointer.getcopy)
  1042. else
  1043. if (methodpointer.resulttype.def.deftype=classrefdef) then
  1044. begin
  1045. { constructor call via classreference => allocate memory }
  1046. if (procdefinition.proctypeoption=potype_constructor) and
  1047. is_class(tclassrefdef(methodpointer.resulttype.def).pointertype.def) then
  1048. begin
  1049. vmttree:=methodpointer.getcopy;
  1050. { Only a typenode can be passed when it is called with <class of xx>.create }
  1051. if vmttree.nodetype=typen then
  1052. vmttree:=cloadvmtaddrnode.create(vmttree);
  1053. end
  1054. else
  1055. vmttree:=cpointerconstnode.create(0,voidpointertype);
  1056. end
  1057. else
  1058. { class }
  1059. if is_class(methodpointer.resulttype.def) then
  1060. begin
  1061. { destructor: release instance, flag(vmt)=1
  1062. constructor: direct call, do nothing, leave vmt=0 }
  1063. if (procdefinition.proctypeoption=potype_destructor) then
  1064. vmttree:=cpointerconstnode.create(1,voidpointertype)
  1065. else
  1066. vmttree:=cpointerconstnode.create(0,voidpointertype);
  1067. end
  1068. else
  1069. { object }
  1070. begin
  1071. { destructor: direct call, no dispose, vmt=0
  1072. constructor: initialize object, load vmt }
  1073. if (procdefinition.proctypeoption=potype_constructor) then
  1074. { old styled inherited call? }
  1075. if (methodpointer.nodetype=typen) then
  1076. vmttree:=cpointerconstnode.create(0,voidpointertype)
  1077. else
  1078. vmttree:=cloadvmtaddrnode.create(ctypenode.create(methodpointer.resulttype))
  1079. else
  1080. vmttree:=cpointerconstnode.create(0,voidpointertype);
  1081. end;
  1082. result:=vmttree;
  1083. end;
  1084. procedure tcallnode.bind_paraitem;
  1085. var
  1086. i : integer;
  1087. pt : tcallparanode;
  1088. oldppt : ^tcallparanode;
  1089. varargspara,
  1090. currpara : tparaitem;
  1091. used_by_callnode : boolean;
  1092. hiddentree : tnode;
  1093. newstatement : tstatementnode;
  1094. temp : ttempcreatenode;
  1095. begin
  1096. pt:=tcallparanode(left);
  1097. oldppt:=@left;
  1098. { flag all callparanodes that belong to the varargs }
  1099. i:=paralength;
  1100. while (i>procdefinition.maxparacount) do
  1101. begin
  1102. include(pt.callparaflags,cpf_varargs_para);
  1103. oldppt:[email protected];
  1104. pt:=tcallparanode(pt.right);
  1105. dec(i);
  1106. end;
  1107. { skip varargs that are inserted by array of const }
  1108. while assigned(pt) and
  1109. (cpf_varargs_para in pt.callparaflags) do
  1110. pt:=tcallparanode(pt.right);
  1111. { process normal parameters and insert hidden parameters }
  1112. currpara:=tparaitem(procdefinition.Para.last);
  1113. while assigned(currpara) do
  1114. begin
  1115. if currpara.is_hidden then
  1116. begin
  1117. { generate hidden tree }
  1118. used_by_callnode:=false;
  1119. hiddentree:=nil;
  1120. if (vo_is_funcret in tvarsym(currpara.parasym).varoptions) then
  1121. begin
  1122. { Generate funcretnode if not specified }
  1123. if assigned(funcretnode) then
  1124. begin
  1125. hiddentree:=funcretnode.getcopy;
  1126. end
  1127. else
  1128. begin
  1129. hiddentree:=internalstatements(newstatement);
  1130. { need to use resulttype instead of procdefinition.rettype,
  1131. because they can be different }
  1132. temp:=ctempcreatenode.create(resulttype,resulttype.def.size,tt_persistent);
  1133. addstatement(newstatement,temp);
  1134. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  1135. addstatement(newstatement,ctemprefnode.create(temp));
  1136. end;
  1137. end
  1138. else
  1139. if vo_is_high_value in tvarsym(currpara.parasym).varoptions then
  1140. begin
  1141. if not assigned(pt) then
  1142. internalerror(200304082);
  1143. { we need the information of the previous parameter }
  1144. hiddentree:=gen_high_tree(pt.left,tparaitem(currpara.previous).paratype.def);
  1145. end
  1146. else
  1147. if vo_is_self in tvarsym(currpara.parasym).varoptions then
  1148. begin
  1149. if assigned(right) then
  1150. hiddentree:=gen_self_tree_methodpointer
  1151. else
  1152. hiddentree:=gen_self_tree;
  1153. end
  1154. else
  1155. if vo_is_vmt in tvarsym(currpara.parasym).varoptions then
  1156. begin
  1157. hiddentree:=gen_vmt_tree;
  1158. end
  1159. else
  1160. if vo_is_parentfp in tvarsym(currpara.parasym).varoptions then
  1161. begin
  1162. if not(assigned(procdefinition.owner.defowner)) then
  1163. internalerror(200309287);
  1164. hiddentree:=cloadparentfpnode.create(tprocdef(procdefinition.owner.defowner));
  1165. end
  1166. {$ifdef powerpc}
  1167. else
  1168. { lib parameter has no special type but proccalloptions must be a syscall }
  1169. if (target_info.system in [system_powerpc_morphos,system_m68k_amiga]) and
  1170. (procdefinition.proccalloption=pocall_syscall) then
  1171. begin
  1172. hiddentree:=cloadnode.create(tprocdef(procdefinition).libsym,tprocdef(procdefinition).libsym.owner);
  1173. end
  1174. {$endif powerpc}
  1175. else
  1176. { add the hidden parameter }
  1177. if not assigned(hiddentree) then
  1178. internalerror(200304073);
  1179. { Already insert para and let the previous node point to
  1180. this new node }
  1181. pt:=ccallparanode.create(hiddentree,oldppt^);
  1182. pt.used_by_callnode:=used_by_callnode;
  1183. oldppt^:=pt;
  1184. end;
  1185. if not assigned(pt) then
  1186. internalerror(200310052);
  1187. pt.paraitem:=currpara;
  1188. oldppt:[email protected];
  1189. pt:=tcallparanode(pt.right);
  1190. currpara:=tparaitem(currpara.previous)
  1191. end;
  1192. { Create paraitems for varargs }
  1193. pt:=tcallparanode(left);
  1194. while assigned(pt) do
  1195. begin
  1196. if cpf_varargs_para in pt.callparaflags then
  1197. begin
  1198. if not assigned(varargsparas) then
  1199. varargsparas:=tvarargspara.create;
  1200. varargspara:=tparaitem.create;
  1201. varargspara.paratyp:=vs_value;
  1202. varargspara.paratype:=pt.resulttype;
  1203. { varargspara is left-right, use insert
  1204. instead of concat }
  1205. varargsparas.insert(varargspara);
  1206. pt.paraitem:=varargspara;
  1207. end;
  1208. pt:=tcallparanode(pt.right);
  1209. end;
  1210. end;
  1211. function tcallnode.det_resulttype:tnode;
  1212. var
  1213. candidates : tcallcandidates;
  1214. oldcallnode : tcallnode;
  1215. hpt : tnode;
  1216. pt : tcallparanode;
  1217. lastpara : longint;
  1218. currpara : tparaitem;
  1219. cand_cnt : integer;
  1220. i : longint;
  1221. method_must_be_valid,
  1222. is_const : boolean;
  1223. hp : tnode;
  1224. mptemp : ttempcreatenode;
  1225. newstatement : tstatementnode;
  1226. label
  1227. errorexit;
  1228. begin
  1229. result:=nil;
  1230. candidates:=nil;
  1231. oldcallnode:=aktcallnode;
  1232. aktcallnode:=nil;
  1233. { determine length of parameter list }
  1234. pt:=tcallparanode(left);
  1235. paralength:=0;
  1236. while assigned(pt) do
  1237. begin
  1238. inc(paralength);
  1239. pt:=tcallparanode(pt.right);
  1240. end;
  1241. { determine the type of the parameters }
  1242. if assigned(left) then
  1243. begin
  1244. tcallparanode(left).get_paratype;
  1245. if codegenerror then
  1246. goto errorexit;
  1247. end;
  1248. if assigned(methodpointer) then
  1249. begin
  1250. resulttypepass(methodpointer);
  1251. hp:=methodpointer;
  1252. while assigned(hp) and
  1253. (hp.nodetype=typeconvn) do
  1254. hp:=tunarynode(hp).left;
  1255. if assigned(hp) and
  1256. (
  1257. { call result must always be loaded in temp to prevent
  1258. double creation }
  1259. (hp.nodetype=calln)
  1260. { Also optimize also complex loads }
  1261. {$warning Complex loads can also be optimized}
  1262. // not(hp.nodetype in [typen,loadvmtaddrn,loadn])
  1263. ) then
  1264. begin
  1265. { methodpointer loading }
  1266. methodpointerinit:=internalstatements(newstatement);
  1267. mptemp:=ctempcreatenode.create_reg(methodpointer.resulttype,methodpointer.resulttype.def.size,tt_persistent);
  1268. addstatement(newstatement,mptemp);
  1269. addstatement(newstatement,cassignmentnode.create(
  1270. ctemprefnode.create(mptemp),
  1271. methodpointer));
  1272. resulttypepass(methodpointerinit);
  1273. { new methodpointer is only a temp reference }
  1274. methodpointer:=ctemprefnode.create(mptemp);
  1275. resulttypepass(methodpointer);
  1276. { methodpointer cleanup }
  1277. methodpointerdone:=ctempdeletenode.create(mptemp);
  1278. resulttypepass(methodpointerdone);
  1279. end;
  1280. end;
  1281. { procedure variable ? }
  1282. if assigned(right) then
  1283. begin
  1284. set_varstate(right,vs_used,true);
  1285. resulttypepass(right);
  1286. if codegenerror then
  1287. exit;
  1288. procdefinition:=tabstractprocdef(right.resulttype.def);
  1289. { Compare parameters from right to left }
  1290. currpara:=tparaitem(procdefinition.Para.last);
  1291. { Skip default parameters }
  1292. if not(po_varargs in procdefinition.procoptions) then
  1293. begin
  1294. { ignore hidden parameters }
  1295. while assigned(currpara) and (currpara.is_hidden) do
  1296. currpara:=tparaitem(currpara.previous);
  1297. for i:=1 to procdefinition.maxparacount-paralength do
  1298. begin
  1299. if not assigned(currpara) then
  1300. internalerror(200402261);
  1301. if not assigned(currpara.defaultvalue) then
  1302. begin
  1303. CGMessage(parser_e_wrong_parameter_size);
  1304. goto errorexit;
  1305. end;
  1306. currpara:=tparaitem(currpara.previous);
  1307. end;
  1308. end;
  1309. while assigned(currpara) and (currpara.is_hidden) do
  1310. currpara:=tparaitem(currpara.previous);
  1311. pt:=tcallparanode(left);
  1312. lastpara:=paralength;
  1313. while assigned(currpara) and assigned(pt) do
  1314. begin
  1315. { only goto next para if we're out of the varargs }
  1316. if not(po_varargs in procdefinition.procoptions) or
  1317. (lastpara<=procdefinition.maxparacount) then
  1318. begin
  1319. repeat
  1320. currpara:=tparaitem(currpara.previous);
  1321. until (not assigned(currpara)) or (not currpara.is_hidden);
  1322. end;
  1323. pt:=tcallparanode(pt.right);
  1324. dec(lastpara);
  1325. end;
  1326. if assigned(pt) or
  1327. (assigned(currpara) and
  1328. not assigned(currpara.defaultvalue)) then
  1329. begin
  1330. if assigned(pt) then
  1331. aktfilepos:=pt.fileinfo;
  1332. CGMessage(parser_e_wrong_parameter_size);
  1333. goto errorexit;
  1334. end;
  1335. end
  1336. else
  1337. { not a procedure variable }
  1338. begin
  1339. { do we know the procedure to call ? }
  1340. if not(assigned(procdefinition)) then
  1341. begin
  1342. candidates:=tcallcandidates.create(symtableprocentry,symtableproc,left,(nf_isproperty in flags));
  1343. { no procedures found? then there is something wrong
  1344. with the parameter size or the procedures are
  1345. not accessible }
  1346. if candidates.count=0 then
  1347. begin
  1348. { when it's an auto inherited call and there
  1349. is no procedure found, but the procedures
  1350. were defined with overload directive and at
  1351. least two procedures are defined then we ignore
  1352. this inherited by inserting a nothingn. Only
  1353. do this ugly hack in Delphi mode as it looks more
  1354. like a bug. It's also not documented }
  1355. if (m_delphi in aktmodeswitches) and
  1356. (cnf_anon_inherited in callnodeflags) and
  1357. (symtableprocentry.owner.symtabletype=objectsymtable) and
  1358. (po_overload in symtableprocentry.first_procdef.procoptions) and
  1359. (symtableprocentry.procdef_count>=2) then
  1360. result:=cnothingnode.create
  1361. else
  1362. begin
  1363. { in tp mode we can try to convert to procvar if
  1364. there are no parameters specified. Only try it
  1365. when there is only one proc definition, else the
  1366. loadnode will give a strange error }
  1367. if not(assigned(left)) and
  1368. not(cnf_inherited in callnodeflags) and
  1369. (m_tp_procvar in aktmodeswitches) and
  1370. (symtableprocentry.procdef_count=1) then
  1371. begin
  1372. hpt:=cloadnode.create(tprocsym(symtableprocentry),symtableproc);
  1373. if assigned(methodpointer) then
  1374. tloadnode(hpt).set_mp(methodpointer.getcopy);
  1375. resulttypepass(hpt);
  1376. result:=hpt;
  1377. end
  1378. else
  1379. begin
  1380. if assigned(left) then
  1381. aktfilepos:=left.fileinfo;
  1382. CGMessage(parser_e_wrong_parameter_size);
  1383. symtableprocentry.write_parameter_lists(nil);
  1384. end;
  1385. end;
  1386. goto errorexit;
  1387. end;
  1388. { Retrieve information about the candidates }
  1389. candidates.get_information;
  1390. {$ifdef EXTDEBUG}
  1391. { Display info when multiple candidates are found }
  1392. if candidates.count>1 then
  1393. candidates.dump_info(V_Debug);
  1394. {$endif EXTDEBUG}
  1395. { Choose the best candidate and count the number of
  1396. candidates left }
  1397. cand_cnt:=candidates.choose_best(procdefinition);
  1398. { All parameters are checked, check if there are any
  1399. procedures left }
  1400. if cand_cnt>0 then
  1401. begin
  1402. { Multiple candidates left? }
  1403. if cand_cnt>1 then
  1404. begin
  1405. CGMessage(type_e_cant_choose_overload_function);
  1406. {$ifdef EXTDEBUG}
  1407. candidates.dump_info(V_Hint);
  1408. {$else EXTDEBUG}
  1409. candidates.list(false);
  1410. {$endif EXTDEBUG}
  1411. { we'll just use the first candidate to make the
  1412. call }
  1413. end;
  1414. { assign procdefinition }
  1415. if symtableproc=nil then
  1416. symtableproc:=procdefinition.owner;
  1417. { update browser information }
  1418. if make_ref then
  1419. begin
  1420. tprocdef(procdefinition).lastref:=tref.create(tprocdef(procdefinition).lastref,@fileinfo);
  1421. inc(tprocdef(procdefinition).refcount);
  1422. if tprocdef(procdefinition).defref=nil then
  1423. tprocdef(procdefinition).defref:=tprocdef(procdefinition).lastref;
  1424. end;
  1425. end
  1426. else
  1427. begin
  1428. { No candidates left, this must be a type error,
  1429. because wrong size is already checked. procdefinition
  1430. is filled with the first (random) definition that is
  1431. found. We use this definition to display a nice error
  1432. message that the wrong type is passed }
  1433. candidates.find_wrong_para;
  1434. candidates.list(true);
  1435. {$ifdef EXTDEBUG}
  1436. candidates.dump_info(V_Hint);
  1437. {$endif EXTDEBUG}
  1438. { We can not proceed, release all procs and exit }
  1439. candidates.free;
  1440. goto errorexit;
  1441. end;
  1442. candidates.free;
  1443. end; { end of procedure to call determination }
  1444. end;
  1445. { add needed default parameters }
  1446. if assigned(procdefinition) and
  1447. (paralength<procdefinition.maxparacount) then
  1448. begin
  1449. currpara:=tparaitem(procdefinition.Para.first);
  1450. i:=0;
  1451. while (i<paralength) do
  1452. begin
  1453. if not assigned(currpara) then
  1454. internalerror(200306181);
  1455. if not currpara.is_hidden then
  1456. inc(i);
  1457. currpara:=tparaitem(currpara.next);
  1458. end;
  1459. while assigned(currpara) and currpara.is_hidden do
  1460. currpara:=tparaitem(currpara.next);
  1461. while assigned(currpara) do
  1462. begin
  1463. if not assigned(currpara.defaultvalue) then
  1464. internalerror(200212142);
  1465. left:=ccallparanode.create(genconstsymtree(tconstsym(currpara.defaultvalue)),left);
  1466. { Ignore vs_hidden parameters }
  1467. repeat
  1468. currpara:=tparaitem(currpara.next);
  1469. until (not assigned(currpara)) or (not currpara.is_hidden);
  1470. end;
  1471. end;
  1472. { handle predefined procedures }
  1473. is_const:=(po_internconst in procdefinition.procoptions) and
  1474. ((block_type in [bt_const,bt_type]) or
  1475. (assigned(left) and (tcallparanode(left).left.nodetype in [realconstn,ordconstn])));
  1476. if (procdefinition.proccalloption=pocall_internproc) or is_const then
  1477. begin
  1478. if assigned(left) then
  1479. begin
  1480. { ptr and settextbuf needs two args }
  1481. if assigned(tcallparanode(left).right) then
  1482. begin
  1483. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,left);
  1484. left:=nil;
  1485. end
  1486. else
  1487. begin
  1488. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,tcallparanode(left).left);
  1489. tcallparanode(left).left:=nil;
  1490. end;
  1491. end
  1492. else
  1493. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,nil);
  1494. result:=hpt;
  1495. goto errorexit;
  1496. end;
  1497. { ensure that the result type is set }
  1498. if not(cnf_restypeset in callnodeflags) then
  1499. begin
  1500. { constructors return their current class type, not the type where the
  1501. constructor is declared, this can be different because of inheritance }
  1502. if (procdefinition.proctypeoption=potype_constructor) and
  1503. assigned(methodpointer) and
  1504. assigned(methodpointer.resulttype.def) and
  1505. (methodpointer.resulttype.def.deftype=classrefdef) then
  1506. resulttype:=tclassrefdef(methodpointer.resulttype.def).pointertype
  1507. else
  1508. resulttype:=procdefinition.rettype;
  1509. end
  1510. else
  1511. resulttype:=restype;
  1512. {if resulttype.def.needs_inittable then
  1513. include(current_procinfo.flags,pi_needs_implicit_finally);}
  1514. if assigned(methodpointer) then
  1515. begin
  1516. { when methodpointer is a callnode we must load it first into a
  1517. temp to prevent the processing callnode twice }
  1518. if (methodpointer.nodetype=calln) then
  1519. internalerror(200405121);
  1520. { direct call to inherited abstract method, then we
  1521. can already give a error in the compiler instead
  1522. of a runtime error }
  1523. if (cnf_inherited in callnodeflags) and
  1524. (po_abstractmethod in procdefinition.procoptions) then
  1525. CGMessage(cg_e_cant_call_abstract_method);
  1526. { if an inherited con- or destructor should be }
  1527. { called in a con- or destructor then a warning }
  1528. { will be made }
  1529. { con- and destructors need a pointer to the vmt }
  1530. if (cnf_inherited in callnodeflags) and
  1531. (procdefinition.proctypeoption in [potype_constructor,potype_destructor]) and
  1532. is_object(methodpointer.resulttype.def) and
  1533. not(current_procinfo.procdef.proctypeoption in [potype_constructor,potype_destructor]) then
  1534. CGMessage(cg_w_member_cd_call_from_method);
  1535. if methodpointer.nodetype<>typen then
  1536. begin
  1537. { Remove all postfix operators }
  1538. hpt:=methodpointer;
  1539. while assigned(hpt) and (hpt.nodetype in [subscriptn,vecn]) do
  1540. hpt:=tunarynode(hpt).left;
  1541. if (procdefinition.proctypeoption=potype_constructor) and
  1542. assigned(symtableproc) and
  1543. (symtableproc.symtabletype=withsymtable) and
  1544. (tnode(twithsymtable(symtableproc).withrefnode).nodetype=temprefn) then
  1545. CGmessage(cg_e_cannot_call_cons_dest_inside_with);
  1546. { R.Init then R will be initialized by the constructor,
  1547. Also allow it for simple loads }
  1548. if (procdefinition.proctypeoption=potype_constructor) or
  1549. ((hpt.nodetype=loadn) and
  1550. (
  1551. (methodpointer.resulttype.def.deftype=classrefdef) or
  1552. (
  1553. (methodpointer.resulttype.def.deftype=objectdef) and
  1554. not(oo_has_virtual in tobjectdef(methodpointer.resulttype.def).objectoptions)
  1555. )
  1556. )
  1557. ) then
  1558. method_must_be_valid:=false
  1559. else
  1560. method_must_be_valid:=true;
  1561. set_varstate(methodpointer,vs_used,method_must_be_valid);
  1562. { The object is already used if it is called once }
  1563. if (hpt.nodetype=loadn) and
  1564. (tloadnode(hpt).symtableentry.typ=varsym) then
  1565. tvarsym(tloadnode(hpt).symtableentry).varstate:=vs_used;
  1566. end;
  1567. { if we are calling the constructor check for abstract
  1568. methods. Ignore inherited and member calls, because the
  1569. class is then already created }
  1570. if (procdefinition.proctypeoption=potype_constructor) and
  1571. not(cnf_inherited in callnodeflags) and
  1572. not(cnf_member_call in callnodeflags) then
  1573. verifyabstractcalls;
  1574. end
  1575. else
  1576. begin
  1577. { When this is method the methodpointer must be available }
  1578. if (right=nil) and
  1579. (procdefinition.owner.symtabletype=objectsymtable) then
  1580. internalerror(200305061);
  1581. end;
  1582. { Change loading of array of const to varargs }
  1583. if assigned(left) and
  1584. is_array_of_const(tparaitem(procdefinition.para.last).paratype.def) and
  1585. (procdefinition.proccalloption in [pocall_cppdecl,pocall_cdecl]) then
  1586. convert_carg_array_of_const;
  1587. { bind paraitems to the callparanodes and insert hidden parameters }
  1588. aktcallnode:=self;
  1589. bind_paraitem;
  1590. { methodpointer is only needed for virtual calls, and
  1591. it should then be loaded with the VMT }
  1592. if (po_virtualmethod in procdefinition.procoptions) and
  1593. not(assigned(methodpointer) and
  1594. (methodpointer.nodetype=typen)) then
  1595. begin
  1596. if not assigned(methodpointer) then
  1597. internalerror(200305063);
  1598. if (methodpointer.resulttype.def.deftype<>classrefdef) then
  1599. begin
  1600. methodpointer:=cloadvmtaddrnode.create(methodpointer);
  1601. resulttypepass(methodpointer);
  1602. end;
  1603. end
  1604. else
  1605. begin
  1606. { not needed anymore }
  1607. methodpointer.free;
  1608. methodpointer:=nil;
  1609. end;
  1610. { insert type conversions for parameters }
  1611. if assigned(left) then
  1612. tcallparanode(left).insert_typeconv(true);
  1613. errorexit:
  1614. aktcallnode:=oldcallnode;
  1615. end;
  1616. procedure tcallnode.order_parameters;
  1617. var
  1618. hp,hpcurr,hpnext,hpfirst,hpprev : tcallparanode;
  1619. currloc : tcgloc;
  1620. begin
  1621. hpfirst:=nil;
  1622. hpcurr:=tcallparanode(left);
  1623. while assigned(hpcurr) do
  1624. begin
  1625. { pull out }
  1626. hpnext:=tcallparanode(hpcurr.right);
  1627. { pull in at the correct place.
  1628. Used order:
  1629. 1. LOC_REFERENCE with smallest offset (x86 only)
  1630. 2. LOC_REFERENCE with most registers
  1631. 3. LOC_REGISTER with most registers }
  1632. currloc:=hpcurr.paraitem.paraloc[callerside].loc;
  1633. hpprev:=nil;
  1634. hp:=hpfirst;
  1635. while assigned(hp) do
  1636. begin
  1637. case currloc of
  1638. LOC_REFERENCE :
  1639. begin
  1640. case hp.paraitem.paraloc[callerside].loc of
  1641. LOC_REFERENCE :
  1642. begin
  1643. { Offset is calculated like:
  1644. sub esp,12
  1645. mov [esp+8],para3
  1646. mov [esp+4],para2
  1647. mov [esp],para1
  1648. call function
  1649. That means the for pushes the para with the
  1650. highest offset (see para3) needs to be pushed first
  1651. }
  1652. if (hpcurr.registersint>hp.registersint)
  1653. {$ifdef x86}
  1654. or (hpcurr.paraitem.paraloc[callerside].reference.offset>hp.paraitem.paraloc[callerside].reference.offset)
  1655. {$endif x86}
  1656. then
  1657. break;
  1658. end;
  1659. LOC_REGISTER,
  1660. LOC_FPUREGISTER :
  1661. break;
  1662. end;
  1663. end;
  1664. LOC_FPUREGISTER,
  1665. LOC_REGISTER :
  1666. begin
  1667. if (hp.paraitem.paraloc[callerside].loc=currloc) and
  1668. (hpcurr.registersint>hp.registersint) then
  1669. break;
  1670. end;
  1671. end;
  1672. hpprev:=hp;
  1673. hp:=tcallparanode(hp.right);
  1674. end;
  1675. hpcurr.right:=hp;
  1676. if assigned(hpprev) then
  1677. hpprev.right:=hpcurr
  1678. else
  1679. hpfirst:=hpcurr;
  1680. { next }
  1681. hpcurr:=hpnext;
  1682. end;
  1683. left:=hpfirst;
  1684. end;
  1685. function tcallnode.replaceparaload(var n: tnode; arg: pointer): foreachnoderesult;
  1686. var
  1687. paras: tcallparanode;
  1688. temp: tnode;
  1689. begin
  1690. result := fen_false;
  1691. n.fileinfo := pfileposinfo(arg)^;
  1692. if (n.nodetype = loadn) then
  1693. begin
  1694. paras := tcallparanode(left);
  1695. while assigned(paras) and
  1696. (paras.paraitem.parasym <> tloadnode(n).symtableentry) do
  1697. paras := tcallparanode(paras.right);
  1698. if assigned(paras) then
  1699. begin
  1700. n.free;
  1701. n := paras.left.getcopy;
  1702. resulttypepass(n);
  1703. result := fen_true;
  1704. end
  1705. {$ifndef VER1_0}
  1706. else
  1707. begin
  1708. { local? }
  1709. if (tloadnode(n).symtableentry.typ <> varsym) or
  1710. (tloadnode(n).symtableentry.owner <> tprocdef(procdefinition).localst) then
  1711. exit;
  1712. if (tloadnode(n).symtableentry.indexnr > high(inlinelocals)) or
  1713. not assigned(inlinelocals[tloadnode(n).symtableentry.indexnr]) then
  1714. internalerror(20040720);
  1715. temp := inlinelocals[tloadnode(n).symtableentry.indexnr].getcopy;
  1716. n.free;
  1717. n := temp;
  1718. resulttypepass(n);
  1719. result := fen_true;
  1720. end;
  1721. {$endif ndef VER1_0}
  1722. end;
  1723. end;
  1724. {$ifndef VER1_0}
  1725. type
  1726. ptempnodes = ^ttempnodes;
  1727. ttempnodes = record
  1728. createstatement, deletestatement: tstatementnode;
  1729. end;
  1730. procedure tcallnode.createlocaltemps(p:TNamedIndexItem;arg:pointer);
  1731. var
  1732. tempinfo: ptempnodes absolute ptempnodes(arg);
  1733. tempnode: ttempcreatenode;
  1734. begin
  1735. if (tsymentry(p).typ <> varsym) then
  1736. exit;
  1737. if (p.indexnr > high(inlinelocals)) then
  1738. setlength(inlinelocals,p.indexnr+10);
  1739. {$ifndef VER1_0}
  1740. if (vo_is_funcret in tvarsym(p).varoptions) and
  1741. assigned(funcretnode) then
  1742. begin
  1743. if node_complexity(funcretnode) > 1 then
  1744. begin
  1745. { can this happen? }
  1746. { we may have to replace the funcretnode with the address of funcretnode }
  1747. { loaded in a temp in this case, because the expression may e.g. contain }
  1748. { a global variable that gets changed inside the function }
  1749. internalerror(2004072101);
  1750. end;
  1751. inlinelocals[tvarsym(p).indexnr] := funcretnode.getcopy
  1752. end
  1753. else
  1754. {$endif ndef VER1_0}
  1755. begin
  1756. if (cs_regvars in aktglobalswitches) and
  1757. (([vo_regable{$ifndef x86},vo_fpuregable{$endif}] * tvarsym(p).varoptions) <> []) and
  1758. (not tvarsym(p).vartype.def.needs_inittable) then
  1759. tempnode := ctempcreatenode.create_reg(tvarsym(p).vartype,tvarsym(p).vartype.def.size,tt_persistent)
  1760. else
  1761. tempnode := ctempcreatenode.create(tvarsym(p).vartype,tvarsym(p).vartype.def.size,tt_persistent);
  1762. addstatement(tempinfo^.createstatement,tempnode);
  1763. if assigned(tvarsym(p).defaultconstsym) then
  1764. begin
  1765. { warning: duplicate from psub.pas:initializevars() -> must refactor }
  1766. addstatement(tempinfo^.createstatement,cassignmentnode.create(
  1767. ctemprefnode.create(tempnode),
  1768. cloadnode.create(tvarsym(p).defaultconstsym,tvarsym(p).defaultconstsym.owner)));
  1769. end;
  1770. if (vo_is_funcret in tvarsym(p).varoptions) then
  1771. begin
  1772. funcretnode := ctemprefnode.create(tempnode);
  1773. addstatement(tempinfo^.deletestatement,ctempdeletenode.create_normal_temp(tempnode));
  1774. end
  1775. else
  1776. addstatement(tempinfo^.deletestatement,ctempdeletenode.create(tempnode));
  1777. inlinelocals[p.indexnr] := ctemprefnode.create(tempnode);
  1778. end;
  1779. end;
  1780. {$endif ndef VER1_0}
  1781. procedure tcallnode.createinlineparas(var createstatement, deletestatement: tstatementnode);
  1782. var
  1783. para: tcallparanode;
  1784. tempnode: ttempcreatenode;
  1785. hp: tnode;
  1786. {$ifndef VER1_0}
  1787. tempnodes: ttempnodes;
  1788. {$endif ndef VER1_0}
  1789. begin
  1790. { parameters }
  1791. para := tcallparanode(left);
  1792. while assigned(para) do
  1793. begin
  1794. if (para.paraitem.parasym.typ = varsym) and
  1795. { para.left will already be the same as funcretnode in the following case, so don't change }
  1796. (not(vo_is_funcret in tvarsym(para.paraitem.parasym).varoptions) or
  1797. (not assigned(funcretnode))) then
  1798. begin
  1799. { create temps for value parameters, function result and also for }
  1800. { const parameters which are passed by value instead of by reference }
  1801. if (vo_is_funcret in tvarsym(para.paraitem.parasym).varoptions) or
  1802. (para.paraitem.paratyp = vs_value) or
  1803. ((para.paraitem.paratyp = vs_const) and
  1804. (not paramanager.push_addr_param(vs_const,para.left.resulttype.def,procdefinition.proccalloption) or
  1805. { the problem is that we can't take the address of a function result :( }
  1806. (node_complexity(para.left) >= NODE_COMPLEXITY_INF))) then
  1807. begin
  1808. if (cs_regvars in aktglobalswitches) and
  1809. (([vo_regable,vo_fpuregable] * tvarsym(para.paraitem.parasym).varoptions) <> []) and
  1810. (not tvarsym(para.paraitem.parasym).vartype.def.needs_inittable) then
  1811. tempnode := ctempcreatenode.create_reg(para.left.resulttype,para.left.resulttype.def.size,tt_persistent)
  1812. else
  1813. tempnode := ctempcreatenode.create(para.left.resulttype,para.left.resulttype.def.size,tt_persistent);
  1814. addstatement(createstatement,tempnode);
  1815. { assign the value of the parameter to the temp, except in case of the function result }
  1816. { (in that case, para.left is a block containing the creation of a new temp, while we }
  1817. { only need a temprefnode, so delete the old stuff) }
  1818. if not(vo_is_funcret in tvarsym(para.paraitem.parasym).varoptions) then
  1819. begin
  1820. addstatement(createstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  1821. para.left));
  1822. para.left := ctemprefnode.create(tempnode);
  1823. addstatement(deletestatement,ctempdeletenode.create(tempnode));
  1824. end
  1825. else
  1826. begin
  1827. if not(assigned(funcretnode)) then
  1828. funcretnode := ctemprefnode.create(tempnode);
  1829. para.left.free;
  1830. para.left := ctemprefnode.create(tempnode);
  1831. addstatement(deletestatement,ctempdeletenode.create_normal_temp(tempnode));
  1832. end
  1833. end
  1834. else if node_complexity(para.left) > 1 then
  1835. begin
  1836. if (cs_regvars in aktglobalswitches) and
  1837. not tvarsym(para.paraitem.parasym).vartype.def.needs_inittable then
  1838. tempnode := ctempcreatenode.create_reg(voidpointertype,voidpointertype.def.size,tt_persistent)
  1839. else
  1840. tempnode := ctempcreatenode.create(voidpointertype,voidpointertype.def.size,tt_persistent);
  1841. addstatement(createstatement,tempnode);
  1842. addstatement(createstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  1843. caddrnode.create(para.left)));
  1844. hp := cderefnode.create(ctemprefnode.create(tempnode));
  1845. inserttypeconv_explicit(hp,para.left.resulttype);
  1846. para.left := hp;
  1847. addstatement(deletestatement,ctempdeletenode.create(tempnode));
  1848. end;
  1849. para := tcallparanode(para.right);
  1850. end;
  1851. end;
  1852. {$ifndef VER1_0}
  1853. { local variables }
  1854. if not assigned(tprocdef(procdefinition).localst) or
  1855. (tprocdef(procdefinition).localst.symindex.count = 0) then
  1856. exit;
  1857. tempnodes.createstatement := createstatement;
  1858. tempnodes.deletestatement := deletestatement;
  1859. setlength(inlinelocals,tprocdef(procdefinition).localst.symindex.count);
  1860. tprocdef(procdefinition).localst.foreach({$ifdef FPCPROCVAR}@{$endif}createlocaltemps,@tempnodes);
  1861. createstatement := tempnodes.createstatement;
  1862. deletestatement := tempnodes.deletestatement;
  1863. {$endif ndef VER1_0}
  1864. end;
  1865. function tcallnode.pass_1 : tnode;
  1866. var
  1867. createstatement,deletestatement: tstatementnode;
  1868. createblock,deleteblock: tblocknode;
  1869. i: longint;
  1870. label
  1871. errorexit;
  1872. begin
  1873. result:=nil;
  1874. {!!!!!!!!
  1875. if (procdefinition.proccalloption=pocall_inline) and
  1876. { can we inline this procedure at the node level? }
  1877. (tprocdef(procdefinition).inlininginfo^.inlinenode) then
  1878. begin
  1879. { inherit flags }
  1880. current_procinfo.flags := current_procinfo.flags + ((procdefinition as tprocdef).inlininginfo^.flags*inherited_inlining_flags);
  1881. if assigned(methodpointer) then
  1882. CGMessage(cg_e_unable_inline_object_methods);
  1883. if assigned(right) then
  1884. CGMessage(cg_e_unable_inline_procvar);
  1885. if assigned(inlinecode) then
  1886. internalerror(2004071110);
  1887. if assigned(tprocdef(procdefinition).inlininginfo^.code) then
  1888. result:=tprocdef(procdefinition).inlininginfo^.code.getcopy
  1889. else
  1890. CGMessage(cg_e_no_code_for_inline_stored);
  1891. if assigned(result) then
  1892. begin
  1893. createblock := internalstatements(createstatement);
  1894. deleteblock := internalstatements(deletestatement);
  1895. { replace complex parameters with temps }
  1896. createinlineparas(createstatement,deletestatement);
  1897. { replace the parameter loads with the parameter values }
  1898. foreachnode(result,{$ifdef FPCPROCVAR}@{$endif}replaceparaload,@fileinfo);
  1899. { free the temps for the locals }
  1900. for i := 0 to high(inlinelocals) do
  1901. if assigned(inlinelocals[i]) then
  1902. inlinelocals[i].free;
  1903. setlength(inlinelocals,0);
  1904. addstatement(createstatement,result);
  1905. addstatement(createstatement,deleteblock);
  1906. { set function result location if necessary }
  1907. if assigned(funcretnode) and
  1908. (cnf_return_value_used in callnodeflags) then
  1909. addstatement(createstatement,funcretnode.getcopy);
  1910. result := createblock;
  1911. { consider it must not be inlined if called
  1912. again inside the args or itself }
  1913. procdefinition.proccalloption:=pocall_default;
  1914. firstpass(result);
  1915. procdefinition.proccalloption:=pocall_inline;
  1916. exit;
  1917. end;
  1918. end;
  1919. }
  1920. { calculate the parameter info for the procdef }
  1921. if not procdefinition.has_paraloc_info then
  1922. begin
  1923. procdefinition.requiredargarea:=paramanager.create_paraloc_info(procdefinition,callerside);
  1924. procdefinition.has_paraloc_info:=true;
  1925. end;
  1926. { calculate the parameter size needed for this call include varargs if they are available }
  1927. if assigned(varargsparas) then
  1928. pushedparasize:=paramanager.create_varargs_paraloc_info(procdefinition,varargsparas)
  1929. else
  1930. pushedparasize:=procdefinition.requiredargarea;
  1931. { record maximum parameter size used in this proc }
  1932. current_procinfo.allocate_push_parasize(pushedparasize);
  1933. { work trough all parameters to get the register requirements }
  1934. if assigned(left) then
  1935. tcallparanode(left).det_registers;
  1936. { order parameters }
  1937. order_parameters;
  1938. if assigned(methodpointerinit) then
  1939. firstpass(methodpointerinit);
  1940. if assigned(methodpointerdone) then
  1941. firstpass(methodpointerdone);
  1942. { function result node }
  1943. if assigned(_funcretnode) then
  1944. firstpass(_funcretnode);
  1945. { procedure variable ? }
  1946. if assigned(right) then
  1947. begin
  1948. firstpass(right);
  1949. { procedure does a call }
  1950. if not (block_type in [bt_const,bt_type]) then
  1951. include(current_procinfo.flags,pi_do_call);
  1952. end
  1953. else
  1954. { not a procedure variable }
  1955. begin
  1956. { calc the correture value for the register }
  1957. { handle predefined procedures }
  1958. if (procdefinition.proccalloption=pocall_inline) then
  1959. begin
  1960. { inherit flags }
  1961. current_procinfo.flags := current_procinfo.flags + ((procdefinition as tprocdef).inlininginfo^.flags*inherited_inlining_flags);
  1962. if assigned(methodpointer) then
  1963. CGMessage(cg_e_unable_inline_object_methods);
  1964. if assigned(right) then
  1965. CGMessage(cg_e_unable_inline_procvar);
  1966. if not assigned(inlinecode) then
  1967. begin
  1968. if assigned(tprocdef(procdefinition).inlininginfo^.code) then
  1969. inlinecode:=tprocdef(procdefinition).inlininginfo^.code.getcopy
  1970. else
  1971. CGMessage(cg_e_no_code_for_inline_stored);
  1972. if assigned(inlinecode) then
  1973. begin
  1974. { consider it has not inlined if called
  1975. again inside the args }
  1976. procdefinition.proccalloption:=pocall_default;
  1977. firstpass(inlinecode);
  1978. end;
  1979. end;
  1980. end
  1981. else
  1982. begin
  1983. if not (block_type in [bt_const,bt_type]) then
  1984. include(current_procinfo.flags,pi_do_call);
  1985. end;
  1986. end;
  1987. { implicit finally needed ? }
  1988. if resulttype.def.needs_inittable and
  1989. not paramanager.ret_in_param(resulttype.def,procdefinition.proccalloption) and
  1990. not assigned(funcretnode) then
  1991. include(current_procinfo.flags,pi_needs_implicit_finally);
  1992. { get a register for the return value }
  1993. if (not is_void(resulttype.def)) then
  1994. begin
  1995. if paramanager.ret_in_param(resulttype.def,procdefinition.proccalloption) then
  1996. begin
  1997. expectloc:=LOC_REFERENCE;
  1998. end
  1999. else
  2000. { for win32 records returned in EDX:EAX, we
  2001. move them to memory after ... }
  2002. if (resulttype.def.deftype=recorddef) then
  2003. begin
  2004. expectloc:=LOC_REFERENCE;
  2005. end
  2006. else
  2007. { ansi/widestrings must be registered, so we can dispose them }
  2008. if is_ansistring(resulttype.def) or
  2009. is_widestring(resulttype.def) then
  2010. begin
  2011. expectloc:=LOC_REFERENCE;
  2012. registersint:=1;
  2013. end
  2014. else
  2015. { we have only to handle the result if it is used }
  2016. if (cnf_return_value_used in callnodeflags) then
  2017. begin
  2018. case resulttype.def.deftype of
  2019. enumdef,
  2020. orddef :
  2021. begin
  2022. if (procdefinition.proctypeoption=potype_constructor) then
  2023. begin
  2024. expectloc:=LOC_REGISTER;
  2025. registersint:=1;
  2026. end
  2027. else
  2028. begin
  2029. expectloc:=LOC_REGISTER;
  2030. if is_64bit(resulttype.def) then
  2031. registersint:=2
  2032. else
  2033. registersint:=1;
  2034. end;
  2035. end;
  2036. floatdef :
  2037. begin
  2038. expectloc:=LOC_FPUREGISTER;
  2039. {$ifdef cpufpemu}
  2040. if (cs_fp_emulation in aktmoduleswitches) then
  2041. registersint:=1
  2042. else
  2043. {$endif cpufpemu}
  2044. {$ifdef m68k}
  2045. if (tfloatdef(resulttype.def).typ=s32real) then
  2046. registersint:=1
  2047. else
  2048. {$endif m68k}
  2049. registersfpu:=1;
  2050. end;
  2051. else
  2052. begin
  2053. expectloc:=LOC_REGISTER;
  2054. registersint:=1;
  2055. end;
  2056. end;
  2057. end
  2058. else
  2059. expectloc:=LOC_VOID;
  2060. end
  2061. else
  2062. expectloc:=LOC_VOID;
  2063. {$ifdef m68k}
  2064. { we need one more address register for virtual calls on m68k }
  2065. if (po_virtualmethod in procdefinition.procoptions) then
  2066. inc(registersint);
  2067. {$endif m68k}
  2068. { a fpu can be used in any procedure !! }
  2069. {$ifdef i386}
  2070. registersfpu:=procdefinition.fpu_used;
  2071. {$endif i386}
  2072. { if this is a call to a method calc the registers }
  2073. if (methodpointer<>nil) then
  2074. begin
  2075. if methodpointer.nodetype<>typen then
  2076. begin
  2077. firstpass(methodpointer);
  2078. registersfpu:=max(methodpointer.registersfpu,registersfpu);
  2079. registersint:=max(methodpointer.registersint,registersint);
  2080. {$ifdef SUPPORT_MMX }
  2081. registersmmx:=max(methodpointer.registersmmx,registersmmx);
  2082. {$endif SUPPORT_MMX}
  2083. end;
  2084. end;
  2085. { determine the registers of the procedure variable }
  2086. { is this OK for inlined procs also ?? (PM) }
  2087. if assigned(inlinecode) then
  2088. begin
  2089. registersfpu:=max(inlinecode.registersfpu,registersfpu);
  2090. registersint:=max(inlinecode.registersint,registersint);
  2091. {$ifdef SUPPORT_MMX}
  2092. registersmmx:=max(inlinecode.registersmmx,registersmmx);
  2093. {$endif SUPPORT_MMX}
  2094. end;
  2095. { determine the registers of the procedure variable }
  2096. { is this OK for inlined procs also ?? (PM) }
  2097. if assigned(right) then
  2098. begin
  2099. registersfpu:=max(right.registersfpu,registersfpu);
  2100. registersint:=max(right.registersint,registersint);
  2101. {$ifdef SUPPORT_MMX}
  2102. registersmmx:=max(right.registersmmx,registersmmx);
  2103. {$endif SUPPORT_MMX}
  2104. end;
  2105. { determine the registers of the procedure }
  2106. if assigned(left) then
  2107. begin
  2108. registersfpu:=max(left.registersfpu,registersfpu);
  2109. registersint:=max(left.registersint,registersint);
  2110. {$ifdef SUPPORT_MMX}
  2111. registersmmx:=max(left.registersmmx,registersmmx);
  2112. {$endif SUPPORT_MMX}
  2113. end;
  2114. errorexit:
  2115. if assigned(inlinecode) then
  2116. procdefinition.proccalloption:=pocall_inline;
  2117. end;
  2118. {$ifdef state_tracking}
  2119. function Tcallnode.track_state_pass(exec_known:boolean):boolean;
  2120. var hp:Tcallparanode;
  2121. value:Tnode;
  2122. begin
  2123. track_state_pass:=false;
  2124. hp:=Tcallparanode(left);
  2125. while assigned(hp) do
  2126. begin
  2127. if left.track_state_pass(exec_known) then
  2128. begin
  2129. left.resulttype.def:=nil;
  2130. do_resulttypepass(left);
  2131. end;
  2132. value:=aktstate.find_fact(hp.left);
  2133. if value<>nil then
  2134. begin
  2135. track_state_pass:=true;
  2136. hp.left.destroy;
  2137. hp.left:=value.getcopy;
  2138. do_resulttypepass(hp.left);
  2139. end;
  2140. hp:=Tcallparanode(hp.right);
  2141. end;
  2142. end;
  2143. {$endif}
  2144. function tcallnode.para_count:longint;
  2145. var
  2146. ppn : tcallparanode;
  2147. begin
  2148. result:=0;
  2149. ppn:=tcallparanode(left);
  2150. while assigned(ppn) do
  2151. begin
  2152. if not(assigned(ppn.paraitem) and
  2153. ppn.paraitem.is_hidden) then
  2154. inc(result);
  2155. ppn:=tcallparanode(ppn.right);
  2156. end;
  2157. end;
  2158. function tcallnode.docompare(p: tnode): boolean;
  2159. begin
  2160. docompare :=
  2161. inherited docompare(p) and
  2162. (symtableprocentry = tcallnode(p).symtableprocentry) and
  2163. (procdefinition = tcallnode(p).procdefinition) and
  2164. (methodpointer.isequal(tcallnode(p).methodpointer)) and
  2165. (((cnf_restypeset in callnodeflags) and (cnf_restypeset in tcallnode(p).callnodeflags) and
  2166. (equal_defs(restype.def,tcallnode(p).restype.def))) or
  2167. (not(cnf_restypeset in callnodeflags) and not(cnf_restypeset in tcallnode(p).callnodeflags)));
  2168. end;
  2169. procedure tcallnode.printnodedata(var t:text);
  2170. begin
  2171. if assigned(procdefinition) and
  2172. (procdefinition.deftype=procdef) then
  2173. writeln(t,printnodeindention,'proc = ',tprocdef(procdefinition).fullprocname(true))
  2174. else
  2175. begin
  2176. if assigned(symtableprocentry) then
  2177. writeln(t,printnodeindention,'proc = ',symtableprocentry.name)
  2178. else
  2179. writeln(t,printnodeindention,'proc = <nil>');
  2180. end;
  2181. printnode(t,methodpointer);
  2182. printnode(t,right);
  2183. printnode(t,left);
  2184. end;
  2185. begin
  2186. ccallnode:=tcallnode;
  2187. ccallparanode:=tcallparanode;
  2188. end.
  2189. {
  2190. $Log$
  2191. Revision 1.244 2004-08-14 14:50:42 florian
  2192. * fixed several sparc alignment issues
  2193. + Jonas' inline node patch; non functional yet
  2194. Revision 1.243 2004/07/16 19:45:15 jonas
  2195. + temps can now also hold fpu values in registers (take care with use,
  2196. bacause of the x86 fpu stack)
  2197. * fpu parameters to node-inlined procedures can now also be put in
  2198. a register
  2199. Revision 1.242 2004/07/15 21:02:05 jonas
  2200. * the condition for when to use a temp in case of reference var/const
  2201. parameters was inverse
  2202. Revision 1.241 2004/07/15 19:55:39 jonas
  2203. + (incomplete) node_complexity function to assess the complexity of a
  2204. tree
  2205. + support for inlining value and const parameters at the node level
  2206. (all procedures without local variables and without formal parameters
  2207. can now be inlined at the node level)
  2208. Revision 1.240 2004/07/12 09:14:04 jonas
  2209. * inline procedures at the node tree level, but only under some very
  2210. limited circumstances for now (only procedures, and only if they have
  2211. no or only vs_out/vs_var parameters).
  2212. * fixed ppudump for inline procedures
  2213. * fixed ppudump for ppc
  2214. Revision 1.239 2004/06/20 08:55:29 florian
  2215. * logs truncated
  2216. Revision 1.238 2004/06/16 20:07:08 florian
  2217. * dwarf branch merged
  2218. Revision 1.237 2004/05/25 18:51:49 peter
  2219. * fix tcallnode.getcopy. the parameters need to be copied after
  2220. methodpointerinit is copied
  2221. Revision 1.236 2004/05/24 17:31:51 peter
  2222. * fix passing of array to open array of array (bug 3113)
  2223. Revision 1.235 2004/05/23 18:28:41 peter
  2224. * methodpointer is loaded into a temp when it was a calln
  2225. Revision 1.234 2004/05/23 15:06:20 peter
  2226. * implicit_finally flag must be set in pass1
  2227. * add check whether the implicit frame is generated when expected
  2228. }