Changes in src/Parser/DeclarationNode.cc [4eb3a7c5:af60383]
- File:
-
- 1 edited
-
src/Parser/DeclarationNode.cc (modified) (16 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/Parser/DeclarationNode.cc
r4eb3a7c5 raf60383 82 82 83 83 DeclarationNode::~DeclarationNode() { 84 delete name; 85 84 86 // delete variable.name; 85 87 delete variable.assertions; … … 99 101 DeclarationNode * DeclarationNode::clone() const { 100 102 DeclarationNode * newnode = new DeclarationNode; 101 newnode-> set_next( maybeCopy( get_next() ));103 newnode->next = maybeCopy( next ); 102 104 newnode->name = name ? new string( *name ) : nullptr; 103 105 104 newnode->builtin = NoBuiltinType;105 106 newnode->type = maybeCopy( type ); 106 107 newnode->inLine = inLine; … … 170 171 171 172 void DeclarationNode::printList( std::ostream & os, int indent ) const { 172 Parse Node::printList( os, indent );173 ParseList::printList( os, indent ); 173 174 if ( hasEllipsis ) { 174 175 os << string( indent, ' ' ) << "and a variable number of other arguments" << endl; … … 433 434 DeclarationNode * newnode = new DeclarationNode; 434 435 newnode->type = new TypeData( TypeData::Builtin ); 435 newnode->builtin = bt; 436 newnode->type->builtintype = newnode->builtin; 436 newnode->type->builtintype = bt; 437 437 return newnode; 438 438 } // DeclarationNode::newBuiltinType … … 554 554 } // DeclarationNode::copySpecifiers 555 555 556 static void addQualifiersToType( TypeData *& src, TypeData * dst ) {557 if ( dst->base ) {558 addQualifiersToType( src, dst->base );559 } else if ( dst->kind == TypeData::Function ) {560 dst->base = src;561 src = nullptr;562 } else {563 dst->qualifiers |= src->qualifiers;564 } // if565 } // addQualifiersToType566 567 556 DeclarationNode * DeclarationNode::addQualifiers( DeclarationNode * q ) { 568 557 if ( ! q ) { return this; } // empty qualifier … … 580 569 } // if 581 570 582 if ( q->type->forall ) { // forall qualifier ?583 if ( type->forall ) { // polymorphic routine ?584 type->forall->appendList( q->type->forall ); // augment forall qualifier585 } else {586 if ( type->kind == TypeData::Aggregate ) { // struct/union ?587 if ( type->aggregate.params ) { // polymorphic ?588 type->aggregate.params->appendList( q->type->forall ); // augment forall qualifier589 } else { // not polymorphic590 type->aggregate.params = q->type->forall; // set forall qualifier591 } // if592 } else { // not polymorphic593 type->forall = q->type->forall; // make polymorphic routine594 } // if595 } // if596 q->type->forall = nullptr; // forall qualifier moved597 } // if598 599 571 checkQualifiers( type, q->type ); 572 BuiltinType const builtin = type->builtintype; 600 573 if ( (builtin == Zero || builtin == One) && q->type->qualifiers.any() && error.length() == 0 ) { 601 574 SemanticWarning( yylloc, Warning::BadQualifiersZeroOne, builtinTypeNames[builtin] ); 602 575 } // if 603 addQualifiersToType( q->type, type ); 576 type = ::addQualifiers( q->type, type ); 577 q->type = nullptr; 604 578 605 579 delete q; … … 607 581 } // addQualifiers 608 582 609 static void addTypeToType( TypeData *& src, TypeData *& dst ) {610 if ( src->forall && dst->kind == TypeData::Function ) {611 if ( dst->forall ) {612 dst->forall->appendList( src->forall );613 } else {614 dst->forall = src->forall;615 } // if616 src->forall = nullptr;617 } // if618 if ( dst->base ) {619 addTypeToType( src, dst->base );620 } else {621 switch ( dst->kind ) {622 case TypeData::Unknown:623 src->qualifiers |= dst->qualifiers;624 dst = src;625 src = nullptr;626 break;627 case TypeData::Basic:628 dst->qualifiers |= src->qualifiers;629 if ( src->kind != TypeData::Unknown ) {630 assert( src->kind == TypeData::Basic );631 632 if ( dst->basictype == DeclarationNode::NoBasicType ) {633 dst->basictype = src->basictype;634 } else if ( src->basictype != DeclarationNode::NoBasicType )635 SemanticError( yylloc, "multiple declaration types \"%s\" and \"%s\".",636 DeclarationNode::basicTypeNames[ dst->basictype ],637 DeclarationNode::basicTypeNames[ src->basictype ] );638 if ( dst->complextype == DeclarationNode::NoComplexType ) {639 dst->complextype = src->complextype;640 } else if ( src->complextype != DeclarationNode::NoComplexType )641 SemanticError( yylloc, "multiple declaration types \"%s\" and \"%s\".",642 DeclarationNode::complexTypeNames[ src->complextype ],643 DeclarationNode::complexTypeNames[ src->complextype ] );644 if ( dst->signedness == DeclarationNode::NoSignedness ) {645 dst->signedness = src->signedness;646 } else if ( src->signedness != DeclarationNode::NoSignedness )647 SemanticError( yylloc, "conflicting type specifier \"%s\" and \"%s\".",648 DeclarationNode::signednessNames[ dst->signedness ],649 DeclarationNode::signednessNames[ src->signedness ] );650 if ( dst->length == DeclarationNode::NoLength ) {651 dst->length = src->length;652 } else if ( dst->length == DeclarationNode::Long && src->length == DeclarationNode::Long ) {653 dst->length = DeclarationNode::LongLong;654 } else if ( src->length != DeclarationNode::NoLength )655 SemanticError( yylloc, "conflicting type specifier \"%s\" and \"%s\".",656 DeclarationNode::lengthNames[ dst->length ],657 DeclarationNode::lengthNames[ src->length ] );658 } // if659 break;660 default:661 switch ( src->kind ) {662 case TypeData::Aggregate:663 case TypeData::Enum:664 dst->base = new TypeData( TypeData::AggregateInst );665 dst->base->aggInst.aggregate = src;666 if ( src->kind == TypeData::Aggregate ) {667 dst->base->aggInst.params = maybeCopy( src->aggregate.actuals );668 } // if669 dst->base->qualifiers |= src->qualifiers;670 src = nullptr;671 break;672 default:673 if ( dst->forall ) {674 dst->forall->appendList( src->forall );675 } else {676 dst->forall = src->forall;677 } // if678 src->forall = nullptr;679 dst->base = src;680 src = nullptr;681 } // switch682 } // switch683 } // if684 }685 686 583 DeclarationNode * DeclarationNode::addType( DeclarationNode * o, bool copyattr ) { 687 if ( o ) { 688 checkSpecifiers( o ); 689 copySpecifiers( o, copyattr ); 690 if ( o->type ) { 691 if ( ! type ) { 692 if ( o->type->kind == TypeData::Aggregate || o->type->kind == TypeData::Enum ) { 693 // Hide type information aggregate instances. 694 type = new TypeData( TypeData::AggregateInst ); 695 type->aggInst.aggregate = o->type; // change ownership 696 type->aggInst.aggregate->aggregate.attributes.swap( o->attributes ); // change ownership 697 if ( o->type->kind == TypeData::Aggregate ) { 698 type->aggInst.hoistType = o->type->aggregate.body; 699 type->aggInst.params = maybeCopy( o->type->aggregate.actuals ); 700 } else { 701 type->aggInst.hoistType = o->type->enumeration.body; 702 } // if 703 type->qualifiers |= o->type->qualifiers; 704 } else { 705 type = o->type; 706 } // if 707 o->type = nullptr; // change ownership 708 } else { 709 addTypeToType( o->type, type ); 710 } // if 711 } // if 712 if ( o->bitfieldWidth ) { 713 bitfieldWidth = o->bitfieldWidth; 714 } // if 715 716 // there may be typedefs chained onto the type 717 if ( o->get_next() ) { 718 set_last( o->get_next()->clone() ); 719 } // if 720 } // if 584 if ( !o ) return this; 585 586 checkSpecifiers( o ); 587 copySpecifiers( o, copyattr ); 588 if ( o->type ) { 589 type = ::addType( o->type, type, o->attributes ); 590 o->type = nullptr; 591 } // if 592 if ( o->bitfieldWidth ) { 593 bitfieldWidth = o->bitfieldWidth; 594 } // if 595 596 // there may be typedefs chained onto the type 597 if ( o->next ) { 598 set_last( o->next->clone() ); 599 } // if 600 721 601 delete o; 722 723 602 return this; 724 603 } 725 604 726 605 DeclarationNode * DeclarationNode::addEnumBase( DeclarationNode * o ) { 727 if ( o && o->type ){728 type->base = o->type;606 if ( o && o->type ) { 607 type->base = o->type; 729 608 } // if 730 609 delete o; … … 745 624 if ( variable.tyClass != ast::TypeDecl::NUMBER_OF_KINDS ) { 746 625 if ( variable.assertions ) { 747 variable.assertions-> appendList( assertions );626 variable.assertions->set_last( assertions ); 748 627 } else { 749 628 variable.assertions = assertions; … … 756 635 case TypeData::Symbolic: 757 636 if ( type->symbolic.assertions ) { 758 type->symbolic.assertions-> appendList( assertions );637 type->symbolic.assertions->set_last( assertions ); 759 638 } else { 760 639 type->symbolic.assertions = assertions; … … 810 689 DeclarationNode * DeclarationNode::setBase( TypeData * newType ) { 811 690 if ( type ) { 812 TypeData * prevBase = type; 813 TypeData * curBase = type->base; 814 while ( curBase != nullptr ) { 815 prevBase = curBase; 816 curBase = curBase->base; 817 } // while 818 prevBase->base = newType; 691 type->setLastBase( newType ); 819 692 } else { 820 693 type = newType; … … 857 730 assert( p->type->kind == TypeData::Pointer || p->type->kind == TypeData::Reference ); 858 731 if ( type ) { 859 switch ( type->kind ) { 860 case TypeData::Aggregate: 861 case TypeData::Enum: 862 p->type->base = new TypeData( TypeData::AggregateInst ); 863 p->type->base->aggInst.aggregate = type; 864 if ( type->kind == TypeData::Aggregate ) { 865 p->type->base->aggInst.params = maybeCopy( type->aggregate.actuals ); 866 } // if 867 p->type->base->qualifiers |= type->qualifiers; 868 break; 869 870 default: 871 p->type->base = type; 872 } // switch 732 p->type->base = makeNewBase( type ); 873 733 type = nullptr; 874 734 } // if … … 880 740 } 881 741 882 static TypeData * findLast( TypeData * a ) {883 assert( a );884 TypeData * cur = a;885 while ( cur->base ) {886 cur = cur->base;887 } // while888 return cur;889 }890 891 742 DeclarationNode * DeclarationNode::addNewArray( DeclarationNode * a ) { 892 743 if ( ! a ) return this; 893 744 assert( a->type->kind == TypeData::Array ); 894 TypeData * lastArray = findLast( a->type );895 745 if ( type ) { 896 switch ( type->kind ) { 897 case TypeData::Aggregate: 898 case TypeData::Enum: 899 lastArray->base = new TypeData( TypeData::AggregateInst ); 900 lastArray->base->aggInst.aggregate = type; 901 if ( type->kind == TypeData::Aggregate ) { 902 lastArray->base->aggInst.params = maybeCopy( type->aggregate.actuals ); 903 } // if 904 lastArray->base->qualifiers |= type->qualifiers; 905 break; 906 default: 907 lastArray->base = type; 908 } // switch 746 a->type->setLastBase( makeNewBase( type ) ); 909 747 type = nullptr; 910 748 } // if … … 960 798 DeclarationNode * DeclarationNode::cloneBaseType( DeclarationNode * o, bool copyattr ) { 961 799 if ( ! o ) return nullptr; 962 963 800 o->copySpecifiers( this, copyattr ); 964 801 if ( type ) { 965 TypeData * srcType = type; 966 967 // search for the base type by scanning off pointers and array designators 968 while ( srcType->base ) { 969 srcType = srcType->base; 970 } // while 971 972 TypeData * newType = srcType->clone(); 973 if ( newType->kind == TypeData::AggregateInst ) { 974 // don't duplicate members 975 if ( newType->aggInst.aggregate->kind == TypeData::Enum ) { 976 delete newType->aggInst.aggregate->enumeration.constants; 977 newType->aggInst.aggregate->enumeration.constants = nullptr; 978 newType->aggInst.aggregate->enumeration.body = false; 979 } else { 980 assert( newType->aggInst.aggregate->kind == TypeData::Aggregate ); 981 delete newType->aggInst.aggregate->aggregate.fields; 982 newType->aggInst.aggregate->aggregate.fields = nullptr; 983 newType->aggInst.aggregate->aggregate.body = false; 984 } // if 985 // don't hoist twice 986 newType->aggInst.hoistType = false; 987 } // if 988 989 newType->forall = maybeCopy( type->forall ); 990 if ( ! o->type ) { 991 o->type = newType; 992 } else { 993 addTypeToType( newType, o->type ); 994 delete newType; 995 } // if 802 o->type = ::cloneBaseType( type, o->type ); 996 803 } // if 997 804 return o; … … 1099 906 std::back_insert_iterator<std::vector<ast::ptr<ast::Decl>>> out( outputList ); 1100 907 1101 for ( const DeclarationNode * cur = firstNode ; cur ; cur = strict_next( cur )) {908 for ( const DeclarationNode * cur = firstNode ; cur ; cur = cur->next ) { 1102 909 try { 1103 910 bool extracted_named = false; … … 1167 974 std::back_insert_iterator<std::vector<ast::ptr<ast::DeclWithType>>> out( outputList ); 1168 975 1169 for ( const DeclarationNode * cur = firstNode; cur; cur = strict_next( cur )) {976 for ( const DeclarationNode * cur = firstNode; cur; cur = cur->next ) { 1170 977 try { 1171 978 ast::Decl * decl = cur->build(); … … 1217 1024 std::back_insert_iterator<std::vector<ast::ptr<ast::Type>>> out( outputList ); 1218 1025 1219 for ( const DeclarationNode * cur = firstNode ; cur ; cur = strict_next( cur )) {1026 for ( const DeclarationNode * cur = firstNode ; cur ; cur = cur->next ) { 1220 1027 try { 1221 1028 * out++ = cur->buildType();
Note:
See TracChangeset
for help on using the changeset viewer.