From 2293284854f9aa139c3763e04ce7d5c78c1a0a27 Mon Sep 17 00:00:00 2001 From: urso Date: Sat, 20 Jan 2024 23:17:36 +0100 Subject: [PATCH] remove qualified trait --- derive/src/derive_attr.rs | 17 ++----------- derive/src/derive_shared.rs | 2 ++ derive/src/derive_type.rs | 13 ++-------- src/asmfmt/printers/attrtype.rs | 35 --------------------------- src/asmfmt/printers/mod.rs | 7 +++--- src/asmfmt/printers/tests.rs | 31 +++++++++++++----------- src/attribute.rs | 30 ++++++++++------------- src/common_traits.rs | 38 +----------------------------- src/dialects/builtin/attributes.rs | 22 ++++++----------- src/dialects/builtin/types.rs | 24 +++++++------------ src/dialects/llvm/types.rs | 15 +++++------- src/printable.rs | 19 --------------- src/type.rs | 36 ++++++++++++++++++++-------- 13 files changed, 89 insertions(+), 200 deletions(-) diff --git a/derive/src/derive_attr.rs b/derive/src/derive_attr.rs index 5a2ab56..f3c35ca 100644 --- a/derive/src/derive_attr.rs +++ b/derive/src/derive_attr.rs @@ -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 { @@ -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 for DefAttribute { @@ -119,16 +118,7 @@ impl From 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 } } } @@ -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 } } diff --git a/derive/src/derive_shared.rs b/derive/src/derive_shared.rs index 68441e6..b45e50c 100644 --- a/derive/src/derive_shared.rs +++ b/derive/src/derive_shared.rs @@ -29,6 +29,7 @@ impl ToTokens for VerifiersRegister { } } +/* pub struct ImplQualified { pub ident: syn::Ident, pub qualifier: syn::Path, @@ -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 { diff --git a/derive/src/derive_type.rs b/derive/src/derive_type.rs index 3b5df0e..6eed19b 100644 --- a/derive/src/derive_type.rs +++ b/derive/src/derive_type.rs @@ -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 { @@ -113,17 +113,11 @@ fn err_struct_attrib_required(span: Span, attr: &str) -> syn::Error { struct DefType { input: DefTypeInput, - qualified: ImplQualified, } impl From 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 } } } @@ -185,13 +179,10 @@ fn impl_type(def_type: &DefType) -> TokenStream { } }; - let impl_qualified = &def_type.qualified; quote! { #def_struct #impl_type - - #impl_qualified } } diff --git a/src/asmfmt/printers/attrtype.rs b/src/asmfmt/printers/attrtype.rs index cde54ab..113d024 100644 --- a/src/asmfmt/printers/attrtype.rs +++ b/src/asmfmt/printers/attrtype.rs @@ -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)*)) => { @@ -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(v: &T) -> impl Printable + '_ -where - T: 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)*)) => { diff --git a/src/asmfmt/printers/mod.rs b/src/asmfmt/printers/mod.rs index 8745efd..d02b60f 100644 --- a/src/asmfmt/printers/mod.rs +++ b/src/asmfmt/printers/mod.rs @@ -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); @@ -372,6 +376,3 @@ pub fn operation_generic_format(op: T) -> impl Printable { }, ) } - -#[cfg(test)] -mod tests; diff --git a/src/asmfmt/printers/tests.rs b/src/asmfmt/printers/tests.rs index 120289e..1a5a684 100644 --- a/src/asmfmt/printers/tests.rs +++ b/src/asmfmt/printers/tests.rs @@ -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, @@ -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, } +impl VecType { + fn get(ctx: &mut Context, elem: Ptr) -> Ptr { + Type::register_instance(Self { elem }, ctx) + } +} impl Verify for VecType { fn verify(&self, ctx: &Context) -> Result<()> { self.elem.verify(ctx)?; @@ -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] @@ -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] @@ -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 >"); + assert_eq!(got, "testing.vec testing.integer 32<4,true>"); } #[test] @@ -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] @@ -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] @@ -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, ""); } #[test] @@ -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>)>"# ); } diff --git a/src/attribute.rs b/src/attribute.rs index b92f6eb..8e5505f 100644 --- a/src/attribute.rs +++ b/src/attribute.rs @@ -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, @@ -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; @@ -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())?; + ::fmt(self.deref(), ctx, state, f) + } +} + /// Cast reference to an [Attribute] object to an interface reference. pub fn attr_cast(attr: &dyn Attribute) -> Option<&T> { attr.cast::() @@ -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() - } - } } } diff --git a/src/common_traits.rs b/src/common_traits.rs index 510c109..33e6e84 100644 --- a/src/common_traits.rs +++ b/src/common_traits.rs @@ -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 ID type as the -// qualifier. -pub trait Qualified { - type Qualifier; - - fn get_qualifier(&self, ctx: &Context) -> Self::Qualifier; -} - -impl Qualified for &T { - type Qualifier = T::Qualifier; - - fn get_qualifier(&self, ctx: &Context) -> Self::Qualifier { - (*self).get_qualifier(ctx) - } -} - -impl Qualified for Box { - type Qualifier = T::Qualifier; - - fn get_qualifier(&self, ctx: &Context) -> Self::Qualifier { - (**self).get_qualifier(ctx) - } -} - -impl Qualified for Ptr { - 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 { diff --git a/src/dialects/builtin/attributes.rs b/src/dialects/builtin/attributes.rs index 0f58aaf..ba8b804 100644 --- a/src/dialects/builtin/attributes.rs +++ b/src/dialects/builtin/attributes.rs @@ -8,7 +8,7 @@ use sorted_vector_map::SortedVectorMap; use thiserror::Error; use crate::{ - asmfmt::printers::{concat, qualifier, quoted}, + asmfmt::printers::{concat, quoted}, attribute::{AttrObj, Attribute}, common_traits::Verify, context::{Context, Ptr}, @@ -50,7 +50,7 @@ impl Printable for StringAttr { state: &printable::State, f: &mut core::fmt::Formatter<'_>, ) -> core::fmt::Result { - concat((qualifier(self), " ", quoted(&self.0))).fmt(ctx, state, f) + quoted(&self.0).fmt(ctx, state, f) } } @@ -119,15 +119,7 @@ impl Printable for IntegerAttr { _state: &printable::State, f: &mut core::fmt::Formatter<'_>, ) -> core::fmt::Result { - concat(( - qualifier(self), - " <", - format!("0x{:x}", self.val), - ": ", - &self.ty, - ">", - )) - .fmt(ctx, _state, f) + concat(("<", format!("0x{:x}", self.val), ": ", &self.ty, ">")).fmt(ctx, _state, f) } } @@ -393,11 +385,11 @@ impl UnitAttr { impl Printable for UnitAttr { fn fmt( &self, - ctx: &Context, - state: &printable::State, + _ctx: &Context, + _state: &printable::State, f: &mut core::fmt::Formatter<'_>, ) -> core::fmt::Result { - qualifier(self).fmt(ctx, state, f) + write!(f, "()") } } @@ -438,7 +430,7 @@ impl Printable for TypeAttr { state: &printable::State, f: &mut core::fmt::Formatter<'_>, ) -> core::fmt::Result { - concat((qualifier(self), " <", self.0, ">")).fmt(ctx, state, f) + concat(("<", self.0, ">")).fmt(ctx, state, f) } } diff --git a/src/dialects/builtin/types.rs b/src/dialects/builtin/types.rs index ecfdbd1..58b03cb 100644 --- a/src/dialects/builtin/types.rs +++ b/src/dialects/builtin/types.rs @@ -8,7 +8,7 @@ use combine::{ }; use crate::{ - asmfmt::printers::{concat, functional_type, qualifier, typed}, + asmfmt::printers::{functional_type, typed}, common_traits::Verify, context::{Context, Ptr}, dialect::Dialect, @@ -86,12 +86,11 @@ impl Parsable for IntegerType { impl Printable for IntegerType { fn fmt( &self, - ctx: &Context, - state: &printable::State, + _ctx: &Context, + _state: &printable::State, f: &mut core::fmt::Formatter<'_>, ) -> core::fmt::Result { - qualifier(self).fmt(ctx, state, f)?; - write!(f, " <",)?; + write!(f, "<",)?; match &self.signedness { Signedness::Signed => write!(f, "si{}", self.width)?, Signedness::Unsigned => write!(f, "ui{}", self.width)?, @@ -156,12 +155,7 @@ impl Printable for FunctionType { state: &printable::State, f: &mut core::fmt::Formatter<'_>, ) -> core::fmt::Result { - concat(( - qualifier(self), - " ", - functional_type(typed(&self.inputs), typed(&self.results)), - )) - .fmt(ctx, state, f) + functional_type(typed(&self.inputs), typed(&self.results)).fmt(ctx, state, f) } } @@ -220,11 +214,11 @@ impl UnitType { impl Printable for UnitType { fn fmt( &self, - ctx: &Context, - state: &printable::State, - f: &mut core::fmt::Formatter<'_>, + _ctx: &Context, + _state: &printable::State, + _f: &mut core::fmt::Formatter<'_>, ) -> core::fmt::Result { - qualifier(self).fmt(ctx, state, f) + Ok(()) } } diff --git a/src/dialects/llvm/types.rs b/src/dialects/llvm/types.rs index b0ce185..fb7dd40 100644 --- a/src/dialects/llvm/types.rs +++ b/src/dialects/llvm/types.rs @@ -1,5 +1,5 @@ use crate::{ - asmfmt::printers::{concat, enclosed, list_with_sep, qualifier}, + asmfmt::printers::{concat, enclosed, list_with_sep}, common_traits::Verify, context::{Context, Ptr}, dialect::Dialect, @@ -181,7 +181,9 @@ impl Printable for StructType { state: &printable::State, f: &mut core::fmt::Formatter<'_>, ) -> core::fmt::Result { - write!(f, "{} <", Self::get_type_id_static().disp(ctx))?; + // write!(f, "<", Self::get_type_id_static().disp(ctx))?; + write!(f, "<")?; + use std::cell::RefCell; // Ugly, but also the simplest way to avoid infinite recursion. // MLIR does the same: see LLVMTypeSyntax::printStructType. @@ -332,7 +334,7 @@ impl Printable for PointerType { state: &printable::State, f: &mut core::fmt::Formatter<'_>, ) -> core::fmt::Result { - concat((qualifier(self), " <", self.to, ">")).fmt(ctx, state, f) + concat(("<", self.to, ">")).fmt(ctx, state, f) } } @@ -413,12 +415,7 @@ mod tests { assert!(StructType::get_existing_named(&ctx, "LinkedList2").is_none()); assert_eq!( - list_struct - .deref(&ctx) - .downcast_ref::() - .unwrap() - .disp(&ctx) - .to_string(), + list_struct.disp(&ctx).to_string(), "llvm.struct , next: llvm.ptr > }>" ); diff --git a/src/printable.rs b/src/printable.rs index 96ba116..1fb7081 100644 --- a/src/printable.rs +++ b/src/printable.rs @@ -4,7 +4,6 @@ use std::{ cell::RefCell, fmt::{self, Display}, rc::Rc, - sync::Arc, }; use crate::{common_traits::RcSharable, context::Context}; @@ -170,24 +169,6 @@ impl<'a, T: Printable + ?Sized> Printable for &'a T { } } -impl Printable for Rc { - fn fmt(&self, ctx: &Context, state: &State, f: &mut fmt::Formatter<'_>) -> fmt::Result { - (**self).fmt(ctx, state, f) - } -} - -impl Printable for Arc { - fn fmt(&self, ctx: &Context, state: &State, f: &mut fmt::Formatter<'_>) -> fmt::Result { - (**self).fmt(ctx, state, f) - } -} - -impl Printable for Box { - fn fmt(&self, ctx: &Context, state: &State, f: &mut fmt::Formatter<'_>) -> fmt::Result { - self.as_ref().fmt(ctx, state, f) - } -} - #[derive(Clone, Copy)] /// When printing lists, how must they be separated pub enum ListSeparator { diff --git a/src/type.rs b/src/type.rs index 8c85e79..8b21ba7 100644 --- a/src/type.rs +++ b/src/type.rs @@ -8,7 +8,7 @@ //! //! The [impl_type](crate::impl_type) macro can be used to implement [Type] for a rust type. -use crate::common_traits::{Qualified, Verify}; +use crate::common_traits::Verify; use crate::context::{private::ArenaObj, ArenaCell, Context, Ptr}; use crate::dialect::{Dialect, DialectName}; use crate::error::Result; @@ -22,7 +22,7 @@ use crate::storage_uniquer::TypeValueHash; use combine::error::StdParseResult2; use combine::{parser, Parser, StreamOnce}; use downcast_rs::{impl_downcast, Downcast}; -use std::fmt::Debug; +use std::fmt::{Debug, Display}; use std::hash::{Hash, Hasher}; use std::marker::PhantomData; use std::ops::Deref; @@ -188,6 +188,12 @@ impl Printable for TypeName { } } +impl Display for TypeName { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", *self.0) + } +} + impl Parsable for TypeName { type Arg = (); type Parsed = TypeName; @@ -244,6 +250,12 @@ impl Printable for TypeId { } } +impl Display for TypeId { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}.{}", self.dialect, self.name) + } +} + /// Since we can't store the [Type] trait in the arena, /// we store boxed dyn objects of it instead. pub type TypeObj = Box; @@ -254,6 +266,18 @@ impl PartialEq for TypeObj { } } +impl Printable for TypeObj { + fn fmt( + &self, + ctx: &Context, + state: &printable::State, + f: &mut std::fmt::Formatter<'_>, + ) -> std::fmt::Result { + write!(f, "{} ", self.get_type_id())?; + ::fmt(self.deref(), ctx, state, f) + } +} + impl Eq for TypeObj {} impl Hash for TypeObj { @@ -349,14 +373,6 @@ macro_rules! impl_type { } } } - - impl $crate::common_traits::Qualified for $structname { - type Qualifier = $crate::r#type::TypeId; - - fn get_qualifier(&self, _ctx: &$crate::context::Context) -> Self::Qualifier { - Self::get_type_id_static() - } - } } }