-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce the symbol collection context and a symbol collection step. (…
…#5661) ## Description This PR introduces a new symbol collection step to the compiler pipeline, that is done after parsing but before type checking. It collects identifiers for the top level entities in the AST and allows referencing through a parsed declaration id. These will be used in a subsequent PR that introduces a new AST order resolving pass that runs before type checking, thus allowing us to get rid of the deferred monormophization "hack", simplifying some things and eliminating some bugs. ## Checklist - [x] I have linked to any relevant issues. - [x] I have commented my code, particularly in hard-to-understand areas. - [x] I have updated the documentation where relevant (API docs, the reference, and the Sway book). - [x] I have added tests that prove my fix is effective or that my feature works. - [x] I have added (or requested a maintainer to add) the necessary `Breaking*` or `New Feature` labels where relevant. - [x] 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). - [x] I have requested a review from the relevant team or maintainers.
- Loading branch information
Showing
11 changed files
with
306 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
use crate::{ | ||
language::{parsed::Declaration, Visibility}, | ||
semantic_analysis::Namespace, | ||
}; | ||
use sway_error::handler::{ErrorEmitted, Handler}; | ||
use sway_types::{span::Span, Ident}; | ||
|
||
#[derive(Clone)] | ||
/// Contextual state tracked and accumulated throughout symbol collecting. | ||
pub struct SymbolCollectionContext { | ||
/// The namespace context accumulated throughout symbol collecting. | ||
pub(crate) namespace: Namespace, | ||
} | ||
|
||
impl SymbolCollectionContext { | ||
/// Initialize a context at the top-level of a module with its namespace. | ||
pub fn new(namespace: Namespace) -> Self { | ||
Self { namespace } | ||
} | ||
|
||
/// Scope the `CollectionContext` with a new lexical scope. | ||
pub fn scoped<T>( | ||
mut self, | ||
with_scoped_ctx: impl FnOnce(SymbolCollectionContext) -> Result<T, ErrorEmitted>, | ||
) -> Result<T, ErrorEmitted> { | ||
self.namespace.module_mut().push_new_lexical_scope(); | ||
let ret = with_scoped_ctx(self.clone()); | ||
self.namespace.module_mut().pop_lexical_scope(); | ||
ret | ||
} | ||
|
||
/// Enter the submodule with the given name and produce a collection context ready for | ||
/// collecting its content. | ||
/// | ||
/// Returns the result of the given `with_submod_ctx` function. | ||
pub fn enter_submodule<T>( | ||
&mut self, | ||
mod_name: Ident, | ||
visibility: Visibility, | ||
module_span: Span, | ||
with_submod_ctx: impl FnOnce(&mut SymbolCollectionContext) -> T, | ||
) -> T { | ||
self.namespace | ||
.push_new_submodule(mod_name, visibility, module_span); | ||
//let Self { namespace, .. } = self; | ||
//let mut submod_ns = namespace.enter_submodule(mod_name, visibility, module_span); | ||
let ret = with_submod_ctx(self); | ||
self.namespace.pop_submodule(); | ||
ret | ||
} | ||
|
||
/// Short-hand for calling [Items::insert_parsed_symbol]. | ||
pub(crate) fn insert_parsed_symbol( | ||
&mut self, | ||
_handler: &Handler, | ||
name: Ident, | ||
item: Declaration, | ||
) -> Result<(), ErrorEmitted> { | ||
self.namespace | ||
.module_mut() | ||
.current_items_mut() | ||
.insert_parsed_symbol(name, item) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.