ncal.pas 104 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656
  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: TList;
  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. { convert pascal to C types }
  503. case left.resulttype.def.deftype of
  504. stringdef :
  505. inserttypeconv(left,charpointertype);
  506. floatdef :
  507. inserttypeconv(left,s64floattype);
  508. end;
  509. set_varstate(left,vs_read,[vsf_must_be_valid]);
  510. resulttype:=left.resulttype;
  511. { also update parasym type to get the correct parameter location
  512. for the new types }
  513. parasym.vartype:=left.resulttype;
  514. end
  515. else
  516. if (vo_is_hidden_para in parasym.varoptions) then
  517. begin
  518. set_varstate(left,vs_read,[vsf_must_be_valid]);
  519. resulttype:=left.resulttype;
  520. end
  521. else
  522. begin
  523. { Do we need arrayconstructor -> set conversion, then insert
  524. it here before the arrayconstructor node breaks the tree
  525. with its conversions of enum->ord }
  526. if (left.nodetype=arrayconstructorn) and
  527. (parasym.vartype.def.deftype=setdef) then
  528. inserttypeconv(left,parasym.vartype);
  529. { set some settings needed for arrayconstructor }
  530. if is_array_constructor(left.resulttype.def) then
  531. begin
  532. if left.nodetype<>arrayconstructorn then
  533. internalerror(200504041);
  534. if is_array_of_const(parasym.vartype.def) then
  535. begin
  536. { force variant array }
  537. include(left.flags,nf_forcevaria);
  538. end
  539. else
  540. begin
  541. include(left.flags,nf_novariaallowed);
  542. { now that the resultting type is know we can insert the required
  543. typeconvs for the array constructor }
  544. if parasym.vartype.def.deftype=arraydef then
  545. tarrayconstructornode(left).force_type(tarraydef(parasym.vartype.def).elementtype);
  546. end;
  547. end;
  548. { check if local proc/func is assigned to procvar }
  549. if left.resulttype.def.deftype=procvardef then
  550. test_local_to_procvar(tprocvardef(left.resulttype.def),parasym.vartype.def);
  551. { test conversions }
  552. if not(is_shortstring(left.resulttype.def) and
  553. is_shortstring(parasym.vartype.def)) and
  554. (parasym.vartype.def.deftype<>formaldef) then
  555. begin
  556. { Process open parameters }
  557. if paramanager.push_high_param(parasym.varspez,parasym.vartype.def,aktcallnode.procdefinition.proccalloption) then
  558. begin
  559. { insert type conv but hold the ranges of the array }
  560. oldtype:=left.resulttype;
  561. inserttypeconv(left,parasym.vartype);
  562. left.resulttype:=oldtype;
  563. end
  564. else
  565. begin
  566. check_ranges(left.fileinfo,left,parasym.vartype.def);
  567. inserttypeconv(left,parasym.vartype);
  568. end;
  569. if codegenerror then
  570. begin
  571. dec(parsing_para_level);
  572. exit;
  573. end;
  574. end;
  575. { check var strings }
  576. if (cs_strict_var_strings in aktlocalswitches) and
  577. is_shortstring(left.resulttype.def) and
  578. is_shortstring(parasym.vartype.def) and
  579. (parasym.varspez in [vs_out,vs_var]) and
  580. not(is_open_string(parasym.vartype.def)) and
  581. not(equal_defs(left.resulttype.def,parasym.vartype.def)) then
  582. begin
  583. aktfilepos:=left.fileinfo;
  584. CGMessage(type_e_strict_var_string_violation);
  585. end;
  586. { Handle formal parameters separate }
  587. if (parasym.vartype.def.deftype=formaldef) then
  588. begin
  589. { load procvar if a procedure is passed }
  590. if ((m_tp_procvar in aktmodeswitches) or
  591. (m_mac_procvar in aktmodeswitches)) and
  592. (left.nodetype=calln) and
  593. (is_void(left.resulttype.def)) then
  594. load_procvar_from_calln(left);
  595. case parasym.varspez of
  596. vs_var,
  597. vs_out :
  598. begin
  599. if not valid_for_formal_var(left,true) then
  600. CGMessagePos(left.fileinfo,parser_e_illegal_parameter_list);
  601. end;
  602. vs_const :
  603. begin
  604. if not valid_for_formal_const(left,true) then
  605. CGMessagePos(left.fileinfo,parser_e_illegal_parameter_list);
  606. end;
  607. end;
  608. end
  609. else
  610. begin
  611. { check if the argument is allowed }
  612. if (parasym.varspez in [vs_out,vs_var]) then
  613. valid_for_var(left,true);
  614. end;
  615. if parasym.varspez in [vs_var,vs_out] then
  616. set_unique(left);
  617. { When the address needs to be pushed then the register is
  618. not regable. Exception is when the location is also a var
  619. parameter and we can pass the address transparently }
  620. if (
  621. not(
  622. (vo_is_hidden_para in parasym.varoptions) and
  623. (left.resulttype.def.deftype in [pointerdef,classrefdef])
  624. ) and
  625. paramanager.push_addr_param(parasym.varspez,parasym.vartype.def,
  626. aktcallnode.procdefinition.proccalloption) and
  627. not(
  628. (left.nodetype=loadn) and
  629. (tloadnode(left).is_addr_param_load)
  630. )
  631. ) then
  632. make_not_regable(left);
  633. if do_count then
  634. begin
  635. case parasym.varspez of
  636. vs_out :
  637. set_varstate(left,vs_written,[]);
  638. vs_var :
  639. set_varstate(left,vs_readwritten,[vsf_must_be_valid,vsf_use_hints]);
  640. else
  641. set_varstate(left,vs_read,[vsf_must_be_valid]);
  642. end;
  643. end;
  644. { must only be done after typeconv PM }
  645. resulttype:=parasym.vartype;
  646. end;
  647. end;
  648. { process next node }
  649. if assigned(right) then
  650. tcallparanode(right).insert_typeconv(do_count);
  651. dec(parsing_para_level);
  652. {$ifdef extdebug}
  653. if do_count then
  654. count_ref:=store_count_ref;
  655. {$endif def extdebug}
  656. end;
  657. procedure tcallparanode.det_registers;
  658. begin
  659. if assigned(right) then
  660. begin
  661. tcallparanode(right).det_registers;
  662. registersint:=right.registersint;
  663. registersfpu:=right.registersfpu;
  664. {$ifdef SUPPORT_MMX}
  665. registersmmx:=right.registersmmx;
  666. {$endif}
  667. end;
  668. firstpass(left);
  669. if left.registersint>registersint then
  670. registersint:=left.registersint;
  671. if left.registersfpu>registersfpu then
  672. registersfpu:=left.registersfpu;
  673. {$ifdef SUPPORT_MMX}
  674. if left.registersmmx>registersmmx then
  675. registersmmx:=left.registersmmx;
  676. {$endif SUPPORT_MMX}
  677. end;
  678. procedure tcallparanode.firstcallparan;
  679. begin
  680. if not assigned(left.resulttype.def) then
  681. get_paratype;
  682. det_registers;
  683. end;
  684. function tcallparanode.docompare(p: tnode): boolean;
  685. begin
  686. docompare :=
  687. inherited docompare(p) and
  688. (callparaflags = tcallparanode(p).callparaflags)
  689. ;
  690. end;
  691. procedure tcallparanode.printnodetree(var t:text);
  692. begin
  693. printnodelist(t);
  694. end;
  695. {****************************************************************************
  696. TCALLNODE
  697. ****************************************************************************}
  698. constructor tcallnode.create(l:tnode;v : tprocsym;st : tsymtable; mp: tnode; callflags:tcallnodeflags);
  699. begin
  700. inherited create(calln,l,nil);
  701. symtableprocentry:=v;
  702. symtableproc:=st;
  703. callnodeflags:=callflags+[cnf_return_value_used];
  704. methodpointer:=mp;
  705. methodpointerinit:=nil;
  706. methodpointerdone:=nil;
  707. procdefinition:=nil;
  708. _funcretnode:=nil;
  709. paralength:=-1;
  710. varargsparas:=nil;
  711. end;
  712. constructor tcallnode.create_procvar(l,r:tnode);
  713. begin
  714. inherited create(calln,l,r);
  715. symtableprocentry:=nil;
  716. symtableproc:=nil;
  717. methodpointer:=nil;
  718. methodpointerinit:=nil;
  719. methodpointerdone:=nil;
  720. procdefinition:=nil;
  721. callnodeflags:=[cnf_return_value_used];
  722. _funcretnode:=nil;
  723. paralength:=-1;
  724. varargsparas:=nil;
  725. end;
  726. constructor tcallnode.createintern(const name: string; params: tnode);
  727. var
  728. srsym: tsym;
  729. begin
  730. srsym := tsym(systemunit.search(name));
  731. if not assigned(srsym) and
  732. (cs_compilesystem in aktmoduleswitches) then
  733. srsym := tsym(systemunit.search(upper(name)));
  734. if not assigned(srsym) or
  735. (srsym.typ<>procsym) then
  736. Message1(cg_f_unknown_compilerproc,name);
  737. self.create(params,tprocsym(srsym),srsym.owner,nil,[]);
  738. end;
  739. constructor tcallnode.createinternres(const name: string; params: tnode; const res: ttype);
  740. begin
  741. self.createintern(name,params);
  742. restype := res;
  743. include(callnodeflags,cnf_restypeset);
  744. { both the normal and specified resulttype either have to be returned via a }
  745. { parameter or not, but no mixing (JM) }
  746. if paramanager.ret_in_param(restype.def,symtableprocentry.first_procdef.proccalloption) xor
  747. paramanager.ret_in_param(symtableprocentry.first_procdef.rettype.def,symtableprocentry.first_procdef.proccalloption) then
  748. internalerror(200108291);
  749. end;
  750. constructor tcallnode.createinternreturn(const name: string; params: tnode; returnnode : tnode);
  751. begin
  752. self.createintern(name,params);
  753. _funcretnode:=returnnode;
  754. if not paramanager.ret_in_param(symtableprocentry.first_procdef.rettype.def,symtableprocentry.first_procdef.proccalloption) then
  755. internalerror(200204247);
  756. end;
  757. procedure tcallnode.setfuncretnode(const returnnode: tnode);
  758. var
  759. para: tcallparanode;
  760. begin
  761. if assigned(_funcretnode) then
  762. _funcretnode.free;
  763. _funcretnode := returnnode;
  764. { if the resulttype pass hasn't occurred yet, that one will do }
  765. { everything }
  766. if assigned(resulttype.def) then
  767. begin
  768. { these are returned as values, but we can optimize their loading }
  769. { as well }
  770. if is_ansistring(resulttype.def) or
  771. is_widestring(resulttype.def) then
  772. exit;
  773. para := tcallparanode(left);
  774. while assigned(para) do
  775. begin
  776. if (vo_is_hidden_para in para.parasym.varoptions) and
  777. (vo_is_funcret in tparavarsym(para.parasym).varoptions) then
  778. begin
  779. para.left.free;
  780. para.left := _funcretnode.getcopy;
  781. exit;
  782. end;
  783. para := tcallparanode(para.right);
  784. end;
  785. { no hidden resultpara found, error! }
  786. if not(po_inline in procdefinition.procoptions) then
  787. internalerror(200306087);
  788. end;
  789. end;
  790. destructor tcallnode.destroy;
  791. var
  792. i : longint;
  793. begin
  794. methodpointer.free;
  795. methodpointerinit.free;
  796. methodpointerdone.free;
  797. _funcretnode.free;
  798. if assigned(varargsparas) then
  799. begin
  800. for i:=0 to varargsparas.count-1 do
  801. tparavarsym(varargsparas[i]).free;
  802. varargsparas.free;
  803. end;
  804. inherited destroy;
  805. end;
  806. constructor tcallnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  807. begin
  808. inherited ppuload(t,ppufile);
  809. ppufile.getderef(symtableprocentryderef);
  810. {$ifdef fpc}
  811. {$warning FIXME: No withsymtable support}
  812. {$endif}
  813. symtableproc:=nil;
  814. ppufile.getderef(procdefinitionderef);
  815. ppufile.getsmallset(callnodeflags);
  816. methodpointer:=ppuloadnode(ppufile);
  817. methodpointerinit:=tblocknode(ppuloadnode(ppufile));
  818. methodpointerdone:=tblocknode(ppuloadnode(ppufile));
  819. _funcretnode:=ppuloadnode(ppufile);
  820. end;
  821. procedure tcallnode.ppuwrite(ppufile:tcompilerppufile);
  822. begin
  823. inherited ppuwrite(ppufile);
  824. ppufile.putderef(symtableprocentryderef);
  825. ppufile.putderef(procdefinitionderef);
  826. ppufile.putsmallset(callnodeflags);
  827. ppuwritenode(ppufile,methodpointer);
  828. ppuwritenode(ppufile,methodpointerinit);
  829. ppuwritenode(ppufile,methodpointerdone);
  830. ppuwritenode(ppufile,_funcretnode);
  831. end;
  832. procedure tcallnode.buildderefimpl;
  833. begin
  834. inherited buildderefimpl;
  835. symtableprocentryderef.build(symtableprocentry);
  836. procdefinitionderef.build(procdefinition);
  837. if assigned(methodpointer) then
  838. methodpointer.buildderefimpl;
  839. if assigned(methodpointerinit) then
  840. methodpointerinit.buildderefimpl;
  841. if assigned(methodpointerdone) then
  842. methodpointerdone.buildderefimpl;
  843. if assigned(_funcretnode) then
  844. _funcretnode.buildderefimpl;
  845. end;
  846. procedure tcallnode.derefimpl;
  847. var
  848. pt : tcallparanode;
  849. i : integer;
  850. begin
  851. inherited derefimpl;
  852. symtableprocentry:=tprocsym(symtableprocentryderef.resolve);
  853. if assigned(symtableprocentry) then
  854. symtableproc:=symtableprocentry.owner;
  855. procdefinition:=tabstractprocdef(procdefinitionderef.resolve);
  856. if assigned(methodpointer) then
  857. methodpointer.derefimpl;
  858. if assigned(methodpointerinit) then
  859. methodpointerinit.derefimpl;
  860. if assigned(methodpointerdone) then
  861. methodpointerdone.derefimpl;
  862. if assigned(_funcretnode) then
  863. _funcretnode.derefimpl;
  864. { Connect parasyms }
  865. pt:=tcallparanode(left);
  866. while assigned(pt) and
  867. (cpf_varargs_para in pt.callparaflags) do
  868. pt:=tcallparanode(pt.right);
  869. for i:=procdefinition.paras.count-1 downto 0 do
  870. begin
  871. if not assigned(pt) then
  872. internalerror(200311077);
  873. pt.parasym:=tparavarsym(procdefinition.paras[i]);
  874. pt:=tcallparanode(pt.right);
  875. end;
  876. if assigned(pt) then
  877. internalerror(200311078);
  878. end;
  879. function tcallnode._getcopy : tnode;
  880. var
  881. n : tcallnode;
  882. i : integer;
  883. hp,hpn : tparavarsym;
  884. oldleft : tnode;
  885. begin
  886. { Need to use a hack here to prevent the parameters from being copied.
  887. The parameters must be copied between methodpointerinit/methodpointerdone because
  888. the can reference methodpointer }
  889. oldleft:=left;
  890. left:=nil;
  891. n:=tcallnode(inherited _getcopy);
  892. left:=oldleft;
  893. n.symtableprocentry:=symtableprocentry;
  894. n.symtableproc:=symtableproc;
  895. n.procdefinition:=procdefinition;
  896. n.restype := restype;
  897. n.callnodeflags := callnodeflags;
  898. if assigned(methodpointerinit) then
  899. n.methodpointerinit:=tblocknode(methodpointerinit._getcopy)
  900. else
  901. n.methodpointerinit:=nil;
  902. { methodpointerinit is copied, now references to the temp will also be copied
  903. correctly. We can now copy the parameters and methodpointer }
  904. if assigned(left) then
  905. n.left:=left._getcopy
  906. else
  907. n.left:=nil;
  908. if assigned(methodpointer) then
  909. n.methodpointer:=methodpointer._getcopy
  910. else
  911. n.methodpointer:=nil;
  912. if assigned(methodpointerdone) then
  913. n.methodpointerdone:=tblocknode(methodpointerdone._getcopy)
  914. else
  915. n.methodpointerdone:=nil;
  916. if assigned(_funcretnode) then
  917. n._funcretnode:=_funcretnode._getcopy
  918. else
  919. n._funcretnode:=nil;
  920. if assigned(varargsparas) then
  921. begin
  922. n.varargsparas:=tvarargsparalist.create;
  923. for i:=0 to varargsparas.count-1 do
  924. begin
  925. hp:=tparavarsym(varargsparas[i]);
  926. hpn:=tparavarsym.create(hp.realname,hp.paranr,hp.varspez,hp.vartype,[]);
  927. n.varargsparas.add(hpn);
  928. end;
  929. end
  930. else
  931. n.varargsparas:=nil;
  932. result:=n;
  933. end;
  934. procedure tcallnode.insertintolist(l : tnodelist);
  935. begin
  936. end;
  937. procedure tcallnode.convert_carg_array_of_const;
  938. var
  939. hp : tarrayconstructornode;
  940. oldleft : tcallparanode;
  941. begin
  942. oldleft:=tcallparanode(left);
  943. if oldleft.left.nodetype<>arrayconstructorn then
  944. begin
  945. CGMessage1(type_e_wrong_type_in_array_constructor,oldleft.left.resulttype.def.typename);
  946. exit;
  947. end;
  948. include(callnodeflags,cnf_uses_varargs);
  949. { Get arrayconstructor node and insert typeconvs }
  950. hp:=tarrayconstructornode(oldleft.left);
  951. hp.insert_typeconvs;
  952. { Add c args parameters }
  953. { It could be an empty set }
  954. if assigned(hp) and
  955. assigned(hp.left) then
  956. begin
  957. while assigned(hp) do
  958. begin
  959. left:=ccallparanode.create(hp.left,left);
  960. { set callparanode resulttype and flags }
  961. left.resulttype:=hp.left.resulttype;
  962. include(tcallparanode(left).callparaflags,cpf_varargs_para);
  963. hp.left:=nil;
  964. hp:=tarrayconstructornode(hp.right);
  965. end;
  966. end;
  967. { Remove value of old array of const parameter, but keep it
  968. in the list because it is required for bind_parasym.
  969. Generate a nothign to keep callparanoed.left valid }
  970. oldleft.left.free;
  971. oldleft.left:=cnothingnode.create;
  972. end;
  973. procedure tcallnode.verifyabstract(p : tnamedindexitem;arg:pointer);
  974. var
  975. hp : tprocdef;
  976. j: integer;
  977. begin
  978. if (tsym(p).typ=procsym) then
  979. begin
  980. for j:=1 to tprocsym(p).procdef_count do
  981. begin
  982. { index starts at 1 }
  983. hp:=tprocsym(p).procdef[j];
  984. { If this is an abstract method insert into the list }
  985. if (po_abstractmethod in hp.procoptions) then
  986. AbstractMethodsList.Insert(hp.procsym.realname)
  987. else
  988. { If this symbol is a virtual (includes override) method,
  989. then remove it from the list }
  990. if po_virtualmethod in hp.procoptions then
  991. AbstractMethodsList.Remove(hp.procsym.realname);
  992. end;
  993. end;
  994. end;
  995. procedure tcallnode.verifyabstractcalls;
  996. var
  997. objectdf : tobjectdef;
  998. parents : tlinkedlist;
  999. objectinfo : tobjectinfoitem;
  1000. stritem : tstringlistitem;
  1001. begin
  1002. objectdf := nil;
  1003. { verify if trying to create an instance of a class which contains
  1004. non-implemented abstract methods }
  1005. { first verify this class type, no class than exit }
  1006. { also, this checking can only be done if the constructor is directly
  1007. called, indirect constructor calls cannot be checked.
  1008. }
  1009. if assigned(methodpointer) then
  1010. begin
  1011. if (methodpointer.resulttype.def.deftype = objectdef) then
  1012. objectdf:=tobjectdef(methodpointer.resulttype.def)
  1013. else
  1014. if (methodpointer.resulttype.def.deftype = classrefdef) and
  1015. (tclassrefdef(methodpointer.resulttype.def).pointertype.def.deftype = objectdef) and
  1016. (methodpointer.nodetype in [typen,loadvmtaddrn]) then
  1017. objectdf:=tobjectdef(tclassrefdef(methodpointer.resulttype.def).pointertype.def);
  1018. end;
  1019. if not assigned(objectdf) then
  1020. exit;
  1021. parents := tlinkedlist.create;
  1022. AbstractMethodsList := tstringlist.create;
  1023. { insert all parents in this class : the first item in the
  1024. list will be the base parent of the class .
  1025. }
  1026. while assigned(objectdf) do
  1027. begin
  1028. objectinfo:=tobjectinfoitem.create(objectdf);
  1029. parents.insert(objectinfo);
  1030. objectdf := objectdf.childof;
  1031. end;
  1032. { now all parents are in the correct order
  1033. insert all abstract methods in the list, and remove
  1034. those which are overriden by parent classes.
  1035. }
  1036. objectinfo:=tobjectinfoitem(parents.first);
  1037. while assigned(objectinfo) do
  1038. begin
  1039. objectdf := objectinfo.objinfo;
  1040. if assigned(objectdf.symtable) then
  1041. objectdf.symtable.foreach(@verifyabstract,nil);
  1042. objectinfo:=tobjectinfoitem(objectinfo.next);
  1043. end;
  1044. if assigned(parents) then
  1045. parents.free;
  1046. { Finally give out a warning for each abstract method still in the list }
  1047. stritem := tstringlistitem(AbstractMethodsList.first);
  1048. if assigned(stritem) then
  1049. Message1(type_w_instance_with_abstract,objectdf.objrealname^);
  1050. while assigned(stritem) do
  1051. begin
  1052. if assigned(stritem.fpstr) then
  1053. Message1(sym_h_param_list,stritem.str);
  1054. stritem := tstringlistitem(stritem.next);
  1055. end;
  1056. if assigned(AbstractMethodsList) then
  1057. AbstractMethodsList.Free;
  1058. end;
  1059. function tcallnode.gen_self_tree_methodpointer:tnode;
  1060. var
  1061. hsym : tfieldvarsym;
  1062. begin
  1063. { find self field in methodpointer record }
  1064. hsym:=tfieldvarsym(trecorddef(methodpointertype.def).symtable.search('self'));
  1065. if not assigned(hsym) then
  1066. internalerror(200305251);
  1067. { Load tmehodpointer(right).self }
  1068. result:=csubscriptnode.create(
  1069. hsym,
  1070. ctypeconvnode.create_internal(right.getcopy,methodpointertype));
  1071. end;
  1072. function tcallnode.gen_self_tree:tnode;
  1073. var
  1074. selftree : tnode;
  1075. begin
  1076. selftree:=nil;
  1077. { inherited }
  1078. if (cnf_inherited in callnodeflags) then
  1079. selftree:=load_self_node
  1080. else
  1081. { constructors }
  1082. if (procdefinition.proctypeoption=potype_constructor) then
  1083. begin
  1084. { push 0 as self when allocation is needed }
  1085. if (methodpointer.resulttype.def.deftype=classrefdef) or
  1086. (cnf_new_call in callnodeflags) then
  1087. selftree:=cpointerconstnode.create(0,voidpointertype)
  1088. else
  1089. begin
  1090. if methodpointer.nodetype=typen then
  1091. selftree:=load_self_node
  1092. else
  1093. selftree:=methodpointer.getcopy;
  1094. end;
  1095. end
  1096. else
  1097. { Calling a static/class method }
  1098. if (po_classmethod in procdefinition.procoptions) or
  1099. (po_staticmethod in procdefinition.procoptions) then
  1100. begin
  1101. if (procdefinition.deftype<>procdef) then
  1102. internalerror(200305062);
  1103. if (oo_has_vmt in tprocdef(procdefinition)._class.objectoptions) then
  1104. begin
  1105. { we only need the vmt, loading self is not required and there is no
  1106. need to check for typen, because that will always get the
  1107. loadvmtaddrnode added }
  1108. selftree:=methodpointer.getcopy;
  1109. if methodpointer.resulttype.def.deftype<>classrefdef then
  1110. selftree:=cloadvmtaddrnode.create(selftree);
  1111. end
  1112. else
  1113. selftree:=cpointerconstnode.create(0,voidpointertype);
  1114. end
  1115. else
  1116. begin
  1117. if methodpointer.nodetype=typen then
  1118. selftree:=load_self_node
  1119. else
  1120. selftree:=methodpointer.getcopy;
  1121. end;
  1122. result:=selftree;
  1123. end;
  1124. function tcallnode.gen_vmt_tree:tnode;
  1125. var
  1126. vmttree : tnode;
  1127. begin
  1128. vmttree:=nil;
  1129. if not(procdefinition.proctypeoption in [potype_constructor,potype_destructor]) then
  1130. internalerror(200305051);
  1131. { Handle classes and legacy objects separate to make it
  1132. more maintainable }
  1133. if (methodpointer.resulttype.def.deftype=classrefdef) then
  1134. begin
  1135. if not is_class(tclassrefdef(methodpointer.resulttype.def).pointertype.def) then
  1136. internalerror(200501041);
  1137. { constructor call via classreference => allocate memory }
  1138. if (procdefinition.proctypeoption=potype_constructor) then
  1139. begin
  1140. vmttree:=methodpointer.getcopy;
  1141. { Only a typenode can be passed when it is called with <class of xx>.create }
  1142. if vmttree.nodetype=typen then
  1143. vmttree:=cloadvmtaddrnode.create(vmttree);
  1144. end
  1145. else
  1146. begin
  1147. { Call afterconstruction }
  1148. vmttree:=cpointerconstnode.create(1,voidpointertype);
  1149. end;
  1150. end
  1151. else
  1152. { Class style objects }
  1153. if is_class(methodpointer.resulttype.def) then
  1154. begin
  1155. { inherited call, no create/destroy }
  1156. if (cnf_inherited in callnodeflags) then
  1157. vmttree:=cpointerconstnode.create(0,voidpointertype)
  1158. else
  1159. { do not create/destroy when called from member function
  1160. without specifying self explicit }
  1161. if (cnf_member_call in callnodeflags) then
  1162. begin
  1163. { destructor: don't release instance, vmt=0
  1164. constructor:
  1165. if called from a constructor in the same class then
  1166. don't call afterconstruction, vmt=0
  1167. else
  1168. call afterconstrution, vmt=1 }
  1169. if (procdefinition.proctypeoption=potype_destructor) then
  1170. vmttree:=cpointerconstnode.create(0,voidpointertype)
  1171. else
  1172. begin
  1173. if (current_procinfo.procdef.proctypeoption=potype_constructor) and
  1174. (procdefinition.proctypeoption=potype_constructor) then
  1175. vmttree:=cpointerconstnode.create(0,voidpointertype)
  1176. else
  1177. vmttree:=cpointerconstnode.create(1,voidpointertype);
  1178. end;
  1179. end
  1180. else
  1181. { normal call to method like cl1.proc }
  1182. begin
  1183. { destructor: release instance, vmt=1
  1184. constructor:
  1185. if called from a constructor in the same class using self.create then
  1186. don't call afterconstruction, vmt=0
  1187. else
  1188. call afterconstrution, vmt=1 }
  1189. if (procdefinition.proctypeoption=potype_destructor) then
  1190. vmttree:=cpointerconstnode.create(1,voidpointertype)
  1191. else
  1192. begin
  1193. if (current_procinfo.procdef.proctypeoption=potype_constructor) and
  1194. (procdefinition.proctypeoption=potype_constructor) and
  1195. (nf_is_self in methodpointer.flags) then
  1196. vmttree:=cpointerconstnode.create(0,voidpointertype)
  1197. else
  1198. vmttree:=cpointerconstnode.create(1,voidpointertype);
  1199. end;
  1200. end;
  1201. end
  1202. else
  1203. { Old style object }
  1204. begin
  1205. { constructor with extended syntax called from new }
  1206. if (cnf_new_call in callnodeflags) then
  1207. vmttree:=cloadvmtaddrnode.create(ctypenode.create(methodpointer.resulttype))
  1208. else
  1209. { destructor with extended syntax called from dispose }
  1210. if (cnf_dispose_call in callnodeflags) then
  1211. vmttree:=cloadvmtaddrnode.create(methodpointer.getcopy)
  1212. else
  1213. { inherited call, no create/destroy }
  1214. if (cnf_inherited in callnodeflags) then
  1215. vmttree:=cpointerconstnode.create(0,voidpointertype)
  1216. else
  1217. { do not create/destroy when called from member function
  1218. without specifying self explicit }
  1219. if (cnf_member_call in callnodeflags) then
  1220. begin
  1221. { destructor: don't release instance, vmt=0
  1222. constructor: don't initialize instance, vmt=0 }
  1223. vmttree:=cpointerconstnode.create(0,voidpointertype)
  1224. end
  1225. else
  1226. { normal object call like obj.proc }
  1227. begin
  1228. { destructor: direct call, no dispose, vmt=0
  1229. constructor: initialize object, load vmt }
  1230. if (procdefinition.proctypeoption=potype_constructor) then
  1231. begin
  1232. { old styled inherited call? }
  1233. if (methodpointer.nodetype=typen) then
  1234. vmttree:=cpointerconstnode.create(0,voidpointertype)
  1235. else
  1236. vmttree:=cloadvmtaddrnode.create(ctypenode.create(methodpointer.resulttype))
  1237. end
  1238. else
  1239. vmttree:=cpointerconstnode.create(0,voidpointertype);
  1240. end;
  1241. end;
  1242. result:=vmttree;
  1243. end;
  1244. procedure tcallnode.bind_parasym;
  1245. var
  1246. i : integer;
  1247. pt : tcallparanode;
  1248. oldppt : ^tcallparanode;
  1249. varargspara,
  1250. currpara : tparavarsym;
  1251. used_by_callnode : boolean;
  1252. hiddentree : tnode;
  1253. newstatement : tstatementnode;
  1254. temp : ttempcreatenode;
  1255. begin
  1256. pt:=tcallparanode(left);
  1257. oldppt:=@left;
  1258. { flag all callparanodes that belong to the varargs }
  1259. i:=paralength;
  1260. while (i>procdefinition.maxparacount) do
  1261. begin
  1262. include(pt.callparaflags,cpf_varargs_para);
  1263. oldppt:[email protected];
  1264. pt:=tcallparanode(pt.right);
  1265. dec(i);
  1266. end;
  1267. { skip varargs that are inserted by array of const }
  1268. while assigned(pt) and
  1269. (cpf_varargs_para in pt.callparaflags) do
  1270. pt:=tcallparanode(pt.right);
  1271. { process normal parameters and insert hidden parameters }
  1272. for i:=procdefinition.paras.count-1 downto 0 do
  1273. begin
  1274. currpara:=tparavarsym(procdefinition.paras[i]);
  1275. if vo_is_hidden_para in currpara.varoptions then
  1276. begin
  1277. { generate hidden tree }
  1278. used_by_callnode:=false;
  1279. hiddentree:=nil;
  1280. if (vo_is_funcret in currpara.varoptions) then
  1281. begin
  1282. { Generate funcretnode if not specified }
  1283. if assigned(funcretnode) then
  1284. begin
  1285. hiddentree:=funcretnode.getcopy;
  1286. end
  1287. else
  1288. begin
  1289. hiddentree:=internalstatements(newstatement);
  1290. { need to use resulttype instead of procdefinition.rettype,
  1291. because they can be different }
  1292. temp:=ctempcreatenode.create(resulttype,resulttype.def.size,tt_persistent,false);
  1293. addstatement(newstatement,temp);
  1294. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  1295. addstatement(newstatement,ctemprefnode.create(temp));
  1296. end;
  1297. end
  1298. else
  1299. if vo_is_high_para in currpara.varoptions then
  1300. begin
  1301. if not assigned(pt) or
  1302. (i=0) then
  1303. internalerror(200304082);
  1304. { we need the information of the previous parameter }
  1305. hiddentree:=gen_high_tree(pt.left,tparavarsym(procdefinition.paras[i-1]).vartype.def);
  1306. end
  1307. else
  1308. if vo_is_self in currpara.varoptions then
  1309. begin
  1310. if assigned(right) then
  1311. hiddentree:=gen_self_tree_methodpointer
  1312. else
  1313. hiddentree:=gen_self_tree;
  1314. end
  1315. else
  1316. if vo_is_vmt in currpara.varoptions then
  1317. begin
  1318. hiddentree:=gen_vmt_tree;
  1319. end
  1320. {$if defined(powerpc) or defined(m68k)}
  1321. else
  1322. if vo_is_syscall_lib in currpara.varoptions then
  1323. begin
  1324. { lib parameter has no special type but proccalloptions must be a syscall }
  1325. hiddentree:=cloadnode.create(tprocdef(procdefinition).libsym,tprocdef(procdefinition).libsym.owner);
  1326. end
  1327. {$endif powerpc or m68k}
  1328. else
  1329. if vo_is_parentfp in currpara.varoptions then
  1330. begin
  1331. if not(assigned(procdefinition.owner.defowner)) then
  1332. internalerror(200309287);
  1333. hiddentree:=cloadparentfpnode.create(tprocdef(procdefinition.owner.defowner));
  1334. end;
  1335. { add the hidden parameter }
  1336. if not assigned(hiddentree) then
  1337. internalerror(200304073);
  1338. { Already insert para and let the previous node point to
  1339. this new node }
  1340. pt:=ccallparanode.create(hiddentree,oldppt^);
  1341. pt.used_by_callnode:=used_by_callnode;
  1342. oldppt^:=pt;
  1343. end;
  1344. if not assigned(pt) then
  1345. internalerror(200310052);
  1346. pt.parasym:=currpara;
  1347. oldppt:[email protected];
  1348. pt:=tcallparanode(pt.right);
  1349. end;
  1350. { Create parasyms for varargs, first count the number of varargs paras,
  1351. then insert the parameters with numbering in reverse order. The SortParas
  1352. will set the correct order at the end}
  1353. pt:=tcallparanode(left);
  1354. i:=0;
  1355. while assigned(pt) do
  1356. begin
  1357. if cpf_varargs_para in pt.callparaflags then
  1358. inc(i);
  1359. pt:=tcallparanode(pt.right);
  1360. end;
  1361. if (i>0) then
  1362. begin
  1363. varargsparas:=tvarargsparalist.create;
  1364. pt:=tcallparanode(left);
  1365. while assigned(pt) do
  1366. begin
  1367. if cpf_varargs_para in pt.callparaflags then
  1368. begin
  1369. varargspara:=tparavarsym.create('va'+tostr(i),i,vs_value,pt.resulttype,[]);
  1370. dec(i);
  1371. { varargspara is left-right, use insert
  1372. instead of concat }
  1373. varargsparas.add(varargspara);
  1374. pt.parasym:=varargspara;
  1375. end;
  1376. pt:=tcallparanode(pt.right);
  1377. end;
  1378. varargsparas.sortparas;
  1379. end;
  1380. end;
  1381. function tcallnode.det_resulttype:tnode;
  1382. var
  1383. candidates : tcallcandidates;
  1384. oldcallnode : tcallnode;
  1385. hpt : tnode;
  1386. pt : tcallparanode;
  1387. lastpara : longint;
  1388. paraidx,
  1389. cand_cnt : integer;
  1390. i : longint;
  1391. is_const : boolean;
  1392. label
  1393. errorexit;
  1394. begin
  1395. result:=nil;
  1396. candidates:=nil;
  1397. oldcallnode:=aktcallnode;
  1398. aktcallnode:=self;
  1399. { determine length of parameter list }
  1400. pt:=tcallparanode(left);
  1401. paralength:=0;
  1402. while assigned(pt) do
  1403. begin
  1404. inc(paralength);
  1405. pt:=tcallparanode(pt.right);
  1406. end;
  1407. { determine the type of the parameters }
  1408. if assigned(left) then
  1409. begin
  1410. tcallparanode(left).get_paratype;
  1411. if codegenerror then
  1412. goto errorexit;
  1413. end;
  1414. if assigned(methodpointer) then
  1415. begin
  1416. resulttypepass(methodpointer);
  1417. maybe_load_para_in_temp(methodpointer);
  1418. end;
  1419. { procedure variable ? }
  1420. if assigned(right) then
  1421. begin
  1422. set_varstate(right,vs_read,[vsf_must_be_valid]);
  1423. resulttypepass(right);
  1424. if codegenerror then
  1425. exit;
  1426. procdefinition:=tabstractprocdef(right.resulttype.def);
  1427. { Compare parameters from right to left }
  1428. paraidx:=procdefinition.Paras.count-1;
  1429. { Skip default parameters }
  1430. if not(po_varargs in procdefinition.procoptions) then
  1431. begin
  1432. { ignore hidden parameters }
  1433. while (paraidx>=0) and (vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) do
  1434. dec(paraidx);
  1435. for i:=1 to procdefinition.maxparacount-paralength do
  1436. begin
  1437. if paraidx<0 then
  1438. internalerror(200402261);
  1439. if not assigned(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym) then
  1440. begin
  1441. CGMessage(parser_e_wrong_parameter_size);
  1442. goto errorexit;
  1443. end;
  1444. dec(paraidx);
  1445. end;
  1446. end;
  1447. while (paraidx>=0) and (vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) do
  1448. dec(paraidx);
  1449. pt:=tcallparanode(left);
  1450. lastpara:=paralength;
  1451. while (paraidx>=0) and assigned(pt) do
  1452. begin
  1453. { only goto next para if we're out of the varargs }
  1454. if not(po_varargs in procdefinition.procoptions) or
  1455. (lastpara<=procdefinition.maxparacount) then
  1456. begin
  1457. repeat
  1458. dec(paraidx);
  1459. until (paraidx<0) or not(vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions);
  1460. end;
  1461. pt:=tcallparanode(pt.right);
  1462. dec(lastpara);
  1463. end;
  1464. if assigned(pt) or
  1465. ((paraidx>=0) and
  1466. not assigned(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym)) then
  1467. begin
  1468. if assigned(pt) then
  1469. aktfilepos:=pt.fileinfo;
  1470. CGMessage(parser_e_wrong_parameter_size);
  1471. goto errorexit;
  1472. end;
  1473. end
  1474. else
  1475. { not a procedure variable }
  1476. begin
  1477. { do we know the procedure to call ? }
  1478. if not(assigned(procdefinition)) then
  1479. begin
  1480. candidates:=tcallcandidates.create(symtableprocentry,symtableproc,left,(nf_isproperty in flags),
  1481. { ignore possible private in delphi mode for anon. inherited (FK) }
  1482. (m_delphi in aktmodeswitches) and (cnf_anon_inherited in callnodeflags));
  1483. { no procedures found? then there is something wrong
  1484. with the parameter size or the procedures are
  1485. not accessible }
  1486. if candidates.count=0 then
  1487. begin
  1488. { when it's an auto inherited call and there
  1489. is no procedure found, but the procedures
  1490. were defined with overload directive and at
  1491. least two procedures are defined then we ignore
  1492. this inherited by inserting a nothingn. Only
  1493. do this ugly hack in Delphi mode as it looks more
  1494. like a bug. It's also not documented }
  1495. if (m_delphi in aktmodeswitches) and
  1496. (cnf_anon_inherited in callnodeflags) and
  1497. (symtableprocentry.owner.symtabletype=objectsymtable) and
  1498. (po_overload in symtableprocentry.first_procdef.procoptions) and
  1499. (symtableprocentry.procdef_count>=2) then
  1500. result:=cnothingnode.create
  1501. else
  1502. begin
  1503. { in tp mode we can try to convert to procvar if
  1504. there are no parameters specified. Only try it
  1505. when there is only one proc definition, else the
  1506. loadnode will give a strange error }
  1507. if not(assigned(left)) and
  1508. not(cnf_inherited in callnodeflags) and
  1509. ((m_tp_procvar in aktmodeswitches) or
  1510. (m_mac_procvar in aktmodeswitches)) and
  1511. (symtableprocentry.procdef_count=1) and
  1512. (not assigned(methodpointer) or
  1513. (methodpointer.nodetype <> typen)) then
  1514. begin
  1515. hpt:=cloadnode.create(tprocsym(symtableprocentry),symtableproc);
  1516. if assigned(methodpointer) then
  1517. tloadnode(hpt).set_mp(get_load_methodpointer);
  1518. resulttypepass(hpt);
  1519. result:=hpt;
  1520. end
  1521. else
  1522. begin
  1523. if assigned(left) then
  1524. aktfilepos:=left.fileinfo;
  1525. CGMessage(parser_e_wrong_parameter_size);
  1526. symtableprocentry.write_parameter_lists(nil);
  1527. end;
  1528. end;
  1529. goto errorexit;
  1530. end;
  1531. { Retrieve information about the candidates }
  1532. candidates.get_information;
  1533. {$ifdef EXTDEBUG}
  1534. { Display info when multiple candidates are found }
  1535. if candidates.count>1 then
  1536. candidates.dump_info(V_Debug);
  1537. {$endif EXTDEBUG}
  1538. { Choose the best candidate and count the number of
  1539. candidates left }
  1540. cand_cnt:=candidates.choose_best(procdefinition);
  1541. { All parameters are checked, check if there are any
  1542. procedures left }
  1543. if cand_cnt>0 then
  1544. begin
  1545. { Multiple candidates left? }
  1546. if cand_cnt>1 then
  1547. begin
  1548. CGMessage(type_e_cant_choose_overload_function);
  1549. {$ifdef EXTDEBUG}
  1550. candidates.dump_info(V_Hint);
  1551. {$else EXTDEBUG}
  1552. candidates.list(false);
  1553. {$endif EXTDEBUG}
  1554. { we'll just use the first candidate to make the
  1555. call }
  1556. end;
  1557. { assign procdefinition }
  1558. if symtableproc=nil then
  1559. symtableproc:=procdefinition.owner;
  1560. { update browser information }
  1561. if make_ref then
  1562. begin
  1563. tprocdef(procdefinition).lastref:=tref.create(tprocdef(procdefinition).lastref,@fileinfo);
  1564. inc(tprocdef(procdefinition).refcount);
  1565. if tprocdef(procdefinition).defref=nil then
  1566. tprocdef(procdefinition).defref:=tprocdef(procdefinition).lastref;
  1567. end;
  1568. end
  1569. else
  1570. begin
  1571. { No candidates left, this must be a type error,
  1572. because wrong size is already checked. procdefinition
  1573. is filled with the first (random) definition that is
  1574. found. We use this definition to display a nice error
  1575. message that the wrong type is passed }
  1576. candidates.find_wrong_para;
  1577. candidates.list(true);
  1578. {$ifdef EXTDEBUG}
  1579. candidates.dump_info(V_Hint);
  1580. {$endif EXTDEBUG}
  1581. { We can not proceed, release all procs and exit }
  1582. candidates.free;
  1583. goto errorexit;
  1584. end;
  1585. candidates.free;
  1586. end; { end of procedure to call determination }
  1587. end;
  1588. { check for hints (deprecated etc) }
  1589. if (procdefinition.deftype = procdef) then
  1590. check_hints(tprocdef(procdefinition).procsym,tprocdef(procdefinition).symoptions);
  1591. { add needed default parameters }
  1592. if assigned(procdefinition) and
  1593. (paralength<procdefinition.maxparacount) then
  1594. begin
  1595. paraidx:=0;
  1596. i:=0;
  1597. while (i<paralength) do
  1598. begin
  1599. if paraidx>=procdefinition.Paras.count then
  1600. internalerror(200306181);
  1601. if not(vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) then
  1602. inc(i);
  1603. inc(paraidx);
  1604. end;
  1605. while (paraidx<procdefinition.paras.count) and (vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) do
  1606. inc(paraidx);
  1607. while (paraidx<procdefinition.paras.count) do
  1608. begin
  1609. if not assigned(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym) then
  1610. internalerror(200212142);
  1611. left:=ccallparanode.create(genconstsymtree(
  1612. tconstsym(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym)),left);
  1613. { Ignore vs_hidden parameters }
  1614. repeat
  1615. inc(paraidx);
  1616. until (paraidx>=procdefinition.paras.count) or not(vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions);
  1617. end;
  1618. end;
  1619. { handle predefined procedures }
  1620. is_const:=(po_internconst in procdefinition.procoptions) and
  1621. ((block_type in [bt_const,bt_type]) or
  1622. (assigned(left) and (tcallparanode(left).left.nodetype in [realconstn,ordconstn])));
  1623. if (procdefinition.proccalloption=pocall_internproc) or is_const then
  1624. begin
  1625. if assigned(left) then
  1626. begin
  1627. { ptr and settextbuf needs two args }
  1628. if assigned(tcallparanode(left).right) then
  1629. begin
  1630. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,left);
  1631. left:=nil;
  1632. end
  1633. else
  1634. begin
  1635. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,tcallparanode(left).left);
  1636. tcallparanode(left).left:=nil;
  1637. end;
  1638. end
  1639. else
  1640. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,nil);
  1641. result:=hpt;
  1642. goto errorexit;
  1643. end;
  1644. { ensure that the result type is set }
  1645. if not(cnf_restypeset in callnodeflags) then
  1646. begin
  1647. { constructors return their current class type, not the type where the
  1648. constructor is declared, this can be different because of inheritance }
  1649. if (procdefinition.proctypeoption=potype_constructor) and
  1650. assigned(methodpointer) and
  1651. assigned(methodpointer.resulttype.def) and
  1652. (methodpointer.resulttype.def.deftype=classrefdef) then
  1653. resulttype:=tclassrefdef(methodpointer.resulttype.def).pointertype
  1654. else
  1655. { Member call to a (inherited) constructor from the class, the return
  1656. value is always self, so we change it to voidtype to generate an
  1657. error and to prevent users from generating non-working code
  1658. when they expect to clone the current instance, see bug 3662 (PFV) }
  1659. if (procdefinition.proctypeoption=potype_constructor) and
  1660. is_class(tprocdef(procdefinition)._class) and
  1661. assigned(methodpointer) and
  1662. (nf_is_self in methodpointer.flags) then
  1663. resulttype:=voidtype
  1664. else
  1665. resulttype:=procdefinition.rettype;
  1666. end
  1667. else
  1668. resulttype:=restype;
  1669. {if resulttype.def.needs_inittable then
  1670. include(current_procinfo.flags,pi_needs_implicit_finally);}
  1671. if assigned(methodpointer) then
  1672. begin
  1673. { when methodpointer is a callnode we must load it first into a
  1674. temp to prevent the processing callnode twice }
  1675. if (methodpointer.nodetype=calln) then
  1676. internalerror(200405121);
  1677. { direct call to inherited abstract method, then we
  1678. can already give a error in the compiler instead
  1679. of a runtime error }
  1680. if (cnf_inherited in callnodeflags) and
  1681. (po_abstractmethod in procdefinition.procoptions) then
  1682. CGMessage(cg_e_cant_call_abstract_method);
  1683. { if an inherited con- or destructor should be }
  1684. { called in a con- or destructor then a warning }
  1685. { will be made }
  1686. { con- and destructors need a pointer to the vmt }
  1687. if (cnf_inherited in callnodeflags) and
  1688. (procdefinition.proctypeoption in [potype_constructor,potype_destructor]) and
  1689. is_object(methodpointer.resulttype.def) and
  1690. not(current_procinfo.procdef.proctypeoption in [potype_constructor,potype_destructor]) then
  1691. CGMessage(cg_w_member_cd_call_from_method);
  1692. if methodpointer.nodetype<>typen then
  1693. begin
  1694. { Remove all postfix operators }
  1695. hpt:=methodpointer;
  1696. while assigned(hpt) and (hpt.nodetype in [subscriptn,vecn]) do
  1697. hpt:=tunarynode(hpt).left;
  1698. if (procdefinition.proctypeoption=potype_constructor) and
  1699. assigned(symtableproc) and
  1700. (symtableproc.symtabletype=withsymtable) and
  1701. (tnode(twithsymtable(symtableproc).withrefnode).nodetype=temprefn) then
  1702. CGmessage(cg_e_cannot_call_cons_dest_inside_with);
  1703. { R.Init then R will be initialized by the constructor,
  1704. Also allow it for simple loads }
  1705. if (procdefinition.proctypeoption=potype_constructor) or
  1706. ((hpt.nodetype=loadn) and
  1707. (
  1708. (methodpointer.resulttype.def.deftype=classrefdef) or
  1709. (
  1710. (methodpointer.resulttype.def.deftype=objectdef) and
  1711. not(oo_has_virtual in tobjectdef(methodpointer.resulttype.def).objectoptions)
  1712. )
  1713. )
  1714. ) then
  1715. set_varstate(methodpointer,vs_read,[])
  1716. else
  1717. set_varstate(methodpointer,vs_read,[vsf_must_be_valid]);
  1718. { The object is already used if it is called once }
  1719. if (hpt.nodetype=loadn) and
  1720. (tloadnode(hpt).symtableentry.typ in [localvarsym,paravarsym,globalvarsym]) then
  1721. set_varstate(hpt,vs_read,[]);
  1722. // tabstractvarsym(tloadnode(hpt).symtableentry).varstate:=vs_readwritten;
  1723. end;
  1724. { if we are calling the constructor check for abstract
  1725. methods. Ignore inherited and member calls, because the
  1726. class is then already created }
  1727. if (procdefinition.proctypeoption=potype_constructor) and
  1728. not(cnf_inherited in callnodeflags) and
  1729. not(cnf_member_call in callnodeflags) then
  1730. verifyabstractcalls;
  1731. end
  1732. else
  1733. begin
  1734. { When this is method the methodpointer must be available }
  1735. if (right=nil) and
  1736. (procdefinition.owner.symtabletype=objectsymtable) then
  1737. internalerror(200305061);
  1738. end;
  1739. { Set flag that the procedure uses varargs, also if they are not passed it is still
  1740. needed for x86_64 to pass the number of SSE registers used }
  1741. if po_varargs in procdefinition.procoptions then
  1742. include(callnodeflags,cnf_uses_varargs);
  1743. { Change loading of array of const to varargs }
  1744. if assigned(left) and
  1745. is_array_of_const(tparavarsym(procdefinition.paras[procdefinition.paras.count-1]).vartype.def) and
  1746. (procdefinition.proccalloption in [pocall_cppdecl,pocall_cdecl]) then
  1747. convert_carg_array_of_const;
  1748. { bind parasyms to the callparanodes and insert hidden parameters }
  1749. bind_parasym;
  1750. { insert type conversions for parameters }
  1751. if assigned(left) then
  1752. tcallparanode(left).insert_typeconv(true);
  1753. errorexit:
  1754. aktcallnode:=oldcallnode;
  1755. end;
  1756. procedure tcallnode.order_parameters;
  1757. var
  1758. hp,hpcurr,hpnext,hpfirst,hpprev : tcallparanode;
  1759. currloc : tcgloc;
  1760. begin
  1761. hpfirst:=nil;
  1762. hpcurr:=tcallparanode(left);
  1763. while assigned(hpcurr) do
  1764. begin
  1765. { pull out }
  1766. hpnext:=tcallparanode(hpcurr.right);
  1767. { pull in at the correct place.
  1768. Used order:
  1769. 1. LOC_REFERENCE with smallest offset (x86 only)
  1770. 2. LOC_REFERENCE with most registers
  1771. 3. LOC_REGISTER with most registers
  1772. For the moment we only look at the first parameter field. Combining it
  1773. with multiple parameter fields will make things a lot complexer (PFV) }
  1774. if not assigned(hpcurr.parasym.paraloc[callerside].location) then
  1775. internalerror(200412152);
  1776. currloc:=hpcurr.parasym.paraloc[callerside].location^.loc;
  1777. hpprev:=nil;
  1778. hp:=hpfirst;
  1779. while assigned(hp) do
  1780. begin
  1781. case currloc of
  1782. LOC_REFERENCE :
  1783. begin
  1784. case hp.parasym.paraloc[callerside].location^.loc of
  1785. LOC_REFERENCE :
  1786. begin
  1787. { Offset is calculated like:
  1788. sub esp,12
  1789. mov [esp+8],para3
  1790. mov [esp+4],para2
  1791. mov [esp],para1
  1792. call function
  1793. That means the for pushes the para with the
  1794. highest offset (see para3) needs to be pushed first
  1795. }
  1796. if (hpcurr.registersint>hp.registersint)
  1797. {$ifdef x86}
  1798. or (hpcurr.parasym.paraloc[callerside].location^.reference.offset>hp.parasym.paraloc[callerside].location^.reference.offset)
  1799. {$endif x86}
  1800. then
  1801. break;
  1802. end;
  1803. LOC_REGISTER,
  1804. LOC_FPUREGISTER :
  1805. break;
  1806. end;
  1807. end;
  1808. LOC_FPUREGISTER,
  1809. LOC_REGISTER :
  1810. begin
  1811. if (hp.parasym.paraloc[callerside].location^.loc=currloc) and
  1812. (hpcurr.registersint>hp.registersint) then
  1813. break;
  1814. end;
  1815. end;
  1816. hpprev:=hp;
  1817. hp:=tcallparanode(hp.right);
  1818. end;
  1819. hpcurr.right:=hp;
  1820. if assigned(hpprev) then
  1821. hpprev.right:=hpcurr
  1822. else
  1823. hpfirst:=hpcurr;
  1824. { next }
  1825. hpcurr:=hpnext;
  1826. end;
  1827. left:=hpfirst;
  1828. end;
  1829. function tcallnode.replaceparaload(var n: tnode; arg: pointer): foreachnoderesult;
  1830. var
  1831. paras: tcallparanode;
  1832. temp: tnode;
  1833. begin
  1834. result := fen_false;
  1835. n.fileinfo := pfileposinfo(arg)^;
  1836. if (n.nodetype = loadn) then
  1837. begin
  1838. case tloadnode(n).symtableentry.typ of
  1839. paravarsym :
  1840. begin
  1841. paras := tcallparanode(left);
  1842. while assigned(paras) and
  1843. (paras.parasym <> tloadnode(n).symtableentry) do
  1844. paras := tcallparanode(paras.right);
  1845. if assigned(paras) then
  1846. begin
  1847. n.free;
  1848. n := paras.left.getcopy;
  1849. resulttypepass(n);
  1850. result := fen_true;
  1851. end;
  1852. end;
  1853. localvarsym :
  1854. begin
  1855. { local? }
  1856. if (tloadnode(n).symtableentry.owner <> tprocdef(procdefinition).localst) then
  1857. exit;
  1858. if (tloadnode(n).symtableentry.indexnr >= inlinelocals.count) or
  1859. not assigned(inlinelocals[tloadnode(n).symtableentry.indexnr]) then
  1860. internalerror(20040720);
  1861. temp := tnode(inlinelocals[tloadnode(n).symtableentry.indexnr]).getcopy;
  1862. n.free;
  1863. n := temp;
  1864. resulttypepass(n);
  1865. result := fen_true;
  1866. end;
  1867. end;
  1868. end;
  1869. end;
  1870. type
  1871. ptempnodes = ^ttempnodes;
  1872. ttempnodes = record
  1873. createstatement, deletestatement: tstatementnode;
  1874. end;
  1875. procedure tcallnode.createlocaltemps(p:TNamedIndexItem;arg:pointer);
  1876. var
  1877. tempinfo: ptempnodes absolute arg;
  1878. tempnode: ttempcreatenode;
  1879. begin
  1880. if (tsymentry(p).typ <> localvarsym) then
  1881. exit;
  1882. if (p.indexnr >= inlinelocals.count) then
  1883. inlinelocals.count:=p.indexnr+10;
  1884. if (vo_is_funcret in tabstractvarsym(p).varoptions) and
  1885. assigned(funcretnode) then
  1886. begin
  1887. if node_complexity(funcretnode) > 1 then
  1888. begin
  1889. { can this happen? }
  1890. { we may have to replace the funcretnode with the address of funcretnode }
  1891. { loaded in a temp in this case, because the expression may e.g. contain }
  1892. { a global variable that gets changed inside the function }
  1893. internalerror(2004072101);
  1894. end;
  1895. inlinelocals[tabstractvarsym(p).indexnr] := funcretnode.getcopy
  1896. end
  1897. else
  1898. begin
  1899. tempnode := ctempcreatenode.create(tabstractvarsym(p).vartype,tabstractvarsym(p).vartype.def.size,tt_persistent,tabstractvarsym(p).varregable<>vr_none);
  1900. addstatement(tempinfo^.createstatement,tempnode);
  1901. if assigned(tlocalvarsym(p).defaultconstsym) then
  1902. begin
  1903. { warning: duplicate from psub.pas:initializevars() -> must refactor }
  1904. addstatement(tempinfo^.createstatement,cassignmentnode.create(
  1905. ctemprefnode.create(tempnode),
  1906. cloadnode.create(tlocalvarsym(p).defaultconstsym,tlocalvarsym(p).defaultconstsym.owner)));
  1907. end;
  1908. if (vo_is_funcret in tlocalvarsym(p).varoptions) then
  1909. begin
  1910. funcretnode := ctemprefnode.create(tempnode);
  1911. addstatement(tempinfo^.deletestatement,ctempdeletenode.create_normal_temp(tempnode));
  1912. end
  1913. else
  1914. addstatement(tempinfo^.deletestatement,ctempdeletenode.create(tempnode));
  1915. inlinelocals[p.indexnr] := ctemprefnode.create(tempnode);
  1916. end;
  1917. end;
  1918. function nonlocalvars(var n: tnode; arg: pointer): foreachnoderesult;
  1919. begin
  1920. result := fen_false;
  1921. { this is just to play it safe, there are more safe situations }
  1922. if (n.nodetype = derefn) or
  1923. ((n.nodetype = loadn) and
  1924. { globals and fields of (possibly global) objects could always be changed in the callee }
  1925. ((tloadnode(n).symtable.symtabletype in [globalsymtable,objectsymtable]) or
  1926. { statics can only be modified by functions in the same unit }
  1927. ((tloadnode(n).symtable.symtabletype = staticsymtable) and
  1928. (tloadnode(n).symtable = tsymtable(arg))))) or
  1929. ((n.nodetype = subscriptn) and
  1930. (tsubscriptnode(n).vs.owner.symtabletype = objectsymtable)) then
  1931. result := fen_norecurse_true;
  1932. end;
  1933. procedure tcallnode.createinlineparas(var createstatement, deletestatement: tstatementnode);
  1934. var
  1935. para: tcallparanode;
  1936. tempnode: ttempcreatenode;
  1937. tempnodes: ttempnodes;
  1938. n: tnode;
  1939. paracomplexity: longint;
  1940. begin
  1941. { parameters }
  1942. para := tcallparanode(left);
  1943. while assigned(para) do
  1944. begin
  1945. if (para.parasym.typ = paravarsym) and
  1946. { para.left will already be the same as funcretnode in the following case, so don't change }
  1947. (not(vo_is_funcret in tparavarsym(para.parasym).varoptions) or
  1948. (not assigned(funcretnode))) then
  1949. begin
  1950. { must take copy of para.left, because if it contains a }
  1951. { temprefn pointing to a copied temp (e.g. methodpointer), }
  1952. { then this parameter must be changed to point to the copy of }
  1953. { that temp (JM) }
  1954. n := para.left.getcopy;
  1955. para.left.free;
  1956. para.left := n;
  1957. firstpass(para.left);
  1958. { create temps for value parameters, function result and also for }
  1959. { const parameters which are passed by value instead of by reference }
  1960. { we need to take care that we use the type of the defined parameter and not of the
  1961. passed parameter, because these can be different in case of a formaldef (PFV) }
  1962. paracomplexity := node_complexity(para.left);
  1963. { check if we have to create a temp, assign the parameter's }
  1964. { contents to that temp and then substitute the paramter }
  1965. { with the temp everywhere in the function }
  1966. if
  1967. ((tparavarsym(para.parasym).varregable = vr_none) and
  1968. not(para.left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE])) or
  1969. { we can't assign to formaldef temps }
  1970. ((para.parasym.vartype.def.deftype<>formaldef) and
  1971. (
  1972. { if paracomplexity > 1, we normally take the address of }
  1973. { the parameter expression, store it in a temp and }
  1974. { substitute the dereferenced temp in the inlined function }
  1975. { We can't do this if we can't take the address of the }
  1976. { parameter expression, so in that case assign to a temp }
  1977. ((paracomplexity > 1) and
  1978. (not valid_for_addr(para.left,false) or
  1979. (para.left.nodetype = calln) or
  1980. is_constnode(para.left))) or
  1981. { the problem is that we can't take the address of a function result :( }
  1982. (vo_is_funcret in tparavarsym(para.parasym).varoptions) or
  1983. { we do not need to create a temp for value parameters }
  1984. { which are not modified in the inlined function }
  1985. { const parameters can get vs_readwritten if their }
  1986. { address is taken }
  1987. ((((para.parasym.varspez = vs_value) and
  1988. (para.parasym.varstate in [vs_initialised,vs_declared,vs_read])) or
  1989. { in case of const, this is only necessary if the }
  1990. { variable would be passed by value normally, or if }
  1991. { there is such a variable somewhere in an expression }
  1992. ((para.parasym.varspez = vs_const) and
  1993. (not paramanager.push_addr_param(vs_const,para.parasym.vartype.def,procdefinition.proccalloption) or
  1994. (paracomplexity > 1)))) and
  1995. { however, if we pass a global variable, an object field or}
  1996. { an expression containing a pointer dereference as }
  1997. { parameter, this value could be modified in other ways as }
  1998. { well and in such cases create a temp to be on the safe }
  1999. { side }
  2000. foreachnodestatic(para.left,@nonlocalvars,pointer(symtableproc))) or
  2001. { value parameters of which we know they are modified by }
  2002. { definition have to be copied to a temp }
  2003. ((para.parasym.varspez = vs_value) and
  2004. not(para.parasym.varstate in [vs_initialised,vs_declared,vs_read])) or
  2005. { the compiler expects that it can take the address of parameters passed by reference in
  2006. the case of const so we can't replace the node simply by a constant node
  2007. When playing with this code, ensure that
  2008. function f(const a,b : longint) : longint;inline;
  2009. begin
  2010. result:=a*b;
  2011. end;
  2012. [...]
  2013. ...:=f(10,20));
  2014. [...]
  2015. is still folded. (FK)
  2016. }
  2017. ((para.parasym.varspez = vs_const) and
  2018. { const para's can get vs_readwritten if their address }
  2019. { is taken }
  2020. ((para.parasym.varstate = vs_readwritten) or
  2021. { call-by-reference const's may need to be passed by }
  2022. { reference to function called in the inlined code }
  2023. (paramanager.push_addr_param(vs_const,para.parasym.vartype.def,procdefinition.proccalloption) and
  2024. (not valid_for_addr(para.left,false) or
  2025. is_constnode(para.left)))))
  2026. )
  2027. ) then
  2028. begin
  2029. { in theory, this is always regable, but ncgcall can't }
  2030. { handle it yet in all situations (JM) }
  2031. tempnode := ctempcreatenode.create(para.parasym.vartype,para.parasym.vartype.def.size,tt_persistent,tparavarsym(para.parasym).varregable <> vr_none);
  2032. addstatement(createstatement,tempnode);
  2033. { assign the value of the parameter to the temp, except in case of the function result }
  2034. { (in that case, para.left is a block containing the creation of a new temp, while we }
  2035. { only need a temprefnode, so delete the old stuff) }
  2036. if not(vo_is_funcret in tparavarsym(para.parasym).varoptions) then
  2037. begin
  2038. addstatement(createstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  2039. para.left));
  2040. para.left := ctemprefnode.create(tempnode);
  2041. addstatement(deletestatement,ctempdeletenode.create(tempnode));
  2042. end
  2043. else
  2044. begin
  2045. if not(assigned(funcretnode)) then
  2046. funcretnode := ctemprefnode.create(tempnode);
  2047. para.left.free;
  2048. para.left := ctemprefnode.create(tempnode);
  2049. addstatement(deletestatement,ctempdeletenode.create_normal_temp(tempnode));
  2050. end
  2051. end
  2052. { otherwise if the parameter is "complex", take the address }
  2053. { of the parameter expression, store it in a temp and replace }
  2054. { occurrences of the parameter with dereferencings of this }
  2055. { temp }
  2056. else if (paracomplexity > 1) then
  2057. begin
  2058. tempnode := ctempcreatenode.create(voidpointertype,voidpointertype.def.size,tt_persistent,tparavarsym(para.parasym).varregable<>vr_none);
  2059. addstatement(createstatement,tempnode);
  2060. addstatement(createstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  2061. caddrnode.create_internal(para.left)));
  2062. para.left := ctypeconvnode.create_internal(cderefnode.create(ctemprefnode.create(tempnode)),para.left.resulttype);
  2063. addstatement(deletestatement,ctempdeletenode.create(tempnode));
  2064. end;
  2065. end;
  2066. para := tcallparanode(para.right);
  2067. end;
  2068. { local variables }
  2069. if not assigned(tprocdef(procdefinition).localst) or
  2070. (tprocdef(procdefinition).localst.symindex.count = 0) then
  2071. exit;
  2072. tempnodes.createstatement := createstatement;
  2073. tempnodes.deletestatement := deletestatement;
  2074. inlinelocals.count:=tprocdef(procdefinition).localst.symindex.count;
  2075. tprocdef(procdefinition).localst.foreach(@createlocaltemps,@tempnodes);
  2076. createstatement := tempnodes.createstatement;
  2077. deletestatement := tempnodes.deletestatement;
  2078. end;
  2079. function tcallnode.pass1_inline:tnode;
  2080. var
  2081. createstatement,deletestatement: tstatementnode;
  2082. createblock,deleteblock: tblocknode;
  2083. body : tnode;
  2084. i: longint;
  2085. begin
  2086. if not(assigned(tprocdef(procdefinition).inlininginfo) and
  2087. assigned(tprocdef(procdefinition).inlininginfo^.code)) then
  2088. internalerror(200412021);
  2089. { inherit flags }
  2090. current_procinfo.flags := current_procinfo.flags + ((procdefinition as tprocdef).inlininginfo^.flags*inherited_inlining_flags);
  2091. { create blocks for loading/deleting of local data }
  2092. createblock:=internalstatements(createstatement);
  2093. deleteblock:=internalstatements(deletestatement);
  2094. { add methodpointer init code to init statement }
  2095. { (fini must be done later, as it will delete the hookoncopy info) }
  2096. if assigned(methodpointerinit) then
  2097. addstatement(createstatement,methodpointerinit.getcopy);
  2098. inlinelocals:=tlist.create;
  2099. { get copy of the procedure body }
  2100. body:=tprocdef(procdefinition).inlininginfo^.code.getcopy;
  2101. { replace complex parameters with temps }
  2102. createinlineparas(createstatement,deletestatement);
  2103. { replace the parameter loads with the parameter values }
  2104. foreachnode(body,@replaceparaload,@fileinfo);
  2105. { copy methodpointer fini code }
  2106. if assigned(methodpointerdone) then
  2107. addstatement(deletestatement,methodpointerdone.getcopy);
  2108. { free the temps for the locals }
  2109. for i := 0 to inlinelocals.count-1 do
  2110. if assigned(inlinelocals[i]) then
  2111. tnode(inlinelocals[i]).free;
  2112. inlinelocals.free;
  2113. inlinelocals:=nil;
  2114. addstatement(createstatement,body);
  2115. addstatement(createstatement,deleteblock);
  2116. { set function result location if necessary }
  2117. if assigned(funcretnode) and
  2118. (cnf_return_value_used in callnodeflags) then
  2119. addstatement(createstatement,funcretnode.getcopy);
  2120. { consider it must not be inlined if called
  2121. again inside the args or itself }
  2122. exclude(procdefinition.procoptions,po_inline);
  2123. dosimplify(createblock);
  2124. firstpass(createblock);
  2125. include(procdefinition.procoptions,po_inline);
  2126. { return inlined block }
  2127. result := createblock;
  2128. {$ifdef DEBUGINLINE}
  2129. writeln;
  2130. writeln('**************************',tprocdef(procdefinition).mangledname);
  2131. printnode(output,result);
  2132. {$endif DEBUGINLINE}
  2133. end;
  2134. procedure tcallnode.check_stack_parameters;
  2135. var
  2136. hp : tcallparanode;
  2137. begin
  2138. hp:=tcallparanode(left);
  2139. while assigned(hp) do
  2140. begin
  2141. if assigned(hp.parasym) and
  2142. assigned(hp.parasym.paraloc[callerside].location) and
  2143. (hp.parasym.paraloc[callerside].location^.loc=LOC_REFERENCE) then
  2144. include(current_procinfo.flags,pi_has_stackparameter);
  2145. hp:=tcallparanode(hp.right);
  2146. end;
  2147. end;
  2148. function tcallnode.pass_1 : tnode;
  2149. var
  2150. st : tsymtable;
  2151. begin
  2152. result:=nil;
  2153. { Can we inline the procedure? }
  2154. if ([po_inline,po_has_inlininginfo] <= procdefinition.procoptions) then
  2155. begin
  2156. { Check if we can inline the procedure when it references proc/var that
  2157. are not in the globally available }
  2158. st:=procdefinition.owner;
  2159. if (st.symtabletype=objectsymtable) then
  2160. st:=st.defowner.owner;
  2161. if (pi_uses_static_symtable in tprocdef(procdefinition).inlininginfo^.flags) and
  2162. (st.symtabletype=globalsymtable) and
  2163. (not st.iscurrentunit) then
  2164. begin
  2165. Comment(V_lineinfo+V_Debug,'Not inlining "'+tprocdef(procdefinition).procsym.realname+'", references static symtable');
  2166. end
  2167. else
  2168. begin
  2169. result:=pass1_inline;
  2170. exit;
  2171. end;
  2172. end;
  2173. { calculate the parameter info for the procdef }
  2174. if not procdefinition.has_paraloc_info then
  2175. begin
  2176. procdefinition.requiredargarea:=paramanager.create_paraloc_info(procdefinition,callerside);
  2177. procdefinition.has_paraloc_info:=true;
  2178. end;
  2179. { calculate the parameter size needed for this call include varargs if they are available }
  2180. if assigned(varargsparas) then
  2181. pushedparasize:=paramanager.create_varargs_paraloc_info(procdefinition,varargsparas)
  2182. else
  2183. pushedparasize:=procdefinition.requiredargarea;
  2184. { record maximum parameter size used in this proc }
  2185. current_procinfo.allocate_push_parasize(pushedparasize);
  2186. { work trough all parameters to get the register requirements }
  2187. if assigned(left) then
  2188. begin
  2189. tcallparanode(left).det_registers;
  2190. if cs_optimize in aktglobalswitches then
  2191. begin
  2192. { check for stacked parameters }
  2193. check_stack_parameters;
  2194. end;
  2195. end;
  2196. { order parameters }
  2197. order_parameters;
  2198. if assigned(methodpointerinit) then
  2199. firstpass(methodpointerinit);
  2200. if assigned(methodpointerdone) then
  2201. firstpass(methodpointerdone);
  2202. { function result node }
  2203. if assigned(_funcretnode) then
  2204. firstpass(_funcretnode);
  2205. { procedure variable ? }
  2206. if assigned(right) then
  2207. firstpass(right);
  2208. if not (block_type in [bt_const,bt_type]) then
  2209. include(current_procinfo.flags,pi_do_call);
  2210. { implicit finally needed ? }
  2211. if resulttype.def.needs_inittable and
  2212. not paramanager.ret_in_param(resulttype.def,procdefinition.proccalloption) and
  2213. not assigned(funcretnode) then
  2214. include(current_procinfo.flags,pi_needs_implicit_finally);
  2215. { get a register for the return value }
  2216. if (not is_void(resulttype.def)) then
  2217. begin
  2218. if paramanager.ret_in_param(resulttype.def,procdefinition.proccalloption) then
  2219. begin
  2220. expectloc:=LOC_REFERENCE;
  2221. end
  2222. else
  2223. { for win32 records returned in EDX:EAX, we
  2224. move them to memory after ... }
  2225. if (resulttype.def.deftype=recorddef) then
  2226. begin
  2227. expectloc:=LOC_REFERENCE;
  2228. end
  2229. else
  2230. { ansi/widestrings must be registered, so we can dispose them }
  2231. if is_ansistring(resulttype.def) or
  2232. is_widestring(resulttype.def) then
  2233. begin
  2234. expectloc:=LOC_REFERENCE;
  2235. registersint:=1;
  2236. end
  2237. else
  2238. { we have only to handle the result if it is used }
  2239. if (cnf_return_value_used in callnodeflags) then
  2240. begin
  2241. case resulttype.def.deftype of
  2242. enumdef,
  2243. orddef :
  2244. begin
  2245. if (procdefinition.proctypeoption=potype_constructor) then
  2246. begin
  2247. expectloc:=LOC_REGISTER;
  2248. registersint:=1;
  2249. end
  2250. else
  2251. begin
  2252. expectloc:=LOC_REGISTER;
  2253. if is_64bit(resulttype.def) then
  2254. registersint:=2
  2255. else
  2256. registersint:=1;
  2257. end;
  2258. end;
  2259. floatdef :
  2260. begin
  2261. expectloc:=LOC_FPUREGISTER;
  2262. {$ifdef cpufpemu}
  2263. if (cs_fp_emulation in aktmoduleswitches) then
  2264. registersint:=1
  2265. else
  2266. {$endif cpufpemu}
  2267. {$ifdef m68k}
  2268. if (tfloatdef(resulttype.def).typ=s32real) then
  2269. registersint:=1
  2270. else
  2271. {$endif m68k}
  2272. registersfpu:=1;
  2273. end;
  2274. else
  2275. begin
  2276. expectloc:=LOC_REGISTER;
  2277. registersint:=1;
  2278. end;
  2279. end;
  2280. end
  2281. else
  2282. expectloc:=LOC_VOID;
  2283. end
  2284. else
  2285. expectloc:=LOC_VOID;
  2286. {$ifdef m68k}
  2287. { we need one more address register for virtual calls on m68k }
  2288. if (po_virtualmethod in procdefinition.procoptions) then
  2289. inc(registersint);
  2290. {$endif m68k}
  2291. { a fpu can be used in any procedure !! }
  2292. {$ifdef i386}
  2293. registersfpu:=procdefinition.fpu_used;
  2294. {$endif i386}
  2295. { if this is a call to a method calc the registers }
  2296. if (methodpointer<>nil) then
  2297. begin
  2298. if methodpointer.nodetype<>typen then
  2299. begin
  2300. firstpass(methodpointer);
  2301. registersfpu:=max(methodpointer.registersfpu,registersfpu);
  2302. registersint:=max(methodpointer.registersint,registersint);
  2303. {$ifdef SUPPORT_MMX }
  2304. registersmmx:=max(methodpointer.registersmmx,registersmmx);
  2305. {$endif SUPPORT_MMX}
  2306. end;
  2307. end;
  2308. { determine the registers of the procedure variable }
  2309. { is this OK for inlined procs also ?? (PM) }
  2310. if assigned(right) then
  2311. begin
  2312. registersfpu:=max(right.registersfpu,registersfpu);
  2313. registersint:=max(right.registersint,registersint);
  2314. {$ifdef SUPPORT_MMX}
  2315. registersmmx:=max(right.registersmmx,registersmmx);
  2316. {$endif SUPPORT_MMX}
  2317. end;
  2318. { determine the registers of the procedure }
  2319. if assigned(left) then
  2320. begin
  2321. registersfpu:=max(left.registersfpu,registersfpu);
  2322. registersint:=max(left.registersint,registersint);
  2323. {$ifdef SUPPORT_MMX}
  2324. registersmmx:=max(left.registersmmx,registersmmx);
  2325. {$endif SUPPORT_MMX}
  2326. end;
  2327. end;
  2328. {$ifdef state_tracking}
  2329. function Tcallnode.track_state_pass(exec_known:boolean):boolean;
  2330. var hp:Tcallparanode;
  2331. value:Tnode;
  2332. begin
  2333. track_state_pass:=false;
  2334. hp:=Tcallparanode(left);
  2335. while assigned(hp) do
  2336. begin
  2337. if left.track_state_pass(exec_known) then
  2338. begin
  2339. left.resulttype.def:=nil;
  2340. do_resulttypepass(left);
  2341. end;
  2342. value:=aktstate.find_fact(hp.left);
  2343. if value<>nil then
  2344. begin
  2345. track_state_pass:=true;
  2346. hp.left.destroy;
  2347. hp.left:=value.getcopy;
  2348. do_resulttypepass(hp.left);
  2349. end;
  2350. hp:=Tcallparanode(hp.right);
  2351. end;
  2352. end;
  2353. {$endif}
  2354. function tcallnode.para_count:longint;
  2355. var
  2356. ppn : tcallparanode;
  2357. begin
  2358. result:=0;
  2359. ppn:=tcallparanode(left);
  2360. while assigned(ppn) do
  2361. begin
  2362. if not(assigned(ppn.parasym) and
  2363. (vo_is_hidden_para in ppn.parasym.varoptions)) then
  2364. inc(result);
  2365. ppn:=tcallparanode(ppn.right);
  2366. end;
  2367. end;
  2368. function tcallnode.get_load_methodpointer:tnode;
  2369. var
  2370. newstatement : tstatementnode;
  2371. begin
  2372. if assigned(methodpointerinit) then
  2373. begin
  2374. result:=internalstatements(newstatement);
  2375. addstatement(newstatement,methodpointerinit);
  2376. addstatement(newstatement,methodpointer);
  2377. addstatement(newstatement,methodpointerdone);
  2378. methodpointerinit:=nil;
  2379. methodpointer:=nil;
  2380. methodpointerdone:=nil;
  2381. end
  2382. else
  2383. begin
  2384. result:=methodpointer;
  2385. methodpointer:=nil;
  2386. end;
  2387. end;
  2388. function tcallnode.docompare(p: tnode): boolean;
  2389. begin
  2390. docompare :=
  2391. inherited docompare(p) and
  2392. (symtableprocentry = tcallnode(p).symtableprocentry) and
  2393. (procdefinition = tcallnode(p).procdefinition) and
  2394. (methodpointer.isequal(tcallnode(p).methodpointer)) and
  2395. (((cnf_restypeset in callnodeflags) and (cnf_restypeset in tcallnode(p).callnodeflags) and
  2396. (equal_defs(restype.def,tcallnode(p).restype.def))) or
  2397. (not(cnf_restypeset in callnodeflags) and not(cnf_restypeset in tcallnode(p).callnodeflags)));
  2398. end;
  2399. procedure tcallnode.printnodedata(var t:text);
  2400. begin
  2401. if assigned(procdefinition) and
  2402. (procdefinition.deftype=procdef) then
  2403. writeln(t,printnodeindention,'proc = ',tprocdef(procdefinition).fullprocname(true))
  2404. else
  2405. begin
  2406. if assigned(symtableprocentry) then
  2407. writeln(t,printnodeindention,'proc = ',symtableprocentry.name)
  2408. else
  2409. writeln(t,printnodeindention,'proc = <nil>');
  2410. end;
  2411. printnode(t,methodpointer);
  2412. printnode(t,right);
  2413. printnode(t,left);
  2414. end;
  2415. begin
  2416. ccallnode:=tcallnode;
  2417. ccallparanode:=tcallparanode;
  2418. end.