internal/impl: initial commit

This provides an implementation of the has, get, set, clear methods for each
field in a message. The approach taken here is similar to the table-driven
implementation in the current v1 proto package.

The pointer_reflect.go and pointer_unsafe.go files are a simplified version of
the same files in the v1 implementation. They provide a pointer abstraction
that enables a high-efficiency approach in a non-purego environment.
The unsafe fast-path is not implemented in this commit.

This commit only implements the accessor methods for scalars using pure
Go reflection.

Change-Id: Icdf707e9d4e3385e55434f93b30a341a7680ae11
Reviewed-on: https://go-review.googlesource.com/135136
Reviewed-by: Damien Neil <dneil@google.com>
diff --git a/internal/impl/message.go b/internal/impl/message.go
new file mode 100644
index 0000000..2f10f1d
--- /dev/null
+++ b/internal/impl/message.go
@@ -0,0 +1,91 @@
+// Copyright 2018 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package impl
+
+import (
+	"reflect"
+	"strconv"
+	"strings"
+
+	pref "google.golang.org/proto/reflect/protoreflect"
+)
+
+type MessageInfo struct {
+	// TODO: Split fields into dense and sparse maps similar to the current
+	// table-driven implementation in v1?
+	fields map[pref.FieldNumber]*fieldInfo
+}
+
+// generateFieldFuncs generates per-field functions for all common operations
+// to be performed on each field. It takes in a reflect.Type representing the
+// Go struct, and a protoreflect.MessageDescriptor to match with the fields
+// in the struct.
+//
+// This code assumes that the struct is well-formed and panics if there are
+// any discrepancies.
+func (mi *MessageInfo) generateFieldFuncs(t reflect.Type, md pref.MessageDescriptor) {
+	// Generate a mapping of field numbers and names to Go struct field or type.
+	fields := map[pref.FieldNumber]reflect.StructField{}
+	oneofs := map[pref.Name]reflect.StructField{}
+	oneofFields := map[pref.FieldNumber]reflect.Type{}
+	special := map[string]reflect.StructField{}
+fieldLoop:
+	for i := 0; i < t.NumField(); i++ {
+		f := t.Field(i)
+		for _, s := range strings.Split(f.Tag.Get("protobuf"), ",") {
+			if len(s) > 0 && strings.Trim(s, "0123456789") == "" {
+				n, _ := strconv.ParseUint(s, 10, 64)
+				fields[pref.FieldNumber(n)] = f
+				continue fieldLoop
+			}
+		}
+		if s := f.Tag.Get("protobuf_oneof"); len(s) > 0 {
+			oneofs[pref.Name(s)] = f
+			continue fieldLoop
+		}
+		switch f.Name {
+		case "XXX_weak", "XXX_unrecognized", "XXX_sizecache", "XXX_extensions", "XXX_InternalExtensions":
+			special[f.Name] = f
+			continue fieldLoop
+		}
+	}
+	if fn, ok := t.MethodByName("XXX_OneofFuncs"); ok {
+		vs := fn.Func.Call([]reflect.Value{reflect.New(fn.Type.In(0)).Elem()})[3]
+	oneofLoop:
+		for _, v := range vs.Interface().([]interface{}) {
+			tf := reflect.TypeOf(v).Elem()
+			f := tf.Field(0)
+			for _, s := range strings.Split(f.Tag.Get("protobuf"), ",") {
+				if len(s) > 0 && strings.Trim(s, "0123456789") == "" {
+					n, _ := strconv.ParseUint(s, 10, 64)
+					oneofFields[pref.FieldNumber(n)] = tf
+					continue oneofLoop
+				}
+			}
+		}
+	}
+
+	mi.fields = map[pref.FieldNumber]*fieldInfo{}
+	for i := 0; i < md.Fields().Len(); i++ {
+		fd := md.Fields().Get(i)
+		fs := fields[fd.Number()]
+		var fi fieldInfo
+		switch {
+		case fd.IsWeak():
+			fi = fieldInfoForWeak(fd, special["XXX_weak"])
+		case fd.OneofType() != nil:
+			fi = fieldInfoForOneof(fd, oneofs[fd.OneofType().Name()], oneofFields[fd.Number()])
+		case fd.IsMap():
+			fi = fieldInfoForMap(fd, fs)
+		case fd.Cardinality() == pref.Repeated:
+			fi = fieldInfoForVector(fd, fs)
+		case fd.Kind() != pref.MessageKind && fd.Kind() != pref.GroupKind:
+			fi = fieldInfoForScalar(fd, fs)
+		default:
+			fi = fieldInfoForMessage(fd, fs)
+		}
+		mi.fields[fd.Number()] = &fi
+	}
+}
diff --git a/internal/impl/message_field.go b/internal/impl/message_field.go
new file mode 100644
index 0000000..eb3a2cf
--- /dev/null
+++ b/internal/impl/message_field.go
@@ -0,0 +1,250 @@
+// Copyright 2018 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package impl
+
+import (
+	"fmt"
+	"reflect"
+
+	"google.golang.org/proto/internal/flags"
+	pref "google.golang.org/proto/reflect/protoreflect"
+)
+
+type fieldInfo struct {
+	// TODO: specialize marshal and unmarshal functions?
+
+	has     func(pointer) bool
+	get     func(pointer) pref.Value
+	set     func(pointer, pref.Value)
+	clear   func(pointer)
+	mutable func(pointer) pref.Mutable
+}
+
+func fieldInfoForWeak(fd pref.FieldDescriptor, fs reflect.StructField) fieldInfo {
+	if !flags.Proto1Legacy {
+		panic("weak fields not supported")
+	}
+	// TODO: support weak fields.
+	panic(fmt.Sprintf("invalid field: %v", fd))
+}
+
+func fieldInfoForOneof(fd pref.FieldDescriptor, fs reflect.StructField, ot reflect.Type) fieldInfo {
+	// TODO: support oneof fields.
+	panic(fmt.Sprintf("invalid field: %v", fd))
+}
+
+func fieldInfoForMap(fd pref.FieldDescriptor, fs reflect.StructField) fieldInfo {
+	// TODO: support map fields.
+	panic(fmt.Sprintf("invalid field: %v", fd))
+}
+
+func fieldInfoForVector(fd pref.FieldDescriptor, fs reflect.StructField) fieldInfo {
+	// TODO: support vector fields.
+	panic(fmt.Sprintf("invalid field: %v", fd))
+}
+
+var emptyBytes = reflect.ValueOf([]byte{})
+
+func fieldInfoForScalar(fd pref.FieldDescriptor, fs reflect.StructField) fieldInfo {
+	ft := fs.Type
+	nullable := fd.Syntax() == pref.Proto2
+	if nullable {
+		if ft.Kind() != reflect.Ptr && ft.Kind() != reflect.Slice {
+			panic(fmt.Sprintf("invalid type: got %v, want pointer", ft))
+		}
+		if ft.Kind() == reflect.Ptr {
+			ft = ft.Elem()
+		}
+	}
+	conv := matchGoTypePBKind(ft, fd.Kind())
+	fieldOffset := offsetOf(fs)
+	// TODO: Implement unsafe fast path?
+	return fieldInfo{
+		has: func(p pointer) bool {
+			rv := p.apply(fieldOffset).asType(fs.Type).Elem()
+			if nullable {
+				return !rv.IsNil()
+			}
+			switch rv.Kind() {
+			case reflect.Bool:
+				return rv.Bool()
+			case reflect.Int32, reflect.Int64:
+				return rv.Int() > 0
+			case reflect.Uint32, reflect.Uint64:
+				return rv.Uint() > 0
+			case reflect.Float32, reflect.Float64:
+				return rv.Float() > 0
+			case reflect.String, reflect.Slice:
+				return rv.Len() > 0
+			default:
+				panic(fmt.Sprintf("invalid type: %v", rv.Type())) // should never happen
+			}
+		},
+		get: func(p pointer) pref.Value {
+			rv := p.apply(fieldOffset).asType(fs.Type).Elem()
+			if nullable {
+				if rv.IsNil() {
+					pv := fd.Default()
+					if fd.Kind() == pref.BytesKind && len(pv.Bytes()) > 0 {
+						return pref.ValueOf(append([]byte(nil), pv.Bytes()...)) // copy default bytes for safety
+					}
+					return pv
+				}
+				if rv.Kind() == reflect.Ptr {
+					rv = rv.Elem()
+				}
+			}
+			return conv.toPB(rv)
+		},
+		set: func(p pointer, v pref.Value) {
+			rv := p.apply(fieldOffset).asType(fs.Type).Elem()
+			if nullable && rv.Kind() == reflect.Ptr {
+				if rv.IsNil() {
+					rv.Set(reflect.New(ft))
+				}
+				rv = rv.Elem()
+			}
+			rv.Set(conv.toGo(v))
+			if nullable && rv.Kind() == reflect.Slice && rv.IsNil() {
+				rv.Set(emptyBytes)
+			}
+		},
+		clear: func(p pointer) {
+			rv := p.apply(fieldOffset).asType(fs.Type).Elem()
+			rv.Set(reflect.Zero(rv.Type()))
+		},
+		mutable: func(p pointer) pref.Mutable {
+			panic("invalid mutable call")
+		},
+	}
+}
+
+func fieldInfoForMessage(fd pref.FieldDescriptor, fs reflect.StructField) fieldInfo {
+	// TODO: support vector fields.
+	panic(fmt.Sprintf("invalid field: %v", fd))
+}
+
+// messageV1 is the protoV1.Message interface.
+type messageV1 interface {
+	Reset()
+	String() string
+	ProtoMessage()
+}
+
+var (
+	boolType    = reflect.TypeOf(bool(false))
+	int32Type   = reflect.TypeOf(int32(0))
+	int64Type   = reflect.TypeOf(int64(0))
+	uint32Type  = reflect.TypeOf(uint32(0))
+	uint64Type  = reflect.TypeOf(uint64(0))
+	float32Type = reflect.TypeOf(float32(0))
+	float64Type = reflect.TypeOf(float64(0))
+	stringType  = reflect.TypeOf(string(""))
+	bytesType   = reflect.TypeOf([]byte(nil))
+
+	enumIfaceV2    = reflect.TypeOf((*pref.ProtoEnum)(nil)).Elem()
+	messageIfaceV1 = reflect.TypeOf((*messageV1)(nil)).Elem()
+	messageIfaceV2 = reflect.TypeOf((*pref.ProtoMessage)(nil)).Elem()
+
+	byteType = reflect.TypeOf(byte(0))
+)
+
+// matchGoTypePBKind matches a Go type with the protobuf kind.
+//
+// This matcher deliberately supports a wider range of Go types than what
+// protoc-gen-go historically generated to be able to automatically wrap some
+// v1 messages generated by other forks of protoc-gen-go.
+func matchGoTypePBKind(t reflect.Type, k pref.Kind) converter {
+	switch k {
+	case pref.BoolKind:
+		if t.Kind() == reflect.Bool {
+			return makeScalarConverter(t, boolType)
+		}
+	case pref.Int32Kind, pref.Sint32Kind, pref.Sfixed32Kind:
+		if t.Kind() == reflect.Int32 {
+			return makeScalarConverter(t, int32Type)
+		}
+	case pref.Int64Kind, pref.Sint64Kind, pref.Sfixed64Kind:
+		if t.Kind() == reflect.Int64 {
+			return makeScalarConverter(t, int64Type)
+		}
+	case pref.Uint32Kind, pref.Fixed32Kind:
+		if t.Kind() == reflect.Uint32 {
+			return makeScalarConverter(t, uint32Type)
+		}
+	case pref.Uint64Kind, pref.Fixed64Kind:
+		if t.Kind() == reflect.Uint64 {
+			return makeScalarConverter(t, uint64Type)
+		}
+	case pref.FloatKind:
+		if t.Kind() == reflect.Float32 {
+			return makeScalarConverter(t, float32Type)
+		}
+	case pref.DoubleKind:
+		if t.Kind() == reflect.Float64 {
+			return makeScalarConverter(t, float64Type)
+		}
+	case pref.StringKind:
+		if t.Kind() == reflect.String || (t.Kind() == reflect.Slice && t.Elem() == byteType) {
+			return makeScalarConverter(t, stringType)
+		}
+	case pref.BytesKind:
+		if t.Kind() == reflect.String || (t.Kind() == reflect.Slice && t.Elem() == byteType) {
+			return makeScalarConverter(t, bytesType)
+		}
+	case pref.EnumKind:
+		// Handle v2 enums, which must satisfy the proto.Enum interface.
+		if t.Kind() != reflect.Ptr && t.Implements(enumIfaceV2) {
+			// TODO: implement this.
+		}
+
+		// Handle v1 enums, which we identify as simply a named int32 type.
+		if t.Kind() == reflect.Int32 && t.PkgPath() != "" {
+			// TODO: need logic to wrap a legacy enum to implement this.
+		}
+	case pref.MessageKind, pref.GroupKind:
+		// Handle v2 messages, which must satisfy the proto.Message interface.
+		if t.Kind() == reflect.Ptr && t.Implements(messageIfaceV2) {
+			// TODO: implement this.
+		}
+
+		// Handle v1 messages, which we need to wrap as a v2 message.
+		if t.Kind() == reflect.Ptr && t.Implements(messageIfaceV1) {
+			// TODO: need logic to wrap a legacy message.
+		}
+	}
+	panic(fmt.Sprintf("invalid Go type %v for protobuf kind %v", t, k))
+}
+
+// converter provides functions for converting to/from Go reflect.Value types
+// and protobuf protoreflect.Value types.
+type converter struct {
+	toPB func(reflect.Value) pref.Value
+	toGo func(pref.Value) reflect.Value
+}
+
+func makeScalarConverter(goType, pbType reflect.Type) converter {
+	return converter{
+		toPB: func(v reflect.Value) pref.Value {
+			if v.Type() != goType {
+				panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), goType))
+			}
+			if goType.Kind() == reflect.String && pbType.Kind() == reflect.Slice && v.Len() == 0 {
+				return pref.ValueOf([]byte(nil)) // ensure empty string is []byte(nil)
+			}
+			return pref.ValueOf(v.Convert(pbType).Interface())
+		},
+		toGo: func(v pref.Value) reflect.Value {
+			rv := reflect.ValueOf(v.Interface())
+			if rv.Type() != pbType {
+				panic(fmt.Sprintf("invalid type: got %v, want %v", rv.Type(), pbType))
+			}
+			if pbType.Kind() == reflect.String && goType.Kind() == reflect.Slice && rv.Len() == 0 {
+				return reflect.Zero(goType) // ensure empty string is []byte(nil)
+			}
+			return rv.Convert(goType)
+		},
+	}
+}
diff --git a/internal/impl/message_test.go b/internal/impl/message_test.go
new file mode 100644
index 0000000..f9d674a
--- /dev/null
+++ b/internal/impl/message_test.go
@@ -0,0 +1,292 @@
+// Copyright 2018 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package impl
+
+import (
+	"reflect"
+	"testing"
+
+	"github.com/google/go-cmp/cmp"
+
+	pref "google.golang.org/proto/reflect/protoreflect"
+	ptype "google.golang.org/proto/reflect/prototype"
+)
+
+type (
+	MyBool    bool
+	MyInt32   int32
+	MyInt64   int64
+	MyUint32  uint32
+	MyUint64  uint64
+	MyFloat32 float32
+	MyFloat64 float64
+	MyString  string
+	MyBytes   []byte
+)
+
+type ScalarProto2 struct {
+	Bool    *bool    `protobuf:"1"`
+	Int32   *int32   `protobuf:"2"`
+	Int64   *int64   `protobuf:"3"`
+	Uint32  *uint32  `protobuf:"4"`
+	Uint64  *uint64  `protobuf:"5"`
+	Float32 *float32 `protobuf:"6"`
+	Float64 *float64 `protobuf:"7"`
+	String  *string  `protobuf:"8"`
+	StringA []byte   `protobuf:"9"`
+	Bytes   []byte   `protobuf:"10"`
+	BytesA  *string  `protobuf:"11"`
+
+	MyBool    *MyBool    `protobuf:"12"`
+	MyInt32   *MyInt32   `protobuf:"13"`
+	MyInt64   *MyInt64   `protobuf:"14"`
+	MyUint32  *MyUint32  `protobuf:"15"`
+	MyUint64  *MyUint64  `protobuf:"16"`
+	MyFloat32 *MyFloat32 `protobuf:"17"`
+	MyFloat64 *MyFloat64 `protobuf:"18"`
+	MyString  *MyString  `protobuf:"19"`
+	MyStringA MyBytes    `protobuf:"20"`
+	MyBytes   MyBytes    `protobuf:"21"`
+	MyBytesA  *MyString  `protobuf:"22"`
+}
+
+type ScalarProto3 struct {
+	Bool    bool    `protobuf:"1"`
+	Int32   int32   `protobuf:"2"`
+	Int64   int64   `protobuf:"3"`
+	Uint32  uint32  `protobuf:"4"`
+	Uint64  uint64  `protobuf:"5"`
+	Float32 float32 `protobuf:"6"`
+	Float64 float64 `protobuf:"7"`
+	String  string  `protobuf:"8"`
+	StringA []byte  `protobuf:"9"`
+	Bytes   []byte  `protobuf:"10"`
+	BytesA  string  `protobuf:"11"`
+
+	MyBool    MyBool    `protobuf:"12"`
+	MyInt32   MyInt32   `protobuf:"13"`
+	MyInt64   MyInt64   `protobuf:"14"`
+	MyUint32  MyUint32  `protobuf:"15"`
+	MyUint64  MyUint64  `protobuf:"16"`
+	MyFloat32 MyFloat32 `protobuf:"17"`
+	MyFloat64 MyFloat64 `protobuf:"18"`
+	MyString  MyString  `protobuf:"19"`
+	MyStringA MyBytes   `protobuf:"20"`
+	MyBytes   MyBytes   `protobuf:"21"`
+	MyBytesA  MyString  `protobuf:"22"`
+}
+
+func TestFieldFuncs(t *testing.T) {
+	V := pref.ValueOf
+	type (
+		// has checks that each field matches the list.
+		hasOp []bool
+		// get checks that each field returns values matching the list.
+		getOp []pref.Value
+		// set calls set on each field with the given value in the list.
+		setOp []pref.Value
+		// clear calls clear on each field.
+		clearOp []bool
+		// equal checks that the current message equals the provided value.
+		equalOp struct{ want interface{} }
+
+		testOp interface{} // has | get | set | clear | equal
+	)
+
+	tests := []struct {
+		structType  reflect.Type
+		messageDesc ptype.StandaloneMessage
+		testOps     []testOp
+	}{{
+		structType: reflect.TypeOf(ScalarProto2{}),
+		messageDesc: ptype.StandaloneMessage{
+			Syntax:   pref.Proto2,
+			FullName: "ScalarProto2",
+			Fields: []ptype.Field{
+				{Name: "f1", Number: 1, Cardinality: pref.Optional, Kind: pref.BoolKind, Default: V(bool(true))},
+				{Name: "f2", Number: 2, Cardinality: pref.Optional, Kind: pref.Int32Kind, Default: V(int32(2))},
+				{Name: "f3", Number: 3, Cardinality: pref.Optional, Kind: pref.Int64Kind, Default: V(int64(3))},
+				{Name: "f4", Number: 4, Cardinality: pref.Optional, Kind: pref.Uint32Kind, Default: V(uint32(4))},
+				{Name: "f5", Number: 5, Cardinality: pref.Optional, Kind: pref.Uint64Kind, Default: V(uint64(5))},
+				{Name: "f6", Number: 6, Cardinality: pref.Optional, Kind: pref.FloatKind, Default: V(float32(6))},
+				{Name: "f7", Number: 7, Cardinality: pref.Optional, Kind: pref.DoubleKind, Default: V(float64(7))},
+				{Name: "f8", Number: 8, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("8"))},
+				{Name: "f9", Number: 9, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("9"))},
+				{Name: "f10", Number: 10, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("10"))},
+				{Name: "f11", Number: 11, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("11"))},
+
+				{Name: "f12", Number: 12, Cardinality: pref.Optional, Kind: pref.BoolKind, Default: V(bool(true))},
+				{Name: "f13", Number: 13, Cardinality: pref.Optional, Kind: pref.Int32Kind, Default: V(int32(13))},
+				{Name: "f14", Number: 14, Cardinality: pref.Optional, Kind: pref.Int64Kind, Default: V(int64(14))},
+				{Name: "f15", Number: 15, Cardinality: pref.Optional, Kind: pref.Uint32Kind, Default: V(uint32(15))},
+				{Name: "f16", Number: 16, Cardinality: pref.Optional, Kind: pref.Uint64Kind, Default: V(uint64(16))},
+				{Name: "f17", Number: 17, Cardinality: pref.Optional, Kind: pref.FloatKind, Default: V(float32(17))},
+				{Name: "f18", Number: 18, Cardinality: pref.Optional, Kind: pref.DoubleKind, Default: V(float64(18))},
+				{Name: "f19", Number: 19, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("19"))},
+				{Name: "f20", Number: 20, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("20"))},
+				{Name: "f21", Number: 21, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("21"))},
+				{Name: "f22", Number: 22, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("22"))},
+			},
+		},
+		testOps: []testOp{
+			hasOp([]bool{
+				false, false, false, false, false, false, false, false, false, false, false,
+				false, false, false, false, false, false, false, false, false, false, false,
+			}),
+			getOp([]pref.Value{
+				V(bool(true)), V(int32(2)), V(int64(3)), V(uint32(4)), V(uint64(5)), V(float32(6)), V(float64(7)), V(string("8")), V(string("9")), V([]byte("10")), V([]byte("11")),
+				V(bool(true)), V(int32(13)), V(int64(14)), V(uint32(15)), V(uint64(16)), V(float32(17)), V(float64(18)), V(string("19")), V(string("20")), V([]byte("21")), V([]byte("22")),
+			}),
+			setOp([]pref.Value{
+				V(bool(false)), V(int32(0)), V(int64(0)), V(uint32(0)), V(uint64(0)), V(float32(0)), V(float64(0)), V(string("")), V(string("")), V([]byte(nil)), V([]byte(nil)),
+				V(bool(false)), V(int32(0)), V(int64(0)), V(uint32(0)), V(uint64(0)), V(float32(0)), V(float64(0)), V(string("")), V(string("")), V([]byte(nil)), V([]byte(nil)),
+			}),
+			hasOp([]bool{
+				true, true, true, true, true, true, true, true, true, true, true,
+				true, true, true, true, true, true, true, true, true, true, true,
+			}),
+			equalOp{&ScalarProto2{
+				new(bool), new(int32), new(int64), new(uint32), new(uint64), new(float32), new(float64), new(string), []byte{}, []byte{}, new(string),
+				new(MyBool), new(MyInt32), new(MyInt64), new(MyUint32), new(MyUint64), new(MyFloat32), new(MyFloat64), new(MyString), MyBytes{}, MyBytes{}, new(MyString),
+			}},
+			clearOp([]bool{
+				true, true, true, true, true, true, true, true, true, true, true,
+				true, true, true, true, true, true, true, true, true, true, true,
+			}),
+			equalOp{&ScalarProto2{}},
+		},
+	}, {
+		structType: reflect.TypeOf(ScalarProto3{}),
+		messageDesc: ptype.StandaloneMessage{
+			Syntax:   pref.Proto3,
+			FullName: "ScalarProto3",
+			Fields: []ptype.Field{
+				{Name: "f1", Number: 1, Cardinality: pref.Optional, Kind: pref.BoolKind},
+				{Name: "f2", Number: 2, Cardinality: pref.Optional, Kind: pref.Int32Kind},
+				{Name: "f3", Number: 3, Cardinality: pref.Optional, Kind: pref.Int64Kind},
+				{Name: "f4", Number: 4, Cardinality: pref.Optional, Kind: pref.Uint32Kind},
+				{Name: "f5", Number: 5, Cardinality: pref.Optional, Kind: pref.Uint64Kind},
+				{Name: "f6", Number: 6, Cardinality: pref.Optional, Kind: pref.FloatKind},
+				{Name: "f7", Number: 7, Cardinality: pref.Optional, Kind: pref.DoubleKind},
+				{Name: "f8", Number: 8, Cardinality: pref.Optional, Kind: pref.StringKind},
+				{Name: "f9", Number: 9, Cardinality: pref.Optional, Kind: pref.StringKind},
+				{Name: "f10", Number: 10, Cardinality: pref.Optional, Kind: pref.BytesKind},
+				{Name: "f11", Number: 11, Cardinality: pref.Optional, Kind: pref.BytesKind},
+
+				{Name: "f12", Number: 12, Cardinality: pref.Optional, Kind: pref.BoolKind},
+				{Name: "f13", Number: 13, Cardinality: pref.Optional, Kind: pref.Int32Kind},
+				{Name: "f14", Number: 14, Cardinality: pref.Optional, Kind: pref.Int64Kind},
+				{Name: "f15", Number: 15, Cardinality: pref.Optional, Kind: pref.Uint32Kind},
+				{Name: "f16", Number: 16, Cardinality: pref.Optional, Kind: pref.Uint64Kind},
+				{Name: "f17", Number: 17, Cardinality: pref.Optional, Kind: pref.FloatKind},
+				{Name: "f18", Number: 18, Cardinality: pref.Optional, Kind: pref.DoubleKind},
+				{Name: "f19", Number: 19, Cardinality: pref.Optional, Kind: pref.StringKind},
+				{Name: "f20", Number: 20, Cardinality: pref.Optional, Kind: pref.StringKind},
+				{Name: "f21", Number: 21, Cardinality: pref.Optional, Kind: pref.BytesKind},
+				{Name: "f22", Number: 22, Cardinality: pref.Optional, Kind: pref.BytesKind},
+			},
+		},
+		testOps: []testOp{
+			hasOp([]bool{
+				false, false, false, false, false, false, false, false, false, false, false,
+				false, false, false, false, false, false, false, false, false, false, false,
+			}),
+			getOp([]pref.Value{
+				V(bool(false)), V(int32(0)), V(int64(0)), V(uint32(0)), V(uint64(0)), V(float32(0)), V(float64(0)), V(string("")), V(string("")), V([]byte(nil)), V([]byte(nil)),
+				V(bool(false)), V(int32(0)), V(int64(0)), V(uint32(0)), V(uint64(0)), V(float32(0)), V(float64(0)), V(string("")), V(string("")), V([]byte(nil)), V([]byte(nil)),
+			}),
+			setOp([]pref.Value{
+				V(bool(false)), V(int32(0)), V(int64(0)), V(uint32(0)), V(uint64(0)), V(float32(0)), V(float64(0)), V(string("")), V(string("")), V([]byte(nil)), V([]byte(nil)),
+				V(bool(false)), V(int32(0)), V(int64(0)), V(uint32(0)), V(uint64(0)), V(float32(0)), V(float64(0)), V(string("")), V(string("")), V([]byte(nil)), V([]byte(nil)),
+			}),
+			hasOp([]bool{
+				false, false, false, false, false, false, false, false, false, false, false,
+				false, false, false, false, false, false, false, false, false, false, false,
+			}),
+			equalOp{&ScalarProto3{}},
+			setOp([]pref.Value{
+				V(bool(true)), V(int32(2)), V(int64(3)), V(uint32(4)), V(uint64(5)), V(float32(6)), V(float64(7)), V(string("8")), V(string("9")), V([]byte("10")), V([]byte("11")),
+				V(bool(true)), V(int32(13)), V(int64(14)), V(uint32(15)), V(uint64(16)), V(float32(17)), V(float64(18)), V(string("19")), V(string("20")), V([]byte("21")), V([]byte("22")),
+			}),
+			hasOp([]bool{
+				true, true, true, true, true, true, true, true, true, true, true,
+				true, true, true, true, true, true, true, true, true, true, true,
+			}),
+			equalOp{&ScalarProto3{
+				true, 2, 3, 4, 5, 6, 7, "8", []byte("9"), []byte("10"), "11",
+				true, 13, 14, 15, 16, 17, 18, "19", []byte("20"), []byte("21"), "22",
+			}},
+			clearOp([]bool{
+				true, true, true, true, true, true, true, true, true, true, true,
+				true, true, true, true, true, true, true, true, true, true, true,
+			}),
+			equalOp{&ScalarProto3{}},
+		},
+	}}
+
+	for _, tt := range tests {
+		t.Run(tt.structType.Name(), func(t *testing.T) {
+			// Construct the message descriptor.
+			md, err := ptype.NewMessage(&tt.messageDesc)
+			if err != nil {
+				t.Fatalf("NewMessage error: %v", err)
+			}
+
+			// Generate the field functions from the message descriptor.
+			var mi MessageInfo
+			mi.generateFieldFuncs(tt.structType, md) // must not panic
+
+			// Test the field functions.
+			m := reflect.New(tt.structType)
+			p := pointerOfValue(m)
+			for i, op := range tt.testOps {
+				switch op := op.(type) {
+				case hasOp:
+					got := map[pref.FieldNumber]bool{}
+					want := map[pref.FieldNumber]bool{}
+					for j, ok := range op {
+						n := pref.FieldNumber(j + 1)
+						got[n] = mi.fields[n].has(p)
+						want[n] = ok
+					}
+					if diff := cmp.Diff(want, got); diff != "" {
+						t.Errorf("operation %d, has mismatch (-want, +got):\n%s", i, diff)
+					}
+				case getOp:
+					got := map[pref.FieldNumber]pref.Value{}
+					want := map[pref.FieldNumber]pref.Value{}
+					for j, v := range op {
+						n := pref.FieldNumber(j + 1)
+						got[n] = mi.fields[n].get(p)
+						want[n] = v
+					}
+					xformValue := cmp.Transformer("", func(v pref.Value) interface{} {
+						return v.Interface()
+					})
+					if diff := cmp.Diff(want, got, xformValue); diff != "" {
+						t.Errorf("operation %d, get mismatch (-want, +got):\n%s", i, diff)
+					}
+				case setOp:
+					for j, v := range op {
+						n := pref.FieldNumber(j + 1)
+						mi.fields[n].set(p, v)
+					}
+				case clearOp:
+					for j, ok := range op {
+						n := pref.FieldNumber(j + 1)
+						if ok {
+							mi.fields[n].clear(p)
+						}
+					}
+				case equalOp:
+					got := m.Interface()
+					if diff := cmp.Diff(op.want, got); diff != "" {
+						t.Errorf("operation %d, equal mismatch (-want, +got):\n%s", i, diff)
+					}
+				}
+			}
+		})
+	}
+}
diff --git a/internal/impl/pointer_reflect.go b/internal/impl/pointer_reflect.go
new file mode 100644
index 0000000..2b6bbf9
--- /dev/null
+++ b/internal/impl/pointer_reflect.go
@@ -0,0 +1,47 @@
+// Copyright 2018 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// +build purego
+
+package impl
+
+import (
+	"fmt"
+	"reflect"
+)
+
+// offset represents the offset to a struct field, accessible from a pointer.
+// The offset is the field index into a struct.
+type offset []int
+
+// offsetOf returns a field offset for the struct field.
+func offsetOf(f reflect.StructField) offset {
+	if len(f.Index) != 1 {
+		panic("embedded structs are not supported")
+	}
+	return f.Index
+}
+
+// pointer is an abstract representation of a pointer to a struct or field.
+type pointer struct{ v reflect.Value }
+
+// pointerOfValue returns v as a pointer.
+func pointerOfValue(v reflect.Value) pointer {
+	return pointer{v: v}
+}
+
+// apply adds an offset to the pointer to derive a new pointer
+// to a specified field. The current pointer must be pointing at a struct.
+func (p pointer) apply(f offset) pointer {
+	// TODO: Handle unexported fields in an API that hides XXX fields?
+	return pointer{v: p.v.Elem().FieldByIndex(f).Addr()}
+}
+
+// asType treats p as a pointer to an object of type t and returns the value.
+func (p pointer) asType(t reflect.Type) reflect.Value {
+	if p.v.Type().Elem() != t {
+		panic(fmt.Sprintf("invalid type: got %v, want %v", p.v.Type(), t))
+	}
+	return p.v
+}
diff --git a/internal/impl/pointer_unsafe.go b/internal/impl/pointer_unsafe.go
new file mode 100644
index 0000000..c7286e1
--- /dev/null
+++ b/internal/impl/pointer_unsafe.go
@@ -0,0 +1,40 @@
+// Copyright 2018 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// +build !purego
+
+package impl
+
+import (
+	"reflect"
+	"unsafe"
+)
+
+// offset represents the offset to a struct field, accessible from a pointer.
+// The offset is the byte offset to the field from the start of the struct.
+type offset uintptr
+
+// offsetOf returns a field offset for the struct field.
+func offsetOf(f reflect.StructField) offset {
+	return offset(f.Offset)
+}
+
+// pointer is a pointer to a message struct or field.
+type pointer struct{ p unsafe.Pointer }
+
+// pointerOfValue returns v as a pointer.
+func pointerOfValue(v reflect.Value) pointer {
+	return pointer{p: unsafe.Pointer(v.Pointer())}
+}
+
+// apply adds an offset to the pointer to derive a new pointer
+// to a specified field. The current pointer must be pointing at a struct.
+func (p pointer) apply(f offset) pointer {
+	return pointer{p: unsafe.Pointer(uintptr(p.p) + uintptr(f))}
+}
+
+// asType treats p as a pointer to an object of type t and returns the value.
+func (p pointer) asType(t reflect.Type) reflect.Value {
+	return reflect.NewAt(t, p.p)
+}