ncal.pas 195 KB

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