raz / commit bbc103eb

commit
bbc103eb79f91c564ffe3bddcf412f44f9141f0b
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
92592e25
A procedure type's parameter or result may name a type declared later in its type section, as report 2026-10.36 has it: laterType gives a placeholder, as a pointer's base had, and resolveLaters puts the type in its place at the section's end, laying several results out again; Sym.Type's fwd is gone

patch · log

13 files changed, +275 -58

CLAUDE.md modified +9 -2

@@ -305,11 +305,18 @@
305305 - `Width` follows glibc's `wcwidth` but gives a control 0, and 0 to the
306306 format characters glibc counts as 1 (Egyptian hieroglyph controls,
307307 interlinear annotation), but for the prepended concatenation marks.
308+- A pointer's base, and a procedure type's parameter or result (report
309+ 2026-10.36, 4 and 5.9), may name a type declared later in its `type`
310+ section, if the name is a bare one that nothing in scope declares yet
311+ (`[]Later` or `[2]Later` doesn't wait): a placeholder of form `fNone`
312+ stands for it until the section's end (`laterType`,
313+ `resolveLaters`), which lays several results out again. A procedure
314+ type outside a `type` section, a variable's, waits for nothing.
308315 - An `extern` module variable is read-only for an importer, as the
309316 report says of every exported variable (3).
310317 - Warnings come only for a module with no errors. A procedure naming
311- itself doesn't use it; a type named by a pointer in its own
312- declaration does. An enumeration's names are never warned of, nor a
318+ itself doesn't use it; a type named by a pointer, or by a procedure
319+ type's parameter or result, in its own declaration does. An enumeration's names are never warned of, nor a
313320 generic module's parameters; a generic module is warned of in its
314321 first instance only. Assigning a variable whole (`x :=`, `a, b :=`)
315322 doesn't read it; anything else does.

conform/err/LaterMissing.raz (history) added +6 -0

@@ -0,0 +1,6 @@
1+module LaterMissing;
2+type
3+ R = record
4+ each: proc(var r: R; m: Missing);
5+ end;
6+end LaterMissing.

conform/err/LaterOwnField.raz (history) added +8 -0

