mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-23 16:52:18 +01:00
Actually check assertions.
This commit is contained in:
1 parent
06eaadb1d6
commit
5bc5e43335
3 files changed
+184
-34
No files matched your search
+134
-2
@@ -4,14 +4,46 @@
|
|||||||
package check
|
package check
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
"math/big"
|
"math/big"
|
||||||
|
|
||||||
a "github.com/google/puffs/lang/ast"
|
a "github.com/google/puffs/lang/ast"
|
||||||
t "github.com/google/puffs/lang/token"
|
t "github.com/google/puffs/lang/token"
|
||||||
)
|
)
|
||||||
|
|
||||||
// binds returns fact's lower or upper bound for id.
|
type facts []*a.Expr
|
||||||
func binds(fact *a.Expr, id t.ID) (*big.Int, *big.Int) {
|
|
||||||
|
func (z *facts) appendFact(x *a.Expr) {
|
||||||
|
// TODO: make this faster than O(N) by keeping facts sorted somehow?
|
||||||
|
for _, f := range *z {
|
||||||
|
if f.Eq(x) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*z = append(*z, x)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (z facts) refine(n *a.Expr, min *big.Int, max *big.Int) (*big.Int, *big.Int) {
|
||||||
|
if n.ID0() != 0 || !n.ID1().IsIdent() {
|
||||||
|
// TODO.
|
||||||
|
return min, max
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, f := range z {
|
||||||
|
f0, f1 := refine(f, n.ID1())
|
||||||
|
if f0 != nil && min.Cmp(f0) < 0 {
|
||||||
|
min = f0
|
||||||
|
}
|
||||||
|
if f1 != nil && max.Cmp(f1) > 0 {
|
||||||
|
max = f1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return min, max
|
||||||
|
}
|
||||||
|
|
||||||
|
// refine returns fact's lower or upper bound for id.
|
||||||
|
func refine(fact *a.Expr, id t.ID) (*big.Int, *big.Int) {
|
||||||
op := fact.ID0()
|
op := fact.ID0()
|
||||||
if !op.IsBinaryOp() || op.Key() == t.KeyAs {
|
if !op.IsBinaryOp() || op.Key() == t.KeyAs {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
@@ -62,3 +94,103 @@ func binds(fact *a.Expr, id t.ID) (*big.Int, *big.Int) {
|
|||||||
|
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func argValue(m *t.IDMap, args []*a.Node, name string) *a.Expr {
|
||||||
|
if x := m.ByName(name); x != 0 {
|
||||||
|
for _, a := range args {
|
||||||
|
if a.Arg().Name() == x {
|
||||||
|
return a.Arg().Value()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// parseBinaryOp parses n as "lhs op rhs".
|
||||||
|
func parseBinaryOp(n *a.Expr) (op t.Key, lhs *a.Expr, rhs *a.Expr) {
|
||||||
|
if !n.ID0().IsBinaryOp() {
|
||||||
|
return 0, nil, nil
|
||||||
|
}
|
||||||
|
op = n.ID0().Key()
|
||||||
|
if op == t.KeyAs {
|
||||||
|
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) {
|
||||||
|
switch op {
|
||||||
|
case t.KeyXBinaryNotEq:
|
||||||
|
return lMax.Cmp(rMin) < 0 || lMin.Cmp(rMax) > 0
|
||||||
|
case t.KeyXBinaryLessThan:
|
||||||
|
return lMax.Cmp(rMin) < 0
|
||||||
|
case t.KeyXBinaryLessEq:
|
||||||
|
return lMax.Cmp(rMin) <= 0
|
||||||
|
case t.KeyXBinaryEqEq:
|
||||||
|
return lMin.Cmp(rMax) == 0 && lMax.Cmp(rMin) == 0
|
||||||
|
case t.KeyXBinaryGreaterEq:
|
||||||
|
return lMin.Cmp(rMax) >= 0
|
||||||
|
case t.KeyXBinaryGreaterThan:
|
||||||
|
return lMin.Cmp(rMax) > 0
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *checker) proveBinaryOp(op t.Key, lhs *a.Expr, rhs *a.Expr) (ok bool) {
|
||||||
|
if cv := lhs.ConstValue(); cv != nil {
|
||||||
|
rMin, rMax, err := q.bcheckExpr(rhs, 0)
|
||||||
|
if err != nil {
|
||||||
|
// TODO: should this function return the error?
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if proveBinaryOpConstValues(op, cv, cv, rMin, rMax) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if cv := rhs.ConstValue(); cv != nil {
|
||||||
|
lMin, lMax, err := q.bcheckExpr(lhs, 0)
|
||||||
|
if err != nil {
|
||||||
|
// TODO: should this function return the error?
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if proveBinaryOpConstValues(op, lMin, lMax, cv, cv) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, f := range q.facts {
|
||||||
|
if f.ID0().Key() == op && f.LHS().Expr().Eq(lhs) && f.RHS().Expr().Eq(rhs) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
var errFailed = errors.New("failed")
|
||||||
|
|
||||||
|
type reason func(q *checker, n *a.Assert) error
|
||||||
|
|
||||||
|
type reasonMap map[t.Key]reason
|
||||||
|
|
||||||
|
var reasons = [...]struct {
|
||||||
|
s string
|
||||||
|
r reason
|
||||||
|
}{
|
||||||
|
{`"a < b: a < c <= b"`, func(q *checker, n *a.Assert) error {
|
||||||
|
c := argValue(q.idMap, n.Args(), "c")
|
||||||
|
if c == nil {
|
||||||
|
return errFailed
|
||||||
|
}
|
||||||
|
op, a, b := parseBinaryOp(n.Condition())
|
||||||
|
if op != t.KeyXBinaryLessThan {
|
||||||
|
return errFailed
|
||||||
|
}
|
||||||
|
if !q.proveBinaryOp(t.KeyXBinaryLessThan, a, c) {
|
||||||
|
return fmt.Errorf("cannot prove \"%s < %s\"", a.String(q.idMap), c.String(q.idMap))
|
||||||
|
}
|
||||||
|
if !q.proveBinaryOp(t.KeyXBinaryLessEq, c, b) {
|
||||||
|
return fmt.Errorf("cannot prove \"%s <= %s\"", c.String(q.idMap), b.String(q.idMap))
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}},
|
||||||
|
}
|
||||||
+25
-18
@@ -53,8 +53,27 @@ func (c *checker) bcheckStatement(n *a.Node) error {
|
|||||||
|
|
||||||
switch n.Kind() {
|
switch n.Kind() {
|
||||||
case a.KAssert:
|
case a.KAssert:
|
||||||
// TODO: check the assertion.
|
o := n.Assert()
|
||||||
c.facts = append(c.facts, n.Assert().Condition())
|
condition := o.Condition()
|
||||||
|
err := errFailed
|
||||||
|
if reasonID := o.Reason(); reasonID != 0 {
|
||||||
|
if reasonFunc := c.reasonMap[reasonID.Key()]; reasonFunc != nil {
|
||||||
|
err = reasonFunc(c, o)
|
||||||
|
} else {
|
||||||
|
err = fmt.Errorf("no such reason %s", reasonID.String(c.idMap))
|
||||||
|
}
|
||||||
|
} else if condition.ID0().IsBinaryOp() && condition.ID0().Key() != t.KeyAs {
|
||||||
|
if c.proveBinaryOp(condition.ID0().Key(), condition.LHS().Expr(), condition.RHS().Expr()) {
|
||||||
|
err = nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
if err == errFailed {
|
||||||
|
return fmt.Errorf("check: cannot prove %q", condition.String(c.idMap))
|
||||||
|
}
|
||||||
|
return fmt.Errorf("check: cannot prove %q: %v", condition.String(c.idMap), err)
|
||||||
|
}
|
||||||
|
c.facts.appendFact(condition)
|
||||||
|
|
||||||
case a.KAssign:
|
case a.KAssign:
|
||||||
o := n.Assign()
|
o := n.Assign()
|
||||||
@@ -162,9 +181,9 @@ func (c *checker) bcheckWhile(n *a.While) error {
|
|||||||
if m.Assert().Keyword().Key() == t.KeyPost {
|
if m.Assert().Keyword().Key() == t.KeyPost {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
c.facts = append(c.facts, m.Assert().Condition())
|
c.facts.appendFact(m.Assert().Condition())
|
||||||
}
|
}
|
||||||
c.facts = append(c.facts, n.Condition())
|
c.facts.appendFact(n.Condition())
|
||||||
for _, m := range n.Body() {
|
for _, m := range n.Body() {
|
||||||
if err := c.bcheckStatement(m); err != nil {
|
if err := c.bcheckStatement(m); err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -179,7 +198,7 @@ func (c *checker) bcheckWhile(n *a.While) error {
|
|||||||
if m.Assert().Keyword().Key() == t.KeyPre {
|
if m.Assert().Keyword().Key() == t.KeyPre {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
c.facts = append(c.facts, m.Assert().Condition())
|
c.facts.appendFact(m.Assert().Condition())
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -199,19 +218,7 @@ func (c *checker) bcheckExpr(n *a.Expr, depth uint32) (*big.Int, *big.Int, error
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
if n.ID0() == 0 && n.ID1().IsIdent() {
|
n0, n1 = c.facts.refine(n, n0, n1)
|
||||||
for _, f := range c.facts {
|
|
||||||
f0, f1 := binds(f, n.ID1())
|
|
||||||
if f0 != nil && n0.Cmp(f0) < 0 {
|
|
||||||
n0 = f0
|
|
||||||
}
|
|
||||||
if f1 != nil && n1.Cmp(f1) > 0 {
|
|
||||||
n1 = f1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// TODO: propagate facts about e.g. "this.foo".
|
|
||||||
}
|
|
||||||
return n0, n1, nil
|
return n0, n1, nil
|
||||||
case t.FlagsUnaryOp:
|
case t.FlagsUnaryOp:
|
||||||
return c.bcheckExprUnaryOp(n, depth)
|
return c.bcheckExprUnaryOp(n, depth)
|
||||||
|
|||||||
+25
-14
@@ -82,11 +82,19 @@ func Check(m *t.IDMap, files ...*a.File) (*Checker, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
c := &Checker{
|
rMap := reasonMap{}
|
||||||
idMap: m,
|
for _, r := range reasons {
|
||||||
structs: map[t.ID]Struct{},
|
if id := m.ByName(r.s); id != 0 {
|
||||||
funcs: map[t.QID]Func{},
|
rMap[id.Key()] = r.r
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
c := &Checker{
|
||||||
|
idMap: m,
|
||||||
|
reasonMap: rMap,
|
||||||
|
structs: map[t.ID]Struct{},
|
||||||
|
funcs: map[t.QID]Func{},
|
||||||
|
}
|
||||||
|
|
||||||
for _, phase := range phases {
|
for _, phase := range phases {
|
||||||
for _, f := range files {
|
for _, f := range files {
|
||||||
for _, n := range f.TopLevelDecls() {
|
for _, n := range f.TopLevelDecls() {
|
||||||
@@ -115,9 +123,10 @@ var phases = [...]struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Checker struct {
|
type Checker struct {
|
||||||
idMap *t.IDMap
|
idMap *t.IDMap
|
||||||
structs map[t.ID]Struct
|
reasonMap reasonMap
|
||||||
funcs map[t.QID]Func
|
structs map[t.ID]Struct
|
||||||
|
funcs map[t.QID]Func
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Checker) Funcs() map[t.QID]Func { return c.funcs }
|
func (c *Checker) Funcs() map[t.QID]Func { return c.funcs }
|
||||||
@@ -261,9 +270,10 @@ func (c *Checker) checkFuncContract(n *a.Node) error {
|
|||||||
func (c *Checker) checkFuncBody(n *a.Node) error {
|
func (c *Checker) checkFuncBody(n *a.Node) error {
|
||||||
f := n.Func()
|
f := n.Func()
|
||||||
q := &checker{
|
q := &checker{
|
||||||
c: c,
|
c: c,
|
||||||
idMap: c.idMap,
|
idMap: c.idMap,
|
||||||
f: c.funcs[f.QID()],
|
reasonMap: c.reasonMap,
|
||||||
|
f: c.funcs[f.QID()],
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fill in the TypeMap with all local variables. Note that they have
|
// Fill in the TypeMap with all local variables. Note that they have
|
||||||
@@ -329,12 +339,13 @@ func (c *Checker) checkFuncBody(n *a.Node) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type checker struct {
|
type checker struct {
|
||||||
c *Checker
|
c *Checker
|
||||||
idMap *t.IDMap
|
idMap *t.IDMap
|
||||||
f Func
|
reasonMap reasonMap
|
||||||
|
f Func
|
||||||
|
|
||||||
errFilename string
|
errFilename string
|
||||||
errLine uint32
|
errLine uint32
|
||||||
|
|
||||||
facts []*a.Expr
|
facts facts
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user