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