Skip to content

Commit

Permalink
Unify namespace imports.
Browse files Browse the repository at this point in the history
  • Loading branch information
tritao committed Jun 6, 2024
1 parent e1fcf98 commit 0507e78
Show file tree
Hide file tree
Showing 7 changed files with 238 additions and 66 deletions.
25 changes: 25 additions & 0 deletions sway-core/src/decl_engine/parsed_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,31 @@ decl_engine_insert!(enum_slab, EnumDeclaration);
decl_engine_insert!(enum_variant_slab, EnumVariant);
decl_engine_insert!(type_alias_slab, TypeAliasDeclaration);

macro_rules! decl_engine_replace {
($slab:ident, $decl:ty) => {
impl ParsedDeclEngineReplace<$decl> for ParsedDeclEngine {
fn replace(&self, index: ParsedDeclId<$decl>, decl: $decl) {
self.$slab.replace(index.inner(), decl);
}
}
};
}

decl_engine_replace!(variable_slab, VariableDeclaration);
decl_engine_replace!(function_slab, FunctionDeclaration);
decl_engine_replace!(trait_slab, TraitDeclaration);
decl_engine_replace!(trait_fn_slab, TraitFn);
decl_engine_replace!(trait_type_slab, TraitTypeDeclaration);
decl_engine_replace!(impl_trait_slab, ImplTrait);
decl_engine_replace!(impl_self_slab, ImplSelf);
decl_engine_replace!(struct_slab, StructDeclaration);
decl_engine_replace!(storage_slab, StorageDeclaration);
decl_engine_replace!(abi_slab, AbiDeclaration);
decl_engine_replace!(constant_slab, ConstantDeclaration);
decl_engine_replace!(enum_slab, EnumDeclaration);
decl_engine_replace!(enum_variant_slab, EnumVariant);
decl_engine_replace!(type_alias_slab, TypeAliasDeclaration);

