Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/AST/Convert.cpp

    r675d816 r19e567dd  
    1616#include "Convert.hpp"
    1717
    18 #include "AST/Pass.hpp"
    19 
    2018#include "AST/Attribute.hpp"
    2119#include "AST/Decl.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//================================================================================================
     
    413412        }
    414413
     414        TypeSubstitution * convertTypeSubstitution(const ast::TypeSubstitution * src) {
     415
     416                TypeSubstitution *rslt = new TypeSubstitution();
     417
     418                for (decltype(src->begin()) src_i = src->begin(); src_i != src->end(); src_i++) {
     419                        rslt->add( src_i->first,
     420                                   get<Type>().accept1(src_i->second) );
     421                }
     422
     423                for (decltype(src->beginVar()) src_i = src->beginVar(); src_i != src->endVar(); src_i++) {
     424                        rslt->addVar( src_i->first,
     425                                      get<Expression>().accept1(src_i->second) );
     426                }
     427
     428                return rslt;
     429        }
     430
     431        void convertInferUnion(std::map<UniqueId,ParamEntry> &tgtInferParams,
     432                                                   std::vector<UniqueId>         &tgtResnSlots,
     433                                                   const ast::Expr::InferUnion   &srcInferred ) {
     434
     435                assert( tgtInferParams.empty() );
     436                assert( tgtResnSlots.empty() );
     437
     438                if ( srcInferred.mode == ast::Expr::InferUnion::Params ) {
     439                        const ast::InferredParams &srcParams = srcInferred.inferParamsConst();
     440                        for (auto srcParam : srcParams) {
     441                                tgtInferParams[srcParam.first] = ParamEntry(
     442                                        srcParam.second.decl,
     443                                        get<Type>().accept1(srcParam.second.actualType),
     444                                        get<Type>().accept1(srcParam.second.formalType),
     445                                        get<Expression>().accept1(srcParam.second.expr)
     446                                );
     447                        }
     448                } else if ( srcInferred.mode == ast::Expr::InferUnion::Slots  ) {
     449                        const ast::ResnSlots &srcSlots = srcInferred.resnSlotsConst();
     450                        for (auto srcSlot : srcSlots) {
     451                                tgtResnSlots.push_back(srcSlot);
     452                        }
     453                }
     454        }
     455
     456        Expression * visitBaseExpr(const ast::Expr * src, Expression * tgt) {
     457
     458                tgt->location = src->location;
     459
     460                tgt->result = get<Type>().accept1(src->result);
     461                tgt->env    = convertTypeSubstitution(src->env);
     462
     463                tgt->extension = src->extension;
     464                convertInferUnion(tgt->inferParams, tgt->resnSlots, src->inferred);
     465
     466                return tgt;
     467        }
     468
    415469        const ast::Expr * visit( const ast::ApplicationExpr * node ) override final {
    416                 (void)node;
     470                auto expr = visitBaseExpr( node,
     471                        new ApplicationExpr(
     472                                get<Expression>().accept1(node->func),
     473                                get<Expression>().acceptL(node->args)
     474                        )
     475                );
     476                this->node = expr;
    417477                return nullptr;
    418478        }
    419479
    420480        const ast::Expr * visit( const ast::UntypedExpr * node ) override final {
    421                 (void)node;
     481                auto expr = visitBaseExpr( node,
     482                        new UntypedExpr(
     483                                get<Expression>().accept1(node->func),
     484                                get<Expression>().acceptL(node->args)
     485                        )
     486                );
     487                this->node = expr;
    422488                return nullptr;
    423489        }
    424490
    425491        const ast::Expr * visit( const ast::NameExpr * node ) override final {
    426                 (void)node;
     492                auto expr = visitBaseExpr( node,
     493                        new NameExpr(
     494                                node->name
     495                        )
     496                );
     497                this->node = expr;
    427498                return nullptr;
    428499        }
     
    11721243        }
    11731244
    1174         virtual void visit( ApplicationExpr * ) override final {
    1175 
    1176         }
    1177 
    1178         virtual void visit( UntypedExpr * ) override final {
    1179 
    1180         }
    1181 
    1182         virtual void visit( NameExpr * ) override final {
    1183 
    1184         }
    1185 
    1186         virtual void visit( CastExpr * ) override final {
    1187 
     1245        ast::TypeSubstitution * convertTypeSubstitution(const TypeSubstitution * old) {
     1246
     1247                ast::TypeSubstitution *rslt = new ast::TypeSubstitution();
     1248
     1249                for (decltype(old->begin()) old_i = old->begin(); old_i != old->end(); old_i++) {
     1250                        rslt->add( old_i->first,
     1251                                   getAccept1<ast::Type>(old_i->second) );
     1252                }
     1253
     1254                for (decltype(old->beginVar()) old_i = old->beginVar(); old_i != old->endVar(); old_i++) {
     1255                        rslt->addVar( old_i->first,
     1256                                      getAccept1<ast::Expr>(old_i->second) );
     1257                }
     1258
     1259                return rslt;
     1260        }
     1261
     1262        void convertInferUnion(ast::Expr::InferUnion               &newInferred,
     1263                                                   const std::map<UniqueId,ParamEntry> &oldInferParams,
     1264                                                   const std::vector<UniqueId>         &oldResnSlots) {
     1265
     1266                assert( oldInferParams.empty() || oldResnSlots.empty() );
     1267                assert( newInferred.mode == ast::Expr::InferUnion::Empty );
     1268
     1269                if ( !oldInferParams.empty() ) {
     1270                        ast::InferredParams &tgt = newInferred.inferParams();
     1271                        for (auto old : oldInferParams) {
     1272                                tgt[old.first] = ast::ParamEntry(
     1273                                        old.second.decl,
     1274                                        getAccept1<ast::Type>(old.second.actualType),
     1275                                        getAccept1<ast::Type>(old.second.formalType),
     1276                                        getAccept1<ast::Expr>(old.second.expr)
     1277                                );
     1278                        }
     1279                } else if ( !oldResnSlots.empty() ) {
     1280                        ast::ResnSlots &tgt = newInferred.resnSlots();
     1281                        for (auto old : oldResnSlots) {
     1282                                tgt.push_back(old);
     1283                        }
     1284                }
     1285        }
     1286
     1287        ast::Expr * visitBaseExpr(Expression * old, ast::Expr * nw) {
     1288
     1289                nw->result = GET_ACCEPT_1(result, Type);
     1290                nw->env    = convertTypeSubstitution(old->env);
     1291
     1292                nw->extension = old->extension;
     1293                convertInferUnion(nw->inferred, old->inferParams, old->resnSlots);
     1294
     1295                return nw;
     1296        }
     1297
     1298        virtual void visit( ApplicationExpr * old ) override final {
     1299                this->node = visitBaseExpr( old,
     1300                        new ast::ApplicationExpr(
     1301                                old->location,
     1302                                GET_ACCEPT_1(function, Expr),
     1303                                GET_ACCEPT_V(args, Expr)
     1304                        )
     1305                );
     1306        }
     1307
     1308        virtual void visit( UntypedExpr * old ) override final {
     1309                this->node = visitBaseExpr( old,
     1310                        new ast::UntypedExpr(
     1311                                old->location,
     1312                                GET_ACCEPT_1(function, Expr),
     1313                                GET_ACCEPT_V(args, Expr)
     1314                        )
     1315                );
     1316        }
     1317
     1318        virtual void visit( NameExpr * old ) override final {
     1319                this->node = visitBaseExpr( old,
     1320                        new ast::NameExpr(
     1321                                old->location,
     1322                                old->get_name()
     1323                        )
     1324                );
     1325        }
     1326
     1327        virtual void visit( CastExpr * old ) override final {
     1328                this->node = visitBaseExpr( old,
     1329                        new ast::CastExpr(
     1330                                old->location,
     1331                                nullptr, // cast's "to" type is expr's result type; converted in visitBaseExpr
     1332                                old->isGenerated ? ast::GeneratedCast : ast::ExplicitCast
     1333                        )
     1334                );
    11881335        }
    11891336
Note: See TracChangeset for help on using the changeset viewer.