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 type Importer func(imports map[string]*Object, path string) (pkg *Object, err error)
64
65
66
67
68
69
70
71
72
73
74 func NewPackage(fset *token.FileSet, files map[string]*File, importer Importer, universe *Scope) (*Package, error) {
75 var p pkgBuilder
76 p.fset = fset
77
78
79 pkgName := ""
80 pkgScope := NewScope(universe)
81 for _, file := range files {
82
83 switch name := file.Name.Name; {
84 case pkgName == "":
85 pkgName = name
86 case name != pkgName:
87 p.errorf(file.Package, "package %s; expected %s", name, pkgName)
88 continue
89 }
90
91
92 for _, obj := range file.Scope.Objects {
93 p.declare(pkgScope, nil, obj)
94 }
95 }
96
97
98 imports := make(map[string]*Object)
99
100
101 for _, file := range files {
102
103
104 if file.Name.Name != pkgName {
105 continue
106 }
107
108
109 importErrors := false
110 fileScope := NewScope(pkgScope)
111 for _, spec := range file.Imports {
112 if importer == nil {
113 importErrors = true
114 continue
115 }
116 path, _ := strconv.Unquote(spec.Path.Value)
117 pkg, err := importer(imports, path)
118 if err != nil {
119 p.errorf(spec.Path.Pos(), "could not import %s (%s)", path, err)
120 importErrors = true
121 continue
122 }
123
124
125
126
127
128 name := pkg.Name
129 if spec.Name != nil {
130 name = spec.Name.Name
131 }
132
133
134 if name == "." {
135
136 for _, obj := range pkg.Data.(*Scope).Objects {
137 p.declare(fileScope, pkgScope, obj)
138 }
139 } else if name != "_" {
140
141
142
143
144 obj := NewObj(Pkg, name)
145 obj.Decl = spec
146 obj.Data = pkg.Data
147 p.declare(fileScope, pkgScope, obj)
148 }
149 }
150
151
152 if importErrors {
153
154
155
156
157 pkgScope.Outer = nil
158 }
159 i := 0
160 for _, ident := range file.Unresolved {
161 if !resolve(fileScope, ident) {
162 p.errorf(ident.Pos(), "undeclared name: %s", ident.Name)
163 file.Unresolved[i] = ident
164 i++
165 }
166
167 }
168 file.Unresolved = file.Unresolved[0:i]
169 pkgScope.Outer = universe
170 }
171
172 p.errors.Sort()
173 return &Package{pkgName, pkgScope, imports, files}, p.errors.Err()
174 }
175
View as plain text