ncal.pas 93 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452
  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,parabase,
  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. {$ifndef VER1_0}
  44. { info for inlining }
  45. inlinelocals: array of tnode;
  46. {$endif VER1_0}
  47. { number of parameters passed from the source, this does not include the hidden parameters }
  48. paralength : smallint;
  49. function gen_self_tree_methodpointer:tnode;
  50. function gen_self_tree:tnode;
  51. function gen_vmt_tree:tnode;
  52. procedure bind_paraitem;
  53. { function return node, this is used to pass the data for a
  54. ret_in_param return value }
  55. _funcretnode : tnode;
  56. procedure setfuncretnode(const returnnode: tnode);
  57. procedure convert_carg_array_of_const;
  58. procedure order_parameters;
  59. procedure createinlineparas(var createstatement, deletestatement: tstatementnode);
  60. function replaceparaload(var n: tnode; arg: pointer): foreachnoderesult;
  61. {$ifndef VER1_0}
  62. procedure createlocaltemps(p:TNamedIndexItem;arg:pointer);
  63. {$endif VER1_0}
  64. protected
  65. pushedparasize : longint;
  66. public
  67. { the symbol containing the definition of the procedure }
  68. { to call }
  69. symtableprocentry : tprocsym;
  70. symtableprocentryderef : tderef;
  71. { symtable where the entry was found, needed for with support }
  72. symtableproc : tsymtable;
  73. { the definition of the procedure to call }
  74. procdefinition : tabstractprocdef;
  75. procdefinitionderef : tderef;
  76. { tree that contains the pointer to the object for this method }
  77. methodpointerinit,
  78. methodpointerdone,
  79. methodpointer : tnode;
  80. { inline function body }
  81. inlinecode : tnode;
  82. { varargs tparaitems }
  83. varargsparas : tvarargspara;
  84. { node that specifies where the result should be put for calls }
  85. { that return their result in a parameter }
  86. property funcretnode: tnode read _funcretnode write setfuncretnode;
  87. { separately specified resulttype for some compilerprocs (e.g. }
  88. { you can't have a function with an "array of char" resulttype }
  89. { the RTL) (JM) }
  90. restype: ttype;
  91. callnodeflags : tcallnodeflags;
  92. { only the processor specific nodes need to override this }
  93. { constructor }
  94. constructor create(l:tnode; v : tprocsym;st : tsymtable; mp: tnode; callflags:tcallnodeflags);virtual;
  95. constructor create_procvar(l,r:tnode);
  96. constructor createintern(const name: string; params: tnode);
  97. constructor createinternres(const name: string; params: tnode; const res: ttype);
  98. constructor createinternreturn(const name: string; params: tnode; returnnode : tnode);
  99. destructor destroy;override;
  100. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  101. procedure ppuwrite(ppufile:tcompilerppufile);override;
  102. procedure buildderefimpl;override;
  103. procedure derefimpl;override;
  104. function getcopy : tnode;override;
  105. { Goes through all symbols in a class and subclasses and calls
  106. verify abstract for each .
  107. }
  108. procedure verifyabstractcalls;
  109. { called for each definition in a class and verifies if a method
  110. is abstract or not, if it is abstract, give out a warning
  111. }
  112. procedure verifyabstract(p : tnamedindexitem;arg:pointer);
  113. procedure insertintolist(l : tnodelist);override;
  114. function pass_1 : tnode;override;
  115. function det_resulttype:tnode;override;
  116. {$ifdef state_tracking}
  117. function track_state_pass(exec_known:boolean):boolean;override;
  118. {$endif state_tracking}
  119. function docompare(p: tnode): boolean; override;
  120. procedure printnodedata(var t:text);override;
  121. function para_count:longint;
  122. private
  123. AbstractMethodsList : TStringList;
  124. end;
  125. tcallnodeclass = class of tcallnode;
  126. tcallparaflag = (
  127. cpf_is_colon_para,
  128. cpf_varargs_para { belongs this para to varargs }
  129. );
  130. tcallparaflags = set of tcallparaflag;
  131. tcallparanode = class(tbinarynode)
  132. public
  133. callparaflags : tcallparaflags;
  134. paraitem : tparaitem;
  135. used_by_callnode : boolean;
  136. { only the processor specific nodes need to override this }
  137. { constructor }
  138. constructor create(expr,next : tnode);virtual;
  139. destructor destroy;override;
  140. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  141. procedure ppuwrite(ppufile:tcompilerppufile);override;
  142. function getcopy : tnode;override;
  143. procedure insertintolist(l : tnodelist);override;
  144. procedure get_paratype;
  145. procedure insert_typeconv(do_count : boolean);
  146. procedure det_registers;
  147. procedure firstcallparan;
  148. procedure secondcallparan;virtual;abstract;
  149. function docompare(p: tnode): boolean; override;
  150. procedure printnodetree(var t:text);override;
  151. end;
  152. tcallparanodeclass = class of tcallparanode;
  153. function reverseparameters(p: tcallparanode): tcallparanode;
  154. var
  155. ccallnode : tcallnodeclass;
  156. ccallparanode : tcallparanodeclass;
  157. { Current callnode, this is needed for having a link
  158. between the callparanodes and the callnode they belong to }
  159. aktcallnode : tcallnode;
  160. implementation
  161. uses
  162. systems,
  163. verbose,globals,
  164. symconst,defutil,defcmp,
  165. htypechk,pass_1,
  166. ncnv,nld,ninl,nadd,ncon,nmem,
  167. procinfo,
  168. cgbase
  169. ;
  170. type
  171. tobjectinfoitem = class(tlinkedlistitem)
  172. objinfo : tobjectdef;
  173. constructor create(def : tobjectdef);
  174. end;
  175. {****************************************************************************
  176. HELPERS
  177. ****************************************************************************}
  178. function reverseparameters(p: tcallparanode): tcallparanode;
  179. var
  180. hp1, hp2: tcallparanode;
  181. begin
  182. hp1:=nil;
  183. while assigned(p) do
  184. begin
  185. { pull out }
  186. hp2:=p;
  187. p:=tcallparanode(p.right);
  188. { pull in }
  189. hp2.right:=hp1;
  190. hp1:=hp2;
  191. end;
  192. reverseparameters:=hp1;
  193. end;
  194. function gen_high_tree(p:tnode;paradef:tdef):tnode;
  195. var
  196. temp: tnode;
  197. len : integer;
  198. loadconst : boolean;
  199. hightree : tnode;
  200. begin
  201. len:=-1;
  202. loadconst:=true;
  203. hightree:=nil;
  204. case p.resulttype.def.deftype of
  205. arraydef :
  206. begin
  207. if (paradef.deftype<>arraydef) then
  208. internalerror(200405241);
  209. { handle special case of passing an single array to an array of array }
  210. if compare_defs(tarraydef(paradef).elementtype.def,p.resulttype.def,nothingn)>=te_equal then
  211. len:=0
  212. else
  213. begin
  214. { handle via a normal inline in_high_x node }
  215. loadconst := false;
  216. hightree := geninlinenode(in_high_x,false,p.getcopy);
  217. resulttypepass(hightree);
  218. { only substract low(array) if it's <> 0 }
  219. temp := geninlinenode(in_low_x,false,p.getcopy);
  220. resulttypepass(temp);
  221. if (temp.nodetype <> ordconstn) or
  222. (tordconstnode(temp).value <> 0) then
  223. hightree := caddnode.create(subn,hightree,temp)
  224. else
  225. temp.free;
  226. end;
  227. end;
  228. stringdef :
  229. begin
  230. if is_open_string(paradef) then
  231. begin
  232. { handle via a normal inline in_high_x node }
  233. loadconst := false;
  234. hightree := geninlinenode(in_high_x,false,p.getcopy);
  235. end
  236. else
  237. begin
  238. { passing a string to an array of char }
  239. if (p.nodetype=stringconstn) then
  240. begin
  241. len:=str_length(p);
  242. if len>0 then
  243. dec(len);
  244. end
  245. else
  246. begin
  247. hightree:=caddnode.create(subn,geninlinenode(in_length_x,false,p.getcopy),
  248. cordconstnode.create(1,s32inttype,false));
  249. loadconst:=false;
  250. end;
  251. end;
  252. end;
  253. else
  254. len:=0;
  255. end;
  256. if loadconst then
  257. hightree:=cordconstnode.create(len,s32inttype,true)
  258. else
  259. begin
  260. if not assigned(hightree) then
  261. internalerror(200304071);
  262. { Need to use explicit, because it can also be a enum }
  263. hightree:=ctypeconvnode.create_explicit(hightree,s32inttype);
  264. end;
  265. result:=hightree;
  266. end;
  267. {****************************************************************************
  268. TOBJECTINFOITEM
  269. ****************************************************************************}
  270. constructor tobjectinfoitem.create(def : tobjectdef);
  271. begin
  272. inherited create;
  273. objinfo := def;
  274. end;
  275. {****************************************************************************
  276. TCALLPARANODE
  277. ****************************************************************************}
  278. constructor tcallparanode.create(expr,next : tnode);
  279. begin
  280. inherited create(callparan,expr,next);
  281. if not assigned(expr) then
  282. internalerror(200305091);
  283. expr.set_file_line(self);
  284. callparaflags:=[];
  285. end;
  286. destructor tcallparanode.destroy;
  287. begin
  288. { When the node is used by callnode then
  289. we don't destroy left, the callnode takes care of it }
  290. if used_by_callnode then
  291. left:=nil;
  292. inherited destroy;
  293. end;
  294. constructor tcallparanode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  295. begin
  296. inherited ppuload(t,ppufile);
  297. ppufile.getsmallset(callparaflags);
  298. end;
  299. procedure tcallparanode.ppuwrite(ppufile:tcompilerppufile);
  300. begin
  301. inherited ppuwrite(ppufile);
  302. ppufile.putsmallset(callparaflags);
  303. end;
  304. function tcallparanode.getcopy : tnode;
  305. var
  306. n : tcallparanode;
  307. begin
  308. n:=tcallparanode(inherited getcopy);
  309. n.callparaflags:=callparaflags;
  310. n.paraitem:=paraitem;
  311. result:=n;
  312. end;
  313. procedure tcallparanode.insertintolist(l : tnodelist);
  314. begin
  315. end;
  316. procedure tcallparanode.get_paratype;
  317. var
  318. old_array_constructor : boolean;
  319. begin
  320. inc(parsing_para_level);
  321. if assigned(right) then
  322. tcallparanode(right).get_paratype;
  323. old_array_constructor:=allow_array_constructor;
  324. allow_array_constructor:=true;
  325. resulttypepass(left);
  326. allow_array_constructor:=old_array_constructor;
  327. if codegenerror then
  328. resulttype:=generrortype
  329. else
  330. resulttype:=left.resulttype;
  331. dec(parsing_para_level);
  332. end;
  333. procedure tcallparanode.insert_typeconv(do_count : boolean);
  334. var
  335. oldtype : ttype;
  336. {$ifdef extdebug}
  337. store_count_ref : boolean;
  338. {$endif def extdebug}
  339. begin
  340. inc(parsing_para_level);
  341. {$ifdef extdebug}
  342. if do_count then
  343. begin
  344. store_count_ref:=count_ref;
  345. count_ref:=true;
  346. end;
  347. {$endif def extdebug}
  348. { Be sure to have the resulttype }
  349. if not assigned(left.resulttype.def) then
  350. resulttypepass(left);
  351. if (left.nodetype<>nothingn) then
  352. begin
  353. { Convert tp procvars, this is needs to be done
  354. here to make the change permanent. in the overload
  355. choosing the changes are only made temporary }
  356. if (left.resulttype.def.deftype=procvardef) and
  357. (paraitem.paratype.def.deftype<>procvardef) then
  358. begin
  359. if maybe_call_procvar(left,true) then
  360. resulttype:=left.resulttype;
  361. end;
  362. { Handle varargs and hidden paras directly, no typeconvs or }
  363. { typechecking needed }
  364. if (cpf_varargs_para in callparaflags) then
  365. begin
  366. { convert pascal to C types }
  367. case left.resulttype.def.deftype of
  368. stringdef :
  369. inserttypeconv(left,charpointertype);
  370. floatdef :
  371. inserttypeconv(left,s64floattype);
  372. end;
  373. set_varstate(left,vs_used,true);
  374. resulttype:=left.resulttype;
  375. { also update paraitem type to get the correct parameter location
  376. for the new types }
  377. paraitem.paratype:=left.resulttype;
  378. end
  379. else
  380. if (paraitem.is_hidden) then
  381. begin
  382. set_varstate(left,vs_used,true);
  383. resulttype:=left.resulttype;
  384. end
  385. else
  386. begin
  387. { Do we need arrayconstructor -> set conversion, then insert
  388. it here before the arrayconstructor node breaks the tree
  389. with its conversions of enum->ord }
  390. if (left.nodetype=arrayconstructorn) and
  391. (paraitem.paratype.def.deftype=setdef) then
  392. inserttypeconv(left,paraitem.paratype);
  393. { set some settings needed for arrayconstructor }
  394. if is_array_constructor(left.resulttype.def) then
  395. begin
  396. if is_array_of_const(paraitem.paratype.def) then
  397. begin
  398. { force variant array }
  399. include(left.flags,nf_forcevaria);
  400. end
  401. else
  402. begin
  403. include(left.flags,nf_novariaallowed);
  404. { now that the resultting type is know we can insert the required
  405. typeconvs for the array constructor }
  406. if paraitem.paratype.def.deftype=arraydef then
  407. tarrayconstructornode(left).force_type(tarraydef(paraitem.paratype.def).elementtype);
  408. end;
  409. end;
  410. { check if local proc/func is assigned to procvar }
  411. if left.resulttype.def.deftype=procvardef then
  412. test_local_to_procvar(tprocvardef(left.resulttype.def),paraitem.paratype.def);
  413. { test conversions }
  414. if not(is_shortstring(left.resulttype.def) and
  415. is_shortstring(paraitem.paratype.def)) and
  416. (paraitem.paratype.def.deftype<>formaldef) then
  417. begin
  418. { Process open parameters }
  419. if paramanager.push_high_param(paraitem.paratyp,paraitem.paratype.def,aktcallnode.procdefinition.proccalloption) then
  420. begin
  421. { insert type conv but hold the ranges of the array }
  422. oldtype:=left.resulttype;
  423. inserttypeconv(left,paraitem.paratype);
  424. left.resulttype:=oldtype;
  425. end
  426. else
  427. begin
  428. { for ordinals, floats and enums, verify if we might cause
  429. some range-check errors. }
  430. if (paraitem.paratype.def.deftype in [enumdef,orddef,floatdef]) and
  431. (left.resulttype.def.deftype in [enumdef,orddef,floatdef]) and
  432. (left.nodetype in [vecn,loadn,calln]) then
  433. begin
  434. if (left.resulttype.def.size>paraitem.paratype.def.size) then
  435. begin
  436. if (cs_check_range in aktlocalswitches) then
  437. Message(type_w_smaller_possible_range_check)
  438. else
  439. Message(type_h_smaller_possible_range_check);
  440. end;
  441. end;
  442. inserttypeconv(left,paraitem.paratype);
  443. end;
  444. if codegenerror then
  445. begin
  446. dec(parsing_para_level);
  447. exit;
  448. end;
  449. end;
  450. { check var strings }
  451. if (cs_strict_var_strings in aktlocalswitches) and
  452. is_shortstring(left.resulttype.def) and
  453. is_shortstring(paraitem.paratype.def) and
  454. (paraitem.paratyp in [vs_out,vs_var]) and
  455. not(is_open_string(paraitem.paratype.def)) and
  456. not(equal_defs(left.resulttype.def,paraitem.paratype.def)) then
  457. begin
  458. aktfilepos:=left.fileinfo;
  459. CGMessage(type_e_strict_var_string_violation);
  460. end;
  461. { Handle formal parameters separate }
  462. if (paraitem.paratype.def.deftype=formaldef) then
  463. begin
  464. { load procvar if a procedure is passed }
  465. if (m_tp_procvar in aktmodeswitches) and
  466. (left.nodetype=calln) and
  467. (is_void(left.resulttype.def)) then
  468. load_procvar_from_calln(left);
  469. case paraitem.paratyp of
  470. vs_var,
  471. vs_out :
  472. begin
  473. if not valid_for_formal_var(left) then
  474. CGMessagePos(left.fileinfo,parser_e_illegal_parameter_list);
  475. end;
  476. vs_const :
  477. begin
  478. if not valid_for_formal_const(left) then
  479. CGMessagePos(left.fileinfo,parser_e_illegal_parameter_list);
  480. end;
  481. end;
  482. end
  483. else
  484. begin
  485. { check if the argument is allowed }
  486. if (paraitem.paratyp in [vs_out,vs_var]) then
  487. valid_for_var(left);
  488. end;
  489. if paraitem.paratyp in [vs_var,vs_const] then
  490. begin
  491. { Causes problems with const ansistrings if also }
  492. { done for vs_const (JM) }
  493. if paraitem.paratyp = vs_var then
  494. set_unique(left);
  495. make_not_regable(left);
  496. end;
  497. { ansistrings out paramaters doesn't need to be }
  498. { unique, they are finalized }
  499. if paraitem.paratyp=vs_out then
  500. make_not_regable(left);
  501. if do_count then
  502. begin
  503. if paraitem.paratyp in [vs_var,vs_out] then
  504. set_varstate(left,vs_used,false)
  505. else
  506. set_varstate(left,vs_used,true);
  507. end;
  508. { must only be done after typeconv PM }
  509. resulttype:=paraitem.paratype;
  510. end;
  511. end;
  512. { process next node }
  513. if assigned(right) then
  514. tcallparanode(right).insert_typeconv(do_count);
  515. dec(parsing_para_level);
  516. {$ifdef extdebug}
  517. if do_count then
  518. count_ref:=store_count_ref;
  519. {$endif def extdebug}
  520. end;
  521. procedure tcallparanode.det_registers;
  522. begin
  523. if assigned(right) then
  524. begin
  525. tcallparanode(right).det_registers;
  526. registersint:=right.registersint;
  527. registersfpu:=right.registersfpu;
  528. {$ifdef SUPPORT_MMX}
  529. registersmmx:=right.registersmmx;
  530. {$endif}
  531. end;
  532. firstpass(left);
  533. if left.registersint>registersint then
  534. registersint:=left.registersint;
  535. if left.registersfpu>registersfpu then
  536. registersfpu:=left.registersfpu;
  537. {$ifdef SUPPORT_MMX}
  538. if left.registersmmx>registersmmx then
  539. registersmmx:=left.registersmmx;
  540. {$endif SUPPORT_MMX}
  541. end;
  542. procedure tcallparanode.firstcallparan;
  543. begin
  544. if not assigned(left.resulttype.def) then
  545. get_paratype;
  546. det_registers;
  547. end;
  548. function tcallparanode.docompare(p: tnode): boolean;
  549. begin
  550. docompare :=
  551. inherited docompare(p) and
  552. (callparaflags = tcallparanode(p).callparaflags)
  553. ;
  554. end;
  555. procedure tcallparanode.printnodetree(var t:text);
  556. begin
  557. printnodelist(t);
  558. end;
  559. {****************************************************************************
  560. TCALLNODE
  561. ****************************************************************************}
  562. constructor tcallnode.create(l:tnode;v : tprocsym;st : tsymtable; mp: tnode; callflags:tcallnodeflags);
  563. begin
  564. inherited create(calln,l,nil);
  565. symtableprocentry:=v;
  566. symtableproc:=st;
  567. callnodeflags:=callflags+[cnf_return_value_used];
  568. methodpointer:=mp;
  569. methodpointerinit:=nil;
  570. methodpointerdone:=nil;
  571. procdefinition:=nil;
  572. _funcretnode:=nil;
  573. inlinecode:=nil;
  574. paralength:=-1;
  575. varargsparas:=nil;
  576. end;
  577. constructor tcallnode.create_procvar(l,r:tnode);
  578. begin
  579. inherited create(calln,l,r);
  580. symtableprocentry:=nil;
  581. symtableproc:=nil;
  582. methodpointer:=nil;
  583. methodpointerinit:=nil;
  584. methodpointerdone:=nil;
  585. procdefinition:=nil;
  586. callnodeflags:=[cnf_return_value_used];
  587. _funcretnode:=nil;
  588. inlinecode:=nil;
  589. paralength:=-1;
  590. varargsparas:=nil;
  591. end;
  592. constructor tcallnode.createintern(const name: string; params: tnode);
  593. var
  594. srsym: tsym;
  595. symowner: tsymtable;
  596. begin
  597. if not (cs_compilesystem in aktmoduleswitches) then
  598. begin
  599. srsym := searchsymonlyin(systemunit,name);
  600. symowner := systemunit;
  601. end
  602. else
  603. begin
  604. searchsym(name,srsym,symowner);
  605. if not assigned(srsym) then
  606. searchsym(upper(name),srsym,symowner);
  607. end;
  608. if not assigned(srsym) or
  609. (srsym.typ <> procsym) then
  610. begin
  611. {$ifdef EXTDEBUG}
  612. Comment(V_Error,'unknown compilerproc '+name);
  613. {$endif EXTDEBUG}
  614. internalerror(200107271);
  615. end;
  616. self.create(params,tprocsym(srsym),symowner,nil,[]);
  617. end;
  618. constructor tcallnode.createinternres(const name: string; params: tnode; const res: ttype);
  619. begin
  620. self.createintern(name,params);
  621. restype := res;
  622. include(callnodeflags,cnf_restypeset);
  623. { both the normal and specified resulttype either have to be returned via a }
  624. { parameter or not, but no mixing (JM) }
  625. if paramanager.ret_in_param(restype.def,pocall_compilerproc) xor
  626. paramanager.ret_in_param(symtableprocentry.first_procdef.rettype.def,symtableprocentry.first_procdef.proccalloption) then
  627. internalerror(200108291);
  628. end;
  629. constructor tcallnode.createinternreturn(const name: string; params: tnode; returnnode : tnode);
  630. begin
  631. self.createintern(name,params);
  632. _funcretnode:=returnnode;
  633. if not paramanager.ret_in_param(symtableprocentry.first_procdef.rettype.def,symtableprocentry.first_procdef.proccalloption) then
  634. internalerror(200204247);
  635. end;
  636. procedure tcallnode.setfuncretnode(const returnnode: tnode);
  637. var
  638. para: tcallparanode;
  639. begin
  640. if assigned(_funcretnode) then
  641. _funcretnode.free;
  642. _funcretnode := returnnode;
  643. { if the resulttype pass hasn't occurred yet, that one will do }
  644. { everything }
  645. if assigned(resulttype.def) then
  646. begin
  647. { these are returned as values, but we can optimize their loading }
  648. { as well }
  649. if is_ansistring(resulttype.def) or
  650. is_widestring(resulttype.def) then
  651. exit;
  652. para := tcallparanode(left);
  653. while assigned(para) do
  654. begin
  655. if para.paraitem.is_hidden and
  656. (vo_is_funcret in tvarsym(para.paraitem.parasym).varoptions) then
  657. begin
  658. para.left.free;
  659. para.left := _funcretnode.getcopy;
  660. exit;
  661. end;
  662. para := tcallparanode(para.right);
  663. end;
  664. { no hidden resultpara found, error! }
  665. if not(procdefinition.proccalloption = pocall_inline) then
  666. internalerror(200306087);
  667. end;
  668. end;
  669. destructor tcallnode.destroy;
  670. begin
  671. methodpointer.free;
  672. methodpointerinit.free;
  673. methodpointerdone.free;
  674. _funcretnode.free;
  675. inlinecode.free;
  676. if assigned(varargsparas) then
  677. varargsparas.free;
  678. inherited destroy;
  679. end;
  680. constructor tcallnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  681. begin
  682. inherited ppuload(t,ppufile);
  683. ppufile.getderef(symtableprocentryderef);
  684. {$ifdef fpc}
  685. {$warning FIXME: No withsymtable support}
  686. {$endif}
  687. symtableproc:=nil;
  688. ppufile.getderef(procdefinitionderef);
  689. ppufile.getsmallset(callnodeflags);
  690. methodpointer:=ppuloadnode(ppufile);
  691. methodpointerinit:=ppuloadnode(ppufile);
  692. methodpointerdone:=ppuloadnode(ppufile);
  693. _funcretnode:=ppuloadnode(ppufile);
  694. inlinecode:=ppuloadnode(ppufile);
  695. end;
  696. procedure tcallnode.ppuwrite(ppufile:tcompilerppufile);
  697. begin
  698. inherited ppuwrite(ppufile);
  699. ppufile.putderef(symtableprocentryderef);
  700. ppufile.putderef(procdefinitionderef);
  701. ppufile.putsmallset(callnodeflags);
  702. ppuwritenode(ppufile,methodpointer);
  703. ppuwritenode(ppufile,methodpointerinit);
  704. ppuwritenode(ppufile,methodpointerdone);
  705. ppuwritenode(ppufile,_funcretnode);
  706. ppuwritenode(ppufile,inlinecode);
  707. end;
  708. procedure tcallnode.buildderefimpl;
  709. begin
  710. inherited buildderefimpl;
  711. symtableprocentryderef.build(symtableprocentry);
  712. procdefinitionderef.build(procdefinition);
  713. if assigned(methodpointer) then
  714. methodpointer.buildderefimpl;
  715. if assigned(methodpointerinit) then
  716. methodpointerinit.buildderefimpl;
  717. if assigned(methodpointerdone) then
  718. methodpointerdone.buildderefimpl;
  719. if assigned(_funcretnode) then
  720. _funcretnode.buildderefimpl;
  721. if assigned(inlinecode) then
  722. inlinecode.buildderefimpl;
  723. end;
  724. procedure tcallnode.derefimpl;
  725. var
  726. pt : tcallparanode;
  727. currpara : tparaitem;
  728. begin
  729. inherited derefimpl;
  730. symtableprocentry:=tprocsym(symtableprocentryderef.resolve);
  731. symtableproc:=symtableprocentry.owner;
  732. procdefinition:=tprocdef(procdefinitionderef.resolve);
  733. if assigned(methodpointer) then
  734. methodpointer.derefimpl;
  735. if assigned(methodpointerinit) then
  736. methodpointerinit.derefimpl;
  737. if assigned(methodpointerdone) then
  738. methodpointerdone.derefimpl;
  739. if assigned(_funcretnode) then
  740. _funcretnode.derefimpl;
  741. if assigned(inlinecode) then
  742. inlinecode.derefimpl;
  743. { Connect paraitems }
  744. pt:=tcallparanode(left);
  745. while assigned(pt) and
  746. (cpf_varargs_para in pt.callparaflags) do
  747. pt:=tcallparanode(pt.right);
  748. currpara:=tparaitem(procdefinition.Para.last);
  749. while assigned(currpara) do
  750. begin
  751. if not assigned(pt) then
  752. internalerror(200311077);
  753. pt.paraitem:=currpara;
  754. pt:=tcallparanode(pt.right);
  755. currpara:=tparaitem(currpara.previous);
  756. end;
  757. if assigned(currpara) or assigned(pt) then
  758. internalerror(200311078);
  759. end;
  760. function tcallnode.getcopy : tnode;
  761. var
  762. n : tcallnode;
  763. hp : tparaitem;
  764. oldleft : tnode;
  765. begin
  766. { Need to use a hack here to prevent the parameters from being copied.
  767. The parameters must be copied between methodpointerinit/methodpointerdone because
  768. the can reference methodpointer }
  769. oldleft:=left;
  770. left:=nil;
  771. n:=tcallnode(inherited getcopy);
  772. left:=oldleft;
  773. n.symtableprocentry:=symtableprocentry;
  774. n.symtableproc:=symtableproc;
  775. n.procdefinition:=procdefinition;
  776. n.restype := restype;
  777. n.callnodeflags := callnodeflags;
  778. if assigned(methodpointerinit) then
  779. n.methodpointerinit:=methodpointerinit.getcopy
  780. else
  781. n.methodpointerinit:=nil;
  782. { methodpointerinit is copied, now references to the temp will also be copied
  783. correctly. We can now copy the parameters and methodpointer }
  784. if assigned(left) then
  785. n.left:=left.getcopy
  786. else
  787. n.left:=nil;
  788. if assigned(methodpointer) then
  789. n.methodpointer:=methodpointer.getcopy
  790. else
  791. n.methodpointer:=nil;
  792. if assigned(methodpointerdone) then
  793. n.methodpointerdone:=methodpointerdone.getcopy
  794. else
  795. n.methodpointerdone:=nil;
  796. if assigned(_funcretnode) then
  797. n._funcretnode:=_funcretnode.getcopy
  798. else
  799. n._funcretnode:=nil;
  800. if assigned(inlinecode) then
  801. n.inlinecode:=inlinecode.getcopy
  802. else
  803. n.inlinecode:=nil;
  804. if assigned(varargsparas) then
  805. begin
  806. n.varargsparas:=tvarargspara.create;
  807. hp:=tparaitem(varargsparas.first);
  808. while assigned(hp) do
  809. begin
  810. n.varargsparas.concat(hp.getcopy);
  811. hp:=tparaitem(hp.next);
  812. end;
  813. end
  814. else
  815. n.varargsparas:=nil;
  816. result:=n;
  817. end;
  818. procedure tcallnode.insertintolist(l : tnodelist);
  819. begin
  820. end;
  821. procedure tcallnode.convert_carg_array_of_const;
  822. var
  823. hp : tarrayconstructornode;
  824. oldleft : tcallparanode;
  825. begin
  826. oldleft:=tcallparanode(left);
  827. if oldleft.left.nodetype<>arrayconstructorn then
  828. begin
  829. CGMessage1(type_e_wrong_type_in_array_constructor,oldleft.left.resulttype.def.typename);
  830. exit;
  831. end;
  832. { Get arrayconstructor node and insert typeconvs }
  833. hp:=tarrayconstructornode(oldleft.left);
  834. hp.insert_typeconvs;
  835. { Add c args parameters }
  836. { It could be an empty set }
  837. if assigned(hp) and
  838. assigned(hp.left) then
  839. begin
  840. while assigned(hp) do
  841. begin
  842. left:=ccallparanode.create(hp.left,left);
  843. { set callparanode resulttype and flags }
  844. left.resulttype:=hp.left.resulttype;
  845. include(tcallparanode(left).callparaflags,cpf_varargs_para);
  846. hp.left:=nil;
  847. hp:=tarrayconstructornode(hp.right);
  848. end;
  849. end;
  850. { Remove value of old array of const parameter, but keep it
  851. in the list because it is required for bind_paraitem.
  852. Generate a nothign to keep callparanoed.left valid }
  853. oldleft.left.free;
  854. oldleft.left:=cnothingnode.create;
  855. end;
  856. procedure tcallnode.verifyabstract(p : tnamedindexitem;arg:pointer);
  857. var
  858. hp : tprocdef;
  859. j: integer;
  860. begin
  861. if (tsym(p).typ=procsym) then
  862. begin
  863. for j:=1 to tprocsym(p).procdef_count do
  864. begin
  865. { index starts at 1 }
  866. hp:=tprocsym(p).procdef[j];
  867. { If this is an abstract method insert into the list }
  868. if (po_abstractmethod in hp.procoptions) then
  869. AbstractMethodsList.Insert(hp.procsym.realname)
  870. else
  871. { If this symbol is an overriding method, then remove it from the list }
  872. if po_overridingmethod in hp.procoptions then
  873. AbstractMethodsList.Remove(hp.procsym.realname);
  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.objrealname^);
  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. For the moment we only look at the first parameter field. Combining it
  1626. with multiple parameter fields will make things a lot complexer (PFV) }
  1627. currloc:=hpcurr.paraitem.paraloc[callerside].location^.loc;
  1628. hpprev:=nil;
  1629. hp:=hpfirst;
  1630. while assigned(hp) do
  1631. begin
  1632. case currloc of
  1633. LOC_REFERENCE :
  1634. begin
  1635. case hp.paraitem.paraloc[callerside].location^.loc of
  1636. LOC_REFERENCE :
  1637. begin
  1638. { Offset is calculated like:
  1639. sub esp,12
  1640. mov [esp+8],para3
  1641. mov [esp+4],para2
  1642. mov [esp],para1
  1643. call function
  1644. That means the for pushes the para with the
  1645. highest offset (see para3) needs to be pushed first
  1646. }
  1647. if (hpcurr.registersint>hp.registersint)
  1648. {$ifdef x86}
  1649. or (hpcurr.paraitem.paraloc[callerside].location^.reference.offset>hp.paraitem.paraloc[callerside].location^.reference.offset)
  1650. {$endif x86}
  1651. then
  1652. break;
  1653. end;
  1654. LOC_REGISTER,
  1655. LOC_FPUREGISTER :
  1656. break;
  1657. end;
  1658. end;
  1659. LOC_FPUREGISTER,
  1660. LOC_REGISTER :
  1661. begin
  1662. if (hp.paraitem.paraloc[callerside].location^.loc=currloc) and
  1663. (hpcurr.registersint>hp.registersint) then
  1664. break;
  1665. end;
  1666. end;
  1667. hpprev:=hp;
  1668. hp:=tcallparanode(hp.right);
  1669. end;
  1670. hpcurr.right:=hp;
  1671. if assigned(hpprev) then
  1672. hpprev.right:=hpcurr
  1673. else
  1674. hpfirst:=hpcurr;
  1675. { next }
  1676. hpcurr:=hpnext;
  1677. end;
  1678. left:=hpfirst;
  1679. end;
  1680. function tcallnode.replaceparaload(var n: tnode; arg: pointer): foreachnoderesult;
  1681. var
  1682. paras: tcallparanode;
  1683. temp: tnode;
  1684. begin
  1685. result := fen_false;
  1686. n.fileinfo := pfileposinfo(arg)^;
  1687. if (n.nodetype = loadn) then
  1688. begin
  1689. paras := tcallparanode(left);
  1690. while assigned(paras) and
  1691. (paras.paraitem.parasym <> tloadnode(n).symtableentry) do
  1692. paras := tcallparanode(paras.right);
  1693. if assigned(paras) then
  1694. begin
  1695. n.free;
  1696. n := paras.left.getcopy;
  1697. resulttypepass(n);
  1698. result := fen_true;
  1699. end
  1700. {$ifndef VER1_0}
  1701. else
  1702. begin
  1703. { local? }
  1704. if (tloadnode(n).symtableentry.typ <> varsym) or
  1705. (tloadnode(n).symtableentry.owner <> tprocdef(procdefinition).localst) then
  1706. exit;
  1707. if (tloadnode(n).symtableentry.indexnr > high(inlinelocals)) or
  1708. not assigned(inlinelocals[tloadnode(n).symtableentry.indexnr]) then
  1709. internalerror(20040720);
  1710. temp := inlinelocals[tloadnode(n).symtableentry.indexnr].getcopy;
  1711. n.free;
  1712. n := temp;
  1713. resulttypepass(n);
  1714. result := fen_true;
  1715. end;
  1716. {$endif ndef VER1_0}
  1717. end;
  1718. end;
  1719. {$ifndef VER1_0}
  1720. type
  1721. ptempnodes = ^ttempnodes;
  1722. ttempnodes = record
  1723. createstatement, deletestatement: tstatementnode;
  1724. end;
  1725. procedure tcallnode.createlocaltemps(p:TNamedIndexItem;arg:pointer);
  1726. var
  1727. tempinfo: ptempnodes absolute ptempnodes(arg);
  1728. tempnode: ttempcreatenode;
  1729. begin
  1730. if (tsymentry(p).typ <> varsym) then
  1731. exit;
  1732. if (p.indexnr > high(inlinelocals)) then
  1733. setlength(inlinelocals,p.indexnr+10);
  1734. {$ifndef VER1_0}
  1735. if (vo_is_funcret in tvarsym(p).varoptions) and
  1736. assigned(funcretnode) then
  1737. begin
  1738. if node_complexity(funcretnode) > 1 then
  1739. begin
  1740. { can this happen? }
  1741. { we may have to replace the funcretnode with the address of funcretnode }
  1742. { loaded in a temp in this case, because the expression may e.g. contain }
  1743. { a global variable that gets changed inside the function }
  1744. internalerror(2004072101);
  1745. end;
  1746. inlinelocals[tvarsym(p).indexnr] := funcretnode.getcopy
  1747. end
  1748. else
  1749. {$endif ndef VER1_0}
  1750. begin
  1751. if (cs_regvars in aktglobalswitches) and
  1752. (([vo_regable{$ifndef x86},vo_fpuregable{$endif}] * tvarsym(p).varoptions) <> []) and
  1753. (not tvarsym(p).vartype.def.needs_inittable) then
  1754. tempnode := ctempcreatenode.create_reg(tvarsym(p).vartype,tvarsym(p).vartype.def.size,tt_persistent)
  1755. else
  1756. tempnode := ctempcreatenode.create(tvarsym(p).vartype,tvarsym(p).vartype.def.size,tt_persistent);
  1757. addstatement(tempinfo^.createstatement,tempnode);
  1758. if assigned(tvarsym(p).defaultconstsym) then
  1759. begin
  1760. { warning: duplicate from psub.pas:initializevars() -> must refactor }
  1761. addstatement(tempinfo^.createstatement,cassignmentnode.create(
  1762. ctemprefnode.create(tempnode),
  1763. cloadnode.create(tvarsym(p).defaultconstsym,tvarsym(p).defaultconstsym.owner)));
  1764. end;
  1765. if (vo_is_funcret in tvarsym(p).varoptions) then
  1766. begin
  1767. funcretnode := ctemprefnode.create(tempnode);
  1768. addstatement(tempinfo^.deletestatement,ctempdeletenode.create_normal_temp(tempnode));
  1769. end
  1770. else
  1771. addstatement(tempinfo^.deletestatement,ctempdeletenode.create(tempnode));
  1772. inlinelocals[p.indexnr] := ctemprefnode.create(tempnode);
  1773. end;
  1774. end;
  1775. {$endif ndef VER1_0}
  1776. procedure tcallnode.createinlineparas(var createstatement, deletestatement: tstatementnode);
  1777. var
  1778. para: tcallparanode;
  1779. tempnode: ttempcreatenode;
  1780. hp: tnode;
  1781. {$ifndef VER1_0}
  1782. tempnodes: ttempnodes;
  1783. {$endif ndef VER1_0}
  1784. begin
  1785. { parameters }
  1786. para := tcallparanode(left);
  1787. while assigned(para) do
  1788. begin
  1789. if (para.paraitem.parasym.typ = varsym) and
  1790. { para.left will already be the same as funcretnode in the following case, so don't change }
  1791. (not(vo_is_funcret in tvarsym(para.paraitem.parasym).varoptions) or
  1792. (not assigned(funcretnode))) then
  1793. begin
  1794. { create temps for value parameters, function result and also for }
  1795. { const parameters which are passed by value instead of by reference }
  1796. if (vo_is_funcret in tvarsym(para.paraitem.parasym).varoptions) or
  1797. (para.paraitem.paratyp = vs_value) or
  1798. ((para.paraitem.paratyp = vs_const) and
  1799. (not paramanager.push_addr_param(vs_const,para.left.resulttype.def,procdefinition.proccalloption) or
  1800. { the problem is that we can't take the address of a function result :( }
  1801. (node_complexity(para.left) >= NODE_COMPLEXITY_INF))) then
  1802. begin
  1803. if (cs_regvars in aktglobalswitches) and
  1804. (([vo_regable,vo_fpuregable] * tvarsym(para.paraitem.parasym).varoptions) <> []) and
  1805. (not tvarsym(para.paraitem.parasym).vartype.def.needs_inittable) then
  1806. tempnode := ctempcreatenode.create_reg(para.left.resulttype,para.left.resulttype.def.size,tt_persistent)
  1807. else
  1808. tempnode := ctempcreatenode.create(para.left.resulttype,para.left.resulttype.def.size,tt_persistent);
  1809. addstatement(createstatement,tempnode);
  1810. { assign the value of the parameter to the temp, except in case of the function result }
  1811. { (in that case, para.left is a block containing the creation of a new temp, while we }
  1812. { only need a temprefnode, so delete the old stuff) }
  1813. if not(vo_is_funcret in tvarsym(para.paraitem.parasym).varoptions) then
  1814. begin
  1815. addstatement(createstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  1816. para.left));
  1817. para.left := ctemprefnode.create(tempnode);
  1818. addstatement(deletestatement,ctempdeletenode.create(tempnode));
  1819. end
  1820. else
  1821. begin
  1822. if not(assigned(funcretnode)) then
  1823. funcretnode := ctemprefnode.create(tempnode);
  1824. para.left.free;
  1825. para.left := ctemprefnode.create(tempnode);
  1826. addstatement(deletestatement,ctempdeletenode.create_normal_temp(tempnode));
  1827. end
  1828. end
  1829. else if node_complexity(para.left) > 1 then
  1830. begin
  1831. if (cs_regvars in aktglobalswitches) and
  1832. not tvarsym(para.paraitem.parasym).vartype.def.needs_inittable then
  1833. tempnode := ctempcreatenode.create_reg(voidpointertype,voidpointertype.def.size,tt_persistent)
  1834. else
  1835. tempnode := ctempcreatenode.create(voidpointertype,voidpointertype.def.size,tt_persistent);
  1836. addstatement(createstatement,tempnode);
  1837. addstatement(createstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  1838. caddrnode.create(para.left)));
  1839. hp := cderefnode.create(ctemprefnode.create(tempnode));
  1840. inserttypeconv_explicit(hp,para.left.resulttype);
  1841. para.left := hp;
  1842. addstatement(deletestatement,ctempdeletenode.create(tempnode));
  1843. end;
  1844. para := tcallparanode(para.right);
  1845. end;
  1846. end;
  1847. {$ifndef VER1_0}
  1848. { local variables }
  1849. if not assigned(tprocdef(procdefinition).localst) or
  1850. (tprocdef(procdefinition).localst.symindex.count = 0) then
  1851. exit;
  1852. tempnodes.createstatement := createstatement;
  1853. tempnodes.deletestatement := deletestatement;
  1854. setlength(inlinelocals,tprocdef(procdefinition).localst.symindex.count);
  1855. tprocdef(procdefinition).localst.foreach({$ifdef FPCPROCVAR}@{$endif}createlocaltemps,@tempnodes);
  1856. createstatement := tempnodes.createstatement;
  1857. deletestatement := tempnodes.deletestatement;
  1858. {$endif ndef VER1_0}
  1859. end;
  1860. function tcallnode.pass_1 : tnode;
  1861. var
  1862. createstatement,deletestatement: tstatementnode;
  1863. createblock,deleteblock: tblocknode;
  1864. i: longint;
  1865. label
  1866. errorexit;
  1867. begin
  1868. result:=nil;
  1869. {!!!!!!!!
  1870. if (procdefinition.proccalloption=pocall_inline) and
  1871. { can we inline this procedure at the node level? }
  1872. (tprocdef(procdefinition).inlininginfo^.inlinenode) then
  1873. begin
  1874. { inherit flags }
  1875. current_procinfo.flags := current_procinfo.flags + ((procdefinition as tprocdef).inlininginfo^.flags*inherited_inlining_flags);
  1876. if assigned(methodpointer) then
  1877. CGMessage(cg_e_unable_inline_object_methods);
  1878. if assigned(right) then
  1879. CGMessage(cg_e_unable_inline_procvar);
  1880. if assigned(inlinecode) then
  1881. internalerror(2004071110);
  1882. if assigned(tprocdef(procdefinition).inlininginfo^.code) then
  1883. result:=tprocdef(procdefinition).inlininginfo^.code.getcopy
  1884. else
  1885. CGMessage(cg_e_no_code_for_inline_stored);
  1886. if assigned(result) then
  1887. begin
  1888. createblock := internalstatements(createstatement);
  1889. deleteblock := internalstatements(deletestatement);
  1890. { replace complex parameters with temps }
  1891. createinlineparas(createstatement,deletestatement);
  1892. { replace the parameter loads with the parameter values }
  1893. foreachnode(result,{$ifdef FPCPROCVAR}@{$endif}replaceparaload,@fileinfo);
  1894. { free the temps for the locals }
  1895. for i := 0 to high(inlinelocals) do
  1896. if assigned(inlinelocals[i]) then
  1897. inlinelocals[i].free;
  1898. setlength(inlinelocals,0);
  1899. addstatement(createstatement,result);
  1900. addstatement(createstatement,deleteblock);
  1901. { set function result location if necessary }
  1902. if assigned(funcretnode) and
  1903. (cnf_return_value_used in callnodeflags) then
  1904. addstatement(createstatement,funcretnode.getcopy);
  1905. result := createblock;
  1906. { consider it must not be inlined if called
  1907. again inside the args or itself }
  1908. procdefinition.proccalloption:=pocall_default;
  1909. firstpass(result);
  1910. procdefinition.proccalloption:=pocall_inline;
  1911. exit;
  1912. end;
  1913. end;
  1914. }
  1915. { calculate the parameter info for the procdef }
  1916. if not procdefinition.has_paraloc_info then
  1917. begin
  1918. procdefinition.requiredargarea:=paramanager.create_paraloc_info(procdefinition,callerside);
  1919. procdefinition.has_paraloc_info:=true;
  1920. end;
  1921. { calculate the parameter size needed for this call include varargs if they are available }
  1922. if assigned(varargsparas) then
  1923. pushedparasize:=paramanager.create_varargs_paraloc_info(procdefinition,varargsparas)
  1924. else
  1925. pushedparasize:=procdefinition.requiredargarea;
  1926. { record maximum parameter size used in this proc }
  1927. current_procinfo.allocate_push_parasize(pushedparasize);
  1928. { work trough all parameters to get the register requirements }
  1929. if assigned(left) then
  1930. tcallparanode(left).det_registers;
  1931. { order parameters }
  1932. order_parameters;
  1933. if assigned(methodpointerinit) then
  1934. firstpass(methodpointerinit);
  1935. if assigned(methodpointerdone) then
  1936. firstpass(methodpointerdone);
  1937. { function result node }
  1938. if assigned(_funcretnode) then
  1939. firstpass(_funcretnode);
  1940. { procedure variable ? }
  1941. if assigned(right) then
  1942. begin
  1943. firstpass(right);
  1944. { procedure does a call }
  1945. if not (block_type in [bt_const,bt_type]) then
  1946. include(current_procinfo.flags,pi_do_call);
  1947. end
  1948. else
  1949. { not a procedure variable }
  1950. begin
  1951. { calc the correture value for the register }
  1952. { handle predefined procedures }
  1953. if (procdefinition.proccalloption=pocall_inline) then
  1954. begin
  1955. { inherit flags }
  1956. current_procinfo.flags := current_procinfo.flags + ((procdefinition as tprocdef).inlininginfo^.flags*inherited_inlining_flags);
  1957. if assigned(methodpointer) then
  1958. CGMessage(cg_e_unable_inline_object_methods);
  1959. if assigned(right) then
  1960. CGMessage(cg_e_unable_inline_procvar);
  1961. if not assigned(inlinecode) then
  1962. begin
  1963. if assigned(tprocdef(procdefinition).inlininginfo^.code) then
  1964. inlinecode:=tprocdef(procdefinition).inlininginfo^.code.getcopy
  1965. else
  1966. CGMessage(cg_e_no_code_for_inline_stored);
  1967. if assigned(inlinecode) then
  1968. begin
  1969. { consider it has not inlined if called
  1970. again inside the args }
  1971. procdefinition.proccalloption:=pocall_default;
  1972. firstpass(inlinecode);
  1973. end;
  1974. end;
  1975. end
  1976. else
  1977. begin
  1978. if not (block_type in [bt_const,bt_type]) then
  1979. include(current_procinfo.flags,pi_do_call);
  1980. end;
  1981. end;
  1982. { implicit finally needed ? }
  1983. if resulttype.def.needs_inittable and
  1984. not paramanager.ret_in_param(resulttype.def,procdefinition.proccalloption) and
  1985. not assigned(funcretnode) then
  1986. include(current_procinfo.flags,pi_needs_implicit_finally);
  1987. { get a register for the return value }
  1988. if (not is_void(resulttype.def)) then
  1989. begin
  1990. if paramanager.ret_in_param(resulttype.def,procdefinition.proccalloption) then
  1991. begin
  1992. expectloc:=LOC_REFERENCE;
  1993. end
  1994. else
  1995. { for win32 records returned in EDX:EAX, we
  1996. move them to memory after ... }
  1997. if (resulttype.def.deftype=recorddef) then
  1998. begin
  1999. expectloc:=LOC_REFERENCE;
  2000. end
  2001. else
  2002. { ansi/widestrings must be registered, so we can dispose them }
  2003. if is_ansistring(resulttype.def) or
  2004. is_widestring(resulttype.def) then
  2005. begin
  2006. expectloc:=LOC_REFERENCE;
  2007. registersint:=1;
  2008. end
  2009. else
  2010. { we have only to handle the result if it is used }
  2011. if (cnf_return_value_used in callnodeflags) then
  2012. begin
  2013. case resulttype.def.deftype of
  2014. enumdef,
  2015. orddef :
  2016. begin
  2017. if (procdefinition.proctypeoption=potype_constructor) then
  2018. begin
  2019. expectloc:=LOC_REGISTER;
  2020. registersint:=1;
  2021. end
  2022. else
  2023. begin
  2024. expectloc:=LOC_REGISTER;
  2025. if is_64bit(resulttype.def) then
  2026. registersint:=2
  2027. else
  2028. registersint:=1;
  2029. end;
  2030. end;
  2031. floatdef :
  2032. begin
  2033. expectloc:=LOC_FPUREGISTER;
  2034. {$ifdef cpufpemu}
  2035. if (cs_fp_emulation in aktmoduleswitches) then
  2036. registersint:=1
  2037. else
  2038. {$endif cpufpemu}
  2039. {$ifdef m68k}
  2040. if (tfloatdef(resulttype.def).typ=s32real) then
  2041. registersint:=1
  2042. else
  2043. {$endif m68k}
  2044. registersfpu:=1;
  2045. end;
  2046. else
  2047. begin
  2048. expectloc:=LOC_REGISTER;
  2049. registersint:=1;
  2050. end;
  2051. end;
  2052. end
  2053. else
  2054. expectloc:=LOC_VOID;
  2055. end
  2056. else
  2057. expectloc:=LOC_VOID;
  2058. {$ifdef m68k}
  2059. { we need one more address register for virtual calls on m68k }
  2060. if (po_virtualmethod in procdefinition.procoptions) then
  2061. inc(registersint);
  2062. {$endif m68k}
  2063. { a fpu can be used in any procedure !! }
  2064. {$ifdef i386}
  2065. registersfpu:=procdefinition.fpu_used;
  2066. {$endif i386}
  2067. { if this is a call to a method calc the registers }
  2068. if (methodpointer<>nil) then
  2069. begin
  2070. if methodpointer.nodetype<>typen then
  2071. begin
  2072. firstpass(methodpointer);
  2073. registersfpu:=max(methodpointer.registersfpu,registersfpu);
  2074. registersint:=max(methodpointer.registersint,registersint);
  2075. {$ifdef SUPPORT_MMX }
  2076. registersmmx:=max(methodpointer.registersmmx,registersmmx);
  2077. {$endif SUPPORT_MMX}
  2078. end;
  2079. end;
  2080. { determine the registers of the procedure variable }
  2081. { is this OK for inlined procs also ?? (PM) }
  2082. if assigned(inlinecode) then
  2083. begin
  2084. registersfpu:=max(inlinecode.registersfpu,registersfpu);
  2085. registersint:=max(inlinecode.registersint,registersint);
  2086. {$ifdef SUPPORT_MMX}
  2087. registersmmx:=max(inlinecode.registersmmx,registersmmx);
  2088. {$endif SUPPORT_MMX}
  2089. end;
  2090. { determine the registers of the procedure variable }
  2091. { is this OK for inlined procs also ?? (PM) }
  2092. if assigned(right) then
  2093. begin
  2094. registersfpu:=max(right.registersfpu,registersfpu);
  2095. registersint:=max(right.registersint,registersint);
  2096. {$ifdef SUPPORT_MMX}
  2097. registersmmx:=max(right.registersmmx,registersmmx);
  2098. {$endif SUPPORT_MMX}
  2099. end;
  2100. { determine the registers of the procedure }
  2101. if assigned(left) then
  2102. begin
  2103. registersfpu:=max(left.registersfpu,registersfpu);
  2104. registersint:=max(left.registersint,registersint);
  2105. {$ifdef SUPPORT_MMX}
  2106. registersmmx:=max(left.registersmmx,registersmmx);
  2107. {$endif SUPPORT_MMX}
  2108. end;
  2109. errorexit:
  2110. if assigned(inlinecode) then
  2111. procdefinition.proccalloption:=pocall_inline;
  2112. end;
  2113. {$ifdef state_tracking}
  2114. function Tcallnode.track_state_pass(exec_known:boolean):boolean;
  2115. var hp:Tcallparanode;
  2116. value:Tnode;
  2117. begin
  2118. track_state_pass:=false;
  2119. hp:=Tcallparanode(left);
  2120. while assigned(hp) do
  2121. begin
  2122. if left.track_state_pass(exec_known) then
  2123. begin
  2124. left.resulttype.def:=nil;
  2125. do_resulttypepass(left);
  2126. end;
  2127. value:=aktstate.find_fact(hp.left);
  2128. if value<>nil then
  2129. begin
  2130. track_state_pass:=true;
  2131. hp.left.destroy;
  2132. hp.left:=value.getcopy;
  2133. do_resulttypepass(hp.left);
  2134. end;
  2135. hp:=Tcallparanode(hp.right);
  2136. end;
  2137. end;
  2138. {$endif}
  2139. function tcallnode.para_count:longint;
  2140. var
  2141. ppn : tcallparanode;
  2142. begin
  2143. result:=0;
  2144. ppn:=tcallparanode(left);
  2145. while assigned(ppn) do
  2146. begin
  2147. if not(assigned(ppn.paraitem) and
  2148. ppn.paraitem.is_hidden) then
  2149. inc(result);
  2150. ppn:=tcallparanode(ppn.right);
  2151. end;
  2152. end;
  2153. function tcallnode.docompare(p: tnode): boolean;
  2154. begin
  2155. docompare :=
  2156. inherited docompare(p) and
  2157. (symtableprocentry = tcallnode(p).symtableprocentry) and
  2158. (procdefinition = tcallnode(p).procdefinition) and
  2159. (methodpointer.isequal(tcallnode(p).methodpointer)) and
  2160. (((cnf_restypeset in callnodeflags) and (cnf_restypeset in tcallnode(p).callnodeflags) and
  2161. (equal_defs(restype.def,tcallnode(p).restype.def))) or
  2162. (not(cnf_restypeset in callnodeflags) and not(cnf_restypeset in tcallnode(p).callnodeflags)));
  2163. end;
  2164. procedure tcallnode.printnodedata(var t:text);
  2165. begin
  2166. if assigned(procdefinition) and
  2167. (procdefinition.deftype=procdef) then
  2168. writeln(t,printnodeindention,'proc = ',tprocdef(procdefinition).fullprocname(true))
  2169. else
  2170. begin
  2171. if assigned(symtableprocentry) then
  2172. writeln(t,printnodeindention,'proc = ',symtableprocentry.name)
  2173. else
  2174. writeln(t,printnodeindention,'proc = <nil>');
  2175. end;
  2176. printnode(t,methodpointer);
  2177. printnode(t,right);
  2178. printnode(t,left);
  2179. end;
  2180. begin
  2181. ccallnode:=tcallnode;
  2182. ccallparanode:=tcallparanode;
  2183. end.
  2184. {
  2185. $Log$
  2186. Revision 1.248 2004-09-21 17:25:12 peter
  2187. * paraloc branch merged
  2188. Revision 1.247 2004/09/13 20:29:00 peter
  2189. * use realname for abstract procs found
  2190. Revision 1.246.4.1 2004/08/31 20:43:06 peter
  2191. * paraloc patch
  2192. Revision 1.246 2004/08/28 20:00:50 peter
  2193. * use objrealname in Message1
  2194. Revision 1.245 2004/08/22 10:17:13 peter
  2195. * fixed crash when passing array constructor to formal parameter
  2196. Revision 1.244 2004/08/14 14:50:42 florian
  2197. * fixed several sparc alignment issues
  2198. + Jonas' inline node patch; non functional yet
  2199. Revision 1.243 2004/07/16 19:45:15 jonas
  2200. + temps can now also hold fpu values in registers (take care with use,
  2201. bacause of the x86 fpu stack)
  2202. * fpu parameters to node-inlined procedures can now also be put in
  2203. a register
  2204. Revision 1.242 2004/07/15 21:02:05 jonas
  2205. * the condition for when to use a temp in case of reference var/const
  2206. parameters was inverse
  2207. Revision 1.241 2004/07/15 19:55:39 jonas
  2208. + (incomplete) node_complexity function to assess the complexity of a
  2209. tree
  2210. + support for inlining value and const parameters at the node level
  2211. (all procedures without local variables and without formal parameters
  2212. can now be inlined at the node level)
  2213. Revision 1.240 2004/07/12 09:14:04 jonas
  2214. * inline procedures at the node tree level, but only under some very
  2215. limited circumstances for now (only procedures, and only if they have
  2216. no or only vs_out/vs_var parameters).
  2217. * fixed ppudump for inline procedures
  2218. * fixed ppudump for ppc
  2219. Revision 1.239 2004/06/20 08:55:29 florian
  2220. * logs truncated
  2221. Revision 1.238 2004/06/16 20:07:08 florian
  2222. * dwarf branch merged
  2223. Revision 1.237 2004/05/25 18:51:49 peter
  2224. * fix tcallnode.getcopy. the parameters need to be copied after
  2225. methodpointerinit is copied
  2226. Revision 1.236 2004/05/24 17:31:51 peter
  2227. * fix passing of array to open array of array (bug 3113)
  2228. Revision 1.235 2004/05/23 18:28:41 peter
  2229. * methodpointer is loaded into a temp when it was a calln
  2230. Revision 1.234 2004/05/23 15:06:20 peter
  2231. * implicit_finally flag must be set in pass1
  2232. * add check whether the implicit frame is generated when expected
  2233. }