cl_typeck/
source.rs

1//! Holds the [Source] of a definition in the AST
2
3use cl_ast::ast::*;
4use std::fmt;
5
6#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
7pub enum Source<'a> {
8    Root,
9    Module(&'a Module),
10    Alias(&'a Alias),
11    Enum(&'a Enum),
12    Variant(&'a Variant),
13    Struct(&'a Struct),
14    Const(&'a Const),
15    Static(&'a Static),
16    Function(&'a Function),
17    Local(&'a Let),
18    Impl(&'a Impl),
19    Use(&'a Use),
20    Ty(&'a TyKind),
21}
22
23impl Source<'_> {
24    pub fn name(&self) -> Option<Sym> {
25        match self {
26            Source::Root => None,
27            Source::Module(v) => Some(v.name),
28            Source::Alias(v) => Some(v.name),
29            Source::Enum(v) => Some(v.name),
30            Source::Variant(v) => Some(v.name),
31            Source::Struct(v) => Some(v.name),
32            Source::Const(v) => Some(v.name),
33            Source::Static(v) => Some(v.name),
34            Source::Function(v) => Some(v.name),
35            Source::Local(_) => None,
36            Source::Impl(_) | Source::Use(_) | Source::Ty(_) => None,
37        }
38    }
39
40    /// Returns `true` if this [Source] defines a named value
41    pub fn is_named_value(&self) -> bool {
42        matches!(self, Self::Const(_) | Self::Static(_) | Self::Function(_))
43    }
44
45    /// Returns `true` if this [Source] defines a named type
46    pub fn is_named_type(&self) -> bool {
47        matches!(
48            self,
49            Self::Module(_) | Self::Alias(_) | Self::Enum(_) | Self::Struct(_)
50        )
51    }
52
53    /// Returns `true` if this [Source] refers to a [Ty] with no name
54    pub fn is_anon_type(&self) -> bool {
55        matches!(self, Self::Ty(_))
56    }
57
58    /// Returns `true` if this [Source] refers to an [Impl] block
59    pub fn is_impl(&self) -> bool {
60        matches!(self, Self::Impl(_))
61    }
62
63    /// Returns `true` if this [Source] refers to a [Use] import
64    pub fn is_use_import(&self) -> bool {
65        matches!(self, Self::Use(_))
66    }
67}
68
69impl fmt::Display for Source<'_> {
70    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71        match self {
72            Self::Root => "🌳 root 🌳".fmt(f),
73            Self::Module(arg0) => arg0.fmt(f),
74            Self::Alias(arg0) => arg0.fmt(f),
75            Self::Enum(arg0) => arg0.fmt(f),
76            Self::Variant(arg0) => arg0.fmt(f),
77            Self::Struct(arg0) => arg0.fmt(f),
78            Self::Const(arg0) => arg0.fmt(f),
79            Self::Static(arg0) => arg0.fmt(f),
80            Self::Function(arg0) => arg0.fmt(f),
81            Self::Impl(arg0) => arg0.fmt(f),
82            Self::Use(arg0) => arg0.fmt(f),
83            Self::Ty(arg0) => arg0.fmt(f),
84            Self::Local(arg0) => arg0.fmt(f),
85        }
86    }
87}