Skip to content

Commit

Permalink
refactor: Use Arc instead of Rc
Browse files Browse the repository at this point in the history
  • Loading branch information
sbwtw committed Nov 8, 2024
1 parent 69d31e1 commit ec959f1
Show file tree
Hide file tree
Showing 13 changed files with 120 additions and 94 deletions.
10 changes: 5 additions & 5 deletions lib/src/ast/declaration_statement.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::ast::*;
use crate::parser::TokenKind;
use crate::utils::HasAttribute;
use std::rc::Rc;
use std::sync::Arc;

const EMPTY_VARIABLES: &[Rc<Variable>] = &[];
const EMPTY_VARIABLES: &[Arc<Variable>] = &[];

#[derive(Debug)]
pub enum DeclKind {
Expand Down Expand Up @@ -88,7 +88,7 @@ impl Declaration {
}

#[inline]
pub fn variables(&self) -> &[Rc<Variable>] {
pub fn variables(&self) -> &[Arc<Variable>] {
match self.kind {
DeclKind::Fun(ref f) => f.parameters(),
DeclKind::Struct(ref s) => s.variables(),
Expand Down Expand Up @@ -125,7 +125,7 @@ impl Declaration {
}

#[inline]
pub fn new_struct(name: StString, variables: SmallVec8<Rc<Variable>>) -> Self {
pub fn new_struct(name: StString, variables: SmallVec8<Arc<Variable>>) -> Self {
Self::struct_(Box::new(StructDeclare::new(name, variables)))
}

Expand All @@ -137,7 +137,7 @@ impl Declaration {
}

#[inline]
pub fn new_enum(name: StString, ty: Option<Type>, fields: SmallVec8<Rc<Variable>>) -> Self {
pub fn new_enum(name: StString, ty: Option<Type>, fields: SmallVec8<Arc<Variable>>) -> Self {
Self::enum_(Box::new(EnumDeclare::new(name, ty, fields)))
}

Expand Down
8 changes: 4 additions & 4 deletions lib/src/ast/function_declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ use crate::impl_ast_display;
use crate::impl_has_attribute;
use crate::prelude::*;
use crate::utils::AttrMap8;
use std::rc::Rc;
use std::sync::Arc;

#[derive(Debug)]
pub struct FunctionDeclare {
name: StString,
decl_class: DeclareClass,
return_type: Option<Type>,
parameters: SmallVec8<Rc<Variable>>,
parameters: SmallVec8<Arc<Variable>>,
attributes: AttrMap8,
}

Expand All @@ -21,7 +21,7 @@ impl FunctionDeclare {
name: StString,
class: DeclareClass,
ty: Option<Type>,
variables: SmallVec8<Rc<Variable>>,
variables: SmallVec8<Arc<Variable>>,
) -> Self {
Self {
name,
Expand All @@ -44,7 +44,7 @@ impl FunctionDeclare {
&self.return_type
}

pub fn parameters(&self) -> &[Rc<Variable>] {
pub fn parameters(&self) -> &[Arc<Variable>] {
self.parameters.as_slice()
}
}
8 changes: 4 additions & 4 deletions lib/src/ast/global_variable_declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ use crate::ast::{SmallVec8, Variable};
use crate::impl_has_attribute;
use crate::parser::StString;
use crate::utils::AttrMap8;
use std::rc::Rc;
use std::sync::Arc;

#[derive(Debug)]
pub struct GlobalVariableDeclare {
name: StString,
variables: SmallVec8<Rc<Variable>>,
variables: SmallVec8<Arc<Variable>>,
attributes: AttrMap8,
}

impl_has_attribute!(GlobalVariableDeclare, attributes);

impl GlobalVariableDeclare {
pub fn new(name: Option<StString>, variables: SmallVec8<Rc<Variable>>) -> Self {
pub fn new(name: Option<StString>, variables: SmallVec8<Arc<Variable>>) -> Self {
Self {
name: name.unwrap_or(StString::empty()),
variables,
Expand All @@ -26,7 +26,7 @@ impl GlobalVariableDeclare {
&self.name
}

pub fn variables(&self) -> &[Rc<Variable>] {
pub fn variables(&self) -> &[Arc<Variable>] {
self.variables.as_slice()
}
}
53 changes: 39 additions & 14 deletions lib/src/ast/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use crate::parser::{LiteralValue, Operator, StString};
use bitflags::bitflags;
use smallvec::SmallVec;
use std::cell::RefCell;
use std::fmt::{self, Debug, Display, Formatter};
use std::hash::{Hash, Hasher};
use std::rc::Rc;

use crate::parser::{LiteralValue, Operator, StString};
use std::sync::{Arc, RwLock};

mod message;
pub use message::*;
Expand Down Expand Up @@ -131,13 +129,13 @@ impl_into_expression!(LiteralValue, |x| Expression::literal(Box::new(

#[derive(Clone, Debug)]
pub struct Type {
inner: Rc<TypeInner>,
inner: Arc<TypeInner>,
}

impl Type {
pub fn from_class(class: TypeClass) -> Self {
Self {
inner: Rc::new(TypeInner { class }),
inner: Arc::new(TypeInner { class }),
}
}

Expand Down Expand Up @@ -288,7 +286,7 @@ pub enum UserTypeClass {
Union,
}

#[derive(Debug, Eq, PartialEq, Clone)]
#[derive(Debug, Clone)]
pub enum TypeClass {
/// 'BIT', one bit type
Bit,
Expand Down Expand Up @@ -317,11 +315,36 @@ pub enum TypeClass {
/// 'STRING' string type
String,
/// UserType
UserType(RefCell<UserType>),
UserType(Arc<RwLock<UserType>>),
/// ArrayType
Array(RefCell<ArrayType>),
Array(Arc<RwLock<ArrayType>>),
}

impl PartialEq for TypeClass {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::Int, Self::Int) => true,
(Self::Bit, Self::Bit) => true,
(Self::Bool, Self::Bool) => true,
(Self::SInt, Self::SInt) => true,
(Self::Byte, Self::Byte) => true,
(Self::UInt, Self::UInt) => true,
(Self::UDInt, Self::UDInt) => true,
(Self::ULInt, Self::ULInt) => true,
(Self::Real, Self::Real) => true,
(Self::LReal, Self::LReal) => true,
(Self::String, Self::String) => true,
(Self::UserType(a), Self::UserType(b)) => {
a.read().unwrap().name() == b.read().unwrap().name()
}
(Self::Array(a), Self::Array(b)) => false,
_ => false,
}
}
}

impl Eq for TypeClass {}

impl Hash for TypeClass {
fn hash<H: Hasher>(&self, state: &mut H) {
let tag = match self {
Expand All @@ -338,11 +361,13 @@ impl Hash for TypeClass {
// TODO: incomplete implements
match self {
TypeClass::UserType(user_type) => {
user_type.borrow().name().hash(state);
// user_type.borrow().name().hash(state);
user_type.read().unwrap().name().hash(state);
}
TypeClass::Array(array_type) => {
let array_type = array_type.borrow();
let base_type = array_type.base_type().borrow();
// let array_type = array_type.borrow();
let array_type = array_type.read().unwrap();
let base_type = array_type.base_type().read().unwrap();
base_type.type_class().hash(state);
}
_ => {}
Expand All @@ -366,8 +391,8 @@ impl Display for TypeClass {
TypeClass::Real => write!(f, "REAL"),
TypeClass::LReal => write!(f, "LREAL"),
TypeClass::String => write!(f, "STRING"),
TypeClass::UserType(u) => write!(f, "{}", u.borrow()),
TypeClass::Array(arr) => write!(f, "{}", arr.borrow()),
TypeClass::UserType(u) => write!(f, "{}", u.read().unwrap()),
TypeClass::Array(arr) => write!(f, "{}", arr.read().unwrap()),
}
}
}
27 changes: 13 additions & 14 deletions lib/src/ast/types.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::rc::Rc;

use crate::{ast::*, impl_has_attribute, utils::AttrMap8};
use std::sync::Arc;

macro_rules! builtin_type_impl {
(struct $name:ident, $class:expr) => {
Expand Down Expand Up @@ -60,7 +59,7 @@ impl UserType {

impl From<UserType> for Type {
fn from(value: UserType) -> Self {
let class = TypeClass::UserType(RefCell::new(value));
let class = TypeClass::UserType(Arc::new(RwLock::new(value)));

Type::from_class(class)
}
Expand All @@ -76,14 +75,14 @@ impl Display for UserType {
pub struct EnumDeclare {
name: StString,
ty: Option<Type>,
fields: SmallVec8<Rc<Variable>>,
fields: SmallVec8<Arc<Variable>>,
attributes: AttrMap8,
}

impl_has_attribute!(EnumDeclare, attributes);

impl EnumDeclare {
pub fn new(name: StString, ty: Option<Type>, fields: SmallVec8<Rc<Variable>>) -> Self {
pub fn new(name: StString, ty: Option<Type>, fields: SmallVec8<Arc<Variable>>) -> Self {
Self {
name,
ty,
Expand All @@ -100,7 +99,7 @@ impl EnumDeclare {
&self.ty
}

pub fn fields(&self) -> &[Rc<Variable>] {
pub fn fields(&self) -> &[Arc<Variable>] {
&self.fields
}
}
Expand Down Expand Up @@ -143,14 +142,14 @@ impl AliasDeclare {
#[derive(Debug)]
pub struct StructDeclare {
name: StString,
variables: SmallVec8<Rc<Variable>>,
variables: SmallVec8<Arc<Variable>>,
attributes: AttrMap8,
}

impl_has_attribute!(StructDeclare, attributes);

impl StructDeclare {
pub fn new(name: StString, variables: SmallVec8<Rc<Variable>>) -> Self {
pub fn new(name: StString, variables: SmallVec8<Arc<Variable>>) -> Self {
Self {
name,
variables,
Expand All @@ -162,11 +161,11 @@ impl StructDeclare {
&self.name
}

pub fn variables(&self) -> &[Rc<Variable>] {
pub fn variables(&self) -> &[Arc<Variable>] {
self.variables.as_slice()
}

pub fn variables_mut(&mut self) -> &mut [Rc<Variable>] {
pub fn variables_mut(&mut self) -> &mut [Arc<Variable>] {
self.variables.as_mut_slice()
}
}
Expand All @@ -179,20 +178,20 @@ impl StructDeclare {

#[derive(Debug)]
pub struct ArrayType {
base_type: RefCell<Type>,
base_type: Arc<RwLock<Type>>,
dimensions: SmallVec3<RangeExpression>,
}

impl ArrayType {
pub fn new(base: Type, dimensions: SmallVec3<RangeExpression>) -> Self {
Self {
base_type: RefCell::new(base),
base_type: Arc::new(RwLock::new(base)),
dimensions,
}
}

#[inline]
pub fn base_type(&self) -> &RefCell<Type> {
pub fn base_type(&self) -> &Arc<RwLock<Type>> {
&self.base_type
}
}
Expand Down Expand Up @@ -220,7 +219,7 @@ impl Clone for ArrayType {

impl From<ArrayType> for Type {
fn from(value: ArrayType) -> Self {
let class = TypeClass::Array(RefCell::new(value));
let class = TypeClass::Array(Arc::new(RwLock::new(value)));

Type::from_class(class)
}
Expand Down
12 changes: 6 additions & 6 deletions lib/src/ast/variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::impl_has_attribute;
use crate::parser::StString;
use crate::utils::AttrMap8;
use std::default::Default;
use std::rc::Rc;
use std::sync::Arc;

#[derive(Debug)]
pub struct Variable {
Expand Down Expand Up @@ -42,10 +42,10 @@ impl Variable {
pub fn multiple_variable_with_type(
names: SmallVec8<StString>,
ty: Type,
) -> SmallVec8<Rc<Self>> {
) -> SmallVec8<Arc<Self>> {
names
.iter()
.map(|x| Rc::new(Self::with_type(x.clone(), ty.clone())))
.map(|x| Arc::new(Self::with_type(x.clone(), ty.clone())))
.collect()
}

Expand Down Expand Up @@ -109,10 +109,10 @@ pub struct VariableDeclareGroup;
impl VariableDeclareGroup {
pub fn from_variables(
flags: VariableFlags,
mut vars: SmallVec8<Rc<Variable>>,
) -> SmallVec8<Rc<Variable>> {
mut vars: SmallVec8<Arc<Variable>>,
) -> SmallVec8<Arc<Variable>> {
for v in vars.iter_mut() {
Rc::get_mut(v).unwrap().set_flags(flags);
Arc::get_mut(v).unwrap().set_flags(flags);
}

vars
Expand Down
Loading

0 comments on commit ec959f1

Please sign in to comment.