ncal.pas 242 KB

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