#include "TableGenBackends.h"
#include <deque>
#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/TableGen/Record.h"
#include "llvm/TableGen/TableGenBackend.h"
namespace {
using llvm::formatv;
class Hierarchy {
public:
Hierarchy(const llvm::RecordKeeper &Records) {
for (llvm::Record *T : Records.getAllDerivedDefinitions("NodeType"))
add(T);
for (llvm::Record *Derived : Records.getAllDerivedDefinitions("NodeType"))
if (llvm::Record *Base = Derived->getValueAsOptionalDef("base"))
link(Derived, Base);
for (NodeType &N : AllTypes) {
llvm::sort(N.Derived, [](const NodeType *L, const NodeType *R) {
return L->Record->getName() < R->Record->getName();
});
assert(N.Record->isSubClassOf("Alternatives") ||
N.Record->isSubClassOf("External") || N.Derived.empty());
assert(!N.Record->isSubClassOf("Alternatives") || !N.Derived.empty());
}
}
struct NodeType {
const llvm::Record *Record = nullptr;
const NodeType *Base = nullptr;
std::vector<const NodeType *> Derived;
llvm::StringRef name() const { return Record->getName(); }
};
NodeType &get(llvm::StringRef Name = "Node") {
auto NI = ByName.find(Name);
assert(NI != ByName.end() && "no such node");
return *NI->second;
}
void visit(llvm::function_ref<void(const NodeType &)> CB,
const NodeType *Start = nullptr) {
if (Start == nullptr)
Start = &get();
CB(*Start);
for (const NodeType *D : Start->Derived)
visit(CB, D);
}
private:
void add(const llvm::Record *R) {
AllTypes.emplace_back();
AllTypes.back().Record = R;
bool Inserted = ByName.try_emplace(R->getName(), &AllTypes.back()).second;
assert(Inserted && "Duplicate node name");
(void)Inserted;
}
void link(const llvm::Record *Derived, const llvm::Record *Base) {
auto &CN = get(Derived->getName()), &PN = get(Base->getName());
assert(CN.Base == nullptr && "setting base twice");
PN.Derived.push_back(&CN);
CN.Base = &PN;
}
std::deque<NodeType> AllTypes;
llvm::DenseMap<llvm::StringRef, NodeType *> ByName;
};
const Hierarchy::NodeType &firstConcrete(const Hierarchy::NodeType &N) {
return N.Derived.empty() ? N : firstConcrete(*N.Derived.front());
}
const Hierarchy::NodeType &lastConcrete(const Hierarchy::NodeType &N) {
return N.Derived.empty() ? N : lastConcrete(*N.Derived.back());
}
struct SyntaxConstraint {
SyntaxConstraint(const llvm::Record &R) {
if (R.isSubClassOf("Optional")) {
*this = SyntaxConstraint(*R.getValueAsDef("inner"));
} else if (R.isSubClassOf("AnyToken")) {
NodeType = "Leaf";
} else if (R.isSubClassOf("NodeType")) {
NodeType = R.getName().str();
} else {
assert(false && "Unhandled Syntax kind");
}
}
std::string NodeType;
};
}
void clang::EmitClangSyntaxNodeList(llvm::RecordKeeper &Records,
llvm::raw_ostream &OS) {
llvm::emitSourceFileHeader("Syntax tree node list", OS);
Hierarchy H(Records);
OS << R"cpp(
#ifndef NODE
#define NODE(Kind, Base)
#endif
#ifndef CONCRETE_NODE
#define CONCRETE_NODE(Kind, Base) NODE(Kind, Base)
#endif
#ifndef ABSTRACT_NODE
#define ABSTRACT_NODE(Kind, Base, First, Last) NODE(Kind, Base)
#endif
)cpp";
H.visit([&](const Hierarchy::NodeType &N) {
if (N.Base == nullptr)
return;
if (N.Derived.empty())
OS << formatv("CONCRETE_NODE({0},{1})\n", N.name(), N.Base->name());
else
OS << formatv("ABSTRACT_NODE({0},{1},{2},{3})\n", N.name(),
N.Base->name(), firstConcrete(N).name(),
lastConcrete(N).name());
});
OS << R"cpp(
#undef NODE
#undef CONCRETE_NODE
#undef ABSTRACT_NODE
)cpp";
}
static void printDoc(llvm::StringRef Doc, llvm::raw_ostream &OS) {
Doc = Doc.rtrim();
llvm::StringRef Line;
while (Line.trim().empty() && !Doc.empty())
std::tie(Line, Doc) = Doc.split('\n');
llvm::StringRef Indent = Line.take_while(llvm::isSpace);
for (; !Line.empty() || !Doc.empty(); std::tie(Line, Doc) = Doc.split('\n')) {
Line.consume_front(Indent);
OS << "/// " << Line << "\n";
}
}
void clang::EmitClangSyntaxNodeClasses(llvm::RecordKeeper &Records,
llvm::raw_ostream &OS) {
llvm::emitSourceFileHeader("Syntax tree node list", OS);
Hierarchy H(Records);
OS << "\n// Forward-declare node types so we don't have to carefully "
"sequence definitions.\n";
H.visit([&](const Hierarchy::NodeType &N) {
OS << "class " << N.name() << ";\n";
});
OS << "\n// Node definitions\n\n";
H.visit([&](const Hierarchy::NodeType &N) {
if (N.Record->isSubClassOf("External"))
return;
printDoc(N.Record->getValueAsString("documentation"), OS);
OS << formatv("class {0}{1} : public {2} {{\n", N.name(),
N.Derived.empty() ? " final" : "", N.Base->name());
if (N.Derived.empty())
OS << formatv("public:\n {0}() : {1}(NodeKind::{0}) {{}\n", N.name(),
N.Base->name());
else
OS << formatv("protected:\n {0}(NodeKind K) : {1}(K) {{}\npublic:\n",
N.name(), N.Base->name());
if (N.Record->isSubClassOf("Sequence")) {
for (const auto &C : N.Record->getValueAsListOfDefs("children")) {
assert(C->isSubClassOf("Role"));
llvm::StringRef Role = C->getValueAsString("role");
SyntaxConstraint Constraint(*C->getValueAsDef("syntax"));
for (const char *Const : {"", "const "})
OS << formatv(
" {2}{1} *get{0}() {2} {{\n"
" return llvm::cast_or_null<{1}>(findChild(NodeRole::{0}));\n"
" }\n",
Role, Constraint.NodeType, Const);
}
}
OS << " static bool classof(const Node *N);\n";
OS << "};\n\n";
});
}