Skip to content

Commit

Permalink
Move the definition for TyDeclaration to the ty module. (FuelLabs…
Browse files Browse the repository at this point in the history
  • Loading branch information
emilyaherbert authored Oct 12, 2022
1 parent f51d3f4 commit 081791f
Show file tree
Hide file tree
Showing 35 changed files with 516 additions and 451 deletions.
4 changes: 2 additions & 2 deletions sway-core/src/control_flow_analysis/analyze_return_paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,12 @@ fn connect_node(

fn connect_declaration(
node: &TyAstNode,
decl: &TyDeclaration,
decl: &ty::TyDeclaration,
graph: &mut ControlFlowGraph,
span: Span,
leaves: &[NodeIndex],
) -> Result<Vec<NodeIndex>, CompileError> {
use TyDeclaration::*;
use ty::TyDeclaration::*;
match decl {
TraitDeclaration(_)
| AbiDeclaration(_)
Expand Down
35 changes: 20 additions & 15 deletions sway-core/src/control_flow_analysis/dead_code_analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
language::{parsed::TreeType, ty, CallPath, Visibility},
semantic_analysis::{
ast_node::{
TyAbiDeclaration, TyCodeBlock, TyConstantDeclaration, TyDeclaration, TyEnumDeclaration,
TyAbiDeclaration, TyCodeBlock, TyConstantDeclaration, TyEnumDeclaration,
TyFunctionDeclaration, TyStructDeclaration, TyStructExpressionField,
TyTraitDeclaration, TyVariableDeclaration, VariableMutability,
},
Expand Down Expand Up @@ -129,7 +129,7 @@ impl ControlFlowGraph {
ControlFlowGraphNode::ProgramNode(TyAstNode {
span,
content:
TyAstNodeContent::Declaration(TyDeclaration::FunctionDeclaration(
TyAstNodeContent::Declaration(ty::TyDeclaration::FunctionDeclaration(
decl_id,
)),
..
Expand All @@ -152,7 +152,7 @@ impl ControlFlowGraph {
ControlFlowGraphNode::OrganizationalDominator(_) => false,
ControlFlowGraphNode::ProgramNode(TyAstNode {
content:
TyAstNodeContent::Declaration(TyDeclaration::FunctionDeclaration(
TyAstNodeContent::Declaration(ty::TyDeclaration::FunctionDeclaration(
decl_id,
)),
..
Expand All @@ -162,26 +162,31 @@ impl ControlFlowGraph {
}
ControlFlowGraphNode::ProgramNode(TyAstNode {
content:
TyAstNodeContent::Declaration(TyDeclaration::TraitDeclaration(decl_id)),
TyAstNodeContent::Declaration(ty::TyDeclaration::TraitDeclaration(
decl_id,
)),
..
}) => de_get_trait(decl_id.clone(), &decl_id.span())?
.visibility
.is_public(),
ControlFlowGraphNode::ProgramNode(TyAstNode {
content:
TyAstNodeContent::Declaration(TyDeclaration::StructDeclaration(decl_id)),
TyAstNodeContent::Declaration(ty::TyDeclaration::StructDeclaration(
decl_id,
)),
..
}) => {
let struct_decl = de_get_struct(decl_id.clone(), &decl_id.span())?;
struct_decl.visibility == Visibility::Public
}
ControlFlowGraphNode::ProgramNode(TyAstNode {
content: TyAstNodeContent::Declaration(TyDeclaration::ImplTrait { .. }),
content:
TyAstNodeContent::Declaration(ty::TyDeclaration::ImplTrait { .. }),
..
}) => true,
ControlFlowGraphNode::ProgramNode(TyAstNode {
content:
TyAstNodeContent::Declaration(TyDeclaration::ConstantDeclaration(
TyAstNodeContent::Declaration(ty::TyDeclaration::ConstantDeclaration(
decl_id,
)),
..
Expand Down Expand Up @@ -278,15 +283,15 @@ fn connect_node(
}

fn connect_declaration(
decl: &TyDeclaration,
decl: &ty::TyDeclaration,
graph: &mut ControlFlowGraph,
entry_node: NodeIndex,
span: Span,
exit_node: Option<NodeIndex>,
tree_type: &TreeType,
leaves: &[NodeIndex],
) -> Result<Vec<NodeIndex>, CompileError> {
use TyDeclaration::*;
use ty::TyDeclaration::*;
match decl {
VariableDeclaration(var_decl) => {
let TyVariableDeclaration {
Expand Down Expand Up @@ -1246,22 +1251,22 @@ fn construct_dead_code_warning_from_node(node: &TyAstNode) -> Option<CompileWarn
// if this is a function, struct, or trait declaration that is never called, then it is dead
// code.
TyAstNode {
content: TyAstNodeContent::Declaration(TyDeclaration::FunctionDeclaration(_)),
content: TyAstNodeContent::Declaration(ty::TyDeclaration::FunctionDeclaration(_)),
span,
..
} => CompileWarning {
span: span.clone(),
warning_content: Warning::DeadFunctionDeclaration,
},
TyAstNode {
content: TyAstNodeContent::Declaration(TyDeclaration::StructDeclaration { .. }),
content: TyAstNodeContent::Declaration(ty::TyDeclaration::StructDeclaration { .. }),
span,
} => CompileWarning {
span: span.clone(),
warning_content: Warning::DeadStructDeclaration,
},
TyAstNode {
content: TyAstNodeContent::Declaration(TyDeclaration::TraitDeclaration(decl_id)),
content: TyAstNodeContent::Declaration(ty::TyDeclaration::TraitDeclaration(decl_id)),
..
} => {
let span = match de_get_trait(decl_id.clone(), &decl_id.span()) {
Expand All @@ -1274,7 +1279,7 @@ fn construct_dead_code_warning_from_node(node: &TyAstNode) -> Option<CompileWarn
}
}
TyAstNode {
content: TyAstNodeContent::Declaration(TyDeclaration::ImplTrait(decl_id)),
content: TyAstNodeContent::Declaration(ty::TyDeclaration::ImplTrait(decl_id)),
span,
} => match de_get_impl_trait(decl_id.clone(), span) {
Ok(TyImplTrait { methods, .. }) if methods.is_empty() => return None,
Expand All @@ -1284,13 +1289,13 @@ fn construct_dead_code_warning_from_node(node: &TyAstNode) -> Option<CompileWarn
},
},
TyAstNode {
content: TyAstNodeContent::Declaration(TyDeclaration::AbiDeclaration { .. }),
content: TyAstNodeContent::Declaration(ty::TyDeclaration::AbiDeclaration { .. }),
..
} => return None,
// We handle storage fields individually. There is no need to emit any warnings for the
// storage declaration itself.
TyAstNode {
content: TyAstNodeContent::Declaration(TyDeclaration::StorageDeclaration { .. }),
content: TyAstNodeContent::Declaration(ty::TyDeclaration::StorageDeclaration { .. }),
..
} => return None,
TyAstNode {
Expand Down
1 change: 1 addition & 0 deletions sway-core/src/declaration_engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ pub(crate) mod declaration_id;
pub(crate) mod declaration_wrapper;

pub use declaration_engine::*;
pub(crate) use declaration_id::*;
30 changes: 15 additions & 15 deletions sway-core/src/ir_generation/compile.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
declaration_engine::declaration_engine::de_get_constant,
language::Visibility,
language::{ty, Visibility},
metadata::MetadataManager,
semantic_analysis::{ast_node::*, namespace},
type_system::look_up_type_id,
Expand All @@ -20,7 +20,7 @@ pub(super) fn compile_script(
context: &mut Context,
main_function: TyFunctionDeclaration,
namespace: &namespace::Module,
declarations: Vec<TyDeclaration>,
declarations: Vec<ty::TyDeclaration>,
) -> Result<Module, CompileError> {
let module = Module::new(context, Kind::Script);
let mut md_mgr = MetadataManager::default();
Expand All @@ -36,7 +36,7 @@ pub(super) fn compile_contract(
context: &mut Context,
abi_entries: Vec<TyFunctionDeclaration>,
namespace: &namespace::Module,
declarations: Vec<TyDeclaration>,
declarations: Vec<ty::TyDeclaration>,
) -> Result<Module, CompileError> {
let module = Module::new(context, Kind::Contract);
let mut md_mgr = MetadataManager::default();
Expand Down Expand Up @@ -90,11 +90,11 @@ fn compile_declarations(
md_mgr: &mut MetadataManager,
module: Module,
namespace: &namespace::Module,
declarations: Vec<TyDeclaration>,
declarations: Vec<ty::TyDeclaration>,
) -> Result<(), CompileError> {
for declaration in declarations {
match declaration {
TyDeclaration::ConstantDeclaration(ref decl_id) => {
ty::TyDeclaration::ConstantDeclaration(ref decl_id) => {
let decl = de_get_constant(decl_id.clone(), &declaration.span())?;
compile_const_decl(
&mut LookupEnv {
Expand All @@ -108,14 +108,14 @@ fn compile_declarations(
)?;
}

TyDeclaration::FunctionDeclaration(_decl) => {
ty::TyDeclaration::FunctionDeclaration(_decl) => {
// We no longer compile functions other than `main()` until we can improve the name
// resolution. Currently there isn't enough information in the AST to fully
// distinguish similarly named functions and especially trait methods.
//
//compile_function(context, module, decl).map(|_| ())?
}
TyDeclaration::ImplTrait(_) => {
ty::TyDeclaration::ImplTrait(_) => {
// And for the same reason we don't need to compile impls at all.
//
// compile_impl(
Expand All @@ -126,14 +126,14 @@ fn compile_declarations(
//)?,
}

TyDeclaration::StructDeclaration(_)
| TyDeclaration::EnumDeclaration(_)
| TyDeclaration::TraitDeclaration(_)
| TyDeclaration::VariableDeclaration(_)
| TyDeclaration::AbiDeclaration(_)
| TyDeclaration::GenericTypeForFunctionScope { .. }
| TyDeclaration::StorageDeclaration(_)
| TyDeclaration::ErrorRecovery => (),
ty::TyDeclaration::StructDeclaration(_)
| ty::TyDeclaration::EnumDeclaration(_)
| ty::TyDeclaration::TraitDeclaration(_)
| ty::TyDeclaration::VariableDeclaration(_)
| ty::TyDeclaration::AbiDeclaration(_)
| ty::TyDeclaration::GenericTypeForFunctionScope { .. }
| ty::TyDeclaration::StorageDeclaration(_)
| ty::TyDeclaration::ErrorRecovery => (),
}
}
Ok(())
Expand Down
4 changes: 2 additions & 2 deletions sway-core/src/ir_generation/const_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
metadata::MetadataManager,
semantic_analysis::{
declaration::ProjectionKind, namespace, TyAstNode, TyAstNodeContent, TyConstantDeclaration,
TyDeclaration, TyStructExpressionField,
TyStructExpressionField,
},
};

Expand Down Expand Up @@ -43,7 +43,7 @@ pub(crate) fn compile_const_decl(
// See if we it's a global const and whether we can compile it *now*.
let decl = module_ns.check_symbol(name)?;
let decl_name_value = match decl {
TyDeclaration::ConstantDeclaration(decl_id) => {
ty::TyDeclaration::ConstantDeclaration(decl_id) => {
let TyConstantDeclaration { name, value, .. } =
de_get_constant(decl_id.clone(), &name.span())?;
Some((name, value))
Expand Down
62 changes: 36 additions & 26 deletions sway-core/src/ir_generation/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,57 +114,67 @@ impl FnCompiler {
let span_md_idx = md_mgr.span_to_md(context, &ast_node.span);
match ast_node.content {
TyAstNodeContent::Declaration(td) => match td {
TyDeclaration::VariableDeclaration(tvd) => {
ty::TyDeclaration::VariableDeclaration(tvd) => {
self.compile_var_decl(context, md_mgr, *tvd, span_md_idx)
}
TyDeclaration::ConstantDeclaration(decl_id) => {
ty::TyDeclaration::ConstantDeclaration(decl_id) => {
let tcd = declaration_engine::de_get_constant(decl_id, &ast_node.span)?;
self.compile_const_decl(context, md_mgr, tcd, span_md_idx)?;
Ok(None)
}
TyDeclaration::FunctionDeclaration(_) => Err(CompileError::UnexpectedDeclaration {
decl_type: "function",
span: ast_node.span,
}),
TyDeclaration::TraitDeclaration(_) => Err(CompileError::UnexpectedDeclaration {
decl_type: "trait",
span: ast_node.span,
}),
TyDeclaration::StructDeclaration(_) => Err(CompileError::UnexpectedDeclaration {
decl_type: "struct",
span: ast_node.span,
}),
TyDeclaration::EnumDeclaration(decl_id) => {
ty::TyDeclaration::FunctionDeclaration(_) => {
Err(CompileError::UnexpectedDeclaration {
decl_type: "function",
span: ast_node.span,
})
}
ty::TyDeclaration::TraitDeclaration(_) => {
Err(CompileError::UnexpectedDeclaration {
decl_type: "trait",
span: ast_node.span,
})
}
ty::TyDeclaration::StructDeclaration(_) => {
Err(CompileError::UnexpectedDeclaration {
decl_type: "struct",
span: ast_node.span,
})
}
ty::TyDeclaration::EnumDeclaration(decl_id) => {
let ted = declaration_engine::de_get_enum(decl_id, &ast_node.span)?;
create_enum_aggregate(context, ted.variants).map(|_| ())?;
Ok(None)
}
TyDeclaration::ImplTrait(_) => {
ty::TyDeclaration::ImplTrait(_) => {
// XXX What if we ignore the trait implementation??? Potentially since
// we currently inline everything and below we 'recreate' the functions
// lazily as they are called, nothing needs to be done here. BUT!
// This is obviously not really correct, and eventually we want to
// compile and then call these properly.
Ok(None)
}
TyDeclaration::AbiDeclaration(_) => Err(CompileError::UnexpectedDeclaration {
ty::TyDeclaration::AbiDeclaration(_) => Err(CompileError::UnexpectedDeclaration {
decl_type: "abi",
span: ast_node.span,
}),
TyDeclaration::GenericTypeForFunctionScope { .. } => {
ty::TyDeclaration::GenericTypeForFunctionScope { .. } => {
Err(CompileError::UnexpectedDeclaration {
decl_type: "abi",
span: ast_node.span,
})
}
TyDeclaration::ErrorRecovery { .. } => Err(CompileError::UnexpectedDeclaration {
decl_type: "error recovery",
span: ast_node.span,
}),
TyDeclaration::StorageDeclaration(_) => Err(CompileError::UnexpectedDeclaration {
decl_type: "storage",
span: ast_node.span,
}),
ty::TyDeclaration::ErrorRecovery { .. } => {
Err(CompileError::UnexpectedDeclaration {
decl_type: "error recovery",
span: ast_node.span,
})
}
ty::TyDeclaration::StorageDeclaration(_) => {
Err(CompileError::UnexpectedDeclaration {
decl_type: "storage",
span: ast_node.span,
})
}
},
TyAstNodeContent::Expression(te) => {
// An expression with an ignored return value... I assume.
Expand Down
Loading

0 comments on commit 081791f

Please sign in to comment.