Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/AST/Convert.cpp

    r675d816 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
    1616#include "Convert.hpp"
    17 
    18 #include "AST/Pass.hpp"
    1917
    2018#include "AST/Attribute.hpp"
     
    2321#include "AST/Init.hpp"
    2422#include "AST/Stmt.hpp"
    25 
     23#include "AST/TypeSubstitution.hpp"
    2624
    2725#include "SynTree/Attribute.h"
    2826#include "SynTree/Declaration.h"
     27#include "SynTree/TypeSubstitution.h"
    2928
    3029//================================================================================================
     
    4241//================================================================================================
    4342class 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:
    4452        BaseSyntaxNode * node = nullptr;
    4553
    46         template<typename T>
    47         struct Getter {
    48                 ConverterNewToOld & visitor;
    49 
    50                 template<typename U>
    51                 T * accept1( const ast::ptr<U> & ptr ) {
    52                         ptr->accept( visitor );
    53                         T * ret = strict_dynamic_cast< T * >( visitor.node );
    54                         visitor.node = nullptr;
    55                         return ret;
    56                 }
    57 
    58                 template<typename U>
    59                 std::list< T * > acceptL( const U & container ) {
    60                         std::list< T * > ret;
    61                         for (auto ptr : container ) {
    62                                 ret.emplace_back( accept1( ptr ) );
    63                         }
    64                         return ret;
    65                 }
    66         };
    67 
    68     template<typename T>
    69     Getter<T> get() {
    70         return Getter<T>{ *this };
    71     }
    72 
    73         Label makeLabel(Statement * labelled, const ast::Label& label) {
    74                 return Label(
    75                         label.name,
    76                         labelled,
    77                         get<Attribute>().acceptL(label.attributes)
    78                 );
    79         }
    80 
    81         template<template <class...> class C>
    82         std::list<Label> makeLabelL(Statement * labelled, const C<ast::Label>& labels) {
    83                 std::list<Label> ret;
    84                 for (auto label : labels) {
    85                         ret.push_back( makeLabel(labelled, label) );
    86                 }
    87                 return ret;
    88         }
    89 
    90 public:
    91         Declaration * decl( const ast::Decl * declNode ) {
    92                 return get<Declaration>().accept1( ast::ptr<ast::Decl>( declNode ) );
    93         }
    94 
    95 private:
     54        // All the visit functions:
     55
    9656        const ast::DeclWithType * visit( const ast::ObjectDecl * node ) override final {
    9757                (void)node;
     
    145105
    146106        const ast::CompoundStmt * visit( const ast::CompoundStmt * node ) override final {
    147                 auto stmt = new CompoundStmt( get<Statement>().acceptL( node->kids ) );
    148                 stmt->location = node->location;
    149                 stmt->labels = makeLabelL( stmt, node->labels );
    150                 this->node = stmt;
     107                (void)node;
    151108                return nullptr;
    152109        }
    153110
    154111        const ast::Stmt * visit( const ast::ExprStmt * node ) override final {
    155                 auto stmt = new ExprStmt( get<Expression>().accept1( node->expr ) );
    156                 stmt->location = node->location;
    157                 stmt->labels = makeLabelL( stmt, node->labels );
    158                 this->node = stmt;
     112                (void)node;
    159113                return nullptr;
    160114        }
    161115
    162116        const ast::Stmt * visit( const ast::AsmStmt * node ) override final {
    163                 auto stmt = new AsmStmt(
    164                         node->isVolatile,
    165                         get<Expression>().accept1( node->instruction ),
    166                         get<Expression>().acceptL( node->output ),
    167                         get<Expression>().acceptL( node->input ),
    168                         get<ConstantExpr>().acceptL( node->clobber ),
    169                         makeLabelL( nullptr, node->gotoLabels ) // What are these labelling?
    170                 );
    171                 stmt->location = node->location;
    172                 stmt->labels = makeLabelL( stmt, node->labels );
    173                 this->node = stmt;
     117                (void)node;
    174118                return nullptr;
    175119        }
    176120
    177121        const ast::Stmt * visit( const ast::DirectiveStmt * node ) override final {
    178                 auto stmt = new DirectiveStmt( node->directive );
    179                 stmt->location = node->location;
    180                 stmt->labels = makeLabelL( stmt, node->labels );
    181                 this->node = stmt;
     122                (void)node;
    182123                return nullptr;
    183124        }
    184125
    185126        const ast::Stmt * visit( const ast::IfStmt * node ) override final {
    186                 auto stmt = new IfStmt(
    187                         get<Expression>().accept1( node->cond ),
    188                         get<Statement>().accept1( node->thenPart ),
    189                         get<Statement>().accept1( node->elsePart ),
    190                         get<Statement>().acceptL( node->inits )
    191                 );
    192                 stmt->location = node->location;
    193                 stmt->labels = makeLabelL( stmt, node->labels );
    194                 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;
    195138                return nullptr;
    196139        }
    197140
    198141        const ast::Stmt * visit( const ast::SwitchStmt * node ) override final {
    199                 auto stmt = new SwitchStmt(
    200                         get<Expression>().accept1( node->cond ),
    201                         get<Statement>().acceptL( node->stmts )
    202                 );
    203                 stmt->location = node->location;
    204                 stmt->labels = makeLabelL( stmt, node->labels );
    205                 this->node = stmt;
     142                (void)node;
    206143                return nullptr;
    207144        }
    208145
    209146        const ast::Stmt * visit( const ast::CaseStmt * node ) override final {
    210                 auto stmt = new CaseStmt(
    211                         get<Expression>().accept1( node->cond ),
    212                         get<Statement>().acceptL( node->stmts ),
    213                         node->isDefault()
    214                 );
    215                 stmt->location = node->location;
    216                 stmt->labels = makeLabelL( stmt, node->labels );
    217                 this->node = stmt;
    218                 return nullptr;
    219         }
    220 
    221         const ast::Stmt * visit( const ast::WhileStmt * node ) override final {
    222                 auto inits = get<Statement>().acceptL( node->inits );
    223                 auto stmt = new WhileStmt(
    224                         get<Expression>().accept1( node->cond ),
    225                         get<Statement>().accept1( node->body ),
    226                         inits,
    227                         node->isDoWhile
    228                 );
    229                 stmt->location = node->location;
    230                 stmt->labels = makeLabelL( stmt, node->labels );
    231                 this->node = stmt;
    232                 return nullptr;
    233         }
    234 
    235         const ast::Stmt * visit( const ast::ForStmt * node ) override final {
    236                 auto stmt = new ForStmt(
    237                         get<Statement>().acceptL( node->inits ),
    238                         get<Expression>().accept1( node->cond ),
    239                         get<Expression>().accept1( node->inc ),
    240                         get<Statement>().accept1( node->body )
    241                 );
    242                 stmt->location = node->location;
    243                 stmt->labels = makeLabelL( stmt, node->labels );
    244                 this->node = stmt;
     147                (void)node;
    245148                return nullptr;
    246149        }
    247150
    248151        const ast::Stmt * visit( const ast::BranchStmt * node ) override final {
    249                 BranchStmt * stmt;
    250                 if (node->computedTarget) {
    251                         stmt = new BranchStmt( get<Expression>().accept1( node->computedTarget ),
    252                                 BranchStmt::Goto );
    253                 } else {
    254                         BranchStmt::Type type;
    255                         switch (node->kind) {
    256                         #define CASE(n) \
    257                         case ast::BranchStmt::n: \
    258                                 type = BranchStmt::n; \
    259                                 break
    260                         CASE(Goto);
    261                         CASE(Break);
    262                         CASE(Continue);
    263                         CASE(FallThrough);
    264                         CASE(FallThroughDefault);
    265                         #undef CASE
    266                         default:
    267                                 assertf(false, "Invalid ast::BranchStmt::Kind: %d\n", node->kind);
    268                         }
    269 
    270                         // The labels here are also weird.
    271                         stmt = new BranchStmt( makeLabel( nullptr, node->originalTarget ), type );
    272                         stmt->target = makeLabel( stmt, node->target );
    273                 }
    274                 stmt->location = node->location;
    275                 stmt->labels = makeLabelL( stmt, node->labels );
    276                 this->node = stmt;
     152                (void)node;
    277153                return nullptr;
    278154        }
    279155
    280156        const ast::Stmt * visit( const ast::ReturnStmt * node ) override final {
    281                 auto stmt = new ReturnStmt( get<Expression>().accept1( node->expr ) );
    282                 stmt->location = node->location;
    283                 stmt->labels = makeLabelL( stmt, node->labels );
    284                 this->node = stmt;
     157                (void)node;
    285158                return nullptr;
    286159        }
    287160
    288161        const ast::Stmt * visit( const ast::ThrowStmt * node ) override final {
    289                 ThrowStmt::Kind kind;
    290                 switch (node->kind) {
    291                 case ast::ThrowStmt::Terminate:
    292                         kind = ThrowStmt::Terminate;
    293                         break;
    294                 case ast::ThrowStmt::Resume:
    295                         kind = ThrowStmt::Resume;
    296                         break;
    297                 default:
    298                         assertf(false, "Invalid ast::ThrowStmt::Kind: %d\n", node->kind);
    299                 }
    300                 auto stmt = new ThrowStmt(
    301                         kind,
    302                         get<Expression>().accept1( node->expr ),
    303                         get<Expression>().accept1( node->target )
    304                 );
    305                 stmt->location = node->location;
    306                 stmt->labels = makeLabelL( stmt, node->labels );
    307                 this->node = stmt;
     162                (void)node;
    308163                return nullptr;
    309164        }
    310165
    311166        const ast::Stmt * visit( const ast::TryStmt * node ) override final {
    312                 auto handlers = get<CatchStmt>().acceptL( node->handlers );
    313                 auto stmt = new TryStmt(
    314                         get<CompoundStmt>().accept1( node->body ),
    315                         handlers,
    316                         get<FinallyStmt>().accept1( node->finally )
    317                 );
    318                 stmt->location = node->location;
    319                 stmt->labels = makeLabelL( stmt, node->labels );
    320                 this->node = stmt;
     167                (void)node;
    321168                return nullptr;
    322169        }
    323170
    324171        const ast::Stmt * visit( const ast::CatchStmt * node ) override final {
    325                 CatchStmt::Kind kind;
    326                 switch (node->kind) {
    327                 case ast::CatchStmt::Terminate:
    328                         kind = CatchStmt::Terminate;
    329                         break;
    330                 case ast::CatchStmt::Resume:
    331                         kind = CatchStmt::Resume;
    332                         break;
    333                 default:
    334                         assertf(false, "Invalid ast::CatchStmt::Kind: %d\n", node->kind);
    335                 }
    336                 auto stmt = new CatchStmt(
    337                         kind,
    338                         get<Declaration>().accept1( node->decl ),
    339                         get<Expression>().accept1( node->cond ),
    340                         get<Statement>().accept1( node->body )
    341                 );
    342                 stmt->location = node->location;
    343                 stmt->labels = makeLabelL( stmt, node->labels );
    344                 this->node = stmt;
     172                (void)node;
    345173                return nullptr;
    346174        }
    347175
    348176        const ast::Stmt * visit( const ast::FinallyStmt * node ) override final {
    349                 auto stmt = new FinallyStmt( get<CompoundStmt>().accept1( node->body ) );
    350                 stmt->location = node->location;
    351                 stmt->labels = makeLabelL( stmt, node->labels );
    352                 this->node = stmt;
     177                (void)node;
    353178                return nullptr;
    354179        }
    355180
    356181        const ast::Stmt * visit( const ast::WaitForStmt * node ) override final {
    357                 auto stmt = new WaitForStmt;
    358                 stmt->clauses.reserve( node->clauses.size() );
    359                 for ( auto clause : node->clauses ) {
    360                         stmt->clauses.push_back({{
    361                                         get<Expression>().accept1( clause.target.function ),
    362                                         get<Expression>().acceptL( clause.target.arguments ),
    363                                 },
    364                                 get<Statement>().accept1( clause.stmt ),
    365                                 get<Expression>().accept1( clause.cond ),
    366                         });
    367                 }
    368                 stmt->timeout = {
    369                         get<Expression>().accept1( node->timeout.time ),
    370                         get<Statement>().accept1( node->timeout.stmt ),
    371                         get<Expression>().accept1( node->timeout.cond ),
    372                 };
    373                 stmt->orelse = {
    374                         get<Statement>().accept1( node->orElse.stmt ),
    375                         get<Expression>().accept1( node->orElse.cond ),
    376                 };
    377                 stmt->location = node->location;
    378                 stmt->labels = makeLabelL( stmt, node->labels );
    379                 this->node = stmt;
     182                (void)node;
    380183                return nullptr;
    381184        }
    382185
    383186        const ast::Stmt * visit( const ast::WithStmt * node ) override final {
    384                 auto stmt = new WithStmt(
    385                         get<Expression>().acceptL( node->exprs ),
    386                         get<Statement>().accept1( node->stmt )
    387                 );
    388                 stmt->location = node->location;
    389                 stmt->labels = makeLabelL( stmt, node->labels );
    390                 this->node = stmt;
     187                (void)node;
    391188                return nullptr;
    392189        }
    393190
    394191        const ast::NullStmt * visit( const ast::NullStmt * node ) override final {
    395                 auto stmt = new NullStmt();
    396                 stmt->location = node->location;
    397                 stmt->labels = makeLabelL( stmt, node->labels );
    398                 this->node = stmt;
     192                (void)node;
    399193                return nullptr;
    400194        }
    401195
    402196        const ast::Stmt * visit( const ast::DeclStmt * node ) override final {
    403                 auto stmt = new DeclStmt( get<Declaration>().accept1( node->decl ) );
    404                 stmt->location = node->location;
    405                 stmt->labels = makeLabelL( stmt, node->labels );
    406                 this->node = stmt;
     197                (void)node;
    407198                return nullptr;
    408199        }
     
    723514        std::list< Declaration * > decls;
    724515        for(auto d : translationUnit) {
    725                 decls.emplace_back( c.decl( d ) );
     516                d->accept( c );
     517                decls.emplace_back( c.get<Declaration>() );
    726518                delete d;
    727519        }
     
    1172964        }
    1173965
     966        ast::TypeSubstitution * convertTypeSubstitution(const TypeSubstitution * old) {
     967
     968                ast::TypeSubstitution *rslt = new ast::TypeSubstitution();
     969
     970                for (decltype(old->begin()) old_i = old->begin(); old_i != old->end(); old_i++) {
     971                        rslt->add( old_i->first,
     972                                   getAccept1<ast::Type>(old_i->second) );
     973                }
     974
     975                for (decltype(old->beginVar()) old_i = old->beginVar(); old_i != old->endVar(); old_i++) {
     976                        rslt->addVar( old_i->first,
     977                                      getAccept1<ast::Expr>(old_i->second) );
     978                }
     979        }
     980
     981        void convertInferUnion(ast::Expr::InferUnion &nwInferred, InferredParams oldInferParams, const std::vector<UniqueId> &oldResnSlots) {
     982               
     983                (void) nwInferred;
     984                (void) oldInferParams;
     985                (void) oldResnSlots;
     986               
     987                // TODO
     988        }
     989
     990        ast::Expr * visitBaseExpr(Expression * old, ast::Expr * nw) {
     991
     992                nw->result = GET_ACCEPT_1(result, Type);
     993                nw->env    = convertTypeSubstitution(old->env);
     994
     995                nw->extension = old->extension;
     996                convertInferUnion(nw->inferred, old->inferParams, old->resnSlots);
     997
     998                return nw;
     999        }
     1000
    11741001        virtual void visit( ApplicationExpr * ) override final {
    1175 
     1002                // TODO
    11761003        }
    11771004
    11781005        virtual void visit( UntypedExpr * ) override final {
    1179 
    1180         }
    1181 
    1182         virtual void visit( NameExpr * ) override final {
    1183 
     1006                // TODO
     1007        }
     1008
     1009        virtual void visit( NameExpr * old ) override final {
     1010                this->node = visitBaseExpr( old,
     1011                        new ast::NameExpr(
     1012                                old->location,
     1013                                old->get_name()
     1014                        )
     1015                );
    11841016        }
    11851017
    11861018        virtual void visit( CastExpr * ) override final {
    1187 
     1019                // TODO ... (rest)
    11881020        }
    11891021
Note: See TracChangeset for help on using the changeset viewer.