ncal.pas 150 KB

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