nld.pas 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165
  1. {
  2. Copyright (c) 2000-2002 by Florian Klaempfl
  3. Type checking and register allocation for load/assignment nodes
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit nld;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,
  22. {$ifdef state_tracking}
  23. nstate,
  24. {$endif}
  25. symconst,symbase,symtype,symsym,symdef;
  26. type
  27. tloadnode = class(tunarynode)
  28. symtableentry : tsym;
  29. symtableentryderef : tderef;
  30. symtable : tsymtable;
  31. procdef : tprocdef;
  32. procdefderef : tderef;
  33. constructor create(v : tsym;st : tsymtable);virtual;
  34. constructor create_procvar(v : tsym;d:tprocdef;st : tsymtable);virtual;
  35. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  36. procedure ppuwrite(ppufile:tcompilerppufile);override;
  37. procedure buildderefimpl;override;
  38. procedure derefimpl;override;
  39. procedure set_mp(p:tnode);
  40. function is_addr_param_load:boolean;
  41. function _getcopy : tnode;override;
  42. function pass_1 : tnode;override;
  43. function det_resulttype:tnode;override;
  44. procedure mark_write;override;
  45. function docompare(p: tnode): boolean; override;
  46. procedure printnodedata(var t:text);override;
  47. end;
  48. tloadnodeclass = class of tloadnode;
  49. { different assignment types }
  50. tassigntype = (at_normal,at_plus,at_minus,at_star,at_slash);
  51. tassignmentnode = class(tbinarynode)
  52. assigntype : tassigntype;
  53. constructor create(l,r : tnode);virtual;
  54. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  55. procedure ppuwrite(ppufile:tcompilerppufile);override;
  56. function _getcopy : tnode;override;
  57. function pass_1 : tnode;override;
  58. function det_resulttype:tnode;override;
  59. {$ifdef state_tracking}
  60. function track_state_pass(exec_known:boolean):boolean;override;
  61. {$endif state_tracking}
  62. function docompare(p: tnode): boolean; override;
  63. end;
  64. tassignmentnodeclass = class of tassignmentnode;
  65. tarrayconstructorrangenode = class(tbinarynode)
  66. constructor create(l,r : tnode);virtual;
  67. function pass_1 : tnode;override;
  68. function det_resulttype:tnode;override;
  69. end;
  70. tarrayconstructorrangenodeclass = class of tarrayconstructorrangenode;
  71. tarrayconstructornode = class(tbinarynode)
  72. constructor create(l,r : tnode);virtual;
  73. function _getcopy : tnode;override;
  74. function pass_1 : tnode;override;
  75. function det_resulttype:tnode;override;
  76. function docompare(p: tnode): boolean; override;
  77. procedure force_type(tt:ttype);
  78. procedure insert_typeconvs;
  79. end;
  80. tarrayconstructornodeclass = class of tarrayconstructornode;
  81. ttypenode = class(tnode)
  82. allowed : boolean;
  83. restype : ttype;
  84. constructor create(t : ttype);virtual;
  85. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  86. procedure ppuwrite(ppufile:tcompilerppufile);override;
  87. procedure buildderefimpl;override;
  88. procedure derefimpl;override;
  89. function pass_1 : tnode;override;
  90. function det_resulttype:tnode;override;
  91. function docompare(p: tnode): boolean; override;
  92. end;
  93. ttypenodeclass = class of ttypenode;
  94. trttinode = class(tnode)
  95. l1,l2 : longint;
  96. rttitype : trttitype;
  97. rttidef : tstoreddef;
  98. rttidefderef : tderef;
  99. constructor create(def:tstoreddef;rt:trttitype);virtual;
  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. function pass_1 : tnode;override;
  106. function det_resulttype:tnode;override;
  107. function docompare(p: tnode): boolean; override;
  108. end;
  109. trttinodeclass = class of trttinode;
  110. var
  111. cloadnode : tloadnodeclass;
  112. cassignmentnode : tassignmentnodeclass;
  113. carrayconstructorrangenode : tarrayconstructorrangenodeclass;
  114. carrayconstructornode : tarrayconstructornodeclass;
  115. ctypenode : ttypenodeclass;
  116. crttinode : trttinodeclass;
  117. implementation
  118. uses
  119. cutils,verbose,globtype,globals,systems,
  120. symnot,
  121. defutil,defcmp,
  122. htypechk,pass_1,procinfo,paramgr,
  123. ncon,ninl,ncnv,nmem,ncal,nutils,
  124. cgobj,cgbase
  125. ;
  126. {*****************************************************************************
  127. TLOADNODE
  128. *****************************************************************************}
  129. constructor tloadnode.create(v : tsym;st : tsymtable);
  130. begin
  131. inherited create(loadn,nil);
  132. if not assigned(v) then
  133. internalerror(200108121);
  134. symtableentry:=v;
  135. symtable:=st;
  136. procdef:=nil;
  137. end;
  138. constructor tloadnode.create_procvar(v : tsym;d:tprocdef;st : tsymtable);
  139. begin
  140. inherited create(loadn,nil);
  141. if not assigned(v) then
  142. internalerror(200108121);
  143. symtableentry:=v;
  144. symtable:=st;
  145. procdef:=d;
  146. end;
  147. constructor tloadnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  148. begin
  149. inherited ppuload(t,ppufile);
  150. ppufile.getderef(symtableentryderef);
  151. symtable:=nil;
  152. ppufile.getderef(procdefderef);
  153. end;
  154. procedure tloadnode.ppuwrite(ppufile:tcompilerppufile);
  155. begin
  156. inherited ppuwrite(ppufile);
  157. ppufile.putderef(symtableentryderef);
  158. ppufile.putderef(procdefderef);
  159. end;
  160. procedure tloadnode.buildderefimpl;
  161. begin
  162. inherited buildderefimpl;
  163. symtableentryderef.build(symtableentry);
  164. procdefderef.build(procdef);
  165. end;
  166. procedure tloadnode.derefimpl;
  167. begin
  168. inherited derefimpl;
  169. symtableentry:=tsym(symtableentryderef.resolve);
  170. symtable:=symtableentry.owner;
  171. procdef:=tprocdef(procdefderef.resolve);
  172. end;
  173. procedure tloadnode.set_mp(p:tnode);
  174. begin
  175. { typen nodes should not be set }
  176. if p.nodetype=typen then
  177. internalerror(200301042);
  178. left:=p;
  179. end;
  180. function tloadnode._getcopy : tnode;
  181. var
  182. n : tloadnode;
  183. begin
  184. n:=tloadnode(inherited _getcopy);
  185. n.symtable:=symtable;
  186. n.symtableentry:=symtableentry;
  187. n.procdef:=procdef;
  188. result:=n;
  189. end;
  190. function tloadnode.is_addr_param_load:boolean;
  191. begin
  192. result:=(symtable.symtabletype=parasymtable) and
  193. (symtableentry.typ=paravarsym) and
  194. not(vo_has_local_copy in tparavarsym(symtableentry).varoptions) and
  195. not(nf_load_self_pointer in flags) and
  196. paramanager.push_addr_param(tparavarsym(symtableentry).varspez,tparavarsym(symtableentry).vartype.def,tprocdef(symtable.defowner).proccalloption);
  197. end;
  198. function tloadnode.det_resulttype:tnode;
  199. begin
  200. result:=nil;
  201. case symtableentry.typ of
  202. absolutevarsym :
  203. resulttype:=tabsolutevarsym(symtableentry).vartype;
  204. constsym:
  205. begin
  206. if tconstsym(symtableentry).consttyp=constresourcestring then
  207. resulttype:=cansistringtype
  208. else
  209. internalerror(22799);
  210. end;
  211. globalvarsym,
  212. paravarsym,
  213. localvarsym :
  214. begin
  215. inc(tabstractvarsym(symtableentry).refs);
  216. { Nested variable? The we need to load the framepointer of
  217. the parent procedure }
  218. if assigned(current_procinfo) then
  219. begin
  220. if (symtable.symtabletype in [localsymtable,parasymtable]) and
  221. (symtable.symtablelevel<>current_procinfo.procdef.parast.symtablelevel) then
  222. begin
  223. if assigned(left) then
  224. internalerror(200309289);
  225. left:=cloadparentfpnode.create(tprocdef(symtable.defowner));
  226. { we can't inline the referenced parent procedure }
  227. exclude(tprocdef(symtable.defowner).procoptions,po_inline);
  228. { reference in nested procedures, variable needs to be in memory }
  229. make_not_regable(self,vr_none);
  230. end;
  231. { static variables referenced in procedures or from finalization,
  232. variable needs to be in memory.
  233. It is too hard and the benefit is too small to detect whether a
  234. variable is only used in the finalization to add support for it (PFV) }
  235. if (symtable.symtabletype=staticsymtable) and
  236. (
  237. (symtable.symtablelevel<>current_procinfo.procdef.localst.symtablelevel) or
  238. (current_procinfo.procdef.proctypeoption=potype_unitfinalize)
  239. ) then
  240. make_not_regable(self,vr_none);
  241. end;
  242. { fix self type which is declared as voidpointer in the
  243. definition }
  244. if vo_is_self in tabstractvarsym(symtableentry).varoptions then
  245. begin
  246. resulttype.setdef(tprocdef(symtableentry.owner.defowner)._class);
  247. if (po_classmethod in tprocdef(symtableentry.owner.defowner).procoptions) or
  248. (po_staticmethod in tprocdef(symtableentry.owner.defowner).procoptions) then
  249. resulttype.setdef(tclassrefdef.create(resulttype))
  250. else if is_object(resulttype.def) and
  251. (nf_load_self_pointer in flags) then
  252. resulttype.setdef(tpointerdef.create(resulttype));
  253. end
  254. else if vo_is_vmt in tabstractvarsym(symtableentry).varoptions then
  255. begin
  256. resulttype.setdef(tprocdef(symtableentry.owner.defowner)._class);
  257. resulttype.setdef(tclassrefdef.create(resulttype));
  258. end
  259. else
  260. resulttype:=tabstractvarsym(symtableentry).vartype;
  261. end;
  262. typedconstsym :
  263. resulttype:=ttypedconstsym(symtableentry).typedconsttype;
  264. procsym :
  265. begin
  266. { Return the first procdef. In case of overlaoded
  267. procdefs the matching procdef will be choosen
  268. when the expected procvardef is known, see get_information
  269. in htypechk.pas (PFV) }
  270. if not assigned(procdef) then
  271. procdef:=tprocsym(symtableentry).first_procdef;
  272. { the result is a procdef, addrn and proc_to_procvar
  273. typeconvn need this as resulttype so they know
  274. that the address needs to be returned }
  275. resulttype.setdef(procdef);
  276. { process methodpointer }
  277. if assigned(left) then
  278. resulttypepass(left);
  279. end;
  280. labelsym:
  281. resulttype:=voidtype;
  282. else
  283. internalerror(200104141);
  284. end;
  285. end;
  286. procedure Tloadnode.mark_write;
  287. begin
  288. include(flags,nf_write);
  289. end;
  290. function tloadnode.pass_1 : tnode;
  291. begin
  292. result:=nil;
  293. expectloc:=LOC_REFERENCE;
  294. registersint:=0;
  295. registersfpu:=0;
  296. {$ifdef SUPPORT_MMX}
  297. registersmmx:=0;
  298. {$endif SUPPORT_MMX}
  299. if (cs_create_pic in aktmoduleswitches) and
  300. not(symtableentry.typ in [paravarsym,localvarsym]) then
  301. include(current_procinfo.flags,pi_needs_got);
  302. case symtableentry.typ of
  303. absolutevarsym :
  304. ;
  305. constsym:
  306. begin
  307. if tconstsym(symtableentry).consttyp=constresourcestring then
  308. expectloc:=LOC_CREFERENCE;
  309. end;
  310. globalvarsym,
  311. localvarsym,
  312. paravarsym :
  313. begin
  314. if assigned(left) then
  315. firstpass(left);
  316. if not is_addr_param_load and
  317. tabstractvarsym(symtableentry).is_regvar(is_addr_param_load) then
  318. begin
  319. case tabstractvarsym(symtableentry).varregable of
  320. vr_intreg,
  321. vr_addr :
  322. expectloc:=LOC_CREGISTER;
  323. vr_fpureg :
  324. expectloc:=LOC_CFPUREGISTER;
  325. vr_mmreg :
  326. expectloc:=LOC_CMMREGISTER;
  327. end
  328. end
  329. else
  330. if (tabstractvarsym(symtableentry).varspez=vs_const) then
  331. expectloc:=LOC_CREFERENCE;
  332. { we need a register for call by reference parameters }
  333. if paramanager.push_addr_param(tabstractvarsym(symtableentry).varspez,tabstractvarsym(symtableentry).vartype.def,pocall_default) then
  334. registersint:=1;
  335. if ([vo_is_thread_var,vo_is_dll_var]*tabstractvarsym(symtableentry).varoptions)<>[] then
  336. registersint:=1;
  337. if (target_info.system=system_powerpc_darwin) and
  338. ([vo_is_dll_var,vo_is_external] * tabstractvarsym(symtableentry).varoptions <> []) then
  339. include(current_procinfo.flags,pi_needs_got);
  340. { call to get address of threadvar }
  341. if (vo_is_thread_var in tabstractvarsym(symtableentry).varoptions) then
  342. include(current_procinfo.flags,pi_do_call);
  343. if nf_write in flags then
  344. Tabstractvarsym(symtableentry).trigger_notifications(vn_onwrite)
  345. else
  346. Tabstractvarsym(symtableentry).trigger_notifications(vn_onread);
  347. { count variable references }
  348. if cg.t_times>1 then
  349. inc(tabstractvarsym(symtableentry).refs,cg.t_times-1);
  350. end;
  351. typedconstsym :
  352. ;
  353. procsym :
  354. begin
  355. { method pointer ? }
  356. if assigned(left) then
  357. begin
  358. expectloc:=LOC_CREFERENCE;
  359. firstpass(left);
  360. registersint:=max(registersint,left.registersint);
  361. registersfpu:=max(registersfpu,left.registersfpu);
  362. {$ifdef SUPPORT_MMX}
  363. registersmmx:=max(registersmmx,left.registersmmx);
  364. {$endif SUPPORT_MMX}
  365. end;
  366. end;
  367. labelsym :
  368. ;
  369. else
  370. internalerror(200104143);
  371. end;
  372. end;
  373. function tloadnode.docompare(p: tnode): boolean;
  374. begin
  375. docompare :=
  376. inherited docompare(p) and
  377. (symtableentry = tloadnode(p).symtableentry) and
  378. (procdef = tloadnode(p).procdef) and
  379. (symtable = tloadnode(p).symtable);
  380. end;
  381. procedure Tloadnode.printnodedata(var t:text);
  382. begin
  383. inherited printnodedata(t);
  384. write(t,printnodeindention,'symbol = ',symtableentry.name);
  385. if symtableentry.typ=procsym then
  386. write(t,printnodeindention,'procdef = ',procdef.mangledname);
  387. writeln(t,'');
  388. end;
  389. {*****************************************************************************
  390. TASSIGNMENTNODE
  391. *****************************************************************************}
  392. constructor tassignmentnode.create(l,r : tnode);
  393. begin
  394. inherited create(assignn,l,r);
  395. l.mark_write;
  396. assigntype:=at_normal;
  397. end;
  398. constructor tassignmentnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  399. begin
  400. inherited ppuload(t,ppufile);
  401. assigntype:=tassigntype(ppufile.getbyte);
  402. end;
  403. procedure tassignmentnode.ppuwrite(ppufile:tcompilerppufile);
  404. begin
  405. inherited ppuwrite(ppufile);
  406. ppufile.putbyte(byte(assigntype));
  407. end;
  408. function tassignmentnode._getcopy : tnode;
  409. var
  410. n : tassignmentnode;
  411. begin
  412. n:=tassignmentnode(inherited _getcopy);
  413. n.assigntype:=assigntype;
  414. result:=n;
  415. end;
  416. function tassignmentnode.det_resulttype:tnode;
  417. var
  418. hp : tnode;
  419. useshelper : boolean;
  420. begin
  421. result:=nil;
  422. resulttype:=voidtype;
  423. { must be made unique }
  424. set_unique(left);
  425. resulttypepass(left);
  426. if is_ansistring(left.resulttype.def) then
  427. begin
  428. { fold <ansistring>:=<ansistring>+<char|shortstring|ansistring> }
  429. if (right.nodetype=addn) and
  430. left.isequal(tbinarynode(right).left) and
  431. { don't fold multiple concatenations else we could get trouble
  432. with multiple uses of s
  433. }
  434. (tbinarynode(right).left.nodetype<>addn) and
  435. (tbinarynode(right).right.nodetype<>addn) then
  436. begin
  437. { don't do a resulttypepass(right), since then the addnode }
  438. { may insert typeconversions that make this optimization }
  439. { opportunity quite difficult to detect (JM) }
  440. resulttypepass(tbinarynode(right).left);
  441. resulttypepass(tbinarynode(right).right);
  442. if (tbinarynode(right).right.nodetype=stringconstn) or
  443. is_char(tbinarynode(right).right.resulttype.def) or
  444. is_shortstring(tbinarynode(right).right.resulttype.def) or
  445. is_ansistring(tbinarynode(right).right.resulttype.def) then
  446. begin
  447. { remove property flag so it'll not trigger an error }
  448. exclude(left.flags,nf_isproperty);
  449. { generate call to helper }
  450. hp:=ccallparanode.create(tbinarynode(right).right,
  451. ccallparanode.create(left,nil));
  452. if is_char(tbinarynode(right).right.resulttype.def) then
  453. result:=ccallnode.createintern('fpc_'+Tstringdef(left.resulttype.def).stringtypname+'_append_char',hp)
  454. else if is_shortstring(tbinarynode(right).right.resulttype.def) then
  455. result:=ccallnode.createintern('fpc_'+Tstringdef(left.resulttype.def).stringtypname+'_append_shortstring',hp)
  456. else
  457. result:=ccallnode.createintern('fpc_'+Tstringdef(left.resulttype.def).stringtypname+'_append_ansistring',hp);
  458. tbinarynode(right).right:=nil;
  459. left:=nil;
  460. exit;
  461. end;
  462. end;
  463. end
  464. else
  465. if is_shortstring(left.resulttype.def) then
  466. begin
  467. { fold <shortstring>:=<shortstring>+<shortstring>,
  468. <shortstring>+<char> is handled by an optimized node }
  469. if (right.nodetype=addn) and
  470. left.isequal(tbinarynode(right).left) and
  471. { don't fold multiple concatenations else we could get trouble
  472. with multiple uses of s }
  473. (tbinarynode(right).left.nodetype<>addn) and
  474. (tbinarynode(right).right.nodetype<>addn) then
  475. begin
  476. { don't do a resulttypepass(right), since then the addnode }
  477. { may insert typeconversions that make this optimization }
  478. { opportunity quite difficult to detect (JM) }
  479. resulttypepass(tbinarynode(right).left);
  480. resulttypepass(tbinarynode(right).right);
  481. if is_shortstring(tbinarynode(right).right.resulttype.def) then
  482. begin
  483. { remove property flag so it'll not trigger an error }
  484. exclude(left.flags,nf_isproperty);
  485. { generate call to helper }
  486. hp:=ccallparanode.create(tbinarynode(right).right,
  487. ccallparanode.create(left,nil));
  488. if is_shortstring(tbinarynode(right).right.resulttype.def) then
  489. result:=ccallnode.createintern('fpc_shortstr_append_shortstr',hp);
  490. tbinarynode(right).right:=nil;
  491. left:=nil;
  492. exit;
  493. end;
  494. end;
  495. end;
  496. resulttypepass(right);
  497. set_varstate(right,vs_read,[vsf_must_be_valid]);
  498. set_varstate(left,vs_written,[]);
  499. if codegenerror then
  500. exit;
  501. { tp procvar support, when we don't expect a procvar
  502. then we need to call the procvar }
  503. if (left.resulttype.def.deftype<>procvardef) then
  504. maybe_call_procvar(right,true);
  505. { assignments to formaldefs and open arrays aren't allowed }
  506. if (left.resulttype.def.deftype=formaldef) or
  507. is_open_array(left.resulttype.def) then
  508. CGMessage(type_e_operator_not_allowed);
  509. { test if node can be assigned, properties are allowed }
  510. valid_for_assignment(left,true);
  511. { assigning nil to a dynamic array clears the array }
  512. if is_dynamic_array(left.resulttype.def) and
  513. (right.nodetype=niln) then
  514. begin
  515. hp:=ccallparanode.create(caddrnode.create_internal
  516. (crttinode.create(tstoreddef(left.resulttype.def),initrtti)),
  517. ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),nil));
  518. result := ccallnode.createintern('fpc_dynarray_clear',hp);
  519. left:=nil;
  520. exit;
  521. end;
  522. { shortstring helpers can do the conversion directly,
  523. so treat them separatly }
  524. if (is_shortstring(left.resulttype.def)) then
  525. begin
  526. { insert typeconv, except for chars that are handled in
  527. secondpass and except for ansi/wide string that can
  528. be converted immediatly }
  529. if not(is_char(right.resulttype.def) or
  530. (right.resulttype.def.deftype=stringdef)) then
  531. inserttypeconv(right,left.resulttype);
  532. if right.resulttype.def.deftype=stringdef then
  533. begin
  534. useshelper:=true;
  535. { convert constant strings to shortstrings. But
  536. skip empty constant strings, that will be handled
  537. in secondpass }
  538. if (right.nodetype=stringconstn) then
  539. begin
  540. { verify if range fits within shortstring }
  541. { just emit a warning, delphi gives an }
  542. { error, only if the type definition of }
  543. { of the string is less < 255 characters }
  544. if not is_open_string(left.resulttype.def) and
  545. (tstringconstnode(right).len > tstringdef(left.resulttype.def).len) then
  546. cgmessage(type_w_string_too_long);
  547. inserttypeconv(right,left.resulttype);
  548. if (tstringconstnode(right).len=0) then
  549. useshelper:=false;
  550. end;
  551. { rest is done in pass 1 (JM) }
  552. if useshelper then
  553. exit;
  554. end
  555. end
  556. else
  557. begin
  558. { check if the assignment may cause a range check error }
  559. check_ranges(fileinfo,right,left.resulttype.def);
  560. inserttypeconv(right,left.resulttype);
  561. end;
  562. { call helpers for interface }
  563. if is_interfacecom(left.resulttype.def) then
  564. begin
  565. {
  566. hp:=
  567. ccallparanode.create(
  568. ctypeconvnode.create_internal(right,voidpointertype),
  569. ccallparanode.create(
  570. ctypeconvnode.create_internal(left,voidpointertype),
  571. nil));
  572. result:=ccallnode.createintern('fpc_intf_assign',hp);
  573. }
  574. hp:=
  575. ccallparanode.create(
  576. cguidconstnode.create(tobjectdef(left.resulttype.def).iidguid^),
  577. ccallparanode.create(
  578. ctypeconvnode.create_internal(right,voidpointertype),
  579. ccallparanode.create(
  580. ctypeconvnode.create_internal(left,voidpointertype),
  581. nil)));
  582. result:=ccallnode.createintern('fpc_intf_assign_by_iid',hp);
  583. left:=nil;
  584. right:=nil;
  585. exit;
  586. end;
  587. { call helpers for variant, they can contain non ref. counted types like
  588. vararrays which must be really copied }
  589. if left.resulttype.def.deftype=variantdef then
  590. begin
  591. hp:=ccallparanode.create(ctypeconvnode.create_internal(
  592. caddrnode.create_internal(right),voidpointertype),
  593. ccallparanode.create(ctypeconvnode.create_internal(
  594. caddrnode.create_internal(left),voidpointertype),
  595. nil));
  596. result:=ccallnode.createintern('fpc_variant_copy',hp);
  597. left:=nil;
  598. right:=nil;
  599. exit;
  600. end;
  601. { call helpers for windows widestrings, they aren't ref. counted }
  602. if (tf_winlikewidestring in target_info.flags) and is_widestring(left.resulttype.def) then
  603. begin
  604. hp:=ccallparanode.create(ctypeconvnode.create_internal(right,voidpointertype),
  605. ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),
  606. nil));
  607. result:=ccallnode.createintern('fpc_widestr_assign',hp);
  608. left:=nil;
  609. right:=nil;
  610. exit;
  611. end;
  612. { check if local proc/func is assigned to procvar }
  613. if right.resulttype.def.deftype=procvardef then
  614. test_local_to_procvar(tprocvardef(right.resulttype.def),left.resulttype.def);
  615. end;
  616. function tassignmentnode.pass_1 : tnode;
  617. var
  618. hp: tnode;
  619. begin
  620. result:=nil;
  621. expectloc:=LOC_VOID;
  622. firstpass(left);
  623. firstpass(right);
  624. { assignment to refcounted variable -> inc/decref }
  625. if (not is_class(left.resulttype.def) and
  626. left.resulttype.def.needs_inittable) then
  627. include(current_procinfo.flags,pi_do_call);
  628. if codegenerror then
  629. exit;
  630. if (is_shortstring(left.resulttype.def)) then
  631. begin
  632. if right.resulttype.def.deftype=stringdef then
  633. begin
  634. if (right.nodetype<>stringconstn) or
  635. (tstringconstnode(right).len<>0) then
  636. begin
  637. if (cs_opt_level1 in aktoptimizerswitches) and
  638. (right.nodetype in [calln,blockn]) and
  639. (left.nodetype = temprefn) and
  640. is_shortstring(right.resulttype.def) and
  641. not is_open_string(left.resulttype.def) and
  642. (tstringdef(left.resulttype.def).len = 255) then
  643. begin
  644. { the blocknode case is handled in pass_2 at the temp }
  645. { reference level (mainly for callparatemp) (JM) }
  646. if (right.nodetype = calln) then
  647. begin
  648. tcallnode(right).funcretnode := left;
  649. result := right;
  650. end
  651. else
  652. exit;
  653. end
  654. else
  655. begin
  656. hp:=ccallparanode.create
  657. (right,
  658. ccallparanode.create(cinlinenode.create
  659. (in_high_x,false,left.getcopy),nil));
  660. result:=ccallnode.createinternreturn('fpc_'+tstringdef(right.resulttype.def).stringtypname+'_to_shortstr',hp,left);
  661. firstpass(result);
  662. end;
  663. left:=nil;
  664. right:=nil;
  665. exit;
  666. end;
  667. end;
  668. end;
  669. if (cs_opt_level1 in aktoptimizerswitches) and
  670. (right.nodetype = calln) and
  671. { left must be a temp, since otherwise as soon as you modify the }
  672. { result, the current left node is modified and that one may }
  673. { still be an argument to the function or even accessed in the }
  674. { function }
  675. (((left.nodetype = temprefn) and
  676. paramanager.ret_in_param(right.resulttype.def,
  677. tcallnode(right).procdefinition.proccalloption)) or
  678. { there's special support for ansi/widestrings in the callnode }
  679. is_ansistring(right.resulttype.def) or
  680. is_widestring(right.resulttype.def)) then
  681. begin
  682. make_not_regable(left,vr_addr);
  683. tcallnode(right).funcretnode := left;
  684. result := right;
  685. left := nil;
  686. right := nil;
  687. exit;
  688. end;
  689. registersint:=left.registersint+right.registersint;
  690. registersfpu:=max(left.registersfpu,right.registersfpu);
  691. {$ifdef SUPPORT_MMX}
  692. registersmmx:=max(left.registersmmx,right.registersmmx);
  693. {$endif SUPPORT_MMX}
  694. end;
  695. function tassignmentnode.docompare(p: tnode): boolean;
  696. begin
  697. docompare :=
  698. inherited docompare(p) and
  699. (assigntype = tassignmentnode(p).assigntype);
  700. end;
  701. {$ifdef state_tracking}
  702. function Tassignmentnode.track_state_pass(exec_known:boolean):boolean;
  703. var se:Tstate_entry;
  704. begin
  705. track_state_pass:=false;
  706. if exec_known then
  707. begin
  708. track_state_pass:=right.track_state_pass(exec_known);
  709. {Force a new resulttype pass.}
  710. right.resulttype.def:=nil;
  711. do_resulttypepass(right);
  712. resulttypepass(right);
  713. aktstate.store_fact(left.getcopy,right.getcopy);
  714. end
  715. else
  716. aktstate.delete_fact(left);
  717. end;
  718. {$endif}
  719. {*****************************************************************************
  720. TARRAYCONSTRUCTORRANGENODE
  721. *****************************************************************************}
  722. constructor tarrayconstructorrangenode.create(l,r : tnode);
  723. begin
  724. inherited create(arrayconstructorrangen,l,r);
  725. end;
  726. function tarrayconstructorrangenode.det_resulttype:tnode;
  727. begin
  728. result:=nil;
  729. resulttypepass(left);
  730. resulttypepass(right);
  731. set_varstate(left,vs_read,[vsf_must_be_valid]);
  732. set_varstate(right,vs_read,[vsf_must_be_valid]);
  733. if codegenerror then
  734. exit;
  735. resulttype:=left.resulttype;
  736. end;
  737. function tarrayconstructorrangenode.pass_1 : tnode;
  738. begin
  739. firstpass(left);
  740. firstpass(right);
  741. expectloc:=LOC_CREFERENCE;
  742. calcregisters(self,0,0,0);
  743. result:=nil;
  744. end;
  745. {****************************************************************************
  746. TARRAYCONSTRUCTORNODE
  747. *****************************************************************************}
  748. constructor tarrayconstructornode.create(l,r : tnode);
  749. begin
  750. inherited create(arrayconstructorn,l,r);
  751. end;
  752. function tarrayconstructornode._getcopy : tnode;
  753. var
  754. n : tarrayconstructornode;
  755. begin
  756. n:=tarrayconstructornode(inherited _getcopy);
  757. result:=n;
  758. end;
  759. function tarrayconstructornode.det_resulttype:tnode;
  760. var
  761. htype : ttype;
  762. hp : tarrayconstructornode;
  763. len : longint;
  764. varia : boolean;
  765. begin
  766. result:=nil;
  767. { are we allowing array constructor? Then convert it to a set }
  768. if not allow_array_constructor then
  769. begin
  770. hp:=tarrayconstructornode(getcopy);
  771. arrayconstructor_to_set(tnode(hp));
  772. result:=hp;
  773. exit;
  774. end;
  775. { only pass left tree, right tree contains next construct if any }
  776. htype.reset;
  777. len:=0;
  778. varia:=false;
  779. if assigned(left) then
  780. begin
  781. hp:=self;
  782. while assigned(hp) do
  783. begin
  784. resulttypepass(hp.left);
  785. set_varstate(hp.left,vs_read,[vsf_must_be_valid]);
  786. if (htype.def=nil) then
  787. htype:=hp.left.resulttype
  788. else
  789. begin
  790. if ((nf_novariaallowed in flags) or (not varia)) and
  791. (not equal_defs(htype.def,hp.left.resulttype.def)) then
  792. begin
  793. varia:=true;
  794. end;
  795. end;
  796. inc(len);
  797. hp:=tarrayconstructornode(hp.right);
  798. end;
  799. end;
  800. { Set the type of empty or varia arrays to void. Also
  801. do this if the type is array of const/open array
  802. because those can't be used with setelementtype }
  803. if not assigned(htype.def) or
  804. varia or
  805. is_array_of_const(htype.def) or
  806. is_open_array(htype.def) then
  807. htype:=voidtype;
  808. resulttype.setdef(tarraydef.create(0,len-1,s32inttype));
  809. tarraydef(resulttype.def).setelementtype(htype);
  810. include(tarraydef(resulttype.def).arrayoptions,ado_IsConstructor);
  811. if varia then
  812. include(tarraydef(resulttype.def).arrayoptions,ado_IsVariant);
  813. end;
  814. procedure tarrayconstructornode.force_type(tt:ttype);
  815. var
  816. hp : tarrayconstructornode;
  817. begin
  818. tarraydef(resulttype.def).setelementtype(tt);
  819. include(tarraydef(resulttype.def).arrayoptions,ado_IsConstructor);
  820. exclude(tarraydef(resulttype.def).arrayoptions,ado_IsVariant);
  821. if assigned(left) then
  822. begin
  823. hp:=self;
  824. while assigned(hp) do
  825. begin
  826. inserttypeconv(hp.left,tt);
  827. hp:=tarrayconstructornode(hp.right);
  828. end;
  829. end;
  830. end;
  831. procedure tarrayconstructornode.insert_typeconvs;
  832. var
  833. hp : tarrayconstructornode;
  834. dovariant : boolean;
  835. begin
  836. dovariant:=(nf_forcevaria in flags) or (ado_isvariant in tarraydef(resulttype.def).arrayoptions);
  837. { only pass left tree, right tree contains next construct if any }
  838. if assigned(left) then
  839. begin
  840. hp:=self;
  841. while assigned(hp) do
  842. begin
  843. resulttypepass(hp.left);
  844. { Insert typeconvs for array of const }
  845. if dovariant then
  846. { at this time C varargs are no longer an arrayconstructornode }
  847. insert_varargstypeconv(hp.left,false);
  848. hp:=tarrayconstructornode(hp.right);
  849. end;
  850. end;
  851. end;
  852. function tarrayconstructornode.pass_1 : tnode;
  853. var
  854. hp : tarrayconstructornode;
  855. do_variant:boolean;
  856. begin
  857. do_variant:=(nf_forcevaria in flags) or (ado_isvariant in tarraydef(resulttype.def).arrayoptions);
  858. result:=nil;
  859. { Insert required type convs, this must be
  860. done in pass 1, because the call must be
  861. resulttypepassed already }
  862. if assigned(left) then
  863. begin
  864. insert_typeconvs;
  865. { call firstpass for all nodes }
  866. hp:=self;
  867. while assigned(hp) do
  868. begin
  869. if hp.left<>nil then
  870. begin
  871. {This check is pessimistic; a call will happen depending
  872. on the location in which the elements will be found in
  873. pass 2.}
  874. if not do_variant then
  875. include(current_procinfo.flags,pi_do_call);
  876. firstpass(hp.left);
  877. end;
  878. hp:=tarrayconstructornode(hp.right);
  879. end;
  880. end;
  881. expectloc:=LOC_CREFERENCE;
  882. calcregisters(self,0,0,0);
  883. end;
  884. function tarrayconstructornode.docompare(p: tnode): boolean;
  885. begin
  886. docompare:=inherited docompare(p);
  887. end;
  888. {*****************************************************************************
  889. TTYPENODE
  890. *****************************************************************************}
  891. constructor ttypenode.create(t : ttype);
  892. begin
  893. inherited create(typen);
  894. restype:=t;
  895. allowed:=false;
  896. end;
  897. constructor ttypenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  898. begin
  899. inherited ppuload(t,ppufile);
  900. ppufile.gettype(restype);
  901. allowed:=boolean(ppufile.getbyte);
  902. end;
  903. procedure ttypenode.ppuwrite(ppufile:tcompilerppufile);
  904. begin
  905. inherited ppuwrite(ppufile);
  906. ppufile.puttype(restype);
  907. ppufile.putbyte(byte(allowed));
  908. end;
  909. procedure ttypenode.buildderefimpl;
  910. begin
  911. inherited buildderefimpl;
  912. restype.buildderef;
  913. end;
  914. procedure ttypenode.derefimpl;
  915. begin
  916. inherited derefimpl;
  917. restype.resolve;
  918. end;
  919. function ttypenode.det_resulttype:tnode;
  920. begin
  921. result:=nil;
  922. resulttype:=restype;
  923. { check if it's valid }
  924. if restype.def.deftype = errordef then
  925. CGMessage(parser_e_illegal_expression);
  926. end;
  927. function ttypenode.pass_1 : tnode;
  928. begin
  929. result:=nil;
  930. expectloc:=LOC_VOID;
  931. { a typenode can't generate code, so we give here
  932. an error. Else it'll be an abstract error in pass_2.
  933. Only when the allowed flag is set we don't generate
  934. an error }
  935. if not allowed then
  936. Message(parser_e_no_type_not_allowed_here);
  937. end;
  938. function ttypenode.docompare(p: tnode): boolean;
  939. begin
  940. docompare :=
  941. inherited docompare(p);
  942. end;
  943. {*****************************************************************************
  944. TRTTINODE
  945. *****************************************************************************}
  946. constructor trttinode.create(def:tstoreddef;rt:trttitype);
  947. begin
  948. inherited create(rttin);
  949. rttidef:=def;
  950. rttitype:=rt;
  951. end;
  952. constructor trttinode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  953. begin
  954. inherited ppuload(t,ppufile);
  955. ppufile.getderef(rttidefderef);
  956. rttitype:=trttitype(ppufile.getbyte);
  957. end;
  958. procedure trttinode.ppuwrite(ppufile:tcompilerppufile);
  959. begin
  960. inherited ppuwrite(ppufile);
  961. ppufile.putderef(rttidefderef);
  962. ppufile.putbyte(byte(rttitype));
  963. end;
  964. procedure trttinode.buildderefimpl;
  965. begin
  966. inherited buildderefimpl;
  967. rttidefderef.build(rttidef);
  968. end;
  969. procedure trttinode.derefimpl;
  970. begin
  971. inherited derefimpl;
  972. rttidef:=tstoreddef(rttidefderef.resolve);
  973. end;
  974. function trttinode._getcopy : tnode;
  975. var
  976. n : trttinode;
  977. begin
  978. n:=trttinode(inherited _getcopy);
  979. n.rttidef:=rttidef;
  980. n.rttitype:=rttitype;
  981. result:=n;
  982. end;
  983. function trttinode.det_resulttype:tnode;
  984. begin
  985. { rtti information will be returned as a void pointer }
  986. result:=nil;
  987. resulttype:=voidpointertype;
  988. end;
  989. function trttinode.pass_1 : tnode;
  990. begin
  991. result:=nil;
  992. expectloc:=LOC_CREFERENCE;
  993. end;
  994. function trttinode.docompare(p: tnode): boolean;
  995. begin
  996. docompare :=
  997. inherited docompare(p) and
  998. (rttidef = trttinode(p).rttidef) and
  999. (rttitype = trttinode(p).rttitype);
  1000. end;
  1001. begin
  1002. cloadnode:=tloadnode;
  1003. cassignmentnode:=tassignmentnode;
  1004. carrayconstructorrangenode:=tarrayconstructorrangenode;
  1005. carrayconstructornode:=tarrayconstructornode;
  1006. ctypenode:=ttypenode;
  1007. crttinode:=trttinode;
  1008. end.