Skip to content

Commit

Permalink
dsadsadas
Browse files Browse the repository at this point in the history
  • Loading branch information
tritao committed Oct 18, 2024
1 parent 152a6de commit 87c09d3
Show file tree
Hide file tree
Showing 9 changed files with 174 additions and 25 deletions.
24 changes: 23 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,28 @@
"name": "Attach to forc-lsp",
"pid": "${command:pickProcess}",
"program": "${env:HOME}/.cargo/bin/forc-lsp"
}
},
{
"type": "lldb",
"request": "launch",
"name": "Debug executable 'forc'",
"cargo": {
"args": [
"build",
"--bin=forc",
"--package=forc"
],
"filter": {
"name": "forc",
"kind": "bin"
}
},
"args": [
"build",
"--path",
"const_rec"
],
"cwd": "${workspaceFolder}"
},
]
}
4 changes: 4 additions & 0 deletions sway-core/src/language/parsed/declaration/struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use crate::{
};
use sway_types::{ident::Ident, span::Span, Named, Spanned};

use super::ImplSelfOrTrait;

#[derive(Debug, Clone)]
pub struct StructDeclaration {
pub name: Ident,
Expand All @@ -15,6 +17,8 @@ pub struct StructDeclaration {
pub type_parameters: Vec<TypeParameter>,
pub visibility: Visibility,
pub(crate) span: Span,
// the impl blocks implementing this struct
pub impls: Vec<ImplSelfOrTrait>,
}

impl EqWithEngines for StructDeclaration {}
Expand Down
10 changes: 6 additions & 4 deletions sway-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -570,10 +570,12 @@ pub fn parsed_to_ast(
// Build the dependency graph for the submodules.
build_module_dep_graph(handler, &mut parse_program.root)?;

let namespace = Namespace::init_root(initial_namespace);
let initial_namespace = Namespace::init_root(initial_namespace);
// Collect the program symbols.
let mut collection_ctx =
ty::TyProgram::collect(handler, engines, parse_program, namespace.clone())?;
let namespace = initial_namespace.clone();
let mut collection_ctx = ty::TyProgram::collect(handler, engines, parse_program, namespace)?;

println!("namespace {:#?}", collection_ctx.namespace);

let resolve_ctx = SymbolResolveContext::new(engines, &mut collection_ctx);
parse_program.resolve_symbols(handler, resolve_ctx);
Expand All @@ -584,7 +586,7 @@ pub fn parsed_to_ast(
engines,
parse_program,
&mut collection_ctx,
namespace,
initial_namespace,
package_name,
build_config,
);
Expand Down
55 changes: 44 additions & 11 deletions sway-core/src/semantic_analysis/ast_node/declaration/impl_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ use crate::{
},
namespace::{IsExtendingExistingImpl, IsImplSelf, TryInsertingTraitImplOnFailure},
semantic_analysis::{
symbol_collection_context::SymbolCollectionContext, AbiMode, ConstShadowingMode,
TyNodeDepGraphNodeId, TypeCheckAnalysis, TypeCheckAnalysisContext, TypeCheckContext,
TypeCheckFinalization, TypeCheckFinalizationContext,
symbol_collection_context::SymbolCollectionContext, type_resolve::resolve_type, AbiMode,
ConstShadowingMode, TyNodeDepGraphNodeId, TypeCheckAnalysis, TypeCheckAnalysisContext,
TypeCheckContext, TypeCheckFinalization, TypeCheckFinalizationContext,
},
type_system::*,
};
Expand All @@ -37,15 +37,22 @@ impl TyImplSelfOrTrait {
ctx: &mut SymbolCollectionContext,
decl_id: &ParsedDeclId<ImplSelfOrTrait>,
) -> Result<(), ErrorEmitted> {
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 mut impl_trait = engines
.pe()
.get_impl_self_or_trait(decl_id)
.as_ref()
.clone();

if !impl_trait.is_self {
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| {
let (_ret, _scope) = ctx.scoped(engines, impl_trait.block_span.clone(), |scoped_ctx| {
impl_trait.items.iter().for_each(|item| match item {
ImplItem::Fn(decl_id) => {
let _ = TyFunctionDecl::collect(handler, engines, scoped_ctx, decl_id);
Expand All @@ -59,6 +66,32 @@ impl TyImplSelfOrTrait {
});
Ok(())
});

println!(
"impl type id {:?}",
engines.help_out(impl_trait.implementing_for.type_id)
);

impl_trait.implementing_for.type_id = resolve_type(
handler,
engines,
&ctx.namespace,
ctx.namespace.mod_path(),
impl_trait.implementing_for.type_id,
&impl_trait.implementing_for.span,
EnforceTypeArguments::Yes,
None,
None,
&SubstTypesContext::dummy(engines),
)?;

println!(
"after impl type id resolve {:?}",
engines.help_out(impl_trait.implementing_for.type_id)
);

engines.pe().replace(*decl_id, impl_trait);

Ok(())
}

Expand Down
75 changes: 72 additions & 3 deletions sway-core/src/semantic_analysis/namespace/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -740,13 +740,61 @@ impl Root {
.chain(&call_path.prefixes)
.cloned()
.collect();
self.resolve_symbol_and_mod_path(
let res = self.resolve_symbol_and_mod_path(
handler,
engines,
&symbol_path,
&call_path.suffix,
self_type,
)
);

if res.is_ok() {
return res;
}

// First lets get the submodule and then hierarchically lookup the call path,
// starting from the module's root scope.
let submodule = self.module.lookup_submodule(handler, engines, mod_path)?;
let mut scope_id_opt = Some(submodule.root_lexical_scope_id());

for ident in call_path.prefixes.iter() {
println!("checking for ident {:?}", ident);
if scope_id_opt.is_none() {
break;
}

let scope = submodule.lexical_scopes.get(scope_id_opt.unwrap()).unwrap();
let item = scope.items.symbols().get(ident);
println!("new item looked up {:?}", engines.help_out(item));
if item.is_none() {
break;
}

match item.unwrap() {
ResolvedDeclaration::Parsed(decl) => {
let span = decl.span(engines);
scope_id_opt = submodule.lexical_scopes_spans.get(&span).copied();
println!("new scope from item span {:?}", scope_id_opt);
}
ResolvedDeclaration::Typed(_decl_id) => todo!(),
};
}

if let Some(scope_id) = scope_id_opt {
let scope = submodule.lexical_scopes.get(scope_id).unwrap();
todo!();
// scope.items.reso
// let r = self
// .resolve_symbol(handler, engines, scope, &call_path.suffix, self_type)
// .map(|rd| (rd, mod_path.to_vec()));
// println!(
// "resolve_symbol_from_scope symbol {:?} {:?}",
// call_path.suffix, r
// );
// return r;
}

res
}

#[allow(clippy::too_many_arguments)]
Expand Down Expand Up @@ -936,7 +984,28 @@ impl Root {
decl: ResolvedDeclaration,
) -> Result<TypeInfo, ErrorEmitted> {
match decl {
ResolvedDeclaration::Parsed(_decl) => todo!(),
ResolvedDeclaration::Parsed(decl) => Ok(match decl.clone() {
Declaration::StructDeclaration(struct_ty_decl) => {
TypeInfo::UntypedStruct(struct_ty_decl)
}
Declaration::EnumDeclaration(enum_ty_decl) => TypeInfo::UntypedEnum(enum_ty_decl),
Declaration::TraitTypeDeclaration(type_decl) => {
let type_decl = engines.pe().get_trait_type(&type_decl);
if type_decl.ty_opt.is_none() {
return Err(handler.emit_err(CompileError::Internal(
"Trait type declaration has no type",
symbol.span(),
)));
}
(*engines.te().get(type_decl.ty_opt.clone().unwrap().type_id)).clone()
}
_ => {
return Err(handler.emit_err(CompileError::SymbolNotFound {
name: symbol.clone(),
span: symbol.span(),
}))
}
}),
ResolvedDeclaration::Typed(decl) => Ok(match decl.clone() {
ty::TyDecl::StructDecl(struct_ty_decl) => TypeInfo::Struct(struct_ty_decl.decl_id),
ty::TyDecl::EnumDecl(enum_ty_decl) => TypeInfo::Enum(enum_ty_decl.decl_id),
Expand Down
8 changes: 2 additions & 6 deletions sway-core/src/semantic_analysis/symbol_collection_context.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::{
language::{parsed::Declaration, Visibility},
namespace::LexicalScopeId,
namespace::ModulePath,
namespace::{LexicalScopeId, ModulePath},
semantic_analysis::Namespace,
Engines,
};
Expand All @@ -10,14 +9,11 @@ use sway_types::{span::Span, Ident};

use super::{ConstShadowingMode, GenericShadowingMode};

#[derive(Clone)]
//#[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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,7 @@ fn item_struct_to_struct_declaration(
)?,
visibility: pub_token_opt_to_visibility(item_struct.visibility),
span,
impls: vec![],
});
Ok(struct_declaration_id)
}
Expand Down
12 changes: 12 additions & 0 deletions sway-core/src/type_system/ast_elements/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,8 @@ impl TypeBinding<QualifiedCallPath> {
) -> Result<Declaration, ErrorEmitted> {
let engines = ctx.engines();

println!("resolve_symbol {:?}", self.inner);

// The first step is to determine if the call path refers to a module,
// enum, function or constant.
// If only one exists, then we use that one. Otherwise, if more than one exist, it is
Expand Down Expand Up @@ -650,6 +652,11 @@ impl SymbolResolveTypeBinding<ParsedDeclId<ConstantDeclaration>> for TypeBinding
handler: &Handler,
ctx: SymbolResolveContext,
) -> Result<ParsedDeclId<ConstantDeclaration>, ErrorEmitted> {
println!(
"resolve_symbol SymbolResolveTypeBinding Constant {:?}",
self
);

// Grab the declaration.
let unknown_decl = ctx
.resolve_call_path_with_visibility_check(handler, &self.inner)?
Expand All @@ -676,6 +683,11 @@ impl SymbolResolveTypeBinding<(ParsedDeclId<ConstantDeclaration>, TypeBinding<Ca
span: self.span.clone(),
};

println!(
"resolve_symbol SymbolResolveTypeBinding Constant {:?}",
call_path_binding
);

let type_info_opt = call_path_binding
.clone()
.inner
Expand Down
10 changes: 10 additions & 0 deletions sway-core/src/type_system/monomorphization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use sway_types::{Ident, Span, Spanned};
use crate::{
decl_engine::{engine::DeclEngineGetParsedDeclId, DeclEngineInsert},
language::{
parsed::Declaration,
ty::{self},
CallPath,
},
Expand Down Expand Up @@ -231,6 +232,15 @@ pub(crate) fn type_decl_opt_to_type_id(
let decl_engine = engines.de();
let type_engine = engines.te();
Ok(match type_decl_opt {
Some(ResolvedDeclaration::Parsed(Declaration::StructDeclaration(decl_id))) => {
type_engine.insert(engines, TypeInfo::UntypedStruct(decl_id), span.source_id())
}
Some(ResolvedDeclaration::Parsed(Declaration::EnumDeclaration(decl_id))) => {
todo!();
}
Some(ResolvedDeclaration::Parsed(Declaration::TypeAliasDeclaration(decl_id))) => {
todo!();
}
Some(ResolvedDeclaration::Typed(ty::TyDecl::StructDecl(ty::StructDecl {
decl_id: original_id,
..
Expand Down

0 comments on commit 87c09d3

Please sign in to comment.