ncal.pas 107 KB

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