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);
174 r_empty_name = false;
177 void Formatter::visit(Swizzle &swizzle)
179 visit_expression(*swizzle.left, swizzle.oper, false);
180 append(format(".%s", swizzle.component_group));
183 void Formatter::visit(UnaryExpression &unary)
185 if(unary.oper->type==Operator::PREFIX)
186 append(unary.oper->token);
187 visit_expression(*unary.expression, unary.oper, unary.oper->type==Operator::PREFIX);
188 if(unary.oper->type==Operator::POSTFIX)
189 append(unary.oper->token);
192 void Formatter::visit(BinaryExpression &binary)
194 visit_expression(*binary.left, binary.oper, false);
195 append(binary.oper->token);
196 visit_expression(*binary.right, binary.oper, true);
197 if(binary.oper->token2[0])
198 append(binary.oper->token2);
201 void Formatter::visit(Assignment &assign)
203 visit_expression(*assign.left, assign.oper, false);
204 append(format(" %s ", assign.oper->token));
205 visit_expression(*assign.right, assign.oper, true);
208 void Formatter::visit(TernaryExpression &ternary)
210 visit_expression(*ternary.condition, ternary.oper, false);
211 append(ternary.oper->token);
212 visit_expression(*ternary.true_expr, ternary.oper, false);
213 if(ternary.oper->token2)
214 append(ternary.oper->token2);
215 visit_expression(*ternary.false_expr, ternary.oper, true);
218 void Formatter::visit(FunctionCall &call)
220 append(format("%s(", call.name));
221 for(NodeArray<Expression>::iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i)
223 if(i!=call.arguments.begin())
230 void Formatter::visit(ExpressionStatement &expr)
232 expr.expression->visit(*this);
236 void Formatter::visit(Import &import)
238 append(format("import %s;", import.module));
241 void Formatter::visit(Precision &prec)
243 append(format("precision %s %s;", prec.precision, prec.type));
246 void Formatter::visit(Layout &layout)
249 for(vector<Layout::Qualifier>::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); ++i)
251 if(i!=layout.qualifiers.begin())
255 append(format("=%d", i->value));
260 void Formatter::visit(InterfaceLayout &layout)
262 layout.layout.visit(*this);
263 append(format(" %s;", layout.interface));
266 void Formatter::visit(StructDeclaration &strct)
268 append(format("struct %s\n", strct.name));
269 strct.members.visit(*this);
273 void Formatter::visit(VariableDeclaration &var)
277 var.layout->visit(*this);
282 if(!var.interpolation.empty())
283 append(format("%s ", var.interpolation));
284 if(!var.sampling.empty())
285 append(format("%s ", var.sampling));
286 if(!var.interface.empty())
288 string interface = var.interface;
289 if(stage && stage->required_features.glsl_version && stage->required_features.glsl_version<Version(1, 30))
291 if(stage->type==Stage::VERTEX && var.interface=="in")
292 interface = "attribute";
293 else if((stage->type==Stage::VERTEX && var.interface=="out") || (stage->type==Stage::FRAGMENT && var.interface=="in"))
294 interface = "varying";
296 append(format("%s ", interface));
298 if(!var.precision.empty())
299 append(format("%s ", var.precision));
300 string type_name = var.type_declaration->name;
302 type_name = type_name.substr(0, type_name.find('['));
303 append(format("%s %s", type_name, var.name));
308 var.array_size->visit(*this);
311 if(var.init_expression)
314 var.init_expression->visit(*this);
320 void Formatter::visit(InterfaceBlock &iface)
324 iface.layout->visit(*this);
327 append(format("%s %s\n", iface.interface, iface.block_name));
328 if(iface.struct_declaration)
329 iface.struct_declaration->members.visit(*this);
330 if(!iface.instance_name.empty())
333 append(iface.instance_name);
340 void Formatter::visit(FunctionDeclaration &func)
342 append(format("%s %s(", func.return_type_declaration->name, func.name));
343 for(NodeArray<VariableDeclaration>::iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
345 if(i!=func.parameters.begin())
347 SetFlag set(parameter_list);
351 if(func.definition==&func)
354 func.body.visit(*this);
360 void Formatter::visit(Conditional &cond)
363 cond.condition->visit(*this);
366 cond.body.visit(*this);
367 if(!cond.else_body.body.empty())
369 Conditional *else_cond = dynamic_cast<Conditional *>(cond.else_body.body.front().get());
370 if(cond.else_body.body.size()==1 && else_cond)
373 set_source(else_cond->source, else_cond->line);
374 append(format("%selse ", string(indent*2, ' ')));
375 else_cond->visit(*this);
379 append(format("\n%selse\n", string(indent*2, ' ')));
380 cond.else_body.visit(*this);
385 void Formatter::visit(Iteration &iter)
387 if(!iter.init_statement && iter.condition && !iter.loop_expression)
390 iter.condition->visit(*this);
396 if(iter.init_statement)
397 iter.init_statement->visit(*this);
403 iter.condition->visit(*this);
406 if(iter.loop_expression)
409 iter.loop_expression->visit(*this);
414 if(iter.body.body.empty())
419 iter.body.visit(*this);
423 void Formatter::visit(Passthrough &pass)
425 append("passthrough");
429 pass.subscript->visit(*this);
435 void Formatter::visit(Return &ret)
441 ret.expression->visit(*this);
446 void Formatter::visit(Jump &jump)
448 append(jump.keyword);