From a94c657743c29c3a7d392f9421db4d9283f83f04 Mon Sep 17 00:00:00 2001 From: caina Date: Sat, 23 May 2026 22:47:38 -0300 Subject: [PATCH 1/4] refactor: refactored hir to have less query functions and rewrote it to use them --- crates/hir/src/components.rs | 2 +- crates/hir/src/declarations.rs | 52 ++++++++++++-------- crates/hir/src/helpers/names.rs | 31 +----------- crates/hir/src/helpers/types.rs | 9 ---- crates/hir/src/lib.rs | 1 + crates/hir/src/model/types.rs | 12 ++--- crates/hir/src/names.rs | 64 +++++------------------- crates/hir/src/queries.rs | 86 +++++++++++++++++++++++++++++++++ 8 files changed, 139 insertions(+), 118 deletions(-) create mode 100644 crates/hir/src/queries.rs diff --git a/crates/hir/src/components.rs b/crates/hir/src/components.rs index 20e90c42..39b55f26 100644 --- a/crates/hir/src/components.rs +++ b/crates/hir/src/components.rs @@ -27,7 +27,7 @@ impl SlynxHir { let HirType::Component { props } = self.get_type(&ty) else { unreachable!("The type should be a component instead"); }; - match props.iter().position(|prop| prop.name() == prop_name) { + match props.iter().position(|prop| prop.name() == interned_name) { None => Err(HIRError::name_unrecognized(interned_name, *span)), Some(index) if matches!( diff --git a/crates/hir/src/declarations.rs b/crates/hir/src/declarations.rs index 91093dae..243ea954 100644 --- a/crates/hir/src/declarations.rs +++ b/crates/hir/src/declarations.rs @@ -94,15 +94,6 @@ impl SlynxHir { }) } - ///Retrieves the type of something by knowing the provided `ref_ty` is a reference to it - pub fn get_type_from_ref(&self, ref_ty: crate::TypeId) -> &HirType { - if let HirType::Reference { rf, .. } = self.get_type(&ref_ty) { - self.get_type(rf) - } else { - unreachable!("The provided ref_ty should be of type Reference"); - } - } - /// Resolves an object declaration, filling in its field types and pushing the declaration. pub fn resolve_object( &mut self, @@ -227,13 +218,32 @@ impl SlynxHir { .iter() .filter_map(|member| match &member.kind { ComponentMemberKind::Property { - name, modifier, ty, .. + name, + modifier, + ty: Some(generic), + .. } => { - let ty = match ty { - Some(generic) => self.get_typeid_of_name(&generic.identifier, &member.span), - _ => Ok(self.infer_type()), + let name = self.intern_name(name); + let ty_name = self.intern_name(&generic.identifier); + let ty = match self.get_type_of_name(ty_name, &member.span) { + Ok(v) => v, + Err(e) => return Some(Err(e)), }; - Some(ty.map(|ty| ComponentProperty::new(*modifier, name.clone(), ty))) + + Some(Ok(ComponentProperty::new(*modifier, name, ty))) + } + ComponentMemberKind::Property { + name, + modifier, + ty: None, + .. + } => { + let name = self.intern_name(&name); + Some(Ok(ComponentProperty::new( + *modifier, + name, + self.infer_type(), + ))) } ComponentMemberKind::Child(_) => None, }) @@ -310,25 +320,25 @@ impl SlynxHir { } ///Resolves an alias type, mapping the given `name` to the given `target` - pub fn resolve_alias( + pub(crate) fn resolve_alias( &mut self, name: &GenericIdentifier, target: &GenericIdentifier, span: Span, ) -> Result<()> { - let target_ty = self.get_typeid_of_name(&target.identifier, &target.span)?; + let intern_name = self.intern_name(&target.identifier); + let target_ty = self.get_type_of_name(intern_name, &target.span)?; - let alias_name = self.modules.intern_name(&name.identifier); let Some(alias_ty) = self .modules .types_module - .get_type_from_name_mut(&alias_name) + .get_type_from_name_mut(&intern_name) else { - return Err(HIRError::name_unrecognized(alias_name, name.span)); + return Err(HIRError::name_unrecognized(intern_name, name.span)); }; *alias_ty = HirType::new_ref(target_ty); - let Some((decl, ty)) = self.modules.get_declaration_by_name(&alias_name) else { - return Err(HIRError::name_unrecognized(alias_name, name.span)); + let Some((decl, ty)) = self.modules.get_declaration_by_name(&intern_name) else { + return Err(HIRError::name_unrecognized(intern_name, name.span)); }; self.declarations .push(HirDeclaration::new_alias(decl, ty, span)); diff --git a/crates/hir/src/helpers/names.rs b/crates/hir/src/helpers/names.rs index 44431a4b..67a91254 100644 --- a/crates/hir/src/helpers/names.rs +++ b/crates/hir/src/helpers/names.rs @@ -1,5 +1,4 @@ use common::{Span, SymbolPointer}; -use slynx_parser::GenericIdentifier; use crate::{HIRError, HirType, Result, SlynxHir, VariableId}; @@ -8,35 +7,7 @@ impl SlynxHir { pub fn get_name_from_pointer(&self, ptr: SymbolPointer) -> &str { self.modules.symbols_resolver.get_name(ptr) } - ///Retrieves the type of the provided `name` but in the global scope. The difference of a 'named' to a 'name' is that this function - ///tries to the the provided `name` as some identifier to something, and the name version does so after checking if the provided name itself - ///is a type - pub fn get_type_from_name(&mut self, name: &str, span: &Span) -> Result<&HirType> { - let name_id = self.modules.intern_name(name); - match self.get_type_from_pointer(&name_id) { - Some(ty) => Ok(ty), - _ => Err(HIRError::name_unrecognized(name_id, *span)), - } - } - /// Retrieves a mutable reference to the [`HirType`] registered under the given name. - /// - /// Returns an error if no type with that name exists. - pub fn get_reference_to_type(&mut self, name: &str, span: &Span) -> Result<&mut HirType> { - let name_symbol = self.modules.intern_name(name); - match self.get_type_mut_from_name(&name_symbol) { - Some(ty) => Ok(ty), - _ => Err(HIRError::name_unrecognized(name_symbol, *span)), - } - } - ///Retrieves the type of the provided `name` but in the global scope - pub fn get_type_of_name(&mut self, name: &GenericIdentifier, span: &Span) -> Result { - let name_id = self.modules.intern_name(&name.identifier); - match HirType::new(&name.identifier) { - Some(value) => Ok(value), - _ if let Some(ty) = self.get_typeid_from_name(&name_id) => Ok(HirType::new_ref(*ty)), - _ => Err(HIRError::name_unrecognized(name_id, *span)), - } - } + ///Tries to retrieve a variable with the provided `name` on the current active scope pub fn get_variable(&mut self, symbol: SymbolPointer, span: &Span) -> Result { if let Some(variable) = self.modules.find_variable(symbol) { diff --git a/crates/hir/src/helpers/types.rs b/crates/hir/src/helpers/types.rs index 26e92f49..171af4db 100644 --- a/crates/hir/src/helpers/types.rs +++ b/crates/hir/src/helpers/types.rs @@ -35,15 +35,6 @@ impl SlynxHir { pub fn component_type(&self) -> TypeId { self.modules.types_module.generic_component_id() } - ///Gets the HIR type of the given `ty` - pub fn get_type(&self, ty: &TypeId) -> &HirType { - self.modules.types_module.get_type(ty) - } - - ///Gets the HIR type of the given `ty` - pub fn get_type_mut(&mut self, ty: &TypeId) -> &mut HirType { - self.modules.types_module.get_type_mut(ty) - } ///Creates a new tuple type with the given `fields` and returns its typeid pub fn create_tuple_type(&mut self, fields: Vec) -> TypeId { diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 28ec7f26..186c0768 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -82,6 +82,7 @@ pub mod model; pub mod modules; /// Name resolution utilities. pub mod names; +mod queries; mod statements; pub use crate::error::{HIRError, HIRErrorKind}; diff --git a/crates/hir/src/model/types.rs b/crates/hir/src/model/types.rs index 1e836f86..42d53711 100644 --- a/crates/hir/src/model/types.rs +++ b/crates/hir/src/model/types.rs @@ -138,7 +138,7 @@ pub enum FieldMethod { /// } /// ``` #[derive(Debug, Clone)] -pub struct ComponentProperty(VisibilityModifier, String, TypeId); +pub struct ComponentProperty(VisibilityModifier, SymbolPointer, TypeId); impl ComponentProperty { /// Creates a new component property. @@ -152,7 +152,7 @@ impl ComponentProperty { /// # Returns /// /// A new [`ComponentProperty`] instance. - pub fn new(visibility: VisibilityModifier, name: String, ty: TypeId) -> Self { + pub fn new(visibility: VisibilityModifier, name: SymbolPointer, ty: TypeId) -> Self { Self(visibility, name, ty) } @@ -166,7 +166,7 @@ impl ComponentProperty { /// # Returns /// /// A new [`ComponentProperty`] with public visibility. - pub fn new_public(name: String, ty: TypeId) -> Self { + pub fn new_public(name: SymbolPointer, ty: TypeId) -> Self { Self::new(VisibilityModifier::Public, name, ty) } @@ -180,7 +180,7 @@ impl ComponentProperty { /// # Returns /// /// A new [`ComponentProperty`] with private visibility. - pub fn new_private(name: String, ty: TypeId) -> Self { + pub fn new_private(name: SymbolPointer, ty: TypeId) -> Self { Self::new(VisibilityModifier::Private, name, ty) } @@ -190,8 +190,8 @@ impl ComponentProperty { } /// Returns the property's name. - pub fn name(&self) -> &str { - &self.1 + pub fn name(&self) -> SymbolPointer { + self.1 } /// Returns the property's type ID. diff --git a/crates/hir/src/names.rs b/crates/hir/src/names.rs index 5bd30f24..c92036dc 100644 --- a/crates/hir/src/names.rs +++ b/crates/hir/src/names.rs @@ -5,11 +5,17 @@ use crate::{ }; use common::{Span, SymbolPointer}; -use slynx_parser::GenericIdentifier; //file specific to implement things related to name resolution impl SlynxHir { + pub fn intern_name(&mut self, name: &str) -> SymbolPointer { + self.modules.intern_name(name) + } ///Since when a object is defined, its generated as an unnamed type, and has got a reference to it, this retrieves the inner layout of the object - pub fn get_object_type_from_name(&mut self, name: &str, span: &Span) -> Result<&HirType> { + pub(crate) fn get_object_type_from_name( + &mut self, + name: &str, + span: &Span, + ) -> Result<&HirType> { let name_symbol = self.modules.intern_name(name); if let Some(ref_id) = self.modules.types_module.get_id(&name_symbol) && let HirType::Reference { rf, .. } = self.modules.types_module.get_type(ref_id) @@ -24,51 +30,8 @@ impl SlynxHir { } } - /// Resolves the [`TypeId`] for the given plain type name string. - /// - /// Handles built-in names (`int`, `float`, `str`, `bool`, `void`, `Component`) directly, - /// and falls back to the module's type registry for user-defined types. - pub fn get_typeid_of_name(&mut self, name: &str, span: &Span) -> Result { - match name { - "Component" => Ok(self.component_type()), - "()" | "void" => Ok(self.void_type()), - "bool" => Ok(self.bool_type()), - "int" => Ok(self.int32_type()), - "float" => Ok(self.float32_type()), - "str" => Ok(self.str_type()), - _ => self.modules.get_type_by_name(name, span).cloned(), - } - } - - /// Resolves the [`TypeId`] for a [`GenericIdentifier`], handling tuple and generic types. - pub fn get_typeid_of_generic(&mut self, gener: &GenericIdentifier) -> Result { - match gener.identifier.as_str() { - "tuple" if let Some(ref types) = gener.generic => { - let fields = types - .iter() - .map(|field| self.get_typeid_of_generic(field)) - .collect::>>()?; - Ok(self.create_tuple_type(fields)) - } - "tuple" if let None = &gener.generic => { - let interned = self.modules.intern_name(&gener.identifier); - Err(HIRError::name_unrecognized(interned, gener.span)) - } - "()" => Ok(self.void_type()), - _ if let Some(ref generics) = gener.generic => { - let gen_ids = generics - .iter() - .map(|generic| self.get_typeid_of_generic(generic)) - .collect::>>()?; - let base_id = self.get_typeid_of_name(&gener.identifier, &gener.span)?; - Ok(self.create_unnamed_type(HirType::new_generic_ref(base_id, gen_ids))) - } - _ => self.get_typeid_of_name(&gener.identifier, &gener.span), - } - } - ///Creates a mutable variable with the given `name` and `ty` - pub fn create_mutable_variable( + pub(crate) fn create_mutable_variable( &mut self, symbol: SymbolPointer, ty: TypeId, @@ -77,7 +40,7 @@ impl SlynxHir { self.modules.create_variable(symbol, true, ty, span) } ///Creates a imutable variable with the given `name` and `ty` - pub fn create_variable( + pub(crate) fn create_variable( &mut self, symbol: SymbolPointer, ty: TypeId, @@ -87,16 +50,15 @@ impl SlynxHir { } ///Tries to retrieve the type and `TypeId` of the provided `name` in the global scope - pub fn retrieve_information_of_type( + pub(crate) fn retrieve_information_of_type( &mut self, name: &str, span: &Span, ) -> Result<(TypeId, &HirType)> { let name_symbol = self.modules.intern_name(name); match () { - _ if let Ok(id) = self.get_typeid_of_name(name, span) => Ok((id, self.get_type(&id))), - _ if let Some(id) = self.get_typeid_from_name(&name_symbol) => { - Ok((*id, self.get_type(id))) + _ if let Ok(id) = self.get_type_of_name(name_symbol, span) => { + Ok((id, self.get_type(&id))) } _ => Err(HIRError::name_unrecognized(name_symbol, *span)), } diff --git a/crates/hir/src/queries.rs b/crates/hir/src/queries.rs new file mode 100644 index 00000000..914f22c3 --- /dev/null +++ b/crates/hir/src/queries.rs @@ -0,0 +1,86 @@ +use common::{Span, SymbolPointer}; +use slynx_parser::GenericIdentifier; + +use crate::{DeclarationId, HIRError, HirType, Result, SlynxHir, TypeId}; + +impl SlynxHir { + ///Gets the HIR type of the given `ty` + pub fn get_type(&self, ty: &TypeId) -> &HirType { + self.modules.types_module.get_type(ty) + } + ///Gets the HIR type of the given `ty` + pub fn get_type_mut(&mut self, ty: &TypeId) -> &mut HirType { + self.modules.types_module.get_type_mut(ty) + } + + pub fn get_name(&self, name: SymbolPointer) -> &str { + self.modules.symbols_resolver.get_name(name) + } + + pub fn get_declaration_name(&self, id: DeclarationId) -> &str { + let ty = self.modules.declarations_module.get_declaration_type(id); + let ptr = self + .modules + .types_module + .get_type_name(&ty) + .expect("Declaration should contain a name"); + self.get_name(*ptr) + } + ///Retrieves the type of something by asserting the provided `ref_ty` is a reference type to it + pub fn get_type_from_ref(&self, ref_ty: TypeId) -> &HirType { + if let HirType::Reference { rf, .. } = self.get_type(&ref_ty) { + self.get_type(rf) + } else { + unreachable!("The provided ref_ty should be of type Reference"); + } + } + /// Resolves the [`TypeId`] for the given plain type name string. + /// + /// Handles built-in names (`int`, `float`, `str`, `bool`, `void`, `Component`) directly, + /// and falls back to the module's type registry for user-defined types. + pub fn get_type_of_name(&self, name: SymbolPointer, span: &Span) -> Result { + let name_ref = self.get_name(name); + match name_ref { + "Component" => Ok(self.component_type()), + "()" | "void" => Ok(self.void_type()), + "bool" => Ok(self.bool_type()), + "int" => Ok(self.int32_type()), + "float" => Ok(self.float32_type()), + "str" => Ok(self.str_type()), + _ => self + .modules + .types_module + .get_id(&name) + .cloned() + .ok_or(HIRError::type_unrecognized(name, *span)), + } + } + + /// Resolves the [`TypeId`] for a [`GenericIdentifier`], handling tuple and generic types. + pub(crate) fn get_type_of_generic(&mut self, gener: &GenericIdentifier) -> Result { + let name = self.intern_name(&gener.identifier); + match gener.identifier.as_str() { + "tuple" if let Some(ref types) = gener.generic => { + let fields = types + .iter() + .map(|field| self.get_type_of_generic(field)) + .collect::>>()?; + Ok(self.create_tuple_type(fields)) + } + "tuple" if let None = &gener.generic => { + let interned = self.modules.intern_name(&gener.identifier); + Err(HIRError::name_unrecognized(interned, gener.span)) + } + "()" => Ok(self.void_type()), + _ if let Some(ref generics) = gener.generic => { + let gen_ids = generics + .iter() + .map(|generic| self.get_type_of_generic(generic)) + .collect::>>()?; + let base_id = self.get_type_of_name(name, &gener.span)?; + Ok(self.create_unnamed_type(HirType::new_generic_ref(base_id, gen_ids))) + } + _ => self.get_type_of_name(name, &gener.span), + } + } +} From 7ef3a004058a417d194f680a4b8fff1f2909e1c2 Mon Sep 17 00:00:00 2001 From: caina Date: Sun, 24 May 2026 11:19:45 -0300 Subject: [PATCH 2/4] refactor: removed unused code --- crates/hir/src/helpers/names.rs | 2 +- crates/hir/src/helpers/types.rs | 16 ---------------- crates/hir/src/names.rs | 25 +------------------------ crates/hir/src/queries.rs | 29 ----------------------------- 4 files changed, 2 insertions(+), 70 deletions(-) diff --git a/crates/hir/src/helpers/names.rs b/crates/hir/src/helpers/names.rs index 67a91254..5f0fbe67 100644 --- a/crates/hir/src/helpers/names.rs +++ b/crates/hir/src/helpers/names.rs @@ -1,6 +1,6 @@ use common::{Span, SymbolPointer}; -use crate::{HIRError, HirType, Result, SlynxHir, VariableId}; +use crate::{HIRError, Result, SlynxHir, VariableId}; impl SlynxHir { /// Returns the source-level name string for the given symbol pointer. diff --git a/crates/hir/src/helpers/types.rs b/crates/hir/src/helpers/types.rs index 171af4db..38870430 100644 --- a/crates/hir/src/helpers/types.rs +++ b/crates/hir/src/helpers/types.rs @@ -35,7 +35,6 @@ impl SlynxHir { pub fn component_type(&self) -> TypeId { self.modules.types_module.generic_component_id() } - ///Creates a new tuple type with the given `fields` and returns its typeid pub fn create_tuple_type(&mut self, fields: Vec) -> TypeId { self.modules.types_module.create_tuple_type(fields) @@ -50,21 +49,6 @@ impl SlynxHir { self.modules.types_module.create_type(name, ty) } - /// Returns the [`HirType`] associated with the given name, if it exists. - pub fn get_type_from_pointer(&self, name: &SymbolPointer) -> Option<&HirType> { - self.modules.types_module.get_type_from_name(name) - } - - /// Returns a mutable reference to the [`HirType`] associated with the given name, if it exists. - pub fn get_type_mut_from_name(&mut self, name: &SymbolPointer) -> Option<&mut HirType> { - self.modules.types_module.get_type_from_name_mut(name) - } - - /// Returns the [`TypeId`] associated with the given name, if it exists. - pub fn get_typeid_from_name(&self, name: &SymbolPointer) -> Option<&TypeId> { - self.modules.types_module.get_id(name) - } - /// Returns the field layout (as a slice of symbol pointers) for the object with the given [`TypeId`]. pub fn get_object_fields(&self, ty: TypeId) -> Option<&[SymbolPointer]> { self.modules.declarations_module.get_object_body(ty) diff --git a/crates/hir/src/names.rs b/crates/hir/src/names.rs index c92036dc..824d8f44 100644 --- a/crates/hir/src/names.rs +++ b/crates/hir/src/names.rs @@ -1,8 +1,4 @@ -use crate::{ - Result, SlynxHir, TypeId, VariableId, - error::{HIRError, InvalidTypeReason}, - model::HirType, -}; +use crate::{Result, SlynxHir, TypeId, VariableId, error::HIRError, model::HirType}; use common::{Span, SymbolPointer}; //file specific to implement things related to name resolution @@ -10,25 +6,6 @@ impl SlynxHir { pub fn intern_name(&mut self, name: &str) -> SymbolPointer { self.modules.intern_name(name) } - ///Since when a object is defined, its generated as an unnamed type, and has got a reference to it, this retrieves the inner layout of the object - pub(crate) fn get_object_type_from_name( - &mut self, - name: &str, - span: &Span, - ) -> Result<&HirType> { - let name_symbol = self.modules.intern_name(name); - if let Some(ref_id) = self.modules.types_module.get_id(&name_symbol) - && let HirType::Reference { rf, .. } = self.modules.types_module.get_type(ref_id) - { - Ok(self.modules.types_module.get_type(rf)) - } else { - Err(HIRError::invalid_type( - name_symbol, - InvalidTypeReason::IncorrectUsage, - *span, - )) - } - } ///Creates a mutable variable with the given `name` and `ty` pub(crate) fn create_mutable_variable( diff --git a/crates/hir/src/queries.rs b/crates/hir/src/queries.rs index 914f22c3..1cdaddc6 100644 --- a/crates/hir/src/queries.rs +++ b/crates/hir/src/queries.rs @@ -1,5 +1,4 @@ use common::{Span, SymbolPointer}; -use slynx_parser::GenericIdentifier; use crate::{DeclarationId, HIRError, HirType, Result, SlynxHir, TypeId}; @@ -55,32 +54,4 @@ impl SlynxHir { .ok_or(HIRError::type_unrecognized(name, *span)), } } - - /// Resolves the [`TypeId`] for a [`GenericIdentifier`], handling tuple and generic types. - pub(crate) fn get_type_of_generic(&mut self, gener: &GenericIdentifier) -> Result { - let name = self.intern_name(&gener.identifier); - match gener.identifier.as_str() { - "tuple" if let Some(ref types) = gener.generic => { - let fields = types - .iter() - .map(|field| self.get_type_of_generic(field)) - .collect::>>()?; - Ok(self.create_tuple_type(fields)) - } - "tuple" if let None = &gener.generic => { - let interned = self.modules.intern_name(&gener.identifier); - Err(HIRError::name_unrecognized(interned, gener.span)) - } - "()" => Ok(self.void_type()), - _ if let Some(ref generics) = gener.generic => { - let gen_ids = generics - .iter() - .map(|generic| self.get_type_of_generic(generic)) - .collect::>>()?; - let base_id = self.get_type_of_name(name, &gener.span)?; - Ok(self.create_unnamed_type(HirType::new_generic_ref(base_id, gen_ids))) - } - _ => self.get_type_of_name(name, &gener.span), - } - } } From 9493a1e07e3562b6c9b08f15942f3495aac0ff88 Mon Sep 17 00:00:00 2001 From: caina Date: Sun, 24 May 2026 11:29:52 -0300 Subject: [PATCH 3/4] chore: made a lot of methods on hir to be public only to the hir itself --- crates/hir/src/components.rs | 10 +++++----- crates/hir/src/declarations.rs | 22 +++++++++++++--------- crates/hir/src/expression.rs | 8 ++++---- crates/hir/src/helpers/expressions.rs | 24 ++++++++++++++---------- crates/hir/src/helpers/names.rs | 5 ----- crates/hir/src/statements.rs | 13 ++++++++----- 6 files changed, 44 insertions(+), 38 deletions(-) diff --git a/crates/hir/src/components.rs b/crates/hir/src/components.rs index 39b55f26..c72e86ca 100644 --- a/crates/hir/src/components.rs +++ b/crates/hir/src/components.rs @@ -56,7 +56,7 @@ impl SlynxHir { } /// Resolves the provided values on a component. The `ty` is the type of the component we are resolving it - pub fn resolve_component_members( + pub(crate) fn resolve_component_members( &mut self, members: &[ComponentMemberValue], ty: TypeId, @@ -73,7 +73,7 @@ impl SlynxHir { /// Resolves the provided `values` as members of the `Text` specialized component. /// /// Expects exactly one `text` property assignment and no children. - pub fn resolve_specialize_text( + pub(crate) fn resolve_specialize_text( &mut self, values: &[ComponentMemberValue], span: &Span, @@ -116,7 +116,7 @@ impl SlynxHir { } ///Resolves the provided `children` knowning it is a specialized div component - pub fn resolve_specialized_div( + pub(crate) fn resolve_specialized_div( &mut self, children: &[ComponentMemberValue], _: &Span, @@ -151,7 +151,7 @@ impl SlynxHir { Ok(HirSpecializedComponentExpression::new_div(children, style)) } ///Tries to resolve the given `child` as, either a specialized component, or a normal user defined component - pub fn try_resolve_specialized<'a>( + pub(crate) fn try_resolve_specialized<'a>( &mut self, child: &'a ComponentExpression, ) -> ( @@ -172,7 +172,7 @@ impl SlynxHir { } ///Resolves the provided `component` expression. If it's a specialized one, resolves as a `SpecializedComponent`, otherwise as a normal 'Component' - pub fn resolve_component_expression( + pub(crate) fn resolve_component_expression( &mut self, component: &ComponentExpression, ) -> Result { diff --git a/crates/hir/src/declarations.rs b/crates/hir/src/declarations.rs index 243ea954..f1524480 100644 --- a/crates/hir/src/declarations.rs +++ b/crates/hir/src/declarations.rs @@ -14,7 +14,7 @@ use slynx_parser::{ impl SlynxHir { ///Hoists a `stylesheet` declaration - pub fn hoist_stylesheet(&mut self, name: &str, args: &[TypedName]) { + pub(crate) fn hoist_stylesheet(&mut self, name: &str, args: &[TypedName]) { self.modules.create_declaration( name, HirType::Style { @@ -24,7 +24,7 @@ impl SlynxHir { } ///Resolves a `stylesheet` declaration - pub fn resolve_stylesheet( + pub(crate) fn resolve_stylesheet( &mut self, name: &GenericIdentifier, args: &[TypedName], @@ -73,7 +73,7 @@ impl SlynxHir { } ///Resolves a style usage from the given `usage` expression. It's expected to be a function call. The reason is cause the same syntax for function call is used when calling styles - pub fn resolve_style_usage(&mut self, usage: &ASTExpression) -> Result { + pub(crate) fn resolve_style_usage(&mut self, usage: &ASTExpression) -> Result { let (name, args) = match &usage.kind { ASTExpressionKind::FunctionCall { name, args } => (name, args), _ => unreachable!("Style usage should be a function call on parsing"), @@ -95,7 +95,7 @@ impl SlynxHir { } /// Resolves an object declaration, filling in its field types and pushing the declaration. - pub fn resolve_object( + pub(crate) fn resolve_object( &mut self, name: &GenericIdentifier, fields: &[ObjectField], @@ -133,7 +133,11 @@ impl SlynxHir { } /// Hoists a function declaration by registering its signature without processing its body. - pub fn hoist_function(&mut self, name: &GenericIdentifier, args: &[TypedName]) -> Result<()> { + pub(crate) fn hoist_function( + &mut self, + name: &GenericIdentifier, + args: &[TypedName], + ) -> Result<()> { let args = args.iter().map(|_| self.int32_type()).collect(); let return_type = self.int32_type(); self.modules @@ -143,7 +147,7 @@ impl SlynxHir { } /// Resolves a function declaration, type-checking its body and pushing the HIR declaration. - pub fn resolve_function( + pub(crate) fn resolve_function( &mut self, name: &GenericIdentifier, args: &[TypedName], @@ -209,7 +213,7 @@ impl SlynxHir { } /// Hoists a component declaration by registering its property layout without resolving children. - pub fn hoist_component( + pub(crate) fn hoist_component( &mut self, name: &GenericIdentifier, members: &[ComponentMember], @@ -255,7 +259,7 @@ impl SlynxHir { } /// Resolves the member definitions of a component body into [`ComponentMemberDeclaration`]s. - pub fn resolve_component_defs( + pub(crate) fn resolve_component_defs( &mut self, def: &[ComponentMember], ) -> Result> { @@ -293,7 +297,7 @@ impl SlynxHir { } ///Resolves a component declaration that contains the given `members` and the given `name` - pub fn resolve_component_declaration( + pub(crate) fn resolve_component_declaration( &mut self, members: &[ComponentMember], name: &GenericIdentifier, diff --git a/crates/hir/src/expression.rs b/crates/hir/src/expression.rs index 4cf8dc02..ca1f8b54 100644 --- a/crates/hir/src/expression.rs +++ b/crates/hir/src/expression.rs @@ -10,7 +10,7 @@ use slynx_parser::{ASTExpression, ASTExpressionKind, ASTStatement, GenericIdenti impl SlynxHir { ///Generates a new object expression for the given `ty` object, and the given `fields`. - pub fn generate_object_expression( + pub(crate) fn generate_object_expression( &mut self, ty: TypeId, fields: &[NamedExpr], @@ -174,7 +174,7 @@ impl SlynxHir { } /// Resolves an `if` expression, type-checking the condition and both branches. - pub fn resolve_if_expression( + pub(crate) fn resolve_if_expression( &mut self, condition: &ASTExpression, if_body: &[ASTStatement], @@ -335,7 +335,7 @@ impl SlynxHir { /// Resolves the provided `expr` trying to infer its type, if not able, keeps as infer, and on later phases fallsback to the default value. /// Ty only serves to tell the type of the expression if it's needed to infer and check if it doesnt correspond - pub fn generate_expression( + pub(crate) fn generate_expression( &mut self, expr: &ASTExpression, ty: Option, @@ -401,7 +401,7 @@ impl SlynxHir { } ///Resolves the binary operation with the provided `lhs` and `rhs`. - pub fn resolve_binary( + pub(crate) fn resolve_binary( &mut self, lhs: &ASTExpression, op: Operator, diff --git a/crates/hir/src/helpers/expressions.rs b/crates/hir/src/helpers/expressions.rs index beb4b287..5f1d4d9a 100644 --- a/crates/hir/src/helpers/expressions.rs +++ b/crates/hir/src/helpers/expressions.rs @@ -6,7 +6,7 @@ use common::{Operator, Span, SymbolPointer}; impl SlynxHir { /// Creates a tuple expression with the given type and values. - pub fn create_tuple_expression( + pub(crate) fn create_tuple_expression( &self, tuple_ty: TypeId, values: Vec, @@ -20,7 +20,7 @@ impl SlynxHir { } } /// Creates a field access expression on `parent` at the given `field` index. - pub fn create_field_access_expression( + pub(crate) fn create_field_access_expression( &self, parent: HirExpression, field: usize, @@ -38,7 +38,7 @@ impl SlynxHir { } } /// Creates a boolean literal expression. - pub fn create_boolean_expression(&self, b: bool, span: Span) -> HirExpression { + pub(crate) fn create_boolean_expression(&self, b: bool, span: Span) -> HirExpression { HirExpression { id: ExpressionId::new(), ty: self.bool_type(), @@ -47,7 +47,11 @@ impl SlynxHir { } } /// Creates a string literal expression. - pub fn create_strliteral_expression(&self, s: SymbolPointer, span: Span) -> HirExpression { + pub(crate) fn create_strliteral_expression( + &self, + s: SymbolPointer, + span: Span, + ) -> HirExpression { HirExpression { id: ExpressionId::new(), ty: self.str_type(), @@ -56,7 +60,7 @@ impl SlynxHir { } } /// Creates an identifier expression referencing the given variable. - pub fn create_identifier_expression( + pub(crate) fn create_identifier_expression( &self, var: VariableId, ty: TypeId, @@ -70,7 +74,7 @@ impl SlynxHir { } } /// Creates an int expression that must be inferred. - pub fn create_int_expression(&self, i: i32, span: Span) -> HirExpression { + pub(crate) fn create_int_expression(&self, i: i32, span: Span) -> HirExpression { HirExpression { kind: HirExpressionKind::Int(i), id: ExpressionId::new(), @@ -80,7 +84,7 @@ impl SlynxHir { } /// Creates a float expression. - pub fn create_float_expression(&self, float: f32, span: Span) -> HirExpression { + pub(crate) fn create_float_expression(&self, float: f32, span: Span) -> HirExpression { HirExpression { kind: HirExpressionKind::Float(float), id: ExpressionId::new(), @@ -89,7 +93,7 @@ impl SlynxHir { } } /// Creates a binary expression. - pub fn create_binary_expression( + pub(crate) fn create_binary_expression( &self, left: HirExpression, right: HirExpression, @@ -109,7 +113,7 @@ impl SlynxHir { } } /// Creates a if expression. - pub fn create_if_expression( + pub(crate) fn create_if_expression( &self, condition: HirExpression, then_body: Vec, @@ -128,7 +132,7 @@ impl SlynxHir { span, } } - pub fn create_component_expression( + pub(crate) fn create_component_expression( &mut self, component: HirComponentExpression, ty: TypeId, diff --git a/crates/hir/src/helpers/names.rs b/crates/hir/src/helpers/names.rs index 5f0fbe67..55156a8f 100644 --- a/crates/hir/src/helpers/names.rs +++ b/crates/hir/src/helpers/names.rs @@ -3,11 +3,6 @@ use common::{Span, SymbolPointer}; use crate::{HIRError, Result, SlynxHir, VariableId}; impl SlynxHir { - /// Returns the source-level name string for the given symbol pointer. - pub fn get_name_from_pointer(&self, ptr: SymbolPointer) -> &str { - self.modules.symbols_resolver.get_name(ptr) - } - ///Tries to retrieve a variable with the provided `name` on the current active scope pub fn get_variable(&mut self, symbol: SymbolPointer, span: &Span) -> Result { if let Some(variable) = self.modules.find_variable(symbol) { diff --git a/crates/hir/src/statements.rs b/crates/hir/src/statements.rs index 537b82fc..92c161c5 100644 --- a/crates/hir/src/statements.rs +++ b/crates/hir/src/statements.rs @@ -10,7 +10,7 @@ use slynx_parser::{ASTStatement, ASTStatementKind, NamedExpr, StyleBlock, StyleS impl SlynxHir { /// Resolves an AST statement into a typed [`HirStatement`]. - pub fn resolve_statement(&mut self, statement: &ASTStatement) -> Result { + pub(crate) fn resolve_statement(&mut self, statement: &ASTStatement) -> Result { match &statement.kind { ASTStatementKind::Expression(expr) => { let expr = self.generate_expression(expr, None)?; @@ -62,7 +62,7 @@ impl SlynxHir { } ///Transforms the given `statement` into an HIR style statement - pub fn resolve_stylesheet_statement( + pub(crate) fn resolve_stylesheet_statement( &mut self, statement: &StyleSheetStatement, ) -> Result { @@ -77,7 +77,7 @@ impl SlynxHir { } ///Type of styles is made by its name. - pub fn resolve_style_type(&mut self, name: &str, span: Span) -> Result { + pub(crate) fn resolve_style_type(&mut self, name: &str, span: Span) -> Result { let ty = match name { "backgroundColor" | "foregroundColor" => self.int32_type(), _ => { @@ -89,7 +89,7 @@ impl SlynxHir { } ///Transforms the given `definitions` in a vector of `StyleDefinition` - pub fn resolve_style_definitions( + pub(crate) fn resolve_style_definitions( &mut self, definitions: &[NamedExpr], ) -> Result> { @@ -105,7 +105,10 @@ impl SlynxHir { } ///Resolves the given `styles` blocks, and creates `HirStyleBlock`s based on them - pub fn resolve_stylesblock(&mut self, styles: &[StyleBlock]) -> Result> { + pub(crate) fn resolve_stylesblock( + &mut self, + styles: &[StyleBlock], + ) -> Result> { let mut out = Vec::new(); for style in styles { let kind = { From df7ecc81a54b9c869e2699c03dc098489c68f43c Mon Sep 17 00:00:00 2001 From: caina Date: Sun, 24 May 2026 14:24:09 -0300 Subject: [PATCH 4/4] chore: removed duplicated function and its usage --- crates/hir/src/components.rs | 4 ++-- crates/hir/src/declarations.rs | 21 ++++++++++----------- crates/hir/src/expression.rs | 4 ++-- crates/hir/src/lib.rs | 2 +- crates/hir/src/names.rs | 17 +---------------- crates/hir/src/statements.rs | 28 ++++++++++------------------ 6 files changed, 26 insertions(+), 50 deletions(-) diff --git a/crates/hir/src/components.rs b/crates/hir/src/components.rs index c72e86ca..c7527699 100644 --- a/crates/hir/src/components.rs +++ b/crates/hir/src/components.rs @@ -179,8 +179,8 @@ impl SlynxHir { match self.try_resolve_specialized(component) { (Some(spec), None) => spec.map(HirComponentExpression::Specialized), (None, Some(component)) => { - let (id, _) = - self.retrieve_information_of_type(&component.name.identifier, &component.span)?; + let name = self.intern_name(&component.name.identifier); + let id = self.get_type_of_name(name, &component.span)?; let (properties, children) = self.resolve_component_members(&component.values, id)?; Ok(HirComponentExpression::new_normal( diff --git a/crates/hir/src/declarations.rs b/crates/hir/src/declarations.rs index f1524480..dff5b1d6 100644 --- a/crates/hir/src/declarations.rs +++ b/crates/hir/src/declarations.rs @@ -42,8 +42,8 @@ impl SlynxHir { .iter() .map(|arg| { let symbol = self.modules.intern_name(&arg.name); - let (ty, _) = - self.retrieve_information_of_type(&arg.kind.identifier, &arg.kind.span)?; + let ty_symbol = self.modules.intern_name(&arg.kind.identifier); + let ty = self.get_type_of_name(ty_symbol, &arg.kind.span)?; self.create_variable(symbol, ty, &arg.span).map(|v| (v, ty)) }) .collect::, Vec<_>)>>()?; @@ -108,8 +108,8 @@ impl SlynxHir { if self.modules.intern_name(&field.name.name) == symbol_name { Err(HIRError::recursive(symbol_name, field.name.span)) } else { - self.retrieve_information_of_type(&field.name.kind.identifier, &field.name.span) - .map(|v| v.0) + let name = self.intern_name(&field.name.kind.identifier); + self.get_type_of_name(name, &field.name.span) } }) .collect::>>()?; @@ -165,16 +165,15 @@ impl SlynxHir { let (args, argsty) = args .iter() .map(|arg| { + let ty_symbol = self.modules.intern_name(&arg.kind.identifier); let symbol = self.modules.intern_name(&arg.name); - let (ty, _) = - self.retrieve_information_of_type(&arg.kind.identifier, &arg.kind.span)?; + let ty = self.get_type_of_name(ty_symbol, &arg.kind.span)?; self.create_variable(symbol, ty, &arg.span).map(|v| (v, ty)) }) .collect::, Vec<_>)>>()?; { - let ret_tyid = self - .retrieve_information_of_type(&return_type.identifier, span)? - .0; + let return_symbol = self.intern_name(&return_type.identifier); + let ret_tyid = self.get_type_of_name(return_symbol, span)?; let HirType::Function { args, return_type: ret, @@ -269,8 +268,8 @@ impl SlynxHir { match &def.kind { ComponentMemberKind::Property { ty, rhs, name, .. } => { let ty = if let Some(ty) = ty { - self.retrieve_information_of_type(&ty.identifier, &ty.span)? - .0 + let symbol = self.intern_name(&ty.identifier); + self.get_type_of_name(symbol, &ty.span)? } else { self.infer_type() }; diff --git a/crates/hir/src/expression.rs b/crates/hir/src/expression.rs index ca1f8b54..0f6ef599 100644 --- a/crates/hir/src/expression.rs +++ b/crates/hir/src/expression.rs @@ -376,8 +376,8 @@ impl SlynxHir { Ok(self.create_float_expression(*float, expr.span)) } ASTExpressionKind::Component(component) => { - let (id, _) = - self.retrieve_information_of_type(&component.name.identifier, &component.span)?; + let symbol = self.intern_name(&component.name.identifier); + let id = self.get_type_of_name(symbol, &component.span)?; let component = self.resolve_component_expression(component)?; Ok(self.create_component_expression(component, id, expr.span)) } diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 186c0768..c1c03938 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -156,7 +156,7 @@ pub struct SlynxHir { /// This is the primary interface for working with the HIR's namespace and /// type system. It provides methods for creating variables, looking up /// declarations, and managing nested scopes. - pub modules: HirModules, + pub(crate) modules: HirModules, /// All top-level declarations generated from the source. /// diff --git a/crates/hir/src/names.rs b/crates/hir/src/names.rs index 824d8f44..7d9b03b6 100644 --- a/crates/hir/src/names.rs +++ b/crates/hir/src/names.rs @@ -1,4 +1,4 @@ -use crate::{Result, SlynxHir, TypeId, VariableId, error::HIRError, model::HirType}; +use crate::{Result, SlynxHir, TypeId, VariableId}; use common::{Span, SymbolPointer}; //file specific to implement things related to name resolution @@ -25,19 +25,4 @@ impl SlynxHir { ) -> Result { self.modules.create_variable(symbol, false, ty, span) } - - ///Tries to retrieve the type and `TypeId` of the provided `name` in the global scope - pub(crate) fn retrieve_information_of_type( - &mut self, - name: &str, - span: &Span, - ) -> Result<(TypeId, &HirType)> { - let name_symbol = self.modules.intern_name(name); - match () { - _ if let Ok(id) = self.get_type_of_name(name_symbol, span) => { - Ok((id, self.get_type(&id))) - } - _ => Err(HIRError::name_unrecognized(name_symbol, *span)), - } - } } diff --git a/crates/hir/src/statements.rs b/crates/hir/src/statements.rs index 92c161c5..4a528d26 100644 --- a/crates/hir/src/statements.rs +++ b/crates/hir/src/statements.rs @@ -25,27 +25,19 @@ impl SlynxHir { span: statement.span, }) } - ASTStatementKind::MutableVar { name, ty, rhs } => { + kind @ (ASTStatementKind::MutableVar { name, ty, rhs } + | ASTStatementKind::Var { name, ty, rhs }) => { let typeid = ty.as_ref().and_then(|t| { - self.retrieve_information_of_type(&t.identifier, &statement.span) - .ok() - .map(|inner| inner.0) + let symbol = self.intern_name(&t.identifier); + self.get_type_of_name(symbol, &statement.span).ok() }); + let name = self.intern_name(name); let rhs = self.generate_expression(rhs, typeid)?; - let name = self.modules.intern_name(name); - let id = self.create_mutable_variable(name, rhs.ty, &statement.span)?; - - Ok(HirStatement::new_variable(id, rhs, statement.span)) - } - ASTStatementKind::Var { name, ty, rhs } => { - let typeid = ty.as_ref().and_then(|t| { - self.retrieve_information_of_type(&t.identifier, &statement.span) - .ok() - .map(|inner| inner.0) - }); - let rhs = self.generate_expression(rhs, typeid)?; - let name = self.modules.intern_name(name); - let id = self.create_variable(name, rhs.ty, &statement.span)?; + let id = if let ASTStatementKind::MutableVar { .. } = kind { + self.create_mutable_variable(name, rhs.ty, &statement.span) + } else { + self.create_variable(name, rhs.ty, &statement.span) + }?; Ok(HirStatement::new_variable(id, rhs, statement.span)) }