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 @@
$namespace("capnp::annotations");
annotation namespace(file): Text;
annotation name(field, enumerant): Text;
annotation name(field, enumerant, struct, enum): Text;
......@@ -5,51 +5,54 @@
namespace capnp {
namespace schemas {
static const ::capnp::_::AlignedData<18> b_b9c6f99ebf805f2c = {
static const ::capnp::_::AlignedData<19> b_b9c6f99ebf805f2c = {
{ 0, 0, 0, 0, 5, 0, 5, 0,
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,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
17, 0, 0, 0, 162, 0, 0, 0,
25, 0, 0, 0, 7, 0, 0, 0,
17, 0, 0, 0, 242, 0, 0, 0,
29, 0, 0, 0, 7, 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,
99, 43, 43, 46, 99, 97, 112, 110,
112, 58, 110, 97, 109, 101, 115, 112,
97, 99, 101, 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,
115, 112, 97, 99, 101, 0, 0, 0,
0, 0, 0, 0, 1, 0, 1, 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, }
};
const ::capnp::_::RawSchema s_b9c6f99ebf805f2c = {
0xb9c6f99ebf805f2c, b_b9c6f99ebf805f2c.words, 18, nullptr, nullptr,
0xb9c6f99ebf805f2c, b_b9c6f99ebf805f2c.words, 19, 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,
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,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
17, 0, 0, 0, 122, 0, 0, 0,
21, 0, 0, 0, 7, 0, 0, 0,
17, 0, 0, 0, 202, 0, 0, 0,
29, 0, 0, 0, 7, 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,
99, 43, 43, 46, 99, 97, 112, 110,
112, 58, 110, 97, 109, 101, 0, 0,
0, 0, 0, 0, 1, 0, 1, 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, }
};
const ::capnp::_::RawSchema s_f264a779fef191ce = {
0xf264a779fef191ce, b_f264a779fef191ce.words, 17, nullptr, nullptr,
0xf264a779fef191ce, b_f264a779fef191ce.words, 19, nullptr, nullptr,
0, 0, nullptr, nullptr, nullptr
};
} // namespace schemas
......
......@@ -191,14 +191,15 @@ private:
auto node = schema.getProto();
if (node.getScopeId() == 0) {
usedImports.insert(node.getId());
for (auto annotation: node.getAnnotations()) {
if (annotation.getId() == NAMESPACE_ANNOTATION_ID) {
return kj::strTree(" ::", annotation.getValue().getText());
}
KJ_IF_MAYBE(ns, annotationValue(node, NAMESPACE_ANNOTATION_ID)) {
return kj::strTree(" ::", ns->getText());
}
return kj::strTree(" ");
} else {
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()) {
if (nested.getId() == node.getId()) {
return kj::strTree(cppFullName(parent), "::", nested.getName());
......@@ -271,13 +272,23 @@ private:
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()) {
if (annotation.getId() == NAME_ANNOTATION_ID) {
return annotation.getValue().getText();
if (annotation.getId() == annotationId) {
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) {
......@@ -299,7 +310,7 @@ private:
if (value.getEnum() < schema.getEnumerants().size()) {
return kj::strTree(
cppFullName(schema), "::",
toUpperCase(enumerantName(schema.getEnumerants()[value.getEnum()].getProto())));
toUpperCase(protoName(schema.getEnumerants()[value.getEnum()].getProto())));
} else {
return kj::strTree("static_cast<", cppFullName(schema), ">(", value.getEnum(), ")");
}
......@@ -581,18 +592,12 @@ private:
FieldText makeFieldText(kj::StringPtr scope, StructSchema::Field field) {
auto proto = field.getProto();
auto baseName = proto.getName();
for (auto annotation: proto.getAnnotations()) {
if (annotation.getId() == NAME_ANNOTATION_ID) {
baseName = annotation.getValue().getText();
break;
}
}
auto baseName = protoName(proto);
kj::String titleCase = toTitleCase(baseName);
DiscriminantChecks unionDiscrim;
if (hasDiscriminantValue(proto)) {
unionDiscrim = makeDiscriminantChecks(scope, proto.getName(), field.getContainingStruct());
unionDiscrim = makeDiscriminantChecks(scope, baseName, field.getContainingStruct());
}
switch (proto.which()) {
......@@ -1170,6 +1175,9 @@ private:
StructText makeStructText(kj::StringPtr scope, kj::StringPtr name, StructSchema schema,
kj::Array<kj::StringTree> nestedTypeDecls) {
auto proto = schema.getProto();
KJ_IF_MAYBE(annotatedName, annotationValue(proto, NAME_ANNOTATION_ID)) {
name = annotatedName->getText();
}
auto fullName = kj::str(scope, name);
auto subScope = kj::str(fullName, "::");
auto fieldTexts = KJ_MAP(f, schema.getFields()) { return makeFieldText(subScope, f); };
......@@ -1192,7 +1200,7 @@ private:
" enum Which: uint16_t {\n",
KJ_MAP(f, structNode.getFields()) {
if (hasDiscriminantValue(f)) {
return kj::strTree(" ", toUpperCase(f.getName()), ",\n");
return kj::strTree(" ", toUpperCase(protoName(f)), ",\n");
} else {
return kj::strTree();
}
......@@ -1556,6 +1564,9 @@ private:
NodeText makeNodeText(kj::StringPtr namespace_, kj::StringPtr scope,
kj::StringPtr name, Schema schema) {
auto proto = schema.getProto();
KJ_IF_MAYBE(annotatedName, annotationValue(proto, NAME_ANNOTATION_ID)) {
name = annotatedName->getText();
}
auto fullName = kj::str(scope, name);
auto subScope = kj::str(fullName, "::");
auto hexId = kj::hex(proto.getId());
......@@ -1711,6 +1722,9 @@ private:
kj::StringPtr name, Schema schema,
kj::Array<kj::StringTree> nestedTypeDecls) {
auto proto = schema.getProto();
KJ_IF_MAYBE(annotatedName, annotationValue(proto, NAME_ANNOTATION_ID)) {
name = annotatedName->getText();
}
auto fullName = kj::str(scope, name);
auto hexId = kj::hex(proto.getId());
......@@ -1751,7 +1765,7 @@ private:
scope.size() == 0 ? kj::strTree() : kj::strTree(
" enum class ", name, ": uint16_t {\n",
KJ_MAP(e, enumerants) {
return kj::strTree(" ", toUpperCase(enumerantName(e.getProto())), ",\n");
return kj::strTree(" ", toUpperCase(protoName(e.getProto())), ",\n");
},
" };\n"
"\n"),
......@@ -1759,7 +1773,7 @@ private:
scope.size() > 0 ? kj::strTree() : kj::strTree(
"enum class ", name, ": uint16_t {\n",
KJ_MAP(e, enumerants) {
return kj::strTree(" ", toUpperCase(enumerantName(e.getProto())), ",\n");
return kj::strTree(" ", toUpperCase(protoName(e.getProto())), ",\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