ncal.pas 147 KB

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