ncal.pas 167 KB

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