Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix problems with ownership in validation code #301

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ClassTypeProvider(classSpecs: ClassSpecs, var topClass: ClassSpec) extends
inClass.parentClass.toDataType
case Identifier.IO =>
KaitaiStreamType
case Identifier.ITERATOR =>
case Identifier.THIS =>
currentIteratorType
case Identifier.SWITCH_ON =>
currentSwitchType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ object Identifier {
val ROOT = "_root"
val PARENT = "_parent"
val IO = "_io"
val ITERATOR = "_"
val ITERATOR2 = "_buf"
val THIS = "_"
val THIS_RAW = "_buf"
val INDEX = "_index"
val SWITCH_ON = "_on"
val IS_LE = "_is_le"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ class CSharpCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
val ioName = s"io_$privateVarName"

val args = rep match {
case RepeatUntil(_) => translator.doName(Identifier.ITERATOR2)
case RepeatUntil(_) => translator.doName(Identifier.THIS_RAW)
case _ => getRawIdExpr(varName, rep)
}

Expand Down Expand Up @@ -322,9 +322,9 @@ class CSharpCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)

override def handleAssignmentRepeatUntil(id: Identifier, expr: String, isRaw: Boolean): Unit = {
val (typeDecl, tempVar) = if (isRaw) {
("byte[] ", translator.doName(Identifier.ITERATOR2))
("byte[] ", translator.doName(Identifier.THIS_RAW))
} else {
("", translator.doName(Identifier.ITERATOR))
("", translator.doName(Identifier.THIS))
}
out.puts(s"$typeDecl$tempVar = $expr;")
out.puts(s"${privateMemberName(id)}.Add($tempVar);")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ class CppCompiler(
val ioId = IoStorageIdentifier(id)

val args = rep match {
case RepeatUntil(_) => translator.doName(Identifier.ITERATOR2)
case RepeatUntil(_) => translator.doName(Identifier.THIS_RAW)
case _ => getRawIdExpr(id, rep)
}

Expand Down Expand Up @@ -619,9 +619,9 @@ class CppCompiler(

override def handleAssignmentRepeatUntil(id: Identifier, expr: String, isRaw: Boolean): Unit = {
val (typeDecl, tempVar) = if (isRaw) {
("std::string ", translator.doName(Identifier.ITERATOR2))
("std::string ", translator.doName(Identifier.THIS_RAW))
} else {
("", translator.doName(Identifier.ITERATOR))
("", translator.doName(Identifier.THIS))
}

val (wrappedTempVar, rawPtrExpr) = if (config.cppConfig.pointers == UniqueAndRawPointers) {
Expand Down Expand Up @@ -1039,7 +1039,7 @@ class CppCompiler(
case UndecidedEndiannessError => "kaitai::undecided_endianness_error"
case ConversionError => "std::invalid_argument"
case validationErr: ValidationError =>
val cppType = kaitaiType2NativeType(validationErr.dt, true)
val cppType = kaitaiType2NativeType(validationErr.dt)
val cppErrName = validationErr match {
case _: ValidationNotEqualError => "validation_not_equal_error"
case _: ValidationLessThanError => "validation_less_than_error"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ class GoCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
val ioName = idToStr(IoStorageIdentifier(varName))

val args = rep match {
case RepeatUntil(_) => translator.specialName(Identifier.ITERATOR2)
case RepeatUntil(_) => translator.specialName(Identifier.THIS_RAW)
case _ => getRawIdExpr(varName, rep)
}

Expand Down Expand Up @@ -341,7 +341,7 @@ class GoCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)

override def handleAssignmentRepeatUntil(id: Identifier, r: TranslatorResult, isRaw: Boolean): Unit = {
val expr = translator.resToStr(r)
val tempVar = translator.specialName(if (isRaw) Identifier.ITERATOR2 else Identifier.ITERATOR)
val tempVar = translator.specialName(if (isRaw) Identifier.THIS_RAW else Identifier.THIS)
out.puts(s"$tempVar := $expr")
out.puts(s"${privateMemberName(id)} = append(${privateMemberName(id)}, $tempVar)")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ class JavaCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
val ioName = idToStr(IoStorageIdentifier(varName))

val args = rep match {
case RepeatUntil(_) => translator.doName(Identifier.ITERATOR2)
case RepeatUntil(_) => translator.doName(Identifier.THIS_RAW)
case _ => getRawIdExpr(varName, rep)
}

Expand Down Expand Up @@ -400,9 +400,9 @@ class JavaCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)

override def handleAssignmentRepeatUntil(id: Identifier, expr: String, isRaw: Boolean): Unit = {
val (typeDecl, tempVar) = if (isRaw) {
("byte[] ", translator.doName(Identifier.ITERATOR2))
("byte[] ", translator.doName(Identifier.THIS_RAW))
} else {
("", translator.doName(Identifier.ITERATOR))
("", translator.doName(Identifier.THIS))
}
out.puts(s"$typeDecl$tempVar = $expr;")
out.puts(s"${privateMemberName(id)}.add($tempVar);")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ class JavaScriptCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
}

override def handleAssignmentRepeatUntil(id: Identifier, expr: String, isRaw: Boolean): Unit = {
val tmpName = translator.doName(if (isRaw) Identifier.ITERATOR2 else Identifier.ITERATOR)
val tmpName = translator.doName(if (isRaw) Identifier.THIS_RAW else Identifier.THIS)
out.puts(s"var $tmpName = $expr;")
out.puts(s"${privateMemberName(id)}.push($tmpName);")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ class LuaCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
override def handleAssignmentRepeatExpr(id: Identifier, expr: String): Unit =
out.puts(s"${privateMemberName(id)}[i + 1] = $expr")
override def handleAssignmentRepeatUntil(id: Identifier, expr: String, isRaw: Boolean): Unit = {
val tmpName = translator.doName(if (isRaw) Identifier.ITERATOR2 else Identifier.ITERATOR)
val tmpName = translator.doName(if (isRaw) Identifier.THIS_RAW else Identifier.THIS)
out.puts(s"local $tmpName = $expr")
out.puts(s"${privateMemberName(id)}[i + 1] = $tmpName")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class NimCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
val ioName = s"${idToStr(id)}Io"
val arg = rep match {
case NoRepeat => idToStr(id) + "Expr"
case _ => translator.doName(Identifier.ITERATOR2)
case _ => translator.doName(Identifier.THIS_RAW)
}
out.puts(s"let $ioName = newKaitaiStream($arg)")
ioName
Expand Down Expand Up @@ -369,8 +369,8 @@ class NimCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
def handleAssignmentIterative(id: Identifier, expr: String): Unit = {
// Need better design for this XXX
val exprName = id match {
case _: RawIdentifier => translator.doName(Identifier.ITERATOR2)
case _ => translator.doName(Identifier.ITERATOR)
case _: RawIdentifier => translator.doName(Identifier.THIS_RAW)
case _ => translator.doName(Identifier.THIS)
}
out.puts(s"let $exprName = $expr")
out.puts(s"${privateMemberName(id)}.add($exprName)")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ class PHPCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
val ioName = s"$$_io_${idToStr(id)}"

val args = rep match {
case RepeatUntil(_) => translator.doLocalName(Identifier.ITERATOR2)
case RepeatUntil(_) => translator.doLocalName(Identifier.THIS_RAW)
case _ => getRawIdExpr(id, rep)
}

Expand Down Expand Up @@ -313,7 +313,7 @@ class PHPCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
}

override def handleAssignmentRepeatUntil(id: Identifier, expr: String, isRaw: Boolean): Unit = {
val tmpName = translator.doLocalName(if (isRaw) Identifier.ITERATOR2 else Identifier.ITERATOR)
val tmpName = translator.doLocalName(if (isRaw) Identifier.THIS_RAW else Identifier.THIS)
out.puts(s"$tmpName = $expr;")
out.puts(s"${privateMemberName(id)}[] = $tmpName;")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ class PerlCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
val memberName = privateMemberName(id)

val args = rep match {
case RepeatUntil(_) => translator.doName(Identifier.ITERATOR2)
case RepeatUntil(_) => translator.doName(Identifier.THIS_RAW)
case _ => getRawIdExpr(id, rep)
}

Expand Down Expand Up @@ -271,9 +271,9 @@ class PerlCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)

override def handleAssignmentRepeatUntil(id: Identifier, expr: String, isRaw: Boolean): Unit = {
val (decl, tmpName) = if (isRaw) {
("my ", translator.doName(Identifier.ITERATOR2))
("my ", translator.doName(Identifier.THIS_RAW))
} else {
("", translator.doName(Identifier.ITERATOR))
("", translator.doName(Identifier.THIS))
}
out.puts(s"$decl$tmpName = $expr;")
out.puts(s"push @{${privateMemberName(id)}}, $tmpName;")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ class PythonCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
}

override def handleAssignmentRepeatUntil(id: Identifier, expr: String, isRaw: Boolean): Unit = {
val tmpName = translator.doName(if (isRaw) Identifier.ITERATOR2 else Identifier.ITERATOR)
val tmpName = translator.doName(if (isRaw) Identifier.THIS_RAW else Identifier.THIS)
out.puts(s"$tmpName = $expr")
out.puts(s"${privateMemberName(id)}.append($tmpName)")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ class RubyCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
}

override def handleAssignmentRepeatUntil(id: Identifier, expr: String, isRaw: Boolean): Unit = {
val tmpName = translator.doName(if (isRaw) Identifier.ITERATOR2 else Identifier.ITERATOR)
val tmpName = translator.doName(if (isRaw) Identifier.THIS_RAW else Identifier.THIS)
out.puts(s"$tmpName = $expr")
out.puts(s"${privateMemberName(id)} << $tmpName")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ class RustCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
}
val t = localTemporaryName(id)
out.puts(s"let $t = ${privateMemberName(id)};")
out.puts(s"let ${translator.doLocalName(Identifier.ITERATOR)} = $copy_type$t.last().unwrap();")
out.puts(s"let ${translator.doLocalName(Identifier.THIS)} = $copy_type$t.last().unwrap();")
}

override def condRepeatUntilFooter(id: Identifier,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,18 @@ trait ValidateOps extends ExceptionNames {
val typeProvider: ClassTypeProvider

def attrValidate(attr: AttrLikeSpec, valid: ValidationSpec): Unit = {
val attrTypeRef = attr.dataType.asNonOwning()
val itemValue = Identifier.itemExpr(attr.id, attr.cond.repeat)
valid match {
case ValidationEq(expected) =>
attrValidateExprCompare(attr, Ast.cmpop.Eq, expected, ValidationNotEqualError(attr.dataType))
attrValidateExprCompare(attr, Ast.cmpop.Eq, expected, ValidationNotEqualError(attrTypeRef))
case ValidationMin(min) =>
attrValidateExprCompare(attr, Ast.cmpop.GtE, min, ValidationLessThanError(attr.dataType))
attrValidateExprCompare(attr, Ast.cmpop.GtE, min, ValidationLessThanError(attrTypeRef))
case ValidationMax(max) =>
attrValidateExprCompare(attr, Ast.cmpop.LtE, max, ValidationGreaterThanError(attr.dataType))
attrValidateExprCompare(attr, Ast.cmpop.LtE, max, ValidationGreaterThanError(attrTypeRef))
case ValidationRange(min, max) =>
attrValidateExprCompare(attr, Ast.cmpop.GtE, min, ValidationLessThanError(attr.dataType))
attrValidateExprCompare(attr, Ast.cmpop.LtE, max, ValidationGreaterThanError(attr.dataType))
attrValidateExprCompare(attr, Ast.cmpop.GtE, min, ValidationLessThanError(attrTypeRef))
attrValidateExprCompare(attr, Ast.cmpop.LtE, max, ValidationGreaterThanError(attrTypeRef))
case ValidationAnyOf(values) =>
val bigOrExpr = Ast.expr.BoolOp(
Ast.boolop.Or,
Expand All @@ -41,7 +42,7 @@ trait ValidateOps extends ExceptionNames {
attrValidateExpr(
attr,
checkExpr = bigOrExpr,
err = ValidationNotAnyOfError(attr.dataType),
err = ValidationNotAnyOfError(attrTypeRef),
errArgs = List(
itemValue,
Ast.expr.InternalName(IoIdentifier),
Expand All @@ -62,16 +63,19 @@ trait ValidateOps extends ExceptionNames {
)
case ValidationExpr(expr) =>
blockScopeHeader
typeProvider._currentIteratorType = Some(attr.dataType)
typeProvider._currentIteratorType = Some(attrTypeRef)
// Store value of attribute in the temporary variable with a name that is
// used for `_` variable, because in expression we refer to current value
// using this variable
handleAssignmentTempVar(
attr.dataType,
translator.translate(Ast.expr.Name(Ast.identifier(Identifier.ITERATOR))),
attrTypeRef,
translator.translate(Ast.expr.Name(Ast.identifier(Identifier.THIS))),
translator.translate(itemValue)
)
attrValidateExpr(
attr,
expr,
ValidationExprError(attr.dataType),
ValidationExprError(attrTypeRef),
List(
itemValue,
Ast.expr.InternalName(IoIdentifier),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import io.kaitai.struct.ConstructClassCompiler
class ConstructTranslator(provider: TypeProvider, importList: ImportList) extends PythonTranslator(provider, importList, RuntimeConfig()) {
override def doLocalName(s: String) = {
s match {
case Identifier.ITERATOR => "obj_"
case Identifier.THIS => "obj_"
case Identifier.INDEX => "i"
case Identifier.ROOT => "this._root"
case Identifier.IO => "_io"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ class CppTranslator(provider: TypeProvider, importListSrc: CppImportList, import
s"${translate(value)}->${doName(attrName)}"

override def doName(s: String) = s match {
case Identifier.ITERATOR => "_"
case Identifier.ITERATOR2 => "_buf"
case Identifier.THIS => "_"
case Identifier.THIS_RAW => "_buf"
case Identifier.INDEX => "i"
case _ => s"$s()"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,8 @@ class GoTranslator(out: StringLanguageOutputWriter, provider: TypeProvider, impo
ResultString(s"this.${specialName(s)}")

// These can be local only
case Identifier.ITERATOR |
Identifier.ITERATOR2 =>
case Identifier.THIS |
Identifier.THIS_RAW =>
ResultString(specialName(s))
case Identifier.INDEX => ResultString("i")

Expand All @@ -228,9 +228,9 @@ class GoTranslator(out: StringLanguageOutputWriter, provider: TypeProvider, impo
def specialName(id: String): String = id match {
case Identifier.ROOT | Identifier.PARENT | Identifier.IO =>
id
case Identifier.ITERATOR =>
case Identifier.THIS =>
"_it"
case Identifier.ITERATOR2 =>
case Identifier.THIS_RAW =>
"_buf"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ class JavaTranslator(provider: TypeProvider, importList: ImportList) extends Bas

override def doName(s: String) =
s match {
case Identifier.ITERATOR => "_it"
case Identifier.ITERATOR2 => "_buf"
case Identifier.THIS => "_it"
case Identifier.THIS_RAW => "_buf"
case Identifier.SWITCH_ON => "on"
case Identifier.INDEX => "i"
case _ => s"${Utils.lowerCamelCase(s)}()"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class LuaTranslator(provider: TypeProvider, importList: ImportList) extends Base
"string.char(" + values.map(translate).mkString(", ") + ")"

override def doLocalName(s: String) = s match {
case Identifier.ITERATOR => "_"
case Identifier.THIS => "_"
case Identifier.INDEX => "i"
case _ => s"self.${doName(s)}"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class NimTranslator(provider: TypeProvider, importList: ImportList) extends Base
case Identifier.ROOT => "root"
case Identifier.PARENT => "parent"
case Identifier.IO => "io"
case Identifier.ITERATOR => "it"
case Identifier.ITERATOR2 => "buf"
case Identifier.THIS => "it"
case Identifier.THIS_RAW => "buf"
case Identifier.INDEX => "i"
case Identifier.SWITCH_ON => "on"
case Identifier.IS_LE => "isLe"
Expand All @@ -32,7 +32,7 @@ class NimTranslator(provider: TypeProvider, importList: ImportList) extends Base
}
override def doLocalName(s: String): String =
s match {
case Identifier.ITERATOR => doName(s)
case Identifier.THIS => doName(s)
case Identifier.INDEX => doName(s)
case Identifier.ROOT => s"${ksToNim(provider.determineType(Identifier.ROOT))}(this.${doName(s)})"
case _ => s"this.${doName(s)}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ class PHPTranslator(provider: TypeProvider, config: RuntimeConfig) extends BaseT

override def doLocalName(s: String) = {
s match {
case Identifier.ITERATOR => "$_"
case Identifier.ITERATOR2 => "$_buf"
case Identifier.THIS => "$_"
case Identifier.THIS_RAW => "$_buf"
case Identifier.INDEX => "$i"
case _ => s"$$this->${doName(s)}"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ class PerlTranslator(provider: TypeProvider, importList: ImportList) extends Bas

override def doName(s: String) = {
s match {
case Identifier.ITERATOR => "$_"
case Identifier.ITERATOR2 => "$_buf"
case Identifier.THIS => "$_"
case Identifier.THIS_RAW => "$_buf"
case Identifier.INDEX => "$i"
case _ => s"$s()"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class PythonTranslator(provider: TypeProvider, importList: ImportList, config: R

override def doLocalName(s: String) = {
s match {
case Identifier.ITERATOR => "_"
case Identifier.THIS => "_"
case Identifier.INDEX => "i"
case _ => s"self.${doName(s)}"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,8 @@ class RustTranslator(provider: TypeProvider, config: RuntimeConfig)
}

override def doLocalName(s: String): String = s match {
case Identifier.ITERATOR => "_tmpa"
case Identifier.ITERATOR2 => "_tmpb"
case Identifier.THIS => "_tmpa"
case Identifier.THIS_RAW => "_tmpb"
case Identifier.INDEX => "_i"
case Identifier.IO => s"${RustCompiler.privateMemberName(IoIdentifier)}"
case Identifier.ROOT => "_r"
Expand Down
Loading