Skip to content

Commit

Permalink
Implement DelineatedPathExpression AST resolve handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
tritao committed Aug 19, 2024
1 parent 85c9d2a commit 1861270
Show file tree
Hide file tree
Showing 9 changed files with 347 additions and 43 deletions.
30 changes: 29 additions & 1 deletion sway-core/src/language/parsed/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ impl Declaration {
}
}

pub(crate) fn to_fn_ref(
pub(crate) fn to_fn_decl(
&self,
handler: &Handler,
engines: &Engines,
Expand Down Expand Up @@ -166,6 +166,34 @@ impl Declaration {
}
}

pub(crate) fn to_enum_decl(
&self,
handler: &Handler,
engines: &Engines,
) -> Result<ParsedDeclId<EnumDeclaration>, ErrorEmitted> {
match self {
Declaration::EnumDeclaration(decl_id) => Ok(*decl_id),
decl => Err(handler.emit_err(CompileError::DeclIsNotAnEnum {
actually: decl.friendly_type_name().to_string(),
span: decl.span(engines),
})),
}
}

pub(crate) fn to_const_decl(
&self,
handler: &Handler,
engines: &Engines,
) -> Result<ParsedDeclId<ConstantDeclaration>, ErrorEmitted> {
match self {
Declaration::ConstantDeclaration(decl_id) => Ok(*decl_id),
decl => Err(handler.emit_err(CompileError::DeclIsNotAConstant {
actually: decl.friendly_type_name().to_string(),
span: decl.span(engines),
})),
}
}

#[allow(unused)]
pub(crate) fn visibility(&self, decl_engine: &ParsedDeclEngine) -> Visibility {
match self {
Expand Down
4 changes: 3 additions & 1 deletion sway-core/src/language/parsed/expression/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub use method_name::MethodName;
pub use scrutinee::*;
use sway_ast::intrinsics::Intrinsic;

use super::{FunctionDeclaration, StructDeclaration};
use super::{Declaration, FunctionDeclaration, StructDeclaration};

/// Represents a parsed, but not yet type checked, [Expression](https://en.wikipedia.org/wiki/Expression_(computer_science)).
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -295,6 +295,8 @@ impl PartialEqWithEngines for AmbiguousPathExpression {
#[derive(Debug, Clone)]
pub struct DelineatedPathExpression {
pub call_path_binding: TypeBinding<QualifiedCallPath>,
pub resolved_call_path_binding: Option<TypeBinding<ResolvedCallPath<Declaration>>>,

/// When args is equal to Option::None then it means that the
/// [DelineatedPathExpression] was initialized from an expression
/// that does not end with parenthesis.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ impl ty::TyExpression {
ExpressionKind::DelineatedPath(delineated_path_expression) => {
let DelineatedPathExpression {
call_path_binding,
resolved_call_path_binding: _,
args,
} = *delineated_path_expression.clone();
Self::type_check_delineated_path(
Expand Down
1 change: 1 addition & 0 deletions sway-core/src/semantic_analysis/node_dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,7 @@ impl Dependencies {
ExpressionKind::DelineatedPath(delineated_path_expression) => {
let DelineatedPathExpression {
call_path_binding,
resolved_call_path_binding: _,
args,
} = &**delineated_path_expression;
// It's either a module path which we can ignore, or an enum variant path, in which
Expand Down
62 changes: 41 additions & 21 deletions sway-core/src/semantic_analysis/symbol_resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
},
CallPath, CallPathTree, ResolvedCallPath,
},
TraitConstraint, TypeArgument, TypeBinding, TypeParameter,
TraitConstraint, TypeArgs, TypeArgument, TypeBinding, TypeParameter,
};

use super::symbol_resolve_context::SymbolResolveContext;
Expand Down Expand Up @@ -495,23 +495,14 @@ impl ResolveSymbols for StructScrutineeField {
}

impl ResolveSymbols for Expression {
fn resolve_symbols(&mut self, handler: &Handler, ctx: SymbolResolveContext) {
self.kind.resolve_symbols(handler, ctx);
}
}

impl ResolveSymbols for ExpressionKind {
fn resolve_symbols(&mut self, handler: &Handler, mut ctx: SymbolResolveContext) {
match self {
match &mut self.kind {
ExpressionKind::Error(_, _) => {}
ExpressionKind::Literal(_) => {}
ExpressionKind::AmbiguousPathExpression(_) => {}
ExpressionKind::FunctionApplication(expr) => {
let result = SymbolResolveTypeBinding::resolve_symbol(
&mut expr.call_path_binding,
&Handler::default(),
ctx.by_ref(),
);
let result = expr.call_path_binding.resolve_symbol(handler, ctx.by_ref());

if let Ok(result) = result {
expr.resolved_call_path_binding = Some(TypeBinding::<
ResolvedCallPath<ParsedDeclId<FunctionDeclaration>>,
Expand Down Expand Up @@ -547,13 +538,11 @@ impl ResolveSymbols for ExpressionKind {
.iter_mut()
.for_each(|e| e.resolve_symbols(handler, ctx.by_ref())),
ExpressionKind::Struct(expr) => {
expr.call_path_binding
.resolve_symbols(handler, ctx.by_ref());
let result = SymbolResolveTypeBinding::resolve_symbol(
&mut expr.call_path_binding,
&Handler::default(),
ctx.by_ref(),
);
// expr.call_path_binding
// .resolve_symbols(handler, ctx.by_ref());

let result = expr.call_path_binding.resolve_symbol(handler, ctx.by_ref());

if let Ok(result) = result {
expr.resolved_call_path_binding = Some(TypeBinding::<
ResolvedCallPath<ParsedDeclId<StructDeclaration>>,
Expand Down Expand Up @@ -604,7 +593,25 @@ impl ResolveSymbols for ExpressionKind {
}
ExpressionKind::Subfield(expr) => expr.prefix.resolve_symbols(handler, ctx),
ExpressionKind::DelineatedPath(expr) => {
expr.call_path_binding.resolve_symbols(handler, ctx)
// expr.call_path_binding
// .resolve_symbols(handler, ctx.by_ref());

let result =
expr.call_path_binding
.resolve_symbol(handler, ctx.by_ref(), self.span.clone());

// if let Ok(result) = result {
// expr.resolved_call_path_binding = Some(TypeBinding::<
// ResolvedCallPath<ParsedDeclId<StructDeclaration>>,
// > {
// inner: ResolvedCallPath {
// decl: result,
// unresolved_call_path: expr.call_path_binding.inner.clone(),
// },
// span: expr.call_path_binding.span.clone(),
// type_arguments: expr.call_path_binding.type_arguments.clone(),
// });
// }
}
ExpressionKind::AbiCast(expr) => {
expr.abi_name.resolve_symbols(handler, ctx.by_ref());
Expand Down Expand Up @@ -644,3 +651,16 @@ impl ResolveSymbols for ExpressionKind {
}
}
}

impl<T> ResolveSymbols for TypeBinding<T> {
fn resolve_symbols(&mut self, handler: &Handler, mut ctx: SymbolResolveContext) {
match self.type_arguments {
TypeArgs::Regular(ref mut args) => args
.iter_mut()
.for_each(|arg| arg.resolve_symbols(handler, ctx.by_ref())),
TypeArgs::Prefix(ref mut args) => args
.iter_mut()
.for_each(|arg| arg.resolve_symbols(handler, ctx.by_ref())),
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2904,6 +2904,7 @@ fn path_expr_to_expression(
Expression {
kind: ExpressionKind::DelineatedPath(Box::new(DelineatedPathExpression {
call_path_binding,
resolved_call_path_binding: None,
args: None,
})),
span,
Expand Down
Loading

0 comments on commit 1861270

Please sign in to comment.