internal/filedesc, internal/filetype: initial commit

The internal/fileinit package is split apart into two packages:
* internal/filedesc constructs descriptors from the raw proto.
It is very similar to the previous internal/fileinit package.
* internal/filetype wraps descriptors with Go type information

Overview:
* The internal/fileinit package will be deleted in a future CL.
It is kept around since the v1 repo currently depends on it.
* The internal/prototype package is deleted. All former usages of it
are now using internal/filedesc instead. Most significantly,
the reflect/protodesc package was almost entirely re-written.
* The internal/impl package drops support for messages that do not
have a Descriptor method (pre-2016). This removes a significant amount
of technical debt.
filedesc.Builder to parse raw descriptors.
* The internal/encoding/defval package now handles enum values by name.

Change-Id: I3957bcc8588a70470fd6c7de1122216b80615ab7
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/182360
Reviewed-by: Damien Neil <dneil@google.com>
diff --git a/internal/encoding/defval/default.go b/internal/encoding/defval/default.go
index a15df02..f03acdf 100644
--- a/internal/encoding/defval/default.go
+++ b/internal/encoding/defval/default.go
@@ -34,54 +34,55 @@
 )
 
 // Unmarshal deserializes the default string s according to the given kind k.
-// When using the Descriptor format on an enum kind, a Value of type string
-// representing the enum identifier is returned. It is the caller's
-// responsibility to verify that the identifier is valid.
-func Unmarshal(s string, k pref.Kind, f Format) (pref.Value, error) {
+// When k is an enum, a list of enum value descriptors must be provided.
+func Unmarshal(s string, k pref.Kind, evs pref.EnumValueDescriptors, f Format) (pref.Value, pref.EnumValueDescriptor, error) {
 	switch k {
 	case pref.BoolKind:
 		if f == GoTag {
 			switch s {
 			case "1":
-				return pref.ValueOf(true), nil
+				return pref.ValueOf(true), nil, nil
 			case "0":
-				return pref.ValueOf(false), nil
+				return pref.ValueOf(false), nil, nil
 			}
 		} else {
 			switch s {
 			case "true":
-				return pref.ValueOf(true), nil
+				return pref.ValueOf(true), nil, nil
 			case "false":
-				return pref.ValueOf(false), nil
+				return pref.ValueOf(false), nil, nil
 			}
 		}
 	case pref.EnumKind:
 		if f == GoTag {
-			// Go tags used the numeric form of the enum value.
+			// Go tags use the numeric form of the enum value.
 			if n, err := strconv.ParseInt(s, 10, 32); err == nil {
-				return pref.ValueOf(pref.EnumNumber(n)), nil
+				if ev := evs.ByNumber(pref.EnumNumber(n)); ev != nil {
+					return pref.ValueOf(ev.Number()), ev, nil
+				}
 			}
 		} else {
-			// Descriptor default_value used the enum identifier.
-			if pref.Name(s).IsValid() {
-				return pref.ValueOf(s), nil
+			// Descriptor default_value use the enum identifier.
+			ev := evs.ByName(pref.Name(s))
+			if ev != nil {
+				return pref.ValueOf(ev.Number()), ev, nil
 			}
 		}
 	case pref.Int32Kind, pref.Sint32Kind, pref.Sfixed32Kind:
 		if v, err := strconv.ParseInt(s, 10, 32); err == nil {
-			return pref.ValueOf(int32(v)), nil
+			return pref.ValueOf(int32(v)), nil, nil
 		}
 	case pref.Int64Kind, pref.Sint64Kind, pref.Sfixed64Kind:
 		if v, err := strconv.ParseInt(s, 10, 64); err == nil {
-			return pref.ValueOf(int64(v)), nil
+			return pref.ValueOf(int64(v)), nil, nil
 		}
 	case pref.Uint32Kind, pref.Fixed32Kind:
 		if v, err := strconv.ParseUint(s, 10, 32); err == nil {
-			return pref.ValueOf(uint32(v)), nil
+			return pref.ValueOf(uint32(v)), nil, nil
 		}
 	case pref.Uint64Kind, pref.Fixed64Kind:
 		if v, err := strconv.ParseUint(s, 10, 64); err == nil {
-			return pref.ValueOf(uint64(v)), nil
+			return pref.ValueOf(uint64(v)), nil, nil
 		}
 	case pref.FloatKind, pref.DoubleKind:
 		var v float64
@@ -98,25 +99,26 @@
 		}
 		if err == nil {
 			if k == pref.FloatKind {
-				return pref.ValueOf(float32(v)), nil
+				return pref.ValueOf(float32(v)), nil, nil
 			} else {
-				return pref.ValueOf(float64(v)), nil
+				return pref.ValueOf(float64(v)), nil, nil
 			}
 		}
 	case pref.StringKind:
 		// String values are already unescaped and can be used as is.
-		return pref.ValueOf(s), nil
+		return pref.ValueOf(s), nil, nil
 	case pref.BytesKind:
 		if b, ok := unmarshalBytes(s); ok {
-			return pref.ValueOf(b), nil
+			return pref.ValueOf(b), nil, nil
 		}
 	}
-	return pref.Value{}, errors.New("invalid default value for %v: %q", k, s)
+	return pref.Value{}, nil, errors.New("invalid default value for %v: %q", k, s)
 }
 
 // Marshal serializes v as the default string according to the given kind k.
-// Enums are serialized in numeric form regardless of format chosen.
-func Marshal(v pref.Value, k pref.Kind, f Format) (string, error) {
+// When specifying the Descriptor format for an enum kind, the associated
+// enum value descriptor must be provided.
+func Marshal(v pref.Value, ev pref.EnumValueDescriptor, k pref.Kind, f Format) (string, error) {
 	switch k {
 	case pref.BoolKind:
 		if f == GoTag {
@@ -133,7 +135,11 @@
 			}
 		}
 	case pref.EnumKind:
-		return strconv.FormatInt(int64(v.Enum()), 10), nil
+		if f == GoTag {
+			return strconv.FormatInt(int64(v.Enum()), 10), nil
+		} else {
+			return string(ev.Name()), nil
+		}
 	case pref.Int32Kind, pref.Sint32Kind, pref.Sfixed32Kind, pref.Int64Kind, pref.Sint64Kind, pref.Sfixed64Kind:
 		return strconv.FormatInt(v.Int(), 10), nil
 	case pref.Uint32Kind, pref.Fixed32Kind, pref.Uint64Kind, pref.Fixed64Kind: