ncon.pas 39 KB

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