@@ -34,9 +34,15 @@ type Object interface {
34
34
// 0 for all other objects (including objects in file scopes).
35
35
order () uint32
36
36
37
+ // color returns the object's color.
38
+ color () color
39
+
37
40
// setOrder sets the order number of the object. It must be > 0.
38
41
setOrder (uint32 )
39
42
43
+ // setColor sets the object's color. It must not be white.
44
+ setColor (color color )
45
+
40
46
// setParent sets the parent scope of the object.
41
47
setParent (* Scope )
42
48
@@ -78,9 +84,41 @@ type object struct {
78
84
name string
79
85
typ Type
80
86
order_ uint32
87
+ color_ color
81
88
scopePos_ token.Pos
82
89
}
83
90
91
+ // color encodes the color of an object (see Checker.objDecl for details).
92
+ type color uint32
93
+
94
+ // An object may be painted in one of three colors.
95
+ // Color values other than white or black are considered grey.
96
+ const (
97
+ white color = iota
98
+ black
99
+ grey // must be > white and black
100
+ )
101
+
102
+ func (c color ) String () string {
103
+ switch c {
104
+ case white :
105
+ return "white"
106
+ case black :
107
+ return "black"
108
+ default :
109
+ return "grey"
110
+ }
111
+ }
112
+
113
+ // colorFor returns the (initial) color for an object depending on
114
+ // whether its type t is known or not.
115
+ func colorFor (t Type ) color {
116
+ if t != nil {
117
+ return black
118
+ }
119
+ return white
120
+ }
121
+
84
122
// Parent returns the scope in which the object is declared.
85
123
// The result is nil for methods and struct fields.
86
124
func (obj * object ) Parent () * Scope { return obj .parent }
@@ -108,10 +146,12 @@ func (obj *object) Id() string { return Id(obj.pkg, obj.name) }
108
146
109
147
func (obj * object ) String () string { panic ("abstract" ) }
110
148
func (obj * object ) order () uint32 { return obj .order_ }
149
+ func (obj * object ) color () color { return obj .color_ }
111
150
func (obj * object ) scopePos () token.Pos { return obj .scopePos_ }
112
151
113
152
func (obj * object ) setParent (parent * Scope ) { obj .parent = parent }
114
153
func (obj * object ) setOrder (order uint32 ) { assert (order > 0 ); obj .order_ = order }
154
+ func (obj * object ) setColor (color color ) { assert (color != white ); obj .color_ = color }
115
155
func (obj * object ) setScopePos (pos token.Pos ) { obj .scopePos_ = pos }
116
156
117
157
func (obj * object ) sameId (pkg * Package , name string ) bool {
@@ -147,7 +187,7 @@ type PkgName struct {
147
187
// NewPkgName returns a new PkgName object representing an imported package.
148
188
// The remaining arguments set the attributes found with all Objects.
149
189
func NewPkgName (pos token.Pos , pkg * Package , name string , imported * Package ) * PkgName {
150
- return & PkgName {object {nil , pos , pkg , name , Typ [Invalid ], 0 , token .NoPos }, imported , false }
190
+ return & PkgName {object {nil , pos , pkg , name , Typ [Invalid ], 0 , black , token .NoPos }, imported , false }
151
191
}
152
192
153
193
// Imported returns the package that was imported.
@@ -164,7 +204,7 @@ type Const struct {
164
204
// NewConst returns a new constant with value val.
165
205
// The remaining arguments set the attributes found with all Objects.
166
206
func NewConst (pos token.Pos , pkg * Package , name string , typ Type , val constant.Value ) * Const {
167
- return & Const {object {nil , pos , pkg , name , typ , 0 , token .NoPos }, val , false }
207
+ return & Const {object {nil , pos , pkg , name , typ , 0 , colorFor ( typ ), token .NoPos }, val , false }
168
208
}
169
209
170
210
// Val returns the constant's value.
@@ -185,7 +225,7 @@ type TypeName struct {
185
225
// argument for NewNamed, which will set the TypeName's type as a side-
186
226
// effect.
187
227
func NewTypeName (pos token.Pos , pkg * Package , name string , typ Type ) * TypeName {
188
- return & TypeName {object {nil , pos , pkg , name , typ , 0 , token .NoPos }}
228
+ return & TypeName {object {nil , pos , pkg , name , typ , 0 , colorFor ( typ ), token .NoPos }}
189
229
}
190
230
191
231
// IsAlias reports whether obj is an alias name for a type.
@@ -224,19 +264,19 @@ type Var struct {
224
264
// NewVar returns a new variable.
225
265
// The arguments set the attributes found with all Objects.
226
266
func NewVar (pos token.Pos , pkg * Package , name string , typ Type ) * Var {
227
- return & Var {object : object {nil , pos , pkg , name , typ , 0 , token .NoPos }}
267
+ return & Var {object : object {nil , pos , pkg , name , typ , 0 , colorFor ( typ ), token .NoPos }}
228
268
}
229
269
230
270
// NewParam returns a new variable representing a function parameter.
231
271
func NewParam (pos token.Pos , pkg * Package , name string , typ Type ) * Var {
232
- return & Var {object : object {nil , pos , pkg , name , typ , 0 , token .NoPos }, used : true } // parameters are always 'used'
272
+ return & Var {object : object {nil , pos , pkg , name , typ , 0 , colorFor ( typ ), token .NoPos }, used : true } // parameters are always 'used'
233
273
}
234
274
235
275
// NewField returns a new variable representing a struct field.
236
276
// For embedded fields, the name is the unqualified type name
237
277
/// under which the field is accessible.
238
278
func NewField (pos token.Pos , pkg * Package , name string , typ Type , embedded bool ) * Var {
239
- return & Var {object : object {nil , pos , pkg , name , typ , 0 , token .NoPos }, embedded : embedded , isField : true }
279
+ return & Var {object : object {nil , pos , pkg , name , typ , 0 , colorFor ( typ ), token .NoPos }, embedded : embedded , isField : true }
240
280
}
241
281
242
282
// Anonymous reports whether the variable is an embedded field.
@@ -266,7 +306,7 @@ func NewFunc(pos token.Pos, pkg *Package, name string, sig *Signature) *Func {
266
306
if sig != nil {
267
307
typ = sig
268
308
}
269
- return & Func {object {nil , pos , pkg , name , typ , 0 , token .NoPos }}
309
+ return & Func {object {nil , pos , pkg , name , typ , 0 , colorFor ( typ ), token .NoPos }}
270
310
}
271
311
272
312
// FullName returns the package- or receiver-type-qualified name of
@@ -291,7 +331,7 @@ type Label struct {
291
331
292
332
// NewLabel returns a new label.
293
333
func NewLabel (pos token.Pos , pkg * Package , name string ) * Label {
294
- return & Label {object {pos : pos , pkg : pkg , name : name , typ : Typ [Invalid ]}, false }
334
+ return & Label {object {pos : pos , pkg : pkg , name : name , typ : Typ [Invalid ], color_ : black }, false }
295
335
}
296
336
297
337
// A Builtin represents a built-in function.
@@ -302,7 +342,7 @@ type Builtin struct {
302
342
}
303
343
304
344
func newBuiltin (id builtinId ) * Builtin {
305
- return & Builtin {object {name : predeclaredFuncs [id ].name , typ : Typ [Invalid ]}, id }
345
+ return & Builtin {object {name : predeclaredFuncs [id ].name , typ : Typ [Invalid ], color_ : black }, id }
306
346
}
307
347
308
348
// Nil represents the predeclared value nil.
0 commit comments