Commit f7961072 authored by Jason Choy's avatar Jason Choy

Added support for structs to the name annotation and fixed discriminant check names

parent fb952aa6
...@@ -25,4 +25,4 @@ ...@@ -25,4 +25,4 @@
$namespace("capnp::annotations"); $namespace("capnp::annotations");
annotation namespace(file): Text; annotation namespace(file): Text;
annotation name(field, enumerant): Text; annotation name(field, enumerant, struct, enum): Text;
...@@ -5,51 +5,54 @@ ...@@ -5,51 +5,54 @@
namespace capnp { namespace capnp {
namespace schemas { namespace schemas {
static const ::capnp::_::AlignedData<18> b_b9c6f99ebf805f2c = { static const ::capnp::_::AlignedData<19> b_b9c6f99ebf805f2c = {
{ 0, 0, 0, 0, 5, 0, 5, 0, { 0, 0, 0, 0, 5, 0, 5, 0,
44, 95, 128, 191, 158, 249, 198, 185, 44, 95, 128, 191, 158, 249, 198, 185,
10, 0, 0, 0, 5, 0, 1, 0, 20, 0, 0, 0, 5, 0, 1, 0,
129, 78, 48, 184, 123, 125, 248, 189, 129, 78, 48, 184, 123, 125, 248, 189,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17, 0, 0, 0, 162, 0, 0, 0, 17, 0, 0, 0, 242, 0, 0, 0,
25, 0, 0, 0, 7, 0, 0, 0, 29, 0, 0, 0, 7, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
20, 0, 0, 0, 2, 0, 1, 0, 24, 0, 0, 0, 2, 0, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
99, 43, 43, 46, 99, 97, 112, 110, 115, 114, 99, 47, 99, 97, 112, 110,
112, 58, 110, 97, 109, 101, 115, 112, 112, 47, 99, 43, 43, 46, 99, 97,
97, 99, 101, 0, 0, 0, 0, 0, 112, 110, 112, 58, 110, 97, 109, 101,
115, 112, 97, 99, 101, 0, 0, 0,
0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0,
12, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, } 0, 0, 0, 0, 0, 0, 0, 0, }
}; };
const ::capnp::_::RawSchema s_b9c6f99ebf805f2c = { const ::capnp::_::RawSchema s_b9c6f99ebf805f2c = {
0xb9c6f99ebf805f2c, b_b9c6f99ebf805f2c.words, 18, nullptr, nullptr, 0xb9c6f99ebf805f2c, b_b9c6f99ebf805f2c.words, 19, nullptr, nullptr,
0, 0, nullptr, nullptr, nullptr 0, 0, nullptr, nullptr, nullptr
}; };
static const ::capnp::_::AlignedData<17> b_f264a779fef191ce = { static const ::capnp::_::AlignedData<19> b_f264a779fef191ce = {
{ 0, 0, 0, 0, 5, 0, 5, 0, { 0, 0, 0, 0, 5, 0, 5, 0,
206, 145, 241, 254, 121, 167, 100, 242, 206, 145, 241, 254, 121, 167, 100, 242,
10, 0, 0, 0, 5, 0, 40, 0, 20, 0, 0, 0, 5, 0, 126, 0,
129, 78, 48, 184, 123, 125, 248, 189, 129, 78, 48, 184, 123, 125, 248, 189,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17, 0, 0, 0, 122, 0, 0, 0, 17, 0, 0, 0, 202, 0, 0, 0,
21, 0, 0, 0, 7, 0, 0, 0, 29, 0, 0, 0, 7, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
16, 0, 0, 0, 2, 0, 1, 0, 24, 0, 0, 0, 2, 0, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0,
115, 114, 99, 47, 99, 97, 112, 110,
112, 47, 99, 43, 43, 46, 99, 97,
112, 110, 112, 58, 110, 97, 109, 101,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
99, 43, 43, 46, 99, 97, 112, 110,
112, 58, 110, 97, 109, 101, 0, 0,
0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0,
12, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, } 0, 0, 0, 0, 0, 0, 0, 0, }
}; };
const ::capnp::_::RawSchema s_f264a779fef191ce = { const ::capnp::_::RawSchema s_f264a779fef191ce = {
0xf264a779fef191ce, b_f264a779fef191ce.words, 17, nullptr, nullptr, 0xf264a779fef191ce, b_f264a779fef191ce.words, 19, nullptr, nullptr,
0, 0, nullptr, nullptr, nullptr 0, 0, nullptr, nullptr, nullptr
}; };
} // namespace schemas } // namespace schemas
......
...@@ -191,14 +191,15 @@ private: ...@@ -191,14 +191,15 @@ private:
auto node = schema.getProto(); auto node = schema.getProto();
if (node.getScopeId() == 0) { if (node.getScopeId() == 0) {
usedImports.insert(node.getId()); usedImports.insert(node.getId());
for (auto annotation: node.getAnnotations()) { KJ_IF_MAYBE(ns, annotationValue(node, NAMESPACE_ANNOTATION_ID)) {
if (annotation.getId() == NAMESPACE_ANNOTATION_ID) { return kj::strTree(" ::", ns->getText());
return kj::strTree(" ::", annotation.getValue().getText());
}
} }
return kj::strTree(" "); return kj::strTree(" ");
} else { } else {
Schema parent = schemaLoader.get(node.getScopeId()); Schema parent = schemaLoader.get(node.getScopeId());
KJ_IF_MAYBE(annotatedName, annotationValue(node, NAME_ANNOTATION_ID)) {
return kj::strTree(cppFullName(parent), "::", annotatedName->getText());
}
for (auto nested: parent.getProto().getNestedNodes()) { for (auto nested: parent.getProto().getNestedNodes()) {
if (nested.getId() == node.getId()) { if (nested.getId() == node.getId()) {
return kj::strTree(cppFullName(parent), "::", nested.getName()); return kj::strTree(cppFullName(parent), "::", nested.getName());
...@@ -271,13 +272,23 @@ private: ...@@ -271,13 +272,23 @@ private:
KJ_UNREACHABLE; KJ_UNREACHABLE;
} }
kj::StringPtr enumerantName(schema::Enumerant::Reader proto) { template <typename P>
kj::Maybe<schema::Value::Reader> annotationValue(P proto, uint64_t annotationId) {
for (auto annotation: proto.getAnnotations()) { for (auto annotation: proto.getAnnotations()) {
if (annotation.getId() == NAME_ANNOTATION_ID) { if (annotation.getId() == annotationId) {
return annotation.getValue().getText(); return annotation.getValue();
} }
} }
return proto.getName(); return kj::Maybe<schema::Value::Reader>();
}
template <typename P>
kj::StringPtr protoName(P proto) {
KJ_IF_MAYBE(name, annotationValue(proto, NAME_ANNOTATION_ID)) {
return name->getText();
} else {
return proto.getName();
}
} }
kj::StringTree literalValue(schema::Type::Reader type, schema::Value::Reader value) { kj::StringTree literalValue(schema::Type::Reader type, schema::Value::Reader value) {
...@@ -299,7 +310,7 @@ private: ...@@ -299,7 +310,7 @@ private:
if (value.getEnum() < schema.getEnumerants().size()) { if (value.getEnum() < schema.getEnumerants().size()) {
return kj::strTree( return kj::strTree(
cppFullName(schema), "::", cppFullName(schema), "::",
toUpperCase(enumerantName(schema.getEnumerants()[value.getEnum()].getProto()))); toUpperCase(protoName(schema.getEnumerants()[value.getEnum()].getProto())));
} else { } else {
return kj::strTree("static_cast<", cppFullName(schema), ">(", value.getEnum(), ")"); return kj::strTree("static_cast<", cppFullName(schema), ">(", value.getEnum(), ")");
} }
...@@ -581,18 +592,12 @@ private: ...@@ -581,18 +592,12 @@ private:
FieldText makeFieldText(kj::StringPtr scope, StructSchema::Field field) { FieldText makeFieldText(kj::StringPtr scope, StructSchema::Field field) {
auto proto = field.getProto(); auto proto = field.getProto();
auto baseName = proto.getName(); auto baseName = protoName(proto);
for (auto annotation: proto.getAnnotations()) {
if (annotation.getId() == NAME_ANNOTATION_ID) {
baseName = annotation.getValue().getText();
break;
}
}
kj::String titleCase = toTitleCase(baseName); kj::String titleCase = toTitleCase(baseName);
DiscriminantChecks unionDiscrim; DiscriminantChecks unionDiscrim;
if (hasDiscriminantValue(proto)) { if (hasDiscriminantValue(proto)) {
unionDiscrim = makeDiscriminantChecks(scope, proto.getName(), field.getContainingStruct()); unionDiscrim = makeDiscriminantChecks(scope, baseName, field.getContainingStruct());
} }
switch (proto.which()) { switch (proto.which()) {
...@@ -1170,6 +1175,9 @@ private: ...@@ -1170,6 +1175,9 @@ private:
StructText makeStructText(kj::StringPtr scope, kj::StringPtr name, StructSchema schema, StructText makeStructText(kj::StringPtr scope, kj::StringPtr name, StructSchema schema,
kj::Array<kj::StringTree> nestedTypeDecls) { kj::Array<kj::StringTree> nestedTypeDecls) {
auto proto = schema.getProto(); auto proto = schema.getProto();
KJ_IF_MAYBE(annotatedName, annotationValue(proto, NAME_ANNOTATION_ID)) {
name = annotatedName->getText();
}
auto fullName = kj::str(scope, name); auto fullName = kj::str(scope, name);
auto subScope = kj::str(fullName, "::"); auto subScope = kj::str(fullName, "::");
auto fieldTexts = KJ_MAP(f, schema.getFields()) { return makeFieldText(subScope, f); }; auto fieldTexts = KJ_MAP(f, schema.getFields()) { return makeFieldText(subScope, f); };
...@@ -1192,7 +1200,7 @@ private: ...@@ -1192,7 +1200,7 @@ private:
" enum Which: uint16_t {\n", " enum Which: uint16_t {\n",
KJ_MAP(f, structNode.getFields()) { KJ_MAP(f, structNode.getFields()) {
if (hasDiscriminantValue(f)) { if (hasDiscriminantValue(f)) {
return kj::strTree(" ", toUpperCase(f.getName()), ",\n"); return kj::strTree(" ", toUpperCase(protoName(f)), ",\n");
} else { } else {
return kj::strTree(); return kj::strTree();
} }
...@@ -1556,6 +1564,9 @@ private: ...@@ -1556,6 +1564,9 @@ private:
NodeText makeNodeText(kj::StringPtr namespace_, kj::StringPtr scope, NodeText makeNodeText(kj::StringPtr namespace_, kj::StringPtr scope,
kj::StringPtr name, Schema schema) { kj::StringPtr name, Schema schema) {
auto proto = schema.getProto(); auto proto = schema.getProto();
KJ_IF_MAYBE(annotatedName, annotationValue(proto, NAME_ANNOTATION_ID)) {
name = annotatedName->getText();
}
auto fullName = kj::str(scope, name); auto fullName = kj::str(scope, name);
auto subScope = kj::str(fullName, "::"); auto subScope = kj::str(fullName, "::");
auto hexId = kj::hex(proto.getId()); auto hexId = kj::hex(proto.getId());
...@@ -1711,6 +1722,9 @@ private: ...@@ -1711,6 +1722,9 @@ private:
kj::StringPtr name, Schema schema, kj::StringPtr name, Schema schema,
kj::Array<kj::StringTree> nestedTypeDecls) { kj::Array<kj::StringTree> nestedTypeDecls) {
auto proto = schema.getProto(); auto proto = schema.getProto();
KJ_IF_MAYBE(annotatedName, annotationValue(proto, NAME_ANNOTATION_ID)) {
name = annotatedName->getText();
}
auto fullName = kj::str(scope, name); auto fullName = kj::str(scope, name);
auto hexId = kj::hex(proto.getId()); auto hexId = kj::hex(proto.getId());
...@@ -1751,7 +1765,7 @@ private: ...@@ -1751,7 +1765,7 @@ private:
scope.size() == 0 ? kj::strTree() : kj::strTree( scope.size() == 0 ? kj::strTree() : kj::strTree(
" enum class ", name, ": uint16_t {\n", " enum class ", name, ": uint16_t {\n",
KJ_MAP(e, enumerants) { KJ_MAP(e, enumerants) {
return kj::strTree(" ", toUpperCase(enumerantName(e.getProto())), ",\n"); return kj::strTree(" ", toUpperCase(protoName(e.getProto())), ",\n");
}, },
" };\n" " };\n"
"\n"), "\n"),
...@@ -1759,7 +1773,7 @@ private: ...@@ -1759,7 +1773,7 @@ private:
scope.size() > 0 ? kj::strTree() : kj::strTree( scope.size() > 0 ? kj::strTree() : kj::strTree(
"enum class ", name, ": uint16_t {\n", "enum class ", name, ": uint16_t {\n",
KJ_MAP(e, enumerants) { KJ_MAP(e, enumerants) {
return kj::strTree(" ", toUpperCase(enumerantName(e.getProto())), ",\n"); return kj::strTree(" ", toUpperCase(protoName(e.getProto())), ",\n");
}, },
"};\n" "};\n"
"\n"), "\n"),
......
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