miden-assembly-syntax 0.24.0

Parsing and semantic analysis of the Miden Assembly language
Documentation
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
use alloc::{sync::Arc, vec::Vec};

use miden_debug_types::{SourceSpan, Span, Spanned};

use super::{Ident, Path, Visibility};

/// The explicit source form used by an import declaration.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ImportKind {
    /// A module import such as `use some::module` or `use some::module as sm`.
    Module,
    /// An item import such as `use {foo, bar as baz} from some::module`.
    Item,
}

/// A source-level import declaration.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ImportDecl {
    Module(ModuleImport),
    Items(ItemImportGroup),
}

/// A concrete import recorded in a semantically-analyzed module.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Import {
    Module(ModuleImport),
    Item(ItemImport),
}

/// Imports a foreign module into scope under a local name.
#[derive(Debug, Clone)]
pub struct ModuleImport {
    span: SourceSpan,
    visibility: Visibility,
    module_path: Span<Arc<Path>>,
    local_name: Ident,
    /// The number of times this import has been used locally.
    pub uses: usize,
}

/// Imports one or more foreign items from a module.
#[derive(Debug, Clone)]
pub struct ItemImportGroup {
    span: SourceSpan,
    visibility: Visibility,
    module_path: Span<Arc<Path>>,
    specs: Vec<ImportSpec>,
}

/// A single item import within an item import group.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ImportSpec {
    source_name: Ident,
    local_name: Ident,
}

/// Imports a single foreign item from a module into the local scope.
#[derive(Debug, Clone)]
pub struct ItemImport {
    span: SourceSpan,
    visibility: Visibility,
    module_path: Span<Arc<Path>>,
    source_name: Ident,
    local_name: Ident,
    /// The number of times this import has been used locally.
    pub uses: usize,
}

impl ImportDecl {
    pub fn kind(&self) -> ImportKind {
        match self {
            Self::Module(_) => ImportKind::Module,
            Self::Items(_) => ImportKind::Item,
        }
    }

    pub fn visibility(&self) -> Visibility {
        match self {
            Self::Module(import) => import.visibility(),
            Self::Items(import) => import.visibility(),
        }
    }

    pub fn module_path(&self) -> Span<&Path> {
        match self {
            Self::Module(import) => import.module_path(),
            Self::Items(import) => import.module_path(),
        }
    }
}

impl Import {
    pub fn kind(&self) -> ImportKind {
        match self {
            Self::Module(_) => ImportKind::Module,
            Self::Item(_) => ImportKind::Item,
        }
    }

    pub fn visibility(&self) -> Visibility {
        match self {
            Self::Module(import) => import.visibility(),
            Self::Item(import) => import.visibility(),
        }
    }

    pub fn module_path(&self) -> Span<&Path> {
        match self {
            Self::Module(import) => import.module_path(),
            Self::Item(import) => import.module_path(),
        }
    }

    pub fn local_name(&self) -> &Ident {
        match self {
            Self::Module(import) => import.local_name(),
            Self::Item(import) => import.local_name(),
        }
    }

    /// Returns true if this import has at least one use in its containing module.
    pub fn is_used(&self) -> bool {
        match self {
            Self::Module(import) => import.is_used(),
            Self::Item(import) => import.is_used(),
        }
    }

    /// Returns the most specific source span for unused-import diagnostics.
    pub fn unused_span(&self) -> SourceSpan {
        match self {
            Self::Module(import) => import.local_name().span(),
            Self::Item(import) => import.local_name().span(),
        }
    }
}

impl ModuleImport {
    pub fn new(
        span: SourceSpan,
        visibility: Visibility,
        module_path: Span<Arc<Path>>,
        local_name: Ident,
    ) -> Self {
        Self {
            span,
            visibility,
            module_path,
            local_name,
            uses: 0,
        }
    }

    pub fn visibility(&self) -> Visibility {
        self.visibility
    }

    pub fn module_path(&self) -> Span<&Path> {
        self.module_path.as_deref()
    }

    pub fn set_module_path(&mut self, path: Span<Arc<Path>>) {
        self.module_path = path;
    }

    pub fn local_name(&self) -> &Ident {
        &self.local_name
    }

    /// Returns true if this import has at least one use in its containing module.
    pub fn is_used(&self) -> bool {
        self.uses > 0
    }
}

impl ItemImportGroup {
    pub fn new(
        span: SourceSpan,
        visibility: Visibility,
        module_path: Span<Arc<Path>>,
        specs: Vec<ImportSpec>,
    ) -> Self {
        Self { span, visibility, module_path, specs }
    }

    pub fn visibility(&self) -> Visibility {
        self.visibility
    }

    pub fn module_path(&self) -> Span<&Path> {
        self.module_path.as_deref()
    }

    pub fn specs(&self) -> &[ImportSpec] {
        &self.specs
    }
}

impl ImportSpec {
    pub fn new(source_name: Ident, local_name: Ident) -> Self {
        Self { source_name, local_name }
    }

    pub fn source_name(&self) -> &Ident {
        &self.source_name
    }

