source: src/Parser/ExpressionNode.cpp@ 2fd3de7

Last change on this file since 2fd3de7 was 2737b37, checked in by Peter A. Buhr <pabuhr@…>, 5 days ago

formatting

  • Property mode set to 100644
File size: 30.8 KB
Line 
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.cpp --
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 : Fri May 1 11:07:48 2026
13// Update Count : 1128
14//
15
16#include "ExpressionNode.hpp"
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/BasicKind.hpp" // for BasicKind
26#include "AST/Expr.hpp" // for NameExpr
27#include "AST/Type.hpp" // for Type, LengthFlag, DimentionFlag
28#include "Common/SemanticError.hpp"// for SemanticError
29#include "Common/Utility.hpp" // for maybeMoveBuild, maybeBuild, CodeLo...
30#include "DeclarationNode.hpp" // for DeclarationNode
31#include "InitializerNode.hpp" // for InitializerNode
32#include "TypeData.hpp" // for addType, build_basic_type, build_c...
33#include "ParserUtility.hpp" // for notZeroExpr
34
35using 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// static inline bool checkH( char c ) { return c == 'h' || c == 'H'; }
52// static inline bool checkZ( char c ) { return c == 'z' || c == 'Z'; }
53// static inline bool checkU( char c ) { return c == 'u' || c == 'U'; }
54static inline bool checkF( char c ) { return c == 'f' || c == 'F'; }
55static inline bool checkD( char c ) { return c == 'd' || c == 'D'; }
56static inline bool checkF80( char c ) { return c == 'w' || c == 'W'; }
57static inline bool checkF128( char c ) { return c == 'q' || c == 'Q'; }
58static inline bool checkL( char c ) { return c == 'l' || c == 'L'; }
59static inline bool checkI( char c ) { return c == 'i' || c == 'I'; }
60static inline bool checkB( char c ) { return c == 'b' || c == 'B'; }
61static inline bool checkX( char c ) { return c == 'x' || c == 'X'; }
62// static inline bool checkN( char c ) { return c == 'n' || c == 'N'; }
63
64void lnthSuffix( string & str, int & type, int & ltype ) {
65 // 'u' can appear before or after length suffix
66 string::size_type posn = str.find_last_of( "lL" );
67
68 if ( posn == string::npos ) return; // no suffix
69 size_t end = str.length() - 1;
70 if ( posn == end ) { type = 3; return; } // no length after 'l' => 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
87 char fix = '\0';
88 if ( str[end] == 'u' || str[end] == 'U' ) fix = str[end]; // ends with 'uU' ?
89 str.erase( posn ); // remove length suffix and possibly uU
90 if ( type == 5 ) { // L128 does not need uU
91 end = str.length() - 1;
92 if ( str[end] == 'u' || str[end] == 'U' ) str.erase( end ); // ends with 'uU' ? remove
93 } else if ( fix != '\0' ) str += fix; // put 'uU' back if removed
94} // lnthSuffix
95
96void valueToType( unsigned long long int & v, bool dec, int & type, bool & Unsigned ) {
97 // use value to determine type
98 if ( v <= INT_MAX ) { // signed int
99 type = 2;
100 } else if ( v <= UINT_MAX && ! dec ) { // unsigned int
101 type = 2;
102 Unsigned = true; // unsigned
103 } else if ( v <= LONG_MAX ) { // signed long int
104 type = 3;
105 } else if ( v <= ULONG_MAX && ( ! dec || LONG_MAX == LLONG_MAX ) ) { // signed long int
106 type = 3;
107 Unsigned = true; // unsigned long int
108 } else if ( v <= LLONG_MAX ) { // signed long long int
109 type = 4;
110 } else { // unsigned long long int
111 type = 4;
112 Unsigned = true; // unsigned long long int
113 } // if
114} // valueToType
115
116static void scanbin( string & str, unsigned long long int & v ) {
117 v = 0;
118 size_t last = str.length() - 1; // last subscript of constant
119 for ( unsigned int i = 2;; ) { // ignore prefix
120 if ( str[i] == '1' ) v |= 1;
121 i += 1;
122 if ( i == last - 1 || (str[i] != '0' && str[i] != '1') ) break;
123 v <<= 1;
124 } // for
125} // scanbin
126
127ast::Expr * build_constantInteger( const CodeLocation & location, string & str ) {
128 static const ast::BasicKind kind[2][6] = {
129 // short (h) must be before char (hh) because shorter type has the longer suffix
130 { ast::BasicKind::ShortSignedInt, ast::BasicKind::SignedChar, ast::BasicKind::SignedInt, ast::BasicKind::LongSignedInt, ast::BasicKind::LongLongSignedInt, /* BasicKind::SignedInt128 */ ast::BasicKind::LongLongSignedInt, },
131 { ast::BasicKind::ShortUnsignedInt, ast::BasicKind::UnsignedChar, ast::BasicKind::UnsignedInt, ast::BasicKind::LongUnsignedInt, ast::BasicKind::LongLongUnsignedInt, /* BasicKind::UnsignedInt128 */ ast::BasicKind::LongLongUnsignedInt, },
132 };
133
134 static const char * lnthsInt[2][6] = {
135 { "int16_t", "int8_t", "int32_t", "int64_t", "size_t", "uintptr_t", },
136 { "uint16_t", "uint8_t", "uint32_t", "uint64_t", "size_t", "uintptr_t", },
137 }; // lnthsInt
138
139 string str2( "0x0" );
140 unsigned long long int v, v2 = 0; // converted integral value
141 ast::Expr * ret, * ret2;
142
143 int type = -1; // 0 => short, 1 => char, 2 => int, 3 => long int, 4 => long long int, 5 => int128
144 int ltype = -1; // 0 => 16 bits, 1 => 8 bits, 2 => 32 bits, 3 => 64 bits, 4 => size_t, 5 => intptr, 6 => pointer
145 bool dec = true, Unsigned = false; // decimal, unsigned constant
146
147 // special constants
148 if ( str == "0" ) {
149 ret = new ast::ConstantExpr( location, new ast::ZeroType(), str, 0 );
150 goto CLEANUP;
151 } // if
152 if ( str == "1" ) {
153 ret = new ast::ConstantExpr( location, new ast::OneType(), str, 1 );
154 goto CLEANUP;
155 } // if
156
157 // 'u' can appear before or after length suffix
158 if ( str.find_last_of( "uU" ) != string::npos ) Unsigned = true;
159
160 if ( isdigit( str[str.length() - 1] ) ) { // no suffix ?
161 lnthSuffix( str, type, ltype ); // could have length suffix
162 } else {
163 // At least one digit in integer constant, so safe to backup while looking for suffix. This
164 // declaration and the comma expressions in the conditions mimic the declare and check
165 // pattern allowed in later compiler versions. (Only some early compilers/C++ standards do
166 // not support it.)
167 string::size_type posn;
168 // pointer value
169 if ( posn = str.find_last_of( "pP" ), posn != string::npos ) {
170 ltype = 5; str.erase( posn, 1 );
171 // size_t
172 } else if ( posn = str.find_last_of( "zZ" ), posn != string::npos ) {
173 Unsigned = true; type = 2; ltype = 4; str.erase( posn, 1 );
174 // signed char
175 } else if ( posn = str.rfind( "hh" ), posn != string::npos ) {
176 type = 1; str.erase( posn, 2 );
177 // signed char
178 } else if ( posn = str.rfind( "HH" ), posn != string::npos ) {
179 type = 1; str.erase( posn, 2 );
180 // short
181 } else if ( posn = str.find_last_of( "hH" ), posn != string::npos ) {
182 type = 0; str.erase( posn, 1 );
183 // int (natural number)
184 } else if ( posn = str.find_last_of( "nN" ), posn != string::npos ) {
185 type = 2; str.erase( posn, 1 );
186 } else if ( str.rfind( "ll" ) != string::npos || str.rfind( "LL" ) != string::npos ) {
187 type = 4;
188 } else {
189 lnthSuffix( str, type, ltype );
190 } // if
191 } // if
192
193 // Cannot be just "0"/"1"; sscanf stops at the suffix, if any; value goes over the wall => always generate
194
195#if ! defined(__SIZEOF_INT128__)
196 if ( type == 5 ) SemanticError( yylloc, "int128 constant is not supported on this target \"%s\"", str.c_str() );
197#endif // ! __SIZEOF_INT128__
198
199 if ( str[0] == '0' ) { // radix character ?
200 dec = false;
201 if ( checkX( str[1] ) ) { // hex constant ?
202 if ( type < 5 ) { // not L128 ?
203 sscanf( (char *)str.c_str(), "%llx", &v );
204#if defined(__SIZEOF_INT128__)
205 } else { // hex int128 constant
206 unsigned int len = str.length();
207 if ( len > (2 + 16 + 16) ) SemanticError( yylloc, "128-bit hexadecimal constant to large \"%s\"", str.c_str() );
208 // hex digits < 2^64
209 if ( len > (2 + 16) ) {
210 str2 = "0x" + str.substr( len - 16 );
211 sscanf( (char *)str2.c_str(), "%llx", &v2 );
212 str = str.substr( 0, len - 16 );
213 } // if
214 sscanf( (char *)str.c_str(), "%llx", &v );
215#endif // __SIZEOF_INT128__
216 } // if
217 //printf( "%llx %llu\n", v, v );
218 } else if ( checkB( str[1] ) ) { // binary constant ?
219#if defined(__SIZEOF_INT128__)
220 unsigned int len = str.length();
221 if ( type == 5 && len > 2 + 64 ) {
222 if ( len > 2 + 64 + 64 ) SemanticError( yylloc, "128-bit binary constant to large \"%s\".", str.c_str() );
223 str2 = "0b" + str.substr( len - 64 );
224 str = str.substr( 0, len - 64 );
225 scanbin( str2, v2 );
226 } // if
227#endif // __SIZEOF_INT128__
228 scanbin( str, v );
229 //printf( "%#llx %llu\n", v, v );
230 } else { // octal constant
231 if ( type < 5 ) { // not L128 ?
232 sscanf( (char *)str.c_str(), "%llo", &v );
233#if defined(__SIZEOF_INT128__)
234 } else { // octal int128 constant
235 unsigned int len = str.length();
236 if ( len > 1 + 43 || (len == 1 + 43 && str[0] > '3') ) SemanticError( yylloc, "128-bit octal constant to large \"%s\"", str.c_str() );
237 char buf[32];
238 if ( len <= 1 + 21 ) { // value < 21 octal digitis
239 sscanf( (char *)str.c_str(), "%llo", &v );
240 } else {
241 sscanf( &str[len - 21], "%llo", &v );
242 __int128 val = v; // accumulate bits
243 str[len - 21] ='\0'; // shorten string
244 sscanf( &str[len == 43 ? 1 : 0], "%llo", &v );
245 val |= (__int128)v << 63; // store bits
246 if ( len == 1 + 43 ) { // most significant 2 bits ?
247 str[2] = '\0'; // shorten string
248 sscanf( &str[1], "%llo", &v ); // process most significant 2 bits
249 val |= (__int128)v << 126; // store bits
250 } // if
251 v = val >> 64; v2 = (uint64_t)val; // replace octal constant with 2 hex constants
252 sprintf( buf, "%#llx", v2 );
253 str2 = buf;
254 } // if
255 sprintf( buf, "%#llx", v );
256 str = buf;
257#endif // __SIZEOF_INT128__
258 } // if
259 //printf( "%#llo %llu\n", v, v );
260 } // if
261 } else { // decimal constant ?
262 if ( type < 5 ) { // not L128 ?
263 sscanf( (char *)str.c_str(), "%llu", &v );
264#if defined(__SIZEOF_INT128__)
265 } else { // decimal int128 constant
266 #define P10_UINT64 10'000'000'000'000'000'000ULL // 19 zeroes
267 unsigned int len = str.length();
268 if ( str.length() == 39 && str > (Unsigned ? "340282366920938463463374607431768211455" : "170141183460469231731687303715884105727") )
269 SemanticError( yylloc, "128-bit decimal constant to large \"%s\".", str.c_str() );
270 char buf[32];
271 if ( len <= 19 ) { // value < 19 decimal digitis
272 sscanf( (char *)str.c_str(), "%llu", &v );
273 } else {
274 sscanf( &str[len - 19], "%llu", &v );
275 __int128 val = v; // accumulate bits
276 str[len - 19] ='\0'; // shorten string
277 sscanf( &str[len == 39 ? 1 : 0], "%llu", &v );
278 val += (__int128)v * (__int128)P10_UINT64; // store bits
279 if ( len == 39 ) { // most significant 2 bits ?
280 str[1] = '\0'; // shorten string
281 sscanf( &str[0], "%llu", &v ); // process most significant 2 bits
282 val += (__int128)v * (__int128)P10_UINT64 * (__int128)P10_UINT64; // store bits
283 } // if
284 v = val >> 64; v2 = (uint64_t)val; // replace decimal constant with 2 hex constants
285 sprintf( buf, "%#llx", v2 );
286 str2 = buf;
287 } // if
288 sprintf( buf, "%#llx", v );
289 str = buf;
290#endif // __SIZEOF_INT128__
291 } // if
292 //printf( "%llu\n", v );
293 } // if
294
295 if ( type == -1 ) { // no suffix => determine type from value size
296 valueToType( v, dec, type, Unsigned );
297 } // if
298 /* printf( "%s %llo %s %llo\n", str.c_str(), v, str2.c_str(), v2 ); */
299
300 //if ( !( 0 <= type && type <= 6 ) ) { printf( "%s %lu %d %s\n", fred.c_str(), fred.length(), type, str.c_str() ); }
301 assert( 0 <= type && type <= 6 );
302
303 // Constant type is correct for overload resolving.
304 ret = new ast::ConstantExpr( location, 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, ret, new ast::BasicType( kind[Unsigned][type] ), ast::ExplicitCast );
308 } else if ( ltype != -1 ) { // explicit length ?
309 if ( ltype == 6 ) { // int128, (int128)constant
310 ret2 = new ast::ConstantExpr( location, new ast::BasicType( ast::BasicKind::LongLongSignedInt ), str2, v2 );
311 ret = build_compoundLiteral( location,
312 DeclarationNode::newFromTypeData( addType( build_basic_type( TypeData::Int128 ), build_signedness( TypeData::Unsigned ) ) ),
313 new InitializerNode( (new InitializerNode( new ExpressionNode( v2 == 0 ? ret2 : ret ) ))->set_last( new InitializerNode( new ExpressionNode( v2 == 0 ? ret : ret2 ) ) ), true )
314 );
315 } else { // explicit length, (length_type)constant
316 ret = new ast::CastExpr( location, ret, new ast::TypeInstType( lnthsInt[Unsigned][ltype], ast::TypeDecl::Dtype ), ast::ExplicitCast );
317 if ( ltype == 5 ) { // pointer, intptr( (uintptr_t)constant )
318 ret = build_func( location, new ExpressionNode( build_varref( location, new string( "intptr" ) ) ), new ExpressionNode( ret ) );
319 } // if
320 } // if
321 } // if
322
323 CLEANUP: ;
324 delete &str; // created by lex
325 return ret;
326} // build_constantInteger
327
328
329static inline void checkFnxFloat( string & str, size_t last, bool & explnth, int & type ) {
330 string::size_type posn;
331 // floating-point constant has minimum of 2 characters, 1. or .1, so safe to look ahead
332 if ( str[1] == 'x' ) { // hex ?
333 posn = str.find_last_of( "pP" ); // back for exponent (must have)
334 posn = str.find_first_of( "fF", posn + 1 ); // forward for size (fF allowed in hex constant)
335 } else {
336 posn = str.find_last_of( "fF" ); // back for size (fF not allowed)
337 } // if
338 if ( posn == string::npos ) return;
339 explnth = true;
340 posn += 1; // advance to size
341 if ( str[posn] == '3' ) { // 32
342 if ( str[last] != 'x' ) type = 6;
343 else type = 7;
344 } else if ( str[posn] == '6' ) { // 64
345 if ( str[last] != 'x' ) type = 8;
346 else type = 9;
347 } else if ( str[posn] == '8' ) { // 80
348 type = 3;
349 } else if ( str[posn] == '1' ) { // 16/128
350 if ( str[posn + 1] == '6' ) { // 16
351 type = 5;
352 } else { // 128
353 if ( str[last] != 'x' ) type = 10;
354 else type = 11;
355 } // if
356 } else {
357 assertf( false, "internal error, bad floating point length %s", str.c_str() );
358 } // if
359} // checkFnxFloat
360
361
362ast::Expr * build_constantFloat( const CodeLocation & location, string & str ) {
363 static const ast::BasicKind kind[2][12] = {
364 { ast::BasicKind::Float, ast::BasicKind::Double, ast::BasicKind::LongDouble, ast::BasicKind::Float80, ast::BasicKind::uuFloat128, ast::BasicKind::Float16, ast::BasicKind::Float32, ast::BasicKind::Float32x, ast::BasicKind::Float64, ast::BasicKind::Float64x, ast::BasicKind::Float128, ast::BasicKind::Float128x },
365 { ast::BasicKind::FloatComplex, ast::BasicKind::DoubleComplex, ast::BasicKind::LongDoubleComplex, ast::BasicKind::NUMBER_OF_BASIC_TYPES, ast::BasicKind::NUMBER_OF_BASIC_TYPES, ast::BasicKind::Float16Complex, ast::BasicKind::Float32Complex, ast::BasicKind::Float32xComplex, ast::BasicKind::Float64Complex, ast::BasicKind::Float64xComplex, ast::BasicKind::Float128Complex, ast::BasicKind::Float128xComplex },
366 };
367
368 // floating-point constant has minimum of 2 characters 1. or .1
369 size_t last = str.length() - 1;
370 double v;
371 int type; // 0 => float, 1 => double, 3 => long double, ...
372 bool complx = false; // real, complex
373 bool explnth = false; // explicit literal length
374
375 sscanf( str.c_str(), "%lg", &v );
376
377 if ( checkI( str[last] ) ) { // imaginary ?
378 complx = true;
379 last -= 1; // backup one character
380 } // if
381
382 if ( checkF( str[last] ) ) { // float ?
383 type = 0;
384 } else if ( checkD( str[last] ) ) { // double ?
385 type = 1;
386 } else if ( checkL( str[last] ) ) { // long double ?
387 type = 2;
388 } else if ( checkF80( str[last] ) ) { // __float80 ?
389 type = 3;
390 } else if ( checkF128( str[last] ) ) { // __float128 ?
391 type = 4;
392 } else {
393 type = 1; // double (default if no suffix)
394 checkFnxFloat( str, last, explnth, type );
395 } // if
396
397 if ( ! complx && checkI( str[last - 1] ) ) { // imaginary ?
398 complx = true;
399 } // if
400
401 assert( 0 <= type && type < 12 );
402 ast::Expr * ret = new ast::ConstantExpr( location, new ast::BasicType( kind[complx][type] ), str, v );
403 // explicit length ?
404 if ( explnth ) {
405 ret = new ast::CastExpr( location, ret, new ast::BasicType( kind[complx][type] ), ast::ExplicitCast );
406 } // if
407
408 delete &str; // created by lex
409 return ret;
410} // build_constantFloat
411
412static void sepString( string & str, string & units, char delimit ) {
413 string::size_type posn = str.find_last_of( delimit ) + 1;
414 if ( posn != str.length() ) {
415 units = "?" + str.substr( posn ); // extract units
416 str.erase( posn ); // remove units
417 } // if
418} // sepString
419
420static ast::Type * charstrKind( string & str ) {
421 ast::Type * kind;
422 switch ( str[0] ) { // str has >= 2 characters, i.e, null string "" => safe to look at subscripts 0/1
423 case 'u':
424 if ( str[1] == '8' ) goto Default; // utf-8 characters => array of char (save check for char as no 8 allowed)
425 // lookup type of associated typedef
426 kind = new ast::TypeInstType( "char16_t", ast::TypeDecl::Dtype );
427 break;
428 case 'U':
429 kind = new ast::TypeInstType( "char32_t", ast::TypeDecl::Dtype );
430 break;
431 case 'L':
432 kind = new ast::TypeInstType( "wchar_t", ast::TypeDecl::Dtype );
433 break;
434 Default: // char default string type
435 default:
436 kind = new ast::BasicType( ast::BasicKind::Char );
437 } // switch
438 return kind;
439} // charstrKind
440
441static bool isoctal( char ch ) {
442 return ('0' <= ch && ch <= '7');
443} // isoctal
444
445// A "sequence" is the series of characters in a character/string literal that becomes a single
446// character value in the runtime value.
447static size_t sequenceLength( const std::string & str, size_t pos ) {
448 // Most "sequences" are just a single character, filter those out:
449 if ( '\\' != str[pos] ) return 1;
450 switch ( str[pos + 1] ) {
451 // Simple Escape Sequence (\_ where _ is one of the following):
452 case '\'': case '\"': case '?': case '\\':
453 case 'a': case 'b': case 'f': case 'n': case 'r': case 't': case 'v':
454 // GCC Escape Sequence (as simple, just some different letters):
455 case 'e':
456 return 2;
457 // Numeric Escape Sequence (\___ where _ is 1-3 octal digits):
458 case '0': case '1': case '2': case '3':
459 case '4': case '5': case '6': case '7':
460 return ( !isoctal( str[pos + 2] ) ) ? 2 :
461 ( !isoctal( str[pos + 3] ) ) ? 3 : 4;
462 // Numeric Escape Sequence (\x_ where _ is 1 or more hexadecimal digits):
463 case 'x': {
464 size_t length = 2;
465 while ( isxdigit( str[pos + length] ) ) ++length;
466 return length;
467 }
468 // Universal Character Name (\u____ where _ is 4 decimal digits):
469 case 'u':
470 return 6;
471 // Universal Character Name (\U________ where _ is 8 decimal digits):
472 case 'U':
473 return 10;
474 default:
475 SemanticError( yylloc, "Unknown escape sequence \"\\%c\"", str[pos + 1] );
476 return 1;
477 } // switch
478} // sequenceLength
479
480ast::Expr * build_constantChar( const CodeLocation & location, string & str ) {
481 string units; // units
482 sepString( str, units, '\'' ); // separate constant from units
483
484 ast::Expr * ret = new ast::ConstantExpr( location, charstrKind( str ), str, (unsigned long long int)(unsigned char)str[1] );
485 if ( units.length() != 0 ) {
486 ret = new ast::UntypedExpr( location, new ast::NameExpr( location, units ), { ret } );
487 } // if
488
489 delete &str; // created by lex
490 return ret;
491} // build_constantChar
492
493ast::Expr * build_constantStr( const CodeLocation & location, string & str ) {
494 assert( str.length() > 0 );
495 string units; // units
496 sepString( str, units, '"' ); // separate constant from units
497
498 // The length value of the type is equal to the number of "sequences" not including the openning
499 // and closing quotes in the literal plus 1 for the implicit null terminator.
500 size_t length = 1;
501 for ( size_t pos = 1 ; pos < str.size() - 1 ; pos += sequenceLength( str, pos ) ) {
502 if ( '"' == str[pos] ) { // concatenated strings ? "ABC" "DEF"
503 int cnt = 1; // skip outside quotes and space between
504 for ( unsigned int i = pos + 1; str[i] != '"'; i += 1, cnt += 1 );
505 pos += cnt;
506 continue; // not part of length
507 } // if
508 length += 1;
509 } // for
510
511 ast::ArrayType * at = new ast::ArrayType( charstrKind( str ), ast::ConstantExpr::from_ulong( location, length ), ast::FixedLen, ast::DynamicDim );
512 ast::Expr * ret = new ast::ConstantExpr( location, at, str, std::nullopt );
513 if ( units.length() != 0 ) {
514 ret = new ast::UntypedExpr( location, new ast::NameExpr( location, units ), { ret } );
515 } // if
516
517 delete &str; // created by lex
518 return ret;
519} // build_constantStr
520
521ast::Expr * build_field_name_FLOATING_FRACTIONconstant( const CodeLocation & location, const string & str ) {
522 if ( str.find_first_not_of( "0123456789", 1 ) != string::npos ) SemanticError( yylloc, "invalid tuple index \"%s\".", str.c_str() );
523 ast::Expr * ret = build_constantInteger( location, *new string( str.substr(1) ) );
524 delete &str;
525 return ret;
526} // build_field_name_FLOATING_FRACTIONconstant
527
528ast::Expr * build_field_name_FLOATING_DECIMALconstant( const CodeLocation & location, const string & str ) {
529 if ( str[str.size() - 1] != '.' ) SemanticError( yylloc, "invalid tuple index \"%s\".", str.c_str() );
530 ast::Expr * ret = build_constantInteger( location, *new string( str.substr( 0, str.size()-1 ) ) );
531 delete &str;
532 return ret;
533} // build_field_name_FLOATING_DECIMALconstant
534
535ast::Expr * build_field_name_FLOATINGconstant( const CodeLocation & location, const string & str ) {
536 // str is of the form A.B -> separate at the . and return member expression
537 int a, b;
538 char dot;
539 stringstream ss( str );
540 ss >> a >> dot >> b;
541 auto ret = new ast::UntypedMemberExpr( location, ast::ConstantExpr::from_int( location, b ), ast::ConstantExpr::from_int( location, a )
542 );
543 delete &str;
544 return ret;
545} // build_field_name_FLOATINGconstant
546
547ast::Expr * make_field_name_fraction_constants( const CodeLocation & location, ast::Expr * fieldName, ast::Expr * fracts ) {
548 if ( nullptr == fracts ) {
549 return fieldName;
550 } else if ( auto memberExpr = dynamic_cast<ast::UntypedMemberExpr *>( fracts ) ) {
551 memberExpr->member = make_field_name_fraction_constants( location, fieldName, ast::mutate( memberExpr->aggregate.get() ) );
552 return memberExpr;
553 } else {
554 return new ast::UntypedMemberExpr( location, fracts, fieldName );
555 } // if
556} // make_field_name_fraction_constants
557
558ast::Expr * build_field_name_fraction_constants( const CodeLocation & location, ast::Expr * fieldName, ExpressionNode * fracts ) {
559 return make_field_name_fraction_constants( location, fieldName, maybeMoveBuild( fracts ) );
560} // build_field_name_fraction_constants
561
562ast::NameExpr * build_varref( const CodeLocation & location, const string * name ) {
563 ast::NameExpr * expr = new ast::NameExpr( location, *name );
564 delete name;
565 return expr;
566} // build_varref
567
568ast::QualifiedNameExpr * build_qualified_expr( const CodeLocation & location, const DeclarationNode * decl_node, const ast::NameExpr * name ) {
569 ast::Decl * newDecl = maybeBuild( decl_node );
570 if ( ast::DeclWithType * newDeclWithType = dynamic_cast<ast::DeclWithType *>( newDecl ) ) {
571 if ( const ast::Type * t = newDeclWithType->get_type() ) {
572 if ( auto typeInst = dynamic_cast<const ast::TypeInstType *>( t ) ) {
573 newDecl = new ast::EnumDecl( location, typeInst->name );
574 }
575 }
576 }
577 return new ast::QualifiedNameExpr( location, newDecl, name->name );
578}
579
580ast::QualifiedNameExpr * build_qualified_expr( const CodeLocation & location, const ast::EnumDecl * decl, const ast::NameExpr * name ) {
581 return new ast::QualifiedNameExpr( location, decl, name->name );
582}
583
584ast::DimensionExpr * build_dimensionref( const CodeLocation & location, 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
591static const char * OperName[] = { // must harmonize with OperKinds
592 // diadic
593 "SizeOf", "AlignOf", "OffsetOf", "?+?", "?-?", "?\\?", "?*?", "?/?", "?%?", "||", "&&",
594 "?|?", "?&?", "?^?", "Cast", "?<<?", "?>>?", "?<?", "?>?", "?<=?", "?>=?", "?==?", "?!=?",
595 "?=?", "?@=?", "?\\=?", "?*=?", "?/=?", "?%=?", "?+=?", "?-=?", "?<<=?", "?>>=?", "?&=?", "?^=?", "?|=?",
596 "?[?]", "...",
597 // monadic
598 "+?", "-?", "AddressOf", "*?", "!?", "~?", "++?", "?++", "--?", "?--",
599}; // OperName
600
601ast::Expr * build_cast( const CodeLocation & location, DeclarationNode * decl_node, ExpressionNode * expr_node, ast::CastKind kind ) {
602 ast::Type * targetType = maybeMoveBuildType( decl_node );
603 if ( dynamic_cast<ast::VoidType *>( targetType ) ) {
604 delete targetType;
605 return new ast::CastExpr( location, maybeMoveBuild( expr_node ), ast::ExplicitCast, kind );
606 } else {
607 return new ast::CastExpr( location, maybeMoveBuild( expr_node ), targetType, ast::ExplicitCast, kind );
608 } // if
609} // build_cast
610
611ast::Expr * build_keyword_cast( const CodeLocation & location, ast::AggregateDecl::Aggregate target, ExpressionNode * expr_node ) {
612 return new ast::KeywordCastExpr( location, maybeMoveBuild( expr_node ), target );
613}
614
615ast::Expr * build_virtual_cast( const CodeLocation & location, DeclarationNode * decl_node, ExpressionNode * expr_node ) {
616 return new ast::VirtualCastExpr( location, maybeMoveBuild( expr_node ), maybeMoveBuildType( decl_node ) );
617} // build_virtual_cast
618
619ast::Expr * build_fieldSel( const CodeLocation & location, ExpressionNode * expr_node, ast::Expr * member ) {
620 return new ast::UntypedMemberExpr( location, member, maybeMoveBuild( expr_node ) );
621} // build_fieldSel
622
623ast::Expr * build_pfieldSel( const CodeLocation & location, ExpressionNode * expr_node, ast::Expr * member ) {
624 auto deref = new ast::UntypedExpr( location, new ast::NameExpr( location, "*?" ) );
625 deref->location = expr_node->location;
626 deref->args.push_back( maybeMoveBuild( expr_node ) );
627 auto ret = new ast::UntypedMemberExpr( location, member, deref );
628 return ret;
629} // build_pfieldSel
630
631ast::Expr * build_offsetOf( const CodeLocation & location, DeclarationNode * decl_node, ast::NameExpr * member ) {
632 ast::Expr * ret = new ast::UntypedOffsetofExpr( location, maybeMoveBuildType( decl_node ), member->name );
633 ret->result = new ast::BasicType( ast::BasicKind::LongUnsignedInt );
634 delete member;
635 return ret;
636} // build_offsetOf
637
638ast::Expr * build_and_or( const CodeLocation & location, ExpressionNode * expr_node1, ExpressionNode * expr_node2, ast::LogicalFlag flag ) {
639 return new ast::LogicalExpr( location, maybeMoveBuild( expr_node1 ), maybeMoveBuild( expr_node2 ), flag );
640} // build_and_or
641
642ast::Expr * build_unary_val( const CodeLocation & location, OperKinds op, ExpressionNode * expr_node ) {
643 std::vector<ast::ptr<ast::Expr>> args;
644 args.push_back( maybeMoveBuild( expr_node ) );
645 return new ast::UntypedExpr( location, new ast::NameExpr( location, OperName[ (int)op ] ), std::move( args ) );
646} // build_unary_val
647
648ast::Expr * build_binary_val( const CodeLocation & location, OperKinds op, ExpressionNode * expr_node1, ExpressionNode * expr_node2 ) {
649 std::vector<ast::ptr<ast::Expr>> args;
650 args.push_back( maybeMoveBuild( expr_node1 ) );
651 args.push_back( maybeMoveBuild( expr_node2 ) );
652 return new ast::UntypedExpr( location, new ast::NameExpr( location, OperName[ (int)op ] ), std::move( args ) );
653} // build_binary_val
654
655ast::Expr * build_cond( const CodeLocation & location, ExpressionNode * expr_node1, ExpressionNode * expr_node2, ExpressionNode * expr_node3 ) {
656 return new ast::ConditionalExpr( location, maybeMoveBuild( expr_node1 ), maybeMoveBuild( expr_node2 ), maybeMoveBuild( expr_node3 ) );
657} // build_cond
658
659ast::Expr * build_tuple( const CodeLocation & location, ExpressionNode * expr_node ) {
660 std::vector<ast::ptr<ast::Expr>> exprs;
661 buildMoveList( expr_node, exprs );
662 return new ast::UntypedTupleExpr( location, std::move( exprs ) );
663} // build_tuple
664
665ast::Expr * build_func( const CodeLocation & location, ExpressionNode * function, ExpressionNode * expr_node ) {
666 std::vector<ast::ptr<ast::Expr>> args;
667 buildMoveList( expr_node, args );
668 return new ast::UntypedExpr( location, maybeMoveBuild( function ), std::move( args ) );
669} // build_func
670
671ast::Expr * build_compoundLiteral( const CodeLocation & location, DeclarationNode * decl_node, InitializerNode * kids ) {
672 // compound literal type
673 ast::Decl * newDecl = maybeBuild( decl_node );
674 // non-sue compound-literal type
675 if ( ast::DeclWithType * newDeclWithType = dynamic_cast<ast::DeclWithType *>( newDecl ) ) {
676 return new ast::CompoundLiteralExpr( location, newDeclWithType->get_type(), maybeMoveBuild( kids ) );
677 // these types do not have associated type information
678 } else if ( auto newDeclStructDecl = dynamic_cast<ast::StructDecl *>( newDecl ) ) {
679 if ( newDeclStructDecl->body ) {
680 return new ast::CompoundLiteralExpr( location, new ast::StructInstType( newDeclStructDecl ), maybeMoveBuild( kids ) );
681 } else {
682 return new ast::CompoundLiteralExpr( location, new ast::StructInstType( newDeclStructDecl->name ), maybeMoveBuild( kids ) );
683 } // if
684 } else if ( auto newDeclUnionDecl = dynamic_cast<ast::UnionDecl *>( newDecl ) ) {
685 if ( newDeclUnionDecl->body ) {
686 return new ast::CompoundLiteralExpr( location, new ast::UnionInstType( newDeclUnionDecl ), maybeMoveBuild( kids ) );
687 } else {
688 return new ast::CompoundLiteralExpr( location, new ast::UnionInstType( newDeclUnionDecl->name ), maybeMoveBuild( kids ) );
689 } // if
690 } else if ( auto newDeclEnumDecl = dynamic_cast<ast::EnumDecl *>( newDecl ) ) {
691 if ( newDeclEnumDecl->body ) {
692 return new ast::CompoundLiteralExpr( location, new ast::EnumInstType( newDeclEnumDecl ), maybeMoveBuild( kids ) );
693 } else {
694 return new ast::CompoundLiteralExpr( location, new ast::EnumInstType( newDeclEnumDecl->name ), maybeMoveBuild( kids ) );
695 } // if
696 } else {
697 assert( false );
698 } // if
699} // build_compoundLiteral
700
701ast::Expr * build_va_arg( const CodeLocation & location, ExpressionNode * function, DeclarationNode * declaration ) {
702 return build_func( location, new ExpressionNode( build_varref( location, new std::string( "__builtin_va_arg" ) ) ),
703 function->set_last( new ExpressionNode( new ast::TypeExpr( location, maybeMoveBuildType( declaration ) ) ) ) );
704}
705
706// Local Variables: //
707// tab-width: 4 //
708// End: //
Note: See TracBrowser for help on using the repository browser.