ncal.pas 225 KB

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