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