ncal.pas 217 KB

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