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 Jul 12, 2024
1 parent c13b67d commit 6a16d32
Show file tree
Hide file tree
Showing 8 changed files with 342 additions and 31 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 @@ -118,7 +118,7 @@ impl Declaration {
}
}

pub(crate) fn to_fn_ref(
pub(crate) fn to_fn_decl(
&self,
handler: &Handler,
engines: &Engines,
Expand Down Expand Up @@ -146,6 +146,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 @@ -618,6 +618,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
47 changes: 37 additions & 10 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 @@ -491,14 +491,8 @@ 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(_) => {}
Expand Down Expand Up @@ -599,8 +593,28 @@ impl ResolveSymbols for ExpressionKind {
.for_each(|arg| arg.resolve_symbols(handler, ctx.by_ref()));
}
ExpressionKind::Subfield(expr) => expr.prefix.resolve_symbols(handler, ctx),
ExpressionKind::DelineatedPath(expr) => {
expr.call_path_binding.resolve_symbols(handler, ctx)
ExpressionKind::DelineatedPath(ref mut expr) => {
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 @@ -640,3 +654,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 @@ -2900,6 +2900,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 6a16d32

Please sign in to comment.