Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Serde for AstNode types #6605

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion sway-ast/src/intrinsics.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use serde::{Deserialize, Serialize};
use std::fmt;

#[derive(Eq, PartialEq, Debug, Clone, Hash)]
#[derive(Eq, PartialEq, Debug, Clone, Hash, Serialize, Deserialize)]
pub enum Intrinsic {
IsReferenceType,
SizeOfType,
Expand Down
14 changes: 7 additions & 7 deletions sway-ast/src/literal.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
use crate::priv_prelude::*;

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash, Serialize)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash, Serialize, Deserialize)]
pub struct LitString {
pub span: Span,
pub parsed: String,
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash, Serialize)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash, Serialize, Deserialize)]
pub struct LitChar {
pub span: Span,
pub parsed: char,
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash, Serialize)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash, Serialize, Deserialize)]
pub struct LitInt {
pub span: Span,
pub parsed: BigUint,
pub ty_opt: Option<(LitIntType, Span)>,
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash, Serialize)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash, Serialize, Deserialize)]
pub enum LitIntType {
U8,
U16,
Expand All @@ -32,13 +32,13 @@ pub enum LitIntType {
I64,
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash, Serialize)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash, Serialize, Deserialize)]
pub struct LitBool {
pub span: Span,
pub kind: LitBoolType,
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash, Serialize)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash, Serialize, Deserialize)]
pub enum LitBoolType {
True,
False,
Expand All @@ -53,7 +53,7 @@ impl From<LitBoolType> for bool {
}
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash, Serialize)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash, Serialize, Deserialize)]
pub enum Literal {
String(LitString),
Char(LitChar),
Expand Down
2 changes: 1 addition & 1 deletion sway-ast/src/priv_prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub use {
},
extension_trait::extension_trait,
num_bigint::BigUint,
serde::Serialize,
serde::{Deserialize, Serialize},
sway_types::{
ast::{Delimiter, PunctKind},
Ident, Span, Spanned,
Expand Down
2 changes: 1 addition & 1 deletion sway-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ graph-cycles.workspace = true
hashbrown.workspace = true
hex = { workspace = true, optional = true }
im.workspace = true
indexmap.workspace = true
indexmap = { workspace = true, features = ["serde"] }
itertools.workspace = true
lazy_static.workspace = true
object = { workspace = true, features = ["write"] }
Expand Down
41 changes: 30 additions & 11 deletions sway-core/src/decl_engine/id.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
use std::collections::hash_map::DefaultHasher;
use std::hash::Hasher;
use std::marker::PhantomData;
use std::{fmt, hash::Hash};

use sway_types::{Named, Spanned};

use crate::language::ty::{TyDeclParsedType, TyTraitType};
use crate::{
decl_engine::*,
engine_threading::*,
language::ty::{
TyEnumDecl, TyFunctionDecl, TyImplSelfOrTrait, TyStructDecl, TyTraitDecl, TyTraitFn,
TyTypeAliasDecl,
TyDeclParsedType, TyEnumDecl, TyFunctionDecl, TyImplSelfOrTrait, TyStructDecl, TyTraitDecl,
TyTraitFn, TyTraitType, TyTypeAliasDecl,
},
type_system::*,
};
use serde::{Deserialize, Serialize};
use std::{
collections::hash_map::DefaultHasher,
fmt,
hash::{Hash, Hasher},
marker::PhantomData,
};
use sway_types::{Named, Spanned};

pub type DeclIdIndexType = usize;

Expand All @@ -27,7 +27,7 @@ impl<T> fmt::Debug for DeclId<T> {
}
}

#[derive(Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Debug)]
#[derive(Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Debug, Serialize, Deserialize)]
pub struct DeclUniqueId(pub(crate) u64);

impl<T> DeclId<T> {
Expand Down Expand Up @@ -258,3 +258,22 @@ where
}
}
}

impl<T> Serialize for DeclId<T> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
self.0.serialize(serializer)
}
}

impl<'de, T> Deserialize<'de> for DeclId<T> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let id = DeclIdIndexType::deserialize(deserializer)?;
Ok(DeclId::new(id))
}
}
6 changes: 3 additions & 3 deletions sway-core/src/decl_engine/interface_decl_id.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::{parsed_engine::ParsedDeclEngineGet, parsed_id::ParsedDeclId};
use crate::{
decl_engine::*,
engine_threading::{EqWithEngines, PartialEqWithEngines, PartialEqWithEnginesContext},
Expand All @@ -6,8 +7,7 @@ use crate::{
ty,
},
};

use super::{parsed_engine::ParsedDeclEngineGet, parsed_id::ParsedDeclId};
use serde::{Deserialize, Serialize};

#[derive(Debug, Eq, PartialEq, Hash, Clone)]
pub enum ParsedInterfaceDeclId {
Expand Down Expand Up @@ -43,7 +43,7 @@ impl From<ParsedDeclId<TraitDeclaration>> for ParsedInterfaceDeclId {
}
}

#[derive(Debug, Eq, PartialEq, Hash, Clone)]
#[derive(Debug, Eq, PartialEq, Hash, Clone, Serialize, Deserialize)]
pub enum InterfaceDeclId {
Abi(DeclId<ty::TyAbiDecl>),
Trait(DeclId<ty::TyTraitDecl>),
Expand Down
47 changes: 35 additions & 12 deletions sway-core/src/decl_engine/parsed_id.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
use std::hash::{DefaultHasher, Hasher};
use std::marker::PhantomData;
use std::{fmt, hash::Hash};

use sway_types::{Named, Spanned};

use crate::engine_threading::{
EqWithEngines, HashWithEngines, PartialEqWithEngines, PartialEqWithEnginesContext,
use super::{
parsed_engine::{ParsedDeclEngine, ParsedDeclEngineGet, ParsedDeclEngineIndex},
DeclUniqueId,
};
use crate::Engines;

use super::parsed_engine::{ParsedDeclEngine, ParsedDeclEngineGet, ParsedDeclEngineIndex};
use super::DeclUniqueId;
use crate::{
engine_threading::{
EqWithEngines, HashWithEngines, PartialEqWithEngines, PartialEqWithEnginesContext,
},
Engines,
};
use serde::{Deserialize, Serialize};
use std::{
hash::{DefaultHasher, Hasher},
marker::PhantomData,
{fmt, hash::Hash},
};
use sway_types::{Named, Spanned};

pub type ParsedDeclIdIndexType = usize;

Expand Down Expand Up @@ -91,6 +95,25 @@ impl<T> Ord for ParsedDeclId<T> {
}
}

impl<T> Serialize for ParsedDeclId<T> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
self.0.serialize(serializer)
}
}

impl<'de, T> Deserialize<'de> for ParsedDeclId<T> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let id = usize::deserialize(deserializer)?;
Ok(ParsedDeclId::new(id))
}
}

