Changeset af60383 for src/Parser/DeclarationNode.cc
- Timestamp:
- Mar 5, 2024, 10:17:17 AM (4 months ago)
- Branches:
- master
- Children:
- f6e8c67
- Parents:
- 44adf1b
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/Parser/DeclarationNode.cc
r44adf1b raf60383 104 104 newnode->name = name ? new string( *name ) : nullptr; 105 105 106 newnode->builtin = NoBuiltinType;107 106 newnode->type = maybeCopy( type ); 108 107 newnode->inLine = inLine; … … 435 434 DeclarationNode * newnode = new DeclarationNode; 436 435 newnode->type = new TypeData( TypeData::Builtin ); 437 newnode->builtin = bt; 438 newnode->type->builtintype = newnode->builtin; 436 newnode->type->builtintype = bt; 439 437 return newnode; 440 438 } // DeclarationNode::newBuiltinType … … 556 554 } // DeclarationNode::copySpecifiers 557 555 558 static void addQualifiersToType( TypeData *& src, TypeData * dst ) {559 if ( dst->base ) {560 addQualifiersToType( src, dst->base );561 } else if ( dst->kind == TypeData::Function ) {562 dst->base = src;563 src = nullptr;564 } else {565 dst->qualifiers |= src->qualifiers;566 } // if567 } // addQualifiersToType568 569 556 DeclarationNode * DeclarationNode::addQualifiers( DeclarationNode * q ) { 570 557 if ( ! q ) { return this; } // empty qualifier … … 582 569 } // if 583 570 584 if ( q->type->forall ) { // forall qualifier ?585 if ( type->forall ) { // polymorphic routine ?586 type->forall->set_last( q->type->forall ); // augment forall qualifier587 } else {588 if ( type->kind == TypeData::Aggregate ) { // struct/union ?589 if ( type->aggregate.params ) { // polymorphic ?590 type->aggregate.params->set_last( q->type->forall ); // augment forall qualifier591 } else { // not polymorphic592 type->aggregate.params = q->type->forall; // set forall qualifier593 } // if594 } else { // not polymorphic595 type->forall = q->type->forall; // make polymorphic routine596 } // if597 } // if598 q->type->forall = nullptr; // forall qualifier moved599 } // if600 601 571 checkQualifiers( type, q->type ); 572 BuiltinType const builtin = type->builtintype; 602 573 if ( (builtin == Zero || builtin == One) && q->type->qualifiers.any() && error.length() == 0 ) { 603 574 SemanticWarning( yylloc, Warning::BadQualifiersZeroOne, builtinTypeNames[builtin] ); 604 575 } // if 605 addQualifiersToType( q->type, type ); 576 type = ::addQualifiers( q->type, type ); 577 q->type = nullptr; 606 578 607 579 delete q; … … 609 581 } // addQualifiers 610 582 611 static void addTypeToType( TypeData *& src, TypeData *& dst ) {612 if ( src->forall && dst->kind == TypeData::Function ) {613 if ( dst->forall ) {614 dst->forall->set_last( src->forall );615 } else {616 dst->forall = src->forall;617 } // if618 src->forall = nullptr;619 } // if620 if ( dst->base ) {621 addTypeToType( src, dst->base );622 } else {623 switch ( dst->kind ) {624 case TypeData::Unknown:625 src->qualifiers |= dst->qualifiers;626 dst = src;627 src = nullptr;628 break;629 case TypeData::Basic:630 dst->qualifiers |= src->qualifiers;631 if ( src->kind != TypeData::Unknown ) {632 assert( src->kind == TypeData::Basic );633 634 if ( dst->basictype == DeclarationNode::NoBasicType ) {635 dst->basictype = src->basictype;636 } else if ( src->basictype != DeclarationNode::NoBasicType )637 SemanticError( yylloc, "multiple declaration types \"%s\" and \"%s\".",638 DeclarationNode::basicTypeNames[ dst->basictype ],639 DeclarationNode::basicTypeNames[ src->basictype ] );640 if ( dst->complextype == DeclarationNode::NoComplexType ) {641 dst->complextype = src->complextype;642 } else if ( src->complextype != DeclarationNode::NoComplexType )643 SemanticError( yylloc, "multiple declaration types \"%s\" and \"%s\".",644 DeclarationNode::complexTypeNames[ src->complextype ],645 DeclarationNode::complexTypeNames[ src->complextype ] );646 if ( dst->signedness == DeclarationNode::NoSignedness ) {647 dst->signedness = src->signedness;648 } else if ( src->signedness != DeclarationNode::NoSignedness )649 SemanticError( yylloc, "conflicting type specifier \"%s\" and \"%s\".",650 DeclarationNode::signednessNames[ dst->signedness ],651 DeclarationNode::signednessNames[ src->signedness ] );652 if ( dst->length == DeclarationNode::NoLength ) {653 dst->length = src->length;654 } else if ( dst->length == DeclarationNode::Long && src->length == DeclarationNode::Long ) {655 dst->length = DeclarationNode::LongLong;656 } else if ( src->length != DeclarationNode::NoLength )657 SemanticError( yylloc, "conflicting type specifier \"%s\" and \"%s\".",658 DeclarationNode::lengthNames[ dst->length ],659 DeclarationNode::lengthNames[ src->length ] );660 } // if661 break;662 default:663 switch ( src->kind ) {664 case TypeData::Aggregate:665 case TypeData::Enum:666 dst->base = new TypeData( TypeData::AggregateInst );667 dst->base->aggInst.aggregate = src;668 if ( src->kind == TypeData::Aggregate ) {669 dst->base->aggInst.params = maybeCopy( src->aggregate.actuals );670 } // if671 dst->base->qualifiers |= src->qualifiers;672 src = nullptr;673 break;674 default:675 if ( dst->forall ) {676 dst->forall->set_last( src->forall );677 } else {678 dst->forall = src->forall;679 } // if680 src->forall = nullptr;681 dst->base = src;682 src = nullptr;683 } // switch684 } // switch685 } // if686 }687 688 583 DeclarationNode * DeclarationNode::addType( DeclarationNode * o, bool copyattr ) { 689 if ( o ) { 690 checkSpecifiers( o ); 691 copySpecifiers( o, copyattr ); 692 if ( o->type ) { 693 if ( ! type ) { 694 if ( o->type->kind == TypeData::Aggregate || o->type->kind == TypeData::Enum ) { 695 // Hide type information aggregate instances. 696 type = new TypeData( TypeData::AggregateInst ); 697 type->aggInst.aggregate = o->type; // change ownership 698 type->aggInst.aggregate->aggregate.attributes.swap( o->attributes ); // change ownership 699 if ( o->type->kind == TypeData::Aggregate ) { 700 type->aggInst.hoistType = o->type->aggregate.body; 701 type->aggInst.params = maybeCopy( o->type->aggregate.actuals ); 702 } else { 703 type->aggInst.hoistType = o->type->enumeration.body; 704 } // if 705 type->qualifiers |= o->type->qualifiers; 706 } else { 707 type = o->type; 708 } // if 709 o->type = nullptr; // change ownership 710 } else { 711 addTypeToType( o->type, type ); 712 } // if 713 } // if 714 if ( o->bitfieldWidth ) { 715 bitfieldWidth = o->bitfieldWidth; 716 } // if 717 718 // there may be typedefs chained onto the type 719 if ( o->next ) { 720 set_last( o->next->clone() ); 721 } // if 722 } // 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 723 601 delete o; 724 725 602 return this; 726 603 } 727 604 728 605 DeclarationNode * DeclarationNode::addEnumBase( DeclarationNode * o ) { 729 if ( o && o->type ){730 type->base = o->type;606 if ( o && o->type ) { 607 type->base = o->type; 731 608 } // if 732 609 delete o; … … 812 689 DeclarationNode * DeclarationNode::setBase( TypeData * newType ) { 813 690 if ( type ) { 814 TypeData * prevBase = type; 815 TypeData * curBase = type->base; 816 while ( curBase != nullptr ) { 817 prevBase = curBase; 818 curBase = curBase->base; 819 } // while 820 prevBase->base = newType; 691 type->setLastBase( newType ); 821 692 } else { 822 693 type = newType; … … 859 730 assert( p->type->kind == TypeData::Pointer || p->type->kind == TypeData::Reference ); 860 731 if ( type ) { 861 switch ( type->kind ) { 862 case TypeData::Aggregate: 863 case TypeData::Enum: 864 p->type->base = new TypeData( TypeData::AggregateInst ); 865 p->type->base->aggInst.aggregate = type; 866 if ( type->kind == TypeData::Aggregate ) { 867 p->type->base->aggInst.params = maybeCopy( type->aggregate.actuals ); 868 } // if 869 p->type->base->qualifiers |= type->qualifiers; 870 break; 871 872 default: 873 p->type->base = type; 874 } // switch 732 p->type->base = makeNewBase( type ); 875 733 type = nullptr; 876 734 } // if … … 882 740 } 883 741 884 static TypeData * findLast( TypeData * a ) {885 assert( a );886 TypeData * cur = a;887 while ( cur->base ) {888 cur = cur->base;889 } // while890 return cur;891 }892 893 742 DeclarationNode * DeclarationNode::addNewArray( DeclarationNode * a ) { 894 743 if ( ! a ) return this; 895 744 assert( a->type->kind == TypeData::Array ); 896 TypeData * lastArray = findLast( a->type );897 745 if ( type ) { 898 switch ( type->kind ) { 899 case TypeData::Aggregate: 900 case TypeData::Enum: 901 lastArray->base = new TypeData( TypeData::AggregateInst ); 902 lastArray->base->aggInst.aggregate = type; 903 if ( type->kind == TypeData::Aggregate ) { 904 lastArray->base->aggInst.params = maybeCopy( type->aggregate.actuals ); 905 } // if 906 lastArray->base->qualifiers |= type->qualifiers; 907 break; 908 default: 909 lastArray->base = type; 910 } // switch 746 a->type->setLastBase( makeNewBase( type ) ); 911 747 type = nullptr; 912 748 } // if … … 962 798 DeclarationNode * DeclarationNode::cloneBaseType( DeclarationNode * o, bool copyattr ) { 963 799 if ( ! o ) return nullptr; 964 965 800 o->copySpecifiers( this, copyattr ); 966 801 if ( type ) { 967 TypeData * srcType = type; 968 969 // search for the base type by scanning off pointers and array designators 970 while ( srcType->base ) { 971 srcType = srcType->base; 972 } // while 973 974 TypeData * newType = srcType->clone(); 975 if ( newType->kind == TypeData::AggregateInst ) { 976 // don't duplicate members 977 if ( newType->aggInst.aggregate->kind == TypeData::Enum ) { 978 delete newType->aggInst.aggregate->enumeration.constants; 979 newType->aggInst.aggregate->enumeration.constants = nullptr; 980 newType->aggInst.aggregate->enumeration.body = false; 981 } else { 982 assert( newType->aggInst.aggregate->kind == TypeData::Aggregate ); 983 delete newType->aggInst.aggregate->aggregate.fields; 984 newType->aggInst.aggregate->aggregate.fields = nullptr; 985 newType->aggInst.aggregate->aggregate.body = false; 986 } // if 987 // don't hoist twice 988 newType->aggInst.hoistType = false; 989 } // if 990 991 newType->forall = maybeCopy( type->forall ); 992 if ( ! o->type ) { 993 o->type = newType; 994 } else { 995 addTypeToType( newType, o->type ); 996 delete newType; 997 } // if 802 o->type = ::cloneBaseType( type, o->type ); 998 803 } // if 999 804 return o;
Note: See TracChangeset
for help on using the changeset viewer.