summaryrefslogtreecommitdiff
path: root/upbc
diff options
context:
space:
mode:
authorJoshua Haberman <jhaberman@gmail.com>2018-12-13 07:12:58 -0800
committerJoshua Haberman <jhaberman@gmail.com>2018-12-13 07:12:58 -0800
commit6bcdaa13528bd9b924705ae9835dd8f0ca0337d5 (patch)
treebe91afb3af543e73d8b5d5fd873c23d77d138d00 /upbc
parent2c0e0054a85651ff20067ec504844a4611097041 (diff)
Changed generated array accessors to be more convenient.
Diffstat (limited to 'upbc')
-rw-r--r--upbc/generator.cc65
-rw-r--r--upbc/message_layout.cc16
-rw-r--r--upbc/message_layout.h5
3 files changed, 57 insertions, 29 deletions
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<const google::protobuf::FieldDescriptor*, Size>
field_offsets_;
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback