Skip to content

Commit 5671175

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 54524e1 commit 5671175

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

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

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,18 @@ trait ValidateOps extends ExceptionNames {
1414
val typeProvider: ClassTypeProvider
1515

1616
def attrValidate(attrId: Identifier, attr: AttrLikeSpec, valid: ValidationSpec): Unit = {
17+
val attrTypeRef = attr.dataType.asNonOwning()
1718
val itemValue = Identifier.itemExpr(attrId, attr.cond.repeat)
1819
valid match {
1920
case ValidationEq(expected) =>
20-
attrValidateExprCompare(attrId, attr, Ast.cmpop.Eq, expected, ValidationNotEqualError(attr.dataType))
21+
attrValidateExprCompare(attrId, attr, Ast.cmpop.Eq, expected, ValidationNotEqualError(attrTypeRef))
2122
case ValidationMin(min) =>
22-
attrValidateExprCompare(attrId, attr, Ast.cmpop.GtE, min, ValidationLessThanError(attr.dataType))
23+
attrValidateExprCompare(attrId, attr, Ast.cmpop.GtE, min, ValidationLessThanError(attrTypeRef))
2324
case ValidationMax(max) =>
24-
attrValidateExprCompare(attrId, attr, Ast.cmpop.LtE, max, ValidationGreaterThanError(attr.dataType))
25+
attrValidateExprCompare(attrId, attr, Ast.cmpop.LtE, max, ValidationGreaterThanError(attrTypeRef))
2526
case ValidationRange(min, max) =>
26-
attrValidateExprCompare(attrId, attr, Ast.cmpop.GtE, min, ValidationLessThanError(attr.dataType))
27-
attrValidateExprCompare(attrId, attr, Ast.cmpop.LtE, max, ValidationGreaterThanError(attr.dataType))
27+
attrValidateExprCompare(attrId, attr, Ast.cmpop.GtE, min, ValidationLessThanError(attrTypeRef))
28+
attrValidateExprCompare(attrId, attr, Ast.cmpop.LtE, max, ValidationGreaterThanError(attrTypeRef))
2829
case ValidationAnyOf(values) =>
2930
val bigOrExpr = Ast.expr.BoolOp(
3031
Ast.boolop.Or,
@@ -39,9 +40,9 @@ trait ValidateOps extends ExceptionNames {
3940

4041
attrValidateExpr(
4142
attrId,
42-
attr.dataType,
43+
attrTypeRef,
4344
checkExpr = bigOrExpr,
44-
err = ValidationNotAnyOfError(attr.dataType),
45+
err = ValidationNotAnyOfError(attrTypeRef),
4546
errArgs = List(
4647
itemValue,
4748
Ast.expr.InternalName(IoIdentifier),
@@ -50,17 +51,20 @@ trait ValidateOps extends ExceptionNames {
5051
)
5152
case ValidationExpr(expr) =>
5253
blockScopeHeader
53-
typeProvider._currentIteratorType = Some(attr.dataType)
54+
typeProvider._currentIteratorType = Some(attrTypeRef)
55+
// Store value of attribute in the temporary variable with a name that is
56+
// used for `_` variable, because in expression we refer to current value
57+
// using this variable
5458
handleAssignmentTempVar(
55-
attr.dataType,
59+
attrTypeRef,
5660
translator.translate(Ast.expr.Name(Ast.identifier(Identifier.ITERATOR))),
5761
translator.translate(itemValue)
5862
)
5963
attrValidateExpr(
6064
attrId,
61-
attr.dataType,
65+
attrTypeRef,
6266
expr,
63-
ValidationExprError(attr.dataType),
67+
ValidationExprError(attrTypeRef),
6468
List(
6569
itemValue,
6670
Ast.expr.InternalName(IoIdentifier),
@@ -75,7 +79,7 @@ trait ValidateOps extends ExceptionNames {
7579
val itemValue = Identifier.itemExpr(attrId, attr.cond.repeat)
7680
attrValidateExpr(
7781
attrId,
78-
attr.dataType,
82+
attr.dataType.asNonOwning(),
7983
checkExpr = Ast.expr.Compare(
8084
itemValue,
8185
op,

0 commit comments

Comments
 (0)