1 #include <msp/core/raii.h>
2 #include <msp/strings/format.h>
11 string Formatter::apply(Stage &s)
16 const Version &ver = s.required_features.glsl_version;
20 append(format("#version %d%02d", ver.major, ver.minor));
21 if(s.required_features.target_api==OPENGL_ES && ver>=Version(3, 0))
26 if(s.required_features.arb_enhanced_layouts)
27 append("#extension GL_ARB_enhanced_layouts: require\n");
28 if(s.required_features.arb_explicit_attrib_location)
29 append("#extension GL_ARB_explicit_attrib_location: require\n");
30 if(s.required_features.arb_explicit_uniform_location)
31 append("#extension GL_ARB_explicit_uniform_location: require\n");
32 if(s.required_features.arb_gpu_shader5)
33 append("#extension GL_ARB_gpu_shader5: require\n");
34 if(s.required_features.arb_separate_shader_objects)
35 append("#extension GL_ARB_separate_shader_objects: require\n");
36 if(s.required_features.arb_uniform_buffer_object)
37 append("#extension GL_ARB_uniform_buffer_object: require\n");
38 if(s.required_features.ext_gpu_shader4)
39 append("#extension GL_EXT_gpu_shader4: require\n");
40 if(s.required_features.ext_texture_array)
41 append("#extension GL_EXT_texture_array: require\n");
44 s.content.visit(*this);
49 void Formatter::append(const string &text)
57 void Formatter::append(char c)
64 void Formatter::set_source(unsigned index, unsigned line)
66 if(index!=source_index || (index && line!=source_line))
68 if(index==source_index && line==source_line+1)
73 if(stage && stage->required_features.glsl_version && stage->required_features.glsl_version<Version(3, 30))
75 formatted += format("#line %d %d\n", l, index);
82 void Formatter::visit(Block &block)
84 unsigned brace_indent = indent;
85 bool use_braces = (block.use_braces || (indent && block.body.size()!=1));
87 append(format("%s{\n", string(brace_indent*2, ' ')));
89 SetForScope<unsigned> set(indent, indent+(indent>0 || use_braces));
90 string spaces(indent*2, ' ');
92 for(const RefPtr<Statement> &s: block.body)
94 if(omit_builtin && s->source<=BUILTIN_SOURCE)
99 set_source(s->source, s->line);
105 append(format("\n%s}", string(brace_indent*2, ' ')));
108 void Formatter::visit_expression(Expression &expr, const Operator *outer_oper, bool on_rhs)
110 unsigned outer_precedence = (outer_oper ? outer_oper->precedence : 20);
111 unsigned inner_precedence = (expr.oper ? expr.oper->precedence : 0);
113 bool needs_parentheses = (inner_precedence>=outer_precedence);
115 // Omit parentheses if the outer operator encloses this operand.
116 if(outer_oper && outer_oper->type==Operator::BINARY && outer_oper->token2[0] && on_rhs)
117 needs_parentheses = false;
121 /* Omit parentheses if the inner expression's operator sits between the
122 expression and the outer operator. */
123 bool oper_on_left = expr.oper->type==Operator::PREFIX;
124 bool oper_on_right = expr.oper->type==Operator::POSTFIX || (expr.oper->type==Operator::BINARY && expr.oper->token2[0]);
125 if(expr.oper && ((oper_on_left && on_rhs) || (oper_on_right && !on_rhs)))
126 needs_parentheses = false;
128 // Omit parentheses if the operator's natural grouping works out.
129 if(expr.oper==outer_oper)
130 needs_parentheses = (expr.oper->assoc!=Operator::ASSOCIATIVE && on_rhs!=(expr.oper->assoc==Operator::RIGHT_TO_LEFT));
133 if(needs_parentheses)
136 if(needs_parentheses)
140 void Formatter::visit(Literal &literal)
142 append(literal.token);
145 void Formatter::visit(VariableReference &var)
148 r_empty_name = false;
151 void Formatter::visit(InterfaceBlockReference &iface)
153 r_empty_name = iface.declaration->instance_name.empty();
155 append(iface.declaration->instance_name);
158 void Formatter::visit(MemberAccess &memacc)
160 visit_expression(*memacc.left, memacc.oper, false);
163 append(memacc.member);
164 r_empty_name = false;
167 void Formatter::visit(Swizzle &swizzle)
169 visit_expression(*swizzle.left, swizzle.oper, false);
170 append(format(".%s", swizzle.component_group));
173 void Formatter::visit(UnaryExpression &unary)
175 if(unary.oper->type==Operator::PREFIX)
176 append(unary.oper->token);
177 visit_expression(*unary.expression, unary.oper, unary.oper->type==Operator::PREFIX);
178 if(unary.oper->type==Operator::POSTFIX)
179 append(unary.oper->token);
182 void Formatter::visit(BinaryExpression &binary)
184 visit_expression(*binary.left, binary.oper, false);
185 append(binary.oper->token);
186 visit_expression(*binary.right, binary.oper, true);
187 if(binary.oper->token2[0])
188 append(binary.oper->token2);
191 void Formatter::visit(Assignment &assign)
193 visit_expression(*assign.left, assign.oper, false);
194 append(format(" %s ", assign.oper->token));
195 visit_expression(*assign.right, assign.oper, true);
198 void Formatter::visit(TernaryExpression &ternary)
200 visit_expression(*ternary.condition, ternary.oper, false);
201 append(ternary.oper->token);
202 visit_expression(*ternary.true_expr, ternary.oper, false);
203 if(ternary.oper->token2)
204 append(ternary.oper->token2);
205 visit_expression(*ternary.false_expr, ternary.oper, true);
208 void Formatter::visit(FunctionCall &call)
210 append(format("%s(", call.name));
211 for(auto i=call.arguments.begin(); i!=call.arguments.end(); ++i)
213 if(i!=call.arguments.begin())
220 void Formatter::visit(ExpressionStatement &expr)
222 expr.expression->visit(*this);
226 void Formatter::visit(Import &import)
228 append(format("import %s;", import.module));
231 void Formatter::visit(Precision &prec)
233 append(format("precision %s %s;", prec.precision, prec.type));
236 void Formatter::visit(Layout &layout)
239 for(auto i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); ++i)
241 if(i!=layout.qualifiers.begin())
245 append(format("=%d", i->value));
250 void Formatter::visit(InterfaceLayout &layout)
252 layout.layout.visit(*this);
253 append(format(" %s;", layout.interface));
256 void Formatter::visit(StructDeclaration &strct)
258 append(format("struct %s\n", strct.name));
259 strct.members.visit(*this);
263 void Formatter::visit(VariableDeclaration &var)
267 var.layout->visit(*this);
272 if(!var.interpolation.empty())
273 append(format("%s ", var.interpolation));
274 if(!var.sampling.empty())
275 append(format("%s ", var.sampling));
276 if(!var.interface.empty())
278 string interface = var.interface;
279 if(stage && stage->required_features.glsl_version && stage->required_features.glsl_version<Version(1, 30))
281 if(stage->type==Stage::VERTEX && var.interface=="in")
282 interface = "attribute";
283 else if((stage->type==Stage::VERTEX && var.interface=="out") || (stage->type==Stage::FRAGMENT && var.interface=="in"))
284 interface = "varying";
286 append(format("%s ", interface));
288 if(!var.precision.empty())
289 append(format("%s ", var.precision));
290 string type_name = var.type_declaration->name;
292 type_name = type_name.substr(0, type_name.find('['));
293 append(format("%s %s", type_name, var.name));
298 var.array_size->visit(*this);
301 if(var.init_expression)
304 var.init_expression->visit(*this);
310 void Formatter::visit(InterfaceBlock &iface)
314 iface.layout->visit(*this);
317 append(format("%s %s\n", iface.interface, iface.block_name));
318 if(iface.struct_declaration)
319 iface.struct_declaration->members.visit(*this);
320 if(!iface.instance_name.empty())
323 append(iface.instance_name);
330 void Formatter::visit(FunctionDeclaration &func)
332 append(format("%s %s(", func.return_type_declaration->name, func.name));
333 for(auto i=func.parameters.begin(); i!=func.parameters.end(); ++i)
335 if(i!=func.parameters.begin())
337 SetFlag set(parameter_list);
341 if(func.definition==&func)
344 func.body.visit(*this);
350 void Formatter::visit(Conditional &cond)
353 cond.condition->visit(*this);
356 cond.body.visit(*this);
357 if(!cond.else_body.body.empty())
359 Conditional *else_cond = dynamic_cast<Conditional *>(cond.else_body.body.front().get());
360 if(cond.else_body.body.size()==1 && else_cond)
363 set_source(else_cond->source, else_cond->line);
364 append(format("%selse ", string(indent*2, ' ')));
365 else_cond->visit(*this);
369 append(format("\n%selse\n", string(indent*2, ' ')));
370 cond.else_body.visit(*this);
375 void Formatter::visit(Iteration &iter)
377 if(!iter.init_statement && iter.condition && !iter.loop_expression)
380 iter.condition->visit(*this);
386 if(iter.init_statement)
387 iter.init_statement->visit(*this);
393 iter.condition->visit(*this);
396 if(iter.loop_expression)
399 iter.loop_expression->visit(*this);
404 if(iter.body.body.empty())
409 iter.body.visit(*this);
413 void Formatter::visit(Passthrough &pass)
415 append("passthrough");
419 pass.subscript->visit(*this);
425 void Formatter::visit(Return &ret)
431 ret.expression->visit(*this);
436 void Formatter::visit(Jump &jump)
438 append(jump.keyword);