]> Cypherpunks.ru repositories - gostls13.git/blobdiff - src/go/types/api.go
go/types, types2: implement Alias proposal (export API)
[gostls13.git] / src / go / types / api.go
index e202d6dea8eea26780f97c7b49ca99cbcb41620f..6635253fdfdd37f07a5f97293be36bad2b282bd8 100644 (file)
@@ -4,23 +4,23 @@
 
 // Package types declares the data types and implements
 // the algorithms for type-checking of Go packages. Use
-// Config.Check to invoke the type checker for a package.
-// Alternatively, create a new type checker with NewChecker
-// and invoke it incrementally by calling Checker.Files.
+// [Config.Check] to invoke the type checker for a package.
+// Alternatively, create a new type checker with [NewChecker]
+// and invoke it incrementally by calling [Checker.Files].
 //
 // Type-checking consists of several interdependent phases:
 //
 // Name resolution maps each identifier (ast.Ident) in the program to the
-// language object (Object) it denotes.
-// Use Info.{Defs,Uses,Implicits} for the results of name resolution.
+// language object ([Object]) it denotes.
+// Use [Info].{Defs,Uses,Implicits} for the results of name resolution.
 //
 // Constant folding computes the exact constant value (constant.Value)
 // for every expression (ast.Expr) that is a compile-time constant.
 // Use Info.Types[expr].Value for the results of constant folding.
 //
-// Type inference computes the type (Type) of every expression (ast.Expr)
+// [Type] inference computes the type ([Type]) of every expression ([ast.Expr])
 // and checks for compliance with the language specification.
-// Use Info.Types[expr].Type for the results of type inference.
+// Use [Info.Types][expr].Type for the results of type inference.
 //
 // For a tutorial, see https://golang.org/s/types-tutorial.
 package types
@@ -73,7 +73,7 @@ func (e *ArgumentError) Unwrap() error { return e.Err }
 //
 // CAUTION: This interface does not support the import of locally
 // vendored packages. See https://golang.org/s/go15vendor.
