ncal.pas 194 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656
  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. { must be copied before the funcretnode, because the callcleanup block
  1470. may contain a ttempdeletenode that sets the tempinfo of the
  1471. corresponding temp to ti_nextref_set_hookoncopy_nil, and this nextref
  1472. itself may be the funcretnode }
  1473. if assigned(callcleanupblock) then
  1474. n.callcleanupblock:=tblocknode(callcleanupblock.dogetcopy)
  1475. else
  1476. n.callcleanupblock:=nil;
  1477. if assigned(funcretnode) then
  1478. n.funcretnode:=funcretnode.dogetcopy
  1479. else
  1480. n.funcretnode:=nil;
  1481. if assigned(varargsparas) then
  1482. begin
  1483. n.varargsparas:=tvarargsparalist.create(true);
  1484. for i:=0 to varargsparas.count-1 do
  1485. begin
  1486. hp:=tparavarsym(varargsparas[i]);
  1487. hpn:=cparavarsym.create(hp.realname,hp.paranr,hp.varspez,hp.vardef,[]);
  1488. n.varargsparas.add(hpn);
  1489. para:=tcallparanode(n.left);
  1490. while assigned(para) do
  1491. begin
  1492. if (para.parasym=hp) then
  1493. para.parasym:=hpn;
  1494. para:=tcallparanode(para.right);
  1495. end;
  1496. end;
  1497. end
  1498. else
  1499. n.varargsparas:=nil;
  1500. {$ifdef symansistr}
  1501. n.fforcedprocname:=fforcedprocname;
  1502. {$else symansistr}
  1503. if assigned(fforcedprocname) then
  1504. n.fforcedprocname:=stringdup(fforcedprocname^)
  1505. else
  1506. n.fforcedprocname:=nil;
  1507. {$endif symansistr}
  1508. result:=n;
  1509. end;
  1510. function tcallnode.docompare(p: tnode): boolean;
  1511. begin
  1512. docompare :=
  1513. inherited docompare(p) and
  1514. (symtableprocentry = tcallnode(p).symtableprocentry) and
  1515. (procdefinition = tcallnode(p).procdefinition) and
  1516. (methodpointer.isequal(tcallnode(p).methodpointer)) and
  1517. (((cnf_typedefset in callnodeflags) and (cnf_typedefset in tcallnode(p).callnodeflags) and
  1518. (equal_defs(typedef,tcallnode(p).typedef))) or
  1519. (not(cnf_typedefset in callnodeflags) and not(cnf_typedefset in tcallnode(p).callnodeflags)));
  1520. end;
  1521. procedure tcallnode.printnodedata(var t:text);
  1522. begin
  1523. if assigned(procdefinition) and
  1524. (procdefinition.typ=procdef) then
  1525. writeln(t,printnodeindention,'proc = ',tprocdef(procdefinition).fullprocname(true))
  1526. else
  1527. begin
  1528. if assigned(symtableprocentry) then
  1529. writeln(t,printnodeindention,'proc = ',symtableprocentry.name)
  1530. else
  1531. writeln(t,printnodeindention,'proc = <nil>');
  1532. end;
  1533. if assigned(methodpointer) then
  1534. begin
  1535. writeln(t,printnodeindention,'methodpointer =');
  1536. printnode(t,methodpointer);
  1537. end;
  1538. if assigned(callinitblock) then
  1539. begin
  1540. writeln(t,printnodeindention,'callinitblock =');
  1541. printnode(t,callinitblock);
  1542. end;
  1543. if assigned(callcleanupblock) then
  1544. begin
  1545. writeln(t,printnodeindention,'callcleanupblock =');
  1546. printnode(t,callcleanupblock);
  1547. end;
  1548. if assigned(right) then
  1549. begin
  1550. writeln(t,printnodeindention,'right =');
  1551. printnode(t,right);
  1552. end;
  1553. if assigned(left) then
  1554. begin
  1555. writeln(t,printnodeindention,'left =');
  1556. printnode(t,left);
  1557. end;
  1558. end;
  1559. procedure tcallnode.insertintolist(l : tnodelist);
  1560. begin
  1561. end;
  1562. procedure tcallnode.add_init_statement(n:tnode);
  1563. var
  1564. lastinitstatement : tstatementnode;
  1565. begin
  1566. if not assigned(callinitblock) then
  1567. callinitblock:=internalstatements(lastinitstatement)
  1568. else
  1569. lastinitstatement:=laststatement(callinitblock);
  1570. { all these nodes must be immediately typechecked, because this routine }
  1571. { can be called from pass_1 (i.e., after typecheck has already run) and }
  1572. { moreover, the entire blocks themselves are also only typechecked in }
  1573. { pass_1, while the the typeinfo is already required after the }
  1574. { typecheck pass for simplify purposes (not yet perfect, because the }
  1575. { statementnodes themselves are not typechecked this way) }
  1576. typecheckpass(n);
  1577. addstatement(lastinitstatement,n);
  1578. end;
  1579. procedure tcallnode.add_done_statement(n:tnode);
  1580. var
  1581. lastdonestatement : tstatementnode;
  1582. begin
  1583. if not assigned(callcleanupblock) then
  1584. callcleanupblock:=internalstatements(lastdonestatement)
  1585. else
  1586. lastdonestatement:=laststatement(callcleanupblock);
  1587. { see comments in add_init_statement }
  1588. typecheckpass(n);
  1589. addstatement(lastdonestatement,n);
  1590. end;
  1591. function tcallnode.para_count:longint;
  1592. var
  1593. ppn : tcallparanode;
  1594. begin
  1595. result:=0;
  1596. ppn:=tcallparanode(left);
  1597. while assigned(ppn) do
  1598. begin
  1599. if not(assigned(ppn.parasym) and
  1600. (vo_is_hidden_para in ppn.parasym.varoptions)) then
  1601. inc(result);
  1602. ppn:=tcallparanode(ppn.right);
  1603. end;
  1604. end;
  1605. function tcallnode.required_para_count: longint;
  1606. var
  1607. ppn : tcallparanode;
  1608. begin
  1609. result:=0;
  1610. ppn:=tcallparanode(left);
  1611. while assigned(ppn) do
  1612. begin
  1613. if not(assigned(ppn.parasym) and
  1614. ((vo_is_hidden_para in ppn.parasym.varoptions) or
  1615. assigned(ppn.parasym.defaultconstsym))) then
  1616. inc(result);
  1617. ppn:=tcallparanode(ppn.right);
  1618. end;
  1619. end;
  1620. function tcallnode.is_simple_para_load(p:tnode; may_be_in_reg: boolean):boolean;
  1621. var
  1622. hp : tnode;
  1623. begin
  1624. hp:=p;
  1625. while assigned(hp) and
  1626. (hp.nodetype=typeconvn) and
  1627. (ttypeconvnode(hp).convtype=tc_equal) do
  1628. hp:=tunarynode(hp).left;
  1629. result:=(hp.nodetype in [typen,loadvmtaddrn,loadn,temprefn,arrayconstructorn,addrn]);
  1630. if result and
  1631. not(may_be_in_reg) then
  1632. case hp.nodetype of
  1633. loadn:
  1634. result:=(tabstractvarsym(tloadnode(hp).symtableentry).varregable in [vr_none,vr_addr]);
  1635. temprefn:
  1636. result:=not(ti_may_be_in_reg in ttemprefnode(hp).tempinfo^.flags);
  1637. end;
  1638. end;
  1639. function tcallnode.getforcedprocname: TSymStr;
  1640. begin
  1641. {$ifdef symansistr}
  1642. result:=fforcedprocname;
  1643. {$else}
  1644. if assigned(fforcedprocname) then
  1645. result:=fforcedprocname^
  1646. else
  1647. result:='';
  1648. {$endif}
  1649. end;
  1650. function look_for_call(var n: tnode; arg: pointer): foreachnoderesult;
  1651. begin
  1652. case n.nodetype of
  1653. calln,asn:
  1654. result := fen_norecurse_true;
  1655. typen,loadvmtaddrn,loadn,temprefn,arrayconstructorn:
  1656. result := fen_norecurse_false;
  1657. else
  1658. result := fen_false;
  1659. end;
  1660. end;
  1661. procedure tcallnode.maybe_load_in_temp(var p:tnode);
  1662. var
  1663. loadp,
  1664. refp : tnode;
  1665. hdef : tdef;
  1666. ptemp : ttempcreatenode;
  1667. usederef : boolean;
  1668. begin
  1669. { Load all complex loads into a temp to prevent
  1670. double calls to a function. We can't simply check for a hp.nodetype=calln }
  1671. if assigned(p) and
  1672. foreachnodestatic(p,@look_for_call,nil) then
  1673. begin
  1674. { temp create }
  1675. usederef:=(p.resultdef.typ in [arraydef,recorddef]) or
  1676. is_shortstring(p.resultdef) or
  1677. is_object(p.resultdef);
  1678. if usederef then
  1679. hdef:=getpointerdef(p.resultdef)
  1680. else
  1681. hdef:=p.resultdef;
  1682. ptemp:=ctempcreatenode.create(hdef,hdef.size,tt_persistent,true);
  1683. if usederef then
  1684. begin
  1685. loadp:=caddrnode.create_internal(p);
  1686. refp:=cderefnode.create(ctemprefnode.create(ptemp));
  1687. end
  1688. else
  1689. begin
  1690. loadp:=p;
  1691. refp:=ctemprefnode.create(ptemp)
  1692. end;
  1693. add_init_statement(ptemp);
  1694. add_init_statement(cassignmentnode.create(
  1695. ctemprefnode.create(ptemp),
  1696. loadp));
  1697. add_done_statement(ctempdeletenode.create(ptemp));
  1698. { new tree is only a temp reference }
  1699. p:=refp;
  1700. typecheckpass(p);
  1701. end;
  1702. end;
  1703. function tcallnode.gen_high_tree(var p:tnode;paradef:tdef):tnode;
  1704. { When passing an array to an open array, or a string to an open string,
  1705. some code is needed that generates the high bound of the array. This
  1706. function returns a tree containing the nodes for it. }
  1707. var
  1708. temp: tnode;
  1709. len : integer;
  1710. loadconst : boolean;
  1711. hightree,l,r : tnode;
  1712. defkind: tdeftyp;
  1713. begin
  1714. len:=-1;
  1715. loadconst:=true;
  1716. hightree:=nil;
  1717. { constant strings are internally stored as array of char, but if the
  1718. parameter is a string also treat it like one }
  1719. defkind:=p.resultdef.typ;
  1720. if (p.nodetype=stringconstn) and
  1721. (paradef.typ=stringdef) then
  1722. defkind:=stringdef;
  1723. case defkind of
  1724. arraydef :
  1725. begin
  1726. if (paradef.typ<>arraydef) then
  1727. internalerror(200405241);
  1728. { passing a string to an array of char }
  1729. if (p.nodetype=stringconstn) and
  1730. is_char(tarraydef(paradef).elementdef) then
  1731. begin
  1732. len:=tstringconstnode(p).len;
  1733. if len>0 then
  1734. dec(len);
  1735. end
  1736. else
  1737. { handle special case of passing an single array to an array of array }
  1738. if compare_defs(tarraydef(paradef).elementdef,p.resultdef,nothingn)>=te_equal then
  1739. len:=0
  1740. else
  1741. begin
  1742. { handle via a normal inline in_high_x node }
  1743. loadconst:=false;
  1744. { slice? }
  1745. if (p.nodetype=inlinen) and (tinlinenode(p).inlinenumber=in_slice_x) then
  1746. with Tcallparanode(Tinlinenode(p).left) do
  1747. begin
  1748. {Array slice using slice builtin function.}
  1749. l:=Tcallparanode(right).left;
  1750. hightree:=caddnode.create(subn,l,genintconstnode(1));
  1751. Tcallparanode(right).left:=nil;
  1752. {Remove the inline node.}
  1753. temp:=p;
  1754. p:=left;
  1755. Tcallparanode(tinlinenode(temp).left).left:=nil;
  1756. temp.free;
  1757. typecheckpass(hightree);
  1758. end
  1759. else if (p.nodetype=vecn) and (Tvecnode(p).right.nodetype=rangen) then
  1760. begin
  1761. {Array slice using .. operator.}
  1762. with Trangenode(Tvecnode(p).right) do
  1763. begin
  1764. l:=left; {Get lower bound.}
  1765. r:=right; {Get upper bound.}
  1766. end;
  1767. {In the procedure the array range is 0..(upper_bound-lower_bound).}
  1768. hightree:=caddnode.create(subn,r,l);
  1769. {Replace the rangnode in the tree by its lower_bound, and
  1770. dispose the rangenode.}
  1771. temp:=Tvecnode(p).right;
  1772. Tvecnode(p).right:=l.getcopy;
  1773. {Typecheckpass can only be performed *after* the l.getcopy since it
  1774. can modify the tree, and l is in the hightree.}
  1775. typecheckpass(hightree);
  1776. with Trangenode(temp) do
  1777. begin
  1778. left:=nil;
  1779. right:=nil;
  1780. end;
  1781. temp.free;
  1782. {Tree changed from p[l..h] to p[l], recalculate resultdef.}
  1783. p.resultdef:=nil;
  1784. typecheckpass(p);
  1785. end
  1786. else
  1787. begin
  1788. maybe_load_in_temp(p);
  1789. hightree:=geninlinenode(in_high_x,false,p.getcopy);
  1790. typecheckpass(hightree);
  1791. { only substract low(array) if it's <> 0 }
  1792. temp:=geninlinenode(in_low_x,false,p.getcopy);
  1793. typecheckpass(temp);
  1794. if (temp.nodetype <> ordconstn) or
  1795. (tordconstnode(temp).value <> 0) then
  1796. begin
  1797. hightree:=caddnode.create(subn,hightree,temp);
  1798. include(hightree.flags,nf_internal);
  1799. end
  1800. else
  1801. temp.free;
  1802. end;
  1803. end;
  1804. end;
  1805. stringdef :
  1806. begin
  1807. if is_open_string(paradef) then
  1808. begin
  1809. { a stringconstn is not a simple parameter and hence would be
  1810. loaded in a temp, but in that case the high() node
  1811. a) goes wrong (it cannot deal with a temp node)
  1812. b) would give a generic result instead of one specific to
  1813. this constant string
  1814. }
  1815. if p.nodetype<>stringconstn then
  1816. maybe_load_in_temp(p);
  1817. { handle via a normal inline in_high_x node }
  1818. loadconst := false;
  1819. hightree := geninlinenode(in_high_x,false,p.getcopy);
  1820. end
  1821. else
  1822. { handle special case of passing an single string to an array of string }
  1823. if compare_defs(tarraydef(paradef).elementdef,p.resultdef,nothingn)>=te_equal then
  1824. len:=0
  1825. else
  1826. { passing a string to an array of char }
  1827. if (p.nodetype=stringconstn) and
  1828. is_char(tarraydef(paradef).elementdef) then
  1829. begin
  1830. len:=tstringconstnode(p).len;
  1831. if len>0 then
  1832. dec(len);
  1833. end
  1834. else
  1835. begin
  1836. maybe_load_in_temp(p);
  1837. hightree:=caddnode.create(subn,geninlinenode(in_length_x,false,p.getcopy),
  1838. cordconstnode.create(1,sinttype,false));
  1839. loadconst:=false;
  1840. end;
  1841. end;
  1842. else
  1843. len:=0;
  1844. end;
  1845. if loadconst then
  1846. hightree:=cordconstnode.create(len,sinttype,true)
  1847. else
  1848. begin
  1849. if not assigned(hightree) then
  1850. internalerror(200304071);
  1851. { Need to use explicit, because it can also be a enum }
  1852. hightree:=ctypeconvnode.create_internal(hightree,sinttype);
  1853. end;
  1854. result:=hightree;
  1855. end;
  1856. function tcallnode.gen_procvar_context_tree_self:tnode;
  1857. begin
  1858. { Load tmehodpointer(right).self }
  1859. result:=genloadfield(ctypeconvnode.create_internal(
  1860. right.getcopy,methodpointertype),
  1861. 'self');
  1862. end;
  1863. function tcallnode.gen_procvar_context_tree_parentfp: tnode;
  1864. begin
  1865. { Load tnestedprocpointer(right).parentfp }
  1866. result:=genloadfield(ctypeconvnode.create_internal(
  1867. right.getcopy,nestedprocpointertype),
  1868. 'parentfp');
  1869. end;
  1870. function tcallnode.gen_self_tree:tnode;
  1871. var
  1872. selftree : tnode;
  1873. selfdef : tdef;
  1874. temp : ttempcreatenode;
  1875. begin
  1876. selftree:=nil;
  1877. { When methodpointer was a callnode we must load it first into a
  1878. temp to prevent processing the callnode twice }
  1879. if (methodpointer.nodetype=calln) then
  1880. internalerror(200405121);
  1881. { Objective-C: objc_convert_to_message_send() already did all necessary
  1882. transformation on the methodpointer }
  1883. if (procdefinition.typ=procdef) and
  1884. (po_objc in tprocdef(procdefinition).procoptions) then
  1885. selftree:=methodpointer.getcopy
  1886. { inherited }
  1887. else if (cnf_inherited in callnodeflags) then
  1888. begin
  1889. selftree:=call_self_node.getcopy;
  1890. { we can call an inherited class static/method from a regular method
  1891. -> self node must change from instance pointer to vmt pointer)
  1892. }
  1893. if (procdefinition.procoptions*[po_classmethod,po_staticmethod] <> []) and
  1894. (selftree.resultdef.typ<>classrefdef) then
  1895. selftree:=cloadvmtaddrnode.create(selftree);
  1896. end
  1897. else
  1898. { constructors }
  1899. if (procdefinition.proctypeoption=potype_constructor) then
  1900. begin
  1901. if (methodpointer.resultdef.typ=classrefdef) or
  1902. (cnf_new_call in callnodeflags) then
  1903. if not is_javaclass(tdef(procdefinition.owner.defowner)) then
  1904. begin
  1905. if (cnf_new_call in callnodeflags) then
  1906. { old-style object: push 0 as self }
  1907. selftree:=cpointerconstnode.create(0,voidpointertype)
  1908. else
  1909. begin
  1910. { class-style: push classtype }
  1911. selftree:=methodpointer.getcopy;
  1912. if selftree.nodetype=typen then
  1913. begin
  1914. selftree:=cloadvmtaddrnode.create(selftree);
  1915. tloadvmtaddrnode(selftree).forcall:=true;
  1916. end;
  1917. end;
  1918. end
  1919. else
  1920. { special handling for Java constructors, handled in
  1921. tjvmcallnode.extra_pre_call_code }
  1922. selftree:=cnothingnode.create
  1923. else
  1924. begin
  1925. if methodpointer.nodetype=typen then
  1926. if (methodpointer.resultdef.typ<>objectdef) then
  1927. begin
  1928. if not(target_info.system in systems_jvm) then
  1929. begin
  1930. { TSomeRecord.Constructor call. We need to allocate }
  1931. { self node as a temp node of the result type }
  1932. temp:=ctempcreatenode.create(methodpointer.resultdef,methodpointer.resultdef.size,tt_persistent,false);
  1933. add_init_statement(temp);
  1934. add_done_statement(ctempdeletenode.create_normal_temp(temp));
  1935. selftree:=ctemprefnode.create(temp);
  1936. end
  1937. else
  1938. begin
  1939. { special handling for Java constructors, handled in
  1940. tjvmcallnode.extra_pre_call_code }
  1941. selftree:=cnothingnode.create
  1942. end;
  1943. end
  1944. else
  1945. selftree:=call_self_node.getcopy
  1946. else
  1947. selftree:=methodpointer.getcopy;
  1948. end;
  1949. end
  1950. else
  1951. { Calling a static/class method }
  1952. if (po_classmethod in procdefinition.procoptions) or
  1953. (po_staticmethod in procdefinition.procoptions) then
  1954. begin
  1955. if (procdefinition.typ<>procdef) then
  1956. internalerror(200305062);
  1957. { if the method belongs to a helper then we need to use the
  1958. extended type for references to Self }
  1959. if is_objectpascal_helper(tprocdef(procdefinition).struct) then
  1960. selfdef:=tobjectdef(tprocdef(procdefinition).struct).extendeddef
  1961. else
  1962. selfdef:=tprocdef(procdefinition).struct;
  1963. if ((selfdef.typ in [recorddef,objectdef]) and
  1964. (oo_has_vmt in tabstractrecorddef(selfdef).objectoptions)) or
  1965. { all Java classes have a "VMT" }
  1966. (target_info.system in systems_jvm) then
  1967. begin
  1968. { we only need the vmt, loading self is not required and there is no
  1969. need to check for typen, because that will always get the
  1970. loadvmtaddrnode added }
  1971. selftree:=methodpointer.getcopy;
  1972. if (methodpointer.resultdef.typ<>classrefdef) or
  1973. (methodpointer.nodetype = typen) then
  1974. selftree:=cloadvmtaddrnode.create(selftree);
  1975. end
  1976. else
  1977. selftree:=cpointerconstnode.create(0,voidpointertype);
  1978. end
  1979. else
  1980. begin
  1981. if methodpointer.nodetype=typen then
  1982. selftree:=call_self_node.getcopy
  1983. else
  1984. selftree:=methodpointer.getcopy;
  1985. end;
  1986. result:=selftree;
  1987. end;
  1988. procedure tcallnode.register_created_object_types;
  1989. function checklive(def: tdef): boolean;
  1990. begin
  1991. if assigned(current_procinfo) and
  1992. not(po_inline in current_procinfo.procdef.procoptions) and
  1993. not wpoinfomanager.symbol_live(current_procinfo.procdef.mangledname) then
  1994. begin
  1995. {$ifdef debug_deadcode}
  1996. writeln(' NOT adding creadion of ',def.typename,' because performed in dead stripped proc: ',current_procinfo.procdef.typename);
  1997. {$endif debug_deadcode}
  1998. result:=false;
  1999. end
  2000. else
  2001. result:=true;
  2002. end;
  2003. var
  2004. crefdef,
  2005. systobjectdef : tdef;
  2006. begin
  2007. { only makes sense for methods }
  2008. if not assigned(methodpointer) then
  2009. exit;
  2010. if (methodpointer.resultdef.typ=classrefdef) then
  2011. begin
  2012. { constructor call via classreference => allocate memory }
  2013. if (procdefinition.proctypeoption=potype_constructor) then
  2014. begin
  2015. { Only a typenode can be passed when it is called with <class of xx>.create }
  2016. if (methodpointer.nodetype=typen) then
  2017. begin
  2018. if checklive(methodpointer.resultdef) then
  2019. { we know the exact class type being created }
  2020. tclassrefdef(methodpointer.resultdef).pointeddef.register_created_object_type
  2021. end
  2022. else
  2023. begin
  2024. { the loadvmtaddrnode is already created in case of classtype.create }
  2025. if (methodpointer.nodetype=loadvmtaddrn) and
  2026. (tloadvmtaddrnode(methodpointer).left.nodetype=typen) then
  2027. begin
  2028. if checklive(methodpointer.resultdef) then
  2029. tclassrefdef(methodpointer.resultdef).pointeddef.register_created_object_type
  2030. end
  2031. else
  2032. begin
  2033. if checklive(methodpointer.resultdef) then
  2034. begin
  2035. { special case: if the classref comes from x.classtype (with classtype,
  2036. being tobject.classtype) then the created instance is x or a descendant
  2037. of x (rather than tobject or a descendant of tobject)
  2038. }
  2039. systobjectdef:=search_system_type('TOBJECT').typedef;
  2040. if (methodpointer.nodetype=calln) and
  2041. { not a procvar call }
  2042. not assigned(right) and
  2043. { procdef is owned by system.tobject }
  2044. (tprocdef(tcallnode(methodpointer).procdefinition).owner.defowner=systobjectdef) and
  2045. { we're calling system.tobject.classtype }
  2046. (tcallnode(methodpointer).symtableprocentry.name='CLASSTYPE') and
  2047. { could again be a classrefdef, but unlikely }
  2048. (tcallnode(methodpointer).methodpointer.resultdef.typ=objectdef) and
  2049. { don't go through this trouble if it was already a tobject }
  2050. (tcallnode(methodpointer).methodpointer.resultdef<>systobjectdef) then
  2051. begin
  2052. { register this object type as classref, so all descendents will also
  2053. be marked as instantiatable (only the pointeddef will actually be
  2054. recorded, so it's no problem that the clasrefdef is only temporary)
  2055. }
  2056. crefdef:=cclassrefdef.create(tcallnode(methodpointer).methodpointer.resultdef);
  2057. { and register it }
  2058. crefdef.register_created_object_type;
  2059. end
  2060. else
  2061. { the created class can be any child class as well -> register classrefdef }
  2062. methodpointer.resultdef.register_created_object_type;
  2063. end;
  2064. end;
  2065. end;
  2066. end
  2067. end
  2068. else
  2069. { Old style object }
  2070. if is_object(methodpointer.resultdef) then
  2071. begin
  2072. { constructor with extended syntax called from new }
  2073. if (cnf_new_call in callnodeflags) then
  2074. begin
  2075. if checklive(methodpointer.resultdef) then
  2076. methodpointer.resultdef.register_created_object_type;
  2077. end
  2078. else
  2079. { normal object call like obj.proc }
  2080. if not(cnf_dispose_call in callnodeflags) and
  2081. not(cnf_inherited in callnodeflags) and
  2082. not(cnf_member_call in callnodeflags) then
  2083. begin
  2084. if (procdefinition.proctypeoption=potype_constructor) then
  2085. begin
  2086. if (methodpointer.nodetype<>typen) and
  2087. checklive(methodpointer.resultdef) then
  2088. methodpointer.resultdef.register_created_object_type;
  2089. end
  2090. end;
  2091. end;
  2092. end;
  2093. function tcallnode.get_expect_loc: tcgloc;
  2094. var
  2095. realresdef: tstoreddef;
  2096. begin
  2097. if not assigned(typedef) then
  2098. realresdef:=tstoreddef(resultdef)
  2099. else
  2100. realresdef:=tstoreddef(typedef);
  2101. if realresdef.is_intregable then
  2102. result:=LOC_REGISTER
  2103. else if (realresdef.typ=floatdef) and
  2104. not(cs_fp_emulation in current_settings.moduleswitches) then
  2105. if use_vectorfpu(realresdef) then
  2106. result:=LOC_MMREGISTER
  2107. else
  2108. result:=LOC_FPUREGISTER
  2109. else
  2110. result:=LOC_REFERENCE
  2111. end;
  2112. procedure tcallnode.gen_vmt_entry_load;
  2113. var
  2114. vmt_def: trecorddef;
  2115. begin
  2116. if not assigned(right) and
  2117. (forcedprocname='') and
  2118. (po_virtualmethod in procdefinition.procoptions) and
  2119. not is_objectpascal_helper(tprocdef(procdefinition).struct) and
  2120. assigned(methodpointer) and
  2121. (methodpointer.nodetype<>typen) then
  2122. begin
  2123. vmt_entry:=load_vmt_for_self_node(methodpointer.getcopy);
  2124. { get the right entry in the VMT }
  2125. vmt_entry:=cderefnode.create(vmt_entry);
  2126. typecheckpass(vmt_entry);
  2127. vmt_def:=trecorddef(vmt_entry.resultdef);
  2128. { tobjectdef(tprocdef(procdefinition).struct) can be a parent of the
  2129. methodpointer's resultdef, but the vmtmethodoffset of the method
  2130. in that objectdef is obviously the same as in any child class }
  2131. vmt_entry:=csubscriptnode.create(
  2132. trecordsymtable(vmt_def.symtable).findfieldbyoffset(
  2133. tobjectdef(tprocdef(procdefinition).struct).vmtmethodoffset(tprocdef(procdefinition).extnumber)
  2134. ),
  2135. vmt_entry
  2136. );
  2137. firstpass(vmt_entry);
  2138. end;
  2139. end;
  2140. procedure tcallnode.gen_syscall_para(para: tcallparanode);
  2141. begin
  2142. { unsupported }
  2143. internalerror(2014040101);
  2144. end;
  2145. procedure tcallnode.objc_convert_to_message_send;
  2146. var
  2147. block,
  2148. selftree : tnode;
  2149. statements : tstatementnode;
  2150. field : tfieldvarsym;
  2151. temp : ttempcreatenode;
  2152. selfrestype,
  2153. objcsupertype : tdef;
  2154. srsym : tsym;
  2155. srsymtable : tsymtable;
  2156. msgsendname : string;
  2157. begin
  2158. if not(m_objectivec1 in current_settings.modeswitches) then
  2159. Message(parser_f_modeswitch_objc_required);
  2160. { typecheck pass must already have run on the call node,
  2161. because pass1 calls this method
  2162. }
  2163. { default behaviour: call objc_msgSend and friends;
  2164. 64 bit targets for Mac OS X can override this as they
  2165. can call messages via an indirect function call similar to
  2166. dynamically linked functions, ARM maybe as well (not checked)
  2167. Which variant of objc_msgSend is used depends on the
  2168. result type, and on whether or not it's an inherited call.
  2169. }
  2170. { make sure we don't perform this transformation twice in case
  2171. firstpass would be called multiple times }
  2172. include(callnodeflags,cnf_objc_processed);
  2173. { make sure the methodpointer doesn't get translated into a call
  2174. as well (endless loop) }
  2175. if methodpointer.nodetype=loadvmtaddrn then
  2176. tloadvmtaddrnode(methodpointer).forcall:=true;
  2177. { A) set the appropriate objc_msgSend* variant to call }
  2178. { The AArch64 abi does not require special handling for struct returns }
  2179. {$ifndef aarch64}
  2180. { record returned via implicit pointer }
  2181. if paramanager.ret_in_param(resultdef,procdefinition) then
  2182. begin
  2183. if not(cnf_inherited in callnodeflags) then
  2184. msgsendname:='OBJC_MSGSEND_STRET'
  2185. {$if defined(onlymacosx10_6) or defined(arm) }
  2186. else if (target_info.system in systems_objc_nfabi) then
  2187. msgsendname:='OBJC_MSGSENDSUPER2_STRET'
  2188. {$endif onlymacosx10_6 or arm}
  2189. else
  2190. msgsendname:='OBJC_MSGSENDSUPER_STRET'
  2191. end
  2192. {$ifdef i386}
  2193. { special case for fpu results on i386 for non-inherited calls }
  2194. { TODO: also for x86_64 "extended" results }
  2195. else if (resultdef.typ=floatdef) and
  2196. not(cnf_inherited in callnodeflags) then
  2197. msgsendname:='OBJC_MSGSEND_FPRET'
  2198. {$endif i386}
  2199. { default }
  2200. else
  2201. {$endif aarch64}
  2202. if not(cnf_inherited in callnodeflags) then
  2203. msgsendname:='OBJC_MSGSEND'
  2204. {$if defined(onlymacosx10_6) or defined(arm) or defined(aarch64)}
  2205. else if (target_info.system in systems_objc_nfabi) then
  2206. msgsendname:='OBJC_MSGSENDSUPER2'
  2207. {$endif onlymacosx10_6 or arm}
  2208. else
  2209. msgsendname:='OBJC_MSGSENDSUPER';
  2210. { get the mangled name }
  2211. srsym:=nil;
  2212. if not searchsym_in_named_module('OBJC',msgsendname,srsym,srsymtable) or
  2213. (srsym.typ<>procsym) or
  2214. (tprocsym(srsym).ProcdefList.count<>1) then
  2215. Message1(cg_f_unknown_compilerproc,'objc.'+msgsendname);
  2216. {$ifdef symansistr}
  2217. fforcedprocname:=tprocdef(tprocsym(srsym).ProcdefList[0]).mangledname;
  2218. {$else symansistr}
  2219. fforcedprocname:=stringdup(tprocdef(tprocsym(srsym).ProcdefList[0]).mangledname);
  2220. {$endif symansistr}
  2221. { B) Handle self }
  2222. { 1) in case of sending a message to a superclass, self is a pointer to
  2223. an objc_super record
  2224. }
  2225. if (cnf_inherited in callnodeflags) then
  2226. begin
  2227. block:=internalstatements(statements);
  2228. objcsupertype:=search_named_unit_globaltype('OBJC','OBJC_SUPER',true).typedef;
  2229. if (objcsupertype.typ<>recorddef) then
  2230. internalerror(2009032901);
  2231. { temp for the for the objc_super record }
  2232. temp:=ctempcreatenode.create(objcsupertype,objcsupertype.size,tt_persistent,false);
  2233. addstatement(statements,temp);
  2234. { initialize objc_super record }
  2235. selftree:=call_self_node.getcopy;
  2236. { we can call an inherited class static/method from a regular method
  2237. -> self node must change from instance pointer to vmt pointer)
  2238. }
  2239. if (po_classmethod in procdefinition.procoptions) and
  2240. (selftree.resultdef.typ<>classrefdef) then
  2241. begin
  2242. selftree:=cloadvmtaddrnode.create(selftree);
  2243. { since we're in a class method of the current class, its
  2244. information has already been initialized (and that of all of
  2245. its parent classes too) }
  2246. tloadvmtaddrnode(selftree).forcall:=true;
  2247. typecheckpass(selftree);
  2248. end;
  2249. selfrestype:=selftree.resultdef;
  2250. field:=tfieldvarsym(trecorddef(objcsupertype).symtable.find('RECEIVER'));
  2251. if not assigned(field) then
  2252. internalerror(2009032902);
  2253. { first the destination object/class instance }
  2254. addstatement(statements,
  2255. cassignmentnode.create(
  2256. csubscriptnode.create(field,ctemprefnode.create(temp)),
  2257. selftree
  2258. )
  2259. );
  2260. { and secondly, the class type in which the selector must be looked
  2261. up (the parent class in case of an instance method, the parent's
  2262. metaclass in case of a class method) }
  2263. field:=tfieldvarsym(trecorddef(objcsupertype).symtable.find('_CLASS'));
  2264. if not assigned(field) then
  2265. internalerror(2009032903);
  2266. addstatement(statements,
  2267. cassignmentnode.create(
  2268. csubscriptnode.create(field,ctemprefnode.create(temp)),
  2269. objcsuperclassnode(selftree.resultdef)
  2270. )
  2271. );
  2272. { result of this block is the address of this temp }
  2273. addstatement(statements,ctypeconvnode.create_internal(
  2274. caddrnode.create_internal(ctemprefnode.create(temp)),selfrestype)
  2275. );
  2276. { replace the method pointer with the address of this temp }
  2277. methodpointer.free;
  2278. methodpointer:=block;
  2279. typecheckpass(block);
  2280. end
  2281. else
  2282. { 2) regular call (not inherited) }
  2283. begin
  2284. { a) If we're calling a class method, use a class ref. }
  2285. if (po_classmethod in procdefinition.procoptions) and
  2286. ((methodpointer.nodetype=typen) or
  2287. (methodpointer.resultdef.typ<>classrefdef)) then
  2288. begin
  2289. methodpointer:=cloadvmtaddrnode.create(methodpointer);
  2290. { no need to obtain the class ref by calling class(), sending
  2291. this message will initialize it if necessary }
  2292. tloadvmtaddrnode(methodpointer).forcall:=true;
  2293. firstpass(methodpointer);
  2294. end;
  2295. end;
  2296. end;
  2297. function tcallnode.gen_vmt_tree:tnode;
  2298. var
  2299. vmttree : tnode;
  2300. begin
  2301. vmttree:=nil;
  2302. if not(procdefinition.proctypeoption in [potype_constructor,potype_destructor]) then
  2303. internalerror(200305051);
  2304. { When methodpointer was a callnode we must load it first into a
  2305. temp to prevent the processing callnode twice }
  2306. if (methodpointer.nodetype=calln) then
  2307. internalerror(200405122);
  2308. { Handle classes and legacy objects separate to make it
  2309. more maintainable }
  2310. if (methodpointer.resultdef.typ=classrefdef) then
  2311. begin
  2312. if not is_class(tclassrefdef(methodpointer.resultdef).pointeddef) then
  2313. internalerror(200501041);
  2314. { constructor call via classreference => allocate memory }
  2315. if (procdefinition.proctypeoption=potype_constructor) then
  2316. begin
  2317. vmttree:=cpointerconstnode.create(1,voidpointertype);
  2318. end
  2319. else { <class of xx>.destroy is not valid }
  2320. InternalError(2014020601);
  2321. end
  2322. else
  2323. { Class style objects }
  2324. if is_class(methodpointer.resultdef) then
  2325. begin
  2326. { inherited call, no create/destroy }
  2327. if (cnf_inherited in callnodeflags) then
  2328. vmttree:=cpointerconstnode.create(0,voidpointertype)
  2329. else
  2330. { do not create/destroy when called from member function
  2331. without specifying self explicit }
  2332. if (cnf_member_call in callnodeflags) then
  2333. begin
  2334. { destructor (in the same class, since cnf_member_call):
  2335. if not called from a destructor then
  2336. call beforedestruction and release instance, vmt=1
  2337. else
  2338. don't release instance, vmt=0
  2339. constructor (in the same class, since cnf_member_call):
  2340. if called from a constructor then
  2341. don't call afterconstruction, vmt=0
  2342. else
  2343. call afterconstrution but not NewInstance, vmt=-1 }
  2344. if (procdefinition.proctypeoption=potype_destructor) then
  2345. if (current_procinfo.procdef.proctypeoption<>potype_constructor) then
  2346. vmttree:=cpointerconstnode.create(1,voidpointertype)
  2347. else
  2348. vmttree:=cpointerconstnode.create(0,voidpointertype)
  2349. else if (current_procinfo.procdef.proctypeoption=potype_constructor) and
  2350. (procdefinition.proctypeoption=potype_constructor) then
  2351. vmttree:=cpointerconstnode.create(0,voidpointertype)
  2352. else
  2353. vmttree:=cpointerconstnode.create(TConstPtrUInt(-1),voidpointertype);
  2354. end
  2355. else
  2356. { normal call to method like cl1.proc }
  2357. begin
  2358. { destructor:
  2359. if not called from exception block in constructor
  2360. call beforedestruction and release instance, vmt=1
  2361. else
  2362. don't call beforedestruction and release instance, vmt=-1
  2363. constructor:
  2364. if called from a constructor in the same class using self.create then
  2365. don't call afterconstruction, vmt=0
  2366. else
  2367. call afterconstruction, vmt=1 }
  2368. if (procdefinition.proctypeoption=potype_destructor) then
  2369. if not(cnf_create_failed in callnodeflags) then
  2370. vmttree:=cpointerconstnode.create(1,voidpointertype)
  2371. else
  2372. vmttree:=cpointerconstnode.create(TConstPtrUInt(-1),voidpointertype)
  2373. else
  2374. begin
  2375. if (current_procinfo.procdef.proctypeoption=potype_constructor) and
  2376. (procdefinition.proctypeoption=potype_constructor) and
  2377. (methodpointer.nodetype=loadn) and
  2378. (loadnf_is_self in tloadnode(methodpointer).loadnodeflags) then
  2379. vmttree:=cpointerconstnode.create(0,voidpointertype)
  2380. else
  2381. vmttree:=cpointerconstnode.create(TConstPtrUInt(-1),voidpointertype);
  2382. end;
  2383. end;
  2384. end
  2385. else
  2386. { Old style object }
  2387. begin
  2388. { constructor with extended syntax called from new }
  2389. if (cnf_new_call in callnodeflags) then
  2390. vmttree:=cloadvmtaddrnode.create(ctypenode.create(methodpointer.resultdef))
  2391. else
  2392. { destructor with extended syntax called from dispose }
  2393. { value -1 is what fpc_help_constructor() changes VMT to when it allocates memory }
  2394. if (cnf_dispose_call in callnodeflags) then
  2395. vmttree:=cpointerconstnode.create(TConstPtrUInt(-1),voidpointertype)
  2396. else
  2397. { destructor called from exception block in constructor }
  2398. if (cnf_create_failed in callnodeflags) then
  2399. vmttree:=ctypeconvnode.create_internal(call_vmt_node.getcopy,voidpointertype)
  2400. else
  2401. { inherited call, no create/destroy }
  2402. if (cnf_inherited in callnodeflags) then
  2403. vmttree:=cpointerconstnode.create(0,voidpointertype)
  2404. else
  2405. { do not create/destroy when called from member function
  2406. without specifying self explicit }
  2407. if (cnf_member_call in callnodeflags) then
  2408. begin
  2409. { destructor: don't release instance, vmt=0
  2410. constructor: don't initialize instance, vmt=0 }
  2411. vmttree:=cpointerconstnode.create(0,voidpointertype)
  2412. end
  2413. else
  2414. { normal object call like obj.proc }
  2415. begin
  2416. { destructor: direct call, no dispose, vmt=0
  2417. constructor: initialize object, load vmt }
  2418. if (procdefinition.proctypeoption=potype_constructor) then
  2419. begin
  2420. { old styled inherited call? }
  2421. if (methodpointer.nodetype=typen) then
  2422. vmttree:=cpointerconstnode.create(0,voidpointertype)
  2423. else
  2424. vmttree:=cloadvmtaddrnode.create(ctypenode.create(methodpointer.resultdef))
  2425. end
  2426. else
  2427. vmttree:=cpointerconstnode.create(0,voidpointertype);
  2428. end;
  2429. end;
  2430. result:=vmttree;
  2431. end;
  2432. function tcallnode.gen_block_context: tnode;
  2433. begin
  2434. { the self parameter of a block invocation is that address of the
  2435. block literal (which is what right contains) }
  2436. result:=right.getcopy;
  2437. end;
  2438. function check_funcret_used_as_para(var n: tnode; arg: pointer): foreachnoderesult;
  2439. var
  2440. destsym : tsym absolute arg;
  2441. begin
  2442. result := fen_false;
  2443. if (n.nodetype=loadn) and
  2444. (tloadnode(n).symtableentry = destsym) then
  2445. result := fen_norecurse_true;
  2446. end;
  2447. function tcallnode.funcret_can_be_reused:boolean;
  2448. var
  2449. realassignmenttarget: tnode;
  2450. alignment: longint;
  2451. begin
  2452. result:=false;
  2453. { we are processing an assignment node? }
  2454. if not(assigned(aktassignmentnode) and
  2455. (aktassignmentnode.right=self) and
  2456. (aktassignmentnode.left.resultdef=resultdef)) then
  2457. exit;
  2458. { destination must be able to be passed as var parameter }
  2459. if not valid_for_var(aktassignmentnode.left,false) then
  2460. exit;
  2461. { destination must be a simple load so it doesn't need a temp when
  2462. it is evaluated }
  2463. if not is_simple_para_load(aktassignmentnode.left,false) then
  2464. exit;
  2465. { remove possible typecasts }
  2466. realassignmenttarget:=actualtargetnode(@aktassignmentnode.left)^;
  2467. { when it is not passed in a parameter it will only be used after the
  2468. function call }
  2469. if not paramanager.ret_in_param(resultdef,procdefinition) then
  2470. begin
  2471. { don't replace the function result if we are inlining and if the destination is complex, this
  2472. could lead to lengthy code in case the function result is used often and it is assigned e.g.
  2473. to a threadvar }
  2474. result:=not(cnf_do_inline in callnodeflags) or
  2475. (node_complexity(aktassignmentnode.left)<=1);
  2476. exit;
  2477. end;
  2478. { if the result is the same as the self parameter (in case of objects),
  2479. we can't optimise. We have to check this explicitly becaise
  2480. hidden parameters such as self have not yet been inserted at this
  2481. point
  2482. }
  2483. if assigned(methodpointer) and
  2484. realassignmenttarget.isequal(actualtargetnode(@methodpointer)^) then
  2485. exit;
  2486. { when we substitute a function result inside an inlined function,
  2487. we may take the address of this function result. Therefore the
  2488. substituted function result may not be in a register, as we cannot
  2489. take its address in that case }
  2490. if (realassignmenttarget.nodetype=temprefn) and
  2491. not(ti_addr_taken in ttemprefnode(realassignmenttarget).tempinfo^.flags) and
  2492. not(ti_may_be_in_reg in ttemprefnode(realassignmenttarget).tempinfo^.flags) then
  2493. begin
  2494. result:=true;
  2495. exit;
  2496. end;
  2497. if (realassignmenttarget.nodetype=loadn) and
  2498. { nested procedures may access the current procedure's locals }
  2499. (procdefinition.parast.symtablelevel=normal_function_level) and
  2500. { must be a local variable, a value para or a hidden function result }
  2501. { parameter (which can be passed by address, but in that case it got }
  2502. { through these same checks at the caller side and is thus safe }
  2503. (
  2504. (tloadnode(realassignmenttarget).symtableentry.typ=localvarsym) or
  2505. (
  2506. (tloadnode(realassignmenttarget).symtableentry.typ=paravarsym) and
  2507. ((tparavarsym(tloadnode(realassignmenttarget).symtableentry).varspez = vs_value) or
  2508. (vo_is_funcret in tparavarsym(tloadnode(realassignmenttarget).symtableentry).varoptions))
  2509. )
  2510. ) and
  2511. { the address may not have been taken of the variable/parameter, because }
  2512. { otherwise it's possible that the called function can access it via a }
  2513. { global variable or other stored state }
  2514. (
  2515. not(tabstractvarsym(tloadnode(realassignmenttarget).symtableentry).addr_taken) and
  2516. (tabstractvarsym(tloadnode(realassignmenttarget).symtableentry).varregable in [vr_none,vr_addr])
  2517. ) then
  2518. begin
  2519. { If the funcret is also used as a parameter we can't optimize because the funcret
  2520. and the parameter will point to the same address. That means that a change of the result variable
  2521. will result also in a change of the parameter value }
  2522. result:=not foreachnodestatic(left,@check_funcret_used_as_para,tloadnode(realassignmenttarget).symtableentry);
  2523. { ensure that it is aligned using the default alignment }
  2524. alignment:=tabstractvarsym(tloadnode(realassignmenttarget).symtableentry).vardef.alignment;
  2525. if (used_align(alignment,target_info.alignment.localalignmin,target_info.alignment.localalignmax)<>
  2526. used_align(alignment,current_settings.alignment.localalignmin,current_settings.alignment.localalignmax)) then
  2527. result:=false;
  2528. exit;
  2529. end;
  2530. end;
  2531. procedure tcallnode.maybe_create_funcret_node;
  2532. var
  2533. temp : ttempcreatenode;
  2534. begin
  2535. if procdefinition.proctypeoption=potype_constructor then
  2536. exit;
  2537. { For the function result we need to create a temp node for:
  2538. - Inlined functions
  2539. - Types requiring initialization/finalization
  2540. - Types passed in parameters }
  2541. if not is_void(resultdef) and
  2542. not assigned(funcretnode) and
  2543. (
  2544. (cnf_do_inline in callnodeflags) or
  2545. is_managed_type(resultdef) or
  2546. paramanager.ret_in_param(resultdef,procdefinition)
  2547. ) then
  2548. begin
  2549. { Optimize calls like x:=f() where we can use x directly as
  2550. result instead of using a temp. Condition is that x cannot be accessed from f().
  2551. This implies that x is a local variable or value parameter of the current block
  2552. and its address is not passed to f. One problem: what if someone takes the
  2553. address of x, puts it in a pointer variable/field and then accesses it that way
  2554. from within the function? This is solved (in a conservative way) using the
  2555. ti_addr_taken flag.
  2556. When the result is not not passed in a parameter there are no problem because
  2557. then it means only reference counted types (eg. ansistrings) that need a decr
  2558. of the refcount before being assigned. This is all done after the call so there
  2559. is no issue with exceptions and possible use of the old value in the called
  2560. function }
  2561. if funcret_can_be_reused then
  2562. begin
  2563. funcretnode:=aktassignmentnode.left.getcopy;
  2564. include(funcretnode.flags,nf_is_funcret);
  2565. { notify the assignment node that the assignment can be removed }
  2566. include(aktassignmentnode.flags,nf_assign_done_in_right);
  2567. end
  2568. else
  2569. begin
  2570. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,
  2571. (cnf_do_inline in callnodeflags) and
  2572. not(tabstractvarsym(tprocdef(procdefinition).funcretsym).varregable in [vr_none,vr_addr]));
  2573. include(temp.flags,nf_is_funcret);
  2574. { if a managed type is returned by reference, assigning something
  2575. to the result on the caller side will take care of decreasing
  2576. the reference count }
  2577. if paramanager.ret_in_param(resultdef,procdefinition) then
  2578. include(temp.tempinfo^.flags,ti_nofini);
  2579. add_init_statement(temp);
  2580. { When the function result is not used in an inlined function
  2581. we need to delete the temp. This can currently only be done by
  2582. a tempdeletenode and not after converting it to a normal temp }
  2583. if not(cnf_return_value_used in callnodeflags) and
  2584. (cnf_do_inline in callnodeflags) then
  2585. add_done_statement(ctempdeletenode.create(temp))
  2586. else
  2587. add_done_statement(ctempdeletenode.create_normal_temp(temp));
  2588. funcretnode:=ctemprefnode.create(temp);
  2589. include(funcretnode.flags,nf_is_funcret);
  2590. end;
  2591. end;
  2592. end;
  2593. procedure tcallnode.gen_hidden_parameters;
  2594. var
  2595. para : tcallparanode;
  2596. begin
  2597. para:=tcallparanode(left);
  2598. while assigned(para) do
  2599. begin
  2600. { The processing of high() and typeinfo() is already
  2601. done in the typecheckpass. We only need to process the
  2602. nodes that still have a nothingn }
  2603. if (vo_is_hidden_para in para.parasym.varoptions) and
  2604. (para.left.nodetype=nothingn) then
  2605. begin
  2606. { remove dummy nothingn }
  2607. para.left.free;
  2608. para.left:=nil;
  2609. { generate the corresponding nodes for the hidden parameter type }
  2610. if (vo_is_funcret in para.parasym.varoptions) then
  2611. begin
  2612. if not assigned(funcretnode) then
  2613. internalerror(200709083);
  2614. para.left:=funcretnode;
  2615. funcretnode:=nil;
  2616. end
  2617. else
  2618. if vo_is_self in para.parasym.varoptions then
  2619. begin
  2620. if assigned(right) then
  2621. para.left:=gen_procvar_context_tree_self
  2622. else
  2623. para.left:=gen_self_tree;
  2624. { make sure that e.g. the self pointer of an advanced
  2625. record does not become a regvar, because it's a vs_var
  2626. parameter }
  2627. if paramanager.push_addr_param(para.parasym.varspez,para.parasym.vardef,
  2628. procdefinition.proccalloption) then
  2629. make_not_regable(para.left,[ra_addr_regable]);
  2630. end
  2631. else
  2632. if vo_is_vmt in para.parasym.varoptions then
  2633. begin
  2634. para.left:=gen_vmt_tree;
  2635. end
  2636. else
  2637. if vo_is_syscall_lib in para.parasym.varoptions then
  2638. gen_syscall_para(para)
  2639. else
  2640. if vo_is_parentfp in para.parasym.varoptions then
  2641. begin
  2642. if not assigned(right) then
  2643. begin
  2644. if assigned(procdefinition.owner.defowner) then
  2645. para.left:=cloadparentfpnode.create(tprocdef(procdefinition.owner.defowner),lpf_forpara)
  2646. { exceptfilters called from main level are not owned }
  2647. else if procdefinition.proctypeoption=potype_exceptfilter then
  2648. para.left:=cloadparentfpnode.create(current_procinfo.procdef,lpf_forpara)
  2649. else
  2650. internalerror(200309287);
  2651. end
  2652. else if not(po_is_block in procdefinition.procoptions) then
  2653. para.left:=gen_procvar_context_tree_parentfp
  2654. else
  2655. para.left:=gen_block_context
  2656. end
  2657. else
  2658. if vo_is_range_check in para.parasym.varoptions then
  2659. begin
  2660. para.left:=cordconstnode.create(Ord(cs_check_range in current_settings.localswitches),pasbool8type,false);
  2661. end
  2662. else
  2663. if vo_is_overflow_check in para.parasym.varoptions then
  2664. begin
  2665. para.left:=cordconstnode.create(Ord(cs_check_overflow in current_settings.localswitches),pasbool8type,false);
  2666. end
  2667. else
  2668. if vo_is_msgsel in para.parasym.varoptions then
  2669. begin
  2670. para.left:=cobjcselectornode.create(cstringconstnode.createstr(tprocdef(procdefinition).messageinf.str^));
  2671. end;
  2672. end;
  2673. if not assigned(para.left) then
  2674. internalerror(200709084);
  2675. para:=tcallparanode(para.right);
  2676. end;
  2677. end;
  2678. procedure tcallnode.verifyabstract(sym:TObject;arg:pointer);
  2679. var
  2680. pd : tprocdef;
  2681. i : longint;
  2682. j : integer;
  2683. hs : string;
  2684. begin
  2685. if (tsym(sym).typ<>procsym) then
  2686. exit;
  2687. for i:=0 to tprocsym(sym).ProcdefList.Count-1 do
  2688. begin
  2689. pd:=tprocdef(tprocsym(sym).ProcdefList[i]);
  2690. hs:=pd.procsym.name+pd.typename_paras([]);
  2691. j:=AbstractMethodsList.FindIndexOf(hs);
  2692. if j<>-1 then
  2693. AbstractMethodsList[j]:=pd
  2694. else
  2695. AbstractMethodsList.Add(hs,pd);
  2696. end;
  2697. end;
  2698. procedure tcallnode.verifyabstractcalls;
  2699. var
  2700. objectdf : tobjectdef;
  2701. parents : tlinkedlist;
  2702. objectinfo : tobjectinfoitem;
  2703. pd : tprocdef;
  2704. i : integer;
  2705. begin
  2706. objectdf := nil;
  2707. { verify if trying to create an instance of a class which contains
  2708. non-implemented abstract methods }
  2709. { first verify this class type, no class than exit }
  2710. { also, this checking can only be done if the constructor is directly
  2711. called, indirect constructor calls cannot be checked.
  2712. }
  2713. if assigned(methodpointer) and
  2714. not((methodpointer.nodetype=loadn) and
  2715. (loadnf_is_self in tloadnode(methodpointer).loadnodeflags)) then
  2716. begin
  2717. if (methodpointer.resultdef.typ = objectdef) then
  2718. objectdf:=tobjectdef(methodpointer.resultdef)
  2719. else
  2720. if (methodpointer.resultdef.typ = classrefdef) and
  2721. (tclassrefdef(methodpointer.resultdef).pointeddef.typ = objectdef) and
  2722. (methodpointer.nodetype in [typen,loadvmtaddrn]) then
  2723. objectdf:=tobjectdef(tclassrefdef(methodpointer.resultdef).pointeddef);
  2724. end;
  2725. if not assigned(objectdf) then
  2726. exit;
  2727. { quick exit if nothing to check }
  2728. if objectdf.abstractcnt = 0 then
  2729. exit;
  2730. parents := tlinkedlist.create;
  2731. AbstractMethodsList := TFPHashList.create;
  2732. { insert all parents in this class : the first item in the
  2733. list will be the base parent of the class .
  2734. }
  2735. while assigned(objectdf) do
  2736. begin
  2737. objectinfo:=tobjectinfoitem.create(objectdf);
  2738. parents.insert(objectinfo);
  2739. objectdf := objectdf.childof;
  2740. end;
  2741. { now all parents are in the correct order
  2742. insert all abstract methods in the list, and remove
  2743. those which are overridden by parent classes.
  2744. }
  2745. objectinfo:=tobjectinfoitem(parents.first);
  2746. while assigned(objectinfo) do
  2747. begin
  2748. objectdf := objectinfo.objinfo;
  2749. if assigned(objectdf.symtable) then
  2750. objectdf.symtable.SymList.ForEachCall(@verifyabstract,nil);
  2751. objectinfo:=tobjectinfoitem(objectinfo.next);
  2752. end;
  2753. if assigned(parents) then
  2754. parents.free;
  2755. { Finally give out a warning for each abstract method still in the list }
  2756. for i:=0 to AbstractMethodsList.Count-1 do
  2757. begin
  2758. pd:=tprocdef(AbstractMethodsList[i]);
  2759. if po_abstractmethod in pd.procoptions then
  2760. begin
  2761. Message2(type_w_instance_with_abstract,objectdf.objrealname^,pd.procsym.RealName);
  2762. MessagePos1(pd.fileinfo,sym_h_abstract_method_list,pd.fullprocname(true));
  2763. end;
  2764. end;
  2765. if assigned(AbstractMethodsList) then
  2766. AbstractMethodsList.Free;
  2767. end;
  2768. procedure tcallnode.convert_carg_array_of_const;
  2769. var
  2770. hp : tarrayconstructornode;
  2771. oldleft : tcallparanode;
  2772. begin
  2773. oldleft:=tcallparanode(left);
  2774. if oldleft.left.nodetype<>arrayconstructorn then
  2775. begin
  2776. CGMessage1(type_e_wrong_type_in_array_constructor,oldleft.left.resultdef.typename);
  2777. exit;
  2778. end;
  2779. include(callnodeflags,cnf_uses_varargs);
  2780. { Get arrayconstructor node and insert typeconvs }
  2781. hp:=tarrayconstructornode(oldleft.left);
  2782. { Add c args parameters }
  2783. { It could be an empty set }
  2784. if assigned(hp) and
  2785. assigned(hp.left) then
  2786. begin
  2787. while assigned(hp) do
  2788. begin
  2789. left:=ccallparanode.create(hp.left,left);
  2790. { set callparanode resultdef and flags }
  2791. left.resultdef:=hp.left.resultdef;
  2792. include(tcallparanode(left).callparaflags,cpf_varargs_para);
  2793. hp.left:=nil;
  2794. hp:=tarrayconstructornode(hp.right);
  2795. end;
  2796. end;
  2797. { Remove value of old array of const parameter, but keep it
  2798. in the list because it is required for bind_parasym.
  2799. Generate a nothign to keep callparanoed.left valid }
  2800. oldleft.left.free;
  2801. oldleft.left:=cnothingnode.create;
  2802. end;
  2803. procedure tcallnode.bind_parasym;
  2804. type
  2805. pcallparanode = ^tcallparanode;
  2806. var
  2807. i : integer;
  2808. pt : tcallparanode;
  2809. oldppt : pcallparanode;
  2810. varargspara,
  2811. currpara : tparavarsym;
  2812. hiddentree : tnode;
  2813. paradef : tdef;
  2814. begin
  2815. pt:=tcallparanode(left);
  2816. oldppt:=pcallparanode(@left);
  2817. { flag all callparanodes that belong to the varargs }
  2818. i:=paralength;
  2819. while (i>procdefinition.maxparacount) do
  2820. begin
  2821. include(pt.callparaflags,cpf_varargs_para);
  2822. oldppt:=pcallparanode(@pt.right);
  2823. pt:=tcallparanode(pt.right);
  2824. dec(i);
  2825. end;
  2826. { skip varargs that are inserted by array of const }
  2827. while assigned(pt) and
  2828. (cpf_varargs_para in pt.callparaflags) do
  2829. pt:=tcallparanode(pt.right);
  2830. { process normal parameters and insert hidden parameter nodes, the content
  2831. of the hidden parameters will be updated in pass1 }
  2832. for i:=procdefinition.paras.count-1 downto 0 do
  2833. begin
  2834. currpara:=tparavarsym(procdefinition.paras[i]);
  2835. if vo_is_hidden_para in currpara.varoptions then
  2836. begin
  2837. { Here we handle only the parameters that depend on
  2838. the types of the previous parameter. The typeconversion
  2839. can change the type in the next step. For example passing
  2840. an array can be change to a pointer and a deref }
  2841. if vo_is_high_para in currpara.varoptions then
  2842. begin
  2843. if not assigned(pt) or (i=0) then
  2844. internalerror(200304081);
  2845. { we need the information of the previous parameter }
  2846. paradef:=tparavarsym(procdefinition.paras[i-1]).vardef;
  2847. hiddentree:=gen_high_tree(pt.left,paradef);
  2848. { for open array of managed type, a copy of high parameter is
  2849. necessary to properly initialize before the call }
  2850. if is_open_array(paradef) and
  2851. (tparavarsym(procdefinition.paras[i-1]).varspez=vs_out) and
  2852. is_managed_type(tarraydef(paradef).elementdef) then
  2853. begin
  2854. typecheckpass(hiddentree);
  2855. {this eliminates double call to fpc_dynarray_high, if any}
  2856. maybe_load_in_temp(hiddentree);
  2857. oldppt^.third:=hiddentree.getcopy;
  2858. end;
  2859. end
  2860. else
  2861. if vo_is_typinfo_para in currpara.varoptions then
  2862. begin
  2863. if not assigned(pt) or (i=0) then
  2864. internalerror(200304082);
  2865. hiddentree:=caddrnode.create_internal(
  2866. crttinode.create(Tstoreddef(pt.resultdef),fullrtti,rdt_normal)
  2867. );
  2868. end
  2869. else
  2870. hiddentree:=cnothingnode.create;
  2871. pt:=ccallparanode.create(hiddentree,oldppt^);
  2872. oldppt^:=pt;
  2873. end;
  2874. if not assigned(pt) then
  2875. internalerror(200310052);
  2876. pt.parasym:=currpara;
  2877. oldppt:=pcallparanode(@pt.right);
  2878. pt:=tcallparanode(pt.right);
  2879. end;
  2880. { Create parasyms for varargs, first count the number of varargs paras,
  2881. then insert the parameters with numbering in reverse order. The SortParas
  2882. will set the correct order at the end}
  2883. pt:=tcallparanode(left);
  2884. i:=0;
  2885. while assigned(pt) do
  2886. begin
  2887. if cpf_varargs_para in pt.callparaflags then
  2888. inc(i);
  2889. pt:=tcallparanode(pt.right);
  2890. end;
  2891. if (i>0) then
  2892. begin
  2893. include(current_procinfo.flags,pi_calls_c_varargs);
  2894. varargsparas:=tvarargsparalist.create;
  2895. pt:=tcallparanode(left);
  2896. while assigned(pt) do
  2897. begin
  2898. if cpf_varargs_para in pt.callparaflags then
  2899. begin
  2900. varargspara:=cparavarsym.create('va'+tostr(i),i,vs_value,pt.resultdef,[]);
  2901. dec(i);
  2902. { varargspara is left-right, use insert
  2903. instead of concat }
  2904. varargsparas.add(varargspara);
  2905. pt.parasym:=varargspara;
  2906. end;
  2907. pt:=tcallparanode(pt.right);
  2908. end;
  2909. varargsparas.sortparas;
  2910. end;
  2911. end;
  2912. function tcallnode.pass_typecheck:tnode;
  2913. var
  2914. candidates : tcallcandidates;
  2915. oldcallnode : tcallnode;
  2916. hpt : tnode;
  2917. pt : tcallparanode;
  2918. lastpara : longint;
  2919. paraidx,
  2920. cand_cnt : integer;
  2921. i : longint;
  2922. ignorevisibility,
  2923. is_const : boolean;
  2924. statements : tstatementnode;
  2925. converted_result_data : ttempcreatenode;
  2926. calltype: tdispcalltype;
  2927. begin
  2928. result:=nil;
  2929. candidates:=nil;
  2930. oldcallnode:=aktcallnode;
  2931. aktcallnode:=self;
  2932. try
  2933. { determine length of parameter list }
  2934. pt:=tcallparanode(left);
  2935. paralength:=0;
  2936. while assigned(pt) do
  2937. begin
  2938. inc(paralength);
  2939. pt:=tcallparanode(pt.right);
  2940. end;
  2941. { determine the type of the parameters }
  2942. if assigned(left) then
  2943. begin
  2944. tcallparanode(left).get_paratype;
  2945. if codegenerror then
  2946. exit;
  2947. end;
  2948. if assigned(methodpointer) then
  2949. typecheckpass(methodpointer);
  2950. { procedure variable ? }
  2951. if assigned(right) then
  2952. begin
  2953. set_varstate(right,vs_read,[vsf_must_be_valid]);
  2954. typecheckpass(right);
  2955. if codegenerror then
  2956. exit;
  2957. procdefinition:=tabstractprocdef(right.resultdef);
  2958. { Compare parameters from right to left }
  2959. paraidx:=procdefinition.Paras.count-1;
  2960. { Skip default parameters }
  2961. if not(po_varargs in procdefinition.procoptions) then
  2962. begin
  2963. { ignore hidden parameters }
  2964. while (paraidx>=0) and (vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) do
  2965. dec(paraidx);
  2966. for i:=1 to procdefinition.maxparacount-paralength do
  2967. begin
  2968. if paraidx<0 then
  2969. internalerror(200402265);
  2970. if not assigned(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym) then
  2971. begin
  2972. CGMessage1(parser_e_wrong_parameter_size,'<Procedure Variable>');
  2973. exit;
  2974. end;
  2975. dec(paraidx);
  2976. end;
  2977. end;
  2978. while (paraidx>=0) and (vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) do
  2979. dec(paraidx);
  2980. pt:=tcallparanode(left);
  2981. lastpara:=paralength;
  2982. while (paraidx>=0) and assigned(pt) do
  2983. begin
  2984. { only goto next para if we're out of the varargs }
  2985. if not(po_varargs in procdefinition.procoptions) or
  2986. (lastpara<=procdefinition.maxparacount) then
  2987. begin
  2988. repeat
  2989. dec(paraidx);
  2990. until (paraidx<0) or not(vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions);
  2991. end;
  2992. pt:=tcallparanode(pt.right);
  2993. dec(lastpara);
  2994. end;
  2995. if assigned(pt) or
  2996. ((paraidx>=0) and
  2997. not assigned(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym)) then
  2998. begin
  2999. if assigned(pt) then
  3000. current_filepos:=pt.fileinfo;
  3001. CGMessage1(parser_e_wrong_parameter_size,'<Procedure Variable>');
  3002. exit;
  3003. end;
  3004. end
  3005. else
  3006. { not a procedure variable }
  3007. begin
  3008. { do we know the procedure to call ? }
  3009. if not(assigned(procdefinition)) then
  3010. begin
  3011. { ignore possible private for properties or in delphi mode for anon. inherited (FK) }
  3012. ignorevisibility:=(nf_isproperty in flags) or
  3013. ((m_delphi in current_settings.modeswitches) and (cnf_anon_inherited in callnodeflags));
  3014. candidates:=tcallcandidates.create(symtableprocentry,symtableproc,left,ignorevisibility,
  3015. not(nf_isproperty in flags),cnf_objc_id_call in callnodeflags,cnf_unit_specified in callnodeflags,
  3016. callnodeflags*[cnf_anon_inherited,cnf_inherited]=[],cnf_anon_inherited in callnodeflags);
  3017. { no procedures found? then there is something wrong
  3018. with the parameter size or the procedures are
  3019. not accessible }
  3020. if candidates.count=0 then
  3021. begin
  3022. { when it's an auto inherited call and there
  3023. is no procedure found, but the procedures
  3024. were defined with overload directive and at
  3025. least two procedures are defined then we ignore
  3026. this inherited by inserting a nothingn. Only
  3027. do this ugly hack in Delphi mode as it looks more
  3028. like a bug. It's also not documented }
  3029. if (m_delphi in current_settings.modeswitches) and
  3030. (cnf_anon_inherited in callnodeflags) and
  3031. (symtableprocentry.owner.symtabletype=ObjectSymtable) and
  3032. (po_overload in tprocdef(symtableprocentry.ProcdefList[0]).procoptions) and
  3033. (symtableprocentry.ProcdefList.Count>=2) then
  3034. result:=cnothingnode.create
  3035. else
  3036. begin
  3037. { in tp mode we can try to convert to procvar if
  3038. there are no parameters specified }
  3039. if not(assigned(left)) and
  3040. not(cnf_inherited in callnodeflags) and
  3041. ((m_tp_procvar in current_settings.modeswitches) or
  3042. (m_mac_procvar in current_settings.modeswitches)) and
  3043. (not assigned(methodpointer) or
  3044. (methodpointer.nodetype <> typen)) then
  3045. begin
  3046. hpt:=cloadnode.create(tprocsym(symtableprocentry),symtableproc);
  3047. if assigned(methodpointer) then
  3048. tloadnode(hpt).set_mp(methodpointer.getcopy);
  3049. typecheckpass(hpt);
  3050. result:=hpt;
  3051. end
  3052. else
  3053. begin
  3054. CGMessagePos1(fileinfo,parser_e_wrong_parameter_size,symtableprocentry.realname);
  3055. symtableprocentry.write_parameter_lists(nil);
  3056. end;
  3057. end;
  3058. candidates.free;
  3059. exit;
  3060. end;
  3061. { Retrieve information about the candidates }
  3062. candidates.get_information;
  3063. {$ifdef EXTDEBUG}
  3064. { Display info when multiple candidates are found }
  3065. if candidates.count>1 then
  3066. candidates.dump_info(V_Debug);
  3067. {$endif EXTDEBUG}
  3068. { Choose the best candidate and count the number of
  3069. candidates left }
  3070. cand_cnt:=candidates.choose_best(procdefinition,
  3071. assigned(left) and
  3072. not assigned(tcallparanode(left).right) and
  3073. (tcallparanode(left).left.resultdef.typ=variantdef));
  3074. { All parameters are checked, check if there are any
  3075. procedures left }
  3076. if cand_cnt>0 then
  3077. begin
  3078. { Multiple candidates left? }
  3079. if cand_cnt>1 then
  3080. begin
  3081. CGMessage(type_e_cant_choose_overload_function);
  3082. {$ifdef EXTDEBUG}
  3083. candidates.dump_info(V_Hint);
  3084. {$else EXTDEBUG}
  3085. candidates.list(false);
  3086. {$endif EXTDEBUG}
  3087. { we'll just use the first candidate to make the
  3088. call }
  3089. end;
  3090. { assign procdefinition }
  3091. if symtableproc=nil then
  3092. symtableproc:=procdefinition.owner;
  3093. end
  3094. else
  3095. begin
  3096. { No candidates left, this must be a type error,
  3097. because wrong size is already checked. procdefinition
  3098. is filled with the first (random) definition that is
  3099. found. We use this definition to display a nice error
  3100. message that the wrong type is passed }
  3101. candidates.find_wrong_para;
  3102. candidates.list(true);
  3103. {$ifdef EXTDEBUG}
  3104. candidates.dump_info(V_Hint);
  3105. {$endif EXTDEBUG}
  3106. { We can not proceed, release all procs and exit }
  3107. candidates.free;
  3108. exit;
  3109. end;
  3110. candidates.free;
  3111. end; { end of procedure to call determination }
  3112. end;
  3113. { check for hints (deprecated etc) }
  3114. if procdefinition.typ = procdef then
  3115. check_hints(tprocdef(procdefinition).procsym,tprocdef(procdefinition).symoptions,tprocdef(procdefinition).deprecatedmsg);
  3116. { add reference to corresponding procsym; may not be the one
  3117. originally found/passed to the constructor because of overloads }
  3118. if procdefinition.typ = procdef then
  3119. addsymref(tprocdef(procdefinition).procsym);
  3120. { add needed default parameters }
  3121. if (paralength<procdefinition.maxparacount) then
  3122. begin
  3123. paraidx:=0;
  3124. i:=0;
  3125. while (i<paralength) do
  3126. begin
  3127. if paraidx>=procdefinition.Paras.count then
  3128. internalerror(200306181);
  3129. if not(vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) then
  3130. inc(i);
  3131. inc(paraidx);
  3132. end;
  3133. while (paraidx<procdefinition.paras.count) and (vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) do
  3134. inc(paraidx);
  3135. while (paraidx<procdefinition.paras.count) do
  3136. begin
  3137. if not assigned(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym) then
  3138. internalerror(200212142);
  3139. left:=ccallparanode.create(genconstsymtree(
  3140. tconstsym(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym)),left);
  3141. { Ignore vs_hidden parameters }
  3142. repeat
  3143. inc(paraidx);
  3144. until (paraidx>=procdefinition.paras.count) or
  3145. not(vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions);
  3146. end;
  3147. end;
  3148. { recursive call? }
  3149. if assigned(current_procinfo) and
  3150. (procdefinition=current_procinfo.procdef) then
  3151. include(current_procinfo.flags,pi_is_recursive);
  3152. { handle predefined procedures }
  3153. is_const:=(po_internconst in procdefinition.procoptions) and
  3154. ((block_type in [bt_const,bt_type,bt_const_type,bt_var_type]) or
  3155. (assigned(left) and ((tcallparanode(left).left.nodetype in [realconstn,ordconstn])
  3156. and (not assigned(tcallparanode(left).right) or (tcallparanode(left).right.nodetype in [realconstn,ordconstn])))));
  3157. if (procdefinition.proccalloption=pocall_internproc) or is_const then
  3158. begin
  3159. if assigned(left) then
  3160. begin
  3161. { convert types to those of the prototype, this is required by functions like ror, rol, sar
  3162. some use however a dummy type (Typedfile) so this would break them }
  3163. if not(tprocdef(procdefinition).extnumber in [fpc_in_Reset_TypedFile,fpc_in_Rewrite_TypedFile]) then
  3164. begin
  3165. { bind parasyms to the callparanodes and insert hidden parameters }
  3166. bind_parasym;
  3167. { insert type conversions for parameters }
  3168. if assigned(left) then
  3169. tcallparanode(left).insert_typeconv;
  3170. end;
  3171. { ptr and settextbuf need two args }
  3172. if assigned(tcallparanode(left).right) then
  3173. begin
  3174. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,left);
  3175. left:=nil;
  3176. end
  3177. else
  3178. begin
  3179. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,tcallparanode(left).left);
  3180. tcallparanode(left).left:=nil;
  3181. end;
  3182. end
  3183. else
  3184. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,nil);
  3185. result:=hpt;
  3186. exit;
  3187. end;
  3188. { ensure that the result type is set }
  3189. if not(cnf_typedefset in callnodeflags) then
  3190. begin
  3191. { constructors return their current class type, not the type where the
  3192. constructor is declared, this can be different because of inheritance }
  3193. if (procdefinition.proctypeoption=potype_constructor) and
  3194. assigned(methodpointer) and
  3195. assigned(methodpointer.resultdef) and
  3196. (methodpointer.resultdef.typ=classrefdef) then
  3197. resultdef:=tclassrefdef(methodpointer.resultdef).pointeddef
  3198. else
  3199. { Member call to a (inherited) constructor from the class, the return
  3200. value is always self, so we change it to voidtype to generate an
  3201. error and to prevent users from generating non-working code
  3202. when they expect to clone the current instance, see bug 3662 (PFV) }
  3203. if (procdefinition.proctypeoption=potype_constructor) and
  3204. is_class(tprocdef(procdefinition).struct) and
  3205. assigned(methodpointer) and
  3206. (methodpointer.nodetype=loadn) and
  3207. (loadnf_is_self in tloadnode(methodpointer).loadnodeflags) then
  3208. resultdef:=voidtype
  3209. else
  3210. resultdef:=procdefinition.returndef;
  3211. end
  3212. else
  3213. resultdef:=typedef;
  3214. { Check object/class for methods }
  3215. if assigned(methodpointer) then
  3216. begin
  3217. { direct call to inherited abstract method, then we
  3218. can already give a error in the compiler instead
  3219. of a runtime error }
  3220. if (cnf_inherited in callnodeflags) and
  3221. (po_abstractmethod in procdefinition.procoptions) then
  3222. begin
  3223. if (m_delphi in current_settings.modeswitches) and
  3224. (cnf_anon_inherited in callnodeflags) then
  3225. begin
  3226. CGMessage(cg_h_inherited_ignored);
  3227. result:=cnothingnode.create;
  3228. exit;
  3229. end
  3230. else
  3231. CGMessage(cg_e_cant_call_abstract_method);
  3232. end;
  3233. { directly calling an interface/protocol/category/class helper
  3234. method via its type is not possible (always must be called via
  3235. the actual instance) }
  3236. if (methodpointer.nodetype=typen) and
  3237. (is_interface(methodpointer.resultdef) or
  3238. is_objc_protocol_or_category(methodpointer.resultdef)) then
  3239. CGMessage1(type_e_class_type_expected,methodpointer.resultdef.typename);
  3240. { if an inherited con- or destructor should be }
  3241. { called in a con- or destructor then a warning }
  3242. { will be made }
  3243. { con- and destructors need a pointer to the vmt }
  3244. if (cnf_inherited in callnodeflags) and
  3245. (procdefinition.proctypeoption in [potype_constructor,potype_destructor]) and
  3246. is_object(methodpointer.resultdef) and
  3247. not(current_procinfo.procdef.proctypeoption in [potype_constructor,potype_destructor]) then
  3248. CGMessage(cg_w_member_cd_call_from_method);
  3249. if methodpointer.nodetype<>typen then
  3250. begin
  3251. { Remove all postfix operators }
  3252. hpt:=methodpointer;
  3253. while assigned(hpt) and (hpt.nodetype in [subscriptn,vecn]) do
  3254. hpt:=tunarynode(hpt).left;
  3255. if ((hpt.nodetype=loadvmtaddrn) or
  3256. ((hpt.nodetype=loadn) and assigned(tloadnode(hpt).resultdef) and (tloadnode(hpt).resultdef.typ=classrefdef))) and
  3257. not (procdefinition.proctypeoption=potype_constructor) and
  3258. not (po_classmethod in procdefinition.procoptions) and
  3259. not (po_staticmethod in procdefinition.procoptions) then
  3260. { error: we are calling instance method from the class method/static method }
  3261. CGMessage(parser_e_only_class_members);
  3262. if (procdefinition.proctypeoption=potype_constructor) and
  3263. assigned(symtableproc) and
  3264. (symtableproc.symtabletype=withsymtable) and
  3265. (tnode(twithsymtable(symtableproc).withrefnode).nodetype=temprefn) then
  3266. CGmessage(cg_e_cannot_call_cons_dest_inside_with);
  3267. { skip (absolute and other simple) type conversions -- only now,
  3268. because the checks above have to take type conversions into
  3269. e.g. class reference types account }
  3270. hpt:=actualtargetnode(@hpt)^;
  3271. { R.Init then R will be initialized by the constructor,
  3272. Also allow it for simple loads }
  3273. if (procdefinition.proctypeoption=potype_constructor) or
  3274. ((hpt.nodetype=loadn) and
  3275. (((methodpointer.resultdef.typ=objectdef) and
  3276. not(oo_has_virtual in tobjectdef(methodpointer.resultdef).objectoptions)) or
  3277. (methodpointer.resultdef.typ=recorddef)
  3278. )
  3279. ) then
  3280. { a constructor will and a method may write something to }
  3281. { the fields }
  3282. set_varstate(methodpointer,vs_readwritten,[])
  3283. else
  3284. set_varstate(methodpointer,vs_read,[vsf_must_be_valid]);
  3285. end;
  3286. { if we are calling the constructor check for abstract
  3287. methods. Ignore inherited and member calls, because the
  3288. class is then already created }
  3289. if (procdefinition.proctypeoption=potype_constructor) and
  3290. not(cnf_inherited in callnodeflags) and
  3291. not(cnf_member_call in callnodeflags) then
  3292. verifyabstractcalls;
  3293. end
  3294. else
  3295. begin
  3296. { When this is method the methodpointer must be available }
  3297. if (right=nil) and
  3298. (procdefinition.owner.symtabletype in [ObjectSymtable,recordsymtable]) and
  3299. not procdefinition.no_self_node then
  3300. internalerror(200305061);
  3301. end;
  3302. { Set flag that the procedure uses varargs, also if they are not passed it is still
  3303. needed for x86_64 to pass the number of SSE registers used }
  3304. if po_varargs in procdefinition.procoptions then
  3305. include(callnodeflags,cnf_uses_varargs);
  3306. { set the appropriate node flag if the call never returns }
  3307. if po_noreturn in procdefinition.procoptions then
  3308. include(callnodeflags,cnf_call_never_returns);
  3309. { Change loading of array of const to varargs }
  3310. if assigned(left) and
  3311. is_array_of_const(tparavarsym(procdefinition.paras[procdefinition.paras.count-1]).vardef) and
  3312. (procdefinition.proccalloption in cdecl_pocalls) then
  3313. convert_carg_array_of_const;
  3314. { bind parasyms to the callparanodes and insert hidden parameters }
  3315. bind_parasym;
  3316. { insert type conversions for parameters }
  3317. if assigned(left) then
  3318. tcallparanode(left).insert_typeconv;
  3319. { dispinterface methode invoke? }
  3320. if assigned(methodpointer) and is_dispinterface(methodpointer.resultdef) then
  3321. begin
  3322. case procdefinition.proctypeoption of
  3323. potype_propgetter: calltype:=dct_propget;
  3324. potype_propsetter: calltype:=dct_propput;
  3325. else
  3326. calltype:=dct_method;
  3327. end;
  3328. { if the result is used, we've to insert a call to convert the type to be on the "safe side" }
  3329. if (cnf_return_value_used in callnodeflags) and not is_void(procdefinition.returndef) then
  3330. begin
  3331. result:=internalstatements(statements);
  3332. converted_result_data:=ctempcreatenode.create(procdefinition.returndef,sizeof(procdefinition.returndef),
  3333. tt_persistent,true);
  3334. addstatement(statements,converted_result_data);
  3335. addstatement(statements,cassignmentnode.create(ctemprefnode.create(converted_result_data),
  3336. ctypeconvnode.create_internal(
  3337. translate_disp_call(methodpointer,parameters,calltype,'',tprocdef(procdefinition).dispid,procdefinition.returndef),
  3338. procdefinition.returndef)));
  3339. addstatement(statements,ctempdeletenode.create_normal_temp(converted_result_data));
  3340. addstatement(statements,ctemprefnode.create(converted_result_data));
  3341. end
  3342. else
  3343. result:=translate_disp_call(methodpointer,parameters,calltype,'',tprocdef(procdefinition).dispid,voidtype);
  3344. { don't free reused nodes }
  3345. methodpointer:=nil;
  3346. parameters:=nil;
  3347. end;
  3348. if assigned(call_self_node) then
  3349. typecheckpass(call_self_node);
  3350. if assigned(call_vmt_node) then
  3351. typecheckpass(call_vmt_node);
  3352. finally
  3353. aktcallnode:=oldcallnode;
  3354. end;
  3355. end;
  3356. procedure tcallnode.order_parameters;
  3357. var
  3358. hp,hpcurr,hpnext,hpfirst,hpprev : tcallparanode;
  3359. currloc : tcgloc;
  3360. begin
  3361. hpfirst:=nil;
  3362. hpcurr:=tcallparanode(left);
  3363. { cache all info about parameters containing stack tainting calls,
  3364. since we will need it a lot below and calculting it can be expensive }
  3365. while assigned(hpcurr) do
  3366. begin
  3367. hpcurr.init_contains_stack_tainting_call_cache;
  3368. hpcurr:=tcallparanode(hpcurr.right);
  3369. end;
  3370. hpcurr:=tcallparanode(left);
  3371. while assigned(hpcurr) do
  3372. begin
  3373. { pull out }
  3374. hpnext:=tcallparanode(hpcurr.right);
  3375. { pull in at the correct place.
  3376. Used order:
  3377. 1. LOC_REFERENCE with smallest offset (i386 only)
  3378. 2. LOC_REFERENCE with least complexity (non-i386 only)
  3379. 3. LOC_REFERENCE with most complexity (non-i386 only)
  3380. 4. LOC_REGISTER with most complexity
  3381. 5. LOC_REGISTER with least complexity
  3382. For the moment we only look at the first parameter field. Combining it
  3383. with multiple parameter fields will make things a lot complexer (PFV)
  3384. The reason for the difference regarding complexity ordering
  3385. between LOC_REFERENCE and LOC_REGISTER is mainly for calls:
  3386. we first want to treat the LOC_REFERENCE destinations whose
  3387. calculation does not require a call, because their location
  3388. may contain registers which might otherwise have to be saved
  3389. if a call has to be evaluated first. The calculated value is
  3390. stored on the stack and will thus no longer occupy any
  3391. register.
  3392. Similarly, for the register parameters we first want to
  3393. evaluate the calls, because otherwise the already loaded
  3394. register parameters will have to be saved so the intermediate
  3395. call can be evaluated (JM) }
  3396. if not assigned(hpcurr.parasym.paraloc[callerside].location) then
  3397. internalerror(200412152);
  3398. currloc:=hpcurr.parasym.paraloc[callerside].location^.loc;
  3399. hpprev:=nil;
  3400. hp:=hpfirst;
  3401. { on fixed_stack targets, always evaluate parameters containing
  3402. a call with stack parameters before all other parameters,
  3403. because they will prevent any other parameters from being put
  3404. in their final place; if both the current and the next para
  3405. contain a stack tainting call, don't do anything to prevent
  3406. them from keeping on chasing eachother's tail }
  3407. while assigned(hp) do
  3408. begin
  3409. if paramanager.use_fixed_stack and
  3410. hpcurr.contains_stack_tainting_call_cached then
  3411. break;
  3412. case currloc of
  3413. LOC_REFERENCE :
  3414. begin
  3415. case hp.parasym.paraloc[callerside].location^.loc of
  3416. LOC_REFERENCE :
  3417. begin
  3418. { Offset is calculated like:
  3419. sub esp,12
  3420. mov [esp+8],para3
  3421. mov [esp+4],para2
  3422. mov [esp],para1
  3423. call function
  3424. That means the for pushes the para with the
  3425. highest offset (see para3) needs to be pushed first
  3426. }
  3427. {$if defined(i386) or defined(i8086) or defined(m68k)}
  3428. { the i386, i8086, m68k and jvm code generators expect all reference }
  3429. { parameters to be in this order so they can use }
  3430. { pushes in case of no fixed stack }
  3431. if (not paramanager.use_fixed_stack and
  3432. (hpcurr.parasym.paraloc[callerside].location^.reference.offset>
  3433. hp.parasym.paraloc[callerside].location^.reference.offset)) or
  3434. (paramanager.use_fixed_stack and
  3435. (node_complexity(hpcurr)<node_complexity(hp))) then
  3436. {$elseif defined(jvm)}
  3437. if (hpcurr.parasym.paraloc[callerside].location^.reference.offset<hp.parasym.paraloc[callerside].location^.reference.offset) then
  3438. {$else jvm}
  3439. if (node_complexity(hpcurr)<node_complexity(hp)) then
  3440. {$endif jvm}
  3441. break;
  3442. end;
  3443. LOC_MMREGISTER,
  3444. LOC_REGISTER,
  3445. LOC_FPUREGISTER :
  3446. break;
  3447. end;
  3448. end;
  3449. LOC_MMREGISTER,
  3450. LOC_FPUREGISTER,
  3451. LOC_REGISTER :
  3452. begin
  3453. if (hp.parasym.paraloc[callerside].location^.loc<>LOC_REFERENCE) and
  3454. (node_complexity(hpcurr)>node_complexity(hp)) then
  3455. break;
  3456. end;
  3457. end;
  3458. hpprev:=hp;
  3459. hp:=tcallparanode(hp.right);
  3460. end;
  3461. hpcurr.right:=hp;
  3462. if assigned(hpprev) then
  3463. hpprev.right:=hpcurr
  3464. else
  3465. hpfirst:=hpcurr;
  3466. { next }
  3467. hpcurr:=hpnext;
  3468. end;
  3469. left:=hpfirst;
  3470. { now mark each parameter that is followed by a stack-tainting call,
  3471. to determine on use_fixed_stack targets which ones can immediately be
  3472. put in their final destination. Unforunately we can never put register
  3473. parameters immediately in their final destination (even on register-
  3474. rich architectures such as the PowerPC), because the code generator
  3475. can still insert extra calls that only make use of register
  3476. parameters (fpc_move() etc. }
  3477. hpcurr:=hpfirst;
  3478. while assigned(hpcurr) do
  3479. begin
  3480. if hpcurr.contains_stack_tainting_call_cached then
  3481. begin
  3482. { all parameters before this one are followed by a stack
  3483. tainting call }
  3484. hp:=hpfirst;
  3485. while hp<>hpcurr do
  3486. begin
  3487. hp.ffollowed_by_stack_tainting_call_cached:=true;
  3488. hp:=tcallparanode(hp.right);
  3489. end;
  3490. hpfirst:=hpcurr;
  3491. end;
  3492. hpcurr:=tcallparanode(hpcurr.right);
  3493. end;
  3494. end;
  3495. procedure tcallnode.check_stack_parameters;
  3496. var
  3497. hp : tcallparanode;
  3498. begin
  3499. hp:=tcallparanode(left);
  3500. while assigned(hp) do
  3501. begin
  3502. if assigned(hp.parasym) and
  3503. assigned(hp.parasym.paraloc[callerside].location) and
  3504. (hp.parasym.paraloc[callerside].location^.loc=LOC_REFERENCE) then
  3505. include(current_procinfo.flags,pi_has_stackparameter);
  3506. hp:=tcallparanode(hp.right);
  3507. end;
  3508. end;
  3509. procedure tcallnode.check_inlining;
  3510. var
  3511. st : tsymtable;
  3512. para : tcallparanode;
  3513. begin
  3514. { Can we inline the procedure? }
  3515. if (po_inline in procdefinition.procoptions) and
  3516. (procdefinition.typ=procdef) and
  3517. tprocdef(procdefinition).has_inlininginfo and
  3518. { Prevent too deep inlining recursion and code bloat by inlining
  3519. The actual formuala is
  3520. inlinelevel+1 /-------
  3521. node count < -------------\/ 10000
  3522. This allows exponential grow of the code only to a certain limit.
  3523. Remarks
  3524. - The current approach calculates the inlining level top down, so outer call nodes (nodes closer to the leaf) might not be inlined
  3525. if the max. complexity is reached. This is done because it makes the implementation easier and because
  3526. there might be situations were it is more beneficial to inline inner nodes and do the calls to the outer nodes
  3527. if the outer nodes are in a seldomly used code path
  3528. - The code avoids to use functions from the math unit
  3529. }
  3530. (node_count(tprocdef(procdefinition).inlininginfo^.code)<round(exp((1.0/(inlinelevel+1))*ln(10000)))) then
  3531. begin
  3532. include(callnodeflags,cnf_do_inline);
  3533. { Check if we can inline the procedure when it references proc/var that
  3534. are not in the globally available }
  3535. st:=procdefinition.owner;
  3536. while (st.symtabletype in [ObjectSymtable,recordsymtable]) do
  3537. st:=st.defowner.owner;
  3538. if (pi_uses_static_symtable in tprocdef(procdefinition).inlininginfo^.flags) and
  3539. (st.symtabletype=globalsymtable) and
  3540. (not st.iscurrentunit) then
  3541. begin
  3542. Comment(V_lineinfo+V_Debug,'Not inlining "'+tprocdef(procdefinition).procsym.realname+'", references static symtable');
  3543. exclude(callnodeflags,cnf_do_inline);
  3544. end;
  3545. para:=tcallparanode(parameters);
  3546. while assigned(para) do
  3547. begin
  3548. if not para.can_be_inlined then
  3549. begin
  3550. Comment(V_lineinfo+V_Debug,'Not inlining "'+tprocdef(procdefinition).procsym.realname+
  3551. '", invocation parameter contains an unsafe/unsupported construct');
  3552. exclude(callnodeflags,cnf_do_inline);
  3553. break;
  3554. end;
  3555. para:=tcallparanode(para.nextpara);
  3556. end;
  3557. end;
  3558. end;
  3559. function tcallnode.pass_1 : tnode;
  3560. procedure mark_unregable_parameters;
  3561. var
  3562. hp : tcallparanode;
  3563. begin
  3564. hp:=tcallparanode(left);
  3565. while assigned(hp) do
  3566. begin
  3567. do_typecheckpass(hp.left);
  3568. { When the address needs to be pushed then the register is
  3569. not regable. Exception is when the location is also a var
  3570. parameter and we can pass the address transparently (but
  3571. that is handled by make_not_regable if ra_addr_regable is
  3572. passed, and make_not_regable always needs to called for
  3573. the ra_addr_taken info for non-invisble parameters) }
  3574. if (not (cpf_varargs_para in hp.callparaflags)) and (
  3575. not(
  3576. (vo_is_hidden_para in hp.parasym.varoptions) and
  3577. (hp.left.resultdef.typ in [pointerdef,classrefdef])
  3578. ) and
  3579. paramanager.push_addr_param(hp.parasym.varspez,hp.parasym.vardef,
  3580. self.procdefinition.proccalloption)
  3581. ) then
  3582. { pushing the address of a variable to take the place of a temp
  3583. as the complex function result of a function does not make its
  3584. address escape the current block, as the "address of the
  3585. function result" is not something which can be stored
  3586. persistently by the callee (it becomes invalid when the callee
  3587. returns) }
  3588. if not(vo_is_funcret in hp.parasym.varoptions) then
  3589. make_not_regable(hp.left,[ra_addr_regable,ra_addr_taken])
  3590. else
  3591. make_not_regable(hp.left,[ra_addr_regable]);
  3592. hp:=tcallparanode(hp.right);
  3593. end;
  3594. end;
  3595. var
  3596. para: tcallparanode;
  3597. oldcallnode: tcallnode;
  3598. begin
  3599. result:=nil;
  3600. oldcallnode:=aktcallnode;
  3601. aktcallnode:=self;
  3602. try
  3603. { as pass_1 is never called on the methodpointer node, we must check
  3604. here that it's not a helper type }
  3605. if assigned(methodpointer) and
  3606. (methodpointer.nodetype=typen) and
  3607. is_objectpascal_helper(ttypenode(methodpointer).typedef) and
  3608. not ttypenode(methodpointer).helperallowed then
  3609. begin
  3610. CGMessage(parser_e_no_category_as_types);
  3611. { we get an internal error when trying to insert the hidden
  3612. parameters in this case }
  3613. exit;
  3614. end;
  3615. { can we get rid of the call? }
  3616. if (cs_opt_remove_emtpy_proc in current_settings.optimizerswitches) and
  3617. not(cnf_return_value_used in callnodeflags) and
  3618. (procdefinition.typ=procdef) and
  3619. tprocdef(procdefinition).isempty and
  3620. { allow only certain proc options }
  3621. ((tprocdef(procdefinition).procoptions-[po_none,po_classmethod,po_staticmethod,
  3622. po_interrupt,po_iocheck,po_assembler,po_msgstr,po_msgint,po_exports,po_external,po_overload,
  3623. po_nostackframe,po_has_mangledname,po_has_public_name,po_forward,po_global,
  3624. po_inline,po_compilerproc,po_has_importdll,po_has_importname,po_kylixlocal,po_dispid,po_delphi_nested_cc,
  3625. po_rtlproc,po_ignore_for_overload_resolution,po_auto_raised_visibility])=[]) then
  3626. begin
  3627. { check parameters for side effects }
  3628. para:=tcallparanode(left);
  3629. while assigned(para) do
  3630. begin
  3631. if (para.parasym.typ = paravarsym) and
  3632. ((para.parasym.refs>0) or
  3633. { array of consts are converted later on so we need to skip them here
  3634. else no error detection is done }
  3635. is_array_of_const(para.parasym.vardef) or
  3636. not(cs_opt_dead_values in current_settings.optimizerswitches) or
  3637. might_have_sideeffects(para.left)) then
  3638. break;
  3639. para:=tcallparanode(para.right);
  3640. end;
  3641. { finally, remove it if no parameter with side effect has been found }
  3642. if para=nil then
  3643. begin
  3644. result:=cnothingnode.create;
  3645. exit;
  3646. end;
  3647. end;
  3648. { convert Objective-C calls into a message call }
  3649. if (procdefinition.typ=procdef) and
  3650. (po_objc in tprocdef(procdefinition).procoptions) then
  3651. begin
  3652. if not(cnf_objc_processed in callnodeflags) then
  3653. objc_convert_to_message_send;
  3654. end
  3655. else
  3656. begin
  3657. { The following don't apply to obj-c: obj-c methods can never be
  3658. inlined because they're always virtual and the destination can
  3659. change at run, and for the same reason we also can't perform
  3660. WPO on them (+ they have no constructors) }
  3661. { Check if the call can be inlined, sets the cnf_do_inline flag }
  3662. check_inlining;
  3663. { must be called before maybe_load_in_temp(methodpointer), because
  3664. it converts the methodpointer into a temp in case it's a call
  3665. (and we want to know the original call)
  3666. }
  3667. register_created_object_types;
  3668. end;
  3669. { Maybe optimize the loading of the methodpointer using a temp. When the methodpointer
  3670. is a calln this is even required to not execute the calln twice.
  3671. This needs to be done after the resulttype pass, because in the resulttype we can still convert the
  3672. calln to a loadn (PFV) }
  3673. if assigned(methodpointer) then
  3674. maybe_load_in_temp(methodpointer);
  3675. { Create destination (temp or assignment-variable reuse) for function result if it not yet set }
  3676. maybe_create_funcret_node;
  3677. { Insert the self,vmt,function result in the parameters }
  3678. gen_hidden_parameters;
  3679. { Remove useless nodes from init/final blocks }
  3680. { (simplify depends on typecheck info) }
  3681. if assigned(callinitblock) then
  3682. begin
  3683. typecheckpass(tnode(callinitblock));
  3684. doinlinesimplify(tnode(callinitblock));
  3685. end;
  3686. if assigned(callcleanupblock) then
  3687. begin
  3688. typecheckpass(tnode(callcleanupblock));
  3689. doinlinesimplify(tnode(callcleanupblock));
  3690. end;
  3691. { If a constructor calls another constructor of the same or of an
  3692. inherited class, some targets (jvm) have to generate different
  3693. entry code for the constructor. }
  3694. if (current_procinfo.procdef.proctypeoption=potype_constructor) and
  3695. (procdefinition.typ=procdef) and
  3696. (tprocdef(procdefinition).proctypeoption=potype_constructor) and
  3697. ([cnf_member_call,cnf_inherited] * callnodeflags <> []) then
  3698. current_procinfo.ConstructorCallingConstructor:=true;
  3699. { object check helper will load VMT -> needs GOT }
  3700. if (cs_check_object in current_settings.localswitches) and
  3701. (cs_create_pic in current_settings.moduleswitches) then
  3702. include(current_procinfo.flags,pi_needs_got);
  3703. { Continue with checking a normal call or generate the inlined code }
  3704. if cnf_do_inline in callnodeflags then
  3705. result:=pass1_inline
  3706. else
  3707. begin
  3708. mark_unregable_parameters;
  3709. result:=pass1_normal;
  3710. end;
  3711. finally
  3712. aktcallnode:=oldcallnode;
  3713. end;
  3714. end;
  3715. function tcallnode.pass1_normal : tnode;
  3716. begin
  3717. result:=nil;
  3718. { calculate the parameter info for the procdef }
  3719. procdefinition.init_paraloc_info(callerside);
  3720. { calculate the parameter size needed for this call include varargs if they are available }
  3721. if assigned(varargsparas) then
  3722. pushedparasize:=paramanager.create_varargs_paraloc_info(procdefinition,varargsparas)
  3723. else
  3724. pushedparasize:=procdefinition.callerargareasize;
  3725. { record maximum parameter size used in this proc }
  3726. current_procinfo.allocate_push_parasize(pushedparasize);
  3727. { check for stacked parameters }
  3728. if assigned(left) and
  3729. (current_settings.optimizerswitches*[cs_opt_stackframe,cs_opt_level1]<>[]) then
  3730. check_stack_parameters;
  3731. if assigned(callinitblock) then
  3732. firstpass(tnode(callinitblock));
  3733. { function result node (tempref or simple load) }
  3734. if assigned(funcretnode) then
  3735. firstpass(funcretnode);
  3736. { parameters }
  3737. if assigned(left) then
  3738. tcallparanode(left).firstcallparan;
  3739. { procedure variable ? }
  3740. if assigned(right) then
  3741. firstpass(right);
  3742. if assigned(methodpointer) and
  3743. (methodpointer.nodetype<>typen) then
  3744. firstpass(methodpointer);
  3745. if assigned(callcleanupblock) then
  3746. firstpass(tnode(callcleanupblock));
  3747. if not (block_type in [bt_const,bt_type,bt_const_type,bt_var_type]) then
  3748. include(current_procinfo.flags,pi_do_call);
  3749. { order parameters }
  3750. order_parameters;
  3751. { get a register for the return value }
  3752. if (not is_void(resultdef)) then
  3753. begin
  3754. if paramanager.ret_in_param(resultdef,procdefinition) then
  3755. begin
  3756. expectloc:=LOC_REFERENCE;
  3757. end
  3758. else
  3759. { ansi/widestrings must be registered, so we can dispose them }
  3760. if is_ansistring(resultdef) or
  3761. is_widestring(resultdef) or
  3762. is_unicodestring(resultdef) then
  3763. begin
  3764. expectloc:=LOC_REFERENCE;
  3765. end
  3766. else
  3767. { we have only to handle the result if it is used }
  3768. if (cnf_return_value_used in callnodeflags) then
  3769. expectloc:=get_expect_loc
  3770. else
  3771. expectloc:=LOC_VOID;
  3772. end
  3773. else
  3774. expectloc:=LOC_VOID;
  3775. { create tree for VMT entry if required }
  3776. gen_vmt_entry_load;
  3777. end;
  3778. {$ifdef state_tracking}
  3779. function Tcallnode.track_state_pass(exec_known:boolean):boolean;
  3780. var hp:Tcallparanode;
  3781. value:Tnode;
  3782. begin
  3783. track_state_pass:=false;
  3784. hp:=Tcallparanode(left);
  3785. while assigned(hp) do
  3786. begin
  3787. if left.track_state_pass(exec_known) then
  3788. begin
  3789. left.resultdef:=nil;
  3790. do_typecheckpass(left);
  3791. end;
  3792. value:=aktstate.find_fact(hp.left);
  3793. if value<>nil then
  3794. begin
  3795. track_state_pass:=true;
  3796. hp.left.destroy;
  3797. hp.left:=value.getcopy;
  3798. do_typecheckpass(hp.left);
  3799. end;
  3800. hp:=Tcallparanode(hp.right);
  3801. end;
  3802. end;
  3803. {$endif}
  3804. {**************************************************************************
  3805. INLINING SUPPORT
  3806. **************************************************************************}
  3807. function tcallnode.replaceparaload(var n: tnode; arg: pointer): foreachnoderesult;
  3808. var
  3809. paras: tcallparanode;
  3810. temp: tnode;
  3811. indexnr : integer;
  3812. begin
  3813. result := fen_false;
  3814. n.fileinfo := pfileposinfo(arg)^;
  3815. if (n.nodetype = loadn) then
  3816. begin
  3817. case tloadnode(n).symtableentry.typ of
  3818. paravarsym :
  3819. begin
  3820. paras := tcallparanode(left);
  3821. while assigned(paras) and
  3822. (paras.parasym <> tloadnode(n).symtableentry) do
  3823. paras := tcallparanode(paras.right);
  3824. if assigned(paras) then
  3825. begin
  3826. temp:=paras.left.getcopy;
  3827. { inherit modification information, this is needed by the dfa/cse }
  3828. temp.flags:=temp.flags+(n.flags*[nf_modify,nf_write,nf_address_taken]);
  3829. n.free;
  3830. n:=temp;
  3831. typecheckpass(n);
  3832. result := fen_true;
  3833. end;
  3834. end;
  3835. localvarsym :
  3836. begin
  3837. { local? }
  3838. if (tloadnode(n).symtableentry.owner <> tprocdef(procdefinition).localst) then
  3839. exit;
  3840. indexnr:=tloadnode(n).symtableentry.owner.SymList.IndexOf(tloadnode(n).symtableentry);
  3841. if (indexnr >= inlinelocals.count) or
  3842. not assigned(inlinelocals[indexnr]) then
  3843. internalerror(20040720);
  3844. temp := tnode(inlinelocals[indexnr]).getcopy;
  3845. { inherit modification information, this is needed by the dfa/cse }
  3846. temp.flags:=temp.flags+(n.flags*[nf_modify,nf_write,nf_address_taken]);
  3847. n.free;
  3848. n:=temp;
  3849. typecheckpass(n);
  3850. result := fen_true;
  3851. end;
  3852. end;
  3853. end;
  3854. end;
  3855. procedure tcallnode.createlocaltemps(p:TObject;arg:pointer);
  3856. var
  3857. tempnode: ttempcreatenode;
  3858. indexnr : integer;
  3859. begin
  3860. if (TSym(p).typ <> localvarsym) then
  3861. exit;
  3862. indexnr:=TSym(p).Owner.SymList.IndexOf(p);
  3863. if (indexnr >= inlinelocals.count) then
  3864. inlinelocals.count:=indexnr+10;
  3865. if (vo_is_funcret in tabstractvarsym(p).varoptions) then
  3866. begin
  3867. if not assigned(funcretnode) then
  3868. internalerror(200709081);
  3869. inlinelocals[indexnr] := funcretnode.getcopy
  3870. end
  3871. else
  3872. begin
  3873. tempnode :=ctempcreatenode.create(tabstractvarsym(p).vardef,
  3874. tabstractvarsym(p).vardef.size,tt_persistent,tabstractvarsym(p).is_regvar(false));
  3875. addstatement(inlineinitstatement,tempnode);
  3876. if localvartrashing <> -1 then
  3877. cnodeutils.maybe_trash_variable(inlineinitstatement,tabstractnormalvarsym(p),ctemprefnode.create(tempnode));
  3878. addstatement(inlinecleanupstatement,ctempdeletenode.create(tempnode));
  3879. { inherit addr_taken flag }
  3880. if (tabstractvarsym(p).addr_taken) then
  3881. include(tempnode.tempinfo^.flags,ti_addr_taken);
  3882. inlinelocals[indexnr] := ctemprefnode.create(tempnode);
  3883. end;
  3884. end;
  3885. function nonlocalvars(var n: tnode; arg: pointer): foreachnoderesult;
  3886. begin
  3887. result := fen_false;
  3888. { this is just to play it safe, there are more safe situations }
  3889. if (n.nodetype = derefn) or
  3890. ((n.nodetype = loadn) and
  3891. { globals and fields of (possibly global) objects could always be changed in the callee }
  3892. ((tloadnode(n).symtable.symtabletype in [globalsymtable,ObjectSymtable]) or
  3893. { statics can only be modified by functions in the same unit }
  3894. ((tloadnode(n).symtable.symtabletype = staticsymtable) and
  3895. (tloadnode(n).symtable = TSymtable(arg))) or
  3896. { if the addr of the symbol is taken somewhere, it can be also non-local }
  3897. (tabstractvarsym(tloadnode(n).symtableentry).addr_taken)
  3898. )) or
  3899. ((n.nodetype = subscriptn) and
  3900. (tsubscriptnode(n).vs.owner.symtabletype = ObjectSymtable)) then
  3901. result := fen_norecurse_true;
  3902. end;
  3903. procedure tcallnode.createinlineparas;
  3904. var
  3905. para: tcallparanode;
  3906. tempnode: ttempcreatenode;
  3907. n: tnode;
  3908. paracomplexity: longint;
  3909. pushconstaddr: boolean;
  3910. trytotakeaddress : Boolean;
  3911. begin
  3912. { parameters }
  3913. para := tcallparanode(left);
  3914. pushconstaddr := false;
  3915. while assigned(para) do
  3916. begin
  3917. if (para.parasym.typ = paravarsym) and
  3918. ((para.parasym.refs>0) or
  3919. not(cs_opt_dead_values in current_settings.optimizerswitches) or
  3920. might_have_sideeffects(para.left)) then
  3921. begin
  3922. { must take copy of para.left, because if it contains a }
  3923. { temprefn pointing to a copied temp (e.g. methodpointer), }
  3924. { then this parameter must be changed to point to the copy of }
  3925. { that temp (JM) }
  3926. n := para.left.getcopy;
  3927. para.left.free;
  3928. para.left := n;
  3929. firstpass(para.left);
  3930. { determine how a parameter is passed to the inlined body
  3931. There are three options:
  3932. - insert the node tree of the callparanode directly
  3933. If a parameter is used only once, this is the best option if we can do so
  3934. - get the address of the argument, store it in a temp and insert a dereference to this temp
  3935. If the node tree cannot be inserted directly, taking the address of the argument and using it
  3936. is the second best option, but even this is not always possible
  3937. - assign the value of the argument to a newly created temp
  3938. This is the fall back which works always
  3939. Notes:
  3940. - we need to take care that we use the type of the defined parameter and not of the
  3941. passed parameter, because these can be different in case of a formaldef (PFV)
  3942. }
  3943. { pre-compute some values }
  3944. paracomplexity:=node_complexity(para.left);
  3945. if para.parasym.varspez=vs_const then
  3946. pushconstaddr:=paramanager.push_addr_param(vs_const,para.parasym.vardef,procdefinition.proccalloption);
  3947. { if the parameter is "complex", try to take the address
  3948. of the parameter expression, store it in a temp and replace
  3949. occurrences of the parameter with dereferencings of this
  3950. temp
  3951. }
  3952. trytotakeaddress:=
  3953. { don't create a temp. for function results }
  3954. not(nf_is_funcret in para.left.flags) and
  3955. { this makes only sense if the parameter is reasonable complex else inserting directly is a better solution }
  3956. ((paracomplexity>2) or
  3957. { don't create a temp. for the often seen case that p^ is passed to a var parameter }
  3958. ((paracomplexity>1) and not((para.left.nodetype=derefn) and (para.parasym.varspez = vs_var))));
  3959. { check if we have to create a temp, assign the parameter's
  3960. contents to that temp and then substitute the parameter
  3961. with the temp everywhere in the function }
  3962. if
  3963. ((tparavarsym(para.parasym).varregable in [vr_none,vr_addr]) and
  3964. not(para.left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE])) or
  3965. { we can't assign to formaldef temps }
  3966. ((para.parasym.vardef.typ<>formaldef) and
  3967. (
  3968. { can we take the address of the argument? }
  3969. (trytotakeaddress and not(para.left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE])) or
  3970. (trytotakeaddress and
  3971. (not valid_for_addr(para.left,false) or
  3972. (para.left.nodetype = calln) or
  3973. is_constnode(para.left))) or
  3974. { we do not need to create a temp for value parameters }
  3975. { which are not modified in the inlined function }
  3976. { const parameters can get vs_readwritten if their }
  3977. { address is taken }
  3978. ((((para.parasym.varspez = vs_value) and
  3979. (para.parasym.varstate in [vs_initialised,vs_declared,vs_read])) or
  3980. { in case of const, this is only necessary if the
  3981. variable would be passed by value normally and if it is modified or if
  3982. there is such a variable somewhere in an expression }
  3983. ((para.parasym.varspez = vs_const) and
  3984. (not pushconstaddr))) and
  3985. { however, if we pass a global variable, an object field or}
  3986. { an expression containing a pointer dereference as }
  3987. { parameter, this value could be modified in other ways as }
  3988. { well and in such cases create a temp to be on the safe }
  3989. { side }
  3990. foreachnodestatic(para.left,@nonlocalvars,pointer(symtableproc))) or
  3991. { value parameters of which we know they are modified by }
  3992. { definition have to be copied to a temp }
  3993. { the same goes for cases of "x:=f(x)" where x is passed }
  3994. { as value parameter to f(), at least if we optimized }
  3995. { invocation by setting the funcretnode to x to avoid }
  3996. { assignment afterwards (since x may be read inside the }
  3997. { function after it modified result==x) }
  3998. ((para.parasym.varspez = vs_value) and
  3999. (not(para.parasym.varstate in [vs_initialised,vs_declared,vs_read]) or
  4000. (assigned(aktassignmentnode) and
  4001. (aktassignmentnode.right=self) and
  4002. (nf_assign_done_in_right in aktassignmentnode.flags) and
  4003. actualtargetnode(@aktassignmentnode.left)^.isequal(actualtargetnode(@para.left)^)))) or
  4004. { the compiler expects that it can take the address of parameters passed by reference in
  4005. the case of const so we can't replace the node simply by a constant node
  4006. When playing with this code, ensure that
  4007. function f(const a,b : longint) : longint;inline;
  4008. begin
  4009. result:=a*b;
  4010. end;
  4011. [...]
  4012. ...:=f(10,20));
  4013. [...]
  4014. is still folded. (FK)
  4015. }
  4016. ((para.parasym.varspez = vs_const) and
  4017. { const para's can get vs_readwritten if their address }
  4018. { is taken -> in case they are not passed by reference, }
  4019. { to keep the same behaviour as without inlining we have }
  4020. { to make a copy in case the originally passed parameter }
  4021. { value gets changed inside the callee }
  4022. ((not pushconstaddr and
  4023. (para.parasym.varstate = vs_readwritten)
  4024. ) or
  4025. { call-by-reference const's may need to be passed by }
  4026. { reference to function called in the inlined code }
  4027. (pushconstaddr and
  4028. not valid_for_addr(para.left,false))
  4029. )
  4030. )
  4031. )
  4032. ) then
  4033. begin
  4034. { don't create a new temp unnecessarily, but make sure we
  4035. do create a new one if the old one could be a regvar and
  4036. the new one cannot be one }
  4037. if not(tparavarsym(para.parasym).varspez in [vs_out,vs_var]) and (((para.left.nodetype<>temprefn) or
  4038. (((tparavarsym(para.parasym).varregable in [vr_none,vr_addr])) and
  4039. (ti_may_be_in_reg in ttemprefnode(para.left).tempinfo^.flags)))) then
  4040. begin
  4041. tempnode := ctempcreatenode.create(para.parasym.vardef,para.parasym.vardef.size,
  4042. tt_persistent,tparavarsym(para.parasym).is_regvar(false));
  4043. addstatement(inlineinitstatement,tempnode);
  4044. if localvartrashing <> -1 then
  4045. cnodeutils.maybe_trash_variable(inlineinitstatement,para.parasym,ctemprefnode.create(tempnode));
  4046. addstatement(inlinecleanupstatement,ctempdeletenode.create(tempnode));
  4047. addstatement(inlineinitstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  4048. para.left));
  4049. para.left := ctemprefnode.create(tempnode);
  4050. { inherit addr_taken flag }
  4051. if (tabstractvarsym(para.parasym).addr_taken) then
  4052. include(tempnode.tempinfo^.flags,ti_addr_taken);
  4053. end;
  4054. end
  4055. else if trytotakeaddress then
  4056. wrapcomplexinlinepara(para);
  4057. end;
  4058. para := tcallparanode(para.right);
  4059. end;
  4060. { local variables }
  4061. if not assigned(tprocdef(procdefinition).localst) or
  4062. (tprocdef(procdefinition).localst.SymList.count = 0) then
  4063. exit;
  4064. inlinelocals.count:=tprocdef(procdefinition).localst.SymList.count;
  4065. tprocdef(procdefinition).localst.SymList.ForEachCall(@createlocaltemps,nil);
  4066. end;
  4067. procedure tcallnode.wrapcomplexinlinepara(para: tcallparanode);
  4068. var
  4069. ptrtype: tdef;
  4070. tempnode: ttempcreatenode;
  4071. paraaddr: taddrnode;
  4072. begin
  4073. ptrtype:=getpointerdef(para.left.resultdef);
  4074. tempnode:=ctempcreatenode.create(ptrtype,ptrtype.size,tt_persistent,true);
  4075. addstatement(inlineinitstatement,tempnode);
  4076. addstatement(inlinecleanupstatement,ctempdeletenode.create(tempnode));
  4077. { inherit addr_taken flag }
  4078. if (tabstractvarsym(para.parasym).addr_taken) then
  4079. include(tempnode.tempinfo^.flags,ti_addr_taken);
  4080. { inherit read only }
  4081. if tabstractvarsym(para.parasym).varspez=vs_const then
  4082. include(tempnode.tempinfo^.flags,ti_const);
  4083. paraaddr:=caddrnode.create_internal(para.left);
  4084. include(paraaddr.flags,nf_typedaddr);
  4085. addstatement(inlineinitstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  4086. paraaddr));
  4087. para.left:=cderefnode.create(ctemprefnode.create(tempnode));
  4088. end;
  4089. function tcallnode.optimize_funcret_assignment(inlineblock: tblocknode): tnode;
  4090. var
  4091. hp : tstatementnode;
  4092. hp2 : tnode;
  4093. resassign : tassignmentnode;
  4094. begin
  4095. result:=nil;
  4096. if not assigned(funcretnode) or
  4097. not(cnf_return_value_used in callnodeflags) then
  4098. exit;
  4099. { tempcreatenode for the function result }
  4100. hp:=tstatementnode(inlineblock.left);
  4101. if not(assigned(hp)) or
  4102. (hp.left.nodetype <> tempcreaten) or
  4103. not(nf_is_funcret in hp.left.flags) then
  4104. exit;
  4105. { constant assignment? right must be a constant (mainly to avoid trying
  4106. to reuse local temps which may already be freed afterwards once these
  4107. checks are made looser) }
  4108. hp:=tstatementnode(hp.right);
  4109. if not(assigned(hp)) or
  4110. (hp.left.nodetype<>assignn) or
  4111. not is_constnode(tassignmentnode(hp.left).right) then
  4112. exit;
  4113. { left must be function result }
  4114. resassign:=tassignmentnode(hp.left);
  4115. hp2:=resassign.left;
  4116. { can have extra type conversion due to absolute mapping
  4117. of <fucntionname> on function result var }
  4118. if (hp2.nodetype=typeconvn) and (ttypeconvnode(hp2).convtype=tc_equal) then
  4119. hp2:=ttypeconvnode(hp2).left;
  4120. if (hp2.nodetype<>temprefn) or
  4121. not(nf_is_funcret in hp2.flags) then
  4122. exit;
  4123. { tempdelete to normal of the function result }
  4124. hp:=tstatementnode(hp.right);
  4125. if not(assigned(hp)) or
  4126. (hp.left.nodetype <> tempdeleten) then
  4127. exit;
  4128. { the function result once more }
  4129. hp:=tstatementnode(hp.right);
  4130. if not(assigned(hp)) or
  4131. (hp.left.nodetype<>temprefn) or
  4132. not(nf_is_funcret in hp.left.flags) then
  4133. exit;
  4134. { should be the end }
  4135. if assigned(hp.right) then
  4136. exit;
  4137. { we made it! }
  4138. result:=tassignmentnode(resassign).right.getcopy;
  4139. firstpass(result);
  4140. end;
  4141. { this procedure removes the user code flag because it prevents optimizations }
  4142. function removeusercodeflag(var n : tnode; arg : pointer) : foreachnoderesult;
  4143. begin
  4144. result:=fen_false;
  4145. if nf_usercode_entry in n.flags then
  4146. begin
  4147. exclude(n.flags,nf_usercode_entry);
  4148. result:=fen_norecurse_true;
  4149. end;
  4150. end;
  4151. function tcallnode.pass1_inline:tnode;
  4152. var
  4153. n,
  4154. body : tnode;
  4155. para : tcallparanode;
  4156. inlineblock,
  4157. inlinecleanupblock : tblocknode;
  4158. begin
  4159. inc(inlinelevel);
  4160. result:=nil;
  4161. if not(assigned(tprocdef(procdefinition).inlininginfo) and
  4162. assigned(tprocdef(procdefinition).inlininginfo^.code)) then
  4163. internalerror(200412021);
  4164. inlinelocals:=TFPObjectList.create(true);
  4165. { inherit flags }
  4166. current_procinfo.flags:=current_procinfo.flags+
  4167. ((procdefinition as tprocdef).inlininginfo^.flags*inherited_inlining_flags);
  4168. { Create new code block for inlining }
  4169. inlineblock:=internalstatements(inlineinitstatement);
  4170. { make sure that valid_for_assign() returns false for this block
  4171. (otherwise assigning values to the block will result in assigning
  4172. values to the inlined function's result) }
  4173. include(inlineblock.flags,nf_no_lvalue);
  4174. inlinecleanupblock:=internalstatements(inlinecleanupstatement);
  4175. if assigned(callinitblock) then
  4176. addstatement(inlineinitstatement,callinitblock.getcopy);
  4177. { replace complex parameters with temps }
  4178. createinlineparas;
  4179. { create a copy of the body and replace parameter loads with the parameter values }
  4180. body:=tprocdef(procdefinition).inlininginfo^.code.getcopy;
  4181. foreachnodestatic(pm_postprocess,body,@removeusercodeflag,nil);
  4182. foreachnode(pm_preprocess,body,@replaceparaload,@fileinfo);
  4183. { Concat the body and finalization parts }
  4184. addstatement(inlineinitstatement,body);
  4185. addstatement(inlineinitstatement,inlinecleanupblock);
  4186. inlinecleanupblock:=nil;
  4187. if assigned(callcleanupblock) then
  4188. addstatement(inlineinitstatement,callcleanupblock.getcopy);
  4189. { the last statement of the new inline block must return the
  4190. location and type of the function result.
  4191. This is not needed when the result is not used, also the tempnode is then
  4192. already destroyed by a tempdelete in the callcleanupblock tree }
  4193. if not is_void(resultdef) and
  4194. (cnf_return_value_used in callnodeflags) then
  4195. begin
  4196. if assigned(funcretnode) then
  4197. addstatement(inlineinitstatement,funcretnode.getcopy)
  4198. else
  4199. begin
  4200. para:=tcallparanode(left);
  4201. while assigned(para) do
  4202. begin
  4203. if (vo_is_hidden_para in para.parasym.varoptions) and
  4204. (vo_is_funcret in para.parasym.varoptions) then
  4205. begin
  4206. addstatement(inlineinitstatement,para.left.getcopy);
  4207. break;
  4208. end;
  4209. para:=tcallparanode(para.right);
  4210. end;
  4211. end;
  4212. end;
  4213. { consider it must not be inlined if called
  4214. again inside the args or itself }
  4215. exclude(procdefinition.procoptions,po_inline);
  4216. typecheckpass(tnode(inlineblock));
  4217. doinlinesimplify(tnode(inlineblock));
  4218. firstpass(tnode(inlineblock));
  4219. include(procdefinition.procoptions,po_inline);
  4220. result:=inlineblock;
  4221. { if the function result is used then verify that the blocknode
  4222. returns the same result type as the original callnode }
  4223. if (cnf_return_value_used in callnodeflags) and
  4224. (result.resultdef<>resultdef) then
  4225. internalerror(200709171);
  4226. { free the temps for the locals }
  4227. inlinelocals.free;
  4228. inlinelocals:=nil;
  4229. inlineinitstatement:=nil;
  4230. inlinecleanupstatement:=nil;
  4231. { if all that's left of the inlined function is an constant assignment
  4232. to the result, replace the whole block with the constant only }
  4233. n:=optimize_funcret_assignment(inlineblock);
  4234. if assigned(n) then
  4235. begin
  4236. inlineblock.free;
  4237. result:=n;
  4238. end;
  4239. {$ifdef DEBUGINLINE}
  4240. writeln;
  4241. writeln('**************************',tprocdef(procdefinition).mangledname);
  4242. printnode(output,result);
  4243. {$endif DEBUGINLINE}
  4244. dec(inlinelevel);
  4245. end;
  4246. end.