source: src/Parser/ExpressionNode.cc@ b4cd58ed

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since b4cd58ed was cf5af9c, checked in by Peter A. Buhr <pabuhr@…>, 5 years ago

change from SIZEOF_POINTER to SIZEOF_INT128 to determine if int128 exists

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