ncal.pas 164 KB

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