Skip to content

Commit

Permalink
refactor: reduce 1 Rc<RefCell<>>
Browse files Browse the repository at this point in the history
  • Loading branch information
RanolP committed Nov 14, 2023
1 parent 0f3099a commit 8a72dfb
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 32 deletions.
30 changes: 16 additions & 14 deletions crates/psl/src/codegen/construct/scope.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
use std::{cell::RefCell, collections::HashMap, rc::Rc};

use crate::codegen::pass::NamesResolved;

use super::Type;

pub struct Scope {
parent: Option<Rc<RefCell<Scope>>>,
root: Rc<RefCell<NamesResolved>>,

parent: Option<usize>,

variable_names: HashMap<String, Type>,
}

impl Scope {
pub fn new() -> Scope {
pub fn new(root: Rc<RefCell<NamesResolved>>) -> Scope {
Scope {
root,
parent: None,

variable_names: HashMap::new(),
}
}

Check warning on line 22 in crates/psl/src/codegen/construct/scope.rs

View check run for this annotation

Codecov / codecov/patch

crates/psl/src/codegen/construct/scope.rs#L16-L22

Added lines #L16 - L22 were not covered by tests

pub fn create_subscope(scope: Rc<RefCell<Scope>>) -> Scope {
pub fn with_parentt(root: Rc<RefCell<NamesResolved>>, parent: usize) -> Scope {
Scope {
parent: Some(scope.clone()),
root,
parent: Some(parent),

variable_names: HashMap::new(),
}
Expand All @@ -31,15 +36,12 @@ impl Scope {

pub fn get_variable_type(&self, name: &String) -> Option<Type> {
self.variable_names.get(name).cloned().or_else(|| {
self.parent
.as_deref()
.and_then(|parent| parent.borrow().get_variable_type(name))
self.parent.and_then(|parent| {
self.root
.borrow()
.get(&parent)
.and_then(|parent| parent.get_variable_type(name))
})
})
}

Check warning on line 46 in crates/psl/src/codegen/construct/scope.rs

View check run for this annotation

Codecov / codecov/patch

crates/psl/src/codegen/construct/scope.rs#L37-L46

Added lines #L37 - L46 were not covered by tests
}

impl Default for Scope {
fn default() -> Self {
Scope::new()
}
}
6 changes: 3 additions & 3 deletions crates/psl/src/codegen/context.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{cell::Ref, hash::Hash};
use std::hash::Hash;

use super::{construct::Scope, pass::NamesResolved};

Expand All @@ -13,7 +13,7 @@ impl CodegenContext {
}
}

pub fn scope<T: Hash + 'static>(&self, node: &T) -> Ref<Scope> {
self.name_resolution.get(node).unwrap().borrow()
pub fn scope<T: Hash + 'static>(&self, node: &T) -> &Scope {
self.name_resolution.get(node).unwrap()

Check warning on line 17 in crates/psl/src/codegen/context.rs

View check run for this annotation

Codecov / codecov/patch

crates/psl/src/codegen/context.rs#L16-L17

Added lines #L16 - L17 were not covered by tests
}
}
45 changes: 30 additions & 15 deletions crates/psl/src/codegen/pass/name_resolution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ impl<T: NameResolutionPass> NameResolutionPass for Option<T> {
type AstKey = (TypeId, u64);

pub struct NamesResolved {
resolved: HashMap<AstKey, Rc<RefCell<Scope>>>,
vec: Vec<Scope>,
resolved: HashMap<AstKey, usize>,
}

impl NamesResolved {
Expand All @@ -42,46 +43,60 @@ impl NamesResolved {
(ty, hash)
}

Check warning on line 44 in crates/psl/src/codegen/pass/name_resolution.rs

View check run for this annotation

Codecov / codecov/patch

crates/psl/src/codegen/pass/name_resolution.rs#L36-L44

Added lines #L36 - L44 were not covered by tests

pub fn get<K: Hash + 'static>(&self, key: &K) -> Option<&Rc<RefCell<Scope>>> {
self.resolved.get(&Self::make_keys(key))
pub fn insert(&mut self, scope: Scope) -> usize {
self.vec.push(scope);
self.vec.len() - 1
}

Check warning on line 49 in crates/psl/src/codegen/pass/name_resolution.rs

View check run for this annotation

Codecov / codecov/patch

crates/psl/src/codegen/pass/name_resolution.rs#L46-L49

Added lines #L46 - L49 were not covered by tests

pub fn set<K: Hash + 'static>(&mut self, key: &K, scope: Rc<RefCell<Scope>>) {
self.resolved.insert(Self::make_keys(key), scope);
pub fn get<K: Hash + 'static>(&self, key: &K) -> Option<&Scope> {
self.resolved
.get(&Self::make_keys(key))
.and_then(|idx| self.vec.get(*idx))
}

Check warning on line 55 in crates/psl/src/codegen/pass/name_resolution.rs

View check run for this annotation

Codecov / codecov/patch

crates/psl/src/codegen/pass/name_resolution.rs#L51-L55

Added lines #L51 - L55 were not covered by tests

pub fn set<K: Hash + 'static>(&mut self, key: &K, idx: usize) {
self.resolved.insert(Self::make_keys(key), idx);
}

Check warning on line 59 in crates/psl/src/codegen/pass/name_resolution.rs

View check run for this annotation

Codecov / codecov/patch

crates/psl/src/codegen/pass/name_resolution.rs#L57-L59

Added lines #L57 - L59 were not covered by tests
}

pub struct NameResolutionContext {
// as we use single-threaded codegen, there must be no more than 1 mutator.
root: Rc<RefCell<NamesResolved>>,
scope: Rc<RefCell<Scope>>,
scope: usize,
}

impl NameResolutionContext {
pub fn new() -> Self {
NameResolutionContext {
root: Rc::new(RefCell::new(NamesResolved {
resolved: HashMap::new(),
})),
scope: Rc::new(RefCell::new(Scope::new())),
}
let root = Rc::new(RefCell::new(NamesResolved {
vec: Vec::new(),
resolved: HashMap::new(),
}));
let scope = root.borrow_mut().insert(Scope::new(Rc::clone(&root)));

NameResolutionContext { root, scope }
}

Check warning on line 77 in crates/psl/src/codegen/pass/name_resolution.rs

View check run for this annotation

Codecov / codecov/patch

crates/psl/src/codegen/pass/name_resolution.rs#L69-L77

Added lines #L69 - L77 were not covered by tests

pub fn visit<T: NameResolutionPass + Hash + 'static>(&mut self, node: &T) {
self.root.borrow_mut().set(node, Rc::clone(&self.scope));
self.root.borrow_mut().set(node, self.scope);

node.resolve(self);
}

Check warning on line 83 in crates/psl/src/codegen/pass/name_resolution.rs

View check run for this annotation

Codecov / codecov/patch

crates/psl/src/codegen/pass/name_resolution.rs#L79-L83

Added lines #L79 - L83 were not covered by tests

pub fn create_subscope(&mut self) -> NameResolutionContext {
let new_scope = self
.root
.borrow_mut()
.insert(Scope::with_parentt(Rc::clone(&self.root), self.scope));
NameResolutionContext {
root: Rc::clone(&self.root),
scope: Rc::new(RefCell::new(Scope::create_subscope(Rc::clone(&self.scope)))),
scope: new_scope,
}
}

Check warning on line 94 in crates/psl/src/codegen/pass/name_resolution.rs

View check run for this annotation

Codecov / codecov/patch

crates/psl/src/codegen/pass/name_resolution.rs#L85-L94

Added lines #L85 - L94 were not covered by tests

pub fn scope_mut(&mut self) -> RefMut<Scope> {
self.scope.borrow_mut()
RefMut::map(self.root.borrow_mut(), |root| {
root.vec.get_mut(self.scope).unwrap()
})
}

Check warning on line 100 in crates/psl/src/codegen/pass/name_resolution.rs

View check run for this annotation

Codecov / codecov/patch

crates/psl/src/codegen/pass/name_resolution.rs#L96-L100

Added lines #L96 - L100 were not covered by tests

pub fn finish(self) -> NamesResolved {
Expand Down

0 comments on commit 8a72dfb

Please sign in to comment.