ncal.pas 114 KB

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