ncal.pas 175 KB

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