[d66a43b] | 1 | #include "TranslateEnumRange.hpp"
|
---|
[567c775] | 2 |
|
---|
| 3 | #include "AST/Pass.hpp"
|
---|
| 4 | #include "AST/TranslationUnit.hpp"
|
---|
| 5 |
|
---|
| 6 | namespace ControlStruct {
|
---|
| 7 |
|
---|
| 8 | struct addInit {
|
---|
| 9 | const ast::Stmt * postvisit( const ast::ForStmt * stmt );
|
---|
| 10 | };
|
---|
| 11 |
|
---|
| 12 | struct translateEnumRangeCore {
|
---|
| 13 | const ast::Stmt * postvisit( const ast::ForStmt * stmt );
|
---|
| 14 | };
|
---|
| 15 |
|
---|
| 16 | const ast::Stmt* addInit::postvisit( const ast::ForStmt * stmt ) {
|
---|
| 17 | if ( stmt->range_over ) {
|
---|
| 18 | auto inits = stmt->inits;
|
---|
| 19 | auto init = stmt->inits.front();
|
---|
| 20 |
|
---|
| 21 | if (auto declStmt = init.as<ast::DeclStmt>()) {
|
---|
| 22 | auto decl = declStmt->decl;
|
---|
| 23 | if ( auto objDecl = decl.as<ast::ObjectDecl>()) {
|
---|
| 24 | if ( !objDecl->init ) {
|
---|
| 25 | auto location = stmt->location;
|
---|
[73d0e3f4] | 26 | ast::SingleInit * newInit = new ast::SingleInit( location,
|
---|
[6d2b3dc] | 27 | stmt->is_inc?
|
---|
| 28 | ast::UntypedExpr::createCall( location, "lowerBound", {} ):
|
---|
| 29 | ast::UntypedExpr::createCall( location, "upperBound", {} ),
|
---|
| 30 | ast::ConstructFlag::MaybeConstruct
|
---|
| 31 | );
|
---|
[567c775] | 32 | auto objDeclWithInit = ast::mutate_field( objDecl, &ast::ObjectDecl::init, newInit );
|
---|
| 33 | auto declWithInit = ast::mutate_field( declStmt, &ast::DeclStmt::decl, objDeclWithInit );
|
---|
| 34 | stmt = ast::mutate_field_index( stmt, &ast::ForStmt::inits, 0, declWithInit );
|
---|
| 35 | }
|
---|
| 36 | }
|
---|
| 37 | }
|
---|
| 38 | }
|
---|
| 39 | return stmt;
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | const ast::Stmt* translateEnumRangeCore::postvisit( const ast::ForStmt * stmt ) {
|
---|
| 43 | if ( !stmt->range_over ) return stmt;
|
---|
| 44 | auto location = stmt->location;
|
---|
| 45 | auto declStmt = stmt->inits.front().strict_as<ast::DeclStmt>();
|
---|
| 46 | auto initDecl = declStmt->decl.strict_as<ast::ObjectDecl>();
|
---|
| 47 | auto indexName = initDecl->name;
|
---|
[73d0e3f4] | 48 |
|
---|
[d3aa55e9] | 49 | // Both inc and dec check if the current posn less than the number of enumerator
|
---|
| 50 | // for dec, it keeps call pred until it passes 0 (the first enumerator) and underflow,
|
---|
| 51 | // it wraps around and become unsigned max
|
---|
| 52 | ast::UntypedExpr * condition = ast::UntypedExpr::createCall( location,
|
---|
[5f210c0] | 53 | stmt->is_inc? "?<=?": "?>=?",
|
---|
[6d2b3dc] | 54 | { new ast::NameExpr( location, indexName ),
|
---|
[5f210c0] | 55 | stmt->is_inc?
|
---|
| 56 | ast::UntypedExpr::createCall( location, "upperBound", {} ):
|
---|
| 57 | ast::UntypedExpr::createCall( location, "lowerBound", {} )
|
---|
| 58 | });
|
---|
[d3aa55e9] | 59 | auto increment = ast::UntypedExpr::createCall( location,
|
---|
[0c327ce] | 60 | stmt->is_inc? "succ_unsafe": "pred_unsafe",
|
---|
[6d2b3dc] | 61 | { new ast::NameExpr( location, indexName ) });
|
---|
[567c775] | 62 | auto assig = ast::UntypedExpr::createAssign( location, new ast::NameExpr( location, indexName ), increment );
|
---|
| 63 | auto mut = ast::mutate_field( stmt, &ast::ForStmt::cond, condition );
|
---|
| 64 | mut = ast::mutate_field(stmt, &ast::ForStmt::inc, assig );
|
---|
| 65 | return mut;
|
---|
[a9ae5ca] | 66 | }
|
---|
[567c775] | 67 |
|
---|
| 68 | void translateEnumRange( ast::TranslationUnit & translationUnit ) {
|
---|
| 69 | ast::Pass<addInit>::run( translationUnit );
|
---|
| 70 | ast::Pass<translateEnumRangeCore>::run( translationUnit );
|
---|
| 71 | }
|
---|
[d66a43b] | 72 | }
|
---|