Changes in src/Parser/DeclarationNode.cc [dd020c0:43c89a7]
- File:
-
- 1 edited
-
src/Parser/DeclarationNode.cc (modified) (22 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/Parser/DeclarationNode.cc
rdd020c0 r43c89a7 10 10 // Created On : Sat May 16 12:34:05 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Fri Mar 3 21:38:34201713 // Update Count : 86212 // Last Modified On : Thu Feb 23 22:21:06 2017 13 // Update Count : 775 14 14 // 15 15 … … 32 32 33 33 // These must remain in the same order as the corresponding DeclarationNode enumerations. 34 const char * DeclarationNode::storageClassNames[] = { "extern", "static", "auto", "register", "_Thread_local", "inline", "fortran", "_Noreturn", "NoStorageClassNames" }; 35 const char * DeclarationNode::funcSpecifierNames[] = { "inline", "fortran", "_Noreturn", "NoFunctionSpecifierNames" }; 36 const char * DeclarationNode::typeQualifierNames[] = { "const", "restrict", "volatile", "lvalue", "_Atomic", "NoTypeQualifierNames" }; 37 const char * DeclarationNode::basicTypeNames[] = { "void", "_Bool", "char", "int", "float", "double", "long double", "NoBasicTypeNames" }; 38 const char * DeclarationNode::complexTypeNames[] = { "_Complex", "_Imaginary", "NoComplexTypeNames" }; 39 const char * DeclarationNode::signednessNames[] = { "signed", "unsigned", "NoSignednessNames" }; 40 const char * DeclarationNode::lengthNames[] = { "short", "long", "long long", "NoLengthNames" }; 41 const char * DeclarationNode::aggregateNames[] = { "struct", "union", "context", "NoAggregateNames" }; 42 const char * DeclarationNode::typeClassNames[] = { "otype", "dtype", "ftype", "NoTypeClassNames" }; 43 const char * DeclarationNode::builtinTypeNames[] = { "__builtin_va_list", "NoBuiltinTypeNames" }; 34 const char * DeclarationNode::storageName[] = { "extern", "static", "auto", "register", "inline", "fortran", "_Noreturn", "_Thread_local", "NoStorageClass" }; 35 const char * DeclarationNode::qualifierName[] = { "const", "restrict", "volatile", "lvalue", "_Atomic", "NoQualifier" }; 36 const char * DeclarationNode::basicTypeName[] = { "void", "_Bool", "char", "int", "float", "double", "long double", "NoBasicType" }; 37 const char * DeclarationNode::complexTypeName[] = { "_Complex", "_Imaginary", "NoComplexType" }; 38 const char * DeclarationNode::signednessName[] = { "signed", "unsigned", "NoSignedness" }; 39 const char * DeclarationNode::lengthName[] = { "short", "long", "long long", "NoLength" }; 40 const char * DeclarationNode::aggregateName[] = { "struct", "union", "context" }; 41 const char * DeclarationNode::typeClassName[] = { "otype", "dtype", "ftype" }; 42 const char * DeclarationNode::builtinTypeName[] = { "__builtin_va_list" }; 44 43 45 44 UniqueName DeclarationNode::anonymous( "__anonymous" ); … … 51 50 storageClass( NoStorageClass ), 52 51 bitfieldWidth( nullptr ), 52 isInline( false ), 53 isNoreturn( false ), 53 54 hasEllipsis( false ), 54 55 linkage( ::linkage ), … … 91 92 newnode->storageClass = storageClass; 92 93 newnode->bitfieldWidth = maybeClone( bitfieldWidth ); 93 newnode->funcSpec = funcSpec; 94 newnode->isInline = isInline; 95 newnode->isNoreturn = isNoreturn; 94 96 newnode->enumeratorValue.reset( maybeClone( enumeratorValue.get() ) ); 95 97 newnode->hasEllipsis = hasEllipsis; … … 116 118 } 117 119 118 void DeclarationNode::print_FuncSpec( std::ostream & output, DeclarationNode::FuncSpec funcSpec ) {119 if ( funcSpec.any() ) { // function specifiers?120 for ( unsigned int i = 0; i < DeclarationNode::NoFuncSpecifier; i += 1 ) {121 if ( funcSpec[i] ) {122 output << DeclarationNode::funcSpecifierNames[i] << ' ';123 } // if124 } // for125 } // if126 } // print_FuncSpec127 128 120 void DeclarationNode::print( std::ostream &os, int indent ) const { 129 121 os << string( indent, ' ' ); … … 138 130 } // if 139 131 140 if ( storageClass != NoStorageClass ) os << DeclarationNode::storage ClassNames[storageClass] << ' ';141 print_FuncSpec( os, funcSpec );142 132 if ( storageClass != NoStorageClass ) os << DeclarationNode::storageName[storageClass] << ' '; 133 if ( isInline ) os << DeclarationNode::storageName[Inline] << ' '; 134 if ( isNoreturn ) os << DeclarationNode::storageName[Noreturn] << ' '; 143 135 if ( type ) { 144 136 type->print( os, indent ); … … 191 183 } // DeclarationNode::newFunction 192 184 185 DeclarationNode * DeclarationNode::newQualifier( Qualifier q ) { 186 DeclarationNode * newnode = new DeclarationNode; 187 newnode->type = new TypeData(); 188 newnode->type->qualifiers[ q ] = 1; 189 return newnode; 190 } // DeclarationNode::newQualifier 191 192 DeclarationNode * DeclarationNode::newForall( DeclarationNode * forall ) { 193 DeclarationNode * newnode = new DeclarationNode; 194 newnode->type = new TypeData( TypeData::Unknown ); 195 newnode->type->forall = forall; 196 return newnode; 197 } // DeclarationNode::newForall 193 198 194 199 DeclarationNode * DeclarationNode::newStorageClass( DeclarationNode::StorageClass sc ) { … … 197 202 return newnode; 198 203 } // DeclarationNode::newStorageClass 199 200 DeclarationNode * DeclarationNode::newFuncSpecifier( DeclarationNode::FuncSpecifier fs ) {201 DeclarationNode * newnode = new DeclarationNode;202 newnode->funcSpec[ fs ] = true;203 return newnode;204 } // DeclarationNode::newFuncSpecifier205 206 DeclarationNode * DeclarationNode::newTypeQualifier( TypeQualifier tq ) {207 DeclarationNode * newnode = new DeclarationNode;208 newnode->type = new TypeData();209 newnode->type->typeQualifiers[ tq ] = true;210 return newnode;211 } // DeclarationNode::newQualifier212 204 213 205 DeclarationNode * DeclarationNode::newBasicType( BasicType bt ) { … … 238 230 return newnode; 239 231 } // DeclarationNode::newLength 240 241 DeclarationNode * DeclarationNode::newForall( DeclarationNode * forall ) {242 DeclarationNode * newnode = new DeclarationNode;243 newnode->type = new TypeData( TypeData::Unknown );244 newnode->type->forall = forall;245 return newnode;246 } // DeclarationNode::newForall247 232 248 233 DeclarationNode * DeclarationNode::newFromTypedef( string * name ) { … … 443 428 444 429 void DeclarationNode::checkQualifiers( const TypeData * src, const TypeData * dst ) { 445 const TypeData::TypeQualifiers &qsrc = src->typeQualifiers, &qdst = dst->typeQualifiers; // optimization446 447 if ( (qsrc & qdst).any() ) { // common qualifier ?448 for ( unsigned int i = 0; i < NoTypeQualifier; i += 1 ) {// find common qualifiers430 TypeData::Qualifiers qsrc = src->qualifiers, qdst = dst->qualifiers; // optimization 431 432 if ( (qsrc & qdst).any() ) { // common qualifier ? 433 for ( int i = 0; i < NoQualifier; i += 1 ) { // find common qualifiers 449 434 if ( qsrc[i] && qdst[i] ) { 450 appendError( error, string( "duplicate " ) + DeclarationNode:: typeQualifierNames[i] );435 appendError( error, string( "duplicate " ) + DeclarationNode::qualifierName[i] ); 451 436 } // if 452 437 } // for … … 455 440 456 441 void DeclarationNode::checkStorageClasses( DeclarationNode * q ) { 457 const FuncSpec &src = funcSpec, &dst = q->funcSpec; // optimization458 if ( (src & dst).any() ) { // common specifier ?459 for ( unsigned int i = 0; i < NoFuncSpecifier; i += 1 ) { // find common specifier460 if ( src[i] && dst[i] ) {461 appendError( error, string( "duplicate " ) + DeclarationNode::funcSpecifierNames[i] );462 } // if463 } // for464 } // if465 466 442 if ( storageClass != NoStorageClass && q->storageClass != NoStorageClass ) { 467 443 if ( storageClass == q->storageClass ) { // duplicate qualifier 468 appendError( error, string( "duplicate " ) + storage ClassNames[ storageClass ] );444 appendError( error, string( "duplicate " ) + storageName[ storageClass ] ); 469 445 } else { // only one storage class 470 appendError( error, string( "conflicting " ) + storage ClassNames[ storageClass ] + " & " + storageClassNames[ q->storageClass ] );446 appendError( error, string( "conflicting " ) + storageName[ storageClass ] + " & " + storageName[ q->storageClass ] ); 471 447 q->storageClass = storageClass; // FIX ERROR, prevent assertions from triggering 472 448 } // if 473 449 } // if 474 475 450 appendError( error, q->error ); 476 451 } // DeclarationNode::checkStorageClasses 477 452 478 453 DeclarationNode * DeclarationNode::copyStorageClasses( DeclarationNode * q ) { 479 funcSpec = funcSpec | q->funcSpec;480 454 isInline = isInline || q->isInline; 455 isNoreturn = isNoreturn || q->isNoreturn; 481 456 // do not overwrite an existing value with NoStorageClass 482 457 if ( q->storageClass != NoStorageClass ) { … … 506 481 src = nullptr; 507 482 } else { 508 dst-> typeQualifiers |= src->typeQualifiers;483 dst->qualifiers |= src->qualifiers; 509 484 } // if 510 485 } // addQualifiersToType … … 564 539 switch ( dst->kind ) { 565 540 case TypeData::Unknown: 566 src-> typeQualifiers |= dst->typeQualifiers;541 src->qualifiers |= dst->qualifiers; 567 542 dst = src; 568 543 src = nullptr; 569 544 break; 570 545 case TypeData::Basic: 571 dst-> typeQualifiers |= src->typeQualifiers;546 dst->qualifiers |= src->qualifiers; 572 547 if ( src->kind != TypeData::Unknown ) { 573 548 assert( src->kind == TypeData::Basic ); … … 576 551 dst->basictype = src->basictype; 577 552 } else if ( src->basictype != DeclarationNode::NoBasicType ) 578 throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::basicTypeName s[ src->basictype ] + " in type: ", src );553 throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::basicTypeName[ src->basictype ] + " in type: ", src ); 579 554 580 555 if ( dst->complextype == DeclarationNode::NoComplexType ) { 581 556 dst->complextype = src->complextype; 582 557 } else if ( src->complextype != DeclarationNode::NoComplexType ) 583 throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::complexTypeName s[ src->complextype ] + " in type: ", src );558 throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::complexTypeName[ src->complextype ] + " in type: ", src ); 584 559 585 560 if ( dst->signedness == DeclarationNode::NoSignedness ) { 586 561 dst->signedness = src->signedness; 587 562 } else if ( src->signedness != DeclarationNode::NoSignedness ) 588 throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::signednessName s[ src->signedness ] + " in type: ", src );563 throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::signednessName[ src->signedness ] + " in type: ", src ); 589 564 590 565 if ( dst->length == DeclarationNode::NoLength ) { … … 593 568 dst->length = DeclarationNode::LongLong; 594 569 } else if ( src->length != DeclarationNode::NoLength ) 595 throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::lengthName s[ src->length ] + " in type: ", src );570 throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::lengthName[ src->length ] + " in type: ", src ); 596 571 } // if 597 572 break; … … 605 580 dst->base->aggInst.params = maybeClone( src->aggregate.actuals ); 606 581 } // if 607 dst->base-> typeQualifiers |= src->typeQualifiers;582 dst->base->qualifiers |= src->qualifiers; 608 583 src = nullptr; 609 584 break; … … 637 612 type->aggInst.hoistType = o->type->enumeration.body; 638 613 } // if 639 type-> typeQualifiers |= o->type->typeQualifiers;614 type->qualifiers |= o->type->qualifiers; 640 615 } else { 641 616 type = o->type; … … 793 768 p->type->base->aggInst.params = maybeClone( type->aggregate.actuals ); 794 769 } // if 795 p->type->base-> typeQualifiers |= type->typeQualifiers;770 p->type->base->qualifiers |= type->qualifiers; 796 771 break; 797 772 … … 830 805 lastArray->base->aggInst.params = maybeClone( type->aggregate.actuals ); 831 806 } // if 832 lastArray->base-> typeQualifiers |= type->typeQualifiers;807 lastArray->base->qualifiers |= type->qualifiers; 833 808 break; 834 809 default: … … 1030 1005 } // if 1031 1006 1007 // if ( variable.name ) { 1032 1008 if ( variable.tyClass != NoTypeClass ) { 1033 1009 static const TypeDecl::Kind kindMap[] = { TypeDecl::Any, TypeDecl::Dtype, TypeDecl::Ftype, TypeDecl::Ttype }; 1034 1010 assertf( sizeof(kindMap)/sizeof(kindMap[0] == NoTypeClass-1), "DeclarationNode::build: kindMap is out of sync." ); 1035 1011 assertf( variable.tyClass < sizeof(kindMap)/sizeof(kindMap[0]), "Variable's tyClass is out of bounds." ); 1012 // TypeDecl * ret = new TypeDecl( *variable.name, DeclarationNode::NoStorageClass, nullptr, kindMap[ variable.tyClass ] ); 1036 1013 TypeDecl * ret = new TypeDecl( *name, DeclarationNode::NoStorageClass, nullptr, kindMap[ variable.tyClass ] ); 1037 1014 buildList( variable.assertions, ret->get_assertions() ); … … 1040 1017 1041 1018 if ( type ) { 1042 // Function specifiers can only appear on a function definition/declaration. 1043 // 1044 // inline _Noreturn int f(); // allowed 1045 // inline _Noreturn int g( int i ); // allowed 1046 // inline _Noreturn int i; // disallowed 1047 if ( type->kind != TypeData::Function && funcSpec.any() ) { 1048 throw SemanticError( "invalid function specifier for ", this ); 1049 } // if 1050 return buildDecl( type, name ? *name : string( "" ), storageClass, maybeBuild< Expression >( bitfieldWidth ), funcSpec, linkage, asmName, maybeBuild< Initializer >(initializer), attributes )->set_extension( extension ); 1051 } // if 1052 1053 // SUE's cannot have function specifiers, either 1054 // 1055 // inlne _Noreturn struct S { ... }; // disallowed 1056 // inlne _Noreturn enum E { ... }; // disallowed 1057 if ( funcSpec.any() ) { 1058 throw SemanticError( "invalid function specifier for ", this ); 1059 } // if 1060 assertf( name, "ObjectDecl must a have name\n" ); 1061 return (new ObjectDecl( *name, storageClass, linkage, maybeBuild< Expression >( bitfieldWidth ), nullptr, maybeBuild< Initializer >( initializer ) ))->set_asmName( asmName )->set_extension( extension ); 1019 return buildDecl( type, name ? *name : string( "" ), storageClass, maybeBuild< Expression >( bitfieldWidth ), isInline, isNoreturn, linkage, asmName, maybeBuild< Initializer >(initializer), attributes )->set_extension( extension ); 1020 } // if 1021 1022 if ( ! isInline && ! isNoreturn ) { 1023 assertf( name, "ObjectDecl are assumed to have names\n" ); 1024 return (new ObjectDecl( *name, storageClass, linkage, maybeBuild< Expression >( bitfieldWidth ), nullptr, maybeBuild< Initializer >( initializer ) ))->set_asmName( asmName )->set_extension( extension ); 1025 } // if 1026 1027 throw SemanticError( "invalid function specifier ", this ); 1062 1028 } 1063 1029 … … 1066 1032 1067 1033 if ( attr.expr ) { 1034 // return new AttrType( buildQualifiers( type ), *attr.name, attr.expr->build() ); 1068 1035 return new AttrType( buildQualifiers( type ), *name, attr.expr->build(), attributes ); 1069 1036 } else if ( attr.type ) { 1037 // return new AttrType( buildQualifiers( type ), *attr.name, attr.type->buildType() ); 1070 1038 return new AttrType( buildQualifiers( type ), *name, attr.type->buildType(), attributes ); 1071 1039 } // if
Note:
See TracChangeset
for help on using the changeset viewer.