ncal.pas 190 KB

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