Commit 608d7ed7 authored by Kenton Varda's avatar Kenton Varda

Support parsing values for structs with unnamed unions.

parent 372612cd
......@@ -101,16 +101,19 @@ struct ValueExpression {
name @6 :DeclName;
list @7 :List(ValueExpression);
structValue @8 :List(FieldAssignment);
unionValue @9 :FieldAssignment;
}
struct FieldAssignment {
fieldName @0 :LocatedText;
value @1 :ValueExpression;
union {
notUnion @1 :Void;
union @2 :LocatedText; # Name of union member being assigned.
}
value @3 :ValueExpression;
}
startByte @10 :UInt32;
endByte @11 :UInt32;
startByte @9 :UInt32;
endByte @10 :UInt32;
}
struct Declaration {
......
......@@ -456,7 +456,7 @@ const ::capnp::_::RawSchema s_8751968764a2e298 = {
0x8751968764a2e298, b_8751968764a2e298.words, 78, d_8751968764a2e298, m_8751968764a2e298,
2, 4, nullptr, nullptr
};
static const ::capnp::_::AlignedData<195> b_9ca8b2acb16fc545 = {
static const ::capnp::_::AlignedData<180> b_9ca8b2acb16fc545 = {
{ 0, 0, 0, 0, 3, 0, 4, 0,
69, 197, 111, 177, 172, 178, 168, 156,
198, 195, 187, 220, 104, 225, 107, 197,
......@@ -483,54 +483,50 @@ static const ::capnp::_::AlignedData<195> b_9ca8b2acb16fc545 = {
41, 0, 0, 0, 42, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
36, 0, 0, 0, 1, 0, 1, 0,
10, 0, 1, 0, 0, 0, 0, 0,
73, 2, 0, 0, 82, 0, 0, 0,
9, 0, 1, 0, 0, 0, 0, 0,
13, 2, 0, 0, 82, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
72, 2, 0, 0, 1, 0, 2, 0,
11, 0, 2, 0, 0, 0, 0, 0,
101, 2, 0, 0, 66, 0, 0, 0,
12, 2, 0, 0, 1, 0, 2, 0,
10, 0, 2, 0, 0, 0, 0, 0,
41, 2, 0, 0, 66, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
96, 2, 0, 0, 1, 0, 2, 0,
36, 2, 0, 0, 1, 0, 2, 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, 0, 0, 0,
33, 1, 0, 0, 42, 0, 0, 0,
17, 1, 0, 0, 42, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
28, 1, 0, 0, 1, 0, 2, 0,
12, 1, 0, 0, 1, 0, 2, 0,
8, 0, 7, 0, 0, 0, 0, 0,
69, 1, 0, 0, 98, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
68, 1, 0, 0, 1, 0, 2, 0,
9, 0, 8, 0, 0, 0, 0, 0,
109, 1, 0, 0, 90, 0, 0, 0,
53, 1, 0, 0, 98, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
108, 1, 0, 0, 1, 0, 2, 0,
52, 1, 0, 0, 1, 0, 2, 0,
117, 110, 107, 110, 111, 119, 110, 0,
0, 0, 0, 0, 0, 0, 0, 0,
4, 0, 0, 0, 2, 0, 1, 0,
......@@ -620,17 +616,6 @@ static const ::capnp::_::AlignedData<195> b_9ca8b2acb16fc545 = {
14, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
117, 110, 105, 111, 110, 86, 97, 108,
117, 101, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
4, 0, 0, 0, 2, 0, 1, 0,
12, 0, 0, 0, 2, 0, 1, 0,
16, 0, 0, 0, 0, 0, 0, 0,
14, 186, 127, 178, 248, 124, 181, 182,
0, 0, 0, 0, 0, 0, 0, 0,
16, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
115, 116, 97, 114, 116, 66, 121, 116,
101, 0, 0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 0, 0, 0,
......@@ -669,14 +654,13 @@ static const ::capnp::_::RawSchema::MemberInfo m_9ca8b2acb16fc545[] = {
{ 1, 1 },
{ 1, 4 },
{ 1, 7 },
{ 1, 8 },
{ 1, 0 },
};
const ::capnp::_::RawSchema s_9ca8b2acb16fc545 = {
0x9ca8b2acb16fc545, b_9ca8b2acb16fc545.words, 195, d_9ca8b2acb16fc545, m_9ca8b2acb16fc545,
3, 12, nullptr, nullptr
0x9ca8b2acb16fc545, b_9ca8b2acb16fc545.words, 180, d_9ca8b2acb16fc545, m_9ca8b2acb16fc545,
3, 11, nullptr, nullptr
};
static const ::capnp::_::AlignedData<49> b_b6b57cf8b27fba0e = {
static const ::capnp::_::AlignedData<86> b_b6b57cf8b27fba0e = {
{ 0, 0, 0, 0, 3, 0, 4, 0,
14, 186, 127, 178, 248, 124, 181, 182,
69, 197, 111, 177, 172, 178, 168, 156,
......@@ -694,20 +678,57 @@ static const ::capnp::_::AlignedData<49> b_b6b57cf8b27fba0e = {
108, 100, 65, 115, 115, 105, 103, 110,
109, 101, 110, 116, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 1, 0,
0, 0, 2, 0, 7, 0, 0, 0,
1, 0, 3, 0, 7, 0, 0, 0,
1, 0, 0, 0, 103, 0, 0, 0,
12, 0, 0, 0, 1, 0, 3, 0,
0, 0, 0, 0, 0, 0, 0, 0,
41, 0, 0, 0, 82, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
40, 0, 0, 0, 1, 0, 2, 0,
1, 0, 1, 0, 1, 0, 0, 0,
69, 0, 0, 0, 10, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
64, 0, 0, 0, 1, 0, 1, 0,
3, 0, 2, 0, 0, 0, 0, 0,
185, 0, 0, 0, 50, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
180, 0, 0, 0, 1, 0, 2, 0,
102, 105, 101, 108, 100, 78, 97, 109,
101, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
4, 0, 0, 0, 2, 0, 1, 0,
12, 0, 0, 0, 2, 0, 1, 0,
16, 0, 0, 0, 0, 0, 0, 0,
100, 212, 41, 101, 181, 22, 88, 231,
0, 0, 0, 0, 0,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, 0, 0, 0, 0, 0, 0, 0,
4, 0, 0, 0, 2, 0, 1, 0,
12, 0, 0, 0, 2, 0, 1, 0,
16, 0, 0, 0, 0, 0, 0, 0,
......@@ -732,12 +753,15 @@ static const ::capnp::_::RawSchema* const d_b6b57cf8b27fba0e[] = {
&s_e75816b56529d464,
};
static const ::capnp::_::RawSchema::MemberInfo m_b6b57cf8b27fba0e[] = {
{ 0, 0 },
{ 0, 1 },
{ 0, 0 },
{ 0, 3 },
{ 0, 4 },
{ 0, 2 },
};
const ::capnp::_::RawSchema s_b6b57cf8b27fba0e = {
0xb6b57cf8b27fba0e, b_b6b57cf8b27fba0e.words, 49, d_b6b57cf8b27fba0e, m_b6b57cf8b27fba0e,
2, 2, nullptr, nullptr
0xb6b57cf8b27fba0e, b_b6b57cf8b27fba0e.words, 86, d_b6b57cf8b27fba0e, m_b6b57cf8b27fba0e,
2, 5, nullptr, nullptr
};
static const ::capnp::_::AlignedData<664> b_96efe787c17e83bb = {
{ 0, 0, 0, 0, 3, 0, 4, 0,
......
This diff is collapsed.
......@@ -1647,27 +1647,56 @@ void NodeTranslator::compileValueInner(
auto dstSchema = dstStruct.getSchema();
for (auto assignment: srcStruct) {
auto fieldName = assignment.getFieldName();
switch (assignment.which()) {
case ValueExpression::FieldAssignment::NOT_UNION:
KJ_IF_MAYBE(member, dstSchema.findMemberByName(fieldName.getValue())) {
DynamicSlot slot(dstStruct, *member);
compileValue(assignment.getValue(), slot, isBootstrap);
} else {
errorReporter.addErrorOn(fieldName, kj::str(
"Value has no field named '", fieldName.getValue(), "'."));
"Struct has no field named '", fieldName.getValue(), "'."));
}
break;
case ValueExpression::FieldAssignment::UNION: {
StructSchema::Union unionMember;
if (fieldName.getValue().size() == 0) {
// Unnamed.
KJ_IF_MAYBE(unnamedUnion, dstSchema.getUnnamedUnion()) {
unionMember = *unnamedUnion;
} else {
errorReporter.addErrorOn(assignment.getUnion(), kj::str(
"Struct type '", dstSchema.getProto().getDisplayName(),
"' does not contain an unnamed union."));
break;
}
} else {
KJ_IF_MAYBE(member, dstSchema.findMemberByName(fieldName.getValue())) {
if (member->getProto().getBody().which() ==
schema::StructNode::Member::Body::UNION_MEMBER) {
unionMember = member->asUnion();
} else {
errorReporter.addErrorOn(fieldName, kj::str(
"'", fieldName.getValue(), "' is not a union."));
break;
}
case ValueExpression::Body::UNION_VALUE: {
auto srcUnion = src.getBody().getUnionValue();
auto dstUnion = dst.getUnion();
auto fieldName = srcUnion.getFieldName();
KJ_IF_MAYBE(member, dstUnion.getSchema().findMemberByName(fieldName.getValue())) {
DynamicSlot slot(dstUnion, *member);
compileValue(srcUnion.getValue(), slot, isBootstrap);
} else {
errorReporter.addErrorOn(fieldName, kj::str(
"Union has no field named '", fieldName.getValue(), "'."));
"Struct has no field named '", fieldName.getValue(), "'."));
break;
}
}
KJ_IF_MAYBE(member, unionMember.findMemberByName(assignment.getUnion().getValue())) {
DynamicSlot slot(dstStruct, *member);
compileValue(assignment.getValue(), slot, isBootstrap);
} else {
errorReporter.addErrorOn(assignment.getUnion(), kj::str(
"Union has no member named '", assignment.getUnion().getValue(), "'."));
}
}
}
}
break;
}
......
......@@ -372,19 +372,40 @@ CapnpParser::CapnpParser(Orphanage orphanageParam, const ErrorReporter& errorRep
return result;
}));
auto& fieldAssignment = arena.copy(p::transform(
p::sequence(p::optional(p::sequence(identifier, op("="))), parsers.valueExpression),
[this](kj::Maybe<Located<Text::Reader>>&& fieldName, Orphan<ValueExpression>&& value)
// Matches "value" or "unionName(value)".
auto& fieldValue = arena.copy(p::oneOf(
p::transform(p::sequence(identifier, parsers.parenthesizedValueExpression),
[this](Located<Text::Reader>&& name, Orphan<ValueExpression>&& value)
-> Orphan<ValueExpression::FieldAssignment> {
auto result = orphanage.newOrphan<ValueExpression::FieldAssignment>();
auto builder = result.get();
name.copyTo(builder.initUnion());
builder.adoptValue(kj::mv(value));
return result;
}),
p::transform(parsers.valueExpression,
[this](Orphan<ValueExpression>&& value)
-> Orphan<ValueExpression::FieldAssignment> {
auto result = orphanage.newOrphan<ValueExpression::FieldAssignment>();
auto builder = result.get();
// The field name is optional for now because this makes it easier for us to parse unions
// later. We'll produce an error later if the name is missing when required.
builder.setNotUnion();
builder.adoptValue(kj::mv(value));
return result;
})));
// Parser for a "name = value" pair. Also matches "name = unionMember(value)",
// "unionMember(value)" (unnamed union), and just "value" (which is not actually a valid field
// assigment, but simplifies the parser for parenthesizedValueExpression).
auto& fieldAssignment = arena.copy(p::transform(
p::sequence(p::optional(p::sequence(identifier, op("="))), fieldValue),
[this](kj::Maybe<Located<Text::Reader>>&& fieldName,
Orphan<ValueExpression::FieldAssignment>&& assignment)
-> Orphan<ValueExpression::FieldAssignment> {
auto builder = assignment.get();
KJ_IF_MAYBE(fn, fieldName) {
fn->copyTo(builder.initFieldName());
}
builder.adoptValue(kj::mv(value));
return result;
return kj::mv(assignment);
}));
parsers.parenthesizedValueExpression = arena.copy(p::transform(
......@@ -393,8 +414,10 @@ CapnpParser::CapnpParser(Orphanage orphanageParam, const ErrorReporter& errorRep
-> Orphan<ValueExpression> {
if (value.value.size() == 1) {
KJ_IF_MAYBE(firstVal, value.value[0]) {
if (!firstVal->get().hasFieldName()) {
// There is only one value and it isn't an assignment, therefore the value is
auto reader = firstVal->getReader();
if (reader.getFieldName().getValue().size() == 0 &&
reader.which() == ValueExpression::FieldAssignment::NOT_UNION) {
// There is only one value and it isn't an assignment or union, therefore the value is
// not a struct.
return firstVal->get().disownValue();
}
......@@ -418,12 +441,12 @@ CapnpParser::CapnpParser(Orphanage orphanageParam, const ErrorReporter& errorRep
auto structBuilder = builder.getBody().initStructValue(value.value.size());
for (uint i = 0; i < value.value.size(); i++) {
KJ_IF_MAYBE(field, value.value[i]) {
if (field->get().hasFieldName()) {
auto reader = field->getReader();
if (reader.getFieldName().getValue().size() > 0 ||
reader.which() == ValueExpression::FieldAssignment::UNION) {
structBuilder.adoptWithCaveats(i, kj::mv(*field));
} else {
auto fieldValue = field->get().getValue();
errorReporter.addError(fieldValue.getStartByte(), fieldValue.getEndByte(),
"Missing field name.");
errorReporter.addErrorOn(reader.getValue(), "Missing field name.");
}
}
}
......@@ -479,21 +502,6 @@ CapnpParser::CapnpParser(Orphanage orphanageParam, const ErrorReporter& errorRep
auto builder = result.get();
builder.getBody().setString(value.value);
value.copyLocationTo(builder);
return result;
}),
p::transform(p::sequence(identifier, parsers.parenthesizedValueExpression),
[this](Located<Text::Reader>&& fieldName, Orphan<ValueExpression>&& value)
-> Orphan<ValueExpression> {
auto result = orphanage.newOrphan<ValueExpression>();
auto builder = result.get();
builder.setStartByte(fieldName.startByte);
builder.setEndByte(value.get().getEndByte());
auto unionBuilder = builder.getBody().initUnionValue();
fieldName.copyTo(unionBuilder.initFieldName());
unionBuilder.adoptValue(kj::mv(value));
return result;
}),
p::transformWithLocation(parsers.declName,
......@@ -527,7 +535,9 @@ CapnpParser::CapnpParser(Orphanage orphanageParam, const ErrorReporter& errorRep
auto structBuilder = builder.getBody().initStructValue(value.value.size());
for (uint i = 0; i < value.value.size(); i++) {
KJ_IF_MAYBE(field, value.value[i]) {
if (field->get().hasFieldName()) {
auto reader = field->get();
if (reader.getFieldName().getValue().size() > 0 ||
reader.which() == ValueExpression::FieldAssignment::UNION) {
structBuilder.adoptWithCaveats(i, kj::mv(*field));
} else {
auto fieldValue = field->get().getValue();
......
......@@ -367,35 +367,6 @@ TEST(Encoding, UnionLayout) {
#undef INIT_UNION
}
TEST(Encoding, UnionDefault) {
MallocMessageBuilder builder;
TestUnionDefaults::Reader reader = builder.getRoot<TestUnionDefaults>().asReader();
{
auto field = reader.getS16s8s64s8Set();
EXPECT_EQ(TestUnion::Union0::U0F0S16, field.getUnion0().which());
EXPECT_EQ(TestUnion::Union1::U1F0S8 , field.getUnion1().which());
EXPECT_EQ(TestUnion::Union2::U2F0S64, field.getUnion2().which());
EXPECT_EQ(TestUnion::Union3::U3F0S8 , field.getUnion3().which());
EXPECT_EQ(321, field.getUnion0().getU0f0s16());
EXPECT_EQ(123, field.getUnion1().getU1f0s8());
EXPECT_EQ(12345678901234567ll, field.getUnion2().getU2f0s64());
EXPECT_EQ(55, field.getUnion3().getU3f0s8());
}
{
auto field = reader.getS0sps1s32Set();
EXPECT_EQ(TestUnion::Union0::U0F1S0 , field.getUnion0().which());
EXPECT_EQ(TestUnion::Union1::U1F0SP , field.getUnion1().which());
EXPECT_EQ(TestUnion::Union2::U2F0S1 , field.getUnion2().which());
EXPECT_EQ(TestUnion::Union3::U3F0S32, field.getUnion3().which());
EXPECT_EQ(Void::VOID, field.getUnion0().getU0f1s0());
EXPECT_EQ("foo", field.getUnion1().getU1f0sp());
EXPECT_EQ(true, field.getUnion2().getU2f0s1());
EXPECT_EQ(12345678, field.getUnion3().getU3f0s32());
}
}
TEST(Encoding, UnnamedUnion) {
MallocMessageBuilder builder;
auto root = builder.initRoot<test::TestUnnamedUnion>();
......@@ -419,10 +390,10 @@ TEST(Encoding, UnnamedUnion) {
StructSchema schema = Schema::from<test::TestUnnamedUnion>();
KJ_IF_MAYBE(u, schema.getUnnamedUnion()) {
// The discriminant is allocated between allocating the first and second members.
EXPECT_EQ(1, u->getProto().getBody().getUnionMember().getDiscriminantOffset());
// The discriminant is allocated just before allocating "bar".
EXPECT_EQ(2, u->getProto().getBody().getUnionMember().getDiscriminantOffset());
EXPECT_EQ(0, u->getMemberByName("foo").getProto().getBody().getFieldMember().getOffset());
EXPECT_EQ(1, u->getMemberByName("bar").getProto().getBody().getFieldMember().getOffset());
EXPECT_EQ(2, u->getMemberByName("bar").getProto().getBody().getFieldMember().getOffset());
// The union receives the ordinal of its first member, since it does not explicitly declare one.
EXPECT_EQ(1, u->getProto().getOrdinal());
......@@ -431,6 +402,51 @@ TEST(Encoding, UnnamedUnion) {
}
}
TEST(Encoding, UnionDefault) {
MallocMessageBuilder builder;
TestUnionDefaults::Reader reader = builder.getRoot<TestUnionDefaults>().asReader();
{
auto field = reader.getS16s8s64s8Set();
EXPECT_EQ(TestUnion::Union0::U0F0S16, field.getUnion0().which());
EXPECT_EQ(TestUnion::Union1::U1F0S8 , field.getUnion1().which());
EXPECT_EQ(TestUnion::Union2::U2F0S64, field.getUnion2().which());
EXPECT_EQ(TestUnion::Union3::U3F0S8 , field.getUnion3().which());
EXPECT_EQ(321, field.getUnion0().getU0f0s16());
EXPECT_EQ(123, field.getUnion1().getU1f0s8());
EXPECT_EQ(12345678901234567ll, field.getUnion2().getU2f0s64());
EXPECT_EQ(55, field.getUnion3().getU3f0s8());
}
{
auto field = reader.getS0sps1s32Set();
EXPECT_EQ(TestUnion::Union0::U0F1S0 , field.getUnion0().which());
EXPECT_EQ(TestUnion::Union1::U1F0SP , field.getUnion1().which());
EXPECT_EQ(TestUnion::Union2::U2F0S1 , field.getUnion2().which());
EXPECT_EQ(TestUnion::Union3::U3F0S32, field.getUnion3().which());
EXPECT_EQ(Void::VOID, field.getUnion0().getU0f1s0());
EXPECT_EQ("foo", field.getUnion1().getU1f0sp());
EXPECT_EQ(true, field.getUnion2().getU2f0s1());
EXPECT_EQ(12345678, field.getUnion3().getU3f0s32());
}
{
auto field = reader.getUnnamed1();
EXPECT_EQ(test::TestUnnamedUnion::FOO, field.which());
EXPECT_EQ(123, field.getFoo());
EXPECT_FALSE(field.hasBefore());
EXPECT_FALSE(field.hasAfter());
}
{
auto field = reader.getUnnamed2();
EXPECT_EQ(test::TestUnnamedUnion::BAR, field.which());
EXPECT_EQ(321, field.getBar());
EXPECT_EQ("foo", field.getBefore());
EXPECT_EQ("bar", field.getAfter());
}
}
// =======================================================================================
TEST(Encoding, ListDefaults) {
......
......@@ -256,6 +256,19 @@ struct TestUnion {
byte0 @49: UInt8;
}
struct TestUnnamedUnion {
before @0 :Text;
union {
foo @1 :UInt16;
bar @3 :UInt32;
}
middle @2 :UInt16;
after @4 :Text;
}
struct TestUnionDefaults {
s16s8s64s8Set @0 :TestUnion =
(union0 = u0f0s16(321), union1 = u1f0s8(123), union2 = u2f0s64(12345678901234567),
......@@ -263,6 +276,9 @@ struct TestUnionDefaults {
s0sps1s32Set @1 :TestUnion =
(union0 = u0f1s0(void), union1 = u1f0sp("foo"), union2 = u2f0s1(true),
union3 = u3f0s32(12345678));
unnamed1 @2 :TestUnnamedUnion = (foo(123));
unnamed2 @3 :TestUnnamedUnion = (bar(321), before = "foo", after = "bar");
}
struct TestNestedTypes {
......@@ -383,14 +399,3 @@ struct TestStructUnion {
object @2 :TestObject;
}
}
struct TestUnnamedUnion {
before @0 :Text;
union {
foo @1 :UInt16;
bar @2 :UInt32;
}
after @3 :Text;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment