Changeset d3aa55e9 for src/ControlStruct/TranslateEnumRange.cpp
- Timestamp:
- Jun 27, 2024, 4:42:01 PM (8 weeks ago)
- Branches:
- master
- Children:
- 7552fde
- Parents:
- d5efcb7
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/ControlStruct/TranslateEnumRange.cpp
rd5efcb7 rd3aa55e9 26 26 auto enumDecl = enumInst->base; 27 27 28 auto init = stmt->inits.front();28 auto objInit = stmt->inits.front(); 29 29 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>()) { 31 35 auto decl = declStmt->decl; 32 36 if ( auto objDecl = decl.as<ast::ObjectDecl>()) { … … 57 61 if ( !objDecl->init ) { 58 62 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 ) ); 60 67 auto objDeclWithInit = ast::mutate_field( objDecl, &ast::ObjectDecl::init, newInit ); 61 68 auto declWithInit = ast::mutate_field( declStmt, &ast::DeclStmt::decl, objDeclWithInit ); … … 78 85 auto enumInst = typeExpr->type.strict_as<ast::EnumInstType>(); 79 86 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 ) }); 89 100 auto assig = ast::UntypedExpr::createAssign( location, new ast::NameExpr( location, indexName ), increment ); 90 101 auto mut = ast::mutate_field( stmt, &ast::ForStmt::cond, condition );
Note: See TracChangeset
for help on using the changeset viewer.