ncal.pas 86 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This file implements the node for sub procedure calling.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit ncal;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cutils,cclasses,
  23. globtype,cpuinfo,
  24. paramgr,
  25. node,nbas,nutils,
  26. {$ifdef state_tracking}
  27. nstate,
  28. {$endif state_tracking}
  29. symbase,symtype,symsym,symdef,symtable;
  30. type
  31. tcallnodeflag = (
  32. cnf_restypeset,
  33. cnf_return_value_used,
  34. cnf_inherited,
  35. cnf_anon_inherited,
  36. cnf_new_call,
  37. cnf_dispose_call,
  38. cnf_member_call { called with implicit methodpointer tree }
  39. );
  40. tcallnodeflags = set of tcallnodeflag;
  41. tcallnode = class(tbinarynode)
  42. private
  43. { number of parameters passed from the source, this does not include the hidden parameters }
  44. paralength : smallint;
  45. function gen_self_tree_methodpointer:tnode;
  46. function gen_self_tree:tnode;
  47. function gen_vmt_tree:tnode;
  48. procedure bind_paraitem;
  49. { function return node, this is used to pass the data for a
  50. ret_in_param return value }
  51. _funcretnode : tnode;
  52. procedure setfuncretnode(const returnnode: tnode);
  53. procedure convert_carg_array_of_const;
  54. procedure order_parameters;
  55. procedure createinlineparas(var createstatement, deletestatement: tstatementnode);
  56. function replaceparaload(var n: tnode; arg: pointer): foreachnoderesult;
  57. protected
  58. pushedparasize : longint;
  59. public
  60. { the symbol containing the definition of the procedure }
  61. { to call }
  62. symtableprocentry : tprocsym;
  63. symtableprocentryderef : tderef;
  64. { symtable where the entry was found, needed for with support }
  65. symtableproc : tsymtable;
  66. { the definition of the procedure to call }
  67. procdefinition : tabstractprocdef;
  68. procdefinitionderef : tderef;
  69. { tree that contains the pointer to the object for this method }
  70. methodpointerinit,
  71. methodpointerdone,
  72. methodpointer : tnode;
  73. { inline function body }
  74. inlinecode : tnode;
  75. { varargs tparaitems }
  76. varargsparas : tvarargspara;
  77. { node that specifies where the result should be put for calls }
  78. { that return their result in a parameter }
  79. property funcretnode: tnode read _funcretnode write setfuncretnode;
  80. { separately specified resulttype for some compilerprocs (e.g. }
  81. { you can't have a function with an "array of char" resulttype }
  82. { the RTL) (JM) }
  83. restype: ttype;
  84. callnodeflags : tcallnodeflags;
  85. { only the processor specific nodes need to override this }
  86. { constructor }
  87. constructor create(l:tnode; v : tprocsym;st : tsymtable; mp: tnode; callflags:tcallnodeflags);virtual;
  88. constructor create_procvar(l,r:tnode);
  89. constructor createintern(const name: string; params: tnode);
  90. constructor createinternres(const name: string; params: tnode; const res: ttype);
  91. constructor createinternreturn(const name: string; params: tnode; returnnode : tnode);
  92. destructor destroy;override;
  93. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  94. procedure ppuwrite(ppufile:tcompilerppufile);override;
  95. procedure buildderefimpl;override;
  96. procedure derefimpl;override;
  97. function getcopy : tnode;override;
  98. { Goes through all symbols in a class and subclasses and calls
  99. verify abstract for each .
  100. }
  101. procedure verifyabstractcalls;
  102. { called for each definition in a class and verifies if a method
  103. is abstract or not, if it is abstract, give out a warning
  104. }
  105. procedure verifyabstract(p : tnamedindexitem;arg:pointer);
  106. procedure insertintolist(l : tnodelist);override;
  107. function pass_1 : tnode;override;
  108. function det_resulttype:tnode;override;
  109. {$ifdef state_tracking}
  110. function track_state_pass(exec_known:boolean):boolean;override;
  111. {$endif state_tracking}
  112. function docompare(p: tnode): boolean; override;
  113. procedure printnodedata(var t:text);override;
  114. function para_count:longint;
  115. private
  116. AbstractMethodsList : TStringList;
  117. end;
  118. tcallnodeclass = class of tcallnode;
  119. tcallparaflag = (
  120. cpf_is_colon_para,
  121. cpf_varargs_para { belongs this para to varargs }
  122. );
  123. tcallparaflags = set of tcallparaflag;
  124. tcallparanode = class(tbinarynode)
  125. public
  126. callparaflags : tcallparaflags;
  127. paraitem : tparaitem;
  128. used_by_callnode : boolean;
  129. { only the processor specific nodes need to override this }
  130. { constructor }
  131. constructor create(expr,next : tnode);virtual;
  132. destructor destroy;override;
  133. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  134. procedure ppuwrite(ppufile:tcompilerppufile);override;
  135. function getcopy : tnode;override;
  136. procedure insertintolist(l : tnodelist);override;
  137. procedure get_paratype;
  138. procedure insert_typeconv(do_count : boolean);
  139. procedure det_registers;
  140. procedure firstcallparan;
  141. procedure secondcallparan;virtual;abstract;
  142. function docompare(p: tnode): boolean; override;
  143. procedure printnodetree(var t:text);override;
  144. end;
  145. tcallparanodeclass = class of tcallparanode;
  146. function reverseparameters(p: tcallparanode): tcallparanode;
  147. var
  148. ccallnode : tcallnodeclass;
  149. ccallparanode : tcallparanodeclass;
  150. { Current callnode, this is needed for having a link
  151. between the callparanodes and the callnode they belong to }
  152. aktcallnode : tcallnode;
  153. implementation
  154. uses
  155. systems,
  156. verbose,globals,
  157. symconst,defutil,defcmp,
  158. htypechk,pass_1,
  159. ncnv,nld,ninl,nadd,ncon,nmem,
  160. procinfo,
  161. cgbase
  162. ;
  163. type
  164. tobjectinfoitem = class(tlinkedlistitem)
  165. objinfo : tobjectdef;
  166. constructor create(def : tobjectdef);
  167. end;
  168. {****************************************************************************
  169. HELPERS
  170. ****************************************************************************}
  171. function reverseparameters(p: tcallparanode): tcallparanode;
  172. var
  173. hp1, hp2: tcallparanode;
  174. begin
  175. hp1:=nil;
  176. while assigned(p) do
  177. begin
  178. { pull out }
  179. hp2:=p;
  180. p:=tcallparanode(p.right);
  181. { pull in }
  182. hp2.right:=hp1;
  183. hp1:=hp2;
  184. end;
  185. reverseparameters:=hp1;
  186. end;
  187. function gen_high_tree(p:tnode;paradef:tdef):tnode;
  188. var
  189. temp: tnode;
  190. len : integer;
  191. loadconst : boolean;
  192. hightree : tnode;
  193. begin
  194. len:=-1;
  195. loadconst:=true;
  196. hightree:=nil;
  197. case p.resulttype.def.deftype of
  198. arraydef :
  199. begin
  200. if (paradef.deftype<>arraydef) then
  201. internalerror(200405241);
  202. { handle special case of passing an single array to an array of array }
  203. if compare_defs(tarraydef(paradef).elementtype.def,p.resulttype.def,nothingn)>=te_equal then
  204. len:=0
  205. else
  206. begin
  207. { handle via a normal inline in_high_x node }
  208. loadconst := false;
  209. hightree := geninlinenode(in_high_x,false,p.getcopy);
  210. resulttypepass(hightree);
  211. { only substract low(array) if it's <> 0 }
  212. temp := geninlinenode(in_low_x,false,p.getcopy);
  213. resulttypepass(temp);
  214. if (temp.nodetype <> ordconstn) or
  215. (tordconstnode(temp).value <> 0) then
  216. hightree := caddnode.create(subn,hightree,temp)
  217. else
  218. temp.free;
  219. end;
  220. end;
  221. stringdef :
  222. begin
  223. if is_open_string(paradef) then
  224. begin
  225. { handle via a normal inline in_high_x node }
  226. loadconst := false;
  227. hightree := geninlinenode(in_high_x,false,p.getcopy);
  228. end
  229. else
  230. begin
  231. { passing a string to an array of char }
  232. if (p.nodetype=stringconstn) then
  233. begin
  234. len:=str_length(p);
  235. if len>0 then
  236. dec(len);
  237. end
  238. else
  239. begin
  240. hightree:=caddnode.create(subn,geninlinenode(in_length_x,false,p.getcopy),
  241. cordconstnode.create(1,s32inttype,false));
  242. loadconst:=false;
  243. end;
  244. end;
  245. end;
  246. else
  247. len:=0;
  248. end;
  249. if loadconst then
  250. hightree:=cordconstnode.create(len,s32inttype,true)
  251. else
  252. begin
  253. if not assigned(hightree) then
  254. internalerror(200304071);
  255. { Need to use explicit, because it can also be a enum }
  256. hightree:=ctypeconvnode.create_explicit(hightree,s32inttype);
  257. end;
  258. result:=hightree;
  259. end;
  260. {****************************************************************************
  261. TOBJECTINFOITEM
  262. ****************************************************************************}
  263. constructor tobjectinfoitem.create(def : tobjectdef);
  264. begin
  265. inherited create;
  266. objinfo := def;
  267. end;
  268. {****************************************************************************
  269. TCALLPARANODE
  270. ****************************************************************************}
  271. constructor tcallparanode.create(expr,next : tnode);
  272. begin
  273. inherited create(callparan,expr,next);
  274. if not assigned(expr) then
  275. internalerror(200305091);
  276. expr.set_file_line(self);
  277. callparaflags:=[];
  278. end;
  279. destructor tcallparanode.destroy;
  280. begin
  281. { When the node is used by callnode then
  282. we don't destroy left, the callnode takes care of it }
  283. if used_by_callnode then
  284. left:=nil;
  285. inherited destroy;
  286. end;
  287. constructor tcallparanode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  288. begin
  289. inherited ppuload(t,ppufile);
  290. ppufile.getsmallset(callparaflags);
  291. end;
  292. procedure tcallparanode.ppuwrite(ppufile:tcompilerppufile);
  293. begin
  294. inherited ppuwrite(ppufile);
  295. ppufile.putsmallset(callparaflags);
  296. end;
  297. function tcallparanode.getcopy : tnode;
  298. var
  299. n : tcallparanode;
  300. begin
  301. n:=tcallparanode(inherited getcopy);
  302. n.callparaflags:=callparaflags;
  303. n.paraitem:=paraitem;
  304. result:=n;
  305. end;
  306. procedure tcallparanode.insertintolist(l : tnodelist);
  307. begin
  308. end;
  309. procedure tcallparanode.get_paratype;
  310. var
  311. old_array_constructor : boolean;
  312. begin
  313. inc(parsing_para_level);
  314. if assigned(right) then
  315. tcallparanode(right).get_paratype;
  316. old_array_constructor:=allow_array_constructor;
  317. allow_array_constructor:=true;
  318. resulttypepass(left);
  319. allow_array_constructor:=old_array_constructor;
  320. if codegenerror then
  321. resulttype:=generrortype
  322. else
  323. resulttype:=left.resulttype;
  324. dec(parsing_para_level);
  325. end;
  326. procedure tcallparanode.insert_typeconv(do_count : boolean);
  327. var
  328. oldtype : ttype;
  329. {$ifdef extdebug}
  330. store_count_ref : boolean;
  331. {$endif def extdebug}
  332. begin
  333. inc(parsing_para_level);
  334. {$ifdef extdebug}
  335. if do_count then
  336. begin
  337. store_count_ref:=count_ref;
  338. count_ref:=true;
  339. end;
  340. {$endif def extdebug}
  341. { Be sure to have the resulttype }
  342. if not assigned(left.resulttype.def) then
  343. resulttypepass(left);
  344. if (left.nodetype<>nothingn) then
  345. begin
  346. { Convert tp procvars, this is needs to be done
  347. here to make the change permanent. in the overload
  348. choosing the changes are only made temporary }
  349. if (left.resulttype.def.deftype=procvardef) and
  350. (paraitem.paratype.def.deftype<>procvardef) then
  351. begin
  352. if maybe_call_procvar(left,true) then
  353. resulttype:=left.resulttype;
  354. end;
  355. { Handle varargs and hidden paras directly, no typeconvs or }
  356. { typechecking needed }
  357. if (cpf_varargs_para in callparaflags) then
  358. begin
  359. { convert pascal to C types }
  360. case left.resulttype.def.deftype of
  361. stringdef :
  362. inserttypeconv(left,charpointertype);
  363. floatdef :
  364. inserttypeconv(left,s64floattype);
  365. end;
  366. set_varstate(left,vs_used,true);
  367. resulttype:=left.resulttype;
  368. { also update paraitem type to get the correct parameter location
  369. for the new types }
  370. paraitem.paratype:=left.resulttype;
  371. end
  372. else
  373. if (paraitem.is_hidden) then
  374. begin
  375. set_varstate(left,vs_used,true);
  376. resulttype:=left.resulttype;
  377. end
  378. else
  379. begin
  380. { Do we need arrayconstructor -> set conversion, then insert
  381. it here before the arrayconstructor node breaks the tree
  382. with its conversions of enum->ord }
  383. if (left.nodetype=arrayconstructorn) and
  384. (paraitem.paratype.def.deftype=setdef) then
  385. inserttypeconv(left,paraitem.paratype);
  386. { set some settings needed for arrayconstructor }
  387. if is_array_constructor(left.resulttype.def) then
  388. begin
  389. if is_array_of_const(paraitem.paratype.def) then
  390. begin
  391. { force variant array }
  392. include(left.flags,nf_forcevaria);
  393. end
  394. else
  395. begin
  396. include(left.flags,nf_novariaallowed);
  397. { now that the resultting type is know we can insert the required
  398. typeconvs for the array constructor }
  399. tarrayconstructornode(left).force_type(tarraydef(paraitem.paratype.def).elementtype);
  400. end;
  401. end;
  402. { check if local proc/func is assigned to procvar }
  403. if left.resulttype.def.deftype=procvardef then
  404. test_local_to_procvar(tprocvardef(left.resulttype.def),paraitem.paratype.def);
  405. { test conversions }
  406. if not(is_shortstring(left.resulttype.def) and
  407. is_shortstring(paraitem.paratype.def)) and
  408. (paraitem.paratype.def.deftype<>formaldef) then
  409. begin
  410. { Process open parameters }
  411. if paramanager.push_high_param(paraitem.paratyp,paraitem.paratype.def,aktcallnode.procdefinition.proccalloption) then
  412. begin
  413. { insert type conv but hold the ranges of the array }
  414. oldtype:=left.resulttype;
  415. inserttypeconv(left,paraitem.paratype);
  416. left.resulttype:=oldtype;
  417. end
  418. else
  419. begin
  420. { for ordinals, floats and enums, verify if we might cause
  421. some range-check errors. }
  422. if (paraitem.paratype.def.deftype in [enumdef,orddef,floatdef]) and
  423. (left.resulttype.def.deftype in [enumdef,orddef,floatdef]) and
  424. (left.nodetype in [vecn,loadn,calln]) then
  425. begin
  426. if (left.resulttype.def.size>paraitem.paratype.def.size) then
  427. begin
  428. if (cs_check_range in aktlocalswitches) then
  429. Message(type_w_smaller_possible_range_check)
  430. else
  431. Message(type_h_smaller_possible_range_check);
  432. end;
  433. end;
  434. inserttypeconv(left,paraitem.paratype);
  435. end;
  436. if codegenerror then
  437. begin
  438. dec(parsing_para_level);
  439. exit;
  440. end;
  441. end;
  442. { check var strings }
  443. if (cs_strict_var_strings in aktlocalswitches) and
  444. is_shortstring(left.resulttype.def) and
  445. is_shortstring(paraitem.paratype.def) and
  446. (paraitem.paratyp in [vs_out,vs_var]) and
  447. not(is_open_string(paraitem.paratype.def)) and
  448. not(equal_defs(left.resulttype.def,paraitem.paratype.def)) then
  449. begin
  450. aktfilepos:=left.fileinfo;
  451. CGMessage(type_e_strict_var_string_violation);
  452. end;
  453. { Handle formal parameters separate }
  454. if (paraitem.paratype.def.deftype=formaldef) then
  455. begin
  456. { load procvar if a procedure is passed }
  457. if (m_tp_procvar in aktmodeswitches) and
  458. (left.nodetype=calln) and
  459. (is_void(left.resulttype.def)) then
  460. load_procvar_from_calln(left);
  461. case paraitem.paratyp of
  462. vs_var,
  463. vs_out :
  464. begin
  465. if not valid_for_formal_var(left) then
  466. CGMessagePos(left.fileinfo,parser_e_illegal_parameter_list);
  467. end;
  468. vs_const :
  469. begin
  470. if not valid_for_formal_const(left) then
  471. CGMessagePos(left.fileinfo,parser_e_illegal_parameter_list);
  472. end;
  473. end;
  474. end
  475. else
  476. begin
  477. { check if the argument is allowed }
  478. if (paraitem.paratyp in [vs_out,vs_var]) then
  479. valid_for_var(left);
  480. end;
  481. if paraitem.paratyp in [vs_var,vs_const] then
  482. begin
  483. { Causes problems with const ansistrings if also }
  484. { done for vs_const (JM) }
  485. if paraitem.paratyp = vs_var then
  486. set_unique(left);
  487. make_not_regable(left);
  488. end;
  489. { ansistrings out paramaters doesn't need to be }
  490. { unique, they are finalized }
  491. if paraitem.paratyp=vs_out then
  492. make_not_regable(left);
  493. if do_count then
  494. begin
  495. if paraitem.paratyp in [vs_var,vs_out] then
  496. set_varstate(left,vs_used,false)
  497. else
  498. set_varstate(left,vs_used,true);
  499. end;
  500. { must only be done after typeconv PM }
  501. resulttype:=paraitem.paratype;
  502. end;
  503. end;
  504. { process next node }
  505. if assigned(right) then
  506. tcallparanode(right).insert_typeconv(do_count);
  507. dec(parsing_para_level);
  508. {$ifdef extdebug}
  509. if do_count then
  510. count_ref:=store_count_ref;
  511. {$endif def extdebug}
  512. end;
  513. procedure tcallparanode.det_registers;
  514. begin
  515. if assigned(right) then
  516. begin
  517. tcallparanode(right).det_registers;
  518. registersint:=right.registersint;
  519. registersfpu:=right.registersfpu;
  520. {$ifdef SUPPORT_MMX}
  521. registersmmx:=right.registersmmx;
  522. {$endif}
  523. end;
  524. firstpass(left);
  525. if left.registersint>registersint then
  526. registersint:=left.registersint;
  527. if left.registersfpu>registersfpu then
  528. registersfpu:=left.registersfpu;
  529. {$ifdef SUPPORT_MMX}
  530. if left.registersmmx>registersmmx then
  531. registersmmx:=left.registersmmx;
  532. {$endif SUPPORT_MMX}
  533. end;
  534. procedure tcallparanode.firstcallparan;
  535. begin
  536. if not assigned(left.resulttype.def) then
  537. get_paratype;
  538. det_registers;
  539. end;
  540. function tcallparanode.docompare(p: tnode): boolean;
  541. begin
  542. docompare :=
  543. inherited docompare(p) and
  544. (callparaflags = tcallparanode(p).callparaflags)
  545. ;
  546. end;
  547. procedure tcallparanode.printnodetree(var t:text);
  548. begin
  549. printnodelist(t);
  550. end;
  551. {****************************************************************************
  552. TCALLNODE
  553. ****************************************************************************}
  554. constructor tcallnode.create(l:tnode;v : tprocsym;st : tsymtable; mp: tnode; callflags:tcallnodeflags);
  555. begin
  556. inherited create(calln,l,nil);
  557. symtableprocentry:=v;
  558. symtableproc:=st;
  559. callnodeflags:=callflags+[cnf_return_value_used];
  560. methodpointer:=mp;
  561. methodpointerinit:=nil;
  562. methodpointerdone:=nil;
  563. procdefinition:=nil;
  564. _funcretnode:=nil;
  565. inlinecode:=nil;
  566. paralength:=-1;
  567. varargsparas:=nil;
  568. end;
  569. constructor tcallnode.create_procvar(l,r:tnode);
  570. begin
  571. inherited create(calln,l,r);
  572. symtableprocentry:=nil;
  573. symtableproc:=nil;
  574. methodpointer:=nil;
  575. methodpointerinit:=nil;
  576. methodpointerdone:=nil;
  577. procdefinition:=nil;
  578. callnodeflags:=[cnf_return_value_used];
  579. _funcretnode:=nil;
  580. inlinecode:=nil;
  581. paralength:=-1;
  582. varargsparas:=nil;
  583. end;
  584. constructor tcallnode.createintern(const name: string; params: tnode);
  585. var
  586. srsym: tsym;
  587. symowner: tsymtable;
  588. begin
  589. if not (cs_compilesystem in aktmoduleswitches) then
  590. begin
  591. srsym := searchsymonlyin(systemunit,name);
  592. symowner := systemunit;
  593. end
  594. else
  595. begin
  596. searchsym(name,srsym,symowner);
  597. if not assigned(srsym) then
  598. searchsym(upper(name),srsym,symowner);
  599. end;
  600. if not assigned(srsym) or
  601. (srsym.typ <> procsym) then
  602. begin
  603. {$ifdef EXTDEBUG}
  604. Comment(V_Error,'unknown compilerproc '+name);
  605. {$endif EXTDEBUG}
  606. internalerror(200107271);
  607. end;
  608. self.create(params,tprocsym(srsym),symowner,nil,[]);
  609. end;
  610. constructor tcallnode.createinternres(const name: string; params: tnode; const res: ttype);
  611. begin
  612. self.createintern(name,params);
  613. restype := res;
  614. include(callnodeflags,cnf_restypeset);
  615. { both the normal and specified resulttype either have to be returned via a }
  616. { parameter or not, but no mixing (JM) }
  617. if paramanager.ret_in_param(restype.def,pocall_compilerproc) xor
  618. paramanager.ret_in_param(symtableprocentry.first_procdef.rettype.def,symtableprocentry.first_procdef.proccalloption) then
  619. internalerror(200108291);
  620. end;
  621. constructor tcallnode.createinternreturn(const name: string; params: tnode; returnnode : tnode);
  622. begin
  623. self.createintern(name,params);
  624. _funcretnode:=returnnode;
  625. if not paramanager.ret_in_param(symtableprocentry.first_procdef.rettype.def,symtableprocentry.first_procdef.proccalloption) then
  626. internalerror(200204247);
  627. end;
  628. procedure tcallnode.setfuncretnode(const returnnode: tnode);
  629. var
  630. para: tcallparanode;
  631. begin
  632. if assigned(_funcretnode) then
  633. _funcretnode.free;
  634. _funcretnode := returnnode;
  635. { if the resulttype pass hasn't occurred yet, that one will do }
  636. { everything }
  637. if assigned(resulttype.def) then
  638. begin
  639. { these are returned as values, but we can optimize their loading }
  640. { as well }
  641. if is_ansistring(resulttype.def) or
  642. is_widestring(resulttype.def) then
  643. exit;
  644. para := tcallparanode(left);
  645. while assigned(para) do
  646. begin
  647. if para.paraitem.is_hidden and
  648. (vo_is_funcret in tvarsym(para.paraitem.parasym).varoptions) then
  649. begin
  650. para.left.free;
  651. para.left := _funcretnode.getcopy;
  652. exit;
  653. end;
  654. para := tcallparanode(para.right);
  655. end;
  656. { no hidden resultpara found, error! }
  657. if not(procdefinition.proccalloption = pocall_inline) then
  658. internalerror(200306087);
  659. end;
  660. end;
  661. destructor tcallnode.destroy;
  662. begin
  663. methodpointer.free;
  664. methodpointerinit.free;
  665. methodpointerdone.free;
  666. _funcretnode.free;
  667. inlinecode.free;
  668. if assigned(varargsparas) then
  669. varargsparas.free;
  670. inherited destroy;
  671. end;
  672. constructor tcallnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  673. begin
  674. inherited ppuload(t,ppufile);
  675. ppufile.getderef(symtableprocentryderef);
  676. {$ifdef fpc}
  677. {$warning FIXME: No withsymtable support}
  678. {$endif}
  679. symtableproc:=nil;
  680. ppufile.getderef(procdefinitionderef);
  681. ppufile.getsmallset(callnodeflags);
  682. methodpointer:=ppuloadnode(ppufile);
  683. methodpointerinit:=ppuloadnode(ppufile);
  684. methodpointerdone:=ppuloadnode(ppufile);
  685. _funcretnode:=ppuloadnode(ppufile);
  686. inlinecode:=ppuloadnode(ppufile);
  687. end;
  688. procedure tcallnode.ppuwrite(ppufile:tcompilerppufile);
  689. begin
  690. inherited ppuwrite(ppufile);
  691. ppufile.putderef(symtableprocentryderef);
  692. ppufile.putderef(procdefinitionderef);
  693. ppufile.putsmallset(callnodeflags);
  694. ppuwritenode(ppufile,methodpointer);
  695. ppuwritenode(ppufile,methodpointerinit);
  696. ppuwritenode(ppufile,methodpointerdone);
  697. ppuwritenode(ppufile,_funcretnode);
  698. ppuwritenode(ppufile,inlinecode);
  699. end;
  700. procedure tcallnode.buildderefimpl;
  701. begin
  702. inherited buildderefimpl;
  703. symtableprocentryderef.build(symtableprocentry);
  704. procdefinitionderef.build(procdefinition);
  705. if assigned(methodpointer) then
  706. methodpointer.buildderefimpl;
  707. if assigned(methodpointerinit) then
  708. methodpointerinit.buildderefimpl;
  709. if assigned(methodpointerdone) then
  710. methodpointerdone.buildderefimpl;
  711. if assigned(_funcretnode) then
  712. _funcretnode.buildderefimpl;
  713. if assigned(inlinecode) then
  714. inlinecode.buildderefimpl;
  715. end;
  716. procedure tcallnode.derefimpl;
  717. var
  718. pt : tcallparanode;
  719. currpara : tparaitem;
  720. begin
  721. inherited derefimpl;
  722. symtableprocentry:=tprocsym(symtableprocentryderef.resolve);
  723. symtableproc:=symtableprocentry.owner;
  724. procdefinition:=tprocdef(procdefinitionderef.resolve);
  725. if assigned(methodpointer) then
  726. methodpointer.derefimpl;
  727. if assigned(methodpointerinit) then
  728. methodpointerinit.derefimpl;
  729. if assigned(methodpointerdone) then
  730. methodpointerdone.derefimpl;
  731. if assigned(_funcretnode) then
  732. _funcretnode.derefimpl;
  733. if assigned(inlinecode) then
  734. inlinecode.derefimpl;
  735. { Connect paraitems }
  736. pt:=tcallparanode(left);
  737. while assigned(pt) and
  738. (cpf_varargs_para in pt.callparaflags) do
  739. pt:=tcallparanode(pt.right);
  740. currpara:=tparaitem(procdefinition.Para.last);
  741. while assigned(currpara) do
  742. begin
  743. if not assigned(pt) then
  744. internalerror(200311077);
  745. pt.paraitem:=currpara;
  746. pt:=tcallparanode(pt.right);
  747. currpara:=tparaitem(currpara.previous);
  748. end;
  749. if assigned(currpara) or assigned(pt) then
  750. internalerror(200311078);
  751. end;
  752. function tcallnode.getcopy : tnode;
  753. var
  754. n : tcallnode;
  755. hp : tparaitem;
  756. oldleft : tnode;
  757. begin
  758. { Need to use a hack here to prevent the parameters from being copied.
  759. The parameters must be copied between methodpointerinit/methodpointerdone because
  760. the can reference methodpointer }
  761. oldleft:=left;
  762. left:=nil;
  763. n:=tcallnode(inherited getcopy);
  764. left:=oldleft;
  765. n.symtableprocentry:=symtableprocentry;
  766. n.symtableproc:=symtableproc;
  767. n.procdefinition:=procdefinition;
  768. n.restype := restype;
  769. n.callnodeflags := callnodeflags;
  770. if assigned(methodpointerinit) then
  771. n.methodpointerinit:=methodpointerinit.getcopy
  772. else
  773. n.methodpointerinit:=nil;
  774. { methodpointerinit is copied, now references to the temp will also be copied
  775. correctly. We can now copy the parameters and methodpointer }
  776. if assigned(left) then
  777. n.left:=left.getcopy
  778. else
  779. n.left:=nil;
  780. if assigned(methodpointer) then
  781. n.methodpointer:=methodpointer.getcopy
  782. else
  783. n.methodpointer:=nil;
  784. if assigned(methodpointerdone) then
  785. n.methodpointerdone:=methodpointerdone.getcopy
  786. else
  787. n.methodpointerdone:=nil;
  788. if assigned(_funcretnode) then
  789. n._funcretnode:=_funcretnode.getcopy
  790. else
  791. n._funcretnode:=nil;
  792. if assigned(inlinecode) then
  793. n.inlinecode:=inlinecode.getcopy
  794. else
  795. n.inlinecode:=nil;
  796. if assigned(varargsparas) then
  797. begin
  798. n.varargsparas:=tvarargspara.create;
  799. hp:=tparaitem(varargsparas.first);
  800. while assigned(hp) do
  801. begin
  802. n.varargsparas.concat(hp.getcopy);
  803. hp:=tparaitem(hp.next);
  804. end;
  805. end
  806. else
  807. n.varargsparas:=nil;
  808. result:=n;
  809. end;
  810. procedure tcallnode.insertintolist(l : tnodelist);
  811. begin
  812. end;
  813. procedure tcallnode.convert_carg_array_of_const;
  814. var
  815. hp : tarrayconstructornode;
  816. oldleft : tcallparanode;
  817. begin
  818. oldleft:=tcallparanode(left);
  819. if oldleft.left.nodetype<>arrayconstructorn then
  820. begin
  821. CGMessage1(type_e_wrong_type_in_array_constructor,oldleft.left.resulttype.def.typename);
  822. exit;
  823. end;
  824. { Get arrayconstructor node and insert typeconvs }
  825. hp:=tarrayconstructornode(oldleft.left);
  826. hp.insert_typeconvs;
  827. { Add c args parameters }
  828. { It could be an empty set }
  829. if assigned(hp) and
  830. assigned(hp.left) then
  831. begin
  832. while assigned(hp) do
  833. begin
  834. left:=ccallparanode.create(hp.left,left);
  835. { set callparanode resulttype and flags }
  836. left.resulttype:=hp.left.resulttype;
  837. include(tcallparanode(left).callparaflags,cpf_varargs_para);
  838. hp.left:=nil;
  839. hp:=tarrayconstructornode(hp.right);
  840. end;
  841. end;
  842. { Remove value of old array of const parameter, but keep it
  843. in the list because it is required for bind_paraitem.
  844. Generate a nothign to keep callparanoed.left valid }
  845. oldleft.left.free;
  846. oldleft.left:=cnothingnode.create;
  847. end;
  848. procedure tcallnode.verifyabstract(p : tnamedindexitem;arg:pointer);
  849. var
  850. hp : tprocdef;
  851. j: integer;
  852. begin
  853. if (tsym(p).typ=procsym) then
  854. begin
  855. for j:=1 to tprocsym(p).procdef_count do
  856. begin
  857. { index starts at 1 }
  858. hp:=tprocsym(p).procdef[j];
  859. { If this is an abstract method insert into the list }
  860. if (po_abstractmethod in hp.procoptions) then
  861. AbstractMethodsList.Insert(hp.procsym.name)
  862. else
  863. { If this symbol is already in the list, and it is
  864. an overriding method or dynamic, then remove it from the list
  865. }
  866. begin
  867. { symbol was found }
  868. if AbstractMethodsList.Find(hp.procsym.name) <> nil then
  869. begin
  870. if po_overridingmethod in hp.procoptions then
  871. AbstractMethodsList.Remove(hp.procsym.name);
  872. end;
  873. end;
  874. end;
  875. end;
  876. end;
  877. procedure tcallnode.verifyabstractcalls;
  878. var
  879. objectdf : tobjectdef;
  880. parents : tlinkedlist;
  881. objectinfo : tobjectinfoitem;
  882. stritem : tstringlistitem;
  883. begin
  884. objectdf := nil;
  885. { verify if trying to create an instance of a class which contains
  886. non-implemented abstract methods }
  887. { first verify this class type, no class than exit }
  888. { also, this checking can only be done if the constructor is directly
  889. called, indirect constructor calls cannot be checked.
  890. }
  891. if assigned(methodpointer) then
  892. begin
  893. if (methodpointer.resulttype.def.deftype = objectdef) then
  894. objectdf:=tobjectdef(methodpointer.resulttype.def)
  895. else
  896. if (methodpointer.resulttype.def.deftype = classrefdef) and
  897. (tclassrefdef(methodpointer.resulttype.def).pointertype.def.deftype = objectdef) and
  898. (methodpointer.nodetype in [typen,loadvmtaddrn]) then
  899. objectdf:=tobjectdef(tclassrefdef(methodpointer.resulttype.def).pointertype.def);
  900. end;
  901. if not assigned(objectdf) then
  902. exit;
  903. parents := tlinkedlist.create;
  904. AbstractMethodsList := tstringlist.create;
  905. { insert all parents in this class : the first item in the
  906. list will be the base parent of the class .
  907. }
  908. while assigned(objectdf) do
  909. begin
  910. objectinfo:=tobjectinfoitem.create(objectdf);
  911. parents.insert(objectinfo);
  912. objectdf := objectdf.childof;
  913. end;
  914. { now all parents are in the correct order
  915. insert all abstract methods in the list, and remove
  916. those which are overriden by parent classes.
  917. }
  918. objectinfo:=tobjectinfoitem(parents.first);
  919. while assigned(objectinfo) do
  920. begin
  921. objectdf := objectinfo.objinfo;
  922. if assigned(objectdf.symtable) then
  923. objectdf.symtable.foreach({$ifdef FPCPROCVAR}@{$endif}verifyabstract,nil);
  924. objectinfo:=tobjectinfoitem(objectinfo.next);
  925. end;
  926. if assigned(parents) then
  927. parents.free;
  928. { Finally give out a warning for each abstract method still in the list }
  929. stritem := tstringlistitem(AbstractMethodsList.first);
  930. if assigned(stritem) then
  931. Message1(type_w_instance_with_abstract,objectdf.objname^);
  932. while assigned(stritem) do
  933. begin
  934. if assigned(stritem.fpstr) then
  935. Message1(sym_h_param_list,stritem.str);
  936. stritem := tstringlistitem(stritem.next);
  937. end;
  938. if assigned(AbstractMethodsList) then
  939. AbstractMethodsList.Free;
  940. end;
  941. function tcallnode.gen_self_tree_methodpointer:tnode;
  942. var
  943. hsym : tvarsym;
  944. begin
  945. { find self field in methodpointer record }
  946. hsym:=tvarsym(trecorddef(methodpointertype.def).symtable.search('self'));
  947. if not assigned(hsym) then
  948. internalerror(200305251);
  949. { Load tmehodpointer(right).self }
  950. result:=csubscriptnode.create(
  951. hsym,
  952. ctypeconvnode.create_explicit(right.getcopy,methodpointertype));
  953. end;
  954. function tcallnode.gen_self_tree:tnode;
  955. var
  956. selftree : tnode;
  957. begin
  958. selftree:=nil;
  959. { inherited }
  960. if (cnf_inherited in callnodeflags) then
  961. selftree:=load_self_node
  962. else
  963. { constructors }
  964. if (procdefinition.proctypeoption=potype_constructor) then
  965. begin
  966. { push 0 as self when allocation is needed }
  967. if (methodpointer.resulttype.def.deftype=classrefdef) or
  968. (cnf_new_call in callnodeflags) then
  969. selftree:=cpointerconstnode.create(0,voidpointertype)
  970. else
  971. begin
  972. if methodpointer.nodetype=typen then
  973. selftree:=load_self_node
  974. else
  975. selftree:=methodpointer.getcopy;
  976. end;
  977. end
  978. else
  979. { Calling a static/class method }
  980. if (po_classmethod in procdefinition.procoptions) or
  981. (po_staticmethod in procdefinition.procoptions) then
  982. begin
  983. if (procdefinition.deftype<>procdef) then
  984. internalerror(200305062);
  985. if (oo_has_vmt in tprocdef(procdefinition)._class.objectoptions) then
  986. begin
  987. { we only need the vmt, loading self is not required and there is no
  988. need to check for typen, because that will always get the
  989. loadvmtaddrnode added }
  990. selftree:=methodpointer.getcopy;
  991. if methodpointer.resulttype.def.deftype<>classrefdef then
  992. selftree:=cloadvmtaddrnode.create(selftree);
  993. end
  994. else
  995. selftree:=cpointerconstnode.create(0,voidpointertype);
  996. end
  997. else
  998. begin
  999. if methodpointer.nodetype=typen then
  1000. selftree:=load_self_node
  1001. else
  1002. selftree:=methodpointer.getcopy;
  1003. end;
  1004. result:=selftree;
  1005. end;
  1006. function tcallnode.gen_vmt_tree:tnode;
  1007. var
  1008. vmttree : tnode;
  1009. begin
  1010. vmttree:=nil;
  1011. if not(procdefinition.proctypeoption in [potype_constructor,potype_destructor]) then
  1012. internalerror(200305051);
  1013. { inherited call, no create/destroy }
  1014. if (cnf_inherited in callnodeflags) then
  1015. vmttree:=cpointerconstnode.create(0,voidpointertype)
  1016. else
  1017. { do not create/destroy when called from member function
  1018. without specifying self explicit }
  1019. if (cnf_member_call in callnodeflags) then
  1020. begin
  1021. if (methodpointer.resulttype.def.deftype=classrefdef) and
  1022. (procdefinition.proctypeoption=potype_constructor) then
  1023. vmttree:=methodpointer.getcopy
  1024. else
  1025. vmttree:=cpointerconstnode.create(0,voidpointertype);
  1026. end
  1027. else
  1028. { constructor with extended syntax called from new }
  1029. if (cnf_new_call in callnodeflags) then
  1030. vmttree:=cloadvmtaddrnode.create(ctypenode.create(methodpointer.resulttype))
  1031. else
  1032. { destructor with extended syntax called from dispose }
  1033. if (cnf_dispose_call in callnodeflags) then
  1034. vmttree:=cloadvmtaddrnode.create(methodpointer.getcopy)
  1035. else
  1036. if (methodpointer.resulttype.def.deftype=classrefdef) then
  1037. begin
  1038. { constructor call via classreference => allocate memory }
  1039. if (procdefinition.proctypeoption=potype_constructor) and
  1040. is_class(tclassrefdef(methodpointer.resulttype.def).pointertype.def) then
  1041. begin
  1042. vmttree:=methodpointer.getcopy;
  1043. { Only a typenode can be passed when it is called with <class of xx>.create }
  1044. if vmttree.nodetype=typen then
  1045. vmttree:=cloadvmtaddrnode.create(vmttree);
  1046. end
  1047. else
  1048. vmttree:=cpointerconstnode.create(0,voidpointertype);
  1049. end
  1050. else
  1051. { class }
  1052. if is_class(methodpointer.resulttype.def) then
  1053. begin
  1054. { destructor: release instance, flag(vmt)=1
  1055. constructor: direct call, do nothing, leave vmt=0 }
  1056. if (procdefinition.proctypeoption=potype_destructor) then
  1057. vmttree:=cpointerconstnode.create(1,voidpointertype)
  1058. else
  1059. vmttree:=cpointerconstnode.create(0,voidpointertype);
  1060. end
  1061. else
  1062. { object }
  1063. begin
  1064. { destructor: direct call, no dispose, vmt=0
  1065. constructor: initialize object, load vmt }
  1066. if (procdefinition.proctypeoption=potype_constructor) then
  1067. { old styled inherited call? }
  1068. if (methodpointer.nodetype=typen) then
  1069. vmttree:=cpointerconstnode.create(0,voidpointertype)
  1070. else
  1071. vmttree:=cloadvmtaddrnode.create(ctypenode.create(methodpointer.resulttype))
  1072. else
  1073. vmttree:=cpointerconstnode.create(0,voidpointertype);
  1074. end;
  1075. result:=vmttree;
  1076. end;
  1077. procedure tcallnode.bind_paraitem;
  1078. var
  1079. i : integer;
  1080. pt : tcallparanode;
  1081. oldppt : ^tcallparanode;
  1082. varargspara,
  1083. currpara : tparaitem;
  1084. used_by_callnode : boolean;
  1085. hiddentree : tnode;
  1086. newstatement : tstatementnode;
  1087. temp : ttempcreatenode;
  1088. begin
  1089. pt:=tcallparanode(left);
  1090. oldppt:=@left;
  1091. { flag all callparanodes that belong to the varargs }
  1092. i:=paralength;
  1093. while (i>procdefinition.maxparacount) do
  1094. begin
  1095. include(pt.callparaflags,cpf_varargs_para);
  1096. oldppt:[email protected];
  1097. pt:=tcallparanode(pt.right);
  1098. dec(i);
  1099. end;
  1100. { skip varargs that are inserted by array of const }
  1101. while assigned(pt) and
  1102. (cpf_varargs_para in pt.callparaflags) do
  1103. pt:=tcallparanode(pt.right);
  1104. { process normal parameters and insert hidden parameters }
  1105. currpara:=tparaitem(procdefinition.Para.last);
  1106. while assigned(currpara) do
  1107. begin
  1108. if currpara.is_hidden then
  1109. begin
  1110. { generate hidden tree }
  1111. used_by_callnode:=false;
  1112. hiddentree:=nil;
  1113. if (vo_is_funcret in tvarsym(currpara.parasym).varoptions) then
  1114. begin
  1115. { Generate funcretnode if not specified }
  1116. if assigned(funcretnode) then
  1117. begin
  1118. hiddentree:=funcretnode.getcopy;
  1119. end
  1120. else
  1121. begin
  1122. hiddentree:=internalstatements(newstatement);
  1123. { need to use resulttype instead of procdefinition.rettype,
  1124. because they can be different }
  1125. temp:=ctempcreatenode.create(resulttype,resulttype.def.size,tt_persistent);
  1126. addstatement(newstatement,temp);
  1127. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  1128. addstatement(newstatement,ctemprefnode.create(temp));
  1129. end;
  1130. end
  1131. else
  1132. if vo_is_high_value in tvarsym(currpara.parasym).varoptions then
  1133. begin
  1134. if not assigned(pt) then
  1135. internalerror(200304082);
  1136. { we need the information of the previous parameter }
  1137. hiddentree:=gen_high_tree(pt.left,tparaitem(currpara.previous).paratype.def);
  1138. end
  1139. else
  1140. if vo_is_self in tvarsym(currpara.parasym).varoptions then
  1141. begin
  1142. if assigned(right) then
  1143. hiddentree:=gen_self_tree_methodpointer
  1144. else
  1145. hiddentree:=gen_self_tree;
  1146. end
  1147. else
  1148. if vo_is_vmt in tvarsym(currpara.parasym).varoptions then
  1149. begin
  1150. hiddentree:=gen_vmt_tree;
  1151. end
  1152. else
  1153. if vo_is_parentfp in tvarsym(currpara.parasym).varoptions then
  1154. begin
  1155. if not(assigned(procdefinition.owner.defowner)) then
  1156. internalerror(200309287);
  1157. hiddentree:=cloadparentfpnode.create(tprocdef(procdefinition.owner.defowner));
  1158. end
  1159. {$ifdef powerpc}
  1160. else
  1161. { lib parameter has no special type but proccalloptions must be a syscall }
  1162. if (target_info.system in [system_powerpc_morphos,system_m68k_amiga]) and
  1163. (procdefinition.proccalloption=pocall_syscall) then
  1164. begin
  1165. hiddentree:=cloadnode.create(tprocdef(procdefinition).libsym,tprocdef(procdefinition).libsym.owner);
  1166. end
  1167. {$endif powerpc}
  1168. else
  1169. { add the hidden parameter }
  1170. if not assigned(hiddentree) then
  1171. internalerror(200304073);
  1172. { Already insert para and let the previous node point to
  1173. this new node }
  1174. pt:=ccallparanode.create(hiddentree,oldppt^);
  1175. pt.used_by_callnode:=used_by_callnode;
  1176. oldppt^:=pt;
  1177. end;
  1178. if not assigned(pt) then
  1179. internalerror(200310052);
  1180. pt.paraitem:=currpara;
  1181. oldppt:[email protected];
  1182. pt:=tcallparanode(pt.right);
  1183. currpara:=tparaitem(currpara.previous)
  1184. end;
  1185. { Create paraitems for varargs }
  1186. pt:=tcallparanode(left);
  1187. while assigned(pt) do
  1188. begin
  1189. if cpf_varargs_para in pt.callparaflags then
  1190. begin
  1191. if not assigned(varargsparas) then
  1192. varargsparas:=tvarargspara.create;
  1193. varargspara:=tparaitem.create;
  1194. varargspara.paratyp:=vs_value;
  1195. varargspara.paratype:=pt.resulttype;
  1196. { varargspara is left-right, use insert
  1197. instead of concat }
  1198. varargsparas.insert(varargspara);
  1199. pt.paraitem:=varargspara;
  1200. end;
  1201. pt:=tcallparanode(pt.right);
  1202. end;
  1203. end;
  1204. function tcallnode.det_resulttype:tnode;
  1205. var
  1206. candidates : tcallcandidates;
  1207. oldcallnode : tcallnode;
  1208. hpt : tnode;
  1209. pt : tcallparanode;
  1210. lastpara : longint;
  1211. currpara : tparaitem;
  1212. cand_cnt : integer;
  1213. i : longint;
  1214. method_must_be_valid,
  1215. is_const : boolean;
  1216. hp : tnode;
  1217. mptemp : ttempcreatenode;
  1218. newstatement : tstatementnode;
  1219. label
  1220. errorexit;
  1221. begin
  1222. result:=nil;
  1223. candidates:=nil;
  1224. oldcallnode:=aktcallnode;
  1225. aktcallnode:=nil;
  1226. { determine length of parameter list }
  1227. pt:=tcallparanode(left);
  1228. paralength:=0;
  1229. while assigned(pt) do
  1230. begin
  1231. inc(paralength);
  1232. pt:=tcallparanode(pt.right);
  1233. end;
  1234. { determine the type of the parameters }
  1235. if assigned(left) then
  1236. begin
  1237. tcallparanode(left).get_paratype;
  1238. if codegenerror then
  1239. goto errorexit;
  1240. end;
  1241. if assigned(methodpointer) then
  1242. begin
  1243. resulttypepass(methodpointer);
  1244. hp:=methodpointer;
  1245. while assigned(hp) and
  1246. (hp.nodetype=typeconvn) do
  1247. hp:=tunarynode(hp).left;
  1248. if assigned(hp) and
  1249. (
  1250. { call result must always be loaded in temp to prevent
  1251. double creation }
  1252. (hp.nodetype=calln)
  1253. { Also optimize also complex loads }
  1254. {$warning Complex loads can also be optimized}
  1255. // not(hp.nodetype in [typen,loadvmtaddrn,loadn])
  1256. ) then
  1257. begin
  1258. { methodpointer loading }
  1259. methodpointerinit:=internalstatements(newstatement);
  1260. mptemp:=ctempcreatenode.create_reg(methodpointer.resulttype,methodpointer.resulttype.def.size,tt_persistent);
  1261. addstatement(newstatement,mptemp);
  1262. addstatement(newstatement,cassignmentnode.create(
  1263. ctemprefnode.create(mptemp),
  1264. methodpointer));
  1265. resulttypepass(methodpointerinit);
  1266. { new methodpointer is only a temp reference }
  1267. methodpointer:=ctemprefnode.create(mptemp);
  1268. resulttypepass(methodpointer);
  1269. { methodpointer cleanup }
  1270. methodpointerdone:=ctempdeletenode.create(mptemp);
  1271. resulttypepass(methodpointerdone);
  1272. end;
  1273. end;
  1274. { procedure variable ? }
  1275. if assigned(right) then
  1276. begin
  1277. set_varstate(right,vs_used,true);
  1278. resulttypepass(right);
  1279. if codegenerror then
  1280. exit;
  1281. procdefinition:=tabstractprocdef(right.resulttype.def);
  1282. { Compare parameters from right to left }
  1283. currpara:=tparaitem(procdefinition.Para.last);
  1284. { Skip default parameters }
  1285. if not(po_varargs in procdefinition.procoptions) then
  1286. begin
  1287. { ignore hidden parameters }
  1288. while assigned(currpara) and (currpara.is_hidden) do
  1289. currpara:=tparaitem(currpara.previous);
  1290. for i:=1 to procdefinition.maxparacount-paralength do
  1291. begin
  1292. if not assigned(currpara) then
  1293. internalerror(200402261);
  1294. if not assigned(currpara.defaultvalue) then
  1295. begin
  1296. CGMessage(parser_e_wrong_parameter_size);
  1297. goto errorexit;
  1298. end;
  1299. currpara:=tparaitem(currpara.previous);
  1300. end;
  1301. end;
  1302. while assigned(currpara) and (currpara.is_hidden) do
  1303. currpara:=tparaitem(currpara.previous);
  1304. pt:=tcallparanode(left);
  1305. lastpara:=paralength;
  1306. while assigned(currpara) and assigned(pt) do
  1307. begin
  1308. { only goto next para if we're out of the varargs }
  1309. if not(po_varargs in procdefinition.procoptions) or
  1310. (lastpara<=procdefinition.maxparacount) then
  1311. begin
  1312. repeat
  1313. currpara:=tparaitem(currpara.previous);
  1314. until (not assigned(currpara)) or (not currpara.is_hidden);
  1315. end;
  1316. pt:=tcallparanode(pt.right);
  1317. dec(lastpara);
  1318. end;
  1319. if assigned(pt) or
  1320. (assigned(currpara) and
  1321. not assigned(currpara.defaultvalue)) then
  1322. begin
  1323. if assigned(pt) then
  1324. aktfilepos:=pt.fileinfo;
  1325. CGMessage(parser_e_wrong_parameter_size);
  1326. goto errorexit;
  1327. end;
  1328. end
  1329. else
  1330. { not a procedure variable }
  1331. begin
  1332. { do we know the procedure to call ? }
  1333. if not(assigned(procdefinition)) then
  1334. begin
  1335. candidates:=tcallcandidates.create(symtableprocentry,symtableproc,left,(nf_isproperty in flags));
  1336. { no procedures found? then there is something wrong
  1337. with the parameter size or the procedures are
  1338. not accessible }
  1339. if candidates.count=0 then
  1340. begin
  1341. { when it's an auto inherited call and there
  1342. is no procedure found, but the procedures
  1343. were defined with overload directive and at
  1344. least two procedures are defined then we ignore
  1345. this inherited by inserting a nothingn. Only
  1346. do this ugly hack in Delphi mode as it looks more
  1347. like a bug. It's also not documented }
  1348. if (m_delphi in aktmodeswitches) and
  1349. (cnf_anon_inherited in callnodeflags) and
  1350. (symtableprocentry.owner.symtabletype=objectsymtable) and
  1351. (po_overload in symtableprocentry.first_procdef.procoptions) and
  1352. (symtableprocentry.procdef_count>=2) then
  1353. result:=cnothingnode.create
  1354. else
  1355. begin
  1356. { in tp mode we can try to convert to procvar if
  1357. there are no parameters specified. Only try it
  1358. when there is only one proc definition, else the
  1359. loadnode will give a strange error }
  1360. if not(assigned(left)) and
  1361. not(cnf_inherited in callnodeflags) and
  1362. (m_tp_procvar in aktmodeswitches) and
  1363. (symtableprocentry.procdef_count=1) then
  1364. begin
  1365. hpt:=cloadnode.create(tprocsym(symtableprocentry),symtableproc);
  1366. if assigned(methodpointer) then
  1367. tloadnode(hpt).set_mp(methodpointer.getcopy);
  1368. resulttypepass(hpt);
  1369. result:=hpt;
  1370. end
  1371. else
  1372. begin
  1373. if assigned(left) then
  1374. aktfilepos:=left.fileinfo;
  1375. CGMessage(parser_e_wrong_parameter_size);
  1376. symtableprocentry.write_parameter_lists(nil);
  1377. end;
  1378. end;
  1379. goto errorexit;
  1380. end;
  1381. { Retrieve information about the candidates }
  1382. candidates.get_information;
  1383. {$ifdef EXTDEBUG}
  1384. { Display info when multiple candidates are found }
  1385. if candidates.count>1 then
  1386. candidates.dump_info(V_Debug);
  1387. {$endif EXTDEBUG}
  1388. { Choose the best candidate and count the number of
  1389. candidates left }
  1390. cand_cnt:=candidates.choose_best(procdefinition);
  1391. { All parameters are checked, check if there are any
  1392. procedures left }
  1393. if cand_cnt>0 then
  1394. begin
  1395. { Multiple candidates left? }
  1396. if cand_cnt>1 then
  1397. begin
  1398. CGMessage(type_e_cant_choose_overload_function);
  1399. {$ifdef EXTDEBUG}
  1400. candidates.dump_info(V_Hint);
  1401. {$else EXTDEBUG}
  1402. candidates.list(false);
  1403. {$endif EXTDEBUG}
  1404. { we'll just use the first candidate to make the
  1405. call }
  1406. end;
  1407. { assign procdefinition }
  1408. if symtableproc=nil then
  1409. symtableproc:=procdefinition.owner;
  1410. { update browser information }
  1411. if make_ref then
  1412. begin
  1413. tprocdef(procdefinition).lastref:=tref.create(tprocdef(procdefinition).lastref,@fileinfo);
  1414. inc(tprocdef(procdefinition).refcount);
  1415. if tprocdef(procdefinition).defref=nil then
  1416. tprocdef(procdefinition).defref:=tprocdef(procdefinition).lastref;
  1417. end;
  1418. end
  1419. else
  1420. begin
  1421. { No candidates left, this must be a type error,
  1422. because wrong size is already checked. procdefinition
  1423. is filled with the first (random) definition that is
  1424. found. We use this definition to display a nice error
  1425. message that the wrong type is passed }
  1426. candidates.find_wrong_para;
  1427. candidates.list(true);
  1428. {$ifdef EXTDEBUG}
  1429. candidates.dump_info(V_Hint);
  1430. {$endif EXTDEBUG}
  1431. { We can not proceed, release all procs and exit }
  1432. candidates.free;
  1433. goto errorexit;
  1434. end;
  1435. candidates.free;
  1436. end; { end of procedure to call determination }
  1437. end;
  1438. { add needed default parameters }
  1439. if assigned(procdefinition) and
  1440. (paralength<procdefinition.maxparacount) then
  1441. begin
  1442. currpara:=tparaitem(procdefinition.Para.first);
  1443. i:=0;
  1444. while (i<paralength) do
  1445. begin
  1446. if not assigned(currpara) then
  1447. internalerror(200306181);
  1448. if not currpara.is_hidden then
  1449. inc(i);
  1450. currpara:=tparaitem(currpara.next);
  1451. end;
  1452. while assigned(currpara) and currpara.is_hidden do
  1453. currpara:=tparaitem(currpara.next);
  1454. while assigned(currpara) do
  1455. begin
  1456. if not assigned(currpara.defaultvalue) then
  1457. internalerror(200212142);
  1458. left:=ccallparanode.create(genconstsymtree(tconstsym(currpara.defaultvalue)),left);
  1459. { Ignore vs_hidden parameters }
  1460. repeat
  1461. currpara:=tparaitem(currpara.next);
  1462. until (not assigned(currpara)) or (not currpara.is_hidden);
  1463. end;
  1464. end;
  1465. { handle predefined procedures }
  1466. is_const:=(po_internconst in procdefinition.procoptions) and
  1467. ((block_type in [bt_const,bt_type]) or
  1468. (assigned(left) and (tcallparanode(left).left.nodetype in [realconstn,ordconstn])));
  1469. if (procdefinition.proccalloption=pocall_internproc) or is_const then
  1470. begin
  1471. if assigned(left) then
  1472. begin
  1473. { ptr and settextbuf needs two args }
  1474. if assigned(tcallparanode(left).right) then
  1475. begin
  1476. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,left);
  1477. left:=nil;
  1478. end
  1479. else
  1480. begin
  1481. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,tcallparanode(left).left);
  1482. tcallparanode(left).left:=nil;
  1483. end;
  1484. end
  1485. else
  1486. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,nil);
  1487. result:=hpt;
  1488. goto errorexit;
  1489. end;
  1490. { ensure that the result type is set }
  1491. if not(cnf_restypeset in callnodeflags) then
  1492. begin
  1493. { constructors return their current class type, not the type where the
  1494. constructor is declared, this can be different because of inheritance }
  1495. if (procdefinition.proctypeoption=potype_constructor) and
  1496. assigned(methodpointer) and
  1497. assigned(methodpointer.resulttype.def) and
  1498. (methodpointer.resulttype.def.deftype=classrefdef) then
  1499. resulttype:=tclassrefdef(methodpointer.resulttype.def).pointertype
  1500. else
  1501. resulttype:=procdefinition.rettype;
  1502. end
  1503. else
  1504. resulttype:=restype;
  1505. {if resulttype.def.needs_inittable then
  1506. include(current_procinfo.flags,pi_needs_implicit_finally);}
  1507. if assigned(methodpointer) then
  1508. begin
  1509. { when methodpointer is a callnode we must load it first into a
  1510. temp to prevent the processing callnode twice }
  1511. if (methodpointer.nodetype=calln) then
  1512. internalerror(200405121);
  1513. { direct call to inherited abstract method, then we
  1514. can already give a error in the compiler instead
  1515. of a runtime error }
  1516. if (cnf_inherited in callnodeflags) and
  1517. (po_abstractmethod in procdefinition.procoptions) then
  1518. CGMessage(cg_e_cant_call_abstract_method);
  1519. { if an inherited con- or destructor should be }
  1520. { called in a con- or destructor then a warning }
  1521. { will be made }
  1522. { con- and destructors need a pointer to the vmt }
  1523. if (cnf_inherited in callnodeflags) and
  1524. (procdefinition.proctypeoption in [potype_constructor,potype_destructor]) and
  1525. is_object(methodpointer.resulttype.def) and
  1526. not(current_procinfo.procdef.proctypeoption in [potype_constructor,potype_destructor]) then
  1527. CGMessage(cg_w_member_cd_call_from_method);
  1528. if methodpointer.nodetype<>typen then
  1529. begin
  1530. { Remove all postfix operators }
  1531. hpt:=methodpointer;
  1532. while assigned(hpt) and (hpt.nodetype in [subscriptn,vecn]) do
  1533. hpt:=tunarynode(hpt).left;
  1534. if (procdefinition.proctypeoption=potype_constructor) and
  1535. assigned(symtableproc) and
  1536. (symtableproc.symtabletype=withsymtable) and
  1537. (tnode(twithsymtable(symtableproc).withrefnode).nodetype=temprefn) then
  1538. CGmessage(cg_e_cannot_call_cons_dest_inside_with);
  1539. { R.Init then R will be initialized by the constructor,
  1540. Also allow it for simple loads }
  1541. if (procdefinition.proctypeoption=potype_constructor) or
  1542. ((hpt.nodetype=loadn) and
  1543. (
  1544. (methodpointer.resulttype.def.deftype=classrefdef) or
  1545. (
  1546. (methodpointer.resulttype.def.deftype=objectdef) and
  1547. not(oo_has_virtual in tobjectdef(methodpointer.resulttype.def).objectoptions)
  1548. )
  1549. )
  1550. ) then
  1551. method_must_be_valid:=false
  1552. else
  1553. method_must_be_valid:=true;
  1554. set_varstate(methodpointer,vs_used,method_must_be_valid);
  1555. { The object is already used if it is called once }
  1556. if (hpt.nodetype=loadn) and
  1557. (tloadnode(hpt).symtableentry.typ=varsym) then
  1558. tvarsym(tloadnode(hpt).symtableentry).varstate:=vs_used;
  1559. end;
  1560. { if we are calling the constructor check for abstract
  1561. methods. Ignore inherited and member calls, because the
  1562. class is then already created }
  1563. if (procdefinition.proctypeoption=potype_constructor) and
  1564. not(cnf_inherited in callnodeflags) and
  1565. not(cnf_member_call in callnodeflags) then
  1566. verifyabstractcalls;
  1567. end
  1568. else
  1569. begin
  1570. { When this is method the methodpointer must be available }
  1571. if (right=nil) and
  1572. (procdefinition.owner.symtabletype=objectsymtable) then
  1573. internalerror(200305061);
  1574. end;
  1575. { Change loading of array of const to varargs }
  1576. if assigned(left) and
  1577. is_array_of_const(tparaitem(procdefinition.para.last).paratype.def) and
  1578. (procdefinition.proccalloption in [pocall_cppdecl,pocall_cdecl]) then
  1579. convert_carg_array_of_const;
  1580. { bind paraitems to the callparanodes and insert hidden parameters }
  1581. aktcallnode:=self;
  1582. bind_paraitem;
  1583. { methodpointer is only needed for virtual calls, and
  1584. it should then be loaded with the VMT }
  1585. if (po_virtualmethod in procdefinition.procoptions) and
  1586. not(assigned(methodpointer) and
  1587. (methodpointer.nodetype=typen)) then
  1588. begin
  1589. if not assigned(methodpointer) then
  1590. internalerror(200305063);
  1591. if (methodpointer.resulttype.def.deftype<>classrefdef) then
  1592. begin
  1593. methodpointer:=cloadvmtaddrnode.create(methodpointer);
  1594. resulttypepass(methodpointer);
  1595. end;
  1596. end
  1597. else
  1598. begin
  1599. { not needed anymore }
  1600. methodpointer.free;
  1601. methodpointer:=nil;
  1602. end;
  1603. { insert type conversions for parameters }
  1604. if assigned(left) then
  1605. tcallparanode(left).insert_typeconv(true);
  1606. errorexit:
  1607. aktcallnode:=oldcallnode;
  1608. end;
  1609. procedure tcallnode.order_parameters;
  1610. var
  1611. hp,hpcurr,hpnext,hpfirst,hpprev : tcallparanode;
  1612. currloc : tcgloc;
  1613. begin
  1614. hpfirst:=nil;
  1615. hpcurr:=tcallparanode(left);
  1616. while assigned(hpcurr) do
  1617. begin
  1618. { pull out }
  1619. hpnext:=tcallparanode(hpcurr.right);
  1620. { pull in at the correct place.
  1621. Used order:
  1622. 1. LOC_REFERENCE with smallest offset (x86 only)
  1623. 2. LOC_REFERENCE with most registers
  1624. 3. LOC_REGISTER with most registers }
  1625. currloc:=hpcurr.paraitem.paraloc[callerside].loc;
  1626. hpprev:=nil;
  1627. hp:=hpfirst;
  1628. while assigned(hp) do
  1629. begin
  1630. case currloc of
  1631. LOC_REFERENCE :
  1632. begin
  1633. case hp.paraitem.paraloc[callerside].loc of
  1634. LOC_REFERENCE :
  1635. begin
  1636. { Offset is calculated like:
  1637. sub esp,12
  1638. mov [esp+8],para3
  1639. mov [esp+4],para2
  1640. mov [esp],para1
  1641. call function
  1642. That means the for pushes the para with the
  1643. highest offset (see para3) needs to be pushed first
  1644. }
  1645. if (hpcurr.registersint>hp.registersint)
  1646. {$ifdef x86}
  1647. or (hpcurr.paraitem.paraloc[callerside].reference.offset>hp.paraitem.paraloc[callerside].reference.offset)
  1648. {$endif x86}
  1649. then
  1650. break;
  1651. end;
  1652. LOC_REGISTER,
  1653. LOC_FPUREGISTER :
  1654. break;
  1655. end;
  1656. end;
  1657. LOC_FPUREGISTER,
  1658. LOC_REGISTER :
  1659. begin
  1660. if (hp.paraitem.paraloc[callerside].loc=currloc) and
  1661. (hpcurr.registersint>hp.registersint) then
  1662. break;
  1663. end;
  1664. end;
  1665. hpprev:=hp;
  1666. hp:=tcallparanode(hp.right);
  1667. end;
  1668. hpcurr.right:=hp;
  1669. if assigned(hpprev) then
  1670. hpprev.right:=hpcurr
  1671. else
  1672. hpfirst:=hpcurr;
  1673. { next }
  1674. hpcurr:=hpnext;
  1675. end;
  1676. left:=hpfirst;
  1677. end;
  1678. function tcallnode.replaceparaload(var n: tnode; arg: pointer): foreachnoderesult;
  1679. var
  1680. paras: tcallparanode;
  1681. begin
  1682. result := fen_false;
  1683. if (n.nodetype = loadn) then
  1684. begin
  1685. paras := tcallparanode(left);
  1686. while assigned(paras) and
  1687. (paras.paraitem.parasym <> tloadnode(n).symtableentry) do
  1688. paras := tcallparanode(paras.right);
  1689. if assigned(paras) then
  1690. begin
  1691. n.free;
  1692. n := paras.left.getcopy;
  1693. resulttypepass(n);
  1694. result := fen_true;
  1695. end;
  1696. end;
  1697. end;
  1698. procedure tcallnode.createinlineparas(var createstatement, deletestatement: tstatementnode);
  1699. var
  1700. para: tcallparanode;
  1701. tempnode: ttempcreatenode;
  1702. hp: tnode;
  1703. begin
  1704. { parameters }
  1705. para := tcallparanode(left);
  1706. while assigned(para) do
  1707. begin
  1708. { create temps for value parameters, and also for const parameters }
  1709. { which are passed by value instead of by reference }
  1710. if (para.paraitem.paratyp = vs_value) or
  1711. ((para.paraitem.paratyp = vs_const) and
  1712. not paramanager.push_addr_param(vs_const,para.left.resulttype.def,procdefinition.proccalloption)) then
  1713. begin
  1714. if (cs_regvars in aktglobalswitches) and
  1715. (([vo_regable,vo_fpuregable] * tvarsym(para.paraitem.parasym).varoptions) <> []) and
  1716. (not tvarsym(para.paraitem.parasym).vartype.def.needs_inittable) then
  1717. tempnode := ctempcreatenode.create_reg(para.left.resulttype,para.left.resulttype.def.size,tt_persistent)
  1718. else
  1719. tempnode := ctempcreatenode.create(para.left.resulttype,para.left.resulttype.def.size,tt_persistent);
  1720. addstatement(createstatement,tempnode);
  1721. addstatement(createstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  1722. para.left));
  1723. para.left := ctemprefnode.create(tempnode);
  1724. addstatement(deletestatement,ctempdeletenode.create(tempnode));
  1725. end
  1726. else if node_complexity(para.left) > 1 then
  1727. begin
  1728. if (cs_regvars in aktglobalswitches) and
  1729. not tvarsym(para.paraitem.parasym).vartype.def.needs_inittable then
  1730. tempnode := ctempcreatenode.create_reg(voidpointertype,voidpointertype.def.size,tt_persistent)
  1731. else
  1732. tempnode := ctempcreatenode.create(voidpointertype,voidpointertype.def.size,tt_persistent);
  1733. addstatement(createstatement,tempnode);
  1734. addstatement(createstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  1735. caddrnode.create(para.left)));
  1736. hp := cderefnode.create(ctemprefnode.create(tempnode));
  1737. inserttypeconv_explicit(hp,para.left.resulttype);
  1738. para.left := hp;
  1739. addstatement(deletestatement,ctempdeletenode.create(tempnode));
  1740. end;
  1741. para := tcallparanode(para.right);
  1742. end;
  1743. end;
  1744. function tcallnode.pass_1 : tnode;
  1745. var
  1746. createstatement,deletestatement: tstatementnode;
  1747. createblock,deleteblock: tblocknode;
  1748. label
  1749. errorexit;
  1750. begin
  1751. result:=nil;
  1752. if (procdefinition.proccalloption=pocall_inline) and
  1753. { can we inline this procedure at the node level? }
  1754. (tprocdef(procdefinition).inlininginfo^.inlinenode) then
  1755. begin
  1756. { inherit flags }
  1757. current_procinfo.flags := current_procinfo.flags + ((procdefinition as tprocdef).inlininginfo^.flags*inherited_inlining_flags);
  1758. if assigned(methodpointer) then
  1759. CGMessage(cg_e_unable_inline_object_methods);
  1760. if assigned(right) then
  1761. CGMessage(cg_e_unable_inline_procvar);
  1762. if assigned(inlinecode) then
  1763. internalerror(2004071110);
  1764. if assigned(tprocdef(procdefinition).inlininginfo^.code) then
  1765. result:=tprocdef(procdefinition).inlininginfo^.code.getcopy
  1766. else
  1767. CGMessage(cg_e_no_code_for_inline_stored);
  1768. if assigned(result) then
  1769. begin
  1770. createblock := internalstatements(createstatement);
  1771. deleteblock := internalstatements(deletestatement);
  1772. { replace complex parameters with temps }
  1773. createinlineparas(createstatement,deletestatement);
  1774. { replace the parameter loads with the parameter values }
  1775. foreachnode(result,{$ifdef FPCPROCVAR}@{$endif}replaceparaload,pointer(funcretnode));
  1776. addstatement(createstatement,result);
  1777. addstatement(createstatement,deleteblock);
  1778. result := createblock;
  1779. { consider it must not be inlined if called
  1780. again inside the args or itself }
  1781. procdefinition.proccalloption:=pocall_default;
  1782. firstpass(result);
  1783. procdefinition.proccalloption:=pocall_inline;
  1784. exit;
  1785. end;
  1786. end;
  1787. { calculate the parameter info for the procdef }
  1788. if not procdefinition.has_paraloc_info then
  1789. begin
  1790. procdefinition.requiredargarea:=paramanager.create_paraloc_info(procdefinition,callerside);
  1791. procdefinition.has_paraloc_info:=true;
  1792. end;
  1793. { calculate the parameter size needed for this call include varargs if they are available }
  1794. if assigned(varargsparas) then
  1795. pushedparasize:=paramanager.create_varargs_paraloc_info(procdefinition,varargsparas)
  1796. else
  1797. pushedparasize:=procdefinition.requiredargarea;
  1798. { record maximum parameter size used in this proc }
  1799. current_procinfo.allocate_push_parasize(pushedparasize);
  1800. { work trough all parameters to get the register requirements }
  1801. if assigned(left) then
  1802. tcallparanode(left).det_registers;
  1803. { order parameters }
  1804. order_parameters;
  1805. if assigned(methodpointerinit) then
  1806. firstpass(methodpointerinit);
  1807. if assigned(methodpointerdone) then
  1808. firstpass(methodpointerdone);
  1809. { function result node }
  1810. if assigned(_funcretnode) then
  1811. firstpass(_funcretnode);
  1812. { procedure variable ? }
  1813. if assigned(right) then
  1814. begin
  1815. firstpass(right);
  1816. { procedure does a call }
  1817. if not (block_type in [bt_const,bt_type]) then
  1818. include(current_procinfo.flags,pi_do_call);
  1819. end
  1820. else
  1821. { not a procedure variable }
  1822. begin
  1823. { calc the correture value for the register }
  1824. { handle predefined procedures }
  1825. if (procdefinition.proccalloption=pocall_inline) then
  1826. begin
  1827. { inherit flags }
  1828. current_procinfo.flags := current_procinfo.flags + ((procdefinition as tprocdef).inlininginfo^.flags*inherited_inlining_flags);
  1829. if assigned(methodpointer) then
  1830. CGMessage(cg_e_unable_inline_object_methods);
  1831. if assigned(right) then
  1832. CGMessage(cg_e_unable_inline_procvar);
  1833. if not assigned(inlinecode) then
  1834. begin
  1835. if assigned(tprocdef(procdefinition).inlininginfo^.code) then
  1836. inlinecode:=tprocdef(procdefinition).inlininginfo^.code.getcopy
  1837. else
  1838. CGMessage(cg_e_no_code_for_inline_stored);
  1839. if assigned(inlinecode) then
  1840. begin
  1841. { consider it has not inlined if called
  1842. again inside the args }
  1843. procdefinition.proccalloption:=pocall_default;
  1844. firstpass(inlinecode);
  1845. end;
  1846. end;
  1847. end
  1848. else
  1849. begin
  1850. if not (block_type in [bt_const,bt_type]) then
  1851. include(current_procinfo.flags,pi_do_call);
  1852. end;
  1853. end;
  1854. { implicit finally needed ? }
  1855. if resulttype.def.needs_inittable and
  1856. not paramanager.ret_in_param(resulttype.def,procdefinition.proccalloption) and
  1857. not assigned(funcretnode) then
  1858. include(current_procinfo.flags,pi_needs_implicit_finally);
  1859. { get a register for the return value }
  1860. if (not is_void(resulttype.def)) then
  1861. begin
  1862. if paramanager.ret_in_param(resulttype.def,procdefinition.proccalloption) then
  1863. begin
  1864. expectloc:=LOC_REFERENCE;
  1865. end
  1866. else
  1867. { for win32 records returned in EDX:EAX, we
  1868. move them to memory after ... }
  1869. if (resulttype.def.deftype=recorddef) then
  1870. begin
  1871. expectloc:=LOC_REFERENCE;
  1872. end
  1873. else
  1874. { ansi/widestrings must be registered, so we can dispose them }
  1875. if is_ansistring(resulttype.def) or
  1876. is_widestring(resulttype.def) then
  1877. begin
  1878. expectloc:=LOC_REFERENCE;
  1879. registersint:=1;
  1880. end
  1881. else
  1882. { we have only to handle the result if it is used }
  1883. if (cnf_return_value_used in callnodeflags) then
  1884. begin
  1885. case resulttype.def.deftype of
  1886. enumdef,
  1887. orddef :
  1888. begin
  1889. if (procdefinition.proctypeoption=potype_constructor) then
  1890. begin
  1891. expectloc:=LOC_REGISTER;
  1892. registersint:=1;
  1893. end
  1894. else
  1895. begin
  1896. expectloc:=LOC_REGISTER;
  1897. if is_64bit(resulttype.def) then
  1898. registersint:=2
  1899. else
  1900. registersint:=1;
  1901. end;
  1902. end;
  1903. floatdef :
  1904. begin
  1905. expectloc:=LOC_FPUREGISTER;
  1906. {$ifdef cpufpemu}
  1907. if (cs_fp_emulation in aktmoduleswitches) then
  1908. registersint:=1
  1909. else
  1910. {$endif cpufpemu}
  1911. {$ifdef m68k}
  1912. if (tfloatdef(resulttype.def).typ=s32real) then
  1913. registersint:=1
  1914. else
  1915. {$endif m68k}
  1916. registersfpu:=1;
  1917. end;
  1918. else
  1919. begin
  1920. expectloc:=LOC_REGISTER;
  1921. registersint:=1;
  1922. end;
  1923. end;
  1924. end
  1925. else
  1926. expectloc:=LOC_VOID;
  1927. end
  1928. else
  1929. expectloc:=LOC_VOID;
  1930. {$ifdef m68k}
  1931. { we need one more address register for virtual calls on m68k }
  1932. if (po_virtualmethod in procdefinition.procoptions) then
  1933. inc(registersint);
  1934. {$endif m68k}
  1935. { a fpu can be used in any procedure !! }
  1936. {$ifdef i386}
  1937. registersfpu:=procdefinition.fpu_used;
  1938. {$endif i386}
  1939. { if this is a call to a method calc the registers }
  1940. if (methodpointer<>nil) then
  1941. begin
  1942. if methodpointer.nodetype<>typen then
  1943. begin
  1944. firstpass(methodpointer);
  1945. registersfpu:=max(methodpointer.registersfpu,registersfpu);
  1946. registersint:=max(methodpointer.registersint,registersint);
  1947. {$ifdef SUPPORT_MMX }
  1948. registersmmx:=max(methodpointer.registersmmx,registersmmx);
  1949. {$endif SUPPORT_MMX}
  1950. end;
  1951. end;
  1952. { determine the registers of the procedure variable }
  1953. { is this OK for inlined procs also ?? (PM) }
  1954. if assigned(inlinecode) then
  1955. begin
  1956. registersfpu:=max(inlinecode.registersfpu,registersfpu);
  1957. registersint:=max(inlinecode.registersint,registersint);
  1958. {$ifdef SUPPORT_MMX}
  1959. registersmmx:=max(inlinecode.registersmmx,registersmmx);
  1960. {$endif SUPPORT_MMX}
  1961. end;
  1962. { determine the registers of the procedure variable }
  1963. { is this OK for inlined procs also ?? (PM) }
  1964. if assigned(right) then
  1965. begin
  1966. registersfpu:=max(right.registersfpu,registersfpu);
  1967. registersint:=max(right.registersint,registersint);
  1968. {$ifdef SUPPORT_MMX}
  1969. registersmmx:=max(right.registersmmx,registersmmx);
  1970. {$endif SUPPORT_MMX}
  1971. end;
  1972. { determine the registers of the procedure }
  1973. if assigned(left) then
  1974. begin
  1975. registersfpu:=max(left.registersfpu,registersfpu);
  1976. registersint:=max(left.registersint,registersint);
  1977. {$ifdef SUPPORT_MMX}
  1978. registersmmx:=max(left.registersmmx,registersmmx);
  1979. {$endif SUPPORT_MMX}
  1980. end;
  1981. errorexit:
  1982. if assigned(inlinecode) then
  1983. procdefinition.proccalloption:=pocall_inline;
  1984. end;
  1985. {$ifdef state_tracking}
  1986. function Tcallnode.track_state_pass(exec_known:boolean):boolean;
  1987. var hp:Tcallparanode;
  1988. value:Tnode;
  1989. begin
  1990. track_state_pass:=false;
  1991. hp:=Tcallparanode(left);
  1992. while assigned(hp) do
  1993. begin
  1994. if left.track_state_pass(exec_known) then
  1995. begin
  1996. left.resulttype.def:=nil;
  1997. do_resulttypepass(left);
  1998. end;
  1999. value:=aktstate.find_fact(hp.left);
  2000. if value<>nil then
  2001. begin
  2002. track_state_pass:=true;
  2003. hp.left.destroy;
  2004. hp.left:=value.getcopy;
  2005. do_resulttypepass(hp.left);
  2006. end;
  2007. hp:=Tcallparanode(hp.right);
  2008. end;
  2009. end;
  2010. {$endif}
  2011. function tcallnode.para_count:longint;
  2012. var
  2013. ppn : tcallparanode;
  2014. begin
  2015. result:=0;
  2016. ppn:=tcallparanode(left);
  2017. while assigned(ppn) do
  2018. begin
  2019. if not(assigned(ppn.paraitem) and
  2020. ppn.paraitem.is_hidden) then
  2021. inc(result);
  2022. ppn:=tcallparanode(ppn.right);
  2023. end;
  2024. end;
  2025. function tcallnode.docompare(p: tnode): boolean;
  2026. begin
  2027. docompare :=
  2028. inherited docompare(p) and
  2029. (symtableprocentry = tcallnode(p).symtableprocentry) and
  2030. (procdefinition = tcallnode(p).procdefinition) and
  2031. (methodpointer.isequal(tcallnode(p).methodpointer)) and
  2032. (((cnf_restypeset in callnodeflags) and (cnf_restypeset in tcallnode(p).callnodeflags) and
  2033. (equal_defs(restype.def,tcallnode(p).restype.def))) or
  2034. (not(cnf_restypeset in callnodeflags) and not(cnf_restypeset in tcallnode(p).callnodeflags)));
  2035. end;
  2036. procedure tcallnode.printnodedata(var t:text);
  2037. begin
  2038. if assigned(procdefinition) and
  2039. (procdefinition.deftype=procdef) then
  2040. writeln(t,printnodeindention,'proc = ',tprocdef(procdefinition).fullprocname(true))
  2041. else
  2042. begin
  2043. if assigned(symtableprocentry) then
  2044. writeln(t,printnodeindention,'proc = ',symtableprocentry.name)
  2045. else
  2046. writeln(t,printnodeindention,'proc = <nil>');
  2047. end;
  2048. printnode(t,methodpointer);
  2049. printnode(t,right);
  2050. printnode(t,left);
  2051. end;
  2052. begin
  2053. ccallnode:=tcallnode;
  2054. ccallparanode:=tcallparanode;
  2055. end.
  2056. {
  2057. $Log$
  2058. Revision 1.243 2004-07-16 19:45:15 jonas
  2059. + temps can now also hold fpu values in registers (take care with use,
  2060. bacause of the x86 fpu stack)
  2061. * fpu parameters to node-inlined procedures can now also be put in
  2062. a register
  2063. Revision 1.242 2004/07/15 21:02:05 jonas
  2064. * the condition for when to use a temp in case of reference var/const
  2065. parameters was inverse
  2066. Revision 1.241 2004/07/15 19:55:39 jonas
  2067. + (incomplete) node_complexity function to assess the complexity of a
  2068. tree
  2069. + support for inlining value and const parameters at the node level
  2070. (all procedures without local variables and without formal parameters
  2071. can now be inlined at the node level)
  2072. Revision 1.240 2004/07/12 09:14:04 jonas
  2073. * inline procedures at the node tree level, but only under some very
  2074. limited circumstances for now (only procedures, and only if they have
  2075. no or only vs_out/vs_var parameters).
  2076. * fixed ppudump for inline procedures
  2077. * fixed ppudump for ppc
  2078. Revision 1.239 2004/06/20 08:55:29 florian
  2079. * logs truncated
  2080. Revision 1.238 2004/06/16 20:07:08 florian
  2081. * dwarf branch merged
  2082. Revision 1.237 2004/05/25 18:51:49 peter
  2083. * fix tcallnode.getcopy. the parameters need to be copied after
  2084. methodpointerinit is copied
  2085. Revision 1.236 2004/05/24 17:31:51 peter
  2086. * fix passing of array to open array of array (bug 3113)
  2087. Revision 1.235 2004/05/23 18:28:41 peter
  2088. * methodpointer is loaded into a temp when it was a calln
  2089. Revision 1.234 2004/05/23 15:06:20 peter
  2090. * implicit_finally flag must be set in pass1
  2091. * add check whether the implicit frame is generated when expected
  2092. }