ncal.pas 210 KB

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