Skip to content

Commit

Permalink
fix: assign stmt in schema codegen and runtime value merge. (#787)
Browse files Browse the repository at this point in the history
* fix: schema config key update at runtime

Signed-off-by: peefy <[email protected]>

* fix: schema unification optional annotation

Signed-off-by: peefy <[email protected]>

* chore: remove un-used code in lsp

Signed-off-by: peefy <[email protected]>

* test: add more test cases for schema inherit and modification in-place.

Signed-off-by: peefy <[email protected]>

* fix: normal attribute assign in the schema and add more test cases

Signed-off-by: peefy <[email protected]>

* fix: wrong grammar test cases for assign stmt in schema.

Signed-off-by: peefy <[email protected]>

---------

Signed-off-by: peefy <[email protected]>
  • Loading branch information
Peefy authored Oct 18, 2023
1 parent 4486c08 commit 630cf64
Show file tree
Hide file tree
Showing 24 changed files with 271 additions and 5 deletions.
4 changes: 2 additions & 2 deletions compiler_base/error/src/diagnostic/diagnostic_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
//! For more information about template loader, see doc in "compiler_base/error/src/diagnostic/diagnostic_message.rs".

use crate::{
diagnostic::diagnostic_message::TemplateLoader, emit_diagnostic_to_uncolored_text,
emitter::EmitResultText, Diagnostic, DiagnosticStyle, Emitter, EmitterWriter,
diagnostic::diagnostic_message::TemplateLoader, emit_diagnostic_to_uncolored_text, Diagnostic,
DiagnosticStyle, Emitter, EmitterWriter,
};
use anyhow::{bail, Context, Result};
use compiler_base_span::fatal_error::FatalError;
Expand Down
93 changes: 93 additions & 0 deletions kclvm/compiler/src/codegen/llvm/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2459,6 +2459,99 @@ impl<'ctx> LLVMCodeGenContext<'ctx> {
right_value.expect(kcl_error::INTERNAL_ERROR_MSG),
],
);

