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. );
  50. tcallnodeflags = set of tcallnodeflag;
  51. tcallparanode = class;
  52. tcallnode = class(tbinarynode)
  53. private
  54. { number of parameters passed from the source, this does not include the hidden parameters }
  55. paralength : smallint;
  56. function getforcedprocname: TSymStr;
  57. function is_simple_para_load(p:tnode; may_be_in_reg: boolean):boolean;
  58. procedure maybe_load_in_temp(var p:tnode);
  59. function gen_high_tree(var p:tnode;paradef:tdef):tnode;
  60. function gen_procvar_context_tree_self:tnode;
  61. function gen_procvar_context_tree_parentfp:tnode;
  62. function gen_self_tree:tnode;
  63. function use_caller_self(check_for_callee_self: boolean): boolean;
  64. procedure maybe_gen_call_self_node;
  65. function gen_vmt_tree:tnode;
  66. function gen_block_context:tnode;
  67. procedure gen_hidden_parameters;
  68. function funcret_can_be_reused:boolean;
  69. procedure maybe_create_funcret_node;
  70. procedure bind_parasym;
  71. procedure add_init_statement(n:tnode);
  72. procedure add_done_statement(n:tnode);
  73. procedure convert_carg_array_of_const;
  74. procedure order_parameters;
  75. procedure check_inlining;
  76. function pass1_normal:tnode;
  77. procedure register_created_object_types;
  78. function get_expect_loc: tcgloc;
  79. protected
  80. function safe_call_self_node: tnode;
  81. procedure gen_vmt_entry_load; virtual;
  82. procedure gen_syscall_para(para: tcallparanode); virtual;
  83. procedure objc_convert_to_message_send;virtual;
  84. protected
  85. { inlining support }
  86. inlinelocals : TFPObjectList;
  87. inlineinitstatement,
  88. inlinecleanupstatement : tstatementnode;
  89. { checks whether we have to create a temp to store the value of a
  90. parameter passed to an inline routine to preserve correctness.
  91. On exit, complexpara contains true if the parameter is a complex
  92. expression and for which we can try to create a temp (even though
  93. it's not strictly necessary) for speed and code size reasons.
  94. Returns true if the temp creation has been handled, false otherwise
  95. }
  96. function maybecreateinlineparatemp(para: tcallparanode; out complexpara: boolean): boolean;
  97. procedure createinlineparas;
  98. procedure wrapcomplexinlinepara(para: tcallparanode); virtual;
  99. function replaceparaload(var n: tnode; arg: pointer): foreachnoderesult;
  100. procedure createlocaltemps(p:TObject;arg:pointer);
  101. function optimize_funcret_assignment(inlineblock: tblocknode): tnode;
  102. function pass1_inline:tnode;
  103. protected
  104. pushedparasize : longint;
  105. { Objective-C support: force the call node to call the routine with
  106. this name rather than the name of symtableprocentry (don't store
  107. to ppu, is set while processing the node). Also used on the JVM
  108. target for calling virtual methods, as this is name-based and not
  109. based on VMT entry locations }
  110. {$ifdef symansistr}
  111. fforcedprocname: TSymStr;
  112. {$else symansistr}
  113. fforcedprocname: pshortstring;
  114. {$endif symansistr}
  115. property forcedprocname: TSymStr read getforcedprocname;
  116. public
  117. { the symbol containing the definition of the procedure }
  118. { to call }
  119. symtableprocentry : tprocsym;
  120. symtableprocentryderef : tderef;
  121. { symtable where the entry was found, needed for with support }
  122. symtableproc : TSymtable;
  123. { the definition of the procedure to call }
  124. procdefinition : tabstractprocdef;
  125. procdefinitionderef : tderef;
  126. { tree that contains the pointer to the object for this method }
  127. methodpointer : tnode;
  128. { tree representing the VMT entry to call (if any) }
  129. vmt_entry : tnode;
  130. { tree that contains the self/vmt parameter when this node was created
  131. (so it's still valid when this node is processed in an inline
  132. context)
  133. }
  134. call_self_node,
  135. call_vmt_node: tnode;
  136. { initialize/finalization of temps }
  137. callinitblock,
  138. callcleanupblock : tblocknode;
  139. { function return node for initialized types or supplied return variable.
  140. When the result is passed in a parameter then it is set to nil }
  141. funcretnode : tnode;
  142. { varargs parasyms }
  143. varargsparas : tvarargsparalist;
  144. { separately specified resultdef for some compilerprocs (e.g.
  145. you can't have a function with an "array of char" resultdef
  146. the RTL) (JM)
  147. }
  148. typedef: tdef;
  149. callnodeflags : tcallnodeflags;
  150. spezcontext : tspecializationcontext;
  151. { only the processor specific nodes need to override this }
  152. { constructor }
  153. constructor create(l:tnode; v : tprocsym;st : TSymtable; mp: tnode; callflags:tcallnodeflags;sc:tspecializationcontext);virtual;
  154. constructor create_procvar(l,r:tnode);
  155. constructor createintern(const name: string; params: tnode);
  156. constructor createinternfromunit(const fromunit, procname: string; params: tnode);
  157. constructor createinternres(const name: string; params: tnode; res:tdef);
  158. constructor createinternresfromunit(const fromunit, procname: string; params: tnode; res:tdef);
  159. constructor createinternreturn(const name: string; params: tnode; returnnode : tnode);
  160. constructor createinternmethod(mp: tnode; const name: string; params: tnode);
  161. constructor createinternmethodres(mp: tnode; const name: string; params: tnode; res:tdef);
  162. destructor destroy;override;
  163. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  164. procedure ppuwrite(ppufile:tcompilerppufile);override;
  165. procedure buildderefimpl;override;
  166. procedure derefimpl;override;
  167. function dogetcopy : tnode;override;
  168. { Goes through all symbols in a class and subclasses and calls
  169. verify abstract for each .
  170. }
  171. procedure verifyabstractcalls;
  172. { called for each definition in a class and verifies if a method
  173. is abstract or not, if it is abstract, give out a warning
  174. }
  175. procedure verifyabstract(sym:TObject;arg:pointer);
  176. procedure insertintolist(l : tnodelist);override;
  177. function pass_1 : tnode;override;
  178. function pass_typecheck:tnode;override;
  179. {$ifdef state_tracking}
  180. function track_state_pass(exec_known:boolean):boolean;override;
  181. {$endif state_tracking}
  182. function docompare(p: tnode): boolean; override;
  183. procedure printnodedata(var t:text);override;
  184. function para_count:longint;
  185. function required_para_count:longint;
  186. { checks if there are any parameters which end up at the stack, i.e.
  187. which have LOC_REFERENCE and set pi_has_stackparameter if this applies }
  188. procedure check_stack_parameters;
  189. { force the name of the to-be-called routine to a particular string,
  190. used for Objective-C message sending. }
  191. property parameters : tnode read left write left;
  192. property pushed_parasize: longint read pushedparasize;
  193. private
  194. AbstractMethodsList : TFPHashList;
  195. end;
  196. tcallnodeclass = class of tcallnode;
  197. tcallparaflag = (
  198. cpf_is_colon_para,
  199. cpf_varargs_para { belongs this para to varargs }
  200. );
  201. tcallparaflags = set of tcallparaflag;
  202. tcallparanode = class(ttertiarynode)
  203. private
  204. fcontains_stack_tainting_call_cached,
  205. ffollowed_by_stack_tainting_call_cached : boolean;
  206. protected
  207. { in case of copy-out parameters: initialization code, and the code to
  208. copy back the parameter value after the call (including any required
  209. finalization code }
  210. fparainit,
  211. fparacopyback: tnode;
  212. procedure handlemanagedbyrefpara(orgparadef: tdef);virtual;
  213. { on some targets, value parameters that are passed by reference must
  214. be copied to a temp location by the caller (and then a reference to
  215. this temp location must be passed) }
  216. procedure copy_value_by_ref_para;
  217. public
  218. callparaflags : tcallparaflags;
  219. parasym : tparavarsym;
  220. { only the processor specific nodes need to override this }
  221. { constructor }
  222. constructor create(expr,next : tnode);virtual;
  223. destructor destroy;override;
  224. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  225. procedure ppuwrite(ppufile:tcompilerppufile);override;
  226. procedure buildderefimpl; override;
  227. procedure derefimpl; override;
  228. function dogetcopy : tnode;override;
  229. procedure insertintolist(l : tnodelist);override;
  230. function pass_typecheck : tnode;override;
  231. function pass_1 : tnode;override;
  232. procedure get_paratype;
  233. procedure firstcallparan;
  234. procedure insert_typeconv;
  235. procedure secondcallparan;virtual;abstract;
  236. function docompare(p: tnode): boolean; override;
  237. procedure printnodetree(var t:text);override;
  238. { returns whether a parameter contains a type conversion from }
  239. { a refcounted into a non-refcounted type }
  240. function can_be_inlined: boolean;
  241. property paravalue : tnode read left write left;
  242. property nextpara : tnode read right write right;
  243. { third is reused to store the parameter name (only while parsing
  244. vardispatch calls, never in real node tree) and copy of 'high'
  245. parameter tree when the parameter is an open array of managed type }
  246. property parametername : tnode read third write third;
  247. { returns whether the evaluation of this parameter involves a
  248. stack tainting call }
  249. function contains_stack_tainting_call: boolean;
  250. { initialises the fcontains_stack_tainting_call_cached field with the
  251. result of contains_stack_tainting_call so that it can be quickly
  252. accessed via the contains_stack_tainting_call_cached property }
  253. procedure init_contains_stack_tainting_call_cache;
  254. { returns result of contains_stack_tainting_call cached during last
  255. call to init_contains_stack_tainting_call_cache }
  256. property contains_stack_tainting_call_cached: boolean read fcontains_stack_tainting_call_cached;
  257. { returns whether this parameter is followed by at least one other
  258. parameter whose evaluation involves a stack tainting parameter
  259. (result is only valid after order_parameters has been called) }
  260. property followed_by_stack_tainting_call_cached: boolean read ffollowed_by_stack_tainting_call_cached;
  261. property paracopyback: tnode read fparacopyback;
  262. end;
  263. tcallparanodeclass = class of tcallparanode;
  264. tdispcalltype = (
  265. dct_method,
  266. dct_propget,
  267. dct_propput
  268. );
  269. function reverseparameters(p: tcallparanode): tcallparanode;
  270. function translate_disp_call(selfnode,parametersnode: tnode; calltype: tdispcalltype; const methodname : ansistring;
  271. dispid : longint;resultdef : tdef) : tnode;
  272. var
  273. ccallnode : tcallnodeclass = tcallnode;
  274. ccallparanode : tcallparanodeclass = tcallparanode;
  275. { Current callnode, this is needed for having a link
  276. between the callparanodes and the callnode they belong to }
  277. aktcallnode : tcallnode;
  278. const
  279. { track current inlining depth }
  280. inlinelevel : longint = 0;
  281. implementation
  282. uses
  283. systems,
  284. verbose,globals,fmodule,
  285. aasmbase,aasmdata,
  286. symconst,defutil,defcmp,compinnr,
  287. htypechk,pass_1,
  288. ncnv,nflw,nld,ninl,nadd,ncon,nmem,nset,nobjc,
  289. pgenutil,
  290. ngenutil,objcutil,aasmcnst,
  291. procinfo,cpuinfo,
  292. wpobase;
  293. type
  294. tobjectinfoitem = class(tlinkedlistitem)
  295. objinfo : tobjectdef;
  296. constructor create(def : tobjectdef);
  297. end;
  298. {****************************************************************************
  299. HELPERS
  300. ****************************************************************************}
  301. function reverseparameters(p: tcallparanode): tcallparanode;
  302. var
  303. hp1, hp2: tcallparanode;
  304. begin
  305. hp1:=nil;
  306. while assigned(p) do
  307. begin
  308. { pull out }
  309. hp2:=p;
  310. p:=tcallparanode(p.right);
  311. { pull in }
  312. hp2.right:=hp1;
  313. hp1:=hp2;
  314. end;
  315. reverseparameters:=hp1;
  316. end;
  317. function translate_disp_call(selfnode,parametersnode: tnode; calltype: tdispcalltype; const methodname : ansistring;
  318. dispid : longint;resultdef : tdef) : tnode;
  319. const
  320. DISPATCH_METHOD = $1;
  321. DISPATCH_PROPERTYGET = $2;
  322. DISPATCH_PROPERTYPUT = $4;
  323. DISPATCH_PROPERTYPUTREF = $8;
  324. DISPATCH_CONSTRUCT = $4000;
  325. calltypes: array[tdispcalltype] of byte = (
  326. DISPATCH_METHOD, DISPATCH_PROPERTYGET, DISPATCH_PROPERTYPUT
  327. );
  328. var
  329. statements : tstatementnode;
  330. result_data,
  331. params : ttempcreatenode;
  332. paramssize : cardinal;
  333. resultvalue : tnode;
  334. para : tcallparanode;
  335. namedparacount,
  336. paracount : longint;
  337. assignmenttype,
  338. vardatadef,
  339. pvardatadef : tdef;
  340. useresult: boolean;
  341. restype: byte;
  342. selftemp: ttempcreatenode;
  343. selfpara: tnode;
  344. vardispatchparadef: trecorddef;
  345. vardispatchfield: tsym;
  346. tcb: ttai_typedconstbuilder;
  347. calldescsym: tstaticvarsym;
  348. names : ansistring;
  349. variantdispatch : boolean;
  350. function is_byref_para(out assign_type: tdef): boolean;
  351. begin
  352. result:=(assigned(para.parasym) and (para.parasym.varspez in [vs_var,vs_out,vs_constref])) or
  353. (variantdispatch and valid_for_var(para.left,false));
  354. if result or (para.left.resultdef.typ in [variantdef]) then
  355. assign_type:=voidpointertype
  356. else
  357. case para.left.resultdef.size of
  358. 1..4:
  359. assign_type:=u32inttype;
  360. 8:
  361. assign_type:=u64inttype;
  362. else
  363. internalerror(2007042801);
  364. end;
  365. end;
  366. function getvardef(sourcedef: TDef): longint;
  367. begin
  368. if is_ansistring(sourcedef) then
  369. result:=varStrArg
  370. else
  371. if is_unicodestring(sourcedef) then
  372. result:=varUStrArg
  373. else
  374. if is_interfacecom_or_dispinterface(sourcedef) then
  375. begin
  376. { distinct IDispatch and IUnknown interfaces }
  377. if def_is_related(tobjectdef(sourcedef),interface_idispatch) then
  378. result:=vardispatch
  379. else
  380. result:=varunknown;
  381. end
  382. else
  383. result:=sourcedef.getvardef;
  384. end;
  385. begin
  386. variantdispatch:=selfnode.resultdef.typ=variantdef;
  387. result:=internalstatements(statements);
  388. result_data:=nil;
  389. selftemp:=nil;
  390. selfpara:=nil;
  391. useresult := assigned(resultdef) and not is_void(resultdef);
  392. if useresult then
  393. begin
  394. { get temp for the result }
  395. result_data:=ctempcreatenode.create(colevarianttype,colevarianttype.size,tt_persistent,true);
  396. addstatement(statements,result_data);
  397. end;
  398. { first, count and check parameters }
  399. para:=tcallparanode(parametersnode);
  400. paracount:=0;
  401. namedparacount:=0;
  402. while assigned(para) do
  403. begin
  404. typecheckpass(para.left);
  405. { skip hidden dispinterface parameters like $self, $result,
  406. but count skipped variantdispatch parameters. }
  407. if (not variantdispatch) and (para.left.nodetype=nothingn) then
  408. begin
  409. para:=tcallparanode(para.nextpara);
  410. continue;
  411. end;
  412. inc(paracount);
  413. if assigned(para.parametername) then
  414. inc(namedparacount);
  415. { insert some extra casts }
  416. if para.left.nodetype=stringconstn then
  417. inserttypeconv_internal(para.left,cwidestringtype)
  418. { force automatable boolean type }
  419. else if is_boolean(para.left.resultdef) then
  420. inserttypeconv_internal(para.left,bool16type)
  421. { force automatable float type }
  422. else if is_extended(para.left.resultdef)
  423. and (current_settings.fputype<>fpu_none) then
  424. inserttypeconv_internal(para.left,s64floattype)
  425. else if is_shortstring(para.left.resultdef) then
  426. inserttypeconv_internal(para.left,cwidestringtype)
  427. { skip this check if we've already typecasted to automatable type }
  428. else if (para.left.nodetype<>nothingn) and (not is_automatable(para.left.resultdef)) then
  429. CGMessagePos1(para.left.fileinfo,type_e_not_automatable,para.left.resultdef.typename);
  430. para:=tcallparanode(para.nextpara);
  431. end;
  432. { create a temp to store parameter values }
  433. vardispatchparadef:=crecorddef.create_global_internal('',voidpointertype.size,voidpointertype.size,current_settings.alignment.maxCrecordalign);
  434. { the size will be set once the vardistpatchparadef record has been completed }
  435. params:=ctempcreatenode.create(vardispatchparadef,0,tt_persistent,false);
  436. addstatement(statements,params);
  437. tcb:=ctai_typedconstbuilder.create([tcalo_make_dead_strippable,tcalo_new_section]);
  438. tcb.begin_anonymous_record('',1,sizeof(pint),1,1);
  439. if not variantdispatch then { generate a tdispdesc record }
  440. begin
  441. { dispid }
  442. tcb.emit_ord_const(dispid,s32inttype);
  443. { restype }
  444. if useresult then
  445. restype:=getvardef(resultdef)
  446. else
  447. restype:=0;
  448. tcb.emit_ord_const(restype,u8inttype);
  449. end;
  450. tcb.emit_ord_const(calltypes[calltype],u8inttype);
  451. tcb.emit_ord_const(paracount,u8inttype);
  452. tcb.emit_ord_const(namedparacount,u8inttype);
  453. { build up parameters and description }
  454. para:=tcallparanode(parametersnode);
  455. paramssize:=0;
  456. names := #0;
  457. while assigned(para) do
  458. begin
  459. { Skipped parameters are actually (varType=varError, vError=DISP_E_PARAMNOTFOUND).
  460. Generate only varType here, the value will be added by RTL. }
  461. if para.left.nodetype=nothingn then
  462. begin
  463. if variantdispatch then
  464. tcb.emit_ord_const(varError,u8inttype);
  465. para:=tcallparanode(para.nextpara);
  466. continue;
  467. end;
  468. if assigned(para.parametername) then
  469. begin
  470. if para.parametername.nodetype=stringconstn then
  471. names:=names+tstringconstnode(para.parametername).value_str+#0
  472. else
  473. internalerror(200611041);
  474. end;
  475. restype:=getvardef(para.left.resultdef);
  476. if is_byref_para(assignmenttype) then
  477. restype:=restype or $80;
  478. { assign the argument/parameter to the temporary location }
  479. { for Variants, we always pass a pointer, RTL helpers must handle it
  480. depending on byref bit }
  481. vardispatchfield:=vardispatchparadef.add_field_by_def('',assignmenttype);
  482. if assignmenttype=voidpointertype then
  483. addstatement(statements,cassignmentnode.create(
  484. csubscriptnode.create(vardispatchfield,ctemprefnode.create(params)),
  485. ctypeconvnode.create_internal(caddrnode.create_internal(para.left),voidpointertype)))
  486. else
  487. addstatement(statements,cassignmentnode.create(
  488. csubscriptnode.create(vardispatchfield,ctemprefnode.create(params)),
  489. ctypeconvnode.create_internal(para.left,assignmenttype)));
  490. inc(paramssize,max(voidpointertype.size,assignmenttype.size));
  491. tcb.emit_ord_const(restype,u8inttype);
  492. para.left:=nil;
  493. para:=tcallparanode(para.nextpara);
  494. end;
  495. { finalize the parameter record }
  496. trecordsymtable(vardispatchparadef.symtable).addalignmentpadding;
  497. { Set final size for parameter block }
  498. params.size:=paramssize;
  499. { old argument list skeleton isn't needed anymore }
  500. parametersnode.free;
  501. pvardatadef:=tpointerdef(search_system_type('PVARDATA').typedef);
  502. if useresult then
  503. resultvalue:=caddrnode.create(ctemprefnode.create(result_data))
  504. else
  505. resultvalue:=cpointerconstnode.create(0,voidpointertype);
  506. if variantdispatch then
  507. begin
  508. tcb.emit_pchar_const(pchar(methodname),length(methodname),true);
  509. { length-1 because we added a null terminator to the string itself
  510. already }
  511. tcb.emit_pchar_const(pchar(names),length(names)-1,true);
  512. end;
  513. { may be referred from other units in case of inlining -> global
  514. -> must have unique name in entire progream }
  515. calldescsym:=cstaticvarsym.create(
  516. internaltypeprefixName[itp_vardisp_calldesc]+current_module.modulename^+'$'+tostr(current_module.localsymtable.SymList.count),
  517. vs_const,tcb.end_anonymous_record,[vo_is_public,vo_is_typed_const],
  518. false);
  519. calldescsym.varstate:=vs_initialised;
  520. current_module.localsymtable.insert(calldescsym);
  521. current_asmdata.AsmLists[al_typedconsts].concatList(
  522. tcb.get_final_asmlist(
  523. current_asmdata.DefineAsmSymbol(calldescsym.mangledname,AB_GLOBAL,AT_DATA,calldescsym.vardef),
  524. calldescsym.vardef,sec_rodata_norel,
  525. lower(calldescsym.mangledname),sizeof(pint)
  526. )
  527. );
  528. tcb.free;
  529. if variantdispatch then
  530. begin
  531. { actual call }
  532. vardatadef:=trecorddef(search_system_type('TVARDATA').typedef);
  533. { the Variant should behave similar to hidden 'self' parameter of objects/records,
  534. see issues #26773 and #27044 }
  535. if not valid_for_var(selfnode,false) then
  536. begin
  537. selftemp:=ctempcreatenode.create(selfnode.resultdef,selfnode.resultdef.size,tt_persistent,false);
  538. addstatement(statements,selftemp);
  539. addstatement(statements,cassignmentnode.create(ctemprefnode.create(selftemp),selfnode));
  540. selfpara:=ctemprefnode.create(selftemp);
  541. end
  542. else
  543. selfpara:=selfnode;
  544. addstatement(statements,ccallnode.createintern('fpc_dispinvoke_variant',
  545. { parameters are passed always reverted, i.e. the last comes first }
  546. ccallparanode.create(caddrnode.create(ctemprefnode.create(params)),
  547. ccallparanode.create(caddrnode.create(cloadnode.create(calldescsym,current_module.localsymtable)),
  548. ccallparanode.create(ctypeconvnode.create_internal(selfpara,vardatadef),
  549. ccallparanode.create(ctypeconvnode.create_internal(resultvalue,pvardatadef),nil)))))
  550. );
  551. if assigned(selftemp) then
  552. addstatement(statements,ctempdeletenode.create(selftemp));
  553. end
  554. else
  555. begin
  556. addstatement(statements,ccallnode.createintern('fpc_dispatch_by_id',
  557. { parameters are passed always reverted, i.e. the last comes first }
  558. ccallparanode.create(caddrnode.create(ctemprefnode.create(params)),
  559. ccallparanode.create(caddrnode.create(cloadnode.create(calldescsym,current_module.localsymtable)),
  560. ccallparanode.create(ctypeconvnode.create_internal(selfnode,voidpointertype),
  561. ccallparanode.create(ctypeconvnode.create_internal(resultvalue,pvardatadef),nil)))))
  562. );
  563. end;
  564. addstatement(statements,ctempdeletenode.create(params));
  565. if useresult then
  566. begin
  567. { clean up }
  568. addstatement(statements,ctempdeletenode.create_normal_temp(result_data));
  569. addstatement(statements,ctemprefnode.create(result_data));
  570. end;
  571. end;
  572. {****************************************************************************
  573. TOBJECTINFOITEM
  574. ****************************************************************************}
  575. constructor tobjectinfoitem.create(def : tobjectdef);
  576. begin
  577. inherited create;
  578. objinfo := def;
  579. end;
  580. {****************************************************************************
  581. TCALLPARANODE
  582. ****************************************************************************}
  583. procedure tcallparanode.handlemanagedbyrefpara(orgparadef: tdef);
  584. var
  585. temp: ttempcreatenode;
  586. npara: tcallparanode;
  587. paraaddrtype: tdef;
  588. begin
  589. { release memory for reference counted out parameters }
  590. if (parasym.varspez=vs_out) and
  591. is_managed_type(orgparadef) and
  592. (not is_open_array(resultdef) or
  593. is_managed_type(tarraydef(resultdef).elementdef)) and
  594. not(target_info.system in systems_garbage_collected_managed_types) then
  595. begin
  596. { after converting a parameter to an open array, its resultdef is
  597. set back to its original resultdef so we can get the value of the
  598. "high" parameter correctly, even though we already inserted a
  599. type conversion to "open array". Since here we work on this
  600. converted parameter, set it back to the type to which it was
  601. converted in order to avoid type mismatches at the LLVM level }
  602. if is_open_array(parasym.vardef) and
  603. is_dynamic_array(orgparadef) then
  604. begin
  605. left.resultdef:=resultdef;
  606. orgparadef:=resultdef;
  607. end;
  608. paraaddrtype:=cpointerdef.getreusable(orgparadef);
  609. { create temp with address of the parameter }
  610. temp:=ctempcreatenode.create(
  611. paraaddrtype,paraaddrtype.size,tt_persistent,true);
  612. { put this code in the init/done statement of the call node, because
  613. we should finalize all out parameters before other parameters
  614. are evaluated (in case e.g. a managed out parameter is also
  615. passed by value, we must not pass the pointer to the now possibly
  616. freed data as the value parameter, but the finalized/nil value }
  617. aktcallnode.add_init_statement(temp);
  618. aktcallnode.add_init_statement(
  619. cassignmentnode.create(
  620. ctemprefnode.create(temp),
  621. caddrnode.create(left)));
  622. if not is_open_array(resultdef) or
  623. not is_managed_type(tarraydef(resultdef).elementdef) then
  624. { finalize the entire parameter }
  625. aktcallnode.add_init_statement(
  626. cnodeutils.finalize_data_node(
  627. cderefnode.create(ctemprefnode.create(temp))))
  628. else
  629. begin
  630. { passing a (part of, in case of slice) dynamic array as an
  631. open array -> finalize the dynamic array contents, not the
  632. dynamic array itself }
  633. npara:=ccallparanode.create(
  634. { array length = high + 1 }
  635. caddnode.create(addn,third.getcopy,genintconstnode(1)),
  636. ccallparanode.create(caddrnode.create_internal
  637. (crttinode.create(tstoreddef(tarraydef(resultdef).elementdef),initrtti,rdt_normal)),
  638. ccallparanode.create(caddrnode.create_internal(
  639. cderefnode.create(ctemprefnode.create(temp))),nil)));
  640. aktcallnode.add_init_statement(
  641. ccallnode.createintern('fpc_finalize_array',npara));
  642. end;
  643. left:=cderefnode.create(ctemprefnode.create(temp));
  644. firstpass(left);
  645. aktcallnode.add_done_statement(ctempdeletenode.create(temp));
  646. end;
  647. end;
  648. procedure tcallparanode.copy_value_by_ref_para;
  649. var
  650. initstat,
  651. copybackstat,
  652. finistat: tstatementnode;
  653. finiblock: tblocknode;
  654. paratemp: ttempcreatenode;
  655. arraysize,
  656. arraybegin: tnode;
  657. lefttemp: ttempcreatenode;
  658. vardatatype,
  659. temparraydef: tdef;
  660. begin
  661. { this routine is for targets where by-reference value parameters need
  662. to be copied by the caller. It's basically the node-level equivalent
  663. of thlcgobj.g_copyvalueparas }
  664. { in case of an array constructor, we don't need a copy since the array
  665. constructor itself is already constructed on the fly (and hence if
  666. it's modified by the caller, that's no problem) }
  667. if not is_array_constructor(left.resultdef) then
  668. begin
  669. fparainit:=internalstatements(initstat);
  670. fparacopyback:=internalstatements(copybackstat);
  671. finiblock:=internalstatements(finistat);
  672. paratemp:=nil;
  673. { making a copy of an open array, an array of const or a dynamic
  674. array requires dynamic memory allocation since we don't know the
  675. size at compile time }
  676. if is_open_array(left.resultdef) or
  677. is_array_of_const(left.resultdef) or
  678. (is_dynamic_array(left.resultdef) and
  679. is_open_array(parasym.vardef)) then
  680. begin
  681. paratemp:=ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  682. if is_dynamic_array(left.resultdef) then
  683. begin
  684. { note that in insert_typeconv, this dynamic array was
  685. already converted into an open array (-> dereferenced)
  686. and then its resultdef was restored to the original
  687. dynamic array one -> get the address before treating it
  688. as a dynamic array here }
  689. { first restore the actual resultdef of left }
  690. temparraydef:=left.resultdef;
  691. left.resultdef:=parasym.vardef;
  692. { get its address }
  693. lefttemp:=ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  694. addstatement(initstat,lefttemp);
  695. addstatement(finistat,ctempdeletenode.create(lefttemp));
  696. addstatement(initstat,
  697. cassignmentnode.create(
  698. ctemprefnode.create(lefttemp),
  699. caddrnode.create_internal(left)
  700. )
  701. );
  702. { restore the resultdef }
  703. left.resultdef:=temparraydef;
  704. { now treat that address (correctly) as the original
  705. dynamic array to get its start and length }
  706. arraybegin:=cvecnode.create(
  707. ctypeconvnode.create_explicit(ctemprefnode.create(lefttemp),
  708. left.resultdef),
  709. genintconstnode(0)
  710. );
  711. arraysize:=caddnode.create(muln,
  712. geninlinenode(in_length_x,false,
  713. ctypeconvnode.create_explicit(ctemprefnode.create(lefttemp),
  714. left.resultdef)
  715. ),
  716. genintconstnode(tarraydef(left.resultdef).elementdef.size)
  717. );
  718. end
  719. else
  720. begin
  721. { no problem here that left is used multiple times, as
  722. sizeof() will simply evaluate to the high parameter }
  723. arraybegin:=left.getcopy;
  724. arraysize:=geninlinenode(in_sizeof_x,false,left);
  725. end;
  726. addstatement(initstat,paratemp);
  727. { paratemp:=getmem(sizeof(para)) }
  728. addstatement(initstat,
  729. cassignmentnode.create(
  730. ctemprefnode.create(paratemp),
  731. ccallnode.createintern('fpc_getmem',
  732. ccallparanode.create(
  733. arraysize.getcopy,nil
  734. )
  735. )
  736. )
  737. );
  738. { move(para,temp,sizeof(arr)) (no "left.getcopy" below because
  739. we replace left afterwards) }
  740. addstatement(initstat,
  741. cifnode.create_internal(
  742. caddnode.create_internal(
  743. unequaln,
  744. arraysize.getcopy,
  745. genintconstnode(0)
  746. ),
  747. ccallnode.createintern('MOVE',
  748. ccallparanode.create(
  749. arraysize,
  750. ccallparanode.create(
  751. cderefnode.create(ctemprefnode.create(paratemp)),
  752. ccallparanode.create(
  753. arraybegin,nil
  754. )
  755. )
  756. )
  757. ),
  758. nil
  759. )
  760. );
  761. { no reference count increases, that's still done on the callee
  762. side because for compatibility with targets that perform this
  763. copy on the callee side, that should only be done for non-
  764. assember functions (and we can't know that 100% certain here,
  765. e.g. in case of external declarations) (*) }
  766. { free the memory again after the call: freemem(paratemp) }
  767. addstatement(finistat,
  768. ccallnode.createintern('fpc_freemem',
  769. ccallparanode.create(
  770. ctemprefnode.create(paratemp),nil
  771. )
  772. )
  773. );
  774. { replace the original parameter with a dereference of the
  775. temp typecasted to the same type as the original parameter
  776. (don't free left, it has been reused above) }
  777. left:=ctypeconvnode.create_internal(
  778. cderefnode.create(ctemprefnode.create(paratemp)),
  779. left.resultdef);
  780. end
  781. else if is_shortstring(parasym.vardef) then
  782. begin
  783. { the shortstring parameter may have a different size than the
  784. parameter type -> assign and truncate/extend }
  785. paratemp:=ctempcreatenode.create(parasym.vardef,parasym.vardef.size,tt_persistent,false);
  786. addstatement(initstat,paratemp);
  787. { assign shortstring }
  788. addstatement(initstat,
  789. cassignmentnode.create(
  790. ctemprefnode.create(paratemp),left
  791. )
  792. );
  793. { replace parameter with temp (don't free left, it has been
  794. reused above) }
  795. left:=ctemprefnode.create(paratemp);
  796. end
  797. else if parasym.vardef.typ=variantdef then
  798. begin
  799. vardatatype:=search_system_type('TVARDATA').typedef;
  800. paratemp:=ctempcreatenode.create(vardatatype,vardatatype.size,tt_persistent,false);
  801. addstatement(initstat,paratemp);
  802. addstatement(initstat,
  803. ccallnode.createintern('fpc_variant_copy_overwrite',
  804. ccallparanode.create(
  805. ctypeconvnode.create_explicit(ctemprefnode.create(paratemp),
  806. vardatatype
  807. ),
  808. ccallparanode.create(ctypeconvnode.create_explicit(left,
  809. vardatatype),
  810. nil
  811. )
  812. )
  813. )
  814. );
  815. { replace parameter with temp (don't free left, it has been
  816. reused above) }
  817. left:=ctypeconvnode.create_explicit(ctemprefnode.create(paratemp),parasym.vardef);
  818. end
  819. else if is_managed_type(left.resultdef) then
  820. begin
  821. { don't increase/decrease the reference count here, will be done by
  822. the callee (see (*) above) -> typecast to array of byte
  823. for the assignment to the temp }
  824. temparraydef:=carraydef.getreusable(u8inttype,left.resultdef.size);
  825. paratemp:=ctempcreatenode.create(temparraydef,temparraydef.size,tt_persistent,false);
  826. addstatement(initstat,paratemp);
  827. addstatement(initstat,
  828. cassignmentnode.create(
  829. ctemprefnode.create(paratemp),
  830. ctypeconvnode.create_internal(left,temparraydef)
  831. )
  832. );
  833. left:=ctypeconvnode.create_explicit(ctemprefnode.create(paratemp),left.resultdef);
  834. end
  835. else
  836. begin
  837. paratemp:=ctempcreatenode.create(left.resultdef,left.resultdef.size,tt_persistent,false);
  838. addstatement(initstat,paratemp);
  839. addstatement(initstat,
  840. cassignmentnode.create(ctemprefnode.create(paratemp),left)
  841. );
  842. { replace parameter with temp (don't free left, it has been
  843. reused above) }
  844. left:=ctemprefnode.create(paratemp);
  845. end;
  846. addstatement(finistat,ctempdeletenode.create(paratemp));
  847. addstatement(copybackstat,finiblock);
  848. firstpass(fparainit);
  849. firstpass(left);
  850. firstpass(fparacopyback);
  851. end;
  852. end;
  853. constructor tcallparanode.create(expr,next : tnode);
  854. begin
  855. inherited create(callparan,expr,next,nil);
  856. if not assigned(expr) then
  857. internalerror(200305091);
  858. expr.fileinfo:=fileinfo;
  859. callparaflags:=[];
  860. if expr.nodetype = typeconvn then
  861. ttypeconvnode(expr).warn_pointer_to_signed:=false;
  862. end;
  863. destructor tcallparanode.destroy;
  864. begin
  865. fparainit.free;
  866. fparacopyback.free;
  867. inherited destroy;
  868. end;
  869. constructor tcallparanode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  870. begin
  871. inherited ppuload(t,ppufile);
  872. ppufile.getsmallset(callparaflags);
  873. fparainit:=ppuloadnode(ppufile);
  874. fparacopyback:=ppuloadnode(ppufile);
  875. end;
  876. procedure tcallparanode.ppuwrite(ppufile:tcompilerppufile);
  877. begin
  878. inherited ppuwrite(ppufile);
  879. ppufile.putsmallset(callparaflags);
  880. ppuwritenode(ppufile,fparainit);
  881. ppuwritenode(ppufile,fparacopyback);
  882. end;
  883. procedure tcallparanode.buildderefimpl;
  884. begin
  885. inherited buildderefimpl;
  886. if assigned(fparainit) then
  887. fparainit.buildderefimpl;
  888. if assigned(fparacopyback) then
  889. fparacopyback.buildderefimpl;
  890. end;
  891. procedure tcallparanode.derefimpl;
  892. begin
  893. inherited derefimpl;
  894. if assigned(fparainit) then
  895. fparainit.derefimpl;
  896. if assigned(fparacopyback) then
  897. fparacopyback.derefimpl;
  898. end;
  899. function tcallparanode.dogetcopy : tnode;
  900. var
  901. n : tcallparanode;
  902. initcopy: tnode;
  903. begin
  904. initcopy:=nil;
  905. { must be done before calling inherited getcopy, because can create
  906. tempcreatenodes for values used in left }
  907. if assigned(fparainit) then
  908. initcopy:=fparainit.getcopy;
  909. n:=tcallparanode(inherited dogetcopy);
  910. n.callparaflags:=callparaflags;
  911. n.parasym:=parasym;
  912. n.fparainit:=initcopy;
  913. if assigned(fparacopyback) then
  914. n.fparacopyback:=fparacopyback.getcopy;
  915. result:=n;
  916. end;
  917. procedure tcallparanode.insertintolist(l : tnodelist);
  918. begin
  919. end;
  920. function tcallparanode.pass_typecheck : tnode;
  921. begin
  922. { need to use get_paratype }
  923. internalerror(200709251);
  924. result:=nil;
  925. end;
  926. function tcallparanode.pass_1 : tnode;
  927. begin
  928. { need to use firstcallparan }
  929. internalerror(200709252);
  930. result:=nil;
  931. end;
  932. procedure tcallparanode.get_paratype;
  933. begin
  934. if assigned(right) then
  935. tcallparanode(right).get_paratype;
  936. if assigned(fparainit) then
  937. typecheckpass(fparainit);
  938. typecheckpass(left);
  939. if assigned(third) then
  940. typecheckpass(third);
  941. if assigned(fparacopyback) then
  942. typecheckpass(fparacopyback);
  943. if codegenerror then
  944. resultdef:=generrordef
  945. else
  946. resultdef:=left.resultdef;
  947. end;
  948. procedure tcallparanode.firstcallparan;
  949. begin
  950. if assigned(right) then
  951. tcallparanode(right).firstcallparan;
  952. if not assigned(left.resultdef) then
  953. get_paratype;
  954. if assigned(parasym) and
  955. (parasym.varspez in [vs_var,vs_out,vs_constref]) and
  956. { for record constructors }
  957. (left.nodetype<>nothingn) then
  958. handlemanagedbyrefpara(left.resultdef);
  959. { for targets that have to copy "value parameters by reference" on the
  960. caller side
  961. aktcallnode may not be assigned in case firstcallparan is called for
  962. fake parameters to inline nodes (in that case, we don't have a real
  963. call and hence no "caller side" either)
  964. }
  965. if assigned(aktcallnode) and
  966. (target_info.system in systems_caller_copy_addr_value_para) and
  967. ((assigned(parasym) and
  968. (parasym.varspez=vs_value)) or
  969. (cpf_varargs_para in callparaflags)) and
  970. (left.nodetype<>nothingn) and
  971. not(vo_has_local_copy in parasym.varoptions) and
  972. ((not is_open_array(parasym.vardef) and
  973. not is_array_of_const(parasym.vardef)) or
  974. not(aktcallnode.procdefinition.proccalloption in cdecl_pocalls)) and
  975. paramanager.push_addr_param(vs_value,parasym.vardef,
  976. aktcallnode.procdefinition.proccalloption) then
  977. copy_value_by_ref_para;
  978. { does it need to load RTTI? }
  979. if assigned(parasym) and (parasym.varspez=vs_out) and
  980. (cs_create_pic in current_settings.moduleswitches) and
  981. (
  982. is_rtti_managed_type(left.resultdef) or
  983. (
  984. is_open_array(resultdef) and
  985. is_managed_type(tarraydef(resultdef).elementdef)
  986. )
  987. ) and
  988. not(target_info.system in systems_garbage_collected_managed_types) then
  989. include(current_procinfo.flags,pi_needs_got);
  990. if assigned(fparainit) then
  991. firstpass(fparainit);
  992. firstpass(left);
  993. if assigned(fparacopyback) then
  994. firstpass(fparacopyback);
  995. if assigned(third) then
  996. firstpass(third);
  997. expectloc:=left.expectloc;
  998. end;
  999. procedure tcallparanode.insert_typeconv;
  1000. var
  1001. olddef : tdef;
  1002. hp : tnode;
  1003. block : tblocknode;
  1004. statements : tstatementnode;
  1005. temp : ttempcreatenode;
  1006. owningprocdef: tprocdef;
  1007. begin
  1008. { Be sure to have the resultdef }
  1009. if not assigned(left.resultdef) then
  1010. typecheckpass(left);
  1011. if (left.nodetype<>nothingn) then
  1012. begin
  1013. { convert loads of the function result variable into procvars
  1014. representing the current function in case the formal parameter is
  1015. a procvar (CodeWarrior Pascal contains the same kind of
  1016. automatic disambiguation; you can use the function name in both
  1017. meanings, so we cannot statically pick either the function result
  1018. or the function definition in pexpr) }
  1019. if (m_mac in current_settings.modeswitches) and
  1020. (parasym.vardef.typ=procvardef) and
  1021. is_ambiguous_funcret_load(left,owningprocdef) then
  1022. begin
  1023. hp:=cloadnode.create_procvar(owningprocdef.procsym,owningprocdef,owningprocdef.procsym.owner);
  1024. typecheckpass(hp);
  1025. left.free;
  1026. left:=hp;
  1027. end;
  1028. { Convert tp procvars, this is needs to be done
  1029. here to make the change permanent. in the overload
  1030. choosing the changes are only made temporarily
  1031. Don't do this for parentfp parameters, as for calls to nested
  1032. procvars they are a copy of right, which is the procvar itself
  1033. and hence turning that into a call would result into endless
  1034. recursion. For regular nested calls, the parentfp node can
  1035. never be a procvar (it's a loadparentfpnode). }
  1036. if not(vo_is_parentfp in parasym.varoptions) and
  1037. (left.resultdef.typ=procvardef) and
  1038. not(parasym.vardef.typ in [procvardef,formaldef]) then
  1039. begin
  1040. if maybe_call_procvar(left,true) then
  1041. resultdef:=left.resultdef
  1042. end;
  1043. { Remove implicitly inserted typecast to pointer for
  1044. @procvar in macpas }
  1045. if (m_mac_procvar in current_settings.modeswitches) and
  1046. (parasym.vardef.typ=procvardef) and
  1047. (left.nodetype=typeconvn) and
  1048. is_voidpointer(left.resultdef) and
  1049. (ttypeconvnode(left).left.nodetype=typeconvn) and
  1050. (ttypeconvnode(ttypeconvnode(left).left).convtype=tc_proc_2_procvar) then
  1051. begin
  1052. hp:=left;
  1053. left:=ttypeconvnode(left).left;
  1054. ttypeconvnode(hp).left:=nil;
  1055. hp.free;
  1056. end;
  1057. maybe_global_proc_to_nested(left,parasym.vardef);
  1058. { Handle varargs and hidden paras directly, no typeconvs or }
  1059. { pass_typechecking needed }
  1060. if (cpf_varargs_para in callparaflags) then
  1061. begin
  1062. { this should only happen vor C varargs }
  1063. { the necessary conversions have already been performed in }
  1064. { tarrayconstructornode.insert_typeconvs }
  1065. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1066. insert_varargstypeconv(left,true);
  1067. resultdef:=left.resultdef;
  1068. { also update parasym type to get the correct parameter location
  1069. for the new types }
  1070. parasym.vardef:=left.resultdef;
  1071. end
  1072. else
  1073. if (vo_is_hidden_para in parasym.varoptions) then
  1074. begin
  1075. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1076. resultdef:=left.resultdef;
  1077. end
  1078. else
  1079. begin
  1080. { Do we need arrayconstructor -> set conversion, then insert
  1081. it here before the arrayconstructor node breaks the tree
  1082. with its conversions of enum->ord }
  1083. if (left.nodetype=arrayconstructorn) and
  1084. (parasym.vardef.typ=setdef) then
  1085. inserttypeconv(left,parasym.vardef);
  1086. { set some settings needed for arrayconstructor }
  1087. if is_array_constructor(left.resultdef) then
  1088. begin
  1089. if left.nodetype<>arrayconstructorn then
  1090. internalerror(200504041);
  1091. if is_array_of_const(parasym.vardef) then
  1092. begin
  1093. { force variant array }
  1094. include(left.flags,nf_forcevaria);
  1095. end
  1096. else
  1097. begin
  1098. include(left.flags,nf_novariaallowed);
  1099. { now that the resultting type is know we can insert the required
  1100. typeconvs for the array constructor }
  1101. if parasym.vardef.typ=arraydef then
  1102. tarrayconstructornode(left).force_type(tarraydef(parasym.vardef).elementdef);
  1103. end;
  1104. end;
  1105. { check if local proc/func is assigned to procvar }
  1106. if left.resultdef.typ=procvardef then
  1107. test_local_to_procvar(tprocvardef(left.resultdef),parasym.vardef);
  1108. { test conversions }
  1109. if not(is_shortstring(left.resultdef) and
  1110. is_shortstring(parasym.vardef)) and
  1111. (parasym.vardef.typ<>formaldef) and
  1112. not(parasym.univpara) then
  1113. begin
  1114. { Process open parameters }
  1115. if paramanager.keep_para_array_range(parasym.varspez,parasym.vardef,aktcallnode.procdefinition.proccalloption) then
  1116. begin
  1117. { insert type conv but hold the ranges of the array }
  1118. olddef:=left.resultdef;
  1119. inserttypeconv(left,parasym.vardef);
  1120. left.resultdef:=olddef;
  1121. end
  1122. else
  1123. begin
  1124. check_ranges(left.fileinfo,left,parasym.vardef);
  1125. inserttypeconv(left,parasym.vardef);
  1126. end;
  1127. if codegenerror then
  1128. exit;
  1129. end;
  1130. { truncate shortstring value parameters at the caller side if }
  1131. { they are passed by value (if passed by reference, then the }
  1132. { callee will truncate when copying in the string) }
  1133. { This happens e.g. on x86_64 for small strings }
  1134. if is_shortstring(left.resultdef) and
  1135. is_shortstring(parasym.vardef) and
  1136. (parasym.varspez=vs_value) and
  1137. not paramanager.push_addr_param(parasym.varspez,parasym.vardef,
  1138. aktcallnode.procdefinition.proccalloption) and
  1139. ((is_open_string(left.resultdef) and
  1140. (tstringdef(parasym.vardef).len < 255)) or
  1141. (not is_open_string(left.resultdef) and
  1142. { when a stringconstn is typeconverted, then only its }
  1143. { def is modified, not the contents (needed because in }
  1144. { Delphi/TP, if you pass a longer string to a const }
  1145. { parameter, then the callee has to see this longer }
  1146. { string) }
  1147. (((left.nodetype<>stringconstn) and
  1148. (tstringdef(parasym.vardef).len<tstringdef(left.resultdef).len)) or
  1149. ((left.nodetype=stringconstn) and
  1150. (tstringdef(parasym.vardef).len<tstringconstnode(left).len))))) then
  1151. begin
  1152. block:=internalstatements(statements);
  1153. { temp for the new string }
  1154. temp:=ctempcreatenode.create(parasym.vardef,parasym.vardef.size,
  1155. tt_persistent,true);
  1156. addstatement(statements,temp);
  1157. { assign parameter to temp }
  1158. addstatement(statements,cassignmentnode.create(ctemprefnode.create(temp),left));
  1159. left:=nil;
  1160. { release temp after next use }
  1161. addstatement(statements,ctempdeletenode.create_normal_temp(temp));
  1162. addstatement(statements,ctemprefnode.create(temp));
  1163. typecheckpass(tnode(block));
  1164. left:=block;
  1165. end;
  1166. { check var strings }
  1167. if (cs_strict_var_strings in current_settings.localswitches) and
  1168. is_shortstring(left.resultdef) and
  1169. is_shortstring(parasym.vardef) and
  1170. (parasym.varspez in [vs_out,vs_var,vs_constref]) and
  1171. not(is_open_string(parasym.vardef)) and
  1172. not(equal_defs(left.resultdef,parasym.vardef)) then
  1173. begin
  1174. CGMessagePos(left.fileinfo,type_e_strict_var_string_violation);
  1175. end;
  1176. { passing a value to an "univ" parameter implies an explicit
  1177. typecast to the parameter type. Must be done before the
  1178. valid_for_var() check, since the typecast can result in
  1179. an invalid lvalue in case of var/out parameters. }
  1180. if (parasym.univpara) then
  1181. begin
  1182. { load procvar if a procedure is passed }
  1183. if ((m_tp_procvar in current_settings.modeswitches) or
  1184. (m_mac_procvar in current_settings.modeswitches)) and
  1185. (left.nodetype=calln) and
  1186. (is_void(left.resultdef)) then
  1187. begin
  1188. load_procvar_from_calln(left);
  1189. { load_procvar_from_calln() creates a loadn for a
  1190. a procedure, which means that the type conversion
  1191. below will type convert the first instruction
  1192. bytes of the procedure -> convert to a procvar }
  1193. left:=ctypeconvnode.create_proc_to_procvar(left);
  1194. typecheckpass(left);
  1195. end;
  1196. inserttypeconv_explicit(left,parasym.vardef);
  1197. end;
  1198. { Handle formal parameters separate }
  1199. if (parasym.vardef.typ=formaldef) then
  1200. begin
  1201. { load procvar if a procedure is passed }
  1202. if ((m_tp_procvar in current_settings.modeswitches) or
  1203. (m_mac_procvar in current_settings.modeswitches)) and
  1204. (left.nodetype=calln) and
  1205. (is_void(left.resultdef)) then
  1206. load_procvar_from_calln(left);
  1207. case parasym.varspez of
  1208. vs_var,
  1209. vs_constref,
  1210. vs_out :
  1211. begin
  1212. if not valid_for_formal_var(left,true) then
  1213. CGMessagePos(left.fileinfo,parser_e_illegal_parameter_list);
  1214. end;
  1215. vs_const :
  1216. begin
  1217. if not valid_for_formal_const(left,true) then
  1218. CGMessagePos(left.fileinfo,parser_e_illegal_parameter_list)
  1219. else if (target_info.system in systems_managed_vm) and
  1220. (left.resultdef.typ in [orddef,floatdef]) then
  1221. begin
  1222. left:=cinlinenode.create(in_box_x,false,ccallparanode.create(left,nil));
  1223. typecheckpass(left);
  1224. end;
  1225. end;
  1226. end;
  1227. end
  1228. else
  1229. begin
  1230. { check if the argument is allowed }
  1231. if (parasym.varspez in [vs_out,vs_var]) then
  1232. valid_for_var(left,true);
  1233. end;
  1234. if parasym.varspez in [vs_var,vs_out,vs_constref] then
  1235. set_unique(left);
  1236. case parasym.varspez of
  1237. vs_out :
  1238. begin
  1239. { first set written separately to avoid false }
  1240. { uninitialized warnings (tbs/tb0542) }
  1241. set_varstate(left,vs_written,[]);
  1242. set_varstate(left,vs_readwritten,[]);
  1243. { compilerprocs never capture the address of their
  1244. parameters }
  1245. if not(po_compilerproc in aktcallnode.procdefinition.procoptions) then
  1246. make_not_regable(left,[ra_addr_regable,ra_addr_taken])
  1247. else
  1248. make_not_regable(left,[ra_addr_regable])
  1249. end;
  1250. vs_var,
  1251. vs_constref:
  1252. begin
  1253. set_varstate(left,vs_readwritten,[vsf_must_be_valid,vsf_use_hints]);
  1254. { constref takes also the address, but storing it is actually the compiler
  1255. is not supposed to expect }
  1256. if parasym.varspez=vs_var then
  1257. { compilerprocs never capture the address of their
  1258. parameters }
  1259. if not(po_compilerproc in aktcallnode.procdefinition.procoptions) then
  1260. make_not_regable(left,[ra_addr_regable,ra_addr_taken])
  1261. else
  1262. make_not_regable(left,[ra_addr_regable])
  1263. end;
  1264. else
  1265. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1266. end;
  1267. { must only be done after typeconv PM }
  1268. resultdef:=parasym.vardef;
  1269. end;
  1270. end;
  1271. { process next node }
  1272. if assigned(right) then
  1273. tcallparanode(right).insert_typeconv;
  1274. end;
  1275. function tcallparanode.can_be_inlined: boolean;
  1276. var
  1277. n: tnode;
  1278. begin
  1279. n:=left;
  1280. result:=false;
  1281. while assigned(n) and
  1282. (n.nodetype=typeconvn) do
  1283. begin
  1284. { look for type conversion nodes which convert a }
  1285. { refcounted type into a non-refcounted type }
  1286. if not is_managed_type(n.resultdef) and
  1287. is_managed_type(ttypeconvnode(n).left.resultdef) then
  1288. exit;
  1289. n:=ttypeconvnode(n).left;
  1290. end;
  1291. { also check for dereferencing constant pointers, like }
  1292. { tsomerecord(nil^) passed to a const r: tsomerecord }
  1293. { parameter }
  1294. if (n.nodetype=derefn) then
  1295. begin
  1296. repeat
  1297. n:=tunarynode(n).left;
  1298. until (n.nodetype<>typeconvn);
  1299. if (n.nodetype in [niln,pointerconstn]) then
  1300. exit
  1301. end;
  1302. result:=true;
  1303. end;
  1304. function check_contains_stack_tainting_call(var n: tnode; arg: pointer): foreachnoderesult;
  1305. begin
  1306. if (n.nodetype=calln) and
  1307. tcallnode(n).procdefinition.stack_tainting_parameter(callerside) then
  1308. result:=fen_norecurse_true
  1309. else
  1310. result:=fen_false;
  1311. end;
  1312. function tcallparanode.contains_stack_tainting_call: boolean;
  1313. begin
  1314. result:=foreachnodestatic(pm_postprocess,left,@check_contains_stack_tainting_call,nil);
  1315. end;
  1316. procedure tcallparanode.init_contains_stack_tainting_call_cache;
  1317. begin
  1318. fcontains_stack_tainting_call_cached:=contains_stack_tainting_call;
  1319. end;
  1320. function tcallparanode.docompare(p: tnode): boolean;
  1321. begin
  1322. docompare :=
  1323. inherited docompare(p) and
  1324. fparainit.isequal(tcallparanode(p).fparainit) and
  1325. fparacopyback.isequal(tcallparanode(p).fparacopyback) and
  1326. (callparaflags = tcallparanode(p).callparaflags)
  1327. ;
  1328. end;
  1329. procedure tcallparanode.printnodetree(var t:text);
  1330. begin
  1331. printnodelist(t);
  1332. end;
  1333. {****************************************************************************
  1334. TCALLNODE
  1335. ****************************************************************************}
  1336. constructor tcallnode.create(l:tnode;v : tprocsym;st : TSymtable; mp: tnode; callflags:tcallnodeflags;sc:tspecializationcontext);
  1337. var
  1338. srsym: tsym;
  1339. srsymtable: tsymtable;
  1340. begin
  1341. inherited create(calln,l,nil);
  1342. spezcontext:=sc;
  1343. symtableprocentry:=v;
  1344. symtableproc:=st;
  1345. callnodeflags:=callflags+[cnf_return_value_used];
  1346. methodpointer:=mp;
  1347. callinitblock:=nil;
  1348. callcleanupblock:=nil;
  1349. procdefinition:=nil;
  1350. funcretnode:=nil;
  1351. paralength:=-1;
  1352. varargsparas:=nil;
  1353. if assigned(current_structdef) and
  1354. assigned(mp) and
  1355. assigned(current_procinfo) then
  1356. begin
  1357. { only needed when calling a destructor from an exception block in a
  1358. contructor of a TP-style object }
  1359. if (current_procinfo.procdef.proctypeoption=potype_constructor) and
  1360. (cnf_create_failed in callflags) then
  1361. if is_object(current_structdef) then
  1362. call_vmt_node:=load_vmt_pointer_node
  1363. else if is_class(current_structdef) then
  1364. begin
  1365. if not searchsym(copy(internaltypeprefixName[itp_vmt_afterconstruction_local],2,255),srsym,srsymtable) then
  1366. internalerror(2016090801);
  1367. call_vmt_node:=cloadnode.create(srsym,srsymtable);
  1368. end;
  1369. end;
  1370. end;
  1371. constructor tcallnode.create_procvar(l,r:tnode);
  1372. begin
  1373. create(l,nil,nil,nil,[],nil);
  1374. right:=r;
  1375. end;
  1376. constructor tcallnode.createintern(const name: string; params: tnode);
  1377. var
  1378. srsym: tsym;
  1379. begin
  1380. srsym := tsym(systemunit.Find(name));
  1381. { in case we are looking for a non-external compilerproc of which we
  1382. only have parsed the declaration until now (the symbol name will
  1383. still be uppercased, because it needs to be matched when we
  1384. encounter the implementation) }
  1385. if not assigned(srsym) and
  1386. (cs_compilesystem in current_settings.moduleswitches) then
  1387. srsym := tsym(systemunit.Find(upper(name)));
  1388. if not assigned(srsym) or
  1389. (srsym.typ<>procsym) then
  1390. Message1(cg_f_unknown_compilerproc,name);
  1391. create(params,tprocsym(srsym),srsym.owner,nil,[],nil);
  1392. end;
  1393. constructor tcallnode.createinternfromunit(const fromunit, procname: string; params: tnode);
  1394. var
  1395. srsym: tsym;
  1396. srsymtable: tsymtable;
  1397. begin
  1398. srsym:=nil;
  1399. if not searchsym_in_named_module(fromunit,procname,srsym,srsymtable) or
  1400. (srsym.typ<>procsym) then
  1401. Message1(cg_f_unknown_compilerproc,fromunit+'.'+procname);
  1402. create(params,tprocsym(srsym),srsymtable,nil,[],nil);
  1403. end;
  1404. constructor tcallnode.createinternres(const name: string; params: tnode; res:tdef);
  1405. var
  1406. pd : tprocdef;
  1407. begin
  1408. createintern(name,params);
  1409. typedef:=res;
  1410. include(callnodeflags,cnf_typedefset);
  1411. pd:=tprocdef(symtableprocentry.ProcdefList[0]);
  1412. { both the normal and specified resultdef either have to be returned via a }
  1413. { parameter or not, but no mixing (JM) }
  1414. if paramanager.ret_in_param(typedef,pd) xor
  1415. paramanager.ret_in_param(pd.returndef,pd) then
  1416. internalerror(2001082911);
  1417. end;
  1418. constructor tcallnode.createinternresfromunit(const fromunit, procname: string; params: tnode; res:tdef);
  1419. var
  1420. pd : tprocdef;
  1421. begin
  1422. createinternfromunit(fromunit,procname,params);
  1423. typedef:=res;
  1424. include(callnodeflags,cnf_typedefset);
  1425. pd:=tprocdef(symtableprocentry.ProcdefList[0]);
  1426. { both the normal and specified resultdef either have to be returned via a }
  1427. { parameter or not, but no mixing (JM) }
  1428. if paramanager.ret_in_param(typedef,pd) xor
  1429. paramanager.ret_in_param(pd.returndef,pd) then
  1430. internalerror(200108291);
  1431. end;
  1432. constructor tcallnode.createinternreturn(const name: string; params: tnode; returnnode : tnode);
  1433. begin
  1434. createintern(name,params);
  1435. funcretnode:=returnnode;
  1436. end;
  1437. constructor tcallnode.createinternmethod(mp: tnode; const name: string; params: tnode);
  1438. var
  1439. ps: tsym;
  1440. recdef: tabstractrecorddef;
  1441. begin
  1442. typecheckpass(mp);
  1443. if mp.resultdef.typ=classrefdef then
  1444. recdef:=tabstractrecorddef(tclassrefdef(mp.resultdef).pointeddef)
  1445. else
  1446. recdef:=tabstractrecorddef(mp.resultdef);
  1447. ps:=search_struct_member(recdef,name);
  1448. if not assigned(ps) or
  1449. (ps.typ<>procsym) then
  1450. internalerror(2011062806);
  1451. create(params,tprocsym(ps),ps.owner,mp,[],nil);
  1452. end;
  1453. constructor tcallnode.createinternmethodres(mp: tnode; const name: string; params: tnode; res: tdef);
  1454. begin
  1455. createinternmethod(mp,name,params);
  1456. typedef:=res;
  1457. include(callnodeflags,cnf_typedefset)
  1458. end;
  1459. destructor tcallnode.destroy;
  1460. begin
  1461. methodpointer.free;
  1462. callinitblock.free;
  1463. callcleanupblock.free;
  1464. funcretnode.free;
  1465. if assigned(varargsparas) then
  1466. varargsparas.free;
  1467. call_self_node.free;
  1468. call_vmt_node.free;
  1469. vmt_entry.free;
  1470. {$ifndef symansistr}
  1471. stringdispose(fforcedprocname);
  1472. {$endif symansistr}
  1473. inherited destroy;
  1474. end;
  1475. constructor tcallnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1476. begin
  1477. callinitblock:=tblocknode(ppuloadnode(ppufile));
  1478. methodpointer:=ppuloadnode(ppufile);
  1479. call_self_node:=ppuloadnode(ppufile);
  1480. call_vmt_node:=ppuloadnode(ppufile);
  1481. callcleanupblock:=tblocknode(ppuloadnode(ppufile));
  1482. funcretnode:=ppuloadnode(ppufile);
  1483. inherited ppuload(t,ppufile);
  1484. ppufile.getderef(symtableprocentryderef);
  1485. { TODO: FIXME: No withsymtable support}
  1486. symtableproc:=nil;
  1487. ppufile.getderef(procdefinitionderef);
  1488. ppufile.getsmallset(callnodeflags);
  1489. end;
  1490. procedure tcallnode.ppuwrite(ppufile:tcompilerppufile);
  1491. begin
  1492. ppuwritenode(ppufile,callinitblock);
  1493. ppuwritenode(ppufile,methodpointer);
  1494. ppuwritenode(ppufile,call_self_node);
  1495. ppuwritenode(ppufile,call_vmt_node);
  1496. ppuwritenode(ppufile,callcleanupblock);
  1497. ppuwritenode(ppufile,funcretnode);
  1498. inherited ppuwrite(ppufile);
  1499. ppufile.putderef(symtableprocentryderef);
  1500. ppufile.putderef(procdefinitionderef);
  1501. ppufile.putsmallset(callnodeflags);
  1502. end;
  1503. procedure tcallnode.buildderefimpl;
  1504. begin
  1505. inherited buildderefimpl;
  1506. symtableprocentryderef.build(symtableprocentry);
  1507. procdefinitionderef.build(procdefinition);
  1508. if assigned(methodpointer) then
  1509. methodpointer.buildderefimpl;
  1510. if assigned(call_self_node) then
  1511. call_self_node.buildderefimpl;
  1512. if assigned(call_vmt_node) then
  1513. call_vmt_node.buildderefimpl;
  1514. if assigned(callinitblock) then
  1515. callinitblock.buildderefimpl;
  1516. if assigned(callcleanupblock) then
  1517. callcleanupblock.buildderefimpl;
  1518. if assigned(funcretnode) then
  1519. funcretnode.buildderefimpl;
  1520. end;
  1521. procedure tcallnode.derefimpl;
  1522. var
  1523. pt : tcallparanode;
  1524. i : integer;
  1525. begin
  1526. inherited derefimpl;
  1527. symtableprocentry:=tprocsym(symtableprocentryderef.resolve);
  1528. if assigned(symtableprocentry) then
  1529. symtableproc:=symtableprocentry.owner;
  1530. procdefinition:=tabstractprocdef(procdefinitionderef.resolve);
  1531. if assigned(methodpointer) then
  1532. methodpointer.derefimpl;
  1533. if assigned(call_self_node) then
  1534. call_self_node.derefimpl;
  1535. if assigned(call_vmt_node) then
  1536. call_vmt_node.derefimpl;
  1537. if assigned(callinitblock) then
  1538. callinitblock.derefimpl;
  1539. if assigned(callcleanupblock) then
  1540. callcleanupblock.derefimpl;
  1541. if assigned(funcretnode) then
  1542. funcretnode.derefimpl;
  1543. { generic method has no procdefinition }
  1544. if assigned(procdefinition) then
  1545. begin
  1546. { Connect parasyms }
  1547. pt:=tcallparanode(left);
  1548. while assigned(pt) and
  1549. (cpf_varargs_para in pt.callparaflags) do
  1550. pt:=tcallparanode(pt.right);
  1551. for i:=procdefinition.paras.count-1 downto 0 do
  1552. begin
  1553. if not assigned(pt) then
  1554. internalerror(200311077);
  1555. pt.parasym:=tparavarsym(procdefinition.paras[i]);
  1556. pt:=tcallparanode(pt.right);
  1557. end;
  1558. if assigned(pt) then
  1559. internalerror(200311078);
  1560. end;
  1561. end;
  1562. function tcallnode.dogetcopy : tnode;
  1563. var
  1564. n : tcallnode;
  1565. i : integer;
  1566. hp,hpn : tparavarsym;
  1567. oldleft, oldright : tnode;
  1568. para: tcallparanode;
  1569. begin
  1570. { Need to use a hack here to prevent the parameters from being copied.
  1571. The parameters must be copied between callinitblock/callcleanupblock because
  1572. they can reference methodpointer }
  1573. { same goes for right (= self/context for procvars) }
  1574. oldleft:=left;
  1575. left:=nil;
  1576. oldright:=right;
  1577. right:=nil;
  1578. n:=tcallnode(inherited dogetcopy);
  1579. left:=oldleft;
  1580. right:=oldright;
  1581. n.symtableprocentry:=symtableprocentry;
  1582. n.symtableproc:=symtableproc;
  1583. n.procdefinition:=procdefinition;
  1584. n.typedef := typedef;
  1585. n.callnodeflags := callnodeflags;
  1586. n.pushedparasize:=pushedparasize;
  1587. if assigned(callinitblock) then
  1588. n.callinitblock:=tblocknode(callinitblock.dogetcopy)
  1589. else
  1590. n.callinitblock:=nil;
  1591. { callinitblock is copied, now references to the temp will also be copied
  1592. correctly. We can now copy the parameters, funcret and methodpointer }
  1593. if assigned(left) then
  1594. n.left:=left.dogetcopy
  1595. else
  1596. n.left:=nil;
  1597. if assigned(right) then
  1598. n.right:=right.dogetcopy
  1599. else
  1600. n.right:=nil;
  1601. if assigned(methodpointer) then
  1602. n.methodpointer:=methodpointer.dogetcopy
  1603. else
  1604. n.methodpointer:=nil;
  1605. if assigned(call_self_node) then
  1606. n.call_self_node:=call_self_node.dogetcopy
  1607. else
  1608. n.call_self_node:=nil;
  1609. if assigned(call_vmt_node) then
  1610. n.call_vmt_node:=call_vmt_node.dogetcopy
  1611. else
  1612. n.call_vmt_node:=nil;
  1613. if assigned(vmt_entry) then
  1614. n.vmt_entry:=vmt_entry.dogetcopy
  1615. else
  1616. n.vmt_entry:=nil;
  1617. { must be copied before the funcretnode, because the callcleanup block
  1618. may contain a ttempdeletenode that sets the tempinfo of the
  1619. corresponding temp to ti_nextref_set_hookoncopy_nil, and this nextref
  1620. itself may be the funcretnode }
  1621. if assigned(callcleanupblock) then
  1622. n.callcleanupblock:=tblocknode(callcleanupblock.dogetcopy)
  1623. else
  1624. n.callcleanupblock:=nil;
  1625. if assigned(funcretnode) then
  1626. n.funcretnode:=funcretnode.dogetcopy
  1627. else
  1628. n.funcretnode:=nil;
  1629. if assigned(varargsparas) then
  1630. begin
  1631. n.varargsparas:=tvarargsparalist.create(true);
  1632. for i:=0 to varargsparas.count-1 do
  1633. begin
  1634. hp:=tparavarsym(varargsparas[i]);
  1635. hpn:=cparavarsym.create(hp.realname,hp.paranr,hp.varspez,hp.vardef,[]);
  1636. n.varargsparas.add(hpn);
  1637. para:=tcallparanode(n.left);
  1638. while assigned(para) do
  1639. begin
  1640. if (para.parasym=hp) then
  1641. para.parasym:=hpn;
  1642. para:=tcallparanode(para.right);
  1643. end;
  1644. end;
  1645. end
  1646. else
  1647. n.varargsparas:=nil;
  1648. {$ifdef symansistr}
  1649. n.fforcedprocname:=fforcedprocname;
  1650. {$else symansistr}
  1651. if assigned(fforcedprocname) then
  1652. n.fforcedprocname:=stringdup(fforcedprocname^)
  1653. else
  1654. n.fforcedprocname:=nil;
  1655. {$endif symansistr}
  1656. result:=n;
  1657. end;
  1658. function tcallnode.docompare(p: tnode): boolean;
  1659. begin
  1660. docompare :=
  1661. inherited docompare(p) and
  1662. (symtableprocentry = tcallnode(p).symtableprocentry) and
  1663. (procdefinition = tcallnode(p).procdefinition) and
  1664. { this implicitly also compares the vmt_entry node, as it is
  1665. deterministically based on the methodpointer }
  1666. (methodpointer.isequal(tcallnode(p).methodpointer)) and
  1667. (((cnf_typedefset in callnodeflags) and (cnf_typedefset in tcallnode(p).callnodeflags) and
  1668. (equal_defs(typedef,tcallnode(p).typedef))) or
  1669. (not(cnf_typedefset in callnodeflags) and not(cnf_typedefset in tcallnode(p).callnodeflags)));
  1670. end;
  1671. procedure tcallnode.printnodedata(var t:text);
  1672. begin
  1673. if assigned(procdefinition) and
  1674. (procdefinition.typ=procdef) then
  1675. writeln(t,printnodeindention,'proc = ',tprocdef(procdefinition).fullprocname(true))
  1676. else
  1677. begin
  1678. if assigned(symtableprocentry) then
  1679. writeln(t,printnodeindention,'proc = ',symtableprocentry.name)
  1680. else
  1681. writeln(t,printnodeindention,'proc = <nil>');
  1682. end;
  1683. if assigned(methodpointer) then
  1684. begin
  1685. writeln(t,printnodeindention,'methodpointer =');
  1686. printnode(t,methodpointer);
  1687. end;
  1688. if assigned(funcretnode) then
  1689. begin
  1690. writeln(t,printnodeindention,'funcretnode =');
  1691. printnode(t,funcretnode);
  1692. end;
  1693. if assigned(callinitblock) then
  1694. begin
  1695. writeln(t,printnodeindention,'callinitblock =');
  1696. printnode(t,callinitblock);
  1697. end;
  1698. if assigned(callcleanupblock) then
  1699. begin
  1700. writeln(t,printnodeindention,'callcleanupblock =');
  1701. printnode(t,callcleanupblock);
  1702. end;
  1703. if assigned(right) then
  1704. begin
  1705. writeln(t,printnodeindention,'right =');
  1706. printnode(t,right);
  1707. end;
  1708. if assigned(left) then
  1709. begin
  1710. writeln(t,printnodeindention,'left =');
  1711. printnode(t,left);
  1712. end;
  1713. end;
  1714. procedure tcallnode.insertintolist(l : tnodelist);
  1715. begin
  1716. end;
  1717. procedure tcallnode.add_init_statement(n:tnode);
  1718. var
  1719. lastinitstatement : tstatementnode;
  1720. begin
  1721. if not assigned(callinitblock) then
  1722. callinitblock:=internalstatements(lastinitstatement)
  1723. else
  1724. lastinitstatement:=laststatement(callinitblock);
  1725. { all these nodes must be immediately typechecked, because this routine }
  1726. { can be called from pass_1 (i.e., after typecheck has already run) and }
  1727. { moreover, the entire blocks themselves are also only typechecked in }
  1728. { pass_1, while the the typeinfo is already required after the }
  1729. { typecheck pass for simplify purposes (not yet perfect, because the }
  1730. { statementnodes themselves are not typechecked this way) }
  1731. firstpass(n);
  1732. addstatement(lastinitstatement,n);
  1733. end;
  1734. procedure tcallnode.add_done_statement(n:tnode);
  1735. var
  1736. lastdonestatement : tstatementnode;
  1737. begin
  1738. if not assigned(callcleanupblock) then
  1739. callcleanupblock:=internalstatements(lastdonestatement)
  1740. else
  1741. lastdonestatement:=laststatement(callcleanupblock);
  1742. { see comments in add_init_statement }
  1743. firstpass(n);
  1744. addstatement(lastdonestatement,n);
  1745. end;
  1746. function tcallnode.para_count:longint;
  1747. var
  1748. ppn : tcallparanode;
  1749. begin
  1750. result:=0;
  1751. ppn:=tcallparanode(left);
  1752. while assigned(ppn) do
  1753. begin
  1754. if not(assigned(ppn.parasym) and
  1755. (vo_is_hidden_para in ppn.parasym.varoptions)) then
  1756. inc(result);
  1757. ppn:=tcallparanode(ppn.right);
  1758. end;
  1759. end;
  1760. function tcallnode.required_para_count: longint;
  1761. var
  1762. ppn : tcallparanode;
  1763. begin
  1764. result:=0;
  1765. ppn:=tcallparanode(left);
  1766. while assigned(ppn) do
  1767. begin
  1768. if not(assigned(ppn.parasym) and
  1769. ((vo_is_hidden_para in ppn.parasym.varoptions) or
  1770. assigned(ppn.parasym.defaultconstsym))) then
  1771. inc(result);
  1772. ppn:=tcallparanode(ppn.right);
  1773. end;
  1774. end;
  1775. function tcallnode.is_simple_para_load(p:tnode; may_be_in_reg: boolean):boolean;
  1776. var
  1777. hp : tnode;
  1778. begin
  1779. hp:=p;
  1780. while assigned(hp) and
  1781. (hp.nodetype=typeconvn) and
  1782. (ttypeconvnode(hp).convtype=tc_equal) do
  1783. hp:=tunarynode(hp).left;
  1784. result:=(hp.nodetype in [typen,loadvmtaddrn,loadn,temprefn,arrayconstructorn,addrn]);
  1785. if result and
  1786. not(may_be_in_reg) then
  1787. case hp.nodetype of
  1788. loadn:
  1789. result:=(tabstractvarsym(tloadnode(hp).symtableentry).varregable in [vr_none,vr_addr]);
  1790. temprefn:
  1791. result:=not(ti_may_be_in_reg in ttemprefnode(hp).tempflags);
  1792. end;
  1793. end;
  1794. function tcallnode.getforcedprocname: TSymStr;
  1795. begin
  1796. {$ifdef symansistr}
  1797. result:=fforcedprocname;
  1798. {$else}
  1799. if assigned(fforcedprocname) then
  1800. result:=fforcedprocname^
  1801. else
  1802. result:='';
  1803. {$endif}
  1804. end;
  1805. function look_for_call(var n: tnode; arg: pointer): foreachnoderesult;
  1806. begin
  1807. case n.nodetype of
  1808. calln,asn:
  1809. result := fen_norecurse_true;
  1810. typen,loadvmtaddrn,loadn,temprefn,arrayconstructorn:
  1811. result := fen_norecurse_false;
  1812. else
  1813. result := fen_false;
  1814. end;
  1815. end;
  1816. procedure tcallnode.maybe_load_in_temp(var p:tnode);
  1817. var
  1818. loadp,
  1819. refp : tnode;
  1820. hdef : tdef;
  1821. ptemp : ttempcreatenode;
  1822. usederef : boolean;
  1823. begin
  1824. { Load all complex loads into a temp to prevent
  1825. double calls to a function. We can't simply check for a hp.nodetype=calln }
  1826. if assigned(p) and
  1827. foreachnodestatic(p,@look_for_call,nil) then
  1828. begin
  1829. { temp create }
  1830. usederef:=(p.resultdef.typ in [arraydef,recorddef]) or
  1831. is_shortstring(p.resultdef) or
  1832. is_object(p.resultdef);
  1833. if usederef then
  1834. hdef:=cpointerdef.getreusable(p.resultdef)
  1835. else
  1836. hdef:=p.resultdef;
  1837. ptemp:=ctempcreatenode.create(hdef,hdef.size,tt_persistent,true);
  1838. if usederef then
  1839. begin
  1840. loadp:=caddrnode.create_internal(p);
  1841. refp:=cderefnode.create(ctemprefnode.create(ptemp));
  1842. end
  1843. else
  1844. begin
  1845. loadp:=p;
  1846. refp:=ctemprefnode.create(ptemp)
  1847. end;
  1848. add_init_statement(ptemp);
  1849. add_init_statement(cassignmentnode.create(
  1850. ctemprefnode.create(ptemp),
  1851. loadp));
  1852. add_done_statement(ctempdeletenode.create(ptemp));
  1853. { new tree is only a temp reference }
  1854. p:=refp;
  1855. typecheckpass(p);
  1856. end;
  1857. end;
  1858. function tcallnode.gen_high_tree(var p:tnode;paradef:tdef):tnode;
  1859. { When passing an array to an open array, or a string to an open string,
  1860. some code is needed that generates the high bound of the array. This
  1861. function returns a tree containing the nodes for it. }
  1862. var
  1863. temp: tnode;
  1864. len : integer;
  1865. loadconst : boolean;
  1866. hightree,l,r : tnode;
  1867. defkind: tdeftyp;
  1868. begin
  1869. len:=-1;
  1870. loadconst:=true;
  1871. hightree:=nil;
  1872. { constant strings are internally stored as array of char, but if the
  1873. parameter is a string also treat it like one }
  1874. defkind:=p.resultdef.typ;
  1875. if (p.nodetype=stringconstn) and
  1876. (paradef.typ=stringdef) then
  1877. defkind:=stringdef;
  1878. case defkind of
  1879. arraydef :
  1880. begin
  1881. if (paradef.typ<>arraydef) then
  1882. internalerror(200405241);
  1883. { passing a string to an array of char }
  1884. if (p.nodetype=stringconstn) and
  1885. is_char(tarraydef(paradef).elementdef) then
  1886. begin
  1887. len:=tstringconstnode(p).len;
  1888. if len>0 then
  1889. dec(len);
  1890. end
  1891. else
  1892. { handle special case of passing an single array to an array of array }
  1893. if compare_defs(tarraydef(paradef).elementdef,p.resultdef,nothingn)>=te_equal then
  1894. len:=0
  1895. else
  1896. begin
  1897. { handle via a normal inline in_high_x node }
  1898. loadconst:=false;
  1899. { slice? }
  1900. if (p.nodetype=inlinen) and (tinlinenode(p).inlinenumber=in_slice_x) then
  1901. with Tcallparanode(Tinlinenode(p).left) do
  1902. begin
  1903. {Array slice using slice builtin function.}
  1904. l:=Tcallparanode(right).left;
  1905. hightree:=caddnode.create(subn,geninlinenode(in_ord_x,false,l),genintconstnode(1));
  1906. Tcallparanode(right).left:=nil;
  1907. {Remove the inline node.}
  1908. temp:=p;
  1909. p:=left;
  1910. Tcallparanode(tinlinenode(temp).left).left:=nil;
  1911. temp.free;
  1912. typecheckpass(hightree);
  1913. end
  1914. else if (p.nodetype=vecn) and (Tvecnode(p).right.nodetype=rangen) then
  1915. begin
  1916. {Array slice using .. operator.}
  1917. with Trangenode(Tvecnode(p).right) do
  1918. begin
  1919. l:=geninlinenode(in_ord_x,false,left); {Get lower bound.}
  1920. r:=geninlinenode(in_ord_x,false,right); {Get upper bound.}
  1921. end;
  1922. {In the procedure the array range is 0..(upper_bound-lower_bound).}
  1923. hightree:=caddnode.create(subn,r,l);
  1924. {Replace the rangnode in the tree by its lower_bound, and
  1925. dispose the rangenode.}
  1926. temp:=Tvecnode(p).right;
  1927. Tvecnode(p).right:=l.getcopy;
  1928. {Typecheckpass can only be performed *after* the l.getcopy since it
  1929. can modify the tree, and l is in the hightree.}
  1930. typecheckpass(hightree);
  1931. with Trangenode(temp) do
  1932. begin
  1933. left:=nil;
  1934. right:=nil;
  1935. end;
  1936. temp.free;
  1937. {Tree changed from p[l..h] to p[l], recalculate resultdef.}
  1938. p.resultdef:=nil;
  1939. typecheckpass(p);
  1940. end
  1941. else
  1942. begin
  1943. maybe_load_in_temp(p);
  1944. hightree:=geninlinenode(in_ord_x,false,geninlinenode(in_high_x,false,p.getcopy));
  1945. typecheckpass(hightree);
  1946. { only substract low(array) if it's <> 0 }
  1947. temp:=geninlinenode(in_ord_x,false,geninlinenode(in_low_x,false,p.getcopy));
  1948. typecheckpass(temp);
  1949. if (temp.nodetype <> ordconstn) or
  1950. (tordconstnode(temp).value <> 0) then
  1951. begin
  1952. hightree:=caddnode.create(subn,hightree,temp);
  1953. include(hightree.flags,nf_internal);
  1954. end
  1955. else
  1956. temp.free;
  1957. end;
  1958. end;
  1959. end;
  1960. stringdef :
  1961. begin
  1962. if is_open_string(paradef) then
  1963. begin
  1964. { a stringconstn is not a simple parameter and hence would be
  1965. loaded in a temp, but in that case the high() node
  1966. a) goes wrong (it cannot deal with a temp node)
  1967. b) would give a generic result instead of one specific to
  1968. this constant string
  1969. }
  1970. if p.nodetype<>stringconstn then
  1971. maybe_load_in_temp(p);
  1972. { handle via a normal inline in_high_x node }
  1973. loadconst := false;
  1974. hightree := geninlinenode(in_high_x,false,p.getcopy);
  1975. end
  1976. else
  1977. { handle special case of passing an single string to an array of string }
  1978. if compare_defs(tarraydef(paradef).elementdef,p.resultdef,nothingn)>=te_equal then
  1979. len:=0
  1980. else
  1981. { passing a string to an array of char }
  1982. if (p.nodetype=stringconstn) and
  1983. is_char(tarraydef(paradef).elementdef) then
  1984. begin
  1985. len:=tstringconstnode(p).len;
  1986. if len>0 then
  1987. dec(len);
  1988. end
  1989. else
  1990. begin
  1991. maybe_load_in_temp(p);
  1992. hightree:=caddnode.create(subn,geninlinenode(in_length_x,false,p.getcopy),
  1993. cordconstnode.create(1,sizesinttype,false));
  1994. loadconst:=false;
  1995. end;
  1996. end;
  1997. else
  1998. len:=0;
  1999. end;
  2000. if loadconst then
  2001. hightree:=cordconstnode.create(len,sizesinttype,true)
  2002. else
  2003. begin
  2004. if not assigned(hightree) then
  2005. internalerror(200304071);
  2006. { Need to use explicit, because it can also be a enum }
  2007. hightree:=ctypeconvnode.create_internal(hightree,sizesinttype);
  2008. end;
  2009. result:=hightree;
  2010. end;
  2011. function tcallnode.gen_procvar_context_tree_self:tnode;
  2012. begin
  2013. { Load tmehodpointer(right).self }
  2014. result:=genloadfield(ctypeconvnode.create_internal(
  2015. right.getcopy,methodpointertype),
  2016. 'self');
  2017. end;
  2018. function tcallnode.gen_procvar_context_tree_parentfp: tnode;
  2019. begin
  2020. { Load tnestedprocpointer(right).parentfp }
  2021. result:=genloadfield(ctypeconvnode.create_internal(
  2022. right.getcopy,nestedprocpointertype),
  2023. 'parentfp');
  2024. end;
  2025. function tcallnode.gen_self_tree:tnode;
  2026. var
  2027. selftree : tnode;
  2028. selfdef : tdef;
  2029. temp : ttempcreatenode;
  2030. begin
  2031. selftree:=nil;
  2032. { When methodpointer was a callnode we must load it first into a
  2033. temp to prevent processing the callnode twice }
  2034. if (methodpointer.nodetype=calln) then
  2035. internalerror(200405121);
  2036. { Objective-C: objc_convert_to_message_send() already did all necessary
  2037. transformation on the methodpointer }
  2038. if (procdefinition.typ=procdef) and
  2039. (po_objc in tprocdef(procdefinition).procoptions) then
  2040. selftree:=methodpointer.getcopy
  2041. { inherited }
  2042. else if (cnf_inherited in callnodeflags) then
  2043. begin
  2044. selftree:=safe_call_self_node.getcopy;
  2045. { we can call an inherited class static/method from a regular method
  2046. -> self node must change from instance pointer to vmt pointer)
  2047. }
  2048. if (procdefinition.procoptions*[po_classmethod,po_staticmethod] <> []) and
  2049. (selftree.resultdef.typ<>classrefdef) then
  2050. selftree:=cloadvmtaddrnode.create(selftree);
  2051. end
  2052. else
  2053. { constructors }
  2054. if (procdefinition.proctypeoption=potype_constructor) then
  2055. begin
  2056. if (methodpointer.resultdef.typ=classrefdef) or
  2057. (cnf_new_call in callnodeflags) then
  2058. if not is_javaclass(tdef(procdefinition.owner.defowner)) then
  2059. begin
  2060. if (cnf_new_call in callnodeflags) then
  2061. { old-style object: push 0 as self }
  2062. selftree:=cpointerconstnode.create(0,voidpointertype)
  2063. else
  2064. begin
  2065. { class-style: push classtype }
  2066. selftree:=methodpointer.getcopy;
  2067. if selftree.nodetype=typen then
  2068. begin
  2069. selftree:=cloadvmtaddrnode.create(selftree);
  2070. tloadvmtaddrnode(selftree).forcall:=true;
  2071. end;
  2072. end;
  2073. end
  2074. else
  2075. { special handling for Java constructors, handled in
  2076. tjvmcallnode.extra_pre_call_code }
  2077. selftree:=cnothingnode.create
  2078. else
  2079. begin
  2080. if methodpointer.nodetype=typen then
  2081. if (methodpointer.resultdef.typ<>objectdef) then
  2082. begin
  2083. if not(target_info.system in systems_jvm) then
  2084. begin
  2085. { TSomeRecord.Constructor call. We need to allocate }
  2086. { self node as a temp node of the result type }
  2087. temp:=ctempcreatenode.create(methodpointer.resultdef,methodpointer.resultdef.size,tt_persistent,false);
  2088. add_init_statement(temp);
  2089. add_done_statement(ctempdeletenode.create_normal_temp(temp));
  2090. selftree:=ctemprefnode.create(temp);
  2091. end
  2092. else
  2093. begin
  2094. { special handling for Java constructors, handled in
  2095. tjvmcallnode.extra_pre_call_code }
  2096. selftree:=cnothingnode.create
  2097. end;
  2098. end
  2099. else
  2100. selftree:=safe_call_self_node.getcopy
  2101. else
  2102. selftree:=methodpointer.getcopy;
  2103. end;
  2104. end
  2105. else
  2106. { Calling a static/class method }
  2107. if (po_classmethod in procdefinition.procoptions) or
  2108. (po_staticmethod in procdefinition.procoptions) then
  2109. begin
  2110. if (procdefinition.typ<>procdef) then
  2111. internalerror(200305062);
  2112. { if the method belongs to a helper then we need to use the
  2113. extended type for references to Self }
  2114. if is_objectpascal_helper(tprocdef(procdefinition).struct) then
  2115. selfdef:=tobjectdef(tprocdef(procdefinition).struct).extendeddef
  2116. else
  2117. selfdef:=tprocdef(procdefinition).struct;
  2118. if ((selfdef.typ in [recorddef,objectdef]) and
  2119. (oo_has_vmt in tabstractrecorddef(selfdef).objectoptions)) or
  2120. { all Java classes have a "VMT" }
  2121. (target_info.system in systems_jvm) then
  2122. begin
  2123. { we only need the vmt, loading self is not required and there is no
  2124. need to check for typen, because that will always get the
  2125. loadvmtaddrnode added }
  2126. selftree:=methodpointer.getcopy;
  2127. if (methodpointer.resultdef.typ<>classrefdef) or
  2128. (methodpointer.nodetype = typen) then
  2129. selftree:=cloadvmtaddrnode.create(selftree);
  2130. end
  2131. else
  2132. selftree:=cpointerconstnode.create(0,voidpointertype);
  2133. end
  2134. else
  2135. begin
  2136. if methodpointer.nodetype=typen then
  2137. selftree:=safe_call_self_node.getcopy
  2138. else
  2139. selftree:=methodpointer.getcopy;
  2140. end;
  2141. result:=selftree;
  2142. end;
  2143. function tcallnode.use_caller_self(check_for_callee_self: boolean): boolean;
  2144. var
  2145. i: longint;
  2146. ps: tparavarsym;
  2147. begin
  2148. result:=false;
  2149. { is there a self parameter? }
  2150. if check_for_callee_self then
  2151. begin
  2152. ps:=nil;
  2153. for i:=0 to procdefinition.paras.count-1 do
  2154. begin
  2155. ps:=tparavarsym(procdefinition.paras[i]);
  2156. if vo_is_self in ps.varoptions then
  2157. break;
  2158. ps:=nil;
  2159. end;
  2160. if not assigned(ps) then
  2161. exit;
  2162. end;
  2163. { we need to load the'self' parameter of the current routine as the
  2164. 'self' parameter of the called routine if
  2165. 1) we're calling an inherited routine
  2166. 2) we're calling a constructor via type.constructorname and
  2167. type is not a classrefdef (i.e., we're calling a constructor like
  2168. a regular method)
  2169. 3) we're calling any regular (non-class/non-static) method via
  2170. a typenode (the methodpointer is then that typenode, but the
  2171. passed self node must become the current self node)
  2172. In other cases, we either don't have to pass the 'self' parameter of
  2173. the current routine to the called one, or methodpointer will already
  2174. contain it (e.g. because a method was called via "method", in which
  2175. case the parser already passed 'self' as the method pointer, or via
  2176. "self.method") }
  2177. if (cnf_inherited in callnodeflags) or
  2178. ((procdefinition.proctypeoption=potype_constructor) and
  2179. not((methodpointer.resultdef.typ=classrefdef) or
  2180. (cnf_new_call in callnodeflags)) and
  2181. (methodpointer.nodetype=typen) and
  2182. (methodpointer.resultdef.typ=objectdef)) or
  2183. (assigned(methodpointer) and
  2184. (procdefinition.proctypeoption<>potype_constructor) and
  2185. not(po_classmethod in procdefinition.procoptions) and
  2186. not(po_staticmethod in procdefinition.procoptions) and
  2187. (methodpointer.nodetype=typen)) then
  2188. result:=true;
  2189. end;
  2190. procedure tcallnode.maybe_gen_call_self_node;
  2191. begin
  2192. if cnf_call_self_node_done in callnodeflags then
  2193. exit;
  2194. include(callnodeflags,cnf_call_self_node_done);
  2195. if use_caller_self(true) then
  2196. call_self_node:=load_self_node;
  2197. end;
  2198. procedure tcallnode.register_created_object_types;
  2199. function checklive(def: tdef): boolean;
  2200. begin
  2201. if assigned(current_procinfo) and
  2202. not(po_inline in current_procinfo.procdef.procoptions) and
  2203. not wpoinfomanager.symbol_live(current_procinfo.procdef.mangledname) then
  2204. begin
  2205. {$ifdef debug_deadcode}
  2206. writeln(' NOT adding creadion of ',def.typename,' because performed in dead stripped proc: ',current_procinfo.procdef.typename);
  2207. {$endif debug_deadcode}
  2208. result:=false;
  2209. end
  2210. else
  2211. result:=true;
  2212. end;
  2213. var
  2214. crefdef,
  2215. systobjectdef : tdef;
  2216. begin
  2217. { only makes sense for methods }
  2218. if not assigned(methodpointer) then
  2219. exit;
  2220. if (methodpointer.resultdef.typ=classrefdef) then
  2221. begin
  2222. { constructor call via classreference => allocate memory }
  2223. if (procdefinition.proctypeoption=potype_constructor) then
  2224. begin
  2225. { Only a typenode can be passed when it is called with <class of xx>.create }
  2226. if (methodpointer.nodetype=typen) then
  2227. begin
  2228. if checklive(methodpointer.resultdef) then
  2229. { we know the exact class type being created }
  2230. tclassrefdef(methodpointer.resultdef).pointeddef.register_created_object_type
  2231. end
  2232. else
  2233. begin
  2234. { the loadvmtaddrnode is already created in case of classtype.create }
  2235. if (methodpointer.nodetype=loadvmtaddrn) and
  2236. (tloadvmtaddrnode(methodpointer).left.nodetype=typen) then
  2237. begin
  2238. if checklive(methodpointer.resultdef) then
  2239. tclassrefdef(methodpointer.resultdef).pointeddef.register_created_object_type
  2240. end
  2241. else
  2242. begin
  2243. if checklive(methodpointer.resultdef) then
  2244. begin
  2245. { special case: if the classref comes from x.classtype (with classtype,
  2246. being tobject.classtype) then the created instance is x or a descendant
  2247. of x (rather than tobject or a descendant of tobject)
  2248. }
  2249. systobjectdef:=search_system_type('TOBJECT').typedef;
  2250. if (methodpointer.nodetype=calln) and
  2251. { not a procvar call }
  2252. not assigned(right) and
  2253. { procdef is owned by system.tobject }
  2254. (tprocdef(tcallnode(methodpointer).procdefinition).owner.defowner=systobjectdef) and
  2255. { we're calling system.tobject.classtype }
  2256. (tcallnode(methodpointer).symtableprocentry.name='CLASSTYPE') and
  2257. { could again be a classrefdef, but unlikely }
  2258. (tcallnode(methodpointer).methodpointer.resultdef.typ=objectdef) and
  2259. { don't go through this trouble if it was already a tobject }
  2260. (tcallnode(methodpointer).methodpointer.resultdef<>systobjectdef) then
  2261. begin
  2262. { register this object type as classref, so all descendents will also
  2263. be marked as instantiatable (only the pointeddef will actually be
  2264. recorded, so it's no problem that the clasrefdef is only temporary)
  2265. }
  2266. crefdef:=cclassrefdef.create(tcallnode(methodpointer).methodpointer.resultdef);
  2267. { and register it }
  2268. crefdef.register_created_object_type;
  2269. end
  2270. else
  2271. { the created class can be any child class as well -> register classrefdef }
  2272. methodpointer.resultdef.register_created_object_type;
  2273. end;
  2274. end;
  2275. end;
  2276. end
  2277. end
  2278. else
  2279. { Old style object }
  2280. if is_object(methodpointer.resultdef) then
  2281. begin
  2282. { constructor with extended syntax called from new }
  2283. if (cnf_new_call in callnodeflags) then
  2284. begin
  2285. if checklive(methodpointer.resultdef) then
  2286. methodpointer.resultdef.register_created_object_type;
  2287. end
  2288. else
  2289. { normal object call like obj.proc }
  2290. if not(cnf_dispose_call in callnodeflags) and
  2291. not(cnf_inherited in callnodeflags) and
  2292. not(cnf_member_call in callnodeflags) then
  2293. begin
  2294. if (procdefinition.proctypeoption=potype_constructor) then
  2295. begin
  2296. if (methodpointer.nodetype<>typen) and
  2297. checklive(methodpointer.resultdef) then
  2298. methodpointer.resultdef.register_created_object_type;
  2299. end
  2300. end;
  2301. end;
  2302. end;
  2303. function tcallnode.get_expect_loc: tcgloc;
  2304. var
  2305. realresdef: tstoreddef;
  2306. begin
  2307. if not assigned(typedef) then
  2308. realresdef:=tstoreddef(resultdef)
  2309. else
  2310. realresdef:=tstoreddef(typedef);
  2311. if realresdef.is_intregable then
  2312. result:=LOC_REGISTER
  2313. else if (realresdef.typ=floatdef) and
  2314. not(cs_fp_emulation in current_settings.moduleswitches) then
  2315. if use_vectorfpu(realresdef) then
  2316. result:=LOC_MMREGISTER
  2317. else
  2318. result:=LOC_FPUREGISTER
  2319. else
  2320. result:=LOC_REFERENCE
  2321. end;
  2322. function tcallnode.safe_call_self_node: tnode;
  2323. begin
  2324. if not assigned(call_self_node) then
  2325. begin
  2326. CGMessage(parser_e_illegal_expression);
  2327. call_self_node:=cerrornode.create;
  2328. end;
  2329. result:=call_self_node;
  2330. end;
  2331. procedure tcallnode.gen_vmt_entry_load;
  2332. var
  2333. vmt_def: trecorddef;
  2334. begin
  2335. if not assigned(right) and
  2336. (forcedprocname='') and
  2337. (po_virtualmethod in procdefinition.procoptions) and
  2338. not is_objectpascal_helper(tprocdef(procdefinition).struct) and
  2339. assigned(methodpointer) and
  2340. (methodpointer.nodetype<>typen) then
  2341. begin
  2342. vmt_entry:=load_vmt_for_self_node(methodpointer.getcopy);
  2343. { get the right entry in the VMT }
  2344. vmt_entry:=cderefnode.create(vmt_entry);
  2345. typecheckpass(vmt_entry);
  2346. vmt_def:=trecorddef(vmt_entry.resultdef);
  2347. { tobjectdef(tprocdef(procdefinition).struct) can be a parent of the
  2348. methodpointer's resultdef, but the vmtmethodoffset of the method
  2349. in that objectdef is obviously the same as in any child class }
  2350. vmt_entry:=csubscriptnode.create(
  2351. trecordsymtable(vmt_def.symtable).findfieldbyoffset(
  2352. tobjectdef(tprocdef(procdefinition).struct).vmtmethodoffset(tprocdef(procdefinition).extnumber)
  2353. ),
  2354. vmt_entry
  2355. );
  2356. firstpass(vmt_entry);
  2357. end;
  2358. end;
  2359. procedure tcallnode.gen_syscall_para(para: tcallparanode);
  2360. begin
  2361. { unsupported }
  2362. internalerror(2014040101);
  2363. end;
  2364. procedure tcallnode.objc_convert_to_message_send;
  2365. var
  2366. block,
  2367. selftree : tnode;
  2368. statements : tstatementnode;
  2369. field : tfieldvarsym;
  2370. temp : ttempcreatenode;
  2371. selfrestype,
  2372. objcsupertype : tdef;
  2373. srsym : tsym;
  2374. srsymtable : tsymtable;
  2375. msgsendname : string;
  2376. begin
  2377. if not(m_objectivec1 in current_settings.modeswitches) then
  2378. Message(parser_f_modeswitch_objc_required);
  2379. { typecheck pass must already have run on the call node,
  2380. because pass1 calls this method
  2381. }
  2382. { default behaviour: call objc_msgSend and friends;
  2383. 64 bit targets for Mac OS X can override this as they
  2384. can call messages via an indirect function call similar to
  2385. dynamically linked functions, ARM maybe as well (not checked)
  2386. Which variant of objc_msgSend is used depends on the
  2387. result type, and on whether or not it's an inherited call.
  2388. }
  2389. { make sure we don't perform this transformation twice in case
  2390. firstpass would be called multiple times }
  2391. include(callnodeflags,cnf_objc_processed);
  2392. { make sure the methodpointer doesn't get translated into a call
  2393. as well (endless loop) }
  2394. if methodpointer.nodetype=loadvmtaddrn then
  2395. tloadvmtaddrnode(methodpointer).forcall:=true;
  2396. { A) set the appropriate objc_msgSend* variant to call }
  2397. { The AArch64 abi does not require special handling for struct returns }
  2398. {$ifndef aarch64}
  2399. { record returned via implicit pointer }
  2400. if paramanager.ret_in_param(resultdef,procdefinition) then
  2401. begin
  2402. if not(cnf_inherited in callnodeflags) then
  2403. msgsendname:='OBJC_MSGSEND_STRET'
  2404. {$if defined(onlymacosx10_6) or defined(arm) }
  2405. else if (target_info.system in systems_objc_nfabi) then
  2406. msgsendname:='OBJC_MSGSENDSUPER2_STRET'
  2407. {$endif onlymacosx10_6 or arm}
  2408. else
  2409. msgsendname:='OBJC_MSGSENDSUPER_STRET'
  2410. end
  2411. {$ifdef i386}
  2412. { special case for fpu results on i386 for non-inherited calls }
  2413. { TODO: also for x86_64 "extended" results }
  2414. else if (resultdef.typ=floatdef) and
  2415. not(cnf_inherited in callnodeflags) then
  2416. msgsendname:='OBJC_MSGSEND_FPRET'
  2417. {$endif i386}
  2418. { default }
  2419. else
  2420. {$endif aarch64}
  2421. if not(cnf_inherited in callnodeflags) then
  2422. msgsendname:='OBJC_MSGSEND'
  2423. {$if defined(onlymacosx10_6) or defined(arm) or defined(aarch64)}
  2424. else if (target_info.system in systems_objc_nfabi) then
  2425. msgsendname:='OBJC_MSGSENDSUPER2'
  2426. {$endif onlymacosx10_6 or arm}
  2427. else
  2428. msgsendname:='OBJC_MSGSENDSUPER';
  2429. { get the mangled name }
  2430. srsym:=nil;
  2431. if not searchsym_in_named_module('OBJC',msgsendname,srsym,srsymtable) or
  2432. (srsym.typ<>procsym) or
  2433. (tprocsym(srsym).ProcdefList.count<>1) then
  2434. Message1(cg_f_unknown_compilerproc,'objc.'+msgsendname);
  2435. {$ifdef symansistr}
  2436. fforcedprocname:=tprocdef(tprocsym(srsym).ProcdefList[0]).mangledname;
  2437. {$else symansistr}
  2438. fforcedprocname:=stringdup(tprocdef(tprocsym(srsym).ProcdefList[0]).mangledname);
  2439. {$endif symansistr}
  2440. { B) Handle self }
  2441. { 1) in case of sending a message to a superclass, self is a pointer to
  2442. an objc_super record
  2443. }
  2444. if (cnf_inherited in callnodeflags) then
  2445. begin
  2446. block:=internalstatements(statements);
  2447. objcsupertype:=search_named_unit_globaltype('OBJC','OBJC_SUPER',true).typedef;
  2448. if (objcsupertype.typ<>recorddef) then
  2449. internalerror(2009032901);
  2450. { temp for the for the objc_super record }
  2451. temp:=ctempcreatenode.create(objcsupertype,objcsupertype.size,tt_persistent,false);
  2452. addstatement(statements,temp);
  2453. { initialize objc_super record }
  2454. selftree:=safe_call_self_node.getcopy;
  2455. { we can call an inherited class static/method from a regular method
  2456. -> self node must change from instance pointer to vmt pointer)
  2457. }
  2458. if (po_classmethod in procdefinition.procoptions) and
  2459. (selftree.resultdef.typ<>classrefdef) then
  2460. begin
  2461. selftree:=cloadvmtaddrnode.create(selftree);
  2462. { since we're in a class method of the current class, its
  2463. information has already been initialized (and that of all of
  2464. its parent classes too) }
  2465. tloadvmtaddrnode(selftree).forcall:=true;
  2466. typecheckpass(selftree);
  2467. end;
  2468. selfrestype:=selftree.resultdef;
  2469. field:=tfieldvarsym(trecorddef(objcsupertype).symtable.find('RECEIVER'));
  2470. if not assigned(field) then
  2471. internalerror(2009032902);
  2472. { first the destination object/class instance }
  2473. addstatement(statements,
  2474. cassignmentnode.create(
  2475. csubscriptnode.create(field,ctemprefnode.create(temp)),
  2476. selftree
  2477. )
  2478. );
  2479. { and secondly, the class type in which the selector must be looked
  2480. up (the parent class in case of an instance method, the parent's
  2481. metaclass in case of a class method) }
  2482. field:=tfieldvarsym(trecorddef(objcsupertype).symtable.find('_CLASS'));
  2483. if not assigned(field) then
  2484. internalerror(2009032903);
  2485. addstatement(statements,
  2486. cassignmentnode.create(
  2487. csubscriptnode.create(field,ctemprefnode.create(temp)),
  2488. objcsuperclassnode(selftree.resultdef)
  2489. )
  2490. );
  2491. { result of this block is the address of this temp }
  2492. addstatement(statements,ctypeconvnode.create_internal(
  2493. caddrnode.create_internal(ctemprefnode.create(temp)),selfrestype)
  2494. );
  2495. { replace the method pointer with the address of this temp }
  2496. methodpointer.free;
  2497. methodpointer:=block;
  2498. typecheckpass(block);
  2499. end
  2500. else
  2501. { 2) regular call (not inherited) }
  2502. begin
  2503. { a) If we're calling a class method, use a class ref. }
  2504. if (po_classmethod in procdefinition.procoptions) and
  2505. ((methodpointer.nodetype=typen) or
  2506. (methodpointer.resultdef.typ<>classrefdef)) then
  2507. begin
  2508. methodpointer:=cloadvmtaddrnode.create(methodpointer);
  2509. { no need to obtain the class ref by calling class(), sending
  2510. this message will initialize it if necessary }
  2511. tloadvmtaddrnode(methodpointer).forcall:=true;
  2512. firstpass(methodpointer);
  2513. end;
  2514. end;
  2515. end;
  2516. function tcallnode.gen_vmt_tree:tnode;
  2517. var
  2518. vmttree : tnode;
  2519. begin
  2520. vmttree:=nil;
  2521. if not(procdefinition.proctypeoption in [potype_constructor,potype_destructor]) then
  2522. internalerror(200305051);
  2523. { When methodpointer was a callnode we must load it first into a
  2524. temp to prevent the processing callnode twice }
  2525. if (methodpointer.nodetype=calln) then
  2526. internalerror(200405122);
  2527. { Handle classes and legacy objects separate to make it
  2528. more maintainable }
  2529. if (methodpointer.resultdef.typ=classrefdef) then
  2530. begin
  2531. if not is_class(tclassrefdef(methodpointer.resultdef).pointeddef) then
  2532. internalerror(200501041);
  2533. { constructor call via classreference => allocate memory }
  2534. if (procdefinition.proctypeoption=potype_constructor) then
  2535. begin
  2536. vmttree:=cpointerconstnode.create(1,voidpointertype);
  2537. end
  2538. else { <class of xx>.destroy is not valid }
  2539. InternalError(2014020601);
  2540. end
  2541. else
  2542. { Class style objects }
  2543. if is_class(methodpointer.resultdef) then
  2544. begin
  2545. { inherited call, no create/destroy }
  2546. if (cnf_inherited in callnodeflags) then
  2547. vmttree:=cpointerconstnode.create(0,voidpointertype)
  2548. else
  2549. { do not create/destroy when called from member function
  2550. without specifying self explicit }
  2551. if (cnf_member_call in callnodeflags) then
  2552. begin
  2553. { destructor (in the same class, since cnf_member_call):
  2554. if not called from a destructor then
  2555. call beforedestruction and release instance, vmt=1
  2556. else
  2557. don't release instance, vmt=0
  2558. constructor (in the same class, since cnf_member_call):
  2559. if called from a constructor then
  2560. don't call afterconstruction, vmt=0
  2561. else
  2562. call afterconstrution but not NewInstance, vmt=-1 }
  2563. if (procdefinition.proctypeoption=potype_destructor) then
  2564. if (current_procinfo.procdef.proctypeoption<>potype_constructor) then
  2565. vmttree:=cpointerconstnode.create(1,voidpointertype)
  2566. else
  2567. vmttree:=cpointerconstnode.create(0,voidpointertype)
  2568. else if (current_procinfo.procdef.proctypeoption=potype_constructor) and
  2569. (procdefinition.proctypeoption=potype_constructor) then
  2570. vmttree:=cpointerconstnode.create(0,voidpointertype)
  2571. else
  2572. vmttree:=cpointerconstnode.create(TConstPtrUInt(-1),voidpointertype);
  2573. end
  2574. else
  2575. { normal call to method like cl1.proc }
  2576. begin
  2577. { destructor:
  2578. if not(called from exception block in constructor) or
  2579. (called from afterconstruction)
  2580. call beforedestruction and release instance, vmt=1
  2581. else
  2582. don't call beforedestruction and release instance, vmt=-1
  2583. constructor:
  2584. if called from a constructor in the same class using self.create then
  2585. don't call afterconstruction, vmt=0
  2586. else
  2587. call afterconstruction, vmt=1 }
  2588. if (procdefinition.proctypeoption=potype_destructor) then
  2589. if (cnf_create_failed in callnodeflags) and
  2590. is_class(methodpointer.resultdef) then
  2591. vmttree:=call_vmt_node.getcopy
  2592. else if not(cnf_create_failed in callnodeflags) then
  2593. vmttree:=cpointerconstnode.create(1,voidpointertype)
  2594. else
  2595. vmttree:=cpointerconstnode.create(TConstPtrUInt(-1),voidpointertype)
  2596. else
  2597. begin
  2598. if (current_procinfo.procdef.proctypeoption=potype_constructor) and
  2599. (procdefinition.proctypeoption=potype_constructor) and
  2600. (methodpointer.nodetype=loadn) and
  2601. (loadnf_is_self in tloadnode(methodpointer).loadnodeflags) then
  2602. vmttree:=cpointerconstnode.create(0,voidpointertype)
  2603. else
  2604. vmttree:=cpointerconstnode.create(TConstPtrUInt(-1),voidpointertype);
  2605. end;
  2606. end;
  2607. end
  2608. else
  2609. { Old style object }
  2610. begin
  2611. { constructor with extended syntax called from new }
  2612. if (cnf_new_call in callnodeflags) then
  2613. vmttree:=cloadvmtaddrnode.create(ctypenode.create(methodpointer.resultdef))
  2614. else
  2615. { destructor with extended syntax called from dispose }
  2616. { value -1 is what fpc_help_constructor() changes VMT to when it allocates memory }
  2617. if (cnf_dispose_call in callnodeflags) then
  2618. vmttree:=cpointerconstnode.create(TConstPtrUInt(-1),voidpointertype)
  2619. else
  2620. { destructor called from exception block in constructor }
  2621. if (cnf_create_failed in callnodeflags) then
  2622. vmttree:=ctypeconvnode.create_internal(call_vmt_node.getcopy,voidpointertype)
  2623. else
  2624. { inherited call, no create/destroy }
  2625. if (cnf_inherited in callnodeflags) then
  2626. vmttree:=cpointerconstnode.create(0,voidpointertype)
  2627. else
  2628. { do not create/destroy when called from member function
  2629. without specifying self explicit }
  2630. if (cnf_member_call in callnodeflags) then
  2631. begin
  2632. { destructor: don't release instance, vmt=0
  2633. constructor: don't initialize instance, vmt=0 }
  2634. vmttree:=cpointerconstnode.create(0,voidpointertype)
  2635. end
  2636. else
  2637. { normal object call like obj.proc }
  2638. begin
  2639. { destructor: direct call, no dispose, vmt=0
  2640. constructor: initialize object, load vmt }
  2641. if (procdefinition.proctypeoption=potype_constructor) then
  2642. begin
  2643. { old styled inherited call? }
  2644. if (methodpointer.nodetype=typen) then
  2645. vmttree:=cpointerconstnode.create(0,voidpointertype)
  2646. else
  2647. vmttree:=cloadvmtaddrnode.create(ctypenode.create(methodpointer.resultdef))
  2648. end
  2649. else
  2650. vmttree:=cpointerconstnode.create(0,voidpointertype);
  2651. end;
  2652. end;
  2653. result:=vmttree;
  2654. end;
  2655. function tcallnode.gen_block_context: tnode;
  2656. begin
  2657. { the self parameter of a block invocation is that address of the
  2658. block literal (which is what right contains) }
  2659. result:=right.getcopy;
  2660. end;
  2661. function check_funcret_used_as_para(var n: tnode; arg: pointer): foreachnoderesult;
  2662. var
  2663. destsym : tsym absolute arg;
  2664. begin
  2665. result := fen_false;
  2666. if (n.nodetype=loadn) and
  2667. (tloadnode(n).symtableentry = destsym) then
  2668. result := fen_norecurse_true;
  2669. end;
  2670. function check_funcret_temp_used_as_para(var n: tnode; arg: pointer): foreachnoderesult;
  2671. var
  2672. tempinfo : ptempinfo absolute arg;
  2673. begin
  2674. result := fen_false;
  2675. if (n.nodetype=temprefn) and
  2676. (ttemprefnode(n).tempinfo = tempinfo) then
  2677. result := fen_norecurse_true;
  2678. end;
  2679. function tcallnode.funcret_can_be_reused:boolean;
  2680. var
  2681. realassignmenttarget: tnode;
  2682. alignment: longint;
  2683. begin
  2684. result:=false;
  2685. { we are processing an assignment node? }
  2686. if not(assigned(aktassignmentnode) and
  2687. (aktassignmentnode.right=self) and
  2688. (aktassignmentnode.left.resultdef=resultdef)) then
  2689. exit;
  2690. { destination must be able to be passed as var parameter }
  2691. if not valid_for_var(aktassignmentnode.left,false) then
  2692. exit;
  2693. { destination must be a simple load so it doesn't need a temp when
  2694. it is evaluated }
  2695. if not is_simple_para_load(aktassignmentnode.left,false) then
  2696. exit;
  2697. { remove possible typecasts }
  2698. realassignmenttarget:=actualtargetnode(@aktassignmentnode.left)^;
  2699. { when the result is returned by value (instead of by writing it to the
  2700. address passed in a hidden parameter), aktassignmentnode.left will
  2701. only be changed once the function has returned and we don't have to
  2702. perform any checks regarding whether it may alias with one of the
  2703. parameters -- unless this is an inline function, in which case
  2704. writes to the function result will directly change it and we do have
  2705. to check for potential aliasing }
  2706. if not paramanager.ret_in_param(resultdef,procdefinition) then
  2707. begin
  2708. if not(cnf_do_inline in callnodeflags) then
  2709. begin
  2710. result:=true;
  2711. exit;
  2712. end
  2713. else
  2714. begin
  2715. { don't replace the function result if we are inlining and if
  2716. the destination is complex, this could lead to lengthy
  2717. code in case the function result is used often and it is
  2718. assigned e.g. to a threadvar }
  2719. if node_complexity(aktassignmentnode.left)>1 then
  2720. exit;
  2721. end;
  2722. end;
  2723. { if the result is the same as the self parameter (in case of objects),
  2724. we can't optimise. We have to check this explicitly becaise
  2725. hidden parameters such as self have not yet been inserted at this
  2726. point
  2727. }
  2728. if assigned(methodpointer) and
  2729. realassignmenttarget.isequal(actualtargetnode(@methodpointer)^) then
  2730. exit;
  2731. { when we substitute a function result inside an inlined function,
  2732. we may take the address of this function result. Therefore the
  2733. substituted function result may not be in a register, as we cannot
  2734. take its address in that case }
  2735. if (realassignmenttarget.nodetype=temprefn) and
  2736. not(ti_addr_taken in ttemprefnode(realassignmenttarget).tempflags) and
  2737. not(ti_may_be_in_reg in ttemprefnode(realassignmenttarget).tempflags) then
  2738. begin
  2739. result:=not foreachnodestatic(left,@check_funcret_temp_used_as_para,ttemprefnode(realassignmenttarget).tempinfo);
  2740. exit;
  2741. end;
  2742. if (realassignmenttarget.nodetype=loadn) and
  2743. { nested procedures may access the current procedure's locals }
  2744. (procdefinition.parast.symtablelevel=normal_function_level) and
  2745. { must be a local variable, a value para or a hidden function result }
  2746. { parameter (which can be passed by address, but in that case it got }
  2747. { through these same checks at the caller side and is thus safe ) }
  2748. { other option: we're calling a compilerproc, because those don't
  2749. rely on global state
  2750. }
  2751. ((po_compilerproc in procdefinition.procoptions) or
  2752. (
  2753. (
  2754. (tloadnode(realassignmenttarget).symtableentry.typ=localvarsym) or
  2755. (
  2756. (tloadnode(realassignmenttarget).symtableentry.typ=paravarsym) and
  2757. ((tparavarsym(tloadnode(realassignmenttarget).symtableentry).varspez = vs_value) or
  2758. (vo_is_funcret in tparavarsym(tloadnode(realassignmenttarget).symtableentry).varoptions))
  2759. )
  2760. ) and
  2761. { the address may not have been taken of the variable/parameter, because }
  2762. { otherwise it's possible that the called function can access it via a }
  2763. { global variable or other stored state }
  2764. (
  2765. not(tabstractvarsym(tloadnode(realassignmenttarget).symtableentry).addr_taken) and
  2766. (tabstractvarsym(tloadnode(realassignmenttarget).symtableentry).varregable in [vr_none,vr_addr])
  2767. )
  2768. )
  2769. ) then
  2770. begin
  2771. { If the funcret is also used as a parameter we can't optimize because the funcret
  2772. and the parameter will point to the same address. That means that a change of the result variable
  2773. will result also in a change of the parameter value }
  2774. result:=not foreachnodestatic(left,@check_funcret_used_as_para,tloadnode(realassignmenttarget).symtableentry);
  2775. { ensure that it is aligned using the default alignment }
  2776. alignment:=tabstractvarsym(tloadnode(realassignmenttarget).symtableentry).vardef.alignment;
  2777. if (used_align(alignment,target_info.alignment.localalignmin,target_info.alignment.localalignmax)<>
  2778. used_align(alignment,current_settings.alignment.localalignmin,current_settings.alignment.localalignmax)) then
  2779. result:=false;
  2780. exit;
  2781. end;
  2782. end;
  2783. procedure tcallnode.maybe_create_funcret_node;
  2784. var
  2785. temp : ttempcreatenode;
  2786. begin
  2787. if procdefinition.proctypeoption=potype_constructor then
  2788. exit;
  2789. { For the function result we need to create a temp node for:
  2790. - Inlined functions
  2791. - Types requiring initialization/finalization
  2792. - Types passed in parameters }
  2793. if not is_void(resultdef) and
  2794. not assigned(funcretnode) and
  2795. (
  2796. (cnf_do_inline in callnodeflags) or
  2797. is_managed_type(resultdef) or
  2798. paramanager.ret_in_param(resultdef,procdefinition)
  2799. ) then
  2800. begin
  2801. { Optimize calls like x:=f() where we can use x directly as
  2802. result instead of using a temp. Condition is that x cannot be accessed from f().
  2803. This implies that x is a local variable or value parameter of the current block
  2804. and its address is not passed to f. One problem: what if someone takes the
  2805. address of x, puts it in a pointer variable/field and then accesses it that way
  2806. from within the function? This is solved (in a conservative way) using the
  2807. ti_addr_taken flag.
  2808. When the result is not not passed in a parameter there are no problem because
  2809. then it means only reference counted types (eg. ansistrings) that need a decr
  2810. of the refcount before being assigned. This is all done after the call so there
  2811. is no issue with exceptions and possible use of the old value in the called
  2812. function }
  2813. if funcret_can_be_reused then
  2814. begin
  2815. funcretnode:=aktassignmentnode.left.getcopy;
  2816. include(funcretnode.flags,nf_is_funcret);
  2817. { notify the assignment node that the assignment can be removed }
  2818. include(aktassignmentnode.flags,nf_assign_done_in_right);
  2819. end
  2820. else
  2821. begin
  2822. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,
  2823. (cnf_do_inline in callnodeflags) and
  2824. not(tabstractvarsym(tprocdef(procdefinition).funcretsym).varregable in [vr_none,vr_addr]));
  2825. include(temp.flags,nf_is_funcret);
  2826. { if a managed type is returned by reference, assigning something
  2827. to the result on the caller side will take care of decreasing
  2828. the reference count }
  2829. if paramanager.ret_in_param(resultdef,procdefinition) then
  2830. temp.includetempflag(ti_nofini);
  2831. add_init_statement(temp);
  2832. { When the function result is not used in an inlined function
  2833. we need to delete the temp. This can currently only be done by
  2834. a tempdeletenode and not after converting it to a normal temp }
  2835. if not(cnf_return_value_used in callnodeflags) and
  2836. (cnf_do_inline in callnodeflags) then
  2837. add_done_statement(ctempdeletenode.create(temp))
  2838. else
  2839. add_done_statement(ctempdeletenode.create_normal_temp(temp));
  2840. funcretnode:=ctemprefnode.create(temp);
  2841. include(funcretnode.flags,nf_is_funcret);
  2842. end;
  2843. end;
  2844. end;
  2845. procedure tcallnode.gen_hidden_parameters;
  2846. var
  2847. para : tcallparanode;
  2848. begin
  2849. para:=tcallparanode(left);
  2850. while assigned(para) do
  2851. begin
  2852. { The processing of high() and typeinfo() is already
  2853. done in the typecheckpass. We only need to process the
  2854. nodes that still have a nothingn }
  2855. if (vo_is_hidden_para in para.parasym.varoptions) and
  2856. (para.left.nodetype=nothingn) then
  2857. begin
  2858. { remove dummy nothingn }
  2859. para.left.free;
  2860. para.left:=nil;
  2861. { generate the corresponding nodes for the hidden parameter type }
  2862. if (vo_is_funcret in para.parasym.varoptions) then
  2863. begin
  2864. if not assigned(funcretnode) then
  2865. internalerror(200709083);
  2866. { if funcretnode is a temprefnode, we have to keep it intact
  2867. if it may have been created in maybe_create_funcret_node(),
  2868. because then it will also be destroyed by a
  2869. ctempdeletenode.create_normal_temp() in the cleanup code
  2870. for this call code. In that case we have to copy this
  2871. ttemprefnode after the tempdeletenode to reset its
  2872. tempinfo^.hookoncopy. This is done by copying funcretnode
  2873. in tcallnode.getcopy(), but for that to work we can't reset
  2874. funcretnode to nil here. }
  2875. if (funcretnode.nodetype<>temprefn) or
  2876. (not(cnf_return_value_used in callnodeflags) and
  2877. (cnf_do_inline in callnodeflags)) then
  2878. begin
  2879. para.left:=funcretnode;
  2880. funcretnode:=nil;
  2881. end
  2882. else
  2883. para.left:=funcretnode.getcopy;
  2884. end
  2885. else
  2886. if vo_is_self in para.parasym.varoptions then
  2887. begin
  2888. if assigned(right) then
  2889. para.left:=gen_procvar_context_tree_self
  2890. else
  2891. para.left:=gen_self_tree;
  2892. { make sure that e.g. the self pointer of an advanced
  2893. record does not become a regvar, because it's a vs_var
  2894. parameter }
  2895. if paramanager.push_addr_param(para.parasym.varspez,para.parasym.vardef,
  2896. procdefinition.proccalloption) then
  2897. make_not_regable(para.left,[ra_addr_regable]);
  2898. end
  2899. else
  2900. if vo_is_vmt in para.parasym.varoptions then
  2901. begin
  2902. para.left:=gen_vmt_tree;
  2903. end
  2904. else
  2905. if vo_is_syscall_lib in para.parasym.varoptions then
  2906. gen_syscall_para(para)
  2907. else
  2908. if vo_is_range_check in para.parasym.varoptions then
  2909. begin
  2910. para.left:=cordconstnode.create(Ord(cs_check_range in current_settings.localswitches),pasbool8type,false);
  2911. end
  2912. else
  2913. if vo_is_overflow_check in para.parasym.varoptions then
  2914. begin
  2915. para.left:=cordconstnode.create(Ord(cs_check_overflow in current_settings.localswitches),pasbool8type,false);
  2916. end
  2917. else
  2918. if vo_is_msgsel in para.parasym.varoptions then
  2919. begin
  2920. para.left:=cobjcselectornode.create(cstringconstnode.createstr(tprocdef(procdefinition).messageinf.str^));
  2921. end;
  2922. end;
  2923. if not assigned(para.left) then
  2924. internalerror(200709084);
  2925. para:=tcallparanode(para.right);
  2926. end;
  2927. end;
  2928. procedure tcallnode.verifyabstract(sym:TObject;arg:pointer);
  2929. var
  2930. pd : tprocdef;
  2931. i : longint;
  2932. j : integer;
  2933. hs : string;
  2934. begin
  2935. if (tsym(sym).typ<>procsym) then
  2936. exit;
  2937. for i:=0 to tprocsym(sym).ProcdefList.Count-1 do
  2938. begin
  2939. pd:=tprocdef(tprocsym(sym).ProcdefList[i]);
  2940. hs:=pd.procsym.name+pd.typename_paras([]);
  2941. j:=AbstractMethodsList.FindIndexOf(hs);
  2942. if j<>-1 then
  2943. AbstractMethodsList[j]:=pd
  2944. else
  2945. AbstractMethodsList.Add(hs,pd);
  2946. end;
  2947. end;
  2948. procedure tcallnode.verifyabstractcalls;
  2949. var
  2950. objectdf : tobjectdef;
  2951. parents : tlinkedlist;
  2952. objectinfo : tobjectinfoitem;
  2953. pd : tprocdef;
  2954. i : integer;
  2955. begin
  2956. objectdf := nil;
  2957. { verify if trying to create an instance of a class which contains
  2958. non-implemented abstract methods }
  2959. { first verify this class type, no class than exit }
  2960. { also, this checking can only be done if the constructor is directly
  2961. called, indirect constructor calls cannot be checked.
  2962. }
  2963. if assigned(methodpointer) and
  2964. not((methodpointer.nodetype=loadn) and
  2965. (loadnf_is_self in tloadnode(methodpointer).loadnodeflags)) then
  2966. begin
  2967. if (methodpointer.resultdef.typ = objectdef) then
  2968. objectdf:=tobjectdef(methodpointer.resultdef)
  2969. else
  2970. if (methodpointer.resultdef.typ = classrefdef) and
  2971. (tclassrefdef(methodpointer.resultdef).pointeddef.typ = objectdef) and
  2972. (methodpointer.nodetype in [typen,loadvmtaddrn]) then
  2973. objectdf:=tobjectdef(tclassrefdef(methodpointer.resultdef).pointeddef);
  2974. end;
  2975. if not assigned(objectdf) then
  2976. exit;
  2977. { quick exit if nothing to check }
  2978. if objectdf.abstractcnt = 0 then
  2979. exit;
  2980. parents := tlinkedlist.create;
  2981. AbstractMethodsList := TFPHashList.create;
  2982. { insert all parents in this class : the first item in the
  2983. list will be the base parent of the class .
  2984. }
  2985. while assigned(objectdf) do
  2986. begin
  2987. objectinfo:=tobjectinfoitem.create(objectdf);
  2988. parents.insert(objectinfo);
  2989. objectdf := objectdf.childof;
  2990. end;
  2991. { now all parents are in the correct order
  2992. insert all abstract methods in the list, and remove
  2993. those which are overridden by parent classes.
  2994. }
  2995. objectinfo:=tobjectinfoitem(parents.first);
  2996. while assigned(objectinfo) do
  2997. begin
  2998. objectdf := objectinfo.objinfo;
  2999. if assigned(objectdf.symtable) then
  3000. objectdf.symtable.SymList.ForEachCall(@verifyabstract,nil);
  3001. objectinfo:=tobjectinfoitem(objectinfo.next);
  3002. end;
  3003. if assigned(parents) then
  3004. parents.free;
  3005. { Finally give out a warning for each abstract method still in the list }
  3006. for i:=0 to AbstractMethodsList.Count-1 do
  3007. begin
  3008. pd:=tprocdef(AbstractMethodsList[i]);
  3009. if po_abstractmethod in pd.procoptions then
  3010. begin
  3011. Message2(type_w_instance_with_abstract,objectdf.objrealname^,pd.procsym.RealName);
  3012. MessagePos1(pd.fileinfo,sym_h_abstract_method_list,pd.fullprocname(true));
  3013. end;
  3014. end;
  3015. if assigned(AbstractMethodsList) then
  3016. AbstractMethodsList.Free;
  3017. end;
  3018. procedure tcallnode.convert_carg_array_of_const;
  3019. var
  3020. hp : tarrayconstructornode;
  3021. oldleft : tcallparanode;
  3022. begin
  3023. oldleft:=tcallparanode(left);
  3024. if oldleft.left.nodetype<>arrayconstructorn then
  3025. begin
  3026. CGMessage1(type_e_wrong_type_in_array_constructor,oldleft.left.resultdef.typename);
  3027. exit;
  3028. end;
  3029. include(callnodeflags,cnf_uses_varargs);
  3030. { Get arrayconstructor node and insert typeconvs }
  3031. hp:=tarrayconstructornode(oldleft.left);
  3032. { Add c args parameters }
  3033. { It could be an empty set }
  3034. if assigned(hp) and
  3035. assigned(hp.left) then
  3036. begin
  3037. while assigned(hp) do
  3038. begin
  3039. left:=ccallparanode.create(hp.left,left);
  3040. { set callparanode resultdef and flags }
  3041. left.resultdef:=hp.left.resultdef;
  3042. include(tcallparanode(left).callparaflags,cpf_varargs_para);
  3043. hp.left:=nil;
  3044. hp:=tarrayconstructornode(hp.right);
  3045. end;
  3046. end;
  3047. { Remove value of old array of const parameter, but keep it
  3048. in the list because it is required for bind_parasym.
  3049. Generate a nothign to keep callparanoed.left valid }
  3050. oldleft.left.free;
  3051. oldleft.left:=cnothingnode.create;
  3052. end;
  3053. procedure tcallnode.bind_parasym;
  3054. type
  3055. pcallparanode = ^tcallparanode;
  3056. var
  3057. i : integer;
  3058. pt : tcallparanode;
  3059. oldppt : pcallparanode;
  3060. varargspara,
  3061. currpara : tparavarsym;
  3062. hiddentree : tnode;
  3063. paradef : tdef;
  3064. begin
  3065. pt:=tcallparanode(left);
  3066. oldppt:=pcallparanode(@left);
  3067. { flag all callparanodes that belong to the varargs }
  3068. i:=paralength;
  3069. while (i>procdefinition.maxparacount) do
  3070. begin
  3071. include(pt.callparaflags,cpf_varargs_para);
  3072. oldppt:=pcallparanode(@pt.right);
  3073. pt:=tcallparanode(pt.right);
  3074. dec(i);
  3075. end;
  3076. { skip varargs that are inserted by array of const }
  3077. while assigned(pt) and
  3078. (cpf_varargs_para in pt.callparaflags) do
  3079. pt:=tcallparanode(pt.right);
  3080. { process normal parameters and insert hidden parameter nodes, the content
  3081. of the hidden parameters will be updated in pass1 }
  3082. for i:=procdefinition.paras.count-1 downto 0 do
  3083. begin
  3084. currpara:=tparavarsym(procdefinition.paras[i]);
  3085. if vo_is_hidden_para in currpara.varoptions then
  3086. begin
  3087. { Here we handle only the parameters that depend on
  3088. the types of the previous parameter. The typeconversion
  3089. can change the type in the next step. For example passing
  3090. an array can be change to a pointer and a deref.
  3091. We also handle the generation of parentfp parameters, as they
  3092. must all be created before pass_1 on targets that use explicit
  3093. parentfp structs (rather than the frame pointer). The reason
  3094. is that the necessary initialisation code for the these
  3095. structures is attached to the procedure's nodetree after
  3096. the resulttype pass.
  3097. }
  3098. if vo_is_high_para in currpara.varoptions then
  3099. begin
  3100. if not assigned(pt) or (i=0) then
  3101. internalerror(200304081);
  3102. { we need the information of the previous parameter }
  3103. paradef:=tparavarsym(procdefinition.paras[i-1]).vardef;
  3104. hiddentree:=gen_high_tree(pt.left,paradef);
  3105. { for open array of managed type, a copy of high parameter is
  3106. necessary to properly initialize before the call }
  3107. if is_open_array(paradef) and
  3108. (tparavarsym(procdefinition.paras[i-1]).varspez=vs_out) and
  3109. is_managed_type(tarraydef(paradef).elementdef) then
  3110. begin
  3111. typecheckpass(hiddentree);
  3112. {this eliminates double call to fpc_dynarray_high, if any}
  3113. maybe_load_in_temp(hiddentree);
  3114. oldppt^.third:=hiddentree.getcopy;
  3115. end;
  3116. end
  3117. else
  3118. if vo_is_typinfo_para in currpara.varoptions then
  3119. begin
  3120. if not assigned(pt) or (i=0) then
  3121. internalerror(200304082);
  3122. hiddentree:=caddrnode.create_internal(
  3123. crttinode.create(Tstoreddef(pt.resultdef),fullrtti,rdt_normal)
  3124. );
  3125. end
  3126. else if vo_is_parentfp in currpara.varoptions then
  3127. begin
  3128. if assigned(right) and (right.resultdef.typ=procvardef) and
  3129. not tabstractprocdef(right.resultdef).is_addressonly then
  3130. maybe_load_in_temp(right);
  3131. if not assigned(right) then
  3132. begin
  3133. if assigned(procdefinition.owner.defowner) then
  3134. hiddentree:=cloadparentfpnode.create(tprocdef(procdefinition.owner.defowner),lpf_forpara)
  3135. { exceptfilters called from main level are not owned }
  3136. else if procdefinition.proctypeoption=potype_exceptfilter then
  3137. hiddentree:=cloadparentfpnode.create(current_procinfo.procdef,lpf_forpara)
  3138. else
  3139. internalerror(200309287);
  3140. end
  3141. else if not(po_is_block in procdefinition.procoptions) then
  3142. hiddentree:=gen_procvar_context_tree_parentfp
  3143. else
  3144. hiddentree:=gen_block_context
  3145. end
  3146. else
  3147. hiddentree:=cnothingnode.create;
  3148. pt:=ccallparanode.create(hiddentree,oldppt^);
  3149. oldppt^:=pt;
  3150. end;
  3151. if not assigned(pt) then
  3152. internalerror(200310052);
  3153. pt.parasym:=currpara;
  3154. oldppt:=pcallparanode(@pt.right);
  3155. pt:=tcallparanode(pt.right);
  3156. end;
  3157. { Create parasyms for varargs, first count the number of varargs paras,
  3158. then insert the parameters with numbering in reverse order. The SortParas
  3159. will set the correct order at the end}
  3160. pt:=tcallparanode(left);
  3161. i:=0;
  3162. while assigned(pt) do
  3163. begin
  3164. if cpf_varargs_para in pt.callparaflags then
  3165. inc(i);
  3166. pt:=tcallparanode(pt.right);
  3167. end;
  3168. if (i>0) then
  3169. begin
  3170. include(current_procinfo.flags,pi_calls_c_varargs);
  3171. varargsparas:=tvarargsparalist.create;
  3172. pt:=tcallparanode(left);
  3173. while assigned(pt) do
  3174. begin
  3175. if cpf_varargs_para in pt.callparaflags then
  3176. begin
  3177. varargspara:=cparavarsym.create('va'+tostr(i),i,vs_value,pt.resultdef,[]);
  3178. dec(i);
  3179. { varargspara is left-right, use insert
  3180. instead of concat }
  3181. varargsparas.add(varargspara);
  3182. pt.parasym:=varargspara;
  3183. end;
  3184. pt:=tcallparanode(pt.right);
  3185. end;
  3186. varargsparas.sortparas;
  3187. end;
  3188. end;
  3189. function tcallnode.pass_typecheck:tnode;
  3190. var
  3191. candidates : tcallcandidates;
  3192. oldcallnode : tcallnode;
  3193. hpt : tnode;
  3194. pt : tcallparanode;
  3195. lastpara : longint;
  3196. paraidx,
  3197. cand_cnt : integer;
  3198. i : longint;
  3199. ignorevisibility,
  3200. is_const : boolean;
  3201. statements : tstatementnode;
  3202. converted_result_data : ttempcreatenode;
  3203. calltype: tdispcalltype;
  3204. begin
  3205. result:=nil;
  3206. candidates:=nil;
  3207. oldcallnode:=aktcallnode;
  3208. aktcallnode:=self;
  3209. try
  3210. { determine length of parameter list }
  3211. pt:=tcallparanode(left);
  3212. paralength:=0;
  3213. while assigned(pt) do
  3214. begin
  3215. inc(paralength);
  3216. pt:=tcallparanode(pt.right);
  3217. end;
  3218. { determine the type of the parameters }
  3219. if assigned(left) then
  3220. begin
  3221. tcallparanode(left).get_paratype;
  3222. if codegenerror then
  3223. exit;
  3224. end;
  3225. if assigned(methodpointer) then
  3226. typecheckpass(methodpointer);
  3227. { procedure variable ? }
  3228. if assigned(right) then
  3229. begin
  3230. set_varstate(right,vs_read,[vsf_must_be_valid]);
  3231. typecheckpass(right);
  3232. if codegenerror then
  3233. exit;
  3234. procdefinition:=tabstractprocdef(right.resultdef);
  3235. { Compare parameters from right to left }
  3236. paraidx:=procdefinition.Paras.count-1;
  3237. { Skip default parameters }
  3238. if not(po_varargs in procdefinition.procoptions) then
  3239. begin
  3240. { ignore hidden parameters }
  3241. while (paraidx>=0) and (vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) do
  3242. dec(paraidx);
  3243. for i:=1 to procdefinition.maxparacount-paralength do
  3244. begin
  3245. if paraidx<0 then
  3246. internalerror(200402265);
  3247. if not assigned(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym) then
  3248. begin
  3249. CGMessage1(parser_e_wrong_parameter_size,'<Procedure Variable>');
  3250. exit;
  3251. end;
  3252. dec(paraidx);
  3253. end;
  3254. end;
  3255. while (paraidx>=0) and (vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) do
  3256. dec(paraidx);
  3257. pt:=tcallparanode(left);
  3258. lastpara:=paralength;
  3259. while (paraidx>=0) and assigned(pt) do
  3260. begin
  3261. { only goto next para if we're out of the varargs }
  3262. if not(po_varargs in procdefinition.procoptions) or
  3263. (lastpara<=procdefinition.maxparacount) then
  3264. begin
  3265. repeat
  3266. dec(paraidx);
  3267. until (paraidx<0) or not(vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions);
  3268. end;
  3269. pt:=tcallparanode(pt.right);
  3270. dec(lastpara);
  3271. end;
  3272. if assigned(pt) or
  3273. ((paraidx>=0) and
  3274. not assigned(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym)) then
  3275. begin
  3276. if assigned(pt) then
  3277. current_filepos:=pt.fileinfo;
  3278. CGMessage1(parser_e_wrong_parameter_size,'<Procedure Variable>');
  3279. exit;
  3280. end;
  3281. end
  3282. else
  3283. { not a procedure variable }
  3284. begin
  3285. { do we know the procedure to call ? }
  3286. if not(assigned(procdefinition)) then
  3287. begin
  3288. { according to bug reports 32539 and 20551, real variant of sqr/abs should be used when they are called for variants to be
  3289. delphi compatible, this is in contrast to normal overloading behaviour, so fix this by a terrible hack to be compatible }
  3290. if assigned(left) and assigned(tcallparanode(left).left) and
  3291. (tcallparanode(left).left.resultdef.typ=variantdef) and assigned(symtableproc.name) and (symtableproc.name^='SYSTEM') then
  3292. begin
  3293. if symtableprocentry.Name='SQR' then
  3294. begin
  3295. result:=cinlinenode.createintern(in_sqr_real,false,tcallparanode(left).left.getcopy);
  3296. exit;
  3297. end;
  3298. if symtableprocentry.Name='ABS' then
  3299. begin
  3300. result:=cinlinenode.createintern(in_abs_real,false,tcallparanode(left).left.getcopy);
  3301. exit;
  3302. end;
  3303. end;
  3304. { ignore possible private for properties or in delphi mode for anon. inherited (FK) }
  3305. ignorevisibility:=(nf_isproperty in flags) or
  3306. ((m_delphi in current_settings.modeswitches) and (cnf_anon_inherited 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_h_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.