Changes in / [f6964ef:172d9342]


Ignore:
Location:
src/AST
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/AST/Convert.cpp

    rf6964ef r172d9342  
    1010// Created On       : Thu May 09 15::37::05 2019
    1111// Last Modified By : Andrew Beach
    12 // Last Modified On : Fri May 17 16:01:00 2019
    13 // Update Count     : 4
     12// Last Modified On : Fri May 17 12:13:00 2019
     13// Update Count     : 3
    1414//
    1515
     
    4141//================================================================================================
    4242class ConverterNewToOld : public ast::Visitor {
     43public:
     44        template<typename T>
     45        T * get() {
     46                T * ret = strict_dynamic_cast< T * >( node );
     47                node = nullptr;
     48                return ret;
     49        }
     50
     51private:
    4352        BaseSyntaxNode * node = nullptr;
    4453
    45         template<typename T>
    46         struct Getter {
    47                 ConverterNewToOld & visitor;
    48 
    49                 template<typename U>
    50                 T * accept1( const ast::ptr<U> & ptr ) {
    51                         ptr->accept( visitor );
    52                         T * ret = strict_dynamic_cast< T * >( visitor.node );
    53                         visitor.node = nullptr;
    54                         return ret;
    55                 }
    56 
    57                 template<typename U>
    58                 std::list< T * > acceptL( const U & container ) {
    59                         std::list< T * > ret;
    60                         for (auto ptr : container ) {
    61                                 ret.emplace_back( accept1( ptr ) );
    62                         }
    63                         return ret;
    64                 }
    65         };
    66 
    67     template<typename T>
    68     Getter<T> get() {
    69         return Getter<T>{ *this };
    70     }
    71 
    72         Label makeLabel(Statement * labelled, const ast::Label& label) {
    73                 return Label(
    74                         label.name,
    75                         labelled,
    76                         get<Attribute>().acceptL(label.attributes)
    77                 );
    78         }
    79 
    80         template<template <class...> class C>
    81         std::list<Label> makeLabelL(Statement * labelled, const C<ast::Label>& labels) {
    82                 std::list<Label> ret;
    83                 for (auto label : labels) {
    84                         ret.push_back( makeLabel(labelled, label) );
    85                 }
    86                 return ret;
    87         }
    88 
    89 public:
    90         Declaration * decl( const ast::Decl * declNode ) {
    91                 return get<Declaration>().accept1( ast::ptr<ast::Decl>( declNode ) );
    92         }
    93 
    94 private:
     54        // All the visit functions:
     55
    9556        const ast::DeclWithType * visit( const ast::ObjectDecl * node ) override final {
    9657                (void)node;
     
    144105
    145106        const ast::CompoundStmt * visit( const ast::CompoundStmt * node ) override final {
    146                 auto stmt = new CompoundStmt( get<Statement>().acceptL( node->kids ) );
    147                 stmt->location = node->location;
    148                 stmt->labels = makeLabelL( stmt, node->labels );
    149                 this->node = stmt;
     107                (void)node;
    150108                return nullptr;
    151109        }
    152110
    153111        const ast::Stmt * visit( const ast::ExprStmt * node ) override final {
    154                 auto stmt = new ExprStmt( get<Expression>().accept1( node->expr ) );
    155                 stmt->location = node->location;
    156                 stmt->labels = makeLabelL( stmt, node->labels );
    157                 this->node = stmt;
     112                (void)node;
    158113                return nullptr;
    159114        }
    160115
    161116        const ast::Stmt * visit( const ast::AsmStmt * node ) override final {
    162                 auto stmt = new AsmStmt(
    163                         node->isVolatile,
    164                         get<Expression>().accept1( node->instruction ),
    165                         get<Expression>().acceptL( node->output ),
    166                         get<Expression>().acceptL( node->input ),
    167                         get<ConstantExpr>().acceptL( node->clobber ),
    168                         makeLabelL( nullptr, node->gotoLabels ) // What are these labelling?
    169                 );
    170                 stmt->location = node->location;
    171                 stmt->labels = makeLabelL( stmt, node->labels );
    172                 this->node = stmt;
     117                (void)node;
    173118                return nullptr;
    174119        }
    175120
    176121        const ast::Stmt * visit( const ast::DirectiveStmt * node ) override final {
    177                 auto stmt = new DirectiveStmt( node->directive );
    178                 stmt->location = node->location;
    179                 stmt->labels = makeLabelL( stmt, node->labels );
    180                 this->node = stmt;
     122                (void)node;
    181123                return nullptr;
    182124        }
    183125
    184126        const ast::Stmt * visit( const ast::IfStmt * node ) override final {
    185                 auto stmt = new IfStmt(
    186                         get<Expression>().accept1( node->cond ),
    187                         get<Statement>().accept1( node->thenPart ),
    188                         get<Statement>().accept1( node->elsePart ),
    189                         get<Statement>().acceptL( node->inits )
    190                 );
    191                 stmt->location = node->location;
    192                 stmt->labels = makeLabelL( stmt, node->labels );
    193                 this->node = stmt;
     127                (void)node;
     128                return nullptr;
     129        }
     130
     131        const ast::Stmt * visit( const ast::WhileStmt * node ) override final {
     132                (void)node;
     133                return nullptr;
     134        }
     135
     136        const ast::Stmt * visit( const ast::ForStmt * node ) override final {
     137                (void)node;
    194138                return nullptr;
    195139        }
    196140
    197141        const ast::Stmt * visit( const ast::SwitchStmt * node ) override final {
    198                 auto stmt = new SwitchStmt(
    199                         get<Expression>().accept1( node->cond ),
    200                         get<Statement>().acceptL( node->stmts )
    201                 );
    202                 stmt->location = node->location;
    203                 stmt->labels = makeLabelL( stmt, node->labels );
    204                 this->node = stmt;
     142                (void)node;
    205143                return nullptr;
    206144        }
    207145
    208146        const ast::Stmt * visit( const ast::CaseStmt * node ) override final {
    209                 auto stmt = new CaseStmt(
    210                         get<Expression>().accept1( node->cond ),
    211                         get<Statement>().acceptL( node->stmts ),
    212                         node->isDefault()
    213                 );
    214                 stmt->location = node->location;
    215                 stmt->labels = makeLabelL( stmt, node->labels );
    216                 this->node = stmt;
    217                 return nullptr;
    218         }
    219 
    220         const ast::Stmt * visit( const ast::WhileStmt * node ) override final {
    221                 auto inits = get<Statement>().acceptL( node->inits );
    222                 auto stmt = new WhileStmt(
    223                         get<Expression>().accept1( node->cond ),
    224                         get<Statement>().accept1( node->body ),
    225                         inits,
    226                         node->isDoWhile
    227                 );
    228                 stmt->location = node->location;
    229                 stmt->labels = makeLabelL( stmt, node->labels );
    230                 this->node = stmt;
    231                 return nullptr;
    232         }
    233 
    234         const ast::Stmt * visit( const ast::ForStmt * node ) override final {
    235                 auto stmt = new ForStmt(
    236                         get<Statement>().acceptL( node->inits ),
    237                         get<Expression>().accept1( node->cond ),
    238                         get<Expression>().accept1( node->inc ),
    239                         get<Statement>().accept1( node->body )
    240                 );
    241                 stmt->location = node->location;
    242                 stmt->labels = makeLabelL( stmt, node->labels );
    243                 this->node = stmt;
     147                (void)node;
    244148                return nullptr;
    245149        }
    246150
    247151        const ast::Stmt * visit( const ast::BranchStmt * node ) override final {
    248                 BranchStmt * stmt;
    249                 if (node->computedTarget) {
    250                         stmt = new BranchStmt( get<Expression>().accept1( node->computedTarget ),
    251                                 BranchStmt::Goto );
    252                 } else {
    253                         BranchStmt::Type type;
    254                         switch (node->kind) {
    255                         #define CASE(n) \
    256                         case ast::BranchStmt::n: \
    257                                 type = BranchStmt::n; \
    258                                 break
    259                         CASE(Goto);
    260                         CASE(Break);
    261                         CASE(Continue);
    262                         CASE(FallThrough);
    263                         CASE(FallThroughDefault);
    264                         #undef CASE
    265                         default:
    266                                 assertf(false, "Invalid ast::BranchStmt::Kind: %d\n", node->kind);
    267                         }
    268 
    269                         // The labels here are also weird.
    270                         stmt = new BranchStmt( makeLabel( nullptr, node->originalTarget ), type );
    271                         stmt->target = makeLabel( stmt, node->target );
    272                 }
    273                 stmt->location = node->location;
    274                 stmt->labels = makeLabelL( stmt, node->labels );
    275                 this->node = stmt;
     152                (void)node;
    276153                return nullptr;
    277154        }
    278155
    279156        const ast::Stmt * visit( const ast::ReturnStmt * node ) override final {
    280                 auto stmt = new ReturnStmt( get<Expression>().accept1( node->expr ) );
    281                 stmt->location = node->location;
    282                 stmt->labels = makeLabelL( stmt, node->labels );
    283                 this->node = stmt;
     157                (void)node;
    284158                return nullptr;
    285159        }
    286160
    287161        const ast::Stmt * visit( const ast::ThrowStmt * node ) override final {
    288                 ThrowStmt::Kind kind;
    289                 switch (node->kind) {
    290                 case ast::ThrowStmt::Terminate:
    291                         kind = ThrowStmt::Terminate;
    292                         break;
    293                 case ast::ThrowStmt::Resume:
    294                         kind = ThrowStmt::Resume;
    295                         break;
    296                 default:
    297                         assertf(false, "Invalid ast::ThrowStmt::Kind: %d\n", node->kind);
    298                 }
    299                 auto stmt = new ThrowStmt(
    300                         kind,
    301                         get<Expression>().accept1( node->expr ),
    302                         get<Expression>().accept1( node->target )
    303                 );
    304                 stmt->location = node->location;
    305                 stmt->labels = makeLabelL( stmt, node->labels );
    306                 this->node = stmt;
     162                (void)node;
    307163                return nullptr;
    308164        }
    309165
    310166        const ast::Stmt * visit( const ast::TryStmt * node ) override final {
    311                 auto handlers = get<CatchStmt>().acceptL( node->handlers );
    312                 auto stmt = new TryStmt(
    313                         get<CompoundStmt>().accept1( node->body ),
    314                         handlers,
    315                         get<FinallyStmt>().accept1( node->finally )
    316                 );
    317                 stmt->location = node->location;
    318                 stmt->labels = makeLabelL( stmt, node->labels );
    319                 this->node = stmt;
     167                (void)node;
    320168                return nullptr;
    321169        }
    322170
    323171        const ast::Stmt * visit( const ast::CatchStmt * node ) override final {
    324                 CatchStmt::Kind kind;
    325                 switch (node->kind) {
    326                 case ast::CatchStmt::Terminate:
    327                         kind = CatchStmt::Terminate;
    328                         break;
    329                 case ast::CatchStmt::Resume:
    330                         kind = CatchStmt::Resume;
    331                         break;
    332                 default:
    333                         assertf(false, "Invalid ast::CatchStmt::Kind: %d\n", node->kind);
    334                 }
    335                 auto stmt = new CatchStmt(
    336                         kind,
    337                         get<Declaration>().accept1( node->decl ),
    338                         get<Expression>().accept1( node->cond ),
    339                         get<Statement>().accept1( node->body )
    340                 );
    341                 stmt->location = node->location;
    342                 stmt->labels = makeLabelL( stmt, node->labels );
    343                 this->node = stmt;
     172                (void)node;
    344173                return nullptr;
    345174        }
    346175
    347176        const ast::Stmt * visit( const ast::FinallyStmt * node ) override final {
    348                 auto stmt = new FinallyStmt( get<CompoundStmt>().accept1( node->body ) );
    349                 stmt->location = node->location;
    350                 stmt->labels = makeLabelL( stmt, node->labels );
    351                 this->node = stmt;
     177                (void)node;
    352178                return nullptr;
    353179        }
    354180
    355181        const ast::Stmt * visit( const ast::WaitForStmt * node ) override final {
    356                 auto stmt = new WaitForStmt;
    357                 stmt->clauses.reserve( node->clauses.size() );
    358                 for ( auto clause : node->clauses ) {
    359                         stmt->clauses.push_back({{
    360                                         get<Expression>().accept1( clause.target.function ),
    361                                         get<Expression>().acceptL( clause.target.arguments ),
    362                                 },
    363                                 get<Statement>().accept1( clause.stmt ),
    364                                 get<Expression>().accept1( clause.cond ),
    365                         });
    366                 }
    367                 stmt->timeout = {
    368                         get<Expression>().accept1( node->timeout.time ),
    369                         get<Statement>().accept1( node->timeout.stmt ),
    370                         get<Expression>().accept1( node->timeout.cond ),
    371                 };
    372                 stmt->orelse = {
    373                         get<Statement>().accept1( node->orElse.stmt ),
    374                         get<Expression>().accept1( node->orElse.cond ),
    375                 };
    376                 stmt->location = node->location;
    377                 stmt->labels = makeLabelL( stmt, node->labels );
    378                 this->node = stmt;
     182                (void)node;
    379183                return nullptr;
    380184        }
    381185
    382186        const ast::Stmt * visit( const ast::WithStmt * node ) override final {
    383                 auto stmt = new WithStmt(
    384                         get<Expression>().acceptL( node->exprs ),
    385                         get<Statement>().accept1( node->stmt )
    386                 );
    387                 stmt->location = node->location;
    388                 stmt->labels = makeLabelL( stmt, node->labels );
    389                 this->node = stmt;
     187                (void)node;
    390188                return nullptr;
    391189        }
    392190
    393191        const ast::NullStmt * visit( const ast::NullStmt * node ) override final {
    394                 auto stmt = new NullStmt();
    395                 stmt->location = node->location;
    396                 stmt->labels = makeLabelL( stmt, node->labels );
    397                 this->node = stmt;
     192                (void)node;
    398193                return nullptr;
    399194        }
    400195
    401196        const ast::Stmt * visit( const ast::DeclStmt * node ) override final {
    402                 auto stmt = new DeclStmt( get<Declaration>().accept1( node->decl ) );
    403                 stmt->location = node->location;
    404                 stmt->labels = makeLabelL( stmt, node->labels );
    405                 this->node = stmt;
     197                (void)node;
    406198                return nullptr;
    407199        }
     
    722514        std::list< Declaration * > decls;
    723515        for(auto d : translationUnit) {
    724                 decls.emplace_back( c.decl( d ) );
     516                d->accept( c );
     517                decls.emplace_back( c.get<Declaration>() );
    725518                delete d;
    726519        }
  • src/AST/Stmt.hpp

    rf6964ef r172d9342  
    1010// Created On       : Wed May  8 13:00:00 2019
    1111// Last Modified By : Andrew Beach
    12 // Last Modified On : Fri May 17 12:45:00 2019
    13 // Update Count     : 5
     12// Last Modified On : Fri May 17 10:03:00 2019
     13// Update Count     : 4
    1414//
    1515
     
    175175        : Stmt(loc, std::move(labels)), cond(cond), stmts(std::move(stmts)) {}
    176176
    177         bool isDefault() const { return !cond; }
     177        bool isDefault() { return !cond; }
    178178
    179179        const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
Note: See TracChangeset for help on using the changeset viewer.