ncal.pas 213 KB

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