ncal.pas 155 KB

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