Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure that using imported nested types doesn't affect their parent type
Related: * kaitai-io/kaitai_struct#703 * kaitai-io/kaitai_struct#963 Until now, using a nested type of an imported spec could affect the derived parent type of that nested type. For example: `main.ksy` ```ksy meta: id: main imports: - imported seq: - id: foo type: imported::nested_type ``` `imported.ksy` ```ksy meta: id: imported types: nested_type: {} ``` If you compile `main.ksy` for Java (`kaitai-struct-compiler -t java main.ksy`), `Imported.java` looks like this: ```java public class Imported extends KaitaiStruct { // ... public static class NestedType extends KaitaiStruct { public NestedType(KaitaiStream _io, Main _parent, Imported _root) { /* ... */ } // ^^^^^^^^^^^^ } // ... } ``` Notice the `Main _parent` parameter - the compiler saw that `imported::nested_type` is only used from the `main` type, so it decided that the `_parent` type of `nested_type` will be `main`. However, this means that the `_parent` crosses KSY spec boundaries and the generated `Imported.java` code will be different depending on whether you compile it as imported from `main.ksy` or standalone. Furthermore, you could even access fields of `main` in `imported::nested_type` via `_parent`, but that would mean that `imported.ksy` would only work when imported and compiled via `main.ksy`. From <kaitai-io/kaitai_struct#71 (comment)>, I suppose none of this should be possible: > It would be a huge mess if `_root` relied on this particular ksy being > imported from some other ksy, and only work in that case. I agree that `_root` and `_parent` arguably shouldn't cross spec boundaries at all, they should only be passed locally within one .ksy spec, and therefore also parent types should only be derived from local type usages. This commit only adjusts the parent type derivation, not invocations of imported nested types with `_parent` and `_root` still being passed (see also kaitai-io/kaitai_struct#963) - they will be fixed later.
- Loading branch information