ptx_parser/type/
module.rs

1use super::common::*;
2use super::function::{DwarfDirective, FunctionKernelDirective};
3use super::variable::ModuleVariableDirective;
4use crate::parser::Span;
5
6/// A full PTX module containing directives and function definitions.
7#[derive(Debug, Clone, PartialEq)]
8pub struct Module {
9    pub directives: Vec<ModuleDirective>,
10    pub span: Span,
11}
12
13impl Module {
14    pub fn with_span(mut self, span: Span) -> Self {
15        self.span = span;
16        self
17    }
18}
19
20impl Default for Module {
21    fn default() -> Self {
22        Self {
23            directives: Vec::new(),
24            span: 0..0,
25        }
26    }
27}
28
29/// Module-level directives recognised by the parser.
30#[derive(Debug, Clone, PartialEq)]
31pub enum ModuleDirective {
32    ModuleVariable { directive: ModuleVariableDirective, span: Span },
33    FunctionKernel { directive: FunctionKernelDirective, span: Span },
34    ModuleInfo { directive: ModuleInfoDirectiveKind, span: Span },
35    Debug { directive: ModuleDebugDirective, span: Span },
36    Linking { directive: LinkingDirective, span: Span },
37}
38
39impl ModuleDirective {
40    pub fn span(&self) -> Span {
41        match self {
42            ModuleDirective::ModuleVariable { span, .. } => span.clone(),
43            ModuleDirective::FunctionKernel { span, .. } => span.clone(),
44            ModuleDirective::ModuleInfo { span, .. } => span.clone(),
45            ModuleDirective::Debug { span, .. } => span.clone(),
46            ModuleDirective::Linking { span, .. } => span.clone(),
47        }
48    }
49}
50
51/// Directives that apply to the PTX module as a whole.
52#[derive(Debug, Clone, PartialEq)]
53pub enum ModuleInfoDirectiveKind {
54    Version { directive: VersionDirective, span: Span },
55    Target { directive: TargetDirective, span: Span },
56    AddressSize { directive: AddressSizeDirective, span: Span },
57}
58
59impl ModuleInfoDirectiveKind {
60    pub fn span(&self) -> Span {
61        match self {
62            ModuleInfoDirectiveKind::Version { span, .. } => span.clone(),
63            ModuleInfoDirectiveKind::Target { span, .. } => span.clone(),
64            ModuleInfoDirectiveKind::AddressSize { span, .. } => span.clone(),
65        }
66    }
67}
68
69/// Structured representation of the `.version` directive.
70#[derive(Debug, Clone, PartialEq)]
71pub struct VersionDirective {
72    pub major: u32,
73    pub minor: u32,
74    pub span: Span,
75}
76
77impl VersionDirective {
78    pub fn with_span(mut self, span: Span) -> Self {
79        self.span = span;
80        self
81    }
82}
83
84/// Structured representation of the `.target` directive.
85#[derive(Debug, Clone, PartialEq)]
86pub struct TargetDirective {
87    pub entries: Vec<String>,
88    pub span: Span,
89}
90
91impl TargetDirective {
92    pub fn with_span(mut self, span: Span) -> Self {
93        self.span = span;
94        self
95    }
96}
97
98/// Structured representation of the `.address_size` directive.
99#[derive(Debug, Clone, PartialEq)]
100pub struct AddressSizeDirective {
101    pub size: u32,
102    pub span: Span,
103}
104
105impl AddressSizeDirective {
106    pub fn with_span(mut self, span: Span) -> Self {
107        self.span = span;
108        self
109    }
110}
111
112/// Debugging directives defined by the PTX ISA.
113#[derive(Debug, Clone, PartialEq)]
114pub enum ModuleDebugDirective {
115    File { directive: FileDirective, span: Span },
116    Section { directive: SectionDirective, span: Span },
117    Dwarf { directive: DwarfDirective, span: Span },
118}
119
120impl ModuleDebugDirective {
121    pub fn span(&self) -> Span {
122        match self {
123            ModuleDebugDirective::File { span, .. } => span.clone(),
124            ModuleDebugDirective::Section { span, .. } => span.clone(),
125            ModuleDebugDirective::Dwarf { span, .. } => span.clone(),
126        }
127    }
128}
129
130/// Structured representation of the `.file` directive.
131#[derive(Debug, Clone, PartialEq)]
132pub struct FileDirective {
133    pub index: u32,
134    pub path: String,
135    pub span: Span,
136}
137
138impl FileDirective {
139    pub fn with_span(mut self, span: Span) -> Self {
140        self.span = span;
141        self
142    }
143}
144
145/// Structured representation of the `.section` directive.
146#[derive(Debug, Clone, PartialEq)]
147pub struct SectionDirective {
148    pub name: String,
149    pub attributes: Vec<String>,
150    pub span: Span,
151}
152
153impl SectionDirective {
154    pub fn with_span(mut self, span: Span) -> Self {
155        self.span = span;
156        self
157    }
158}
159
160/// Linking directives that influence symbol visibility. TODO: further parse the
161/// prototype, which should be a function signature.
162#[derive(Debug, Clone, PartialEq)]
163pub struct LinkingDirective {
164    pub kind: CodeOrDataLinkage,
165    pub prototype: String,
166    pub span: Span,
167}
168
169impl LinkingDirective {
170    pub fn with_span(mut self, span: Span) -> Self {
171        self.span = span;
172        self
173    }
174}