2
0

ncon.pas 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342
  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,constexp,
  22. node,
  23. aasmbase,cpuinfo,globals,
  24. symconst,symtype,symdef,symsym;
  25. type
  26. trealconstnode = class(tnode)
  27. typedef : tdef;
  28. typedefderef : tderef;
  29. value_real : bestreal;
  30. value_currency : currency;
  31. lab_real : tasmlabel;
  32. constructor create(v : bestreal;def:tdef);virtual;
  33. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  34. procedure ppuwrite(ppufile:tcompilerppufile);override;
  35. procedure buildderefimpl;override;
  36. procedure derefimpl;override;
  37. function dogetcopy : tnode;override;
  38. function pass_1 : tnode;override;
  39. function pass_typecheck:tnode;override;
  40. function docompare(p: tnode) : boolean; override;
  41. procedure printnodedata(var t:text);override;
  42. {$ifdef DEBUG_NODE_XML}
  43. procedure XMLPrintNodeData(var T: Text); override;
  44. {$endif DEBUG_NODE_XML}
  45. end;
  46. trealconstnodeclass = class of trealconstnode;
  47. tordconstnode = class(tnode)
  48. typedef : tdef;
  49. typedefderef : tderef;
  50. value : TConstExprInt;
  51. rangecheck : boolean;
  52. { create an ordinal constant node of the specified type and value.
  53. _rangecheck determines if the value of the ordinal should be checked
  54. against the ranges of the type definition.
  55. }
  56. constructor create(const v : tconstexprint;def:tdef; _rangecheck : boolean);virtual;
  57. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  58. procedure ppuwrite(ppufile:tcompilerppufile);override;
  59. procedure buildderefimpl;override;
  60. procedure derefimpl;override;
  61. function dogetcopy : tnode;override;
  62. function pass_1 : tnode;override;
  63. function pass_typecheck:tnode;override;
  64. function docompare(p: tnode) : boolean; override;
  65. procedure printnodedata(var t:text);override;
  66. {$ifdef DEBUG_NODE_XML}
  67. procedure XMLPrintNodeInfo(var T: Text); override;
  68. procedure XMLPrintNodeData(var T: Text); override;
  69. {$endif DEBUG_NODE_XML}
  70. end;
  71. tordconstnodeclass = class of tordconstnode;
  72. tpointerconstnode = class(tnode)
  73. typedef : tdef;
  74. typedefderef : tderef;
  75. value : TConstPtrUInt;
  76. constructor create(v : TConstPtrUInt;def:tdef);virtual;
  77. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  78. procedure ppuwrite(ppufile:tcompilerppufile);override;
  79. procedure buildderefimpl;override;
  80. procedure derefimpl;override;
  81. function dogetcopy : tnode;override;
  82. function pass_1 : tnode;override;
  83. function pass_typecheck:tnode;override;
  84. function docompare(p: tnode) : boolean; override;
  85. procedure printnodedata(var t : text); override;
  86. {$ifdef DEBUG_NODE_XML}
  87. procedure XMLPrintNodeData(var T: Text); override;
  88. {$endif DEBUG_NODE_XML}
  89. end;
  90. tpointerconstnodeclass = class of tpointerconstnode;
  91. tconststringtype = (
  92. cst_conststring,
  93. cst_shortstring,
  94. cst_longstring,
  95. cst_ansistring,
  96. cst_widestring,
  97. cst_unicodestring
  98. );
  99. tstringconstnode = class(tnode)
  100. value_str : pchar;
  101. len : longint;
  102. lab_str : tasmlabel;
  103. astringdef : tdef;
  104. astringdefderef : tderef;
  105. cst_type : tconststringtype;
  106. constructor createstr(const s : string);virtual;
  107. constructor createpchar(s: pchar; l: longint; def: tdef);virtual;
  108. constructor createunistr(w : pcompilerwidestring);virtual;
  109. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  110. procedure ppuwrite(ppufile:tcompilerppufile);override;
  111. procedure buildderefimpl;override;
  112. procedure derefimpl;override;
  113. destructor destroy;override;
  114. function dogetcopy : tnode;override;
  115. function pass_1 : tnode;override;
  116. function pass_typecheck:tnode;override;
  117. function getpcharcopy : pchar;
  118. function docompare(p: tnode) : boolean; override;
  119. procedure changestringtype(def:tdef);
  120. function fullcompare(p: tstringconstnode): longint;
  121. { returns whether this platform uses the nil pointer to represent
  122. empty dynamic strings }
  123. class function emptydynstrnil: boolean; virtual;
  124. {$ifdef DEBUG_NODE_XML}
  125. procedure XMLPrintNodeData(var T: Text); override;
  126. {$endif DEBUG_NODE_XML}
  127. end;
  128. tstringconstnodeclass = class of tstringconstnode;
  129. tsetconstnode = class(tunarynode)
  130. typedef : tdef;
  131. typedefderef : tderef;
  132. value_set : pconstset;
  133. lab_set : tasmsymbol;
  134. constructor create(s : pconstset;def:tdef);virtual;
  135. destructor destroy;override;
  136. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  137. procedure ppuwrite(ppufile:tcompilerppufile);override;
  138. procedure buildderefimpl;override;
  139. procedure derefimpl;override;
  140. procedure adjustforsetbase;
  141. function dogetcopy : tnode;override;
  142. function pass_1 : tnode;override;
  143. function pass_typecheck:tnode;override;
  144. function docompare(p: tnode) : boolean; override;
  145. function elements : AInt;
  146. end;
  147. tsetconstnodeclass = class of tsetconstnode;
  148. tnilnode = class(tnode)
  149. constructor create;virtual;
  150. function pass_1 : tnode;override;
  151. function pass_typecheck:tnode;override;
  152. end;
  153. tnilnodeclass = class of tnilnode;
  154. tguidconstnode = class(tnode)
  155. value : tguid;
  156. lab_set : tasmsymbol;
  157. constructor create(const g:tguid);virtual;
  158. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  159. procedure ppuwrite(ppufile:tcompilerppufile);override;
  160. function dogetcopy : tnode;override;
  161. function pass_1 : tnode;override;
  162. function pass_typecheck:tnode;override;
  163. function docompare(p: tnode) : boolean; override;
  164. end;
  165. tguidconstnodeclass = class of tguidconstnode;
  166. var
  167. crealconstnode : trealconstnodeclass = trealconstnode;
  168. cordconstnode : tordconstnodeclass = tordconstnode;
  169. cpointerconstnode : tpointerconstnodeclass = tpointerconstnode;
  170. cstringconstnode : tstringconstnodeclass = tstringconstnode;
  171. csetconstnode : tsetconstnodeclass = tsetconstnode;
  172. cguidconstnode : tguidconstnodeclass = tguidconstnode;
  173. cnilnode : tnilnodeclass=tnilnode;
  174. function genintconstnode(const v : TConstExprInt) : tordconstnode;
  175. function genenumnode(v : tenumsym) : tordconstnode;
  176. { some helper routines }
  177. function get_ordinal_value(p : tnode) : TConstExprInt;
  178. function get_string_value(p : tnode; def: tstringdef) : tstringconstnode;
  179. function is_constresourcestringnode(p : tnode) : boolean;
  180. function is_emptyset(p : tnode):boolean;
  181. function genconstsymtree(p : tconstsym) : tnode;
  182. function getbooleanvalue(p : tnode) : boolean;
  183. implementation
  184. uses
  185. cutils,
  186. verbose,systems,sysutils,
  187. defcmp,defutil,procinfo,
  188. cgbase,
  189. nld;
  190. function genintconstnode(const v : TConstExprInt) : tordconstnode;
  191. var
  192. htype : tdef;
  193. begin
  194. int_to_type(v,htype);
  195. genintconstnode:=cordconstnode.create(v,htype,true);
  196. end;
  197. function genenumnode(v : tenumsym) : tordconstnode;
  198. var
  199. htype : tdef;
  200. begin
  201. htype:=v.definition;
  202. genenumnode:=cordconstnode.create(int64(v.value),htype,true);
  203. end;
  204. function get_ordinal_value(p : tnode) : TConstExprInt;
  205. begin
  206. get_ordinal_value:=0;
  207. if is_constnode(p) then
  208. begin
  209. if p.nodetype=ordconstn then
  210. get_ordinal_value:=tordconstnode(p).value
  211. else
  212. Message(type_e_ordinal_expr_expected);
  213. end
  214. else
  215. Message(type_e_constant_expr_expected);
  216. end;
  217. function get_string_value(p: tnode; def: tstringdef): tstringconstnode;
  218. var
  219. stringVal: string;
  220. pWideStringVal: pcompilerwidestring;
  221. begin
  222. stringVal:='';
  223. if is_constcharnode(p) then
  224. begin
  225. SetLength(stringVal,1);
  226. stringVal[1]:=char(tordconstnode(p).value.uvalue);
  227. result:=cstringconstnode.createstr(stringVal);
  228. end
  229. else if is_constwidecharnode(p) then
  230. begin
  231. initwidestring(pWideStringVal);
  232. concatwidestringchar(pWideStringVal, tcompilerwidechar(tordconstnode(p).value.uvalue));
  233. result:=cstringconstnode.createunistr(pWideStringVal);
  234. end
  235. else if p.nodetype=stringconstn then
  236. result:=tstringconstnode(p.getcopy)
  237. else
  238. begin
  239. Message(type_e_string_expr_expected);
  240. stringVal:='';
  241. result:=cstringconstnode.createstr(stringVal);
  242. end;
  243. result.changestringtype(def);
  244. end;
  245. function is_constresourcestringnode(p : tnode) : boolean;
  246. begin
  247. is_constresourcestringnode:=(p.nodetype=loadn) and
  248. (tloadnode(p).symtableentry.typ=constsym) and
  249. (tconstsym(tloadnode(p).symtableentry).consttyp=constresourcestring);
  250. end;
  251. function is_emptyset(p : tnode):boolean;
  252. begin
  253. is_emptyset:=(p.nodetype=setconstn) and
  254. (Tsetconstnode(p).value_set^=[]);
  255. end;
  256. function genconstsymtree(p : tconstsym) : tnode;
  257. var
  258. p1 : tnode;
  259. len : longint;
  260. pc : pchar;
  261. begin
  262. p1:=nil;
  263. case p.consttyp of
  264. constord :
  265. begin
  266. if p.constdef=nil then
  267. internalerror(200403232);
  268. p1:=cordconstnode.create(p.value.valueord,p.constdef,true);
  269. end;
  270. conststring :
  271. begin
  272. len:=p.value.len;
  273. if not(cs_refcountedstrings in current_settings.localswitches) and (len>255) then
  274. begin
  275. message(parser_e_string_const_too_long);
  276. len:=255;
  277. end;
  278. getmem(pc,len+1);
  279. move(pchar(p.value.valueptr)^,pc^,len);
  280. pc[len]:=#0;
  281. p1:=cstringconstnode.createpchar(pc,len,p.constdef);
  282. end;
  283. constwstring :
  284. p1:=cstringconstnode.createunistr(pcompilerwidestring(p.value.valueptr));
  285. constreal :
  286. p1:=crealconstnode.create(pbestreal(p.value.valueptr)^,p.constdef);
  287. constset :
  288. p1:=csetconstnode.create(pconstset(p.value.valueptr),p.constdef);
  289. constpointer :
  290. p1:=cpointerconstnode.create(p.value.valueordptr,p.constdef);
  291. constnil :
  292. p1:=cnilnode.create;
  293. constguid :
  294. p1:=cguidconstnode.create(pguid(p.value.valueptr)^);
  295. else
  296. internalerror(200205103);
  297. end;
  298. genconstsymtree:=p1;
  299. end;
  300. function getbooleanvalue(p : tnode) : boolean;
  301. begin
  302. if is_constboolnode(p) then
  303. result:=tordconstnode(p).value<>0
  304. else
  305. internalerror(2013111601);
  306. end;
  307. {*****************************************************************************
  308. TREALCONSTNODE
  309. *****************************************************************************}
  310. { generic code }
  311. { overridden by: }
  312. { i386 }
  313. constructor trealconstnode.create(v : bestreal;def:tdef);
  314. begin
  315. if current_settings.fputype=fpu_none then
  316. internalerror(2008022401);
  317. inherited create(realconstn);
  318. typedef:=def;
  319. case tfloatdef(def).floattype of
  320. s32real:
  321. v:=single(v);
  322. s64real:
  323. v:=double(v);
  324. s80real,
  325. sc80real,
  326. s64comp,
  327. s64currency:
  328. v:=extended(v);
  329. s128real:
  330. internalerror(2013102701);
  331. end;
  332. value_real:=v;
  333. value_currency:=v;
  334. lab_real:=nil;
  335. end;
  336. constructor trealconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  337. var
  338. i : int64;
  339. begin
  340. inherited ppuload(t,ppufile);
  341. ppufile.getderef(typedefderef);
  342. value_real:=ppufile.getreal;
  343. i:=ppufile.getint64;
  344. value_currency:=PCurrency(@i)^;
  345. lab_real:=tasmlabel(ppufile.getasmsymbol);
  346. end;
  347. procedure trealconstnode.ppuwrite(ppufile:tcompilerppufile);
  348. begin
  349. inherited ppuwrite(ppufile);
  350. ppufile.putderef(typedefderef);
  351. ppufile.putreal(value_real);
  352. ppufile.putint64(PInt64(@value_currency)^);
  353. ppufile.putasmsymbol(lab_real);
  354. end;
  355. procedure trealconstnode.buildderefimpl;
  356. begin
  357. inherited buildderefimpl;
  358. typedefderef.build(typedef);
  359. end;
  360. procedure trealconstnode.derefimpl;
  361. begin
  362. inherited derefimpl;
  363. typedef:=tdef(typedefderef.resolve);
  364. end;
  365. function trealconstnode.dogetcopy : tnode;
  366. var
  367. n : trealconstnode;
  368. begin
  369. n:=trealconstnode(inherited dogetcopy);
  370. n.typedef:=typedef;
  371. n.value_real:=value_real;
  372. n.value_currency:=value_currency;
  373. n.lab_real:=lab_real;
  374. dogetcopy:=n;
  375. end;
  376. function trealconstnode.pass_typecheck:tnode;
  377. begin
  378. result:=nil;
  379. resultdef:=typedef;
  380. { range checking? }
  381. if floating_point_range_check_error or
  382. (tfloatdef(resultdef).floattype in [s64comp,s64currency]) then
  383. begin
  384. { use CGMessage so that the resultdef will get set to errordef
  385. by pass1.typecheckpass_internal if a range error was triggered,
  386. which in turn will prevent any potential parent type conversion
  387. node from creating a new realconstnode with this exact same value
  388. and hence trigger the same error again }
  389. case tfloatdef(resultdef).floattype of
  390. s32real :
  391. begin
  392. if ts32real(value_real)=MathInf.Value then
  393. CGMessage(parser_e_range_check_error);
  394. end;
  395. s64real:
  396. begin
  397. if ts64real(value_real)=MathInf.Value then
  398. CGMessage(parser_e_range_check_error);
  399. end;
  400. s80real,
  401. sc80real:
  402. begin
  403. if ts80real(value_real)=MathInf.Value then
  404. CGMessage(parser_e_range_check_error);
  405. end;
  406. s64comp,
  407. s64currency:
  408. begin
  409. if (value_real>9223372036854775807.0) or
  410. (value_real<-9223372036854775808.0) then
  411. CGMessage(parser_e_range_check_error)
  412. end;
  413. s128real:
  414. begin
  415. if ts128real(value_real)=MathInf.Value then
  416. CGMessage(parser_e_range_check_error);
  417. end;
  418. end;
  419. end;
  420. end;
  421. function trealconstnode.pass_1 : tnode;
  422. begin
  423. result:=nil;
  424. expectloc:=LOC_CREFERENCE;
  425. end;
  426. function trealconstnode.docompare(p: tnode): boolean;
  427. begin
  428. docompare :=
  429. inherited docompare(p) and
  430. { this should be always true }
  431. (trealconstnode(p).typedef.typ=floatdef) and (typedef.typ=floatdef) and
  432. (tfloatdef(typedef).floattype = tfloatdef(trealconstnode(p).typedef).floattype) and
  433. (
  434. (
  435. (tfloatdef(typedef).floattype=s64currency) and
  436. (value_currency=trealconstnode(p).value_currency)
  437. )
  438. or
  439. (
  440. (tfloatdef(typedef).floattype<>s64currency) and
  441. (value_real = trealconstnode(p).value_real) and
  442. { floating point compares for non-numbers give strange results usually }
  443. is_number_float(value_real) and
  444. is_number_float(trealconstnode(p).value_real)
  445. )
  446. );
  447. end;
  448. procedure Trealconstnode.printnodedata(var t:text);
  449. begin
  450. inherited printnodedata(t);
  451. writeln(t,printnodeindention,'value = ',value_real);
  452. end;
  453. {$ifdef DEBUG_NODE_XML}
  454. procedure TRealConstNode.XMLPrintNodeData(var T: Text);
  455. begin
  456. inherited XMLPrintNodeData(T);
  457. WriteLn(T, printnodeindention, '<value>', value_real, '</value>');
  458. end;
  459. {$endif DEBUG_NODE_XML}
  460. {*****************************************************************************
  461. TORDCONSTNODE
  462. *****************************************************************************}
  463. constructor tordconstnode.create(const v : tconstexprint;def:tdef;_rangecheck : boolean);
  464. begin
  465. inherited create(ordconstn);
  466. value:=v;
  467. typedef:=def;
  468. rangecheck := _rangecheck;
  469. end;
  470. constructor tordconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  471. begin
  472. inherited ppuload(t,ppufile);
  473. ppufile.getderef(typedefderef);
  474. value:=ppufile.getexprint;
  475. { normally, the value is already compiled, so we don't need
  476. to do once again a range check
  477. }
  478. rangecheck := false;
  479. end;
  480. procedure tordconstnode.ppuwrite(ppufile:tcompilerppufile);
  481. begin
  482. inherited ppuwrite(ppufile);
  483. ppufile.putderef(typedefderef);
  484. ppufile.putexprint(value);
  485. end;
  486. procedure tordconstnode.buildderefimpl;
  487. begin
  488. inherited buildderefimpl;
  489. typedefderef.build(typedef);
  490. end;
  491. procedure tordconstnode.derefimpl;
  492. begin
  493. inherited derefimpl;
  494. typedef:=tdef(typedefderef.resolve);
  495. end;
  496. function tordconstnode.dogetcopy : tnode;
  497. var
  498. n : tordconstnode;
  499. begin
  500. n:=tordconstnode(inherited dogetcopy);
  501. n.value:=value;
  502. n.typedef := typedef;
  503. dogetcopy:=n;
  504. end;
  505. function tordconstnode.pass_typecheck:tnode;
  506. begin
  507. result:=nil;
  508. resultdef:=typedef;
  509. { only do range checking when explicitly asked for it
  510. and if the type can be range checked, see tests/tbs/tb0539.pp }
  511. if (resultdef.typ in [orddef,enumdef]) then
  512. adaptrange(resultdef,value,nf_internal in flags, not rangecheck)
  513. end;
  514. function tordconstnode.pass_1 : tnode;
  515. begin
  516. result:=nil;
  517. expectloc:=LOC_CONSTANT;
  518. end;
  519. function tordconstnode.docompare(p: tnode): boolean;
  520. begin
  521. docompare :=
  522. inherited docompare(p) and
  523. (value = tordconstnode(p).value) and
  524. equal_defs(typedef,tordconstnode(p).typedef);
  525. end;
  526. procedure Tordconstnode.printnodedata(var t:text);
  527. begin
  528. inherited printnodedata(t);
  529. writeln(t,printnodeindention,'value = ',tostr(value));
  530. end;
  531. {$ifdef DEBUG_NODE_XML}
  532. procedure TOrdConstNode.XMLPrintNodeInfo(var T: Text);
  533. begin
  534. inherited XMLPrintNodeInfo(T);
  535. Write(T, ' rangecheck="', rangecheck, '"');
  536. end;
  537. procedure TOrdConstNode.XMLPrintNodeData(var T: Text);
  538. begin
  539. inherited XMLPrintNodeData(T);
  540. WriteLn(T, printnodeindention, '<value>', tostr(value), '</value>');
  541. end;
  542. {$endif DEBUG_NODE_XML}
  543. {*****************************************************************************
  544. TPOINTERCONSTNODE
  545. *****************************************************************************}
  546. constructor tpointerconstnode.create(v : TConstPtrUInt;def:tdef);
  547. begin
  548. inherited create(pointerconstn);
  549. value:=v;
  550. typedef:=def;
  551. end;
  552. constructor tpointerconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  553. begin
  554. inherited ppuload(t,ppufile);
  555. ppufile.getderef(typedefderef);
  556. value:=ppufile.getptruint;
  557. end;
  558. procedure tpointerconstnode.ppuwrite(ppufile:tcompilerppufile);
  559. begin
  560. inherited ppuwrite(ppufile);
  561. ppufile.putderef(typedefderef);
  562. ppufile.putptruint(value);
  563. end;
  564. procedure tpointerconstnode.buildderefimpl;
  565. begin
  566. inherited buildderefimpl;
  567. typedefderef.build(typedef);
  568. end;
  569. procedure tpointerconstnode.derefimpl;
  570. begin
  571. inherited derefimpl;
  572. typedef:=tdef(typedefderef.resolve);
  573. end;
  574. function tpointerconstnode.dogetcopy : tnode;
  575. var
  576. n : tpointerconstnode;
  577. begin
  578. n:=tpointerconstnode(inherited dogetcopy);
  579. n.value:=value;
  580. n.typedef := typedef;
  581. dogetcopy:=n;
  582. end;
  583. function tpointerconstnode.pass_typecheck:tnode;
  584. begin
  585. result:=nil;
  586. resultdef:=typedef;
  587. end;
  588. function tpointerconstnode.pass_1 : tnode;
  589. begin
  590. result:=nil;
  591. expectloc:=LOC_CONSTANT;
  592. end;
  593. function tpointerconstnode.docompare(p: tnode): boolean;
  594. begin
  595. docompare :=
  596. inherited docompare(p) and
  597. (value = tpointerconstnode(p).value);
  598. end;
  599. procedure tpointerconstnode.printnodedata(var t : text);
  600. begin
  601. inherited printnodedata(t);
  602. writeln(t,printnodeindention,'value = $',hexstr(PUInt(value),sizeof(PUInt)*2));
  603. end;
  604. {$ifdef DEBUG_NODE_XML}
  605. procedure TPointerConstNode.XMLPrintNodeData(var T: Text);
  606. begin
  607. inherited XMLPrintNodeData(T);
  608. WriteLn(T, PrintNodeIndention, '<value>$', hexstr(PUInt(value),sizeof(PUInt)*2), '</value>');
  609. end;
  610. {$endif DEBUG_NODE_XML}
  611. {*****************************************************************************
  612. TSTRINGCONSTNODE
  613. *****************************************************************************}
  614. constructor tstringconstnode.createstr(const s : string);
  615. var
  616. l : longint;
  617. begin
  618. inherited create(stringconstn);
  619. l:=length(s);
  620. len:=l;
  621. { stringdup write even past a #0 }
  622. getmem(value_str,l+1);
  623. move(s[1],value_str^,l);
  624. value_str[l]:=#0;
  625. lab_str:=nil;
  626. cst_type:=cst_conststring;
  627. end;
  628. constructor tstringconstnode.createunistr(w : pcompilerwidestring);
  629. begin
  630. inherited create(stringconstn);
  631. len:=getlengthwidestring(w);
  632. initwidestring(pcompilerwidestring(value_str));
  633. copywidestring(w,pcompilerwidestring(value_str));
  634. lab_str:=nil;
  635. cst_type:=cst_unicodestring;
  636. end;
  637. constructor tstringconstnode.createpchar(s: pchar; l: longint; def: tdef);
  638. begin
  639. inherited create(stringconstn);
  640. len:=l;
  641. value_str:=s;
  642. if assigned(def) and
  643. is_ansistring(def) then
  644. begin
  645. cst_type:=cst_ansistring;
  646. astringdef:=def;
  647. end
  648. else
  649. cst_type:=cst_conststring;
  650. lab_str:=nil;
  651. end;
  652. destructor tstringconstnode.destroy;
  653. begin
  654. if cst_type in [cst_widestring,cst_unicodestring] then
  655. donewidestring(pcompilerwidestring(value_str))
  656. else
  657. ansistringdispose(value_str,len);
  658. inherited destroy;
  659. end;
  660. constructor tstringconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  661. var
  662. pw : pcompilerwidestring;
  663. i : longint;
  664. begin
  665. inherited ppuload(t,ppufile);
  666. cst_type:=tconststringtype(ppufile.getbyte);
  667. len:=ppufile.getlongint;
  668. if cst_type in [cst_widestring,cst_unicodestring] then
  669. begin
  670. initwidestring(pw);
  671. setlengthwidestring(pw,len);
  672. { don't use getdata, because the compilerwidechars may have to
  673. be byteswapped
  674. }
  675. {$if sizeof(tcompilerwidechar) = 2}
  676. for i:=0 to pw^.len-1 do
  677. pw^.data[i]:=ppufile.getword;
  678. {$elseif sizeof(tcompilerwidechar) = 4}
  679. for i:=0 to pw^.len-1 do
  680. pw^.data[i]:=cardinal(ppufile.getlongint);
  681. {$else}
  682. {$error Unsupported tcompilerwidechar size}
  683. {$endif}
  684. pcompilerwidestring(value_str):=pw
  685. end
  686. else
  687. begin
  688. getmem(value_str,len+1);
  689. ppufile.getdata(value_str^,len);
  690. value_str[len]:=#0;
  691. end;
  692. lab_str:=tasmlabel(ppufile.getasmsymbol);
  693. if cst_type=cst_ansistring then
  694. ppufile.getderef(astringdefderef);
  695. end;
  696. procedure tstringconstnode.ppuwrite(ppufile:tcompilerppufile);
  697. begin
  698. inherited ppuwrite(ppufile);
  699. ppufile.putbyte(byte(cst_type));
  700. ppufile.putlongint(len);
  701. if cst_type in [cst_widestring,cst_unicodestring] then
  702. ppufile.putdata(pcompilerwidestring(value_str)^.data^,len*sizeof(tcompilerwidechar))
  703. else
  704. ppufile.putdata(value_str^,len);
  705. ppufile.putasmsymbol(lab_str);
  706. if cst_type=cst_ansistring then
  707. ppufile.putderef(astringdefderef);
  708. end;
  709. procedure tstringconstnode.buildderefimpl;
  710. begin
  711. inherited buildderefimpl;
  712. if cst_type=cst_ansistring then
  713. astringdefderef.build(astringdef);
  714. end;
  715. procedure tstringconstnode.derefimpl;
  716. begin
  717. inherited derefimpl;
  718. if cst_type=cst_ansistring then
  719. astringdef:=tdef(astringdefderef.resolve);
  720. end;
  721. function tstringconstnode.dogetcopy : tnode;
  722. var
  723. n : tstringconstnode;
  724. begin
  725. n:=tstringconstnode(inherited dogetcopy);
  726. n.cst_type:=cst_type;
  727. n.len:=len;
  728. n.lab_str:=lab_str;
  729. if cst_type in [cst_widestring,cst_unicodestring] then
  730. begin
  731. initwidestring(pcompilerwidestring(n.value_str));
  732. copywidestring(pcompilerwidestring(value_str),pcompilerwidestring(n.value_str));
  733. end
  734. else
  735. n.value_str:=getpcharcopy;
  736. n.astringdef:=astringdef;
  737. dogetcopy:=n;
  738. end;
  739. function tstringconstnode.pass_typecheck:tnode;
  740. var
  741. l : aint;
  742. begin
  743. result:=nil;
  744. case cst_type of
  745. cst_conststring :
  746. begin
  747. { handle and store as array[0..len-1] of char }
  748. if len>0 then
  749. l:=len-1
  750. else
  751. l:=0;
  752. resultdef:=carraydef.create(0,l,s32inttype);
  753. tarraydef(resultdef).elementdef:=cansichartype;
  754. include(tarraydef(resultdef).arrayoptions,ado_IsConstString);
  755. end;
  756. cst_shortstring :
  757. resultdef:=cshortstringtype;
  758. cst_ansistring :
  759. if not assigned(astringdef) then
  760. resultdef:=getansistringdef
  761. else
  762. resultdef:=astringdef;
  763. cst_unicodestring :
  764. resultdef:=cunicodestringtype;
  765. cst_widestring :
  766. resultdef:=cwidestringtype;
  767. cst_longstring :
  768. resultdef:=clongstringtype;
  769. end;
  770. end;
  771. function tstringconstnode.pass_1 : tnode;
  772. begin
  773. result:=nil;
  774. if (cst_type in [cst_ansistring,cst_widestring,cst_unicodestring]) then
  775. begin
  776. if len=0 then
  777. expectloc:=LOC_CONSTANT
  778. else
  779. expectloc:=LOC_REGISTER
  780. end
  781. else
  782. expectloc:=LOC_CREFERENCE;
  783. end;
  784. function tstringconstnode.getpcharcopy : pchar;
  785. var
  786. pc : pchar;
  787. begin
  788. pc:=nil;
  789. getmem(pc,len+1);
  790. if pc=nil then
  791. Message(general_f_no_memory_left);
  792. move(value_str^,pc^,len+1);
  793. getpcharcopy:=pc;
  794. end;
  795. function tstringconstnode.docompare(p: tnode): boolean;
  796. begin
  797. docompare :=
  798. inherited docompare(p) and
  799. (len = tstringconstnode(p).len) and
  800. (lab_str = tstringconstnode(p).lab_str) and
  801. { This is enough as soon as labels are allocated, otherwise }
  802. { fall back to content compare. }
  803. (assigned(lab_str) or
  804. (cst_type = tstringconstnode(p).cst_type) and
  805. (fullcompare(tstringconstnode(p)) = 0))
  806. ;
  807. end;
  808. procedure tstringconstnode.changestringtype(def:tdef);
  809. const
  810. st2cst : array[tstringtype] of tconststringtype = (
  811. cst_shortstring,cst_longstring,cst_ansistring,cst_widestring,cst_unicodestring);
  812. var
  813. pw : pcompilerwidestring;
  814. pc : pchar;
  815. cp1 : tstringencoding;
  816. cp2 : tstringencoding;
  817. l,l2 : longint;
  818. begin
  819. if def.typ<>stringdef then
  820. internalerror(200510011);
  821. { convert ascii 2 unicode }
  822. if (tstringdef(def).stringtype in [st_widestring,st_unicodestring]) and
  823. not(cst_type in [cst_widestring,cst_unicodestring]) then
  824. begin
  825. initwidestring(pw);
  826. ascii2unicode(value_str,len,current_settings.sourcecodepage,pw);
  827. ansistringdispose(value_str,len);
  828. pcompilerwidestring(value_str):=pw;
  829. end
  830. else
  831. { convert unicode 2 ascii }
  832. if (cst_type in [cst_widestring,cst_unicodestring]) and
  833. not(tstringdef(def).stringtype in [st_widestring,st_unicodestring]) then
  834. begin
  835. cp1:=tstringdef(def).encoding;
  836. if (cp1=globals.CP_NONE) or (cp1=0) then
  837. cp1:=current_settings.sourcecodepage;
  838. if (cp1=CP_UTF8) then
  839. begin
  840. pw:=pcompilerwidestring(value_str);
  841. l2:=len;
  842. l:=UnicodeToUtf8(nil,0,PUnicodeChar(pw^.data),l2);
  843. getmem(pc,l);
  844. UnicodeToUtf8(pc,l,PUnicodeChar(pw^.data),l2);
  845. len:=l-1;
  846. donewidestring(pw);
  847. value_str:=pc;
  848. end
  849. else
  850. begin
  851. pw:=pcompilerwidestring(value_str);
  852. getmem(pc,getlengthwidestring(pw)+1);
  853. unicode2ascii(pw,pc,cp1);
  854. donewidestring(pw);
  855. value_str:=pc;
  856. end;
  857. end
  858. else
  859. if (tstringdef(def).stringtype = st_ansistring) and
  860. not(cst_type in [cst_widestring,cst_unicodestring]) then
  861. begin
  862. cp1:=tstringdef(def).encoding;
  863. if cp1=0 then
  864. cp1:=current_settings.sourcecodepage;
  865. if (cst_type = cst_ansistring) then
  866. begin
  867. cp2:=tstringdef(resultdef).encoding;
  868. if cp2=0 then
  869. cp2:=current_settings.sourcecodepage;
  870. end
  871. else if (cst_type in [cst_shortstring,cst_conststring,cst_longstring]) then
  872. cp2:=current_settings.sourcecodepage
  873. else
  874. internalerror(2013112916);
  875. { don't change string if codepages are equal or string length is 0 }
  876. if (cp1<>cp2) and (len>0) then
  877. begin
  878. if cpavailable(cp1) and cpavailable(cp2) then
  879. changecodepage(value_str,len,cp2,value_str,cp1)
  880. else if (cp1 <> globals.CP_NONE) and (cp2 <> globals.CP_NONE) then
  881. begin
  882. { if source encoding is UTF8 convert using UTF8->UTF16->destination encoding }
  883. if (cp2=CP_UTF8) then
  884. begin
  885. if not cpavailable(cp1) then
  886. Message1(option_code_page_not_available,IntToStr(cp1));
  887. initwidestring(pw);
  888. setlengthwidestring(pw,len);
  889. { returns room for terminating 0 }
  890. l:=Utf8ToUnicode(PUnicodeChar(pw^.data),len,value_str,len);
  891. if (l<>getlengthwidestring(pw)) then
  892. begin
  893. setlengthwidestring(pw,l);
  894. ReAllocMem(value_str,l);
  895. end;
  896. unicode2ascii(pw,value_str,cp1);
  897. len:=l-1;
  898. donewidestring(pw);
  899. end
  900. else
  901. { if destination encoding is UTF8 convert using source encoding->UTF16->UTF8 }
  902. if (cp1=CP_UTF8) then
  903. begin
  904. if not cpavailable(cp2) then
  905. Message1(option_code_page_not_available,IntToStr(cp2));
  906. initwidestring(pw);
  907. setlengthwidestring(pw,len);
  908. ascii2unicode(value_str,len,cp2,pw);
  909. { returns room for terminating 0 }
  910. l:=UnicodeToUtf8(nil,0,PUnicodeChar(pw^.data),len);
  911. if l<>len then
  912. ReAllocMem(value_str,l);
  913. len:=l-1;
  914. UnicodeToUtf8(value_str,PUnicodeChar(pw^.data),l);
  915. donewidestring(pw);
  916. end
  917. else
  918. begin
  919. { output error message that encoding is not available for the compiler }
  920. if not cpavailable(cp1) then
  921. Message1(option_code_page_not_available,IntToStr(cp1));
  922. if not cpavailable(cp2) then
  923. Message1(option_code_page_not_available,IntToStr(cp2));
  924. end;
  925. end;
  926. end;
  927. end;
  928. cst_type:=st2cst[tstringdef(def).stringtype];
  929. resultdef:=def;
  930. end;
  931. function tstringconstnode.fullcompare(p: tstringconstnode): longint;
  932. begin
  933. if cst_type<>p.cst_type then
  934. InternalError(2009121701);
  935. if cst_type in [cst_widestring,cst_unicodestring] then
  936. result:=comparewidestrings(pcompilerwidestring(value_str),pcompilerwidestring(p.value_str))
  937. else
  938. result:=compareansistrings(value_str,p.value_str,len,p.len);
  939. end;
  940. class function tstringconstnode.emptydynstrnil: boolean;
  941. begin
  942. result:=true;
  943. end;
  944. {$ifdef DEBUG_NODE_XML}
  945. procedure TStringConstNode.XMLPrintNodeData(var T: Text);
  946. var
  947. OutputStr: ansistring;
  948. begin
  949. inherited XMLPrintNodeData(T);
  950. Write(T, printnodeindention, '<stringtype>');
  951. case cst_type of
  952. cst_conststring:
  953. Write(T, 'conststring');
  954. cst_shortstring:
  955. Write(T, 'shortstring');
  956. cst_longstring:
  957. Write(T, 'longstring');
  958. cst_ansistring:
  959. Write(T, 'ansistring');
  960. cst_widestring:
  961. Write(T, 'widestring');
  962. cst_unicodestring:
  963. Write(T, 'unicodestring');
  964. end;
  965. WriteLn(T, '</stringtype>');
  966. WriteLn(T, printnodeindention, '<length>', len, '</length>');
  967. if len = 0 then
  968. begin
  969. WriteLn(T, printnodeindention, '<value />');
  970. Exit;
  971. end;
  972. case cst_type of
  973. cst_widestring, cst_unicodestring:
  974. begin
  975. { value_str is of type PCompilerWideString }
  976. SetLength(OutputStr, len);
  977. UnicodeToUtf8(PChar(OutputStr), PUnicodeChar(PCompilerWideString(value_str)^.data), len + 1); { +1 for the null terminator }
  978. end;
  979. else
  980. OutputStr := ansistring(value_str);
  981. SetLength(OutputStr, len);
  982. end;
  983. WriteLn(T, printnodeindention, '<value>', SanitiseXMLString(OutputStr), '</value>');
  984. end;
  985. {$endif DEBUG_NODE_XML}
  986. {*****************************************************************************
  987. TSETCONSTNODE
  988. *****************************************************************************}
  989. constructor tsetconstnode.create(s : pconstset;def:tdef);
  990. begin
  991. inherited create(setconstn,nil);
  992. typedef:=def;
  993. if assigned(s) then
  994. begin
  995. new(value_set);
  996. value_set^:=s^;
  997. end
  998. else
  999. value_set:=nil;
  1000. end;
  1001. destructor tsetconstnode.destroy;
  1002. begin
  1003. if assigned(value_set) then
  1004. dispose(value_set);
  1005. inherited destroy;
  1006. end;
  1007. constructor tsetconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1008. begin
  1009. inherited ppuload(t,ppufile);
  1010. ppufile.getderef(typedefderef);
  1011. new(value_set);
  1012. ppufile.getnormalset(value_set^);
  1013. end;
  1014. procedure tsetconstnode.ppuwrite(ppufile:tcompilerppufile);
  1015. begin
  1016. inherited ppuwrite(ppufile);
  1017. ppufile.putderef(typedefderef);
  1018. ppufile.putnormalset(value_set^);
  1019. end;
  1020. procedure tsetconstnode.buildderefimpl;
  1021. begin
  1022. inherited buildderefimpl;
  1023. typedefderef.build(typedef);
  1024. end;
  1025. procedure tsetconstnode.derefimpl;
  1026. begin
  1027. inherited derefimpl;
  1028. typedef:=tdef(typedefderef.resolve);
  1029. end;
  1030. type
  1031. setbytes = array[0..31] of byte;
  1032. Psetbytes = ^setbytes;
  1033. procedure tsetconstnode.adjustforsetbase;
  1034. var
  1035. i, diff: longint;
  1036. begin
  1037. { Internally, the compiler stores all sets with setbase 0, so we have }
  1038. { to convert the set to its actual format in case setbase<>0 when }
  1039. { writing it out }
  1040. if (tsetdef(resultdef).setbase<>0) then
  1041. begin
  1042. if (tsetdef(resultdef).setbase and 7)<>0 then
  1043. internalerror(2007091501);
  1044. diff:=tsetdef(resultdef).setbase div 8;
  1045. { This is endian-neutral in the new set format: in both cases, }
  1046. { the first byte contains the first elements of the set. }
  1047. { Since the compiler/base rtl cannot contain packed sets before }
  1048. { they work for big endian, it's no problem that the code below }
  1049. { is wrong for the old big endian set format (setbase cannot be }
  1050. { <>0 with non-packed sets). }
  1051. for i:=0 to tsetdef(resultdef).size-1 do
  1052. begin
  1053. Psetbytes(value_set)^[i]:=Psetbytes(value_set)^[i+diff];
  1054. Psetbytes(value_set)^[i+diff]:=0;
  1055. end;
  1056. end;
  1057. end;
  1058. function tsetconstnode.dogetcopy : tnode;
  1059. var
  1060. n : tsetconstnode;
  1061. begin
  1062. n:=tsetconstnode(inherited dogetcopy);
  1063. if assigned(value_set) then
  1064. begin
  1065. new(n.value_set);
  1066. n.value_set^:=value_set^
  1067. end
  1068. else
  1069. n.value_set:=nil;
  1070. n.typedef := typedef;
  1071. n.lab_set:=lab_set;
  1072. dogetcopy:=n;
  1073. end;
  1074. function tsetconstnode.pass_typecheck:tnode;
  1075. begin
  1076. result:=nil;
  1077. resultdef:=typedef;
  1078. end;
  1079. function tsetconstnode.pass_1 : tnode;
  1080. begin
  1081. result:=nil;
  1082. if is_smallset(resultdef) then
  1083. expectloc:=LOC_CONSTANT
  1084. else
  1085. expectloc:=LOC_CREFERENCE;
  1086. end;
  1087. function tsetconstnode.docompare(p: tnode): boolean;
  1088. begin
  1089. docompare:=(inherited docompare(p)) and
  1090. (value_set^=Tsetconstnode(p).value_set^);
  1091. end;
  1092. function tsetconstnode.elements : AInt;
  1093. var
  1094. i : longint;
  1095. begin
  1096. result:=0;
  1097. if not(assigned(value_set)) then
  1098. exit;
  1099. for i:=0 to tsetdef(resultdef).size-1 do
  1100. result:=result+ PopCnt(Psetbytes(value_set)^[i]);
  1101. end;
  1102. {*****************************************************************************
  1103. TNILNODE
  1104. *****************************************************************************}
  1105. constructor tnilnode.create;
  1106. begin
  1107. inherited create(niln);
  1108. end;
  1109. function tnilnode.pass_typecheck:tnode;
  1110. begin
  1111. result:=nil;
  1112. resultdef:=voidpointertype;
  1113. end;
  1114. function tnilnode.pass_1 : tnode;
  1115. begin
  1116. result:=nil;
  1117. expectloc:=LOC_CONSTANT;
  1118. end;
  1119. {*****************************************************************************
  1120. TGUIDCONSTNODE
  1121. *****************************************************************************}
  1122. constructor tguidconstnode.create(const g:tguid);
  1123. begin
  1124. inherited create(guidconstn);
  1125. value:=g;
  1126. end;
  1127. constructor tguidconstnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1128. begin
  1129. inherited ppuload(t,ppufile);
  1130. ppufile.getguid(value);
  1131. end;
  1132. procedure tguidconstnode.ppuwrite(ppufile:tcompilerppufile);
  1133. begin
  1134. inherited ppuwrite(ppufile);
  1135. ppufile.putguid(value);
  1136. end;
  1137. function tguidconstnode.dogetcopy : tnode;
  1138. var
  1139. n : tguidconstnode;
  1140. begin
  1141. n:=tguidconstnode(inherited dogetcopy);
  1142. n.value:=value;
  1143. n.lab_set:=lab_set;
  1144. dogetcopy:=n;
  1145. end;
  1146. function tguidconstnode.pass_typecheck:tnode;
  1147. begin
  1148. result:=nil;
  1149. resultdef:=rec_tguid;
  1150. end;
  1151. function tguidconstnode.pass_1 : tnode;
  1152. begin
  1153. result:=nil;
  1154. expectloc:=LOC_CREFERENCE;
  1155. end;
  1156. function tguidconstnode.docompare(p: tnode): boolean;
  1157. begin
  1158. docompare :=
  1159. inherited docompare(p) and
  1160. (guid2string(value) = guid2string(tguidconstnode(p).value));
  1161. end;
  1162. end.