Changes in src/Parser/DeclarationNode.cc [d1625f8:413ad05]
- File:
-
- 1 edited
-
src/Parser/DeclarationNode.cc (modified) (36 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/Parser/DeclarationNode.cc
rd1625f8 r413ad05 10 10 // Created On : Sat May 16 12:34:05 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue Aug 9 08:39:20201613 // Update Count : 16912 // Last Modified On : Sun Aug 28 22:12:44 2016 13 // Update Count : 278 14 14 // 15 15 … … 25 25 #include "SynTree/Expression.h" 26 26 27 #include "Parser.h"28 27 #include "TypedefTable.h" 29 28 extern TypedefTable typedefTable; … … 42 41 UniqueName DeclarationNode::anonymous( "__anonymous" ); 43 42 44 extern LinkageSpec:: Typelinkage; // defined in parser.yy43 extern LinkageSpec::Spec linkage; // defined in parser.yy 45 44 46 45 DeclarationNode *DeclarationNode::clone() const { … … 48 47 newnode->type = maybeClone( type ); 49 48 newnode->name = name; 50 newnode->storageClasses = storageClasses; 51 //PAB newnode->bitfieldWidth = maybeClone( bitfieldWidth ); 52 newnode->bitfieldWidth = bitfieldWidth; 49 newnode->storageClass = storageClass; 50 newnode->isInline = isInline; 51 newnode->isNoreturn = isNoreturn; 52 newnode->bitfieldWidth = maybeClone( bitfieldWidth ); 53 53 newnode->hasEllipsis = hasEllipsis; 54 newnode->initializer = initializer;55 newnode-> next = maybeClone( next);54 newnode->initializer = maybeClone( initializer ); 55 newnode->set_next( maybeClone( get_next() ) ); 56 56 newnode->linkage = linkage; 57 57 return newnode; 58 58 } // DeclarationNode::clone 59 59 60 DeclarationNode::DeclarationNode() : type( 0 ), bitfieldWidth( 0 ), initializer( 0 ), hasEllipsis( false ), linkage( ::linkage ) { 60 DeclarationNode::DeclarationNode() 61 : type( 0 ) 62 , storageClass( NoStorageClass ) 63 , isInline( false ) 64 , isNoreturn( false ) 65 , bitfieldWidth( 0 ) 66 , initializer( 0 ) 67 , hasEllipsis( false ) 68 , linkage( ::linkage ) 69 , extension( false ) 70 , error() { 61 71 } 62 72 … … 83 93 } // if 84 94 85 printEnums( storageClasses.begin(), storageClasses.end(), DeclarationNode::storageName, os ); 95 if ( storageClass != NoStorageClass ) os << DeclarationNode::storageName[storageClass] << ' '; 96 if ( isInline ) os << DeclarationNode::storageName[Inline] << ' '; 97 if ( isNoreturn ) os << DeclarationNode::storageName[Noreturn] << ' '; 86 98 if ( type ) { 87 99 type->print( os, indent ); … … 135 147 } // DeclarationNode::newFunction 136 148 137 DeclarationNode * DeclarationNode::newQualifier( Qualifier q ) {149 DeclarationNode * DeclarationNode::newQualifier( Qualifier q ) { 138 150 DeclarationNode *newnode = new DeclarationNode; 139 151 newnode->type = new TypeData(); 140 newnode->type->qualifiers .push_back( q );152 newnode->type->qualifiers[ q ] = 1; 141 153 return newnode; 142 154 } // DeclarationNode::newQualifier 143 155 144 DeclarationNode *DeclarationNode::newStorageClass( DeclarationNode::StorageClass sc ) { 145 DeclarationNode *newnode = new DeclarationNode; 146 newnode->storageClasses.push_back( sc ); 156 DeclarationNode * DeclarationNode::newForall( DeclarationNode *forall ) { 157 DeclarationNode *newnode = new DeclarationNode; 158 newnode->type = new TypeData( TypeData::Unknown ); 159 newnode->type->forall = forall; 160 return newnode; 161 } // DeclarationNode::newForall 162 163 DeclarationNode * DeclarationNode::newStorageClass( DeclarationNode::StorageClass sc ) { 164 DeclarationNode *newnode = new DeclarationNode; 165 //switch (sc) { 166 // case Inline: newnode->isInline = true; break; 167 // case Noreturn: newnode->isNoreturn = true; break; 168 // default: newnode->storageClass = sc; break; 169 //} 170 newnode->storageClass = sc; 147 171 return newnode; 148 172 } // DeclarationNode::newStorageClass 149 173 150 DeclarationNode * DeclarationNode::newBasicType( BasicType bt ) {174 DeclarationNode * DeclarationNode::newBasicType( BasicType bt ) { 151 175 DeclarationNode *newnode = new DeclarationNode; 152 176 newnode->type = new TypeData( TypeData::Basic ); … … 155 179 } // DeclarationNode::newBasicType 156 180 157 DeclarationNode *DeclarationNode::newBuiltinType( BuiltinType bt ) { 181 DeclarationNode * DeclarationNode::newModifier( Modifier mod ) { 182 DeclarationNode *newnode = new DeclarationNode; 183 newnode->type = new TypeData( TypeData::Basic ); 184 newnode->type->basic->modifiers.push_back( mod ); 185 return newnode; 186 } // DeclarationNode::newModifier 187 188 DeclarationNode * DeclarationNode::newBuiltinType( BuiltinType bt ) { 158 189 DeclarationNode *newnode = new DeclarationNode; 159 190 newnode->type = new TypeData( TypeData::Builtin ); … … 162 193 } // DeclarationNode::newBuiltinType 163 194 164 DeclarationNode *DeclarationNode::newModifier( Modifier mod ) { 165 DeclarationNode *newnode = new DeclarationNode; 166 newnode->type = new TypeData( TypeData::Basic ); 167 newnode->type->basic->modifiers.push_back( mod ); 168 return newnode; 169 } // DeclarationNode::newModifier 170 171 DeclarationNode *DeclarationNode::newForall( DeclarationNode *forall ) { 172 DeclarationNode *newnode = new DeclarationNode; 173 newnode->type = new TypeData( TypeData::Unknown ); 174 newnode->type->forall = forall; 175 return newnode; 176 } // DeclarationNode::newForall 177 178 DeclarationNode *DeclarationNode::newFromTypedef( std::string *name ) { 195 DeclarationNode * DeclarationNode::newFromTypedef( std::string *name ) { 179 196 DeclarationNode *newnode = new DeclarationNode; 180 197 newnode->type = new TypeData( TypeData::SymbolicInst ); … … 185 202 } // DeclarationNode::newFromTypedef 186 203 187 DeclarationNode * DeclarationNode::newAggregate( Aggregate kind, const std::string *name, ExpressionNode *actuals, DeclarationNode *fields, bool body ) {204 DeclarationNode * DeclarationNode::newAggregate( Aggregate kind, const std::string *name, ExpressionNode *actuals, DeclarationNode *fields, bool body ) { 188 205 DeclarationNode *newnode = new DeclarationNode; 189 206 newnode->type = new TypeData( TypeData::Aggregate ); … … 214 231 DeclarationNode *newnode = new DeclarationNode; 215 232 newnode->name = assign_strptr( name ); 216 newnode->enumeratorValue = constant;233 newnode->enumeratorValue.reset( constant ); 217 234 typedefTable.addToEnclosingScope( newnode->name, TypedefTable::ID ); 218 235 return newnode; … … 284 301 newnode->type->array->dimension = size; 285 302 newnode->type->array->isStatic = isStatic; 286 if ( newnode->type->array->dimension == 0 || dynamic_cast<ConstantExpr *>( newnode->type->array->dimension->build()) ) {303 if ( newnode->type->array->dimension == 0 || newnode->type->array->dimension->isExpressionType<ConstantExpr *>() ) { 287 304 newnode->type->array->isVarLen = false; 288 305 } else { … … 353 370 src = 0; 354 371 } else { 355 dst->qualifiers.splice( dst->qualifiers.end(), src->qualifiers ); 356 } // if 357 } // if 358 } 372 dst->qualifiers |= src->qualifiers; 373 } // if 374 } // if 375 } 376 377 void DeclarationNode::checkQualifiers( const TypeData *src, const TypeData *dst ) { 378 TypeData::Qualifiers qsrc = src->qualifiers, qdst = dst->qualifiers; 379 380 if ( (qsrc & qdst).any() ) { // common bits between qualifier masks ? 381 error = "duplicate qualifier "; 382 int j = 0; // separator detector 383 for ( int i = 0; i < DeclarationNode::NoOfQualifier; i += 1 ) { 384 if ( qsrc[i] & qdst[i] ) { // find specific qualifiers in common 385 if ( j > 0 ) error += ", "; 386 error += DeclarationNode::qualifierName[i]; 387 j += 1; 388 } // if 389 } // for 390 error += " in declaration of "; 391 } // if 392 } // DeclarationNode::checkQualifiers 359 393 360 394 DeclarationNode *DeclarationNode::addQualifiers( DeclarationNode *q ) { 361 395 if ( q ) { 362 storageClasses.splice( storageClasses.end(), q->storageClasses);396 copyStorageClasses(q); 363 397 if ( q->type ) { 364 398 if ( ! type ) { 365 399 type = new TypeData; 400 } else { 401 checkQualifiers( q->type, type ); 366 402 } // if 367 403 addQualifiersToType( q->type, type ); … … 387 423 388 424 DeclarationNode *DeclarationNode::copyStorageClasses( DeclarationNode *q ) { 389 storageClasses = q->storageClasses; 425 isInline = isInline || q->isInline; 426 isNoreturn = isNoreturn || q->isNoreturn; 427 if ( storageClass == NoStorageClass ) { 428 storageClass = q->storageClass; 429 } else if ( q->storageClass != NoStorageClass ) { 430 q->error = "invalid combination of storage classes in declaration of "; 431 } // if 432 if ( error.empty() ) error = q->error; 390 433 return this; 391 434 } … … 406 449 switch ( dst->kind ) { 407 450 case TypeData::Unknown: 408 src->qualifiers .splice( src->qualifiers.end(), dst->qualifiers );451 src->qualifiers |= dst->qualifiers; 409 452 dst = src; 410 453 src = 0; 411 454 break; 412 455 case TypeData::Basic: 413 dst->qualifiers .splice( dst->qualifiers.end(), src->qualifiers );456 dst->qualifiers |= src->qualifiers; 414 457 if ( src->kind != TypeData::Unknown ) { 415 458 assert( src->kind == TypeData::Basic ); … … 427 470 dst->base->aggInst->params = maybeClone( src->aggregate->actuals ); 428 471 } // if 429 dst->base->qualifiers .splice( dst->base->qualifiers.end(), src->qualifiers );472 dst->base->qualifiers |= src->qualifiers; 430 473 src = 0; 431 474 break; … … 447 490 DeclarationNode *DeclarationNode::addType( DeclarationNode *o ) { 448 491 if ( o ) { 449 storageClasses.splice( storageClasses.end(), o->storageClasses);492 copyStorageClasses( o ); 450 493 if ( o->type ) { 451 494 if ( ! type ) { … … 456 499 type->aggInst->params = maybeClone( o->type->aggregate->actuals ); 457 500 } // if 458 type->qualifiers .splice( type->qualifiers.end(), o->type->qualifiers );501 type->qualifiers |= o->type->qualifiers; 459 502 } else { 460 503 type = o->type; … … 470 513 471 514 // there may be typedefs chained onto the type 472 if ( o->get_ link() ) {473 set_l ink( o->get_link()->clone() );515 if ( o->get_next() ) { 516 set_last( o->get_next()->clone() ); 474 517 } // if 475 518 } // if … … 591 634 p->type->base->aggInst->params = maybeClone( type->aggregate->actuals ); 592 635 } // if 593 p->type->base->qualifiers .splice( p->type->base->qualifiers.end(), type->qualifiers );636 p->type->base->qualifiers |= type->qualifiers; 594 637 break; 595 638 … … 628 671 lastArray->base->aggInst->params = maybeClone( type->aggregate->actuals ); 629 672 } // if 630 lastArray->base->qualifiers .splice( lastArray->base->qualifiers.end(), type->qualifiers );673 lastArray->base->qualifiers |= type->qualifiers; 631 674 break; 632 675 default: … … 694 737 } // if 695 738 newnode->type->forall = maybeClone( type->forall ); 696 newnode-> storageClasses = storageClasses;739 newnode->copyStorageClasses( this ); 697 740 newnode->name = assign_strptr( newName ); 698 741 return newnode; … … 701 744 DeclarationNode *DeclarationNode::cloneBaseType( DeclarationNode *o ) { 702 745 if ( o ) { 703 o-> storageClasses.insert( o->storageClasses.end(), storageClasses.begin(), storageClasses.end());746 o->copyStorageClasses( this ); 704 747 if ( type ) { 705 748 TypeData *srcType = type; … … 734 777 DeclarationNode *newnode = new DeclarationNode; 735 778 newnode->type = maybeClone( type ); 736 newnode-> storageClasses = storageClasses;779 newnode->copyStorageClasses( this ); 737 780 newnode->name = assign_strptr( newName ); 738 781 return newnode; … … 741 784 DeclarationNode *DeclarationNode::cloneType( DeclarationNode *o ) { 742 785 if ( o ) { 743 o-> storageClasses.insert( o->storageClasses.end(), storageClasses.begin(), storageClasses.end());786 o->copyStorageClasses( this ); 744 787 if ( type ) { 745 788 TypeData *newType = type->clone(); … … 752 795 } // if 753 796 } // if 797 delete o; 754 798 return o; 755 }756 757 DeclarationNode *DeclarationNode::appendList( DeclarationNode *node ) {758 if ( node != 0 ) {759 set_link( node );760 } // if761 return this;762 799 } 763 800 764 801 DeclarationNode *DeclarationNode::extractAggregate() const { 765 802 if ( type ) { 766 TypeData *ret = type ->extractAggregate();803 TypeData *ret = typeextractAggregate( type ); 767 804 if ( ret ) { 768 805 DeclarationNode *newnode = new DeclarationNode; … … 776 813 void buildList( const DeclarationNode *firstNode, std::list< Declaration * > &outputList ) { 777 814 SemanticError errors; 778 std::back_insert_iterator< std::list< Declaration * > > out( outputList );815 std::back_insert_iterator< std::list< Declaration * > > out( outputList ); 779 816 const DeclarationNode *cur = firstNode; 780 817 while ( cur ) { … … 786 823 *out++ = decl; 787 824 } // if 825 delete extr; 788 826 } // if 789 827 Declaration *decl = cur->build(); … … 794 832 errors.append( e ); 795 833 } // try 796 cur = dynamic_cast< DeclarationNode *>( cur->get_link() );834 cur = dynamic_cast< DeclarationNode * >( cur->get_next() ); 797 835 } // while 798 836 if ( ! errors.isEmpty() ) { … … 801 839 } 802 840 803 void buildList( const DeclarationNode *firstNode, std::list< DeclarationWithType * > &outputList ) {841 void buildList( const DeclarationNode *firstNode, std::list< DeclarationWithType * > &outputList ) { 804 842 SemanticError errors; 805 std::back_insert_iterator< std::list< DeclarationWithType * > > out( outputList );843 std::back_insert_iterator< std::list< DeclarationWithType * > > out( outputList ); 806 844 const DeclarationNode *cur = firstNode; 807 845 while ( cur ) { … … 817 855 Declaration *decl = cur->build(); 818 856 if ( decl ) { 819 if ( DeclarationWithType *dwt = dynamic_cast< DeclarationWithType * >( decl ) ) {857 if ( DeclarationWithType *dwt = dynamic_cast< DeclarationWithType * >( decl ) ) { 820 858 *out++ = dwt; 821 } else if ( StructDecl *agg = dynamic_cast< StructDecl * >( decl ) ) {859 } else if ( StructDecl *agg = dynamic_cast< StructDecl * >( decl ) ) { 822 860 StructInstType *inst = new StructInstType( Type::Qualifiers(), agg->get_name() ); 823 861 *out++ = new ObjectDecl( "", DeclarationNode::NoStorageClass, linkage, 0, inst, 0 ); 824 862 delete agg; 825 } else if ( UnionDecl *agg = dynamic_cast< UnionDecl * >( decl ) ) {863 } else if ( UnionDecl *agg = dynamic_cast< UnionDecl * >( decl ) ) { 826 864 UnionInstType *inst = new UnionInstType( Type::Qualifiers(), agg->get_name() ); 827 865 *out++ = new ObjectDecl( "", DeclarationNode::NoStorageClass, linkage, 0, inst, 0 ); … … 831 869 errors.append( e ); 832 870 } // try 833 cur = dynamic_cast< DeclarationNode * >( cur->get_link() );871 cur = dynamic_cast< DeclarationNode * >( cur->get_next() ); 834 872 } // while 835 873 if ( ! errors.isEmpty() ) { … … 838 876 } 839 877 840 void buildTypeList( const DeclarationNode *firstNode, std::list< Type * > &outputList ) {878 void buildTypeList( const DeclarationNode *firstNode, std::list< Type * > &outputList ) { 841 879 SemanticError errors; 842 std::back_insert_iterator< std::list< Type * > > out( outputList );880 std::back_insert_iterator< std::list< Type * > > out( outputList ); 843 881 const DeclarationNode *cur = firstNode; 844 882 while ( cur ) { … … 848 886 errors.append( e ); 849 887 } // try 850 cur = dynamic_cast< DeclarationNode * >( cur->get_link() );888 cur = dynamic_cast< DeclarationNode * >( cur->get_next() ); 851 889 } // while 852 890 if ( ! errors.isEmpty() ) { … … 856 894 857 895 Declaration *DeclarationNode::build() const { 896 if ( ! error.empty() ) throw SemanticError( error, this ); 858 897 if ( type ) { 859 return type->buildDecl( name, buildStorageClass(), maybeBuild< Expression >( bitfieldWidth ), buildFuncSpecifier( Inline ), buildFuncSpecifier( Noreturn ), linkage, maybeBuild< Initializer >(initializer) )->set_extension( extension );860 } // if 861 if ( ! buildFuncSpecifier( Inline ) && ! buildFuncSpecifier( Noreturn )) {862 return (new ObjectDecl( name, buildStorageClass(), linkage, maybeBuild< Expression >( bitfieldWidth ), 0, maybeBuild< Initializer >( initializer ) ))->set_extension( extension );898 return buildDecl( type, name, storageClass, maybeBuild< Expression >( bitfieldWidth ), isInline, isNoreturn, linkage, maybeBuild< Initializer >(initializer) )->set_extension( extension ); 899 } // if 900 if ( ! isInline && ! isNoreturn ) { 901 return (new ObjectDecl( name, storageClass, linkage, maybeBuild< Expression >( bitfieldWidth ), 0, maybeBuild< Initializer >( initializer ) ))->set_extension( extension ); 863 902 } // if 864 903 throw SemanticError( "invalid function specifier in declaration of ", this ); … … 870 909 switch ( type->kind ) { 871 910 case TypeData::Enum: 872 return new EnumInstType( type->buildQualifiers(), type->enumeration->name );911 return new EnumInstType( buildQualifiers( type ), type->enumeration->name ); 873 912 case TypeData::Aggregate: { 874 913 ReferenceToType *ret; 875 914 switch ( type->aggregate->kind ) { 876 915 case DeclarationNode::Struct: 877 ret = new StructInstType( type->buildQualifiers(), type->aggregate->name );916 ret = new StructInstType( buildQualifiers( type ), type->aggregate->name ); 878 917 break; 879 918 case DeclarationNode::Union: 880 ret = new UnionInstType( type->buildQualifiers(), type->aggregate->name );919 ret = new UnionInstType( buildQualifiers( type ), type->aggregate->name ); 881 920 break; 882 921 case DeclarationNode::Trait: 883 ret = new TraitInstType( type->buildQualifiers(), type->aggregate->name );922 ret = new TraitInstType( buildQualifiers( type ), type->aggregate->name ); 884 923 break; 885 924 default: … … 890 929 } 891 930 case TypeData::Symbolic: { 892 TypeInstType *ret = new TypeInstType( type->buildQualifiers(), type->symbolic->name, false );931 TypeInstType *ret = new TypeInstType( buildQualifiers( type ), type->symbolic->name, false ); 893 932 buildList( type->symbolic->actuals, ret->get_parameters() ); 894 933 return ret; 895 934 } 896 935 default: 897 return type ->build();936 return typebuild( type ); 898 937 } // switch 899 938 } 900 939 901 DeclarationNode::StorageClass DeclarationNode::buildStorageClass() const {902 DeclarationNode::StorageClass ret = DeclarationNode::NoStorageClass;903 for ( std::list< DeclarationNode::StorageClass >::const_iterator i = storageClasses.begin(); i != storageClasses.end(); ++i ) {904 if ( *i == DeclarationNode::Inline || *i == DeclarationNode::Noreturn ) continue; // ignore function specifiers905 if ( ret != DeclarationNode::NoStorageClass ) { // already have a valid storage class ?906 throw SemanticError( "invalid combination of storage classes in declaration of ", this );907 } // if908 ret = *i;909 } // for910 return ret;911 }912 913 bool DeclarationNode::buildFuncSpecifier( DeclarationNode::StorageClass key ) const {914 std::list< DeclarationNode::StorageClass >::const_iterator first = std::find( storageClasses.begin(), storageClasses.end(), key );915 if ( first == storageClasses.end() ) return false; // not found916 first = std::find( ++first, storageClasses.end(), key ); // found917 if ( first == storageClasses.end() ) return true; // not found again918 throw SemanticError( "duplicate function specifier in declaration of ", this );919 }940 // DeclarationNode::StorageClass DeclarationNode::buildStorageClass() const { 941 // DeclarationNode::StorageClass ret = DeclarationNode::NoStorageClass; 942 // for ( std::list< DeclarationNode::StorageClass >::const_iterator i = storageClasses.begin(); i != storageClasses.end(); ++i ) { 943 // if ( *i == DeclarationNode::Inline || *i == DeclarationNode::Noreturn ) continue; // ignore function specifiers 944 // if ( ret != DeclarationNode::NoStorageClass ) { // already have a valid storage class ? 945 // throw SemanticError( "invalid combination of storage classes in declaration of ", this ); 946 // } // if 947 // ret = *i; 948 // } // for 949 // return ret; 950 // } 951 952 // bool DeclarationNode::buildFuncSpecifier( DeclarationNode::StorageClass key ) const { 953 // std::list< DeclarationNode::StorageClass >::const_iterator first = std::find( storageClasses.begin(), storageClasses.end(), key ); 954 // if ( first == storageClasses.end() ) return false; // not found 955 // first = std::find( ++first, storageClasses.end(), key ); // found 956 // if ( first == storageClasses.end() ) return true; // not found again 957 // throw SemanticError( "duplicate function specifier in declaration of ", this ); 958 // } 920 959 921 960 // Local Variables: //
Note:
See TracChangeset
for help on using the changeset viewer.