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
use cairo_lang_debug::DebugWithDb;
use cairo_lang_diagnostics::DiagnosticAdded;
use cairo_lang_proc_macros::{DebugWithDb, SemanticObject};
use cairo_lang_syntax::node::ast;
use id_arena::Arena;
use smol_str::SmolStr;

use super::fmt::ExprFormatter;
use crate::{semantic, ConcreteStructId, ExprLiteral, ExprStringLiteral, LocalVariable, PatternId};

/// Semantic representation of a Pattern.
/// A pattern is a way to "destructure" values. A pattern may introduce new variables that are bound
/// to inner values of a specific value. For example, a tuple pattern destructures a tuple
/// and may result in new variables for an elements of that tuple.
/// This is used both in let statements and match statements.
// TODO(spapini): Replace this doc with a reference to the language documentation about patterns,
// once it is available.
#[derive(Clone, Debug, PartialEq, Eq, DebugWithDb, SemanticObject)]
#[debug_db(ExprFormatter<'a>)]
pub enum Pattern {
    Literal(PatternLiteral),
    StringLiteral(PatternStringLiteral),
    Variable(PatternVariable),
    Struct(PatternStruct),
    Tuple(PatternTuple),
    EnumVariant(PatternEnumVariant),
    Otherwise(PatternOtherwise),
    Missing(PatternMissing),
}
impl Pattern {
    pub fn ty(&self) -> semantic::TypeId {
        match self {
            Pattern::Literal(literal) => literal.literal.ty,
            Pattern::StringLiteral(string_literal) => string_literal.string_literal.ty,
            Pattern::Variable(variable) => variable.var.ty,
            Pattern::Struct(pattern_struct) => pattern_struct.ty,
            Pattern::Tuple(pattern_tuple) => pattern_tuple.ty,
            Pattern::EnumVariant(pattern_enum_variant) => pattern_enum_variant.ty,
            Pattern::Otherwise(pattern_otherwise) => pattern_otherwise.ty,
            Pattern::Missing(pattern_missing) => pattern_missing.ty,
        }
    }

    pub fn variables(&self, arena: &Arena<Pattern>) -> Vec<PatternVariable> {
        match self {
            Pattern::Variable(variable) => vec![variable.clone()],
            Pattern::Struct(pattern_struct) => pattern_struct
                .field_patterns
                .iter()
                .flat_map(|(_member, pattern)| arena[*pattern].variables(arena))
                .collect(),
            Pattern::Tuple(pattern_tuple) => pattern_tuple
                .field_patterns
                .iter()
                .flat_map(|pattern| arena[*pattern].variables(arena))
                .collect(),
            Pattern::EnumVariant(pattern_enum_variant) => {
                match &pattern_enum_variant.inner_pattern {
                    Some(inner_pattern) => arena[*inner_pattern].variables(arena),
                    None => vec![],
                }
            }
            Pattern::Literal(_)
            | Pattern::StringLiteral(_)
            | Pattern::Otherwise(_)
            | Pattern::Missing(_) => vec![],
        }
    }

    pub fn stable_ptr(&self) -> ast::PatternPtr {
        match self {
            Pattern::Literal(pattern) => pattern.stable_ptr,
            Pattern::StringLiteral(pattern) => pattern.stable_ptr,
            Pattern::Variable(pattern) => pattern.stable_ptr,
            Pattern::Struct(pattern) => pattern.stable_ptr.into(),
            Pattern::Tuple(pattern) => pattern.stable_ptr.into(),
            Pattern::EnumVariant(pattern) => pattern.stable_ptr,
            Pattern::Otherwise(pattern) => pattern.stable_ptr.into(),
            Pattern::Missing(pattern) => pattern.stable_ptr,
        }
    }
}

#[derive(Clone, Debug, Hash, PartialEq, Eq, DebugWithDb, SemanticObject)]
#[debug_db(ExprFormatter<'a>)]
pub struct PatternLiteral {
    pub literal: ExprLiteral,
    #[hide_field_debug_with_db]
    #[dont_rewrite]
    pub stable_ptr: ast::PatternPtr,
}

#[derive(Clone, Debug, Hash, PartialEq, Eq, DebugWithDb, SemanticObject)]
#[debug_db(ExprFormatter<'a>)]
pub struct PatternStringLiteral {
    pub string_literal: ExprStringLiteral,
    #[hide_field_debug_with_db]
    #[dont_rewrite]
    pub stable_ptr: ast::PatternPtr,
}

/// A pattern that binds the matched value to a variable.
#[derive(Clone, Debug, Hash, PartialEq, Eq, SemanticObject)]
pub struct PatternVariable {
    #[dont_rewrite]
    pub name: SmolStr,
    pub var: LocalVariable,
    #[dont_rewrite]
    pub stable_ptr: ast::PatternPtr,
}
impl DebugWithDb<ExprFormatter<'_>> for PatternVariable {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>, _db: &ExprFormatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.name)
    }
}

/// A pattern that destructures a struct to its fields.
#[derive(Clone, Debug, PartialEq, Eq, DebugWithDb, SemanticObject)]
#[debug_db(ExprFormatter<'a>)]
pub struct PatternStruct {
    pub concrete_struct_id: ConcreteStructId,
    // TODO(spapini): This should be ConcreteMember, when available.
    pub field_patterns: Vec<(semantic::Member, PatternId)>,
    pub ty: semantic::TypeId,
    #[dont_rewrite]
    pub n_snapshots: usize,
    #[hide_field_debug_with_db]
    #[dont_rewrite]
    pub stable_ptr: ast::PatternStructPtr,
}

/// A pattern that destructures a tuple to its fields.
#[derive(Clone, Debug, Hash, PartialEq, Eq, DebugWithDb, SemanticObject)]
#[debug_db(ExprFormatter<'a>)]
pub struct PatternTuple {
    pub field_patterns: Vec<PatternId>,
    pub ty: semantic::TypeId,
    #[hide_field_debug_with_db]
    #[dont_rewrite]
    pub stable_ptr: ast::PatternTuplePtr,
}

/// A pattern that destructures a specific variant of an enum to its inner value.
#[derive(Clone, Debug, Hash, PartialEq, Eq, DebugWithDb, SemanticObject)]
#[debug_db(ExprFormatter<'a>)]
pub struct PatternEnumVariant {
    pub variant: semantic::ConcreteVariant,
    pub inner_pattern: Option<PatternId>,
    pub ty: semantic::TypeId,
    #[hide_field_debug_with_db]
    #[dont_rewrite]
    pub stable_ptr: ast::PatternPtr,
}

#[derive(Clone, Debug, Hash, PartialEq, Eq, DebugWithDb, SemanticObject)]
#[debug_db(ExprFormatter<'a>)]
pub struct PatternOtherwise {
    pub ty: semantic::TypeId,
    #[hide_field_debug_with_db]
    #[dont_rewrite]
    pub stable_ptr: ast::TerminalUnderscorePtr,
}

#[derive(Clone, Debug, Hash, PartialEq, Eq, DebugWithDb, SemanticObject)]
#[debug_db(ExprFormatter<'a>)]
pub struct PatternMissing {
    pub ty: semantic::TypeId,
    #[hide_field_debug_with_db]
    #[dont_rewrite]
    pub stable_ptr: ast::PatternPtr,
    #[hide_field_debug_with_db]
    #[dont_rewrite]
    pub diag_added: DiagnosticAdded,
}