1 #include <msp/core/raii.h>
10 void TraversingVisitor::visit(Block &block)
12 if(&block!=current_block)
14 SetForScope<Block *> set_block(current_block, &block);
15 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
19 void TraversingVisitor::visit(ParenthesizedExpression &parexpr)
21 parexpr.expression->visit(*this);
24 void TraversingVisitor::visit(MemberAccess &memacc)
26 memacc.left->visit(*this);
29 void TraversingVisitor::visit(UnaryExpression &unary)
31 unary.expression->visit(*this);
34 void TraversingVisitor::visit(BinaryExpression &binary)
36 binary.left->visit(*this);
37 binary.right->visit(*this);
40 void TraversingVisitor::visit(Assignment &assign)
42 visit(static_cast<BinaryExpression &>(assign));
45 void TraversingVisitor::visit(FunctionCall &call)
47 for(NodeArray<Expression>::iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i)
51 void TraversingVisitor::visit(ExpressionStatement &expr)
53 expr.expression->visit(*this);
56 void TraversingVisitor::visit(InterfaceLayout &layout)
58 layout.layout.visit(*this);
61 void TraversingVisitor::visit(StructDeclaration &strct)
63 strct.members.visit(*this);
66 void TraversingVisitor::visit(VariableDeclaration &var)
69 var.layout->visit(*this);
70 if(var.init_expression)
71 var.init_expression->visit(*this);
73 var.array_size->visit(*this);
76 void TraversingVisitor::visit(InterfaceBlock &iface)
78 iface.members.visit(*this);
81 void TraversingVisitor::visit(FunctionDeclaration &func)
84 SetForScope<Block *> set_block(current_block, &func.body);
85 for(NodeArray<VariableDeclaration>::iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
87 func.body.visit(*this);
90 void TraversingVisitor::visit(Conditional &cond)
92 cond.condition->visit(*this);
93 cond.body.visit(*this);
94 cond.else_body.visit(*this);
97 void TraversingVisitor::visit(Iteration &iter)
100 SetForScope<Block *> set_block(current_block, &iter.body);
101 if(iter.init_statement)
102 iter.init_statement->visit(*this);
104 iter.condition->visit(*this);
105 iter.body.visit(*this);
106 if(iter.loop_expression)
107 iter.loop_expression->visit(*this);
110 void TraversingVisitor::visit(Passthrough &pass)
113 pass.subscript->visit(*this);
116 void TraversingVisitor::visit(Return &ret)
119 ret.expression->visit(*this);
123 NodeRemover::NodeRemover():
126 recursive_remove(false)
129 void NodeRemover::apply(Stage &s, const set<Node *> &tr)
133 s.content.visit(*this);
137 void NodeRemover::remove_from_map(map<string, T *> &vars, const string &key, T &node)
139 typename map<string, T *>::iterator i = vars.find(key);
140 if(i!=vars.end() && i->second==&node)
144 void NodeRemover::visit(Block &block)
146 SetForScope<Block *> set_block(current_block, &block);
147 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); )
150 if(to_remove->count(i->get()))
151 block.body.erase(i++);
157 void NodeRemover::visit(StructDeclaration &strct)
159 if(to_remove->count(&strct))
160 remove_from_map(stage->types, strct.name, strct);
163 void NodeRemover::visit(VariableDeclaration &var)
165 if(recursive_remove || to_remove->count(&var))
167 remove_from_map(current_block->variables, var.name, var);
168 stage->locations.erase(var.name);
169 if(var.linked_declaration)
170 var.linked_declaration->linked_declaration = 0;
172 else if(var.init_expression && to_remove->count(var.init_expression.get()))
173 var.init_expression = 0;
176 void NodeRemover::visit(InterfaceBlock &iface)
178 if(to_remove->count(&iface))
180 remove_from_map(stage->interface_blocks, iface.name, iface);
181 if(!iface.instance_name.empty())
182 remove_from_map(stage->interface_blocks, iface.instance_name, iface);
184 SetFlag set_recursive(recursive_remove, recursive_remove || to_remove->count(&iface));
185 TraversingVisitor::visit(iface);
188 void NodeRemover::visit(FunctionDeclaration &func)
190 if(to_remove->count(&func))
191 remove_from_map(stage->functions, func.name, func);
192 TraversingVisitor::visit(func);
195 void NodeRemover::visit(Iteration &iter)
197 if(to_remove->count(iter.init_statement.get()))
198 iter.init_statement = 0;
199 TraversingVisitor::visit(iter);
203 NodeReorderer::NodeReorderer():
208 void NodeReorderer::apply(Stage &stage, Node &before, const set<Node *> &tr)
210 reorder_before = &before;
212 stage.content.visit(*this);
215 void NodeReorderer::visit(Block &block)
217 NodeList<Statement>::iterator insert_point = block.body.end();
218 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); )
221 if(insert_point!=block.body.end() && to_reorder->count(i->get()))
223 NodeList<Statement>::iterator j = i++;
224 block.body.splice(insert_point, block.body, j);
228 if(i->get()==reorder_before)