source: src/Parser/ExpressionNode.cc@ fc9bb79

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 fc9bb79 was f6582252, checked in by Peter A. Buhr <pabuhr@…>, 5 years ago

add support for int128 constants

  • Property mode set to 100644
File size: 26.6 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 : Sat Jul 11 16:44:49 2020
13// Update Count : 1034
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 ( str[0] == '0' ) { // radix character ?
189 dec = false;
190 if ( checkX( str[1] ) ) { // hex constant ?
191 if ( type < 5 ) { // not L128 ?
192 sscanf( (char *)str.c_str(), "%llx", &v );
193 } else { // hex int128 constant
194 unsigned int len = str.length();
195 if ( len > (2 + 16 + 16) ) SemanticError( yylloc, "128-bit hexadecimal constant to large " + str );
196 if ( len <= (2 + 16) ) goto FHEX1; // hex digits < 2^64
197 str2 = "0x" + str.substr( len - 16 );
198 sscanf( (char *)str2.c_str(), "%llx", &v2 );
199 str = str.substr( 0, len - 16 );
200 FHEX1: ;
201 sscanf( (char *)str.c_str(), "%llx", &v );
202 } // if
203 //printf( "%llx %llu\n", v, v );
204 } else if ( checkB( str[1] ) ) { // binary constant ?
205 unsigned int len = str.length();
206 if ( type == 5 && len > 2 + 64 ) {
207 if ( len > 2 + 64 + 64 ) SemanticError( yylloc, "128-bit binary constant to large " + str );
208 str2 = "0b" + str.substr( len - 64 );
209 str = str.substr( 0, len - 64 );
210 scanbin( str2, v2 );
211 } // if
212 scanbin( str, v );
213 //printf( "%#llx %llu\n", v, v );
214 } else { // octal constant
215 if ( type < 5 ) { // not L128 ?
216 sscanf( (char *)str.c_str(), "%llo", &v );
217 } else { // octal int128 constant
218 unsigned int len = str.length();
219 char buf[32];
220 __int128 val = v;
221
222 if ( len > 1 + 43 || (len == 1 + 43 && str[0] > '3') ) SemanticError( yylloc, "128-bit octal constant to large " + str );
223 if ( len <= 1 + 21 ) { // value < 21 octal digitis
224 sscanf( (char *)str.c_str(), "%llo", &v ); // leave value in octal
225 } else {
226 sscanf( &str[len - 21], "%llo", &v );
227 val = v; // store bits
228 str[len - 21] ='\0'; // shorten string
229 sscanf( &str[len == 43 ? 1 : 0], "%llo", &v );
230 val |= (__int128)v << 63; // store bits
231 if ( len == 1 + 43 ) { // most significant 2 bits ?
232 str[2] = '\0'; // shorten string
233 sscanf( &str[1], "%llo", &v ); // process most significant 2 bits
234 val |= (__int128)v << 126; // store bits
235 } // if
236 v = val >> 64; v2 = (uint64_t)val; // replace octal constant with 2 hex constants
237 sprintf( buf, "%#llx", v2 );
238 str2 = buf;
239 sprintf( buf, "%#llx", v );
240 str = buf;
241 } // if
242 } // if
243 //printf( "%#llo %llu\n", v, v );
244 } // if
245 } else { // decimal constant ?
246 if ( type < 5 ) { // not L128 ?
247 sscanf( (char *)str.c_str(), "%llu", &v );
248 } else { // decimal int128 constant
249 #define P10_UINT64 10'000'000'000'000'000'000ULL // 19 zeroes
250 unsigned int len = str.length();
251 char buf[32];
252 __int128 val = v;
253
254 if ( str.length() == 39 && str > (Unsigned ? "340282366920938463463374607431768211455" : "170141183460469231731687303715884105727") )
255 SemanticError( yylloc, "128-bit decimal constant to large " + str );
256 if ( len <= 19 ) { // value < 19 decimal digitis
257 sscanf( (char *)str.c_str(), "%llu", &v ); // leave value in decimal
258 } else {
259 sscanf( &str[len - 19], "%llu", &v );
260 val = v; // store bits
261 str[len - 19] ='\0'; // shorten string
262 sscanf( &str[len == 39 ? 1 : 0], "%llu", &v );
263 val += (__int128)v * (__int128)P10_UINT64; // store bits
264 if ( len == 39 ) { // most significant 2 bits ?
265 str[1] = '\0'; // shorten string
266 sscanf( &str[0], "%llu", &v ); // process most significant 2 bits
267 val += (__int128)v * (__int128)P10_UINT64 * (__int128)P10_UINT64; // store bits
268 } // if
269 v = val >> 64; v2 = (uint64_t)val; // replace decimal constant with 2 hex constants
270 sprintf( buf, "%#llx", v2 );
271 str2 = buf;
272 sprintf( buf, "%#llx", v );
273 str = buf;
274 } // if
275 } // if
276 //printf( "%llu\n", v );
277 } // if
278
279 if ( type == -1 ) { // no suffix => determine type from value size
280 valueToType( v, dec, type, Unsigned );
281 } // if
282 /* printf( "%s %llo %s %llo\n", str.c_str(), v, str2.c_str(), v2 ); */
283
284 //if ( !( 0 <= type && type <= 6 ) ) { printf( "%s %lu %d %s\n", fred.c_str(), fred.length(), type, str.c_str() ); }
285 assert( 0 <= type && type <= 6 );
286
287 // Constant type is correct for overload resolving.
288 ret = new ConstantExpr( Constant( new BasicType( noQualifiers, kind[Unsigned][type] ), str, v ) );
289 if ( Unsigned && type < 2 ) { // hh or h, less than int ?
290 // int i = -1uh => 65535 not -1, so cast is necessary for unsigned, which unfortunately eliminates warnings for large values.
291 ret = new CastExpr( ret, new BasicType( Type::Qualifiers(), kind[Unsigned][type] ), false );
292 } else if ( ltype != -1 ) { // explicit length ?
293 if ( ltype == 6 ) { // int128, (int128)constant
294// ret = new CastExpr( ret, new BasicType( Type::Qualifiers(), kind[Unsigned][type] ), false );
295 ret2 = new ConstantExpr( Constant( new BasicType( noQualifiers, BasicType::LongLongSignedInt ), str2, v2 ) );
296 ret = build_compoundLiteral( DeclarationNode::newBasicType( DeclarationNode::Int128 )->addType( DeclarationNode::newSignedNess( DeclarationNode::Unsigned ) ),
297 new InitializerNode( (InitializerNode *)(new InitializerNode( new ExpressionNode( v2 == 0 ? ret2 : ret ) ))->set_last( new InitializerNode( new ExpressionNode( v2 == 0 ? ret : ret2 ) ) ), true ) );
298 } else { // explicit length, (length_type)constant
299 ret = new CastExpr( ret, new TypeInstType( Type::Qualifiers(), lnthsInt[Unsigned][ltype], false ), false );
300 if ( ltype == 5 ) { // pointer, intptr( (uintptr_t)constant )
301 ret = build_func( new ExpressionNode( build_varref( new string( "intptr" ) ) ), new ExpressionNode( ret ) );
302 } // if
303 } // if
304 } // if
305
306 CLEANUP: ;
307 delete &str; // created by lex
308 return ret;
309} // build_constantInteger
310
311
312static inline void checkFnxFloat( string & str, size_t last, bool & explnth, int & type ) {
313 string::size_type posn;
314 // floating-point constant has minimum of 2 characters, 1. or .1, so safe to look ahead
315 if ( str[1] == 'x' ) { // hex ?
316 posn = str.find_last_of( "pP" ); // back for exponent (must have)
317 posn = str.find_first_of( "fF", posn + 1 ); // forward for size (fF allowed in hex constant)
318 } else {
319 posn = str.find_last_of( "fF" ); // back for size (fF not allowed)
320 } // if
321 if ( posn == string::npos ) return;
322 explnth = true;
323 posn += 1; // advance to size
324 if ( str[posn] == '3' ) { // 32
325 if ( str[last] != 'x' ) type = 6;
326 else type = 7;
327 } else if ( str[posn] == '6' ) { // 64
328 if ( str[last] != 'x' ) type = 8;
329 else type = 9;
330 } else if ( str[posn] == '8' ) { // 80
331 type = 3;
332 } else if ( str[posn] == '1' ) { // 16/128
333 if ( str[posn + 1] == '6' ) { // 16
334 type = 5;
335 } else { // 128
336 if ( str[last] != 'x' ) type = 10;
337 else type = 11;
338 } // if
339 } else {
340 assertf( false, "internal error, bad floating point length %s", str.c_str() );
341 } // if
342} // checkFnxFloat
343
344
345Expression * build_constantFloat( string & str ) {
346 static const BasicType::Kind kind[2][12] = {
347 { BasicType::Float, BasicType::Double, BasicType::LongDouble, BasicType::uuFloat80, BasicType::uuFloat128, BasicType::uFloat16, BasicType::uFloat32, BasicType::uFloat32x, BasicType::uFloat64, BasicType::uFloat64x, BasicType::uFloat128, BasicType::uFloat128x },
348 { 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 },
349 };
350
351 // floating-point constant has minimum of 2 characters 1. or .1
352 size_t last = str.length() - 1;
353 double v;
354 int type; // 0 => float, 1 => double, 3 => long double, ...
355 bool complx = false; // real, complex
356 bool explnth = false; // explicit literal length
357
358 sscanf( str.c_str(), "%lg", &v );
359
360 if ( checkI( str[last] ) ) { // imaginary ?
361 complx = true;
362 last -= 1; // backup one character
363 } // if
364
365 if ( checkF( str[last] ) ) { // float ?
366 type = 0;
367 } else if ( checkD( str[last] ) ) { // double ?
368 type = 1;
369 } else if ( checkL( str[last] ) ) { // long double ?
370 type = 2;
371 } else if ( checkF80( str[last] ) ) { // __float80 ?
372 type = 3;
373 } else if ( checkF128( str[last] ) ) { // __float128 ?
374 type = 4;
375 } else {
376 type = 1; // double (default if no suffix)
377 checkFnxFloat( str, last, explnth, type );
378 } // if
379
380 if ( ! complx && checkI( str[last - 1] ) ) { // imaginary ?
381 complx = true;
382 } // if
383
384 assert( 0 <= type && type < 12 );
385 Expression * ret = new ConstantExpr( Constant( new BasicType( noQualifiers, kind[complx][type] ), str, v ) );
386 if ( explnth ) { // explicit length ?
387 ret = new CastExpr( ret, new BasicType( Type::Qualifiers(), kind[complx][type] ), false );
388 } // if
389
390 delete &str; // created by lex
391 return ret;
392} // build_constantFloat
393
394static void sepString( string & str, string & units, char delimit ) {
395 string::size_type posn = str.find_last_of( delimit ) + 1;
396 if ( posn != str.length() ) {
397 units = "?" + str.substr( posn ); // extract units
398 str.erase( posn ); // remove units
399 } // if
400} // sepString
401
402Expression * build_constantChar( string & str ) {
403 string units; // units
404 sepString( str, units, '\'' ); // separate constant from units
405
406 Expression * ret = new ConstantExpr( Constant( new BasicType( noQualifiers, BasicType::Char ), str, (unsigned long long int)(unsigned char)str[1] ) );
407 if ( units.length() != 0 ) {
408 ret = new UntypedExpr( new NameExpr( units ), { ret } );
409 } // if
410
411 delete &str; // created by lex
412 return ret;
413} // build_constantChar
414
415Expression * build_constantStr( string & str ) {
416 assert( str.length() > 0 );
417 string units; // units
418 sepString( str, units, '"' ); // separate constant from units
419
420 Type * strtype;
421 switch ( str[0] ) { // str has >= 2 characters, i.e, null string "" => safe to look at subscripts 0/1
422 case 'u':
423 if ( str[1] == '8' ) goto Default; // utf-8 characters => array of char
424 // lookup type of associated typedef
425 strtype = new TypeInstType( Type::Qualifiers( Type::Const ), "char16_t", false );
426 break;
427 case 'U':
428 strtype = new TypeInstType( Type::Qualifiers( Type::Const ), "char32_t", false );
429 break;
430 case 'L':
431 strtype = new TypeInstType( Type::Qualifiers( Type::Const ), "wchar_t", false );
432 break;
433 Default: // char default string type
434 default:
435 strtype = new BasicType( Type::Qualifiers( Type::Const ), BasicType::Char );
436 } // switch
437 ArrayType * at = new ArrayType( noQualifiers, strtype,
438 new ConstantExpr( Constant::from_ulong( str.size() + 1 - 2 ) ), // +1 for '\0' and -2 for '"'
439 false, false );
440 Expression * ret = new ConstantExpr( Constant( at, str, std::nullopt ) );
441 if ( units.length() != 0 ) {
442 ret = new UntypedExpr( new NameExpr( units ), { ret } );
443 } // if
444
445 delete &str; // created by lex
446 return ret;
447} // build_constantStr
448
449Expression * build_field_name_FLOATING_FRACTIONconstant( const string & str ) {
450 if ( str.find_first_not_of( "0123456789", 1 ) != string::npos ) SemanticError( yylloc, "invalid tuple index " + str );
451 Expression * ret = build_constantInteger( *new string( str.substr(1) ) );
452 delete &str;
453 return ret;
454} // build_field_name_FLOATING_FRACTIONconstant
455
456Expression * build_field_name_FLOATING_DECIMALconstant( const string & str ) {
457 if ( str[str.size() - 1] != '.' ) SemanticError( yylloc, "invalid tuple index " + str );
458 Expression * ret = build_constantInteger( *new string( str.substr( 0, str.size()-1 ) ) );
459 delete &str;
460 return ret;
461} // build_field_name_FLOATING_DECIMALconstant
462
463Expression * build_field_name_FLOATINGconstant( const string & str ) {
464 // str is of the form A.B -> separate at the . and return member expression
465 int a, b;
466 char dot;
467 stringstream ss( str );
468 ss >> a >> dot >> b;
469 UntypedMemberExpr * ret = new UntypedMemberExpr( new ConstantExpr( Constant::from_int( b ) ), new ConstantExpr( Constant::from_int( a ) ) );
470 delete &str;
471 return ret;
472} // build_field_name_FLOATINGconstant
473
474Expression * make_field_name_fraction_constants( Expression * fieldName, Expression * fracts ) {
475 if ( fracts ) {
476 if ( UntypedMemberExpr * memberExpr = dynamic_cast< UntypedMemberExpr * >( fracts ) ) {
477 memberExpr->set_member( make_field_name_fraction_constants( fieldName, memberExpr->get_aggregate() ) );
478 return memberExpr;
479 } else {
480 return new UntypedMemberExpr( fracts, fieldName );
481 } // if
482 } // if
483 return fieldName;
484} // make_field_name_fraction_constants
485
486Expression * build_field_name_fraction_constants( Expression * fieldName, ExpressionNode * fracts ) {
487 return make_field_name_fraction_constants( fieldName, maybeMoveBuild< Expression >( fracts ) );
488} // build_field_name_fraction_constants
489
490NameExpr * build_varref( const string * name ) {
491 NameExpr * expr = new NameExpr( *name );
492 delete name;
493 return expr;
494} // build_varref
495
496// TODO: get rid of this and OperKinds and reuse code from OperatorTable
497static const char * OperName[] = { // must harmonize with OperKinds
498 // diadic
499 "SizeOf", "AlignOf", "OffsetOf", "?+?", "?-?", "?\\?", "?*?", "?/?", "?%?", "||", "&&",
500 "?|?", "?&?", "?^?", "Cast", "?<<?", "?>>?", "?<?", "?>?", "?<=?", "?>=?", "?==?", "?!=?",
501 "?=?", "?@=?", "?\\=?", "?*=?", "?/=?", "?%=?", "?+=?", "?-=?", "?<<=?", "?>>=?", "?&=?", "?^=?", "?|=?",
502 "?[?]", "...",
503 // monadic
504 "+?", "-?", "AddressOf", "*?", "!?", "~?", "++?", "?++", "--?", "?--",
505}; // OperName
506
507Expression * build_cast( DeclarationNode * decl_node, ExpressionNode * expr_node ) {
508 Type * targetType = maybeMoveBuildType( decl_node );
509 if ( dynamic_cast< VoidType * >( targetType ) ) {
510 delete targetType;
511 return new CastExpr( maybeMoveBuild< Expression >(expr_node), false );
512 } else {
513 return new CastExpr( maybeMoveBuild< Expression >(expr_node), targetType, false );
514 } // if
515} // build_cast
516
517Expression * build_keyword_cast( AggregateDecl::Aggregate target, ExpressionNode * expr_node ) {
518 return new KeywordCastExpr( maybeMoveBuild< Expression >(expr_node), target );
519}
520
521Expression * build_virtual_cast( DeclarationNode * decl_node, ExpressionNode * expr_node ) {
522 return new VirtualCastExpr( maybeMoveBuild< Expression >( expr_node ), maybeMoveBuildType( decl_node ) );
523} // build_virtual_cast
524
525Expression * build_fieldSel( ExpressionNode * expr_node, Expression * member ) {
526 return new UntypedMemberExpr( member, maybeMoveBuild< Expression >(expr_node) );
527} // build_fieldSel
528
529Expression * build_pfieldSel( ExpressionNode * expr_node, Expression * member ) {
530 UntypedExpr * deref = new UntypedExpr( new NameExpr( "*?" ) );
531 deref->location = expr_node->location;
532 deref->get_args().push_back( maybeMoveBuild< Expression >(expr_node) );
533 UntypedMemberExpr * ret = new UntypedMemberExpr( member, deref );
534 return ret;
535} // build_pfieldSel
536
537Expression * build_offsetOf( DeclarationNode * decl_node, NameExpr * member ) {
538 Expression * ret = new UntypedOffsetofExpr( maybeMoveBuildType( decl_node ), member->get_name() );
539 delete member;
540 return ret;
541} // build_offsetOf
542
543Expression * build_and_or( ExpressionNode * expr_node1, ExpressionNode * expr_node2, bool kind ) {
544 return new LogicalExpr( notZeroExpr( maybeMoveBuild< Expression >(expr_node1) ), notZeroExpr( maybeMoveBuild< Expression >(expr_node2) ), kind );
545} // build_and_or
546
547Expression * build_unary_val( OperKinds op, ExpressionNode * expr_node ) {
548 list< Expression * > args;
549 args.push_back( maybeMoveBuild< Expression >(expr_node) );
550 return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
551} // build_unary_val
552
553Expression * build_unary_ptr( OperKinds op, ExpressionNode * expr_node ) {
554 list< Expression * > args;
555 args.push_back( maybeMoveBuild< Expression >(expr_node) ); // xxx -- this is exactly the same as the val case now, refactor this code.
556 return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
557} // build_unary_ptr
558
559Expression * build_binary_val( OperKinds op, ExpressionNode * expr_node1, ExpressionNode * expr_node2 ) {
560 list< Expression * > args;
561 args.push_back( maybeMoveBuild< Expression >(expr_node1) );
562 args.push_back( maybeMoveBuild< Expression >(expr_node2) );
563 return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
564} // build_binary_val
565
566Expression * build_binary_ptr( OperKinds op, ExpressionNode * expr_node1, ExpressionNode * expr_node2 ) {
567 list< Expression * > args;
568 args.push_back( maybeMoveBuild< Expression >(expr_node1) );
569 args.push_back( maybeMoveBuild< Expression >(expr_node2) );
570 return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
571} // build_binary_ptr
572
573Expression * build_cond( ExpressionNode * expr_node1, ExpressionNode * expr_node2, ExpressionNode * expr_node3 ) {
574 return new ConditionalExpr( notZeroExpr( maybeMoveBuild< Expression >(expr_node1) ), maybeMoveBuild< Expression >(expr_node2), maybeMoveBuild< Expression >(expr_node3) );
575} // build_cond
576
577Expression * build_tuple( ExpressionNode * expr_node ) {
578 list< Expression * > exprs;
579 buildMoveList( expr_node, exprs );
580 return new UntypedTupleExpr( exprs );;
581} // build_tuple
582
583Expression * build_func( ExpressionNode * function, ExpressionNode * expr_node ) {
584 list< Expression * > args;
585 buildMoveList( expr_node, args );
586 return new UntypedExpr( maybeMoveBuild< Expression >(function), args );
587} // build_func
588
589Expression * build_compoundLiteral( DeclarationNode * decl_node, InitializerNode * kids ) {
590 Declaration * newDecl = maybeBuild< Declaration >(decl_node); // compound literal type
591 if ( DeclarationWithType * newDeclWithType = dynamic_cast< DeclarationWithType * >( newDecl ) ) { // non-sue compound-literal type
592 return new CompoundLiteralExpr( newDeclWithType->get_type(), maybeMoveBuild< Initializer >(kids) );
593 // these types do not have associated type information
594 } else if ( StructDecl * newDeclStructDecl = dynamic_cast< StructDecl * >( newDecl ) ) {
595 if ( newDeclStructDecl->has_body() ) {
596 return new CompoundLiteralExpr( new StructInstType( Type::Qualifiers(), newDeclStructDecl ), maybeMoveBuild< Initializer >(kids) );
597 } else {
598 return new CompoundLiteralExpr( new StructInstType( Type::Qualifiers(), newDeclStructDecl->get_name() ), maybeMoveBuild< Initializer >(kids) );
599 } // if
600 } else if ( UnionDecl * newDeclUnionDecl = dynamic_cast< UnionDecl * >( newDecl ) ) {
601 if ( newDeclUnionDecl->has_body() ) {
602 return new CompoundLiteralExpr( new UnionInstType( Type::Qualifiers(), newDeclUnionDecl ), maybeMoveBuild< Initializer >(kids) );
603 } else {
604 return new CompoundLiteralExpr( new UnionInstType( Type::Qualifiers(), newDeclUnionDecl->get_name() ), maybeMoveBuild< Initializer >(kids) );
605 } // if
606 } else if ( EnumDecl * newDeclEnumDecl = dynamic_cast< EnumDecl * >( newDecl ) ) {
607 if ( newDeclEnumDecl->has_body() ) {
608 return new CompoundLiteralExpr( new EnumInstType( Type::Qualifiers(), newDeclEnumDecl ), maybeMoveBuild< Initializer >(kids) );
609 } else {
610 return new CompoundLiteralExpr( new EnumInstType( Type::Qualifiers(), newDeclEnumDecl->get_name() ), maybeMoveBuild< Initializer >(kids) );
611 } // if
612 } else {
613 assert( false );
614 } // if
615} // build_compoundLiteral
616
617// Local Variables: //
618// tab-width: 4 //
619// mode: c++ //
620// compile-command: "make install" //
621// End: //
Note: See TracBrowser for help on using the repository browser.