Commit f651e5cf authored by Kenton Varda's avatar Kenton Varda

Reverse order of KJ_MAP to be consistent with e.g. KJ_IF_MAYBE.

parent e65d5d0f
......@@ -119,9 +119,9 @@ struct OrderByName {
template <typename MemberList>
kj::Array<uint> makeMembersByName(MemberList&& members) {
auto sorted = KJ_MAP(members, member) { return member; };
auto sorted = KJ_MAP(member, members) { return member; };
std::sort(sorted.begin(), sorted.end(), OrderByName());
return KJ_MAP(sorted, member) { return member.getIndex(); };
return KJ_MAP(member, sorted) { return member.getIndex(); };
}
kj::StringPtr baseName(kj::StringPtr path) {
......@@ -534,7 +534,7 @@ private:
"inline bool ", scope, "Reader::has", titleCase, "() const {\n",
unionDiscrim.has,
" return ",
kj::StringTree(KJ_MAP(slots, slot) {
kj::StringTree(KJ_MAP(slot, slots) {
switch (sectionFor(slot.whichType)) {
case Section::NONE:
return kj::strTree();
......@@ -552,7 +552,7 @@ private:
"inline bool ", scope, "Builder::has", titleCase, "() {\n",
unionDiscrim.has,
" return ",
kj::StringTree(KJ_MAP(slots, slot) {
kj::StringTree(KJ_MAP(slot, slots) {
switch (sectionFor(slot.whichType)) {
case Section::NONE:
return kj::strTree();
......@@ -577,7 +577,7 @@ private:
"}\n"
"inline ", scope, titleCase, "::Builder ", scope, "Builder::init", titleCase, "() {\n",
unionDiscrim.set,
KJ_MAP(slots, slot) {
KJ_MAP(slot, slots) {
switch (sectionFor(slot.whichType)) {
case Section::NONE:
return kj::strTree();
......@@ -1094,10 +1094,10 @@ private:
// Convert the encoded schema to a literal byte array.
kj::ArrayPtr<const word> rawSchema = schema.asUncheckedMessage();
auto schemaLiteral = kj::StringTree(KJ_MAP(rawSchema, w) {
auto schemaLiteral = kj::StringTree(KJ_MAP(w, rawSchema) {
const byte* bytes = reinterpret_cast<const byte*>(&w);
return kj::strTree(KJ_MAP(kj::range<uint>(0, sizeof(word)), i) {
return kj::strTree(KJ_MAP(i, kj::range<uint>(0, sizeof(word))) {
auto text = kj::toCharSequence(kj::implicitCast<uint>(bytes[i]));
return kj::strTree(kj::repeat(' ', 4 - text.size()), text, ",");
});
......@@ -1140,15 +1140,15 @@ private:
" {", kj::mv(schemaLiteral), " }\n"
"};\n"
"static const ::capnp::_::RawSchema* const d_", hexId, "[] = {\n",
KJ_MAP(deps, depId) {
KJ_MAP(depId, deps) {
return kj::strTree(" &s_", kj::hex(depId), ",\n");
},
"};\n"
"static const uint16_t m_", hexId, "[] = {",
kj::StringTree(KJ_MAP(membersByName, index) { return kj::strTree(index); }, ", "),
kj::StringTree(KJ_MAP(index, membersByName) { return kj::strTree(index); }, ", "),
"};\n"
"static const uint16_t i_", hexId, "[] = {",
kj::StringTree(KJ_MAP(membersByDiscrim, index) { return kj::strTree(index); }, ", "),
kj::StringTree(KJ_MAP(index, membersByDiscrim) { return kj::strTree(index); }, ", "),
"};\n"
"const ::capnp::_::RawSchema s_", hexId, " = {\n"
" 0x", hexId, ", b_", hexId, ".words, ", rawSchema.size(), ", d_", hexId, ", m_", hexId, ",\n"
......@@ -1161,7 +1161,7 @@ private:
case schema::Node::STRUCT: {
auto fieldTexts =
KJ_MAP(schema.asStruct().getFields(), f) { return makeFieldText(subScope, f); };
KJ_MAP(f, schema.asStruct().getFields()) { return makeFieldText(subScope, f); };
auto structNode = proto.getStruct();
uint discrimOffset = structNode.getDiscriminantOffset();
......@@ -1178,7 +1178,7 @@ private:
" class Builder;\n",
structNode.getDiscriminantCount() == 0 ? kj::strTree() : kj::strTree(
" enum Which: uint16_t {\n",
KJ_MAP(structNode.getFields(), f) {
KJ_MAP(f, structNode.getFields()) {
if (f.hasDiscriminantValue()) {
return kj::strTree(" ", toUpperCase(f.getName()), ",\n");
} else {
......@@ -1186,17 +1186,17 @@ private:
}
},
" };\n"),
KJ_MAP(nestedTexts, n) { return kj::mv(n.outerTypeDecl); },
KJ_MAP(n, nestedTexts) { return kj::mv(n.outerTypeDecl); },
"};\n"
"\n",
KJ_MAP(nestedTexts, n) { return kj::mv(n.outerTypeDef); }),
KJ_MAP(n, nestedTexts) { return kj::mv(n.outerTypeDef); }),
kj::strTree(
makeReaderDef(fullName, name, structNode.getDiscriminantCount() != 0,
KJ_MAP(fieldTexts, f) { return kj::mv(f.readerMethodDecls); }),
KJ_MAP(f, fieldTexts) { return kj::mv(f.readerMethodDecls); }),
makeBuilderDef(fullName, name, structNode.getDiscriminantCount() != 0,
KJ_MAP(fieldTexts, f) { return kj::mv(f.builderMethodDecls); }),
KJ_MAP(nestedTexts, n) { return kj::mv(n.readerBuilderDefs); }),
KJ_MAP(f, fieldTexts) { return kj::mv(f.builderMethodDecls); }),
KJ_MAP(n, nestedTexts) { return kj::mv(n.readerBuilderDefs); }),
kj::strTree(
structNode.getDiscriminantCount() == 0 ? kj::strTree() : kj::strTree(
......@@ -1207,16 +1207,16 @@ private:
" return _builder.getDataField<Which>(", discrimOffset, " * ::capnp::ELEMENTS);\n"
"}\n"
"\n"),
KJ_MAP(fieldTexts, f) { return kj::mv(f.inlineMethodDefs); },
KJ_MAP(nestedTexts, n) { return kj::mv(n.inlineMethodDefs); }),
KJ_MAP(f, fieldTexts) { return kj::mv(f.inlineMethodDefs); },
KJ_MAP(n, nestedTexts) { return kj::mv(n.inlineMethodDefs); }),
kj::strTree(
kj::mv(schemaDecl),
KJ_MAP(nestedTexts, n) { return kj::mv(n.capnpSchemaDecls); }),
KJ_MAP(n, nestedTexts) { return kj::mv(n.capnpSchemaDecls); }),
kj::strTree(
kj::mv(schemaDef),
KJ_MAP(nestedTexts, n) { return kj::mv(n.capnpSchemaDefs); }),
KJ_MAP(n, nestedTexts) { return kj::mv(n.capnpSchemaDefs); }),
kj::strTree(
"CAPNP_DECLARE_STRUCT(\n"
......@@ -1225,12 +1225,12 @@ private:
structNode.getPointerCount(), ", ",
FIELD_SIZE_NAMES[static_cast<uint>(structNode.getPreferredListEncoding())],
");\n",
KJ_MAP(nestedTexts, n) { return kj::mv(n.capnpPrivateDecls); }),
KJ_MAP(n, nestedTexts) { return kj::mv(n.capnpPrivateDecls); }),
kj::strTree(
"CAPNP_DEFINE_STRUCT(\n"
" ", namespace_, "::", fullName, ");\n",
KJ_MAP(nestedTexts, n) { return kj::mv(n.capnpPrivateDefs); }),
KJ_MAP(n, nestedTexts) { return kj::mv(n.capnpPrivateDefs); }),
};
}
......@@ -1240,7 +1240,7 @@ private:
return NodeText {
scope.size() == 0 ? kj::strTree() : kj::strTree(
" enum class ", name, ": uint16_t {\n",
KJ_MAP(enumerants, e) {
KJ_MAP(e, enumerants) {
return kj::strTree(" ", toUpperCase(e.getProto().getName()), ",\n");
},
" };\n"
......@@ -1248,7 +1248,7 @@ private:
scope.size() > 0 ? kj::strTree() : kj::strTree(
"enum class ", name, ": uint16_t {\n",
KJ_MAP(enumerants, e) {
KJ_MAP(e, enumerants) {
return kj::strTree(" ", toUpperCase(e.getProto().getName()), ",\n");
},
"};\n"
......@@ -1363,7 +1363,7 @@ private:
}
}
auto nodeTexts = KJ_MAP(node.getNestedNodes(), nested) {
auto nodeTexts = KJ_MAP(nested, node.getNestedNodes()) {
return makeNodeText(namespacePrefix, "", nested.getName(), schemaLoader.get(nested.getId()));
};
......@@ -1391,7 +1391,7 @@ private:
"use the same version of the Cap'n Proto compiler and library.\"\n"
"#endif\n"
"\n",
KJ_MAP(includes, path) {
KJ_MAP(path, includes) {
if (path.startsWith("/")) {
return kj::strTree("#include <", path.slice(1), ".h>\n");
} else {
......@@ -1400,30 +1400,30 @@ private:
},
"\n",
KJ_MAP(namespaceParts, n) { return kj::strTree("namespace ", n, " {\n"); }, "\n",
KJ_MAP(nodeTexts, n) { return kj::mv(n.outerTypeDef); },
KJ_MAP(namespaceParts, n) { return kj::strTree("} // namespace\n"); }, "\n",
KJ_MAP(n, namespaceParts) { return kj::strTree("namespace ", n, " {\n"); }, "\n",
KJ_MAP(n, nodeTexts) { return kj::mv(n.outerTypeDef); },
KJ_MAP(n, namespaceParts) { return kj::strTree("} // namespace\n"); }, "\n",
separator, "\n"
"namespace capnp {\n"
"namespace schemas {\n"
"\n",
KJ_MAP(nodeTexts, n) { return kj::mv(n.capnpSchemaDecls); },
KJ_MAP(n, nodeTexts) { return kj::mv(n.capnpSchemaDecls); },
"\n"
"} // namespace schemas\n"
"namespace _ { // private\n"
"\n",
KJ_MAP(nodeTexts, n) { return kj::mv(n.capnpPrivateDecls); },
KJ_MAP(n, nodeTexts) { return kj::mv(n.capnpPrivateDecls); },
"\n"
"} // namespace _ (private)\n"
"} // namespace capnp\n"
"\n", separator, "\n",
KJ_MAP(namespaceParts, n) { return kj::strTree("namespace ", n, " {\n"); }, "\n",
KJ_MAP(nodeTexts, n) { return kj::mv(n.readerBuilderDefs); },
KJ_MAP(n, namespaceParts) { return kj::strTree("namespace ", n, " {\n"); }, "\n",
KJ_MAP(n, nodeTexts) { return kj::mv(n.readerBuilderDefs); },
separator, "\n",
KJ_MAP(nodeTexts, n) { return kj::mv(n.inlineMethodDefs); },
KJ_MAP(namespaceParts, n) { return kj::strTree("} // namespace\n"); }, "\n",
KJ_MAP(n, nodeTexts) { return kj::mv(n.inlineMethodDefs); },
KJ_MAP(n, namespaceParts) { return kj::strTree("} // namespace\n"); }, "\n",
"#endif // CAPNP_INCLUDED_", kj::hex(node.getId()), "_\n"),
kj::strTree(
......@@ -1434,10 +1434,10 @@ private:
"\n"
"namespace capnp {\n"
"namespace schemas {\n",
KJ_MAP(nodeTexts, n) { return kj::mv(n.capnpSchemaDefs); },
KJ_MAP(n, nodeTexts) { return kj::mv(n.capnpSchemaDefs); },
"} // namespace schemas\n"
"namespace _ { // private\n",
KJ_MAP(nodeTexts, n) { return kj::mv(n.capnpPrivateDefs); },
KJ_MAP(n, nodeTexts) { return kj::mv(n.capnpPrivateDefs); },
"} // namespace _ (private)\n"
"} // namespace capnp\n")
};
......
......@@ -296,7 +296,7 @@ private:
}
kj::StringTree genAnnotations(List<schema::Annotation>::Reader list, Schema scope) {
return kj::strTree(KJ_MAP(list, ann) { return genAnnotation(ann, scope); });
return kj::strTree(KJ_MAP(ann, list) { return genAnnotation(ann, scope); });
}
kj::StringTree genAnnotations(Schema schema) {
auto proto = schema.getProto();
......@@ -326,7 +326,7 @@ private:
template <typename MemberList>
kj::Array<decltype(kj::instance<MemberList>()[0])> sortByCodeOrder(MemberList&& list) {
auto sorted = KJ_MAP(list, item) { return item; };
auto sorted = KJ_MAP(item, list) { return item; };
std::sort(sorted.begin(), sorted.end(), OrderByCodeOrder());
return kj::mv(sorted);
}
......@@ -338,7 +338,7 @@ private:
// subsequent fields.
bool seenUnion = false;
return KJ_MAP(sortByCodeOrder(schema.getFields()), field) {
return KJ_MAP(field, sortByCodeOrder(schema.getFields())) {
if (field.getProto().hasDiscriminantValue()) {
if (seenUnion) {
return kj::strTree();
......@@ -350,7 +350,7 @@ private:
auto unionFields = sortByCodeOrder(schema.getUnionFields());
return kj::strTree(
indent, "union { # tag bits [", offset * 16, ", ", offset * 16 + 16, ")\n",
KJ_MAP(unionFields, uField) {
KJ_MAP(uField, unionFields) {
return genStructField(uField.getProto(), schema, indent.next());
},
indent, "}\n");
......@@ -424,7 +424,7 @@ private:
case schema::Node::ENUM: {
return kj::strTree(
indent, "enum ", name, " @0x", kj::hex(proto.getId()), genAnnotations(schema), " {\n",
KJ_MAP(sortByCodeOrder(schema.asEnum().getEnumerants()), enumerant) {
KJ_MAP(enumerant, sortByCodeOrder(schema.asEnum().getEnumerants())) {
return kj::strTree(indent.next(), enumerant.getProto().getName(), " @",
enumerant.getIndex(),
genAnnotations(enumerant.getProto().getAnnotations(), schema),
......@@ -437,12 +437,12 @@ private:
return kj::strTree(
indent, "interface ", name, " @0x", kj::hex(proto.getId()),
genAnnotations(schema), " {\n",
KJ_MAP(sortByCodeOrder(schema.asInterface().getMethods()), method) {
KJ_MAP(method, sortByCodeOrder(schema.asInterface().getMethods())) {
int i = 0;
auto methodProto = method.getProto();
return kj::strTree(
indent.next(), methodProto.getName(), " @", method.getIndex(), "(",
KJ_MAP(methodProto.getParams(), param) {
KJ_MAP(param, methodProto.getParams()) {
bool hasDefault = i >= methodProto.getRequiredParamCount() ||
!isEmptyValue(param.getDefaultValue());
return kj::strTree(
......@@ -504,7 +504,7 @@ private:
kj::StringTree genNestedDecls(Schema schema, Indent indent) {
uint64_t id = schema.getProto().getId();
return kj::strTree(KJ_MAP(schema.getProto().getNestedNodes(), nested) {
return kj::strTree(KJ_MAP(nested, schema.getProto().getNestedNodes()) {
return genDecl(schemaLoader.get(nested.getId()), nested.getName(), id, indent);
});
}
......@@ -516,7 +516,7 @@ private:
return kj::strTree(
"# ", proto.getDisplayName(), "\n",
"@0x", kj::hex(proto.getId()), ";\n",
KJ_MAP(proto.getAnnotations(), ann) { return genAnnotation(ann, file, "", ";\n"); },
KJ_MAP(ann, proto.getAnnotations()) { return genAnnotation(ann, file, "", ";\n"); },
genNestedDecls(file, Indent(0)));
}
......
......@@ -523,7 +523,7 @@ const Compiler::Node::Content& Compiler::Node::getContent(Content::State minimum
// Create the final schema.
KJ_IF_MAYBE(exception, kj::runCatchingExceptions([&](){
auto nodeSet = locked->translator->finish();
locked->auxSchemas = KJ_MAP(nodeSet.auxNodes, auxNode) {
locked->auxSchemas = KJ_MAP(auxNode, nodeSet.auxNodes) {
return module->getCompiler().getFinalLoader().loadOnce(auxNode);
};
locked->finalSchema = module->getCompiler().getFinalLoader().loadOnce(nodeSet.node);
......
......@@ -536,7 +536,7 @@ NodeTranslator::NodeTranslator(
NodeTranslator::NodeSet NodeTranslator::getBootstrapNode() {
return NodeSet {
wipNode.getReader(),
KJ_MAP(groups, g) { return g.getReader(); }
KJ_MAP(g, groups) { return g.getReader(); }
};
}
......
......@@ -173,7 +173,7 @@ static kj::StringTree print(const DynamicValue::Reader& value,
case DynamicValue::LIST: {
auto listValue = value.as<DynamicList>();
auto which = listValue.getSchema().whichElementType();
kj::Array<kj::StringTree> elements = KJ_MAP(listValue, element) {
kj::Array<kj::StringTree> elements = KJ_MAP(element, listValue) {
return print(element, which, indent.next(), BARE);
};
return kj::strTree('[', indent.delimit(kj::mv(elements), mode), ']');
......
......@@ -331,7 +331,7 @@ TEST(Array, OwnConst) {
TEST(Array, Map) {
StringPtr foo = "abcd";
Array<char> bar = KJ_MAP(foo, c) -> char { return c + 1; };
Array<char> bar = KJ_MAP(c, foo) -> char { return c + 1; };
EXPECT_STREQ("bcde", str(bar).cStr());
}
......
......@@ -466,15 +466,15 @@ private:
};
// =======================================================================================
// KJ_MAP_ARRAY
// KJ_MAP
#define KJ_MAP(array, elementName) \
#define KJ_MAP(elementName, array) \
::kj::_::Mapper<KJ_DECLTYPE_REF(array)>(array) * [&](decltype(*(array).begin()) elementName)
// Applies some function to every element of an array, returning an Array of the results, with
// nice syntax. Example:
//
// StringPtr foo = "abcd";
// Array<char> bar = KJ_MAP(foo, c) -> char { return c + 1; };
// Array<char> bar = KJ_MAP(c, foo) -> char { return c + 1; };
// KJ_ASSERT(str(bar) == "bcde");
namespace _ { // private
......
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