source: src/Parser/ExpressionNode.cc@ a588f2a

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since a588f2a was ccb776b, checked in by Peter A. Buhr <pabuhr@…>, 5 years ago

fix #if to elide int128 code for 32-bit build

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