Changes in src/Parser/DeclarationNode.cc [843054c2:de62360d]
- File:
-
- 1 edited
-
src/Parser/DeclarationNode.cc (modified) (53 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/Parser/DeclarationNode.cc
r843054c2 rde62360d 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
Note:
See TracChangeset
for help on using the changeset viewer.