raz / commit 32b1a75b

commit
32b1a75b03b1f4a8a2e26a7ca70fee4aa6cb16a8
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
eaa6cf3c
OFFSET(T, f) and SIZE(T.f): a field's place and size, as report 2026-10.36 has them

Sym enters OFFSET; Parse's builtin reads the type with parseType and
the field with typeField, which refuses a type that isn't a record
(with help for a variant, whose cases share one place) and a path a.b
(with help that asks a's type). The name before a "." in a type can
only be a module's, so parseType reads M.T whole, and a "." past the
type is a field's: SIZE(M.T.f) needs no reading ahead. Both are
untyped constants, the field's size or its offset, so they stand in a
constant too.

Tests: conform/run/Offsets (padding, a nested record, a packed one,
another module's record, in constants); conform/err Offset{Path,
Variant,NotRecord,Unknown,Variable}, Size{Path,NotRecord}, their
messages in test/msg. compiler.md, the editors' built-in lists and
CLAUDE.md's readings say so. Code lines 11,880 -> 11,926.

patch · log

23 files changed, +210 -6

CLAUDE.md modified +5 -0

@@ -353,3 +353,8 @@
353353 count of 0 to the width; `POPCOUNT` by `Bytes.count`'s sums of two,
354354 four and eight bits and a multiply by `0101...H`, in rax and rdx. A
355355 set's `POPCOUNT` counts its bits the same way.
356+- `SIZE(T.f)` and `OFFSET(T, f)` (report 2026-10.36): the type is read
357+ by `parseType`, where a name before a `.` must be a module's, so
358+ `M.T` is read whole and a `.` past the type is a field's,
359+ `SIZE(M.T.f)` too. `T` must be a record type (a variant is refused,
360+ with help); a path `a.b` is refused, its help naming `a`'s type.

conform/err/OffsetNotRecord.raz (history) added +4 -0

@@ -0,0 +1,4 @@
1+module OffsetNotRecord;
2+type Cells = [4]U32;
3+const At = OFFSET(Cells, len);
4+end OffsetNotRecord.

conform/err/OffsetPath.raz (history) added +5 -0

@@ -0,0 +1,5 @@
1+module OffsetPath;
2+type Pair = record lo, hi: U16; end;
3+ Outer = record flag: U8; inner: Pair; end;
4+const At = OFFSET(Outer, inner.hi);
5+end OffsetPath.

conform/err/OffsetUnknown.raz (history) added +4 -0

@@ -0,0 +1,4 @@
1+module OffsetUnknown;
2+type Pair = record lo, hi: U16; end;
3+const At = OFFSET(Pair, high);
4+end OffsetUnknown.

conform/err/OffsetVariable.raz (history) added +9 -0

@@ -0,0 +1,9 @@
1+module OffsetVariable;
2+type Pair = record lo, hi: U16; end;
3+var p: Pair;
4+proc main*;
5+var n: INT;
6+begin
7+ n := OFFSET(p, hi);
8+end;
9+end OffsetVariable.

conform/err/OffsetVariant.raz (history) added +4 -0

@@ -0,0 +1,4 @@
1+module OffsetVariant;
2+type Shape = variant circle(r: I32); square(side: I64); end;
3+const At = OFFSET(Shape, circle);
4+end OffsetVariant.

conform/err/SizeNotRecord.raz (history) added +4 -0

@@ -0,0 +1,4 @@
1+module SizeNotRecord;
2+type Cells = [4]U32;
3+const N = SIZE(Cells.len);
4+end SizeNotRecord.

conform/err/SizePath.raz (history) added +5 -0

@@ -0,0 +1,5 @@
1+module SizePath;
2+type Pair = record lo, hi: U16; end;
3+ Outer = record flag: U8; inner: Pair; end;
4+const N = SIZE(Outer.inner.hi);
5+end SizePath.

conform/run/Offsets.expect (history) added +15 -0

@@ -0,0 +1,15 @@
1+Mixed.a 0 1
2+Mixed.b 4 4
3+Mixed.c 8 2
4+Mixed.d 16 8
5+Mixed.e 24 1
6+Mixed 0 32
7+Outer.flag 0 1
8+Outer.inner 2 4
9+Outer.tail 8 12
10+Pair.hi 2 2
11+Tight.b 1 4
12+Tight.c 5 2
13+Head.len 4 4
14+Head.id 8 2
15+24 4 4 2 18

conform/run/Offsets.raz (history) added +48 -0

@@ -0,0 +1,48 @@
1+(* OFFSET(T, f) and SIZE(T.f), where a field lies in a record type and
2+ how big it is (report sections 5.5 and 6.8): fields aligned to their
3+ size, with padding before them; a nested record's field; a packed
4+ record's, with none; another module's record, M.T; and both in a
5+ constant. *)
6+module Offsets;
7+import Out, Layout;
8+
9+type
10+ Mixed = record a: U8; b: I32; c: U16; d: I64; e: U8; end;
11+ Pair = record lo, hi: U16; end;
12+ Outer = record flag: U8; inner: Pair; tail: [3]I32; end;
13+ Tight = packed record a: U8; b: I32; c: U16; end;
14+
15+const
16+ EndOfD = OFFSET(Mixed, d) + SIZE(Mixed.d);
17+ HiAt = OFFSET(Outer, inner) + OFFSET(Pair, hi);
18+ LenAt = OFFSET(Layout.Head, len);
19+ IdSize = SIZE(Layout.Head.id);
20+ Typed = U8(OFFSET(Mixed, e) + 250); (* 24 + 250 wraps to 18 *)
21+
22+(* name, then the offset and the size of a field *)
23+proc row(name: []U8; off, size: INT);
24+begin
25+ Out.str(name); Out.str(" "); Out.int(off); Out.str(" "); Out.int(size); Out.ln;
26+end;
27+
28+proc main*;
29+begin
30+ row("Mixed.a", OFFSET(Mixed, a), SIZE(Mixed.a));
31+ row("Mixed.b", OFFSET(Mixed, b), SIZE(Mixed.b));
32+ row("Mixed.c", OFFSET(Mixed, c), SIZE(Mixed.c));
33+ row("Mixed.d", OFFSET(Mixed, d), SIZE(Mixed.d));
34+ row("Mixed.e", OFFSET(Mixed, e), SIZE(Mixed.e));
35+ row("Mixed", 0, SIZE(Mixed));
36+ row("Outer.flag", OFFSET(Outer, flag), SIZE(Outer.flag));
37+ row("Outer.inner", OFFSET(Outer, inner), SIZE(Outer.inner));
38+ row("Outer.tail", OFFSET(Outer, tail), SIZE(Outer.tail));
39+ row("Pair.hi", OFFSET(Pair, hi), SIZE(Pair.hi));
40+ row("Tight.b", OFFSET(Tight, b), SIZE(Tight.b));
41+ row("Tight.c", OFFSET(Tight, c), SIZE(Tight.c));
42+ row("Head.len", OFFSET(Layout.Head, len), SIZE(Layout.Head.len));
43+ row("Head.id", OFFSET(Layout.Head, id), SIZE(Layout.Head.id));
44+ Out.int(EndOfD); Out.str(" "); Out.int(HiAt); Out.str(" "); Out.int(LenAt); Out.str(" ");
45+ Out.int(IdSize); Out.str(" "); Out.int(I64(Typed)); Out.ln;
46+end;
47+
48+end Offsets.

conform/run/mods/Layout.raz (history) added +6 -0

@@ -0,0 +1,6 @@
1+module Layout;
2+
3+(* a record another module asks OFFSET and SIZE of *)
4+type Head* = record tag*: U8; len*: U32; id*: U16; end;
5+
6+end Layout.

doc/compiler.md (history) modified +10 -0

@@ -1650,6 +1650,16 @@
16501650 parses its own arguments, since `SIZE` takes a type and `INC` a
16511651 variable.
16521652
1653+`SIZE(T.f)` and `OFFSET(T, f)` (report 2026-10.36) read the type with
1654+`parseType` and then the field with `typeField`. The name before a `.`
1655+in a type can only be a module's, so `parseType` reads `M.T` whole,
1656+and a `.` after the type is always a field's: `SIZE(M.T.f)` is field
1657+`f` of `M.T`, with nothing read ahead. `typeField` refuses a type that
1658+isn't a record, with help for a variant, whose cases share one place,
1659+and a path, `a.b`, with help that asks `a`'s type instead. Each is an
1660+untyped constant, the field's size or its `val`, the offset
1661+`recordType` gave it, so both may stand in a constant.
1662+
16531663 `PTR(s)` (report 2026-10.34) is `ptrOf`, which refuses what isn't a
16541664 slice, an array or a string with a message that says what PTR is for
16551665 (and, for a pointer, what to write), and Item's `startOf`, which makes

editor/emacs/raz-mode.el (history) modified +1 -1

@@ -32,7 +32,7 @@
3232 '("I8" "I16" "I32" "I64" "INT" "UINT" "U8" "U16" "U32" "U64" "BOOL" "F32" "F64" "ERROR"))
3333
3434 (defconst raz-builtins
35- '("SIZE" "ADR" "LEN" "PTR" "NEW" "MSG" "SYSCALL" "FENCE" "CAS" "XCHG" "XADD"
35+ '("SIZE" "OFFSET" "ADR" "LEN" "PTR" "NEW" "MSG" "SYSCALL" "FENCE" "CAS" "XCHG" "XADD"
3636 "ARGC" "ARGV" "INC" "DEC" "COPY" "ROL" "ROR" "MIN" "MAX"
3737 "CLZ" "CTZ" "POPCOUNT"))
3838

