ncal.pas 237 KB

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