1 #include <msp/core/raii.h>
10 void NodeVisitor::visit(Assignment &assign)
12 visit(static_cast<BinaryExpression &>(assign));
16 void TraversingVisitor::visit(Block &block)
18 if(&block!=current_block)
20 SetForScope<Block *> set_block(current_block, &block);
21 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
25 void TraversingVisitor::visit(ParenthesizedExpression &parexpr)
27 parexpr.expression->visit(*this);
30 void TraversingVisitor::visit(MemberAccess &memacc)
32 memacc.left->visit(*this);
35 void TraversingVisitor::visit(UnaryExpression &unary)
37 unary.expression->visit(*this);
40 void TraversingVisitor::visit(BinaryExpression &binary)
42 binary.left->visit(*this);
43 binary.right->visit(*this);
46 void TraversingVisitor::visit(FunctionCall &call)
48 for(NodeArray<Expression>::iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i)
52 void TraversingVisitor::visit(ExpressionStatement &expr)
54 expr.expression->visit(*this);
57 void TraversingVisitor::visit(InterfaceLayout &layout)
59 layout.layout.visit(*this);
62 void TraversingVisitor::visit(StructDeclaration &strct)
64 strct.members.visit(*this);
67 void TraversingVisitor::visit(VariableDeclaration &var)
70 var.layout->visit(*this);
71 if(var.init_expression)
72 var.init_expression->visit(*this);
74 var.array_size->visit(*this);
77 void TraversingVisitor::visit(InterfaceBlock &iface)
79 iface.members.visit(*this);
82 void TraversingVisitor::visit(FunctionDeclaration &func)
85 SetForScope<Block *> set_block(current_block, &func.body);
86 for(NodeArray<VariableDeclaration>::iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
88 func.body.visit(*this);
91 void TraversingVisitor::visit(Conditional &cond)
93 cond.condition->visit(*this);
94 cond.body.visit(*this);
95 cond.else_body.visit(*this);
98 void TraversingVisitor::visit(Iteration &iter)
101 SetForScope<Block *> set_block(current_block, &iter.body);
102 if(iter.init_statement)
103 iter.init_statement->visit(*this);
105 iter.condition->visit(*this);
106 if(iter.loop_expression)
107 iter.loop_expression->visit(*this);
108 iter.body.visit(*this);
111 void TraversingVisitor::visit(Passthrough &pass)
114 pass.subscript->visit(*this);
117 void TraversingVisitor::visit(Return &ret)
120 ret.expression->visit(*this);
124 NodeRemover::NodeRemover():
127 recursive_remove(false)
130 void NodeRemover::apply(Stage &s, const set<Node *> &tr)
134 s.content.visit(*this);
138 void NodeRemover::remove_from_map(map<string, T *> &vars, const string &key, T &node)
140 typename map<string, T *>::iterator i = vars.find(key);
141 if(i!=vars.end() && i->second==&node)
145 void NodeRemover::visit(Block &block)
147 SetForScope<Block *> set_block(current_block, &block);
148 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); )
151 if(to_remove->count(i->get()))
152 block.body.erase(i++);
158 void NodeRemover::visit(StructDeclaration &strct)
160 if(to_remove->count(&strct))
161 remove_from_map(stage->types, strct.name, strct);
164 void NodeRemover::visit(VariableDeclaration &var)
166 if(recursive_remove || to_remove->count(&var))
168 remove_from_map(current_block->variables, var.name, var);
169 stage->locations.erase(var.name);
170 if(var.linked_declaration)
171 var.linked_declaration->linked_declaration = 0;
173 else if(var.init_expression && to_remove->count(var.init_expression.get()))
174 var.init_expression = 0;
177 void NodeRemover::visit(InterfaceBlock &iface)
179 if(to_remove->count(&iface))
181 remove_from_map(stage->interface_blocks, iface.name, iface);
182 if(!iface.instance_name.empty())
183 remove_from_map(stage->interface_blocks, iface.instance_name, iface);
185 SetFlag set_recursive(recursive_remove, recursive_remove || to_remove->count(&iface));
186 TraversingVisitor::visit(iface);
189 void NodeRemover::visit(Iteration &iter)
191 if(to_remove->count(iter.init_statement.get()))
192 iter.init_statement = 0;
193 TraversingVisitor::visit(iter);