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(FunctionDeclaration &func)
92 SetForScope<Block *> set_block(current_block, &func.body);
93 for(const RefPtr<VariableDeclaration> &p: func.parameters)
95 func.body.visit(*this);
98 void TraversingVisitor::visit(Conditional &cond)
100 visit(cond.condition);
101 cond.body.visit(*this);
102 cond.else_body.visit(*this);
105 void TraversingVisitor::visit(Iteration &iter)
108 SetForScope<Block *> set_block(current_block, &iter.body);
109 if(iter.init_statement)
110 iter.init_statement->visit(*this);
112 visit(iter.condition);
113 iter.body.visit(*this);
114 if(iter.loop_expression)
115 visit(iter.loop_expression);
118 void TraversingVisitor::visit(Passthrough &pass)
121 visit(pass.subscript);
124 void TraversingVisitor::visit(Return &ret)
127 visit(ret.expression);
131 void NodeRemover::apply(Stage &s, const set<Node *> &tr)
135 s.content.visit(*this);
139 void NodeRemover::remove_from_map(map<string, T *> &vars, const string &key, T &node)
141 auto i = vars.find(key);
142 if(i!=vars.end() && i->second==&node)
146 void NodeRemover::visit(Block &block)
148 SetForScope<Block *> set_block(current_block, &block);
149 for(auto i=block.body.begin(); i!=block.body.end(); )
152 if(to_remove->count(i->get()))
153 block.body.erase(i++);
159 void NodeRemover::visit(TypeDeclaration &type)
161 if(to_remove->count(&type))
162 remove_from_map(stage->types, type.name, type);
165 void NodeRemover::visit(StructDeclaration &strct)
167 if(to_remove->count(&strct))
169 remove_from_map<TypeDeclaration>(stage->types, strct.name, strct);
170 if(strct.block_declaration)
172 string key = format("%s %s", strct.block_declaration->interface, strct.block_name);
173 remove_from_map(stage->interface_blocks, key, *strct.block_declaration);
174 remove_from_map(stage->interface_blocks, strct.block_declaration->name, *strct.block_declaration);
175 strct.block_declaration->block_declaration = 0;
180 void NodeRemover::visit(VariableDeclaration &var)
182 if(to_remove->count(&var))
184 remove_from_map(current_block->variables, var.name, var);
185 if(var.block_declaration)
187 remove_from_map(stage->interface_blocks, format("%s %s", var.interface, var.block_declaration->block_name), var);
188 remove_from_map(stage->interface_blocks, var.name, var);
189 var.block_declaration->block_declaration = 0;
191 stage->locations.erase(var.name);
192 if(var.linked_declaration)
193 var.linked_declaration->linked_declaration = 0;
195 else if(var.init_expression && to_remove->count(var.init_expression.get()))
196 var.init_expression = 0;
199 void NodeRemover::visit(FunctionDeclaration &func)
201 if(to_remove->count(&func))
203 remove_from_map(stage->functions, func.name, func);
204 if(!func.signature.empty())
205 remove_from_map(stage->functions, func.name+func.signature, func);
207 TraversingVisitor::visit(func);
210 void NodeRemover::visit(Iteration &iter)
212 if(to_remove->count(iter.init_statement.get()))
213 iter.init_statement = 0;
214 TraversingVisitor::visit(iter);
218 void NodeReorderer::apply(Stage &stage, Node &before, const set<Node *> &tr)
220 reorder_before = &before;
222 stage.content.visit(*this);
225 void NodeReorderer::visit(Block &block)
227 auto insert_point = block.body.end();
228 for(auto i=block.body.begin(); i!=block.body.end(); )
231 if(insert_point!=block.body.end() && to_reorder->count(i->get()))
232 block.body.splice(insert_point, block.body, i++);
235 if(i->get()==reorder_before)