Skip to content

Commit 58ecf64

Browse files
committed
go/internal/gcimporter: write export data for go/types
Add an iexport.go (and corresponding iexport_test.go) file, which is an adapted version of $GOROOT/src/cmd/compile/internal/gc/iexport.go. This code writes exportdata for a *go/types.Package. A majority of this code is directly copied from iexport.go, with a change of types, while some of it had to be modified slightly. Updates golang/go#28260 Change-Id: Ic7e8e99f0c6b886839280b410afffb037da8a79b Reviewed-on: https://go-review.googlesource.com/c/156901 Run-TryBot: Rebecca Stambler <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Robert Griesemer <[email protected]>
1 parent 9bdeadd commit 58ecf64

File tree

5 files changed

+1094
-50
lines changed

5 files changed

+1094
-50
lines changed

go/internal/gcimporter/bexport.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,10 @@ func BExportData(fset *token.FileSet, pkg *types.Package) (b []byte, err error)
127127
// --- generic export data ---
128128

129129
// populate type map with predeclared "known" types
130-
for index, typ := range predeclared {
130+
for index, typ := range predeclared() {
131131
p.typIndex[typ] = index
132132
}
133-
if len(p.typIndex) != len(predeclared) {
133+
if len(p.typIndex) != len(predeclared()) {
134134
return nil, internalError("duplicate entries in type map?")
135135
}
136136

go/internal/gcimporter/bimport.go

Lines changed: 53 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func BImportData(fset *token.FileSet, imports map[string]*types.Package, data []
126126
// --- generic export data ---
127127

128128
// populate typList with predeclared "known" types
129-
p.typList = append(p.typList, predeclared...)
129+
p.typList = append(p.typList, predeclared()...)
130130

131131
// read package data
132132
pkg = p.pkg()
@@ -976,50 +976,58 @@ const (
976976
aliasTag
977977
)
978978

979-
var predeclared = []types.Type{
980-
// basic types
981-
types.Typ[types.Bool],
982-
types.Typ[types.Int],
983-
types.Typ[types.Int8],
984-
types.Typ[types.Int16],
985-
types.Typ[types.Int32],
986-
types.Typ[types.Int64],
987-
types.Typ[types.Uint],
988-
types.Typ[types.Uint8],
989-
types.Typ[types.Uint16],
990-
types.Typ[types.Uint32],
991-
types.Typ[types.Uint64],
992-
types.Typ[types.Uintptr],
993-
types.Typ[types.Float32],
994-
types.Typ[types.Float64],
995-
types.Typ[types.Complex64],
996-
types.Typ[types.Complex128],
997-
types.Typ[types.String],
998-
999-
// basic type aliases
1000-
types.Universe.Lookup("byte").Type(),
1001-
types.Universe.Lookup("rune").Type(),
1002-
1003-
// error
1004-
types.Universe.Lookup("error").Type(),
1005-
1006-
// untyped types
1007-
types.Typ[types.UntypedBool],
1008-
types.Typ[types.UntypedInt],
1009-
types.Typ[types.UntypedRune],
1010-
types.Typ[types.UntypedFloat],
1011-
types.Typ[types.UntypedComplex],
1012-
types.Typ[types.UntypedString],
1013-
types.Typ[types.UntypedNil],
1014-
1015-
// package unsafe
1016-
types.Typ[types.UnsafePointer],
1017-
1018-
// invalid type
1019-
types.Typ[types.Invalid], // only appears in packages with errors
1020-
1021-
// used internally by gc; never used by this package or in .a files
1022-
anyType{},
979+
var predecl []types.Type // initialized lazily
980+
981+
func predeclared() []types.Type {
982+
if predecl == nil {
983+
// initialize lazily to be sure that all
984+
// elements have been initialized before
985+
predecl = []types.Type{ // basic types
986+
types.Typ[types.Bool],
987+
types.Typ[types.Int],
988+
types.Typ[types.Int8],
989+
types.Typ[types.Int16],
990+
types.Typ[types.Int32],
991+
types.Typ[types.Int64],
992+
types.Typ[types.Uint],
993+
types.Typ[types.Uint8],
994+
types.Typ[types.Uint16],
995+
types.Typ[types.Uint32],
996+
types.Typ[types.Uint64],
997+
types.Typ[types.Uintptr],
998+
types.Typ[types.Float32],
999+
types.Typ[types.Float64],
1000+
types.Typ[types.Complex64],
1001+
types.Typ[types.Complex128],
1002+
types.Typ[types.String],
1003+
1004+
// basic type aliases
1005+
types.Universe.Lookup("byte").Type(),
1006+
types.Universe.Lookup("rune").Type(),
1007+
1008+
// error
1009+
types.Universe.Lookup("error").Type(),
1010+
1011+
// untyped types
1012+
types.Typ[types.UntypedBool],
1013+
types.Typ[types.UntypedInt],
1014+
types.Typ[types.UntypedRune],
1015+
types.Typ[types.UntypedFloat],
1016+
types.Typ[types.UntypedComplex],
1017+
types.Typ[types.UntypedString],
1018+
types.Typ[types.UntypedNil],
1019+
1020+
// package unsafe
1021+
types.Typ[types.UnsafePointer],
1022+
1023+
// invalid type
1024+
types.Typ[types.Invalid], // only appears in packages with errors
1025+
1026+
// used internally by gc; never used by this package or in .a files
1027+
anyType{},
1028+
}
1029+
}
1030+
return predecl
10231031
}
10241032

10251033
type anyType struct{}

0 commit comments

Comments
 (0)