blob: e464beff23ac3b783d9dd32f5857fe5db9ffcb91 [file] [log] [blame]
Joe Tsaifa02f4e2018-09-12 16:20:37 -07001// Copyright 2018 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package impl
6
7import (
Joe Tsaic6b75612018-09-13 14:24:37 -07008 "fmt"
Joe Tsaifa02f4e2018-09-12 16:20:37 -07009 "reflect"
10 "strconv"
11 "strings"
Joe Tsaic6b75612018-09-13 14:24:37 -070012 "sync"
Damien Neilc37adef2019-04-01 13:49:56 -070013 "sync/atomic"
Joe Tsaifa02f4e2018-09-12 16:20:37 -070014
Joe Tsai4663ebc2019-11-13 17:28:51 -080015 "google.golang.org/protobuf/internal/genname"
Damien Neil16163b42019-08-06 15:43:25 -070016 "google.golang.org/protobuf/reflect/protoreflect"
Damien Neile89e6242019-05-13 23:55:40 -070017 pref "google.golang.org/protobuf/reflect/protoreflect"
18 piface "google.golang.org/protobuf/runtime/protoiface"
Joe Tsaifa02f4e2018-09-12 16:20:37 -070019)
20
Joe Tsai4fe96632019-05-22 05:12:36 -040021// MessageInfo provides protobuf related functionality for a given Go type
22// that represents a message. A given instance of MessageInfo is tied to
Joe Tsaic6b75612018-09-13 14:24:37 -070023// exactly one Go type, which must be a pointer to a struct type.
Joe Tsai0484b1a2019-08-13 15:36:08 -070024//
25// The exported fields must be populated before any methods are called
26// and cannot be mutated after set.
Joe Tsai4fe96632019-05-22 05:12:36 -040027type MessageInfo struct {
Damien Neil16163b42019-08-06 15:43:25 -070028 // GoReflectType is the underlying message Go type and must be populated.
Damien Neil16163b42019-08-06 15:43:25 -070029 GoReflectType reflect.Type // pointer to struct
Damien Neil8012b442019-01-18 09:32:24 -080030
Damien Neil16163b42019-08-06 15:43:25 -070031 // Desc is the underlying message descriptor type and must be populated.
Damien Neil16163b42019-08-06 15:43:25 -070032 Desc pref.MessageDescriptor
Joe Tsaic6b75612018-09-13 14:24:37 -070033
Joe Tsaic0e4bb22019-07-06 13:05:11 -070034 // Exporter must be provided in a purego environment in order to provide
35 // access to unexported fields.
36 Exporter exporter
37
Joe Tsai09912272019-07-08 10:38:11 -070038 // OneofWrappers is list of pointers to oneof wrapper struct types.
39 OneofWrappers []interface{}
40
Damien Neilc37adef2019-04-01 13:49:56 -070041 initMu sync.Mutex // protects all unexported fields
42 initDone uint32
Joe Tsaic6b75612018-09-13 14:24:37 -070043
Joe Tsai0484b1a2019-08-13 15:36:08 -070044 reflectMessageInfo // for reflection implementation
45 coderMessageInfo // for fast-path method implementations
Joe Tsai82760ce2019-06-20 03:09:57 -070046}
47
Joe Tsaic0e4bb22019-07-06 13:05:11 -070048// exporter is a function that returns a reference to the ith field of v,
49// where v is a pointer to a struct. It returns nil if it does not support
50// exporting the requested field (e.g., already exported).
51type exporter func(v interface{}, i int) interface{}
52
Joe Tsai070c1012019-07-26 23:45:58 -070053// getMessageInfo returns the MessageInfo for any message type that
54// is generated by our implementation of protoc-gen-go (for v2 and on).
55// If it is unable to obtain a MessageInfo, it returns nil.
56func getMessageInfo(mt reflect.Type) *MessageInfo {
57 m, ok := reflect.Zero(mt).Interface().(pref.ProtoMessage)
Damien Neilc37adef2019-04-01 13:49:56 -070058 if !ok {
Joe Tsai070c1012019-07-26 23:45:58 -070059 return nil
Damien Neilc37adef2019-04-01 13:49:56 -070060 }
Joe Tsai070c1012019-07-26 23:45:58 -070061 mr, ok := m.ProtoReflect().(interface{ ProtoMessageInfo() *MessageInfo })
Damien Neilc37adef2019-04-01 13:49:56 -070062 if !ok {
Joe Tsai070c1012019-07-26 23:45:58 -070063 return nil
Damien Neilc37adef2019-04-01 13:49:56 -070064 }
Joe Tsai070c1012019-07-26 23:45:58 -070065 return mr.ProtoMessageInfo()
Joe Tsaifa02f4e2018-09-12 16:20:37 -070066}
67
Joe Tsai4fe96632019-05-22 05:12:36 -040068func (mi *MessageInfo) init() {
Joe Tsai0484b1a2019-08-13 15:36:08 -070069 // This function is called in the hot path. Inline the sync.Once logic,
70 // since allocating a closure for Once.Do is expensive.
Damien Neilc37adef2019-04-01 13:49:56 -070071 // Keep init small to ensure that it can be inlined.
Joe Tsai82760ce2019-06-20 03:09:57 -070072 if atomic.LoadUint32(&mi.initDone) == 0 {
73 mi.initOnce()
Damien Neilc37adef2019-04-01 13:49:56 -070074 }
Damien Neilc37adef2019-04-01 13:49:56 -070075}
Joe Tsaic6b75612018-09-13 14:24:37 -070076
Joe Tsai4fe96632019-05-22 05:12:36 -040077func (mi *MessageInfo) initOnce() {
Damien Neilc37adef2019-04-01 13:49:56 -070078 mi.initMu.Lock()
79 defer mi.initMu.Unlock()
80 if mi.initDone == 1 {
81 return
82 }
83
Damien Neil16163b42019-08-06 15:43:25 -070084 t := mi.GoReflectType
Damien Neilc37adef2019-04-01 13:49:56 -070085 if t.Kind() != reflect.Ptr && t.Elem().Kind() != reflect.Struct {
86 panic(fmt.Sprintf("got %v, want *struct kind", t))
87 }
Joe Tsai0484b1a2019-08-13 15:36:08 -070088 t = t.Elem()
Damien Neilc37adef2019-04-01 13:49:56 -070089
Joe Tsai0484b1a2019-08-13 15:36:08 -070090 si := mi.makeStructInfo(t)
91 mi.makeReflectFuncs(t, si)
92 mi.makeCoderMethods(t, si)
Damien Neilc37adef2019-04-01 13:49:56 -070093
94 atomic.StoreUint32(&mi.initDone, 1)
95}
96
Joe Tsai378c1322019-04-25 23:48:08 -070097type (
98 SizeCache = int32
Joe Tsai3d8e3692019-04-08 13:52:14 -070099 WeakFields = map[int32]piface.MessageV1
Joe Tsai378c1322019-04-25 23:48:08 -0700100 UnknownFields = []byte
101 ExtensionFields = map[int32]ExtensionField
102)
103
104var (
105 sizecacheType = reflect.TypeOf(SizeCache(0))
Joe Tsai3d8e3692019-04-08 13:52:14 -0700106 weakFieldsType = reflect.TypeOf(WeakFields(nil))
Joe Tsai378c1322019-04-25 23:48:08 -0700107 unknownFieldsType = reflect.TypeOf(UnknownFields(nil))
108 extensionFieldsType = reflect.TypeOf(ExtensionFields(nil))
109)
Damien Neilc37adef2019-04-01 13:49:56 -0700110
Damien Neil3eaddf02019-05-09 11:33:55 -0700111type structInfo struct {
Joe Tsai93fd9682019-07-06 13:02:14 -0700112 sizecacheOffset offset
Joe Tsai3d8e3692019-04-08 13:52:14 -0700113 weakOffset offset
Joe Tsai93fd9682019-07-06 13:02:14 -0700114 unknownOffset offset
Joe Tsaic0e4bb22019-07-06 13:05:11 -0700115 extensionOffset offset
Joe Tsai93fd9682019-07-06 13:02:14 -0700116
Damien Neil3eaddf02019-05-09 11:33:55 -0700117 fieldsByNumber map[pref.FieldNumber]reflect.StructField
118 oneofsByName map[pref.Name]reflect.StructField
119 oneofWrappersByType map[reflect.Type]pref.FieldNumber
120 oneofWrappersByNumber map[pref.FieldNumber]reflect.Type
121}
122
Joe Tsai4fe96632019-05-22 05:12:36 -0400123func (mi *MessageInfo) makeStructInfo(t reflect.Type) structInfo {
Damien Neil3eaddf02019-05-09 11:33:55 -0700124 si := structInfo{
Joe Tsai93fd9682019-07-06 13:02:14 -0700125 sizecacheOffset: invalidOffset,
Joe Tsai3d8e3692019-04-08 13:52:14 -0700126 weakOffset: invalidOffset,
Joe Tsai93fd9682019-07-06 13:02:14 -0700127 unknownOffset: invalidOffset,
Joe Tsaic0e4bb22019-07-06 13:05:11 -0700128 extensionOffset: invalidOffset,
Joe Tsai93fd9682019-07-06 13:02:14 -0700129
Damien Neil3eaddf02019-05-09 11:33:55 -0700130 fieldsByNumber: map[pref.FieldNumber]reflect.StructField{},
131 oneofsByName: map[pref.Name]reflect.StructField{},
132 oneofWrappersByType: map[reflect.Type]pref.FieldNumber{},
133 oneofWrappersByNumber: map[pref.FieldNumber]reflect.Type{},
134 }
Joe Tsai93fd9682019-07-06 13:02:14 -0700135
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700136fieldLoop:
137 for i := 0; i < t.NumField(); i++ {
Joe Tsai613285c2019-11-13 01:51:27 -0800138 switch f := t.Field(i); f.Name {
Joe Tsai4663ebc2019-11-13 17:28:51 -0800139 case genname.SizeCache, genname.SizeCacheA:
Joe Tsai613285c2019-11-13 01:51:27 -0800140 if f.Type == sizecacheType {
141 si.sizecacheOffset = offsetOf(f, mi.Exporter)
142 }
Joe Tsai4663ebc2019-11-13 17:28:51 -0800143 case genname.WeakFields, genname.WeakFieldsA:
Joe Tsai613285c2019-11-13 01:51:27 -0800144 if f.Type == weakFieldsType {
145 si.weakOffset = offsetOf(f, mi.Exporter)
146 }
Joe Tsai4663ebc2019-11-13 17:28:51 -0800147 case genname.UnknownFields, genname.UnknownFieldsA:
Joe Tsai613285c2019-11-13 01:51:27 -0800148 if f.Type == unknownFieldsType {
149 si.unknownOffset = offsetOf(f, mi.Exporter)
150 }
Joe Tsai4663ebc2019-11-13 17:28:51 -0800151 case genname.ExtensionFields, genname.ExtensionFieldsA, genname.ExtensionFieldsB:
Joe Tsai613285c2019-11-13 01:51:27 -0800152 if f.Type == extensionFieldsType {
153 si.extensionOffset = offsetOf(f, mi.Exporter)
154 }
155 default:
156 for _, s := range strings.Split(f.Tag.Get("protobuf"), ",") {
157 if len(s) > 0 && strings.Trim(s, "0123456789") == "" {
158 n, _ := strconv.ParseUint(s, 10, 64)
159 si.fieldsByNumber[pref.FieldNumber(n)] = f
160 continue fieldLoop
161 }
162 }
163 if s := f.Tag.Get("protobuf_oneof"); len(s) > 0 {
164 si.oneofsByName[pref.Name(s)] = f
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700165 continue fieldLoop
166 }
167 }
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700168 }
Joe Tsai93fd9682019-07-06 13:02:14 -0700169
170 // Derive a mapping of oneof wrappers to fields.
Joe Tsai09912272019-07-08 10:38:11 -0700171 oneofWrappers := mi.OneofWrappers
Joe Tsai2c870bb2018-10-17 11:46:52 -0700172 if fn, ok := reflect.PtrTo(t).MethodByName("XXX_OneofFuncs"); ok {
Joe Tsaid7e97bc2018-11-26 12:57:27 -0800173 oneofWrappers = fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[3].Interface().([]interface{})
174 }
175 if fn, ok := reflect.PtrTo(t).MethodByName("XXX_OneofWrappers"); ok {
176 oneofWrappers = fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[0].Interface().([]interface{})
177 }
178 for _, v := range oneofWrappers {
179 tf := reflect.TypeOf(v).Elem()
180 f := tf.Field(0)
181 for _, s := range strings.Split(f.Tag.Get("protobuf"), ",") {
182 if len(s) > 0 && strings.Trim(s, "0123456789") == "" {
183 n, _ := strconv.ParseUint(s, 10, 64)
Damien Neil3eaddf02019-05-09 11:33:55 -0700184 si.oneofWrappersByType[tf] = pref.FieldNumber(n)
185 si.oneofWrappersByNumber[pref.FieldNumber(n)] = tf
Joe Tsaid7e97bc2018-11-26 12:57:27 -0800186 break
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700187 }
188 }
189 }
Joe Tsai93fd9682019-07-06 13:02:14 -0700190
Damien Neil3eaddf02019-05-09 11:33:55 -0700191 return si
192}
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700193
Damien Neil16163b42019-08-06 15:43:25 -0700194func (mi *MessageInfo) New() protoreflect.Message {
195 return mi.MessageOf(reflect.New(mi.GoReflectType.Elem()).Interface())
196}
197func (mi *MessageInfo) Zero() protoreflect.Message {
198 return mi.MessageOf(reflect.Zero(mi.GoReflectType).Interface())
199}
200func (mi *MessageInfo) Descriptor() protoreflect.MessageDescriptor { return mi.Desc }