ncal.pas 204 KB

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