quantalang 1.0.0

The QuantaLang compiler — an effects-oriented systems language with multi-backend codegen (C, HLSL, GLSL, SPIR-V, LLVM IR, WebAssembly, x86-64, ARM64)
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
// ===============================================================================
// QUANTALANG AST - ITEMS (TOP-LEVEL DECLARATIONS)
// ===============================================================================
// Copyright (c) 2022-2026 Zain Dana Harper. MIT License.
// ===============================================================================

//! Item AST nodes.
//!
//! Items are top-level declarations: functions, structs, enums, traits, etc.

use super::{
    Attribute, Block, Expr, Generics, Ident, Mutability, NodeId, Path, Pattern, Type, TypeBound,
    Visibility,
};
use crate::lexer::Span;

/// An item node.
#[derive(Debug, Clone, PartialEq)]
pub struct Item {
    /// The kind of item.
    pub kind: ItemKind,
    /// Visibility.
    pub vis: Visibility,
    /// Attributes on this item.
    pub attrs: Vec<Attribute>,
    /// The span of this item.
    pub span: Span,
    /// Node ID.
    pub id: NodeId,
}

impl Item {
    /// Create a new item.
    pub fn new(kind: ItemKind, vis: Visibility, attrs: Vec<Attribute>, span: Span) -> Self {
        Self {
            kind,
            vis,
            attrs,
            span,
            id: NodeId::DUMMY,
        }
    }

    /// Get the name of this item (if it has one).
    pub fn name(&self) -> Option<&Ident> {
        match &self.kind {
            ItemKind::Function(f) => Some(&f.name),
            ItemKind::Struct(s) => Some(&s.name),
            ItemKind::Enum(e) => Some(&e.name),
            ItemKind::Trait(t) => Some(&t.name),
            ItemKind::TypeAlias(t) => Some(&t.name),
            ItemKind::Const(c) => Some(&c.name),
            ItemKind::Static(s) => Some(&s.name),
            ItemKind::Mod(m) => Some(&m.name),
            ItemKind::Impl(_) => None,
            ItemKind::Use(_) => None,
            ItemKind::ExternCrate(e) => Some(&e.name),
            ItemKind::ExternBlock(_) => None,
            ItemKind::Macro(m) => m.name.as_ref(),
            ItemKind::MacroRules(m) => Some(&m.name),
            ItemKind::Effect(e) => Some(&e.name),
        }
    }
}

/// The kind of item.
#[derive(Debug, Clone, PartialEq)]
pub enum ItemKind {
    /// Function definition.
    Function(Box<FnDef>),
    /// Struct definition.
    Struct(Box<StructDef>),
    /// Enum definition.
    Enum(Box<EnumDef>),
    /// Trait definition.
    Trait(Box<TraitDef>),
    /// Impl block.
    Impl(Box<ImplDef>),
    /// Type alias.
    TypeAlias(Box<TypeAliasDef>),
    /// Constant.
    Const(Box<ConstDef>),
    /// Static.
    Static(Box<StaticDef>),
    /// Module.
    Mod(Box<ModDef>),
    /// Use declaration.
    Use(Box<UseDef>),
    /// Extern crate.
    ExternCrate(Box<ExternCrateDef>),
    /// Extern block.
    ExternBlock(Box<ExternBlockDef>),
    /// Macro definition.
    Macro(Box<MacroDef>),
    /// Macro rules.
    MacroRules(Box<MacroRulesDef>),
    /// Effect definition (QuantaLang extension).
    Effect(Box<EffectDef>),
}

/// Function definition.
#[derive(Debug, Clone, PartialEq)]
pub struct FnDef {
    /// Function name.
    pub name: Ident,
    /// Generic parameters.
    pub generics: Generics,
    /// Function signature.
    pub sig: FnSig,
    /// Function body (None for declarations).
    pub body: Option<Box<Block>>,
}

