Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
raviqqe committed Feb 17, 2024
1 parent b538d3e commit 9d4d882
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 43 deletions.
8 changes: 4 additions & 4 deletions macro/src/dialect/generation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,21 @@ pub fn generate_operation(operation: &Operation) -> Result<TokenStream, Error> {
.results()
.enumerate()
.map(|(index, result)| generate_result_accessor(result, index, operation.result_len()))
.collect::<Result<Vec<_>, _>>()?;
.collect::<Vec<_>>();
let operand_accessors = operation
.operands()
.enumerate()
.map(|(index, operand)| generate_operand_accessor(operand, index, operation.operand_len()))
.collect::<Result<Vec<_>, _>>()?;
.collect::<Vec<_>>();
let region_accessors = operation
.regions()
.enumerate()
.map(|(index, region)| generate_region_accessor(index, region))
.map(|(index, region)| generate_region_accessor(region, index))
.collect::<Vec<_>>();
let successor_accessors = operation
.successors()
.enumerate()
.map(|(index, region)| generate_successor_accessor(index, region))
.map(|(index, region)| generate_successor_accessor(region, index))
.collect::<Vec<_>>();
let attribute_accessors = operation
.attributes()
Expand Down
11 changes: 10 additions & 1 deletion macro/src/dialect/generation/element_accessor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub fn generate_element_getter(
let count = Ident::new(&format!("{singular_kind}_count"), Span::call_site());
let name = field.name();

match field.variadic_kind() {
let body = match field.variadic_kind() {
VariadicKind::Simple { unfixed_seen } => {
if field.is_optional() {
// Optional element, and some singular elements.
Expand Down Expand Up @@ -110,5 +110,14 @@ pub fn generate_element_getter(
#get_elements
}
}
};

let identifier = field.singular_identifier();
let return_type = field.return_type();

quote! {
pub fn #identifier(&self) -> #return_type {
#body
}
}
}
24 changes: 4 additions & 20 deletions macro/src/dialect/generation/operand_accessor.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,14 @@
use super::element_accessor::generate_element_getter;
use crate::dialect::{
error::Error,
operation::{Operand, OperationField},
};
use crate::dialect::operation::Operand;
use proc_macro2::{Ident, Span, TokenStream};
use quote::quote;

pub fn generate_operand_accessor(
field: &Operand,
index: usize,
length: usize,
) -> Result<TokenStream, Error> {
let ident = field.singular_identifier();
let return_type = field.return_type();
let body = generate_element_getter(
pub fn generate_operand_accessor(field: &Operand, index: usize, length: usize) -> TokenStream {
generate_element_getter(
field,
"operand",
"operands",
&Ident::new("OperandNotFound", Span::call_site()),
index,
length,
);

Ok(quote! {
pub fn #ident(&self) -> #return_type {
#body
}
})
)
}
2 changes: 1 addition & 1 deletion macro/src/dialect/generation/region_accessor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::dialect::operation::{OperationField, Region};
use proc_macro2::TokenStream;
use quote::quote;

pub fn generate_region_accessor(index: usize, region: &Region) -> TokenStream {
pub fn generate_region_accessor(region: &Region, index: usize) -> TokenStream {
let identifier = &region.singular_identifier();
let return_type = &region.return_type();
let body = if region.is_variadic() {
Expand Down
20 changes: 4 additions & 16 deletions macro/src/dialect/generation/result_accessor.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,18 @@
use super::element_accessor::generate_element_getter;
use crate::dialect::{
error::Error,
operation::{OperationField, OperationResult},
};
use crate::dialect::operation::OperationResult;
use proc_macro2::{Ident, Span, TokenStream};
use quote::quote;

pub fn generate_result_accessor(
result: &OperationResult,
index: usize,
length: usize,
) -> Result<TokenStream, Error> {
let identifier = result.singular_identifier();
let return_type = result.return_type();
let body = generate_element_getter(
) -> TokenStream {
generate_element_getter(
result,
"result",
"results",
&Ident::new("ResultNotFound", Span::call_site()),
index,
length,
);

Ok(quote! {
pub fn #identifier(&self) -> #return_type {
#body
}
})
)
}
2 changes: 1 addition & 1 deletion macro/src/dialect/generation/successor_accessor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::dialect::operation::{OperationField, Successor};
use proc_macro2::TokenStream;
use quote::quote;

pub fn generate_successor_accessor(index: usize, successor: &Successor) -> TokenStream {
pub fn generate_successor_accessor(successor: &Successor, index: usize) -> TokenStream {
let identifier = successor.singular_identifier();
let return_type = successor.return_type();
let body = if successor.is_variadic() {
Expand Down

0 comments on commit 9d4d882

Please sign in to comment.