// Copyright 2019 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 filedesc

import (
	
	
	
	

	

	
	
	
	
	
)

type FileImports []protoreflect.FileImport

func ( *FileImports) () int                            { return len(*) }
func ( *FileImports) ( int) protoreflect.FileImport   { return (*)[] }
func ( *FileImports) ( fmt.State,  rune)          { descfmt.FormatList(, , ) }
func ( *FileImports) (pragma.DoNotImplement) {}

type Names struct {
	List []protoreflect.Name
	once sync.Once
	has  map[protoreflect.Name]int // protected by once
}

func ( *Names) () int                            { return len(.List) }
func ( *Names) ( int) protoreflect.Name         { return .List[] }
func ( *Names) ( protoreflect.Name) bool        { return .lazyInit().has[] > 0 }
func ( *Names) ( fmt.State,  rune)          { descfmt.FormatList(, , ) }
func ( *Names) (pragma.DoNotImplement) {}
func ( *Names) () *Names {
	.once.Do(func() {
		if len(.List) > 0 {
			.has = make(map[protoreflect.Name]int, len(.List))
			for ,  := range .List {
				.has[] = .has[] + 1
			}
		}
	})
	return 
}

// CheckValid reports any errors with the set of names with an error message
// that completes the sentence: "ranges is invalid because it has ..."
func ( *Names) () error {
	for ,  := range .lazyInit().has {
		switch {
		case  > 1:
			return errors.New("duplicate name: %q", )
		case false && !.IsValid():
			// NOTE: The C++ implementation does not validate the identifier.
			// See https://github.com/protocolbuffers/protobuf/issues/6335.
			return errors.New("invalid name: %q", )
		}
	}
	return nil
}

type EnumRanges struct {
	List   [][2]protoreflect.EnumNumber // start inclusive; end inclusive
	once   sync.Once
	sorted [][2]protoreflect.EnumNumber // protected by once
}

func ( *EnumRanges) () int                             { return len(.List) }
func ( *EnumRanges) ( int) [2]protoreflect.EnumNumber { return .List[] }
func ( *EnumRanges) ( protoreflect.EnumNumber) bool {
	for  := .lazyInit().sorted; len() > 0; {
		 := len() / 2
		switch  := enumRange([]); {
		case  < .Start():
			 = [:] // search lower
		case  > .End():
			 = [+1:] // search upper
		default:
			return true
		}
	}
	return false
}
func ( *EnumRanges) ( fmt.State,  rune)          { descfmt.FormatList(, , ) }
func ( *EnumRanges) (pragma.DoNotImplement) {}
func ( *EnumRanges) () *EnumRanges {
	.once.Do(func() {
		.sorted = append(.sorted, .List...)
		sort.Slice(.sorted, func(,  int) bool {
			return .sorted[][0] < .sorted[][0]
		})
	})
	return 
}

// CheckValid reports any errors with the set of names with an error message
// that completes the sentence: "ranges is invalid because it has ..."
func ( *EnumRanges) () error {
	var  enumRange
	for ,  := range .lazyInit().sorted {
		 := enumRange()
		switch {
		case !(.Start() <= .End()):
			return errors.New("invalid range: %v", )
		case !(.End() < .Start()) &&  > 0:
			return errors.New("overlapping ranges: %v with %v", , )
		}
		 = 
	}
	return nil
}

type enumRange [2]protoreflect.EnumNumber

func ( enumRange) () protoreflect.EnumNumber { return [0] } // inclusive
func ( enumRange) () protoreflect.EnumNumber   { return [1] } // inclusive
func ( enumRange) () string {
	if .Start() == .End() {
		return fmt.Sprintf("%d", .Start())
	}
	return fmt.Sprintf("%d to %d", .Start(), .End())
}

type FieldRanges struct {
	List   [][2]protoreflect.FieldNumber // start inclusive; end exclusive
	once   sync.Once
	sorted [][2]protoreflect.FieldNumber // protected by once
}

func ( *FieldRanges) () int                              { return len(.List) }
func ( *FieldRanges) ( int) [2]protoreflect.FieldNumber { return .List[] }
func ( *FieldRanges) ( protoreflect.FieldNumber) bool {
	for  := .lazyInit().sorted; len() > 0; {
		 := len() / 2
		switch  := fieldRange([]); {
		case  < .Start():
			 = [:] // search lower
		case  > .End():
			 = [+1:] // search upper
		default:
			return true
		}
	}
	return false
}
func ( *FieldRanges) ( fmt.State,  rune)          { descfmt.FormatList(, , ) }
func ( *FieldRanges) (pragma.DoNotImplement) {}
func ( *FieldRanges) () *FieldRanges {
	.once.Do(func() {
		.sorted = append(.sorted, .List...)
		sort.Slice(.sorted, func(,  int) bool {
			return .sorted[][0] < .sorted[][0]
		})
	})
	return 
}

