ncal.pas 154 KB

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