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 init = stmt->inits.front(); |
---|
29 | |
---|
30 | if (auto declStmt = init.as<ast::DeclStmt>()) { |
---|
31 | auto decl = declStmt->decl; |
---|
32 | if ( auto objDecl = decl.as<ast::ObjectDecl>()) { |
---|
33 | if ( !objDecl->type && type ) { |
---|
34 | auto objDeclWithType = ast::mutate_field( objDecl, &ast::ObjectDecl::type, type ); |
---|
35 | auto declWithType = ast::mutate_field( declStmt, &ast::DeclStmt::decl, objDeclWithType ); |
---|
36 | stmt = ast::mutate_field_index( stmt, &ast::ForStmt::inits, 0, declWithType ); |
---|
37 | } |
---|
38 | } |
---|
39 | } |
---|
40 | } |
---|
41 | return stmt; |
---|
42 | } |
---|
43 | |
---|
44 | const ast::Stmt* addInit::postvisit( const ast::ForStmt * stmt ) { |
---|
45 | if ( stmt->range_over ) { |
---|
46 | auto typeExpr = stmt->range_over.strict_as<ast::TypeExpr>(); |
---|
47 | auto type = typeExpr->type; |
---|
48 | auto inits = stmt->inits; |
---|
49 | auto enumInst = type.strict_as<ast::EnumInstType>(); |
---|
50 | auto enumDecl = enumInst->base; |
---|
51 | |
---|
52 | auto init = stmt->inits.front(); |
---|
53 | |
---|
54 | if (auto declStmt = init.as<ast::DeclStmt>()) { |
---|
55 | auto decl = declStmt->decl; |
---|
56 | if ( auto objDecl = decl.as<ast::ObjectDecl>()) { |
---|
57 | if ( !objDecl->init ) { |
---|
58 | auto location = stmt->location; |
---|
59 | auto newInit = new ast::SingleInit( location, new ast::NameExpr( location, enumDecl->members.front()->name ) ); |
---|
60 | auto objDeclWithInit = ast::mutate_field( objDecl, &ast::ObjectDecl::init, newInit ); |
---|
61 | auto declWithInit = ast::mutate_field( declStmt, &ast::DeclStmt::decl, objDeclWithInit ); |
---|
62 | stmt = ast::mutate_field_index( stmt, &ast::ForStmt::inits, 0, declWithInit ); |
---|
63 | } |
---|
64 | } |
---|
65 | } |
---|
66 | } |
---|
67 | return stmt; |
---|
68 | } |
---|
69 | |
---|
70 | const ast::Stmt* translateEnumRangeCore::postvisit( const ast::ForStmt * stmt ) { |
---|
71 | if ( !stmt->range_over ) return stmt; |
---|
72 | auto location = stmt->location; |
---|
73 | auto declStmt = stmt->inits.front().strict_as<ast::DeclStmt>(); |
---|
74 | auto initDecl = declStmt->decl.strict_as<ast::ObjectDecl>(); |
---|
75 | auto indexName = initDecl->name; |
---|
76 | |
---|
77 | auto typeExpr = stmt->range_over.strict_as<ast::TypeExpr>(); |
---|
78 | auto enumInst = typeExpr->type.strict_as<ast::EnumInstType>(); |
---|
79 | 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 | }); |
---|
89 | auto assig = ast::UntypedExpr::createAssign( location, new ast::NameExpr( location, indexName ), increment ); |
---|
90 | auto mut = ast::mutate_field( stmt, &ast::ForStmt::cond, condition ); |
---|
91 | mut = ast::mutate_field(stmt, &ast::ForStmt::inc, assig ); |
---|
92 | return mut; |
---|
93 | } |
---|
94 | |
---|
95 | void translateEnumRange( ast::TranslationUnit & translationUnit ) { |
---|
96 | ast::Pass<addInitType>::run( translationUnit ); |
---|
97 | ast::Pass<addInit>::run( translationUnit ); |
---|
98 | ast::Pass<translateEnumRangeCore>::run( translationUnit ); |
---|
99 | } |
---|
100 | } |
---|