blob: bd5ab4bdaa2342213974b801777b37c892ce7f1d [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
Joe Tsaic0e4bb22019-07-06 13:05:11 -070014const UnsafeEnabled = true
15
Joe Tsaifa02f4e2018-09-12 16:20:37 -070016// offset represents the offset to a struct field, accessible from a pointer.
17// The offset is the byte offset to the field from the start of the struct.
18type offset uintptr
19
20// offsetOf returns a field offset for the struct field.
Joe Tsaic0e4bb22019-07-06 13:05:11 -070021func offsetOf(f reflect.StructField, x exporter) offset {
Joe Tsaifa02f4e2018-09-12 16:20:37 -070022 return offset(f.Offset)
23}
24
Damien Neilc37adef2019-04-01 13:49:56 -070025// IsValid reports whether the offset is valid.
26func (f offset) IsValid() bool { return f != invalidOffset }
27
28// invalidOffset is an invalid field offset.
29var invalidOffset = ^offset(0)
30
31// zeroOffset is a noop when calling pointer.Apply.
32var zeroOffset = offset(0)
33
Joe Tsaifa02f4e2018-09-12 16:20:37 -070034// pointer is a pointer to a message struct or field.
35type pointer struct{ p unsafe.Pointer }
36
37// pointerOfValue returns v as a pointer.
38func pointerOfValue(v reflect.Value) pointer {
39 return pointer{p: unsafe.Pointer(v.Pointer())}
40}
41
Joe Tsaic6b75612018-09-13 14:24:37 -070042// pointerOfIface returns the pointer portion of an interface.
Joe Tsai6cf80c42018-12-01 04:57:09 -080043func pointerOfIface(v interface{}) pointer {
Joe Tsaic6b75612018-09-13 14:24:37 -070044 type ifaceHeader struct {
45 Type unsafe.Pointer
46 Data unsafe.Pointer
47 }
Joe Tsai6cf80c42018-12-01 04:57:09 -080048 return pointer{p: (*ifaceHeader)(unsafe.Pointer(&v)).Data}
Joe Tsaic6b75612018-09-13 14:24:37 -070049}
50
Joe Tsai6cf80c42018-12-01 04:57:09 -080051// IsNil reports whether the pointer is nil.
52func (p pointer) IsNil() bool {
53 return p.p == nil
54}
55
56// Apply adds an offset to the pointer to derive a new pointer
57// to a specified field. The pointer must be valid and pointing at a struct.
58func (p pointer) Apply(f offset) pointer {
59 if p.IsNil() {
60 panic("invalid nil pointer")
61 }
Joe Tsaifa02f4e2018-09-12 16:20:37 -070062 return pointer{p: unsafe.Pointer(uintptr(p.p) + uintptr(f))}
63}
64
Joe Tsai6cf80c42018-12-01 04:57:09 -080065// AsValueOf treats p as a pointer to an object of type t and returns the value.
66// It is equivalent to reflect.ValueOf(p.AsIfaceOf(t))
67func (p pointer) AsValueOf(t reflect.Type) reflect.Value {
Joe Tsaifa02f4e2018-09-12 16:20:37 -070068 return reflect.NewAt(t, p.p)
69}
Joe Tsai6cf80c42018-12-01 04:57:09 -080070
71// AsIfaceOf treats p as a pointer to an object of type t and returns the value.
72// It is equivalent to p.AsValueOf(t).Interface()
73func (p pointer) AsIfaceOf(t reflect.Type) interface{} {
74 // TODO: Use tricky unsafe magic to directly create ifaceHeader.
75 return p.AsValueOf(t).Interface()
76}
Damien Neilc37adef2019-04-01 13:49:56 -070077
Joe Tsai89d49632019-06-04 16:20:00 -070078func (p pointer) Bool() *bool { return (*bool)(p.p) }
79func (p pointer) BoolPtr() **bool { return (**bool)(p.p) }
80func (p pointer) BoolSlice() *[]bool { return (*[]bool)(p.p) }
81func (p pointer) Int32() *int32 { return (*int32)(p.p) }
82func (p pointer) Int32Ptr() **int32 { return (**int32)(p.p) }
83func (p pointer) Int32Slice() *[]int32 { return (*[]int32)(p.p) }
84func (p pointer) Int64() *int64 { return (*int64)(p.p) }
85func (p pointer) Int64Ptr() **int64 { return (**int64)(p.p) }
86func (p pointer) Int64Slice() *[]int64 { return (*[]int64)(p.p) }
87func (p pointer) Uint32() *uint32 { return (*uint32)(p.p) }
88func (p pointer) Uint32Ptr() **uint32 { return (**uint32)(p.p) }
89func (p pointer) Uint32Slice() *[]uint32 { return (*[]uint32)(p.p) }
90func (p pointer) Uint64() *uint64 { return (*uint64)(p.p) }
91func (p pointer) Uint64Ptr() **uint64 { return (**uint64)(p.p) }
92func (p pointer) Uint64Slice() *[]uint64 { return (*[]uint64)(p.p) }
93func (p pointer) Float32() *float32 { return (*float32)(p.p) }
94func (p pointer) Float32Ptr() **float32 { return (**float32)(p.p) }
95func (p pointer) Float32Slice() *[]float32 { return (*[]float32)(p.p) }
96func (p pointer) Float64() *float64 { return (*float64)(p.p) }
97func (p pointer) Float64Ptr() **float64 { return (**float64)(p.p) }
98func (p pointer) Float64Slice() *[]float64 { return (*[]float64)(p.p) }
99func (p pointer) String() *string { return (*string)(p.p) }
100func (p pointer) StringPtr() **string { return (**string)(p.p) }
101func (p pointer) StringSlice() *[]string { return (*[]string)(p.p) }
102func (p pointer) Bytes() *[]byte { return (*[]byte)(p.p) }
103func (p pointer) BytesSlice() *[][]byte { return (*[][]byte)(p.p) }
104func (p pointer) Extensions() *map[int32]ExtensionField { return (*map[int32]ExtensionField)(p.p) }
Damien Neilc37adef2019-04-01 13:49:56 -0700105
106func (p pointer) Elem() pointer {
107 return pointer{p: *(*unsafe.Pointer)(p.p)}
108}
109
110// PointerSlice loads []*T from p as a []pointer.
111// The value returned is aliased with the original slice.
112// This behavior differs from the implementation in pointer_reflect.go.
113func (p pointer) PointerSlice() []pointer {
114 // Super-tricky - p should point to a []*T where T is a
115 // message type. We load it as []pointer.
116 return *(*[]pointer)(p.p)
117}