53 #include <type_traits>
54 #include <unordered_map>
55 #include <unordered_set>
59 #ifdef ENABLE_TOSTRING_RAPIDJSON
60 #if __has_include(<rapidjson/document.h> )
61 #include <rapidjson/document.h>
62 #include <rapidjson/stringbuffer.h>
63 #include <rapidjson/writer.h>
65 #undefine ENABLE_TOSTRING_RAPIDJSON
69 #ifdef ENABLE_TOSTRING_LLVM
70 #if __has_include(<llvm/Support/raw_os_ostream.h> )
71 #include <llvm/IR/Value.h>
72 #include <llvm/Support/raw_os_ostream.h>
73 #include "clang/Driver/Job.h"
74 #include "llvm/IR/Function.h"
75 #include "llvm/IR/Module.h"
76 #include "llvm/Option/ArgList.h"
78 #undefine ENABLE_TOSTRING_LLVM
82 #ifdef ENABLE_TOSTRING_PTHREAD
90 #define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
93 std::lock_guard<std::mutex> print_lock(toString_PRINT_mutex); \
94 std::cout << std::hex \
95 << ((std::hash<std::thread::id>()(std::this_thread::get_id())) & 0xffff) \
96 << std::dec << " [" << __FILENAME__ << ":" << __func__ << "#" << __LINE__ \
97 << "]: " #__VA_ARGS__ "=" << ::toString(std::make_tuple(__VA_ARGS__)) \
102 template <
typename T>
104 std::stringstream stream;
107 stream << std::string(
typeid(
T).
name());
109 char* demangled = abi::__cxa_demangle(
typeid(
T).
name(), 0, 0, &status);
110 stream << std::string(demangled);
116 template <
typename T,
typename... Args>
118 std::stringstream stream;
121 stream << std::string(
typeid(v).
name());
123 char* demangled = abi::__cxa_demangle(
typeid(v).
name(), 0, 0, &status);
124 stream << std::string(demangled);
127 stream <<
"@0x" << std::hex << (uintptr_t)(reinterpret_cast<const void*>(v));
133 template <
typename T,
typename =
void>
135 template <
typename T>
141 template <
typename T,
typename =
void>
143 template <
typename T>
149 #ifdef ENABLE_TOSTRING_to_string
150 template <
typename T,
typename =
void>
151 struct has_to_string : std::false_type {};
152 template <
typename T>
153 struct has_to_string<
T, decltype(std::declval<T>().
to_string(), void())>
156 inline constexpr
bool has_to_string_v = has_to_string<T>::value;
159 #ifdef ENABLE_TOSTRING_str
160 template <
typename T,
typename =
void>
161 struct has_str : std::false_type {};
162 template <
typename T>
163 struct has_str<
T, decltype(std::declval<T>().str(), void())> : std::true_type {};
165 inline constexpr
bool has_str_v = has_str<T>::value;
168 template <
typename T,
typename =
void>
170 template <
typename T>
172 decltype(std::declval<T>().printTo(std::declval<std::ostream&>()),
173 void())> : std::true_type {};
179 template <
typename T>
181 if constexpr (std::is_same_v<T, std::string>) {
182 return "\"" + v +
"\"";
183 #ifdef ENABLE_TOSTRING_RAPIDJSON
184 }
else if constexpr (std::is_same_v<T, rapidjson::Value>) {
185 rapidjson::StringBuffer buffer;
186 rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
188 return buffer.GetString();
190 #ifdef ENABLE_TOSTRING_LLVM
191 }
else if constexpr (std::is_same_v<T, llvm::Module>) {
192 std::string type_str;
193 llvm::raw_string_ostream rso(type_str);
194 v.print(rso,
nullptr);
195 return "(" + rso.str() +
")";
196 }
else if constexpr (std::is_same_v<T, llvm::Function>) {
197 std::string type_str;
198 llvm::raw_string_ostream rso(type_str);
199 v.print(rso,
nullptr);
200 return "(" + rso.str() +
")";
201 }
else if constexpr (std::is_same_v<T, llvm::Value>) {
202 std::string type_str;
203 llvm::raw_string_ostream rso(type_str);
205 return "(" + rso.str() +
")";
206 }
else if constexpr (std::is_same_v<T, llvm::Argument>) {
207 std::string type_str;
208 llvm::raw_string_ostream rso(type_str);
210 return "(" + rso.str() +
")";
211 }
else if constexpr (std::is_same_v<T, llvm::Type>) {
212 std::string type_str;
213 llvm::raw_string_ostream rso(type_str);
215 return "(" + rso.str() +
")";
216 }
else if constexpr (std::is_same_v<T, llvm::Triple>) {
218 }
else if constexpr (std::is_same_v<T, llvm::opt::ArgStringList>) {
220 for (
unsigned i = 0; i < v.size(); i++) {
226 return "[" + r +
"]";
227 }
else if constexpr (std::is_same_v<T, llvm::opt::DerivedArgList>) {
229 for (
unsigned i = 0; i < v.getNumInputArgStrings(); i++) {
233 r += v.getArgString(i);
235 return "[" + r +
"]";
236 }
else if constexpr (std::is_same_v<T, clang::driver::JobList>) {
237 std::string type_str;
238 llvm::raw_string_ostream rso(type_str);
239 v.Print(rso,
nullptr,
true);
242 #ifdef ENABLE_TOSTRING_PTHREAD
243 }
else if constexpr (std::is_same_v<T, pthread_mutex_t>) {
249 return "{" + r +
"}";
251 }
else if constexpr (std::is_same_v<T, bool>) {
252 return v ?
"True" :
"False";
253 }
else if constexpr (std::is_arithmetic_v<T>) {
255 #ifdef ENABLE_TOSTRING_str
256 }
else if constexpr (has_str_v<T>) {
259 #ifdef ENABLE_TOSTRING_to_string
260 }
else if constexpr (has_to_string_v<T>) {
261 return v.to_string();
263 }
else if constexpr (has_toString_v<T>) {
265 }
else if constexpr (get_has_toString_v<T>) {
267 return (ptr == NULL ?
"NULL" :
"&" + ptr->toString());
268 }
else if constexpr (std::is_same_v<T, void*>) {
269 std::ostringstream ss;
270 ss << std::hex << (uintptr_t)v;
271 return "0x" + ss.str();
272 }
else if constexpr (std::is_same_v<
274 std::chrono::time_point<std::chrono::system_clock>>) {
275 std::string s(30,
'\0');
276 auto converted_v = (std::chrono::time_point<std::chrono::system_clock>)v;
277 std::time_t ts = std::chrono::system_clock::to_time_t(v);
278 std::strftime(&s[0], s.size(),
"%Y-%m-%d %H:%M:%S", std::localtime(&ts));
280 std::to_string((converted_v.time_since_epoch().count() / 1000) % 1000000);
281 }
else if constexpr (std::is_same_v<T, JoinType>) {
292 return "WINDOW_FUNCTION";
294 return "WINDOW_FUNCTION_FRAMING";
300 }
else if constexpr (std::is_pointer_v<T>) {
301 return (v == NULL ?
"NULL" :
"&" +
toString(*v));
302 }
else if constexpr (has_printTo_v<T>) {
303 std::ostringstream ss;
311 #ifdef ENABLE_TOSTRING_PTHREAD
312 template <
typename T>
314 if constexpr (std::is_same_v<T, std::mutex>) {
315 auto p = v.native_handle();
324 template <
typename T1,
typename T2>
329 template <
typename T>
331 auto result = std::string(
"[");
332 for (
size_t i = 0; i < v.size(); ++i) {
342 template <
typename T1,
typename T2>
343 std::string
toString(
const std::unordered_map<T1, T2>& v) {
344 auto result = std::string(
"{");
346 for (
const auto& p : v) {
357 template <
typename T1,
typename T2>
359 auto result = std::string(
"{");
361 for (
const auto& p : v) {
372 template <
typename T>
374 auto result = std::string(
"[");
376 for (
const auto& p : v) {
387 template <
typename T>
388 std::string
toString(
const std::unordered_set<T>& v) {
389 auto result = std::string(
"{");
391 for (
const auto& p : v) {
402 template <
typename T>
404 auto result = std::string(
"{");
406 for (
const auto& p : v) {
417 template <
typename... Ts,
size_t... Is>
419 const std::index_sequence<0, Is...>) {
420 return (
toString(std::get<0>(t)) + ... + (
", " +
toString(std::get<Is>(t))));
423 template <
typename...
T>
424 std::string
toStringImpl(
const std::tuple<>& t,
const std::index_sequence<>) {
428 template <
typename... Ts>
430 return "(" +
toStringImpl(t, std::index_sequence_for<Ts...>{}) +
")";
437 #endif // ifndef __CUDACC__
static std::mutex toString_PRINT_mutex
constexpr bool has_toString_v
std::string toStringImpl(const std::tuple< Ts...> &t, const std::index_sequence< 0, Is...>)
struct get_has_toString< T, decltype(std::declval< T >().get() -> void())> constexpr bool get_has_toString_v
std::string toString(const ExecutorDeviceType &device_type)
std::string typeName(const T *v)
constexpr bool has_printTo_v
Common Enum definitions for SQL processing.