ptx_parser/pretty_print/
function.rs

1// TreeDisplay implementations for function types (src/type/function.rs)
2
3use super::{TreeDisplay, TreeFormatter};
4use crate::r#type::function::*;
5
6impl TreeDisplay for AliasFunctionDirective {
7    fn tree_display(&self, f: &mut TreeFormatter, source: &str) -> std::fmt::Result {
8        f.root(&format!(
9            "AliasFunctionDirective [{}]",
10            f.format_raw(self.span, source)
11        ))?;
12        f.field(false, "alias", &self.alias.val)?;
13        f.field(true, "target", &self.target.val)
14    }
15}
16
17impl TreeDisplay for FuncFunctionDirective {
18    fn tree_display(&self, f: &mut TreeFormatter, source: &str) -> std::fmt::Result {
19        f.root(&format!(
20            "FuncFunctionDirective [{}]",
21            f.format_raw(self.span, source)
22        ))?;
23        f.field_vec(false, "attributes", &self.attributes, source)?;
24        f.field_option(false, "return_param", &self.return_param, source)?;
25        f.field(false, "name", &self.name.val)?;
26        f.field_vec(false, "params", &self.params, source)?;
27        f.field_vec(false, "directives", &self.directives, source)?;
28        f.field_option(true, "body", &self.body, source)
29    }
30}
31
32impl TreeDisplay for EntryFunctionDirective {
33    fn tree_display(&self, f: &mut TreeFormatter, source: &str) -> std::fmt::Result {
34        f.root(&format!(
35            "EntryFunctionDirective [{}]",
36            f.format_raw(self.span, source)
37        ))?;
38        f.field(false, "name", &self.name.val)?;
39        f.field_vec(false, "params", &self.params, source)?;
40        f.field_vec(false, "directives", &self.directives, source)?;
41        f.field_option(true, "body", &self.body, source)
42    }
43}
44
45impl TreeDisplay for FuncFunctionHeaderDirective {
46    fn tree_display(&self, f: &mut TreeFormatter, source: &str) -> std::fmt::Result {
47        match self {
48            FuncFunctionHeaderDirective::NoReturn { span } => f.root(&format!(
49                "FuncFunctionHeaderDirective::NoReturn [{}]",
50                f.format_raw(*span, source)
51            )),
52            FuncFunctionHeaderDirective::Pragma { args, span } => {
53                f.root(&format!(
54                    "FuncFunctionHeaderDirective::Pragma [{}]",
55                    f.format_raw(*span, source)
56                ))?;
57                f.field_vec(true, "args", args, source)
58            }
59            FuncFunctionHeaderDirective::AbiPreserve { value, span } => {
60                f.root(&format!(
61                    "FuncFunctionHeaderDirective::AbiPreserve [{}]",
62                    f.format_raw(*span, source)
63                ))?;
64                f.field(true, "value", &value.to_string())
65            }
66            FuncFunctionHeaderDirective::AbiPreserveControl { value, span } => {
67                f.root(&format!(
68                    "FuncFunctionHeaderDirective::AbiPreserveControl [{}]",
69                    f.format_raw(*span, source)
70                ))?;
71                f.field(true, "value", &value.to_string())
72            }
73        }
74    }
75}
76
77impl TreeDisplay for EntryFunctionHeaderDirective {
78    fn tree_display(&self, f: &mut TreeFormatter, source: &str) -> std::fmt::Result {
79        match self {
80            EntryFunctionHeaderDirective::MaxNReg { value, span } => {
81                f.root(&format!(
82                    "EntryFunctionHeaderDirective::MaxNReg [{}]",
83                    f.format_raw(*span, source)
84                ))?;
85                f.field(true, "value", &value.to_string())
86            }
87            EntryFunctionHeaderDirective::MaxNTid { dim, span } => {
88                f.root(&format!(
89                    "EntryFunctionHeaderDirective::MaxNTid [{}]",
90                    f.format_raw(*span, source)
91                ))?;
92                f.field_with_child(true, "dim", dim, source)
93            }
94            EntryFunctionHeaderDirective::ReqNTid { dim, span } => {
95                f.root(&format!(
96                    "EntryFunctionHeaderDirective::ReqNTid [{}]",
97                    f.format_raw(*span, source)
98                ))?;
99                f.field_with_child(true, "dim", dim, source)
100            }
101            EntryFunctionHeaderDirective::MinNCtaPerSm { value, span } => {
102                f.root(&format!(
103                    "EntryFunctionHeaderDirective::MinNCtaPerSm [{}]",
104                    f.format_raw(*span, source)
105                ))?;
106                f.field(true, "value", &value.to_string())
107            }
108            EntryFunctionHeaderDirective::MaxNCtaPerSm { value, span } => {
109                f.root(&format!(
110                    "EntryFunctionHeaderDirective::MaxNCtaPerSm [{}]",
111                    f.format_raw(*span, source)
112                ))?;
113                f.field(true, "value", &value.to_string())
114            }
115            EntryFunctionHeaderDirective::Pragma { args, span } => {
116                f.root(&format!(
117                    "EntryFunctionHeaderDirective::Pragma [{}]",
118                    f.format_raw(*span, source)
119                ))?;
120                f.field_vec(true, "args", args, source)
121            }
122            EntryFunctionHeaderDirective::ReqNctaPerCluster { dim, span } => {
123                f.root(&format!(
124                    "EntryFunctionHeaderDirective::ReqNctaPerCluster [{}]",
125                    f.format_raw(*span, source)
126                ))?;
127                f.field_with_child(true, "dim", dim, source)
128            }
129            EntryFunctionHeaderDirective::ExplicitCluster { span } => f.root(&format!(
130                "EntryFunctionHeaderDirective::ExplicitCluster [{}]",
131                f.format_raw(*span, source)
132            )),
133            EntryFunctionHeaderDirective::MaxClusterRank { value, span } => {
134                f.root(&format!(
135                    "EntryFunctionHeaderDirective::MaxClusterRank [{}]",
136                    f.format_raw(*span, source)
137                ))?;
138                f.field(true, "value", &value.to_string())
139            }
140            EntryFunctionHeaderDirective::BlocksAreClusters { span } => f.root(&format!(
141                "EntryFunctionHeaderDirective::BlocksAreClusters [{}]",
142                f.format_raw(*span, source)
143            )),
144        }
145    }
146}
147
148impl TreeDisplay for FunctionBody {
149    fn tree_display(&self, f: &mut TreeFormatter, source: &str) -> std::fmt::Result {
150        f.root(&format!(
151            "FunctionBody [{}]",
152            f.format_raw(self.span, source)
153        ))?;
154        f.field_vec(true, "statements", &self.statements, source)
155    }
156}
157
158impl TreeDisplay for FunctionStatement {
159    fn tree_display(&self, f: &mut TreeFormatter, source: &str) -> std::fmt::Result {
160        match self {
161            FunctionStatement::Label { label, span } => {
162                f.root(&format!(
163                    "FunctionStatement::Label [{}]",
164                    f.format_raw(*span, source)
165                ))?;
166                f.field_with_child(true, "label", label, source)
167            }
168            FunctionStatement::Directive { directive, span } => {
169                f.root(&format!(
170                    "FunctionStatement::Directive [{}]",
171                    f.format_raw(*span, source)
172                ))?;
173                f.field_with_child(true, "directive", directive, source)
174            }
175            FunctionStatement::Instruction {
176                instruction,
177                span: _,
178            } => {
179                // Display instruction inline since it contains all the details
180                instruction.tree_display(f, source)
181            }
182            FunctionStatement::Block { statements, span } => {
183                f.root(&format!(
184                    "FunctionStatement::Block [{}]",
185                    f.format_raw(*span, source)
186                ))?;
187                f.field_vec(true, "statements", statements, source)
188            }
189        }
190    }
191}
192
193impl TreeDisplay for RegisterDirective {
194    fn tree_display(&self, f: &mut TreeFormatter, source: &str) -> std::fmt::Result {
195        f.root(&format!(
196            "RegisterDirective [{}]",
197            f.format_raw(self.span, source)
198        ))?;
199        f.field_with_child(false, "ty", &self.ty, source)?;
200        f.field_vec(true, "registers", &self.registers, source)
201    }
202}
203
204impl TreeDisplay for RegisterTarget {
205    fn tree_display(&self, f: &mut TreeFormatter, source: &str) -> std::fmt::Result {
206        f.root(&format!(
207            "RegisterTarget [{}]",
208            f.format_raw(self.span, source)
209        ))?;
210        f.field(false, "name", &self.name.val)?;
211        match self.range {
212            Some(r) => f.field(true, "range", &format!("Some({})", r))?,
213            None => f.field(true, "range", "None")?,
214        };
215        Ok(())
216    }
217}
218
219impl TreeDisplay for StatementDirective {
220    fn tree_display(&self, f: &mut TreeFormatter, source: &str) -> std::fmt::Result {
221        match self {
222            StatementDirective::Loc { directive, span } => {
223                f.root(&format!(
224                    "StatementDirective::Loc [{}]",
225                    f.format_raw(*span, source)
226                ))?;
227                f.field_with_child(true, "directive", directive, source)
228            }
229            StatementDirective::Pragma { directive, span } => {
230                f.root(&format!(
231                    "StatementDirective::Pragma [{}]",
232                    f.format_raw(*span, source)
233                ))?;
234                f.field_with_child(true, "directive", directive, source)
235            }
236            StatementDirective::Section { directive, span } => {
237                f.root(&format!(
238                    "StatementDirective::Section [{}]",
239                    f.format_raw(*span, source)
240                ))?;
241                f.field_with_child(true, "directive", directive, source)
242            }
243            StatementDirective::Reg { directive, span } => {
244                f.root(&format!(
245                    "StatementDirective::Reg [{}]",
246                    f.format_raw(*span, source)
247                ))?;
248                f.field_with_child(true, "directive", directive, source)
249            }
250            StatementDirective::Local { directive, span } => {
251                f.root(&format!(
252                    "StatementDirective::Local [{}]",
253                    f.format_raw(*span, source)
254                ))?;
255                f.field_with_child(true, "directive", directive, source)
256            }
257            StatementDirective::Param { directive, span } => {
258                f.root(&format!(
259                    "StatementDirective::Param [{}]",
260                    f.format_raw(*span, source)
261                ))?;
262                f.field_with_child(true, "directive", directive, source)
263            }
264            StatementDirective::Shared { directive, span } => {
265                f.root(&format!(
266                    "StatementDirective::Shared [{}]",
267                    f.format_raw(*span, source)
268                ))?;
269                f.field_with_child(true, "directive", directive, source)
270            }
271            StatementDirective::Dwarf { directive, span } => {
272                f.root(&format!(
273                    "StatementDirective::Dwarf [{}]",
274                    f.format_raw(*span, source)
275                ))?;
276                f.field_with_child(true, "directive", directive, source)
277            }
278            StatementDirective::BranchTargets { directive, span } => {
279                f.root(&format!(
280                    "StatementDirective::BranchTargets [{}]",
281                    f.format_raw(*span, source)
282                ))?;
283                f.field_with_child(true, "directive", directive, source)
284            }
285            StatementDirective::CallTargets { directive, span } => {
286                f.root(&format!(
287                    "StatementDirective::CallTargets [{}]",
288                    f.format_raw(*span, source)
289                ))?;
290                f.field_with_child(true, "directive", directive, source)
291            }
292            StatementDirective::CallPrototype { directive, span } => {
293                f.root(&format!(
294                    "StatementDirective::CallPrototype [{}]",
295                    f.format_raw(*span, source)
296                ))?;
297                f.field_with_child(true, "directive", directive, source)
298            }
299        }
300    }
301}
302
303impl TreeDisplay for DwarfDirective {
304    fn tree_display(&self, f: &mut TreeFormatter, source: &str) -> std::fmt::Result {
305        f.root(&format!(
306            "DwarfDirective [{}]",
307            f.format_raw(self.span, source)
308        ))?;
309        f.field(true, "kind", &format!("{:?}", self.kind))
310    }
311}
312
313impl TreeDisplay for SectionDirective {
314    fn tree_display(&self, f: &mut TreeFormatter, source: &str) -> std::fmt::Result {
315        f.root(&format!(
316            "SectionDirective [{}]",
317            f.format_raw(self.span, source)
318        ))?;
319        f.field(false, "name", &format!("\"{}\"", self.name))?;
320        f.field(
321            true,
322            "entries",
323            &format!("<{} entries>", self.entries.len()),
324        )
325    }
326}
327
328impl TreeDisplay for LocationDirective {
329    fn tree_display(&self, f: &mut TreeFormatter, source: &str) -> std::fmt::Result {
330        f.root(&format!(
331            "LocationDirective [{}]",
332            f.format_raw(self.span, source)
333        ))?;
334        f.field(false, "file_index", &self.file_index.to_string())?;
335        f.field(false, "line", &self.line.to_string())?;
336        f.field(false, "column", &self.column.to_string())?;
337        f.field_option(true, "inlined_at", &self.inlined_at, source)
338    }
339}
340
341impl TreeDisplay for LocationInlinedAt {
342    fn tree_display(&self, f: &mut TreeFormatter, source: &str) -> std::fmt::Result {
343        f.root(&format!(
344            "LocationInlinedAt [{}]",
345            f.format_raw(self.span, source)
346        ))?;
347        f.field(false, "file_index", &self.file_index.to_string())?;
348        f.field(false, "line", &self.line.to_string())?;
349        f.field(false, "column", &self.column.to_string())?;
350        f.field(false, "function_name", &self.function_name.val)?;
351        f.field_with_child(false, "label", &self.label, source)?;
352        match self.label_offset {
353            Some(offset) => f.field(true, "label_offset", &format!("Some({})", offset)),
354            None => f.field(true, "label_offset", "None"),
355        }
356    }
357}
358
359impl TreeDisplay for PragmaDirective {
360    fn tree_display(&self, f: &mut TreeFormatter, source: &str) -> std::fmt::Result {
361        f.root(&format!(
362            "PragmaDirective [{}]",
363            f.format_raw(self.span, source)
364        ))?;
365        f.field(true, "kind", &format!("{:?}", self.kind))
366    }
367}
368
369impl TreeDisplay for BranchTargetsDirective {
370    fn tree_display(&self, f: &mut TreeFormatter, source: &str) -> std::fmt::Result {
371        f.root(&format!(
372            "BranchTargetsDirective [{}]",
373            f.format_raw(self.span, source)
374        ))?;
375        f.field_vec(true, "labels", &self.labels, source)
376    }
377}
378
379impl TreeDisplay for CallTargetsDirective {
380    fn tree_display(&self, f: &mut TreeFormatter, source: &str) -> std::fmt::Result {
381        f.root(&format!(
382            "CallTargetsDirective [{}]",
383            f.format_raw(self.span, source)
384        ))?;
385        let targets: Vec<String> = self.targets.iter().map(|t| t.val.clone()).collect();
386        f.field_vec(true, "targets", &targets, source)
387    }
388}
389
390impl TreeDisplay for CallPrototypeDirective {
391    fn tree_display(&self, f: &mut TreeFormatter, source: &str) -> std::fmt::Result {
392        f.root(&format!(
393            "CallPrototypeDirective [{}]",
394            f.format_raw(self.span, source)
395        ))?;
396        f.field_option(false, "return_param", &self.return_param, source)?;
397        f.field_vec(false, "params", &self.params, source)?;
398        f.field(false, "noreturn", &self.noreturn.to_string())?;
399        match self.abi_preserve {
400            Some(v) => f.field(false, "abi_preserve", &format!("Some({})", v))?,
401            None => f.field(false, "abi_preserve", "None")?,
402        }
403        match self.abi_preserve_control {
404            Some(v) => f.field(true, "abi_preserve_control", &format!("Some({})", v)),
405            None => f.field(true, "abi_preserve_control", "None"),
406        }
407    }
408}
409
410impl TreeDisplay for FunctionDim {
411    fn tree_display(&self, f: &mut TreeFormatter, source: &str) -> std::fmt::Result {
412        match self {
413            FunctionDim::X { x, span } => {
414                f.root(&format!("FunctionDim::X [{}]", f.format_raw(*span, source)))?;
415                f.field(true, "x", &x.to_string())
416            }
417            FunctionDim::XY { x, y, span } => {
418                f.root(&format!(
419                    "FunctionDim::XY [{}]",
420                    f.format_raw(*span, source)
421                ))?;
422                f.field(false, "x", &x.to_string())?;
423                f.field(true, "y", &y.to_string())
424            }
425            FunctionDim::XYZ { x, y, z, span } => {
426                f.root(&format!(
427                    "FunctionDim::XYZ [{}]",
428                    f.format_raw(*span, source)
429                ))?;
430                f.field(false, "x", &x.to_string())?;
431                f.field(false, "y", &y.to_string())?;
432                f.field(true, "z", &z.to_string())
433            }
434        }
435    }
436}