Skip to content

Commit

Permalink
Add symbol resolve context.
Browse files Browse the repository at this point in the history
  • Loading branch information
tritao committed May 20, 2024
1 parent 3ed139f commit e2578a1
Show file tree
Hide file tree
Showing 26 changed files with 2,093 additions and 70 deletions.
28 changes: 27 additions & 1 deletion sway-core/src/decl_engine/interface_decl_id.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,30 @@
use crate::{decl_engine::*, language::ty};
use crate::{
decl_engine::*,
language::{
parsed::{AbiDeclaration, TraitDeclaration},
ty,
},
};

use super::parsed_id::ParsedDeclId;

#[derive(Debug, Eq, PartialEq, Hash, Clone)]
pub enum ParsedInterfaceDeclId {
Abi(ParsedDeclId<AbiDeclaration>),
Trait(ParsedDeclId<TraitDeclaration>),
}

impl From<ParsedDeclId<AbiDeclaration>> for ParsedInterfaceDeclId {
fn from(id: ParsedDeclId<AbiDeclaration>) -> Self {
Self::Abi(id)
}
}

impl From<ParsedDeclId<TraitDeclaration>> for ParsedInterfaceDeclId {
fn from(id: ParsedDeclId<TraitDeclaration>) -> Self {
Self::Trait(id)
}
}

#[derive(Debug, Eq, PartialEq, Hash, Clone)]
pub enum InterfaceDeclId {
Expand Down
24 changes: 24 additions & 0 deletions sway-core/src/decl_engine/parsed_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,30 @@ decl_engine_insert!(constant_slab, ConstantDeclaration);
decl_engine_insert!(enum_slab, EnumDeclaration);
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!(type_alias_slab, TypeAliasDeclaration);

macro_rules! decl_engine_clear {
($($slab:ident, $decl:ty);* $(;)?) => {
impl ParsedDeclEngine {
Expand Down
6 changes: 6 additions & 0 deletions sway-core/src/language/call_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,12 @@ pub struct CallPath<T = Ident> {
pub is_absolute: bool,
}

#[derive(Debug, Clone, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct ResolvedCallPath<T, U = Ident> {
pub decl: T,
pub unresolved_call_path: CallPath<U>,
}

impl std::convert::From<Ident> for CallPath {
fn from(other: Ident) -> Self {
CallPath {
Expand Down
62 changes: 61 additions & 1 deletion sway-core/src/language/parsed/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,21 @@ pub use r#enum::*;
pub use r#struct::*;
pub use r#trait::*;
pub use storage::*;
use sway_error::{
error::CompileError,
handler::{ErrorEmitted, Handler},
};
use sway_types::Spanned;
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},
language::Visibility,
Engines,
};

Expand Down Expand Up @@ -95,6 +103,58 @@ impl Declaration {
TraitTypeDeclaration(decl_id) => pe.get_trait_type(decl_id).span(),
}
}

pub(crate) fn to_fn_ref(
&self,
handler: &Handler,
engines: &Engines,
) -> Result<ParsedDeclId<FunctionDeclaration>, ErrorEmitted> {
match self {
Declaration::FunctionDeclaration(decl_id) => Ok(*decl_id),
decl => Err(handler.emit_err(CompileError::DeclIsNotAFunction {
actually: decl.friendly_type_name().to_string(),
span: decl.span(engines),
})),
}
}

pub(crate) fn to_struct_decl(
&self,
handler: &Handler,
engines: &Engines,
) -> Result<ParsedDeclId<StructDeclaration>, ErrorEmitted> {
match self {
Declaration::StructDeclaration(decl_id) => Ok(*decl_id),
decl => Err(handler.emit_err(CompileError::DeclIsNotAStruct {
actually: decl.friendly_type_name().to_string(),
span: decl.span(engines),
})),
}
}

#[allow(unused)]
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(_) => Visibility::Public,
}
}
}