impl<T> ParsedDeclId<T> {
pub(crate) fn new(id: usize) -> Self {
ParsedDeclId(id, PhantomData)
Expand Down
11 changes: 5 additions & 6 deletions sway-core/src/decl_engine/ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@
//! `fn my_function() { .. }`, and to use [DeclRef] for cases like function
//! application `my_function()`.

use std::hash::{Hash, Hasher};

use sway_error::handler::{ErrorEmitted, Handler};
use sway_types::{Ident, Named, Span, Spanned};

use crate::{
decl_engine::*,
engine_threading::*,
Expand All @@ -35,6 +30,10 @@ use crate::{
semantic_analysis::TypeCheckContext,
type_system::*,
};
use serde::{Deserialize, Serialize};
use std::hash::{Hash, Hasher};
use sway_error::handler::{ErrorEmitted, Handler};
use sway_types::{Ident, Named, Span, Spanned};

pub type DeclRefFunction = DeclRef<DeclId<TyFunctionDecl>>;
pub type DeclRefTrait = DeclRef<DeclId<TyTraitDecl>>;
Expand All @@ -53,7 +52,7 @@ pub type DeclRefMixedInterface = DeclRef<InterfaceDeclId>;
/// Represents the use of / syntactic reference to a declaration. A
/// smart-wrapper around a [DeclId], containing additional information about a
/// declaration.
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DeclRef<I> {
/// The name of the declaration.
// NOTE: In the case of storage, the name is "storage".
Expand Down
6 changes: 3 additions & 3 deletions sway-core/src/language/asm.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use serde::{Deserialize, Serialize};
use std::hash::{Hash, Hasher};

use sway_types::{BaseIdent, Ident, Span};

#[derive(Debug, Clone, Eq)]
#[derive(Debug, Clone, Eq, Serialize, Deserialize)]
pub struct AsmOp {
pub(crate) op_name: Ident,
pub(crate) op_args: Vec<Ident>,
Expand Down Expand Up @@ -43,7 +43,7 @@ impl PartialEq for AsmOp {
}
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct AsmRegister {
pub(crate) name: String,
}
Expand Down
24 changes: 11 additions & 13 deletions sway-core/src/language/call_path.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
use std::{
cmp::Ordering,
fmt,
hash::{Hash, Hasher},
sync::Arc,
};

use crate::{
engine_threading::{
DebugWithEngines, DisplayWithEngines, EqWithEngines, HashWithEngines, OrdWithEngines,
OrdWithEnginesContext, PartialEqWithEngines, PartialEqWithEnginesContext,
},
parsed::QualifiedPathType,
Engines, Ident, Namespace,
};

use serde::{Deserialize, Serialize};
use std::{
cmp::Ordering,
fmt,
hash::{Hash, Hasher},
sync::Arc,
};
use sway_error::{
error::CompileError,
handler::{ErrorEmitted, Handler},
};
use sway_types::{span::Span, Spanned};

use super::parsed::QualifiedPathType;

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct CallPathTree {
pub qualified_call_path: QualifiedCallPath,
pub children: Vec<CallPathTree>,
Expand Down Expand Up @@ -75,7 +73,7 @@ impl OrdWithEngines for CallPathTree {
}
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize, Deserialize)]

pub struct QualifiedCallPath {
pub call_path: CallPath,
Expand Down Expand Up @@ -179,7 +177,7 @@ impl DebugWithEngines for QualifiedCallPath {

/// In the expression `a::b::c()`, `a` and `b` are the prefixes and `c` is the suffix.
/// `c` can be any type `T`, but in practice `c` is either an `Ident` or a `TypeInfo`.
#[derive(Debug, Clone, Eq, PartialEq, Hash, Ord, PartialOrd)]
#[derive(Debug, Clone, Eq, PartialEq, Hash, Ord, PartialOrd, Serialize, Deserialize)]
pub struct CallPath<T = Ident> {
pub prefixes: Vec<Ident>,
pub suffix: T,
Expand Down
4 changes: 3 additions & 1 deletion sway-core/src/language/lazy_op.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum LazyOp {
And,
Or,
Expand Down
9 changes: 4 additions & 5 deletions sway-core/src/language/literal.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
use crate::{type_system::*, Engines};

use sway_error::error::CompileError;
use sway_types::{integer_bits::IntegerBits, span, u256::U256};

use serde::{Deserialize, Serialize};
use std::{
fmt,
hash::{Hash, Hasher},
num::{IntErrorKind, ParseIntError},
};
use sway_error::error::CompileError;
use sway_types::{integer_bits::IntegerBits, span, u256::U256};

#[derive(Debug, Clone, Eq)]
#[derive(Debug, Clone, Eq, Serialize, Deserialize)]
pub enum Literal {
U8(u8),
U16(u16),
Expand Down
Loading
Loading