Changes in src/Parser/ExpressionNode.cc [6611177:46da46b]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/Parser/ExpressionNode.cc
r6611177 r46da46b 9 9 // Author : Peter A. Buhr 10 10 // Created On : Sat May 16 13:17:07 2015 11 // Last Modified By : Andrew Beach 12 // Last Modified On : Tue Apr 4 11:07:00 2023 13 // Update Count : 1083 14 // 15 16 #include "ExpressionNode.h" 11 // Last Modified By : Peter A. Buhr 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 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 … … 195 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; … … 205 206 unsigned int len = str.length(); 206 207 if ( len > (2 + 16 + 16) ) SemanticError( yylloc, "128-bit hexadecimal constant to large " + 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 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__ … … 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 ) { 464 Expression * build_field_name_FLOATING_FRACTIONconstant( const string & str ) { 504 465 if ( str.find_first_not_of( "0123456789", 1 ) != string::npos ) SemanticError( yylloc, "invalid tuple index " + str ); 505 ast::Expr * ret = build_constantInteger( location, 506 *new string( str.substr(1) ) ); 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 ) { 471 Expression * build_field_name_FLOATING_DECIMALconstant( const string & str ) { 513 472 if ( str[str.size() - 1] != '.' ) SemanticError( yylloc, "invalid tuple index " + str ); 514 ast::Expr * ret = build_constantInteger( 515 location, *new string( str.substr( 0, str.size()-1 ) ) ); 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::Type * targetType = maybeMoveBuildType( decl_node ); 605 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 ) ) { 606 549 delete targetType; 607 return new ast::CastExpr( location, 608 maybeMoveBuild( expr_node ), 609 ast::ExplicitCast ); 550 return new CastExpr( maybeMoveBuild< Expression >(expr_node), false, kind ); 610 551 } else { 611 return new ast::CastExpr( location, 612 maybeMoveBuild( expr_node ), 613 targetType, 614 ast::ExplicitCast ); 552 return new CastExpr( maybeMoveBuild< Expression >(expr_node), targetType, false, kind ); 615 553 } // if 616 554 } // build_cast 617 555 618 ast::Expr * build_keyword_cast( const CodeLocation & location, 619 ast::AggregateDecl::Aggregate target, 620 ExpressionNode * expr_node ) { 621 return new ast::KeywordCastExpr( location, 622 maybeMoveBuild( expr_node ), 623 target 624 ); 556 Expression * build_keyword_cast( AggregateDecl::Aggregate target, ExpressionNode * expr_node ) { 557 return new KeywordCastExpr( maybeMoveBuild< Expression >(expr_node), target ); 625 558 } 626 559 627 ast::Expr * build_virtual_cast( const CodeLocation & location, 628 DeclarationNode * decl_node, 629 ExpressionNode * expr_node ) { 630 return new ast::VirtualCastExpr( location, 631 maybeMoveBuild( expr_node ), 632 maybeMoveBuildType( decl_node ) 633 ); 560 Expression * build_virtual_cast( DeclarationNode * decl_node, ExpressionNode * expr_node ) { 561 return new VirtualCastExpr( maybeMoveBuild< Expression >( expr_node ), maybeMoveBuildType( decl_node ) ); 634 562 } // build_virtual_cast 635 563 636 ast::Expr * build_fieldSel( const CodeLocation & location, 637 ExpressionNode * expr_node, 638 ast::Expr * member ) { 639 return new ast::UntypedMemberExpr( location, 640 member, 641 maybeMoveBuild( expr_node ) 642 ); 564 Expression * build_fieldSel( ExpressionNode * expr_node, Expression * member ) { 565 return new UntypedMemberExpr( member, maybeMoveBuild< Expression >(expr_node) ); 643 566 } // build_fieldSel 644 567 645 ast::Expr * build_pfieldSel( const CodeLocation & location, 646 ExpressionNode * expr_node, 647 ast::Expr * member ) { 648 auto deref = new ast::UntypedExpr( location, 649 new ast::NameExpr( location, "*?" ) 650 ); 568 Expression * build_pfieldSel( ExpressionNode * expr_node, Expression * member ) { 569 UntypedExpr * deref = new UntypedExpr( new NameExpr( "*?" ) ); 651 570 deref->location = expr_node->location; 652 deref-> args.push_back( maybeMoveBuild( expr_node) );653 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 ); 654 573 return ret; 655 574 } // build_pfieldSel 656 575 657 ast::Expr * build_offsetOf( const CodeLocation & location, 658 DeclarationNode * decl_node, 659 ast::NameExpr * member ) { 660 ast::Expr * ret = new ast::UntypedOffsetofExpr( location, 661 maybeMoveBuildType( decl_node ), 662 member->name 663 ); 664 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() ); 665 578 delete member; 666 579 return ret; 667 580 } // build_offsetOf 668 581 669 ast::Expr * build_and_or( const CodeLocation & location, 670 ExpressionNode * expr_node1, 671 ExpressionNode * expr_node2, 672 ast::LogicalFlag flag ) { 673 return new ast::LogicalExpr( location, 674 notZeroExpr( maybeMoveBuild( expr_node1 ) ), 675 notZeroExpr( maybeMoveBuild( expr_node2 ) ), 676 flag 677 ); 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 ); 678 584 } // build_and_or 679 585 680 ast::Expr * build_unary_val( const CodeLocation & location, 681 OperKinds op, 682 ExpressionNode * expr_node ) { 683 std::vector<ast::ptr<ast::Expr>> args; 684 args.push_back( maybeMoveBuild( expr_node ) ); 685 return new ast::UntypedExpr( location, 686 new ast::NameExpr( location, OperName[ (int)op ] ), 687 std::move( args ) 688 ); 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 ); 689 590 } // build_unary_val 690 591 691 ast::Expr * build_binary_val( const CodeLocation & location, 692 OperKinds op,693 ExpressionNode * expr_node1,694 ExpressionNode * expr_node2 ) {695 std::vector<ast::ptr<ast::Expr>> args; 696 args.push_back( maybeMoveBuild( expr_node1 ) ); 697 args.push_back( maybeMoveBuild( expr_node2 ) ); 698 return new ast::UntypedExpr( location,699 new ast::NameExpr( location, OperName[ (int)op ] ),700 std::move( args )701 );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 ); 702 603 } // build_binary_val 703 604 704 ast::Expr * build_cond( const CodeLocation & location, 705 ExpressionNode * expr_node1,706 ExpressionNode * expr_node2,707 ExpressionNode * expr_node3 ) {708 return new ast::ConditionalExpr( location,709 notZeroExpr( maybeMoveBuild( expr_node1 ) ), 710 maybeMoveBuild( expr_node2 ), 711 maybeMoveBuild( expr_node3 ) 712 );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) ); 713 614 } // build_cond 714 615 715 ast::Expr * build_tuple( const CodeLocation & location, 716 ExpressionNode * expr_node ) { 717 std::vector<ast::ptr<ast::Expr>> exprs; 616 Expression * build_tuple( ExpressionNode * expr_node ) { 617 list< Expression * > exprs; 718 618 buildMoveList( expr_node, exprs ); 719 return new ast::UntypedTupleExpr( location, std::move( exprs ) );619 return new UntypedTupleExpr( exprs );; 720 620 } // build_tuple 721 621 722 ast::Expr * build_func( const CodeLocation & location, 723 ExpressionNode * function, 724 ExpressionNode * expr_node ) { 725 std::vector<ast::ptr<ast::Expr>> args; 622 Expression * build_func( ExpressionNode * function, ExpressionNode * expr_node ) { 623 list< Expression * > args; 726 624 buildMoveList( expr_node, args ); 727 return new ast::UntypedExpr( location, 728 maybeMoveBuild( function ), 729 std::move( args ) 730 ); 625 return new UntypedExpr( maybeMoveBuild< Expression >(function), args ); 731 626 } // build_func 732 627 733 ast::Expr * build_compoundLiteral( const CodeLocation & location, 734 DeclarationNode * decl_node, 735 InitializerNode * kids ) { 736 // compound literal type 737 ast::Decl * newDecl = maybeBuild( decl_node ); 738 // non-sue compound-literal type 739 if ( ast::DeclWithType * newDeclWithType = dynamic_cast<ast::DeclWithType *>( newDecl ) ) { 740 return new ast::CompoundLiteralExpr( location, 741 newDeclWithType->get_type(), 742 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) ); 743 632 // these types do not have associated type information 744 } else if ( auto newDeclStructDecl = dynamic_cast<ast::StructDecl *>( newDecl ) ) { 745 if ( newDeclStructDecl->body ) { 746 return new ast::CompoundLiteralExpr( location, 747 new ast::StructInstType( newDeclStructDecl ), 748 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) ); 749 636 } else { 750 return new ast::CompoundLiteralExpr( location, 751 new ast::StructInstType( newDeclStructDecl->name ), 752 maybeMoveBuild( kids ) ); 753 } // if 754 } else if ( auto newDeclUnionDecl = dynamic_cast<ast::UnionDecl *>( newDecl ) ) { 755 if ( newDeclUnionDecl->body ) { 756 return new ast::CompoundLiteralExpr( location, 757 new ast::UnionInstType( newDeclUnionDecl ), 758 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) ); 759 642 } else { 760 return new ast::CompoundLiteralExpr( location, 761 new ast::UnionInstType( newDeclUnionDecl->name ), 762 maybeMoveBuild( kids ) ); 763 } // if 764 } else if ( auto newDeclEnumDecl = dynamic_cast<ast::EnumDecl *>( newDecl ) ) { 765 if ( newDeclEnumDecl->body ) { 766 return new ast::CompoundLiteralExpr( location, 767 new ast::EnumInstType( newDeclEnumDecl ), 768 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) ); 769 648 } else { 770 return new ast::CompoundLiteralExpr( location, 771 new ast::EnumInstType( newDeclEnumDecl->name ), 772 maybeMoveBuild( kids ) ); 649 return new CompoundLiteralExpr( new EnumInstType( Type::Qualifiers(), newDeclEnumDecl->get_name() ), maybeMoveBuild< Initializer >(kids) ); 773 650 } // if 774 651 } else { … … 779 656 // Local Variables: // 780 657 // tab-width: 4 // 658 // mode: c++ // 659 // compile-command: "make install" // 781 660 // End: //
Note: See TracChangeset
for help on using the changeset viewer.