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(const RefPtr<Statement> &s: block.body)
19 void TraversingVisitor::visit(RefPtr<Expression> &expr)
24 void TraversingVisitor::visit(MemberAccess &memacc)
29 void TraversingVisitor::visit(Swizzle &swizzle)
34 void TraversingVisitor::visit(UnaryExpression &unary)
36 visit(unary.expression);
39 void TraversingVisitor::visit(BinaryExpression &binary)
45 void TraversingVisitor::visit(Assignment &assign)
51 void TraversingVisitor::visit(TernaryExpression &ternary)
53 visit(ternary.condition);
54 visit(ternary.true_expr);
55 visit(ternary.false_expr);
58 void TraversingVisitor::visit(FunctionCall &call)
60 for(RefPtr<Expression> &a: call.arguments)
64 void TraversingVisitor::visit(ExpressionStatement &expr)
66 visit(expr.expression);
69 void TraversingVisitor::visit(InterfaceLayout &layout)
71 layout.layout.visit(*this);
74 void TraversingVisitor::visit(StructDeclaration &strct)
76 strct.members.visit(*this);
79 void TraversingVisitor::visit(VariableDeclaration &var)
82 var.layout->visit(*this);
83 if(var.init_expression)
84 visit(var.init_expression);
86 visit(var.array_size);
89 void TraversingVisitor::visit(InterfaceBlock &iface)
92 iface.layout->visit(*this);
94 iface.members->visit(*this);
97 void TraversingVisitor::visit(FunctionDeclaration &func)
100 SetForScope<Block *> set_block(current_block, &func.body);
101 for(const RefPtr<VariableDeclaration> &p: func.parameters)
103 func.body.visit(*this);
106 void TraversingVisitor::visit(Conditional &cond)
108 visit(cond.condition);
109 cond.body.visit(*this);
110 cond.else_body.visit(*this);
113 void TraversingVisitor::visit(Iteration &iter)
116 SetForScope<Block *> set_block(current_block, &iter.body);
117 if(iter.init_statement)
118 iter.init_statement->visit(*this);
120 visit(iter.condition);
121 iter.body.visit(*this);
122 if(iter.loop_expression)
123 visit(iter.loop_expression);
126 void TraversingVisitor::visit(Passthrough &pass)
129 visit(pass.subscript);
132 void TraversingVisitor::visit(Return &ret)
135 visit(ret.expression);
139 void NodeRemover::apply(Stage &s, const set<Node *> &tr)
143 s.content.visit(*this);
147 void NodeRemover::remove_from_map(map<string, T *> &vars, const string &key, T &node)
149 auto i = vars.find(key);
150 if(i!=vars.end() && i->second==&node)
154 void NodeRemover::visit(Block &block)
156 SetForScope<Block *> set_block(current_block, &block);
157 for(auto i=block.body.begin(); i!=block.body.end(); )
160 if(to_remove->count(i->get()))
161 block.body.erase(i++);
167 void NodeRemover::visit(TypeDeclaration &type)
169 if(to_remove->count(&type))
170 remove_from_map(stage->types, type.name, type);
173 void NodeRemover::visit(VariableDeclaration &var)
175 if(recursive_remove || to_remove->count(&var))
177 remove_from_map(current_block->variables, var.name, var);
178 stage->locations.erase(var.name);
179 if(var.linked_declaration)
180 var.linked_declaration->linked_declaration = 0;
182 else if(var.init_expression && to_remove->count(var.init_expression.get()))
183 var.init_expression = 0;
186 void NodeRemover::visit(InterfaceBlock &iface)
188 if(to_remove->count(&iface))
190 remove_from_map(stage->interface_blocks, format("%s %s", iface.interface, iface.block_name), iface);
191 if(!iface.instance_name.empty())
192 remove_from_map(stage->interface_blocks, iface.instance_name, iface);
194 SetFlag set_recursive(recursive_remove, recursive_remove || to_remove->count(&iface));
195 TraversingVisitor::visit(iface);
198 void NodeRemover::visit(FunctionDeclaration &func)
200 if(to_remove->count(&func))
202 remove_from_map(stage->functions, func.name, func);
203 if(!func.signature.empty())
204 remove_from_map(stage->functions, func.name+func.signature, func);
206 TraversingVisitor::visit(func);
209 void NodeRemover::visit(Iteration &iter)
211 if(to_remove->count(iter.init_statement.get()))
212 iter.init_statement = 0;
213 TraversingVisitor::visit(iter);
217 void NodeReorderer::apply(Stage &stage, Node &before, const set<Node *> &tr)
219 reorder_before = &before;
221 stage.content.visit(*this);
224 void NodeReorderer::visit(Block &block)
226 auto insert_point = block.body.end();
227 for(auto i=block.body.begin(); i!=block.body.end(); )
230 if(insert_point!=block.body.end() && to_reorder->count(i->get()))
231 block.body.splice(insert_point, block.body, i++);
234 if(i->get()==reorder_before)