-// If possible, external implementations should implement ImporterFrom.
+// If possible, external implementations should implement [ImporterFrom].
 type Importer interface {
        // Import returns the imported package for the given import path.
        // The semantics is like for ImporterFrom.ImportFrom except that
@@ -114,11 +114,11 @@ type Config struct {
        // type checker will initialize this field with a newly created context.
        Context *Context
 
-       // GoVersion describes the accepted Go language version. The string
-       // must follow the format "go%d.%d" (e.g. "go1.12") or it must be
-       // empty; an empty string disables Go language version checks.
-       // If the format is invalid, invoking the type checker will cause a
-       // panic.
+       // GoVersion describes the accepted Go language version. The string must
+       // start with a prefix of the form "go%d.%d" (e.g. "go1.20", "go1.21rc1", or
+       // "go1.21.0") or it must be empty; an empty string disables Go language
+       // version checks. If the format is invalid, invoking the type checker will
+       // result in an error.
        GoVersion string
 
        // If IgnoreFuncBodies is set, function bodies are not
@@ -171,12 +171,11 @@ type Config struct {
        // for unused imports.
        DisableUnusedImportCheck bool
 
-       // If _EnableReverseTypeInference is set, uninstantiated and
-       // partially instantiated generic functions may be assigned
-       // (incl. returned) to variables of function type and type
-       // inference will attempt to infer the missing type arguments.
-       // See proposal go.dev/issue/59338.
-       _EnableReverseTypeInference bool
+       // If a non-empty _ErrorURL format string is provided, it is used
+       // to format an error URL link that is appended to the first line
+       // of an error message. ErrorURL must be a format string containing
+       // exactly one "%s" format, e.g. "[go.dev/e/%s]".
+       _ErrorURL string
 }
 
 func srcimporter_setUsesCgo(conf *Config) {
@@ -286,6 +285,17 @@ type Info struct {
        // in source order. Variables without an initialization expression do not
        // appear in this list.
        InitOrder []*Initializer
+
+       // FileVersions maps a file to its Go version string.
+       // If the file doesn't specify a version, the reported
+       // string is Config.GoVersion.
+       // Version strings begin with “go”, like “go1.21”, and
+       // are suitable for use with the [go/version] package.
+       FileVersions map[*ast.File]string
+}
+
+func (info *Info) recordTypes() bool {
+       return info.Types != nil
 }
 
 // TypeOf returns the type of expression e, or nil if not found.
@@ -305,8 +315,8 @@ func (info *Info) TypeOf(e ast.Expr) Type {
 // ObjectOf returns the object denoted by the specified id,
 // or nil if not found.
 //
-// If id is an embedded struct field, ObjectOf returns the field (*Var)
-// it defines, not the type (*TypeName) it uses.
+// If id is an embedded struct field, [Info.ObjectOf] returns the field (*[Var])
+// it defines, not the type (*[TypeName]) it uses.
 //
 // Precondition: the Uses and Defs maps are populated.
 func (info *Info) ObjectOf(id *ast.Ident) Object {
@@ -316,6 +326,23 @@ func (info *Info) ObjectOf(id *ast.Ident) Object {
        return info.Uses[id]
 }
 
+// PkgNameOf returns the local package name defined by the import,
+// or nil if not found.
+//
+// For dot-imports, the package name is ".".
+//
+// Precondition: the Defs and Implicts maps are populated.
+func (info *Info) PkgNameOf(imp *ast.ImportSpec) *PkgName {
+       var obj Object
+       if imp.Name != nil {
+               obj = info.Defs[imp.Name]
+       } else {
+               obj = info.Implicits[imp]
+       }
+       pkgname, _ := obj.(*PkgName)
+       return pkgname
+}
+
 // TypeAndValue reports the type and value (for constants)
 // of the corresponding expression.
 type TypeAndValue struct {
@@ -377,8 +404,8 @@ func (tv TypeAndValue) HasOk() bool {
 }
 
 // Instance reports the type arguments and instantiated type for type and
-// function instantiations. For type instantiations, Type will be of dynamic
-// type *Named. For function instantiations, Type will be of dynamic type
+// function instantiations. For type instantiations, [Type] will be of dynamic
+// type *[Named]. For function instantiations, [Type] will be of dynamic type
 // *Signature.
 type Instance struct {
        TypeArgs *TypeList
@@ -408,10 +435,10 @@ func (init *Initializer) String() string {
 
 // Check type-checks a package and returns the resulting package object and
 // the first error if any. Additionally, if info != nil, Check populates each
-// of the non-nil maps in the Info struct.
+// of the non-nil maps in the [Info] struct.
 //
 // The package is marked as complete if no errors occurred, otherwise it is
-// incomplete. See Config.Error for controlling behavior in the presence of
+// incomplete. See [Config.Error] for controlling behavior in the presence of
 // errors.
 //
 // The package is specified by a list of *ast.Files and corresponding
@@ -432,7 +459,7 @@ func (conf *Config) Check(path string, fset *token.FileSet, files []*ast.File, i
 func AssertableTo(V *Interface, T Type) bool {
        // Checker.newAssertableTo suppresses errors for invalid types, so we need special
        // handling here.
-       if T.Underlying() == Typ[Invalid] {
+       if !isValid(T.Underlying()) {
                return false
        }
        return (*Checker)(nil).newAssertableTo(nopos, V, T, nil)
@@ -470,7 +497,7 @@ func Implements(V Type, T *Interface) bool {
        }
        // Checker.implements suppresses errors for invalid types, so we need special
        // handling here.
-       if V.Underlying() == Typ[Invalid] {
+       if !isValid(V.Underlying()) {
                return false
        }
        return (*Checker)(nil).implements(0, V, T, false, nil)
@@ -485,14 +512,14 @@ func Satisfies(V Type, T *Interface) bool {
 }
 
 // Identical reports whether x and y are identical types.
-// Receivers of Signature types are ignored.
+// Receivers of [Signature] types are ignored.
 func Identical(x, y Type) bool {
        var c comparer
        return c.identical(x, y, nil)
 }
 
 // IdenticalIgnoreTags reports whether x and y are identical types if tags are ignored.
-// Receivers of Signature types are ignored.
+// Receivers of [Signature] types are ignored.
 func IdenticalIgnoreTags(x, y Type) bool {
        var c comparer
        c.ignoreTags = true