OLD | NEW |
1 //===- subzero/src/IceBuildDefs.h - Translator build defines ----*- C++ -*-===// | 1 //===- subzero/src/IceBuildDefs.h - Translator build defines ----*- C++ -*-===// |
2 // | 2 // |
3 // The Subzero Code Generator | 3 // The Subzero Code Generator |
4 // | 4 // |
5 // This file is distributed under the University of Illinois Open Source | 5 // This file is distributed under the University of Illinois Open Source |
6 // License. See LICENSE.TXT for details. | 6 // License. See LICENSE.TXT for details. |
7 // | 7 // |
8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
9 /// | 9 /// |
10 /// \file | 10 /// \file |
11 /// \brief Defines constexpr functions to query various #define values. | 11 /// \brief Define the Ice::BuildDefs namespace |
12 /// | |
13 //===----------------------------------------------------------------------===// | 12 //===----------------------------------------------------------------------===// |
14 | 13 |
15 #ifndef SUBZERO_SRC_ICEBUILDDEFS_H | 14 #ifndef SUBZERO_SRC_ICEBUILDDEFS_H |
16 #define SUBZERO_SRC_ICEBUILDDEFS_H | 15 #define SUBZERO_SRC_ICEBUILDDEFS_H |
17 | 16 |
18 namespace Ice { | 17 namespace Ice { |
| 18 /// \brief Defines constexpr functions that express various Subzero build |
| 19 /// system defined values. |
| 20 /// |
| 21 /// These resulting constexpr functions allow code to in effect be |
| 22 /// conditionally compiled without having to do this using the older C++ |
| 23 /// preprocessor solution. |
| 24 |
| 25 /** \verbatim |
| 26 |
| 27 For example whenever the value of FEATURE_SUPPORTED is needed, instead |
| 28 of (except in these constexpr functions): |
| 29 |
| 30 #if FEATURE_SUPPORTED ... |
| 31 ... |
| 32 #endif |
| 33 |
| 34 We can have: |
| 35 |
| 36 namespace Ice { |
| 37 namespace BuildDefs { |
| 38 |
| 39 // Use this form when FEATURE_SUPPORTED is guaranteed to be defined on the |
| 40 // C++ compiler command line as 0 or 1. |
| 41 constexpr bool hasFeature() { return FEATURE_SUPPORTED; } |
| 42 |
| 43 or |
| 44 |
| 45 // Use this form when FEATURE_SUPPORTED may not necessarily be defined on |
| 46 // the C++ compiler command line. |
| 47 constexpr bool hasFeature() { |
| 48 #if FEATURE_SUPPORTED |
| 49 return true; |
| 50 #else // !FEATURE_SUPPORTED |
| 51 return false; |
| 52 #endif // !FEATURE_SUPPORTED |
| 53 } |
| 54 |
| 55 ...} // end of namespace BuildDefs |
| 56 } // end of namespace Ice |
| 57 |
| 58 |
| 59 And later in the code: |
| 60 |
| 61 if (Ice::BuildDefs::hasFeature() { |
| 62 ... |
| 63 } |
| 64 |
| 65 \endverbatim |
| 66 |
| 67 Since hasFeature() returns a constexpr, an optimizing compiler will know to |
| 68 keep or discard the above fragment. In addition, the code will always be |
| 69 looked at by the compiler which eliminates the problem with defines in that |
| 70 if you don't build that variant, you don't even know if the code would |
| 71 compile unless you build with that variant. |
| 72 |
| 73 **/ |
| 74 |
| 75 |
19 namespace BuildDefs { | 76 namespace BuildDefs { |
20 | 77 |
21 // The ALLOW_* etc. symbols must be #defined to zero or non-zero. | 78 // The ALLOW_* etc. symbols must be #defined to zero or non-zero. |
| 79 /// Return true if ALLOW_DISABLE_IR_GEN is defined as a non-zero value |
22 constexpr bool disableIrGen() { return ALLOW_DISABLE_IR_GEN; } | 80 constexpr bool disableIrGen() { return ALLOW_DISABLE_IR_GEN; } |
| 81 /// Return true if ALLOW_DUMP is defined as a non-zero value |
23 constexpr bool dump() { return ALLOW_DUMP; } | 82 constexpr bool dump() { return ALLOW_DUMP; } |
| 83 /// Return true if ALLOW_LLVM_CL is defined as a non-zero value |
24 constexpr bool llvmCl() { return ALLOW_LLVM_CL; } | 84 constexpr bool llvmCl() { return ALLOW_LLVM_CL; } |
| 85 /// Return true if ALLOW_LLVM_IR is defined as a non-zero value |
25 constexpr bool llvmIr() { return ALLOW_LLVM_IR; } | 86 constexpr bool llvmIr() { return ALLOW_LLVM_IR; } |
| 87 /// Return true if ALLOW_LLVM_IR_AS_INPUT is defined as a non-zero value |
26 constexpr bool llvmIrAsInput() { return ALLOW_LLVM_IR_AS_INPUT; } | 88 constexpr bool llvmIrAsInput() { return ALLOW_LLVM_IR_AS_INPUT; } |
| 89 /// Return true if ALLOW_MINIMAL_BUILD is defined as a non-zero value |
27 constexpr bool minimal() { return ALLOW_MINIMAL_BUILD; } | 90 constexpr bool minimal() { return ALLOW_MINIMAL_BUILD; } |
28 | 91 |
29 // NDEBUG can be undefined, or defined to something arbitrary. | 92 /// Return true if NDEBUG is defined |
30 constexpr bool asserts() { | 93 constexpr bool asserts() { |
31 #ifdef NDEBUG | 94 #ifdef NDEBUG |
32 return false; | 95 return false; |
33 #else // !NDEBUG | 96 #else // !NDEBUG |
34 return true; | 97 return true; |
35 #endif // !NDEBUG | 98 #endif // !NDEBUG |
36 } | 99 } |
37 | 100 |
38 // PNACL_BROWSER_TRANSLATOR can be undefined, or defined to something non-zero | 101 /// Return true if PNACL_BROWSER_TRANSLATOR is defined |
39 // to indicate a browser-based translator. | |
40 constexpr bool browser() { | 102 constexpr bool browser() { |
41 #if PNACL_BROWSER_TRANSLATOR | 103 #if PNACL_BROWSER_TRANSLATOR |
42 return true; | 104 return true; |
43 #else // !PNACL_BROWSER_TRANSLATOR | 105 #else // !PNACL_BROWSER_TRANSLATOR |
44 return false; | 106 return false; |
45 #endif // !PNACL_BROWSER_TRANSLATOR | 107 #endif // !PNACL_BROWSER_TRANSLATOR |
46 } | 108 } |
47 | 109 |
48 // ALLOW_EXTRA_VALIDATION can be undefined, or defined to something non-zero. | 110 /// Return true if ALLOW_EXTRA_VALIDATION is defined |
49 constexpr bool extraValidation() { | 111 constexpr bool extraValidation() { |
50 #if ALLOW_EXTRA_VALIDATION | 112 #if ALLOW_EXTRA_VALIDATION |
51 return true; | 113 return true; |
52 #else // !ALLOW_EXTRA_VALIDATION | 114 #else // !ALLOW_EXTRA_VALIDATION |
53 return false; | 115 return false; |
54 #endif // !ALLOW_EXTRA_VALIDATION | 116 #endif // !ALLOW_EXTRA_VALIDATION |
55 } | 117 } |
56 | 118 |
57 } // end of namespace BuildDefs | 119 } // end of namespace BuildDefs |
58 } // end of namespace Ice | 120 } // end of namespace Ice |
59 | 121 |
60 #endif // SUBZERO_SRC_ICEBUILDDEFS_H | 122 #endif // SUBZERO_SRC_ICEBUILDDEFS_H |
OLD | NEW |