ncal.pas 104 KB

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