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 : Thu Dec 14 18:57:07 2023 |
---|
13 | // Update Count : 1087 |
---|
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 \"%s\"", str.c_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 \"%s\"", str.c_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 \"%s\".", str.c_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 \"%s\"", str.c_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 \"%s\".", str.c_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 \"%s\".", str.c_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 \"%s\".", str.c_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::CastExpr::CastKind kind ) { |
---|
605 | ast::Type * targetType = maybeMoveBuildType( decl_node ); |
---|
606 | if ( dynamic_cast<ast::VoidType *>( targetType ) ) { |
---|
607 | delete targetType; |
---|
608 | return new ast::CastExpr( location, |
---|
609 | maybeMoveBuild( expr_node ), |
---|
610 | ast::ExplicitCast, kind ); |
---|
611 | } else { |
---|
612 | return new ast::CastExpr( location, |
---|
613 | maybeMoveBuild( expr_node ), |
---|
614 | targetType, |
---|
615 | ast::ExplicitCast, kind ); |
---|
616 | } // if |
---|
617 | } // build_cast |
---|
618 | |
---|
619 | ast::Expr * build_keyword_cast( const CodeLocation & location, |
---|
620 | ast::AggregateDecl::Aggregate target, |
---|
621 | ExpressionNode * expr_node ) { |
---|
622 | return new ast::KeywordCastExpr( location, |
---|
623 | maybeMoveBuild( expr_node ), |
---|
624 | target |
---|
625 | ); |
---|
626 | } |
---|
627 | |
---|
628 | ast::Expr * build_virtual_cast( const CodeLocation & location, |
---|
629 | DeclarationNode * decl_node, |
---|
630 | ExpressionNode * expr_node ) { |
---|
631 | return new ast::VirtualCastExpr( location, |
---|
632 | maybeMoveBuild( expr_node ), |
---|
633 | maybeMoveBuildType( decl_node ) |
---|
634 | ); |
---|
635 | } // build_virtual_cast |
---|
636 | |
---|
637 | ast::Expr * build_fieldSel( const CodeLocation & location, |
---|
638 | ExpressionNode * expr_node, |
---|
639 | ast::Expr * member ) { |
---|
640 | return new ast::UntypedMemberExpr( location, |
---|
641 | member, |
---|
642 | maybeMoveBuild( expr_node ) |
---|
643 | ); |
---|
644 | } // build_fieldSel |
---|
645 | |
---|
646 | ast::Expr * build_pfieldSel( const CodeLocation & location, |
---|
647 | ExpressionNode * expr_node, |
---|
648 | ast::Expr * member ) { |
---|
649 | auto deref = new ast::UntypedExpr( location, |
---|
650 | new ast::NameExpr( location, "*?" ) |
---|
651 | ); |
---|
652 | deref->location = expr_node->location; |
---|
653 | deref->args.push_back( maybeMoveBuild( expr_node ) ); |
---|
654 | auto ret = new ast::UntypedMemberExpr( location, member, deref ); |
---|
655 | return ret; |
---|
656 | } // build_pfieldSel |
---|
657 | |
---|
658 | ast::Expr * build_offsetOf( const CodeLocation & location, |
---|
659 | DeclarationNode * decl_node, |
---|
660 | ast::NameExpr * member ) { |
---|
661 | ast::Expr * ret = new ast::UntypedOffsetofExpr( location, |
---|
662 | maybeMoveBuildType( decl_node ), |
---|
663 | member->name |
---|
664 | ); |
---|
665 | ret->result = new ast::BasicType( ast::BasicType::LongUnsignedInt ); |
---|
666 | delete member; |
---|
667 | return ret; |
---|
668 | } // build_offsetOf |
---|
669 | |
---|
670 | ast::Expr * build_and_or( const CodeLocation & location, |
---|
671 | ExpressionNode * expr_node1, |
---|
672 | ExpressionNode * expr_node2, |
---|
673 | ast::LogicalFlag flag ) { |
---|
674 | return new ast::LogicalExpr( location, |
---|
675 | maybeMoveBuild( expr_node1 ), |
---|
676 | maybeMoveBuild( expr_node2 ), |
---|
677 | flag |
---|
678 | ); |
---|
679 | } // build_and_or |
---|
680 | |
---|
681 | ast::Expr * build_unary_val( const CodeLocation & location, |
---|
682 | OperKinds op, |
---|
683 | ExpressionNode * expr_node ) { |
---|
684 | std::vector<ast::ptr<ast::Expr>> args; |
---|
685 | args.push_back( maybeMoveBuild( expr_node ) ); |
---|
686 | return new ast::UntypedExpr( location, |
---|
687 | new ast::NameExpr( location, OperName[ (int)op ] ), |
---|
688 | std::move( args ) |
---|
689 | ); |
---|
690 | } // build_unary_val |
---|
691 | |
---|
692 | ast::Expr * build_enum_pos_expr( const CodeLocation & location, ast::Expr * expr_node ) { |
---|
693 | // return nullptr |
---|
694 | return new ast::EnumPosExpr( location, std::move( expr_node ) ); |
---|
695 | } |
---|
696 | |
---|
697 | ast::Expr * build_binary_val( const CodeLocation & location, |
---|
698 | OperKinds op, |
---|
699 | ExpressionNode * expr_node1, |
---|
700 | ExpressionNode * expr_node2 ) { |
---|
701 | std::vector<ast::ptr<ast::Expr>> args; |
---|
702 | args.push_back( maybeMoveBuild( expr_node1 ) ); |
---|
703 | args.push_back( maybeMoveBuild( expr_node2 ) ); |
---|
704 | return new ast::UntypedExpr( location, |
---|
705 | new ast::NameExpr( location, OperName[ (int)op ] ), |
---|
706 | std::move( args ) |
---|
707 | ); |
---|
708 | } // build_binary_val |
---|
709 | |
---|
710 | ast::Expr * build_cond( const CodeLocation & location, |
---|
711 | ExpressionNode * expr_node1, |
---|
712 | ExpressionNode * expr_node2, |
---|
713 | ExpressionNode * expr_node3 ) { |
---|
714 | return new ast::ConditionalExpr( location, |
---|
715 | maybeMoveBuild( expr_node1 ), |
---|
716 | maybeMoveBuild( expr_node2 ), |
---|
717 | maybeMoveBuild( expr_node3 ) |
---|
718 | ); |
---|
719 | } // build_cond |
---|
720 | |
---|
721 | ast::Expr * build_tuple( const CodeLocation & location, |
---|
722 | ExpressionNode * expr_node ) { |
---|
723 | std::vector<ast::ptr<ast::Expr>> exprs; |
---|
724 | buildMoveList( expr_node, exprs ); |
---|
725 | return new ast::UntypedTupleExpr( location, std::move( exprs ) ); |
---|
726 | } // build_tuple |
---|
727 | |
---|
728 | ast::Expr * build_func( const CodeLocation & location, |
---|
729 | ExpressionNode * function, |
---|
730 | ExpressionNode * expr_node ) { |
---|
731 | std::vector<ast::ptr<ast::Expr>> args; |
---|
732 | buildMoveList( expr_node, args ); |
---|
733 | return new ast::UntypedExpr( location, |
---|
734 | maybeMoveBuild( function ), |
---|
735 | std::move( args ) |
---|
736 | ); |
---|
737 | } // build_func |
---|
738 | |
---|
739 | ast::Expr * build_compoundLiteral( const CodeLocation & location, |
---|
740 | DeclarationNode * decl_node, |
---|
741 | InitializerNode * kids ) { |
---|
742 | // compound literal type |
---|
743 | ast::Decl * newDecl = maybeBuild( decl_node ); |
---|
744 | // non-sue compound-literal type |
---|
745 | if ( ast::DeclWithType * newDeclWithType = dynamic_cast<ast::DeclWithType *>( newDecl ) ) { |
---|
746 | return new ast::CompoundLiteralExpr( location, |
---|
747 | newDeclWithType->get_type(), |
---|
748 | maybeMoveBuild( kids ) ); |
---|
749 | // these types do not have associated type information |
---|
750 | } else if ( auto newDeclStructDecl = dynamic_cast<ast::StructDecl *>( newDecl ) ) { |
---|
751 | if ( newDeclStructDecl->body ) { |
---|
752 | return new ast::CompoundLiteralExpr( location, |
---|
753 | new ast::StructInstType( newDeclStructDecl ), |
---|
754 | maybeMoveBuild( kids ) ); |
---|
755 | } else { |
---|
756 | return new ast::CompoundLiteralExpr( location, |
---|
757 | new ast::StructInstType( newDeclStructDecl->name ), |
---|
758 | maybeMoveBuild( kids ) ); |
---|
759 | } // if |
---|
760 | } else if ( auto newDeclUnionDecl = dynamic_cast<ast::UnionDecl *>( newDecl ) ) { |
---|
761 | if ( newDeclUnionDecl->body ) { |
---|
762 | return new ast::CompoundLiteralExpr( location, |
---|
763 | new ast::UnionInstType( newDeclUnionDecl ), |
---|
764 | maybeMoveBuild( kids ) ); |
---|
765 | } else { |
---|
766 | return new ast::CompoundLiteralExpr( location, |
---|
767 | new ast::UnionInstType( newDeclUnionDecl->name ), |
---|
768 | maybeMoveBuild( kids ) ); |
---|
769 | } // if |
---|
770 | } else if ( auto newDeclEnumDecl = dynamic_cast<ast::EnumDecl *>( newDecl ) ) { |
---|
771 | if ( newDeclEnumDecl->body ) { |
---|
772 | return new ast::CompoundLiteralExpr( location, |
---|
773 | new ast::EnumInstType( newDeclEnumDecl ), |
---|
774 | maybeMoveBuild( kids ) ); |
---|
775 | } else { |
---|
776 | return new ast::CompoundLiteralExpr( location, |
---|
777 | new ast::EnumInstType( newDeclEnumDecl->name ), |
---|
778 | maybeMoveBuild( kids ) ); |
---|
779 | } // if |
---|
780 | } else { |
---|
781 | assert( false ); |
---|
782 | } // if |
---|
783 | } // build_compoundLiteral |
---|
784 | |
---|
785 | // Local Variables: // |
---|
786 | // tab-width: 4 // |
---|
787 | // End: // |
---|