// CheckValid reports any errors with the set of ranges with an error message
// that completes the sentence: "ranges is invalid because it has ..."
func ( *FieldRanges) ( bool) error {
	var  fieldRange
	for ,  := range .lazyInit().sorted {
		 := fieldRange()
		switch {
		case !isValidFieldNumber(.Start(), ):
			return errors.New("invalid field number: %d", .Start())
		case !isValidFieldNumber(.End(), ):
			return errors.New("invalid field number: %d", .End())
		case !(.Start() <= .End()):
			return errors.New("invalid range: %v", )
		case !(.End() < .Start()) &&  > 0:
			return errors.New("overlapping ranges: %v with %v", , )
		}
		 = 
	}
	return nil
}

// isValidFieldNumber reports whether the field number is valid.
// Unlike the FieldNumber.IsValid method, it allows ranges that cover the
// reserved number range.
func ( protoreflect.FieldNumber,  bool) bool {
	return protowire.MinValidNumber <=  && ( <= protowire.MaxValidNumber || )
}

// CheckOverlap reports an error if p and q overlap.
func ( *FieldRanges) ( *FieldRanges) error {
	 := .lazyInit().sorted
	 := .lazyInit().sorted
	for ,  := 0, 0;  < len() &&  < len(); {
		 := fieldRange([])
		 := fieldRange([])
		if !(.End() < .Start() || .End() < .Start()) {
			return errors.New("overlapping ranges: %v with %v", , )
		}
		if .Start() < .Start() {
			++
		} else {
			++
		}
	}
	return nil
}

type fieldRange [2]protoreflect.FieldNumber

func ( fieldRange) () protoreflect.FieldNumber { return [0] }     // inclusive
func ( fieldRange) () protoreflect.FieldNumber   { return [1] - 1 } // inclusive
func ( fieldRange) () string {
	if .Start() == .End() {
		return fmt.Sprintf("%d", .Start())
	}
	return fmt.Sprintf("%d to %d", .Start(), .End())
}

type FieldNumbers struct {
	List []protoreflect.FieldNumber
	once sync.Once
	has  map[protoreflect.FieldNumber]struct{} // protected by once
}

func ( *FieldNumbers) () int                           { return len(.List) }
func ( *FieldNumbers) ( int) protoreflect.FieldNumber { return .List[] }
func ( *FieldNumbers) ( protoreflect.FieldNumber) bool {
	.once.Do(func() {
		if len(.List) > 0 {
			.has = make(map[protoreflect.FieldNumber]struct{}, len(.List))
			for ,  := range .List {
				.has[] = struct{}{}
			}
		}
	})
	,  := .has[]
	return 
}
func ( *FieldNumbers) ( fmt.State,  rune)          { descfmt.FormatList(, , ) }
func ( *FieldNumbers) (pragma.DoNotImplement) {}

type OneofFields struct {
	List   []protoreflect.FieldDescriptor
	once   sync.Once
	byName map[protoreflect.Name]protoreflect.FieldDescriptor        // protected by once
	byJSON map[string]protoreflect.FieldDescriptor                   // protected by once
	byText map[string]protoreflect.FieldDescriptor                   // protected by once
	byNum  map[protoreflect.FieldNumber]protoreflect.FieldDescriptor // protected by once
}

func ( *OneofFields) () int                               { return len(.List) }
func ( *OneofFields) ( int) protoreflect.FieldDescriptor { return .List[] }
func ( *OneofFields) ( protoreflect.Name) protoreflect.FieldDescriptor {
	return .lazyInit().byName[]
}
func ( *OneofFields) ( string) protoreflect.FieldDescriptor {
	return .lazyInit().byJSON[]
}
func ( *OneofFields) ( string) protoreflect.FieldDescriptor {
	return .lazyInit().byText[]
}
func ( *OneofFields) ( protoreflect.FieldNumber) protoreflect.FieldDescriptor {
	return .lazyInit().byNum[]
}
func ( *OneofFields) ( fmt.State,  rune)          { descfmt.FormatList(, , ) }
func ( *OneofFields) (pragma.DoNotImplement) {}

func ( *OneofFields) () *OneofFields {
	.once.Do(func() {
		if len(.List) > 0 {
			.byName = make(map[protoreflect.Name]protoreflect.FieldDescriptor, len(.List))
			.byJSON = make(map[string]protoreflect.FieldDescriptor, len(.List))
			.byText = make(map[string]protoreflect.FieldDescriptor, len(.List))
			.byNum = make(map[protoreflect.FieldNumber]protoreflect.FieldDescriptor, len(.List))
			for ,  := range .List {
				// Field names and numbers are guaranteed to be unique.
				.byName[.Name()] = 
				.byJSON[.JSONName()] = 
				.byText[.TextName()] = 
				.byNum[.Number()] = 
			}
		}
	})
	return 
}

type SourceLocations struct {
	// List is a list of SourceLocations.
	// The SourceLocation.Next field does not need to be populated
	// as it will be lazily populated upon first need.
	List []protoreflect.SourceLocation

	// File is the parent file descriptor that these locations are relative to.
	// If non-nil, ByDescriptor verifies that the provided descriptor
	// is a child of this file descriptor.
	File protoreflect.FileDescriptor

	once   sync.Once
	byPath map[pathKey]int
}

