source: src/Parser/ExpressionNode.cc@ 3c714ad

Last change on this file since 3c714ad was 24d6572, checked in by Fangren Yu <f37yu@…>, 2 years ago

Merge branch 'master' into ast-experimental

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