ncal.pas 107 KB

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