ncal.pas 238 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655
  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(callinitblock) then
  1773. begin
  1774. WriteLn(T, PrintNodeIndention, '<callinitblock>');
  1775. PrintNodeIndent;
  1776. XMLPrintNode(T, callinitblock);
  1777. PrintNodeUnindent;
  1778. WriteLn(T, PrintNodeIndention, '</callinitblock>');
  1779. end;
  1780. if assigned(callcleanupblock) then
  1781. begin
  1782. WriteLn(T, PrintNodeIndention, '<callcleanupblock>');
  1783. PrintNodeIndent;
  1784. XMLPrintNode(T, callcleanupblock);
  1785. PrintNodeUnindent;
  1786. WriteLn(T, PrintNodeIndention, '</callcleanupblock>');
  1787. end;
  1788. inherited XMLPrintNodeData(T);
  1789. end;
  1790. {$endif DEBUG_NODE_XML}
  1791. procedure tcallnode.printnodedata(var t:text);
  1792. begin
  1793. if assigned(procdefinition) and
  1794. (procdefinition.typ=procdef) then
  1795. writeln(t,printnodeindention,'proc = ',tprocdef(procdefinition).fullprocname(true))
  1796. else
  1797. begin
  1798. if assigned(symtableprocentry) then
  1799. writeln(t,printnodeindention,'proc = ',symtableprocentry.name)
  1800. else
  1801. writeln(t,printnodeindention,'proc = <nil>');
  1802. end;
  1803. if intrinsiccode <> Default(TInlineNumber) then
  1804. writeln(t,printnodeindention,'intrinsiccode = ', intrinsiccode);
  1805. if assigned(methodpointer) then
  1806. begin
  1807. writeln(t,printnodeindention,'methodpointer =');
  1808. printnode(t,methodpointer);
  1809. end;
  1810. if assigned(funcretnode) then
  1811. begin
  1812. writeln(t,printnodeindention,'funcretnode =');
  1813. printnode(t,funcretnode);
  1814. end;
  1815. if assigned(callinitblock) then
  1816. begin
  1817. writeln(t,printnodeindention,'callinitblock =');
  1818. printnode(t,callinitblock);
  1819. end;
  1820. if assigned(callcleanupblock) then
  1821. begin
  1822. writeln(t,printnodeindention,'callcleanupblock =');
  1823. printnode(t,callcleanupblock);
  1824. end;
  1825. if assigned(right) then
  1826. begin
  1827. writeln(t,printnodeindention,'right =');
  1828. printnode(t,right);
  1829. end;
  1830. if assigned(left) then
  1831. begin
  1832. writeln(t,printnodeindention,'left =');
  1833. printnode(t,left);
  1834. end;
  1835. end;
  1836. procedure tcallnode.insertintolist(l : tnodelist);
  1837. begin
  1838. end;
  1839. procedure tcallnode.add_init_statement(n:tnode);
  1840. var
  1841. lastinitstatement, before_firstpass : tstatementnode;
  1842. was_first_statement : boolean;
  1843. begin
  1844. if not assigned(n) then
  1845. exit;
  1846. if not assigned(callinitblock) then
  1847. begin
  1848. callinitblock:=internalstatements(lastinitstatement);
  1849. lastinitstatement.left.free;
  1850. lastinitstatement.left:=n;
  1851. firstpass(tnode(callinitblock));
  1852. exit;
  1853. end;
  1854. lastinitstatement:=laststatement(callinitblock);
  1855. was_first_statement:=(lastinitstatement=callinitblock.statements);
  1856. { all these nodes must be immediately typechecked, because this routine }
  1857. { can be called from pass_1 (i.e., after typecheck has already run) and }
  1858. { moreover, the entire blocks themselves are also only typechecked in }
  1859. { pass_1, while the the typeinfo is already required after the }
  1860. { typecheck pass for simplify purposes (not yet perfect, because the }
  1861. { statementnodes themselves are not typechecked this way) }
  1862. addstatement(lastinitstatement,n);
  1863. before_firstpass:=lastinitstatement;
  1864. firstpass(tnode(lastinitstatement));
  1865. if was_first_statement and (lastinitstatement<>before_firstpass) then
  1866. callinitblock.statements:=lastinitstatement;
  1867. { Update expectloc for callinitblock }
  1868. callinitblock.expectloc:=lastinitstatement.expectloc;
  1869. end;
  1870. procedure tcallnode.add_done_statement(n:tnode);
  1871. var
  1872. lastdonestatement, before_firstpass : tstatementnode;
  1873. was_first_statement : boolean;
  1874. begin
  1875. if not assigned(n) then
  1876. exit;
  1877. if not assigned(callcleanupblock) then
  1878. begin
  1879. callcleanupblock:=internalstatements(lastdonestatement);
  1880. lastdonestatement.left.free;
  1881. lastdonestatement.left:=n;
  1882. firstpass(tnode(callcleanupblock));
  1883. exit;
  1884. end;
  1885. lastdonestatement:=laststatement(callcleanupblock);
  1886. was_first_statement:=(lastdonestatement=callcleanupblock.statements);
  1887. { see comments in add_init_statement }
  1888. addstatement(lastdonestatement,n);
  1889. before_firstpass:=lastdonestatement;
  1890. firstpass(tnode(lastdonestatement));
  1891. if was_first_statement and (lastdonestatement<>before_firstpass) then
  1892. callcleanupblock.statements:=lastdonestatement;
  1893. { Update expectloc for callcleanupblock }
  1894. callcleanupblock.expectloc:=lastdonestatement.expectloc;
  1895. end;
  1896. function tcallnode.para_count:longint;
  1897. var
  1898. ppn : tcallparanode;
  1899. begin
  1900. result:=0;
  1901. ppn:=tcallparanode(left);
  1902. while assigned(ppn) do
  1903. begin
  1904. if not(assigned(ppn.parasym) and
  1905. (vo_is_hidden_para in ppn.parasym.varoptions)) then
  1906. inc(result);
  1907. ppn:=tcallparanode(ppn.right);
  1908. end;
  1909. end;
  1910. function tcallnode.required_para_count: longint;
  1911. var
  1912. ppn : tcallparanode;
  1913. begin
  1914. result:=0;
  1915. ppn:=tcallparanode(left);
  1916. while assigned(ppn) do
  1917. begin
  1918. if not(assigned(ppn.parasym) and
  1919. ((vo_is_hidden_para in ppn.parasym.varoptions) or
  1920. assigned(ppn.parasym.defaultconstsym))) then
  1921. inc(result);
  1922. ppn:=tcallparanode(ppn.right);
  1923. end;
  1924. end;
  1925. function tcallnode.GetParaFromIndex(const Index: Integer): TCallParaNode;
  1926. var
  1927. hp : TCallParaNode;
  1928. Count: Integer;
  1929. begin
  1930. Result := nil;
  1931. Count := 0;
  1932. hp := TCallParaNode(left);
  1933. repeat
  1934. { If the original indices have not yet been set, just go by the order
  1935. they appear in the node tree }
  1936. if hp.originalindex = -1 then
  1937. begin
  1938. if Count = Index then
  1939. begin
  1940. Result := hp;
  1941. Exit;
  1942. end;
  1943. Inc(Count);
  1944. end
  1945. else if hp.originalindex = Index then
  1946. begin
  1947. Result := hp;
  1948. Exit;
  1949. end;
  1950. hp := TCallParaNode(hp.right);
  1951. until not Assigned(hp);
  1952. end;
  1953. function tcallnode.is_simple_para_load(p:tnode; may_be_in_reg: boolean):boolean;
  1954. var
  1955. hp : tnode;
  1956. begin
  1957. hp:=p;
  1958. while assigned(hp) and
  1959. (hp.nodetype=typeconvn) and
  1960. (ttypeconvnode(hp).convtype=tc_equal) do
  1961. hp:=tunarynode(hp).left;
  1962. result:=(hp.nodetype in [typen,loadvmtaddrn,loadn,temprefn,arrayconstructorn,addrn]);
  1963. if result and
  1964. not(may_be_in_reg) then
  1965. case hp.nodetype of
  1966. loadn:
  1967. result:=(tabstractvarsym(tloadnode(hp).symtableentry).varregable in [vr_none,vr_addr]);
  1968. temprefn:
  1969. result:=not(ti_may_be_in_reg in ttemprefnode(hp).tempflags);
  1970. else
  1971. ;
  1972. end;
  1973. end;
  1974. function tcallnode.getoverrideprocnamedef: tprocdef; inline;
  1975. begin
  1976. result:=foverrideprocnamedef;
  1977. end;
  1978. function look_for_call(var n: tnode; arg: pointer): foreachnoderesult;
  1979. begin
  1980. case n.nodetype of
  1981. calln,asn:
  1982. result := fen_norecurse_true;
  1983. typen,loadvmtaddrn,loadn,temprefn,arrayconstructorn:
  1984. result := fen_norecurse_false;
  1985. else
  1986. result := fen_false;
  1987. end;
  1988. end;
  1989. procedure tcallnode.maybe_load_in_temp(var p:tnode);
  1990. begin
  1991. { Load all complex loads into a temp to prevent
  1992. double calls to a function. We can't simply check for a hp.nodetype=calln }
  1993. if assigned(p) and
  1994. foreachnodestatic(p,@look_for_call,nil) then
  1995. load_in_temp(p);
  1996. end;
  1997. procedure tcallnode.load_in_temp(var p:tnode);
  1998. var
  1999. loadp,
  2000. refp : tnode;
  2001. hdef : tdef;
  2002. ptemp : ttempcreatenode;
  2003. usederef : boolean;
  2004. begin
  2005. if assigned(p) then
  2006. begin
  2007. { temp create }
  2008. usederef:=(p.resultdef.typ in [arraydef,recorddef]) or
  2009. is_shortstring(p.resultdef) or
  2010. is_object(p.resultdef);
  2011. if usederef then
  2012. hdef:=cpointerdef.getreusable(p.resultdef)
  2013. else
  2014. hdef:=p.resultdef;
  2015. ptemp:=ctempcreatenode.create(hdef,hdef.size,tt_persistent,true);
  2016. if usederef then
  2017. begin
  2018. loadp:=caddrnode.create_internal(p);
  2019. refp:=cderefnode.create(ctemprefnode.create(ptemp));
  2020. end
  2021. else
  2022. begin
  2023. loadp:=p;
  2024. refp:=ctemprefnode.create(ptemp);
  2025. { ensure that an invokable isn't called again }
  2026. if is_invokable(hdef) then
  2027. include(ttemprefnode(refp).flags,nf_load_procvar);
  2028. end;
  2029. add_init_statement(ptemp);
  2030. add_init_statement(cassignmentnode.create(
  2031. ctemprefnode.create(ptemp),
  2032. loadp));
  2033. add_done_statement(ctempdeletenode.create(ptemp));
  2034. { new tree is only a temp reference }
  2035. p:=refp;
  2036. typecheckpass(p);
  2037. end;
  2038. end;
  2039. function tcallnode.gen_high_tree(var p:tnode;paradef:tdef):tnode;
  2040. { When passing an array to an open array, or a string to an open string,
  2041. some code is needed that generates the high bound of the array. This
  2042. function returns a tree containing the nodes for it. }
  2043. var
  2044. temp: tnode;
  2045. len : integer;
  2046. loadconst : boolean;
  2047. hightree,l,r : tnode;
  2048. defkind: tdeftyp;
  2049. begin
  2050. len:=-1;
  2051. loadconst:=true;
  2052. hightree:=nil;
  2053. { constant strings are internally stored as array of char, but if the
  2054. parameter is a string also treat it like one }
  2055. defkind:=p.resultdef.typ;
  2056. if (p.nodetype=stringconstn) and
  2057. (paradef.typ=stringdef) then
  2058. defkind:=stringdef;
  2059. case defkind of
  2060. arraydef :
  2061. begin
  2062. if (paradef.typ<>arraydef) then
  2063. internalerror(200405241);
  2064. { passing a string to an array of char }
  2065. if (p.nodetype=stringconstn) and
  2066. is_char(tarraydef(paradef).elementdef) then
  2067. begin
  2068. len:=tstringconstnode(p).len;
  2069. if len>0 then
  2070. dec(len);
  2071. end
  2072. else
  2073. { handle special case of passing an single array to an array of array }
  2074. if compare_defs(tarraydef(paradef).elementdef,p.resultdef,nothingn)>=te_equal then
  2075. len:=0
  2076. else
  2077. begin
  2078. { handle via a normal inline in_high_x node }
  2079. loadconst:=false;
  2080. { slice? }
  2081. if (p.nodetype=inlinen) and (tinlinenode(p).inlinenumber=in_slice_x) then
  2082. with Tcallparanode(Tinlinenode(p).left) do
  2083. begin
  2084. {Array slice using slice builtin function.}
  2085. l:=Tcallparanode(right).left;
  2086. hightree:=caddnode.create(subn,geninlinenode(in_ord_x,false,l),genintconstnode(1));
  2087. Tcallparanode(right).left:=nil;
  2088. {Remove the inline node.}
  2089. temp:=p;
  2090. p:=left;
  2091. Tcallparanode(tinlinenode(temp).left).left:=nil;
  2092. temp.free;
  2093. typecheckpass(hightree);
  2094. end
  2095. else if (p.nodetype=vecn) and (Tvecnode(p).right.nodetype=rangen) then
  2096. begin
  2097. {Array slice using .. operator.}
  2098. with Trangenode(Tvecnode(p).right) do
  2099. begin
  2100. l:=geninlinenode(in_ord_x,false,left); {Get lower bound.}
  2101. r:=geninlinenode(in_ord_x,false,right); {Get upper bound.}
  2102. end;
  2103. {In the procedure the array range is 0..(upper_bound-lower_bound).}
  2104. hightree:=caddnode.create(subn,r,l);
  2105. {Replace the rangnode in the tree by its lower_bound, and
  2106. dispose the rangenode.}
  2107. temp:=Tvecnode(p).right;
  2108. Tvecnode(p).right:=l.getcopy;
  2109. {Typecheckpass can only be performed *after* the l.getcopy since it
  2110. can modify the tree, and l is in the hightree.}
  2111. typecheckpass(hightree);
  2112. with Trangenode(temp) do
  2113. begin
  2114. left:=nil;
  2115. right:=nil;
  2116. end;
  2117. temp.free;
  2118. {Tree changed from p[l..h] to p[l], recalculate resultdef.}
  2119. p.resultdef:=nil;
  2120. typecheckpass(p);
  2121. end
  2122. else
  2123. begin
  2124. maybe_load_in_temp(p);
  2125. hightree:=geninlinenode(in_ord_x,false,geninlinenode(in_high_x,false,p.getcopy));
  2126. typecheckpass(hightree);
  2127. { only substract low(array) if it's <> 0 }
  2128. temp:=geninlinenode(in_ord_x,false,geninlinenode(in_low_x,false,p.getcopy));
  2129. typecheckpass(temp);
  2130. if (temp.nodetype <> ordconstn) or
  2131. (tordconstnode(temp).value <> 0) then
  2132. begin
  2133. hightree:=caddnode.create(subn,hightree,temp);
  2134. include(hightree.flags,nf_internal);
  2135. end
  2136. else
  2137. temp.free;
  2138. end;
  2139. end;
  2140. end;
  2141. stringdef :
  2142. begin
  2143. if is_open_string(paradef) then
  2144. begin
  2145. { a stringconstn is not a simple parameter and hence would be
  2146. loaded in a temp, but in that case the high() node
  2147. a) goes wrong (it cannot deal with a temp node)
  2148. b) would give a generic result instead of one specific to
  2149. this constant string
  2150. }
  2151. if p.nodetype<>stringconstn then
  2152. maybe_load_in_temp(p);
  2153. { handle via a normal inline in_high_x node }
  2154. loadconst := false;
  2155. hightree := geninlinenode(in_high_x,false,p.getcopy);
  2156. end
  2157. else
  2158. { handle special case of passing an single string to an array of string }
  2159. if compare_defs(tarraydef(paradef).elementdef,p.resultdef,nothingn)>=te_equal then
  2160. len:=0
  2161. else
  2162. { passing a string to an array of char }
  2163. if (p.nodetype=stringconstn) and
  2164. is_char(tarraydef(paradef).elementdef) then
  2165. begin
  2166. len:=tstringconstnode(p).len;
  2167. if len>0 then
  2168. dec(len);
  2169. end
  2170. else
  2171. begin
  2172. maybe_load_in_temp(p);
  2173. hightree:=caddnode.create(subn,geninlinenode(in_length_x,false,p.getcopy),
  2174. cordconstnode.create(1,sizesinttype,false));
  2175. loadconst:=false;
  2176. end;
  2177. end;
  2178. else
  2179. len:=0;
  2180. end;
  2181. if loadconst then
  2182. hightree:=cordconstnode.create(len,sizesinttype,true)
  2183. else
  2184. begin
  2185. if not assigned(hightree) then
  2186. internalerror(200304071);
  2187. { Need to use explicit, because it can also be a enum }
  2188. hightree:=ctypeconvnode.create_internal(hightree,sizesinttype);
  2189. end;
  2190. result:=hightree;
  2191. end;
  2192. function tcallnode.gen_procvar_context_tree_self:tnode;
  2193. begin
  2194. { Load tmehodpointer(right).self }
  2195. result:=genloadfield(ctypeconvnode.create_internal(
  2196. right.getcopy,methodpointertype),
  2197. 'self');
  2198. end;
  2199. function tcallnode.gen_procvar_context_tree_parentfp: tnode;
  2200. begin
  2201. { Load tnestedprocpointer(right).parentfp }
  2202. result:=genloadfield(ctypeconvnode.create_internal(
  2203. right.getcopy,nestedprocpointertype),
  2204. 'parentfp');
  2205. end;
  2206. function tcallnode.gen_self_tree:tnode;
  2207. var
  2208. selftree : tnode;
  2209. selfdef : tdef;
  2210. temp : ttempcreatenode;
  2211. begin
  2212. selftree:=nil;
  2213. { When methodpointer was a callnode we must load it first into a
  2214. temp to prevent processing the callnode twice }
  2215. if (methodpointer.nodetype=calln) then
  2216. internalerror(200405121);
  2217. { Objective-C: objc_convert_to_message_send() already did all necessary
  2218. transformation on the methodpointer }
  2219. if (procdefinition.typ=procdef) and
  2220. (po_objc in tprocdef(procdefinition).procoptions) then
  2221. selftree:=methodpointer.getcopy
  2222. { inherited }
  2223. else if (cnf_inherited in callnodeflags) then
  2224. begin
  2225. selftree:=safe_call_self_node.getcopy;
  2226. { we can call an inherited class static/method from a regular method
  2227. -> self node must change from instance pointer to vmt pointer)
  2228. }
  2229. if (procdefinition.procoptions*[po_classmethod,po_staticmethod] <> []) and
  2230. (selftree.resultdef.typ<>classrefdef) then
  2231. selftree:=cloadvmtaddrnode.create(selftree);
  2232. end
  2233. else
  2234. { constructors }
  2235. if (procdefinition.proctypeoption=potype_constructor) then
  2236. begin
  2237. if (methodpointer.resultdef.typ=classrefdef) or
  2238. (cnf_new_call in callnodeflags) then
  2239. if not is_javaclass(tdef(procdefinition.owner.defowner)) then
  2240. begin
  2241. if (cnf_new_call in callnodeflags) then
  2242. { old-style object: push 0 as self }
  2243. selftree:=cpointerconstnode.create(0,voidpointertype)
  2244. else
  2245. begin
  2246. { class-style: push classtype }
  2247. selftree:=methodpointer.getcopy;
  2248. if selftree.nodetype=typen then
  2249. begin
  2250. selftree:=cloadvmtaddrnode.create(selftree);
  2251. tloadvmtaddrnode(selftree).forcall:=true;
  2252. end;
  2253. end;
  2254. end
  2255. else
  2256. { special handling for Java constructors, handled in
  2257. tjvmcallnode.extra_pre_call_code }
  2258. selftree:=cnothingnode.create
  2259. else
  2260. begin
  2261. if methodpointer.nodetype=typen then
  2262. if (methodpointer.resultdef.typ<>objectdef) then
  2263. begin
  2264. if not(target_info.system in systems_jvm) then
  2265. begin
  2266. { TSomeRecord.Constructor call. We need to allocate }
  2267. { self node as a temp node of the result type }
  2268. temp:=ctempcreatenode.create(methodpointer.resultdef,methodpointer.resultdef.size,tt_persistent,false);
  2269. add_init_statement(temp);
  2270. add_done_statement(ctempdeletenode.create_normal_temp(temp));
  2271. selftree:=ctemprefnode.create(temp);
  2272. end
  2273. else
  2274. begin
  2275. { special handling for Java constructors, handled in
  2276. tjvmcallnode.extra_pre_call_code }
  2277. selftree:=cnothingnode.create
  2278. end;
  2279. end
  2280. else
  2281. selftree:=safe_call_self_node.getcopy
  2282. else
  2283. selftree:=methodpointer.getcopy;
  2284. end;
  2285. end
  2286. else
  2287. { Calling a static/class method }
  2288. if (po_classmethod in procdefinition.procoptions) or
  2289. (po_staticmethod in procdefinition.procoptions) then
  2290. begin
  2291. if (procdefinition.typ<>procdef) then
  2292. internalerror(200305062);
  2293. { if the method belongs to a helper then we need to use the
  2294. extended type for references to Self }
  2295. if is_objectpascal_helper(tprocdef(procdefinition).struct) then
  2296. selfdef:=tobjectdef(tprocdef(procdefinition).struct).extendeddef
  2297. else
  2298. selfdef:=tprocdef(procdefinition).struct;
  2299. if ((selfdef.typ in [recorddef,objectdef]) and
  2300. (oo_has_vmt in tabstractrecorddef(selfdef).objectoptions)) or
  2301. { all Java classes have a "VMT" }
  2302. (target_info.system in systems_jvm) then
  2303. begin
  2304. { we only need the vmt, loading self is not required and there is no
  2305. need to check for typen, because that will always get the
  2306. loadvmtaddrnode added }
  2307. selftree:=methodpointer.getcopy;
  2308. if (methodpointer.resultdef.typ<>classrefdef) or
  2309. (methodpointer.nodetype = typen) then
  2310. selftree:=cloadvmtaddrnode.create(selftree);
  2311. end
  2312. else
  2313. selftree:=cpointerconstnode.create(0,voidpointertype);
  2314. end
  2315. else
  2316. begin
  2317. if methodpointer.nodetype=typen then
  2318. selftree:=safe_call_self_node.getcopy
  2319. else
  2320. selftree:=methodpointer.getcopy;
  2321. end;
  2322. result:=selftree;
  2323. end;
  2324. function tcallnode.use_caller_self(check_for_callee_self: boolean): boolean;
  2325. var
  2326. i: longint;
  2327. ps: tparavarsym;
  2328. begin
  2329. result:=false;
  2330. { is there a self parameter? }
  2331. if check_for_callee_self then
  2332. begin
  2333. ps:=nil;
  2334. for i:=0 to procdefinition.paras.count-1 do
  2335. begin
  2336. ps:=tparavarsym(procdefinition.paras[i]);
  2337. if vo_is_self in ps.varoptions then
  2338. break;
  2339. ps:=nil;
  2340. end;
  2341. if not assigned(ps) then
  2342. exit;
  2343. end;
  2344. { we need to load the'self' parameter of the current routine as the
  2345. 'self' parameter of the called routine if
  2346. 1) we're calling an inherited routine
  2347. 2) we're calling a constructor via type.constructorname and
  2348. type is not a classrefdef (i.e., we're calling a constructor like
  2349. a regular method)
  2350. 3) we're calling any regular (non-class/non-static) method via
  2351. a typenode (the methodpointer is then that typenode, but the
  2352. passed self node must become the current self node)
  2353. In other cases, we either don't have to pass the 'self' parameter of
  2354. the current routine to the called one, or methodpointer will already
  2355. contain it (e.g. because a method was called via "method", in which
  2356. case the parser already passed 'self' as the method pointer, or via
  2357. "self.method") }
  2358. if (cnf_inherited in callnodeflags) or
  2359. ((procdefinition.proctypeoption=potype_constructor) and
  2360. not((methodpointer.resultdef.typ=classrefdef) or
  2361. (cnf_new_call in callnodeflags)) and
  2362. (methodpointer.nodetype=typen) and
  2363. (methodpointer.resultdef.typ=objectdef)) or
  2364. (assigned(methodpointer) and
  2365. (procdefinition.proctypeoption<>potype_constructor) and
  2366. not(po_classmethod in procdefinition.procoptions) and
  2367. not(po_staticmethod in procdefinition.procoptions) and
  2368. (methodpointer.nodetype=typen)) then
  2369. result:=true;
  2370. end;
  2371. procedure tcallnode.maybe_gen_call_self_node;
  2372. begin
  2373. if cnf_call_self_node_done in callnodeflags then
  2374. exit;
  2375. include(callnodeflags,cnf_call_self_node_done);
  2376. if use_caller_self(true) then
  2377. call_self_node:=load_self_node;
  2378. end;
  2379. procedure tcallnode.register_created_object_types;
  2380. var
  2381. crefdef,
  2382. systobjectdef : tdef;
  2383. begin
  2384. { only makes sense for methods }
  2385. if not assigned(methodpointer) then
  2386. exit;
  2387. { inherited calls don't create an instance of the inherited type, but of
  2388. the current type }
  2389. if ([cnf_inherited,cnf_anon_inherited,cnf_ignore_devirt_wpo]*callnodeflags)<>[] then
  2390. exit;
  2391. if (methodpointer.resultdef.typ=classrefdef) then
  2392. begin
  2393. { constructor call via classreference => instance can be created
  2394. same with calling newinstance without a instance-self (don't
  2395. consider self-based newinstance calls, because then everything
  2396. will be assumed to be just a TObject since TObject.Create calls
  2397. NewInstance) }
  2398. if procdefinition.wpo_may_create_instance(methodpointer) then
  2399. begin
  2400. { Only a typenode can be passed when it is called with <class of xx>.create }
  2401. if (methodpointer.nodetype=typen) then
  2402. begin
  2403. if wpoinfomanager.symbol_live_in_currentproc(methodpointer.resultdef) then
  2404. { we know the exact class type being created }
  2405. tclassrefdef(methodpointer.resultdef).pointeddef.register_created_object_type
  2406. end
  2407. else
  2408. begin
  2409. { the loadvmtaddrnode is already created in case of classtype.create }
  2410. if (methodpointer.nodetype=loadvmtaddrn) and
  2411. (tloadvmtaddrnode(methodpointer).left.nodetype=typen) then
  2412. begin
  2413. if wpoinfomanager.symbol_live_in_currentproc(methodpointer.resultdef) then
  2414. tclassrefdef(methodpointer.resultdef).pointeddef.register_created_object_type
  2415. end
  2416. else
  2417. begin
  2418. if wpoinfomanager.symbol_live_in_currentproc(methodpointer.resultdef) then
  2419. begin
  2420. { special case: if the classref comes from x.classtype (with classtype,
  2421. being tobject.classtype) then the created instance is x or a descendant
  2422. of x (rather than tobject or a descendant of tobject)
  2423. }
  2424. systobjectdef:=search_system_type('TOBJECT').typedef;
  2425. if (methodpointer.nodetype=calln) and
  2426. { not a procvar call }
  2427. not assigned(right) and
  2428. { procdef is owned by system.tobject }
  2429. (tprocdef(tcallnode(methodpointer).procdefinition).owner.defowner=systobjectdef) and
  2430. { we're calling system.tobject.classtype }
  2431. (tcallnode(methodpointer).symtableprocentry.name='CLASSTYPE') and
  2432. { could again be a classrefdef, but unlikely }
  2433. (tcallnode(methodpointer).methodpointer.resultdef.typ=objectdef) and
  2434. { don't go through this trouble if it was already a tobject }
  2435. (tcallnode(methodpointer).methodpointer.resultdef<>systobjectdef) then
  2436. begin
  2437. { register this object type as classref, so all descendents will also
  2438. be marked as instantiatable (only the pointeddef will actually be
  2439. recorded, so it's no problem that the clasrefdef is only temporary)
  2440. }
  2441. crefdef:=cclassrefdef.create(tcallnode(methodpointer).methodpointer.resultdef);
  2442. { and register it }
  2443. crefdef.register_created_object_type;
  2444. end
  2445. else
  2446. { the created class can be any child class as well -> register classrefdef }
  2447. methodpointer.resultdef.register_created_object_type;
  2448. end;
  2449. end;
  2450. end;
  2451. end
  2452. end
  2453. else
  2454. { Old style object }
  2455. if is_object(methodpointer.resultdef) then
  2456. begin
  2457. { constructor with extended syntax called from new }
  2458. if (cnf_new_call in callnodeflags) then
  2459. begin
  2460. if wpoinfomanager.symbol_live_in_currentproc(methodpointer.resultdef) then
  2461. methodpointer.resultdef.register_created_object_type;
  2462. end
  2463. else
  2464. { normal object call like obj.proc }
  2465. if not(cnf_dispose_call in callnodeflags) and
  2466. not(cnf_inherited in callnodeflags) and
  2467. not(cnf_member_call in callnodeflags) then
  2468. begin
  2469. if (procdefinition.proctypeoption=potype_constructor) then
  2470. begin
  2471. if (methodpointer.nodetype<>typen) and
  2472. wpoinfomanager.symbol_live_in_currentproc(methodpointer.resultdef) then
  2473. methodpointer.resultdef.register_created_object_type;
  2474. end
  2475. end;
  2476. end;
  2477. end;
  2478. function tcallnode.get_expect_loc: tcgloc;
  2479. var
  2480. realresdef: tstoreddef;
  2481. begin
  2482. if not assigned(typedef) then
  2483. realresdef:=tstoreddef(resultdef)
  2484. else
  2485. realresdef:=tstoreddef(typedef);
  2486. if realresdef.is_intregable then
  2487. result:=LOC_REGISTER
  2488. else if (realresdef.typ=floatdef) and
  2489. not(cs_fp_emulation in current_settings.moduleswitches) then
  2490. if use_vectorfpu(realresdef) then
  2491. result:=LOC_MMREGISTER
  2492. else
  2493. {$ifdef x86}
  2494. result:=LOC_REFERENCE
  2495. {$else x86}
  2496. result:=LOC_FPUREGISTER
  2497. {$endif x86}
  2498. else
  2499. result:=LOC_REFERENCE
  2500. end;
  2501. function tcallnode.handle_compilerproc: tnode;
  2502. var
  2503. para: TCallParaNode;
  2504. maxlennode, outnode, valnode: TNode;
  2505. MaxStrLen: Int64;
  2506. StringLiteral, name: string;
  2507. ValOutput: TConstExprInt;
  2508. ValCode: Longint;
  2509. NewStatements: TStatementNode;
  2510. si : ShortInt;
  2511. b: Byte;
  2512. i: SmallInt;
  2513. w: Word;
  2514. li: LongInt;
  2515. dw: DWord;
  2516. i64: Int64;
  2517. qw: QWord;
  2518. begin
  2519. result := nil;
  2520. case intrinsiccode of
  2521. in_str_x_string:
  2522. begin
  2523. { rare optimization opportunity which takes some extra time,
  2524. so check only at level 3+ }
  2525. if not(cs_opt_level3 in current_settings.optimizerswitches) then
  2526. exit;
  2527. { If n is a constant, attempt to convert, for example:
  2528. "Str(5, Output);" to "Output := '5';" }
  2529. { Format of the internal function (also for fpc_shortstr_uint) is:
  2530. $fpc_shortstr_sint(Int64;Int64;out OpenString;<const Int64>); }
  2531. { Remember the parameters are in reverse order - the leftmost one
  2532. can usually be ignored }
  2533. para := GetParaFromIndex(1);
  2534. if Assigned(para) then
  2535. begin
  2536. { Output variable }
  2537. outnode := para.left;
  2538. para := GetParaFromIndex(2);
  2539. if Assigned(para) then
  2540. begin
  2541. { Maximum length }
  2542. maxlennode := para.left;
  2543. if is_integer(maxlennode.resultdef) then
  2544. begin
  2545. para := GetParaFromIndex(3);
  2546. while (maxlennode.nodetype = typeconvn) and (ttypeconvnode(maxlennode).convtype in [tc_equal, tc_int_2_int]) do
  2547. begin
  2548. maxlennode := ttypeconvnode(maxlennode).left;
  2549. end;
  2550. if Assigned(para) and is_constintnode(maxlennode) then
  2551. begin
  2552. { Numeric value }
  2553. valnode := para.left;
  2554. if is_integer(valnode.resultdef) and not Assigned(GetParaFromIndex(4)) then
  2555. begin
  2556. while (valnode.nodetype = typeconvn) and (ttypeconvnode(valnode).convtype in [tc_equal, tc_int_2_int]) do
  2557. begin
  2558. valnode := ttypeconvnode(valnode).left;
  2559. end;
  2560. if is_constintnode(valnode) then
  2561. begin
  2562. MaxStrLen := TOrdConstNode(maxlennode).value.svalue;
  2563. { If we've gotten this far, we can convert the node into a direct assignment }
  2564. StringLiteral := tostr(tordconstnode(valnode).value);
  2565. if MaxStrLen <> -1 then
  2566. SetLength(StringLiteral, Integer(MaxStrLen));
  2567. result := cassignmentnode.create(
  2568. outnode.getcopy,
  2569. cstringconstnode.createstr(StringLiteral)
  2570. );
  2571. end;
  2572. end;
  2573. end;
  2574. end;
  2575. end;
  2576. end;
  2577. end;
  2578. in_val_x:
  2579. begin
  2580. { rare optimization opportunity which takes some extra time,
  2581. so check only at level 3+ }
  2582. if not(cs_opt_level3 in current_settings.optimizerswitches) then
  2583. exit;
  2584. { If the input is a constant, attempt to convert, for example:
  2585. "Val('5', Output, Code);" to "Output := 5; Code := 0;" }
  2586. { Format of the internal function fpc_val_sint_*str) is:
  2587. fpc_val_sint_*str(SizeInt; *String; out ValSInt): ValSInt; }
  2588. { Remember the parameters are in reverse order - the leftmost one
  2589. is the integer data size can usually be ignored.
  2590. For fpc_val_uint_*str variants, the data size is not present as
  2591. of FPC 3.2.0
  2592. Para indices:
  2593. * 0 = Code output (present even if omitted in original code)
  2594. * 1 = String input
  2595. * 2 = Data size
  2596. }
  2597. para := GetParaFromIndex(0);
  2598. if Assigned(para) then
  2599. begin
  2600. outnode := para.left;
  2601. para := GetParaFromIndex(1);
  2602. if Assigned(para) then
  2603. begin
  2604. valnode:=para.left;
  2605. name:=tprocdef(procdefinition).fullprocname(true);
  2606. if is_conststringnode(valnode) and
  2607. { we can handle only the fpc_val_sint helpers so far }
  2608. ((copy(name,1,13)='$fpc_val_sint') or (copy(name,1,13)='$fpc_val_uint')) then
  2609. begin
  2610. ValOutput.signed := is_signed(ResultDef);
  2611. case Longint(tordconstnode(GetParaFromIndex(2).paravalue).value.svalue) of
  2612. 1:
  2613. if ValOutput.signed then
  2614. begin
  2615. Val(TStringConstNode(valnode).value_str, si, ValCode);
  2616. ValOutput.svalue:=si;
  2617. end
  2618. else
  2619. begin
  2620. Val(TStringConstNode(valnode).value_str, b, ValCode);
  2621. ValOutput.uvalue:=b;
  2622. end;
  2623. 2:
  2624. if ValOutput.signed then
  2625. begin
  2626. Val(TStringConstNode(valnode).value_str, i, ValCode);
  2627. ValOutput.svalue:=i;
  2628. end
  2629. else
  2630. begin
  2631. Val(TStringConstNode(valnode).value_str, w, ValCode);
  2632. ValOutput.uvalue:=w;
  2633. end;
  2634. 4:
  2635. if ValOutput.signed then
  2636. begin
  2637. Val(TStringConstNode(valnode).value_str, li, ValCode);
  2638. ValOutput.svalue:=li;
  2639. end
  2640. else
  2641. begin
  2642. Val(TStringConstNode(valnode).value_str, dw, ValCode);
  2643. ValOutput.uvalue:=dw;
  2644. end;
  2645. 8:
  2646. if ValOutput.signed then
  2647. begin
  2648. Val(TStringConstNode(valnode).value_str, i64, ValCode);
  2649. ValOutput.svalue:=i64;
  2650. end
  2651. else
  2652. begin
  2653. Val(TStringConstNode(valnode).value_str, qw, ValCode);
  2654. ValOutput.uvalue:=qw;
  2655. end;
  2656. else
  2657. Internalerror(2024011402);
  2658. end;
  2659. { Due to the way the node tree works, we have to insert
  2660. the assignment to the Code output within the
  2661. assignment to the value output (function result),
  2662. so use a block node for that}
  2663. Result := internalstatements(NewStatements);
  2664. { Create a node for writing the Code output }
  2665. addstatement(
  2666. NewStatements,
  2667. CAssignmentNode.Create_Internal(
  2668. outnode.getcopy(), { The original will get destroyed }
  2669. COrdConstNode.Create(ValCode, outnode.ResultDef, False)
  2670. )
  2671. );
  2672. { Now actually create the function result }
  2673. case resultdef.typ of
  2674. orddef:
  2675. valnode := COrdConstNode.Create(ValOutput, resultdef, False);
  2676. else
  2677. Internalerror(2024011401);
  2678. end;
  2679. addstatement(NewStatements, valnode);
  2680. { Result will now undergo firstpass }
  2681. end;
  2682. end;
  2683. end;
  2684. end;
  2685. else
  2686. ;
  2687. end;
  2688. end;
  2689. function tcallnode.safe_call_self_node: tnode;
  2690. begin
  2691. if not assigned(call_self_node) then
  2692. begin
  2693. CGMessage(parser_e_illegal_expression);
  2694. call_self_node:=cerrornode.create;
  2695. end;
  2696. result:=call_self_node;
  2697. end;
  2698. procedure tcallnode.gen_vmt_entry_load;
  2699. var
  2700. vmt_def: trecorddef;
  2701. begin
  2702. if not assigned(right) and
  2703. not assigned(overrideprocnamedef) and
  2704. (po_virtualmethod in procdefinition.procoptions) and
  2705. not is_objectpascal_helper(tprocdef(procdefinition).struct) and
  2706. assigned(methodpointer) and
  2707. (methodpointer.nodetype<>typen) then
  2708. begin
  2709. vmt_entry:=load_vmt_for_self_node(methodpointer.getcopy);
  2710. { get the right entry in the VMT }
  2711. vmt_entry:=cderefnode.create(vmt_entry);
  2712. typecheckpass(vmt_entry);
  2713. vmt_def:=trecorddef(vmt_entry.resultdef);
  2714. { tobjectdef(tprocdef(procdefinition).struct) can be a parent of the
  2715. methodpointer's resultdef, but the vmtmethodoffset of the method
  2716. in that objectdef is obviously the same as in any child class }
  2717. vmt_entry:=csubscriptnode.create(
  2718. trecordsymtable(vmt_def.symtable).findfieldbyoffset(
  2719. tobjectdef(tprocdef(procdefinition).struct).vmtmethodoffset(tprocdef(procdefinition).extnumber)
  2720. ),
  2721. vmt_entry
  2722. );
  2723. firstpass(vmt_entry);
  2724. end;
  2725. end;
  2726. procedure tcallnode.gen_syscall_para(para: tcallparanode);
  2727. begin
  2728. { unsupported }
  2729. internalerror(2014040101);
  2730. end;
  2731. procedure tcallnode.objc_convert_to_message_send;
  2732. var
  2733. block,
  2734. selftree : tnode;
  2735. statements : tstatementnode;
  2736. field : tfieldvarsym;
  2737. temp : ttempcreatenode;
  2738. selfrestype,
  2739. objcsupertype : tdef;
  2740. srsym : tsym;
  2741. srsymtable : tsymtable;
  2742. msgsendname : string;
  2743. begin
  2744. if not(m_objectivec1 in current_settings.modeswitches) then
  2745. Message(parser_f_modeswitch_objc_required);
  2746. { typecheck pass must already have run on the call node,
  2747. because pass1 calls this method
  2748. }
  2749. { default behaviour: call objc_msgSend and friends;
  2750. 64 bit targets for Mac OS X can override this as they
  2751. can call messages via an indirect function call similar to
  2752. dynamically linked functions, ARM maybe as well (not checked)
  2753. Which variant of objc_msgSend is used depends on the
  2754. result type, and on whether or not it's an inherited call.
  2755. }
  2756. { make sure we don't perform this transformation twice in case
  2757. firstpass would be called multiple times }
  2758. include(callnodeflags,cnf_objc_processed);
  2759. { make sure the methodpointer doesn't get translated into a call
  2760. as well (endless loop) }
  2761. if methodpointer.nodetype=loadvmtaddrn then
  2762. tloadvmtaddrnode(methodpointer).forcall:=true;
  2763. { A) set the appropriate objc_msgSend* variant to call }
  2764. { The AArch64 abi does not require special handling for struct returns }
  2765. {$ifndef aarch64}
  2766. { record returned via implicit pointer }
  2767. if paramanager.ret_in_param(resultdef,procdefinition) then
  2768. begin
  2769. if not(cnf_inherited in callnodeflags) then
  2770. msgsendname:='OBJC_MSGSEND_STRET'
  2771. else if (target_info.system in systems_objc_nfabi) and
  2772. (not MacOSXVersionMin.isvalid or
  2773. (MacOSXVersionMin.relationto(10,6,0)>=0)) then
  2774. msgsendname:='OBJC_MSGSENDSUPER2_STRET'
  2775. else
  2776. msgsendname:='OBJC_MSGSENDSUPER_STRET'
  2777. end
  2778. {$ifdef i386}
  2779. { special case for fpu results on i386 for non-inherited calls }
  2780. { TODO: also for x86_64 "extended" results }
  2781. else if (resultdef.typ=floatdef) and
  2782. not(cnf_inherited in callnodeflags) then
  2783. msgsendname:='OBJC_MSGSEND_FPRET'
  2784. {$endif i386}
  2785. { default }
  2786. else
  2787. {$endif aarch64}
  2788. if not(cnf_inherited in callnodeflags) then
  2789. msgsendname:='OBJC_MSGSEND'
  2790. else if (target_info.system in systems_objc_nfabi) and
  2791. (not MacOSXVersionMin.isvalid or
  2792. (MacOSXVersionMin.relationto(10,6,0)>=0)) then
  2793. msgsendname:='OBJC_MSGSENDSUPER2'
  2794. else
  2795. msgsendname:='OBJC_MSGSENDSUPER';
  2796. { get the mangled name }
  2797. srsym:=nil;
  2798. if not searchsym_in_named_module('OBJC',msgsendname,srsym,srsymtable) or
  2799. (srsym.typ<>procsym) or
  2800. (tprocsym(srsym).ProcdefList.count<>1) then
  2801. Message1(cg_f_unknown_compilerproc,'objc.'+msgsendname);
  2802. foverrideprocnamedef:=tprocdef(tprocsym(srsym).ProcdefList[0]);
  2803. { B) Handle self }
  2804. { 1) in case of sending a message to a superclass, self is a pointer to
  2805. an objc_super record
  2806. }
  2807. if (cnf_inherited in callnodeflags) then
  2808. begin
  2809. block:=internalstatements(statements);
  2810. objcsupertype:=search_named_unit_globaltype('OBJC','OBJC_SUPER',true).typedef;
  2811. if (objcsupertype.typ<>recorddef) then
  2812. internalerror(2009032901);
  2813. { temp for the for the objc_super record }
  2814. temp:=ctempcreatenode.create(objcsupertype,objcsupertype.size,tt_persistent,false);
  2815. addstatement(statements,temp);
  2816. { initialize objc_super record }
  2817. selftree:=safe_call_self_node.getcopy;
  2818. { we can call an inherited class static/method from a regular method
  2819. -> self node must change from instance pointer to vmt pointer)
  2820. }
  2821. if (po_classmethod in procdefinition.procoptions) and
  2822. (selftree.resultdef.typ<>classrefdef) then
  2823. begin
  2824. selftree:=cloadvmtaddrnode.create(selftree);
  2825. { since we're in a class method of the current class, its
  2826. information has already been initialized (and that of all of
  2827. its parent classes too) }
  2828. tloadvmtaddrnode(selftree).forcall:=true;
  2829. typecheckpass(selftree);
  2830. end;
  2831. selfrestype:=selftree.resultdef;
  2832. field:=tfieldvarsym(trecorddef(objcsupertype).symtable.find('RECEIVER'));
  2833. if not assigned(field) then
  2834. internalerror(2009032902);
  2835. { first the destination object/class instance }
  2836. addstatement(statements,
  2837. cassignmentnode.create(
  2838. csubscriptnode.create(field,ctemprefnode.create(temp)),
  2839. selftree
  2840. )
  2841. );
  2842. { and secondly, the class type in which the selector must be looked
  2843. up (the parent class in case of an instance method, the parent's
  2844. metaclass in case of a class method) }
  2845. field:=tfieldvarsym(trecorddef(objcsupertype).symtable.find('_CLASS'));
  2846. if not assigned(field) then
  2847. internalerror(2009032903);
  2848. addstatement(statements,
  2849. cassignmentnode.create(
  2850. csubscriptnode.create(field,ctemprefnode.create(temp)),
  2851. objcsuperclassnode(selftree.resultdef)
  2852. )
  2853. );
  2854. { result of this block is the address of this temp }
  2855. addstatement(statements,ctypeconvnode.create_internal(
  2856. caddrnode.create_internal(ctemprefnode.create(temp)),selfrestype)
  2857. );
  2858. { replace the method pointer with the address of this temp }
  2859. methodpointer.free;
  2860. methodpointer:=block;
  2861. typecheckpass(block);
  2862. end
  2863. else
  2864. { 2) regular call (not inherited) }
  2865. begin
  2866. { a) If we're calling a class method, use a class ref. }
  2867. if (po_classmethod in procdefinition.procoptions) and
  2868. ((methodpointer.nodetype=typen) or
  2869. (methodpointer.resultdef.typ<>classrefdef)) then
  2870. begin
  2871. methodpointer:=cloadvmtaddrnode.create(methodpointer);
  2872. { no need to obtain the class ref by calling class(), sending
  2873. this message will initialize it if necessary }
  2874. tloadvmtaddrnode(methodpointer).forcall:=true;
  2875. firstpass(methodpointer);
  2876. end;
  2877. end;
  2878. end;
  2879. function tcallnode.gen_vmt_tree:tnode;
  2880. var
  2881. vmttree : tnode;
  2882. begin
  2883. vmttree:=nil;
  2884. if not(procdefinition.proctypeoption in [potype_constructor,potype_destructor]) then
  2885. internalerror(200305051);
  2886. { When methodpointer was a callnode we must load it first into a
  2887. temp to prevent the processing callnode twice }
  2888. if (methodpointer.nodetype=calln) then
  2889. internalerror(200405122);
  2890. { Handle classes and legacy objects separate to make it
  2891. more maintainable }
  2892. if (methodpointer.resultdef.typ=classrefdef) then
  2893. begin
  2894. if not is_class(tclassrefdef(methodpointer.resultdef).pointeddef) then
  2895. internalerror(200501041);
  2896. { constructor call via classreference => allocate memory }
  2897. if (procdefinition.proctypeoption=potype_constructor) then
  2898. begin
  2899. vmttree:=cpointerconstnode.create(1,voidpointertype);
  2900. end
  2901. else { <class of xx>.destroy is not valid }
  2902. InternalError(2014020601);
  2903. end
  2904. else
  2905. { Class style objects }
  2906. if is_class(methodpointer.resultdef) then
  2907. begin
  2908. { inherited call, no create/destroy }
  2909. if (cnf_inherited in callnodeflags) then
  2910. vmttree:=cpointerconstnode.create(0,voidpointertype)
  2911. else
  2912. { do not create/destroy when called from member function
  2913. without specifying self explicit }
  2914. if (cnf_member_call in callnodeflags) then
  2915. begin
  2916. { destructor (in the same class, since cnf_member_call):
  2917. if not called from a destructor then
  2918. call beforedestruction and release instance, vmt=1
  2919. else
  2920. don't release instance, vmt=0
  2921. constructor (in the same class, since cnf_member_call):
  2922. if called from a constructor then
  2923. don't call afterconstruction, vmt=0
  2924. else
  2925. call afterconstrution but not NewInstance, vmt=-1 }
  2926. if (procdefinition.proctypeoption=potype_destructor) then
  2927. if (current_procinfo.procdef.proctypeoption<>potype_constructor) then
  2928. vmttree:=cpointerconstnode.create(1,voidpointertype)
  2929. else
  2930. vmttree:=cpointerconstnode.create(0,voidpointertype)
  2931. else if (current_procinfo.procdef.proctypeoption=potype_constructor) and
  2932. (procdefinition.proctypeoption=potype_constructor) then
  2933. vmttree:=cpointerconstnode.create(0,voidpointertype)
  2934. else
  2935. vmttree:=cpointerconstnode.create(TConstPtrUInt(-1),voidpointertype);
  2936. end
  2937. else
  2938. { normal call to method like cl1.proc }
  2939. begin
  2940. { destructor:
  2941. if not(called from exception block in constructor) or
  2942. (called from afterconstruction)
  2943. call beforedestruction and release instance, vmt=1
  2944. else
  2945. don't call beforedestruction and release instance, vmt=-1
  2946. constructor:
  2947. if called from a constructor in the same class using self.create then
  2948. don't call afterconstruction, vmt=0
  2949. else
  2950. call afterconstruction, vmt=1 }
  2951. if (procdefinition.proctypeoption=potype_destructor) then
  2952. if (cnf_create_failed in callnodeflags) and
  2953. is_class(methodpointer.resultdef) then
  2954. vmttree:=call_vmt_node.getcopy
  2955. else if not(cnf_create_failed in callnodeflags) then
  2956. vmttree:=cpointerconstnode.create(1,voidpointertype)
  2957. else
  2958. vmttree:=cpointerconstnode.create(TConstPtrUInt(-1),voidpointertype)
  2959. else
  2960. begin
  2961. if (current_procinfo.procdef.proctypeoption=potype_constructor) and
  2962. (procdefinition.proctypeoption=potype_constructor) and
  2963. (methodpointer.nodetype=loadn) and
  2964. (loadnf_is_self in tloadnode(methodpointer).loadnodeflags) then
  2965. vmttree:=cpointerconstnode.create(0,voidpointertype)
  2966. else
  2967. vmttree:=cpointerconstnode.create(TConstPtrUInt(-1),voidpointertype);
  2968. end;
  2969. end;
  2970. end
  2971. else
  2972. { Old style object }
  2973. begin
  2974. { constructor with extended syntax called from new }
  2975. if (cnf_new_call in callnodeflags) then
  2976. vmttree:=cloadvmtaddrnode.create(ctypenode.create(methodpointer.resultdef))
  2977. else
  2978. { destructor with extended syntax called from dispose }
  2979. { value -1 is what fpc_help_constructor() changes VMT to when it allocates memory }
  2980. if (cnf_dispose_call in callnodeflags) then
  2981. vmttree:=cpointerconstnode.create(TConstPtrUInt(-1),voidpointertype)
  2982. else
  2983. { destructor called from exception block in constructor }
  2984. if (cnf_create_failed in callnodeflags) then
  2985. vmttree:=ctypeconvnode.create_internal(call_vmt_node.getcopy,voidpointertype)
  2986. else
  2987. { inherited call, no create/destroy }
  2988. if (cnf_inherited in callnodeflags) then
  2989. vmttree:=cpointerconstnode.create(0,voidpointertype)
  2990. else
  2991. { do not create/destroy when called from member function
  2992. without specifying self explicit }
  2993. if (cnf_member_call in callnodeflags) then
  2994. begin
  2995. { destructor: don't release instance, vmt=0
  2996. constructor: don't initialize instance, vmt=0 }
  2997. vmttree:=cpointerconstnode.create(0,voidpointertype)
  2998. end
  2999. else
  3000. { normal object call like obj.proc }
  3001. begin
  3002. { destructor: direct call, no dispose, vmt=0
  3003. constructor: initialize object, load vmt }
  3004. if (procdefinition.proctypeoption=potype_constructor) then
  3005. begin
  3006. { old styled inherited call? }
  3007. if (methodpointer.nodetype=typen) then
  3008. vmttree:=cpointerconstnode.create(0,voidpointertype)
  3009. else
  3010. vmttree:=cloadvmtaddrnode.create(ctypenode.create(methodpointer.resultdef))
  3011. end
  3012. else
  3013. vmttree:=cpointerconstnode.create(0,voidpointertype);
  3014. end;
  3015. end;
  3016. result:=vmttree;
  3017. end;
  3018. function tcallnode.gen_block_context: tnode;
  3019. begin
  3020. { the self parameter of a block invocation is that address of the
  3021. block literal (which is what right contains) }
  3022. result:=right.getcopy;
  3023. end;
  3024. function check_funcret_used_as_para(var n: tnode; arg: pointer): foreachnoderesult;
  3025. var
  3026. destsym : tsym absolute arg;
  3027. begin
  3028. result := fen_false;
  3029. if (n.nodetype=loadn) and
  3030. (tloadnode(n).symtableentry = destsym) then
  3031. result := fen_norecurse_true;
  3032. end;
  3033. function check_funcret_temp_used_as_para(var n: tnode; arg: pointer): foreachnoderesult;
  3034. var
  3035. tempinfo : ptempinfo absolute arg;
  3036. begin
  3037. result := fen_false;
  3038. if (n.nodetype=temprefn) and
  3039. (ttemprefnode(n).tempinfo = tempinfo) then
  3040. result := fen_norecurse_true;
  3041. end;
  3042. function tcallnode.funcret_can_be_reused:boolean;
  3043. var
  3044. realassignmenttarget: tnode;
  3045. alignment: longint;
  3046. begin
  3047. result:=false;
  3048. { we are processing an assignment node? }
  3049. if not(assigned(aktassignmentnode) and
  3050. (aktassignmentnode.right=self) and
  3051. (aktassignmentnode.left.resultdef=resultdef)) then
  3052. exit;
  3053. { destination must be able to be passed as var parameter }
  3054. if not valid_for_var(aktassignmentnode.left,false) then
  3055. exit;
  3056. { destination must be a simple load so it doesn't need a temp when
  3057. it is evaluated }
  3058. if not is_simple_para_load(aktassignmentnode.left,false) then
  3059. exit;
  3060. { remove possible typecasts }
  3061. realassignmenttarget:=actualtargetnode(@aktassignmentnode.left)^;
  3062. { when the result is returned by value (instead of by writing it to the
  3063. address passed in a hidden parameter), aktassignmentnode.left will
  3064. only be changed once the function has returned and we don't have to
  3065. perform any checks regarding whether it may alias with one of the
  3066. parameters -- unless this is an inline function, in which case
  3067. writes to the function result will directly change it and we do have
  3068. to check for potential aliasing }
  3069. if not paramanager.ret_in_param(resultdef,procdefinition) then
  3070. begin
  3071. if not(cnf_do_inline in callnodeflags) then
  3072. begin
  3073. result:=true;
  3074. exit;
  3075. end
  3076. else
  3077. begin
  3078. { don't replace the function result if we are inlining and if
  3079. the destination is complex, this could lead to lengthy
  3080. code in case the function result is used often and it is
  3081. assigned e.g. to a threadvar }
  3082. if node_complexity(aktassignmentnode.left)>1 then
  3083. exit;
  3084. end;
  3085. end;
  3086. { if the result is the same as the self parameter (in case of objects),
  3087. we can't optimise. We have to check this explicitly because
  3088. hidden parameters such as self have not yet been inserted at this
  3089. point
  3090. }
  3091. if assigned(methodpointer) and
  3092. realassignmenttarget.isequal(actualtargetnode(@methodpointer)^) then
  3093. exit;
  3094. { when we substitute a function result inside an inlined function,
  3095. we may take the address of this function result. Therefore the
  3096. substituted function result may not be in a register, as we cannot
  3097. take its address in that case }
  3098. if (realassignmenttarget.nodetype=temprefn) and
  3099. not(ti_addr_taken in ttemprefnode(realassignmenttarget).tempflags) and
  3100. not(ti_may_be_in_reg in ttemprefnode(realassignmenttarget).tempflags) then
  3101. begin
  3102. result:=not foreachnodestatic(left,@check_funcret_temp_used_as_para,ttemprefnode(realassignmenttarget).tempinfo);
  3103. exit;
  3104. end;
  3105. if (realassignmenttarget.nodetype=loadn) and
  3106. { nested procedures may access the current procedure's locals }
  3107. (procdefinition.parast.symtablelevel=normal_function_level) and
  3108. { must be a local variable, a value para or a hidden function result }
  3109. { parameter (which can be passed by address, but in that case it got }
  3110. { through these same checks at the caller side and is thus safe ) }
  3111. { other option: we're calling a compilerproc, because those don't
  3112. rely on global state
  3113. }
  3114. ((po_compilerproc in procdefinition.procoptions) or
  3115. (
  3116. (
  3117. (tloadnode(realassignmenttarget).symtableentry.typ=localvarsym) or
  3118. (
  3119. (tloadnode(realassignmenttarget).symtableentry.typ=paravarsym) and
  3120. ((tparavarsym(tloadnode(realassignmenttarget).symtableentry).varspez = vs_value) or
  3121. (vo_is_funcret in tparavarsym(tloadnode(realassignmenttarget).symtableentry).varoptions))
  3122. )
  3123. ) and
  3124. { the address may not have been taken of the variable/parameter, because }
  3125. { otherwise it's possible that the called function can access it via a }
  3126. { global variable or other stored state }
  3127. (
  3128. not(tabstractvarsym(tloadnode(realassignmenttarget).symtableentry).addr_taken) and
  3129. (tabstractvarsym(tloadnode(realassignmenttarget).symtableentry).varregable in [vr_none,vr_addr])
  3130. )
  3131. )
  3132. ) then
  3133. begin
  3134. { If the funcret is also used as a parameter we can't optimize because the funcret
  3135. and the parameter will point to the same address. That means that a change of the result variable
  3136. will result also in a change of the parameter value }
  3137. result:=not foreachnodestatic(left,@check_funcret_used_as_para,tloadnode(realassignmenttarget).symtableentry);
  3138. { ensure that it is aligned using the default alignment }
  3139. alignment:=tabstractvarsym(tloadnode(realassignmenttarget).symtableentry).vardef.alignment;
  3140. if (used_align(alignment,target_info.alignment.localalignmin,target_info.alignment.localalignmax)<>
  3141. used_align(alignment,current_settings.alignment.localalignmin,current_settings.alignment.localalignmax)) then
  3142. result:=false;
  3143. exit;
  3144. end;
  3145. end;
  3146. procedure tcallnode.maybe_create_funcret_node;
  3147. var
  3148. temp : ttempcreatenode;
  3149. begin
  3150. if procdefinition.proctypeoption=potype_constructor then
  3151. exit;
  3152. { For the function result we need to create a temp node for:
  3153. - Inlined functions
  3154. - Types requiring initialization/finalization
  3155. - Types passed in parameters }
  3156. if not is_void(resultdef) and
  3157. not assigned(funcretnode) and
  3158. (
  3159. (cnf_do_inline in callnodeflags) or
  3160. is_managed_type(resultdef) or
  3161. paramanager.ret_in_param(resultdef,procdefinition)
  3162. ) then
  3163. begin
  3164. { Optimize calls like x:=f() where we can use x directly as
  3165. result instead of using a temp. Condition is that x cannot be accessed from f().
  3166. This implies that x is a local variable or value parameter of the current block
  3167. and its address is not passed to f. One problem: what if someone takes the
  3168. address of x, puts it in a pointer variable/field and then accesses it that way
  3169. from within the function? This is solved (in a conservative way) using the
  3170. ti_addr_taken flag.
  3171. When the result is not not passed in a parameter there are no problem because
  3172. then it means only reference counted types (eg. ansistrings) that need a decr
  3173. of the refcount before being assigned. This is all done after the call so there
  3174. is no issue with exceptions and possible use of the old value in the called
  3175. function }
  3176. if funcret_can_be_reused then
  3177. begin
  3178. funcretnode:=aktassignmentnode.left.getcopy;
  3179. include(funcretnode.flags,nf_is_funcret);
  3180. { notify the assignment node that the assignment can be removed }
  3181. include(aktassignmentnode.assignmentnodeflags,anf_assign_done_in_right);
  3182. end
  3183. else
  3184. begin
  3185. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,
  3186. (cnf_do_inline in callnodeflags) and
  3187. not(tabstractvarsym(tprocdef(procdefinition).funcretsym).varregable in [vr_none,vr_addr]));
  3188. include(temp.flags,nf_is_funcret);
  3189. { if a managed type is returned by reference, assigning something
  3190. to the result on the caller side will take care of decreasing
  3191. the reference count }
  3192. if paramanager.ret_in_param(resultdef,procdefinition) then
  3193. temp.includetempflag(ti_nofini);
  3194. add_init_statement(temp);
  3195. { When the function result is not used in an inlined function
  3196. we need to delete the temp. This can currently only be done by
  3197. a tempdeletenode and not after converting it to a normal temp }
  3198. if not(cnf_return_value_used in callnodeflags) and
  3199. (cnf_do_inline in callnodeflags) then
  3200. add_done_statement(ctempdeletenode.create(temp))
  3201. else
  3202. add_done_statement(ctempdeletenode.create_normal_temp(temp));
  3203. funcretnode:=ctemprefnode.create(temp);
  3204. include(funcretnode.flags,nf_is_funcret);
  3205. end;
  3206. end;
  3207. end;
  3208. procedure tcallnode.gen_hidden_parameters;
  3209. var
  3210. para : tcallparanode;
  3211. begin
  3212. para:=tcallparanode(left);
  3213. while assigned(para) do
  3214. begin
  3215. { The processing of high() and typeinfo() is already
  3216. done in the typecheckpass. We only need to process the
  3217. nodes that still have a nothingn }
  3218. if (vo_is_hidden_para in para.parasym.varoptions) and
  3219. (para.left.nodetype=nothingn) then
  3220. begin
  3221. { remove dummy nothingn }
  3222. para.left.free;
  3223. para.left:=nil;
  3224. { generate the corresponding nodes for the hidden parameter type }
  3225. if (vo_is_funcret in para.parasym.varoptions) then
  3226. begin
  3227. if not assigned(funcretnode) then
  3228. internalerror(200709083);
  3229. { if funcretnode is a temprefnode, we have to keep it intact
  3230. if it may have been created in maybe_create_funcret_node(),
  3231. because then it will also be destroyed by a
  3232. ctempdeletenode.create_normal_temp() in the cleanup code
  3233. for this call code. In that case we have to copy this
  3234. ttemprefnode after the tempdeletenode to reset its
  3235. tempinfo^.hookoncopy. This is done by copying funcretnode
  3236. in tcallnode.getcopy(), but for that to work we can't reset
  3237. funcretnode to nil here. }
  3238. if (funcretnode.nodetype<>temprefn) or
  3239. (not(cnf_return_value_used in callnodeflags) and
  3240. (cnf_do_inline in callnodeflags)) then
  3241. begin
  3242. para.left:=funcretnode;
  3243. funcretnode:=nil;
  3244. end
  3245. else
  3246. para.left:=funcretnode.getcopy;
  3247. end
  3248. else
  3249. if vo_is_self in para.parasym.varoptions then
  3250. begin
  3251. if assigned(right) then
  3252. para.left:=gen_procvar_context_tree_self
  3253. else
  3254. para.left:=gen_self_tree;
  3255. { make sure that e.g. the self pointer of an advanced
  3256. record does not become a regvar, because it's a vs_var
  3257. parameter }
  3258. if paramanager.push_addr_param(para.parasym.varspez,para.parasym.vardef,
  3259. procdefinition.proccalloption) then
  3260. make_not_regable(para.left,[ra_addr_regable]);
  3261. end
  3262. else
  3263. if vo_is_vmt in para.parasym.varoptions then
  3264. begin
  3265. para.left:=gen_vmt_tree;
  3266. end
  3267. else
  3268. if vo_is_syscall_lib in para.parasym.varoptions then
  3269. gen_syscall_para(para)
  3270. else
  3271. if vo_is_range_check in para.parasym.varoptions then
  3272. begin
  3273. para.left:=cordconstnode.create(Ord(cs_check_range in current_settings.localswitches),pasbool1type,false);
  3274. end
  3275. else
  3276. if vo_is_overflow_check in para.parasym.varoptions then
  3277. begin
  3278. para.left:=cordconstnode.create(Ord(cs_check_overflow in current_settings.localswitches),pasbool1type,false);
  3279. end
  3280. else
  3281. if vo_is_msgsel in para.parasym.varoptions then
  3282. begin
  3283. para.left:=cobjcselectornode.create(cstringconstnode.createstr(tprocdef(procdefinition).messageinf.str^));
  3284. end;
  3285. end;
  3286. if not assigned(para.left) then
  3287. internalerror(200709084);
  3288. para:=tcallparanode(para.right);
  3289. end;
  3290. end;
  3291. procedure tcallnode.verifyabstract(sym:TObject;arg:pointer);
  3292. var
  3293. pd : tprocdef;
  3294. i : longint;
  3295. j : integer;
  3296. hs : string;
  3297. begin
  3298. if (tsym(sym).typ<>procsym) then
  3299. exit;
  3300. for i:=0 to tprocsym(sym).ProcdefList.Count-1 do
  3301. begin
  3302. pd:=tprocdef(tprocsym(sym).ProcdefList[i]);
  3303. hs:=pd.procsym.name+pd.typename_paras([]);
  3304. j:=AbstractMethodsList.FindIndexOf(hs);
  3305. if j<>-1 then
  3306. AbstractMethodsList[j]:=pd
  3307. else
  3308. AbstractMethodsList.Add(hs,pd);
  3309. end;
  3310. end;
  3311. procedure tcallnode.verifyabstractcalls;
  3312. var
  3313. objectdf : tobjectdef;
  3314. parents : tlinkedlist;
  3315. objectinfo : tobjectinfoitem;
  3316. pd : tprocdef;
  3317. i : integer;
  3318. begin
  3319. objectdf := nil;
  3320. { verify if trying to create an instance of a class which contains
  3321. non-implemented abstract methods }
  3322. { first verify this class type, no class than exit }
  3323. { also, this checking can only be done if the constructor is directly
  3324. called, indirect constructor calls cannot be checked.
  3325. }
  3326. if assigned(methodpointer) and
  3327. not((methodpointer.nodetype=loadn) and
  3328. (loadnf_is_self in tloadnode(methodpointer).loadnodeflags)) then
  3329. begin
  3330. if (methodpointer.resultdef.typ = objectdef) then
  3331. objectdf:=tobjectdef(methodpointer.resultdef)
  3332. else
  3333. if (methodpointer.resultdef.typ = classrefdef) and
  3334. (tclassrefdef(methodpointer.resultdef).pointeddef.typ = objectdef) and
  3335. (methodpointer.nodetype in [typen,loadvmtaddrn]) then
  3336. objectdf:=tobjectdef(tclassrefdef(methodpointer.resultdef).pointeddef);
  3337. end;
  3338. if not assigned(objectdf) then
  3339. exit;
  3340. { quick exit if nothing to check }
  3341. if objectdf.abstractcnt = 0 then
  3342. exit;
  3343. parents := tlinkedlist.create;
  3344. AbstractMethodsList := TFPHashList.create;
  3345. { insert all parents in this class : the first item in the
  3346. list will be the base parent of the class .
  3347. }
  3348. while assigned(objectdf) do
  3349. begin
  3350. objectinfo:=tobjectinfoitem.create(objectdf);
  3351. parents.insert(objectinfo);
  3352. objectdf := objectdf.childof;
  3353. end;
  3354. { now all parents are in the correct order
  3355. insert all abstract methods in the list, and remove
  3356. those which are overridden by parent classes.
  3357. }
  3358. objectinfo:=tobjectinfoitem(parents.first);
  3359. while assigned(objectinfo) do
  3360. begin
  3361. objectdf := objectinfo.objinfo;
  3362. if assigned(objectdf.symtable) then
  3363. objectdf.symtable.SymList.ForEachCall(@verifyabstract,nil);
  3364. objectinfo:=tobjectinfoitem(objectinfo.next);
  3365. end;
  3366. if assigned(parents) then
  3367. parents.free;
  3368. { Finally give out a warning for each abstract method still in the list }
  3369. for i:=0 to AbstractMethodsList.Count-1 do
  3370. begin
  3371. pd:=tprocdef(AbstractMethodsList[i]);
  3372. if po_abstractmethod in pd.procoptions then
  3373. begin
  3374. Message2(type_w_instance_with_abstract,objectdf.typesymbolprettyname,pd.customprocname([pno_proctypeoption, pno_paranames,pno_ownername, pno_noclassmarker, pno_prettynames]));
  3375. MessagePos1(pd.fileinfo,sym_h_abstract_method_list,pd.fullprocname(true));
  3376. end;
  3377. end;
  3378. if assigned(AbstractMethodsList) then
  3379. AbstractMethodsList.Free;
  3380. end;
  3381. procedure tcallnode.convert_carg_array_of_const;
  3382. var
  3383. hp : tarrayconstructornode;
  3384. oldleft : tcallparanode;
  3385. begin
  3386. oldleft:=tcallparanode(left);
  3387. if oldleft.left.nodetype<>arrayconstructorn then
  3388. begin
  3389. CGMessage1(type_e_wrong_type_in_array_constructor,oldleft.left.resultdef.typename);
  3390. exit;
  3391. end;
  3392. include(callnodeflags,cnf_uses_varargs);
  3393. { Get arrayconstructor node and insert typeconvs }
  3394. hp:=tarrayconstructornode(oldleft.left);
  3395. { Add c args parameters }
  3396. { It could be an empty set }
  3397. if assigned(hp) and
  3398. assigned(hp.left) then
  3399. begin
  3400. while assigned(hp) do
  3401. begin
  3402. left:=ccallparanode.create(hp.left,left);
  3403. { set callparanode resultdef and flags }
  3404. left.resultdef:=hp.left.resultdef;
  3405. include(tcallparanode(left).callparaflags,cpf_varargs_para);
  3406. hp.left:=nil;
  3407. hp:=tarrayconstructornode(hp.right);
  3408. end;
  3409. end;
  3410. { Remove value of old array of const parameter, but keep it
  3411. in the list because it is required for bind_parasym.
  3412. Generate a nothign to keep callparanoed.left valid }
  3413. oldleft.left.free;
  3414. oldleft.left:=cnothingnode.create;
  3415. end;
  3416. procedure tcallnode.bind_parasym;
  3417. type
  3418. pcallparanode = ^tcallparanode;
  3419. var
  3420. i : integer;
  3421. pt : tcallparanode;
  3422. oldppt : pcallparanode;
  3423. varargspara,
  3424. currpara : tparavarsym;
  3425. hiddentree : tnode;
  3426. paradef : tdef;
  3427. begin
  3428. pt:=tcallparanode(left);
  3429. oldppt:=pcallparanode(@left);
  3430. { flag all callparanodes that belong to the varargs }
  3431. i:=paralength;
  3432. while (i>procdefinition.maxparacount) do
  3433. begin
  3434. include(pt.callparaflags,cpf_varargs_para);
  3435. oldppt:=pcallparanode(@pt.right);
  3436. pt:=tcallparanode(pt.right);
  3437. dec(i);
  3438. end;
  3439. { skip varargs that are inserted by array of const }
  3440. while assigned(pt) and
  3441. (cpf_varargs_para in pt.callparaflags) do
  3442. pt:=tcallparanode(pt.right);
  3443. { process normal parameters and insert hidden parameter nodes, the content
  3444. of the hidden parameters will be updated in pass1 }
  3445. for i:=procdefinition.paras.count-1 downto 0 do
  3446. begin
  3447. currpara:=tparavarsym(procdefinition.paras[i]);
  3448. if vo_is_hidden_para in currpara.varoptions then
  3449. begin
  3450. { Here we handle only the parameters that depend on
  3451. the types of the previous parameter. The typeconversion
  3452. can change the type in the next step. For example passing
  3453. an array can be change to a pointer and a deref.
  3454. We also handle the generation of parentfp parameters, as they
  3455. must all be created before pass_1 on targets that use explicit
  3456. parentfp structs (rather than the frame pointer). The reason
  3457. is that the necessary initialisation code for the these
  3458. structures is attached to the procedure's nodetree after
  3459. the resulttype pass.
  3460. }
  3461. if vo_is_high_para in currpara.varoptions then
  3462. begin
  3463. if not assigned(pt) or (i=0) then
  3464. internalerror(200304081);
  3465. { we need the information of the previous parameter }
  3466. paradef:=tparavarsym(procdefinition.paras[i-1]).vardef;
  3467. hiddentree:=gen_high_tree(pt.left,paradef);
  3468. { for open array of managed type, a copy of high parameter is
  3469. necessary to properly initialize before the call }
  3470. if is_open_array(paradef) and
  3471. (tparavarsym(procdefinition.paras[i-1]).varspez=vs_out) and
  3472. is_managed_type(tarraydef(paradef).elementdef) then
  3473. begin
  3474. typecheckpass(hiddentree);
  3475. {this eliminates double call to fpc_dynarray_high, if any}
  3476. maybe_load_in_temp(hiddentree);
  3477. oldppt^.third:=hiddentree.getcopy;
  3478. end;
  3479. end
  3480. else
  3481. if vo_is_typinfo_para in currpara.varoptions then
  3482. begin
  3483. if not assigned(pt) or (i=0) then
  3484. internalerror(200304082);
  3485. hiddentree:=caddrnode.create_internal(
  3486. crttinode.create(Tstoreddef(pt.resultdef),fullrtti,rdt_normal)
  3487. );
  3488. end
  3489. else if vo_is_parentfp in currpara.varoptions then
  3490. begin
  3491. if assigned(right) and (right.resultdef.typ=procvardef) and
  3492. not tabstractprocdef(right.resultdef).is_addressonly then
  3493. maybe_load_in_temp(right);
  3494. if not assigned(right) then
  3495. begin
  3496. if assigned(procdefinition.owner.defowner) then
  3497. begin
  3498. if paramanager.can_opt_unused_para(currpara) then
  3499. { If parentfp is unused by the target proc, create a dummy
  3500. pointerconstnode which will be discarded later. }
  3501. hiddentree:=cpointerconstnode.create(0,currpara.vardef)
  3502. else
  3503. begin
  3504. hiddentree:=cloadparentfpnode.create(tprocdef(procdefinition.owner.defowner),lpf_forpara);
  3505. if is_nested_pd(current_procinfo.procdef) then
  3506. current_procinfo.set_needs_parentfp(tprocdef(procdefinition.owner.defowner).parast.symtablelevel);
  3507. end;
  3508. end
  3509. { exceptfilters called from main level are not owned }
  3510. else if procdefinition.proctypeoption=potype_exceptfilter then
  3511. hiddentree:=cloadparentfpnode.create(current_procinfo.procdef,lpf_forpara)
  3512. else
  3513. internalerror(200309287);
  3514. end
  3515. else if not(po_is_block in procdefinition.procoptions) then
  3516. hiddentree:=gen_procvar_context_tree_parentfp
  3517. else
  3518. hiddentree:=gen_block_context
  3519. end
  3520. else
  3521. hiddentree:=cnothingnode.create;
  3522. pt:=ccallparanode.create(hiddentree,oldppt^);
  3523. oldppt^:=pt;
  3524. end;
  3525. if not assigned(pt) then
  3526. internalerror(200310052);
  3527. pt.parasym:=currpara;
  3528. oldppt:=pcallparanode(@pt.right);
  3529. pt:=tcallparanode(pt.right);
  3530. end;
  3531. { Create parasyms for varargs, first count the number of varargs paras,
  3532. then insert the parameters with numbering in reverse order. The SortParas
  3533. will set the correct order at the end}
  3534. pt:=tcallparanode(left);
  3535. i:=0;
  3536. while assigned(pt) do
  3537. begin
  3538. if cpf_varargs_para in pt.callparaflags then
  3539. inc(i);
  3540. pt:=tcallparanode(pt.right);
  3541. end;
  3542. if (i>0) then
  3543. begin
  3544. include(current_procinfo.flags,pi_calls_c_varargs);
  3545. varargsparas:=tvarargsparalist.create;
  3546. pt:=tcallparanode(left);
  3547. while assigned(pt) do
  3548. begin
  3549. if cpf_varargs_para in pt.callparaflags then
  3550. begin
  3551. varargspara:=cparavarsym.create('va'+tostr(i),i,vs_value,pt.resultdef,[]);
  3552. dec(i);
  3553. { varargspara is left-right, use insert
  3554. instead of concat }
  3555. varargsparas.add(varargspara);
  3556. pt.parasym:=varargspara;
  3557. end;
  3558. pt:=tcallparanode(pt.right);
  3559. end;
  3560. varargsparas.sortparas;
  3561. end;
  3562. end;
  3563. function tcallnode.pass_typecheck:tnode;
  3564. function is_undefined_recursive(def:tdef):boolean;
  3565. begin
  3566. { might become more refined in the future }
  3567. if def.typ=undefineddef then
  3568. result:=true
  3569. else if def.typ=arraydef then
  3570. result:=is_undefined_recursive(tarraydef(def).elementdef)
  3571. else
  3572. result:=false;
  3573. end;
  3574. var
  3575. candidates : tcallcandidates;
  3576. oldcallnode : tcallnode;
  3577. hpt,tmp : tnode;
  3578. pt : tcallparanode;
  3579. lastpara : longint;
  3580. paraidx,
  3581. cand_cnt : integer;
  3582. i : longint;
  3583. ignoregenericparacall,
  3584. ignorevisibility,
  3585. is_const : boolean;
  3586. statements : tstatementnode;
  3587. converted_result_data : ttempcreatenode;
  3588. calltype: tdispcalltype;
  3589. invokesym : tsym;
  3590. begin
  3591. result:=nil;
  3592. candidates:=nil;
  3593. oldcallnode:=aktcallnode;
  3594. aktcallnode:=self;
  3595. try
  3596. { determine length of parameter list }
  3597. pt:=tcallparanode(left);
  3598. paralength:=0;
  3599. while assigned(pt) do
  3600. begin
  3601. inc(paralength);
  3602. pt:=tcallparanode(pt.right);
  3603. end;
  3604. { determine the type of the parameters }
  3605. if assigned(left) then
  3606. begin
  3607. tcallparanode(left).get_paratype;
  3608. if codegenerror then
  3609. exit;
  3610. end;
  3611. if assigned(methodpointer) then
  3612. typecheckpass(methodpointer);
  3613. { procedure variable ? }
  3614. if assigned(right) then
  3615. begin
  3616. set_varstate(right,vs_read,[vsf_must_be_valid]);
  3617. typecheckpass(right);
  3618. if codegenerror then
  3619. exit;
  3620. if is_invokable(right.resultdef) then
  3621. begin
  3622. procdefinition:=get_invoke_procdef(tobjectdef(right.resultdef));
  3623. if assigned(methodpointer) then
  3624. internalerror(2021041004);
  3625. methodpointer:=right;
  3626. { don't convert again when this is used as the self parameter }
  3627. include(right.flags,nf_load_procvar);
  3628. right:=nil;
  3629. end
  3630. else
  3631. procdefinition:=tabstractprocdef(right.resultdef);
  3632. { Compare parameters from right to left }
  3633. paraidx:=procdefinition.Paras.count-1;
  3634. { Skip default parameters }
  3635. if not(po_varargs in procdefinition.procoptions) then
  3636. begin
  3637. { ignore hidden parameters }
  3638. while (paraidx>=0) and (vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) do
  3639. dec(paraidx);
  3640. for i:=1 to procdefinition.maxparacount-paralength do
  3641. begin
  3642. if paraidx<0 then
  3643. internalerror(200402265);
  3644. if not assigned(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym) then
  3645. begin
  3646. CGMessage1(parser_e_wrong_parameter_size,'<Procedure Variable>');
  3647. exit;
  3648. end;
  3649. dec(paraidx);
  3650. end;
  3651. end;
  3652. while (paraidx>=0) and (vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) do
  3653. dec(paraidx);
  3654. pt:=tcallparanode(left);
  3655. lastpara:=paralength;
  3656. while (paraidx>=0) and assigned(pt) do
  3657. begin
  3658. { only goto next para if we're out of the varargs }
  3659. if not(po_varargs in procdefinition.procoptions) or
  3660. (lastpara<=procdefinition.maxparacount) then
  3661. begin
  3662. repeat
  3663. dec(paraidx);
  3664. until (paraidx<0) or not(vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions);
  3665. end;
  3666. pt:=tcallparanode(pt.right);
  3667. dec(lastpara);
  3668. end;
  3669. if assigned(pt) or
  3670. ((paraidx>=0) and
  3671. not assigned(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym)) then
  3672. begin
  3673. if assigned(pt) then
  3674. current_filepos:=pt.fileinfo;
  3675. CGMessage1(parser_e_wrong_parameter_size,'<Procedure Variable>');
  3676. exit;
  3677. end;
  3678. end
  3679. else
  3680. { not a procedure variable }
  3681. begin
  3682. { do we know the procedure to call ? }
  3683. if not(assigned(procdefinition)) then
  3684. begin
  3685. { according to bug reports 32539 and 20551, real variant of sqr/abs should be used when they are called for variants to be
  3686. delphi compatible, this is in contrast to normal overloading behaviour, so fix this by a terrible hack to be compatible }
  3687. if assigned(left) and assigned(tcallparanode(left).left) and
  3688. (tcallparanode(left).left.resultdef.typ=variantdef) and assigned(symtableproc.name) and (symtableproc.name^='SYSTEM') then
  3689. begin
  3690. if symtableprocentry.Name='SQR' then
  3691. begin
  3692. result:=cinlinenode.createintern(in_sqr_real,false,tcallparanode(left).left.getcopy);
  3693. exit;
  3694. end;
  3695. if symtableprocentry.Name='ABS' then
  3696. begin
  3697. result:=cinlinenode.createintern(in_abs_real,false,tcallparanode(left).left.getcopy);
  3698. exit;
  3699. end;
  3700. end;
  3701. { ignore possible private for properties or in delphi mode for anon. inherited (FK) }
  3702. ignorevisibility:=(nf_isproperty in flags) or
  3703. ((m_delphi in current_settings.modeswitches) and (cnf_anon_inherited in callnodeflags)) or
  3704. (cnf_ignore_visibility in callnodeflags);
  3705. candidates:=tcallcandidates.create(symtableprocentry,symtableproc,left,ignorevisibility,
  3706. not(nf_isproperty in flags),cnf_objc_id_call in callnodeflags,cnf_unit_specified in callnodeflags,
  3707. callnodeflags*[cnf_anon_inherited,cnf_inherited]=[],cnf_anon_inherited in callnodeflags,spezcontext);
  3708. { no procedures found? then there is something wrong
  3709. with the parameter size or the procedures are
  3710. not accessible }
  3711. if candidates.count=0 then
  3712. begin
  3713. { when it's an auto inherited call and there
  3714. is no procedure found, but the procedures
  3715. were defined with overload directive and at
  3716. least two procedures are defined then we ignore
  3717. this inherited by inserting a nothingn. Only
  3718. do this ugly hack in Delphi mode as it looks more
  3719. like a bug. It's also not documented }
  3720. if (m_delphi in current_settings.modeswitches) and
  3721. (cnf_anon_inherited in callnodeflags) and
  3722. (symtableprocentry.owner.symtabletype=ObjectSymtable) and
  3723. (po_overload in tprocdef(symtableprocentry.ProcdefList[0]).procoptions) and
  3724. (symtableprocentry.ProcdefList.Count>=2) then
  3725. result:=cnothingnode.create
  3726. else
  3727. begin
  3728. { in tp mode we can try to convert to procvar if
  3729. there are no parameters specified }
  3730. if not(assigned(left)) and
  3731. ([cnf_inherited,cnf_no_convert_procvar]*callnodeflags=[]) and
  3732. ((m_tp_procvar in current_settings.modeswitches) or
  3733. (m_mac_procvar in current_settings.modeswitches)) and
  3734. (not assigned(methodpointer) or
  3735. (methodpointer.nodetype <> typen)) then
  3736. begin
  3737. hpt:=cloadnode.create(tprocsym(symtableprocentry),symtableproc);
  3738. if assigned(methodpointer) then
  3739. tloadnode(hpt).set_mp(methodpointer.getcopy);
  3740. typecheckpass(hpt);
  3741. result:=hpt;
  3742. end
  3743. else
  3744. begin
  3745. CGMessagePos1(fileinfo,parser_e_wrong_parameter_size,symtableprocentry.realname);
  3746. symtableprocentry.write_parameter_lists(nil);
  3747. end;
  3748. end;
  3749. candidates.free;
  3750. exit;
  3751. end;
  3752. { Retrieve information about the candidates }
  3753. candidates.get_information;
  3754. {$ifdef EXTDEBUG}
  3755. { Display info when multiple candidates are found }
  3756. if candidates.count>1 then
  3757. candidates.dump_info(V_Debug);
  3758. {$endif EXTDEBUG}
  3759. { Choose the best candidate and count the number of
  3760. candidates left }
  3761. cand_cnt:=candidates.choose_best(procdefinition,
  3762. assigned(left) and
  3763. not assigned(tcallparanode(left).right) and
  3764. (tcallparanode(left).left.resultdef.typ=variantdef));
  3765. { All parameters are checked, check if there are any
  3766. procedures left }
  3767. if cand_cnt>0 then
  3768. begin
  3769. { Multiple candidates left? }
  3770. if cand_cnt>1 then
  3771. begin
  3772. { if we're inside a generic and call another function
  3773. with generic types as arguments we don't complain in
  3774. the generic, but only during the specialization }
  3775. ignoregenericparacall:=false;
  3776. if assigned(current_procinfo) and (df_generic in current_procinfo.procdef.defoptions) then
  3777. begin
  3778. pt:=tcallparanode(left);
  3779. while assigned(pt) do
  3780. begin
  3781. if is_undefined_recursive(pt.resultdef) then
  3782. begin
  3783. ignoregenericparacall:=true;
  3784. break;
  3785. end;
  3786. pt:=tcallparanode(pt.right);
  3787. end;
  3788. end;
  3789. if not ignoregenericparacall then
  3790. begin
  3791. CGMessage(type_e_cant_choose_overload_function);
  3792. {$ifdef EXTDEBUG}
  3793. candidates.dump_info(V_Hint);
  3794. {$else EXTDEBUG}
  3795. candidates.list(false);
  3796. {$endif EXTDEBUG}
  3797. end;
  3798. { we'll just use the first candidate to make the
  3799. call }
  3800. end;
  3801. { assign procdefinition }
  3802. if symtableproc=nil then
  3803. symtableproc:=procdefinition.owner;
  3804. end
  3805. else
  3806. begin
  3807. { No candidates left, this must be a type error,
  3808. because wrong size is already checked. procdefinition
  3809. is filled with the first (random) definition that is
  3810. found. We use this definition to display a nice error
  3811. message that the wrong type is passed }
  3812. candidates.find_wrong_para;
  3813. candidates.list(true);
  3814. {$ifdef EXTDEBUG}
  3815. candidates.dump_info(V_Hint);
  3816. {$endif EXTDEBUG}
  3817. { We can not proceed, release all procs and exit }
  3818. candidates.free;
  3819. exit;
  3820. end;
  3821. { if the final procedure definition is not yet owned,
  3822. ensure that it is }
  3823. procdefinition.register_def;
  3824. if procdefinition.is_specialization and (procdefinition.typ=procdef) then
  3825. maybe_add_pending_specialization(procdefinition,candidates.para_anon_syms);
  3826. candidates.free;
  3827. end; { end of procedure to call determination }
  3828. end;
  3829. if procdefinition.typ = procdef then
  3830. begin
  3831. { check for hints (deprecated etc) }
  3832. check_hints(tprocdef(procdefinition).procsym,tprocdef(procdefinition).symoptions,tprocdef(procdefinition).deprecatedmsg);
  3833. { add reference to corresponding procsym; may not be the one
  3834. originally found/passed to the constructor because of overloads }
  3835. addsymref(tprocdef(procdefinition).procsym,procdefinition);
  3836. { ensure that the generic is considered as used as for an
  3837. implicit specialization must only be called after the final
  3838. overload was picked }
  3839. if assigned(tprocdef(procdefinition).genericdef) and
  3840. assigned(tprocdef(tprocdef(procdefinition).genericdef).procsym) and
  3841. (tprocdef(tprocdef(procdefinition).genericdef).procsym.refs=0) then
  3842. addsymref(tprocdef(tprocdef(procdefinition).genericdef).procsym);
  3843. end;
  3844. { add needed default parameters }
  3845. if (paralength<procdefinition.maxparacount) then
  3846. begin
  3847. paraidx:=0;
  3848. i:=0;
  3849. while (i<paralength) do
  3850. begin
  3851. if paraidx>=procdefinition.Paras.count then
  3852. internalerror(200306181);
  3853. if not(vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) then
  3854. inc(i);
  3855. inc(paraidx);
  3856. end;
  3857. while (paraidx<procdefinition.paras.count) and (vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) do
  3858. inc(paraidx);
  3859. while (paraidx<procdefinition.paras.count) do
  3860. begin
  3861. if not assigned(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym) then
  3862. internalerror(200212142);
  3863. left:=ccallparanode.create(genconstsymtree(
  3864. tconstsym(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym)),left);
  3865. { Ignore vs_hidden parameters }
  3866. repeat
  3867. inc(paraidx);
  3868. until (paraidx>=procdefinition.paras.count) or
  3869. not(vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions);
  3870. end;
  3871. end;
  3872. { recursive call? }
  3873. if assigned(current_procinfo) and
  3874. (procdefinition=current_procinfo.procdef) then
  3875. include(current_procinfo.flags,pi_is_recursive);
  3876. { handle predefined procedures }
  3877. is_const:=(po_internconst in procdefinition.procoptions) and
  3878. ((block_type in [bt_const,bt_type,bt_const_type,bt_var_type]) or
  3879. (assigned(left) and ((tcallparanode(left).left.nodetype in [realconstn,ordconstn])
  3880. and (not assigned(tcallparanode(left).right) or (tcallparanode(tcallparanode(left).right).left.nodetype in [realconstn,ordconstn])))));
  3881. if (procdefinition.proccalloption=pocall_internproc) or is_const then
  3882. begin
  3883. if assigned(left) then
  3884. begin
  3885. { convert types to those of the prototype, this is required by functions like ror, rol, sar
  3886. some use however a dummy type (Typedfile) so this would break them }
  3887. if not(tinlinenumber(tprocdef(procdefinition).extnumber) in
  3888. [in_Reset_TypedFile,in_Rewrite_TypedFile,in_reset_typedfile_name,in_rewrite_typedfile_name]) then
  3889. begin
  3890. { bind parasyms to the callparanodes and insert hidden parameters }
  3891. bind_parasym;
  3892. { insert type conversions for parameters }
  3893. if assigned(left) then
  3894. tcallparanode(left).insert_typeconv;
  3895. end;
  3896. { ptr and settextbuf need two args }
  3897. if assigned(tcallparanode(left).right) then
  3898. begin
  3899. hpt:=geninlinenode(tinlinenumber(tprocdef(procdefinition).extnumber),is_const,left);
  3900. left:=nil;
  3901. end
  3902. else
  3903. begin
  3904. hpt:=geninlinenode(tinlinenumber(tprocdef(procdefinition).extnumber),is_const,tcallparanode(left).left);
  3905. tcallparanode(left).left:=nil;
  3906. end;
  3907. end
  3908. else
  3909. hpt:=geninlinenode(tinlinenumber(tprocdef(procdefinition).extnumber),is_const,nil);
  3910. result:=hpt;
  3911. exit;
  3912. end;
  3913. { in case this is an Objective-C message that returns a related object type by convention,
  3914. override the default result type }
  3915. if po_objc_related_result_type in procdefinition.procoptions then
  3916. begin
  3917. { don't crash in case of syntax errors }
  3918. if assigned(methodpointer) then
  3919. begin
  3920. include(callnodeflags,cnf_typedefset);
  3921. typedef:=methodpointer.resultdef;
  3922. if typedef.typ=classrefdef then
  3923. typedef:=tclassrefdef(typedef).pointeddef;
  3924. end;
  3925. end;
  3926. { ensure that the result type is set }
  3927. if not(cnf_typedefset in callnodeflags) then
  3928. begin
  3929. { constructors return their current class type, not the type where the
  3930. constructor is declared, this can be different because of inheritance }
  3931. if (procdefinition.proctypeoption=potype_constructor) and
  3932. assigned(methodpointer) and
  3933. assigned(methodpointer.resultdef) and
  3934. (methodpointer.resultdef.typ=classrefdef) then
  3935. resultdef:=tclassrefdef(methodpointer.resultdef).pointeddef
  3936. else
  3937. { Member call to a (inherited) constructor from the class, the return
  3938. value is always self, so we change it to voidtype to generate an
  3939. error and to prevent users from generating non-working code
  3940. when they expect to clone the current instance, see bug 3662 (PFV) }
  3941. if (procdefinition.proctypeoption=potype_constructor) and
  3942. is_class(tprocdef(procdefinition).struct) and
  3943. assigned(methodpointer) and
  3944. (methodpointer.nodetype=loadn) and
  3945. (loadnf_is_self in tloadnode(methodpointer).loadnodeflags) then
  3946. resultdef:=voidtype
  3947. else
  3948. resultdef:=procdefinition.returndef;
  3949. end
  3950. else
  3951. resultdef:=typedef;
  3952. { Check object/class for methods }
  3953. if assigned(methodpointer) then
  3954. begin
  3955. { direct call to inherited abstract method, then we
  3956. can already give a error in the compiler instead
  3957. of a runtime error }
  3958. if (cnf_inherited in callnodeflags) and
  3959. (po_abstractmethod in procdefinition.procoptions) then
  3960. begin
  3961. if (m_delphi in current_settings.modeswitches) and
  3962. (cnf_anon_inherited in callnodeflags) then
  3963. begin
  3964. CGMessage(cg_h_inherited_ignored);
  3965. result:=cnothingnode.create;
  3966. exit;
  3967. end
  3968. else
  3969. CGMessage(cg_e_cant_call_abstract_method);
  3970. end;
  3971. { directly calling an interface/protocol/category/class helper
  3972. method via its type is not possible (always must be called via
  3973. the actual instance) }
  3974. if (methodpointer.nodetype=typen) and
  3975. ((
  3976. is_interface(methodpointer.resultdef) and not
  3977. is_objectpascal_helper(tdef(procdefinition.owner.defowner))
  3978. ) or
  3979. is_objc_protocol_or_category(methodpointer.resultdef)) then
  3980. CGMessage1(type_e_class_type_expected,methodpointer.resultdef.typename);
  3981. { if an inherited con- or destructor should be }
  3982. { called in a con- or destructor then a warning }
  3983. { will be made }
  3984. { con- and destructors need a pointer to the vmt }
  3985. if (cnf_inherited in callnodeflags) and
  3986. (procdefinition.proctypeoption in [potype_constructor,potype_destructor]) and
  3987. is_object(methodpointer.resultdef) and
  3988. not(current_procinfo.procdef.proctypeoption in [potype_constructor,potype_destructor]) then
  3989. CGMessage(cg_w_member_cd_call_from_method);
  3990. if methodpointer.nodetype<>typen then
  3991. begin
  3992. { if the value a type helper works on is a derefentiation (before
  3993. removing postix operators) we need to pass the original pointer
  3994. as Self as the Self value might be changed by the helper }
  3995. if is_objectpascal_helper(tdef(procdefinition.owner.defowner)) and
  3996. not is_implicit_pointer_object_type(tobjectdef(procdefinition.owner.defowner).extendeddef) then
  3997. begin
  3998. hpt:=methodpointer;
  3999. hpt:=actualtargetnode(@hpt)^;
  4000. if hpt.nodetype=derefn then
  4001. begin
  4002. tmp:=tderefnode(hpt).left;
  4003. tderefnode(hpt).left:=nil;
  4004. methodpointer.free;
  4005. methodpointer:=tmp;
  4006. end;
  4007. end;
  4008. hpt:=methodpointer;
  4009. { Remove all postfix operators }
  4010. while assigned(hpt) and (hpt.nodetype in [subscriptn,vecn]) do
  4011. hpt:=tunarynode(hpt).left;
  4012. if ((hpt.nodetype=loadvmtaddrn) or
  4013. ((hpt.nodetype=loadn) and assigned(tloadnode(hpt).resultdef) and (tloadnode(hpt).resultdef.typ=classrefdef))) and
  4014. not (procdefinition.proctypeoption=potype_constructor) and
  4015. not (po_classmethod in procdefinition.procoptions) and
  4016. not (po_staticmethod in procdefinition.procoptions) then
  4017. { error: we are calling instance method from the class method/static method }
  4018. CGMessage(parser_e_only_class_members);
  4019. if (procdefinition.proctypeoption=potype_constructor) and
  4020. assigned(symtableproc) and
  4021. (symtableproc.symtabletype=withsymtable) and
  4022. (tnode(twithsymtable(symtableproc).withrefnode).nodetype=temprefn) then
  4023. CGmessage(cg_e_cannot_call_cons_dest_inside_with);
  4024. { skip (absolute and other simple) type conversions -- only now,
  4025. because the checks above have to take type conversions into
  4026. e.g. class reference types account }
  4027. hpt:=actualtargetnode(@hpt)^;
  4028. { R.Init then R will be initialized by the constructor,
  4029. Also allow it for simple loads }
  4030. if (procdefinition.proctypeoption=potype_constructor) or
  4031. ((hpt.nodetype=loadn) and
  4032. (((methodpointer.resultdef.typ=objectdef) and
  4033. not(oo_has_virtual in tobjectdef(methodpointer.resultdef).objectoptions)) or
  4034. (methodpointer.resultdef.typ=recorddef)
  4035. )
  4036. ) then
  4037. { a constructor will and a method may write something to }
  4038. { the fields }
  4039. set_varstate(methodpointer,vs_readwritten,[])
  4040. else
  4041. set_varstate(methodpointer,vs_read,[vsf_must_be_valid]);
  4042. end;
  4043. { if we are calling the constructor check for abstract
  4044. methods. Ignore inherited and member calls, because the
  4045. class is then already created }
  4046. if (procdefinition.proctypeoption=potype_constructor) and
  4047. not(cnf_inherited in callnodeflags) and
  4048. not(cnf_member_call in callnodeflags) then
  4049. verifyabstractcalls;
  4050. end
  4051. else
  4052. begin
  4053. { When this is method the methodpointer must be available }
  4054. if (right=nil) and
  4055. (procdefinition.owner.symtabletype in [ObjectSymtable,recordsymtable]) and
  4056. not procdefinition.no_self_node then
  4057. internalerror(200305061);
  4058. end;
  4059. { Set flag that the procedure uses varargs, also if they are not passed it is still
  4060. needed for x86_64 to pass the number of SSE registers used }
  4061. if po_varargs in procdefinition.procoptions then
  4062. include(callnodeflags,cnf_uses_varargs);
  4063. { set the appropriate node flag if the call never returns }
  4064. if po_noreturn in procdefinition.procoptions then
  4065. include(callnodeflags,cnf_call_never_returns);
  4066. { Change loading of array of const to varargs }
  4067. if assigned(left) and
  4068. is_array_of_const(tparavarsym(procdefinition.paras[procdefinition.paras.count-1]).vardef) and
  4069. (procdefinition.proccalloption in cdecl_pocalls) then
  4070. convert_carg_array_of_const;
  4071. { bind parasyms to the callparanodes and insert hidden parameters }
  4072. bind_parasym;
  4073. { insert type conversions for parameters }
  4074. if assigned(left) then
  4075. tcallparanode(left).insert_typeconv;
  4076. { dispinterface methode invoke? }
  4077. if assigned(methodpointer) and is_dispinterface(methodpointer.resultdef) then
  4078. begin
  4079. case procdefinition.proctypeoption of
  4080. potype_propgetter: calltype:=dct_propget;
  4081. potype_propsetter: calltype:=dct_propput;
  4082. else
  4083. calltype:=dct_method;
  4084. end;
  4085. { if the result is used, we've to insert a call to convert the type to be on the "safe side" }
  4086. if (cnf_return_value_used in callnodeflags) and not is_void(procdefinition.returndef) then
  4087. begin
  4088. result:=internalstatements(statements);
  4089. converted_result_data:=ctempcreatenode.create(procdefinition.returndef,sizeof(procdefinition.returndef),
  4090. tt_persistent,true);
  4091. addstatement(statements,converted_result_data);
  4092. addstatement(statements,cassignmentnode.create(ctemprefnode.create(converted_result_data),
  4093. ctypeconvnode.create_internal(
  4094. translate_disp_call(methodpointer,parameters,calltype,'',tprocdef(procdefinition).dispid,procdefinition.returndef),
  4095. procdefinition.returndef)));
  4096. addstatement(statements,ctempdeletenode.create_normal_temp(converted_result_data));
  4097. addstatement(statements,ctemprefnode.create(converted_result_data));
  4098. end
  4099. else
  4100. result:=translate_disp_call(methodpointer,parameters,calltype,'',tprocdef(procdefinition).dispid,voidtype);
  4101. { don't free reused nodes }
  4102. methodpointer:=nil;
  4103. parameters:=nil;
  4104. end;
  4105. maybe_gen_call_self_node;
  4106. if assigned(call_self_node) then
  4107. typecheckpass(call_self_node);
  4108. if assigned(call_vmt_node) then
  4109. typecheckpass(call_vmt_node);
  4110. if assigned(current_procinfo) and
  4111. (procdefinition.typ=procdef) and
  4112. (procdefinition.parast.symtablelevel<=current_procinfo.procdef.parast.symtablelevel) and
  4113. (procdefinition.parast.symtablelevel>normal_function_level) and
  4114. (current_procinfo.procdef.parast.symtablelevel>normal_function_level) then
  4115. current_procinfo.add_captured_sym(tprocdef(procdefinition).procsym,procdefinition,fileinfo);
  4116. finally
  4117. aktcallnode:=oldcallnode;
  4118. end;
  4119. end;
  4120. function tcallnode.simplify(forinline : boolean) : tnode;
  4121. begin
  4122. { See if there's any special handling we can do based on the intrinsic code }
  4123. if (intrinsiccode <> Default(TInlineNumber)) then
  4124. result := handle_compilerproc
  4125. else
  4126. result := nil;
  4127. end;
  4128. procedure tcallnode.order_parameters;
  4129. var
  4130. hp,hpcurr,hpnext,hpfirst,hpprev : tcallparanode;
  4131. currloc : tcgloc;
  4132. indexcount: Integer;
  4133. begin
  4134. indexcount:=0;
  4135. hpfirst:=nil;
  4136. hpcurr:=tcallparanode(left);
  4137. { cache all info about parameters containing stack tainting calls,
  4138. since we will need it a lot below and calculting it can be expensive }
  4139. while assigned(hpcurr) do
  4140. begin
  4141. { Also remember the original parameter order for the sake of
  4142. tcallnode.simplify }
  4143. if hpcurr.originalindex = -1 then
  4144. begin
  4145. hpcurr.originalindex := indexcount;
  4146. Inc(indexcount);
  4147. end;
  4148. hpcurr.init_contains_stack_tainting_call_cache;
  4149. hpcurr:=tcallparanode(hpcurr.right);
  4150. end;
  4151. hpcurr:=tcallparanode(left);
  4152. while assigned(hpcurr) do
  4153. begin
  4154. { pull out }
  4155. hpnext:=tcallparanode(hpcurr.right);
  4156. { pull in at the correct place.
  4157. Used order:
  4158. 1. vs_out for a reference-counted type
  4159. 2. LOC_REFERENCE with smallest offset (i386 only)
  4160. 3. LOC_REFERENCE with least complexity (non-i386 only)
  4161. 4. LOC_REFERENCE with most complexity (non-i386 only)
  4162. 5. LOC_REGISTER with most complexity
  4163. 6. LOC_REGISTER with least complexity
  4164. For the moment we only look at the first parameter field. Combining it
  4165. with multiple parameter fields will make things a lot complexer (PFV)
  4166. The reason for the difference regarding complexity ordering
  4167. between LOC_REFERENCE and LOC_REGISTER is mainly for calls:
  4168. we first want to treat the LOC_REFERENCE destinations whose
  4169. calculation does not require a call, because their location
  4170. may contain registers which might otherwise have to be saved
  4171. if a call has to be evaluated first. The calculated value is
  4172. stored on the stack and will thus no longer occupy any
  4173. register.
  4174. Similarly, for the register parameters we first want to
  4175. evaluate the calls, because otherwise the already loaded
  4176. register parameters will have to be saved so the intermediate
  4177. call can be evaluated (JM) }
  4178. if not assigned(hpcurr.parasym.paraloc[callerside].location) then
  4179. internalerror(200412152);
  4180. currloc:=hpcurr.parasym.paraloc[callerside].location^.loc;
  4181. hpprev:=nil;
  4182. hp:=hpfirst;
  4183. { on fixed_stack targets, always evaluate parameters containing
  4184. a call with stack parameters before all other parameters,
  4185. because they will prevent any other parameters from being put
  4186. in their final place; if both the current and the next para
  4187. contain a stack tainting call, don't do anything to prevent
  4188. them from keeping on chasing eachother's tail }
  4189. while assigned(hp) do
  4190. begin
  4191. if paramanager.use_fixed_stack and
  4192. hpcurr.contains_stack_tainting_call_cached then
  4193. break;
  4194. case currloc of
  4195. LOC_REFERENCE :
  4196. begin
  4197. case hp.parasym.paraloc[callerside].location^.loc of
  4198. LOC_REFERENCE :
  4199. begin
  4200. { Offset is calculated like:
  4201. sub esp,12
  4202. mov [esp+8],para3
  4203. mov [esp+4],para2
  4204. mov [esp],para1
  4205. call function
  4206. That means the for pushes the para with the
  4207. highest offset (see para3) needs to be pushed first
  4208. }
  4209. {$if defined(i386) or defined(i8086) or defined(m68k) or defined(z80)}
  4210. { the i386, i8086, m68k, z80 and jvm code generators expect all reference }
  4211. { parameters to be in this order so they can use }
  4212. { pushes in case of no fixed stack }
  4213. if (not paramanager.use_fixed_stack and
  4214. (hpcurr.parasym.paraloc[callerside].location^.reference.offset>
  4215. hp.parasym.paraloc[callerside].location^.reference.offset)) or
  4216. (paramanager.use_fixed_stack and
  4217. (node_complexity(hpcurr.left)<node_complexity(hp.left))) then
  4218. {$elseif defined(jvm) or defined(wasm)}
  4219. if (hpcurr.parasym.paraloc[callerside].location^.reference.offset<hp.parasym.paraloc[callerside].location^.reference.offset) then
  4220. {$else jvm}
  4221. if (node_complexity(hpcurr.left)<node_complexity(hp.left)) then
  4222. {$endif jvm}
  4223. break;
  4224. end;
  4225. LOC_MMREGISTER,
  4226. LOC_REGISTER,
  4227. LOC_FPUREGISTER :
  4228. break;
  4229. else
  4230. ;
  4231. end;
  4232. end;
  4233. LOC_MMREGISTER,
  4234. LOC_FPUREGISTER,
  4235. LOC_REGISTER :
  4236. begin
  4237. if (hp.parasym.paraloc[callerside].location^.loc<>LOC_REFERENCE) and
  4238. (node_complexity(hpcurr.left)>node_complexity(hp.left)) then
  4239. break;
  4240. end;
  4241. else
  4242. ;
  4243. end;
  4244. hpprev:=hp;
  4245. hp:=tcallparanode(hp.right);
  4246. end;
  4247. hpcurr.right:=hp;
  4248. if assigned(hpprev) then
  4249. hpprev.right:=hpcurr
  4250. else
  4251. hpfirst:=hpcurr;
  4252. { next }
  4253. hpcurr:=hpnext;
  4254. end;
  4255. left:=hpfirst;
  4256. { now mark each parameter that is followed by a stack-tainting call,
  4257. to determine on use_fixed_stack targets which ones can immediately be
  4258. put in their final destination. Unforunately we can never put register
  4259. parameters immediately in their final destination (even on register-
  4260. rich architectures such as the PowerPC), because the code generator
  4261. can still insert extra calls that only make use of register
  4262. parameters (fpc_move() etc. }
  4263. hpcurr:=hpfirst;
  4264. while assigned(hpcurr) do
  4265. begin
  4266. if hpcurr.contains_stack_tainting_call_cached then
  4267. begin
  4268. { all parameters before this one are followed by a stack
  4269. tainting call }
  4270. hp:=hpfirst;
  4271. while hp<>hpcurr do
  4272. begin
  4273. hp.ffollowed_by_stack_tainting_call_cached:=true;
  4274. hp:=tcallparanode(hp.right);
  4275. end;
  4276. hpfirst:=hpcurr;
  4277. end;
  4278. hpcurr:=tcallparanode(hpcurr.right);
  4279. end;
  4280. end;
  4281. procedure tcallnode.check_stack_parameters;
  4282. var
  4283. hp : tcallparanode;
  4284. loc : pcgparalocation;
  4285. begin
  4286. hp:=tcallparanode(left);
  4287. while assigned(hp) do
  4288. begin
  4289. if assigned(hp.parasym) then
  4290. begin
  4291. loc:=hp.parasym.paraloc[callerside].location;
  4292. while assigned(loc) do
  4293. begin
  4294. if loc^.loc=LOC_REFERENCE then
  4295. begin
  4296. include(current_procinfo.flags,pi_has_stackparameter);
  4297. exit;
  4298. end;
  4299. loc:=loc^.next;
  4300. end;
  4301. end;
  4302. hp:=tcallparanode(hp.right);
  4303. end;
  4304. end;
  4305. procedure tcallnode.check_inlining;
  4306. var
  4307. st : tsymtable;
  4308. para : tcallparanode;
  4309. begin
  4310. { Can we inline the procedure? }
  4311. if (po_inline in procdefinition.procoptions) and
  4312. (procdefinition.typ=procdef) and
  4313. tprocdef(procdefinition).has_inlininginfo and
  4314. { Prevent too deep inlining recursion and code bloat by inlining
  4315. The actual formuala is
  4316. inlinelevel/3+1 /-------
  4317. node count < -----------------\/ 10000
  4318. This allows exponential grow of the code only to a certain limit.
  4319. Remarks
  4320. - The current approach calculates the inlining level top down, so outer call nodes (nodes closer to the leaf) might not be inlined
  4321. if the max. complexity is reached. This is done because it makes the implementation easier and because
  4322. there might be situations were it is more beneficial to inline inner nodes and do the calls to the outer nodes
  4323. if the outer nodes are in a seldomly used code path
  4324. - The code avoids to use functions from the math unit
  4325. }
  4326. (node_count(tprocdef(procdefinition).inlininginfo^.code)<round(exp((1.0/(inlinelevel/3.0+1))*ln(10000)))) then
  4327. begin
  4328. include(callnodeflags,cnf_do_inline);
  4329. { Check if we can inline the procedure when it references proc/var that
  4330. are not in the globally available }
  4331. st:=procdefinition.owner;
  4332. while (st.symtabletype in [ObjectSymtable,recordsymtable]) do
  4333. st:=st.defowner.owner;
  4334. if not(tf_supports_hidden_symbols in target_info.flags) and
  4335. (pi_uses_static_symtable in tprocdef(procdefinition).inlininginfo^.flags) and
  4336. (st.symtabletype=globalsymtable) and
  4337. (not st.iscurrentunit) then
  4338. begin
  4339. Comment(V_lineinfo+V_Debug,'Not inlining "'+tprocdef(procdefinition).procsym.realname+'", references private symbols from other unit');
  4340. exclude(callnodeflags,cnf_do_inline);
  4341. end;
  4342. para:=tcallparanode(parameters);
  4343. while assigned(para) do
  4344. begin
  4345. if not para.can_be_inlined then
  4346. begin
  4347. Comment(V_lineinfo+V_Debug,'Not inlining "'+tprocdef(procdefinition).procsym.realname+
  4348. '", invocation parameter contains an unsafe/unsupported construct');
  4349. exclude(callnodeflags,cnf_do_inline);
  4350. break;
  4351. end;
  4352. para:=tcallparanode(para.nextpara);
  4353. end;
  4354. end;
  4355. end;
  4356. function tcallnode.pass_1 : tnode;
  4357. procedure mark_unregable_parameters;
  4358. var
  4359. hp : tcallparanode;
  4360. begin
  4361. hp:=tcallparanode(left);
  4362. while assigned(hp) do
  4363. begin
  4364. do_typecheckpass(hp.left);
  4365. { When the address needs to be pushed then the register is
  4366. not regable. Exception is when the location is also a var
  4367. parameter and we can pass the address transparently (but
  4368. that is handled by make_not_regable if ra_addr_regable is
  4369. passed, and make_not_regable always needs to called for
  4370. the ra_addr_taken info for non-invisble parameters) }
  4371. if (not (cpf_varargs_para in hp.callparaflags)) and (
  4372. not(
  4373. (vo_is_hidden_para in hp.parasym.varoptions) and
  4374. (hp.left.resultdef.typ in [pointerdef,classrefdef])
  4375. ) and
  4376. paramanager.push_addr_param(hp.parasym.varspez,hp.parasym.vardef,
  4377. self.procdefinition.proccalloption)
  4378. ) then
  4379. { pushing the address of a variable to take the place of a temp
  4380. as the complex function result of a function does not make its
  4381. address escape the current block, as the "address of the
  4382. function result" is not something which can be stored
  4383. persistently by the callee (it becomes invalid when the callee
  4384. returns) }
  4385. if not(vo_is_funcret in hp.parasym.varoptions) and
  4386. not(po_compilerproc in procdefinition.procoptions) then
  4387. make_not_regable(hp.left,[ra_addr_regable,ra_addr_taken])
  4388. else
  4389. make_not_regable(hp.left,[ra_addr_regable]);
  4390. hp:=tcallparanode(hp.right);
  4391. end;
  4392. end;
  4393. var
  4394. para: tcallparanode;
  4395. oldcallnode: tcallnode;
  4396. begin
  4397. result:=nil;
  4398. oldcallnode:=aktcallnode;
  4399. aktcallnode:=self;
  4400. try
  4401. { as pass_1 is never called on the methodpointer node, we must check
  4402. here that it's not a helper type }
  4403. if assigned(methodpointer) and
  4404. (methodpointer.nodetype=typen) and
  4405. is_objectpascal_helper(ttypenode(methodpointer).typedef) and
  4406. not ttypenode(methodpointer).helperallowed then
  4407. begin
  4408. CGMessage(parser_e_no_category_as_types);
  4409. { we get an internal error when trying to insert the hidden
  4410. parameters in this case }
  4411. exit;
  4412. end;
  4413. { can we get rid of the call? }
  4414. if (cs_opt_remove_empty_proc in current_settings.optimizerswitches) and
  4415. not(cnf_return_value_used in callnodeflags) and
  4416. (procdefinition.typ=procdef) and
  4417. tprocdef(procdefinition).isempty and
  4418. { allow only certain proc options }
  4419. ((tprocdef(procdefinition).procoptions-[po_none,po_classmethod,po_staticmethod,
  4420. po_interrupt,po_iocheck,po_assembler,po_msgstr,po_msgint,po_exports,po_external,po_overload,
  4421. po_nostackframe,po_has_mangledname,po_has_public_name,po_forward,po_global,
  4422. po_inline,po_compilerproc,po_has_importdll,po_has_importname,po_kylixlocal,po_dispid,po_delphi_nested_cc,
  4423. po_rtlproc,po_ignore_for_overload_resolution,po_auto_raised_visibility])=[]) then
  4424. begin
  4425. { check parameters for side effects }
  4426. para:=tcallparanode(left);
  4427. while assigned(para) do
  4428. begin
  4429. if (para.parasym.typ = paravarsym) and
  4430. ((para.parasym.refs>0) or
  4431. { array of consts are converted later on so we need to skip them here
  4432. else no error detection is done }
  4433. is_array_of_const(para.parasym.vardef) or
  4434. not(cs_opt_dead_values in current_settings.optimizerswitches) or
  4435. might_have_sideeffects(para.left)) then
  4436. break;
  4437. para:=tcallparanode(para.right);
  4438. end;
  4439. { finally, remove it if no parameter with side effect has been found }
  4440. if para=nil then
  4441. begin
  4442. result:=cnothingnode.create;
  4443. exit;
  4444. end;
  4445. end;
  4446. { convert Objective-C calls into a message call }
  4447. if (procdefinition.typ=procdef) and
  4448. (po_objc in tprocdef(procdefinition).procoptions) then
  4449. begin
  4450. if not(cnf_objc_processed in callnodeflags) then
  4451. objc_convert_to_message_send;
  4452. end
  4453. else
  4454. begin
  4455. { The following don't apply to obj-c: obj-c methods can never be
  4456. inlined because they're always virtual and the destination can
  4457. change at run, and for the same reason we also can't perform
  4458. WPO on them (+ they have no constructors) }
  4459. { Check if the call can be inlined, sets the cnf_do_inline flag }
  4460. check_inlining;
  4461. { must be called before maybe_load_in_temp(methodpointer), because
  4462. it converts the methodpointer into a temp in case it's a call
  4463. (and we want to know the original call)
  4464. }
  4465. register_created_object_types;
  4466. end;
  4467. { Maybe optimize the loading of the methodpointer using a temp. When the methodpointer
  4468. is a calln this is even required to not execute the calln twice.
  4469. This needs to be done after the resulttype pass, because in the resulttype we can still convert the
  4470. calln to a loadn (PFV) }
  4471. if assigned(methodpointer) then
  4472. maybe_load_in_temp(methodpointer);
  4473. if assigned(right) and (right.resultdef.typ=procvardef) and
  4474. not tabstractprocdef(right.resultdef).is_addressonly then
  4475. maybe_load_in_temp(right);
  4476. { the return value might be stored on the current stack by allocating a temp. }
  4477. if not(paramanager.ret_in_param(procdefinition.returndef,procdefinition)) then
  4478. inc(current_procinfo.estimatedtempsize,procdefinition.returndef.size);
  4479. { Create destination (temp or assignment-variable reuse) for function result if it not yet set }
  4480. maybe_create_funcret_node;
  4481. { Insert the self,vmt,function result in the parameters }
  4482. gen_hidden_parameters;
  4483. { Remove useless nodes from init/final blocks }
  4484. { (simplify depends on typecheck info) }
  4485. if assigned(callinitblock) then
  4486. begin
  4487. typecheckpass(tnode(callinitblock));
  4488. doinlinesimplify(tnode(callinitblock));
  4489. end;
  4490. if assigned(callcleanupblock) then
  4491. begin
  4492. typecheckpass(tnode(callcleanupblock));
  4493. doinlinesimplify(tnode(callcleanupblock));
  4494. end;
  4495. { If a constructor calls another constructor of the same or of an
  4496. inherited class, some targets (jvm) have to generate different
  4497. entry code for the constructor. }
  4498. if (current_procinfo.procdef.proctypeoption=potype_constructor) and
  4499. (procdefinition.typ=procdef) and
  4500. (tprocdef(procdefinition).proctypeoption=potype_constructor) and
  4501. ([cnf_member_call,cnf_inherited] * callnodeflags <> []) then
  4502. current_procinfo.ConstructorCallingConstructor:=true;
  4503. { Continue with checking a normal call or generate the inlined code }
  4504. if cnf_do_inline in callnodeflags then
  4505. result:=pass1_inline
  4506. else
  4507. begin
  4508. if (po_inline in procdefinition.procoptions) and not(po_compilerproc in procdefinition.procoptions) and
  4509. (procdefinition.typ=procdef) and
  4510. not (pio_inline_not_possible in tprocdef(procdefinition).implprocoptions) then
  4511. begin
  4512. Message1(cg_n_no_inline,tprocdef(procdefinition).customprocname([pno_proctypeoption, pno_paranames,pno_ownername, pno_noclassmarker, pno_prettynames]));
  4513. end;
  4514. mark_unregable_parameters;
  4515. result:=pass1_normal;
  4516. end;
  4517. finally
  4518. aktcallnode:=oldcallnode;
  4519. end;
  4520. end;
  4521. function tcallnode.pass1_normal : tnode;
  4522. begin
  4523. result:=nil;
  4524. { calculate the parameter info for the procdef }
  4525. procdefinition.init_paraloc_info(callerside);
  4526. { calculate the parameter size needed for this call include varargs if they are available }
  4527. if assigned(varargsparas) then
  4528. pushedparasize:=paramanager.create_varargs_paraloc_info(procdefinition,callerside,varargsparas)
  4529. else
  4530. pushedparasize:=procdefinition.callerargareasize;
  4531. { record maximum parameter size used in this proc }
  4532. current_procinfo.allocate_push_parasize(pushedparasize);
  4533. { check for stacked parameters }
  4534. if assigned(left) and
  4535. (current_settings.optimizerswitches*[cs_opt_stackframe,cs_opt_level1]<>[]) then
  4536. check_stack_parameters;
  4537. if assigned(callinitblock) then
  4538. firstpass(tnode(callinitblock));
  4539. { function result node (tempref or simple load) }
  4540. if assigned(funcretnode) then
  4541. firstpass(funcretnode);
  4542. { parameters }
  4543. if assigned(left) then
  4544. tcallparanode(left).firstcallparan;
  4545. { procedure variable ? }
  4546. if assigned(right) then
  4547. firstpass(right);
  4548. if assigned(methodpointer) and
  4549. (methodpointer.nodetype<>typen) then
  4550. firstpass(methodpointer);
  4551. if assigned(callcleanupblock) then
  4552. firstpass(tnode(callcleanupblock));
  4553. if not (block_type in [bt_const,bt_type,bt_const_type,bt_var_type]) then
  4554. include(current_procinfo.flags,pi_do_call);
  4555. { order parameters }
  4556. order_parameters;
  4557. { get a register for the return value }
  4558. if (not is_void(resultdef)) then
  4559. begin
  4560. if paramanager.ret_in_param(resultdef,procdefinition) then
  4561. begin
  4562. expectloc:=LOC_REFERENCE;
  4563. end
  4564. else
  4565. { ansi/widestrings must be registered, so we can dispose them }
  4566. if is_ansistring(resultdef) or
  4567. is_widestring(resultdef) or
  4568. is_unicodestring(resultdef) then
  4569. begin
  4570. expectloc:=LOC_REFERENCE;
  4571. end
  4572. else
  4573. { we have only to handle the result if it is used }
  4574. if (cnf_return_value_used in callnodeflags) then
  4575. expectloc:=get_expect_loc
  4576. else
  4577. expectloc:=LOC_VOID;
  4578. end
  4579. else
  4580. expectloc:=LOC_VOID;
  4581. { create tree for VMT entry if required }
  4582. gen_vmt_entry_load;
  4583. end;
  4584. {$ifdef state_tracking}
  4585. function Tcallnode.track_state_pass(exec_known:boolean):boolean;
  4586. var hp:Tcallparanode;
  4587. value:Tnode;
  4588. begin
  4589. track_state_pass:=false;
  4590. hp:=Tcallparanode(left);
  4591. while assigned(hp) do
  4592. begin
  4593. if left.track_state_pass(exec_known) then
  4594. begin
  4595. left.resultdef:=nil;
  4596. do_typecheckpass(left);
  4597. end;
  4598. value:=aktstate.find_fact(hp.left);
  4599. if value<>nil then
  4600. begin
  4601. track_state_pass:=true;
  4602. hp.left.destroy;
  4603. hp.left:=value.getcopy;
  4604. do_typecheckpass(hp.left);
  4605. end;
  4606. hp:=Tcallparanode(hp.right);
  4607. end;
  4608. end;
  4609. {$endif}
  4610. {**************************************************************************
  4611. INLINING SUPPORT
  4612. **************************************************************************}
  4613. function tcallnode.replaceparaload(var n: tnode; arg: pointer): foreachnoderesult;
  4614. var
  4615. paras: tcallparanode;
  4616. temp: tnode;
  4617. indexnr : integer;
  4618. begin
  4619. result := fen_false;
  4620. n.fileinfo := pfileposinfo(arg)^;
  4621. if (n.nodetype = loadn) then
  4622. begin
  4623. case tloadnode(n).symtableentry.typ of
  4624. paravarsym :
  4625. begin
  4626. paras := tcallparanode(left);
  4627. while assigned(paras) and
  4628. (paras.parasym <> tloadnode(n).symtableentry) do
  4629. paras := tcallparanode(paras.right);
  4630. if assigned(paras) then
  4631. begin
  4632. temp:=paras.left.getcopy;
  4633. { inherit modification information, this is needed by the dfa/cse }
  4634. temp.flags:=temp.flags+(n.flags*[nf_modify,nf_write,nf_address_taken]);
  4635. n.free;
  4636. n:=temp;
  4637. typecheckpass(n);
  4638. result := fen_true;
  4639. end;
  4640. end;
  4641. localvarsym :
  4642. begin
  4643. { local? }
  4644. if (tloadnode(n).symtableentry.owner <> tprocdef(procdefinition).localst) then
  4645. exit;
  4646. indexnr:=tloadnode(n).symtableentry.owner.SymList.IndexOf(tloadnode(n).symtableentry);
  4647. if (indexnr >= inlinelocals.count) or
  4648. not assigned(inlinelocals[indexnr]) then
  4649. internalerror(20040720);
  4650. temp := tnode(inlinelocals[indexnr]).getcopy;
  4651. { inherit modification information, this is needed by the dfa/cse }
  4652. temp.flags:=temp.flags+(n.flags*[nf_modify,nf_write,nf_address_taken]);
  4653. n.free;
  4654. n:=temp;
  4655. typecheckpass(n);
  4656. result := fen_true;
  4657. end;
  4658. else
  4659. ;
  4660. end;
  4661. end;
  4662. end;
  4663. procedure tcallnode.createlocaltemps(p:TObject;arg:pointer);
  4664. var
  4665. tempnode: ttempcreatenode;
  4666. indexnr : integer;
  4667. begin
  4668. if (TSym(p).typ <> localvarsym) then
  4669. exit;
  4670. indexnr:=TSym(p).Owner.SymList.IndexOf(p);
  4671. if (indexnr >= inlinelocals.count) then
  4672. inlinelocals.count:=indexnr+10;
  4673. if (vo_is_funcret in tabstractvarsym(p).varoptions) then
  4674. begin
  4675. if not assigned(funcretnode) then
  4676. internalerror(200709081);
  4677. inlinelocals[indexnr] := funcretnode.getcopy
  4678. end
  4679. else
  4680. begin
  4681. tempnode :=ctempcreatenode.create(tabstractvarsym(p).vardef,
  4682. tabstractvarsym(p).vardef.size,tt_persistent,tabstractvarsym(p).is_regvar(false));
  4683. addstatement(inlineinitstatement,tempnode);
  4684. if localvartrashing <> -1 then
  4685. cnodeutils.maybe_trash_variable(inlineinitstatement,tabstractnormalvarsym(p),ctemprefnode.create(tempnode));
  4686. addstatement(inlinecleanupstatement,ctempdeletenode.create(tempnode));
  4687. { inherit addr_taken flag }
  4688. if (tabstractvarsym(p).addr_taken) then
  4689. tempnode.includetempflag(ti_addr_taken);
  4690. inlinelocals[indexnr] := ctemprefnode.create(tempnode);
  4691. end;
  4692. end;
  4693. function nonlocalvars(var n: tnode; arg: pointer): foreachnoderesult;
  4694. begin
  4695. result := fen_false;
  4696. { this is just to play it safe, there are more safe situations }
  4697. if (n.nodetype = derefn) or
  4698. ((n.nodetype = loadn) and
  4699. { can be nil in case of internally generated labels like $raiseaddr }
  4700. assigned(tloadnode(n).symtable) and
  4701. { globals and fields of (possibly global) objects could always be changed in the callee }
  4702. ((tloadnode(n).symtable.symtabletype in [globalsymtable,ObjectSymtable]) or
  4703. { statics can only be modified by functions in the same unit }
  4704. ((tloadnode(n).symtable.symtabletype = staticsymtable) and
  4705. (tloadnode(n).symtable = TSymtable(arg))) or
  4706. { if the addr of the symbol is taken somewhere, it can be also non-local }
  4707. ((tloadnode(n).symtableentry.typ in [localvarsym,paravarsym,staticvarsym]) and
  4708. (tabstractvarsym(tloadnode(n).symtableentry).addr_taken))
  4709. )
  4710. ) or
  4711. ((n.nodetype = subscriptn) and
  4712. (tsubscriptnode(n).vs.owner.symtabletype = ObjectSymtable)) then
  4713. result := fen_norecurse_true;
  4714. end;
  4715. function tcallnode.paraneedsinlinetemp(para: tcallparanode; const pushconstaddr, complexpara: boolean): boolean;
  4716. begin
  4717. { if it's an assignable call-by-reference parameter, we cannot pass a
  4718. temp since then the modified valua will be lost }
  4719. if para.parasym.varspez in [vs_var,vs_out] then
  4720. exit(false);
  4721. { We cannot create a formaldef temp and assign something to it }
  4722. if para.parasym.vardef.typ=formaldef then
  4723. exit(false);
  4724. { We don't need temps for parameters that are already temps, except if
  4725. the passed temp could be put in a regvar while the parameter inside
  4726. the routine cannot be (e.g., because its address is taken in the
  4727. routine), or if the temp is a const and the parameter gets modified }
  4728. if (para.left.nodetype=temprefn) and
  4729. (not(ti_may_be_in_reg in ttemprefnode(para.left).tempflags) or
  4730. not(tparavarsym(para.parasym).varregable in [vr_none,vr_addr])) and
  4731. (not(ti_const in ttemprefnode(para.left).tempflags) or
  4732. (tparavarsym(para.parasym).varstate in [vs_initialised,vs_declared,vs_read])) then
  4733. exit(false);
  4734. { We need a temp if the passed value will not be in memory, while
  4735. the parameter inside the routine must be in memory }
  4736. if (tparavarsym(para.parasym).varregable in [vr_none,vr_addr]) and
  4737. not(para.left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  4738. exit(true);
  4739. { We try to handle complex expressions later by taking their address
  4740. and storing this address in a temp (which is then dereferenced when
  4741. the value is used; that doesn't work if we cannot take the address
  4742. of the expression though, in which case we store the result of the
  4743. expression in a temp }
  4744. if (complexpara and not(para.left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE]) or
  4745. (complexpara and
  4746. (not valid_for_addr(para.left,false) or
  4747. (para.left.nodetype=calln) or
  4748. is_constnode(para.left)))) then
  4749. exit(true);
  4750. { Normally, we do not need to create a temp for value parameters that
  4751. are not modified in the inlined function, and neither for const
  4752. parameters that are passed by value.
  4753. However, if we pass a global variable, an object field, a variable
  4754. whose address has been taken, or an expression containing a pointer
  4755. dereference as parameter, this value could be modified in other ways
  4756. as well (even inside the callee) and in such cases we still create a
  4757. temp to be on the safe side.
  4758. We *must not* create a temp for global variables passed by
  4759. reference to a const parameter, because if not inlined then any
  4760. changes to the original value will also be visible in the callee
  4761. (although this is technically undefined behaviour, since with
  4762. "const" the programmer tells the compiler this argument will not
  4763. change). }
  4764. if (((para.parasym.varspez=vs_value) and
  4765. (para.parasym.varstate in [vs_initialised,vs_declared,vs_read])) or
  4766. ((para.parasym.varspez=vs_const) and
  4767. not pushconstaddr)) and
  4768. foreachnodestatic(para.left,@nonlocalvars,pointer(symtableproc)) then
  4769. exit(true);
  4770. { Value parameters of which we know they are modified by definition
  4771. have to be copied to a temp }
  4772. if (para.parasym.varspez=vs_value) and
  4773. not(para.parasym.varstate in [vs_initialised,vs_declared,vs_read]) then
  4774. exit(true);
  4775. { the compiler expects that it can take the address of parameters passed by reference in
  4776. the case of const so we can't replace the node simply by a constant node
  4777. When playing with this code, ensure that
  4778. function f(const a,b : longint) : longint;inline;
  4779. begin
  4780. result:=a*b;
  4781. end;
  4782. [...]
  4783. ...:=f(10,20));
  4784. [...]
  4785. is still folded. (FK)
  4786. }
  4787. if (para.parasym.varspez=vs_const) and
  4788. { const para's can get vs_readwritten if their address is taken ->
  4789. in case they are not passed by reference, to keep the same
  4790. behaviour as without inlining we have to make a copy in case the
  4791. originally passed parameter value gets changed inside the callee
  4792. }
  4793. (not pushconstaddr and
  4794. (para.parasym.varstate=vs_readwritten)
  4795. ) or
  4796. { call-by-reference const's may need to be passed by reference to
  4797. function called in the inlined code }
  4798. (pushconstaddr and
  4799. not valid_for_addr(para.left,false)) then
  4800. exit(true);
  4801. { insert value parameters directly if they are complex instead
  4802. of inserting a reference to the temp.
  4803. - this keeps the node tree simpler
  4804. - alignment is propagated }
  4805. if (para.parasym.varspez=vs_value) and
  4806. complexpara then
  4807. exit(true);
  4808. result:=false;
  4809. end;
  4810. function tcallnode.maybecreateinlineparatemp(para: tcallparanode; out complexpara: boolean): boolean;
  4811. var
  4812. tempnode: ttempcreatenode;
  4813. realtarget: tnode;
  4814. paracomplexity: longint;
  4815. pushconstaddr: boolean;
  4816. begin
  4817. result:=false;
  4818. { determine how a parameter is passed to the inlined body
  4819. There are three options:
  4820. - insert the node tree of the callparanode directly
  4821. If a parameter is used only once, this is the best option if we can do so
  4822. - get the address of the argument, store it in a temp and insert a dereference to this temp
  4823. If the node tree cannot be inserted directly, taking the address of the argument and using it
  4824. is the second best option, but even this is not always possible
  4825. - assign the value of the argument to a newly created temp
  4826. This is the fall back which works always
  4827. Notes:
  4828. - we need to take care that we use the type of the defined parameter and not of the
  4829. passed parameter, because these can be different in case of a formaldef (PFV)
  4830. }
  4831. { pre-compute some values }
  4832. paracomplexity:=node_complexity(para.left);
  4833. if para.parasym.varspez=vs_const then
  4834. pushconstaddr:=paramanager.push_addr_param(vs_const,para.parasym.vardef,procdefinition.proccalloption)
  4835. else
  4836. pushconstaddr:=false;
  4837. realtarget:=actualtargetnode(@para.left)^;
  4838. { if the parameter is "complex", try to take the address of the
  4839. parameter expression, store it in a temp and replace occurrences of
  4840. the parameter with dereferencings of this temp
  4841. }
  4842. complexpara:=
  4843. { don't create a temp. for function results }
  4844. not(nf_is_funcret in realtarget.flags) and
  4845. { this makes only sense if the parameter is reasonably complex,
  4846. otherwise inserting directly is a better solution }
  4847. (
  4848. (paracomplexity>2) or
  4849. { don't create a temp. for the often seen case that p^ is passed to a var parameter }
  4850. ((paracomplexity>1) and
  4851. not((realtarget.nodetype=derefn) and (para.parasym.varspez in [vs_var,vs_out,vs_constref])) and
  4852. not((realtarget.nodetype=loadn) and tloadnode(realtarget).is_addr_param_load) and
  4853. not(realtarget.nodetype=realconstn)
  4854. )
  4855. );
  4856. { check if we have to create a temp, assign the parameter's
  4857. contents to that temp and then substitute the parameter
  4858. with the temp everywhere in the function }
  4859. if paraneedsinlinetemp(para,pushconstaddr,complexpara) then
  4860. begin
  4861. tempnode:=ctempcreatenode.create(para.parasym.vardef,para.parasym.vardef.size,
  4862. tt_persistent,tparavarsym(para.parasym).is_regvar(false));
  4863. addstatement(inlineinitstatement,tempnode);
  4864. addstatement(inlinecleanupstatement,ctempdeletenode.create(tempnode));
  4865. addstatement(inlineinitstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  4866. para.left));
  4867. para.left := ctemprefnode.create(tempnode);
  4868. { inherit addr_taken flag }
  4869. if (tabstractvarsym(para.parasym).addr_taken) then
  4870. tempnode.includetempflag(ti_addr_taken);
  4871. { inherit const }
  4872. if tabstractvarsym(para.parasym).varspez=vs_const then
  4873. begin
  4874. tempnode.includetempflag(ti_const);
  4875. { apply less strict rules for the temp. to be a register than
  4876. ttempcreatenode does
  4877. this way, dyn. array, ansistrings etc. can be put into registers as well }
  4878. if tparavarsym(para.parasym).is_regvar(false) then
  4879. tempnode.includetempflag(ti_may_be_in_reg);
  4880. end;
  4881. result:=true;
  4882. end
  4883. { for formaldefs, we do not need a temp., but it must be inherited if they are not regable }
  4884. else if (para.parasym.vardef.typ=formaldef) and not(tparavarsym(para.parasym).is_regvar(false)) then
  4885. make_not_regable(para.left,[ra_addr_regable]);
  4886. end;
  4887. procedure tcallnode.createinlineparas;
  4888. var
  4889. para: tcallparanode;
  4890. n: tnode;
  4891. complexpara: boolean;
  4892. begin
  4893. { parameters }
  4894. para := tcallparanode(left);
  4895. while assigned(para) do
  4896. begin
  4897. if (para.parasym.typ = paravarsym) and
  4898. ((para.parasym.refs>0) or
  4899. not(cs_opt_dead_values in current_settings.optimizerswitches) or
  4900. might_have_sideeffects(para.left)) then
  4901. begin
  4902. { must take copy of para.left, because if it contains a }
  4903. { temprefn pointing to a copied temp (e.g. methodpointer), }
  4904. { then this parameter must be changed to point to the copy of }
  4905. { that temp (JM) }
  4906. n := para.left.getcopy;
  4907. para.left.free;
  4908. para.left := n;
  4909. firstpass(para.left);
  4910. if not maybecreateinlineparatemp(para,complexpara) and
  4911. complexpara then
  4912. wrapcomplexinlinepara(para);
  4913. end;
  4914. para := tcallparanode(para.right);
  4915. end;
  4916. { local variables }
  4917. if not assigned(tprocdef(procdefinition).localst) or
  4918. (tprocdef(procdefinition).localst.SymList.count = 0) then
  4919. exit;
  4920. inlinelocals.count:=tprocdef(procdefinition).localst.SymList.count;
  4921. tprocdef(procdefinition).localst.SymList.ForEachCall(@createlocaltemps,nil);
  4922. end;
  4923. procedure tcallnode.wrapcomplexinlinepara(para: tcallparanode);
  4924. var
  4925. ptrtype: tdef;
  4926. tempnode: ttempcreatenode;
  4927. paraaddr: taddrnode;
  4928. isfuncretnode : boolean;
  4929. begin
  4930. ptrtype:=cpointerdef.getreusable(para.left.resultdef);
  4931. tempnode:=ctempcreatenode.create(ptrtype,ptrtype.size,tt_persistent,true);
  4932. addstatement(inlineinitstatement,tempnode);
  4933. isfuncretnode:=nf_is_funcret in para.left.flags;
  4934. if isfuncretnode then
  4935. addstatement(inlinecleanupstatement,ctempdeletenode.create_normal_temp(tempnode))
  4936. else
  4937. addstatement(inlinecleanupstatement,ctempdeletenode.create(tempnode));
  4938. { inherit addr_taken flag }
  4939. if (tabstractvarsym(para.parasym).addr_taken) then
  4940. tempnode.includetempflag(ti_addr_taken);
  4941. { inherit read only }
  4942. if tabstractvarsym(para.parasym).varspez=vs_const then
  4943. tempnode.includetempflag(ti_const);
  4944. paraaddr:=caddrnode.create_internal(para.left);
  4945. include(paraaddr.addrnodeflags,anf_typedaddr);
  4946. addstatement(inlineinitstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  4947. paraaddr));
  4948. para.left:=cderefnode.create(ctemprefnode.create(tempnode));
  4949. if isfuncretnode then
  4950. Include(para.left.flags,nf_is_funcret);
  4951. end;
  4952. function UsesTmp(var n: tnode; arg: pointer): foreachnoderesult;
  4953. begin
  4954. Result:=fen_false;
  4955. if (n.nodetype=temprefn) and (ttemprefnode(n).tempinfo=arg) then
  4956. Result:=fen_norecurse_true;
  4957. end;
  4958. function tcallnode.optimize_funcret_assignment(inlineblock: tblocknode): tnode;
  4959. var
  4960. hp : tstatementnode;
  4961. hp2 : tnode;
  4962. resassign : tassignmentnode;
  4963. begin
  4964. result:=nil;
  4965. if not assigned(funcretnode) or
  4966. not(cnf_return_value_used in callnodeflags) then
  4967. exit;
  4968. { block already optimized? }
  4969. if not(inlineblock.nodetype=blockn) then
  4970. exit;
  4971. { tempcreatenode for the function result }
  4972. hp:=tstatementnode(inlineblock.left);
  4973. if not(assigned(hp)) or
  4974. (hp.left.nodetype <> tempcreaten) or
  4975. not(nf_is_funcret in hp.left.flags) then
  4976. exit;
  4977. hp:=tstatementnode(hp.right);
  4978. if not(assigned(hp)) or
  4979. (hp.left.nodetype<>assignn)
  4980. { FK: check commented, original comment was:
  4981. constant assignment? right must be a constant (mainly to avoid trying
  4982. to reuse local temps which may already be freed afterwards once these
  4983. checks are made looser)
  4984. or
  4985. not is_constnode(tassignmentnode(hp.left).right)
  4986. So far I found no example why removing this check might be a problem.
  4987. If this needs to be revert, issue #36279 must be checked/solved again.
  4988. }
  4989. then
  4990. exit;
  4991. { left must be function result }
  4992. resassign:=tassignmentnode(hp.left);
  4993. hp2:=resassign.left;
  4994. { can have extra type conversion due to absolute mapping
  4995. of <fucntionname> on function result var }
  4996. if (hp2.nodetype=typeconvn) and (ttypeconvnode(hp2).convtype=tc_equal) then
  4997. hp2:=ttypeconvnode(hp2).left;
  4998. if (hp2.nodetype<>temprefn) or
  4999. { check if right references the temp. being removed, i.e. using an uninitialized result }
  5000. foreachnodestatic(resassign.right,@UsesTmp,ttemprefnode(hp2).tempinfo) or
  5001. not(nf_is_funcret in hp2.flags) then
  5002. exit;
  5003. { tempdelete to normal of the function result }
  5004. hp:=tstatementnode(hp.right);
  5005. if not(assigned(hp)) or
  5006. (hp.left.nodetype <> tempdeleten) then
  5007. exit;
  5008. { the function result once more }
  5009. hp:=tstatementnode(hp.right);
  5010. if not(assigned(hp)) or
  5011. (hp.left.nodetype<>temprefn) or
  5012. not(nf_is_funcret in hp.left.flags) then
  5013. exit;
  5014. { should be the end }
  5015. if assigned(hp.right) then
  5016. exit;
  5017. { we made it! }
  5018. result:=ctypeconvnode.create_internal(tassignmentnode(resassign).right.getcopy,hp2.resultdef);
  5019. firstpass(result);
  5020. end;
  5021. { this procedure removes the user code flag because it prevents optimizations }
  5022. function removeusercodeflag(var n : tnode; arg : pointer) : foreachnoderesult;
  5023. begin
  5024. result:=fen_false;
  5025. if nf_usercode_entry in n.flags then
  5026. begin
  5027. exclude(n.flags,nf_usercode_entry);
  5028. result:=fen_norecurse_true;
  5029. end;
  5030. end;
  5031. { reference symbols that are imported from another unit }
  5032. function importglobalsyms(var n:tnode; arg:pointer):foreachnoderesult;
  5033. var
  5034. sym : tsym;
  5035. begin
  5036. result:=fen_false;
  5037. if n.nodetype=loadn then
  5038. begin
  5039. sym:=tloadnode(n).symtableentry;
  5040. if sym.typ=staticvarsym then
  5041. begin
  5042. if FindUnitSymtable(tloadnode(n).symtable).moduleid<>current_module.moduleid then
  5043. current_module.addimportedsym(sym);
  5044. end
  5045. else if (sym.typ=constsym) and (tconstsym(sym).consttyp in [constwresourcestring,constresourcestring]) then
  5046. begin
  5047. if tloadnode(n).symtableentry.owner.moduleid<>current_module.moduleid then
  5048. current_module.addimportedsym(sym);
  5049. end;
  5050. end
  5051. else if (n.nodetype=calln) then
  5052. begin
  5053. if (assigned(tcallnode(n).procdefinition)) and
  5054. (tcallnode(n).procdefinition.typ=procdef) and
  5055. (findunitsymtable(tcallnode(n).procdefinition.owner).moduleid<>current_module.moduleid) then
  5056. current_module.addimportedsym(tprocdef(tcallnode(n).procdefinition).procsym);
  5057. end;
  5058. end;
  5059. function tcallnode.pass1_inline:tnode;
  5060. var
  5061. n,
  5062. body : tnode;
  5063. para : tcallparanode;
  5064. inlineblock,
  5065. inlinecleanupblock : tblocknode;
  5066. begin
  5067. inc(inlinelevel);
  5068. result:=nil;
  5069. if not(assigned(tprocdef(procdefinition).inlininginfo) and
  5070. assigned(tprocdef(procdefinition).inlininginfo^.code)) then
  5071. internalerror(200412021);
  5072. inlinelocals:=TFPObjectList.create(true);
  5073. { inherit flags }
  5074. current_procinfo.flags:=current_procinfo.flags+
  5075. ((procdefinition as tprocdef).inlininginfo^.flags*inherited_inlining_flags);
  5076. { Create new code block for inlining }
  5077. inlineblock:=internalstatements(inlineinitstatement);
  5078. { make sure that valid_for_assign() returns false for this block
  5079. (otherwise assigning values to the block will result in assigning
  5080. values to the inlined function's result) }
  5081. include(inlineblock.flags,nf_no_lvalue);
  5082. inlinecleanupblock:=internalstatements(inlinecleanupstatement);
  5083. if assigned(callinitblock) then
  5084. addstatement(inlineinitstatement,callinitblock.getcopy);
  5085. { replace complex parameters with temps }
  5086. createinlineparas;
  5087. { create a copy of the body and replace parameter loads with the parameter values }
  5088. body:=tprocdef(procdefinition).inlininginfo^.code.getcopy;
  5089. foreachnodestatic(pm_postprocess,body,@removeusercodeflag,nil);
  5090. foreachnodestatic(pm_postprocess,body,@importglobalsyms,nil);
  5091. foreachnode(pm_preprocess,body,@replaceparaload,@fileinfo);
  5092. { Concat the body and finalization parts }
  5093. addstatement(inlineinitstatement,body);
  5094. addstatement(inlineinitstatement,inlinecleanupblock);
  5095. inlinecleanupblock:=nil;
  5096. if assigned(callcleanupblock) then
  5097. addstatement(inlineinitstatement,callcleanupblock.getcopy);
  5098. { the last statement of the new inline block must return the
  5099. location and type of the function result.
  5100. This is not needed when the result is not used, also the tempnode is then
  5101. already destroyed by a tempdelete in the callcleanupblock tree }
  5102. if not is_void(resultdef) and
  5103. (cnf_return_value_used in callnodeflags) then
  5104. begin
  5105. if assigned(funcretnode) then
  5106. addstatement(inlineinitstatement,funcretnode.getcopy)
  5107. else
  5108. begin
  5109. para:=tcallparanode(left);
  5110. while assigned(para) do
  5111. begin
  5112. if (vo_is_hidden_para in para.parasym.varoptions) and
  5113. (vo_is_funcret in para.parasym.varoptions) then
  5114. begin
  5115. addstatement(inlineinitstatement,para.left.getcopy);
  5116. break;
  5117. end;
  5118. para:=tcallparanode(para.right);
  5119. end;
  5120. end;
  5121. end;
  5122. typecheckpass(tnode(inlineblock));
  5123. doinlinesimplify(tnode(inlineblock));
  5124. node_reset_flags(tnode(inlineblock),[],[tnf_pass1_done]);
  5125. firstpass(tnode(inlineblock));
  5126. result:=inlineblock;
  5127. { if the function result is used then verify that the blocknode
  5128. returns the same result type as the original callnode }
  5129. if (cnf_return_value_used in callnodeflags) and
  5130. (result.resultdef<>resultdef) then
  5131. internalerror(200709171);
  5132. { free the temps for the locals }
  5133. inlinelocals.free;
  5134. inlinelocals:=nil;
  5135. inlineinitstatement:=nil;
  5136. inlinecleanupstatement:=nil;
  5137. n:=optimize_funcret_assignment(inlineblock);
  5138. if assigned(n) then
  5139. begin
  5140. inlineblock.free;
  5141. result:=n;
  5142. end;
  5143. {$ifdef DEBUGINLINE}
  5144. writeln;
  5145. writeln('**************************',tprocdef(procdefinition).mangledname);
  5146. printnode(output,result);
  5147. {$endif DEBUGINLINE}
  5148. dec(inlinelevel);
  5149. end;
  5150. end.