From 0c64c4b594b5ee0a2ab87d955bfd0fe0d746da9d Mon Sep 17 00:00:00 2001 From: Josh Haberman Date: Thu, 28 Mar 2019 13:51:23 -0700 Subject: [PATCH] WIP. --- upb/legacy_msg_reflection.c | 9 --------- upb/legacy_msg_reflection.h | 3 --- upb/msg.h | 15 +++++++++++++++ upbc/generator.cc | 15 +++++++++++++-- 4 files changed, 28 insertions(+), 14 deletions(-) diff --git a/upb/legacy_msg_reflection.c b/upb/legacy_msg_reflection.c index 0683dde545..af788dd132 100644 --- a/upb/legacy_msg_reflection.c +++ b/upb/legacy_msg_reflection.c @@ -211,15 +211,6 @@ bool upb_array_set(upb_array *arr, size_t i, upb_msgval val) { /** upb_map *******************************************************************/ -struct upb_map { - upb_fieldtype_t key_type; - upb_fieldtype_t val_type; - /* We may want to optimize this to use inttable where possible, for greater - * efficiency and lower memory footprint. */ - upb_strtable strtab; - upb_arena *arena; -}; - static void upb_map_tokey(upb_fieldtype_t type, upb_msgval *key, const char **out_key, size_t *out_len) { switch (type) { diff --git a/upb/legacy_msg_reflection.h b/upb/legacy_msg_reflection.h index 32a621b4b7..7c7c2cc687 100644 --- a/upb/legacy_msg_reflection.h +++ b/upb/legacy_msg_reflection.h @@ -7,9 +7,6 @@ #include "upb/port_def.inc" -struct upb_map; -typedef struct upb_map upb_map; - struct upb_mapiter; typedef struct upb_mapiter upb_mapiter; diff --git a/upb/msg.h b/upb/msg.h index e46733f4f6..8394d6f0fd 100644 --- a/upb/msg.h +++ b/upb/msg.h @@ -11,6 +11,8 @@ #include #include + +#include "upb/table.int.h" #include "upb/upb.h" #ifdef __cplusplus @@ -55,6 +57,19 @@ typedef struct { upb_arena *arena; } upb_array; +/* Our internal representation for maps. */ +typedef struct { + /* First element must be upb_array so code like parser/serializer can treat + * map fields as array. */ + upb_array array; + + /* A side index whose values are indexes into the array. May be NULL. */ + union { + upb_strtable *strtab; + upb_inttable *inttab; + } table; +} upb_map; + upb_msg *upb_msg_new(const upb_msglayout *l, upb_arena *a); upb_msg *upb_msg_new(const upb_msglayout *l, upb_arena *a); diff --git a/upbc/generator.cc b/upbc/generator.cc index 27600693da..6e8de0a7ec 100644 --- a/upbc/generator.cc +++ b/upbc/generator.cc @@ -368,8 +368,10 @@ void GenerateMessageInHeader(const protobuf::Descriptor* message, Output& output GetSizeInit(layout.GetOneofCaseOffset(oneof))); } - for (auto field : FieldNumberOrder(message)) { + // Generate const methods. + for (auto field : FieldNumberOrder(message)) { + // Generate hazzer (if any). if (layout.HasHasbit(field)) { output( "UPB_INLINE bool $0_has_$1(const $0 *msg) { " @@ -384,6 +386,7 @@ void GenerateMessageInHeader(const protobuf::Descriptor* message, Output& output field->number()); } + // Generate getter. if (field->is_repeated()) { output( "UPB_INLINE $0 const* $1_$2(const $1 *msg, size_t *len) { " @@ -409,8 +412,15 @@ void GenerateMessageInHeader(const protobuf::Descriptor* message, Output& output output("\n"); + // Generate mutable methods. + for (auto field : FieldNumberOrder(message)) { - if (field->is_repeated()) { + if (message->options().map_entry() && field->name() == "key") { + // Emit nothing, map keys cannot be changed directly. Users must use + // the mutators of the map itself. + } else if (field->is_map()) { + // TODO(haberman): add map-based mutators. + } else if (field->is_repeated()) { output( "UPB_INLINE $0* $1_mutable_$2($1 *msg, size_t *len) {\n" " return ($0*)_upb_array_mutable_accessor(msg, $3, len);\n" @@ -453,6 +463,7 @@ void GenerateMessageInHeader(const protobuf::Descriptor* message, Output& output UpbType(field)); } } else { + // Non-repeated field. output("UPB_INLINE void $0_set_$1($0 *msg, $2 value) {\n", msgname, field->name(), CType(field)); if (field->containing_oneof()) {