Example #1
0
// Read reads export data from in, decodes it, and returns type
// information for the package.
// The package name is specified by path.
// File position information is added to fset.
//
// Read may inspect and add to the imports map to ensure that references
// within the export data to other packages are consistent.  The caller
// must ensure that imports[path] does not exist, or exists but is
// incomplete (see types.Package.Complete), and Read inserts the
// resulting package into this map entry.
//
// On return, the state of the reader is undefined.
func Read(in io.Reader, fset *token.FileSet, imports map[string]*types.Package, path string) (*types.Package, error) {
	data, err := ioutil.ReadAll(in)
	if err != nil {
		return nil, fmt.Errorf("reading export data for %q: %v", path, err)
	}

	// The App Engine Go runtime v1.6 uses the old export data format.
	// TODO(adonovan): delete once v1.7 has been around for a while.
	if bytes.HasPrefix(data, []byte("package ")) {
		return gcimporter.ImportData(imports, path, path, bytes.NewReader(data))
	}

	_, pkg, err := gcimporter.BImportData(fset, imports, data, path)
	return pkg, err
}
Example #2
0
// Resolve resolves the package information for unit and its dependencies.  On
// success the package corresponding to unit is located via ImportPath in the
// Packages map of the returned value.
//
// If info != nil, it is used to populate the Info field of the return value
// and will contain the output of the type checker in each user-provided map
// field.
func Resolve(unit *apb.CompilationUnit, f Fetcher, info *types.Info) (*PackageInfo, error) {
	isSource := make(map[string]bool)
	for _, src := range unit.SourceFile {
		isSource[src] = true
	}

	deps := make(map[string]*types.Package) // import path → package
	imap := make(map[string]*spb.VName)     // import path → vname
	srcs := make(map[string]string)         // file path → text
	fset := token.NewFileSet()              // location info for the parser
	var files []*ast.File                   // parsed sources

	// Classify the required inputs as either sources, which are to be parsed,
	// or dependencies, which are to be "imported" via the type-checker's
	// import mechanism.  If successful, this populates fset and files with the
	// lexical and syntactic content of the package's own sources.
	for _, ri := range unit.RequiredInput {
		if ri.Info == nil {
			return nil, errors.New("required input file info missing")
		}

		// Fetch the contents of each required input.
		fpath := ri.Info.Path
		data, err := f.Fetch(fpath, ri.Info.Digest)
		if err != nil {
			return nil, fmt.Errorf("fetching %q (%s): %v", fpath, ri.Info.Digest, err)
		}

		// Source inputs need to be parsed, so we can give their ASTs to the
		// type checker later on.
		if isSource[fpath] {
			srcs[fpath] = string(data)
			parsed, err := parser.ParseFile(fset, fpath, data, parser.AllErrors)
			if err != nil {
				return nil, fmt.Errorf("parsing %q: %v", fpath, err)
			}
			files = append(files, parsed)
			continue
		}

		// For archives, recover the import path from the VName and read the
		// archive header for type information.  If the VName is not set, the
		// import is considered bogus.
		if ri.VName == nil {
			return nil, fmt.Errorf("missing vname for %q", fpath)
		}
		hdr := bufio.NewReader(bytes.NewReader(data))
		if _, err := gcimporter.FindExportData(hdr); err != nil {
			return nil, fmt.Errorf("scanning export data in %q: %v", fpath, err)
		}

		ipath := path.Join(ri.VName.Corpus, ri.VName.Path)
		if govname.IsStandardLibrary(ri.VName) {
			ipath = ri.VName.Path
		}
		imap[ipath] = ri.VName

		if _, err := gcimporter.ImportData(deps, fpath, ipath, hdr); err != nil {
			return nil, fmt.Errorf("importing %q: %v", ipath, err)
		}
	}

	// Fill in the mapping from packages to vnames.
	vmap := make(map[*types.Package]*spb.VName)
	for ip, vname := range imap {
		if pkg := deps[ip]; pkg != nil {
			vmap[pkg] = vname
		}
	}
	pi := &PackageInfo{
		Name:         files[0].Name.Name,
		ImportPath:   path.Join(unit.VName.Corpus, unit.VName.Path),
		VNames:       vmap,
		FileSet:      fset,
		Files:        files,
		SourceText:   srcs,
		Dependencies: deps,
		Info:         info,

		sigs: make(map[types.Object]string),
	}

	// Run the type-checker and collect any errors it generates.  Errors in the
	// type checker are not returned directly; the caller can read them from
	// the Errors field.
	c := &types.Config{
		FakeImportC:              true, // so we can handle cgo
		DisableUnusedImportCheck: true, // this is not fatal to type-checking
		Importer:                 pi,
		Error: func(err error) {
			pi.Errors = append(pi.Errors, err)
		},
	}
	if pkg, _ := c.Check(pi.Name, pi.FileSet, pi.Files, pi.Info); pkg != nil {
		pi.Package = pkg
		vmap[pkg] = unit.VName
	}
	return pi, nil
}