Skip to content

Commit

Permalink
Move the storage definitions to the ty module. (FuelLabs#2983)
Browse files Browse the repository at this point in the history
  • Loading branch information
emilyaherbert authored Oct 11, 2022
1 parent 0190824 commit d51a24e
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 49 deletions.
2 changes: 1 addition & 1 deletion sway-core/src/ir_generation/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1880,7 +1880,7 @@ impl FnCompiler {
&mut self,
context: &mut Context,
md_mgr: &mut MetadataManager,
fields: &[TypeCheckedStorageAccessDescriptor],
fields: &[ty::TyStorageAccessDescriptor],
ix: &StateIndex,
span_md_idx: Option<MetadataIndex>,
) -> Result<Value, CompileError> {
Expand Down
9 changes: 4 additions & 5 deletions sway-core/src/ir_generation/types.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use crate::{
semantic_analysis::{
ProjectionKind, TyEnumVariant, TyStorageReassignDescriptor,
TypeCheckedStorageAccessDescriptor,
},
language::ty,
semantic_analysis::{ProjectionKind, TyEnumVariant, TyStorageReassignDescriptor},
type_system::{to_typeinfo, TypeId, TypeInfo},
};

Expand Down Expand Up @@ -114,7 +112,8 @@ impl TypedNamedField for ProjectionKind {
}
}

impl_typed_named_field_for!(TypeCheckedStorageAccessDescriptor);
use ty::TyStorageAccessDescriptor;
impl_typed_named_field_for!(TyStorageAccessDescriptor);
impl_typed_named_field_for!(TyStorageReassignDescriptor);

pub(super) fn get_indices_for_struct_access<F: TypedNamedField>(
Expand Down
4 changes: 2 additions & 2 deletions sway-core/src/language/ty/expression/expression_variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
ContractCallParams, ProjectionKind, TyAsmRegisterDeclaration, TyCodeBlock,
TyEnumDeclaration, TyEnumVariant, TyIntrinsicFunctionKind, TyReassignment,
TyReturnStatement, TyStorageReassignment, TyStructExpressionField, TyStructField,
TypeCheckedStorageAccess, VariableMutability,
VariableMutability,
},
type_system::*,
TyFunctionDeclaration,
Expand Down Expand Up @@ -108,7 +108,7 @@ pub enum TyExpressionVariant {
// this span may be used for errors in the future, although it is not right now.
span: Span,
},
StorageAccess(TypeCheckedStorageAccess),
StorageAccess(TyStorageAccess),
IntrinsicFunction(TyIntrinsicFunctionKind),
/// a zero-sized type-system-only compile-time thing that is used for constructing ABI casts.
AbiName(AbiName),
Expand Down
2 changes: 2 additions & 0 deletions sway-core/src/language/ty/expression/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#[allow(clippy::module_inception)]
mod expression;
mod expression_variant;
mod storage;

pub use expression::*;
pub use expression_variant::*;
pub use storage::*;
34 changes: 34 additions & 0 deletions sway-core/src/language/ty/expression/storage.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use sway_types::{state::StateIndex, Ident, Span, Spanned};

use crate::type_system::TypeId;

/// Describes the full storage access including all the subfields
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TyStorageAccess {
pub fields: Vec<TyStorageAccessDescriptor>,
pub(crate) ix: StateIndex,
}

impl Spanned for TyStorageAccess {
fn span(&self) -> Span {
self.fields
.iter()
.fold(self.fields[0].span.clone(), |acc, field| {
Span::join(acc, field.span.clone())
})
}
}

impl TyStorageAccess {
pub fn storage_field_name(&self) -> Ident {
self.fields[0].name.clone()
}
}

/// Describes a single subfield access in the sequence when accessing a subfield within storage.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TyStorageAccessDescriptor {
pub name: Ident,
pub(crate) type_id: TypeId,
pub(crate) span: Span,
}
12 changes: 5 additions & 7 deletions sway-core/src/semantic_analysis/ast_node/declaration/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ use crate::{
},
language::ty,
metadata::MetadataManager,
semantic_analysis::{
TyStructField, TypeCheckedStorageAccess, TypeCheckedStorageAccessDescriptor,
},
semantic_analysis::TyStructField,
type_system::{look_up_type_id, TypeId, TypeInfo},
AttributesMap, Ident,
};
Expand Down Expand Up @@ -47,7 +45,7 @@ impl TyStorageDeclaration {
&self,
fields: Vec<Ident>,
storage_fields: &[TyStorageField],
) -> CompileResult<(TypeCheckedStorageAccess, TypeId)> {
) -> CompileResult<(ty::TyStorageAccess, TypeId)> {
let mut errors = vec![];
let warnings = vec![];

Expand All @@ -74,7 +72,7 @@ impl TyStorageDeclaration {
}
};

