mirror of
https://github.com/golang/go
synced 2024-11-19 17:14:44 -07:00
cmd/compile/internal/types: add Pkg and SetPkg methods to Type
The go/types API exposes what package objects were declared in, which includes struct fields, interface methods, and function parameters. The compiler implicitly tracks these for non-exported identifiers (through the Sym's associated Pkg), but exported identifiers always use localpkg. To simplify identifying this, add an explicit package field to struct, interface, and function types. Change-Id: I6adc5dc653e78f058714259845fb3077066eec82 Reviewed-on: https://go-review.googlesource.com/107622 Reviewed-by: Robert Griesemer <gri@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
parent
d292f77e95
commit
03f546eb60
@ -26,9 +26,9 @@ func TestSizeof(t *testing.T) {
|
|||||||
{Type{}, 52, 88},
|
{Type{}, 52, 88},
|
||||||
{Map{}, 20, 40},
|
{Map{}, 20, 40},
|
||||||
{Forward{}, 20, 32},
|
{Forward{}, 20, 32},
|
||||||
{Func{}, 28, 48},
|
{Func{}, 32, 56},
|
||||||
{Struct{}, 12, 24},
|
{Struct{}, 16, 32},
|
||||||
{Interface{}, 4, 8},
|
{Interface{}, 8, 16},
|
||||||
{Chan{}, 8, 16},
|
{Chan{}, 8, 16},
|
||||||
{Array{}, 12, 16},
|
{Array{}, 12, 16},
|
||||||
{DDDField{}, 4, 8},
|
{DDDField{}, 4, 8},
|
||||||
|
@ -132,7 +132,7 @@ type Type struct {
|
|||||||
// TFORW: *Forward
|
// TFORW: *Forward
|
||||||
// TFUNC: *Func
|
// TFUNC: *Func
|
||||||
// TSTRUCT: *Struct
|
// TSTRUCT: *Struct
|
||||||
// TINTER: *Inter
|
// TINTER: *Interface
|
||||||
// TDDDFIELD: DDDField
|
// TDDDFIELD: DDDField
|
||||||
// TFUNCARGS: FuncArgs
|
// TFUNCARGS: FuncArgs
|
||||||
// TCHANARGS: ChanArgs
|
// TCHANARGS: ChanArgs
|
||||||
@ -183,6 +183,40 @@ func (t *Type) SetNoalg(b bool) { t.flags.set(typeNoalg, b) }
|
|||||||
func (t *Type) SetDeferwidth(b bool) { t.flags.set(typeDeferwidth, b) }
|
func (t *Type) SetDeferwidth(b bool) { t.flags.set(typeDeferwidth, b) }
|
||||||
func (t *Type) SetRecur(b bool) { t.flags.set(typeRecur, b) }
|
func (t *Type) SetRecur(b bool) { t.flags.set(typeRecur, b) }
|
||||||
|
|
||||||
|
// Pkg returns the package that t appeared in.
|
||||||
|
//
|
||||||
|
// Pkg is only defined for function, struct, and interface types
|
||||||
|
// (i.e., types with named elements). This information isn't used by
|
||||||
|
// cmd/compile itself, but we need to track it because it's exposed by
|
||||||
|
// the go/types API.
|
||||||
|
func (t *Type) Pkg() *Pkg {
|
||||||
|
switch t.Etype {
|
||||||
|
case TFUNC:
|
||||||
|
return t.Extra.(*Func).pkg
|
||||||
|
case TSTRUCT:
|
||||||
|
return t.Extra.(*Struct).pkg
|
||||||
|
case TINTER:
|
||||||
|
return t.Extra.(*Interface).pkg
|
||||||
|
default:
|
||||||
|
Fatalf("Pkg: unexpected kind: %v", t)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetPkg sets the package that t appeared in.
|
||||||
|
func (t *Type) SetPkg(pkg *Pkg) {
|
||||||
|
switch t.Etype {
|
||||||
|
case TFUNC:
|
||||||
|
t.Extra.(*Func).pkg = pkg
|
||||||
|
case TSTRUCT:
|
||||||
|
t.Extra.(*Struct).pkg = pkg
|
||||||
|
case TINTER:
|
||||||
|
t.Extra.(*Interface).pkg = pkg
|
||||||
|
default:
|
||||||
|
Fatalf("Pkg: unexpected kind: %v", t)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Map contains Type fields specific to maps.
|
// Map contains Type fields specific to maps.
|
||||||
type Map struct {
|
type Map struct {
|
||||||
Key *Type // Key type
|
Key *Type // Key type
|
||||||
@ -218,6 +252,7 @@ type Func struct {
|
|||||||
Params *Type // function params
|
Params *Type // function params
|
||||||
|
|
||||||
Nname *Node
|
Nname *Node
|
||||||
|
pkg *Pkg
|
||||||
|
|
||||||
// Argwid is the total width of the function receiver, params, and results.
|
// Argwid is the total width of the function receiver, params, and results.
|
||||||
// It gets calculated via a temporary TFUNCARGS type.
|
// It gets calculated via a temporary TFUNCARGS type.
|
||||||
@ -236,6 +271,7 @@ func (t *Type) FuncType() *Func {
|
|||||||
// StructType contains Type fields specific to struct types.
|
// StructType contains Type fields specific to struct types.
|
||||||
type Struct struct {
|
type Struct struct {
|
||||||
fields Fields
|
fields Fields
|
||||||
|
pkg *Pkg
|
||||||
|
|
||||||
// Maps have three associated internal structs (see struct MapType).
|
// Maps have three associated internal structs (see struct MapType).
|
||||||
// Map links such structs back to their map type.
|
// Map links such structs back to their map type.
|
||||||
@ -263,6 +299,7 @@ func (t *Type) StructType() *Struct {
|
|||||||
// Interface contains Type fields specific to interface types.
|
// Interface contains Type fields specific to interface types.
|
||||||
type Interface struct {
|
type Interface struct {
|
||||||
Fields Fields
|
Fields Fields
|
||||||
|
pkg *Pkg
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ptr contains Type fields specific to pointer types.
|
// Ptr contains Type fields specific to pointer types.
|
||||||
|
Loading…
Reference in New Issue
Block a user