Add a nptr T type

This commit is contained in:
Nigel Tao committed 2018-07-15 17:10:03 +10:00
1 parent 15f2f42b77
commit 29eb64a0a4
11 files changed
+39 -27

No files matched your search

+4 -4
View File
@@ -636,14 +636,14 @@ const MaxTypeExprDepth = 63
// TypeExpr is a type expression, such as "base.u32", "base.u32[..8]", "foo",
// "pkg.bar", "ptr T", "array[8] T", "slice T" or "table T":
// - ID0: <0|IDArray|IDFunc|IDPtr|IDSlice|IDTable>
// - ID0: <0|IDArray|IDFunc|IDNptr|IDPtr|IDSlice|IDTable>
// - ID1: <0|pkg>
// - ID2: <0|type name>
// - LHS: <nil|Expr>
// - MHS: <nil|Expr>
// - RHS: <nil|TypeExpr>
//
// An IDPtr ID0 means "ptr RHS". RHS is the inner type.
// An IDNptr or IDPtr ID0 means "nptr RHS" or "ptr RHS". RHS is the inner type.
//
// An IDArray ID0 means "array[LHS] RHS". RHS is the inner type.
//
@@ -684,7 +684,7 @@ func (n *TypeExpr) Innermost() *TypeExpr {
}
func (n *TypeExpr) Pointee() *TypeExpr {
for ; n != nil && n.id0 == t.IDPtr; n = n.Inner() {
for ; n != nil && (n.id0 == t.IDNptr || n.id0 == t.IDPtr); n = n.Inner() {
}
return n
}
@@ -748,7 +748,7 @@ func (n *TypeExpr) HasPointers() bool {
return true
}
}
case t.IDPtr, t.IDNptr, t.IDSlice, t.IDTable:
case t.IDNptr, t.IDPtr, t.IDSlice, t.IDTable:
return true
}
}
+3
View File
@@ -223,6 +223,9 @@ func (n *TypeExpr) appendStr(buf []byte, tm *t.Map, depth uint32) []byte {
switch n.Decorator() {
case 0:
buf = append(buf, n.QID().Str(tm)...)
case t.IDNptr:
buf = append(buf, "nptr "...)
return n.Inner().appendStr(buf, tm, depth)
case t.IDPtr:
buf = append(buf, "ptr "...)
return n.Inner().appendStr(buf, tm, depth)
+18 -3
View File
@@ -888,8 +888,6 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (a.Bounds, error) {
func (q *checker) bcheckExprCall(n *a.Expr, depth uint32) error {
// TODO: handle func pre/post conditions.
//
// TODO: bcheck the receiver, e.g. ptr vs nptr.
lhs := n.LHS().AsExpr()
f, err := q.c.resolveFunc(lhs.MType())
if err != nil {
@@ -905,7 +903,24 @@ func (q *checker) bcheckExprCall(n *a.Expr, depth uint32) error {
return err
}
}
return nil
recv := lhs.LHS().AsExpr()
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")
}
var errNotASpecialCase = errors.New("not a special case")
+4 -4
View File
@@ -27,12 +27,12 @@ import (
)
var (
exprIn = a.NewExpr(0, 0, 0, t.IDIn, nil, nil, nil, nil)
exprOut = a.NewExpr(0, 0, 0, t.IDOut, nil, nil, nil, nil)
exprThis = a.NewExpr(0, 0, 0, t.IDThis, nil, nil, nil, nil)
exprIn = a.NewExpr(0, 0, 0, t.IDIn, nil, nil, nil, nil)
exprOut = a.NewExpr(0, 0, 0, t.IDOut, nil, nil, nil, nil)
exprThis = a.NewExpr(0, 0, 0, t.IDThis, nil, nil, nil, nil)
exprNullptr = a.NewExpr(0, 0, 0, t.IDNullptr, nil, nil, nil, nil)
)
// typeExprFoo is an *ast.Node MType (implicit type).
var (
typeExprGeneric1 = a.NewTypeExpr(0, t.IDBase, t.IDDagger1, nil, nil, nil)
typeExprGeneric2 = a.NewTypeExpr(0, t.IDBase, t.IDDagger2, nil, nil, nil)
+1 -2
View File
@@ -1009,8 +1009,7 @@ swtch:
}
fallthrough
// TODO: also check t.IDNptr? Where else should we look for nptr?
case t.IDPtr, t.IDSlice, t.IDTable:
case t.IDNptr, t.IDPtr, t.IDSlice, t.IDTable:
if err := q.tcheckTypeExpr(typ.Inner(), depth); err != nil {
return err
}
+2 -2
View File
@@ -377,13 +377,13 @@ func (p *parser) parseFieldNode() (*a.Node, error) {
}
func (p *parser) parseTypeExpr() (*a.TypeExpr, error) {
if p.peek1() == t.IDPtr {
if x := p.peek1(); x == t.IDNptr || x == t.IDPtr {
p.src = p.src[1:]
rhs, err := p.parseTypeExpr()
if err != nil {
return nil, err
}
return a.NewTypeExpr(t.IDPtr, 0, 0, nil, nil, rhs), nil
return a.NewTypeExpr(x, 0, 0, nil, nil, rhs), nil
}
decorator, arrayLength := t.ID(0), (*a.Expr)(nil)