Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emilyaherbert/param stack 3 #4303

Closed
wants to merge 12 commits into from
2 changes: 1 addition & 1 deletion sway-core/src/abi_generation/evm_json_abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ pub fn json_abi_str(
Unknown => "unknown".into(),
UnknownGeneric { name, .. } => name.to_string(),
Placeholder(_) => "_".to_string(),
TypeParam(n) => format!("typeparam({n})"),
TypeParam { index, .. } => format!("typeparam({index})"),
Str(x) => format!("str[{}]", x.val()),
UnsignedInteger(x) => match x {
IntegerBits::Eight => "uint8",
Expand Down
2 changes: 1 addition & 1 deletion sway-core/src/abi_generation/fuel_json_abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,7 @@ impl TypeInfo {
Unknown => "unknown".into(),
UnknownGeneric { name, .. } => name.to_string(),
Placeholder(_) => "_".to_string(),
TypeParam(n) => format!("typeparam({n})"),
TypeParam { index, .. } => format!("typeparam({index})"),
Str(x) => format!("str[{}]", x.val()),
UnsignedInteger(x) => match x {
IntegerBits::Eight => "u8",
Expand Down
1 change: 1 addition & 0 deletions sway-core/src/asm_generation/fuel/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ impl<'ir> FuelAsmBuilder<'ir> {
(Some(span), Some(decl_index)) => Some(DeclRef::new(
Ident::new(span.clone()),
*decl_index,
todo!(),
span.clone(),
)),
_ => None,
Expand Down
14 changes: 1 addition & 13 deletions sway-core/src/control_flow_analysis/dead_code_analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1020,26 +1020,14 @@ fn connect_expression<'eng: 'cfg, 'cfg>(
let fn_decl = decl_engine.get_function(fn_ref);
let mut is_external = false;

// in the case of monomorphized functions, first check if we already have a node for
// it in the namespace. if not then we need to check to see if the namespace contains
// the decl id parents (the original generic non monomorphized decl id).
let mut exists = false;
let parents = decl_engine.find_all_parents(engines, &fn_ref.id().clone());
for parent in parents.iter() {
if let Ok(parent_decl_id) = DeclId::try_from(parent) {
let parent = decl_engine.get_function(&parent_decl_id);
exists |= graph.namespace.get_function(&parent).is_some();
}
}

// find the function in the namespace
let fn_namespace_entry = graph.namespace.get_function(&fn_decl).cloned();

let mut leaves = leaves.to_vec();

// if the parent node exists in this module, then add the monomorphized version
// to the graph.
if fn_namespace_entry.is_none() && exists {
if fn_namespace_entry.is_none() {
let (l_leaves, _new_exit_node) = connect_node(
engines,
&ty::TyAstNode {
Expand Down
86 changes: 19 additions & 67 deletions sway-core/src/decl_engine/engine.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
use std::{
collections::{HashMap, HashSet, VecDeque},
sync::RwLock,
};

use sway_types::{Named, Spanned};

use crate::{
concurrent_slab::ConcurrentSlab,
decl_engine::*,
engine_threading::*,
language::ty::{
self, TyAbiDecl, TyConstantDecl, TyEnumDecl, TyFunctionDecl, TyImplTrait, TyStorageDecl,
TyStructDecl, TyTraitDecl, TyTraitFn, TyTypeAliasDecl,
},
type_system::SubstList,
};

/// Used inside of type inference to store declarations.
Expand All @@ -28,8 +23,6 @@ pub struct DeclEngine {
constant_slab: ConcurrentSlab<TyConstantDecl>,
enum_slab: ConcurrentSlab<TyEnumDecl>,
type_alias_slab: ConcurrentSlab<TyTypeAliasDecl>,

parents: RwLock<HashMap<FunctionalDeclId, Vec<FunctionalDeclId>>>,
}

pub trait DeclEngineGet<I, U> {
Expand All @@ -40,7 +33,7 @@ pub trait DeclEngineInsert<T>
where
T: Named + Spanned,
{
fn insert(&self, decl: T) -> DeclRef<DeclId<T>>;
fn insert(&self, decl: T, subst_list: SubstList) -> DeclRef<DeclId<T>>;
}

pub trait DeclEngineReplace<T> {
Expand All @@ -54,6 +47,21 @@ where
{
}

// impl DeclEngineGet<DeclId<ty::TyFunctionDeclaration>, ty::TyFunctionDeclaration> for DeclEngine {
// fn get(&self, index: &DeclId<ty::TyFunctionDeclaration>) -> ty::TyFunctionDeclaration {
// self.function_slab.get(index.inner())
// }
// }

// impl DeclEngineGet<DeclRef<DeclId<ty::TyFunctionDeclaration>>, ty::TyFunctionDeclaration>
// for DeclEngine
// {
// fn get(&self, index: &DeclRef<DeclId<ty::TyFunctionDeclaration>>) -> ty::TyFunctionDeclaration {
// let decl = self.function_slab.get(index.id().inner());
// todo!()
// }
// }

macro_rules! decl_engine_get {
($slab:ident, $decl:ty) => {
impl DeclEngineGet<DeclId<$decl>, $decl> for DeclEngine {
Expand Down Expand Up @@ -83,11 +91,12 @@ decl_engine_get!(type_alias_slab, ty::TyTypeAliasDecl);
macro_rules! decl_engine_insert {
($slab:ident, $decl:ty) => {
impl DeclEngineInsert<$decl> for DeclEngine {
fn insert(&self, decl: $decl) -> DeclRef<DeclId<$decl>> {
fn insert(&self, decl: $decl, subst_list: SubstList) -> DeclRef<DeclId<$decl>> {
let span = decl.span();
DeclRef::new(
decl.name().clone(),
DeclId::new(self.$slab.insert(decl)),
subst_list,
span,
)
}
Expand Down Expand Up @@ -142,63 +151,6 @@ decl_engine_index!(enum_slab, ty::TyEnumDecl);
decl_engine_index!(type_alias_slab, ty::TyTypeAliasDecl);

impl DeclEngine {
/// Given a [DeclRef] `index`, finds all the parents of `index` and all the
/// recursive parents of those parents, and so on. Does not perform
/// duplicated computation---if the parents of a [DeclRef] have already been
/// found, we do not find them again.
#[allow(clippy::map_entry)]
pub(crate) fn find_all_parents<'a, T>(
&self,
engines: Engines<'_>,
index: &'a T,
) -> Vec<FunctionalDeclId>
where
FunctionalDeclId: From<&'a T>,
{
let index: FunctionalDeclId = FunctionalDeclId::from(index);
let parents = self.parents.read().unwrap();
let mut acc_parents: HashMap<FunctionalDeclId, FunctionalDeclId> = HashMap::new();
let mut already_checked: HashSet<FunctionalDeclId> = HashSet::new();
let mut left_to_check: VecDeque<FunctionalDeclId> = VecDeque::from([index]);
while let Some(curr) = left_to_check.pop_front() {
if !already_checked.insert(curr.clone()) {
continue;
}
if let Some(curr_parents) = parents.get(&curr) {
for curr_parent in curr_parents.iter() {
if !acc_parents.contains_key(curr_parent) {
acc_parents.insert(curr_parent.clone(), curr_parent.clone());
}
if !left_to_check.iter().any(|x| match (x, curr_parent) {
(
FunctionalDeclId::TraitFn(x_id),
FunctionalDeclId::TraitFn(curr_parent_id),
) => self.get(x_id).eq(&self.get(curr_parent_id), engines),
(
FunctionalDeclId::Function(x_id),
FunctionalDeclId::Function(curr_parent_id),
) => self.get(x_id).eq(&self.get(curr_parent_id), engines),
_ => false,
}) {
left_to_check.push_back(curr_parent.clone());
}
}
}
}
acc_parents.values().cloned().collect()
}

pub(crate) fn register_parent<I>(&self, index: FunctionalDeclId, parent: FunctionalDeclId)
where
FunctionalDeclId: From<DeclId<I>>,
{
let mut parents = self.parents.write().unwrap();
parents
.entry(index)
.and_modify(|e| e.push(parent.clone()))
.or_insert_with(|| vec![parent]);
}

/// Friendly helper method for calling the `get` method from the
/// implementation of [DeclEngineGet] for [DeclEngine]
///
Expand Down
1 change: 1 addition & 0 deletions sway-core/src/decl_engine/functional_decl_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ impl TryFrom<DeclRefMixedFunctional> for DeclRefFunction {
FunctionalDeclId::Function(id) => Ok(DeclRef::new(
value.name().clone(),
id,
value.subst_list().clone(),
value.decl_span().clone(),
)),
actually @ FunctionalDeclId::TraitFn(_) => Err(CompileError::DeclIsNotAFunction {
Expand Down
128 changes: 0 additions & 128 deletions sway-core/src/decl_engine/id.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
use std::marker::PhantomData;
use std::{fmt, hash::Hash};

use crate::{
decl_engine::*,
engine_threading::*,
language::ty::{
TyEnumDecl, TyFunctionDecl, TyImplTrait, TyStructDecl, TyTraitDecl, TyTraitFn,
TyTypeAliasDecl,
},
type_system::*,
};

/// An ID used to refer to an item in the [DeclEngine](super::decl_engine::DeclEngine)
pub struct DeclId<T>(usize, PhantomData<T>);

Expand Down Expand Up @@ -59,10 +49,6 @@ impl<T> DeclId<T> {
pub(crate) fn new(id: usize) -> Self {
DeclId(id, PhantomData)
}

pub(crate) fn replace_id(&mut self, index: Self) {
self.0 = index.0;
}
}

#[allow(clippy::from_over_into)]
Expand All @@ -71,117 +57,3 @@ impl<T> Into<usize> for DeclId<T> {
self.0
}
}

impl SubstTypes for DeclId<TyFunctionDecl> {
fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: Engines<'_>) {
let decl_engine = engines.de();
let mut decl = decl_engine.get(self);
decl.subst(type_mapping, engines);
decl_engine.replace(*self, decl);
}
}
impl SubstTypes for DeclId<TyTraitDecl> {
fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: Engines<'_>) {
let decl_engine = engines.de();
let mut decl = decl_engine.get(self);
decl.subst(type_mapping, engines);
decl_engine.replace(*self, decl);
}
}
impl SubstTypes for DeclId<TyTraitFn> {
fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: Engines<'_>) {
let decl_engine = engines.de();
let mut decl = decl_engine.get(self);
decl.subst(type_mapping, engines);
decl_engine.replace(*self, decl);
}
}
impl SubstTypes for DeclId<TyImplTrait> {
fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: Engines<'_>) {
let decl_engine = engines.de();
let mut decl = decl_engine.get(self);
decl.subst(type_mapping, engines);
decl_engine.replace(*self, decl);
}
}
impl SubstTypes for DeclId<TyStructDecl> {
fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: Engines<'_>) {
let decl_engine = engines.de();
let mut decl = decl_engine.get(self);
decl.subst(type_mapping, engines);
decl_engine.replace(*self, decl);
}
}
impl SubstTypes for DeclId<TyEnumDecl> {
fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: Engines<'_>) {
let decl_engine = engines.de();
let mut decl = decl_engine.get(self);
decl.subst(type_mapping, engines);
decl_engine.replace(*self, decl);
}
}
impl SubstTypes for DeclId<TyTypeAliasDecl> {
fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: Engines<'_>) {
let decl_engine = engines.de();
let mut decl = decl_engine.get(self);
decl.subst(type_mapping, engines);
decl_engine.replace(*self, decl);
}
}

impl ReplaceSelfType for DeclId<TyFunctionDecl> {
fn replace_self_type(&mut self, engines: Engines<'_>, self_type: TypeId) {
let decl_engine = engines.de();
let mut decl = decl_engine.get(self);
decl.replace_self_type(engines, self_type);
decl_engine.replace(*self, decl);
}
}
impl ReplaceSelfType for DeclId<TyTraitDecl> {
fn replace_self_type(&mut self, engines: Engines<'_>, self_type: TypeId) {
let decl_engine = engines.de();
let mut decl = decl_engine.get(self);
decl.replace_self_type(engines, self_type);
decl_engine.replace(*self, decl);
}
}
impl ReplaceSelfType for DeclId<TyTraitFn> {
fn replace_self_type(&mut self, engines: Engines<'_>, self_type: TypeId) {
let decl_engine = engines.de();
let mut decl = decl_engine.get(self);
decl.replace_self_type(engines, self_type);
decl_engine.replace(*self, decl);
}
}
impl ReplaceSelfType for DeclId<TyImplTrait> {
fn replace_self_type(&mut self, engines: Engines<'_>, self_type: TypeId) {
let decl_engine = engines.de();
let mut decl = decl_engine.get(self);
decl.replace_self_type(engines, self_type);
decl_engine.replace(*self, decl);
}
}
impl ReplaceSelfType for DeclId<TyStructDecl> {
fn replace_self_type(&mut self, engines: Engines<'_>, self_type: TypeId) {
let decl_engine = engines.de();
let mut decl = decl_engine.get(self);
decl.replace_self_type(engines, self_type);
decl_engine.replace(*self, decl);
}
}
impl ReplaceSelfType for DeclId<TyEnumDecl> {
fn replace_self_type(&mut self, engines: Engines<'_>, self_type: TypeId) {
let decl_engine = engines.de();
let mut decl = decl_engine.get(self);
decl.replace_self_type(engines, self_type);
decl_engine.replace(*self, decl);
}
}
impl ReplaceSelfType for DeclId<TyTypeAliasDecl> {
fn replace_self_type(&mut self, engines: Engines<'_>, self_type: TypeId) {
let decl_engine = engines.de();
let mut decl = decl_engine.get(self);
decl.replace_self_type(engines, self_type);
decl_engine.replace(*self, decl);
}
}
Loading