Skip to content

Commit 6dd7ed9

Browse files
ghaithmhasel
andcommitted
Add support for a scoped index
The scoped index contains new variables and datatypes that could later be merged into the main index. Any ast statetment that could declare new variables or represents a scope is marked with a new scope. Further statements would inherit this scope to see new variables. TODO: Going out of scope would merge the new variables with unique names back into the index. Co-authored-by: Michael <[email protected]>
1 parent a036ec9 commit 6dd7ed9

File tree

6 files changed

+302
-125
lines changed

6 files changed

+302
-125
lines changed

compiler/plc_ast/src/provider.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::sync::{
55

66
use crate::ast::AstId;
77

8-
#[derive(Clone)]
8+
#[derive(Debug, Clone)]
99
pub struct IdProvider {
1010
current_id: Arc<AtomicUsize>,
1111
}

src/index.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use self::{
2222

2323
pub mod const_expressions;
2424
mod instance_iterator;
25+
pub mod scoped_index;
2526
pub mod symbol;
2627
#[cfg(test)]
2728
mod tests;
@@ -734,7 +735,7 @@ impl PouIndexEntry {
734735
/// the TypeIndex carries all types.
735736
/// it is extracted into its seaprate struct so it can be
736737
/// internally borrowed individually from the other maps
737-
#[derive(Debug)]
738+
#[derive(Debug, Clone)]
738739
pub struct TypeIndex {
739740
/// all types (structs, enums, type, POUs, etc.)
740741
types: SymbolMap<String, DataType>,

src/index/scoped_index.rs

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
use std::{collections::HashMap, rc::Rc};
2+
3+
use plc_ast::provider::IdProvider;
4+
use plc_source::source_location::SourceLocation;
5+
6+
use crate::typesystem::DataType;
7+
8+
use super::VariableIndexEntry;
9+
10+
/// A minimal index implementation that can be used for local scopes
11+
#[derive(Debug, Clone)]
12+
pub struct ScopedIndex {
13+
///The scope of the current index, this is usually a POU
14+
scope: String,
15+
16+
/// A unique identifier that new variables in this scope will inherit
17+
suffix_provider: IdProvider,
18+
19+
/// The location that caused this scope to be created
20+
start_location: SourceLocation,
21+
22+
/// New variables defined by this index
23+
variables: HashMap<String, VariableIndexEntry>,
24+
25+
/// Datatypes defined by this index
26+
type_index: HashMap<String, DataType>,
27+
28+
parent: Option<Rc<ScopedIndex>>,
29+
}
30+
31+
impl ScopedIndex {
32+
pub fn merge_into(self, target: &mut Self) {
33+
target.variables.extend(self.variables);
34+
target.type_index.extend(self.type_index);
35+
}
36+
37+
pub fn add_variable(&mut self, _name: &str) {}
38+
39+
pub fn add_type(&mut self, _name: &str) {}
40+
41+
pub fn find_variable(&self, _name: &str) -> Option<&VariableIndexEntry> {
42+
todo!()
43+
}
44+
45+
pub fn find_type(&self, _name: &str) -> Option<&DataType> {
46+
todo!()
47+
}
48+
49+
pub fn new(
50+
parent: Option<Rc<ScopedIndex>>,
51+
location: SourceLocation,
52+
scope: &str,
53+
suffix_provider: IdProvider,
54+
) -> ScopedIndex {
55+
ScopedIndex {
56+
scope: scope.to_string(),
57+
suffix_provider,
58+
start_location: location,
59+
parent,
60+
type_index: Default::default(),
61+
variables: Default::default(),
62+
}
63+
}
64+
}

src/index/symbol.rs

+10
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ impl<K, V> Default for SymbolMap<K, V> {
1818
}
1919
}
2020

21+
impl<K, V> Clone for SymbolMap<K, V>
22+
where
23+
K: Clone,
24+
V: Clone,
25+
{
26+
fn clone(&self) -> Self {
27+
Self { inner_map: self.inner_map.clone() }
28+
}
29+
}
30+
2131
impl<K, V> SymbolMap<K, V>
2232
where
2333
K: Hash + Eq,

0 commit comments

Comments
 (0)