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 : Sun Mar 10 16:10:32 2019
|
---|
13 | // Update Count : 976
|
---|
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 |
|
---|
33 | class Initializer;
|
---|
34 |
|
---|
35 | using 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 | extern 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'; }
|
---|
56 | static inline bool checkF( char c ) { return c == 'f' || c == 'F'; }
|
---|
57 | static inline bool checkD( char c ) { return c == 'd' || c == 'D'; }
|
---|
58 | static inline bool checkF80( char c ) { return c == 'w' || c == 'W'; }
|
---|
59 | static inline bool checkF128( char c ) { return c == 'q' || c == 'Q'; }
|
---|
60 | static inline bool checkL( char c ) { return c == 'l' || c == 'L'; }
|
---|
61 | static inline bool checkI( char c ) { return c == 'i' || c == 'I'; }
|
---|
62 | static inline bool checkB( char c ) { return c == 'b' || c == 'B'; }
|
---|
63 | static inline bool checkX( char c ) { return c == 'x' || c == 'X'; }
|
---|
64 | // static inline bool checkN( char c ) { return c == 'n' || c == 'N'; }
|
---|
65 |
|
---|
66 | void 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
|
---|
88 | } // lnthSuffix
|
---|
89 |
|
---|
90 | void 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 |
|
---|
110 | Expression * build_constantInteger( string & str ) {
|
---|
111 | static const BasicType::Kind kind[2][7] = {
|
---|
112 | // short (h) must be before char (hh) because shorter type has the longer suffix
|
---|
113 | { BasicType::ShortSignedInt, BasicType::SignedChar, BasicType::SignedInt, BasicType::LongSignedInt, BasicType::LongLongSignedInt, BasicType::SignedInt128, },
|
---|
114 | { BasicType::ShortUnsignedInt, BasicType::UnsignedChar, BasicType::UnsignedInt, BasicType::LongUnsignedInt, BasicType::LongLongUnsignedInt, BasicType::UnsignedInt128, },
|
---|
115 | };
|
---|
116 |
|
---|
117 | static const char * lnthsInt[2][6] = {
|
---|
118 | { "int16_t", "int8_t", "int32_t", "int64_t", "size_t", "uintptr_t", },
|
---|
119 | { "uint16_t", "uint8_t", "uint32_t", "uint64_t", "size_t", "uintptr_t", },
|
---|
120 | }; // lnthsInt
|
---|
121 |
|
---|
122 | unsigned long long int v; // converted integral value
|
---|
123 | size_t last = str.length() - 1; // last subscript of constant
|
---|
124 | Expression * ret;
|
---|
125 | //string fred( str );
|
---|
126 |
|
---|
127 | int type = -1; // 0 => short, 1 => char, 2 => int, 3 => long int, 4 => long long int, 5 => int128
|
---|
128 | int ltype = -1; // 0 => 16 bits, 1 => 8 bits, 2 => 32 bits, 3 => 64 bits, 4 => size_t, 5 => intptr, 6 => pointer
|
---|
129 | bool dec = true, Unsigned = false; // decimal, unsigned constant
|
---|
130 |
|
---|
131 | // special constants
|
---|
132 | if ( str == "0" ) {
|
---|
133 | ret = new ConstantExpr( Constant( (Type *)new ZeroType( noQualifiers ), str, (unsigned long long int)0 ) );
|
---|
134 | goto CLEANUP;
|
---|
135 | } // if
|
---|
136 | if ( str == "1" ) {
|
---|
137 | ret = new ConstantExpr( Constant( (Type *)new OneType( noQualifiers ), str, (unsigned long long int)1 ) );
|
---|
138 | goto CLEANUP;
|
---|
139 | } // if
|
---|
140 |
|
---|
141 | // Cannot be just "0"/"1"; sscanf stops at the suffix, if any; value goes over the wall => always generate
|
---|
142 |
|
---|
143 | if ( str[0] == '0' ) { // radix character ?
|
---|
144 | dec = false;
|
---|
145 | if ( checkX( str[1] ) ) { // hex constant ?
|
---|
146 | sscanf( (char *)str.c_str(), "%llx", &v );
|
---|
147 | //printf( "%llx %llu\n", v, v );
|
---|
148 | } else if ( checkB( str[1] ) ) { // binary constant ?
|
---|
149 | v = 0; // compute value
|
---|
150 | for ( unsigned int i = 2;; ) { // ignore prefix
|
---|
151 | if ( str[i] == '1' ) v |= 1;
|
---|
152 | i += 1;
|
---|
153 | if ( i == last - 1 || (str[i] != '0' && str[i] != '1') ) break;
|
---|
154 | v <<= 1;
|
---|
155 | } // for
|
---|
156 | //printf( "%#llx %llu\n", v, v );
|
---|
157 | } else { // octal constant
|
---|
158 | sscanf( (char *)str.c_str(), "%llo", &v );
|
---|
159 | //printf( "%#llo %llu\n", v, v );
|
---|
160 | } // if
|
---|
161 | } else { // decimal constant ?
|
---|
162 | sscanf( (char *)str.c_str(), "%llu", &v );
|
---|
163 | //printf( "%llu\n", v );
|
---|
164 | } // if
|
---|
165 |
|
---|
166 | string::size_type posn;
|
---|
167 |
|
---|
168 | if ( isdigit( str[last] ) ) { // no suffix ?
|
---|
169 | lnthSuffix( str, type, ltype ); // could have length suffix
|
---|
170 | if ( type == -1 ) { // no suffix
|
---|
171 | valueToType( v, dec, type, Unsigned );
|
---|
172 | } // if
|
---|
173 | } else {
|
---|
174 | // At least one digit in integer constant, so safe to backup while looking for suffix.
|
---|
175 |
|
---|
176 | posn = str.find_last_of( "pP" );
|
---|
177 | if ( posn != string::npos ) { valueToType( v, dec, type, Unsigned ); ltype = 5; str.erase( posn, 1 ); goto FINI; }
|
---|
178 |
|
---|
179 | posn = str.find_last_of( "zZ" );
|
---|
180 | if ( posn != string::npos ) { Unsigned = true; type = 2; ltype = 4; str.erase( posn, 1 ); goto FINI; }
|
---|
181 |
|
---|
182 | // 'u' can appear before or after length suffix
|
---|
183 | if ( str.find_last_of( "uU" ) != string::npos ) Unsigned = true;
|
---|
184 |
|
---|
185 | posn = str.rfind( "hh" );
|
---|
186 | if ( posn != string::npos ) { type = 1; str.erase( posn, 2 ); goto FINI; }
|
---|
187 |
|
---|
188 | posn = str.rfind( "HH" );
|
---|
189 | if ( posn != string::npos ) { type = 1; str.erase( posn, 2 ); goto FINI; }
|
---|
190 |
|
---|
191 | posn = str.find_last_of( "hH" );
|
---|
192 | if ( posn != string::npos ) { type = 0; str.erase( posn, 1 ); goto FINI; }
|
---|
193 |
|
---|
194 | posn = str.find_last_of( "nN" );
|
---|
195 | if ( posn != string::npos ) { type = 2; str.erase( posn, 1 ); goto FINI; }
|
---|
196 |
|
---|
197 | if ( str.rfind( "ll" ) != string::npos || str.rfind( "LL" ) != string::npos ) { type = 4; goto FINI; }
|
---|
198 |
|
---|
199 | lnthSuffix( str, type, ltype ); // must be after check for "ll"
|
---|
200 | if ( type == -1 ) { // only 'u' suffix ?
|
---|
201 | valueToType( v, dec, type, Unsigned );
|
---|
202 | } // if
|
---|
203 | FINI: ;
|
---|
204 | } // if
|
---|
205 |
|
---|
206 | //if ( !( 0 <= type && type <= 6 ) ) { printf( "%s %lu %d %s\n", fred.c_str(), fred.length(), type, str.c_str() ); }
|
---|
207 | assert( 0 <= type && type <= 6 );
|
---|
208 |
|
---|
209 | // Constant type is correct for overload resolving.
|
---|
210 | ret = new ConstantExpr( Constant( new BasicType( noQualifiers, kind[Unsigned][type] ), str, v ) );
|
---|
211 | if ( Unsigned && type < 2 ) { // hh or h, less than int ?
|
---|
212 | // int i = -1uh => 65535 not -1, so cast is necessary for unsigned, which unfortunately eliminates warnings for large values.
|
---|
213 | ret = new CastExpr( ret, new BasicType( Type::Qualifiers(), kind[Unsigned][type] ), false );
|
---|
214 | } else if ( ltype != -1 ) { // explicit length ?
|
---|
215 | if ( ltype == 6 ) { // int128, (int128)constant
|
---|
216 | ret = new CastExpr( ret, new BasicType( Type::Qualifiers(), kind[Unsigned][type] ), false );
|
---|
217 | } else { // explicit length, (length_type)constant
|
---|
218 | ret = new CastExpr( ret, new TypeInstType( Type::Qualifiers(), lnthsInt[Unsigned][ltype], false ), false );
|
---|
219 | if ( ltype == 5 ) { // pointer, intptr( (uintptr_t)constant )
|
---|
220 | ret = build_func( new ExpressionNode( build_varref( new string( "intptr" ) ) ), new ExpressionNode( ret ) );
|
---|
221 | } // if
|
---|
222 | } // if
|
---|
223 | } // if
|
---|
224 |
|
---|
225 | CLEANUP: ;
|
---|
226 | delete &str; // created by lex
|
---|
227 | return ret;
|
---|
228 | } // build_constantInteger
|
---|
229 |
|
---|
230 |
|
---|
231 | static inline void checkFnxFloat( string & str, size_t last, bool & explnth, int & type ) {
|
---|
232 | string::size_type posn;
|
---|
233 | // floating-point constant has minimum of 2 characters, 1. or .1, so safe to look ahead
|
---|
234 | if ( str[1] == 'x' ) { // hex ?
|
---|
235 | posn = str.find_last_of( "pP" ); // back for exponent (must have)
|
---|
236 | posn = str.find_first_of( "fF", posn + 1 ); // forward for size (fF allowed in hex constant)
|
---|
237 | } else {
|
---|
238 | posn = str.find_last_of( "fF" ); // back for size (fF not allowed)
|
---|
239 | } // if
|
---|
240 | if ( posn == string::npos ) return;
|
---|
241 | explnth = true;
|
---|
242 | posn += 1; // advance to size
|
---|
243 | if ( str[posn] == '3' ) { // 32
|
---|
244 | if ( str[last] != 'x' ) type = 6;
|
---|
245 | else type = 7;
|
---|
246 | } else if ( str[posn] == '6' ) { // 64
|
---|
247 | if ( str[last] != 'x' ) type = 8;
|
---|
248 | else type = 9;
|
---|
249 | } else if ( str[posn] == '8' ) { // 80
|
---|
250 | type = 3;
|
---|
251 | } else if ( str[posn] == '1' ) { // 16/128
|
---|
252 | if ( str[posn + 1] == '6' ) { // 16
|
---|
253 | type = 5;
|
---|
254 | } else { // 128
|
---|
255 | if ( str[last] != 'x' ) type = 10;
|
---|
256 | else type = 11;
|
---|
257 | } // if
|
---|
258 | } else {
|
---|
259 | assertf( false, "internal error, bad floating point length %s", str.c_str() );
|
---|
260 | } // if
|
---|
261 | } // checkFnxFloat
|
---|
262 |
|
---|
263 |
|
---|
264 | Expression * build_constantFloat( string & str ) {
|
---|
265 | static const BasicType::Kind kind[2][12] = {
|
---|
266 | { BasicType::Float, BasicType::Double, BasicType::LongDouble, BasicType::uuFloat80, BasicType::uuFloat128, BasicType::uFloat16, BasicType::uFloat32, BasicType::uFloat32x, BasicType::uFloat64, BasicType::uFloat64x, BasicType::uFloat128, BasicType::uFloat128x },
|
---|
267 | { BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, (BasicType::Kind)-1, (BasicType::Kind)-1, BasicType::uFloat16Complex, BasicType::uFloat32Complex, BasicType::uFloat32xComplex, BasicType::uFloat64Complex, BasicType::uFloat64xComplex, BasicType::uFloat128Complex, BasicType::uFloat128xComplex },
|
---|
268 | };
|
---|
269 |
|
---|
270 | // floating-point constant has minimum of 2 characters 1. or .1
|
---|
271 | size_t last = str.length() - 1;
|
---|
272 | double v;
|
---|
273 | int type; // 0 => float, 1 => double, 3 => long double, ...
|
---|
274 | bool complx = false; // real, complex
|
---|
275 | bool explnth = false; // explicit literal length
|
---|
276 |
|
---|
277 | sscanf( str.c_str(), "%lg", &v );
|
---|
278 |
|
---|
279 | if ( checkI( str[last] ) ) { // imaginary ?
|
---|
280 | complx = true;
|
---|
281 | last -= 1; // backup one character
|
---|
282 | } // if
|
---|
283 |
|
---|
284 | if ( checkF( str[last] ) ) { // float ?
|
---|
285 | type = 0;
|
---|
286 | } else if ( checkD( str[last] ) ) { // double ?
|
---|
287 | type = 1;
|
---|
288 | } else if ( checkL( str[last] ) ) { // long double ?
|
---|
289 | type = 2;
|
---|
290 | } else if ( checkF80( str[last] ) ) { // __float80 ?
|
---|
291 | type = 3;
|
---|
292 | } else if ( checkF128( str[last] ) ) { // __float128 ?
|
---|
293 | type = 4;
|
---|
294 | } else {
|
---|
295 | type = 1; // double (default if no suffix)
|
---|
296 | checkFnxFloat( str, last, explnth, type );
|
---|
297 | } // if
|
---|
298 |
|
---|
299 | if ( ! complx && checkI( str[last - 1] ) ) { // imaginary ?
|
---|
300 | complx = true;
|
---|
301 | } // if
|
---|
302 |
|
---|
303 | assert( 0 <= type && type < 12 );
|
---|
304 | Expression * ret = new ConstantExpr( Constant( new BasicType( noQualifiers, kind[complx][type] ), str, v ) );
|
---|
305 | if ( explnth ) { // explicit length ?
|
---|
306 | ret = new CastExpr( ret, new BasicType( Type::Qualifiers(), kind[complx][type] ), false );
|
---|
307 | } // if
|
---|
308 |
|
---|
309 | delete &str; // created by lex
|
---|
310 | return ret;
|
---|
311 | } // build_constantFloat
|
---|
312 |
|
---|
313 | static void sepString( string & str, string & units, char delimit ) {
|
---|
314 | string::size_type posn = str.find_last_of( delimit ) + 1;
|
---|
315 | if ( posn != str.length() ) {
|
---|
316 | units = "?" + str.substr( posn ); // extract units
|
---|
317 | str.erase( posn ); // remove units
|
---|
318 | } // if
|
---|
319 | } // sepString
|
---|
320 |
|
---|
321 | Expression * build_constantChar( string & str ) {
|
---|
322 | string units; // units
|
---|
323 | sepString( str, units, '\'' ); // separate constant from units
|
---|
324 |
|
---|
325 | Expression * ret = new ConstantExpr( Constant( new BasicType( noQualifiers, BasicType::Char ), str, (unsigned long long int)(unsigned char)str[1] ) );
|
---|
326 | if ( units.length() != 0 ) {
|
---|
327 | ret = new UntypedExpr( new NameExpr( units ), { ret } );
|
---|
328 | } // if
|
---|
329 |
|
---|
330 | delete &str; // created by lex
|
---|
331 | return ret;
|
---|
332 | } // build_constantChar
|
---|
333 |
|
---|
334 | Expression * build_constantStr( string & str ) {
|
---|
335 | assert( str.length() > 0 );
|
---|
336 | string units; // units
|
---|
337 | sepString( str, units, '"' ); // separate constant from units
|
---|
338 |
|
---|
339 | Type * strtype;
|
---|
340 | switch ( str[0] ) { // str has >= 2 characters, i.e, null string "" => safe to look at subscripts 0/1
|
---|
341 | case 'u':
|
---|
342 | if ( str[1] == '8' ) goto Default; // utf-8 characters => array of char
|
---|
343 | // lookup type of associated typedef
|
---|
344 | strtype = new TypeInstType( Type::Qualifiers( Type::Const ), "char16_t", false );
|
---|
345 | break;
|
---|
346 | case 'U':
|
---|
347 | strtype = new TypeInstType( Type::Qualifiers( Type::Const ), "char32_t", false );
|
---|
348 | break;
|
---|
349 | case 'L':
|
---|
350 | strtype = new TypeInstType( Type::Qualifiers( Type::Const ), "wchar_t", false );
|
---|
351 | break;
|
---|
352 | Default: // char default string type
|
---|
353 | default:
|
---|
354 | strtype = new BasicType( Type::Qualifiers( Type::Const ), BasicType::Char );
|
---|
355 | } // switch
|
---|
356 | ArrayType * at = new ArrayType( noQualifiers, strtype,
|
---|
357 | new ConstantExpr( Constant::from_ulong( str.size() + 1 - 2 ) ), // +1 for '\0' and -2 for '"'
|
---|
358 | false, false );
|
---|
359 | Expression * ret = new ConstantExpr( Constant( at, str, std::nullopt ) );
|
---|
360 | if ( units.length() != 0 ) {
|
---|
361 | ret = new UntypedExpr( new NameExpr( units ), { ret } );
|
---|
362 | } // if
|
---|
363 |
|
---|
364 | delete &str; // created by lex
|
---|
365 | return ret;
|
---|
366 | } // build_constantStr
|
---|
367 |
|
---|
368 | Expression * build_field_name_FLOATING_FRACTIONconstant( const string & str ) {
|
---|
369 | if ( str.find_first_not_of( "0123456789", 1 ) != string::npos ) SemanticError( yylloc, "invalid tuple index " + str );
|
---|
370 | Expression * ret = build_constantInteger( *new string( str.substr(1) ) );
|
---|
371 | delete &str;
|
---|
372 | return ret;
|
---|
373 | } // build_field_name_FLOATING_FRACTIONconstant
|
---|
374 |
|
---|
375 | Expression * build_field_name_FLOATING_DECIMALconstant( const string & str ) {
|
---|
376 | if ( str[str.size()-1] != '.' ) SemanticError( yylloc, "invalid tuple index " + str );
|
---|
377 | Expression * ret = build_constantInteger( *new string( str.substr( 0, str.size()-1 ) ) );
|
---|
378 | delete &str;
|
---|
379 | return ret;
|
---|
380 | } // build_field_name_FLOATING_DECIMALconstant
|
---|
381 |
|
---|
382 | Expression * build_field_name_FLOATINGconstant( const string & str ) {
|
---|
383 | // str is of the form A.B -> separate at the . and return member expression
|
---|
384 | int a, b;
|
---|
385 | char dot;
|
---|
386 | stringstream ss( str );
|
---|
387 | ss >> a >> dot >> b;
|
---|
388 | UntypedMemberExpr * ret = new UntypedMemberExpr( new ConstantExpr( Constant::from_int( b ) ), new ConstantExpr( Constant::from_int( a ) ) );
|
---|
389 | delete &str;
|
---|
390 | return ret;
|
---|
391 | } // build_field_name_FLOATINGconstant
|
---|
392 |
|
---|
393 | Expression * make_field_name_fraction_constants( Expression * fieldName, Expression * fracts ) {
|
---|
394 | if ( fracts ) {
|
---|
395 | if ( UntypedMemberExpr * memberExpr = dynamic_cast< UntypedMemberExpr * >( fracts ) ) {
|
---|
396 | memberExpr->set_member( make_field_name_fraction_constants( fieldName, memberExpr->get_aggregate() ) );
|
---|
397 | return memberExpr;
|
---|
398 | } else {
|
---|
399 | return new UntypedMemberExpr( fracts, fieldName );
|
---|
400 | } // if
|
---|
401 | } // if
|
---|
402 | return fieldName;
|
---|
403 | } // make_field_name_fraction_constants
|
---|
404 |
|
---|
405 | Expression * build_field_name_fraction_constants( Expression * fieldName, ExpressionNode * fracts ) {
|
---|
406 | return make_field_name_fraction_constants( fieldName, maybeMoveBuild< Expression >( fracts ) );
|
---|
407 | } // build_field_name_fraction_constants
|
---|
408 |
|
---|
409 | NameExpr * build_varref( const string * name ) {
|
---|
410 | NameExpr * expr = new NameExpr( *name );
|
---|
411 | delete name;
|
---|
412 | return expr;
|
---|
413 | } // build_varref
|
---|
414 |
|
---|
415 | // TODO: get rid of this and OperKinds and reuse code from OperatorTable
|
---|
416 | static const char * OperName[] = { // must harmonize with OperKinds
|
---|
417 | // diadic
|
---|
418 | "SizeOf", "AlignOf", "OffsetOf", "?+?", "?-?", "?\\?", "?*?", "?/?", "?%?", "||", "&&",
|
---|
419 | "?|?", "?&?", "?^?", "Cast", "?<<?", "?>>?", "?<?", "?>?", "?<=?", "?>=?", "?==?", "?!=?",
|
---|
420 | "?=?", "?@=?", "?\\=?", "?*=?", "?/=?", "?%=?", "?+=?", "?-=?", "?<<=?", "?>>=?", "?&=?", "?^=?", "?|=?",
|
---|
421 | "?[?]", "...",
|
---|
422 | // monadic
|
---|
423 | "+?", "-?", "AddressOf", "*?", "!?", "~?", "++?", "?++", "--?", "?--",
|
---|
424 | }; // OperName
|
---|
425 |
|
---|
426 | Expression * build_cast( DeclarationNode * decl_node, ExpressionNode * expr_node ) {
|
---|
427 | Type * targetType = maybeMoveBuildType( decl_node );
|
---|
428 | if ( dynamic_cast< VoidType * >( targetType ) ) {
|
---|
429 | delete targetType;
|
---|
430 | return new CastExpr( maybeMoveBuild< Expression >(expr_node), false );
|
---|
431 | } else {
|
---|
432 | return new CastExpr( maybeMoveBuild< Expression >(expr_node), targetType, false );
|
---|
433 | } // if
|
---|
434 | } // build_cast
|
---|
435 |
|
---|
436 | Expression * build_keyword_cast( KeywordCastExpr::Target target, ExpressionNode * expr_node ) {
|
---|
437 | return new KeywordCastExpr( maybeMoveBuild< Expression >(expr_node), target );
|
---|
438 | }
|
---|
439 |
|
---|
440 | Expression * build_virtual_cast( DeclarationNode * decl_node, ExpressionNode * expr_node ) {
|
---|
441 | return new VirtualCastExpr( maybeMoveBuild< Expression >( expr_node ), maybeMoveBuildType( decl_node ) );
|
---|
442 | } // build_virtual_cast
|
---|
443 |
|
---|
444 | Expression * build_fieldSel( ExpressionNode * expr_node, Expression * member ) {
|
---|
445 | return new UntypedMemberExpr( member, maybeMoveBuild< Expression >(expr_node) );
|
---|
446 | } // build_fieldSel
|
---|
447 |
|
---|
448 | Expression * build_pfieldSel( ExpressionNode * expr_node, Expression * member ) {
|
---|
449 | UntypedExpr * deref = new UntypedExpr( new NameExpr( "*?" ) );
|
---|
450 | deref->location = expr_node->location;
|
---|
451 | deref->get_args().push_back( maybeMoveBuild< Expression >(expr_node) );
|
---|
452 | UntypedMemberExpr * ret = new UntypedMemberExpr( member, deref );
|
---|
453 | return ret;
|
---|
454 | } // build_pfieldSel
|
---|
455 |
|
---|
456 | Expression * build_offsetOf( DeclarationNode * decl_node, NameExpr * member ) {
|
---|
457 | Expression * ret = new UntypedOffsetofExpr( maybeMoveBuildType( decl_node ), member->get_name() );
|
---|
458 | delete member;
|
---|
459 | return ret;
|
---|
460 | } // build_offsetOf
|
---|
461 |
|
---|
462 | Expression * build_and_or( ExpressionNode * expr_node1, ExpressionNode * expr_node2, bool kind ) {
|
---|
463 | return new LogicalExpr( notZeroExpr( maybeMoveBuild< Expression >(expr_node1) ), notZeroExpr( maybeMoveBuild< Expression >(expr_node2) ), kind );
|
---|
464 | } // build_and_or
|
---|
465 |
|
---|
466 | Expression * build_unary_val( OperKinds op, ExpressionNode * expr_node ) {
|
---|
467 | list< Expression * > args;
|
---|
468 | args.push_back( maybeMoveBuild< Expression >(expr_node) );
|
---|
469 | return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
|
---|
470 | } // build_unary_val
|
---|
471 |
|
---|
472 | Expression * build_unary_ptr( OperKinds op, ExpressionNode * expr_node ) {
|
---|
473 | list< Expression * > args;
|
---|
474 | args.push_back( maybeMoveBuild< Expression >(expr_node) ); // xxx -- this is exactly the same as the val case now, refactor this code.
|
---|
475 | return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
|
---|
476 | } // build_unary_ptr
|
---|
477 |
|
---|
478 | Expression * build_binary_val( OperKinds op, ExpressionNode * expr_node1, ExpressionNode * expr_node2 ) {
|
---|
479 | list< Expression * > args;
|
---|
480 | args.push_back( maybeMoveBuild< Expression >(expr_node1) );
|
---|
481 | args.push_back( maybeMoveBuild< Expression >(expr_node2) );
|
---|
482 | return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
|
---|
483 | } // build_binary_val
|
---|
484 |
|
---|
485 | Expression * build_binary_ptr( OperKinds op, ExpressionNode * expr_node1, ExpressionNode * expr_node2 ) {
|
---|
486 | list< Expression * > args;
|
---|
487 | args.push_back( maybeMoveBuild< Expression >(expr_node1) );
|
---|
488 | args.push_back( maybeMoveBuild< Expression >(expr_node2) );
|
---|
489 | return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
|
---|
490 | } // build_binary_ptr
|
---|
491 |
|
---|
492 | Expression * build_cond( ExpressionNode * expr_node1, ExpressionNode * expr_node2, ExpressionNode * expr_node3 ) {
|
---|
493 | return new ConditionalExpr( notZeroExpr( maybeMoveBuild< Expression >(expr_node1) ), maybeMoveBuild< Expression >(expr_node2), maybeMoveBuild< Expression >(expr_node3) );
|
---|
494 | } // build_cond
|
---|
495 |
|
---|
496 | Expression * build_tuple( ExpressionNode * expr_node ) {
|
---|
497 | list< Expression * > exprs;
|
---|
498 | buildMoveList( expr_node, exprs );
|
---|
499 | return new UntypedTupleExpr( exprs );;
|
---|
500 | } // build_tuple
|
---|
501 |
|
---|
502 | Expression * build_func( ExpressionNode * function, ExpressionNode * expr_node ) {
|
---|
503 | list< Expression * > args;
|
---|
504 | buildMoveList( expr_node, args );
|
---|
505 | return new UntypedExpr( maybeMoveBuild< Expression >(function), args );
|
---|
506 | } // build_func
|
---|
507 |
|
---|
508 | Expression * build_compoundLiteral( DeclarationNode * decl_node, InitializerNode * kids ) {
|
---|
509 | Declaration * newDecl = maybeBuild< Declaration >(decl_node); // compound literal type
|
---|
510 | if ( DeclarationWithType * newDeclWithType = dynamic_cast< DeclarationWithType * >( newDecl ) ) { // non-sue compound-literal type
|
---|
511 | return new CompoundLiteralExpr( newDeclWithType->get_type(), maybeMoveBuild< Initializer >(kids) );
|
---|
512 | // these types do not have associated type information
|
---|
513 | } else if ( StructDecl * newDeclStructDecl = dynamic_cast< StructDecl * >( newDecl ) ) {
|
---|
514 | if ( newDeclStructDecl->has_body() ) {
|
---|
515 | return new CompoundLiteralExpr( new StructInstType( Type::Qualifiers(), newDeclStructDecl ), maybeMoveBuild< Initializer >(kids) );
|
---|
516 | } else {
|
---|
517 | return new CompoundLiteralExpr( new StructInstType( Type::Qualifiers(), newDeclStructDecl->get_name() ), maybeMoveBuild< Initializer >(kids) );
|
---|
518 | } // if
|
---|
519 | } else if ( UnionDecl * newDeclUnionDecl = dynamic_cast< UnionDecl * >( newDecl ) ) {
|
---|
520 | if ( newDeclUnionDecl->has_body() ) {
|
---|
521 | return new CompoundLiteralExpr( new UnionInstType( Type::Qualifiers(), newDeclUnionDecl ), maybeMoveBuild< Initializer >(kids) );
|
---|
522 | } else {
|
---|
523 | return new CompoundLiteralExpr( new UnionInstType( Type::Qualifiers(), newDeclUnionDecl->get_name() ), maybeMoveBuild< Initializer >(kids) );
|
---|
524 | } // if
|
---|
525 | } else if ( EnumDecl * newDeclEnumDecl = dynamic_cast< EnumDecl * >( newDecl ) ) {
|
---|
526 | if ( newDeclEnumDecl->has_body() ) {
|
---|
527 | return new CompoundLiteralExpr( new EnumInstType( Type::Qualifiers(), newDeclEnumDecl ), maybeMoveBuild< Initializer >(kids) );
|
---|
528 | } else {
|
---|
529 | return new CompoundLiteralExpr( new EnumInstType( Type::Qualifiers(), newDeclEnumDecl->get_name() ), maybeMoveBuild< Initializer >(kids) );
|
---|
530 | } // if
|
---|
531 | } else {
|
---|
532 | assert( false );
|
---|
533 | } // if
|
---|
534 | } // build_compoundLiteral
|
---|
535 |
|
---|
536 | // Local Variables: //
|
---|
537 | // tab-width: 4 //
|
---|
538 | // mode: c++ //
|
---|
539 | // compile-command: "make install" //
|
---|
540 | // End: //
|
---|