// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package impl

import (
	
	
	
	
	
	

	
	
	
)

// MessageInfo provides protobuf related functionality for a given Go type
// that represents a message. A given instance of MessageInfo is tied to
// exactly one Go type, which must be a pointer to a struct type.
//
// The exported fields must be populated before any methods are called
// and cannot be mutated after set.
type MessageInfo struct {
	// GoReflectType is the underlying message Go type and must be populated.
	GoReflectType reflect.Type // pointer to struct

	// Desc is the underlying message descriptor type and must be populated.
	Desc protoreflect.MessageDescriptor

	// Exporter must be provided in a purego environment in order to provide
	// access to unexported fields.
	Exporter exporter

	// OneofWrappers is list of pointers to oneof wrapper struct types.
	OneofWrappers []interface{}

	initMu   sync.Mutex // protects all unexported fields
	initDone uint32

	reflectMessageInfo // for reflection implementation
	coderMessageInfo   // for fast-path method implementations
}

// exporter is a function that returns a reference to the ith field of v,
// where v is a pointer to a struct. It returns nil if it does not support
// exporting the requested field (e.g., already exported).
type exporter func(v interface{}, i int) interface{}

// getMessageInfo returns the MessageInfo for any message type that
// is generated by our implementation of protoc-gen-go (for v2 and on).
// If it is unable to obtain a MessageInfo, it returns nil.
func ( reflect.Type) *MessageInfo {
	,  := reflect.Zero().Interface().(protoreflect.ProtoMessage)
	if ! {
		return nil
	}
	,  := .ProtoReflect().(interface{ () *MessageInfo })
	if ! {
		return nil
	}
	return .()
}

func ( *MessageInfo) () {
	// This function is called in the hot path. Inline the sync.Once logic,
	// since allocating a closure for Once.Do is expensive.
	// Keep init small to ensure that it can be inlined.
	if atomic.LoadUint32(&.initDone) == 0 {
		.initOnce()
	}
}

func ( *MessageInfo) () {
	.initMu.Lock()
	defer .initMu.Unlock()
	if .initDone == 1 {
		return
	}

	 := .GoReflectType
	if .Kind() != reflect.Ptr && .Elem().Kind() != reflect.Struct {
		panic(fmt.Sprintf("got %v, want *struct kind", ))
	}
	 = .Elem()

	 := .makeStructInfo()
	.makeReflectFuncs(, )
	.makeCoderMethods(, )

	atomic.StoreUint32(&.initDone, 1)
}

// getPointer returns the pointer for a message, which should be of
// the type of the MessageInfo. If the message is of a different type,
// it returns ok==false.
func ( *MessageInfo) ( protoreflect.Message) ( pointer,  bool) {
	switch m := .(type) {
	case *messageState:
		return .pointer(), .messageInfo() == 
	case *messageReflectWrapper:
		return .pointer(), .messageInfo() == 
	}
	return pointer{}, false
}

type (
	SizeCache       = int32
	WeakFields      = map[int32]protoreflect.ProtoMessage
	UnknownFields   = unknownFieldsA // TODO: switch to unknownFieldsB
	unknownFieldsA  = []byte
	unknownFieldsB  = *[]byte
	ExtensionFields = map[int32]ExtensionField
)

var (
	sizecacheType       = reflect.TypeOf(SizeCache(0))
	weakFieldsType      = reflect.TypeOf(WeakFields(nil))
	unknownFieldsAType  = reflect.TypeOf(unknownFieldsA(nil))
	unknownFieldsBType  = reflect.TypeOf(unknownFieldsB(nil))
	extensionFieldsType = reflect.TypeOf(ExtensionFields(nil))
)

type structInfo struct {
	sizecacheOffset offset
	sizecacheType   reflect.Type
	weakOffset      offset
	weakType        reflect.Type
	unknownOffset   offset
	unknownType     reflect.Type
	extensionOffset offset
	extensionType   reflect.Type

	fieldsByNumber        map[protoreflect.FieldNumber]reflect.StructField
	oneofsByName          map[protoreflect.Name]reflect.StructField
	oneofWrappersByType   map[reflect.Type]protoreflect.FieldNumber
	oneofWrappersByNumber map[protoreflect.FieldNumber]reflect.Type
}

