Skip to main content

oak_valkyrie/ast/
items_nodes.rs

1use super::{Attribute, Block, EnumVariant, EnumsKind, Expr, Function, GenericParam, Identifier, NamePath, Param, Span, Statement, StructureKind, Type, VariantCase};
2
3/// An item in a Valkyrie module
4#[derive(Debug, Clone, PartialEq, Eq, Hash)]
5#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6pub enum Item {
7    /// A namespace declaration.
8    Namespace(Namespace),
9    /// A class declaration.
10    Class(Class),
11    /// A value type structure (immutable, copied on assignment).
12    Structure(Class),
13    /// A singleton declaration.
14    Singleton(Singleton),
15    /// A flags (bitflags) declaration.
16    Flags(Flags),
17    /// An enum declaration.
18    Enums(Enums),
19    /// A trait declaration.
20    Trait(Trait),
21    /// A widget declaration.
22    Widget(Widget),
23    /// A using (import) statement.
24    Using(Using),
25    /// A micro (small function) declaration.
26    Micro(MicroDefinition),
27    /// A type function declaration.
28    TypeFunction(TypeFunction),
29    /// A statement at module level.
30    Statement(Statement),
31    /// A variant declaration.
32    Variant(Variant),
33    /// An effect declaration.
34    Effect(Effect),
35    /// A property declaration (getter or setter).
36    Property(Property),
37    /// Template text content.
38    TemplateText {
39        /// The text content.
40        content: String,
41        /// The source code span.
42        #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
43        span: Span,
44    },
45    /// Template control structure.
46    TemplateControl {
47        /// The items within the control structure.
48        items: Vec<Item>,
49        /// The source code span.
50        #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
51        span: Span,
52    },
53    /// Template interpolation expression.
54    TemplateInterpolation {
55        /// The interpolated expression.
56        expr: Expr,
57        /// The source code span.
58        #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
59        span: Span,
60    },
61}
62
63/// A namespace declaration
64#[derive(Debug, Clone, PartialEq, Eq, Hash)]
65#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
66pub struct Namespace {
67    /// The name path of the namespace.
68    pub name: NamePath,
69    /// Annotations applied to the namespace.
70    pub annotations: Vec<Attribute>,
71    /// Items declared within the namespace.
72    pub items: Vec<Item>,
73    /// The source code span.
74    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
75    pub span: Span,
76}
77
78/// A parent class with optional alias for renamed inheritance.
79#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
80#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
81pub struct Parent {
82    /// Optional alias for disambiguation (e.g., "primary" in "primary: Parent1").
83    pub alias: Option<Identifier>,
84    /// Parent class name path.
85    pub name: NamePath,
86    /// Source span.
87    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
88    pub span: Span,
89}
90
91/// A class declaration
92#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
93#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
94pub struct Class {
95    /// The keyword kind used for this class (class, struct, structure, widget, trait).
96    pub kind: StructureKind,
97    /// The class name.
98    pub name: Identifier,
99    /// Generic parameters for the class.
100    pub generics: Vec<GenericParam>,
101    /// Parent classes or traits this class inherits from.
102    pub parents: Vec<Parent>,
103    /// Items (fields, methods) declared within the class.
104    pub items: Vec<Item>,
105    /// Annotations applied to the class.
106    pub annotations: Vec<Attribute>,
107    /// The source code span.
108    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
109    pub span: Span,
110    /// Whether this class is abstract (cannot be instantiated directly).
111    pub is_abstract: bool,
112    /// Whether this class is sealed (restricted inheritance).
113    pub is_sealed: bool,
114    /// Whether this class is final (cannot be inherited).
115    pub is_final: bool,
116}
117
118/// A flags (bitflags) declaration
119#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
120#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
121pub struct Flags {
122    /// The flags name.
123    pub name: Identifier,
124    /// The flag variants.
125    pub variants: Vec<EnumVariant>,
126    /// Annotations applied to the flags.
127    pub annotations: Vec<Attribute>,
128    /// The source code span.
129    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
130    pub span: Span,
131}
132
133/// An enum declaration
134#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
135#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
136pub struct Enums {
137    /// The keyword kind used for this enum (enums, enum, unity).
138    pub kind: EnumsKind,
139    /// The enum name.
140    pub name: Identifier,
141    /// Generic parameters for the enum.
142    pub generics: Vec<GenericParam>,
143    /// The enum variants.
144    pub variants: Vec<EnumVariant>,
145    /// Annotations applied to the enum.
146    pub annotations: Vec<Attribute>,
147    /// The source code span.
148    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
149    pub span: Span,
150}
151
152/// A trait declaration
153#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
154#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
155pub struct Trait {
156    /// The trait name.
157    pub name: Identifier,
158    /// Generic parameters for the trait.
159    pub generics: Vec<GenericParam>,
160    /// Methods declared in the trait.
161    pub methods: Vec<Function>,
162    /// Associated types declared in the trait.
163    pub associated_types: Vec<AssociatedType>,
164    /// Annotations applied to the trait.
165    pub annotations: Vec<Attribute>,
166    /// The source code span.
167    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
168    pub span: Span,
169}
170
171/// An associated type declaration in a trait.
172#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
173#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
174pub struct AssociatedType {
175    /// The associated type name.
176    pub name: Identifier,
177    /// Type bounds that the associated type must satisfy.
178    pub bounds: Vec<Type>,
179    /// Default type for the associated type, if any.
180    pub default: Option<Type>,
181    /// Annotations applied to the associated type.
182    pub annotations: Vec<Attribute>,
183    /// The source code span.
184    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
185    pub span: Span,
186}
187
188/// A widget declaration
189#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
190#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
191pub struct Widget {
192    /// The widget name.
193    pub name: Identifier,
194    /// Generic parameters for the widget.
195    pub generics: Vec<GenericParam>,
196    /// Items (properties, methods) declared within the widget.
197    pub items: Vec<Item>,
198    /// Annotations applied to the widget.
199    pub annotations: Vec<Attribute>,
200    /// The source code span.
201    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
202    pub span: Span,
203}
204
205/// A singleton declaration.
206///
207/// Singletons are classes that have exactly one instance globally.
208/// They are useful for managing global state, configuration, or resources.
209///
210/// # Example
211///
212/// ```v
213/// singleton GlobalConfig {
214///     host: String = "localhost"
215///     port: i32 = 8080
216///
217///     micro get_url(self) -> String {
218///         f"{self.host}:{self.port}"
219///     }
220/// }
221/// ```
222///
223/// # Semantics
224///
225/// - A singleton has exactly one global instance
226/// - The instance is lazily initialized on first access
227/// - Singleton members are accessed through the singleton name directly
228/// - Singletons cannot be instantiated with constructors
229#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
230#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
231pub struct Singleton {
232    /// The singleton name.
233    pub name: Identifier,
234    /// Generic parameters for the singleton.
235    pub generics: Vec<GenericParam>,
236    /// Parent traits this singleton implements.
237    pub parents: Vec<Parent>,
238    /// Items (fields, methods) declared within the singleton.
239    pub items: Vec<Item>,
240    /// Annotations applied to the singleton.
241    pub annotations: Vec<Attribute>,
242    /// The source code span.
243    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
244    pub span: Span,
245}
246
247/// A using (import) statement
248#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
249#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
250pub struct Using {
251    /// The path to import.
252    pub path: NamePath,
253    /// Optional alias for the import.
254    pub alias: Option<Identifier>,
255    /// Selective import list (e.g., `{Never, Unit}` in `using core::primitive.{Never, Unit}`)
256    pub imports: Vec<Identifier>,
257    /// The source code span.
258    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
259    pub span: Span,
260}
261
262/// A micro (small function) declaration
263#[derive(Debug, Clone, PartialEq, Eq, Hash)]
264#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
265pub struct MicroDefinition {
266    /// The micro name.
267    pub name: Identifier,
268    /// Generic parameters for the micro.
269    pub generics: Vec<GenericParam>,
270    /// Parameters for the micro.
271    pub params: Vec<Param>,
272    /// Return type annotation, if any.
273    pub return_type: Option<Type>,
274    /// The body of the micro.
275    pub body: Block,
276    /// Annotations applied to the micro.
277    pub annotations: Vec<Attribute>,
278    /// The source code span.
279    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
280    pub span: Span,
281    /// Whether this function is abstract (has no body implementation).
282    pub is_abstract: bool,
283    /// Whether this function is final (cannot be overridden).
284    pub is_final: bool,
285}
286
287/// A type function declaration
288#[derive(Debug, Clone, PartialEq, Eq, Hash)]
289#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
290pub struct TypeFunction {
291    /// The type function name.
292    pub name: Identifier,
293    /// Generic parameters for the type function.
294    pub generics: Vec<GenericParam>,
295    /// Parameters for the type function.
296    pub params: Vec<Param>,
297    /// Return type annotation, if any.
298    pub return_type: Option<Type>,
299    /// The body of the type function.
300    pub body: Block,
301    /// Annotations applied to the type function.
302    pub annotations: Vec<Attribute>,
303    /// The source code span.
304    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
305    pub span: Span,
306}
307
308/// A variant declaration
309#[derive(Debug, Clone, PartialEq, Eq, Hash)]
310#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
311pub struct Variant {
312    /// The variant name.
313    pub name: Identifier,
314    /// Generic parameters for the variant.
315    pub generics: Vec<GenericParam>,
316    /// The variant cases.
317    pub cases: Vec<VariantCase>,
318    /// Annotations applied to the variant.
319    pub annotations: Vec<Attribute>,
320    /// The source code span.
321    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
322    pub span: Span,
323}
324
325/// An effect declaration
326#[derive(Debug, Clone, PartialEq, Eq, Hash)]
327#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
328pub struct Effect {
329    /// The effect name.
330    pub name: Identifier,
331    /// Operations defined by the effect.
332    pub operations: Vec<Function>,
333    /// Annotations applied to the effect.
334    pub annotations: Vec<Attribute>,
335    /// The source code span.
336    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
337    pub span: Span,
338}
339
340/// The kind of a property (getter or setter).
341#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
342#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
343pub enum PropertyKind {
344    /// A getter property.
345    Getter,
346    /// A setter property.
347    Setter,
348}
349
350/// A property declaration (getter or setter).
351#[derive(Debug, Clone, PartialEq, Eq, Hash)]
352#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
353pub struct Property {
354    /// The name of the property.
355    pub name: Identifier,
356    /// Whether this is a getter or setter.
357    pub kind: PropertyKind,
358    /// Generic parameters for the property.
359    pub generics: Vec<GenericParam>,
360    /// Annotations on the property.
361    pub annotations: Vec<Attribute>,
362    /// Parameters for the property (self for getter, self + value for setter).
363    pub params: Vec<Param>,
364    /// Return type for getter, None for setter.
365    pub return_type: Option<Type>,
366    /// The body of the property.
367    pub body: Block,
368    /// Source span.
369    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
370    pub span: Span,
371    /// Whether this property is abstract (has no body implementation).
372    ///
373    /// Abstract properties are declared without a body in abstract classes
374    /// and must be implemented by concrete subclasses.
375    pub is_abstract: bool,
376    /// Whether this property is final (cannot be overridden).
377    pub is_final: bool,
378    /// Whether this property is static (belongs to the class, not instances).
379    ///
380    /// Static properties are accessed via `ClassName.property_name` syntax
381    /// and do not have access to `self`.
382    pub is_static: bool,
383    /// Whether this property is virtual (can be overridden by subclasses).
384    ///
385    /// Virtual properties use dynamic dispatch through the vtable,
386    /// allowing subclasses to provide their own implementation.
387    pub is_virtual: bool,
388    /// Whether this property overrides a parent class property.
389    ///
390    /// Override properties must match the signature of the parent property
391    /// and are verified during type checking.
392    pub is_override: bool,
393    /// Whether this property uses lazy initialization.
394    ///
395    /// Lazy properties cache their computed value after first access.
396    /// The getter is only called once, and subsequent accesses return
397    /// the cached value.
398    pub is_lazy: bool,
399}