ncal.pas 217 KB

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