lang: allow "slice_var as ptr array[etc] etc"

This commit is contained in:
Nigel Tao committed 2023-06-13 22:38:25 +10:00
1 parent 6411561deb
commit 19f56ebfd2
7 files changed
+83 -5

No files matched your search

+1
View File
@@ -4,6 +4,7 @@
## Work In Progress
- Added `example/toy-aux-image`.
- Added `slice_var as nptr array[etc] etc` conversion.
- Added `std/jpeg`.
- Added `std/netpbm`.
- Changed `lzw.set_literal_width` to `lzw.set_quirk`.
+20
View File
@@ -404,6 +404,26 @@ func (g *gen) writeExprXMinusY(b *buffer, x *a.Expr, y *a.Expr, depth uint32) er
}
func (g *gen) writeExprAs(b *buffer, lhs *a.Expr, rhs *a.TypeExpr, depth uint32) error {
if rhs.IsPointerType() {
if !lhs.MType().IsEitherSliceType() {
// TODO: fix this.
return fmt.Errorf("cannot convert Wuffs conversion from %q to %q to C", lhs.Str(g.tm), rhs.Str(g.tm))
}
if (lhs.Operator() == t.IDDotDot) && (lhs.MHS() == nil) && (lhs.RHS() == nil) {
b.writes("&")
if err := g.writeExpr(b, lhs.LHS().AsExpr(), false, depth); err != nil {
return err
}
b.writes("[0u]")
return nil
}
if err := g.writeExpr(b, lhs, false, depth); err != nil {
return err
}
b.writes(".ptr")
return nil
}
// Drop the "& redundantMask" in "(foo & redundantMask) as base.uxx". It's
// redundant in C/C++ (but not in Wuffs).
if rhs.IsNumType() {
+4
View File
@@ -342,6 +342,10 @@ func (g *gen) writeVars(b *buffer, f *funk, inStructDecl bool) error {
return fmt.Errorf("TODO: support token_{reader,writer} typed variables")
}
if typ.IsPointerType() && typ.IsRecursivelyReadOnly() {
b.writes("const ")
}
if err := g.writeCTypeName(b, typ, vPrefix, name); err != nil {
return err
}
+10
View File
@@ -941,6 +941,16 @@ func (n *TypeExpr) IsReadOnly() bool {
return (n.id0 == t.IDRoarray) || (n.id0 == t.IDRoslice) || (n.id0 == t.IDRotable)
}
func (n *TypeExpr) IsRecursivelyReadOnly() bool {
for ; n != nil; n = n.Inner() {
switch n.id0 {
case t.IDRoarray, t.IDRoslice, t.IDRotable:
return true
}
}
return false
}
func (n *TypeExpr) IsContainerOfSpecificNumType(container t.ID, numType t.ID) bool {
return (n.id0 == container) && n.Inner().IsNumType() && (n.Inner().id2 == numType)
}
+13
View File
@@ -17,6 +17,7 @@ package check
import (
"errors"
"fmt"
"math/big"
a "github.com/google/wuffs/lang/ast"
t "github.com/google/wuffs/lang/token"
@@ -382,3 +383,15 @@ func (q *checker) proveRecvNotEqNullptr(recv *a.Expr) error {
}
return fmt.Errorf("check: cannot prove %q", recv.Str(q.tm)+" <> nullptr")
}
func (q *checker) proveSliceLengthAtLeast(n *a.Expr, minInclLength *big.Int) error {
if (n.Operator() == t.IDDotDot) && (n.MHS() == nil) && (n.RHS() == nil) {
if lTyp := n.LHS().AsExpr().MType(); lTyp.IsEitherArrayType() &&
lTyp.ArrayLength().ConstValue().Cmp(minInclLength) >= 0 {
return nil
}
}
// TODO: the general case, where n isn't "foo[..]" and foo isn't an array
// of length >= minInclLength.
return fmt.Errorf("check: cannot prove %q", n.Str(q.tm)+".length() >= "+minInclLength.String())
}
+21 -3
View File
@@ -953,10 +953,28 @@ func (q *checker) bcheckExpr1(n *a.Expr, depth uint32) (bounds, error) {
case op.IsXUnaryOp():
return q.bcheckExprUnaryOp(n, depth)
case op.IsXBinaryOp():
if op == t.IDXBinaryAs {
return q.bcheckExpr(n.LHS().AsExpr(), depth)
if op != t.IDXBinaryAs {
return q.bcheckExprBinaryOp(op, n.LHS().AsExpr(), n.RHS().AsExpr(), depth)
}
return q.bcheckExprBinaryOp(op, n.LHS().AsExpr(), n.RHS().AsExpr(), depth)
b, err := q.bcheckExpr(n.LHS().AsExpr(), depth)
if err != nil {
return bounds{}, err
} else if lhs := n.LHS().AsExpr(); lhs.MType().IsEitherSliceType() {
cv := (*big.Int)(nil)
rhs := n.RHS().AsTypeExpr()
if (rhs.Decorator() == t.IDPtr) && (rhs.Inner().IsEitherArrayType()) {
cv = rhs.Inner().ArrayLength().ConstValue()
}
if cv == nil {
// TODO: fix this.
return bounds{}, fmt.Errorf("check: cannot bounds-check conversion to %q", rhs.Str(q.tm))
}
if err := q.proveSliceLengthAtLeast(lhs, cv); err != nil {
return bounds{}, err
}
b = bounds{one, maxPointerBounds}
}
return b, nil
case op.IsXAssociativeOp():
return q.bcheckExprAssociativeOp(n, depth)
}
+14 -2
View File
@@ -332,9 +332,11 @@ func (q *checker) tcheckAssign(n *a.Assign) error {
return err
}
for l := lhs; l != nil; l = l.LHS().AsExpr() {
if (l.Operator() == t.IDOpenBracket) && l.LHS().MType().IsReadOnly() {
if l.Operator() != t.IDOpenBracket {
// No-op.
} else if lTyp := l.LHS().MType(); lTyp.IsRecursivelyReadOnly() {
return fmt.Errorf("check: assignment %q: assignee fragment %q, of type %q, has read-only type",
n.Operator().Str(q.tm), l.LHS().AsExpr().Str(q.tm), l.LHS().AsExpr().MType().Str(q.tm))
n.Operator().Str(q.tm), l.LHS().AsExpr().Str(q.tm), lTyp.Str(q.tm))
}
}
lTyp := lhs.MType()
@@ -848,6 +850,16 @@ func (q *checker) tcheckExprBinaryOp(n *a.Expr, depth uint32) error {
if lTyp.IsNumTypeOrIdeal() && rhs.IsNumType() {
n.SetMType(rhs)
return nil
} else if lTyp.IsEitherSliceType() &&
(rhs.Decorator() == t.IDPtr) &&
rhs.Inner().IsEitherArrayType() &&
lTyp.Inner().Eq(rhs.Inner().Inner()) {
if lTyp.IsReadOnly() || rhs.IsRecursivelyReadOnly() {
// TODO.
} else {
n.SetMType(rhs)
return nil
}
}
return fmt.Errorf("check: cannot convert expression %q, of type %q, as type %q",
lhs.Str(q.tm), lTyp.Str(q.tm), rhs.Str(q.tm))