The compiler: const parameters (report 2026-10.33, section 8) A const parameter is passed by address, as a var one, and only read: its Obj has isVar and isConst, and an item for it or a part of it is ro, as another module's variable is. onlyRead tells them apart, for a message that says the name is only read here, and so that a slice of its array may still be made to read. passArg passes a variable in place, where a value parameter's record is copied, and anything else in a temporary of the statement (constTemp). A match on a const parameter or a constant gives names that are only read. Procedure types compare const as they compare var. Tests in conform/run and conform/err; doc/compiler.md and raz(7) say so.
| @@ -0,0 +1,12 @@ | ||
| 1 | +module ConstAdr; | |
| 2 | +type R = record n: INT; end; | |
| 3 | +proc f(const r: R): ^R; | |
| 4 | +begin | |
| 5 | + return ADR(r); | |
| 6 | +end; | |
| 7 | +proc main*; | |
| 8 | +var r: R; | |
| 9 | +begin | |
| 10 | + _ := f(r); | |
| 11 | +end; | |
| 12 | +end ConstAdr. |
| @@ -0,0 +1,12 @@ | ||
| 1 | +module ConstAssign; | |
| 2 | +type R = record n: INT; end; | |
| 3 | +proc f(const r: R); | |
| 4 | +begin | |
| 5 | + r.n := 1; | |
| 6 | +end; | |
| 7 | +proc main*; | |
| 8 | +var r: R; | |
| 9 | +begin | |
| 10 | + f(r); | |
| 11 | +end; | |
| 12 | +end ConstAssign. |
| @@ -0,0 +1,15 @@ | ||
| 1 | +module ConstMatchName; | |
| 2 | +type S = variant circle(r: INT); dot; end; | |
| 3 | +proc f(const s: S); | |
| 4 | +begin | |
| 5 | + match s of | |
| 6 | + | circle(r): r := 0; | |
| 7 | + | dot: | |
| 8 | + end; | |
| 9 | +end; | |
| 10 | +proc main*; | |
| 11 | +var s: S; | |
| 12 | +begin | |
| 13 | + f(s); | |
| 14 | +end; | |
| 15 | +end ConstMatchName. |
| @@ -0,0 +1,10 @@ | ||
| 1 | +module ConstProcType; | |
| 2 | +type R = record n: INT; end; | |
| 3 | +proc f(r: R): INT; begin return r.n; end; | |
| 4 | +proc main*; | |
| 5 | +var p: proc(const r: R): INT; | |
| 6 | +begin | |
| 7 | + p := f; | |
| 8 | + _ := p; | |
| 9 | +end; | |
| 10 | +end ConstProcType. |
| @@ -0,0 +1,13 @@ | ||
| 1 | +module ConstVarArg; | |
| 2 | +type R = record n: INT; end; | |
| 3 | +proc g(var r: R); begin r.n := 0; end; | |
| 4 | +proc f(const r: R); | |
| 5 | +begin | |
| 6 | + g(r); | |
| 7 | +end; | |
| 8 | +proc main*; | |
| 9 | +var r: R; | |
| 10 | +begin | |
| 11 | + f(r); | |
| 12 | +end; | |
| 13 | +end ConstVarArg. |
| @@ -0,0 +1,12 @@ | ||
| 1 | +4 | |
| 2 | +10 | |
| 3 | +5 | |
| 4 | +104 | |
| 5 | +42 | |
| 6 | +4 | |
| 7 | +77 | |
| 8 | +12 | |
| 9 | +12 | |
| 10 | +Tbc | |
| 11 | +80 | |
| 12 | +9 |
| @@ -0,0 +1,79 @@ | ||
| 1 | +module ConstParam; | |
| 2 | +import Out; | |
| 3 | + | |
| 4 | +type | |
| 5 | + Big = record a: [100]INT; name: []U8; n: INT; end; | |
| 6 | + Shape = variant circle(r: INT); rect(w, h: INT); end; | |
| 7 | + | |
| 8 | +const Ten: Big = {n: 10, name: "ten"}; | |
| 9 | + | |
| 10 | +var g: Big; | |
| 11 | + | |
| 12 | +proc sum(const b: Big): INT; | |
| 13 | +var i, s: INT; | |
| 14 | +begin | |
| 15 | + s := b.n; | |
| 16 | + for i := 0 to 99 do s := s + b.a[i]; end; | |
| 17 | + return s; | |
| 18 | +end; | |
| 19 | + | |
| 20 | +proc first(const s: []U8): U8; | |
| 21 | +begin | |
| 22 | + return s[0]; | |
| 23 | +end; | |
| 24 | + | |
| 25 | +proc twice(const n: INT): INT; | |
| 26 | +begin | |
| 27 | + return n * 2; | |
| 28 | +end; | |
| 29 | + | |
| 30 | +proc seen(const b: Big; var w: Big): INT; (* sees w's change: same variable *) | |
| 31 | +begin | |
| 32 | + w.n := 77; | |
| 33 | + return b.n; | |
| 34 | +end; | |
| 35 | + | |
| 36 | +proc area(const s: Shape): INT; | |
| 37 | +begin | |
| 38 | + match s of | |
| 39 | + | circle(r): return 3 * r * r; | |
| 40 | + | rect(w, h): return w * h; | |
| 41 | + end; | |
| 42 | +end; | |
| 43 | + | |
| 44 | +proc make(n: INT): Big; | |
| 45 | +var b: Big; | |
| 46 | +begin | |
| 47 | + b.n := n; | |
| 48 | + return b; | |
| 49 | +end; | |
| 50 | + | |
| 51 | +proc label(const b: Big): []U8; | |
| 52 | +begin | |
| 53 | + b.name[0] := 'T'; (* allowed: the bytes a slice in it points at *) | |
| 54 | + return b.name; | |
| 55 | +end; | |
| 56 | + | |
| 57 | +proc main*; | |
| 58 | +var b: Big; sh: Shape; buf: [3]U8; f: proc(const b: Big): INT; | |
| 59 | +begin | |
| 60 | + b.a[5] := 3; b.n := 1; | |
| 61 | + Out.int(sum(b)); Out.ln; | |
| 62 | + Out.int(sum(Ten)); Out.ln; | |
| 63 | + Out.int(sum(make(5))); Out.ln; | |
| 64 | + Out.int(I64(first("hey"))); Out.ln; | |
| 65 | + Out.int(twice(21)); Out.ln; | |
| 66 | + Out.int(twice(b.n + 1)); Out.ln; | |
| 67 | + Out.int(seen(b, b)); Out.ln; | |
| 68 | + sh := rect(3, 4); | |
| 69 | + Out.int(area(sh)); Out.ln; | |
| 70 | + Out.int(area(circle(2))); Out.ln; | |
| 71 | + buf := "abc"; b.name := buf[0..3]; | |
| 72 | + Out.str(label(b)); Out.ln; | |
| 73 | + f := sum; | |
| 74 | + Out.int(f(b)); Out.ln; | |
| 75 | + g.n := 9; | |
| 76 | + Out.int(sum(g)); Out.ln; | |
| 77 | +end; | |
| 78 | + | |
| 79 | +end ConstParam. |
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | # Reading the raz compiler |
| 2 | 2 | |
| 3 | -The compiler is eleven files of raz, about 11,200 lines of code (the | |
| 3 | +The compiler is eleven files of raz, about 11,400 lines of code (the | |
| 4 | 4 | budget is 14,000). It is its own compiler: the |
| 5 | 5 | frozen Pascal compiler in `seed/` builds `src/` the first time, and |
| 6 | 6 | after that a raz builds `src/` (`make fixpoint` checks that it makes |
| @@ -14,13 +14,13 @@ | ||
| 14 | 14 | | File | Lines | Code | What | |
| 15 | 15 | |---|---|---|---| |
| 16 | 16 | | `src/Diag.raz` | 410 | 315 | Messages: positions, source excerpts, colour, counts | |
| 17 | -| `src/Scan.raz` | 1,125 | 934 | Symbols, literals, comments, positions | | |
| 18 | -| `src/Sym.raz` | 707 | 566 | Types, objects, scopes, layout | | |
| 17 | +| `src/Scan.raz` | 1,148 | 949 | Symbols, literals, comments, positions | | |
| 18 | +| `src/Sym.raz` | 712 | 570 | Types, objects, scopes, layout | | |
| 19 | 19 | | `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes | |
| 20 | 20 | | `src/X64.raz` | 1,517 | 1,253 | The instruction encoder, jumps within a procedure, the assembler | |
| 21 | 21 | | `src/Gen.raz` | 2,552 | 1,821 | The stack machine, for x86-64: values, operations, checks, calls, frames | |
| 22 | -| `src/Item.raz` | 2,037 | 1,651 | Items: types, constants, variables, operators, conversions, calls | | |
| 23 | -| `src/Parse.raz` | 4,176 | 3,615 | Declarations, statements, expressions, built-ins, recovery, warnings | | |
| 22 | +| `src/Item.raz` | 2,091 | 1,682 | Items: types, constants, variables, operators, conversions, calls | | |
| 23 | +| `src/Parse.raz` | 4,182 | 3,619 | Declarations, statements, expressions, built-ins, recovery, warnings | | |
| 24 | 24 | | `src/Link.raz` | 381 | 298 | Tree shaking, layout, patching, the places | |
| 25 | 25 | | `src/Elf.raz` | 449 | 355 | Static and dynamic ELF, symbol table, line numbers | |
| 26 | 26 | | `src/Raz.raz` | 326 | 253 | The command line | |
| @@ -1374,7 +1374,25 @@ | ||
| 1374 | 1374 | |
| 1375 | 1375 | Calls: `beginCall`, then `passArg` for each argument, then `endCall`, |
| 1376 | 1376 | which has Gen's `call` make the call. Each argument is left on Gen's |
| 1377 | -stack, a slice as its two words (`pushWords`). `return f(x);` passing on | |
| 1377 | +stack, a slice as its two words (`pushWords`). | |
| 1378 | + | |
| 1379 | +A `const` parameter (report section 8) is a `var` one that is never | |
| 1380 | +changed: its `Obj` has `isVar`, so Gen passes and reaches it by | |
| 1381 | +address as it does a `var`, and `isConst` too, which makes an item | |
| 1382 | +for it, or for a part of it, `ro`, as another module's variable is. | |
| 1383 | +`onlyRead` tells the two apart, so that `notReadOnly` says "is only | |
| 1384 | +read here", and a slice of its array may still be made, to read, as a | |
| 1385 | +constant's may. `passArg` gives a variable's own address, where a value | |
| 1386 | +parameter's record is copied first: | |
| 1387 | + | |
| 1388 | + value parameter, a record: copyToTemp(a); address(a) | |
| 1389 | + const parameter: address(a) (* a variable, in place *) | |
| 1390 | + | |
| 1391 | +and anything else, a constant number, an expression, a call's result, | |
| 1392 | +a packed record's field, it puts in a temporary of the statement first | |
| 1393 | +(`constTemp`). A `match` on something only read gives names that are | |
| 1394 | +`isConst` too (`matchStatement`), where another module's variable is | |
| 1395 | +copied first and its names are the copy's. `return f(x);` passing on | |
| 1378 | 1396 | f's several results, with `returnAll`, pushes them from the call's |
| 1379 | 1397 | temporary if they are two words, or else copies them to the place its |
| 1380 | 1398 | own hidden pointer names. |
| @@ -547,7 +547,15 @@ | ||
| 547 | 547 | .It |
| 548 | 548 | A parameter is a copy; a |
| 549 | 549 | .Cm var |
| 550 | -one is the caller's variable, of the parameter's type exactly. | |
| 550 | +one is the caller's variable, of the parameter's type exactly; a | |
| 551 | +.Cm const | |
| 552 | +one is the caller's variable too, read in place and never changed, and | |
| 553 | +takes any expression of its type. | |
| 554 | +A procedure takes what it reads as a value when it is a few words, as | |
| 555 | +.Cm const | |
| 556 | +when it is more; what it changes as | |
| 557 | +.Cm var ; | |
| 558 | +and a pointer only for what it keeps. | |
| 551 | 559 | .It |
| 552 | 560 | .Cm "proc f(x: INT): (INT, ERROR);" |
| 553 | 561 | returns several results. |
| @@ -1437,7 +1437,10 @@ | ||
| 1437 | 1437 | either, write it through a view (section 6.9), or assign the names a |
| 1438 | 1438 | `match` arm gives its fields. What a slice or a pointer in it points at |
| 1439 | 1439 | isn't part of it: the procedure may write through them, as it may |
| 1440 | -through a value parameter's. Its argument is any expression of the | |
| 1440 | +through a value parameter's; and a slice it makes of an array in it, | |
| 1441 | +to read, is a slice like any other, which writes the caller's array if | |
| 1442 | +written through. `const` keeps the procedure from changing the | |
| 1443 | +variable by its name, not every path to its bytes. Its argument is any expression of the | |
| 1441 | 1444 | parameter's type: a variable or a constant is passed in place, anything |
| 1442 | 1445 | else as a temporary of the statement, as an array a call returns is |
| 1443 | 1446 | (below). If the variable changes while the procedure runs, through a |
| @@ -267,7 +267,8 @@ | ||
| 267 | 267 | end; |
| 268 | 268 | | oVar: |
| 269 | 269 | x.mode := mVar; |
| 270 | - x.ro := (o^.lev = 0) and (o^.home # Sym.curModule); | |
| 270 | + (* another module's variable, and a const parameter, are only read *) | |
| 271 | + x.ro := ((o^.lev = 0) and (o^.home # Sym.curModule)) or o^.isConst; | |
| 271 | 272 | x.isVar := not x.ro; |
| 272 | 273 | if o^.ext then |
| 273 | 274 | Gen.pushVar(globalAdr(o^.chunk), 8, false, Gen.kWord); |
| @@ -538,10 +539,24 @@ | ||
| 538 | 539 | return true; |
| 539 | 540 | end; |
| 540 | 541 | |
| 542 | +(* x is only read: a const parameter, a part of one, or a name a match | |
| 543 | + gives a field of one, or of a constant (report sections 7.4 and 8). A | |
| 544 | + part reached through a pointer or a slice in it isn't: what they point | |
| 545 | + at is the caller's to change, as through a value parameter's. *) | |
| 546 | +proc onlyRead*(x: Item): BOOL; | |
| 547 | +begin | |
| 548 | + return (x.mode = mVar) and x.ro and (x.obj # nil) and x.obj^.isConst; | |
| 549 | +end; | |
| 550 | + | |
| 541 | 551 | (* Another module's exported variable can be read, not changed, nor |
| 542 | - handed to anything that could change it (report section 3). *) | |
| 552 | + handed to anything that could change it (report section 3); nor can | |
| 553 | + what is only read, above. *) | |
| 543 | 554 | proc notReadOnly*(x: Item): ERROR; |
| 544 | 555 | begin |
| 556 | + if onlyRead(x) then | |
| 557 | + return Scan.markHelp(x.pos, say1("{} is only read here, and can't be changed", Diag.source(x.pos)), | |
| 558 | + "a const parameter, a constant, and the names a match gives their fields, are only read: take a var parameter to change it"); | |
| 559 | + end; | |
| 545 | 560 | if x.ro then |
| 546 | 561 | return Scan.markAt(x.pos, Diag.say("{} is {}'s, and can't be changed outside it", |
| 547 | 562 | [Fmt.str(Diag.source(x.pos)), Fmt.str(Scan.nameOf(x.obj^.home^.name))])); |
| @@ -605,7 +620,9 @@ | ||
| 605 | 620 | and Sym.same(s^.base, t^.base) then |
| 606 | 621 | x.typ := t; |
| 607 | 622 | elsif (s^.form = Sym.fArray) and (t^.form = Sym.fSlice) and Sym.same(s^.base, t^.base) then |
| 608 | - try notReadOnly(x); | |
| 623 | + (* a slice of what is only read may be made, to read, as of a | |
| 624 | + constant; another module's array can't be lent to be written *) | |
| 625 | + if not onlyRead(x) then try notReadOnly(x); end; | |
| 609 | 626 | if x.vol then return Scan.markAt(x.pos, "can't make a slice through a volatile pointer"); end; |
| 610 | 627 | if x.odd then return Scan.markAt(x.pos, "can't make a slice of a field of a packed record"); end; |
| 611 | 628 | if x.view then return noAddress(x, "can't make a slice of a view"); end; |
| @@ -783,7 +800,7 @@ | ||
| 783 | 800 | if x.vol then return Scan.markAt(x.pos, "can't make a slice through a volatile pointer"); end; |
| 784 | 801 | if x.odd then return Scan.markAt(x.pos, "can't make a slice of a field of a packed record"); end; |
| 785 | 802 | if x.view then return noAddress(x, "can't make a slice of a view"); end; |
| 786 | - if t^.form = Sym.fArray then try notReadOnly(x); end; | |
| 803 | + if (t^.form = Sym.fArray) and not onlyRead(x) then try notReadOnly(x); end; (* as in coerce *) | |
| 787 | 804 | try value(hi); |
| 788 | 805 | inFrame := x.frame and (t^.form # Sym.fPtr); |
| 789 | 806 | tmp := Gen.allocTemp(16, 8); |
| @@ -1555,6 +1572,31 @@ | ||
| 1555 | 1572 | return nil; |
| 1556 | 1573 | end; |
| 1557 | 1574 | |
| 1575 | +(* A const parameter's argument that isn't a variable, or is one with no | |
| 1576 | + address of its own (a packed record's field, a view, through ^VOLATILE), | |
| 1577 | + in a temporary of the statement, whose address then goes (report | |
| 1578 | + section 8): a slice's two words, a record or an array copied, or a | |
| 1579 | + single value stored. *) | |
| 1580 | +proc constTemp(var x: Item): ERROR; | |
| 1581 | +var t: I64; | |
| 1582 | +begin | |
| 1583 | + if x.typ^.form = Sym.fSlice then | |
| 1584 | + try pushWords(x); | |
| 1585 | + t := Gen.allocTemp(16, 8); | |
| 1586 | + Gen.popVar(frameAdr(t + 8), 8); (* the length, on top *) | |
| 1587 | + Gen.popVar(frameAdr(t), 8); | |
| 1588 | + elsif Sym.isAggregate(x.typ) then | |
| 1589 | + if x.view then return noAddress(x, "a view of a record or an array can't be a const parameter"); end; | |
| 1590 | + return copyToTemp(x); | |
| 1591 | + else | |
| 1592 | + try value(x); | |
| 1593 | + t := Gen.allocTemp(x.typ^.size, x.typ^.align); | |
| 1594 | + Gen.popVar(frameAdr(t), scalarSize(x.typ)); | |
| 1595 | + end; | |
| 1596 | + tempItem(x, x.typ, t); | |
| 1597 | + return nil; | |
| 1598 | +end; | |
| 1599 | + | |
| 1558 | 1600 | proc passArg*(var c: Gen.CallInfo; var a: Item): ERROR; |
| 1559 | 1601 | var p: ^Sym.Obj; |
| 1560 | 1602 | begin |
| @@ -1579,7 +1621,17 @@ | ||
| 1579 | 1621 | c.isF[c.n] := (p^.typ^.form = Sym.fReal) and not p^.isVar; |
| 1580 | 1622 | passing := p; |
| 1581 | 1623 | passingAt := a.pos; |
| 1582 | - if p^.isVar then | |
| 1624 | + if p^.isConst then | |
| 1625 | + (* by address, as var, but any expression of the type: a variable, | |
| 1626 | + or a constant in read-only data, in place; anything else in a | |
| 1627 | + temporary. Before: | |
| 1628 | + value parameter, a record: copied to a temporary, its address goes | |
| 1629 | + after, for const: | |
| 1630 | + its own address goes; nothing is copied *) | |
| 1631 | + try coerce(a, p^.typ); | |
| 1632 | + if (a.mode # mVar) or a.odd or a.view or a.vol then try constTemp(a); end; | |
| 1633 | + try address(a); | |
| 1634 | + elsif p^.isVar then | |
| 1583 | 1635 | try notReadOnly(a); |
| 1584 | 1636 | if (a.mode # mVar) or not a.isVar then return Scan.markAt(a.pos, "a var parameter needs a variable"); end; |
| 1585 | 1637 | if a.odd then return Scan.markAt(a.pos, "a field of a packed record can't be a var parameter"); end; |
| @@ -1718,7 +1770,9 @@ | ||
| 1718 | 1770 | if (x.mode # mVar) or (x.typ^.form # Sym.fVariant) then |
| 1719 | 1771 | return 0, Scan.markAt(x.pos, say1("match needs a variant, an enumeration, an integer or a string, found {}", what(x))); |
| 1720 | 1772 | end; |
| 1721 | - if x.ro then try copyToTemp(x); end; (* its arms' names stand for a copy's fields *) | |
| 1773 | + (* another module's variable: its arms' names stand for a copy's fields; | |
| 1774 | + what is only read is matched in place, its names only read too *) | |
| 1775 | + if x.ro and not onlyRead(x) then try copyToTemp(x); end; | |
| 1722 | 1776 | try address(x); |
| 1723 | 1777 | p := Gen.allocTemp(8, 8); |
| 1724 | 1778 | Gen.popVar(frameAdr(p), 8); |
| @@ -712,7 +712,7 @@ | ||
| 712 | 712 | |
| 713 | 713 | proc signature: (PType, ERROR); |
| 714 | 714 | var |
| 715 | - t, pt: PType; p, first, last: PObj; isVar: BOOL; n: INT; | |
| 715 | + t, pt: PType; p, first, last: PObj; isVar, isConst: BOOL; n: INT; | |
| 716 | 716 | begin |
| 717 | 717 | t := Sym.newType(Sym.fProc, 8, 8); |
| 718 | 718 | last := nil; n := 0; |
| @@ -725,12 +725,15 @@ | ||
| 725 | 725 | Scan.next; t^.variadic := true; break; |
| 726 | 726 | end; |
| 727 | 727 | isVar := Scan.tok = Scan.sVar; |
| 728 | - if isVar then Scan.next; end; | |
| 728 | + isConst := Scan.tok = Scan.sConst; | |
| 729 | + if isVar or isConst then Scan.next; end; | |
| 729 | 730 | first := nil; |
| 730 | 731 | repeat |
| 731 | 732 | if Scan.tok # Scan.sIdent then return nil, expected("a name"); end; |
| 732 | 733 | p := Sym.newObj(Sym.oVar, Scan.id, nil); |
| 733 | - p^.isVar := isVar; | |
| 734 | + (* a const parameter goes by address, as a var one *) | |
| 735 | + p^.isVar := isVar or isConst; | |
| 736 | + p^.isConst := isConst; | |
| 734 | 737 | p^.lev := 1; |
| 735 | 738 | if last = nil then t^.fields := p; else last^.next := p; end; |
| 736 | 739 | last := p; |
| @@ -2084,11 +2087,13 @@ | ||
| 2084 | 2087 | proc matchStatement(start: Diag.Pos; ind: INT): ERROR; |
| 2085 | 2088 | var |
| 2086 | 2089 | x: TItem; t: PType; c, f, o: PObj; p: I64; L, endL, n, first, disp, elseAt: INT; |
| 2087 | - seen: [256]BOOL; hasElse, all, byValue, isStr: BOOL; endAt: Diag.Pos; ls: Labels; | |
| 2090 | + seen: [256]BOOL; hasElse, all, byValue, isStr, onlyRead: BOOL; endAt: Diag.Pos; ls: Labels; | |
| 2088 | 2091 | begin |
| 2089 | 2092 | Scan.next; |
| 2090 | 2093 | try expression(x, nil); |
| 2091 | 2094 | if x.typ^.form = Sym.fConst then try Item.coerce(x, Sym.tI64); end; |
| 2095 | + (* a const parameter or a constant: then so are the names its arms give *) | |
| 2096 | + onlyRead := Item.onlyRead(x) or ((x.mode = Item.mVar) and (x.obj # nil) and (x.obj^.cls = Sym.oConst)); | |
| 2092 | 2097 | p := try Item.matchBegin(x); |
| 2093 | 2098 | t := x.typ; |
| 2094 | 2099 | isStr := t^.form = Sym.fSlice; (* matchBegin made a string a slice *) |
| @@ -2154,7 +2159,7 @@ | ||
| 2154 | 2159 | return Scan.mark(say1("more names than {} has fields", nameOf(c^.name))); |
| 2155 | 2160 | end; |
| 2156 | 2161 | o := Sym.newObj(Sym.oVar, Scan.id, f^.typ); |
| 2157 | - o^.lev := 1; o^.ind := true; o^.isVar := true; | |
| 2162 | + o^.lev := 1; o^.ind := true; o^.isVar := true; o^.isConst := onlyRead; | |
| 2158 | 2163 | o^.val := p; o^.off := f^.val; |
| 2159 | 2164 | try declare(o); |
| 2160 | 2165 | f := f^.next; |
| @@ -2898,6 +2903,7 @@ | ||
| 2898 | 2903 | o^.pos := q^.pos; |
| 2899 | 2904 | o^.lev := 1; |
| 2900 | 2905 | o^.isVar := q^.isVar; |
| 2906 | + o^.isConst := q^.isConst; | |
| 2901 | 2907 | o^.param := true; |
| 2902 | 2908 | if last = nil then params := o; else last^.next := o; end; |
| 2903 | 2909 | last := o; |
| @@ -3637,7 +3643,7 @@ | ||
| 3637 | 3643 | Diag.warning(o^.pos, say1("{} may reach its end without return, and then returns zero", s)); |
| 3638 | 3644 | end; |
| 3639 | 3645 | if o^.exported or (s[0] = '_') then return; end; |
| 3640 | - if o^.used and (o^.read or (o^.cls # Sym.oVar) or o^.isVar or o^.ext) then return; end; | |
| 3646 | + if o^.used and (o^.read or (o^.cls # Sym.oVar) or (o^.isVar and not o^.isConst) or o^.ext) then return; end; | |
| 3641 | 3647 | match o^.cls of |
| 3642 | 3648 | | oConst: t := say1("constant {}", s); |
| 3643 | 3649 | | oType: t := say1("type {}", s); |
| @@ -58,7 +58,9 @@ | ||
| 58 | 58 | lev*: INT; (* oVar: 0 for a module variable, 1 for a local *) |
| 59 | 59 | ind*: BOOL; (* oVar: the frame slot holds the variable's address *) |
| 60 | 60 | off*: I64; (* oVar with ind: added to that address *) |
| 61 | - isVar*: BOOL; (* a var parameter *) | |
| 61 | + isVar*: BOOL; (* a var parameter, or a const one: passed by address *) | |
| 62 | + isConst*: BOOL; (* a const parameter, or a name a match gives a field | |
| 63 | + of something only read: never changed *) | |
| 62 | 64 | param*: BOOL; (* oVar: a parameter *) |
| 63 | 65 | used*: BOOL; (* named somewhere but in its own body *) |
| 64 | 66 | read*: BOOL; (* oVar: named other than as what := assigns *) |
| @@ -472,7 +474,9 @@ | ||
| 472 | 474 | if (a^.base # nil) and not same(a^.base, b^.base) then return false; end; |
| 473 | 475 | p := a^.fields; q := b^.fields; |
| 474 | 476 | while p # nil do |
| 475 | - if (p^.isVar # q^.isVar) or not same(p^.typ, q^.typ) then return false; end; | |
| 477 | + if (p^.isVar # q^.isVar) or (p^.isConst # q^.isConst) or not same(p^.typ, q^.typ) then | |
| 478 | + return false; | |
| 479 | + end; | |
| 476 | 480 | p := p^.next; q := q^.next; |
| 477 | 481 | end; |
| 478 | 482 | return true; |
| @@ -550,7 +554,8 @@ | ||
| 550 | 554 | Buf.str(b, "proc("); |
| 551 | 555 | f := t^.fields; |
| 552 | 556 | while f # nil do |
| 553 | - if f^.isVar then Buf.str(b, "var "); end; | |
| 557 | + if f^.isConst then Buf.str(b, "const "); | |
| 558 | + elsif f^.isVar then Buf.str(b, "var "); end; | |
| 554 | 559 | typeText(b, f^.typ); |
| 555 | 560 | f := f^.next; |
| 556 | 561 | if f # nil then Buf.str(b, ", "); end; |
| @@ -606,7 +611,7 @@ | ||
| 606 | 611 | while f # nil do |
| 607 | 612 | if (t^.form # fRecord) or f^.exported then |
| 608 | 613 | mix(h, f^.name); |
| 609 | - mix(h, bit(f^.isVar)); | |
| 614 | + mix(h, bit(f^.isVar) + 2 * bit(f^.isConst)); | |
| 610 | 615 | shape(h, f^.typ, false); |
| 611 | 616 | g := f^.fields; |
| 612 | 617 | while g # nil do shape(h, g^.typ, false); g := g^.next; end; |
| @@ -0,0 +1,5 @@ | ||
| 1 | +conform/err/ConstAdr.raz:5:16: error: r is only read here, and can't be changed | |
| 2 | + 5 | return ADR(r); | |
| 3 | + | ^ | |
| 4 | +help: a const parameter, a constant, and the names a match gives their fields, are only read: take a var parameter to change it | |
| 5 | +raz: 1 error |
| @@ -0,0 +1,5 @@ | ||
| 1 | +conform/err/ConstAssign.raz:5:5: error: r.n is only read here, and can't be changed | |
| 2 | + 5 | r.n := 1; | |
| 3 | + | ^^^ | |
| 4 | +help: a const parameter, a constant, and the names a match gives their fields, are only read: take a var parameter to change it | |
| 5 | +raz: 1 error |
| @@ -0,0 +1,5 @@ | ||
| 1 | +conform/err/ConstMatchName.raz:6:18: error: r is only read here, and can't be changed | |
| 2 | + 6 | | circle(r): r := 0; | |
| 3 | + | ^ | |
| 4 | +help: a const parameter, a constant, and the names a match gives their fields, are only read: take a var parameter to change it | |
| 5 | +raz: 1 error |
| @@ -0,0 +1,4 @@ | ||
| 1 | +conform/err/ConstProcType.raz:7:10: error: can't use proc(R): INT as proc(const R): INT | |
| 2 | + 7 | p := f; | |
| 3 | + | ^ | |
| 4 | +raz: 1 error |
| @@ -0,0 +1,5 @@ | ||
| 1 | +conform/err/ConstVarArg.raz:6:7: error: r is only read here, and can't be changed | |
| 2 | + 6 | g(r); | |
| 3 | + | ^ | |
| 4 | +help: a const parameter, a constant, and the names a match gives their fields, are only read: take a var parameter to change it | |
| 5 | +raz: 1 error |