Ignore:
Timestamp:
Jun 27, 2024, 4:42:01 PM (8 weeks ago)
Author:
JiadaL <j82liang@…>
Branches:
master
Children:
7552fde
Parents:
d5efcb7
Message:
  1. Disallow implicit conversion from cfa enum to int during on the function call site; 2. implement the reverse enum loop
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/ControlStruct/TranslateEnumRange.cpp

    rd5efcb7 rd3aa55e9  
    2626        auto enumDecl = enumInst->base;
    2727
    28         auto init = stmt->inits.front();
     28        auto objInit = stmt->inits.front();
    2929
    30         if (auto declStmt = init.as<ast::DeclStmt>()) {
     30        auto initLocation = objInit->location;
     31        auto rangeLocation = stmt->range_over->location;
     32        assert( stmt->inits.size() == 1 );
     33
     34        if (auto declStmt = objInit.as<ast::DeclStmt>()) {
    3135            auto decl = declStmt->decl;
    3236            if ( auto objDecl = decl.as<ast::ObjectDecl>()) {
     
    5761                if ( !objDecl->init ) {
    5862                    auto location = stmt->location;
    59                     auto newInit = new ast::SingleInit( location, new ast::NameExpr( location, enumDecl->members.front()->name ) );
     63                    ast::SingleInit * newInit;
     64                    newInit = new ast::SingleInit( location,
     65                        new ast::NameExpr( location,
     66                        stmt->is_inc? enumDecl->members.front()->name: enumDecl->members.back()->name ) );
    6067                    auto objDeclWithInit = ast::mutate_field( objDecl, &ast::ObjectDecl::init, newInit );
    6168                    auto declWithInit = ast::mutate_field( declStmt, &ast::DeclStmt::decl, objDeclWithInit );
     
    7885    auto enumInst = typeExpr->type.strict_as<ast::EnumInstType>();
    7986    auto enumDecl = enumInst->base;
    80 
    81     auto condition = ast::UntypedExpr::createCall( location, "?<=?", {
    82         new ast::NameExpr( location, indexName ),
    83         // ast::ConstantExpr::from_ulong( location, enumDecl->members.size() )
    84         new ast::NameExpr( location, enumDecl->members.back()->name )
    85     });
    86     auto increment = ast::UntypedExpr::createCall( location, "succ", {
    87         new ast::NameExpr( location, indexName )
    88     });
     87   
     88    // Both inc and dec check if the current posn less than the number of enumerator
     89    // for dec, it keeps call pred until it passes 0 (the first enumerator) and underflow,
     90    // it wraps around and become unsigned max
     91    ast::UntypedExpr * condition = ast::UntypedExpr::createCall( location,
     92        "?<=?",
     93        {
     94            ast::UntypedExpr::createCall(location, "posn", { new ast::NameExpr( location, indexName ) } ),
     95            ast::ConstantExpr::from_ulong( location, enumDecl->members.size()-1 )
     96        });
     97    auto increment = ast::UntypedExpr::createCall( location,
     98        stmt->is_inc? "succ": "pred",
     99        { new ast::NameExpr( location, indexName ) });
    89100    auto assig = ast::UntypedExpr::createAssign( location, new ast::NameExpr( location, indexName ), increment );
    90101    auto mut = ast::mutate_field( stmt, &ast::ForStmt::cond, condition );
Note: See TracChangeset for help on using the changeset viewer.