Generate C code for a non-trivial slice-of-slice.

This commit is contained in:
Nigel Tao committed 2017-09-29 17:07:41 +10:00
1 parent ead5e45ae7
commit 3b57548b0d
9 files changed
+221 -44

No files matched your search

+20 -1
View File
@@ -283,7 +283,7 @@ func (q *checker) proveBinaryOp(op t.Key, lhs *a.Expr, rhs *a.Expr) error {
continue
}
factOp := x.ID0().Key()
if factOp == op && x.RHS().Expr().Eq(rhs) {
if opImpliesOp(factOp, op) && x.RHS().Expr().Eq(rhs) {
return nil
}
@@ -309,6 +309,21 @@ func (q *checker) proveBinaryOp(op t.Key, lhs *a.Expr, rhs *a.Expr) error {
return errFailed
}
// 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 {
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
}
return false
}
func errFailedOrNil(ok bool) error {
if ok {
return nil
@@ -319,6 +334,10 @@ func errFailedOrNil(ok bool) error {
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())
}
if err := q.proveBinaryOp(op.Key(), lhs, rhs); err != nil {
n := a.NewExpr(a.FlagsTypeChecked, op, 0, lhs.Node(), nil, rhs.Node(), nil)
return fmt.Errorf("cannot prove %q: %v", n.String(q.tm), err)
+34 -35
View File
@@ -766,52 +766,51 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int,
}
case t.KeyColon:
lhs := n.LHS().Expr()
mhs := n.MHS().Expr()
rhs := n.RHS().Expr()
if mhs == nil && rhs == nil {
return nil, nil, nil
}
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 {
lengthExpr := (*a.Expr)(nil)
if lTyp := lhs.MType(); 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)
id, err := q.tm.Insert(cv.String())
if err != nil {
return nil, nil, err
}
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,
)
}
lengthExpr = a.NewExpr(a.FlagsTypeChecked, 0, id, nil, nil, nil, nil)
lengthExpr.SetConstValue(cv)
lengthExpr.SetMType(typeExprIdeal)
} else {
// Slice of a slice.
return nil, nil, fmt.Errorf("TODO: bcheckExprOther for a non-trivial slice of a slice")
lengthExpr = a.NewExpr(a.FlagsTypeChecked, t.IDDot, t.IDLength, lhs.Node(), nil, nil, nil)
lengthExpr.SetMType(typeExprPlaceholder) // HACK.
lengthExpr = a.NewExpr(a.FlagsTypeChecked, t.IDOpenParen, 0, lengthExpr.Node(), nil, nil, nil)
lengthExpr.SetMType(typeExprU64)
}
if mhs == nil {
mhs = zeroExpr
}
if rhs == nil {
rhs = lengthExpr
}
if mhs != zeroExpr {
if err := proveReasonRequirement(q, t.IDXBinaryLessEq, zeroExpr, mhs); err != nil {
return nil, nil, err
}
}
if err := proveReasonRequirement(q, t.IDXBinaryLessEq, mhs, rhs); err != nil {
return nil, nil, err
}
if rhs != lengthExpr {
if err := proveReasonRequirement(q, t.IDXBinaryLessEq, rhs, lengthExpr); err != nil {
return nil, nil, err
}
}
return nil, nil, nil
+10
View File
@@ -39,6 +39,8 @@ const (
// Key is the high 16 bits of an ID. It is the map key for a Map.
type Key uint32
func (k Key) isXOp() bool { return minXKey <= k && k <= maxXKey }
const (
FlagsBits = 16
FlagsMask = 1<<FlagsBits - 1
@@ -78,6 +80,10 @@ func (x ID) IsAssign() bool { return Flags(x)&FlagsAssign != 0 }
func (x ID) IsImplicitSemicolon() bool { return Flags(x)&FlagsImplicitSemicolon != 0 }
func (x ID) IsNumType() bool { return Flags(x)&FlagsNumType != 0 }
func (x ID) IsXUnaryOp() bool { return x.Key().isXOp() && x.IsUnaryOp() }
func (x ID) IsXBinaryOp() bool { return x.Key().isXOp() && x.IsBinaryOp() }
func (x ID) IsXAssociativeOp() bool { return x.Key().isXOp() && x.IsAssociativeOp() }
// QID is a qualified ID, such as "foo.bar". QID[0] is "foo"'s ID and QID[1] is
// "bar"'s. QID[0] may be 0 for a plain "bar".
type QID [2]ID
@@ -116,6 +122,10 @@ func (t Token) IsAssign() bool { return Flags(t.ID)&FlagsAssign != 0
func (t Token) IsImplicitSemicolon() bool { return Flags(t.ID)&FlagsImplicitSemicolon != 0 }
func (t Token) IsNumType() bool { return Flags(t.ID)&FlagsNumType != 0 }
func (t Token) IsXUnaryOp() bool { return t.ID.Key().isXOp() && t.ID.IsUnaryOp() }
func (t Token) IsXBinaryOp() bool { return t.ID.Key().isXOp() && t.ID.IsBinaryOp() }
func (t Token) IsXAssociativeOp() bool { return t.ID.Key().isXOp() && t.ID.IsAssociativeOp() }
// nBuiltInKeys is the number of built-in Keys. The packing is:
// - Zero is invalid.
// - [0x01, 0x0F] are reserved for implementation details.