ncal.pas 118 KB

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