Skip to content

Commit

Permalink
remove qualified trait
Browse files Browse the repository at this point in the history
  • Loading branch information
urso committed Jan 20, 2024
1 parent a543e1e commit 2293284
Show file tree
Hide file tree
Showing 13 changed files with 89 additions and 200 deletions.
17 changes: 2 additions & 15 deletions derive/src/derive_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use syn::{

use crate::{
attr::{require_once, Attribute, AttributeName, DialectName, IRKind},
derive_shared::{build_struct_body, ImplQualified, VerifiersRegister},
derive_shared::{build_struct_body, VerifiersRegister},
};

struct DefAttributeInput {
Expand Down Expand Up @@ -109,7 +109,6 @@ fn err_struct_attrib_required(span: Span, attr: &str) -> syn::Error {
struct DefAttribute {
input: DefAttributeInput,
verifiers: VerifiersRegister,
qualified: ImplQualified,
}

impl From<DefAttributeInput> for DefAttribute {
Expand All @@ -119,16 +118,7 @@ impl From<DefAttributeInput> for DefAttribute {
verifiers_name: format_ident!("AttrInterfaceVerifier_{}", &input.ident),
ifc_name: syn::parse_quote! { ::pliron::attribute::AttrInterfaceVerifier },
};
let qualified = ImplQualified {
ident: input.ident.clone(),
qualifier: syn::parse_quote! { ::pliron::attribute::AttrId },
getter: quote! { self.get_attr_id() },
};
Self {
input,
verifiers,
qualified,
}
Self { input, verifiers }
}
}

Expand Down Expand Up @@ -196,15 +186,12 @@ fn impl_attribute(def_attrib: &DefAttribute) -> TokenStream {
}
};

let impl_qualified_trait = &def_attrib.qualified;
quote! {
#def_struct

#verifiers_register

#impl_attribute_trait

#impl_qualified_trait
}
}

Expand Down
2 changes: 2 additions & 0 deletions derive/src/derive_shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ impl ToTokens for VerifiersRegister {
}
}

/*
pub struct ImplQualified {
pub ident: syn::Ident,
pub qualifier: syn::Path,
Expand All @@ -52,6 +53,7 @@ impl ToTokens for ImplQualified {
.to_tokens(tokens);
}
}
*/

