...

Source file src/google.golang.org/protobuf/internal/impl/pointer_reflect.go

Documentation: google.golang.org/protobuf/internal/impl

     1  // 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  //go:build purego || appengine
     6  // +build purego appengine
     7  
     8  package impl
     9  
    10  import (
    11  	"fmt"
    12  	"reflect"
    13  	"sync"
    14  )
    15  
    16  const UnsafeEnabled = false
    17  
    18  // Pointer is an opaque pointer type.
    19  type Pointer interface{}
    20  
    21  // offset represents the offset to a struct field, accessible from a pointer.
    22  // The offset is the field index into a struct.
    23  type offset struct {
    24  	index  int
    25  	export exporter
    26  }
    27  
    28  // offsetOf returns a field offset for the struct field.
    29  func offsetOf(f reflect.StructField, x exporter) offset {
    30  	if len(f.Index) != 1 {
    31  		panic("embedded structs are not supported")
    32  	}
    33  	if f.PkgPath == "" {
    34  		return offset{index: f.Index[0]} // field is already exported
    35  	}
    36  	if x == nil {
    37  		panic("exporter must be provided for unexported field")
    38  	}
    39  	return offset{index: f.Index[0], export: x}
    40  }
    41  
    42  // IsValid reports whether the offset is valid.
    43  func (f offset) IsValid() bool { return f.index >= 0 }
    44  
    45  // invalidOffset is an invalid field offset.
    46  var invalidOffset = offset{index: -1}
    47  
    48  // zeroOffset is a noop when calling pointer.Apply.
    49  var zeroOffset = offset{index: 0}
    50  
    51  // pointer is an abstract representation of a pointer to a struct or field.
    52  type pointer struct{ v reflect.Value }
    53  
    54  // pointerOf returns p as a pointer.
    55  func pointerOf(p Pointer) pointer {
    56  	return pointerOfIface(p)
    57  }
    58  
    59  // pointerOfValue returns v as a pointer.
    60  func pointerOfValue(v reflect.Value) pointer {
    61  	return pointer{v: v}
    62  }
    63  
    64  // pointerOfIface returns the pointer portion of an interface.
    65  func pointerOfIface(v interface{}) pointer {
    66  	return pointer{v: reflect.ValueOf(v)}
    67  }
    68  
    69  // IsNil reports whether the pointer is nil.
    70  func (p pointer) IsNil() bool {
    71  	return p.v.IsNil()
    72  }
    73  
    74  // Apply adds an offset to the pointer to derive a new pointer
    75  // to a specified field. The current pointer must be pointing at a struct.
    76  func (p pointer) Apply(f offset) pointer {
    77  	if f.export != nil {
    78  		if v := reflect.ValueOf(f.export(p.v.Interface(), f.index)); v.IsValid() {
    79  			return pointer{v: v}
    80  		}
    81  	}
    82  	return pointer{v: p.v.Elem().Field(f.index).Addr()}
    83  }
    84  
    85  // AsValueOf treats p as a pointer to an object of type t and returns the value.
    86  // It is equivalent to reflect.ValueOf(p.AsIfaceOf(t))
    87  func (p pointer) AsValueOf(t reflect.Type) reflect.Value {
    88  	if got := p.v.Type().Elem(); got != t {
    89  		panic(fmt.Sprintf("invalid type: got %v, want %v", got, t))
    90  	}
    91  	return p.v
    92  }
    93  
    94  // AsIfaceOf treats p as a pointer to an object of type t and returns the value.
    95  // It is equivalent to p.AsValueOf(t).Interface()
    96  func (p pointer) AsIfaceOf(t reflect.Type) interface{} {
    97  	return p.AsValueOf(t).Interface()
    98  }
    99  
   100  func (p pointer) Bool() *bool              { return p.v.Interface().(*bool) }
   101  func (p pointer) BoolPtr() **bool          { return p.v.Interface().(**bool) }
   102  func (p pointer) BoolSlice() *[]bool       { return p.v.Interface().(*[]bool) }
   103  func (p pointer) Int32() *int32            { return p.v.Interface().(*int32) }
   104  func (p pointer) Int32Ptr() **int32        { return p.v.Interface().(**int32) }
   105  func (p pointer) Int32Slice() *[]int32     { return p.v.Interface().(*[]int32) }
   106  func (p pointer) Int64() *int64            { return p.v.Interface().(*int64) }
   107  func (p pointer) Int64Ptr() **int64        { return p.v.Interface().(**int64) }
   108  func (p pointer) Int64Slice() *[]int64     { return p.v.Interface().(*[]int64) }
   109  func (p pointer) Uint32() *uint32          { return p.v.Interface().(*uint32) }
   110  func (p pointer) Uint32Ptr() **uint32      { return p.v.Interface().(**uint32) }
   111  func (p pointer) Uint32Slice() *[]uint32   { return p.v.Interface().(*[]uint32) }
   112  func (p pointer) Uint64() *uint64          { return p.v.Interface().(*uint64) }
   113  func (p pointer) Uint64Ptr() **uint64      { return p.v.Interface().(**uint64) }
   114  func (p pointer) Uint64Slice() *[]uint64   { return p.v.Interface().(*[]uint64) }
   115  func (p pointer) Float32() *float32        { return p.v.Interface().(*float32) }
   116  func (p pointer) Float32Ptr() **float32    { return p.v.Interface().(**float32) }
   117  func (p pointer) Float32Slice() *[]float32 { return p.v.Interface().(*[]float32) }
   118  func (p pointer) Float64() *float64        { return p.v.Interface().(*float64) }
   119  func (p pointer) Float64Ptr() **float64    { return p.v.Interface().(**float64) }
   120  func (p pointer) Float64Slice() *[]float64 { return p.v.Interface().(*[]float64) }
   121  func (p pointer) String() *string          { return p.v.Interface().(*string) }
   122  func (p pointer) StringPtr() **string      { return p.v.Interface().(**string) }
   123  func (p pointer) StringSlice() *[]string   { return p.v.Interface().(*[]string) }
   124  func (p pointer) Bytes() *[]byte           { return p.v.Interface().(*[]byte) }
   125  func (p pointer) BytesPtr() **[]byte       { return p.v.Interface().(**[]byte) }
   126  func (p pointer) BytesSlice() *[][]byte    { return p.v.Interface().(*[][]byte) }
   127  func (p pointer) WeakFields() *weakFields  { return (*weakFields)(p.v.Interface().(*WeakFields)) }
   128  func (p pointer) Extensions() *map[int32]ExtensionField {
   129  	return p.v.Interface().(*map[int32]ExtensionField)
   130  }
   131  
   132  func (p pointer) Elem() pointer {
   133  	return pointer{v: p.v.Elem()}
   134  }
   135  
   136  // PointerSlice copies []*T from p as a new []pointer.
   137  // This behavior differs from the implementation in pointer_unsafe.go.
   138  func (p pointer) PointerSlice() []pointer {
   139  	// TODO: reconsider this
   140  	if p.v.IsNil() {
   141  		return nil
   142  	}
   143  	n := p.v.Elem().Len()
   144  	s := make([]pointer, n)
   145  	for i := 0; i < n; i++ {
   146  		s[i] = pointer{v: p.v.Elem().Index(i)}
   147  	}
   148  	return s
   149  }
   150  
   151  // AppendPointerSlice appends v to p, which must be a []*T.
   152  func (p pointer) AppendPointerSlice(v pointer) {
   153  	sp := p.v.Elem()
   154  	sp.Set(reflect.Append(sp, v.v))
   155  }
   156  
   157  // SetPointer sets *p to v.
   158  func (p pointer) SetPointer(v pointer) {
   159  	p.v.Elem().Set(v.v)
   160  }
   161  
   162  func growSlice(p pointer, addCap int) {
   163  	// TODO: Once we only support Go 1.20 and newer, use reflect.Grow.
   164  	in := p.v.Elem()
   165  	out := reflect.MakeSlice(in.Type(), in.Len(), in.Len()+addCap)
   166  	reflect.Copy(out, in)
   167  	p.v.Elem().Set(out)
   168  }
   169  
   170  func (p pointer) growBoolSlice(addCap int) {
   171  	growSlice(p, addCap)
   172  }
   173  
   174  func (p pointer) growInt32Slice(addCap int) {
   175  	growSlice(p, addCap)
   176  }
   177  
   178  func (p pointer) growUint32Slice(addCap int) {
   179  	growSlice(p, addCap)
   180  }
   181  
   182  func (p pointer) growInt64Slice(addCap int) {
   183  	growSlice(p, addCap)
   184  }
   185  
   186  func (p pointer) growUint64Slice(addCap int) {
   187  	growSlice(p, addCap)
   188  }
   189  
   190  func (p pointer) growFloat64Slice(addCap int) {
   191  	growSlice(p, addCap)
   192  }
   193  
   194  func (p pointer) growFloat32Slice(addCap int) {
   195  	growSlice(p, addCap)
   196  }
   197  
   198  func (Export) MessageStateOf(p Pointer) *messageState     { panic("not supported") }
   199  func (ms *messageState) pointer() pointer                 { panic("not supported") }
   200  func (ms *messageState) messageInfo() *MessageInfo        { panic("not supported") }
   201  func (ms *messageState) LoadMessageInfo() *MessageInfo    { panic("not supported") }
   202  func (ms *messageState) StoreMessageInfo(mi *MessageInfo) { panic("not supported") }
   203  
   204  type atomicNilMessage struct {
   205  	once sync.Once
   206  	m    messageReflectWrapper
   207  }
   208  
   209  func (m *atomicNilMessage) Init(mi *MessageInfo) *messageReflectWrapper {
   210  	m.once.Do(func() {
   211  		m.m.p = pointerOfIface(reflect.Zero(mi.GoReflectType).Interface())
   212  		m.m.mi = mi
   213  	})
   214  	return &m.m
   215  }
   216  

View as plain text