    pub fn local_name(&self) -> &Ident {
        &self.local_name
    }

    pub fn is_renamed(&self) -> bool {
        self.source_name != self.local_name
    }
}

impl ItemImport {
    pub fn new(
        span: SourceSpan,
        visibility: Visibility,
        module_path: Span<Arc<Path>>,
        source_name: Ident,
        local_name: Ident,
    ) -> Self {
        Self {
            span,
            visibility,
            module_path,
            source_name,
            local_name,
            uses: 0,
        }
    }

    pub fn visibility(&self) -> Visibility {
        self.visibility
    }

    pub fn module_path(&self) -> Span<&Path> {
        self.module_path.as_deref()
    }

    pub fn source_name(&self) -> &Ident {
        &self.source_name
    }

    pub fn local_name(&self) -> &Ident {
        &self.local_name
    }

    pub fn target_path(&self) -> Span<Arc<Path>> {
        Span::new(self.source_name.span(), self.module_path.inner().join(&self.source_name).into())
    }

    /// Returns true if this import has at least one use in its containing module.
    pub fn is_used(&self) -> bool {
        self.uses > 0 || self.visibility.is_public()
    }
}

impl Spanned for Import {
    fn span(&self) -> SourceSpan {
        match self {
            Self::Module(import) => import.span(),
            Self::Item(import) => import.span(),
        }
    }
}

impl Spanned for ImportDecl {
    fn span(&self) -> SourceSpan {
        match self {
            Self::Module(import) => import.span(),
            Self::Items(import) => import.span(),
        }
    }
}

impl Spanned for ModuleImport {
    fn span(&self) -> SourceSpan {
        self.span
    }
}

impl Spanned for ItemImportGroup {
    fn span(&self) -> SourceSpan {
        self.span
    }
}

impl Spanned for ImportSpec {
    fn span(&self) -> SourceSpan {
        self.source_name.span()
    }
}

impl Spanned for ItemImport {
    fn span(&self) -> SourceSpan {
        self.span
    }
}

impl Eq for ModuleImport {}

impl PartialEq for ModuleImport {
    fn eq(&self, other: &Self) -> bool {
        self.visibility == other.visibility
            && self.module_path.inner() == other.module_path.inner()
            && self.local_name == other.local_name
    }
}

impl Eq for ItemImportGroup {}

impl PartialEq for ItemImportGroup {
    fn eq(&self, other: &Self) -> bool {
        self.visibility == other.visibility
            && self.module_path.inner() == other.module_path.inner()
            && self.specs == other.specs
    }
}

impl Eq for ItemImport {}

impl PartialEq for ItemImport {
    fn eq(&self, other: &Self) -> bool {
        self.visibility == other.visibility
            && self.module_path.inner() == other.module_path.inner()
            && self.source_name == other.source_name
            && self.local_name == other.local_name
    }
}

impl crate::prettier::PrettyPrint for ImportDecl {
    fn render(&self) -> crate::prettier::Document {
        match self {
            Self::Module(import) => import.render(),
            Self::Items(import) => import.render(),
        }
    }
}

impl crate::prettier::PrettyPrint for Import {
    fn render(&self) -> crate::prettier::Document {
        match self {
            Self::Module(import) => import.render(),
            Self::Item(import) => import.render(),
        }
    }
}

impl crate::prettier::PrettyPrint for ModuleImport {
    fn render(&self) -> crate::prettier::Document {
        use crate::prettier::*;

        let mut doc = const_text("use") + const_text(" ") + display(self.module_path.inner());
        if self.module_path.last().is_none_or(|name| name != self.local_name.as_str()) {
            doc += const_text(" as ") + display(&self.local_name);
        }
        doc
    }
}

impl crate::prettier::PrettyPrint for ItemImportGroup {
    fn render(&self) -> crate::prettier::Document {
        use crate::prettier::*;

        let mut doc = Document::Empty;
        if self.visibility.is_public() {
            doc += display(self.visibility) + const_text(" ");
        }
        doc += const_text("use {");

        for (index, spec) in self.specs.iter().enumerate() {
            if index > 0 {
                doc += const_text(", ");
            }
            doc += spec.render();
        }

        doc + const_text("} from ") + display(self.module_path.inner())
    }
}

impl crate::prettier::PrettyPrint for ImportSpec {
    fn render(&self) -> crate::prettier::Document {
        use crate::prettier::*;

        let mut doc = display(&self.source_name);
        if self.is_renamed() {
            doc += const_text(" as ") + display(&self.local_name);
        }
        doc
    }
}

impl crate::prettier::PrettyPrint for ItemImport {
    fn render(&self) -> crate::prettier::Document {
        use crate::prettier::*;

        let mut doc = Document::Empty;
        if self.visibility.is_public() {
            doc += display(self.visibility) + const_text(" ");
        }
        doc += const_text("use {") + display(&self.source_name);
        if self.source_name != self.local_name {
            doc += const_text(" as ") + display(&self.local_name);
        }
        doc + const_text("} from ") + display(self.module_path.inner())
    }
}