Skip to content

Commit

Permalink
More thorough declaration collection handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
tritao committed Sep 16, 2024
1 parent 4d6c573 commit 6a8f29c
Show file tree
Hide file tree
Showing 15 changed files with 256 additions and 103 deletions.
24 changes: 19 additions & 5 deletions sway-core/src/semantic_analysis/ast_node/declaration/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ use sway_error::error::CompileError;
use sway_types::{Ident, Named, Span, Spanned};

use crate::{
decl_engine::{DeclEngineGetParsedDeclId, DeclEngineInsert, DeclEngineInsertArc, DeclId},
decl_engine::{
parsed_id::ParsedDeclId, DeclEngineGetParsedDeclId, DeclEngineInsert, DeclEngineInsertArc,
DeclId,
},
language::ty::{TyAbiDecl, TyFunctionDecl},
namespace::{IsExtendingExistingImpl, IsImplSelf, TryInsertingTraitImplOnFailure},
semantic_analysis::{
Expand Down Expand Up @@ -33,12 +36,23 @@ impl ty::TyAbiDecl {
handler: &Handler,
engines: &Engines,
ctx: &mut SymbolCollectionContext,
abi_decl: &AbiDeclaration,
decl_id: &ParsedDeclId<AbiDeclaration>,
) -> Result<(), ErrorEmitted> {
let abi_decl = engines.pe().get_abi(decl_id);
ctx.insert_parsed_symbol(
handler,
engines,
abi_decl.name.clone(),
Declaration::AbiDeclaration(*decl_id),
)?;

let _ = ctx.scoped(engines, abi_decl.span.clone(), |scoped_ctx| {
abi_decl.methods.iter().for_each(|m| {
let method_decl = engines.pe().get_function(m).as_ref().clone();
let _ = TyFunctionDecl::collect(handler, engines, scoped_ctx, &method_decl);
abi_decl.interface_surface.iter().for_each(|item| {
let _ = TyTraitItem::collect(handler, engines, scoped_ctx, item);
});

abi_decl.methods.iter().for_each(|decl_id| {
let _ = TyFunctionDecl::collect(handler, engines, scoped_ctx, decl_id);
});
Ok(())
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,42 @@ use sway_error::{
warning::{CompileWarning, Warning},
};
use sway_types::{style::is_screaming_snake_case, Spanned};
use symbol_collection_context::SymbolCollectionContext;

use crate::{
decl_engine::{DeclEngineGetParsedDeclId, DeclEngineInsert, ReplaceDecls},
decl_engine::{
parsed_id::ParsedDeclId, DeclEngineGetParsedDeclId, DeclEngineInsert, ReplaceDecls,
},
language::{
parsed::*,
ty::{self, TyConfigurableDecl},
ty::{self, TyConfigurableDecl, TyExpression},
CallPath,
},
semantic_analysis::{type_check_context::EnforceTypeArguments, *},
SubstTypes, SubstTypesContext, TypeArgument, TypeBinding, TypeCheckTypeBinding, TypeInfo,
Engines, SubstTypes, SubstTypesContext, TypeArgument, TypeBinding, TypeCheckTypeBinding,
TypeInfo,
};

impl ty::TyConfigurableDecl {
pub(crate) fn collect(
handler: &Handler,
engines: &Engines,
ctx: &mut SymbolCollectionContext,
decl_id: &ParsedDeclId<ConfigurableDeclaration>,
) -> Result<(), ErrorEmitted> {
let configurable_decl = engines.pe().get_configurable(decl_id);
ctx.insert_parsed_symbol(
handler,
engines,
configurable_decl.name.clone(),
Declaration::ConfigurableDeclaration(*decl_id),
)?;
if let Some(value) = &configurable_decl.value {
TyExpression::collect(handler, engines, ctx, value)?;
}
Ok(())
}

pub fn type_check(
handler: &Handler,
mut ctx: TypeCheckContext,
Expand Down
23 changes: 22 additions & 1 deletion sway-core/src/semantic_analysis/ast_node/declaration/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,39 @@ use sway_error::{
warning::{CompileWarning, Warning},
};
use sway_types::{style::is_screaming_snake_case, Spanned};
use symbol_collection_context::SymbolCollectionContext;

use crate::{
decl_engine::parsed_id::ParsedDeclId,
language::{
parsed::{self, *},
ty::{self, TyConstantDecl},
ty::{self, TyConstantDecl, TyExpression},
CallPath,
},
semantic_analysis::{type_check_context::EnforceTypeArguments, *},
Engines, SubstTypes, SubstTypesContext, TypeInfo,
};

impl ty::TyConstantDecl {
pub(crate) fn collect(
handler: &Handler,
engines: &Engines,
ctx: &mut SymbolCollectionContext,
decl_id: &ParsedDeclId<ConstantDeclaration>,
) -> Result<(), ErrorEmitted> {
let constant_decl = engines.pe().get_constant(decl_id);
ctx.insert_parsed_symbol(
handler,
engines,
constant_decl.name.clone(),
Declaration::ConstantDeclaration(*decl_id),
)?;
if let Some(value) = &constant_decl.value {
TyExpression::collect(handler, engines, ctx, value)?;
}
Ok(())
}

pub fn type_check(
handler: &Handler,
mut ctx: TypeCheckContext,
Expand Down
60 changes: 16 additions & 44 deletions sway-core/src/semantic_analysis/ast_node/declaration/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ use crate::{
language::{
parsed::{self, StorageEntry},
ty::{
self, FunctionDecl, TyAbiDecl, TyDecl, TyEnumDecl, TyFunctionDecl, TyImplSelfOrTrait,
TyStorageField, TyStructDecl, TyTraitDecl, TyVariableDecl,
self, FunctionDecl, TyAbiDecl, TyConfigurableDecl, TyConstantDecl, TyDecl, TyEnumDecl,
TyFunctionDecl, TyImplSelfOrTrait, TyStorageDecl, TyStorageField, TyStructDecl,
TyTraitDecl, TyTraitFn, TyTraitType, TyTypeAliasDecl, TyVariableDecl,
},
CallPath,
},
Expand All @@ -31,73 +32,44 @@ impl TyDecl {
) -> Result<(), ErrorEmitted> {
match &decl {
parsed::Declaration::VariableDeclaration(decl_id) => {
let var_decl = engines.pe().get_variable(decl_id);
ctx.insert_parsed_symbol(handler, engines, var_decl.name.clone(), decl)?;
TyVariableDecl::collect(handler, engines, ctx, &var_decl)?
TyVariableDecl::collect(handler, engines, ctx, decl_id)?
}
parsed::Declaration::ConstantDeclaration(decl_id) => {
let const_decl = engines.pe().get_constant(decl_id).as_ref().clone();
ctx.insert_parsed_symbol(handler, engines, const_decl.name.clone(), decl)?;
TyConstantDecl::collect(handler, engines, ctx, decl_id)?
}
parsed::Declaration::ConfigurableDeclaration(decl_id) => {
let config_decl = engines.pe().get_configurable(decl_id).as_ref().clone();
ctx.insert_parsed_symbol(handler, engines, config_decl.name.clone(), decl)?;
TyConfigurableDecl::collect(handler, engines, ctx, decl_id)?
}
parsed::Declaration::TraitTypeDeclaration(decl_id) => {
let trait_type_decl = engines.pe().get_trait_type(decl_id).as_ref().clone();
ctx.insert_parsed_symbol(handler, engines, trait_type_decl.name.clone(), decl)?;
TyTraitType::collect(handler, engines, ctx, decl_id)?
}
parsed::Declaration::TraitFnDeclaration(decl_id) => {
let trait_fn_decl = engines.pe().get_trait_fn(decl_id).as_ref().clone();
ctx.insert_parsed_symbol(handler, engines, trait_fn_decl.name.clone(), decl)?;
TyTraitFn::collect(handler, engines, ctx, decl_id)?
}
parsed::Declaration::EnumDeclaration(decl_id) => {
let enum_decl = engines.pe().get_enum(decl_id).as_ref().clone();
ctx.insert_parsed_symbol(handler, engines, enum_decl.name.clone(), decl)?;
TyEnumDecl::collect(handler, engines, ctx, &enum_decl)?
TyEnumDecl::collect(handler, engines, ctx, decl_id)?
}
parsed::Declaration::EnumVariantDeclaration(_decl) => {}
parsed::Declaration::FunctionDeclaration(decl_id) => {
let fn_decl = engines.pe().get_function(decl_id).as_ref().clone();
let _ = ctx.insert_parsed_symbol(handler, engines, fn_decl.name.clone(), decl);
TyFunctionDecl::collect(handler, engines, ctx, &fn_decl)?
TyFunctionDecl::collect(handler, engines, ctx, decl_id)?
}
parsed::Declaration::TraitDeclaration(decl_id) => {
let trait_decl = engines.pe().get_trait(decl_id).as_ref().clone();
ctx.insert_parsed_symbol(handler, engines, trait_decl.name.clone(), decl)?;
TyTraitDecl::collect(handler, engines, ctx, &trait_decl)?
TyTraitDecl::collect(handler, engines, ctx, decl_id)?
}
parsed::Declaration::ImplSelfOrTrait(decl_id) => {
let impl_trait = engines
.pe()
.get_impl_self_or_trait(decl_id)
.as_ref()
.clone();
ctx.insert_parsed_symbol(
handler,
engines,
impl_trait.trait_name.suffix.clone(),
decl,
)?;
TyImplSelfOrTrait::collect(handler, engines, ctx, &impl_trait)?
TyImplSelfOrTrait::collect(handler, engines, ctx, decl_id)?
}
parsed::Declaration::StructDeclaration(decl_id) => {
let struct_decl = engines.pe().get_struct(decl_id).as_ref().clone();
ctx.insert_parsed_symbol(handler, engines, struct_decl.name.clone(), decl)?;
TyStructDecl::collect(handler, engines, ctx, &struct_decl)?
TyStructDecl::collect(handler, engines, ctx, decl_id)?
}
parsed::Declaration::AbiDeclaration(decl_id) => {
let abi_decl = engines.pe().get_abi(decl_id).as_ref().clone();
ctx.insert_parsed_symbol(handler, engines, abi_decl.name.clone(), decl)?;
TyAbiDecl::collect(handler, engines, ctx, &abi_decl)?
TyAbiDecl::collect(handler, engines, ctx, decl_id)?
}
parsed::Declaration::StorageDeclaration(decl_id) => {
let _storage_decl = engines.pe().get_storage(decl_id).as_ref().clone();
//ctx.insert_parsed_symbol(handler, storage_decl.name.clone(), decl)?;
TyStorageDecl::collect(handler, engines, ctx, decl_id)?
}
parsed::Declaration::TypeAliasDeclaration(decl_id) => {
let type_alias_decl = engines.pe().get_type_alias(decl_id).as_ref().clone();
ctx.insert_parsed_symbol(handler, engines, type_alias_decl.name, decl.clone())?;
TyTypeAliasDecl::collect(handler, engines, ctx, decl_id)?
}
};

Expand Down
15 changes: 12 additions & 3 deletions sway-core/src/semantic_analysis/ast_node/declaration/enum.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{
decl_engine::parsed_id::ParsedDeclId,
language::{parsed::*, ty, CallPath},
semantic_analysis::{type_check_context::EnforceTypeArguments, *},
type_system::*,
Expand All @@ -9,13 +10,21 @@ use symbol_collection_context::SymbolCollectionContext;

impl ty::TyEnumDecl {
pub(crate) fn collect(
_handler: &Handler,
handler: &Handler,
engines: &Engines,
ctx: &mut SymbolCollectionContext,
decl: &EnumDeclaration,
decl_id: &ParsedDeclId<EnumDeclaration>,
) -> Result<(), ErrorEmitted> {
let enum_decl = engines.pe().get_enum(decl_id);
ctx.insert_parsed_symbol(
handler,
engines,
enum_decl.name.clone(),
Declaration::EnumDeclaration(*decl_id),
)?;

// create a namespace for the decl, used to create a scope for generics
let _ = ctx.scoped(engines, decl.span.clone(), |mut _ctx| Ok(()));
let _ = ctx.scoped(engines, enum_decl.span.clone(), |mut _ctx| Ok(()));
Ok(())
}

Expand Down
12 changes: 10 additions & 2 deletions sway-core/src/semantic_analysis/ast_node/declaration/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use sway_error::{
use symbol_collection_context::SymbolCollectionContext;

use crate::{
decl_engine::{DeclId, DeclRefFunction},
decl_engine::{parsed_id::ParsedDeclId, DeclId, DeclRefFunction},
language::{
parsed::*,
ty::{self, TyCodeBlock, TyFunctionDecl},
Expand All @@ -25,8 +25,16 @@ impl ty::TyFunctionDecl {
handler: &Handler,
engines: &Engines,
ctx: &mut SymbolCollectionContext,
fn_decl: &FunctionDeclaration,
decl_id: &ParsedDeclId<FunctionDeclaration>,
) -> Result<(), ErrorEmitted> {
let fn_decl = engines.pe().get_function(decl_id);
let _ = ctx.insert_parsed_symbol(
handler,
engines,
fn_decl.name.clone(),
Declaration::FunctionDeclaration(*decl_id),
);

// create a namespace for the function
let _ = ctx.scoped(engines, fn_decl.span.clone(), |scoped_ctx| {
TyCodeBlock::collect(handler, engines, scoped_ctx, &fn_decl.body)
Expand Down
29 changes: 20 additions & 9 deletions sway-core/src/semantic_analysis/ast_node/declaration/impl_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use crate::{
language::{
parsed::*,
ty::{
self, TyDecl, TyFunctionDecl, TyImplItem, TyImplSelfOrTrait, TyTraitInterfaceItem,
TyTraitItem,
self, TyConstantDecl, TyDecl, TyFunctionDecl, TyImplItem, TyImplSelfOrTrait,
TyTraitInterfaceItem, TyTraitItem, TyTraitType,
},
*,
},
Expand All @@ -36,16 +36,27 @@ impl TyImplSelfOrTrait {
handler: &Handler,
engines: &Engines,
ctx: &mut SymbolCollectionContext,
decl: &ImplSelfOrTrait,
decl_id: &ParsedDeclId<ImplSelfOrTrait>,
) -> Result<(), ErrorEmitted> {
let _ = ctx.scoped(engines, decl.block_span.clone(), |scoped_ctx| {
decl.items.iter().for_each(|item| match item {
let impl_trait = engines.pe().get_impl_self_or_trait(decl_id);
ctx.insert_parsed_symbol(
handler,
engines,
impl_trait.trait_name.suffix.clone(),
Declaration::ImplSelfOrTrait(*decl_id),
)?;

let _ = ctx.scoped(engines, impl_trait.block_span.clone(), |scoped_ctx| {
impl_trait.items.iter().for_each(|item| match item {
ImplItem::Fn(decl_id) => {
let fn_decl = engines.pe().get_function(decl_id).as_ref().clone();
let _ = TyFunctionDecl::collect(handler, engines, scoped_ctx, &fn_decl);
let _ = TyFunctionDecl::collect(handler, engines, scoped_ctx, decl_id);
}
ImplItem::Constant(decl_id) => {
let _ = TyConstantDecl::collect(handler, engines, scoped_ctx, decl_id);
}
ImplItem::Type(decl_id) => {
let _ = TyTraitType::collect(handler, engines, scoped_ctx, decl_id);
}
ImplItem::Constant(_) => {}
ImplItem::Type(_) => {}
});
Ok(())
});
Expand Down
19 changes: 16 additions & 3 deletions sway-core/src/semantic_analysis/ast_node/declaration/storage.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
use std::collections::HashMap;

use crate::{
decl_engine::parsed_id::ParsedDeclId,
fuel_prelude::fuel_tx::StorageSlot,
ir_generation::{
const_eval::compile_constant_expression_to_constant,
storage::{get_storage_key_string, serialize_to_storage_slots},
},
language::ty::{self, TyExpression, TyStorageField},
language::{
parsed::StorageDeclaration,
ty::{self, TyExpression, TyStorageField},
},
metadata::MetadataManager,
semantic_analysis::{
TypeCheckAnalysis, TypeCheckAnalysisContext, TypeCheckFinalization,
TypeCheckFinalizationContext,
symbol_collection_context::SymbolCollectionContext, TypeCheckAnalysis,
TypeCheckAnalysisContext, TypeCheckFinalization, TypeCheckFinalizationContext,
},
Engines,
};
Expand All @@ -24,6 +28,15 @@ use sway_ir::{ConstantValue, Context, Module};
use sway_types::{u256::U256, Spanned};

impl ty::TyStorageDecl {
pub(crate) fn collect(
_handler: &Handler,
_engines: &Engines,
_ctx: &mut SymbolCollectionContext,
_decl_id: &ParsedDeclId<StorageDeclaration>,
) -> Result<(), ErrorEmitted> {
Ok(())
}

pub(crate) fn get_initialized_storage_slots(
&self,
handler: &Handler,
Expand Down
Loading

0 comments on commit 6a8f29c

Please sign in to comment.