nld.pas 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210
  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. begin
  208. {$ifdef ansistring_bits}
  209. case aktansistring_bits of
  210. sb_16:
  211. resulttype:=cansistringtype16;
  212. sb_32:
  213. resulttype:=cansistringtype32;
  214. sb_64:
  215. resulttype:=cansistringtype64;
  216. end;
  217. {$else}
  218. resulttype:=cansistringtype
  219. {$endif}
  220. end
  221. else
  222. internalerror(22799);
  223. end;
  224. globalvarsym,
  225. paravarsym,
  226. localvarsym :
  227. begin
  228. inc(tabstractvarsym(symtableentry).refs);
  229. { Nested variable? The we need to load the framepointer of
  230. the parent procedure }
  231. if assigned(current_procinfo) then
  232. begin
  233. if (symtable.symtabletype in [localsymtable,parasymtable]) and
  234. (symtable.symtablelevel<>current_procinfo.procdef.parast.symtablelevel) then
  235. begin
  236. if assigned(left) then
  237. internalerror(200309289);
  238. left:=cloadparentfpnode.create(tprocdef(symtable.defowner));
  239. { we can't inline the referenced parent procedure }
  240. exclude(tprocdef(symtable.defowner).procoptions,po_inline);
  241. { reference in nested procedures, variable needs to be in memory }
  242. make_not_regable(self);
  243. end;
  244. { static variables referenced in procedures or from finalization,
  245. variable needs to be in memory.
  246. It is too hard and the benefit is too small to detect whether a
  247. variable is only used in the finalization to add support for it (PFV) }
  248. if (symtable.symtabletype=staticsymtable) and
  249. (
  250. (symtable.symtablelevel<>current_procinfo.procdef.localst.symtablelevel) or
  251. (current_procinfo.procdef.proctypeoption=potype_unitfinalize)
  252. ) then
  253. make_not_regable(self);
  254. end;
  255. { fix self type which is declared as voidpointer in the
  256. definition }
  257. if vo_is_self in tabstractvarsym(symtableentry).varoptions then
  258. begin
  259. resulttype.setdef(tprocdef(symtableentry.owner.defowner)._class);
  260. if (po_classmethod in tprocdef(symtableentry.owner.defowner).procoptions) or
  261. (po_staticmethod in tprocdef(symtableentry.owner.defowner).procoptions) then
  262. resulttype.setdef(tclassrefdef.create(resulttype))
  263. else if is_object(resulttype.def) and
  264. (nf_load_self_pointer in flags) then
  265. resulttype.setdef(tpointerdef.create(resulttype));
  266. end
  267. else if vo_is_vmt in tabstractvarsym(symtableentry).varoptions then
  268. begin
  269. resulttype.setdef(tprocdef(symtableentry.owner.defowner)._class);
  270. resulttype.setdef(tclassrefdef.create(resulttype));
  271. end
  272. else
  273. resulttype:=tabstractvarsym(symtableentry).vartype;
  274. end;
  275. typedconstsym :
  276. resulttype:=ttypedconstsym(symtableentry).typedconsttype;
  277. procsym :
  278. begin
  279. if not assigned(procdef) then
  280. begin
  281. if Tprocsym(symtableentry).procdef_count>1 then
  282. CGMessage(parser_e_no_overloaded_procvars);
  283. procdef:=tprocsym(symtableentry).first_procdef;
  284. end;
  285. { the result is a procdef, addrn and proc_to_procvar
  286. typeconvn need this as resulttype so they know
  287. that the address needs to be returned }
  288. resulttype.setdef(procdef);
  289. { process methodpointer }
  290. if assigned(left) then
  291. resulttypepass(left);
  292. end;
  293. labelsym:
  294. resulttype:=voidtype;
  295. else
  296. internalerror(200104141);
  297. end;
  298. end;
  299. procedure Tloadnode.mark_write;
  300. begin
  301. include(flags,nf_write);
  302. end;
  303. function tloadnode.pass_1 : tnode;
  304. begin
  305. result:=nil;
  306. expectloc:=LOC_REFERENCE;
  307. registersint:=0;
  308. registersfpu:=0;
  309. {$ifdef SUPPORT_MMX}
  310. registersmmx:=0;
  311. {$endif SUPPORT_MMX}
  312. if (cs_create_pic in aktmoduleswitches) and
  313. not(symtableentry.typ in [paravarsym,localvarsym]) then
  314. include(current_procinfo.flags,pi_needs_got);
  315. case symtableentry.typ of
  316. absolutevarsym :
  317. ;
  318. constsym:
  319. begin
  320. if tconstsym(symtableentry).consttyp=constresourcestring then
  321. expectloc:=LOC_CREFERENCE;
  322. end;
  323. globalvarsym,
  324. localvarsym,
  325. paravarsym :
  326. begin
  327. if assigned(left) then
  328. firstpass(left);
  329. if not is_addr_param_load and
  330. tabstractvarsym(symtableentry).is_regvar then
  331. begin
  332. case tabstractvarsym(symtableentry).varregable of
  333. vr_intreg :
  334. expectloc:=LOC_CREGISTER;
  335. vr_fpureg :
  336. expectloc:=LOC_CFPUREGISTER;
  337. vr_mmreg :
  338. expectloc:=LOC_CMMREGISTER;
  339. end
  340. end
  341. else
  342. if (tabstractvarsym(symtableentry).varspez=vs_const) then
  343. expectloc:=LOC_CREFERENCE;
  344. { we need a register for call by reference parameters }
  345. if paramanager.push_addr_param(tabstractvarsym(symtableentry).varspez,tabstractvarsym(symtableentry).vartype.def,pocall_default) then
  346. registersint:=1;
  347. if ([vo_is_thread_var,vo_is_dll_var]*tabstractvarsym(symtableentry).varoptions)<>[] then
  348. registersint:=1;
  349. if (target_info.system=system_powerpc_darwin) and
  350. ([vo_is_dll_var,vo_is_external] * tabstractvarsym(symtableentry).varoptions <> []) then
  351. include(current_procinfo.flags,pi_needs_got);
  352. { call to get address of threadvar }
  353. if (vo_is_thread_var in tabstractvarsym(symtableentry).varoptions) then
  354. include(current_procinfo.flags,pi_do_call);
  355. if nf_write in flags then
  356. Tabstractvarsym(symtableentry).trigger_notifications(vn_onwrite)
  357. else
  358. Tabstractvarsym(symtableentry).trigger_notifications(vn_onread);
  359. { count variable references }
  360. if cg.t_times>1 then
  361. inc(tabstractvarsym(symtableentry).refs,cg.t_times-1);
  362. end;
  363. typedconstsym :
  364. ;
  365. procsym :
  366. begin
  367. { method pointer ? }
  368. if assigned(left) then
  369. begin
  370. expectloc:=LOC_CREFERENCE;
  371. firstpass(left);
  372. registersint:=max(registersint,left.registersint);
  373. registersfpu:=max(registersfpu,left.registersfpu);
  374. {$ifdef SUPPORT_MMX}
  375. registersmmx:=max(registersmmx,left.registersmmx);
  376. {$endif SUPPORT_MMX}
  377. end;
  378. end;
  379. labelsym :
  380. ;
  381. else
  382. internalerror(200104143);
  383. end;
  384. end;
  385. function tloadnode.docompare(p: tnode): boolean;
  386. begin
  387. docompare :=
  388. inherited docompare(p) and
  389. (symtableentry = tloadnode(p).symtableentry) and
  390. (procdef = tloadnode(p).procdef) and
  391. (symtable = tloadnode(p).symtable);
  392. end;
  393. procedure Tloadnode.printnodedata(var t:text);
  394. begin
  395. inherited printnodedata(t);
  396. write(t,printnodeindention,'symbol = ',symtableentry.name);
  397. if symtableentry.typ=procsym then
  398. write(t,printnodeindention,'procdef = ',procdef.mangledname);
  399. writeln(t,'');
  400. end;
  401. {*****************************************************************************
  402. TASSIGNMENTNODE
  403. *****************************************************************************}
  404. constructor tassignmentnode.create(l,r : tnode);
  405. begin
  406. inherited create(assignn,l,r);
  407. l.mark_write;
  408. assigntype:=at_normal;
  409. end;
  410. constructor tassignmentnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  411. begin
  412. inherited ppuload(t,ppufile);
  413. assigntype:=tassigntype(ppufile.getbyte);
  414. end;
  415. procedure tassignmentnode.ppuwrite(ppufile:tcompilerppufile);
  416. begin
  417. inherited ppuwrite(ppufile);
  418. ppufile.putbyte(byte(assigntype));
  419. end;
  420. function tassignmentnode._getcopy : tnode;
  421. var
  422. n : tassignmentnode;
  423. begin
  424. n:=tassignmentnode(inherited _getcopy);
  425. n.assigntype:=assigntype;
  426. result:=n;
  427. end;
  428. function tassignmentnode.det_resulttype:tnode;
  429. var
  430. hp : tnode;
  431. useshelper : boolean;
  432. original_size : longint;
  433. begin
  434. result:=nil;
  435. resulttype:=voidtype;
  436. original_size := 0;
  437. { must be made unique }
  438. set_unique(left);
  439. resulttypepass(left);
  440. if is_ansistring(left.resulttype.def) then
  441. begin
  442. { fold <ansistring>:=<ansistring>+<char|shortstring|ansistring> }
  443. if (right.nodetype=addn) and
  444. left.isequal(tbinarynode(right).left) and
  445. { don't fold multiple concatenations else we could get trouble
  446. with multiple uses of s
  447. }
  448. (tbinarynode(right).left.nodetype<>addn) and
  449. (tbinarynode(right).right.nodetype<>addn) then
  450. begin
  451. { don't do a resulttypepass(right), since then the addnode }
  452. { may insert typeconversions that make this optimization }
  453. { opportunity quite difficult to detect (JM) }
  454. resulttypepass(tbinarynode(right).left);
  455. resulttypepass(tbinarynode(right).right);
  456. if (tbinarynode(right).right.nodetype=stringconstn) or
  457. is_char(tbinarynode(right).right.resulttype.def) or
  458. is_shortstring(tbinarynode(right).right.resulttype.def) or
  459. is_ansistring(tbinarynode(right).right.resulttype.def) then
  460. begin
  461. { remove property flag so it'll not trigger an error }
  462. exclude(left.flags,nf_isproperty);
  463. { generate call to helper }
  464. hp:=ccallparanode.create(tbinarynode(right).right,
  465. ccallparanode.create(left,nil));
  466. if is_char(tbinarynode(right).right.resulttype.def) then
  467. result:=ccallnode.createintern('fpc_'+Tstringdef(left.resulttype.def).stringtypname+'_append_char',hp)
  468. else if is_shortstring(tbinarynode(right).right.resulttype.def) then
  469. result:=ccallnode.createintern('fpc_'+Tstringdef(left.resulttype.def).stringtypname+'_append_shortstring',hp)
  470. else
  471. result:=ccallnode.createintern('fpc_'+Tstringdef(left.resulttype.def).stringtypname+'_append_ansistring',hp);
  472. tbinarynode(right).right:=nil;
  473. left:=nil;
  474. exit;
  475. end;
  476. end;
  477. end
  478. else
  479. if is_shortstring(left.resulttype.def) then
  480. begin
  481. { fold <shortstring>:=<shortstring>+<shortstring>,
  482. <shortstring>+<char> is handled by an optimized node }
  483. if (right.nodetype=addn) and
  484. left.isequal(tbinarynode(right).left) and
  485. { don't fold multiple concatenations else we could get trouble
  486. with multiple uses of s }
  487. (tbinarynode(right).left.nodetype<>addn) and
  488. (tbinarynode(right).right.nodetype<>addn) then
  489. begin
  490. { don't do a resulttypepass(right), since then the addnode }
  491. { may insert typeconversions that make this optimization }
  492. { opportunity quite difficult to detect (JM) }
  493. resulttypepass(tbinarynode(right).left);
  494. resulttypepass(tbinarynode(right).right);
  495. if is_shortstring(tbinarynode(right).right.resulttype.def) then
  496. begin
  497. { remove property flag so it'll not trigger an error }
  498. exclude(left.flags,nf_isproperty);
  499. { generate call to helper }
  500. hp:=ccallparanode.create(tbinarynode(right).right,
  501. ccallparanode.create(left,nil));
  502. if is_shortstring(tbinarynode(right).right.resulttype.def) then
  503. result:=ccallnode.createintern('fpc_shortstr_append_shortstr',hp);
  504. tbinarynode(right).right:=nil;
  505. left:=nil;
  506. exit;
  507. end;
  508. end;
  509. end;
  510. resulttypepass(right);
  511. set_varstate(left,vs_written,[]);
  512. set_varstate(right,vs_read,[vsf_must_be_valid]);
  513. if codegenerror then
  514. exit;
  515. { tp procvar support, when we don't expect a procvar
  516. then we need to call the procvar }
  517. if (left.resulttype.def.deftype<>procvardef) then
  518. maybe_call_procvar(right,true);
  519. { assignments to formaldefs and open arrays aren't allowed }
  520. if (left.resulttype.def.deftype=formaldef) or
  521. is_open_array(left.resulttype.def) then
  522. CGMessage(type_e_operator_not_allowed);
  523. { test if node can be assigned, properties are allowed }
  524. valid_for_assignment(left,true);
  525. { assigning nil to a dynamic array clears the array }
  526. if is_dynamic_array(left.resulttype.def) and
  527. (right.nodetype=niln) then
  528. begin
  529. hp:=ccallparanode.create(caddrnode.create_internal
  530. (crttinode.create(tstoreddef(left.resulttype.def),initrtti)),
  531. ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),nil));
  532. result := ccallnode.createintern('fpc_dynarray_clear',hp);
  533. left:=nil;
  534. exit;
  535. end;
  536. { shortstring helpers can do the conversion directly,
  537. so treat them separatly }
  538. if (is_shortstring(left.resulttype.def)) then
  539. begin
  540. { insert typeconv, except for chars that are handled in
  541. secondpass and except for ansi/wide string that can
  542. be converted immediatly }
  543. if not(is_char(right.resulttype.def) or
  544. (right.resulttype.def.deftype=stringdef)) then
  545. inserttypeconv(right,left.resulttype);
  546. if right.resulttype.def.deftype=stringdef then
  547. begin
  548. useshelper:=true;
  549. { convert constant strings to shortstrings. But
  550. skip empty constant strings, that will be handled
  551. in secondpass }
  552. if (right.nodetype=stringconstn) then
  553. begin
  554. { verify if range fits within shortstring }
  555. { just emit a warning, delphi gives an }
  556. { error, only if the type definition of }
  557. { of the string is less < 255 characters }
  558. if not is_open_string(left.resulttype.def) and
  559. (tstringconstnode(right).len > tstringdef(left.resulttype.def).len) then
  560. cgmessage(type_w_string_too_long);
  561. inserttypeconv(right,left.resulttype);
  562. if (tstringconstnode(right).len=0) then
  563. useshelper:=false;
  564. end;
  565. { rest is done in pass 1 (JM) }
  566. if useshelper then
  567. exit;
  568. end
  569. end
  570. else
  571. begin
  572. { get the size before the type conversion - check for all nodes }
  573. if assigned(right.resulttype.def) and
  574. (right.resulttype.def.deftype in [enumdef,orddef,floatdef]) and
  575. (right.nodetype in [loadn,vecn,calln]) then
  576. original_size := right.resulttype.def.size;
  577. inserttypeconv(right,left.resulttype);
  578. end;
  579. { check if the assignment may cause a range check error }
  580. { if its not explicit, and only if the values are }
  581. { ordinals, enumdef and floatdef }
  582. if (right.nodetype = typeconvn) and
  583. not (nf_explicit in ttypeconvnode(right).flags) then
  584. begin
  585. if assigned(left.resulttype.def) and
  586. (left.resulttype.def.deftype in [enumdef,orddef,floatdef]) and
  587. not is_boolean(left.resulttype.def) then
  588. begin
  589. if (original_size <> 0) and
  590. (left.resulttype.def.size < original_size) then
  591. begin
  592. if (cs_check_range in aktlocalswitches) then
  593. Message(type_w_smaller_possible_range_check)
  594. else
  595. Message(type_h_smaller_possible_range_check);
  596. end;
  597. end;
  598. end;
  599. { call helpers for interface }
  600. if is_interfacecom(left.resulttype.def) then
  601. begin
  602. hp:=ccallparanode.create(ctypeconvnode.create_internal
  603. (right,voidpointertype),
  604. ccallparanode.create(ctypeconvnode.create_internal
  605. (left,voidpointertype),nil));
  606. result:=ccallnode.createintern('fpc_intf_assign',hp);
  607. left:=nil;
  608. right:=nil;
  609. exit;
  610. end;
  611. { call helpers for variant, they can contain non ref. counted types like
  612. vararrays which must be really copied }
  613. if left.resulttype.def.deftype=variantdef then
  614. begin
  615. hp:=ccallparanode.create(ctypeconvnode.create_internal(
  616. caddrnode.create_internal(right),voidpointertype),
  617. ccallparanode.create(ctypeconvnode.create_internal(
  618. caddrnode.create_internal(left),voidpointertype),
  619. nil));
  620. result:=ccallnode.createintern('fpc_variant_copy',hp);
  621. left:=nil;
  622. right:=nil;
  623. exit;
  624. end;
  625. { check if local proc/func is assigned to procvar }
  626. if right.resulttype.def.deftype=procvardef then
  627. test_local_to_procvar(tprocvardef(right.resulttype.def),left.resulttype.def);
  628. end;
  629. function tassignmentnode.pass_1 : tnode;
  630. var
  631. hp: tnode;
  632. begin
  633. result:=nil;
  634. expectloc:=LOC_VOID;
  635. firstpass(left);
  636. firstpass(right);
  637. { assignment to refcounted variable -> inc/decref }
  638. if (not is_class(left.resulttype.def) and
  639. left.resulttype.def.needs_inittable) then
  640. include(current_procinfo.flags,pi_do_call);
  641. if codegenerror then
  642. exit;
  643. if (is_shortstring(left.resulttype.def)) then
  644. begin
  645. if right.resulttype.def.deftype=stringdef then
  646. begin
  647. if (right.nodetype<>stringconstn) or
  648. (tstringconstnode(right).len<>0) then
  649. begin
  650. if (cs_optimize in aktglobalswitches) and
  651. (right.nodetype in [calln,blockn]) and
  652. (left.nodetype = temprefn) and
  653. is_shortstring(right.resulttype.def) and
  654. not is_open_string(left.resulttype.def) and
  655. (tstringdef(left.resulttype.def).len = 255) then
  656. begin
  657. { the blocknode case is handled in pass_2 at the temp }
  658. { reference level (mainly for callparatemp) (JM) }
  659. if (right.nodetype = calln) then
  660. begin
  661. tcallnode(right).funcretnode := left;
  662. result := right;
  663. end
  664. else
  665. exit;
  666. end
  667. else
  668. begin
  669. hp:=ccallparanode.create
  670. (right,
  671. ccallparanode.create(cinlinenode.create
  672. (in_high_x,false,left.getcopy),nil));
  673. result:=ccallnode.createinternreturn('fpc_'+tstringdef(right.resulttype.def).stringtypname+'_to_shortstr',hp,left);
  674. firstpass(result);
  675. end;
  676. left:=nil;
  677. right:=nil;
  678. exit;
  679. end;
  680. end;
  681. end;
  682. if (cs_optimize in aktglobalswitches) and
  683. (right.nodetype = calln) and
  684. { left must be a temp, since otherwise as soon as you modify the }
  685. { result, the current left node is modified and that one may }
  686. { still be an argument to the function or even accessed in the }
  687. { function }
  688. (((left.nodetype = temprefn) and
  689. paramanager.ret_in_param(right.resulttype.def,
  690. tcallnode(right).procdefinition.proccalloption)) or
  691. { there's special support for ansi/widestrings in the callnode }
  692. is_ansistring(right.resulttype.def) or
  693. is_widestring(right.resulttype.def)) then
  694. begin
  695. tcallnode(right).funcretnode := left;
  696. result := right;
  697. left := nil;
  698. right := nil;
  699. exit;
  700. end;
  701. registersint:=left.registersint+right.registersint;
  702. registersfpu:=max(left.registersfpu,right.registersfpu);
  703. {$ifdef SUPPORT_MMX}
  704. registersmmx:=max(left.registersmmx,right.registersmmx);
  705. {$endif SUPPORT_MMX}
  706. end;
  707. function tassignmentnode.docompare(p: tnode): boolean;
  708. begin
  709. docompare :=
  710. inherited docompare(p) and
  711. (assigntype = tassignmentnode(p).assigntype);
  712. end;
  713. {$ifdef state_tracking}
  714. function Tassignmentnode.track_state_pass(exec_known:boolean):boolean;
  715. var se:Tstate_entry;
  716. begin
  717. track_state_pass:=false;
  718. if exec_known then
  719. begin
  720. track_state_pass:=right.track_state_pass(exec_known);
  721. {Force a new resulttype pass.}
  722. right.resulttype.def:=nil;
  723. do_resulttypepass(right);
  724. resulttypepass(right);
  725. aktstate.store_fact(left.getcopy,right.getcopy);
  726. end
  727. else
  728. aktstate.delete_fact(left);
  729. end;
  730. {$endif}
  731. {*****************************************************************************
  732. TARRAYCONSTRUCTORRANGENODE
  733. *****************************************************************************}
  734. constructor tarrayconstructorrangenode.create(l,r : tnode);
  735. begin
  736. inherited create(arrayconstructorrangen,l,r);
  737. end;
  738. function tarrayconstructorrangenode.det_resulttype:tnode;
  739. begin
  740. result:=nil;
  741. resulttypepass(left);
  742. resulttypepass(right);
  743. set_varstate(left,vs_read,[vsf_must_be_valid]);
  744. set_varstate(right,vs_read,[vsf_must_be_valid]);
  745. if codegenerror then
  746. exit;
  747. resulttype:=left.resulttype;
  748. end;
  749. function tarrayconstructorrangenode.pass_1 : tnode;
  750. begin
  751. firstpass(left);
  752. firstpass(right);
  753. expectloc:=LOC_CREFERENCE;
  754. calcregisters(self,0,0,0);
  755. result:=nil;
  756. end;
  757. {****************************************************************************
  758. TARRAYCONSTRUCTORNODE
  759. *****************************************************************************}
  760. constructor tarrayconstructornode.create(l,r : tnode);
  761. begin
  762. inherited create(arrayconstructorn,l,r);
  763. end;
  764. function tarrayconstructornode._getcopy : tnode;
  765. var
  766. n : tarrayconstructornode;
  767. begin
  768. n:=tarrayconstructornode(inherited _getcopy);
  769. result:=n;
  770. end;
  771. function tarrayconstructornode.det_resulttype:tnode;
  772. var
  773. htype : ttype;
  774. hp : tarrayconstructornode;
  775. len : longint;
  776. varia : boolean;
  777. begin
  778. result:=nil;
  779. { are we allowing array constructor? Then convert it to a set }
  780. if not allow_array_constructor then
  781. begin
  782. hp:=tarrayconstructornode(getcopy);
  783. arrayconstructor_to_set(tnode(hp));
  784. result:=hp;
  785. exit;
  786. end;
  787. { only pass left tree, right tree contains next construct if any }
  788. htype.reset;
  789. len:=0;
  790. varia:=false;
  791. if assigned(left) then
  792. begin
  793. hp:=self;
  794. while assigned(hp) do
  795. begin
  796. resulttypepass(hp.left);
  797. set_varstate(hp.left,vs_read,[vsf_must_be_valid]);
  798. if (htype.def=nil) then
  799. htype:=hp.left.resulttype
  800. else
  801. begin
  802. if ((nf_novariaallowed in flags) or (not varia)) and
  803. (not equal_defs(htype.def,hp.left.resulttype.def)) then
  804. begin
  805. varia:=true;
  806. end;
  807. end;
  808. inc(len);
  809. hp:=tarrayconstructornode(hp.right);
  810. end;
  811. end;
  812. { Set the type of empty or varia arrays to void. Also
  813. do this if the type is array of const/open array
  814. because those can't be used with setelementtype }
  815. if not assigned(htype.def) or
  816. varia or
  817. is_array_of_const(htype.def) or
  818. is_open_array(htype.def) then
  819. htype:=voidtype;
  820. resulttype.setdef(tarraydef.create(0,len-1,s32inttype));
  821. tarraydef(resulttype.def).setelementtype(htype);
  822. tarraydef(resulttype.def).IsConstructor:=true;
  823. tarraydef(resulttype.def).IsVariant:=varia;
  824. end;
  825. procedure tarrayconstructornode.force_type(tt:ttype);
  826. var
  827. hp : tarrayconstructornode;
  828. begin
  829. tarraydef(resulttype.def).setelementtype(tt);
  830. tarraydef(resulttype.def).IsConstructor:=true;
  831. tarraydef(resulttype.def).IsVariant:=false;
  832. if assigned(left) then
  833. begin
  834. hp:=self;
  835. while assigned(hp) do
  836. begin
  837. inserttypeconv(hp.left,tt);
  838. hp:=tarrayconstructornode(hp.right);
  839. end;
  840. end;
  841. end;
  842. procedure tarrayconstructornode.insert_typeconvs;
  843. var
  844. hp : tarrayconstructornode;
  845. dovariant : boolean;
  846. begin
  847. dovariant:=(nf_forcevaria in flags) or tarraydef(resulttype.def).isvariant;
  848. { only pass left tree, right tree contains next construct if any }
  849. if assigned(left) then
  850. begin
  851. hp:=self;
  852. while assigned(hp) do
  853. begin
  854. resulttypepass(hp.left);
  855. { Insert typeconvs for array of const }
  856. if dovariant then
  857. begin
  858. case hp.left.resulttype.def.deftype of
  859. enumdef :
  860. hp.left:=ctypeconvnode.create_internal(hp.left,s32inttype);
  861. arraydef :
  862. begin
  863. if is_chararray(hp.left.resulttype.def) then
  864. hp.left:=ctypeconvnode.create_internal(hp.left,charpointertype)
  865. else
  866. if is_widechararray(hp.left.resulttype.def) then
  867. hp.left:=ctypeconvnode.create_internal(hp.left,widecharpointertype)
  868. else
  869. CGMessagePos1(hp.left.fileinfo,type_e_wrong_type_in_array_constructor,hp.left.resulttype.def.typename);
  870. end;
  871. orddef :
  872. begin
  873. if is_integer(hp.left.resulttype.def) and
  874. not(is_64bitint(hp.left.resulttype.def)) then
  875. hp.left:=ctypeconvnode.create(hp.left,s32inttype);
  876. end;
  877. floatdef :
  878. if not(is_currency(hp.left.resulttype.def)) then
  879. hp.left:=ctypeconvnode.create(hp.left,pbestrealtype^);
  880. procvardef :
  881. hp.left:=ctypeconvnode.create(hp.left,voidpointertype);
  882. stringdef,
  883. variantdef,
  884. pointerdef,
  885. classrefdef:
  886. ;
  887. objectdef :
  888. if is_object(hp.left.resulttype.def) then
  889. CGMessagePos1(hp.left.fileinfo,type_e_wrong_type_in_array_constructor,hp.left.resulttype.def.typename);
  890. else
  891. CGMessagePos1(hp.left.fileinfo,type_e_wrong_type_in_array_constructor,hp.left.resulttype.def.typename);
  892. end;
  893. end;
  894. resulttypepass(hp.left);
  895. hp:=tarrayconstructornode(hp.right);
  896. end;
  897. end;
  898. end;
  899. function tarrayconstructornode.pass_1 : tnode;
  900. var
  901. hp : tarrayconstructornode;
  902. do_variant:boolean;
  903. begin
  904. do_variant:=(nf_forcevaria in flags) or tarraydef(resulttype.def).isvariant;
  905. result:=nil;
  906. { Insert required type convs, this must be
  907. done in pass 1, because the call must be
  908. resulttypepassed already }
  909. if assigned(left) then
  910. begin
  911. insert_typeconvs;
  912. { call firstpass for all nodes }
  913. hp:=self;
  914. while assigned(hp) do
  915. begin
  916. if hp.left<>nil then
  917. begin
  918. {This check is pessimistic; a call will happen depending
  919. on the location in which the elements will be found in
  920. pass 2.}
  921. if not do_variant then
  922. include(current_procinfo.flags,pi_do_call);
  923. firstpass(hp.left);
  924. end;
  925. hp:=tarrayconstructornode(hp.right);
  926. end;
  927. end;
  928. expectloc:=LOC_CREFERENCE;
  929. calcregisters(self,0,0,0);
  930. end;
  931. function tarrayconstructornode.docompare(p: tnode): boolean;
  932. begin
  933. docompare:=inherited docompare(p);
  934. end;
  935. {*****************************************************************************
  936. TTYPENODE
  937. *****************************************************************************}
  938. constructor ttypenode.create(t : ttype);
  939. begin
  940. inherited create(typen);
  941. restype:=t;
  942. allowed:=false;
  943. end;
  944. constructor ttypenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  945. begin
  946. inherited ppuload(t,ppufile);
  947. ppufile.gettype(restype);
  948. allowed:=boolean(ppufile.getbyte);
  949. end;
  950. procedure ttypenode.ppuwrite(ppufile:tcompilerppufile);
  951. begin
  952. inherited ppuwrite(ppufile);
  953. ppufile.puttype(restype);
  954. ppufile.putbyte(byte(allowed));
  955. end;
  956. procedure ttypenode.buildderefimpl;
  957. begin
  958. inherited buildderefimpl;
  959. restype.buildderef;
  960. end;
  961. procedure ttypenode.derefimpl;
  962. begin
  963. inherited derefimpl;
  964. restype.resolve;
  965. end;
  966. function ttypenode.det_resulttype:tnode;
  967. begin
  968. result:=nil;
  969. resulttype:=restype;
  970. { check if it's valid }
  971. if restype.def.deftype = errordef then
  972. CGMessage(parser_e_illegal_expression);
  973. end;
  974. function ttypenode.pass_1 : tnode;
  975. begin
  976. result:=nil;
  977. expectloc:=LOC_VOID;
  978. { a typenode can't generate code, so we give here
  979. an error. Else it'll be an abstract error in pass_2.
  980. Only when the allowed flag is set we don't generate
  981. an error }
  982. if not allowed then
  983. Message(parser_e_no_type_not_allowed_here);
  984. end;
  985. function ttypenode.docompare(p: tnode): boolean;
  986. begin
  987. docompare :=
  988. inherited docompare(p);
  989. end;
  990. {*****************************************************************************
  991. TRTTINODE
  992. *****************************************************************************}
  993. constructor trttinode.create(def:tstoreddef;rt:trttitype);
  994. begin
  995. inherited create(rttin);
  996. rttidef:=def;
  997. rttitype:=rt;
  998. end;
  999. constructor trttinode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1000. begin
  1001. inherited ppuload(t,ppufile);
  1002. ppufile.getderef(rttidefderef);
  1003. rttitype:=trttitype(ppufile.getbyte);
  1004. end;
  1005. procedure trttinode.ppuwrite(ppufile:tcompilerppufile);
  1006. begin
  1007. inherited ppuwrite(ppufile);
  1008. ppufile.putderef(rttidefderef);
  1009. ppufile.putbyte(byte(rttitype));
  1010. end;
  1011. procedure trttinode.buildderefimpl;
  1012. begin
  1013. inherited buildderefimpl;
  1014. rttidefderef.build(rttidef);
  1015. end;
  1016. procedure trttinode.derefimpl;
  1017. begin
  1018. inherited derefimpl;
  1019. rttidef:=tstoreddef(rttidefderef.resolve);
  1020. end;
  1021. function trttinode._getcopy : tnode;
  1022. var
  1023. n : trttinode;
  1024. begin
  1025. n:=trttinode(inherited _getcopy);
  1026. n.rttidef:=rttidef;
  1027. n.rttitype:=rttitype;
  1028. result:=n;
  1029. end;
  1030. function trttinode.det_resulttype:tnode;
  1031. begin
  1032. { rtti information will be returned as a void pointer }
  1033. result:=nil;
  1034. resulttype:=voidpointertype;
  1035. end;
  1036. function trttinode.pass_1 : tnode;
  1037. begin
  1038. result:=nil;
  1039. expectloc:=LOC_CREFERENCE;
  1040. end;
  1041. function trttinode.docompare(p: tnode): boolean;
  1042. begin
  1043. docompare :=
  1044. inherited docompare(p) and
  1045. (rttidef = trttinode(p).rttidef) and
  1046. (rttitype = trttinode(p).rttitype);
  1047. end;
  1048. begin
  1049. cloadnode:=tloadnode;
  1050. cassignmentnode:=tassignmentnode;
  1051. carrayconstructorrangenode:=tarrayconstructorrangenode;
  1052. carrayconstructornode:=tarrayconstructornode;
  1053. ctypenode:=ttypenode;
  1054. crttinode:=trttinode;
  1055. end.