ncal.pas 127 KB

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