type_checked_buf.push(TypeCheckedStorageAccessDescriptor {
type_checked_buf.push(ty::TyStorageAccessDescriptor {
name: first_field.clone(),
type_id: *initial_field_type,
span: first_field.span(),
Expand All @@ -99,7 +97,7 @@ impl TyStorageDeclaration {
.find(|x| x.name.as_str() == field.as_str())
{
Some(struct_field) => {
type_checked_buf.push(TypeCheckedStorageAccessDescriptor {
type_checked_buf.push(ty::TyStorageAccessDescriptor {
name: field.clone(),
type_id: struct_field.type_id,
span: field.span().clone(),
Expand All @@ -125,7 +123,7 @@ impl TyStorageDeclaration {

ok(
(
TypeCheckedStorageAccess {
ty::TyStorageAccess {
fields: type_checked_buf,
ix,
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,44 +1,13 @@
use crate::{language::ty, type_system::*};

use sway_types::{state::StateIndex, Ident, Span, Spanned};
use sway_types::Ident;

#[derive(Clone, Debug)]
pub struct ContractCallParams {
pub(crate) func_selector: [u8; 4],
pub(crate) contract_address: Box<ty::TyExpression>,
}

/// Describes the full storage access including all the subfields
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TypeCheckedStorageAccess {
pub fields: Vec<TypeCheckedStorageAccessDescriptor>,
pub(crate) ix: StateIndex,
}

impl Spanned for TypeCheckedStorageAccess {
fn span(&self) -> Span {
self.fields
.iter()
.fold(self.fields[0].span.clone(), |acc, field| {
Span::join(acc, field.span.clone())
})
}
}

impl TypeCheckedStorageAccess {
pub fn storage_field_name(&self) -> Ident {
self.fields[0].name.clone()
}
}

/// Describes a single subfield access in the sequence when accessing a subfield within storage.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TypeCheckedStorageAccessDescriptor {
pub name: Ident,
pub(crate) type_id: TypeId,
pub(crate) span: Span,
}

#[derive(Clone, Debug)]
pub struct TyAsmRegisterDeclaration {
pub(crate) initializer: Option<ty::TyExpression>,
Expand Down
4 changes: 2 additions & 2 deletions sway-core/src/semantic_analysis/namespace/items.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
declaration_engine::{declaration_engine::de_get_storage, declaration_id::DeclarationId},
error::*,
language::CallPath,
language::{ty, CallPath},
namespace::*,
semantic_analysis::*,
type_system::*,
Expand Down Expand Up @@ -59,7 +59,7 @@ impl Items {
fields: Vec<Ident>,
storage_fields: &[TyStorageField],
access_span: &Span,
) -> CompileResult<(TypeCheckedStorageAccess, TypeId)> {
) -> CompileResult<(ty::TyStorageAccess, TypeId)> {
let mut warnings = vec![];
let mut errors = vec![];
match self.declared_storage {
Expand Down

0 comments on commit d51a24e

Please sign in to comment.