1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
use crate::source_location_accessor::SourceLocationAccessor;
use crate::type_id::{NodeTypeId, NodeTypeIdAccessor};
use crate::SourceLocation;
use std::collections::HashMap;
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
struct Key {
type_id: NodeTypeId,
loc: SourceLocation,
}
impl Key {
fn new<NodeT>(node: &NodeT) -> Self
where
NodeT: SourceLocationAccessor + NodeTypeIdAccessor,
{
Self {
type_id: node.get_type_id(),
loc: node.get_loc(),
}
}
}
impl NodeTypeIdAccessor for Key {
fn get_type_id(&self) -> NodeTypeId {
self.type_id
}
}
impl SourceLocationAccessor for Key {
fn set_loc(&mut self, start: SourceLocation, end: SourceLocation) {
self.loc.start = start.start;
self.loc.end = end.end;
}
fn get_loc(&self) -> SourceLocation {
self.loc
}
}
#[derive(Debug)]
struct Item<ValueT> {
#[allow(dead_code)]
key: Key,
value: ValueT,
}
impl<ValueT> Item<ValueT> {
fn new(key: Key, value: ValueT) -> Self {
Self { key, value }
}
}
type ItemIndex = usize;
#[derive(Debug)]
pub struct AssociatedDataItemIndex {
index: ItemIndex,
}
impl AssociatedDataItemIndex {
fn new(index: ItemIndex) -> Self {
Self { index }
}
}
// A map from AST node to `ValueT`, to associate extra data to AST node.
#[derive(Debug)]
pub struct AssociatedData<ValueT> {
items: Vec<Item<ValueT>>,
map: HashMap<Key, ItemIndex>,
}
impl<ValueT> AssociatedData<ValueT> {
pub fn new() -> Self {
Self {
items: Vec::new(),
map: HashMap::new(),
}
}
// Insert an item for `node`.
// Returns the index of the inserted item.
pub fn insert<NodeT>(&mut self, node: &NodeT, value: ValueT) -> AssociatedDataItemIndex
where
NodeT: SourceLocationAccessor + NodeTypeIdAccessor,
{
let index = self.items.len();
let key = Key::new(node);
self.items.push(Item::new(key.clone(), value));
self.map.insert(key, index);
AssociatedDataItemIndex::new(index)
}
// Get the immutable reference of the item for the index.
// The index is the return value of `insert` or `to_index`.
pub fn get_by_index(&self, index: AssociatedDataItemIndex) -> &ValueT {
// NOTE: This can panic if `index` is created by another instance of
// `AssociatedData`.
&self.items[index.index].value
}
// Get the mutable reference of the item for the index.
// The index is the return value of `insert` or `to_index`.
pub fn get_mut_by_index(&mut self, index: AssociatedDataItemIndex) -> &mut ValueT {
// NOTE: This can panic if `index` is created by another instance of
// `AssociatedData`.
&mut self.items[index.index].value
}
// Get the immutable reference of the item for `node`.
// `None` if there's no item inserted for `node`.
pub fn get<NodeT>(&self, node: &NodeT) -> Option<&ValueT>
where
NodeT: SourceLocationAccessor + NodeTypeIdAccessor,
{
self.to_index(node)
.and_then(|index| Some(self.get_by_index(index)))
}
// Get the mutable reference of the item for `node`.
// `None` if there's no item inserted for `node`.
pub fn get_mut<NodeT>(&mut self, node: &NodeT) -> Option<&mut ValueT>
where
NodeT: SourceLocationAccessor + NodeTypeIdAccessor,
{
self.to_index(node)
.and_then(move |index| Some(self.get_mut_by_index(index)))
}
// Returns the index for the item for `node`.
// `None` if there's no item inserted for `node`.
pub fn to_index<NodeT>(&self, node: &NodeT) -> Option<AssociatedDataItemIndex>
where
NodeT: SourceLocationAccessor + NodeTypeIdAccessor,
{
let key = Key::new(node);
match self.map.get(&key) {
Some(index) => Some(AssociatedDataItemIndex::new(*index)),
None => None,
}
}
}
|