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 5package types 6 7import ( 8 "fmt" 9 "go/token" 10) 11 12// A Package describes a Go package. 13type 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} 21 22// NewPackage returns a new Package for the given package path and name. 23// The package is not complete and contains no explicit imports. 24func NewPackage(path, name string) *Package { 25 scope := NewScope(Universe, token.NoPos, token.NoPos, fmt.Sprintf("package %q", path)) 26 return &Package{path: path, name: name, scope: scope} 27} 28 29// Path returns the package path. 30func (pkg *Package) Path() string { return pkg.path } 31 32// Name returns the package name. 33func (pkg *Package) Name() string { return pkg.name } 34 35// SetName sets the package name. 36func (pkg *Package) SetName(name string) { pkg.name = name } 37 38// Scope returns the (complete or incomplete) package scope 39// holding the objects declared at package level (TypeNames, 40// Consts, Vars, and Funcs). 41func (pkg *Package) Scope() *Scope { return pkg.scope } 42 43// A package is complete if its scope contains (at least) all 44// exported objects; otherwise it is incomplete. 45func (pkg *Package) Complete() bool { return pkg.complete } 46 47// MarkComplete marks a package as complete. 48func (pkg *Package) MarkComplete() { pkg.complete = true } 49 50// Imports returns the list of packages directly imported by 51// pkg; the list is in source order. 52// 53// If pkg was loaded from export data, Imports includes packages that 54// provide package-level objects referenced by pkg. This may be more or 55// less than the set of packages directly imported by pkg's source code. 56func (pkg *Package) Imports() []*Package { return pkg.imports } 57 58// SetImports sets the list of explicitly imported packages to list. 59// It is the caller's responsibility to make sure list elements are unique. 60func (pkg *Package) SetImports(list []*Package) { pkg.imports = list } 61 62func (pkg *Package) String() string { 63 return fmt.Sprintf("package %s (%q)", pkg.name, pkg.path) 64} 65