Commit 9baabb1d authored by Kenton Varda's avatar Kenton Varda

Fully implement and test 'using'.

parent 08c0a048
......@@ -287,6 +287,10 @@ TEST(Encoding, UnionDefault) {
}
}
// =======================================================================================
// Tests of generated code, not really of the encoding.
// TODO(cleanup): Move to a different test?
TEST(Encoding, NestedTypes) {
// This is more of a test of the generated code than the encoding.
......@@ -310,6 +314,13 @@ TEST(Encoding, Imports) {
checkTestMessage(root.asReader().getField());
}
TEST(Encoding, Using) {
MallocMessageBuilder builder;
TestUsing::Reader reader = builder.getRoot<TestUsing>().asReader();
EXPECT_EQ(TestNestedTypes::NestedEnum::BAR, reader.getOuterNestedEnum());
EXPECT_EQ(TestNestedTypes::NestedStruct::NestedEnum::QUUX, reader.getInnerNestedEnum());
}
} // namespace
} // namespace internal
} // namespace capnproto
......@@ -56,6 +56,7 @@ using ::capnproto::test::TestEnum;
using ::capnproto::test::TestUnion;
using ::capnproto::test::TestUnionDefaults;
using ::capnproto::test::TestNestedTypes;
using ::capnproto::test::TestUsing;
void initTestMessage(test::TestAllTypes::Builder builder);
void initTestMessage(test::TestDefaults::Builder builder);
......
......@@ -266,3 +266,11 @@ struct TestNestedTypes {
outerNestedEnum @1 :NestedEnum = bar;
innerNestedEnum @2 :NestedStruct.NestedEnum = quux;
}
struct TestUsing {
using OuterNestedEnum = TestNestedTypes.NestedEnum;
using TestNestedTypes.NestedStruct.NestedEnum;
outerNestedEnum @1 :OuterNestedEnum = bar;
innerNestedEnum @0 :NestedEnum = quux;
}
......@@ -173,9 +173,19 @@ topLine (Just statements) = liftM Left $ typeDecl statements
usingDecl = do
usingKeyword
name <- located typeIdentifier
equalsSign
maybeName <- optionMaybe $ try (do
name <- located typeIdentifier
equalsSign
return name)
target <- declName
name <- let
inferredName = case target of
AbsoluteName s -> return s
RelativeName s -> return s
ImportName _ -> fail "When 'using' an 'import' you must specify a name, e.g.: \
\using Foo = import \"foo.capnp\";"
MemberName _ s -> return s
in maybe inferredName return maybeName
return (UsingDecl name target)
constantDecl = do
......@@ -216,7 +226,7 @@ structDecl statements = do
return (StructDecl name annotations children)
structLine :: Maybe [Located Statement] -> TokenParser Declaration
structLine Nothing = constantDecl <|> fieldDecl <|> annotationDecl
structLine Nothing = usingDecl <|> constantDecl <|> fieldDecl <|> annotationDecl
structLine (Just statements) = typeDecl statements <|> unionDecl statements <|> unionDecl statements
unionDecl statements = do
......@@ -272,7 +282,7 @@ interfaceDecl statements = do
return (InterfaceDecl name annotations children)
interfaceLine :: Maybe [Located Statement] -> TokenParser Declaration
interfaceLine Nothing = constantDecl <|> methodDecl <|> annotationDecl
interfaceLine Nothing = usingDecl <|> constantDecl <|> methodDecl <|> annotationDecl
interfaceLine (Just statements) = typeDecl statements
methodDecl = do
......
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