tdp

package
v0.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 24, 2025 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package tdp contains the "object file format" used by hyperpb's parser. "TDP" stands for "table-driven parser".

Subpackages of this package contain other components, like the parser and compiler.

All fields in this package are exported because they are assembled and accessed by other internal packages. None of the types in this file should ever be exposed to users, either directly or as the type of an interface.

Index

Constants

View Source
const SignBits = 0x80_80_80_80_80_80_80_80

Variables

This section is empty.

Functions

This section is empty.

Types

type Accessor

type Accessor struct {
	Offset Offset

	// The Getter for extracting the field.
	Getter Getter
}

Accessor is all the information necessary for accessing a field of a [message].

func (*Accessor) Format

func (a *Accessor) Format(s fmt.State, verb rune)

Format implements fmt.Formatter.

type Aux

type Aux struct {
	Layout debug.Value[TypeLayout]

	Library          *Library
	Descriptor       protoreflect.MessageDescriptor
	Methods          protoiface.Methods
	FieldDescriptors []protoreflect.FieldDescriptor

	// Field indices that are required or contain required fields.
	// Negative numbers are the complement of a message field which
	// might contain required fields.
	Required []int32
}

Aux is data on a typeHeader that is stored behind a pointer and kept alive in the traces struct in [compiler.compile]. These rarely-accessed fields ensure that parser-relevant data is closer together in cache.

type Field

type Field struct {

	// The Message type for this field, if there is one.
	Message *Type
	Accessor
	// contains filtered or unexported fields
}

Field is an optimized descriptor for a message field.

func (*Field) Format

func (f *Field) Format(s fmt.State, verb rune)

Format implements fmt.Formatter.

func (*Field) Get

Get gets the value of this field out of a message of appropriate type. Returns nil if the field is unset.

This performs no type-checking! Callers are responsible for ensuring that m is of the correct type.

func (*Field) IsValid

func (f *Field) IsValid() bool

IsValid returns whether or not this is the sentinel invalid field in a Type's field table.

type FieldLayout

type FieldLayout struct {
	Size, Align, Bits, Padding uint32

	Index  int // Which field is this in the MessageDescriptor?
	Offset Offset
}

FieldLayout is layout information for a [field]. Only for debugging.

type FieldParser

type FieldParser struct {

	// The expected, partially decoded Tag value for the field.
	Tag Tag

	// Byte offset to the typeParser this fieldParser uses, if any.
	Message *TypeParser

	// Field Offset information for the field this parser parses. Duplicated
	// from [getter].
	Offset Offset

	// For non-singular fields, the default size to preallocate for this field.
	Preload uint32

	// The parser to jump to after this one, depending on whether the parse
	// succeeds or fails.
	NextOk, NextErr xunsafe.Addr[FieldParser]

	// The thunk to call for this field. The type of this thunk is stored in
	// the parser package.
	Parse uintptr
	// contains filtered or unexported fields
}

FieldParser is a parser for a single field.

func (*FieldParser) Format

func (p *FieldParser) Format(s fmt.State, verb rune)

Format implements fmt.Formatter.

type Getter

type Getter func(unsafe.Pointer, *Type, *Accessor) protoreflect.Value

Getter is a thunk for extracting a value out of a field.

type Int

type Int interface {
	~int8 | ~uint8 | ~int32 | ~int64 | ~uint32 | ~uint64
}

Int is any of the integer types that this package has to handle generically.

type Library

type Library struct {
	Base  *Type
	Types map[protoreflect.MessageDescriptor]*Type
	Bytes int

	// Used to store compilation metadata. Actually a []hyperpb.CompileOptions.
	Metadata any
}

Library represents the full output of [Compile]. Given any Type, it can be used to obtain the Type for any other message type that was compiled as part of the same call to [Compile].

func (*Library) AtOffset

func (l *Library) AtOffset(n uint32) *Type

AtOffset the Type at the give byte offset in this Library.

func (*Library) Type

Type returns the Type for the given descriptor in this library.

If not present, returns false.

type Number

type Number interface {
	Int | ~float32 | ~float64
}

Number is anything from Int, or a float.

type Offset

type Offset struct {
	// First Bit index for bits allocated to this field.
	//
	// If this is a oneof field, this is instead an offset into the
	// oneof word table.
	Bit uint32

	// Byte offset within the containing message to the Data for this field.
	//
	// If negative, this is a cold field, and this is the negation of an offset
	// into the cold field area.
	Data int32

	// This field's Number. Only used by oneof fields; all other fields have
	// a zero here.
	Number uint32
}

Offset is field offset information for a generated message type's field.

func (Offset) Format

