github.com/rsc/go@v0.0.0-20150416155037-e040fd465409/src/go/types/package.go (about)

     1  // Copyright 2013 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package types
     6  
     7  import "fmt"
     8  
     9  // A Package describes a Go package.
    10  type Package struct {
    11  	path     string
    12  	name     string
    13  	scope    *Scope
    14  	complete bool
    15  	imports  []*Package
    16  	fake     bool // scope lookup errors are silently dropped if package is fake (internal use only)
    17  }
    18  
    19  // NewPackage returns a new Package for the given package path and name;
    20  // the name must not be the blank identifier.
    21  // The package is not complete and contains no explicit imports.
    22  func NewPackage(path, name string) *Package {
    23  	if name == "_" {
    24  		panic("invalid package name _")
    25  	}
    26  	scope := NewScope(Universe, fmt.Sprintf("package %q", path))
    27  	return &Package{path: path, name: name, scope: scope}
    28  }
    29  
    30  // Path returns the package path.
    31  func (pkg *Package) Path() string { return pkg.path }
    32  
    33  // Name returns the package name.
    34  func (pkg *Package) Name() string { return pkg.name }
    35  
    36  // Scope returns the (complete or incomplete) package scope
    37  // holding the objects declared at package level (TypeNames,
    38  // Consts, Vars, and Funcs).
    39  func (pkg *Package) Scope() *Scope { return pkg.scope }
    40  
    41  // A package is complete if its scope contains (at least) all
    42  // exported objects; otherwise it is incomplete.
    43  func (pkg *Package) Complete() bool { return pkg.complete }
    44  
    45  // MarkComplete marks a package as complete.
    46  func (pkg *Package) MarkComplete() { pkg.complete = true }
    47  
    48  // Imports returns the list of packages explicitly imported by
    49  // pkg; the list is in source order. Package unsafe is excluded.
    50  func (pkg *Package) Imports() []*Package { return pkg.imports }
    51  
    52  // SetImports sets the list of explicitly imported packages to list.
    53  // It is the caller's responsibility to make sure list elements are unique.
    54  func (pkg *Package) SetImports(list []*Package) { pkg.imports = list }
    55  
    56  func (pkg *Package) String() string {
    57  	return fmt.Sprintf("package %s (%q)", pkg.name, pkg.path)
    58  }