editor/vim/syntax/raz.vim (history) modified +1 -1

@@ -14,7 +14,7 @@
1414 syntax keyword razKeyword repeat return set shl shr then to try type until
1515 syntax keyword razKeyword var variant VOLATILE while xor
1616 syntax keyword razType I8 I16 I32 I64 INT UINT U8 U16 U32 U64 BOOL F32 F64 ERROR
17-syntax keyword razBuiltin SIZE ADR LEN PTR NEW MSG SYSCALL FENCE CAS XCHG XADD
17+syntax keyword razBuiltin SIZE OFFSET ADR LEN PTR NEW MSG SYSCALL FENCE CAS XCHG XADD
1818 syntax keyword razBuiltin ARGC ARGV INC DEC COPY ROL ROR MIN MAX
1919 syntax keyword razBuiltin CLZ CTZ POPCOUNT
2020 syntax keyword razConstant true false nil

src/Parse.raz (history) modified +56 -4

@@ -1002,8 +1002,42 @@
10021002 return Item.startOf(x, newPtr(e, false));
10031003 end;
10041004
1005+(* The field f of SIZE(T.f) or OFFSET(T, f), at its name, of the type
1006+ t written at at. T is a record type: a variant's cases share their
1007+ place, so have none of their own. f is one name, not a path a.b,
1008+ which would be a field of f's type, asked of that type. *)
1009+proc typeField(t: PType; at: Diag.Pos; isOffset: BOOL): (PObj, ERROR);
1010+var f: PObj; what, s, hint, g: []U8; dot: Diag.Pos;
1011+begin
1012+ if isOffset then what := "OFFSET"; else what := "SIZE(T.f)"; end;
1013+ if t^.form # Sym.fRecord then
1014+ s := say2("{} needs a record type, found {}", what, Sym.typeName(t));
1015+ if t^.form = Sym.fVariant then
1016+ return nil, Scan.markHelp(at, s, "a variant's cases share one place, so have no offset or size of their own");
1017+ end;
1018+ return nil, Scan.markAt(at, s);
1019+ end;
1020+ if Scan.tok # Scan.sIdent then return nil, expected("a field"); end;
1021+ f := try fieldOf(t);
1022+ Scan.next;
1023+ if Scan.tok = Scan.sPeriod then
1024+ dot := Scan.tokPos();
1025+ Scan.next;
1026+ if Scan.tok = Scan.sIdent then g := nameOf(Scan.id); else g := "g"; end;
1027+ if isOffset then
1028+ hint := Diag.say("add the offset within {}'s type: OFFSET({}, {}) + OFFSET({}, {})",
1029+ [Fmt.str(nameOf(f^.name)), Fmt.str(Sym.typeName(t)), Fmt.str(nameOf(f^.name)),
1030+ Fmt.str(Sym.typeName(f^.typ)), Fmt.str(g)]);
1031+ else
1032+ hint := Diag.say("ask {}'s type: SIZE({}.{})", [Fmt.str(nameOf(f^.name)), Fmt.str(Sym.typeName(f^.typ)), Fmt.str(g)]);
1033+ end;
1034+ return nil, Scan.markHelp(dot, say1("{} takes one field name, not a path", what), hint);
1035+ end;
1036+ return f, nil;
1037+end;
1038+
10051039 proc builtin(var x: TItem): ERROR;
1006-var which, n: INT; y: TItem; t: PType; start: Diag.Pos;
1040+var which, n, start: INT; y: TItem; t: PType; f: PObj; at: Diag.Pos;
10071041 begin
10081042 which := INT(x.obj^.val);
10091043 if (which = Sym.bFence) or (which = Sym.bArgc) or (which = Sym.bArgv) then
@@ -1023,8 +1057,26 @@
10231057 try expect(Scan.sLparen);
10241058 match which of
10251059 | Sym.bSize:
1060+ (* SIZE(T) or SIZE(T.f): parseType reads M.T whole, since the
1061+ name before a "." there must be a module's; so a "." past the
1062+ type is always a field's *)
1063+ start := Scan.tokStart();
10261064 t := try parseType();
1027- Item.makeConst(x, Sym.tConst, t^.size);
1065+ if Scan.tok = Scan.sPeriod then
1066+ at := Scan.spanFromAt(start);
1067+ Scan.next;
1068+ f := try typeField(t, at, false);
1069+ Item.makeConst(x, Sym.tConst, f^.typ^.size);
1070+ else
1071+ Item.makeConst(x, Sym.tConst, t^.size);
1072+ end;
1073+ | Sym.bOffset:
1074+ start := Scan.tokStart();
1075+ t := try parseType();
1076+ at := Scan.spanFromAt(start);
1077+ try expect(Scan.sComma);
1078+ f := try typeField(t, at, true);
1079+ Item.makeConst(x, Sym.tConst, f^.val); (* a field's val is its offset *)
10281080 | Sym.bAdr:
10291081 try expression(y, nil);
10301082 try Item.notReadOnly(y);
@@ -1103,9 +1155,9 @@
11031155 try expression(y, nil);
11041156 try Item.rotate(x, y, which = Sym.bRol);
11051157 | Sym.bMin, Sym.bMax:
1106- start := Scan.tokPos();
1158+ at := Scan.tokPos();
11071159 t := try parseType();
1108- try Item.limit(x, t, which = Sym.bMax, Scan.spanFrom(start));
1160+ try Item.limit(x, t, which = Sym.bMax, Scan.spanFrom(at));
11091161 | Sym.bClz, Sym.bCtz, Sym.bPopcount:
11101162 try expression(x, nil);
11111163 try Item.countBits(x, which);

src/Sym.raz (history) modified +2 -0

@@ -133,6 +133,7 @@
133133 bFence* = 6; bCas* = 7; bXchg* = 8; bXadd* = 9; bArgc* = 10; bArgv* = 11;
134134 bInc* = 12; bDec* = 13; bCopy* = 14; bPtr* = 15; bRol* = 16; bRor* = 17;
135135 bMin* = 18; bMax* = 19; bClz* = 20; bCtz* = 21; bPopcount* = 22;
136+ bOffset* = 23;
136137
137138 MaxDepth* = 64; (* scopes open at once *)
138139
@@ -699,6 +700,7 @@
699700 enter("ADR", oBuiltin, nil, bAdr);
700701 enter("LEN", oBuiltin, nil, bLen);
701702 enter("PTR", oBuiltin, nil, bPtr);
703+ enter("OFFSET", oBuiltin, nil, bOffset);
702704 enter("NEW", oBuiltin, nil, bNew);
703705 enter("MSG", oBuiltin, nil, bMsg);
704706 enter("SYSCALL", oBuiltin, nil, bSyscall);

test/msg/OffsetNotRecord.err (history) added +4 -0

@@ -0,0 +1,4 @@
1+conform/err/OffsetNotRecord.raz:3:19: error: OFFSET needs a record type, found Cells
2+ 3 | const At = OFFSET(Cells, len);
3+ | ^^^^^
4+raz: 1 error

test/msg/OffsetPath.err (history) added +5 -0

@@ -0,0 +1,5 @@
1+conform/err/OffsetPath.raz:4:31: error: OFFSET takes one field name, not a path
2+ 4 | const At = OFFSET(Outer, inner.hi);
3+ | ^
4+help: add the offset within inner's type: OFFSET(Outer, inner) + OFFSET(Pair, hi)
5+raz: 1 error

test/msg/OffsetUnknown.err (history) added +4 -0

@@ -0,0 +1,4 @@
1+conform/err/OffsetUnknown.raz:3:25: error: Pair has no field high
2+ 3 | const At = OFFSET(Pair, high);
3+ | ^^^^
4+raz: 1 error

test/msg/OffsetVariable.err (history) added +4 -0

@@ -0,0 +1,4 @@
1+conform/err/OffsetVariable.raz:7:17: error: p isn't a type
2+ 7 | n := OFFSET(p, hi);
3+ | ^
4+raz: 1 error

test/msg/OffsetVariant.err (history) added +5 -0

@@ -0,0 +1,5 @@
1+conform/err/OffsetVariant.raz:3:19: error: OFFSET needs a record type, found Shape
2+ 3 | const At = OFFSET(Shape, circle);
3+ | ^^^^^
4+help: a variant's cases share one place, so have no offset or size of their own
5+raz: 1 error

test/msg/SizeNotRecord.err (history) added +4 -0

@@ -0,0 +1,4 @@
1+conform/err/SizeNotRecord.raz:3:16: error: SIZE(T.f) needs a record type, found Cells
2+ 3 | const N = SIZE(Cells.len);
3+ | ^^^^^
4+raz: 1 error

test/msg/SizePath.err (history) added +5 -0

@@ -0,0 +1,5 @@
1+conform/err/SizePath.raz:4:27: error: SIZE(T.f) takes one field name, not a path
2+ 4 | const N = SIZE(Outer.inner.hi);
3+ | ^
4+help: ask inner's type: SIZE(Pair.hi)
5+raz: 1 error