From 0c6531378de18678455f46aec950aa4d46085049 Mon Sep 17 00:00:00 2001 From: Protobuf Team Bot Date: Thu, 11 Aug 2022 11:06:59 -0700 Subject: [PATCH] Merge GetEnum into GetInt32. Rename SetEnum to SetEnumProto2 to be clear that upb only treats Proto2 enum as enum. Proto3 enums should use SetInt32. PiperOrigin-RevId: 467000685 --- upb/mini_table.h | 5 +++++ upb/mini_table_accessors.h | 19 +++++++++---------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/upb/mini_table.h b/upb/mini_table.h index 9e5eab27a6..238c6d7222 100644 --- a/upb/mini_table.h +++ b/upb/mini_table.h @@ -45,6 +45,11 @@ UPB_INLINE const upb_MiniTable* upb_MiniTable_GetSubMessageTable( return mini_table->subs[field->submsg_index].submsg; } +UPB_INLINE const upb_MiniTable_Enum* upb_MiniTable_GetSubEnumTable( + const upb_MiniTable* mini_table, const upb_MiniTable_Field* field) { + return mini_table->subs[field->submsg_index].subenum; +} + // Validates enum value against range defined by enum mini table. UPB_INLINE bool upb_MiniTable_Enum_CheckValue(const upb_MiniTable_Enum* e, int32_t val) { diff --git a/upb/mini_table_accessors.h b/upb/mini_table_accessors.h index ccc3dc5360..a0abdc83b9 100644 --- a/upb/mini_table_accessors.h +++ b/upb/mini_table_accessors.h @@ -30,6 +30,7 @@ #include "upb/array.h" #include "upb/internal/mini_table_accessors.h" +#include "upb/mini_table.h" #include "upb/msg_internal.h" // Must be last. @@ -63,7 +64,8 @@ UPB_INLINE int32_t upb_MiniTable_GetInt32(const upb_Message* msg, const upb_MiniTable_Field* field) { UPB_ASSERT(field->descriptortype == kUpb_FieldType_Int32 || field->descriptortype == kUpb_FieldType_SInt32 || - field->descriptortype == kUpb_FieldType_SFixed32); + field->descriptortype == kUpb_FieldType_SFixed32 || + field->descriptortype == kUpb_FieldType_Enum); return *UPB_PTR_AT(msg, field->offset, int32_t); } @@ -93,16 +95,13 @@ UPB_INLINE void upb_MiniTable_SetUInt32(upb_Message* msg, *UPB_PTR_AT(msg, field->offset, uint32_t) = value; } -UPB_INLINE int32_t upb_MiniTable_GetEnum(const upb_Message* msg, - const upb_MiniTable_Field* field) { - UPB_ASSERT(field->descriptortype == kUpb_FieldType_Enum); - return *UPB_PTR_AT(msg, field->offset, int32_t); -} - -UPB_INLINE void upb_MiniTable_SetEnum(upb_Message* msg, - const upb_MiniTable_Field* field, - int32_t value) { +UPB_INLINE void upb_MiniTable_SetEnumProto2(upb_Message* msg, + const upb_MiniTable* msg_mini_table, + const upb_MiniTable_Field* field, + int32_t value) { UPB_ASSERT(field->descriptortype == kUpb_FieldType_Enum); + UPB_ASSERT(upb_MiniTable_Enum_CheckValue( + upb_MiniTable_GetSubEnumTable(msg_mini_table, field), value)); _upb_MiniTable_SetPresence(msg, field); *UPB_PTR_AT(msg, field->offset, int32_t) = value; }