Skip to content

Commit

Permalink
Split crate_namespace out of Namespace (FuelLabs#437)
Browse files Browse the repository at this point in the history
* move import methods to NamespaceInner

* start refactoring find_method_for_type

* remove is_absolute from find_method_for_type

* move find_method_for_type to NamespaceInner

* WIP - remove Namespace entirely

* remove crate_namespace from forc

* keep removing crate_namespace

* remove crate_namespace from more types

* finish removing crate_namespace from TypedParseTree

* rename NamespaceInner back to Namespace

* run rustfmt
  • Loading branch information
canndrew authored Dec 9, 2021
1 parent 4fea960 commit a8aa08d
Show file tree
Hide file tree
Showing 16 changed files with 456 additions and 417 deletions.
18 changes: 9 additions & 9 deletions core_lang/src/asm_generation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use crate::{
error::*,
parse_tree::Literal,
semantic_analysis::{
NamespaceInner, TypedAstNode, TypedAstNodeContent, TypedDeclaration,
TypedFunctionDeclaration, TypedParseTree,
Namespace, TypedAstNode, TypedAstNodeContent, TypedDeclaration, TypedFunctionDeclaration,
TypedParseTree,
},
types::ResolvedType,
BuildConfig, Ident, TypeInfo,
Expand Down Expand Up @@ -617,8 +617,8 @@ impl<'sc> AsmNamespace<'sc> {
}
}

pub(crate) fn compile_ast_to_asm<'n, 'sc>(
ast: TypedParseTree<'n, 'sc>,
pub(crate) fn compile_ast_to_asm<'sc>(
ast: TypedParseTree<'sc>,
build_config: &BuildConfig,
) -> CompileResult<'sc, FinalizedAsm<'sc>> {
let mut register_sequencer = RegisterSequencer::new();
Expand All @@ -639,7 +639,7 @@ pub(crate) fn compile_ast_to_asm<'n, 'sc>(
&mut register_sequencer,
&mut asm_buf,
&declarations,
&ast_namespace.inner,
&ast_namespace,
),
return err(warnings, errors),
warnings,
Expand Down Expand Up @@ -694,7 +694,7 @@ pub(crate) fn compile_ast_to_asm<'n, 'sc>(
&mut register_sequencer,
&mut asm_buf,
&declarations,
&ast_namespace.inner,
&ast_namespace,
),
return err(warnings, errors),
warnings,
Expand Down Expand Up @@ -736,7 +736,7 @@ pub(crate) fn compile_ast_to_asm<'n, 'sc>(
&mut register_sequencer,
&mut asm_buf,
&declarations,
&ast_namespace.inner,
&ast_namespace,
),
return err(warnings, errors),
warnings,
Expand Down Expand Up @@ -1199,7 +1199,7 @@ fn add_all_constant_decls<'sc>(
register_sequencer: &mut RegisterSequencer,
asm_buf: &mut Vec<Op<'sc>>,
declarations: &[TypedDeclaration<'sc>],
ast_namespace: &NamespaceInner<'sc>,
ast_namespace: &Namespace<'sc>,
) -> CompileResult<'sc, ()> {
let mut warnings = vec![];
let mut errors = vec![];
Expand Down Expand Up @@ -1244,7 +1244,7 @@ fn add_module_constant_decls<'sc>(
namespace: &mut AsmNamespace<'sc>,
register_sequencer: &mut RegisterSequencer,
asm_buf: &mut Vec<Op<'sc>>,
ast_namespace: &NamespaceInner<'sc>,
ast_namespace: &Namespace<'sc>,
) -> CompileResult<'sc, ()> {
let mut warnings = vec![];
let mut errors = vec![];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::{error::*, semantic_analysis::TypedParseTree};
use petgraph::prelude::NodeIndex;

impl<'sc> ControlFlowGraph<'sc> {
pub(crate) fn construct_return_path_graph<'n>(ast: &TypedParseTree<'n, 'sc>) -> Self {
pub(crate) fn construct_return_path_graph(ast: &TypedParseTree<'sc>) -> Self {
let mut graph = ControlFlowGraph {
graph: Graph::new(),
entry_points: vec![],
Expand Down
4 changes: 2 additions & 2 deletions core_lang/src/control_flow_analysis/dead_code_analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ impl<'sc> ControlFlowGraph<'sc> {
.collect()
}

pub(crate) fn append_to_dead_code_graph<'n>(
ast: &TypedParseTree<'n, 'sc>,
pub(crate) fn append_to_dead_code_graph(
ast: &TypedParseTree<'sc>,
tree_type: TreeType,
graph: &mut ControlFlowGraph<'sc>,
// the `Result` return is just to handle `Unimplemented` errors
Expand Down
52 changes: 25 additions & 27 deletions core_lang/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ pub use crate::parse_tree::{Declaration, Expression, UseStatement, WhileLoop};
pub use crate::span::Span;
pub use error::{CompileError, CompileResult, CompileWarning};
pub use ident::Ident;
pub use semantic_analysis::{
Namespace, NamespaceInner, TypedDeclaration, TypedFunctionDeclaration,
};
pub use semantic_analysis::{Namespace, TypedDeclaration, TypedFunctionDeclaration};
pub use type_engine::TypeInfo;

// todo rename to language name
Expand All @@ -51,14 +49,14 @@ pub struct HllParseTree<'sc> {
}

#[derive(Debug)]
pub struct HllTypedParseTree<'n, 'sc> {
pub library_exports: LibraryExports<'n, 'sc>,
pub struct HllTypedParseTree<'sc> {
pub library_exports: LibraryExports<'sc>,
}

#[derive(Debug)]
pub struct LibraryExports<'n, 'sc> {
pub namespace: Namespace<'n, 'sc>,
trees: Vec<TypedParseTree<'n, 'sc>>,
pub struct LibraryExports<'sc> {
pub namespace: Namespace<'sc>,
trees: Vec<TypedParseTree<'sc>>,
}

#[derive(Debug)]
Expand Down Expand Up @@ -132,13 +130,13 @@ pub fn parse<'sc>(
ok(res, warnings, errors)
}

pub enum CompilationResult<'n, 'sc> {
pub enum CompilationResult<'sc> {
Success {
asm: FinalizedAsm<'sc>,
warnings: Vec<CompileWarning<'sc>>,
},
Library {
exports: LibraryExports<'n, 'sc>,
exports: LibraryExports<'sc>,
warnings: Vec<CompileWarning<'sc>>,
},
Failure {
Expand Down Expand Up @@ -184,8 +182,8 @@ fn get_end(err: &pest::error::Error<Rule>) -> usize {

/// This struct represents the compilation of an internal dependency
/// defined through an include statement (the `dep` keyword).
pub(crate) struct InnerDependencyCompileResult<'n, 'sc> {
library_exports: LibraryExports<'n, 'sc>,
pub(crate) struct InnerDependencyCompileResult<'sc> {
library_exports: LibraryExports<'sc>,
}
/// For internal compiler use.
/// Compiles an included file and returns its control flow and dead code graphs.
Expand All @@ -194,13 +192,13 @@ pub(crate) struct InnerDependencyCompileResult<'n, 'sc> {
/// TODO -- there is _so_ much duplicated code and messiness in this file around the
/// different types of compilation and stuff. After we get to a good state with the MVP,
/// clean up the types here with the power of hindsight
pub(crate) fn compile_inner_dependency<'n, 'sc>(
pub(crate) fn compile_inner_dependency<'sc>(
input: &'sc str,
initial_namespace: &Namespace<'n, 'sc>,
initial_namespace: &Namespace<'sc>,
build_config: BuildConfig,
dead_code_graph: &mut ControlFlowGraph<'sc>,
dependency_graph: &mut HashMap<String, HashSet<String>>,
) -> CompileResult<'sc, InnerDependencyCompileResult<'n, 'sc>> {
) -> CompileResult<'sc, InnerDependencyCompileResult<'sc>> {
let mut warnings = Vec::new();
let mut errors = Vec::new();
let parse_tree = check!(
Expand Down Expand Up @@ -247,9 +245,9 @@ pub(crate) fn compile_inner_dependency<'n, 'sc>(
trees: vec![],
};
for (ref name, parse_tree) in res {
exports.namespace.inner.insert_module(
exports.namespace.insert_module(
name.primary_name.to_string(),
parse_tree.namespace().inner.clone(),
parse_tree.namespace().clone(),
);
exports.trees.push(parse_tree);
}
Expand Down Expand Up @@ -278,12 +276,12 @@ pub(crate) fn compile_inner_dependency<'n, 'sc>(
)
}

pub fn compile_to_asm<'n, 'sc>(
pub fn compile_to_asm<'sc>(
input: &'sc str,
initial_namespace: &Namespace<'n, 'sc>,
initial_namespace: &Namespace<'sc>,
build_config: BuildConfig,
dependency_graph: &mut HashMap<String, HashSet<String>>,
) -> CompilationResult<'n, 'sc> {
) -> CompilationResult<'sc> {
let mut warnings = Vec::new();
let mut errors = Vec::new();
let parse_tree = check!(
Expand Down Expand Up @@ -339,9 +337,9 @@ pub fn compile_to_asm<'n, 'sc>(
trees: vec![],
};
for (ref name, parse_tree) in res {
exports.namespace.inner.insert_module(
exports.namespace.insert_module(
name.primary_name.to_string(),
parse_tree.namespace().inner.clone(),
parse_tree.namespace().clone(),
);
exports.trees.push(parse_tree);
}
Expand Down Expand Up @@ -452,7 +450,7 @@ pub fn compile_to_asm<'n, 'sc>(
}
pub fn compile_to_bytecode<'n, 'sc>(
input: &'sc str,
initial_namespace: &Namespace<'n, 'sc>,
initial_namespace: &Namespace<'sc>,
build_config: BuildConfig,
dependency_graph: &mut HashMap<String, HashSet<String>>,
) -> BytecodeCompilationResult<'sc> {
Expand Down Expand Up @@ -486,8 +484,8 @@ pub fn compile_to_bytecode<'n, 'sc>(
}
}

fn perform_control_flow_analysis<'n, 'sc>(
tree: &Option<TypedParseTree<'n, 'sc>>,
fn perform_control_flow_analysis<'sc>(
tree: &Option<TypedParseTree<'sc>>,
tree_type: TreeType,
dead_code_graph: &mut ControlFlowGraph<'sc>,
) -> (Vec<CompileWarning<'sc>>, Vec<CompileError<'sc>>) {
Expand All @@ -507,8 +505,8 @@ fn perform_control_flow_analysis<'n, 'sc>(
None => (vec![], vec![]),
}
}
fn perform_control_flow_analysis_on_library_exports<'n, 'sc>(
lib: &LibraryExports<'n, 'sc>,
fn perform_control_flow_analysis_on_library_exports<'sc>(
lib: &LibraryExports<'sc>,
dead_code_graph: &mut ControlFlowGraph<'sc>,
) -> (Vec<CompileWarning<'sc>>, Vec<CompileError<'sc>>) {
let mut warnings = vec![];
Expand Down
6 changes: 3 additions & 3 deletions core_lang/src/parse_tree/declaration/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::span::Span;
use crate::type_engine::*;

use crate::Ident;
use crate::NamespaceInner;
use crate::Namespace;
use crate::{
error::*,
semantic_analysis::ast_node::{declaration::insert_type_parameters, TypedEnumDeclaration},
Expand Down Expand Up @@ -38,7 +38,7 @@ impl<'sc> EnumDeclaration<'sc> {
/// something.
pub(crate) fn to_typed_decl(
&self,
namespace: &mut NamespaceInner<'sc>,
namespace: &mut Namespace<'sc>,
self_type: TypeId,
) -> TypedEnumDeclaration<'sc> {
let mut variants_buf = vec![];
Expand Down Expand Up @@ -153,7 +153,7 @@ impl<'sc> EnumDeclaration<'sc> {
impl<'sc> EnumVariant<'sc> {
pub(crate) fn to_typed_decl(
&self,
namespace: &mut NamespaceInner<'sc>,
namespace: &mut Namespace<'sc>,
self_type: TypeId,
span: Span<'sc>,
type_mapping: &[(TypeParameter, TypeId)],
Expand Down
4 changes: 3 additions & 1 deletion core_lang/src/semantic_analysis/ast_node/code_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ pub(crate) struct TypedCodeBlock<'sc> {
impl<'sc> TypedCodeBlock<'sc> {
pub(crate) fn type_check<'n>(
other: CodeBlock<'sc>,
namespace: &Namespace<'n, 'sc>,
namespace: &Namespace<'sc>,
crate_namespace: Option<&'n Namespace<'sc>>,
// this is for the return or implicit return
type_annotation: TypeId,
help_text: impl Into<String> + Clone,
Expand All @@ -36,6 +37,7 @@ impl<'sc> TypedCodeBlock<'sc> {
TypedAstNode::type_check(
node.clone(),
&mut local_namespace,
crate_namespace,
type_annotation,
help_text.clone(),
self_type,
Expand Down
13 changes: 5 additions & 8 deletions core_lang/src/semantic_analysis/ast_node/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,8 @@ impl TypedReassignment<'_> {
impl<'sc> TypedFunctionDeclaration<'sc> {
pub fn type_check<'n>(
fn_decl: FunctionDeclaration<'sc>,
namespace: &mut Namespace<'n, 'sc>,
namespace: &mut Namespace<'sc>,
crate_namespace: Option<&Namespace<'sc>>,
_return_type_annotation: TypeId,
_help_text: impl Into<String>,
// If there are any `Self` types in this declaration,
Expand Down Expand Up @@ -807,7 +808,6 @@ impl<'sc> TypedFunctionDeclaration<'sc> {
insert_type(TypeInfo::Ref(matching_id))
} else {
namespace
.inner
.resolve_type_with_self(return_type, self_type)
.unwrap_or_else(|_| {
errors.push(CompileError::UnknownType {
Expand All @@ -820,9 +820,7 @@ impl<'sc> TypedFunctionDeclaration<'sc> {
// insert parameters and generic type declarations into namespace
let mut namespace = namespace.clone();
type_parameters.iter().for_each(|param| {
namespace
.inner
.insert(param.name_ident.clone(), param.into());
namespace.insert(param.name_ident.clone(), param.into());
});
for FunctionParameter {
name,
Expand All @@ -834,7 +832,6 @@ impl<'sc> TypedFunctionDeclaration<'sc> {
insert_type(TypeInfo::Ref(matching_id))
} else {
namespace
.inner
.resolve_type_with_self(r#type, self_type)
.unwrap_or_else(|_| {
errors.push(CompileError::UnknownType {
Expand All @@ -843,7 +840,7 @@ impl<'sc> TypedFunctionDeclaration<'sc> {
insert_type(TypeInfo::ErrorRecovery)
})
};
namespace.inner.insert(
namespace.insert(
name.clone(),
TypedDeclaration::VariableDeclaration(TypedVariableDeclaration {
name: name.clone(),
Expand All @@ -865,6 +862,7 @@ impl<'sc> TypedFunctionDeclaration<'sc> {
TypedCodeBlock::type_check(
body.clone(),
&namespace,
crate_namespace,
return_type,
"Function body's return type does not match up with its return type annotation.",
self_type,
Expand Down Expand Up @@ -897,7 +895,6 @@ impl<'sc> TypedFunctionDeclaration<'sc> {
insert_type(TypeInfo::Ref(matching_id))
} else {
namespace
.inner
.resolve_type_with_self(r#type, self_type)
.unwrap_or_else(|_| {
errors.push(CompileError::UnknownType {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ pub(crate) fn instantiate_enum<'n, 'sc>(
enum_decl: TypedEnumDeclaration<'sc>,
enum_field_name: Ident<'sc>,
args: Vec<Expression<'sc>>,
namespace: &mut Namespace<'n, 'sc>,
namespace: &mut Namespace<'sc>,
crate_namespace: Option<&'n Namespace<'sc>>,
self_type: TypeId,
build_config: &BuildConfig,
dead_code_graph: &mut ControlFlowGraph<'sc>,
Expand Down Expand Up @@ -66,6 +67,7 @@ pub(crate) fn instantiate_enum<'n, 'sc>(
TypedExpression::type_check(
single_expr.clone(),
namespace,
crate_namespace,
Some(enum_field_type),
"Enum instantiator must match its declared variant type.",
self_type,
Expand Down
Loading

0 comments on commit a8aa08d

Please sign in to comment.