github.com/epfl-dcsl/gotee@v0.0.0-20200909122901-014b35f5e5e9/src/go/internal/srcimporter/srcimporter.go (about)

     1  // Copyright 2017 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 srcimporter implements importing directly
     6  // from source files rather than installed packages.
     7  package srcimporter // import "go/internal/srcimporter"
     8  
     9  import (
    10  	"fmt"
    11  	"go/ast"
    12  	"go/build"
    13  	"go/parser"
    14  	"go/token"
    15  	"go/types"
    16  	"path/filepath"
    17  	"sync"
    18  )
    19  
    20  // An Importer provides the context for importing packages from source code.
    21  type Importer struct {
    22  	ctxt     *build.Context
    23  	fset     *token.FileSet
    24  	sizes    types.Sizes
    25  	packages map[string]*types.Package
    26  }
    27  
    28  // NewImporter returns a new Importer for the given context, file set, and map
    29  // of packages. The context is used to resolve import paths to package paths,
    30  // and identifying the files belonging to the package. If the context provides
    31  // non-nil file system functions, they are used instead of the regular package
    32  // os functions. The file set is used to track position information of package
    33  // files; and imported packages are added to the packages map.
    34  func New(ctxt *build.Context, fset *token.FileSet, packages map[string]*types.Package) *Importer {
    35  	return &Importer{
    36  		ctxt:     ctxt,
    37  		fset:     fset,
    38  		sizes:    types.SizesFor(ctxt.Compiler, ctxt.GOARCH), // uses go/types default if GOARCH not found
    39  		packages: packages,
    40  	}
    41  }
    42  
    43  // Importing is a sentinel taking the place in Importer.packages
    44  // for a package that is in the process of being imported.
    45  var importing types.Package
    46  
    47  // Import(path) is a shortcut for ImportFrom(path, ".", 0).
    48  func (p *Importer) Import(path string) (*types.Package, error) {
    49  	return p.ImportFrom(path, ".", 0) // use "." rather than "" (see issue #24441)
    50  }
    51  
    52  // ImportFrom imports the package with the given import path resolved from the given srcDir,
    53  // adds the new package to the set of packages maintained by the importer, and returns the
    54  // package. Package path resolution and file system operations are controlled by the context
    55  // maintained with the importer. The import mode must be zero but is otherwise ignored.
    56  // Packages that are not comprised entirely of pure Go files may fail to import because the
    57  // type checker may not be able to determine all exported entities (e.g. due to cgo dependencies).
    58  func (p *Importer) ImportFrom(path, srcDir string, mode types.ImportMode) (*types.Package, error) {
    59  	if mode != 0 {
    60  		panic("non-zero import mode")
    61  	}
    62  
    63  	if abs, err := p.absPath(srcDir); err == nil { // see issue #14282
    64  		srcDir = abs
    65  	}
    66  	bp, err := p.ctxt.Import(path, srcDir, 0)
    67  	if err != nil {
    68  		return nil, err // err may be *build.NoGoError - return as is
    69  	}
    70  
    71  	// package unsafe is known to the type checker
    72  	if bp.ImportPath == "unsafe" {
    73  		return types.Unsafe, nil
    74  	}
    75  
    76  	// no need to re-import if the package was imported completely before
    77  	pkg := p.packages[bp.ImportPath]
    78  	if pkg != nil {
    79  		if pkg == &importing {
    80  			return nil, fmt.Errorf("import cycle through package %q", bp.ImportPath)
    81  		}
    82  		if !pkg.Complete() {
    83  			// Package exists but is not complete - we cannot handle this
    84  			// at the moment since the source importer replaces the package
    85  			// wholesale rather than augmenting it (see #19337 for details).
    86  			// Return incomplete package with error (see #16088).
    87  			return pkg, fmt.Errorf("reimported partially imported package %q", bp.ImportPath)
    88  		}
    89  		return pkg, nil
    90  	}
    91  
    92  	p.packages[bp.ImportPath] = &importing
    93  	defer func() {
    94  		// clean up in case of error
    95  		// TODO(gri) Eventually we may want to leave a (possibly empty)
    96  		// package in the map in all cases (and use that package to
    97  		// identify cycles). See also issue 16088.
    98  		if p.packages[bp.ImportPath] == &importing {
    99  			p.packages[bp.ImportPath] = nil
   100  		}
   101  	}()
   102  
   103  	var filenames []string
   104  	filenames = append(filenames, bp.GoFiles...)
   105  	filenames = append(filenames, bp.CgoFiles...)
   106  
   107  	files, err := p.parseFiles(bp.Dir, filenames)
   108  	if err != nil {
   109  		return nil, err
   110  	}
   111  
   112  	// type-check package files
   113  	var firstHardErr error
   114  	conf := types.Config{
   115  		IgnoreFuncBodies: true,
   116  		FakeImportC:      true,
   117  		// continue type-checking after the first error
   118  		Error: func(err error) {
   119  			if firstHardErr == nil && !err.(types.Error).Soft {
   120  				firstHardErr = err
   121  			}
   122  		},
   123  		Importer: p,
   124  		Sizes:    p.sizes,
   125  	}
   126  	pkg, err = conf.Check(bp.ImportPath, p.fset, files, nil)
   127  	if err != nil {
   128  		// If there was a hard error it is possibly unsafe
   129  		// to use the package as it may not be fully populated.
   130  		// Do not return it (see also #20837, #20855).
   131  		if firstHardErr != nil {
   132  			pkg = nil
   133  			err = firstHardErr // give preference to first hard error over any soft error
   134  		}
   135  		return pkg, fmt.Errorf("type-checking package %q failed (%v)", bp.ImportPath, err)
   136  	}
   137  	if firstHardErr != nil {
   138  		// this can only happen if we have a bug in go/types
   139  		panic("package is not safe yet no error was returned")
   140  	}
   141  
   142  	p.packages[bp.ImportPath] = pkg
   143  	return pkg, nil
   144  }
   145  
   146  func (p *Importer) parseFiles(dir string, filenames []string) ([]*ast.File, error) {
   147  	open := p.ctxt.OpenFile // possibly nil
   148  
   149  	files := make([]*ast.File, len(filenames))
   150  	errors := make([]error, len(filenames))
   151  
   152  	var wg sync.WaitGroup
   153  	wg.Add(len(filenames))
   154  	for i, filename := range filenames {
   155  		go func(i int, filepath string) {
   156  			defer wg.Done()
   157  			if open != nil {
   158  				src, err := open(filepath)
   159  				if err != nil {
   160  					errors[i] = fmt.Errorf("opening package file %s failed (%v)", filepath, err)
   161  					return
   162  				}
   163  				files[i], errors[i] = parser.ParseFile(p.fset, filepath, src, 0)
   164  				src.Close() // ignore Close error - parsing may have succeeded which is all we need
   165  			} else {
   166  				// Special-case when ctxt doesn't provide a custom OpenFile and use the
   167  				// parser's file reading mechanism directly. This appears to be quite a
   168  				// bit faster than opening the file and providing an io.ReaderCloser in
   169  				// both cases.
   170  				// TODO(gri) investigate performance difference (issue #19281)
   171  				files[i], errors[i] = parser.ParseFile(p.fset, filepath, nil, 0)
   172  			}
   173  		}(i, p.joinPath(dir, filename))
   174  	}
   175  	wg.Wait()
   176  
   177  	// if there are errors, return the first one for deterministic results
   178  	for _, err := range errors {
   179  		if err != nil {
   180  			return nil, err
   181  		}
   182  	}
   183  
   184  	return files, nil
   185  }
   186  
   187  // context-controlled file system operations
   188  
   189  func (p *Importer) absPath(path string) (string, error) {
   190  	// TODO(gri) This should be using p.ctxt.AbsPath which doesn't
   191  	// exist but probably should. See also issue #14282.
   192  	return filepath.Abs(path)
   193  }
   194  
   195  func (p *Importer) isAbsPath(path string) bool {
   196  	if f := p.ctxt.IsAbsPath; f != nil {
   197  		return f(path)
   198  	}
   199  	return filepath.IsAbs(path)
   200  }
   201  
   202  func (p *Importer) joinPath(elem ...string) string {
   203  	if f := p.ctxt.JoinPath; f != nil {
   204  		return f(elem...)
   205  	}
   206  	return filepath.Join(elem...)
   207  }