blob: c1d890233c986ded88dda10de2cab43ee8f74e01 [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
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 Tsai3d8e3692019-04-08 13:52:14 -0700112 WeakFields = map[int32]piface.MessageV1
Joe Tsai378c1322019-04-25 23:48:08 -0700113 UnknownFields = []byte
114 ExtensionFields = map[int32]ExtensionField
115)
116
117var (
118 sizecacheType = reflect.TypeOf(SizeCache(0))
Joe Tsai3d8e3692019-04-08 13:52:14 -0700119 weakFieldsType = reflect.TypeOf(WeakFields(nil))
Joe Tsai378c1322019-04-25 23:48:08 -0700120 unknownFieldsType = reflect.TypeOf(UnknownFields(nil))
121 extensionFieldsType = reflect.TypeOf(ExtensionFields(nil))
122)
Damien Neilc37adef2019-04-01 13:49:56 -0700123
Damien Neil3eaddf02019-05-09 11:33:55 -0700124type structInfo struct {
Joe Tsai93fd9682019-07-06 13:02:14 -0700125 sizecacheOffset offset
Joe Tsai3d8e3692019-04-08 13:52:14 -0700126 weakOffset offset
Joe Tsai93fd9682019-07-06 13:02:14 -0700127 unknownOffset offset
Joe Tsaic0e4bb22019-07-06 13:05:11 -0700128 extensionOffset offset
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}
135
Joe Tsai4fe96632019-05-22 05:12:36 -0400136func (mi *MessageInfo) makeStructInfo(t reflect.Type) structInfo {
Damien Neil3eaddf02019-05-09 11:33:55 -0700137 si := structInfo{
Joe Tsai93fd9682019-07-06 13:02:14 -0700138 sizecacheOffset: invalidOffset,
Joe Tsai3d8e3692019-04-08 13:52:14 -0700139 weakOffset: invalidOffset,
Joe Tsai93fd9682019-07-06 13:02:14 -0700140 unknownOffset: invalidOffset,
Joe Tsaic0e4bb22019-07-06 13:05:11 -0700141 extensionOffset: invalidOffset,
Joe Tsai93fd9682019-07-06 13:02:14 -0700142
Damien Neil3eaddf02019-05-09 11:33:55 -0700143 fieldsByNumber: map[pref.FieldNumber]reflect.StructField{},
144 oneofsByName: map[pref.Name]reflect.StructField{},
145 oneofWrappersByType: map[reflect.Type]pref.FieldNumber{},
146 oneofWrappersByNumber: map[pref.FieldNumber]reflect.Type{},
147 }
Joe Tsai93fd9682019-07-06 13:02:14 -0700148
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700149fieldLoop:
150 for i := 0; i < t.NumField(); i++ {
Joe Tsai613285c2019-11-13 01:51:27 -0800151 switch f := t.Field(i); f.Name {
Joe Tsai4663ebc2019-11-13 17:28:51 -0800152 case genname.SizeCache, genname.SizeCacheA:
Joe Tsai613285c2019-11-13 01:51:27 -0800153 if f.Type == sizecacheType {
154 si.sizecacheOffset = offsetOf(f, mi.Exporter)
155 }
Joe Tsai4663ebc2019-11-13 17:28:51 -0800156 case genname.WeakFields, genname.WeakFieldsA:
Joe Tsai613285c2019-11-13 01:51:27 -0800157 if f.Type == weakFieldsType {
158 si.weakOffset = offsetOf(f, mi.Exporter)
159 }
Joe Tsai4663ebc2019-11-13 17:28:51 -0800160 case genname.UnknownFields, genname.UnknownFieldsA:
Joe Tsai613285c2019-11-13 01:51:27 -0800161 if f.Type == unknownFieldsType {
162 si.unknownOffset = offsetOf(f, mi.Exporter)
163 }
Joe Tsai4663ebc2019-11-13 17:28:51 -0800164 case genname.ExtensionFields, genname.ExtensionFieldsA, genname.ExtensionFieldsB:
Joe Tsai613285c2019-11-13 01:51:27 -0800165 if f.Type == extensionFieldsType {
166 si.extensionOffset = offsetOf(f, mi.Exporter)
167 }
168 default:
169 for _, s := range strings.Split(f.Tag.Get("protobuf"), ",") {
170 if len(s) > 0 && strings.Trim(s, "0123456789") == "" {
171 n, _ := strconv.ParseUint(s, 10, 64)
172 si.fieldsByNumber[pref.FieldNumber(n)] = f
173 continue fieldLoop
174 }
175 }
176 if s := f.Tag.Get("protobuf_oneof"); len(s) > 0 {
177 si.oneofsByName[pref.Name(s)] = f
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700178 continue fieldLoop
179 }
180 }
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700181 }
Joe Tsai93fd9682019-07-06 13:02:14 -0700182
183 // Derive a mapping of oneof wrappers to fields.
Joe Tsai09912272019-07-08 10:38:11 -0700184 oneofWrappers := mi.OneofWrappers
Joe Tsai7dfcffe2020-04-02 10:31:06 -0700185 for _, method := range []string{"XXX_OneofFuncs", "XXX_OneofWrappers"} {
186 if fn, ok := reflect.PtrTo(t).MethodByName(method); ok {
187 for _, v := range fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))}) {
188 if vs, ok := v.Interface().([]interface{}); ok {
189 oneofWrappers = vs
190 }
191 }
192 }
Joe Tsaid7e97bc2018-11-26 12:57:27 -0800193 }
194 for _, v := range oneofWrappers {
195 tf := reflect.TypeOf(v).Elem()
196 f := tf.Field(0)
197 for _, s := range strings.Split(f.Tag.Get("protobuf"), ",") {
198 if len(s) > 0 && strings.Trim(s, "0123456789") == "" {
199 n, _ := strconv.ParseUint(s, 10, 64)
Damien Neil3eaddf02019-05-09 11:33:55 -0700200 si.oneofWrappersByType[tf] = pref.FieldNumber(n)
201 si.oneofWrappersByNumber[pref.FieldNumber(n)] = tf
Joe Tsaid7e97bc2018-11-26 12:57:27 -0800202 break
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700203 }
204 }
205 }
Joe Tsai93fd9682019-07-06 13:02:14 -0700206
Damien Neil3eaddf02019-05-09 11:33:55 -0700207 return si
208}
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700209
Damien Neil16163b42019-08-06 15:43:25 -0700210func (mi *MessageInfo) New() protoreflect.Message {
211 return mi.MessageOf(reflect.New(mi.GoReflectType.Elem()).Interface())
212}
213func (mi *MessageInfo) Zero() protoreflect.Message {
214 return mi.MessageOf(reflect.Zero(mi.GoReflectType).Interface())
215}
216func (mi *MessageInfo) Descriptor() protoreflect.MessageDescriptor { return mi.Desc }