Skip to content

Commit

Permalink
Unify parsed and typed symbols namespace maps. (#6070)
Browse files Browse the repository at this point in the history
## Description

As title PR, this unifies the different maps we had to represent both
parsed and typed declaration for namespace module symbols.

This simplifies all the code that handles symbols since there is a
single map to represent symbols at all stages in the compiler.

I tried moving all shadowing-related diagnostics to be run on the parsed
declarations but it will only possible to do until the other PR for full
collection/resolving is done. At the moment it leads to out of order
diagnostics which breaks the test. So just left a TODO and will move
those over in a separate PR once its possible to do so.

Depends on #6062.

## Checklist

- [ ] I have linked to any relevant issues.
- [ ] I have commented my code, particularly in hard-to-understand
areas.
- [ ] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [ ] I have added tests that prove my fix is effective or that my
feature works.
- [ ] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [ ] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [ ] I have requested a review from the relevant team or maintainers.

---------

Co-authored-by: IGI-111 <igi-111@protonmail.com>
Co-authored-by: Joshua Batty <joshpbatty@gmail.com>
  • Loading branch information
3 people authored May 31, 2024
1 parent d0ef108 commit a34950b
Show file tree
Hide file tree
Showing 11 changed files with 338 additions and 70 deletions.
38 changes: 20 additions & 18 deletions sway-core/src/ir_generation/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,24 +202,26 @@ pub(crate) fn compile_constants(
module_ns: &namespace::Module,
) -> Result<(), CompileError> {
for decl_name in module_ns.current_items().get_all_declared_symbols() {
if let Some(ty::TyDecl::ConstantDecl(ty::ConstantDecl { decl_id, .. })) =
module_ns.current_items().symbols.get(decl_name)
{
let const_decl = engines.de().get_constant(decl_id);
let call_path = const_decl.call_path.clone();
compile_const_decl(
&mut LookupEnv {
engines,
context,
md_mgr,
module,
module_ns: Some(module_ns),
function_compiler: None,
lookup: compile_const_decl,
},
&call_path,
&Some((*const_decl).clone()),
)?;
if let Some(resolved_decl) = module_ns.current_items().symbols.get(decl_name) {
if let ty::TyDecl::ConstantDecl(ty::ConstantDecl { decl_id, .. }) =
&resolved_decl.expect_typed_ref()
{
let const_decl = engines.de().get_constant(decl_id);
let call_path = const_decl.call_path.clone();
compile_const_decl(
&mut LookupEnv {
engines,
context,
md_mgr,
module,
module_ns: Some(module_ns),
function_compiler: None,
lookup: compile_const_decl,
},
&call_path,
&Some((*const_decl).clone()),
)?;
}
}
}

Expand Down
33 changes: 30 additions & 3 deletions sway-core/src/language/parsed/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,15 @@ pub use type_alias::*;
pub use variable::*;

use crate::{
decl_engine::{parsed_engine::ParsedDeclEngineGet, parsed_id::ParsedDeclId},
decl_engine::{
parsed_engine::{ParsedDeclEngine, ParsedDeclEngineGet},
parsed_id::ParsedDeclId,
},
engine_threading::{
DebugWithEngines, DisplayWithEngines, EqWithEngines, PartialEqWithEngines,
PartialEqWithEnginesContext,
},
language::Visibility,
Engines,
};

Expand Down Expand Up @@ -88,8 +92,7 @@ impl Declaration {
}
}

#[allow(dead_code)]
fn span(&self, engines: &Engines) -> sway_types::Span {
pub fn span(&self, engines: &Engines) -> sway_types::Span {
use Declaration::*;
let pe = engines.pe();
match self {
Expand All @@ -108,6 +111,30 @@ impl Declaration {
TraitTypeDeclaration(decl_id) => pe.get_trait_type(decl_id).span(),
}
}

pub(crate) fn visibility(&self, decl_engine: &ParsedDeclEngine) -> Visibility {
match self {
Declaration::TraitDeclaration(decl_id) => decl_engine.get_trait(decl_id).visibility,
Declaration::ConstantDeclaration(decl_id) => {
decl_engine.get_constant(decl_id).visibility
}
Declaration::StructDeclaration(decl_id) => decl_engine.get_struct(decl_id).visibility,
Declaration::EnumDeclaration(decl_id) => decl_engine.get_enum(decl_id).visibility,
Declaration::FunctionDeclaration(decl_id) => {
decl_engine.get_function(decl_id).visibility
}
Declaration::TypeAliasDeclaration(decl_id) => {
decl_engine.get_type_alias(decl_id).visibility
}
Declaration::VariableDeclaration(_decl_id) => Visibility::Private,
Declaration::ImplTrait(_)
| Declaration::ImplSelf(_)
| Declaration::StorageDeclaration(_)
| Declaration::AbiDeclaration(_)
| Declaration::TraitTypeDeclaration(_)
| Declaration::EnumVariantDeclaration(_) => Visibility::Public,
}
}
}

impl DisplayWithEngines for Declaration {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ impl TyDecl {
if let ty::TyTraitItem::Fn(f) = i {
let decl = engines.de().get(f.id());
let _ = ctx.namespace.module_mut(ctx.engines()).write(engines, |m| {
m.current_items_mut().insert_symbol(
m.current_items_mut().insert_typed_symbol(
handler,
engines,
Ident::new_no_span(format!(
Expand Down
29 changes: 25 additions & 4 deletions sway-core/src/semantic_analysis/collection_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,32 @@ use crate::{
use sway_error::handler::{ErrorEmitted, Handler};
use sway_types::{span::Span, Ident};

use super::{ConstShadowingMode, GenericShadowingMode};

#[derive(Clone)]
/// Contextual state tracked and accumulated throughout symbol collecting.
pub struct SymbolCollectionContext {
/// The namespace context accumulated throughout symbol collecting.
pub(crate) namespace: Namespace,

/// Whether or not a const declaration shadows previous const declarations sequentially.
///
/// This is `Sequential` while checking const declarations in functions, otherwise `ItemStyle`.
const_shadowing_mode: ConstShadowingMode,
/// Whether or not a generic type parameters shadows previous generic type parameters.
///
/// This is `Disallow` everywhere except while checking type parameters bounds in struct instantiation.
generic_shadowing_mode: GenericShadowingMode,
}

impl SymbolCollectionContext {
/// Initialize a context at the top-level of a module with its namespace.
pub fn new(namespace: Namespace) -> Self {
Self { namespace }
Self {
namespace,
const_shadowing_mode: ConstShadowingMode::ItemStyle,
generic_shadowing_mode: GenericShadowingMode::Disallow,
}
}

/// Scope the `CollectionContext` with a new lexical scope.
Expand Down Expand Up @@ -59,14 +74,20 @@ impl SymbolCollectionContext {
/// Short-hand for calling [Items::insert_parsed_symbol].
pub(crate) fn insert_parsed_symbol(
&mut self,
_handler: &Handler,
handler: &Handler,
engines: &Engines,
name: Ident,
item: Declaration,
) -> Result<(), ErrorEmitted> {
self.namespace.module_mut(engines).write(engines, |m| {
m.current_items_mut()
.insert_parsed_symbol(name.clone(), item.clone())
m.current_items_mut().insert_parsed_symbol(
handler,
engines,
name.clone(),
item.clone(),
self.const_shadowing_mode,
self.generic_shadowing_mode,
)
})
}
}
4 changes: 2 additions & 2 deletions sway-core/src/semantic_analysis/namespace/contract_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{
Engines, Namespace,
};

use super::{lexical_scope::SymbolMap, Module, Root};
use super::{lexical_scope::SymbolMap, root::ResolvedDeclaration, Module, Root};

/// `contract_id_value` is injected here via forc-pkg when producing the `dependency_namespace` for a contract which has tests enabled.
/// This allows us to provide a contract's `CONTRACT_ID` constant to its own unit tests.
Expand Down Expand Up @@ -117,7 +117,7 @@ fn default_with_contract_id_inner(
);
}
};
compiled_constants.insert(name, typed_decl);
compiled_constants.insert(name, ResolvedDeclaration::Typed(typed_decl));

let mut ret = Module::default();
ret.current_lexical_scope_mut().items.symbols = compiled_constants;
Expand Down
Loading

0 comments on commit a34950b

Please sign in to comment.