Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/AST/Convert.cpp

    r293dc1c r954c954  
    2525#include "AST/Init.hpp"
    2626#include "AST/Stmt.hpp"
    27 #include "AST/TranslationUnit.hpp"
    2827#include "AST/TypeSubstitution.hpp"
    2928
     
    4847
    4948//================================================================================================
    50 namespace ast {
     49namespace {
    5150
    5251// This is to preserve the FindSpecialDecls hack. It does not (and perhaps should not)
    5352// allow us to use the same stratagy in the new ast.
    54 // xxx - since convert back pass works, this concern seems to be unnecessary.
    55 
    56 // these need to be accessed in new FixInit now
    5753ast::Type * sizeType = nullptr;
    5854ast::FunctionDecl * dereferenceOperator = nullptr;
     
    6763        using Cache = std::unordered_map< const ast::Node *, BaseSyntaxNode * >;
    6864        Cache cache;
    69 
    70         // Statements can no longer be shared.
    71         // however, since StmtExprResult is now implemented, need to still maintain
    72         // readonly references.
    73         Cache readonlyCache;
    7465
    7566        template<typename T>
     
    163154        }
    164155
    165         const ast::DeclWithType * visit( const ast::ObjectDecl * node ) override final {       
     156        const ast::DeclWithType * visit( const ast::ObjectDecl * node ) override final {
     157                auto&& bfwd = get<Expression>().accept1( node->bitfieldWidth );
     158                auto&& type = get<Type>().accept1( node->type );
     159                auto&& init = get<Initializer>().accept1( node->init );
     160                auto&& attr = get<Attribute>().acceptL( node->attributes );
    166161                if ( inCache( node ) ) {
    167162                        return nullptr;
    168163                }
    169                 auto bfwd = get<Expression>().accept1( node->bitfieldWidth );
    170                 auto type = get<Type>().accept1( node->type );
    171                 auto attr = get<Attribute>().acceptL( node->attributes );
    172 
    173164                auto decl = new ObjectDecl(
    174165                        node->name,
     
    177168                        bfwd,
    178169                        type->clone(),
    179                         nullptr, // prevent infinite loop
     170                        init,
    180171                        attr,
    181172                        Type::FuncSpecifiers( node->funcSpec.val )
    182173                );
    183 
    184                 // handles the case where node->init references itself
    185                 // xxx - does it really happen?
    186                 declWithTypePostamble(decl, node);
    187                 auto init = get<Initializer>().accept1( node->init );
    188                 decl->init = init;
    189                
    190                 this->node = decl;
    191                 return nullptr;
     174                return declWithTypePostamble( decl, node );
    192175        }
    193176
     
    222205                decl->statements = get<CompoundStmt>().accept1( node->stmts );
    223206                decl->withExprs = get<Expression>().acceptL( node->withExprs );
    224                 if ( ast::dereferenceOperator == node ) {
     207                if ( dereferenceOperator == node ) {
    225208                        Validate::dereferenceOperator = decl;
    226209                }
    227                 if ( ast::dtorStructDestroy == node ) {
     210                if ( dtorStructDestroy == node ) {
    228211                        Validate::dtorStructDestroy = decl;
    229212                }
     
    284267                );
    285268
    286                 if ( ast::dtorStruct == node ) {
     269                if ( dtorStruct == node ) {
    287270                        Validate::dtorStruct = decl;
    288271                }
     
    337320
    338321        const ast::Stmt * stmtPostamble( Statement * stmt, const ast::Stmt * node ) {
    339                 // force statements in old tree to be unique.
    340                 // cache.emplace( node, stmt );
    341                 readonlyCache.emplace( node, stmt );
     322                cache.emplace( node, stmt );
    342323                stmt->location = node->location;
    343324                stmt->labels = makeLabelL( stmt, node->labels );
     
    356337                if ( inCache( node ) ) return nullptr;
    357338                auto stmt = new ExprStmt( nullptr );
     339                cache.emplace( node, stmt );
    358340                stmt->expr = get<Expression>().accept1( node->expr );
    359341                return stmtPostamble( stmt, node );
     
    10291011                auto stmts = node->stmts;
    10301012                // disable sharing between multiple StmtExprs explicitly.
    1031                 // this should no longer be true.
    1032 
     1013                if (inCache(stmts)) {
     1014                        stmts = ast::deepCopy(stmts.get());
     1015                }
    10331016                auto rslt = new StmtExpr(
    10341017                        get<CompoundStmt>().accept1(stmts)
     
    10371020                rslt->returnDecls = get<ObjectDecl>().acceptL(node->returnDecls);
    10381021                rslt->dtors       = get<Expression>().acceptL(node->dtors);
    1039                 if (node->resultExpr) {
    1040                         // this MUST be found by children visit
    1041                         rslt->resultExpr  = strict_dynamic_cast<ExprStmt *>(readonlyCache.at(node->resultExpr));
    1042                 }
    10431022
    10441023                auto expr = visitBaseExpr( node, rslt );
     
    10571036
    10581037                auto expr = visitBaseExpr( node, rslt );
    1059                 this->node = expr->clone();
     1038                this->node = expr;
    10601039                return nullptr;
    10611040        }
     
    11471126                auto type = new BasicType{ cv( node ), (BasicType::Kind)(unsigned)node->kind };
    11481127                // I believe this should always be a BasicType.
    1149                 if ( ast::sizeType == node ) {
     1128                if ( sizeType == node ) {
    11501129                        Validate::SizeType = type;
    11511130                }
     
    14051384};
    14061385
    1407 std::list< Declaration * > convert( const ast::TranslationUnit && translationUnit ) {
     1386std::list< Declaration * > convert( const std::list< ast::ptr< ast::Decl > > && translationUnit ) {
    14081387        ConverterNewToOld c;
    14091388        std::list< Declaration * > decls;
    1410         for(auto d : translationUnit.decls) {
     1389        for(auto d : translationUnit) {
    14111390                decls.emplace_back( c.decl( d ) );
    14121391        }
     
    15501529
    15511530                // function type is now derived from parameter decls instead of storing them
    1552 
    1553                 /*
    15541531                auto ftype = new ast::FunctionType((ast::ArgumentFlag)old->type->isVarArgs, cv(old->type));
    15551532                ftype->params.reserve(paramVars.size());
     
    15631540                }
    15641541                ftype->forall = std::move(forall);
    1565                 */
    1566 
    1567                 // can function type have attributes? seems not to be the case.
    1568                 // visitType(old->type, ftype);
     1542                visitType(old->type, ftype);
    15691543
    15701544                auto decl = new ast::FunctionDecl{
     
    15721546                        old->name,
    15731547                        // GET_ACCEPT_1(type, FunctionType),
    1574                         std::move(forall),
    15751548                        std::move(paramVars),
    15761549                        std::move(returnVars),
     
    15791552                        { old->linkage.val },
    15801553                        GET_ACCEPT_V(attributes, Attribute),
    1581                         { old->get_funcSpec().val },
    1582                         old->type->isVarArgs
     1554                        { old->get_funcSpec().val }
    15831555                };
    15841556
    1585                 // decl->type = ftype;
     1557                decl->type = ftype;
    15861558                cache.emplace( old, decl );
    15871559
     
    15981570
    15991571                if ( Validate::dereferenceOperator == old ) {
    1600                         ast::dereferenceOperator = decl;
     1572                        dereferenceOperator = decl;
    16011573                }
    16021574
    16031575                if ( Validate::dtorStructDestroy == old ) {
    1604                         ast::dtorStructDestroy = decl;
     1576                        dtorStructDestroy = decl;
    16051577                }
    16061578        }
     
    16271599
    16281600                if ( Validate::dtorStruct == old ) {
    1629                         ast::dtorStruct = decl;
     1601                        dtorStruct = decl;
    16301602                }
    16311603        }
     
    25592531                // I believe this should always be a BasicType.
    25602532                if ( Validate::SizeType == old ) {
    2561                         ast::sizeType = type;
     2533                        sizeType = type;
    25622534                }
    25632535                visitType( old, type );
     
    28042776#undef GET_ACCEPT_1
    28052777
    2806 ast::TranslationUnit convert( const std::list< Declaration * > && translationUnit ) {
     2778std::list< ast::ptr< ast::Decl > > convert( const std::list< Declaration * > && translationUnit ) {
    28072779        ConverterOldToNew c;
    2808         ast::TranslationUnit unit;
     2780        std::list< ast::ptr< ast::Decl > > decls;
    28092781        for(auto d : translationUnit) {
    28102782                d->accept( c );
    2811                 unit.decls.emplace_back( c.decl() );
     2783                decls.emplace_back( c.decl() );
    28122784        }
    28132785        deleteAll(translationUnit);
    2814         return unit;
     2786        return decls;
    28152787}
Note: See TracChangeset for help on using the changeset viewer.