Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/DeclarationNode.cc

    rde62360d r843054c2  
    1010// Created On       : Sat May 16 12:34:05 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Wed Jun 24 15:29:19 2015
    13 // Update Count     : 86
     12// Last Modified On : Thu May 21 09:28:54 2015
     13// Update Count     : 13
    1414//
    1515
     
    2121
    2222#include "TypeData.h"
    23 
    24 #include "SynTree/Declaration.h"
    2523#include "SynTree/Expression.h"
    2624
    27 #include "Parser.h"
    28 #include "TypedefTable.h"
    29 extern TypedefTable typedefTable;
    3025
    3126using namespace std;
    3227
    3328// 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", "" };
    3529const char *DeclarationNode::qualifierName[] = { "const", "restrict", "volatile", "lvalue", "_Atomic" };
    3630const char *DeclarationNode::basicTypeName[] = { "char", "int", "float", "double", "void", "_Bool", "_Complex", "_Imaginary" };
    37 const char *DeclarationNode::modifierName[]  = { "signed", "unsigned", "short", "long" };
    38 const char *DeclarationNode::aggregateName[] = { "struct", "union", "context" };
     31const char *DeclarationNode::modifierName[] = { "signed", "unsigned", "short", "long" };
     32const char *DeclarationNode::tyConName[] = { "struct", "union", "context" };
    3933const char *DeclarationNode::typeClassName[] = { "type", "dtype", "ftype" };
    4034
     
    6963}
    7064
     65const char *storageClassName[] = {
     66        // order must correspond with DeclarationNode::StorageClass
     67        "extern",
     68        "static",
     69        "auto",
     70        "register",
     71        "inline",
     72        "fortran",
     73};
     74
    7175void DeclarationNode::print( std::ostream &os, int indent ) const {
    72         os << string( indent, ' ' );
     76        os << string(indent, ' ' );
    7377        if ( name == "" ) {
    7478                os << "unnamed: ";
    7579        } else {
    7680                os << name << ": ";
    77         } // if
     81        }
    7882
    7983        if ( linkage != LinkageSpec::Cforall ) {
    8084                os << LinkageSpec::toString( linkage ) << " ";
    81         } // if
    82 
    83         printEnums( storageClasses.begin(), storageClasses.end(), DeclarationNode::storageName, os );
     85        }
     86
     87        printEnums( storageClasses.begin(), storageClasses.end(), storageClassName, os );
    8488        if ( type ) {
    8589                type->print( os, indent );
    8690        } else {
    8791                os << "untyped entity ";
    88         } // if
     92        }
    8993
    9094        if ( bitfieldWidth ) {
    91                 os << endl << string( indent + 2, ' ' ) << "with bitfield width ";
     95                os << endl << string(indent+2,  ' ') << "with bitfield width ";
    9296                bitfieldWidth->printOneLine( os );
    93         } // if
     97        }
    9498
    9599        if ( initializer != 0 ) {
    96                 os << endl << string( indent + 2, ' ' ) << "with initializer ";
     100                os << endl << string(indent+2,  ' ') << "with initializer ";
    97101                initializer->printOneLine( os );
    98         } // if
     102        }
    99103
    100104        os << endl;
     
    105109        if ( hasEllipsis ) {
    106110                os << string( indent, ' ' )  << "and a variable number of other arguments" << endl;
    107         } // if
     111        }
    108112}
    109113
     
    119123        if ( body ) {
    120124                newnode->type->function->hasBody = true;
    121         } // if
     125        }
    122126
    123127        if ( ret ) {
     
    125129                ret->type = 0;
    126130                delete ret;
    127         } // if
     131        }
    128132
    129133        return newnode;
     
    137141}
    138142
    139 DeclarationNode *DeclarationNode::newStorageClass( DeclarationNode::StorageClass sc ) {
     143DeclarationNode *DeclarationNode::newStorageClass( StorageClass sc ) {
    140144        DeclarationNode *newnode = new DeclarationNode;
    141145        newnode->storageClasses.push_back( sc );
     
    157161}
    158162
    159 DeclarationNode *DeclarationNode::newForall( DeclarationNode *forall ) {
     163DeclarationNode *DeclarationNode::newForall( DeclarationNode* forall ) {
    160164        DeclarationNode *newnode = new DeclarationNode;
    161165        newnode->type = new TypeData( TypeData::Unknown );
     
    164168}
    165169
    166 DeclarationNode *DeclarationNode::newFromTypedef( std::string *name ) {
     170DeclarationNode *DeclarationNode::newFromTypedef( std::string* name ) {
    167171        DeclarationNode *newnode = new DeclarationNode;
    168172        newnode->type = new TypeData( TypeData::SymbolicInst );
     
    173177}
    174178
    175 DeclarationNode *DeclarationNode::newAggregate( Aggregate kind, std::string *name, DeclarationNode *formals, ExpressionNode *actuals, DeclarationNode *fields ) {
     179DeclarationNode *DeclarationNode::newAggregate( TyCon kind, std::string* name, DeclarationNode *formals, ExpressionNode *actuals, DeclarationNode *fields ) {
    176180        DeclarationNode *newnode = new DeclarationNode;
    177181        newnode->type = new TypeData( TypeData::Aggregate );
     
    180184        if ( newnode->type->aggregate->name == "" ) {
    181185                newnode->type->aggregate->name = DeclarationNode::anonymous.newName();
    182         } // if
    183 
    184         // SKULLDUGGERY: generate a typedef for the aggregate name so that the aggregate does not have to be qualified by
    185         // "struct"
    186         typedefTable.addToEnclosingScope( newnode->type->aggregate->name, TypedefTable::TD );
    187         DeclarationNode *typedf = new DeclarationNode;
    188         typedf->name = newnode->type->aggregate->name;
    189         newnode->appendList( typedf->addType( newnode->clone() )->addTypedef() );
    190 
     186        }
    191187        newnode->type->aggregate->params = formals;
    192188        newnode->type->aggregate->actuals = actuals;
     
    202198        if ( newnode->type->enumeration->name == "" ) {
    203199                newnode->type->enumeration->name = DeclarationNode::anonymous.newName();
    204         } // if
    205 
    206         // SKULLDUGGERY: generate a typedef for the enumeration name so that the enumeration does not have to be qualified
    207         // by "enum"
    208         typedefTable.addToEnclosingScope( newnode->type->enumeration->name, TypedefTable::TD );
    209         DeclarationNode *typedf = new DeclarationNode;
    210         typedf->name = newnode->type->enumeration->name;
    211         newnode->appendList( typedf->addType( newnode->clone() )->addTypedef() );
    212 
     200        }
    213201        newnode->type->enumeration->constants = constants;
    214202        return newnode;
    215203}
    216204
    217 DeclarationNode *DeclarationNode::newEnumConstant( std::string *name, ExpressionNode *constant ) {
     205DeclarationNode *DeclarationNode::newEnumConstant( std::string* name, ExpressionNode *constant ) {
    218206        DeclarationNode *newnode = new DeclarationNode;
    219207        newnode->name = assign_strptr( name );
     
    222210}
    223211
    224 DeclarationNode *DeclarationNode::newName( std::string *name ) {
     212DeclarationNode *DeclarationNode::newName( std::string* name ) {
    225213        DeclarationNode *newnode = new DeclarationNode;
    226214        newnode->name = assign_strptr( name );
     
    228216}
    229217
    230 DeclarationNode *DeclarationNode::newFromTypeGen( std::string *name, ExpressionNode *params ) {
     218DeclarationNode *DeclarationNode::newFromTypeGen( std::string* name, ExpressionNode *params ) {
    231219        DeclarationNode *newnode = new DeclarationNode;
    232220        newnode->type = new TypeData( TypeData::SymbolicInst );
     
    237225}
    238226
    239 DeclarationNode *DeclarationNode::newTypeParam( TypeClass tc, std::string *name ) {
     227DeclarationNode *DeclarationNode::newTypeParam( TypeClass tc, std::string* name ) {
    240228        DeclarationNode *newnode = new DeclarationNode;
    241229        newnode->name = assign_strptr( name );
     
    343331                        } else {
    344332                                dst->forall = src->forall;
    345                         } // if
     333                        }
    346334                        src->forall = 0;
    347                 } // if
     335                }
    348336                if ( dst->base ) {
    349337                        addQualifiersToType( src, dst->base );
     
    353341                } else {
    354342                        dst->qualifiers.splice( dst->qualifiers.end(), src->qualifiers );
    355                 } // if
    356         } // if
     343                }
     344        }
    357345}
    358346         
     
    363351                        if ( ! type ) {
    364352                                type = new TypeData;
    365                         } // if
     353                        }
    366354                        addQualifiersToType( q->type, type );
    367355                        if ( q->type && q->type->forall ) {
     
    369357                                        type->forall->appendList( q->type->forall );
    370358                                } else {
    371                                         if ( type->kind == TypeData::Aggregate ) {
    372                                                 type->aggregate->params = q->type->forall;
    373                                         } else {
    374                                                 type->forall = q->type->forall;
    375                                         } // if
    376                                 } // if
     359                                        type->forall = q->type->forall;
     360                                }
    377361                                q->type->forall = 0;
    378                         } // if
    379                 } // if
    380         } // if
     362                        }
     363                }
     364        }
    381365        delete q;
    382366        return this;
     
    395379                        } else {
    396380                                dst->forall = src->forall;
    397                         } // if
     381                        }
    398382                        src->forall = 0;
    399                 } // if
     383                }
    400384                if ( dst->base ) {
    401385                        addTypeToType( src, dst->base );
     
    414398                                        dst->basic->modifiers.splice( dst->basic->modifiers.end(), src->basic->modifiers );
    415399                                        dst->basic->typeSpec.splice( dst->basic->typeSpec.end(), src->basic->typeSpec );
    416                                 } // if
     400                                }
    417401                                break;
    418402
     
    425409                                        if ( src->kind == TypeData::Aggregate ) {
    426410                                                dst->base->aggInst->params = maybeClone( src->aggregate->actuals );
    427                                         } // if
     411                                        }
    428412                                        dst->base->qualifiers.splice( dst->base->qualifiers.end(), src->qualifiers );
    429413                                        src = 0;
     
    435419                                        } else {
    436420                                                dst->forall = src->forall;
    437                                         } // if
     421                                        }
    438422                                        src->forall = 0;
    439423                                        dst->base = src;
    440424                                        src = 0;
    441                                 } // switch
    442                         } // switch
    443                 } // if
    444         } // if
     425                                }
     426                        }
     427                }
     428        }
    445429}
    446430
     
    455439                                        if ( o->type->kind == TypeData::Aggregate ) {
    456440                                                type->aggInst->params = maybeClone( o->type->aggregate->actuals );
    457                                         } // if
     441                                        }
    458442                                        type->qualifiers.splice( type->qualifiers.end(), o->type->qualifiers );
    459443                                } else {
    460444                                        type = o->type;
    461                                 } // if
     445                                }
    462446                                o->type = 0;
    463447                        } else {
    464448                                addTypeToType( o->type, type );
    465                         } // if
    466                 } // if
     449                        }
     450                }
    467451                if ( o->bitfieldWidth ) {
    468452                        bitfieldWidth = o->bitfieldWidth;
    469                 } // if
    470         } // if
     453                }
     454        }
    471455        delete o;
    472456        return this;
     
    483467}
    484468
    485 DeclarationNode *DeclarationNode::addAssertions( DeclarationNode *assertions ) {
     469DeclarationNode *DeclarationNode::addAssertions( DeclarationNode* assertions ) {
    486470        assert( type );
    487471        switch ( type->kind ) {
     
    491475                } else {
    492476                        type->symbolic->assertions = assertions;
    493                 } // if
     477                }
    494478                break;
     479       
    495480          case TypeData::Variable:
    496481                if ( type->variable->assertions ) {
     
    498483                } else {
    499484                        type->variable->assertions = assertions;
    500                 } // if
     485                }
    501486                break;
     487       
    502488          default:
    503489                assert( false );
    504         } // switch
     490        }
    505491       
    506492        return this;
    507493}
    508494
    509 DeclarationNode *DeclarationNode::addName( std::string *newname ) {
     495DeclarationNode *DeclarationNode::addName( std::string* newname ) {
    510496        name = assign_strptr( newname );
    511497        return this;
     
    540526}
    541527
    542 static void setBase( TypeData *&type, TypeData *newType ) {
     528static void
     529setBase( TypeData *&type, TypeData *newType ) {
    543530        if ( type ) {
    544531                TypeData *prevBase = type;
     
    547534                        prevBase = curBase;
    548535                        curBase = curBase->base;
    549                 } // while
     536                }
    550537                prevBase->base = newType;
    551538        } else {
    552539                type = newType;
    553         } // if
     540        }
    554541}
    555542
     
    560547                p->type = 0;
    561548                delete p;
    562         } // if
     549        }
    563550        return this;
    564551}
     
    570557                a->type = 0;
    571558                delete a;
    572         } // if
     559        }
    573560        return this;
    574561}
     
    585572                                if ( type->kind == TypeData::Aggregate ) {
    586573                                        p->type->base->aggInst->params = maybeClone( type->aggregate->actuals );
    587                                 } // if
     574                                }
    588575                                p->type->base->qualifiers.splice( p->type->base->qualifiers.end(), type->qualifiers );
    589576                                break;
     
    591578                          default:
    592579                                p->type->base = type;
    593                         } // switch
     580                        }
    594581                        type = 0;
    595                 } // if
     582                }
    596583                delete this;
    597584                return p;
    598585        } else {
    599586                return this;
    600         } // if
     587        }
    601588}
    602589
     
    606593        while ( cur->base ) {
    607594                cur = cur->base;
    608         } // while
     595        }
    609596        return cur;
    610597}
     
    622609                                if ( type->kind == TypeData::Aggregate ) {
    623610                                        lastArray->base->aggInst->params = maybeClone( type->aggregate->actuals );
    624                                 } // if
     611                                }
    625612                                lastArray->base->qualifiers.splice( lastArray->base->qualifiers.end(), type->qualifiers );
    626613                                break;
    627614                          default:
    628615                                lastArray->base = type;
    629                         } // switch
     616                        }
    630617                        type = 0;
    631                 } // if
     618                }
    632619                delete this;
    633620                return a;
    634621        } else {
    635622                return this;
    636         } // if
     623        }
    637624}
    638625
     
    650637                } else {
    651638                        type->function->idList = ids;
    652                 } // if
     639                }
    653640                return type;
    654641        } else {
     
    656643                newtype->function->idList = ids;
    657644                return newtype;
    658         } // if
     645        }
    659646}
    660647       
     
    675662        while ( srcType->base ) {
    676663                srcType = srcType->base;
    677         } // while
     664        }
    678665        newnode->type = maybeClone( srcType );
    679666        if ( newnode->type->kind == TypeData::AggregateInst ) {
     
    686673                        delete newnode->type->aggInst->aggregate->aggregate->members;
    687674                        newnode->type->aggInst->aggregate->aggregate->members = 0;
    688                 } // if
    689         } // if
     675                }
     676        }
    690677        newnode->type->forall = maybeClone( type->forall );
    691678        newnode->storageClasses = storageClasses;
     
    701688                        while ( srcType->base ) {
    702689                                srcType = srcType->base;
    703                         } // while
     690                        }
    704691                        TypeData *newType = srcType->clone();
    705692                        if ( newType->kind == TypeData::AggregateInst ) {
     
    712699                                        delete newType->aggInst->aggregate->aggregate->members;
    713700                                        newType->aggInst->aggregate->aggregate->members = 0;
    714                                 } // if
    715                         } // if
     701                                }
     702                        }
    716703                        newType->forall = maybeClone( type->forall );
    717704                        if ( ! o->type ) {
     
    720707                                addTypeToType( newType, o->type );
    721708                                delete newType;
    722                         } // if
    723                 } // if
    724         } // if
     709                        }
     710                }
     711        }
    725712        return o;
    726713}
     
    744731                                addTypeToType( newType, o->type );
    745732                                delete newType;
    746                         } // if
    747                 } // if
    748         } // if
     733                        }
     734                }
     735        }
    749736        return o;
    750737}
     
    753740        if ( node != 0 ) {
    754741                set_link( node );
    755         } // if
     742        }
    756743        return this;
    757744}
     
    769756}
    770757
    771 void buildList( const DeclarationNode *firstNode, std::list< Declaration * > &outputList ) {
     758void buildList( const DeclarationNode *firstNode, std::list< Declaration* > &outputList ) {
    772759        SemanticError errors;
    773         std::back_insert_iterator< std::list< Declaration *> > out( outputList );
     760        std::back_insert_iterator< std::list< Declaration* > > out( outputList );
    774761        const DeclarationNode *cur = firstNode;
    775762        while ( cur ) {
     
    789776                        errors.append( e );
    790777                } // try
    791                 cur = dynamic_cast< DeclarationNode *>( cur->get_link() );
     778                cur = dynamic_cast< DeclarationNode* >( cur->get_link() );
    792779        } // while
    793780        if ( ! errors.isEmpty() ) {
     
    796783}
    797784
    798 void buildList( const DeclarationNode *firstNode, std::list< DeclarationWithType *> &outputList ) {
     785void buildList( const DeclarationNode *firstNode, std::list< DeclarationWithType* > &outputList ) {
    799786        SemanticError errors;
    800         std::back_insert_iterator< std::list< DeclarationWithType *> > out( outputList );
     787        std::back_insert_iterator< std::list< DeclarationWithType* > > out( outputList );
    801788        const DeclarationNode *cur = firstNode;
    802789        while ( cur ) {
     
    812799                        Declaration *decl = cur->build();
    813800                        if ( decl ) {
    814                                 if ( DeclarationWithType *dwt = dynamic_cast< DeclarationWithType *>( decl ) ) {
     801                                if ( DeclarationWithType *dwt = dynamic_cast< DeclarationWithType* >( decl ) ) {
    815802                                        *out++ = dwt;
    816                                 } else if ( StructDecl *agg = dynamic_cast< StructDecl *>( decl ) ) {
     803                                } else if ( StructDecl *agg = dynamic_cast< StructDecl* >( decl ) ) {
    817804                                        StructInstType *inst = new StructInstType( Type::Qualifiers(), agg->get_name() );
    818                                         *out++ = new ObjectDecl( "", DeclarationNode::NoStorageClass, linkage, 0, inst, 0 );
     805                                        *out++ = new ObjectDecl( "", Declaration::NoStorageClass, linkage, 0, inst, 0 );
    819806                                        delete agg;
    820                                 } else if ( UnionDecl *agg = dynamic_cast< UnionDecl *>( decl ) ) {
     807                                } else if ( UnionDecl *agg = dynamic_cast< UnionDecl* >( decl ) ) {
    821808                                        UnionInstType *inst = new UnionInstType( Type::Qualifiers(), agg->get_name() );
    822                                         *out++ = new ObjectDecl( "", DeclarationNode::NoStorageClass, linkage, 0, inst, 0 );
     809                                        *out++ = new ObjectDecl( "", Declaration::NoStorageClass, linkage, 0, inst, 0 );
    823810                                } // if
    824811                        } // if
     
    826813                        errors.append( e );
    827814                } // try
    828                 cur = dynamic_cast< DeclarationNode *>( cur->get_link() );
     815                cur = dynamic_cast< DeclarationNode* >( cur->get_link() );
    829816        } // while
    830817        if ( ! errors.isEmpty() ) {
     
    833820}
    834821
    835 void buildTypeList( const DeclarationNode *firstNode, std::list< Type *> &outputList ) {
     822void buildTypeList( const DeclarationNode *firstNode, std::list< Type* > &outputList ) {
    836823        SemanticError errors;
    837         std::back_insert_iterator< std::list< Type *> > out( outputList );
     824        std::back_insert_iterator< std::list< Type* > > out( outputList );
    838825        const DeclarationNode *cur = firstNode;
    839826        while ( cur ) {
     
    843830                        errors.append( e );
    844831                } // try
    845                 cur = dynamic_cast< DeclarationNode *>( cur->get_link() );
     832                cur = dynamic_cast< DeclarationNode* >( cur->get_link() );
    846833        } // while
    847834        if ( ! errors.isEmpty() ) {
     
    852839Declaration *DeclarationNode::build() const {
    853840        if ( type ) {
    854                 Declaration *newDecl = type->buildDecl( name, buildStorageClass(), maybeBuild< Expression >( bitfieldWidth ), buildFuncSpecifier( Inline ), buildFuncSpecifier( Noreturn ), linkage, maybeBuild< Initializer >(initializer) );
     841                Declaration *newDecl = type->buildDecl( name, buildStorageClass(), maybeBuild< Expression >( bitfieldWidth ), buildInline(), linkage, maybeBuild< Initializer >(initializer) );
    855842                return newDecl;
    856843        } // if
    857         if ( ! buildFuncSpecifier( Inline ) && ! buildFuncSpecifier( Noreturn ) ) {
     844        if ( ! buildInline() ) {
    858845                return new ObjectDecl( name, buildStorageClass(), linkage, maybeBuild< Expression >( bitfieldWidth ), 0, maybeBuild< Initializer >( initializer ) );
    859846        } // if
    860         throw SemanticError( "invalid function specifier in declaration of ", this );
     847        throw SemanticError( "invalid inline specification in declaration of ", this );
    861848}
    862849
     
    895882}
    896883
    897 DeclarationNode::StorageClass DeclarationNode::buildStorageClass() const {
    898         DeclarationNode::StorageClass ret = DeclarationNode::NoStorageClass;
    899         for ( std::list< DeclarationNode::StorageClass >::const_iterator i = storageClasses.begin(); i != storageClasses.end(); ++i ) {
    900           if ( *i == DeclarationNode::Inline || *i == DeclarationNode::Noreturn ) continue; // ignore function specifiers
    901           if ( ret != DeclarationNode::NoStorageClass ) {       // already have a valid storage class ?
     884Declaration::StorageClass DeclarationNode::buildStorageClass() const {
     885        static const Declaration::StorageClass scMap[] = { 
     886                Declaration::Extern,
     887                Declaration::Static,
     888                Declaration::Auto,
     889                Declaration::Register,
     890                Declaration::Inline,
     891                Declaration::Fortran
     892        }; 
     893 
     894        Declaration::StorageClass ret = Declaration::NoStorageClass;
     895        for ( std::list< StorageClass >::const_iterator i = storageClasses.begin(); i != storageClasses.end(); ++i ) {
     896                assert( unsigned( *i ) < sizeof( scMap ) / sizeof( scMap[0] ) );
     897          if ( *i == Inline ) continue;
     898          if ( ret != Declaration::NoStorageClass ) {
    902899                        throw SemanticError( "invalid combination of storage classes in declaration of ", this );
    903                 } // if
    904                 ret = *i;
    905         } // for
     900                }
     901                ret = scMap[ *i ];
     902        }
    906903        return ret;
    907904}
    908905
    909 bool DeclarationNode::buildFuncSpecifier( DeclarationNode::StorageClass key ) const {
    910         std::list< DeclarationNode::StorageClass >::const_iterator first = std::find( storageClasses.begin(), storageClasses.end(), key );
    911   if ( first == storageClasses.end() ) return false;    // not found
    912         first = std::find( ++first, storageClasses.end(), key ); // found
    913   if ( first == storageClasses.end() ) return true;             // not found again
    914         throw SemanticError( "duplicate function specifier in declaration of ", this );
     906bool DeclarationNode::buildInline() const {
     907        std::list< StorageClass >::const_iterator first = std::find( storageClasses.begin(), storageClasses.end(), Inline );
     908  if ( first == storageClasses.end() ) return false;
     909        std::list< StorageClass >::const_iterator next = std::find( ++first, storageClasses.end(), Inline );
     910  if ( next == storageClasses.end() ) return true;
     911        throw SemanticError( "duplicate inline specification in declaration of ", this );
    915912}
    916913
Note: See TracChangeset for help on using the changeset viewer.