gopkg.in/alecthomas/gometalinter.v3@v3.0.0/_linters/src/golang.org/x/tools/go/ssa/ssautil/load.go (about)

     1  // Copyright 2015 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 ssautil
     6  
     7  // This file defines utility functions for constructing programs in SSA form.
     8  
     9  import (
    10  	"go/ast"
    11  	"go/token"
    12  	"go/types"
    13  
    14  	"golang.org/x/tools/go/loader"
    15  	"golang.org/x/tools/go/ssa"
    16  )
    17  
    18  // CreateProgram returns a new program in SSA form, given a program
    19  // loaded from source.  An SSA package is created for each transitively
    20  // error-free package of lprog.
    21  //
    22  // Code for bodies of functions is not built until Build is called
    23  // on the result.
    24  //
    25  // mode controls diagnostics and checking during SSA construction.
    26  //
    27  func CreateProgram(lprog *loader.Program, mode ssa.BuilderMode) *ssa.Program {
    28  	prog := ssa.NewProgram(lprog.Fset, mode)
    29  
    30  	for _, info := range lprog.AllPackages {
    31  		if info.TransitivelyErrorFree {
    32  			prog.CreatePackage(info.Pkg, info.Files, &info.Info, info.Importable)
    33  		}
    34  	}
    35  
    36  	return prog
    37  }
    38  
    39  // BuildPackage builds an SSA program with IR for a single package.
    40  //
    41  // It populates pkg by type-checking the specified file ASTs.  All
    42  // dependencies are loaded using the importer specified by tc, which
    43  // typically loads compiler export data; SSA code cannot be built for
    44  // those packages.  BuildPackage then constructs an ssa.Program with all
    45  // dependency packages created, and builds and returns the SSA package
    46  // corresponding to pkg.
    47  //
    48  // The caller must have set pkg.Path() to the import path.
    49  //
    50  // The operation fails if there were any type-checking or import errors.
    51  //
    52  // See ../ssa/example_test.go for an example.
    53  //
    54  func BuildPackage(tc *types.Config, fset *token.FileSet, pkg *types.Package, files []*ast.File, mode ssa.BuilderMode) (*ssa.Package, *types.Info, error) {
    55  	if fset == nil {
    56  		panic("no token.FileSet")
    57  	}
    58  	if pkg.Path() == "" {
    59  		panic("package has no import path")
    60  	}
    61  
    62  	info := &types.Info{
    63  		Types:      make(map[ast.Expr]types.TypeAndValue),
    64  		Defs:       make(map[*ast.Ident]types.Object),
    65  		Uses:       make(map[*ast.Ident]types.Object),
    66  		Implicits:  make(map[ast.Node]types.Object),
    67  		Scopes:     make(map[ast.Node]*types.Scope),
    68  		Selections: make(map[*ast.SelectorExpr]*types.Selection),
    69  	}
    70  	if err := types.NewChecker(tc, fset, pkg, info).Files(files); err != nil {
    71  		return nil, nil, err
    72  	}
    73  
    74  	prog := ssa.NewProgram(fset, mode)
    75  
    76  	// Create SSA packages for all imports.
    77  	// Order is not significant.
    78  	created := make(map[*types.Package]bool)
    79  	var createAll func(pkgs []*types.Package)
    80  	createAll = func(pkgs []*types.Package) {
    81  		for _, p := range pkgs {
    82  			if !created[p] {
    83  				created[p] = true
    84  				prog.CreatePackage(p, nil, nil, true)
    85  				createAll(p.Imports())
    86  			}
    87  		}
    88  	}
    89  	createAll(pkg.Imports())
    90  
    91  	// Create and build the primary package.
    92  	ssapkg := prog.CreatePackage(pkg, files, info, false)
    93  	ssapkg.Build()
    94  	return ssapkg, info, nil
    95  }