move message/field modifiers functions out of mini_descriptor_encode.c

PiperOrigin-RevId: 477486367
pull/13171/head
Eric Salo 2 years ago committed by Copybara-Service
parent efd06e46a4
commit 1e3deb013d
  1. 34
      upb/reflection/field_def.c
  2. 4
      upb/reflection/field_def.h
  3. 12
      upb/reflection/message_def.c
  4. 1
      upb/reflection/message_def.h
  5. 46
      upb/reflection/mini_descriptor_encode.c

@ -268,6 +268,32 @@ bool _upb_FieldDef_IsProto3Optional(const upb_FieldDef* f) {
int _upb_FieldDef_LayoutIndex(const upb_FieldDef* f) { return f->layout_index; }
uint64_t _upb_FieldDef_Modifiers(const upb_FieldDef* f) {
uint64_t out = f->is_packed_ ? kUpb_FieldModifier_IsPacked : 0;
switch (f->label_) {
case kUpb_Label_Optional:
if (!upb_FieldDef_HasPresence(f)) {
out |= kUpb_FieldModifier_IsProto3Singular;
}
break;
case kUpb_Label_Repeated:
out |= kUpb_FieldModifier_IsRepeated;
break;
case kUpb_Label_Required:
out |= kUpb_FieldModifier_IsRequired;
break;
}
if (f->type_ == 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;
}
}
return out;
}
bool upb_FieldDef_HasDefault(const upb_FieldDef* f) { return f->has_default; }
bool upb_FieldDef_HasPresence(const upb_FieldDef* f) {
@ -295,14 +321,14 @@ bool upb_FieldDef_IsPrimitive(const upb_FieldDef* f) {
return !upb_FieldDef_IsString(f) && !upb_FieldDef_IsSubMessage(f);
}
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;
}
bool upb_FieldDef_IsRequired(const upb_FieldDef* f) {
return upb_FieldDef_Label(f) == kUpb_Label_Required;
}
bool upb_FieldDef_IsString(const upb_FieldDef* f) {
return upb_FieldDef_CType(f) == kUpb_CType_String ||
upb_FieldDef_CType(f) == kUpb_CType_Bytes;

@ -84,9 +84,7 @@ const upb_MiniTable_Extension* _upb_FieldDef_ExtensionMiniTable(
bool _upb_FieldDef_IsClosedEnum(const upb_FieldDef* f);
bool _upb_FieldDef_IsProto3Optional(const upb_FieldDef* f);
int _upb_FieldDef_LayoutIndex(const upb_FieldDef* f);
void _upb_FieldDef_MakeLayout(upb_DefBuilder* ctx, const upb_MessageDef* m);
uint64_t _upb_FieldDef_Modifiers(const upb_FieldDef* f);
void _upb_FieldDef_Resolve(upb_DefBuilder* ctx, const char* prefix,
upb_FieldDef* f);

@ -458,6 +458,18 @@ void _upb_MessageDef_LinkMiniTable(upb_DefBuilder* ctx,
}
}
uint64_t _upb_MessageDef_Modifiers(const upb_MessageDef* m) {
uint64_t out = 0;
if (upb_FileDef_Syntax(m->file) == kUpb_Syntax_Proto3) {
out |= kUpb_MessageModifier_ValidateUtf8;
out |= kUpb_MessageModifier_DefaultIsPacked;
}
if (m->ext_range_count) {
out |= kUpb_MessageModifier_IsExtendable;
}
return out;
}
static void create_msgdef(upb_DefBuilder* ctx, const char* prefix,
const google_protobuf_DescriptorProto* msg_proto,
const upb_MessageDef* containing_type,

@ -161,6 +161,7 @@ bool _upb_MessageDef_IsSorted(const upb_MessageDef* m);
bool _upb_MessageDef_IsValidExtensionNumber(const upb_MessageDef* m, int n);
void _upb_MessageDef_LinkMiniTable(upb_DefBuilder* ctx,
const upb_MessageDef* m);
uint64_t _upb_MessageDef_Modifiers(const upb_MessageDef* m);
void _upb_MessageDef_Resolve(upb_DefBuilder* ctx, upb_MessageDef* m);
// Allocate and initialize an array of |n| message defs.

@ -82,45 +82,6 @@ static bool upb_DescState_Grow(DescState* d, upb_Arena* a) {
/******************************************************************************/
// Copied from upbc/protoc-gen-upb.cc TODO(salo): can we consolidate?
static uint64_t upb_Field_Modifiers(const upb_FieldDef* f) {
uint64_t out = 0;
if (upb_FieldDef_IsRepeated(f)) {
out |= kUpb_FieldModifier_IsRepeated;
}
if (upb_FieldDef_IsPacked(f)) {
out |= kUpb_FieldModifier_IsPacked;
}
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_FieldDef_IsOptional(f) && !upb_FieldDef_HasPresence(f)) {
out |= kUpb_FieldModifier_IsProto3Singular;
}
if (upb_FieldDef_IsRequired(f)) {
out |= kUpb_FieldModifier_IsRequired;
}
return out;
}
static uint64_t upb_Message_Modifiers(const upb_MessageDef* m) {
uint64_t out = 0;
const upb_FileDef* file_def = upb_MessageDef_File(m);
if (upb_FileDef_Syntax(file_def) == kUpb_Syntax_Proto3) {
out |= kUpb_MessageModifier_ValidateUtf8;
out |= kUpb_MessageModifier_DefaultIsPacked;
}
if (upb_MessageDef_ExtensionRangeCount(m)) {
out |= kUpb_MessageModifier_IsExtendable;
}
return out;
}
/******************************************************************************/
bool upb_MiniDescriptor_EncodeEnum(const upb_EnumDef* e, upb_Arena* a,
upb_StringView* out) {
DescState s;
@ -174,7 +135,7 @@ bool upb_MiniDescriptor_EncodeField(const upb_FieldDef* f, upb_Arena* a,
const upb_FieldType type = upb_FieldDef_Type(f);
const int number = upb_FieldDef_Number(f);
const uint64_t modifiers = upb_Field_Modifiers(f);
const uint64_t modifiers = _upb_FieldDef_Modifiers(f);
if (!upb_DescState_Grow(&s, a)) return false;
s.ptr = upb_MtDataEncoder_PutField(&s.e, s.ptr, type, number, modifiers);
@ -203,14 +164,15 @@ bool upb_MiniDescriptor_EncodeMessage(const upb_MessageDef* m, upb_Arena* a,
}
if (!upb_DescState_Grow(&s, a)) return false;
s.ptr = upb_MtDataEncoder_StartMessage(&s.e, s.ptr, upb_Message_Modifiers(m));
s.ptr =
upb_MtDataEncoder_StartMessage(&s.e, s.ptr, _upb_MessageDef_Modifiers(m));
const int field_count = upb_MessageDef_FieldCount(m);
for (int i = 0; i < field_count; i++) {
const upb_FieldDef* f = sorted ? sorted[i] : upb_MessageDef_Field(m, i);
const upb_FieldType type = upb_FieldDef_Type(f);
const int number = upb_FieldDef_Number(f);
const uint64_t modifiers = upb_Field_Modifiers(f);
const uint64_t modifiers = _upb_FieldDef_Modifiers(f);
if (!upb_DescState_Grow(&s, a)) return false;
s.ptr = upb_MtDataEncoder_PutField(&s.e, s.ptr, type, number, modifiers);

Loading…
Cancel
Save