ncal.pas 122 KB

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