ncon.pas 26 KB

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