2
0

ncal.pas 214 KB

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