ncon.pas 26 KB

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