| OLD | NEW |
| 1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef V8_BASE_MACROS_H_ | 5 #ifndef V8_BASE_MACROS_H_ |
| 6 #define V8_BASE_MACROS_H_ | 6 #define V8_BASE_MACROS_H_ |
| 7 | 7 |
| 8 #include <cstring> | 8 #include <cstring> |
| 9 | 9 |
| 10 #include "include/v8stdint.h" | 10 #include "include/v8stdint.h" |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 // Under C++11, just use static_assert. | 123 // Under C++11, just use static_assert. |
| 124 #define COMPILE_ASSERT(expr, msg) static_assert(expr, #msg) | 124 #define COMPILE_ASSERT(expr, msg) static_assert(expr, #msg) |
| 125 | 125 |
| 126 #else | 126 #else |
| 127 | 127 |
| 128 template <bool> | 128 template <bool> |
| 129 struct CompileAssert {}; | 129 struct CompileAssert {}; |
| 130 | 130 |
| 131 #define COMPILE_ASSERT(expr, msg) \ | 131 #define COMPILE_ASSERT(expr, msg) \ |
| 132 typedef CompileAssert<static_cast<bool>(expr)> \ | 132 typedef CompileAssert<static_cast<bool>(expr)> \ |
| 133 msg[static_cast<bool>(expr) ? 1 : -1] ALLOW_UNUSED | 133 msg[static_cast<bool>(expr) ? 1 : -1] ALLOW_UNUSED_TYPE |
| 134 | 134 |
| 135 // Implementation details of COMPILE_ASSERT: | 135 // Implementation details of COMPILE_ASSERT: |
| 136 // | 136 // |
| 137 // - COMPILE_ASSERT works by defining an array type that has -1 | 137 // - COMPILE_ASSERT works by defining an array type that has -1 |
| 138 // elements (and thus is invalid) when the expression is false. | 138 // elements (and thus is invalid) when the expression is false. |
| 139 // | 139 // |
| 140 // - The simpler definition | 140 // - The simpler definition |
| 141 // | 141 // |
| 142 // #define COMPILE_ASSERT(expr, msg) typedef char msg[(expr) ? 1 : -1] | 142 // #define COMPILE_ASSERT(expr, msg) typedef char msg[(expr) ? 1 : -1] |
| 143 // | 143 // |
| 144 // does not work, as gcc supports variable-length arrays whose sizes | 144 // does not work, as gcc supports variable-length arrays whose sizes |
| 145 // are determined at run-time (this is gcc's extension and not part | 145 // are determined at run-time (this is gcc's extension and not part |
| 146 // of the C++ standard). As a result, gcc fails to reject the | 146 // of the C++ standard). As a result, gcc fails to reject the |
| 147 // following code with the simple definition: | 147 // following code with the simple definition: |
| 148 // | 148 // |
| 149 // int foo; | 149 // int foo; |
| 150 // COMPILE_ASSERT(foo, msg); // not supposed to compile as foo is | 150 // COMPILE_ASSERT(foo, msg); // not supposed to compile as foo is |
| 151 // // not a compile-time constant. | 151 // // not a compile-time constant. |
| 152 // | 152 // |
| 153 // - By using the type CompileAssert<(bool(expr))>, we ensures that | 153 // - By using the type CompileAssert<static_cast<bool>(expr)>, we ensure that |
| 154 // expr is a compile-time constant. (Template arguments must be | 154 // expr is a compile-time constant. (Template arguments must be |
| 155 // determined at compile-time.) | 155 // determined at compile-time.) |
| 156 // | 156 // |
| 157 // - The outer parentheses in CompileAssert<(bool(expr))> are necessary | 157 // - The array size is (static_cast<bool>(expr) ? 1 : -1), instead of simply |
| 158 // to work around a bug in gcc 3.4.4 and 4.0.1. If we had written | |
| 159 // | |
| 160 // CompileAssert<bool(expr)> | |
| 161 // | |
| 162 // instead, these compilers will refuse to compile | |
| 163 // | |
| 164 // COMPILE_ASSERT(5 > 0, some_message); | |
| 165 // | |
| 166 // (They seem to think the ">" in "5 > 0" marks the end of the | |
| 167 // template argument list.) | |
| 168 // | |
| 169 // - The array size is (bool(expr) ? 1 : -1), instead of simply | |
| 170 // | 158 // |
| 171 // ((expr) ? 1 : -1). | 159 // ((expr) ? 1 : -1). |
| 172 // | 160 // |
| 173 // This is to avoid running into a bug in MS VC 7.1, which | 161 // This is to avoid running into a bug in MS VC 7.1, which |
| 174 // causes ((0.0) ? 1 : -1) to incorrectly evaluate to 1. | 162 // causes ((0.0) ? 1 : -1) to incorrectly evaluate to 1. |
| 175 | 163 |
| 176 #endif | 164 #endif |
| 177 | 165 |
| 178 | 166 |
| 179 // bit_cast<Dest,Source> is a template function that implements the | 167 // bit_cast<Dest,Source> is a template function that implements the |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 304 #define SEMI_STATIC_JOIN_HELPER(a, b) a##b | 292 #define SEMI_STATIC_JOIN_HELPER(a, b) a##b |
| 305 // Causes an error during compilation of the condition is not | 293 // Causes an error during compilation of the condition is not |
| 306 // statically known to be true. It is formulated as a typedef so that | 294 // statically known to be true. It is formulated as a typedef so that |
| 307 // it can be used wherever a typedef can be used. Beware that this | 295 // it can be used wherever a typedef can be used. Beware that this |
| 308 // actually causes each use to introduce a new defined type with a | 296 // actually causes each use to introduce a new defined type with a |
| 309 // name depending on the source line. | 297 // name depending on the source line. |
| 310 template <int> class StaticAssertionHelper { }; | 298 template <int> class StaticAssertionHelper { }; |
| 311 #define STATIC_ASSERT(test) \ | 299 #define STATIC_ASSERT(test) \ |
| 312 typedef \ | 300 typedef \ |
| 313 StaticAssertionHelper<sizeof(StaticAssertion<static_cast<bool>((test))>)> \ | 301 StaticAssertionHelper<sizeof(StaticAssertion<static_cast<bool>((test))>)> \ |
| 314 SEMI_STATIC_JOIN(__StaticAssertTypedef__, __LINE__) ALLOW_UNUSED | 302 SEMI_STATIC_JOIN(__StaticAssertTypedef__, __LINE__) ALLOW_UNUSED_TYPE |
| 315 | 303 |
| 316 #endif | 304 #endif |
| 317 | 305 |
| 318 | 306 |
| 319 // The USE(x) template is used to silence C++ compiler warnings | 307 // The USE(x) template is used to silence C++ compiler warnings |
| 320 // issued for (yet) unused variables (typically parameters). | 308 // issued for (yet) unused variables (typically parameters). |
| 321 template <typename T> | 309 template <typename T> |
| 322 inline void USE(T) { } | 310 inline void USE(T) { } |
| 323 | 311 |
| 324 | 312 |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 402 } | 390 } |
| 403 | 391 |
| 404 | 392 |
| 405 // Return the smallest multiple of m which is >= x. | 393 // Return the smallest multiple of m which is >= x. |
| 406 template <typename T> | 394 template <typename T> |
| 407 inline T RoundUp(T x, intptr_t m) { | 395 inline T RoundUp(T x, intptr_t m) { |
| 408 return RoundDown<T>(static_cast<T>(x + m - 1), m); | 396 return RoundDown<T>(static_cast<T>(x + m - 1), m); |
| 409 } | 397 } |
| 410 | 398 |
| 411 #endif // V8_BASE_MACROS_H_ | 399 #endif // V8_BASE_MACROS_H_ |
| OLD | NEW |