ncal.pas 218 KB

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