ncal.pas 126 KB

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