Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/DeclarationNode.cc

    r413ad05 rd1625f8  
    1010// Created On       : Sat May 16 12:34:05 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sun Aug 28 22:12:44 2016
    13 // Update Count     : 278
     12// Last Modified On : Tue Aug  9 08:39:20 2016
     13// Update Count     : 169
    1414//
    1515
     
    2525#include "SynTree/Expression.h"
    2626
     27#include "Parser.h"
    2728#include "TypedefTable.h"
    2829extern TypedefTable typedefTable;
     
    4142UniqueName DeclarationNode::anonymous( "__anonymous" );
    4243
    43 extern LinkageSpec::Spec linkage;                                               // defined in parser.yy
     44extern LinkageSpec::Type linkage;                                               // defined in parser.yy
    4445
    4546DeclarationNode *DeclarationNode::clone() const {
     
    4748        newnode->type = maybeClone( type );
    4849        newnode->name = name;
    49         newnode->storageClass = storageClass;
    50         newnode->isInline = isInline;
    51         newnode->isNoreturn = isNoreturn;
    52         newnode->bitfieldWidth = maybeClone( bitfieldWidth );
     50        newnode->storageClasses = storageClasses;
     51//PAB   newnode->bitfieldWidth = maybeClone( bitfieldWidth );
     52        newnode->bitfieldWidth = bitfieldWidth;
    5353        newnode->hasEllipsis = hasEllipsis;
    54         newnode->initializer = maybeClone( initializer );
    55         newnode->set_next( maybeClone( get_next() ) );
     54        newnode->initializer = initializer;
     55        newnode->next = maybeClone( next );
    5656        newnode->linkage = linkage;
    5757        return newnode;
    5858} // DeclarationNode::clone
    5959
    60 DeclarationNode::DeclarationNode()
    61         : type( 0 )
    62         , storageClass( NoStorageClass )
    63         , isInline( false )
    64         , isNoreturn( false )
    65         , bitfieldWidth( 0 )
    66         , initializer( 0 )
    67         , hasEllipsis( false )
    68         , linkage( ::linkage )
    69         , extension( false )
    70         , error() {
     60DeclarationNode::DeclarationNode() : type( 0 ), bitfieldWidth( 0 ), initializer( 0 ), hasEllipsis( false ), linkage( ::linkage ) {
    7161}
    7262
     
    9383        } // if
    9484
    95         if ( storageClass != NoStorageClass ) os << DeclarationNode::storageName[storageClass] << ' ';
    96         if ( isInline ) os << DeclarationNode::storageName[Inline] << ' ';
    97         if ( isNoreturn ) os << DeclarationNode::storageName[Noreturn] << ' ';
     85        printEnums( storageClasses.begin(), storageClasses.end(), DeclarationNode::storageName, os );
    9886        if ( type ) {
    9987                type->print( os, indent );
     
    147135} // DeclarationNode::newFunction
    148136
    149 DeclarationNode * DeclarationNode::newQualifier( Qualifier q ) {
     137DeclarationNode *DeclarationNode::newQualifier( Qualifier q ) {
    150138        DeclarationNode *newnode = new DeclarationNode;
    151139        newnode->type = new TypeData();
    152         newnode->type->qualifiers[ q ] = 1;
     140        newnode->type->qualifiers.push_back( q );
    153141        return newnode;
    154142} // DeclarationNode::newQualifier
    155143
    156 DeclarationNode * DeclarationNode::newForall( DeclarationNode *forall ) {
     144DeclarationNode *DeclarationNode::newStorageClass( DeclarationNode::StorageClass sc ) {
     145        DeclarationNode *newnode = new DeclarationNode;
     146        newnode->storageClasses.push_back( sc );
     147        return newnode;
     148} // DeclarationNode::newStorageClass
     149
     150DeclarationNode *DeclarationNode::newBasicType( BasicType bt ) {
     151        DeclarationNode *newnode = new DeclarationNode;
     152        newnode->type = new TypeData( TypeData::Basic );
     153        newnode->type->basic->typeSpec.push_back( bt );
     154        return newnode;
     155} // DeclarationNode::newBasicType
     156
     157DeclarationNode *DeclarationNode::newBuiltinType( BuiltinType bt ) {
     158        DeclarationNode *newnode = new DeclarationNode;
     159        newnode->type = new TypeData( TypeData::Builtin );
     160        newnode->type->builtin->type = bt;
     161        return newnode;
     162} // DeclarationNode::newBuiltinType
     163
     164DeclarationNode *DeclarationNode::newModifier( Modifier mod ) {
     165        DeclarationNode *newnode = new DeclarationNode;
     166        newnode->type = new TypeData( TypeData::Basic );
     167        newnode->type->basic->modifiers.push_back( mod );
     168        return newnode;
     169} // DeclarationNode::newModifier
     170
     171DeclarationNode *DeclarationNode::newForall( DeclarationNode *forall ) {
    157172        DeclarationNode *newnode = new DeclarationNode;
    158173        newnode->type = new TypeData( TypeData::Unknown );
     
    161176} // DeclarationNode::newForall
    162177
    163 DeclarationNode * DeclarationNode::newStorageClass( DeclarationNode::StorageClass sc ) {
    164         DeclarationNode *newnode = new DeclarationNode;
    165         //switch (sc) {
    166         //      case Inline: newnode->isInline = true; break;
    167         //      case Noreturn: newnode->isNoreturn = true; break;
    168         //      default: newnode->storageClass = sc; break;
    169         //}
    170         newnode->storageClass = sc;
    171         return newnode;
    172 } // DeclarationNode::newStorageClass
    173 
    174 DeclarationNode * DeclarationNode::newBasicType( BasicType bt ) {
    175         DeclarationNode *newnode = new DeclarationNode;
    176         newnode->type = new TypeData( TypeData::Basic );
    177         newnode->type->basic->typeSpec.push_back( bt );
    178         return newnode;
    179 } // DeclarationNode::newBasicType
    180 
    181 DeclarationNode * DeclarationNode::newModifier( Modifier mod ) {
    182         DeclarationNode *newnode = new DeclarationNode;
    183         newnode->type = new TypeData( TypeData::Basic );
    184         newnode->type->basic->modifiers.push_back( mod );
    185         return newnode;
    186 } // DeclarationNode::newModifier
    187 
    188 DeclarationNode * DeclarationNode::newBuiltinType( BuiltinType bt ) {
    189         DeclarationNode *newnode = new DeclarationNode;
    190         newnode->type = new TypeData( TypeData::Builtin );
    191         newnode->type->builtin->type = bt;
    192         return newnode;
    193 } // DeclarationNode::newBuiltinType
    194 
    195 DeclarationNode * DeclarationNode::newFromTypedef( std::string *name ) {
     178DeclarationNode *DeclarationNode::newFromTypedef( std::string *name ) {
    196179        DeclarationNode *newnode = new DeclarationNode;
    197180        newnode->type = new TypeData( TypeData::SymbolicInst );
     
    202185} // DeclarationNode::newFromTypedef
    203186
    204 DeclarationNode * DeclarationNode::newAggregate( Aggregate kind, const std::string *name, ExpressionNode *actuals, DeclarationNode *fields, bool body ) {
     187DeclarationNode *DeclarationNode::newAggregate( Aggregate kind, const std::string *name, ExpressionNode *actuals, DeclarationNode *fields, bool body ) {
    205188        DeclarationNode *newnode = new DeclarationNode;
    206189        newnode->type = new TypeData( TypeData::Aggregate );
     
    231214        DeclarationNode *newnode = new DeclarationNode;
    232215        newnode->name = assign_strptr( name );
    233         newnode->enumeratorValue.reset( constant );
     216        newnode->enumeratorValue = constant;
    234217        typedefTable.addToEnclosingScope( newnode->name, TypedefTable::ID );
    235218        return newnode;
     
    301284        newnode->type->array->dimension = size;
    302285        newnode->type->array->isStatic = isStatic;
    303         if ( newnode->type->array->dimension == 0 || newnode->type->array->dimension->isExpressionType<ConstantExpr *>() ) {
     286        if ( newnode->type->array->dimension == 0 || dynamic_cast<ConstantExpr *>( newnode->type->array->dimension->build() ) ) {
    304287                newnode->type->array->isVarLen = false;
    305288        } else {
     
    370353                        src = 0;
    371354                } else {
    372                         dst->qualifiers |= src->qualifiers;
    373                 } // if
    374         } // if
    375 }
    376 
    377 void DeclarationNode::checkQualifiers( const TypeData *src, const TypeData *dst ) {
    378         TypeData::Qualifiers qsrc = src->qualifiers, qdst = dst->qualifiers;
    379 
    380         if ( (qsrc & qdst).any() ) {                                            // common bits between qualifier masks ?
    381                 error = "duplicate qualifier ";
    382                 int j = 0;                                                                              // separator detector
    383                 for ( int i = 0; i < DeclarationNode::NoOfQualifier; i += 1 ) {
    384                         if ( qsrc[i] & qdst[i] ) {                                      // find specific qualifiers in common
    385                                 if ( j > 0 ) error += ", ";
    386                                 error += DeclarationNode::qualifierName[i];
    387                                 j += 1;
    388                         } // if
    389                 } // for
    390                 error += " in declaration of ";
    391         } // if
    392 } // DeclarationNode::checkQualifiers
     355                        dst->qualifiers.splice( dst->qualifiers.end(), src->qualifiers );
     356                } // if
     357        } // if
     358}
    393359
    394360DeclarationNode *DeclarationNode::addQualifiers( DeclarationNode *q ) {
    395361        if ( q ) {
    396                 copyStorageClasses(q);
     362                storageClasses.splice( storageClasses.end(), q->storageClasses );
    397363                if ( q->type ) {
    398364                        if ( ! type ) {
    399365                                type = new TypeData;
    400                         } else {
    401                                 checkQualifiers( q->type, type );
    402366                        } // if
    403367                        addQualifiersToType( q->type, type );
     
    423387
    424388DeclarationNode *DeclarationNode::copyStorageClasses( DeclarationNode *q ) {
    425         isInline = isInline || q->isInline;
    426         isNoreturn = isNoreturn || q->isNoreturn;
    427         if ( storageClass == NoStorageClass ) {
    428                 storageClass = q->storageClass;
    429         } else if ( q->storageClass != NoStorageClass ) {
    430                 q->error = "invalid combination of storage classes in declaration of ";
    431         } // if
    432         if ( error.empty() ) error = q->error;
     389        storageClasses = q->storageClasses;
    433390        return this;
    434391}
     
    449406                        switch ( dst->kind ) {
    450407                          case TypeData::Unknown:
    451                                 src->qualifiers |= dst->qualifiers;
     408                                src->qualifiers.splice( src->qualifiers.end(), dst->qualifiers );
    452409                                dst = src;
    453410                                src = 0;
    454411                                break;
    455412                          case TypeData::Basic:
    456                                 dst->qualifiers |= src->qualifiers;
     413                                dst->qualifiers.splice( dst->qualifiers.end(), src->qualifiers );
    457414                                if ( src->kind != TypeData::Unknown ) {
    458415                                        assert( src->kind == TypeData::Basic );
     
    470427                                                dst->base->aggInst->params = maybeClone( src->aggregate->actuals );
    471428                                        } // if
    472                                         dst->base->qualifiers |= src->qualifiers;
     429                                        dst->base->qualifiers.splice( dst->base->qualifiers.end(), src->qualifiers );
    473430                                        src = 0;
    474431                                        break;
     
    490447DeclarationNode *DeclarationNode::addType( DeclarationNode *o ) {
    491448        if ( o ) {
    492                 copyStorageClasses( o );
     449                storageClasses.splice( storageClasses.end(), o->storageClasses );
    493450                if ( o->type ) {
    494451                        if ( ! type ) {
     
    499456                                                type->aggInst->params = maybeClone( o->type->aggregate->actuals );
    500457                                        } // if
    501                                         type->qualifiers |= o->type->qualifiers;
     458                                        type->qualifiers.splice( type->qualifiers.end(), o->type->qualifiers );
    502459                                } else {
    503460                                        type = o->type;
     
    513470
    514471                // there may be typedefs chained onto the type
    515                 if ( o->get_next() ) {
    516                         set_last( o->get_next()->clone() );
     472                if ( o->get_link() ) {
     473                        set_link( o->get_link()->clone() );
    517474                } // if
    518475        } // if
     
    634591                                        p->type->base->aggInst->params = maybeClone( type->aggregate->actuals );
    635592                                } // if
    636                                 p->type->base->qualifiers |= type->qualifiers;
     593                                p->type->base->qualifiers.splice( p->type->base->qualifiers.end(), type->qualifiers );
    637594                                break;
    638595
     
    671628                                        lastArray->base->aggInst->params = maybeClone( type->aggregate->actuals );
    672629                                } // if
    673                                 lastArray->base->qualifiers |= type->qualifiers;
     630                                lastArray->base->qualifiers.splice( lastArray->base->qualifiers.end(), type->qualifiers );
    674631                                break;
    675632                          default:
     
    737694        } // if
    738695        newnode->type->forall = maybeClone( type->forall );
    739         newnode->copyStorageClasses( this );
     696        newnode->storageClasses = storageClasses;
    740697        newnode->name = assign_strptr( newName );
    741698        return newnode;
     
    744701DeclarationNode *DeclarationNode::cloneBaseType( DeclarationNode *o ) {
    745702        if ( o ) {
    746                 o->copyStorageClasses( this );
     703                o->storageClasses.insert( o->storageClasses.end(), storageClasses.begin(), storageClasses.end() );
    747704                if ( type ) {
    748705                        TypeData *srcType = type;
     
    777734        DeclarationNode *newnode = new DeclarationNode;
    778735        newnode->type = maybeClone( type );
    779         newnode->copyStorageClasses( this );
     736        newnode->storageClasses = storageClasses;
    780737        newnode->name = assign_strptr( newName );
    781738        return newnode;
     
    784741DeclarationNode *DeclarationNode::cloneType( DeclarationNode *o ) {
    785742        if ( o ) {
    786                 o->copyStorageClasses( this );
     743                o->storageClasses.insert( o->storageClasses.end(), storageClasses.begin(), storageClasses.end() );
    787744                if ( type ) {
    788745                        TypeData *newType = type->clone();
     
    795752                } // if
    796753        } // if
    797         delete o;
    798754        return o;
     755}
     756
     757DeclarationNode *DeclarationNode::appendList( DeclarationNode *node ) {
     758        if ( node != 0 ) {
     759                set_link( node );
     760        } // if
     761        return this;
    799762}
    800763
    801764DeclarationNode *DeclarationNode::extractAggregate() const {
    802765        if ( type ) {
    803                 TypeData *ret = typeextractAggregate( type );
     766                TypeData *ret = type->extractAggregate();
    804767                if ( ret ) {
    805768                        DeclarationNode *newnode = new DeclarationNode;
     
    813776void buildList( const DeclarationNode *firstNode, std::list< Declaration * > &outputList ) {
    814777        SemanticError errors;
    815         std::back_insert_iterator< std::list< Declaration * > > out( outputList );
     778        std::back_insert_iterator< std::list< Declaration *> > out( outputList );
    816779        const DeclarationNode *cur = firstNode;
    817780        while ( cur ) {
     
    823786                                        *out++ = decl;
    824787                                } // if
    825                                 delete extr;
    826788                        } // if
    827789                        Declaration *decl = cur->build();
     
    832794                        errors.append( e );
    833795                } // try
    834                 cur = dynamic_cast< DeclarationNode * >( cur->get_next() );
     796                cur = dynamic_cast<DeclarationNode *>( cur->get_link() );
    835797        } // while
    836798        if ( ! errors.isEmpty() ) {
     
    839801}
    840802
    841 void buildList( const DeclarationNode *firstNode, std::list< DeclarationWithType * > &outputList ) {
     803void buildList( const DeclarationNode *firstNode, std::list< DeclarationWithType *> &outputList ) {
    842804        SemanticError errors;
    843         std::back_insert_iterator< std::list< DeclarationWithType * > > out( outputList );
     805        std::back_insert_iterator< std::list< DeclarationWithType *> > out( outputList );
    844806        const DeclarationNode *cur = firstNode;
    845807        while ( cur ) {
     
    855817                        Declaration *decl = cur->build();
    856818                        if ( decl ) {
    857                                 if ( DeclarationWithType *dwt = dynamic_cast< DeclarationWithType * >( decl ) ) {
     819                                if ( DeclarationWithType *dwt = dynamic_cast< DeclarationWithType *>( decl ) ) {
    858820                                        *out++ = dwt;
    859                                 } else if ( StructDecl *agg = dynamic_cast< StructDecl * >( decl ) ) {
     821                                } else if ( StructDecl *agg = dynamic_cast< StructDecl *>( decl ) ) {
    860822                                        StructInstType *inst = new StructInstType( Type::Qualifiers(), agg->get_name() );
    861823                                        *out++ = new ObjectDecl( "", DeclarationNode::NoStorageClass, linkage, 0, inst, 0 );
    862824                                        delete agg;
    863                                 } else if ( UnionDecl *agg = dynamic_cast< UnionDecl * >( decl ) ) {
     825                                } else if ( UnionDecl *agg = dynamic_cast< UnionDecl *>( decl ) ) {
    864826                                        UnionInstType *inst = new UnionInstType( Type::Qualifiers(), agg->get_name() );
    865827                                        *out++ = new ObjectDecl( "", DeclarationNode::NoStorageClass, linkage, 0, inst, 0 );
     
    869831                        errors.append( e );
    870832                } // try
    871                 cur = dynamic_cast< DeclarationNode * >( cur->get_next() );
     833                cur = dynamic_cast< DeclarationNode *>( cur->get_link() );
    872834        } // while
    873835        if ( ! errors.isEmpty() ) {
     
    876838}
    877839
    878 void buildTypeList( const DeclarationNode *firstNode, std::list< Type * > &outputList ) {
     840void buildTypeList( const DeclarationNode *firstNode, std::list< Type *> &outputList ) {
    879841        SemanticError errors;
    880         std::back_insert_iterator< std::list< Type * > > out( outputList );
     842        std::back_insert_iterator< std::list< Type *> > out( outputList );
    881843        const DeclarationNode *cur = firstNode;
    882844        while ( cur ) {
     
    886848                        errors.append( e );
    887849                } // try
    888                 cur = dynamic_cast< DeclarationNode * >( cur->get_next() );
     850                cur = dynamic_cast< DeclarationNode *>( cur->get_link() );
    889851        } // while
    890852        if ( ! errors.isEmpty() ) {
     
    894856
    895857Declaration *DeclarationNode::build() const {
    896         if ( ! error.empty() ) throw SemanticError( error, this );
    897858        if ( type ) {
    898                 return buildDecl( type, name, storageClass, maybeBuild< Expression >( bitfieldWidth ), isInline, isNoreturn, linkage, maybeBuild< Initializer >(initializer) )->set_extension( extension );
    899         } // if
    900         if ( ! isInline && ! isNoreturn ) {
    901                 return (new ObjectDecl( name, storageClass, linkage, maybeBuild< Expression >( bitfieldWidth ), 0, maybeBuild< Initializer >( initializer ) ))->set_extension( extension );
     859                return type->buildDecl( name, buildStorageClass(), maybeBuild< Expression >( bitfieldWidth ), buildFuncSpecifier( Inline ), buildFuncSpecifier( Noreturn ), linkage, maybeBuild< Initializer >(initializer) )->set_extension( extension );
     860        } // if
     861        if ( ! buildFuncSpecifier( Inline ) && ! buildFuncSpecifier( Noreturn ) ) {
     862                return (new ObjectDecl( name, buildStorageClass(), linkage, maybeBuild< Expression >( bitfieldWidth ), 0, maybeBuild< Initializer >( initializer ) ))->set_extension( extension );
    902863        } // if
    903864        throw SemanticError( "invalid function specifier in declaration of ", this );
     
    909870        switch ( type->kind ) {
    910871          case TypeData::Enum:
    911                 return new EnumInstType( buildQualifiers( type ), type->enumeration->name );
     872                return new EnumInstType( type->buildQualifiers(), type->enumeration->name );
    912873          case TypeData::Aggregate: {
    913874                  ReferenceToType *ret;
    914875                  switch ( type->aggregate->kind ) {
    915876                        case DeclarationNode::Struct:
    916                           ret = new StructInstType( buildQualifiers( type ), type->aggregate->name );
     877                          ret = new StructInstType( type->buildQualifiers(), type->aggregate->name );
    917878                          break;
    918879                        case DeclarationNode::Union:
    919                           ret = new UnionInstType( buildQualifiers( type ), type->aggregate->name );
     880                          ret = new UnionInstType( type->buildQualifiers(), type->aggregate->name );
    920881                          break;
    921882                        case DeclarationNode::Trait:
    922                           ret = new TraitInstType( buildQualifiers( type ), type->aggregate->name );
     883                          ret = new TraitInstType( type->buildQualifiers(), type->aggregate->name );
    923884                          break;
    924885                        default:
     
    929890          }
    930891          case TypeData::Symbolic: {
    931                   TypeInstType *ret = new TypeInstType( buildQualifiers( type ), type->symbolic->name, false );
     892                  TypeInstType *ret = new TypeInstType( type->buildQualifiers(), type->symbolic->name, false );
    932893                  buildList( type->symbolic->actuals, ret->get_parameters() );
    933894                  return ret;
    934895          }
    935896          default:
    936                 return typebuild( type );
     897                return type->build();
    937898        } // switch
    938899}
    939900
    940 // DeclarationNode::StorageClass DeclarationNode::buildStorageClass() const {
    941 //      DeclarationNode::StorageClass ret = DeclarationNode::NoStorageClass;
    942 //      for ( std::list< DeclarationNode::StorageClass >::const_iterator i = storageClasses.begin(); i != storageClasses.end(); ++i ) {
    943 //        if ( *i == DeclarationNode::Inline || *i == DeclarationNode::Noreturn ) continue; // ignore function specifiers
    944 //        if ( ret != DeclarationNode::NoStorageClass ) {       // already have a valid storage class ?
    945 //                      throw SemanticError( "invalid combination of storage classes in declaration of ", this );
    946 //              } // if
    947 //              ret = *i;
    948 //      } // for
    949 //      return ret;
    950 // }
    951 
    952 // bool DeclarationNode::buildFuncSpecifier( DeclarationNode::StorageClass key ) const {
    953 //      std::list< DeclarationNode::StorageClass >::const_iterator first = std::find( storageClasses.begin(), storageClasses.end(), key );
    954 //   if ( first == storageClasses.end() ) return false; // not found
    955 //      first = std::find( ++first, storageClasses.end(), key ); // found
    956 //   if ( first == storageClasses.end() ) return true;          // not found again
    957 //      throw SemanticError( "duplicate function specifier in declaration of ", this );
    958 // }
     901DeclarationNode::StorageClass DeclarationNode::buildStorageClass() const {
     902        DeclarationNode::StorageClass ret = DeclarationNode::NoStorageClass;
     903        for ( std::list< DeclarationNode::StorageClass >::const_iterator i = storageClasses.begin(); i != storageClasses.end(); ++i ) {
     904          if ( *i == DeclarationNode::Inline || *i == DeclarationNode::Noreturn ) continue; // ignore function specifiers
     905          if ( ret != DeclarationNode::NoStorageClass ) {       // already have a valid storage class ?
     906                        throw SemanticError( "invalid combination of storage classes in declaration of ", this );
     907                } // if
     908                ret = *i;
     909        } // for
     910        return ret;
     911}
     912
     913bool DeclarationNode::buildFuncSpecifier( DeclarationNode::StorageClass key ) const {
     914        std::list< DeclarationNode::StorageClass >::const_iterator first = std::find( storageClasses.begin(), storageClasses.end(), key );
     915  if ( first == storageClasses.end() ) return false;    // not found
     916        first = std::find( ++first, storageClasses.end(), key ); // found
     917  if ( first == storageClasses.end() ) return true;             // not found again
     918        throw SemanticError( "duplicate function specifier in declaration of ", this );
     919}
    959920
    960921// Local Variables: //
Note: See TracChangeset for help on using the changeset viewer.