ptx_parser/pretty_print/
variable.rs

1// TreeDisplay implementations for variable types (src/type/variable.rs)
2
3use crate::r#type::variable::*;
4use super::{TreeDisplay, TreeFormatter};
5
6impl TreeDisplay for ModuleVariableDirective {
7    fn tree_display(&self, f: &mut TreeFormatter, source: &str) -> std::fmt::Result {
8        match self {
9            ModuleVariableDirective::Tex { directive, span } => {
10                f.root(&format!("ModuleVariableDirective::Tex [{}]", f.format_raw(*span, source)))?;
11                f.field_with_child(true, "directive", directive, source)
12            }
13            ModuleVariableDirective::Shared { directive, span } => {
14                f.root(&format!("ModuleVariableDirective::Shared [{}]", f.format_raw(*span, source)))?;
15                f.field_with_child(true, "directive", directive, source)
16            }
17            ModuleVariableDirective::Global { directive, span } => {
18                f.root(&format!("ModuleVariableDirective::Global [{}]", f.format_raw(*span, source)))?;
19                f.field_with_child(true, "directive", directive, source)
20            }
21            ModuleVariableDirective::Const { directive, span } => {
22                f.root(&format!("ModuleVariableDirective::Const [{}]", f.format_raw(*span, source)))?;
23                f.field_with_child(true, "directive", directive, source)
24            }
25        }
26    }
27}
28
29impl TreeDisplay for VariableDirective {
30    fn tree_display(&self, f: &mut TreeFormatter, source: &str) -> std::fmt::Result {
31        f.root(&format!("VariableDirective [{}]", f.format_raw(self.span, source)))?;
32        f.field_vec(false, "attributes", &self.attributes, source)?;
33        f.field_vec(false, "modifiers", &self.modifiers, source)?;
34        f.field_with_child(false, "ty", &self.ty, source)?;
35        f.field(false, "name", &self.name.val)?;
36        f.field(false, "array_dims", &format!("{:?}", self.array_dims))?;
37        f.field_option(true, "initializer", &self.initializer, source)
38    }
39}
40
41impl TreeDisplay for VariableModifier {
42    fn tree_display(&self, f: &mut TreeFormatter, source: &str) -> std::fmt::Result {
43        match self {
44            VariableModifier::Vector { value, span } => {
45                f.root(&format!("VariableModifier::Vector [{}]", f.format_raw(*span, source)))?;
46                f.field(true, "value", &value.to_string())
47            }
48            VariableModifier::Alignment { value, span } => {
49                f.root(&format!("VariableModifier::Alignment [{}]", f.format_raw(*span, source)))?;
50                f.field(true, "value", &value.to_string())
51            }
52            VariableModifier::Ptr { span } => {
53                f.root(&format!("VariableModifier::Ptr [{}]", f.format_raw(*span, source)))
54            }
55        }
56    }
57}
58
59impl TreeDisplay for ParameterDirective {
60    fn tree_display(&self, f: &mut TreeFormatter, source: &str) -> std::fmt::Result {
61        match self {
62            ParameterDirective::Register { ty, name, span } => {
63                f.root(&format!("ParameterDirective::Register [{}]", f.format_raw(*span, source)))?;
64                f.field_with_child(false, "ty", ty, source)?;
65                f.field(true, "name", &name.val)
66            }
67            ParameterDirective::Parameter { align, ptr, space, ty, name, array, span } => {
68                f.root(&format!("ParameterDirective::Parameter [{}]", f.format_raw(*span, source)))?;
69                match align {
70                    Some(a) => f.field(false, "align", &format!("Some({})", a))?,
71                    None => f.field(false, "align", "None")?,
72                }
73                f.field(false, "ptr", &ptr.to_string())?;
74                f.field_option(false, "space", space, source)?;
75                f.field_with_child(false, "ty", ty, source)?;
76                f.field(false, "name", &name.val)?;
77                f.field(true, "array", &format!("{:?}", array))
78            }
79        }
80    }
81}
82
83impl TreeDisplay for ParamStateSpace {
84    fn tree_display(&self, f: &mut TreeFormatter, source: &str) -> std::fmt::Result {
85        let (variant, span) = match self {
86            ParamStateSpace::Const { span } => ("Const", span),
87            ParamStateSpace::Global { span } => ("Global", span),
88            ParamStateSpace::Local { span } => ("Local", span),
89            ParamStateSpace::Shared { span } => ("Shared", span),
90        };
91        f.root(&format!("ParamStateSpace::{} [{}]", variant, f.format_raw(*span, source)))
92    }
93}
94
95impl TreeDisplay for InitializerValue {
96    fn tree_display(&self, f: &mut TreeFormatter, source: &str) -> std::fmt::Result {
97        match self {
98            InitializerValue::NumericLiteral { value, span } => {
99                f.root(&format!("InitializerValue::NumericLiteral [{}]", f.format_raw(*span, source)))?;
100                f.field(true, "value", &format!("{:?}", value))
101            }
102            InitializerValue::FunctionSymbol { name, span } => {
103                f.root(&format!("InitializerValue::FunctionSymbol [{}]", f.format_raw(*span, source)))?;
104                f.field(true, "name", &name.val)
105            }
106            InitializerValue::StringLiteral { value, span } => {
107                f.root(&format!("InitializerValue::StringLiteral [{}]", f.format_raw(*span, source)))?;
108                f.field(true, "value", &format!("\"{}\"", value))
109            }
110        }
111    }
112}
113
114impl TreeDisplay for GlobalInitializer {
115    fn tree_display(&self, f: &mut TreeFormatter, source: &str) -> std::fmt::Result {
116        match self {
117            GlobalInitializer::Scalar { value, span } => {
118                f.root(&format!("GlobalInitializer::Scalar [{}]", f.format_raw(*span, source)))?;
119                f.field_with_child(true, "value", value, source)
120            }
121            GlobalInitializer::Aggregate { values, span } => {
122                f.root(&format!("GlobalInitializer::Aggregate [{}] ({} values)",
123                    f.format_raw(*span, source), values.len()))
124            }
125        }
126    }
127}