Skip to content

Commit

Permalink
The mini descriptor encoder now uses the internal api (upb_MessageDef…
Browse files Browse the repository at this point in the history
…, upb_FileDef, etc) instead of dealing directly with the proto fields. So much simpler and cleaner!

PiperOrigin-RevId: 454389613
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Jun 12, 2022
1 parent bb38fc9 commit 65cd34d
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 360 deletions.
8 changes: 8 additions & 0 deletions upb/def.c
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,14 @@ bool upb_FieldDef_IsString(const upb_FieldDef* f) {
upb_FieldDef_CType(f) == kUpb_CType_Bytes;
}

bool upb_FieldDef_IsOptional(const upb_FieldDef* f) {
return upb_FieldDef_Label(f) == kUpb_Label_Optional;
}

bool upb_FieldDef_IsRequired(const upb_FieldDef* f) {
return upb_FieldDef_Label(f) == kUpb_Label_Required;
}

bool upb_FieldDef_IsRepeated(const upb_FieldDef* f) {
return upb_FieldDef_Label(f) == kUpb_Label_Repeated;
}
Expand Down
2 changes: 2 additions & 0 deletions upb/def.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ const upb_OneofDef* upb_FieldDef_RealContainingOneof(const upb_FieldDef* f);
uint32_t upb_FieldDef_Index(const upb_FieldDef* f);
bool upb_FieldDef_IsSubMessage(const upb_FieldDef* f);
bool upb_FieldDef_IsString(const upb_FieldDef* f);
bool upb_FieldDef_IsOptional(const upb_FieldDef* f);
bool upb_FieldDef_IsRequired(const upb_FieldDef* f);
bool upb_FieldDef_IsRepeated(const upb_FieldDef* f);
bool upb_FieldDef_IsPrimitive(const upb_FieldDef* f);
bool upb_FieldDef_IsMap(const upb_FieldDef* f);
Expand Down
232 changes: 66 additions & 166 deletions upb/mini_descriptor.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,9 @@

#include "upb/mini_descriptor.h"

#include <assert.h>
#include <inttypes.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

#include "upb/def.h"
#include "upb/mini_table.h"