pub(crate) fn build_struct_body(ds: &DataStruct) -> proc_macro2::TokenStream {
match ds.fields {
Expand Down
13 changes: 2 additions & 11 deletions derive/src/derive_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use syn::{

use crate::{
attr::{require_once, Attribute, DialectName, IRKind, TypeName},
derive_shared::{build_struct_body, ImplQualified},
derive_shared::build_struct_body,
};

struct DefTypeInput {
Expand Down Expand Up @@ -113,17 +113,11 @@ fn err_struct_attrib_required(span: Span, attr: &str) -> syn::Error {

struct DefType {
input: DefTypeInput,
qualified: ImplQualified,
}

impl From<DefTypeInput> for DefType {
fn from(input: DefTypeInput) -> Self {
let qualified = ImplQualified {
ident: input.ident.clone(),
qualifier: syn::parse_quote! { ::pliron::r#type::TypeId },
getter: quote! { self.get_type_id() },
};
Self { input, qualified }
Self { input }
}
}

Expand Down Expand Up @@ -185,13 +179,10 @@ fn impl_type(def_type: &DefType) -> TokenStream {
}
};

let impl_qualified = &def_type.qualified;
quote! {
#def_struct

#impl_type

#impl_qualified
}
}

Expand Down
35 changes: 0 additions & 35 deletions src/asmfmt/printers/attrtype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,6 @@
//! ====================================
//!
use std::fmt;

use crate::{
common_traits::Qualified,
context::Context,
printable::{Printable, State},
};

use super::PrinterFn;

#[macro_export]
macro_rules! at_params_directive {
($self:ident, ($($printer:ident),*), (), ($($params:ident)*)) => {
Expand All @@ -30,31 +20,6 @@ macro_rules! at_params_directive {
#[allow(unused_imports)]
pub(crate) use at_params_directive;

#[macro_export]
macro_rules! at_qualifier_directive {
($self:ident, ($($printer:ident),*), (), ($($_param:ident)*)) => {
qualifier($self).fmt($($printer),*)?;
};
($self:ident, ($($printer:ident),*), ($field_name:tt), ($($_param:ident)*)) => {
qualifier(&$self.$field_name).fmt($($printer),*)?;
};
}
#[allow(unused_imports)]
pub(crate) use at_qualifier_directive;

// Prints the qualifier o a qualified object like a type or an attribute.
pub fn qualifier<T>(v: &T) -> impl Printable + '_
where
T: Qualified,
<T as Qualified>::Qualifier: Printable,
{
PrinterFn(
move |ctx: &Context, state: &State, f: &mut fmt::Formatter<'_>| {
v.get_qualifier(ctx).fmt(ctx, state, f)
},
)
}

#[macro_export]
macro_rules! at_qualified_directive {
($self:ident, ($($_printer:ident),*), (), ($($_param:tt)*)) => {
Expand Down
7 changes: 4 additions & 3 deletions src/asmfmt/printers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ use crate::{
};

mod attrtype;
#[allow(unused_imports)]
pub use attrtype::*;

mod op;
pub use op::*;

#[cfg(test)]
mod tests;

/// Wrap a function to implement the Printable trait
struct PrinterFn<F>(F);

Expand Down Expand Up @@ -372,6 +376,3 @@ pub fn operation_generic_format<T: Op>(op: T) -> impl Printable {
},
)
}

#[cfg(test)]
mod tests;
31 changes: 17 additions & 14 deletions src/asmfmt/printers/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl Verify for SimpleType {
//#[asm_format = "`int <` params `>`"]
//#[asm_format = "`int <` struct(params) `>`"]
//#[asm_format = "`int <` struct($width, $sign) `>`"]
#[asm_format = "`int` $width `<` $align `,` $sign `>`"]
#[asm_format = "$width `<` $align `,` $sign `>`"]
pub struct IntegerType {
width: u32,
sign: bool,
Expand All @@ -95,10 +95,16 @@ impl Verify for IntegerType {
#[type_name = "testing.vec"]
#[derive(Hash, PartialEq, Eq, Debug, Printable, NotParsableType)]
// #[asm_format = "`vec [` qualified($elem) `]` "]
#[asm_format = "`vec ` qualifier($elem) ` <` $elem `>` "]
// #[asm_format = "`vec ` qualifier($elem) ` <` $elem `>` "]
#[asm_format = "$elem"]
pub struct VecType {
elem: Ptr<TypeObj>,
}
impl VecType {
fn get(ctx: &mut Context, elem: Ptr<TypeObj>) -> Ptr<TypeObj> {
Type::register_instance(Self { elem }, ctx)
}
}
impl Verify for VecType {
fn verify(&self, ctx: &Context) -> Result<()> {
self.elem.verify(ctx)?;
Expand Down Expand Up @@ -271,7 +277,7 @@ fn print_simple_type() {
register_dialect(&mut ctx);

let got = SimpleType::get(&mut ctx).disp(&ctx).to_string();
assert_eq!(got, "()");
assert_eq!(got, "testing.simple_type ()");
}

#[test]
Expand All @@ -281,7 +287,7 @@ fn print_integer_type() {

let ty = IntegerType::get(&mut ctx, 32, true, 8);
let got = ty.disp(&ctx).to_string();
assert_eq!(got, "int32<8,true>");
assert_eq!(got, "testing.integer 32<8,true>");
}

#[test]
Expand Down Expand Up @@ -318,9 +324,9 @@ fn print_vec_type() {
register_dialect(&mut ctx);

let i32_ty = IntegerType::get(&mut ctx, 32, true, 4);
let vec_ty = VecType { elem: i32_ty };
let vec_ty = VecType::get(&mut ctx, i32_ty);
let got = vec_ty.disp(&ctx).to_string();
assert_eq!(got, "vec testing.integer <int32<4,true>>");
assert_eq!(got, "testing.vec testing.integer 32<4,true>");
}

#[test]
Expand All @@ -333,7 +339,7 @@ fn print_function() {
let func_ty = FunctionType::get(&mut ctx, vec![i32_ty, i32_ty], vec![u64_ty]);

let got = func_ty.disp(&ctx).to_string();
assert_eq!(got, "(int32<4,true>,int32<4,true>) -> (int32<8,false>)");
assert_eq!(got, "testing.function (testing.integer 32<4,true>,testing.integer 32<4,true>) -> (testing.integer 32<8,false>)");
}

#[test]
Expand Down Expand Up @@ -362,12 +368,9 @@ fn print_int_attr() {
register_dialect(&mut ctx);

let ty = IntegerType::get(&mut ctx, 32, true, 8);
let attr = IntegerAttr {
ty,
val: ApInt::from(42),
};
let attr = IntegerAttr::create(ty, ApInt::from(42));
let got = attr.disp(&ctx).to_string();
assert_eq!(got, "0x2a: int32<8,true>");
assert_eq!(got, "testing.int 0x2a: testing.integer 32<8,true>");
}

#[test]
Expand All @@ -377,7 +380,7 @@ fn print_vec_attr() {

let vec_attr = VecAttr(vec![UnitAttr::create(), UnitAttr::create()]);
let got = vec_attr.disp(&ctx).to_string();
assert_eq!(got, "<(),()>");
assert_eq!(got, "<testing.unit (),testing.unit ()>");
}

#[test]
Expand All @@ -393,6 +396,6 @@ fn print_const_op() {
let got = const_op.disp(&ctx).to_string();
assert_eq!(
got,
r#"op_0_0_res0 = "testing.const"() {"constant.value" = 0x2a: int32<4,true>} : <() -> (int32<4,true>)>"#
r#"op_0_0_res0 = "testing.const"() {"constant.value" = testing.int 0x2a: testing.integer 32<4,true>} : <() -> (testing.integer 32<4,true>)>"#
);
}
30 changes: 13 additions & 17 deletions src/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use intertrait::{cast::CastRef, CastFrom};
use rustc_hash::FxHashMap;

use crate::{
common_traits::{Qualified, Verify},
common_traits::Verify,
context::Context,
dialect::{Dialect, DialectName},
error::Result,
Expand Down Expand Up @@ -83,14 +83,6 @@ pub trait Attribute: Printable + Verify + Downcast + CastFrom + Sync + DynClone
impl_downcast!(Attribute);
dyn_clone::clone_trait_object!(Attribute);

impl Qualified for dyn Attribute {
type Qualifier = AttrId;

fn get_qualifier(&self, _ctx: &Context) -> Self::Qualifier {
self.get_attr_id()
}
}

/// [Attribute] objects are boxed and stored in the IR.
pub type AttrObj = Box<dyn Attribute>;

Expand All @@ -102,6 +94,18 @@ impl PartialEq for AttrObj {

impl Eq for AttrObj {}

impl Printable for AttrObj {
fn fmt(
&self,
ctx: &Context,
state: &printable::State,
f: &mut core::fmt::Formatter<'_>,
) -> core::fmt::Result {
write!(f, "{} ", self.get_attr_id())?;
<dyn Attribute as Printable>::fmt(self.deref(), ctx, state, f)
}
}

/// Cast reference to an [Attribute] object to an interface reference.
pub fn attr_cast<T: ?Sized + Attribute>(attr: &dyn Attribute) -> Option<&T> {
attr.cast::<T>()
Expand Down Expand Up @@ -327,14 +331,6 @@ macro_rules! impl_attr {
Ok(())
}
}

impl $crate::common_traits::Qualified for $structname {
type Qualifier = $crate::attribute::AttrId;

fn get_qualifier(&self, _ctx: &Context) -> Self::Qualifier {
self.get_attr_id()
}
}
}
}

Expand Down
38 changes: 1 addition & 37 deletions src/common_traits.rs
Original file line number Diff line number Diff line change
@@ -1,42 +1,6 @@
//! Utility traits such as [Named], [Verify] etc.
use crate::{
context::{private::ArenaObj, Context, Ptr},
error::Result,
};

// Objects that have a qualified name within a dialect.
// All Attribute and Type instances are qualified objects with their <kind>ID type as the
// qualifier.
pub trait Qualified {
type Qualifier;

fn get_qualifier(&self, ctx: &Context) -> Self::Qualifier;
}

impl<T: Qualified> Qualified for &T {
type Qualifier = T::Qualifier;

fn get_qualifier(&self, ctx: &Context) -> Self::Qualifier {
(*self).get_qualifier(ctx)
}
}

impl<T: Qualified + ?Sized> Qualified for Box<T> {
type Qualifier = T::Qualifier;

fn get_qualifier(&self, ctx: &Context) -> Self::Qualifier {
(**self).get_qualifier(ctx)
}
}

impl<T: Qualified + ArenaObj> Qualified for Ptr<T> {
type Qualifier = T::Qualifier;

fn get_qualifier(&self, ctx: &Context) -> Self::Qualifier {
self.deref(ctx).get_qualifier(ctx)
}
}
use crate::{context::Context, error::Result};

/// Check and ensure correctness.
pub trait Verify {
Expand Down
Loading

0 comments on commit 2293284

Please sign in to comment.