blob: a104e28e858fa12dc5e2e95c2e7c0d6c06cde3b1 [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 Tsaie0b77db2020-05-26 11:21:59 -070015 "google.golang.org/protobuf/internal/genid"
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"
Joe Tsai1a290e92020-06-05 15:31:25 -070018 preg "google.golang.org/protobuf/reflect/protoregistry"
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
Damien Neile8e88752020-02-11 11:25:16 -080097// getPointer returns the pointer for a message, which should be of
98// the type of the MessageInfo. If the message is of a different type,
99// it returns ok==false.
100func (mi *MessageInfo) getPointer(m pref.Message) (p pointer, ok bool) {
101 switch m := m.(type) {
102 case *messageState:
Damien Neil075e0742020-02-27 12:33:08 -0800103 return m.pointer(), m.messageInfo() == mi
Damien Neile8e88752020-02-11 11:25:16 -0800104 case *messageReflectWrapper:
Damien Neil075e0742020-02-27 12:33:08 -0800105 return m.pointer(), m.messageInfo() == mi
Damien Neile8e88752020-02-11 11:25:16 -0800106 }
107 return pointer{}, false
108}
109
Joe Tsai378c1322019-04-25 23:48:08 -0700110type (
111 SizeCache = int32
Joe Tsai3ebaa922020-04-23 00:57:01 -0700112 WeakFields = map[int32]protoreflect.ProtoMessage
Joe Tsai5d634732020-07-26 22:37:47 -0700113 UnknownFields = unknownFieldsA // TODO: switch to unknownFieldsB
114 unknownFieldsA = []byte
115 unknownFieldsB = *[]byte
Joe Tsai378c1322019-04-25 23:48:08 -0700116 ExtensionFields = map[int32]ExtensionField
117)
118
119var (
120 sizecacheType = reflect.TypeOf(SizeCache(0))
Joe Tsai3d8e3692019-04-08 13:52:14 -0700121 weakFieldsType = reflect.TypeOf(WeakFields(nil))
Joe Tsai5d634732020-07-26 22:37:47 -0700122 unknownFieldsAType = reflect.TypeOf(unknownFieldsA(nil))
123 unknownFieldsBType = reflect.TypeOf(unknownFieldsB(nil))
Joe Tsai378c1322019-04-25 23:48:08 -0700124 extensionFieldsType = reflect.TypeOf(ExtensionFields(nil))
125)
Damien Neilc37adef2019-04-01 13:49:56 -0700126
Damien Neil3eaddf02019-05-09 11:33:55 -0700127type structInfo struct {
Joe Tsai93fd9682019-07-06 13:02:14 -0700128 sizecacheOffset offset
Joe Tsai5d634732020-07-26 22:37:47 -0700129 sizecacheType reflect.Type
Joe Tsai3d8e3692019-04-08 13:52:14 -0700130 weakOffset offset
Joe Tsai5d634732020-07-26 22:37:47 -0700131 weakType reflect.Type
Joe Tsai93fd9682019-07-06 13:02:14 -0700132 unknownOffset offset
Joe Tsai5d634732020-07-26 22:37:47 -0700133 unknownType reflect.Type
Joe Tsaic0e4bb22019-07-06 13:05:11 -0700134 extensionOffset offset
Joe Tsai5d634732020-07-26 22:37:47 -0700135 extensionType reflect.Type
Joe Tsai93fd9682019-07-06 13:02:14 -0700136
Damien Neil3eaddf02019-05-09 11:33:55 -0700137 fieldsByNumber map[pref.FieldNumber]reflect.StructField
138 oneofsByName map[pref.Name]reflect.StructField
139 oneofWrappersByType map[reflect.Type]pref.FieldNumber
140 oneofWrappersByNumber map[pref.FieldNumber]reflect.Type
141}
142
Joe Tsai4fe96632019-05-22 05:12:36 -0400143func (mi *MessageInfo) makeStructInfo(t reflect.Type) structInfo {
Damien Neil3eaddf02019-05-09 11:33:55 -0700144 si := structInfo{
Joe Tsai93fd9682019-07-06 13:02:14 -0700145 sizecacheOffset: invalidOffset,
Joe Tsai3d8e3692019-04-08 13:52:14 -0700146 weakOffset: invalidOffset,
Joe Tsai93fd9682019-07-06 13:02:14 -0700147 unknownOffset: invalidOffset,
Joe Tsaic0e4bb22019-07-06 13:05:11 -0700148 extensionOffset: invalidOffset,
Joe Tsai93fd9682019-07-06 13:02:14 -0700149
Damien Neil3eaddf02019-05-09 11:33:55 -0700150 fieldsByNumber: map[pref.FieldNumber]reflect.StructField{},
151 oneofsByName: map[pref.Name]reflect.StructField{},
152 oneofWrappersByType: map[reflect.Type]pref.FieldNumber{},
153 oneofWrappersByNumber: map[pref.FieldNumber]reflect.Type{},
154 }
Joe Tsai93fd9682019-07-06 13:02:14 -0700155
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700156fieldLoop:
157 for i := 0; i < t.NumField(); i++ {
Joe Tsai613285c2019-11-13 01:51:27 -0800158 switch f := t.Field(i); f.Name {
Joe Tsaie0b77db2020-05-26 11:21:59 -0700159 case genid.SizeCache_goname, genid.SizeCacheA_goname:
Joe Tsai613285c2019-11-13 01:51:27 -0800160 if f.Type == sizecacheType {
161 si.sizecacheOffset = offsetOf(f, mi.Exporter)
Joe Tsai5d634732020-07-26 22:37:47 -0700162 si.sizecacheType = f.Type
Joe Tsai613285c2019-11-13 01:51:27 -0800163 }
Joe Tsaie0b77db2020-05-26 11:21:59 -0700164 case genid.WeakFields_goname, genid.WeakFieldsA_goname:
Joe Tsai613285c2019-11-13 01:51:27 -0800165 if f.Type == weakFieldsType {
166 si.weakOffset = offsetOf(f, mi.Exporter)
Joe Tsai5d634732020-07-26 22:37:47 -0700167 si.weakType = f.Type
Joe Tsai613285c2019-11-13 01:51:27 -0800168 }
Joe Tsaie0b77db2020-05-26 11:21:59 -0700169 case genid.UnknownFields_goname, genid.UnknownFieldsA_goname:
Joe Tsai5d634732020-07-26 22:37:47 -0700170 if f.Type == unknownFieldsAType || f.Type == unknownFieldsBType {
Joe Tsai613285c2019-11-13 01:51:27 -0800171 si.unknownOffset = offsetOf(f, mi.Exporter)
Joe Tsai5d634732020-07-26 22:37:47 -0700172 si.unknownType = f.Type
Joe Tsai613285c2019-11-13 01:51:27 -0800173 }
Joe Tsaie0b77db2020-05-26 11:21:59 -0700174 case genid.ExtensionFields_goname, genid.ExtensionFieldsA_goname, genid.ExtensionFieldsB_goname:
Joe Tsai613285c2019-11-13 01:51:27 -0800175 if f.Type == extensionFieldsType {
176 si.extensionOffset = offsetOf(f, mi.Exporter)
Joe Tsai5d634732020-07-26 22:37:47 -0700177 si.extensionType = f.Type
Joe Tsai613285c2019-11-13 01:51:27 -0800178 }
179 default:
180 for _, s := range strings.Split(f.Tag.Get("protobuf"), ",") {
181 if len(s) > 0 && strings.Trim(s, "0123456789") == "" {
182 n, _ := strconv.ParseUint(s, 10, 64)
183 si.fieldsByNumber[pref.FieldNumber(n)] = f
184 continue fieldLoop
185 }
186 }
187 if s := f.Tag.Get("protobuf_oneof"); len(s) > 0 {
188 si.oneofsByName[pref.Name(s)] = f
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700189 continue fieldLoop
190 }
191 }
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700192 }
Joe Tsai93fd9682019-07-06 13:02:14 -0700193
194 // Derive a mapping of oneof wrappers to fields.
Joe Tsai09912272019-07-08 10:38:11 -0700195 oneofWrappers := mi.OneofWrappers
Joe Tsai7dfcffe2020-04-02 10:31:06 -0700196 for _, method := range []string{"XXX_OneofFuncs", "XXX_OneofWrappers"} {
197 if fn, ok := reflect.PtrTo(t).MethodByName(method); ok {
198 for _, v := range fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))}) {
199 if vs, ok := v.Interface().([]interface{}); ok {
200 oneofWrappers = vs
201 }
202 }
203 }
Joe Tsaid7e97bc2018-11-26 12:57:27 -0800204 }
205 for _, v := range oneofWrappers {
206 tf := reflect.TypeOf(v).Elem()
207 f := tf.Field(0)
208 for _, s := range strings.Split(f.Tag.Get("protobuf"), ",") {
209 if len(s) > 0 && strings.Trim(s, "0123456789") == "" {
210 n, _ := strconv.ParseUint(s, 10, 64)
Damien Neil3eaddf02019-05-09 11:33:55 -0700211 si.oneofWrappersByType[tf] = pref.FieldNumber(n)
212 si.oneofWrappersByNumber[pref.FieldNumber(n)] = tf
Joe Tsaid7e97bc2018-11-26 12:57:27 -0800213 break
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700214 }
215 }
216 }
Joe Tsai93fd9682019-07-06 13:02:14 -0700217
Damien Neil3eaddf02019-05-09 11:33:55 -0700218 return si
219}
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700220
Damien Neil16163b42019-08-06 15:43:25 -0700221func (mi *MessageInfo) New() protoreflect.Message {
222 return mi.MessageOf(reflect.New(mi.GoReflectType.Elem()).Interface())
223}
224func (mi *MessageInfo) Zero() protoreflect.Message {
225 return mi.MessageOf(reflect.Zero(mi.GoReflectType).Interface())
226}
Joe Tsai1a290e92020-06-05 15:31:25 -0700227func (mi *MessageInfo) Descriptor() protoreflect.MessageDescriptor {
228 return mi.Desc
229}
230func (mi *MessageInfo) Enum(i int) protoreflect.EnumType {
231 mi.init()
232 fd := mi.Desc.Fields().Get(i)
233 return Export{}.EnumTypeOf(mi.fieldTypes[fd.Number()])
234}
235func (mi *MessageInfo) Message(i int) protoreflect.MessageType {
236 mi.init()
237 fd := mi.Desc.Fields().Get(i)
238 switch {
239 case fd.IsWeak():
240 mt, _ := preg.GlobalTypes.FindMessageByName(fd.Message().FullName())
241 return mt
242 case fd.IsMap():
243 return mapEntryType{fd.Message(), mi.fieldTypes[fd.Number()]}
244 default:
245 return Export{}.MessageTypeOf(mi.fieldTypes[fd.Number()])
246 }
247}
248
249type mapEntryType struct {
250 desc protoreflect.MessageDescriptor
251 valType interface{} // zero value of enum or message type
252}
253
254func (mt mapEntryType) New() protoreflect.Message {
255 return nil
256}
257func (mt mapEntryType) Zero() protoreflect.Message {
258 return nil
259}
260func (mt mapEntryType) Descriptor() protoreflect.MessageDescriptor {
261 return mt.desc
262}
263func (mt mapEntryType) Enum(i int) protoreflect.EnumType {
264 fd := mt.desc.Fields().Get(i)
265 if fd.Enum() == nil {
266 return nil
267 }
268 return Export{}.EnumTypeOf(mt.valType)
269}
270func (mt mapEntryType) Message(i int) protoreflect.MessageType {
271 fd := mt.desc.Fields().Get(i)
272 if fd.Message() == nil {
273 return nil
274 }
275 return Export{}.MessageTypeOf(mt.valType)
276}