Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/DeclarationNode.cc

    r59db689 r68cd1ce  
    1010// Created On       : Sat May 16 12:34:05 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sat Jun  6 23:29:13 2015
    13 // Update Count     : 23
     12// Last Modified On : Sat Jun 13 08:02:03 2015
     13// Update Count     : 58
    1414//
    1515
     
    2121
    2222#include "TypeData.h"
     23
     24#include "SynTree/Declaration.h"
    2325#include "SynTree/Expression.h"
    2426
    25 
    2627using namespace std;
    2728
    2829// These must remain in the same order as the corresponding DeclarationNode enumerations.
     30const char *DeclarationNode::storageName[] = { "extern", "static", "auto", "register", "inline", "fortran", "_Noreturn", "_Thread_local", "" };
    2931const char *DeclarationNode::qualifierName[] = { "const", "restrict", "volatile", "lvalue", "_Atomic" };
    3032const char *DeclarationNode::basicTypeName[] = { "char", "int", "float", "double", "void", "_Bool", "_Complex", "_Imaginary" };
    31 const char *DeclarationNode::modifierName[] = { "signed", "unsigned", "short", "long" };
    32 const char *DeclarationNode::tyConName[] = { "struct", "union", "context" };
     33const char *DeclarationNode::modifierName[]  = { "signed", "unsigned", "short", "long" };
     34const char *DeclarationNode::aggregateName[] = { "struct", "union", "context" };
    3335const char *DeclarationNode::typeClassName[] = { "type", "dtype", "ftype" };
    3436
     
    6365}
    6466
    65 const char *storageClassName[] = {
    66         // order must correspond with DeclarationNode::StorageClass
    67         "extern",
    68         "static",
    69         "auto",
    70         "register",
    71         "inline",
    72         "fortran",
    73 };
    74 
    7567void DeclarationNode::print( std::ostream &os, int indent ) const {
    7668        os << string( indent, ' ' );
     
    7971        } else {
    8072                os << name << ": ";
    81         }
     73        } // if
    8274
    8375        if ( linkage != LinkageSpec::Cforall ) {
    8476                os << LinkageSpec::toString( linkage ) << " ";
    85         }
    86 
    87         printEnums( storageClasses.begin(), storageClasses.end(), storageClassName, os );
     77        } // if
     78
     79        printEnums( storageClasses.begin(), storageClasses.end(), DeclarationNode::storageName, os );
    8880        if ( type ) {
    8981                type->print( os, indent );
    9082        } else {
    9183                os << "untyped entity ";
    92         }
     84        } // if
    9385
    9486        if ( bitfieldWidth ) {
    9587                os << endl << string( indent + 2, ' ' ) << "with bitfield width ";
    9688                bitfieldWidth->printOneLine( os );
    97         }
     89        } // if
    9890
    9991        if ( initializer != 0 ) {
    10092                os << endl << string( indent + 2, ' ' ) << "with initializer ";
    10193                initializer->printOneLine( os );
    102         }
     94        } // if
    10395
    10496        os << endl;
     
    109101        if ( hasEllipsis ) {
    110102                os << string( indent, ' ' )  << "and a variable number of other arguments" << endl;
    111         }
     103        } // if
    112104}
    113105
     
    123115        if ( body ) {
    124116                newnode->type->function->hasBody = true;
    125         }
     117        } // if
    126118
    127119        if ( ret ) {
     
    129121                ret->type = 0;
    130122                delete ret;
    131         }
     123        } // if
    132124
    133125        return newnode;
     
    141133}
    142134
    143 DeclarationNode *DeclarationNode::newStorageClass( StorageClass sc ) {
     135DeclarationNode *DeclarationNode::newStorageClass( DeclarationNode::StorageClass sc ) {
    144136        DeclarationNode *newnode = new DeclarationNode;
    145137        newnode->storageClasses.push_back( sc );
     
    177169}
    178170
    179 DeclarationNode *DeclarationNode::newAggregate( TyCon kind, std::string *name, DeclarationNode *formals, ExpressionNode *actuals, DeclarationNode *fields ) {
     171DeclarationNode *DeclarationNode::newAggregate( Aggregate kind, std::string *name, DeclarationNode *formals, ExpressionNode *actuals, DeclarationNode *fields ) {
    180172        DeclarationNode *newnode = new DeclarationNode;
    181173        newnode->type = new TypeData( TypeData::Aggregate );
     
    184176        if ( newnode->type->aggregate->name == "" ) {
    185177                newnode->type->aggregate->name = DeclarationNode::anonymous.newName();
    186         }
     178        } // if
    187179        newnode->type->aggregate->params = formals;
    188180        newnode->type->aggregate->actuals = actuals;
     
    198190        if ( newnode->type->enumeration->name == "" ) {
    199191                newnode->type->enumeration->name = DeclarationNode::anonymous.newName();
    200         }
     192        } // if
    201193        newnode->type->enumeration->constants = constants;
    202194        return newnode;
     
    331323                        } else {
    332324                                dst->forall = src->forall;
    333                         }
     325                        } // if
    334326                        src->forall = 0;
    335                 }
     327                } // if
    336328                if ( dst->base ) {
    337329                        addQualifiersToType( src, dst->base );
     
    341333                } else {
    342334                        dst->qualifiers.splice( dst->qualifiers.end(), src->qualifiers );
    343                 }
    344         }
     335                } // if
     336        } // if
    345337}
    346338         
     
    351343                        if ( ! type ) {
    352344                                type = new TypeData;
    353                         }
     345                        } // if
    354346                        addQualifiersToType( q->type, type );
    355347                        if ( q->type && q->type->forall ) {
     
    357349                                        type->forall->appendList( q->type->forall );
    358350                                } else {
    359                                         type->forall = q->type->forall;
    360                                 }
     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
    361357                                q->type->forall = 0;
    362                         }
    363                 }
    364         }
     358                        } // if
     359                } // if
     360        } // if
    365361        delete q;
    366362        return this;
     
    379375                        } else {
    380376                                dst->forall = src->forall;
    381                         }
     377                        } // if
    382378                        src->forall = 0;
    383                 }
     379                } // if
    384380                if ( dst->base ) {
    385381                        addTypeToType( src, dst->base );
     
    398394                                        dst->basic->modifiers.splice( dst->basic->modifiers.end(), src->basic->modifiers );
    399395                                        dst->basic->typeSpec.splice( dst->basic->typeSpec.end(), src->basic->typeSpec );
    400                                 }
     396                                } // if
    401397                                break;
    402398
     
    409405                                        if ( src->kind == TypeData::Aggregate ) {
    410406                                                dst->base->aggInst->params = maybeClone( src->aggregate->actuals );
    411                                         }
     407                                        } // if
    412408                                        dst->base->qualifiers.splice( dst->base->qualifiers.end(), src->qualifiers );
    413409                                        src = 0;
     
    419415                                        } else {
    420416                                                dst->forall = src->forall;
    421                                         }
     417                                        } // if
    422418                                        src->forall = 0;
    423419                                        dst->base = src;
    424420                                        src = 0;
    425                                 }
    426                         }
    427                 }
    428         }
     421                                } // switch
     422                        } // switch
     423                } // if
     424        } // if
    429425}
    430426
     
    439435                                        if ( o->type->kind == TypeData::Aggregate ) {
    440436                                                type->aggInst->params = maybeClone( o->type->aggregate->actuals );
    441                                         }
     437                                        } // if
    442438                                        type->qualifiers.splice( type->qualifiers.end(), o->type->qualifiers );
    443439                                } else {
    444440                                        type = o->type;
    445                                 }
     441                                } // if
    446442                                o->type = 0;
    447443                        } else {
    448444                                addTypeToType( o->type, type );
    449                         }
    450                 }
     445                        } // if
     446                } // if
    451447                if ( o->bitfieldWidth ) {
    452448                        bitfieldWidth = o->bitfieldWidth;
    453                 }
    454         }
     449                } // if
     450        } // if
    455451        delete o;
    456452        return this;
     
    475471                } else {
    476472                        type->symbolic->assertions = assertions;
    477                 }
     473                } // if
    478474                break;
    479        
    480475          case TypeData::Variable:
    481476                if ( type->variable->assertions ) {
     
    483478                } else {
    484479                        type->variable->assertions = assertions;
    485                 }
     480                } // if
    486481                break;
    487        
    488482          default:
    489483                assert( false );
    490         }
     484        } // switch
    491485       
    492486        return this;
     
    526520}
    527521
    528 static void
    529 setBase( TypeData *&type, TypeData *newType ) {
     522static void setBase( TypeData *&type, TypeData *newType ) {
    530523        if ( type ) {
    531524                TypeData *prevBase = type;
     
    534527                        prevBase = curBase;
    535528                        curBase = curBase->base;
    536                 }
     529                } // while
    537530                prevBase->base = newType;
    538531        } else {
    539532                type = newType;
    540         }
     533        } // if
    541534}
    542535
     
    547540                p->type = 0;
    548541                delete p;
    549         }
     542        } // if
    550543        return this;
    551544}
     
    557550                a->type = 0;
    558551                delete a;
    559         }
     552        } // if
    560553        return this;
    561554}
     
    572565                                if ( type->kind == TypeData::Aggregate ) {
    573566                                        p->type->base->aggInst->params = maybeClone( type->aggregate->actuals );
    574                                 }
     567                                } // if
    575568                                p->type->base->qualifiers.splice( p->type->base->qualifiers.end(), type->qualifiers );
    576569                                break;
     
    578571                          default:
    579572                                p->type->base = type;
    580                         }
     573                        } // switch
    581574                        type = 0;
    582                 }
     575                } // if
    583576                delete this;
    584577                return p;
    585578        } else {
    586579                return this;
    587         }
     580        } // if
    588581}
    589582
     
    593586        while ( cur->base ) {
    594587                cur = cur->base;
    595         }
     588        } // while
    596589        return cur;
    597590}
     
    609602                                if ( type->kind == TypeData::Aggregate ) {
    610603                                        lastArray->base->aggInst->params = maybeClone( type->aggregate->actuals );
    611                                 }
     604                                } // if
    612605                                lastArray->base->qualifiers.splice( lastArray->base->qualifiers.end(), type->qualifiers );
    613606                                break;
    614607                          default:
    615608                                lastArray->base = type;
    616                         }
     609                        } // switch
    617610                        type = 0;
    618                 }
     611                } // if
    619612                delete this;
    620613                return a;
    621614        } else {
    622615                return this;
    623         }
     616        } // if
    624617}
    625618
     
    637630                } else {
    638631                        type->function->idList = ids;
    639                 }
     632                } // if
    640633                return type;
    641634        } else {
     
    643636                newtype->function->idList = ids;
    644637                return newtype;
    645         }
     638        } // if
    646639}
    647640       
     
    662655        while ( srcType->base ) {
    663656                srcType = srcType->base;
    664         }
     657        } // while
    665658        newnode->type = maybeClone( srcType );
    666659        if ( newnode->type->kind == TypeData::AggregateInst ) {
     
    673666                        delete newnode->type->aggInst->aggregate->aggregate->members;
    674667                        newnode->type->aggInst->aggregate->aggregate->members = 0;
    675                 }
    676         }
     668                } // if
     669        } // if
    677670        newnode->type->forall = maybeClone( type->forall );
    678671        newnode->storageClasses = storageClasses;
     
    688681                        while ( srcType->base ) {
    689682                                srcType = srcType->base;
    690                         }
     683                        } // while
    691684                        TypeData *newType = srcType->clone();
    692685                        if ( newType->kind == TypeData::AggregateInst ) {
     
    699692                                        delete newType->aggInst->aggregate->aggregate->members;
    700693                                        newType->aggInst->aggregate->aggregate->members = 0;
    701                                 }
    702                         }
     694                                } // if
     695                        } // if
    703696                        newType->forall = maybeClone( type->forall );
    704697                        if ( ! o->type ) {
     
    707700                                addTypeToType( newType, o->type );
    708701                                delete newType;
    709                         }
    710                 }
    711         }
     702                        } // if
     703                } // if
     704        } // if
    712705        return o;
    713706}
     
    731724                                addTypeToType( newType, o->type );
    732725                                delete newType;
    733                         }
    734                 }
    735         }
     726                        } // if
     727                } // if
     728        } // if
    736729        return o;
    737730}
     
    740733        if ( node != 0 ) {
    741734                set_link( node );
    742         }
     735        } // if
    743736        return this;
    744737}
     
    803796                                } else if ( StructDecl *agg = dynamic_cast< StructDecl *>( decl ) ) {
    804797                                        StructInstType *inst = new StructInstType( Type::Qualifiers(), agg->get_name() );
    805                                         *out++ = new ObjectDecl( "", Declaration::NoStorageClass, linkage, 0, inst, 0 );
     798                                        *out++ = new ObjectDecl( "", DeclarationNode::NoStorageClass, linkage, 0, inst, 0 );
    806799                                        delete agg;
    807800                                } else if ( UnionDecl *agg = dynamic_cast< UnionDecl *>( decl ) ) {
    808801                                        UnionInstType *inst = new UnionInstType( Type::Qualifiers(), agg->get_name() );
    809                                         *out++ = new ObjectDecl( "", Declaration::NoStorageClass, linkage, 0, inst, 0 );
     802                                        *out++ = new ObjectDecl( "", DeclarationNode::NoStorageClass, linkage, 0, inst, 0 );
    810803                                } // if
    811804                        } // if
     
    882875}
    883876
    884 Declaration::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 ) {
     877DeclarationNode::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 ?
    899883                        throw SemanticError( "invalid combination of storage classes in declaration of ", this );
    900                 }
    901                 ret = scMap[ *i ];
    902         }
     884                } // if
     885                ret = *i;
     886        } // for
    903887        return ret;
    904888}
    905889
    906890bool DeclarationNode::buildInline() const {
    907         std::list< StorageClass >::const_iterator first = std::find( storageClasses.begin(), storageClasses.end(), Inline );
     891        std::list< DeclarationNode::StorageClass >::const_iterator first = std::find( storageClasses.begin(), storageClasses.end(), DeclarationNode::Inline );
    908892  if ( first == storageClasses.end() ) return false;
    909         std::list< StorageClass >::const_iterator next = std::find( ++first, storageClasses.end(), Inline );
     893        std::list< DeclarationNode::StorageClass >::const_iterator next = std::find( ++first, storageClasses.end(), DeclarationNode::Inline );
    910894  if ( next == storageClasses.end() ) return true;
    911895        throw SemanticError( "duplicate inline specification in declaration of ", this );
Note: See TracChangeset for help on using the changeset viewer.