raz / commit c473acc2

commit
c473acc2e8e8d8bbba8425c15978525c6b816f7a
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
a48b818d
raz.md: OFFSET, SIZE of a field, and BYTES

patch · log

3 files changed, +64 -6

doc/raz.7 (history) modified +22 -0

@@ -414,6 +414,18 @@
414414 The size of type
415415 .Ar T ,
416416 an untyped constant.
417+.It Fn SIZE T.f
418+The size of field
419+.Ar f
420+of the record type
421+.Ar T ,
422+an untyped constant.
423+.It Fn OFFSET T f
424+The byte offset of field
425+.Ar f
426+in the record type
427+.Ar T ,
428+an untyped constant.
417429 .It Fn ADR x
418430 The address of the variable
419431 .Ar x .
@@ -439,6 +451,16 @@
439451 a
440452 .Cm ^Mem.Allocator ,
441453 or to nil.
454+.It Fn BYTES x
455+The bytes of the variable
456+.Ar x
457+as a
458+.Li []U8 :
459+for a
460+.Li []T
461+its
462+.Li LEN(s) * SIZE(T)
463+bytes; of a type a view may take.
442464 .It Fn MSG e
443465 The message of an
444466 .Cm ERROR .

doc/raz.md (history) modified +37 -5

@@ -52,7 +52,10 @@
5252 rotated (sections 6.1 and 6.8); it breaks no program. 2026-10.36 is a
5353 twelfth: `MIN(T)` and `MAX(T)`, the limits of an integer type, and
5454 `CLZ`, `CTZ` and `POPCOUNT`, which count bits (sections 6.1 and 6.8);
55-and `INT(b)`, a `BOOL` as an integer (section 6.7); it breaks no program.
55+`INT(b)`, a `BOOL` as an integer (section 6.7); `OFFSET(T, f)` and
56+`SIZE(T.f)`, where a field lies and how big it is; and `BYTES(x)`, the
57+bytes of a variable or a slice (sections 4 and 6.8); it breaks no
58+program.
5659
5760 The language is frozen, softly, from this version on: it changes only
5861 to mend a defect, and its library grows only by what breaks no program
@@ -379,7 +382,7 @@
379382 procedure may be declared `forward` and defined later (section 8).
380383
381384 **Constants** are evaluated by the compiler. Their expressions use only
382-literals, other constants, operators, conversions, `SIZE`, the `LEN`
385+literals, other constants, operators, conversions, `SIZE`, `OFFSET`, the `LEN`
383386 of arrays and strings, `MIN`, `MAX`, `ROL`, `ROR`, `CLZ`, `CTZ`,
384387 `POPCOUNT`, and a variant's cases. A constant is an integer,
385388 a real, a `BOOL`, a name of an enumeration, a set, a string, an array,
@@ -793,8 +796,8 @@
793796 taken. On a target whose `INT` is narrower, a pointer's offset is cut
794797 to an `INT`.
795798
796-`SIZE(T)`, and `LEN` of an array or a string constant, are untyped
797-constants, like a literal.
799+`SIZE(T)`, `SIZE(T.f)`, `OFFSET(T, f)`, and `LEN` of an array or a
800+string constant, are untyped constants, like a literal.
798801
799802 `div` rounds toward zero and `mod` takes the sign of the dividend, as
800803 in C: `-7 div 2` is `-3`, `-7 mod 2` is `-1`. Dividing by zero, and
@@ -960,9 +963,12 @@
960963 | Call | Result |
961964 |---|---|
962965 | `SIZE(T)` | the size of type `T` in bytes, an untyped constant; `T` is a type, not a variable |
966+| `SIZE(T.f)` | the size of field `f` of the record type `T`, an untyped constant |
967+| `OFFSET(T, f)` | the byte offset of field `f` in the record type `T`, an untyped constant |
963968 | `ADR(x)` | the address of variable `x`, a `^T` |
964969 | `LEN(a)` | the number of elements of an array or a string constant (an untyped constant) or of a slice (an `INT`) |
965970 | `PTR(s)` | the address the slice `s` starts at, a `^T` for `s: []T`, unchecked; for an array `a`, that of `a[0..LEN(a)]` |
971+| `BYTES(x)` | the bytes of `x` as a `[]U8`: for a slice `s: []T`, its `LEN(s) * SIZE(T)` bytes; for a variable of any other type `T`, its `SIZE(T)` bytes |
966972 | `NEW(p, a)` | sets `p: ^T` to a fresh, zeroed `T` from the allocator `a: ^Mem.Allocator`, or to `nil` if `a` has no room |
967973 | `MSG(e)` | the message of the error `e`, a `[]U8` |
968974 | `COPY(d, s)` | copies the elements of `s` to the start of `d`, both arrays or slices of one type `T`; `LEN(s)` must be at most `LEN(d)` |
@@ -989,6 +995,31 @@
989995 `Mem.Allocator` (the library, `doc/raz-lib.md`) is the one type of the library the
990996 language names.
991997
998+`SIZE(T.f)` and `OFFSET(T, f)` ask about a type's field, not about
999+storage, so `SIZE` still takes no variable. `f` is one field name: not
1000+a path, `a.b`, and not a case of a variant, whose cases share their
1001+place. A record handed to C is checked against C's struct with them,
1002+as C checks one with `offsetof` and `sizeof`.
1003+
1004+`BYTES(x)` is the bytes of `x`, as a `[]U8` that can be read and
1005+written: for a slice `s: []T`, the `LEN(s) * SIZE(T)` bytes it holds;
1006+for a variable of any other type `T`, its `SIZE(T)` bytes.
1007+
1008+```
1009+n := Sys.write(fd, BYTES(samples)); (* samples: []I32 *)
1010+n := Sys.read(fd, BYTES(h)); (* fills h: Header, a record *)
1011+```
1012+
1013+`T` must be a type a view may take (section 6.9), since writing through
1014+the slice must not make a value mean nothing: a `BOOL`, a set, an
1015+enumeration, a slice, a variant, a procedure type, `ERROR`, or a record
1016+or array holding one, is an error. The slice is made, and checked, as
1017+any slice of the variable is, so its bytes follow the rules for it: the
1018+bytes of a local can't be returned (section 7.3), and a `const`
1019+parameter, a view, a volatile variable and a string constant are
1020+errors, as they are for `ADR`. The bytes are in the target's order
1021+(appendix A).
1022+
9921023 `PTR(s)` is the first of the two words of `s` (section 5.3), and is not
9931024 checked. For a slice that isn't empty it is `ADR(s[0])`; for an empty
9941025 one, where `s[0]` would stop the program, it is what the slice holds:
@@ -1215,7 +1246,8 @@
12151246 A slice returned must not point into the procedure's own frame, which
12161247 is gone once it returns. It is an error to return, as a slice, a local
12171248 array, a value parameter that is an array, an array literal other than
1218-`[]`, or an array a call returns, or a slice of one, a field or an
1249+`[]`, or an array a call returns, or a slice of one, or the `BYTES` of
1250+a local variable, a field or an
12191251 element of one included:
12201252
12211253 ```

doc/report-history.md (history) modified +5 -1

@@ -242,4 +242,8 @@
242242 predeclared name may be hidden (section 2), so it breaks no program.
243243 A `BOOL` also converts to an integer, 0 for `false` and 1 for `true`
244244 (6.7), a constant too; there is still none from an integer to a
245- `BOOL`. It breaks no program.
245+ `BOOL`. `OFFSET(T, f)` and `SIZE(T.f)` are the offset of a field in
246+ a record type and its size, untyped constants (sections 4 and 6.8);
247+ and `BYTES(x)` is the bytes of a variable or a slice as a `[]U8`,
248+ of a type a view may take (sections 6.8, 6.9 and 7.3). It breaks no
249+ program.