Unexport check.TypeExprFoo variables.

This commit is contained in:
Nigel Tao committed 2017-06-10 12:16:29 +10:00
1 parent 1c62391db3
commit 321ebf8617
4 files changed
+40 -33

No files matched your search

+9 -1
View File
@@ -470,13 +470,21 @@ func (n *TypeExpr) Max() *Expr { return n.mhs.Expr() }
func (n *TypeExpr) Inner() *TypeExpr { return n.rhs.TypeExpr() } func (n *TypeExpr) Inner() *TypeExpr { return n.rhs.TypeExpr() }
func (n *TypeExpr) IsBool() bool { func (n *TypeExpr) IsBool() bool {
return n.id0 == 0 && n.id1.Key() == t.KeyBool && n.lhs == nil && n.mhs == nil && n.rhs == nil return n.id0 == 0 && n.id1.Key() == t.KeyBool
}
func (n *TypeExpr) IsIdeal() bool {
return n.id0 == 0 && n.id1.Key() == t.KeyDoubleZ
} }
func (n *TypeExpr) IsNumType() bool { func (n *TypeExpr) IsNumType() bool {
return n.id0 == 0 && n.id1.IsNumType() return n.id0 == 0 && n.id1.IsNumType()
} }
func (n *TypeExpr) IsNumTypeOrIdeal() bool {
return n.id0 == 0 && (n.id1.IsNumType() || n.id1.Key() == t.KeyDoubleZ)
}
func (n *TypeExpr) IsRefined() bool { func (n *TypeExpr) IsRefined() bool {
return t.Key(n.id0>>t.KeyShift) != t.KeyOpenBracket && (n.lhs != nil || n.mhs != nil) return t.Key(n.id0>>t.KeyShift) != t.KeyOpenBracket && (n.lhs != nil || n.mhs != nil)
} }
+3 -2
View File
@@ -38,7 +38,7 @@ var (
func init() { func init() {
zeroExpr.SetConstValue(zero) zeroExpr.SetConstValue(zero)
zeroExpr.SetMType(TypeExprIdeal) zeroExpr.SetMType(typeExprIdeal)
} }
func btoi(b bool) *big.Int { func btoi(b bool) *big.Int {
@@ -815,7 +815,8 @@ func (q *checker) bcheckExprAssociativeOp(n *a.Expr, depth uint32) (*big.Int, *b
} }
func (q *checker) bcheckTypeExpr(n *a.TypeExpr) (*big.Int, *big.Int, error) { func (q *checker) bcheckTypeExpr(n *a.TypeExpr) (*big.Int, *big.Int, error) {
if n.Eq(TypeExprIdeal) { if n.IsIdeal() {
// TODO: can an ideal type be refined??
return nil, nil, nil return nil, nil, nil
} }
+5 -7
View File
@@ -55,15 +55,13 @@ func trimQuotes(s string) string {
return s return s
} }
// TypeExprFoo is an *ast.Node MType (implicit type). // typeExprFoo is an *ast.Node MType (implicit type).
var ( var (
TypeExprBool = a.NewTypeExpr(0, t.IDBool, nil, nil, nil) typeExprBool = a.NewTypeExpr(0, t.IDBool, nil, nil, nil)
TypeExprIdeal = a.NewTypeExpr(0, t.IDDoubleZ, nil, nil, nil) typeExprIdeal = a.NewTypeExpr(0, t.IDDoubleZ, nil, nil, nil)
TypeExprU8 = a.NewTypeExpr(0, t.IDU8, nil, nil, nil) typeExprU8 = a.NewTypeExpr(0, t.IDU8, nil, nil, nil)
) )
func numeric(n *a.TypeExpr) bool { return n == TypeExprIdeal || n.IsNumType() }
// TypeMap maps from variable names (as token IDs) to types. // TypeMap maps from variable names (as token IDs) to types.
type TypeMap map[t.ID]*a.TypeExpr type TypeMap map[t.ID]*a.TypeExpr
@@ -399,7 +397,7 @@ func (c *Checker) checkFuncBody(node *a.Node) error {
if typ := o.MType(); typ == nil { if typ := o.MType(); typ == nil {
return fmt.Errorf("check: internal error: expression %q has no (implicit) type", return fmt.Errorf("check: internal error: expression %q has no (implicit) type",
o.String(q.tm)) o.String(q.tm))
} else if typ == TypeExprIdeal && o.ConstValue() == nil { } else if typ.IsIdeal() && o.ConstValue() == nil {
return fmt.Errorf("check: internal error: expression %q has ideal number type "+ return fmt.Errorf("check: internal error: expression %q has ideal number type "+
"but no const value", o.String(q.tm)) "but no const value", o.String(q.tm))
} }
+23 -23
View File
@@ -59,7 +59,7 @@ func (q *checker) tcheckStatement(n *a.Node) error {
if err := q.tcheckExpr(cond, 0); err != nil { if err := q.tcheckExpr(cond, 0); err != nil {
return err return err
} }
if !cond.MType().Eq(TypeExprBool) { if !cond.MType().IsBool() {
return fmt.Errorf("check: if condition %q, of type %q, does not have a boolean type", return fmt.Errorf("check: if condition %q, of type %q, does not have a boolean type",
cond.String(q.tm), cond.MType().String(q.tm)) cond.String(q.tm), cond.MType().String(q.tm))
} }
@@ -143,7 +143,7 @@ func (q *checker) tcheckStatement(n *a.Node) error {
if err := q.tcheckExpr(cond, 0); err != nil { if err := q.tcheckExpr(cond, 0); err != nil {
return err return err
} }
if !cond.MType().Eq(TypeExprBool) { if !cond.MType().IsBool() {
return fmt.Errorf("check: for-loop condition %q, of type %q, does not have a boolean type", return fmt.Errorf("check: for-loop condition %q, of type %q, does not have a boolean type",
cond.String(q.tm), cond.MType().String(q.tm)) cond.String(q.tm), cond.MType().String(q.tm))
} }
@@ -176,7 +176,7 @@ func (q *checker) tcheckAssert(n *a.Assert) error {
if err := q.tcheckExpr(cond, 0); err != nil { if err := q.tcheckExpr(cond, 0); err != nil {
return err return err
} }
if !cond.MType().Eq(TypeExprBool) { if !cond.MType().IsBool() {
return fmt.Errorf("check: assert condition %q, of type %q, does not have a boolean type", return fmt.Errorf("check: assert condition %q, of type %q, does not have a boolean type",
cond.String(q.tm), cond.MType().String(q.tm)) cond.String(q.tm), cond.MType().String(q.tm))
} }
@@ -203,7 +203,7 @@ func (q *checker) tcheckAssign(n *a.Assign) error {
rTyp := rhs.MType() rTyp := rhs.MType()
if n.Operator().Key() == t.KeyEq { if n.Operator().Key() == t.KeyEq {
if (rTyp == TypeExprIdeal && lTyp.IsNumType()) || lTyp.EqIgnoringRefinements(rTyp) { if (rTyp.IsIdeal() && lTyp.IsNumType()) || lTyp.EqIgnoringRefinements(rTyp) {
return nil return nil
} }
return fmt.Errorf("check: cannot assign %q of type %q to %q of type %q", return fmt.Errorf("check: cannot assign %q of type %q to %q of type %q",
@@ -224,7 +224,7 @@ func (q *checker) tcheckAssign(n *a.Assign) error {
n.Operator().String(q.tm), rhs.String(q.tm), rTyp.String(q.tm)) n.Operator().String(q.tm), rhs.String(q.tm), rTyp.String(q.tm))
} }
if rTyp == TypeExprIdeal || lTyp.EqIgnoringRefinements(rTyp) { if rTyp.IsIdeal() || lTyp.EqIgnoringRefinements(rTyp) {
return nil return nil
} }
return fmt.Errorf("check: assignment %q: %q and %q, of types %q and %q, do not have compatible types", return fmt.Errorf("check: assignment %q: %q and %q, of types %q and %q, do not have compatible types",
@@ -283,7 +283,7 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
return fmt.Errorf("check: invalid numeric literal %q", s) return fmt.Errorf("check: invalid numeric literal %q", s)
} }
n.SetConstValue(z) n.SetConstValue(z)
n.SetMType(TypeExprIdeal) n.SetMType(typeExprIdeal)
return nil return nil
} else if id1.IsIdent() { } else if id1.IsIdent() {
@@ -299,12 +299,12 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
switch id1.Key() { switch id1.Key() {
case t.KeyFalse: case t.KeyFalse:
n.SetConstValue(zero) n.SetConstValue(zero)
n.SetMType(TypeExprBool) n.SetMType(typeExprBool)
return nil return nil
case t.KeyTrue: case t.KeyTrue:
n.SetConstValue(one) n.SetConstValue(one)
n.SetMType(TypeExprBool) n.SetMType(typeExprBool)
return nil return nil
case t.KeyUnderscore: case t.KeyUnderscore:
@@ -327,7 +327,7 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
return err return err
} }
} }
n.SetMType(TypeExprU8) // HACK. n.SetMType(typeExprU8) // HACK.
return nil return nil
} }
// TODO: delete this hack that only matches "foo.low_bits(etc)". // TODO: delete this hack that only matches "foo.low_bits(etc)".
@@ -337,7 +337,7 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
return err return err
} }
n.LHS().SetTypeChecked() n.LHS().SetTypeChecked()
n.LHS().Expr().SetMType(TypeExprU8) // HACK. n.LHS().Expr().SetMType(typeExprU8) // HACK.
for _, o := range n.Args() { for _, o := range n.Args() {
if err := q.tcheckArg(o.Arg(), depth); err != nil { if err := q.tcheckArg(o.Arg(), depth); err != nil {
return err return err
@@ -449,7 +449,7 @@ func (q *checker) tcheckDot(n *a.Expr, depth uint32) error {
// //
// Another hack is using TypeExprU8 until a TypeExpr can represent // Another hack is using TypeExprU8 until a TypeExpr can represent
// function types. // function types.
n.SetMType(TypeExprU8) n.SetMType(typeExprU8)
return nil return nil
default: default:
s = q.c.structs[name].Struct s = q.c.structs[name].Struct
@@ -480,7 +480,7 @@ func (q *checker) tcheckExprUnaryOp(n *a.Expr, depth uint32) error {
switch n.ID0().Key() { switch n.ID0().Key() {
case t.KeyXUnaryPlus, t.KeyXUnaryMinus: case t.KeyXUnaryPlus, t.KeyXUnaryMinus:
if !numeric(rTyp) { if !rTyp.IsNumTypeOrIdeal() {
return fmt.Errorf("check: unary %q: %q, of type %q, does not have a numeric type", return fmt.Errorf("check: unary %q: %q, of type %q, does not have a numeric type",
n.ID0().AmbiguousForm().String(q.tm), rhs.String(q.tm), rTyp.String(q.tm)) n.ID0().AmbiguousForm().String(q.tm), rhs.String(q.tm), rTyp.String(q.tm))
} }
@@ -494,14 +494,14 @@ func (q *checker) tcheckExprUnaryOp(n *a.Expr, depth uint32) error {
return nil return nil
case t.KeyXUnaryNot: case t.KeyXUnaryNot:
if !rTyp.Eq(TypeExprBool) { if !rTyp.IsBool() {
return fmt.Errorf("check: unary %q: %q, of type %q, does not have a boolean type", return fmt.Errorf("check: unary %q: %q, of type %q, does not have a boolean type",
n.ID0().AmbiguousForm().String(q.tm), rhs.String(q.tm), rTyp.String(q.tm)) n.ID0().AmbiguousForm().String(q.tm), rhs.String(q.tm), rTyp.String(q.tm))
} }
if cv := rhs.ConstValue(); cv != nil { if cv := rhs.ConstValue(); cv != nil {
n.SetConstValue(btoi(cv.Cmp(zero) == 0)) n.SetConstValue(btoi(cv.Cmp(zero) == 0))
} }
n.SetMType(TypeExprBool.Unrefined()) n.SetMType(typeExprBool)
return nil return nil
} }
return fmt.Errorf("check: unrecognized token.Key (0x%X) for tcheckExprUnaryOp", n.ID0().Key()) return fmt.Errorf("check: unrecognized token.Key (0x%X) for tcheckExprUnaryOp", n.ID0().Key())
@@ -519,7 +519,7 @@ func (q *checker) tcheckExprBinaryOp(n *a.Expr, depth uint32) error {
if err := q.tcheckTypeExpr(rhs, 0); err != nil { if err := q.tcheckTypeExpr(rhs, 0); err != nil {
return err return err
} }
if numeric(lTyp) && rhs.IsNumType() { if lTyp.IsNumTypeOrIdeal() && rhs.IsNumType() {
n.SetMType(rhs) n.SetMType(rhs)
return nil return nil
} }
@@ -534,22 +534,22 @@ func (q *checker) tcheckExprBinaryOp(n *a.Expr, depth uint32) error {
switch op.Key() { switch op.Key() {
default: default:
if !numeric(lTyp) { if !lTyp.IsNumTypeOrIdeal() {
return fmt.Errorf("check: binary %q: %q, of type %q, does not have a numeric type", return fmt.Errorf("check: binary %q: %q, of type %q, does not have a numeric type",
op.AmbiguousForm().String(q.tm), lhs.String(q.tm), lTyp.String(q.tm)) op.AmbiguousForm().String(q.tm), lhs.String(q.tm), lTyp.String(q.tm))
} }
if !numeric(rTyp) { if !rTyp.IsNumTypeOrIdeal() {
return fmt.Errorf("check: binary %q: %q, of type %q, does not have a numeric type", return fmt.Errorf("check: binary %q: %q, of type %q, does not have a numeric type",
op.AmbiguousForm().String(q.tm), rhs.String(q.tm), rTyp.String(q.tm)) op.AmbiguousForm().String(q.tm), rhs.String(q.tm), rTyp.String(q.tm))
} }
case t.KeyXBinaryNotEq, t.KeyXBinaryEqEq: case t.KeyXBinaryNotEq, t.KeyXBinaryEqEq:
// No-op. // No-op.
case t.KeyXBinaryAnd, t.KeyXBinaryOr: case t.KeyXBinaryAnd, t.KeyXBinaryOr:
if lTyp != TypeExprBool { if !lTyp.IsBool() {
return fmt.Errorf("check: binary %q: %q, of type %q, does not have a boolean type", return fmt.Errorf("check: binary %q: %q, of type %q, does not have a boolean type",
op.AmbiguousForm().String(q.tm), lhs.String(q.tm), lTyp.String(q.tm)) op.AmbiguousForm().String(q.tm), lhs.String(q.tm), lTyp.String(q.tm))
} }
if rTyp != TypeExprBool { if !rTyp.IsBool() {
return fmt.Errorf("check: binary %q: %q, of type %q, does not have a boolean type", return fmt.Errorf("check: binary %q: %q, of type %q, does not have a boolean type",
op.AmbiguousForm().String(q.tm), rhs.String(q.tm), rTyp.String(q.tm)) op.AmbiguousForm().String(q.tm), rhs.String(q.tm), rTyp.String(q.tm))
} }
@@ -557,7 +557,7 @@ func (q *checker) tcheckExprBinaryOp(n *a.Expr, depth uint32) error {
switch op.Key() { switch op.Key() {
default: default:
if !lTyp.EqIgnoringRefinements(rTyp) && lTyp != TypeExprIdeal && rTyp != TypeExprIdeal { if !lTyp.EqIgnoringRefinements(rTyp) && !lTyp.IsIdeal() && !rTyp.IsIdeal() {
return fmt.Errorf("check: binary %q: %q and %q, of types %q and %q, do not have compatible types", return fmt.Errorf("check: binary %q: %q and %q, of types %q and %q, do not have compatible types",
op.AmbiguousForm().String(q.tm), op.AmbiguousForm().String(q.tm),
lhs.String(q.tm), rhs.String(q.tm), lhs.String(q.tm), rhs.String(q.tm),
@@ -565,7 +565,7 @@ func (q *checker) tcheckExprBinaryOp(n *a.Expr, depth uint32) error {
) )
} }
case t.KeyXBinaryShiftL, t.KeyXBinaryShiftR: case t.KeyXBinaryShiftL, t.KeyXBinaryShiftR:
if (lTyp == TypeExprIdeal) && (rTyp != TypeExprIdeal) { if lTyp.IsIdeal() && !rTyp.IsIdeal() {
return fmt.Errorf("check: binary %q: %q and %q, of types %q and %q; "+ return fmt.Errorf("check: binary %q: %q and %q, of types %q and %q; "+
"cannot shift an ideal number by a non-ideal number", "cannot shift an ideal number by a non-ideal number",
op.AmbiguousForm().String(q.tm), op.AmbiguousForm().String(q.tm),
@@ -582,8 +582,8 @@ func (q *checker) tcheckExprBinaryOp(n *a.Expr, depth uint32) error {
} }
if comparisonOps[0xFF&op.Key()] { if comparisonOps[0xFF&op.Key()] {
n.SetMType(TypeExprBool) n.SetMType(typeExprBool)
} else if lTyp != TypeExprIdeal { } else if !lTyp.IsIdeal() {
n.SetMType(lTyp.Unrefined()) n.SetMType(lTyp.Unrefined())
} else { } else {
n.SetMType(rTyp.Unrefined()) n.SetMType(rTyp.Unrefined())