]> Cypherpunks.ru repositories - gostls13.git/blobdiff - src/cmd/compile/internal/types2/object.go
go/types, types2: implement Alias proposal (export API)
[gostls13.git] / src / cmd / compile / internal / types2 / object.go
index 844bc34b6a3d1e7034880d667b48b1bedcef87f2..251587224b825451499be0849a96037a5b68e526 100644 (file)
@@ -16,7 +16,6 @@ import (
 // An Object describes a named language entity such as a package,
 // constant, type, variable, function (incl. methods), or label.
 // All objects implement the Object interface.
-//
 type Object interface {
        Parent() *Scope  // scope in which this object is declared; nil for methods and struct fields
        Pos() syntax.Pos // position of object identifier in declaration
@@ -186,6 +185,42 @@ func (obj *object) sameId(pkg *Package, name string) bool {
        return pkg.path == obj.pkg.path
 }
 
+// less reports whether object a is ordered before object b.
+//
+// Objects are ordered nil before non-nil, exported before
+// non-exported, then by name, and finally (for non-exported
+// functions) by package path.
+func (a *object) less(b *object) bool {
+       if a == b {
+               return false
+       }
+
+       // Nil before non-nil.
+       if a == nil {
+               return true
+       }
+       if b == nil {
+               return false
+       }
+
+       // Exported functions before non-exported.
+       ea := isExported(a.name)
+       eb := isExported(b.name)
+       if ea != eb {
+               return ea
+       }
+
+       // Order by name and then (for non-exported names) by package.
+       if a.name != b.name {
+               return a.name < b.name
+       }
+       if !ea {
+               return a.pkg.path < b.pkg.path
+       }
+
+       return false
+}
+
 // A PkgName represents an imported Go package.
 // PkgNames don't have a type.
 type PkgName struct {
@@ -237,11 +272,21 @@ func NewTypeName(pos syntax.Pos, pkg *Package, name string, typ Type) *TypeName
        return &TypeName{object{nil, pos, pkg, name, typ, 0, colorFor(typ), nopos}}
 }
 
+// NewTypeNameLazy returns a new defined type like NewTypeName, but it
+// lazily calls resolve to finish constructing the Named object.
+func NewTypeNameLazy(pos syntax.Pos, pkg *Package, name string, load func(named *Named) (tparams []*TypeParam, underlying Type, methods []*Func)) *TypeName {
+       obj := NewTypeName(pos, pkg, name, nil)
+       NewNamed(obj, nil, nil).loader = load
+       return obj
+}
+
 // IsAlias reports whether obj is an alias name for a type.
 func (obj *TypeName) IsAlias() bool {
        switch t := obj.typ.(type) {
        case nil:
                return false
+       // case *Alias:
+       //      handled by default case
        case *Basic:
                // unsafe.Pointer is not an alias.
                if obj.pkg == Unsafe {
@@ -256,6 +301,8 @@ func (obj *TypeName) IsAlias() bool {
                return obj.pkg != nil || t.name != obj.name || t == universeByte || t == universeRune
        case *Named:
                return obj != t.obj
+       case *TypeParam:
+               return obj != t.obj
        default:
                return true
        }
@@ -267,6 +314,7 @@ type Var struct {
        embedded bool // if set, the variable is an embedded struct field, and name is the type name
        isField  bool // var is struct field
        used     bool // set if the variable was used
+       origin   *Var // if non-nil, the Var from which this one was instantiated
 }
 
 // NewVar returns a new variable.
@@ -282,7 +330,7 @@ func NewParam(pos syntax.Pos, pkg *Package, name string, typ Type) *Var {
 
 // NewField returns a new variable representing a struct field.
 // For embedded fields, the name is the unqualified type name
-/// under which the field is accessible.
+// under which the field is accessible.
 func NewField(pos syntax.Pos, pkg *Package, name string, typ Type, embedded bool) *Var {
        return &Var{object: object{nil, pos, pkg, name, typ, 0, colorFor(typ), nopos}, embedded: embedded, isField: true}
 }
@@ -297,6 +345,20 @@ func (obj *Var) Embedded() bool { return obj.embedded }
 // IsField reports whether the variable is a struct field.
 func (obj *Var) IsField() bool { return obj.isField }
 
+// Origin returns the canonical Var for its receiver, i.e. the Var object
+// recorded in Info.Defs.
+//
+// For synthetic Vars created during instantiation (such as struct fields or
+// function parameters that depend on type arguments), this will be the
+// corresponding Var on the generic (uninstantiated) type. For all other Vars
+// Origin returns the receiver.
+func (obj *Var) Origin() *Var {
+       if obj.origin != nil {
+               return obj.origin
+       }
+       return obj
+}
+
 func (*Var) isDependency() {} // a variable may be a dependency of an initialization expression
 
 // A Func represents a declared function, concrete method, or abstract
@@ -304,7 +366,8 @@ func (*Var) isDependency() {} // a variable may be a dependency of an initializa
 // An abstract method may belong to many interfaces due to embedding.
 type Func struct {
        object
-       hasPtrRecv bool // only valid for methods that don't have a type yet
+       hasPtrRecv_ bool  // only valid for methods that don't have a type yet; use hasPtrRecv() to read
+       origin      *Func // if non-nil, the Func from which this one was instantiated
 }
 
 // NewFunc returns a new function with the given signature, representing
@@ -315,7 +378,7 @@ func NewFunc(pos syntax.Pos, pkg *Package, name string, sig *Signature) *Func {
        if sig != nil {
                typ = sig
        }
-       return &Func{object{nil, pos, pkg, name, typ, 0, colorFor(typ), nopos}, false}
+       return &Func{object{nil, pos, pkg, name, typ, 0, colorFor(typ), nopos}, false, nil}
 }
 
 // FullName returns the package- or receiver-type-qualified name of
@@ -327,36 +390,47 @@ func (obj *Func) FullName() string {
 }
 
 // Scope returns the scope of the function's body block.
+// The result is nil for imported or instantiated functions and methods
+// (but there is also no mechanism to get to an instantiated function).
 func (obj *Func) Scope() *Scope { return obj.typ.(*Signature).scope }
 
-// Less reports whether function a is ordered before function b.
+// Origin returns the canonical Func for its receiver, i.e. the Func object
+// recorded in Info.Defs.
 //
-// Functions are ordered exported before non-exported, then by name,
-// and finally (for non-exported functions) by package path.
-//
-// TODO(gri) The compiler also sorts by package height before package
-//           path for non-exported names.
-func (a *Func) less(b *Func) bool {
-       if a == b {
-               return false
-       }
-
-       // Exported functions before non-exported.
-       ea := isExported(a.name)
-       eb := isExported(b.name)
-       if ea != eb {
-               return ea
+// For synthetic functions created during instantiation (such as methods on an
+// instantiated Named type or interface methods that depend on type arguments),
+// this will be the corresponding Func on the generic (uninstantiated) type.
+// For all other Funcs Origin returns the receiver.
+func (obj *Func) Origin() *Func {
+       if obj.origin != nil {
+               return obj.origin
        }
+       return obj
+}
 
-       // Order by name and then (for non-exported names) by package.
-       if a.name != b.name {
-               return a.name < b.name
-       }
-       if !ea {
-               return a.pkg.path < b.pkg.path
+// Pkg returns the package to which the function belongs.
+//
+// The result is nil for methods of types in the Universe scope,
+// like method Error of the error built-in interface type.
+func (obj *Func) Pkg() *Package { return obj.object.Pkg() }
+
+// hasPtrRecv reports whether the receiver is of the form *T for the given method obj.
+func (obj *Func) hasPtrRecv() bool {
+       // If a method's receiver type is set, use that as the source of truth for the receiver.
+       // Caution: Checker.funcDecl (decl.go) marks a function by setting its type to an empty
+       // signature. We may reach here before the signature is fully set up: we must explicitly
+       // check if the receiver is set (we cannot just look for non-nil obj.typ).
+       if sig, _ := obj.typ.(*Signature); sig != nil && sig.recv != nil {
+               _, isPtr := deref(sig.recv.typ)
+               return isPtr
        }
 
-       return false
+       // If a method's type is not set it may be a method/function that is:
+       // 1) client-supplied (via NewFunc with no signature), or
+       // 2) internally created but not yet type-checked.
+       // For case 1) we can't do anything; the client must know what they are doing.
+       // For case 2) we can use the information gathered by the resolver.
+       return obj.hasPtrRecv_
 }
 
 func (*Func) isDependency() {} // a function may be a dependency of an initialization expression
@@ -407,6 +481,9 @@ func writeObject(buf *bytes.Buffer, obj Object, qf Qualifier) {
        case *TypeName:
                tname = obj
                buf.WriteString("type")
+               if isTypeParam(typ) {
+                       buf.WriteString(" parameter")
+               }
 
        case *Var:
                if obj.isField {
@@ -443,7 +520,7 @@ func writeObject(buf *bytes.Buffer, obj Object, qf Qualifier) {
 
        // For package-level objects, qualify the name.
        if obj.Pkg() != nil && obj.Pkg().scope.Lookup(obj.Name()) == obj {
-               writePackage(buf, obj.Pkg(), qf)
+               buf.WriteString(packagePrefix(obj.Pkg(), qf))
        }
        buf.WriteString(obj.Name())
 
@@ -452,26 +529,41 @@ func writeObject(buf *bytes.Buffer, obj Object, qf Qualifier) {
        }
 
        if tname != nil {
-               // We have a type object: Don't print anything more for
-               // basic types since there's no more information (names
-               // are the same; see also comment in TypeName.IsAlias).
-               if _, ok := typ.(*Basic); ok {
+               switch t := typ.(type) {
+               case *Basic:
+                       // Don't print anything more for basic types since there's
+                       // no more information.
                        return
+               case *Named:
+                       if t.TypeParams().Len() > 0 {
+                               newTypeWriter(buf, qf).tParamList(t.TypeParams().list())
+                       }
                }
                if tname.IsAlias() {
                        buf.WriteString(" =")
+               } else if t, _ := typ.(*TypeParam); t != nil {
+                       typ = t.bound
                } else {
+                       // TODO(gri) should this be fromRHS for *Named?
                        typ = under(typ)
                }
        }
 
+       // Special handling for any: because WriteType will format 'any' as 'any',
+       // resulting in the object string `type any = any` rather than `type any =
+       // interface{}`. To avoid this, swap in a different empty interface.
+       if obj == universeAny {
+               assert(Identical(typ, &emptyInterface))
+               typ = &emptyInterface
+       }
+
        buf.WriteByte(' ')
        WriteType(buf, typ, qf)
 }
 
-func writePackage(buf *bytes.Buffer, pkg *Package, qf Qualifier) {
+func packagePrefix(pkg *Package, qf Qualifier) string {
        if pkg == nil {
-               return
+               return ""
        }
        var s string
        if qf != nil {
@@ -480,9 +572,9 @@ func writePackage(buf *bytes.Buffer, pkg *Package, qf Qualifier) {
                s = pkg.Path()
        }
        if s != "" {
-               buf.WriteString(s)
-               buf.WriteByte('.')
+               s += "."
        }
+       return s
 }
 
 // ObjectString returns the string form of obj.
@@ -520,7 +612,7 @@ func writeFuncName(buf *bytes.Buffer, f *Func, qf Qualifier) {
                        buf.WriteByte(')')
                        buf.WriteByte('.')
                } else if f.pkg != nil {
-                       writePackage(buf, f.pkg, qf)
+                       buf.WriteString(packagePrefix(f.pkg, qf))
                }
        }
        buf.WriteString(f.name)