OSDN Git Service

Update Go library to r60.
[pf3gnuchains/gcc-fork.git] / libgo / go / go / ast / resolve.go
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.
4
5 // This file implements NewPackage.
6
7 package ast
8
9 import (
10         "fmt"
11         "go/scanner"
12         "go/token"
13         "os"
14         "strconv"
15 )
16
17 type pkgBuilder struct {
18         scanner.ErrorVector
19         fset *token.FileSet
20 }
21
22 func (p *pkgBuilder) error(pos token.Pos, msg string) {
23         p.Error(p.fset.Position(pos), msg)
24 }
25
26 func (p *pkgBuilder) errorf(pos token.Pos, format string, args ...interface{}) {
27         p.error(pos, fmt.Sprintf(format, args...))
28 }
29
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)
35         }
36         if alt != nil {
37                 prevDecl := ""
38                 if pos := alt.Pos(); pos.IsValid() {
39                         prevDecl = fmt.Sprintf("\n\tprevious declaration at %s", p.fset.Position(pos))
40                 }
41                 p.error(obj.Pos(), fmt.Sprintf("%s redeclared in this block%s", obj.Name, prevDecl))
42         }
43 }
44
45 func resolve(scope *Scope, ident *Ident) bool {
46         for ; scope != nil; scope = scope.Outer {
47                 if obj := scope.Lookup(ident.Name); obj != nil {
48                         ident.Obj = obj
49                         return true
50                 }
51         }
52         return false
53 }
54
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
63 // return pkg.
64 type Importer func(imports map[string]*Object, path string) (pkg *Object, err os.Error)
65
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.
74 //
75 func NewPackage(fset *token.FileSet, files map[string]*File, importer Importer, universe *Scope) (*Package, os.Error) {
76         var p pkgBuilder
77         p.fset = fset
78
79         // complete package scope
80         pkgName := ""
81         pkgScope := NewScope(universe)
82         for _, file := range files {
83                 // package names must match
84                 switch name := file.Name.Name; {
85                 case pkgName == "":
86                         pkgName = name
87                 case name != pkgName:
88                         p.errorf(file.Package, "package %s; expected %s", name, pkgName)
89                         continue // ignore this file
90                 }
91
92                 // collect top-level file objects in package scope
93                 for _, obj := range file.Scope.Objects {
94                         p.declare(pkgScope, nil, obj)
95                 }
96         }
97
98         // package global mapping of imported package ids to package objects
99         imports := make(map[string]*Object)
100
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 {
106                         continue
107                 }
108
109                 // build file scope by processing all imports
110                 importErrors := false
111                 fileScope := NewScope(pkgScope)
112                 for _, spec := range file.Imports {
113                         if importer == nil {
114                                 importErrors = true
115                                 continue
116                         }
117                         path, _ := strconv.Unquote(string(spec.Path.Value))
118                         pkg, err := importer(imports, path)
119                         if err != nil {
120                                 p.errorf(spec.Path.Pos(), "could not import %s (%s)", path, err)
121                                 importErrors = true
122                                 continue
123                         }
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.
127
128                         // local name overrides imported package name
129                         name := pkg.Name
130                         if spec.Name != nil {
131                                 name = spec.Name.Name
132                         }
133
134                         // add import to file scope
135                         if name == "." {
136                                 // merge imported scope with file scope
137                                 for _, obj := range pkg.Data.(*Scope).Objects {
138                                         p.declare(fileScope, pkgScope, obj)
139                                 }
140                         } else {
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)
146                                 obj.Decl = spec
147                                 obj.Data = pkg.Data
148                                 p.declare(fileScope, pkgScope, obj)
149                         }
150                 }
151
152                 // resolve identifiers
153                 if importErrors {
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)
158                         pkgScope.Outer = nil
159                 }
160                 i := 0
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
165                                 i++
166                         }
167
168                 }
169                 file.Unresolved = file.Unresolved[0:i]
170                 pkgScope.Outer = universe // reset universe scope
171         }
172
173         return &Package{pkgName, pkgScope, imports, files}, p.GetError(scanner.Sorted)
174 }