Changeset bb7422a for src/Parser/ExpressionNode.cc
- Timestamp:
- Apr 4, 2023, 2:25:52 PM (15 months ago)
- Branches:
- ADT, ast-experimental, master
- Children:
- beeff61e, e02e13f
- Parents:
- 4541b09
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/Parser/ExpressionNode.cc
r4541b09 rbb7422a 10 10 // Created On : Sat May 16 13:17:07 2015 11 11 // Last Modified By : Andrew Beach 12 // Last Modified On : Tue Mar 14 12:00:00 202313 // Update Count : 108 212 // Last Modified On : Tue Apr 4 11:07:00 2023 13 // Update Count : 1083 14 14 // 15 15 … … 21 21 #include <string> // for string, operator+, operator== 22 22 23 #include "AST/Expr.hpp" // for NameExpr 24 #include "AST/Type.hpp" // for BaseType, SueInstType 23 25 #include "Common/SemanticError.h" // for SemanticError 24 26 #include "Common/utility.h" // for maybeMoveBuild, maybeBuild, CodeLo... 25 27 #include "ParseNode.h" // for ExpressionNode, maybeMoveBuildType 26 #include "SynTree/Constant.h" // for Constant27 #include "SynTree/Declaration.h" // for EnumDecl, StructDecl, UnionDecl28 #include "SynTree/Expression.h" // for Expression, ConstantExpr, NameExpr29 #include "SynTree/Statement.h" // for CompoundStmt, Statement30 #include "SynTree/Type.h" // for BasicType, Type, Type::Qualifiers31 28 #include "parserutility.h" // for notZeroExpr 32 33 class Initializer;34 29 35 30 using namespace std; … … 48 43 // because the CONT rule is NOT triggered if the pattern is empty. Hence, constants are reparsed here to determine their 49 44 // type. 50 51 extern const Type::Qualifiers noQualifiers; // no qualifiers on constants52 45 53 46 // static inline bool checkH( char c ) { return c == 'h' || c == 'H'; } … … 127 120 } // scanbin 128 121 129 Expression * build_constantInteger( string & str ) { 130 static const BasicType::Kind kind[2][6] = { 122 ast::Expr * build_constantInteger( 123 const CodeLocation & location, string & str ) { 124 static const ast::BasicType::Kind kind[2][6] = { 131 125 // short (h) must be before char (hh) because shorter type has the longer suffix 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, },126 { ast::BasicType::ShortSignedInt, ast::BasicType::SignedChar, ast::BasicType::SignedInt, ast::BasicType::LongSignedInt, ast::BasicType::LongLongSignedInt, /* BasicType::SignedInt128 */ ast::BasicType::LongLongSignedInt, }, 127 { ast::BasicType::ShortUnsignedInt, ast::BasicType::UnsignedChar, ast::BasicType::UnsignedInt, ast::BasicType::LongUnsignedInt, ast::BasicType::LongLongUnsignedInt, /* BasicType::UnsignedInt128 */ ast::BasicType::LongLongUnsignedInt, }, 134 128 }; 135 129 … … 141 135 string str2( "0x0" ); 142 136 unsigned long long int v, v2 = 0; // converted integral value 143 Expression* ret, * ret2;137 ast::Expr * ret, * ret2; 144 138 145 139 int type = -1; // 0 => short, 1 => char, 2 => int, 3 => long int, 4 => long long int, 5 => int128 … … 149 143 // special constants 150 144 if ( str == "0" ) { 151 ret = new ConstantExpr( Constant( (Type *)new ZeroType( noQualifiers ), str, (unsigned long long int)0 ));145 ret = new ast::ConstantExpr( location, new ast::ZeroType(), str, 0 ); 152 146 goto CLEANUP; 153 147 } // if 154 148 if ( str == "1" ) { 155 ret = new ConstantExpr( Constant( (Type *)new OneType( noQualifiers ), str, (unsigned long long int)1 ));149 ret = new ast::ConstantExpr( location, new ast::OneType(), str, 1 ); 156 150 goto CLEANUP; 157 151 } // if … … 304 298 305 299 // Constant type is correct for overload resolving. 306 ret = new ConstantExpr( Constant( new BasicType( noQualifiers, kind[Unsigned][type] ), str, v ) ); 300 ret = new ast::ConstantExpr( location, 301 new ast::BasicType( kind[Unsigned][type] ), str, v ); 307 302 if ( Unsigned && type < 2 ) { // hh or h, less than int ? 308 303 // int i = -1uh => 65535 not -1, so cast is necessary for unsigned, which unfortunately eliminates warnings for large values. 309 ret = new CastExpr( ret, new BasicType( Type::Qualifiers(), kind[Unsigned][type] ), false ); 304 ret = new ast::CastExpr( location, 305 ret, 306 new ast::BasicType( kind[Unsigned][type] ), 307 ast::ExplicitCast ); 310 308 } else if ( ltype != -1 ) { // explicit length ? 311 309 if ( ltype == 6 ) { // int128, (int128)constant 312 // ret = new CastExpr( ret, new BasicType( Type::Qualifiers(), kind[Unsigned][type] ), false ); 313 ret2 = new ConstantExpr( Constant( new BasicType( noQualifiers, BasicType::LongLongSignedInt ), str2, v2 ) ); 314 ret = build_compoundLiteral( 315 DeclarationNode::newBasicType( DeclarationNode::Int128 )->addType( DeclarationNode::newSignedNess( DeclarationNode::Unsigned ) ), 316 new InitializerNode( (InitializerNode *)(new InitializerNode( new ExpressionNode( v2 == 0 ? ret2 : ret ) ))->set_last( new InitializerNode( new ExpressionNode( v2 == 0 ? ret : ret2 ) ) ), true ) ); 310 ret2 = new ast::ConstantExpr( location, 311 new ast::BasicType( ast::BasicType::LongLongSignedInt ), 312 str2, 313 v2 ); 314 ret = build_compoundLiteral( location, 315 DeclarationNode::newBasicType( 316 DeclarationNode::Int128 317 )->addType( 318 DeclarationNode::newSignedNess( DeclarationNode::Unsigned ) ), 319 new InitializerNode( 320 (InitializerNode *)(new InitializerNode( new ExpressionNode( v2 == 0 ? ret2 : ret ) ))->set_last( new InitializerNode( new ExpressionNode( v2 == 0 ? ret : ret2 ) ) ), true ) 321 ); 317 322 } else { // explicit length, (length_type)constant 318 ret = new CastExpr( ret, new TypeInstType( Type::Qualifiers(), lnthsInt[Unsigned][ltype], false ), false ); 323 ret = new ast::CastExpr( location, 324 ret, 325 new ast::TypeInstType( lnthsInt[Unsigned][ltype], ast::TypeDecl::Dtype ), 326 ast::ExplicitCast ); 319 327 if ( ltype == 5 ) { // pointer, intptr( (uintptr_t)constant ) 320 ret = build_func( new ExpressionNode( build_varref( new string( "intptr" ) ) ), new ExpressionNode( ret ) ); 328 ret = build_func( location, 329 new ExpressionNode( 330 build_varref( location, new string( "intptr" ) ) ), 331 new ExpressionNode( ret ) ); 321 332 } // if 322 333 } // if … … 362 373 363 374 364 Expression * build_constantFloat( string & str ) { 365 static const BasicType::Kind kind[2][12] = { 366 { BasicType::Float, BasicType::Double, BasicType::LongDouble, BasicType::uuFloat80, BasicType::uuFloat128, BasicType::uFloat16, BasicType::uFloat32, BasicType::uFloat32x, BasicType::uFloat64, BasicType::uFloat64x, BasicType::uFloat128, BasicType::uFloat128x }, 367 { 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 }, 375 ast::Expr * build_constantFloat( 376 const CodeLocation & location, string & str ) { 377 static const ast::BasicType::Kind kind[2][12] = { 378 { 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 }, 379 { 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 }, 368 380 }; 369 381 … … 402 414 403 415 assert( 0 <= type && type < 12 ); 404 Expression * ret = new ConstantExpr( Constant( new BasicType( noQualifiers, kind[complx][type] ), str, v ) ); 405 if ( explnth ) { // explicit length ? 406 ret = new CastExpr( ret, new BasicType( Type::Qualifiers(), kind[complx][type] ), false ); 416 ast::Expr * ret = new ast::ConstantExpr( location, 417 new ast::BasicType( kind[complx][type] ), 418 str, 419 v ); 420 // explicit length ? 421 if ( explnth ) { 422 ret = new ast::CastExpr( location, 423 ret, 424 new ast::BasicType( kind[complx][type] ), 425 ast::ExplicitCast ); 407 426 } // if 408 427 … … 419 438 } // sepString 420 439 421 Expression * build_constantChar(string & str ) {440 ast::Expr * build_constantChar( const CodeLocation & location, string & str ) { 422 441 string units; // units 423 442 sepString( str, units, '\'' ); // separate constant from units 424 443 425 Expression * ret = new ConstantExpr( Constant( new BasicType( noQualifiers, BasicType::Char ), str, (unsigned long long int)(unsigned char)str[1] ) ); 444 ast::Expr * ret = new ast::ConstantExpr( location, 445 new ast::BasicType( ast::BasicType::Char ), 446 str, 447 (unsigned long long int)(unsigned char)str[1] ); 426 448 if ( units.length() != 0 ) { 427 ret = new UntypedExpr( new NameExpr( units ), { ret } ); 449 ret = new ast::UntypedExpr( location, 450 new ast::NameExpr( location, units ), 451 { ret } ); 428 452 } // if 429 453 … … 432 456 } // build_constantChar 433 457 434 Expression * build_constantStr( string & str ) { 458 ast::Expr * build_constantStr( 459 const CodeLocation & location, 460 string & str ) { 435 461 assert( str.length() > 0 ); 436 462 string units; // units 437 463 sepString( str, units, '"' ); // separate constant from units 438 464 439 Type * strtype;465 ast::Type * strtype; 440 466 switch ( str[0] ) { // str has >= 2 characters, i.e, null string "" => safe to look at subscripts 0/1 441 467 case 'u': 442 468 if ( str[1] == '8' ) goto Default; // utf-8 characters => array of char 443 469 // lookup type of associated typedef 444 strtype = new TypeInstType( Type::Qualifiers( ), "char16_t", false );470 strtype = new ast::TypeInstType( "char16_t", ast::TypeDecl::Dtype ); 445 471 break; 446 472 case 'U': 447 strtype = new TypeInstType( Type::Qualifiers( ), "char32_t", false );473 strtype = new ast::TypeInstType( "char32_t", ast::TypeDecl::Dtype ); 448 474 break; 449 475 case 'L': 450 strtype = new TypeInstType( Type::Qualifiers( ), "wchar_t", false );476 strtype = new ast::TypeInstType( "wchar_t", ast::TypeDecl::Dtype ); 451 477 break; 452 478 Default: // char default string type 453 479 default: 454 strtype = new BasicType( Type::Qualifiers( ),BasicType::Char );480 strtype = new ast::BasicType( ast::BasicType::Char ); 455 481 } // switch 456 ArrayType * at = new ArrayType( noQualifiers, strtype, 457 new ConstantExpr( Constant::from_ulong( str.size() + 1 - 2 ) ), // +1 for '\0' and -2 for '"' 458 false, false ); 459 Expression * ret = new ConstantExpr( Constant( at, str, std::nullopt ) ); 482 ast::ArrayType * at = new ast::ArrayType( 483 strtype, 484 // Length is adjusted: +1 for '\0' and -2 for '"' 485 ast::ConstantExpr::from_ulong( location, str.size() + 1 - 2 ), 486 ast::FixedLen, 487 ast::DynamicDim ); 488 ast::Expr * ret = new ast::ConstantExpr( location, at, str, std::nullopt ); 460 489 if ( units.length() != 0 ) { 461 ret = new UntypedExpr( new NameExpr( units ), { ret } ); 490 ret = new ast::UntypedExpr( location, 491 new ast::NameExpr( location, units ), 492 { ret } ); 462 493 } // if 463 494 … … 466 497 } // build_constantStr 467 498 468 Expression * build_field_name_FLOATING_FRACTIONconstant( const string & str ) { 499 ast::Expr * build_field_name_FLOATING_FRACTIONconstant( 500 const CodeLocation & location, const string & str ) { 469 501 if ( str.find_first_not_of( "0123456789", 1 ) != string::npos ) SemanticError( yylloc, "invalid tuple index " + str ); 470 Expression * ret = build_constantInteger( *new string( str.substr(1) ) ); 502 ast::Expr * ret = build_constantInteger( location, 503 *new string( str.substr(1) ) ); 471 504 delete &str; 472 505 return ret; 473 506 } // build_field_name_FLOATING_FRACTIONconstant 474 507 475 Expression * build_field_name_FLOATING_DECIMALconstant( const string & str ) { 508 ast::Expr * build_field_name_FLOATING_DECIMALconstant( 509 const CodeLocation & location, const string & str ) { 476 510 if ( str[str.size() - 1] != '.' ) SemanticError( yylloc, "invalid tuple index " + str ); 477 Expression * ret = build_constantInteger( *new string( str.substr( 0, str.size()-1 ) ) ); 511 ast::Expr * ret = build_constantInteger( 512 location, *new string( str.substr( 0, str.size()-1 ) ) ); 478 513 delete &str; 479 514 return ret; 480 515 } // build_field_name_FLOATING_DECIMALconstant 481 516 482 Expression * build_field_name_FLOATINGconstant( const string & str ) { 517 ast::Expr * build_field_name_FLOATINGconstant( const CodeLocation & location, 518 const string & str ) { 483 519 // str is of the form A.B -> separate at the . and return member expression 484 520 int a, b; … … 486 522 stringstream ss( str ); 487 523 ss >> a >> dot >> b; 488 UntypedMemberExpr * ret = new UntypedMemberExpr( new ConstantExpr( Constant::from_int( b ) ), new ConstantExpr( Constant::from_int( a ) ) ); 524 auto ret = new ast::UntypedMemberExpr( location, 525 ast::ConstantExpr::from_int( location, b ), 526 ast::ConstantExpr::from_int( location, a ) 527 ); 489 528 delete &str; 490 529 return ret; 491 530 } // build_field_name_FLOATINGconstant 492 531 493 Expression * make_field_name_fraction_constants( Expression * fieldName, Expression * fracts ) { 494 if ( fracts ) { 495 if ( UntypedMemberExpr * memberExpr = dynamic_cast< UntypedMemberExpr * >( fracts ) ) { 496 memberExpr->set_member( make_field_name_fraction_constants( fieldName, memberExpr->get_aggregate() ) ); 497 return memberExpr; 498 } else { 499 return new UntypedMemberExpr( fracts, fieldName ); 500 } // if 501 } // if 502 return fieldName; 532 ast::Expr * make_field_name_fraction_constants( const CodeLocation & location, 533 ast::Expr * fieldName, 534 ast::Expr * fracts ) { 535 if ( nullptr == fracts ) { 536 return fieldName; 537 } else if ( auto memberExpr = dynamic_cast<ast::UntypedMemberExpr *>( fracts ) ) { 538 memberExpr->member = make_field_name_fraction_constants( location, 539 fieldName, 540 ast::mutate( memberExpr->aggregate.get() ) ); 541 return memberExpr; 542 } else { 543 return new ast::UntypedMemberExpr( location, fracts, fieldName ); 544 } // if 503 545 } // make_field_name_fraction_constants 504 546 505 Expression * build_field_name_fraction_constants( Expression * fieldName, ExpressionNode * fracts ) { 506 return make_field_name_fraction_constants( fieldName, maybeMoveBuild( fracts ) ); 547 ast::Expr * build_field_name_fraction_constants( const CodeLocation & location, 548 ast::Expr * fieldName, 549 ExpressionNode * fracts ) { 550 return make_field_name_fraction_constants( location, fieldName, maybeMoveBuild( fracts ) ); 507 551 } // build_field_name_fraction_constants 508 552 509 NameExpr * build_varref( const string * name ) { 510 NameExpr * expr = new NameExpr( *name ); 553 ast::NameExpr * build_varref( const CodeLocation & location, 554 const string * name ) { 555 ast::NameExpr * expr = new ast::NameExpr( location, *name ); 511 556 delete name; 512 557 return expr; 513 558 } // build_varref 514 559 515 QualifiedNameExpr * build_qualified_expr( const DeclarationNode * decl_node, const NameExpr * name ) { 516 Declaration * newDecl = maybeBuild(decl_node); 517 if ( DeclarationWithType * newDeclWithType = dynamic_cast< DeclarationWithType * >( newDecl ) ) { 518 const Type * t = newDeclWithType->get_type(); 519 if ( t ) { 520 if ( const TypeInstType * typeInst = dynamic_cast<const TypeInstType *>( t ) ) { 521 newDecl= new EnumDecl( typeInst->name ); 560 ast::QualifiedNameExpr * build_qualified_expr( const CodeLocation & location, 561 const DeclarationNode * decl_node, 562 const ast::NameExpr * name ) { 563 ast::Decl * newDecl = maybeBuild( decl_node ); 564 if ( ast::DeclWithType * newDeclWithType = dynamic_cast<ast::DeclWithType *>( newDecl ) ) { 565 if ( const ast::Type * t = newDeclWithType->get_type() ) { 566 if ( auto typeInst = dynamic_cast<const ast::TypeInstType *>( t ) ) { 567 newDecl = new ast::EnumDecl( location, typeInst->name ); 522 568 } 523 569 } 524 570 } 525 return new QualifiedNameExpr(newDecl, name->name );571 return new ast::QualifiedNameExpr( location, newDecl, name->name ); 526 572 } 527 573 528 QualifiedNameExpr * build_qualified_expr( const EnumDecl * decl_node, const NameExpr * name ) { 529 EnumDecl * newDecl = const_cast< EnumDecl * >( decl_node ); 530 return new QualifiedNameExpr( newDecl, name->name ); 574 ast::QualifiedNameExpr * build_qualified_expr( const CodeLocation & location, 575 const ast::EnumDecl * decl, 576 const ast::NameExpr * name ) { 577 return new ast::QualifiedNameExpr( location, decl, name->name ); 531 578 } 532 579 533 DimensionExpr * build_dimensionref( const string * name ) { 534 DimensionExpr * expr = new DimensionExpr( *name ); 580 ast::DimensionExpr * build_dimensionref( const CodeLocation & location, 581 const string * name ) { 582 ast::DimensionExpr * expr = new ast::DimensionExpr( location, *name ); 535 583 delete name; 536 584 return expr; … … 548 596 }; // OperName 549 597 550 Expression * build_cast( DeclarationNode * decl_node, ExpressionNode * expr_node ) { 551 Type * targetType = maybeMoveBuildType( decl_node ); 552 if ( dynamic_cast< VoidType * >( targetType ) ) { 598 ast::Expr * build_cast( const CodeLocation & location, 599 DeclarationNode * decl_node, 600 ExpressionNode * expr_node ) { 601 ast::Type * targetType = maybeMoveBuildType( decl_node ); 602 if ( dynamic_cast<ast::VoidType *>( targetType ) ) { 553 603 delete targetType; 554 return new CastExpr( maybeMoveBuild( expr_node ), false ); 604 return new ast::CastExpr( location, 605 maybeMoveBuild( expr_node ), 606 ast::ExplicitCast ); 555 607 } else { 556 return new CastExpr( maybeMoveBuild( expr_node ), targetType, false ); 608 return new ast::CastExpr( location, 609 maybeMoveBuild( expr_node ), 610 targetType, 611 ast::ExplicitCast ); 557 612 } // if 558 613 } // build_cast 559 614 560 Expression * build_keyword_cast( AggregateDecl::Aggregate target, ExpressionNode * expr_node ) { 561 return new KeywordCastExpr( maybeMoveBuild( expr_node ), target ); 615 ast::Expr * build_keyword_cast( const CodeLocation & location, 616 ast::AggregateDecl::Aggregate target, 617 ExpressionNode * expr_node ) { 618 return new ast::KeywordCastExpr( location, 619 maybeMoveBuild( expr_node ), 620 target 621 ); 562 622 } 563 623 564 Expression * build_virtual_cast( DeclarationNode * decl_node, ExpressionNode * expr_node ) { 565 return new VirtualCastExpr( maybeMoveBuild( expr_node ), maybeMoveBuildType( decl_node ) ); 624 ast::Expr * build_virtual_cast( const CodeLocation & location, 625 DeclarationNode * decl_node, 626 ExpressionNode * expr_node ) { 627 return new ast::VirtualCastExpr( location, 628 maybeMoveBuild( expr_node ), 629 maybeMoveBuildType( decl_node ) 630 ); 566 631 } // build_virtual_cast 567 632 568 Expression * build_fieldSel( ExpressionNode * expr_node, Expression * member ) { 569 return new UntypedMemberExpr( member, maybeMoveBuild( expr_node ) ); 633 ast::Expr * build_fieldSel( const CodeLocation & location, 634 ExpressionNode * expr_node, 635 ast::Expr * member ) { 636 return new ast::UntypedMemberExpr( location, 637 member, 638 maybeMoveBuild( expr_node ) 639 ); 570 640 } // build_fieldSel 571 641 572 Expression * build_pfieldSel( ExpressionNode * expr_node, Expression * member ) { 573 UntypedExpr * deref = new UntypedExpr( new NameExpr( "*?" ) ); 642 ast::Expr * build_pfieldSel( const CodeLocation & location, 643 ExpressionNode * expr_node, 644 ast::Expr * member ) { 645 auto deref = new ast::UntypedExpr( location, 646 new ast::NameExpr( location, "*?" ) 647 ); 574 648 deref->location = expr_node->location; 575 deref-> get_args().push_back( maybeMoveBuild( expr_node ) );576 UntypedMemberExpr * ret = new UntypedMemberExpr(member, deref );649 deref->args.push_back( maybeMoveBuild( expr_node ) ); 650 auto ret = new ast::UntypedMemberExpr( location, member, deref ); 577 651 return ret; 578 652 } // build_pfieldSel 579 653 580 Expression * build_offsetOf( DeclarationNode * decl_node, NameExpr * member ) { 581 Expression * ret = new UntypedOffsetofExpr( maybeMoveBuildType( decl_node ), member->get_name() ); 654 ast::Expr * build_offsetOf( const CodeLocation & location, 655 DeclarationNode * decl_node, 656 ast::NameExpr * member ) { 657 ast::Expr * ret = new ast::UntypedOffsetofExpr( location, 658 maybeMoveBuildType( decl_node ), 659 member->name 660 ); 661 ret->result = new ast::BasicType( ast::BasicType::LongUnsignedInt ); 582 662 delete member; 583 663 return ret; 584 664 } // build_offsetOf 585 665 586 Expression * build_and_or( ExpressionNode * expr_node1, ExpressionNode * expr_node2, bool kind ) { 587 return new LogicalExpr( notZeroExpr( maybeMoveBuild( expr_node1 ) ), notZeroExpr( maybeMoveBuild( expr_node2 ) ), kind ); 666 ast::Expr * build_and_or( const CodeLocation & location, 667 ExpressionNode * expr_node1, 668 ExpressionNode * expr_node2, 669 ast::LogicalFlag flag ) { 670 return new ast::LogicalExpr( location, 671 notZeroExpr( maybeMoveBuild( expr_node1 ) ), 672 notZeroExpr( maybeMoveBuild( expr_node2 ) ), 673 flag 674 ); 588 675 } // build_and_or 589 676 590 Expression * build_unary_val( OperKinds op, ExpressionNode * expr_node ) { 591 list< Expression * > args; 677 ast::Expr * build_unary_val( const CodeLocation & location, 678 OperKinds op, 679 ExpressionNode * expr_node ) { 680 std::vector<ast::ptr<ast::Expr>> args; 592 681 args.push_back( maybeMoveBuild( expr_node ) ); 593 return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args ); 682 return new ast::UntypedExpr( location, 683 new ast::NameExpr( location, OperName[ (int)op ] ), 684 std::move( args ) 685 ); 594 686 } // build_unary_val 595 687 596 Expression * build_binary_val( OperKinds op, ExpressionNode * expr_node1, ExpressionNode * expr_node2 ) { 597 list< Expression * > args; 688 ast::Expr * build_binary_val( const CodeLocation & location, 689 OperKinds op, 690 ExpressionNode * expr_node1, 691 ExpressionNode * expr_node2 ) { 692 std::vector<ast::ptr<ast::Expr>> args; 598 693 args.push_back( maybeMoveBuild( expr_node1 ) ); 599 694 args.push_back( maybeMoveBuild( expr_node2 ) ); 600 return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args ); 695 return new ast::UntypedExpr( location, 696 new ast::NameExpr( location, OperName[ (int)op ] ), 697 std::move( args ) 698 ); 601 699 } // build_binary_val 602 700 603 Expression * build_binary_ptr( OperKinds op, ExpressionNode * expr_node1, ExpressionNode * expr_node2 ) { 604 list< Expression * > args;605 args.push_back( maybeMoveBuild( expr_node1 ) );606 args.push_back( maybeMoveBuild( expr_node2 ) );607 return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args);701 ast::Expr * build_binary_ptr( const CodeLocation & location, 702 OperKinds op, 703 ExpressionNode * expr_node1, 704 ExpressionNode * expr_node2 ) { 705 return build_binary_val( location, op, expr_node1, expr_node2 ); 608 706 } // build_binary_ptr 609 707 610 Expression * build_cond( ExpressionNode * expr_node1, ExpressionNode * expr_node2, ExpressionNode * expr_node3 ) { 611 return new ConditionalExpr( notZeroExpr( maybeMoveBuild( expr_node1 ) ), maybeMoveBuild( expr_node2 ), maybeMoveBuild( expr_node3 ) ); 708 ast::Expr * build_cond( const CodeLocation & location, 709 ExpressionNode * expr_node1, 710 ExpressionNode * expr_node2, 711 ExpressionNode * expr_node3 ) { 712 return new ast::ConditionalExpr( location, 713 notZeroExpr( maybeMoveBuild( expr_node1 ) ), 714 maybeMoveBuild( expr_node2 ), 715 maybeMoveBuild( expr_node3 ) 716 ); 612 717 } // build_cond 613 718 614 Expression * build_tuple( ExpressionNode * expr_node ) { 615 list< Expression * > exprs; 719 ast::Expr * build_tuple( const CodeLocation & location, 720 ExpressionNode * expr_node ) { 721 std::vector<ast::ptr<ast::Expr>> exprs; 616 722 buildMoveList( expr_node, exprs ); 617 return new UntypedTupleExpr( exprs );;723 return new ast::UntypedTupleExpr( location, std::move( exprs ) ); 618 724 } // build_tuple 619 725 620 Expression * build_func( ExpressionNode * function, ExpressionNode * expr_node ) { 621 list< Expression * > args; 726 ast::Expr * build_func( const CodeLocation & location, 727 ExpressionNode * function, 728 ExpressionNode * expr_node ) { 729 std::vector<ast::ptr<ast::Expr>> args; 622 730 buildMoveList( expr_node, args ); 623 return new UntypedExpr( maybeMoveBuild( function ), args ); 731 return new ast::UntypedExpr( location, 732 maybeMoveBuild( function ), 733 std::move( args ) 734 ); 624 735 } // build_func 625 736 626 Expression * build_compoundLiteral( DeclarationNode * decl_node, InitializerNode * kids ) { 627 Declaration * newDecl = maybeBuild( decl_node ); // compound literal type 628 if ( DeclarationWithType * newDeclWithType = dynamic_cast< DeclarationWithType * >( newDecl ) ) { // non-sue compound-literal type 629 return new CompoundLiteralExpr( newDeclWithType->get_type(), maybeMoveBuild( kids ) ); 737 ast::Expr * build_compoundLiteral( const CodeLocation & location, 738 DeclarationNode * decl_node, 739 InitializerNode * kids ) { 740 // compound literal type 741 ast::Decl * newDecl = maybeBuild( decl_node ); 742 // non-sue compound-literal type 743 if ( ast::DeclWithType * newDeclWithType = dynamic_cast<ast::DeclWithType *>( newDecl ) ) { 744 return new ast::CompoundLiteralExpr( location, 745 newDeclWithType->get_type(), 746 maybeMoveBuild( kids ) ); 630 747 // these types do not have associated type information 631 } else if ( StructDecl * newDeclStructDecl = dynamic_cast< StructDecl * >( newDecl ) ) { 632 if ( newDeclStructDecl->has_body() ) { 633 return new CompoundLiteralExpr( new StructInstType( Type::Qualifiers(), newDeclStructDecl ), maybeMoveBuild( kids ) ); 748 } else if ( auto newDeclStructDecl = dynamic_cast<ast::StructDecl *>( newDecl ) ) { 749 if ( newDeclStructDecl->body ) { 750 return new ast::CompoundLiteralExpr( location, 751 new ast::StructInstType( newDeclStructDecl ), 752 maybeMoveBuild( kids ) ); 634 753 } else { 635 return new CompoundLiteralExpr( new StructInstType( Type::Qualifiers(), newDeclStructDecl->get_name() ), maybeMoveBuild( kids ) ); 636 } // if 637 } else if ( UnionDecl * newDeclUnionDecl = dynamic_cast< UnionDecl * >( newDecl ) ) { 638 if ( newDeclUnionDecl->has_body() ) { 639 return new CompoundLiteralExpr( new UnionInstType( Type::Qualifiers(), newDeclUnionDecl ), maybeMoveBuild( kids ) ); 754 return new ast::CompoundLiteralExpr( location, 755 new ast::StructInstType( newDeclStructDecl->name ), 756 maybeMoveBuild( kids ) ); 757 } // if 758 } else if ( auto newDeclUnionDecl = dynamic_cast<ast::UnionDecl *>( newDecl ) ) { 759 if ( newDeclUnionDecl->body ) { 760 return new ast::CompoundLiteralExpr( location, 761 new ast::UnionInstType( newDeclUnionDecl ), 762 maybeMoveBuild( kids ) ); 640 763 } else { 641 return new CompoundLiteralExpr( new UnionInstType( Type::Qualifiers(), newDeclUnionDecl->get_name() ), maybeMoveBuild( kids ) ); 642 } // if 643 } else if ( EnumDecl * newDeclEnumDecl = dynamic_cast< EnumDecl * >( newDecl ) ) { 644 if ( newDeclEnumDecl->has_body() ) { 645 return new CompoundLiteralExpr( new EnumInstType( Type::Qualifiers(), newDeclEnumDecl ), maybeMoveBuild( kids ) ); 764 return new ast::CompoundLiteralExpr( location, 765 new ast::UnionInstType( newDeclUnionDecl->name ), 766 maybeMoveBuild( kids ) ); 767 } // if 768 } else if ( auto newDeclEnumDecl = dynamic_cast<ast::EnumDecl *>( newDecl ) ) { 769 if ( newDeclEnumDecl->body ) { 770 return new ast::CompoundLiteralExpr( location, 771 new ast::EnumInstType( newDeclEnumDecl ), 772 maybeMoveBuild( kids ) ); 646 773 } else { 647 return new CompoundLiteralExpr( new EnumInstType( Type::Qualifiers(), newDeclEnumDecl->get_name() ), maybeMoveBuild( kids ) ); 774 return new ast::CompoundLiteralExpr( location, 775 new ast::EnumInstType( newDeclEnumDecl->name ), 776 maybeMoveBuild( kids ) ); 648 777 } // if 649 778 } else {
Note: See TracChangeset
for help on using the changeset viewer.