Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/DeclarationNode.cc

    r3a5131ed rdd020c0  
    1010// Created On       : Sat May 16 12:34:05 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Feb 16 13:06:50 2017
    13 // Update Count     : 753
     12// Last Modified On : Fri Mar  3 21:38:34 2017
     13// Update Count     : 862
    1414//
    1515
     
    3232
    3333// These must remain in the same order as the corresponding DeclarationNode enumerations.
    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" };
     34const char * DeclarationNode::storageClassNames[] = { "extern", "static", "auto", "register", "_Thread_local", "inline", "fortran", "_Noreturn", "NoStorageClassNames" };
     35const char * DeclarationNode::funcSpecifierNames[] = { "inline", "fortran", "_Noreturn", "NoFunctionSpecifierNames" };
     36const char * DeclarationNode::typeQualifierNames[] = { "const", "restrict", "volatile", "lvalue", "_Atomic", "NoTypeQualifierNames" };
     37const char * DeclarationNode::basicTypeNames[] = { "void", "_Bool", "char", "int", "float", "double", "long double", "NoBasicTypeNames" };
     38const char * DeclarationNode::complexTypeNames[] = { "_Complex", "_Imaginary", "NoComplexTypeNames" };
     39const char * DeclarationNode::signednessNames[] = { "signed", "unsigned", "NoSignednessNames" };
     40const char * DeclarationNode::lengthNames[] = { "short", "long", "long long", "NoLengthNames" };
     41const char * DeclarationNode::aggregateNames[] = { "struct", "union", "context", "NoAggregateNames" };
     42const char * DeclarationNode::typeClassNames[] = { "otype", "dtype", "ftype", "NoTypeClassNames" };
     43const char * DeclarationNode::builtinTypeNames[] = { "__builtin_va_list", "NoBuiltinTypeNames" };
    4344
    4445UniqueName DeclarationNode::anonymous( "__anonymous" );
     
    5051                storageClass( NoStorageClass ),
    5152                bitfieldWidth( nullptr ),
    52                 isInline( false ),
    53                 isNoreturn( false ),
    5453                hasEllipsis( false ),
    5554                linkage( ::linkage ),
     
    9291        newnode->storageClass = storageClass;
    9392        newnode->bitfieldWidth = maybeClone( bitfieldWidth );
    94         newnode->isInline = isInline;
    95         newnode->isNoreturn = isNoreturn;
     93        newnode->funcSpec = funcSpec;
    9694        newnode->enumeratorValue.reset( maybeClone( enumeratorValue.get() ) );
    9795        newnode->hasEllipsis = hasEllipsis;
     
    118116}
    119117
     118void 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
    120128void DeclarationNode::print( std::ostream &os, int indent ) const {
    121129        os << string( indent, ' ' );
     
    130138        } // if
    131139
    132         if ( storageClass != NoStorageClass ) os << DeclarationNode::storageName[storageClass] << ' ';
    133         if ( isInline ) os << DeclarationNode::storageName[Inline] << ' ';
    134         if ( isNoreturn ) os << DeclarationNode::storageName[Noreturn] << ' ';
     140        if ( storageClass != NoStorageClass ) os << DeclarationNode::storageClassNames[storageClass] << ' ';
     141        print_FuncSpec( os, funcSpec );
     142
    135143        if ( type ) {
    136144                type->print( os, indent );
     
    174182        } // if
    175183
    176         if ( body ) {
    177                 newnode->type->function.hasBody = true;
    178         } // if
    179 
    180184        if ( ret ) {
    181185                newnode->type->base = ret->type;
     
    187191} // DeclarationNode::newFunction
    188192
    189 DeclarationNode * DeclarationNode::newQualifier( Qualifier q ) {
     193
     194DeclarationNode * DeclarationNode::newStorageClass( DeclarationNode::StorageClass sc ) {
     195        DeclarationNode * newnode = new DeclarationNode;
     196        newnode->storageClass = sc;
     197        return newnode;
     198} // DeclarationNode::newStorageClass
     199
     200DeclarationNode * DeclarationNode::newFuncSpecifier( DeclarationNode::FuncSpecifier fs ) {
     201        DeclarationNode * newnode = new DeclarationNode;
     202        newnode->funcSpec[ fs ] = true;
     203        return newnode;
     204} // DeclarationNode::newFuncSpecifier
     205
     206DeclarationNode * DeclarationNode::newTypeQualifier( TypeQualifier tq ) {
    190207        DeclarationNode * newnode = new DeclarationNode;
    191208        newnode->type = new TypeData();
    192         newnode->type->qualifiers[ q ] = 1;
     209        newnode->type->typeQualifiers[ tq ] = true;
    193210        return newnode;
    194211} // DeclarationNode::newQualifier
     212
     213DeclarationNode * DeclarationNode::newBasicType( BasicType bt ) {
     214        DeclarationNode * newnode = new DeclarationNode;
     215        newnode->type = new TypeData( TypeData::Basic );
     216        newnode->type->basictype = bt;
     217        return newnode;
     218} // DeclarationNode::newBasicType
     219
     220DeclarationNode * DeclarationNode::newComplexType( ComplexType ct ) {
     221        DeclarationNode * newnode = new DeclarationNode;
     222        newnode->type = new TypeData( TypeData::Basic );
     223        newnode->type->complextype = ct;
     224        return newnode;
     225} // DeclarationNode::newComplexType
     226
     227DeclarationNode * DeclarationNode::newSignedNess( Signedness sn ) {
     228        DeclarationNode * newnode = new DeclarationNode;
     229        newnode->type = new TypeData( TypeData::Basic );
     230        newnode->type->signedness = sn;
     231        return newnode;
     232} // DeclarationNode::newSignedNess
     233
     234DeclarationNode * DeclarationNode::newLength( Length lnth ) {
     235        DeclarationNode * newnode = new DeclarationNode;
     236        newnode->type = new TypeData( TypeData::Basic );
     237        newnode->type->length = lnth;
     238        return newnode;
     239} // DeclarationNode::newLength
    195240
    196241DeclarationNode * DeclarationNode::newForall( DeclarationNode * forall ) {
     
    200245        return newnode;
    201246} // DeclarationNode::newForall
    202 
    203 DeclarationNode * DeclarationNode::newStorageClass( DeclarationNode::StorageClass sc ) {
    204         DeclarationNode * newnode = new DeclarationNode;
    205         newnode->storageClass = sc;
    206         return newnode;
    207 } // DeclarationNode::newStorageClass
    208 
    209 DeclarationNode * DeclarationNode::newBasicType( BasicType bt ) {
    210         DeclarationNode * newnode = new DeclarationNode;
    211         newnode->type = new TypeData( TypeData::Basic );
    212         newnode->type->basictype = bt;
    213         return newnode;
    214 } // DeclarationNode::newBasicType
    215 
    216 DeclarationNode * DeclarationNode::newComplexType( ComplexType ct ) {
    217         DeclarationNode * newnode = new DeclarationNode;
    218         newnode->type = new TypeData( TypeData::Basic );
    219         newnode->type->complextype = ct;
    220         return newnode;
    221 } // DeclarationNode::newComplexType
    222 
    223 DeclarationNode * DeclarationNode::newSignedNess( Signedness sn ) {
    224         DeclarationNode * newnode = new DeclarationNode;
    225         newnode->type = new TypeData( TypeData::Basic );
    226         newnode->type->signedness = sn;
    227         return newnode;
    228 } // DeclarationNode::newSignedNess
    229 
    230 DeclarationNode * DeclarationNode::newLength( Length lnth ) {
    231         DeclarationNode * newnode = new DeclarationNode;
    232         newnode->type = new TypeData( TypeData::Basic );
    233         newnode->type->length = lnth;
    234         return newnode;
    235 } // DeclarationNode::newLength
    236247
    237248DeclarationNode * DeclarationNode::newFromTypedef( string * name ) {
     
    259270} // DeclarationNode::newAggregate
    260271
    261 DeclarationNode * DeclarationNode::newEnum( string * name, DeclarationNode * constants ) {
     272DeclarationNode * DeclarationNode::newEnum( string * name, DeclarationNode * constants, bool body ) {
    262273        DeclarationNode * newnode = new DeclarationNode;
    263274        newnode->type = new TypeData( TypeData::Enum );
     
    268279        } // if
    269280        newnode->type->enumeration.constants = constants;
     281        newnode->type->enumeration.body = body;
    270282        return newnode;
    271283} // DeclarationNode::newEnum
     
    431443
    432444void DeclarationNode::checkQualifiers( const TypeData * src, const TypeData * dst ) {
    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
     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
    437449                        if ( qsrc[i] && qdst[i] ) {
    438                                 appendError( error, string( "duplicate " ) + DeclarationNode::qualifierName[i] );
     450                                appendError( error, string( "duplicate " ) + DeclarationNode::typeQualifierNames[i] );
    439451                        } // if
    440452                } // for
     
    443455
    444456void 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
    445466        if ( storageClass != NoStorageClass && q->storageClass != NoStorageClass ) {
    446467                if ( storageClass == q->storageClass ) {                // duplicate qualifier
    447                         appendError( error, string( "duplicate " ) + storageName[ storageClass ] );
     468                        appendError( error, string( "duplicate " ) + storageClassNames[ storageClass ] );
    448469                } else {                                                                                // only one storage class
    449                         appendError( error, string( "conflicting " ) + storageName[ storageClass ] + " & " + storageName[ q->storageClass ] );
     470                        appendError( error, string( "conflicting " ) + storageClassNames[ storageClass ] + " & " + storageClassNames[ q->storageClass ] );
    450471                        q->storageClass = storageClass;                         // FIX ERROR, prevent assertions from triggering
    451472                } // if
    452473        } // if
     474
    453475        appendError( error, q->error );
    454476} // DeclarationNode::checkStorageClasses
    455477
    456478DeclarationNode * DeclarationNode::copyStorageClasses( DeclarationNode * q ) {
    457         isInline = isInline || q->isInline;
    458         isNoreturn = isNoreturn || q->isNoreturn;
     479        funcSpec = funcSpec | q->funcSpec;
     480
    459481        // do not overwrite an existing value with NoStorageClass
    460482        if ( q->storageClass != NoStorageClass ) {
     
    484506                src = nullptr;
    485507        } else {
    486                 dst->qualifiers |= src->qualifiers;
     508                dst->typeQualifiers |= src->typeQualifiers;
    487509        } // if
    488510} // addQualifiersToType
     
    542564                switch ( dst->kind ) {
    543565                  case TypeData::Unknown:
    544                         src->qualifiers |= dst->qualifiers;
     566                        src->typeQualifiers |= dst->typeQualifiers;
    545567                        dst = src;
    546568                        src = nullptr;
    547569                        break;
    548570                  case TypeData::Basic:
    549                         dst->qualifiers |= src->qualifiers;
     571                        dst->typeQualifiers |= src->typeQualifiers;
    550572                        if ( src->kind != TypeData::Unknown ) {
    551573                                assert( src->kind == TypeData::Basic );
     
    554576                                        dst->basictype = src->basictype;
    555577                                } else if ( src->basictype != DeclarationNode::NoBasicType )
    556                                         throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::basicTypeName[ src->basictype ] + " in type: ", src );
     578                                        throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::basicTypeNames[ src->basictype ] + " in type: ", src );
    557579
    558580                                if ( dst->complextype == DeclarationNode::NoComplexType ) {
    559581                                        dst->complextype = src->complextype;
    560582                                } else if ( src->complextype != DeclarationNode::NoComplexType )
    561                                         throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::complexTypeName[ src->complextype ] + " in type: ", src );
     583                                        throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::complexTypeNames[ src->complextype ] + " in type: ", src );
    562584
    563585                                if ( dst->signedness == DeclarationNode::NoSignedness ) {
    564586                                        dst->signedness = src->signedness;
    565587                                } else if ( src->signedness != DeclarationNode::NoSignedness )
    566                                         throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::signednessName[ src->signedness ] + " in type: ", src );
     588                                        throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::signednessNames[ src->signedness ] + " in type: ", src );
    567589
    568590                                if ( dst->length == DeclarationNode::NoLength ) {
     
    571593                                        dst->length = DeclarationNode::LongLong;
    572594                                } else if ( src->length != DeclarationNode::NoLength )
    573                                         throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::lengthName[ src->length ] + " in type: ", src );
     595                                        throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::lengthNames[ src->length ] + " in type: ", src );
    574596                        } // if
    575597                        break;
     
    583605                                        dst->base->aggInst.params = maybeClone( src->aggregate.actuals );
    584606                                } // if
    585                                 dst->base->qualifiers |= src->qualifiers;
     607                                dst->base->typeQualifiers |= src->typeQualifiers;
    586608                                src = nullptr;
    587609                                break;
     
    610632                                        type->aggInst.aggregate = o->type;
    611633                                        if ( o->type->kind == TypeData::Aggregate ) {
     634                                                type->aggInst.hoistType = o->type->aggregate.body;
    612635                                                type->aggInst.params = maybeClone( o->type->aggregate.actuals );
     636                                        } else {
     637                                                type->aggInst.hoistType = o->type->enumeration.body;
    613638                                        } // if
    614                                         type->qualifiers |= o->type->qualifiers;
     639                                        type->typeQualifiers |= o->type->typeQualifiers;
    615640                                } else {
    616641                                        type = o->type;
     
    698723        assert( ! type->function.body );
    699724        type->function.body = body;
    700         type->function.hasBody = true;
    701725        return this;
    702726}
     
    769793                                        p->type->base->aggInst.params = maybeClone( type->aggregate.actuals );
    770794                                } // if
    771                                 p->type->base->qualifiers |= type->qualifiers;
     795                                p->type->base->typeQualifiers |= type->typeQualifiers;
    772796                                break;
    773797
     
    806830                                        lastArray->base->aggInst.params = maybeClone( type->aggregate.actuals );
    807831                                } // if
    808                                 lastArray->base->qualifiers |= type->qualifiers;
     832                                lastArray->base->typeQualifiers |= type->typeQualifiers;
    809833                                break;
    810834                          default:
     
    885909                                newType->aggInst.aggregate->aggregate.fields = nullptr;
    886910                        } // if
     911                        // don't hoist twice
     912                        newType->aggInst.hoistType = false;
    887913                } // if
    888914
     
    945971        SemanticError errors;
    946972        std::back_insert_iterator< std::list< DeclarationWithType * > > out( outputList );
    947        
     973
    948974        for ( const DeclarationNode * cur = firstNode; cur; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ) ) {
    949975                try {
     
    957983                                        auto obj = new ObjectDecl( "", DeclarationNode::NoStorageClass, linkage, nullptr, inst, nullptr );
    958984                                        obj->location = cur->location;
    959                                         * out++ = obj; 
     985                                        * out++ = obj;
    960986                                        delete agg;
    961987                                } else if ( UnionDecl * agg = dynamic_cast< UnionDecl * >( decl ) ) {
     
    10041030        } // if
    10051031
    1006 //      if ( variable.name ) {
    10071032        if ( variable.tyClass != NoTypeClass ) {
    10081033                static const TypeDecl::Kind kindMap[] = { TypeDecl::Any, TypeDecl::Dtype, TypeDecl::Ftype, TypeDecl::Ttype };
    10091034                assertf( sizeof(kindMap)/sizeof(kindMap[0] == NoTypeClass-1), "DeclarationNode::build: kindMap is out of sync." );
    10101035                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 ] );
    10121036                TypeDecl * ret = new TypeDecl( *name, DeclarationNode::NoStorageClass, nullptr, kindMap[ variable.tyClass ] );
    10131037                buildList( variable.assertions, ret->get_assertions() );
     
    10161040
    10171041        if ( type ) {
    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 );
     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 );
    10271062}
    10281063
     
    10311066
    10321067        if ( attr.expr ) {
    1033 //              return new AttrType( buildQualifiers( type ), *attr.name, attr.expr->build() );
    10341068                return new AttrType( buildQualifiers( type ), *name, attr.expr->build(), attributes );
    10351069        } else if ( attr.type ) {
    1036 //              return new AttrType( buildQualifiers( type ), *attr.name, attr.type->buildType() );
    10371070                return new AttrType( buildQualifiers( type ), *name, attr.type->buildType(), attributes );
    10381071        } // if
    10391072
    10401073        switch ( type->kind ) {
    1041           case TypeData::Enum: {
    1042                   EnumDecl * typedecl = buildEnum( type, attributes );
    1043                   return new EnumInstType( buildQualifiers( type ), typedecl );
    1044           }
     1074          case TypeData::Enum:
    10451075          case TypeData::Aggregate: {
    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
     1076                  ReferenceToType * ret = buildComAggInst( type, attributes );
    10621077                  buildList( type->aggregate.actuals, ret->get_parameters() );
    10631078                  return ret;
Note: See TracChangeset for help on using the changeset viewer.