Skip to content

Commit

Permalink
feat: add resolve print options for the resolver to speedup the norma…
Browse files Browse the repository at this point in the history
…l compilation process (#684)

feat: add resolve print options for the resolver to speedup the normel compilation process
  • Loading branch information
Peefy authored Aug 27, 2023
1 parent 03911df commit 7f0b903
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 48 deletions.
10 changes: 8 additions & 2 deletions kclvm/query/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use anyhow::Result;
use indexmap::IndexMap;
use kclvm_parser::{load_program, LoadProgramOptions, ParseSession};
use kclvm_sema::{
resolver::{resolve_program, scope::Scope},
resolver::{resolve_program_with_opts, scope::Scope, Options},
ty::SchemaType,
};

Expand Down Expand Up @@ -121,7 +121,13 @@ fn resolve_file(file: &str, code: Option<&str>) -> Result<Rc<RefCell<Scope>>> {
return Err(anyhow::anyhow!("{err}"));
}
};
let scope = resolve_program(&mut program);
let scope = resolve_program_with_opts(
&mut program,
Options {
resolve_val: true,
..Default::default()
},
);
match scope.main_scope() {
Some(scope) => Ok(scope.clone()),
None => Err(anyhow::anyhow!("main scope is not found")),
Expand Down
17 changes: 12 additions & 5 deletions kclvm/sema/src/resolver/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,11 @@ impl<'ctx> Resolver<'ctx> {
let name = unification_stmt.value.node.name.node.get_name();
let ty = self.parse_ty_str_with_scope(&name, stmt.get_span_pos());
let is_optional = true;
let default = print_schema_expr(&unification_stmt.value.node);
let default = if self.options.resolve_val {
print_schema_expr(&unification_stmt.value.node)
} else {
"".to_string()
};
(
unification_stmt.target.node.get_name(),
ty,
Expand All @@ -619,10 +623,13 @@ impl<'ctx> Resolver<'ctx> {
schema_attr.ty.get_span_pos(),
);
let is_optional = schema_attr.is_optional;
let default = schema_attr
.value
.as_ref()
.map(|v| print_ast_node(ASTNode::Expr(v)));
let default = schema_attr.value.as_ref().map(|v| {
if self.options.resolve_val {
print_ast_node(ASTNode::Expr(v))
} else {
"".to_string()
}
});
// Schema attribute decorators
let decorators = self.resolve_decorators(
&schema_attr.decorators,
Expand Down
35 changes: 22 additions & 13 deletions kclvm/sema/src/resolver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,25 +136,34 @@ pub struct Context {
pub type_alias_mapping: IndexMap<String, IndexMap<String, String>>,
}

/// Resolve options
#[derive(Clone, Debug, Default)]
/// Resolve options.
/// - lint_check: whether to run lint passes
/// - resolve_val: whether to resolve and print their AST to value for some nodes.
#[derive(Clone, Debug)]
pub struct Options {
pub raise_err: bool,
pub config_auto_fix: bool,
pub lint_check: bool,
pub resolve_val: bool,
}

/// Resolve program
impl Default for Options {
fn default() -> Self {
Self {
lint_check: true,
resolve_val: false,
}
}
}

/// Resolve program with default options.
#[inline]
pub fn resolve_program(program: &mut Program) -> ProgramScope {
resolve_program_with_opts(program, Options::default())
}

/// Resolve program with options. See [Options]
pub fn resolve_program_with_opts(program: &mut Program, opts: Options) -> ProgramScope {
pre_process_program(program);
let mut resolver = Resolver::new(
program,
Options {
raise_err: true,
config_auto_fix: false,
lint_check: true,
},
);
let mut resolver = Resolver::new(program, opts);
resolver.resolve_import();
let scope = resolver.check_and_lint(kclvm_ast::MAIN_PKG);
let type_alias_mapping = resolver.ctx.type_alias_mapping.clone();
Expand Down
40 changes: 22 additions & 18 deletions kclvm/sema/src/resolver/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,23 +222,27 @@ impl<'ctx> Resolver<'ctx> {
args: &'ctx [ast::NodeRef<ast::Expr>],
kwargs: &'ctx [ast::NodeRef<ast::Keyword>],
) -> (Vec<String>, HashMap<String, String>) {
(
args.iter()
.map(|a| print_ast_node(ASTNode::Expr(a)))
.collect(),
kwargs
.iter()
.map(|a| {
(
a.node.arg.node.get_name(),
a.node
.value
.as_ref()
.map(|v| print_ast_node(ASTNode::Expr(v)))
.unwrap_or_default(),
)
})
.collect(),
)
if self.options.resolve_val {
(
args.iter()
.map(|a| print_ast_node(ASTNode::Expr(a)))
.collect(),
kwargs
.iter()
.map(|a| {
(
a.node.arg.node.get_name(),
a.node
.value
.as_ref()
.map(|v| print_ast_node(ASTNode::Expr(v)))
.unwrap_or_default(),
)
})
.collect(),
)
} else {
(vec![], HashMap::new())
}
}
}
9 changes: 1 addition & 8 deletions kclvm/sema/src/resolver/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,14 +258,7 @@ fn test_lint() {
let mut program =
load_program(sess.clone(), &["./src/resolver/test_data/lint.k"], None).unwrap();
pre_process_program(&mut program);
let mut resolver = Resolver::new(
&program,
Options {
raise_err: true,
config_auto_fix: false,
lint_check: true,
},
);
let mut resolver = Resolver::new(&program, Options::default());
resolver.resolve_import();
resolver.check_and_lint(kclvm_ast::MAIN_PKG);

Expand Down
4 changes: 2 additions & 2 deletions kclvm/tools/src/LSP/src/request.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crossbeam_channel::Sender;

use lsp_types::{CodeAction, CodeActionKind, CodeActionOrCommand, Position, Range, TextEdit};
use lsp_types::TextEdit;
use ra_ap_vfs::VfsPath;
use std::{collections::HashMap, ops::Index, time::Instant};
use std::time::Instant;

use crate::{
completion::completion,
Expand Down

0 comments on commit 7f0b903

Please sign in to comment.