diff --git a/go/importer/export.go b/go/importer/export.go index bcc9982c..e0bdf694 100644 --- a/go/importer/export.go +++ b/go/importer/export.go @@ -26,7 +26,7 @@ const ( version = "v0" ) -// Object and type tags. Must be < 0. +// Tags. Must be < 0. const ( // Packages packageTag = -(iota + 1) @@ -50,7 +50,7 @@ const ( namedTag // Values - intTag + int64Tag floatTag fractionTag complexTag @@ -181,7 +181,7 @@ func (p *exporter) value(x exact.Value) { p.string(exact.StringVal(x)) case exact.Int: if i, ok := exact.Int64Val(x); ok { - p.int(intTag) + p.int(int64Tag) p.int64(i) return } diff --git a/go/importer/import.go b/go/importer/import.go index a1edc5b8..6c3f69e2 100644 --- a/go/importer/import.go +++ b/go/importer/import.go @@ -132,7 +132,7 @@ func (p *importer) value() exact.Value { return exact.MakeBool(true) case stringTag: return exact.MakeString(p.string()) - case intTag: + case int64Tag: return exact.MakeInt64(p.int64()) case floatTag: return p.float() @@ -282,30 +282,31 @@ func (p *importer) typ() types.Type { return t case namedTag: - // import type object + // read type object name := p.string() pkg := p.pkg() scope := pkg.Scope() obj := scope.Lookup(name) + + // if the object doesn't exist yet, create and insert it if obj == nil { - new := types.NewTypeName(token.NoPos, pkg, name, nil) - types.NewNamed(new, nil, nil) - scope.Insert(new) - obj = new + obj = types.NewTypeName(token.NoPos, pkg, name, nil) + scope.Insert(obj) } + + // associate new named type with obj if it doesn't exist yet + t0 := types.NewNamed(obj.(*types.TypeName), nil, nil) + + // but record the existing type, if any t := obj.Type().(*types.Named) p.record(t) - // import underlying type - u := p.typ() - if t.Underlying() == nil { - t.SetUnderlying(u) - } + // read underlying type + t0.SetUnderlying(p.typ()) // read associated methods - n := p.int() - for i := 0; i < n; i++ { - t.AddMethod(types.NewFunc(token.NoPos, pkg, p.string(), p.typ().(*types.Signature))) + for i, n := 0, p.int(); i < n; i++ { + t0.AddMethod(types.NewFunc(token.NoPos, pkg, p.string(), p.typ().(*types.Signature))) } return t