Ignore:
Timestamp:
Oct 23, 2020, 9:06:16 PM (4 years ago)
Author:
Fangren Yu <f37yu@…>
Branches:
ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
37b7d95
Parents:
41b8ea4
Message:

move FixInit? to new ast

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/AST/Convert.cpp

    r41b8ea4 r490fb92e  
    4747
    4848//================================================================================================
    49 namespace {
     49namespace ast {
    5050
    5151// This is to preserve the FindSpecialDecls hack. It does not (and perhaps should not)
    5252// allow us to use the same stratagy in the new ast.
     53// xxx - since convert back pass works, this concern seems to be unnecessary.
     54
     55// these need to be accessed in new FixInit now
    5356ast::Type * sizeType = nullptr;
    5457ast::FunctionDecl * dereferenceOperator = nullptr;
     
    6366        using Cache = std::unordered_map< const ast::Node *, BaseSyntaxNode * >;
    6467        Cache cache;
     68
     69        // Statements can no longer be shared.
     70        // however, since StmtExprResult is now implemented, need to still maintain
     71        // readonly references.
     72        Cache readonlyCache;
    6573
    6674        template<typename T>
     
    154162        }
    155163
    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 );
     164        const ast::DeclWithType * visit( const ast::ObjectDecl * node ) override final {       
    161165                if ( inCache( node ) ) {
    162166                        return nullptr;
    163167                }
     168                auto bfwd = get<Expression>().accept1( node->bitfieldWidth );
     169                auto type = get<Type>().accept1( node->type );
     170                auto attr = get<Attribute>().acceptL( node->attributes );
     171
    164172                auto decl = new ObjectDecl(
    165173                        node->name,
     
    168176                        bfwd,
    169177                        type->clone(),
    170                         init,
     178                        nullptr, // prevent infinite loop
    171179                        attr,
    172180                        Type::FuncSpecifiers( node->funcSpec.val )
    173181                );
    174                 return declWithTypePostamble( decl, node );
     182
     183                // handles the case where node->init references itself
     184                // xxx - does it really happen?
     185                declWithTypePostamble(decl, node);
     186                auto init = get<Initializer>().accept1( node->init );
     187                decl->init = init;
     188               
     189                this->node = decl;
     190                return nullptr;
    175191        }
    176192
     
    205221                decl->statements = get<CompoundStmt>().accept1( node->stmts );
    206222                decl->withExprs = get<Expression>().acceptL( node->withExprs );
    207                 if ( dereferenceOperator == node ) {
     223                if ( ast::dereferenceOperator == node ) {
    208224                        Validate::dereferenceOperator = decl;
    209225                }
    210                 if ( dtorStructDestroy == node ) {
     226                if ( ast::dtorStructDestroy == node ) {
    211227                        Validate::dtorStructDestroy = decl;
    212228                }
     
    267283                );
    268284
    269                 if ( dtorStruct == node ) {
     285                if ( ast::dtorStruct == node ) {
    270286                        Validate::dtorStruct = decl;
    271287                }
     
    320336
    321337        const ast::Stmt * stmtPostamble( Statement * stmt, const ast::Stmt * node ) {
    322                 cache.emplace( node, stmt );
     338                // force statements in old tree to be unique.
     339                // cache.emplace( node, stmt );
     340                readonlyCache.emplace( node, stmt );
    323341                stmt->location = node->location;
    324342                stmt->labels = makeLabelL( stmt, node->labels );
     
    337355                if ( inCache( node ) ) return nullptr;
    338356                auto stmt = new ExprStmt( nullptr );
    339                 cache.emplace( node, stmt );
    340357                stmt->expr = get<Expression>().accept1( node->expr );
    341358                return stmtPostamble( stmt, node );
     
    10111028                auto stmts = node->stmts;
    10121029                // disable sharing between multiple StmtExprs explicitly.
    1013                 if (inCache(stmts)) {
    1014                         stmts = ast::deepCopy(stmts.get());
    1015                 }
     1030                // this should no longer be true.
     1031
    10161032                auto rslt = new StmtExpr(
    10171033                        get<CompoundStmt>().accept1(stmts)
     
    10201036                rslt->returnDecls = get<ObjectDecl>().acceptL(node->returnDecls);
    10211037                rslt->dtors       = get<Expression>().acceptL(node->dtors);
     1038                if (node->resultExpr) {
     1039                        // this MUST be found by children visit
     1040                        rslt->resultExpr  = strict_dynamic_cast<ExprStmt *>(readonlyCache.at(node->resultExpr));
     1041                }
    10221042
    10231043                auto expr = visitBaseExpr( node, rslt );
     
    10361056
    10371057                auto expr = visitBaseExpr( node, rslt );
    1038                 this->node = expr;
     1058                this->node = expr->clone();
    10391059                return nullptr;
    10401060        }
     
    11261146                auto type = new BasicType{ cv( node ), (BasicType::Kind)(unsigned)node->kind };
    11271147                // I believe this should always be a BasicType.
    1128                 if ( sizeType == node ) {
     1148                if ( ast::sizeType == node ) {
    11291149                        Validate::SizeType = type;
    11301150                }
     
    15291549
    15301550                // function type is now derived from parameter decls instead of storing them
     1551
     1552                /*
    15311553                auto ftype = new ast::FunctionType((ast::ArgumentFlag)old->type->isVarArgs, cv(old->type));
    15321554                ftype->params.reserve(paramVars.size());
     
    15401562                }
    15411563                ftype->forall = std::move(forall);
    1542                 visitType(old->type, ftype);
     1564                */
     1565
     1566                // can function type have attributes? seems not to be the case.
     1567                // visitType(old->type, ftype);
    15431568
    15441569                auto decl = new ast::FunctionDecl{
     
    15461571                        old->name,
    15471572                        // GET_ACCEPT_1(type, FunctionType),
     1573                        std::move(forall),
    15481574                        std::move(paramVars),
    15491575                        std::move(returnVars),
     
    15521578                        { old->linkage.val },
    15531579                        GET_ACCEPT_V(attributes, Attribute),
    1554                         { old->get_funcSpec().val }
     1580                        { old->get_funcSpec().val },
     1581                        old->type->isVarArgs
    15551582                };
    15561583
    1557                 decl->type = ftype;
     1584                // decl->type = ftype;
    15581585                cache.emplace( old, decl );
    15591586
     
    15701597
    15711598                if ( Validate::dereferenceOperator == old ) {
    1572                         dereferenceOperator = decl;
     1599                        ast::dereferenceOperator = decl;
    15731600                }
    15741601
    15751602                if ( Validate::dtorStructDestroy == old ) {
    1576                         dtorStructDestroy = decl;
     1603                        ast::dtorStructDestroy = decl;
    15771604                }
    15781605        }
     
    15991626
    16001627                if ( Validate::dtorStruct == old ) {
    1601                         dtorStruct = decl;
     1628                        ast::dtorStruct = decl;
    16021629                }
    16031630        }
     
    25312558                // I believe this should always be a BasicType.
    25322559                if ( Validate::SizeType == old ) {
    2533                         sizeType = type;
     2560                        ast::sizeType = type;
    25342561                }
    25352562                visitType( old, type );
Note: See TracChangeset for help on using the changeset viewer.