Changeset 937e51d for src/Parser
- Timestamp:
- Jun 26, 2015, 4:00:26 PM (11 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, string, with_gc
- Children:
- 0df292b, e0ff3e6
- Parents:
- eb50842 (diff), 1869adf (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. - Location:
- src/Parser
- Files:
-
- 4 added
- 1 deleted
- 13 edited
- 1 moved
-
DeclarationNode.cc (modified) (53 diffs)
-
ExpressionNode.cc (modified) (24 diffs)
-
InitializerNode.cc (modified) (2 diffs)
-
ParseNode.cc (modified) (4 diffs)
-
ParseNode.h (modified) (23 diffs)
-
Parser.cc (modified) (2 diffs)
-
StatementNode.cc (modified) (19 diffs)
-
TypeData.cc (modified) (52 diffs)
-
TypeData.h (modified) (3 diffs)
-
TypedefTable.cc (modified) (9 diffs)
-
TypedefTable.h (modified) (3 diffs)
-
cfa.y (deleted)
-
lex.cc (added)
-
lex.h (modified) (4 diffs)
-
lex.ll (moved) (moved from src/Parser/lex.l ) (8 diffs)
-
module.mk (modified) (2 diffs)
-
parser.cc (added)
-
parser.h (added)
-
parser.yy (added)
Legend:
- Unmodified
- Added
- Removed
-
src/Parser/DeclarationNode.cc
reb50842 r937e51d 10 10 // Created On : Sat May 16 12:34:05 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Thu May 21 09:28:54201513 // Update Count : 1312 // Last Modified On : Wed Jun 24 15:29:19 2015 13 // Update Count : 86 14 14 // 15 15 … … 21 21 22 22 #include "TypeData.h" 23 24 #include "SynTree/Declaration.h" 23 25 #include "SynTree/Expression.h" 24 26 27 #include "Parser.h" 28 #include "TypedefTable.h" 29 extern TypedefTable typedefTable; 25 30 26 31 using namespace std; 27 32 28 33 // These must remain in the same order as the corresponding DeclarationNode enumerations. 34 const char *DeclarationNode::storageName[] = { "extern", "static", "auto", "register", "inline", "fortran", "_Noreturn", "_Thread_local", "" }; 29 35 const char *DeclarationNode::qualifierName[] = { "const", "restrict", "volatile", "lvalue", "_Atomic" }; 30 36 const char *DeclarationNode::basicTypeName[] = { "char", "int", "float", "double", "void", "_Bool", "_Complex", "_Imaginary" }; 31 const char *DeclarationNode::modifierName[] = { "signed", "unsigned", "short", "long" };32 const char *DeclarationNode:: tyConName[] = { "struct", "union", "context" };37 const char *DeclarationNode::modifierName[] = { "signed", "unsigned", "short", "long" }; 38 const char *DeclarationNode::aggregateName[] = { "struct", "union", "context" }; 33 39 const char *DeclarationNode::typeClassName[] = { "type", "dtype", "ftype" }; 34 40 … … 63 69 } 64 70 65 const char *storageClassName[] = {66 // order must correspond with DeclarationNode::StorageClass67 "extern",68 "static",69 "auto",70 "register",71 "inline",72 "fortran",73 };74 75 71 void DeclarationNode::print( std::ostream &os, int indent ) const { 76 os << string( indent, ' ' );72 os << string( indent, ' ' ); 77 73 if ( name == "" ) { 78 74 os << "unnamed: "; 79 75 } else { 80 76 os << name << ": "; 81 } 77 } // if 82 78 83 79 if ( linkage != LinkageSpec::Cforall ) { 84 80 os << LinkageSpec::toString( linkage ) << " "; 85 } 86 87 printEnums( storageClasses.begin(), storageClasses.end(), storageClassName, os );81 } // if 82 83 printEnums( storageClasses.begin(), storageClasses.end(), DeclarationNode::storageName, os ); 88 84 if ( type ) { 89 85 type->print( os, indent ); 90 86 } else { 91 87 os << "untyped entity "; 92 } 88 } // if 93 89 94 90 if ( bitfieldWidth ) { 95 os << endl << string( indent+2, ' ') << "with bitfield width ";91 os << endl << string( indent + 2, ' ' ) << "with bitfield width "; 96 92 bitfieldWidth->printOneLine( os ); 97 } 93 } // if 98 94 99 95 if ( initializer != 0 ) { 100 os << endl << string( indent+2, ' ') << "with initializer ";96 os << endl << string( indent + 2, ' ' ) << "with initializer "; 101 97 initializer->printOneLine( os ); 102 } 98 } // if 103 99 104 100 os << endl; … … 109 105 if ( hasEllipsis ) { 110 106 os << string( indent, ' ' ) << "and a variable number of other arguments" << endl; 111 } 107 } // if 112 108 } 113 109 … … 123 119 if ( body ) { 124 120 newnode->type->function->hasBody = true; 125 } 121 } // if 126 122 127 123 if ( ret ) { … … 129 125 ret->type = 0; 130 126 delete ret; 131 } 127 } // if 132 128 133 129 return newnode; … … 141 137 } 142 138 143 DeclarationNode *DeclarationNode::newStorageClass( StorageClass sc ) {139 DeclarationNode *DeclarationNode::newStorageClass( DeclarationNode::StorageClass sc ) { 144 140 DeclarationNode *newnode = new DeclarationNode; 145 141 newnode->storageClasses.push_back( sc ); … … 161 157 } 162 158 163 DeclarationNode *DeclarationNode::newForall( DeclarationNode *forall ) {159 DeclarationNode *DeclarationNode::newForall( DeclarationNode *forall ) { 164 160 DeclarationNode *newnode = new DeclarationNode; 165 161 newnode->type = new TypeData( TypeData::Unknown ); … … 168 164 } 169 165 170 DeclarationNode *DeclarationNode::newFromTypedef( std::string *name ) {166 DeclarationNode *DeclarationNode::newFromTypedef( std::string *name ) { 171 167 DeclarationNode *newnode = new DeclarationNode; 172 168 newnode->type = new TypeData( TypeData::SymbolicInst ); … … 177 173 } 178 174 179 DeclarationNode *DeclarationNode::newAggregate( TyCon kind, std::string*name, DeclarationNode *formals, ExpressionNode *actuals, DeclarationNode *fields ) {175 DeclarationNode *DeclarationNode::newAggregate( Aggregate kind, std::string *name, DeclarationNode *formals, ExpressionNode *actuals, DeclarationNode *fields ) { 180 176 DeclarationNode *newnode = new DeclarationNode; 181 177 newnode->type = new TypeData( TypeData::Aggregate ); … … 184 180 if ( newnode->type->aggregate->name == "" ) { 185 181 newnode->type->aggregate->name = DeclarationNode::anonymous.newName(); 186 } 182 } // if 183 184 // SKULLDUGGERY: generate a typedef for the aggregate name so that the aggregate does not have to be qualified by 185 // "struct" 186 typedefTable.addToEnclosingScope( newnode->type->aggregate->name, TypedefTable::TD ); 187 DeclarationNode *typedf = new DeclarationNode; 188 typedf->name = newnode->type->aggregate->name; 189 newnode->appendList( typedf->addType( newnode->clone() )->addTypedef() ); 190 187 191 newnode->type->aggregate->params = formals; 188 192 newnode->type->aggregate->actuals = actuals; … … 198 202 if ( newnode->type->enumeration->name == "" ) { 199 203 newnode->type->enumeration->name = DeclarationNode::anonymous.newName(); 200 } 204 } // if 205 206 // SKULLDUGGERY: generate a typedef for the enumeration name so that the enumeration does not have to be qualified 207 // by "enum" 208 typedefTable.addToEnclosingScope( newnode->type->enumeration->name, TypedefTable::TD ); 209 DeclarationNode *typedf = new DeclarationNode; 210 typedf->name = newnode->type->enumeration->name; 211 newnode->appendList( typedf->addType( newnode->clone() )->addTypedef() ); 212 201 213 newnode->type->enumeration->constants = constants; 202 214 return newnode; 203 215 } 204 216 205 DeclarationNode *DeclarationNode::newEnumConstant( std::string *name, ExpressionNode *constant ) {217 DeclarationNode *DeclarationNode::newEnumConstant( std::string *name, ExpressionNode *constant ) { 206 218 DeclarationNode *newnode = new DeclarationNode; 207 219 newnode->name = assign_strptr( name ); … … 210 222 } 211 223 212 DeclarationNode *DeclarationNode::newName( std::string *name ) {224 DeclarationNode *DeclarationNode::newName( std::string *name ) { 213 225 DeclarationNode *newnode = new DeclarationNode; 214 226 newnode->name = assign_strptr( name ); … … 216 228 } 217 229 218 DeclarationNode *DeclarationNode::newFromTypeGen( std::string *name, ExpressionNode *params ) {230 DeclarationNode *DeclarationNode::newFromTypeGen( std::string *name, ExpressionNode *params ) { 219 231 DeclarationNode *newnode = new DeclarationNode; 220 232 newnode->type = new TypeData( TypeData::SymbolicInst ); … … 225 237 } 226 238 227 DeclarationNode *DeclarationNode::newTypeParam( TypeClass tc, std::string *name ) {239 DeclarationNode *DeclarationNode::newTypeParam( TypeClass tc, std::string *name ) { 228 240 DeclarationNode *newnode = new DeclarationNode; 229 241 newnode->name = assign_strptr( name ); … … 331 343 } else { 332 344 dst->forall = src->forall; 333 } 345 } // if 334 346 src->forall = 0; 335 } 347 } // if 336 348 if ( dst->base ) { 337 349 addQualifiersToType( src, dst->base ); … … 341 353 } else { 342 354 dst->qualifiers.splice( dst->qualifiers.end(), src->qualifiers ); 343 } 344 } 355 } // if 356 } // if 345 357 } 346 358 … … 351 363 if ( ! type ) { 352 364 type = new TypeData; 353 } 365 } // if 354 366 addQualifiersToType( q->type, type ); 355 367 if ( q->type && q->type->forall ) { … … 357 369 type->forall->appendList( q->type->forall ); 358 370 } else { 359 type->forall = q->type->forall; 360 } 371 if ( type->kind == TypeData::Aggregate ) { 372 type->aggregate->params = q->type->forall; 373 } else { 374 type->forall = q->type->forall; 375 } // if 376 } // if 361 377 q->type->forall = 0; 362 } 363 } 364 } 378 } // if 379 } // if 380 } // if 365 381 delete q; 366 382 return this; … … 379 395 } else { 380 396 dst->forall = src->forall; 381 } 397 } // if 382 398 src->forall = 0; 383 } 399 } // if 384 400 if ( dst->base ) { 385 401 addTypeToType( src, dst->base ); … … 398 414 dst->basic->modifiers.splice( dst->basic->modifiers.end(), src->basic->modifiers ); 399 415 dst->basic->typeSpec.splice( dst->basic->typeSpec.end(), src->basic->typeSpec ); 400 } 416 } // if 401 417 break; 402 418 … … 409 425 if ( src->kind == TypeData::Aggregate ) { 410 426 dst->base->aggInst->params = maybeClone( src->aggregate->actuals ); 411 } 427 } // if 412 428 dst->base->qualifiers.splice( dst->base->qualifiers.end(), src->qualifiers ); 413 429 src = 0; … … 419 435 } else { 420 436 dst->forall = src->forall; 421 } 437 } // if 422 438 src->forall = 0; 423 439 dst->base = src; 424 440 src = 0; 425 } 426 } 427 } 428 } 441 } // switch 442 } // switch 443 } // if 444 } // if 429 445 } 430 446 … … 439 455 if ( o->type->kind == TypeData::Aggregate ) { 440 456 type->aggInst->params = maybeClone( o->type->aggregate->actuals ); 441 } 457 } // if 442 458 type->qualifiers.splice( type->qualifiers.end(), o->type->qualifiers ); 443 459 } else { 444 460 type = o->type; 445 } 461 } // if 446 462 o->type = 0; 447 463 } else { 448 464 addTypeToType( o->type, type ); 449 } 450 } 465 } // if 466 } // if 451 467 if ( o->bitfieldWidth ) { 452 468 bitfieldWidth = o->bitfieldWidth; 453 } 454 } 469 } // if 470 } // if 455 471 delete o; 456 472 return this; … … 467 483 } 468 484 469 DeclarationNode *DeclarationNode::addAssertions( DeclarationNode *assertions ) {485 DeclarationNode *DeclarationNode::addAssertions( DeclarationNode *assertions ) { 470 486 assert( type ); 471 487 switch ( type->kind ) { … … 475 491 } else { 476 492 type->symbolic->assertions = assertions; 477 } 493 } // if 478 494 break; 479 480 495 case TypeData::Variable: 481 496 if ( type->variable->assertions ) { … … 483 498 } else { 484 499 type->variable->assertions = assertions; 485 } 500 } // if 486 501 break; 487 488 502 default: 489 503 assert( false ); 490 } 504 } // switch 491 505 492 506 return this; 493 507 } 494 508 495 DeclarationNode *DeclarationNode::addName( std::string *newname ) {509 DeclarationNode *DeclarationNode::addName( std::string *newname ) { 496 510 name = assign_strptr( newname ); 497 511 return this; … … 526 540 } 527 541 528 static void 529 setBase( TypeData *&type, TypeData *newType ) { 542 static void setBase( TypeData *&type, TypeData *newType ) { 530 543 if ( type ) { 531 544 TypeData *prevBase = type; … … 534 547 prevBase = curBase; 535 548 curBase = curBase->base; 536 } 549 } // while 537 550 prevBase->base = newType; 538 551 } else { 539 552 type = newType; 540 } 553 } // if 541 554 } 542 555 … … 547 560 p->type = 0; 548 561 delete p; 549 } 562 } // if 550 563 return this; 551 564 } … … 557 570 a->type = 0; 558 571 delete a; 559 } 572 } // if 560 573 return this; 561 574 } … … 572 585 if ( type->kind == TypeData::Aggregate ) { 573 586 p->type->base->aggInst->params = maybeClone( type->aggregate->actuals ); 574 } 587 } // if 575 588 p->type->base->qualifiers.splice( p->type->base->qualifiers.end(), type->qualifiers ); 576 589 break; … … 578 591 default: 579 592 p->type->base = type; 580 } 593 } // switch 581 594 type = 0; 582 } 595 } // if 583 596 delete this; 584 597 return p; 585 598 } else { 586 599 return this; 587 } 600 } // if 588 601 } 589 602 … … 593 606 while ( cur->base ) { 594 607 cur = cur->base; 595 } 608 } // while 596 609 return cur; 597 610 } … … 609 622 if ( type->kind == TypeData::Aggregate ) { 610 623 lastArray->base->aggInst->params = maybeClone( type->aggregate->actuals ); 611 } 624 } // if 612 625 lastArray->base->qualifiers.splice( lastArray->base->qualifiers.end(), type->qualifiers ); 613 626 break; 614 627 default: 615 628 lastArray->base = type; 616 } 629 } // switch 617 630 type = 0; 618 } 631 } // if 619 632 delete this; 620 633 return a; 621 634 } else { 622 635 return this; 623 } 636 } // if 624 637 } 625 638 … … 637 650 } else { 638 651 type->function->idList = ids; 639 } 652 } // if 640 653 return type; 641 654 } else { … … 643 656 newtype->function->idList = ids; 644 657 return newtype; 645 } 658 } // if 646 659 } 647 660 … … 662 675 while ( srcType->base ) { 663 676 srcType = srcType->base; 664 } 677 } // while 665 678 newnode->type = maybeClone( srcType ); 666 679 if ( newnode->type->kind == TypeData::AggregateInst ) { … … 673 686 delete newnode->type->aggInst->aggregate->aggregate->members; 674 687 newnode->type->aggInst->aggregate->aggregate->members = 0; 675 } 676 } 688 } // if 689 } // if 677 690 newnode->type->forall = maybeClone( type->forall ); 678 691 newnode->storageClasses = storageClasses; … … 688 701 while ( srcType->base ) { 689 702 srcType = srcType->base; 690 } 703 } // while 691 704 TypeData *newType = srcType->clone(); 692 705 if ( newType->kind == TypeData::AggregateInst ) { … … 699 712 delete newType->aggInst->aggregate->aggregate->members; 700 713 newType->aggInst->aggregate->aggregate->members = 0; 701 } 702 } 714 } // if 715 } // if 703 716 newType->forall = maybeClone( type->forall ); 704 717 if ( ! o->type ) { … … 707 720 addTypeToType( newType, o->type ); 708 721 delete newType; 709 } 710 } 711 } 722 } // if 723 } // if 724 } // if 712 725 return o; 713 726 } … … 731 744 addTypeToType( newType, o->type ); 732 745 delete newType; 733 } 734 } 735 } 746 } // if 747 } // if 748 } // if 736 749 return o; 737 750 } … … 740 753 if ( node != 0 ) { 741 754 set_link( node ); 742 } 755 } // if 743 756 return this; 744 757 } … … 756 769 } 757 770 758 void buildList( const DeclarationNode *firstNode, std::list< Declaration * > &outputList ) {771 void buildList( const DeclarationNode *firstNode, std::list< Declaration * > &outputList ) { 759 772 SemanticError errors; 760 std::back_insert_iterator< std::list< Declaration *> > out( outputList );773 std::back_insert_iterator< std::list< Declaration *> > out( outputList ); 761 774 const DeclarationNode *cur = firstNode; 762 775 while ( cur ) { … … 776 789 errors.append( e ); 777 790 } // try 778 cur = dynamic_cast< DeclarationNode *>( cur->get_link() );791 cur = dynamic_cast< DeclarationNode *>( cur->get_link() ); 779 792 } // while 780 793 if ( ! errors.isEmpty() ) { … … 783 796 } 784 797 785 void buildList( const DeclarationNode *firstNode, std::list< DeclarationWithType *> &outputList ) {798 void buildList( const DeclarationNode *firstNode, std::list< DeclarationWithType *> &outputList ) { 786 799 SemanticError errors; 787 std::back_insert_iterator< std::list< DeclarationWithType *> > out( outputList );800 std::back_insert_iterator< std::list< DeclarationWithType *> > out( outputList ); 788 801 const DeclarationNode *cur = firstNode; 789 802 while ( cur ) { … … 799 812 Declaration *decl = cur->build(); 800 813 if ( decl ) { 801 if ( DeclarationWithType *dwt = dynamic_cast< DeclarationWithType *>( decl ) ) {814 if ( DeclarationWithType *dwt = dynamic_cast< DeclarationWithType *>( decl ) ) { 802 815 *out++ = dwt; 803 } else if ( StructDecl *agg = dynamic_cast< StructDecl *>( decl ) ) {816 } else if ( StructDecl *agg = dynamic_cast< StructDecl *>( decl ) ) { 804 817 StructInstType *inst = new StructInstType( Type::Qualifiers(), agg->get_name() ); 805 *out++ = new ObjectDecl( "", Declaration ::NoStorageClass, linkage, 0, inst, 0 );818 *out++ = new ObjectDecl( "", DeclarationNode::NoStorageClass, linkage, 0, inst, 0 ); 806 819 delete agg; 807 } else if ( UnionDecl *agg = dynamic_cast< UnionDecl *>( decl ) ) {820 } else if ( UnionDecl *agg = dynamic_cast< UnionDecl *>( decl ) ) { 808 821 UnionInstType *inst = new UnionInstType( Type::Qualifiers(), agg->get_name() ); 809 *out++ = new ObjectDecl( "", Declaration ::NoStorageClass, linkage, 0, inst, 0 );822 *out++ = new ObjectDecl( "", DeclarationNode::NoStorageClass, linkage, 0, inst, 0 ); 810 823 } // if 811 824 } // if … … 813 826 errors.append( e ); 814 827 } // try 815 cur = dynamic_cast< DeclarationNode *>( cur->get_link() );828 cur = dynamic_cast< DeclarationNode *>( cur->get_link() ); 816 829 } // while 817 830 if ( ! errors.isEmpty() ) { … … 820 833 } 821 834 822 void buildTypeList( const DeclarationNode *firstNode, std::list< Type *> &outputList ) {835 void buildTypeList( const DeclarationNode *firstNode, std::list< Type *> &outputList ) { 823 836 SemanticError errors; 824 std::back_insert_iterator< std::list< Type *> > out( outputList );837 std::back_insert_iterator< std::list< Type *> > out( outputList ); 825 838 const DeclarationNode *cur = firstNode; 826 839 while ( cur ) { … … 830 843 errors.append( e ); 831 844 } // try 832 cur = dynamic_cast< DeclarationNode *>( cur->get_link() );845 cur = dynamic_cast< DeclarationNode *>( cur->get_link() ); 833 846 } // while 834 847 if ( ! errors.isEmpty() ) { … … 839 852 Declaration *DeclarationNode::build() const { 840 853 if ( type ) { 841 Declaration *newDecl = type->buildDecl( name, buildStorageClass(), maybeBuild< Expression >( bitfieldWidth ), build Inline(), linkage, maybeBuild< Initializer >(initializer) );854 Declaration *newDecl = type->buildDecl( name, buildStorageClass(), maybeBuild< Expression >( bitfieldWidth ), buildFuncSpecifier( Inline ), buildFuncSpecifier( Noreturn ), linkage, maybeBuild< Initializer >(initializer) ); 842 855 return newDecl; 843 856 } // if 844 if ( ! build Inline() ) {857 if ( ! buildFuncSpecifier( Inline ) && ! buildFuncSpecifier( Noreturn ) ) { 845 858 return new ObjectDecl( name, buildStorageClass(), linkage, maybeBuild< Expression >( bitfieldWidth ), 0, maybeBuild< Initializer >( initializer ) ); 846 859 } // if 847 throw SemanticError( "invalid inline specificationin declaration of ", this );860 throw SemanticError( "invalid function specifier in declaration of ", this ); 848 861 } 849 862 … … 882 895 } 883 896 884 Declaration::StorageClass DeclarationNode::buildStorageClass() const { 885 static const Declaration::StorageClass scMap[] = { 886 Declaration::Extern, 887 Declaration::Static, 888 Declaration::Auto, 889 Declaration::Register, 890 Declaration::Inline, 891 Declaration::Fortran 892 }; 893 894 Declaration::StorageClass ret = Declaration::NoStorageClass; 895 for ( std::list< StorageClass >::const_iterator i = storageClasses.begin(); i != storageClasses.end(); ++i ) { 896 assert( unsigned( *i ) < sizeof( scMap ) / sizeof( scMap[0] ) ); 897 if ( *i == Inline ) continue; 898 if ( ret != Declaration::NoStorageClass ) { 897 DeclarationNode::StorageClass DeclarationNode::buildStorageClass() const { 898 DeclarationNode::StorageClass ret = DeclarationNode::NoStorageClass; 899 for ( std::list< DeclarationNode::StorageClass >::const_iterator i = storageClasses.begin(); i != storageClasses.end(); ++i ) { 900 if ( *i == DeclarationNode::Inline || *i == DeclarationNode::Noreturn ) continue; // ignore function specifiers 901 if ( ret != DeclarationNode::NoStorageClass ) { // already have a valid storage class ? 899 902 throw SemanticError( "invalid combination of storage classes in declaration of ", this ); 900 } 901 ret = scMap[ *i ];902 } 903 } // if 904 ret = *i; 905 } // for 903 906 return ret; 904 907 } 905 908 906 bool DeclarationNode::build Inline() const {907 std::list< StorageClass >::const_iterator first = std::find( storageClasses.begin(), storageClasses.end(), Inline);908 if ( first == storageClasses.end() ) return false; 909 std::list< StorageClass >::const_iterator next = std::find( ++first, storageClasses.end(), Inline );910 if ( next == storageClasses.end() ) return true;911 throw SemanticError( "duplicate inline specificationin declaration of ", this );909 bool DeclarationNode::buildFuncSpecifier( DeclarationNode::StorageClass key ) const { 910 std::list< DeclarationNode::StorageClass >::const_iterator first = std::find( storageClasses.begin(), storageClasses.end(), key ); 911 if ( first == storageClasses.end() ) return false; // not found 912 first = std::find( ++first, storageClasses.end(), key ); // found 913 if ( first == storageClasses.end() ) return true; // not found again 914 throw SemanticError( "duplicate function specifier in declaration of ", this ); 912 915 } 913 916 -
src/Parser/ExpressionNode.cc
reb50842 r937e51d 9 9 // Author : Rodolfo G. Esteves 10 10 // Created On : Sat May 16 13:17:07 2015 11 // Last Modified By : Peter A. Buhr12 // Last Modified On : Sat May 16 13:19:35201513 // Update Count : 211 // Last Modified By : Rob Schluntz 12 // Last Modified On : Wed Jun 24 16:20:00 2015 13 // Update Count : 158 14 14 // 15 15 … … 17 17 #include <cctype> 18 18 #include <algorithm> 19 #include <sstream> 20 #include <cstdio> 21 #include <climits> 19 22 20 23 #include "ParseNode.h" 21 #include "SynTree/Type.h"22 24 #include "SynTree/Constant.h" 23 25 #include "SynTree/Expression.h" 24 #include "SynTree/Declaration.h"25 26 #include "UnimplementedError.h" 26 27 #include "parseutility.h" … … 31 32 ExpressionNode::ExpressionNode() : ParseNode(), argName( 0 ) {} 32 33 33 ExpressionNode::ExpressionNode( string *name_) : ParseNode( *name_ ), argName( 0 ) { 34 delete name_; 35 } 34 ExpressionNode::ExpressionNode( const string *name_ ) : ParseNode( name_ ), argName( 0 ) {} 36 35 37 36 ExpressionNode::ExpressionNode( const ExpressionNode &other ) : ParseNode( other.name ) { … … 43 42 } 44 43 45 ExpressionNode * ExpressionNode::set_asArgName( std::string *aName ) {44 ExpressionNode * ExpressionNode::set_asArgName( const std::string *aName ) { 46 45 argName = new VarRefNode( aName ); 47 46 return this; … … 55 54 void ExpressionNode::printDesignation( std::ostream &os, int indent ) const { 56 55 if ( argName ) { 57 os << string( ' ', indent) << "(designated by: ";56 os << string( indent, ' ' ) << "(designated by: "; 58 57 argName->printOneLine( os, indent ); 59 58 os << ")" << std::endl; … … 61 60 } 62 61 62 //############################################################################## 63 63 64 NullExprNode::NullExprNode() {} 64 65 … … 85 86 } 86 87 87 // enum ConstantNode::Type = { Integer, Float, Character, String, Range } 88 89 ConstantNode::ConstantNode( void ) : ExpressionNode(), sign( true ), longs(0), size(0) {} 90 91 ConstantNode::ConstantNode( string *name_) : ExpressionNode( name_), sign( true ), longs(0), size(0) {} 92 93 ConstantNode::ConstantNode( Type t, string *inVal ) : type( t ), sign( true ), longs(0), size(0) { 94 if ( inVal ) { 95 value = *inVal; 96 delete inVal; 97 } else { 98 value = ""; 99 } // if 100 101 classify( value ); 102 } 103 104 ConstantNode::ConstantNode( const ConstantNode &other ) : ExpressionNode( other ), type( other.type ), value( other.value ), sign( other.sign ), 105 base( other.base ), longs( other.longs ), size( other.size ) { 106 } 107 108 // for some reason, std::tolower doesn't work as an argument to std::transform in g++ 3.1 109 inline char tolower_hack( char c ) { 110 return std::tolower( c ); 111 } 112 113 void ConstantNode::classify( std::string &str ) { 88 //############################################################################## 89 90 static inline bool checkU( char c ) { return c == 'u' || c == 'U'; } 91 static inline bool checkL( char c ) { return c == 'l' || c == 'L'; } 92 static inline bool checkF( char c ) { return c == 'f' || c == 'F'; } 93 static inline bool checkX( char c ) { return c == 'x' || c == 'X'; } 94 95 // Difficult to separate extra parts of constants during lexing because actions are not allow in the middle of patterns: 96 // 97 // prefix action constant action suffix 98 // 99 // Alternatively, breaking a pattern using BEGIN does not work if the following pattern can be empty: 100 // 101 // constant BEGIN CONT ... 102 // <CONT>(...)? BEGIN 0 ... // possible empty suffix 103 // 104 // because the CONT rule is NOT triggered if the pattern is empty. Hence, constants are reparsed here to determine their 105 // type. 106 107 ConstantNode::ConstantNode( Type t, string *inVal ) : type( t ), value( *inVal ) { 108 // lexing divides constants into 4 kinds 109 switch ( type ) { 110 case Integer: 111 { 112 static const BasicType::Kind kind[2][3] = { 113 { BasicType::SignedInt, BasicType::LongSignedInt, BasicType::LongLongSignedInt }, 114 { BasicType::UnsignedInt, BasicType::LongUnsignedInt, BasicType::LongLongUnsignedInt }, 115 }; 116 size_t last = value.length() - 1; // last character of constant 117 unsigned long long v; // converted integral value 118 bool dec = true, Unsigned = false; // decimal, unsigned constant 119 int size; // 0 => int, 1 => long, 2 => long long 120 121 if ( value[0] == '0' ) { // octal constant ? 122 dec = false; 123 if ( last != 0 && checkX( value[1] ) ) { // hex constant ? 124 sscanf( (char *)value.c_str(), "%llx", &v ); 125 //printf( "%llx %llu\n", v, v ); 126 } else { 127 sscanf( (char *)value.c_str(), "%llo", &v ); 128 //printf( "%llo %llu\n", v, v ); 129 } // if 130 } else { // decimal constant ? 131 sscanf( (char *)value.c_str(), "%llu", &v ); 132 //printf( "%llu %llu\n", v, v ); 133 } // if 134 135 if ( v <= INT_MAX ) { // signed int 136 size = 0; 137 } else if ( v <= UINT_MAX && ! dec ) { // unsigned int 138 size = 0; 139 Unsigned = true; // unsigned 140 } else if ( v <= LONG_MAX ) { // signed long int 141 size = 1; 142 } else if ( v <= ULONG_MAX && ( ! dec || LONG_MAX == LLONG_MAX ) ) { // signed long int 143 size = 1; 144 Unsigned = true; // unsigned long int 145 } else if ( v <= LLONG_MAX ) { // signed long long int 146 size = 2; 147 } else { // unsigned long long int 148 size = 2; 149 Unsigned = true; // unsigned long long int 150 } // if 151 152 if ( checkU( value[last] ) ) { // suffix 'u' ? 153 Unsigned = true; 154 if ( last > 0 && checkL( value[ last - 1 ] ) ) { // suffix 'l' ? 155 size = 1; 156 if ( last > 1 && checkL( value[ last - 2 ] ) ) { // suffix 'll' ? 157 size = 2; 158 } // if 159 } // if 160 } else if ( checkL( value[ last ] ) ) { // suffix 'l' ? 161 size = 1; 162 if ( last > 0 && checkL( value[ last - 1 ] ) ) { // suffix 'll' ? 163 size = 2; 164 if ( last > 1 && checkU( value[ last - 2 ] ) ) { // suffix 'u' ? 165 Unsigned = true; 166 } // if 167 } else { 168 if ( last > 0 && checkU( value[ last - 1 ] ) ) { // suffix 'u' ? 169 Unsigned = true; 170 } // if 171 } // if 172 } // if 173 btype = kind[Unsigned][size]; // lookup constant type 174 break; 175 } 176 case Float: 177 { 178 size_t len = value.length() - 1; 179 180 btype = BasicType::Double; // default 181 if ( checkF( value[len] ) ) { // float ? 182 btype = BasicType::Float; 183 } // if 184 if ( checkL( value[len] ) ) { // long double ? 185 btype = BasicType::LongDouble; 186 } // if 187 break; 188 } 189 case Character: 190 btype = BasicType::Char; // default 191 if ( string( "LUu" ).find( value[0] ) != string::npos ) { 192 // ??? 193 } // if 194 break; 195 case String: 196 // array of char 197 if ( string( "LUu" ).find( value[0] ) != string::npos ) { 198 if ( value[0] == 'u' && value[1] == '8' ) { 199 // ??? 200 } else { 201 // ??? 202 } // if 203 } // if 204 break; 205 } // switch 206 } // ConstantNode::ConstantNode 207 208 ConstantNode *ConstantNode::appendstr( const std::string *newValue ) { 209 assert( newValue != 0 ); 210 assert( type == String ); 211 212 // "abc" "def" "ghi" => "abcdefghi", so remove new text from quotes and insert before last quote in old string. 213 value.insert( value.length() - 1, newValue->substr( 1, newValue->length() - 2 ) ); 214 215 delete newValue; // allocated by lexer 216 return this; 217 } 218 219 void ConstantNode::printOneLine( std::ostream &os, int indent ) const { 220 os << string( indent, ' ' ); 221 printDesignation( os ); 222 114 223 switch ( type ) { 115 224 case Integer: 116 225 case Float: 117 {118 std::string sfx("");119 char c;120 int i = str.length() - 1;121 122 while ( i >= 0 && ! isxdigit( c = str.at( i--)) )123 sfx += c;124 125 value = str.substr( 0, i + 2 );126 127 // get rid of underscores128 value.erase( remove( value.begin(), value.end(), '_'), value.end());129 130 std::transform( sfx.begin(), sfx.end(), sfx.begin(), tolower_hack );131 132 if ( sfx.find("ll") != string::npos ) {133 longs = 2;134 } else if ( sfx.find("l") != string::npos ) {135 longs = 1;136 } // if137 138 assert(( longs >= 0) && ( longs <= 2));139 140 if ( sfx.find("u") != string::npos )141 sign = false;142 143 break;144 }145 case Character:146 {147 // remove underscores from hex and oct escapes148 if ( str.substr(1,2) == "\\x")149 value.erase( remove( value.begin(), value.end(), '_'), value.end());150 151 break;152 }153 default:154 // shouldn't be here155 ;156 }157 }158 159 ConstantNode::Type ConstantNode::get_type( void ) const {160 return type;161 }162 163 ConstantNode *ConstantNode::append( std::string *newValue ) {164 if ( newValue ) {165 if ( type == String ) {166 std::string temp = *newValue;167 value.resize( value.size() - 1 );168 value += newValue->substr(1, newValue->size());169 } else170 value += *newValue;171 172 delete newValue;173 } // if174 return this;175 }176 177 void ConstantNode::printOneLine( std::ostream &os, int indent ) const {178 os << string( indent, ' ');179 printDesignation( os );180 181 switch ( type ) {182 /* integers */183 case Integer:184 226 os << value ; 185 227 break; 186 case Float:187 os << value ;188 break;189 190 228 case Character: 191 229 os << "'" << value << "'"; 192 230 break; 193 194 231 case String: 195 232 os << '"' << value << '"'; 196 233 break; 197 } 234 } // switch 198 235 199 236 os << ' '; … … 206 243 207 244 Expression *ConstantNode::build() const { 208 ::Type::Qualifiers q; 209 BasicType *bt; 210 211 switch ( get_type()) { 212 case Integer: 213 /* Cfr. standard 6.4.4.1 */ 214 //bt.set_kind( BasicType::SignedInt ); 215 bt = new BasicType( q, BasicType::SignedInt ); 216 break; 217 case Float: 218 bt = new BasicType( q, BasicType::Float ); 219 break; 220 case Character: 221 bt = new BasicType( q, BasicType::Char ); 222 break; 245 ::Type::Qualifiers q; // no qualifiers on constants 246 247 switch ( get_type() ) { 223 248 case String: 224 // string should probably be a primitive type 225 ArrayType *at; 226 std::string value = get_value(); 227 at = new ArrayType( q, new BasicType( q, BasicType::Char ), 228 new ConstantExpr( Constant( new BasicType( q, BasicType::SignedInt ), 229 toString( value.size() - 1 ) ) ), // account for '\0' 230 false, false ); 231 return new ConstantExpr( Constant( at, value ), maybeBuild< Expression >( get_argName() ) ); 249 { 250 // string should probably be a primitive type 251 ArrayType *at = new ArrayType( q, new BasicType( q, BasicType::Char ), 252 new ConstantExpr( 253 Constant( new BasicType( q, BasicType::UnsignedInt ), 254 toString( value.size()+1-2 ) ) ), // +1 for '\0' and -2 for '"' 255 false, false ); 256 return new ConstantExpr( Constant( at, value ), maybeBuild< Expression >( get_argName() ) ); 257 } 258 default: 259 return new ConstantExpr( Constant( new BasicType( q, btype ), get_value() ), maybeBuild< Expression >( get_argName() ) ); 232 260 } 233 return new ConstantExpr( Constant( bt, get_value()), maybeBuild< Expression >( get_argName() ) ); 234 } 261 } 262 263 //############################################################################## 235 264 236 265 VarRefNode::VarRefNode() : isLabel( false ) {} 237 266 238 VarRefNode::VarRefNode( string *name_, bool labelp ) : ExpressionNode( name_), isLabel( labelp ) {}267 VarRefNode::VarRefNode( const string *name_, bool labelp ) : ExpressionNode( name_ ), isLabel( labelp ) {} 239 268 240 269 VarRefNode::VarRefNode( const VarRefNode &other ) : ExpressionNode( other ), isLabel( other.isLabel ) { … … 252 281 void VarRefNode::print( std::ostream &os, int indent ) const { 253 282 printDesignation( os ); 254 os << '\r' << string( indent, ' ') << "Referencing: ";283 os << string( indent, ' ' ) << "Referencing: "; 255 284 os << "Variable: " << get_name(); 256 285 os << endl; 257 286 } 258 287 288 //############################################################################## 289 259 290 OperatorNode::OperatorNode( Type t ) : type( t ) {} 260 291 … … 275 306 void OperatorNode::print( std::ostream &os, int indent ) const{ 276 307 printDesignation( os ); 277 os << '\r' << string( indent, ' ') << "Operator: " << OpName[type] << endl;308 os << string( indent, ' ' ) << "Operator: " << OpName[type] << endl; 278 309 return; 279 310 } 280 311 281 std::stringOperatorNode::get_typename( void ) const{282 return string( OpName[ type ]);312 const char *OperatorNode::get_typename( void ) const{ 313 return OpName[ type ]; 283 314 } 284 315 … … 288 319 "Cond", "NCond", 289 320 // diadic 290 "SizeOf", "AlignOf", "Attr", "CompLit", "Plus", "Minus", "Mul", "Div", "Mod", "Or",321 "SizeOf", "AlignOf", "Attr", "CompLit", "Plus", "Minus", "Mul", "Div", "Mod", "Or", 291 322 "And", "BitOr", "BitAnd", "Xor", "Cast", "LShift", "RShift", "LThan", "GThan", 292 323 "LEThan", "GEThan", "Eq", "Neq", "Assign", "MulAssn", "DivAssn", "ModAssn", "PlusAssn", … … 297 328 }; 298 329 330 //############################################################################## 331 299 332 CompositeExprNode::CompositeExprNode( void ) : ExpressionNode(), function( 0 ), arguments( 0 ) { 300 333 } 301 334 302 CompositeExprNode::CompositeExprNode( string *name_) : ExpressionNode( name_), function( 0 ), arguments( 0 ) {335 CompositeExprNode::CompositeExprNode( const string *name_ ) : ExpressionNode( name_ ), function( 0 ), arguments( 0 ) { 303 336 } 304 337 … … 331 364 // the names that users use to define operator functions 332 365 static const char *opFuncName[] = { 333 "", "","",334 "", "",335 // diadic336 "", "", "", "", "?+?", "?-?", "?*?", "?/?", "?%?", "","",337 "?|?", "?&?", "?^?", "", "?<<?", "?>>?", "?<?", "?>?","?<=?",338 "?>=?", "?==?", "?!=?", "?=?", "?*=?", "?/=?", "?%=?", "?+=?","?-=?",339 "?<<=?", "?>>=?", "?&=?", "?^=?", "?|=?", "?[?]", "","","Range",340 // monadic341 "+?", "-?", "", "*?", "!?", "~?", "++?", "?++", "--?", "?--", "LabAddress"366 "", "", "", 367 "", "", 368 //diadic 369 "", "", "", "", "?+?", "?-?", "?*?", "?/?", "?%?", "", "", 370 "?|?", "?&?", "?^?", "", "?<<?", "?>>?", "?<?", "?>?", "?<=?", 371 "?>=?", "?==?", "?!=?", "?=?", "?*=?", "?/=?", "?%=?", "?+=?", "?-=?", 372 "?<<=?", "?>>=?", "?&=?", "?^=?", "?|=?", "?[?]", "", "", "Range", 373 //monadic 374 "+?", "-?", "", "*?", "!?", "~?", "++?", "?++", "--?", "?--", "&&" 342 375 }; 343 376 … … 350 383 buildList( get_args(), args ); 351 384 352 if ( ! ( op = dynamic_cast<OperatorNode *>( function )) ) { 353 // a function as opposed to an operator 385 if ( ! ( op = dynamic_cast<OperatorNode *>( function ) ) ) { // function as opposed to operator 354 386 return new UntypedExpr( function->build(), args, maybeBuild< Expression >( get_argName() )); 355 } else { 356 switch ( op->get_type()) { 357 case OperatorNode::Incr: 358 case OperatorNode::Decr: 359 case OperatorNode::IncrPost: 360 case OperatorNode::DecrPost: 361 case OperatorNode::Assign: 362 case OperatorNode::MulAssn: 363 case OperatorNode::DivAssn: 364 case OperatorNode::ModAssn: 365 case OperatorNode::PlusAssn: 366 case OperatorNode::MinusAssn: 367 case OperatorNode::LSAssn: 368 case OperatorNode::RSAssn: 369 case OperatorNode::AndAssn: 370 case OperatorNode::ERAssn: 371 case OperatorNode::OrAssn: 372 // the rewrite rules for these expressions specify that the first argument has its address taken 373 assert( ! args.empty() ); 374 args.front() = new AddressExpr( args.front() ); 375 break; 376 default: 377 /* do nothing */ 378 ; 379 } 380 381 switch ( op->get_type() ) { 382 case OperatorNode::Incr: 383 case OperatorNode::Decr: 384 case OperatorNode::IncrPost: 385 case OperatorNode::DecrPost: 386 case OperatorNode::Assign: 387 case OperatorNode::MulAssn: 388 case OperatorNode::DivAssn: 389 case OperatorNode::ModAssn: 390 case OperatorNode::PlusAssn: 391 case OperatorNode::MinusAssn: 392 case OperatorNode::LSAssn: 393 case OperatorNode::RSAssn: 394 case OperatorNode::AndAssn: 395 case OperatorNode::ERAssn: 396 case OperatorNode::OrAssn: 397 case OperatorNode::Plus: 398 case OperatorNode::Minus: 399 case OperatorNode::Mul: 400 case OperatorNode::Div: 401 case OperatorNode::Mod: 402 case OperatorNode::BitOr: 403 case OperatorNode::BitAnd: 404 case OperatorNode::Xor: 405 case OperatorNode::LShift: 406 case OperatorNode::RShift: 407 case OperatorNode::LThan: 408 case OperatorNode::GThan: 409 case OperatorNode::LEThan: 410 case OperatorNode::GEThan: 411 case OperatorNode::Eq: 412 case OperatorNode::Neq: 413 case OperatorNode::Index: 414 case OperatorNode::Range: 415 case OperatorNode::UnPlus: 416 case OperatorNode::UnMinus: 417 case OperatorNode::PointTo: 418 case OperatorNode::Neg: 419 case OperatorNode::BitNeg: 420 case OperatorNode::LabelAddress: 421 return new UntypedExpr( new NameExpr( opFuncName[ op->get_type() ] ), args ); 422 case OperatorNode::AddressOf: 423 assert( args.size() == 1 ); 424 assert( args.front() ); 425 426 return new AddressExpr( args.front() ); 427 case OperatorNode::Cast: 428 { 429 TypeValueNode * arg = dynamic_cast<TypeValueNode *>( get_args()); 430 assert( arg ); 431 432 DeclarationNode *decl_node = arg->get_decl(); 433 ExpressionNode *expr_node = dynamic_cast<ExpressionNode *>( arg->get_link()); 434 435 Type *targetType = decl_node->buildType(); 436 if ( dynamic_cast< VoidType* >( targetType ) ) { 437 delete targetType; 438 return new CastExpr( expr_node->build(), maybeBuild< Expression >( get_argName() ) ); 439 } else { 440 return new CastExpr( expr_node->build(),targetType, maybeBuild< Expression >( get_argName() ) ); 441 } // if 442 } 443 case OperatorNode::FieldSel: 444 { 445 assert( args.size() == 2 ); 446 447 NameExpr *member = dynamic_cast<NameExpr *>( args.back()); 448 // TupleExpr *memberTup = dynamic_cast<TupleExpr *>( args.back()); 449 450 if ( member != 0 ) { 451 UntypedMemberExpr *ret = new UntypedMemberExpr( member->get_name(), args.front()); 452 delete member; 453 return ret; 454 /* else if ( memberTup != 0 ) 455 { 456 UntypedMemberExpr *ret = new UntypedMemberExpr( memberTup->get_name(), args.front()); 457 delete member; 458 return ret; 459 } */ 460 } else 461 assert( false ); 462 } 463 case OperatorNode::PFieldSel: 464 { 465 assert( args.size() == 2 ); 466 467 NameExpr *member = dynamic_cast<NameExpr *>( args.back()); // modify for Tuples xxx 468 assert( member != 0 ); 469 470 UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) ); 471 deref->get_args().push_back( args.front() ); 472 473 UntypedMemberExpr *ret = new UntypedMemberExpr( member->get_name(), deref ); 387 } // if 388 389 switch ( op->get_type()) { 390 case OperatorNode::Incr: 391 case OperatorNode::Decr: 392 case OperatorNode::IncrPost: 393 case OperatorNode::DecrPost: 394 case OperatorNode::Assign: 395 case OperatorNode::MulAssn: 396 case OperatorNode::DivAssn: 397 case OperatorNode::ModAssn: 398 case OperatorNode::PlusAssn: 399 case OperatorNode::MinusAssn: 400 case OperatorNode::LSAssn: 401 case OperatorNode::RSAssn: 402 case OperatorNode::AndAssn: 403 case OperatorNode::ERAssn: 404 case OperatorNode::OrAssn: 405 // the rewrite rules for these expressions specify that the first argument has its address taken 406 assert( ! args.empty() ); 407 args.front() = new AddressExpr( args.front() ); 408 break; 409 default: 410 /* do nothing */ 411 ; 412 } 413 414 switch ( op->get_type() ) { 415 case OperatorNode::Incr: 416 case OperatorNode::Decr: 417 case OperatorNode::IncrPost: 418 case OperatorNode::DecrPost: 419 case OperatorNode::Assign: 420 case OperatorNode::MulAssn: 421 case OperatorNode::DivAssn: 422 case OperatorNode::ModAssn: 423 case OperatorNode::PlusAssn: 424 case OperatorNode::MinusAssn: 425 case OperatorNode::LSAssn: 426 case OperatorNode::RSAssn: 427 case OperatorNode::AndAssn: 428 case OperatorNode::ERAssn: 429 case OperatorNode::OrAssn: 430 case OperatorNode::Plus: 431 case OperatorNode::Minus: 432 case OperatorNode::Mul: 433 case OperatorNode::Div: 434 case OperatorNode::Mod: 435 case OperatorNode::BitOr: 436 case OperatorNode::BitAnd: 437 case OperatorNode::Xor: 438 case OperatorNode::LShift: 439 case OperatorNode::RShift: 440 case OperatorNode::LThan: 441 case OperatorNode::GThan: 442 case OperatorNode::LEThan: 443 case OperatorNode::GEThan: 444 case OperatorNode::Eq: 445 case OperatorNode::Neq: 446 case OperatorNode::Index: 447 case OperatorNode::Range: 448 case OperatorNode::UnPlus: 449 case OperatorNode::UnMinus: 450 case OperatorNode::PointTo: 451 case OperatorNode::Neg: 452 case OperatorNode::BitNeg: 453 case OperatorNode::LabelAddress: 454 return new UntypedExpr( new NameExpr( opFuncName[ op->get_type() ] ), args ); 455 case OperatorNode::AddressOf: 456 assert( args.size() == 1 ); 457 assert( args.front() ); 458 459 return new AddressExpr( args.front() ); 460 case OperatorNode::Cast: 461 { 462 TypeValueNode * arg = dynamic_cast<TypeValueNode *>( get_args()); 463 assert( arg ); 464 465 DeclarationNode *decl_node = arg->get_decl(); 466 ExpressionNode *expr_node = dynamic_cast<ExpressionNode *>( arg->get_link()); 467 468 Type *targetType = decl_node->buildType(); 469 if ( dynamic_cast< VoidType* >( targetType ) ) { 470 delete targetType; 471 return new CastExpr( expr_node->build(), maybeBuild< Expression >( get_argName() ) ); 472 } else { 473 return new CastExpr( expr_node->build(),targetType, maybeBuild< Expression >( get_argName() ) ); 474 } // if 475 } 476 case OperatorNode::FieldSel: 477 { 478 assert( args.size() == 2 ); 479 480 NameExpr *member = dynamic_cast<NameExpr *>( args.back()); 481 // TupleExpr *memberTup = dynamic_cast<TupleExpr *>( args.back()); 482 483 if ( member != 0 ) { 484 UntypedMemberExpr *ret = new UntypedMemberExpr( member->get_name(), args.front()); 474 485 delete member; 475 486 return ret; 487 /* else if ( memberTup != 0 ) 488 { 489 UntypedMemberExpr *ret = new UntypedMemberExpr( memberTup->get_name(), args.front()); 490 delete member; 491 return ret; 492 } */ 493 } else 494 assert( false ); 495 } 496 case OperatorNode::PFieldSel: 497 { 498 assert( args.size() == 2 ); 499 500 NameExpr *member = dynamic_cast<NameExpr *>( args.back()); // modify for Tuples xxx 501 assert( member != 0 ); 502 503 UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) ); 504 deref->get_args().push_back( args.front() ); 505 506 UntypedMemberExpr *ret = new UntypedMemberExpr( member->get_name(), deref ); 507 delete member; 508 return ret; 509 } 510 case OperatorNode::AlignOf: 511 case OperatorNode::SizeOf: 512 { 513 /// bool isSizeOf = ( op->get_type() == OperatorNode::SizeOf ); 514 515 if ( TypeValueNode * arg = dynamic_cast<TypeValueNode *>( get_args()) ) { 516 return new SizeofExpr( arg->get_decl()->buildType()); 517 } else { 518 return new SizeofExpr( args.front()); 519 } // if 520 } 521 case OperatorNode::Attr: 522 { 523 VarRefNode *var = dynamic_cast<VarRefNode *>( get_args()); 524 assert( var ); 525 if ( ! get_args()->get_link() ) { 526 return new AttrExpr( var->build(), ( Expression*)0); 527 } else if ( TypeValueNode * arg = dynamic_cast<TypeValueNode *>( get_args()->get_link()) ) { 528 return new AttrExpr( var->build(), arg->get_decl()->buildType()); 529 } else { 530 return new AttrExpr( var->build(), args.back()); 531 } // if 532 } 533 case OperatorNode::CompLit: 534 throw UnimplementedError( "C99 compound literals" ); 535 // the short-circuited operators 536 case OperatorNode::Or: 537 case OperatorNode::And: 538 assert( args.size() == 2); 539 return new LogicalExpr( notZeroExpr( args.front() ), notZeroExpr( args.back() ), ( op->get_type() == OperatorNode::And ) ); 540 case OperatorNode::Cond: 541 { 542 assert( args.size() == 3); 543 std::list< Expression* >::const_iterator i = args.begin(); 544 Expression *arg1 = notZeroExpr( *i++ ); 545 Expression *arg2 = *i++; 546 Expression *arg3 = *i++; 547 return new ConditionalExpr( arg1, arg2, arg3 ); 548 } 549 case OperatorNode::NCond: 550 throw UnimplementedError( "GNU 2-argument conditional expression" ); 551 case OperatorNode::Comma: 552 { 553 assert( args.size() == 2); 554 std::list< Expression* >::const_iterator i = args.begin(); 555 Expression *ret = *i++; 556 while ( i != args.end() ) { 557 ret = new CommaExpr( ret, *i++ ); 476 558 } 477 case OperatorNode::AlignOf: 478 case OperatorNode::SizeOf: 479 { 480 /// bool isSizeOf = ( op->get_type() == OperatorNode::SizeOf ); 481 482 if ( TypeValueNode * arg = dynamic_cast<TypeValueNode *>( get_args()) ) { 483 return new SizeofExpr( arg->get_decl()->buildType()); 484 } else { 485 return new SizeofExpr( args.front()); 486 } // if 487 } 488 case OperatorNode::Attr: 489 { 490 VarRefNode *var = dynamic_cast<VarRefNode *>( get_args()); 491 assert( var ); 492 if ( ! get_args()->get_link() ) { 493 return new AttrExpr( var->build(), ( Expression*)0); 494 } else if ( TypeValueNode * arg = dynamic_cast<TypeValueNode *>( get_args()->get_link()) ) { 495 return new AttrExpr( var->build(), arg->get_decl()->buildType()); 496 } else { 497 return new AttrExpr( var->build(), args.back()); 498 } // if 499 } 500 case OperatorNode::CompLit: 501 throw UnimplementedError( "C99 compound literals" ); 502 // the short-circuited operators 503 case OperatorNode::Or: 504 case OperatorNode::And: 505 assert( args.size() == 2); 506 return new LogicalExpr( notZeroExpr( args.front() ), notZeroExpr( args.back() ), ( op->get_type() == OperatorNode::And ) ); 507 case OperatorNode::Cond: 508 { 509 assert( args.size() == 3); 510 std::list< Expression* >::const_iterator i = args.begin(); 511 Expression *arg1 = notZeroExpr( *i++ ); 512 Expression *arg2 = *i++; 513 Expression *arg3 = *i++; 514 return new ConditionalExpr( arg1, arg2, arg3 ); 515 } 516 case OperatorNode::NCond: 517 throw UnimplementedError( "GNU 2-argument conditional expression" ); 518 case OperatorNode::Comma: 519 { 520 assert( args.size() == 2); 521 std::list< Expression* >::const_iterator i = args.begin(); 522 Expression *ret = *i++; 523 while ( i != args.end() ) { 524 ret = new CommaExpr( ret, *i++ ); 525 } 526 return ret; 527 } 528 // Tuples 529 case OperatorNode::TupleC: 530 { 531 TupleExpr *ret = new TupleExpr(); 532 std::copy( args.begin(), args.end(), back_inserter( ret->get_exprs() ) ); 533 return ret; 534 } 535 default: 536 // shouldn't happen 537 return 0; 538 } 539 } 559 return ret; 560 } 561 // Tuples 562 case OperatorNode::TupleC: 563 { 564 TupleExpr *ret = new TupleExpr(); 565 std::copy( args.begin(), args.end(), back_inserter( ret->get_exprs() ) ); 566 return ret; 567 } 568 default: 569 // shouldn't happen 570 return 0; 571 } // switch 540 572 } 541 573 … … 552 584 void CompositeExprNode::print( std::ostream &os, int indent ) const { 553 585 printDesignation( os ); 554 os << '\r' << string( indent, ' ') << "Application of: " << endl;586 os << string( indent, ' ' ) << "Application of: " << endl; 555 587 function->print( os, indent + ParseNode::indent_by ); 556 588 557 os << '\r' << string( indent, ' ') ;589 os << string( indent, ' ' ) ; 558 590 if ( arguments ) { 559 591 os << "... on arguments: " << endl; … … 586 618 } 587 619 620 //############################################################################## 621 588 622 CommaExprNode::CommaExprNode(): CompositeExprNode( new OperatorNode( OperatorNode::Comma )) {} 589 623 … … 603 637 } 604 638 639 //############################################################################## 640 605 641 ValofExprNode::ValofExprNode( StatementNode *s ): body( s ) {} 606 642 … … 614 650 void ValofExprNode::print( std::ostream &os, int indent ) const { 615 651 printDesignation( os ); 616 os << string( indent, ' ' ) << "Valof Expression:" << std::endl;652 os << string( indent, ' ' ) << "Valof Expression:" << std::endl; 617 653 get_body()->print( os, indent + 4); 618 654 } … … 625 661 return new UntypedValofExpr ( get_body()->build(), maybeBuild< Expression >( get_argName() ) ); 626 662 } 663 664 //############################################################################## 627 665 628 666 ForCtlExprNode::ForCtlExprNode( ParseNode *init_, ExpressionNode *cond, ExpressionNode *incr ) throw ( SemanticError ) : condition( cond ), change( incr ) { … … 633 671 ExpressionNode *exp; 634 672 635 if (( decl = dynamic_cast<DeclarationNode *>( init_)) != 0)673 if (( decl = dynamic_cast<DeclarationNode *>(init_) ) != 0) 636 674 init = new StatementNode( decl ); 637 675 else if (( exp = dynamic_cast<ExpressionNode *>( init_)) != 0) … … 659 697 660 698 void ForCtlExprNode::print( std::ostream &os, int indent ) const{ 661 os << string( indent,' ' ) << "For Control Expression -- :" << endl;662 663 os << "\r" << string( indent + 2,' ') << "initialization: ";664 if ( init != 0 )665 init->print ( os, indent + 4);666 667 os << "\n\r" << string( indent + 2,' ') << "condition: ";668 if ( condition != 0 )669 condition->print( os, indent + 4 );670 os << "\n\r" << string( indent + 2,' ') << "increment: ";671 if ( change != 0 )672 change->print( os, indent + 4 );699 os << string( indent,' ' ) << "For Control Expression -- :" << endl; 700 701 os << string( indent + 2, ' ' ) << "initialization:" << endl; 702 if ( init != 0 ) 703 init->printList( os, indent + 4 ); 704 705 os << string( indent + 2, ' ' ) << "condition: " << endl; 706 if ( condition != 0 ) 707 condition->print( os, indent + 4 ); 708 os << string( indent + 2, ' ' ) << "increment: " << endl; 709 if ( change != 0 ) 710 change->print( os, indent + 4 ); 673 711 } 674 712 … … 677 715 } 678 716 679 TypeValueNode::TypeValueNode( DeclarationNode *decl ) 680 : decl( decl ) { 681 } 682 683 TypeValueNode::TypeValueNode( const TypeValueNode &other ) 684 : ExpressionNode( other ), decl( maybeClone( other.decl ) ) {717 //############################################################################## 718 719 TypeValueNode::TypeValueNode( DeclarationNode *decl ) : decl( decl ) { 720 } 721 722 TypeValueNode::TypeValueNode( const TypeValueNode &other ) : ExpressionNode( other ), decl( maybeClone( other.decl ) ) { 685 723 } 686 724 … … 700 738 701 739 ExpressionNode *flattenCommas( ExpressionNode *list ) { 702 if ( CompositeExprNode *composite = dynamic_cast< CompositeExprNode * >( list ) ) 703 { 704 OperatorNode *op; 705 if ( ( op = dynamic_cast< OperatorNode * >( composite->get_function() )) && ( op->get_type() == OperatorNode::Comma ) ) 706 { 707 if ( ExpressionNode *next = dynamic_cast< ExpressionNode * >( list->get_link() ) ) 708 composite->add_arg( next ); 709 return flattenCommas( composite->get_args() ); 710 } 711 } 740 if ( CompositeExprNode *composite = dynamic_cast< CompositeExprNode * >( list ) ) { 741 OperatorNode *op; 742 if ( ( op = dynamic_cast< OperatorNode * >( composite->get_function() )) && ( op->get_type() == OperatorNode::Comma ) ) { 743 if ( ExpressionNode *next = dynamic_cast< ExpressionNode * >( list->get_link() ) ) 744 composite->add_arg( next ); 745 return flattenCommas( composite->get_args() ); 746 } // if 747 } // if 712 748 713 749 if ( ExpressionNode *next = dynamic_cast< ExpressionNode * >( list->get_link() ) ) … … 722 758 if ( ( op = dynamic_cast< OperatorNode * >( composite->get_function() )) && ( op->get_type() == OperatorNode::TupleC ) ) 723 759 return composite->get_args(); 724 } 760 } // if 725 761 return tuple; 726 762 } -
src/Parser/InitializerNode.cc
reb50842 r937e51d 10 10 // Created On : Sat May 16 13:20:24 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Sat May 16 13:21:40201513 // Update Count : 212 // Last Modified On : Sat Jun 6 15:49:42 2015 13 // Update Count : 3 14 14 // 15 15 … … 48 48 49 49 void InitializerNode::print( std::ostream &os, int indent ) const { 50 os << std::string( indent, ' ') << "Initializer expression" << std::endl;50 os << std::string( indent, ' ' ) << "Initializer expression" << std::endl; 51 51 } 52 52 -
src/Parser/ParseNode.cc
reb50842 r937e51d 10 10 // Created On : Sat May 16 13:26:29 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue May 19 16:48:30201513 // Update Count : 312 // Last Modified On : Sat Jun 6 20:17:58 2015 13 // Update Count : 23 14 14 // 15 15 … … 20 20 int ParseNode::indent_by = 4; 21 21 22 ParseNode::ParseNode( void ) :next( 0 ) {};23 ParseNode::ParseNode( string _name ) : name( _name), next( 0 ) {}22 ParseNode::ParseNode() : name( 0 ), next( 0 ) {}; 23 ParseNode::ParseNode( const string *name_ ) : name( name_ ), next( 0 ) {} 24 24 25 ParseNode *ParseNode::set_name( string _name ) { 26 name = _name; 27 return this; 28 } 29 30 ParseNode *ParseNode::set_name( string *_name ) { 31 name = *_name; // deep copy 32 delete _name; 33 34 return this; 35 } 36 37 ParseNode::~ParseNode( void ) { 25 ParseNode::~ParseNode() { 38 26 delete next; 39 27 }; 40 28 41 string ParseNode::get_name( void ) { 42 return name; 43 } 44 45 ParseNode *ParseNode::get_link( void ) const { 29 ParseNode *ParseNode::get_link() const { 46 30 return next; 47 31 } 48 32 49 ParseNode *ParseNode::get_last( void) {33 ParseNode *ParseNode::get_last() { 50 34 ParseNode *current = this; 51 35 … … 56 40 } 57 41 58 ParseNode *ParseNode::set_link( ParseNode *_next) {42 ParseNode *ParseNode::set_link( ParseNode *next_ ) { 59 43 ParseNode *follow; 60 44 61 if ( _next== 0 ) return this;45 if ( next_ == 0 ) return this; 62 46 63 47 for ( follow = this; follow->next != 0; follow = follow->next ); 64 follow->next = _next;48 follow->next = next_; 65 49 66 50 return this; 67 51 } 68 52 69 const string ParseNode::get_name(void) const { 70 return name; 71 } 72 73 void ParseNode::print(std::ostream &os, int indent) const {} 53 void ParseNode::print( std::ostream &os, int indent ) const {} 74 54 75 55 … … 78 58 79 59 if ( next ) { 80 next->printList( os, indent );81 } 60 next->printList( os, indent ); 61 } // if 82 62 } 83 63 -
src/Parser/ParseNode.h
reb50842 r937e51d 10 10 // Created On : Sat May 16 13:28:16 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Sat May 16 13:30:24201513 // Update Count : 312 // Last Modified On : Wed Jun 24 14:09:51 2015 13 // Update Count : 81 14 14 // 15 15 … … 22 22 23 23 #include "utility.h" 24 #include "SynTree/Declaration.h" 24 #include "Parser/LinkageSpec.h" 25 #include "SynTree/Type.h" 26 //#include "SynTree/Declaration.h" 25 27 #include "UniqueName.h" 26 28 … … 36 38 class ParseNode { 37 39 public: 38 ParseNode( void ); 39 ParseNode ( std::string ); 40 virtual ~ParseNode( void ); 41 42 ParseNode *set_name ( std::string ) ; 43 ParseNode *set_name ( std::string * ) ; 44 45 std::string get_name( void ); 46 47 ParseNode *get_link( void ) const; 48 ParseNode *get_last( void ); 40 ParseNode(); 41 ParseNode( const std::string * ); 42 virtual ~ParseNode(); 43 44 ParseNode *get_link() const; 45 ParseNode *get_last(); 49 46 ParseNode *set_link( ParseNode * ); 50 47 void set_next( ParseNode *newlink ) { next = newlink; } … … 52 49 virtual ParseNode *clone() const { return 0; }; 53 50 54 const std::string get_name( void ) const;51 const std::string &get_name() const { return *name; } 55 52 virtual void print( std::ostream &, int indent = 0 ) const; 56 53 virtual void printList( std::ostream &, int indent = 0 ) const; … … 58 55 ParseNode &operator,( ParseNode &); 59 56 protected: 60 std::stringname;57 const std::string *name; 61 58 ParseNode *next; 62 59 static int indent_by; … … 68 65 public: 69 66 ExpressionNode(); 70 ExpressionNode( std::string * );67 ExpressionNode( const std::string * ); 71 68 ExpressionNode( const ExpressionNode &other ); 72 69 virtual ~ExpressionNode() {} // cannot delete asArgName because it might be referenced elsewhere … … 77 74 78 75 ExpressionNode *get_argName() const { return argName; } 79 ExpressionNode *set_asArgName( std::string *aName );76 ExpressionNode *set_asArgName( const std::string *aName ); 80 77 ExpressionNode *set_asArgName( ExpressionNode *aDesignator ); 81 78 … … 105 102 class ConstantNode : public ExpressionNode { 106 103 public: 107 enum Type { 108 Integer, Float, Character, String /* , Range, EnumConstant */ 109 }; 110 111 ConstantNode( void ); 112 ConstantNode( std::string * ); 104 enum Type { Integer, Float, Character, String }; 105 113 106 ConstantNode( Type, std::string * ); 114 ConstantNode( const ConstantNode &other );115 107 116 108 virtual ConstantNode *clone() const { return new ConstantNode( *this ); } 117 118 Type get_type( void ) const ; 109 Type get_type( void ) const { return type; } 119 110 virtual void print( std::ostream &, int indent = 0) const; 120 111 virtual void printOneLine( std::ostream &, int indent = 0) const; 121 112 122 std::stringget_value() const { return value; }123 ConstantNode *append (std::string *newValue );113 const std::string &get_value() const { return value; } 114 ConstantNode *appendstr( const std::string *newValue ); 124 115 125 116 Expression *build() const; 126 117 private: 127 void classify( std::string &);128 118 Type type; 129 std::string value; 130 bool sign; 131 short base; 132 int longs, size; 119 BasicType::Kind btype; 120 std::string &value; 133 121 }; 134 122 … … 136 124 public: 137 125 VarRefNode(); 138 VarRefNode( std::string *, bool isLabel = false );126 VarRefNode( const std::string *, bool isLabel = false ); 139 127 VarRefNode( const VarRefNode &other ); 140 128 … … 143 131 virtual VarRefNode *clone() const { return new VarRefNode( *this ); } 144 132 145 virtual void print( std::ostream &, int indent = 0 ) const;146 virtual void printOneLine( std::ostream &, int indent = 0 ) const;133 virtual void print( std::ostream &, int indent = 0 ) const; 134 virtual void printOneLine( std::ostream &, int indent = 0 ) const; 147 135 private: 148 136 bool isLabel; … … 183 171 virtual OperatorNode *clone() const { return new OperatorNode( *this ); } 184 172 185 Type get_type( void) const;186 std::string get_typename( void) const;173 Type get_type() const; 174 const char *get_typename() const; 187 175 188 176 virtual void print( std::ostream &, int indent = 0) const; … … 198 186 class CompositeExprNode : public ExpressionNode { 199 187 public: 200 CompositeExprNode( void);201 CompositeExprNode( std::string * );188 CompositeExprNode(); 189 CompositeExprNode( const std::string * ); 202 190 CompositeExprNode( ExpressionNode *f, ExpressionNode *args = 0 ); 203 191 CompositeExprNode( ExpressionNode *f, ExpressionNode *arg1, ExpressionNode *arg2 ); … … 278 266 public: 279 267 enum Qualifier { Const, Restrict, Volatile, Lvalue, Atomic, Attribute }; 280 enum StorageClass { Extern, Static, Auto, Register, Inline, Fortran };268 enum StorageClass { Extern, Static, Auto, Register, Inline, Fortran, Noreturn, Threadlocal, NoStorageClass, }; 281 269 enum BasicType { Char, Int, Float, Double, Void, Bool, Complex, Imaginary }; 282 enum Modifier { Signed, Unsigned, Short, Long };283 enum TyCon{ Struct, Union, Context };270 enum Modifier { Signed, Unsigned, Short, Long }; 271 enum Aggregate { Struct, Union, Context }; 284 272 enum TypeClass { Type, Dtype, Ftype }; 285 273 274 static const char *storageName[]; 286 275 static const char *qualifierName[]; 287 276 static const char *basicTypeName[]; 288 277 static const char *modifierName[]; 289 static const char * tyConName[];278 static const char *aggregateName[]; 290 279 static const char *typeClassName[]; 291 280 … … 298 287 static DeclarationNode *newForall( DeclarationNode *); 299 288 static DeclarationNode *newFromTypedef( std::string *); 300 static DeclarationNode *newAggregate( TyConkind, std::string *name, DeclarationNode *formals, ExpressionNode *actuals, DeclarationNode *fields );289 static DeclarationNode *newAggregate( Aggregate kind, std::string *name, DeclarationNode *formals, ExpressionNode *actuals, DeclarationNode *fields ); 301 290 static DeclarationNode *newEnum( std::string *name, DeclarationNode *constants ); 302 291 static DeclarationNode *newEnumConstant( std::string *name, ExpressionNode *constant ); 303 292 static DeclarationNode *newName( std::string *); 304 static DeclarationNode *newFromTypeGen( std::string *, ExpressionNode *params );293 static DeclarationNode *newFromTypeGen( std::string *, ExpressionNode *params ); 305 294 static DeclarationNode *newTypeParam( TypeClass, std::string *); 306 295 static DeclarationNode *newContext( std::string *name, DeclarationNode *params, DeclarationNode *asserts ); … … 313 302 static DeclarationNode *newTuple( DeclarationNode *members ); 314 303 static DeclarationNode *newTypeof( ExpressionNode *expr ); 315 static DeclarationNode *newAttr( std::string *, ExpressionNode *expr );316 static DeclarationNode *newAttr( std::string *, DeclarationNode *type );304 static DeclarationNode *newAttr( std::string *, ExpressionNode *expr ); 305 static DeclarationNode *newAttr( std::string *, DeclarationNode *type ); 317 306 318 307 DeclarationNode *addQualifiers( DeclarationNode *); … … 340 329 DeclarationNode *cloneBaseType( DeclarationNode *newdecl ); 341 330 342 DeclarationNode *appendList( DeclarationNode *);331 DeclarationNode *appendList( DeclarationNode * ); 343 332 344 333 DeclarationNode *clone() const; … … 350 339 351 340 bool get_hasEllipsis() const; 352 std::stringget_name() const { return name; }341 const std::string &get_name() const { return name; } 353 342 LinkageSpec::Type get_linkage() const { return linkage; } 354 343 DeclarationNode *extractAggregate() const; … … 357 346 ~DeclarationNode(); 358 347 private: 359 Declaration::StorageClass buildStorageClass() const;360 bool build Inline() const;348 StorageClass buildStorageClass() const; 349 bool buildFuncSpecifier( StorageClass key ) const; 361 350 362 351 TypeData *type; … … 380 369 }; 381 370 382 StatementNode( void);383 StatementNode( std::string);371 StatementNode(); 372 StatementNode( const std::string * ); 384 373 StatementNode( Type, ExpressionNode *e = 0, StatementNode *s = 0 ); 385 374 StatementNode( Type, std::string *target ); … … 387 376 388 377 389 ~StatementNode( void);390 391 static StatementNode *newCatchStmt( DeclarationNode *d = 0, StatementNode *s = 0, bool catchRestP = false );378 ~StatementNode(); 379 380 static StatementNode *newCatchStmt( DeclarationNode *d = 0, StatementNode *s = 0, bool catchRestP = false ); 392 381 393 382 void set_control( ExpressionNode * ); … … 396 385 ExpressionNode *get_control() const ; 397 386 StatementNode *get_block() const; 398 StatementNode::Type get_type( void) const;399 400 StatementNode *add_label( std::string * );387 StatementNode::Type get_type() const; 388 389 StatementNode *add_label( const std::string * ); 401 390 std::list<std::string> *get_labels() const; 402 391 … … 429 418 class CompoundStmtNode : public StatementNode { 430 419 public: 431 CompoundStmtNode( void);432 CompoundStmtNode( std::string * );420 CompoundStmtNode(); 421 CompoundStmtNode( const std::string * ); 433 422 CompoundStmtNode( StatementNode * ); 434 423 ~CompoundStmtNode(); … … 499 488 500 489 // in DeclarationNode.cc 501 void buildList( const DeclarationNode *firstNode, std::list< Declaration * > &outputList );490 void buildList( const DeclarationNode *firstNode, std::list< Declaration * > &outputList ); 502 491 void buildList( const DeclarationNode *firstNode, std::list< DeclarationWithType *> &outputList ); 503 void buildTypeList( const DeclarationNode *firstNode, std::list< Type * > &outputList );492 void buildTypeList( const DeclarationNode *firstNode, std::list< Type * > &outputList ); 504 493 505 494 // in ExpressionNode.cc -
src/Parser/Parser.cc
reb50842 r937e51d 10 10 // Created On : Sat May 16 14:54:28 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : S at May 16 14:55:59 201513 // Update Count : 212 // Last Modified On : Sun May 31 23:45:19 2015 13 // Update Count : 4 14 14 // 15 15 … … 17 17 #include "TypedefTable.h" 18 18 #include "lex.h" 19 #include " cfa.tab.h"19 #include "parser.h" 20 20 21 21 // global variables in cfa.y -
src/Parser/StatementNode.cc
reb50842 r937e51d 10 10 // Created On : Sat May 16 14:59:41 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Sat May 16 15:10:45201513 // Update Count : 712 // Last Modified On : Sat Jun 6 23:25:41 2015 13 // Update Count : 19 14 14 // 15 15 … … 36 36 StatementNode::StatementNode() : ParseNode(), control( 0 ), block( 0 ), labels( 0 ), target( 0 ), decl( 0 ), isCatchRest ( false ) {} 37 37 38 StatementNode::StatementNode( string name_) : ParseNode( name_), control( 0 ), block( 0 ), labels( 0 ), target( 0 ), decl( 0 ), isCatchRest ( false ) {}38 StatementNode::StatementNode( const string *name_ ) : ParseNode( name_ ), control( 0 ), block( 0 ), labels( 0 ), target( 0 ), decl( 0 ), isCatchRest ( false ) {} 39 39 40 40 StatementNode::StatementNode( DeclarationNode *decl ) : type( Decl ), control( 0 ), block( 0 ), labels( 0 ), target( 0 ), isCatchRest ( false ) { … … 49 49 next->set_next( new StatementNode( dynamic_cast< DeclarationNode* >( decl->get_link() ) ) ); 50 50 decl->set_next( 0 ); 51 } 51 } // if 52 52 } else { 53 53 if ( decl->get_link() ) { 54 54 next = new StatementNode( dynamic_cast< DeclarationNode* >( decl->get_link() ) ); 55 55 decl->set_next( 0 ); 56 } 56 } // if 57 57 this->decl = decl; 58 } 59 } 58 } // if 59 } // if 60 60 } 61 61 … … 67 67 68 68 StatementNode::StatementNode( Type t, string *_target ) : 69 type( t ), control( 0 ), block( 0 ), labels( 0 ), target(_target ), decl( 0 ), isCatchRest ( false ) {}69 type( t ), control( 0 ), block( 0 ), labels( 0 ), target(_target ), decl( 0 ), isCatchRest ( false ) {} 70 70 71 71 StatementNode::~StatementNode() { … … 98 98 } else { 99 99 newnode->target = 0; 100 } 100 } // if 101 101 newnode->decl = maybeClone( decl ); 102 102 return newnode; … … 125 125 } 126 126 127 StatementNode *StatementNode::add_label( std::string *l ) {127 StatementNode *StatementNode::add_label( const std::string *l ) { 128 128 if ( l != 0 ) { 129 129 if ( labels == 0 ) … … 132 132 labels->push_front(*l ); 133 133 delete l; 134 } 134 } // if 135 135 return this; 136 136 } … … 151 151 else 152 152 block->set_link( stmt ); 153 } 153 } // if 154 154 return this; 155 155 } … … 165 165 else 166 166 block->set_link( stmt ); 167 } 167 } // if 168 168 return this; 169 169 } 170 170 171 171 void StatementNode::print( std::ostream &os, int indent ) const { 172 if ( labels != 0 ) 172 if ( labels != 0 ) { 173 173 if ( ! labels->empty()) { 174 174 std::list<std::string>::const_iterator i; 175 175 176 os << '\r' << string( indent, ' ');176 os << string( indent, ' ' ); 177 177 for ( i = labels->begin(); i != labels->end(); i++ ) 178 178 os << *i << ":"; 179 179 os << endl; 180 } 180 } // if 181 } // if 181 182 182 183 switch ( type ) { … … 193 194 break; 194 195 default: 195 os << '\r' << string( indent, ' ') << StatementNode::StType[type] << endl;196 os << string( indent, ' ' ) << StatementNode::StType[type] << endl; 196 197 if ( type == Catch ) { 197 198 if ( decl ) { 198 os << '\r' <<string( indent + ParseNode::indent_by, ' ' ) << "Declaration: " << endl;199 os << string( indent + ParseNode::indent_by, ' ' ) << "Declaration: " << endl; 199 200 decl->print( os, indent + 2*ParseNode::indent_by ); 200 201 } else if ( isCatchRest ) { 201 os << '\r' <<string( indent + ParseNode::indent_by, ' ' ) << "Catches the rest " << endl;202 os << string( indent + ParseNode::indent_by, ' ' ) << "Catches the rest " << endl; 202 203 } else { 203 204 ; // should never reach here 204 } 205 } 205 } // if 206 } // if 206 207 if ( control ) { 207 os << '\r' <<string( indent + ParseNode::indent_by, ' ' ) << "Expression: " << endl;208 os << string( indent + ParseNode::indent_by, ' ' ) << "Expression: " << endl; 208 209 control->printList( os, indent + 2*ParseNode::indent_by ); 209 } 210 } // if 210 211 if ( block ) { 211 os << '\r' <<string( indent + ParseNode::indent_by, ' ' ) << "Branches of execution: " << endl;212 os << string( indent + ParseNode::indent_by, ' ' ) << "Branches of execution: " << endl; 212 213 block->printList( os, indent + 2*ParseNode::indent_by ); 213 } 214 } // if 214 215 if ( target ) { 215 os << '\r' <<string( indent + ParseNode::indent_by, ' ' ) << "Target: " << get_target() << endl;216 } 216 os << string( indent + ParseNode::indent_by, ' ' ) << "Target: " << get_target() << endl; 217 } // if 217 218 break; 218 } 219 } // switch 219 220 } 220 221 … … 227 228 std::back_insert_iterator< std::list<Label> > lab_it( labs ); 228 229 copy( labels->begin(), labels->end(), lab_it ); 229 } 230 } // if 230 231 231 232 // try { … … 254 255 elseb = branches.front(); 255 256 branches.pop_front(); 256 } 257 } // if 257 258 return new IfStmt( labs, notZeroExpr( get_control()->build() ), thenb, elseb ); 258 259 } … … 299 300 assert( get_control() != 0 ); 300 301 return new BranchStmt( labs, get_control()->build(), BranchStmt::Goto ); 301 } 302 } // if 302 303 303 304 return new BranchStmt( labs, get_target(), BranchStmt::Goto ); … … 322 323 if ( ( finallyBlock = dynamic_cast<FinallyStmt *>( branches.back())) ) { 323 324 branches.pop_back(); 324 } 325 } // if 325 326 return new TryStmt( labs, tryBlock, branches, finallyBlock ); 326 327 } … … 342 343 // shouldn't be here 343 344 return 0; 344 } 345 } 346 347 CompoundStmtNode::CompoundStmtNode() : first( 0 ), last( 0 ) { 348 } 349 350 CompoundStmtNode::CompoundStmtNode( string *name_) : StatementNode(*name_), first( 0 ), last( 0 ) { 351 } 352 353 CompoundStmtNode::CompoundStmtNode( StatementNode *stmt ): first( stmt ) { 345 } // switch 346 } 347 348 CompoundStmtNode::CompoundStmtNode() : first( 0 ), last( 0 ) {} 349 350 CompoundStmtNode::CompoundStmtNode( const string *name_ ) : StatementNode( name_ ), first( 0 ), last( 0 ) {} 351 352 CompoundStmtNode::CompoundStmtNode( StatementNode *stmt ) : first( stmt ) { 354 353 if ( first ) { 355 354 last = ( StatementNode *)( stmt->get_last()); 356 355 } else { 357 356 last = 0; 358 } 357 } // if 359 358 } 360 359 … … 367 366 last->set_link( stmt ); 368 367 last = ( StatementNode *)( stmt->get_link()); 369 } 368 } // if 370 369 } 371 370 … … 373 372 if ( first ) { 374 373 first->printList( os, indent+2 ); 375 } 374 } // if 376 375 } 377 376 … … 383 382 std::back_insert_iterator< std::list<Label> > lab_it( labs ); 384 383 copy( labels->begin(), labels->end(), lab_it ); 385 } 384 } // if 386 385 387 386 CompoundStmt *cs = new CompoundStmt( labs ); … … 391 390 392 391 void NullStmtNode::print( ostream &os, int indent ) const { 393 os << "\r" << string( indent, ' ') << "Null Statement:" << endl;392 os << string( indent, ' ' ) << "Null Statement:" << endl; 394 393 } 395 394 -
src/Parser/TypeData.cc
reb50842 r937e51d 10 10 // Created On : Sat May 16 15:12:51 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Sat May 16 15:17:56201513 // Update Count : 412 // Last Modified On : Thu Jun 18 22:06:23 2015 13 // Update Count : 21 14 14 // 15 15 … … 89 89 attr->type = 0; 90 90 break; 91 } 91 } // switch 92 92 } 93 93 … … 155 155 delete attr; 156 156 break; 157 } 157 } // switch 158 158 } 159 159 … … 225 225 newtype->attr->type = maybeClone( attr->type ); 226 226 break; 227 } 227 } // switch 228 228 return newtype; 229 229 } … … 238 238 os << "forall " << endl; 239 239 forall->printList( os, indent+4 ); 240 } 240 } // if 241 241 242 242 switch ( kind ) { … … 249 249 os << "to "; 250 250 base->print( os, indent ); 251 } 251 } // if 252 252 break; 253 253 case EnumConstant: … … 261 261 if ( array->isStatic ) { 262 262 os << "static "; 263 } 263 } // if 264 264 if ( array->dimension ) { 265 265 os << "array of "; … … 269 269 } else { 270 270 os << "open array of "; 271 } 271 } // if 272 272 if ( base ) { 273 273 base->print( os, indent ); 274 } 274 } // if 275 275 break; 276 276 case Function: … … 281 281 } else { 282 282 os << string( indent+2, ' ' ) << "with no parameters " << endl; 283 } 283 } // if 284 284 if ( function->idList ) { 285 285 os << string( indent+2, ' ' ) << "with old-style identifier list " << endl; 286 286 function->idList->printList( os, indent+4 ); 287 } 287 } // if 288 288 if ( function->oldDeclList ) { 289 289 os << string( indent+2, ' ' ) << "with old-style declaration list " << endl; 290 290 function->oldDeclList->printList( os, indent+4 ); 291 } 291 } // if 292 292 os << string( indent+2, ' ' ) << "returning "; 293 293 if ( base ) { … … 295 295 } else { 296 296 os << "nothing "; 297 } 297 } // if 298 298 os << endl; 299 299 if ( function->hasBody ) { 300 300 os << string( indent+2, ' ' ) << "with body " << endl; 301 } 301 } // if 302 302 if ( function->body ) { 303 303 function->body->printList( os, indent+2 ); 304 } 304 } // if 305 305 break; 306 306 case Aggregate: 307 os << DeclarationNode:: tyConName[ aggregate->kind ] << ' ' << aggregate->name << endl;307 os << DeclarationNode::aggregateName[ aggregate->kind ] << ' ' << aggregate->name << endl; 308 308 if ( aggregate->params ) { 309 309 os << string( indent+2, ' ' ) << "with type parameters " << endl; 310 310 aggregate->params->printList( os, indent+4 ); 311 } 311 } // if 312 312 if ( aggregate->actuals ) { 313 313 os << string( indent+2, ' ' ) << "instantiated with actual parameters " << endl; 314 314 aggregate->actuals->printList( os, indent+4 ); 315 } 315 } // if 316 316 if ( aggregate->members ) { 317 317 os << string( indent+2, ' ' ) << "with members " << endl; … … 319 319 /// } else { 320 320 /// os << string( indent+2, ' ' ) << "with no members " << endl; 321 } 321 } // if 322 322 break; 323 323 case AggregateInst: … … 327 327 } else { 328 328 os << "instance of an unspecified aggregate "; 329 } 329 } // if 330 330 if ( aggInst->params ) { 331 331 os << string( indent+2, ' ' ) << "with parameters " << endl; 332 332 aggInst->params->printList( os, indent+2 ); 333 } 333 } // if 334 334 break; 335 335 case Enum: … … 338 338 os << "with constants" << endl; 339 339 enumeration->constants->printList( os, indent+2 ); 340 } 340 } // if 341 341 break; 342 342 case SymbolicInst: … … 345 345 os << " with parameters" << endl; 346 346 symbolic->actuals->printList( os, indent + 2 ); 347 } 347 } // if 348 348 break; 349 349 case Symbolic: … … 352 352 } else { 353 353 os << "type definition "; 354 } 354 } // if 355 355 if ( symbolic->params ) { 356 356 os << endl << string( indent+2, ' ' ) << "with parameters" << endl; 357 357 symbolic->params->printList( os, indent + 2 ); 358 } 358 } // if 359 359 if ( symbolic->assertions ) { 360 360 os << endl << string( indent+2, ' ' ) << "with assertions" << endl; 361 361 symbolic->assertions->printList( os, indent + 4 ); 362 362 os << string( indent+2, ' ' ); 363 } 363 } // if 364 364 if ( base ) { 365 365 os << "for "; 366 366 base->print( os, indent + 2 ); 367 } 367 } // if 368 368 break; 369 369 case Variable: … … 373 373 variable->assertions->printList( os, indent + 4 ); 374 374 os << string( indent+2, ' ' ); 375 } 375 } // if 376 376 break; 377 377 case Tuple: … … 380 380 os << "with members " << endl; 381 381 tuple->members->printList( os, indent + 2 ); 382 } 382 } // if 383 383 break; 384 384 case Typeof: … … 386 386 if ( typeexpr->expr ) { 387 387 typeexpr->expr->print( os, indent + 2 ); 388 } 388 } // if 389 389 break; 390 390 case Attr: … … 392 392 if ( attr->expr ) { 393 393 attr->expr->print( os, indent + 2 ); 394 } 394 } // if 395 395 if ( attr->type ) { 396 396 attr->type->print( os, indent + 2 ); 397 } 398 break; 399 } 397 } // if 398 break; 399 } // switch 400 400 } 401 401 … … 408 408 ret = clone(); 409 409 ret->qualifiers.clear(); 410 } 410 } // if 411 411 break; 412 412 case Enum: … … 414 414 ret = clone(); 415 415 ret->qualifiers.clear(); 416 } 416 } // if 417 417 break; 418 418 case AggregateInst: 419 419 if ( aggInst->aggregate ) { 420 420 ret = aggInst->aggregate->extractAggregate( false ); 421 } 421 } // if 422 422 break; 423 423 default: 424 424 if ( base ) { 425 425 ret = base->extractAggregate( false ); 426 } 427 } 426 } // if 427 } // switch 428 428 return ret; 429 429 } … … 434 434 if ( (*i)->get_kind() == TypeDecl::Any ) { 435 435 FunctionType *assignType = new FunctionType( Type::Qualifiers(), false ); 436 assignType->get_parameters().push_back( new ObjectDecl( "", Declaration ::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), (*i)->get_name(), *i ) ), 0 ) );437 assignType->get_parameters().push_back( new ObjectDecl( "", Declaration ::NoStorageClass, LinkageSpec::Cforall, 0, new TypeInstType( Type::Qualifiers(), (*i)->get_name(), *i ), 0 ) );438 assignType->get_returnVals().push_back( new ObjectDecl( "", Declaration ::NoStorageClass, LinkageSpec::Cforall, 0, new TypeInstType( Type::Qualifiers(), (*i)->get_name(), *i ), 0 ) );439 (*i)->get_assertions().push_front( new FunctionDecl( "?=?", Declaration ::NoStorageClass, LinkageSpec::Cforall, assignType, 0, false ) );440 } 441 } 442 } 443 444 Declaration *TypeData::buildDecl( std::string name, Declaration ::StorageClass sc, Expression *bitfieldWidth, bool isInline, LinkageSpec::Type linkage, Initializer *init ) const {436 assignType->get_parameters().push_back( new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), (*i)->get_name(), *i ) ), 0 ) ); 437 assignType->get_parameters().push_back( new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new TypeInstType( Type::Qualifiers(), (*i)->get_name(), *i ), 0 ) ); 438 assignType->get_returnVals().push_back( new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new TypeInstType( Type::Qualifiers(), (*i)->get_name(), *i ), 0 ) ); 439 (*i)->get_assertions().push_front( new FunctionDecl( "?=?", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, assignType, 0, false, false ) ); 440 } // if 441 } // for 442 } 443 444 Declaration *TypeData::buildDecl( std::string name, DeclarationNode::StorageClass sc, Expression *bitfieldWidth, bool isInline, bool isNoreturn, LinkageSpec::Type linkage, Initializer *init ) const { 445 445 if ( kind == TypeData::Function ) { 446 446 FunctionDecl *decl; … … 450 450 CompoundStmt *body = dynamic_cast< CompoundStmt* >( stmt ); 451 451 assert( body ); 452 decl = new FunctionDecl( name, sc, linkage, buildFunction(), body, isInline );452 decl = new FunctionDecl( name, sc, linkage, buildFunction(), body, isInline, isNoreturn ); 453 453 } else { 454 454 // std::list<Label> ls; 455 decl = new FunctionDecl( name, sc, linkage, buildFunction(), new CompoundStmt( std::list<Label>() ), isInline );456 } 455 decl = new FunctionDecl( name, sc, linkage, buildFunction(), new CompoundStmt( std::list<Label>() ), isInline, isNoreturn ); 456 } // if 457 457 } else { 458 decl = new FunctionDecl( name, sc, linkage, buildFunction(), 0, isInline );459 } 458 decl = new FunctionDecl( name, sc, linkage, buildFunction(), 0, isInline, isNoreturn ); 459 } // if 460 460 for ( DeclarationNode *cur = function->idList; cur != 0; cur = dynamic_cast< DeclarationNode* >( cur->get_link() ) ) { 461 461 if ( cur->get_name() != "" ) { 462 462 decl->get_oldIdents().insert( decl->get_oldIdents().end(), cur->get_name() ); 463 } 464 } 463 } // if 464 } // for 465 465 buildList( function->oldDeclList, decl->get_oldDecls() ); 466 466 return decl; … … 474 474 return buildVariable(); 475 475 } else { 476 if ( isInline ) {477 throw SemanticError( "invalid inline specification in declaration of ", this );476 if ( isInline || isNoreturn ) { 477 throw SemanticError( "invalid inline or _Noreturn specification in declaration of ", this ); 478 478 } else { 479 479 return new ObjectDecl( name, sc, linkage, bitfieldWidth, build(), init ); 480 } 481 } 480 } // if 481 } // if 482 482 return 0; 483 483 } … … 514 514 case Variable: 515 515 assert( false ); 516 } 516 } // switch 517 517 518 518 return 0; … … 541 541 q.isAttribute = true; 542 542 break; 543 } 544 } 543 } // switch 544 } // for 545 545 return q; 546 546 } … … 563 563 } else { 564 564 return new VoidType( buildQualifiers() ); 565 } 565 } // if 566 566 } else { 567 567 ret = kindMap[ *i ]; 568 } 568 } // if 569 569 } else { 570 570 switch ( *i ) { … … 582 582 default: 583 583 throw SemanticError( "invalid type specifier \"float\" in type: ", this ); 584 } 585 } 584 } // switch 585 } // if 586 586 break; 587 587 case DeclarationNode::Double: … … 595 595 default: 596 596 throw SemanticError( "invalid type specifier \"double\" in type: ", this ); 597 } 598 } 597 } // switch 598 } // if 599 599 break; 600 601 600 case DeclarationNode::Complex: 602 601 switch ( ret ) { … … 609 608 default: 610 609 throw SemanticError( "invalid type specifier \"_Complex\" in type: ", this ); 611 } 610 } // switch 612 611 break; 613 612 case DeclarationNode::Imaginary: … … 621 620 default: 622 621 throw SemanticError( "invalid type specifier \"_Imaginary\" in type: ", this ); 623 } 622 } // switch 624 623 break; 625 624 default: 626 625 throw SemanticError( std::string( "invalid type specifier \"" ) + DeclarationNode::basicTypeName[ *i ] + "\" in type: ", this ); 627 } 628 } 626 } // switch 627 } // if 629 628 if ( *i == DeclarationNode::Double ) { 630 629 sawDouble = true; 631 } 632 } 630 } // if 631 } // for 633 632 634 633 for ( std::list< DeclarationNode::Modifier >::const_iterator i = basic->modifiers.begin(); i != basic->modifiers.end(); ++i ) { … … 663 662 default: 664 663 throw SemanticError( "invalid type modifier \"long\" in type: ", this ); 665 } 666 } 664 } // switch 665 } // if 667 666 break; 668 667 case DeclarationNode::Short: … … 680 679 default: 681 680 throw SemanticError( "invalid type modifier \"short\" in type: ", this ); 682 } 683 } 681 } // switch 682 } // if 684 683 break; 685 684 case DeclarationNode::Signed: … … 691 690 } else { 692 691 switch ( ret ) { 693 case BasicType::LongLongSignedInt: // PAB692 case BasicType::LongLongSignedInt: 694 693 ret = BasicType::LongLongUnsignedInt; 695 694 break; … … 705 704 default: 706 705 throw SemanticError( "invalid type modifer \"signed\" in type: ", this ); 707 } 708 } 706 } // switch 707 } // if 709 708 break; 710 709 case DeclarationNode::Unsigned: … … 716 715 } else { 717 716 switch ( ret ) { 718 case BasicType::LongLongSignedInt: // PAB717 case BasicType::LongLongSignedInt: 719 718 ret = BasicType::LongLongUnsignedInt; 720 719 break; … … 733 732 default: 734 733 throw SemanticError( "invalid type modifer \"unsigned\" in type: ", this ); 735 } 736 } 737 break; 738 } 734 } // switch 735 } // if 736 break; 737 } // switch 739 738 740 739 if ( *i == DeclarationNode::Signed ) { 741 740 sawSigned = true; 742 } 743 } 741 } // if 742 } // for 744 743 745 744 BasicType *bt; … … 748 747 } else { 749 748 bt = new BasicType( buildQualifiers(), ret ); 750 } 749 } // if 751 750 buildForall( forall, bt->get_forall() ); 752 751 return bt; … … 760 759 } else { 761 760 pt = new PointerType( buildQualifiers(), new BasicType( Type::Qualifiers(), BasicType::SignedInt ) ); 762 } 761 } // if 763 762 buildForall( forall, pt->get_forall() ); 764 763 return pt; … … 773 772 at = new ArrayType( buildQualifiers(), new BasicType( Type::Qualifiers(), BasicType::SignedInt ), 774 773 maybeBuild< Expression >( array->dimension ), array->isVarLen, array->isStatic ); 775 } 774 } // if 776 775 buildForall( forall, at->get_forall() ); 777 776 return at; … … 791 790 break; 792 791 default: 793 ft->get_returnVals().push_back( dynamic_cast< DeclarationWithType* >( base->buildDecl( "", Declaration ::NoStorageClass, 0, false, LinkageSpec::Cforall ) ) );794 } 792 ft->get_returnVals().push_back( dynamic_cast< DeclarationWithType* >( base->buildDecl( "", DeclarationNode::NoStorageClass, 0, false, false, LinkageSpec::Cforall ) ) ); 793 } // switch 795 794 } else { 796 ft->get_returnVals().push_back( new ObjectDecl( "", Declaration ::NoStorageClass, LinkageSpec::Cforall, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), 0 ) );797 } 795 ft->get_returnVals().push_back( new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), 0 ) ); 796 } // if 798 797 return ft; 799 798 } … … 806 805 at = new StructDecl( aggregate->name ); 807 806 break; 808 809 807 case DeclarationNode::Union: 810 808 at = new UnionDecl( aggregate->name ); 811 809 break; 812 813 810 case DeclarationNode::Context: 814 811 at = new ContextDecl( aggregate->name ); 815 812 break; 816 817 813 default: 818 814 assert( false ); 819 } 815 } // switch 820 816 buildList( aggregate->params, at->get_parameters() ); 821 817 buildList( aggregate->members, at->get_members() ); … … 838 834 ReferenceToType *TypeData::buildAggInst() const { 839 835 assert( kind == AggregateInst ); 840 std::string name;841 836 842 837 ReferenceToType *ret; … … 857 852 default: 858 853 assert( false ); 859 } 860 } 854 } // switch 855 } // if 861 856 buildList( aggInst->params, ret->get_parameters() ); 862 857 buildForall( forall, ret->get_forall() ); … … 864 859 } 865 860 866 NamedTypeDecl *TypeData::buildSymbolic( const std::string &name, Declaration ::StorageClass sc ) const {861 NamedTypeDecl *TypeData::buildSymbolic( const std::string &name, DeclarationNode::StorageClass sc ) const { 867 862 assert( kind == Symbolic ); 868 863 NamedTypeDecl *ret; … … 871 866 } else { 872 867 ret = new TypeDecl( name, sc, maybeBuild< Type >( base ), TypeDecl::Any ); 873 } 868 } // if 874 869 buildList( symbolic->params, ret->get_parameters() ); 875 870 buildList( symbolic->assertions, ret->get_assertions() ); … … 881 876 static const TypeDecl::Kind kindMap[] = { TypeDecl::Any, TypeDecl::Ftype, TypeDecl::Dtype }; 882 877 883 TypeDecl *ret = new TypeDecl( variable->name, Declaration ::NoStorageClass, 0, kindMap[ variable->tyClass ] );878 TypeDecl *ret = new TypeDecl( variable->name, DeclarationNode::NoStorageClass, 0, kindMap[ variable->tyClass ] ); 884 879 buildList( variable->assertions, ret->get_assertions() ); 885 886 880 return ret; 887 881 } … … 891 885 EnumDecl *ret = new EnumDecl( enumeration->name ); 892 886 buildList( enumeration->constants, ret->get_members() ); 893 894 887 return ret; 895 888 } … … 900 893 buildList( symbolic->actuals, ret->get_parameters() ); 901 894 buildForall( forall, ret->get_forall() ); 902 903 895 return ret; 904 896 } … … 909 901 buildTypeList( tuple->members, ret->get_types() ); 910 902 buildForall( forall, ret->get_forall() ); 911 912 903 return ret; 913 904 } … … 918 909 assert( typeexpr->expr ); 919 910 TypeofType *ret = new TypeofType( buildQualifiers(), typeexpr->expr->build() ); 920 921 911 return ret; 922 912 } … … 931 921 assert( attr->type ); 932 922 ret = new AttrType( buildQualifiers(), attr->name, attr->type->buildType() ); 933 } 934 923 } // if 935 924 return ret; 936 925 } -
src/Parser/TypeData.h
reb50842 r937e51d 10 10 // Created On : Sat May 16 15:18:36 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Sat May 16 15:20:00201513 // Update Count : 212 // Last Modified On : Thu Jun 18 21:03:18 2015 13 // Update Count : 7 14 14 // 15 15 … … 44 44 45 45 struct Aggregate_t { 46 DeclarationNode:: TyConkind;46 DeclarationNode::Aggregate kind; 47 47 std::string name; 48 48 DeclarationNode *params; … … 120 120 TypeData *extractAggregate( bool toplevel = true ) const; 121 121 // helper function for DeclNodeImpl::build 122 Declaration * buildDecl( std::string name, Declaration ::StorageClass sc, Expression *bitfieldWidth, bool isInline, LinkageSpec::Type linkage, Initializer *init = 0 ) const;122 Declaration * buildDecl( std::string name, DeclarationNode::StorageClass sc, Expression *bitfieldWidth, bool isInline, bool isNoreturn, LinkageSpec::Type linkage, Initializer *init = 0 ) const; 123 123 // helper functions for build() 124 124 Type::Qualifiers buildQualifiers() const; 125 Type * buildBasicType() const;125 Type * buildBasicType() const; 126 126 PointerType * buildPointer() const; 127 127 ArrayType * buildArray() const; 128 128 AggregateDecl * buildAggregate() const; 129 129 ReferenceToType * buildAggInst() const; 130 NamedTypeDecl * buildSymbolic( const std::string &name, Declaration ::StorageClass sc ) const;130 NamedTypeDecl * buildSymbolic( const std::string &name, DeclarationNode::StorageClass sc ) const; 131 131 TypeDecl* buildVariable() const; 132 132 EnumDecl* buildEnum() const; -
src/Parser/TypedefTable.cc
reb50842 r937e51d 10 10 // Created On : Sat May 16 15:20:13 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : S at May 16 15:24:03201513 // Update Count : 212 // Last Modified On : Sun Jun 21 11:46:15 2015 13 // Update Count : 7 14 14 // 15 15 … … 29 29 TypedefTable::TypedefTable() : currentScope( 0 ) {} 30 30 31 bool TypedefTable::isKind( stringidentifier, kind_t kind ) const {31 bool TypedefTable::isKind( const string &identifier, kind_t kind ) const { 32 32 tableType::const_iterator id_pos = table.find( identifier ); 33 if ( id_pos == table.end() ) {33 if ( id_pos == table.end() ) { 34 34 return true; 35 35 } else { 36 36 return (*((*id_pos ).second.begin())).kind == kind; 37 } 37 } // if 38 38 } 39 39 40 bool TypedefTable::isIdentifier( stringidentifier ) const {40 bool TypedefTable::isIdentifier( const string &identifier ) const { 41 41 return isKind( identifier, ID ); 42 42 } 43 43 44 bool TypedefTable::isTypedef( stringidentifier ) const {44 bool TypedefTable::isTypedef( const string &identifier ) const { 45 45 return isKind( identifier, TD ); 46 46 } 47 47 48 bool TypedefTable::isTypegen( stringidentifier ) const {48 bool TypedefTable::isTypegen( const string &identifier ) const { 49 49 return isKind( identifier, TG ); 50 50 } … … 66 66 while ( listPos != (*curPos ).second.end() && listPos->scope > scope ) { 67 67 listPos++; 68 } 68 } // while 69 69 (*curPos ).second.insert( listPos, newEntry ); 70 } 71 } 70 } // if 71 } // if 72 72 } 73 73 … … 102 102 } 103 103 104 void TypedefTable::openContext( std::stringcontextName ) {104 void TypedefTable::openContext( const std::string &contextName ) { 105 105 map< string, deferListType >::iterator i = contexts.find( contextName ); 106 106 if ( i != contexts.end() ) { … … 108 108 for ( deferListType::iterator i = entries.begin(); i != entries.end(); i++) { 109 109 addToEnclosingScope( i->identifier, i->kind ); 110 } 111 } 110 } // for 111 } // if 112 112 } 113 113 114 void TypedefTable::enterScope( void) {114 void TypedefTable::enterScope() { 115 115 currentScope += 1; 116 116 deferListStack.push( deferListType() ); … … 119 119 } 120 120 121 void TypedefTable::leaveScope( void) {121 void TypedefTable::leaveScope() { 122 122 debugPrint( "Leaving scope " << currentScope << endl ); 123 123 for ( tableType::iterator i = table.begin(); i != table.end(); ) { … … 129 129 table.erase( i++ ); 130 130 } else ++i; 131 } 131 } // for 132 132 currentScope -= 1; 133 133 for ( deferListType::iterator i = deferListStack.top().begin(); i != deferListStack.top().end(); i++) { 134 134 addToCurrentScope( i->identifier, i->kind ); 135 } 135 } // for 136 136 deferListStack.pop(); 137 137 debugPrint( "nextIdentifiers size is " << nextIdentifiers.size() << " top is " << nextIdentifiers.top() << endl ); … … 139 139 } 140 140 141 void TypedefTable::enterContext( std::stringcontextName ) {141 void TypedefTable::enterContext( const std::string &contextName ) { 142 142 currentContext = contextName; 143 143 contextScope = currentScope; 144 144 } 145 145 146 void TypedefTable::leaveContext( void) {146 void TypedefTable::leaveContext() { 147 147 currentContext = ""; 148 148 } … … 156 156 } 157 157 debugPrint( endl ); 158 } 158 } // for 159 159 } 160 160 -
src/Parser/TypedefTable.h
reb50842 r937e51d 10 10 // Created On : Sat May 16 15:24:36 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Sat May 16 15:25:59201513 // Update Count : 312 // Last Modified On : Thu Jun 18 21:03:17 2015 13 // Update Count : 7 14 14 // 15 15 … … 49 49 std::stack< std::string > nextIdentifiers; 50 50 51 bool isKind( std::stringidentifier, kind_t kind ) const;51 bool isKind( const std::string &identifier, kind_t kind ) const; 52 52 void addToScope( const std::string &identifier, kind_t kind, int scope ); 53 53 public: 54 54 TypedefTable(); 55 55 56 bool isIdentifier( std::stringidentifier ) const;57 bool isTypedef( std::stringidentifier ) const;58 bool isTypegen( std::stringidentifier ) const;56 bool isIdentifier( const std::string &identifier ) const; 57 bool isTypedef( const std::string &identifier ) const; 58 bool isTypegen( const std::string &identifier ) const; 59 59 60 // "addToCurrentScope" adds the identifier/type pair to the current scope This does less than you think it does,60 // "addToCurrentScope" adds the identifier/type pair to the current scope. This does less than you think it does, 61 61 // since each declaration is within its own scope. Mostly useful for type parameters. 62 62 void addToCurrentScope( const std::string &identifier, kind_t kind ); … … 77 77 78 78 // dump the definitions from a pre-defined context into the current scope 79 void openContext( std::stringcontextName );79 void openContext( const std::string &contextName ); 80 80 81 81 void enterScope(); 82 82 void leaveScope(); 83 void enterContext( std::stringcontextName );83 void enterContext( const std::string &contextName ); 84 84 void leaveContext(); 85 85 -
src/Parser/lex.h
reb50842 r937e51d 10 10 // Created On : Sat Sep 22 08:58:10 2001 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Sat May 16 12:18:48 201513 // Update Count : 3 3412 // Last Modified On : Mon Jun 8 20:28:48 2015 13 // Update Count : 341 14 14 // 15 15 … … 18 18 19 19 int yylex(); 20 void yyerror( char *);20 void yyerror( const char * ); 21 21 22 22 // External declarations for information sharing between lexer and scanner … … 35 35 class Token { 36 36 public: 37 std::string *str; 37 std::string *str; // must be pointer as used in union 38 38 Location loc; 39 39 … … 44 44 45 45 // Local Variables: // 46 // fill-column: 110 //47 46 // tab-width: 4 // 48 47 // mode: c++ // -
src/Parser/lex.ll
reb50842 r937e51d 10 10 * Created On : Sat Sep 22 08:58:10 2001 11 11 * Last Modified By : Peter A. Buhr 12 * Last Modified On : Tue May 19 15:41:54 201513 * Update Count : 3 3112 * Last Modified On : Fri Jun 19 11:10:14 2015 13 * Update Count : 392 14 14 */ 15 15 16 16 %option yylineno 17 %option nounput 17 18 18 19 %{ 19 // This lexer assumes the program has been preprocessed by cpp. Hence, all user level preprocessor directive 20 // have been performed and removed from the source. The only exceptions are preprocessor directives passed to21 // the compiler (e.g.,line-number directives) and C/C++ style comments, which are ignored.20 // This lexer assumes the program has been preprocessed by cpp. Hence, all user level preprocessor directive have been 21 // performed and removed from the source. The only exceptions are preprocessor directives passed to the compiler (e.g., 22 // line-number directives) and C/C++ style comments, which are ignored. 22 23 23 24 //**************************** Includes and Defines **************************** … … 27 28 #include "lex.h" 28 29 #include "ParseNode.h" 29 #include " cfa.tab.h"// YACC generated definitions based on C++ grammar30 #include "parser.h" // YACC generated definitions based on C++ grammar 30 31 31 32 char *yyfilename; 32 33 std::string *strtext; // accumulate parts of character and string constant value 33 34 35 #define RETURN_LOCN(x) yylval.tok.loc.file = yyfilename; yylval.tok.loc.line = yylineno; return( x ) 36 #define RETURN_VAL(x) yylval.tok.str = new std::string( yytext ); RETURN_LOCN( x ) 37 #define RETURN_CHAR(x) yylval.tok.str = NULL; RETURN_LOCN( x ) 38 #define RETURN_STR(x) yylval.tok.str = strtext; RETURN_LOCN( x ) 39 34 40 #define WHITE_RETURN(x) // do nothing 35 #define NEWLINE_RETURN() WHITE_RETURN('\n') 36 #define RETURN_VAL(x) yylval.tok.str = new std::string(yytext); \ 37 yylval.tok.loc.file = yyfilename; \ 38 yylval.tok.loc.line = yylineno; \ 39 return(x) 40 #define RETURN_STR(x) yylval.tok.str = strtext; \ 41 yylval.tok.loc.file = yyfilename; \ 42 yylval.tok.loc.line = yylineno; \ 43 return(x) 44 45 #define KEYWORD_RETURN(x) RETURN_VAL(x) // keyword 46 #define IDENTIFIER_RETURN() RETURN_VAL((typedefTable.isIdentifier(yytext) ? IDENTIFIER : typedefTable.isTypedef(yytext) ? TYPEDEFname : TYPEGENname)) 47 //#define ATTRIBUTE_RETURN() RETURN_VAL((typedefTable.isIdentifier(yytext) ? ATTR_IDENTIFIER : typedefTable.isTypedef(yytext) ? ATTR_TYPEDEFname : ATTR_TYPEGENname)) 48 #define ATTRIBUTE_RETURN() RETURN_VAL(ATTR_IDENTIFIER) 49 50 #define ASCIIOP_RETURN() RETURN_VAL((int)yytext[0]) // single character operator 51 #define NAMEDOP_RETURN(x) RETURN_VAL(x) // multichar operator, with a name 52 53 #define NUMERIC_RETURN(x) rm_underscore(); RETURN_VAL(x) // numeric constant 41 #define NEWLINE_RETURN() WHITE_RETURN( '\n' ) 42 #define ASCIIOP_RETURN() RETURN_CHAR( (int)yytext[0] ) // single character operator 43 #define NAMEDOP_RETURN(x) RETURN_VAL( x ) // multichar operator, with a name 44 #define NUMERIC_RETURN(x) rm_underscore(); RETURN_VAL( x ) // numeric constant 45 #define KEYWORD_RETURN(x) RETURN_CHAR( x ) // keyword 46 #define IDENTIFIER_RETURN() RETURN_VAL( (typedefTable.isIdentifier( yytext ) ? IDENTIFIER : typedefTable.isTypedef( yytext ) ? TYPEDEFname : TYPEGENname ) ) 47 #define ATTRIBUTE_RETURN() RETURN_VAL( ATTR_IDENTIFIER ) 54 48 55 49 void rm_underscore() { … … 114 108 hex_escape "\\""x""_"?{hex_digits} 115 109 escape_seq {simple_escape}|{octal_escape}|{hex_escape}|{universal_char} 110 cwide_prefix "L"|"U"|"u" 111 swide_prefix {cwide_prefix}|"u8" 116 112 117 113 // display/white-space characters … … 165 161 ^{h_white}*"#"[^\n]*"\n" ; 166 162 167 /* ignore C style comments */163 /* ignore C style comments (ALSO HANDLED BY CPP) */ 168 164 "/*" { BEGIN COMMENT; } 169 <COMMENT>.|\n ;170 <COMMENT>"*/" { BEGIN 0; }171 172 /* ignore C++ style comments */173 "//"[^\n]*"\n" ;165 <COMMENT>.|\n ; 166 <COMMENT>"*/" { BEGIN 0; } 167 168 /* ignore C++ style comments (ALSO HANDLED BY CPP) */ 169 "//"[^\n]*"\n" ; 174 170 175 171 /* ignore whitespace */ … … 275 271 "0" { NUMERIC_RETURN(ZERO); } // CFA 276 272 "1" { NUMERIC_RETURN(ONE); } // CFA 277 {decimal_constant} { NUMERIC_RETURN(INTEGERconstant); }278 {octal_constant} { NUMERIC_RETURN(INTEGERconstant); }279 {hex_constant} { NUMERIC_RETURN(INTEGERconstant); }273 {decimal_constant} { NUMERIC_RETURN(INTEGERconstant); } 274 {octal_constant} { NUMERIC_RETURN(INTEGERconstant); } 275 {hex_constant} { NUMERIC_RETURN(INTEGERconstant); } 280 276 {floating_constant} { NUMERIC_RETURN(FLOATINGconstant); } 281 277 {hex_floating_constant} { NUMERIC_RETURN(FLOATINGconstant); } 282 278 283 279 /* character constant, allows empty value */ 284 "L"?"_"?[']{ BEGIN QUOTE; rm_underscore(); strtext = new std::string; *strtext += std::string( yytext ); }280 ({cwide_prefix}[_]?)?['] { BEGIN QUOTE; rm_underscore(); strtext = new std::string; *strtext += std::string( yytext ); } 285 281 <QUOTE>[^'\\\n]* { *strtext += std::string( yytext ); } 286 282 <QUOTE>['\n] { BEGIN 0; *strtext += std::string( yytext); RETURN_STR(CHARACTERconstant); } … … 288 284 289 285 /* string constant */ 290 "L"?"_"?["]{ BEGIN STRING; rm_underscore(); strtext = new std::string; *strtext += std::string( yytext ); }286 ({swide_prefix}[_]?)?["] { BEGIN STRING; rm_underscore(); strtext = new std::string; *strtext += std::string( yytext ); } 291 287 <STRING>[^"\\\n]* { *strtext += std::string( yytext ); } 292 <STRING>["\n] { BEGIN 0; *strtext += std::string( yytext ); RETURN_STR(STRINGliteral); }288 <STRING>["\n] { BEGIN 0; *strtext += std::string( yytext ); RETURN_STR(STRINGliteral); } 293 289 /* " stop highlighting */ 294 290 291 /* common character/string constant */ 295 292 <QUOTE,STRING>{escape_seq} { rm_underscore(); *strtext += std::string( yytext ); } 296 <QUOTE,STRING>[\\] { *strtext += std::string( yytext ); } // unknown escape character 293 <QUOTE,STRING>"\\"{h_white}*"\n" {} // continuation (ALSO HANDLED BY CPP) 294 <QUOTE,STRING>"\\" { *strtext += std::string( yytext ); } // unknown escape character 297 295 298 296 /* punctuation */ … … 355 353 /* CFA, operator identifier */ 356 354 {op_unary}"?" { IDENTIFIER_RETURN(); } // unary 357 "?"({op_unary_pre_post}|"()"|"[?]" ) { IDENTIFIER_RETURN(); }355 "?"({op_unary_pre_post}|"()"|"[?]"|"{}") { IDENTIFIER_RETURN(); } 358 356 "?"{op_binary_over}"?" { IDENTIFIER_RETURN(); } // binary 359 357 /* … … 400 398 401 399 // Local Variables: // 402 // fill-column: 110//400 // mode: c++ // 403 401 // tab-width: 4 // 404 // mode: c++ //405 402 // compile-command: "make install" // 406 403 // End: // -
src/Parser/module.mk
reb50842 r937e51d 8 8 ## module.mk -- 9 9 ## 10 ## Author : Richard C. Bilson10 ## Author : Peter A. Buhr 11 11 ## Created On : Sat May 16 15:29:09 2015 12 12 ## Last Modified By : Peter A. Buhr 13 ## Last Modified On : Thu May 21 21:17:07 201514 ## Update Count : 213 ## Last Modified On : Mon Jun 8 20:23:47 2015 14 ## Update Count : 87 15 15 ############################################################################### 16 16 17 YACC=bison 18 YFLAGS=-d --debug -v 19 LEX=flex 20 LFLAGS= 17 BUILT_SOURCES = Parser/parser.h 21 18 22 SRC += Parser/cfa.y \ 23 Parser/lex.l \ 19 AM_YFLAGS = -d -t -v 20 cfa_cpp_LDADD = ${LEXLIB} # yywrap 21 MAINTAINERCLEANFILES = Parser/parser.output 22 23 SRC += Parser/parser.yy \ 24 Parser/lex.ll \ 24 25 Parser/TypedefTable.cc \ 25 26 Parser/ParseNode.cc \ … … 32 33 Parser/parseutility.cc \ 33 34 Parser/Parser.cc 34 35 EXTRA_OUTPUT += Parser/cfa.tab.cc \36 Parser/cfa.tab.h \37 Parser/lex.yy.cc \38 Parser/cfa.output39 40 LIBS += -lfl41 42 Parser/Parser.cc: Parser/cfa.tab.h43 44 Parser/cfa.tab.cc: Parser/cfa.y45 $(YACC) $(YFLAGS) $< --file-prefix=Parser/cfa46 -mv Parser/cfa.tab.c Parser/cfa.tab.cc47 48 Parser/cfa.tab.h: Parser/cfa.tab.cc49 50 Parser/lex.yy.cc: Parser/lex.l Parser/cfa.tab.h Parser/TypedefTable.h51 $(LEX) $(LFLAGS) -o$@ $<52 53 Parser/lex.yy.o: Parser/lex.yy.cc Parser/ParseNode.h54 $(CXX) $(CXXFLAGS) -Wno-unused -c -o $@ $<
Note:
See TracChangeset
for help on using the changeset viewer.