Source file src/cmd/compile/internal/types2/package.go

     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 types2
     6  
     7  import (
     8  	"fmt"
     9  )
    10  
    11  // A Package describes a Go package.
    12  type Package struct {
    13  	path     string
    14  	name     string
    15  	scope    *Scope
    16  	imports  []*Package
    17  	height   int
    18  	complete bool
    19  	fake     bool // scope lookup errors are silently dropped if package is fake (internal use only)
    20  	cgo      bool // uses of this package will be rewritten into uses of declarations from _cgo_gotypes.go
    21  }
    22  
    23  // NewPackage returns a new Package for the given package path and name.
    24  // The package is not complete and contains no explicit imports.
    25  func NewPackage(path, name string) *Package {
    26  	return NewPackageHeight(path, name, 0)
    27  }
    28  
    29  // NewPackageHeight is like NewPackage, but allows specifying the
    30  // package's height.
    31  func NewPackageHeight(path, name string, height int) *Package {
    32  	scope := NewScope(Universe, nopos, nopos, fmt.Sprintf("package %q", path))
    33  	return &Package{path: path, name: name, scope: scope, height: height}
    34  }
    35  
    36  // Path returns the package path.
    37  func (pkg *Package) Path() string { return pkg.path }
    38  
    39  // Name returns the package name.
    40  func (pkg *Package) Name() string { return pkg.name }
    41  
    42  // Height returns the package height.
    43  func (pkg *Package) Height() int { return pkg.height }
    44  
    45  // SetName sets the package name.
    46  func (pkg *Package) SetName(name string) { pkg.name = name }
    47  
    48  // Scope returns the (complete or incomplete) package scope
    49  // holding the objects declared at package level (TypeNames,
    50  // Consts, Vars, and Funcs).
    51  // For a nil pkg receiver, Scope returns the Universe scope.
    52  func (pkg *Package) Scope() *Scope {
    53  	if pkg != nil {
    54  		return pkg.scope
    55  	}
    56  	return Universe
    57  }
    58  
    59  // A package is complete if its scope contains (at least) all
    60  // exported objects; otherwise it is incomplete.
    61  func (pkg *Package) Complete() bool { return pkg.complete }
    62  
    63  // MarkComplete marks a package as complete.
    64  func (pkg *Package) MarkComplete() { pkg.complete = true }
    65  
    66  // Imports returns the list of packages directly imported by
    67  // pkg; the list is in source order.
    68  //
    69  // If pkg was loaded from export data, Imports includes packages that
    70  // provide package-level objects referenced by pkg. This may be more or
    71  // less than the set of packages directly imported by pkg's source code.
    72  func (pkg *Package) Imports() []*Package { return pkg.imports }
    73  
    74  // SetImports sets the list of explicitly imported packages to list.
    75  // It is the caller's responsibility to make sure list elements are unique.
    76  func (pkg *Package) SetImports(list []*Package) { pkg.imports = list }
    77  
    78  func (pkg *Package) String() string {
    79  	return fmt.Sprintf("package %s (%q)", pkg.name, pkg.path)
    80  }
    81  

View as plain text