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

feat : namer for new architecture of semantic model #762

Merged
merged 2 commits into from
Oct 20, 2023
Merged
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
20 changes: 20 additions & 0 deletions kclvm/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions kclvm/ast/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
compiler_base_span = {path = "../../compiler_base/span", version = "0.0.2"}
uuid = { version = "1.4.1", features = ["v4"] }
compiler_base_span = { path = "../../compiler_base/span", version = "0.0.2" }
serde = { version = "1", features = ["derive"] }
serde_json = "1.0"

kclvm-span = {path = "../span"}
kclvm-error = {path = "../error"}
kclvm-span = { path = "../span" }
kclvm-error = { path = "../error" }

[dev-dependencies]
kclvm-parser = {path = "../parser"}
kclvm-parser = { path = "../parser" }
34 changes: 33 additions & 1 deletion kclvm/ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ use serde::{Deserialize, Serialize};
use std::collections::HashMap;

use compiler_base_span::{Loc, Span};
use std::fmt::Debug;
use uuid;

use super::token;
use crate::{node_ref, pos::ContainsPos};
Expand Down Expand Up @@ -79,12 +81,22 @@ impl Into<Range> for Pos {
}
}

#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct AstIndex(uuid::Uuid);

impl Default for AstIndex {
fn default() -> Self {
Self(uuid::Uuid::new_v4())
}
}
/// Node is the file, line and column number information
/// that all AST nodes need to contain.
/// In fact, column and end_column are the counts of character,
/// For example, `\t` is counted as 1 character, so it is recorded as 1 here, but generally col is 4.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[derive(Serialize, Deserialize, Clone, PartialEq)]
pub struct Node<T> {
#[serde(skip_serializing, skip_deserializing, default)]
pub id: AstIndex,
pub node: T,
pub filename: String,
pub line: u64,
Expand All @@ -93,6 +105,19 @@ pub struct Node<T> {
pub end_column: u64,
}

impl<T: Debug> Debug for Node<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Node")
.field("node", &self.node)
.field("filename", &self.filename)
.field("line", &self.line)
.field("column", &self.column)
.field("end_line", &self.end_line)
.field("end_column", &self.end_column)
.finish()
}
}

