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_enhanced_layouts)
36 append("#extension GL_ARB_enhanced_layouts: require\n");
37 if(s.required_features.arb_explicit_attrib_location)
38 append("#extension GL_ARB_explicit_attrib_location: require\n");
39 if(s.required_features.arb_explicit_uniform_location)
40 append("#extension GL_ARB_explicit_uniform_location: require\n");
41 if(s.required_features.arb_gpu_shader5)
42 append("#extension GL_ARB_gpu_shader5: require\n");
43 if(s.required_features.arb_separate_shader_objects)
44 append("#extension GL_ARB_separate_shader_objects: require\n");
45 if(s.required_features.arb_uniform_buffer_object)
46 append("#extension GL_ARB_uniform_buffer_object: require\n");
47 if(s.required_features.ext_gpu_shader4)
48 append("#extension GL_EXT_gpu_shader4: require\n");
49 if(s.required_features.ext_texture_array)
50 append("#extension GL_EXT_texture_array: require\n");
53 s.content.visit(*this);
58 void Formatter::append(const string &text)
61 for(string::const_iterator i=text.begin(); i!=text.end(); ++i)
66 void Formatter::append(char c)
73 void Formatter::set_source(unsigned index, unsigned line)
75 if(index!=source_index || (index && line!=source_line))
77 if(index==source_index && line==source_line+1)
82 if(stage && stage->required_features.glsl_version && stage->required_features.glsl_version<Version(3, 30))
84 formatted += format("#line %d %d\n", l, index);
91 void Formatter::visit(Block &block)
93 unsigned brace_indent = indent;
94 bool use_braces = (block.use_braces || (indent && block.body.size()!=1));
96 append(format("%s{\n", string(brace_indent*2, ' ')));
98 SetForScope<unsigned> set(indent, indent+(indent>0 || use_braces));
99 string spaces(indent*2, ' ');
101 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
103 if(omit_builtin && (*i)->source<=BUILTIN_SOURCE)
108 set_source((*i)->source, (*i)->line);
114 append(format("\n%s}", string(brace_indent*2, ' ')));
117 void Formatter::visit_expression(Expression &expr, const Operator *outer_oper, bool on_rhs)
119 unsigned outer_precedence = (outer_oper ? outer_oper->precedence : 20);
120 unsigned inner_precedence = (expr.oper ? expr.oper->precedence : 0);
122 bool needs_parentheses = (inner_precedence>=outer_precedence);
124 // Omit parentheses if the outer operator encloses this operand.
125 if(outer_oper && outer_oper->type==Operator::BINARY && outer_oper->token2[0] && on_rhs)
126 needs_parentheses = false;
130 /* Omit parentheses if the inner expression's operator sits between the
131 expression and the outer operator. */
132 bool oper_on_left = expr.oper->type==Operator::PREFIX;
133 bool oper_on_right = expr.oper->type==Operator::POSTFIX || (expr.oper->type==Operator::BINARY && expr.oper->token2[0]);
134 if(expr.oper && ((oper_on_left && on_rhs) || (oper_on_right && !on_rhs)))
135 needs_parentheses = false;
137 // Omit parentheses if the operator's natural grouping works out.
138 if(expr.oper==outer_oper)
139 needs_parentheses = (expr.oper->assoc!=Operator::ASSOCIATIVE && on_rhs!=(expr.oper->assoc==Operator::RIGHT_TO_LEFT));
142 if(needs_parentheses)
145 if(needs_parentheses)
149 void Formatter::visit(Literal &literal)
151 append(literal.token);
154 void Formatter::visit(VariableReference &var)
159 void Formatter::visit(InterfaceBlockReference &iface)
164 void Formatter::visit(MemberAccess &memacc)
166 visit_expression(*memacc.left, memacc.oper, false);
167 append(format(".%s", memacc.member));
170 void Formatter::visit(Swizzle &swizzle)
172 visit_expression(*swizzle.left, swizzle.oper, false);
173 append(format(".%s", swizzle.component_group));
176 void Formatter::visit(UnaryExpression &unary)
178 if(unary.oper->type==Operator::PREFIX)
179 append(unary.oper->token);
180 visit_expression(*unary.expression, unary.oper, unary.oper->type==Operator::PREFIX);
181 if(unary.oper->type==Operator::POSTFIX)
182 append(unary.oper->token);
185 void Formatter::visit(BinaryExpression &binary)
187 visit_expression(*binary.left, binary.oper, false);
188 append(binary.oper->token);
189 visit_expression(*binary.right, binary.oper, true);
190 if(binary.oper->token2[0])
191 append(binary.oper->token2);
194 void Formatter::visit(Assignment &assign)
196 visit_expression(*assign.left, assign.oper, false);
197 append(format(" %s ", assign.oper->token));
198 visit_expression(*assign.right, assign.oper, true);
201 void Formatter::visit(TernaryExpression &ternary)
203 visit_expression(*ternary.condition, ternary.oper, false);
204 append(ternary.oper->token);
205 visit_expression(*ternary.true_expr, ternary.oper, false);
206 if(ternary.oper->token2)
207 append(ternary.oper->token2);
208 visit_expression(*ternary.false_expr, ternary.oper, true);
211 void Formatter::visit(FunctionCall &call)
213 append(format("%s(", call.name));
214 for(NodeArray<Expression>::iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i)
216 if(i!=call.arguments.begin())
223 void Formatter::visit(ExpressionStatement &expr)
225 expr.expression->visit(*this);
229 void Formatter::visit(Import &import)
231 append(format("import %s;", import.module));
234 void Formatter::visit(Precision &prec)
236 append(format("precision %s %s;", prec.precision, prec.type));
239 void Formatter::visit(Layout &layout)
242 for(vector<Layout::Qualifier>::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); ++i)
244 if(i!=layout.qualifiers.begin())
248 append(format("=%d", i->value));
253 void Formatter::visit(InterfaceLayout &layout)
255 layout.layout.visit(*this);
256 append(format(" %s;", layout.interface));
259 void Formatter::visit(StructDeclaration &strct)
261 append(format("struct %s\n", strct.name));
262 strct.members.visit(*this);
266 void Formatter::visit(VariableDeclaration &var)
270 var.layout->visit(*this);
275 if(!var.interpolation.empty())
276 append(format("%s ", var.interpolation));
277 if(!var.sampling.empty())
278 append(format("%s ", var.sampling));
279 if(!var.interface.empty())
281 string interface = var.interface;
282 if(stage && stage->required_features.glsl_version && stage->required_features.glsl_version<Version(1, 30))
284 if(stage->type==Stage::VERTEX && var.interface=="in")
285 interface = "attribute";
286 else if((stage->type==Stage::VERTEX && var.interface=="out") || (stage->type==Stage::FRAGMENT && var.interface=="in"))
287 interface = "varying";
289 append(format("%s ", interface));
291 if(!var.precision.empty())
292 append(format("%s ", var.precision));
293 string type_name = var.type_declaration->name;
295 type_name = type_name.substr(0, type_name.find('['));
296 append(format("%s %s", type_name, var.name));
301 var.array_size->visit(*this);
304 if(var.init_expression)
307 var.init_expression->visit(*this);
313 void Formatter::visit(InterfaceBlock &iface)
317 iface.layout->visit(*this);
320 append(format("%s %s\n", iface.interface, iface.block_name));
321 if(iface.struct_declaration)
322 iface.struct_declaration->members.visit(*this);
323 if(!iface.instance_name.empty())
326 append(iface.instance_name);
333 void Formatter::visit(FunctionDeclaration &func)
335 append(format("%s %s(", func.return_type_declaration->name, func.name));
336 for(NodeArray<VariableDeclaration>::iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
338 if(i!=func.parameters.begin())
340 SetFlag set(parameter_list);
344 if(func.definition==&func)
347 func.body.visit(*this);
353 void Formatter::visit(Conditional &cond)
356 cond.condition->visit(*this);
359 cond.body.visit(*this);
360 if(!cond.else_body.body.empty())
362 Conditional *else_cond = dynamic_cast<Conditional *>(cond.else_body.body.front().get());
363 if(cond.else_body.body.size()==1 && else_cond)
366 set_source(else_cond->source, else_cond->line);
367 append(format("%selse ", string(indent*2, ' ')));
368 else_cond->visit(*this);
372 append(format("\n%selse\n", string(indent*2, ' ')));
373 cond.else_body.visit(*this);
378 void Formatter::visit(Iteration &iter)
380 if(!iter.init_statement && iter.condition && !iter.loop_expression)
383 iter.condition->visit(*this);
389 if(iter.init_statement)
390 iter.init_statement->visit(*this);
396 iter.condition->visit(*this);
399 if(iter.loop_expression)
402 iter.loop_expression->visit(*this);
407 if(iter.body.body.empty())
412 iter.body.visit(*this);
416 void Formatter::visit(Passthrough &pass)
418 append("passthrough");
422 pass.subscript->visit(*this);
428 void Formatter::visit(Return &ret)
434 ret.expression->visit(*this);
439 void Formatter::visit(Jump &jump)
441 append(jump.keyword);