ncal.pas 210 KB

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