blob: fc77cfc11adf8386c2e1e8e76a23205666793efc [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
5// +build !purego
6
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
23// pointer is a pointer to a message struct or field.
24type pointer struct{ p unsafe.Pointer }
25
26// pointerOfValue returns v as a pointer.
27func pointerOfValue(v reflect.Value) pointer {
28 return pointer{p: unsafe.Pointer(v.Pointer())}
29}
30
Joe Tsaic6b75612018-09-13 14:24:37 -070031// pointerOfIface returns the pointer portion of an interface.
Joe Tsai6cf80c42018-12-01 04:57:09 -080032func pointerOfIface(v interface{}) pointer {
Joe Tsaic6b75612018-09-13 14:24:37 -070033 type ifaceHeader struct {
34 Type unsafe.Pointer
35 Data unsafe.Pointer
36 }
Joe Tsai6cf80c42018-12-01 04:57:09 -080037 return pointer{p: (*ifaceHeader)(unsafe.Pointer(&v)).Data}
Joe Tsaic6b75612018-09-13 14:24:37 -070038}
39
Joe Tsai6cf80c42018-12-01 04:57:09 -080040// IsNil reports whether the pointer is nil.
41func (p pointer) IsNil() bool {
42 return p.p == nil
43}
44
45// Apply adds an offset to the pointer to derive a new pointer
46// to a specified field. The pointer must be valid and pointing at a struct.
47func (p pointer) Apply(f offset) pointer {
48 if p.IsNil() {
49 panic("invalid nil pointer")
50 }
Joe Tsaifa02f4e2018-09-12 16:20:37 -070051 return pointer{p: unsafe.Pointer(uintptr(p.p) + uintptr(f))}
52}
53
Joe Tsai6cf80c42018-12-01 04:57:09 -080054// AsValueOf treats p as a pointer to an object of type t and returns the value.
55// It is equivalent to reflect.ValueOf(p.AsIfaceOf(t))
56func (p pointer) AsValueOf(t reflect.Type) reflect.Value {
Joe Tsaifa02f4e2018-09-12 16:20:37 -070057 return reflect.NewAt(t, p.p)
58}
Joe Tsai6cf80c42018-12-01 04:57:09 -080059
60// AsIfaceOf treats p as a pointer to an object of type t and returns the value.
61// It is equivalent to p.AsValueOf(t).Interface()
62func (p pointer) AsIfaceOf(t reflect.Type) interface{} {
63 // TODO: Use tricky unsafe magic to directly create ifaceHeader.
64 return p.AsValueOf(t).Interface()
65}