1 // Copyright 2011 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.
5 // This file implements NewPackage.
17 type pkgBuilder struct {
22 func (p *pkgBuilder) error(pos token.Pos, msg string) {
23 p.Error(p.fset.Position(pos), msg)
26 func (p *pkgBuilder) errorf(pos token.Pos, format string, args ...interface{}) {
27 p.error(pos, fmt.Sprintf(format, args...))
30 func (p *pkgBuilder) declare(scope, altScope *Scope, obj *Object) {
31 alt := scope.Insert(obj)
32 if alt == nil && altScope != nil {
33 // see if there is a conflicting declaration in altScope
34 alt = altScope.Lookup(obj.Name)
38 if pos := alt.Pos(); pos.IsValid() {
39 prevDecl = fmt.Sprintf("\n\tprevious declaration at %s", p.fset.Position(pos))
41 p.error(obj.Pos(), fmt.Sprintf("%s redeclared in this block%s", obj.Name, prevDecl))
45 func resolve(scope *Scope, ident *Ident) bool {
46 for ; scope != nil; scope = scope.Outer {
47 if obj := scope.Lookup(ident.Name); obj != nil {
55 // An Importer resolves import paths to package Objects.
56 // The imports map records the packages already imported,
57 // indexed by package id (canonical import path).
58 // An Importer must determine the canonical import path and
59 // check the map to see if it is already present in the imports map.
60 // If so, the Importer can return the map entry. Otherwise, the
61 // Importer should load the package data for the given path into
62 // a new *Object (pkg), record pkg in the imports map, and then
64 type Importer func(imports map[string]*Object, path string) (pkg *Object, err os.Error)
66 // NewPackage creates a new Package node from a set of File nodes. It resolves
67 // unresolved identifiers across files and updates each file's Unresolved list
68 // accordingly. If a non-nil importer and universe scope are provided, they are
69 // used to resolve identifiers not declared in any of the package files. Any
70 // remaining unresolved identifiers are reported as undeclared. If the files
71 // belong to different packages, one package name is selected and files with
72 // different package names are reported and then ignored.
73 // The result is a package node and a scanner.ErrorList if there were errors.
75 func NewPackage(fset *token.FileSet, files map[string]*File, importer Importer, universe *Scope) (*Package, os.Error) {
79 // complete package scope
81 pkgScope := NewScope(universe)
82 for _, file := range files {
83 // package names must match
84 switch name := file.Name.Name; {
88 p.errorf(file.Package, "package %s; expected %s", name, pkgName)
89 continue // ignore this file
92 // collect top-level file objects in package scope
93 for _, obj := range file.Scope.Objects {
94 p.declare(pkgScope, nil, obj)
98 // package global mapping of imported package ids to package objects
99 imports := make(map[string]*Object)
101 // complete file scopes with imports and resolve identifiers
102 for _, file := range files {
103 // ignore file if it belongs to a different package
104 // (error has already been reported)
105 if file.Name.Name != pkgName {
109 // build file scope by processing all imports
110 importErrors := false
111 fileScope := NewScope(pkgScope)
112 for _, spec := range file.Imports {
117 path, _ := strconv.Unquote(string(spec.Path.Value))
118 pkg, err := importer(imports, path)
120 p.errorf(spec.Path.Pos(), "could not import %s (%s)", path, err)
124 // TODO(gri) If a local package name != "." is provided,
125 // global identifier resolution could proceed even if the
126 // import failed. Consider adjusting the logic here a bit.
128 // local name overrides imported package name
130 if spec.Name != nil {
131 name = spec.Name.Name
134 // add import to file scope
136 // merge imported scope with file scope
137 for _, obj := range pkg.Data.(*Scope).Objects {
138 p.declare(fileScope, pkgScope, obj)
141 // declare imported package object in file scope
142 // (do not re-use pkg in the file scope but create
143 // a new object instead; the Decl field is different
144 // for different files)
145 obj := NewObj(Pkg, name)
148 p.declare(fileScope, pkgScope, obj)
152 // resolve identifiers
154 // don't use the universe scope without correct imports
155 // (objects in the universe may be shadowed by imports;
156 // with missing imports, identifiers might get resolved
157 // incorrectly to universe objects)
161 for _, ident := range file.Unresolved {
162 if !resolve(fileScope, ident) {
163 p.errorf(ident.Pos(), "undeclared name: %s", ident.Name)
164 file.Unresolved[i] = ident
169 file.Unresolved = file.Unresolved[0:i]
170 pkgScope.Outer = universe // reset universe scope
173 return &Package{pkgName, pkgScope, imports, files}, p.GetError(scanner.Sorted)