ptx_parser/type/
variable.rs

1use crate::Spanned;
2use crate::parser::Span;
3use crate::r#type::common::{AttributeDirective, DataType};
4use crate::r#type::{FunctionSymbol, Immediate, VariableSymbol};
5
6/// Module-level declarations that reserve storage in a specific address space.
7#[derive(Debug, Clone, PartialEq, Eq, Spanned)]
8pub enum ModuleVariableDirective {
9    /// Deprecated `.tex` variable declaration.
10    ///
11    /// Example:
12    /// .tex .u32 tex_a; // is equivalent to .global .texref tex_a;
13    Tex {
14        directive: VariableDirective,
15        span: Span,
16    },
17    Shared {
18        directive: VariableDirective,
19        span: Span,
20    },
21    Global {
22        directive: VariableDirective,
23        span: Span,
24    },
25    Const {
26        directive: VariableDirective,
27        span: Span,
28    },
29}
30
31/// Module-scoped variable declaration shared by `.tex`, `.shared`, `.global`, and `.const`.
32#[derive(Debug, Clone, PartialEq, Eq, Spanned)]
33pub struct VariableDirective {
34    /// Example:
35    /// .global .attribute(.managed) .s32 g;
36    /// .global .attribute(.managed) .u64 x;
37    /// .global .attribute(.unified(19,95)) .f32 f;
38    pub attributes: Vec<AttributeDirective>,
39    /// Modifiers applied to the variable (e.g., `.v4`, `.align 16`, `.ptr`).
40    pub modifiers: Vec<VariableModifier>,
41    /// The data type of the variable (e.g., `.s32`, `.f64`).
42    pub ty: DataType,
43    /// The variable name.
44    pub name: VariableSymbol,
45    /// The array dimensions, if any.
46    /// Example:
47    /// .global .s32 offset[][2] = { {-1, 0}, {0, -1}, {1, 0}, {0, 1} };
48    pub array_dims: Vec<Option<u64>>,
49    /// Optional global initializer for the variable.
50    pub initializer: Option<GlobalInitializer>,
51    pub span: Span,
52}
53
54/// Qualifiers left on module variable declarations (e.g. `.v4`).
55#[derive(Debug, Clone, PartialEq, Eq, Spanned)]
56pub enum VariableModifier {
57    Vector { value: u32, span: Span },
58    Alignment { value: u32, span: Span },
59    Ptr { span: Span },
60}
61
62/// Parameters, used in function declarations and calls, e.g., `.param .b32 p`.
63#[derive(Debug, Clone, PartialEq, Eq, Spanned)]
64pub enum ParameterDirective {
65    Register {
66        ty: DataType,
67        name: VariableSymbol,
68        span: Span,
69    },
70    Parameter {
71        align: Option<u32>,
72        ptr: bool,
73        space: Option<ParamStateSpace>,
74        ty: DataType,
75        name: VariableSymbol,
76        array: Vec<Option<u64>>,
77        span: Span,
78    },
79}
80
81/// Address space qualifiers for parameters.
82#[derive(Debug, Clone, PartialEq, Eq, Spanned)]
83pub enum ParamStateSpace {
84    /// .const
85    Const { span: Span },
86    /// .global
87    Global { span: Span },
88    /// .local
89    Local { span: Span },
90    /// .shared
91    Shared { span: Span },
92}
93
94// Values that can appear in global initialiser lists.
95#[derive(Debug, Clone, PartialEq, Eq, Spanned)]
96pub enum InitializerValue {
97    NumericLiteral { value: Immediate, span: Span },
98    FunctionSymbol { name: FunctionSymbol, span: Span },
99    StringLiteral { value: String, span: Span },
100}
101
102/// Structured representation of a global variable initialiser.
103#[derive(Debug, Clone, PartialEq, Eq, Spanned)]
104pub enum GlobalInitializer {
105    Scalar {
106        value: InitializerValue,
107        span: Span,
108    },
109    Aggregate {
110        values: Vec<GlobalInitializer>,
111        span: Span,
112    },
113}