/// Function signature.
#[derive(Debug, Clone, PartialEq)]
pub struct FnSig {
    /// Whether this is unsafe.
    pub is_unsafe: bool,
    /// Whether this is async.
    pub is_async: bool,
    /// Whether this is const.
    pub is_const: bool,
    /// Extern ABI (if any).
    pub abi: Option<String>,
    /// Parameters.
    pub params: Vec<Param>,
    /// Return type (None for unit).
    pub return_ty: Option<Box<Type>>,
    /// Effect annotations.
    pub effects: Vec<Path>,
}

impl Default for FnSig {
    fn default() -> Self {
        Self {
            is_unsafe: false,
            is_async: false,
            is_const: false,
            abi: None,
            params: Vec::new(),
            return_ty: None,
            effects: Vec::new(),
        }
    }
}

/// Function parameter.
#[derive(Debug, Clone, PartialEq)]
pub struct Param {
    /// Attributes.
    pub attrs: Vec<Attribute>,
    /// The pattern.
    pub pattern: Pattern,
    /// The type.
    pub ty: Box<Type>,
    /// Default value.
    pub default: Option<Box<Expr>>,
    /// Span.
    pub span: Span,
}

/// Struct definition.
#[derive(Debug, Clone, PartialEq)]
pub struct StructDef {
    /// Struct name.
    pub name: Ident,
    /// Generic parameters.
    pub generics: Generics,
    /// Struct fields.
    pub fields: StructFields,
}

/// Struct field variants.
#[derive(Debug, Clone, PartialEq)]
pub enum StructFields {
    /// Named fields: `struct Point { x: i32, y: i32 }`
    Named(Vec<FieldDef>),
    /// Tuple fields: `struct Point(i32, i32);`
    Tuple(Vec<TupleFieldDef>),
    /// Unit struct: `struct Unit;`
    Unit,
}

/// Named field definition.
#[derive(Debug, Clone, PartialEq)]
pub struct FieldDef {
    /// Visibility.
    pub vis: Visibility,
    /// Attributes.
    pub attrs: Vec<Attribute>,
    /// Field name.
    pub name: Ident,
    /// Field type.
    pub ty: Box<Type>,
    /// Default value.
    pub default: Option<Box<Expr>>,
    /// Span.
    pub span: Span,
}

/// Tuple field definition.
#[derive(Debug, Clone, PartialEq)]
pub struct TupleFieldDef {
    /// Visibility.
    pub vis: Visibility,
    /// Attributes.
    pub attrs: Vec<Attribute>,
    /// Field type.
    pub ty: Box<Type>,
    /// Span.
    pub span: Span,
}

/// Enum definition.
#[derive(Debug, Clone, PartialEq)]
pub struct EnumDef {
    /// Enum name.
    pub name: Ident,
    /// Generic parameters.
    pub generics: Generics,
    /// Variants.
    pub variants: Vec<EnumVariant>,
}

/// Enum variant.
#[derive(Debug, Clone, PartialEq)]
pub struct EnumVariant {
    /// Attributes.
    pub attrs: Vec<Attribute>,
    /// Variant name.
    pub name: Ident,
    /// Variant fields.
    pub fields: StructFields,
    /// Discriminant expression.
    pub discriminant: Option<Box<Expr>>,
    /// Span.
    pub span: Span,
}

/// Trait definition.
#[derive(Debug, Clone, PartialEq)]
pub struct TraitDef {
    /// Trait name.
    pub name: Ident,
    /// Whether this is unsafe.
    pub is_unsafe: bool,
    /// Whether this is auto.
    pub is_auto: bool,
    /// Generic parameters.
    pub generics: Generics,
    /// Supertraits.
    pub supertraits: Vec<TypeBound>,
    /// Trait items.
    pub items: Vec<TraitItem>,
}

