35 std::unordered_set<int> node_refs;
47 static small test(
decltype(&C::toJSON));
52 static const bool value =
sizeof(test<T>(0)) ==
sizeof(
char);
58 explicit JSONGenerator(std::ostream &out,
bool dumpSourceInfo =
false)
59 : out(out), dumpSourceInfo(dumpSourceInfo) {}
65 out << std::endl << ++indent;
67 for (
size_t i = 1; i < v.size(); i++) {
68 out <<
"," << std::endl << indent;
71 out << std::endl << --indent;
77 void generate(
const std::vector<T> &v) {
80 out << std::endl << ++indent;
82 for (
size_t i = 1; i < v.size(); i++) {
83 out <<
"," << std::endl << indent;
86 out << std::endl << --indent;
91 template <
typename T,
typename U>
92 void generate(
const std::pair<T, U> &v) {
94 out <<
"{" << std::endl;
96 out << std::endl << --indent <<
"}";
99 template <
typename T,
typename U>
100 void toJSON(
const std::pair<T, U> &v) {
101 out << indent <<
"\"first\" : ";
103 out <<
"," << std::endl << indent <<
"\"second\" : ";
107 template <
typename T>
108 void generate(
const std::optional<T> &v) {
110 out <<
"{ \"valid\" : false }";
113 out <<
"{" << std::endl << ++indent;
114 out <<
"\"valid\" : true," << std::endl;
115 out <<
"\"value\" : ";
117 out << std::endl << --indent <<
"}";
120 template <
typename T>
121 void generate(
const std::set<T> &v) {
122 out <<
"[" << std::endl;
127 for (it++; it != v.end(); ++it) {
128 out <<
"," << std::endl << indent;
131 out << std::endl << --indent;
136 template <
typename T>
138 out <<
"[" << std::endl;
143 for (it++; it != v.end(); ++it) {
144 out <<
"," << std::endl << indent;
147 out << std::endl << --indent;
152 template <
typename K,
typename V>
153 void generate(
const std::map<K, V> &v) {
154 out <<
"[" << std::endl;
159 for (it++; it != v.end(); ++it) {
160 out <<
"," << std::endl << indent;
163 out << std::endl << --indent;
168 template <
typename K,
typename V>
170 out <<
"[" << std::endl;
175 for (it++; it != v.end(); ++it) {
176 out <<
"," << std::endl << indent;
179 out << std::endl << --indent;
184 void generate(
bool v) { out << (v ?
"true" :
"false"); }
185 template <
typename T>
186 typename std::enable_if<std::is_integral<T>::value>::type generate(T v) {
187 out << std::to_string(v);
189 void generate(
double v) { out << std::to_string(v); }
190 template <
typename T>
191 typename std::enable_if<std::is_same<T, big_int>::value>::type generate(
const T &v) {
202 template <
typename T>
203 typename std::enable_if<std::is_same<T, LTBitMatrix>::value || std::is_enum<T>::value>::type
205 out <<
"\"" << v <<
"\"";
208 void generate(
const bitvec &v) { out <<
"\"" << v <<
"\""; }
210 void generate(
const match_t &v) {
211 out <<
"{" << std::endl
212 << (indent + 1) <<
"\"word0\" : " << v.word0 <<
"," << std::endl
213 << (indent + 1) <<
"\"word1\" : " << v.word1 << std::endl
217 template <
typename T>
218 typename std::enable_if<has_toJSON<T>::value && !std::is_base_of<IR::Node, T>::value>::type
219 generate(
const T &v) {
221 out <<
"{" << std::endl;
223 out << std::endl << --indent <<
"}";
227 out <<
"{" << std::endl;
229 if (node_refs.find(v.id) != node_refs.end()) {
230 out << indent <<
"\"Node_ID\" : " << v.id;
232 node_refs.insert(v.id);
234 if (dumpSourceInfo) {
235 v.sourceInfoToJSON(*
this);
238 out << std::endl << --indent <<
"}";
241 template <
typename T>
242 typename std::enable_if<std::is_pointer<T>::value &&
243 has_toJSON<typename std::remove_pointer<T>::type>::value>::type
251 template <
typename T,
size_t N>
252 void generate(
const T (&v)[N]) {
255 out << std::endl << ++indent;
257 for (
size_t i = 1; i < N; i++) {
258 out <<
"," << std::endl << indent;
261 out << std::endl << --indent;
278 JSONGenerator &operator<<(std::ostream &(*fn)(std::ostream &)) {
282 template <
typename T>