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: add symbol to node and type symbol APIs #1004

Merged
merged 1 commit into from
Jan 25, 2024
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
3 changes: 2 additions & 1 deletion kclvm/api/src/service/into.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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()),
Expand Down
16 changes: 15 additions & 1 deletion kclvm/api/src/service/service_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<LoadPackageResult> {
Expand All @@ -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());
}
Expand All @@ -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,
Expand Down
16 changes: 10 additions & 6 deletions kclvm/loader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,14 @@ pub struct Packages {
pub symbols: IndexMap<SymbolRef, SymbolInfo>,
/// Scope information
pub scopes: IndexMap<ScopeRef, ScopeInfo>,
// AST Node-Symbol mapping
/// AST Node-Symbol mapping
pub node_symbol_map: IndexMap<NodeKey, SymbolRef>,
// <Package path>-<Root scope> mapping
/// <Package path>-<Root scope> mapping
pub pkg_scope_map: IndexMap<String, ScopeRef>,
/// Symbol-AST Node mapping
pub symbol_node_map: IndexMap<SymbolRef, NodeKey>,
/// Fully qualified name mapping
pub fully_qualified_name_map: IndexMap<String, SymbolRef>,
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -169,10 +173,6 @@ pub fn load_packages(opts: &LoadPackageOptions) -> Result<Packages> {
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);
}
}
}
}
Expand All @@ -191,6 +191,10 @@ pub fn load_packages(opts: &LoadPackageOptions) -> Result<Packages> {
);
}
}
// 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)
}

Expand Down
2 changes: 1 addition & 1 deletion kclvm/sema/src/advanced_resolver/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
37 changes: 23 additions & 14 deletions kclvm/sema/src/core/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ pub struct SymbolDB {
pub(crate) fully_qualified_name_map: IndexMap<String, SymbolRef>,
pub(crate) schema_builtin_symbols: IndexMap<SymbolRef, IndexMap<String, SymbolRef>>,
pub(crate) node_symbol_map: IndexMap<NodeKey, SymbolRef>,
pub(crate) symbol_ref_map: IndexMap<SymbolRef, NodeKey>,
pub(crate) symbol_node_map: IndexMap<SymbolRef, NodeKey>,
}

impl SymbolData {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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<NodeKey, SymbolRef> {
self.symbols_info.node_symbol_map.clone()
}

pub fn alloc_comment_symbol(
&mut self,
comment: CommentSymbol,
Expand All @@ -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<NodeKey, SymbolRef> {
&self.symbols_info.node_symbol_map
}

#[inline]
pub fn get_symbol_node_map(&self) -> &IndexMap<SymbolRef, NodeKey> {
&self.symbols_info.symbol_node_map
}

pub fn get_fully_qualified_name_map(&self) -> &IndexMap<String, SymbolRef> {
&self.symbols_info.fully_qualified_name_map
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
Expand Down
16 changes: 9 additions & 7 deletions kclvm/spec/gpyrpc/gpyrpc.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, SymbolIndex> node_symbol_map = 3; // Map key is the AST index UUID string.
map<string, Symbol> symbols = 4; // Map key is the SymbolIndex json string.
map<string, ScopeIndex> pkg_scope_map = 5; // Map key is the package path.
map<string, Scope> 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<string, Scope> scopes = 5; // Map key is the ScopeIndex json string.
map<string, Symbol> symbols = 6; // Map key is the SymbolIndex json string.
map<string, SymbolIndex> node_symbol_map = 7; // Map key is the AST index UUID string.
map<string, string> symbol_node_map = 8; // Map key is the SymbolIndex json string.
map<string, SymbolIndex> fully_qualified_name_map = 9; // Map key is the fully_qualified_name e.g. `pkg.Name`
map<string, ScopeIndex> 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;
Expand Down
Loading