// TODO(mdempsky): Split expr into addr, for lvalues.
const (
- exprNone codeExpr = iota
- exprConst
- exprType // type expression
- exprLocal // local variable
- exprGlobal // global variable or function
+ exprConst codeExpr = iota
+ exprType // type expression
+ exprLocal // local variable
+ exprGlobal // global variable or function
exprBlank
exprCompLit
exprFuncLit
pos := r.pos()
init := r.stmt()
- cond := r.expr()
+ cond := r.optExpr()
post := r.stmt()
body := r.blockStmt()
r.closeAnotherScope()
iface = x.Type()
tag = ir.NewTypeSwitchGuard(pos, ident, x)
} else {
- tag = r.expr()
+ tag = r.optExpr()
}
clauses := make([]*ir.CaseClause, r.Len())
default:
panic("unhandled expression")
- case exprNone:
- return nil
-
case exprBlank:
// blank only allowed in LHS of assignments
// TODO(mdempsky): Handle directly in assignList instead?
pos := r.pos()
var index [3]ir.Node
for i := range index {
- index[i] = r.expr()
+ index[i] = r.optExpr()
}
op := ir.OSLICE
if index[2] != nil {
}
}
+func (r *reader) optExpr() ir.Node {
+ if r.Bool() {
+ return r.expr()
+ }
+ return nil
+}
+
func (r *reader) compLit() ir.Node {
r.Sync(pkgbits.SyncCompLit)
pos := r.pos()
} else {
w.pos(stmt)
w.stmt(stmt.Init)
- w.expr(stmt.Cond)
+ w.optExpr(stmt.Cond)
w.stmt(stmt.Post)
}
}
w.expr(guard.X)
} else {
- w.expr(stmt.Tag)
+ w.optExpr(stmt.Tag)
}
w.Len(len(stmt.Body))
// @@@ Expressions
func (w *writer) expr(expr syntax.Expr) {
+ base.Assertf(expr != nil, "missing expression")
+
expr = unparen(expr) // skip parens; unneeded after typecheck
obj, inst := lookupObj(w.p.info, expr)
default:
w.p.unexpected("expression", expr)
- case nil: // absent slice index, for condition, or switch tag
- w.Code(exprNone)
-
case *syntax.Name:
assert(expr.Value == "_")
w.Code(exprBlank)
w.expr(expr.X)
w.pos(expr)
for _, n := range &expr.Index {
- w.expr(n)
+ w.optExpr(n)
}
case *syntax.AssertExpr:
}
}
+func (w *writer) optExpr(expr syntax.Expr) {
+ if w.Bool(expr != nil) {
+ w.expr(expr)
+ }
+}
+
func (w *writer) compLit(lit *syntax.CompositeLit) {
tv, ok := w.p.info.Types[lit]
assert(ok)