all: implement proto1 weak fields

This implements generation of and reflection support for weak fields.
Weak fields are a proto1 feature where the "weak" option can be specified
on a singular message field. A weak reference results in generated code
that does not directly link in the dependency containing the weak message.

Weak field support is not added to any of the serialization logic.

Change-Id: I08ccfa72bc80b2ffb6af527a1677a0a81dcf33fb
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/185399
Reviewed-by: Damien Neil <dneil@google.com>
diff --git a/internal/impl/legacy_export.go b/internal/impl/legacy_export.go
index f08f242..07c16b5 100644
--- a/internal/impl/legacy_export.go
+++ b/internal/impl/legacy_export.go
@@ -7,11 +7,15 @@
 import (
 	"encoding/binary"
 	"encoding/json"
+	"fmt"
 	"hash/crc32"
 	"math"
+	"reflect"
 
 	"google.golang.org/protobuf/internal/errors"
 	pref "google.golang.org/protobuf/reflect/protoreflect"
+	"google.golang.org/protobuf/reflect/protoregistry"
+	piface "google.golang.org/protobuf/runtime/protoiface"
 )
 
 // These functions exist to support exported APIs in generated protobufs.
@@ -74,3 +78,13 @@
 	out = append(out, gzipFooter[:]...)
 	return out
 }
+
+// WeakNil returns a typed nil pointer to a concrete message.
+// It panics if the message is not linked into the binary.
+func (Export) WeakNil(s pref.FullName) piface.MessageV1 {
+	mt, err := protoregistry.GlobalTypes.FindMessageByName(s)
+	if err == nil {
+		panic(fmt.Sprintf("weak message %v is not linked in", s))
+	}
+	return reflect.Zero(mt.GoType()).Interface().(piface.MessageV1)
+}