Skip to content

Commit

Permalink
Remove unused and unfinished SharedPointers support
Browse files Browse the repository at this point in the history
Extracted from #276

In SharedPointers mode, the compiler never generates correct code and in
many cases throws a `scala.MatchError` due to missing arms in switches.

It was not possible to run the compiler with this setting anyway, so it
is better to remove it for now until it is fully implemented.

Fixes the following warnings (13):
```
[warn] /home/runner/work/kaitai_struct_compiler/kaitai_struct_compiler/compiler/shared/src/main/scala/io/kaitai/struct/languages/CppCompiler.scala:511:26: match may not be exhaustive.
[warn] It would fail on the following input: SharedPointers
[warn]         config.cppConfig.pointers match {
[warn]                          ^
[warn] /home/runner/work/kaitai_struct_compiler/kaitai_struct_compiler/compiler/shared/src/main/scala/io/kaitai/struct/languages/CppCompiler.scala:511:26: match may not be exhaustive.
[warn] It would fail on the following input: SharedPointers
[warn]         config.cppConfig.pointers match {
[warn]                          ^
[warn] /home/runner/work/kaitai_struct_compiler/kaitai_struct_compiler/compiler/shared/src/main/scala/io/kaitai/struct/languages/CppCompiler.scala:727:22: match may not be exhaustive.
[warn] It would fail on the following input: SharedPointers
[warn]     config.cppConfig.pointers match {
[warn]                      ^
[warn] /home/runner/work/kaitai_struct_compiler/kaitai_struct_compiler/compiler/shared/src/main/scala/io/kaitai/struct/languages/CppCompiler.scala:870:24: match may not be exhaustive.
[warn] It would fail on the following input: SharedPointers
[warn]       config.cppConfig.pointers match {
[warn]                        ^
[warn] /home/runner/work/kaitai_struct_compiler/kaitai_struct_compiler/compiler/shared/src/main/scala/io/kaitai/struct/languages/CppCompiler.scala:980:22: match may not be exhaustive.
[warn] It would fail on the following input: SharedPointers
[warn]     config.cppConfig.pointers match {
[warn]                      ^
[warn] /home/runner/work/kaitai_struct_compiler/kaitai_struct_compiler/compiler/shared/src/main/scala/io/kaitai/struct/languages/CppCompiler.scala:727:22: match may not be exhaustive.
[warn] It would fail on the following input: SharedPointers
[warn]     config.cppConfig.pointers match {
[warn]                      ^
[warn] /home/runner/work/kaitai_struct_compiler/kaitai_struct_compiler/compiler/shared/src/main/scala/io/kaitai/struct/languages/CppCompiler.scala:1103:48: match may not be exhaustive.
[warn] It would fail on the following input: SharedPointers
[warn]       case ArrayTypeInStream(inType) => config.pointers match {
[warn]                                                ^
[warn] /home/runner/work/kaitai_struct_compiler/kaitai_struct_compiler/compiler/shared/src/main/scala/io/kaitai/struct/languages/CppCompiler.scala:1108:44: match may not be exhaustive.
[warn] It would fail on the following input: SharedPointers
[warn]       case OwnedKaitaiStreamType => config.pointers match {
[warn]                                            ^
[warn] /home/runner/work/kaitai_struct_compiler/kaitai_struct_compiler/compiler/shared/src/main/scala/io/kaitai/struct/languages/CppCompiler.scala:870:24: match may not be exhaustive.
[warn] It would fail on the following input: SharedPointers
[warn]       config.cppConfig.pointers match {
[warn]                        ^
[warn] /home/runner/work/kaitai_struct_compiler/kaitai_struct_compiler/compiler/shared/src/main/scala/io/kaitai/struct/languages/CppCompiler.scala:980:22: match may not be exhaustive.
[warn] It would fail on the following input: SharedPointers
[warn]     config.cppConfig.pointers match {
[warn]                      ^
[warn] /home/runner/work/kaitai_struct_compiler/kaitai_struct_compiler/compiler/shared/src/main/scala/io/kaitai/struct/languages/CppCompiler.scala:1103:48: match may not be exhaustive.
[warn] It would fail on the following input: SharedPointers
[warn]       case ArrayTypeInStream(inType) => config.pointers match {
[warn]                                                ^
[warn] /home/runner/work/kaitai_struct_compiler/kaitai_struct_compiler/compiler/shared/src/main/scala/io/kaitai/struct/languages/CppCompiler.scala:1108:44: match may not be exhaustive.
[warn] It would fail on the following input: SharedPointers
[warn]       case OwnedKaitaiStreamType => config.pointers match {
[warn]                                            ^
[warn] /home/runner/work/kaitai_struct_compiler/kaitai_struct_compiler/compiler/shared/src/main/scala/io/kaitai/struct/translators/CppTranslator.scala:106:24: match may not be exhaustive.
[warn] It would fail on the following input: SharedPointers
[warn]       config.cppConfig.pointers match {
[warn]                        ^
```
  • Loading branch information
