ncal.pas 215 KB

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