|
| 1 | +#include <stdbool.h> |
1 | 2 | #include <stddef.h> |
2 | 3 |
|
| 4 | +struct S { |
| 5 | + int i; |
| 6 | +}; |
| 7 | + |
| 8 | +#define LITERAL_INT 0xFFFF |
| 9 | +#define LITERAL_BOOL true |
| 10 | +#define LITERAL_FLOAT 3.14 |
| 11 | +#define LITERAL_CHAR 'x' |
| 12 | +#define LITERAL_STR "hello" |
| 13 | +#define LITERAL_ARRAY {1, 2, 3} |
| 14 | +#define LITERAL_STRUCT ((struct S){.i = 5}) |
| 15 | + |
| 16 | +#define NESTED_INT LITERAL_INT |
| 17 | +#define NESTED_BOOL LITERAL_BOOL |
| 18 | +#define NESTED_FLOAT LITERAL_FLOAT |
| 19 | +#define NESTED_CHAR LITERAL_CHAR |
| 20 | +#define NESTED_STR LITERAL_STR |
| 21 | +#define NESTED_ARRAY LITERAL_ARRAY |
| 22 | +#define NESTED_STRUCT LITERAL_STRUCT |
| 23 | + |
| 24 | +#define INT_ARITHMETIC NESTED_INT + LITERAL_INT + 1 |
| 25 | +#define MIXED_ARITHMETIC LITERAL_INT + NESTED_FLOAT *LITERAL_CHAR - true |
| 26 | +#define PARENS (NESTED_INT * (LITERAL_CHAR + true)) |
| 27 | +#define PTR_ARITHMETIC (LITERAL_STR + 5) - 3 |
| 28 | +#define WIDENING_CAST ((unsigned long long)LITERAL_INT) |
| 29 | +#define NARROWING_CAST (char)LITERAL_INT |
| 30 | +#define CONVERSION_CAST ((double)LITERAL_INT) |
| 31 | +#define INDEXING NESTED_STR[(int)LITERAL_FLOAT] |
| 32 | +#define STR_CONCATENATION NESTED_STR " " LITERAL_STR " world" |
| 33 | +#define BUILTIN __builtin_clz(LITERAL_INT) |
| 34 | +#define REF_MACRO &INDEXING |
| 35 | +#define REF_LITERAL &LITERAL_STRUCT |
| 36 | +#define TERNARY LITERAL_BOOL ? 1 : 2 |
| 37 | +#define MEMBER LITERAL_STRUCT.i |
| 38 | + |
| 39 | +void local_muts() { |
| 40 | + int literal_int = LITERAL_INT; |
| 41 | + bool literal_bool = LITERAL_BOOL; |
| 42 | + float literal_float = LITERAL_FLOAT; |
| 43 | + char literal_char = LITERAL_CHAR; |
| 44 | + const char *literal_str_ptr = LITERAL_STR; |
| 45 | + char literal_str[] = LITERAL_STR; |
| 46 | + int literal_array[] = LITERAL_ARRAY; |
| 47 | + struct S literal_struct = LITERAL_STRUCT; |
| 48 | + |
| 49 | + int nested_int = NESTED_INT; |
| 50 | + bool nested_bool = NESTED_BOOL; |
| 51 | + float nested_float = NESTED_FLOAT; |
| 52 | + char nested_char = NESTED_CHAR; |
| 53 | + const char *nested_str_ptr = NESTED_STR; |
| 54 | + char nested_str[] = NESTED_STR; |
| 55 | + int nested_array[] = NESTED_ARRAY; |
| 56 | + struct S nested_struct = NESTED_STRUCT; |
| 57 | + |
| 58 | + int int_arithmetic = INT_ARITHMETIC; |
| 59 | + float mixed_arithmetic = MIXED_ARITHMETIC; |
| 60 | + int parens = PARENS; |
| 61 | + const char *ptr_arithmetic = PTR_ARITHMETIC; |
| 62 | + unsigned long long widening_cast = WIDENING_CAST; |
| 63 | + char narrowing_cast = NARROWING_CAST; |
| 64 | + double conversion_cast = CONVERSION_CAST; |
| 65 | + char indexing = INDEXING; |
| 66 | + const char *str_concatenation_ptr = STR_CONCATENATION; |
| 67 | + char str_concatenation[] = STR_CONCATENATION; |
| 68 | + const char *ref_indexing = REF_MACRO; |
| 69 | + const struct S *ref_struct = REF_LITERAL; |
| 70 | + int ternary = TERNARY; |
| 71 | + int member = MEMBER; |
| 72 | +} |
| 73 | + |
| 74 | +void local_consts() { |
| 75 | + const int literal_int = LITERAL_INT; |
| 76 | + const bool literal_bool = LITERAL_BOOL; |
| 77 | + const float literal_float = LITERAL_FLOAT; |
| 78 | + const char literal_char = LITERAL_CHAR; |
| 79 | + const char *const literal_str_ptr = LITERAL_STR; |
| 80 | + const char literal_str[] = LITERAL_STR; |
| 81 | + const int literal_array[] = LITERAL_ARRAY; |
| 82 | + const struct S literal_struct = LITERAL_STRUCT; |
| 83 | + |
| 84 | + const int nested_int = NESTED_INT; |
| 85 | + const bool nested_bool = NESTED_BOOL; |
| 86 | + const float nested_float = NESTED_FLOAT; |
| 87 | + const char nested_char = NESTED_CHAR; |
| 88 | + const char *const nested_str_ptr = NESTED_STR; |
| 89 | + const char nested_str[] = NESTED_STR; |
| 90 | + const int nested_array[] = NESTED_ARRAY; |
| 91 | + const struct S nested_struct = NESTED_STRUCT; |
| 92 | + |
| 93 | + const int int_arithmetic = INT_ARITHMETIC; |
| 94 | + const float mixed_arithmetic = MIXED_ARITHMETIC; |
| 95 | + const int parens = PARENS; |
| 96 | + const char *const ptr_arithmetic = PTR_ARITHMETIC; |
| 97 | + const unsigned long long widening_cast = WIDENING_CAST; |
| 98 | + const char narrowing_cast = NARROWING_CAST; |
| 99 | + const double conversion_cast = CONVERSION_CAST; |
| 100 | + const char indexing = INDEXING; |
| 101 | + const char *const str_concatenation_ptr = STR_CONCATENATION; |
| 102 | + const char str_concatenation[] = STR_CONCATENATION; |
| 103 | + const char *const ref_indexing = REF_MACRO; |
| 104 | + const struct S *const ref_struct = REF_LITERAL; |
| 105 | + const int ternary = TERNARY; |
| 106 | + const int member = MEMBER; |
| 107 | +} |
| 108 | + |
| 109 | +// TODO These are declared in the global scope and thus clash, |
| 110 | +// which is an error for statics. |
| 111 | +#if 0 |
| 112 | +void local_static_consts() { |
| 113 | + static const int literal_int = LITERAL_INT; |
| 114 | + static const bool literal_bool = LITERAL_BOOL; |
| 115 | + static const float literal_float = LITERAL_FLOAT; |
| 116 | + static const char literal_char = LITERAL_CHAR; |
| 117 | + static const char *const literal_str_ptr = LITERAL_STR; |
| 118 | + static const char literal_str[] = LITERAL_STR; |
| 119 | + static const int literal_array[] = LITERAL_ARRAY; |
| 120 | + static const struct S literal_struct = LITERAL_STRUCT; |
| 121 | + |
| 122 | + static const int nested_int = NESTED_INT; |
| 123 | + static const bool nested_bool = NESTED_BOOL; |
| 124 | + static const float nested_float = NESTED_FLOAT; |
| 125 | + static const char nested_char = NESTED_CHAR; |
| 126 | + static const char *const nested_str_ptr = NESTED_STR; |
| 127 | + static const char nested_str[] = NESTED_STR; |
| 128 | + static const int nested_array[] = NESTED_ARRAY; |
| 129 | + static const struct S nested_struct = NESTED_STRUCT; |
| 130 | + |
| 131 | + static const int int_arithmetic = INT_ARITHMETIC; |
| 132 | + static const float mixed_arithmetic = MIXED_ARITHMETIC; |
| 133 | + static const int parens = PARENS; |
| 134 | + static const char *const ptr_arithmetic = PTR_ARITHMETIC; |
| 135 | + static const unsigned long long widening_cast = WIDENING_CAST; |
| 136 | + static const char narrowing_cast = NARROWING_CAST; |
| 137 | + static const double conversion_cast = CONVERSION_CAST; |
| 138 | + static const char indexing = INDEXING; |
| 139 | + static const char *const str_concatenation_ptr = STR_CONCATENATION; |
| 140 | + static const char str_concatenation[] = STR_CONCATENATION; |
| 141 | + static const char *const ref_indexing = REF_MACRO; |
| 142 | + static const struct S *const ref_struct = REF_LITERAL; |
| 143 | + static const int ternary = TERNARY; |
| 144 | + static const int member = MEMBER; |
| 145 | +} |
| 146 | +#endif |
| 147 | + |
| 148 | +// global static consts |
| 149 | + |
| 150 | +static const int global_static_const_literal_int = LITERAL_INT; |
| 151 | +static const bool global_static_const_literal_bool = LITERAL_BOOL; |
| 152 | +static const float global_static_const_literal_float = LITERAL_FLOAT; |
| 153 | +static const char global_static_const_literal_char = LITERAL_CHAR; |
| 154 | +static const char *const global_static_const_literal_str_ptr = LITERAL_STR; |
| 155 | +static const char global_static_const_literal_str[] = LITERAL_STR; |
| 156 | +static const int global_static_const_literal_array[] = LITERAL_ARRAY; |
| 157 | +static const struct S global_static_const_literal_struct = LITERAL_STRUCT; |
| 158 | + |
| 159 | +static const int global_static_const_nested_int = NESTED_INT; |
| 160 | +static const bool global_static_const_nested_bool = NESTED_BOOL; |
| 161 | +static const float global_static_const_nested_float = NESTED_FLOAT; |
| 162 | +static const char global_static_const_nested_char = NESTED_CHAR; |
| 163 | +static const char *const global_static_const_nested_str_ptr = NESTED_STR; |
| 164 | +static const char global_static_const_nested_str[] = NESTED_STR; |
| 165 | +static const int global_static_const_nested_array[] = NESTED_ARRAY; |
| 166 | +static const struct S global_static_const_nested_struct = NESTED_STRUCT; |
| 167 | + |
| 168 | +static const int global_static_const_int_arithmetic = INT_ARITHMETIC; |
| 169 | +static const float global_static_const_mixed_arithmetic = MIXED_ARITHMETIC; |
| 170 | +static const int global_static_const_parens = PARENS; |
| 171 | +static const char *const global_static_const_ptr_arithmetic = PTR_ARITHMETIC; |
| 172 | +static const unsigned long long global_static_const_widening_cast = |
| 173 | + WIDENING_CAST; |
| 174 | +static const char global_static_const_narrowing_cast = NARROWING_CAST; |
| 175 | +static const double global_static_const_conversion_cast = CONVERSION_CAST; |
| 176 | +static const char global_static_const_indexing = INDEXING; |
| 177 | +static const char *const global_static_const_str_concatenation_ptr = |
| 178 | + STR_CONCATENATION; |
| 179 | +static const char global_static_const_str_concatenation[] = STR_CONCATENATION; |
| 180 | +static const char *const global_static_const_ref_indexing = REF_MACRO; |
| 181 | +static const struct S *const global_static_const_ref_struct = REF_LITERAL; |
| 182 | +static const int global_static_const_ternary = TERNARY; |
| 183 | +static const int global_static_const_member = MEMBER; |
| 184 | + |
| 185 | +void global_static_consts() { |
| 186 | + // Need to use `static`s or else they'll be removed when translated. |
| 187 | + (void)global_static_const_literal_int; |
| 188 | + (void)global_static_const_literal_bool; |
| 189 | + (void)global_static_const_literal_float; |
| 190 | + (void)global_static_const_literal_char; |
| 191 | + (void)global_static_const_literal_str_ptr; |
| 192 | + (void)global_static_const_literal_str; |
| 193 | + (void)global_static_const_literal_array; |
| 194 | + (void)global_static_const_literal_struct; |
| 195 | + |
| 196 | + (void)global_static_const_nested_int; |
| 197 | + (void)global_static_const_nested_bool; |
| 198 | + (void)global_static_const_nested_float; |
| 199 | + (void)global_static_const_nested_char; |
| 200 | + (void)global_static_const_nested_str_ptr; |
| 201 | + (void)global_static_const_nested_str; |
| 202 | + (void)global_static_const_nested_array; |
| 203 | + (void)global_static_const_nested_struct; |
| 204 | + |
| 205 | + (void)global_static_const_int_arithmetic; |
| 206 | + (void)global_static_const_mixed_arithmetic; |
| 207 | + (void)global_static_const_parens; |
| 208 | + (void)global_static_const_ptr_arithmetic; |
| 209 | + (void)global_static_const_widening_cast; |
| 210 | + (void)global_static_const_narrowing_cast; |
| 211 | + (void)global_static_const_conversion_cast; |
| 212 | + (void)global_static_const_indexing; |
| 213 | + (void)global_static_const_str_concatenation_ptr; |
| 214 | + (void)global_static_const_str_concatenation; |
| 215 | + (void)global_static_const_ref_indexing; |
| 216 | + (void)global_static_const_ref_struct; |
| 217 | + (void)global_static_const_ternary; |
| 218 | + (void)global_static_const_member; |
| 219 | +} |
| 220 | + |
| 221 | +// global consts |
| 222 | + |
| 223 | +const int global_const_literal_int = LITERAL_INT; |
| 224 | +const bool global_const_literal_bool = LITERAL_BOOL; |
| 225 | +const float global_const_literal_float = LITERAL_FLOAT; |
| 226 | +const char global_const_literal_char = LITERAL_CHAR; |
| 227 | +const char *const global_const_literal_str_ptr = LITERAL_STR; |
| 228 | +const char global_const_literal_str[] = LITERAL_STR; |
| 229 | +static const int global_const_literal_array[] = LITERAL_ARRAY; |
| 230 | +static const struct S global_const_literal_struct = LITERAL_STRUCT; |
| 231 | + |
| 232 | +const int global_const_nested_int = NESTED_INT; |
| 233 | +const bool global_const_nested_bool = NESTED_BOOL; |
| 234 | +const float global_const_nested_float = NESTED_FLOAT; |
| 235 | +const char global_const_nested_char = NESTED_CHAR; |
| 236 | +const char *const global_const_nested_str_ptr = NESTED_STR; |
| 237 | +const char global_const_nested_str[] = NESTED_STR; |
| 238 | +static const int global_const_nested_array[] = NESTED_ARRAY; |
| 239 | +static const struct S global_const_nested_struct = NESTED_STRUCT; |
| 240 | + |
| 241 | +const int global_const_int_arithmetic = INT_ARITHMETIC; |
| 242 | +const float global_const_mixed_arithmetic = MIXED_ARITHMETIC; |
| 243 | +const int global_const_parens = PARENS; |
| 244 | +const char *const global_const_ptr_arithmetic = PTR_ARITHMETIC; |
| 245 | +const unsigned long long global_const_widening_cast = WIDENING_CAST; |
| 246 | +const char global_const_narrowing_cast = NARROWING_CAST; |
| 247 | +const double global_const_conversion_cast = CONVERSION_CAST; |
| 248 | +const char global_const_indexing = INDEXING; |
| 249 | +const char *const global_const_str_concatenation_ptr = STR_CONCATENATION; |
| 250 | +const char global_const_str_concatenation[] = STR_CONCATENATION; |
| 251 | +static const char *const global_const_ref_indexing = REF_MACRO; |
| 252 | +static const struct S *const global_const_ref_struct = REF_LITERAL; |
| 253 | +static const int global_const_ternary = TERNARY; |
| 254 | +static const int global_const_member = MEMBER; |
| 255 | + |
3 | 256 | typedef unsigned long long U64; |
4 | 257 |
|
5 | 258 | #define TEST_FN_MACRO(x) ((x) * (x)) |
|
0 commit comments