Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
emilyaherbert committed Aug 9, 2022
1 parent f9c785a commit bf2f453
Show file tree
Hide file tree
Showing 22 changed files with 590 additions and 397 deletions.
417 changes: 264 additions & 153 deletions sway-core/src/convert_parse_tree.rs

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions sway-core/src/semantic_analysis/ast_node/code_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ pub struct TypedCodeBlock {
}

impl CopyTypes for TypedCodeBlock {
fn copy_types(&mut self, type_mapping: &TypeMapping) {
fn copy_types(&mut self, type_engine: &mut TypeEngine, type_mapping: &TypeMapping) {
self.contents
.iter_mut()
.for_each(|x| x.copy_types(type_mapping));
.for_each(|x| x.copy_types(type_engine, type_mapping));
}
}

Expand Down Expand Up @@ -73,7 +73,8 @@ impl TypedCodeBlock {
let typed_code_block = TypedCodeBlock {
contents: evaluated_contents,
};
let type_id = return_type.unwrap_or_else(|| insert_type(TypeInfo::Tuple(Vec::new())));
let type_id =
return_type.unwrap_or_else(|| ctx.type_engine.insert_type(TypeInfo::Tuple(Vec::new())));
ok((typed_code_block, type_id), warnings, errors)
}
}
30 changes: 19 additions & 11 deletions sway-core/src/semantic_analysis/ast_node/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,25 @@ pub enum TypedDeclaration {
impl CopyTypes for TypedDeclaration {
/// The entry point to monomorphizing typed declarations. Instantiates all new type ids,
/// assuming `self` has already been copied.
fn copy_types(&mut self, type_mapping: &TypeMapping) {
fn copy_types(&mut self, type_engine: &mut TypeEngine, type_mapping: &TypeMapping) {
use TypedDeclaration::*;
match self {
VariableDeclaration(ref mut var_decl) => var_decl.copy_types(type_mapping),
ConstantDeclaration(ref mut const_decl) => const_decl.copy_types(type_mapping),
FunctionDeclaration(ref mut fn_decl) => fn_decl.copy_types(type_mapping),
TraitDeclaration(ref mut trait_decl) => trait_decl.copy_types(type_mapping),
StructDeclaration(ref mut struct_decl) => struct_decl.copy_types(type_mapping),
EnumDeclaration(ref mut enum_decl) => enum_decl.copy_types(type_mapping),
Reassignment(ref mut reassignment) => reassignment.copy_types(type_mapping),
ImplTrait(impl_trait) => impl_trait.copy_types(type_mapping),
VariableDeclaration(ref mut var_decl) => var_decl.copy_types(type_engine, type_mapping),
ConstantDeclaration(ref mut const_decl) => {
const_decl.copy_types(type_engine, type_mapping)
}
FunctionDeclaration(ref mut fn_decl) => fn_decl.copy_types(type_engine, type_mapping),
TraitDeclaration(ref mut trait_decl) => {
trait_decl.copy_types(type_engine, type_mapping)
}
StructDeclaration(ref mut struct_decl) => {
struct_decl.copy_types(type_engine, type_mapping)
}
EnumDeclaration(ref mut enum_decl) => enum_decl.copy_types(type_engine, type_mapping),
Reassignment(ref mut reassignment) => {
reassignment.copy_types(type_engine, type_mapping)
}
ImplTrait(impl_trait) => impl_trait.copy_types(type_engine, type_mapping),
// generics in an ABI is unsupported by design
AbiDeclaration(..)
| StorageDeclaration(..)
Expand Down Expand Up @@ -397,7 +405,7 @@ pub struct TypedConstantDeclaration {

impl CopyTypes for TypedConstantDeclaration {
fn copy_types(&mut self, type_mapping: &TypeMapping) {
self.value.copy_types(type_mapping);
self.value.copy_types(type_engine, type_mapping);
}
}

Expand Down Expand Up @@ -494,7 +502,7 @@ pub struct TypedReassignment {

impl CopyTypes for TypedReassignment {
fn copy_types(&mut self, type_mapping: &TypeMapping) {
self.rhs.copy_types(type_mapping);
self.rhs.copy_types(type_engine, type_mapping);
self.lhs_type
.update_type(type_mapping, &self.lhs_base_name.span());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ pub struct TypedIntrinsicFunctionKind {
}

impl CopyTypes for TypedIntrinsicFunctionKind {
fn copy_types(&mut self, type_mapping: &TypeMapping) {
fn copy_types(&mut self, type_engine: &mut TypeEngine, type_mapping: &TypeMapping) {
for arg in &mut self.arguments {
arg.copy_types(type_mapping);
arg.copy_types(type_engine, type_mapping);
}
for targ in &mut self.type_arguments {
targ.type_id.update_type(type_mapping, &targ.span);
targ.type_id
.update_type(type_engine, type_mapping, &targ.span);
}
}
}
Expand Down Expand Up @@ -92,7 +93,7 @@ impl TypedIntrinsicFunctionKind {
}
let ctx = ctx
.with_help_text("")
.with_type_annotation(insert_type(TypeInfo::Unknown));
.with_type_annotation(ctx.type_engine.insert_type(TypeInfo::Unknown));
let exp = check!(
TypedExpression::type_check(ctx, arguments[0].clone()),
return err(warnings, errors),
Expand All @@ -105,7 +106,9 @@ impl TypedIntrinsicFunctionKind {
type_arguments: vec![],
span,
};
let return_type = insert_type(TypeInfo::UnsignedInteger(IntegerBits::SixtyFour));
let return_type = ctx
.type_engine
.insert_type(TypeInfo::UnsignedInteger(IntegerBits::SixtyFour));
(intrinsic_function, return_type)
}
Intrinsic::SizeOfType => {
Expand All @@ -128,12 +131,13 @@ impl TypedIntrinsicFunctionKind {
let targ = type_arguments[0].clone();
let type_id = check!(
ctx.resolve_type_with_self(
insert_type(resolve_type(targ.type_id, &targ.span).unwrap()),
ctx.type_engine
.insert_type(resolve_type(targ.type_id, &targ.span).unwrap()),
&targ.span,
EnforceTypeArguments::Yes,
None
),
insert_type(TypeInfo::ErrorRecovery),
ctx.type_engine.insert_type(TypeInfo::ErrorRecovery),
warnings,
errors,
);
Expand All @@ -146,7 +150,9 @@ impl TypedIntrinsicFunctionKind {
}],
span,
};
let return_type = insert_type(TypeInfo::UnsignedInteger(IntegerBits::SixtyFour));
let return_type = ctx
.type_engine
.insert_type(TypeInfo::UnsignedInteger(IntegerBits::SixtyFour));
(intrinsic_function, return_type)
}
Intrinsic::IsReferenceType => {
Expand All @@ -161,12 +167,13 @@ impl TypedIntrinsicFunctionKind {
let targ = type_arguments[0].clone();
let type_id = check!(
ctx.resolve_type_with_self(
insert_type(resolve_type(targ.type_id, &targ.span).unwrap()),
ctx.type_engine
.insert_type(resolve_type(targ.type_id, &targ.span).unwrap()),
&targ.span,
EnforceTypeArguments::Yes,
None
),
insert_type(TypeInfo::ErrorRecovery),
ctx.type_engine.insert_type(TypeInfo::ErrorRecovery),
warnings,
errors,
);
Expand All @@ -179,7 +186,10 @@ impl TypedIntrinsicFunctionKind {
}],
span,
};
(intrinsic_function, insert_type(TypeInfo::Boolean))
(
intrinsic_function,
ctx.type_engine.insert_type(TypeInfo::Boolean),
)
}
Intrinsic::GetStorageKey => (
TypedIntrinsicFunctionKind {
Expand All @@ -188,7 +198,7 @@ impl TypedIntrinsicFunctionKind {
type_arguments: vec![],
span,
},
insert_type(TypeInfo::B256),
ctx.type_engine.insert_type(TypeInfo::B256),
),
Intrinsic::Eq => {
if arguments.len() != 2 {
Expand All @@ -201,7 +211,7 @@ impl TypedIntrinsicFunctionKind {
}
let mut ctx = ctx
.by_ref()
.with_type_annotation(insert_type(TypeInfo::Unknown));
.with_type_annotation(ctx.type_engine.insert_type(TypeInfo::Unknown));

let lhs = arguments[0].clone();
let lhs = check!(
Expand Down Expand Up @@ -242,7 +252,7 @@ impl TypedIntrinsicFunctionKind {
type_arguments: vec![],
span,
},
insert_type(TypeInfo::Boolean),
ctx.type_engine.insert_type(TypeInfo::Boolean),
)
}
Intrinsic::Gtf => {
Expand All @@ -267,7 +277,7 @@ impl TypedIntrinsicFunctionKind {
// Type check the first argument which is the index
let mut ctx = ctx
.by_ref()
.with_type_annotation(insert_type(TypeInfo::Unknown));
.with_type_annotation(ctx.type_engine.insert_type(TypeInfo::Unknown));
let index = check!(
TypedExpression::type_check(ctx.by_ref(), arguments[0].clone()),
return err(warnings, errors),
Expand All @@ -278,7 +288,7 @@ impl TypedIntrinsicFunctionKind {
// Type check the second argument which is the tx field ID
let mut ctx = ctx
.by_ref()
.with_type_annotation(insert_type(TypeInfo::Unknown));
.with_type_annotation(ctx.type_engine.insert_type(TypeInfo::Unknown));
let tx_field_id = check!(
TypedExpression::type_check(ctx.by_ref(), arguments[1].clone()),
return err(warnings, errors),
Expand Down Expand Up @@ -313,12 +323,13 @@ impl TypedIntrinsicFunctionKind {
let targ = type_arguments[0].clone();
let type_id = check!(
ctx.resolve_type_with_self(
insert_type(resolve_type(targ.type_id, &targ.span).unwrap()),
ctx.type_engine
.insert_type(resolve_type(targ.type_id, &targ.span).unwrap()),
&targ.span,
EnforceTypeArguments::Yes,
None
),
insert_type(TypeInfo::ErrorRecovery),
ctx.type_engine.insert_type(TypeInfo::ErrorRecovery),
warnings,
errors,
);
Expand Down Expand Up @@ -347,7 +358,7 @@ impl TypedIntrinsicFunctionKind {
}
let ctx = ctx
.with_help_text("")
.with_type_annotation(insert_type(TypeInfo::Unknown));
.with_type_annotation(ctx.type_engine.insert_type(TypeInfo::Unknown));
let exp = check!(
TypedExpression::type_check(ctx, arguments[0].clone()),
return err(warnings, errors),
Expand All @@ -372,7 +383,9 @@ impl TypedIntrinsicFunctionKind {
type_arguments: vec![],
span,
};
let return_type = insert_type(TypeInfo::UnsignedInteger(IntegerBits::SixtyFour));
let return_type = ctx
.type_engine
.insert_type(TypeInfo::UnsignedInteger(IntegerBits::SixtyFour));
(intrinsic_function, return_type)
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use crate::{
TypeCheckContext, TypedAstNode, TypedAstNodeContent, TypedCodeBlock, TypedExpression,
TypedExpressionVariant, TypedVariableDeclaration, VariableMutability,
},
type_system::insert_type,
types::DeterministicallyAborts,
CompileResult, MatchBranch, TypeInfo, TypedDeclaration,
};
Expand Down Expand Up @@ -80,7 +79,7 @@ impl TypedMatchBranch {
let typed_result = {
let ctx = ctx
.by_ref()
.with_type_annotation(insert_type(TypeInfo::Unknown));
.with_type_annotation(ctx.type_engine.insert_type(TypeInfo::Unknown));
check!(
TypedExpression::type_check(ctx, result),
return err(warnings, errors),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
},
IsConstant, TypeCheckContext, TypedExpression, TypedExpressionVariant,
},
type_system::{insert_type, TypeId},
type_system::TypeId,
CompileError, CompileResult, LazyOp, Literal, MatchBranch, TypeInfo,
};

Expand Down Expand Up @@ -94,7 +94,7 @@ impl TypedMatchExpression {
LazyOp::And,
new_condition,
inner_condition,
insert_type(TypeInfo::Boolean),
ctx.type_engine.insert_type(TypeInfo::Boolean),
joined_span,
)
}
Expand Down Expand Up @@ -125,7 +125,7 @@ impl TypedMatchExpression {
(Some(prev_if_exp), None) => {
let conditional = TypedExpression {
expression: TypedExpressionVariant::Literal(Literal::Boolean(true)),
return_type: insert_type(TypeInfo::Boolean),
return_type: ctx.type_engine.insert_type(TypeInfo::Boolean),
is_constant: IsConstant::No,
span: result_span.clone(),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use sway_types::{Ident, Span, Spanned};
use crate::{
error::{err, ok},
semantic_analysis::{TypeCheckContext, TypedEnumVariant},
type_system::{insert_type, CreateTypeId, EnforceTypeArguments, TypeArgument, TypeId},
type_system::{CreateTypeId, EnforceTypeArguments, TypeArgument, TypeId},
CompileError, CompileResult, Literal, Scrutinee, StructScrutineeField, TypeInfo,
};

Expand All @@ -20,7 +20,6 @@ pub(crate) enum TypedScrutineeVariant {
Literal(Literal),
Variable(Ident),
StructScrutinee(Vec<TypedStructScrutineeField>),
#[allow(dead_code)]
EnumScrutinee {
variant: TypedEnumVariant,
value: Box<TypedScrutinee>,
Expand All @@ -45,17 +44,17 @@ impl TypedScrutinee {
let typed_scrutinee = match scrutinee {
Scrutinee::CatchAll { span } => TypedScrutinee {
variant: TypedScrutineeVariant::CatchAll,
type_id: insert_type(TypeInfo::Unknown),
type_id: ctx.type_engine.insert_type(TypeInfo::Unknown),
span,
},
Scrutinee::Literal { value, span } => TypedScrutinee {
variant: TypedScrutineeVariant::Literal(value.clone()),
type_id: insert_type(value.to_typeinfo()),
type_id: ctx.type_engine.insert_type(value.to_typeinfo()),
span,
},
Scrutinee::Variable { name, span } => TypedScrutinee {
variant: TypedScrutineeVariant::Variable(name),
type_id: insert_type(TypeInfo::Unknown),
type_id: ctx.type_engine.insert_type(TypeInfo::Unknown),
span,
},
Scrutinee::StructScrutinee {
Expand Down Expand Up @@ -223,7 +222,7 @@ impl TypedScrutinee {
}
TypedScrutinee {
variant: TypedScrutineeVariant::Tuple(typed_elems.clone()),
type_id: insert_type(TypeInfo::Tuple(
type_id: ctx.type_engine.insert_type(TypeInfo::Tuple(
typed_elems
.into_iter()
.map(|x| TypeArgument {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub struct TypedStructExpressionField {
}

impl CopyTypes for TypedStructExpressionField {
fn copy_types(&mut self, type_mapping: &TypeMapping) {
self.value.copy_types(type_mapping);
fn copy_types(&mut self, type_engine: &mut TypeEngine, type_mapping: &TypeMapping) {
self.value.copy_types(type_engine, type_mapping);
}
}
Loading

0 comments on commit bf2f453

Please sign in to comment.