ncal.pas 240 KB

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