Source file
src/go/types/decl.go
1
2
3
4
5 package types
6
7 import (
8 "go/ast"
9 "go/constant"
10 "go/token"
11 )
12
13 func (check *Checker) reportAltDecl(obj Object) {
14 if pos := obj.Pos(); pos.IsValid() {
15
16
17
18 check.errorf(pos, "\tother declaration of %s", obj.Name())
19 }
20 }
21
22 func (check *Checker) declare(scope *Scope, id *ast.Ident, obj Object, pos token.Pos) {
23
24
25
26
27 if obj.Name() != "_" {
28 if alt := scope.Insert(obj); alt != nil {
29 check.errorf(obj.Pos(), "%s redeclared in this block", obj.Name())
30 check.reportAltDecl(alt)
31 return
32 }
33 obj.setScopePos(pos)
34 }
35 if id != nil {
36 check.recordDef(id, obj)
37 }
38 }
39
40
41
42 func (check *Checker) objDecl(obj Object, def *Named, path []*TypeName) {
43 if obj.Type() != nil {
44 return
45 }
46
47 if trace {
48 check.trace(obj.Pos(), "-- declaring %s", obj.Name())
49 check.indent++
50 defer func() {
51 check.indent--
52 check.trace(obj.Pos(), "=> %s", obj)
53 }()
54 }
55
56 d := check.objMap[obj]
57 if d == nil {
58 check.dump("%s: %s should have been declared", obj.Pos(), obj.Name())
59 unreachable()
60 }
61
62
63 defer func(ctxt context) {
64 check.context = ctxt
65 }(check.context)
66 check.context = context{
67 scope: d.file,
68 }
69
70
71
72
73
74
75 switch obj := obj.(type) {
76 case *Const:
77 check.decl = d
78 check.constDecl(obj, d.typ, d.init)
79 case *Var:
80 check.decl = d
81 check.varDecl(obj, d.lhs, d.typ, d.init)
82 case *TypeName:
83
84 check.typeDecl(obj, d.typ, def, path, d.alias)
85 case *Func:
86
87 check.funcDecl(obj, d)
88 default:
89 unreachable()
90 }
91 }
92
93 func (check *Checker) constDecl(obj *Const, typ, init ast.Expr) {
94 assert(obj.typ == nil)
95
96 if obj.visited {
97 obj.typ = Typ[Invalid]
98 return
99 }
100 obj.visited = true
101
102
103 assert(check.iota == nil)
104 check.iota = obj.val
105 defer func() { check.iota = nil }()
106
107
108 obj.val = constant.MakeUnknown()
109
110
111 if typ != nil {
112 t := check.typ(typ)
113 if !isConstType(t) {
114
115
116 if t.Underlying() != Typ[Invalid] {
117 check.errorf(typ.Pos(), "invalid constant type %s", t)
118 }
119 obj.typ = Typ[Invalid]
120 return
121 }
122 obj.typ = t
123 }
124
125
126 var x operand
127 if init != nil {
128 check.expr(&x, init)
129 }
130 check.initConst(obj, &x)
131 }
132
133 func (check *Checker) varDecl(obj *Var, lhs []*Var, typ, init ast.Expr) {
134 assert(obj.typ == nil)
135
136 if obj.visited {
137 obj.typ = Typ[Invalid]
138 return
139 }
140 obj.visited = true
141
142
143 assert(check.iota == nil)
144
145
146 if typ != nil {
147 obj.typ = check.typ(typ)
148
149
150
151
152
153
154
155
156 }
157
158
159 if init == nil {
160 if typ == nil {
161
162 obj.typ = Typ[Invalid]
163 }
164 return
165 }
166
167 if lhs == nil || len(lhs) == 1 {
168 assert(lhs == nil || lhs[0] == obj)
169 var x operand
170 check.expr(&x, init)
171 check.initVar(obj, &x, "variable declaration")
172 return
173 }
174
175 if debug {
176
177 found := false
178 for _, lhs := range lhs {
179 if obj == lhs {
180 found = true
181 break
182 }
183 }
184 if !found {
185 panic("inconsistent lhs")
186 }
187 }
188
189
190
191
192
193 if typ != nil {
194 for _, lhs := range lhs {
195 lhs.typ = obj.typ
196 }
197 }
198
199 check.initVars(lhs, []ast.Expr{init}, token.NoPos)
200 }
201
202
203
204
205 func underlying(typ Type) Type {
206 for {
207 n, _ := typ.(*Named)
208 if n == nil {
209 break
210 }
211 typ = n.underlying
212 }
213 return typ
214 }
215
216 func (n *Named) setUnderlying(typ Type) {
217 if n != nil {
218 n.underlying = typ
219 }
220 }
221
222 func (check *Checker) typeDecl(obj *TypeName, typ ast.Expr, def *Named, path []*TypeName, alias bool) {
223 assert(obj.typ == nil)
224
225
226 assert(check.iota == nil)
227
228 if alias {
229
230 obj.typ = Typ[Invalid]
231 obj.typ = check.typExpr(typ, nil, append(path, obj))
232
233 } else {
234
235 named := &Named{obj: obj}
236 def.setUnderlying(named)
237 obj.typ = named
238
239
240 check.typExpr(typ, named, append(path, obj))
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255 named.underlying = underlying(named.underlying)
256
257 }
258
259
260
261
262
263
264 check.addMethodDecls(obj)
265 }
266
267 func (check *Checker) addMethodDecls(obj *TypeName) {
268
269 methods := check.methods[obj.name]
270 if len(methods) == 0 {
271 return
272 }
273 delete(check.methods, obj.name)
274
275
276 var mset objset
277
278
279
280 base, _ := obj.typ.(*Named)
281 if base != nil {
282 if t, _ := base.underlying.(*Struct); t != nil {
283 for _, fld := range t.fields {
284 if fld.name != "_" {
285 assert(mset.insert(fld) == nil)
286 }
287 }
288 }
289
290
291
292
293 for _, m := range base.methods {
294 assert(m.name != "_")
295 assert(mset.insert(m) == nil)
296 }
297 }
298
299
300 for _, m := range methods {
301
302
303 if m.name != "_" {
304 if alt := mset.insert(m); alt != nil {
305 switch alt.(type) {
306 case *Var:
307 check.errorf(m.pos, "field and method with the same name %s", m.name)
308 case *Func:
309 check.errorf(m.pos, "method %s already declared for %s", m.name, obj)
310 default:
311 unreachable()
312 }
313 check.reportAltDecl(alt)
314 continue
315 }
316 }
317
318
319 check.objDecl(m, nil, nil)
320
321
322 if base != nil && m.name != "_" {
323 base.methods = append(base.methods, m)
324 }
325 }
326 }
327
328 func (check *Checker) funcDecl(obj *Func, decl *declInfo) {
329 assert(obj.typ == nil)
330
331
332 assert(check.iota == nil)
333
334 sig := new(Signature)
335 obj.typ = sig
336 fdecl := decl.fdecl
337 check.funcType(sig, fdecl.Recv, fdecl.Type)
338 if sig.recv == nil && obj.name == "init" && (sig.params.Len() > 0 || sig.results.Len() > 0) {
339 check.errorf(fdecl.Pos(), "func init must have no arguments and no return values")
340
341 }
342
343
344
345 if !check.conf.IgnoreFuncBodies && fdecl.Body != nil {
346 check.later(obj.name, decl, sig, fdecl.Body)
347 }
348 }
349
350 func (check *Checker) declStmt(decl ast.Decl) {
351 pkg := check.pkg
352
353 switch d := decl.(type) {
354 case *ast.BadDecl:
355
356
357 case *ast.GenDecl:
358 var last *ast.ValueSpec
359 for iota, spec := range d.Specs {
360 switch s := spec.(type) {
361 case *ast.ValueSpec:
362 switch d.Tok {
363 case token.CONST:
364
365 switch {
366 case s.Type != nil || len(s.Values) > 0:
367 last = s
368 case last == nil:
369 last = new(ast.ValueSpec)
370 }
371
372
373 lhs := make([]*Const, len(s.Names))
374 for i, name := range s.Names {
375 obj := NewConst(name.Pos(), pkg, name.Name, nil, constant.MakeInt64(int64(iota)))
376 lhs[i] = obj
377
378 var init ast.Expr
379 if i < len(last.Values) {
380 init = last.Values[i]
381 }
382
383 check.constDecl(obj, last.Type, init)
384 }
385
386 check.arityMatch(s, last)
387
388
389
390
391
392 scopePos := s.End()
393 for i, name := range s.Names {
394 check.declare(check.scope, name, lhs[i], scopePos)
395 }
396
397 case token.VAR:
398 lhs0 := make([]*Var, len(s.Names))
399 for i, name := range s.Names {
400 lhs0[i] = NewVar(name.Pos(), pkg, name.Name, nil)
401 }
402
403
404 for i, obj := range lhs0 {
405 var lhs []*Var
406 var init ast.Expr
407 switch len(s.Values) {
408 case len(s.Names):
409
410 init = s.Values[i]
411 case 1:
412
413 lhs = lhs0
414 init = s.Values[0]
415 default:
416 if i < len(s.Values) {
417 init = s.Values[i]
418 }
419 }
420 check.varDecl(obj, lhs, s.Type, init)
421 if len(s.Values) == 1 {
422
423
424
425
426
427 if debug {
428 for _, obj := range lhs0 {
429 assert(obj.typ != nil)
430 }
431 }
432 break
433 }
434 }
435
436 check.arityMatch(s, nil)
437
438
439
440 scopePos := s.End()
441 for i, name := range s.Names {
442
443 check.declare(check.scope, name, lhs0[i], scopePos)
444 }
445
446 default:
447 check.invalidAST(s.Pos(), "invalid token %s", d.Tok)
448 }
449
450 case *ast.TypeSpec:
451 obj := NewTypeName(s.Name.Pos(), pkg, s.Name.Name, nil)
452
453
454
455 scopePos := s.Name.Pos()
456 check.declare(check.scope, s.Name, obj, scopePos)
457 check.typeDecl(obj, s.Type, nil, nil, s.Assign.IsValid())
458
459 default:
460 check.invalidAST(s.Pos(), "const, type, or var declaration expected")
461 }
462 }
463
464 default:
465 check.invalidAST(d.Pos(), "unknown ast.Decl node %T", d)
466 }
467 }
468
View as plain text