ncal.pas 128 KB

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