Commit fd1f0a4d authored by Kenton Varda's avatar Kenton Varda

Fix capnpc-capnp.

parent fe18f08f
......@@ -35,6 +35,7 @@
#include <unistd.h>
#include <unordered_map>
#include <kj/main.h>
#include <algorithm>
#if HAVE_CONFIG_H
#include "config.h"
......@@ -155,152 +156,146 @@ private:
return kj::strTree(kj::mv(path), getUnqualifiedName(target));
}
kj::StringTree genType(schema::Type::Reader type, Schema scope) {
auto body = type.getBody();
switch (body.which()) {
case schema::Type::Body::VOID_TYPE: return kj::strTree("Void");
case schema::Type::Body::BOOL_TYPE: return kj::strTree("Bool");
case schema::Type::Body::INT8_TYPE: return kj::strTree("Int8");
case schema::Type::Body::INT16_TYPE: return kj::strTree("Int16");
case schema::Type::Body::INT32_TYPE: return kj::strTree("Int32");
case schema::Type::Body::INT64_TYPE: return kj::strTree("Int64");
case schema::Type::Body::UINT8_TYPE: return kj::strTree("UInt8");
case schema::Type::Body::UINT16_TYPE: return kj::strTree("UInt16");
case schema::Type::Body::UINT32_TYPE: return kj::strTree("UInt32");
case schema::Type::Body::UINT64_TYPE: return kj::strTree("UInt64");
case schema::Type::Body::FLOAT32_TYPE: return kj::strTree("Float32");
case schema::Type::Body::FLOAT64_TYPE: return kj::strTree("Float64");
case schema::Type::Body::TEXT_TYPE: return kj::strTree("Text");
case schema::Type::Body::DATA_TYPE: return kj::strTree("Data");
case schema::Type::Body::LIST_TYPE:
return kj::strTree("List(", genType(body.getListType(), scope), ")");
case schema::Type::Body::ENUM_TYPE:
return nodeName(scope.getDependency(body.getEnumType()), scope);
case schema::Type::Body::STRUCT_TYPE:
return nodeName(scope.getDependency(body.getStructType()), scope);
case schema::Type::Body::INTERFACE_TYPE:
return nodeName(scope.getDependency(body.getInterfaceType()), scope);
case schema::Type::Body::OBJECT_TYPE: return kj::strTree("Object");
kj::StringTree genType(schema2::Type::Reader type, Schema scope) {
switch (type.which()) {
case schema2::Type::VOID: return kj::strTree("Void");
case schema2::Type::BOOL: return kj::strTree("Bool");
case schema2::Type::INT8: return kj::strTree("Int8");
case schema2::Type::INT16: return kj::strTree("Int16");
case schema2::Type::INT32: return kj::strTree("Int32");
case schema2::Type::INT64: return kj::strTree("Int64");
case schema2::Type::UINT8: return kj::strTree("UInt8");
case schema2::Type::UINT16: return kj::strTree("UInt16");
case schema2::Type::UINT32: return kj::strTree("UInt32");
case schema2::Type::UINT64: return kj::strTree("UInt64");
case schema2::Type::FLOAT32: return kj::strTree("Float32");
case schema2::Type::FLOAT64: return kj::strTree("Float64");
case schema2::Type::TEXT: return kj::strTree("Text");
case schema2::Type::DATA: return kj::strTree("Data");
case schema2::Type::LIST:
return kj::strTree("List(", genType(type.getList(), scope), ")");
case schema2::Type::ENUM:
return nodeName(scope.getDependency(type.getEnum()), scope);
case schema2::Type::STRUCT:
return nodeName(scope.getDependency(type.getStruct()), scope);
case schema2::Type::INTERFACE:
return nodeName(scope.getDependency(type.getInterface()), scope);
case schema2::Type::OBJECT: return kj::strTree("Object");
}
return kj::strTree();
}
int typeSizeBits(schema::Type::Reader type) {
switch (type.getBody().which()) {
case schema::Type::Body::VOID_TYPE: return 0;
case schema::Type::Body::BOOL_TYPE: return 1;
case schema::Type::Body::INT8_TYPE: return 8;
case schema::Type::Body::INT16_TYPE: return 16;
case schema::Type::Body::INT32_TYPE: return 32;
case schema::Type::Body::INT64_TYPE: return 64;
case schema::Type::Body::UINT8_TYPE: return 8;
case schema::Type::Body::UINT16_TYPE: return 16;
case schema::Type::Body::UINT32_TYPE: return 32;
case schema::Type::Body::UINT64_TYPE: return 64;
case schema::Type::Body::FLOAT32_TYPE: return 32;
case schema::Type::Body::FLOAT64_TYPE: return 64;
case schema::Type::Body::TEXT_TYPE: return -1;
case schema::Type::Body::DATA_TYPE: return -1;
case schema::Type::Body::LIST_TYPE: return -1;
case schema::Type::Body::ENUM_TYPE: return 16;
case schema::Type::Body::STRUCT_TYPE: return -1;
case schema::Type::Body::INTERFACE_TYPE: return -1;
case schema::Type::Body::OBJECT_TYPE: return -1;
int typeSizeBits(schema2::Type::Reader type) {
switch (type.which()) {
case schema2::Type::VOID: return 0;
case schema2::Type::BOOL: return 1;
case schema2::Type::INT8: return 8;
case schema2::Type::INT16: return 16;
case schema2::Type::INT32: return 32;
case schema2::Type::INT64: return 64;
case schema2::Type::UINT8: return 8;
case schema2::Type::UINT16: return 16;
case schema2::Type::UINT32: return 32;
case schema2::Type::UINT64: return 64;
case schema2::Type::FLOAT32: return 32;
case schema2::Type::FLOAT64: return 64;
case schema2::Type::TEXT: return -1;
case schema2::Type::DATA: return -1;
case schema2::Type::LIST: return -1;
case schema2::Type::ENUM: return 16;
case schema2::Type::STRUCT: return -1;
case schema2::Type::INTERFACE: return -1;
case schema2::Type::OBJECT: return -1;
}
return 0;
}
bool isEmptyValue(schema::Value::Reader value) {
auto body = value.getBody();
switch (body.which()) {
case schema::Value::Body::VOID_VALUE: return true;
case schema::Value::Body::BOOL_VALUE: return body.getBoolValue() == false;
case schema::Value::Body::INT8_VALUE: return body.getInt8Value() == 0;
case schema::Value::Body::INT16_VALUE: return body.getInt16Value() == 0;
case schema::Value::Body::INT32_VALUE: return body.getInt32Value() == 0;
case schema::Value::Body::INT64_VALUE: return body.getInt64Value() == 0;
case schema::Value::Body::UINT8_VALUE: return body.getUint8Value() == 0;
case schema::Value::Body::UINT16_VALUE: return body.getUint16Value() == 0;
case schema::Value::Body::UINT32_VALUE: return body.getUint32Value() == 0;
case schema::Value::Body::UINT64_VALUE: return body.getUint64Value() == 0;
case schema::Value::Body::FLOAT32_VALUE: return body.getFloat32Value() == 0;
case schema::Value::Body::FLOAT64_VALUE: return body.getFloat64Value() == 0;
case schema::Value::Body::TEXT_VALUE: return !body.hasTextValue();
case schema::Value::Body::DATA_VALUE: return !body.hasDataValue();
case schema::Value::Body::LIST_VALUE: return !body.hasListValue();
case schema::Value::Body::ENUM_VALUE: return body.getEnumValue() == 0;
case schema::Value::Body::STRUCT_VALUE: return !body.hasStructValue();
case schema::Value::Body::INTERFACE_VALUE: return true;
case schema::Value::Body::OBJECT_VALUE: return true;
bool isEmptyValue(schema2::Value::Reader value) {
switch (value.which()) {
case schema2::Value::VOID: return true;
case schema2::Value::BOOL: return value.getBool() == false;
case schema2::Value::INT8: return value.getInt8() == 0;
case schema2::Value::INT16: return value.getInt16() == 0;
case schema2::Value::INT32: return value.getInt32() == 0;
case schema2::Value::INT64: return value.getInt64() == 0;
case schema2::Value::UINT8: return value.getUint8() == 0;
case schema2::Value::UINT16: return value.getUint16() == 0;
case schema2::Value::UINT32: return value.getUint32() == 0;
case schema2::Value::UINT64: return value.getUint64() == 0;
case schema2::Value::FLOAT32: return value.getFloat32() == 0;
case schema2::Value::FLOAT64: return value.getFloat64() == 0;
case schema2::Value::TEXT: return !value.hasText();
case schema2::Value::DATA: return !value.hasData();
case schema2::Value::LIST: return !value.hasList();
case schema2::Value::ENUM: return value.getEnum() == 0;
case schema2::Value::STRUCT: return !value.hasStruct();
case schema2::Value::INTERFACE: return true;
case schema2::Value::OBJECT: return true;
}
return true;
}
kj::StringTree genValue(schema::Type::Reader type, schema::Value::Reader value, Schema scope) {
auto body = value.getBody();
switch (body.which()) {
case schema::Value::Body::VOID_VALUE: return kj::strTree("void");
case schema::Value::Body::BOOL_VALUE:
return kj::strTree(body.getBoolValue() ? "true" : "false");
case schema::Value::Body::INT8_VALUE: return kj::strTree((int)body.getInt8Value());
case schema::Value::Body::INT16_VALUE: return kj::strTree(body.getInt16Value());
case schema::Value::Body::INT32_VALUE: return kj::strTree(body.getInt32Value());
case schema::Value::Body::INT64_VALUE: return kj::strTree(body.getInt64Value());
case schema::Value::Body::UINT8_VALUE: return kj::strTree((uint)body.getUint8Value());
case schema::Value::Body::UINT16_VALUE: return kj::strTree(body.getUint16Value());
case schema::Value::Body::UINT32_VALUE: return kj::strTree(body.getUint32Value());
case schema::Value::Body::UINT64_VALUE: return kj::strTree(body.getUint64Value());
case schema::Value::Body::FLOAT32_VALUE: return kj::strTree(body.getFloat32Value());
case schema::Value::Body::FLOAT64_VALUE: return kj::strTree(body.getFloat64Value());
case schema::Value::Body::TEXT_VALUE:
return kj::strTree(DynamicValue::Reader(body.getTextValue()));
case schema::Value::Body::DATA_VALUE:
return kj::strTree(DynamicValue::Reader(body.getDataValue()));
case schema::Value::Body::LIST_VALUE: {
KJ_REQUIRE(type.getBody().which() == schema::Type::Body::LIST_TYPE, "type/value mismatch");
auto value = body.getListValue<DynamicList>(
ListSchema::of(type.getBody().getListType(), scope));
return kj::strTree(value);
kj::StringTree genValue(schema2::Type::Reader type, schema2::Value::Reader value, Schema scope) {
switch (value.which()) {
case schema2::Value::VOID: return kj::strTree("void");
case schema2::Value::BOOL:
return kj::strTree(value.getBool() ? "true" : "false");
case schema2::Value::INT8: return kj::strTree((int)value.getInt8());
case schema2::Value::INT16: return kj::strTree(value.getInt16());
case schema2::Value::INT32: return kj::strTree(value.getInt32());
case schema2::Value::INT64: return kj::strTree(value.getInt64());
case schema2::Value::UINT8: return kj::strTree((uint)value.getUint8());
case schema2::Value::UINT16: return kj::strTree(value.getUint16());
case schema2::Value::UINT32: return kj::strTree(value.getUint32());
case schema2::Value::UINT64: return kj::strTree(value.getUint64());
case schema2::Value::FLOAT32: return kj::strTree(value.getFloat32());
case schema2::Value::FLOAT64: return kj::strTree(value.getFloat64());
case schema2::Value::TEXT:
return kj::strTree(DynamicValue::Reader(value.getText()));
case schema2::Value::DATA:
return kj::strTree(DynamicValue::Reader(value.getData()));
case schema2::Value::LIST: {
KJ_REQUIRE(type.which() == schema2::Type::LIST, "type/value mismatch");
auto listValue = value.getList<DynamicList>(ListSchema::of(type.getList(), scope));
return kj::strTree(listValue);
}
case schema::Value::Body::ENUM_VALUE: {
KJ_REQUIRE(type.getBody().which() == schema::Type::Body::ENUM_TYPE, "type/value mismatch");
auto enumNode = scope.getDependency(type.getBody().getEnumType()).asEnum().getProto();
auto enumType = enumNode.getBody().getEnumNode();
auto enumerants = enumType.getEnumerants();
KJ_REQUIRE(body.getEnumValue() < enumerants.size(),
"Enum value out-of-range.", body.getEnumValue(), enumNode.getDisplayName());
return kj::strTree(enumerants[body.getEnumValue()].getName());
case schema2::Value::ENUM: {
KJ_REQUIRE(type.which() == schema2::Type::ENUM, "type/value mismatch");
auto enumNode = scope.getDependency(type.getEnum()).asEnum().getProto();
auto enumerants = enumNode.getEnum();
KJ_REQUIRE(value.getEnum() < enumerants.size(),
"Enum value out-of-range.", value.getEnum(), enumNode.getDisplayName());
return kj::strTree(enumerants[value.getEnum()].getName());
}
case schema::Value::Body::STRUCT_VALUE: {
KJ_REQUIRE(type.getBody().which() == schema::Type::Body::STRUCT_TYPE,
"type/value mismatch");
auto value = body.getStructValue<DynamicStruct>(
scope.getDependency(type.getBody().getStructType()).asStruct());
return kj::strTree(value);
case schema2::Value::STRUCT: {
KJ_REQUIRE(type.which() == schema2::Type::STRUCT, "type/value mismatch");
auto structValue = value.getStruct<DynamicStruct>(
scope.getDependency(type.getStruct()).asStruct());
return kj::strTree(structValue);
}
case schema::Value::Body::INTERFACE_VALUE: {
case schema2::Value::INTERFACE: {
return kj::strTree("");
}
case schema::Value::Body::OBJECT_VALUE: {
case schema2::Value::OBJECT: {
return kj::strTree("");
}
}
return kj::strTree("");
}
kj::StringTree genAnnotation(schema::Annotation::Reader annotation,
kj::StringTree genAnnotation(schema2::Annotation::Reader annotation,
Schema scope,
const char* prefix = " ", const char* suffix = "") {
auto decl = schemaLoader.get(annotation.getId());
auto body = decl.getProto().getBody();
KJ_REQUIRE(body.which() == schema::Node::Body::ANNOTATION_NODE);
auto annDecl = body.getAnnotationNode();
auto proto = decl.getProto();
KJ_REQUIRE(proto.which() == schema2::Node::ANNOTATION);
auto annDecl = proto.getAnnotation();
return kj::strTree(prefix, "$", nodeName(decl, scope), "(",
genValue(annDecl.getType(), annotation.getValue(), scope), ")", suffix);
}
kj::StringTree genAnnotations(List<schema::Annotation>::Reader list, Schema scope) {
kj::StringTree genAnnotations(List<schema2::Annotation>::Reader list, Schema scope) {
return kj::strTree(KJ_MAP(list, ann) { return genAnnotation(ann, scope); });
}
kj::StringTree genAnnotations(Schema schema) {
......@@ -308,60 +303,89 @@ private:
return genAnnotations(proto.getAnnotations(), schemaLoader.get(proto.getScopeId()));
}
const char* elementSizeName(schema::ElementSize size) {
const char* elementSizeName(schema2::ElementSize size) {
switch (size) {
case schema::ElementSize::EMPTY: return "void";
case schema::ElementSize::BIT: return "1-bit";
case schema::ElementSize::BYTE: return "8-bit";
case schema::ElementSize::TWO_BYTES: return "16-bit";
case schema::ElementSize::FOUR_BYTES: return "32-bit";
case schema::ElementSize::EIGHT_BYTES: return "64-bit";
case schema::ElementSize::POINTER: return "pointer";
case schema::ElementSize::INLINE_COMPOSITE: return "inline composite";
case schema2::ElementSize::EMPTY: return "void";
case schema2::ElementSize::BIT: return "1-bit";
case schema2::ElementSize::BYTE: return "8-bit";
case schema2::ElementSize::TWO_BYTES: return "16-bit";
case schema2::ElementSize::FOUR_BYTES: return "32-bit";
case schema2::ElementSize::EIGHT_BYTES: return "64-bit";
case schema2::ElementSize::POINTER: return "pointer";
case schema2::ElementSize::INLINE_COMPOSITE: return "inline composite";
}
return "";
}
kj::StringTree genStructMember(schema::StructNode::Member::Reader member,
Schema scope, Indent indent, int unionTag = -1) {
switch (member.getBody().which()) {
case schema::StructNode::Member::Body::FIELD_MEMBER: {
auto field = member.getBody().getFieldMember();
int size = typeSizeBits(field.getType());
return kj::strTree(
indent, member.getName(), " @", member.getOrdinal(),
" :", genType(field.getType(), scope),
isEmptyValue(field.getDefaultValue()) ? kj::strTree("") :
kj::strTree(" = ", genValue(field.getType(), field.getDefaultValue(), scope)),
genAnnotations(member.getAnnotations(), scope),
"; # ", size == -1 ? kj::strTree("ptr[", field.getOffset(), "]")
: kj::strTree("bits[", field.getOffset() * size, ", ",
(field.getOffset() + 1) * size, ")"),
unionTag != -1 ? kj::strTree(", union tag = ", unionTag) : kj::strTree(),
"\n");
struct OrderByCodeOrder {
template <typename T>
inline bool operator()(const T& a, const T& b) const {
return a.getProto().getCodeOrder() < b.getProto().getCodeOrder();
}
};
template <typename MemberList>
kj::Array<decltype(kj::instance<MemberList>()[0])> sortByCodeOrder(MemberList&& list) {
auto sorted = KJ_MAP(list, item) { return item; };
std::sort(sorted.begin(), sorted.end(), OrderByCodeOrder());
return kj::mv(sorted);
}
kj::Array<kj::StringTree> genStructFields(StructSchema schema, Indent indent) {
// Slightly hacky: We want to print in code order, but we also need to print the union in one
// chunk. Its fields should be together in code order anyway, but it's easier to simply
// output the whole union in place of the first union field, and then output nothing for the
// subsequent fields.
bool seenUnion = false;
return KJ_MAP(sortByCodeOrder(schema.getFields()), field) {
if (field.getProto().hasDiscriminantValue()) {
if (seenUnion) {
return kj::strTree();
} else {
seenUnion = true;
uint offset = schema.getProto().getStruct().getDiscriminantOffset();
return kj::strTree(
indent, "union { # tag bits [", offset * 16, ", ", offset * 16 + 16, ")\n",
KJ_MAP(sortByCodeOrder(schema.getUnionFields()), uField) {
return genStructField(uField.getProto(), schema, indent.next());
},
indent, "}\n");
}
} else {
return genStructField(field.getProto(), schema, indent);
}
case schema::StructNode::Member::Body::UNION_MEMBER: {
auto un = member.getBody().getUnionMember();
int i = 0;
};
}
kj::StringTree genStructField(schema2::Field::Reader field, Schema scope, Indent indent) {
switch (field.which()) {
case schema2::Field::REGULAR: {
auto regularField = field.getRegular();
int size = typeSizeBits(regularField.getType());
return kj::strTree(
indent, member.getName(), " @", member.getOrdinal(),
" union", genAnnotations(member.getAnnotations(), scope),
" { # tag bits[", un.getDiscriminantOffset() * 16, ", ",
un.getDiscriminantOffset() * 16 + 16, ")\n",
KJ_MAP(un.getMembers(), member) {
return genStructMember(member, scope, indent.next(), i++);
},
indent, "}\n");
indent, field.getName(), " @", field.getOrdinal().getExplicit(),
" :", genType(regularField.getType(), scope),
isEmptyValue(regularField.getDefaultValue()) ? kj::strTree("") :
kj::strTree(" = ", genValue(
regularField.getType(), regularField.getDefaultValue(), scope)),
genAnnotations(field.getAnnotations(), scope),
"; # ", size == -1 ? kj::strTree("ptr[", regularField.getOffset(), "]")
: kj::strTree("bits[", regularField.getOffset() * size, ", ",
(regularField.getOffset() + 1) * size, ")"),
field.hasDiscriminantValue()
? kj::strTree(", union tag = ", field.getDiscriminantValue()) : kj::strTree(),
"\n");
}
case schema::StructNode::Member::Body::GROUP_MEMBER: {
auto group = member.getBody().getGroupMember();
case schema2::Field::GROUP: {
auto group = scope.getDependency(field.getGroup()).asStruct();
return kj::strTree(
indent, member.getName(),
" group", genAnnotations(member.getAnnotations(), scope), " {",
unionTag != -1 ? kj::strTree(" # union tag = ", unionTag) : kj::strTree(), "\n",
KJ_MAP(group.getMembers(), member) {
return genStructMember(member, scope, indent.next());
},
indent, field.getName(),
" :group", genAnnotations(field.getAnnotations(), scope), " {",
field.hasDiscriminantValue()
? kj::strTree(", union tag = ", field.getDiscriminantValue()) : kj::strTree(),
"\n",
genStructFields(group, indent.next()),
indent, "}\n");
}
}
......@@ -375,54 +399,51 @@ private:
KJ_FAIL_REQUIRE("Aliases not implemented.");
}
switch (proto.getBody().which()) {
case schema::Node::Body::FILE_NODE:
switch (proto.which()) {
case schema2::Node::FILE:
KJ_FAIL_REQUIRE("Encountered nested file node.");
break;
case schema::Node::Body::STRUCT_NODE: {
auto body = proto.getBody().getStructNode();
case schema2::Node::STRUCT: {
auto structProto = proto.getStruct();
return kj::strTree(
indent, "struct ", name,
" @0x", kj::hex(proto.getId()), genAnnotations(schema), " { # ",
body.getDataSectionWordSize() * 8, " bytes, ",
body.getPointerSectionSize(), " ptrs",
body.getPreferredListEncoding() == schema::ElementSize::INLINE_COMPOSITE
structProto.getDataSectionWordSize() * 8, " bytes, ",
structProto.getPointerSectionSize(), " ptrs",
structProto.getPreferredListEncoding() == schema2::ElementSize::INLINE_COMPOSITE
? kj::strTree()
: kj::strTree(", packed as ", elementSizeName(body.getPreferredListEncoding())),
: kj::strTree(", packed as ", elementSizeName(structProto.getPreferredListEncoding())),
"\n",
KJ_MAP(body.getMembers(), member) {
return genStructMember(member, schema, indent.next());
},
genStructFields(schema.asStruct(), indent.next()),
genNestedDecls(schema, indent.next()),
indent, "}\n");
}
case schema::Node::Body::ENUM_NODE: {
auto body = proto.getBody().getEnumNode();
uint i = 0;
case schema2::Node::ENUM: {
return kj::strTree(
indent, "enum ", name, " @0x", kj::hex(proto.getId()), genAnnotations(schema), " {\n",
KJ_MAP(body.getEnumerants(), enumerant) {
return kj::strTree(indent.next(), enumerant.getName(), " @", i++,
genAnnotations(enumerant.getAnnotations(), schema), ";\n");
KJ_MAP(sortByCodeOrder(schema.asEnum().getEnumerants()), enumerant) {
return kj::strTree(indent.next(), enumerant.getProto().getName(), " @",
enumerant.getIndex(),
genAnnotations(enumerant.getProto().getAnnotations(), schema),
";\n");
},
genNestedDecls(schema, indent.next()),
indent, "}\n");
}
case schema::Node::Body::INTERFACE_NODE: {
auto body = proto.getBody().getInterfaceNode();
uint i = 0;
case schema2::Node::INTERFACE: {
return kj::strTree(
indent, "interface ", name, " @0x", kj::hex(proto.getId()),
genAnnotations(schema), " {\n",
KJ_MAP(body.getMethods(), method) {
int j = 0;
KJ_MAP(sortByCodeOrder(schema.asInterface().getMethods()), method) {
int i = 0;
auto methodProto = method.getProto();
return kj::strTree(
indent.next(), method.getName(), " @", i++, "(",
KJ_MAP(method.getParams(), param) {
bool hasDefault = j >= method.getRequiredParamCount() ||
indent.next(), methodProto.getName(), " @", method.getIndex(), "(",
KJ_MAP(methodProto.getParams(), param) {
bool hasDefault = i >= methodProto.getRequiredParamCount() ||
!isEmptyValue(param.getDefaultValue());
return kj::strTree(
j++ > 0 ? ", " : "",
i++ > 0 ? ", " : "",
param.getName(), ": ", genType(param.getType(), schema),
hasDefault
? kj::strTree(" = ", genValue(
......@@ -430,44 +451,48 @@ private:
: kj::strTree(),
genAnnotations(param.getAnnotations(), schema));
},
") :", genType(method.getReturnType(), schema),
genAnnotations(method.getAnnotations(), schema), ";\n");
") :", genType(methodProto.getReturnType(), schema),
genAnnotations(methodProto.getAnnotations(), schema), ";\n");
},
genNestedDecls(schema, indent.next()),
indent, "}\n");
}
case schema::Node::Body::CONST_NODE: {
auto body = proto.getBody().getConstNode();
case schema2::Node::CONST: {
auto constProto = proto.getConst();
return kj::strTree(
indent, "const ", name, " @0x", kj::hex(proto.getId()), " :",
genType(body.getType(), schema), " = ",
genValue(body.getType(), body.getValue(), schema), ";\n");
genType(constProto.getType(), schema), " = ",
genValue(constProto.getType(), constProto.getValue(), schema), ";\n");
}
case schema::Node::Body::ANNOTATION_NODE: {
auto body = proto.getBody().getAnnotationNode();
kj::CappedArray<const char*, 11> targets;
uint i = 0;
if (body.getTargetsFile()) targets[i++] = "file";
if (body.getTargetsConst()) targets[i++] = "const";
if (body.getTargetsEnum()) targets[i++] = "enum";
if (body.getTargetsEnumerant()) targets[i++] = "enumerant";
if (body.getTargetsStruct()) targets[i++] = "struct";
if (body.getTargetsField()) targets[i++] = "field";
if (body.getTargetsUnion()) targets[i++] = "union";
if (body.getTargetsInterface()) targets[i++] = "interface";
if (body.getTargetsMethod()) targets[i++] = "method";
if (body.getTargetsParam()) targets[i++] = "param";
if (body.getTargetsAnnotation()) targets[i++] = "annotation";
if (i == targets.size()) {
targets[0] = "*";
targets.setSize(1);
} else {
targets.setSize(i);
case schema2::Node::ANNOTATION: {
auto annotationProto = proto.getAnnotation();
kj::Vector<kj::String> targets(8);
bool targetsAll = true;
auto dynamic = toDynamic(annotationProto);
for (auto field: dynamic.getSchema().getFields()) {
auto fieldName = field.getProto().getName();
if (fieldName.startsWith("targets")) {
if (dynamic.get(field).as<bool>()) {
auto target = kj::str(fieldName.slice(strlen("targets")));
target[0] = target[0] - 'A' + 'a';
targets.add(kj::mv(target));
} else {
targetsAll = false;
}
}
}
if (targetsAll) {
targets = kj::Vector<kj::String>(1);
targets[0] = kj::heapString("*");
}
return kj::strTree(
indent, "annotation ", name, " @0x", kj::hex(proto.getId()),
" (", strArray(targets, ", "), ") :",
genType(body.getType(), schema), genAnnotations(schema), ";\n");
genType(annotationProto.getType(), schema), genAnnotations(schema), ";\n");
}
}
......@@ -483,9 +508,7 @@ private:
kj::StringTree genFile(Schema file) {
auto proto = file.getProto();
auto body = proto.getBody();
KJ_REQUIRE(body.which() == schema::Node::Body::FILE_NODE, "Expected a file node.",
(uint)body.which());
KJ_REQUIRE(proto.which() == schema2::Node::FILE, "Expected a file node.", (uint)proto.which());
return kj::strTree(
"# ", proto.getDisplayName(), "\n",
......@@ -498,7 +521,7 @@ private:
ReaderOptions options;
options.traversalLimitInWords = 1 << 30; // Don't limit.
StreamFdMessageReader reader(STDIN_FILENO, options);
auto request = reader.getRoot<schema::CodeGeneratorRequest>();
auto request = reader.getRoot<schema2::CodeGeneratorRequest>();
for (auto node: request.getNodes()) {
schemaLoader.load(node);
......@@ -507,8 +530,8 @@ private:
kj::FdOutputStream rawOut(STDOUT_FILENO);
kj::BufferedOutputStreamWrapper out(rawOut);
for (auto fileId: request.getRequestedFiles()) {
genFile(schemaLoader.get(fileId)).visit(
for (auto requestedFile: request.getRequestedFiles()) {
genFile(schemaLoader.get(requestedFile.getId())).visit(
[&](kj::ArrayPtr<const char> text) {
out.write(text.begin(), text.size());
});
......
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