ncal.pas 136 KB

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