ncal.pas 205 KB

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