ncal.pas 200 KB

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