1 #include <msp/core/raii.h>
2 #include <msp/strings/format.h>
11 Formatter::Formatter():
16 parameter_list(false),
21 string Formatter::apply(Stage &s)
26 const Version &ver = s.required_features.glsl_version;
30 append(format("#version %d%02d", ver.major, ver.minor));
31 if(s.required_features.gl_api==OPENGL_ES2 && ver>=Version(3, 0))
36 if(s.required_features.arb_enhanced_layouts)
37 append("#extension GL_ARB_enhanced_layouts: require\n");
38 if(s.required_features.arb_explicit_attrib_location)
39 append("#extension GL_ARB_explicit_attrib_location: require\n");
40 if(s.required_features.arb_explicit_uniform_location)
41 append("#extension GL_ARB_explicit_uniform_location: require\n");
42 if(s.required_features.arb_gpu_shader5)
43 append("#extension GL_ARB_gpu_shader5: require\n");
44 if(s.required_features.arb_separate_shader_objects)
45 append("#extension GL_ARB_separate_shader_objects: require\n");
46 if(s.required_features.arb_uniform_buffer_object)
47 append("#extension GL_ARB_uniform_buffer_object: require\n");
48 if(s.required_features.ext_gpu_shader4)
49 append("#extension GL_EXT_gpu_shader4: require\n");
50 if(s.required_features.ext_texture_array)
51 append("#extension GL_EXT_texture_array: require\n");
54 s.content.visit(*this);
59 void Formatter::append(const string &text)
62 for(string::const_iterator i=text.begin(); i!=text.end(); ++i)
67 void Formatter::append(char c)
74 void Formatter::set_source(unsigned index, unsigned line)
76 if(index!=source_index || (index && line!=source_line))
78 if(index==source_index && line==source_line+1)
83 if(stage && stage->required_features.glsl_version && stage->required_features.glsl_version<Version(3, 30))
85 formatted += format("#line %d %d\n", l, index);
92 void Formatter::visit(Block &block)
94 unsigned brace_indent = indent;
95 bool use_braces = (block.use_braces || (indent && block.body.size()!=1));
97 append(format("%s{\n", string(brace_indent*2, ' ')));
99 SetForScope<unsigned> set(indent, indent+(indent>0 || use_braces));
100 string spaces(indent*2, ' ');
102 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
104 if(omit_builtin && (*i)->source<=BUILTIN_SOURCE)
109 set_source((*i)->source, (*i)->line);
115 append(format("\n%s}", string(brace_indent*2, ' ')));
118 void Formatter::visit_expression(Expression &expr, const Operator *outer_oper, bool on_rhs)
120 unsigned outer_precedence = (outer_oper ? outer_oper->precedence : 20);
121 unsigned inner_precedence = (expr.oper ? expr.oper->precedence : 0);
123 bool needs_parentheses = (inner_precedence>=outer_precedence);
125 // Omit parentheses if the outer operator encloses this operand.
126 if(outer_oper && outer_oper->type==Operator::BINARY && outer_oper->token2[0] && on_rhs)
127 needs_parentheses = false;
131 /* Omit parentheses if the inner expression's operator sits between the
132 expression and the outer operator. */
133 bool oper_on_left = expr.oper->type==Operator::PREFIX;
134 bool oper_on_right = expr.oper->type==Operator::POSTFIX || (expr.oper->type==Operator::BINARY && expr.oper->token2[0]);
135 if(expr.oper && ((oper_on_left && on_rhs) || (oper_on_right && !on_rhs)))
136 needs_parentheses = false;
138 // Omit parentheses if the operator's natural grouping works out.
139 if(expr.oper==outer_oper)
140 needs_parentheses = (expr.oper->assoc!=Operator::ASSOCIATIVE && on_rhs!=(expr.oper->assoc==Operator::RIGHT_TO_LEFT));
143 if(needs_parentheses)
146 if(needs_parentheses)
150 void Formatter::visit(Literal &literal)
152 append(literal.token);
155 void Formatter::visit(VariableReference &var)
158 r_empty_name = false;
161 void Formatter::visit(InterfaceBlockReference &iface)
163 r_empty_name = iface.declaration->instance_name.empty();
165 append(iface.declaration->instance_name);
168 void Formatter::visit(MemberAccess &memacc)
170 visit_expression(*memacc.left, memacc.oper, false);
173 append(memacc.member);
176 void Formatter::visit(Swizzle &swizzle)
178 visit_expression(*swizzle.left, swizzle.oper, false);
179 append(format(".%s", swizzle.component_group));
182 void Formatter::visit(UnaryExpression &unary)
184 if(unary.oper->type==Operator::PREFIX)
185 append(unary.oper->token);
186 visit_expression(*unary.expression, unary.oper, unary.oper->type==Operator::PREFIX);
187 if(unary.oper->type==Operator::POSTFIX)
188 append(unary.oper->token);
191 void Formatter::visit(BinaryExpression &binary)
193 visit_expression(*binary.left, binary.oper, false);
194 append(binary.oper->token);
195 visit_expression(*binary.right, binary.oper, true);
196 if(binary.oper->token2[0])
197 append(binary.oper->token2);
200 void Formatter::visit(Assignment &assign)
202 visit_expression(*assign.left, assign.oper, false);
203 append(format(" %s ", assign.oper->token));
204 visit_expression(*assign.right, assign.oper, true);
207 void Formatter::visit(TernaryExpression &ternary)
209 visit_expression(*ternary.condition, ternary.oper, false);
210 append(ternary.oper->token);
211 visit_expression(*ternary.true_expr, ternary.oper, false);
212 if(ternary.oper->token2)
213 append(ternary.oper->token2);
214 visit_expression(*ternary.false_expr, ternary.oper, true);
217 void Formatter::visit(FunctionCall &call)
219 append(format("%s(", call.name));
220 for(NodeArray<Expression>::iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i)
222 if(i!=call.arguments.begin())
229 void Formatter::visit(ExpressionStatement &expr)
231 expr.expression->visit(*this);
235 void Formatter::visit(Import &import)
237 append(format("import %s;", import.module));
240 void Formatter::visit(Precision &prec)
242 append(format("precision %s %s;", prec.precision, prec.type));
245 void Formatter::visit(Layout &layout)
248 for(vector<Layout::Qualifier>::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); ++i)
250 if(i!=layout.qualifiers.begin())
254 append(format("=%d", i->value));
259 void Formatter::visit(InterfaceLayout &layout)
261 layout.layout.visit(*this);
262 append(format(" %s;", layout.interface));
265 void Formatter::visit(StructDeclaration &strct)
267 append(format("struct %s\n", strct.name));
268 strct.members.visit(*this);
272 void Formatter::visit(VariableDeclaration &var)
276 var.layout->visit(*this);
281 if(!var.interpolation.empty())
282 append(format("%s ", var.interpolation));
283 if(!var.sampling.empty())
284 append(format("%s ", var.sampling));
285 if(!var.interface.empty())
287 string interface = var.interface;
288 if(stage && stage->required_features.glsl_version && stage->required_features.glsl_version<Version(1, 30))
290 if(stage->type==Stage::VERTEX && var.interface=="in")
291 interface = "attribute";
292 else if((stage->type==Stage::VERTEX && var.interface=="out") || (stage->type==Stage::FRAGMENT && var.interface=="in"))
293 interface = "varying";
295 append(format("%s ", interface));
297 if(!var.precision.empty())
298 append(format("%s ", var.precision));
299 string type_name = var.type_declaration->name;
301 type_name = type_name.substr(0, type_name.find('['));
302 append(format("%s %s", type_name, var.name));
307 var.array_size->visit(*this);
310 if(var.init_expression)
313 var.init_expression->visit(*this);
319 void Formatter::visit(InterfaceBlock &iface)
323 iface.layout->visit(*this);
326 append(format("%s %s\n", iface.interface, iface.block_name));
327 if(iface.struct_declaration)
328 iface.struct_declaration->members.visit(*this);
329 if(!iface.instance_name.empty())
332 append(iface.instance_name);
339 void Formatter::visit(FunctionDeclaration &func)
341 append(format("%s %s(", func.return_type_declaration->name, func.name));
342 for(NodeArray<VariableDeclaration>::iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
344 if(i!=func.parameters.begin())
346 SetFlag set(parameter_list);
350 if(func.definition==&func)
353 func.body.visit(*this);
359 void Formatter::visit(Conditional &cond)
362 cond.condition->visit(*this);
365 cond.body.visit(*this);
366 if(!cond.else_body.body.empty())
368 Conditional *else_cond = dynamic_cast<Conditional *>(cond.else_body.body.front().get());
369 if(cond.else_body.body.size()==1 && else_cond)
372 set_source(else_cond->source, else_cond->line);
373 append(format("%selse ", string(indent*2, ' ')));
374 else_cond->visit(*this);
378 append(format("\n%selse\n", string(indent*2, ' ')));
379 cond.else_body.visit(*this);
384 void Formatter::visit(Iteration &iter)
386 if(!iter.init_statement && iter.condition && !iter.loop_expression)
389 iter.condition->visit(*this);
395 if(iter.init_statement)
396 iter.init_statement->visit(*this);
402 iter.condition->visit(*this);
405 if(iter.loop_expression)
408 iter.loop_expression->visit(*this);
413 if(iter.body.body.empty())
418 iter.body.visit(*this);
422 void Formatter::visit(Passthrough &pass)
424 append("passthrough");
428 pass.subscript->visit(*this);
434 void Formatter::visit(Return &ret)
440 ret.expression->visit(*this);
445 void Formatter::visit(Jump &jump)
447 append(jump.keyword);