Ignore:
Timestamp:
Mar 6, 2017, 2:07:04 PM (7 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
2988eeb
Parents:
df47e2f (diff), d107010 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/DeclarationNode.cc

    rdf47e2f r3f80888  
    1010// Created On       : Sat May 16 12:34:05 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Feb 23 22:21:06 2017
    13 // Update Count     : 775
     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 );
     
    183191} // DeclarationNode::newFunction
    184192
    185 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 ) {
    186207        DeclarationNode * newnode = new DeclarationNode;
    187208        newnode->type = new TypeData();
    188         newnode->type->qualifiers[ q ] = 1;
     209        newnode->type->typeQualifiers[ tq ] = true;
    189210        return newnode;
    190211} // 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
    191240
    192241DeclarationNode * DeclarationNode::newForall( DeclarationNode * forall ) {
     
    196245        return newnode;
    197246} // DeclarationNode::newForall
    198 
    199 DeclarationNode * DeclarationNode::newStorageClass( DeclarationNode::StorageClass sc ) {
    200         DeclarationNode * newnode = new DeclarationNode;
    201         newnode->storageClass = sc;
    202         return newnode;
    203 } // DeclarationNode::newStorageClass
    204 
    205 DeclarationNode * DeclarationNode::newBasicType( BasicType bt ) {
    206         DeclarationNode * newnode = new DeclarationNode;
    207         newnode->type = new TypeData( TypeData::Basic );
    208         newnode->type->basictype = bt;
    209         return newnode;
    210 } // DeclarationNode::newBasicType
    211 
    212 DeclarationNode * DeclarationNode::newComplexType( ComplexType ct ) {
    213         DeclarationNode * newnode = new DeclarationNode;
    214         newnode->type = new TypeData( TypeData::Basic );
    215         newnode->type->complextype = ct;
    216         return newnode;
    217 } // DeclarationNode::newComplexType
    218 
    219 DeclarationNode * DeclarationNode::newSignedNess( Signedness sn ) {
    220         DeclarationNode * newnode = new DeclarationNode;
    221         newnode->type = new TypeData( TypeData::Basic );
    222         newnode->type->signedness = sn;
    223         return newnode;
    224 } // DeclarationNode::newSignedNess
    225 
    226 DeclarationNode * DeclarationNode::newLength( Length lnth ) {
    227         DeclarationNode * newnode = new DeclarationNode;
    228         newnode->type = new TypeData( TypeData::Basic );
    229         newnode->type->length = lnth;
    230         return newnode;
    231 } // DeclarationNode::newLength
    232247
    233248DeclarationNode * DeclarationNode::newFromTypedef( string * name ) {
     
    428443
    429444void DeclarationNode::checkQualifiers( const TypeData * src, const TypeData * dst ) {
    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
     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
    434449                        if ( qsrc[i] && qdst[i] ) {
    435                                 appendError( error, string( "duplicate " ) + DeclarationNode::qualifierName[i] );
     450                                appendError( error, string( "duplicate " ) + DeclarationNode::typeQualifierNames[i] );
    436451                        } // if
    437452                } // for
     
    440455
    441456void 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
    442466        if ( storageClass != NoStorageClass && q->storageClass != NoStorageClass ) {
    443467                if ( storageClass == q->storageClass ) {                // duplicate qualifier
    444                         appendError( error, string( "duplicate " ) + storageName[ storageClass ] );
     468                        appendError( error, string( "duplicate " ) + storageClassNames[ storageClass ] );
    445469                } else {                                                                                // only one storage class
    446                         appendError( error, string( "conflicting " ) + storageName[ storageClass ] + " & " + storageName[ q->storageClass ] );
     470                        appendError( error, string( "conflicting " ) + storageClassNames[ storageClass ] + " & " + storageClassNames[ q->storageClass ] );
    447471                        q->storageClass = storageClass;                         // FIX ERROR, prevent assertions from triggering
    448472                } // if
    449473        } // if
     474
    450475        appendError( error, q->error );
    451476} // DeclarationNode::checkStorageClasses
    452477
    453478DeclarationNode * DeclarationNode::copyStorageClasses( DeclarationNode * q ) {
    454         isInline = isInline || q->isInline;
    455         isNoreturn = isNoreturn || q->isNoreturn;
     479        funcSpec = funcSpec | q->funcSpec;
     480
    456481        // do not overwrite an existing value with NoStorageClass
    457482        if ( q->storageClass != NoStorageClass ) {
     
    481506                src = nullptr;
    482507        } else {
    483                 dst->qualifiers |= src->qualifiers;
     508                dst->typeQualifiers |= src->typeQualifiers;
    484509        } // if
    485510} // addQualifiersToType
     
    539564                switch ( dst->kind ) {
    540565                  case TypeData::Unknown:
    541                         src->qualifiers |= dst->qualifiers;
     566                        src->typeQualifiers |= dst->typeQualifiers;
    542567                        dst = src;
    543568                        src = nullptr;
    544569                        break;
    545570                  case TypeData::Basic:
    546                         dst->qualifiers |= src->qualifiers;
     571                        dst->typeQualifiers |= src->typeQualifiers;
    547572                        if ( src->kind != TypeData::Unknown ) {
    548573                                assert( src->kind == TypeData::Basic );
     
    551576                                        dst->basictype = src->basictype;
    552577                                } else if ( src->basictype != DeclarationNode::NoBasicType )
    553                                         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 );
    554579
    555580                                if ( dst->complextype == DeclarationNode::NoComplexType ) {
    556581                                        dst->complextype = src->complextype;
    557582                                } else if ( src->complextype != DeclarationNode::NoComplexType )
    558                                         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 );
    559584
    560585                                if ( dst->signedness == DeclarationNode::NoSignedness ) {
    561586                                        dst->signedness = src->signedness;
    562587                                } else if ( src->signedness != DeclarationNode::NoSignedness )
    563                                         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 );
    564589
    565590                                if ( dst->length == DeclarationNode::NoLength ) {
     
    568593                                        dst->length = DeclarationNode::LongLong;
    569594                                } else if ( src->length != DeclarationNode::NoLength )
    570                                         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 );
    571596                        } // if
    572597                        break;
     
    580605                                        dst->base->aggInst.params = maybeClone( src->aggregate.actuals );
    581606                                } // if
    582                                 dst->base->qualifiers |= src->qualifiers;
     607                                dst->base->typeQualifiers |= src->typeQualifiers;
    583608                                src = nullptr;
    584609                                break;
     
    612637                                                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;
     
    768793                                        p->type->base->aggInst.params = maybeClone( type->aggregate.actuals );
    769794                                } // if
    770                                 p->type->base->qualifiers |= type->qualifiers;
     795                                p->type->base->typeQualifiers |= type->typeQualifiers;
    771796                                break;
    772797
     
    805830                                        lastArray->base->aggInst.params = maybeClone( type->aggregate.actuals );
    806831                                } // if
    807                                 lastArray->base->qualifiers |= type->qualifiers;
     832                                lastArray->base->typeQualifiers |= type->typeQualifiers;
    808833                                break;
    809834                          default:
     
    10051030        } // if
    10061031
    1007 //      if ( variable.name ) {
    10081032        if ( variable.tyClass != NoTypeClass ) {
    10091033                static const TypeDecl::Kind kindMap[] = { TypeDecl::Any, TypeDecl::Dtype, TypeDecl::Ftype, TypeDecl::Ttype };
    10101034                assertf( sizeof(kindMap)/sizeof(kindMap[0] == NoTypeClass-1), "DeclarationNode::build: kindMap is out of sync." );
    10111035                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 ] );
    10131036                TypeDecl * ret = new TypeDecl( *name, DeclarationNode::NoStorageClass, nullptr, kindMap[ variable.tyClass ] );
    10141037                buildList( variable.assertions, ret->get_assertions() );
     
    10171040
    10181041        if ( type ) {
    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 );
     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 );
    10281062}
    10291063
     
    10321066
    10331067        if ( attr.expr ) {
    1034 //              return new AttrType( buildQualifiers( type ), *attr.name, attr.expr->build() );
    10351068                return new AttrType( buildQualifiers( type ), *name, attr.expr->build(), attributes );
    10361069        } else if ( attr.type ) {
    1037 //              return new AttrType( buildQualifiers( type ), *attr.name, attr.type->buildType() );
    10381070                return new AttrType( buildQualifiers( type ), *name, attr.type->buildType(), attributes );
    10391071        } // if
Note: See TracChangeset for help on using the changeset viewer.