ncal.pas 128 KB

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