ncal.pas 113 KB

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