Fold Expr statements into Assign statements

This commit is contained in:
Nigel Tao committed 2018-12-27 17:36:08 +11:00
1 parent 796c8ccc30
commit c9e6ee6fb3
6 files changed
+34 -38

No files matched your search

+4
View File
@@ -240,6 +240,10 @@ func (h *resumabilityHelper) doAssign(r resumabilities, n *a.Assign, depth uint3
return err return err
} }
if n.LHS() == nil {
return nil
}
// If the LHS is not a local variable (e.g. "this.foo[bar] = etc", or if // If the LHS is not a local variable (e.g. "this.foo[bar] = etc", or if
// the LHS is implicitly also on the RHS (e.g. for a += or *= operator), // the LHS is implicitly also on the RHS (e.g. for a += or *= operator),
// walk the LHS Expr. // walk the LHS Expr.
+1 -13
View File
@@ -39,15 +39,7 @@ func (g *gen) writeStatement(b *buffer, n *a.Node, depth uint32) error {
return nil return nil
} }
mightIntroduceTemporaries := false if (n.Kind() == a.KAssign) && (n.AsAssign().LHS() != nil) && n.AsAssign().RHS().Effect().Coroutine() {
switch n.Kind() {
case a.KAssign:
n := n.AsAssign()
mightIntroduceTemporaries = n.RHS().Effect().Coroutine()
case a.KExpr:
// TODO.
}
if mightIntroduceTemporaries {
// Put n's code into its own block, to restrict the scope of the // Put n's code into its own block, to restrict the scope of the
// tPrefix temporary variables. This helps avoid "jump bypasses // tPrefix temporary variables. This helps avoid "jump bypasses
// variable initialization" compiler warnings with the coroutine // variable initialization" compiler warnings with the coroutine
@@ -71,8 +63,6 @@ func (g *gen) writeStatement(b *buffer, n *a.Node, depth uint32) error {
case a.KAssign: case a.KAssign:
n := n.AsAssign() n := n.AsAssign()
return g.writeStatementAssign(b, n.Operator(), n.LHS(), n.RHS(), depth) return g.writeStatementAssign(b, n.Operator(), n.LHS(), n.RHS(), depth)
case a.KExpr:
return g.writeStatementAssign(b, 0, nil, n.AsExpr(), depth)
case a.KIOBind: case a.KIOBind:
return g.writeStatementIOBind(b, n.AsIOBind(), depth) return g.writeStatementIOBind(b, n.AsIOBind(), depth)
case a.KIf: case a.KIf:
@@ -92,8 +82,6 @@ func (g *gen) writeStatement(b *buffer, n *a.Node, depth uint32) error {
} }
func (g *gen) writeStatementAssign(b *buffer, op t.ID, lhs *a.Expr, rhs *a.Expr, depth uint32) error { func (g *gen) writeStatementAssign(b *buffer, op t.ID, lhs *a.Expr, rhs *a.Expr, depth uint32) error {
// TODO: clean this method body up.
if depth > a.MaxExprDepth { if depth > a.MaxExprDepth {
return fmt.Errorf("expression recursion depth too large") return fmt.Errorf("expression recursion depth too large")
} }
+2 -3
View File
@@ -414,9 +414,9 @@ func NewArg(name t.ID, value *Expr) *Arg {
} }
} }
// Assign is "LHS = RHS" or "LHS op= RHS": // Assign is "LHS = RHS" or "LHS op= RHS" or "RHS":
// - ID0: operator // - ID0: operator
// - LHS: <Expr> // - LHS: <nil|Expr>
// - RHS: <Expr> // - RHS: <Expr>
type Assign Node type Assign Node
@@ -791,7 +791,6 @@ const MaxBodyDepth = 255
// Statement means one of: // Statement means one of:
// - Assert // - Assert
// - Assign // - Assign
// - Expr
// - IOBind // - IOBind
// - If // - If
// - Iterate // - Iterate
+19 -13
View File
@@ -230,12 +230,6 @@ func (q *checker) bcheckStatement(n *a.Node) error {
return err return err
} }
case a.KExpr:
n := n.AsExpr()
if _, err := q.bcheckExpr(n, 0); err != nil {
return err
}
case a.KIOBind: case a.KIOBind:
n := n.AsIOBind() n := n.AsIOBind()
if _, err := q.bcheckExpr(n.IO(), 0); err != nil { if _, err := q.bcheckExpr(n.IO(), 0); err != nil {
@@ -372,14 +366,23 @@ func (q *checker) bcheckAssert(n *a.Assert) error {
} }
func (q *checker) bcheckAssignment(lhs *a.Expr, op t.ID, rhs *a.Expr) error { func (q *checker) bcheckAssignment(lhs *a.Expr, op t.ID, rhs *a.Expr) error {
if _, err := q.bcheckExpr(lhs, 0); err != nil { lTyp := (*a.TypeExpr)(nil)
return err if lhs != nil {
if _, err := q.bcheckExpr(lhs, 0); err != nil {
return err
}
lTyp = lhs.MType()
} }
nb, err := q.bcheckAssignment1(lhs, lhs.MType(), op, rhs)
nb, err := q.bcheckAssignment1(lhs, lTyp, op, rhs)
if err != nil { if err != nil {
return err return err
} }
if lhs == nil {
return nil
}
if op == t.IDEq { if op == t.IDEq {
// Drop any facts involving lhs. // Drop any facts involving lhs.
if err := q.facts.update(func(x *a.Expr) (*a.Expr, error) { if err := q.facts.update(func(x *a.Expr) (*a.Expr, error) {
@@ -457,9 +460,12 @@ func (q *checker) bcheckAssignment1(lhs *a.Expr, lTyp *a.TypeExpr, op t.ID, rhs
return bounds{}, fmt.Errorf("check: internal error: missing LHS for op key 0x%02X", op) return bounds{}, fmt.Errorf("check: internal error: missing LHS for op key 0x%02X", op)
} }
lb, err := q.bcheckTypeExpr(lTyp) lb, err := bounds{}, (error)(nil)
if err != nil { if lTyp != nil {
return bounds{}, err lb, err = q.bcheckTypeExpr(lTyp)
if err != nil {
return bounds{}, err
}
} }
rb := bounds{} rb := bounds{}
@@ -472,7 +478,7 @@ func (q *checker) bcheckAssignment1(lhs *a.Expr, lTyp *a.TypeExpr, op t.ID, rhs
return bounds{}, err return bounds{}, err
} }
if (rb[0].Cmp(lb[0]) < 0) || (rb[1].Cmp(lb[1]) > 0) { if (lTyp != nil) && ((rb[0].Cmp(lb[0]) < 0) || (rb[1].Cmp(lb[1]) > 0)) {
if op == t.IDEq { if op == t.IDEq {
return bounds{}, fmt.Errorf("check: expression %q bounds %v is not within bounds %v", return bounds{}, fmt.Errorf("check: expression %q bounds %v is not within bounds %v",
rhs.Str(q.tm), rb, lb) rhs.Str(q.tm), rb, lb)
+6 -6
View File
@@ -57,9 +57,6 @@ func (q *checker) tcheckStatement(n *a.Node) error {
return err return err
} }
case a.KExpr:
return q.tcheckExpr(n.AsExpr(), 0)
case a.KIf: case a.KIf:
for n := n.AsIf(); n != nil; n = n.ElseIf() { for n := n.AsIf(); n != nil; n = n.ElseIf() {
cond := n.Condition() cond := n.Condition()
@@ -256,12 +253,15 @@ func (q *checker) tcheckEq(lID t.ID, lhs *a.Expr, lTyp *a.TypeExpr, rhs *a.Expr,
} }
func (q *checker) tcheckAssign(n *a.Assign) error { func (q *checker) tcheckAssign(n *a.Assign) error {
lhs := n.LHS()
rhs := n.RHS() rhs := n.RHS()
if err := q.tcheckExpr(lhs, 0); err != nil { if err := q.tcheckExpr(rhs, 0); err != nil {
return err return err
} }
if err := q.tcheckExpr(rhs, 0); err != nil { lhs := n.LHS()
if lhs == nil {
return nil
}
if err := q.tcheckExpr(lhs, 0); err != nil {
return err return err
} }
lTyp := lhs.MType() lTyp := lhs.MType()
+2 -3
View File
@@ -701,6 +701,8 @@ func (p *parser) parseAssignNode() (*a.Node, error) {
rhs.Str(p.tm), p.filename, p.line()) rhs.Str(p.tm), p.filename, p.line())
} }
} }
} else {
op = t.IDEq
} }
if p.funcEffect.WeakerThan(rhs.Effect()) { if p.funcEffect.WeakerThan(rhs.Effect()) {
@@ -708,9 +710,6 @@ func (p *parser) parseAssignNode() (*a.Node, error) {
rhs.Str(p.tm), rhs.Effect(), p.funcEffect, p.filename, p.line()) rhs.Str(p.tm), rhs.Effect(), p.funcEffect, p.filename, p.line())
} }
if lhs == nil {
return rhs.AsNode(), nil
}
return a.NewAssign(op, lhs, rhs).AsNode(), nil return a.NewAssign(op, lhs, rhs).AsNode(), nil
} }