1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // ExpressionNode.cc -- |
---|
8 | // |
---|
9 | // Author : Peter A. Buhr |
---|
10 | // Created On : Sat May 16 13:17:07 2015 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Tue Mar 14 09:10:22 2023 |
---|
13 | // Update Count : 1081 |
---|
14 | // |
---|
15 | |
---|
16 | #include <cassert> // for assert |
---|
17 | #include <stdio.h> // for sscanf, size_t |
---|
18 | #include <climits> // for LLONG_MAX, LONG_MAX, INT_MAX, UINT... |
---|
19 | #include <list> // for list |
---|
20 | #include <sstream> // for basic_istream::operator>>, basic_i... |
---|
21 | #include <string> // for string, operator+, operator== |
---|
22 | |
---|
23 | #include "Common/SemanticError.h" // for SemanticError |
---|
24 | #include "Common/utility.h" // for maybeMoveBuild, maybeBuild, CodeLo... |
---|
25 | #include "ParseNode.h" // for ExpressionNode, maybeMoveBuildType |
---|
26 | #include "SynTree/Constant.h" // for Constant |
---|
27 | #include "SynTree/Declaration.h" // for EnumDecl, StructDecl, UnionDecl |
---|
28 | #include "SynTree/Expression.h" // for Expression, ConstantExpr, NameExpr |
---|
29 | #include "SynTree/Statement.h" // for CompoundStmt, Statement |
---|
30 | #include "SynTree/Type.h" // for BasicType, Type, Type::Qualifiers |
---|
31 | #include "parserutility.h" // for notZeroExpr |
---|
32 | |
---|
33 | class Initializer; |
---|
34 | |
---|
35 | using namespace std; |
---|
36 | |
---|
37 | //############################################################################## |
---|
38 | |
---|
39 | // Difficult to separate extra parts of constants during lexing because actions are not allow in the middle of patterns: |
---|
40 | // |
---|
41 | // prefix action constant action suffix |
---|
42 | // |
---|
43 | // Alternatively, breaking a pattern using BEGIN does not work if the following pattern can be empty: |
---|
44 | // |
---|
45 | // constant BEGIN CONT ... |
---|
46 | // <CONT>(...)? BEGIN 0 ... // possible empty suffix |
---|
47 | // |
---|
48 | // because the CONT rule is NOT triggered if the pattern is empty. Hence, constants are reparsed here to determine their |
---|
49 | // type. |
---|
50 | |
---|
51 | extern const Type::Qualifiers noQualifiers; // no qualifiers on constants |
---|
52 | |
---|
53 | // static inline bool checkH( char c ) { return c == 'h' || c == 'H'; } |
---|
54 | // static inline bool checkZ( char c ) { return c == 'z' || c == 'Z'; } |
---|
55 | // static inline bool checkU( char c ) { return c == 'u' || c == 'U'; } |
---|
56 | static inline bool checkF( char c ) { return c == 'f' || c == 'F'; } |
---|
57 | static inline bool checkD( char c ) { return c == 'd' || c == 'D'; } |
---|
58 | static inline bool checkF80( char c ) { return c == 'w' || c == 'W'; } |
---|
59 | static inline bool checkF128( char c ) { return c == 'q' || c == 'Q'; } |
---|
60 | static inline bool checkL( char c ) { return c == 'l' || c == 'L'; } |
---|
61 | static inline bool checkI( char c ) { return c == 'i' || c == 'I'; } |
---|
62 | static inline bool checkB( char c ) { return c == 'b' || c == 'B'; } |
---|
63 | static inline bool checkX( char c ) { return c == 'x' || c == 'X'; } |
---|
64 | // static inline bool checkN( char c ) { return c == 'n' || c == 'N'; } |
---|
65 | |
---|
66 | void lnthSuffix( string & str, int & type, int & ltype ) { |
---|
67 | // 'u' can appear before or after length suffix |
---|
68 | string::size_type posn = str.find_last_of( "lL" ); |
---|
69 | |
---|
70 | if ( posn == string::npos ) return; // no suffix |
---|
71 | size_t end = str.length() - 1; |
---|
72 | if ( posn == end ) { type = 3; return; } // no length after 'l' => long |
---|
73 | |
---|
74 | string::size_type next = posn + 1; // advance to length |
---|
75 | if ( str[next] == '3' ) { // 32 |
---|
76 | type = ltype = 2; |
---|
77 | } else if ( str[next] == '6' ) { // 64 |
---|
78 | type = ltype = 3; |
---|
79 | } else if ( str[next] == '8' ) { // 8 |
---|
80 | type = ltype = 1; |
---|
81 | } else if ( str[next] == '1' ) { |
---|
82 | if ( str[next + 1] == '6' ) { // 16 |
---|
83 | type = ltype = 0; |
---|
84 | } else { // 128 |
---|
85 | type = 5; ltype = 6; |
---|
86 | } // if |
---|
87 | } // if |
---|
88 | |
---|
89 | char fix = '\0'; |
---|
90 | if ( str[end] == 'u' || str[end] == 'U' ) fix = str[end]; // ends with 'uU' ? |
---|
91 | str.erase( posn ); // remove length suffix and possibly uU |
---|
92 | if ( type == 5 ) { // L128 does not need uU |
---|
93 | end = str.length() - 1; |
---|
94 | if ( str[end] == 'u' || str[end] == 'U' ) str.erase( end ); // ends with 'uU' ? remove |
---|
95 | } else if ( fix != '\0' ) str += fix; // put 'uU' back if removed |
---|
96 | } // lnthSuffix |
---|
97 | |
---|
98 | void valueToType( unsigned long long int & v, bool dec, int & type, bool & Unsigned ) { |
---|
99 | // use value to determine type |
---|
100 | if ( v <= INT_MAX ) { // signed int |
---|
101 | type = 2; |
---|
102 | } else if ( v <= UINT_MAX && ! dec ) { // unsigned int |
---|
103 | type = 2; |
---|
104 | Unsigned = true; // unsigned |
---|
105 | } else if ( v <= LONG_MAX ) { // signed long int |
---|
106 | type = 3; |
---|
107 | } else if ( v <= ULONG_MAX && ( ! dec || LONG_MAX == LLONG_MAX ) ) { // signed long int |
---|
108 | type = 3; |
---|
109 | Unsigned = true; // unsigned long int |
---|
110 | } else if ( v <= LLONG_MAX ) { // signed long long int |
---|
111 | type = 4; |
---|
112 | } else { // unsigned long long int |
---|
113 | type = 4; |
---|
114 | Unsigned = true; // unsigned long long int |
---|
115 | } // if |
---|
116 | } // valueToType |
---|
117 | |
---|
118 | static void scanbin( string & str, unsigned long long int & v ) { |
---|
119 | v = 0; |
---|
120 | size_t last = str.length() - 1; // last subscript of constant |
---|
121 | for ( unsigned int i = 2;; ) { // ignore prefix |
---|
122 | if ( str[i] == '1' ) v |= 1; |
---|
123 | i += 1; |
---|
124 | if ( i == last - 1 || (str[i] != '0' && str[i] != '1') ) break; |
---|
125 | v <<= 1; |
---|
126 | } // for |
---|
127 | } // scanbin |
---|
128 | |
---|
129 | Expression * build_constantInteger( string & str ) { |
---|
130 | static const BasicType::Kind kind[2][6] = { |
---|
131 | // short (h) must be before char (hh) because shorter type has the longer suffix |
---|
132 | { BasicType::ShortSignedInt, BasicType::SignedChar, BasicType::SignedInt, BasicType::LongSignedInt, BasicType::LongLongSignedInt, /* BasicType::SignedInt128 */ BasicType::LongLongSignedInt, }, |
---|
133 | { BasicType::ShortUnsignedInt, BasicType::UnsignedChar, BasicType::UnsignedInt, BasicType::LongUnsignedInt, BasicType::LongLongUnsignedInt, /* BasicType::UnsignedInt128 */ BasicType::LongLongUnsignedInt, }, |
---|
134 | }; |
---|
135 | |
---|
136 | static const char * lnthsInt[2][6] = { |
---|
137 | { "int16_t", "int8_t", "int32_t", "int64_t", "size_t", "uintptr_t", }, |
---|
138 | { "uint16_t", "uint8_t", "uint32_t", "uint64_t", "size_t", "uintptr_t", }, |
---|
139 | }; // lnthsInt |
---|
140 | |
---|
141 | string str2( "0x0" ); |
---|
142 | unsigned long long int v, v2 = 0; // converted integral value |
---|
143 | Expression * ret, * ret2; |
---|
144 | |
---|
145 | int type = -1; // 0 => short, 1 => char, 2 => int, 3 => long int, 4 => long long int, 5 => int128 |
---|
146 | int ltype = -1; // 0 => 16 bits, 1 => 8 bits, 2 => 32 bits, 3 => 64 bits, 4 => size_t, 5 => intptr, 6 => pointer |
---|
147 | bool dec = true, Unsigned = false; // decimal, unsigned constant |
---|
148 | |
---|
149 | // special constants |
---|
150 | if ( str == "0" ) { |
---|
151 | ret = new ConstantExpr( Constant( (Type *)new ZeroType( noQualifiers ), str, (unsigned long long int)0 ) ); |
---|
152 | goto CLEANUP; |
---|
153 | } // if |
---|
154 | if ( str == "1" ) { |
---|
155 | ret = new ConstantExpr( Constant( (Type *)new OneType( noQualifiers ), str, (unsigned long long int)1 ) ); |
---|
156 | goto CLEANUP; |
---|
157 | } // if |
---|
158 | |
---|
159 | // 'u' can appear before or after length suffix |
---|
160 | if ( str.find_last_of( "uU" ) != string::npos ) Unsigned = true; |
---|
161 | |
---|
162 | if ( isdigit( str[str.length() - 1] ) ) { // no suffix ? |
---|
163 | lnthSuffix( str, type, ltype ); // could have length suffix |
---|
164 | } else { |
---|
165 | // At least one digit in integer constant, so safe to backup while looking for suffix. |
---|
166 | string::size_type posn; |
---|
167 | // pointer value |
---|
168 | if ( posn = str.find_last_of( "pP" ); posn != string::npos ) { |
---|
169 | ltype = 5; str.erase( posn, 1 ); |
---|
170 | // size_t |
---|
171 | } else if ( posn = str.find_last_of( "zZ" ); posn != string::npos ) { |
---|
172 | Unsigned = true; type = 2; ltype = 4; str.erase( posn, 1 ); |
---|
173 | // signed char |
---|
174 | } else if ( posn = str.rfind( "hh" ); posn != string::npos ) { |
---|
175 | type = 1; str.erase( posn, 2 ); |
---|
176 | // signed char |
---|
177 | } else if ( posn = str.rfind( "HH" ); posn != string::npos ) { |
---|
178 | type = 1; str.erase( posn, 2 ); |
---|
179 | // short |
---|
180 | } else if ( posn = str.find_last_of( "hH" ); posn != string::npos ) { |
---|
181 | type = 0; str.erase( posn, 1 ); |
---|
182 | // int (natural number) |
---|
183 | } else if ( posn = str.find_last_of( "nN" ); posn != string::npos ) { |
---|
184 | type = 2; str.erase( posn, 1 ); |
---|
185 | } else if ( str.rfind( "ll" ) != string::npos || str.rfind( "LL" ) != string::npos ) { |
---|
186 | type = 4; |
---|
187 | } else { |
---|
188 | lnthSuffix( str, type, ltype ); |
---|
189 | } // if |
---|
190 | } // if |
---|
191 | |
---|
192 | // Cannot be just "0"/"1"; sscanf stops at the suffix, if any; value goes over the wall => always generate |
---|
193 | |
---|
194 | #if ! defined(__SIZEOF_INT128__) |
---|
195 | if ( type == 5 ) SemanticError( yylloc, "int128 constant is not supported on this target " + str ); |
---|
196 | #endif // ! __SIZEOF_INT128__ |
---|
197 | |
---|
198 | if ( str[0] == '0' ) { // radix character ? |
---|
199 | dec = false; |
---|
200 | if ( checkX( str[1] ) ) { // hex constant ? |
---|
201 | if ( type < 5 ) { // not L128 ? |
---|
202 | sscanf( (char *)str.c_str(), "%llx", &v ); |
---|
203 | #if defined(__SIZEOF_INT128__) |
---|
204 | } else { // hex int128 constant |
---|
205 | unsigned int len = str.length(); |
---|
206 | if ( len > (2 + 16 + 16) ) SemanticError( yylloc, "128-bit hexadecimal constant to large " + str ); |
---|
207 | // hex digits < 2^64 |
---|
208 | if ( len > (2 + 16) ) { |
---|
209 | str2 = "0x" + str.substr( len - 16 ); |
---|
210 | sscanf( (char *)str2.c_str(), "%llx", &v2 ); |
---|
211 | str = str.substr( 0, len - 16 ); |
---|
212 | } // if |
---|
213 | sscanf( (char *)str.c_str(), "%llx", &v ); |
---|
214 | #endif // __SIZEOF_INT128__ |
---|
215 | } // if |
---|
216 | //printf( "%llx %llu\n", v, v ); |
---|
217 | } else if ( checkB( str[1] ) ) { // binary constant ? |
---|
218 | #if defined(__SIZEOF_INT128__) |
---|
219 | unsigned int len = str.length(); |
---|
220 | if ( type == 5 && len > 2 + 64 ) { |
---|
221 | if ( len > 2 + 64 + 64 ) SemanticError( yylloc, "128-bit binary constant to large " + str ); |
---|
222 | str2 = "0b" + str.substr( len - 64 ); |
---|
223 | str = str.substr( 0, len - 64 ); |
---|
224 | scanbin( str2, v2 ); |
---|
225 | } // if |
---|
226 | #endif // __SIZEOF_INT128__ |
---|
227 | scanbin( str, v ); |
---|
228 | //printf( "%#llx %llu\n", v, v ); |
---|
229 | } else { // octal constant |
---|
230 | if ( type < 5 ) { // not L128 ? |
---|
231 | sscanf( (char *)str.c_str(), "%llo", &v ); |
---|
232 | #if defined(__SIZEOF_INT128__) |
---|
233 | } else { // octal int128 constant |
---|
234 | unsigned int len = str.length(); |
---|
235 | if ( len > 1 + 43 || (len == 1 + 43 && str[0] > '3') ) SemanticError( yylloc, "128-bit octal constant to large " + str ); |
---|
236 | char buf[32]; |
---|
237 | if ( len <= 1 + 21 ) { // value < 21 octal digitis |
---|
238 | sscanf( (char *)str.c_str(), "%llo", &v ); |
---|
239 | } else { |
---|
240 | sscanf( &str[len - 21], "%llo", &v ); |
---|
241 | __int128 val = v; // accumulate bits |
---|
242 | str[len - 21] ='\0'; // shorten string |
---|
243 | sscanf( &str[len == 43 ? 1 : 0], "%llo", &v ); |
---|
244 | val |= (__int128)v << 63; // store bits |
---|
245 | if ( len == 1 + 43 ) { // most significant 2 bits ? |
---|
246 | str[2] = '\0'; // shorten string |
---|
247 | sscanf( &str[1], "%llo", &v ); // process most significant 2 bits |
---|
248 | val |= (__int128)v << 126; // store bits |
---|
249 | } // if |
---|
250 | v = val >> 64; v2 = (uint64_t)val; // replace octal constant with 2 hex constants |
---|
251 | sprintf( buf, "%#llx", v2 ); |
---|
252 | str2 = buf; |
---|
253 | } // if |
---|
254 | sprintf( buf, "%#llx", v ); |
---|
255 | str = buf; |
---|
256 | #endif // __SIZEOF_INT128__ |
---|
257 | } // if |
---|
258 | //printf( "%#llo %llu\n", v, v ); |
---|
259 | } // if |
---|
260 | } else { // decimal constant ? |
---|
261 | if ( type < 5 ) { // not L128 ? |
---|
262 | sscanf( (char *)str.c_str(), "%llu", &v ); |
---|
263 | #if defined(__SIZEOF_INT128__) |
---|
264 | } else { // decimal int128 constant |
---|
265 | #define P10_UINT64 10'000'000'000'000'000'000ULL // 19 zeroes |
---|
266 | unsigned int len = str.length(); |
---|
267 | if ( str.length() == 39 && str > (Unsigned ? "340282366920938463463374607431768211455" : "170141183460469231731687303715884105727") ) |
---|
268 | SemanticError( yylloc, "128-bit decimal constant to large " + str ); |
---|
269 | char buf[32]; |
---|
270 | if ( len <= 19 ) { // value < 19 decimal digitis |
---|
271 | sscanf( (char *)str.c_str(), "%llu", &v ); |
---|
272 | } else { |
---|
273 | sscanf( &str[len - 19], "%llu", &v ); |
---|
274 | __int128 val = v; // accumulate bits |
---|
275 | str[len - 19] ='\0'; // shorten string |
---|
276 | sscanf( &str[len == 39 ? 1 : 0], "%llu", &v ); |
---|
277 | val += (__int128)v * (__int128)P10_UINT64; // store bits |
---|
278 | if ( len == 39 ) { // most significant 2 bits ? |
---|
279 | str[1] = '\0'; // shorten string |
---|
280 | sscanf( &str[0], "%llu", &v ); // process most significant 2 bits |
---|
281 | val += (__int128)v * (__int128)P10_UINT64 * (__int128)P10_UINT64; // store bits |
---|
282 | } // if |
---|
283 | v = val >> 64; v2 = (uint64_t)val; // replace decimal constant with 2 hex constants |
---|
284 | sprintf( buf, "%#llx", v2 ); |
---|
285 | str2 = buf; |
---|
286 | } // if |
---|
287 | sprintf( buf, "%#llx", v ); |
---|
288 | str = buf; |
---|
289 | #endif // __SIZEOF_INT128__ |
---|
290 | } // if |
---|
291 | //printf( "%llu\n", v ); |
---|
292 | } // if |
---|
293 | |
---|
294 | if ( type == -1 ) { // no suffix => determine type from value size |
---|
295 | valueToType( v, dec, type, Unsigned ); |
---|
296 | } // if |
---|
297 | /* printf( "%s %llo %s %llo\n", str.c_str(), v, str2.c_str(), v2 ); */ |
---|
298 | |
---|
299 | //if ( !( 0 <= type && type <= 6 ) ) { printf( "%s %lu %d %s\n", fred.c_str(), fred.length(), type, str.c_str() ); } |
---|
300 | assert( 0 <= type && type <= 6 ); |
---|
301 | |
---|
302 | // Constant type is correct for overload resolving. |
---|
303 | ret = new ConstantExpr( Constant( new BasicType( noQualifiers, kind[Unsigned][type] ), str, v ) ); |
---|
304 | if ( Unsigned && type < 2 ) { // hh or h, less than int ? |
---|
305 | // int i = -1uh => 65535 not -1, so cast is necessary for unsigned, which unfortunately eliminates warnings for large values. |
---|
306 | ret = new CastExpr( ret, new BasicType( Type::Qualifiers(), kind[Unsigned][type] ), false ); |
---|
307 | } else if ( ltype != -1 ) { // explicit length ? |
---|
308 | if ( ltype == 6 ) { // int128, (int128)constant |
---|
309 | // ret = new CastExpr( ret, new BasicType( Type::Qualifiers(), kind[Unsigned][type] ), false ); |
---|
310 | ret2 = new ConstantExpr( Constant( new BasicType( noQualifiers, BasicType::LongLongSignedInt ), str2, v2 ) ); |
---|
311 | ret = build_compoundLiteral( |
---|
312 | DeclarationNode::newBasicType( DeclarationNode::Int128 )->addType( DeclarationNode::newSignedNess( DeclarationNode::Unsigned ) ), |
---|
313 | new InitializerNode( (InitializerNode *)(new InitializerNode( new ExpressionNode( v2 == 0 ? ret2 : ret ) ))->set_last( new InitializerNode( new ExpressionNode( v2 == 0 ? ret : ret2 ) ) ), true ) ); |
---|
314 | } else { // explicit length, (length_type)constant |
---|
315 | ret = new CastExpr( ret, new TypeInstType( Type::Qualifiers(), lnthsInt[Unsigned][ltype], false ), false ); |
---|
316 | if ( ltype == 5 ) { // pointer, intptr( (uintptr_t)constant ) |
---|
317 | ret = build_func( new ExpressionNode( build_varref( new string( "intptr" ) ) ), new ExpressionNode( ret ) ); |
---|
318 | } // if |
---|
319 | } // if |
---|
320 | } // if |
---|
321 | |
---|
322 | CLEANUP: ; |
---|
323 | delete &str; // created by lex |
---|
324 | return ret; |
---|
325 | } // build_constantInteger |
---|
326 | |
---|
327 | |
---|
328 | static inline void checkFnxFloat( string & str, size_t last, bool & explnth, int & type ) { |
---|
329 | string::size_type posn; |
---|
330 | // floating-point constant has minimum of 2 characters, 1. or .1, so safe to look ahead |
---|
331 | if ( str[1] == 'x' ) { // hex ? |
---|
332 | posn = str.find_last_of( "pP" ); // back for exponent (must have) |
---|
333 | posn = str.find_first_of( "fF", posn + 1 ); // forward for size (fF allowed in hex constant) |
---|
334 | } else { |
---|
335 | posn = str.find_last_of( "fF" ); // back for size (fF not allowed) |
---|
336 | } // if |
---|
337 | if ( posn == string::npos ) return; |
---|
338 | explnth = true; |
---|
339 | posn += 1; // advance to size |
---|
340 | if ( str[posn] == '3' ) { // 32 |
---|
341 | if ( str[last] != 'x' ) type = 6; |
---|
342 | else type = 7; |
---|
343 | } else if ( str[posn] == '6' ) { // 64 |
---|
344 | if ( str[last] != 'x' ) type = 8; |
---|
345 | else type = 9; |
---|
346 | } else if ( str[posn] == '8' ) { // 80 |
---|
347 | type = 3; |
---|
348 | } else if ( str[posn] == '1' ) { // 16/128 |
---|
349 | if ( str[posn + 1] == '6' ) { // 16 |
---|
350 | type = 5; |
---|
351 | } else { // 128 |
---|
352 | if ( str[last] != 'x' ) type = 10; |
---|
353 | else type = 11; |
---|
354 | } // if |
---|
355 | } else { |
---|
356 | assertf( false, "internal error, bad floating point length %s", str.c_str() ); |
---|
357 | } // if |
---|
358 | } // checkFnxFloat |
---|
359 | |
---|
360 | |
---|
361 | Expression * build_constantFloat( string & str ) { |
---|
362 | static const BasicType::Kind kind[2][12] = { |
---|
363 | { BasicType::Float, BasicType::Double, BasicType::LongDouble, BasicType::uuFloat80, BasicType::uuFloat128, BasicType::uFloat16, BasicType::uFloat32, BasicType::uFloat32x, BasicType::uFloat64, BasicType::uFloat64x, BasicType::uFloat128, BasicType::uFloat128x }, |
---|
364 | { BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, BasicType::NUMBER_OF_BASIC_TYPES, BasicType::NUMBER_OF_BASIC_TYPES, BasicType::uFloat16Complex, BasicType::uFloat32Complex, BasicType::uFloat32xComplex, BasicType::uFloat64Complex, BasicType::uFloat64xComplex, BasicType::uFloat128Complex, BasicType::uFloat128xComplex }, |
---|
365 | }; |
---|
366 | |
---|
367 | // floating-point constant has minimum of 2 characters 1. or .1 |
---|
368 | size_t last = str.length() - 1; |
---|
369 | double v; |
---|
370 | int type; // 0 => float, 1 => double, 3 => long double, ... |
---|
371 | bool complx = false; // real, complex |
---|
372 | bool explnth = false; // explicit literal length |
---|
373 | |
---|
374 | sscanf( str.c_str(), "%lg", &v ); |
---|
375 | |
---|
376 | if ( checkI( str[last] ) ) { // imaginary ? |
---|
377 | complx = true; |
---|
378 | last -= 1; // backup one character |
---|
379 | } // if |
---|
380 | |
---|
381 | if ( checkF( str[last] ) ) { // float ? |
---|
382 | type = 0; |
---|
383 | } else if ( checkD( str[last] ) ) { // double ? |
---|
384 | type = 1; |
---|
385 | } else if ( checkL( str[last] ) ) { // long double ? |
---|
386 | type = 2; |
---|
387 | } else if ( checkF80( str[last] ) ) { // __float80 ? |
---|
388 | type = 3; |
---|
389 | } else if ( checkF128( str[last] ) ) { // __float128 ? |
---|
390 | type = 4; |
---|
391 | } else { |
---|
392 | type = 1; // double (default if no suffix) |
---|
393 | checkFnxFloat( str, last, explnth, type ); |
---|
394 | } // if |
---|
395 | |
---|
396 | if ( ! complx && checkI( str[last - 1] ) ) { // imaginary ? |
---|
397 | complx = true; |
---|
398 | } // if |
---|
399 | |
---|
400 | assert( 0 <= type && type < 12 ); |
---|
401 | Expression * ret = new ConstantExpr( Constant( new BasicType( noQualifiers, kind[complx][type] ), str, v ) ); |
---|
402 | if ( explnth ) { // explicit length ? |
---|
403 | ret = new CastExpr( ret, new BasicType( Type::Qualifiers(), kind[complx][type] ), false ); |
---|
404 | } // if |
---|
405 | |
---|
406 | delete &str; // created by lex |
---|
407 | return ret; |
---|
408 | } // build_constantFloat |
---|
409 | |
---|
410 | static void sepString( string & str, string & units, char delimit ) { |
---|
411 | string::size_type posn = str.find_last_of( delimit ) + 1; |
---|
412 | if ( posn != str.length() ) { |
---|
413 | units = "?" + str.substr( posn ); // extract units |
---|
414 | str.erase( posn ); // remove units |
---|
415 | } // if |
---|
416 | } // sepString |
---|
417 | |
---|
418 | Expression * build_constantChar( string & str ) { |
---|
419 | string units; // units |
---|
420 | sepString( str, units, '\'' ); // separate constant from units |
---|
421 | |
---|
422 | Expression * ret = new ConstantExpr( Constant( new BasicType( noQualifiers, BasicType::Char ), str, (unsigned long long int)(unsigned char)str[1] ) ); |
---|
423 | if ( units.length() != 0 ) { |
---|
424 | ret = new UntypedExpr( new NameExpr( units ), { ret } ); |
---|
425 | } // if |
---|
426 | |
---|
427 | delete &str; // created by lex |
---|
428 | return ret; |
---|
429 | } // build_constantChar |
---|
430 | |
---|
431 | Expression * build_constantStr( string & str ) { |
---|
432 | assert( str.length() > 0 ); |
---|
433 | string units; // units |
---|
434 | sepString( str, units, '"' ); // separate constant from units |
---|
435 | |
---|
436 | Type * strtype; |
---|
437 | switch ( str[0] ) { // str has >= 2 characters, i.e, null string "" => safe to look at subscripts 0/1 |
---|
438 | case 'u': |
---|
439 | if ( str[1] == '8' ) goto Default; // utf-8 characters => array of char |
---|
440 | // lookup type of associated typedef |
---|
441 | strtype = new TypeInstType( Type::Qualifiers( ), "char16_t", false ); |
---|
442 | break; |
---|
443 | case 'U': |
---|
444 | strtype = new TypeInstType( Type::Qualifiers( ), "char32_t", false ); |
---|
445 | break; |
---|
446 | case 'L': |
---|
447 | strtype = new TypeInstType( Type::Qualifiers( ), "wchar_t", false ); |
---|
448 | break; |
---|
449 | Default: // char default string type |
---|
450 | default: |
---|
451 | strtype = new BasicType( Type::Qualifiers( ), BasicType::Char ); |
---|
452 | } // switch |
---|
453 | ArrayType * at = new ArrayType( noQualifiers, strtype, |
---|
454 | new ConstantExpr( Constant::from_ulong( str.size() + 1 - 2 ) ), // +1 for '\0' and -2 for '"' |
---|
455 | false, false ); |
---|
456 | Expression * ret = new ConstantExpr( Constant( at, str, std::nullopt ) ); |
---|
457 | if ( units.length() != 0 ) { |
---|
458 | ret = new UntypedExpr( new NameExpr( units ), { ret } ); |
---|
459 | } // if |
---|
460 | |
---|
461 | delete &str; // created by lex |
---|
462 | return ret; |
---|
463 | } // build_constantStr |
---|
464 | |
---|
465 | Expression * build_field_name_FLOATING_FRACTIONconstant( const string & str ) { |
---|
466 | if ( str.find_first_not_of( "0123456789", 1 ) != string::npos ) SemanticError( yylloc, "invalid tuple index " + str ); |
---|
467 | Expression * ret = build_constantInteger( *new string( str.substr(1) ) ); |
---|
468 | delete &str; |
---|
469 | return ret; |
---|
470 | } // build_field_name_FLOATING_FRACTIONconstant |
---|
471 | |
---|
472 | Expression * build_field_name_FLOATING_DECIMALconstant( const string & str ) { |
---|
473 | if ( str[str.size() - 1] != '.' ) SemanticError( yylloc, "invalid tuple index " + str ); |
---|
474 | Expression * ret = build_constantInteger( *new string( str.substr( 0, str.size()-1 ) ) ); |
---|
475 | delete &str; |
---|
476 | return ret; |
---|
477 | } // build_field_name_FLOATING_DECIMALconstant |
---|
478 | |
---|
479 | Expression * build_field_name_FLOATINGconstant( const string & str ) { |
---|
480 | // str is of the form A.B -> separate at the . and return member expression |
---|
481 | int a, b; |
---|
482 | char dot; |
---|
483 | stringstream ss( str ); |
---|
484 | ss >> a >> dot >> b; |
---|
485 | UntypedMemberExpr * ret = new UntypedMemberExpr( new ConstantExpr( Constant::from_int( b ) ), new ConstantExpr( Constant::from_int( a ) ) ); |
---|
486 | delete &str; |
---|
487 | return ret; |
---|
488 | } // build_field_name_FLOATINGconstant |
---|
489 | |
---|
490 | Expression * make_field_name_fraction_constants( Expression * fieldName, Expression * fracts ) { |
---|
491 | if ( fracts ) { |
---|
492 | if ( UntypedMemberExpr * memberExpr = dynamic_cast< UntypedMemberExpr * >( fracts ) ) { |
---|
493 | memberExpr->set_member( make_field_name_fraction_constants( fieldName, memberExpr->get_aggregate() ) ); |
---|
494 | return memberExpr; |
---|
495 | } else { |
---|
496 | return new UntypedMemberExpr( fracts, fieldName ); |
---|
497 | } // if |
---|
498 | } // if |
---|
499 | return fieldName; |
---|
500 | } // make_field_name_fraction_constants |
---|
501 | |
---|
502 | Expression * build_field_name_fraction_constants( Expression * fieldName, ExpressionNode * fracts ) { |
---|
503 | return make_field_name_fraction_constants( fieldName, maybeMoveBuild( fracts ) ); |
---|
504 | } // build_field_name_fraction_constants |
---|
505 | |
---|
506 | NameExpr * build_varref( const string * name ) { |
---|
507 | NameExpr * expr = new NameExpr( *name ); |
---|
508 | delete name; |
---|
509 | return expr; |
---|
510 | } // build_varref |
---|
511 | |
---|
512 | QualifiedNameExpr * build_qualified_expr( const DeclarationNode * decl_node, const NameExpr * name ) { |
---|
513 | Declaration * newDecl = maybeBuild(decl_node); |
---|
514 | if ( DeclarationWithType * newDeclWithType = dynamic_cast< DeclarationWithType * >( newDecl ) ) { |
---|
515 | const Type * t = newDeclWithType->get_type(); |
---|
516 | if ( t ) { |
---|
517 | if ( const TypeInstType * typeInst = dynamic_cast<const TypeInstType *>( t ) ) { |
---|
518 | newDecl= new EnumDecl( typeInst->name ); |
---|
519 | } |
---|
520 | } |
---|
521 | } |
---|
522 | return new QualifiedNameExpr( newDecl, name->name ); |
---|
523 | } |
---|
524 | |
---|
525 | QualifiedNameExpr * build_qualified_expr( const EnumDecl * decl_node, const NameExpr * name ) { |
---|
526 | EnumDecl * newDecl = const_cast< EnumDecl * >( decl_node ); |
---|
527 | return new QualifiedNameExpr( newDecl, name->name ); |
---|
528 | } |
---|
529 | |
---|
530 | DimensionExpr * build_dimensionref( const string * name ) { |
---|
531 | DimensionExpr * expr = new DimensionExpr( *name ); |
---|
532 | delete name; |
---|
533 | return expr; |
---|
534 | } // build_varref |
---|
535 | |
---|
536 | // TODO: get rid of this and OperKinds and reuse code from OperatorTable |
---|
537 | static const char * OperName[] = { // must harmonize with OperKinds |
---|
538 | // diadic |
---|
539 | "SizeOf", "AlignOf", "OffsetOf", "?+?", "?-?", "?\\?", "?*?", "?/?", "?%?", "||", "&&", |
---|
540 | "?|?", "?&?", "?^?", "Cast", "?<<?", "?>>?", "?<?", "?>?", "?<=?", "?>=?", "?==?", "?!=?", |
---|
541 | "?=?", "?@=?", "?\\=?", "?*=?", "?/=?", "?%=?", "?+=?", "?-=?", "?<<=?", "?>>=?", "?&=?", "?^=?", "?|=?", |
---|
542 | "?[?]", "...", |
---|
543 | // monadic |
---|
544 | "+?", "-?", "AddressOf", "*?", "!?", "~?", "++?", "?++", "--?", "?--", |
---|
545 | }; // OperName |
---|
546 | |
---|
547 | Expression * build_cast( DeclarationNode * decl_node, ExpressionNode * expr_node ) { |
---|
548 | Type * targetType = maybeMoveBuildType( decl_node ); |
---|
549 | if ( dynamic_cast< VoidType * >( targetType ) ) { |
---|
550 | delete targetType; |
---|
551 | return new CastExpr( maybeMoveBuild( expr_node ), false ); |
---|
552 | } else { |
---|
553 | return new CastExpr( maybeMoveBuild( expr_node ), targetType, false ); |
---|
554 | } // if |
---|
555 | } // build_cast |
---|
556 | |
---|
557 | Expression * build_keyword_cast( AggregateDecl::Aggregate target, ExpressionNode * expr_node ) { |
---|
558 | return new KeywordCastExpr( maybeMoveBuild( expr_node ), target ); |
---|
559 | } |
---|
560 | |
---|
561 | Expression * build_virtual_cast( DeclarationNode * decl_node, ExpressionNode * expr_node ) { |
---|
562 | return new VirtualCastExpr( maybeMoveBuild( expr_node ), maybeMoveBuildType( decl_node ) ); |
---|
563 | } // build_virtual_cast |
---|
564 | |
---|
565 | Expression * build_fieldSel( ExpressionNode * expr_node, Expression * member ) { |
---|
566 | return new UntypedMemberExpr( member, maybeMoveBuild( expr_node ) ); |
---|
567 | } // build_fieldSel |
---|
568 | |
---|
569 | Expression * build_pfieldSel( ExpressionNode * expr_node, Expression * member ) { |
---|
570 | UntypedExpr * deref = new UntypedExpr( new NameExpr( "*?" ) ); |
---|
571 | deref->location = expr_node->location; |
---|
572 | deref->get_args().push_back( maybeMoveBuild( expr_node ) ); |
---|
573 | UntypedMemberExpr * ret = new UntypedMemberExpr( member, deref ); |
---|
574 | return ret; |
---|
575 | } // build_pfieldSel |
---|
576 | |
---|
577 | Expression * build_offsetOf( DeclarationNode * decl_node, NameExpr * member ) { |
---|
578 | Expression * ret = new UntypedOffsetofExpr( maybeMoveBuildType( decl_node ), member->get_name() ); |
---|
579 | delete member; |
---|
580 | return ret; |
---|
581 | } // build_offsetOf |
---|
582 | |
---|
583 | Expression * build_and_or( ExpressionNode * expr_node1, ExpressionNode * expr_node2, bool kind ) { |
---|
584 | return new LogicalExpr( notZeroExpr( maybeMoveBuild( expr_node1 ) ), notZeroExpr( maybeMoveBuild( expr_node2 ) ), kind ); |
---|
585 | } // build_and_or |
---|
586 | |
---|
587 | Expression * build_unary_val( OperKinds op, ExpressionNode * expr_node ) { |
---|
588 | list< Expression * > args; |
---|
589 | args.push_back( maybeMoveBuild( expr_node ) ); |
---|
590 | return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args ); |
---|
591 | } // build_unary_val |
---|
592 | |
---|
593 | Expression * build_binary_val( OperKinds op, ExpressionNode * expr_node1, ExpressionNode * expr_node2 ) { |
---|
594 | list< Expression * > args; |
---|
595 | args.push_back( maybeMoveBuild( expr_node1 ) ); |
---|
596 | args.push_back( maybeMoveBuild( expr_node2 ) ); |
---|
597 | return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args ); |
---|
598 | } // build_binary_val |
---|
599 | |
---|
600 | Expression * build_binary_ptr( OperKinds op, ExpressionNode * expr_node1, ExpressionNode * expr_node2 ) { |
---|
601 | list< Expression * > args; |
---|
602 | args.push_back( maybeMoveBuild( expr_node1 ) ); |
---|
603 | args.push_back( maybeMoveBuild( expr_node2 ) ); |
---|
604 | return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args ); |
---|
605 | } // build_binary_ptr |
---|
606 | |
---|
607 | Expression * build_cond( ExpressionNode * expr_node1, ExpressionNode * expr_node2, ExpressionNode * expr_node3 ) { |
---|
608 | return new ConditionalExpr( notZeroExpr( maybeMoveBuild( expr_node1 ) ), maybeMoveBuild( expr_node2 ), maybeMoveBuild( expr_node3 ) ); |
---|
609 | } // build_cond |
---|
610 | |
---|
611 | Expression * build_tuple( ExpressionNode * expr_node ) { |
---|
612 | list< Expression * > exprs; |
---|
613 | buildMoveList( expr_node, exprs ); |
---|
614 | return new UntypedTupleExpr( exprs );; |
---|
615 | } // build_tuple |
---|
616 | |
---|
617 | Expression * build_func( ExpressionNode * function, ExpressionNode * expr_node ) { |
---|
618 | list< Expression * > args; |
---|
619 | buildMoveList( expr_node, args ); |
---|
620 | return new UntypedExpr( maybeMoveBuild( function ), args ); |
---|
621 | } // build_func |
---|
622 | |
---|
623 | Expression * build_compoundLiteral( DeclarationNode * decl_node, InitializerNode * kids ) { |
---|
624 | Declaration * newDecl = maybeBuild( decl_node ); // compound literal type |
---|
625 | if ( DeclarationWithType * newDeclWithType = dynamic_cast< DeclarationWithType * >( newDecl ) ) { // non-sue compound-literal type |
---|
626 | return new CompoundLiteralExpr( newDeclWithType->get_type(), maybeMoveBuild( kids ) ); |
---|
627 | // these types do not have associated type information |
---|
628 | } else if ( StructDecl * newDeclStructDecl = dynamic_cast< StructDecl * >( newDecl ) ) { |
---|
629 | if ( newDeclStructDecl->has_body() ) { |
---|
630 | return new CompoundLiteralExpr( new StructInstType( Type::Qualifiers(), newDeclStructDecl ), maybeMoveBuild( kids ) ); |
---|
631 | } else { |
---|
632 | return new CompoundLiteralExpr( new StructInstType( Type::Qualifiers(), newDeclStructDecl->get_name() ), maybeMoveBuild( kids ) ); |
---|
633 | } // if |
---|
634 | } else if ( UnionDecl * newDeclUnionDecl = dynamic_cast< UnionDecl * >( newDecl ) ) { |
---|
635 | if ( newDeclUnionDecl->has_body() ) { |
---|
636 | return new CompoundLiteralExpr( new UnionInstType( Type::Qualifiers(), newDeclUnionDecl ), maybeMoveBuild( kids ) ); |
---|
637 | } else { |
---|
638 | return new CompoundLiteralExpr( new UnionInstType( Type::Qualifiers(), newDeclUnionDecl->get_name() ), maybeMoveBuild( kids ) ); |
---|
639 | } // if |
---|
640 | } else if ( EnumDecl * newDeclEnumDecl = dynamic_cast< EnumDecl * >( newDecl ) ) { |
---|
641 | if ( newDeclEnumDecl->has_body() ) { |
---|
642 | return new CompoundLiteralExpr( new EnumInstType( Type::Qualifiers(), newDeclEnumDecl ), maybeMoveBuild( kids ) ); |
---|
643 | } else { |
---|
644 | return new CompoundLiteralExpr( new EnumInstType( Type::Qualifiers(), newDeclEnumDecl->get_name() ), maybeMoveBuild( kids ) ); |
---|
645 | } // if |
---|
646 | } else { |
---|
647 | assert( false ); |
---|
648 | } // if |
---|
649 | } // build_compoundLiteral |
---|
650 | |
---|
651 | // Local Variables: // |
---|
652 | // tab-width: 4 // |
---|
653 | // End: // |
---|