func ( *SourceLocations) () int                              { return len(.List) }
func ( *SourceLocations) ( int) protoreflect.SourceLocation { return .lazyInit().List[] }
func ( *SourceLocations) ( pathKey) protoreflect.SourceLocation {
	if ,  := .lazyInit().byPath[];  {
		return .List[]
	}
	return protoreflect.SourceLocation{}
}
func ( *SourceLocations) ( protoreflect.SourcePath) protoreflect.SourceLocation {
	return .byKey(newPathKey())
}
func ( *SourceLocations) ( protoreflect.Descriptor) protoreflect.SourceLocation {
	if .File != nil &&  != nil && .File != .ParentFile() {
		return protoreflect.SourceLocation{} // mismatching parent files
	}
	var  [16]int32
	 := [:0]
	for {
		switch .(type) {
		case protoreflect.FileDescriptor:
			// Reverse the path since it was constructed in reverse.
			for ,  := 0, len()-1;  < ; ,  = +1, -1 {
				[], [] = [], []
			}
			return .byKey(newPathKey())
		case protoreflect.MessageDescriptor:
			 = append(, int32(.Index()))
			 = .Parent()
			switch .(type) {
			case protoreflect.FileDescriptor:
				 = append(, int32(genid.FileDescriptorProto_MessageType_field_number))
			case protoreflect.MessageDescriptor:
				 = append(, int32(genid.DescriptorProto_NestedType_field_number))
			default:
				return protoreflect.SourceLocation{}
			}
		case protoreflect.FieldDescriptor:
			 := .(protoreflect.FieldDescriptor).IsExtension()
			 = append(, int32(.Index()))
			 = .Parent()
			if  {
				switch .(type) {
				case protoreflect.FileDescriptor:
					 = append(, int32(genid.FileDescriptorProto_Extension_field_number))
				case protoreflect.MessageDescriptor:
					 = append(, int32(genid.DescriptorProto_Extension_field_number))
				default:
					return protoreflect.SourceLocation{}
				}
			} else {
				switch .(type) {
				case protoreflect.MessageDescriptor:
					 = append(, int32(genid.DescriptorProto_Field_field_number))
				default:
					return protoreflect.SourceLocation{}
				}
			}
		case protoreflect.OneofDescriptor:
			 = append(, int32(.Index()))
			 = .Parent()
			switch .(type) {
			case protoreflect.MessageDescriptor:
				 = append(, int32(genid.DescriptorProto_OneofDecl_field_number))
			default:
				return protoreflect.SourceLocation{}
			}
		case protoreflect.EnumDescriptor:
			 = append(, int32(.Index()))
			 = .Parent()
			switch .(type) {
			case protoreflect.FileDescriptor:
				 = append(, int32(genid.FileDescriptorProto_EnumType_field_number))
			case protoreflect.MessageDescriptor:
				 = append(, int32(genid.DescriptorProto_EnumType_field_number))
			default:
				return protoreflect.SourceLocation{}
			}
		case protoreflect.EnumValueDescriptor:
			 = append(, int32(.Index()))
			 = .Parent()
			switch .(type) {
			case protoreflect.EnumDescriptor:
				 = append(, int32(genid.EnumDescriptorProto_Value_field_number))
			default:
				return protoreflect.SourceLocation{}
			}
		case protoreflect.ServiceDescriptor:
			 = append(, int32(.Index()))
			 = .Parent()
			switch .(type) {
			case protoreflect.FileDescriptor:
				 = append(, int32(genid.FileDescriptorProto_Service_field_number))
			default:
				return protoreflect.SourceLocation{}
			}
		case protoreflect.MethodDescriptor:
			 = append(, int32(.Index()))
			 = .Parent()
			switch .(type) {
			case protoreflect.ServiceDescriptor:
				 = append(, int32(genid.ServiceDescriptorProto_Method_field_number))
			default:
				return protoreflect.SourceLocation{}
			}
		default:
			return protoreflect.SourceLocation{}
		}
	}
}
func ( *SourceLocations) () *SourceLocations {
	.once.Do(func() {
		if len(.List) > 0 {
			// Collect all the indexes for a given path.
			 := make(map[pathKey][]int, len(.List))
			for ,  := range .List {
				 := newPathKey(.Path)
				[] = append([], )
			}

			// Update the next index for all locations.
			.byPath = make(map[pathKey]int, len(.List))
			for ,  := range  {
				for  := 0;  < len()-1; ++ {
					.List[[]].Next = [+1]
				}
				.List[[len()-1]].Next = 0
				.byPath[] = [0] // record the first location for this path
			}
		}
	})
	return 
}
func ( *SourceLocations) (pragma.DoNotImplement) {}

// pathKey is a comparable representation of protoreflect.SourcePath.
type pathKey struct {
	arr [16]uint8 // first n-1 path segments; last element is the length
	str string    // used if the path does not fit in arr
}

func ( protoreflect.SourcePath) ( pathKey) {
	if len() < len(.arr) {
		for ,  := range  {
			if  < 0 || math.MaxUint8 <=  {
				return pathKey{str: .String()}
			}
			.arr[] = uint8()
		}
		.arr[len(.arr)-1] = uint8(len())
		return 
	}
	return pathKey{str: .String()}
}