ncal.pas 206 KB

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