blob: f04a5615c3cfd88fa4f26cbd0677308e37fe1940 [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
Joe Tsaibc534a92018-12-18 16:07:48 -08005// +build !purego,!appengine
Joe Tsaifa02f4e2018-09-12 16:20:37 -07006
7package impl
8
9import (
10 "reflect"
11 "unsafe"
12)
13
14// offset represents the offset to a struct field, accessible from a pointer.
15// The offset is the byte offset to the field from the start of the struct.
16type offset uintptr
17
18// offsetOf returns a field offset for the struct field.
19func offsetOf(f reflect.StructField) offset {
20 return offset(f.Offset)
21}
22
Damien Neilc37adef2019-04-01 13:49:56 -070023// IsValid reports whether the offset is valid.
24func (f offset) IsValid() bool { return f != invalidOffset }
25
26// invalidOffset is an invalid field offset.
27var invalidOffset = ^offset(0)
28
29// zeroOffset is a noop when calling pointer.Apply.
30var zeroOffset = offset(0)
31
Joe Tsaifa02f4e2018-09-12 16:20:37 -070032// pointer is a pointer to a message struct or field.
33type pointer struct{ p unsafe.Pointer }
34
35// pointerOfValue returns v as a pointer.
36func pointerOfValue(v reflect.Value) pointer {
37 return pointer{p: unsafe.Pointer(v.Pointer())}
38}
39
Joe Tsaic6b75612018-09-13 14:24:37 -070040// pointerOfIface returns the pointer portion of an interface.
Joe Tsai6cf80c42018-12-01 04:57:09 -080041func pointerOfIface(v interface{}) pointer {
Joe Tsaic6b75612018-09-13 14:24:37 -070042 type ifaceHeader struct {
43 Type unsafe.Pointer
44 Data unsafe.Pointer
45 }
Joe Tsai6cf80c42018-12-01 04:57:09 -080046 return pointer{p: (*ifaceHeader)(unsafe.Pointer(&v)).Data}
Joe Tsaic6b75612018-09-13 14:24:37 -070047}
48
Joe Tsai6cf80c42018-12-01 04:57:09 -080049// IsNil reports whether the pointer is nil.
50func (p pointer) IsNil() bool {
51 return p.p == nil
52}
53
54// Apply adds an offset to the pointer to derive a new pointer
55// to a specified field. The pointer must be valid and pointing at a struct.
56func (p pointer) Apply(f offset) pointer {
57 if p.IsNil() {
58 panic("invalid nil pointer")
59 }
Joe Tsaifa02f4e2018-09-12 16:20:37 -070060 return pointer{p: unsafe.Pointer(uintptr(p.p) + uintptr(f))}
61}
62
Joe Tsai6cf80c42018-12-01 04:57:09 -080063// AsValueOf treats p as a pointer to an object of type t and returns the value.
64// It is equivalent to reflect.ValueOf(p.AsIfaceOf(t))
65func (p pointer) AsValueOf(t reflect.Type) reflect.Value {
Joe Tsaifa02f4e2018-09-12 16:20:37 -070066 return reflect.NewAt(t, p.p)
67}
Joe Tsai6cf80c42018-12-01 04:57:09 -080068
69// AsIfaceOf treats p as a pointer to an object of type t and returns the value.
70// It is equivalent to p.AsValueOf(t).Interface()
71func (p pointer) AsIfaceOf(t reflect.Type) interface{} {
72 // TODO: Use tricky unsafe magic to directly create ifaceHeader.
73 return p.AsValueOf(t).Interface()
74}
Damien Neilc37adef2019-04-01 13:49:56 -070075
76func (p pointer) Bool() *bool { return (*bool)(p.p) }
77func (p pointer) BoolPtr() **bool { return (**bool)(p.p) }
78func (p pointer) BoolSlice() *[]bool { return (*[]bool)(p.p) }
79func (p pointer) Int32() *int32 { return (*int32)(p.p) }
80func (p pointer) Int32Ptr() **int32 { return (**int32)(p.p) }
81func (p pointer) Int32Slice() *[]int32 { return (*[]int32)(p.p) }
82func (p pointer) Int64() *int64 { return (*int64)(p.p) }
83func (p pointer) Int64Ptr() **int64 { return (**int64)(p.p) }
84func (p pointer) Int64Slice() *[]int64 { return (*[]int64)(p.p) }
85func (p pointer) Uint32() *uint32 { return (*uint32)(p.p) }
86func (p pointer) Uint32Ptr() **uint32 { return (**uint32)(p.p) }
87func (p pointer) Uint32Slice() *[]uint32 { return (*[]uint32)(p.p) }
88func (p pointer) Uint64() *uint64 { return (*uint64)(p.p) }
89func (p pointer) Uint64Ptr() **uint64 { return (**uint64)(p.p) }
90func (p pointer) Uint64Slice() *[]uint64 { return (*[]uint64)(p.p) }
91func (p pointer) Float32() *float32 { return (*float32)(p.p) }
92func (p pointer) Float32Ptr() **float32 { return (**float32)(p.p) }
93func (p pointer) Float32Slice() *[]float32 { return (*[]float32)(p.p) }
94func (p pointer) Float64() *float64 { return (*float64)(p.p) }
95func (p pointer) Float64Ptr() **float64 { return (**float64)(p.p) }
96func (p pointer) Float64Slice() *[]float64 { return (*[]float64)(p.p) }
97func (p pointer) String() *string { return (*string)(p.p) }
98func (p pointer) StringPtr() **string { return (**string)(p.p) }
99func (p pointer) StringSlice() *[]string { return (*[]string)(p.p) }
100func (p pointer) Bytes() *[]byte { return (*[]byte)(p.p) }
101func (p pointer) BytesSlice() *[][]byte { return (*[][]byte)(p.p) }
102func (p pointer) Extensions() *legacyExtensionMap { return (*legacyExtensionMap)(p.p) }
103
104func (p pointer) Elem() pointer {
105 return pointer{p: *(*unsafe.Pointer)(p.p)}
106}
107
108// PointerSlice loads []*T from p as a []pointer.
109// The value returned is aliased with the original slice.
110// This behavior differs from the implementation in pointer_reflect.go.
111func (p pointer) PointerSlice() []pointer {
112 // Super-tricky - p should point to a []*T where T is a
113 // message type. We load it as []pointer.
114 return *(*[]pointer)(p.p)
115}