ncal.pas 178 KB

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