Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/DeclarationNode.cc

    rdd020c0 r3a5131ed  
    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 16 13:06:50 2017
     13// Update Count     : 753
    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 );
     
    182174        } // if
    183175
     176        if ( body ) {
     177                newnode->type->function.hasBody = true;
     178        } // if
     179
    184180        if ( ret ) {
    185181                newnode->type->base = ret->type;
     
    191187} // DeclarationNode::newFunction
    192188
     189DeclarationNode * DeclarationNode::newQualifier( Qualifier q ) {
     190        DeclarationNode * newnode = new DeclarationNode;
     191        newnode->type = new TypeData();
     192        newnode->type->qualifiers[ q ] = 1;
     193        return newnode;
     194} // DeclarationNode::newQualifier
     195
     196DeclarationNode * DeclarationNode::newForall( DeclarationNode * forall ) {
     197        DeclarationNode * newnode = new DeclarationNode;
     198        newnode->type = new TypeData( TypeData::Unknown );
     199        newnode->type->forall = forall;
     200        return newnode;
     201} // DeclarationNode::newForall
    193202
    194203DeclarationNode * DeclarationNode::newStorageClass( DeclarationNode::StorageClass sc ) {
     
    197206        return newnode;
    198207} // 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
    212208
    213209DeclarationNode * DeclarationNode::newBasicType( BasicType bt ) {
     
    238234        return newnode;
    239235} // 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
    247236
    248237DeclarationNode * DeclarationNode::newFromTypedef( string * name ) {
     
    270259} // DeclarationNode::newAggregate
    271260
    272 DeclarationNode * DeclarationNode::newEnum( string * name, DeclarationNode * constants, bool body ) {
     261DeclarationNode * DeclarationNode::newEnum( string * name, DeclarationNode * constants ) {
    273262        DeclarationNode * newnode = new DeclarationNode;
    274263        newnode->type = new TypeData( TypeData::Enum );
     
    279268        } // if
    280269        newnode->type->enumeration.constants = constants;
    281         newnode->type->enumeration.body = body;
    282270        return newnode;
    283271} // DeclarationNode::newEnum
     
    443431
    444432void 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
     433        TypeData::Qualifiers qsrc = src->qualifiers, qdst = dst->qualifiers; // optimization
     434
     435        if ( (qsrc & qdst).any() ) {                                            // common qualifier ?
     436                for ( int i = 0; i < NoQualifier; i += 1 ) {    // find common qualifiers
    449437                        if ( qsrc[i] && qdst[i] ) {
    450                                 appendError( error, string( "duplicate " ) + DeclarationNode::typeQualifierNames[i] );
     438                                appendError( error, string( "duplicate " ) + DeclarationNode::qualifierName[i] );
    451439                        } // if
    452440                } // for
     
    455443
    456444void 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 
    466445        if ( storageClass != NoStorageClass && q->storageClass != NoStorageClass ) {
    467446                if ( storageClass == q->storageClass ) {                // duplicate qualifier
    468                         appendError( error, string( "duplicate " ) + storageClassNames[ storageClass ] );
     447                        appendError( error, string( "duplicate " ) + storageName[ storageClass ] );
    469448                } else {                                                                                // only one storage class
    470                         appendError( error, string( "conflicting " ) + storageClassNames[ storageClass ] + " & " + storageClassNames[ q->storageClass ] );
     449                        appendError( error, string( "conflicting " ) + storageName[ storageClass ] + " & " + storageName[ q->storageClass ] );
    471450                        q->storageClass = storageClass;                         // FIX ERROR, prevent assertions from triggering
    472451                } // if
    473452        } // if
    474 
    475453        appendError( error, q->error );
    476454} // DeclarationNode::checkStorageClasses
    477455
    478456DeclarationNode * DeclarationNode::copyStorageClasses( DeclarationNode * q ) {
    479         funcSpec = funcSpec | q->funcSpec;
    480 
     457        isInline = isInline || q->isInline;
     458        isNoreturn = isNoreturn || q->isNoreturn;
    481459        // do not overwrite an existing value with NoStorageClass
    482460        if ( q->storageClass != NoStorageClass ) {
     
    506484                src = nullptr;
    507485        } else {
    508                 dst->typeQualifiers |= src->typeQualifiers;
     486                dst->qualifiers |= src->qualifiers;
    509487        } // if
    510488} // addQualifiersToType
     
    564542                switch ( dst->kind ) {
    565543                  case TypeData::Unknown:
    566                         src->typeQualifiers |= dst->typeQualifiers;
     544                        src->qualifiers |= dst->qualifiers;
    567545                        dst = src;
    568546                        src = nullptr;
    569547                        break;
    570548                  case TypeData::Basic:
    571                         dst->typeQualifiers |= src->typeQualifiers;
     549                        dst->qualifiers |= src->qualifiers;
    572550                        if ( src->kind != TypeData::Unknown ) {
    573551                                assert( src->kind == TypeData::Basic );
     
    576554                                        dst->basictype = src->basictype;
    577555                                } else if ( src->basictype != DeclarationNode::NoBasicType )
    578                                         throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::basicTypeNames[ src->basictype ] + " in type: ", src );
     556                                        throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::basicTypeName[ src->basictype ] + " in type: ", src );
    579557
    580558                                if ( dst->complextype == DeclarationNode::NoComplexType ) {
    581559                                        dst->complextype = src->complextype;
    582560                                } else if ( src->complextype != DeclarationNode::NoComplexType )
    583                                         throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::complexTypeNames[ src->complextype ] + " in type: ", src );
     561                                        throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::complexTypeName[ src->complextype ] + " in type: ", src );
    584562
    585563                                if ( dst->signedness == DeclarationNode::NoSignedness ) {
    586564                                        dst->signedness = src->signedness;
    587565                                } else if ( src->signedness != DeclarationNode::NoSignedness )
    588                                         throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::signednessNames[ src->signedness ] + " in type: ", src );
     566                                        throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::signednessName[ src->signedness ] + " in type: ", src );
    589567
    590568                                if ( dst->length == DeclarationNode::NoLength ) {
     
    593571                                        dst->length = DeclarationNode::LongLong;
    594572                                } else if ( src->length != DeclarationNode::NoLength )
    595                                         throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::lengthNames[ src->length ] + " in type: ", src );
     573                                        throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::lengthName[ src->length ] + " in type: ", src );
    596574                        } // if
    597575                        break;
     
    605583                                        dst->base->aggInst.params = maybeClone( src->aggregate.actuals );
    606584                                } // if
    607                                 dst->base->typeQualifiers |= src->typeQualifiers;
     585                                dst->base->qualifiers |= src->qualifiers;
    608586                                src = nullptr;
    609587                                break;
     
    632610                                        type->aggInst.aggregate = o->type;
    633611                                        if ( o->type->kind == TypeData::Aggregate ) {
    634                                                 type->aggInst.hoistType = o->type->aggregate.body;
    635612                                                type->aggInst.params = maybeClone( o->type->aggregate.actuals );
    636                                         } else {
    637                                                 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;
     
    723698        assert( ! type->function.body );
    724699        type->function.body = body;
     700        type->function.hasBody = true;
    725701        return this;
    726702}
     
    793769                                        p->type->base->aggInst.params = maybeClone( type->aggregate.actuals );
    794770                                } // if
    795                                 p->type->base->typeQualifiers |= type->typeQualifiers;
     771                                p->type->base->qualifiers |= type->qualifiers;
    796772                                break;
    797773
     
    830806                                        lastArray->base->aggInst.params = maybeClone( type->aggregate.actuals );
    831807                                } // if
    832                                 lastArray->base->typeQualifiers |= type->typeQualifiers;
     808                                lastArray->base->qualifiers |= type->qualifiers;
    833809                                break;
    834810                          default:
     
    909885                                newType->aggInst.aggregate->aggregate.fields = nullptr;
    910886                        } // if
    911                         // don't hoist twice
    912                         newType->aggInst.hoistType = false;
    913887                } // if
    914888
     
    971945        SemanticError errors;
    972946        std::back_insert_iterator< std::list< DeclarationWithType * > > out( outputList );
    973 
     947       
    974948        for ( const DeclarationNode * cur = firstNode; cur; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ) ) {
    975949                try {
     
    983957                                        auto obj = new ObjectDecl( "", DeclarationNode::NoStorageClass, linkage, nullptr, inst, nullptr );
    984958                                        obj->location = cur->location;
    985                                         * out++ = obj;
     959                                        * out++ = obj; 
    986960                                        delete agg;
    987961                                } else if ( UnionDecl * agg = dynamic_cast< UnionDecl * >( decl ) ) {
     
    10301004        } // if
    10311005
     1006//      if ( variable.name ) {
    10321007        if ( variable.tyClass != NoTypeClass ) {
    10331008                static const TypeDecl::Kind kindMap[] = { TypeDecl::Any, TypeDecl::Dtype, TypeDecl::Ftype, TypeDecl::Ttype };
    10341009                assertf( sizeof(kindMap)/sizeof(kindMap[0] == NoTypeClass-1), "DeclarationNode::build: kindMap is out of sync." );
    10351010                assertf( variable.tyClass < sizeof(kindMap)/sizeof(kindMap[0]), "Variable's tyClass is out of bounds." );
     1011//              TypeDecl * ret = new TypeDecl( *variable.name, DeclarationNode::NoStorageClass, nullptr, kindMap[ variable.tyClass ] );
    10361012                TypeDecl * ret = new TypeDecl( *name, DeclarationNode::NoStorageClass, nullptr, kindMap[ variable.tyClass ] );
    10371013                buildList( variable.assertions, ret->get_assertions() );
     
    10401016
    10411017        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 );
     1018                return buildDecl( type, name ? *name : string( "" ), storageClass, maybeBuild< Expression >( bitfieldWidth ), isInline, isNoreturn, linkage, asmName, maybeBuild< Initializer >(initializer), attributes )->set_extension( extension );
     1019        } // if
     1020
     1021        if ( ! isInline && ! isNoreturn ) {
     1022                assertf( name, "ObjectDecl are assumed to have names\n" );
     1023                return (new ObjectDecl( *name, storageClass, linkage, maybeBuild< Expression >( bitfieldWidth ), nullptr, maybeBuild< Initializer >( initializer ) ))->set_asmName( asmName )->set_extension( extension );
     1024        } // if
     1025
     1026        throw SemanticError( "invalid function specifier ", this );
    10621027}
    10631028
     
    10661031
    10671032        if ( attr.expr ) {
     1033//              return new AttrType( buildQualifiers( type ), *attr.name, attr.expr->build() );
    10681034                return new AttrType( buildQualifiers( type ), *name, attr.expr->build(), attributes );
    10691035        } else if ( attr.type ) {
     1036//              return new AttrType( buildQualifiers( type ), *attr.name, attr.type->buildType() );
    10701037                return new AttrType( buildQualifiers( type ), *name, attr.type->buildType(), attributes );
    10711038        } // if
    10721039
    10731040        switch ( type->kind ) {
    1074           case TypeData::Enum:
     1041          case TypeData::Enum: {
     1042                  EnumDecl * typedecl = buildEnum( type, attributes );
     1043                  return new EnumInstType( buildQualifiers( type ), typedecl );
     1044          }
    10751045          case TypeData::Aggregate: {
    1076                   ReferenceToType * ret = buildComAggInst( type, attributes );
     1046                  AggregateDecl * typedecl = buildAggregate( type, attributes );
     1047                  ReferenceToType * ret;
     1048                  switch ( type->aggregate.kind ) {
     1049                        case DeclarationNode::Struct:
     1050                          ret = new StructInstType( buildQualifiers( type ), (StructDecl *)typedecl );
     1051                          break;
     1052                        case DeclarationNode::Union:
     1053                          ret = new UnionInstType( buildQualifiers( type ), (UnionDecl *)typedecl );
     1054                          break;
     1055                        case DeclarationNode::Trait:
     1056                          assert( false );
     1057                          //ret = new TraitInstType( buildQualifiers( type ), (TraitDecl *)typedecl );
     1058                          break;
     1059                        default:
     1060                          assert( false );
     1061                  } // switch
    10771062                  buildList( type->aggregate.actuals, ret->get_parameters() );
    10781063                  return ret;
Note: See TracChangeset for help on using the changeset viewer.