Skip to content

Commit

Permalink
fix: config unification with non config expression. (#423)
Browse files Browse the repository at this point in the history
  • Loading branch information
Peefy committed Feb 23, 2023
1 parent c5b99cc commit a217e25
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 66 deletions.
2 changes: 2 additions & 0 deletions kclvm/compiler/src/codegen/llvm/context.rs
Expand Up @@ -80,6 +80,7 @@ pub struct LLVMCodeGenContext<'ctx> {
pub local_vars: RefCell<HashSet<String>>,
pub schema_stack: RefCell<Vec<value::SchemaType>>,
pub lambda_stack: RefCell<Vec<bool>>,
pub schema_expr_stack: RefCell<Vec<()>>,
pub pkgpath_stack: RefCell<Vec<String>>,
pub filename_stack: RefCell<Vec<String>>,
pub target_vars: RefCell<Vec<String>>,
Expand Down Expand Up @@ -1070,6 +1071,7 @@ impl<'ctx> LLVMCodeGenContext<'ctx> {
local_vars: RefCell::new(HashSet::new()),
schema_stack: RefCell::new(vec![]),
lambda_stack: RefCell::new(vec![false]),
schema_expr_stack: RefCell::new(vec![]),
pkgpath_stack: RefCell::new(vec![String::from(MAIN_PKG_PATH)]),
filename_stack: RefCell::new(vec![String::from("")]),
target_vars: RefCell::new(vec![String::from("")]),
Expand Down
12 changes: 10 additions & 2 deletions kclvm/compiler/src/codegen/llvm/node.rs
Expand Up @@ -1669,7 +1669,8 @@ impl<'ctx> TypedResultWalker<'ctx> for LLVMCodeGenContext<'ctx> {
self.dict_insert(dict_value, name.as_str(), value, 0, -1);
}
let pkgpath = self.native_global_string_value(&self.current_pkgpath());
let is_in_schema = self.schema_stack.borrow().len() > 0;
let is_in_schema =
self.schema_stack.borrow().len() > 0 || self.schema_expr_stack.borrow().len() > 0;
Ok(self.build_call(
&ApiFunc::kclvm_value_function_invoke.name(),
&[
Expand Down Expand Up @@ -1895,6 +1896,9 @@ impl<'ctx> TypedResultWalker<'ctx> for LLVMCodeGenContext<'ctx> {

fn walk_schema_expr(&self, schema_expr: &'ctx ast::SchemaExpr) -> Self::Result {
check_backtrack_stop!(self);
{
self.schema_expr_stack.borrow_mut().push(());
}
let config_value = self
.walk_expr(&schema_expr.config)
.expect(kcl_error::COMPILE_ERROR_MSG);
Expand Down Expand Up @@ -1936,11 +1940,15 @@ impl<'ctx> TypedResultWalker<'ctx> for LLVMCodeGenContext<'ctx> {
);
// Check the required attributes only when the values of all attributes
// in the final schema are solved.
let is_in_schema = self.schema_stack.borrow().len() > 0;
let is_in_schema =
self.schema_stack.borrow().len() > 0 || self.schema_expr_stack.borrow().len() > 0;
if !is_in_schema {
self.build_void_call(&ApiFunc::kclvm_schema_optional_check.name(), &[schema]);
}
utils::update_ctx_filename(self, &schema_expr.config);
{
self.schema_expr_stack.borrow_mut().pop();
}
Ok(schema)
}

Expand Down
66 changes: 2 additions & 64 deletions kclvm/sema/src/pre_process/config.rs
Expand Up @@ -308,70 +308,8 @@ fn unify_config_entries(
}
}
let mut entries = vec![];
for (key, items) in bucket.iter_mut() {
if key == NAME_NONE_BUCKET_KEY {
entries.append(items);
} else {
let mut schema_index = None;
for (i, item) in items.iter().enumerate() {
if let ast::Expr::Schema(_) = item.node.value.node {
schema_index = Some(i);
break;
}
}
match schema_index {
Some(index) => {
let mut merged_schema = items[index].as_ref().clone();
for (i, item) in items.iter().enumerate() {
match &item.node.value.node {
ast::Expr::Schema(item_schema_expr) => {
if let ast::Expr::Schema(schema_expr) =
&mut merged_schema.node.value.node
{
if let ast::Expr::Config(schema_config) =
&mut schema_expr.config.node
{
if let ast::Expr::Config(config_expr) =
&item_schema_expr.config.node
{
if i < index {
let mut items = config_expr.items.clone();
items.append(&mut schema_config.items);
schema_config.items = items;
} else if i > index {
let mut items = config_expr.items.clone();
schema_config.items.append(&mut items);
}
}
}
}
}
ast::Expr::Config(item_config_expr) => {
if let ast::Expr::Schema(schema_expr) =
&mut merged_schema.node.value.node
{
if let ast::Expr::Config(schema_config) =
&mut schema_expr.config.node
{
if i < index {
let mut items = item_config_expr.items.clone();
items.append(&mut schema_config.items);
schema_config.items = items;
} else if i > index {
let mut items = item_config_expr.items.clone();
schema_config.items.append(&mut items);
}
}
}
}
_ => entries.push(item.clone()),
}
}
entries.push(Box::new(merged_schema));
}
None => entries.append(items),
};
}
for (_, items) in bucket.iter_mut() {
entries.append(items);
}
// Unify config entries recursively.
for entry in &mut entries {
Expand Down
22 changes: 22 additions & 0 deletions test/grammar/unification/schema_simple_10/main.k
@@ -0,0 +1,22 @@
schema Resource:
cpu: int
memory: str

schema Config:
resource: Resource

r = Resource {
cpu = 4
memory = "8Gi"
}

config: Config {
resource: {**r}
}

config: Config {
resource: Resource {
cpu = 2
memory = "4Gi"
}
}
7 changes: 7 additions & 0 deletions test/grammar/unification/schema_simple_10/stdout.golden
@@ -0,0 +1,7 @@
r:
cpu: 4
memory: 8Gi
config:
resource:
cpu: 2
memory: 4Gi
25 changes: 25 additions & 0 deletions test/grammar/unification/schema_simple_11/main.k
@@ -0,0 +1,25 @@
schema Resource:
cpu: int
memory: str

schema Config:
resource: Resource

r = Resource {
cpu = 4
memory = "8Gi"
}

config: Config {
resource: Resource {
cpu = 2
memory = "4Gi"
}
}

config: Config {
resource: r | {
cpu = 8
memory = "16Gi"
}
}
7 changes: 7 additions & 0 deletions test/grammar/unification/schema_simple_11/stdout.golden
@@ -0,0 +1,7 @@
r:
cpu: 4
memory: 8Gi
config:
resource:
cpu: 8
memory: 16Gi
22 changes: 22 additions & 0 deletions test/grammar/unification/schema_simple_9/main.k
@@ -0,0 +1,22 @@
schema Resource:
cpu: int
memory: str

schema Config:
resource: Resource

r = Resource {
cpu = 4
memory = "8Gi"
}

config: Config {
resource: Resource {
cpu = 2
memory = "4Gi"
}
}

config: Config {
resource: r
}
7 changes: 7 additions & 0 deletions test/grammar/unification/schema_simple_9/stdout.golden
@@ -0,0 +1,7 @@
r:
cpu: 4
memory: 8Gi
config:
resource:
cpu: 4
memory: 8Gi

0 comments on commit a217e25

Please sign in to comment.