/// Item in a trait definition.
#[derive(Debug, Clone, PartialEq)]
pub struct TraitItem {
    /// Attributes.
    pub attrs: Vec<Attribute>,
    /// The kind of trait item.
    pub kind: TraitItemKind,
    /// Span.
    pub span: Span,
    /// Node ID.
    pub id: NodeId,
}

/// Kind of trait item.
#[derive(Debug, Clone, PartialEq)]
pub enum TraitItemKind {
    /// Associated function/method.
    Function(Box<FnDef>),
    /// Associated type.
    Type {
        name: Ident,
        generics: Generics,
        bounds: Vec<TypeBound>,
        default: Option<Box<Type>>,
    },
    /// Associated constant.
    Const {
        name: Ident,
        ty: Box<Type>,
        default: Option<Box<Expr>>,
    },
    /// Macro invocation.
    Macro {
        path: Path,
        tokens: Vec<super::TokenTree>,
    },
}

/// Impl block.
#[derive(Debug, Clone, PartialEq)]
pub struct ImplDef {
    /// Whether this is unsafe.
    pub is_unsafe: bool,
    /// Whether this is negative (`impl !Trait for Type`).
    pub is_negative: bool,
    /// Generic parameters.
    pub generics: Generics,
    /// Trait being implemented (None for inherent impl).
    pub trait_ref: Option<TraitRef>,
    /// Type being implemented for.
    pub self_ty: Box<Type>,
    /// Impl items.
    pub items: Vec<ImplItem>,
}

/// Trait reference in an impl.
#[derive(Debug, Clone, PartialEq)]
pub struct TraitRef {
    /// The trait path.
    pub path: Path,
    /// Whether this is a `!Trait` bound.
    pub is_negative: bool,
}

/// Item in an impl block.
#[derive(Debug, Clone, PartialEq)]
pub struct ImplItem {
    /// Visibility.
    pub vis: Visibility,
    /// Attributes.
    pub attrs: Vec<Attribute>,
    /// Whether this is a default impl.
    pub is_default: bool,
    /// The kind of impl item.
    pub kind: ImplItemKind,
    /// Span.
    pub span: Span,
    /// Node ID.
    pub id: NodeId,
}

/// Kind of impl item.
#[derive(Debug, Clone, PartialEq)]
pub enum ImplItemKind {
    /// Associated function/method.
    Function(Box<FnDef>),
    /// Associated type.
    Type {
        name: Ident,
        generics: Generics,
        ty: Box<Type>,
    },
    /// Associated constant.
    Const {
        name: Ident,
        ty: Box<Type>,
        value: Box<Expr>,
    },
    /// Macro invocation.
    Macro {
        path: Path,
        tokens: Vec<super::TokenTree>,
    },
}

/// Type alias definition.
#[derive(Debug, Clone, PartialEq)]
pub struct TypeAliasDef {
    /// Type name.
    pub name: Ident,
    /// Generic parameters.
    pub generics: Generics,
    /// Bounds (for associated types).
    pub bounds: Vec<TypeBound>,
    /// The aliased type.
    pub ty: Option<Box<Type>>,
}

/// Constant definition.
#[derive(Debug, Clone, PartialEq)]
pub struct ConstDef {
    /// Constant name.
    pub name: Ident,
    /// Type.
    pub ty: Box<Type>,
    /// Value.
    pub value: Option<Box<Expr>>,
}

/// Static definition.
#[derive(Debug, Clone, PartialEq)]
pub struct StaticDef {
    /// Static name.
    pub name: Ident,
    /// Mutability.
    pub mutability: Mutability,
    /// Type.
    pub ty: Box<Type>,
    /// Value.
    pub value: Option<Box<Expr>>,
}

/// Module definition.
#[derive(Debug, Clone, PartialEq)]
pub struct ModDef {
    /// Module name.
    pub name: Ident,
    /// Module content (None for `mod foo;`).
    pub content: Option<ModContent>,
    /// Whether this is unsafe.
    pub is_unsafe: bool,
}

