Debian Codegraph @

third_party/rust/glsl/src/transpiler/glsl.rs

raw
1 //! A GLSL450/GLSL460 transpiler that takes a syntax tree and writes it as a plain raw GLSL
2 //! [`String`].
3 //!
4 //! # Foreword
5 //!
6 //! This module exports several functions that just transform a part of a syntax tree into its raw
7 //! GLSL [`String`] representation.
8 //!
9 //! > Important note: this module – and actually, any [`transpiler`] module – is not responsible in
10 //! > optimizing the syntax tree nor semantically check its validity. This is done in other stages
11 //! > of the compilation process.
12 //!
13 //! In order to achieve that purpose, you could:
14 //!
15 //! - For each elements in the AST, return a [`String`] or [`Cow<str>`].
16 //! - Insert the string representation via a formatter.
17 //!
18 //! The second solution is better because it lets the user handle the memory the way they want:
19 //! they might just use a dynamic buffer that implements [`Write`] or simply pass a `&mut`
20 //! [`String`]. It’s up to you.
21 //!
22 //! # How to use this module
23 //!
24 //! First, head over to the [`syntax`] module. That module defines the AST items defined by GLSL. This
25 //! very module provides you with functions like `show_*` taking the AST item and writing it to a
26 //! [`Write`] object. You’re likely to be interested in [`show_translation_unit`] to start with.
27 //!
28 //! [`Cow<str>`]: std::borrow::Cow
29 //! [`Write`]: std::fmt::Write
30 //! [`show_translation_unit`]: crate::transpiler::glsl::show_translation_unit
31 //! [`syntax`]: crate::syntax
32 //! [`transpiler`]: crate::transpiler
33
34 use std::fmt::Write;
35
36 use crate::syntax;
37
38 // Precedence information for transpiling parentheses properly
39 trait HasPrecedence {
40 fn precedence(&self) -> u32;
41 }
42
43 impl HasPrecedence for syntax::Expr {
44 fn precedence(&self) -> u32 {
45 match self {
46 // 0 isn't a valid precedence, but we use this to represent atomic expressions
47 Self::Variable(_)
48 | Self::IntConst(_)
49 | Self::UIntConst(_)
50 | Self::BoolConst(_)
51 | Self::FloatConst(_)
52 | Self::DoubleConst(_) => 0,
53 // Precedence operator expression is precedence of operator
54 Self::Unary(op, _) => op.precedence(),
55 Self::Binary(op, _, _) => op.precedence(),
56 Self::Ternary(_, _, _) => 15,
57 Self::Assignment(_, op, _) => op.precedence(),
58 Self::Bracket(_, _)
59 | Self::FunCall(_, _)
60 | Self::Dot(_, _)
61 | Self::PostInc(_)
62 | Self::PostDec(_) => 2,
63 Self::Comma(_, _) => 17,
64 }
65 }
66 }
67
68 impl HasPrecedence for syntax::UnaryOp {
69 fn precedence(&self) -> u32 {
70 3
71 }
72 }
73
74 impl HasPrecedence for syntax::BinaryOp {
75 fn precedence(&self) -> u32 {
76 match self {
77 Self::Mult | Self::Div | Self::Mod => 4,
78 Self::Add | Self::Sub => 5,
79 Self::LShift | Self::RShift => 6,
80 Self::LT | Self::GT | Self::LTE | Self::GTE => 7,
81 Self::Equal | Self::NonEqual => 8,
82 Self::BitAnd => 9,
83 Self::BitXor => 10,
84 Self::BitOr => 11,
85 Self::And => 12,
86 Self::Xor => 13,
87 Self::Or => 14,
88 }
89 }
90 }
91
92 impl HasPrecedence for syntax::AssignmentOp {
93 fn precedence(&self) -> u32 {
94 16
95 }
96 }
97
98 pub fn show_identifier<F>(f: &mut F, i: &syntax::Identifier)
99 where
100 F: Write,
101 {
102 let _ = f.write_str(&i.0);
103 }
104
105 pub fn show_type_name<F>(f: &mut F, t: &syntax::TypeName)
106 where
107 F: Write,
108 {
109 let _ = f.write_str(&t.0);
110 }
111
112 pub fn show_type_specifier_non_array<F>(f: &mut F, t: &syntax::TypeSpecifierNonArray)
113 where
114 F: Write,
115 {
116 match *t {
117 syntax::TypeSpecifierNonArray::Void => {
118 let _ = f.write_str("void");
119 }
120 syntax::TypeSpecifierNonArray::Bool => {
121 let _ = f.write_str("bool");
122 }
123 syntax::TypeSpecifierNonArray::Int => {
124 let _ = f.write_str("int");
125 }
126 syntax::TypeSpecifierNonArray::UInt => {
127 let _ = f.write_str("uint");
128 }
129 syntax::TypeSpecifierNonArray::Float => {
130 let _ = f.write_str("float");
131 }
132 syntax::TypeSpecifierNonArray::Double => {
133 let _ = f.write_str("double");
134 }
135 syntax::TypeSpecifierNonArray::Vec2 => {
136 let _ = f.write_str("vec2");
137 }
138 syntax::TypeSpecifierNonArray::Vec3 => {
139 let _ = f.write_str("vec3");
140 }
141 syntax::TypeSpecifierNonArray::Vec4 => {
142 let _ = f.write_str("vec4");
143 }
144 syntax::TypeSpecifierNonArray::DVec2 => {
145 let _ = f.write_str("dvec2");
146 }
147 syntax::TypeSpecifierNonArray::DVec3 => {
148 let _ = f.write_str("dvec3");
149 }
150 syntax::TypeSpecifierNonArray::DVec4 => {
151 let _ = f.write_str("dvec4");
152 }
153 syntax::TypeSpecifierNonArray::BVec2 => {
154 let _ = f.write_str("bvec2");
155 }
156 syntax::TypeSpecifierNonArray::BVec3 => {
157 let _ = f.write_str("bvec3");
158 }
159 syntax::TypeSpecifierNonArray::BVec4 => {
160 let _ = f.write_str("bvec4");
161 }
162 syntax::TypeSpecifierNonArray::IVec2 => {
163 let _ = f.write_str("ivec2");
164 }
165 syntax::TypeSpecifierNonArray::IVec3 => {
166 let _ = f.write_str("ivec3");
167 }
168 syntax::TypeSpecifierNonArray::IVec4 => {
169 let _ = f.write_str("ivec4");
170 }
171 syntax::TypeSpecifierNonArray::UVec2 => {
172 let _ = f.write_str("uvec2");
173 }
174 syntax::TypeSpecifierNonArray::UVec3 => {
175 let _ = f.write_str("uvec3");
176 }
177 syntax::TypeSpecifierNonArray::UVec4 => {
178 let _ = f.write_str("uvec4");
179 }
180 syntax::TypeSpecifierNonArray::Mat2 => {
181 let _ = f.write_str("mat2");
182 }
183 syntax::TypeSpecifierNonArray::Mat3 => {
184 let _ = f.write_str("mat3");
185 }
186 syntax::TypeSpecifierNonArray::Mat4 => {
187 let _ = f.write_str("mat4");
188 }
189 syntax::TypeSpecifierNonArray::Mat23 => {
190 let _ = f.write_str("mat2x3");
191 }
192 syntax::TypeSpecifierNonArray::Mat24 => {
193 let _ = f.write_str("mat2x4");
194 }
195 syntax::TypeSpecifierNonArray::Mat32 => {
196 let _ = f.write_str("mat3x2");
197 }
198 syntax::TypeSpecifierNonArray::Mat34 => {
199 let _ = f.write_str("mat3x4");
200 }
201 syntax::TypeSpecifierNonArray::Mat42 => {
202 let _ = f.write_str("mat4x2");
203 }
204 syntax::TypeSpecifierNonArray::Mat43 => {
205 let _ = f.write_str("mat4x3");
206 }
207 syntax::TypeSpecifierNonArray::DMat2 => {
208 let _ = f.write_str("dmat2");
209 }
210 syntax::TypeSpecifierNonArray::DMat3 => {
211 let _ = f.write_str("dmat3");
212 }
213 syntax::TypeSpecifierNonArray::DMat4 => {
214 let _ = f.write_str("dmat4");
215 }
216 syntax::TypeSpecifierNonArray::DMat23 => {
217 let _ = f.write_str("dmat2x3");
218 }
219 syntax::TypeSpecifierNonArray::DMat24 => {
220 let _ = f.write_str("dmat2x4");
221 }
222 syntax::TypeSpecifierNonArray::DMat32 => {
223 let _ = f.write_str("dmat3x2");
224 }
225 syntax::TypeSpecifierNonArray::DMat34 => {
226 let _ = f.write_str("dmat3x4");
227 }
228 syntax::TypeSpecifierNonArray::DMat42 => {
229 let _ = f.write_str("dmat4x2");
230 }
231 syntax::TypeSpecifierNonArray::DMat43 => {
232 let _ = f.write_str("dmat4x3");
233 }
234 syntax::TypeSpecifierNonArray::Sampler1D => {
235 let _ = f.write_str("sampler1D");
236 }
237 syntax::TypeSpecifierNonArray::Image1D => {
238 let _ = f.write_str("image1D");
239 }
240 syntax::TypeSpecifierNonArray::Sampler2D => {
241 let _ = f.write_str("sampler2D");
242 }
243 syntax::TypeSpecifierNonArray::Image2D => {
244 let _ = f.write_str("image2D");
245 }
246 syntax::TypeSpecifierNonArray::Sampler3D => {
247 let _ = f.write_str("sampler3D");
248 }
249 syntax::TypeSpecifierNonArray::Image3D => {
250 let _ = f.write_str("image3D");
251 }
252 syntax::TypeSpecifierNonArray::SamplerCube => {
253 let _ = f.write_str("samplerCube");
254 }
255 syntax::TypeSpecifierNonArray::ImageCube => {
256 let _ = f.write_str("imageCube");
257 }
258 syntax::TypeSpecifierNonArray::Sampler2DRect => {
259 let _ = f.write_str("sampler2DRect");
260 }
261 syntax::TypeSpecifierNonArray::Image2DRect => {
262 let _ = f.write_str("image2DRect");
263 }
264 syntax::TypeSpecifierNonArray::Sampler1DArray => {
265 let _ = f.write_str("sampler1DArray");
266 }
267 syntax::TypeSpecifierNonArray::Image1DArray => {
268 let _ = f.write_str("image1DArray");
269 }
270 syntax::TypeSpecifierNonArray::Sampler2DArray => {
271 let _ = f.write_str("sampler2DArray");
272 }
273 syntax::TypeSpecifierNonArray::Image2DArray => {
274 let _ = f.write_str("image2DArray");
275 }
276 syntax::TypeSpecifierNonArray::SamplerBuffer => {
277 let _ = f.write_str("samplerBuffer");
278 }
279 syntax::TypeSpecifierNonArray::ImageBuffer => {
280 let _ = f.write_str("imageBuffer");
281 }
282 syntax::TypeSpecifierNonArray::Sampler2DMS => {
283 let _ = f.write_str("sampler2DMS");
284 }
285 syntax::TypeSpecifierNonArray::Image2DMS => {
286 let _ = f.write_str("image2DMS");
287 }
288 syntax::TypeSpecifierNonArray::Sampler2DMSArray => {
289 let _ = f.write_str("sampler2DMSArray");
290 }
291 syntax::TypeSpecifierNonArray::Image2DMSArray => {
292 let _ = f.write_str("image2DMSArray");
293 }
294 syntax::TypeSpecifierNonArray::SamplerCubeArray => {
295 let _ = f.write_str("samplerCubeArray");
296 }
297 syntax::TypeSpecifierNonArray::ImageCubeArray => {
298 let _ = f.write_str("imageCubeArray");
299 }
300 syntax::TypeSpecifierNonArray::Sampler1DShadow => {
301 let _ = f.write_str("sampler1DShadow");
302 }
303 syntax::TypeSpecifierNonArray::Sampler2DShadow => {
304 let _ = f.write_str("sampler2DShadow");
305 }
306 syntax::TypeSpecifierNonArray::Sampler2DRectShadow => {
307 let _ = f.write_str("sampler2DRectShadow");
308 }
309 syntax::TypeSpecifierNonArray::Sampler1DArrayShadow => {
310 let _ = f.write_str("sampler1DArrayShadow");
311 }
312 syntax::TypeSpecifierNonArray::Sampler2DArrayShadow => {
313 let _ = f.write_str("sampler2DArrayShadow");
314 }
315 syntax::TypeSpecifierNonArray::SamplerCubeShadow => {
316 let _ = f.write_str("samplerCubeShadow");
317 }
318 syntax::TypeSpecifierNonArray::SamplerCubeArrayShadow => {
319 let _ = f.write_str("samplerCubeArrayShadow");
320 }
321 syntax::TypeSpecifierNonArray::ISampler1D => {
322 let _ = f.write_str("isampler1D");
323 }
324 syntax::TypeSpecifierNonArray::IImage1D => {
325 let _ = f.write_str("iimage1D");
326 }
327 syntax::TypeSpecifierNonArray::ISampler2D => {
328 let _ = f.write_str("isampler2D");
329 }
330 syntax::TypeSpecifierNonArray::IImage2D => {
331 let _ = f.write_str("iimage2D");
332 }
333 syntax::TypeSpecifierNonArray::ISampler3D => {
334 let _ = f.write_str("isampler3D");
335 }
336 syntax::TypeSpecifierNonArray::IImage3D => {
337 let _ = f.write_str("iimage3D");
338 }
339 syntax::TypeSpecifierNonArray::ISamplerCube => {
340 let _ = f.write_str("isamplerCube");
341 }
342 syntax::TypeSpecifierNonArray::IImageCube => {
343 let _ = f.write_str("iimageCube");
344 }
345 syntax::TypeSpecifierNonArray::ISampler2DRect => {
346 let _ = f.write_str("isampler2DRect");
347 }
348 syntax::TypeSpecifierNonArray::IImage2DRect => {
349 let _ = f.write_str("iimage2DRect");
350 }
351 syntax::TypeSpecifierNonArray::ISampler1DArray => {
352 let _ = f.write_str("isampler1DArray");
353 }
354 syntax::TypeSpecifierNonArray::IImage1DArray => {
355 let _ = f.write_str("iimage1DArray");
356 }
357 syntax::TypeSpecifierNonArray::ISampler2DArray => {
358 let _ = f.write_str("isampler2DArray");
359 }
360 syntax::TypeSpecifierNonArray::IImage2DArray => {
361 let _ = f.write_str("iimage2DArray");
362 }
363 syntax::TypeSpecifierNonArray::ISamplerBuffer => {
364 let _ = f.write_str("isamplerBuffer");
365 }
366 syntax::TypeSpecifierNonArray::IImageBuffer => {
367 let _ = f.write_str("iimageBuffer");
368 }
369 syntax::TypeSpecifierNonArray::ISampler2DMS => {
370 let _ = f.write_str("isampler2MS");
371 }
372 syntax::TypeSpecifierNonArray::IImage2DMS => {
373 let _ = f.write_str("iimage2DMS");
374 }
375 syntax::TypeSpecifierNonArray::ISampler2DMSArray => {
376 let _ = f.write_str("isampler2DMSArray");
377 }
378 syntax::TypeSpecifierNonArray::IImage2DMSArray => {
379 let _ = f.write_str("iimage2DMSArray");
380 }
381 syntax::TypeSpecifierNonArray::ISamplerCubeArray => {
382 let _ = f.write_str("isamplerCubeArray");
383 }
384 syntax::TypeSpecifierNonArray::IImageCubeArray => {
385 let _ = f.write_str("iimageCubeArray");
386 }
387 syntax::TypeSpecifierNonArray::AtomicUInt => {
388 let _ = f.write_str("atomic_uint");
389 }
390 syntax::TypeSpecifierNonArray::USampler1D => {
391 let _ = f.write_str("usampler1D");
392 }
393 syntax::TypeSpecifierNonArray::UImage1D => {
394 let _ = f.write_str("uimage1D");
395 }
396 syntax::TypeSpecifierNonArray::USampler2D => {
397 let _ = f.write_str("usampler2D");
398 }
399 syntax::TypeSpecifierNonArray::UImage2D => {
400 let _ = f.write_str("uimage2D");
401 }
402 syntax::TypeSpecifierNonArray::USampler3D => {
403 let _ = f.write_str("usampler3D");
404 }
405 syntax::TypeSpecifierNonArray::UImage3D => {
406 let _ = f.write_str("uimage3D");
407 }
408 syntax::TypeSpecifierNonArray::USamplerCube => {
409 let _ = f.write_str("usamplerCube");
410 }
411 syntax::TypeSpecifierNonArray::UImageCube => {
412 let _ = f.write_str("uimageCube");
413 }
414 syntax::TypeSpecifierNonArray::USampler2DRect => {
415 let _ = f.write_str("usampler2DRect");
416 }
417 syntax::TypeSpecifierNonArray::UImage2DRect => {
418 let _ = f.write_str("uimage2DRect");
419 }
420 syntax::TypeSpecifierNonArray::USampler1DArray => {
421 let _ = f.write_str("usampler1DArray");
422 }
423 syntax::TypeSpecifierNonArray::UImage1DArray => {
424 let _ = f.write_str("uimage1DArray");
425 }
426 syntax::TypeSpecifierNonArray::USampler2DArray => {
427 let _ = f.write_str("usampler2DArray");
428 }
429 syntax::TypeSpecifierNonArray::UImage2DArray => {
430 let _ = f.write_str("uimage2DArray");
431 }
432 syntax::TypeSpecifierNonArray::USamplerBuffer => {
433 let _ = f.write_str("usamplerBuffer");
434 }
435 syntax::TypeSpecifierNonArray::UImageBuffer => {
436 let _ = f.write_str("uimageBuffer");
437 }
438 syntax::TypeSpecifierNonArray::USampler2DMS => {
439 let _ = f.write_str("usampler2DMS");
440 }
441 syntax::TypeSpecifierNonArray::UImage2DMS => {
442 let _ = f.write_str("uimage2DMS");
443 }
444 syntax::TypeSpecifierNonArray::USampler2DMSArray => {
445 let _ = f.write_str("usamplerDMSArray");
446 }
447 syntax::TypeSpecifierNonArray::UImage2DMSArray => {
448 let _ = f.write_str("uimage2DMSArray");
449 }
450 syntax::TypeSpecifierNonArray::USamplerCubeArray => {
451 let _ = f.write_str("usamplerCubeArray");
452 }
453 syntax::TypeSpecifierNonArray::UImageCubeArray => {
454 let _ = f.write_str("uimageCubeArray");
455 }
456 syntax::TypeSpecifierNonArray::Struct(ref s) => show_struct_non_declaration(f, s),
457 syntax::TypeSpecifierNonArray::TypeName(ref tn) => show_type_name(f, tn),
458 }
459 }
460
461 pub fn show_type_specifier<F>(f: &mut F, t: &syntax::TypeSpecifier)
462 where
463 F: Write,
464 {
465 show_type_specifier_non_array(f, &t.ty);
466
467 if let Some(ref arr_spec) = t.array_specifier {
468 show_array_spec(f, arr_spec);
469 }
470 }
471
472 pub fn show_fully_specified_type<F>(f: &mut F, t: &syntax::FullySpecifiedType)
473 where
474 F: Write,
475 {
476 if let Some(ref qual) = t.qualifier {
477 show_type_qualifier(f, &qual);
478 let _ = f.write_str(" ");
479 }
480
481 show_type_specifier(f, &t.ty);
482 }
483
484 pub fn show_struct_non_declaration<F>(f: &mut F, s: &syntax::StructSpecifier)
485 where
486 F: Write,
487 {
488 let _ = f.write_str("struct ");
489
490 if let Some(ref name) = s.name {
491 let _ = write!(f, "{} ", name);
492 }
493
494 let _ = f.write_str("{\n");
495
496 for field in &s.fields.0 {
497 show_struct_field(f, field);
498 }
499
500 let _ = f.write_str("}");
501 }
502
503 pub fn show_struct<F>(f: &mut F, s: &syntax::StructSpecifier)
504 where
505 F: Write,
506 {
507 show_struct_non_declaration(f, s);
508 let _ = f.write_str(";\n");
509 }
510
511 pub fn show_struct_field<F>(f: &mut F, field: &syntax::StructFieldSpecifier)
512 where
513 F: Write,
514 {
515 if let Some(ref qual) = field.qualifier {
516 show_type_qualifier(f, &qual);
517 let _ = f.write_str(" ");
518 }
519
520 show_type_specifier(f, &field.ty);
521 let _ = f.write_str(" ");
522
523 // there’s at least one identifier
524 let mut identifiers = field.identifiers.0.iter();
525 let identifier = identifiers.next().unwrap();
526
527 show_arrayed_identifier(f, identifier);
528
529 // write the rest of the identifiers
530 for identifier in identifiers {
531 let _ = f.write_str(", ");
532 show_arrayed_identifier(f, identifier);
533 }
534
535 let _ = f.write_str(";\n");
536 }
537
538 pub fn show_array_spec<F>(f: &mut F, a: &syntax::ArraySpecifier)
539 where
540 F: Write,
541 {
542 for dimension in &a.dimensions {
543 match *dimension {
544 syntax::ArraySpecifierDimension::Unsized => {
545 let _ = f.write_str("[]");
546 }
547 syntax::ArraySpecifierDimension::ExplicitlySized(ref e) => {
548 let _ = f.write_str("[");
549 show_expr(f, &e);
550 let _ = f.write_str("]");
551 }
552 }
553 }
554 }
555
556 pub fn show_arrayed_identifier<F>(f: &mut F, a: &syntax::ArrayedIdentifier)
557 where
558 F: Write,
559 {
560 let _ = write!(f, "{}", a.ident);
561
562 if let Some(ref arr_spec) = a.array_spec {
563 show_array_spec(f, arr_spec);
564 }
565 }
566
567 pub fn show_type_qualifier<F>(f: &mut F, q: &syntax::TypeQualifier)
568 where
569 F: Write,
570 {
571 let mut qualifiers = q.qualifiers.0.iter();
572 let first = qualifiers.next().unwrap();
573
574 show_type_qualifier_spec(f, first);
575
576 for qual_spec in qualifiers {
577 let _ = f.write_str(" ");
578 show_type_qualifier_spec(f, qual_spec)
579 }
580 }
581
582 pub fn show_type_qualifier_spec<F>(f: &mut F, q: &syntax::TypeQualifierSpec)
583 where
584 F: Write,
585 {
586 match *q {
587 syntax::TypeQualifierSpec::Storage(ref s) => show_storage_qualifier(f, &s),
588 syntax::TypeQualifierSpec::Layout(ref l) => show_layout_qualifier(f, &l),
589 syntax::TypeQualifierSpec::Precision(ref p) => show_precision_qualifier(f, &p),
590 syntax::TypeQualifierSpec::Interpolation(ref i) => show_interpolation_qualifier(f, &i),
591 syntax::TypeQualifierSpec::Invariant => {
592 let _ = f.write_str("invariant");
593 }
594 syntax::TypeQualifierSpec::Precise => {
595 let _ = f.write_str("precise");
596 }
597 }
598 }
599
600 pub fn show_storage_qualifier<F>(f: &mut F, q: &syntax::StorageQualifier)
601 where
602 F: Write,
603 {
604 match *q {
605 syntax::StorageQualifier::Const => {
606 let _ = f.write_str("const");
607 }
608 syntax::StorageQualifier::InOut => {
609 let _ = f.write_str("inout");
610 }
611 syntax::StorageQualifier::In => {
612 let _ = f.write_str("in");
613 }
614 syntax::StorageQualifier::Out => {
615 let _ = f.write_str("out");
616 }
617 syntax::StorageQualifier::Centroid => {
618 let _ = f.write_str("centroid");
619 }
620 syntax::StorageQualifier::Patch => {
621 let _ = f.write_str("patch");
622 }
623 syntax::StorageQualifier::Sample => {
624 let _ = f.write_str("sample");
625 }
626 syntax::StorageQualifier::Uniform => {
627 let _ = f.write_str("uniform");
628 }
629 syntax::StorageQualifier::Attribute => {
630 let _ = f.write_str("attribute");
631 }
632 syntax::StorageQualifier::Varying => {
633 let _ = f.write_str("varying");
634 }
635 syntax::StorageQualifier::Buffer => {
636 let _ = f.write_str("buffer");
637 }
638 syntax::StorageQualifier::Shared => {
639 let _ = f.write_str("shared");
640 }
641 syntax::StorageQualifier::Coherent => {
642 let _ = f.write_str("coherent");
643 }
644 syntax::StorageQualifier::Volatile => {
645 let _ = f.write_str("volatile");
646 }
647 syntax::StorageQualifier::Restrict => {
648 let _ = f.write_str("restrict");
649 }
650 syntax::StorageQualifier::ReadOnly => {
651 let _ = f.write_str("readonly");
652 }
653 syntax::StorageQualifier::WriteOnly => {
654 let _ = f.write_str("writeonly");
655 }
656 syntax::StorageQualifier::Subroutine(ref n) => show_subroutine(f, &n),
657 }
658 }
659
660 pub fn show_subroutine<F>(f: &mut F, types: &Vec<syntax::TypeName>)
661 where
662 F: Write,
663 {
664 let _ = f.write_str("subroutine");
665
666 if !types.is_empty() {
667 let _ = f.write_str("(");
668
669 let mut types_iter = types.iter();
670 let first = types_iter.next().unwrap();
671
672 show_type_name(f, first);
673
674 for type_name in types_iter {
675 let _ = f.write_str(", ");
676 show_type_name(f, type_name);
677 }
678
679 let _ = f.write_str(")");
680 }
681 }
682
683 pub fn show_layout_qualifier<F>(f: &mut F, l: &syntax::LayoutQualifier)
684 where
685 F: Write,
686 {
687 let mut qualifiers = l.ids.0.iter();
688 let first = qualifiers.next().unwrap();
689
690 let _ = f.write_str("layout (");
691 show_layout_qualifier_spec(f, first);
692
693 for qual_spec in qualifiers {
694 let _ = f.write_str(", ");
695 show_layout_qualifier_spec(f, qual_spec);
696 }
697
698 let _ = f.write_str(")");
699 }
700
701 pub fn show_layout_qualifier_spec<F>(f: &mut F, l: &syntax::LayoutQualifierSpec)
702 where
703 F: Write,
704 {
705 match *l {
706 syntax::LayoutQualifierSpec::Identifier(ref i, Some(ref e)) => {
707 let _ = write!(f, "{} = ", i);
708 show_expr(f, &e);
709 }
710 syntax::LayoutQualifierSpec::Identifier(ref i, None) => show_identifier(f, &i),
711 syntax::LayoutQualifierSpec::Shared => {
712 let _ = f.write_str("shared");
713 }
714 }
715 }
716
717 pub fn show_precision_qualifier<F>(f: &mut F, p: &syntax::PrecisionQualifier)
718 where
719 F: Write,
720 {
721 match *p {
722 syntax::PrecisionQualifier::High => {
723 let _ = f.write_str("highp");
724 }
725 syntax::PrecisionQualifier::Medium => {
726 let _ = f.write_str("mediump");
727 }
728 syntax::PrecisionQualifier::Low => {
729 let _ = f.write_str("low");
730 }
731 }
732 }
733
734 pub fn show_interpolation_qualifier<F>(f: &mut F, i: &syntax::InterpolationQualifier)
735 where
736 F: Write,
737 {
738 match *i {
739 syntax::InterpolationQualifier::Smooth => {
740 let _ = f.write_str("smooth");
741 }
742 syntax::InterpolationQualifier::Flat => {
743 let _ = f.write_str("flat");
744 }
745 syntax::InterpolationQualifier::NoPerspective => {
746 let _ = f.write_str("noperspective");
747 }
748 }
749 }
750
751 pub fn show_float<F>(f: &mut F, x: f32)
752 where
753 F: Write,
754 {
755 if x.fract() == 0. {
756 let _ = write!(f, "{}.", x);
757 } else {
758 let _ = write!(f, "{}", x);
759 }
760 }
761
762 pub fn show_double<F>(f: &mut F, x: f64)
763 where
764 F: Write,
765 {
766 if x.fract() == 0. {
767 let _ = write!(f, "{}.lf", x);
768 } else {
769 let _ = write!(f, "{}lf", x);
770 }
771 }
772
773 pub fn show_expr<F>(f: &mut F, expr: &syntax::Expr)
774 where
775 F: Write,
776 {
777 match *expr {
778 syntax::Expr::Variable(ref i) => show_identifier(f, &i),
779 syntax::Expr::IntConst(ref x) => {
780 let _ = write!(f, "{}", x);
781 }
782 syntax::Expr::UIntConst(ref x) => {
783 let _ = write!(f, "{}u", x);
784 }
785 syntax::Expr::BoolConst(ref x) => {
786 let _ = write!(f, "{}", x);
787 }
788 syntax::Expr::FloatConst(ref x) => show_float(f, *x),
789 syntax::Expr::DoubleConst(ref x) => show_double(f, *x),
790 syntax::Expr::Unary(ref op, ref e) => {
791 // Note: all unary ops are right-to-left associative
792 show_unary_op(f, &op);
793
794 if e.precedence() > op.precedence() {
795 let _ = f.write_str("(");
796 show_expr(f, &e);
797 let _ = f.write_str(")");
798 } else if let syntax::Expr::Unary(eop, _) = &**e {
799 // Prevent double-unary plus/minus turning into inc/dec
800 if eop == op && (*eop == syntax::UnaryOp::Add || *eop == syntax::UnaryOp::Minus) {
801 let _ = f.write_str("(");
802 show_expr(f, &e);
803 let _ = f.write_str(")");
804 } else {
805 show_expr(f, &e);
806 }
807 } else {
808 show_expr(f, &e);
809 }
810 }
811 syntax::Expr::Binary(ref op, ref l, ref r) => {
812 // Note: all binary ops are left-to-right associative (<= for left part)
813
814 if l.precedence() <= op.precedence() {
815 show_expr(f, &l);
816 } else {
817 let _ = f.write_str("(");
818 show_expr(f, &l);
819 let _ = f.write_str(")");
820 }
821
822 show_binary_op(f, &op);
823
824 if r.precedence() < op.precedence() {
825 show_expr(f, &r);
826 } else {
827 let _ = f.write_str("(");
828 show_expr(f, &r);
829 let _ = f.write_str(")");
830 }
831 }
832 syntax::Expr::Ternary(ref c, ref s, ref e) => {
833 // Note: ternary is right-to-left associative (<= for right part)
834
835 if c.precedence() < expr.precedence() {
836 show_expr(f, &c);
837 } else {
838 let _ = f.write_str("(");
839 show_expr(f, &c);
840 let _ = f.write_str(")");
841 }
842 let _ = f.write_str(" ? ");
843 show_expr(f, &s);
844 let _ = f.write_str(" : ");
845 if e.precedence() <= expr.precedence() {
846 show_expr(f, &e);
847 } else {
848 let _ = f.write_str("(");
849 show_expr(f, &e);
850 let _ = f.write_str(")");
851 }
852 }
853 syntax::Expr::Assignment(ref v, ref op, ref e) => {
854 // Note: all assignment ops are right-to-left associative
855
856 if v.precedence() < op.precedence() {
857 show_expr(f, &v);
858 } else {
859 let _ = f.write_str("(");
860 show_expr(f, &v);
861 let _ = f.write_str(")");
862 }
863
864 let _ = f.write_str(" ");
865 show_assignment_op(f, &op);
866 let _ = f.write_str(" ");
867
868 if e.precedence() <= op.precedence() {
869 show_expr(f, &e);
870 } else {
871 let _ = f.write_str("(");
872 show_expr(f, &e);
873 let _ = f.write_str(")");
874 }
875 }
876 syntax::Expr::Bracket(ref e, ref a) => {
877 // Note: bracket is left-to-right associative
878
879 if e.precedence() <= expr.precedence() {
880 show_expr(f, &e);
881 } else {
882 let _ = f.write_str("(");
883 show_expr(f, &e);
884 let _ = f.write_str(")");
885 }
886
887 show_array_spec(f, &a);
888 }
889 syntax::Expr::FunCall(ref fun, ref args) => {
890 show_function_identifier(f, &fun);
891 let _ = f.write_str("(");
892
893 if !args.is_empty() {
894 let mut args_iter = args.iter();
895 let first = args_iter.next().unwrap();
896 show_expr(f, first);
897
898 for e in args_iter {
899 let _ = f.write_str(", ");
900 show_expr(f, e);
901 }
902 }
903
904 let _ = f.write_str(")");
905 }
906 syntax::Expr::Dot(ref e, ref i) => {
907 // Note: dot is left-to-right associative
908
909 if e.precedence() <= expr.precedence() {
910 show_expr(f, &e);
911 } else {
912 let _ = f.write_str("(");
913 show_expr(f, &e);
914 let _ = f.write_str(")");
915 }
916 let _ = f.write_str(".");
917 show_identifier(f, &i);
918 }
919 syntax::Expr::PostInc(ref e) => {
920 // Note: post-increment is right-to-left associative
921
922 if e.precedence() < expr.precedence() {
923 show_expr(f, &e);
924 } else {
925 let _ = f.write_str("(");
926 show_expr(f, &e);
927 let _ = f.write_str(")");
928 }
929
930 let _ = f.write_str("++");
931 }
932 syntax::Expr::PostDec(ref e) => {
933 // Note: post-decrement is right-to-left associative
934
935 if e.precedence() < expr.precedence() {
936 show_expr(f, &e);
937 } else {
938 let _ = f.write_str("(");
939 show_expr(f, &e);
940 let _ = f.write_str(")");
941 }
942
943 let _ = f.write_str("--");
944 }
945 syntax::Expr::Comma(ref a, ref b) => {
946 // Note: comma is left-to-right associative
947
948 if a.precedence() <= expr.precedence() {
949 show_expr(f, &a);
950 } else {
951 let _ = f.write_str("(");
952 show_expr(f, &a);
953 let _ = f.write_str(")");
954 }
955
956 let _ = f.write_str(", ");
957
958 if b.precedence() < expr.precedence() {
959 show_expr(f, &b);
960 } else {
961 let _ = f.write_str("(");
962 show_expr(f, &b);
963 let _ = f.write_str(")");
964 }
965 }
966 }
967 }
968
969 pub fn show_path<F>(f: &mut F, path: &syntax::Path)
970 where
971 F: Write,
972 {
973 match path {
974 syntax::Path::Absolute(s) => {
975 let _ = write!(f, "<{}>", s);
976 }
977 syntax::Path::Relative(s) => {
978 let _ = write!(f, "\"{}\"", s);
979 }
980 }
981 }
982
983 pub fn show_unary_op<F>(f: &mut F, op: &syntax::UnaryOp)
984 where
985 F: Write,
986 {
987 match *op {
988 syntax::UnaryOp::Inc => {
989 let _ = f.write_str("++");
990 }
991 syntax::UnaryOp::Dec => {
992 let _ = f.write_str("--");
993 }
994 syntax::UnaryOp::Add => {
995 let _ = f.write_str("+");
996 }
997 syntax::UnaryOp::Minus => {
998 let _ = f.write_str("-");
999 }
1000 syntax::UnaryOp::Not => {
1001 let _ = f.write_str("!");
1002 }
1003 syntax::UnaryOp::Complement => {
1004 let _ = f.write_str("~");
1005 }
1006 }
1007 }
1008
1009 pub fn show_binary_op<F>(f: &mut F, op: &syntax::BinaryOp)
1010 where
1011 F: Write,
1012 {
1013 match *op {
1014 syntax::BinaryOp::Or => {
1015 let _ = f.write_str("||");
1016 }
1017 syntax::BinaryOp::Xor => {
1018 let _ = f.write_str("^^");
1019 }
1020 syntax::BinaryOp::And => {
1021 let _ = f.write_str("&&");
1022 }
1023 syntax::BinaryOp::BitOr => {
1024 let _ = f.write_str("|");
1025 }
1026 syntax::BinaryOp::BitXor => {
1027 let _ = f.write_str("^");
1028 }
1029 syntax::BinaryOp::BitAnd => {
1030 let _ = f.write_str("&");
1031 }
1032 syntax::BinaryOp::Equal => {
1033 let _ = f.write_str("==");
1034 }
1035 syntax::BinaryOp::NonEqual => {
1036 let _ = f.write_str("!=");
1037 }
1038 syntax::BinaryOp::LT => {
1039 let _ = f.write_str("<");
1040 }
1041 syntax::BinaryOp::GT => {
1042 let _ = f.write_str(">");
1043 }
1044 syntax::BinaryOp::LTE => {
1045 let _ = f.write_str("<=");
1046 }
1047 syntax::BinaryOp::GTE => {
1048 let _ = f.write_str(">=");
1049 }
1050 syntax::BinaryOp::LShift => {
1051 let _ = f.write_str("<<");
1052 }
1053 syntax::BinaryOp::RShift => {
1054 let _ = f.write_str(">>");
1055 }
1056 syntax::BinaryOp::Add => {
1057 let _ = f.write_str("+");
1058 }
1059 syntax::BinaryOp::Sub => {
1060 let _ = f.write_str("-");
1061 }
1062 syntax::BinaryOp::Mult => {
1063 let _ = f.write_str("*");
1064 }
1065 syntax::BinaryOp::Div => {
1066 let _ = f.write_str("/");
1067 }
1068 syntax::BinaryOp::Mod => {
1069 let _ = f.write_str("%");
1070 }
1071 }
1072 }
1073
1074 pub fn show_assignment_op<F>(f: &mut F, op: &syntax::AssignmentOp)
1075 where
1076 F: Write,
1077 {
1078 match *op {
1079 syntax::AssignmentOp::Equal => {
1080 let _ = f.write_str("=");
1081 }
1082 syntax::AssignmentOp::Mult => {
1083 let _ = f.write_str("*=");
1084 }
1085 syntax::AssignmentOp::Div => {
1086 let _ = f.write_str("/=");
1087 }
1088 syntax::AssignmentOp::Mod => {
1089 let _ = f.write_str("%=");
1090 }
1091 syntax::AssignmentOp::Add => {
1092 let _ = f.write_str("+=");
1093 }
1094 syntax::AssignmentOp::Sub => {
1095 let _ = f.write_str("-=");
1096 }
1097 syntax::AssignmentOp::LShift => {
1098 let _ = f.write_str("<<=");
1099 }
1100 syntax::AssignmentOp::RShift => {
1101 let _ = f.write_str(">>=");
1102 }
1103 syntax::AssignmentOp::And => {
1104 let _ = f.write_str("&=");
1105 }
1106 syntax::AssignmentOp::Xor => {
1107 let _ = f.write_str("^=");
1108 }
1109 syntax::AssignmentOp::Or => {
1110 let _ = f.write_str("|=");
1111 }
1112 }
1113 }
1114
1115 pub fn show_function_identifier<F>(f: &mut F, i: &syntax::FunIdentifier)
1116 where
1117 F: Write,
1118 {
1119 match *i {
1120 syntax::FunIdentifier::Identifier(ref n) => show_identifier(f, &n),
1121 syntax::FunIdentifier::Expr(ref e) => show_expr(f, &*e),
1122 }
1123 }
1124
1125 pub fn show_declaration<F>(f: &mut F, d: &syntax::Declaration)
1126 where
1127 F: Write,
1128 {
1129 match *d {
1130 syntax::Declaration::FunctionPrototype(ref proto) => {
1131 show_function_prototype(f, &proto);
1132 let _ = f.write_str(";\n");
1133 }
1134 syntax::Declaration::InitDeclaratorList(ref list) => {
1135 show_init_declarator_list(f, &list);
1136 let _ = f.write_str(";\n");
1137 }
1138 syntax::Declaration::Precision(ref qual, ref ty) => {
1139 show_precision_qualifier(f, &qual);
1140 show_type_specifier(f, &ty);
1141 let _ = f.write_str(";\n");
1142 }
1143 syntax::Declaration::Block(ref block) => {
1144 show_block(f, &block);
1145 let _ = f.write_str(";\n");
1146 }
1147 syntax::Declaration::Global(ref qual, ref identifiers) => {
1148 show_type_qualifier(f, &qual);
1149
1150 if !identifiers.is_empty() {
1151 let mut iter = identifiers.iter();
1152 let first = iter.next().unwrap();
1153 show_identifier(f, first);
1154
1155 for identifier in iter {
1156 let _ = write!(f, ", {}", identifier);
1157 }
1158 }
1159
1160 let _ = f.write_str(";\n");
1161 }
1162 }
1163 }
1164
1165 pub fn show_function_prototype<F>(f: &mut F, fp: &syntax::FunctionPrototype)
1166 where
1167 F: Write,
1168 {
1169 show_fully_specified_type(f, &fp.ty);
1170 let _ = f.write_str(" ");
1171 show_identifier(f, &fp.name);
1172
1173 let _ = f.write_str("(");
1174
1175 if !fp.parameters.is_empty() {
1176 let mut iter = fp.parameters.iter();
1177 let first = iter.next().unwrap();
1178 show_function_parameter_declaration(f, first);
1179
1180 for param in iter {
1181 let _ = f.write_str(", ");
1182 show_function_parameter_declaration(f, param);
1183 }
1184 }
1185
1186 let _ = f.write_str(")");
1187 }
1188 pub fn show_function_parameter_declaration<F>(f: &mut F, p: &syntax::FunctionParameterDeclaration)
1189 where
1190 F: Write,
1191 {
1192 match *p {
1193 syntax::FunctionParameterDeclaration::Named(ref qual, ref fpd) => {
1194 if let Some(ref q) = *qual {
1195 show_type_qualifier(f, q);
1196 let _ = f.write_str(" ");
1197 }
1198
1199 show_function_parameter_declarator(f, fpd);
1200 }
1201 syntax::FunctionParameterDeclaration::Unnamed(ref qual, ref ty) => {
1202 if let Some(ref q) = *qual {
1203 show_type_qualifier(f, q);
1204 let _ = f.write_str(" ");
1205 }
1206
1207 show_type_specifier(f, ty);
1208 }
1209 }
1210 }
1211
1212 pub fn show_function_parameter_declarator<F>(f: &mut F, p: &syntax::FunctionParameterDeclarator)
1213 where
1214 F: Write,
1215 {
1216 show_type_specifier(f, &p.ty);
1217 let _ = f.write_str(" ");
1218 show_arrayed_identifier(f, &p.ident);
1219 }
1220
1221 pub fn show_init_declarator_list<F>(f: &mut F, i: &syntax::InitDeclaratorList)
1222 where
1223 F: Write,
1224 {
1225 show_single_declaration(f, &i.head);
1226
1227 for decl in &i.tail {
1228 let _ = f.write_str(", ");
1229 show_single_declaration_no_type(f, decl);
1230 }
1231 }
1232
1233 pub fn show_single_declaration<F>(f: &mut F, d: &syntax::SingleDeclaration)
1234 where
1235 F: Write,
1236 {
1237 show_fully_specified_type(f, &d.ty);
1238
1239 if let Some(ref name) = d.name {
1240 let _ = f.write_str(" ");
1241 show_identifier(f, name);
1242 }
1243
1244 if let Some(ref arr_spec) = d.array_specifier {
1245 show_array_spec(f, arr_spec);
1246 }
1247
1248 if let Some(ref initializer) = d.initializer {
1249 let _ = f.write_str(" = ");
1250 show_initializer(f, initializer);
1251 }
1252 }
1253
1254 pub fn show_single_declaration_no_type<F>(f: &mut F, d: &syntax::SingleDeclarationNoType)
1255 where
1256 F: Write,
1257 {
1258 show_arrayed_identifier(f, &d.ident);
1259
1260 if let Some(ref initializer) = d.initializer {
1261 let _ = f.write_str(" = ");
1262 show_initializer(f, initializer);
1263 }
1264 }
1265
1266 pub fn show_initializer<F>(f: &mut F, i: &syntax::Initializer)
1267 where
1268 F: Write,
1269 {
1270 match *i {
1271 syntax::Initializer::Simple(ref e) => show_expr(f, e),
1272 syntax::Initializer::List(ref list) => {
1273 let mut iter = list.0.iter();
1274 let first = iter.next().unwrap();
1275
1276 let _ = f.write_str("{ ");
1277 show_initializer(f, first);
1278
1279 for ini in iter {
1280 let _ = f.write_str(", ");
1281 show_initializer(f, ini);
1282 }
1283
1284 let _ = f.write_str(" }");
1285 }
1286 }
1287 }
1288
1289 pub fn show_block<F>(f: &mut F, b: &syntax::Block)
1290 where
1291 F: Write,
1292 {
1293 show_type_qualifier(f, &b.qualifier);
1294 let _ = f.write_str(" ");
1295 show_identifier(f, &b.name);
1296 let _ = f.write_str(" {");
1297
1298 for field in &b.fields {
1299 show_struct_field(f, field);
1300 let _ = f.write_str("\n");
1301 }
1302 let _ = f.write_str("}");
1303
1304 if let Some(ref ident) = b.identifier {
1305 show_arrayed_identifier(f, ident);
1306 }
1307 }
1308
1309 pub fn show_function_definition<F>(f: &mut F, fd: &syntax::FunctionDefinition)
1310 where
1311 F: Write,
1312 {
1313 show_function_prototype(f, &fd.prototype);
1314 let _ = f.write_str(" ");
1315 show_compound_statement(f, &fd.statement);
1316 }
1317
1318 pub fn show_compound_statement<F>(f: &mut F, cst: &syntax::CompoundStatement)
1319 where
1320 F: Write,
1321 {
1322 let _ = f.write_str("{\n");
1323
1324 for st in &cst.statement_list {
1325 show_statement(f, st);
1326 }
1327
1328 let _ = f.write_str("}\n");
1329 }
1330
1331 pub fn show_statement<F>(f: &mut F, st: &syntax::Statement)
1332 where
1333 F: Write,
1334 {
1335 match *st {
1336 syntax::Statement::Compound(ref cst) => show_compound_statement(f, cst),
1337 syntax::Statement::Simple(ref sst) => show_simple_statement(f, sst),
1338 }
1339 }
1340
1341 pub fn show_simple_statement<F>(f: &mut F, sst: &syntax::SimpleStatement)
1342 where
1343 F: Write,
1344 {
1345 match *sst {
1346 syntax::SimpleStatement::Declaration(ref d) => show_declaration(f, d),
1347 syntax::SimpleStatement::Expression(ref e) => show_expression_statement(f, e),
1348 syntax::SimpleStatement::Selection(ref s) => show_selection_statement(f, s),
1349 syntax::SimpleStatement::Switch(ref s) => show_switch_statement(f, s),
1350 syntax::SimpleStatement::CaseLabel(ref cl) => show_case_label(f, cl),
1351 syntax::SimpleStatement::Iteration(ref i) => show_iteration_statement(f, i),
1352 syntax::SimpleStatement::Jump(ref j) => show_jump_statement(f, j),
1353 }
1354 }
1355
1356 pub fn show_expression_statement<F>(f: &mut F, est: &syntax::ExprStatement)
1357 where
1358 F: Write,
1359 {
1360 if let Some(ref e) = *est {
1361 show_expr(f, e);
1362 }
1363
1364 let _ = f.write_str(";\n");
1365 }
1366
1367 pub fn show_selection_statement<F>(f: &mut F, sst: &syntax::SelectionStatement)
1368 where
1369 F: Write,
1370 {
1371 let _ = f.write_str("if (");
1372 show_expr(f, &sst.cond);
1373 let _ = f.write_str(") {\n");
1374 show_selection_rest_statement(f, &sst.rest);
1375 }
1376
1377 pub fn show_selection_rest_statement<F>(f: &mut F, sst: &syntax::SelectionRestStatement)
1378 where
1379 F: Write,
1380 {
1381 match *sst {
1382 syntax::SelectionRestStatement::Statement(ref if_st) => {
1383 show_statement(f, if_st);
1384 let _ = f.write_str("}\n");
1385 }
1386 syntax::SelectionRestStatement::Else(ref if_st, ref else_st) => {
1387 show_statement(f, if_st);
1388 let _ = f.write_str("} else ");
1389 show_statement(f, else_st);
1390 }
1391 }
1392 }
1393
1394 pub fn show_switch_statement<F>(f: &mut F, sst: &syntax::SwitchStatement)
1395 where
1396 F: Write,
1397 {
1398 let _ = f.write_str("switch (");
1399 show_expr(f, &sst.head);
1400 let _ = f.write_str(") {\n");
1401
1402 for st in &sst.body {
1403 show_statement(f, st);
1404 }
1405
1406 let _ = f.write_str("}\n");
1407 }
1408
1409 pub fn show_case_label<F>(f: &mut F, cl: &syntax::CaseLabel)
1410 where
1411 F: Write,
1412 {
1413 match *cl {
1414 syntax::CaseLabel::Case(ref e) => {
1415 let _ = f.write_str("case ");
1416 show_expr(f, e);
1417 let _ = f.write_str(":\n");
1418 }
1419 syntax::CaseLabel::Def => {
1420 let _ = f.write_str("default:\n");
1421 }
1422 }
1423 }
1424
1425 pub fn show_iteration_statement<F>(f: &mut F, ist: &syntax::IterationStatement)
1426 where
1427 F: Write,
1428 {
1429 match *ist {
1430 syntax::IterationStatement::While(ref cond, ref body) => {
1431 let _ = f.write_str("while (");
1432 show_condition(f, cond);
1433 let _ = f.write_str(") ");
1434 show_statement(f, body);
1435 }
1436 syntax::IterationStatement::DoWhile(ref body, ref cond) => {
1437 let _ = f.write_str("do ");
1438 show_statement(f, body);
1439 let _ = f.write_str(" while (");
1440 show_expr(f, cond);
1441 let _ = f.write_str(")\n");
1442 }
1443 syntax::IterationStatement::For(ref init, ref rest, ref body) => {
1444 let _ = f.write_str("for (");
1445 show_for_init_statement(f, init);
1446 show_for_rest_statement(f, rest);
1447 let _ = f.write_str(") ");
1448 show_statement(f, body);
1449 }
1450 }
1451 }
1452
1453 pub fn show_condition<F>(f: &mut F, c: &syntax::Condition)
1454 where
1455 F: Write,
1456 {
1457 match *c {
1458 syntax::Condition::Expr(ref e) => show_expr(f, e),
1459 syntax::Condition::Assignment(ref ty, ref name, ref initializer) => {
1460 show_fully_specified_type(f, ty);
1461 let _ = f.write_str(" ");
1462 show_identifier(f, name);
1463 let _ = f.write_str(" = ");
1464 show_initializer(f, initializer);
1465 }
1466 }
1467 }
1468
1469 pub fn show_for_init_statement<F>(f: &mut F, i: &syntax::ForInitStatement)
1470 where
1471 F: Write,
1472 {
1473 match *i {
1474 syntax::ForInitStatement::Expression(ref expr) => {
1475 if let Some(ref e) = *expr {
1476 show_expr(f, e);
1477 }
1478 }
1479 syntax::ForInitStatement::Declaration(ref d) => show_declaration(f, d),
1480 }
1481 }
1482
1483 pub fn show_for_rest_statement<F>(f: &mut F, r: &syntax::ForRestStatement)
1484 where
1485 F: Write,
1486 {
1487 if let Some(ref cond) = r.condition {
1488 show_condition(f, cond);
1489 }
1490
1491 let _ = f.write_str("; ");
1492
1493 if let Some(ref e) = r.post_expr {
1494 show_expr(f, e);
1495 }
1496 }
1497
1498 pub fn show_jump_statement<F>(f: &mut F, j: &syntax::JumpStatement)
1499 where
1500 F: Write,
1501 {
1502 match *j {
1503 syntax::JumpStatement::Continue => {
1504 let _ = f.write_str("continue;\n");
1505 }
1506 syntax::JumpStatement::Break => {
1507 let _ = f.write_str("break;\n");
1508 }
1509 syntax::JumpStatement::Discard => {
1510 let _ = f.write_str("discard;\n");
1511 }
1512 syntax::JumpStatement::Return(ref e) => {
1513 let _ = f.write_str("return ");
1514 if let Some(e) = e {
1515 show_expr(f, e);
1516 }
1517 let _ = f.write_str(";\n");
1518 }
1519 }
1520 }
1521
1522 pub fn show_preprocessor<F>(f: &mut F, pp: &syntax::Preprocessor)
1523 where
1524 F: Write,
1525 {
1526 match *pp {
1527 syntax::Preprocessor::Define(ref pd) => show_preprocessor_define(f, pd),
1528 syntax::Preprocessor::Else => show_preprocessor_else(f),
1529 syntax::Preprocessor::ElseIf(ref pei) => show_preprocessor_elseif(f, pei),
1530 syntax::Preprocessor::EndIf => show_preprocessor_endif(f),
1531 syntax::Preprocessor::Error(ref pe) => show_preprocessor_error(f, pe),
1532 syntax::Preprocessor::If(ref pi) => show_preprocessor_if(f, pi),
1533 syntax::Preprocessor::IfDef(ref pid) => show_preprocessor_ifdef(f, pid),
1534 syntax::Preprocessor::IfNDef(ref pind) => show_preprocessor_ifndef(f, pind),
1535 syntax::Preprocessor::Include(ref pi) => show_preprocessor_include(f, pi),
1536 syntax::Preprocessor::Line(ref pl) => show_preprocessor_line(f, pl),
1537 syntax::Preprocessor::Pragma(ref pp) => show_preprocessor_pragma(f, pp),
1538 syntax::Preprocessor::Undef(ref pu) => show_preprocessor_undef(f, pu),
1539 syntax::Preprocessor::Version(ref pv) => show_preprocessor_version(f, pv),
1540 syntax::Preprocessor::Extension(ref pe) => show_preprocessor_extension(f, pe),
1541 }
1542 }
1543
1544 pub fn show_preprocessor_define<F>(f: &mut F, pd: &syntax::PreprocessorDefine)
1545 where
1546 F: Write,
1547 {
1548 match *pd {
1549 syntax::PreprocessorDefine::ObjectLike {
1550 ref ident,
1551 ref value,
1552 } => {
1553 let _ = write!(f, "#define {} {}\n", ident, value);
1554 }
1555
1556 syntax::PreprocessorDefine::FunctionLike {
1557 ref ident,
1558 ref args,
1559 ref value,
1560 } => {
1561 let _ = write!(f, "#define {}(", ident);
1562
1563 if !args.is_empty() {
1564 let _ = write!(f, "{}", &args[0]);
1565
1566 for arg in &args[1..args.len()] {
1567 let _ = write!(f, ", {}", arg);
1568 }
1569 }
1570
1571 let _ = write!(f, ") {}\n", value);
1572 }
1573 }
1574 }
1575
1576 pub fn show_preprocessor_else<F>(f: &mut F)
1577 where
1578 F: Write,
1579 {
1580 let _ = f.write_str("#else\n");
1581 }
1582
1583 pub fn show_preprocessor_elseif<F>(f: &mut F, pei: &syntax::PreprocessorElseIf)
1584 where
1585 F: Write,
1586 {
1587 let _ = write!(f, "#elseif {}\n", pei.condition);
1588 }
1589
1590 pub fn show_preprocessor_error<F>(f: &mut F, pe: &syntax::PreprocessorError)
1591 where
1592 F: Write,
1593 {
1594 let _ = writeln!(f, "#error {}", pe.message);
1595 }
1596
1597 pub fn show_preprocessor_endif<F>(f: &mut F)
1598 where
1599 F: Write,
1600 {
1601 let _ = f.write_str("#endif\n");
1602 }
1603
1604 pub fn show_preprocessor_if<F>(f: &mut F, pi: &syntax::PreprocessorIf)
1605 where
1606 F: Write,
1607 {
1608 let _ = write!(f, "#if {}\n", pi.condition);
1609 }
1610
1611 pub fn show_preprocessor_ifdef<F>(f: &mut F, pid: &syntax::PreprocessorIfDef)
1612 where
1613 F: Write,
1614 {
1615 let _ = f.write_str("#ifdef ");
1616 show_identifier(f, &pid.ident);
1617 let _ = f.write_str("\n");
1618 }
1619
1620 pub fn show_preprocessor_ifndef<F>(f: &mut F, pind: &syntax::PreprocessorIfNDef)
1621 where
1622 F: Write,
1623 {
1624 let _ = f.write_str("#ifndef ");
1625 show_identifier(f, &pind.ident);
1626 let _ = f.write_str("\n");
1627 }
1628
1629 pub fn show_preprocessor_include<F>(f: &mut F, pi: &syntax::PreprocessorInclude)
1630 where
1631 F: Write,
1632 {
1633 let _ = f.write_str("#include ");
1634 show_path(f, &pi.path);
1635 let _ = f.write_str("\n");
1636 }
1637
1638 pub fn show_preprocessor_line<F>(f: &mut F, pl: &syntax::PreprocessorLine)
1639 where
1640 F: Write,
1641 {
1642 let _ = write!(f, "#line {}", pl.line);
1643 if let Some(source_string_number) = pl.source_string_number {
1644 let _ = write!(f, " {}", source_string_number);
1645 }
1646 let _ = f.write_str("\n");
1647 }
1648
1649 pub fn show_preprocessor_pragma<F>(f: &mut F, pp: &syntax::PreprocessorPragma)
1650 where
1651 F: Write,
1652 {
1653 let _ = writeln!(f, "#pragma {}", pp.command);
1654 }
1655
1656 pub fn show_preprocessor_undef<F>(f: &mut F, pud: &syntax::PreprocessorUndef)
1657 where
1658 F: Write,
1659 {
1660 let _ = f.write_str("#undef ");
1661 show_identifier(f, &pud.name);
1662 let _ = f.write_str("\n");
1663 }
1664
1665 pub fn show_preprocessor_version<F>(f: &mut F, pv: &syntax::PreprocessorVersion)
1666 where
1667 F: Write,
1668 {
1669 let _ = write!(f, "#version {}", pv.version);
1670
1671 if let Some(ref profile) = pv.profile {
1672 match *profile {
1673 syntax::PreprocessorVersionProfile::Core => {
1674 let _ = f.write_str(" core");
1675 }
1676 syntax::PreprocessorVersionProfile::Compatibility => {
1677 let _ = f.write_str(" compatibility");
1678 }
1679 syntax::PreprocessorVersionProfile::ES => {
1680 let _ = f.write_str(" es");
1681 }
1682 }
1683 }
1684
1685 let _ = f.write_str("\n");
1686 }
1687
1688 pub fn show_preprocessor_extension<F>(f: &mut F, pe: &syntax::PreprocessorExtension)
1689 where
1690 F: Write,
1691 {
1692 let _ = f.write_str("#extension ");
1693
1694 match pe.name {
1695 syntax::PreprocessorExtensionName::All => {
1696 let _ = f.write_str("all");
1697 }
1698 syntax::PreprocessorExtensionName::Specific(ref n) => {
1699 let _ = f.write_str(n);
1700 }
1701 }
1702
1703 if let Some(ref behavior) = pe.behavior {
1704 match *behavior {
1705 syntax::PreprocessorExtensionBehavior::Require => {
1706 let _ = f.write_str(" : require");
1707 }
1708 syntax::PreprocessorExtensionBehavior::Enable => {
1709 let _ = f.write_str(" : enable");
1710 }
1711 syntax::PreprocessorExtensionBehavior::Warn => {
1712 let _ = f.write_str(" : warn");
1713 }
1714 syntax::PreprocessorExtensionBehavior::Disable => {
1715 let _ = f.write_str(" : disable");
1716 }
1717 }
1718 }
1719
1720 let _ = f.write_str("\n");
1721 }
1722
1723 pub fn show_external_declaration<F>(f: &mut F, ed: &syntax::ExternalDeclaration)
1724 where
1725 F: Write,
1726 {
1727 match *ed {
1728 syntax::ExternalDeclaration::Preprocessor(ref pp) => show_preprocessor(f, pp),
1729 syntax::ExternalDeclaration::FunctionDefinition(ref fd) => show_function_definition(f, fd),
1730 syntax::ExternalDeclaration::Declaration(ref d) => show_declaration(f, d),
1731 }
1732 }
1733
1734 pub fn show_translation_unit<F>(f: &mut F, tu: &syntax::TranslationUnit)
1735 where
1736 F: Write,
1737 {
1738 for ed in &(tu.0).0 {
1739 show_external_declaration(f, ed);
1740 }
1741 }
1742
1743 #[cfg(test)]
1744 mod tests {
1745 use super::*;
1746 use crate::parsers::expr;
1747
1748 fn to_string(e: &syntax::Expr) -> String {
1749 let mut s = String::new();
1750 show_expr(&mut s, e);
1751 s
1752 }
1753
1754 #[test]
1755 fn unary_parentheses() {
1756 assert_eq!(to_string(&expr("-a").unwrap().1), "-a");
1757 assert_eq!(to_string(&expr("-(a + b)").unwrap().1), "-(a+b)");
1758 assert_eq!(to_string(&expr("-a.x").unwrap().1), "-a.x");
1759
1760 assert_eq!(to_string(&expr("-(-a)").unwrap().1), "-(-a)");
1761 assert_eq!(to_string(&expr("+(+a)").unwrap().1), "+(+a)");
1762 assert_eq!(to_string(&expr("~~a").unwrap().1), "~~a");
1763 assert_eq!(to_string(&expr("--a").unwrap().1), "--a");
1764 assert_eq!(to_string(&expr("++a").unwrap().1), "++a");
1765 assert_eq!(to_string(&expr("+-a").unwrap().1), "+-a");
1766 }
1767
1768 #[test]
1769 fn binary_parentheses() {
1770 assert_eq!(to_string(&expr("a + b").unwrap().1), "a+b");
1771 assert_eq!(to_string(&expr("a * b + c").unwrap().1), "a*b+c");
1772 assert_eq!(to_string(&expr("(a + b) * c").unwrap().1), "(a+b)*c");
1773 assert_eq!(to_string(&expr("a + (b * c)").unwrap().1), "a+b*c");
1774 assert_eq!(to_string(&expr("a * (b + c)").unwrap().1), "a*(b+c)");
1775 assert_eq!(to_string(&expr("(a * b) * c").unwrap().1), "a*b*c");
1776 assert_eq!(to_string(&expr("a * (b * c)").unwrap().1), "a*(b*c)");
1777 assert_eq!(to_string(&expr("a&&b&&c").unwrap().1), "a&&b&&c");
1778 assert_eq!(
1779 to_string(&expr("n - p > 0. && u.y < n && u.y > p").unwrap().1),
1780 "n-p>0.&&u.y<n&&u.y>p"
1781 );
1782 }
1783
1784 #[test]
1785 fn ternary_parentheses() {
1786 assert_eq!(
1787 to_string(&expr("a ? b : c ? d : e").unwrap().1),
1788 "a ? b : c ? d : e"
1789 );
1790 assert_eq!(
1791 to_string(&expr("(a ? b : c) ? d : e").unwrap().1),
1792 "(a ? b : c) ? d : e"
1793 );
1794 }
1795
1796 #[test]
1797 fn assignment_parentheses() {
1798 assert_eq!(to_string(&expr("a = b = c").unwrap().1), "a = b = c");
1799 assert_eq!(to_string(&expr("(a = b) = c").unwrap().1), "(a = b) = c");
1800 }
1801
1802 #[test]
1803 fn dot_parentheses() {
1804 assert_eq!(to_string(&expr("a.x").unwrap().1), "a.x");
1805 assert_eq!(to_string(&expr("(a + b).x").unwrap().1), "(a+b).x");
1806 }
1807
1808 #[test]
1809 fn test_parentheses() {
1810 use crate::parsers::function_definition;
1811
1812 const SRC: &'static str = r#"vec2 main() {
1813 float n = 0.;
1814 float p = 0.;
1815 float u = vec2(0., 0.);
1816 if (n-p>0.&&u.y<n&&u.y>p) {
1817 }
1818 return u;
1819 }
1820 "#;
1821
1822 // Ideally we would use SRC as the expected, but there's a bug in block braces generation
1823 const DST: &'static str = r#"vec2 main() {
1824 float n = 0.;
1825 float p = 0.;
1826 float u = vec2(0., 0.);
1827 if (n-p>0.&&u.y<n&&u.y>p) {
1828 {
1829 }
1830 }
1831 return u;
1832 }
1833 "#;
1834
1835 let mut s = String::new();
1836 show_function_definition(&mut s, &function_definition(SRC).unwrap().1);
1837
1838 assert_eq!(s, DST);
1839 }
1840
1841 #[test]
1842 fn roundtrip_glsl_complex_expr() {
1843 let zero = syntax::Expr::DoubleConst(0.);
1844 let ray = syntax::Expr::Variable("ray".into());
1845 let raydir = syntax::Expr::Dot(Box::new(ray), "dir".into());
1846 let vec4 = syntax::Expr::FunCall(
1847 syntax::FunIdentifier::Identifier("vec4".into()),
1848 vec![raydir, zero],
1849 );
1850 let view = syntax::Expr::Variable("view".into());
1851 let iview = syntax::Expr::FunCall(
1852 syntax::FunIdentifier::Identifier("inverse".into()),
1853 vec![view],
1854 );
1855 let mul = syntax::Expr::Binary(syntax::BinaryOp::Mult, Box::new(iview), Box::new(vec4));
1856 let xyz = syntax::Expr::Dot(Box::new(mul), "xyz".into());
1857 let input = syntax::Expr::FunCall(
1858 syntax::FunIdentifier::Identifier("normalize".into()),
1859 vec![xyz],
1860 );
1861
1862 let mut output = String::new();
1863 show_expr(&mut output, &input);
1864 let _ = output.write_str(";");
1865
1866 let back = expr(&output);
1867
1868 assert_eq!(back, Ok((";", input)), "intermediate source '{}'", output);
1869 }
1870 }