ncal.pas 129 KB

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