/// Module content.
#[derive(Debug, Clone, PartialEq)]
pub struct ModContent {
    /// Inner attributes.
    pub attrs: Vec<Attribute>,
    /// Items.
    pub items: Vec<Item>,
    /// Span.
    pub span: Span,
}

/// Use declaration.
#[derive(Debug, Clone, PartialEq)]
pub struct UseDef {
    /// The use tree.
    pub tree: UseTree,
}

/// Use tree.
#[derive(Debug, Clone, PartialEq)]
pub struct UseTree {
    /// The kind of use tree.
    pub kind: UseTreeKind,
    /// Span.
    pub span: Span,
}

/// Kind of use tree.
#[derive(Debug, Clone, PartialEq)]
pub enum UseTreeKind {
    /// Simple path: `use std::io;`
    Simple { path: Path, rename: Option<Ident> },
    /// Glob: `use std::io::*;`
    Glob(Path),
    /// Nested: `use std::{io, fs};`
    Nested { path: Path, trees: Vec<UseTree> },
}

/// Extern crate.
#[derive(Debug, Clone, PartialEq)]
pub struct ExternCrateDef {
    /// Crate name.
    pub name: Ident,
    /// Rename.
    pub rename: Option<Ident>,
}

/// Extern block.
#[derive(Debug, Clone, PartialEq)]
pub struct ExternBlockDef {
    /// Whether this is unsafe.
    pub is_unsafe: bool,
    /// ABI.
    pub abi: Option<String>,
    /// Items.
    pub items: Vec<ForeignItem>,
}

/// Item in an extern block.
#[derive(Debug, Clone, PartialEq)]
pub struct ForeignItem {
    /// Visibility.
    pub vis: Visibility,
    /// Attributes.
    pub attrs: Vec<Attribute>,
    /// The kind of foreign item.
    pub kind: ForeignItemKind,
    /// Span.
    pub span: Span,
    /// Node ID.
    pub id: NodeId,
}

/// Kind of foreign item.
#[derive(Debug, Clone, PartialEq)]
pub enum ForeignItemKind {
    /// Foreign function.
    Fn(Box<FnDef>),
    /// Foreign static.
    Static {
        name: Ident,
        mutability: Mutability,
        ty: Box<Type>,
    },
    /// Foreign type.
    Type { name: Ident, bounds: Vec<TypeBound> },
    /// Macro invocation.
    Macro {
        path: Path,
        tokens: Vec<super::TokenTree>,
    },
}

/// Macro definition.
#[derive(Debug, Clone, PartialEq)]
pub struct MacroDef {
    /// Macro name.
    pub name: Option<Ident>,
    /// Macro body.
    pub body: Vec<super::TokenTree>,
}

/// Macro rules definition.
#[derive(Debug, Clone, PartialEq)]
pub struct MacroRulesDef {
    /// Macro name.
    pub name: Ident,
    /// Rules.
    pub rules: Vec<MacroRule>,
}

/// A macro rule.
#[derive(Debug, Clone, PartialEq)]
pub struct MacroRule {
    /// Pattern.
    pub pattern: Vec<super::TokenTree>,
    /// Body.
    pub body: Vec<super::TokenTree>,
    /// Span.
    pub span: Span,
}

/// Effect definition (QuantaLang extension).
#[derive(Debug, Clone, PartialEq)]
pub struct EffectDef {
    /// Effect name.
    pub name: Ident,
    /// Generic parameters.
    pub generics: Generics,
    /// Operations.
    pub operations: Vec<EffectOperation>,
}

/// An operation in an effect.
#[derive(Debug, Clone, PartialEq)]
pub struct EffectOperation {
    /// Attributes.
    pub attrs: Vec<Attribute>,
    /// Operation name.
    pub name: Ident,
    /// Parameters.
    pub params: Vec<Param>,
    /// Return type.
    pub return_ty: Option<Box<Type>>,
    /// Span.
    pub span: Span,
}