ncon.pas 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  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. var
  511. pw : pcompilerwidestring;
  512. begin
  513. inherited ppuload(t,ppufile);
  514. st_type:=tstringtype(ppufile.getbyte);
  515. len:=ppufile.getlongint;
  516. if st_type=st_widestring then
  517. begin
  518. initwidestring(pw);
  519. setlengthwidestring(pw,len);
  520. ppufile.getdata(pw^.data,pw^.len*sizeof(tcompilerwidechar));
  521. pcompilerwidestring(value_str):=pw
  522. end
  523. else
  524. begin
  525. getmem(value_str,len+1);
  526. ppufile.getdata(value_str^,len);
  527. value_str[len]:=#0;
  528. end;
  529. lab_str:=tasmlabel(ppufile.getasmsymbol);
  530. end;
  531. procedure tstringconstnode.ppuwrite(ppufile:tcompilerppufile);
  532. begin
  533. inherited ppuwrite(ppufile);
  534. ppufile.putbyte(byte(st_type));
  535. ppufile.putlongint(len);
  536. if st_type=st_widestring then
  537. ppufile.putdata(pcompilerwidestring(value_str)^.data,len*sizeof(tcompilerwidechar))
  538. else
  539. ppufile.putdata(value_str^,len);
  540. ppufile.putasmsymbol(lab_str);
  541. end;
  542. procedure tstringconstnode.buildderefimpl;
  543. begin
  544. inherited buildderefimpl;
  545. end;
  546. procedure tstringconstnode.derefimpl;
  547. begin
  548. inherited derefimpl;
  549. objectlibrary.derefasmsymbol(tasmsymbol(lab_str));
  550. end;
  551. function tstringconstnode._getcopy : tnode;
  552. var
  553. n : tstringconstnode;
  554. begin
  555. n:=tstringconstnode(inherited _getcopy);
  556. n.st_type:=st_type;
  557. n.len:=len;
  558. n.lab_str:=lab_str;
  559. if st_type=st_widestring then
  560. begin
  561. initwidestring(pcompilerwidestring(n.value_str));
  562. copywidestring(pcompilerwidestring(value_str),pcompilerwidestring(n.value_str));
  563. end
  564. else
  565. n.value_str:=getpcharcopy;
  566. _getcopy:=n;
  567. end;
  568. function tstringconstnode.det_resulttype:tnode;
  569. begin
  570. result:=nil;
  571. case st_type of
  572. st_shortstring :
  573. resulttype:=cshortstringtype;
  574. {$ifdef ansistring_bits}
  575. st_ansistring16:
  576. resulttype:=cansistringtype16;
  577. st_ansistring32:
  578. resulttype:=cansistringtype32;
  579. st_ansistring64:
  580. resulttype:=cansistringtype64;
  581. {$else}
  582. st_ansistring :
  583. resulttype:=cansistringtype;
  584. {$endif}
  585. st_widestring :
  586. resulttype:=cwidestringtype;
  587. st_longstring :
  588. resulttype:=clongstringtype;
  589. end;
  590. end;
  591. function tstringconstnode.pass_1 : tnode;
  592. begin
  593. result:=nil;
  594. {$ifdef ansistring_bits}
  595. if (st_type in [st_ansistring16,st_ansistring32,st_ansistring64,st_widestring]) and
  596. {$else}
  597. if (st_type in [st_ansistring,st_widestring]) and
  598. {$endif}
  599. (len=0) then
  600. expectloc:=LOC_CONSTANT
  601. else
  602. expectloc:=LOC_CREFERENCE;
  603. end;
  604. function tstringconstnode.getpcharcopy : pchar;
  605. var
  606. pc : pchar;
  607. begin
  608. pc:=nil;
  609. getmem(pc,len+1);
  610. if pc=nil then
  611. Message(general_f_no_memory_left);
  612. move(value_str^,pc^,len+1);
  613. getpcharcopy:=pc;
  614. end;
  615. function tstringconstnode.docompare(p: tnode): boolean;
  616. begin
  617. docompare :=
  618. inherited docompare(p) and
  619. (len = tstringconstnode(p).len) and
  620. { Don't compare the pchars, since they may contain null chars }
  621. { Since all equal constant strings are replaced by the same }
  622. { label, the following compare should be enough (JM) }
  623. (lab_str = tstringconstnode(p).lab_str);
  624. end;
  625. {*****************************************************************************
  626. TSETCONSTNODE
  627. *****************************************************************************}
  628. constructor tsetconstnode.create(s : pconstset;const t:ttype);
  629. begin
  630. inherited create(setconstn,nil);
  631. restype:=t;
  632. if assigned(s) then
  633. begin
  634. new(value_set);
  635. value_set^:=s^;
  636. end
  637. else
  638. value_set:=nil;
  639. end;
  640. destructor tsetconstnode.destroy;
  641. begin
  642. if assigned(value_set) then
  643. dispose(value_set);
  644. inherited destroy;
  645. end;
  646. constructor tsetconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  647. begin
  648. inherited ppuload(t,ppufile);
  649. ppufile.gettype(restype);
  650. new(value_set);
  651. ppufile.getdata(value_set^,sizeof(tconstset));
  652. end;
  653. procedure tsetconstnode.ppuwrite(ppufile:tcompilerppufile);
  654. begin
  655. inherited ppuwrite(ppufile);
  656. ppufile.puttype(restype);
  657. ppufile.putdata(value_set^,sizeof(tconstset));
  658. end;
  659. procedure tsetconstnode.buildderefimpl;
  660. begin
  661. inherited buildderefimpl;
  662. restype.buildderef;
  663. end;
  664. procedure tsetconstnode.derefimpl;
  665. begin
  666. inherited derefimpl;
  667. restype.resolve;
  668. end;
  669. function tsetconstnode._getcopy : tnode;
  670. var
  671. n : tsetconstnode;
  672. begin
  673. n:=tsetconstnode(inherited _getcopy);
  674. if assigned(value_set) then
  675. begin
  676. new(n.value_set);
  677. n.value_set^:=value_set^
  678. end
  679. else
  680. n.value_set:=nil;
  681. n.restype := restype;
  682. n.lab_set:=lab_set;
  683. _getcopy:=n;
  684. end;
  685. function tsetconstnode.det_resulttype:tnode;
  686. begin
  687. result:=nil;
  688. resulttype:=restype;
  689. end;
  690. function tsetconstnode.pass_1 : tnode;
  691. begin
  692. result:=nil;
  693. if tsetdef(resulttype.def).settype=smallset then
  694. expectloc:=LOC_CONSTANT
  695. else
  696. expectloc:=LOC_CREFERENCE;
  697. end;
  698. function tsetconstnode.docompare(p: tnode): boolean;
  699. begin
  700. docompare:=(inherited docompare(p)) and
  701. (value_set^=Tsetconstnode(p).value_set^);
  702. end;
  703. {*****************************************************************************
  704. TNILNODE
  705. *****************************************************************************}
  706. constructor tnilnode.create;
  707. begin
  708. inherited create(niln);
  709. end;
  710. function tnilnode.det_resulttype:tnode;
  711. begin
  712. result:=nil;
  713. resulttype:=voidpointertype;
  714. end;
  715. function tnilnode.pass_1 : tnode;
  716. begin
  717. result:=nil;
  718. expectloc:=LOC_CONSTANT;
  719. end;
  720. {*****************************************************************************
  721. TGUIDCONSTNODE
  722. *****************************************************************************}
  723. constructor tguidconstnode.create(const g:tguid);
  724. begin
  725. inherited create(guidconstn);
  726. value:=g;
  727. end;
  728. constructor tguidconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  729. begin
  730. inherited ppuload(t,ppufile);
  731. ppufile.getguid(value);
  732. end;
  733. procedure tguidconstnode.ppuwrite(ppufile:tcompilerppufile);
  734. begin
  735. inherited ppuwrite(ppufile);
  736. ppufile.putguid(value);
  737. end;
  738. function tguidconstnode._getcopy : tnode;
  739. var
  740. n : tguidconstnode;
  741. begin
  742. n:=tguidconstnode(inherited _getcopy);
  743. n.value:=value;
  744. _getcopy:=n;
  745. end;
  746. function tguidconstnode.det_resulttype:tnode;
  747. begin
  748. result:=nil;
  749. resulttype.setdef(rec_tguid);
  750. end;
  751. function tguidconstnode.pass_1 : tnode;
  752. begin
  753. result:=nil;
  754. expectloc:=LOC_CREFERENCE;
  755. end;
  756. function tguidconstnode.docompare(p: tnode): boolean;
  757. begin
  758. docompare :=
  759. inherited docompare(p) and
  760. (guid2string(value) = guid2string(tguidconstnode(p).value));
  761. end;
  762. begin
  763. crealconstnode:=trealconstnode;
  764. cordconstnode:=tordconstnode;
  765. cpointerconstnode:=tpointerconstnode;
  766. cstringconstnode:=tstringconstnode;
  767. csetconstnode:=tsetconstnode;
  768. cnilnode:=tnilnode;
  769. cguidconstnode:=tguidconstnode;
  770. end.