37 std::unordered_set<int> node_refs;
49 static small test(
decltype(&C::toJSON));
54 static const bool value =
sizeof(test<T>(0)) ==
sizeof(
char);
60 explicit JSONGenerator(std::ostream &out,
bool dumpSourceInfo =
false)
61 : out(out), dumpSourceInfo(dumpSourceInfo) {}
67 out << std::endl << ++indent;
69 for (
size_t i = 1; i < v.size(); i++) {
70 out <<
"," << std::endl << indent;
73 out << std::endl << --indent;
79 void generate(
const std::vector<T> &v) {
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;
93 template <
typename T,
typename U>
94 void generate(
const std::pair<T, U> &v) {
96 out <<
"{" << std::endl;
98 out << std::endl << --indent <<
"}";
101 template <
typename T,
typename U>
102 void toJSON(
const std::pair<T, U> &v) {
103 out << indent <<
"\"first\" : ";
105 out <<
"," << std::endl << indent <<
"\"second\" : ";
109 template <
typename T>
110 void generate(
const std::optional<T> &v) {
112 out <<
"{ \"valid\" : false }";
115 out <<
"{" << std::endl << ++indent;
116 out <<
"\"valid\" : true," << std::endl;
117 out <<
"\"value\" : ";
119 out << std::endl << --indent <<
"}";
122 template <
typename T>
123 void generate(
const std::set<T> &v) {
124 out <<
"[" << std::endl;
129 for (it++; it != v.end(); ++it) {
130 out <<
"," << std::endl << indent;
133 out << std::endl << --indent;
138 template <
typename T>
140 out <<
"[" << std::endl;
145 for (it++; it != v.end(); ++it) {
146 out <<
"," << std::endl << indent;
149 out << std::endl << --indent;
154 template <
typename K,
typename V>
155 void generate(
const std::map<K, V> &v) {
156 out <<
"[" << std::endl;
161 for (it++; it != v.end(); ++it) {
162 out <<
"," << std::endl << indent;
165 out << std::endl << --indent;
170 template <
typename K,
typename V>
172 out <<
"[" << std::endl;
177 for (it++; it != v.end(); ++it) {
178 out <<
"," << std::endl << indent;
181 out << std::endl << --indent;
186 void generate(
bool v) { out << (v ?
"true" :
"false"); }
187 template <
typename T>
188 typename std::enable_if<std::is_integral<T>::value>::type generate(T v) {
189 out << std::to_string(v);
191 void generate(
double v) { out << std::to_string(v); }
192 template <
typename T>
193 typename std::enable_if<std::is_same<T, big_int>::value>::type generate(
const T &v) {
204 template <
typename T>
205 typename std::enable_if<std::is_same<T, LTBitMatrix>::value || std::is_enum<T>::value>::type
207 out <<
"\"" << v <<
"\"";
210 void generate(
const bitvec &v) { out <<
"\"" << v <<
"\""; }
212 void generate(
const match_t &v) {
213 out <<
"{" << std::endl
214 << (indent + 1) <<
"\"word0\" : " << v.word0 <<
"," << std::endl
215 << (indent + 1) <<
"\"word1\" : " << v.word1 << std::endl
219 template <
typename T>
220 typename std::enable_if<has_toJSON<T>::value && !std::is_base_of<IR::Node, T>::value>::type
221 generate(
const T &v) {
223 out <<
"{" << std::endl;
225 out << std::endl << --indent <<
"}";
229 out <<
"{" << std::endl;
231 if (node_refs.find(v.id) != node_refs.end()) {
232 out << indent <<
"\"Node_ID\" : " << v.id;
234 node_refs.insert(v.id);
236 if (dumpSourceInfo) {
237 v.sourceInfoToJSON(*
this);
240 out << std::endl << --indent <<
"}";
243 template <
typename T>
244 typename std::enable_if<std::is_pointer<T>::value &&
245 has_toJSON<typename std::remove_pointer<T>::type>::value>::type
253 template <
typename T,
size_t N>
254 void generate(
const T (&v)[N]) {
257 out << std::endl << ++indent;
259 for (
size_t i = 1; i < N; i++) {
260 out <<
"," << std::endl << indent;
263 out << std::endl << --indent;
280 JSONGenerator &operator<<(std::ostream &(*fn)(std::ostream &)) {
284 template <
typename T>
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24