ncal.pas 158 KB

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