libgo: update to Go1.10rc1
[official-gcc.git] / libgo / go / go / types / decl.go
blob9b250b30e74c96a47936313868a967a6259dbe34
1 // Copyright 2014 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.
5 package types
7 import (
8 "go/ast"
9 "go/constant"
10 "go/token"
13 func (check *Checker) reportAltDecl(obj Object) {
14 if pos := obj.Pos(); pos.IsValid() {
15 // We use "other" rather than "previous" here because
16 // the first declaration seen may not be textually
17 // earlier in the source.
18 check.errorf(pos, "\tother declaration of %s", obj.Name()) // secondary error, \t indented
22 func (check *Checker) declare(scope *Scope, id *ast.Ident, obj Object, pos token.Pos) {
23 // spec: "The blank identifier, represented by the underscore
24 // character _, may be used in a declaration like any other
25 // identifier but the declaration does not introduce a new
26 // binding."
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
33 obj.setScopePos(pos)
35 if id != nil {
36 check.recordDef(id, obj)
40 // objDecl type-checks the declaration of obj in its respective (file) context.
41 // See check.typ for the details on def and path.
42 func (check *Checker) objDecl(obj Object, def *Named, path []*TypeName) {
43 if obj.Type() != nil {
44 return // already checked - nothing to do
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 }()
56 d := check.objMap[obj]
57 if d == nil {
58 check.dump("%s: %s should have been declared", obj.Pos(), obj.Name())
59 unreachable()
62 // save/restore current context and setup object context
63 defer func(ctxt context) {
64 check.context = ctxt
65 }(check.context)
66 check.context = context{
67 scope: d.file,
70 // Const and var declarations must not have initialization
71 // cycles. We track them by remembering the current declaration
72 // in check.decl. Initialization expressions depending on other
73 // consts, vars, or functions, add dependencies to the current
74 // check.decl.
75 switch obj := obj.(type) {
76 case *Const:
77 check.decl = d // new package-level const decl
78 check.constDecl(obj, d.typ, d.init)
79 case *Var:
80 check.decl = d // new package-level var decl
81 check.varDecl(obj, d.lhs, d.typ, d.init)
82 case *TypeName:
83 // invalid recursive types are detected via path
84 check.typeDecl(obj, d.typ, def, path, d.alias)
85 case *Func:
86 // functions may be recursive - no need to track dependencies
87 check.funcDecl(obj, d)
88 default:
89 unreachable()
93 func (check *Checker) constDecl(obj *Const, typ, init ast.Expr) {
94 assert(obj.typ == nil)
96 if obj.visited {
97 obj.typ = Typ[Invalid]
98 return
100 obj.visited = true
102 // use the correct value of iota
103 assert(check.iota == nil)
104 check.iota = obj.val
105 defer func() { check.iota = nil }()
107 // provide valid constant value under all circumstances
108 obj.val = constant.MakeUnknown()
110 // determine type, if any
111 if typ != nil {
112 t := check.typ(typ)
113 if !isConstType(t) {
114 // don't report an error if the type is an invalid C (defined) type
115 // (issue #22090)
116 if t.Underlying() != Typ[Invalid] {
117 check.errorf(typ.Pos(), "invalid constant type %s", t)
119 obj.typ = Typ[Invalid]
120 return
122 obj.typ = t
125 // check initialization
126 var x operand
127 if init != nil {
128 check.expr(&x, init)
130 check.initConst(obj, &x)
133 func (check *Checker) varDecl(obj *Var, lhs []*Var, typ, init ast.Expr) {
134 assert(obj.typ == nil)
136 if obj.visited {
137 obj.typ = Typ[Invalid]
138 return
140 obj.visited = true
142 // var declarations cannot use iota
143 assert(check.iota == nil)
145 // determine type, if any
146 if typ != nil {
147 obj.typ = check.typ(typ)
148 // We cannot spread the type to all lhs variables if there
149 // are more than one since that would mark them as checked
150 // (see Checker.objDecl) and the assignment of init exprs,
151 // if any, would not be checked.
153 // TODO(gri) If we have no init expr, we should distribute
154 // a given type otherwise we need to re-evalate the type
155 // expr for each lhs variable, leading to duplicate work.
158 // check initialization
159 if init == nil {
160 if typ == nil {
161 // error reported before by arityMatch
162 obj.typ = Typ[Invalid]
164 return
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
175 if debug {
176 // obj must be one of lhs
177 found := false
178 for _, lhs := range lhs {
179 if obj == lhs {
180 found = true
181 break
184 if !found {
185 panic("inconsistent lhs")
189 // We have multiple variables on the lhs and one init expr.
190 // Make sure all variables have been given the same type if
191 // one was specified, otherwise they assume the type of the
192 // init expression values (was issue #15755).
193 if typ != nil {
194 for _, lhs := range lhs {
195 lhs.typ = obj.typ
199 check.initVars(lhs, []ast.Expr{init}, token.NoPos)
202 // underlying returns the underlying type of typ; possibly by following
203 // forward chains of named types. Such chains only exist while named types
204 // are incomplete.
205 func underlying(typ Type) Type {
206 for {
207 n, _ := typ.(*Named)
208 if n == nil {
209 break
211 typ = n.underlying
213 return typ
216 func (n *Named) setUnderlying(typ Type) {
217 if n != nil {
218 n.underlying = typ
222 func (check *Checker) typeDecl(obj *TypeName, typ ast.Expr, def *Named, path []*TypeName, alias bool) {
223 assert(obj.typ == nil)
225 // type declarations cannot use iota
226 assert(check.iota == nil)
228 if alias {
230 obj.typ = Typ[Invalid]
231 obj.typ = check.typExpr(typ, nil, append(path, obj))
233 } else {
235 named := &Named{obj: obj}
236 def.setUnderlying(named)
237 obj.typ = named // make sure recursive type declarations terminate
239 // determine underlying type of named
240 check.typExpr(typ, named, append(path, obj))
242 // The underlying type of named may be itself a named type that is
243 // incomplete:
245 // type (
246 // A B
247 // B *C
248 // C A
249 // )
251 // The type of C is the (named) type of A which is incomplete,
252 // and which has as its underlying type the named type B.
253 // Determine the (final, unnamed) underlying type by resolving
254 // any forward chain (they always end in an unnamed type).
255 named.underlying = underlying(named.underlying)
259 // check and add associated methods
260 // TODO(gri) It's easy to create pathological cases where the
261 // current approach is incorrect: In general we need to know
262 // and add all methods _before_ type-checking the type.
263 // See https://play.golang.org/p/WMpE0q2wK8
264 check.addMethodDecls(obj)
267 func (check *Checker) addMethodDecls(obj *TypeName) {
268 // get associated methods
269 methods := check.methods[obj.name]
270 if len(methods) == 0 {
271 return // no methods
273 delete(check.methods, obj.name)
275 // use an objset to check for name conflicts
276 var mset objset
278 // spec: "If the base type is a struct type, the non-blank method
279 // and field names must be distinct."
280 base, _ := obj.typ.(*Named) // nil if receiver base type is type alias
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)
290 // Checker.Files may be called multiple times; additional package files
291 // may add methods to already type-checked types. Add pre-existing methods
292 // so that we can detect redeclarations.
293 for _, m := range base.methods {
294 assert(m.name != "_")
295 assert(mset.insert(m) == nil)
299 // type-check methods
300 for _, m := range methods {
301 // spec: "For a base type, the non-blank names of methods bound
302 // to it must be unique."
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()
313 check.reportAltDecl(alt)
314 continue
318 // type-check
319 check.objDecl(m, nil, nil)
321 // methods with blank _ names cannot be found - don't keep them
322 if base != nil && m.name != "_" {
323 base.methods = append(base.methods, m)
328 func (check *Checker) funcDecl(obj *Func, decl *declInfo) {
329 assert(obj.typ == nil)
331 // func declarations cannot use iota
332 assert(check.iota == nil)
334 sig := new(Signature)
335 obj.typ = sig // guard against cycles
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 // ok to continue
343 // function body must be type-checked after global declarations
344 // (functions implemented elsewhere have no body)
345 if !check.conf.IgnoreFuncBodies && fdecl.Body != nil {
346 check.later(obj.name, decl, sig, fdecl.Body)
350 func (check *Checker) declStmt(decl ast.Decl) {
351 pkg := check.pkg
353 switch d := decl.(type) {
354 case *ast.BadDecl:
355 // ignore
357 case *ast.GenDecl:
358 var last *ast.ValueSpec // last ValueSpec with type or init exprs seen
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 // determine which init exprs to use
365 switch {
366 case s.Type != nil || len(s.Values) > 0:
367 last = s
368 case last == nil:
369 last = new(ast.ValueSpec) // make sure last exists
372 // declare all constants
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
378 var init ast.Expr
379 if i < len(last.Values) {
380 init = last.Values[i]
383 check.constDecl(obj, last.Type, init)
386 check.arityMatch(s, last)
388 // spec: "The scope of a constant or variable identifier declared
389 // inside a function begins at the end of the ConstSpec or VarSpec
390 // (ShortVarDecl for short variable declarations) and ends at the
391 // end of the innermost containing block."
392 scopePos := s.End()
393 for i, name := range s.Names {
394 check.declare(check.scope, name, lhs[i], scopePos)
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)
403 // initialize all variables
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 // lhs and rhs match
410 init = s.Values[i]
411 case 1:
412 // rhs is expected to be a multi-valued expression
413 lhs = lhs0
414 init = s.Values[0]
415 default:
416 if i < len(s.Values) {
417 init = s.Values[i]
420 check.varDecl(obj, lhs, s.Type, init)
421 if len(s.Values) == 1 {
422 // If we have a single lhs variable we are done either way.
423 // If we have a single rhs expression, it must be a multi-
424 // valued expression, in which case handling the first lhs
425 // variable will cause all lhs variables to have a type
426 // assigned, and we are done as well.
427 if debug {
428 for _, obj := range lhs0 {
429 assert(obj.typ != nil)
432 break
436 check.arityMatch(s, nil)
438 // declare all variables
439 // (only at this point are the variable scopes (parents) set)
440 scopePos := s.End() // see constant declarations
441 for i, name := range s.Names {
442 // see constant declarations
443 check.declare(check.scope, name, lhs0[i], scopePos)
446 default:
447 check.invalidAST(s.Pos(), "invalid token %s", d.Tok)
450 case *ast.TypeSpec:
451 obj := NewTypeName(s.Name.Pos(), pkg, s.Name.Name, nil)
452 // spec: "The scope of a type identifier declared inside a function
453 // begins at the identifier in the TypeSpec and ends at the end of
454 // the innermost containing block."
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())
459 default:
460 check.invalidAST(s.Pos(), "const, type, or var declaration expected")
464 default:
465 check.invalidAST(d.Pos(), "unknown ast.Decl node %T", d)