Skip to main content

melior_macro/dialect/operation/
builder.rs

1mod type_state;
2
3use self::type_state::TypeState;
4use super::{Operation, OperationField};
5use quote::format_ident;
6use syn::Ident;
7
8pub struct OperationBuilder<'a> {
9    operation: &'a Operation<'a>,
10    identifier: Ident,
11    type_state: TypeState,
12}
13
14impl<'a> OperationBuilder<'a> {
15    pub fn new(operation: &'a Operation<'a>) -> Self {
16        Self {
17            operation,
18            identifier: format_ident!("{}Builder", operation.name()),
19            type_state: Self::create_type_state(operation),
20        }
21    }
22
23    pub const fn operation(&self) -> &Operation {
24        self.operation
25    }
26
27    pub const fn identifier(&self) -> &Ident {
28        &self.identifier
29    }
30
31    pub const fn type_state(&self) -> &TypeState {
32        &self.type_state
33    }
34
35    fn create_type_state(operation: &Operation) -> TypeState {
36        TypeState::new(
37            Self::build_names(operation.required_results()),
38            Self::build_names(operation.required_operands()),
39            Self::build_names(operation.required_regions()),
40            Self::build_names(operation.required_successors()),
41            Self::build_names(operation.required_attributes()),
42        )
43    }
44
45    fn build_names<'b>(
46        fields: impl Iterator<Item = &'b (impl OperationField + 'b)>,
47    ) -> Vec<String> {
48        fields.map(|field| field.name().into()).collect()
49    }
50}