ncal.pas 124 KB

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