Changes in src/Parser/ExpressionNode.cc [ca9d65e:46da46b]
- File:
-
- 1 edited
-
src/Parser/ExpressionNode.cc (modified) (22 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/Parser/ExpressionNode.cc
rca9d65e r46da46b 10 10 // Created On : Sat May 16 13:17:07 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Thu Dec 14 18:57:07 2023 13 // Update Count : 1087 14 // 15 16 #include "ExpressionNode.h" 12 // Last Modified On : Sat Aug 7 09:18:56 2021 13 // Update Count : 1077 14 // 17 15 18 16 #include <cassert> // for assert … … 23 21 #include <string> // for string, operator+, operator== 24 22 25 #include "AST/Expr.hpp" // for NameExpr26 #include "AST/Type.hpp" // for BaseType, SueInstType27 23 #include "Common/SemanticError.h" // for SemanticError 28 24 #include "Common/utility.h" // for maybeMoveBuild, maybeBuild, CodeLo... 29 #include "DeclarationNode.h" // for DeclarationNode 30 #include "InitializerNode.h" // for InitializerNode 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 31 #include "parserutility.h" // for notZeroExpr 32 33 class Initializer; 32 34 33 35 using namespace std; … … 46 48 // because the CONT rule is NOT triggered if the pattern is empty. Hence, constants are reparsed here to determine their 47 49 // type. 50 51 extern const Type::Qualifiers noQualifiers; // no qualifiers on constants 48 52 49 53 // static inline bool checkH( char c ) { return c == 'h' || c == 'H'; } … … 67 71 size_t end = str.length() - 1; 68 72 if ( posn == end ) { type = 3; return; } // no length after 'l' => long 69 73 70 74 string::size_type next = posn + 1; // advance to length 71 75 if ( str[next] == '3' ) { // 32 … … 118 122 if ( str[i] == '1' ) v |= 1; 119 123 i += 1; 120 if ( i == last - 1 || (str[i] != '0' && str[i] != '1') ) break;124 if ( i == last - 1 || (str[i] != '0' && str[i] != '1') ) break; 121 125 v <<= 1; 122 126 } // for 123 127 } // scanbin 124 128 125 ast::Expr * build_constantInteger( 126 const CodeLocation & location, string & str ) { 127 static const ast::BasicType::Kind kind[2][6] = { 129 Expression * build_constantInteger( string & str ) { 130 static const BasicType::Kind kind[2][6] = { 128 131 // short (h) must be before char (hh) because shorter type has the longer suffix 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, },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, }, 131 134 }; 132 135 … … 138 141 string str2( "0x0" ); 139 142 unsigned long long int v, v2 = 0; // converted integral value 140 ast::Expr* ret, * ret2;143 Expression * ret, * ret2; 141 144 142 145 int type = -1; // 0 => short, 1 => char, 2 => int, 3 => long int, 4 => long long int, 5 => int128 … … 146 149 // special constants 147 150 if ( str == "0" ) { 148 ret = new ast::ConstantExpr( location, new ast::ZeroType(), str, 0);151 ret = new ConstantExpr( Constant( (Type *)new ZeroType( noQualifiers ), str, (unsigned long long int)0 ) ); 149 152 goto CLEANUP; 150 153 } // if 151 154 if ( str == "1" ) { 152 ret = new ast::ConstantExpr( location, new ast::OneType(), str, 1);155 ret = new ConstantExpr( Constant( (Type *)new OneType( noQualifiers ), str, (unsigned long long int)1 ) ); 153 156 goto CLEANUP; 154 157 } // if 158 159 string::size_type posn; 155 160 156 161 // 'u' can appear before or after length suffix … … 161 166 } else { 162 167 // At least one digit in integer constant, so safe to backup while looking for suffix. 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.) 166 string::size_type posn; 167 // pointer value 168 if ( posn = str.find_last_of( "pP" ), posn != string::npos ) { 169 ltype = 5; str.erase( posn, 1 ); 170 // size_t 171 } else if ( posn = str.find_last_of( "zZ" ), posn != string::npos ) { 172 Unsigned = true; type = 2; ltype = 4; str.erase( posn, 1 ); 173 // signed char 174 } else if ( posn = str.rfind( "hh" ), posn != string::npos ) { 175 type = 1; str.erase( posn, 2 ); 176 // signed char 177 } else if ( posn = str.rfind( "HH" ), posn != string::npos ) { 178 type = 1; str.erase( posn, 2 ); 179 // short 180 } else if ( posn = str.find_last_of( "hH" ), posn != string::npos ) { 181 type = 0; str.erase( posn, 1 ); 182 // int (natural number) 183 } else if ( posn = str.find_last_of( "nN" ), posn != string::npos ) { 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 168 169 posn = str.find_last_of( "pP" ); // pointer value 170 if ( posn != string::npos ) { ltype = 5; str.erase( posn, 1 ); goto FINI; } 171 172 posn = str.find_last_of( "zZ" ); // size_t 173 if ( posn != string::npos ) { Unsigned = true; type = 2; ltype = 4; str.erase( posn, 1 ); goto FINI; } 174 175 posn = str.rfind( "hh" ); // char 176 if ( posn != string::npos ) { type = 1; str.erase( posn, 2 ); goto FINI; } 177 178 posn = str.rfind( "HH" ); // char 179 if ( posn != string::npos ) { type = 1; str.erase( posn, 2 ); goto FINI; } 180 181 posn = str.find_last_of( "hH" ); // short 182 if ( posn != string::npos ) { type = 0; str.erase( posn, 1 ); goto FINI; } 183 184 posn = str.find_last_of( "nN" ); // int (natural number) 185 if ( posn != string::npos ) { type = 2; str.erase( posn, 1 ); goto FINI; } 186 187 if ( str.rfind( "ll" ) != string::npos || str.rfind( "LL" ) != string::npos ) { type = 4; goto FINI; } 188 189 lnthSuffix( str, type, ltype ); // must be after check for "ll" 190 FINI: ; 190 191 } // if 191 192 … … 193 194 194 195 #if ! defined(__SIZEOF_INT128__) 195 if ( type == 5 ) SemanticError( yylloc, "int128 constant is not supported on this target \"%s\"", str.c_str());196 if ( type == 5 ) SemanticError( yylloc, "int128 constant is not supported on this target " + str ); 196 197 #endif // ! __SIZEOF_INT128__ 197 198 198 199 if ( str[0] == '0' ) { // radix character ? 199 200 dec = false; … … 204 205 } else { // hex int128 constant 205 206 unsigned int len = str.length(); 206 if ( len > (2 + 16 + 16) ) SemanticError( yylloc, "128-bit hexadecimal constant to large \"%s\"", str.c_str() ); 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 ); 212 } // if 207 if ( len > (2 + 16 + 16) ) SemanticError( yylloc, "128-bit hexadecimal constant to large " + str ); 208 if ( len <= (2 + 16) ) goto FHEX1; // hex digits < 2^64 209 str2 = "0x" + str.substr( len - 16 ); 210 sscanf( (char *)str2.c_str(), "%llx", &v2 ); 211 str = str.substr( 0, len - 16 ); 212 FHEX1: ; 213 213 sscanf( (char *)str.c_str(), "%llx", &v ); 214 214 #endif // __SIZEOF_INT128__ … … 219 219 unsigned int len = str.length(); 220 220 if ( type == 5 && len > 2 + 64 ) { 221 if ( len > 2 + 64 + 64 ) SemanticError( yylloc, "128-bit binary constant to large \"%s\".", str.c_str());221 if ( len > 2 + 64 + 64 ) SemanticError( yylloc, "128-bit binary constant to large " + str ); 222 222 str2 = "0b" + str.substr( len - 64 ); 223 223 str = str.substr( 0, len - 64 ); … … 233 233 } else { // octal int128 constant 234 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 \"%s\"", str.c_str());235 if ( len > 1 + 43 || (len == 1 + 43 && str[0] > '3') ) SemanticError( yylloc, "128-bit octal constant to large " + str ); 236 236 char buf[32]; 237 237 if ( len <= 1 + 21 ) { // value < 21 octal digitis … … 266 266 unsigned int len = str.length(); 267 267 if ( str.length() == 39 && str > (Unsigned ? "340282366920938463463374607431768211455" : "170141183460469231731687303715884105727") ) 268 SemanticError( yylloc, "128-bit decimal constant to large \"%s\".", str.c_str());268 SemanticError( yylloc, "128-bit decimal constant to large " + str ); 269 269 char buf[32]; 270 270 if ( len <= 19 ) { // value < 19 decimal digitis … … 301 301 302 302 // Constant type is correct for overload resolving. 303 ret = new ast::ConstantExpr( location, 304 new ast::BasicType( kind[Unsigned][type] ), str, v ); 303 ret = new ConstantExpr( Constant( new BasicType( noQualifiers, kind[Unsigned][type] ), str, v ) ); 305 304 if ( Unsigned && type < 2 ) { // hh or h, less than int ? 306 305 // int i = -1uh => 65535 not -1, so cast is necessary for unsigned, which unfortunately eliminates warnings for large values. 307 ret = new ast::CastExpr( location, 308 ret, 309 new ast::BasicType( kind[Unsigned][type] ), 310 ast::ExplicitCast ); 306 ret = new CastExpr( ret, new BasicType( Type::Qualifiers(), kind[Unsigned][type] ), false ); 311 307 } else if ( ltype != -1 ) { // explicit length ? 312 308 if ( ltype == 6 ) { // int128, (int128)constant 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 ); 309 // ret = new CastExpr( ret, new BasicType( Type::Qualifiers(), kind[Unsigned][type] ), false ); 310 ret2 = new ConstantExpr( Constant( new BasicType( noQualifiers, BasicType::LongLongSignedInt ), str2, v2 ) ); 311 ret = build_compoundLiteral( DeclarationNode::newBasicType( DeclarationNode::Int128 )->addType( DeclarationNode::newSignedNess( DeclarationNode::Unsigned ) ), 312 new InitializerNode( (InitializerNode *)(new InitializerNode( new ExpressionNode( v2 == 0 ? ret2 : ret ) ))->set_last( new InitializerNode( new ExpressionNode( v2 == 0 ? ret : ret2 ) ) ), true ) ); 325 313 } else { // explicit length, (length_type)constant 326 ret = new ast::CastExpr( location, 327 ret, 328 new ast::TypeInstType( lnthsInt[Unsigned][ltype], ast::TypeDecl::Dtype ), 329 ast::ExplicitCast ); 314 ret = new CastExpr( ret, new TypeInstType( Type::Qualifiers(), lnthsInt[Unsigned][ltype], false ), false ); 330 315 if ( ltype == 5 ) { // pointer, intptr( (uintptr_t)constant ) 331 ret = build_func( location, 332 new ExpressionNode( 333 build_varref( location, new string( "intptr" ) ) ), 334 new ExpressionNode( ret ) ); 316 ret = build_func( new ExpressionNode( build_varref( new string( "intptr" ) ) ), new ExpressionNode( ret ) ); 335 317 } // if 336 318 } // if … … 376 358 377 359 378 ast::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 }, 360 Expression * build_constantFloat( string & str ) { 361 static const BasicType::Kind kind[2][12] = { 362 { BasicType::Float, BasicType::Double, BasicType::LongDouble, BasicType::uuFloat80, BasicType::uuFloat128, BasicType::uFloat16, BasicType::uFloat32, BasicType::uFloat32x, BasicType::uFloat64, BasicType::uFloat64x, BasicType::uFloat128, BasicType::uFloat128x }, 363 { 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 }, 383 364 }; 384 365 … … 417 398 418 399 assert( 0 <= type && type < 12 ); 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 ); 400 Expression * ret = new ConstantExpr( Constant( new BasicType( noQualifiers, kind[complx][type] ), str, v ) ); 401 if ( explnth ) { // explicit length ? 402 ret = new CastExpr( ret, new BasicType( Type::Qualifiers(), kind[complx][type] ), false ); 429 403 } // if 430 404 … … 441 415 } // sepString 442 416 443 ast::Expr * build_constantChar( const CodeLocation & location,string & str ) {417 Expression * build_constantChar( string & str ) { 444 418 string units; // units 445 419 sepString( str, units, '\'' ); // separate constant from units 446 420 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] ); 421 Expression * ret = new ConstantExpr( Constant( new BasicType( noQualifiers, BasicType::Char ), str, (unsigned long long int)(unsigned char)str[1] ) ); 451 422 if ( units.length() != 0 ) { 452 ret = new ast::UntypedExpr( location, 453 new ast::NameExpr( location, units ), 454 { ret } ); 423 ret = new UntypedExpr( new NameExpr( units ), { ret } ); 455 424 } // if 456 425 … … 459 428 } // build_constantChar 460 429 461 ast::Expr * build_constantStr( 462 const CodeLocation & location, 463 string & str ) { 430 Expression * build_constantStr( string & str ) { 464 431 assert( str.length() > 0 ); 465 432 string units; // units 466 433 sepString( str, units, '"' ); // separate constant from units 467 434 468 ast::Type * strtype;435 Type * strtype; 469 436 switch ( str[0] ) { // str has >= 2 characters, i.e, null string "" => safe to look at subscripts 0/1 470 case 'u':437 case 'u': 471 438 if ( str[1] == '8' ) goto Default; // utf-8 characters => array of char 472 439 // lookup type of associated typedef 473 strtype = new ast::TypeInstType( "char16_t", ast::TypeDecl::Dtype );440 strtype = new TypeInstType( Type::Qualifiers( ), "char16_t", false ); 474 441 break; 475 case 'U':476 strtype = new ast::TypeInstType( "char32_t", ast::TypeDecl::Dtype );442 case 'U': 443 strtype = new TypeInstType( Type::Qualifiers( ), "char32_t", false ); 477 444 break; 478 case 'L':479 strtype = new ast::TypeInstType( "wchar_t", ast::TypeDecl::Dtype );445 case 'L': 446 strtype = new TypeInstType( Type::Qualifiers( ), "wchar_t", false ); 480 447 break; 481 Default: // char default string type482 default:483 strtype = new ast::BasicType( ast::BasicType::Char );448 Default: // char default string type 449 default: 450 strtype = new BasicType( Type::Qualifiers( ), BasicType::Char ); 484 451 } // switch 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 ); 452 ArrayType * at = new ArrayType( noQualifiers, strtype, 453 new ConstantExpr( Constant::from_ulong( str.size() + 1 - 2 ) ), // +1 for '\0' and -2 for '"' 454 false, false ); 455 Expression * ret = new ConstantExpr( Constant( at, str, std::nullopt ) ); 492 456 if ( units.length() != 0 ) { 493 ret = new ast::UntypedExpr( location, 494 new ast::NameExpr( location, units ), 495 { ret } ); 457 ret = new UntypedExpr( new NameExpr( units ), { ret } ); 496 458 } // if 497 459 … … 500 462 } // build_constantStr 501 463 502 ast::Expr * build_field_name_FLOATING_FRACTIONconstant( 503 const CodeLocation & location, const string & str ) { 504 if ( str.find_first_not_of( "0123456789", 1 ) != string::npos ) SemanticError( yylloc, "invalid tuple index \"%s\".", str.c_str() ); 505 ast::Expr * ret = build_constantInteger( location, 506 *new string( str.substr(1) ) ); 464 Expression * build_field_name_FLOATING_FRACTIONconstant( const string & str ) { 465 if ( str.find_first_not_of( "0123456789", 1 ) != string::npos ) SemanticError( yylloc, "invalid tuple index " + str ); 466 Expression * ret = build_constantInteger( *new string( str.substr(1) ) ); 507 467 delete &str; 508 468 return ret; 509 469 } // build_field_name_FLOATING_FRACTIONconstant 510 470 511 ast::Expr * build_field_name_FLOATING_DECIMALconstant( 512 const CodeLocation & location, const string & str ) { 513 if ( str[str.size() - 1] != '.' ) SemanticError( yylloc, "invalid tuple index \"%s\".", str.c_str() ); 514 ast::Expr * ret = build_constantInteger( 515 location, *new string( str.substr( 0, str.size()-1 ) ) ); 471 Expression * build_field_name_FLOATING_DECIMALconstant( const string & str ) { 472 if ( str[str.size() - 1] != '.' ) SemanticError( yylloc, "invalid tuple index " + str ); 473 Expression * ret = build_constantInteger( *new string( str.substr( 0, str.size()-1 ) ) ); 516 474 delete &str; 517 475 return ret; 518 476 } // build_field_name_FLOATING_DECIMALconstant 519 477 520 ast::Expr * build_field_name_FLOATINGconstant( const CodeLocation & location, 521 const string & str ) { 478 Expression * build_field_name_FLOATINGconstant( const string & str ) { 522 479 // str is of the form A.B -> separate at the . and return member expression 523 480 int a, b; … … 525 482 stringstream ss( str ); 526 483 ss >> a >> dot >> b; 527 auto ret = new ast::UntypedMemberExpr( location, 528 ast::ConstantExpr::from_int( location, b ), 529 ast::ConstantExpr::from_int( location, a ) 530 ); 484 UntypedMemberExpr * ret = new UntypedMemberExpr( new ConstantExpr( Constant::from_int( b ) ), new ConstantExpr( Constant::from_int( a ) ) ); 531 485 delete &str; 532 486 return ret; 533 487 } // build_field_name_FLOATINGconstant 534 488 535 ast::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 ); 547 } // if 489 Expression * make_field_name_fraction_constants( Expression * fieldName, Expression * fracts ) { 490 if ( fracts ) { 491 if ( UntypedMemberExpr * memberExpr = dynamic_cast< UntypedMemberExpr * >( fracts ) ) { 492 memberExpr->set_member( make_field_name_fraction_constants( fieldName, memberExpr->get_aggregate() ) ); 493 return memberExpr; 494 } else { 495 return new UntypedMemberExpr( fracts, fieldName ); 496 } // if 497 } // if 498 return fieldName; 548 499 } // make_field_name_fraction_constants 549 500 550 ast::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 ) ); 501 Expression * build_field_name_fraction_constants( Expression * fieldName, ExpressionNode * fracts ) { 502 return make_field_name_fraction_constants( fieldName, maybeMoveBuild< Expression >( fracts ) ); 554 503 } // build_field_name_fraction_constants 555 504 556 ast::NameExpr * build_varref( const CodeLocation & location, 557 const string * name ) { 558 ast::NameExpr * expr = new ast::NameExpr( location, *name ); 505 NameExpr * build_varref( const string * name ) { 506 NameExpr * expr = new NameExpr( *name ); 559 507 delete name; 560 508 return expr; 561 509 } // build_varref 562 510 563 ast::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 ); 511 QualifiedNameExpr * build_qualified_expr( const DeclarationNode * decl_node, const NameExpr * name ) { 512 Declaration * newDecl = maybeBuild< Declaration >(decl_node); 513 if ( DeclarationWithType * newDeclWithType = dynamic_cast< DeclarationWithType * >( newDecl ) ) { 514 const Type * t = newDeclWithType->get_type(); 515 if ( t ) { 516 if ( const TypeInstType * typeInst = dynamic_cast<const TypeInstType *>( t ) ) { 517 newDecl= new EnumDecl( typeInst->name ); 571 518 } 572 519 } 573 520 } 574 return new ast::QualifiedNameExpr( location,newDecl, name->name );521 return new QualifiedNameExpr( newDecl, name->name ); 575 522 } 576 523 577 ast::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 ); 524 QualifiedNameExpr * build_qualified_expr( const EnumDecl * decl_node, const NameExpr * name ) { 525 EnumDecl * newDecl = const_cast< EnumDecl * >( decl_node ); 526 return new QualifiedNameExpr( newDecl, name->name ); 581 527 } 582 528 583 ast::DimensionExpr * build_dimensionref( const CodeLocation & location, 584 const string * name ) { 585 ast::DimensionExpr * expr = new ast::DimensionExpr( location, *name ); 529 DimensionExpr * build_dimensionref( const string * name ) { 530 DimensionExpr * expr = new DimensionExpr( *name ); 586 531 delete name; 587 532 return expr; … … 599 544 }; // OperName 600 545 601 ast::Expr * build_cast( const CodeLocation & location, 602 DeclarationNode * decl_node, 603 ExpressionNode * expr_node, 604 ast::CastExpr::CastKind kind ) { 605 ast::Type * targetType = maybeMoveBuildType( decl_node ); 606 if ( dynamic_cast<ast::VoidType *>( targetType ) ) { 546 Expression * build_cast( DeclarationNode * decl_node, ExpressionNode * expr_node, CastExpr::CastKind kind ) { 547 Type * targetType = maybeMoveBuildType( decl_node ); 548 if ( dynamic_cast< VoidType * >( targetType ) ) { 607 549 delete targetType; 608 return new ast::CastExpr( location, 609 maybeMoveBuild( expr_node ), 610 ast::ExplicitCast, kind ); 550 return new CastExpr( maybeMoveBuild< Expression >(expr_node), false, kind ); 611 551 } else { 612 return new ast::CastExpr( location, 613 maybeMoveBuild( expr_node ), 614 targetType, 615 ast::ExplicitCast, kind ); 552 return new CastExpr( maybeMoveBuild< Expression >(expr_node), targetType, false, kind ); 616 553 } // if 617 554 } // build_cast 618 555 619 ast::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 ); 556 Expression * build_keyword_cast( AggregateDecl::Aggregate target, ExpressionNode * expr_node ) { 557 return new KeywordCastExpr( maybeMoveBuild< Expression >(expr_node), target ); 626 558 } 627 559 628 ast::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 ); 560 Expression * build_virtual_cast( DeclarationNode * decl_node, ExpressionNode * expr_node ) { 561 return new VirtualCastExpr( maybeMoveBuild< Expression >( expr_node ), maybeMoveBuildType( decl_node ) ); 635 562 } // build_virtual_cast 636 563 637 ast::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 ); 564 Expression * build_fieldSel( ExpressionNode * expr_node, Expression * member ) { 565 return new UntypedMemberExpr( member, maybeMoveBuild< Expression >(expr_node) ); 644 566 } // build_fieldSel 645 567 646 ast::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 ); 568 Expression * build_pfieldSel( ExpressionNode * expr_node, Expression * member ) { 569 UntypedExpr * deref = new UntypedExpr( new NameExpr( "*?" ) ); 652 570 deref->location = expr_node->location; 653 deref-> args.push_back( maybeMoveBuild( expr_node) );654 auto ret = new ast::UntypedMemberExpr( location,member, deref );571 deref->get_args().push_back( maybeMoveBuild< Expression >(expr_node) ); 572 UntypedMemberExpr * ret = new UntypedMemberExpr( member, deref ); 655 573 return ret; 656 574 } // build_pfieldSel 657 575 658 ast::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 ); 576 Expression * build_offsetOf( DeclarationNode * decl_node, NameExpr * member ) { 577 Expression * ret = new UntypedOffsetofExpr( maybeMoveBuildType( decl_node ), member->get_name() ); 666 578 delete member; 667 579 return ret; 668 580 } // build_offsetOf 669 581 670 ast::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 ); 582 Expression * build_and_or( ExpressionNode * expr_node1, ExpressionNode * expr_node2, bool kind ) { 583 return new LogicalExpr( notZeroExpr( maybeMoveBuild< Expression >(expr_node1) ), notZeroExpr( maybeMoveBuild< Expression >(expr_node2) ), kind ); 679 584 } // build_and_or 680 585 681 ast::Expr * build_unary_val( const CodeLocation & location, 682 OperKinds op, 683 ExpressionNode * expr_node ) { 684 std::vector<ast::ptr<ast::Expr>> args; 685 args.push_back( maybeMoveBuild( expr_node ) ); 686 return new ast::UntypedExpr( location, 687 new ast::NameExpr( location, OperName[ (int)op ] ), 688 std::move( args ) 689 ); 586 Expression * build_unary_val( OperKinds op, ExpressionNode * expr_node ) { 587 list< Expression * > args; 588 args.push_back( maybeMoveBuild< Expression >(expr_node) ); 589 return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args ); 690 590 } // build_unary_val 691 591 692 ast::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; 697 args.push_back( maybeMoveBuild( expr_node1 ) ); 698 args.push_back( maybeMoveBuild( expr_node2 ) ); 699 return new ast::UntypedExpr( location,700 new ast::NameExpr( location, OperName[ (int)op ] ),701 std::move( args )702 );592 Expression * build_unary_ptr( OperKinds op, ExpressionNode * expr_node ) { 593 list< Expression * > args; 594 args.push_back( maybeMoveBuild< Expression >(expr_node) ); // xxx -- this is exactly the same as the val case now, refactor this code. 595 return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args ); 596 } // build_unary_ptr 597 598 Expression * build_binary_val( OperKinds op, ExpressionNode * expr_node1, ExpressionNode * expr_node2 ) { 599 list< Expression * > args; 600 args.push_back( maybeMoveBuild< Expression >(expr_node1) ); 601 args.push_back( maybeMoveBuild< Expression >(expr_node2) ); 602 return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args ); 703 603 } // build_binary_val 704 604 705 ast::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 );605 Expression * build_binary_ptr( OperKinds op, ExpressionNode * expr_node1, ExpressionNode * expr_node2 ) { 606 list< Expression * > args; 607 args.push_back( maybeMoveBuild< Expression >(expr_node1) ); 608 args.push_back( maybeMoveBuild< Expression >(expr_node2) ); 609 return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args ); 610 } // build_binary_ptr 611 612 Expression * build_cond( ExpressionNode * expr_node1, ExpressionNode * expr_node2, ExpressionNode * expr_node3 ) { 613 return new ConditionalExpr( notZeroExpr( maybeMoveBuild< Expression >(expr_node1) ), maybeMoveBuild< Expression >(expr_node2), maybeMoveBuild< Expression >(expr_node3) ); 714 614 } // build_cond 715 615 716 ast::Expr * build_tuple( const CodeLocation & location, 717 ExpressionNode * expr_node ) { 718 std::vector<ast::ptr<ast::Expr>> exprs; 616 Expression * build_tuple( ExpressionNode * expr_node ) { 617 list< Expression * > exprs; 719 618 buildMoveList( expr_node, exprs ); 720 return new ast::UntypedTupleExpr( location, std::move( exprs ) );619 return new UntypedTupleExpr( exprs );; 721 620 } // build_tuple 722 621 723 ast::Expr * build_func( const CodeLocation & location, 724 ExpressionNode * function, 725 ExpressionNode * expr_node ) { 726 std::vector<ast::ptr<ast::Expr>> args; 622 Expression * build_func( ExpressionNode * function, ExpressionNode * expr_node ) { 623 list< Expression * > args; 727 624 buildMoveList( expr_node, args ); 728 return new ast::UntypedExpr( location, 729 maybeMoveBuild( function ), 730 std::move( args ) 731 ); 625 return new UntypedExpr( maybeMoveBuild< Expression >(function), args ); 732 626 } // build_func 733 627 734 ast::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 ) ); 628 Expression * build_compoundLiteral( DeclarationNode * decl_node, InitializerNode * kids ) { 629 Declaration * newDecl = maybeBuild< Declaration >(decl_node); // compound literal type 630 if ( DeclarationWithType * newDeclWithType = dynamic_cast< DeclarationWithType * >( newDecl ) ) { // non-sue compound-literal type 631 return new CompoundLiteralExpr( newDeclWithType->get_type(), maybeMoveBuild< Initializer >(kids) ); 744 632 // these types do not have associated type information 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 ) ); 633 } else if ( StructDecl * newDeclStructDecl = dynamic_cast< StructDecl * >( newDecl ) ) { 634 if ( newDeclStructDecl->has_body() ) { 635 return new CompoundLiteralExpr( new StructInstType( Type::Qualifiers(), newDeclStructDecl ), maybeMoveBuild< Initializer >(kids) ); 750 636 } else { 751 return new ast::CompoundLiteralExpr( location, 752 new ast::StructInstType( newDeclStructDecl->name ), 753 maybeMoveBuild( kids ) ); 754 } // if 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 ) ); 637 return new CompoundLiteralExpr( new StructInstType( Type::Qualifiers(), newDeclStructDecl->get_name() ), maybeMoveBuild< Initializer >(kids) ); 638 } // if 639 } else if ( UnionDecl * newDeclUnionDecl = dynamic_cast< UnionDecl * >( newDecl ) ) { 640 if ( newDeclUnionDecl->has_body() ) { 641 return new CompoundLiteralExpr( new UnionInstType( Type::Qualifiers(), newDeclUnionDecl ), maybeMoveBuild< Initializer >(kids) ); 760 642 } else { 761 return new ast::CompoundLiteralExpr( location, 762 new ast::UnionInstType( newDeclUnionDecl->name ), 763 maybeMoveBuild( kids ) ); 764 } // if 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 ) ); 643 return new CompoundLiteralExpr( new UnionInstType( Type::Qualifiers(), newDeclUnionDecl->get_name() ), maybeMoveBuild< Initializer >(kids) ); 644 } // if 645 } else if ( EnumDecl * newDeclEnumDecl = dynamic_cast< EnumDecl * >( newDecl ) ) { 646 if ( newDeclEnumDecl->has_body() ) { 647 return new CompoundLiteralExpr( new EnumInstType( Type::Qualifiers(), newDeclEnumDecl ), maybeMoveBuild< Initializer >(kids) ); 770 648 } else { 771 return new ast::CompoundLiteralExpr( location, 772 new ast::EnumInstType( newDeclEnumDecl->name ), 773 maybeMoveBuild( kids ) ); 649 return new CompoundLiteralExpr( new EnumInstType( Type::Qualifiers(), newDeclEnumDecl->get_name() ), maybeMoveBuild< Initializer >(kids) ); 774 650 } // if 775 651 } else { … … 780 656 // Local Variables: // 781 657 // tab-width: 4 // 658 // mode: c++ // 659 // compile-command: "make install" // 782 660 // End: //
Note:
See TracChangeset
for help on using the changeset viewer.