github.com/rakyll/go@v0.0.0-20170216000551-64c02460d703/src/go/types/resolver.go (about)

     1  // Copyright 2013 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.
     4  
     5  package types
     6  
     7  import (
     8  	"fmt"
     9  	"go/ast"
    10  	"go/constant"
    11  	"go/token"
    12  	"strconv"
    13  	"strings"
    14  	"unicode"
    15  )
    16  
    17  // A declInfo describes a package-level const, type, var, or func declaration.
    18  type declInfo struct {
    19  	file  *Scope        // scope of file containing this declaration
    20  	lhs   []*Var        // lhs of n:1 variable declarations, or nil
    21  	typ   ast.Expr      // type, or nil
    22  	init  ast.Expr      // init/orig expression, or nil
    23  	fdecl *ast.FuncDecl // func declaration, or nil
    24  	alias bool          // type alias declaration
    25  
    26  	// The deps field tracks initialization expression dependencies.
    27  	// As a special (overloaded) case, it also tracks dependencies of
    28  	// interface types on embedded interfaces (see ordering.go).
    29  	deps objSet // lazily initialized
    30  }
    31  
    32  // An objSet is simply a set of objects.
    33  type objSet map[Object]bool
    34  
    35  // hasInitializer reports whether the declared object has an initialization
    36  // expression or function body.
    37  func (d *declInfo) hasInitializer() bool {
    38  	return d.init != nil || d.fdecl != nil && d.fdecl.Body != nil
    39  }
    40  
    41  // addDep adds obj to the set of objects d's init expression depends on.
    42  func (d *declInfo) addDep(obj Object) {
    43  	m := d.deps
    44  	if m == nil {
    45  		m = make(objSet)
    46  		d.deps = m
    47  	}
    48  	m[obj] = true
    49  }
    50  
    51  // arityMatch checks that the lhs and rhs of a const or var decl
    52  // have the appropriate number of names and init exprs. For const
    53  // decls, init is the value spec providing the init exprs; for
    54  // var decls, init is nil (the init exprs are in s in this case).
    55  func (check *Checker) arityMatch(s, init *ast.ValueSpec) {
    56  	l := len(s.Names)
    57  	r := len(s.Values)
    58  	if init != nil {
    59  		r = len(init.Values)
    60  	}
    61  
    62  	switch {
    63  	case init == nil && r == 0:
    64  		// var decl w/o init expr
    65  		if s.Type == nil {
    66  			check.errorf(s.Pos(), "missing type or init expr")
    67  		}
    68  	case l < r:
    69  		if l < len(s.Values) {
    70  			// init exprs from s
    71  			n := s.Values[l]
    72  			check.errorf(n.Pos(), "extra init expr %s", n)
    73  			// TODO(gri) avoid declared but not used error here
    74  		} else {
    75  			// init exprs "inherited"
    76  			check.errorf(s.Pos(), "extra init expr at %s", check.fset.Position(init.Pos()))
    77  			// TODO(gri) avoid declared but not used error here
    78  		}
    79  	case l > r && (init != nil || r != 1):
    80  		n := s.Names[r]
    81  		check.errorf(n.Pos(), "missing init expr for %s", n)
    82  	}
    83  }
    84  
    85  func validatedImportPath(path string) (string, error) {
    86  	s, err := strconv.Unquote(path)
    87  	if err != nil {
    88  		return "", err
    89  	}
    90  	if s == "" {
    91  		return "", fmt.Errorf("empty string")
    92  	}
    93  	const illegalChars = `!"#$%&'()*,:;<=>?[\]^{|}` + "`\uFFFD"
    94  	for _, r := range s {
    95  		if !unicode.IsGraphic(r) || unicode.IsSpace(r) || strings.ContainsRune(illegalChars, r) {
    96  			return s, fmt.Errorf("invalid character %#U", r)
    97  		}
    98  	}
    99  	return s, nil
   100  }
   101  
   102  // declarePkgObj declares obj in the package scope, records its ident -> obj mapping,
   103  // and updates check.objMap. The object must not be a function or method.
   104  func (check *Checker) declarePkgObj(ident *ast.Ident, obj Object, d *declInfo) {
   105  	assert(ident.Name == obj.Name())
   106  
   107  	// spec: "A package-scope or file-scope identifier with name init
   108  	// may only be declared to be a function with this (func()) signature."
   109  	if ident.Name == "init" {
   110  		check.errorf(ident.Pos(), "cannot declare init - must be func")
   111  		return
   112  	}
   113  
   114  	check.declare(check.pkg.scope, ident, obj, token.NoPos)
   115  	check.objMap[obj] = d
   116  	obj.setOrder(uint32(len(check.objMap)))
   117  }
   118  
   119  // filename returns a filename suitable for debugging output.
   120  func (check *Checker) filename(fileNo int) string {
   121  	file := check.files[fileNo]
   122  	if pos := file.Pos(); pos.IsValid() {
   123  		return check.fset.File(pos).Name()
   124  	}
   125  	return fmt.Sprintf("file[%d]", fileNo)
   126  }
   127  
   128  // collectObjects collects all file and package objects and inserts them
   129  // into their respective scopes. It also performs imports and associates
   130  // methods with receiver base type names.
   131  func (check *Checker) collectObjects() {
   132  	pkg := check.pkg
   133  
   134  	// pkgImports is the set of packages already imported by any package file seen
   135  	// so far. Used to avoid duplicate entries in pkg.imports. Allocate and populate
   136  	// it (pkg.imports may not be empty if we are checking test files incrementally).
   137  	var pkgImports = make(map[*Package]bool)
   138  	for _, imp := range pkg.imports {
   139  		pkgImports[imp] = true
   140  	}
   141  
   142  	// srcDir is the directory used by the Importer to look up packages.
   143  	// The typechecker itself doesn't need this information so it is not
   144  	// explicitly provided. Instead, we extract it from position info of
   145  	// the source files as needed.
   146  	// This is the only place where the type-checker (just the importer)
   147  	// needs to know the actual source location of a file.
   148  	// TODO(gri) can we come up with a better API instead?
   149  	var srcDir string
   150  	if len(check.files) > 0 {
   151  		// FileName may be "" (typically for tests) in which case
   152  		// we get "." as the srcDir which is what we would want.
   153  		srcDir = dir(check.fset.Position(check.files[0].Name.Pos()).Filename)
   154  	}
   155  
   156  	for fileNo, file := range check.files {
   157  		// The package identifier denotes the current package,
   158  		// but there is no corresponding package object.
   159  		check.recordDef(file.Name, nil)
   160  
   161  		// Use the actual source file extent rather than *ast.File extent since the
   162  		// latter doesn't include comments which appear at the start or end of the file.
   163  		// Be conservative and use the *ast.File extent if we don't have a *token.File.
   164  		pos, end := file.Pos(), file.End()
   165  		if f := check.fset.File(file.Pos()); f != nil {
   166  			pos, end = token.Pos(f.Base()), token.Pos(f.Base()+f.Size())
   167  		}
   168  		fileScope := NewScope(check.pkg.scope, pos, end, check.filename(fileNo))
   169  		check.recordScope(file, fileScope)
   170  
   171  		for _, decl := range file.Decls {
   172  			switch d := decl.(type) {
   173  			case *ast.BadDecl:
   174  				// ignore
   175  
   176  			case *ast.GenDecl:
   177  				var last *ast.ValueSpec // last ValueSpec with type or init exprs seen
   178  				for iota, spec := range d.Specs {
   179  					switch s := spec.(type) {
   180  					case *ast.ImportSpec:
   181  						// import package
   182  						var imp *Package
   183  						path, err := validatedImportPath(s.Path.Value)
   184  						if err != nil {
   185  							check.errorf(s.Path.Pos(), "invalid import path (%s)", err)
   186  							continue
   187  						}
   188  						if path == "C" && check.conf.FakeImportC {
   189  							// TODO(gri) shouldn't create a new one each time
   190  							imp = NewPackage("C", "C")
   191  							imp.fake = true
   192  						} else {
   193  							// ordinary import
   194  							if importer := check.conf.Importer; importer == nil {
   195  								err = fmt.Errorf("Config.Importer not installed")
   196  							} else if importerFrom, ok := importer.(ImporterFrom); ok {
   197  								imp, err = importerFrom.ImportFrom(path, srcDir, 0)
   198  								if imp == nil && err == nil {
   199  									err = fmt.Errorf("Config.Importer.ImportFrom(%s, %s, 0) returned nil but no error", path, pkg.path)
   200  								}
   201  							} else {
   202  								imp, err = importer.Import(path)
   203  								if imp == nil && err == nil {
   204  									err = fmt.Errorf("Config.Importer.Import(%s) returned nil but no error", path)
   205  								}
   206  							}
   207  							if err != nil {
   208  								check.errorf(s.Path.Pos(), "could not import %s (%s)", path, err)
   209  								continue
   210  							}
   211  						}
   212  
   213  						// add package to list of explicit imports
   214  						// (this functionality is provided as a convenience
   215  						// for clients; it is not needed for type-checking)
   216  						if !pkgImports[imp] {
   217  							pkgImports[imp] = true
   218  							if imp != Unsafe {
   219  								pkg.imports = append(pkg.imports, imp)
   220  							}
   221  						}
   222  
   223  						// local name overrides imported package name
   224  						name := imp.name
   225  						if s.Name != nil {
   226  							name = s.Name.Name
   227  							if path == "C" {
   228  								// match cmd/compile (not prescribed by spec)
   229  								check.errorf(s.Name.Pos(), `cannot rename import "C"`)
   230  								continue
   231  							}
   232  							if name == "init" {
   233  								check.errorf(s.Name.Pos(), "cannot declare init - must be func")
   234  								continue
   235  							}
   236  						}
   237  
   238  						obj := NewPkgName(s.Pos(), pkg, name, imp)
   239  						if s.Name != nil {
   240  							// in a dot-import, the dot represents the package
   241  							check.recordDef(s.Name, obj)
   242  						} else {
   243  							check.recordImplicit(s, obj)
   244  						}
   245  
   246  						if path == "C" {
   247  							// match cmd/compile (not prescribed by spec)
   248  							obj.used = true
   249  						}
   250  
   251  						// add import to file scope
   252  						if name == "." {
   253  							// merge imported scope with file scope
   254  							for _, obj := range imp.scope.elems {
   255  								// A package scope may contain non-exported objects,
   256  								// do not import them!
   257  								if obj.Exported() {
   258  									// TODO(gri) When we import a package, we create
   259  									// a new local package object. We should do the
   260  									// same for each dot-imported object. That way
   261  									// they can have correct position information.
   262  									// (We must not modify their existing position
   263  									// information because the same package - found
   264  									// via Config.Packages - may be dot-imported in
   265  									// another package!)
   266  									check.declare(fileScope, nil, obj, token.NoPos)
   267  									check.recordImplicit(s, obj)
   268  								}
   269  							}
   270  							// add position to set of dot-import positions for this file
   271  							// (this is only needed for "imported but not used" errors)
   272  							check.addUnusedDotImport(fileScope, imp, s.Pos())
   273  						} else {
   274  							// declare imported package object in file scope
   275  							check.declare(fileScope, nil, obj, token.NoPos)
   276  						}
   277  
   278  					case *ast.ValueSpec:
   279  						switch d.Tok {
   280  						case token.CONST:
   281  							// determine which initialization expressions to use
   282  							switch {
   283  							case s.Type != nil || len(s.Values) > 0:
   284  								last = s
   285  							case last == nil:
   286  								last = new(ast.ValueSpec) // make sure last exists
   287  							}
   288  
   289  							// declare all constants
   290  							for i, name := range s.Names {
   291  								obj := NewConst(name.Pos(), pkg, name.Name, nil, constant.MakeInt64(int64(iota)))
   292  
   293  								var init ast.Expr
   294  								if i < len(last.Values) {
   295  									init = last.Values[i]
   296  								}
   297  
   298  								d := &declInfo{file: fileScope, typ: last.Type, init: init}
   299  								check.declarePkgObj(name, obj, d)
   300  							}
   301  
   302  							check.arityMatch(s, last)
   303  
   304  						case token.VAR:
   305  							lhs := make([]*Var, len(s.Names))
   306  							// If there's exactly one rhs initializer, use
   307  							// the same declInfo d1 for all lhs variables
   308  							// so that each lhs variable depends on the same
   309  							// rhs initializer (n:1 var declaration).
   310  							var d1 *declInfo
   311  							if len(s.Values) == 1 {
   312  								// The lhs elements are only set up after the for loop below,
   313  								// but that's ok because declareVar only collects the declInfo
   314  								// for a later phase.
   315  								d1 = &declInfo{file: fileScope, lhs: lhs, typ: s.Type, init: s.Values[0]}
   316  							}
   317  
   318  							// declare all variables
   319  							for i, name := range s.Names {
   320  								obj := NewVar(name.Pos(), pkg, name.Name, nil)
   321  								lhs[i] = obj
   322  
   323  								d := d1
   324  								if d == nil {
   325  									// individual assignments
   326  									var init ast.Expr
   327  									if i < len(s.Values) {
   328  										init = s.Values[i]
   329  									}
   330  									d = &declInfo{file: fileScope, typ: s.Type, init: init}
   331  								}
   332  
   333  								check.declarePkgObj(name, obj, d)
   334  							}
   335  
   336  							check.arityMatch(s, nil)
   337  
   338  						default:
   339  							check.invalidAST(s.Pos(), "invalid token %s", d.Tok)
   340  						}
   341  
   342  					case *ast.TypeSpec:
   343  						obj := NewTypeName(s.Name.Pos(), pkg, s.Name.Name, nil)
   344  						check.declarePkgObj(s.Name, obj, &declInfo{file: fileScope, typ: s.Type, alias: s.Assign.IsValid()})
   345  
   346  					default:
   347  						check.invalidAST(s.Pos(), "unknown ast.Spec node %T", s)
   348  					}
   349  				}
   350  
   351  			case *ast.FuncDecl:
   352  				name := d.Name.Name
   353  				obj := NewFunc(d.Name.Pos(), pkg, name, nil)
   354  				if d.Recv == nil {
   355  					// regular function
   356  					if name == "init" {
   357  						// don't declare init functions in the package scope - they are invisible
   358  						obj.parent = pkg.scope
   359  						check.recordDef(d.Name, obj)
   360  						// init functions must have a body
   361  						if d.Body == nil {
   362  							check.softErrorf(obj.pos, "missing function body")
   363  						}
   364  					} else {
   365  						check.declare(pkg.scope, d.Name, obj, token.NoPos)
   366  					}
   367  				} else {
   368  					// method
   369  					check.recordDef(d.Name, obj)
   370  					// Associate method with receiver base type name, if possible.
   371  					// Ignore methods that have an invalid receiver, or a blank _
   372  					// receiver name. They will be type-checked later, with regular
   373  					// functions.
   374  					if list := d.Recv.List; len(list) > 0 {
   375  						typ := list[0].Type
   376  						if ptr, _ := typ.(*ast.StarExpr); ptr != nil {
   377  							typ = ptr.X
   378  						}
   379  						if base, _ := typ.(*ast.Ident); base != nil && base.Name != "_" {
   380  							check.assocMethod(base.Name, obj)
   381  						}
   382  					}
   383  				}
   384  				info := &declInfo{file: fileScope, fdecl: d}
   385  				check.objMap[obj] = info
   386  				obj.setOrder(uint32(len(check.objMap)))
   387  
   388  			default:
   389  				check.invalidAST(d.Pos(), "unknown ast.Decl node %T", d)
   390  			}
   391  		}
   392  	}
   393  
   394  	// verify that objects in package and file scopes have different names
   395  	for _, scope := range check.pkg.scope.children /* file scopes */ {
   396  		for _, obj := range scope.elems {
   397  			if alt := pkg.scope.Lookup(obj.Name()); alt != nil {
   398  				if pkg, ok := obj.(*PkgName); ok {
   399  					check.errorf(alt.Pos(), "%s already declared through import of %s", alt.Name(), pkg.Imported())
   400  					check.reportAltDecl(pkg)
   401  				} else {
   402  					check.errorf(alt.Pos(), "%s already declared through dot-import of %s", alt.Name(), obj.Pkg())
   403  					// TODO(gri) dot-imported objects don't have a position; reportAltDecl won't print anything
   404  					check.reportAltDecl(obj)
   405  				}
   406  			}
   407  		}
   408  	}
   409  }
   410  
   411  // packageObjects typechecks all package objects in objList, but not function bodies.
   412  func (check *Checker) packageObjects(objList []Object) {
   413  	// add new methods to already type-checked types (from a prior Checker.Files call)
   414  	for _, obj := range objList {
   415  		if obj, _ := obj.(*TypeName); obj != nil && obj.typ != nil {
   416  			check.addMethodDecls(obj)
   417  		}
   418  	}
   419  
   420  	// pre-allocate space for type declaration paths so that the underlying array is reused
   421  	typePath := make([]*TypeName, 0, 8)
   422  
   423  	for _, obj := range objList {
   424  		check.objDecl(obj, nil, typePath)
   425  	}
   426  
   427  	// At this point we may have a non-empty check.methods map; this means that not all
   428  	// entries were deleted at the end of typeDecl because the respective receiver base
   429  	// types were not found. In that case, an error was reported when declaring those
   430  	// methods. We can now safely discard this map.
   431  	check.methods = nil
   432  }
   433  
   434  // functionBodies typechecks all function bodies.
   435  func (check *Checker) functionBodies() {
   436  	for _, f := range check.funcs {
   437  		check.funcBody(f.decl, f.name, f.sig, f.body)
   438  	}
   439  }
   440  
   441  // unusedImports checks for unused imports.
   442  func (check *Checker) unusedImports() {
   443  	// if function bodies are not checked, packages' uses are likely missing - don't check
   444  	if check.conf.IgnoreFuncBodies {
   445  		return
   446  	}
   447  
   448  	// spec: "It is illegal (...) to directly import a package without referring to
   449  	// any of its exported identifiers. To import a package solely for its side-effects
   450  	// (initialization), use the blank identifier as explicit package name."
   451  
   452  	// check use of regular imported packages
   453  	for _, scope := range check.pkg.scope.children /* file scopes */ {
   454  		for _, obj := range scope.elems {
   455  			if obj, ok := obj.(*PkgName); ok {
   456  				// Unused "blank imports" are automatically ignored
   457  				// since _ identifiers are not entered into scopes.
   458  				if !obj.used {
   459  					path := obj.imported.path
   460  					base := pkgName(path)
   461  					if obj.name == base {
   462  						check.softErrorf(obj.pos, "%q imported but not used", path)
   463  					} else {
   464  						check.softErrorf(obj.pos, "%q imported but not used as %s", path, obj.name)
   465  					}
   466  				}
   467  			}
   468  		}
   469  	}
   470  
   471  	// check use of dot-imported packages
   472  	for _, unusedDotImports := range check.unusedDotImports {
   473  		for pkg, pos := range unusedDotImports {
   474  			check.softErrorf(pos, "%q imported but not used", pkg.path)
   475  		}
   476  	}
   477  }
   478  
   479  // pkgName returns the package name (last element) of an import path.
   480  func pkgName(path string) string {
   481  	if i := strings.LastIndex(path, "/"); i >= 0 {
   482  		path = path[i+1:]
   483  	}
   484  	return path
   485  }
   486  
   487  // dir makes a good-faith attempt to return the directory
   488  // portion of path. If path is empty, the result is ".".
   489  // (Per the go/build package dependency tests, we cannot import
   490  // path/filepath and simply use filepath.Dir.)
   491  func dir(path string) string {
   492  	if i := strings.LastIndexAny(path, `/\`); i > 0 {
   493  		return path[:i]
   494  	}
   495  	// i <= 0
   496  	return "."
   497  }