ncal.pas 107 KB

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