source: src/Parser/ExpressionNode.cc@ 1cdc052

ADT ast-experimental
Last change on this file since 1cdc052 was 702e826, checked in by Andrew Beach <ajbeach@…>, 3 years ago

Pre-translation pass on the parser. Entirely code readability improvements, no behaviour (on a larger scale) should be effected.

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