From 6bcdaa13528bd9b924705ae9835dd8f0ca0337d5 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Thu, 13 Dec 2018 07:12:58 -0800 Subject: Changed generated array accessors to be more convenient. --- upbc/generator.cc | 65 +++++++++++++++++++++++++++++++++----------------- upbc/message_layout.cc | 16 ++++++++----- upbc/message_layout.h | 5 +++- 3 files changed, 57 insertions(+), 29 deletions(-) (limited to 'upbc') diff --git a/upbc/generator.cc b/upbc/generator.cc index 0f7d14d..55c90d3 100644 --- a/upbc/generator.cc +++ b/upbc/generator.cc @@ -180,10 +180,6 @@ std::string GetSizeInit(const MessageLayout::Size& size) { std::string CTypeInternal(const protobuf::FieldDescriptor* field, bool is_const) { std::string maybe_const = is_const ? "const " : ""; - if (field->label() == protobuf::FieldDescriptor::LABEL_REPEATED) { - return maybe_const + "upb_array*"; - } - switch (field->cpp_type()) { case protobuf::FieldDescriptor::CPPTYPE_MESSAGE: { std::string maybe_struct = @@ -326,32 +322,57 @@ void GenerateMessageInHeader(const protobuf::Descriptor* message, Output& output } for (auto field : FieldNumberOrder(message)) { - output("UPB_INLINE $0 $1_$2(const $1 *msg) {", CTypeConst(field), msgname, - field->name()); - if (field->containing_oneof()) { - output(" return UPB_READ_ONEOF(msg, $0, $1, $2, $3, $4); }\n", - CTypeConst(field), GetSizeInit(layout.GetFieldOffset(field)), - GetSizeInit(layout.GetOneofCaseOffset(field->containing_oneof())), - field->number(), FieldDefault(field)); + if (field->is_repeated()) { + output( + "UPB_INLINE $0 const* $1_$2(const $1 *msg, size_t *len) { " + "return _upb_array_accessor(UPB_FIELD_AT(msg, const upb_array*, $3), " + "len); }\n", + CTypeConst(field), msgname, field->name(), + GetSizeInit(layout.GetFieldOffset(field))); } else { - output(" return UPB_FIELD_AT(msg, $0, $1); }\n", CTypeConst(field), - GetSizeInit(layout.GetFieldOffset(field))); + output("UPB_INLINE $0 $1_$2(const $1 *msg) {", CTypeConst(field), msgname, + field->name()); + if (field->containing_oneof()) { + output(" return UPB_READ_ONEOF(msg, $0, $1, $2, $3, $4); }\n", + CTypeConst(field), GetSizeInit(layout.GetFieldOffset(field)), + GetSizeInit(layout.GetOneofCaseOffset(field->containing_oneof())), + field->number(), FieldDefault(field)); + } else { + output(" return UPB_FIELD_AT(msg, $0, $1); }\n", CTypeConst(field), + GetSizeInit(layout.GetFieldOffset(field))); + } } } output("\n"); for (auto field : FieldNumberOrder(message)) { - output("UPB_INLINE void $0_set_$1($0 *msg, $2 value) { ", msgname, - field->name(), CType(field)); - if (field->containing_oneof()) { - output("UPB_WRITE_ONEOF(msg, $0, $1, value, $2, $3); }\n", CType(field), - GetSizeInit(layout.GetFieldOffset(field)), - GetSizeInit(layout.GetOneofCaseOffset(field->containing_oneof())), - field->number()); + if (field->is_repeated()) { + output( + "UPB_INLINE $0* $1_$2_mutable($1 *msg, size_t *len) { " + "return _upb_array_mutable_accessor(UPB_FIELD_AT(msg, upb_array*, " + "$3), len); }\n", + CType(field), msgname, field->name(), + GetSizeInit(layout.GetFieldOffset(field))); + output( + "UPB_INLINE $0* $1_$2_resize($1 *msg, size_t len) { " + "return _upb_array_resize_accessor(UPB_FIELD_AT(msg, upb_array*, " + "$3), len, $4); }\n", + CType(field), msgname, field->name(), + GetSizeInit(layout.GetFieldOffset(field)), + GetSizeInit(MessageLayout::SizeOfUnwrapped(field).size)); } else { - output("UPB_FIELD_AT(msg, $0, $1) = value; }\n", CType(field), - GetSizeInit(layout.GetFieldOffset(field))); + output("UPB_INLINE void $0_set_$1($0 *msg, $2 value) { ", msgname, + field->name(), CType(field)); + if (field->containing_oneof()) { + output("UPB_WRITE_ONEOF(msg, $0, $1, value, $2, $3); }\n", CType(field), + GetSizeInit(layout.GetFieldOffset(field)), + GetSizeInit(layout.GetOneofCaseOffset(field->containing_oneof())), + field->number()); + } else { + output("UPB_FIELD_AT(msg, $0, $1) = value; }\n", CType(field), + GetSizeInit(layout.GetFieldOffset(field))); + } } } diff --git a/upbc/message_layout.cc b/upbc/message_layout.cc index b6614f0..5956424 100644 --- a/upbc/message_layout.cc +++ b/upbc/message_layout.cc @@ -30,16 +30,20 @@ bool MessageLayout::HasHasbit(const protobuf::FieldDescriptor* field) { MessageLayout::SizeAndAlign MessageLayout::SizeOf( const protobuf::FieldDescriptor* field) { - if (field->label() == protobuf::FieldDescriptor::LABEL_REPEATED || - field->cpp_type() == protobuf::FieldDescriptor::CPPTYPE_MESSAGE) { - return {{4, 8}, {4, 8}}; + if (field->is_repeated()) { + return {{4, 8}, {4, 8}}; // Pointer to array object. + } else { + return SizeOfUnwrapped(field); } +} +MessageLayout::SizeAndAlign MessageLayout::SizeOfUnwrapped( + const protobuf::FieldDescriptor* field) { switch (field->cpp_type()) { + case protobuf::FieldDescriptor::CPPTYPE_MESSAGE: + return {{4, 8}, {4, 8}}; // Pointer to message. case protobuf::FieldDescriptor::CPPTYPE_STRING: - // upb_stringview - // return {{8, 16}, {4, 8}}; - return {{8, 16}, {8, 16}}; + return {{8, 16}, {4, 8}}; // upb_stringview case protobuf::FieldDescriptor::CPPTYPE_BOOL: return {{1, 1}, {1, 1}}; case protobuf::FieldDescriptor::CPPTYPE_FLOAT: diff --git a/upbc/message_layout.h b/upbc/message_layout.h index bdcc336..a4cb289 100644 --- a/upbc/message_layout.h +++ b/upbc/message_layout.h @@ -60,6 +60,8 @@ class MessageLayout { Size message_size() const { return size_; } static bool HasHasbit(const google::protobuf::FieldDescriptor* field); + static SizeAndAlign SizeOfUnwrapped( + const google::protobuf::FieldDescriptor* field); private: void ComputeLayout(const google::protobuf::Descriptor* descriptor); @@ -87,7 +89,8 @@ class MessageLayout { } static SizeAndAlign SizeOf(const google::protobuf::FieldDescriptor* field); - static int64_t FieldLayoutRank(const google::protobuf::FieldDescriptor* field); + static int64_t FieldLayoutRank( + const google::protobuf::FieldDescriptor* field); std::unordered_map field_offsets_; -- cgit v1.2.3