Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/DeclarationNode.cc

    rdd020c0 r43c89a7  
    1010// Created On       : Sat May 16 12:34:05 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Mar  3 21:38:34 2017
    13 // Update Count     : 862
     12// Last Modified On : Thu Feb 23 22:21:06 2017
     13// Update Count     : 775
    1414//
    1515
     
    3232
    3333// 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" };
     34const char * DeclarationNode::storageName[] = { "extern", "static", "auto", "register", "inline", "fortran", "_Noreturn", "_Thread_local", "NoStorageClass" };
     35const char * DeclarationNode::qualifierName[] = { "const", "restrict", "volatile", "lvalue", "_Atomic", "NoQualifier" };
     36const char * DeclarationNode::basicTypeName[] = { "void", "_Bool", "char", "int", "float", "double", "long double", "NoBasicType" };
     37const char * DeclarationNode::complexTypeName[] = { "_Complex", "_Imaginary", "NoComplexType" };
     38const char * DeclarationNode::signednessName[] = { "signed", "unsigned", "NoSignedness" };
     39const char * DeclarationNode::lengthName[] = { "short", "long", "long long", "NoLength" };
     40const char * DeclarationNode::aggregateName[] = { "struct", "union", "context" };
     41const char * DeclarationNode::typeClassName[] = { "otype", "dtype", "ftype" };
     42const char * DeclarationNode::builtinTypeName[] = { "__builtin_va_list" };
    4443
    4544UniqueName DeclarationNode::anonymous( "__anonymous" );
     
    5150                storageClass( NoStorageClass ),
    5251                bitfieldWidth( nullptr ),
     52                isInline( false ),
     53                isNoreturn( false ),
    5354                hasEllipsis( false ),
    5455                linkage( ::linkage ),
     
    9192        newnode->storageClass = storageClass;
    9293        newnode->bitfieldWidth = maybeClone( bitfieldWidth );
    93         newnode->funcSpec = funcSpec;
     94        newnode->isInline = isInline;
     95        newnode->isNoreturn = isNoreturn;
    9496        newnode->enumeratorValue.reset( maybeClone( enumeratorValue.get() ) );
    9597        newnode->hasEllipsis = hasEllipsis;
     
    116118}
    117119
    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                         } // if
    124                 } // for
    125         } // if
    126 } // print_FuncSpec
    127 
    128120void DeclarationNode::print( std::ostream &os, int indent ) const {
    129121        os << string( indent, ' ' );
     
    138130        } // if
    139131
    140         if ( storageClass != NoStorageClass ) os << DeclarationNode::storageClassNames[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] << ' ';
    143135        if ( type ) {
    144136                type->print( os, indent );
     
    191183} // DeclarationNode::newFunction
    192184
     185DeclarationNode * 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
     192DeclarationNode * 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
    193198
    194199DeclarationNode * DeclarationNode::newStorageClass( DeclarationNode::StorageClass sc ) {
     
    197202        return newnode;
    198203} // 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::newFuncSpecifier
    205 
    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::newQualifier
    212204
    213205DeclarationNode * DeclarationNode::newBasicType( BasicType bt ) {
     
    238230        return newnode;
    239231} // 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::newForall
    247232
    248233DeclarationNode * DeclarationNode::newFromTypedef( string * name ) {
     
    443428
    444429void DeclarationNode::checkQualifiers( const TypeData * src, const TypeData * dst ) {
    445         const TypeData::TypeQualifiers &qsrc = src->typeQualifiers, &qdst = dst->typeQualifiers; // optimization
    446 
    447         if ( (qsrc & qdst).any() ) {                                             // common qualifier ?
    448                 for ( unsigned int i = 0; i < NoTypeQualifier; i += 1 ) { // find common qualifiers
     430        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
    449434                        if ( qsrc[i] && qdst[i] ) {
    450                                 appendError( error, string( "duplicate " ) + DeclarationNode::typeQualifierNames[i] );
     435                                appendError( error, string( "duplicate " ) + DeclarationNode::qualifierName[i] );
    451436                        } // if
    452437                } // for
     
    455440
    456441void DeclarationNode::checkStorageClasses( DeclarationNode * q ) {
    457         const FuncSpec &src = funcSpec, &dst = q->funcSpec; // optimization
    458         if ( (src & dst).any() ) {                                                      // common specifier ?
    459                 for ( unsigned int i = 0; i < NoFuncSpecifier; i += 1 ) { // find common specifier
    460                         if ( src[i] && dst[i] ) {
    461                                 appendError( error, string( "duplicate " ) + DeclarationNode::funcSpecifierNames[i] );
    462                         } // if
    463                 } // for
    464         } // if
    465 
    466442        if ( storageClass != NoStorageClass && q->storageClass != NoStorageClass ) {
    467443                if ( storageClass == q->storageClass ) {                // duplicate qualifier
    468                         appendError( error, string( "duplicate " ) + storageClassNames[ storageClass ] );
     444                        appendError( error, string( "duplicate " ) + storageName[ storageClass ] );
    469445                } else {                                                                                // only one storage class
    470                         appendError( error, string( "conflicting " ) + storageClassNames[ storageClass ] + " & " + storageClassNames[ q->storageClass ] );
     446                        appendError( error, string( "conflicting " ) + storageName[ storageClass ] + " & " + storageName[ q->storageClass ] );
    471447                        q->storageClass = storageClass;                         // FIX ERROR, prevent assertions from triggering
    472448                } // if
    473449        } // if
    474 
    475450        appendError( error, q->error );
    476451} // DeclarationNode::checkStorageClasses
    477452
    478453DeclarationNode * DeclarationNode::copyStorageClasses( DeclarationNode * q ) {
    479         funcSpec = funcSpec | q->funcSpec;
    480 
     454        isInline = isInline || q->isInline;
     455        isNoreturn = isNoreturn || q->isNoreturn;
    481456        // do not overwrite an existing value with NoStorageClass
    482457        if ( q->storageClass != NoStorageClass ) {
     
    506481                src = nullptr;
    507482        } else {
    508                 dst->typeQualifiers |= src->typeQualifiers;
     483                dst->qualifiers |= src->qualifiers;
    509484        } // if
    510485} // addQualifiersToType
     
    564539                switch ( dst->kind ) {
    565540                  case TypeData::Unknown:
    566                         src->typeQualifiers |= dst->typeQualifiers;
     541                        src->qualifiers |= dst->qualifiers;
    567542                        dst = src;
    568543                        src = nullptr;
    569544                        break;
    570545                  case TypeData::Basic:
    571                         dst->typeQualifiers |= src->typeQualifiers;
     546                        dst->qualifiers |= src->qualifiers;
    572547                        if ( src->kind != TypeData::Unknown ) {
    573548                                assert( src->kind == TypeData::Basic );
     
    576551                                        dst->basictype = src->basictype;
    577552                                } else if ( src->basictype != DeclarationNode::NoBasicType )
    578                                         throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::basicTypeNames[ src->basictype ] + " in type: ", src );
     553                                        throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::basicTypeName[ src->basictype ] + " in type: ", src );
    579554
    580555                                if ( dst->complextype == DeclarationNode::NoComplexType ) {
    581556                                        dst->complextype = src->complextype;
    582557                                } else if ( src->complextype != DeclarationNode::NoComplexType )
    583                                         throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::complexTypeNames[ src->complextype ] + " in type: ", src );
     558                                        throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::complexTypeName[ src->complextype ] + " in type: ", src );
    584559
    585560                                if ( dst->signedness == DeclarationNode::NoSignedness ) {
    586561                                        dst->signedness = src->signedness;
    587562                                } else if ( src->signedness != DeclarationNode::NoSignedness )
    588                                         throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::signednessNames[ src->signedness ] + " in type: ", src );
     563                                        throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::signednessName[ src->signedness ] + " in type: ", src );
    589564
    590565                                if ( dst->length == DeclarationNode::NoLength ) {
     
    593568                                        dst->length = DeclarationNode::LongLong;
    594569                                } else if ( src->length != DeclarationNode::NoLength )
    595                                         throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::lengthNames[ src->length ] + " in type: ", src );
     570                                        throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::lengthName[ src->length ] + " in type: ", src );
    596571                        } // if
    597572                        break;
     
    605580                                        dst->base->aggInst.params = maybeClone( src->aggregate.actuals );
    606581                                } // if
    607                                 dst->base->typeQualifiers |= src->typeQualifiers;
     582                                dst->base->qualifiers |= src->qualifiers;
    608583                                src = nullptr;
    609584                                break;
     
    637612                                                type->aggInst.hoistType = o->type->enumeration.body;
    638613                                        } // if
    639                                         type->typeQualifiers |= o->type->typeQualifiers;
     614                                        type->qualifiers |= o->type->qualifiers;
    640615                                } else {
    641616                                        type = o->type;
     
    793768                                        p->type->base->aggInst.params = maybeClone( type->aggregate.actuals );
    794769                                } // if
    795                                 p->type->base->typeQualifiers |= type->typeQualifiers;
     770                                p->type->base->qualifiers |= type->qualifiers;
    796771                                break;
    797772
     
    830805                                        lastArray->base->aggInst.params = maybeClone( type->aggregate.actuals );
    831806                                } // if
    832                                 lastArray->base->typeQualifiers |= type->typeQualifiers;
     807                                lastArray->base->qualifiers |= type->qualifiers;
    833808                                break;
    834809                          default:
     
    10301005        } // if
    10311006
     1007//      if ( variable.name ) {
    10321008        if ( variable.tyClass != NoTypeClass ) {
    10331009                static const TypeDecl::Kind kindMap[] = { TypeDecl::Any, TypeDecl::Dtype, TypeDecl::Ftype, TypeDecl::Ttype };
    10341010                assertf( sizeof(kindMap)/sizeof(kindMap[0] == NoTypeClass-1), "DeclarationNode::build: kindMap is out of sync." );
    10351011                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 ] );
    10361013                TypeDecl * ret = new TypeDecl( *name, DeclarationNode::NoStorageClass, nullptr, kindMap[ variable.tyClass ] );
    10371014                buildList( variable.assertions, ret->get_assertions() );
     
    10401017
    10411018        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 );
    10621028}
    10631029
     
    10661032
    10671033        if ( attr.expr ) {
     1034//              return new AttrType( buildQualifiers( type ), *attr.name, attr.expr->build() );
    10681035                return new AttrType( buildQualifiers( type ), *name, attr.expr->build(), attributes );
    10691036        } else if ( attr.type ) {
     1037//              return new AttrType( buildQualifiers( type ), *attr.name, attr.type->buildType() );
    10701038                return new AttrType( buildQualifiers( type ), *name, attr.type->buildType(), attributes );
    10711039        } // if
Note: See TracChangeset for help on using the changeset viewer.