ncal.pas 104 KB

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