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(ParenthesizedExpression &parexpr)
151 parexpr.expression->visit(*this);
155 void Formatter::visit(VariableReference &var)
160 void Formatter::visit(InterfaceBlockReference &iface)
165 void Formatter::visit(MemberAccess &memacc)
167 visit_expression(*memacc.left, memacc.oper, false);
168 append(format(".%s", memacc.member));
171 void Formatter::visit(Swizzle &swizzle)
173 visit_expression(*swizzle.left, swizzle.oper, false);
174 append(format(".%s", swizzle.component_group));
177 void Formatter::visit(UnaryExpression &unary)
179 if(unary.oper->type==Operator::PREFIX)
180 append(unary.oper->token);
181 visit_expression(*unary.expression, unary.oper, unary.oper->type==Operator::PREFIX);
182 if(unary.oper->type==Operator::POSTFIX)
183 append(unary.oper->token);
186 void Formatter::visit(BinaryExpression &binary)
188 visit_expression(*binary.left, binary.oper, false);
189 append(binary.oper->token);
190 visit_expression(*binary.right, binary.oper, true);
191 if(binary.oper->token2[0])
192 append(binary.oper->token2);
195 void Formatter::visit(Assignment &assign)
197 visit_expression(*assign.left, assign.oper, false);
198 append(format(" %s ", assign.oper->token));
199 visit_expression(*assign.right, assign.oper, true);
202 void Formatter::visit(TernaryExpression &ternary)
204 visit_expression(*ternary.condition, ternary.oper, false);
205 append(ternary.oper->token);
206 visit_expression(*ternary.true_expr, ternary.oper, false);
207 if(ternary.oper->token2)
208 append(ternary.oper->token2);
209 visit_expression(*ternary.false_expr, ternary.oper, true);
212 void Formatter::visit(FunctionCall &call)
214 append(format("%s(", call.name));
215 for(NodeArray<Expression>::iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i)
217 if(i!=call.arguments.begin())
224 void Formatter::visit(ExpressionStatement &expr)
226 expr.expression->visit(*this);
230 void Formatter::visit(Import &import)
232 append(format("import %s;", import.module));
235 void Formatter::visit(Precision &prec)
237 append(format("precision %s %s;", prec.precision, prec.type));
240 void Formatter::visit(Layout &layout)
243 for(vector<Layout::Qualifier>::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); ++i)
245 if(i!=layout.qualifiers.begin())
249 append(format("=%d", i->value));
254 void Formatter::visit(InterfaceLayout &layout)
256 layout.layout.visit(*this);
257 append(format(" %s;", layout.interface));
260 void Formatter::visit(StructDeclaration &strct)
262 append(format("struct %s\n", strct.name));
263 strct.members.visit(*this);
267 void Formatter::visit(VariableDeclaration &var)
271 var.layout->visit(*this);
276 if(!var.interpolation.empty())
277 append(format("%s ", var.interpolation));
278 if(!var.sampling.empty())
279 append(format("%s ", var.sampling));
280 if(!var.interface.empty())
282 string interface = var.interface;
283 if(stage && stage->required_features.glsl_version && stage->required_features.glsl_version<Version(1, 30))
285 if(stage->type==Stage::VERTEX && var.interface=="in")
286 interface = "attribute";
287 else if((stage->type==Stage::VERTEX && var.interface=="out") || (stage->type==Stage::FRAGMENT && var.interface=="in"))
288 interface = "varying";
290 append(format("%s ", interface));
292 if(!var.precision.empty())
293 append(format("%s ", var.precision));
294 string type_name = var.type_declaration->name;
296 type_name = type_name.substr(0, type_name.find('['));
297 append(format("%s %s", type_name, var.name));
302 var.array_size->visit(*this);
305 if(var.init_expression)
308 var.init_expression->visit(*this);
314 void Formatter::visit(InterfaceBlock &iface)
316 append(format("%s %s\n", iface.interface, iface.name));
317 if(iface.struct_declaration)
318 iface.struct_declaration->members.visit(*this);
319 if(!iface.instance_name.empty())
322 append(iface.instance_name);
329 void Formatter::visit(FunctionDeclaration &func)
331 append(format("%s %s(", func.return_type_declaration->name, func.name));
332 for(NodeArray<VariableDeclaration>::iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
334 if(i!=func.parameters.begin())
336 SetFlag set(parameter_list);
340 if(func.definition==&func)
343 func.body.visit(*this);
349 void Formatter::visit(Conditional &cond)
352 cond.condition->visit(*this);
355 cond.body.visit(*this);
356 if(!cond.else_body.body.empty())
358 Conditional *else_cond = dynamic_cast<Conditional *>(cond.else_body.body.front().get());
359 if(cond.else_body.body.size()==1 && else_cond)
362 set_source(else_cond->source, else_cond->line);
363 append(format("%selse ", string(indent*2, ' ')));
364 else_cond->visit(*this);
368 append(format("\n%selse\n", string(indent*2, ' ')));
369 cond.else_body.visit(*this);
374 void Formatter::visit(Iteration &iter)
376 if(!iter.init_statement && iter.condition && !iter.loop_expression)
379 iter.condition->visit(*this);
385 if(iter.init_statement)
386 iter.init_statement->visit(*this);
392 iter.condition->visit(*this);
395 if(iter.loop_expression)
398 iter.loop_expression->visit(*this);
403 if(iter.body.body.empty())
408 iter.body.visit(*this);
412 void Formatter::visit(Passthrough &pass)
414 append("passthrough");
418 pass.subscript->visit(*this);
424 void Formatter::visit(Return &ret)
430 ret.expression->visit(*this);
435 void Formatter::visit(Jump &jump)
437 append(jump.keyword);