2
0

ncal.pas 151 KB

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