Changeset b110bcc for src/Parser/ExpressionNode.cc
- Timestamp:
- Apr 21, 2023, 5:36:12 PM (2 years ago)
- Branches:
- ADT, master
- Children:
- 28f8f15, 6e4c44d
- Parents:
- 2ed94a9 (diff), 699a97d (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/Parser/ExpressionNode.cc
r2ed94a9 rb110bcc 9 9 // Author : Peter A. Buhr 10 10 // Created On : Sat May 16 13:17:07 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Sat Aug 7 09:18:56 2021 13 // Update Count : 1077 14 // 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" 15 17 16 18 #include <cassert> // for assert … … 21 23 #include <string> // for string, operator+, operator== 22 24 25 #include "AST/Expr.hpp" // for NameExpr 26 #include "AST/Type.hpp" // for BaseType, SueInstType 23 27 #include "Common/SemanticError.h" // for SemanticError 24 28 #include "Common/utility.h" // for maybeMoveBuild, maybeBuild, CodeLo... 25 #include "ParseNode.h" // for ExpressionNode, maybeMoveBuildType 26 #include "SynTree/Constant.h" // for Constant 27 #include "SynTree/Declaration.h" // for EnumDecl, StructDecl, UnionDecl 28 #include "SynTree/Expression.h" // for Expression, ConstantExpr, NameExpr 29 #include "SynTree/Statement.h" // for CompoundStmt, Statement 30 #include "SynTree/Type.h" // for BasicType, Type, Type::Qualifiers 29 #include "DeclarationNode.h" // for DeclarationNode 30 #include "InitializerNode.h" // for InitializerNode 31 31 #include "parserutility.h" // for notZeroExpr 32 33 class Initializer;34 32 35 33 using namespace std; … … 48 46 // because the CONT rule is NOT triggered if the pattern is empty. Hence, constants are reparsed here to determine their 49 47 // type. 50 51 extern const Type::Qualifiers noQualifiers; // no qualifiers on constants52 48 53 49 // static inline bool checkH( char c ) { return c == 'h' || c == 'H'; } … … 71 67 size_t end = str.length() - 1; 72 68 if ( posn == end ) { type = 3; return; } // no length after 'l' => long 73 69 74 70 string::size_type next = posn + 1; // advance to length 75 71 if ( str[next] == '3' ) { // 32 … … 122 118 if ( str[i] == '1' ) v |= 1; 123 119 i += 1; 124 120 if ( i == last - 1 || (str[i] != '0' && str[i] != '1') ) break; 125 121 v <<= 1; 126 122 } // for 127 123 } // scanbin 128 124 129 Expression * build_constantInteger( string & str ) { 130 static const BasicType::Kind kind[2][6] = { 125 ast::Expr * build_constantInteger( 126 const CodeLocation & location, string & str ) { 127 static const ast::BasicType::Kind kind[2][6] = { 131 128 // 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, },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, }, 134 131 }; 135 132 … … 141 138 string str2( "0x0" ); 142 139 unsigned long long int v, v2 = 0; // converted integral value 143 Expression* ret, * ret2;140 ast::Expr * ret, * ret2; 144 141 145 142 int type = -1; // 0 => short, 1 => char, 2 => int, 3 => long int, 4 => long long int, 5 => int128 … … 149 146 // special constants 150 147 if ( str == "0" ) { 151 ret = new ConstantExpr( Constant( (Type *)new ZeroType( noQualifiers ), str, (unsigned long long int)0 ));148 ret = new ast::ConstantExpr( location, new ast::ZeroType(), str, 0 ); 152 149 goto CLEANUP; 153 150 } // if 154 151 if ( str == "1" ) { 155 ret = new ConstantExpr( Constant( (Type *)new OneType( noQualifiers ), str, (unsigned long long int)1 ));152 ret = new ast::ConstantExpr( location, new ast::OneType(), str, 1 ); 156 153 goto CLEANUP; 157 154 } // if 158 159 string::size_type posn;160 155 161 156 // 'u' can appear before or after length suffix … … 166 161 } else { 167 162 // At least one digit in integer constant, so safe to backup while looking for suffix. 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: ; 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 191 190 } // if 192 191 … … 196 195 if ( type == 5 ) SemanticError( yylloc, "int128 constant is not supported on this target " + str ); 197 196 #endif // ! __SIZEOF_INT128__ 198 197 199 198 if ( str[0] == '0' ) { // radix character ? 200 199 dec = false; … … 206 205 unsigned int len = str.length(); 207 206 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: ; 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 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 ConstantExpr( Constant( new BasicType( noQualifiers, kind[Unsigned][type] ), str, v ) ); 303 ret = new ast::ConstantExpr( location, 304 new ast::BasicType( kind[Unsigned][type] ), str, v ); 304 305 if ( Unsigned && type < 2 ) { // hh or h, less than int ? 305 306 // int i = -1uh => 65535 not -1, so cast is necessary for unsigned, which unfortunately eliminates warnings for large values. 306 ret = new CastExpr( ret, new BasicType( Type::Qualifiers(), kind[Unsigned][type] ), false ); 307 ret = new ast::CastExpr( location, 308 ret, 309 new ast::BasicType( kind[Unsigned][type] ), 310 ast::ExplicitCast ); 307 311 } else if ( ltype != -1 ) { // explicit length ? 308 312 if ( ltype == 6 ) { // int128, (int128)constant 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 ) ); 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 ); 313 325 } else { // explicit length, (length_type)constant 314 ret = new CastExpr( ret, new TypeInstType( Type::Qualifiers(), lnthsInt[Unsigned][ltype], false ), false ); 326 ret = new ast::CastExpr( location, 327 ret, 328 new ast::TypeInstType( lnthsInt[Unsigned][ltype], ast::TypeDecl::Dtype ), 329 ast::ExplicitCast ); 315 330 if ( ltype == 5 ) { // pointer, intptr( (uintptr_t)constant ) 316 ret = build_func( new ExpressionNode( build_varref( new string( "intptr" ) ) ), new ExpressionNode( ret ) ); 331 ret = build_func( location, 332 new ExpressionNode( 333 build_varref( location, new string( "intptr" ) ) ), 334 new ExpressionNode( ret ) ); 317 335 } // if 318 336 } // if … … 358 376 359 377 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 }, 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 }, 364 383 }; 365 384 … … 398 417 399 418 assert( 0 <= type && type < 12 ); 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 ); 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 ); 403 429 } // if 404 430 … … 415 441 } // sepString 416 442 417 Expression * build_constantChar(string & str ) {443 ast::Expr * build_constantChar( const CodeLocation & location, string & str ) { 418 444 string units; // units 419 445 sepString( str, units, '\'' ); // separate constant from units 420 446 421 Expression * ret = new ConstantExpr( Constant( new BasicType( noQualifiers, BasicType::Char ), str, (unsigned long long int)(unsigned char)str[1] ) ); 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] ); 422 451 if ( units.length() != 0 ) { 423 ret = new UntypedExpr( new NameExpr( units ), { ret } ); 452 ret = new ast::UntypedExpr( location, 453 new ast::NameExpr( location, units ), 454 { ret } ); 424 455 } // if 425 456 … … 428 459 } // build_constantChar 429 460 430 Expression * build_constantStr( string & str ) { 461 ast::Expr * build_constantStr( 462 const CodeLocation & location, 463 string & str ) { 431 464 assert( str.length() > 0 ); 432 465 string units; // units 433 466 sepString( str, units, '"' ); // separate constant from units 434 467 435 Type * strtype;468 ast::Type * strtype; 436 469 switch ( str[0] ) { // str has >= 2 characters, i.e, null string "" => safe to look at subscripts 0/1 437 470 case 'u': 438 471 if ( str[1] == '8' ) goto Default; // utf-8 characters => array of char 439 472 // lookup type of associated typedef 440 strtype = new TypeInstType( Type::Qualifiers( ), "char16_t", false );473 strtype = new ast::TypeInstType( "char16_t", ast::TypeDecl::Dtype ); 441 474 break; 442 443 strtype = new TypeInstType( Type::Qualifiers( ), "char32_t", false );475 case 'U': 476 strtype = new ast::TypeInstType( "char32_t", ast::TypeDecl::Dtype ); 444 477 break; 445 446 strtype = new TypeInstType( Type::Qualifiers( ), "wchar_t", false );478 case 'L': 479 strtype = new ast::TypeInstType( "wchar_t", ast::TypeDecl::Dtype ); 447 480 break; 448 449 450 strtype = new BasicType( Type::Qualifiers( ),BasicType::Char );481 Default: // char default string type 482 default: 483 strtype = new ast::BasicType( ast::BasicType::Char ); 451 484 } // switch 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 ) ); 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 ); 456 492 if ( units.length() != 0 ) { 457 ret = new UntypedExpr( new NameExpr( units ), { ret } ); 493 ret = new ast::UntypedExpr( location, 494 new ast::NameExpr( location, units ), 495 { ret } ); 458 496 } // if 459 497 … … 462 500 } // build_constantStr 463 501 464 Expression * build_field_name_FLOATING_FRACTIONconstant( const string & str ) { 502 ast::Expr * build_field_name_FLOATING_FRACTIONconstant( 503 const CodeLocation & location, const string & str ) { 465 504 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) ) ); 505 ast::Expr * ret = build_constantInteger( location, 506 *new string( str.substr(1) ) ); 467 507 delete &str; 468 508 return ret; 469 509 } // build_field_name_FLOATING_FRACTIONconstant 470 510 471 Expression * build_field_name_FLOATING_DECIMALconstant( const string & str ) { 511 ast::Expr * build_field_name_FLOATING_DECIMALconstant( 512 const CodeLocation & location, const string & str ) { 472 513 if ( str[str.size() - 1] != '.' ) SemanticError( yylloc, "invalid tuple index " + str ); 473 Expression * ret = build_constantInteger( *new string( str.substr( 0, str.size()-1 ) ) ); 514 ast::Expr * ret = build_constantInteger( 515 location, *new string( str.substr( 0, str.size()-1 ) ) ); 474 516 delete &str; 475 517 return ret; 476 518 } // build_field_name_FLOATING_DECIMALconstant 477 519 478 Expression * build_field_name_FLOATINGconstant( const string & str ) { 520 ast::Expr * build_field_name_FLOATINGconstant( const CodeLocation & location, 521 const string & str ) { 479 522 // str is of the form A.B -> separate at the . and return member expression 480 523 int a, b; … … 482 525 stringstream ss( str ); 483 526 ss >> a >> dot >> b; 484 UntypedMemberExpr * ret = new UntypedMemberExpr( new ConstantExpr( Constant::from_int( b ) ), new ConstantExpr( Constant::from_int( a ) ) ); 527 auto ret = new ast::UntypedMemberExpr( location, 528 ast::ConstantExpr::from_int( location, b ), 529 ast::ConstantExpr::from_int( location, a ) 530 ); 485 531 delete &str; 486 532 return ret; 487 533 } // build_field_name_FLOATINGconstant 488 534 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; 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 499 548 } // make_field_name_fraction_constants 500 549 501 Expression * build_field_name_fraction_constants( Expression * fieldName, ExpressionNode * fracts ) { 502 return make_field_name_fraction_constants( fieldName, maybeMoveBuild< Expression >( fracts ) ); 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 ) ); 503 554 } // build_field_name_fraction_constants 504 555 505 NameExpr * build_varref( const string * name ) { 506 NameExpr * expr = new NameExpr( *name ); 556 ast::NameExpr * build_varref( const CodeLocation & location, 557 const string * name ) { 558 ast::NameExpr * expr = new ast::NameExpr( location, *name ); 507 559 delete name; 508 560 return expr; 509 561 } // build_varref 510 562 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 ); 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 ); 518 571 } 519 572 } 520 573 } 521 return new QualifiedNameExpr(newDecl, name->name );574 return new ast::QualifiedNameExpr( location, newDecl, name->name ); 522 575 } 523 576 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 ); 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 ); 527 581 } 528 582 529 DimensionExpr * build_dimensionref( const string * name ) { 530 DimensionExpr * expr = new DimensionExpr( *name ); 583 ast::DimensionExpr * build_dimensionref( const CodeLocation & location, 584 const string * name ) { 585 ast::DimensionExpr * expr = new ast::DimensionExpr( location, *name ); 531 586 delete name; 532 587 return expr; … … 544 599 }; // OperName 545 600 546 Expression * build_cast( DeclarationNode * decl_node, ExpressionNode * expr_node ) { 547 Type * targetType = maybeMoveBuildType( decl_node ); 548 if ( dynamic_cast< VoidType * >( targetType ) ) { 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 ) ) { 549 606 delete targetType; 550 return new CastExpr( maybeMoveBuild< Expression >(expr_node), false ); 607 return new ast::CastExpr( location, 608 maybeMoveBuild( expr_node ), 609 ast::ExplicitCast ); 551 610 } else { 552 return new CastExpr( maybeMoveBuild< Expression >(expr_node), targetType, false ); 611 return new ast::CastExpr( location, 612 maybeMoveBuild( expr_node ), 613 targetType, 614 ast::ExplicitCast ); 553 615 } // if 554 616 } // build_cast 555 617 556 Expression * build_keyword_cast( AggregateDecl::Aggregate target, ExpressionNode * expr_node ) { 557 return new KeywordCastExpr( maybeMoveBuild< Expression >(expr_node), target ); 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 ); 558 625 } 559 626 560 Expression * build_virtual_cast( DeclarationNode * decl_node, ExpressionNode * expr_node ) { 561 return new VirtualCastExpr( maybeMoveBuild< Expression >( expr_node ), maybeMoveBuildType( decl_node ) ); 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 ); 562 634 } // build_virtual_cast 563 635 564 Expression * build_fieldSel( ExpressionNode * expr_node, Expression * member ) { 565 return new UntypedMemberExpr( member, maybeMoveBuild< Expression >(expr_node) ); 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 ); 566 643 } // build_fieldSel 567 644 568 Expression * build_pfieldSel( ExpressionNode * expr_node, Expression * member ) { 569 UntypedExpr * deref = new UntypedExpr( new NameExpr( "*?" ) ); 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 ); 570 651 deref->location = expr_node->location; 571 deref-> get_args().push_back( maybeMoveBuild< Expression >(expr_node) );572 UntypedMemberExpr * ret = new UntypedMemberExpr(member, deref );652 deref->args.push_back( maybeMoveBuild( expr_node ) ); 653 auto ret = new ast::UntypedMemberExpr( location, member, deref ); 573 654 return ret; 574 655 } // build_pfieldSel 575 656 576 Expression * build_offsetOf( DeclarationNode * decl_node, NameExpr * member ) { 577 Expression * ret = new UntypedOffsetofExpr( maybeMoveBuildType( decl_node ), member->get_name() ); 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 ); 578 665 delete member; 579 666 return ret; 580 667 } // build_offsetOf 581 668 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 ); 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 ); 584 678 } // build_and_or 585 679 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 ); 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 ); 590 689 } // build_unary_val 591 690 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);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 ); 603 702 } // build_binary_val 604 703 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));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 ); 614 713 } // build_cond 615 714 616 Expression * build_tuple( ExpressionNode * expr_node ) { 617 list< Expression * > exprs; 715 ast::Expr * build_tuple( const CodeLocation & location, 716 ExpressionNode * expr_node ) { 717 std::vector<ast::ptr<ast::Expr>> exprs; 618 718 buildMoveList( expr_node, exprs ); 619 return new UntypedTupleExpr( exprs );;719 return new ast::UntypedTupleExpr( location, std::move( exprs ) ); 620 720 } // build_tuple 621 721 622 Expression * build_func( ExpressionNode * function, ExpressionNode * expr_node ) { 623 list< Expression * > args; 722 ast::Expr * build_func( const CodeLocation & location, 723 ExpressionNode * function, 724 ExpressionNode * expr_node ) { 725 std::vector<ast::ptr<ast::Expr>> args; 624 726 buildMoveList( expr_node, args ); 625 return new UntypedExpr( maybeMoveBuild< Expression >(function), args ); 727 return new ast::UntypedExpr( location, 728 maybeMoveBuild( function ), 729 std::move( args ) 730 ); 626 731 } // build_func 627 732 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) ); 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 ) ); 632 743 // these types do not have associated type information 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) ); 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 ) ); 636 749 } else { 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) ); 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 ) ); 642 759 } else { 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) ); 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 ) ); 648 769 } else { 649 return new CompoundLiteralExpr( new EnumInstType( Type::Qualifiers(), newDeclEnumDecl->get_name() ), maybeMoveBuild< Initializer >(kids) ); 770 return new ast::CompoundLiteralExpr( location, 771 new ast::EnumInstType( newDeclEnumDecl->name ), 772 maybeMoveBuild( kids ) ); 650 773 } // if 651 774 } else { … … 656 779 // Local Variables: // 657 780 // tab-width: 4 // 658 // mode: c++ //659 // compile-command: "make install" //660 781 // End: //
Note:
See TracChangeset
for help on using the changeset viewer.