@@ -0,0 +1,8 @@
1+module LaterOwnField;
2+(* a field of the record's own type, by value, is still an error (5.9) *)
3+type
4+ R = record
5+ each: proc(var r: R);
6+ r: R;
7+ end;
8+end LaterOwnField.

conform/err/LaterVar.raz (history) added +8 -0

@@ -0,0 +1,8 @@
1+module LaterVar;
2+(* only a type section may name a type declared later (4) *)
3+var v: proc(var r: R);
4+type
5+ R = record
6+ n: INT;
7+ end;
8+end LaterVar.

conform/run/LaterCallback.expect (history) added +6 -0

@@ -0,0 +1,6 @@
1+5
2+50 5
3+5 5
4+1 2
5+2
6+6

conform/run/LaterCallback.raz (history) added +89 -0

@@ -0,0 +1,89 @@
1+module LaterCallback;
2+(* A procedure type's parameter or result may name a type declared later
3+ in its type section, however passed (report, 4 and 5.9). *)
4+import Out;
5+
6+type
7+ Lines = record
8+ each: proc(var r: Lines; s: []U8);
9+ look: proc(const r: Lines): INT;
10+ copy: proc(r: Lines): Lines;
11+ at: proc(p: ^Lines): (INT, Lines);
12+ n: INT;
13+ end;
14+
15+ (* two records whose callbacks name each other *)
16+ Ping = record
17+ hit: proc(var p: Pong): INT;
18+ sent: INT;
19+ end;
20+ Pong = record
21+ back: proc(var p: Ping; q: Pong);
22+ got: INT;
23+ end;
24+
25+ (* a procedure type of its own, naming a later record *)
26+ Visit = proc(t: Tree; depth: INT): Tree;
27+ Tree = record
28+ size: INT;
29+ name: [8]U8;
30+ end;
31+
32+proc count(var r: Lines; s: []U8);
33+begin
34+ INC(r.n, LEN(s));
35+end;
36+
37+proc look(const r: Lines): INT;
38+begin
39+ return r.n;
40+end;
41+
42+proc copy(r: Lines): Lines;
43+begin
44+ r.n := r.n * 10;
45+ return r;
46+end;
47+
48+proc at(p: ^Lines): (INT, Lines);
49+begin
50+ return p^.n, p^;
51+end;
52+
53+proc hit(var p: Pong): INT;
54+begin
55+ INC(p.got);
56+ return p.got;
57+end;
58+
59+proc back(var p: Ping; q: Pong);
60+begin
61+ p.sent := p.sent + q.got;
62+end;
63+
64+proc grow(t: Tree; depth: INT): Tree;
65+begin
66+ t.size := t.size + depth;
67+ return t;
68+end;
69+
70+var
71+ r, c: Lines; k: INT; pi: Ping; po: Pong; v: Visit; t: Tree;
72+begin
73+ r.each := count; r.look := look; r.copy := copy; r.at := at;
74+ r.each(r, "abc");
75+ r.each(r, "de");
76+ Out.int(r.look(r)); Out.ln;
77+ c := r.copy(r);
78+ Out.int(c.n); Out.char(' '); Out.int(r.n); Out.ln;
79+ k, c := r.at(ADR(r));
80+ Out.int(k); Out.char(' '); Out.int(c.n); Out.ln;
81+ pi.hit := hit; po.back := back;
82+ Out.int(pi.hit(po)); Out.char(' '); Out.int(pi.hit(po)); Out.ln;
83+ po.back(pi, po);
84+ Out.int(pi.sent); Out.ln;
85+ v := grow;
86+ t.size := 1;
87+ t := v(v(t, 2), 3);
88+ Out.int(t.size); Out.ln;
89+end LaterCallback.

doc/compiler.md (history) modified +16 -3

@@ -18,12 +18,12 @@
1818 |---|---|---|---|
1919 | `src/Diag.raz` | 422 | 324 | Messages: positions, source excerpts, colour, counts |
2020 | `src/Scan.raz` | 1,169 | 965 | Symbols, literals, comments, positions |
21-| `src/Sym.raz` | 725 | 583 | Types, objects, scopes, layout |
21+| `src/Sym.raz` | 723 | 581 | Types, objects, scopes, layout |
2222 | `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes |
2323 | `src/X64.raz` | 1,605 | 1,318 | The instruction encoder, jumps within a procedure, the assembler |
2424 | `src/Gen.raz` | 2,797 | 1,961 | The stack machine, for x86-64: values, operations, checks, calls, frames |
2525 | `src/Item.raz` | 2,414 | 1,938 | Items: types, constants, variables, operators, conversions, calls |
26-| `src/Parse.raz` | 4,528 | 3,900 | Declarations, statements, expressions, built-ins, recovery, warnings |
26+| `src/Parse.raz` | 4,592 | 3,942 | Declarations, statements, expressions, built-ins, recovery, warnings |
2727 | `src/Link.raz` | 407 | 315 | Tree shaking, layout, patching, the places |
2828 | `src/Elf.raz` | 449 | 355 | Static and dynamic ELF, symbol table, line numbers |
2929 | `src/Raz.raz` | 418 | 333 | The command line |
@@ -1704,7 +1704,20 @@
17041704 rdx, and has gen's `codeProc` run
17051705 the target's assembler) and bodies (`body`).
17061706 `parseType` and its helpers (`recordType`, `variantType`, `signature`,
1707-`enumType`) build `Sym.Type`s. `qualIdent` reads a name, `M.x` too, and
1707+`enumType`) build `Sym.Type`s. In a `type` section (`inTypes`) a
1708+pointer's base and a procedure type's parameter or result may name a
1709+type declared later (report, 4), so that two records can point at each
1710+other and a record's callback can take the record: `laterType` reads
1711+the name, and if nothing is declared by it yet gives a placeholder, a
1712+type of form `fNone` of its own, noting it in `laters` with its owner,
1713+the pointer or the procedure type. When the section ends,
1714+`resolveLaters` puts the type declared in each placeholder's place, in
1715+the owner's base, parameters or results, and lays several results out
1716+again (`layOut`), as their sizes are known only now. Nothing waits on
1717+the size: a pointer is a word and a procedure value an address, and no
1718+call is made inside a `type` section. A field by value is read by
1719+`parseType` alone, so a record holding itself is still "isn't
1720+declared". `qualIdent` reads a name, `M.x` too, and
17081721 `Elem.bold`, a name of an enumeration with its type (`enumName`); it
17091722 refuses a name two enumerations share (`twinError`). An arm of a `match`
17101723 on an enumeration or a variant names its case (`armCase`): a name

src/Parse.raz (history) modified +114 -50

@@ -53,6 +53,17 @@
5353 at: Diag.Pos; (* the import *)
5454 end;
5555
56+ (* A type a type section names before it declares it (report, 4):
57+ a pointer's base, or a procedure type's parameter or result. A
58+ placeholder stands for it in its owner until the section's end,
59+ when resolveLaters puts the type declared in its place. *)
60+ Later = record
61+ name: INT; (* the name of the type *)
62+ at: Diag.Pos; (* where it is named *)
63+ held: PType; (* the placeholder, a type of form fNone *)
64+ owner: PType; (* the pointer or procedure type that holds it *)
65+ end;
66+
5667 const
5768 (* Whether the target has reals (report, appendix A): x86-64 always has
5869 SSE2. Without them, used code that computes with one is an error,
@@ -60,7 +71,7 @@
6071 HasReals* = true;
6172
6273 MaxNames = 64; (* names in one declaration, and the like *)
63- MaxFwd = 256; (* pointers to types not yet declared *)
74+ MaxLater = 256; (* types named before they are declared *)
6475 MaxDefers = 64; (* defer statements in force *)
6576 MaxDoubts = 4096; (* places to warn of, noted while a module is read *)
6677 MaxDeferred = 64; (* generic imports not yet made *)
@@ -81,10 +92,9 @@
8192 firstModule*, lastModule*: PModule; (* in the order they were finished *)
8293
8394 checkMode: BOOL; (* reading a file only for its interface *)
84- inTypes: BOOL; (* in a type section: ^T may name a later type *)
85- fwdTypes: [MaxFwd]PType;
86- fwdPos: [MaxFwd]Diag.Pos; (* where each names its type *)
87- numFwd: INT;
95+ inTypes: BOOL; (* in a type section: ^T, and a procedure type, may name a later type *)
96+ laters: [MaxLater]Later;
97+ numLater: INT;
8898 lastCalled: PObj; (* the procedure the last call was of, if it had a name *)
8999 passOn: BOOL; (* the next factor may be a call with several results, for return *)
90100 curProc: PObj; (* the procedure being compiled, or nil *)
@@ -600,6 +610,86 @@
600610
601611 (* Types *)
602612
613+(* Lays the results t holds out as C would, each field at its offset;
614+ again by resolveLaters, if one names a type declared later. *)
615+proc layOut(t: PType);
616+var f: PObj; off: I64;
617+begin
618+ off := 0;
619+ t^.align := 1;
620+ f := t^.fields;
621+ while f # nil do
622+ off := Sym.alignUp(off, f^.typ^.align);
623+ f^.val := off;
624+ off := off + f^.typ^.size;
625+ if f^.typ^.align > t^.align then t^.align := f^.typ^.align; end;
626+ f := f^.next;
627+ end;
628+ t^.size := Sym.alignUp(off, t^.align);
629+end;
630+
631+(* A type that owner, a pointer or a procedure type, holds. In a type
632+ section it may name a type not declared yet (report, 4): a
633+ placeholder of its own stands for it, noted in laters, and
634+ resolveLaters puts the type in its place at the section's end. A
635+ pointer is a word and a procedure value an address, so neither's
636+ size waits for it. *)
637+proc laterType(owner: PType): (PType, ERROR);
638+var held: PType;
639+begin
640+ if not inTypes or (Scan.tok # Scan.sIdent) or (Sym.lookup(Scan.id) # nil) then return parseType(); end;
641+ if numLater >= MaxLater then return nil, Scan.mark(tooMany(MaxLater, "types named before they are declared")); end;
642+ held := Sym.newType(Sym.fNone, 0, 1);
643+ laters[numLater].name := Scan.id;
644+ laters[numLater].at := Scan.tokPos();
645+ laters[numLater].held := held;
646+ laters[numLater].owner := owner;
647+ INC(numLater);
648+ Scan.next;
649+ return held, nil;
650+end;
651+
652+(* Gives t to each of the list f whose type is the placeholder held *)
653+proc putFor(f: PObj; held, t: PType);
654+begin
655+ while f # nil do
656+ if f^.typ = held then f^.typ := t; end;
657+ f := f^.next;
658+ end;
659+end;
660+
661+(* Puts in place of each placeholder of laters the type it names, now
662+ that the type section is read: in a pointer's base, a procedure
663+ type's parameters and result, and its results, laid out again as
664+ their sizes are now known. A name that isn't a type is poison. *)
665+proc resolveLaters;
666+var i: INT; o: PObj; held, t, owner: PType;
667+begin
668+ for i := 0 to numLater - 1 do
669+ held := laters[i].held;
670+ owner := laters[i].owner;
671+ o := Sym.lookup(laters[i].name);
672+ if (o # nil) and (o^.typ = Sym.tPoison) then
673+ t := Sym.tPoison; (* its declaration had an error, told already *)
674+ elsif (o = nil) or (o^.cls # Sym.oType) then
675+ notDeclared(laters[i].at, laters[i].name);
676+ t := Sym.tPoison;
677+ else
678+ t := o^.typ;
679+ named(o);
680+ end;
681+ if owner^.base = held then owner^.base := t; end;
682+ if owner^.form = Sym.fProc then
683+ putFor(owner^.fields, held, t);
684+ if (owner^.base # nil) and owner^.base^.tuple then
685+ putFor(owner^.base^.fields, held, t);
686+ layOut(owner^.base);
687+ end;
688+ end;
689+ end;
690+ numLater := 0;
691+end;
692+
603693 proc recordType: (PType, ERROR);
604694 var
605695 t, ft: PType; f, last: PObj; names: [MaxNames]INT;
@@ -699,23 +789,19 @@
699789 return t, nil;
700790 end;
701791
702-(* Several results: a record of them, laid out as C would, and
703- returned in rax and rdx if they are two words (inTwoRegs), or else
704- through a hidden pointer. *)
705-proc results: (PType, ERROR);
706-var t, ft: PType; f, last: PObj; off: I64;
792+(* The results of the procedure type sig: a record of them, laid out
793+ as C would, and returned in rax and rdx if they are two words
794+ (inTwoRegs), or else through a hidden pointer. One alone in
795+ parentheses is the result itself. *)
796+proc results(sig: PType): (PType, ERROR);
797+var t: PType; f, last: PObj;
707798 begin
708799 Scan.next;
709800 t := Sym.newType(Sym.fRecord, 0, 1);
710801 t^.tuple := true;
711- off := 0; last := nil;
802+ last := nil;
712803 repeat
713- ft := try parseType();
714- off := Sym.alignUp(off, ft^.align);
715- f := Sym.newObj(Sym.oField, -1, ft);
716- f^.val := off;
717- off := off + ft^.size;
718- if ft^.align > t^.align then t^.align := ft^.align; end;
804+ f := Sym.newObj(Sym.oField, -1, try laterType(sig));
719805 if last = nil then t^.fields := f; else last^.next := f; end;
720806 last := f;
721807 INC(t^.len);
@@ -723,8 +809,8 @@
723809 Scan.next;
724810 until false;
725811 try expect(Scan.sRparen);
726- t^.size := Sym.alignUp(off, t^.align);
727812 if t^.len = 1 then return t^.fields^.typ, nil; end;
813+ layOut(t);
728814 return t, nil;
729815 end;
730816
@@ -762,7 +848,7 @@
762848 Scan.next;
763849 until false;
764850 try expect(Scan.sColon);
765- pt := try parseType();
851+ pt := try laterType(t);
766852 while first # nil do first^.typ := pt; first := first^.next; end;
767853 if Scan.tok # Scan.sSemicolon then break; end;
768854 Scan.next;
@@ -773,7 +859,7 @@
773859 t^.len := n;
774860 if Scan.tok = Scan.sColon then
775861 Scan.next;
776- if Scan.tok = Scan.sLparen then t^.base := try results(); else t^.base := try parseType(); end;
862+ if Scan.tok = Scan.sLparen then t^.base := try results(t); else t^.base := try laterType(t); end;
777863 end;
778864 return t, nil;
779865 end;
@@ -854,20 +940,8 @@
854940 Scan.next;
855941 vol := Scan.tok = Scan.sVolatile;
856942 if vol then Scan.next; end;
857- if inTypes and (Scan.tok = Scan.sIdent) and (Sym.lookup(Scan.id) = nil) then
858- (* a type declared later in this section *)
859- t := newPtr(nil, vol);
860- t^.fwd := Scan.id;
861- if numFwd >= MaxFwd then
862- return nil, Scan.mark(tooMany(MaxFwd, "pointers to types not yet declared"));
863- end;
864- fwdTypes[numFwd] := t;
865- fwdPos[numFwd] := Scan.tokPos();
866- INC(numFwd);
867- Scan.next;
868- else
869- t := newPtr(try parseType(), vol);
870- end;
943+ t := newPtr(nil, vol);
944+ t^.base := try laterType(t);
871945 | sPacked, sRecord: t := try recordType();
872946 | sVariant: t := try variantType();
873947 | sProc: Scan.next; t := try signature();
@@ -3765,7 +3839,7 @@
37653839 end;
37663840
37673841 proc declarations(top: BOOL);
3768-var i: INT; o: PObj; k: Scan.Sym;
3842+var k: Scan.Sym;
37693843 begin
37703844 repeat
37713845 k := Scan.tok;
@@ -3775,20 +3849,10 @@
37753849 while Scan.tok = Scan.sIdent do declaration(k, top); end;
37763850 | sType:
37773851 Scan.next;
3778- numFwd := 0;
3852+ numLater := 0;
37793853 while Scan.tok = Scan.sIdent do declaration(Scan.sType, top); end;
37803854 inTypes := false;
3781- for i := 0 to numFwd - 1 do
3782- o := Sym.lookup(fwdTypes[i]^.fwd);
3783- if (o = nil) or (o^.cls # Sym.oType) then
3784- notDeclared(fwdPos[i], fwdTypes[i]^.fwd);
3785- fwdTypes[i]^.base := Sym.tPoison;
3786- else
3787- fwdTypes[i]^.base := o^.typ;
3788- named(o);
3789- end;
3790- fwdTypes[i]^.fwd := -1;
3791- end;
3855+ resolveLaters();
37923856 | sProc, sExtern: declaration(k, top);
37933857 | else: return;
37943858 end;
@@ -4420,7 +4484,7 @@
44204484 proc makeInstance(i: INT; at: Diag.Pos): ERROR;
44214485 var
44224486 d: Deferred; st: Scan.State; args: Sym.Args; types, exported, ext, wasQuiet: BOOL; nf, nd: INT;
4423- ft: [MaxFwd]PType; fp: [MaxFwd]Diag.Pos; decl: [MaxNames]INT;
4487+ lt: [MaxLater]Later; decl: [MaxNames]INT;
44244488 begin
44254489 d := deferred[i];
44264490 DEC(numDeferred);
@@ -4434,10 +4498,10 @@
44344498 Scan.restoreScan(st);
44354499 args.at := d.at;
44364500 types := inTypes; exported := declExported; ext := inExtern; wasQuiet := Diag.quiet;
4437- nf := numFwd; ft := fwdTypes; fp := fwdPos; nd := numDeclaring; decl := declaring;
4501+ nf := numLater; lt := laters; nd := numDeclaring; decl := declaring;
44384502 d.o^.home := try importModule(d.mname, args);
44394503 inTypes := types; declExported := exported; inExtern := ext; Diag.setQuiet(wasQuiet);
4440- numFwd := nf; fwdTypes := ft; fwdPos := fp; numDeclaring := nd; declaring := decl;
4504+ numLater := nf; laters := lt; numDeclaring := nd; declaring := decl;
44414505 addImport(Sym.curModule, d.o^.home);
44424506 return nil;
44434507 end;

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

@@ -39,7 +39,6 @@
3939 len*: I64; (* fArray: elements; fProc: parameters; fVariant: cases; a tuple: results *)
4040 fields*: ^Obj; (* fRecord: fields; fVariant: cases; fProc: parameters *)
4141 scope*: INT; (* fRecord: the scope of its fields *)
42- fwd*: INT; (* fPtr whose base isn't declared yet: its name, else -1 *)
4342 obj*: ^Obj; (* the declaration that names it, or nil *)
4443 home*: ^Module; (* the module that built it *)
4544 end;
@@ -163,7 +162,6 @@
163162 t^.form := form;
164163 t^.size := size;
165164 t^.align := align;
166- t^.fwd := -1;
167165 t^.home := curModule;
168166 return t;
169167 end;

test/err/DeclFwdPtrs.err (history) modified +1 -1

@@ -1,4 +1,4 @@
1-test/err/DeclFwdPtrs.raz:35:11: error: more than 256 pointers to types not yet declared
1+test/err/DeclFwdPtrs.raz:35:11: error: more than 256 types named before they are declared
22 35 | P256 = ^T256;
33 | ^^^^
44 raz: 1 error

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

@@ -0,0 +1,4 @@
1+conform/err/LaterMissing.raz:4:33: error: Missing isn't declared
2+ 4 | each: proc(var r: R; m: Missing);
3+ | ^^^^^^^
4+raz: 1 error

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

@@ -0,0 +1,4 @@
1+conform/err/LaterOwnField.raz:6:12: error: R isn't declared
2+ 6 | r: R;
3+ | ^
4+raz: 1 error

test/msg/LaterVar.err (history) added +10 -0

@@ -0,0 +1,10 @@
1+conform/err/LaterVar.raz:3:20: error: R isn't declared
2+ 3 | var v: proc(var r: R);
3+ | ^
4+conform/err/LaterVar.raz:5:5: error: R is declared twice
5+ 5 | R = record
6+ | ^
7+conform/err/LaterVar.raz:3:20: note: the first one is here
8+ 3 | var v: proc(var r: R);
9+ | ^
10+raz: 2 errors