Skip to content
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
37 changes: 37 additions & 0 deletions kclvm/ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,43 @@ pub enum Expr {
Missing(MissingExpr),
}

impl Expr {
pub fn get_expr_name(&self) -> String {
match self {
Expr::Identifier(_) => "IdentifierExpression",
Expr::Unary(_) => "UnaryExpression",
Expr::Binary(_) => "BinaryExpression",
Expr::If(_) => "IfExpression",
Expr::Selector(_) => "SelectorExpression",
Expr::Call(_) => "CallExpression",
Expr::Paren(_) => "ParenExpression",
Expr::Quant(_) => "QuantExpression",
Expr::List(_) => "ListExpression",
Expr::ListIfItem(_) => "ListIfItemExpression",
Expr::ListComp(_) => "ListCompExpression",
Expr::Starred(_) => "StarredExpression",
Expr::DictComp(_) => "DictCompExpression",
Expr::ConfigIfEntry(_) => "ConfigIfEntryExpression",
Expr::CompClause(_) => "CompClauseExpression",
Expr::Schema(_) => "SchemaExpression",
Expr::Config(_) => "ConfigExpression",
Expr::Check(_) => "CheckExpression",
Expr::Lambda(_) => "LambdaExpression",
Expr::Subscript(_) => "SubscriptExpression",
Expr::Keyword(_) => "KeywordExpression",
Expr::Arguments(_) => "ArgumentsExpression",
Expr::Compare(_) => "CompareExpression",
Expr::NumberLit(_) => "NumberLitExpression",
Expr::StringLit(_) => "StringLitExpression",
Expr::NameConstantLit(_) => "NameConstantLitExpression",
Expr::JoinedString(_) => "JoinedStringExpression",
Expr::FormattedValue(_) => "FormattedValueExpression",
Expr::Missing(_) => "MissingExpression",
}
.to_string()
}
}

