func (p *noder) varDecl(decl *syntax.VarDecl) []ir.Node {
names := p.declNames(ir.ONAME, decl.NameList)
typ := p.typeExprOrNil(decl.Type)
-
- var exprs []ir.Node
- if decl.Values != nil {
- exprs = p.exprList(decl.Values)
- }
+ exprs := p.exprList(decl.Values)
if pragma, ok := decl.Pragma.(*pragmas); ok {
if len(pragma.Embeds) > 0 {
}
func (p *noder) exprList(expr syntax.Expr) []ir.Node {
- if list, ok := expr.(*syntax.ListExpr); ok {
- return p.exprs(list.ElemList)
+ switch expr := expr.(type) {
+ case nil:
+ return nil
+ case *syntax.ListExpr:
+ return p.exprs(expr.ElemList)
+ default:
+ return []ir.Node{p.expr(expr)}
}
- return []ir.Node{p.expr(expr)}
}
func (p *noder) exprs(exprs []syntax.Expr) []ir.Node {
case *syntax.Name:
return p.mkname(expr)
case *syntax.BasicLit:
- n := ir.NewLiteral(p.basicLit(expr))
+ n := ir.NewBasicLit(p.pos(expr), p.basicLit(expr))
if expr.Kind == syntax.RuneLit {
n.SetType(types.UntypedRune)
}
n.SetDiag(expr.Bad) // avoid follow-on errors if there was a syntax error
return n
case *syntax.CompositeLit:
- n := ir.NewCompLitExpr(p.pos(expr), ir.OCOMPLIT, nil, nil)
- if expr.Type != nil {
- n.Ntype = ir.Node(p.expr(expr.Type)).(ir.Ntype)
- }
+ n := ir.NewCompLitExpr(p.pos(expr), ir.OCOMPLIT, p.typeExpr(expr.Type), nil)
l := p.exprs(expr.ElemList)
for i, e := range l {
l[i] = p.wrapname(expr.ElemList[i], e)
n.SetSliceBounds(index[0], index[1], index[2])
return n
case *syntax.AssertExpr:
- return ir.NewTypeAssertExpr(p.pos(expr), p.expr(expr.X), p.typeExpr(expr.Type).(ir.Ntype))
+ return ir.NewTypeAssertExpr(p.pos(expr), p.expr(expr.X), p.typeExpr(expr.Type))
case *syntax.Operation:
if expr.Op == syntax.Add && expr.Y != nil {
return p.sum(expr)
}
return ir.NewBinaryExpr(pos, op, x, y)
case *syntax.CallExpr:
- n := ir.NewCallExpr(p.pos(expr), ir.OCALL, p.expr(expr.Fun), nil)
- n.Args.Set(p.exprs(expr.ArgList))
+ n := ir.NewCallExpr(p.pos(expr), ir.OCALL, p.expr(expr.Fun), p.exprs(expr.ArgList))
n.IsDDD = expr.HasDots
return n
func (p *noder) stmtFall(stmt syntax.Stmt, fallOK bool) ir.Node {
p.setlineno(stmt)
switch stmt := stmt.(type) {
- case *syntax.EmptyStmt:
+ case nil, *syntax.EmptyStmt:
return nil
case *syntax.LabeledStmt:
return p.labeledStmt(stmt, fallOK)
}
return ir.NewGoDeferStmt(p.pos(stmt), op, p.expr(stmt.Call))
case *syntax.ReturnStmt:
- var results []ir.Node
- if stmt.Results != nil {
- results = p.exprList(stmt.Results)
- }
- n := ir.NewReturnStmt(p.pos(stmt), nil)
- n.Results.Set(results)
+ n := ir.NewReturnStmt(p.pos(stmt), p.exprList(stmt.Results))
if len(n.Results) == 0 && ir.CurFunc != nil {
for _, ln := range ir.CurFunc.Dcl {
if ln.Class_ == ir.PPARAM {
func (p *noder) ifStmt(stmt *syntax.IfStmt) ir.Node {
p.openScope(stmt.Pos())
- n := ir.NewIfStmt(p.pos(stmt), nil, nil, nil)
- if stmt.Init != nil {
- *n.PtrInit() = []ir.Node{p.stmt(stmt.Init)}
- }
- if stmt.Cond != nil {
- n.Cond = p.expr(stmt.Cond)
- }
- n.Body.Set(p.blockStmt(stmt.Then))
+ init := p.simpleStmt(stmt.Init)
+ n := ir.NewIfStmt(p.pos(stmt), p.expr(stmt.Cond), p.blockStmt(stmt.Then), nil)
+ *n.PtrInit() = init
if stmt.Else != nil {
e := p.stmt(stmt.Else)
if e.Op() == ir.OBLOCK {
return n
}
- n := ir.NewForStmt(p.pos(stmt), nil, nil, nil, nil)
- if stmt.Init != nil {
- *n.PtrInit() = []ir.Node{p.stmt(stmt.Init)}
- }
- if stmt.Cond != nil {
- n.Cond = p.expr(stmt.Cond)
- }
- if stmt.Post != nil {
- n.Post = p.stmt(stmt.Post)
- }
- n.Body.Set(p.blockStmt(stmt.Body))
+ n := ir.NewForStmt(p.pos(stmt), p.simpleStmt(stmt.Init), p.expr(stmt.Cond), p.stmt(stmt.Post), p.blockStmt(stmt.Body))
p.closeAnotherScope()
return n
}
func (p *noder) switchStmt(stmt *syntax.SwitchStmt) ir.Node {
p.openScope(stmt.Pos())
- n := ir.NewSwitchStmt(p.pos(stmt), nil, nil)
- if stmt.Init != nil {
- *n.PtrInit() = []ir.Node{p.stmt(stmt.Init)}
- }
- if stmt.Tag != nil {
- n.Tag = p.expr(stmt.Tag)
- }
+
+ init := p.simpleStmt(stmt.Init)
+ n := ir.NewSwitchStmt(p.pos(stmt), p.expr(stmt.Tag), nil)
+ *n.PtrInit() = init
var tswitch *ir.TypeSwitchGuard
if l := n.Tag; l != nil && l.Op() == ir.OTYPESW {
}
p.openScope(clause.Pos())
- n := ir.NewCaseStmt(p.pos(clause), nil, nil)
- if clause.Cases != nil {
- n.List.Set(p.exprList(clause.Cases))
- }
+ n := ir.NewCaseStmt(p.pos(clause), p.exprList(clause.Cases), nil)
if tswitch != nil && tswitch.Tag != nil {
nn := typecheck.NewName(tswitch.Tag.Sym())
typecheck.Declare(nn, typecheck.DeclContext)
}
func (p *noder) selectStmt(stmt *syntax.SelectStmt) ir.Node {
- n := ir.NewSelectStmt(p.pos(stmt), nil)
- n.Cases.Set(p.commClauses(stmt.Body, stmt.Rbrace))
- return n
+ return ir.NewSelectStmt(p.pos(stmt), p.commClauses(stmt.Body, stmt.Rbrace))
+}
+
+func (p *noder) simpleStmt(stmt syntax.SimpleStmt) []ir.Node {
+ if stmt == nil {
+ return nil
+ }
+ return []ir.Node{p.stmt(stmt)}
}
func (p *noder) commClauses(clauses []*syntax.CommClause, rbrace syntax.Pos) []ir.Node {
- nodes := make([]ir.Node, 0, len(clauses))
+ nodes := make([]ir.Node, len(clauses))
for i, clause := range clauses {
p.setlineno(clause)
if i > 0 {
}
p.openScope(clause.Pos())
- n := ir.NewCaseStmt(p.pos(clause), nil, nil)
- if clause.Comm != nil {
- n.List = []ir.Node{p.stmt(clause.Comm)}
- }
- n.Body.Set(p.stmts(clause.Body))
- nodes = append(nodes, n)
+ nodes[i] = ir.NewCaseStmt(p.pos(clause), p.simpleStmt(clause.Comm), p.stmts(clause.Body))
}
if len(clauses) > 0 {
p.closeScope(rbrace)