From 5b40623bf085ed314bc6da8001f6ffbabf2aa924 Mon Sep 17 00:00:00 2001 From: Peefy Date: Thu, 25 Jan 2024 14:44:09 +0800 Subject: [PATCH] feat: add symbol to node and type symbol APIs (#1004) Signed-off-by: peefy --- kclvm/api/src/service/into.rs | 3 +- kclvm/api/src/service/service_impl.rs | 16 +++++++++- kclvm/loader/src/lib.rs | 16 ++++++---- kclvm/sema/src/advanced_resolver/node.rs | 2 +- kclvm/sema/src/core/symbol.rs | 37 +++++++++++++++--------- kclvm/spec/gpyrpc/gpyrpc.proto | 16 +++++----- 6 files changed, 60 insertions(+), 30 deletions(-) diff --git a/kclvm/api/src/service/into.rs b/kclvm/api/src/service/into.rs index 36f5d8d18..cf90367e7 100644 --- a/kclvm/api/src/service/into.rs +++ b/kclvm/api/src/service/into.rs @@ -2,6 +2,7 @@ use crate::gpyrpc::{ CliConfig, Error, KeyValuePair, LoadSettingsFilesResult, Message, Position, Scope, ScopeIndex, Symbol, SymbolIndex, }; +use crate::service::ty::kcl_ty_to_pb_ty; use kclvm_config::settings::SettingsFile; use kclvm_error::Diagnostic; use kclvm_loader::{ScopeInfo, SymbolInfo}; @@ -112,7 +113,7 @@ impl IntoScopeIndex for ScopeRef { impl IntoSymbol for SymbolInfo { fn into_symbol(self) -> Symbol { Symbol { - ty: self.ty.ty_str(), + ty: Some(kcl_ty_to_pb_ty(&self.ty)), name: self.name, owner: self.owner.map(|o| o.into_symbol_index()), def: self.def.map(|d| d.into_symbol_index()), diff --git a/kclvm/api/src/service/service_impl.rs b/kclvm/api/src/service/service_impl.rs index 04664974c..6f871738c 100644 --- a/kclvm/api/src/service/service_impl.rs +++ b/kclvm/api/src/service/service_impl.rs @@ -171,9 +171,11 @@ impl KclvmServiceImpl { /// assert_eq!(result.paths.len(), 3); /// assert_eq!(result.parse_errors.len(), 0); /// assert_eq!(result.type_errors.len(), 0); - /// assert_eq!(result.node_symbol_map.len(), 159); /// assert_eq!(result.symbols.len(), 12); /// assert_eq!(result.scopes.len(), 3); + /// assert_eq!(result.node_symbol_map.len(), 159); + /// assert_eq!(result.symbol_node_map.len(), 159); + /// assert_eq!(result.fully_qualified_name_map.len(), 166); /// assert_eq!(result.pkg_scope_map.len(), 3); /// ``` pub fn load_package(&self, args: &LoadPackageArgs) -> anyhow::Result { @@ -199,12 +201,22 @@ impl KclvmServiceImpl { } let program_json = serde_json::to_string(&packages.program)?; let mut node_symbol_map = HashMap::new(); + let mut symbol_node_map = HashMap::new(); + let mut fully_qualified_name_map = HashMap::new(); let mut pkg_scope_map = HashMap::new(); let mut symbols = HashMap::new(); let mut scopes = HashMap::new(); + // Build sematic mappings for (k, s) in packages.node_symbol_map { node_symbol_map.insert(k.id.to_string(), s.into_symbol_index()); } + for (s, k) in packages.symbol_node_map { + let symbol_index_string = serde_json::to_string(&s)?; + symbol_node_map.insert(symbol_index_string, k.id.to_string()); + } + for (s, k) in packages.fully_qualified_name_map { + fully_qualified_name_map.insert(s, k.into_symbol_index()); + } for (k, s) in packages.pkg_scope_map { pkg_scope_map.insert(k, s.into_scope_index()); } @@ -224,6 +236,8 @@ impl KclvmServiceImpl { .map(|p| p.to_str().unwrap().to_string()) .collect(), node_symbol_map, + symbol_node_map, + fully_qualified_name_map, pkg_scope_map, symbols, scopes, diff --git a/kclvm/loader/src/lib.rs b/kclvm/loader/src/lib.rs index 9b12a89bd..22c5acb87 100644 --- a/kclvm/loader/src/lib.rs +++ b/kclvm/loader/src/lib.rs @@ -52,10 +52,14 @@ pub struct Packages { pub symbols: IndexMap, /// Scope information pub scopes: IndexMap, - // AST Node-Symbol mapping + /// AST Node-Symbol mapping pub node_symbol_map: IndexMap, - // - mapping + /// - mapping pub pkg_scope_map: IndexMap, + /// Symbol-AST Node mapping + pub symbol_node_map: IndexMap, + /// Fully qualified name mapping + pub fully_qualified_name_map: IndexMap, } #[derive(Debug, Clone)] @@ -169,10 +173,6 @@ pub fn load_packages(opts: &LoadPackageOptions) -> Result { is_global: symbol.is_global(), }; packages.symbols.insert(*symbol_ref, info); - let node_symbol_map = symbols.get_node_symbol_map(); - for (k, s) in &node_symbol_map { - packages.node_symbol_map.insert(k.clone(), *s); - } } } } @@ -191,6 +191,10 @@ pub fn load_packages(opts: &LoadPackageOptions) -> Result { ); } } + // Update package semantic mappings + packages.node_symbol_map = symbols.get_node_symbol_map().clone(); + packages.symbol_node_map = symbols.get_symbol_node_map().clone(); + packages.fully_qualified_name_map = symbols.get_fully_qualified_name_map().clone(); Ok(packages) } diff --git a/kclvm/sema/src/advanced_resolver/node.rs b/kclvm/sema/src/advanced_resolver/node.rs index dca600948..e1f4970a8 100644 --- a/kclvm/sema/src/advanced_resolver/node.rs +++ b/kclvm/sema/src/advanced_resolver/node.rs @@ -1129,7 +1129,7 @@ impl<'ctx> AdvancedResolver<'ctx> { let symbols = self.gs.get_symbols(); if let Some(def_symbol_ref) = symbols.get_symbol(key_symbol_ref).unwrap().get_definition() { - if let Some(node_key) = symbols.symbols_info.symbol_ref_map.get(&def_symbol_ref) { + if let Some(node_key) = symbols.symbols_info.symbol_node_map.get(&def_symbol_ref) { if let Some(def_ty) = self.ctx.node_ty_map.get(node_key) { if def_ty.is_schema() { self.ctx.current_schema_symbol = diff --git a/kclvm/sema/src/core/symbol.rs b/kclvm/sema/src/core/symbol.rs index f4aecc488..fa842283c 100644 --- a/kclvm/sema/src/core/symbol.rs +++ b/kclvm/sema/src/core/symbol.rs @@ -77,7 +77,7 @@ pub struct SymbolDB { pub(crate) fully_qualified_name_map: IndexMap, pub(crate) schema_builtin_symbols: IndexMap>, pub(crate) node_symbol_map: IndexMap, - pub(crate) symbol_ref_map: IndexMap, + pub(crate) symbol_node_map: IndexMap, } impl SymbolData { @@ -500,7 +500,7 @@ impl SymbolData { .node_symbol_map .insert(node_key.clone(), symbol_ref); self.symbols_info - .symbol_ref_map + .symbol_node_map .insert(symbol_ref, node_key); self.schemas.get_mut(symbol_id).unwrap().id = Some(symbol_ref); symbol_ref @@ -523,7 +523,7 @@ impl SymbolData { .node_symbol_map .insert(node_key.clone(), symbol_ref); self.symbols_info - .symbol_ref_map + .symbol_node_map .insert(symbol_ref, node_key); self.unresolved.get_mut(symbol_id).unwrap().id = Some(symbol_ref); symbol_ref @@ -544,7 +544,7 @@ impl SymbolData { .node_symbol_map .insert(node_key.clone(), symbol_ref); self.symbols_info - .symbol_ref_map + .symbol_node_map .insert(symbol_ref, node_key); self.type_aliases.get_mut(symbol_id).unwrap().id = Some(symbol_ref); symbol_ref @@ -561,7 +561,7 @@ impl SymbolData { .node_symbol_map .insert(node_key.clone(), symbol_ref); self.symbols_info - .symbol_ref_map + .symbol_node_map .insert(symbol_ref, node_key); self.rules.get_mut(symbol_id).unwrap().id = Some(symbol_ref); symbol_ref @@ -584,7 +584,7 @@ impl SymbolData { .node_symbol_map .insert(node_key.clone(), symbol_ref); self.symbols_info - .symbol_ref_map + .symbol_node_map .insert(symbol_ref, node_key); self.attributes.get_mut(symbol_id).unwrap().id = Some(symbol_ref); symbol_ref @@ -601,7 +601,7 @@ impl SymbolData { .node_symbol_map .insert(node_key.clone(), symbol_ref); self.symbols_info - .symbol_ref_map + .symbol_node_map .insert(symbol_ref, node_key); self.values.get_mut(symbol_id).unwrap().id = Some(symbol_ref); symbol_ref @@ -625,17 +625,12 @@ impl SymbolData { .node_symbol_map .insert(node_key.clone(), symbol_ref); self.symbols_info - .symbol_ref_map + .symbol_node_map .insert(symbol_ref, node_key); self.exprs.get_mut(symbol_id).unwrap().id = Some(symbol_ref); Some(symbol_ref) } - #[inline] - pub fn get_node_symbol_map(&self) -> IndexMap { - self.symbols_info.node_symbol_map.clone() - } - pub fn alloc_comment_symbol( &mut self, comment: CommentSymbol, @@ -650,11 +645,25 @@ impl SymbolData { .node_symbol_map .insert(node_key.clone(), symbol_ref); self.symbols_info - .symbol_ref_map + .symbol_node_map .insert(symbol_ref, node_key); self.exprs.get_mut(symbol_id).unwrap().id = Some(symbol_ref); Some(symbol_ref) } + + #[inline] + pub fn get_node_symbol_map(&self) -> &IndexMap { + &self.symbols_info.node_symbol_map + } + + #[inline] + pub fn get_symbol_node_map(&self) -> &IndexMap { + &self.symbols_info.symbol_node_map + } + + pub fn get_fully_qualified_name_map(&self) -> &IndexMap { + &self.symbols_info.fully_qualified_name_map + } } #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)] diff --git a/kclvm/spec/gpyrpc/gpyrpc.proto b/kclvm/spec/gpyrpc/gpyrpc.proto index 0d9c17eb9..7a86dbb65 100644 --- a/kclvm/spec/gpyrpc/gpyrpc.proto +++ b/kclvm/spec/gpyrpc/gpyrpc.proto @@ -128,16 +128,18 @@ message LoadPackage_Args { message LoadPackage_Result { string program = 1; // JSON string value repeated string paths = 2; // Returns the files in the order they should be compiled - map node_symbol_map = 3; // Map key is the AST index UUID string. - map symbols = 4; // Map key is the SymbolIndex json string. - map pkg_scope_map = 5; // Map key is the package path. - map scopes = 6; // Map key is the ScopeIndex json string. - repeated Error parse_errors = 7; // Parse errors - repeated Error type_errors = 8; // Type errors + repeated Error parse_errors = 3; // Parse errors + repeated Error type_errors = 4; // Type errors + map scopes = 5; // Map key is the ScopeIndex json string. + map symbols = 6; // Map key is the SymbolIndex json string. + map node_symbol_map = 7; // Map key is the AST index UUID string. + map symbol_node_map = 8; // Map key is the SymbolIndex json string. + map fully_qualified_name_map = 9; // Map key is the fully_qualified_name e.g. `pkg.Name` + map pkg_scope_map = 10; // Map key is the package path. } message Symbol { - string ty = 1; + KclType ty = 1; string name = 2; SymbolIndex owner = 3; SymbolIndex def = 4;