ncal.pas 205 KB

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