CreatePackage constructs and returns an SSA Package from the 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()). The real work of building SSA form for ea
(pkg *Package, files []*File, info *Info, importable bool)
| 90 | // until a subsequent call to Package.Build(). |
| 91 | // |
| 92 | func (prog *Program) CreatePackage(pkg *Package, files []*File, info *Info, importable bool) *SSAPackage { |
| 93 | p := &SSAPackage{ |
| 94 | Pkg: pkg, |
| 95 | Members: make(map[string]Member), |
| 96 | values: make(map[Object]Value), |
| 97 | info: info, // transient (CREATE and BUILD phases) |
| 98 | } |
| 99 | |
| 100 | // Add init() function. |
| 101 | obj := NewFunc(NoPos, pkg, "init", new(Signature)) |
| 102 | fn := &Function{ |
| 103 | name: obj.Name(), |
| 104 | object: obj, |
| 105 | Signature: obj.Type().(*Signature), |
| 106 | Synthetic: "package initializer", |
| 107 | Pkg: p, |
| 108 | } |
| 109 | p.InitFunc = fn |
| 110 | p.Members[fn.name] = fn |
| 111 | |
| 112 | // CREATE phase. |
| 113 | // Allocate all package members: vars, funcs, consts and types. |
| 114 | |
| 115 | for _, init := range p.Pkg.inits { |
| 116 | memberFromFunc(p, init) |
| 117 | } |
| 118 | |
| 119 | scope := p.Pkg.Scope() |
| 120 | for _, name := range scope.Names() { |
| 121 | switch obj := scope.Lookup(name).(type) { |
| 122 | case *Func: |
| 123 | memberFromFunc(p, obj) |
| 124 | case *TypeName: |
| 125 | if !obj.IsAlias() { |
| 126 | if named, ok := obj.Type().(*Named); ok { |
| 127 | for i, n := 0, named.NumMethods(); i < n; i++ { |
| 128 | memberFromFunc(p, named.Method(i)) |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | case *Var: |
| 133 | memberFromVar(p, obj) |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | if prog.mode&BareInits == 0 { |
| 138 | // Add initializer guard variable. |
| 139 | initGuard := NewVar(NoPos, pkg, "init$guard", tBool) |
| 140 | p.InitGuard = memberFromVar(p, initGuard) |
| 141 | } |
| 142 | |
| 143 | if prog.mode&PrintPackages != 0 { |
| 144 | printMu.Lock() |
| 145 | p.WriteTo(os.Stdout) |
| 146 | printMu.Unlock() |
| 147 | } |
| 148 | |
| 149 | if importable { |
no test coverage detected