ncal.pas 145 KB

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