ncal.pas 105 KB

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