From 3c3c1d8d2856e7859f4ba36b19c91f1538546d2a Mon Sep 17 00:00:00 2001 From: Matthew Dempsky Date: Fri, 9 Jul 2021 17:47:15 -0700 Subject: [dev.typeparams] cmd/compile: more incremental typecheck for unified IR CL 332469 changed the unified IR reader to incrementally typecheck each statement as they're read/constructed. This CL goes further to incrementally typecheck each expression. While here, this CL reorganizes a few things to make this go more smoothly. In particular, it renames expr to expr0 and adds a new expr wrapper that applies typecheck.Expr; gets rid of exprTypeSwitchguard by moving that logic into switchStmt; and splits exprConvert out from exprCall, which simplifies the logic for typechecking the calleee expression somewhat. Change-Id: I6289de9388dc94a947971f4b7213aafeb2faa5dc Reviewed-on: https://go-review.googlesource.com/c/go/+/333730 Run-TryBot: Matthew Dempsky TryBot-Result: Go Bot Trust: Matthew Dempsky Reviewed-by: Cuong Manh Le --- src/cmd/compile/internal/noder/reader.go | 43 ++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 11 deletions(-) (limited to 'src/cmd/compile/internal/noder/reader.go') diff --git a/src/cmd/compile/internal/noder/reader.go b/src/cmd/compile/internal/noder/reader.go index 122bc70f24..19e51d9eba 100644 --- a/src/cmd/compile/internal/noder/reader.go +++ b/src/cmd/compile/internal/noder/reader.go @@ -1252,7 +1252,7 @@ func (r *reader) assignList() ([]*ir.Name, []ir.Node) { continue } - lhs[i] = r.expr() + lhs[i] = typecheck.AssignExpr(r.expr0()) } return names, lhs @@ -1351,7 +1351,21 @@ func (r *reader) switchStmt(label *types.Sym) ir.Node { r.openScope() pos := r.pos() init := r.stmt() - tag := r.expr() + + var tag ir.Node + if r.bool() { + pos := r.pos() + var ident *ir.Ident + if r.bool() { + pos := r.pos() + sym := typecheck.Lookup(r.string()) + ident = ir.NewIdent(pos, sym) + } + x := r.expr() + tag = ir.NewTypeSwitchGuard(pos, ident, x) + } else { + tag = r.expr() + } tswitch, ok := tag.(*ir.TypeSwitchGuard) if ok && tswitch.Tag == nil { @@ -1432,7 +1446,19 @@ func (r *reader) initDefn(defn ir.InitNode, names []*ir.Name) bool { // @@@ Expressions +// expr reads and returns a typechecked expression. func (r *reader) expr() ir.Node { + n := r.expr0() + if n == nil || n.Op() == ir.OTYPE { + // TODO(mdempsky): Push this responsibility up to callers? + return n + } + return typecheck.Expr(n) +} + +// expr0 reads and returns an expression, possibly untypechecked. +// The caller must typecheck the result as appropriate for its context. +func (r *reader) expr0() ir.Node { switch tag := codeExpr(r.code(syncExpr)); tag { default: panic("unhandled expression") @@ -1522,22 +1548,17 @@ func (r *reader) expr() ir.Node { return ir.NewBinaryExpr(pos, op, x, y) case exprCall: - fun := r.expr() + fun := typecheck.Callee(r.expr0()) pos := r.pos() args := r.exprs() dots := r.bool() return typecheck.Call(pos, fun, args, dots) - case exprTypeSwitchGuard: + case exprConvert: + typ := r.typ() pos := r.pos() - var tag *ir.Ident - if r.bool() { - pos := r.pos() - sym := typecheck.Lookup(r.string()) - tag = ir.NewIdent(pos, sym) - } x := r.expr() - return ir.NewTypeSwitchGuard(pos, tag, x) + return ir.NewConvExpr(pos, ir.OCONV, typ, x) } } -- cgit v1.2.3-54-g00ecf