ncal.pas 107 KB

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