ncal.pas 221 KB

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