Mingun authored and generalmimon committed Apr 3, 2024
1 parent c8a2667 commit 751f059
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 47 deletions.
1 change: 0 additions & 1 deletion shared/src/main/scala/io/kaitai/struct/RuntimeConfig.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ case class CppRuntimeConfig(
object CppRuntimeConfig {
sealed trait Pointers
case object RawPointers extends Pointers
case object SharedPointers extends Pointers
case object UniqueAndRawPointers extends Pointers
}

Expand Down
39 changes: 6 additions & 33 deletions shared/src/main/scala/io/kaitai/struct/languages/CppCompiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class CppCompiler(
importListHdr.addSystem("stdint.h")

config.cppConfig.pointers match {
case SharedPointers | UniqueAndRawPointers =>
case UniqueAndRawPointers =>
importListHdr.addSystem("memory")
case RawPointers =>
// no extra includes
Expand Down Expand Up @@ -116,13 +116,8 @@ class CppCompiler(
override def classHeader(name: List[String]): Unit = {
val className = types2class(List(name.last))

val extraInherits = config.cppConfig.pointers match {
case RawPointers | UniqueAndRawPointers => ""
case SharedPointers => s", std::enable_shared_from_this<$className>"
}

outHdr.puts
outHdr.puts(s"class $className : public $kstructName$extraInherits {")
outHdr.puts(s"class $className : public $kstructName {")
outHdr.inc
accessMode = PrivateAccess
ensureMode(PublicAccess)
Expand Down Expand Up @@ -202,19 +197,9 @@ class CppCompiler(
)
outSrc.inc

// In shared pointers mode, this is required to be able to work with shared pointers to this
// in a constructor. This is obviously a hack and not a good practice.
// https://forum.libcinder.org/topic/solution-calling-shared-from-this-in-the-constructor
if (config.cppConfig.pointers == CppRuntimeConfig.SharedPointers) {
outSrc.puts(s"const auto weakPtrTrick = std::shared_ptr<$classNameBrief>(this, []($classNameBrief*){});")
}

handleAssignmentSimple(ParentIdentifier, pParent)
handleAssignmentSimple(RootIdentifier, if (name == rootClassName) {
config.cppConfig.pointers match {
case RawPointers | UniqueAndRawPointers => "this"
case SharedPointers => "shared_from_this()"
}
"this"
} else {
pRoot
})
Expand Down Expand Up @@ -697,11 +682,7 @@ class CppCompiler(
val parent = t.forcedParent match {
case Some(USER_TYPE_NO_PARENT) => nullPtr
case Some(fp) => translator.translate(fp)
case None =>
config.cppConfig.pointers match {
case RawPointers | UniqueAndRawPointers => "this"
case SharedPointers => s"shared_from_this()"
}
case None => "this"
}
val addEndian = t.classSpec.get.meta.endian match {
case Some(InheritedEndian) => ", m__is_le"
Expand All @@ -712,8 +693,6 @@ class CppCompiler(
config.cppConfig.pointers match {
case RawPointers =>
s"new ${types2class(t.name)}($addParams$io$addArgs)"
case SharedPointers =>
s"std::make_shared<${types2class(t.name)}>($addParams$io$addArgs)"
case UniqueAndRawPointers =>
// C++14
//s"std::make_unique<${types2class(t.name)}>($addParams$io$addArgs)"
Expand Down Expand Up @@ -973,7 +952,7 @@ class CppCompiler(

def nullPtr: String = config.cppConfig.pointers match {
case RawPointers => "0"
case SharedPointers | UniqueAndRawPointers => "nullptr"
case UniqueAndRawPointers => "nullptr"
}

def nonOwningPointer(attrName: Identifier, attrType: DataType): String = {
Expand Down Expand Up @@ -1088,7 +1067,6 @@ object CppCompiler extends LanguageCompilerStatic
})
config.pointers match {
case RawPointers => s"$typeStr*"
case SharedPointers => s"std::shared_ptr<$typeStr>"
case UniqueAndRawPointers =>
if (t.isOwning) s"std::unique_ptr<$typeStr>" else s"$typeStr*"
}
Expand All @@ -1112,14 +1090,9 @@ object CppCompiler extends LanguageCompilerStatic
case KaitaiStreamType => s"$kstreamName*"
case KaitaiStructType => config.pointers match {
case RawPointers => s"$kstructName*"
case SharedPointers => s"std::shared_ptr<$kstructName>"
case UniqueAndRawPointers => s"std::unique_ptr<$kstructName>"
}
case CalcKaitaiStructType(_) => config.pointers match {
case RawPointers => s"$kstructName*"
case SharedPointers => s"std::shared_ptr<$kstructName>"
case UniqueAndRawPointers => s"$kstructName*"
}
case CalcKaitaiStructType(_) => s"$kstructName*"

case st: SwitchType =>
kaitaiType2NativeType(config, combineSwitchType(st), absolute)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package io.kaitai.struct.translators

import java.nio.charset.Charset

import io.kaitai.struct.CppRuntimeConfig.{RawPointers, SharedPointers, UniqueAndRawPointers}
import io.kaitai.struct.CppRuntimeConfig.{RawPointers, UniqueAndRawPointers}
import io.kaitai.struct.datatype.DataType
import io.kaitai.struct.datatype.DataType._
import io.kaitai.struct.exprlang.Ast
Expand Down Expand Up @@ -161,18 +161,6 @@ class CppTranslator(provider: TypeProvider, importListSrc: CppImportList, import
override def doIfExp(condition: expr, ifTrue: expr, ifFalse: expr): String =
s"((${translate(condition)}) ? (${translate(ifTrue)}) : (${translate(ifFalse)}))"
override def doCast(value: Ast.expr, typeName: DataType): String =
config.cppConfig.pointers match {
case RawPointers | UniqueAndRawPointers =>
cppStaticCast(value, typeName)
case SharedPointers =>
typeName match {
case ut: UserType =>
s"std::static_pointer_cast<${CppCompiler.types2class(ut.classSpec.get.name)}>(${translate(value)})"
case _ => cppStaticCast(value, typeName)
}
}

def cppStaticCast(value: Ast.expr, typeName: DataType): String =
s"static_cast<${CppCompiler.kaitaiType2NativeType(config.cppConfig, typeName)}>(${translate(value)})"

// Predefined methods of various types
Expand Down

0 comments on commit 751f059

Please sign in to comment.