impl DisplayWithEngines for Declaration {
Expand Down
1 change: 1 addition & 0 deletions sway-core/src/language/parsed/declaration/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub struct FunctionDeclaration {
pub type_parameters: Vec<TypeParameter>,
pub where_clause: Vec<(Ident, Vec<TraitConstraint>)>,
pub kind: FunctionDeclarationKind,
pub implementing_type: Option<Declaration>,
}

impl DebugWithEngines for FunctionDeclaration {
Expand Down
8 changes: 6 additions & 2 deletions sway-core/src/language/parsed/declaration/impl_trait.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use super::{ConstantDeclaration, FunctionDeclaration, TraitTypeDeclaration};
use crate::{
decl_engine::parsed_id::ParsedDeclId, engine_threading::DebugWithEngines, language::CallPath,
type_system::TypeArgument, Engines, TypeParameter,
decl_engine::{parsed_id::ParsedDeclId, ParsedInterfaceDeclId},
engine_threading::DebugWithEngines,
language::CallPath,
type_system::TypeArgument,
Engines, TypeParameter,
};

use sway_types::{span::Span, Named, Spanned};
Expand Down Expand Up @@ -47,6 +50,7 @@ pub struct ImplTrait {
pub impl_type_parameters: Vec<TypeParameter>,
pub trait_name: CallPath,
pub trait_type_arguments: Vec<TypeArgument>,
pub trait_decl_ref: Option<ParsedInterfaceDeclId>,
pub implementing_for: TypeArgument,
pub items: Vec<ImplItem>,
// the span of the whole impl trait and block
Expand Down
7 changes: 7 additions & 0 deletions sway-core/src/language/parsed/expression/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{cmp::Ordering, fmt, hash::Hasher};

use crate::{
decl_engine::parsed_id::ParsedDeclId,
engine_threading::{
DebugWithEngines, DisplayWithEngines, EqWithEngines, HashWithEngines, OrdWithEngines,
OrdWithEnginesContext, PartialEqWithEngines, PartialEqWithEnginesContext,
Expand All @@ -22,6 +23,8 @@ pub use method_name::MethodName;
pub use scrutinee::*;
use sway_ast::intrinsics::Intrinsic;

use super::{FunctionDeclaration, StructDeclaration};

/// Represents a parsed, but not yet type checked, [Expression](https://en.wikipedia.org/wiki/Expression_(computer_science)).
#[derive(Debug, Clone)]
pub struct Expression {
Expand All @@ -32,6 +35,8 @@ pub struct Expression {
#[derive(Debug, Clone)]
pub struct FunctionApplicationExpression {
pub call_path_binding: TypeBinding<CallPath>,
pub resolved_call_path_binding:
Option<TypeBinding<ResolvedCallPath<ParsedDeclId<FunctionDeclaration>>>>,
pub arguments: Vec<Expression>,
}

Expand All @@ -57,6 +62,8 @@ pub struct ArrayExpression {

#[derive(Debug, Clone)]
pub struct StructExpression {
pub resolved_call_path_binding:
Option<TypeBinding<ResolvedCallPath<ParsedDeclId<StructDeclaration>>>>,
pub call_path_binding: TypeBinding<CallPath>,
pub fields: Vec<StructExpressionField>,
}
Expand Down
8 changes: 7 additions & 1 deletion sway-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ pub use debug_generation::write_dwarf;
use indexmap::IndexMap;
use metadata::MetadataManager;
use query_engine::{ModuleCacheKey, ModulePath, ProgramsCacheEntry};
use semantic_analysis::symbol_resolve::ResolveSymbols;
use semantic_analysis::symbol_resolve_context::SymbolResolveContext;
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -536,9 +538,13 @@ pub fn parsed_to_ast(
build_module_dep_graph(handler, &mut parse_program.root)?;

// Collect the program symbols.
let _collection_ctx =
let mut symbol_collection_ctx =
ty::TyProgram::collect(handler, engines, parse_program, initial_namespace.clone())?;

// Resolve the program symbols.
let resolve_ctx = SymbolResolveContext::new(engines, &mut symbol_collection_ctx);
parse_program.resolve_symbols(handler, resolve_ctx);

// Type check the program.
let typed_program_opt = ty::TyProgram::type_check(
handler,
Expand Down
4 changes: 3 additions & 1 deletion sway-core/src/semantic_analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
pub mod ast_node;
pub(crate) mod cei_pattern_analysis;
pub(crate) mod coins_analysis;
pub mod symbol_collection_context;
mod module;
pub mod namespace;
mod node_dependencies;
mod program;
pub mod symbol_collection_context;
pub mod symbol_resolve;
pub mod symbol_resolve_context;
mod type_check_analysis;
pub(crate) mod type_check_context;
mod type_check_finalization;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ use crate::{
},
namespace::{IsExtendingExistingImpl, IsImplSelf},
semantic_analysis::{
symbol_collection_context::SymbolCollectionContext, type_check_context::EnforceTypeArguments,
ConstShadowingMode, GenericShadowingMode, TypeCheckAnalysis, TypeCheckAnalysisContext,
TypeCheckContext, TypeCheckFinalization, TypeCheckFinalizationContext,
symbol_collection_context::SymbolCollectionContext,
type_check_context::EnforceTypeArguments, ConstShadowingMode, GenericShadowingMode,
TypeCheckAnalysis, TypeCheckAnalysisContext, TypeCheckContext, TypeCheckFinalization,
TypeCheckFinalizationContext,
},
type_system::*,
Engines,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ impl TyImplTrait {
impl_type_parameters,
trait_name,
mut trait_type_arguments,
trait_decl_ref: _,
mut implementing_for,
items,
block_span,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ impl ty::TyExpression {
ExpressionKind::FunctionApplication(function_application_expression) => {
let FunctionApplicationExpression {
call_path_binding,
resolved_call_path_binding: _,
ref arguments,
} = *function_application_expression.clone();
Self::type_check_function_application(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,7 @@ pub(crate) fn type_check_method_application(
]),
span: Span::dummy(),
},
resolved_call_path_binding: None,
arguments: vec![
Expression {
kind: ExpressionKind::Literal(Literal::B256([0u8; 32])),
Expand Down
2 changes: 1 addition & 1 deletion sway-core/src/semantic_analysis/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ use crate::{
};

use super::{
symbol_collection_context::SymbolCollectionContext,
declaration::auto_impl::{self, EncodingAutoImplContext},
symbol_collection_context::SymbolCollectionContext,
};

#[derive(Clone, Debug)]
Expand Down
2 changes: 1 addition & 1 deletion sway-core/src/semantic_analysis/namespace/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub use lexical_scope::{Items, LexicalScope, LexicalScopeId, LexicalScopePath};
pub use module::Module;
pub use namespace::Namespace;
pub use namespace::TryInsertingTraitImplOnFailure;
pub use root::Root;
pub use root::{ResolvedDeclaration, Root};
pub(super) use trait_map::IsExtendingExistingImpl;
pub(super) use trait_map::IsImplSelf;
pub(super) use trait_map::ResolvedTraitImplItem;
Expand Down
2 changes: 1 addition & 1 deletion sway-core/src/semantic_analysis/namespace/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ impl Namespace {
}

/// Pushes a new submodule to the namespace's module hierarchy.
pub fn push_new_submodule(
pub fn push_submodule(
&mut self,
engines: &Engines,
mod_name: Ident,
Expand Down

0 comments on commit e2578a1

Please sign in to comment.