ncal.pas 155 KB

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