Generate C code for a[i:j] with explicit i or j.

This commit is contained in:
Nigel Tao committed 2017-09-28 15:57:25 +10:00
1 parent f259d3c401
commit a9bdeee7c1
8 files changed
+159 -38

No files matched your search

+48 -1
View File
@@ -642,6 +642,11 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int,
// No-op.
case t.KeyOpenParen, t.KeyTry:
_, _, err := q.bcheckExpr(n.LHS().Expr(), depth)
if 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.KeyReadU16BE, 0) || isInSrc(q.tm, n, t.KeyReadU32BE, 0) ||
@@ -766,7 +771,49 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int,
if mhs == nil && rhs == nil {
return nil, nil, nil
}
return nil, nil, fmt.Errorf("TODO: bcheckExprOther for a non-trivial slice")
lTyp := n.LHS().Expr().MType()
mMin, mMax := (*big.Int)(nil), (*big.Int)(nil)
if mhs != nil {
var err error
mMin, mMax, err = q.bcheckExpr(mhs, depth)
if err != nil {
return nil, nil, err
}
}
rMin, rMax := (*big.Int)(nil), (*big.Int)(nil)
if rhs != nil {
var err error
rMin, rMax, err = q.bcheckExpr(rhs, depth)
if err != nil {
return nil, nil, err
}
}
if lTyp.Decorator().Key() == t.KeyOpenBracket {
// Slice of an array.
cv := lTyp.ArrayLength().ConstValue()
if mhs != nil && (mMin.Cmp(zero) < 0 || mMax.Cmp(cv) > 0) {
return nil, nil, fmt.Errorf("check: slice index %q (of range %v..%v) out of range",
mhs.String(q.tm), mMin, mMax)
}
if rhs != nil && (rMin.Cmp(zero) < 0 || rMax.Cmp(cv) > 0) {
return nil, nil, fmt.Errorf("check: slice index %q (of range %v..%v) out of range",
rhs.String(q.tm), rMin, rMax)
}
if mhs != nil && rhs != nil && mMax.Cmp(rMin) > 0 {
return nil, nil, fmt.Errorf("check: slice indexes "+
"%q (of range %v..%v) and %q (of range %v..%v) overlap",
mhs.String(q.tm), mMin, mMax,
rhs.String(q.tm), rMin, rMax,
)
}
} else {
// Slice of a slice.
return nil, nil, fmt.Errorf("TODO: bcheckExprOther for a non-trivial slice of a slice")
}
return nil, nil, nil
case t.KeyDot:
// TODO: delete this hack that only matches "in".
+7
View File
@@ -342,6 +342,11 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
case t.KeyOpenParen, t.KeyTry:
// n is a function call.
// TODO: be consistent about type-checking n.LHS().Expr() or
// n.LHS().Expr().LHS().Expr(). Doing this properly will probably
// require a TypeExpr being able to express function and method types.
// 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.KeyReadU16BE, 0) || isInSrc(q.tm, n, t.KeyReadU32BE, 0) ||
@@ -489,6 +494,8 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
case t.KeyColon:
// n is a slice.
// TODO: require that the i and j in a[i:j] are *unsigned* (or
// non-negative constants)?
if mhs := n.MHS().Expr(); mhs != nil {
if err := q.tcheckExpr(mhs, depth); err != nil {
return err