Rename some "c *checker" receivers for consistency.

This commit is contained in:
Nigel Tao committed 2017-05-20 12:41:21 +10:00
1 parent 9d18ffb1d8
commit c0f5ebec87
2 files changed
+130 -130

No files matched your search

+42 -42
View File
@@ -48,8 +48,8 @@ func neg(i *big.Int) *big.Int {
return big.NewInt(0).Neg(i) return big.NewInt(0).Neg(i)
} }
func (c *checker) bcheckStatement(n *a.Node) error { func (q *checker) bcheckStatement(n *a.Node) error {
c.errFilename, c.errLine = n.Raw().FilenameLine() q.errFilename, q.errLine = n.Raw().FilenameLine()
switch n.Kind() { switch n.Kind() {
case a.KAssert: case a.KAssert:
@@ -57,34 +57,34 @@ func (c *checker) bcheckStatement(n *a.Node) error {
condition := o.Condition() condition := o.Condition()
err := errFailed err := errFailed
if reasonID := o.Reason(); reasonID != 0 { if reasonID := o.Reason(); reasonID != 0 {
if reasonFunc := c.reasonMap[reasonID.Key()]; reasonFunc != nil { if reasonFunc := q.reasonMap[reasonID.Key()]; reasonFunc != nil {
err = reasonFunc(c, o) err = reasonFunc(q, o)
} else { } else {
err = fmt.Errorf("no such reason %s", reasonID.String(c.idMap)) err = fmt.Errorf("no such reason %s", reasonID.String(q.idMap))
} }
} else if condition.ID0().IsBinaryOp() && condition.ID0().Key() != t.KeyAs { } else if condition.ID0().IsBinaryOp() && condition.ID0().Key() != t.KeyAs {
if c.proveBinaryOp(condition.ID0().Key(), condition.LHS().Expr(), condition.RHS().Expr()) { if q.proveBinaryOp(condition.ID0().Key(), condition.LHS().Expr(), condition.RHS().Expr()) {
err = nil err = nil
} }
} }
if err != nil { if err != nil {
if err == errFailed { if err == errFailed {
return fmt.Errorf("check: cannot prove %q", condition.String(c.idMap)) return fmt.Errorf("check: cannot prove %q", condition.String(q.idMap))
} }
return fmt.Errorf("check: cannot prove %q: %v", condition.String(c.idMap), err) return fmt.Errorf("check: cannot prove %q: %v", condition.String(q.idMap), err)
} }
c.facts.appendFact(condition) q.facts.appendFact(condition)
case a.KAssign: case a.KAssign:
o := n.Assign() o := n.Assign()
return c.bcheckAssignment(o.LHS(), o.LHS().MType(), o.Operator(), o.RHS()) return q.bcheckAssignment(o.LHS(), o.LHS().MType(), o.Operator(), o.RHS())
case a.KVar: case a.KVar:
o := n.Var() o := n.Var()
return c.bcheckAssignment(nil, o.XType(), t.IDEq, o.Value()) return q.bcheckAssignment(nil, o.XType(), t.IDEq, o.Value())
case a.KWhile: case a.KWhile:
return c.bcheckWhile(n.While()) return q.bcheckWhile(n.While())
case a.KIf: case a.KIf:
// TODO. // TODO.
@@ -102,15 +102,15 @@ func (c *checker) bcheckStatement(n *a.Node) error {
return nil return nil
} }
func (c *checker) bcheckAssignment(lhs *a.Expr, lTyp *a.TypeExpr, op t.ID, rhs *a.Expr) error { func (q *checker) bcheckAssignment(lhs *a.Expr, lTyp *a.TypeExpr, op t.ID, rhs *a.Expr) error {
if err := c.bcheckAssignment1(lhs, lTyp, op, rhs); err != nil { if err := q.bcheckAssignment1(lhs, lTyp, op, rhs); err != nil {
return err return err
} }
// TODO: drop any facts involving lhs. // TODO: drop any facts involving lhs.
return nil return nil
} }
func (c *checker) bcheckAssignment1(lhs *a.Expr, lTyp *a.TypeExpr, op t.ID, rhs *a.Expr) error { func (q *checker) bcheckAssignment1(lhs *a.Expr, lTyp *a.TypeExpr, op t.ID, rhs *a.Expr) error {
switch lTyp.PackageOrDecorator().Key() { switch lTyp.PackageOrDecorator().Key() {
case t.KeyPtr: case t.KeyPtr:
// TODO: handle. // TODO: handle.
@@ -123,7 +123,7 @@ func (c *checker) bcheckAssignment1(lhs *a.Expr, lTyp *a.TypeExpr, op t.ID, rhs
return nil return nil
} }
lMin, lMax, err := c.bcheckTypeExpr(lTyp) lMin, lMax, err := q.bcheckTypeExpr(lTyp)
if err != nil { if err != nil {
return err return err
} }
@@ -141,9 +141,9 @@ func (c *checker) bcheckAssignment1(lhs *a.Expr, lTyp *a.TypeExpr, op t.ID, rhs
} }
return nil return nil
} }
rMin, rMax, err = c.bcheckExpr(rhs, 0) rMin, rMax, err = q.bcheckExpr(rhs, 0)
} else { } else {
rMin, rMax, err = c.bcheckExprBinaryOp(lhs, op.BinaryForm().Key(), rhs, 0) rMin, rMax, err = q.bcheckExprBinaryOp(lhs, op.BinaryForm().Key(), rhs, 0)
} }
if err != nil { if err != nil {
return err return err
@@ -151,18 +151,18 @@ func (c *checker) bcheckAssignment1(lhs *a.Expr, lTyp *a.TypeExpr, op t.ID, rhs
if rMin.Cmp(lMin) < 0 || rMax.Cmp(lMax) > 0 { if rMin.Cmp(lMin) < 0 || rMax.Cmp(lMax) > 0 {
if op == t.IDEq { if op == t.IDEq {
return fmt.Errorf("check: expression %q bounds [%v..%v] is not within bounds [%v..%v]", return fmt.Errorf("check: expression %q bounds [%v..%v] is not within bounds [%v..%v]",
rhs.String(c.idMap), rMin, rMax, lMin, lMax) rhs.String(q.idMap), rMin, rMax, lMin, lMax)
} else { } else {
return fmt.Errorf("check: assignment %q bounds [%v..%v] is not within bounds [%v..%v]", return fmt.Errorf("check: assignment %q bounds [%v..%v] is not within bounds [%v..%v]",
lhs.String(c.idMap)+" "+op.String(c.idMap)+" "+rhs.String(c.idMap), lhs.String(q.idMap)+" "+op.String(q.idMap)+" "+rhs.String(q.idMap),
rMin, rMax, lMin, lMax) rMin, rMax, lMin, lMax)
} }
} }
return nil return nil
} }
func (c *checker) bcheckWhile(n *a.While) error { func (q *checker) bcheckWhile(n *a.While) error {
if _, _, err := c.bcheckExpr(n.Condition(), 0); err != nil { if _, _, err := q.bcheckExpr(n.Condition(), 0); err != nil {
return err return err
} }
@@ -176,16 +176,16 @@ func (c *checker) bcheckWhile(n *a.While) error {
// Assume the pre and assert conditions, and the while condition. Check // Assume the pre and assert conditions, and the while condition. Check
// the body. // the body.
c.facts = c.facts[:0] q.facts = q.facts[:0]
for _, m := range n.Asserts() { for _, m := range n.Asserts() {
if m.Assert().Keyword().Key() == t.KeyPost { if m.Assert().Keyword().Key() == t.KeyPost {
continue continue
} }
c.facts.appendFact(m.Assert().Condition()) q.facts.appendFact(m.Assert().Condition())
} }
c.facts.appendFact(n.Condition()) q.facts.appendFact(n.Condition())
for _, m := range n.Body() { for _, m := range n.Body() {
if err := c.bcheckStatement(m); err != nil { if err := q.bcheckStatement(m); err != nil {
return err return err
} }
} }
@@ -193,17 +193,17 @@ func (c *checker) bcheckWhile(n *a.While) error {
// TODO: check the assert and post conditions on exit. // TODO: check the assert and post conditions on exit.
// Assume the assert and post conditions. // Assume the assert and post conditions.
c.facts = c.facts[:0] q.facts = q.facts[:0]
for _, m := range n.Asserts() { for _, m := range n.Asserts() {
if m.Assert().Keyword().Key() == t.KeyPre { if m.Assert().Keyword().Key() == t.KeyPre {
continue continue
} }
c.facts.appendFact(m.Assert().Condition()) q.facts.appendFact(m.Assert().Condition())
} }
return nil return nil
} }
func (c *checker) bcheckExpr(n *a.Expr, depth uint32) (*big.Int, *big.Int, error) { func (q *checker) bcheckExpr(n *a.Expr, depth uint32) (*big.Int, *big.Int, error) {
if depth > a.MaxExprDepth { if depth > a.MaxExprDepth {
return nil, nil, fmt.Errorf("check: expression recursion depth too large") return nil, nil, fmt.Errorf("check: expression recursion depth too large")
} }
@@ -214,28 +214,28 @@ func (c *checker) bcheckExpr(n *a.Expr, depth uint32) (*big.Int, *big.Int, error
switch n.ID0().Flags() & (t.FlagsUnaryOp | t.FlagsBinaryOp | t.FlagsAssociativeOp) { switch n.ID0().Flags() & (t.FlagsUnaryOp | t.FlagsBinaryOp | t.FlagsAssociativeOp) {
case 0: case 0:
nMin, nMax, err := c.bcheckTypeExpr(n.MType()) nMin, nMax, err := q.bcheckTypeExpr(n.MType())
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
nMin, nMax = c.facts.refine(n, nMin, nMax) nMin, nMax = q.facts.refine(n, nMin, nMax)
return nMin, nMax, nil return nMin, nMax, nil
case t.FlagsUnaryOp: case t.FlagsUnaryOp:
return c.bcheckExprUnaryOp(n, depth) return q.bcheckExprUnaryOp(n, depth)
case t.FlagsBinaryOp: case t.FlagsBinaryOp:
if n.ID0().Key() == t.KeyXBinaryAs { if n.ID0().Key() == t.KeyXBinaryAs {
// TODO. // TODO.
return zero, zero, nil return zero, zero, nil
} }
return c.bcheckExprBinaryOp(n.LHS().Expr(), n.ID0().Key(), n.RHS().Expr(), depth) return q.bcheckExprBinaryOp(n.LHS().Expr(), n.ID0().Key(), n.RHS().Expr(), depth)
case t.FlagsAssociativeOp: case t.FlagsAssociativeOp:
return c.bcheckExprAssociativeOp(n, depth) return q.bcheckExprAssociativeOp(n, depth)
} }
return nil, nil, fmt.Errorf("check: unrecognized token.Key (0x%X) for bcheckExpr", n.ID0().Key()) return nil, nil, fmt.Errorf("check: unrecognized token.Key (0x%X) for bcheckExpr", n.ID0().Key())
} }
func (c *checker) bcheckExprUnaryOp(n *a.Expr, depth uint32) (*big.Int, *big.Int, error) { func (q *checker) bcheckExprUnaryOp(n *a.Expr, depth uint32) (*big.Int, *big.Int, error) {
rMin, rMax, err := c.bcheckExpr(n.RHS().Expr(), depth) rMin, rMax, err := q.bcheckExpr(n.RHS().Expr(), depth)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
@@ -252,12 +252,12 @@ func (c *checker) bcheckExprUnaryOp(n *a.Expr, depth uint32) (*big.Int, *big.Int
return nil, nil, fmt.Errorf("check: unrecognized token.Key (0x%X) for bcheckExprUnaryOp", n.ID0().Key()) return nil, nil, fmt.Errorf("check: unrecognized token.Key (0x%X) for bcheckExprUnaryOp", n.ID0().Key())
} }
func (c *checker) bcheckExprBinaryOp(lhs *a.Expr, op t.Key, rhs *a.Expr, depth uint32) (*big.Int, *big.Int, error) { func (q *checker) bcheckExprBinaryOp(lhs *a.Expr, op t.Key, rhs *a.Expr, depth uint32) (*big.Int, *big.Int, error) {
lMin, lMax, err := c.bcheckExpr(lhs, depth) lMin, lMax, err := q.bcheckExpr(lhs, depth)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
rMin, rMax, err := c.bcheckExpr(rhs, depth) rMin, rMax, err := q.bcheckExpr(rhs, depth)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
@@ -292,17 +292,17 @@ func (c *checker) bcheckExprBinaryOp(lhs *a.Expr, op t.Key, rhs *a.Expr, depth u
return nil, nil, fmt.Errorf("check: unrecognized token.Key (0x%X) for bcheckExprBinaryOp", op) return nil, nil, fmt.Errorf("check: unrecognized token.Key (0x%X) for bcheckExprBinaryOp", op)
} }
func (c *checker) bcheckExprAssociativeOp(n *a.Expr, depth uint32) (*big.Int, *big.Int, error) { func (q *checker) bcheckExprAssociativeOp(n *a.Expr, depth uint32) (*big.Int, *big.Int, error) {
return nil, nil, fmt.Errorf("check: unrecognized token.Key (0x%X) for bcheckExprAssociativeOp", n.ID0().Key()) return nil, nil, fmt.Errorf("check: unrecognized token.Key (0x%X) for bcheckExprAssociativeOp", n.ID0().Key())
} }
func (c *checker) bcheckTypeExpr(n *a.TypeExpr) (*big.Int, *big.Int, error) { func (q *checker) bcheckTypeExpr(n *a.TypeExpr) (*big.Int, *big.Int, error) {
b := [2]*big.Int{} b := [2]*big.Int{}
if key := n.Name().Key(); key < t.Key(len(numTypeBounds)) { if key := n.Name().Key(); key < t.Key(len(numTypeBounds)) {
b = numTypeBounds[key] b = numTypeBounds[key]
} }
if b[0] == nil || b[1] == nil { if b[0] == nil || b[1] == nil {
return nil, nil, fmt.Errorf("check: internal error: unknown bounds for %q", n.String(c.idMap)) return nil, nil, fmt.Errorf("check: internal error: unknown bounds for %q", n.String(q.idMap))
} }
if n.IsRefined() { if n.IsRefined() {
if x := n.InclMin(); x != nil { if x := n.InclMin(); x != nil {
+88 -88
View File
@@ -11,24 +11,24 @@ import (
t "github.com/google/puffs/lang/token" t "github.com/google/puffs/lang/token"
) )
func (c *checker) tcheckVars(n *a.Node) error { func (q *checker) tcheckVars(n *a.Node) error {
c.errFilename, c.errLine = n.Raw().FilenameLine() q.errFilename, q.errLine = n.Raw().FilenameLine()
if n.Kind() == a.KVar { if n.Kind() == a.KVar {
v := n.Var() v := n.Var()
name := v.Name() name := v.Name()
if _, ok := c.f.LocalVars[name]; ok { if _, ok := q.f.LocalVars[name]; ok {
return fmt.Errorf("check: duplicate var %q", c.idMap.ByID(name)) return fmt.Errorf("check: duplicate var %q", q.idMap.ByID(name))
} }
if err := c.tcheckTypeExpr(v.XType(), 0); err != nil { if err := q.tcheckTypeExpr(v.XType(), 0); err != nil {
return err return err
} }
c.f.LocalVars[name] = v.XType() q.f.LocalVars[name] = v.XType()
return nil return nil
} }
for _, l := range n.Raw().SubLists() { for _, l := range n.Raw().SubLists() {
for _, m := range l { for _, m := range l {
if err := c.tcheckVars(m); err != nil { if err := q.tcheckVars(m); err != nil {
return err return err
} }
} }
@@ -36,27 +36,27 @@ func (c *checker) tcheckVars(n *a.Node) error {
return nil return nil
} }
func (c *checker) tcheckStatement(n *a.Node) error { func (q *checker) tcheckStatement(n *a.Node) error {
c.errFilename, c.errLine = n.Raw().FilenameLine() q.errFilename, q.errLine = n.Raw().FilenameLine()
switch n.Kind() { switch n.Kind() {
case a.KAssert: case a.KAssert:
if err := c.tcheckAssert(n.Assert()); err != nil { if err := q.tcheckAssert(n.Assert()); err != nil {
return err return err
} }
case a.KAssign: case a.KAssign:
if err := c.tcheckAssign(n.Assign()); err != nil { if err := q.tcheckAssign(n.Assign()); err != nil {
return err return err
} }
case a.KVar: case a.KVar:
o := n.Var() o := n.Var()
if !o.XType().Node().TypeChecked() { if !o.XType().Node().TypeChecked() {
return fmt.Errorf("check: internal error: unchecked type expression %q", o.XType().String(c.idMap)) return fmt.Errorf("check: internal error: unchecked type expression %q", o.XType().String(q.idMap))
} }
if value := o.Value(); value != nil { if value := o.Value(); value != nil {
if err := c.tcheckExpr(value, 0); err != nil { if err := q.tcheckExpr(value, 0); err != nil {
return err return err
} }
// TODO: check that value.Type is assignable to o.TypeExpr(). // TODO: check that value.Type is assignable to o.TypeExpr().
@@ -65,21 +65,21 @@ func (c *checker) tcheckStatement(n *a.Node) error {
case a.KWhile: case a.KWhile:
o := n.While() o := n.While()
cond := o.Condition() cond := o.Condition()
if err := c.tcheckExpr(cond, 0); err != nil { if err := q.tcheckExpr(cond, 0); err != nil {
return err return err
} }
if !cond.MType().Eq(TypeExprBoolean) { if !cond.MType().Eq(TypeExprBoolean) {
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(c.idMap), cond.MType().String(c.idMap)) cond.String(q.idMap), cond.MType().String(q.idMap))
} }
for _, m := range o.Asserts() { for _, m := range o.Asserts() {
if err := c.tcheckAssert(m.Assert()); err != nil { if err := q.tcheckAssert(m.Assert()); err != nil {
return err return err
} }
m.SetTypeChecked() m.SetTypeChecked()
} }
for _, m := range o.Body() { for _, m := range o.Body() {
if err := c.tcheckStatement(m); err != nil { if err := q.tcheckStatement(m); err != nil {
return err return err
} }
} }
@@ -87,20 +87,20 @@ func (c *checker) tcheckStatement(n *a.Node) error {
case a.KIf: case a.KIf:
for o := n.If(); o != nil; o = o.ElseIf() { for o := n.If(); o != nil; o = o.ElseIf() {
cond := o.Condition() cond := o.Condition()
if err := c.tcheckExpr(cond, 0); err != nil { if err := q.tcheckExpr(cond, 0); err != nil {
return err return err
} }
if !cond.MType().Eq(TypeExprBoolean) { if !cond.MType().Eq(TypeExprBoolean) {
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(c.idMap), cond.MType().String(c.idMap)) cond.String(q.idMap), cond.MType().String(q.idMap))
} }
for _, m := range o.BodyIfTrue() { for _, m := range o.BodyIfTrue() {
if err := c.tcheckStatement(m); err != nil { if err := q.tcheckStatement(m); err != nil {
return err return err
} }
} }
for _, m := range o.BodyIfFalse() { for _, m := range o.BodyIfFalse() {
if err := c.tcheckStatement(m); err != nil { if err := q.tcheckStatement(m); err != nil {
return err return err
} }
} }
@@ -109,7 +109,7 @@ func (c *checker) tcheckStatement(n *a.Node) error {
case a.KReturn: case a.KReturn:
o := n.Return() o := n.Return()
if value := o.Value(); value != nil { if value := o.Value(); value != nil {
if err := c.tcheckExpr(value, 0); err != nil { if err := q.tcheckExpr(value, 0); err != nil {
return err return err
} }
// TODO: type-check that value is assignable to the return value. // TODO: type-check that value is assignable to the return value.
@@ -127,17 +127,17 @@ func (c *checker) tcheckStatement(n *a.Node) error {
return nil return nil
} }
func (c *checker) tcheckAssert(n *a.Assert) error { func (q *checker) tcheckAssert(n *a.Assert) error {
cond := n.Condition() cond := n.Condition()
if err := c.tcheckExpr(cond, 0); err != nil { if err := q.tcheckExpr(cond, 0); err != nil {
return err return err
} }
if !cond.MType().Eq(TypeExprBoolean) { if !cond.MType().Eq(TypeExprBoolean) {
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(c.idMap), cond.MType().String(c.idMap)) cond.String(q.idMap), cond.MType().String(q.idMap))
} }
for _, a := range n.Args() { for _, a := range n.Args() {
if err := c.tcheckExpr(a.Arg().Value(), 0); err != nil { if err := q.tcheckExpr(a.Arg().Value(), 0); err != nil {
return err return err
} }
a.SetTypeChecked() a.SetTypeChecked()
@@ -146,13 +146,13 @@ func (c *checker) tcheckAssert(n *a.Assert) error {
return nil return nil
} }
func (c *checker) tcheckAssign(n *a.Assign) error { func (q *checker) tcheckAssign(n *a.Assign) error {
lhs := n.LHS() lhs := n.LHS()
rhs := n.RHS() rhs := n.RHS()
if err := c.tcheckExpr(lhs, 0); err != nil { if err := q.tcheckExpr(lhs, 0); err != nil {
return err return err
} }
if err := c.tcheckExpr(rhs, 0); err != nil { if err := q.tcheckExpr(rhs, 0); err != nil {
return err return err
} }
lTyp := lhs.MType() lTyp := lhs.MType()
@@ -163,12 +163,12 @@ func (c *checker) tcheckAssign(n *a.Assign) error {
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",
rhs.String(c.idMap), rTyp.String(c.idMap), lhs.String(c.idMap), lTyp.String(c.idMap)) rhs.String(q.idMap), rTyp.String(q.idMap), lhs.String(q.idMap), lTyp.String(q.idMap))
} }
if !lTyp.IsNumType() { if !lTyp.IsNumType() {
return fmt.Errorf("check: assignment %q: assignee %q, of type %q, does not have numeric type", return fmt.Errorf("check: assignment %q: assignee %q, of type %q, does not have numeric type",
c.idMap.ByID(n.Operator()), lhs.String(c.idMap), lTyp.String(c.idMap)) q.idMap.ByID(n.Operator()), lhs.String(q.idMap), lTyp.String(q.idMap))
} }
switch n.Operator().Key() { switch n.Operator().Key() {
@@ -177,20 +177,20 @@ func (c *checker) tcheckAssign(n *a.Assign) error {
return nil return nil
} }
return fmt.Errorf("check: assignment %q: shift %q, of type %q, does not have numeric type", return fmt.Errorf("check: assignment %q: shift %q, of type %q, does not have numeric type",
c.idMap.ByID(n.Operator()), rhs.String(c.idMap), rTyp.String(c.idMap)) q.idMap.ByID(n.Operator()), rhs.String(q.idMap), rTyp.String(q.idMap))
} }
if rTyp == TypeExprIdealNumber || lTyp.EqIgnoringRefinements(rTyp) { if rTyp == TypeExprIdealNumber || 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",
c.idMap.ByID(n.Operator()), q.idMap.ByID(n.Operator()),
lhs.String(c.idMap), rhs.String(c.idMap), lhs.String(q.idMap), rhs.String(q.idMap),
lTyp.String(c.idMap), rTyp.String(c.idMap), lTyp.String(q.idMap), rTyp.String(q.idMap),
) )
} }
func (c *checker) tcheckExpr(n *a.Expr, depth uint32) error { func (q *checker) tcheckExpr(n *a.Expr, depth uint32) error {
if depth > a.MaxExprDepth { if depth > a.MaxExprDepth {
return fmt.Errorf("check: expression recursion depth too large") return fmt.Errorf("check: expression recursion depth too large")
} }
@@ -198,19 +198,19 @@ func (c *checker) tcheckExpr(n *a.Expr, depth uint32) error {
switch n.ID0().Flags() & (t.FlagsUnaryOp | t.FlagsBinaryOp | t.FlagsAssociativeOp) { switch n.ID0().Flags() & (t.FlagsUnaryOp | t.FlagsBinaryOp | t.FlagsAssociativeOp) {
case 0: case 0:
if err := c.tcheckExprOther(n, depth); err != nil { if err := q.tcheckExprOther(n, depth); err != nil {
return err return err
} }
case t.FlagsUnaryOp: case t.FlagsUnaryOp:
if err := c.tcheckExprUnaryOp(n, depth); err != nil { if err := q.tcheckExprUnaryOp(n, depth); err != nil {
return err return err
} }
case t.FlagsBinaryOp: case t.FlagsBinaryOp:
if err := c.tcheckExprBinaryOp(n, depth); err != nil { if err := q.tcheckExprBinaryOp(n, depth); err != nil {
return err return err
} }
case t.FlagsAssociativeOp: case t.FlagsAssociativeOp:
if err := c.tcheckExprAssociativeOp(n, depth); err != nil { if err := q.tcheckExprAssociativeOp(n, depth); err != nil {
return err return err
} }
default: default:
@@ -220,13 +220,13 @@ func (c *checker) tcheckExpr(n *a.Expr, depth uint32) error {
return nil return nil
} }
func (c *checker) tcheckExprOther(n *a.Expr, depth uint32) error { func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
switch n.ID0().Key() { switch n.ID0().Key() {
case 0: case 0:
id1 := n.ID1() id1 := n.ID1()
if id1.IsNumLiteral() { if id1.IsNumLiteral() {
z := big.NewInt(0) z := big.NewInt(0)
s := c.idMap.ByID(id1) s := q.idMap.ByID(id1)
if _, ok := z.SetString(s, 0); !ok { if _, ok := z.SetString(s, 0); !ok {
return fmt.Errorf("check: invalid numeric literal %q", s) return fmt.Errorf("check: invalid numeric literal %q", s)
} }
@@ -235,14 +235,14 @@ func (c *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
return nil return nil
} else if id1.IsIdent() { } else if id1.IsIdent() {
if c.f.LocalVars != nil { if q.f.LocalVars != nil {
if typ, ok := c.f.LocalVars[id1]; ok { if typ, ok := q.f.LocalVars[id1]; ok {
n.SetMType(typ) n.SetMType(typ)
return nil return nil
} }
} }
// TODO: look for (global) names (constants, funcs, structs). // TODO: look for (global) names (constants, funcs, structs).
return fmt.Errorf("check: unrecognized identifier %q", c.idMap.ByID(id1)) return fmt.Errorf("check: unrecognized identifier %q", q.idMap.ByID(id1))
} }
switch id1.Key() { switch id1.Key() {
case t.KeyFalse: case t.KeyFalse:
@@ -275,14 +275,14 @@ func (c *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
// TODO. // TODO.
case t.KeyDot: case t.KeyDot:
return c.tcheckDot(n, depth) return q.tcheckDot(n, depth)
} }
return fmt.Errorf("check: unrecognized token.Key (0x%X) for checkExprOther", n.ID0().Key()) return fmt.Errorf("check: unrecognized token.Key (0x%X) for checkExprOther", n.ID0().Key())
} }
func (c *checker) tcheckDot(n *a.Expr, depth uint32) error { func (q *checker) tcheckDot(n *a.Expr, depth uint32) error {
lhs := n.LHS().Expr() lhs := n.LHS().Expr()
if err := c.tcheckExpr(lhs, depth); err != nil { if err := q.tcheckExpr(lhs, depth); err != nil {
return err return err
} }
lTyp := lhs.MType() lTyp := lhs.MType()
@@ -295,19 +295,19 @@ func (c *checker) tcheckDot(n *a.Expr, depth uint32) error {
} }
s := (*a.Struct)(nil) s := (*a.Struct)(nil)
if c.f.Func != nil { if q.f.Func != nil {
switch name := lTyp.Name(); name.Key() { switch name := lTyp.Name(); name.Key() {
case t.KeyIn: case t.KeyIn:
s = c.f.Func.In() s = q.f.Func.In()
case t.KeyOut: case t.KeyOut:
s = c.f.Func.Out() s = q.f.Func.Out()
default: default:
s = c.c.structs[name].Struct s = q.c.structs[name].Struct
} }
} }
if s == nil { if s == nil {
return fmt.Errorf("check: no struct type %q found for expression %q", return fmt.Errorf("check: no struct type %q found for expression %q",
lTyp.Name().String(c.idMap), lhs.String(c.idMap)) lTyp.Name().String(q.idMap), lhs.String(q.idMap))
} }
for _, field := range s.Fields() { for _, field := range s.Fields() {
@@ -318,12 +318,12 @@ func (c *checker) tcheckDot(n *a.Expr, depth uint32) error {
} }
} }
return fmt.Errorf("check: no field named %q found in struct type %q for expression %q", return fmt.Errorf("check: no field named %q found in struct type %q for expression %q",
n.ID1().String(c.idMap), lTyp.Name().String(c.idMap), n.String(c.idMap)) n.ID1().String(q.idMap), lTyp.Name().String(q.idMap), n.String(q.idMap))
} }
func (c *checker) tcheckExprUnaryOp(n *a.Expr, depth uint32) error { func (q *checker) tcheckExprUnaryOp(n *a.Expr, depth uint32) error {
rhs := n.RHS().Expr() rhs := n.RHS().Expr()
if err := c.tcheckExpr(rhs, depth); err != nil { if err := q.tcheckExpr(rhs, depth); err != nil {
return err return err
} }
rTyp := rhs.MType() rTyp := rhs.MType()
@@ -332,7 +332,7 @@ func (c *checker) tcheckExprUnaryOp(n *a.Expr, depth uint32) error {
case t.KeyXUnaryPlus, t.KeyXUnaryMinus: case t.KeyXUnaryPlus, t.KeyXUnaryMinus:
if !numeric(rTyp) { if !numeric(rTyp) {
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",
c.idMap.ByID(n.ID0().AmbiguousForm()), rhs.String(c.idMap), rTyp.String(c.idMap)) q.idMap.ByID(n.ID0().AmbiguousForm()), rhs.String(q.idMap), rTyp.String(q.idMap))
} }
if cv := rhs.ConstValue(); cv != nil { if cv := rhs.ConstValue(); cv != nil {
if n.ID0().Key() == t.KeyXUnaryMinus { if n.ID0().Key() == t.KeyXUnaryMinus {
@@ -346,7 +346,7 @@ func (c *checker) tcheckExprUnaryOp(n *a.Expr, depth uint32) error {
case t.KeyXUnaryNot: case t.KeyXUnaryNot:
if !rTyp.Eq(TypeExprBoolean) { if !rTyp.Eq(TypeExprBoolean) {
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",
c.idMap.ByID(n.ID0().AmbiguousForm()), rhs.String(c.idMap), rTyp.String(c.idMap)) q.idMap.ByID(n.ID0().AmbiguousForm()), rhs.String(q.idMap), rTyp.String(q.idMap))
} }
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))
@@ -357,16 +357,16 @@ func (c *checker) tcheckExprUnaryOp(n *a.Expr, depth uint32) error {
return fmt.Errorf("check: unrecognized token.Key (0x%X) for checkExprUnaryOp", n.ID0().Key()) return fmt.Errorf("check: unrecognized token.Key (0x%X) for checkExprUnaryOp", n.ID0().Key())
} }
func (c *checker) tcheckExprBinaryOp(n *a.Expr, depth uint32) error { func (q *checker) tcheckExprBinaryOp(n *a.Expr, depth uint32) error {
lhs := n.LHS().Expr() lhs := n.LHS().Expr()
if err := c.tcheckExpr(lhs, depth); err != nil { if err := q.tcheckExpr(lhs, depth); err != nil {
return err return err
} }
lTyp := lhs.MType() lTyp := lhs.MType()
op := n.ID0() op := n.ID0()
if op.Key() == t.KeyXBinaryAs { if op.Key() == t.KeyXBinaryAs {
rhs := n.RHS().TypeExpr() rhs := n.RHS().TypeExpr()
if err := c.tcheckTypeExpr(rhs, 0); err != nil { if err := q.tcheckTypeExpr(rhs, 0); err != nil {
return err return err
} }
if numeric(lTyp) && rhs.IsNumType() { if numeric(lTyp) && rhs.IsNumType() {
@@ -374,10 +374,10 @@ func (c *checker) tcheckExprBinaryOp(n *a.Expr, depth uint32) error {
return nil return nil
} }
return fmt.Errorf("check: cannot convert expression %q, of type %q, as type %q", return fmt.Errorf("check: cannot convert expression %q, of type %q, as type %q",
lhs.String(c.idMap), lTyp.String(c.idMap), rhs.String(c.idMap)) lhs.String(q.idMap), lTyp.String(q.idMap), rhs.String(q.idMap))
} }
rhs := n.RHS().Expr() rhs := n.RHS().Expr()
if err := c.tcheckExpr(rhs, depth); err != nil { if err := q.tcheckExpr(rhs, depth); err != nil {
return err return err
} }
rTyp := rhs.MType() rTyp := rhs.MType()
@@ -386,22 +386,22 @@ func (c *checker) tcheckExprBinaryOp(n *a.Expr, depth uint32) error {
default: default:
if !numeric(lTyp) { if !numeric(lTyp) {
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",
c.idMap.ByID(op.AmbiguousForm()), lhs.String(c.idMap), lTyp.String(c.idMap)) q.idMap.ByID(op.AmbiguousForm()), lhs.String(q.idMap), lTyp.String(q.idMap))
} }
if !numeric(rTyp) { if !numeric(rTyp) {
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",
c.idMap.ByID(op.AmbiguousForm()), rhs.String(c.idMap), rTyp.String(c.idMap)) q.idMap.ByID(op.AmbiguousForm()), rhs.String(q.idMap), rTyp.String(q.idMap))
} }
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 != TypeExprBoolean { if lTyp != TypeExprBoolean {
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",
c.idMap.ByID(op.AmbiguousForm()), lhs.String(c.idMap), lTyp.String(c.idMap)) q.idMap.ByID(op.AmbiguousForm()), lhs.String(q.idMap), lTyp.String(q.idMap))
} }
if rTyp != TypeExprBoolean { if rTyp != TypeExprBoolean {
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",
c.idMap.ByID(op.AmbiguousForm()), rhs.String(c.idMap), rTyp.String(c.idMap)) q.idMap.ByID(op.AmbiguousForm()), rhs.String(q.idMap), rTyp.String(q.idMap))
} }
} }
@@ -409,24 +409,24 @@ func (c *checker) tcheckExprBinaryOp(n *a.Expr, depth uint32) error {
default: default:
if !lTyp.EqIgnoringRefinements(rTyp) && lTyp != TypeExprIdealNumber && rTyp != TypeExprIdealNumber { if !lTyp.EqIgnoringRefinements(rTyp) && lTyp != TypeExprIdealNumber && rTyp != TypeExprIdealNumber {
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",
c.idMap.ByID(op.AmbiguousForm()), q.idMap.ByID(op.AmbiguousForm()),
lhs.String(c.idMap), rhs.String(c.idMap), lhs.String(q.idMap), rhs.String(q.idMap),
lTyp.String(c.idMap), rTyp.String(c.idMap), lTyp.String(q.idMap), rTyp.String(q.idMap),
) )
} }
case t.KeyXBinaryShiftL, t.KeyXBinaryShiftR: case t.KeyXBinaryShiftL, t.KeyXBinaryShiftR:
if (lTyp == TypeExprIdealNumber) && (rTyp != TypeExprIdealNumber) { if (lTyp == TypeExprIdealNumber) && (rTyp != TypeExprIdealNumber) {
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",
c.idMap.ByID(op.AmbiguousForm()), q.idMap.ByID(op.AmbiguousForm()),
lhs.String(c.idMap), rhs.String(c.idMap), lhs.String(q.idMap), rhs.String(q.idMap),
lTyp.String(c.idMap), rTyp.String(c.idMap), lTyp.String(q.idMap), rTyp.String(q.idMap),
) )
} }
} }
if l, r := lhs.ConstValue(), rhs.ConstValue(); l != nil && r != nil { if l, r := lhs.ConstValue(), rhs.ConstValue(); l != nil && r != nil {
if err := c.setConstValueBinaryOp(n, l, r); err != nil { if err := q.setConstValueBinaryOp(n, l, r); err != nil {
return err return err
} }
} }
@@ -442,7 +442,7 @@ func (c *checker) tcheckExprBinaryOp(n *a.Expr, depth uint32) error {
return nil return nil
} }
func (c *checker) setConstValueBinaryOp(n *a.Expr, l *big.Int, r *big.Int) error { func (q *checker) setConstValueBinaryOp(n *a.Expr, l *big.Int, r *big.Int) error {
switch n.ID0().Key() { switch n.ID0().Key() {
case t.KeyXBinaryPlus: case t.KeyXBinaryPlus:
n.SetConstValue(big.NewInt(0).Add(l, r)) n.SetConstValue(big.NewInt(0).Add(l, r))
@@ -452,7 +452,7 @@ func (c *checker) setConstValueBinaryOp(n *a.Expr, l *big.Int, r *big.Int) error
n.SetConstValue(big.NewInt(0).Mul(l, r)) n.SetConstValue(big.NewInt(0).Mul(l, r))
case t.KeyXBinarySlash: case t.KeyXBinarySlash:
if r.Cmp(zero) == 0 { if r.Cmp(zero) == 0 {
return fmt.Errorf("check: division by zero in const expression %q", n.String(c.idMap)) return fmt.Errorf("check: division by zero in const expression %q", n.String(q.idMap))
} }
// TODO: decide on Euclidean division vs other definitions. See "go doc // TODO: decide on Euclidean division vs other definitions. See "go doc
// math/big int.divmod" for details. // math/big int.divmod" for details.
@@ -460,13 +460,13 @@ func (c *checker) setConstValueBinaryOp(n *a.Expr, l *big.Int, r *big.Int) error
case t.KeyXBinaryShiftL: case t.KeyXBinaryShiftL:
if r.Cmp(zero) < 0 || r.Cmp(ffff) > 0 { if r.Cmp(zero) < 0 || r.Cmp(ffff) > 0 {
return fmt.Errorf("check: shift %q out of range in const expression %q", return fmt.Errorf("check: shift %q out of range in const expression %q",
n.RHS().Expr().String(c.idMap), n.String(c.idMap)) n.RHS().Expr().String(q.idMap), n.String(q.idMap))
} }
n.SetConstValue(big.NewInt(0).Lsh(l, uint(r.Uint64()))) n.SetConstValue(big.NewInt(0).Lsh(l, uint(r.Uint64())))
case t.KeyXBinaryShiftR: case t.KeyXBinaryShiftR:
if r.Cmp(zero) < 0 || r.Cmp(ffff) > 0 { if r.Cmp(zero) < 0 || r.Cmp(ffff) > 0 {
return fmt.Errorf("check: shift %q out of range in const expression %q", return fmt.Errorf("check: shift %q out of range in const expression %q",
n.RHS().Expr().String(c.idMap), n.String(c.idMap)) n.RHS().Expr().String(q.idMap), n.String(q.idMap))
} }
n.SetConstValue(big.NewInt(0).Rsh(l, uint(r.Uint64()))) n.SetConstValue(big.NewInt(0).Rsh(l, uint(r.Uint64())))
case t.KeyXBinaryAmp: case t.KeyXBinaryAmp:
@@ -497,12 +497,12 @@ func (c *checker) setConstValueBinaryOp(n *a.Expr, l *big.Int, r *big.Int) error
return nil return nil
} }
func (c *checker) tcheckExprAssociativeOp(n *a.Expr, depth uint32) error { func (q *checker) tcheckExprAssociativeOp(n *a.Expr, depth uint32) error {
// TODO. // TODO.
return fmt.Errorf("check: unrecognized token.Key (0x%X) for checkExprAssociativeOp", n.ID0().Key()) return fmt.Errorf("check: unrecognized token.Key (0x%X) for checkExprAssociativeOp", n.ID0().Key())
} }
func (c *checker) tcheckTypeExpr(n *a.TypeExpr, depth uint32) error { func (q *checker) tcheckTypeExpr(n *a.TypeExpr, depth uint32) error {
if depth > a.MaxTypeExprDepth { if depth > a.MaxTypeExprDepth {
return fmt.Errorf("check: type expression recursion depth too large") return fmt.Errorf("check: type expression recursion depth too large")
} }
@@ -515,11 +515,11 @@ func (c *checker) tcheckTypeExpr(n *a.TypeExpr, depth uint32) error {
if bound == nil { if bound == nil {
continue continue
} }
if err := c.tcheckExpr(bound, 0); err != nil { if err := q.tcheckExpr(bound, 0); err != nil {
return err return err
} }
if bound.ConstValue() == nil { if bound.ConstValue() == nil {
return fmt.Errorf("check: %q is not constant", bound.String(c.idMap)) return fmt.Errorf("check: %q is not constant", bound.String(q.idMap))
} }
} }
break break
@@ -528,28 +528,28 @@ func (c *checker) tcheckTypeExpr(n *a.TypeExpr, depth uint32) error {
// TODO: reject. // TODO: reject.
} }
// TODO: see if name refers to a struct type. // TODO: see if name refers to a struct type.
return fmt.Errorf("check: %q is not a type", c.idMap.ByID(n.Name())) return fmt.Errorf("check: %q is not a type", q.idMap.ByID(n.Name()))
case t.KeyPtr: case t.KeyPtr:
if err := c.tcheckTypeExpr(n.Inner(), depth); err != nil { if err := q.tcheckTypeExpr(n.Inner(), depth); err != nil {
return err return err
} }
case t.KeyOpenBracket: case t.KeyOpenBracket:
aLen := n.ArrayLength() aLen := n.ArrayLength()
if err := c.tcheckExpr(aLen, 0); err != nil { if err := q.tcheckExpr(aLen, 0); err != nil {
return err return err
} }
if aLen.ConstValue() == nil { if aLen.ConstValue() == nil {
return fmt.Errorf("check: %q is not constant", aLen.String(c.idMap)) return fmt.Errorf("check: %q is not constant", aLen.String(q.idMap))
} }
if err := c.tcheckTypeExpr(n.Inner(), depth); err != nil { if err := q.tcheckTypeExpr(n.Inner(), depth); err != nil {
return err return err
} }
default: default:
// TODO: delete this hack. // TODO: delete this hack.
if n.PackageOrDecorator() == c.idMap.ByName("io") && n.Name() == c.idMap.ByName("buf1") { if n.PackageOrDecorator() == q.idMap.ByName("io") && n.Name() == q.idMap.ByName("buf1") {
break break
} }