Ignore:
Timestamp:
Aug 28, 2016, 11:05:45 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, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
6943a987
Parents:
3f1e68f
Message:

more refactoring of parser code

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/TypeData.cc

    r3f1e68f r413ad05  
    1010// Created On       : Sat May 16 15:12:51 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Wed Aug 24 13:46:55 2016
    13 // Update Count     : 69
     12// Last Modified On : Sun Aug 28 18:28:58 2016
     13// Update Count     : 223
    1414//
    1515
     
    9494                break;
    9595        } // switch
    96 }
     96} // TypeData::TypeData
    9797
    9898TypeData::~TypeData() {
     
    163163                break;
    164164        } // switch
    165 }
    166 
    167 TypeData *TypeData::clone() const {
    168         TypeData *newtype = new TypeData( kind );
     165} // TypeData::~TypeData
     166
     167TypeData * TypeData::clone() const {
     168        TypeData * newtype = new TypeData( kind );
    169169        newtype->qualifiers = qualifiers;
    170170        newtype->base = maybeClone( base );
     
    238238        } // switch
    239239        return newtype;
    240 }
     240} // TypeData::clone
    241241
    242242void TypeData::print( std::ostream &os, int indent ) const {
     
    418418                assert( false );
    419419        } // switch
    420 }
    421 
    422 TypeData *TypeData::extractAggregate( bool toplevel ) const {
    423         TypeData *ret = 0;
    424 
    425         switch ( kind ) {
    426           case Aggregate:
    427                 if ( ! toplevel && aggregate->fields ) {
    428                         ret = clone();
    429 //                      ret->qualifiers.reset();
    430                 } // if
    431                 break;
    432           case Enum:
    433                 if ( ! toplevel && enumeration->constants ) {
    434                         ret = clone();
    435 //                      ret->qualifiers.reset();
    436                 } // if
    437                 break;
    438           case AggregateInst:
    439                 if ( aggInst->aggregate ) {
    440                         ret = aggInst->aggregate->extractAggregate( false );
    441                 } // if
    442                 break;
    443           default:
    444                 if ( base ) {
    445                         ret = base->extractAggregate( false );
    446                 } // if
    447         } // switch
    448         return ret;
    449 }
    450 
    451 void buildForall( const DeclarationNode *firstNode, std::list< TypeDecl* > &outputList ) {
     420} // TypeData::print
     421
     422void buildForall( const DeclarationNode * firstNode, std::list< TypeDecl* > &outputList ) {
    452423        buildList( firstNode, outputList );
    453424        for ( std::list< TypeDecl* >::iterator i = outputList.begin(); i != outputList.end(); ++i ) {
     
    455426                        // add assertion parameters to `type' tyvars in reverse order
    456427                        // add dtor:  void ^?{}(T *)
    457                         FunctionType *dtorType = new FunctionType( Type::Qualifiers(), false );
     428                        FunctionType * dtorType = new FunctionType( Type::Qualifiers(), false );
    458429                        dtorType->get_parameters().push_back( new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), (*i)->get_name(), *i ) ), 0 ) );
    459430                        (*i)->get_assertions().push_front( new FunctionDecl( "^?{}", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, dtorType, 0, false, false ) );
    460431
    461432                        // add copy ctor:  void ?{}(T *, T)
    462                         FunctionType *copyCtorType = new FunctionType( Type::Qualifiers(), false );
     433                        FunctionType * copyCtorType = new FunctionType( Type::Qualifiers(), false );
    463434                        copyCtorType->get_parameters().push_back( new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), (*i)->get_name(), *i ) ), 0 ) );
    464435                        copyCtorType->get_parameters().push_back( new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new TypeInstType( Type::Qualifiers(), (*i)->get_name(), *i ), 0 ) );
     
    466437
    467438                        // add default ctor:  void ?{}(T *)
    468                         FunctionType *ctorType = new FunctionType( Type::Qualifiers(), false );
     439                        FunctionType * ctorType = new FunctionType( Type::Qualifiers(), false );
    469440                        ctorType->get_parameters().push_back( new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), (*i)->get_name(), *i ) ), 0 ) );
    470441                        (*i)->get_assertions().push_front( new FunctionDecl( "?{}", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, ctorType, 0, false, false ) );
    471442
    472443                        // add assignment operator:  T * ?=?(T *, T)
    473                         FunctionType *assignType = new FunctionType( Type::Qualifiers(), false );
     444                        FunctionType * assignType = new FunctionType( Type::Qualifiers(), false );
    474445                        assignType->get_parameters().push_back( new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), (*i)->get_name(), *i ) ), 0 ) );
    475446                        assignType->get_parameters().push_back( new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new TypeInstType( Type::Qualifiers(), (*i)->get_name(), *i ), 0 ) );
     
    480451}
    481452
    482 Declaration *TypeData::buildDecl( std::string name, DeclarationNode::StorageClass sc, Expression *bitfieldWidth, bool isInline, bool isNoreturn, LinkageSpec::Spec linkage, Initializer *init ) const {
    483         if ( kind == TypeData::Function ) {
    484                 FunctionDecl *decl;
    485                 if ( function->hasBody ) {
    486                         if ( function->body ) {
    487                                 Statement *stmt = function->body->build();
    488                                 CompoundStmt *body = dynamic_cast< CompoundStmt* >( stmt );
    489                                 assert( body );
    490                                 decl = new FunctionDecl( name, sc, linkage, buildFunction(), body, isInline, isNoreturn );
    491                         } else {
    492                                 // std::list< Label > ls;
    493                                 decl = new FunctionDecl( name, sc, linkage, buildFunction(), new CompoundStmt( std::list< Label >() ), isInline, isNoreturn );
    494                         } // if
    495                 } else {
    496                         decl = new FunctionDecl( name, sc, linkage, buildFunction(), 0, isInline, isNoreturn );
    497                 } // if
    498                 for ( DeclarationNode *cur = function->idList; cur != 0; cur = dynamic_cast< DeclarationNode* >( cur->get_next() ) ) {
    499                         if ( cur->get_name() != "" ) {
    500                                 decl->get_oldIdents().insert( decl->get_oldIdents().end(), cur->get_name() );
    501                         } // if
    502                 } // for
    503                 buildList( function->oldDeclList, decl->get_oldDecls() );
    504                 return decl;
    505         } else if ( kind == TypeData::Aggregate ) {
    506                 return buildAggregate();
    507         } else if ( kind == TypeData::Enum ) {
    508                 return buildEnum();
    509         } else if ( kind == TypeData::Symbolic ) {
    510                 return buildSymbolic( name, sc );
    511         } else if ( kind == TypeData::Variable ) {
    512                 return buildVariable();
    513         } else {
    514                 return new ObjectDecl( name, sc, linkage, bitfieldWidth, build(), init, std::list< Attribute * >(),  isInline, isNoreturn );
    515         } // if
    516         return 0;
    517 }
    518 
    519 Type *TypeData::build() const {
    520         switch ( kind ) {
    521           case Unknown:
     453Type * typebuild( const TypeData * td ) {
     454        assert( td );
     455        switch ( td->kind ) {
     456          case TypeData::Unknown:
    522457                // fill in implicit int
    523                 return new BasicType( buildQualifiers(), BasicType::SignedInt );
    524           case Basic:
    525                 return buildBasicType();
    526           case Pointer:
    527                 return buildPointer();
    528           case Array:
    529                 return buildArray();
    530           case Function:
    531                 return buildFunction();
    532           case AggregateInst:
    533                 return buildAggInst();
    534           case EnumConstant:
     458                return new BasicType( buildQualifiers( td ), BasicType::SignedInt );
     459          case TypeData::Basic:
     460                return buildBasicType( td );
     461          case TypeData::Pointer:
     462                return buildPointer( td );
     463          case TypeData::Array:
     464                return buildArray( td );
     465          case TypeData::Function:
     466                return buildFunction( td );
     467          case TypeData::AggregateInst:
     468                return buildAggInst( td );
     469          case TypeData::EnumConstant:
    535470                // the name gets filled in later -- by SymTab::Validate
    536                 return new EnumInstType( buildQualifiers(), "" );
    537           case SymbolicInst:
    538                 return buildSymbolicInst();;
    539           case Tuple:
    540                 return buildTuple();
    541           case Typeof:
    542                 return buildTypeof();
    543           case Builtin:
    544                 return new VarArgsType( buildQualifiers() );
    545           case Attr:
    546                 return buildAttr();
    547           case Symbolic:
    548           case Enum:
    549           case Aggregate:
    550           case Variable:
     471                return new EnumInstType( buildQualifiers( td ), "" );
     472          case TypeData::SymbolicInst:
     473                return buildSymbolicInst( td );;
     474          case TypeData::Tuple:
     475                return buildTuple( td );
     476          case TypeData::Typeof:
     477                return buildTypeof( td );
     478          case TypeData::Builtin:
     479                return new VarArgsType( buildQualifiers( td ) );
     480          case TypeData::Attr:
     481                return buildAttr( td );
     482          case TypeData::Symbolic:
     483          case TypeData::Enum:
     484          case TypeData::Aggregate:
     485          case TypeData::Variable:
    551486                assert( false );
    552487        } // switch
    553488        return 0;
    554 }
    555 
    556 Type::Qualifiers TypeData::buildQualifiers() const {
     489} // typebuild
     490
     491TypeData * typeextractAggregate( const TypeData * td, bool toplevel ) {
     492        TypeData * ret = 0;
     493
     494        switch ( td->kind ) {
     495          case TypeData::Aggregate:
     496                if ( ! toplevel && td->aggregate->fields ) {
     497                        ret = td->clone();
     498                } // if
     499                break;
     500          case TypeData::Enum:
     501                if ( ! toplevel && td->enumeration->constants ) {
     502                        ret = td->clone();
     503                } // if
     504                break;
     505          case TypeData::AggregateInst:
     506                if ( td->aggInst->aggregate ) {
     507                        ret = typeextractAggregate( td->aggInst->aggregate, false );
     508                } // if
     509                break;
     510          default:
     511                if ( td->base ) {
     512                        ret = typeextractAggregate( td->base, false );
     513                } // if
     514        } // switch
     515        return ret;
     516} // typeextractAggregate
     517
     518Type::Qualifiers buildQualifiers( const TypeData * td ) {
    557519        Type::Qualifiers q;
    558         q.isConst = qualifiers[ DeclarationNode::Const ];
    559         q.isVolatile = qualifiers[ DeclarationNode::Volatile ];
    560         q.isRestrict = qualifiers[ DeclarationNode::Restrict ];
    561         q.isLvalue = qualifiers[ DeclarationNode::Lvalue ];
    562         q.isAtomic = qualifiers[ DeclarationNode::Atomic ];;
     520        q.isConst = td->qualifiers[ DeclarationNode::Const ];
     521        q.isVolatile = td->qualifiers[ DeclarationNode::Volatile ];
     522        q.isRestrict = td->qualifiers[ DeclarationNode::Restrict ];
     523        q.isLvalue = td->qualifiers[ DeclarationNode::Lvalue ];
     524        q.isAtomic = td->qualifiers[ DeclarationNode::Atomic ];;
    563525        return q;
    564 }
    565 
    566 Type *TypeData::buildBasicType() const {
     526} // buildQualifiers
     527
     528Type * buildBasicType( const TypeData * td ) {
    567529        static const BasicType::Kind kindMap[] = { BasicType::Char, BasicType::SignedInt, BasicType::Float, BasicType::Double,
    568530                                                                                           BasicType::Char /* void */, BasicType::Bool, BasicType::DoubleComplex,
     
    573535        BasicType::Kind ret;
    574536
    575         for ( std::list< DeclarationNode::BasicType >::const_iterator i = basic->typeSpec.begin(); i != basic->typeSpec.end(); ++i ) {
     537        for ( std::list< DeclarationNode::BasicType >::const_iterator i = td->basic->typeSpec.begin(); i != td->basic->typeSpec.end(); ++i ) {
    576538                if ( ! init ) {
    577539                        init = true;
    578540                        if ( *i == DeclarationNode::Void ) {
    579                                 if ( basic->typeSpec.size() != 1 || ! basic->modifiers.empty() ) {
    580                                         throw SemanticError( "invalid type specifier \"void\" in type: ", this );
     541                                if ( td->basic->typeSpec.size() != 1 || ! td->basic->modifiers.empty() ) {
     542                                        throw SemanticError( "invalid type specifier \"void\" in type: ", td );
    581543                                } else {
    582                                         return new VoidType( buildQualifiers() );
     544                                        return new VoidType( buildQualifiers( td ) );
    583545                                } // if
    584546                        } else {
     
    589551                          case DeclarationNode::Float:
    590552                                if ( sawDouble ) {
    591                                         throw SemanticError( "invalid type specifier \"float\" in type: ", this );
     553                                        throw SemanticError( "invalid type specifier \"float\" in type: ", td );
    592554                                } else {
    593555                                        switch ( ret ) {
     
    599561                                                break;
    600562                                          default:
    601                                                 throw SemanticError( "invalid type specifier \"float\" in type: ", this );
     563                                                throw SemanticError( "invalid type specifier \"float\" in type: ", td );
    602564                                        } // switch
    603565                                } // if
     
    605567                          case DeclarationNode::Double:
    606568                                if ( sawDouble ) {
    607                                         throw SemanticError( "duplicate type specifier \"double\" in type: ", this );
     569                                        throw SemanticError( "duplicate type specifier \"double\" in type: ", td );
    608570                                } else {
    609571                                        switch ( ret ) {
     
    612574                                                break;
    613575                                          default:
    614                                                 throw SemanticError( "invalid type specifier \"double\" in type: ", this );
     576                                                throw SemanticError( "invalid type specifier \"double\" in type: ", td );
    615577                                        } // switch
    616578                                } // if
     
    625587                                        break;
    626588                                  default:
    627                                         throw SemanticError( "invalid type specifier \"_Complex\" in type: ", this );
     589                                        throw SemanticError( "invalid type specifier \"_Complex\" in type: ", td );
    628590                                } // switch
    629591                                break;
     
    637599                                        break;
    638600                                  default:
    639                                         throw SemanticError( "invalid type specifier \"_Imaginary\" in type: ", this );
     601                                        throw SemanticError( "invalid type specifier \"_Imaginary\" in type: ", td );
    640602                                } // switch
    641603                                break;
    642604                          default:
    643                                 throw SemanticError( std::string( "invalid type specifier \"" ) + DeclarationNode::basicTypeName[ *i ] + "\" in type: ", this );
     605                                throw SemanticError( std::string( "invalid type specifier \"" ) + DeclarationNode::basicTypeName[ *i ] + "\" in type: ", td );
    644606                        } // switch
    645607                } // if
     
    649611        } // for
    650612
    651         for ( std::list< DeclarationNode::Modifier >::const_iterator i = basic->modifiers.begin(); i != basic->modifiers.end(); ++i ) {
     613        for ( std::list< DeclarationNode::Modifier >::const_iterator i = td->basic->modifiers.begin(); i != td->basic->modifiers.end(); ++i ) {
    652614                switch ( *i ) {
    653615                  case DeclarationNode::Long:
     
    679641                                        break;
    680642                                  default:
    681                                         throw SemanticError( "invalid type modifier \"long\" in type: ", this );
     643                                        throw SemanticError( "invalid type modifier \"long\" in type: ", td );
    682644                                } // switch
    683645                        } // if
     
    696658                                        break;
    697659                                  default:
    698                                         throw SemanticError( "invalid type modifier \"short\" in type: ", this );
     660                                        throw SemanticError( "invalid type modifier \"short\" in type: ", td );
    699661                                } // switch
    700662                        } // if
     
    705667                                ret = BasicType::SignedInt;
    706668                        } else if ( sawSigned ) {
    707                                 throw SemanticError( "duplicate type modifer \"signed\" in type: ", this );
     669                                throw SemanticError( "duplicate type modifer \"signed\" in type: ", td );
    708670                        } else {
    709671                                switch ( ret ) {
     
    721683                                        break;
    722684                                  default:
    723                                         throw SemanticError( "invalid type modifer \"signed\" in type: ", this );
     685                                        throw SemanticError( "invalid type modifer \"signed\" in type: ", td );
    724686                                } // switch
    725687                        } // if
     
    730692                                ret = BasicType::UnsignedInt;
    731693                        } else if ( sawSigned ) {
    732                                 throw SemanticError( "invalid type modifer \"unsigned\" in type: ", this );
     694                                throw SemanticError( "invalid type modifer \"unsigned\" in type: ", td );
    733695                        } else {
    734696                                switch ( ret ) {
     
    749711                                        break;
    750712                                  default:
    751                                         throw SemanticError( "invalid type modifer \"unsigned\" in type: ", this );
     713                                        throw SemanticError( "invalid type modifer \"unsigned\" in type: ", td );
    752714                                } // switch
    753715                        } // if
     
    760722        } // for
    761723
    762         BasicType *bt;
     724        BasicType * bt;
    763725        if ( ! init ) {
    764                 bt = new BasicType( buildQualifiers(), BasicType::SignedInt );
     726                bt = new BasicType( buildQualifiers( td ), BasicType::SignedInt );
    765727        } else {
    766                 bt = new BasicType( buildQualifiers(), ret );
     728                bt = new BasicType( buildQualifiers( td ), ret );
    767729        } // if
    768         buildForall( forall, bt->get_forall() );
     730        buildForall( td->forall, bt->get_forall() );
    769731        return bt;
    770 }
    771 
    772 
    773 PointerType *TypeData::buildPointer() const {
    774         PointerType *pt;
    775         if ( base ) {
    776                 pt = new PointerType( buildQualifiers(), base->build() );
     732} // buildBasicType
     733
     734PointerType * buildPointer( const TypeData * td ) {
     735        PointerType * pt;
     736        if ( td->base ) {
     737                pt = new PointerType( buildQualifiers( td ), typebuild( td->base ) );
    777738        } else {
    778                 pt = new PointerType( buildQualifiers(), new BasicType( Type::Qualifiers(), BasicType::SignedInt ) );
     739                pt = new PointerType( buildQualifiers( td ), new BasicType( Type::Qualifiers(), BasicType::SignedInt ) );
    779740        } // if
    780         buildForall( forall, pt->get_forall() );
     741        buildForall( td->forall, pt->get_forall() );
    781742        return pt;
    782 }
    783 
    784 ArrayType *TypeData::buildArray() const {
    785         ArrayType *at;
    786         if ( base ) {
    787                 at = new ArrayType( buildQualifiers(), base->build(), maybeBuild< Expression >( array->dimension ),
    788                                                         array->isVarLen, array->isStatic );
     743} // buildPointer
     744
     745ArrayType * buildArray( const TypeData * td ) {
     746        ArrayType * at;
     747        if ( td->base ) {
     748                at = new ArrayType( buildQualifiers( td ), typebuild( td->base ), maybeBuild< Expression >( td->array->dimension ),
     749                                                        td->array->isVarLen, td->array->isStatic );
    789750        } else {
    790                 at = new ArrayType( buildQualifiers(), new BasicType( Type::Qualifiers(), BasicType::SignedInt ),
    791                                                         maybeBuild< Expression >( array->dimension ), array->isVarLen, array->isStatic );
     751                at = new ArrayType( buildQualifiers( td ), new BasicType( Type::Qualifiers(), BasicType::SignedInt ),
     752                                                        maybeBuild< Expression >( td->array->dimension ), td->array->isVarLen, td->array->isStatic );
    792753        } // if
    793         buildForall( forall, at->get_forall() );
     754        buildForall( td->forall, at->get_forall() );
    794755        return at;
    795 }
    796 
    797 FunctionType *TypeData::buildFunction() const {
    798         assert( kind == Function );
    799         bool hasEllipsis = function->params ? function->params->get_hasEllipsis() : true;
    800         if ( ! function->params ) hasEllipsis = ! function->newStyle;
    801         FunctionType *ft = new FunctionType( buildQualifiers(), hasEllipsis );
    802         buildList( function->params, ft->get_parameters() );
    803         buildForall( forall, ft->get_forall() );
    804         if ( base ) {
    805                 switch ( base->kind ) {
    806                   case Tuple:
    807                         buildList( base->tuple->members, ft->get_returnVals() );
     756} // buildPointer
     757
     758AggregateDecl * buildAggregate( const TypeData * td ) {
     759        assert( td->kind == TypeData::Aggregate );
     760        AggregateDecl * at;
     761        switch ( td->aggregate->kind ) {
     762          case DeclarationNode::Struct:
     763                at = new StructDecl( td->aggregate->name );
     764                buildForall( td->aggregate->params, at->get_parameters() );
     765                break;
     766          case DeclarationNode::Union:
     767                at = new UnionDecl( td->aggregate->name );
     768                buildForall( td->aggregate->params, at->get_parameters() );
     769                break;
     770          case DeclarationNode::Trait:
     771                at = new TraitDecl( td->aggregate->name );
     772                buildList( td->aggregate->params, at->get_parameters() );
     773                break;
     774          default:
     775                assert( false );
     776        } // switch
     777
     778        buildList( td->aggregate->fields, at->get_members() );
     779        at->set_body( td->aggregate->body );
     780
     781        return at;
     782} // buildAggregate
     783
     784ReferenceToType * buildAggInst( const TypeData * td ) {
     785        assert( td->kind == TypeData::AggregateInst );
     786
     787        ReferenceToType * ret;
     788        if ( td->aggInst->aggregate->kind == TypeData::Enum ) {
     789                ret = new EnumInstType( buildQualifiers( td ), td->aggInst->aggregate->enumeration->name );
     790        } else {
     791                assert( td->aggInst->aggregate->kind == TypeData::Aggregate );
     792                switch ( td->aggInst->aggregate->aggregate->kind ) {
     793                  case DeclarationNode::Struct:
     794                        ret = new StructInstType( buildQualifiers( td ), td->aggInst->aggregate->aggregate->name );
     795                        break;
     796                  case DeclarationNode::Union:
     797                        ret = new UnionInstType( buildQualifiers( td ), td->aggInst->aggregate->aggregate->name );
     798                        break;
     799                  case DeclarationNode::Trait:
     800                        ret = new TraitInstType( buildQualifiers( td ), td->aggInst->aggregate->aggregate->name );
    808801                        break;
    809802                  default:
    810                         ft->get_returnVals().push_back( dynamic_cast< DeclarationWithType* >( base->buildDecl( "", DeclarationNode::NoStorageClass, 0, false, false, LinkageSpec::Cforall ) ) );
     803                        assert( false );
     804                } // switch
     805        } // if
     806        buildList( td->aggInst->params, ret->get_parameters() );
     807        buildForall( td->forall, ret->get_forall() );
     808        return ret;
     809} // buildAggInst
     810
     811NamedTypeDecl * buildSymbolic( const TypeData * td, const std::string & name, DeclarationNode::StorageClass sc ) {
     812        assert( td->kind == TypeData::Symbolic );
     813        NamedTypeDecl * ret;
     814        assert( td->base );
     815        if ( td->symbolic->isTypedef ) {
     816                ret = new TypedefDecl( name, sc, typebuild( td->base ) );
     817        } else {
     818                ret = new TypeDecl( name, sc, typebuild( td->base ), TypeDecl::Any );
     819        } // if
     820        buildList( td->symbolic->params, ret->get_parameters() );
     821        buildList( td->symbolic->assertions, ret->get_assertions() );
     822        return ret;
     823} // buildSymbolic
     824
     825TypeDecl * buildVariable( const TypeData * td ) {
     826        assert( td->kind == TypeData::Variable );
     827        static const TypeDecl::Kind kindMap[] = { TypeDecl::Any, TypeDecl::Ftype, TypeDecl::Dtype };
     828
     829        TypeDecl * ret = new TypeDecl( td->variable->name, DeclarationNode::NoStorageClass, 0, kindMap[ td->variable->tyClass ] );
     830        buildList( td->variable->assertions, ret->get_assertions() );
     831        return ret;
     832} // buildSymbolic
     833
     834EnumDecl * buildEnum( const TypeData * td ) {
     835        assert( td->kind == TypeData::Enum );
     836        EnumDecl * ret = new EnumDecl( td->enumeration->name );
     837        buildList( td->enumeration->constants, ret->get_members() );
     838        std::list< Declaration * >::iterator members = ret->get_members().begin();
     839        for ( const DeclarationNode * cur = td->enumeration-> constants; cur != nullptr; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ), ++members ) {
     840                if ( cur->has_enumeratorValue() ) {
     841                        ObjectDecl * member = dynamic_cast< ObjectDecl * >(* members);
     842                        member->set_init( new SingleInit( maybeMoveBuild< Expression >( cur->consume_enumeratorValue() ), std::list< Expression * >() ) );
     843                } // if
     844        } // for
     845        return ret;
     846} // buildEnum
     847
     848TypeInstType * buildSymbolicInst( const TypeData * td ) {
     849        assert( td->kind == TypeData::SymbolicInst );
     850        TypeInstType * ret = new TypeInstType( buildQualifiers( td ), td->symbolic->name, false );
     851        buildList( td->symbolic->actuals, ret->get_parameters() );
     852        buildForall( td->forall, ret->get_forall() );
     853        return ret;
     854} // buildSymbolicInst
     855
     856TupleType * buildTuple( const TypeData * td ) {
     857        assert( td->kind == TypeData::Tuple );
     858        TupleType * ret = new TupleType( buildQualifiers( td ) );
     859        buildTypeList( td->tuple->members, ret->get_types() );
     860        buildForall( td->forall, ret->get_forall() );
     861        return ret;
     862} // buildTuple
     863
     864TypeofType * buildTypeof( const TypeData * td ) {
     865        assert( td->kind == TypeData::Typeof );
     866        assert( td->typeexpr );
     867        assert( td->typeexpr->expr );
     868        return new TypeofType( buildQualifiers( td ), td->typeexpr->expr->build() );
     869} // buildTypeof
     870
     871AttrType * buildAttr( const TypeData * td ) {
     872        assert( td->kind == TypeData::Attr );
     873        assert( td->attr );
     874        AttrType * ret;
     875        if ( td->attr->expr ) {
     876                ret = new AttrType( buildQualifiers( td ), td->attr->name, td->attr->expr->build() );
     877        } else {
     878                assert( td->attr->type );
     879                ret = new AttrType( buildQualifiers( td ), td->attr->name, td->attr->type->buildType() );
     880        } // if
     881        return ret;
     882} // buildAttr
     883
     884Declaration * buildDecl( const TypeData * td, std::string name, DeclarationNode::StorageClass sc, Expression * bitfieldWidth, bool isInline, bool isNoreturn, LinkageSpec::Spec linkage, Initializer * init ) {
     885        if ( td->kind == TypeData::Function ) {
     886                FunctionDecl * decl;
     887                if ( td->function->hasBody ) {
     888                        if ( td->function->body ) {
     889                                Statement * stmt = td->function->body->build();
     890                                CompoundStmt * body = dynamic_cast< CompoundStmt* >( stmt );
     891                                assert( body );
     892                                decl = new FunctionDecl( name, sc, linkage, buildFunction( td ), body, isInline, isNoreturn );
     893                        } else {
     894                                // std::list< Label > ls;
     895                                decl = new FunctionDecl( name, sc, linkage, buildFunction( td ), new CompoundStmt( std::list< Label >() ), isInline, isNoreturn );
     896                        } // if
     897                } else {
     898                        decl = new FunctionDecl( name, sc, linkage, buildFunction( td ), 0, isInline, isNoreturn );
     899                } // if
     900                for ( DeclarationNode * cur = td->function->idList; cur != 0; cur = dynamic_cast< DeclarationNode* >( cur->get_next() ) ) {
     901                        if ( cur->get_name() != "" ) {
     902                                decl->get_oldIdents().insert( decl->get_oldIdents().end(), cur->get_name() );
     903                        } // if
     904                } // for
     905                buildList( td->function->oldDeclList, decl->get_oldDecls() );
     906                return decl;
     907        } else if ( td->kind == TypeData::Aggregate ) {
     908                return buildAggregate( td );
     909        } else if ( td->kind == TypeData::Enum ) {
     910                return buildEnum( td );
     911        } else if ( td->kind == TypeData::Symbolic ) {
     912                return buildSymbolic( td, name, sc );
     913        } else if ( td->kind == TypeData::Variable ) {
     914                return buildVariable( td );
     915        } else {
     916                return new ObjectDecl( name, sc, linkage, bitfieldWidth, typebuild( td ), init, std::list< Attribute * >(), isInline, isNoreturn );
     917        } // if
     918        return 0;
     919} // buildDecl
     920
     921FunctionType * buildFunction( const TypeData * td ) {
     922        assert( td->kind == TypeData::Function );
     923        bool hasEllipsis = td->function->params ? td->function->params->get_hasEllipsis() : true;
     924        if ( ! td->function->params ) hasEllipsis = ! td->function->newStyle;
     925        FunctionType * ft = new FunctionType( buildQualifiers( td ), hasEllipsis );
     926        buildList( td->function->params, ft->get_parameters() );
     927        buildForall( td->forall, ft->get_forall() );
     928        if ( td->base ) {
     929                switch ( td->base->kind ) {
     930                  case TypeData::Tuple:
     931                        buildList( td->base->tuple->members, ft->get_returnVals() );
     932                        break;
     933                  default:
     934                        ft->get_returnVals().push_back( dynamic_cast< DeclarationWithType* >( buildDecl( td->base,  "", DeclarationNode::NoStorageClass, 0, false, false, LinkageSpec::Cforall ) ) );
    811935                } // switch
    812936        } else {
     
    814938        } // if
    815939        return ft;
    816 }
    817 
    818 AggregateDecl *TypeData::buildAggregate() const {
    819         assert( kind == Aggregate );
    820         AggregateDecl *at;
    821         switch ( aggregate->kind ) {
    822           case DeclarationNode::Struct:
    823                 at = new StructDecl( aggregate->name );
    824                 buildForall( aggregate->params, at->get_parameters() );
    825                 break;
    826           case DeclarationNode::Union:
    827                 at = new UnionDecl( aggregate->name );
    828                 buildForall( aggregate->params, at->get_parameters() );
    829                 break;
    830           case DeclarationNode::Trait:
    831                 at = new TraitDecl( aggregate->name );
    832                 buildList( aggregate->params, at->get_parameters() );
    833                 break;
    834           default:
    835                 assert( false );
    836         } // switch
    837 
    838         buildList( aggregate->fields, at->get_members() );
    839         at->set_body( aggregate->body );
    840 
    841         return at;
    842 }
    843 
    844 ReferenceToType *TypeData::buildAggInst() const {
    845         assert( kind == AggregateInst );
    846 
    847         ReferenceToType *ret;
    848         if ( aggInst->aggregate->kind == Enum ) {
    849                 ret = new EnumInstType( buildQualifiers(), aggInst->aggregate->enumeration->name );
    850         } else {
    851                 assert( aggInst->aggregate->kind == Aggregate );
    852                 switch ( aggInst->aggregate->aggregate->kind ) {
    853                   case DeclarationNode::Struct:
    854                         ret = new StructInstType( buildQualifiers(), aggInst->aggregate->aggregate->name );
    855                         break;
    856                   case DeclarationNode::Union:
    857                         ret = new UnionInstType( buildQualifiers(), aggInst->aggregate->aggregate->name );
    858                         break;
    859                   case DeclarationNode::Trait:
    860                         ret = new TraitInstType( buildQualifiers(), aggInst->aggregate->aggregate->name );
    861                         break;
    862                   default:
    863                         assert( false );
    864                 } // switch
    865         } // if
    866         buildList( aggInst->params, ret->get_parameters() );
    867         buildForall( forall, ret->get_forall() );
    868         return ret;
    869 }
    870 
    871 NamedTypeDecl *TypeData::buildSymbolic( const std::string &name, DeclarationNode::StorageClass sc ) const {
    872         assert( kind == Symbolic );
    873         NamedTypeDecl *ret;
    874         if ( symbolic->isTypedef ) {
    875                 ret = new TypedefDecl( name, sc, maybeBuild< Type >( base ) );
    876         } else {
    877                 ret = new TypeDecl( name, sc, maybeBuild< Type >( base ), TypeDecl::Any );
    878         } // if
    879         buildList( symbolic->params, ret->get_parameters() );
    880         buildList( symbolic->assertions, ret->get_assertions() );
    881         return ret;
    882 }
    883 
    884 TypeDecl *TypeData::buildVariable() const {
    885         assert( kind == Variable );
    886         static const TypeDecl::Kind kindMap[] = { TypeDecl::Any, TypeDecl::Ftype, TypeDecl::Dtype };
    887 
    888         TypeDecl *ret = new TypeDecl( variable->name, DeclarationNode::NoStorageClass, 0, kindMap[ variable->tyClass ] );
    889         buildList( variable->assertions, ret->get_assertions() );
    890         return ret;
    891 }
    892 
    893 EnumDecl *TypeData::buildEnum() const {
    894         assert( kind == Enum );
    895         EnumDecl *ret = new EnumDecl( enumeration->name );
    896         buildList( enumeration->constants, ret->get_members() );
    897         std::list< Declaration * >::iterator members = ret->get_members().begin();
    898         for ( const DeclarationNode *cur = enumeration->constants; cur != nullptr; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ), ++members ) {
    899                 if ( cur->has_enumeratorValue() ) {
    900                         ObjectDecl *member = dynamic_cast< ObjectDecl * >(*members);
    901                         member->set_init( new SingleInit( maybeMoveBuild< Expression >( cur->consume_enumeratorValue() ), std::list< Expression * >() ) );
    902                 } // if
    903         } // for
    904         return ret;
    905 }
    906 
    907 TypeInstType *TypeData::buildSymbolicInst() const {
    908         assert( kind == SymbolicInst );
    909         TypeInstType *ret = new TypeInstType( buildQualifiers(), symbolic->name, false );
    910         buildList( symbolic->actuals, ret->get_parameters() );
    911         buildForall( forall, ret->get_forall() );
    912         return ret;
    913 }
    914 
    915 TupleType *TypeData::buildTuple() const {
    916         assert( kind == Tuple );
    917         TupleType *ret = new TupleType( buildQualifiers() );
    918         buildTypeList( tuple->members, ret->get_types() );
    919         buildForall( forall, ret->get_forall() );
    920         return ret;
    921 }
    922 
    923 TypeofType *TypeData::buildTypeof() const {
    924         assert( kind == Typeof );
    925         assert( typeexpr );
    926         assert( typeexpr->expr );
    927         TypeofType *ret = new TypeofType( buildQualifiers(), typeexpr->expr->build() );
    928         return ret;
    929 }
    930 
    931 AttrType *TypeData::buildAttr() const {
    932         assert( kind == Attr );
    933         assert( attr );
    934         AttrType *ret;
    935         if ( attr->expr ) {
    936                 ret = new AttrType( buildQualifiers(), attr->name, attr->expr->build() );
    937         } else {
    938                 assert( attr->type );
    939                 ret = new AttrType( buildQualifiers(), attr->name, attr->type->buildType() );
    940         } // if
    941         return ret;
    942 }
     940} // buildFunction
    943941
    944942// Local Variables: //
Note: See TracChangeset for help on using the changeset viewer.