func ( *MessageInfo) ( reflect.Type) structInfo {
	 := structInfo{
		sizecacheOffset: invalidOffset,
		weakOffset:      invalidOffset,
		unknownOffset:   invalidOffset,
		extensionOffset: invalidOffset,

		fieldsByNumber:        map[protoreflect.FieldNumber]reflect.StructField{},
		oneofsByName:          map[protoreflect.Name]reflect.StructField{},
		oneofWrappersByType:   map[reflect.Type]protoreflect.FieldNumber{},
		oneofWrappersByNumber: map[protoreflect.FieldNumber]reflect.Type{},
	}

:
	for  := 0;  < .NumField(); ++ {
		switch  := .Field(); .Name {
		case genid.SizeCache_goname, genid.SizeCacheA_goname:
			if .Type == sizecacheType {
				.sizecacheOffset = offsetOf(, .Exporter)
				.sizecacheType = .Type
			}
		case genid.WeakFields_goname, genid.WeakFieldsA_goname:
			if .Type == weakFieldsType {
				.weakOffset = offsetOf(, .Exporter)
				.weakType = .Type
			}
		case genid.UnknownFields_goname, genid.UnknownFieldsA_goname:
			if .Type == unknownFieldsAType || .Type == unknownFieldsBType {
				.unknownOffset = offsetOf(, .Exporter)
				.unknownType = .Type
			}
		case genid.ExtensionFields_goname, genid.ExtensionFieldsA_goname, genid.ExtensionFieldsB_goname:
			if .Type == extensionFieldsType {
				.extensionOffset = offsetOf(, .Exporter)
				.extensionType = .Type
			}
		default:
			for ,  := range strings.Split(.Tag.Get("protobuf"), ",") {
				if len() > 0 && strings.Trim(, "0123456789") == "" {
					,  := strconv.ParseUint(, 10, 64)
					.fieldsByNumber[protoreflect.FieldNumber()] = 
					continue 
				}
			}
			if  := .Tag.Get("protobuf_oneof"); len() > 0 {
				.oneofsByName[protoreflect.Name()] = 
				continue 
			}
		}
	}

	// Derive a mapping of oneof wrappers to fields.
	 := .OneofWrappers
	for ,  := range []string{"XXX_OneofFuncs", "XXX_OneofWrappers"} {
		if ,  := reflect.PtrTo().MethodByName();  {
			for ,  := range .Func.Call([]reflect.Value{reflect.Zero(.Type.In(0))}) {
				if ,  := .Interface().([]interface{});  {
					 = 
				}
			}
		}
	}
	for ,  := range  {
		 := reflect.TypeOf().Elem()
		 := .Field(0)
		for ,  := range strings.Split(.Tag.Get("protobuf"), ",") {
			if len() > 0 && strings.Trim(, "0123456789") == "" {
				,  := strconv.ParseUint(, 10, 64)
				.oneofWrappersByType[] = protoreflect.FieldNumber()
				.oneofWrappersByNumber[protoreflect.FieldNumber()] = 
				break
			}
		}
	}

	return 
}

func ( *MessageInfo) () protoreflect.Message {
	 := reflect.New(.GoReflectType.Elem()).Interface()
	if ,  := .(protoreflect.ProtoMessage);  {
		return .ProtoReflect()
	}
	return .MessageOf()
}
func ( *MessageInfo) () protoreflect.Message {
	return .MessageOf(reflect.Zero(.GoReflectType).Interface())
}
func ( *MessageInfo) () protoreflect.MessageDescriptor {
	return .Desc
}
func ( *MessageInfo) ( int) protoreflect.EnumType {
	.init()
	 := .Desc.Fields().Get()
	return Export{}.EnumTypeOf(.fieldTypes[.Number()])
}
func ( *MessageInfo) ( int) protoreflect.MessageType {
	.init()
	 := .Desc.Fields().Get()
	switch {
	case .IsWeak():
		,  := protoregistry.GlobalTypes.FindMessageByName(.Message().FullName())
		return 
	case .IsMap():
		return mapEntryType{.Message(), .fieldTypes[.Number()]}
	default:
		return Export{}.MessageTypeOf(.fieldTypes[.Number()])
	}
}

type mapEntryType struct {
	desc    protoreflect.MessageDescriptor
	valType interface{} // zero value of enum or message type
}

func ( mapEntryType) () protoreflect.Message {
	return nil
}
func ( mapEntryType) () protoreflect.Message {
	return nil
}
func ( mapEntryType) () protoreflect.MessageDescriptor {
	return .desc
}
func ( mapEntryType) ( int) protoreflect.EnumType {
	 := .desc.Fields().Get()
	if .Enum() == nil {
		return nil
	}
	return Export{}.EnumTypeOf(.valType)
}
func ( mapEntryType) ( int) protoreflect.MessageType {
	 := .desc.Fields().Get()
	if .Message() == nil {
		return nil
	}
	return Export{}.MessageTypeOf(.valType)
}