1 #include <msp/core/raii.h>
2 #include <msp/strings/format.h>
11 Formatter::Formatter():
16 parameter_list(false),
20 const string &Formatter::apply(Stage &s)
25 const Version &ver = s.required_features.glsl_version;
29 append(format("#version %d%02d", ver.major, ver.minor));
30 if(s.required_features.gl_api==OPENGL_ES2 && ver>=Version(3, 0))
35 if(s.required_features.arb_explicit_attrib_location)
36 append("#extension arb_explicit_attrib_location: require\n");
37 if(s.required_features.arb_gpu_shader5)
38 append("#extension arb_gpu_shader5: require\n");
39 if(s.required_features.arb_uniform_buffer_object)
40 append("#extension arb_uniform_buffer_object: require\n");
41 if(s.required_features.ext_gpu_shader4)
42 append("#extension ext_gpu_shader4: require\n");
43 if(s.required_features.ext_texture_array)
44 append("#extension ext_texture_array: require\n");
47 s.content.visit(*this);
52 void Formatter::append(const string &text)
55 for(string::const_iterator i=text.begin(); i!=text.end(); ++i)
60 void Formatter::append(char c)
67 void Formatter::set_source(unsigned index, unsigned line)
69 if(index!=source_index || (index && line!=source_line))
71 if(index==source_index && line==source_line+1)
76 if(stage && stage->required_features.glsl_version && stage->required_features.glsl_version<Version(3, 30))
78 formatted += format("#line %d %d\n", l, index);
85 void Formatter::visit(Block &block)
87 unsigned brace_indent = indent;
88 bool use_braces = (block.use_braces || (indent && block.body.size()!=1));
90 append(format("%s{\n", string(brace_indent*2, ' ')));
92 SetForScope<unsigned> set(indent, indent+(indent>0 || use_braces));
93 string spaces(indent*2, ' ');
95 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
97 if(omit_builtin && (*i)->source<=BUILTIN_SOURCE)
102 set_source((*i)->source, (*i)->line);
108 append(format("\n%s}", string(brace_indent*2, ' ')));
111 void Formatter::visit_expression(Expression &expr, const Operator *outer_oper, bool on_rhs)
113 unsigned outer_precedence = (outer_oper ? outer_oper->precedence : 20);
114 unsigned inner_precedence = (expr.oper ? expr.oper->precedence : 0);
116 bool needs_parentheses = (inner_precedence>=outer_precedence);
118 // Omit parentheses if the outer operator encloses this operand.
119 if(outer_oper && outer_oper->type==Operator::BINARY && outer_oper->token2[0] && on_rhs)
120 needs_parentheses = false;
124 /* Omit parentheses if the inner expression's operator sits between the
125 expression and the outer operator. */
126 bool oper_on_left = expr.oper->type==Operator::PREFIX;
127 bool oper_on_right = expr.oper->type==Operator::POSTFIX || (expr.oper->type==Operator::BINARY && expr.oper->token2[0]);
128 if(expr.oper && ((oper_on_left && on_rhs) || (oper_on_right && !on_rhs)))
129 needs_parentheses = false;
131 // Omit parentheses if the operator's natural grouping works out.
132 if(expr.oper==outer_oper)
133 needs_parentheses = (expr.oper->assoc!=Operator::ASSOCIATIVE && on_rhs!=(expr.oper->assoc==Operator::RIGHT_TO_LEFT));
136 if(needs_parentheses)
139 if(needs_parentheses)
143 void Formatter::visit(Literal &literal)
145 append(literal.token);
148 void Formatter::visit(VariableReference &var)
153 void Formatter::visit(InterfaceBlockReference &iface)
158 void Formatter::visit(MemberAccess &memacc)
160 visit_expression(*memacc.left, memacc.oper, false);
161 append(format(".%s", memacc.member));
164 void Formatter::visit(Swizzle &swizzle)
166 visit_expression(*swizzle.left, swizzle.oper, false);
167 append(format(".%s", swizzle.component_group));
170 void Formatter::visit(UnaryExpression &unary)
172 if(unary.oper->type==Operator::PREFIX)
173 append(unary.oper->token);
174 visit_expression(*unary.expression, unary.oper, unary.oper->type==Operator::PREFIX);
175 if(unary.oper->type==Operator::POSTFIX)
176 append(unary.oper->token);
179 void Formatter::visit(BinaryExpression &binary)
181 visit_expression(*binary.left, binary.oper, false);
182 append(binary.oper->token);
183 visit_expression(*binary.right, binary.oper, true);
184 if(binary.oper->token2[0])
185 append(binary.oper->token2);
188 void Formatter::visit(Assignment &assign)
190 visit_expression(*assign.left, assign.oper, false);
191 append(format(" %s ", assign.oper->token));
192 visit_expression(*assign.right, assign.oper, true);
195 void Formatter::visit(TernaryExpression &ternary)
197 visit_expression(*ternary.condition, ternary.oper, false);
198 append(ternary.oper->token);
199 visit_expression(*ternary.true_expr, ternary.oper, false);
200 if(ternary.oper->token2)
201 append(ternary.oper->token2);
202 visit_expression(*ternary.false_expr, ternary.oper, true);
205 void Formatter::visit(FunctionCall &call)
207 append(format("%s(", call.name));
208 for(NodeArray<Expression>::iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i)
210 if(i!=call.arguments.begin())
217 void Formatter::visit(ExpressionStatement &expr)
219 expr.expression->visit(*this);
223 void Formatter::visit(Import &import)
225 append(format("import %s;", import.module));
228 void Formatter::visit(Precision &prec)
230 append(format("precision %s %s;", prec.precision, prec.type));
233 void Formatter::visit(Layout &layout)
236 for(vector<Layout::Qualifier>::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); ++i)
238 if(i!=layout.qualifiers.begin())
242 append(format("=%d", i->value));
247 void Formatter::visit(InterfaceLayout &layout)
249 layout.layout.visit(*this);
250 append(format(" %s;", layout.interface));
253 void Formatter::visit(StructDeclaration &strct)
255 append(format("struct %s\n", strct.name));
256 strct.members.visit(*this);
260 void Formatter::visit(VariableDeclaration &var)
264 var.layout->visit(*this);
269 if(!var.interpolation.empty())
270 append(format("%s ", var.interpolation));
271 if(!var.sampling.empty())
272 append(format("%s ", var.sampling));
273 if(!var.interface.empty())
275 string interface = var.interface;
276 if(stage && stage->required_features.glsl_version && stage->required_features.glsl_version<Version(1, 30))
278 if(stage->type==Stage::VERTEX && var.interface=="in")
279 interface = "attribute";
280 else if((stage->type==Stage::VERTEX && var.interface=="out") || (stage->type==Stage::FRAGMENT && var.interface=="in"))
281 interface = "varying";
283 append(format("%s ", interface));
285 if(!var.precision.empty())
286 append(format("%s ", var.precision));
287 string type_name = var.type_declaration->name;
289 type_name = type_name.substr(0, type_name.find('['));
290 append(format("%s %s", type_name, var.name));
295 var.array_size->visit(*this);
298 if(var.init_expression)
301 var.init_expression->visit(*this);
307 void Formatter::visit(InterfaceBlock &iface)
311 iface.layout->visit(*this);
314 append(format("%s %s\n", iface.interface, iface.block_name));
315 if(iface.struct_declaration)
316 iface.struct_declaration->members.visit(*this);
317 if(!iface.instance_name.empty())
320 append(iface.instance_name);
327 void Formatter::visit(FunctionDeclaration &func)
329 append(format("%s %s(", func.return_type_declaration->name, func.name));
330 for(NodeArray<VariableDeclaration>::iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
332 if(i!=func.parameters.begin())
334 SetFlag set(parameter_list);
338 if(func.definition==&func)
341 func.body.visit(*this);
347 void Formatter::visit(Conditional &cond)
350 cond.condition->visit(*this);
353 cond.body.visit(*this);
354 if(!cond.else_body.body.empty())
356 Conditional *else_cond = dynamic_cast<Conditional *>(cond.else_body.body.front().get());
357 if(cond.else_body.body.size()==1 && else_cond)
360 set_source(else_cond->source, else_cond->line);
361 append(format("%selse ", string(indent*2, ' ')));
362 else_cond->visit(*this);
366 append(format("\n%selse\n", string(indent*2, ' ')));
367 cond.else_body.visit(*this);
372 void Formatter::visit(Iteration &iter)
374 if(!iter.init_statement && iter.condition && !iter.loop_expression)
377 iter.condition->visit(*this);
383 if(iter.init_statement)
384 iter.init_statement->visit(*this);
390 iter.condition->visit(*this);
393 if(iter.loop_expression)
396 iter.loop_expression->visit(*this);
401 if(iter.body.body.empty())
406 iter.body.visit(*this);
410 void Formatter::visit(Passthrough &pass)
412 append("passthrough");
416 pass.subscript->visit(*this);
422 void Formatter::visit(Return &ret)
428 ret.expression->visit(*this);
433 void Formatter::visit(Jump &jump)
435 append(jump.keyword);