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
use std::fmt::Write;

use downcast_rs::{impl_downcast, Downcast};
use thiserror::Error;

use super::{
    block::Block,
    context::Context,
    mnemonic::Mnemonic,
    parse::{ParseFn, ParseState, TokenKind},
    value::Value,
};
use crate::{
    core::parse::ParseErrorKind,
    delimiter,
    parse_error,
    support::cast::{CastMut, CastRef},
    ArenaPtr,
    ControlFlow,
    DataFlow,
    Parse,
    ParseResult,
    Print,
    PrintResult,
    PrintState,
    Region,
    RegionInterface,
    Verify,
};

/// The successor.
pub struct Successor {
    /// The block destination of the successor.
    block: ArenaPtr<Block>,
    /// The arguments passed to the block.
    args: Vec<ArenaPtr<Value>>,
}

impl Successor {
    /// Create a new successor entity.
    pub fn new(block: ArenaPtr<Block>, args: Vec<ArenaPtr<Value>>) -> Self { Self { block, args } }

    /// Get the block destination of the successor.
    pub fn block(&self) -> ArenaPtr<Block> { self.block }

    /// Get the arguments passed to the block.
    pub fn args(&self) -> &[ArenaPtr<Value>] { &self.args }
}

#[derive(Debug, Error)]
#[error("expect block label, found {0:?}")]
struct ExpectBlockLabelInSuccessor(TokenKind);

impl Parse for Successor {
    type Item = Successor;

    /// Parse the successor.
    ///
    /// # Syntax
    ///
    /// ```text
    /// <block_label> `(` <arg_name_list> `)`
    /// ```
    fn parse(ctx: &mut Context, state: &mut ParseState) -> ParseResult<Self::Item> {
        let token = state.stream.consume()?;
        let region = state.curr_region();
        if let TokenKind::BlockLabel(label) = &token.kind {
            let block = Block::reserve_with_name(ctx, label.clone(), region);
            let mut args = Vec::new();
            if state.stream.consume_if(TokenKind::Char('('))?.is_some() {
                loop {
                    if state.stream.consume_if(TokenKind::Char(')'))?.is_some() {
                        break;
                    }
                    let arg = Value::parse(ctx, state)?;
                    args.push(arg);

                    match state.stream.consume()?.kind {
                        TokenKind::Char(')') => break,
                        TokenKind::Char(',') => continue,
                        _ => {
                            return parse_error!(
                                token.span,
                                ParseErrorKind::InvalidToken(
                                    vec![delimiter!(')'), delimiter!(',')].into(),
                                    token.kind
                                )
                            )
                            .into();
                        }
                    }
                }
            }
            Ok(Successor::new(block, args))
        } else {
            parse_error!(token.span, ExpectBlockLabelInSuccessor(token.kind)).into()
        }
    }
}

impl Print for Successor {
    fn print(&self, ctx: &Context, state: &mut PrintState) -> PrintResult<()> {
        let block_name = self.block.deref(&ctx.blocks).name(ctx);
        write!(state.buffer, "^{}", block_name)?;
        if !self.args.is_empty() {
            write!(state.buffer, "(")?;
            for (i, arg) in self.args.iter().enumerate() {
                arg.deref(&ctx.values).print(ctx, state)?;
                if i != self.args.len() - 1 {
                    write!(state.buffer, ", ")?;
                }
            }
            write!(state.buffer, ")")?;
        }
        Ok(())
    }
}

pub struct OpMetadata {
    /// The self ptr.
    self_ptr: ArenaPtr<OpObj>,
    /// The parent block of the operation.
    parent_block: Option<ArenaPtr<Block>>,
}

impl OpMetadata {
    pub fn new(self_ptr: ArenaPtr<OpObj>) -> Self {
        Self {
            self_ptr,
            parent_block: None,
        }
    }
}

/// The trait of all operations.
pub trait Op: Downcast + Print + Verify + DataFlow + ControlFlow + RegionInterface {
    /// Get the mnemonic of the type.
    fn mnemonic(&self) -> Mnemonic;

    /// Get the mnemonic of the type statically.
    fn mnemonic_static() -> Mnemonic
    where
        Self: Sized;

    /// Register the operation to the context.
    ///
    /// The [`Parse`](crate::core::parse::Parse) trait is not object-safe, so
    /// here just pass the parse function.
    fn register(ctx: &mut Context, parse_fn: OpParseFn)
    where
        Self: Sized;

    fn metadata(&self) -> &OpMetadata;

    fn metadata_mut(&mut self) -> &mut OpMetadata;

    /// Get the self ptr.
    fn self_ptr(&self) -> ArenaPtr<OpObj> { self.metadata().self_ptr }

    /// Get the parent block of the operation.
    fn parent_block(&self) -> Option<ArenaPtr<Block>> { self.metadata().parent_block }

    /// Set the parent block of the operation.
    fn set_parent_block(
        &mut self,
        parent_block: Option<ArenaPtr<Block>>,
    ) -> Option<ArenaPtr<Block>> {
        let old = self.metadata_mut().parent_block.take();
        self.metadata_mut().parent_block = parent_block;
        old
    }

    /// Get the parent region of the operation.
    ///
    /// If the operation has no parent block, the parent region will be `None`.
    fn parent_region(&self, ctx: &Context) -> Option<ArenaPtr<Region>> {
        self.parent_block().map(|ptr| {
            let block = ptr.deref(&ctx.blocks);
            block.parent_region()
        })
    }
}

