Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Roblox
GitHub Repository: Roblox/luau
Path: blob/master/Common/include/Luau/Common.h
2727 views
1
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
2
#pragma once
3
4
// Compiler codegen control macros
5
#ifdef _MSC_VER
6
#define LUAU_NORETURN __declspec(noreturn)
7
#define LUAU_NOINLINE __declspec(noinline)
8
#define LUAU_FORCEINLINE __forceinline
9
#define LUAU_LIKELY(x) x
10
#define LUAU_UNLIKELY(x) x
11
#define LUAU_UNREACHABLE() __assume(false)
12
#define LUAU_DEBUGBREAK() __debugbreak()
13
#else
14
#define LUAU_NORETURN __attribute__((__noreturn__))
15
#define LUAU_NOINLINE __attribute__((noinline))
16
#define LUAU_FORCEINLINE inline __attribute__((always_inline))
17
#define LUAU_LIKELY(x) __builtin_expect(x, 1)
18
#define LUAU_UNLIKELY(x) __builtin_expect(x, 0)
19
#define LUAU_UNREACHABLE() __builtin_unreachable()
20
#define LUAU_DEBUGBREAK() __builtin_trap()
21
#endif
22
23
// LUAU_FALLTHROUGH is a C++11-compatible alternative to [[fallthrough]] for use in the VM library
24
#if defined(__clang__) && defined(__has_warning)
25
#if __has_feature(cxx_attributes) && __has_warning("-Wimplicit-fallthrough")
26
#define LUAU_FALLTHROUGH [[clang::fallthrough]]
27
#else
28
#define LUAU_FALLTHROUGH
29
#endif
30
#elif defined(__GNUC__) && __GNUC__ >= 7
31
#define LUAU_FALLTHROUGH [[gnu::fallthrough]]
32
#else
33
#define LUAU_FALLTHROUGH
34
#endif
35
36
#if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
37
#define LUAU_BIG_ENDIAN
38
#endif
39
40
namespace Luau
41
{
42
43
using AssertHandler = int (*)(const char* expression, const char* file, int line, const char* function);
44
45
inline AssertHandler& assertHandler()
46
{
47
static AssertHandler handler = nullptr;
48
return handler;
49
}
50
51
// We want 'inline' to correctly link this function declared in the header
52
// But we also want to prevent compiler from inlining this function when optimization and assertions are enabled together
53
// Reason for that is that compilation times can increase significantly in such a configuration
54
LUAU_NOINLINE inline int assertCallHandler(const char* expression, const char* file, int line, const char* function)
55
{
56
if (AssertHandler handler = assertHandler())
57
return handler(expression, file, line, function);
58
59
return 1;
60
}
61
62
} // namespace Luau
63
64
#if !defined(NDEBUG) || defined(LUAU_ENABLE_ASSERT)
65
#define LUAU_ASSERT(expr) ((void)(!!(expr) || (Luau::assertCallHandler(#expr, __FILE__, __LINE__, __FUNCTION__) && (LUAU_DEBUGBREAK(), 0))))
66
#define LUAU_ASSERTENABLED
67
#else
68
#define LUAU_ASSERT(expr) (void)sizeof(!!(expr))
69
#endif
70
71
namespace Luau
72
{
73
74
template<typename T>
75
struct FValue
76
{
77
static FValue* list;
78
79
T value;
80
bool dynamic;
81
const char* name;
82
FValue* next;
83
84
FValue(const char* name, T def, bool dynamic)
85
: value(def)
86
, dynamic(dynamic)
87
, name(name)
88
, next(list)
89
{
90
list = this;
91
}
92
93
LUAU_FORCEINLINE operator T() const
94
{
95
return value;
96
}
97
};
98
99
template<typename T>
100
FValue<T>* FValue<T>::list = nullptr;
101
102
} // namespace Luau
103
104
#define LUAU_FASTFLAG(flag) \
105
namespace FFlag \
106
{ \
107
extern Luau::FValue<bool> flag; \
108
}
109
#define LUAU_FASTFLAGVARIABLE(flag) \
110
namespace FFlag \
111
{ \
112
Luau::FValue<bool> flag(#flag, false, false); \
113
}
114
#define LUAU_FASTINT(flag) \
115
namespace FInt \
116
{ \
117
extern Luau::FValue<int> flag; \
118
}
119
#define LUAU_FASTINTVARIABLE(flag, def) \
120
namespace FInt \
121
{ \
122
Luau::FValue<int> flag(#flag, def, false); \
123
}
124
125
#define LUAU_DYNAMIC_FASTFLAG(flag) \
126
namespace DFFlag \
127
{ \
128
extern Luau::FValue<bool> flag; \
129
}
130
#define LUAU_DYNAMIC_FASTFLAGVARIABLE(flag, def) \
131
namespace DFFlag \
132
{ \
133
Luau::FValue<bool> flag(#flag, def, true); \
134
}
135
#define LUAU_DYNAMIC_FASTINT(flag) \
136
namespace DFInt \
137
{ \
138
extern Luau::FValue<int> flag; \
139
}
140
#define LUAU_DYNAMIC_FASTINTVARIABLE(flag, def) \
141
namespace DFInt \
142
{ \
143
Luau::FValue<int> flag(#flag, def, true); \
144
}
145
146
#if defined(__GNUC__)
147
#define LUAU_PRINTF_ATTR(fmt, arg) __attribute__((format(printf, fmt, arg)))
148
#else
149
#define LUAU_PRINTF_ATTR(fmt, arg)
150
#endif
151
152