ncal.pas 212 KB

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