[ORC] Add std::tuple support to SimplePackedSerialization.
[llvm-project.git] / llvm / lib / TableGen / JSONBackend.cpp
blob8ddfd9f0452499412d6b2072273d5400a2031a8a
1 //===- JSONBackend.cpp - Generate a JSON dump of all records. -*- C++ -*-=====//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This TableGen back end generates a machine-readable representation
10 // of all the classes and records defined by the input, in JSON format.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/ADT/BitVector.h"
15 #include "llvm/Support/Debug.h"
16 #include "llvm/TableGen/Error.h"
17 #include "llvm/TableGen/Record.h"
18 #include "llvm/TableGen/TableGenBackend.h"
19 #include "llvm/Support/JSON.h"
21 #define DEBUG_TYPE "json-emitter"
23 using namespace llvm;
25 namespace {
27 class JSONEmitter {
28 private:
29 RecordKeeper &Records;
31 json::Value translateInit(const Init &I);
33 public:
34 JSONEmitter(RecordKeeper &R);
36 void run(raw_ostream &OS);
39 } // end anonymous namespace
41 JSONEmitter::JSONEmitter(RecordKeeper &R) : Records(R) {}
43 json::Value JSONEmitter::translateInit(const Init &I) {
45 // Init subclasses that we return as JSON primitive values of one
46 // kind or another.
48 if (isa<UnsetInit>(&I)) {
49 return nullptr;
50 } else if (auto *Bit = dyn_cast<BitInit>(&I)) {
51 return Bit->getValue() ? 1 : 0;
52 } else if (auto *Bits = dyn_cast<BitsInit>(&I)) {
53 json::Array array;
54 for (unsigned i = 0, limit = Bits->getNumBits(); i < limit; i++)
55 array.push_back(translateInit(*Bits->getBit(i)));
56 return std::move(array);
57 } else if (auto *Int = dyn_cast<IntInit>(&I)) {
58 return Int->getValue();
59 } else if (auto *Str = dyn_cast<StringInit>(&I)) {
60 return Str->getValue();
61 } else if (auto *List = dyn_cast<ListInit>(&I)) {
62 json::Array array;
63 for (auto val : *List)
64 array.push_back(translateInit(*val));
65 return std::move(array);
68 // Init subclasses that we return as JSON objects containing a
69 // 'kind' discriminator. For these, we also provide the same
70 // translation back into TableGen input syntax that -print-records
71 // would give.
73 json::Object obj;
74 obj["printable"] = I.getAsString();
76 if (auto *Def = dyn_cast<DefInit>(&I)) {
77 obj["kind"] = "def";
78 obj["def"] = Def->getDef()->getName();
79 return std::move(obj);
80 } else if (auto *Var = dyn_cast<VarInit>(&I)) {
81 obj["kind"] = "var";
82 obj["var"] = Var->getName();
83 return std::move(obj);
84 } else if (auto *VarBit = dyn_cast<VarBitInit>(&I)) {
85 if (auto *Var = dyn_cast<VarInit>(VarBit->getBitVar())) {
86 obj["kind"] = "varbit";
87 obj["var"] = Var->getName();
88 obj["index"] = VarBit->getBitNum();
89 return std::move(obj);
91 } else if (auto *Dag = dyn_cast<DagInit>(&I)) {
92 obj["kind"] = "dag";
93 obj["operator"] = translateInit(*Dag->getOperator());
94 if (auto name = Dag->getName())
95 obj["name"] = name->getAsUnquotedString();
96 json::Array args;
97 for (unsigned i = 0, limit = Dag->getNumArgs(); i < limit; ++i) {
98 json::Array arg;
99 arg.push_back(translateInit(*Dag->getArg(i)));
100 if (auto argname = Dag->getArgName(i))
101 arg.push_back(argname->getAsUnquotedString());
102 else
103 arg.push_back(nullptr);
104 args.push_back(std::move(arg));
106 obj["args"] = std::move(args);
107 return std::move(obj);
110 // Final fallback: anything that gets past here is simply given a
111 // kind field of 'complex', and the only other field is the standard
112 // 'printable' representation.
114 assert(!I.isConcrete());
115 obj["kind"] = "complex";
116 return std::move(obj);
119 void JSONEmitter::run(raw_ostream &OS) {
120 json::Object root;
122 root["!tablegen_json_version"] = 1;
124 // Prepare the arrays that will list the instances of every class.
125 // We mostly fill those in by iterating over the superclasses of
126 // each def, but we also want to ensure we store an empty list for a
127 // class with no instances at all, so we do a preliminary iteration
128 // over the classes, invoking std::map::operator[] to default-
129 // construct the array for each one.
130 std::map<std::string, json::Array> instance_lists;
131 for (const auto &C : Records.getClasses()) {
132 auto &Name = C.second->getNameInitAsString();
133 (void)instance_lists[Name];
136 // Main iteration over the defs.
137 for (const auto &D : Records.getDefs()) {
138 auto &Name = D.second->getNameInitAsString();
139 auto &Def = *D.second;
141 json::Object obj;
142 json::Array fields;
144 for (const RecordVal &RV : Def.getValues()) {
145 if (!Def.isTemplateArg(RV.getNameInit())) {
146 auto Name = RV.getNameInitAsString();
147 if (RV.isNonconcreteOK())
148 fields.push_back(Name);
149 obj[Name] = translateInit(*RV.getValue());
153 obj["!fields"] = std::move(fields);
155 json::Array superclasses;
156 for (const auto &SuperPair : Def.getSuperClasses())
157 superclasses.push_back(SuperPair.first->getNameInitAsString());
158 obj["!superclasses"] = std::move(superclasses);
160 obj["!name"] = Name;
161 obj["!anonymous"] = Def.isAnonymous();
163 root[Name] = std::move(obj);
165 // Add this def to the instance list for each of its superclasses.
166 for (const auto &SuperPair : Def.getSuperClasses()) {
167 auto SuperName = SuperPair.first->getNameInitAsString();
168 instance_lists[SuperName].push_back(Name);
172 // Make a JSON object from the std::map of instance lists.
173 json::Object instanceof;
174 for (auto kv: instance_lists)
175 instanceof[kv.first] = std::move(kv.second);
176 root["!instanceof"] = std::move(instanceof);
178 // Done. Write the output.
179 OS << json::Value(std::move(root)) << "\n";
182 namespace llvm {
184 void EmitJSON(RecordKeeper &RK, raw_ostream &OS) { JSONEmitter(RK).run(OS); }
185 } // end namespace llvm