ncon.pas 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  1. {
  2. Copyright (c) 2000-2002 by Florian Klaempfl
  3. Type checking and register allocation for constants
  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 ncon;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,widestr,
  22. node,
  23. aasmbase,aasmtai,cpuinfo,globals,
  24. symconst,symtype,symdef,symsym;
  25. type
  26. trealconstnode = class(tnode)
  27. restype : ttype;
  28. value_real : bestreal;
  29. lab_real : tasmlabel;
  30. constructor create(v : bestreal;const t:ttype);virtual;
  31. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  32. procedure ppuwrite(ppufile:tcompilerppufile);override;
  33. procedure buildderefimpl;override;
  34. procedure derefimpl;override;
  35. function _getcopy : tnode;override;
  36. function pass_1 : tnode;override;
  37. function det_resulttype:tnode;override;
  38. function docompare(p: tnode) : boolean; override;
  39. procedure printnodedata(var t:text);override;
  40. end;
  41. trealconstnodeclass = class of trealconstnode;
  42. tordconstnode = class(tnode)
  43. restype : ttype;
  44. value : TConstExprInt;
  45. rangecheck : boolean;
  46. { create an ordinal constant node of the specified type and value.
  47. _rangecheck determines if the value of the ordinal should be checked
  48. against the ranges of the type definition.
  49. }
  50. constructor create(v : tconstexprint;const t:ttype; _rangecheck : boolean);virtual;
  51. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  52. procedure ppuwrite(ppufile:tcompilerppufile);override;
  53. procedure buildderefimpl;override;
  54. procedure derefimpl;override;
  55. function _getcopy : tnode;override;
  56. function pass_1 : tnode;override;
  57. function det_resulttype:tnode;override;
  58. function docompare(p: tnode) : boolean; override;
  59. procedure printnodedata(var t:text);override;
  60. end;
  61. tordconstnodeclass = class of tordconstnode;
  62. tpointerconstnode = class(tnode)
  63. restype : ttype;
  64. value : TConstPtrUInt;
  65. constructor create(v : TConstPtrUInt;const t:ttype);virtual;
  66. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  67. procedure ppuwrite(ppufile:tcompilerppufile);override;
  68. procedure buildderefimpl;override;
  69. procedure derefimpl;override;
  70. function _getcopy : tnode;override;
  71. function pass_1 : tnode;override;
  72. function det_resulttype:tnode;override;
  73. function docompare(p: tnode) : boolean; override;
  74. end;
  75. tpointerconstnodeclass = class of tpointerconstnode;
  76. tstringconstnode = class(tnode)
  77. value_str : pchar;
  78. len : longint;
  79. lab_str : tasmlabel;
  80. st_type : tstringtype;
  81. constructor createstr(const s : string;st:tstringtype);virtual;
  82. constructor createpchar(s : pchar;l : longint);virtual;
  83. constructor createwstr(w : pcompilerwidestring);virtual;
  84. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  85. procedure ppuwrite(ppufile:tcompilerppufile);override;
  86. procedure buildderefimpl;override;
  87. procedure derefimpl;override;
  88. destructor destroy;override;
  89. function _getcopy : tnode;override;
  90. function pass_1 : tnode;override;
  91. function det_resulttype:tnode;override;
  92. function getpcharcopy : pchar;
  93. function docompare(p: tnode) : boolean; override;
  94. end;
  95. tstringconstnodeclass = class of tstringconstnode;
  96. tsetconstnode = class(tunarynode)
  97. restype : ttype;
  98. value_set : pconstset;
  99. lab_set : tasmlabel;
  100. constructor create(s : pconstset;const t:ttype);virtual;
  101. destructor destroy;override;
  102. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  103. procedure ppuwrite(ppufile:tcompilerppufile);override;
  104. procedure buildderefimpl;override;
  105. procedure derefimpl;override;
  106. function _getcopy : tnode;override;
  107. function pass_1 : tnode;override;
  108. function det_resulttype:tnode;override;
  109. function docompare(p: tnode) : boolean; override;
  110. end;
  111. tsetconstnodeclass = class of tsetconstnode;
  112. tnilnode = class(tnode)
  113. constructor create;virtual;
  114. function pass_1 : tnode;override;
  115. function det_resulttype:tnode;override;
  116. end;
  117. tnilnodeclass = class of tnilnode;
  118. tguidconstnode = class(tnode)
  119. value : tguid;
  120. constructor create(const g:tguid);virtual;
  121. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  122. procedure ppuwrite(ppufile:tcompilerppufile);override;
  123. function _getcopy : tnode;override;
  124. function pass_1 : tnode;override;
  125. function det_resulttype:tnode;override;
  126. function docompare(p: tnode) : boolean; override;
  127. end;
  128. tguidconstnodeclass = class of tguidconstnode;
  129. var
  130. crealconstnode : trealconstnodeclass;
  131. cordconstnode : tordconstnodeclass;
  132. cpointerconstnode : tpointerconstnodeclass;
  133. cstringconstnode : tstringconstnodeclass;
  134. csetconstnode : tsetconstnodeclass;
  135. cguidconstnode : tguidconstnodeclass;
  136. cnilnode : tnilnodeclass;
  137. function genintconstnode(v : TConstExprInt) : tordconstnode;
  138. function genenumnode(v : tenumsym) : tordconstnode;
  139. { some helper routines }
  140. function get_ordinal_value(p : tnode) : TConstExprInt;
  141. function is_constresourcestringnode(p : tnode) : boolean;
  142. function str_length(p : tnode) : longint;
  143. function is_emptyset(p : tnode):boolean;
  144. function genconstsymtree(p : tconstsym) : tnode;
  145. implementation
  146. uses
  147. cutils,
  148. verbose,systems,
  149. defutil,
  150. cpubase,cgbase,
  151. nld;
  152. function genintconstnode(v : TConstExprInt) : tordconstnode;
  153. var
  154. htype : ttype;
  155. begin
  156. int_to_type(v,htype);
  157. genintconstnode:=cordconstnode.create(v,htype,true);
  158. end;
  159. function genenumnode(v : tenumsym) : tordconstnode;
  160. var
  161. htype : ttype;
  162. begin
  163. htype.setdef(v.definition);
  164. genenumnode:=cordconstnode.create(v.value,htype,true);
  165. end;
  166. function get_ordinal_value(p : tnode) : TConstExprInt;
  167. begin
  168. get_ordinal_value:=0;
  169. if is_constnode(p) then
  170. begin
  171. if p.nodetype=ordconstn then
  172. get_ordinal_value:=tordconstnode(p).value
  173. else
  174. Message(type_e_ordinal_expr_expected);
  175. end
  176. else
  177. Message(type_e_constant_expr_expected);
  178. end;
  179. function is_constresourcestringnode(p : tnode) : boolean;
  180. begin
  181. is_constresourcestringnode:=(p.nodetype=loadn) and
  182. (tloadnode(p).symtableentry.typ=constsym) and
  183. (tconstsym(tloadnode(p).symtableentry).consttyp=constresourcestring);
  184. end;
  185. function str_length(p : tnode) : longint;
  186. begin
  187. str_length:=tstringconstnode(p).len;
  188. end;
  189. function is_emptyset(p : tnode):boolean;
  190. begin
  191. is_emptyset:=(p.nodetype=setconstn) and
  192. (Tsetconstnode(p).value_set^=[]);
  193. end;
  194. function genconstsymtree(p : tconstsym) : tnode;
  195. var
  196. p1 : tnode;
  197. len : longint;
  198. pc : pchar;
  199. begin
  200. p1:=nil;
  201. case p.consttyp of
  202. constord :
  203. p1:=cordconstnode.create(p.value.valueord,p.consttype,true);
  204. conststring :
  205. begin
  206. len:=p.value.len;
  207. if not(cs_ansistrings in aktlocalswitches) and (len>255) then
  208. len:=255;
  209. getmem(pc,len+1);
  210. move(pchar(p.value.valueptr)^,pc^,len);
  211. pc[len]:=#0;
  212. p1:=cstringconstnode.createpchar(pc,len);
  213. end;
  214. constreal :
  215. p1:=crealconstnode.create(pbestreal(p.value.valueptr)^,pbestrealtype^);
  216. constset :
  217. p1:=csetconstnode.create(pconstset(p.value.valueptr),p.consttype);
  218. constpointer :
  219. p1:=cpointerconstnode.create(p.value.valueordptr,p.consttype);
  220. constnil :
  221. p1:=cnilnode.create;
  222. else
  223. internalerror(200205103);
  224. end;
  225. genconstsymtree:=p1;
  226. end;
  227. {*****************************************************************************
  228. TREALCONSTNODE
  229. *****************************************************************************}
  230. { generic code }
  231. { overridden by: }
  232. { i386 }
  233. constructor trealconstnode.create(v : bestreal;const t:ttype);
  234. begin
  235. inherited create(realconstn);
  236. restype:=t;
  237. value_real:=v;
  238. lab_real:=nil;
  239. end;
  240. constructor trealconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  241. begin
  242. inherited ppuload(t,ppufile);
  243. ppufile.gettype(restype);
  244. value_real:=ppufile.getreal;
  245. lab_real:=tasmlabel(ppufile.getasmsymbol);
  246. end;
  247. procedure trealconstnode.ppuwrite(ppufile:tcompilerppufile);
  248. begin
  249. inherited ppuwrite(ppufile);
  250. ppufile.puttype(restype);
  251. ppufile.putreal(value_real);
  252. ppufile.putasmsymbol(lab_real);
  253. end;
  254. procedure trealconstnode.buildderefimpl;
  255. begin
  256. inherited buildderefimpl;
  257. restype.buildderef;
  258. end;
  259. procedure trealconstnode.derefimpl;
  260. begin
  261. inherited derefimpl;
  262. restype.resolve;
  263. objectlibrary.derefasmsymbol(tasmsymbol(lab_real));
  264. end;
  265. function trealconstnode._getcopy : tnode;
  266. var
  267. n : trealconstnode;
  268. begin
  269. n:=trealconstnode(inherited _getcopy);
  270. n.value_real:=value_real;
  271. n.lab_real:=lab_real;
  272. _getcopy:=n;
  273. end;
  274. function trealconstnode.det_resulttype:tnode;
  275. begin
  276. result:=nil;
  277. resulttype:=restype;
  278. end;
  279. function trealconstnode.pass_1 : tnode;
  280. begin
  281. result:=nil;
  282. expectloc:=LOC_CREFERENCE;
  283. { needs to be loaded into an FPU register }
  284. registersfpu:=1;
  285. end;
  286. function trealconstnode.docompare(p: tnode): boolean;
  287. begin
  288. docompare :=
  289. inherited docompare(p) and
  290. (value_real = trealconstnode(p).value_real) and
  291. { floating point compares for non-numbers give strange results usually }
  292. is_number_float(value_real) and
  293. is_number_float(trealconstnode(p).value_real);
  294. end;
  295. procedure Trealconstnode.printnodedata(var t:text);
  296. begin
  297. inherited printnodedata(t);
  298. writeln(t,printnodeindention,'value = ',value_real);
  299. end;
  300. {*****************************************************************************
  301. TORDCONSTNODE
  302. *****************************************************************************}
  303. constructor tordconstnode.create(v : tconstexprint;const t:ttype;_rangecheck : boolean);
  304. begin
  305. inherited create(ordconstn);
  306. value:=v;
  307. restype:=t;
  308. rangecheck := _rangecheck;
  309. end;
  310. constructor tordconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  311. begin
  312. inherited ppuload(t,ppufile);
  313. ppufile.gettype(restype);
  314. value:=ppufile.getexprint;
  315. { normally, the value is already compiled, so we don't need
  316. to do once again a range check
  317. }
  318. rangecheck := false;
  319. end;
  320. procedure tordconstnode.ppuwrite(ppufile:tcompilerppufile);
  321. begin
  322. inherited ppuwrite(ppufile);
  323. ppufile.puttype(restype);
  324. ppufile.putexprint(value);
  325. end;
  326. procedure tordconstnode.buildderefimpl;
  327. begin
  328. inherited buildderefimpl;
  329. restype.buildderef;
  330. end;
  331. procedure tordconstnode.derefimpl;
  332. begin
  333. inherited derefimpl;
  334. restype.resolve;
  335. end;
  336. function tordconstnode._getcopy : tnode;
  337. var
  338. n : tordconstnode;
  339. begin
  340. n:=tordconstnode(inherited _getcopy);
  341. n.value:=value;
  342. n.restype := restype;
  343. _getcopy:=n;
  344. end;
  345. function tordconstnode.det_resulttype:tnode;
  346. begin
  347. result:=nil;
  348. resulttype:=restype;
  349. { only do range checking when explicitly asked for it }
  350. if rangecheck then
  351. testrange(resulttype.def,value,false);
  352. end;
  353. function tordconstnode.pass_1 : tnode;
  354. begin
  355. result:=nil;
  356. expectloc:=LOC_CONSTANT;
  357. end;
  358. function tordconstnode.docompare(p: tnode): boolean;
  359. begin
  360. docompare :=
  361. inherited docompare(p) and
  362. (value = tordconstnode(p).value);
  363. end;
  364. procedure Tordconstnode.printnodedata(var t:text);
  365. begin
  366. inherited printnodedata(t);
  367. writeln(t,printnodeindention,'value = ',value);
  368. end;
  369. {*****************************************************************************
  370. TPOINTERCONSTNODE
  371. *****************************************************************************}
  372. constructor tpointerconstnode.create(v : TConstPtrUInt;const t:ttype);
  373. begin
  374. inherited create(pointerconstn);
  375. value:=v;
  376. restype:=t;
  377. end;
  378. constructor tpointerconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  379. begin
  380. inherited ppuload(t,ppufile);
  381. ppufile.gettype(restype);
  382. value:=ppufile.getptruint;
  383. end;
  384. procedure tpointerconstnode.ppuwrite(ppufile:tcompilerppufile);
  385. begin
  386. inherited ppuwrite(ppufile);
  387. ppufile.puttype(restype);
  388. ppufile.putptruint(value);
  389. end;
  390. procedure tpointerconstnode.buildderefimpl;
  391. begin
  392. inherited buildderefimpl;
  393. restype.buildderef;
  394. end;
  395. procedure tpointerconstnode.derefimpl;
  396. begin
  397. inherited derefimpl;
  398. restype.resolve;
  399. end;
  400. function tpointerconstnode._getcopy : tnode;
  401. var
  402. n : tpointerconstnode;
  403. begin
  404. n:=tpointerconstnode(inherited _getcopy);
  405. n.value:=value;
  406. n.restype := restype;
  407. _getcopy:=n;
  408. end;
  409. function tpointerconstnode.det_resulttype:tnode;
  410. begin
  411. result:=nil;
  412. resulttype:=restype;
  413. end;
  414. function tpointerconstnode.pass_1 : tnode;
  415. begin
  416. result:=nil;
  417. expectloc:=LOC_CONSTANT;
  418. end;
  419. function tpointerconstnode.docompare(p: tnode): boolean;
  420. begin
  421. docompare :=
  422. inherited docompare(p) and
  423. (value = tpointerconstnode(p).value);
  424. end;
  425. {*****************************************************************************
  426. TSTRINGCONSTNODE
  427. *****************************************************************************}
  428. constructor tstringconstnode.createstr(const s : string;st:tstringtype);
  429. var
  430. l : longint;
  431. begin
  432. inherited create(stringconstn);
  433. l:=length(s);
  434. len:=l;
  435. { stringdup write even past a #0 }
  436. getmem(value_str,l+1);
  437. move(s[1],value_str^,l);
  438. value_str[l]:=#0;
  439. lab_str:=nil;
  440. if st=st_default then
  441. begin
  442. if cs_ansistrings in aktlocalswitches then
  443. {$ifdef ansistring_bits}
  444. case aktansistring_bits of
  445. sb_16:
  446. st_type:=st_ansistring16;
  447. sb_32:
  448. st_type:=st_ansistring32;
  449. sb_64:
  450. st_type:=st_ansistring64;
  451. end
  452. {$else}
  453. st_type:=st_ansistring
  454. {$endif}
  455. else
  456. st_type:=st_shortstring;
  457. end
  458. else
  459. st_type:=st;
  460. end;
  461. constructor tstringconstnode.createwstr(w : pcompilerwidestring);
  462. begin
  463. inherited create(stringconstn);
  464. len:=getlengthwidestring(w);
  465. initwidestring(pcompilerwidestring(value_str));
  466. copywidestring(w,pcompilerwidestring(value_str));
  467. lab_str:=nil;
  468. st_type:=st_widestring;
  469. end;
  470. constructor tstringconstnode.createpchar(s : pchar;l : longint);
  471. begin
  472. inherited create(stringconstn);
  473. len:=l;
  474. value_str:=s;
  475. if (cs_ansistrings in aktlocalswitches) or
  476. (len>255) then
  477. {$ifdef ansistring_bits}
  478. case aktansistring_bits of
  479. sb_16:
  480. st_type:=st_ansistring16;
  481. sb_32:
  482. st_type:=st_ansistring32;
  483. sb_64:
  484. st_type:=st_ansistring64;
  485. end
  486. {$else}
  487. st_type:=st_ansistring
  488. {$endif}
  489. else
  490. st_type:=st_shortstring;
  491. lab_str:=nil;
  492. end;
  493. destructor tstringconstnode.destroy;
  494. begin
  495. if st_type=st_widestring then
  496. donewidestring(pcompilerwidestring(value_str))
  497. else
  498. ansistringdispose(value_str,len);
  499. inherited destroy;
  500. end;
  501. constructor tstringconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  502. var
  503. pw : pcompilerwidestring;
  504. begin
  505. inherited ppuload(t,ppufile);
  506. st_type:=tstringtype(ppufile.getbyte);
  507. len:=ppufile.getlongint;
  508. if st_type=st_widestring then
  509. begin
  510. initwidestring(pw);
  511. setlengthwidestring(pw,len);
  512. ppufile.getdata(pw^.data,pw^.len*sizeof(tcompilerwidechar));
  513. pcompilerwidestring(value_str):=pw
  514. end
  515. else
  516. begin
  517. getmem(value_str,len+1);
  518. ppufile.getdata(value_str^,len);
  519. value_str[len]:=#0;
  520. end;
  521. lab_str:=tasmlabel(ppufile.getasmsymbol);
  522. end;
  523. procedure tstringconstnode.ppuwrite(ppufile:tcompilerppufile);
  524. begin
  525. inherited ppuwrite(ppufile);
  526. ppufile.putbyte(byte(st_type));
  527. ppufile.putlongint(len);
  528. if st_type=st_widestring then
  529. ppufile.putdata(pcompilerwidestring(value_str)^.data,len*sizeof(tcompilerwidechar))
  530. else
  531. ppufile.putdata(value_str^,len);
  532. ppufile.putasmsymbol(lab_str);
  533. end;
  534. procedure tstringconstnode.buildderefimpl;
  535. begin
  536. inherited buildderefimpl;
  537. end;
  538. procedure tstringconstnode.derefimpl;
  539. begin
  540. inherited derefimpl;
  541. objectlibrary.derefasmsymbol(tasmsymbol(lab_str));
  542. end;
  543. function tstringconstnode._getcopy : tnode;
  544. var
  545. n : tstringconstnode;
  546. begin
  547. n:=tstringconstnode(inherited _getcopy);
  548. n.st_type:=st_type;
  549. n.len:=len;
  550. n.lab_str:=lab_str;
  551. if st_type=st_widestring then
  552. begin
  553. initwidestring(pcompilerwidestring(n.value_str));
  554. copywidestring(pcompilerwidestring(value_str),pcompilerwidestring(n.value_str));
  555. end
  556. else
  557. n.value_str:=getpcharcopy;
  558. _getcopy:=n;
  559. end;
  560. function tstringconstnode.det_resulttype:tnode;
  561. begin
  562. result:=nil;
  563. case st_type of
  564. st_shortstring :
  565. resulttype:=cshortstringtype;
  566. {$ifdef ansistring_bits}
  567. st_ansistring16:
  568. resulttype:=cansistringtype16;
  569. st_ansistring32:
  570. resulttype:=cansistringtype32;
  571. st_ansistring64:
  572. resulttype:=cansistringtype64;
  573. {$else}
  574. st_ansistring :
  575. resulttype:=cansistringtype;
  576. {$endif}
  577. st_widestring :
  578. resulttype:=cwidestringtype;
  579. st_longstring :
  580. resulttype:=clongstringtype;
  581. end;
  582. end;
  583. function tstringconstnode.pass_1 : tnode;
  584. begin
  585. result:=nil;
  586. {$ifdef ansistring_bits}
  587. if (st_type in [st_ansistring16,st_ansistring32,st_ansistring64,st_widestring]) and
  588. {$else}
  589. if (st_type in [st_ansistring,st_widestring]) and
  590. {$endif}
  591. (len=0) then
  592. expectloc:=LOC_CONSTANT
  593. else
  594. expectloc:=LOC_CREFERENCE;
  595. end;
  596. function tstringconstnode.getpcharcopy : pchar;
  597. var
  598. pc : pchar;
  599. begin
  600. pc:=nil;
  601. getmem(pc,len+1);
  602. if pc=nil then
  603. Message(general_f_no_memory_left);
  604. move(value_str^,pc^,len+1);
  605. getpcharcopy:=pc;
  606. end;
  607. function tstringconstnode.docompare(p: tnode): boolean;
  608. begin
  609. docompare :=
  610. inherited docompare(p) and
  611. (len = tstringconstnode(p).len) and
  612. { Don't compare the pchars, since they may contain null chars }
  613. { Since all equal constant strings are replaced by the same }
  614. { label, the following compare should be enough (JM) }
  615. (lab_str = tstringconstnode(p).lab_str);
  616. end;
  617. {*****************************************************************************
  618. TSETCONSTNODE
  619. *****************************************************************************}
  620. constructor tsetconstnode.create(s : pconstset;const t:ttype);
  621. begin
  622. inherited create(setconstn,nil);
  623. restype:=t;
  624. if assigned(s) then
  625. begin
  626. new(value_set);
  627. value_set^:=s^;
  628. end
  629. else
  630. value_set:=nil;
  631. end;
  632. destructor tsetconstnode.destroy;
  633. begin
  634. if assigned(value_set) then
  635. dispose(value_set);
  636. inherited destroy;
  637. end;
  638. constructor tsetconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  639. begin
  640. inherited ppuload(t,ppufile);
  641. ppufile.gettype(restype);
  642. new(value_set);
  643. ppufile.getdata(value_set^,sizeof(tconstset));
  644. end;
  645. procedure tsetconstnode.ppuwrite(ppufile:tcompilerppufile);
  646. begin
  647. inherited ppuwrite(ppufile);
  648. ppufile.puttype(restype);
  649. ppufile.putdata(value_set^,sizeof(tconstset));
  650. end;
  651. procedure tsetconstnode.buildderefimpl;
  652. begin
  653. inherited buildderefimpl;
  654. restype.buildderef;
  655. end;
  656. procedure tsetconstnode.derefimpl;
  657. begin
  658. inherited derefimpl;
  659. restype.resolve;
  660. end;
  661. function tsetconstnode._getcopy : tnode;
  662. var
  663. n : tsetconstnode;
  664. begin
  665. n:=tsetconstnode(inherited _getcopy);
  666. if assigned(value_set) then
  667. begin
  668. new(n.value_set);
  669. n.value_set^:=value_set^
  670. end
  671. else
  672. n.value_set:=nil;
  673. n.restype := restype;
  674. n.lab_set:=lab_set;
  675. _getcopy:=n;
  676. end;
  677. function tsetconstnode.det_resulttype:tnode;
  678. begin
  679. result:=nil;
  680. resulttype:=restype;
  681. end;
  682. function tsetconstnode.pass_1 : tnode;
  683. begin
  684. result:=nil;
  685. if tsetdef(resulttype.def).settype=smallset then
  686. expectloc:=LOC_CONSTANT
  687. else
  688. expectloc:=LOC_CREFERENCE;
  689. end;
  690. function tsetconstnode.docompare(p: tnode): boolean;
  691. begin
  692. docompare:=(inherited docompare(p)) and
  693. (value_set^=Tsetconstnode(p).value_set^);
  694. end;
  695. {*****************************************************************************
  696. TNILNODE
  697. *****************************************************************************}
  698. constructor tnilnode.create;
  699. begin
  700. inherited create(niln);
  701. end;
  702. function tnilnode.det_resulttype:tnode;
  703. begin
  704. result:=nil;
  705. resulttype:=voidpointertype;
  706. end;
  707. function tnilnode.pass_1 : tnode;
  708. begin
  709. result:=nil;
  710. expectloc:=LOC_CONSTANT;
  711. end;
  712. {*****************************************************************************
  713. TGUIDCONSTNODE
  714. *****************************************************************************}
  715. constructor tguidconstnode.create(const g:tguid);
  716. begin
  717. inherited create(guidconstn);
  718. value:=g;
  719. end;
  720. constructor tguidconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  721. begin
  722. inherited ppuload(t,ppufile);
  723. ppufile.getguid(value);
  724. end;
  725. procedure tguidconstnode.ppuwrite(ppufile:tcompilerppufile);
  726. begin
  727. inherited ppuwrite(ppufile);
  728. ppufile.putguid(value);
  729. end;
  730. function tguidconstnode._getcopy : tnode;
  731. var
  732. n : tguidconstnode;
  733. begin
  734. n:=tguidconstnode(inherited _getcopy);
  735. n.value:=value;
  736. _getcopy:=n;
  737. end;
  738. function tguidconstnode.det_resulttype:tnode;
  739. begin
  740. result:=nil;
  741. resulttype.setdef(rec_tguid);
  742. end;
  743. function tguidconstnode.pass_1 : tnode;
  744. begin
  745. result:=nil;
  746. expectloc:=LOC_CREFERENCE;
  747. end;
  748. function tguidconstnode.docompare(p: tnode): boolean;
  749. begin
  750. docompare :=
  751. inherited docompare(p) and
  752. (guid2string(value) = guid2string(tguidconstnode(p).value));
  753. end;
  754. begin
  755. crealconstnode:=trealconstnode;
  756. cordconstnode:=tordconstnode;
  757. cpointerconstnode:=tpointerconstnode;
  758. cstringconstnode:=tstringconstnode;
  759. csetconstnode:=tsetconstnode;
  760. cnilnode:=tnilnode;
  761. cguidconstnode:=tguidconstnode;
  762. end.