ncal.pas 137 KB

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