Skip to content

Commit

Permalink
Clean up some more compilation-related types (FuelLabs#448)
Browse files Browse the repository at this point in the history
* fix InnerDependencyCompileResult

* fix CompilationResult::Library

* remove unused HllTypedParseTree/LibraryExports types

* run rustfmt
  • Loading branch information
canndrew authored Dec 13, 2021
1 parent 846da6c commit 64ab367
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 81 deletions.
59 changes: 16 additions & 43 deletions core_lang/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,6 @@ pub struct HllParseTree<'sc> {
/// Represents some exportable information that results from compiling some
/// Sway source code.
#[derive(Debug)]
pub struct HllTypedParseTree<'sc> {
pub library_exports: LibraryExports<'sc>,
}

/// Represents some exportable information that results from compiling some
/// Sway source code.
#[derive(Debug)]
pub struct LibraryExports<'sc> {
pub namespace: Namespace<'sc>,
trees: Vec<TypedParseTree<'sc>>,
}

/// Represents a parsed (but not yet type checked) Sway syntax tree.
#[derive(Debug)]
pub struct ParseTree<'sc> {
/// The untyped AST nodes that constitute this tree's root nodes.
pub root_nodes: Vec<AstNode<'sc>>,
Expand Down Expand Up @@ -181,7 +167,8 @@ pub enum CompilationResult<'sc> {
warnings: Vec<CompileWarning<'sc>>,
},
Library {
exports: LibraryExports<'sc>,
name: Ident<'sc>,
namespace: Namespace<'sc>,
warnings: Vec<CompileWarning<'sc>>,
},
Failure {
Expand Down Expand Up @@ -235,7 +222,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<'sc> {
library_exports: LibraryExports<'sc>,
name: Ident<'sc>,
namespace: Namespace<'sc>,
}
/// For internal compiler use.
/// Compiles an included file and returns its control flow and dead code graphs.
Expand Down Expand Up @@ -299,18 +287,11 @@ pub(crate) fn compile_inner_dependency<'sc>(
errors.push(e)
};

let mut library_exports = LibraryExports {
namespace: Default::default(),
trees: vec![],
};
library_exports.namespace.insert_module(
library_name.primary_name.to_string(),
typed_parse_tree.namespace().clone(),
);
library_exports.trees.push(typed_parse_tree);

ok(
InnerDependencyCompileResult { library_exports },
InnerDependencyCompileResult {
name: library_name.clone(),
namespace: typed_parse_tree.into_namespace(),
},
warnings,
errors,
)
Expand Down Expand Up @@ -379,18 +360,11 @@ pub fn compile_to_asm<'sc>(
}
CompilationResult::Success { asm, warnings }
}
TreeType::Library { name } => {
let mut exports = LibraryExports {
namespace: Default::default(),
trees: vec![],
};
exports.namespace.insert_module(
name.primary_name.to_string(),
typed_parse_tree.namespace().clone(),
);
exports.trees.push(typed_parse_tree);
CompilationResult::Library { warnings, exports }
}
TreeType::Library { name } => CompilationResult::Library {
warnings,
name,
namespace: typed_parse_tree.into_namespace(),
},
}
}

Expand Down Expand Up @@ -425,10 +399,9 @@ pub fn compile_to_bytecode<'n, 'sc>(
CompilationResult::Failure { warnings, errors } => {
BytecodeCompilationResult::Failure { warnings, errors }
}
CompilationResult::Library {
warnings,
exports: _exports,
} => BytecodeCompilationResult::Library { warnings },
CompilationResult::Library { warnings, .. } => {
BytecodeCompilationResult::Library { warnings }
}
}
}

