1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//! GLSLT transform utilities definitions

use glsl_lang::ast::*;

use crate::Result;

mod fn_ref;
pub use fn_ref::*;

mod global_scope;
pub(crate) use global_scope::*;

mod instantiate;

mod local_scope;
pub(crate) use local_scope::*;

mod min_unit;
pub use min_unit::*;

mod scope;
pub(crate) use scope::*;

pub mod template;

mod transform_unit;
pub use transform_unit::*;

mod unit;
pub use unit::*;

fn transform_unit<'a, T: TransformUnit>(
    asts: impl std::iter::Iterator<Item = &'a TranslationUnit>,
    inst: &mut T,
) -> Result<()> {
    for (_id, ast) in asts.enumerate() {
        // We clone all declarations since they all have somewhere to go
        for extdecl in ast.0.iter().cloned() {
            inst.parse_external_declaration(extdecl)?;
        }
    }

    Ok(())
}

/// Transform a GLSLT AST to an instantiated GLSL AST
///
/// # Parameters
///
/// * `asts`: iterator of translation units to be concatenated and transformed
///
/// # Errors
///
/// Return an error if the transformation fails. See [crate::Error] for possible failure reasons.
pub fn transform<'a>(
    asts: impl std::iter::Iterator<Item = &'a TranslationUnit>,
) -> Result<TranslationUnit> {
    let mut inst = Unit::new();

    transform_unit(asts, &mut inst)?;

    inst.into_translation_unit()
}

/// Transform a GLSLT AST to an instantiated GLSL AST. Only include symbols transitively
/// referenced by any of the entry points listed in `wanted`.
///
/// # Parameters
///
/// * `asts`: iterator of translation units to be concatenated and transformed
/// * `wanted`: list of entry points to keep in the output
///
/// # Errors
///
/// Return an error if the transformation fails. See [crate::Error] for possible failure reasons.
pub fn transform_min<'a>(
    asts: impl std::iter::Iterator<Item = &'a TranslationUnit>,
    wanted: impl std::iter::Iterator<Item = &'a str>,
) -> Result<TranslationUnit> {
    let mut inst = MinUnit::new();

    transform_unit(asts, &mut inst)?;

    inst.into_translation_unit(wanted)
}