source: src/ControlStruct/TranslateEnumRange.cpp@ 7552fde

Last change on this file since 7552fde was d3aa55e9, checked in by JiadaL <j82liang@…>, 15 months ago
  1. Disallow implicit conversion from cfa enum to int during on the function call site; 2. implement the reverse enum loop
  • Property mode set to 100644
File size: 4.4 KB
RevLine 
[d66a43b]1#include "TranslateEnumRange.hpp"
[567c775]2
3#include "AST/Pass.hpp"
4#include "AST/TranslationUnit.hpp"
5
6namespace ControlStruct {
7
8struct addInitType {
9 const ast::Stmt * postvisit( const ast::ForStmt * stmt );
10};
11
12struct addInit {
13 const ast::Stmt * postvisit( const ast::ForStmt * stmt );
14};
15
16struct translateEnumRangeCore {
17 const ast::Stmt * postvisit( const ast::ForStmt * stmt );
18};
19
20const ast::Stmt* addInitType::postvisit( const ast::ForStmt * stmt ) {
21 if ( stmt->range_over ) {
22 auto typeExpr = stmt->range_over.strict_as<ast::TypeExpr>();
23 auto type = typeExpr->type;
24 auto inits = stmt->inits;
25 auto enumInst = type.strict_as<ast::EnumInstType>();
26 auto enumDecl = enumInst->base;
27
[d3aa55e9]28 auto objInit = stmt->inits.front();
[567c775]29
[d3aa55e9]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>()) {
[567c775]35 auto decl = declStmt->decl;
36 if ( auto objDecl = decl.as<ast::ObjectDecl>()) {
37 if ( !objDecl->type && type ) {
38 auto objDeclWithType = ast::mutate_field( objDecl, &ast::ObjectDecl::type, type );
39 auto declWithType = ast::mutate_field( declStmt, &ast::DeclStmt::decl, objDeclWithType );
40 stmt = ast::mutate_field_index( stmt, &ast::ForStmt::inits, 0, declWithType );
41 }
42 }
43 }
44 }
45 return stmt;
46}
47
48const ast::Stmt* addInit::postvisit( const ast::ForStmt * stmt ) {
49 if ( stmt->range_over ) {
50 auto typeExpr = stmt->range_over.strict_as<ast::TypeExpr>();
51 auto type = typeExpr->type;
52 auto inits = stmt->inits;
53 auto enumInst = type.strict_as<ast::EnumInstType>();
54 auto enumDecl = enumInst->base;
55
56 auto init = stmt->inits.front();
57
58 if (auto declStmt = init.as<ast::DeclStmt>()) {
59 auto decl = declStmt->decl;
60 if ( auto objDecl = decl.as<ast::ObjectDecl>()) {
61 if ( !objDecl->init ) {
62 auto location = stmt->location;
[d3aa55e9]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 ) );
[567c775]67 auto objDeclWithInit = ast::mutate_field( objDecl, &ast::ObjectDecl::init, newInit );
68 auto declWithInit = ast::mutate_field( declStmt, &ast::DeclStmt::decl, objDeclWithInit );
69 stmt = ast::mutate_field_index( stmt, &ast::ForStmt::inits, 0, declWithInit );
70 }
71 }
72 }
73 }
74 return stmt;
75}
76
77const ast::Stmt* translateEnumRangeCore::postvisit( const ast::ForStmt * stmt ) {
78 if ( !stmt->range_over ) return stmt;
79 auto location = stmt->location;
80 auto declStmt = stmt->inits.front().strict_as<ast::DeclStmt>();
81 auto initDecl = declStmt->decl.strict_as<ast::ObjectDecl>();
82 auto indexName = initDecl->name;
83
84 auto typeExpr = stmt->range_over.strict_as<ast::TypeExpr>();
85 auto enumInst = typeExpr->type.strict_as<ast::EnumInstType>();
86 auto enumDecl = enumInst->base;
[d3aa55e9]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 ) });
[567c775]100 auto assig = ast::UntypedExpr::createAssign( location, new ast::NameExpr( location, indexName ), increment );
101 auto mut = ast::mutate_field( stmt, &ast::ForStmt::cond, condition );
102 mut = ast::mutate_field(stmt, &ast::ForStmt::inc, assig );
103 return mut;
104}
105
106void translateEnumRange( ast::TranslationUnit & translationUnit ) {
107 ast::Pass<addInitType>::run( translationUnit );
108 ast::Pass<addInit>::run( translationUnit );
109 ast::Pass<translateEnumRangeCore>::run( translationUnit );
110}
[d66a43b]111}
Note: See TracBrowser for help on using the repository browser.