Skip to content

Commit 804ee53

Browse files
committed
Borrow data for validation
Using this KSY: ``` - id: one_cat type: cat valid: expr: _._sizeof == 1 ``` For C++11 changes this incorrect code: ``` m_one_cat = std::unique_ptr<cat_t>(new cat_t(m__io, this, m__root)); m_one_cat->_read(); { // Definition: cat_t* one_cat() { return m_one_cat.get(); } std::unique_ptr<cat_t> _ = one_cat(); if (!(1 == 1)) { throw kaitai::validation_expr_error<std::unique_ptr<debug_array_user_t::cat_t>>(one_cat(), _io(), std::string("/seq/0")); } } ``` to this: ``` m_one_cat = std::unique_ptr<cat_t>(new cat_t(m__io, this, m__root)); m_one_cat->_read(); { // Definition: cat_t* one_cat() { return m_one_cat.get(); } cat_t* _ = one_cat(); if (!(1 == 1)) { throw kaitai::validation_expr_error<debug_array_user_t::cat_t*>(one_cat(), _io(), std::string("/seq/0")); } } ```
1 parent bd29078 commit 804ee53

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

shared/src/main/scala/io/kaitai/struct/languages/components/ValidateOps.scala

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,18 @@ trait ValidateOps extends ExceptionNames {
1515
val typeProvider: ClassTypeProvider
1616

1717
def attrValidate(attr: AttrLikeSpec, valid: ValidationSpec): Unit = {
18+
val attrTypeRef = attr.dataType.asNonOwning()
1819
val itemValue = Identifier.itemExpr(attr.id, attr.cond.repeat)
1920
valid match {
2021
case ValidationEq(expected) =>
21-
attrValidateExprCompare(attr, Ast.cmpop.Eq, expected, ValidationNotEqualError(attr.dataType))
22+
attrValidateExprCompare(attr, Ast.cmpop.Eq, expected, ValidationNotEqualError(attrTypeRef))
2223
case ValidationMin(min) =>
23-
attrValidateExprCompare(attr, Ast.cmpop.GtE, min, ValidationLessThanError(attr.dataType))
24+
attrValidateExprCompare(attr, Ast.cmpop.GtE, min, ValidationLessThanError(attrTypeRef))
2425
case ValidationMax(max) =>
25-
attrValidateExprCompare(attr, Ast.cmpop.LtE, max, ValidationGreaterThanError(attr.dataType))
26+
attrValidateExprCompare(attr, Ast.cmpop.LtE, max, ValidationGreaterThanError(attrTypeRef))
2627
case ValidationRange(min, max) =>
27-
attrValidateExprCompare(attr, Ast.cmpop.GtE, min, ValidationLessThanError(attr.dataType))
28-
attrValidateExprCompare(attr, Ast.cmpop.LtE, max, ValidationGreaterThanError(attr.dataType))
28+
attrValidateExprCompare(attr, Ast.cmpop.GtE, min, ValidationLessThanError(attrTypeRef))
29+
attrValidateExprCompare(attr, Ast.cmpop.LtE, max, ValidationGreaterThanError(attrTypeRef))
2930
case ValidationAnyOf(values) =>
3031
val bigOrExpr = Ast.expr.BoolOp(
3132
Ast.boolop.Or,
@@ -41,7 +42,7 @@ trait ValidateOps extends ExceptionNames {
4142
attrValidateExpr(
4243
attr,
4344
checkExpr = bigOrExpr,
44-
err = ValidationNotAnyOfError(attr.dataType),
45+
err = ValidationNotAnyOfError(attrTypeRef),
4546
errArgs = List(
4647
itemValue,
4748
Ast.expr.InternalName(IoIdentifier),
@@ -62,16 +63,19 @@ trait ValidateOps extends ExceptionNames {
6263
)
6364
case ValidationExpr(expr) =>
6465
blockScopeHeader
65-
typeProvider._currentIteratorType = Some(attr.dataType)
66+
typeProvider._currentIteratorType = Some(attrTypeRef)
67+
// Store value of attribute in the temporary variable with a name that is
68+
// used for `_` variable, because in expression we refer to current value
69+
// using this variable
6670
handleAssignmentTempVar(
67-
attr.dataType,
71+
attrTypeRef,
6872
translator.translate(Ast.expr.Name(Ast.identifier(Identifier.ITERATOR))),
6973
translator.translate(itemValue)
7074
)
7175
attrValidateExpr(
7276
attr,
7377
expr,
74-
ValidationExprError(attr.dataType),
78+
ValidationExprError(attrTypeRef),
7579
List(
7680
itemValue,
7781
Ast.expr.InternalName(IoIdentifier),

0 commit comments

Comments
 (0)