func (o Offset) Format(s fmt.State, verb rune)

Format implements fmt.Formatter.

type Scalar

type Scalar interface {
	int32 | int64 |
		uint32 | uint64 |
		float32 | float64 |
		protoreflect.EnumNumber | bool
}

Scalar is a Protobuf scalar type.

type Tag

type Tag uint64

Tag is a specially-formatted tag for the parser.

The tag is formatted in the way it would be when encoded in a Protobuf message, but with the high bit of each byte cleared.

func EncodeTag

func EncodeTag(n protowire.Number, t protowire.Type) Tag

encode encodes this field tag from the given number and type.

func (Tag) Decode

func (t Tag) Decode() uint64

Decode decodes this field tag into a number and a type.

func (Tag) Format

func (t Tag) Format(s fmt.State, verb rune)

Format implements fmt.Formatter.

func (Tag) Overflows

func (t Tag) Overflows() bool

Returns whether this tag is "too large", i.e., if it has more than 32 bits when decoded.

type Type

type Type struct {
	*Aux

	// The number of bytes of memory that must be allocated for a *message of
	// this type. This includes the Size of the header. Alignment is implicitly
	// that of uint64.
	Size, ColdSize uint32

	// The "unspecialized" Parser for this type.
	Parser *TypeParser

	// Maps field Numbers to offsets in fields.
	Numbers *swiss.Table[int32, uint32]

	// The number of fields that follow this type, not including the special
	// padding field with number equal to zero.
	Count uint32
	// contains filtered or unexported fields
}

func (*Type) ByDescriptor

func (t *Type) ByDescriptor(fd protoreflect.FieldDescriptor) *Field

ByDescriptor returns the field with the given descriptor.

func (*Type) ByIndex

func (t *Type) ByIndex(n int) *Field

byIndex returns the nth byIndex (in byIndex number order) for this type.

If n == 0 and this type has no fields, returns a byIndex with an invalid byIndex number.

This function does not perform bounds checks.

func (*Type) Format

func (t *Type) Format(s fmt.State, verb rune)

Format implements fmt.Formatter.

func (*Type) ProtoReflect

func (t *Type) ProtoReflect() protoreflect.MessageType

ProtoReflect wraps this type for reflection.

func (*Type) Submessages

func (t *Type) Submessages() iter.Seq[*Type]

Submessages returns an iterator over the types of submessage fields in this type.

type TypeLayout

type TypeLayout struct {
	BitWords int           // Number of 32-bit words in the type.
	Fields   []FieldLayout // Sorted in offset order.
}

TypeLayout is layout information for a Type. Only for debugging.

type TypeParser

type TypeParser struct {

	// Maps offsets to field tags for the first 128 field tags. A value of
	// -1 means that if there is a parser at that position, it is farther away
	// than the first 256 fields.
	TagLUT [128]uint8

	TypeOffset     uint32 // The type that this parser parses.
	DiscardUnknown bool   // Should unknown fields be kept?

	// Maps field tags to offsets in fields.
	Tags *swiss.Table[int32, uint32]

	// If this is an ordinary parser, this is the parser for parsing this
	// message as a "map entry"; that is, it will have a single field with
	// number 2 that forwards to this parser.
	MapEntry *TypeParser

	Entrypoint FieldParser
	// contains filtered or unexported fields
}

TypeParser is a parser for some Type. A Type may have multiple parsers.

func (*TypeParser) Fields

func (p *TypeParser) Fields() *xunsafe.VLA[FieldParser]

Fields returns a raw pointer to this parser's field array.

func (*TypeParser) Format

func (p *TypeParser) Format(s fmt.State, verb rune)

Format implements fmt.Formatter.

Directories

Path Synopsis
linker
Package linker provides a general-purpose in-memory linker, that is used to assemble the output buffer within the compiler.
Package linker provides a general-purpose in-memory linker, that is used to assemble the output buffer within the compiler.
Package dynamic contains the implementation of hyperpb's dynamic message types.
Package dynamic contains the implementation of hyperpb's dynamic message types.
Package maps contains shared layouts for maps field implementations, for sharing between the tdp packages and the gencode packages.
Package maps contains shared layouts for maps field implementations, for sharing between the tdp packages and the gencode packages.
Package repeated contains shared layouts for repeated field implementations, for sharing between the tdp packages and the gencode packages.
Package repeated contains shared layouts for repeated field implementations, for sharing between the tdp packages and the gencode packages.
Package thunks provides all thunks for the parser VM.
Package thunks provides all thunks for the parser VM.
Package vm contains the core interpreter VM for the hyperpb parser.
Package vm contains the core interpreter VM for the hyperpb parser.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL