cgen: allow "var x : nptr array[etc] etc"

This commit is contained in:
Nigel Tao committed 2023-06-13 10:49:37 +10:00
1 parent dfab0d2b3d
commit 8ac22ceab2
7 files changed
+84 -31

No files matched your search

+1 -1
View File
@@ -860,7 +860,7 @@ func (n *TypeExpr) BulkSize() *big.Int {
}
func (n *TypeExpr) IsBulkNumType() bool {
for ; n.id0 == t.IDArray; n = n.rhs.AsTypeExpr() {
for ; (n.id0 == t.IDArray) || (n.id0 == t.IDRoarray); n = n.rhs.AsTypeExpr() {
}
return n.id0 == 0 && n.id1 == t.IDBase && n.id2.IsNumType()
}
+3 -1
View File
@@ -95,7 +95,8 @@ func (n *TypeExpr) EqIgnoringRefinements(o *TypeExpr) bool {
// EqIgnoringRefinementsLHSReadOnly returns whether n and o are equal, ignoring
// the "[i:j]" in "base.u32[i:j]" and allowing n (the Left Hand Side of an
// assignment) to be read-only when o (the Right Hand Side) is read-write.
// assignment) to be read-only when o (the Right Hand Side) is read-write, or n
// to be "nptr T" when o is "ptr T".
func (n *TypeExpr) EqIgnoringRefinementsLHSReadOnly(o *TypeExpr) bool {
return n.eq(o, true, true)
}
@@ -118,6 +119,7 @@ func (n *TypeExpr) eq(o *TypeExpr, ignoreRefinements bool, lhsReadOnly bool) boo
case (n.id0 == t.IDRoarray) && (o.id0 == t.IDArray):
case (n.id0 == t.IDRoslice) && (o.id0 == t.IDSlice):
case (n.id0 == t.IDRotable) && (o.id0 == t.IDTable):
case (n.id0 == t.IDNptr) && (o.id0 == t.IDPtr):
}
}
if n.id1 != o.id1 || n.id2 != o.id2 {
+15
View File
@@ -367,3 +367,18 @@ func proveReasonRequirementForRHSLength(q *checker, op t.ID, lhs *a.Expr, rhs *a
}
return nil
}
func (q *checker) proveRecvNotEqNullptr(recv *a.Expr) error {
for _, x := range q.facts {
if x.Operator() != t.IDXBinaryNotEq {
continue
}
xLHS := x.LHS().AsExpr()
xRHS := x.RHS().AsExpr()
if (xLHS.Eq(exprNullptr) && xRHS.Eq(recv)) ||
(xRHS.Eq(exprNullptr) && xLHS.Eq(recv)) {
return nil
}
}
return fmt.Errorf("check: cannot prove %q", recv.Str(q.tm)+" <> nullptr")
}
+20 -14
View File
@@ -507,7 +507,10 @@ func (q *checker) bcheckAssignment(lhs *a.Expr, op t.ID, rhs *a.Expr) error {
return err
}
if lhs.MType().IsNumType() && rhs.Effect().Pure() {
if !rhs.Effect().Pure() {
// No-op.
} else if lhs.MType().IsNumType() {
q.facts.appendBinaryOpFact(t.IDXBinaryEqEq, lhs, rhs)
if rhs.Operator() == a.ExprOperatorCall {
@@ -520,6 +523,16 @@ func (q *checker) bcheckAssignment(lhs *a.Expr, op t.ID, rhs *a.Expr) error {
}
}
}
} else if lhs.MType().IsPointerType() {
if cv := rhs.ConstValue(); (cv != nil) && (cv.Sign() == 0) {
q.facts.appendBinaryOpFact(t.IDXBinaryEqEq, lhs, exprNullptr)
} else {
q.facts.appendBinaryOpFact(t.IDXBinaryEqEq, lhs, rhs)
if (lhs.MType().Decorator() == t.IDNptr) && (rhs.MType().Decorator() == t.IDPtr) {
q.facts.appendBinaryOpFact(t.IDXBinaryNotEq, lhs, exprNullptr)
}
}
}
// Look for "lhs = x[i .. j]" where i and j are constants.
@@ -979,6 +992,11 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (bounds, error) {
lengthExpr := (*a.Expr)(nil)
if lTyp := lhs.MType(); lTyp.IsEitherArrayType() {
lengthExpr = lTyp.ArrayLength()
} else if lTyp.IsPointerType() && lTyp.Inner().IsEitherArrayType() {
lengthExpr = lTyp.Inner().ArrayLength()
if err := q.proveRecvNotEqNullptr(lhs); err != nil {
return bounds{}, err
}
} else {
lengthExpr = makeSliceLength(lhs)
}
@@ -1093,19 +1111,7 @@ func (q *checker) bcheckExprCall(n *a.Expr, depth uint32) error {
if recv.MType().Decorator() != t.IDNptr {
return nil
}
// Check that q.facts contain "recv != nullptr".
for _, x := range q.facts {
if x.Operator() != t.IDXBinaryNotEq {
continue
}
xLHS := x.LHS().AsExpr()
xRHS := x.RHS().AsExpr()
if (xLHS.Eq(exprNullptr) && xRHS.Eq(recv)) ||
(xRHS.Eq(exprNullptr) && xLHS.Eq(recv)) {
return nil
}
}
return fmt.Errorf("check: cannot prove %q", recv.Str(q.tm)+" != nullptr")
return q.proveRecvNotEqNullptr(recv)
}
var errNotASpecialCase = errors.New("not a special case")
+11 -3
View File
@@ -514,8 +514,12 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
return err
}
lTyp := lhs.MType()
if key := lTyp.Decorator(); key != t.IDArray && key != t.IDRoarray && key != t.IDRoslice && key != t.IDSlice {
return fmt.Errorf("check: %s is an index expression but %s has type %s, not an array or slice type",
if lTyp.IsEitherArrayType() || lTyp.IsEitherSliceType() {
// No-op.
} else if lTyp.IsPointerType() && lTyp.Inner().IsEitherArrayType() {
// No-op.
} else {
return fmt.Errorf("check: %s is an index expression but %s has type %s, not an array, slice or pointer-to-array type",
n.Str(q.tm), lhs.Str(q.tm), lTyp.Str(q.tm))
}
rTyp := rhs.MType()
@@ -523,7 +527,11 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
return fmt.Errorf("check: %s is an index expression but %s has type %s, not a numeric type",
n.Str(q.tm), rhs.Str(q.tm), rTyp.Str(q.tm))
}
n.SetMType(lTyp.Inner())
if lTyp.IsPointerType() {
n.SetMType(lTyp.Inner().Inner())
} else {
n.SetMType(lTyp.Inner())
}
return nil
case t.IDDotDot: