source: src/Parser/ExpressionNode.cc@ bb7422a

ADT ast-experimental
Last change on this file since bb7422a was bb7422a, checked in by Andrew Beach <ajbeach@…>, 2 years ago

Translated parser to the new ast. This incuded a small fix in the resolver so larger expressions can be used in with statements and some updated tests. errors/declaration just is a formatting update. attributes now actually preserves more attributes (unknown if all versions work).

  • Property mode set to 100644
File size: 28.8 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// ExpressionNode.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 : Tue Apr 4 11:07:00 2023
13// Update Count : 1083
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 "AST/Expr.hpp" // for NameExpr
24#include "AST/Type.hpp" // for BaseType, SueInstType
25#include "Common/SemanticError.h" // for SemanticError
26#include "Common/utility.h" // for maybeMoveBuild, maybeBuild, CodeLo...
27#include "ParseNode.h" // for ExpressionNode, maybeMoveBuildType
28#include "parserutility.h" // for notZeroExpr
29
30using namespace std;
31
32//##############################################################################
33
34// Difficult to separate extra parts of constants during lexing because actions are not allow in the middle of patterns:
35//
36// prefix action constant action suffix
37//
38// Alternatively, breaking a pattern using BEGIN does not work if the following pattern can be empty:
39//
40// constant BEGIN CONT ...
41// <CONT>(...)? BEGIN 0 ... // possible empty suffix
42//
43// because the CONT rule is NOT triggered if the pattern is empty. Hence, constants are reparsed here to determine their
44// type.
45
46// static inline bool checkH( char c ) { return c == 'h' || c == 'H'; }
47// static inline bool checkZ( char c ) { return c == 'z' || c == 'Z'; }
48// static inline bool checkU( char c ) { return c == 'u' || c == 'U'; }
49static inline bool checkF( char c ) { return c == 'f' || c == 'F'; }
50static inline bool checkD( char c ) { return c == 'd' || c == 'D'; }
51static inline bool checkF80( char c ) { return c == 'w' || c == 'W'; }
52static inline bool checkF128( char c ) { return c == 'q' || c == 'Q'; }
53static inline bool checkL( char c ) { return c == 'l' || c == 'L'; }
54static inline bool checkI( char c ) { return c == 'i' || c == 'I'; }
55static inline bool checkB( char c ) { return c == 'b' || c == 'B'; }
56static inline bool checkX( char c ) { return c == 'x' || c == 'X'; }
57// static inline bool checkN( char c ) { return c == 'n' || c == 'N'; }
58
59void lnthSuffix( string & str, int & type, int & ltype ) {
60 // 'u' can appear before or after length suffix
61 string::size_type posn = str.find_last_of( "lL" );
62
63 if ( posn == string::npos ) return; // no suffix
64 size_t end = str.length() - 1;
65 if ( posn == end ) { type = 3; return; } // no length after 'l' => long
66
67 string::size_type next = posn + 1; // advance to length
68 if ( str[next] == '3' ) { // 32
69 type = ltype = 2;
70 } else if ( str[next] == '6' ) { // 64
71 type = ltype = 3;
72 } else if ( str[next] == '8' ) { // 8
73 type = ltype = 1;
74 } else if ( str[next] == '1' ) {
75 if ( str[next + 1] == '6' ) { // 16
76 type = ltype = 0;
77 } else { // 128
78 type = 5; ltype = 6;
79 } // if
80 } // if
81
82 char fix = '\0';
83 if ( str[end] == 'u' || str[end] == 'U' ) fix = str[end]; // ends with 'uU' ?
84 str.erase( posn ); // remove length suffix and possibly uU
85 if ( type == 5 ) { // L128 does not need uU
86 end = str.length() - 1;
87 if ( str[end] == 'u' || str[end] == 'U' ) str.erase( end ); // ends with 'uU' ? remove
88 } else if ( fix != '\0' ) str += fix; // put 'uU' back if removed
89} // lnthSuffix
90
91void valueToType( unsigned long long int & v, bool dec, int & type, bool & Unsigned ) {
92 // use value to determine type
93 if ( v <= INT_MAX ) { // signed int
94 type = 2;
95 } else if ( v <= UINT_MAX && ! dec ) { // unsigned int
96 type = 2;
97 Unsigned = true; // unsigned
98 } else if ( v <= LONG_MAX ) { // signed long int
99 type = 3;
100 } else if ( v <= ULONG_MAX && ( ! dec || LONG_MAX == LLONG_MAX ) ) { // signed long int
101 type = 3;
102 Unsigned = true; // unsigned long int
103 } else if ( v <= LLONG_MAX ) { // signed long long int
104 type = 4;
105 } else { // unsigned long long int
106 type = 4;
107 Unsigned = true; // unsigned long long int
108 } // if
109} // valueToType
110
111static void scanbin( string & str, unsigned long long int & v ) {
112 v = 0;
113 size_t last = str.length() - 1; // last subscript of constant
114 for ( unsigned int i = 2;; ) { // ignore prefix
115 if ( str[i] == '1' ) v |= 1;
116 i += 1;
117 if ( i == last - 1 || (str[i] != '0' && str[i] != '1') ) break;
118 v <<= 1;
119 } // for
120} // scanbin
121
122ast::Expr * build_constantInteger(
123 const CodeLocation & location, string & str ) {
124 static const ast::BasicType::Kind kind[2][6] = {
125 // short (h) must be before char (hh) because shorter type has the longer suffix
126 { ast::BasicType::ShortSignedInt, ast::BasicType::SignedChar, ast::BasicType::SignedInt, ast::BasicType::LongSignedInt, ast::BasicType::LongLongSignedInt, /* BasicType::SignedInt128 */ ast::BasicType::LongLongSignedInt, },
127 { ast::BasicType::ShortUnsignedInt, ast::BasicType::UnsignedChar, ast::BasicType::UnsignedInt, ast::BasicType::LongUnsignedInt, ast::BasicType::LongLongUnsignedInt, /* BasicType::UnsignedInt128 */ ast::BasicType::LongLongUnsignedInt, },
128 };
129
130 static const char * lnthsInt[2][6] = {
131 { "int16_t", "int8_t", "int32_t", "int64_t", "size_t", "uintptr_t", },
132 { "uint16_t", "uint8_t", "uint32_t", "uint64_t", "size_t", "uintptr_t", },
133 }; // lnthsInt
134
135 string str2( "0x0" );
136 unsigned long long int v, v2 = 0; // converted integral value
137 ast::Expr * ret, * ret2;
138
139 int type = -1; // 0 => short, 1 => char, 2 => int, 3 => long int, 4 => long long int, 5 => int128
140 int ltype = -1; // 0 => 16 bits, 1 => 8 bits, 2 => 32 bits, 3 => 64 bits, 4 => size_t, 5 => intptr, 6 => pointer
141 bool dec = true, Unsigned = false; // decimal, unsigned constant
142
143 // special constants
144 if ( str == "0" ) {
145 ret = new ast::ConstantExpr( location, new ast::ZeroType(), str, 0 );
146 goto CLEANUP;
147 } // if
148 if ( str == "1" ) {
149 ret = new ast::ConstantExpr( location, new ast::OneType(), str, 1 );
150 goto CLEANUP;
151 } // if
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 } else {
159 // At least one digit in integer constant, so safe to backup while looking for suffix.
160 // This declaration and the comma expressions in the conditions mimic
161 // the declare and check pattern allowed in later compiler versions.
162 // (Only some early compilers/C++ standards do not support it.)
163 string::size_type posn;
164 // pointer value
165 if ( posn = str.find_last_of( "pP" ), posn != string::npos ) {
166 ltype = 5; str.erase( posn, 1 );
167 // size_t
168 } else if ( posn = str.find_last_of( "zZ" ), posn != string::npos ) {
169 Unsigned = true; type = 2; ltype = 4; str.erase( posn, 1 );
170 // signed char
171 } else if ( posn = str.rfind( "hh" ), posn != string::npos ) {
172 type = 1; str.erase( posn, 2 );
173 // signed char
174 } else if ( posn = str.rfind( "HH" ), posn != string::npos ) {
175 type = 1; str.erase( posn, 2 );
176 // short
177 } else if ( posn = str.find_last_of( "hH" ), posn != string::npos ) {
178 type = 0; str.erase( posn, 1 );
179 // int (natural number)
180 } else if ( posn = str.find_last_of( "nN" ), posn != string::npos ) {
181 type = 2; str.erase( posn, 1 );
182 } else if ( str.rfind( "ll" ) != string::npos || str.rfind( "LL" ) != string::npos ) {
183 type = 4;
184 } else {
185 lnthSuffix( str, type, ltype );
186 } // if
187 } // if
188
189 // Cannot be just "0"/"1"; sscanf stops at the suffix, if any; value goes over the wall => always generate
190
191#if ! defined(__SIZEOF_INT128__)
192 if ( type == 5 ) SemanticError( yylloc, "int128 constant is not supported on this target " + str );
193#endif // ! __SIZEOF_INT128__
194
195 if ( str[0] == '0' ) { // radix character ?
196 dec = false;
197 if ( checkX( str[1] ) ) { // hex constant ?
198 if ( type < 5 ) { // not L128 ?
199 sscanf( (char *)str.c_str(), "%llx", &v );
200#if defined(__SIZEOF_INT128__)
201 } else { // hex int128 constant
202 unsigned int len = str.length();
203 if ( len > (2 + 16 + 16) ) SemanticError( yylloc, "128-bit hexadecimal constant to large " + str );
204 // hex digits < 2^64
205 if ( len > (2 + 16) ) {
206 str2 = "0x" + str.substr( len - 16 );
207 sscanf( (char *)str2.c_str(), "%llx", &v2 );
208 str = str.substr( 0, len - 16 );
209 } // if
210 sscanf( (char *)str.c_str(), "%llx", &v );
211#endif // __SIZEOF_INT128__
212 } // if
213 //printf( "%llx %llu\n", v, v );
214 } else if ( checkB( str[1] ) ) { // binary constant ?
215#if defined(__SIZEOF_INT128__)
216 unsigned int len = str.length();
217 if ( type == 5 && len > 2 + 64 ) {
218 if ( len > 2 + 64 + 64 ) SemanticError( yylloc, "128-bit binary constant to large " + str );
219 str2 = "0b" + str.substr( len - 64 );
220 str = str.substr( 0, len - 64 );
221 scanbin( str2, v2 );
222 } // if
223#endif // __SIZEOF_INT128__
224 scanbin( str, v );
225 //printf( "%#llx %llu\n", v, v );
226 } else { // octal constant
227 if ( type < 5 ) { // not L128 ?
228 sscanf( (char *)str.c_str(), "%llo", &v );
229#if defined(__SIZEOF_INT128__)
230 } else { // octal int128 constant
231 unsigned int len = str.length();
232 if ( len > 1 + 43 || (len == 1 + 43 && str[0] > '3') ) SemanticError( yylloc, "128-bit octal constant to large " + str );
233 char buf[32];
234 if ( len <= 1 + 21 ) { // value < 21 octal digitis
235 sscanf( (char *)str.c_str(), "%llo", &v );
236 } else {
237 sscanf( &str[len - 21], "%llo", &v );
238 __int128 val = v; // accumulate bits
239 str[len - 21] ='\0'; // shorten string
240 sscanf( &str[len == 43 ? 1 : 0], "%llo", &v );
241 val |= (__int128)v << 63; // store bits
242 if ( len == 1 + 43 ) { // most significant 2 bits ?
243 str[2] = '\0'; // shorten string
244 sscanf( &str[1], "%llo", &v ); // process most significant 2 bits
245 val |= (__int128)v << 126; // store bits
246 } // if
247 v = val >> 64; v2 = (uint64_t)val; // replace octal constant with 2 hex constants
248 sprintf( buf, "%#llx", v2 );
249 str2 = buf;
250 } // if
251 sprintf( buf, "%#llx", v );
252 str = buf;
253#endif // __SIZEOF_INT128__
254 } // if
255 //printf( "%#llo %llu\n", v, v );
256 } // if
257 } else { // decimal constant ?
258 if ( type < 5 ) { // not L128 ?
259 sscanf( (char *)str.c_str(), "%llu", &v );
260#if defined(__SIZEOF_INT128__)
261 } else { // decimal int128 constant
262 #define P10_UINT64 10'000'000'000'000'000'000ULL // 19 zeroes
263 unsigned int len = str.length();
264 if ( str.length() == 39 && str > (Unsigned ? "340282366920938463463374607431768211455" : "170141183460469231731687303715884105727") )
265 SemanticError( yylloc, "128-bit decimal constant to large " + str );
266 char buf[32];
267 if ( len <= 19 ) { // value < 19 decimal digitis
268 sscanf( (char *)str.c_str(), "%llu", &v );
269 } else {
270 sscanf( &str[len - 19], "%llu", &v );
271 __int128 val = v; // accumulate bits
272 str[len - 19] ='\0'; // shorten string
273 sscanf( &str[len == 39 ? 1 : 0], "%llu", &v );
274 val += (__int128)v * (__int128)P10_UINT64; // store bits
275 if ( len == 39 ) { // most significant 2 bits ?
276 str[1] = '\0'; // shorten string
277 sscanf( &str[0], "%llu", &v ); // process most significant 2 bits
278 val += (__int128)v * (__int128)P10_UINT64 * (__int128)P10_UINT64; // store bits
279 } // if
280 v = val >> 64; v2 = (uint64_t)val; // replace decimal constant with 2 hex constants
281 sprintf( buf, "%#llx", v2 );
282 str2 = buf;
283 } // if
284 sprintf( buf, "%#llx", v );
285 str = buf;
286#endif // __SIZEOF_INT128__
287 } // if
288 //printf( "%llu\n", v );
289 } // if
290
291 if ( type == -1 ) { // no suffix => determine type from value size
292 valueToType( v, dec, type, Unsigned );
293 } // if
294 /* printf( "%s %llo %s %llo\n", str.c_str(), v, str2.c_str(), v2 ); */
295
296 //if ( !( 0 <= type && type <= 6 ) ) { printf( "%s %lu %d %s\n", fred.c_str(), fred.length(), type, str.c_str() ); }
297 assert( 0 <= type && type <= 6 );
298
299 // Constant type is correct for overload resolving.
300 ret = new ast::ConstantExpr( location,
301 new ast::BasicType( 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 ast::CastExpr( location,
305 ret,
306 new ast::BasicType( kind[Unsigned][type] ),
307 ast::ExplicitCast );
308 } else if ( ltype != -1 ) { // explicit length ?
309 if ( ltype == 6 ) { // int128, (int128)constant
310 ret2 = new ast::ConstantExpr( location,
311 new ast::BasicType( ast::BasicType::LongLongSignedInt ),
312 str2,
313 v2 );
314 ret = build_compoundLiteral( location,
315 DeclarationNode::newBasicType(
316 DeclarationNode::Int128
317 )->addType(
318 DeclarationNode::newSignedNess( DeclarationNode::Unsigned ) ),
319 new InitializerNode(
320 (InitializerNode *)(new InitializerNode( new ExpressionNode( v2 == 0 ? ret2 : ret ) ))->set_last( new InitializerNode( new ExpressionNode( v2 == 0 ? ret : ret2 ) ) ), true )
321 );
322 } else { // explicit length, (length_type)constant
323 ret = new ast::CastExpr( location,
324 ret,
325 new ast::TypeInstType( lnthsInt[Unsigned][ltype], ast::TypeDecl::Dtype ),
326 ast::ExplicitCast );
327 if ( ltype == 5 ) { // pointer, intptr( (uintptr_t)constant )
328 ret = build_func( location,
329 new ExpressionNode(
330 build_varref( location, new string( "intptr" ) ) ),
331 new ExpressionNode( ret ) );
332 } // if
333 } // if
334 } // if
335
336 CLEANUP: ;
337 delete &str; // created by lex
338 return ret;
339} // build_constantInteger
340
341
342static inline void checkFnxFloat( string & str, size_t last, bool & explnth, int & type ) {
343 string::size_type posn;
344 // floating-point constant has minimum of 2 characters, 1. or .1, so safe to look ahead
345 if ( str[1] == 'x' ) { // hex ?
346 posn = str.find_last_of( "pP" ); // back for exponent (must have)
347 posn = str.find_first_of( "fF", posn + 1 ); // forward for size (fF allowed in hex constant)
348 } else {
349 posn = str.find_last_of( "fF" ); // back for size (fF not allowed)
350 } // if
351 if ( posn == string::npos ) return;
352 explnth = true;
353 posn += 1; // advance to size
354 if ( str[posn] == '3' ) { // 32
355 if ( str[last] != 'x' ) type = 6;
356 else type = 7;
357 } else if ( str[posn] == '6' ) { // 64
358 if ( str[last] != 'x' ) type = 8;
359 else type = 9;
360 } else if ( str[posn] == '8' ) { // 80
361 type = 3;
362 } else if ( str[posn] == '1' ) { // 16/128
363 if ( str[posn + 1] == '6' ) { // 16
364 type = 5;
365 } else { // 128
366 if ( str[last] != 'x' ) type = 10;
367 else type = 11;
368 } // if
369 } else {
370 assertf( false, "internal error, bad floating point length %s", str.c_str() );
371 } // if
372} // checkFnxFloat
373
374
375ast::Expr * build_constantFloat(
376 const CodeLocation & location, string & str ) {
377 static const ast::BasicType::Kind kind[2][12] = {
378 { ast::BasicType::Float, ast::BasicType::Double, ast::BasicType::LongDouble, ast::BasicType::uuFloat80, ast::BasicType::uuFloat128, ast::BasicType::uFloat16, ast::BasicType::uFloat32, ast::BasicType::uFloat32x, ast::BasicType::uFloat64, ast::BasicType::uFloat64x, ast::BasicType::uFloat128, ast::BasicType::uFloat128x },
379 { ast::BasicType::FloatComplex, ast::BasicType::DoubleComplex, ast::BasicType::LongDoubleComplex, ast::BasicType::NUMBER_OF_BASIC_TYPES, ast::BasicType::NUMBER_OF_BASIC_TYPES, ast::BasicType::uFloat16Complex, ast::BasicType::uFloat32Complex, ast::BasicType::uFloat32xComplex, ast::BasicType::uFloat64Complex, ast::BasicType::uFloat64xComplex, ast::BasicType::uFloat128Complex, ast::BasicType::uFloat128xComplex },
380 };
381
382 // floating-point constant has minimum of 2 characters 1. or .1
383 size_t last = str.length() - 1;
384 double v;
385 int type; // 0 => float, 1 => double, 3 => long double, ...
386 bool complx = false; // real, complex
387 bool explnth = false; // explicit literal length
388
389 sscanf( str.c_str(), "%lg", &v );
390
391 if ( checkI( str[last] ) ) { // imaginary ?
392 complx = true;
393 last -= 1; // backup one character
394 } // if
395
396 if ( checkF( str[last] ) ) { // float ?
397 type = 0;
398 } else if ( checkD( str[last] ) ) { // double ?
399 type = 1;
400 } else if ( checkL( str[last] ) ) { // long double ?
401 type = 2;
402 } else if ( checkF80( str[last] ) ) { // __float80 ?
403 type = 3;
404 } else if ( checkF128( str[last] ) ) { // __float128 ?
405 type = 4;
406 } else {
407 type = 1; // double (default if no suffix)
408 checkFnxFloat( str, last, explnth, type );
409 } // if
410
411 if ( ! complx && checkI( str[last - 1] ) ) { // imaginary ?
412 complx = true;
413 } // if
414
415 assert( 0 <= type && type < 12 );
416 ast::Expr * ret = new ast::ConstantExpr( location,
417 new ast::BasicType( kind[complx][type] ),
418 str,
419 v );
420 // explicit length ?
421 if ( explnth ) {
422 ret = new ast::CastExpr( location,
423 ret,
424 new ast::BasicType( kind[complx][type] ),
425 ast::ExplicitCast );
426 } // if
427
428 delete &str; // created by lex
429 return ret;
430} // build_constantFloat
431
432static void sepString( string & str, string & units, char delimit ) {
433 string::size_type posn = str.find_last_of( delimit ) + 1;
434 if ( posn != str.length() ) {
435 units = "?" + str.substr( posn ); // extract units
436 str.erase( posn ); // remove units
437 } // if
438} // sepString
439
440ast::Expr * build_constantChar( const CodeLocation & location, string & str ) {
441 string units; // units
442 sepString( str, units, '\'' ); // separate constant from units
443
444 ast::Expr * ret = new ast::ConstantExpr( location,
445 new ast::BasicType( ast::BasicType::Char ),
446 str,
447 (unsigned long long int)(unsigned char)str[1] );
448 if ( units.length() != 0 ) {
449 ret = new ast::UntypedExpr( location,
450 new ast::NameExpr( location, units ),
451 { ret } );
452 } // if
453
454 delete &str; // created by lex
455 return ret;
456} // build_constantChar
457
458ast::Expr * build_constantStr(
459 const CodeLocation & location,
460 string & str ) {
461 assert( str.length() > 0 );
462 string units; // units
463 sepString( str, units, '"' ); // separate constant from units
464
465 ast::Type * strtype;
466 switch ( str[0] ) { // str has >= 2 characters, i.e, null string "" => safe to look at subscripts 0/1
467 case 'u':
468 if ( str[1] == '8' ) goto Default; // utf-8 characters => array of char
469 // lookup type of associated typedef
470 strtype = new ast::TypeInstType( "char16_t", ast::TypeDecl::Dtype );
471 break;
472 case 'U':
473 strtype = new ast::TypeInstType( "char32_t", ast::TypeDecl::Dtype );
474 break;
475 case 'L':
476 strtype = new ast::TypeInstType( "wchar_t", ast::TypeDecl::Dtype );
477 break;
478 Default: // char default string type
479 default:
480 strtype = new ast::BasicType( ast::BasicType::Char );
481 } // switch
482 ast::ArrayType * at = new ast::ArrayType(
483 strtype,
484 // Length is adjusted: +1 for '\0' and -2 for '"'
485 ast::ConstantExpr::from_ulong( location, str.size() + 1 - 2 ),
486 ast::FixedLen,
487 ast::DynamicDim );
488 ast::Expr * ret = new ast::ConstantExpr( location, at, str, std::nullopt );
489 if ( units.length() != 0 ) {
490 ret = new ast::UntypedExpr( location,
491 new ast::NameExpr( location, units ),
492 { ret } );
493 } // if
494
495 delete &str; // created by lex
496 return ret;
497} // build_constantStr
498
499ast::Expr * build_field_name_FLOATING_FRACTIONconstant(
500 const CodeLocation & location, const string & str ) {
501 if ( str.find_first_not_of( "0123456789", 1 ) != string::npos ) SemanticError( yylloc, "invalid tuple index " + str );
502 ast::Expr * ret = build_constantInteger( location,
503 *new string( str.substr(1) ) );
504 delete &str;
505 return ret;
506} // build_field_name_FLOATING_FRACTIONconstant
507
508ast::Expr * build_field_name_FLOATING_DECIMALconstant(
509 const CodeLocation & location, const string & str ) {
510 if ( str[str.size() - 1] != '.' ) SemanticError( yylloc, "invalid tuple index " + str );
511 ast::Expr * ret = build_constantInteger(
512 location, *new string( str.substr( 0, str.size()-1 ) ) );
513 delete &str;
514 return ret;
515} // build_field_name_FLOATING_DECIMALconstant
516
517ast::Expr * build_field_name_FLOATINGconstant( const CodeLocation & location,
518 const string & str ) {
519 // str is of the form A.B -> separate at the . and return member expression
520 int a, b;
521 char dot;
522 stringstream ss( str );
523 ss >> a >> dot >> b;
524 auto ret = new ast::UntypedMemberExpr( location,
525 ast::ConstantExpr::from_int( location, b ),
526 ast::ConstantExpr::from_int( location, a )
527 );
528 delete &str;
529 return ret;
530} // build_field_name_FLOATINGconstant
531
532ast::Expr * make_field_name_fraction_constants( const CodeLocation & location,
533 ast::Expr * fieldName,
534 ast::Expr * fracts ) {
535 if ( nullptr == fracts ) {
536 return fieldName;
537 } else if ( auto memberExpr = dynamic_cast<ast::UntypedMemberExpr *>( fracts ) ) {
538 memberExpr->member = make_field_name_fraction_constants( location,
539 fieldName,
540 ast::mutate( memberExpr->aggregate.get() ) );
541 return memberExpr;
542 } else {
543 return new ast::UntypedMemberExpr( location, fracts, fieldName );
544 } // if
545} // make_field_name_fraction_constants
546
547ast::Expr * build_field_name_fraction_constants( const CodeLocation & location,
548 ast::Expr * fieldName,
549 ExpressionNode * fracts ) {
550 return make_field_name_fraction_constants( location, fieldName, maybeMoveBuild( fracts ) );
551} // build_field_name_fraction_constants
552
553ast::NameExpr * build_varref( const CodeLocation & location,
554 const string * name ) {
555 ast::NameExpr * expr = new ast::NameExpr( location, *name );
556 delete name;
557 return expr;
558} // build_varref
559
560ast::QualifiedNameExpr * build_qualified_expr( const CodeLocation & location,
561 const DeclarationNode * decl_node,
562 const ast::NameExpr * name ) {
563 ast::Decl * newDecl = maybeBuild( decl_node );
564 if ( ast::DeclWithType * newDeclWithType = dynamic_cast<ast::DeclWithType *>( newDecl ) ) {
565 if ( const ast::Type * t = newDeclWithType->get_type() ) {
566 if ( auto typeInst = dynamic_cast<const ast::TypeInstType *>( t ) ) {
567 newDecl = new ast::EnumDecl( location, typeInst->name );
568 }
569 }
570 }
571 return new ast::QualifiedNameExpr( location, newDecl, name->name );
572}
573
574ast::QualifiedNameExpr * build_qualified_expr( const CodeLocation & location,
575 const ast::EnumDecl * decl,
576 const ast::NameExpr * name ) {
577 return new ast::QualifiedNameExpr( location, decl, name->name );
578}
579
580ast::DimensionExpr * build_dimensionref( const CodeLocation & location,
581 const string * name ) {
582 ast::DimensionExpr * expr = new ast::DimensionExpr( location, *name );
583 delete name;
584 return expr;
585} // build_varref
586
587// TODO: get rid of this and OperKinds and reuse code from OperatorTable
588static const char * OperName[] = { // must harmonize with OperKinds
589 // diadic
590 "SizeOf", "AlignOf", "OffsetOf", "?+?", "?-?", "?\\?", "?*?", "?/?", "?%?", "||", "&&",
591 "?|?", "?&?", "?^?", "Cast", "?<<?", "?>>?", "?<?", "?>?", "?<=?", "?>=?", "?==?", "?!=?",
592 "?=?", "?@=?", "?\\=?", "?*=?", "?/=?", "?%=?", "?+=?", "?-=?", "?<<=?", "?>>=?", "?&=?", "?^=?", "?|=?",
593 "?[?]", "...",
594 // monadic
595 "+?", "-?", "AddressOf", "*?", "!?", "~?", "++?", "?++", "--?", "?--",
596}; // OperName
597
598ast::Expr * build_cast( const CodeLocation & location,
599 DeclarationNode * decl_node,
600 ExpressionNode * expr_node ) {
601 ast::Type * targetType = maybeMoveBuildType( decl_node );
602 if ( dynamic_cast<ast::VoidType *>( targetType ) ) {
603 delete targetType;
604 return new ast::CastExpr( location,
605 maybeMoveBuild( expr_node ),
606 ast::ExplicitCast );
607 } else {
608 return new ast::CastExpr( location,
609 maybeMoveBuild( expr_node ),
610 targetType,
611 ast::ExplicitCast );
612 } // if
613} // build_cast
614
615ast::Expr * build_keyword_cast( const CodeLocation & location,
616 ast::AggregateDecl::Aggregate target,
617 ExpressionNode * expr_node ) {
618 return new ast::KeywordCastExpr( location,
619 maybeMoveBuild( expr_node ),
620 target
621 );
622}
623
624ast::Expr * build_virtual_cast( const CodeLocation & location,
625 DeclarationNode * decl_node,
626 ExpressionNode * expr_node ) {
627 return new ast::VirtualCastExpr( location,
628 maybeMoveBuild( expr_node ),
629 maybeMoveBuildType( decl_node )
630 );
631} // build_virtual_cast
632
633ast::Expr * build_fieldSel( const CodeLocation & location,
634 ExpressionNode * expr_node,
635 ast::Expr * member ) {
636 return new ast::UntypedMemberExpr( location,
637 member,
638 maybeMoveBuild( expr_node )
639 );
640} // build_fieldSel
641
642ast::Expr * build_pfieldSel( const CodeLocation & location,
643 ExpressionNode * expr_node,
644 ast::Expr * member ) {
645 auto deref = new ast::UntypedExpr( location,
646 new ast::NameExpr( location, "*?" )
647 );
648 deref->location = expr_node->location;
649 deref->args.push_back( maybeMoveBuild( expr_node ) );
650 auto ret = new ast::UntypedMemberExpr( location, member, deref );
651 return ret;
652} // build_pfieldSel
653
654ast::Expr * build_offsetOf( const CodeLocation & location,
655 DeclarationNode * decl_node,
656 ast::NameExpr * member ) {
657 ast::Expr * ret = new ast::UntypedOffsetofExpr( location,
658 maybeMoveBuildType( decl_node ),
659 member->name
660 );
661 ret->result = new ast::BasicType( ast::BasicType::LongUnsignedInt );
662 delete member;
663 return ret;
664} // build_offsetOf
665
666ast::Expr * build_and_or( const CodeLocation & location,
667 ExpressionNode * expr_node1,
668 ExpressionNode * expr_node2,
669 ast::LogicalFlag flag ) {
670 return new ast::LogicalExpr( location,
671 notZeroExpr( maybeMoveBuild( expr_node1 ) ),
672 notZeroExpr( maybeMoveBuild( expr_node2 ) ),
673 flag
674 );
675} // build_and_or
676
677ast::Expr * build_unary_val( const CodeLocation & location,
678 OperKinds op,
679 ExpressionNode * expr_node ) {
680 std::vector<ast::ptr<ast::Expr>> args;
681 args.push_back( maybeMoveBuild( expr_node ) );
682 return new ast::UntypedExpr( location,
683 new ast::NameExpr( location, OperName[ (int)op ] ),
684 std::move( args )
685 );
686} // build_unary_val
687
688ast::Expr * build_binary_val( const CodeLocation & location,
689 OperKinds op,
690 ExpressionNode * expr_node1,
691 ExpressionNode * expr_node2 ) {
692 std::vector<ast::ptr<ast::Expr>> args;
693 args.push_back( maybeMoveBuild( expr_node1 ) );
694 args.push_back( maybeMoveBuild( expr_node2 ) );
695 return new ast::UntypedExpr( location,
696 new ast::NameExpr( location, OperName[ (int)op ] ),
697 std::move( args )
698 );
699} // build_binary_val
700
701ast::Expr * build_binary_ptr( const CodeLocation & location,
702 OperKinds op,
703 ExpressionNode * expr_node1,
704 ExpressionNode * expr_node2 ) {
705 return build_binary_val( location, op, expr_node1, expr_node2 );
706} // build_binary_ptr
707
708ast::Expr * build_cond( const CodeLocation & location,
709 ExpressionNode * expr_node1,
710 ExpressionNode * expr_node2,
711 ExpressionNode * expr_node3 ) {
712 return new ast::ConditionalExpr( location,
713 notZeroExpr( maybeMoveBuild( expr_node1 ) ),
714 maybeMoveBuild( expr_node2 ),
715 maybeMoveBuild( expr_node3 )
716 );
717} // build_cond
718
719ast::Expr * build_tuple( const CodeLocation & location,
720 ExpressionNode * expr_node ) {
721 std::vector<ast::ptr<ast::Expr>> exprs;
722 buildMoveList( expr_node, exprs );
723 return new ast::UntypedTupleExpr( location, std::move( exprs ) );
724} // build_tuple
725
726ast::Expr * build_func( const CodeLocation & location,
727 ExpressionNode * function,
728 ExpressionNode * expr_node ) {
729 std::vector<ast::ptr<ast::Expr>> args;
730 buildMoveList( expr_node, args );
731 return new ast::UntypedExpr( location,
732 maybeMoveBuild( function ),
733 std::move( args )
734 );
735} // build_func
736
737ast::Expr * build_compoundLiteral( const CodeLocation & location,
738 DeclarationNode * decl_node,
739 InitializerNode * kids ) {
740 // compound literal type
741 ast::Decl * newDecl = maybeBuild( decl_node );
742 // non-sue compound-literal type
743 if ( ast::DeclWithType * newDeclWithType = dynamic_cast<ast::DeclWithType *>( newDecl ) ) {
744 return new ast::CompoundLiteralExpr( location,
745 newDeclWithType->get_type(),
746 maybeMoveBuild( kids ) );
747 // these types do not have associated type information
748 } else if ( auto newDeclStructDecl = dynamic_cast<ast::StructDecl *>( newDecl ) ) {
749 if ( newDeclStructDecl->body ) {
750 return new ast::CompoundLiteralExpr( location,
751 new ast::StructInstType( newDeclStructDecl ),
752 maybeMoveBuild( kids ) );
753 } else {
754 return new ast::CompoundLiteralExpr( location,
755 new ast::StructInstType( newDeclStructDecl->name ),
756 maybeMoveBuild( kids ) );
757 } // if
758 } else if ( auto newDeclUnionDecl = dynamic_cast<ast::UnionDecl *>( newDecl ) ) {
759 if ( newDeclUnionDecl->body ) {
760 return new ast::CompoundLiteralExpr( location,
761 new ast::UnionInstType( newDeclUnionDecl ),
762 maybeMoveBuild( kids ) );
763 } else {
764 return new ast::CompoundLiteralExpr( location,
765 new ast::UnionInstType( newDeclUnionDecl->name ),
766 maybeMoveBuild( kids ) );
767 } // if
768 } else if ( auto newDeclEnumDecl = dynamic_cast<ast::EnumDecl *>( newDecl ) ) {
769 if ( newDeclEnumDecl->body ) {
770 return new ast::CompoundLiteralExpr( location,
771 new ast::EnumInstType( newDeclEnumDecl ),
772 maybeMoveBuild( kids ) );
773 } else {
774 return new ast::CompoundLiteralExpr( location,
775 new ast::EnumInstType( newDeclEnumDecl->name ),
776 maybeMoveBuild( kids ) );
777 } // if
778 } else {
779 assert( false );
780 } // if
781} // build_compoundLiteral
782
783// Local Variables: //
784// tab-width: 4 //
785// End: //
Note: See TracBrowser for help on using the repository browser.