ncal.pas 105 KB

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