ncal.pas 203 KB

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