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