ncal.pas 103 KB

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