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