// Must be last.
Expand Down Expand Up @@ -86,90 +81,25 @@ static void upb_DescState_Emit(const DescState* d, upb_StringView* str) {

/******************************************************************************/

// Type and Field accessors.

static inline bool upb_Type_IsPackable(upb_FieldType type) {
return (type != kUpb_FieldType_String && type != kUpb_FieldType_Group &&
type != kUpb_FieldType_Message && type != kUpb_FieldType_Bytes);
}

static inline bool upb_Field_IsOneof(const google_protobuf_FieldDescriptorProto* f) {
return google_protobuf_FieldDescriptorProto_has_oneof_index(f);
}

static inline bool upb_Field_IsOptional(const google_protobuf_FieldDescriptorProto* f) {
const upb_Label label = google_protobuf_FieldDescriptorProto_label(f);
return label == kUpb_Label_Optional;
}

static inline bool upb_Field_IsRepeated(const google_protobuf_FieldDescriptorProto* f) {
const upb_Label label = google_protobuf_FieldDescriptorProto_label(f);
return label == kUpb_Label_Repeated;
}

static inline bool upb_Field_IsRequired(const google_protobuf_FieldDescriptorProto* f) {
const upb_Label label = google_protobuf_FieldDescriptorProto_label(f);
return label == kUpb_Label_Required;
}

static inline bool upb_Field_IsPackable(const google_protobuf_FieldDescriptorProto* f) {
if (!upb_Field_IsRepeated(f)) return false;

const upb_FieldType type = google_protobuf_FieldDescriptorProto_type(f);
return upb_Type_IsPackable(type);
}

static bool upb_Field_IsPacked(const google_protobuf_FieldDescriptorProto* f,
upb_Syntax syntax) {
if (!upb_Field_IsPackable(f)) return false;

const bool has_options = google_protobuf_FieldDescriptorProto_has_options(f);
const google_protobuf_FieldOptions* options = google_protobuf_FieldDescriptorProto_options(f);

switch (syntax) {
case kUpb_Syntax_Proto2:
if (!has_options) return false;
break;

default:
if (!has_options) return true;
if (!google_protobuf_FieldOptions_has_packed(options)) return true;
break;
}

return google_protobuf_FieldOptions_packed(options);
}

static inline int Field_OneofIndex(const google_protobuf_FieldDescriptorProto* f) {
return google_protobuf_FieldDescriptorProto_oneof_index(f);
}

static bool upb_Field_HasPresence(const google_protobuf_FieldDescriptorProto* f,
upb_Syntax syntax) {
if (upb_Field_IsRepeated(f)) return false;

const upb_FieldType type = google_protobuf_FieldDescriptorProto_type(f);
return type == kUpb_FieldType_Message || type == kUpb_FieldType_Group ||
upb_Field_IsOneof(f) || syntax == kUpb_Syntax_Proto2;
}

uint64_t upb_Field_Modifier(const google_protobuf_FieldDescriptorProto* f,
upb_Syntax syntax) {
// Copied from upbc/protoc-gen-upb.cc TODO(salo): can we consolidate?
static uint64_t upb_Field_Modifier(const upb_FieldDef* f) {
uint64_t out = 0;
if (upb_Field_IsRepeated(f)) {
if (upb_FieldDef_IsRepeated(f)) {
out |= kUpb_FieldModifier_IsRepeated;
}
if (upb_Field_IsPacked(f, syntax)) {
if (upb_FieldDef_IsPacked(f)) {
out |= kUpb_FieldModifier_IsPacked;
}
if (google_protobuf_FieldDescriptorProto_type(f) == kUpb_FieldType_Enum &&
syntax == kUpb_Syntax_Proto2) {
out |= kUpb_FieldModifier_IsClosedEnum;
if (upb_FieldDef_Type(f) == kUpb_FieldType_Enum) {
const upb_FileDef* file_def = upb_EnumDef_File(upb_FieldDef_EnumSubDef(f));
if (upb_FileDef_Syntax(file_def) == kUpb_Syntax_Proto2) {
out |= kUpb_FieldModifier_IsClosedEnum;
}
}
if (upb_Field_IsOptional(f) && !upb_Field_HasPresence(f, syntax)) {
if (upb_FieldDef_IsOptional(f) && !upb_FieldDef_HasPresence(f)) {
out |= kUpb_FieldModifier_IsProto3Singular;
}
if (upb_Field_IsRequired(f)) {
if (upb_FieldDef_IsRequired(f)) {
out |= kUpb_FieldModifier_IsRequired;
}
return out;
Expand All @@ -179,71 +109,51 @@ uint64_t upb_Field_Modifier(const google_protobuf_FieldDescriptorProto* f,

// Sort by enum value.
static int upb_MiniDescriptor_CompareEnums(const void* a, const void* b) {
const google_protobuf_EnumValueDescriptorProto* A = *(void**)a;
const google_protobuf_EnumValueDescriptorProto* B = *(void**)b;
if ((uint32_t)google_protobuf_EnumValueDescriptorProto_number(A) <
(uint32_t)google_protobuf_EnumValueDescriptorProto_number(B))
const upb_EnumValueDef* A = *(void**)a;
const upb_EnumValueDef* B = *(void**)b;
if ((uint32_t)upb_EnumValueDef_Number(A) <
(uint32_t)upb_EnumValueDef_Number(B))
return -1;
if ((uint32_t)google_protobuf_EnumValueDescriptorProto_number(A) >
(uint32_t)google_protobuf_EnumValueDescriptorProto_number(B))
if ((uint32_t)upb_EnumValueDef_Number(A) >
(uint32_t)upb_EnumValueDef_Number(B))
return 1;
return 0;
}

// Sort by field number.
static int upb_MiniDescriptor_CompareFields(const void* a, const void* b) {
const google_protobuf_FieldDescriptorProto* A = *(void**)a;
const google_protobuf_FieldDescriptorProto* B = *(void**)b;
if (google_protobuf_FieldDescriptorProto_number(A) <
google_protobuf_FieldDescriptorProto_number(B))
return -1;
if (google_protobuf_FieldDescriptorProto_number(A) >
google_protobuf_FieldDescriptorProto_number(B))
return 1;
const upb_FieldDef* A = *(void**)a;
const upb_FieldDef* B = *(void**)b;
if (upb_FieldDef_Number(A) < upb_FieldDef_Number(B)) return -1;
if (upb_FieldDef_Number(A) > upb_FieldDef_Number(B)) return 1;
return 0;
}

// Sort first by oneof index then by field number.
static int upb_MiniDescriptor_CompareOneofs(const void* a, const void* b) {
const google_protobuf_FieldDescriptorProto* A = *(void**)a;
const google_protobuf_FieldDescriptorProto* B = *(void**)b;
const int indexA = upb_Field_IsOneof(A) ? Field_OneofIndex(A) : -1;
const int indexB = upb_Field_IsOneof(B) ? Field_OneofIndex(B) : -1;
if (indexA < indexB) return -1;
if (indexA > indexB) return 1;
if (google_protobuf_FieldDescriptorProto_number(A) <
google_protobuf_FieldDescriptorProto_number(B))
return -1;
if (google_protobuf_FieldDescriptorProto_number(A) >
google_protobuf_FieldDescriptorProto_number(B))
return 1;
return 0;
}

upb_StringView upb_MiniDescriptor_EncodeEnum(
const google_protobuf_EnumDescriptorProto* enum_type, upb_Arena* a) {
upb_StringView upb_MiniDescriptor_EncodeEnum(const upb_EnumDef* enum_def,
upb_Arena* a) {
upb_StringView out;
out.data = NULL;
out.size = 0;

size_t len = 0;
const google_protobuf_EnumValueDescriptorProto* const* value_types =
google_protobuf_EnumDescriptorProto_value(enum_type, &len);
DescState s;
upb_DescState_Init(&s);

// Copy and sort.
google_protobuf_EnumValueDescriptorProto** sorted = upb_gmalloc(len * sizeof(void*));
const size_t len = upb_EnumDef_ValueCount(enum_def);
const upb_EnumValueDef** sorted = upb_gmalloc(len * sizeof(void*));
if (!sorted) goto err;
memcpy(sorted, value_types, len * sizeof(void*));
qsort(sorted, len, sizeof(void*), upb_MiniDescriptor_CompareEnums);

DescState s;
upb_DescState_Init(&s);
for (size_t i = 0; i < len; i++) {
sorted[i] = upb_EnumDef_Value(enum_def, i);
}
qsort(sorted, len, sizeof(void*), upb_MiniDescriptor_CompareEnums);

upb_MtDataEncoder_StartEnum(&s.e);

for (size_t i = 0; i < len; i++) {
if (!upb_DescState_Grow(&s, a)) goto err;
const uint32_t number = google_protobuf_EnumValueDescriptorProto_number(sorted[i]);
const upb_EnumValueDef* value_def = sorted[i];
const int number = upb_EnumValueDef_Number(value_def);
s.ptr = upb_MtDataEncoder_PutEnumValue(&s.e, s.ptr, number);
UPB_ASSERT(s.ptr);
}
Expand All @@ -259,9 +169,8 @@ upb_StringView upb_MiniDescriptor_EncodeEnum(
return out;
}

upb_StringView upb_MiniDescriptor_EncodeExtension(
const google_protobuf_FieldDescriptorProto* extension_type, upb_Syntax syntax,
upb_Arena* a) {
upb_StringView upb_MiniDescriptor_EncodeExtension(const upb_FieldDef* field_def,
upb_Arena* a) {
upb_StringView out;
out.data = NULL;
out.size = 0;
Expand All @@ -272,9 +181,10 @@ upb_StringView upb_MiniDescriptor_EncodeExtension(
if (!upb_DescState_Grow(&s, a)) goto err;
upb_MtDataEncoder_StartMessage(&s.e, s.ptr, 0);

const upb_FieldType type = google_protobuf_FieldDescriptorProto_type(extension_type);
const int number = google_protobuf_FieldDescriptorProto_number(extension_type);
const uint64_t modifier = upb_Field_Modifier(extension_type, syntax);
UPB_ASSERT(upb_FieldDef_IsExtension(field_def));
const upb_FieldType type = upb_FieldDef_Type(field_def);
const int number = upb_FieldDef_Number(field_def);
const uint64_t modifier = upb_Field_Modifier(field_def);
upb_MtDataEncoder_PutField(&s.e, s.ptr, type, number, modifier);

upb_DescState_Emit(&s, &out);
Expand All @@ -284,67 +194,57 @@ upb_StringView upb_MiniDescriptor_EncodeExtension(
}

upb_StringView upb_MiniDescriptor_EncodeMessage(
const google_protobuf_DescriptorProto* message_type, upb_Syntax syntax,
upb_Arena* a) {
const upb_MessageDef* message_def, upb_Arena* a) {
upb_StringView out;
out.data = NULL;
out.size = 0;

size_t len = 0;
const google_protobuf_FieldDescriptorProto* const* field_types =
google_protobuf_DescriptorProto_field(message_type, &len);
DescState s;
upb_DescState_Init(&s);

// Copy and sort.
google_protobuf_FieldDescriptorProto** sorted = upb_gmalloc(len * sizeof(void*));
// Make a copy.
const size_t len = upb_MessageDef_FieldCount(message_def);
const upb_FieldDef** sorted = upb_gmalloc(len * sizeof(void*));
if (!sorted) goto err;
memcpy(sorted, field_types, len * sizeof(void*));
qsort(sorted, len, sizeof(void*), upb_MiniDescriptor_CompareFields);

DescState s;
upb_DescState_Init(&s);
// Sort the copy.
for (size_t i = 0; i < len; i++) {
sorted[i] = upb_MessageDef_Field(message_def, i);
}
qsort(sorted, len, sizeof(void*), upb_MiniDescriptor_CompareFields);

if (!upb_DescState_Grow(&s, a)) goto err;
upb_MtDataEncoder_StartMessage(&s.e, s.ptr, 0);

// Encode the fields.
size_t oneof_fields = 0;
for (size_t i = 0; i < len; i++) {
google_protobuf_FieldDescriptorProto* field_type = sorted[i];
if (upb_Field_IsOneof(field_type)) {
// Put all oneof fields at the beginning of the list for the next pass.
sorted[oneof_fields++] = field_type;
}

const upb_FieldType type = google_protobuf_FieldDescriptorProto_type(field_type);
const int number = google_protobuf_FieldDescriptorProto_number(field_type);
const uint64_t modifier = upb_Field_Modifier(field_type, syntax);
const upb_FieldDef* field_def = sorted[i];
const int number = upb_FieldDef_Number(field_def);
const upb_FieldType type = upb_FieldDef_Type(field_def);
const uint64_t modifier = upb_Field_Modifier(field_def);

if (!upb_DescState_Grow(&s, a)) goto err;
s.ptr = upb_MtDataEncoder_PutField(&s.e, s.ptr, type, number, modifier);
UPB_ASSERT(s.ptr);
}

qsort(sorted, oneof_fields, sizeof(void*), upb_MiniDescriptor_CompareOneofs);

// Encode the oneofs.
int previous_index = -1;
for (size_t i = 0; i < oneof_fields; i++) {
google_protobuf_FieldDescriptorProto* field_type = sorted[i];
if (!upb_Field_IsOneof(field_type)) continue;
const int oneof_count = upb_MessageDef_OneofCount(message_def);
for (int i = 0; i < oneof_count; i++) {
if (!upb_DescState_Grow(&s, a)) goto err;
s.ptr = upb_MtDataEncoder_StartOneof(&s.e, s.ptr);
UPB_ASSERT(s.ptr);

const upb_OneofDef* oneof_def = upb_MessageDef_Oneof(message_def, i);
const int field_count = upb_OneofDef_FieldCount(oneof_def);
for (int j = 0; j < field_count; j++) {
const upb_FieldDef* field_def = upb_OneofDef_Field(oneof_def, j);
const int number = upb_FieldDef_Number(field_def);

const int index = Field_OneofIndex(field_type);
if (previous_index != index) {
if (!upb_DescState_Grow(&s, a)) goto err;
s.ptr = upb_MtDataEncoder_StartOneof(&s.e, s.ptr);
s.ptr = upb_MtDataEncoder_PutOneofField(&s.e, s.ptr, number);
UPB_ASSERT(s.ptr);

previous_index = index;
}

if (!upb_DescState_Grow(&s, a)) goto err;
s.ptr = upb_MtDataEncoder_PutOneofField(
&s.e, s.ptr, google_protobuf_FieldDescriptorProto_number(field_type));
UPB_ASSERT(s.ptr);
}

upb_DescState_Emit(&s, &out);
Expand Down
12 changes: 5 additions & 7 deletions upb/mini_descriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,14 @@ extern "C" {

/** upb_MiniDescriptor ********************************************************/

upb_StringView upb_MiniDescriptor_EncodeEnum(
const google_protobuf_EnumDescriptorProto* enum_type, upb_Arena* a);
upb_StringView upb_MiniDescriptor_EncodeEnum(const upb_EnumDef* enum_def,
upb_Arena* a);

upb_StringView upb_MiniDescriptor_EncodeExtension(
const google_protobuf_FieldDescriptorProto* extension_type, upb_Syntax syntax,
upb_Arena* a);
upb_StringView upb_MiniDescriptor_EncodeExtension(const upb_FieldDef* field_def,
upb_Arena* a);

upb_StringView upb_MiniDescriptor_EncodeMessage(
const google_protobuf_DescriptorProto* message_type, upb_Syntax syntax,
upb_Arena* a);
const upb_MessageDef* message_def, upb_Arena* a);

#ifdef __cplusplus
} /* extern "C" */
Expand Down
Loading

0 comments on commit 65cd34d

Please sign in to comment.