38 std::unordered_set<int> node_refs;
50 static small test(
decltype(&C::toJSON));
55 static const bool value =
sizeof(test<T>(0)) ==
sizeof(
char);
58 struct variant_generator {
62 variant_generator(std::ostream &out,
JSONGenerator &generator)
63 : out(out), generator(generator) {}
66 void operator()(
const T &value)
const {
67 out << R
"("value" : )";
68 generator.generate(value);
75 explicit JSONGenerator(std::ostream &out,
bool dumpSourceInfo =
false)
76 : out(out), dumpSourceInfo(dumpSourceInfo) {}
82 out << std::endl << ++indent;
84 for (
size_t i = 1; i < v.size(); i++) {
85 out <<
"," << std::endl << indent;
88 out << std::endl << --indent;
94 void generate(
const std::vector<T> &v) {
97 out << std::endl << ++indent;
99 for (
size_t i = 1; i < v.size(); i++) {
100 out <<
"," << std::endl << indent;
103 out << std::endl << --indent;
108 template <
typename T,
typename U>
109 void generate(
const std::pair<T, U> &v) {
111 out <<
"{" << std::endl;
113 out << std::endl << --indent <<
"}";
116 template <
typename T,
typename U>
117 void toJSON(
const std::pair<T, U> &v) {
118 out << indent <<
"\"first\" : ";
120 out <<
"," << std::endl << indent <<
"\"second\" : ";
124 template <
typename T>
125 void generate(
const std::optional<T> &v) {
127 out <<
"{ \"valid\" : false }";
130 out <<
"{" << std::endl << ++indent;
131 out <<
"\"valid\" : true," << std::endl;
132 out <<
"\"value\" : ";
134 out << std::endl << --indent <<
"}";
137 template <
typename T>
138 void generate(
const std::set<T> &v) {
139 out <<
"[" << std::endl;
144 for (it++; it != v.end(); ++it) {
145 out <<
"," << std::endl << indent;
148 out << std::endl << --indent;
153 template <
typename T>
155 out <<
"[" << std::endl;
160 for (it++; it != v.end(); ++it) {
161 out <<
"," << std::endl << indent;
164 out << std::endl << --indent;
169 template <
typename K,
typename V>
170 void generate(
const std::map<K, V> &v) {
171 out <<
"[" << std::endl;
176 for (it++; it != v.end(); ++it) {
177 out <<
"," << std::endl << indent;
180 out << std::endl << --indent;
185 template <
typename K,
typename V>
187 out <<
"[" << std::endl;
192 for (it++; it != v.end(); ++it) {
193 out <<
"," << std::endl << indent;
196 out << std::endl << --indent;
201 template <
class... Types>
202 void generate(
const std::variant<Types...> &v) {
203 out <<
"{" << std::endl << ++indent;
204 out << R
"("variant_index" : )" << v.index() << "," << std::endl << indent;
205 variant_generator generator(out, *
this);
206 std::visit(generator, v);
207 out << std::endl << --indent <<
"}";
210 void generate(
bool v) { out << (v ?
"true" :
"false"); }
211 template <
typename T>
212 std::enable_if_t<std::is_integral_v<T>> generate(T v) {
213 out << std::to_string(v);
215 void generate(
double v) { out << std::to_string(v); }
216 template <
typename T>
217 std::enable_if_t<std::is_same_v<T, big_int>> generate(
const T &v) {
228 template <
typename T>
229 std::enable_if_t<std::is_same_v<T, LTBitMatrix> || std::is_enum_v<T>> generate(T v) {
230 out <<
"\"" << v <<
"\"";
233 void generate(
const bitvec &v) { out <<
"\"" << v <<
"\""; }
235 void generate(
const match_t &v) {
236 out <<
"{" << std::endl
237 << (indent + 1) <<
"\"word0\" : " << v.word0 <<
"," << std::endl
238 << (indent + 1) <<
"\"word1\" : " << v.word1 << std::endl
242 template <
typename T>
243 std::enable_if_t<has_toJSON<T>::value && !std::is_base_of_v<IR::Node, T>> generate(
const T &v) {
245 out <<
"{" << std::endl;
247 out << std::endl << --indent <<
"}";
251 out <<
"{" << std::endl;
253 if (node_refs.find(v.id) != node_refs.end()) {
254 out << indent <<
"\"Node_ID\" : " << v.id;
256 node_refs.insert(v.id);
258 if (dumpSourceInfo) {
259 v.sourceInfoToJSON(*
this);
262 out << std::endl << --indent <<
"}";
265 template <
typename T>
266 std::enable_if_t<std::is_pointer_v<T> && has_toJSON<std::remove_pointer_t<T>>::value> generate(
274 template <
typename T,
size_t N>
275 void generate(
const T (&v)[N]) {
278 out << std::endl << ++indent;
280 for (
size_t i = 1; i < N; i++) {
281 out <<
"," << std::endl << indent;
284 out << std::endl << --indent;
301 JSONGenerator &operator<<(std::ostream &(*fn)(std::ostream &)) {
305 template <
typename T>
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24