/// Identifier, e.g.
/// ```kcl
/// a
Expand Down
45 changes: 18 additions & 27 deletions kclvm/sema/src/advanced_resolver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1163,53 +1163,43 @@ mod tests {
vec![(1, 0, 1, 2, "_b".to_string(), SymbolKind::Value)],
),
];
let mut skip_def_info = false;
for (filepath, symbols) in except_symbols.iter() {
let abs_filepath = adjust_canonicalization(base_path.join(filepath));
let file_sema_info = gs.sema_db.file_sema_map.get(&abs_filepath).unwrap();

let mut def_count = 0;

// symbols will be sorted according to their position in the file
// now we check all symbols
for (index, symbol_ref) in file_sema_info.symbols.iter().enumerate() {
let symbol = gs.get_symbols().get_symbol(*symbol_ref).unwrap();
let (start, end) = symbol.get_range();
for (index, symbol_info) in symbols.iter().enumerate() {
if skip_def_info {
skip_def_info = false;
continue;
}
let (start_line, start_col, end_line, end_col, name, kind) = symbol_info;
if abs_filepath.is_empty() {
continue;
}
// test look up symbols
let inner_pos = Position {
filename: abs_filepath.clone(),
line: (start.line + end.line) / 2,
column: Some((start.column.unwrap_or(0) + end.column.unwrap_or(0)) / 2),
};
let looked_symbol = gs.look_up_exact_symbol(&inner_pos);
assert_eq!(looked_symbol, Some(*symbol_ref));
let out_pos = Position {
filename: abs_filepath.clone(),
line: (start.line + end.line) / 2 + 1,
column: Some(end.column.unwrap_or(0) + 1),
line: (start_line + end_line) / 2,
column: Some((start_col + end_col) / 2),
};
let looked_symbol = gs.look_up_exact_symbol(&out_pos);
assert_ne!(looked_symbol, Some(*symbol_ref));

let looked_symbol_ref = gs.look_up_exact_symbol(&inner_pos).unwrap();
let looked_symbol = gs.get_symbols().get_symbol(looked_symbol_ref).unwrap();
let (start, end) = looked_symbol.get_range();
// test symbol basic infomation
let (start_line, start_col, end_line, end_col, name, kind) =
symbols.get(index + def_count).unwrap();
assert_eq!(start.filename, abs_filepath);
assert_eq!(start.line, *start_line);
assert_eq!(start.column.unwrap_or(0), *start_col);
assert_eq!(end.line, *end_line);
assert_eq!(end.column.unwrap_or(0), *end_col);
assert_eq!(*name, symbol.get_name());
assert_eq!(symbol_ref.get_kind(), *kind);
assert_eq!(*name, looked_symbol.get_name());
assert_eq!(looked_symbol_ref.get_kind(), *kind);

// test find def
if SymbolKind::Unresolved == symbol_ref.get_kind() {
def_count = def_count + 1;
if SymbolKind::Unresolved == looked_symbol_ref.get_kind() {
let (start_line, start_col, end_line, end_col, path, kind) =
symbols.get(index + def_count).unwrap();
let def_ref = symbol.get_definition().unwrap();
symbols.get(index + 1).unwrap();
let def_ref = looked_symbol.get_definition().unwrap();
let def = gs.get_symbols().get_symbol(def_ref).unwrap();
let (start, end) = def.get_range();
let def_filepath = adjust_canonicalization(base_path.join(path));
Expand All @@ -1221,6 +1211,7 @@ mod tests {
assert_eq!(start.filename, def_filepath);
}
assert_eq!(def_ref.get_kind(), *kind);
skip_def_info = true;
}
}
}
Expand Down
26 changes: 22 additions & 4 deletions kclvm/sema/src/advanced_resolver/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ use kclvm_error::{diagnostic::Range, Position};
use crate::{
core::{
scope::LocalSymbolScopeKind,
symbol::{KCLSymbolSemanticInfo, SymbolRef, UnresolvedSymbol, ValueSymbol},
symbol::{
ExpressionSymbol, KCLSymbolSemanticInfo, SymbolRef, UnresolvedSymbol, ValueSymbol,
},
},
ty::{Type, SCHEMA_MEMBER_FUNCTIONS},
};
Expand Down Expand Up @@ -752,9 +754,25 @@ impl<'ctx> AdvancedResolver<'ctx> {
self.ctx.end_pos = end;
}
self.ctx.cur_node = expr.id.clone();
let result = self.walk_expr(&expr.node);

result
match self.walk_expr(&expr.node) {
None => match self.ctx.node_ty_map.get(&self.ctx.get_node_key(&expr.id)) {
Some(ty) => {
let (_, end) = expr.get_span_pos();
let mut expr_symbol = ExpressionSymbol::new(
format!("@{}", expr.node.get_expr_name()),
end.clone(),
end,
None,
);
expr_symbol.sema_info.ty = Some(ty.clone());
self.gs
.get_symbols_mut()
.alloc_expression_symbol(expr_symbol, self.ctx.get_node_key(&expr.id))
}
None => None,
},
some => some,
}
}

#[inline]
Expand Down
20 changes: 20 additions & 0 deletions kclvm/sema/src/core/global_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,26 @@ impl GlobalState {
},
);
}

for (index, symbol) in self.symbols.exprs.iter() {
let symbol_ref = SymbolRef {
kind: SymbolKind::Expression,
id: index,
};
let filename = symbol.start.filename.clone();
if !file_sema_map.contains_key(&filename) {
file_sema_map.insert(filename.clone(), FileSemanticInfo::new(filename.clone()));
}
let file_sema_info = file_sema_map.get_mut(&filename).unwrap();
file_sema_info.symbols.push(symbol_ref);
file_sema_info.symbol_locs.insert(
symbol_ref,
CachedLocation {
line: symbol.start.line,
column: symbol.start.column.unwrap_or(0),
},
);
}
// remove dummy file
file_sema_map.remove("");

Expand Down
Loading