source: src/Parser/ExpressionNode.cc@ a0a949c

ADT ast-experimental
Last change on this file since a0a949c 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
RevLine 
[b87a5ed]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//
[0caaa6a]7// ExpressionNode.cc --
8//
[f43df73]9// Author : Peter A. Buhr
[b87a5ed]10// Created On : Sat May 16 13:17:07 2015
[702e826]11// Last Modified By : Andrew Beach
12// Last Modified On : Thr Mar 0 10:46:00 2023
13// Update Count : 1080
[0caaa6a]14//
[b87a5ed]15
[ea6332d]16#include <cassert> // for assert
[d180746]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==
[51b73452]22
[d180746]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;
[51b73452]34
35using namespace std;
36
[cd623a4]37//##############################################################################
38
[7bf7fb9]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
[beec62c]51extern const Type::Qualifiers noQualifiers; // no qualifiers on constants
[7bf7fb9]52
[ba01b14]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'; }
[7bf7fb9]56static inline bool checkF( char c ) { return c == 'f' || c == 'F'; }
57static inline bool checkD( char c ) { return c == 'd' || c == 'D'; }
[ba01b14]58static inline bool checkF80( char c ) { return c == 'w' || c == 'W'; }
59static inline bool checkF128( char c ) { return c == 'q' || c == 'Q'; }
[f56c32e]60static inline bool checkL( char c ) { return c == 'l' || c == 'L'; }
[7bf7fb9]61static inline bool checkI( char c ) { return c == 'i' || c == 'I'; }
[0a2168f]62static inline bool checkB( char c ) { return c == 'b' || c == 'B'; }
[7bf7fb9]63static inline bool checkX( char c ) { return c == 'x' || c == 'X'; }
[3d8d7a7]64// static inline bool checkN( char c ) { return c == 'n' || c == 'N'; }
[f56c32e]65
66void lnthSuffix( string & str, int & type, int & ltype ) {
[c5b55c4]67 // 'u' can appear before or after length suffix
[f56c32e]68 string::size_type posn = str.find_last_of( "lL" );
[0a616e0]69
70 if ( posn == string::npos ) return; // no suffix
[c5b55c4]71 size_t end = str.length() - 1;
72 if ( posn == end ) { type = 3; return; } // no length after 'l' => long
[702e826]73
[0a616e0]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;
[f56c32e]86 } // if
87 } // if
[c5b55c4]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
[f56c32e]96} // lnthSuffix
[7bf7fb9]97
[0a616e0]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
[f6582252]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;
[702e826]124 if ( i == last - 1 || (str[i] != '0' && str[i] != '1') ) break;
[f6582252]125 v <<= 1;
126 } // for
127} // scanbin
128
[ba2a68b]129Expression * build_constantInteger( string & str ) {
[05c27fc]130 static const BasicType::Kind kind[2][6] = {
[ba01b14]131 // short (h) must be before char (hh) because shorter type has the longer suffix
[f6582252]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, },
[7bf7fb9]134 };
[76c62b2]135
[0a616e0]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", },
[ba01b14]139 }; // lnthsInt
140
[f6582252]141 string str2( "0x0" );
142 unsigned long long int v, v2 = 0; // converted integral value
143 Expression * ret, * ret2;
[0a616e0]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
[7bf7fb9]148
[6165ce7]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
[ea6332d]158
[f6582252]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 ?
[f56c32e]163 lnthSuffix( str, type, ltype ); // could have length suffix
[702e826]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;
[f56c32e]186 } else {
[702e826]187 lnthSuffix( str, type, ltype );
188 }
[7bf7fb9]189
[f6582252]190 // Cannot be just "0"/"1"; sscanf stops at the suffix, if any; value goes over the wall => always generate
[dbe8e31c]191
[cf5af9c]192#if ! defined(__SIZEOF_INT128__)
[dbe8e31c]193 if ( type == 5 ) SemanticError( yylloc, "int128 constant is not supported on this target " + str );
[cf5af9c]194#endif // ! __SIZEOF_INT128__
[702e826]195
[f6582252]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 );
[c5b55c4]201#if defined(__SIZEOF_INT128__)
[f6582252]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 );
[702e826]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 }
[f6582252]211 sscanf( (char *)str.c_str(), "%llx", &v );
[c5b55c4]212#endif // __SIZEOF_INT128__
[f6582252]213 } // if
214 //printf( "%llx %llu\n", v, v );
215 } else if ( checkB( str[1] ) ) { // binary constant ?
[c5b55c4]216#if defined(__SIZEOF_INT128__)
[013b028]217 unsigned int len = str.length();
[f6582252]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
[c5b55c4]224#endif // __SIZEOF_INT128__
[f6582252]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 );
[cf5af9c]230#if defined(__SIZEOF_INT128__)
[f6582252]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 );
[c5b55c4]234 char buf[32];
[f6582252]235 if ( len <= 1 + 21 ) { // value < 21 octal digitis
[c5b55c4]236 sscanf( (char *)str.c_str(), "%llo", &v );
[f6582252]237 } else {
238 sscanf( &str[len - 21], "%llo", &v );
[791028a]239 __int128 val = v; // accumulate bits
[f6582252]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
[c5b55c4]252 sprintf( buf, "%#llx", v );
253 str = buf;
[cf5af9c]254#endif // __SIZEOF_INT128__
[f6582252]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 );
[cf5af9c]261#if defined(__SIZEOF_INT128__)
[f6582252]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 );
[c5b55c4]267 char buf[32];
[f6582252]268 if ( len <= 19 ) { // value < 19 decimal digitis
[c5b55c4]269 sscanf( (char *)str.c_str(), "%llu", &v );
[f6582252]270 } else {
271 sscanf( &str[len - 19], "%llu", &v );
[791028a]272 __int128 val = v; // accumulate bits
[f6582252]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
[c5b55c4]285 sprintf( buf, "%#llx", v );
286 str = buf;
[cf5af9c]287#endif // __SIZEOF_INT128__
[f6582252]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
[0a616e0]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 );
[f56c32e]299
[a6c5d7c]300 // Constant type is correct for overload resolving.
[ba01b14]301 ret = new ConstantExpr( Constant( new BasicType( noQualifiers, kind[Unsigned][type] ), str, v ) );
302 if ( Unsigned && type < 2 ) { // hh or h, less than int ?
[201aeb9]303 // int i = -1uh => 65535 not -1, so cast is necessary for unsigned, which unfortunately eliminates warnings for large values.
[ba01b14]304 ret = new CastExpr( ret, new BasicType( Type::Qualifiers(), kind[Unsigned][type] ), false );
305 } else if ( ltype != -1 ) { // explicit length ?
[0a616e0]306 if ( ltype == 6 ) { // int128, (int128)constant
[f6582252]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 ) );
[702e826]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 ) );
[0a616e0]312 } else { // explicit length, (length_type)constant
[ba01b14]313 ret = new CastExpr( ret, new TypeInstType( Type::Qualifiers(), lnthsInt[Unsigned][ltype], false ), false );
[3d8d7a7]314 if ( ltype == 5 ) { // pointer, intptr( (uintptr_t)constant )
315 ret = build_func( new ExpressionNode( build_varref( new string( "intptr" ) ) ), new ExpressionNode( ret ) );
[0a616e0]316 } // if
[201aeb9]317 } // if
[7b1d5ec]318 } // if
[e8ccca3]319
[f56c32e]320 CLEANUP: ;
[ab57786]321 delete &str; // created by lex
322 return ret;
[7bf7fb9]323} // build_constantInteger
[51b73452]324
[201aeb9]325
[ba01b14]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
[201aeb9]335 if ( posn == string::npos ) return;
[ba01b14]336 explnth = true;
[201aeb9]337 posn += 1; // advance to size
338 if ( str[posn] == '3' ) { // 32
[ba01b14]339 if ( str[last] != 'x' ) type = 6;
340 else type = 7;
[201aeb9]341 } else if ( str[posn] == '6' ) { // 64
[ba01b14]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
[201aeb9]353 } else {
354 assertf( false, "internal error, bad floating point length %s", str.c_str() );
355 } // if
[ba01b14]356} // checkFnxFloat
[201aeb9]357
358
[f43df73]359Expression * build_constantFloat( string & str ) {
[ba01b14]360 static const BasicType::Kind kind[2][12] = {
[e15853c]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 },
[3c67255]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 },
[7bf7fb9]363 };
[59c24b6]364
[ba01b14]365 // floating-point constant has minimum of 2 characters 1. or .1
[7bf7fb9]366 size_t last = str.length() - 1;
[d56e5bc]367 double v;
[ba01b14]368 int type; // 0 => float, 1 => double, 3 => long double, ...
369 bool complx = false; // real, complex
370 bool explnth = false; // explicit literal length
[d56e5bc]371
372 sscanf( str.c_str(), "%lg", &v );
[51b73452]373
[7bf7fb9]374 if ( checkI( str[last] ) ) { // imaginary ?
375 complx = true;
376 last -= 1; // backup one character
377 } // if
[0caaa6a]378
[7bf7fb9]379 if ( checkF( str[last] ) ) { // float ?
[ba01b14]380 type = 0;
[7bf7fb9]381 } else if ( checkD( str[last] ) ) { // double ?
[ba01b14]382 type = 1;
[7bf7fb9]383 } else if ( checkL( str[last] ) ) { // long double ?
[ba01b14]384 type = 2;
385 } else if ( checkF80( str[last] ) ) { // __float80 ?
386 type = 3;
387 } else if ( checkF128( str[last] ) ) { // __float128 ?
388 type = 4;
[201aeb9]389 } else {
[ba01b14]390 type = 1; // double (default if no suffix)
391 checkFnxFloat( str, last, explnth, type );
[7bf7fb9]392 } // if
[ba01b14]393
[7bf7fb9]394 if ( ! complx && checkI( str[last - 1] ) ) { // imaginary ?
395 complx = true;
396 } // if
[51b73452]397
[ba01b14]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 );
[201aeb9]402 } // if
[76c62b2]403
[ab57786]404 delete &str; // created by lex
405 return ret;
[7bf7fb9]406} // build_constantFloat
[51b73452]407
[76c62b2]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() ) {
[e8ccca3]411 units = "?" + str.substr( posn ); // extract units
[76c62b2]412 str.erase( posn ); // remove units
413 } // if
414} // sepString
415
[f43df73]416Expression * build_constantChar( string & str ) {
[76c62b2]417 string units; // units
418 sepString( str, units, '\'' ); // separate constant from units
419
[e8ccca3]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 ) {
[ba2a68b]422 ret = new UntypedExpr( new NameExpr( units ), { ret } );
[e8ccca3]423 } // if
[76c62b2]424
[ab57786]425 delete &str; // created by lex
426 return ret;
[7bf7fb9]427} // build_constantChar
[51b73452]428
[e612146c]429Expression * build_constantStr( string & str ) {
[6e3eaa57]430 assert( str.length() > 0 );
[76c62b2]431 string units; // units
432 sepString( str, units, '"' ); // separate constant from units
433
[513e165]434 Type * strtype;
435 switch ( str[0] ) { // str has >= 2 characters, i.e, null string "" => safe to look at subscripts 0/1
[702e826]436 case 'u':
[513e165]437 if ( str[1] == '8' ) goto Default; // utf-8 characters => array of char
438 // lookup type of associated typedef
[b81fd95]439 strtype = new TypeInstType( Type::Qualifiers( ), "char16_t", false );
[e612146c]440 break;
[702e826]441 case 'U':
[b81fd95]442 strtype = new TypeInstType( Type::Qualifiers( ), "char32_t", false );
[e612146c]443 break;
[702e826]444 case 'L':
[b81fd95]445 strtype = new TypeInstType( Type::Qualifiers( ), "wchar_t", false );
[e612146c]446 break;
[702e826]447 Default: // char default string type
448 default:
[b81fd95]449 strtype = new BasicType( Type::Qualifiers( ), BasicType::Char );
[e612146c]450 } // switch
[c36298d]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 ) );
[e612146c]455 if ( units.length() != 0 ) {
456 ret = new UntypedExpr( new NameExpr( units ), { ret } );
457 } // if
[5809461]458
[ab57786]459 delete &str; // created by lex
460 return ret;
[7bf7fb9]461} // build_constantStr
[51b73452]462
[930f69e]463Expression * build_field_name_FLOATING_FRACTIONconstant( const string & str ) {
[a16764a6]464 if ( str.find_first_not_of( "0123456789", 1 ) != string::npos ) SemanticError( yylloc, "invalid tuple index " + str );
[930f69e]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 ) {
[3c67255]471 if ( str[str.size() - 1] != '.' ) SemanticError( yylloc, "invalid tuple index " + str );
[930f69e]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
[f43df73]477Expression * build_field_name_FLOATINGconstant( const string & str ) {
[8780e30]478 // str is of the form A.B -> separate at the . and return member expression
479 int a, b;
480 char dot;
[f43df73]481 stringstream ss( str );
[8780e30]482 ss >> a >> dot >> b;
[d56e5bc]483 UntypedMemberExpr * ret = new UntypedMemberExpr( new ConstantExpr( Constant::from_int( b ) ), new ConstantExpr( Constant::from_int( a ) ) );
[8780e30]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 );
[f43df73]495 } // if
496 } // if
[8780e30]497 return fieldName;
498} // make_field_name_fraction_constants
499
500Expression * build_field_name_fraction_constants( Expression * fieldName, ExpressionNode * fracts ) {
[702e826]501 return make_field_name_fraction_constants( fieldName, maybeMoveBuild( fracts ) );
[8780e30]502} // build_field_name_fraction_constants
503
[a2e0687]504NameExpr * build_varref( const string * name ) {
[bf4b4cf]505 NameExpr * expr = new NameExpr( *name );
[7ecbb7e]506 delete name;
507 return expr;
[a2e0687]508} // build_varref
[51b1202]509
[4e2befe3]510QualifiedNameExpr * build_qualified_expr( const DeclarationNode * decl_node, const NameExpr * name ) {
[702e826]511 Declaration * newDecl = maybeBuild(decl_node);
[b0d9ff7]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 }
[e874605]520 return new QualifiedNameExpr( newDecl, name->name );
[b0d9ff7]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 );
[4e2befe3]526}
527
[6e50a6b]528DimensionExpr * build_dimensionref( const string * name ) {
529 DimensionExpr * expr = new DimensionExpr( *name );
530 delete name;
531 return expr;
532} // build_varref
[ea54f1e]533
[5809461]534// TODO: get rid of this and OperKinds and reuse code from OperatorTable
[a2e0687]535static const char * OperName[] = { // must harmonize with OperKinds
[5721a6d]536 // diadic
[e5f2a67]537 "SizeOf", "AlignOf", "OffsetOf", "?+?", "?-?", "?\\?", "?*?", "?/?", "?%?", "||", "&&",
[5721a6d]538 "?|?", "?&?", "?^?", "Cast", "?<<?", "?>>?", "?<?", "?>?", "?<=?", "?>=?", "?==?", "?!=?",
[e5f2a67]539 "?=?", "?@=?", "?\\=?", "?*=?", "?/=?", "?%=?", "?+=?", "?-=?", "?<<=?", "?>>=?", "?&=?", "?^=?", "?|=?",
[d9e2280]540 "?[?]", "...",
[5721a6d]541 // monadic
[5809461]542 "+?", "-?", "AddressOf", "*?", "!?", "~?", "++?", "?++", "--?", "?--",
[a2e0687]543}; // OperName
[5721a6d]544
[a2e0687]545Expression * build_cast( DeclarationNode * decl_node, ExpressionNode * expr_node ) {
546 Type * targetType = maybeMoveBuildType( decl_node );
[d1625f8]547 if ( dynamic_cast< VoidType * >( targetType ) ) {
[064e3ff]548 delete targetType;
[702e826]549 return new CastExpr( maybeMoveBuild( expr_node ), false );
[064e3ff]550 } else {
[702e826]551 return new CastExpr( maybeMoveBuild( expr_node ), targetType, false );
[064e3ff]552 } // if
[a2e0687]553} // build_cast
[a5f0529]554
[312029a]555Expression * build_keyword_cast( AggregateDecl::Aggregate target, ExpressionNode * expr_node ) {
[702e826]556 return new KeywordCastExpr( maybeMoveBuild( expr_node ), target );
[9a705dc8]557}
558
[a2e0687]559Expression * build_virtual_cast( DeclarationNode * decl_node, ExpressionNode * expr_node ) {
[702e826]560 return new VirtualCastExpr( maybeMoveBuild( expr_node ), maybeMoveBuildType( decl_node ) );
[a2e0687]561} // build_virtual_cast
[064e3ff]562
[a2e0687]563Expression * build_fieldSel( ExpressionNode * expr_node, Expression * member ) {
[702e826]564 return new UntypedMemberExpr( member, maybeMoveBuild( expr_node ) );
[a2e0687]565} // build_fieldSel
[064e3ff]566
[a2e0687]567Expression * build_pfieldSel( ExpressionNode * expr_node, Expression * member ) {
568 UntypedExpr * deref = new UntypedExpr( new NameExpr( "*?" ) );
[64ac636]569 deref->location = expr_node->location;
[702e826]570 deref->get_args().push_back( maybeMoveBuild( expr_node ) );
[a2e0687]571 UntypedMemberExpr * ret = new UntypedMemberExpr( member, deref );
[064e3ff]572 return ret;
[a2e0687]573} // build_pfieldSel
[064e3ff]574
[a2e0687]575Expression * build_offsetOf( DeclarationNode * decl_node, NameExpr * member ) {
[a7c90d4]576 Expression * ret = new UntypedOffsetofExpr( maybeMoveBuildType( decl_node ), member->get_name() );
[ac71a86]577 delete member;
578 return ret;
[a2e0687]579} // build_offsetOf
[064e3ff]580
[a2e0687]581Expression * build_and_or( ExpressionNode * expr_node1, ExpressionNode * expr_node2, bool kind ) {
[702e826]582 return new LogicalExpr( notZeroExpr( maybeMoveBuild( expr_node1 ) ), notZeroExpr( maybeMoveBuild( expr_node2 ) ), kind );
[a2e0687]583} // build_and_or
[51e076e]584
[a2e0687]585Expression * build_unary_val( OperKinds op, ExpressionNode * expr_node ) {
[f43df73]586 list< Expression * > args;
[702e826]587 args.push_back( maybeMoveBuild( expr_node ) );
[d9e2280]588 return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
[a2e0687]589} // build_unary_val
590
591Expression * build_binary_val( OperKinds op, ExpressionNode * expr_node1, ExpressionNode * expr_node2 ) {
[f43df73]592 list< Expression * > args;
[702e826]593 args.push_back( maybeMoveBuild( expr_node1 ) );
594 args.push_back( maybeMoveBuild( expr_node2 ) );
[d9e2280]595 return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
[a2e0687]596} // build_binary_val
597
598Expression * build_binary_ptr( OperKinds op, ExpressionNode * expr_node1, ExpressionNode * expr_node2 ) {
[f43df73]599 list< Expression * > args;
[702e826]600 args.push_back( maybeMoveBuild( expr_node1 ) );
601 args.push_back( maybeMoveBuild( expr_node2 ) );
[d9e2280]602 return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
[a2e0687]603} // build_binary_ptr
[51e076e]604
[a2e0687]605Expression * build_cond( ExpressionNode * expr_node1, ExpressionNode * expr_node2, ExpressionNode * expr_node3 ) {
[702e826]606 return new ConditionalExpr( notZeroExpr( maybeMoveBuild( expr_node1 ) ), maybeMoveBuild( expr_node2 ), maybeMoveBuild( expr_node3 ) );
[a2e0687]607} // build_cond
[51e076e]608
[a2e0687]609Expression * build_tuple( ExpressionNode * expr_node ) {
[f43df73]610 list< Expression * > exprs;
[907eccb]611 buildMoveList( expr_node, exprs );
612 return new UntypedTupleExpr( exprs );;
[a2e0687]613} // build_tuple
[9706554]614
[a2e0687]615Expression * build_func( ExpressionNode * function, ExpressionNode * expr_node ) {
[f43df73]616 list< Expression * > args;
[7ecbb7e]617 buildMoveList( expr_node, args );
[702e826]618 return new UntypedExpr( maybeMoveBuild( function ), args );
[a2e0687]619} // build_func
[9706554]620
[a2e0687]621Expression * build_compoundLiteral( DeclarationNode * decl_node, InitializerNode * kids ) {
[702e826]622 Declaration * newDecl = maybeBuild( decl_node ); // compound literal type
[630a82a]623 if ( DeclarationWithType * newDeclWithType = dynamic_cast< DeclarationWithType * >( newDecl ) ) { // non-sue compound-literal type
[702e826]624 return new CompoundLiteralExpr( newDeclWithType->get_type(), maybeMoveBuild( kids ) );
[630a82a]625 // these types do not have associated type information
626 } else if ( StructDecl * newDeclStructDecl = dynamic_cast< StructDecl * >( newDecl ) ) {
[fbcde64]627 if ( newDeclStructDecl->has_body() ) {
[702e826]628 return new CompoundLiteralExpr( new StructInstType( Type::Qualifiers(), newDeclStructDecl ), maybeMoveBuild( kids ) );
[fbcde64]629 } else {
[702e826]630 return new CompoundLiteralExpr( new StructInstType( Type::Qualifiers(), newDeclStructDecl->get_name() ), maybeMoveBuild( kids ) );
[fbcde64]631 } // if
[630a82a]632 } else if ( UnionDecl * newDeclUnionDecl = dynamic_cast< UnionDecl * >( newDecl ) ) {
[fbcde64]633 if ( newDeclUnionDecl->has_body() ) {
[702e826]634 return new CompoundLiteralExpr( new UnionInstType( Type::Qualifiers(), newDeclUnionDecl ), maybeMoveBuild( kids ) );
[fbcde64]635 } else {
[702e826]636 return new CompoundLiteralExpr( new UnionInstType( Type::Qualifiers(), newDeclUnionDecl->get_name() ), maybeMoveBuild( kids ) );
[fbcde64]637 } // if
[630a82a]638 } else if ( EnumDecl * newDeclEnumDecl = dynamic_cast< EnumDecl * >( newDecl ) ) {
[fbcde64]639 if ( newDeclEnumDecl->has_body() ) {
[702e826]640 return new CompoundLiteralExpr( new EnumInstType( Type::Qualifiers(), newDeclEnumDecl ), maybeMoveBuild( kids ) );
[fbcde64]641 } else {
[702e826]642 return new CompoundLiteralExpr( new EnumInstType( Type::Qualifiers(), newDeclEnumDecl->get_name() ), maybeMoveBuild( kids ) );
[fbcde64]643 } // if
[630a82a]644 } else {
645 assert( false );
646 } // if
[a2e0687]647} // build_compoundLiteral
[630a82a]648
[b87a5ed]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.