ncal.pas 129 KB

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