Changeset 400b8be for src/AST


Ignore:
Timestamp:
Mar 28, 2022, 10:41:45 AM (3 years ago)
Author:
Andrew Beach <ajbeach@…>
Branches:
ADT, ast-experimental, enum, master, pthread-emulation, qualifiedEnum
Children:
8e819a9
Parents:
f5bace8
Message:

Added StmtClause and converted the existing nodes that should be clauses.

Location:
src/AST
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • src/AST/Convert.cpp

    rf5bace8 r400b8be  
    353353        }
    354354
     355        void clausePostamble( Statement * stmt, const ast::StmtClause * node ) {
     356                stmt->location = node->location;
     357                this->node = stmt;
     358        }
     359
    355360        const ast::CompoundStmt * visit( const ast::CompoundStmt * node ) override final {
    356361                if ( inCache( node ) ) return nullptr;
     
    401406                auto stmt = new SwitchStmt(
    402407                        get<Expression>().accept1( node->cond ),
    403                         get<Statement>().acceptL( node->stmts )
     408                        get<Statement>().acceptL( node->cases )
    404409                );
    405410                return stmtPostamble( stmt, node );
    406411        }
    407412
    408         const ast::Stmt * visit( const ast::CaseStmt * node ) override final {
     413        const ast::CaseClause * visit( const ast::CaseClause * node ) override final {
    409414                if ( inCache( node ) ) return nullptr;
    410415                auto stmt = new CaseStmt(
     
    413418                        node->isDefault()
    414419                );
    415                 return stmtPostamble( stmt, node );
     420                clausePostamble( stmt, node );
     421                return nullptr;
    416422        }
    417423
     
    509515        }
    510516
    511         const ast::Stmt * visit( const ast::CatchStmt * node ) override final {
     517        const ast::CatchClause * visit( const ast::CatchClause * node ) override final {
    512518                if ( inCache( node ) ) return nullptr;
    513519                CatchStmt::Kind kind;
     
    520526                        break;
    521527                default:
    522                         assertf(false, "Invalid ast::CatchStmt::Kind: %d\n", node->kind);
     528                        assertf(false, "Invalid ast::ExceptionKind: %d\n", node->kind);
    523529                }
    524530                auto stmt = new CatchStmt(
     
    528534                        get<Statement>().accept1( node->body )
    529535                );
    530                 return stmtPostamble( stmt, node );
    531         }
    532 
    533         const ast::Stmt * visit( const ast::FinallyStmt * node ) override final {
     536                return clausePostamble( stmt, node ), nullptr;
     537        }
     538
     539        const ast::FinallyClause * visit( const ast::FinallyClause * node ) override final {
    534540                if ( inCache( node ) ) return nullptr;
    535541                auto stmt = new FinallyStmt( get<CompoundStmt>().accept1( node->body ) );
    536                 return stmtPostamble( stmt, node );
     542                return clausePostamble( stmt, node ), nullptr;
    537543        }
    538544
     
    18841890                        old->location,
    18851891                        GET_ACCEPT_1(condition, Expr),
    1886                         GET_ACCEPT_V(statements, Stmt),
     1892                        GET_ACCEPT_V(statements, CaseClause),
    18871893                        GET_LABELS_V(old->labels)
    18881894                );
     
    18921898        virtual void visit( const CaseStmt * old ) override final {
    18931899                if ( inCache( old ) ) return;
    1894                 this->node = new ast::CaseStmt(
     1900                this->node = new ast::CaseClause(
    18951901                        old->location,
    18961902                        GET_ACCEPT_1(condition, Expr),
    1897                         GET_ACCEPT_V(stmts, Stmt),
    1898                         GET_LABELS_V(old->labels)
    1899                 );
     1903                        GET_ACCEPT_V(stmts, Stmt)
     1904                );
     1905                auto labels = GET_LABELS_V(old->labels);
     1906                assertf(labels.empty(), "Labels found on CaseStmt.");
    19001907                cache.emplace( old, this->node );
    19011908        }
     
    20052012                        old->location,
    20062013                        GET_ACCEPT_1(block, CompoundStmt),
    2007                         GET_ACCEPT_V(handlers, CatchStmt),
    2008                         GET_ACCEPT_1(finallyBlock, FinallyStmt),
     2014                        GET_ACCEPT_V(handlers, CatchClause),
     2015                        GET_ACCEPT_1(finallyBlock, FinallyClause),
    20092016                        GET_LABELS_V(old->labels)
    20102017                );
     
    20262033                }
    20272034
    2028                 this->node = new ast::CatchStmt(
     2035                this->node = new ast::CatchClause(
    20292036                        old->location,
    20302037                        kind,
    20312038                        GET_ACCEPT_1(decl, Decl),
    20322039                        GET_ACCEPT_1(cond, Expr),
    2033                         GET_ACCEPT_1(body, Stmt),
    2034                         GET_LABELS_V(old->labels)
    2035                 );
     2040                        GET_ACCEPT_1(body, Stmt)
     2041                );
     2042                auto labels = GET_LABELS_V(old->labels);
     2043                assertf(labels.empty(), "Labels found on CatchStmt.");
    20362044                cache.emplace( old, this->node );
    20372045        }
     
    20392047        virtual void visit( const FinallyStmt * old ) override final {
    20402048                if ( inCache( old ) ) return;
    2041                 this->node = new ast::FinallyStmt(
    2042                         old->location,
    2043                         GET_ACCEPT_1(block, CompoundStmt),
    2044                         GET_LABELS_V(old->labels)
    2045                 );
     2049                this->node = new ast::FinallyClause(
     2050                        old->location,
     2051                        GET_ACCEPT_1(block, CompoundStmt)
     2052                );
     2053                auto labels = GET_LABELS_V(old->labels);
     2054                assertf(labels.empty(), "Labels found on FinallyStmt.");
    20462055                cache.emplace( old, this->node );
    20472056        }
  • src/AST/Fwd.hpp

    rf5bace8 r400b8be  
    4747class ForStmt;
    4848class SwitchStmt;
    49 class CaseStmt;
     49class CaseClause;
    5050class BranchStmt;
    5151class ReturnStmt;
    5252class ThrowStmt;
    5353class TryStmt;
    54 class CatchStmt;
    55 class FinallyStmt;
     54class CatchClause;
     55class FinallyClause;
    5656class SuspendStmt;
    5757class WaitForStmt;
  • src/AST/Node.cpp

    rf5bace8 r400b8be  
    160160template class ast::ptr_base< ast::SwitchStmt, ast::Node::ref_type::weak >;
    161161template class ast::ptr_base< ast::SwitchStmt, ast::Node::ref_type::strong >;
    162 template class ast::ptr_base< ast::CaseStmt, ast::Node::ref_type::weak >;
    163 template class ast::ptr_base< ast::CaseStmt, ast::Node::ref_type::strong >;
     162template class ast::ptr_base< ast::CaseClause, ast::Node::ref_type::weak >;
     163template class ast::ptr_base< ast::CaseClause, ast::Node::ref_type::strong >;
    164164template class ast::ptr_base< ast::BranchStmt, ast::Node::ref_type::weak >;
    165165template class ast::ptr_base< ast::BranchStmt, ast::Node::ref_type::strong >;
     
    170170template class ast::ptr_base< ast::TryStmt, ast::Node::ref_type::weak >;
    171171template class ast::ptr_base< ast::TryStmt, ast::Node::ref_type::strong >;
    172 template class ast::ptr_base< ast::CatchStmt, ast::Node::ref_type::weak >;
    173 template class ast::ptr_base< ast::CatchStmt, ast::Node::ref_type::strong >;
    174 template class ast::ptr_base< ast::FinallyStmt, ast::Node::ref_type::weak >;
    175 template class ast::ptr_base< ast::FinallyStmt, ast::Node::ref_type::strong >;
     172template class ast::ptr_base< ast::CatchClause, ast::Node::ref_type::weak >;
     173template class ast::ptr_base< ast::CatchClause, ast::Node::ref_type::strong >;
     174template class ast::ptr_base< ast::FinallyClause, ast::Node::ref_type::weak >;
     175template class ast::ptr_base< ast::FinallyClause, ast::Node::ref_type::strong >;
    176176template class ast::ptr_base< ast::WaitForStmt, ast::Node::ref_type::weak >;
    177177template class ast::ptr_base< ast::WaitForStmt, ast::Node::ref_type::strong >;
  • src/AST/Pass.hpp

    rf5bace8 r400b8be  
    149149        const ast::Stmt *             visit( const ast::ForStmt              * ) override final;
    150150        const ast::Stmt *             visit( const ast::SwitchStmt           * ) override final;
    151         const ast::Stmt *             visit( const ast::CaseStmt             * ) override final;
     151        const ast::CaseClause *       visit( const ast::CaseClause           * ) override final;
    152152        const ast::Stmt *             visit( const ast::BranchStmt           * ) override final;
    153153        const ast::Stmt *             visit( const ast::ReturnStmt           * ) override final;
    154154        const ast::Stmt *             visit( const ast::ThrowStmt            * ) override final;
    155155        const ast::Stmt *             visit( const ast::TryStmt              * ) override final;
    156         const ast::Stmt *             visit( const ast::CatchStmt            * ) override final;
    157         const ast::Stmt *             visit( const ast::FinallyStmt          * ) override final;
     156        const ast::CatchClause *      visit( const ast::CatchClause          * ) override final;
     157        const ast::FinallyClause *    visit( const ast::FinallyClause        * ) override final;
    158158        const ast::Stmt *             visit( const ast::SuspendStmt          * ) override final;
    159159        const ast::Stmt *             visit( const ast::WaitForStmt          * ) override final;
  • src/AST/Pass.impl.hpp

    rf5bace8 r400b8be  
    893893        if ( __visit_children() ) {
    894894                maybe_accept( node, &SwitchStmt::cond  );
    895                 maybe_accept( node, &SwitchStmt::stmts );
     895                maybe_accept( node, &SwitchStmt::cases );
    896896        }
    897897
     
    900900
    901901//--------------------------------------------------------------------------
    902 // CaseStmt
    903 template< typename core_t >
    904 const ast::Stmt * ast::Pass< core_t >::visit( const ast::CaseStmt * node ) {
    905         VISIT_START( node );
    906 
    907         if ( __visit_children() ) {
    908                 maybe_accept( node, &CaseStmt::cond  );
    909                 maybe_accept( node, &CaseStmt::stmts );
    910         }
    911 
    912         VISIT_END( Stmt, node );
     902// CaseClause
     903template< typename core_t >
     904const ast::CaseClause * ast::Pass< core_t >::visit( const ast::CaseClause * node ) {
     905        VISIT_START( node );
     906
     907        if ( __visit_children() ) {
     908                maybe_accept( node, &CaseClause::cond  );
     909                maybe_accept( node, &CaseClause::stmts );
     910        }
     911
     912        VISIT_END( CaseClause, node );
    913913}
    914914
     
    964964
    965965//--------------------------------------------------------------------------
    966 // CatchStmt
    967 template< typename core_t >
    968 const ast::Stmt * ast::Pass< core_t >::visit( const ast::CatchStmt * node ) {
     966// CatchClause
     967template< typename core_t >
     968const ast::CatchClause * ast::Pass< core_t >::visit( const ast::CatchClause * node ) {
    969969        VISIT_START( node );
    970970
     
    972972                // catch statements introduce a level of scope (for the caught exception)
    973973                guard_symtab guard { *this };
    974                 maybe_accept( node, &CatchStmt::decl );
    975                 maybe_accept( node, &CatchStmt::cond );
    976                 maybe_accept_as_compound( node, &CatchStmt::body );
    977         }
    978 
    979         VISIT_END( Stmt, node );
    980 }
    981 
    982 //--------------------------------------------------------------------------
    983 // FinallyStmt
    984 template< typename core_t >
    985 const ast::Stmt * ast::Pass< core_t >::visit( const ast::FinallyStmt * node ) {
    986         VISIT_START( node );
    987 
    988         if ( __visit_children() ) {
    989                 maybe_accept( node, &FinallyStmt::body );
    990         }
    991 
    992         VISIT_END( Stmt, node );
     974                maybe_accept( node, &CatchClause::decl );
     975                maybe_accept( node, &CatchClause::cond );
     976                maybe_accept_as_compound( node, &CatchClause::body );
     977        }
     978
     979        VISIT_END( CatchClause, node );
     980}
     981
     982//--------------------------------------------------------------------------
     983// FinallyClause
     984template< typename core_t >
     985const ast::FinallyClause * ast::Pass< core_t >::visit( const ast::FinallyClause * node ) {
     986        VISIT_START( node );
     987
     988        if ( __visit_children() ) {
     989                maybe_accept( node, &FinallyClause::body );
     990        }
     991
     992        VISIT_END( FinallyClause, node );
    993993}
    994994
  • src/AST/Print.cpp

    rf5bace8 r400b8be  
    589589
    590590                ++indent;
    591                 for ( const ast::Stmt * stmt : node->stmts ) {
     591                for ( const ast::CaseClause * stmt : node->cases ) {
    592592                        stmt->accept( *this );
    593593                }
     
    597597        }
    598598
    599         virtual const ast::Stmt * visit( const ast::CaseStmt * node ) override final {
     599        virtual const ast::CaseClause * visit( const ast::CaseClause * node ) override final {
    600600                if ( node->isDefault() ) {
    601601                        os << indent << "Default ";
     
    679679
    680680                os << indent-1 << "... and handlers:" << endl;
    681                 for ( const ast::CatchStmt * stmt : node->handlers ) {
     681                for ( const ast::CatchClause * stmt : node->handlers ) {
    682682                        os << indent;
    683683                        stmt->accept( *this );
     
    693693        }
    694694
    695         virtual const ast::Stmt * visit( const ast::CatchStmt * node ) override final {
     695        virtual const ast::CatchClause * visit( const ast::CatchClause * node ) override final {
    696696                os << "Catch ";
    697697                switch ( node->kind ) {
     
    718718        }
    719719
    720         virtual const ast::Stmt * visit( const ast::FinallyStmt * node ) override final {
     720        virtual const ast::FinallyClause * visit( const ast::FinallyClause * node ) override final {
    721721                os << "Finally Statement" << endl;
    722722                os << indent << "... with block:" << endl;
  • src/AST/Stmt.hpp

    rf5bace8 r400b8be  
    99// Author           : Aaron B. Moss
    1010// Created On       : Wed May  8 13:00:00 2019
    11 // Last Modified By : Peter A. Buhr
    12 // Last Modified On : Wed Feb  2 20:06:41 2022
    13 // Update Count     : 34
     11// Last Modified By : Andrew Beach
     12// Last Modified On : Mon Mar 28  9:50:00 2022
     13// Update Count     : 35
    1414//
    1515
     
    4747  private:
    4848        Stmt * clone() const override = 0;
     49        MUTATE_FRIEND
     50};
     51
     52// Base statement component node (only serves to group them).
     53class StmtClause : public ParseNode {
     54  public:
     55        // This is for non-statements that still belong with the statements,
     56        // but are not statements, usually some sort of clause. Often these can
     57        // (and should) be folded into the approprate parent node, but if they
     58        // cannot be, they are sub-types of this type, for organization.
     59
     60    StmtClause( const CodeLocation & loc )
     61                : ParseNode(loc) {}
     62
     63  private:
     64        StmtClause * clone() const override = 0;
    4965        MUTATE_FRIEND
    5066};
     
    158174  public:
    159175        ptr<Expr> cond;
     176        std::vector<ptr<CaseClause>> cases;
     177
     178        SwitchStmt( const CodeLocation & loc, const Expr * cond,
     179                                const std::vector<ptr<CaseClause>> && cases,
     180                                const std::vector<Label> && labels = {} )
     181                : Stmt(loc, std::move(labels)), cond(cond), cases(std::move(cases)) {}
     182
     183        const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
     184  private:
     185        SwitchStmt * clone() const override { return new SwitchStmt{ *this }; }
     186        MUTATE_FRIEND
     187};
     188
     189// Case label: case ...: or default:
     190class CaseClause final : public StmtClause {
     191  public:
     192        // Null for the default label.
     193        ptr<Expr> cond;
    160194        std::vector<ptr<Stmt>> stmts;
    161195
    162         SwitchStmt( const CodeLocation & loc, const Expr * cond, const std::vector<ptr<Stmt>> && stmts,
    163                                 const std::vector<Label> && labels = {} )
    164                 : Stmt(loc, std::move(labels)), cond(cond), stmts(std::move(stmts)) {}
    165 
    166         const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
    167   private:
    168         SwitchStmt * clone() const override { return new SwitchStmt{ *this }; }
    169         MUTATE_FRIEND
    170 };
    171 
    172 // Case label: case ...: or default:
    173 class CaseStmt final : public Stmt {
    174   public:
    175         // Null for the default label.
    176         ptr<Expr> cond;
    177         std::vector<ptr<Stmt>> stmts;
    178 
    179         CaseStmt( const CodeLocation & loc, const Expr * cond, const std::vector<ptr<Stmt>> && stmts,
    180                           const std::vector<Label> && labels = {} )
    181                 : Stmt(loc, std::move(labels)), cond(cond), stmts(std::move(stmts)) {}
     196        CaseClause( const CodeLocation & loc, const Expr * cond, const std::vector<ptr<Stmt>> && stmts )
     197                : StmtClause(loc), cond(cond), stmts(std::move(stmts)) {}
    182198
    183199        bool isDefault() const { return !cond; }
    184200
    185         const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
    186   private:
    187         CaseStmt * clone() const override { return new CaseStmt{ *this }; }
     201        const CaseClause * accept( Visitor & v ) const override { return v.visit( this ); }
     202  private:
     203        CaseClause * clone() const override { return new CaseClause{ *this }; }
    188204        MUTATE_FRIEND
    189205};
     
    298314  public:
    299315        ptr<CompoundStmt> body;
    300         std::vector<ptr<CatchStmt>> handlers;
    301         ptr<FinallyStmt> finally;
     316        std::vector<ptr<CatchClause>> handlers;
     317        ptr<FinallyClause> finally;
    302318
    303319        TryStmt( const CodeLocation & loc, const CompoundStmt * body,
    304                          const std::vector<ptr<CatchStmt>> && handlers, const FinallyStmt * finally,
     320                         const std::vector<ptr<CatchClause>> && handlers, const FinallyClause * finally,
    305321                         const std::vector<Label> && labels = {} )
    306322                : Stmt(loc, std::move(labels)), body(body), handlers(std::move(handlers)), finally(finally) {}
     
    313329
    314330// Catch clause of try statement
    315 class CatchStmt final : public Stmt {
     331class CatchClause final : public StmtClause {
    316332  public:
    317333        ptr<Decl> decl;
     
    320336        ExceptionKind kind;
    321337
    322         CatchStmt( const CodeLocation & loc, ExceptionKind kind, const Decl * decl, const Expr * cond,
    323                            const Stmt * body, const std::vector<Label> && labels = {} )
    324                 : Stmt(loc, std::move(labels)), decl(decl), cond(cond), body(body), kind(kind) {}
    325 
    326         const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
    327   private:
    328         CatchStmt * clone() const override { return new CatchStmt{ *this }; }
     338        CatchClause( const CodeLocation & loc, ExceptionKind kind, const Decl * decl, const Expr * cond,
     339                           const Stmt * body )
     340                : StmtClause(loc), decl(decl), cond(cond), body(body), kind(kind) {}
     341
     342        const CatchClause * accept( Visitor & v ) const override { return v.visit( this ); }
     343  private:
     344        CatchClause * clone() const override { return new CatchClause{ *this }; }
    329345        MUTATE_FRIEND
    330346};
    331347
    332348// Finally clause of try statement
    333 class FinallyStmt final : public Stmt {
     349class FinallyClause final : public StmtClause {
    334350  public:
    335351        ptr<CompoundStmt> body;
    336352
    337         FinallyStmt( const CodeLocation & loc, const CompoundStmt * body,
    338                                  std::vector<Label> && labels = {} )
    339                 : Stmt(loc, std::move(labels)), body(body) {}
    340 
    341         const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
    342   private:
    343         FinallyStmt * clone() const override { return new FinallyStmt{ *this }; }
     353        FinallyClause( const CodeLocation & loc, const CompoundStmt * body )
     354                : StmtClause(loc), body(body) {}
     355
     356        const FinallyClause * accept( Visitor & v ) const override { return v.visit( this ); }
     357  private:
     358        FinallyClause * clone() const override { return new FinallyClause{ *this }; }
    344359        MUTATE_FRIEND
    345360};
  • src/AST/Visitor.hpp

    rf5bace8 r400b8be  
    4141    virtual const ast::Stmt *             visit( const ast::ForStmt              * ) = 0;
    4242    virtual const ast::Stmt *             visit( const ast::SwitchStmt           * ) = 0;
    43     virtual const ast::Stmt *             visit( const ast::CaseStmt             * ) = 0;
     43    virtual const ast::CaseClause *       visit( const ast::CaseClause           * ) = 0;
    4444    virtual const ast::Stmt *             visit( const ast::BranchStmt           * ) = 0;
    4545    virtual const ast::Stmt *             visit( const ast::ReturnStmt           * ) = 0;
    4646    virtual const ast::Stmt *             visit( const ast::ThrowStmt            * ) = 0;
    4747    virtual const ast::Stmt *             visit( const ast::TryStmt              * ) = 0;
    48     virtual const ast::Stmt *             visit( const ast::CatchStmt            * ) = 0;
    49     virtual const ast::Stmt *             visit( const ast::FinallyStmt          * ) = 0;
     48    virtual const ast::CatchClause *      visit( const ast::CatchClause          * ) = 0;
     49    virtual const ast::FinallyClause *    visit( const ast::FinallyClause        * ) = 0;
    5050    virtual const ast::Stmt *             visit( const ast::SuspendStmt          * ) = 0;
    5151    virtual const ast::Stmt *             visit( const ast::WaitForStmt          * ) = 0;
Note: See TracChangeset for help on using the changeset viewer.