Beispiel #1
0
func typecheck(path string, fset *token.FileSet, whence string, sgoFiles ...*ast.File) (*types.Info, []error) {
	var errors []error
	imp, err := importer.DefaultFrom(sgoFiles, whence)
	if err != nil {
		return nil, []error{err}
	}
	cfg := &types.Config{
		Error: func(err error) {
			errors = append(errors, err)
		},
		Importer: imp,
	}
	info := &types.Info{
		Types:      map[ast.Expr]types.TypeAndValue{},
		Defs:       map[*ast.Ident]types.Object{},
		Uses:       map[*ast.Ident]types.Object{},
		Implicits:  map[ast.Node]types.Object{},
		Selections: map[*ast.SelectorExpr]*types.Selection{},
		Scopes:     map[ast.Node]*types.Scope{},
		InitOrder:  []*types.Initializer{},
	}
	_, err = cfg.Check(path, fset, sgoFiles, info)
	if err != nil {
		return nil, errors
	}
	return info, nil
}
Beispiel #2
0
func (imp *resolveTestImporter) ImportFrom(path, srcDir string, mode ImportMode) (*Package, error) {
	if mode != 0 {
		panic("mode must be 0")
	}
	if imp.importer == nil {
		impFrom, err := importer.DefaultFrom(imp.files, srcDir)
		if err != nil {
			return nil, err
		}
		imp.importer = impFrom.(ImporterFrom)
		imp.imported = make(map[string]bool)
	}
	pkg, err := imp.importer.ImportFrom(path, srcDir, mode)
	if err != nil {
		return nil, err
	}
	imp.imported[path] = true
	return pkg, nil
}