ncal.pas 231 KB

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