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 ( 8 "fmt" 9 "go/token" 10 ) 11 12 // A Package describes a Go package. 13 type Package struct { 14 path string 15 name string 16 scope *Scope 17 complete bool 18 imports []*Package 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 scope := NewScope(Universe, token.NoPos, token.NoPos, 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 // SetName sets the package name. 37 func (pkg *Package) SetName(name string) { pkg.name = name } 38 39 // Scope returns the (complete or incomplete) package scope 40 // holding the objects declared at package level (TypeNames, 41 // Consts, Vars, and Funcs). 42 // For a nil pkg receiver, Scope returns the Universe scope. 43 func (pkg *Package) Scope() *Scope { 44 if pkg != nil { 45 return pkg.scope 46 } 47 return Universe 48 } 49 50 // A package is complete if its scope contains (at least) all 51 // exported objects; otherwise it is incomplete. 52 func (pkg *Package) Complete() bool { return pkg.complete } 53 54 // MarkComplete marks a package as complete. 55 func (pkg *Package) MarkComplete() { pkg.complete = true } 56 57 // Imports returns the list of packages directly imported by 58 // pkg; the list is in source order. 59 // 60 // If pkg was loaded from export data, Imports includes packages that 61 // provide package-level objects referenced by pkg. This may be more or 62 // less than the set of packages directly imported by pkg's source code. 63 func (pkg *Package) Imports() []*Package { return pkg.imports } 64 65 // SetImports sets the list of explicitly imported packages to list. 66 // It is the caller's responsibility to make sure list elements are unique. 67 func (pkg *Package) SetImports(list []*Package) { pkg.imports = list } 68 69 func (pkg *Package) String() string { 70 return fmt.Sprintf("package %s (%q)", pkg.name, pkg.path) 71 } 72