impl<T> Node<T> {
pub fn new(
node: T,
Expand All @@ -103,6 +128,7 @@ impl<T> Node<T> {
end_column: u64,
) -> Self {
Self {
id: AstIndex::default(),
node,
filename,
line,
Expand All @@ -114,6 +140,7 @@ impl<T> Node<T> {

pub fn dummy_node(node: T) -> Self {
Self {
id: AstIndex::default(),
node,
filename: "".to_string(),
line: 1,
Expand All @@ -125,6 +152,7 @@ impl<T> Node<T> {

pub fn node(node: T, (lo, hi): (Loc, Loc)) -> Self {
Self {
id: AstIndex::default(),
node,
filename: format!("{}", lo.file.name.prefer_remapped()),
line: lo.line as u64,
Expand All @@ -136,6 +164,7 @@ impl<T> Node<T> {

pub fn node_with_pos(node: T, pos: PosTuple) -> Self {
Self {
id: AstIndex::default(),
node,
filename: pos.0.clone(),
line: pos.1,
Expand Down Expand Up @@ -178,6 +207,7 @@ impl TryInto<Node<Identifier>> for Node<Expr> {
fn try_into(self) -> Result<Node<Identifier>, Self::Error> {
match self.node {
Expr::Identifier(ident) => Ok(Node {
id: self.id,
node: ident,
filename: self.filename,
line: self.line,
Expand All @@ -194,6 +224,7 @@ impl Node<Expr> {
/// Into a missing identifier.
pub fn into_missing_identifier(&self) -> Node<Identifier> {
Node {
id: self.id.clone(),
node: Identifier {
names: vec![],
pkgpath: String::new(),
Expand All @@ -214,6 +245,7 @@ impl TryInto<Node<SchemaExpr>> for Node<Expr> {
fn try_into(self) -> Result<Node<SchemaExpr>, Self::Error> {
match self.node {
Expr::Schema(schema_expr) => Ok(Node {
id: self.id,
node: schema_expr,
filename: self.filename,
line: self.line,
Expand Down
3 changes: 3 additions & 0 deletions kclvm/parser/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1171,6 +1171,7 @@ impl<'a> Parser<'a> {
x.node.orelse = if_item.orelse;

let t = Node {
id: AstIndex::default(),
node: Expr::ListIfItem(x.node),
filename: x.filename,
line: x.line,
Expand Down Expand Up @@ -1607,6 +1608,7 @@ impl<'a> Parser<'a> {
x.node.orelse = if_entry.node.orelse;

let t = Node {
id: AstIndex::default(),
node: Expr::ConfigIfEntry(x.node),
filename: x.filename,
line: x.line,
Expand Down Expand Up @@ -1646,6 +1648,7 @@ impl<'a> Parser<'a> {

let mut body = {
let node = Node {
id: AstIndex::default(),
node: Expr::NameConstantLit(NameConstantLit {
value: NameConstant::None, // ignore
}),
Expand Down
1 change: 1 addition & 0 deletions kclvm/parser/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ impl<'a> Parser<'a> {
let filename: String = format!("{}", lo.file.name.prefer_remapped());

let node = kclvm_ast::ast::Node {
id: kclvm_ast::ast::AstIndex::default(),
node: Comment {
text: x.as_str().to_string(),
},
Expand Down
1 change: 1 addition & 0 deletions kclvm/parser/src/parser/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,7 @@ impl<'a> Parser<'a> {
index_signature: body_index_signature,

name: Box::new(Node {
id: AstIndex::default(),
node: "".to_string(),
filename: "".to_string(),
line: 0,
Expand Down
18 changes: 9 additions & 9 deletions kclvm/sema/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
generational-arena = "0.2.9"
phf = { version = "0.9", features = ["macros"] }
ahash = "0.7.2"
indexmap = "1.0"
Expand All @@ -20,22 +21,21 @@ regex = "1.7.0"
lazy_static = "1.4.0"
pcre2 = "*"

kclvm-ast = {path = "../ast"}
kclvm-ast-pretty = {path = "../ast_pretty"}
kclvm-runtime = {path = "../runtime"}
kclvm-error = {path = "../error"}
kclvm-span = {path = "../span"}
compiler_base_span = {path = "../../compiler_base/span", version = "0.0.2"}
compiler_base_session = {path = "../../compiler_base/session"}
kclvm-ast = { path = "../ast" }
kclvm-ast-pretty = { path = "../ast_pretty" }
kclvm-runtime = { path = "../runtime" }
kclvm-error = { path = "../error" }
kclvm-span = { path = "../span" }
compiler_base_span = { path = "../../compiler_base/span", version = "0.0.2" }
compiler_base_session = { path = "../../compiler_base/session" }
compiler_base_macros = "0.0.1"
compiler_base_error = "0.0.8"
suggestions = "0.1.1"

[dev-dependencies]
kclvm-parser = {path = "../parser"}
kclvm-parser = { path = "../parser" }
criterion = "0.3"

[[bench]]
name = "my_benchmark"
harness = false

29 changes: 29 additions & 0 deletions kclvm/sema/src/core/global_state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use super::{package::PackageDB, symbol::KCLSymbolData};

#[derive(Default, Debug)]
pub struct GlobalState {
symbols: KCLSymbolData,
packages: PackageDB,
}

impl GlobalState {
pub fn get_symbols(&self) -> &KCLSymbolData {
&self.symbols
}

pub fn get_symbols_mut(&mut self) -> &mut KCLSymbolData {
&mut self.symbols
}

pub fn get_packages(&self) -> &PackageDB {
&self.packages
}

pub fn get_packages_mut(&mut self) -> &mut PackageDB {
&mut self.packages
}

pub fn resolve_symbols(&mut self) {
self.symbols.replace_unresolved_symbol(&self.packages)
}
}
3 changes: 3 additions & 0 deletions kclvm/sema/src/core/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod global_state;
pub mod package;
pub mod symbol;
89 changes: 89 additions & 0 deletions kclvm/sema/src/core/package.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
use indexmap::IndexMap;

#[derive(Default, Debug)]
pub struct PackageDB {
pub(crate) package_info: IndexMap<String, PackageInfo>,
}

impl PackageDB {
pub fn add_package(&mut self, info: PackageInfo) {
self.package_info
.insert(info.fully_qualified_name.clone(), info);
}

pub fn remove_package_info(&mut self, name: &str) {
self.package_info.remove(name);
}

pub fn get_package_info(&self, name: &str) -> Option<&PackageInfo> {
self.package_info.get(name)
}

pub fn get_package_info_mut(&mut self, name: &str) -> Option<&mut PackageInfo> {
self.package_info.get_mut(name)
}
}
#[derive(Debug)]
pub struct PackageInfo {
pub(crate) fully_qualified_name: String,
pub(crate) modules: IndexMap<String, ModuleInfo>,
pub(crate) imports: IndexMap<String, ImportInfo>,
}

impl PackageInfo {
pub fn new(fully_qualified_name: String) -> Self {
Self {
fully_qualified_name,
modules: IndexMap::default(),
imports: IndexMap::default(),
}
}

pub fn add_import_info(&mut self, info: ImportInfo) {
self.imports.insert(info.fully_qualified_name.clone(), info);
}

pub fn remove_import_info(&mut self, name: &str) {
self.imports.remove(name);
}

pub fn get_import_info(&self, name: &str) -> Option<&ImportInfo> {
self.imports.get(name)
}

pub fn add_module_info(&mut self, info: ModuleInfo) {
self.modules.insert(info.filename.clone(), info);
}

pub fn remove_module_info(&mut self, name: &str) {
self.modules.remove(name);
}

pub fn get_module_info(&self, name: &str) -> Option<&ModuleInfo> {
self.modules.get(name)
}
}
#[derive(Debug)]
pub struct ImportInfo {
pub(crate) unqualified_name: String,
pub(crate) fully_qualified_name: String,
}

impl ImportInfo {
pub fn new(unqualified_name: String, fully_qualified_name: String) -> Self {
Self {
unqualified_name,
fully_qualified_name,
}
}
}
#[derive(Debug)]
pub struct ModuleInfo {
pub(crate) filename: String,
}

impl ModuleInfo {
pub fn new(filename: String) -> Self {
Self { filename }
}
}
Loading
Loading