Skip to content

Commit

Permalink
Keep track of parents during function application monomorphization.
Browse files Browse the repository at this point in the history
  • Loading branch information
tritao committed Feb 1, 2023
1 parent 2eb945b commit 7f65d7e
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 10 deletions.
4 changes: 4 additions & 0 deletions sway-core/src/language/ty/ast_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ pub trait GetDeclIdent {
fn get_decl_ident(&self, decl_engine: &DeclEngine) -> Option<Ident>;
}

pub trait GetDeclId {
fn get_decl_id(&self) -> Option<DeclId>;
}

#[derive(Clone, Debug)]
pub struct TyAstNode {
pub content: TyAstNodeContent,
Expand Down
18 changes: 18 additions & 0 deletions sway-core/src/language/ty/declaration/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,24 @@ impl GetDeclIdent for TyDeclaration {
}
}

impl GetDeclId for TyDeclaration {
fn get_decl_id(&self) -> Option<DeclId> {
match self {
TyDeclaration::VariableDeclaration(_) => todo!("not a declaration id yet"),
TyDeclaration::ConstantDeclaration(decl) => Some(decl.clone()),
TyDeclaration::FunctionDeclaration(decl) => Some(decl.clone()),
TyDeclaration::TraitDeclaration(decl) => Some(decl.clone()),
TyDeclaration::StructDeclaration(decl) => Some(decl.clone()),
TyDeclaration::EnumDeclaration(decl) => Some(decl.clone()),
TyDeclaration::ImplTrait(decl) => Some(decl.clone()),
TyDeclaration::AbiDeclaration(decl) => Some(decl.clone()),
TyDeclaration::GenericTypeForFunctionScope { .. } => None,
TyDeclaration::ErrorRecovery(_) => None,
TyDeclaration::StorageDeclaration(_decl) => None,
}
}
}

impl TyDeclaration {
/// Retrieves the declaration as an enum declaration.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ pub(crate) use self::{
use crate::{
asm_lang::{virtual_ops::VirtualOp, virtual_register::VirtualRegister},
error::*,
language::{parsed::*, ty, *},
language::{
parsed::*,
ty::{self, GetDeclId},
*,
},
semantic_analysis::*,
transform::to_parsed_lang::type_name_to_type_info_opt,
type_system::*,
Expand Down Expand Up @@ -482,7 +486,7 @@ impl ty::TyExpression {
);

// check that the decl is a function decl
let function_decl = check!(
let _ = check!(
unknown_decl.expect_function(decl_engine, &span),
return err(warnings, errors),
warnings,
Expand All @@ -491,7 +495,7 @@ impl ty::TyExpression {

instantiate_function_application(
ctx,
function_decl,
unknown_decl.get_decl_id().unwrap(),
call_path_binding,
Some(arguments),
span,
Expand Down Expand Up @@ -1106,14 +1110,24 @@ impl ty::TyExpression {
// Check if this could be a function
let mut function_probe_warnings = Vec::new();
let mut function_probe_errors = Vec::new();
let maybe_function = {

let maybe_function_decl = {
let mut call_path_binding = unknown_call_path_binding.clone();
TypeBinding::type_check_with_ident(&mut call_path_binding, ctx.by_ref())
.flat_map(|unknown_decl| unknown_decl.expect_function(decl_engine, &span))
.ok(&mut function_probe_warnings, &mut function_probe_errors)
.map(|func_decl| (func_decl, call_path_binding))
};

let maybe_function = {
maybe_function_decl.clone().and_then(|f| {
let (fn_decl, call_path_binding) = f;
fn_decl
.expect_function(decl_engine, &span)
.ok(&mut function_probe_warnings, &mut function_probe_errors)
.map(|func_decl| (func_decl, call_path_binding))
})
};

// Check if this could be an enum
let mut enum_probe_warnings = vec![];
let mut enum_probe_errors = vec![];
Expand Down Expand Up @@ -1168,11 +1182,12 @@ impl ty::TyExpression {
errors
)
}
(false, Some((func_decl, call_path_binding)), None, None) => {
(false, Some((_func_decl, call_path_binding)), None, None) => {
warnings.append(&mut function_probe_warnings);
errors.append(&mut function_probe_errors);
let decl_id = maybe_function_decl.and_then(|d| d.0.get_decl_id()).unwrap();
check!(
instantiate_function_application(ctx, func_decl, call_path_binding, args, span),
instantiate_function_application(ctx, decl_id, call_path_binding, args, span),
return err(warnings, errors),
warnings,
errors
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
decl_engine::ReplaceDecls,
decl_engine::{DeclId, ReplaceDecls},
error::*,
language::{ty, *},
semantic_analysis::{ast_node::*, TypeCheckContext},
Expand All @@ -11,7 +11,7 @@ use sway_types::Spanned;
#[allow(clippy::too_many_arguments)]
pub(crate) fn instantiate_function_application(
mut ctx: TypeCheckContext,
mut function_decl: ty::TyFunctionDeclaration,
function_decl_id: DeclId,
call_path_binding: TypeBinding<CallPath>,
arguments: Option<Vec<Expression>>,
span: Span,
Expand All @@ -22,6 +22,10 @@ pub(crate) fn instantiate_function_application(
let decl_engine = ctx.decl_engine;
let engines = ctx.engines();

let mut function_decl = decl_engine
.get_function(function_decl_id.clone(), &span)
.unwrap();

if arguments.is_none() {
errors.push(CompileError::MissingParenthesesForFunction {
method_name: call_path_binding.inner.suffix.clone(),
Expand Down Expand Up @@ -86,7 +90,9 @@ pub(crate) fn instantiate_function_application(
);
function_decl.replace_decls(&decl_mapping, engines);
let return_type = function_decl.return_type;
let new_decl_id = decl_engine.insert(function_decl);
let new_decl_id = decl_engine
.insert(function_decl)
.with_parent(decl_engine, function_decl_id);

let exp = ty::TyExpression {
expression: ty::TyExpressionVariant::FunctionApplication {
Expand Down

0 comments on commit 7f65d7e

Please sign in to comment.