mirror of
https://github.com/golang/go
synced 2024-11-17 01:24:50 -07:00
[dev.unified] cmd/compile/internal/noder: add pkgWriter.typeOf helper
Getting the type of a value expression is already a very common operation during writing, and it's going to become more common to handle implicit conversions. Change-Id: I5401c6b01546bbf8e85b1ed3fe4acf2835925e2f Reviewed-on: https://go-review.googlesource.com/c/go/+/413395 Reviewed-by: Keith Randall <khr@golang.org> Run-TryBot: Matthew Dempsky <mdempsky@google.com> Reviewed-by: David Chase <drchase@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
parent
c70e93ff3d
commit
9cb784ac69
@ -120,6 +120,14 @@ func (pw *pkgWriter) unexpected(what string, p poser) {
|
|||||||
pw.fatalf(p, "unexpected %s: %v (%T)", what, p, p)
|
pw.fatalf(p, "unexpected %s: %v (%T)", what, p, p)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// typeOf returns the Type of the given value expression.
|
||||||
|
func (pw *pkgWriter) typeOf(expr syntax.Expr) types2.Type {
|
||||||
|
tv, ok := pw.info.Types[expr]
|
||||||
|
assert(ok)
|
||||||
|
assert(tv.IsValue())
|
||||||
|
return tv.Type
|
||||||
|
}
|
||||||
|
|
||||||
// A writer provides APIs for writing out an individual element.
|
// A writer provides APIs for writing out an individual element.
|
||||||
type writer struct {
|
type writer struct {
|
||||||
p *pkgWriter
|
p *pkgWriter
|
||||||
@ -1258,9 +1266,7 @@ func (w *writer) switchStmt(stmt *syntax.SwitchStmt) {
|
|||||||
|
|
||||||
var iface types2.Type
|
var iface types2.Type
|
||||||
if guard, ok := stmt.Tag.(*syntax.TypeSwitchGuard); w.Bool(ok) {
|
if guard, ok := stmt.Tag.(*syntax.TypeSwitchGuard); w.Bool(ok) {
|
||||||
tv, ok := w.p.info.Types[guard.X]
|
iface = w.p.typeOf(guard.X)
|
||||||
assert(ok && tv.IsValue())
|
|
||||||
iface = tv.Type
|
|
||||||
|
|
||||||
w.pos(guard)
|
w.pos(guard)
|
||||||
if tag := guard.Lhs; w.Bool(tag != nil) {
|
if tag := guard.Lhs; w.Bool(tag != nil) {
|
||||||
@ -1410,8 +1416,7 @@ func (w *writer) expr(expr syntax.Expr) {
|
|||||||
w.selector(sel.Obj())
|
w.selector(sel.Obj())
|
||||||
|
|
||||||
case *syntax.IndexExpr:
|
case *syntax.IndexExpr:
|
||||||
tv, ok := w.p.info.Types[expr.Index]
|
_ = w.p.typeOf(expr.Index) // ensure this is an index expression, not an instantiation
|
||||||
assert(ok && tv.IsValue())
|
|
||||||
|
|
||||||
w.Code(exprIndex)
|
w.Code(exprIndex)
|
||||||
w.expr(expr.X)
|
w.expr(expr.X)
|
||||||
@ -1427,13 +1432,12 @@ func (w *writer) expr(expr syntax.Expr) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
case *syntax.AssertExpr:
|
case *syntax.AssertExpr:
|
||||||
tv, ok := w.p.info.Types[expr.X]
|
iface := w.p.typeOf(expr.X)
|
||||||
assert(ok && tv.IsValue())
|
|
||||||
|
|
||||||
w.Code(exprAssert)
|
w.Code(exprAssert)
|
||||||
w.expr(expr.X)
|
w.expr(expr.X)
|
||||||
w.pos(expr)
|
w.pos(expr)
|
||||||
w.exprType(tv.Type, expr.Type, false)
|
w.exprType(iface, expr.Type, false)
|
||||||
|
|
||||||
case *syntax.Operation:
|
case *syntax.Operation:
|
||||||
if expr.Y == nil {
|
if expr.Y == nil {
|
||||||
@ -1523,14 +1527,12 @@ func (w *writer) optExpr(expr syntax.Expr) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (w *writer) compLit(lit *syntax.CompositeLit) {
|
func (w *writer) compLit(lit *syntax.CompositeLit) {
|
||||||
tv, ok := w.p.info.Types[lit]
|
typ := w.p.typeOf(lit)
|
||||||
assert(ok)
|
|
||||||
|
|
||||||
w.Sync(pkgbits.SyncCompLit)
|
w.Sync(pkgbits.SyncCompLit)
|
||||||
w.pos(lit)
|
w.pos(lit)
|
||||||
w.typ(tv.Type)
|
w.typ(typ)
|
||||||
|
|
||||||
typ := tv.Type
|
|
||||||
if ptr, ok := types2.CoreType(typ).(*types2.Pointer); ok {
|
if ptr, ok := types2.CoreType(typ).(*types2.Pointer); ok {
|
||||||
typ = ptr.Elem()
|
typ = ptr.Elem()
|
||||||
}
|
}
|
||||||
@ -1562,9 +1564,7 @@ func (w *writer) compLit(lit *syntax.CompositeLit) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (w *writer) funcLit(expr *syntax.FuncLit) {
|
func (w *writer) funcLit(expr *syntax.FuncLit) {
|
||||||
tv, ok := w.p.info.Types[expr]
|
sig := w.p.typeOf(expr).(*types2.Signature)
|
||||||
assert(ok)
|
|
||||||
sig := tv.Type.(*types2.Signature)
|
|
||||||
|
|
||||||
body, closureVars := w.p.bodyIdx(sig, expr.Body, w.dict)
|
body, closureVars := w.p.bodyIdx(sig, expr.Body, w.dict)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user