ncal.pas 218 KB

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