CompilerSysY/include/common.h
2023-07-16 22:26:48 +08:00

92 lines
2.8 KiB
C++

#pragma once
#include "3rdparty/easylogging++.h"
#include "3rdparty/fmt/core.h"
#include <algorithm>
#include <any>
#include <bitset>
#include <cassert>
#include <exception>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <optional>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define uptr(type) std::unique_ptr<type>
#define sptr(type) std::shared_ptr<type>
#define wptr(type) std::weak_ptr<type>
template <typename DST, typename SRC>
inline sptr(DST) shared_cast(SRC src)
{
return std::dynamic_pointer_cast<DST>(src);
}
template <typename DST, typename SRC>
inline sptr(DST) strict_shared_cast(SRC src)
{
sptr(DST) dst = std::dynamic_pointer_cast<DST>(src);
assert(dst);
return dst;
}
#define STD_FIND(container, val) std::find(container.begin(), container.end(), val)
#define GETINDEX(container, val) std::distance(container.begin(), STD_FIND(container, val))
#define STD_FOUND(container, val) (STD_FIND(container, val) != container.end())
#define INSET(cont, val) (cont.find(val) != cont.end())
#define BEGINEND(cont) cont.begin(), cont.end()
#define INF (0x3f3f3f3f)
#define BTWN(v, l, r) (l <= v && v <= r)
#define panic(message) __assert_fail(message, __FILE__, __LINE__, __ASSERT_FUNCTION)
#define DEF_PTR_T(type) \
class type; \
typedef std::shared_ptr<type> type##Ptr_t
#define GET_MACRO(_1, _2, NAME, ...) NAME
#define _sysy_assert1(expr) assert(expr)
#define _sysy_assert2(expr, info) \
(static_cast<bool>(expr) ? void(0) : ([&]() { info }(), __assert_fail(#expr, __FILE__, __LINE__, __ASSERT_FUNCTION)))
#define sysy_assert(...) GET_MACRO(__VA_ARGS__, _sysy_assert2, _sysy_assert1)(__VA_ARGS__)
namespace CompSysY
{
template <typename T>
std::ostream &operator<<(typename std::enable_if<std::is_enum<T>::value, std::ostream>::type &stream, const T &e)
{
return stream << static_cast<typename std::underlying_type<T>::type>(e);
}
class GrammarException : public std::exception
{
public:
GrammarException() : message("Unknown Grammar Exception") {}
GrammarException(const std::string &what_arg) : message(what_arg){};
GrammarException(const char *what_arg) : message(what_arg){};
GrammarException(const char *filename, int line, const char *what_arg)
{
std::ostringstream ss;
ss << filename << ":" << line << ": "
<< "\x1b[031m" << what_arg << "\x1b[0m";
message = ss.str();
};
virtual const char *what() const noexcept
{
return message.c_str();
}
private:
std::string message;
};
} // namespace CompSysY