ncon.pas 26 KB

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