1 // Copyright 2013 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.
7 // This file implements the CREATE phase of SSA construction.
8 // See builder.go for explanation.
18 "golang.org/x/tools/go/types/typeutil"
21 // NewProgram returns a new SSA Program.
23 // mode controls diagnostics and checking during SSA construction.
25 func NewProgram(fset *token.FileSet, mode BuilderMode) *Program {
28 imported: make(map[string]*Package),
29 packages: make(map[*types.Package]*Package),
30 thunks: make(map[selectionKey]*Function),
31 bounds: make(map[*types.Func]*Function),
35 h := typeutil.MakeHasher() // protected by methodsMu, in effect
36 prog.methodSets.SetHasher(h)
37 prog.canon.SetHasher(h)
42 // memberFromObject populates package pkg with a member for the
43 // typechecker object obj.
45 // For objects from Go source code, syntax is the associated syntax
46 // tree (for funcs and vars only); it will be used during the build
49 func memberFromObject(pkg *Package, obj types.Object, syntax ast.Node) {
51 switch obj := obj.(type) {
53 if pkg.Pkg != types.Unsafe {
54 panic("unexpected builtin object: " + obj.String())
58 pkg.Members[name] = &Type{
66 Value: NewConst(obj.Val(), obj.Type()),
69 pkg.values[obj] = c.Value
77 typ: types.NewPointer(obj.Type()), // address
84 sig := obj.Type().(*types.Signature)
85 if sig.Recv() == nil && name == "init" {
87 name = fmt.Sprintf("init#%d", pkg.ninit)
99 fn.Synthetic = "loaded from gc object file"
103 if sig.Recv() == nil {
104 pkg.Members[name] = fn // package-level function
107 default: // (incl. *types.Package)
108 panic("unexpected Object type: " + obj.String())
112 // membersFromDecl populates package pkg with members for each
113 // typechecker object (var, func, const or type) associated with the
116 func membersFromDecl(pkg *Package, decl ast.Decl) {
117 switch decl := decl.(type) {
118 case *ast.GenDecl: // import, const, type or var
121 for _, spec := range decl.Specs {
122 for _, id := range spec.(*ast.ValueSpec).Names {
123 if !isBlankIdent(id) {
124 memberFromObject(pkg, pkg.info.Defs[id], nil)
130 for _, spec := range decl.Specs {
131 for _, id := range spec.(*ast.ValueSpec).Names {
132 if !isBlankIdent(id) {
133 memberFromObject(pkg, pkg.info.Defs[id], spec)
139 for _, spec := range decl.Specs {
140 id := spec.(*ast.TypeSpec).Name
141 if !isBlankIdent(id) {
142 memberFromObject(pkg, pkg.info.Defs[id], nil)
149 if !isBlankIdent(id) {
150 memberFromObject(pkg, pkg.info.Defs[id], decl)
155 // CreatePackage constructs and returns an SSA Package from the
156 // specified type-checked, error-free file ASTs, and populates its
159 // importable determines whether this package should be returned by a
160 // subsequent call to ImportedPackage(pkg.Path()).
162 // The real work of building SSA form for each function is not done
163 // until a subsequent call to Package.Build().
165 func (prog *Program) CreatePackage(pkg *types.Package, files []*ast.File, info *types.Info, importable bool) *Package {
168 Members: make(map[string]Member),
169 values: make(map[types.Object]Value),
171 info: info, // transient (CREATE and BUILD phases)
172 files: files, // transient (CREATE and BUILD phases)
175 // Add init() function.
178 Signature: new(types.Signature),
179 Synthetic: "package initializer",
183 p.Members[p.init.name] = p.init
186 // Allocate all package members: vars, funcs, consts and types.
188 // Go source package.
189 for _, file := range files {
190 for _, decl := range file.Decls {
191 membersFromDecl(p, decl)
195 // GC-compiled binary package (or "unsafe")
197 // No position information.
198 scope := p.Pkg.Scope()
199 for _, name := range scope.Names() {
200 obj := scope.Lookup(name)
201 memberFromObject(p, obj, nil)
202 if obj, ok := obj.(*types.TypeName); ok {
203 if named, ok := obj.Type().(*types.Named); ok {
204 for i, n := 0, named.NumMethods(); i < n; i++ {
205 memberFromObject(p, named.Method(i), nil)
212 if prog.mode&BareInits == 0 {
213 // Add initializer guard variable.
214 initguard := &Global{
217 typ: types.NewPointer(tBool),
219 p.Members[initguard.Name()] = initguard
222 if prog.mode&GlobalDebug != 0 {
226 if prog.mode&PrintPackages != 0 {
233 prog.imported[p.Pkg.Path()] = p
235 prog.packages[p.Pkg] = p
240 // printMu serializes printing of Packages/Functions to stdout.
241 var printMu sync.Mutex
243 // AllPackages returns a new slice containing all packages in the
244 // program prog in unspecified order.
246 func (prog *Program) AllPackages() []*Package {
247 pkgs := make([]*Package, 0, len(prog.packages))
248 for _, pkg := range prog.packages {
249 pkgs = append(pkgs, pkg)
254 // ImportedPackage returns the importable Package whose PkgPath
255 // is path, or nil if no such Package has been created.
257 // A parameter to CreatePackage determines whether a package should be
258 // considered importable. For example, no import declaration can resolve
259 // to the ad-hoc main package created by 'go build foo.go'.
261 // TODO(adonovan): rethink this function and the "importable" concept;
262 // most packages are importable. This function assumes that all
263 // types.Package.Path values are unique within the ssa.Program, which is
264 // false---yet this function remains very convenient.
265 // Clients should use (*Program).Package instead where possible.
266 // SSA doesn't really need a string-keyed map of packages.
268 func (prog *Program) ImportedPackage(path string) *Package {
269 return prog.imported[path]