ncal.pas 105 KB

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