2
0

ncal.pas 217 KB

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