mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-25 08:55:11 +01:00
Propagate "x == constValue" in bounds checking.
This commit is contained in:
1 parent
793eb043a4
commit
70d62bc608
3 files changed
+43
-24
No files matched your search
+16
-8
@@ -12,6 +12,19 @@ import (
|
|||||||
t "github.com/google/puffs/lang/token"
|
t "github.com/google/puffs/lang/token"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// eqEqOtherHandSide returns the other hand side when n is an expression like
|
||||||
|
// "thisHS == thatHS" or "thatHS == thisHS". If not, it returns nil.
|
||||||
|
func eqEqOtherHandSide(n *a.Expr, thisHS *a.Expr) (thatHS *a.Expr) {
|
||||||
|
if n.ID0().Key() == t.KeyXBinaryEqEq {
|
||||||
|
if thisHS.Eq(n.LHS().Expr()) {
|
||||||
|
return n.RHS().Expr()
|
||||||
|
} else if thisHS.Eq(n.RHS().Expr()) {
|
||||||
|
return n.LHS().Expr()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
type facts []*a.Expr
|
type facts []*a.Expr
|
||||||
|
|
||||||
func (z *facts) appendFact(fact *a.Expr) {
|
func (z *facts) appendFact(fact *a.Expr) {
|
||||||
@@ -24,14 +37,9 @@ func (z *facts) appendFact(fact *a.Expr) {
|
|||||||
|
|
||||||
switch fact.ID0().Key() {
|
switch fact.ID0().Key() {
|
||||||
case 0:
|
case 0:
|
||||||
for _, f := range *z {
|
for _, x := range *z {
|
||||||
if f.ID0().Key() != t.KeyXBinaryEqEq {
|
if other := eqEqOtherHandSide(x, fact); other != nil {
|
||||||
continue
|
z.appendFact(other)
|
||||||
}
|
|
||||||
if fact.Eq(f.LHS().Expr()) {
|
|
||||||
z.appendFact(f.RHS().Expr())
|
|
||||||
} else if fact.Eq(f.RHS().Expr()) {
|
|
||||||
z.appendFact(f.LHS().Expr())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case t.KeyXBinaryAnd:
|
case t.KeyXBinaryAnd:
|
||||||
|
|||||||
+26
-15
@@ -558,23 +558,34 @@ func (q *checker) bcheckExpr(n *a.Expr, depth uint32) (nMin *big.Int, nMax *big.
|
|||||||
if cv := n.ConstValue(); cv != nil {
|
if cv := n.ConstValue(); cv != nil {
|
||||||
nMin, nMax = cv, cv
|
nMin, nMax = cv, cv
|
||||||
} else {
|
} else {
|
||||||
// TODO: look in q.facts for "n == constantValue".
|
// Look in the known facts for "n == constantValue".
|
||||||
switch n.ID0().Flags() & (t.FlagsUnaryOp | t.FlagsBinaryOp | t.FlagsAssociativeOp) {
|
for _, x := range q.facts {
|
||||||
case 0:
|
if other := eqEqOtherHandSide(x, n); other != nil {
|
||||||
nMin, nMax, retErr = q.bcheckExprOther(n, depth)
|
if cv := other.ConstValue(); cv != nil {
|
||||||
case t.FlagsUnaryOp:
|
nMin, nMax = cv, cv
|
||||||
nMin, nMax, retErr = q.bcheckExprUnaryOp(n, depth)
|
break
|
||||||
case t.FlagsBinaryOp:
|
}
|
||||||
if n.ID0().Key() == t.KeyXBinaryAs {
|
|
||||||
nMin, nMax, retErr = q.bcheckExpr(n.LHS().Expr(), depth)
|
|
||||||
} else {
|
|
||||||
nMin, nMax, retErr = q.bcheckExprBinaryOp(n.LHS().Expr(), n.ID0().Key(), n.RHS().Expr(), depth)
|
|
||||||
}
|
}
|
||||||
case t.FlagsAssociativeOp:
|
}
|
||||||
nMin, nMax, retErr = q.bcheckExprAssociativeOp(n, depth)
|
|
||||||
|
|
||||||
default:
|
if nMin == nil {
|
||||||
return nil, nil, fmt.Errorf("check: unrecognized token.Key (0x%X) for bcheckExpr", n.ID0().Key())
|
switch n.ID0().Flags() & (t.FlagsUnaryOp | t.FlagsBinaryOp | t.FlagsAssociativeOp) {
|
||||||
|
case 0:
|
||||||
|
nMin, nMax, retErr = q.bcheckExprOther(n, depth)
|
||||||
|
case t.FlagsUnaryOp:
|
||||||
|
nMin, nMax, retErr = q.bcheckExprUnaryOp(n, depth)
|
||||||
|
case t.FlagsBinaryOp:
|
||||||
|
if n.ID0().Key() == t.KeyXBinaryAs {
|
||||||
|
nMin, nMax, retErr = q.bcheckExpr(n.LHS().Expr(), depth)
|
||||||
|
} else {
|
||||||
|
nMin, nMax, retErr = q.bcheckExprBinaryOp(n.LHS().Expr(), n.ID0().Key(), n.RHS().Expr(), depth)
|
||||||
|
}
|
||||||
|
case t.FlagsAssociativeOp:
|
||||||
|
nMin, nMax, retErr = q.bcheckExprAssociativeOp(n, depth)
|
||||||
|
|
||||||
|
default:
|
||||||
|
return nil, nil, fmt.Errorf("check: unrecognized token.Key (0x%X) for bcheckExpr", n.ID0().Key())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if retErr != nil {
|
if retErr != nil {
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ func TestCheck(t *testing.T) {
|
|||||||
|
|
||||||
x = 0
|
x = 0
|
||||||
x = 1 + (x * 0)
|
x = 1 + (x * 0)
|
||||||
y = -(y * 0) - 1
|
y = -y - 1
|
||||||
y = this.i
|
y = this.i
|
||||||
b = not true
|
b = not true
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user