1 | #include "TranslateEnumRange.hpp"
|
---|
2 |
|
---|
3 | #include "AST/Pass.hpp"
|
---|
4 | #include "AST/TranslationUnit.hpp"
|
---|
5 |
|
---|
6 | namespace ControlStruct {
|
---|
7 |
|
---|
8 | struct addInitType {
|
---|
9 | const ast::Stmt * postvisit( const ast::ForStmt * stmt );
|
---|
10 | };
|
---|
11 |
|
---|
12 | struct addInit {
|
---|
13 | const ast::Stmt * postvisit( const ast::ForStmt * stmt );
|
---|
14 | };
|
---|
15 |
|
---|
16 | struct translateEnumRangeCore {
|
---|
17 | const ast::Stmt * postvisit( const ast::ForStmt * stmt );
|
---|
18 | };
|
---|
19 |
|
---|
20 | const 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 |
|
---|
28 | auto objInit = stmt->inits.front();
|
---|
29 | auto initLocation = objInit->location;
|
---|
30 | auto rangeLocation = stmt->range_over->location;
|
---|
31 | assert( stmt->inits.size() == 1 );
|
---|
32 |
|
---|
33 | if (auto declStmt = objInit.as<ast::DeclStmt>()) {
|
---|
34 | auto decl = declStmt->decl;
|
---|
35 | if ( auto objDecl = decl.as<ast::ObjectDecl>()) {
|
---|
36 | if ( !objDecl->type && type ) {
|
---|
37 | auto objDeclWithType = ast::mutate_field( objDecl, &ast::ObjectDecl::type, type );
|
---|
38 | auto declWithType = ast::mutate_field( declStmt, &ast::DeclStmt::decl, objDeclWithType );
|
---|
39 | stmt = ast::mutate_field_index( stmt, &ast::ForStmt::inits, 0, declWithType );
|
---|
40 | }
|
---|
41 | }
|
---|
42 | }
|
---|
43 | }
|
---|
44 | return stmt;
|
---|
45 | }
|
---|
46 |
|
---|
47 | const ast::Stmt* addInit::postvisit( const ast::ForStmt * stmt ) {
|
---|
48 | if ( stmt->range_over ) {
|
---|
49 | auto typeExpr = stmt->range_over.strict_as<ast::TypeExpr>();
|
---|
50 | auto type = typeExpr->type;
|
---|
51 | auto inits = stmt->inits;
|
---|
52 | auto enumInst = type.strict_as<ast::EnumInstType>();
|
---|
53 | auto enumDecl = enumInst->base;
|
---|
54 |
|
---|
55 | auto init = stmt->inits.front();
|
---|
56 |
|
---|
57 | if (auto declStmt = init.as<ast::DeclStmt>()) {
|
---|
58 | auto decl = declStmt->decl;
|
---|
59 | if ( auto objDecl = decl.as<ast::ObjectDecl>()) {
|
---|
60 | if ( !objDecl->init ) {
|
---|
61 | auto location = stmt->location;
|
---|
62 | // auto newInit = new ast::SingleInit( location, new ast::NameExpr( location, enumDecl->members.front()->name ) );
|
---|
63 | ast::SingleInit * newInit = new ast::SingleInit( location,
|
---|
64 | new ast::NameExpr( location,
|
---|
65 | stmt->is_inc? enumDecl->members.front()->name: enumDecl->members.back()->name ) );
|
---|
66 | auto objDeclWithInit = ast::mutate_field( objDecl, &ast::ObjectDecl::init, newInit );
|
---|
67 | auto declWithInit = ast::mutate_field( declStmt, &ast::DeclStmt::decl, objDeclWithInit );
|
---|
68 | stmt = ast::mutate_field_index( stmt, &ast::ForStmt::inits, 0, declWithInit );
|
---|
69 | }
|
---|
70 | }
|
---|
71 | }
|
---|
72 | }
|
---|
73 | return stmt;
|
---|
74 | }
|
---|
75 |
|
---|
76 | const ast::Stmt* translateEnumRangeCore::postvisit( const ast::ForStmt * stmt ) {
|
---|
77 | if ( !stmt->range_over ) return stmt;
|
---|
78 | auto location = stmt->location;
|
---|
79 | auto declStmt = stmt->inits.front().strict_as<ast::DeclStmt>();
|
---|
80 | auto initDecl = declStmt->decl.strict_as<ast::ObjectDecl>();
|
---|
81 | auto indexName = initDecl->name;
|
---|
82 |
|
---|
83 | auto typeExpr = stmt->range_over.strict_as<ast::TypeExpr>();
|
---|
84 | auto enumInst = typeExpr->type.strict_as<ast::EnumInstType>();
|
---|
85 | auto enumDecl = enumInst->base;
|
---|
86 |
|
---|
87 | // Both inc and dec check if the current posn less than the number of enumerator
|
---|
88 | // for dec, it keeps call pred until it passes 0 (the first enumerator) and underflow,
|
---|
89 | // it wraps around and become unsigned max
|
---|
90 | ast::UntypedExpr * condition = ast::UntypedExpr::createCall( location,
|
---|
91 | "?<=?",
|
---|
92 | {new ast::CastExpr(location,
|
---|
93 | new ast::NameExpr( location, indexName ),
|
---|
94 | new ast::BasicType( ast::BasicKind::UnsignedInt
|
---|
95 | ),ast::GeneratedFlag::ExplicitCast),
|
---|
96 | ast::ConstantExpr::from_ulong( location, enumDecl->members.size()-1 ) });
|
---|
97 | auto increment = ast::UntypedExpr::createCall( location,
|
---|
98 | stmt->is_inc? "succ": "pred",{
|
---|
99 | new ast::NameExpr( location, indexName )
|
---|
100 | });
|
---|
101 | auto assig = ast::UntypedExpr::createAssign( location, new ast::NameExpr( location, indexName ), increment );
|
---|
102 | auto mut = ast::mutate_field( stmt, &ast::ForStmt::cond, condition );
|
---|
103 | mut = ast::mutate_field(stmt, &ast::ForStmt::inc, assig );
|
---|
104 | return mut;
|
---|
105 | }
|
---|
106 |
|
---|
107 | void translateEnumRange( ast::TranslationUnit & translationUnit ) {
|
---|
108 | ast::Pass<addInitType>::run( translationUnit );
|
---|
109 | ast::Pass<addInit>::run( translationUnit );
|
---|
110 | ast::Pass<translateEnumRangeCore>::run( translationUnit );
|
---|
111 | }
|
---|
112 | }
|
---|