]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/output.cpp
Remove the using declarations from visitors
[libs/gl.git] / source / glsl / output.cpp
1 #include <msp/core/raii.h>
2 #include <msp/strings/format.h>
3 #include "output.h"
4
5 using namespace std;
6
7 namespace Msp {
8 namespace GL {
9 namespace SL {
10
11 Formatter::Formatter():
12         stage(0),
13         mode(Compiler::PROGRAM),
14         source_index(0),
15         source_line(1),
16         indent(0),
17         parameter_list(false)
18 { }
19
20 const string &Formatter::apply(Stage &s, Compiler::Mode m)
21 {
22         mode = m;
23         stage = &s;
24
25         const Version &ver = s.required_features.glsl_version;
26
27         if(ver)
28         {
29                 append(format("#version %d%02d", ver.major, ver.minor));
30                 if(s.required_features.gl_api==OPENGL_ES2 && ver>=Version(3, 0))
31                         append(" es");
32                 formatted += '\n';
33         }
34
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");
45         formatted += '\n';
46
47         s.content.visit(*this);
48
49         return formatted;
50 }
51
52 void Formatter::append(const string &text)
53 {
54         formatted += text;
55         for(string::const_iterator i=text.begin(); i!=text.end(); ++i)
56                 if(*i=='\n')
57                         ++source_line;
58 }
59
60 void Formatter::append(char c)
61 {
62         formatted += c;
63         if(c=='\n')
64                 ++source_line;
65 }
66
67 void Formatter::set_source(unsigned index, unsigned line)
68 {
69         if(index!=source_index || (index && line!=source_line))
70         {
71                 if(index==source_index && line==source_line+1)
72                         formatted += '\n';
73                 else
74                 {
75                         unsigned l = line;
76                         if(mode==Compiler::PROGRAM && stage && stage->required_features.glsl_version<Version(3, 30))
77                                 --l;
78                         formatted += format("#line %d %d\n", l, index);
79                 }
80         }
81         source_index = index;
82         source_line = line;
83 }
84
85 void Formatter::visit(Literal &literal)
86 {
87         append(literal.token);
88 }
89
90 void Formatter::visit(ParenthesizedExpression &parexpr)
91 {
92         append('(');
93         parexpr.expression->visit(*this);
94         append(')');
95 }
96
97 void Formatter::visit(VariableReference &var)
98 {
99         append(var.name);
100 }
101
102 void Formatter::visit(MemberAccess &memacc)
103 {
104         memacc.left->visit(*this);
105         append(format(".%s", memacc.member));
106 }
107
108 void Formatter::visit(UnaryExpression &unary)
109 {
110         if(unary.prefix)
111                 append(unary.oper);
112         unary.expression->visit(*this);
113         if(!unary.prefix)
114                 append(unary.oper);
115 }
116
117 void Formatter::visit(BinaryExpression &binary)
118 {
119         binary.left->visit(*this);
120         append(binary.oper);
121         binary.right->visit(*this);
122         append(binary.after);
123 }
124
125 void Formatter::visit(Assignment &assign)
126 {
127         assign.left->visit(*this);
128         append(format(" %s ", assign.oper));
129         assign.right->visit(*this);
130 }
131
132 void Formatter::visit(FunctionCall &call)
133 {
134         append(format("%s(", call.name));
135         for(NodeArray<Expression>::iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i)
136         {
137                 if(i!=call.arguments.begin())
138                         append(", ");
139                 (*i)->visit(*this);
140         }
141         append(')');
142 }
143
144 void Formatter::visit(ExpressionStatement &expr)
145 {
146         expr.expression->visit(*this);
147         append(';');
148 }
149
150 void Formatter::visit(Block &block)
151 {
152         unsigned brace_indent = indent;
153         bool use_braces = (block.use_braces || (indent && block.body.size()!=1));
154         if(use_braces)
155                 append(format("%s{\n", string(brace_indent*2, ' ')));
156
157         SetForScope<unsigned> set(indent, indent+(indent>0 || use_braces));
158         string spaces(indent*2, ' ');
159         for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
160         {
161                 if(i!=block.body.begin())
162                         append('\n');
163                 set_source((*i)->source, (*i)->line);
164                 append(spaces);
165                 (*i)->visit(*this);
166         }
167
168         if(use_braces)
169                 append(format("\n%s}", string(brace_indent*2, ' ')));
170 }
171
172 void Formatter::visit(Import &import)
173 {
174         append(format("import %s;", import.module));
175 }
176
177 void Formatter::visit(Precision &prec)
178 {
179         append(format("precision %s %s;", prec.precision, prec.type));
180 }
181
182 void Formatter::visit(Layout &layout)
183 {
184         append("layout(");
185         for(vector<Layout::Qualifier>::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); ++i)
186         {
187                 if(i!=layout.qualifiers.begin())
188                         append(", ");
189                 append(i->name);
190                 if(i->has_value)
191                         append(format("=%d", i->value));
192         }
193         append(')');
194 }
195
196 void Formatter::visit(InterfaceLayout &layout)
197 {
198         layout.layout.visit(*this);
199         append(format(" %s;", layout.interface));
200 }
201
202 void Formatter::visit(StructDeclaration &strct)
203 {
204         append(format("struct %s\n", strct.name));
205         strct.members.visit(*this);
206         append(';');
207 }
208
209 void Formatter::visit(VariableDeclaration &var)
210 {
211         if(var.layout)
212         {
213                 var.layout->visit(*this);
214                 append(' ');
215         }
216         if(var.constant)
217                 append("const ");
218         if(!var.interpolation.empty())
219                 append(format("%s ", var.interpolation));
220         if(!var.sampling.empty())
221                 append(format("%s ", var.sampling));
222         if(!var.interface.empty() && var.interface!=block_interface)
223         {
224                 string interface = var.interface;
225                 if(mode==Compiler::PROGRAM && stage && stage->required_features.glsl_version<Version(1, 30))
226                 {
227                         if(stage->type==Stage::VERTEX && var.interface=="in")
228                                 interface = "attribute";
229                         else if((stage->type==Stage::VERTEX && var.interface=="out") || (stage->type==Stage::FRAGMENT && var.interface=="in"))
230                                 interface = "varying";
231                 }
232                 append(format("%s ", interface));
233         }
234         if(!var.precision.empty())
235                 append(format("%s ", var.precision));
236         append(format("%s %s", var.type, var.name));
237         if(var.array)
238         {
239                 append('[');
240                 if(var.array_size)
241                         var.array_size->visit(*this);
242                 append(']');
243         }
244         if(var.init_expression)
245         {
246                 append(" = ");
247                 var.init_expression->visit(*this);
248         }
249         if(!parameter_list)
250                 append(';');
251 }
252
253 void Formatter::visit(InterfaceBlock &iface)
254 {
255         SetForScope<string> set(block_interface, iface.interface);
256         append(format("%s %s\n", iface.interface, iface.name));
257         iface.members.visit(*this);
258         append(';');
259 }
260
261 void Formatter::visit(FunctionDeclaration &func)
262 {
263         append(format("%s %s(", func.return_type, func.name));
264         for(NodeArray<VariableDeclaration>::iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
265         {
266                 if(i!=func.parameters.begin())
267                         append(", ");
268                 SetFlag set(parameter_list);
269                 (*i)->visit(*this);
270         }
271         append(')');
272         if(func.definition==&func)
273         {
274                 append('\n');
275                 func.body.visit(*this);
276         }
277         else
278                 append(';');
279 }
280
281 void Formatter::visit(Conditional &cond)
282 {
283         append("if(");
284         cond.condition->visit(*this);
285         append(")\n");
286
287         cond.body.visit(*this);
288         if(!cond.else_body.body.empty())
289         {
290                 Conditional *else_cond = dynamic_cast<Conditional *>(cond.else_body.body.front().get());
291                 if(cond.else_body.body.size()==1 && else_cond)
292                 {
293                         append('\n');
294                         set_source(else_cond->source, else_cond->line);
295                         append(format("%selse ", string(indent*2, ' ')));
296                         else_cond->visit(*this);
297                 }
298                 else
299                 {
300                         append(format("\n%selse\n", string(indent*2, ' ')));
301                         cond.else_body.visit(*this);
302                 }
303         }
304 }
305
306 void Formatter::visit(Iteration &iter)
307 {
308         if(!iter.init_statement && iter.condition && !iter.loop_expression)
309         {
310                 append("while(");
311                 iter.condition->visit(*this);
312                 append(')');
313         }
314         else
315         {
316                 append("for(");
317                 if(iter.init_statement)
318                         iter.init_statement->visit(*this);
319                 else
320                         append(';');
321                 if(iter.condition)
322                 {
323                         append(' ');
324                         iter.condition->visit(*this);
325                 }
326                 append(';');
327                 if(iter.loop_expression)
328                 {
329                         append(' ');
330                         iter.loop_expression->visit(*this);
331                 }
332                 append(')');
333         }
334
335         if(iter.body.body.empty())
336                 append(" { }");
337         else
338         {
339                 append('\n');
340                 iter.body.visit(*this);
341         }
342 }
343
344 void Formatter::visit(Passthrough &pass)
345 {
346         append("passthrough");
347         if(pass.subscript)
348         {
349                 append('[');
350                 pass.subscript->visit(*this);
351                 append(']');
352         }
353         append(';');
354 }
355
356 void Formatter::visit(Return &ret)
357 {
358         append("return");
359         if(ret.expression)
360         {
361                 append(' ');
362                 ret.expression->visit(*this);
363         }
364         append(';');
365 }
366
367 void Formatter::visit(Jump &jump)
368 {
369         append(jump.keyword);
370         append(';');
371 }
372
373 } // namespace SL
374 } // namespace GL
375 } // namespace Msp