let is_local_var = {
let local_vars = self.local_vars.borrow_mut();
local_vars.contains(name)
};
let is_not_in_lambda = self.lambda_stack.borrow().is_empty();
// Set config value for the schema attribute if the attribute is in the schema and
// it is not a local variable in the lambda function.
if self.scope_level() >= INNER_LEVEL
&& is_in_schema
&& !is_not_in_lambda
&& !is_local_var
{
let schema_value = self
.get_variable(value::SCHEMA_SELF_NAME)
.expect(kcl_error::INTERNAL_ERROR_MSG);
let config_value = self
.get_variable(value::SCHEMA_CONFIG_NAME)
.expect(kcl_error::INTERNAL_ERROR_MSG);
let string_ptr_value =
self.native_global_string(name, "").into();
let has_key = self
.build_call(
&ApiFunc::kclvm_dict_has_value.name(),
&[config_value, string_ptr_value],
)
.into_int_value();
// The config has the attribute key?
let has_key = self.builder.build_int_compare(
IntPredicate::NE,
has_key,
self.native_i8_zero(),
"",
);
let last_block = self.append_block("");
let then_block = self.append_block("");
let else_block = self.append_block("");
self.br(last_block);
self.builder.position_at_end(last_block);
let none_value = self.none_value();
self.builder
.build_conditional_branch(has_key, then_block, else_block);
self.builder.position_at_end(then_block);
let config_entry = self.build_call(
&ApiFunc::kclvm_dict_get_entry.name(),
&[config_value, string_ptr_value],
);
self.br(else_block);
self.builder.position_at_end(else_block);
let tpe = self.value_ptr_type();
let phi = self.builder.build_phi(tpe, "");
phi.add_incoming(&[
(&none_value, last_block),
(&config_entry, then_block),
]);
let config_value = phi.as_basic_value();
self.value_union(schema_value, config_value);
let cal_map = self
.get_variable(value::SCHEMA_CAL_MAP)
.expect(kcl_error::INTERNAL_ERROR_MSG);
let backtrack_cache = self
.get_variable(value::BACKTRACK_CACHE)
.expect(kcl_error::INTERNAL_ERROR_MSG);
let runtime_type = self
.get_variable(value::SCHEMA_RUNTIME_TYPE)
.expect(kcl_error::INTERNAL_ERROR_MSG);
let name_native_str = self.native_global_string_value(name);
self.build_void_call(
&ApiFunc::kclvm_schema_backtrack_cache.name(),
&[
schema_value,
backtrack_cache,
cal_map,
name_native_str,
runtime_type,
],
);
// Update backtrack meta
{
if let Some(backtrack_meta) =
self.backtrack_meta.borrow_mut().as_mut()
{
if name == backtrack_meta.target {
backtrack_meta.count += 1;
if backtrack_meta.count == backtrack_meta.level {
backtrack_meta.stop = true;
self.ret(schema_value);
return Ok(schema_value);
}
}
}
}
}
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions kclvm/runtime/src/value/val_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,8 @@ impl ValueRef {
let values = &mut schema.config.values;
let ops = &mut schema.config.ops;
let insert_indexs = &mut schema.config.insert_indexs;
// Reserve config keys for the schema update process. Issue: #785
schema.config_keys = value.config_keys.clone();
for (k, v) in &value.config.values {
let op = value
.config
Expand Down
2 changes: 1 addition & 1 deletion kclvm/sema/src/resolver/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ impl<'ctx> Resolver<'ctx> {
ast::Stmt::Unification(unification_stmt) => {
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 is_optional = false;
let default = if self.options.resolve_val {
print_schema_expr(&unification_stmt.value.node)
} else {
Expand Down
1 change: 1 addition & 0 deletions kclvm/tools/src/LSP/src/capabilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use lsp_types::{
};

/// Returns the capabilities of this LSP server implementation given the capabilities of the client.
#[allow(dead_code)]
pub fn server_capabilities(client_caps: &ClientCapabilities) -> ServerCapabilities {
ServerCapabilities {
text_document_sync: Some(TextDocumentSyncCapability::Kind(TextDocumentSyncKind::FULL)),
Expand Down
14 changes: 14 additions & 0 deletions test/grammar/schema/assign_stmt/assign_stmt_0/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
schema Metadata:
environment?: str = "qa"
region?: str
name?: str

schema MySchema1:
metadata?: Metadata = {}

metadata.environment = "dev"
metadata.region = "us-east-1"

output = MySchema1 {
metadata.name = "config"
}
5 changes: 5 additions & 0 deletions test/grammar/schema/assign_stmt/assign_stmt_0/stdout.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
output:
metadata:
environment: dev
region: us-east-1
name: config
14 changes: 14 additions & 0 deletions test/grammar/schema/assign_stmt/assign_stmt_1/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
schema Metadata:
environment?: str = "qa"
region?: str
name?: str

schema MySchema1:
metadata?: Metadata = {}

metadata.environment = "dev"


output = MySchema1 {
metadata.name = "config"
}
4 changes: 4 additions & 0 deletions test/grammar/schema/assign_stmt/assign_stmt_1/stdout.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
output:
metadata:
environment: dev
name: config
13 changes: 13 additions & 0 deletions test/grammar/schema/assign_stmt/assign_stmt_2/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
schema Metadata:
environment?: str = "qa"
region?: str
name?: str

schema MySchema1:
metadata?: Metadata = {
name = "config"
}
metadata.environment = "dev"
metadata.region = "us-east-1"

output = MySchema1 {}
5 changes: 5 additions & 0 deletions test/grammar/schema/assign_stmt/assign_stmt_2/stdout.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
output:
metadata:
environment: dev
region: us-east-1
name: config
16 changes: 16 additions & 0 deletions test/grammar/schema/assign_stmt/assign_stmt_3/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
schema Metadata:
environment: str
region: str
name: str

schema MySchema1:
metadata: Metadata

schema MySchema2(MySchema1):
metadata.environment = "dev"

schema MySchema3(MySchema2):
metadata.region = "us-east-1"

output = MySchema3 {metadata.name = "hello"}

5 changes: 5 additions & 0 deletions test/grammar/schema/assign_stmt/assign_stmt_3/stdout.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
output:
metadata:
environment: dev
region: us-east-1
name: hello
18 changes: 18 additions & 0 deletions test/grammar/schema/assign_stmt/assign_stmt_4/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
schema Metadata:
environment: str
region: str
name: str

schema MySchema1:
metadata: Metadata

schema MySchema2(MySchema1):
metadata.environment = "dev"

schema MySchema3(MySchema2):
metadata.region = "us-east-1"

output = MySchema3 {
metadata.name = "hello"
metadata.environment = "qa"
}
5 changes: 5 additions & 0 deletions test/grammar/schema/assign_stmt/assign_stmt_4/stdout.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
output:
metadata:
environment: qa
region: us-east-1
name: hello
9 changes: 9 additions & 0 deletions test/grammar/schema/assign_stmt/assign_stmt_5/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
schema MySchema:
metadata: {str:} = {}

metadata.environment = "dev"


output = MySchema {
metadata.name = "config"
}
4 changes: 4 additions & 0 deletions test/grammar/schema/assign_stmt/assign_stmt_5/stdout.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
output:
metadata:
name: config
environment: dev
10 changes: 10 additions & 0 deletions test/grammar/schema/assign_stmt/assign_stmt_6/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
schema MySchema:
metadata: {str:} = {}

metadata.environment = "dev"


output = MySchema {
metadata.environment = "qa"
metadata.name = "config"
}
4 changes: 4 additions & 0 deletions test/grammar/schema/assign_stmt/assign_stmt_6/stdout.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
output:
metadata:
environment: qa
name: config
4 changes: 2 additions & 2 deletions test/grammar/schema/init/init_nested_schema_1/stdout.golden
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
alice:
name:
name:
name: rename
gender: female
name: alice
gender: female
16 changes: 16 additions & 0 deletions test/grammar/schema/optional_attr/inherit_2/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
schema Metadata:
environment: str
region: str
name: str

schema MySchema1:
metadata: Metadata

schema MySchema2(MySchema1):
metadata: Metadata {environment = "dev"}

schema MySchema3(MySchema2):
metadata: Metadata {region = "us-east-1"}

output = MySchema3 {metadata.name = "hello"}

5 changes: 5 additions & 0 deletions test/grammar/schema/optional_attr/inherit_2/stdout.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
output:
metadata:
environment: dev
region: us-east-1
name: hello
18 changes: 18 additions & 0 deletions test/grammar/schema/optional_attr/inherit_3/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
schema Metadata:
environment: str
region: str
name: str

schema MySchema1:
metadata: Metadata

schema MySchema2(MySchema1):
metadata: Metadata {environment = "dev"}

schema MySchema3(MySchema2):
metadata: Metadata {region = "us-east-1"}

output = MySchema3 {
metadata.name = "hello"
metadata.environment = "qa"
}
5 changes: 5 additions & 0 deletions test/grammar/schema/optional_attr/inherit_3/stdout.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
output:
metadata:
environment: qa
region: us-east-1
name: hello

0 comments on commit 630cf64

Please sign in to comment.