Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions crates/hir/src/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
) -> (
Expand All @@ -172,15 +172,15 @@ 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<HirComponentExpression> {
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(
Expand Down
95 changes: 54 additions & 41 deletions crates/hir/src/declarations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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],
Expand All @@ -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::<Result<(Vec<_>, Vec<_>)>>()?;
Expand Down Expand Up @@ -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<HirStyleUsage> {
pub(crate) fn resolve_style_usage(&mut self, usage: &ASTExpression) -> Result<HirStyleUsage> {
let (name, args) = match &usage.kind {
ASTExpressionKind::FunctionCall { name, args } => (name, args),
_ => unreachable!("Style usage should be a function call on parsing"),
Expand All @@ -94,17 +94,8 @@ 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(
pub(crate) fn resolve_object(
&mut self,
name: &GenericIdentifier,
fields: &[ObjectField],
Expand All @@ -117,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::<Result<Vec<_>>>()?;
Expand All @@ -142,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
Expand All @@ -152,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],
Expand All @@ -170,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::<Result<(Vec<_>, 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,
Expand Down Expand Up @@ -218,7 +212,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],
Expand All @@ -227,13 +221,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,
})
Expand All @@ -245,7 +258,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<Vec<ComponentMemberDeclaration>> {
Expand All @@ -255,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()
};
Expand All @@ -283,7 +296,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,
Expand All @@ -310,25 +323,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));
Expand Down
12 changes: 6 additions & 6 deletions crates/hir/src/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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<TypeId>,
Expand Down Expand Up @@ -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))
}
Expand All @@ -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,
Expand Down
Loading