ncal.pas 160 KB

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