]> Cypherpunks.ru repositories - gostls13.git/blob - src/cmd/compile/internal/types2/resolver.go
[dev.typeparams] merge: merge branch 'dev.regabi' into 'dev.typeparams'
[gostls13.git] / src / cmd / compile / internal / types2 / resolver.go
1 // UNREVIEWED
2 // Copyright 2013 The Go Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file.
5
6 package types2
7
8 import (
9         "cmd/compile/internal/syntax"
10         "fmt"
11         "go/constant"
12         "sort"
13         "strconv"
14         "strings"
15         "unicode"
16 )
17
18 // A declInfo describes a package-level const, type, var, or func declaration.
19 type declInfo struct {
20         file      *Scope           // scope of file containing this declaration
21         lhs       []*Var           // lhs of n:1 variable declarations, or nil
22         vtyp      syntax.Expr      // type, or nil (for const and var declarations only)
23         init      syntax.Expr      // init/orig expression, or nil (for const and var declarations only)
24         inherited bool             // if set, the init expression is inherited from a previous constant declaration
25         tdecl     *syntax.TypeDecl // type declaration, or nil
26         fdecl     *syntax.FuncDecl // func declaration, or nil
27
28         // The deps field tracks initialization expression dependencies.
29         deps map[Object]bool // lazily initialized
30 }
31
32 // hasInitializer reports whether the declared object has an initialization
33 // expression or function body.
34 func (d *declInfo) hasInitializer() bool {
35         return d.init != nil || d.fdecl != nil && d.fdecl.Body != nil
36 }
37
38 // addDep adds obj to the set of objects d's init expression depends on.
39 func (d *declInfo) addDep(obj Object) {
40         m := d.deps
41         if m == nil {
42                 m = make(map[Object]bool)
43                 d.deps = m
44         }
45         m[obj] = true
46 }
47
48 // arity checks that the lhs and rhs of a const or var decl
49 // have a matching number of names and initialization values.
50 // If inherited is set, the initialization values are from
51 // another (constant) declaration.
52 func (check *Checker) arity(pos syntax.Pos, names []*syntax.Name, inits []syntax.Expr, constDecl, inherited bool) {
53         l := len(names)
54         r := len(inits)
55
56         switch {
57         case l < r:
58                 n := inits[l]
59                 if inherited {
60                         check.errorf(pos, "extra init expr at %s", n.Pos())
61                 } else {
62                         check.errorf(n, "extra init expr %s", n)
63                 }
64         case l > r && (constDecl || r != 1): // if r == 1 it may be a multi-valued function and we can't say anything yet
65                 n := names[r]
66                 check.errorf(n, "missing init expr for %s", n.Value)
67         }
68 }
69
70 func validatedImportPath(path string) (string, error) {
71         s, err := strconv.Unquote(path)
72         if err != nil {
73                 return "", err
74         }
75         if s == "" {
76                 return "", fmt.Errorf("empty string")
77         }
78         const illegalChars = `!"#$%&'()*,:;<=>?[\]^{|}` + "`\uFFFD"
79         for _, r := range s {
80                 if !unicode.IsGraphic(r) || unicode.IsSpace(r) || strings.ContainsRune(illegalChars, r) {
81                         return s, fmt.Errorf("invalid character %#U", r)
82                 }
83         }
84         return s, nil
85 }
86
87 // declarePkgObj declares obj in the package scope, records its ident -> obj mapping,
88 // and updates check.objMap. The object must not be a function or method.
89 func (check *Checker) declarePkgObj(ident *syntax.Name, obj Object, d *declInfo) {
90         assert(ident.Value == obj.Name())
91
92         // spec: "A package-scope or file-scope identifier with name init
93         // may only be declared to be a function with this (func()) signature."
94         if ident.Value == "init" {
95                 check.errorf(ident, "cannot declare init - must be func")
96                 return
97         }
98
99         // spec: "The main package must have package name main and declare
100         // a function main that takes no arguments and returns no value."
101         if ident.Value == "main" && check.pkg.name == "main" {
102                 check.errorf(ident, "cannot declare main - must be func")
103                 return
104         }
105
106         check.declare(check.pkg.scope, ident, obj, nopos)
107         check.objMap[obj] = d
108         obj.setOrder(uint32(len(check.objMap)))
109 }
110
111 // filename returns a filename suitable for debugging output.
112 func (check *Checker) filename(fileNo int) string {
113         file := check.files[fileNo]
114         if pos := file.Pos(); pos.IsKnown() {
115                 // return check.fset.File(pos).Name()
116                 // TODO(gri) do we need the actual file name here?
117                 return pos.RelFilename()
118         }
119         return fmt.Sprintf("file[%d]", fileNo)
120 }
121
122 func (check *Checker) importPackage(pos syntax.Pos, path, dir string) *Package {
123         // If we already have a package for the given (path, dir)
124         // pair, use it instead of doing a full import.
125         // Checker.impMap only caches packages that are marked Complete
126         // or fake (dummy packages for failed imports). Incomplete but
127         // non-fake packages do require an import to complete them.
128         key := importKey{path, dir}
129         imp := check.impMap[key]
130         if imp != nil {
131                 return imp
132         }
133
134         // no package yet => import it
135         if path == "C" && (check.conf.FakeImportC || check.conf.go115UsesCgo) {
136                 imp = NewPackage("C", "C")
137                 imp.fake = true // package scope is not populated
138                 imp.cgo = check.conf.go115UsesCgo
139         } else {
140                 // ordinary import
141                 var err error
142                 if importer := check.conf.Importer; importer == nil {
143                         err = fmt.Errorf("Config.Importer not installed")
144                 } else if importerFrom, ok := importer.(ImporterFrom); ok {
145                         imp, err = importerFrom.ImportFrom(path, dir, 0)
146                         if imp == nil && err == nil {
147                                 err = fmt.Errorf("Config.Importer.ImportFrom(%s, %s, 0) returned nil but no error", path, dir)
148                         }
149                 } else {
150                         imp, err = importer.Import(path)
151                         if imp == nil && err == nil {
152                                 err = fmt.Errorf("Config.Importer.Import(%s) returned nil but no error", path)
153                         }
154                 }
155                 // make sure we have a valid package name
156                 // (errors here can only happen through manipulation of packages after creation)
157                 if err == nil && imp != nil && (imp.name == "_" || imp.name == "") {
158                         err = fmt.Errorf("invalid package name: %q", imp.name)
159                         imp = nil // create fake package below
160                 }
161                 if err != nil {
162                         check.errorf(pos, "could not import %s (%s)", path, err)
163                         if imp == nil {
164                                 // create a new fake package
165                                 // come up with a sensible package name (heuristic)
166                                 name := path
167                                 if i := len(name); i > 0 && name[i-1] == '/' {
168                                         name = name[:i-1]
169                                 }
170                                 if i := strings.LastIndex(name, "/"); i >= 0 {
171                                         name = name[i+1:]
172                                 }
173                                 imp = NewPackage(path, name)
174                         }
175                         // continue to use the package as best as we can
176                         imp.fake = true // avoid follow-up lookup failures
177                 }
178         }
179
180         // package should be complete or marked fake, but be cautious
181         if imp.complete || imp.fake {
182                 check.impMap[key] = imp
183                 check.pkgCnt[imp.name]++
184                 return imp
185         }
186
187         // something went wrong (importer may have returned incomplete package without error)
188         return nil
189 }
190
191 // collectObjects collects all file and package objects and inserts them
192 // into their respective scopes. It also performs imports and associates
193 // methods with receiver base type names.
194 func (check *Checker) collectObjects() {
195         pkg := check.pkg
196
197         // pkgImports is the set of packages already imported by any package file seen
198         // so far. Used to avoid duplicate entries in pkg.imports. Allocate and populate
199         // it (pkg.imports may not be empty if we are checking test files incrementally).
200         // Note that pkgImports is keyed by package (and thus package path), not by an
201         // importKey value. Two different importKey values may map to the same package
202         // which is why we cannot use the check.impMap here.
203         var pkgImports = make(map[*Package]bool)
204         for _, imp := range pkg.imports {
205                 pkgImports[imp] = true
206         }
207
208         type methodInfo struct {
209                 obj  *Func        // method
210                 ptr  bool         // true if pointer receiver
211                 recv *syntax.Name // receiver type name
212         }
213         var methods []methodInfo // collected methods with valid receivers and non-blank _ names
214         var fileScopes []*Scope
215         for fileNo, file := range check.files {
216                 // The package identifier denotes the current package,
217                 // but there is no corresponding package object.
218                 check.recordDef(file.PkgName, nil)
219
220                 fileScope := NewScope(check.pkg.scope, startPos(file), endPos(file), check.filename(fileNo))
221                 fileScopes = append(fileScopes, fileScope)
222                 check.recordScope(file, fileScope)
223
224                 // determine file directory, necessary to resolve imports
225                 // FileName may be "" (typically for tests) in which case
226                 // we get "." as the directory which is what we would want.
227                 fileDir := dir(file.PkgName.Pos().RelFilename()) // TODO(gri) should this be filename?
228
229                 first := -1                // index of first ConstDecl in the current group, or -1
230                 var last *syntax.ConstDecl // last ConstDecl with init expressions, or nil
231                 for index, decl := range file.DeclList {
232                         if _, ok := decl.(*syntax.ConstDecl); !ok {
233                                 first = -1 // we're not in a constant declaration
234                         }
235
236                         switch s := decl.(type) {
237                         case *syntax.ImportDecl:
238                                 // import package
239                                 path, err := validatedImportPath(s.Path.Value)
240                                 if err != nil {
241                                         check.errorf(s.Path, "invalid import path (%s)", err)
242                                         continue
243                                 }
244
245                                 imp := check.importPackage(s.Path.Pos(), path, fileDir)
246                                 if imp == nil {
247                                         continue
248                                 }
249
250                                 // add package to list of explicit imports
251                                 // (this functionality is provided as a convenience
252                                 // for clients; it is not needed for type-checking)
253                                 if !pkgImports[imp] {
254                                         pkgImports[imp] = true
255                                         pkg.imports = append(pkg.imports, imp)
256                                 }
257
258                                 // local name overrides imported package name
259                                 name := imp.name
260                                 if s.LocalPkgName != nil {
261                                         name = s.LocalPkgName.Value
262                                         if path == "C" {
263                                                 // match cmd/compile (not prescribed by spec)
264                                                 check.errorf(s.LocalPkgName, `cannot rename import "C"`)
265                                                 continue
266                                         }
267                                         if name == "init" {
268                                                 check.errorf(s.LocalPkgName, "cannot declare init - must be func")
269                                                 continue
270                                         }
271                                 }
272
273                                 obj := NewPkgName(s.Pos(), pkg, name, imp)
274                                 if s.LocalPkgName != nil {
275                                         // in a dot-import, the dot represents the package
276                                         check.recordDef(s.LocalPkgName, obj)
277                                 } else {
278                                         check.recordImplicit(s, obj)
279                                 }
280
281                                 if path == "C" {
282                                         // match cmd/compile (not prescribed by spec)
283                                         obj.used = true
284                                 }
285
286                                 // add import to file scope
287                                 if name == "." {
288                                         // merge imported scope with file scope
289                                         for _, obj := range imp.scope.elems {
290                                                 // A package scope may contain non-exported objects,
291                                                 // do not import them!
292                                                 if obj.Exported() {
293                                                         // declare dot-imported object
294                                                         // (Do not use check.declare because it modifies the object
295                                                         // via Object.setScopePos, which leads to a race condition;
296                                                         // the object may be imported into more than one file scope
297                                                         // concurrently. See issue #32154.)
298                                                         if alt := fileScope.Insert(obj); alt != nil {
299                                                                 check.errorf(s.LocalPkgName, "%s redeclared in this block", obj.Name())
300                                                                 check.reportAltDecl(alt)
301                                                         }
302                                                 }
303                                         }
304                                         // add position to set of dot-import positions for this file
305                                         // (this is only needed for "imported but not used" errors)
306                                         check.addUnusedDotImport(fileScope, imp, s.Pos())
307                                 } else {
308                                         // declare imported package object in file scope
309                                         // (no need to provide s.LocalPkgName since we called check.recordDef earlier)
310                                         check.declare(fileScope, nil, obj, nopos)
311                                 }
312
313                         case *syntax.ConstDecl:
314                                 // iota is the index of the current constDecl within the group
315                                 if first < 0 || file.DeclList[index-1].(*syntax.ConstDecl).Group != s.Group {
316                                         first = index
317                                         last = nil
318                                 }
319                                 iota := constant.MakeInt64(int64(index - first))
320
321                                 // determine which initialization expressions to use
322                                 inherited := true
323                                 switch {
324                                 case s.Type != nil || s.Values != nil:
325                                         last = s
326                                         inherited = false
327                                 case last == nil:
328                                         last = new(syntax.ConstDecl) // make sure last exists
329                                         inherited = false
330                                 }
331
332                                 // declare all constants
333                                 values := unpackExpr(last.Values)
334                                 for i, name := range s.NameList {
335                                         obj := NewConst(name.Pos(), pkg, name.Value, nil, iota)
336
337                                         var init syntax.Expr
338                                         if i < len(values) {
339                                                 init = values[i]
340                                         }
341
342                                         d := &declInfo{file: fileScope, vtyp: last.Type, init: init, inherited: inherited}
343                                         check.declarePkgObj(name, obj, d)
344                                 }
345
346                                 // Constants must always have init values.
347                                 check.arity(s.Pos(), s.NameList, values, true, inherited)
348
349                         case *syntax.VarDecl:
350                                 lhs := make([]*Var, len(s.NameList))
351                                 // If there's exactly one rhs initializer, use
352                                 // the same declInfo d1 for all lhs variables
353                                 // so that each lhs variable depends on the same
354                                 // rhs initializer (n:1 var declaration).
355                                 var d1 *declInfo
356                                 if _, ok := s.Values.(*syntax.ListExpr); !ok {
357                                         // The lhs elements are only set up after the for loop below,
358                                         // but that's ok because declarePkgObj only collects the declInfo
359                                         // for a later phase.
360                                         d1 = &declInfo{file: fileScope, lhs: lhs, vtyp: s.Type, init: s.Values}
361                                 }
362
363                                 // declare all variables
364                                 values := unpackExpr(s.Values)
365                                 for i, name := range s.NameList {
366                                         obj := NewVar(name.Pos(), pkg, name.Value, nil)
367                                         lhs[i] = obj
368
369                                         d := d1
370                                         if d == nil {
371                                                 // individual assignments
372                                                 var init syntax.Expr
373                                                 if i < len(values) {
374                                                         init = values[i]
375                                                 }
376                                                 d = &declInfo{file: fileScope, vtyp: s.Type, init: init}
377                                         }
378
379                                         check.declarePkgObj(name, obj, d)
380                                 }
381
382                                 // If we have no type, we must have values.
383                                 if s.Type == nil || values != nil {
384                                         check.arity(s.Pos(), s.NameList, values, false, false)
385                                 }
386
387                         case *syntax.TypeDecl:
388                                 obj := NewTypeName(s.Name.Pos(), pkg, s.Name.Value, nil)
389                                 check.declarePkgObj(s.Name, obj, &declInfo{file: fileScope, tdecl: s})
390
391                         case *syntax.FuncDecl:
392                                 d := s // TODO(gri) get rid of this
393                                 name := d.Name.Value
394                                 obj := NewFunc(d.Name.Pos(), pkg, name, nil)
395                                 if d.Recv == nil {
396                                         // regular function
397                                         if name == "init" {
398                                                 if d.TParamList != nil {
399                                                         //check.softErrorf(d.TParamList.Pos(), "func init must have no type parameters")
400                                                         check.softErrorf(d.Name, "func init must have no type parameters")
401                                                 }
402                                                 if t := d.Type; len(t.ParamList) != 0 || len(t.ResultList) != 0 {
403                                                         check.softErrorf(d, "func init must have no arguments and no return values")
404                                                 }
405                                                 // don't declare init functions in the package scope - they are invisible
406                                                 obj.parent = pkg.scope
407                                                 check.recordDef(d.Name, obj)
408                                                 // init functions must have a body
409                                                 if d.Body == nil {
410                                                         // TODO(gri) make this error message consistent with the others above
411                                                         check.softErrorf(obj.pos, "missing function body")
412                                                 }
413                                         } else {
414                                                 check.declare(pkg.scope, d.Name, obj, nopos)
415                                         }
416                                 } else {
417                                         // method
418                                         // d.Recv != nil
419                                         if !methodTypeParamsOk && len(d.TParamList) != 0 {
420                                                 //check.invalidASTf(d.TParamList.Pos(), "method must have no type parameters")
421                                                 check.invalidASTf(d, "method must have no type parameters")
422                                         }
423                                         ptr, recv, _ := check.unpackRecv(d.Recv.Type, false)
424                                         // (Methods with invalid receiver cannot be associated to a type, and
425                                         // methods with blank _ names are never found; no need to collect any
426                                         // of them. They will still be type-checked with all the other functions.)
427                                         if recv != nil && name != "_" {
428                                                 methods = append(methods, methodInfo{obj, ptr, recv})
429                                         }
430                                         check.recordDef(d.Name, obj)
431                                 }
432                                 info := &declInfo{file: fileScope, fdecl: d}
433                                 // Methods are not package-level objects but we still track them in the
434                                 // object map so that we can handle them like regular functions (if the
435                                 // receiver is invalid); also we need their fdecl info when associating
436                                 // them with their receiver base type, below.
437                                 check.objMap[obj] = info
438                                 obj.setOrder(uint32(len(check.objMap)))
439
440                         default:
441                                 check.invalidASTf(s, "unknown syntax.Decl node %T", s)
442                         }
443                 }
444         }
445
446         // verify that objects in package and file scopes have different names
447         for _, scope := range fileScopes {
448                 for _, obj := range scope.elems {
449                         if alt := pkg.scope.Lookup(obj.Name()); alt != nil {
450                                 if pkg, ok := obj.(*PkgName); ok {
451                                         check.errorf(alt, "%s already declared through import of %s", alt.Name(), pkg.Imported())
452                                         check.reportAltDecl(pkg)
453                                 } else {
454                                         check.errorf(alt, "%s already declared through dot-import of %s", alt.Name(), obj.Pkg())
455                                         // TODO(gri) dot-imported objects don't have a position; reportAltDecl won't print anything
456                                         check.reportAltDecl(obj)
457                                 }
458                         }
459                 }
460         }
461
462         // Now that we have all package scope objects and all methods,
463         // associate methods with receiver base type name where possible.
464         // Ignore methods that have an invalid receiver. They will be
465         // type-checked later, with regular functions.
466         if methods != nil {
467                 check.methods = make(map[*TypeName][]*Func)
468                 for i := range methods {
469                         m := &methods[i]
470                         // Determine the receiver base type and associate m with it.
471                         ptr, base := check.resolveBaseTypeName(m.ptr, m.recv)
472                         if base != nil {
473                                 m.obj.hasPtrRecv = ptr
474                                 check.methods[base] = append(check.methods[base], m.obj)
475                         }
476                 }
477         }
478 }
479
480 // unpackRecv unpacks a receiver type and returns its components: ptr indicates whether
481 // rtyp is a pointer receiver, rname is the receiver type name, and tparams are its
482 // type parameters, if any. The type parameters are only unpacked if unpackParams is
483 // set. If rname is nil, the receiver is unusable (i.e., the source has a bug which we
484 // cannot easily work around).
485 func (check *Checker) unpackRecv(rtyp syntax.Expr, unpackParams bool) (ptr bool, rname *syntax.Name, tparams []*syntax.Name) {
486 L: // unpack receiver type
487         // This accepts invalid receivers such as ***T and does not
488         // work for other invalid receivers, but we don't care. The
489         // validity of receiver expressions is checked elsewhere.
490         for {
491                 switch t := rtyp.(type) {
492                 case *syntax.ParenExpr:
493                         rtyp = t.X
494                 // case *ast.StarExpr:
495                 //      rtyp = t.X
496                 case *syntax.Operation:
497                         if t.Op != syntax.Mul || t.Y != nil {
498                                 break
499                         }
500                         rtyp = t.X
501                 default:
502                         break L
503                 }
504         }
505
506         // unpack type parameters, if any
507         if ptyp, _ := rtyp.(*syntax.IndexExpr); ptyp != nil {
508                 rtyp = ptyp.X
509                 if unpackParams {
510                         for _, arg := range unpackExpr(ptyp.Index) {
511                                 var par *syntax.Name
512                                 switch arg := arg.(type) {
513                                 case *syntax.Name:
514                                         par = arg
515                                 case *syntax.BadExpr:
516                                         // ignore - error already reported by parser
517                                 case nil:
518                                         check.invalidASTf(ptyp, "parameterized receiver contains nil parameters")
519                                 default:
520                                         check.errorf(arg, "receiver type parameter %s must be an identifier", arg)
521                                 }
522                                 if par == nil {
523                                         par = newName(arg.Pos(), "_")
524                                 }
525                                 tparams = append(tparams, par)
526                         }
527
528                 }
529         }
530
531         // unpack receiver name
532         if name, _ := rtyp.(*syntax.Name); name != nil {
533                 rname = name
534         }
535
536         return
537 }
538
539 // resolveBaseTypeName returns the non-alias base type name for typ, and whether
540 // there was a pointer indirection to get to it. The base type name must be declared
541 // in package scope, and there can be at most one pointer indirection. If no such type
542 // name exists, the returned base is nil.
543 func (check *Checker) resolveBaseTypeName(seenPtr bool, typ syntax.Expr) (ptr bool, base *TypeName) {
544         // Algorithm: Starting from a type expression, which may be a name,
545         // we follow that type through alias declarations until we reach a
546         // non-alias type name. If we encounter anything but pointer types or
547         // parentheses we're done. If we encounter more than one pointer type
548         // we're done.
549         ptr = seenPtr
550         var seen map[*TypeName]bool
551         for {
552                 typ = unparen(typ)
553
554                 // check if we have a pointer type
555                 // if pexpr, _ := typ.(*ast.StarExpr); pexpr != nil {
556                 if pexpr, _ := typ.(*syntax.Operation); pexpr != nil && pexpr.Op == syntax.Mul && pexpr.Y == nil {
557                         // if we've already seen a pointer, we're done
558                         if ptr {
559                                 return false, nil
560                         }
561                         ptr = true
562                         typ = unparen(pexpr.X) // continue with pointer base type
563                 }
564
565                 // typ must be a name
566                 name, _ := typ.(*syntax.Name)
567                 if name == nil {
568                         return false, nil
569                 }
570
571                 // name must denote an object found in the current package scope
572                 // (note that dot-imported objects are not in the package scope!)
573                 obj := check.pkg.scope.Lookup(name.Value)
574                 if obj == nil {
575                         return false, nil
576                 }
577
578                 // the object must be a type name...
579                 tname, _ := obj.(*TypeName)
580                 if tname == nil {
581                         return false, nil
582                 }
583
584                 // ... which we have not seen before
585                 if seen[tname] {
586                         return false, nil
587                 }
588
589                 // we're done if tdecl defined tname as a new type
590                 // (rather than an alias)
591                 tdecl := check.objMap[tname].tdecl // must exist for objects in package scope
592                 if !tdecl.Alias {
593                         return ptr, tname
594                 }
595
596                 // otherwise, continue resolving
597                 typ = tdecl.Type
598                 if seen == nil {
599                         seen = make(map[*TypeName]bool)
600                 }
601                 seen[tname] = true
602         }
603 }
604
605 // packageObjects typechecks all package objects, but not function bodies.
606 func (check *Checker) packageObjects() {
607         // process package objects in source order for reproducible results
608         objList := make([]Object, len(check.objMap))
609         i := 0
610         for obj := range check.objMap {
611                 objList[i] = obj
612                 i++
613         }
614         sort.Sort(inSourceOrder(objList))
615
616         // add new methods to already type-checked types (from a prior Checker.Files call)
617         for _, obj := range objList {
618                 if obj, _ := obj.(*TypeName); obj != nil && obj.typ != nil {
619                         check.collectMethods(obj)
620                 }
621         }
622
623         // We process non-alias declarations first, in order to avoid situations where
624         // the type of an alias declaration is needed before it is available. In general
625         // this is still not enough, as it is possible to create sufficiently convoluted
626         // recursive type definitions that will cause a type alias to be needed before it
627         // is available (see issue #25838 for examples).
628         // As an aside, the cmd/compiler suffers from the same problem (#25838).
629         var aliasList []*TypeName
630         // phase 1
631         for _, obj := range objList {
632                 // If we have a type alias, collect it for the 2nd phase.
633                 if tname, _ := obj.(*TypeName); tname != nil && check.objMap[tname].tdecl.Alias {
634                         aliasList = append(aliasList, tname)
635                         continue
636                 }
637
638                 check.objDecl(obj, nil)
639         }
640         // phase 2
641         for _, obj := range aliasList {
642                 check.objDecl(obj, nil)
643         }
644
645         // At this point we may have a non-empty check.methods map; this means that not all
646         // entries were deleted at the end of typeDecl because the respective receiver base
647         // types were not found. In that case, an error was reported when declaring those
648         // methods. We can now safely discard this map.
649         check.methods = nil
650 }
651
652 // inSourceOrder implements the sort.Sort interface.
653 type inSourceOrder []Object
654
655 func (a inSourceOrder) Len() int           { return len(a) }
656 func (a inSourceOrder) Less(i, j int) bool { return a[i].order() < a[j].order() }
657 func (a inSourceOrder) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
658
659 // unusedImports checks for unused imports.
660 func (check *Checker) unusedImports() {
661         // if function bodies are not checked, packages' uses are likely missing - don't check
662         if check.conf.IgnoreFuncBodies {
663                 return
664         }
665
666         // spec: "It is illegal (...) to directly import a package without referring to
667         // any of its exported identifiers. To import a package solely for its side-effects
668         // (initialization), use the blank identifier as explicit package name."
669
670         // check use of regular imported packages
671         for _, scope := range check.pkg.scope.children /* file scopes */ {
672                 for _, obj := range scope.elems {
673                         if obj, ok := obj.(*PkgName); ok {
674                                 // Unused "blank imports" are automatically ignored
675                                 // since _ identifiers are not entered into scopes.
676                                 if !obj.used {
677                                         path := obj.imported.path
678                                         base := pkgName(path)
679                                         if obj.name == base {
680                                                 if check.conf.CompilerErrorMessages {
681                                                         check.softErrorf(obj.pos, "%q imported and not used", path)
682                                                 } else {
683                                                         check.softErrorf(obj.pos, "%q imported but not used", path)
684                                                 }
685                                         } else {
686                                                 if check.conf.CompilerErrorMessages {
687                                                         check.softErrorf(obj.pos, "%q imported and not used as %s", path, obj.name)
688                                                 } else {
689                                                         check.softErrorf(obj.pos, "%q imported but not used as %s", path, obj.name)
690                                                 }
691                                         }
692                                 }
693                         }
694                 }
695         }
696
697         // check use of dot-imported packages
698         for _, unusedDotImports := range check.unusedDotImports {
699                 for pkg, pos := range unusedDotImports {
700                         if check.conf.CompilerErrorMessages {
701                                 check.softErrorf(pos, "%q imported and not used", pkg.path)
702                         } else {
703                                 check.softErrorf(pos, "%q imported but not used", pkg.path)
704                         }
705                 }
706         }
707 }
708
709 // pkgName returns the package name (last element) of an import path.
710 func pkgName(path string) string {
711         if i := strings.LastIndex(path, "/"); i >= 0 {
712                 path = path[i+1:]
713         }
714         return path
715 }
716
717 // dir makes a good-faith attempt to return the directory
718 // portion of path. If path is empty, the result is ".".
719 // (Per the go/build package dependency tests, we cannot import
720 // path/filepath and simply use filepath.Dir.)
721 func dir(path string) string {
722         if i := strings.LastIndexAny(path, `/\`); i > 0 {
723                 return path[:i]
724         }
725         // i <= 0
726         return "."
727 }