Source file
src/go/ast/resolve.go
1
2
3
4
5
6
7 package ast
8
9 import (
10 "fmt"
11 "go/scanner"
12 "go/token"
13 "strconv"
14 )
15
16 type pkgBuilder struct {
17 fset *token.FileSet
18 errors scanner.ErrorList
19 }
20
21 func (p *pkgBuilder) error(pos token.Pos, msg string) {
22 p.errors.Add(p.fset.Position(pos), msg)
23 }
24
25 func (p *pkgBuilder) errorf(pos token.Pos, format string, args ...any) {
26 p.error(pos, fmt.Sprintf(format, args...))
27 }
28
29 func (p *pkgBuilder) declare(scope, altScope *Scope, obj *Object) {
30 alt := scope.Insert(obj)
31 if alt == nil && altScope != nil {
32
33 alt = altScope.Lookup(obj.Name)
34 }
35 if alt != nil {
36 prevDecl := ""
37 if pos := alt.Pos(); pos.IsValid() {
38 prevDecl = fmt.Sprintf("\n\tprevious declaration at %s", p.fset.Position(pos))
39 }
40 p.error(obj.Pos(), fmt.Sprintf("%s redeclared in this block%s", obj.Name, prevDecl))
41 }
42 }
43
44 func resolve(scope *Scope, ident *Ident) bool {
45 for ; scope != nil; scope = scope.Outer {
46 if obj := scope.Lookup(ident.Name); obj != nil {
47 ident.Obj = obj
48 return true
49 }
50 }
51 return false
52 }
53
54
55
56
57
58
59
60
61
62
63
64
65 type Importer func(imports map[string]*Object, path string) (pkg *Object, err error)
66
67
68
69
70
71
72
73
74
75
76
77 func NewPackage(fset *token.FileSet, files map[string]*File, importer Importer, universe *Scope) (*Package, error) {
78 var p pkgBuilder
79 p.fset = fset
80
81
82 pkgName := ""
83 pkgScope := NewScope(universe)
84 for _, file := range files {
85
86 switch name := file.Name.Name; {
87 case pkgName == "":
88 pkgName = name
89 case name != pkgName:
90 p.errorf(file.Package, "package %s; expected %s", name, pkgName)
91 continue
92 }
93
94
95 for _, obj := range file.Scope.Objects {
96 p.declare(pkgScope, nil, obj)
97 }
98 }
99
100
101 imports := make(map[string]*Object)
102
103
104 for _, file := range files {
105
106
107 if file.Name.Name != pkgName {
108 continue
109 }
110
111
112 importErrors := false
113 fileScope := NewScope(pkgScope)
114 for _, spec := range file.Imports {
115 if importer == nil {
116 importErrors = true
117 continue
118 }
119 path, _ := strconv.Unquote(spec.Path.Value)
120 pkg, err := importer(imports, path)
121 if err != nil {
122 p.errorf(spec.Path.Pos(), "could not import %s (%s)", path, err)
123 importErrors = true
124 continue
125 }
126
127
128
129
130
131 name := pkg.Name
132 if spec.Name != nil {
133 name = spec.Name.Name
134 }
135
136
137 if name == "." {
138
139 for _, obj := range pkg.Data.(*Scope).Objects {
140 p.declare(fileScope, pkgScope, obj)
141 }
142 } else if name != "_" {
143
144
145
146
147 obj := NewObj(Pkg, name)
148 obj.Decl = spec
149 obj.Data = pkg.Data
150 p.declare(fileScope, pkgScope, obj)
151 }
152 }
153
154
155 if importErrors {
156
157
158
159
160 pkgScope.Outer = nil
161 }
162 i := 0
163 for _, ident := range file.Unresolved {
164 if !resolve(fileScope, ident) {
165 p.errorf(ident.Pos(), "undeclared name: %s", ident.Name)
166 file.Unresolved[i] = ident
167 i++
168 }
169
170 }
171 file.Unresolved = file.Unresolved[0:i]
172 pkgScope.Outer = universe
173 }
174
175 p.errors.Sort()
176 return &Package{pkgName, pkgScope, imports, files}, p.errors.Err()
177 }
178
View as plain text