1 /*
2  * Copyright (c) 2015 Andrew Kelley
3  *
4  * This file is part of zig, which is MIT licensed.
5  * See http://opensource.org/licenses/MIT
6  */
7 
8 #ifndef ZIG_UTIL_BASE_HPP
9 #define ZIG_UTIL_BASE_HPP
10 
11 #include <assert.h>
12 
13 #if defined(_MSC_VER)
14 
15 #define ATTRIBUTE_COLD __declspec(noinline)
16 #define ATTRIBUTE_PRINTF(a, b)
17 #define ATTRIBUTE_RETURNS_NOALIAS __declspec(restrict)
18 #define ATTRIBUTE_NORETURN __declspec(noreturn)
19 #define ATTRIBUTE_MUST_USE
20 
21 #define BREAKPOINT __debugbreak()
22 
23 #else
24 
25 #define ATTRIBUTE_COLD         __attribute__((cold))
26 #define ATTRIBUTE_PRINTF(a, b) __attribute__((format(printf, a, b)))
27 #define ATTRIBUTE_RETURNS_NOALIAS __attribute__((__malloc__))
28 #define ATTRIBUTE_NORETURN __attribute__((noreturn))
29 #define ATTRIBUTE_MUST_USE __attribute__((warn_unused_result))
30 
31 #if defined(__MINGW32__) || defined(__MINGW64__)
32 #define BREAKPOINT __debugbreak()
33 #elif defined(__i386__) || defined(__x86_64__)
34 #define BREAKPOINT __asm__ volatile("int $0x03");
35 #elif defined(__clang__)
36 #define BREAKPOINT __builtin_debugtrap()
37 #elif defined(__GNUC__)
38 #define BREAKPOINT __builtin_trap()
39 #else
40 #include <signal.h>
41 #define BREAKPOINT raise(SIGTRAP)
42 #endif
43 
44 #endif
45 
46 ATTRIBUTE_COLD
47 ATTRIBUTE_NORETURN
48 ATTRIBUTE_PRINTF(1, 2)
49 void zig_panic(const char *format, ...);
50 
zig_assert(bool ok,const char * file,int line,const char * func)51 static inline void zig_assert(bool ok, const char *file, int line, const char *func) {
52     if (!ok) {
53         zig_panic("Assertion failed at %s:%d in %s. This is a bug in the Zig compiler.", file, line, func);
54     }
55 }
56 
57 #ifdef _WIN32
58 #define __func__ __FUNCTION__
59 #endif
60 
61 #define zig_unreachable() zig_panic("Unreachable at %s:%d in %s. This is a bug in the Zig compiler.", __FILE__, __LINE__, __func__)
62 
63 // Assertions in stage1 are always on, and they call zig @panic.
64 #undef assert
65 #define assert(ok) zig_assert(ok, __FILE__, __LINE__, __func__)
66 
67 #if defined(_MSC_VER)
68 #define ZIG_FALLTHROUGH
69 #elif defined(__clang__)
70 #define ZIG_FALLTHROUGH [[clang::fallthrough]]
71 #elif defined(__GNUC__) && __GNUC__ >= 7
72 #define ZIG_FALLTHROUGH __attribute__((fallthrough))
73 #else
74 #define ZIG_FALLTHROUGH
75 #endif
76 
77 #endif
78