Changes in src/Parser/DeclarationNode.cc [de62360d:843054c2]
- File:
-
- 1 edited
-
src/Parser/DeclarationNode.cc (modified) (53 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/Parser/DeclarationNode.cc
rde62360d r843054c2 10 10 // Created On : Sat May 16 12:34:05 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed Jun 24 15:29:19201513 // Update Count : 8612 // Last Modified On : Thu May 21 09:28:54 2015 13 // Update Count : 13 14 14 // 15 15 … … 21 21 22 22 #include "TypeData.h" 23 24 #include "SynTree/Declaration.h"25 23 #include "SynTree/Expression.h" 26 24 27 #include "Parser.h"28 #include "TypedefTable.h"29 extern TypedefTable typedefTable;30 25 31 26 using namespace std; 32 27 33 28 // 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", "" };35 29 const char *DeclarationNode::qualifierName[] = { "const", "restrict", "volatile", "lvalue", "_Atomic" }; 36 30 const char *DeclarationNode::basicTypeName[] = { "char", "int", "float", "double", "void", "_Bool", "_Complex", "_Imaginary" }; 37 const char *DeclarationNode::modifierName[] = { "signed", "unsigned", "short", "long" };38 const char *DeclarationNode:: aggregateName[] = { "struct", "union", "context" };31 const char *DeclarationNode::modifierName[] = { "signed", "unsigned", "short", "long" }; 32 const char *DeclarationNode::tyConName[] = { "struct", "union", "context" }; 39 33 const char *DeclarationNode::typeClassName[] = { "type", "dtype", "ftype" }; 40 34 … … 69 63 } 70 64 65 const char *storageClassName[] = { 66 // order must correspond with DeclarationNode::StorageClass 67 "extern", 68 "static", 69 "auto", 70 "register", 71 "inline", 72 "fortran", 73 }; 74 71 75 void DeclarationNode::print( std::ostream &os, int indent ) const { 72 os << string( indent, ' ' );76 os << string(indent, ' ' ); 73 77 if ( name == "" ) { 74 78 os << "unnamed: "; 75 79 } else { 76 80 os << name << ": "; 77 } // if81 } 78 82 79 83 if ( linkage != LinkageSpec::Cforall ) { 80 84 os << LinkageSpec::toString( linkage ) << " "; 81 } // if82 83 printEnums( storageClasses.begin(), storageClasses.end(), DeclarationNode::storageName, os );85 } 86 87 printEnums( storageClasses.begin(), storageClasses.end(), storageClassName, os ); 84 88 if ( type ) { 85 89 type->print( os, indent ); 86 90 } else { 87 91 os << "untyped entity "; 88 } // if92 } 89 93 90 94 if ( bitfieldWidth ) { 91 os << endl << string( indent + 2, ' ') << "with bitfield width ";95 os << endl << string(indent+2, ' ') << "with bitfield width "; 92 96 bitfieldWidth->printOneLine( os ); 93 } // if97 } 94 98 95 99 if ( initializer != 0 ) { 96 os << endl << string( indent + 2, ' ') << "with initializer ";100 os << endl << string(indent+2, ' ') << "with initializer "; 97 101 initializer->printOneLine( os ); 98 } // if102 } 99 103 100 104 os << endl; … … 105 109 if ( hasEllipsis ) { 106 110 os << string( indent, ' ' ) << "and a variable number of other arguments" << endl; 107 } // if111 } 108 112 } 109 113 … … 119 123 if ( body ) { 120 124 newnode->type->function->hasBody = true; 121 } // if125 } 122 126 123 127 if ( ret ) { … … 125 129 ret->type = 0; 126 130 delete ret; 127 } // if131 } 128 132 129 133 return newnode; … … 137 141 } 138 142 139 DeclarationNode *DeclarationNode::newStorageClass( DeclarationNode::StorageClass sc ) {143 DeclarationNode *DeclarationNode::newStorageClass( StorageClass sc ) { 140 144 DeclarationNode *newnode = new DeclarationNode; 141 145 newnode->storageClasses.push_back( sc ); … … 157 161 } 158 162 159 DeclarationNode *DeclarationNode::newForall( DeclarationNode *forall ) {163 DeclarationNode *DeclarationNode::newForall( DeclarationNode* forall ) { 160 164 DeclarationNode *newnode = new DeclarationNode; 161 165 newnode->type = new TypeData( TypeData::Unknown ); … … 164 168 } 165 169 166 DeclarationNode *DeclarationNode::newFromTypedef( std::string *name ) {170 DeclarationNode *DeclarationNode::newFromTypedef( std::string* name ) { 167 171 DeclarationNode *newnode = new DeclarationNode; 168 172 newnode->type = new TypeData( TypeData::SymbolicInst ); … … 173 177 } 174 178 175 DeclarationNode *DeclarationNode::newAggregate( Aggregate kind, std::string *name, DeclarationNode *formals, ExpressionNode *actuals, DeclarationNode *fields ) {179 DeclarationNode *DeclarationNode::newAggregate( TyCon kind, std::string* name, DeclarationNode *formals, ExpressionNode *actuals, DeclarationNode *fields ) { 176 180 DeclarationNode *newnode = new DeclarationNode; 177 181 newnode->type = new TypeData( TypeData::Aggregate ); … … 180 184 if ( newnode->type->aggregate->name == "" ) { 181 185 newnode->type->aggregate->name = DeclarationNode::anonymous.newName(); 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 186 } 191 187 newnode->type->aggregate->params = formals; 192 188 newnode->type->aggregate->actuals = actuals; … … 202 198 if ( newnode->type->enumeration->name == "" ) { 203 199 newnode->type->enumeration->name = DeclarationNode::anonymous.newName(); 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 200 } 213 201 newnode->type->enumeration->constants = constants; 214 202 return newnode; 215 203 } 216 204 217 DeclarationNode *DeclarationNode::newEnumConstant( std::string *name, ExpressionNode *constant ) {205 DeclarationNode *DeclarationNode::newEnumConstant( std::string* name, ExpressionNode *constant ) { 218 206 DeclarationNode *newnode = new DeclarationNode; 219 207 newnode->name = assign_strptr( name ); … … 222 210 } 223 211 224 DeclarationNode *DeclarationNode::newName( std::string *name ) {212 DeclarationNode *DeclarationNode::newName( std::string* name ) { 225 213 DeclarationNode *newnode = new DeclarationNode; 226 214 newnode->name = assign_strptr( name ); … … 228 216 } 229 217 230 DeclarationNode *DeclarationNode::newFromTypeGen( std::string *name, ExpressionNode *params ) {218 DeclarationNode *DeclarationNode::newFromTypeGen( std::string* name, ExpressionNode *params ) { 231 219 DeclarationNode *newnode = new DeclarationNode; 232 220 newnode->type = new TypeData( TypeData::SymbolicInst ); … … 237 225 } 238 226 239 DeclarationNode *DeclarationNode::newTypeParam( TypeClass tc, std::string *name ) {227 DeclarationNode *DeclarationNode::newTypeParam( TypeClass tc, std::string* name ) { 240 228 DeclarationNode *newnode = new DeclarationNode; 241 229 newnode->name = assign_strptr( name ); … … 343 331 } else { 344 332 dst->forall = src->forall; 345 } // if333 } 346 334 src->forall = 0; 347 } // if335 } 348 336 if ( dst->base ) { 349 337 addQualifiersToType( src, dst->base ); … … 353 341 } else { 354 342 dst->qualifiers.splice( dst->qualifiers.end(), src->qualifiers ); 355 } // if356 } // if343 } 344 } 357 345 } 358 346 … … 363 351 if ( ! type ) { 364 352 type = new TypeData; 365 } // if353 } 366 354 addQualifiersToType( q->type, type ); 367 355 if ( q->type && q->type->forall ) { … … 369 357 type->forall->appendList( q->type->forall ); 370 358 } else { 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 359 type->forall = q->type->forall; 360 } 377 361 q->type->forall = 0; 378 } // if379 } // if380 } // if362 } 363 } 364 } 381 365 delete q; 382 366 return this; … … 395 379 } else { 396 380 dst->forall = src->forall; 397 } // if381 } 398 382 src->forall = 0; 399 } // if383 } 400 384 if ( dst->base ) { 401 385 addTypeToType( src, dst->base ); … … 414 398 dst->basic->modifiers.splice( dst->basic->modifiers.end(), src->basic->modifiers ); 415 399 dst->basic->typeSpec.splice( dst->basic->typeSpec.end(), src->basic->typeSpec ); 416 } // if400 } 417 401 break; 418 402 … … 425 409 if ( src->kind == TypeData::Aggregate ) { 426 410 dst->base->aggInst->params = maybeClone( src->aggregate->actuals ); 427 } // if411 } 428 412 dst->base->qualifiers.splice( dst->base->qualifiers.end(), src->qualifiers ); 429 413 src = 0; … … 435 419 } else { 436 420 dst->forall = src->forall; 437 } // if421 } 438 422 src->forall = 0; 439 423 dst->base = src; 440 424 src = 0; 441 } // switch442 } // switch443 } // if444 } // if425 } 426 } 427 } 428 } 445 429 } 446 430 … … 455 439 if ( o->type->kind == TypeData::Aggregate ) { 456 440 type->aggInst->params = maybeClone( o->type->aggregate->actuals ); 457 } // if441 } 458 442 type->qualifiers.splice( type->qualifiers.end(), o->type->qualifiers ); 459 443 } else { 460 444 type = o->type; 461 } // if445 } 462 446 o->type = 0; 463 447 } else { 464 448 addTypeToType( o->type, type ); 465 } // if466 } // if449 } 450 } 467 451 if ( o->bitfieldWidth ) { 468 452 bitfieldWidth = o->bitfieldWidth; 469 } // if470 } // if453 } 454 } 471 455 delete o; 472 456 return this; … … 483 467 } 484 468 485 DeclarationNode *DeclarationNode::addAssertions( DeclarationNode *assertions ) {469 DeclarationNode *DeclarationNode::addAssertions( DeclarationNode* assertions ) { 486 470 assert( type ); 487 471 switch ( type->kind ) { … … 491 475 } else { 492 476 type->symbolic->assertions = assertions; 493 } // if477 } 494 478 break; 479 495 480 case TypeData::Variable: 496 481 if ( type->variable->assertions ) { … … 498 483 } else { 499 484 type->variable->assertions = assertions; 500 } // if485 } 501 486 break; 487 502 488 default: 503 489 assert( false ); 504 } // switch490 } 505 491 506 492 return this; 507 493 } 508 494 509 DeclarationNode *DeclarationNode::addName( std::string *newname ) {495 DeclarationNode *DeclarationNode::addName( std::string* newname ) { 510 496 name = assign_strptr( newname ); 511 497 return this; … … 540 526 } 541 527 542 static void setBase( TypeData *&type, TypeData *newType ) { 528 static void 529 setBase( TypeData *&type, TypeData *newType ) { 543 530 if ( type ) { 544 531 TypeData *prevBase = type; … … 547 534 prevBase = curBase; 548 535 curBase = curBase->base; 549 } // while536 } 550 537 prevBase->base = newType; 551 538 } else { 552 539 type = newType; 553 } // if540 } 554 541 } 555 542 … … 560 547 p->type = 0; 561 548 delete p; 562 } // if549 } 563 550 return this; 564 551 } … … 570 557 a->type = 0; 571 558 delete a; 572 } // if559 } 573 560 return this; 574 561 } … … 585 572 if ( type->kind == TypeData::Aggregate ) { 586 573 p->type->base->aggInst->params = maybeClone( type->aggregate->actuals ); 587 } // if574 } 588 575 p->type->base->qualifiers.splice( p->type->base->qualifiers.end(), type->qualifiers ); 589 576 break; … … 591 578 default: 592 579 p->type->base = type; 593 } // switch580 } 594 581 type = 0; 595 } // if582 } 596 583 delete this; 597 584 return p; 598 585 } else { 599 586 return this; 600 } // if587 } 601 588 } 602 589 … … 606 593 while ( cur->base ) { 607 594 cur = cur->base; 608 } // while595 } 609 596 return cur; 610 597 } … … 622 609 if ( type->kind == TypeData::Aggregate ) { 623 610 lastArray->base->aggInst->params = maybeClone( type->aggregate->actuals ); 624 } // if611 } 625 612 lastArray->base->qualifiers.splice( lastArray->base->qualifiers.end(), type->qualifiers ); 626 613 break; 627 614 default: 628 615 lastArray->base = type; 629 } // switch616 } 630 617 type = 0; 631 } // if618 } 632 619 delete this; 633 620 return a; 634 621 } else { 635 622 return this; 636 } // if623 } 637 624 } 638 625 … … 650 637 } else { 651 638 type->function->idList = ids; 652 } // if639 } 653 640 return type; 654 641 } else { … … 656 643 newtype->function->idList = ids; 657 644 return newtype; 658 } // if645 } 659 646 } 660 647 … … 675 662 while ( srcType->base ) { 676 663 srcType = srcType->base; 677 } // while664 } 678 665 newnode->type = maybeClone( srcType ); 679 666 if ( newnode->type->kind == TypeData::AggregateInst ) { … … 686 673 delete newnode->type->aggInst->aggregate->aggregate->members; 687 674 newnode->type->aggInst->aggregate->aggregate->members = 0; 688 } // if689 } // if675 } 676 } 690 677 newnode->type->forall = maybeClone( type->forall ); 691 678 newnode->storageClasses = storageClasses; … … 701 688 while ( srcType->base ) { 702 689 srcType = srcType->base; 703 } // while690 } 704 691 TypeData *newType = srcType->clone(); 705 692 if ( newType->kind == TypeData::AggregateInst ) { … … 712 699 delete newType->aggInst->aggregate->aggregate->members; 713 700 newType->aggInst->aggregate->aggregate->members = 0; 714 } // if715 } // if701 } 702 } 716 703 newType->forall = maybeClone( type->forall ); 717 704 if ( ! o->type ) { … … 720 707 addTypeToType( newType, o->type ); 721 708 delete newType; 722 } // if723 } // if724 } // if709 } 710 } 711 } 725 712 return o; 726 713 } … … 744 731 addTypeToType( newType, o->type ); 745 732 delete newType; 746 } // if747 } // if748 } // if733 } 734 } 735 } 749 736 return o; 750 737 } … … 753 740 if ( node != 0 ) { 754 741 set_link( node ); 755 } // if742 } 756 743 return this; 757 744 } … … 769 756 } 770 757 771 void buildList( const DeclarationNode *firstNode, std::list< Declaration * > &outputList ) {758 void buildList( const DeclarationNode *firstNode, std::list< Declaration* > &outputList ) { 772 759 SemanticError errors; 773 std::back_insert_iterator< std::list< Declaration *> > out( outputList );760 std::back_insert_iterator< std::list< Declaration* > > out( outputList ); 774 761 const DeclarationNode *cur = firstNode; 775 762 while ( cur ) { … … 789 776 errors.append( e ); 790 777 } // try 791 cur = dynamic_cast< DeclarationNode *>( cur->get_link() );778 cur = dynamic_cast< DeclarationNode* >( cur->get_link() ); 792 779 } // while 793 780 if ( ! errors.isEmpty() ) { … … 796 783 } 797 784 798 void buildList( const DeclarationNode *firstNode, std::list< DeclarationWithType *> &outputList ) {785 void buildList( const DeclarationNode *firstNode, std::list< DeclarationWithType* > &outputList ) { 799 786 SemanticError errors; 800 std::back_insert_iterator< std::list< DeclarationWithType *> > out( outputList );787 std::back_insert_iterator< std::list< DeclarationWithType* > > out( outputList ); 801 788 const DeclarationNode *cur = firstNode; 802 789 while ( cur ) { … … 812 799 Declaration *decl = cur->build(); 813 800 if ( decl ) { 814 if ( DeclarationWithType *dwt = dynamic_cast< DeclarationWithType *>( decl ) ) {801 if ( DeclarationWithType *dwt = dynamic_cast< DeclarationWithType* >( decl ) ) { 815 802 *out++ = dwt; 816 } else if ( StructDecl *agg = dynamic_cast< StructDecl *>( decl ) ) {803 } else if ( StructDecl *agg = dynamic_cast< StructDecl* >( decl ) ) { 817 804 StructInstType *inst = new StructInstType( Type::Qualifiers(), agg->get_name() ); 818 *out++ = new ObjectDecl( "", Declaration Node::NoStorageClass, linkage, 0, inst, 0 );805 *out++ = new ObjectDecl( "", Declaration::NoStorageClass, linkage, 0, inst, 0 ); 819 806 delete agg; 820 } else if ( UnionDecl *agg = dynamic_cast< UnionDecl *>( decl ) ) {807 } else if ( UnionDecl *agg = dynamic_cast< UnionDecl* >( decl ) ) { 821 808 UnionInstType *inst = new UnionInstType( Type::Qualifiers(), agg->get_name() ); 822 *out++ = new ObjectDecl( "", Declaration Node::NoStorageClass, linkage, 0, inst, 0 );809 *out++ = new ObjectDecl( "", Declaration::NoStorageClass, linkage, 0, inst, 0 ); 823 810 } // if 824 811 } // if … … 826 813 errors.append( e ); 827 814 } // try 828 cur = dynamic_cast< DeclarationNode *>( cur->get_link() );815 cur = dynamic_cast< DeclarationNode* >( cur->get_link() ); 829 816 } // while 830 817 if ( ! errors.isEmpty() ) { … … 833 820 } 834 821 835 void buildTypeList( const DeclarationNode *firstNode, std::list< Type *> &outputList ) {822 void buildTypeList( const DeclarationNode *firstNode, std::list< Type* > &outputList ) { 836 823 SemanticError errors; 837 std::back_insert_iterator< std::list< Type *> > out( outputList );824 std::back_insert_iterator< std::list< Type* > > out( outputList ); 838 825 const DeclarationNode *cur = firstNode; 839 826 while ( cur ) { … … 843 830 errors.append( e ); 844 831 } // try 845 cur = dynamic_cast< DeclarationNode *>( cur->get_link() );832 cur = dynamic_cast< DeclarationNode* >( cur->get_link() ); 846 833 } // while 847 834 if ( ! errors.isEmpty() ) { … … 852 839 Declaration *DeclarationNode::build() const { 853 840 if ( type ) { 854 Declaration *newDecl = type->buildDecl( name, buildStorageClass(), maybeBuild< Expression >( bitfieldWidth ), build FuncSpecifier( Inline ), buildFuncSpecifier( Noreturn), linkage, maybeBuild< Initializer >(initializer) );841 Declaration *newDecl = type->buildDecl( name, buildStorageClass(), maybeBuild< Expression >( bitfieldWidth ), buildInline(), linkage, maybeBuild< Initializer >(initializer) ); 855 842 return newDecl; 856 843 } // if 857 if ( ! build FuncSpecifier( Inline ) && ! buildFuncSpecifier( Noreturn) ) {844 if ( ! buildInline() ) { 858 845 return new ObjectDecl( name, buildStorageClass(), linkage, maybeBuild< Expression >( bitfieldWidth ), 0, maybeBuild< Initializer >( initializer ) ); 859 846 } // if 860 throw SemanticError( "invalid function specifierin declaration of ", this );847 throw SemanticError( "invalid inline specification in declaration of ", this ); 861 848 } 862 849 … … 895 882 } 896 883 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 ? 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 ) { 902 899 throw SemanticError( "invalid combination of storage classes in declaration of ", this ); 903 } // if904 ret = *i;905 } // for900 } 901 ret = scMap[ *i ]; 902 } 906 903 return ret; 907 904 } 908 905 909 bool DeclarationNode::build FuncSpecifier( 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 found912 first = std::find( ++first, storageClasses.end(), key ); // found913 if ( first == storageClasses.end() ) return true; // not found again914 throw SemanticError( "duplicate function specifierin declaration of ", this );906 bool DeclarationNode::buildInline() 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 specification in declaration of ", this ); 915 912 } 916 913
Note:
See TracChangeset
for help on using the changeset viewer.