Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/DeclarationNode.cc

    r68cd1ce r59db689  
    1010// Created On       : Sat May 16 12:34:05 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sat Jun 13 08:02:03 2015
    13 // Update Count     : 58
     12// Last Modified On : Sat Jun  6 23:29:13 2015
     13// Update Count     : 23
    1414//
    1515
     
    2121
    2222#include "TypeData.h"
    23 
    24 #include "SynTree/Declaration.h"
    2523#include "SynTree/Expression.h"
    2624
     25
    2726using namespace std;
    2827
    2928// These must remain in the same order as the corresponding DeclarationNode enumerations.
    30 const char *DeclarationNode::storageName[] = { "extern", "static", "auto", "register", "inline", "fortran", "_Noreturn", "_Thread_local", "" };
    3129const char *DeclarationNode::qualifierName[] = { "const", "restrict", "volatile", "lvalue", "_Atomic" };
    3230const char *DeclarationNode::basicTypeName[] = { "char", "int", "float", "double", "void", "_Bool", "_Complex", "_Imaginary" };
    33 const char *DeclarationNode::modifierName[]  = { "signed", "unsigned", "short", "long" };
    34 const char *DeclarationNode::aggregateName[] = { "struct", "union", "context" };
     31const char *DeclarationNode::modifierName[] = { "signed", "unsigned", "short", "long" };
     32const char *DeclarationNode::tyConName[] = { "struct", "union", "context" };
    3533const char *DeclarationNode::typeClassName[] = { "type", "dtype", "ftype" };
    3634
     
    6563}
    6664
     65const char *storageClassName[] = {
     66        // order must correspond with DeclarationNode::StorageClass
     67        "extern",
     68        "static",
     69        "auto",
     70        "register",
     71        "inline",
     72        "fortran",
     73};
     74
    6775void DeclarationNode::print( std::ostream &os, int indent ) const {
    6876        os << string( indent, ' ' );
     
    7179        } else {
    7280                os << name << ": ";
    73         } // if
     81        }
    7482
    7583        if ( linkage != LinkageSpec::Cforall ) {
    7684                os << LinkageSpec::toString( linkage ) << " ";
    77         } // if
    78 
    79         printEnums( storageClasses.begin(), storageClasses.end(), DeclarationNode::storageName, os );
     85        }
     86
     87        printEnums( storageClasses.begin(), storageClasses.end(), storageClassName, os );
    8088        if ( type ) {
    8189                type->print( os, indent );
    8290        } else {
    8391                os << "untyped entity ";
    84         } // if
     92        }
    8593
    8694        if ( bitfieldWidth ) {
    8795                os << endl << string( indent + 2, ' ' ) << "with bitfield width ";
    8896                bitfieldWidth->printOneLine( os );
    89         } // if
     97        }
    9098
    9199        if ( initializer != 0 ) {
    92100                os << endl << string( indent + 2, ' ' ) << "with initializer ";
    93101                initializer->printOneLine( os );
    94         } // if
     102        }
    95103
    96104        os << endl;
     
    101109        if ( hasEllipsis ) {
    102110                os << string( indent, ' ' )  << "and a variable number of other arguments" << endl;
    103         } // if
     111        }
    104112}
    105113
     
    115123        if ( body ) {
    116124                newnode->type->function->hasBody = true;
    117         } // if
     125        }
    118126
    119127        if ( ret ) {
     
    121129                ret->type = 0;
    122130                delete ret;
    123         } // if
     131        }
    124132
    125133        return newnode;
     
    133141}
    134142
    135 DeclarationNode *DeclarationNode::newStorageClass( DeclarationNode::StorageClass sc ) {
     143DeclarationNode *DeclarationNode::newStorageClass( StorageClass sc ) {
    136144        DeclarationNode *newnode = new DeclarationNode;
    137145        newnode->storageClasses.push_back( sc );
     
    169177}
    170178
    171 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 ) {
    172180        DeclarationNode *newnode = new DeclarationNode;
    173181        newnode->type = new TypeData( TypeData::Aggregate );
     
    176184        if ( newnode->type->aggregate->name == "" ) {
    177185                newnode->type->aggregate->name = DeclarationNode::anonymous.newName();
    178         } // if
     186        }
    179187        newnode->type->aggregate->params = formals;
    180188        newnode->type->aggregate->actuals = actuals;
     
    190198        if ( newnode->type->enumeration->name == "" ) {
    191199                newnode->type->enumeration->name = DeclarationNode::anonymous.newName();
    192         } // if
     200        }
    193201        newnode->type->enumeration->constants = constants;
    194202        return newnode;
     
    323331                        } else {
    324332                                dst->forall = src->forall;
    325                         } // if
     333                        }
    326334                        src->forall = 0;
    327                 } // if
     335                }
    328336                if ( dst->base ) {
    329337                        addQualifiersToType( src, dst->base );
     
    333341                } else {
    334342                        dst->qualifiers.splice( dst->qualifiers.end(), src->qualifiers );
    335                 } // if
    336         } // if
     343                }
     344        }
    337345}
    338346         
     
    343351                        if ( ! type ) {
    344352                                type = new TypeData;
    345                         } // if
     353                        }
    346354                        addQualifiersToType( q->type, type );
    347355                        if ( q->type && q->type->forall ) {
     
    349357                                        type->forall->appendList( q->type->forall );
    350358                                } else {
    351                                         if ( type->kind == TypeData::Aggregate ) {
    352                                                 type->aggregate->params = q->type->forall;
    353                                         } else {
    354                                                 type->forall = q->type->forall;
    355                                         } // if
    356                                 } // if
     359                                        type->forall = q->type->forall;
     360                                }
    357361                                q->type->forall = 0;
    358                         } // if
    359                 } // if
    360         } // if
     362                        }
     363                }
     364        }
    361365        delete q;
    362366        return this;
     
    375379                        } else {
    376380                                dst->forall = src->forall;
    377                         } // if
     381                        }
    378382                        src->forall = 0;
    379                 } // if
     383                }
    380384                if ( dst->base ) {
    381385                        addTypeToType( src, dst->base );
     
    394398                                        dst->basic->modifiers.splice( dst->basic->modifiers.end(), src->basic->modifiers );
    395399                                        dst->basic->typeSpec.splice( dst->basic->typeSpec.end(), src->basic->typeSpec );
    396                                 } // if
     400                                }
    397401                                break;
    398402
     
    405409                                        if ( src->kind == TypeData::Aggregate ) {
    406410                                                dst->base->aggInst->params = maybeClone( src->aggregate->actuals );
    407                                         } // if
     411                                        }
    408412                                        dst->base->qualifiers.splice( dst->base->qualifiers.end(), src->qualifiers );
    409413                                        src = 0;
     
    415419                                        } else {
    416420                                                dst->forall = src->forall;
    417                                         } // if
     421                                        }
    418422                                        src->forall = 0;
    419423                                        dst->base = src;
    420424                                        src = 0;
    421                                 } // switch
    422                         } // switch
    423                 } // if
    424         } // if
     425                                }
     426                        }
     427                }
     428        }
    425429}
    426430
     
    435439                                        if ( o->type->kind == TypeData::Aggregate ) {
    436440                                                type->aggInst->params = maybeClone( o->type->aggregate->actuals );
    437                                         } // if
     441                                        }
    438442                                        type->qualifiers.splice( type->qualifiers.end(), o->type->qualifiers );
    439443                                } else {
    440444                                        type = o->type;
    441                                 } // if
     445                                }
    442446                                o->type = 0;
    443447                        } else {
    444448                                addTypeToType( o->type, type );
    445                         } // if
    446                 } // if
     449                        }
     450                }
    447451                if ( o->bitfieldWidth ) {
    448452                        bitfieldWidth = o->bitfieldWidth;
    449                 } // if
    450         } // if
     453                }
     454        }
    451455        delete o;
    452456        return this;
     
    471475                } else {
    472476                        type->symbolic->assertions = assertions;
    473                 } // if
     477                }
    474478                break;
     479       
    475480          case TypeData::Variable:
    476481                if ( type->variable->assertions ) {
     
    478483                } else {
    479484                        type->variable->assertions = assertions;
    480                 } // if
     485                }
    481486                break;
     487       
    482488          default:
    483489                assert( false );
    484         } // switch
     490        }
    485491       
    486492        return this;
     
    520526}
    521527
    522 static void setBase( TypeData *&type, TypeData *newType ) {
     528static void
     529setBase( TypeData *&type, TypeData *newType ) {
    523530        if ( type ) {
    524531                TypeData *prevBase = type;
     
    527534                        prevBase = curBase;
    528535                        curBase = curBase->base;
    529                 } // while
     536                }
    530537                prevBase->base = newType;
    531538        } else {
    532539                type = newType;
    533         } // if
     540        }
    534541}
    535542
     
    540547                p->type = 0;
    541548                delete p;
    542         } // if
     549        }
    543550        return this;
    544551}
     
    550557                a->type = 0;
    551558                delete a;
    552         } // if
     559        }
    553560        return this;
    554561}
     
    565572                                if ( type->kind == TypeData::Aggregate ) {
    566573                                        p->type->base->aggInst->params = maybeClone( type->aggregate->actuals );
    567                                 } // if
     574                                }
    568575                                p->type->base->qualifiers.splice( p->type->base->qualifiers.end(), type->qualifiers );
    569576                                break;
     
    571578                          default:
    572579                                p->type->base = type;
    573                         } // switch
     580                        }
    574581                        type = 0;
    575                 } // if
     582                }
    576583                delete this;
    577584                return p;
    578585        } else {
    579586                return this;
    580         } // if
     587        }
    581588}
    582589
     
    586593        while ( cur->base ) {
    587594                cur = cur->base;
    588         } // while
     595        }
    589596        return cur;
    590597}
     
    602609                                if ( type->kind == TypeData::Aggregate ) {
    603610                                        lastArray->base->aggInst->params = maybeClone( type->aggregate->actuals );
    604                                 } // if
     611                                }
    605612                                lastArray->base->qualifiers.splice( lastArray->base->qualifiers.end(), type->qualifiers );
    606613                                break;
    607614                          default:
    608615                                lastArray->base = type;
    609                         } // switch
     616                        }
    610617                        type = 0;
    611                 } // if
     618                }
    612619                delete this;
    613620                return a;
    614621        } else {
    615622                return this;
    616         } // if
     623        }
    617624}
    618625
     
    630637                } else {
    631638                        type->function->idList = ids;
    632                 } // if
     639                }
    633640                return type;
    634641        } else {
     
    636643                newtype->function->idList = ids;
    637644                return newtype;
    638         } // if
     645        }
    639646}
    640647       
     
    655662        while ( srcType->base ) {
    656663                srcType = srcType->base;
    657         } // while
     664        }
    658665        newnode->type = maybeClone( srcType );
    659666        if ( newnode->type->kind == TypeData::AggregateInst ) {
     
    666673                        delete newnode->type->aggInst->aggregate->aggregate->members;
    667674                        newnode->type->aggInst->aggregate->aggregate->members = 0;
    668                 } // if
    669         } // if
     675                }
     676        }
    670677        newnode->type->forall = maybeClone( type->forall );
    671678        newnode->storageClasses = storageClasses;
     
    681688                        while ( srcType->base ) {
    682689                                srcType = srcType->base;
    683                         } // while
     690                        }
    684691                        TypeData *newType = srcType->clone();
    685692                        if ( newType->kind == TypeData::AggregateInst ) {
     
    692699                                        delete newType->aggInst->aggregate->aggregate->members;
    693700                                        newType->aggInst->aggregate->aggregate->members = 0;
    694                                 } // if
    695                         } // if
     701                                }
     702                        }
    696703                        newType->forall = maybeClone( type->forall );
    697704                        if ( ! o->type ) {
     
    700707                                addTypeToType( newType, o->type );
    701708                                delete newType;
    702                         } // if
    703                 } // if
    704         } // if
     709                        }
     710                }
     711        }
    705712        return o;
    706713}
     
    724731                                addTypeToType( newType, o->type );
    725732                                delete newType;
    726                         } // if
    727                 } // if
    728         } // if
     733                        }
     734                }
     735        }
    729736        return o;
    730737}
     
    733740        if ( node != 0 ) {
    734741                set_link( node );
    735         } // if
     742        }
    736743        return this;
    737744}
     
    796803                                } else if ( StructDecl *agg = dynamic_cast< StructDecl *>( decl ) ) {
    797804                                        StructInstType *inst = new StructInstType( Type::Qualifiers(), agg->get_name() );
    798                                         *out++ = new ObjectDecl( "", DeclarationNode::NoStorageClass, linkage, 0, inst, 0 );
     805                                        *out++ = new ObjectDecl( "", Declaration::NoStorageClass, linkage, 0, inst, 0 );
    799806                                        delete agg;
    800807                                } else if ( UnionDecl *agg = dynamic_cast< UnionDecl *>( decl ) ) {
    801808                                        UnionInstType *inst = new UnionInstType( Type::Qualifiers(), agg->get_name() );
    802                                         *out++ = new ObjectDecl( "", DeclarationNode::NoStorageClass, linkage, 0, inst, 0 );
     809                                        *out++ = new ObjectDecl( "", Declaration::NoStorageClass, linkage, 0, inst, 0 );
    803810                                } // if
    804811                        } // if
     
    875882}
    876883
    877 DeclarationNode::StorageClass DeclarationNode::buildStorageClass() const {
    878         DeclarationNode::StorageClass ret = DeclarationNode::NoStorageClass;
    879         for ( std::list< DeclarationNode::StorageClass >::const_iterator i = storageClasses.begin(); i != storageClasses.end(); ++i ) {
    880           if ( *i == DeclarationNode::Inline || *i == DeclarationNode::Noreturn ) continue; // ignore function specifiers
    881 
    882           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 ) {
    883899                        throw SemanticError( "invalid combination of storage classes in declaration of ", this );
    884                 } // if
    885                 ret = *i;
    886         } // for
     900                }
     901                ret = scMap[ *i ];
     902        }
    887903        return ret;
    888904}
    889905
    890906bool DeclarationNode::buildInline() const {
    891         std::list< DeclarationNode::StorageClass >::const_iterator first = std::find( storageClasses.begin(), storageClasses.end(), DeclarationNode::Inline );
     907        std::list< StorageClass >::const_iterator first = std::find( storageClasses.begin(), storageClasses.end(), Inline );
    892908  if ( first == storageClasses.end() ) return false;
    893         std::list< DeclarationNode::StorageClass >::const_iterator next = std::find( ++first, storageClasses.end(), DeclarationNode::Inline );
     909        std::list< StorageClass >::const_iterator next = std::find( ++first, storageClasses.end(), Inline );
    894910  if ( next == storageClasses.end() ) return true;
    895911        throw SemanticError( "duplicate inline specification in declaration of ", this );
Note: See TracChangeset for help on using the changeset viewer.