github.com/powerman/golang-tools@v0.1.11-0.20220410185822-5ad214d8d803/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 "github.com/powerman/golang-tools/go/loader" 15 "github.com/powerman/golang-tools/go/packages" 16 "github.com/powerman/golang-tools/go/ssa" 17 "github.com/powerman/golang-tools/internal/typeparams" 18 ) 19 20 // Packages creates an SSA program for a set of packages. 21 // 22 // The packages must have been loaded from source syntax using the 23 // github.com/powerman/golang-tools/go/packages.Load function in LoadSyntax or 24 // LoadAllSyntax mode. 25 // 26 // Packages creates an SSA package for each well-typed package in the 27 // initial list, plus all their dependencies. The resulting list of 28 // packages corresponds to the list of initial packages, and may contain 29 // a nil if SSA code could not be constructed for the corresponding initial 30 // package due to type errors. 31 // 32 // Code for bodies of functions is not built until Build is called on 33 // the resulting Program. SSA code is constructed only for the initial 34 // packages with well-typed syntax trees. 35 // 36 // The mode parameter controls diagnostics and checking during SSA construction. 37 // 38 func Packages(initial []*packages.Package, mode ssa.BuilderMode) (*ssa.Program, []*ssa.Package) { 39 return doPackages(initial, mode, false) 40 } 41 42 // AllPackages creates an SSA program for a set of packages plus all 43 // their dependencies. 44 // 45 // The packages must have been loaded from source syntax using the 46 // github.com/powerman/golang-tools/go/packages.Load function in LoadAllSyntax mode. 47 // 48 // AllPackages creates an SSA package for each well-typed package in the 49 // initial list, plus all their dependencies. The resulting list of 50 // packages corresponds to the list of initial packages, and may contain 51 // a nil if SSA code could not be constructed for the corresponding 52 // initial package due to type errors. 53 // 54 // Code for bodies of functions is not built until Build is called on 55 // the resulting Program. SSA code is constructed for all packages with 56 // well-typed syntax trees. 57 // 58 // The mode parameter controls diagnostics and checking during SSA construction. 59 // 60 func AllPackages(initial []*packages.Package, mode ssa.BuilderMode) (*ssa.Program, []*ssa.Package) { 61 return doPackages(initial, mode, true) 62 } 63 64 func doPackages(initial []*packages.Package, mode ssa.BuilderMode, deps bool) (*ssa.Program, []*ssa.Package) { 65 66 var fset *token.FileSet 67 if len(initial) > 0 { 68 fset = initial[0].Fset 69 } 70 71 prog := ssa.NewProgram(fset, mode) 72 73 isInitial := make(map[*packages.Package]bool, len(initial)) 74 for _, p := range initial { 75 isInitial[p] = true 76 } 77 78 ssamap := make(map[*packages.Package]*ssa.Package) 79 packages.Visit(initial, nil, func(p *packages.Package) { 80 if p.Types != nil && !p.IllTyped { 81 var files []*ast.File 82 if deps || isInitial[p] { 83 files = p.Syntax 84 } 85 ssamap[p] = prog.CreatePackage(p.Types, files, p.TypesInfo, true) 86 } 87 }) 88 89 var ssapkgs []*ssa.Package 90 for _, p := range initial { 91 ssapkgs = append(ssapkgs, ssamap[p]) // may be nil 92 } 93 return prog, ssapkgs 94 } 95 96 // CreateProgram returns a new program in SSA form, given a program 97 // loaded from source. An SSA package is created for each transitively 98 // error-free package of lprog. 99 // 100 // Code for bodies of functions is not built until Build is called 101 // on the result. 102 // 103 // The mode parameter controls diagnostics and checking during SSA construction. 104 // 105 // Deprecated: Use github.com/powerman/golang-tools/go/packages and the Packages 106 // function instead; see ssa.Example_loadPackages. 107 // 108 func CreateProgram(lprog *loader.Program, mode ssa.BuilderMode) *ssa.Program { 109 prog := ssa.NewProgram(lprog.Fset, mode) 110 111 for _, info := range lprog.AllPackages { 112 if info.TransitivelyErrorFree { 113 prog.CreatePackage(info.Pkg, info.Files, &info.Info, info.Importable) 114 } 115 } 116 117 return prog 118 } 119 120 // BuildPackage builds an SSA program with IR for a single package. 121 // 122 // It populates pkg by type-checking the specified file ASTs. All 123 // dependencies are loaded using the importer specified by tc, which 124 // typically loads compiler export data; SSA code cannot be built for 125 // those packages. BuildPackage then constructs an ssa.Program with all 126 // dependency packages created, and builds and returns the SSA package 127 // corresponding to pkg. 128 // 129 // The caller must have set pkg.Path() to the import path. 130 // 131 // The operation fails if there were any type-checking or import errors. 132 // 133 // See ../example_test.go for an example. 134 // 135 func BuildPackage(tc *types.Config, fset *token.FileSet, pkg *types.Package, files []*ast.File, mode ssa.BuilderMode) (*ssa.Package, *types.Info, error) { 136 if fset == nil { 137 panic("no token.FileSet") 138 } 139 if pkg.Path() == "" { 140 panic("package has no import path") 141 } 142 143 info := &types.Info{ 144 Types: make(map[ast.Expr]types.TypeAndValue), 145 Defs: make(map[*ast.Ident]types.Object), 146 Uses: make(map[*ast.Ident]types.Object), 147 Implicits: make(map[ast.Node]types.Object), 148 Scopes: make(map[ast.Node]*types.Scope), 149 Selections: make(map[*ast.SelectorExpr]*types.Selection), 150 } 151 typeparams.InitInstanceInfo(info) 152 if err := types.NewChecker(tc, fset, pkg, info).Files(files); err != nil { 153 return nil, nil, err 154 } 155 156 prog := ssa.NewProgram(fset, mode) 157 158 // Create SSA packages for all imports. 159 // Order is not significant. 160 created := make(map[*types.Package]bool) 161 var createAll func(pkgs []*types.Package) 162 createAll = func(pkgs []*types.Package) { 163 for _, p := range pkgs { 164 if !created[p] { 165 created[p] = true 166 prog.CreatePackage(p, nil, nil, true) 167 createAll(p.Imports()) 168 } 169 } 170 } 171 createAll(pkg.Imports()) 172 173 // Create and build the primary package. 174 ssapkg := prog.CreatePackage(pkg, files, info, false) 175 ssapkg.Build() 176 return ssapkg, info, nil 177 }