ncal.pas 154 KB

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