impl_downcast!(Op);

pub struct OpObj(Box<dyn Op>);

impl<T> From<T> for OpObj
where
    T: Op,
{
    fn from(t: T) -> Self { OpObj(Box::new(t)) }
}

impl AsRef<dyn Op> for OpObj {
    fn as_ref(&self) -> &dyn Op { &*self.0 }
}

impl AsMut<dyn Op> for OpObj {
    fn as_mut(&mut self) -> &mut dyn Op { &mut *self.0 }
}

impl OpObj {
    /// Check if the type object is a concrete type.
    pub fn is_a<T: Op>(&self) -> bool { self.as_ref().is::<T>() }

    /// Try to downcast the type object to a concrete type.
    pub fn as_a<T: Op>(&self) -> Option<&T> { self.as_ref().downcast_ref() }

    /// Check if the type object implements a trait.
    pub fn impls<T: ?Sized + 'static>(&self, ctx: &Context) -> bool {
        self.as_ref().impls::<T>(&ctx.casters)
    }

    /// Try to cast the type object to another trait.
    pub fn cast_ref<T: ?Sized + 'static>(&self, ctx: &Context) -> Option<&T> {
        self.as_ref().cast_ref(&ctx.casters)
    }

    pub fn cast_mut<T: ?Sized + 'static>(&mut self, ctx: &Context) -> Option<&mut T> {
        self.as_mut().cast_mut(&ctx.casters)
    }
}

impl Parse for OpObj {
    type Item = ArenaPtr<OpObj>;

    /// The top-level parsing for an operation.
    ///
    /// This function will parse the operation result, push the names to the
    /// state, then parse the mnemonic and the dialect-specific text.
    ///
    /// e.g. for the oepration text below:
    ///
    /// ```text
    /// %0, %1 = dialect.agnostic_op %2, %3 : (int<32>, int<32>)
    /// ```
    ///
    /// The result part `%0, %1` will be saved as names, then the `=` will be
    /// consumed and the mnemonic will be parsed. According to the mnemonic,
    /// the parse function will be looked up from the context and called.
    ///
    /// The dialect-specific parse function should only parse the rest of the
    /// text.
    ///
    /// # Syntax
    ///
    /// ```text
    /// <result_name_list> `=` <mnemonic> <dialect_specific_text>
    /// ````
    ///
    /// # Notes
    ///
    /// The components of an operation needs to accept the corresponding
    /// `ArenaPtr<OpObj>`, and thus it is necessary to call
    /// `ctx.ops.reserve()` to get the `ArenaPtr<OpObj>` and then
    /// enter the parsing process. Of course, after the operation is
    /// constructed, the slot should be filled.
    fn parse(ctx: &mut Context, state: &mut ParseState) -> ParseResult<Self::Item> {
        let mut result_names = Vec::new();
        let parent = state.curr_block();

        loop {
            let token = state.stream.peek()?;
            match token.kind {
                TokenKind::ValueName(_) => {
                    // eat the value name
                    let token = state.stream.consume()?;

                    if let TokenKind::ValueName(_) = token.kind {
                        result_names.push(token);
                    } else {
                        unreachable!();
                    }

                    // eat the next token, `=` or `,`
                    let token = state.stream.consume()?;
                    match token.kind {
                        TokenKind::Char(',') => continue,
                        TokenKind::Char('=') => break,
                        _ => {
                            return parse_error!(
                                token.span,
                                ParseErrorKind::InvalidToken(
                                    vec![delimiter!(','), delimiter!('=')].into(),
                                    token.kind
                                )
                            )
                            .into();
                        }
                    }
                }
                _ => break,
            }
        }

        let mnemonic = Mnemonic::parse(ctx, state)?;

        let parse_fn = ctx
            .dialects
            .get(mnemonic.primary())
            .unwrap_or_else(|| panic!("dialect {} not found: ", mnemonic.primary().as_str()))
            .get_op_parse_fn(&mnemonic)
            .unwrap_or_else(|| {
                panic!(
                    "op {}.{} not found",
                    mnemonic.primary().as_str(),
                    mnemonic.secondary().as_str()
                )
            });

        state.push_result_names(result_names);
        let op = parse_fn(ctx, state)?;

        if op.deref(&ctx.ops).as_ref().parent_block().is_none() {
            op.deref_mut(&mut ctx.ops).as_mut().set_parent_block(parent);
        }

        Ok(op)
    }
}

/// The parse function type of the operations.
///
/// The parse function should take the result builders and the parent block as
/// arguments and return the operation object.
pub type OpParseFn = ParseFn<ArenaPtr<OpObj>>;

impl Print for OpObj {
    /// Print the operation.
    ///
    /// This is actually symmetric to the parsing process.
    fn print(&self, ctx: &Context, state: &mut PrintState) -> PrintResult<()> {
        let num_results = self.as_ref().num_results();

        if num_results > 0 {
            for i in 0..num_results {
                if let Some(result) = self.as_ref().get_result(i) {
                    result.deref(&ctx.values).print(ctx, state)?;
                } else {
                    panic!("result {} not found", i);
                }
                if i != num_results - 1 {
                    write!(state.buffer, ", ")?;
                }
            }
            write!(state.buffer, " = ")?;
        }

        self.as_ref().mnemonic().print(ctx, state)?;
        write!(state.buffer, " ")?;
        self.as_ref().print(ctx, state)?;
        Ok(())
    }
}