Changeset 29917c6 for src/Parser


Ignore:
Timestamp:
Aug 16, 2016, 6:09:36 PM (8 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
08ac489
Parents:
0da3e2c (diff), b7ea418 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

more refactoring of parser code

Location:
src/Parser
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/DeclarationNode.cc

    r0da3e2c r29917c6  
    1010// Created On       : Sat May 16 12:34:05 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Aug 15 20:48:55 2016
    13 // Update Count     : 178
     12// Last Modified On : Tue Aug 16 18:00:34 2016
     13// Update Count     : 179
    1414//
    1515
     
    2525#include "SynTree/Expression.h"
    2626
    27 #include "Parser.h"
    2827#include "TypedefTable.h"
    2928extern TypedefTable typedefTable;
     
    4847        newnode->type = maybeClone( type );
    4948        newnode->name = name;
    50         newnode->storageClasses = storageClasses;
     49        newnode->storageClass = storageClass;
     50        newnode->isInline = isInline;
     51        newnode->isNoreturn = isNoreturn;
    5152        newnode->bitfieldWidth = bitfieldWidth;
    5253        newnode->hasEllipsis = hasEllipsis;
     
    5758} // DeclarationNode::clone
    5859
    59 DeclarationNode::DeclarationNode() : type( 0 ), bitfieldWidth( 0 ), initializer( 0 ), hasEllipsis( false ), linkage( ::linkage ) {
     60DeclarationNode::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() {
    6071}
    6172
     
    8293        } // if
    8394
    84         printEnums( storageClasses.begin(), storageClasses.end(), DeclarationNode::storageName, os );
     95        if(storageClass != NoStorageClass) os << DeclarationNode::storageName[storageClass] << ' ';
     96        if(isInline) os << DeclarationNode::storageName[Inline] << ' ';
     97        if(isNoreturn) os << DeclarationNode::storageName[Noreturn] << ' ';
    8598        if ( type ) {
    8699                type->print( os, indent );
     
    143156DeclarationNode *DeclarationNode::newStorageClass( DeclarationNode::StorageClass sc ) {
    144157        DeclarationNode *newnode = new DeclarationNode;
    145         newnode->storageClasses.push_back( sc );
     158        switch (sc) {
     159                case Inline: newnode->isInline = true; break;
     160                case Noreturn: newnode->isNoreturn = true; break;
     161                default: newnode->storageClass = sc; break;
     162        }
    146163        return newnode;
    147164} // DeclarationNode::newStorageClass
     
    359376DeclarationNode *DeclarationNode::addQualifiers( DeclarationNode *q ) {
    360377        if ( q ) {
    361                 storageClasses.splice( storageClasses.end(), q->storageClasses );
     378                copyStorageClasses(q);
    362379                if ( q->type ) {
    363380                        if ( ! type ) {
     
    386403
    387404DeclarationNode *DeclarationNode::copyStorageClasses( DeclarationNode *q ) {
    388         storageClasses = q->storageClasses;
     405        isInline = isInline || q->isInline;
     406        isNoreturn = isNoreturn || q->isNoreturn;
     407        if(storageClass == NoStorageClass) {
     408                storageClass = q->storageClass;
     409        }
     410        else if (q->storageClass != NoStorageClass) {
     411                q->error = "invalid combination of storage classes in declaration of ";
     412        }
     413        if(error.empty()) error = q->error;
    389414        return this;
    390415}
     
    446471DeclarationNode *DeclarationNode::addType( DeclarationNode *o ) {
    447472        if ( o ) {
    448                 storageClasses.splice( storageClasses.end(), o->storageClasses );
     473                copyStorageClasses( o );
    449474                if ( o->type ) {
    450475                        if ( ! type ) {
     
    693718        } // if
    694719        newnode->type->forall = maybeClone( type->forall );
    695         newnode->storageClasses = storageClasses;
     720        newnode->copyStorageClasses( this );
    696721        newnode->name = assign_strptr( newName );
    697722        return newnode;
     
    700725DeclarationNode *DeclarationNode::cloneBaseType( DeclarationNode *o ) {
    701726        if ( o ) {
    702                 o->storageClasses.insert( o->storageClasses.end(), storageClasses.begin(), storageClasses.end() );
     727                o->copyStorageClasses( this );
    703728                if ( type ) {
    704729                        TypeData *srcType = type;
     
    733758        DeclarationNode *newnode = new DeclarationNode;
    734759        newnode->type = maybeClone( type );
    735         newnode->storageClasses = storageClasses;
     760        newnode->copyStorageClasses( this );
    736761        newnode->name = assign_strptr( newName );
    737762        return newnode;
     
    740765DeclarationNode *DeclarationNode::cloneType( DeclarationNode *o ) {
    741766        if ( o ) {
    742                 o->storageClasses.insert( o->storageClasses.end(), storageClasses.begin(), storageClasses.end() );
     767                o->copyStorageClasses( this );
    743768                if ( type ) {
    744769                        TypeData *newType = type->clone();
     
    855880
    856881Declaration *DeclarationNode::build() const {
     882        if( !error.empty() ) throw SemanticError( error, this );
    857883        if ( type ) {
    858                 return type->buildDecl( name, buildStorageClass(), maybeBuild< Expression >( bitfieldWidth ), buildFuncSpecifier( Inline ), buildFuncSpecifier( Noreturn ), linkage, maybeBuild< Initializer >(initializer) )->set_extension( extension );
    859         } // if
    860         if ( ! buildFuncSpecifier( Inline ) && ! buildFuncSpecifier( Noreturn ) ) {
    861                 return (new ObjectDecl( name, buildStorageClass(), linkage, maybeBuild< Expression >( bitfieldWidth ), 0, maybeBuild< Initializer >( initializer ) ))->set_extension( extension );
     884                return type->buildDecl( name, storageClass, maybeBuild< Expression >( bitfieldWidth ), isInline, isNoreturn, linkage, maybeBuild< Initializer >(initializer) )->set_extension( extension );
     885        } // if
     886        if ( ! isInline && ! isNoreturn ) {
     887                return (new ObjectDecl( name, storageClass, linkage, maybeBuild< Expression >( bitfieldWidth ), 0, maybeBuild< Initializer >( initializer ) ))->set_extension( extension );
    862888        } // if
    863889        throw SemanticError( "invalid function specifier in declaration of ", this );
     
    898924}
    899925
    900 DeclarationNode::StorageClass DeclarationNode::buildStorageClass() const {
    901         DeclarationNode::StorageClass ret = DeclarationNode::NoStorageClass;
    902         for ( std::list< DeclarationNode::StorageClass >::const_iterator i = storageClasses.begin(); i != storageClasses.end(); ++i ) {
    903           if ( *i == DeclarationNode::Inline || *i == DeclarationNode::Noreturn ) continue; // ignore function specifiers
    904           if ( ret != DeclarationNode::NoStorageClass ) {       // already have a valid storage class ?
    905                         throw SemanticError( "invalid combination of storage classes in declaration of ", this );
    906                 } // if
    907                 ret = *i;
    908         } // for
    909         return ret;
    910 }
    911 
    912 bool DeclarationNode::buildFuncSpecifier( DeclarationNode::StorageClass key ) const {
    913         std::list< DeclarationNode::StorageClass >::const_iterator first = std::find( storageClasses.begin(), storageClasses.end(), key );
    914   if ( first == storageClasses.end() ) return false;    // not found
    915         first = std::find( ++first, storageClasses.end(), key ); // found
    916   if ( first == storageClasses.end() ) return true;             // not found again
    917         throw SemanticError( "duplicate function specifier in declaration of ", this );
    918 }
     926// DeclarationNode::StorageClass DeclarationNode::buildStorageClass() const {
     927//      DeclarationNode::StorageClass ret = DeclarationNode::NoStorageClass;
     928//      for ( std::list< DeclarationNode::StorageClass >::const_iterator i = storageClasses.begin(); i != storageClasses.end(); ++i ) {
     929//        if ( *i == DeclarationNode::Inline || *i == DeclarationNode::Noreturn ) continue; // ignore function specifiers
     930//        if ( ret != DeclarationNode::NoStorageClass ) {       // already have a valid storage class ?
     931//                      throw SemanticError( "invalid combination of storage classes in declaration of ", this );
     932//              } // if
     933//              ret = *i;
     934//      } // for
     935//      return ret;
     936// }
     937
     938// bool DeclarationNode::buildFuncSpecifier( DeclarationNode::StorageClass key ) const {
     939//      std::list< DeclarationNode::StorageClass >::const_iterator first = std::find( storageClasses.begin(), storageClasses.end(), key );
     940//   if ( first == storageClasses.end() ) return false; // not found
     941//      first = std::find( ++first, storageClasses.end(), key ); // found
     942//   if ( first == storageClasses.end() ) return true;          // not found again
     943//      throw SemanticError( "duplicate function specifier in declaration of ", this );
     944// }
    919945
    920946// Local Variables: //
  • src/Parser/ParseNode.h

    r0da3e2c r29917c6  
    271271        DeclarationNode *set_extension( bool exten ) { extension = exten; return this; }
    272272  private:
    273         StorageClass buildStorageClass() const;
    274         bool buildFuncSpecifier( StorageClass key ) const;
     273        // StorageClass buildStorageClass() const;
     274        // bool buildFuncSpecifier( StorageClass key ) const;
    275275
    276276        TypeData *type;
    277277        std::string name;
    278         std::list< StorageClass > storageClasses;
     278        // std::list< StorageClass > storageClasses;
     279        StorageClass storageClass;
     280        bool isInline, isNoreturn;
    279281        std::list< std::string > attributes;
    280282        ExpressionNode *bitfieldWidth;
     
    284286        LinkageSpec::Type linkage;
    285287        bool extension = false;
     288        std::string error;
    286289
    287290        static UniqueName anonymous;
Note: See TracChangeset for help on using the changeset viewer.