From 32b1a75b03b1f4a8a2e26a7ca70fee4aa6cb16a8 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Fri, 23 Oct 2026 12:20:16 +0100 Subject: [PATCH] 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. --- diff --git a/CLAUDE.md b/CLAUDE.md index 155183c..044ece4 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -353,3 +353,8 @@ count of 0 to the width; `POPCOUNT` by `Bytes.count`'s sums of two, four and eight bits and a multiply by `0101...H`, in rax and rdx. A set's `POPCOUNT` counts its bits the same way. +- `SIZE(T.f)` and `OFFSET(T, f)` (report 2026-10.36): the type is read + by `parseType`, where a name before a `.` must be a module's, so + `M.T` is read whole and a `.` past the type is a field's, + `SIZE(M.T.f)` too. `T` must be a record type (a variant is refused, + with help); a path `a.b` is refused, its help naming `a`'s type. diff --git a/conform/err/OffsetNotRecord.raz b/conform/err/OffsetNotRecord.raz new file mode 100644 index 0000000..753faf6 --- /dev/null +++ b/conform/err/OffsetNotRecord.raz @@ -0,0 +1,4 @@ +module OffsetNotRecord; +type Cells = [4]U32; +const At = OFFSET(Cells, len); +end OffsetNotRecord. diff --git a/conform/err/OffsetPath.raz b/conform/err/OffsetPath.raz new file mode 100644 index 0000000..31b3f38 --- /dev/null +++ b/conform/err/OffsetPath.raz @@ -0,0 +1,5 @@ +module OffsetPath; +type Pair = record lo, hi: U16; end; + Outer = record flag: U8; inner: Pair; end; +const At = OFFSET(Outer, inner.hi); +end OffsetPath. diff --git a/conform/err/OffsetUnknown.raz b/conform/err/OffsetUnknown.raz new file mode 100644 index 0000000..d51a3f4 --- /dev/null +++ b/conform/err/OffsetUnknown.raz @@ -0,0 +1,4 @@ +module OffsetUnknown; +type Pair = record lo, hi: U16; end; +const At = OFFSET(Pair, high); +end OffsetUnknown. diff --git a/conform/err/OffsetVariable.raz b/conform/err/OffsetVariable.raz new file mode 100644 index 0000000..6cfc59d --- /dev/null +++ b/conform/err/OffsetVariable.raz @@ -0,0 +1,9 @@ +module OffsetVariable; +type Pair = record lo, hi: U16; end; +var p: Pair; +proc main*; +var n: INT; +begin + n := OFFSET(p, hi); +end; +end OffsetVariable. diff --git a/conform/err/OffsetVariant.raz b/conform/err/OffsetVariant.raz new file mode 100644 index 0000000..240f355 --- /dev/null +++ b/conform/err/OffsetVariant.raz @@ -0,0 +1,4 @@ +module OffsetVariant; +type Shape = variant circle(r: I32); square(side: I64); end; +const At = OFFSET(Shape, circle); +end OffsetVariant. diff --git a/conform/err/SizeNotRecord.raz b/conform/err/SizeNotRecord.raz new file mode 100644 index 0000000..9c4579f --- /dev/null +++ b/conform/err/SizeNotRecord.raz @@ -0,0 +1,4 @@ +module SizeNotRecord; +type Cells = [4]U32; +const N = SIZE(Cells.len); +end SizeNotRecord. diff --git a/conform/err/SizePath.raz b/conform/err/SizePath.raz new file mode 100644 index 0000000..38a89de --- /dev/null +++ b/conform/err/SizePath.raz @@ -0,0 +1,5 @@ +module SizePath; +type Pair = record lo, hi: U16; end; + Outer = record flag: U8; inner: Pair; end; +const N = SIZE(Outer.inner.hi); +end SizePath. diff --git a/conform/run/Offsets.expect b/conform/run/Offsets.expect new file mode 100644 index 0000000..ffa2e0c --- /dev/null +++ b/conform/run/Offsets.expect @@ -0,0 +1,15 @@ +Mixed.a 0 1 +Mixed.b 4 4 +Mixed.c 8 2 +Mixed.d 16 8 +Mixed.e 24 1 +Mixed 0 32 +Outer.flag 0 1 +Outer.inner 2 4 +Outer.tail 8 12 +Pair.hi 2 2 +Tight.b 1 4 +Tight.c 5 2 +Head.len 4 4 +Head.id 8 2 +24 4 4 2 18 diff --git a/conform/run/Offsets.raz b/conform/run/Offsets.raz new file mode 100644 index 0000000..2545009 --- /dev/null +++ b/conform/run/Offsets.raz @@ -0,0 +1,48 @@ +(* OFFSET(T, f) and SIZE(T.f), where a field lies in a record type and + how big it is (report sections 5.5 and 6.8): fields aligned to their + size, with padding before them; a nested record's field; a packed + record's, with none; another module's record, M.T; and both in a + constant. *) +module Offsets; +import Out, Layout; + +type + Mixed = record a: U8; b: I32; c: U16; d: I64; e: U8; end; + Pair = record lo, hi: U16; end; + Outer = record flag: U8; inner: Pair; tail: [3]I32; end; + Tight = packed record a: U8; b: I32; c: U16; end; + +const + EndOfD = OFFSET(Mixed, d) + SIZE(Mixed.d); + HiAt = OFFSET(Outer, inner) + OFFSET(Pair, hi); + LenAt = OFFSET(Layout.Head, len); + IdSize = SIZE(Layout.Head.id); + Typed = U8(OFFSET(Mixed, e) + 250); (* 24 + 250 wraps to 18 *) + +(* name, then the offset and the size of a field *) +proc row(name: []U8; off, size: INT); +begin + Out.str(name); Out.str(" "); Out.int(off); Out.str(" "); Out.int(size); Out.ln; +end; + +proc main*; +begin + row("Mixed.a", OFFSET(Mixed, a), SIZE(Mixed.a)); + row("Mixed.b", OFFSET(Mixed, b), SIZE(Mixed.b)); + row("Mixed.c", OFFSET(Mixed, c), SIZE(Mixed.c)); + row("Mixed.d", OFFSET(Mixed, d), SIZE(Mixed.d)); + row("Mixed.e", OFFSET(Mixed, e), SIZE(Mixed.e)); + row("Mixed", 0, SIZE(Mixed)); + row("Outer.flag", OFFSET(Outer, flag), SIZE(Outer.flag)); + row("Outer.inner", OFFSET(Outer, inner), SIZE(Outer.inner)); + row("Outer.tail", OFFSET(Outer, tail), SIZE(Outer.tail)); + row("Pair.hi", OFFSET(Pair, hi), SIZE(Pair.hi)); + row("Tight.b", OFFSET(Tight, b), SIZE(Tight.b)); + row("Tight.c", OFFSET(Tight, c), SIZE(Tight.c)); + row("Head.len", OFFSET(Layout.Head, len), SIZE(Layout.Head.len)); + row("Head.id", OFFSET(Layout.Head, id), SIZE(Layout.Head.id)); + Out.int(EndOfD); Out.str(" "); Out.int(HiAt); Out.str(" "); Out.int(LenAt); Out.str(" "); + Out.int(IdSize); Out.str(" "); Out.int(I64(Typed)); Out.ln; +end; + +end Offsets. diff --git a/conform/run/mods/Layout.raz b/conform/run/mods/Layout.raz new file mode 100644 index 0000000..4935850 --- /dev/null +++ b/conform/run/mods/Layout.raz @@ -0,0 +1,6 @@ +module Layout; + +(* a record another module asks OFFSET and SIZE of *) +type Head* = record tag*: U8; len*: U32; id*: U16; end; + +end Layout. diff --git a/doc/compiler.md b/doc/compiler.md index 2e1aec1..d209b7e 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -1650,6 +1650,16 @@ parses its own arguments, since `SIZE` takes a type and `INC` a variable. +`SIZE(T.f)` and `OFFSET(T, f)` (report 2026-10.36) read the type with +`parseType` and then the field with `typeField`. The name before a `.` +in a type can only be a module's, so `parseType` reads `M.T` whole, +and a `.` after the type is always a field's: `SIZE(M.T.f)` is field +`f` of `M.T`, with nothing read ahead. `typeField` 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 instead. Each is an +untyped constant, the field's size or its `val`, the offset +`recordType` gave it, so both may stand in a constant. + `PTR(s)` (report 2026-10.34) is `ptrOf`, which refuses what isn't a slice, an array or a string with a message that says what PTR is for (and, for a pointer, what to write), and Item's `startOf`, which makes diff --git a/editor/emacs/raz-mode.el b/editor/emacs/raz-mode.el index 65ec2b3..e694956 100644 --- a/editor/emacs/raz-mode.el +++ b/editor/emacs/raz-mode.el @@ -32,7 +32,7 @@ '("I8" "I16" "I32" "I64" "INT" "UINT" "U8" "U16" "U32" "U64" "BOOL" "F32" "F64" "ERROR")) (defconst raz-builtins - '("SIZE" "ADR" "LEN" "PTR" "NEW" "MSG" "SYSCALL" "FENCE" "CAS" "XCHG" "XADD" + '("SIZE" "OFFSET" "ADR" "LEN" "PTR" "NEW" "MSG" "SYSCALL" "FENCE" "CAS" "XCHG" "XADD" "ARGC" "ARGV" "INC" "DEC" "COPY" "ROL" "ROR" "MIN" "MAX" "CLZ" "CTZ" "POPCOUNT")) diff --git a/editor/vim/syntax/raz.vim b/editor/vim/syntax/raz.vim index 279d454..8f5e128 100644 --- a/editor/vim/syntax/raz.vim +++ b/editor/vim/syntax/raz.vim @@ -14,7 +14,7 @@ syntax keyword razKeyword repeat return set shl shr then to try type until syntax keyword razKeyword var variant VOLATILE while xor syntax keyword razType I8 I16 I32 I64 INT UINT U8 U16 U32 U64 BOOL F32 F64 ERROR -syntax keyword razBuiltin SIZE ADR LEN PTR NEW MSG SYSCALL FENCE CAS XCHG XADD +syntax keyword razBuiltin SIZE OFFSET ADR LEN PTR NEW MSG SYSCALL FENCE CAS XCHG XADD syntax keyword razBuiltin ARGC ARGV INC DEC COPY ROL ROR MIN MAX syntax keyword razBuiltin CLZ CTZ POPCOUNT syntax keyword razConstant true false nil diff --git a/src/Parse.raz b/src/Parse.raz index 912d414..9aea0dd 100644 --- a/src/Parse.raz +++ b/src/Parse.raz @@ -1002,8 +1002,42 @@ return Item.startOf(x, newPtr(e, false)); end; +(* The field f of SIZE(T.f) or OFFSET(T, f), at its name, of the type + t written at at. T is a record type: a variant's cases share their + place, so have none of their own. f is one name, not a path a.b, + which would be a field of f's type, asked of that type. *) +proc typeField(t: PType; at: Diag.Pos; isOffset: BOOL): (PObj, ERROR); +var f: PObj; what, s, hint, g: []U8; dot: Diag.Pos; +begin + if isOffset then what := "OFFSET"; else what := "SIZE(T.f)"; end; + if t^.form # Sym.fRecord then + s := say2("{} needs a record type, found {}", what, Sym.typeName(t)); + if t^.form = Sym.fVariant then + return nil, Scan.markHelp(at, s, "a variant's cases share one place, so have no offset or size of their own"); + end; + return nil, Scan.markAt(at, s); + end; + if Scan.tok # Scan.sIdent then return nil, expected("a field"); end; + f := try fieldOf(t); + Scan.next; + if Scan.tok = Scan.sPeriod then + dot := Scan.tokPos(); + Scan.next; + if Scan.tok = Scan.sIdent then g := nameOf(Scan.id); else g := "g"; end; + if isOffset then + hint := Diag.say("add the offset within {}'s type: OFFSET({}, {}) + OFFSET({}, {})", + [Fmt.str(nameOf(f^.name)), Fmt.str(Sym.typeName(t)), Fmt.str(nameOf(f^.name)), + Fmt.str(Sym.typeName(f^.typ)), Fmt.str(g)]); + else + hint := Diag.say("ask {}'s type: SIZE({}.{})", [Fmt.str(nameOf(f^.name)), Fmt.str(Sym.typeName(f^.typ)), Fmt.str(g)]); + end; + return nil, Scan.markHelp(dot, say1("{} takes one field name, not a path", what), hint); + end; + return f, nil; +end; + proc builtin(var x: TItem): ERROR; -var which, n: INT; y: TItem; t: PType; start: Diag.Pos; +var which, n, start: INT; y: TItem; t: PType; f: PObj; at: Diag.Pos; begin which := INT(x.obj^.val); if (which = Sym.bFence) or (which = Sym.bArgc) or (which = Sym.bArgv) then @@ -1023,8 +1057,26 @@ try expect(Scan.sLparen); match which of | Sym.bSize: + (* SIZE(T) or SIZE(T.f): parseType reads M.T whole, since the + name before a "." there must be a module's; so a "." past the + type is always a field's *) + start := Scan.tokStart(); t := try parseType(); - Item.makeConst(x, Sym.tConst, t^.size); + if Scan.tok = Scan.sPeriod then + at := Scan.spanFromAt(start); + Scan.next; + f := try typeField(t, at, false); + Item.makeConst(x, Sym.tConst, f^.typ^.size); + else + Item.makeConst(x, Sym.tConst, t^.size); + end; + | Sym.bOffset: + start := Scan.tokStart(); + t := try parseType(); + at := Scan.spanFromAt(start); + try expect(Scan.sComma); + f := try typeField(t, at, true); + Item.makeConst(x, Sym.tConst, f^.val); (* a field's val is its offset *) | Sym.bAdr: try expression(y, nil); try Item.notReadOnly(y); @@ -1103,9 +1155,9 @@ try expression(y, nil); try Item.rotate(x, y, which = Sym.bRol); | Sym.bMin, Sym.bMax: - start := Scan.tokPos(); + at := Scan.tokPos(); t := try parseType(); - try Item.limit(x, t, which = Sym.bMax, Scan.spanFrom(start)); + try Item.limit(x, t, which = Sym.bMax, Scan.spanFrom(at)); | Sym.bClz, Sym.bCtz, Sym.bPopcount: try expression(x, nil); try Item.countBits(x, which); diff --git a/src/Sym.raz b/src/Sym.raz index 29051d6..ff2a653 100644 --- a/src/Sym.raz +++ b/src/Sym.raz @@ -133,6 +133,7 @@ bFence* = 6; bCas* = 7; bXchg* = 8; bXadd* = 9; bArgc* = 10; bArgv* = 11; bInc* = 12; bDec* = 13; bCopy* = 14; bPtr* = 15; bRol* = 16; bRor* = 17; bMin* = 18; bMax* = 19; bClz* = 20; bCtz* = 21; bPopcount* = 22; + bOffset* = 23; MaxDepth* = 64; (* scopes open at once *) @@ -699,6 +700,7 @@ enter("ADR", oBuiltin, nil, bAdr); enter("LEN", oBuiltin, nil, bLen); enter("PTR", oBuiltin, nil, bPtr); + enter("OFFSET", oBuiltin, nil, bOffset); enter("NEW", oBuiltin, nil, bNew); enter("MSG", oBuiltin, nil, bMsg); enter("SYSCALL", oBuiltin, nil, bSyscall); diff --git a/test/msg/OffsetNotRecord.err b/test/msg/OffsetNotRecord.err new file mode 100644 index 0000000..5737748 --- /dev/null +++ b/test/msg/OffsetNotRecord.err @@ -0,0 +1,4 @@ +conform/err/OffsetNotRecord.raz:3:19: error: OFFSET needs a record type, found Cells + 3 | const At = OFFSET(Cells, len); + | ^^^^^ +raz: 1 error diff --git a/test/msg/OffsetPath.err b/test/msg/OffsetPath.err new file mode 100644 index 0000000..346ea65 --- /dev/null +++ b/test/msg/OffsetPath.err @@ -0,0 +1,5 @@ +conform/err/OffsetPath.raz:4:31: error: OFFSET takes one field name, not a path + 4 | const At = OFFSET(Outer, inner.hi); + | ^ +help: add the offset within inner's type: OFFSET(Outer, inner) + OFFSET(Pair, hi) +raz: 1 error diff --git a/test/msg/OffsetUnknown.err b/test/msg/OffsetUnknown.err new file mode 100644 index 0000000..eece49e --- /dev/null +++ b/test/msg/OffsetUnknown.err @@ -0,0 +1,4 @@ +conform/err/OffsetUnknown.raz:3:25: error: Pair has no field high + 3 | const At = OFFSET(Pair, high); + | ^^^^ +raz: 1 error diff --git a/test/msg/OffsetVariable.err b/test/msg/OffsetVariable.err new file mode 100644 index 0000000..467c526 --- /dev/null +++ b/test/msg/OffsetVariable.err @@ -0,0 +1,4 @@ +conform/err/OffsetVariable.raz:7:17: error: p isn't a type + 7 | n := OFFSET(p, hi); + | ^ +raz: 1 error diff --git a/test/msg/OffsetVariant.err b/test/msg/OffsetVariant.err new file mode 100644 index 0000000..56a245a --- /dev/null +++ b/test/msg/OffsetVariant.err @@ -0,0 +1,5 @@ +conform/err/OffsetVariant.raz:3:19: error: OFFSET needs a record type, found Shape + 3 | const At = OFFSET(Shape, circle); + | ^^^^^ +help: a variant's cases share one place, so have no offset or size of their own +raz: 1 error diff --git a/test/msg/SizeNotRecord.err b/test/msg/SizeNotRecord.err new file mode 100644 index 0000000..7736f12 --- /dev/null +++ b/test/msg/SizeNotRecord.err @@ -0,0 +1,4 @@ +conform/err/SizeNotRecord.raz:3:16: error: SIZE(T.f) needs a record type, found Cells + 3 | const N = SIZE(Cells.len); + | ^^^^^ +raz: 1 error diff --git a/test/msg/SizePath.err b/test/msg/SizePath.err new file mode 100644 index 0000000..4ae92ce --- /dev/null +++ b/test/msg/SizePath.err @@ -0,0 +1,5 @@ +conform/err/SizePath.raz:4:27: error: SIZE(T.f) takes one field name, not a path + 4 | const N = SIZE(Outer.inner.hi); + | ^ +help: ask inner's type: SIZE(Pair.hi) +raz: 1 error