macro_rules! decl_engine_clear {
($($slab:ident, $decl:ty);* $(;)?) => {
impl ParsedDeclEngine {
Expand Down
6 changes: 4 additions & 2 deletions sway-core/src/language/parsed/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ impl Declaration {
}
Declaration::StructDeclaration(decl_id) => decl_engine.get_struct(decl_id).visibility,
Declaration::EnumDeclaration(decl_id) => decl_engine.get_enum(decl_id).visibility,
Declaration::EnumVariantDeclaration(decl) => {
decl_engine.get_enum(&decl.enum_ref).visibility
}
Declaration::FunctionDeclaration(decl_id) => {
decl_engine.get_function(decl_id).visibility
}
Expand All @@ -131,8 +134,7 @@ impl Declaration {
| Declaration::ImplSelf(_)
| Declaration::StorageDeclaration(_)
| Declaration::AbiDeclaration(_)
| Declaration::TraitTypeDeclaration(_)
| Declaration::EnumVariantDeclaration(_) => Visibility::Public,
| Declaration::TraitTypeDeclaration(_) => Visibility::Public,
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions sway-core/src/semantic_analysis/ast_node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ fn handle_item_trait_imports(
let dst_mod = ctx.namespace.module(engines);

for (_, (_, src, decl)) in dst_mod.current_items().use_item_synonyms.iter() {
let decl = decl.expect_typed_ref();

let src_mod = root_mod.lookup_submodule(handler, engines, src)?;

// if this is an enum or struct or function, import its implementations
Expand Down
39 changes: 32 additions & 7 deletions sway-core/src/semantic_analysis/namespace/lexical_scope.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::{
decl_engine::{parsed_engine::ParsedDeclEngineGet, parsed_id::ParsedDeclId, *},
engine_threading::{Engines, PartialEqWithEngines, PartialEqWithEnginesContext},
engine_threading::{
DebugWithEngines, Engines, PartialEqWithEngines, PartialEqWithEnginesContext,
},
language::{
parsed::{Declaration, FunctionDeclaration},
ty::{self, StructAccessInfo, TyDecl, TyStorageDecl},
Expand All @@ -19,7 +21,7 @@ use sway_error::{
};
use sway_types::{span::Span, Spanned};

use std::sync::Arc;
use std::{fmt, sync::Arc};

pub enum ResolvedFunctionDecl {
Parsed(ParsedDeclId<FunctionDeclaration>),
Expand All @@ -37,8 +39,9 @@ impl ResolvedFunctionDecl {

pub(super) type SymbolMap = im::OrdMap<Ident, ResolvedDeclaration>;
type SourceIdent = Ident;
pub(super) type GlobSynonyms = im::HashMap<Ident, Vec<(ModulePathBuf, ty::TyDecl)>>;
pub(super) type ItemSynonyms = im::HashMap<Ident, (Option<SourceIdent>, ModulePathBuf, ty::TyDecl)>;
pub(super) type GlobSynonyms = im::HashMap<Ident, Vec<(ModulePathBuf, ResolvedDeclaration)>>;
pub(super) type ItemSynonyms =
im::HashMap<Ident, (Option<SourceIdent>, ModulePathBuf, ResolvedDeclaration)>;

/// Represents a lexical scope integer-based identifier, which can be used to reference
/// specific a lexical scope.
Expand All @@ -60,6 +63,28 @@ pub struct LexicalScope {
pub parent: Option<LexicalScopeId>,
}

impl DebugWithEngines for LexicalScope {
fn fmt(&self, f: &mut fmt::Formatter<'_>, engines: &Engines) -> fmt::Result {
let _ = f.write_str("{\n");
let items_fmt = self.items.fmt(f, engines);
let _ = f.write_fmt(format_args!(" items: {:?}\n", items_fmt));
let _ = f.write_fmt(format_args!(" children: {:?}\n", self.children));
let _ = f.write_fmt(format_args!(" parent: {:?}\n", self.parent));
f.write_str("}\n")
}
}

impl DebugWithEngines for Items {
fn fmt(&self, f: &mut fmt::Formatter<'_>, _engines: &Engines) -> fmt::Result {
let _ = f.write_str("{\n");
let _ = f.write_str("parsed_symbols:\n");
for (name, decl) in self.symbols.iter() {
let _ = f.write_fmt(format_args!("{:?}, {:?}\n", name, decl));
}
f.write_str("}\n")
}
}

/// The set of items that exist within some lexical scope via declaration or importing.
#[derive(Clone, Debug, Default)]
pub struct Items {
Expand Down Expand Up @@ -469,12 +494,12 @@ impl Items {
if let Some((ident, (imported_ident, _, decl))) =
self.use_item_synonyms.get_key_value(&name)
{
append_shadowing_error_typed(
append_shadowing_error(
ident,
decl,
true,
imported_ident.is_some(),
item.expect_typed_ref(),
&item,
const_shadowing_mode,
);
}
Expand All @@ -495,7 +520,7 @@ impl Items {
engines: &Engines,
symbol: Ident,
src_path: ModulePathBuf,
decl: &ty::TyDecl,
decl: &ResolvedDeclaration,
) {
if let Some(cur_decls) = self.use_glob_synonyms.get_mut(&symbol) {
// Name already bound. Check if the decl is already imported
Expand Down
22 changes: 20 additions & 2 deletions sway-core/src/semantic_analysis/namespace/module.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use crate::{engine_threading::Engines, language::Visibility, Ident};
use crate::{
engine_threading::{DebugWithEngines, Engines},
language::Visibility,
Ident,
};

use super::{
lexical_scope::{Items, LexicalScope},
Expand All @@ -7,7 +11,7 @@ use super::{
};

use rustc_hash::FxHasher;
use std::hash::BuildHasherDefault;
use std::{fmt, hash::BuildHasherDefault};
use sway_error::handler::Handler;
use sway_error::{error::CompileError, handler::ErrorEmitted};
use sway_types::{span::Span, Spanned};
Expand Down Expand Up @@ -51,6 +55,20 @@ pub struct Module {
pub(crate) mod_path: ModulePathBuf,
}

impl DebugWithEngines for Module {
fn fmt(&self, f: &mut fmt::Formatter<'_>, engines: &Engines) -> fmt::Result {
let _ = f.write_str("{\n");
let _ = f.write_str(" modules:\n");
for (name, module) in self.submodules.iter() {
let _ = f.write_fmt(format_args!("{}", name));
let _ = module.fmt(f, engines);
}
let _ = f.write_str(" lexical scopes:\n");
let _ = self.lexical_scopes.fmt(f, engines);
f.write_str("}\n")
}
}

impl Default for Module {
fn default() -> Self {
Self {
Expand Down
9 changes: 9 additions & 0 deletions sway-core/src/semantic_analysis/namespace/namespace.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::fmt;

use crate::{
engine_threading::DebugWithEngines,
language::{ty, CallPath, Visibility},
Engines, Ident, TypeId,
};
Expand Down Expand Up @@ -46,6 +49,12 @@ pub struct Namespace {
pub(crate) mod_path: ModulePathBuf,
}

impl DebugWithEngines for Namespace {
fn fmt(&self, f: &mut fmt::Formatter<'_>, engines: &Engines) -> fmt::Result {
self.root.module.fmt(f, engines)
}
}

impl Namespace {
pub fn program_id(&self, engines: &Engines) -> &Module {
self.root
Expand Down
Loading

0 comments on commit 0507e78

Please sign in to comment.