ncal.pas 130 KB

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