From bbc103eb79f91c564ffe3bddcf412f44f9141f0b Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Fri, 23 Oct 2026 13:53:01 +0100 Subject: [PATCH] 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 --- diff --git a/CLAUDE.md b/CLAUDE.md index d64846d..2f506fe 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -305,11 +305,18 @@ - `Width` follows glibc's `wcwidth` but gives a control 0, and 0 to the format characters glibc counts as 1 (Egyptian hieroglyph controls, interlinear annotation), but for the prepended concatenation marks. +- A pointer's base, and a procedure type's parameter or result (report + 2026-10.36, 4 and 5.9), may name a type declared later in its `type` + section, if the name is a bare one that nothing in scope declares yet + (`[]Later` or `[2]Later` doesn't wait): a placeholder of form `fNone` + stands for it until the section's end (`laterType`, + `resolveLaters`), which lays several results out again. A procedure + type outside a `type` section, a variable's, waits for nothing. - An `extern` module variable is read-only for an importer, as the report says of every exported variable (3). - Warnings come only for a module with no errors. A procedure naming - itself doesn't use it; a type named by a pointer in its own - declaration does. An enumeration's names are never warned of, nor a + itself doesn't use it; a type named by a pointer, or by a procedure + type's parameter or result, in its own declaration does. An enumeration's names are never warned of, nor a generic module's parameters; a generic module is warned of in its first instance only. Assigning a variable whole (`x :=`, `a, b :=`) doesn't read it; anything else does. diff --git a/conform/err/LaterMissing.raz b/conform/err/LaterMissing.raz new file mode 100644 index 0000000..4883521 --- /dev/null +++ b/conform/err/LaterMissing.raz @@ -0,0 +1,6 @@ +module LaterMissing; +type + R = record + each: proc(var r: R; m: Missing); + end; +end LaterMissing. diff --git a/conform/err/LaterOwnField.raz b/conform/err/LaterOwnField.raz new file mode 100644 index 0000000..d8570b7 --- /dev/null +++ b/conform/err/LaterOwnField.raz @@ -0,0 +1,8 @@ +module LaterOwnField; +(* a field of the record's own type, by value, is still an error (5.9) *) +type + R = record + each: proc(var r: R); + r: R; + end; +end LaterOwnField. diff --git a/conform/err/LaterVar.raz b/conform/err/LaterVar.raz new file mode 100644 index 0000000..faf1d91 --- /dev/null +++ b/conform/err/LaterVar.raz @@ -0,0 +1,8 @@ +module LaterVar; +(* only a type section may name a type declared later (4) *) +var v: proc(var r: R); +type + R = record + n: INT; + end; +end LaterVar. diff --git a/conform/run/LaterCallback.expect b/conform/run/LaterCallback.expect new file mode 100644 index 0000000..26ef451 --- /dev/null +++ b/conform/run/LaterCallback.expect @@ -0,0 +1,6 @@ +5 +50 5 +5 5 +1 2 +2 +6 diff --git a/conform/run/LaterCallback.raz b/conform/run/LaterCallback.raz new file mode 100644 index 0000000..9bc48e0 --- /dev/null +++ b/conform/run/LaterCallback.raz @@ -0,0 +1,89 @@ +module LaterCallback; +(* A procedure type's parameter or result may name a type declared later + in its type section, however passed (report, 4 and 5.9). *) +import Out; + +type + Lines = record + each: proc(var r: Lines; s: []U8); + look: proc(const r: Lines): INT; + copy: proc(r: Lines): Lines; + at: proc(p: ^Lines): (INT, Lines); + n: INT; + end; + + (* two records whose callbacks name each other *) + Ping = record + hit: proc(var p: Pong): INT; + sent: INT; + end; + Pong = record + back: proc(var p: Ping; q: Pong); + got: INT; + end; + + (* a procedure type of its own, naming a later record *) + Visit = proc(t: Tree; depth: INT): Tree; + Tree = record + size: INT; + name: [8]U8; + end; + +proc count(var r: Lines; s: []U8); +begin + INC(r.n, LEN(s)); +end; + +proc look(const r: Lines): INT; +begin + return r.n; +end; + +proc copy(r: Lines): Lines; +begin + r.n := r.n * 10; + return r; +end; + +proc at(p: ^Lines): (INT, Lines); +begin + return p^.n, p^; +end; + +proc hit(var p: Pong): INT; +begin + INC(p.got); + return p.got; +end; + +proc back(var p: Ping; q: Pong); +begin + p.sent := p.sent + q.got; +end; + +proc grow(t: Tree; depth: INT): Tree; +begin + t.size := t.size + depth; + return t; +end; + +var + r, c: Lines; k: INT; pi: Ping; po: Pong; v: Visit; t: Tree; +begin + r.each := count; r.look := look; r.copy := copy; r.at := at; + r.each(r, "abc"); + r.each(r, "de"); + Out.int(r.look(r)); Out.ln; + c := r.copy(r); + Out.int(c.n); Out.char(' '); Out.int(r.n); Out.ln; + k, c := r.at(ADR(r)); + Out.int(k); Out.char(' '); Out.int(c.n); Out.ln; + pi.hit := hit; po.back := back; + Out.int(pi.hit(po)); Out.char(' '); Out.int(pi.hit(po)); Out.ln; + po.back(pi, po); + Out.int(pi.sent); Out.ln; + v := grow; + t.size := 1; + t := v(v(t, 2), 3); + Out.int(t.size); Out.ln; +end LaterCallback. diff --git a/doc/compiler.md b/doc/compiler.md index a962732..d063e49 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -18,12 +18,12 @@ |---|---|---|---| | `src/Diag.raz` | 422 | 324 | Messages: positions, source excerpts, colour, counts | | `src/Scan.raz` | 1,169 | 965 | Symbols, literals, comments, positions | -| `src/Sym.raz` | 725 | 583 | Types, objects, scopes, layout | +| `src/Sym.raz` | 723 | 581 | Types, objects, scopes, layout | | `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes | | `src/X64.raz` | 1,605 | 1,318 | The instruction encoder, jumps within a procedure, the assembler | | `src/Gen.raz` | 2,797 | 1,961 | The stack machine, for x86-64: values, operations, checks, calls, frames | | `src/Item.raz` | 2,414 | 1,938 | Items: types, constants, variables, operators, conversions, calls | -| `src/Parse.raz` | 4,528 | 3,900 | Declarations, statements, expressions, built-ins, recovery, warnings | +| `src/Parse.raz` | 4,592 | 3,942 | Declarations, statements, expressions, built-ins, recovery, warnings | | `src/Link.raz` | 407 | 315 | Tree shaking, layout, patching, the places | | `src/Elf.raz` | 449 | 355 | Static and dynamic ELF, symbol table, line numbers | | `src/Raz.raz` | 418 | 333 | The command line | @@ -1704,7 +1704,20 @@ rdx, and has gen's `codeProc` run the target's assembler) and bodies (`body`). `parseType` and its helpers (`recordType`, `variantType`, `signature`, -`enumType`) build `Sym.Type`s. `qualIdent` reads a name, `M.x` too, and +`enumType`) build `Sym.Type`s. In a `type` section (`inTypes`) a +pointer's base and a procedure type's parameter or result may name a +type declared later (report, 4), so that two records can point at each +other and a record's callback can take the record: `laterType` reads +the name, and if nothing is declared by it yet gives a placeholder, a +type of form `fNone` of its own, noting it in `laters` with its owner, +the pointer or the procedure type. When the section ends, +`resolveLaters` puts the type declared in each placeholder's place, in +the owner's base, parameters or results, and lays several results out +again (`layOut`), as their sizes are known only now. Nothing waits on +the size: a pointer is a word and a procedure value an address, and no +call is made inside a `type` section. A field by value is read by +`parseType` alone, so a record holding itself is still "isn't +declared". `qualIdent` reads a name, `M.x` too, and `Elem.bold`, a name of an enumeration with its type (`enumName`); it refuses a name two enumerations share (`twinError`). An arm of a `match` on an enumeration or a variant names its case (`armCase`): a name diff --git a/src/Parse.raz b/src/Parse.raz index a087b80..e1f6623 100644 --- a/src/Parse.raz +++ b/src/Parse.raz @@ -53,6 +53,17 @@ at: Diag.Pos; (* the import *) end; + (* A type a type section names before it declares it (report, 4): + a pointer's base, or a procedure type's parameter or result. A + placeholder stands for it in its owner until the section's end, + when resolveLaters puts the type declared in its place. *) + Later = record + name: INT; (* the name of the type *) + at: Diag.Pos; (* where it is named *) + held: PType; (* the placeholder, a type of form fNone *) + owner: PType; (* the pointer or procedure type that holds it *) + end; + const (* Whether the target has reals (report, appendix A): x86-64 always has SSE2. Without them, used code that computes with one is an error, @@ -60,7 +71,7 @@ HasReals* = true; MaxNames = 64; (* names in one declaration, and the like *) - MaxFwd = 256; (* pointers to types not yet declared *) + MaxLater = 256; (* types named before they are declared *) MaxDefers = 64; (* defer statements in force *) MaxDoubts = 4096; (* places to warn of, noted while a module is read *) MaxDeferred = 64; (* generic imports not yet made *) @@ -81,10 +92,9 @@ firstModule*, lastModule*: PModule; (* in the order they were finished *) checkMode: BOOL; (* reading a file only for its interface *) - inTypes: BOOL; (* in a type section: ^T may name a later type *) - fwdTypes: [MaxFwd]PType; - fwdPos: [MaxFwd]Diag.Pos; (* where each names its type *) - numFwd: INT; + inTypes: BOOL; (* in a type section: ^T, and a procedure type, may name a later type *) + laters: [MaxLater]Later; + numLater: INT; lastCalled: PObj; (* the procedure the last call was of, if it had a name *) passOn: BOOL; (* the next factor may be a call with several results, for return *) curProc: PObj; (* the procedure being compiled, or nil *) @@ -600,6 +610,86 @@ (* Types *) +(* Lays the results t holds out as C would, each field at its offset; + again by resolveLaters, if one names a type declared later. *) +proc layOut(t: PType); +var f: PObj; off: I64; +begin + off := 0; + t^.align := 1; + f := t^.fields; + while f # nil do + off := Sym.alignUp(off, f^.typ^.align); + f^.val := off; + off := off + f^.typ^.size; + if f^.typ^.align > t^.align then t^.align := f^.typ^.align; end; + f := f^.next; + end; + t^.size := Sym.alignUp(off, t^.align); +end; + +(* A type that owner, a pointer or a procedure type, holds. In a type + section it may name a type not declared yet (report, 4): a + placeholder of its own stands for it, noted in laters, and + resolveLaters puts the type in its place at the section's end. A + pointer is a word and a procedure value an address, so neither's + size waits for it. *) +proc laterType(owner: PType): (PType, ERROR); +var held: PType; +begin + if not inTypes or (Scan.tok # Scan.sIdent) or (Sym.lookup(Scan.id) # nil) then return parseType(); end; + if numLater >= MaxLater then return nil, Scan.mark(tooMany(MaxLater, "types named before they are declared")); end; + held := Sym.newType(Sym.fNone, 0, 1); + laters[numLater].name := Scan.id; + laters[numLater].at := Scan.tokPos(); + laters[numLater].held := held; + laters[numLater].owner := owner; + INC(numLater); + Scan.next; + return held, nil; +end; + +(* Gives t to each of the list f whose type is the placeholder held *) +proc putFor(f: PObj; held, t: PType); +begin + while f # nil do + if f^.typ = held then f^.typ := t; end; + f := f^.next; + end; +end; + +(* Puts in place of each placeholder of laters the type it names, now + that the type section is read: in a pointer's base, a procedure + type's parameters and result, and its results, laid out again as + their sizes are now known. A name that isn't a type is poison. *) +proc resolveLaters; +var i: INT; o: PObj; held, t, owner: PType; +begin + for i := 0 to numLater - 1 do + held := laters[i].held; + owner := laters[i].owner; + o := Sym.lookup(laters[i].name); + if (o # nil) and (o^.typ = Sym.tPoison) then + t := Sym.tPoison; (* its declaration had an error, told already *) + elsif (o = nil) or (o^.cls # Sym.oType) then + notDeclared(laters[i].at, laters[i].name); + t := Sym.tPoison; + else + t := o^.typ; + named(o); + end; + if owner^.base = held then owner^.base := t; end; + if owner^.form = Sym.fProc then + putFor(owner^.fields, held, t); + if (owner^.base # nil) and owner^.base^.tuple then + putFor(owner^.base^.fields, held, t); + layOut(owner^.base); + end; + end; + end; + numLater := 0; +end; + proc recordType: (PType, ERROR); var t, ft: PType; f, last: PObj; names: [MaxNames]INT; @@ -699,23 +789,19 @@ return t, nil; end; -(* Several results: a record of them, laid out as C would, and - returned in rax and rdx if they are two words (inTwoRegs), or else - through a hidden pointer. *) -proc results: (PType, ERROR); -var t, ft: PType; f, last: PObj; off: I64; +(* The results of the procedure type sig: a record of them, laid out + as C would, and returned in rax and rdx if they are two words + (inTwoRegs), or else through a hidden pointer. One alone in + parentheses is the result itself. *) +proc results(sig: PType): (PType, ERROR); +var t: PType; f, last: PObj; begin Scan.next; t := Sym.newType(Sym.fRecord, 0, 1); t^.tuple := true; - off := 0; last := nil; + last := nil; repeat - ft := try parseType(); - off := Sym.alignUp(off, ft^.align); - f := Sym.newObj(Sym.oField, -1, ft); - f^.val := off; - off := off + ft^.size; - if ft^.align > t^.align then t^.align := ft^.align; end; + f := Sym.newObj(Sym.oField, -1, try laterType(sig)); if last = nil then t^.fields := f; else last^.next := f; end; last := f; INC(t^.len); @@ -723,8 +809,8 @@ Scan.next; until false; try expect(Scan.sRparen); - t^.size := Sym.alignUp(off, t^.align); if t^.len = 1 then return t^.fields^.typ, nil; end; + layOut(t); return t, nil; end; @@ -762,7 +848,7 @@ Scan.next; until false; try expect(Scan.sColon); - pt := try parseType(); + pt := try laterType(t); while first # nil do first^.typ := pt; first := first^.next; end; if Scan.tok # Scan.sSemicolon then break; end; Scan.next; @@ -773,7 +859,7 @@ t^.len := n; if Scan.tok = Scan.sColon then Scan.next; - if Scan.tok = Scan.sLparen then t^.base := try results(); else t^.base := try parseType(); end; + if Scan.tok = Scan.sLparen then t^.base := try results(t); else t^.base := try laterType(t); end; end; return t, nil; end; @@ -854,20 +940,8 @@ Scan.next; vol := Scan.tok = Scan.sVolatile; if vol then Scan.next; end; - if inTypes and (Scan.tok = Scan.sIdent) and (Sym.lookup(Scan.id) = nil) then - (* a type declared later in this section *) - t := newPtr(nil, vol); - t^.fwd := Scan.id; - if numFwd >= MaxFwd then - return nil, Scan.mark(tooMany(MaxFwd, "pointers to types not yet declared")); - end; - fwdTypes[numFwd] := t; - fwdPos[numFwd] := Scan.tokPos(); - INC(numFwd); - Scan.next; - else - t := newPtr(try parseType(), vol); - end; + t := newPtr(nil, vol); + t^.base := try laterType(t); | sPacked, sRecord: t := try recordType(); | sVariant: t := try variantType(); | sProc: Scan.next; t := try signature(); @@ -3765,7 +3839,7 @@ end; proc declarations(top: BOOL); -var i: INT; o: PObj; k: Scan.Sym; +var k: Scan.Sym; begin repeat k := Scan.tok; @@ -3775,20 +3849,10 @@ while Scan.tok = Scan.sIdent do declaration(k, top); end; | sType: Scan.next; - numFwd := 0; + numLater := 0; while Scan.tok = Scan.sIdent do declaration(Scan.sType, top); end; inTypes := false; - for i := 0 to numFwd - 1 do - o := Sym.lookup(fwdTypes[i]^.fwd); - if (o = nil) or (o^.cls # Sym.oType) then - notDeclared(fwdPos[i], fwdTypes[i]^.fwd); - fwdTypes[i]^.base := Sym.tPoison; - else - fwdTypes[i]^.base := o^.typ; - named(o); - end; - fwdTypes[i]^.fwd := -1; - end; + resolveLaters(); | sProc, sExtern: declaration(k, top); | else: return; end; @@ -4420,7 +4484,7 @@ proc makeInstance(i: INT; at: Diag.Pos): ERROR; var d: Deferred; st: Scan.State; args: Sym.Args; types, exported, ext, wasQuiet: BOOL; nf, nd: INT; - ft: [MaxFwd]PType; fp: [MaxFwd]Diag.Pos; decl: [MaxNames]INT; + lt: [MaxLater]Later; decl: [MaxNames]INT; begin d := deferred[i]; DEC(numDeferred); @@ -4434,10 +4498,10 @@ Scan.restoreScan(st); args.at := d.at; types := inTypes; exported := declExported; ext := inExtern; wasQuiet := Diag.quiet; - nf := numFwd; ft := fwdTypes; fp := fwdPos; nd := numDeclaring; decl := declaring; + nf := numLater; lt := laters; nd := numDeclaring; decl := declaring; d.o^.home := try importModule(d.mname, args); inTypes := types; declExported := exported; inExtern := ext; Diag.setQuiet(wasQuiet); - numFwd := nf; fwdTypes := ft; fwdPos := fp; numDeclaring := nd; declaring := decl; + numLater := nf; laters := lt; numDeclaring := nd; declaring := decl; addImport(Sym.curModule, d.o^.home); return nil; end; diff --git a/src/Sym.raz b/src/Sym.raz index 274df57..452b500 100644 --- a/src/Sym.raz +++ b/src/Sym.raz @@ -39,7 +39,6 @@ len*: I64; (* fArray: elements; fProc: parameters; fVariant: cases; a tuple: results *) fields*: ^Obj; (* fRecord: fields; fVariant: cases; fProc: parameters *) scope*: INT; (* fRecord: the scope of its fields *) - fwd*: INT; (* fPtr whose base isn't declared yet: its name, else -1 *) obj*: ^Obj; (* the declaration that names it, or nil *) home*: ^Module; (* the module that built it *) end; @@ -163,7 +162,6 @@ t^.form := form; t^.size := size; t^.align := align; - t^.fwd := -1; t^.home := curModule; return t; end; diff --git a/test/err/DeclFwdPtrs.err b/test/err/DeclFwdPtrs.err index 3585697..f47f22b 100644 --- a/test/err/DeclFwdPtrs.err +++ b/test/err/DeclFwdPtrs.err @@ -1,4 +1,4 @@ -test/err/DeclFwdPtrs.raz:35:11: error: more than 256 pointers to types not yet declared +test/err/DeclFwdPtrs.raz:35:11: error: more than 256 types named before they are declared 35 | P256 = ^T256; | ^^^^ raz: 1 error diff --git a/test/msg/LaterMissing.err b/test/msg/LaterMissing.err new file mode 100644 index 0000000..eec4607 --- /dev/null +++ b/test/msg/LaterMissing.err @@ -0,0 +1,4 @@ +conform/err/LaterMissing.raz:4:33: error: Missing isn't declared + 4 | each: proc(var r: R; m: Missing); + | ^^^^^^^ +raz: 1 error diff --git a/test/msg/LaterOwnField.err b/test/msg/LaterOwnField.err new file mode 100644 index 0000000..3b303c9 --- /dev/null +++ b/test/msg/LaterOwnField.err @@ -0,0 +1,4 @@ +conform/err/LaterOwnField.raz:6:12: error: R isn't declared + 6 | r: R; + | ^ +raz: 1 error diff --git a/test/msg/LaterVar.err b/test/msg/LaterVar.err new file mode 100644 index 0000000..b1b85eb --- /dev/null +++ b/test/msg/LaterVar.err @@ -0,0 +1,10 @@ +conform/err/LaterVar.raz:3:20: error: R isn't declared + 3 | var v: proc(var r: R); + | ^ +conform/err/LaterVar.raz:5:5: error: R is declared twice + 5 | R = record + | ^ +conform/err/LaterVar.raz:3:20: note: the first one is here + 3 | var v: proc(var r: R); + | ^ +raz: 2 errors