raz / commit 065335aa

commit
065335aa294855015d3a9303b352d5ee06d578c5
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
414ba15e
Parse: a named record or array constant as an element of a constant

patch · log

3 files changed, +33 -2

src/Parse.raz (history) modified +12 -2

@@ -3261,7 +3261,7 @@
32613261
32623262 (* The part of type t at offset off of the constant *)
32633263 proc constPart(t: PType; off: INT): ERROR;
3264-var e: TItem; f: PObj; k: INT; r: F32; start: Diag.Pos; src: ^U8; st: INT;
3264+var e: TItem; f: PObj; k: INT; r: F32; start: Diag.Pos; src: ^U8; st, fx: INT;
32653265 begin
32663266 start := Scan.tokPos();
32673267 if (t^.form = Sym.fSlice) and (Scan.tok = Scan.sLbrak) and (literalLength() = 0) then
@@ -3331,7 +3331,17 @@
33313331 elsif t^.form = Sym.fVariant then
33323332 return Scan.markAt(e.pos, say1("expected a case of {}", Sym.typeName(t)));
33333333 elsif (t^.form = Sym.fArray) or (t^.form = Sym.fRecord) then
3334- return Scan.markAt(e.pos, say1("expected a literal of {}", Sym.typeName(t)));
3334+ (* a literal, or the whole of a constant of the type: its bytes and addresses are copied *)
3335+ if (e.mode # Item.mVar) or (e.obj = nil) or (e.obj^.cls # Sym.oConst) or (e.typ # e.obj^.typ)
3336+ or not Sym.same(e.typ, t) then
3337+ return Scan.markAt(e.pos, say1("expected a literal of {}", Sym.typeName(t)));
3338+ end;
3339+ st := Chunk.chunks[e.chunk].start;
3340+ COPY(constBytes[off..off + INT(t^.size)], Chunk.rodata[st..st + INT(t^.size)]);
3341+ fx := Chunk.chunks[e.chunk].firstFix;
3342+ for k := fx to fx + Chunk.chunks[e.chunk].numFix - 1 do
3343+ try constFix(off + Chunk.dataFixups[k].at, Chunk.dataFixups[k].chunk);
3344+ end;
33353345 else
33363346 try Item.coerce(e, t);
33373347 if (e.mode = Item.mProc) and not e.obj^.ext then

test/run/ConstOfConst.expect (history) added +1 -0

@@ -0,0 +1 @@
1+onetwo

test/run/ConstOfConst.raz (history) added +20 -0

@@ -0,0 +1,20 @@
1+(* A named record or array constant as an element of an array constant,
2+ and a constant with an address in it. *)
3+module ConstOfConst;
4+import Sys;
5+
6+type R = record a: INT; s: []U8; end;
7+
8+const X: R = {a: 1, s: "one"};
9+const Row: [2]INT = [3, 4];
10+const Y: [2]R = [X, {a: 2, s: "two"}];
11+const Grid: [2][2]INT = [Row, [5, 6]];
12+
13+proc main*;
14+begin
15+ _ := Sys.write(Sys.stdout, Y[0].s);
16+ _ := Sys.write(Sys.stdout, Y[1].s);
17+ _ := Sys.write(Sys.stdout, "\n");
18+ if (Y[0].a # 1) or (Y[1].a # 2) or (Grid[0][1] # 4) or (Grid[1][0] # 5) then Sys.exit(1); end;
19+end;
20+end ConstOfConst.