Expand Down
20 changes: 11 additions & 9 deletions core_lang/src/semantic_analysis/ast_node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,11 @@ fn import_new_file<'n, 'sc>(
};
dep_config.file_name = file_name;
dep_config.dir_of_code = Arc::new(dep_path);
let crate::InnerDependencyCompileResult { library_exports } = check!(
let crate::InnerDependencyCompileResult {
name,
namespace: module,
..
} = check!(
crate::compile_inner_dependency(
static_file_string,
&dep_namespace,
Expand All @@ -762,14 +766,12 @@ fn import_new_file<'n, 'sc>(
errors
);

if let Some((name, module)) = library_exports.namespace.take_the_only_module() {
let name = match statement.alias {
Some(ref alias) => alias.primary_name.to_string(),
None => name,
};
namespace.insert_module(name, module);
}

let name = match statement.alias {
Some(ref alias) => alias,
None => &name,
};
let name = name.primary_name.to_string();
namespace.insert_module(name, module);
ok((), warnings, errors)
}

Expand Down
22 changes: 1 addition & 21 deletions core_lang/src/semantic_analysis/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,6 @@ impl<'sc> Namespace<'sc> {
self.modules.values()
}

// TODO: compile_inner_dependency returns a namespace that contains at most one module and
// nothing else. This is confusing and should be fixed.
pub fn take_the_only_module(self) -> Option<(String, Namespace<'sc>)> {
assert!(self.symbols.is_empty());
assert!(self.implemented_traits.is_empty());
assert!(self.use_synonyms.is_empty());
assert!(self.use_aliases.is_empty());
let mut modules = self.modules.into_iter();
match modules.next() {
Some((name, module)) => {
assert!(modules.next().is_none());
Some((name, module))
}
None => None,
}
}

/// this function either returns a struct (i.e. custom type), `None`, denoting the type that is
/// being looked for is actually a generic, not-yet-resolved type.
///
Expand Down Expand Up @@ -345,10 +328,7 @@ impl<'sc> Namespace<'sc> {
module_name: String,
module_contents: Namespace<'sc>,
) {
self.modules.insert(
module_name,
module_contents.modules.into_iter().next().unwrap().1,
);
self.modules.insert(module_name, module_contents);
}

pub(crate) fn find_enum(&self, enum_name: &Ident<'sc>) -> Option<TypedEnumDeclaration<'sc>> {
Expand Down
2 changes: 1 addition & 1 deletion core_lang/src/semantic_analysis/syntax_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl<'sc> TypedParseTree<'sc> {
}
}

pub(crate) fn namespace(&self) -> &Namespace<'sc> {
pub(crate) fn into_namespace(self) -> Namespace<'sc> {
use TypedParseTree::*;
match self {
Library { namespace, .. } => namespace,
Expand Down
16 changes: 9 additions & 7 deletions forc/src/ops/forc_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ use std::fs::File;
use std::io::Write;

use anyhow::Result;
use core_lang::{
BuildConfig, BytecodeCompilationResult, CompilationResult, LibraryExports, Namespace,
};
use core_lang::{BuildConfig, BytecodeCompilationResult, CompilationResult, Namespace};
use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};

Expand Down Expand Up @@ -236,7 +234,7 @@ fn compile_dependency_lib<'n, 'source, 'manifest>(
silent_mode,
)?;

namespace.insert_dependency_module(dependency_name.to_string(), compiled.namespace);
namespace.insert_dependency_module(dependency_name.to_string(), compiled);

// nothing is returned from this method since it mutates the hashmaps it was given
Ok(())
Expand All @@ -249,10 +247,14 @@ fn compile_library<'source>(
build_config: BuildConfig,
dependency_graph: &mut HashMap<String, HashSet<String>>,
silent_mode: bool,
) -> Result<LibraryExports<'source>, String> {
) -> Result<Namespace<'source>, String> {
let res = core_lang::compile_to_asm(source, namespace, build_config, dependency_graph);
match res {
CompilationResult::Library { exports, warnings } => {
CompilationResult::Library {
namespace,
warnings,
..
} => {
if !silent_mode {
warnings.iter().for_each(format_warning);
}
Expand All @@ -271,7 +273,7 @@ fn compile_library<'source>(
}
));
}
Ok(exports)
Ok(namespace)
}
CompilationResult::Failure { errors, warnings } => {
let e_len = errors.len();
Expand Down

0 comments on commit 64ab367

Please sign in to comment.