ncal.pas 126 KB

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