1 #include <msp/core/raii.h>
2 #include <msp/strings/format.h>
11 Formatter::Formatter():
18 void Formatter::apply(Stage &s)
20 GLApi api = get_gl_api();
21 const Version &ver = s.required_version;
25 append(format("#version %d%02d", ver.major, ver.minor));
26 if(api==OPENGL_ES2 && ver>=Version(3, 0))
31 for(vector<const Extension *>::const_iterator i=s.required_extensions.begin(); i!=s.required_extensions.end(); ++i)
32 append(format("#extension %s: require\n", (*i)->get_name()));
33 if(!s.required_extensions.empty())
36 StageVisitor::apply(s);
39 void Formatter::append(const string &text)
42 for(string::const_iterator i=text.begin(); i!=text.end(); ++i)
47 void Formatter::append(char c)
54 void Formatter::set_source(unsigned index, unsigned line)
56 if(index!=source_index || (index && line!=source_line))
58 if(index==source_index && line==source_line+1)
63 if(stage->required_version<Version(3, 30))
65 formatted += format("#line %d %d\n", l, index);
72 void Formatter::visit(Literal &literal)
74 append(literal.token);
77 void Formatter::visit(ParenthesizedExpression &parexpr)
80 parexpr.expression->visit(*this);
84 void Formatter::visit(VariableReference &var)
89 void Formatter::visit(MemberAccess &memacc)
91 memacc.left->visit(*this);
92 append(format(".%s", memacc.member));
95 void Formatter::visit(UnaryExpression &unary)
99 unary.expression->visit(*this);
104 void Formatter::visit(BinaryExpression &binary)
106 binary.left->visit(*this);
108 binary.right->visit(*this);
109 append(binary.after);
112 void Formatter::visit(Assignment &assign)
114 assign.left->visit(*this);
115 append(format(" %s ", assign.oper));
116 assign.right->visit(*this);
119 void Formatter::visit(FunctionCall &call)
121 append(format("%s(", call.name));
122 for(NodeArray<Expression>::iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i)
124 if(i!=call.arguments.begin())
131 void Formatter::visit(ExpressionStatement &expr)
133 expr.expression->visit(*this);
137 void Formatter::visit(Block &block)
139 unsigned brace_indent = indent;
140 bool use_braces = (block.use_braces || (indent && block.body.size()!=1));
142 append(format("%s{\n", string(brace_indent*2, ' ')));
144 SetForScope<unsigned> set(indent, indent+(indent>0 || use_braces));
145 string spaces(indent*2, ' ');
146 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
148 if(i!=block.body.begin())
150 set_source((*i)->source, (*i)->line);
156 append(format("\n%s}", string(brace_indent*2, ' ')));
159 void Formatter::visit(Import &import)
161 append(format("import %s;", import.module));
164 void Formatter::visit(Precision &prec)
166 append(format("precision %s %s;", prec.precision, prec.type));
169 void Formatter::visit(Layout &layout)
172 for(vector<Layout::Qualifier>::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); ++i)
174 if(i!=layout.qualifiers.begin())
176 append(i->identifier);
177 if(!i->value.empty())
178 append(format("=%s", i->value));
183 void Formatter::visit(InterfaceLayout &layout)
185 layout.layout.visit(*this);
186 append(format(" %s;", layout.interface));
189 void Formatter::visit(StructDeclaration &strct)
191 append(format("struct %s\n", strct.name));
192 strct.members.visit(*this);
196 void Formatter::visit(VariableDeclaration &var)
200 var.layout->visit(*this);
205 if(!var.interpolation.empty())
206 append(format("%s ", var.interpolation));
207 if(!var.sampling.empty())
208 append(format("%s ", var.sampling));
209 if(!var.interface.empty() && var.interface!=block_interface)
211 string interface = var.interface;
212 if(stage->required_version<Version(1, 30))
214 if(stage->type==Stage::VERTEX && var.interface=="in")
215 interface = "attribute";
216 else if((stage->type==Stage::VERTEX && var.interface=="out") || (stage->type==Stage::FRAGMENT && var.interface=="in"))
217 interface = "varying";
219 append(format("%s ", interface));
221 if(!var.precision.empty())
222 append(format("%s ", var.precision));
223 append(format("%s %s", var.type, var.name));
228 var.array_size->visit(*this);
231 if(var.init_expression)
234 var.init_expression->visit(*this);
240 void Formatter::visit(InterfaceBlock &iface)
242 SetForScope<string> set(block_interface, iface.interface);
243 append(format("%s %s\n", iface.interface, iface.name));
244 iface.members.visit(*this);
248 void Formatter::visit(FunctionDeclaration &func)
250 append(format("%s %s(", func.return_type, func.name));
251 for(NodeArray<VariableDeclaration>::iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
253 if(i!=func.parameters.begin())
255 SetFlag set(parameter_list);
259 if(func.definition==&func)
262 func.body.visit(*this);
268 void Formatter::visit(Conditional &cond)
271 cond.condition->visit(*this);
274 cond.body.visit(*this);
275 if(!cond.else_body.body.empty())
277 Conditional *else_cond = dynamic_cast<Conditional *>(cond.else_body.body.front().get());
278 if(cond.else_body.body.size()==1 && else_cond)
281 set_source(else_cond->source, else_cond->line);
282 append(format("%selse ", string(indent*2, ' ')));
283 else_cond->visit(*this);
287 append(format("\n%selse\n", string(indent*2, ' ')));
288 cond.else_body.visit(*this);
293 void Formatter::visit(Iteration &iter)
295 if(!iter.init_statement && iter.condition && !iter.loop_expression)
298 iter.condition->visit(*this);
304 if(iter.init_statement)
305 iter.init_statement->visit(*this);
311 iter.condition->visit(*this);
314 if(iter.loop_expression)
317 iter.loop_expression->visit(*this);
322 if(iter.body.body.empty())
327 iter.body.visit(*this);
331 void Formatter::visit(Return &ret)
337 ret.expression->visit(*this);
342 void Formatter::visit(Jump &jump)
344 append(jump.keyword);