cmd/protoc-gen-go: generate forwarding decls for ExtensionDescs

When publicly importing a package with extension definitions, generate
forwarding declarations for the "E_..." ExtensionDesc var:

    var E_ExtensionField = publicimport.E_ExtensionField

Change-Id: Ifd57c487c3a44f303c2c098a42ea249b219b734f
Reviewed-on: https://go-review.googlesource.com/c/145498
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
diff --git a/cmd/protoc-gen-go/internal_gengo/main.go b/cmd/protoc-gen-go/internal_gengo/main.go
index 89d4011..96b27fb 100644
--- a/cmd/protoc-gen-go/internal_gengo/main.go
+++ b/cmd/protoc-gen-go/internal_gengo/main.go
@@ -194,6 +194,11 @@
 			g.P("const ", value.GoIdent.GoName, " = ", enum.GoIdent.GoName, "(", value.GoIdent, ")")
 		}
 	}
+	for _, ext := range impFile.Extensions {
+		ident := extensionVar(impFile, ext)
+		g.P("var ", ident.GoName, " = ", ident)
+		g.P()
+	}
 	g.P()
 }
 
@@ -823,7 +828,7 @@
 		name = name.Parent()
 	}
 
-	g.P("var ", extensionVar(f, extension), " = &", protogen.GoIdent{
+	g.P("var ", extensionVar(f.File, extension), " = &", protogen.GoIdent{
 		GoImportPath: protoPackage,
 		GoName:       "ExtensionDesc",
 	}, "{")
@@ -848,7 +853,7 @@
 }
 
 // extensionVar returns the var holding the ExtensionDesc for an extension.
-func extensionVar(f *fileInfo, extension *protogen.Extension) protogen.GoIdent {
+func extensionVar(f *protogen.File, extension *protogen.Extension) protogen.GoIdent {
 	name := "E_"
 	if extension.ParentMessage != nil {
 		name += extension.ParentMessage.GoIdent.GoName + "_"
@@ -922,7 +927,7 @@
 	g.P(protogen.GoIdent{
 		GoImportPath: protoPackage,
 		GoName:       "RegisterExtension",
-	}, "(", extensionVar(f, extension), ")")
+	}, "(", extensionVar(f.File, extension), ")")
 	if isExtensionMessageSetElement(gen, extension) {
 		goType, pointer := fieldGoType(g, extension)
 		if pointer {
diff --git a/cmd/protoc-gen-go/testdata/import_public/a.pb.go b/cmd/protoc-gen-go/testdata/import_public/a.pb.go
index 830e965..9b18dc0 100644
--- a/cmd/protoc-gen-go/testdata/import_public/a.pb.go
+++ b/cmd/protoc-gen-go/testdata/import_public/a.pb.go
@@ -57,6 +57,8 @@
 
 const M_Submessage_M_SUBMESSAGE_ZERO = M_Submessage_Submessage_Subenum(sub.M_Submessage_M_SUBMESSAGE_ZERO)
 
+var E_ExtensionField = sub.E_ExtensionField
+
 type Public struct {
 	M                    *sub.M   `protobuf:"bytes,1,opt,name=m" json:"m,omitempty"`
 	E                    *sub.E   `protobuf:"varint,2,opt,name=e,enum=goproto.protoc.import_public.sub.E" json:"e,omitempty"`
diff --git a/cmd/protoc-gen-go/testdata/import_public/sub/a.pb.go b/cmd/protoc-gen-go/testdata/import_public/sub/a.pb.go
index f9d5e4b..b9218c2 100644
--- a/cmd/protoc-gen-go/testdata/import_public/sub/a.pb.go
+++ b/cmd/protoc-gen-go/testdata/import_public/sub/a.pb.go
@@ -138,10 +138,11 @@
 	// Types that are valid to be assigned to OneofField:
 	//	*M_OneofInt32
 	//	*M_OneofInt64
-	OneofField           isM_OneofField `protobuf_oneof:"oneof_field"`
-	XXX_NoUnkeyedLiteral struct{}       `json:"-"`
-	XXX_unrecognized     []byte         `json:"-"`
-	XXX_sizecache        int32          `json:"-"`
+	OneofField                   isM_OneofField `protobuf_oneof:"oneof_field"`
+	XXX_NoUnkeyedLiteral         struct{}       `json:"-"`
+	proto.XXX_InternalExtensions `json:"-"`
+	XXX_unrecognized             []byte `json:"-"`
+	XXX_sizecache                int32  `json:"-"`
 }
 
 func (m *M) Reset()         { *m = M{} }
@@ -151,6 +152,14 @@
 	return fileDescriptor_382f7805394b5c4e, []int{0}
 }
 
+var extRange_M = []proto.ExtensionRange{
+	{Start: 100, End: 536870911},
+}
+
+func (*M) ExtensionRangeArray() []proto.ExtensionRange {
+	return extRange_M
+}
+
 func (m *M) XXX_Unmarshal(b []byte) error {
 	return xxx_messageInfo_M.Unmarshal(m, b)
 }
@@ -422,37 +431,50 @@
 	return n
 }
 
+var E_ExtensionField = &proto.ExtensionDesc{
+	ExtendedType:  (*M)(nil),
+	ExtensionType: (*string)(nil),
+	Field:         100,
+	Name:          "goproto.protoc.import_public.sub.extension_field",
+	Tag:           "bytes,100,opt,name=extension_field",
+	Filename:      "import_public/sub/a.proto",
+}
+
 func init() {
 	proto.RegisterEnum("goproto.protoc.import_public.sub.E", E_name, E_value)
 	proto.RegisterEnum("goproto.protoc.import_public.sub.M_Subenum", M_Subenum_name, M_Subenum_value)
 	proto.RegisterEnum("goproto.protoc.import_public.sub.M_Submessage_Submessage_Subenum", M_Submessage_Submessage_Subenum_name, M_Submessage_Submessage_Subenum_value)
 	proto.RegisterType((*M)(nil), "goproto.protoc.import_public.sub.M")
 	proto.RegisterType((*M_Submessage)(nil), "goproto.protoc.import_public.sub.M.Submessage")
+	proto.RegisterExtension(E_ExtensionField)
 }
 
 func init() { proto.RegisterFile("import_public/sub/a.proto", fileDescriptor_382f7805394b5c4e) }
 
 var fileDescriptor_382f7805394b5c4e = []byte{
-	// 334 bytes of a gzipped FileDescriptorProto
-	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x84, 0x91, 0xcd, 0x4a, 0xc3, 0x40,
-	0x14, 0x85, 0x3b, 0x4d, 0xb5, 0x7a, 0x4b, 0x41, 0x07, 0x2b, 0xb1, 0xab, 0x58, 0x5c, 0x04, 0xa5,
-	0x19, 0x88, 0x21, 0x0b, 0x77, 0x16, 0x82, 0x3f, 0x18, 0x0a, 0x09, 0x6e, 0xba, 0x09, 0x99, 0x64,
-	0x12, 0x03, 0x49, 0xa6, 0x74, 0x66, 0x7c, 0x00, 0x5f, 0xcd, 0x17, 0x13, 0xd3, 0xd6, 0xb6, 0x58,
-	0x71, 0x97, 0x73, 0xef, 0xf9, 0xce, 0xc9, 0x70, 0xe1, 0xa2, 0xa8, 0xe6, 0x7c, 0x21, 0xa3, 0xb9,
-	0xa2, 0x65, 0x91, 0x10, 0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0x7f, 0xfd, 0xfe, 0xc6, 0x75, 0xdd, 0x07,
-	0xe4, 0xe1, 0x23, 0xe8, 0xac, 0xb6, 0x2f, 0xb3, 0xe7, 0xbc, 0x90, 0x6f, 0x8a, 0x5a, 0x09, 0xaf,
-	0x48, 0xce, 0xcb, 0xb8, 0xce, 0x49, 0x73, 0x0c, 0xaa, 0x32, 0xf2, 0x6e, 0x93, 0xa4, 0x4a, 0x97,
-	0x3a, 0x19, 0xe7, 0xac, 0x1e, 0xe7, 0x9c, 0x48, 0x26, 0x64, 0x1a, 0xcb, 0x98, 0xfc, 0xba, 0xf4,
-	0x57, 0x00, 0x00, 0x00, 0xff, 0xff, 0x85, 0xd9, 0xf7, 0xbf, 0x3a, 0x02, 0x00, 0x00,
+	// 377 bytes of a gzipped FileDescriptorProto
+	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x92, 0xcf, 0x8f, 0x93, 0x40,
+	0x14, 0xc7, 0x77, 0x60, 0x75, 0xeb, 0xdb, 0xac, 0xae, 0x13, 0x6b, 0x70, 0x4f, 0x58, 0x3d, 0x90,
+	0x35, 0x65, 0x12, 0x24, 0x1c, 0x7a, 0x93, 0x04, 0x7f, 0xa5, 0xa4, 0x09, 0xc4, 0x4b, 0x2f, 0x84,
+	0x81, 0x01, 0x49, 0x80, 0x69, 0x3a, 0x33, 0xc6, 0x23, 0x7f, 0x80, 0xff, 0x95, 0xff, 0x98, 0x29,
+	0xf4, 0x67, 0xac, 0x71, 0x6f, 0xbc, 0xc7, 0xe7, 0xf3, 0x7d, 0x93, 0x79, 0x03, 0xaf, 0xaa, 0x66,
+	0xc5, 0xd7, 0x32, 0x59, 0x29, 0x5a, 0x57, 0x19, 0x11, 0x8a, 0x92, 0xd4, 0x5e, 0xad, 0xb9, 0xe4,
+	0xd8, 0x2c, 0x79, 0xff, 0x31, 0x94, 0x99, 0x7d, 0x42, 0xda, 0x42, 0xd1, 0xbb, 0x33, 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x50, 0x80, 0x47, 0x70, 0x39, 0x70, 0xb3, 0x39, 0x3c, 0x63, 0x3f, 0x25, 0x6b, 0x45, 0xc5, 0xdb,
+	0x81, 0xc0, 0x6f, 0x1e, 0xb0, 0x1e, 0x23, 0xdf, 0x2c, 0x25, 0x7a, 0xba, 0x77, 0x3f, 0x6e, 0x54,
+	0x7f, 0xbe, 0xfc, 0x5a, 0x56, 0xf2, 0xbb, 0xa2, 0x76, 0xc6, 0x1b, 0x52, 0xf2, 0x3a, 0x6d, 0x4b,
+	0xd2, 0xa7, 0x50, 0x55, 0x90, 0x1f, 0x0e, 0xc9, 0x9a, 0x7c, 0xa8, 0xb3, 0x69, 0xc9, 0xda, 0x69,
+	0xc9, 0x89, 0x64, 0x42, 0xe6, 0xa9, 0x4c, 0xc9, 0x5f, 0x2f, 0xe8, 0x4f, 0x00, 0x00, 0x00, 0xff,
+	0xff, 0xcd, 0x86, 0xfe, 0xf7, 0x92, 0x02, 0x00, 0x00,
 }
diff --git a/cmd/protoc-gen-go/testdata/import_public/sub/a.proto b/cmd/protoc-gen-go/testdata/import_public/sub/a.proto
index 3771366..d3c77bd 100644
--- a/cmd/protoc-gen-go/testdata/import_public/sub/a.proto
+++ b/cmd/protoc-gen-go/testdata/import_public/sub/a.proto
@@ -34,6 +34,12 @@
   enum Subenum {
     M_ZERO = 0;
   }
+
+  extensions 100 to max;
+}
+
+extend M {
+  optional string extension_field = 100;
 }
 
 enum E {