Skip to content

Commit

Permalink
Collect nodes inside each function body and keep a reference to their…
Browse files Browse the repository at this point in the history
… lexical scope.
  • Loading branch information
tritao committed Jul 29, 2024
1 parent dde14c0 commit 9f763b9
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 6 deletions.
2 changes: 2 additions & 0 deletions sway-core/src/language/parsed/declaration/function.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{
engine_threading::*,
language::{parsed::*, *},
namespace::LexicalScopeId,
transform::{self, AttributeKind},
type_system::*,
};
Expand Down Expand Up @@ -28,6 +29,7 @@ pub struct FunctionDeclaration {
pub where_clause: Vec<(Ident, Vec<TraitConstraint>)>,
pub kind: FunctionDeclarationKind,
pub implementing_type: Option<Declaration>,
pub lexical_scope: LexicalScopeId,
}

impl EqWithEngines for FunctionDeclaration {}
Expand Down
16 changes: 16 additions & 0 deletions sway-core/src/semantic_analysis/ast_node/code_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,22 @@ use crate::language::{
};

impl ty::TyCodeBlock {
pub(crate) fn collect(
handler: &Handler,
engines: &Engines,
ctx: &mut SymbolCollectionContext,
code_block: &CodeBlock,
) -> Result<(), ErrorEmitted> {
let _ = code_block
.contents
.iter()
.map(|node| ty::TyAstNode::collect(handler, engines, ctx, node))
.filter_map(|res| res.ok())
.collect::<Vec<_>>();

Ok(())
}

pub(crate) fn type_check(
handler: &Handler,
ctx: TypeCheckContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ use sway_error::handler::{ErrorEmitted, Handler};
use sway_types::{Ident, Named, Spanned};

use crate::{
decl_engine::{DeclEngineGet, DeclEngineInsert, DeclRef, ReplaceFunctionImplementingType},
decl_engine::{
parsed_engine::ParsedDeclEngineReplace, DeclEngineGet, DeclEngineInsert, DeclRef,
ReplaceFunctionImplementingType,
},
language::{
parsed::{self, StorageEntry},
ty::{self, FunctionDecl, TyDecl, TyStorageField},
ty::{self, FunctionDecl, TyCodeBlock, TyDecl, TyStorageField},
CallPath,
},
namespace::{IsExtendingExistingImpl, IsImplSelf},
Expand Down Expand Up @@ -53,8 +56,14 @@ impl TyDecl {
}
parsed::Declaration::EnumVariantDeclaration(_decl) => {}
parsed::Declaration::FunctionDeclaration(decl_id) => {
let fn_decl = engines.pe().get_function(decl_id).as_ref().clone();
let decl_id = *decl_id;
let mut fn_decl = engines.pe().get_function(&decl_id).as_ref().clone();
let _ = ctx.insert_parsed_symbol(handler, engines, fn_decl.name.clone(), decl);
let (_ret, lexical_scope_id) = ctx.scoped(engines, |scoped_ctx| {
TyCodeBlock::collect(handler, engines, scoped_ctx, &fn_decl.body)
});
fn_decl.lexical_scope = lexical_scope_id;
engines.pe().replace(decl_id, fn_decl);
}
parsed::Declaration::TraitDeclaration(decl_id) => {
let trait_decl = engines.pe().get_trait(decl_id).as_ref().clone();
Expand Down
8 changes: 5 additions & 3 deletions sway-core/src/semantic_analysis/symbol_collection_context.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{
language::{parsed::Declaration, Visibility},
namespace::LexicalScopeId,
namespace::ModulePath,
semantic_analysis::Namespace,
Engines,
Expand Down Expand Up @@ -40,15 +41,16 @@ impl SymbolCollectionContext {
&mut self,
engines: &Engines,
with_scoped_ctx: impl FnOnce(&mut SymbolCollectionContext) -> Result<T, ErrorEmitted>,
) -> Result<T, ErrorEmitted> {
self.namespace
) -> (Result<T, ErrorEmitted>, LexicalScopeId) {
let lexical_scope_id: LexicalScopeId = self
.namespace
.module_mut(engines)
.write(engines, |m| m.push_new_lexical_scope());
let ret = with_scoped_ctx(self);
self.namespace
.module_mut(engines)
.write(engines, |m| m.pop_lexical_scope());
ret
(ret, lexical_scope_id)
}

/// Enter the lexical scope and produce a collection context ready for
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,7 @@ pub fn item_fn_to_function_declaration(
.unwrap_or(vec![]),
kind,
implementing_type,
lexical_scope: 0,
};
let decl_id = engines.pe().insert(fn_decl);
Ok(decl_id)
Expand Down

0 comments on commit 9f763b9

Please sign in to comment.