Commit 39e22f04 authored by Robert Griesemer's avatar Robert Griesemer

cmd/compile: pass in importpkg to importer rather than rely on global

First step towards removing global var importpkg.

Change-Id: Ifdda7c295e5720a7ff2da9baea17f03f190d48fa
Reviewed-on: https://go-review.googlesource.com/38594
Run-TryBot: Robert Griesemer <gri@golang.org>
Reviewed-by: 's avatarMatthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 39c8d2b7
...@@ -25,6 +25,7 @@ import ( ...@@ -25,6 +25,7 @@ import (
type importer struct { type importer struct {
in *bufio.Reader in *bufio.Reader
imp *Pkg // imported package
buf []byte // reused for reading strings buf []byte // reused for reading strings
version int // export format version version int // export format version
...@@ -49,10 +50,11 @@ type importer struct { ...@@ -49,10 +50,11 @@ type importer struct {
read int // bytes read read int // bytes read
} }
// Import populates importpkg from the serialized package data. // Import populates imp from the serialized package data.
func Import(in *bufio.Reader) { func Import(in *bufio.Reader, imp *Pkg) {
p := importer{ p := importer{
in: in, in: in,
imp: imp,
version: -1, // unknown version version: -1, // unknown version
strList: []string{""}, // empty string is mapped to 0 strList: []string{""}, // empty string is mapped to 0
} }
...@@ -98,7 +100,7 @@ func Import(in *bufio.Reader) { ...@@ -98,7 +100,7 @@ func Import(in *bufio.Reader) {
case 0: case 0:
// Go1.7 encoding format - nothing to do here // Go1.7 encoding format - nothing to do here
default: default:
formatErrorf("unknown export format version %d (%q)", p.version, versionstr) p.formatErrorf("unknown export format version %d (%q)", p.version, versionstr)
} }
// --- generic export data --- // --- generic export data ---
...@@ -129,7 +131,7 @@ func Import(in *bufio.Reader) { ...@@ -129,7 +131,7 @@ func Import(in *bufio.Reader) {
// self-verification // self-verification
if count := p.int(); count != objcount { if count := p.int(); count != objcount {
formatErrorf("got %d objects; want %d", objcount, count) p.formatErrorf("got %d objects; want %d", objcount, count)
} }
// --- compiler-specific export data --- // --- compiler-specific export data ---
...@@ -149,12 +151,12 @@ func Import(in *bufio.Reader) { ...@@ -149,12 +151,12 @@ func Import(in *bufio.Reader) {
// self-verification // self-verification
if count := p.int(); count != objcount { if count := p.int(); count != objcount {
formatErrorf("got %d objects; want %d", objcount, count) p.formatErrorf("got %d objects; want %d", objcount, count)
} }
// read inlineable functions bodies // read inlineable functions bodies
if dclcontext != PEXTERN { if dclcontext != PEXTERN {
formatErrorf("unexpected context %d", dclcontext) p.formatErrorf("unexpected context %d", dclcontext)
} }
objcount = 0 objcount = 0
...@@ -166,12 +168,12 @@ func Import(in *bufio.Reader) { ...@@ -166,12 +168,12 @@ func Import(in *bufio.Reader) {
// don't process the same function twice // don't process the same function twice
if i <= i0 { if i <= i0 {
formatErrorf("index not increasing: %d <= %d", i, i0) p.formatErrorf("index not increasing: %d <= %d", i, i0)
} }
i0 = i i0 = i
if funcdepth != 0 { if funcdepth != 0 {
formatErrorf("unexpected Funcdepth %d", funcdepth) p.formatErrorf("unexpected Funcdepth %d", funcdepth)
} }
// Note: In the original code, funchdr and funcbody are called for // Note: In the original code, funchdr and funcbody are called for
...@@ -205,11 +207,11 @@ func Import(in *bufio.Reader) { ...@@ -205,11 +207,11 @@ func Import(in *bufio.Reader) {
// self-verification // self-verification
if count := p.int(); count != objcount { if count := p.int(); count != objcount {
formatErrorf("got %d functions; want %d", objcount, count) p.formatErrorf("got %d functions; want %d", objcount, count)
} }
if dclcontext != PEXTERN { if dclcontext != PEXTERN {
formatErrorf("unexpected context %d", dclcontext) p.formatErrorf("unexpected context %d", dclcontext)
} }
p.verifyTypes() p.verifyTypes()
...@@ -224,13 +226,13 @@ func Import(in *bufio.Reader) { ...@@ -224,13 +226,13 @@ func Import(in *bufio.Reader) {
} }
} }
func formatErrorf(format string, args ...interface{}) { func (p *importer) formatErrorf(format string, args ...interface{}) {
if debugFormat { if debugFormat {
Fatalf(format, args...) Fatalf(format, args...)
} }
yyerror("cannot import %q due to version skew - reinstall package (%s)", yyerror("cannot import %q due to version skew - reinstall package (%s)",
importpkg.Path, fmt.Sprintf(format, args...)) p.imp.Path, fmt.Sprintf(format, args...))
errorexit() errorexit()
} }
...@@ -239,7 +241,7 @@ func (p *importer) verifyTypes() { ...@@ -239,7 +241,7 @@ func (p *importer) verifyTypes() {
pt := pair.pt pt := pair.pt
t := pair.t t := pair.t
if !eqtype(pt.Orig, t) { if !eqtype(pt.Orig, t) {
formatErrorf("inconsistent definition for type %v during import\n\t%L (in %q)\n\t%L (in %q)", pt.Sym, pt, pt.Sym.Importdef.Path, t, importpkg.Path) p.formatErrorf("inconsistent definition for type %v during import\n\t%L (in %q)\n\t%L (in %q)", pt.Sym, pt, pt.Sym.Importdef.Path, t, p.imp.Path)
} }
} }
} }
...@@ -259,7 +261,7 @@ func (p *importer) pkg() *Pkg { ...@@ -259,7 +261,7 @@ func (p *importer) pkg() *Pkg {
// otherwise, i is the package tag (< 0) // otherwise, i is the package tag (< 0)
if i != packageTag { if i != packageTag {
formatErrorf("expected package tag, found tag = %d", i) p.formatErrorf("expected package tag, found tag = %d", i)
} }
// read package data // read package data
...@@ -268,22 +270,22 @@ func (p *importer) pkg() *Pkg { ...@@ -268,22 +270,22 @@ func (p *importer) pkg() *Pkg {
// we should never see an empty package name // we should never see an empty package name
if name == "" { if name == "" {
formatErrorf("empty package name for path %q", path) p.formatErrorf("empty package name for path %q", path)
} }
// we should never see a bad import path // we should never see a bad import path
if isbadimport(path) { if isbadimport(path) {
formatErrorf("bad package path %q for package %s", path, name) p.formatErrorf("bad package path %q for package %s", path, name)
} }
// an empty path denotes the package we are currently importing; // an empty path denotes the package we are currently importing;
// it must be the first package we see // it must be the first package we see
if (path == "") != (len(p.pkgList) == 0) { if (path == "") != (len(p.pkgList) == 0) {
formatErrorf("package path %q for pkg index %d", path, len(p.pkgList)) p.formatErrorf("package path %q for pkg index %d", path, len(p.pkgList))
} }
// add package to pkgList // add package to pkgList
pkg := importpkg pkg := p.imp
if path != "" { if path != "" {
pkg = mkpkg(path) pkg = mkpkg(path)
} }
...@@ -294,7 +296,7 @@ func (p *importer) pkg() *Pkg { ...@@ -294,7 +296,7 @@ func (p *importer) pkg() *Pkg {
yyerror("conflicting package names %s and %s for path %q", pkg.Name, name, path) yyerror("conflicting package names %s and %s for path %q", pkg.Name, name, path)
} }
if myimportpath != "" && path == myimportpath { if myimportpath != "" && path == myimportpath {
yyerror("import %q: package depends on %q (import cycle)", importpkg.Path, path) yyerror("import %q: package depends on %q (import cycle)", p.imp.Path, path)
errorexit() errorexit()
} }
p.pkgList = append(p.pkgList, pkg) p.pkgList = append(p.pkgList, pkg)
...@@ -345,7 +347,7 @@ func (p *importer) obj(tag int) { ...@@ -345,7 +347,7 @@ func (p *importer) obj(tag int) {
if sym.Def != nil && sym.Def.Op == ONAME { if sym.Def != nil && sym.Def.Op == ONAME {
// function was imported before (via another import) // function was imported before (via another import)
if !eqtype(sig, sym.Def.Type) { if !eqtype(sig, sym.Def.Type) {
formatErrorf("inconsistent definition for func %v during import\n\t%v\n\t%v", sym, sym.Def.Type, sig) p.formatErrorf("inconsistent definition for func %v during import\n\t%v\n\t%v", sym, sym.Def.Type, sig)
} }
p.funcList = append(p.funcList, nil) p.funcList = append(p.funcList, nil)
break break
...@@ -358,14 +360,14 @@ func (p *importer) obj(tag int) { ...@@ -358,14 +360,14 @@ func (p *importer) obj(tag int) {
importlist = append(importlist, n) importlist = append(importlist, n)
if Debug['E'] > 0 { if Debug['E'] > 0 {
fmt.Printf("import [%q] func %v \n", importpkg.Path, n) fmt.Printf("import [%q] func %v \n", p.imp.Path, n)
if Debug['m'] > 2 && n.Func.Inl.Len() != 0 { if Debug['m'] > 2 && n.Func.Inl.Len() != 0 {
fmt.Printf("inl body: %v\n", n.Func.Inl) fmt.Printf("inl body: %v\n", n.Func.Inl)
} }
} }
default: default:
formatErrorf("unexpected object (tag = %d)", tag) p.formatErrorf("unexpected object (tag = %d)", tag)
} }
} }
...@@ -405,7 +407,7 @@ func (p *importer) newtyp(etype EType) *Type { ...@@ -405,7 +407,7 @@ func (p *importer) newtyp(etype EType) *Type {
func (p *importer) importtype(pt, t *Type) { func (p *importer) importtype(pt, t *Type) {
if pt.Etype == TFORW { if pt.Etype == TFORW {
copytype(pt.nod, t) copytype(pt.nod, t)
pt.Sym.Importdef = importpkg pt.Sym.Importdef = p.imp
pt.Sym.Lastlineno = lineno pt.Sym.Lastlineno = lineno
declare(pt.nod, PEXTERN) declare(pt.nod, PEXTERN)
checkwidth(pt) checkwidth(pt)
...@@ -416,7 +418,7 @@ func (p *importer) importtype(pt, t *Type) { ...@@ -416,7 +418,7 @@ func (p *importer) importtype(pt, t *Type) {
// Collect the types and verify identity later. // Collect the types and verify identity later.
p.cmpList = append(p.cmpList, struct{ pt, t *Type }{pt, t}) p.cmpList = append(p.cmpList, struct{ pt, t *Type }{pt, t})
} else if !eqtype(pt.Orig, t) { } else if !eqtype(pt.Orig, t) {
yyerror("inconsistent definition for type %v during import\n\t%L (in %q)\n\t%L (in %q)", pt.Sym, pt, pt.Sym.Importdef.Path, t, importpkg.Path) yyerror("inconsistent definition for type %v during import\n\t%L (in %q)\n\t%L (in %q)", pt.Sym, pt, pt.Sym.Importdef.Path, t, p.imp.Path)
} }
} }
...@@ -486,7 +488,7 @@ func (p *importer) typ() *Type { ...@@ -486,7 +488,7 @@ func (p *importer) typ() *Type {
n.Type.SetNname(n) n.Type.SetNname(n)
if Debug['E'] > 0 { if Debug['E'] > 0 {
fmt.Printf("import [%q] meth %v \n", importpkg.Path, n) fmt.Printf("import [%q] meth %v \n", p.imp.Path, n)
if Debug['m'] > 2 && n.Func.Inl.Len() != 0 { if Debug['m'] > 2 && n.Func.Inl.Len() != 0 {
fmt.Printf("inl body: %v\n", n.Func.Inl) fmt.Printf("inl body: %v\n", n.Func.Inl)
} }
...@@ -546,11 +548,11 @@ func (p *importer) typ() *Type { ...@@ -546,11 +548,11 @@ func (p *importer) typ() *Type {
ct.Elem = p.typ() ct.Elem = p.typ()
default: default:
formatErrorf("unexpected type (tag = %d)", i) p.formatErrorf("unexpected type (tag = %d)", i)
} }
if t == nil { if t == nil {
formatErrorf("nil type (type tag = %d)", i) p.formatErrorf("nil type (type tag = %d)", i)
} }
return t return t
...@@ -703,7 +705,7 @@ func (p *importer) param(named bool) *Field { ...@@ -703,7 +705,7 @@ func (p *importer) param(named bool) *Field {
if named { if named {
name := p.string() name := p.string()
if name == "" { if name == "" {
formatErrorf("expected named parameter") p.formatErrorf("expected named parameter")
} }
// TODO(gri) Supply function/method package rather than // TODO(gri) Supply function/method package rather than
// encoding the package for each parameter repeatedly. // encoding the package for each parameter repeatedly.
...@@ -758,18 +760,18 @@ func (p *importer) value(typ *Type) (x Val) { ...@@ -758,18 +760,18 @@ func (p *importer) value(typ *Type) (x Val) {
x.U = p.string() x.U = p.string()
case unknownTag: case unknownTag:
formatErrorf("unknown constant (importing package with errors)") p.formatErrorf("unknown constant (importing package with errors)")
case nilTag: case nilTag:
x.U = new(NilVal) x.U = new(NilVal)
default: default:
formatErrorf("unexpected value tag %d", tag) p.formatErrorf("unexpected value tag %d", tag)
} }
// verify ideal type // verify ideal type
if typ.IsUntyped() && untype(x.Ctype()) != typ { if typ.IsUntyped() && untype(x.Ctype()) != typ {
formatErrorf("value %v and type %v don't match", x, typ) p.formatErrorf("value %v and type %v don't match", x, typ)
} }
return return
...@@ -1233,7 +1235,7 @@ func (p *importer) tagOrIndex() int { ...@@ -1233,7 +1235,7 @@ func (p *importer) tagOrIndex() int {
func (p *importer) int() int { func (p *importer) int() int {
x := p.int64() x := p.int64()
if int64(int(x)) != x { if int64(int(x)) != x {
formatErrorf("exported integer too large") p.formatErrorf("exported integer too large")
} }
return int(x) return int(x)
} }
...@@ -1272,12 +1274,12 @@ func (p *importer) string() string { ...@@ -1272,12 +1274,12 @@ func (p *importer) string() string {
func (p *importer) marker(want byte) { func (p *importer) marker(want byte) {
if got := p.rawByte(); got != want { if got := p.rawByte(); got != want {
formatErrorf("incorrect marker: got %c; want %c (pos = %d)", got, want, p.read) p.formatErrorf("incorrect marker: got %c; want %c (pos = %d)", got, want, p.read)
} }
pos := p.read pos := p.read
if n := int(p.rawInt64()); n != pos { if n := int(p.rawInt64()); n != pos {
formatErrorf("incorrect position: got %d; want %d", n, pos) p.formatErrorf("incorrect position: got %d; want %d", n, pos)
} }
} }
...@@ -1285,7 +1287,7 @@ func (p *importer) marker(want byte) { ...@@ -1285,7 +1287,7 @@ func (p *importer) marker(want byte) {
func (p *importer) rawInt64() int64 { func (p *importer) rawInt64() int64 {
i, err := binary.ReadVarint(p) i, err := binary.ReadVarint(p)
if err != nil { if err != nil {
formatErrorf("read error: %v", err) p.formatErrorf("read error: %v", err)
} }
return i return i
} }
...@@ -1312,13 +1314,13 @@ func (p *importer) rawByte() byte { ...@@ -1312,13 +1314,13 @@ func (p *importer) rawByte() byte {
c, err := p.in.ReadByte() c, err := p.in.ReadByte()
p.read++ p.read++
if err != nil { if err != nil {
formatErrorf("read error: %v", err) p.formatErrorf("read error: %v", err)
} }
if c == '|' { if c == '|' {
c, err = p.in.ReadByte() c, err = p.in.ReadByte()
p.read++ p.read++
if err != nil { if err != nil {
formatErrorf("read error: %v", err) p.formatErrorf("read error: %v", err)
} }
switch c { switch c {
case 'S': case 'S':
...@@ -1326,7 +1328,7 @@ func (p *importer) rawByte() byte { ...@@ -1326,7 +1328,7 @@ func (p *importer) rawByte() byte {
case '|': case '|':
// nothing to do // nothing to do
default: default:
formatErrorf("unexpected escape sequence in export data") p.formatErrorf("unexpected escape sequence in export data")
} }
} }
return c return c
......
...@@ -177,7 +177,7 @@ func dumpexport() { ...@@ -177,7 +177,7 @@ func dumpexport() {
pkgMap = make(map[string]*Pkg) pkgMap = make(map[string]*Pkg)
pkgs = nil pkgs = nil
importpkg = mkpkg("") importpkg = mkpkg("")
Import(bufio.NewReader(&copy)) // must not die Import(bufio.NewReader(&copy), importpkg) // must not die
importpkg = nil importpkg = nil
pkgs = savedPkgs pkgs = savedPkgs
pkgMap = savedPkgMap pkgMap = savedPkgMap
......
...@@ -919,7 +919,7 @@ func importfile(f *Val, indent []byte) { ...@@ -919,7 +919,7 @@ func importfile(f *Val, indent []byte) {
fmt.Printf("importing %s (%s)\n", path_, file) fmt.Printf("importing %s (%s)\n", path_, file)
} }
imp.ReadByte() // skip \n after $$B imp.ReadByte() // skip \n after $$B
Import(imp) Import(imp, importpkg)
default: default:
yyerror("no import in %q", path_) yyerror("no import in %q", path_)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment