Changes in src/Parser/DeclarationNode.cc [fb04321:08d5507b]
- File:
-
- 1 edited
-
src/Parser/DeclarationNode.cc (modified) (21 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/Parser/DeclarationNode.cc
rfb04321 r08d5507b 10 10 // Created On : Sat May 16 12:34:05 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : T hu Mar 16 09:10:57201713 // Update Count : 100712 // Last Modified On : Tue Mar 14 10:19:38 2017 13 // Update Count : 964 14 14 // 15 15 … … 33 33 34 34 // These must remain in the same order as the corresponding DeclarationNode enumerations. 35 const char * DeclarationNode::storageClassNames[] = { "extern", "static", "auto", "register", "_Thread_local", "NoStorageClassNames" }; 36 const char * DeclarationNode::funcSpecifierNames[] = { "inline", "fortran", "_Noreturn", "NoFunctionSpecifierNames" }; 37 const char * DeclarationNode::typeQualifierNames[] = { "const", "restrict", "volatile", "lvalue", "mutex", "_Atomic", "NoTypeQualifierNames" }; 35 38 const char * DeclarationNode::basicTypeNames[] = { "void", "_Bool", "char", "int", "float", "double", "long double", "NoBasicTypeNames" }; 36 39 const char * DeclarationNode::complexTypeNames[] = { "_Complex", "_Imaginary", "NoComplexTypeNames" }; … … 113 116 } 114 117 118 void DeclarationNode::print_StorageClass( std::ostream & output, StorageClasses storageClasses ) { 119 if ( storageClasses.val != 0 ) { // storage classes ? 120 for ( unsigned int i = 0; i < DeclarationNode::NoStorageClass; i += 1 ) { 121 if ( storageClasses[i] ) { 122 output << DeclarationNode::storageClassNames[i] << ' '; 123 } // if 124 } // for 125 } // if 126 } // print_StorageClass 127 128 void DeclarationNode::print_FuncSpec( std::ostream & output, DeclarationNode::FuncSpecifiers funcSpec ) { 129 if ( funcSpec.val != 0 ) { // function specifiers ? 130 for ( unsigned int i = 0; i < DeclarationNode::NoFuncSpecifier; i += 1 ) { 131 if ( funcSpec[i] ) { 132 output << DeclarationNode::funcSpecifierNames[i] << ' '; 133 } // if 134 } // for 135 } // if 136 } // print_FuncSpec 137 115 138 void DeclarationNode::print( std::ostream &os, int indent ) const { 116 139 os << string( indent, ' ' ); … … 125 148 } // if 126 149 127 storageClasses.print( os );128 funcSpecs.print( os );150 print_StorageClass( os, storageClasses ); 151 print_FuncSpec( os, funcSpecs ); 129 152 130 153 if ( type ) { … … 179 202 180 203 181 DeclarationNode * DeclarationNode::newStorageClass( Type::StorageClasses sc ) {204 DeclarationNode * DeclarationNode::newStorageClass( DeclarationNode::StorageClasses sc ) { 182 205 DeclarationNode * newnode = new DeclarationNode; 183 206 newnode->storageClasses = sc; … … 185 208 } // DeclarationNode::newStorageClass 186 209 187 DeclarationNode * DeclarationNode::newFuncSpecifier( Type::FuncSpecifiers fs ) {210 DeclarationNode * DeclarationNode::newFuncSpecifier( DeclarationNode::FuncSpecifiers fs ) { 188 211 DeclarationNode * newnode = new DeclarationNode; 189 212 newnode->funcSpecs = fs; … … 191 214 } // DeclarationNode::newFuncSpecifier 192 215 193 DeclarationNode * DeclarationNode::newTypeQualifier( Type ::Qualifierstq ) {216 DeclarationNode * DeclarationNode::newTypeQualifier( TypeQualifier tq ) { 194 217 DeclarationNode * newnode = new DeclarationNode; 195 218 newnode->type = new TypeData(); 196 newnode->type-> qualifiers = tq;219 newnode->type->typeQualifiers[ tq ] = true; 197 220 return newnode; 198 221 } // DeclarationNode::newQualifier … … 434 457 435 458 void DeclarationNode::checkQualifiers( const TypeData * src, const TypeData * dst ) { 436 const Type ::Qualifiers qsrc = src->qualifiers, qdst = dst->qualifiers; // optimization437 438 if ( (qsrc .val & qdst.val) != 0 ) {// duplicates ?439 for ( unsigned int i = 0; i < Type::NumTypeQualifier; i += 1 ) { // find duplicates459 const TypeData::TypeQualifiers qsrc = src->typeQualifiers, qdst = dst->typeQualifiers; // optimization 460 461 if ( (qsrc & qdst).any() ) { // duplicates ? 462 for ( unsigned int i = 0; i < NoTypeQualifier; i += 1 ) { // find duplicates 440 463 if ( qsrc[i] && qdst[i] ) { 441 appendError( error, string( "duplicate " ) + Type::Qualifiers::Names[i] );464 appendError( error, string( "duplicate " ) + DeclarationNode::typeQualifierNames[i] ); 442 465 } // if 443 466 } // for … … 446 469 447 470 void DeclarationNode::checkSpecifiers( DeclarationNode * src ) { 448 if ( (funcSpecs.val & src->funcSpecs.val) != 0 ) { // duplicates ?449 for ( unsigned int i = 0; i < Type::NumFuncSpecifier; i += 1 ) { // find duplicates471 if ( (funcSpecs.val & src->funcSpecs.val) != 0 ) { // duplicates ? 472 for ( unsigned int i = 0; i < NoFuncSpecifier; i += 1 ) { // find duplicates 450 473 if ( funcSpecs[i] && src->funcSpecs[i] ) { 451 appendError( error, string( "duplicate " ) + Type::FuncSpecifiers::Names[i] );474 appendError( error, string( "duplicate " ) + DeclarationNode::funcSpecifierNames[i] ); 452 475 } // if 453 476 } // for 454 477 } // if 455 478 456 if ( storageClasses. any() && src->storageClasses.any()) { // any reason to check ?479 if ( storageClasses.val != 0 && src->storageClasses.val != 0 ) { // any reason to check ? 457 480 if ( (storageClasses.val & src->storageClasses.val ) != 0 ) { // duplicates ? 458 for ( unsigned int i = 0; i < Type::NumStorageClass; i += 1 ) { // find duplicates481 for ( unsigned int i = 0; i < NoStorageClass; i += 1 ) { // find duplicates 459 482 if ( storageClasses[i] && src->storageClasses[i] ) { 460 appendError( error, string( "duplicate " ) + Type::StorageClasses::Names[i] );483 appendError( error, string( "duplicate " ) + storageClassNames[i] ); 461 484 } // if 462 485 } // for 463 486 // src is the new item being added and has a single bit 464 487 } else if ( ! src->storageClasses.is_threadlocal ) { // conflict ? 465 appendError( error, string( "conflicting " ) + Type::StorageClasses::Names[ffs( storageClasses.val ) - 1] +466 " & " + Type::StorageClasses::Names[ffs( src->storageClasses.val ) - 1] );488 appendError( error, string( "conflicting " ) + storageClassNames[ffs( storageClasses.val ) - 1] + 489 " & " + storageClassNames[ffs( src->storageClasses.val ) - 1] ); 467 490 src->storageClasses.val = 0; // FIX to preserve invariant of one basic storage specifier 468 491 } // if … … 473 496 474 497 DeclarationNode * DeclarationNode::copySpecifiers( DeclarationNode * q ) { 475 funcSpecs.val |=q->funcSpecs.val;476 storageClasses.val |=q->storageClasses.val;498 funcSpecs.val = funcSpecs.val | q->funcSpecs.val; 499 storageClasses.val = storageClasses.val | q->storageClasses.val; 477 500 478 501 for ( Attribute *attr: reverseIterate( q->attributes ) ) { … … 497 520 src = nullptr; 498 521 } else { 499 dst-> qualifiers += src->qualifiers;522 dst->typeQualifiers |= src->typeQualifiers; 500 523 } // if 501 524 } // addQualifiersToType … … 555 578 switch ( dst->kind ) { 556 579 case TypeData::Unknown: 557 src-> qualifiers += dst->qualifiers;580 src->typeQualifiers |= dst->typeQualifiers; 558 581 dst = src; 559 582 src = nullptr; 560 583 break; 561 584 case TypeData::Basic: 562 dst-> qualifiers += src->qualifiers;585 dst->typeQualifiers |= src->typeQualifiers; 563 586 if ( src->kind != TypeData::Unknown ) { 564 587 assert( src->kind == TypeData::Basic ); … … 596 619 dst->base->aggInst.params = maybeClone( src->aggregate.actuals ); 597 620 } // if 598 dst->base-> qualifiers += src->qualifiers;621 dst->base->typeQualifiers |= src->typeQualifiers; 599 622 src = nullptr; 600 623 break; … … 628 651 type->aggInst.hoistType = o->type->enumeration.body; 629 652 } // if 630 type-> qualifiers += o->type->qualifiers;653 type->typeQualifiers |= o->type->typeQualifiers; 631 654 } else { 632 655 type = o->type; … … 784 807 p->type->base->aggInst.params = maybeClone( type->aggregate.actuals ); 785 808 } // if 786 p->type->base-> qualifiers += type->qualifiers;809 p->type->base->typeQualifiers |= type->typeQualifiers; 787 810 break; 788 811 … … 809 832 810 833 DeclarationNode * DeclarationNode::addNewArray( DeclarationNode * a ) { 811 if ( ! a ) return this; 812 assert( a->type->kind == TypeData::Array ); 813 TypeData * lastArray = findLast( a->type ); 814 if ( type ) { 815 switch ( type->kind ) { 816 case TypeData::Aggregate: 817 case TypeData::Enum: 818 lastArray->base = new TypeData( TypeData::AggregateInst ); 819 lastArray->base->aggInst.aggregate = type; 820 if ( type->kind == TypeData::Aggregate ) { 821 lastArray->base->aggInst.params = maybeClone( type->aggregate.actuals ); 822 } // if 823 lastArray->base->qualifiers += type->qualifiers; 824 break; 825 default: 826 lastArray->base = type; 827 } // switch 828 type = nullptr; 829 } // if 830 delete this; 831 return a; 834 if ( a ) { 835 assert( a->type->kind == TypeData::Array ); 836 TypeData * lastArray = findLast( a->type ); 837 if ( type ) { 838 switch ( type->kind ) { 839 case TypeData::Aggregate: 840 case TypeData::Enum: 841 lastArray->base = new TypeData( TypeData::AggregateInst ); 842 lastArray->base->aggInst.aggregate = type; 843 if ( type->kind == TypeData::Aggregate ) { 844 lastArray->base->aggInst.params = maybeClone( type->aggregate.actuals ); 845 } // if 846 lastArray->base->typeQualifiers |= type->typeQualifiers; 847 break; 848 default: 849 lastArray->base = type; 850 } // switch 851 type = nullptr; 852 } // if 853 delete this; 854 return a; 855 } else { 856 return this; 857 } // if 832 858 } 833 859 … … 968 994 } else if ( StructDecl * agg = dynamic_cast< StructDecl * >( decl ) ) { 969 995 StructInstType * inst = new StructInstType( Type::Qualifiers(), agg->get_name() ); 970 auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr );996 auto obj = new ObjectDecl( "", DeclarationNode::StorageClasses(), linkage, nullptr, inst, nullptr ); 971 997 obj->location = cur->location; 972 998 * out++ = obj; … … 974 1000 } else if ( UnionDecl * agg = dynamic_cast< UnionDecl * >( decl ) ) { 975 1001 UnionInstType * inst = new UnionInstType( Type::Qualifiers(), agg->get_name() ); 976 auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr );1002 auto obj = new ObjectDecl( "", DeclarationNode::StorageClasses(), linkage, nullptr, inst, nullptr ); 977 1003 obj->location = cur->location; 978 1004 * out++ = obj; … … 1021 1047 assertf( sizeof(kindMap)/sizeof(kindMap[0] == NoTypeClass-1), "DeclarationNode::build: kindMap is out of sync." ); 1022 1048 assertf( variable.tyClass < sizeof(kindMap)/sizeof(kindMap[0]), "Variable's tyClass is out of bounds." ); 1023 TypeDecl * ret = new TypeDecl( *name, Type::StorageClasses(), nullptr, kindMap[ variable.tyClass ] );1049 TypeDecl * ret = new TypeDecl( *name, DeclarationNode::StorageClasses(), nullptr, kindMap[ variable.tyClass ] ); 1024 1050 buildList( variable.assertions, ret->get_assertions() ); 1025 1051 return ret; … … 1032 1058 // inline _Noreturn int g( int i ); // allowed 1033 1059 // inline _Noreturn int i; // disallowed 1034 if ( type->kind != TypeData::Function && funcSpecs. any()) {1060 if ( type->kind != TypeData::Function && funcSpecs.val != 0 ) { 1035 1061 throw SemanticError( "invalid function specifier for ", this ); 1036 1062 } // if … … 1042 1068 // inlne _Noreturn struct S { ... }; // disallowed 1043 1069 // inlne _Noreturn enum E { ... }; // disallowed 1044 if ( funcSpecs. any()) {1070 if ( funcSpecs.val != 0 ) { 1045 1071 throw SemanticError( "invalid function specifier for ", this ); 1046 1072 } // if
Note:
See TracChangeset
for help on using the changeset viewer.