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 <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
diff --git a/go/internal/gcimporter/iimport.go b/go/internal/gcimporter/iimport.go
index 0fd22bb..3cb7ae5 100644
--- a/go/internal/gcimporter/iimport.go
+++ b/go/internal/gcimporter/iimport.go
@@ -109,7 +109,7 @@
 		},
 	}
 
-	for i, pt := range predeclared {
+	for i, pt := range predeclared() {
 		p.typCache[uint64(i)] = pt
 	}
 
@@ -142,8 +142,12 @@
 		p.pkgIndex[pkg] = nameIndex
 		pkgList[i] = pkg
 	}
-
-	localpkg := pkgList[0]
+	var localpkg *types.Package
+	for _, pkg := range pkgList {
+		if pkg.Path() == path {
+			localpkg = pkg
+		}
+	}
 
 	names := make([]string, 0, len(p.pkgIndex[localpkg]))
 	for name := range p.pkgIndex[localpkg] {
@@ -330,6 +334,10 @@
 		val = constant.BinaryOp(re, token.ADD, constant.MakeImag(im))
 
 	default:
+		if b.Kind() == types.Invalid {
+			val = constant.MakeUnknown()
+			return
+		}
 		errorf("unexpected type %v", typ) // panics
 		panic("unreachable")
 	}