ncal.pas 174 KB

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