Remove token ID/Key distinction, part 11 of N

This commit is contained in:
Nigel Tao committed 2018-04-15 17:57:10 +10:00
1 parent b24a929ec7
commit eeebd58d7b
20 files changed
+1218 -1425

No files matched your search

+48 -48
View File
@@ -30,18 +30,18 @@ func otherHandSide(n *a.Expr, thisHS *a.Expr) (op t.ID, thatHS *a.Expr) {
op = n.Operator()
reverseOp := t.ID(0)
switch op.Key() {
case t.KeyXBinaryNotEq:
switch op {
case t.IDXBinaryNotEq:
reverseOp = t.IDXBinaryNotEq
case t.KeyXBinaryLessThan:
case t.IDXBinaryLessThan:
reverseOp = t.IDXBinaryGreaterThan
case t.KeyXBinaryLessEq:
case t.IDXBinaryLessEq:
reverseOp = t.IDXBinaryGreaterEq
case t.KeyXBinaryEqEq:
case t.IDXBinaryEqEq:
reverseOp = t.IDXBinaryEqEq
case t.KeyXBinaryGreaterEq:
case t.IDXBinaryGreaterEq:
reverseOp = t.IDXBinaryLessEq
case t.KeyXBinaryGreaterThan:
case t.IDXBinaryGreaterThan:
reverseOp = t.IDXBinaryLessThan
}
@@ -66,18 +66,18 @@ func (z *facts) appendFact(fact *a.Expr) {
}
}
switch fact.Operator().Key() {
switch fact.Operator() {
case 0:
for _, x := range *z {
if op, other := otherHandSide(x, fact); op.Key() == t.KeyXBinaryEqEq {
if op, other := otherHandSide(x, fact); op == t.IDXBinaryEqEq {
z.appendFact(other)
}
}
case t.KeyXBinaryAnd:
case t.IDXBinaryAnd:
z.appendFact(fact.LHS().Expr())
z.appendFact(fact.RHS().Expr())
return
case t.KeyXAssociativeAnd:
case t.IDXAssociativeAnd:
for _, a := range fact.Args() {
z.appendFact(a.Expr())
}
@@ -124,8 +124,8 @@ func (z facts) refine(n *a.Expr, nMin *big.Int, nMax *big.Int, tm *t.Map) (*big.
}
originalNMin, originalNMax, changed := nMin, nMax, false
switch op.Key() {
case t.KeyXBinaryNotEq:
switch op {
case t.IDXBinaryNotEq:
if nMin.Cmp(cv) == 0 {
nMin = add1(nMin)
changed = true
@@ -133,25 +133,25 @@ func (z facts) refine(n *a.Expr, nMin *big.Int, nMax *big.Int, tm *t.Map) (*big.
nMax = sub1(nMax)
changed = true
}
case t.KeyXBinaryLessThan:
case t.IDXBinaryLessThan:
if nMax.Cmp(cv) >= 0 {
nMax = sub1(cv)
changed = true
}
case t.KeyXBinaryLessEq:
case t.IDXBinaryLessEq:
if nMax.Cmp(cv) > 0 {
nMax = cv
changed = true
}
case t.KeyXBinaryEqEq:
case t.IDXBinaryEqEq:
nMin, nMax = cv, cv
changed = true
case t.KeyXBinaryGreaterEq:
case t.IDXBinaryGreaterEq:
if nMin.Cmp(cv) < 0 {
nMin = cv
changed = true
}
case t.KeyXBinaryGreaterThan:
case t.IDXBinaryGreaterThan:
if nMin.Cmp(cv) <= 0 {
nMin = add1(cv)
changed = true
@@ -188,15 +188,15 @@ func simplify(tm *t.Map, n *a.Expr) (*a.Expr, error) {
}
}
switch op.Key() {
case t.KeyXBinaryPlus:
switch op {
case t.IDXBinaryPlus:
// TODO: more constant folding, so ((x + 1) + 1) becomes (x + 2).
case t.KeyXBinaryMinus:
case t.IDXBinaryMinus:
if lhs.Eq(rhs) {
return zeroExpr, nil
}
if lOp, lLHS, lRHS := parseBinaryOp(lhs); lOp.Key() == t.KeyXBinaryPlus {
if lOp, lLHS, lRHS := parseBinaryOp(lhs); lOp == t.IDXBinaryPlus {
if lLHS.Eq(rhs) {
return lRHS, nil
}
@@ -205,8 +205,8 @@ func simplify(tm *t.Map, n *a.Expr) (*a.Expr, error) {
}
}
case t.KeyXBinaryNotEq, t.KeyXBinaryLessThan, t.KeyXBinaryLessEq,
t.KeyXBinaryEqEq, t.KeyXBinaryGreaterEq, t.KeyXBinaryGreaterThan:
case t.IDXBinaryNotEq, t.IDXBinaryLessThan, t.IDXBinaryLessEq,
t.IDXBinaryEqEq, t.IDXBinaryGreaterEq, t.IDXBinaryGreaterThan:
l, err := simplify(tm, lhs)
if err != nil {
@@ -243,31 +243,31 @@ func parseBinaryOp(n *a.Expr) (op t.ID, lhs *a.Expr, rhs *a.Expr) {
return 0, nil, nil
}
op = n.Operator()
if op.Key() == t.KeyAs {
if op == t.IDAs {
return 0, nil, nil
}
return op, n.LHS().Expr(), n.RHS().Expr()
}
func proveBinaryOpConstValues(op t.Key, lMin *big.Int, lMax *big.Int, rMin *big.Int, rMax *big.Int) (ok bool) {
func proveBinaryOpConstValues(op t.ID, lMin *big.Int, lMax *big.Int, rMin *big.Int, rMax *big.Int) (ok bool) {
switch op {
case t.KeyXBinaryNotEq:
case t.IDXBinaryNotEq:
return lMax.Cmp(rMin) < 0 || lMin.Cmp(rMax) > 0
case t.KeyXBinaryLessThan:
case t.IDXBinaryLessThan:
return lMax.Cmp(rMin) < 0
case t.KeyXBinaryLessEq:
case t.IDXBinaryLessEq:
return lMax.Cmp(rMin) <= 0
case t.KeyXBinaryEqEq:
case t.IDXBinaryEqEq:
return lMin.Cmp(rMax) == 0 && lMax.Cmp(rMin) == 0
case t.KeyXBinaryGreaterEq:
case t.IDXBinaryGreaterEq:
return lMin.Cmp(rMax) >= 0
case t.KeyXBinaryGreaterThan:
case t.IDXBinaryGreaterThan:
return lMin.Cmp(rMax) > 0
}
return false
}
func (q *checker) proveBinaryOp(op t.Key, lhs *a.Expr, rhs *a.Expr) error {
func (q *checker) proveBinaryOp(op t.ID, lhs *a.Expr, rhs *a.Expr) error {
lcv := lhs.ConstValue()
if lcv != nil {
rMin, rMax, err := q.bcheckExpr(rhs, 0)
@@ -293,25 +293,25 @@ func (q *checker) proveBinaryOp(op t.Key, lhs *a.Expr, rhs *a.Expr) error {
if !x.LHS().Expr().Eq(lhs) {
continue
}
factOp := x.Operator().Key()
factOp := x.Operator()
if opImpliesOp(factOp, op) && x.RHS().Expr().Eq(rhs) {
return nil
}
if factOp == t.KeyXBinaryEqEq && rcv != nil {
if factOp == t.IDXBinaryEqEq && rcv != nil {
if factCV := x.RHS().Expr().ConstValue(); factCV != nil {
switch op {
case t.KeyXBinaryNotEq:
case t.IDXBinaryNotEq:
return errFailedOrNil(factCV.Cmp(rcv) != 0)
case t.KeyXBinaryLessThan:
case t.IDXBinaryLessThan:
return errFailedOrNil(factCV.Cmp(rcv) < 0)
case t.KeyXBinaryLessEq:
case t.IDXBinaryLessEq:
return errFailedOrNil(factCV.Cmp(rcv) <= 0)
case t.KeyXBinaryEqEq:
case t.IDXBinaryEqEq:
return errFailedOrNil(factCV.Cmp(rcv) == 0)
case t.KeyXBinaryGreaterEq:
case t.IDXBinaryGreaterEq:
return errFailedOrNil(factCV.Cmp(rcv) >= 0)
case t.KeyXBinaryGreaterThan:
case t.IDXBinaryGreaterThan:
return errFailedOrNil(factCV.Cmp(rcv) > 0)
}
}
@@ -322,15 +322,15 @@ func (q *checker) proveBinaryOp(op t.Key, lhs *a.Expr, rhs *a.Expr) error {
// opImpliesOp returns whether the first op implies the second. For example,
// knowing "x < y" implies that "x != y" and "x <= y".
func opImpliesOp(op0 t.Key, op1 t.Key) bool {
func opImpliesOp(op0 t.ID, op1 t.ID) bool {
if op0 == op1 {
return true
}
switch op0 {
case t.KeyXBinaryLessThan:
return op1 == t.KeyXBinaryNotEq || op1 == t.KeyXBinaryLessEq
case t.KeyXBinaryGreaterThan:
return op1 == t.KeyXBinaryNotEq || op1 == t.KeyXBinaryGreaterEq
case t.IDXBinaryLessThan:
return op1 == t.IDXBinaryNotEq || op1 == t.IDXBinaryLessEq
case t.IDXBinaryGreaterThan:
return op1 == t.IDXBinaryNotEq || op1 == t.IDXBinaryGreaterEq
}
return false
}
@@ -347,9 +347,9 @@ var errFailed = errors.New("failed")
func proveReasonRequirement(q *checker, op t.ID, lhs *a.Expr, rhs *a.Expr) error {
if !op.IsXBinaryOp() {
return fmt.Errorf(
"check: internal error: proveReasonRequirement token.Key (0x%02X) is not an XBinaryOp", op.Key())
"check: internal error: proveReasonRequirement token (0x%02X) is not an XBinaryOp", op)
}
if err := q.proveBinaryOp(op.Key(), lhs, rhs); err != nil {
if err := q.proveBinaryOp(op, lhs, rhs); err != nil {
n := a.NewExpr(a.FlagsTypeChecked, op, 0, 0, lhs.Node(), nil, rhs.Node(), nil)
return fmt.Errorf("cannot prove %q: %v", n.Str(q.tm), err)
}
+121 -121
View File
@@ -23,15 +23,15 @@ import (
)
var numTypeBounds = [256][2]*big.Int{
t.KeyI8: {big.NewInt(-1 << 7), big.NewInt(1<<7 - 1)},
t.KeyI16: {big.NewInt(-1 << 15), big.NewInt(1<<15 - 1)},
t.KeyI32: {big.NewInt(-1 << 31), big.NewInt(1<<31 - 1)},
t.KeyI64: {big.NewInt(-1 << 63), big.NewInt(1<<63 - 1)},
t.KeyU8: {zero, big.NewInt(0).SetUint64(1<<8 - 1)},
t.KeyU16: {zero, big.NewInt(0).SetUint64(1<<16 - 1)},
t.KeyU32: {zero, big.NewInt(0).SetUint64(1<<32 - 1)},
t.KeyU64: {zero, big.NewInt(0).SetUint64(1<<64 - 1)},
t.KeyBool: {zero, one},
t.IDI8: {big.NewInt(-1 << 7), big.NewInt(1<<7 - 1)},
t.IDI16: {big.NewInt(-1 << 15), big.NewInt(1<<15 - 1)},
t.IDI32: {big.NewInt(-1 << 31), big.NewInt(1<<31 - 1)},
t.IDI64: {big.NewInt(-1 << 63), big.NewInt(1<<63 - 1)},
t.IDU8: {zero, big.NewInt(0).SetUint64(1<<8 - 1)},
t.IDU16: {zero, big.NewInt(0).SetUint64(1<<16 - 1)},
t.IDU32: {zero, big.NewInt(0).SetUint64(1<<32 - 1)},
t.IDU64: {zero, big.NewInt(0).SetUint64(1<<64 - 1)},
t.IDBool: {zero, one},
}
var (
@@ -95,13 +95,13 @@ func bitMask(nBits int) *big.Int {
case 1:
return one
case 8:
return numTypeBounds[t.KeyU8][1]
return numTypeBounds[t.IDU8][1]
case 16:
return numTypeBounds[t.KeyU16][1]
return numTypeBounds[t.IDU16][1]
case 32:
return numTypeBounds[t.KeyU32][1]
return numTypeBounds[t.IDU32][1]
case 64:
return numTypeBounds[t.KeyU64][1]
return numTypeBounds[t.IDU64][1]
}
z := big.NewInt(0).Lsh(one, uint(nBits))
return z.Sub(z, one)
@@ -115,22 +115,22 @@ func invert(tm *t.Map, n *a.Expr) (*a.Expr, error) {
return nil, fmt.Errorf("check: invert(%q) called on constant expression", n.Str(tm))
}
op, lhs, rhs, args := n.Operator(), n.LHS().Expr(), n.RHS().Expr(), []*a.Node(nil)
switch op.Key() {
case t.KeyXUnaryNot:
switch op {
case t.IDXUnaryNot:
return rhs, nil
case t.KeyXBinaryNotEq:
case t.IDXBinaryNotEq:
op = t.IDXBinaryEqEq
case t.KeyXBinaryLessThan:
case t.IDXBinaryLessThan:
op = t.IDXBinaryGreaterEq
case t.KeyXBinaryLessEq:
case t.IDXBinaryLessEq:
op = t.IDXBinaryGreaterThan
case t.KeyXBinaryEqEq:
case t.IDXBinaryEqEq:
op = t.IDXBinaryNotEq
case t.KeyXBinaryGreaterEq:
case t.IDXBinaryGreaterEq:
op = t.IDXBinaryLessThan
case t.KeyXBinaryGreaterThan:
case t.IDXBinaryGreaterThan:
op = t.IDXBinaryLessEq
case t.KeyXBinaryAnd, t.KeyXBinaryOr:
case t.IDXBinaryAnd, t.IDXBinaryOr:
var err error
lhs, err = invert(tm, lhs)
if err != nil {
@@ -140,12 +140,12 @@ func invert(tm *t.Map, n *a.Expr) (*a.Expr, error) {
if err != nil {
return nil, err
}
if op.Key() == t.KeyXBinaryAnd {
if op == t.IDXBinaryAnd {
op = t.IDXBinaryOr
} else {
op = t.IDXBinaryAnd
}
case t.KeyXAssociativeAnd, t.KeyXAssociativeOr:
case t.IDXAssociativeAnd, t.IDXAssociativeOr:
args = make([]*a.Node, 0, len(n.Args()))
for _, a := range n.Args() {
v, err := invert(tm, a.Expr())
@@ -154,7 +154,7 @@ func invert(tm *t.Map, n *a.Expr) (*a.Expr, error) {
}
args = append(args, v.Node())
}
if op.Key() == t.KeyXAssociativeAnd {
if op == t.IDXAssociativeAnd {
op = t.IDXAssociativeOr
} else {
op = t.IDXAssociativeAnd
@@ -170,8 +170,8 @@ func invert(tm *t.Map, n *a.Expr) (*a.Expr, error) {
func typeBounds(tm *t.Map, typ *a.TypeExpr) (*big.Int, *big.Int, error) {
b := [2]*big.Int{}
if typ.Decorator() == 0 {
if qid := typ.QID(); qid[0] == t.IDBase && qid[1].Key() < t.Key(len(numTypeBounds)) {
b = numTypeBounds[qid[1].Key()]
if qid := typ.QID(); qid[0] == t.IDBase && qid[1] < t.ID(len(numTypeBounds)) {
b = numTypeBounds[qid[1]]
}
}
if b[0] == nil || b[1] == nil {
@@ -228,7 +228,7 @@ loop:
case a.KJump:
break loop
case a.KRet:
if o.Ret().Keyword().Key() == t.KeyReturn {
if o.Ret().Keyword() == t.IDReturn {
break loop
}
// o is a yield statement.
@@ -280,12 +280,12 @@ func (q *checker) bcheckStatement(n *a.Node) error {
case a.KJump:
n := n.Jump()
skip := t.KeyPost
if n.Keyword().Key() == t.KeyBreak {
skip = t.KeyPre
skip := t.IDPost
if n.Keyword() == t.IDBreak {
skip = t.IDPre
}
for _, o := range n.JumpTarget().Asserts() {
if o.Assert().Keyword().Key() == skip {
if o.Assert().Keyword() == skip {
continue
}
if err := q.bcheckAssert(o.Assert()); err != nil {
@@ -343,13 +343,13 @@ func (q *checker) bcheckAssert(n *a.Assert) error {
err = nil
}
} else if reasonID := n.Reason(); reasonID != 0 {
if reasonFunc := q.reasonMap[reasonID.Key()]; reasonFunc != nil {
if reasonFunc := q.reasonMap[reasonID]; reasonFunc != nil {
err = reasonFunc(q, n)
} else {
err = fmt.Errorf("no such reason %s", reasonID.Str(q.tm))
}
} else if condition.Operator().IsBinaryOp() && condition.Operator().Key() != t.KeyAs {
err = q.proveBinaryOp(condition.Operator().Key(), condition.LHS().Expr(), condition.RHS().Expr())
} else if condition.Operator().IsBinaryOp() && condition.Operator() != t.IDAs {
err = q.proveBinaryOp(condition.Operator(), condition.LHS().Expr(), condition.RHS().Expr())
}
if err != nil {
@@ -400,8 +400,8 @@ func (q *checker) bcheckAssignment(lhs *a.Expr, op t.ID, rhs *a.Expr) error {
if xRHS.Mentions(lhs) {
return nil, nil
}
switch op.Key() {
case t.KeyPlusEq, t.KeyMinusEq:
switch op {
case t.IDPlusEq, t.IDMinusEq:
oRHS := a.NewExpr(a.FlagsTypeChecked, op.BinaryForm(), 0, 0, xRHS.Node(), nil, rhs.Node(), nil)
oRHS.SetMType(xRHS.MType())
oRHS, err := simplify(q.tm, oRHS)
@@ -435,14 +435,14 @@ func (q *checker) bcheckAssignment(lhs *a.Expr, op t.ID, rhs *a.Expr) error {
}
func (q *checker) bcheckAssignment1(lhs *a.Expr, op t.ID, rhs *a.Expr) error {
switch lhs.MType().Decorator().Key() {
case t.KeyPtr:
switch lhs.MType().Decorator() {
case t.IDPtr:
// TODO: handle.
return nil
case t.KeyArray:
case t.IDArray:
// TODO: handle.
return nil
// TODO: t.KeySlice?
// TODO: t.IDSlice?
}
_, _, err := q.bcheckExpr(lhs, 0)
@@ -454,7 +454,7 @@ func (q *checker) bcheckAssignment1(lhs *a.Expr, op t.ID, rhs *a.Expr) error {
func (q *checker) bcheckAssignment2(lhs *a.Expr, lTyp *a.TypeExpr, op t.ID, rhs *a.Expr) error {
if lhs == nil && op != t.IDEq {
return fmt.Errorf("check: internal error: missing LHS for op key 0x%02X", op.Key())
return fmt.Errorf("check: internal error: missing LHS for op key 0x%02X", op)
}
lMin, lMax, err := q.bcheckTypeExpr(lTyp)
@@ -472,7 +472,7 @@ func (q *checker) bcheckAssignment2(lhs *a.Expr, lTyp *a.TypeExpr, op t.ID, rhs
}
rMin, rMax, err = q.bcheckExpr(rhs, 0)
} else {
rMin, rMax, err = q.bcheckExprBinaryOp(op.BinaryForm().Key(), lhs, rhs, 0)
rMin, rMax, err = q.bcheckExprBinaryOp(op.BinaryForm(), lhs, rhs, 0)
}
if err != nil {
return err
@@ -521,7 +521,7 @@ func terminates(body []*a.Node) bool {
case a.KJump:
return true
case a.KRet:
return n.Ret().Keyword().Key() == t.KeyReturn
return n.Ret().Keyword() == t.IDReturn
}
return false
}
@@ -612,7 +612,7 @@ func (q *checker) bcheckIf(n *a.If) error {
func (q *checker) bcheckWhile(n *a.While) error {
// Check the pre and inv conditions on entry.
for _, o := range n.Asserts() {
if o.Assert().Keyword().Key() == t.KeyPost {
if o.Assert().Keyword() == t.IDPost {
continue
}
if err := q.bcheckAssert(o.Assert()); err != nil {
@@ -641,7 +641,7 @@ func (q *checker) bcheckWhile(n *a.While) error {
} else {
q.facts = q.facts[:0]
for _, o := range n.Asserts() {
if o.Assert().Keyword().Key() == t.KeyPost {
if o.Assert().Keyword() == t.IDPost {
continue
}
q.facts.appendFact(o.Assert().Condition())
@@ -652,7 +652,7 @@ func (q *checker) bcheckWhile(n *a.While) error {
q.facts.appendFact(inverse)
}
for _, o := range n.Asserts() {
if o.Assert().Keyword().Key() == t.KeyPost {
if o.Assert().Keyword() == t.IDPost {
if err := q.bcheckAssert(o.Assert()); err != nil {
return err
}
@@ -667,7 +667,7 @@ func (q *checker) bcheckWhile(n *a.While) error {
// Assume the pre and inv conditions...
q.facts = q.facts[:0]
for _, o := range n.Asserts() {
if o.Assert().Keyword().Key() == t.KeyPost {
if o.Assert().Keyword() == t.IDPost {
continue
}
q.facts.appendFact(o.Assert().Condition())
@@ -684,7 +684,7 @@ func (q *checker) bcheckWhile(n *a.While) error {
// body.
if !terminates(n.Body()) {
for _, o := range n.Asserts() {
if o.Assert().Keyword().Key() == t.KeyPost {
if o.Assert().Keyword() == t.IDPost {
continue
}
if err := q.bcheckAssert(o.Assert()); err != nil {
@@ -697,7 +697,7 @@ func (q *checker) bcheckWhile(n *a.While) error {
// Assume the inv and post conditions.
q.facts = q.facts[:0]
for _, o := range n.Asserts() {
if o.Assert().Keyword().Key() == t.KeyPre {
if o.Assert().Keyword() == t.IDPre {
continue
}
q.facts.appendFact(o.Assert().Condition())
@@ -759,10 +759,10 @@ func (q *checker) bcheckExpr1(n *a.Expr, depth uint32) (*big.Int, *big.Int, erro
case op.IsXUnaryOp():
return q.bcheckExprUnaryOp(n, depth)
case op.IsXBinaryOp():
if op.Key() == t.KeyXBinaryAs {
if op == t.IDXBinaryAs {
return q.bcheckExpr(n.LHS().Expr(), depth)
}
return q.bcheckExprBinaryOp(op.Key(), n.LHS().Expr(), n.RHS().Expr(), depth)
return q.bcheckExprBinaryOp(op, n.LHS().Expr(), n.RHS().Expr(), depth)
case op.IsXAssociativeOp():
return q.bcheckExprAssociativeOp(n, depth)
}
@@ -771,7 +771,7 @@ func (q *checker) bcheckExpr1(n *a.Expr, depth uint32) (*big.Int, *big.Int, erro
}
func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int, error) {
switch n.Operator().Key() {
switch n.Operator() {
case 0:
// Look for named consts.
//
@@ -785,23 +785,23 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int,
}
}
case t.KeyOpenParen, t.KeyTry:
case t.IDOpenParen, t.IDTry:
if _, _, err := q.bcheckExpr(n.LHS().Expr(), depth); err != nil {
return nil, nil, err
}
// TODO: delete this hack that only matches "in.src.read_u8?()" etc.
if isInSrc(q.tm, n, t.KeyReadU8, 0) || isInSrc(q.tm, n, t.KeyUnreadU8, 0) ||
isInSrc(q.tm, n, t.KeyReadU16BE, 0) || isInSrc(q.tm, n, t.KeyReadU32BE, 0) ||
isInSrc(q.tm, n, t.KeyReadU32BE, 0) || isInSrc(q.tm, n, t.KeyReadU32LE, 0) ||
isInSrc(q.tm, n, t.KeySkip32, 1) || isInSrc(q.tm, n, t.KeySinceMark, 0) ||
isInSrc(q.tm, n, t.KeyMark, 0) || isInSrc(q.tm, n, t.KeyLimit, 1) ||
isInDst(q.tm, n, t.KeyCopyFromSlice, 1) || isInDst(q.tm, n, t.KeyCopyFromSlice32, 2) ||
isInDst(q.tm, n, t.KeyCopyFromReader32, 2) || isInDst(q.tm, n, t.KeyCopyFromHistory32, 2) ||
isInDst(q.tm, n, t.KeyWriteU8, 1) || isInDst(q.tm, n, t.KeySinceMark, 0) ||
isInDst(q.tm, n, t.KeyMark, 0) || isInDst(q.tm, n, t.KeyIsMarked, 0) ||
isThatMethod(q.tm, n, t.KeyMark, 0) || isThatMethod(q.tm, n, t.KeyLimit, 1) ||
isThatMethod(q.tm, n, t.KeySinceMark, 0) {
if isInSrc(q.tm, n, t.IDReadU8, 0) || isInSrc(q.tm, n, t.IDUnreadU8, 0) ||
isInSrc(q.tm, n, t.IDReadU16BE, 0) || isInSrc(q.tm, n, t.IDReadU32BE, 0) ||
isInSrc(q.tm, n, t.IDReadU32BE, 0) || isInSrc(q.tm, n, t.IDReadU32LE, 0) ||
isInSrc(q.tm, n, t.IDSkip32, 1) || isInSrc(q.tm, n, t.IDSinceMark, 0) ||
isInSrc(q.tm, n, t.IDMark, 0) || isInSrc(q.tm, n, t.IDLimit, 1) ||
isInDst(q.tm, n, t.IDCopyFromSlice, 1) || isInDst(q.tm, n, t.IDCopyFromSlice32, 2) ||
isInDst(q.tm, n, t.IDCopyFromReader32, 2) || isInDst(q.tm, n, t.IDCopyFromHistory32, 2) ||
isInDst(q.tm, n, t.IDWriteU8, 1) || isInDst(q.tm, n, t.IDSinceMark, 0) ||
isInDst(q.tm, n, t.IDMark, 0) || isInDst(q.tm, n, t.IDIsMarked, 0) ||
isThatMethod(q.tm, n, t.IDMark, 0) || isThatMethod(q.tm, n, t.IDLimit, 1) ||
isThatMethod(q.tm, n, t.IDSinceMark, 0) {
for _, o := range n.Args() {
a := o.Arg().Value()
@@ -818,7 +818,7 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int,
break
}
// TODO: delete this hack that only matches "foo.bar_bits(etc)".
if isThatMethod(q.tm, n, t.KeyLowBits, 1) || isThatMethod(q.tm, n, t.KeyHighBits, 1) {
if isThatMethod(q.tm, n, t.IDLowBits, 1) || isThatMethod(q.tm, n, t.IDHighBits, 1) {
a := n.Args()[0].Arg().Value()
aMin, aMax, err := q.bcheckExpr(a, depth)
if err != nil {
@@ -836,13 +836,13 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int,
return zero, bitMask(int(aMax.Int64())), nil
}
// TODO: delete this hack that only matches "foo.is_suspension(etc)".
if isThatMethod(q.tm, n, t.KeyIsError, 0) || isThatMethod(q.tm, n, t.KeyIsOK, 0) ||
isThatMethod(q.tm, n, t.KeyIsSuspension, 0) || isThatMethod(q.tm, n, t.KeySuffix, 1) {
if isThatMethod(q.tm, n, t.IDIsError, 0) || isThatMethod(q.tm, n, t.IDIsOK, 0) ||
isThatMethod(q.tm, n, t.IDIsSuspension, 0) || isThatMethod(q.tm, n, t.IDSuffix, 1) {
// TODO: should return be break? Ditto below.
return nil, nil, nil
}
// TODO: delete this hack that only matches "foo.set_literal_width(etc)".
if isThatMethod(q.tm, n, q.tm.ByName("set_literal_width").Key(), 1) {
if isThatMethod(q.tm, n, q.tm.ByName("set_literal_width"), 1) {
a := n.Args()[0].Arg().Value()
aMin, aMax, err := q.bcheckExpr(a, depth)
if err != nil {
@@ -854,10 +854,10 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int,
return nil, nil, nil
}
// TODO: delete this hack that only matches "foo.decode(etc)".
if isThatMethod(q.tm, n, q.tm.ByName("decode").Key(), 2) ||
isThatMethod(q.tm, n, q.tm.ByName("decode").Key(), 3) ||
isThatMethod(q.tm, n, t.KeyCopyFromSlice, 1) ||
isThatMethod(q.tm, n, q.tm.ByName("update").Key(), 1) {
if isThatMethod(q.tm, n, q.tm.ByName("decode"), 2) ||
isThatMethod(q.tm, n, q.tm.ByName("decode"), 3) ||
isThatMethod(q.tm, n, t.IDCopyFromSlice, 1) ||
isThatMethod(q.tm, n, q.tm.ByName("update"), 1) {
for _, o := range n.Args() {
a := o.Arg().Value()
aMin, aMax, err := q.bcheckExpr(a, depth)
@@ -871,7 +871,7 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int,
return nil, nil, nil
}
// TODO: delete this hack that only matches "foo.length(etc)".
if isThatMethod(q.tm, n, t.KeyLength, 0) || isThatMethod(q.tm, n, t.KeyAvailable, 0) {
if isThatMethod(q.tm, n, t.IDLength, 0) || isThatMethod(q.tm, n, t.IDAvailable, 0) {
break
}
@@ -879,7 +879,7 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int,
return nil, nil, err
}
case t.KeyOpenBracket:
case t.IDOpenBracket:
lhs := n.LHS().Expr()
if _, _, err := q.bcheckExpr(lhs, depth); err != nil {
return nil, nil, err
@@ -903,7 +903,7 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int,
return nil, nil, err
}
case t.KeyColon:
case t.IDColon:
lhs := n.LHS().Expr()
mhs := n.MHS().Expr()
rhs := n.RHS().Expr()
@@ -940,9 +940,9 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int,
}
return nil, nil, nil
case t.KeyDot:
case t.IDDot:
// TODO: delete this hack that only matches "in".
if n.LHS().Expr().Ident().Key() == t.KeyIn {
if n.LHS().Expr().Ident() == t.IDIn {
for _, o := range q.astFunc.In().Fields() {
o := o.Field()
if o.Name() == n.Ident() {
@@ -958,11 +958,11 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int,
return nil, nil, err
}
case t.KeyError, t.KeyStatus, t.KeySuspension:
case t.IDError, t.IDStatus, t.IDSuspension:
// No-op.
default:
return nil, nil, fmt.Errorf("check: unrecognized token.Key (0x%X) for bcheckExprOther", n.Operator().Key())
return nil, nil, fmt.Errorf("check: unrecognized token (0x%X) for bcheckExprOther", n.Operator())
}
return q.bcheckTypeExpr(n.MType())
}
@@ -999,21 +999,21 @@ func (q *checker) bcheckExprUnaryOp(n *a.Expr, depth uint32) (*big.Int, *big.Int
return nil, nil, err
}
switch n.Operator().Key() {
case t.KeyXUnaryPlus:
switch n.Operator() {
case t.IDXUnaryPlus:
return rMin, rMax, nil
case t.KeyXUnaryMinus:
case t.IDXUnaryMinus:
return neg(rMax), neg(rMin), nil
case t.KeyXUnaryNot:
case t.IDXUnaryNot:
return zero, one, nil
case t.KeyXUnaryRef, t.KeyXUnaryDeref:
case t.IDXUnaryRef, t.IDXUnaryDeref:
return q.bcheckTypeExpr(n.MType())
}
return nil, nil, fmt.Errorf("check: unrecognized token.Key (0x%X) for bcheckExprUnaryOp", n.Operator().Key())
return nil, nil, fmt.Errorf("check: unrecognized token (0x%X) for bcheckExprUnaryOp", n.Operator())
}
func (q *checker) bcheckExprBinaryOp(op t.Key, lhs *a.Expr, rhs *a.Expr, depth uint32) (*big.Int, *big.Int, error) {
func (q *checker) bcheckExprBinaryOp(op t.ID, lhs *a.Expr, rhs *a.Expr, depth uint32) (*big.Int, *big.Int, error) {
lMin, lMax, err := q.bcheckExpr(lhs, depth)
if err != nil {
return nil, nil, err
@@ -1021,17 +1021,17 @@ func (q *checker) bcheckExprBinaryOp(op t.Key, lhs *a.Expr, rhs *a.Expr, depth u
return q.bcheckExprBinaryOp1(op, lhs, lMin, lMax, rhs, depth)
}
func (q *checker) bcheckExprBinaryOp1(op t.Key, lhs *a.Expr, lMin *big.Int, lMax *big.Int, rhs *a.Expr, depth uint32) (*big.Int, *big.Int, error) {
func (q *checker) bcheckExprBinaryOp1(op t.ID, lhs *a.Expr, lMin *big.Int, lMax *big.Int, rhs *a.Expr, depth uint32) (*big.Int, *big.Int, error) {
rMin, rMax, err := q.bcheckExpr(rhs, depth)
if err != nil {
return nil, nil, err
}
switch op {
case t.KeyXBinaryPlus:
case t.IDXBinaryPlus:
return big.NewInt(0).Add(lMin, rMin), big.NewInt(0).Add(lMax, rMax), nil
case t.KeyXBinaryMinus:
case t.IDXBinaryMinus:
nMin := big.NewInt(0).Sub(lMin, rMax)
nMax := big.NewInt(0).Sub(lMax, rMin)
for _, x := range q.facts {
@@ -1039,20 +1039,20 @@ func (q *checker) bcheckExprBinaryOp1(op t.Key, lhs *a.Expr, lMin *big.Int, lMax
if !lhs.Eq(xLHS) || !rhs.Eq(xRHS) {
continue
}
switch xOp.Key() {
case t.KeyXBinaryLessThan:
switch xOp {
case t.IDXBinaryLessThan:
nMax = min(nMax, minusOne)
case t.KeyXBinaryLessEq:
case t.IDXBinaryLessEq:
nMax = min(nMax, zero)
case t.KeyXBinaryGreaterEq:
case t.IDXBinaryGreaterEq:
nMin = max(nMin, zero)
case t.KeyXBinaryGreaterThan:
case t.IDXBinaryGreaterThan:
nMin = max(nMin, one)
}
}
return nMin, nMax, nil
case t.KeyXBinaryStar:
case t.IDXBinaryStar:
// TODO: handle multiplication by negative numbers. Note that this
// might reverse the inequality: if 0 < a < b but c < 0 then a*c > b*c.
if lMin.Sign() < 0 {
@@ -1063,10 +1063,10 @@ func (q *checker) bcheckExprBinaryOp1(op t.Key, lhs *a.Expr, lMin *big.Int, lMax
}
return big.NewInt(0).Mul(lMin, rMin), big.NewInt(0).Mul(lMax, rMax), nil
case t.KeyXBinarySlash:
case t.IDXBinarySlash:
// TODO.
case t.KeyXBinaryShiftL:
case t.IDXBinaryShiftL:
if lMin.Sign() < 0 {
return nil, nil, fmt.Errorf("check: shift op argument %q is possibly negative", lhs.Str(q.tm))
}
@@ -1078,7 +1078,7 @@ func (q *checker) bcheckExprBinaryOp1(op t.Key, lhs *a.Expr, lMin *big.Int, lMax
}
return big.NewInt(0).Lsh(lMin, uint(rMin.Uint64())), big.NewInt(0).Lsh(lMax, uint(rMax.Uint64())), nil
case t.KeyXBinaryShiftR:
case t.IDXBinaryShiftR:
if lMin.Sign() < 0 {
return nil, nil, fmt.Errorf("check: shift op argument %q is possibly negative", lhs.Str(q.tm))
}
@@ -1094,36 +1094,36 @@ func (q *checker) bcheckExprBinaryOp1(op t.Key, lhs *a.Expr, lMin *big.Int, lMax
}
return big.NewInt(0).Rsh(lMin, uint(rMax.Uint64())), nMax, nil
case t.KeyXBinaryAmp, t.KeyXBinaryPipe, t.KeyXBinaryHat:
case t.IDXBinaryAmp, t.IDXBinaryPipe, t.IDXBinaryHat:
// TODO: should type-checking ensure that bitwise ops only apply to
// *unsigned* integer types?
if lMin.Sign() < 0 {
return nil, nil, fmt.Errorf("check: bitwise op argument %q is possibly negative", lhs.Str(q.tm))
}
if lMax.Cmp(numTypeBounds[t.KeyU64][1]) > 0 {
if lMax.Cmp(numTypeBounds[t.IDU64][1]) > 0 {
return nil, nil, fmt.Errorf("check: bitwise op argument %q is possibly too large", lhs.Str(q.tm))
}
if rMin.Sign() < 0 {
return nil, nil, fmt.Errorf("check: bitwise op argument %q is possibly negative", rhs.Str(q.tm))
}
if rMax.Cmp(numTypeBounds[t.KeyU64][1]) > 0 {
if rMax.Cmp(numTypeBounds[t.IDU64][1]) > 0 {
return nil, nil, fmt.Errorf("check: bitwise op argument %q is possibly too large", rhs.Str(q.tm))
}
z := (*big.Int)(nil)
switch op {
case t.KeyXBinaryAmp:
case t.IDXBinaryAmp:
z = min(lMax, rMax)
case t.KeyXBinaryPipe, t.KeyXBinaryHat:
case t.IDXBinaryPipe, t.IDXBinaryHat:
z = max(lMax, rMax)
}
// Return [0, z rounded up to the next power-of-2-minus-1]. This is
// conservative, but works fine in practice.
return zero, bitMask(z.BitLen()), nil
case t.KeyXBinaryAmpHat:
case t.IDXBinaryAmpHat:
// TODO.
case t.KeyXBinaryPercent:
case t.IDXBinaryPercent:
if lMin.Sign() < 0 {
return nil, nil, fmt.Errorf("check: modulus op argument %q is possibly negative", lhs.Str(q.tm))
}
@@ -1132,31 +1132,31 @@ func (q *checker) bcheckExprBinaryOp1(op t.Key, lhs *a.Expr, lMin *big.Int, lMax
}
return zero, big.NewInt(0).Sub(rMax, one), nil
case t.KeyXBinaryNotEq, t.KeyXBinaryLessThan, t.KeyXBinaryLessEq, t.KeyXBinaryEqEq,
t.KeyXBinaryGreaterEq, t.KeyXBinaryGreaterThan, t.KeyXBinaryAnd, t.KeyXBinaryOr:
case t.IDXBinaryNotEq, t.IDXBinaryLessThan, t.IDXBinaryLessEq, t.IDXBinaryEqEq,
t.IDXBinaryGreaterEq, t.IDXBinaryGreaterThan, t.IDXBinaryAnd, t.IDXBinaryOr:
return zero, one, nil
case t.KeyXBinaryAs:
case t.IDXBinaryAs:
// Unreachable, as this is checked by the caller.
case t.KeyXBinaryTildePlus:
case t.IDXBinaryTildePlus:
typ := lhs.MType()
if typ.IsIdeal() {
typ = rhs.MType()
}
if qid := typ.QID(); qid[0] == t.IDBase {
b := numTypeBounds[qid[1].Key()]
b := numTypeBounds[qid[1]]
return b[0], b[1], nil
}
}
return nil, nil, fmt.Errorf("check: unrecognized token.Key (0x%X) for bcheckExprBinaryOp", op)
return nil, nil, fmt.Errorf("check: unrecognized token (0x%X) for bcheckExprBinaryOp", op)
}
func (q *checker) bcheckExprAssociativeOp(n *a.Expr, depth uint32) (*big.Int, *big.Int, error) {
op := n.Operator().AmbiguousForm().BinaryForm().Key()
op := n.Operator().AmbiguousForm().BinaryForm()
if op == 0 {
return nil, nil, fmt.Errorf(
"check: unrecognized token.Key (0x%X) for bcheckExprAssociativeOp", n.Operator().Key())
"check: unrecognized token (0x%X) for bcheckExprAssociativeOp", n.Operator())
}
args := n.Args()
if len(args) < 1 {
@@ -1185,24 +1185,24 @@ func (q *checker) bcheckTypeExpr(typ *a.TypeExpr) (*big.Int, *big.Int, error) {
return nil, nil, nil
}
switch typ.Decorator().Key() {
// TODO: case t.KeyOpenParen.
case t.KeyPtr, t.KeyArray, t.KeySlice:
switch typ.Decorator() {
// TODO: case t.IDOpenParen.
case t.IDPtr, t.IDArray, t.IDSlice:
return nil, nil, nil
}
// TODO: is the special cases for io_reader and io_writer superfluous with
// the general purpose code for built-ins below?
if qid := typ.QID(); qid[0] == t.IDBase {
switch qid[1].Key() {
case t.KeyIOReader, t.KeyIOWriter:
switch qid[1] {
case t.IDIOReader, t.IDIOWriter:
return nil, nil, nil
}
}
b := [2]*big.Int{}
if qid := typ.QID(); qid[0] == t.IDBase && qid[1].Key() < t.Key(len(numTypeBounds)) {
b = numTypeBounds[qid[1].Key()]
if qid := typ.QID(); qid[0] == t.IDBase && qid[1] < t.ID(len(numTypeBounds)) {
b = numTypeBounds[qid[1]]
}
// TODO: should || be && instead (see also func typeBounds)? Is this if
// code superfluous?
+3 -3
View File
@@ -80,7 +80,7 @@ func Check(tm *t.Map, files []*a.File, resolveUse func(usePath string) ([]byte,
rMap := reasonMap{}
for _, r := range reasons {
if id := tm.ByName(r.s); id != 0 {
rMap[id.Key()] = r.r
rMap[id] = r.r
}
}
c := &Checker{
@@ -139,7 +139,7 @@ var phases = [...]struct {
type reason func(q *checker, n *a.Assert) error
type reasonMap map[t.Key]reason
type reasonMap map[t.ID]reason
type Checker struct {
tm *t.Map
@@ -359,7 +359,7 @@ func (c *Checker) checkConst(node *a.Node) error {
func (c *Checker) checkConstElement(n *a.Expr, nMin *big.Int, nMax *big.Int, nLists int) error {
if nLists > 0 {
nLists--
if n.Operator().Key() != t.KeyDollar {
if n.Operator() != t.IDDollar {
return fmt.Errorf("invalid const value %q", n.Str(c.tm))
}
for _, o := range n.Args() {
+16 -16
View File
@@ -28,7 +28,7 @@ var reasons = [...]struct {
{`"a < b: b > a"`, func(q *checker, n *a.Assert) error {
op, xa, xb := parseBinaryOp(n.Condition())
if op.Key() != t.KeyXBinaryLessThan {
if op != t.IDXBinaryLessThan {
return errFailed
}
// b > a
@@ -40,7 +40,7 @@ var reasons = [...]struct {
{`"a < b: a < c; c < b"`, func(q *checker, n *a.Assert) error {
op, xa, xb := parseBinaryOp(n.Condition())
if op.Key() != t.KeyXBinaryLessThan {
if op != t.IDXBinaryLessThan {
return errFailed
}
// a < c
@@ -60,7 +60,7 @@ var reasons = [...]struct {
{`"a < b: a < c; c == b"`, func(q *checker, n *a.Assert) error {
op, xa, xb := parseBinaryOp(n.Condition())
if op.Key() != t.KeyXBinaryLessThan {
if op != t.IDXBinaryLessThan {
return errFailed
}
// a < c
@@ -80,7 +80,7 @@ var reasons = [...]struct {
{`"a < b: a == c; c < b"`, func(q *checker, n *a.Assert) error {
op, xa, xb := parseBinaryOp(n.Condition())
if op.Key() != t.KeyXBinaryLessThan {
if op != t.IDXBinaryLessThan {
return errFailed
}
// a == c
@@ -100,7 +100,7 @@ var reasons = [...]struct {
{`"a < b: a < c; c <= b"`, func(q *checker, n *a.Assert) error {
op, xa, xb := parseBinaryOp(n.Condition())
if op.Key() != t.KeyXBinaryLessThan {
if op != t.IDXBinaryLessThan {
return errFailed
}
// a < c
@@ -120,7 +120,7 @@ var reasons = [...]struct {
{`"a < b: a <= c; c < b"`, func(q *checker, n *a.Assert) error {
op, xa, xb := parseBinaryOp(n.Condition())
if op.Key() != t.KeyXBinaryLessThan {
if op != t.IDXBinaryLessThan {
return errFailed
}
// a <= c
@@ -140,7 +140,7 @@ var reasons = [...]struct {
{`"a <= b: b >= a"`, func(q *checker, n *a.Assert) error {
op, xa, xb := parseBinaryOp(n.Condition())
if op.Key() != t.KeyXBinaryLessEq {
if op != t.IDXBinaryLessEq {
return errFailed
}
// b >= a
@@ -152,7 +152,7 @@ var reasons = [...]struct {
{`"a <= b: a <= c; c <= b"`, func(q *checker, n *a.Assert) error {
op, xa, xb := parseBinaryOp(n.Condition())
if op.Key() != t.KeyXBinaryLessEq {
if op != t.IDXBinaryLessEq {
return errFailed
}
// a <= c
@@ -172,7 +172,7 @@ var reasons = [...]struct {
{`"a <= b: a <= c; c == b"`, func(q *checker, n *a.Assert) error {
op, xa, xb := parseBinaryOp(n.Condition())
if op.Key() != t.KeyXBinaryLessEq {
if op != t.IDXBinaryLessEq {
return errFailed
}
// a <= c
@@ -192,7 +192,7 @@ var reasons = [...]struct {
{`"a <= b: a == c; c <= b"`, func(q *checker, n *a.Assert) error {
op, xa, xb := parseBinaryOp(n.Condition())
if op.Key() != t.KeyXBinaryLessEq {
if op != t.IDXBinaryLessEq {
return errFailed
}
// a == c
@@ -212,11 +212,11 @@ var reasons = [...]struct {
{`"a < (b + c): a < c; 0 <= b"`, func(q *checker, n *a.Assert) error {
op, xa, t0 := parseBinaryOp(n.Condition())
if op.Key() != t.KeyXBinaryLessThan {
if op != t.IDXBinaryLessThan {
return errFailed
}
op, xb, xc := parseBinaryOp(t0)
if op.Key() != t.KeyXBinaryPlus {
if op != t.IDXBinaryPlus {
return errFailed
}
// a < c
@@ -232,11 +232,11 @@ var reasons = [...]struct {
{`"a < (b + c): a < (b0 + c0); b0 <= b; c0 <= c"`, func(q *checker, n *a.Assert) error {
op, xa, t0 := parseBinaryOp(n.Condition())
if op.Key() != t.KeyXBinaryLessThan {
if op != t.IDXBinaryLessThan {
return errFailed
}
op, xb, xc := parseBinaryOp(t0)
if op.Key() != t.KeyXBinaryPlus {
if op != t.IDXBinaryPlus {
return errFailed
}
// a < (b0 + c0)
@@ -265,11 +265,11 @@ var reasons = [...]struct {
{`"(a + b) <= c: a <= (c - b)"`, func(q *checker, n *a.Assert) error {
op, t0, xc := parseBinaryOp(n.Condition())
if op.Key() != t.KeyXBinaryLessEq {
if op != t.IDXBinaryLessEq {
return errFailed
}
op, xa, xb := parseBinaryOp(t0)
if op.Key() != t.KeyXBinaryPlus {
if op != t.IDXBinaryPlus {
return errFailed
}
// a <= (c - b)
+1 -1
View File
@@ -128,7 +128,7 @@ func genParseBinaryOps(n *node, arg string) error {
return fmt.Errorf("bad op %q", n.op)
}
fmt.Fprintf(&out, "op, %s, %s := parseBinaryOp(%s)\n", l, r, arg)
fmt.Fprintf(&out, "if op.Key() != t.Key%s { return errFailed }\n", key)
fmt.Fprintf(&out, "if op != t.ID%s { return errFailed }\n", key)
if l[0] == 't' {
if err := genParseBinaryOps(n.lhs, l); err != nil {
return err
+38 -38
View File
@@ -33,16 +33,16 @@ import (
// splitReceiverMethodArgs returns the "receiver", "method" and "args" in the
// expression "receiver.method(args)".
func splitReceiverMethodArgs(n *a.Expr) (receiver *a.Expr, method t.Key, args []*a.Node) {
if n.Operator().Key() != t.KeyOpenParen {
func splitReceiverMethodArgs(n *a.Expr) (receiver *a.Expr, method t.ID, args []*a.Node) {
if n.Operator() != t.IDOpenParen {
return nil, 0, nil
}
args = n.Args()
n = n.LHS().Expr()
if n.Operator().Key() != t.KeyDot {
if n.Operator() != t.IDDot {
return nil, 0, nil
}
return n.LHS().Expr(), n.Ident().Key(), args
return n.LHS().Expr(), n.Ident(), args
}
// TODO: should optimizeNonSuspendible be optimizeExpr and check both
@@ -63,12 +63,12 @@ func (q *checker) optimizeNonSuspendible(n *a.Expr) error {
// TODO: check that nReceiver's type is actually a io_reader or io_writer.
switch nMethod {
case t.KeyCopyFromHistory32:
case t.IDCopyFromHistory32:
return q.optimizeCopyFromHistory32(n)
case t.KeySinceMark:
case t.IDSinceMark:
for _, x := range q.facts {
xReceiver, xMethod, _ := splitReceiverMethodArgs(x)
if xMethod == t.KeyIsMarked && xReceiver.Eq(nReceiver) {
if xMethod == t.IDIsMarked && xReceiver.Eq(nReceiver) {
n.SetBoundsCheckOptimized()
return nil
}
@@ -99,7 +99,7 @@ check0:
for {
for _, x := range q.facts {
xReceiver, xMethod, _ := splitReceiverMethodArgs(x)
if xMethod == t.KeyIsMarked && xReceiver.Eq(nReceiver) {
if xMethod == t.IDIsMarked && xReceiver.Eq(nReceiver) {
break check0
}
}
@@ -110,7 +110,7 @@ check0:
check1:
for {
for _, x := range q.facts {
if x.Operator().Key() != t.KeyXBinaryGreaterThan {
if x.Operator() != t.IDXBinaryGreaterThan {
continue
}
if lhs := x.LHS().Expr(); !lhs.Eq(d) {
@@ -128,13 +128,13 @@ check1:
check2:
for {
for _, x := range q.facts {
if x.Operator().Key() != t.KeyXBinaryLessEq {
if x.Operator() != t.IDXBinaryLessEq {
continue
}
// Check that the LHS is "d as base.u64".
lhs := x.LHS().Expr()
if lhs.Operator().Key() != t.KeyXBinaryAs {
if lhs.Operator() != t.IDXBinaryAs {
continue
}
llhs, lrhs := lhs.LHS().Expr(), lhs.RHS().TypeExpr()
@@ -144,11 +144,11 @@ check2:
// Check that the RHS is "nReceiver.since_mark().length()".
y, method, yArgs := splitReceiverMethodArgs(x.RHS().Expr())
if method != t.KeyLength || len(yArgs) != 0 {
if method != t.IDLength || len(yArgs) != 0 {
continue
}
z, method, zArgs := splitReceiverMethodArgs(y)
if method != t.KeySinceMark || len(zArgs) != 0 {
if method != t.IDSinceMark || len(zArgs) != 0 {
continue
}
if !z.Eq(nReceiver) {
@@ -164,13 +164,13 @@ check2:
check3:
for {
for _, x := range q.facts {
if x.Operator().Key() != t.KeyXBinaryLessEq {
if x.Operator() != t.IDXBinaryLessEq {
continue
}
// Check that the LHS is "l as base.u64".
lhs := x.LHS().Expr()
if lhs.Operator().Key() != t.KeyXBinaryAs {
if lhs.Operator() != t.IDXBinaryAs {
continue
}
llhs, lrhs := lhs.LHS().Expr(), lhs.RHS().TypeExpr()
@@ -180,7 +180,7 @@ check3:
// Check that the RHS is "nReceiver.available()".
y, method, yArgs := splitReceiverMethodArgs(x.RHS().Expr())
if method != t.KeyAvailable || len(yArgs) != 0 {
if method != t.IDAvailable || len(yArgs) != 0 {
continue
}
if !y.Eq(nReceiver) {
@@ -227,13 +227,13 @@ func (q *checker) optimizeSuspendible(n *a.Expr, depth uint32) error {
// TODO: check that nReceiver's type is actually a io_reader or io_writer.
if nMethod == t.KeyUnreadU8 {
if nMethod == t.IDUnreadU8 {
// unread_u8 can never suspend, only succeed or fail.
n.SetProvenNotToSuspend()
return nil
}
if nMethod < t.Key(len(ioMethodAdvances)) {
if nMethod < t.ID(len(ioMethodAdvances)) {
if advance := ioMethodAdvances[nMethod]; advance != nil {
return q.optimizeIOMethodAdvance(n, nReceiver, advance)
}
@@ -244,8 +244,8 @@ func (q *checker) optimizeSuspendible(n *a.Expr, depth uint32) error {
func (q *checker) optimizeIOMethodAdvance(n *a.Expr, receiver *a.Expr, advance *big.Int) error {
return q.facts.update(func(x *a.Expr) (*a.Expr, error) {
op := x.Operator().Key()
if op != t.KeyXBinaryGreaterEq && op != t.KeyXBinaryGreaterThan {
op := x.Operator()
if op != t.IDXBinaryGreaterEq && op != t.IDXBinaryGreaterThan {
return x, nil
}
@@ -256,11 +256,11 @@ func (q *checker) optimizeIOMethodAdvance(n *a.Expr, receiver *a.Expr, advance *
// Check that lhs is "receiver.available()".
lhs := x.LHS().Expr()
if lhs.Operator().Key() != t.KeyOpenParen || len(lhs.Args()) != 0 {
if lhs.Operator() != t.IDOpenParen || len(lhs.Args()) != 0 {
return x, nil
}
lhs = lhs.LHS().Expr()
if lhs.Operator().Key() != t.KeyDot || lhs.Ident().Key() != t.KeyAvailable {
if lhs.Operator() != t.IDDot || lhs.Ident() != t.IDAvailable {
return x, nil
}
lhs = lhs.LHS().Expr()
@@ -272,8 +272,8 @@ func (q *checker) optimizeIOMethodAdvance(n *a.Expr, receiver *a.Expr, advance *
// rcv to be the bytes remaining. If not, discard the fact x.
//
// TODO: mark the call as proven not to suspend.
if op == t.KeyXBinaryGreaterThan {
op = t.KeyXBinaryGreaterEq
if op == t.IDXBinaryGreaterThan {
op = t.IDXBinaryGreaterEq
rcv = big.NewInt(0).Add(rcv, one)
}
if rcv.Cmp(advance) < 0 {
@@ -300,19 +300,19 @@ func (q *checker) optimizeIOMethodAdvance(n *a.Expr, receiver *a.Expr, advance *
}
var ioMethodAdvances = [256]*big.Int{
t.KeyReadU8: one,
t.KeyReadU16BE: two,
t.KeyReadU16LE: two,
t.KeyReadU32BE: four,
t.KeyReadU32LE: four,
t.KeyReadU64BE: eight,
t.KeyReadU64LE: eight,
t.IDReadU8: one,
t.IDReadU16BE: two,
t.IDReadU16LE: two,
t.IDReadU32BE: four,
t.IDReadU32LE: four,
t.IDReadU64BE: eight,
t.IDReadU64LE: eight,
t.KeyWriteU8: one,
t.KeyWriteU16BE: two,
t.KeyWriteU16LE: two,
t.KeyWriteU32BE: four,
t.KeyWriteU32LE: four,
t.KeyWriteU64BE: eight,
t.KeyWriteU64LE: eight,
t.IDWriteU8: one,
t.IDWriteU16BE: two,
t.IDWriteU16LE: two,
t.IDWriteU32BE: four,
t.IDWriteU32LE: four,
t.IDWriteU64BE: eight,
t.IDWriteU64LE: eight,
}
+1 -1
View File
@@ -134,7 +134,7 @@ func parseBuiltInFuncs(tm *t.Map, ss []string, generic bool) (map[t.QQID]*a.Func
}
func (c *Checker) resolveFunc(typ *a.TypeExpr) (*a.Func, error) {
if typ.Decorator().Key() != t.KeyOpenParen {
if typ.Decorator() != t.IDOpenParen {
return nil, fmt.Errorf("check: resolveFunc cannot look up non-func TypeExpr %q", typ.Str(c.tm))
}
lTyp := typ.Receiver()
+112 -112
View File
@@ -149,7 +149,7 @@ func (q *checker) tcheckStatement(n *a.Node) error {
return fmt.Errorf("no matching while/iterate statement for %s%s%s",
n.Keyword().Str(q.tm), sepStr, labelStr)
}
if n.Keyword().Key() == t.KeyBreak {
if n.Keyword() == t.IDBreak {
jumpTarget.SetHasBreak()
} else {
jumpTarget.SetHasContinue()
@@ -178,7 +178,7 @@ func (q *checker) tcheckStatement(n *a.Node) error {
lTyp := n.XType()
rTyp := value.MType()
if n.IterateVariable() {
if lTyp.Decorator().Key() != t.KeyPtr {
if lTyp.Decorator() != t.IDPtr {
return fmt.Errorf("check: iterate variable %q, of type %q, does not have a pointer type",
n.Name().Str(q.tm), lTyp.Str(q.tm))
}
@@ -266,7 +266,7 @@ func (q *checker) tcheckAssign(n *a.Assign) error {
lTyp := lhs.MType()
rTyp := rhs.MType()
if n.Operator().Key() == t.KeyEq {
if n.Operator() == t.IDEq {
return q.tcheckEq(0, lhs, lTyp, rhs, rTyp)
}
@@ -275,14 +275,14 @@ func (q *checker) tcheckAssign(n *a.Assign) error {
n.Operator().Str(q.tm), lhs.Str(q.tm), lTyp.Str(q.tm))
}
switch n.Operator().Key() {
case t.KeyShiftLEq, t.KeyShiftREq:
switch n.Operator() {
case t.IDShiftLEq, t.IDShiftREq:
if !rTyp.IsNumTypeOrIdeal() {
return fmt.Errorf("check: assignment %q: shift %q, of type %q, does not have numeric type",
n.Operator().Str(q.tm), rhs.Str(q.tm), rTyp.Str(q.tm))
}
return nil
case t.KeyTildePlusEq:
case t.IDTildePlusEq:
if !lTyp.IsUnsignedInteger() {
return fmt.Errorf("check: assignment %q: %q, of type %q, does not have unsigned integer type",
n.Operator().Str(q.tm), lhs.Str(q.tm), lTyp.Str(q.tm))
@@ -372,7 +372,7 @@ func (q *checker) tcheckExpr(n *a.Expr, depth uint32) error {
}
func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
switch n.Operator().Key() {
switch n.Operator() {
case 0:
id1 := n.Ident()
if id1.IsNumLiteral(q.tm) {
@@ -403,25 +403,25 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
// structs from used packages.
return fmt.Errorf("check: unrecognized identifier %q", id1.Str(q.tm))
}
switch id1.Key() {
case t.KeyFalse:
switch id1 {
case t.IDFalse:
n.SetConstValue(zero)
n.SetMType(typeExprBool)
return nil
case t.KeyTrue:
case t.IDTrue:
n.SetConstValue(one)
n.SetMType(typeExprBool)
return nil
case t.KeyUnderscore:
case t.IDUnderscore:
// TODO.
case t.KeyThis:
case t.IDThis:
// TODO.
}
case t.KeyOpenParen, t.KeyTry:
case t.IDOpenParen, t.IDTry:
// n is a function call.
// TODO: be consistent about type-checking n.LHS().Expr() or
@@ -429,7 +429,7 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
// require a TypeExpr being able to express function and method types.
// TODO: delete this hack that only matches "foo.decode(etc)".
if isThatMethod(q.tm, n, q.tm.ByName("decode").Key(), 3) {
if isThatMethod(q.tm, n, q.tm.ByName("decode"), 3) {
foo := n.LHS().Expr().LHS().Expr()
if err := q.tcheckExpr(foo, depth); err != nil {
return err
@@ -441,7 +441,7 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
return err
}
}
if n.Operator().Key() == t.KeyTry {
if n.Operator() == t.IDTry {
// TODO: be more principled about inferring "try etc"'s type.
n.SetMType(typeExprStatus)
} else {
@@ -452,7 +452,7 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
return q.tcheckExprCall(n, depth)
case t.KeyOpenBracket:
case t.IDOpenBracket:
// n is an index.
lhs := n.LHS().Expr()
if err := q.tcheckExpr(lhs, depth); err != nil {
@@ -463,7 +463,7 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
return err
}
lTyp := lhs.MType()
if key := lTyp.Decorator().Key(); key != t.KeyArray && key != t.KeySlice {
if key := lTyp.Decorator(); key != t.IDArray && key != t.IDSlice {
return fmt.Errorf("check: %s is an index expression but %s has type %s, not an array or slice type",
n.Str(q.tm), lhs.Str(q.tm), lTyp.Str(q.tm))
}
@@ -475,7 +475,7 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
n.SetMType(lTyp.Inner())
return nil
case t.KeyColon:
case t.IDColon:
// n is a slice.
// TODO: require that the i and j in a[i:j] are *unsigned* (or
// non-negative constants)?
@@ -504,21 +504,21 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
return err
}
lTyp := lhs.MType()
switch lTyp.Decorator().Key() {
switch lTyp.Decorator() {
default:
return fmt.Errorf("check: %s is a slice expression but %s has type %s, not an array or slice type",
n.Str(q.tm), lhs.Str(q.tm), lTyp.Str(q.tm))
case t.KeyArray:
case t.IDArray:
n.SetMType(a.NewTypeExpr(t.IDSlice, 0, 0, nil, nil, lTyp.Inner()))
case t.KeySlice:
case t.IDSlice:
n.SetMType(lTyp)
}
return nil
case t.KeyDot:
case t.IDDot:
return q.tcheckDot(n, depth)
case t.KeyError, t.KeyStatus, t.KeySuspension:
case t.IDError, t.IDStatus, t.IDSuspension:
nominalKeyword := n.Operator()
declaredKeyword := t.ID(0)
if s, ok := q.c.statuses[n.StatusQID()]; ok {
@@ -538,7 +538,7 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
n.SetMType(typeExprStatus)
return nil
case t.KeyDollar:
case t.IDDollar:
for _, o := range n.Args() {
o := o.Expr()
if err := q.tcheckExpr(o, depth); err != nil {
@@ -549,8 +549,8 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
return nil
}
return fmt.Errorf("check: unrecognized token.Key (0x%X) in expression %q for tcheckExprOther",
n.Operator().Key(), n.Str(q.tm))
return fmt.Errorf("check: unrecognized token (0x%X) in expression %q for tcheckExprOther",
n.Operator(), n.Str(q.tm))
}
func (q *checker) tcheckExprCall(n *a.Expr, depth uint32) error {
@@ -586,11 +586,11 @@ func (q *checker) tcheckExprCall(n *a.Expr, depth uint32) error {
}
}
// TODO: distinguish t.KeyOpenParen vs t.KeyTry in a more principled way?
// TODO: distinguish t.IDOpenParen vs t.IDTry in a more principled way?
//
// TODO: figure out calls with and without "?" should interact with the out
// type.
if n.Operator().Key() == t.KeyTry {
if n.Operator() == t.IDTry {
n.SetMType(typeExprStatus)
} else {
outFields := f.Out().Fields()
@@ -614,64 +614,64 @@ func (q *checker) tcheckExprCall(n *a.Expr, depth uint32) error {
return nil
}
func isInSrc(tm *t.Map, n *a.Expr, methodName t.Key, nArgs int) bool {
callSuspendible := methodName != t.KeySinceMark &&
methodName != t.KeyMark &&
methodName != t.KeyLimit
if n.Operator().Key() != t.KeyOpenParen || n.CallSuspendible() != callSuspendible || len(n.Args()) != nArgs {
func isInSrc(tm *t.Map, n *a.Expr, methodName t.ID, nArgs int) bool {
callSuspendible := methodName != t.IDSinceMark &&
methodName != t.IDMark &&
methodName != t.IDLimit
if n.Operator() != t.IDOpenParen || n.CallSuspendible() != callSuspendible || len(n.Args()) != nArgs {
return false
}
n = n.LHS().Expr()
if n.Operator().Key() != t.KeyDot || n.Ident().Key() != methodName {
if n.Operator() != t.IDDot || n.Ident() != methodName {
return false
}
n = n.LHS().Expr()
if n.Operator().Key() != t.KeyDot || n.Ident() != tm.ByName("src") {
if n.Operator() != t.IDDot || n.Ident() != tm.ByName("src") {
return false
}
n = n.LHS().Expr()
return n.Operator() == 0 && n.Ident().Key() == t.KeyIn
return n.Operator() == 0 && n.Ident() == t.IDIn
}
func isInDst(tm *t.Map, n *a.Expr, methodName t.Key, nArgs int) bool {
callSuspendible := methodName != t.KeyCopyFromReader32 &&
methodName != t.KeyCopyFromHistory32 &&
methodName != t.KeyCopyFromSlice32 &&
methodName != t.KeyCopyFromSlice &&
methodName != t.KeySinceMark &&
methodName != t.KeyIsMarked &&
methodName != t.KeyMark &&
methodName != t.KeyLimit
func isInDst(tm *t.Map, n *a.Expr, methodName t.ID, nArgs int) bool {
callSuspendible := methodName != t.IDCopyFromReader32 &&
methodName != t.IDCopyFromHistory32 &&
methodName != t.IDCopyFromSlice32 &&
methodName != t.IDCopyFromSlice &&
methodName != t.IDSinceMark &&
methodName != t.IDIsMarked &&
methodName != t.IDMark &&
methodName != t.IDLimit
// TODO: check that n.Args() is "(x:bar)".
if n.Operator().Key() != t.KeyOpenParen || n.CallSuspendible() != callSuspendible || len(n.Args()) != nArgs {
if n.Operator() != t.IDOpenParen || n.CallSuspendible() != callSuspendible || len(n.Args()) != nArgs {
return false
}
n = n.LHS().Expr()
if n.Operator().Key() != t.KeyDot || n.Ident().Key() != methodName {
if n.Operator() != t.IDDot || n.Ident() != methodName {
return false
}
n = n.LHS().Expr()
if n.Operator().Key() != t.KeyDot || n.Ident() != tm.ByName("dst") {
if n.Operator() != t.IDDot || n.Ident() != tm.ByName("dst") {
return false
}
n = n.LHS().Expr()
return n.Operator() == 0 && n.Ident().Key() == t.KeyIn
return n.Operator() == 0 && n.Ident() == t.IDIn
}
// isThatMethod matches foo.methodName(etc) where etc has nArgs elements.
func isThatMethod(tm *t.Map, n *a.Expr, methodName t.Key, nArgs int) bool {
if k := n.Operator().Key(); k != t.KeyOpenParen && k != t.KeyTry {
func isThatMethod(tm *t.Map, n *a.Expr, methodName t.ID, nArgs int) bool {
if k := n.Operator(); k != t.IDOpenParen && k != t.IDTry {
return false
}
if len(n.Args()) != nArgs {
return false
}
n = n.LHS().Expr()
if n.Operator().Key() != t.KeyDot || n.Ident().Key() != methodName {
if n.Operator() != t.IDDot || n.Ident() != methodName {
return false
}
n = n.LHS().Expr()
if n.Operator().Key() == t.KeyDot &&
if n.Operator() == t.IDDot &&
(n.Ident() == tm.ByName("src") || n.Ident() == tm.ByName("dst")) {
return false
}
@@ -697,7 +697,7 @@ func (q *checker) tcheckDot(n *a.Expr, depth uint32) error {
}
n.SetMType(a.NewTypeExpr(t.IDOpenParen, 0, n.Ident(), lTyp.Node(), nil, nil))
return nil
} else if lTyp.Decorator().Key() != 0 {
} else if lTyp.Decorator() != 0 {
return fmt.Errorf("check: invalid type %q for dot-expression LHS %q", lTyp.Str(q.tm), lhs.Str(q.tm))
}
@@ -714,10 +714,10 @@ func (q *checker) tcheckDot(n *a.Expr, depth uint32) error {
s := (*a.Struct)(nil)
if q.astFunc != nil && lQID[0] == 0 {
switch lQID[1].Key() {
case t.KeyIn:
switch lQID[1] {
case t.IDIn:
s = q.astFunc.In()
case t.KeyOut:
case t.IDOut:
s = q.astFunc.Out()
}
}
@@ -747,14 +747,14 @@ func (q *checker) tcheckExprUnaryOp(n *a.Expr, depth uint32) error {
}
rTyp := rhs.MType()
switch n.Operator().Key() {
case t.KeyXUnaryPlus, t.KeyXUnaryMinus:
switch n.Operator() {
case t.IDXUnaryPlus, t.IDXUnaryMinus:
if !rTyp.IsNumTypeOrIdeal() {
return fmt.Errorf("check: unary %q: %q, of type %q, does not have a numeric type",
n.Operator().AmbiguousForm().Str(q.tm), rhs.Str(q.tm), rTyp.Str(q.tm))
}
if cv := rhs.ConstValue(); cv != nil {
if n.Operator().Key() == t.KeyXUnaryMinus {
if n.Operator() == t.IDXUnaryMinus {
cv = neg(cv)
}
n.SetConstValue(cv)
@@ -762,7 +762,7 @@ func (q *checker) tcheckExprUnaryOp(n *a.Expr, depth uint32) error {
n.SetMType(rTyp.Unrefined())
return nil
case t.KeyXUnaryNot:
case t.IDXUnaryNot:
if !rTyp.IsBool() {
return fmt.Errorf("check: unary %q: %q, of type %q, does not have a boolean type",
n.Operator().AmbiguousForm().Str(q.tm), rhs.Str(q.tm), rTyp.Str(q.tm))
@@ -773,18 +773,18 @@ func (q *checker) tcheckExprUnaryOp(n *a.Expr, depth uint32) error {
n.SetMType(typeExprBool)
return nil
case t.KeyXUnaryRef:
case t.IDXUnaryRef:
// TODO.
case t.KeyXUnaryDeref:
if rTyp.Decorator().Key() != t.KeyPtr { // TODO: t.KeyNptr?
case t.IDXUnaryDeref:
if rTyp.Decorator() != t.IDPtr { // TODO: t.IDNptr?
return fmt.Errorf("check: %q is a dereference of a non-pointer type %q",
n.Str(q.tm), rTyp.Str(q.tm))
}
n.SetMType(rTyp.Inner())
return nil
}
return fmt.Errorf("check: unrecognized token.Key (0x%X) for tcheckExprUnaryOp", n.Operator().Key())
return fmt.Errorf("check: unrecognized token (0x%X) for tcheckExprUnaryOp", n.Operator())
}
func (q *checker) tcheckExprBinaryOp(n *a.Expr, depth uint32) error {
@@ -794,7 +794,7 @@ func (q *checker) tcheckExprBinaryOp(n *a.Expr, depth uint32) error {
}
lTyp := lhs.MType()
op := n.Operator()
if op.Key() == t.KeyXBinaryAs {
if op == t.IDXBinaryAs {
rhs := n.RHS().TypeExpr()
if err := q.tcheckTypeExpr(rhs, 0); err != nil {
return err
@@ -812,7 +812,7 @@ func (q *checker) tcheckExprBinaryOp(n *a.Expr, depth uint32) error {
}
rTyp := rhs.MType()
switch op.Key() {
switch op {
default:
if !lTyp.IsNumTypeOrIdeal() {
return fmt.Errorf("check: binary %q: %q, of type %q, does not have a numeric type",
@@ -822,9 +822,9 @@ func (q *checker) tcheckExprBinaryOp(n *a.Expr, depth uint32) error {
return fmt.Errorf("check: binary %q: %q, of type %q, does not have a numeric type",
op.AmbiguousForm().Str(q.tm), rhs.Str(q.tm), rTyp.Str(q.tm))
}
case t.KeyXBinaryNotEq, t.KeyXBinaryEqEq:
case t.IDXBinaryNotEq, t.IDXBinaryEqEq:
// No-op.
case t.KeyXBinaryAnd, t.KeyXBinaryOr:
case t.IDXBinaryAnd, t.IDXBinaryOr:
if !lTyp.IsBool() {
return fmt.Errorf("check: binary %q: %q, of type %q, does not have a boolean type",
op.AmbiguousForm().Str(q.tm), lhs.Str(q.tm), lTyp.Str(q.tm))
@@ -835,7 +835,7 @@ func (q *checker) tcheckExprBinaryOp(n *a.Expr, depth uint32) error {
}
}
switch op.Key() {
switch op {
default:
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",
@@ -844,7 +844,7 @@ func (q *checker) tcheckExprBinaryOp(n *a.Expr, depth uint32) error {
lTyp.Str(q.tm), rTyp.Str(q.tm),
)
}
case t.KeyXBinaryShiftL, t.KeyXBinaryShiftR:
case t.IDXBinaryShiftL, t.IDXBinaryShiftR:
if lTyp.IsIdeal() && !rTyp.IsIdeal() {
return fmt.Errorf("check: binary %q: %q and %q, of types %q and %q; "+
"cannot shift an ideal number by a non-ideal number",
@@ -855,8 +855,8 @@ func (q *checker) tcheckExprBinaryOp(n *a.Expr, depth uint32) error {
}
}
switch op.Key() {
case t.KeyXBinaryTildePlus:
switch op {
case t.IDXBinaryTildePlus:
typ := lTyp
if typ.IsIdeal() {
typ = rTyp
@@ -885,7 +885,7 @@ func (q *checker) tcheckExprBinaryOp(n *a.Expr, depth uint32) error {
n.SetConstValue(ncv)
}
if comparisonOps[0xFF&op.Key()] {
if comparisonOps[0xFF&op] {
n.SetMType(typeExprBool)
} else if !lTyp.IsIdeal() {
n.SetMType(lTyp.Unrefined())
@@ -897,71 +897,71 @@ func (q *checker) tcheckExprBinaryOp(n *a.Expr, depth uint32) error {
}
func evalConstValueBinaryOp(tm *t.Map, n *a.Expr, l *big.Int, r *big.Int) (*big.Int, error) {
switch n.Operator().Key() {
case t.KeyXBinaryPlus:
switch n.Operator() {
case t.IDXBinaryPlus:
return big.NewInt(0).Add(l, r), nil
case t.KeyXBinaryMinus:
case t.IDXBinaryMinus:
return big.NewInt(0).Sub(l, r), nil
case t.KeyXBinaryStar:
case t.IDXBinaryStar:
return big.NewInt(0).Mul(l, r), nil
case t.KeyXBinarySlash:
case t.IDXBinarySlash:
if r.Sign() == 0 {
return nil, fmt.Errorf("check: division by zero in const expression %q", n.Str(tm))
}
// TODO: decide on Euclidean division vs other definitions. See "go doc
// math/big int.divmod" for details.
return big.NewInt(0).Div(l, r), nil
case t.KeyXBinaryShiftL:
case t.IDXBinaryShiftL:
if r.Sign() < 0 || r.Cmp(ffff) > 0 {
return nil, fmt.Errorf("check: shift %q out of range in const expression %q",
n.RHS().Expr().Str(tm), n.Str(tm))
}
return big.NewInt(0).Lsh(l, uint(r.Uint64())), nil
case t.KeyXBinaryShiftR:
case t.IDXBinaryShiftR:
if r.Sign() < 0 || r.Cmp(ffff) > 0 {
return nil, fmt.Errorf("check: shift %q out of range in const expression %q",
n.RHS().Expr().Str(tm), n.Str(tm))
}
return big.NewInt(0).Rsh(l, uint(r.Uint64())), nil
case t.KeyXBinaryAmp:
case t.IDXBinaryAmp:
return big.NewInt(0).And(l, r), nil
case t.KeyXBinaryAmpHat:
case t.IDXBinaryAmpHat:
return big.NewInt(0).AndNot(l, r), nil
case t.KeyXBinaryPipe:
case t.IDXBinaryPipe:
return big.NewInt(0).Or(l, r), nil
case t.KeyXBinaryHat:
case t.IDXBinaryHat:
return big.NewInt(0).Xor(l, r), nil
case t.KeyXBinaryPercent:
case t.IDXBinaryPercent:
if r.Sign() == 0 {
return nil, fmt.Errorf("check: division by zero in const expression %q", n.Str(tm))
}
return big.NewInt(0).Mod(l, r), nil
case t.KeyXBinaryNotEq:
case t.IDXBinaryNotEq:
return btoi(l.Cmp(r) != 0), nil
case t.KeyXBinaryLessThan:
case t.IDXBinaryLessThan:
return btoi(l.Cmp(r) < 0), nil
case t.KeyXBinaryLessEq:
case t.IDXBinaryLessEq:
return btoi(l.Cmp(r) <= 0), nil
case t.KeyXBinaryEqEq:
case t.IDXBinaryEqEq:
return btoi(l.Cmp(r) == 0), nil
case t.KeyXBinaryGreaterEq:
case t.IDXBinaryGreaterEq:
return btoi(l.Cmp(r) >= 0), nil
case t.KeyXBinaryGreaterThan:
case t.IDXBinaryGreaterThan:
return btoi(l.Cmp(r) > 0), nil
case t.KeyXBinaryAnd:
case t.IDXBinaryAnd:
return btoi((l.Sign() != 0) && (r.Sign() != 0)), nil
case t.KeyXBinaryOr:
case t.IDXBinaryOr:
return btoi((l.Sign() != 0) || (r.Sign() != 0)), nil
case t.KeyXBinaryTildePlus:
case t.IDXBinaryTildePlus:
return nil, fmt.Errorf("check: cannot apply ~+ operator to ideal numbers")
}
return nil, fmt.Errorf("check: unrecognized token.Key (0x%02X) for evalConstValueBinaryOp", n.Operator().Key())
return nil, fmt.Errorf("check: unrecognized token (0x%02X) for evalConstValueBinaryOp", n.Operator())
}
func (q *checker) tcheckExprAssociativeOp(n *a.Expr, depth uint32) error {
switch n.Operator().Key() {
case t.KeyXAssociativePlus, t.KeyXAssociativeStar,
t.KeyXAssociativeAmp, t.KeyXAssociativePipe, t.KeyXAssociativeHat:
switch n.Operator() {
case t.IDXAssociativePlus, t.IDXAssociativeStar,
t.IDXAssociativeAmp, t.IDXAssociativePipe, t.IDXAssociativeHat:
expr, typ := (*a.Expr)(nil), (*a.TypeExpr)(nil)
for _, o := range n.Args() {
@@ -995,7 +995,7 @@ func (q *checker) tcheckExprAssociativeOp(n *a.Expr, depth uint32) error {
n.SetMType(typ)
return nil
case t.KeyXAssociativeAnd, t.KeyXAssociativeOr:
case t.IDXAssociativeAnd, t.IDXAssociativeOr:
for _, o := range n.Args() {
o := o.Expr()
if err := q.tcheckExpr(o, depth); err != nil {
@@ -1010,7 +1010,7 @@ func (q *checker) tcheckExprAssociativeOp(n *a.Expr, depth uint32) error {
return nil
}
return fmt.Errorf("check: unrecognized token.Key (0x%X) for tcheckExprAssociativeOp", n.Operator().Key())
return fmt.Errorf("check: unrecognized token (0x%X) for tcheckExprAssociativeOp", n.Operator())
}
func (q *checker) tcheckTypeExpr(typ *a.TypeExpr, depth uint32) error {
@@ -1020,8 +1020,8 @@ func (q *checker) tcheckTypeExpr(typ *a.TypeExpr, depth uint32) error {
depth++
swtch:
switch typ.Decorator().Key() {
// TODO: also check t.KeyOpenParen.
switch typ.Decorator() {
// TODO: also check t.IDOpenParen.
case 0:
qid := typ.QID()
if qid[0] == t.IDBase && qid[1].IsNumType() {
@@ -1053,7 +1053,7 @@ swtch:
}
return fmt.Errorf("check: %q is not a type", typ.Str(q.tm))
case t.KeyArray:
case t.IDArray:
aLen := typ.ArrayLength()
if err := q.tcheckExpr(aLen, 0); err != nil {
return err
@@ -1063,8 +1063,8 @@ swtch:
}
fallthrough
// TODO: also check t.KeyNptr? Where else should we look for nptr?
case t.KeyPtr, t.KeySlice:
// TODO: also check t.IDNptr? Where else should we look for nptr?
case t.IDPtr, t.IDSlice:
if err := q.tcheckTypeExpr(typ.Inner(), depth); err != nil {
return err
}
@@ -1077,10 +1077,10 @@ swtch:
}
var comparisonOps = [256]bool{
t.KeyXBinaryNotEq: true,
t.KeyXBinaryLessThan: true,
t.KeyXBinaryLessEq: true,
t.KeyXBinaryEqEq: true,
t.KeyXBinaryGreaterEq: true,
t.KeyXBinaryGreaterThan: true,
t.IDXBinaryNotEq: true,
t.IDXBinaryLessThan: true,
t.IDXBinaryLessEq: true,
t.IDXBinaryEqEq: true,
t.IDXBinaryGreaterEq: true,
t.IDXBinaryGreaterThan: true,
}