Jlm
Loading...
Searching...
No Matches
common.hpp
Go to the documentation of this file.
1/*
2 * Copyright 2014 Nico Reißmann <nico.reissmann@gmail.com>
3 * See COPYING for terms of redistribution.
4 */
5
6#ifndef JLM_UTIL_COMMON_HPP
7#define JLM_UTIL_COMMON_HPP
8
9#include <cassert>
10#include <iostream>
11#include <stdexcept>
12
13#ifdef JLM_ENABLE_ASSERTS
14#define JLM_ASSERT(x) assert(x)
15#else
16#define JLM_ASSERT(x) \
17 do \
18 { \
19 } while (0 && (x))
20#endif
21
22#define JLM_NORETURN __attribute__((noreturn))
23
24namespace jlm
25{
26
27JLM_NORETURN static inline void
28unreachable(const char * msg, const char * file, unsigned line)
29{
30 if (msg)
31 std::cerr << msg << "\n";
32
33 std::cerr << "UNREACHABLE executed";
34
35 if (file)
36 std::cerr << " at " << file << ":" << line << "\n";
37
38 abort();
39}
40
41}
42
43#define JLM_UNREACHABLE(msg) jlm::unreachable(msg, __FILE__, __LINE__)
44
45namespace jlm::util
46{
47
48template<class To, class From>
49static inline To *
50assertedCast(From * value)
51{
52 JLM_ASSERT(dynamic_cast<To *>(value));
53 return static_cast<To *>(value);
54}
55
56class Error : public std::runtime_error
57{
58public:
59 ~Error() noexcept override;
60
61 explicit Error(const std::string & msg)
62 : std::runtime_error(msg)
63 {}
64};
65
66class TypeError : public Error
67{
68public:
69 ~TypeError() noexcept override;
70
71 TypeError(const std::string & expected_type, const std::string & received_type)
72 : Error("Type error - expected : " + expected_type + ", received : " + received_type)
73 {}
74};
75
76}
77
78#endif
std::int64_t expected
~Error() noexcept override
~TypeError() noexcept override
#define JLM_NORETURN
Definition common.hpp:22
#define JLM_ASSERT(x)
Definition common.hpp:16
static To * assertedCast(From *value)
Definition common.hpp:50
static JLM_NORETURN void unreachable(const char *msg, const char *file, unsigned line)
Definition common.hpp:28