mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-22 08:47:28 +01:00
Add length fact inside iterate blocks
This commit is contained in:
1 parent
6f6d4a2530
commit
d85a94360e
1 file changed
+53
-11
+53
-11
@@ -298,18 +298,37 @@ func (q *checker) bcheckStatement(n *a.Node) error {
|
|||||||
// TODO: this isn't right, as the body is a loop, not an
|
// TODO: this isn't right, as the body is a loop, not an
|
||||||
// execute-exactly-once block. We should have pre / inv / post
|
// execute-exactly-once block. We should have pre / inv / post
|
||||||
// conditions, a la bcheckWhile.
|
// conditions, a la bcheckWhile.
|
||||||
q.facts = q.facts[:0]
|
|
||||||
for _, o := range n.Body() {
|
// Check the body.
|
||||||
if err := q.bcheckStatement(o); err != nil {
|
{
|
||||||
return err
|
q.facts = q.facts[:0]
|
||||||
|
for _, o := range n.Variables() {
|
||||||
|
v := o.Var()
|
||||||
|
// TODO: use the XType(), not the Value().MType().
|
||||||
|
q.facts = append(q.facts, makeSliceLengthEqEq(v.Name(), v.Value().MType(), n.Step()))
|
||||||
|
}
|
||||||
|
for _, o := range n.Body() {
|
||||||
|
if err := q.bcheckStatement(o); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
q.facts = q.facts[:0]
|
|
||||||
for _, o := range n.Tail() {
|
// Check the tail.
|
||||||
if err := q.bcheckStatement(o); err != nil {
|
{
|
||||||
return err
|
q.facts = q.facts[:0]
|
||||||
|
for _, o := range n.Variables() {
|
||||||
|
v := o.Var()
|
||||||
|
// TODO: use the XType(), not the Value().MType().
|
||||||
|
q.facts = append(q.facts, makeSliceLengthEqEq(v.Name(), v.Value().MType(), t.ID1))
|
||||||
|
}
|
||||||
|
for _, o := range n.Tail() {
|
||||||
|
if err := q.bcheckStatement(o); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
q.facts = q.facts[:0]
|
q.facts = q.facts[:0]
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
@@ -859,7 +878,7 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int,
|
|||||||
// TODO: don't skip bounds checking p[i] for ptr-typed p.
|
// TODO: don't skip bounds checking p[i] for ptr-typed p.
|
||||||
break
|
break
|
||||||
} else {
|
} else {
|
||||||
lengthExpr = makeSliceLengthExpr(lhs)
|
lengthExpr = makeSliceLength(lhs)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := proveReasonRequirement(q, t.IDXBinaryLessEq, zeroExpr, rhs); err != nil {
|
if err := proveReasonRequirement(q, t.IDXBinaryLessEq, zeroExpr, rhs); err != nil {
|
||||||
@@ -882,7 +901,7 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int,
|
|||||||
if lTyp := lhs.MType(); lTyp.IsArrayType() {
|
if lTyp := lhs.MType(); lTyp.IsArrayType() {
|
||||||
lengthExpr = lTyp.ArrayLength()
|
lengthExpr = lTyp.ArrayLength()
|
||||||
} else {
|
} else {
|
||||||
lengthExpr = makeSliceLengthExpr(lhs)
|
lengthExpr = makeSliceLength(lhs)
|
||||||
}
|
}
|
||||||
|
|
||||||
if mhs == nil {
|
if mhs == nil {
|
||||||
@@ -986,7 +1005,8 @@ func (q *checker) bcheckExprCall(n *a.Expr, depth uint32) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeSliceLengthExpr(slice *a.Expr) *a.Expr {
|
// makeSliceLength returns "x.length()".
|
||||||
|
func makeSliceLength(slice *a.Expr) *a.Expr {
|
||||||
x := a.NewExpr(a.FlagsTypeChecked, t.IDDot, 0, t.IDLength, slice.Node(), nil, nil, nil)
|
x := a.NewExpr(a.FlagsTypeChecked, t.IDDot, 0, t.IDLength, slice.Node(), nil, nil, nil)
|
||||||
x.SetMType(a.NewTypeExpr(t.IDFunc, 0, t.IDLength, slice.MType().Node(), nil, nil))
|
x.SetMType(a.NewTypeExpr(t.IDFunc, 0, t.IDLength, slice.MType().Node(), nil, nil))
|
||||||
x = a.NewExpr(a.FlagsTypeChecked, t.IDOpenParen, 0, 0, x.Node(), nil, nil, nil)
|
x = a.NewExpr(a.FlagsTypeChecked, t.IDOpenParen, 0, 0, x.Node(), nil, nil, nil)
|
||||||
@@ -994,6 +1014,28 @@ func makeSliceLengthExpr(slice *a.Expr) *a.Expr {
|
|||||||
return x
|
return x
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// makeSliceLengthEqEq returns "x.length() == n".
|
||||||
|
//
|
||||||
|
// n must be the t.ID of a small power of 2.
|
||||||
|
func makeSliceLengthEqEq(x t.ID, xTyp *a.TypeExpr, n t.ID) *a.Expr {
|
||||||
|
xExpr := a.NewExpr(a.FlagsTypeChecked, 0, 0, x, nil, nil, nil, nil)
|
||||||
|
xExpr.SetMType(xTyp)
|
||||||
|
|
||||||
|
lhs := makeSliceLength(xExpr)
|
||||||
|
|
||||||
|
nValue := n.SmallPowerOf2Value()
|
||||||
|
if nValue == 0 {
|
||||||
|
panic("check: internal error: makeSliceLengthEqEq called but not with a small power of 2")
|
||||||
|
}
|
||||||
|
rhs := a.NewExpr(a.FlagsTypeChecked, 0, 0, n, nil, nil, nil, nil)
|
||||||
|
rhs.SetConstValue(big.NewInt(int64(nValue)))
|
||||||
|
rhs.SetMType(typeExprIdeal)
|
||||||
|
|
||||||
|
ret := a.NewExpr(a.FlagsTypeChecked, t.IDXBinaryEqEq, 0, 0, lhs.Node(), nil, rhs.Node(), nil)
|
||||||
|
ret.SetMType(typeExprBool)
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
func (q *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 := q.bcheckExpr(n.RHS().Expr(), depth)
|
rMin, rMax, err := q.bcheckExpr(n.RHS().Expr(), depth)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in new issue
Block a user