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 : Andrew Beach |
---|
12 | // Last Modified On : Tue Apr 4 11:07:00 2023 |
---|
13 | // Update Count : 1083 |
---|
14 | // |
---|
15 | |
---|
16 | #include "ExpressionNode.h" |
---|
17 | |
---|
18 | #include <cassert> // for assert |
---|
19 | #include <stdio.h> // for sscanf, size_t |
---|
20 | #include <climits> // for LLONG_MAX, LONG_MAX, INT_MAX, UINT... |
---|
21 | #include <list> // for list |
---|
22 | #include <sstream> // for basic_istream::operator>>, basic_i... |
---|
23 | #include <string> // for string, operator+, operator== |
---|
24 | |
---|
25 | #include "AST/Expr.hpp" // for NameExpr |
---|
26 | #include "AST/Type.hpp" // for BaseType, SueInstType |
---|
27 | #include "Common/SemanticError.h" // for SemanticError |
---|
28 | #include "Common/utility.h" // for maybeMoveBuild, maybeBuild, CodeLo... |
---|
29 | #include "DeclarationNode.h" // for DeclarationNode |
---|
30 | #include "InitializerNode.h" // for InitializerNode |
---|
31 | #include "parserutility.h" // for notZeroExpr |
---|
32 | |
---|
33 | using namespace std; |
---|
34 | |
---|
35 | //############################################################################## |
---|
36 | |
---|
37 | // Difficult to separate extra parts of constants during lexing because actions are not allow in the middle of patterns: |
---|
38 | // |
---|
39 | // prefix action constant action suffix |
---|
40 | // |
---|
41 | // Alternatively, breaking a pattern using BEGIN does not work if the following pattern can be empty: |
---|
42 | // |
---|
43 | // constant BEGIN CONT ... |
---|
44 | // <CONT>(...)? BEGIN 0 ... // possible empty suffix |
---|
45 | // |
---|
46 | // because the CONT rule is NOT triggered if the pattern is empty. Hence, constants are reparsed here to determine their |
---|
47 | // type. |
---|
48 | |
---|
49 | // static inline bool checkH( char c ) { return c == 'h' || c == 'H'; } |
---|
50 | // static inline bool checkZ( char c ) { return c == 'z' || c == 'Z'; } |
---|
51 | // static inline bool checkU( char c ) { return c == 'u' || c == 'U'; } |
---|
52 | static inline bool checkF( char c ) { return c == 'f' || c == 'F'; } |
---|
53 | static inline bool checkD( char c ) { return c == 'd' || c == 'D'; } |
---|
54 | static inline bool checkF80( char c ) { return c == 'w' || c == 'W'; } |
---|
55 | static inline bool checkF128( char c ) { return c == 'q' || c == 'Q'; } |
---|
56 | static inline bool checkL( char c ) { return c == 'l' || c == 'L'; } |
---|
57 | static inline bool checkI( char c ) { return c == 'i' || c == 'I'; } |
---|
58 | static inline bool checkB( char c ) { return c == 'b' || c == 'B'; } |
---|
59 | static inline bool checkX( char c ) { return c == 'x' || c == 'X'; } |
---|
60 | // static inline bool checkN( char c ) { return c == 'n' || c == 'N'; } |
---|
61 | |
---|
62 | void lnthSuffix( string & str, int & type, int & ltype ) { |
---|
63 | // 'u' can appear before or after length suffix |
---|
64 | string::size_type posn = str.find_last_of( "lL" ); |
---|
65 | |
---|
66 | if ( posn == string::npos ) return; // no suffix |
---|
67 | size_t end = str.length() - 1; |
---|
68 | if ( posn == end ) { type = 3; return; } // no length after 'l' => long |
---|
69 | |
---|
70 | string::size_type next = posn + 1; // advance to length |
---|
71 | if ( str[next] == '3' ) { // 32 |
---|
72 | type = ltype = 2; |
---|
73 | } else if ( str[next] == '6' ) { // 64 |
---|
74 | type = ltype = 3; |
---|
75 | } else if ( str[next] == '8' ) { // 8 |
---|
76 | type = ltype = 1; |
---|
77 | } else if ( str[next] == '1' ) { |
---|
78 | if ( str[next + 1] == '6' ) { // 16 |
---|
79 | type = ltype = 0; |
---|
80 | } else { // 128 |
---|
81 | type = 5; ltype = 6; |
---|
82 | } // if |
---|
83 | } // if |
---|
84 | |
---|
85 | char fix = '\0'; |
---|
86 | if ( str[end] == 'u' || str[end] == 'U' ) fix = str[end]; // ends with 'uU' ? |
---|
87 | str.erase( posn ); // remove length suffix and possibly uU |
---|
88 | if ( type == 5 ) { // L128 does not need uU |
---|
89 | end = str.length() - 1; |
---|
90 | if ( str[end] == 'u' || str[end] == 'U' ) str.erase( end ); // ends with 'uU' ? remove |
---|
91 | } else if ( fix != '\0' ) str += fix; // put 'uU' back if removed |
---|
92 | } // lnthSuffix |
---|
93 | |
---|
94 | void valueToType( unsigned long long int & v, bool dec, int & type, bool & Unsigned ) { |
---|
95 | // use value to determine type |
---|
96 | if ( v <= INT_MAX ) { // signed int |
---|
97 | type = 2; |
---|
98 | } else if ( v <= UINT_MAX && ! dec ) { // unsigned int |
---|
99 | type = 2; |
---|
100 | Unsigned = true; // unsigned |
---|
101 | } else if ( v <= LONG_MAX ) { // signed long int |
---|
102 | type = 3; |
---|
103 | } else if ( v <= ULONG_MAX && ( ! dec || LONG_MAX == LLONG_MAX ) ) { // signed long int |
---|
104 | type = 3; |
---|
105 | Unsigned = true; // unsigned long int |
---|
106 | } else if ( v <= LLONG_MAX ) { // signed long long int |
---|
107 | type = 4; |
---|
108 | } else { // unsigned long long int |
---|
109 | type = 4; |
---|
110 | Unsigned = true; // unsigned long long int |
---|
111 | } // if |
---|
112 | } // valueToType |
---|
113 | |
---|
114 | static void scanbin( string & str, unsigned long long int & v ) { |
---|
115 | v = 0; |
---|
116 | size_t last = str.length() - 1; // last subscript of constant |
---|
117 | for ( unsigned int i = 2;; ) { // ignore prefix |
---|
118 | if ( str[i] == '1' ) v |= 1; |
---|
119 | i += 1; |
---|
120 | if ( i == last - 1 || (str[i] != '0' && str[i] != '1') ) break; |
---|
121 | v <<= 1; |
---|
122 | } // for |
---|
123 | } // scanbin |
---|
124 | |
---|
125 | ast::Expr * build_constantInteger( |
---|
126 | const CodeLocation & location, string & str ) { |
---|
127 | static const ast::BasicType::Kind kind[2][6] = { |
---|
128 | // short (h) must be before char (hh) because shorter type has the longer suffix |
---|
129 | { ast::BasicType::ShortSignedInt, ast::BasicType::SignedChar, ast::BasicType::SignedInt, ast::BasicType::LongSignedInt, ast::BasicType::LongLongSignedInt, /* BasicType::SignedInt128 */ ast::BasicType::LongLongSignedInt, }, |
---|
130 | { ast::BasicType::ShortUnsignedInt, ast::BasicType::UnsignedChar, ast::BasicType::UnsignedInt, ast::BasicType::LongUnsignedInt, ast::BasicType::LongLongUnsignedInt, /* BasicType::UnsignedInt128 */ ast::BasicType::LongLongUnsignedInt, }, |
---|
131 | }; |
---|
132 | |
---|
133 | static const char * lnthsInt[2][6] = { |
---|
134 | { "int16_t", "int8_t", "int32_t", "int64_t", "size_t", "uintptr_t", }, |
---|
135 | { "uint16_t", "uint8_t", "uint32_t", "uint64_t", "size_t", "uintptr_t", }, |
---|
136 | }; // lnthsInt |
---|
137 | |
---|
138 | string str2( "0x0" ); |
---|
139 | unsigned long long int v, v2 = 0; // converted integral value |
---|
140 | ast::Expr * ret, * ret2; |
---|
141 | |
---|
142 | int type = -1; // 0 => short, 1 => char, 2 => int, 3 => long int, 4 => long long int, 5 => int128 |
---|
143 | int ltype = -1; // 0 => 16 bits, 1 => 8 bits, 2 => 32 bits, 3 => 64 bits, 4 => size_t, 5 => intptr, 6 => pointer |
---|
144 | bool dec = true, Unsigned = false; // decimal, unsigned constant |
---|
145 | |
---|
146 | // special constants |
---|
147 | if ( str == "0" ) { |
---|
148 | ret = new ast::ConstantExpr( location, new ast::ZeroType(), str, 0 ); |
---|
149 | goto CLEANUP; |
---|
150 | } // if |
---|
151 | if ( str == "1" ) { |
---|
152 | ret = new ast::ConstantExpr( location, new ast::OneType(), str, 1 ); |
---|
153 | goto CLEANUP; |
---|
154 | } // if |
---|
155 | |
---|
156 | // 'u' can appear before or after length suffix |
---|
157 | if ( str.find_last_of( "uU" ) != string::npos ) Unsigned = true; |
---|
158 | |
---|
159 | if ( isdigit( str[str.length() - 1] ) ) { // no suffix ? |
---|
160 | lnthSuffix( str, type, ltype ); // could have length suffix |
---|
161 | } else { |
---|
162 | // At least one digit in integer constant, so safe to backup while looking for suffix. |
---|
163 | // This declaration and the comma expressions in the conditions mimic |
---|
164 | // the declare and check pattern allowed in later compiler versions. |
---|
165 | // (Only some early compilers/C++ standards do not support it.) |
---|
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 ast::ConstantExpr( location, |
---|
304 | new ast::BasicType( kind[Unsigned][type] ), str, v ); |
---|
305 | if ( Unsigned && type < 2 ) { // hh or h, less than int ? |
---|
306 | // int i = -1uh => 65535 not -1, so cast is necessary for unsigned, which unfortunately eliminates warnings for large values. |
---|
307 | ret = new ast::CastExpr( location, |
---|
308 | ret, |
---|
309 | new ast::BasicType( kind[Unsigned][type] ), |
---|
310 | ast::ExplicitCast ); |
---|
311 | } else if ( ltype != -1 ) { // explicit length ? |
---|
312 | if ( ltype == 6 ) { // int128, (int128)constant |
---|
313 | ret2 = new ast::ConstantExpr( location, |
---|
314 | new ast::BasicType( ast::BasicType::LongLongSignedInt ), |
---|
315 | str2, |
---|
316 | v2 ); |
---|
317 | ret = build_compoundLiteral( location, |
---|
318 | DeclarationNode::newBasicType( |
---|
319 | DeclarationNode::Int128 |
---|
320 | )->addType( |
---|
321 | DeclarationNode::newSignedNess( DeclarationNode::Unsigned ) ), |
---|
322 | new InitializerNode( |
---|
323 | (InitializerNode *)(new InitializerNode( new ExpressionNode( v2 == 0 ? ret2 : ret ) ))->set_last( new InitializerNode( new ExpressionNode( v2 == 0 ? ret : ret2 ) ) ), true ) |
---|
324 | ); |
---|
325 | } else { // explicit length, (length_type)constant |
---|
326 | ret = new ast::CastExpr( location, |
---|
327 | ret, |
---|
328 | new ast::TypeInstType( lnthsInt[Unsigned][ltype], ast::TypeDecl::Dtype ), |
---|
329 | ast::ExplicitCast ); |
---|
330 | if ( ltype == 5 ) { // pointer, intptr( (uintptr_t)constant ) |
---|
331 | ret = build_func( location, |
---|
332 | new ExpressionNode( |
---|
333 | build_varref( location, new string( "intptr" ) ) ), |
---|
334 | new ExpressionNode( ret ) ); |
---|
335 | } // if |
---|
336 | } // if |
---|
337 | } // if |
---|
338 | |
---|
339 | CLEANUP: ; |
---|
340 | delete &str; // created by lex |
---|
341 | return ret; |
---|
342 | } // build_constantInteger |
---|
343 | |
---|
344 | |
---|
345 | static inline void checkFnxFloat( string & str, size_t last, bool & explnth, int & type ) { |
---|
346 | string::size_type posn; |
---|
347 | // floating-point constant has minimum of 2 characters, 1. or .1, so safe to look ahead |
---|
348 | if ( str[1] == 'x' ) { // hex ? |
---|
349 | posn = str.find_last_of( "pP" ); // back for exponent (must have) |
---|
350 | posn = str.find_first_of( "fF", posn + 1 ); // forward for size (fF allowed in hex constant) |
---|
351 | } else { |
---|
352 | posn = str.find_last_of( "fF" ); // back for size (fF not allowed) |
---|
353 | } // if |
---|
354 | if ( posn == string::npos ) return; |
---|
355 | explnth = true; |
---|
356 | posn += 1; // advance to size |
---|
357 | if ( str[posn] == '3' ) { // 32 |
---|
358 | if ( str[last] != 'x' ) type = 6; |
---|
359 | else type = 7; |
---|
360 | } else if ( str[posn] == '6' ) { // 64 |
---|
361 | if ( str[last] != 'x' ) type = 8; |
---|
362 | else type = 9; |
---|
363 | } else if ( str[posn] == '8' ) { // 80 |
---|
364 | type = 3; |
---|
365 | } else if ( str[posn] == '1' ) { // 16/128 |
---|
366 | if ( str[posn + 1] == '6' ) { // 16 |
---|
367 | type = 5; |
---|
368 | } else { // 128 |
---|
369 | if ( str[last] != 'x' ) type = 10; |
---|
370 | else type = 11; |
---|
371 | } // if |
---|
372 | } else { |
---|
373 | assertf( false, "internal error, bad floating point length %s", str.c_str() ); |
---|
374 | } // if |
---|
375 | } // checkFnxFloat |
---|
376 | |
---|
377 | |
---|
378 | ast::Expr * build_constantFloat( |
---|
379 | const CodeLocation & location, string & str ) { |
---|
380 | static const ast::BasicType::Kind kind[2][12] = { |
---|
381 | { ast::BasicType::Float, ast::BasicType::Double, ast::BasicType::LongDouble, ast::BasicType::uuFloat80, ast::BasicType::uuFloat128, ast::BasicType::uFloat16, ast::BasicType::uFloat32, ast::BasicType::uFloat32x, ast::BasicType::uFloat64, ast::BasicType::uFloat64x, ast::BasicType::uFloat128, ast::BasicType::uFloat128x }, |
---|
382 | { ast::BasicType::FloatComplex, ast::BasicType::DoubleComplex, ast::BasicType::LongDoubleComplex, ast::BasicType::NUMBER_OF_BASIC_TYPES, ast::BasicType::NUMBER_OF_BASIC_TYPES, ast::BasicType::uFloat16Complex, ast::BasicType::uFloat32Complex, ast::BasicType::uFloat32xComplex, ast::BasicType::uFloat64Complex, ast::BasicType::uFloat64xComplex, ast::BasicType::uFloat128Complex, ast::BasicType::uFloat128xComplex }, |
---|
383 | }; |
---|
384 | |
---|
385 | // floating-point constant has minimum of 2 characters 1. or .1 |
---|
386 | size_t last = str.length() - 1; |
---|
387 | double v; |
---|
388 | int type; // 0 => float, 1 => double, 3 => long double, ... |
---|
389 | bool complx = false; // real, complex |
---|
390 | bool explnth = false; // explicit literal length |
---|
391 | |
---|
392 | sscanf( str.c_str(), "%lg", &v ); |
---|
393 | |
---|
394 | if ( checkI( str[last] ) ) { // imaginary ? |
---|
395 | complx = true; |
---|
396 | last -= 1; // backup one character |
---|
397 | } // if |
---|
398 | |
---|
399 | if ( checkF( str[last] ) ) { // float ? |
---|
400 | type = 0; |
---|
401 | } else if ( checkD( str[last] ) ) { // double ? |
---|
402 | type = 1; |
---|
403 | } else if ( checkL( str[last] ) ) { // long double ? |
---|
404 | type = 2; |
---|
405 | } else if ( checkF80( str[last] ) ) { // __float80 ? |
---|
406 | type = 3; |
---|
407 | } else if ( checkF128( str[last] ) ) { // __float128 ? |
---|
408 | type = 4; |
---|
409 | } else { |
---|
410 | type = 1; // double (default if no suffix) |
---|
411 | checkFnxFloat( str, last, explnth, type ); |
---|
412 | } // if |
---|
413 | |
---|
414 | if ( ! complx && checkI( str[last - 1] ) ) { // imaginary ? |
---|
415 | complx = true; |
---|
416 | } // if |
---|
417 | |
---|
418 | assert( 0 <= type && type < 12 ); |
---|
419 | ast::Expr * ret = new ast::ConstantExpr( location, |
---|
420 | new ast::BasicType( kind[complx][type] ), |
---|
421 | str, |
---|
422 | v ); |
---|
423 | // explicit length ? |
---|
424 | if ( explnth ) { |
---|
425 | ret = new ast::CastExpr( location, |
---|
426 | ret, |
---|
427 | new ast::BasicType( kind[complx][type] ), |
---|
428 | ast::ExplicitCast ); |
---|
429 | } // if |
---|
430 | |
---|
431 | delete &str; // created by lex |
---|
432 | return ret; |
---|
433 | } // build_constantFloat |
---|
434 | |
---|
435 | static void sepString( string & str, string & units, char delimit ) { |
---|
436 | string::size_type posn = str.find_last_of( delimit ) + 1; |
---|
437 | if ( posn != str.length() ) { |
---|
438 | units = "?" + str.substr( posn ); // extract units |
---|
439 | str.erase( posn ); // remove units |
---|
440 | } // if |
---|
441 | } // sepString |
---|
442 | |
---|
443 | ast::Expr * build_constantChar( const CodeLocation & location, string & str ) { |
---|
444 | string units; // units |
---|
445 | sepString( str, units, '\'' ); // separate constant from units |
---|
446 | |
---|
447 | ast::Expr * ret = new ast::ConstantExpr( location, |
---|
448 | new ast::BasicType( ast::BasicType::Char ), |
---|
449 | str, |
---|
450 | (unsigned long long int)(unsigned char)str[1] ); |
---|
451 | if ( units.length() != 0 ) { |
---|
452 | ret = new ast::UntypedExpr( location, |
---|
453 | new ast::NameExpr( location, units ), |
---|
454 | { ret } ); |
---|
455 | } // if |
---|
456 | |
---|
457 | delete &str; // created by lex |
---|
458 | return ret; |
---|
459 | } // build_constantChar |
---|
460 | |
---|
461 | ast::Expr * build_constantStr( |
---|
462 | const CodeLocation & location, |
---|
463 | string & str ) { |
---|
464 | assert( str.length() > 0 ); |
---|
465 | string units; // units |
---|
466 | sepString( str, units, '"' ); // separate constant from units |
---|
467 | |
---|
468 | ast::Type * strtype; |
---|
469 | switch ( str[0] ) { // str has >= 2 characters, i.e, null string "" => safe to look at subscripts 0/1 |
---|
470 | case 'u': |
---|
471 | if ( str[1] == '8' ) goto Default; // utf-8 characters => array of char |
---|
472 | // lookup type of associated typedef |
---|
473 | strtype = new ast::TypeInstType( "char16_t", ast::TypeDecl::Dtype ); |
---|
474 | break; |
---|
475 | case 'U': |
---|
476 | strtype = new ast::TypeInstType( "char32_t", ast::TypeDecl::Dtype ); |
---|
477 | break; |
---|
478 | case 'L': |
---|
479 | strtype = new ast::TypeInstType( "wchar_t", ast::TypeDecl::Dtype ); |
---|
480 | break; |
---|
481 | Default: // char default string type |
---|
482 | default: |
---|
483 | strtype = new ast::BasicType( ast::BasicType::Char ); |
---|
484 | } // switch |
---|
485 | ast::ArrayType * at = new ast::ArrayType( |
---|
486 | strtype, |
---|
487 | // Length is adjusted: +1 for '\0' and -2 for '"' |
---|
488 | ast::ConstantExpr::from_ulong( location, str.size() + 1 - 2 ), |
---|
489 | ast::FixedLen, |
---|
490 | ast::DynamicDim ); |
---|
491 | ast::Expr * ret = new ast::ConstantExpr( location, at, str, std::nullopt ); |
---|
492 | if ( units.length() != 0 ) { |
---|
493 | ret = new ast::UntypedExpr( location, |
---|
494 | new ast::NameExpr( location, units ), |
---|
495 | { ret } ); |
---|
496 | } // if |
---|
497 | |
---|
498 | delete &str; // created by lex |
---|
499 | return ret; |
---|
500 | } // build_constantStr |
---|
501 | |
---|
502 | ast::Expr * build_field_name_FLOATING_FRACTIONconstant( |
---|
503 | const CodeLocation & location, const string & str ) { |
---|
504 | if ( str.find_first_not_of( "0123456789", 1 ) != string::npos ) SemanticError( yylloc, "invalid tuple index " + str ); |
---|
505 | ast::Expr * ret = build_constantInteger( location, |
---|
506 | *new string( str.substr(1) ) ); |
---|
507 | delete &str; |
---|
508 | return ret; |
---|
509 | } // build_field_name_FLOATING_FRACTIONconstant |
---|
510 | |
---|
511 | ast::Expr * build_field_name_FLOATING_DECIMALconstant( |
---|
512 | const CodeLocation & location, const string & str ) { |
---|
513 | if ( str[str.size() - 1] != '.' ) SemanticError( yylloc, "invalid tuple index " + str ); |
---|
514 | ast::Expr * ret = build_constantInteger( |
---|
515 | location, *new string( str.substr( 0, str.size()-1 ) ) ); |
---|
516 | delete &str; |
---|
517 | return ret; |
---|
518 | } // build_field_name_FLOATING_DECIMALconstant |
---|
519 | |
---|
520 | ast::Expr * build_field_name_FLOATINGconstant( const CodeLocation & location, |
---|
521 | const string & str ) { |
---|
522 | // str is of the form A.B -> separate at the . and return member expression |
---|
523 | int a, b; |
---|
524 | char dot; |
---|
525 | stringstream ss( str ); |
---|
526 | ss >> a >> dot >> b; |
---|
527 | auto ret = new ast::UntypedMemberExpr( location, |
---|
528 | ast::ConstantExpr::from_int( location, b ), |
---|
529 | ast::ConstantExpr::from_int( location, a ) |
---|
530 | ); |
---|
531 | delete &str; |
---|
532 | return ret; |
---|
533 | } // build_field_name_FLOATINGconstant |
---|
534 | |
---|
535 | ast::Expr * make_field_name_fraction_constants( const CodeLocation & location, |
---|
536 | ast::Expr * fieldName, |
---|
537 | ast::Expr * fracts ) { |
---|
538 | if ( nullptr == fracts ) { |
---|
539 | return fieldName; |
---|
540 | } else if ( auto memberExpr = dynamic_cast<ast::UntypedMemberExpr *>( fracts ) ) { |
---|
541 | memberExpr->member = make_field_name_fraction_constants( location, |
---|
542 | fieldName, |
---|
543 | ast::mutate( memberExpr->aggregate.get() ) ); |
---|
544 | return memberExpr; |
---|
545 | } else { |
---|
546 | return new ast::UntypedMemberExpr( location, fracts, fieldName ); |
---|
547 | } // if |
---|
548 | } // make_field_name_fraction_constants |
---|
549 | |
---|
550 | ast::Expr * build_field_name_fraction_constants( const CodeLocation & location, |
---|
551 | ast::Expr * fieldName, |
---|
552 | ExpressionNode * fracts ) { |
---|
553 | return make_field_name_fraction_constants( location, fieldName, maybeMoveBuild( fracts ) ); |
---|
554 | } // build_field_name_fraction_constants |
---|
555 | |
---|
556 | ast::NameExpr * build_varref( const CodeLocation & location, |
---|
557 | const string * name ) { |
---|
558 | ast::NameExpr * expr = new ast::NameExpr( location, *name ); |
---|
559 | delete name; |
---|
560 | return expr; |
---|
561 | } // build_varref |
---|
562 | |
---|
563 | ast::QualifiedNameExpr * build_qualified_expr( const CodeLocation & location, |
---|
564 | const DeclarationNode * decl_node, |
---|
565 | const ast::NameExpr * name ) { |
---|
566 | ast::Decl * newDecl = maybeBuild( decl_node ); |
---|
567 | if ( ast::DeclWithType * newDeclWithType = dynamic_cast<ast::DeclWithType *>( newDecl ) ) { |
---|
568 | if ( const ast::Type * t = newDeclWithType->get_type() ) { |
---|
569 | if ( auto typeInst = dynamic_cast<const ast::TypeInstType *>( t ) ) { |
---|
570 | newDecl = new ast::EnumDecl( location, typeInst->name ); |
---|
571 | } |
---|
572 | } |
---|
573 | } |
---|
574 | return new ast::QualifiedNameExpr( location, newDecl, name->name ); |
---|
575 | } |
---|
576 | |
---|
577 | ast::QualifiedNameExpr * build_qualified_expr( const CodeLocation & location, |
---|
578 | const ast::EnumDecl * decl, |
---|
579 | const ast::NameExpr * name ) { |
---|
580 | return new ast::QualifiedNameExpr( location, decl, name->name ); |
---|
581 | } |
---|
582 | |
---|
583 | ast::DimensionExpr * build_dimensionref( const CodeLocation & location, |
---|
584 | const string * name ) { |
---|
585 | ast::DimensionExpr * expr = new ast::DimensionExpr( location, *name ); |
---|
586 | delete name; |
---|
587 | return expr; |
---|
588 | } // build_varref |
---|
589 | |
---|
590 | // TODO: get rid of this and OperKinds and reuse code from OperatorTable |
---|
591 | static const char * OperName[] = { // must harmonize with OperKinds |
---|
592 | // diadic |
---|
593 | "SizeOf", "AlignOf", "OffsetOf", "?+?", "?-?", "?\\?", "?*?", "?/?", "?%?", "||", "&&", |
---|
594 | "?|?", "?&?", "?^?", "Cast", "?<<?", "?>>?", "?<?", "?>?", "?<=?", "?>=?", "?==?", "?!=?", |
---|
595 | "?=?", "?@=?", "?\\=?", "?*=?", "?/=?", "?%=?", "?+=?", "?-=?", "?<<=?", "?>>=?", "?&=?", "?^=?", "?|=?", |
---|
596 | "?[?]", "...", |
---|
597 | // monadic |
---|
598 | "+?", "-?", "AddressOf", "*?", "!?", "~?", "++?", "?++", "--?", "?--", |
---|
599 | }; // OperName |
---|
600 | |
---|
601 | ast::Expr * build_cast( const CodeLocation & location, |
---|
602 | DeclarationNode * decl_node, |
---|
603 | ExpressionNode * expr_node ) { |
---|
604 | ast::Type * targetType = maybeMoveBuildType( decl_node ); |
---|
605 | if ( dynamic_cast<ast::VoidType *>( targetType ) ) { |
---|
606 | delete targetType; |
---|
607 | return new ast::CastExpr( location, |
---|
608 | maybeMoveBuild( expr_node ), |
---|
609 | ast::ExplicitCast ); |
---|
610 | } else { |
---|
611 | return new ast::CastExpr( location, |
---|
612 | maybeMoveBuild( expr_node ), |
---|
613 | targetType, |
---|
614 | ast::ExplicitCast ); |
---|
615 | } // if |
---|
616 | } // build_cast |
---|
617 | |
---|
618 | ast::Expr * build_keyword_cast( const CodeLocation & location, |
---|
619 | ast::AggregateDecl::Aggregate target, |
---|
620 | ExpressionNode * expr_node ) { |
---|
621 | return new ast::KeywordCastExpr( location, |
---|
622 | maybeMoveBuild( expr_node ), |
---|
623 | target |
---|
624 | ); |
---|
625 | } |
---|
626 | |
---|
627 | ast::Expr * build_virtual_cast( const CodeLocation & location, |
---|
628 | DeclarationNode * decl_node, |
---|
629 | ExpressionNode * expr_node ) { |
---|
630 | return new ast::VirtualCastExpr( location, |
---|
631 | maybeMoveBuild( expr_node ), |
---|
632 | maybeMoveBuildType( decl_node ) |
---|
633 | ); |
---|
634 | } // build_virtual_cast |
---|
635 | |
---|
636 | ast::Expr * build_fieldSel( const CodeLocation & location, |
---|
637 | ExpressionNode * expr_node, |
---|
638 | ast::Expr * member ) { |
---|
639 | return new ast::UntypedMemberExpr( location, |
---|
640 | member, |
---|
641 | maybeMoveBuild( expr_node ) |
---|
642 | ); |
---|
643 | } // build_fieldSel |
---|
644 | |
---|
645 | ast::Expr * build_pfieldSel( const CodeLocation & location, |
---|
646 | ExpressionNode * expr_node, |
---|
647 | ast::Expr * member ) { |
---|
648 | auto deref = new ast::UntypedExpr( location, |
---|
649 | new ast::NameExpr( location, "*?" ) |
---|
650 | ); |
---|
651 | deref->location = expr_node->location; |
---|
652 | deref->args.push_back( maybeMoveBuild( expr_node ) ); |
---|
653 | auto ret = new ast::UntypedMemberExpr( location, member, deref ); |
---|
654 | return ret; |
---|
655 | } // build_pfieldSel |
---|
656 | |
---|
657 | ast::Expr * build_offsetOf( const CodeLocation & location, |
---|
658 | DeclarationNode * decl_node, |
---|
659 | ast::NameExpr * member ) { |
---|
660 | ast::Expr * ret = new ast::UntypedOffsetofExpr( location, |
---|
661 | maybeMoveBuildType( decl_node ), |
---|
662 | member->name |
---|
663 | ); |
---|
664 | ret->result = new ast::BasicType( ast::BasicType::LongUnsignedInt ); |
---|
665 | delete member; |
---|
666 | return ret; |
---|
667 | } // build_offsetOf |
---|
668 | |
---|
669 | ast::Expr * build_and_or( const CodeLocation & location, |
---|
670 | ExpressionNode * expr_node1, |
---|
671 | ExpressionNode * expr_node2, |
---|
672 | ast::LogicalFlag flag ) { |
---|
673 | return new ast::LogicalExpr( location, |
---|
674 | notZeroExpr( maybeMoveBuild( expr_node1 ) ), |
---|
675 | notZeroExpr( maybeMoveBuild( expr_node2 ) ), |
---|
676 | flag |
---|
677 | ); |
---|
678 | } // build_and_or |
---|
679 | |
---|
680 | ast::Expr * build_unary_val( const CodeLocation & location, |
---|
681 | OperKinds op, |
---|
682 | ExpressionNode * expr_node ) { |
---|
683 | std::vector<ast::ptr<ast::Expr>> args; |
---|
684 | args.push_back( maybeMoveBuild( expr_node ) ); |
---|
685 | return new ast::UntypedExpr( location, |
---|
686 | new ast::NameExpr( location, OperName[ (int)op ] ), |
---|
687 | std::move( args ) |
---|
688 | ); |
---|
689 | } // build_unary_val |
---|
690 | |
---|
691 | ast::Expr * build_binary_val( const CodeLocation & location, |
---|
692 | OperKinds op, |
---|
693 | ExpressionNode * expr_node1, |
---|
694 | ExpressionNode * expr_node2 ) { |
---|
695 | std::vector<ast::ptr<ast::Expr>> args; |
---|
696 | args.push_back( maybeMoveBuild( expr_node1 ) ); |
---|
697 | args.push_back( maybeMoveBuild( expr_node2 ) ); |
---|
698 | return new ast::UntypedExpr( location, |
---|
699 | new ast::NameExpr( location, OperName[ (int)op ] ), |
---|
700 | std::move( args ) |
---|
701 | ); |
---|
702 | } // build_binary_val |
---|
703 | |
---|
704 | ast::Expr * build_cond( const CodeLocation & location, |
---|
705 | ExpressionNode * expr_node1, |
---|
706 | ExpressionNode * expr_node2, |
---|
707 | ExpressionNode * expr_node3 ) { |
---|
708 | return new ast::ConditionalExpr( location, |
---|
709 | notZeroExpr( maybeMoveBuild( expr_node1 ) ), |
---|
710 | maybeMoveBuild( expr_node2 ), |
---|
711 | maybeMoveBuild( expr_node3 ) |
---|
712 | ); |
---|
713 | } // build_cond |
---|
714 | |
---|
715 | ast::Expr * build_tuple( const CodeLocation & location, |
---|
716 | ExpressionNode * expr_node ) { |
---|
717 | std::vector<ast::ptr<ast::Expr>> exprs; |
---|
718 | buildMoveList( expr_node, exprs ); |
---|
719 | return new ast::UntypedTupleExpr( location, std::move( exprs ) ); |
---|
720 | } // build_tuple |
---|
721 | |
---|
722 | ast::Expr * build_func( const CodeLocation & location, |
---|
723 | ExpressionNode * function, |
---|
724 | ExpressionNode * expr_node ) { |
---|
725 | std::vector<ast::ptr<ast::Expr>> args; |
---|
726 | buildMoveList( expr_node, args ); |
---|
727 | return new ast::UntypedExpr( location, |
---|
728 | maybeMoveBuild( function ), |
---|
729 | std::move( args ) |
---|
730 | ); |
---|
731 | } // build_func |
---|
732 | |
---|
733 | ast::Expr * build_compoundLiteral( const CodeLocation & location, |
---|
734 | DeclarationNode * decl_node, |
---|
735 | InitializerNode * kids ) { |
---|
736 | // compound literal type |
---|
737 | ast::Decl * newDecl = maybeBuild( decl_node ); |
---|
738 | // non-sue compound-literal type |
---|
739 | if ( ast::DeclWithType * newDeclWithType = dynamic_cast<ast::DeclWithType *>( newDecl ) ) { |
---|
740 | return new ast::CompoundLiteralExpr( location, |
---|
741 | newDeclWithType->get_type(), |
---|
742 | maybeMoveBuild( kids ) ); |
---|
743 | // these types do not have associated type information |
---|
744 | } else if ( auto newDeclStructDecl = dynamic_cast<ast::StructDecl *>( newDecl ) ) { |
---|
745 | if ( newDeclStructDecl->body ) { |
---|
746 | return new ast::CompoundLiteralExpr( location, |
---|
747 | new ast::StructInstType( newDeclStructDecl ), |
---|
748 | maybeMoveBuild( kids ) ); |
---|
749 | } else { |
---|
750 | return new ast::CompoundLiteralExpr( location, |
---|
751 | new ast::StructInstType( newDeclStructDecl->name ), |
---|
752 | maybeMoveBuild( kids ) ); |
---|
753 | } // if |
---|
754 | } else if ( auto newDeclUnionDecl = dynamic_cast<ast::UnionDecl *>( newDecl ) ) { |
---|
755 | if ( newDeclUnionDecl->body ) { |
---|
756 | return new ast::CompoundLiteralExpr( location, |
---|
757 | new ast::UnionInstType( newDeclUnionDecl ), |
---|
758 | maybeMoveBuild( kids ) ); |
---|
759 | } else { |
---|
760 | return new ast::CompoundLiteralExpr( location, |
---|
761 | new ast::UnionInstType( newDeclUnionDecl->name ), |
---|
762 | maybeMoveBuild( kids ) ); |
---|
763 | } // if |
---|
764 | } else if ( auto newDeclEnumDecl = dynamic_cast<ast::EnumDecl *>( newDecl ) ) { |
---|
765 | if ( newDeclEnumDecl->body ) { |
---|
766 | return new ast::CompoundLiteralExpr( location, |
---|
767 | new ast::EnumInstType( newDeclEnumDecl ), |
---|
768 | maybeMoveBuild( kids ) ); |
---|
769 | } else { |
---|
770 | return new ast::CompoundLiteralExpr( location, |
---|
771 | new ast::EnumInstType( newDeclEnumDecl->name ), |
---|
772 | maybeMoveBuild( kids ) ); |
---|
773 | } // if |
---|
774 | } else { |
---|
775 | assert( false ); |
---|
776 | } // if |
---|
777 | } // build_compoundLiteral |
---|
778 | |
---|
779 | // Local Variables: // |
---|
780 | // tab-width: 4 // |
---|
781 | // End: // |
---|