2019-11-07 19:05:39 +00:00
|
|
|
// Copyright 2013 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2020-02-26 18:46:17 +00:00
|
|
|
package ir
|
2019-11-07 19:05:39 +00:00
|
|
|
|
2020-02-26 18:46:17 +00:00
|
|
|
// This file implements the CREATE phase of IR construction.
|
2019-11-07 19:05:39 +00:00
|
|
|
// See builder.go for explanation.
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"go/ast"
|
|
|
|
"go/token"
|
|
|
|
"go/types"
|
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"golang.org/x/tools/go/types/typeutil"
|
|
|
|
)
|
|
|
|
|
2020-02-26 18:46:17 +00:00
|
|
|
// NewProgram returns a new IR Program.
|
2019-11-07 19:05:39 +00:00
|
|
|
//
|
2020-02-26 18:46:17 +00:00
|
|
|
// mode controls diagnostics and checking during IR construction.
|
2019-11-07 19:05:39 +00:00
|
|
|
//
|
|
|
|
func NewProgram(fset *token.FileSet, mode BuilderMode) *Program {
|
|
|
|
prog := &Program{
|
|
|
|
Fset: fset,
|
|
|
|
imported: make(map[string]*Package),
|
|
|
|
packages: make(map[*types.Package]*Package),
|
|
|
|
thunks: make(map[selectionKey]*Function),
|
|
|
|
bounds: make(map[*types.Func]*Function),
|
|
|
|
mode: mode,
|
|
|
|
}
|
|
|
|
|
|
|
|
h := typeutil.MakeHasher() // protected by methodsMu, in effect
|
|
|
|
prog.methodSets.SetHasher(h)
|
|
|
|
prog.canon.SetHasher(h)
|
|
|
|
|
|
|
|
return prog
|
|
|
|
}
|
|
|
|
|
|
|
|
// memberFromObject populates package pkg with a member for the
|
|
|
|
// typechecker object obj.
|
|
|
|
//
|
|
|
|
// For objects from Go source code, syntax is the associated syntax
|
|
|
|
// tree (for funcs and vars only); it will be used during the build
|
|
|
|
// phase.
|
|
|
|
//
|
|
|
|
func memberFromObject(pkg *Package, obj types.Object, syntax ast.Node) {
|
|
|
|
name := obj.Name()
|
|
|
|
switch obj := obj.(type) {
|
|
|
|
case *types.Builtin:
|
|
|
|
if pkg.Pkg != types.Unsafe {
|
|
|
|
panic("unexpected builtin object: " + obj.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
case *types.TypeName:
|
|
|
|
pkg.Members[name] = &Type{
|
|
|
|
object: obj,
|
|
|
|
pkg: pkg,
|
|
|
|
}
|
|
|
|
|
|
|
|
case *types.Const:
|
|
|
|
c := &NamedConst{
|
|
|
|
object: obj,
|
|
|
|
Value: NewConst(obj.Val(), obj.Type()),
|
|
|
|
pkg: pkg,
|
|
|
|
}
|
|
|
|
pkg.values[obj] = c.Value
|
|
|
|
pkg.Members[name] = c
|
|
|
|
|
|
|
|
case *types.Var:
|
|
|
|
g := &Global{
|
|
|
|
Pkg: pkg,
|
|
|
|
name: name,
|
|
|
|
object: obj,
|
|
|
|
typ: types.NewPointer(obj.Type()), // address
|
|
|
|
}
|
|
|
|
pkg.values[obj] = g
|
|
|
|
pkg.Members[name] = g
|
|
|
|
|
|
|
|
case *types.Func:
|
|
|
|
sig := obj.Type().(*types.Signature)
|
|
|
|
if sig.Recv() == nil && name == "init" {
|
|
|
|
pkg.ninit++
|
|
|
|
name = fmt.Sprintf("init#%d", pkg.ninit)
|
|
|
|
}
|
|
|
|
fn := &Function{
|
|
|
|
name: name,
|
|
|
|
object: obj,
|
|
|
|
Signature: sig,
|
|
|
|
Pkg: pkg,
|
|
|
|
Prog: pkg.Prog,
|
|
|
|
}
|
2020-02-26 18:46:17 +00:00
|
|
|
|
|
|
|
fn.source = syntax
|
|
|
|
fn.initHTML(pkg.printFunc)
|
2019-11-07 19:05:39 +00:00
|
|
|
if syntax == nil {
|
|
|
|
fn.Synthetic = "loaded from gc object file"
|
2020-02-26 18:46:17 +00:00
|
|
|
} else {
|
|
|
|
fn.functionBody = new(functionBody)
|
2019-11-07 19:05:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pkg.values[obj] = fn
|
2020-02-26 18:46:17 +00:00
|
|
|
pkg.Functions = append(pkg.Functions, fn)
|
2019-11-07 19:05:39 +00:00
|
|
|
if sig.Recv() == nil {
|
|
|
|
pkg.Members[name] = fn // package-level function
|
|
|
|
}
|
|
|
|
|
|
|
|
default: // (incl. *types.Package)
|
|
|
|
panic("unexpected Object type: " + obj.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// membersFromDecl populates package pkg with members for each
|
|
|
|
// typechecker object (var, func, const or type) associated with the
|
|
|
|
// specified decl.
|
|
|
|
//
|
|
|
|
func membersFromDecl(pkg *Package, decl ast.Decl) {
|
|
|
|
switch decl := decl.(type) {
|
|
|
|
case *ast.GenDecl: // import, const, type or var
|
|
|
|
switch decl.Tok {
|
|
|
|
case token.CONST:
|
|
|
|
for _, spec := range decl.Specs {
|
|
|
|
for _, id := range spec.(*ast.ValueSpec).Names {
|
|
|
|
if !isBlankIdent(id) {
|
|
|
|
memberFromObject(pkg, pkg.info.Defs[id], nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case token.VAR:
|
|
|
|
for _, spec := range decl.Specs {
|
|
|
|
for _, id := range spec.(*ast.ValueSpec).Names {
|
|
|
|
if !isBlankIdent(id) {
|
|
|
|
memberFromObject(pkg, pkg.info.Defs[id], spec)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case token.TYPE:
|
|
|
|
for _, spec := range decl.Specs {
|
|
|
|
id := spec.(*ast.TypeSpec).Name
|
|
|
|
if !isBlankIdent(id) {
|
|
|
|
memberFromObject(pkg, pkg.info.Defs[id], nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case *ast.FuncDecl:
|
|
|
|
id := decl.Name
|
|
|
|
if !isBlankIdent(id) {
|
|
|
|
memberFromObject(pkg, pkg.info.Defs[id], decl)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-26 18:46:17 +00:00
|
|
|
// CreatePackage constructs and returns an IR Package from the
|
2019-11-07 19:05:39 +00:00
|
|
|
// specified type-checked, error-free file ASTs, and populates its
|
|
|
|
// Members mapping.
|
|
|
|
//
|
|
|
|
// importable determines whether this package should be returned by a
|
|
|
|
// subsequent call to ImportedPackage(pkg.Path()).
|
|
|
|
//
|
2020-02-26 18:46:17 +00:00
|
|
|
// The real work of building IR form for each function is not done
|
2019-11-07 19:05:39 +00:00
|
|
|
// until a subsequent call to Package.Build().
|
|
|
|
//
|
|
|
|
func (prog *Program) CreatePackage(pkg *types.Package, files []*ast.File, info *types.Info, importable bool) *Package {
|
|
|
|
p := &Package{
|
2020-02-26 18:46:17 +00:00
|
|
|
Prog: prog,
|
|
|
|
Members: make(map[string]Member),
|
|
|
|
values: make(map[types.Object]Value),
|
|
|
|
Pkg: pkg,
|
|
|
|
info: info, // transient (CREATE and BUILD phases)
|
|
|
|
files: files, // transient (CREATE and BUILD phases)
|
|
|
|
printFunc: prog.PrintFunc,
|
2019-11-07 19:05:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add init() function.
|
|
|
|
p.init = &Function{
|
2020-02-26 18:46:17 +00:00
|
|
|
name: "init",
|
|
|
|
Signature: new(types.Signature),
|
|
|
|
Synthetic: "package initializer",
|
|
|
|
Pkg: p,
|
|
|
|
Prog: prog,
|
|
|
|
functionBody: new(functionBody),
|
2019-11-07 19:05:39 +00:00
|
|
|
}
|
2020-02-26 18:46:17 +00:00
|
|
|
p.init.initHTML(prog.PrintFunc)
|
2019-11-07 19:05:39 +00:00
|
|
|
p.Members[p.init.name] = p.init
|
2020-02-26 18:46:17 +00:00
|
|
|
p.Functions = append(p.Functions, p.init)
|
2019-11-07 19:05:39 +00:00
|
|
|
|
|
|
|
// CREATE phase.
|
|
|
|
// Allocate all package members: vars, funcs, consts and types.
|
|
|
|
if len(files) > 0 {
|
|
|
|
// Go source package.
|
|
|
|
for _, file := range files {
|
|
|
|
for _, decl := range file.Decls {
|
|
|
|
membersFromDecl(p, decl)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// GC-compiled binary package (or "unsafe")
|
|
|
|
// No code.
|
|
|
|
// No position information.
|
|
|
|
scope := p.Pkg.Scope()
|
|
|
|
for _, name := range scope.Names() {
|
|
|
|
obj := scope.Lookup(name)
|
|
|
|
memberFromObject(p, obj, nil)
|
|
|
|
if obj, ok := obj.(*types.TypeName); ok {
|
|
|
|
if named, ok := obj.Type().(*types.Named); ok {
|
|
|
|
for i, n := 0, named.NumMethods(); i < n; i++ {
|
|
|
|
memberFromObject(p, named.Method(i), nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-26 18:46:17 +00:00
|
|
|
// Add initializer guard variable.
|
|
|
|
initguard := &Global{
|
|
|
|
Pkg: p,
|
|
|
|
name: "init$guard",
|
|
|
|
typ: types.NewPointer(tBool),
|
2019-11-07 19:05:39 +00:00
|
|
|
}
|
2020-02-26 18:46:17 +00:00
|
|
|
p.Members[initguard.Name()] = initguard
|
2019-11-07 19:05:39 +00:00
|
|
|
|
|
|
|
if prog.mode&GlobalDebug != 0 {
|
|
|
|
p.SetDebugMode(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
if prog.mode&PrintPackages != 0 {
|
|
|
|
printMu.Lock()
|
|
|
|
p.WriteTo(os.Stdout)
|
|
|
|
printMu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
if importable {
|
|
|
|
prog.imported[p.Pkg.Path()] = p
|
|
|
|
}
|
|
|
|
prog.packages[p.Pkg] = p
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
// printMu serializes printing of Packages/Functions to stdout.
|
|
|
|
var printMu sync.Mutex
|
|
|
|
|
|
|
|
// AllPackages returns a new slice containing all packages in the
|
|
|
|
// program prog in unspecified order.
|
|
|
|
//
|
|
|
|
func (prog *Program) AllPackages() []*Package {
|
|
|
|
pkgs := make([]*Package, 0, len(prog.packages))
|
|
|
|
for _, pkg := range prog.packages {
|
|
|
|
pkgs = append(pkgs, pkg)
|
|
|
|
}
|
|
|
|
return pkgs
|
|
|
|
}
|
|
|
|
|
|
|
|
// ImportedPackage returns the importable Package whose PkgPath
|
|
|
|
// is path, or nil if no such Package has been created.
|
|
|
|
//
|
|
|
|
// A parameter to CreatePackage determines whether a package should be
|
|
|
|
// considered importable. For example, no import declaration can resolve
|
|
|
|
// to the ad-hoc main package created by 'go build foo.go'.
|
|
|
|
//
|
|
|
|
// TODO(adonovan): rethink this function and the "importable" concept;
|
|
|
|
// most packages are importable. This function assumes that all
|
2020-02-26 18:46:17 +00:00
|
|
|
// types.Package.Path values are unique within the ir.Program, which is
|
2019-11-07 19:05:39 +00:00
|
|
|
// false---yet this function remains very convenient.
|
|
|
|
// Clients should use (*Program).Package instead where possible.
|
2020-02-26 18:46:17 +00:00
|
|
|
// IR doesn't really need a string-keyed map of packages.
|
2019-11-07 19:05:39 +00:00
|
|
|
//
|
|
|
|
func (prog *Program) ImportedPackage(path string) *Package {
|
|
|
|
return prog.imported[path]
|
|
|
|
}
|