ncal.pas 243 KB

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