ncal.pas 125 KB

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