github.com/zebozhuang/go@v0.0.0-20200207033046-f8a98f6f5c5d/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  func (check *Checker) importPackage(pos token.Pos, path, dir string) *Package {
   129  	// If we already have a package for the given (path, dir)
   130  	// pair, use it instead of doing a full import.
   131  	// Checker.impMap only caches packages that are marked Complete
   132  	// or fake (dummy packages for failed imports). Incomplete but
   133  	// non-fake packages do require an import to complete them.
   134  	key := importKey{path, dir}
   135  	imp := check.impMap[key]
   136  	if imp != nil {
   137  		return imp
   138  	}
   139  
   140  	// no package yet => import it
   141  	if path == "C" && check.conf.FakeImportC {
   142  		imp = NewPackage("C", "C")
   143  		imp.fake = true
   144  	} else {
   145  		// ordinary import
   146  		var err error
   147  		if importer := check.conf.Importer; importer == nil {
   148  			err = fmt.Errorf("Config.Importer not installed")
   149  		} else if importerFrom, ok := importer.(ImporterFrom); ok {
   150  			imp, err = importerFrom.ImportFrom(path, dir, 0)
   151  			if imp == nil && err == nil {
   152  				err = fmt.Errorf("Config.Importer.ImportFrom(%s, %s, 0) returned nil but no error", path, dir)
   153  			}
   154  		} else {
   155  			imp, err = importer.Import(path)
   156  			if imp == nil && err == nil {
   157  				err = fmt.Errorf("Config.Importer.Import(%s) returned nil but no error", path)
   158  			}
   159  		}
   160  		// make sure we have a valid package name
   161  		// (errors here can only happen through manipulation of packages after creation)
   162  		if err == nil && imp != nil && (imp.name == "_" || imp.name == "") {
   163  			err = fmt.Errorf("invalid package name: %q", imp.name)
   164  			imp = nil // create fake package below
   165  		}
   166  		if err != nil {
   167  			check.errorf(pos, "could not import %s (%s)", path, err)
   168  			if imp == nil {
   169  				// create a new fake package
   170  				// come up with a sensible package name (heuristic)
   171  				name := path
   172  				if i := len(name); i > 0 && name[i-1] == '/' {
   173  					name = name[:i-1]
   174  				}
   175  				if i := strings.LastIndex(name, "/"); i >= 0 {
   176  					name = name[i+1:]
   177  				}
   178  				imp = NewPackage(path, name)
   179  			}
   180  			// continue to use the package as best as we can
   181  			imp.fake = true // avoid follow-up lookup failures
   182  		}
   183  	}
   184  
   185  	// package should be complete or marked fake, but be cautious
   186  	if imp.complete || imp.fake {
   187  		check.impMap[key] = imp
   188  		return imp
   189  	}
   190  
   191  	// something went wrong (importer may have returned incomplete package without error)
   192  	return nil
   193  }
   194  
   195  // collectObjects collects all file and package objects and inserts them
   196  // into their respective scopes. It also performs imports and associates
   197  // methods with receiver base type names.
   198  func (check *Checker) collectObjects() {
   199  	pkg := check.pkg
   200  
   201  	// pkgImports is the set of packages already imported by any package file seen
   202  	// so far. Used to avoid duplicate entries in pkg.imports. Allocate and populate
   203  	// it (pkg.imports may not be empty if we are checking test files incrementally).
   204  	// Note that pkgImports is keyed by package (and thus package path), not by an
   205  	// importKey value. Two different importKey values may map to the same package
   206  	// which is why we cannot use the check.impMap here.
   207  	var pkgImports = make(map[*Package]bool)
   208  	for _, imp := range pkg.imports {
   209  		pkgImports[imp] = true
   210  	}
   211  
   212  	for fileNo, file := range check.files {
   213  		// The package identifier denotes the current package,
   214  		// but there is no corresponding package object.
   215  		check.recordDef(file.Name, nil)
   216  
   217  		// Use the actual source file extent rather than *ast.File extent since the
   218  		// latter doesn't include comments which appear at the start or end of the file.
   219  		// Be conservative and use the *ast.File extent if we don't have a *token.File.
   220  		pos, end := file.Pos(), file.End()
   221  		if f := check.fset.File(file.Pos()); f != nil {
   222  			pos, end = token.Pos(f.Base()), token.Pos(f.Base()+f.Size())
   223  		}
   224  		fileScope := NewScope(check.pkg.scope, pos, end, check.filename(fileNo))
   225  		check.recordScope(file, fileScope)
   226  
   227  		// determine file directory, necessary to resolve imports
   228  		// FileName may be "" (typically for tests) in which case
   229  		// we get "." as the directory which is what we would want.
   230  		fileDir := dir(check.fset.Position(file.Name.Pos()).Filename)
   231  
   232  		for _, decl := range file.Decls {
   233  			switch d := decl.(type) {
   234  			case *ast.BadDecl:
   235  				// ignore
   236  
   237  			case *ast.GenDecl:
   238  				var last *ast.ValueSpec // last ValueSpec with type or init exprs seen
   239  				for iota, spec := range d.Specs {
   240  					switch s := spec.(type) {
   241  					case *ast.ImportSpec:
   242  						// import package
   243  						path, err := validatedImportPath(s.Path.Value)
   244  						if err != nil {
   245  							check.errorf(s.Path.Pos(), "invalid import path (%s)", err)
   246  							continue
   247  						}
   248  
   249  						imp := check.importPackage(s.Path.Pos(), path, fileDir)
   250  						if imp == nil {
   251  							continue
   252  						}
   253  
   254  						// add package to list of explicit imports
   255  						// (this functionality is provided as a convenience
   256  						// for clients; it is not needed for type-checking)
   257  						if !pkgImports[imp] {
   258  							pkgImports[imp] = true
   259  							pkg.imports = append(pkg.imports, imp)
   260  						}
   261  
   262  						// local name overrides imported package name
   263  						name := imp.name
   264  						if s.Name != nil {
   265  							name = s.Name.Name
   266  							if path == "C" {
   267  								// match cmd/compile (not prescribed by spec)
   268  								check.errorf(s.Name.Pos(), `cannot rename import "C"`)
   269  								continue
   270  							}
   271  							if name == "init" {
   272  								check.errorf(s.Name.Pos(), "cannot declare init - must be func")
   273  								continue
   274  							}
   275  						}
   276  
   277  						obj := NewPkgName(s.Pos(), pkg, name, imp)
   278  						if s.Name != nil {
   279  							// in a dot-import, the dot represents the package
   280  							check.recordDef(s.Name, obj)
   281  						} else {
   282  							check.recordImplicit(s, obj)
   283  						}
   284  
   285  						if path == "C" {
   286  							// match cmd/compile (not prescribed by spec)
   287  							obj.used = true
   288  						}
   289  
   290  						// add import to file scope
   291  						if name == "." {
   292  							// merge imported scope with file scope
   293  							for _, obj := range imp.scope.elems {
   294  								// A package scope may contain non-exported objects,
   295  								// do not import them!
   296  								if obj.Exported() {
   297  									// TODO(gri) When we import a package, we create
   298  									// a new local package object. We should do the
   299  									// same for each dot-imported object. That way
   300  									// they can have correct position information.
   301  									// (We must not modify their existing position
   302  									// information because the same package - found
   303  									// via Config.Packages - may be dot-imported in
   304  									// another package!)
   305  									check.declare(fileScope, nil, obj, token.NoPos)
   306  									check.recordImplicit(s, obj)
   307  								}
   308  							}
   309  							// add position to set of dot-import positions for this file
   310  							// (this is only needed for "imported but not used" errors)
   311  							check.addUnusedDotImport(fileScope, imp, s.Pos())
   312  						} else {
   313  							// declare imported package object in file scope
   314  							check.declare(fileScope, nil, obj, token.NoPos)
   315  						}
   316  
   317  					case *ast.ValueSpec:
   318  						switch d.Tok {
   319  						case token.CONST:
   320  							// determine which initialization expressions to use
   321  							switch {
   322  							case s.Type != nil || len(s.Values) > 0:
   323  								last = s
   324  							case last == nil:
   325  								last = new(ast.ValueSpec) // make sure last exists
   326  							}
   327  
   328  							// declare all constants
   329  							for i, name := range s.Names {
   330  								obj := NewConst(name.Pos(), pkg, name.Name, nil, constant.MakeInt64(int64(iota)))
   331  
   332  								var init ast.Expr
   333  								if i < len(last.Values) {
   334  									init = last.Values[i]
   335  								}
   336  
   337  								d := &declInfo{file: fileScope, typ: last.Type, init: init}
   338  								check.declarePkgObj(name, obj, d)
   339  							}
   340  
   341  							check.arityMatch(s, last)
   342  
   343  						case token.VAR:
   344  							lhs := make([]*Var, len(s.Names))
   345  							// If there's exactly one rhs initializer, use
   346  							// the same declInfo d1 for all lhs variables
   347  							// so that each lhs variable depends on the same
   348  							// rhs initializer (n:1 var declaration).
   349  							var d1 *declInfo
   350  							if len(s.Values) == 1 {
   351  								// The lhs elements are only set up after the for loop below,
   352  								// but that's ok because declareVar only collects the declInfo
   353  								// for a later phase.
   354  								d1 = &declInfo{file: fileScope, lhs: lhs, typ: s.Type, init: s.Values[0]}
   355  							}
   356  
   357  							// declare all variables
   358  							for i, name := range s.Names {
   359  								obj := NewVar(name.Pos(), pkg, name.Name, nil)
   360  								lhs[i] = obj
   361  
   362  								d := d1
   363  								if d == nil {
   364  									// individual assignments
   365  									var init ast.Expr
   366  									if i < len(s.Values) {
   367  										init = s.Values[i]
   368  									}
   369  									d = &declInfo{file: fileScope, typ: s.Type, init: init}
   370  								}
   371  
   372  								check.declarePkgObj(name, obj, d)
   373  							}
   374  
   375  							check.arityMatch(s, nil)
   376  
   377  						default:
   378  							check.invalidAST(s.Pos(), "invalid token %s", d.Tok)
   379  						}
   380  
   381  					case *ast.TypeSpec:
   382  						obj := NewTypeName(s.Name.Pos(), pkg, s.Name.Name, nil)
   383  						check.declarePkgObj(s.Name, obj, &declInfo{file: fileScope, typ: s.Type, alias: s.Assign.IsValid()})
   384  
   385  					default:
   386  						check.invalidAST(s.Pos(), "unknown ast.Spec node %T", s)
   387  					}
   388  				}
   389  
   390  			case *ast.FuncDecl:
   391  				name := d.Name.Name
   392  				obj := NewFunc(d.Name.Pos(), pkg, name, nil)
   393  				if d.Recv == nil {
   394  					// regular function
   395  					if name == "init" {
   396  						// don't declare init functions in the package scope - they are invisible
   397  						obj.parent = pkg.scope
   398  						check.recordDef(d.Name, obj)
   399  						// init functions must have a body
   400  						if d.Body == nil {
   401  							check.softErrorf(obj.pos, "missing function body")
   402  						}
   403  					} else {
   404  						check.declare(pkg.scope, d.Name, obj, token.NoPos)
   405  					}
   406  				} else {
   407  					// method
   408  					check.recordDef(d.Name, obj)
   409  					// Associate method with receiver base type name, if possible.
   410  					// Ignore methods that have an invalid receiver, or a blank _
   411  					// receiver name. They will be type-checked later, with regular
   412  					// functions.
   413  					if list := d.Recv.List; len(list) > 0 {
   414  						typ := list[0].Type
   415  						if ptr, _ := typ.(*ast.StarExpr); ptr != nil {
   416  							typ = ptr.X
   417  						}
   418  						if base, _ := typ.(*ast.Ident); base != nil && base.Name != "_" {
   419  							check.assocMethod(base.Name, obj)
   420  						}
   421  					}
   422  				}
   423  				info := &declInfo{file: fileScope, fdecl: d}
   424  				check.objMap[obj] = info
   425  				obj.setOrder(uint32(len(check.objMap)))
   426  
   427  			default:
   428  				check.invalidAST(d.Pos(), "unknown ast.Decl node %T", d)
   429  			}
   430  		}
   431  	}
   432  
   433  	// verify that objects in package and file scopes have different names
   434  	for _, scope := range check.pkg.scope.children /* file scopes */ {
   435  		for _, obj := range scope.elems {
   436  			if alt := pkg.scope.Lookup(obj.Name()); alt != nil {
   437  				if pkg, ok := obj.(*PkgName); ok {
   438  					check.errorf(alt.Pos(), "%s already declared through import of %s", alt.Name(), pkg.Imported())
   439  					check.reportAltDecl(pkg)
   440  				} else {
   441  					check.errorf(alt.Pos(), "%s already declared through dot-import of %s", alt.Name(), obj.Pkg())
   442  					// TODO(gri) dot-imported objects don't have a position; reportAltDecl won't print anything
   443  					check.reportAltDecl(obj)
   444  				}
   445  			}
   446  		}
   447  	}
   448  }
   449  
   450  // packageObjects typechecks all package objects in objList, but not function bodies.
   451  func (check *Checker) packageObjects(objList []Object) {
   452  	// add new methods to already type-checked types (from a prior Checker.Files call)
   453  	for _, obj := range objList {
   454  		if obj, _ := obj.(*TypeName); obj != nil && obj.typ != nil {
   455  			check.addMethodDecls(obj)
   456  		}
   457  	}
   458  
   459  	// pre-allocate space for type declaration paths so that the underlying array is reused
   460  	typePath := make([]*TypeName, 0, 8)
   461  
   462  	for _, obj := range objList {
   463  		check.objDecl(obj, nil, typePath)
   464  	}
   465  
   466  	// At this point we may have a non-empty check.methods map; this means that not all
   467  	// entries were deleted at the end of typeDecl because the respective receiver base
   468  	// types were not found. In that case, an error was reported when declaring those
   469  	// methods. We can now safely discard this map.
   470  	check.methods = nil
   471  }
   472  
   473  // functionBodies typechecks all function bodies.
   474  func (check *Checker) functionBodies() {
   475  	for _, f := range check.funcs {
   476  		check.funcBody(f.decl, f.name, f.sig, f.body)
   477  	}
   478  }
   479  
   480  // unusedImports checks for unused imports.
   481  func (check *Checker) unusedImports() {
   482  	// if function bodies are not checked, packages' uses are likely missing - don't check
   483  	if check.conf.IgnoreFuncBodies {
   484  		return
   485  	}
   486  
   487  	// spec: "It is illegal (...) to directly import a package without referring to
   488  	// any of its exported identifiers. To import a package solely for its side-effects
   489  	// (initialization), use the blank identifier as explicit package name."
   490  
   491  	// check use of regular imported packages
   492  	for _, scope := range check.pkg.scope.children /* file scopes */ {
   493  		for _, obj := range scope.elems {
   494  			if obj, ok := obj.(*PkgName); ok {
   495  				// Unused "blank imports" are automatically ignored
   496  				// since _ identifiers are not entered into scopes.
   497  				if !obj.used {
   498  					path := obj.imported.path
   499  					base := pkgName(path)
   500  					if obj.name == base {
   501  						check.softErrorf(obj.pos, "%q imported but not used", path)
   502  					} else {
   503  						check.softErrorf(obj.pos, "%q imported but not used as %s", path, obj.name)
   504  					}
   505  				}
   506  			}
   507  		}
   508  	}
   509  
   510  	// check use of dot-imported packages
   511  	for _, unusedDotImports := range check.unusedDotImports {
   512  		for pkg, pos := range unusedDotImports {
   513  			check.softErrorf(pos, "%q imported but not used", pkg.path)
   514  		}
   515  	}
   516  }
   517  
   518  // pkgName returns the package name (last element) of an import path.
   519  func pkgName(path string) string {
   520  	if i := strings.LastIndex(path, "/"); i >= 0 {
   521  		path = path[i+1:]
   522  	}
   523  	return path
   524  }
   525  
   526  // dir makes a good-faith attempt to return the directory
   527  // portion of path. If path is empty, the result is ".".
   528  // (Per the go/build package dependency tests, we cannot import
   529  // path/filepath and simply use filepath.Dir.)
   530  func dir(path string) string {
   531  	if i := strings.LastIndexAny(path, `/\`); i > 0 {
   532  		return path[:i]
   533  	}
   534  	// i <= 0
   535  	return "."
   536  }