ncon.pas 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  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. tconststringtype = (
  77. cst_conststring,
  78. cst_shortstring,
  79. cst_longstring,
  80. cst_ansistring,
  81. cst_widestring
  82. );
  83. tstringconstnode = class(tnode)
  84. value_str : pchar;
  85. len : longint;
  86. lab_str : tasmlabel;
  87. cst_type : tconststringtype;
  88. constructor createstr(const s : string);virtual;
  89. constructor createpchar(s : pchar;l : longint);virtual;
  90. constructor createwstr(w : pcompilerwidestring);virtual;
  91. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  92. procedure ppuwrite(ppufile:tcompilerppufile);override;
  93. procedure buildderefimpl;override;
  94. procedure derefimpl;override;
  95. destructor destroy;override;
  96. function _getcopy : tnode;override;
  97. function pass_1 : tnode;override;
  98. function det_resulttype:tnode;override;
  99. function getpcharcopy : pchar;
  100. function docompare(p: tnode) : boolean; override;
  101. procedure changestringtype(const newtype:ttype);
  102. end;
  103. tstringconstnodeclass = class of tstringconstnode;
  104. tsetconstnode = class(tunarynode)
  105. restype : ttype;
  106. value_set : pconstset;
  107. lab_set : tasmlabel;
  108. constructor create(s : pconstset;const t:ttype);virtual;
  109. destructor destroy;override;
  110. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  111. procedure ppuwrite(ppufile:tcompilerppufile);override;
  112. procedure buildderefimpl;override;
  113. procedure derefimpl;override;
  114. function _getcopy : tnode;override;
  115. function pass_1 : tnode;override;
  116. function det_resulttype:tnode;override;
  117. function docompare(p: tnode) : boolean; override;
  118. end;
  119. tsetconstnodeclass = class of tsetconstnode;
  120. tnilnode = class(tnode)
  121. constructor create;virtual;
  122. function pass_1 : tnode;override;
  123. function det_resulttype:tnode;override;
  124. end;
  125. tnilnodeclass = class of tnilnode;
  126. tguidconstnode = class(tnode)
  127. value : tguid;
  128. constructor create(const g:tguid);virtual;
  129. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  130. procedure ppuwrite(ppufile:tcompilerppufile);override;
  131. function _getcopy : tnode;override;
  132. function pass_1 : tnode;override;
  133. function det_resulttype:tnode;override;
  134. function docompare(p: tnode) : boolean; override;
  135. end;
  136. tguidconstnodeclass = class of tguidconstnode;
  137. var
  138. crealconstnode : trealconstnodeclass;
  139. cordconstnode : tordconstnodeclass;
  140. cpointerconstnode : tpointerconstnodeclass;
  141. cstringconstnode : tstringconstnodeclass;
  142. csetconstnode : tsetconstnodeclass;
  143. cguidconstnode : tguidconstnodeclass;
  144. cnilnode : tnilnodeclass;
  145. function genintconstnode(v : TConstExprInt) : tordconstnode;
  146. function genenumnode(v : tenumsym) : tordconstnode;
  147. { some helper routines }
  148. function get_ordinal_value(p : tnode) : TConstExprInt;
  149. function is_constresourcestringnode(p : tnode) : boolean;
  150. function is_emptyset(p : tnode):boolean;
  151. function genconstsymtree(p : tconstsym) : tnode;
  152. implementation
  153. uses
  154. cutils,
  155. verbose,systems,
  156. defutil,
  157. cpubase,cgbase,
  158. nld;
  159. function genintconstnode(v : TConstExprInt) : tordconstnode;
  160. var
  161. htype : ttype;
  162. begin
  163. int_to_type(v,htype);
  164. genintconstnode:=cordconstnode.create(v,htype,true);
  165. end;
  166. function genenumnode(v : tenumsym) : tordconstnode;
  167. var
  168. htype : ttype;
  169. begin
  170. htype.setdef(v.definition);
  171. genenumnode:=cordconstnode.create(v.value,htype,true);
  172. end;
  173. function get_ordinal_value(p : tnode) : TConstExprInt;
  174. begin
  175. get_ordinal_value:=0;
  176. if is_constnode(p) then
  177. begin
  178. if p.nodetype=ordconstn then
  179. get_ordinal_value:=tordconstnode(p).value
  180. else
  181. Message(type_e_ordinal_expr_expected);
  182. end
  183. else
  184. Message(type_e_constant_expr_expected);
  185. end;
  186. function is_constresourcestringnode(p : tnode) : boolean;
  187. begin
  188. is_constresourcestringnode:=(p.nodetype=loadn) and
  189. (tloadnode(p).symtableentry.typ=constsym) and
  190. (tconstsym(tloadnode(p).symtableentry).consttyp=constresourcestring);
  191. end;
  192. function is_emptyset(p : tnode):boolean;
  193. begin
  194. is_emptyset:=(p.nodetype=setconstn) and
  195. (Tsetconstnode(p).value_set^=[]);
  196. end;
  197. function genconstsymtree(p : tconstsym) : tnode;
  198. var
  199. p1 : tnode;
  200. len : longint;
  201. pc : pchar;
  202. begin
  203. p1:=nil;
  204. case p.consttyp of
  205. constord :
  206. p1:=cordconstnode.create(p.value.valueord,p.consttype,true);
  207. conststring :
  208. begin
  209. len:=p.value.len;
  210. getmem(pc,len+1);
  211. move(pchar(p.value.valueptr)^,pc^,len);
  212. pc[len]:=#0;
  213. p1:=cstringconstnode.createpchar(pc,len);
  214. end;
  215. constreal :
  216. p1:=crealconstnode.create(pbestreal(p.value.valueptr)^,pbestrealtype^);
  217. constset :
  218. p1:=csetconstnode.create(pconstset(p.value.valueptr),p.consttype);
  219. constpointer :
  220. p1:=cpointerconstnode.create(p.value.valueordptr,p.consttype);
  221. constnil :
  222. p1:=cnilnode.create;
  223. else
  224. internalerror(200205103);
  225. end;
  226. genconstsymtree:=p1;
  227. end;
  228. {*****************************************************************************
  229. TREALCONSTNODE
  230. *****************************************************************************}
  231. { generic code }
  232. { overridden by: }
  233. { i386 }
  234. constructor trealconstnode.create(v : bestreal;const t:ttype);
  235. begin
  236. inherited create(realconstn);
  237. restype:=t;
  238. value_real:=v;
  239. lab_real:=nil;
  240. end;
  241. constructor trealconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  242. begin
  243. inherited ppuload(t,ppufile);
  244. ppufile.gettype(restype);
  245. value_real:=ppufile.getreal;
  246. lab_real:=tasmlabel(ppufile.getasmsymbol);
  247. end;
  248. procedure trealconstnode.ppuwrite(ppufile:tcompilerppufile);
  249. begin
  250. inherited ppuwrite(ppufile);
  251. ppufile.puttype(restype);
  252. ppufile.putreal(value_real);
  253. ppufile.putasmsymbol(lab_real);
  254. end;
  255. procedure trealconstnode.buildderefimpl;
  256. begin
  257. inherited buildderefimpl;
  258. restype.buildderef;
  259. end;
  260. procedure trealconstnode.derefimpl;
  261. begin
  262. inherited derefimpl;
  263. restype.resolve;
  264. objectlibrary.derefasmsymbol(tasmsymbol(lab_real));
  265. end;
  266. function trealconstnode._getcopy : tnode;
  267. var
  268. n : trealconstnode;
  269. begin
  270. n:=trealconstnode(inherited _getcopy);
  271. n.value_real:=value_real;
  272. n.lab_real:=lab_real;
  273. _getcopy:=n;
  274. end;
  275. function trealconstnode.det_resulttype:tnode;
  276. begin
  277. result:=nil;
  278. resulttype:=restype;
  279. end;
  280. function trealconstnode.pass_1 : tnode;
  281. begin
  282. result:=nil;
  283. expectloc:=LOC_CREFERENCE;
  284. { needs to be loaded into an FPU register }
  285. registersfpu:=1;
  286. end;
  287. function trealconstnode.docompare(p: tnode): boolean;
  288. begin
  289. docompare :=
  290. inherited docompare(p) and
  291. (value_real = trealconstnode(p).value_real) and
  292. { floating point compares for non-numbers give strange results usually }
  293. is_number_float(value_real) and
  294. is_number_float(trealconstnode(p).value_real);
  295. end;
  296. procedure Trealconstnode.printnodedata(var t:text);
  297. begin
  298. inherited printnodedata(t);
  299. writeln(t,printnodeindention,'value = ',value_real);
  300. end;
  301. {*****************************************************************************
  302. TORDCONSTNODE
  303. *****************************************************************************}
  304. constructor tordconstnode.create(v : tconstexprint;const t:ttype;_rangecheck : boolean);
  305. begin
  306. inherited create(ordconstn);
  307. value:=v;
  308. restype:=t;
  309. rangecheck := _rangecheck;
  310. end;
  311. constructor tordconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  312. begin
  313. inherited ppuload(t,ppufile);
  314. ppufile.gettype(restype);
  315. value:=ppufile.getexprint;
  316. { normally, the value is already compiled, so we don't need
  317. to do once again a range check
  318. }
  319. rangecheck := false;
  320. end;
  321. procedure tordconstnode.ppuwrite(ppufile:tcompilerppufile);
  322. begin
  323. inherited ppuwrite(ppufile);
  324. ppufile.puttype(restype);
  325. ppufile.putexprint(value);
  326. end;
  327. procedure tordconstnode.buildderefimpl;
  328. begin
  329. inherited buildderefimpl;
  330. restype.buildderef;
  331. end;
  332. procedure tordconstnode.derefimpl;
  333. begin
  334. inherited derefimpl;
  335. restype.resolve;
  336. end;
  337. function tordconstnode._getcopy : tnode;
  338. var
  339. n : tordconstnode;
  340. begin
  341. n:=tordconstnode(inherited _getcopy);
  342. n.value:=value;
  343. n.restype := restype;
  344. _getcopy:=n;
  345. end;
  346. function tordconstnode.det_resulttype:tnode;
  347. begin
  348. result:=nil;
  349. resulttype:=restype;
  350. { only do range checking when explicitly asked for it }
  351. if rangecheck then
  352. testrange(resulttype.def,value,false);
  353. end;
  354. function tordconstnode.pass_1 : tnode;
  355. begin
  356. result:=nil;
  357. expectloc:=LOC_CONSTANT;
  358. end;
  359. function tordconstnode.docompare(p: tnode): boolean;
  360. begin
  361. docompare :=
  362. inherited docompare(p) and
  363. (value = tordconstnode(p).value);
  364. end;
  365. procedure Tordconstnode.printnodedata(var t:text);
  366. begin
  367. inherited printnodedata(t);
  368. writeln(t,printnodeindention,'value = ',value);
  369. end;
  370. {*****************************************************************************
  371. TPOINTERCONSTNODE
  372. *****************************************************************************}
  373. constructor tpointerconstnode.create(v : TConstPtrUInt;const t:ttype);
  374. begin
  375. inherited create(pointerconstn);
  376. value:=v;
  377. restype:=t;
  378. end;
  379. constructor tpointerconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  380. begin
  381. inherited ppuload(t,ppufile);
  382. ppufile.gettype(restype);
  383. value:=ppufile.getptruint;
  384. end;
  385. procedure tpointerconstnode.ppuwrite(ppufile:tcompilerppufile);
  386. begin
  387. inherited ppuwrite(ppufile);
  388. ppufile.puttype(restype);
  389. ppufile.putptruint(value);
  390. end;
  391. procedure tpointerconstnode.buildderefimpl;
  392. begin
  393. inherited buildderefimpl;
  394. restype.buildderef;
  395. end;
  396. procedure tpointerconstnode.derefimpl;
  397. begin
  398. inherited derefimpl;
  399. restype.resolve;
  400. end;
  401. function tpointerconstnode._getcopy : tnode;
  402. var
  403. n : tpointerconstnode;
  404. begin
  405. n:=tpointerconstnode(inherited _getcopy);
  406. n.value:=value;
  407. n.restype := restype;
  408. _getcopy:=n;
  409. end;
  410. function tpointerconstnode.det_resulttype:tnode;
  411. begin
  412. result:=nil;
  413. resulttype:=restype;
  414. end;
  415. function tpointerconstnode.pass_1 : tnode;
  416. begin
  417. result:=nil;
  418. expectloc:=LOC_CONSTANT;
  419. end;
  420. function tpointerconstnode.docompare(p: tnode): boolean;
  421. begin
  422. docompare :=
  423. inherited docompare(p) and
  424. (value = tpointerconstnode(p).value);
  425. end;
  426. {*****************************************************************************
  427. TSTRINGCONSTNODE
  428. *****************************************************************************}
  429. constructor tstringconstnode.createstr(const s : string);
  430. var
  431. l : longint;
  432. begin
  433. inherited create(stringconstn);
  434. l:=length(s);
  435. len:=l;
  436. { stringdup write even past a #0 }
  437. getmem(value_str,l+1);
  438. move(s[1],value_str^,l);
  439. value_str[l]:=#0;
  440. lab_str:=nil;
  441. cst_type:=cst_conststring;
  442. end;
  443. constructor tstringconstnode.createwstr(w : pcompilerwidestring);
  444. begin
  445. inherited create(stringconstn);
  446. len:=getlengthwidestring(w);
  447. initwidestring(pcompilerwidestring(value_str));
  448. copywidestring(w,pcompilerwidestring(value_str));
  449. lab_str:=nil;
  450. cst_type:=cst_widestring;
  451. end;
  452. constructor tstringconstnode.createpchar(s : pchar;l : longint);
  453. begin
  454. inherited create(stringconstn);
  455. len:=l;
  456. value_str:=s;
  457. cst_type:=cst_conststring;
  458. lab_str:=nil;
  459. end;
  460. destructor tstringconstnode.destroy;
  461. begin
  462. if cst_type=cst_widestring then
  463. donewidestring(pcompilerwidestring(value_str))
  464. else
  465. ansistringdispose(value_str,len);
  466. inherited destroy;
  467. end;
  468. constructor tstringconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  469. var
  470. pw : pcompilerwidestring;
  471. begin
  472. inherited ppuload(t,ppufile);
  473. cst_type:=tconststringtype(ppufile.getbyte);
  474. len:=ppufile.getlongint;
  475. if cst_type=cst_widestring then
  476. begin
  477. initwidestring(pw);
  478. setlengthwidestring(pw,len);
  479. ppufile.getdata(pw^.data,pw^.len*sizeof(tcompilerwidechar));
  480. pcompilerwidestring(value_str):=pw
  481. end
  482. else
  483. begin
  484. getmem(value_str,len+1);
  485. ppufile.getdata(value_str^,len);
  486. value_str[len]:=#0;
  487. end;
  488. lab_str:=tasmlabel(ppufile.getasmsymbol);
  489. end;
  490. procedure tstringconstnode.ppuwrite(ppufile:tcompilerppufile);
  491. begin
  492. inherited ppuwrite(ppufile);
  493. ppufile.putbyte(byte(cst_type));
  494. ppufile.putlongint(len);
  495. if cst_type=cst_widestring then
  496. ppufile.putdata(pcompilerwidestring(value_str)^.data,len*sizeof(tcompilerwidechar))
  497. else
  498. ppufile.putdata(value_str^,len);
  499. ppufile.putasmsymbol(lab_str);
  500. end;
  501. procedure tstringconstnode.buildderefimpl;
  502. begin
  503. inherited buildderefimpl;
  504. end;
  505. procedure tstringconstnode.derefimpl;
  506. begin
  507. inherited derefimpl;
  508. objectlibrary.derefasmsymbol(tasmsymbol(lab_str));
  509. end;
  510. function tstringconstnode._getcopy : tnode;
  511. var
  512. n : tstringconstnode;
  513. begin
  514. n:=tstringconstnode(inherited _getcopy);
  515. n.cst_type:=cst_type;
  516. n.len:=len;
  517. n.lab_str:=lab_str;
  518. if cst_type=cst_widestring then
  519. begin
  520. initwidestring(pcompilerwidestring(n.value_str));
  521. copywidestring(pcompilerwidestring(value_str),pcompilerwidestring(n.value_str));
  522. end
  523. else
  524. n.value_str:=getpcharcopy;
  525. _getcopy:=n;
  526. end;
  527. function tstringconstnode.det_resulttype:tnode;
  528. var
  529. l : aint;
  530. begin
  531. result:=nil;
  532. case cst_type of
  533. cst_conststring :
  534. begin
  535. { handle and store as array[0..len-1] of char }
  536. if len>0 then
  537. l:=len-1
  538. else
  539. l:=0;
  540. resulttype.setdef(tarraydef.create(0,l,s32inttype));
  541. tarraydef(resulttype.def).setelementtype(cchartype);
  542. end;
  543. cst_shortstring :
  544. resulttype:=cshortstringtype;
  545. cst_ansistring :
  546. resulttype:=cansistringtype;
  547. cst_widestring :
  548. resulttype:=cwidestringtype;
  549. cst_longstring :
  550. resulttype:=clongstringtype;
  551. end;
  552. end;
  553. function tstringconstnode.pass_1 : tnode;
  554. begin
  555. result:=nil;
  556. if (cst_type in [cst_ansistring,cst_widestring]) and
  557. (len=0) then
  558. expectloc:=LOC_CONSTANT
  559. else
  560. expectloc:=LOC_CREFERENCE;
  561. end;
  562. function tstringconstnode.getpcharcopy : pchar;
  563. var
  564. pc : pchar;
  565. begin
  566. pc:=nil;
  567. getmem(pc,len+1);
  568. if pc=nil then
  569. Message(general_f_no_memory_left);
  570. move(value_str^,pc^,len+1);
  571. getpcharcopy:=pc;
  572. end;
  573. function tstringconstnode.docompare(p: tnode): boolean;
  574. begin
  575. docompare :=
  576. inherited docompare(p) and
  577. (len = tstringconstnode(p).len) and
  578. { Don't compare the pchars, since they may contain null chars }
  579. { Since all equal constant strings are replaced by the same }
  580. { label, the following compare should be enough (JM) }
  581. (lab_str = tstringconstnode(p).lab_str);
  582. end;
  583. procedure tstringconstnode.changestringtype(const newtype:ttype);
  584. const
  585. st2cst : array[tstringtype] of tconststringtype = (
  586. cst_shortstring,cst_longstring,cst_ansistring,cst_widestring
  587. );
  588. var
  589. pw : pcompilerwidestring;
  590. pc : pchar;
  591. begin
  592. if newtype.def.deftype<>stringdef then
  593. internalerror(200510011);
  594. { convert ascii 2 unicode }
  595. if (tstringdef(newtype.def).string_typ=st_widestring) and
  596. (cst_type<>cst_widestring) then
  597. begin
  598. initwidestring(pw);
  599. ascii2unicode(value_str,len,pw);
  600. ansistringdispose(value_str,len);
  601. pcompilerwidestring(value_str):=pw;
  602. end
  603. else
  604. { convert unicode 2 ascii }
  605. if (cst_type=cst_widestring) and
  606. (tstringdef(newtype.def).string_typ<>st_widestring) then
  607. begin
  608. pw:=pcompilerwidestring(value_str);
  609. getmem(pc,getlengthwidestring(pw)+1);
  610. unicode2ascii(pw,pc);
  611. donewidestring(pw);
  612. value_str:=pc;
  613. end;
  614. cst_type:=st2cst[tstringdef(newtype.def).string_typ];
  615. resulttype:=newtype;
  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.