blob: cf9fc9af32535009c799f3c7c883ac96cbda2c05 [file] [log] [blame]
Russ Coxd4ce3f12012-09-12 10:36:26 +10001// Go support for Protocol Buffers - Google's data interchange format
2//
David Symondsee6e9c52012-11-29 08:51:07 +11003// Copyright 2012 The Go Authors. All rights reserved.
David Symonds558f13f2014-11-24 10:28:53 +11004// https://github.com/golang/protobuf
Russ Coxd4ce3f12012-09-12 10:36:26 +10005//
6// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are
8// met:
9//
10// * Redistributions of source code must retain the above copyright
11// notice, this list of conditions and the following disclaimer.
12// * Redistributions in binary form must reproduce the above
13// copyright notice, this list of conditions and the following disclaimer
14// in the documentation and/or other materials provided with the
15// distribution.
16// * Neither the name of Google Inc. nor the names of its
17// contributors may be used to endorse or promote products derived from
18// this software without specific prior written permission.
19//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
David Symondsd110e692014-06-16 15:40:44 +100032// +build !appengine appenginevm
Russ Coxd4ce3f12012-09-12 10:36:26 +100033
34// This file contains the implementation of the proto field accesses using package unsafe.
35
36package proto
37
38import (
39 "reflect"
40 "unsafe"
41)
42
43// NOTE: These type_Foo functions would more idiomatically be methods,
44// but Go does not allow methods on pointer types, and we must preserve
45// some pointer type for the garbage collector. We use these
46// funcs with clunky names as our poor approximation to methods.
47//
48// An alternative would be
49// type structPointer struct { p unsafe.Pointer }
50// but that does not registerize as well.
51
52// A structPointer is a pointer to a struct.
53type structPointer unsafe.Pointer
54
55// toStructPointer returns a structPointer equivalent to the given reflect value.
56func toStructPointer(v reflect.Value) structPointer {
57 return structPointer(unsafe.Pointer(v.Pointer()))
58}
59
60// IsNil reports whether p is nil.
61func structPointer_IsNil(p structPointer) bool {
62 return p == nil
63}
64
65// Interface returns the struct pointer, assumed to have element type t,
66// as an interface value.
67func structPointer_Interface(p structPointer, t reflect.Type) interface{} {
68 return reflect.NewAt(t, unsafe.Pointer(p)).Interface()
69}
70
71// A field identifies a field in a struct, accessible from a structPointer.
72// In this implementation, a field is identified by its byte offset from the start of the struct.
73type field uintptr
74
75// toField returns a field equivalent to the given reflect field.
76func toField(f *reflect.StructField) field {
77 return field(f.Offset)
78}
79
80// invalidField is an invalid field identifier.
81const invalidField = ^field(0)
82
83// IsValid reports whether the field identifier is valid.
84func (f field) IsValid() bool {
85 return f != ^field(0)
86}
87
88// Bytes returns the address of a []byte field in the struct.
89func structPointer_Bytes(p structPointer, f field) *[]byte {
90 return (*[]byte)(unsafe.Pointer(uintptr(p) + uintptr(f)))
91}
92
93// BytesSlice returns the address of a [][]byte field in the struct.
94func structPointer_BytesSlice(p structPointer, f field) *[][]byte {
95 return (*[][]byte)(unsafe.Pointer(uintptr(p) + uintptr(f)))
96}
97
98// Bool returns the address of a *bool field in the struct.
99func structPointer_Bool(p structPointer, f field) **bool {
100 return (**bool)(unsafe.Pointer(uintptr(p) + uintptr(f)))
101}
102
David Symondsabd3b412014-11-28 11:43:44 +1100103// BoolVal returns the address of a bool field in the struct.
104func structPointer_BoolVal(p structPointer, f field) *bool {
105 return (*bool)(unsafe.Pointer(uintptr(p) + uintptr(f)))
106}
107
Russ Coxd4ce3f12012-09-12 10:36:26 +1000108// BoolSlice returns the address of a []bool field in the struct.
109func structPointer_BoolSlice(p structPointer, f field) *[]bool {
110 return (*[]bool)(unsafe.Pointer(uintptr(p) + uintptr(f)))
111}
112
113// String returns the address of a *string field in the struct.
114func structPointer_String(p structPointer, f field) **string {
115 return (**string)(unsafe.Pointer(uintptr(p) + uintptr(f)))
116}
117
David Symondsabd3b412014-11-28 11:43:44 +1100118// StringVal returns the address of a string field in the struct.
119func structPointer_StringVal(p structPointer, f field) *string {
120 return (*string)(unsafe.Pointer(uintptr(p) + uintptr(f)))
121}
122
Russ Coxd4ce3f12012-09-12 10:36:26 +1000123// StringSlice returns the address of a []string field in the struct.
124func structPointer_StringSlice(p structPointer, f field) *[]string {
125 return (*[]string)(unsafe.Pointer(uintptr(p) + uintptr(f)))
126}
127
128// ExtMap returns the address of an extension map field in the struct.
129func structPointer_ExtMap(p structPointer, f field) *map[int32]Extension {
130 return (*map[int32]Extension)(unsafe.Pointer(uintptr(p) + uintptr(f)))
131}
132
133// SetStructPointer writes a *struct field in the struct.
134func structPointer_SetStructPointer(p structPointer, f field, q structPointer) {
135 *(*structPointer)(unsafe.Pointer(uintptr(p) + uintptr(f))) = q
136}
137
138// GetStructPointer reads a *struct field in the struct.
139func structPointer_GetStructPointer(p structPointer, f field) structPointer {
140 return *(*structPointer)(unsafe.Pointer(uintptr(p) + uintptr(f)))
141}
142
143// StructPointerSlice the address of a []*struct field in the struct.
144func structPointer_StructPointerSlice(p structPointer, f field) *structPointerSlice {
145 return (*structPointerSlice)(unsafe.Pointer(uintptr(p) + uintptr(f)))
146}
147
148// A structPointerSlice represents a slice of pointers to structs (themselves submessages or groups).
149type structPointerSlice []structPointer
150
151func (v *structPointerSlice) Len() int { return len(*v) }
152func (v *structPointerSlice) Index(i int) structPointer { return (*v)[i] }
153func (v *structPointerSlice) Append(p structPointer) { *v = append(*v, p) }
154
155// A word32 is the address of a "pointer to 32-bit value" field.
156type word32 **uint32
157
158// IsNil reports whether *v is nil.
159func word32_IsNil(p word32) bool {
160 return *p == nil
161}
162
163// Set sets *v to point at a newly allocated word set to x.
164func word32_Set(p word32, o *Buffer, x uint32) {
165 if len(o.uint32s) == 0 {
166 o.uint32s = make([]uint32, uint32PoolSize)
167 }
168 o.uint32s[0] = x
169 *p = &o.uint32s[0]
170 o.uint32s = o.uint32s[1:]
171}
172
173// Get gets the value pointed at by *v.
174func word32_Get(p word32) uint32 {
175 return **p
176}
177
178// Word32 returns the address of a *int32, *uint32, *float32, or *enum field in the struct.
179func structPointer_Word32(p structPointer, f field) word32 {
180 return word32((**uint32)(unsafe.Pointer(uintptr(p) + uintptr(f))))
181}
182
David Symondsabd3b412014-11-28 11:43:44 +1100183// A word32Val is the address of a 32-bit value field.
184type word32Val *uint32
185
186// Set sets *p to x.
187func word32Val_Set(p word32Val, x uint32) {
188 *p = x
189}
190
191// Get gets the value pointed at by p.
192func word32Val_Get(p word32Val) uint32 {
193 return *p
194}
195
196// Word32Val returns the address of a *int32, *uint32, *float32, or *enum field in the struct.
197func structPointer_Word32Val(p structPointer, f field) word32Val {
198 return word32Val((*uint32)(unsafe.Pointer(uintptr(p) + uintptr(f))))
199}
200
Russ Coxd4ce3f12012-09-12 10:36:26 +1000201// A word32Slice is a slice of 32-bit values.
202type word32Slice []uint32
203
204func (v *word32Slice) Append(x uint32) { *v = append(*v, x) }
205func (v *word32Slice) Len() int { return len(*v) }
206func (v *word32Slice) Index(i int) uint32 { return (*v)[i] }
207
208// Word32Slice returns the address of a []int32, []uint32, []float32, or []enum field in the struct.
209func structPointer_Word32Slice(p structPointer, f field) *word32Slice {
210 return (*word32Slice)(unsafe.Pointer(uintptr(p) + uintptr(f)))
211}
212
213// word64 is like word32 but for 64-bit values.
214type word64 **uint64
215
216func word64_Set(p word64, o *Buffer, x uint64) {
217 if len(o.uint64s) == 0 {
218 o.uint64s = make([]uint64, uint64PoolSize)
219 }
220 o.uint64s[0] = x
221 *p = &o.uint64s[0]
222 o.uint64s = o.uint64s[1:]
223}
224
225func word64_IsNil(p word64) bool {
226 return *p == nil
227}
228
229func word64_Get(p word64) uint64 {
230 return **p
231}
232
233func structPointer_Word64(p structPointer, f field) word64 {
234 return word64((**uint64)(unsafe.Pointer(uintptr(p) + uintptr(f))))
235}
236
David Symondsabd3b412014-11-28 11:43:44 +1100237// word64Val is like word32Val but for 64-bit values.
238type word64Val *uint64
239
240func word64Val_Set(p word64Val, o *Buffer, x uint64) {
241 *p = x
242}
243
244func word64Val_Get(p word64Val) uint64 {
245 return *p
246}
247
248func structPointer_Word64Val(p structPointer, f field) word64Val {
249 return word64Val((*uint64)(unsafe.Pointer(uintptr(p) + uintptr(f))))
250}
251
Russ Coxd4ce3f12012-09-12 10:36:26 +1000252// word64Slice is like word32Slice but for 64-bit values.
253type word64Slice []uint64
254
255func (v *word64Slice) Append(x uint64) { *v = append(*v, x) }
256func (v *word64Slice) Len() int { return len(*v) }
257func (v *word64Slice) Index(i int) uint64 { return (*v)[i] }
258
259func structPointer_Word64Slice(p structPointer, f field) *word64Slice {
260 return (*word64Slice)(unsafe.Pointer(uintptr(p) + uintptr(f)))
261}