Skip to content

Commit

Permalink
go/ssa: changes Package.values to objects.
Browse files Browse the repository at this point in the history
Changes the Packages field named values (map[types.Object]Value) to a field named objects (map[types.Object]Member).

This is in preparation for generic functions. Generic functions are expected to be package level Members but not Values in the v0 implementation.

Updates golang/go#48525

Change-Id: I4c283fd3b715eeccad377346adc3ba718a1b741f
Reviewed-on: https://go-review.googlesource.com/c/tools/+/386814
Reviewed-by: Robert Findley <rfindley@google.com>
Run-TryBot: Tim King <taking@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Trust: Tim King <taking@google.com>
  • Loading branch information
timothy-king committed Mar 2, 2022
1 parent a972457 commit e43402d
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 29 deletions.
20 changes: 11 additions & 9 deletions go/ssa/builder.go
Expand Up @@ -341,8 +341,10 @@ func (b *builder) addr(fn *Function, e ast.Expr, escaping bool) lvalue {
return blank{}
}
obj := fn.objectOf(e)
v := fn.Prog.packageLevelValue(obj) // var (address)
if v == nil {
var v Value
if g := fn.Prog.packageLevelMember(obj); g != nil {
v = g.(*Global) // var (address)
} else {
v = fn.lookup(obj, escaping)
}
return &address{addr: v, pos: e.Pos(), expr: e}
Expand Down Expand Up @@ -684,11 +686,11 @@ func (b *builder) expr0(fn *Function, e ast.Expr, tv types.TypeAndValue) Value {
return nilConst(tv.Type)
}
// Package-level func or var?
if v := fn.Prog.packageLevelValue(obj); v != nil {
if _, ok := obj.(*types.Var); ok {
return emitLoad(fn, v) // var (address)
if v := fn.Prog.packageLevelMember(obj); v != nil {
if g, ok := v.(*Global); ok {
return emitLoad(fn, g) // var (address)
}
return v // (func)
return v.(*Function) // (func)
}
// Local var.
return emitLoad(fn, fn.lookup(obj, false)) // var (address)
Expand Down Expand Up @@ -2225,7 +2227,7 @@ func (b *builder) buildFuncDecl(pkg *Package, decl *ast.FuncDecl) {
if isBlankIdent(id) {
return // discard
}
fn := pkg.values[pkg.info.Defs[id]].(*Function)
fn := pkg.objects[pkg.info.Defs[id]].(*Function)
if decl.Recv == nil && id.Name == "init" {
var v Call
v.Call.Value = fn
Expand Down Expand Up @@ -2325,7 +2327,7 @@ func (p *Package) build() {
// 1:1 initialization: var x, y = a(), b()
var lval lvalue
if v := varinit.Lhs[0]; v.Name() != "_" {
lval = &address{addr: p.values[v].(*Global), pos: v.Pos()}
lval = &address{addr: p.objects[v].(*Global), pos: v.Pos()}
} else {
lval = blank{}
}
Expand All @@ -2337,7 +2339,7 @@ func (p *Package) build() {
if v.Name() == "_" {
continue
}
emitStore(init, p.values[v].(*Global), emitExtract(init, tuple, i), v.Pos())
emitStore(init, p.objects[v].(*Global), emitExtract(init, tuple, i), v.Pos())
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions go/ssa/create.go
Expand Up @@ -65,7 +65,7 @@ func memberFromObject(pkg *Package, obj types.Object, syntax ast.Node) {
Value: NewConst(obj.Val(), obj.Type()),
pkg: pkg,
}
pkg.values[obj] = c.Value
pkg.objects[obj] = c
pkg.Members[name] = c

case *types.Var:
Expand All @@ -76,7 +76,7 @@ func memberFromObject(pkg *Package, obj types.Object, syntax ast.Node) {
typ: types.NewPointer(obj.Type()), // address
pos: obj.Pos(),
}
pkg.values[obj] = g
pkg.objects[obj] = g
pkg.Members[name] = g

case *types.Func:
Expand All @@ -99,7 +99,7 @@ func memberFromObject(pkg *Package, obj types.Object, syntax ast.Node) {
fn.Synthetic = "loaded from gc object file"
}

pkg.values[obj] = fn
pkg.objects[obj] = fn
if sig.Recv() == nil {
pkg.Members[name] = fn // package-level function
}
Expand Down Expand Up @@ -166,7 +166,7 @@ func (prog *Program) CreatePackage(pkg *types.Package, files []*ast.File, info *
p := &Package{
Prog: prog,
Members: make(map[string]Member),
values: make(map[types.Object]Value),
objects: make(map[types.Object]Member),
Pkg: pkg,
info: info, // transient (CREATE and BUILD phases)
files: files, // transient (CREATE and BUILD phases)
Expand Down
2 changes: 1 addition & 1 deletion go/ssa/methods.go
Expand Up @@ -118,7 +118,7 @@ func (prog *Program) RuntimeTypes() []types.Type {
// Panic ensues if there is none.
//
func (prog *Program) declaredFunc(obj *types.Func) *Function {
if v := prog.packageLevelValue(obj); v != nil {
if v := prog.packageLevelMember(obj); v != nil {
return v.(*Function)
}
panic("no concrete method: " + obj.String())
Expand Down
18 changes: 9 additions & 9 deletions go/ssa/source.go
Expand Up @@ -123,7 +123,7 @@ func findNamedFunc(pkg *Package, pos token.Pos) *Function {
// Don't call Program.Method: avoid creating wrappers.
obj := mset.At(i).Obj().(*types.Func)
if obj.Pos() == pos {
return pkg.values[obj].(*Function)
return pkg.objects[obj].(*Function)
}
}
}
Expand Down Expand Up @@ -180,14 +180,14 @@ func (prog *Program) Package(obj *types.Package) *Package {
return prog.packages[obj]
}

// packageLevelValue returns the package-level value corresponding to
// packageLevelMember returns the package-level member corresponding to
// the specified named object, which may be a package-level const
// (*Const), var (*Global) or func (*Function) of some package in
// (*NamedConst), var (*Global) or func (*Function) of some package in
// prog. It returns nil if the object is not found.
//
func (prog *Program) packageLevelValue(obj types.Object) Value {
func (prog *Program) packageLevelMember(obj types.Object) Member {
if pkg, ok := prog.packages[obj.Pkg()]; ok {
return pkg.values[obj]
return pkg.objects[obj]
}
return nil
}
Expand All @@ -199,7 +199,7 @@ func (prog *Program) packageLevelValue(obj types.Object) Value {
// result's Signature, both in the params/results and in the receiver.
//
func (prog *Program) FuncValue(obj *types.Func) *Function {
fn, _ := prog.packageLevelValue(obj).(*Function)
fn, _ := prog.packageLevelMember(obj).(*Function)
return fn
}

Expand All @@ -215,8 +215,8 @@ func (prog *Program) ConstValue(obj *types.Const) *Const {
return NewConst(obj.Val(), obj.Type())
}
// Package-level named constant?
if v := prog.packageLevelValue(obj); v != nil {
return v.(*Const)
if v := prog.packageLevelMember(obj); v != nil {
return v.(*NamedConst).Value
}
return NewConst(obj.Val(), obj.Type())
}
Expand Down Expand Up @@ -285,7 +285,7 @@ func (prog *Program) VarValue(obj *types.Var, pkg *Package, ref []ast.Node) (val
}

// Defining ident of package-level var?
if v := prog.packageLevelValue(obj); v != nil {
if v := prog.packageLevelMember(obj); v != nil {
return v.(*Global), true
}

Expand Down
12 changes: 6 additions & 6 deletions go/ssa/ssa.go
Expand Up @@ -45,12 +45,12 @@ type Program struct {
// and unspecified other things too.
//
type Package struct {
Prog *Program // the owning program
Pkg *types.Package // the corresponding go/types.Package
Members map[string]Member // all package members keyed by name (incl. init and init#%d)
values map[types.Object]Value // package members (incl. types and methods), keyed by object
init *Function // Func("init"); the package's init function
debug bool // include full debug info in this package
Prog *Program // the owning program
Pkg *types.Package // the corresponding go/types.Package
Members map[string]Member // all package members keyed by name (incl. init and init#%d)
objects map[types.Object]Member // mapping of package objects to members (incl. methods). Contains *NamedConst, *Global, *Function.
init *Function // Func("init"); the package's init function
debug bool // include full debug info in this package

// The following fields are set transiently, then cleared
// after building.
Expand Down

0 comments on commit e43402d

Please sign in to comment.