new parsing: fix names of arrays of structs (#2849)
* fix offsets inside struct arrays * style * fix index writing for arrays of structs
This commit is contained in:
parent
8ef5e0d1c9
commit
947e364d75
|
@ -14,17 +14,17 @@ public class ArrayIterateScalarLayout extends ArrayLayout {
|
|||
this.prototypeLayout.setOffset(offset + this.prototypeLayout.getSize() * idx);
|
||||
|
||||
// Put a 1-based index on the end of the name to distinguish in TS
|
||||
prefixer.setSuffix(Integer.toString(idx + 1));
|
||||
this.prototypeLayout.writeTunerstudioLayout(ps, prefixer);
|
||||
prefixer.resetSuffix();
|
||||
prefixer.setIndex(idx);
|
||||
this.prototypeLayout.writeTunerstudioLayout(ps, prefixer, 0);
|
||||
prefixer.clearIndex();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer) {
|
||||
protected void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer, int offsetAdd) {
|
||||
// Time to iterate: emit one scalar per array element, with the name modified accordingly
|
||||
|
||||
for (int i = 0; i < length; i++) {
|
||||
emitOne(ps, prefixer, this.offset, i);
|
||||
for (int i = 0; i < this.length; i++) {
|
||||
emitOne(ps, prefixer, this.offset + offsetAdd, i);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -11,20 +11,20 @@ public class ArrayIterateStructLayout extends ArrayLayout {
|
|||
|
||||
private void emitOne(PrintStream ps, StructNamePrefixer prefixer, int offset, int idx) {
|
||||
// Set element's position within the array
|
||||
this.prototypeLayout.setOffset(offset + this.prototypeLayout.getSize() * idx);
|
||||
int offsetAdd = offset + this.prototypeLayout.getSize() * idx;
|
||||
|
||||
// Put a 1-based index on the end of the name to distinguish in TS
|
||||
prefixer.setSuffix(Integer.toString(idx + 1));
|
||||
this.prototypeLayout.writeTunerstudioLayout(ps, prefixer);
|
||||
prefixer.resetSuffix();
|
||||
prefixer.setIndex(idx);
|
||||
this.prototypeLayout.writeTunerstudioLayout(ps, prefixer, offsetAdd);
|
||||
prefixer.clearIndex();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer) {
|
||||
protected void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer, int offsetAdd) {
|
||||
// Time to iterate: emit one scalar per array element, with the name modified accordingly
|
||||
|
||||
for (int i = 0; i < this.length; i++) {
|
||||
emitOne(ps, prefixer, this.offset, i);
|
||||
emitOne(ps, prefixer, this.offset + offsetAdd, i);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -55,8 +55,8 @@ public class ArrayLayout extends Layout {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer) {
|
||||
this.prototypeLayout.writeTunerstudioLayout(ps, prefixer, this.length);
|
||||
protected void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer, int offsetAdd) {
|
||||
this.prototypeLayout.writeTunerstudioLayout(ps, prefixer, offsetAdd, this.length);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -46,12 +46,14 @@ public class BitGroupLayout extends Layout {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer) {
|
||||
protected void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer, int offsetAdd) {
|
||||
int actualOffset = this.offset + offsetAdd;
|
||||
|
||||
for (int i = 0; i < bits.size(); i++) {
|
||||
BitLayout bit = bits.get(i);
|
||||
ps.print(prefixer.get(bit.name));
|
||||
ps.print(" = bits, U32, ");
|
||||
ps.print(this.offset);
|
||||
ps.print(actualOffset);
|
||||
ps.print(", [");
|
||||
ps.print(i + ":" + i);
|
||||
|
||||
|
|
|
@ -35,12 +35,12 @@ public class EnumLayout extends Layout {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer) {
|
||||
protected void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer, int offsetAdd) {
|
||||
ps.print(prefixer.get(this.name));
|
||||
ps.print(" = bits, ");
|
||||
ps.print(this.type.tsType);
|
||||
ps.print(", ");
|
||||
ps.print(this.offset);
|
||||
ps.print(this.offset + offsetAdd);
|
||||
ps.print(", ");
|
||||
|
||||
ps.print("[0:");
|
||||
|
|
|
@ -26,12 +26,12 @@ public abstract class Layout {
|
|||
}
|
||||
|
||||
public final void writeTunerstudioLayout(PrintStream ps) {
|
||||
writeTunerstudioLayout(ps, new StructNamePrefixer());
|
||||
writeTunerstudioLayout(ps, new StructNamePrefixer(), 0);
|
||||
}
|
||||
|
||||
public void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer) {}
|
||||
protected void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer, int offsetAdd) {}
|
||||
|
||||
public void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer, int arrayLength) {
|
||||
protected void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer, int offsetAdd, int arrayLength) {
|
||||
throw new IllegalStateException("This type can't be in an array!");
|
||||
}
|
||||
|
||||
|
|
|
@ -28,12 +28,12 @@ public class ScalarLayout extends Layout {
|
|||
return "Scalar " + type.cType + " " + super.toString();
|
||||
}
|
||||
|
||||
private void printBeforeArrayLength(PrintStream ps, StructNamePrefixer prefixer, String fieldType) {
|
||||
private void printBeforeArrayLength(PrintStream ps, StructNamePrefixer prefixer, String fieldType, int offsetAdd) {
|
||||
ps.print(prefixer.get(this.name));
|
||||
ps.print(" = " + fieldType + ", ");
|
||||
ps.print(this.type.tsType);
|
||||
ps.print(", ");
|
||||
ps.print(this.offset);
|
||||
ps.print(this.offset + offsetAdd);
|
||||
ps.print(", ");
|
||||
}
|
||||
|
||||
|
@ -44,17 +44,17 @@ public class ScalarLayout extends Layout {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer, int arrayLength) {
|
||||
protected void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer, int offsetAdd, int arrayLength) {
|
||||
if (arrayLength == 0) {
|
||||
// Skip zero length arrays, they may be used for dynamic padding but TS doesn't like them
|
||||
return;
|
||||
} else if (arrayLength == 1) {
|
||||
// For 1-length arrays, emit as a plain scalar instead
|
||||
writeTunerstudioLayout(ps, prefixer);
|
||||
writeTunerstudioLayout(ps, prefixer, offsetAdd);
|
||||
return;
|
||||
}
|
||||
|
||||
printBeforeArrayLength(ps, prefixer, "array");
|
||||
printBeforeArrayLength(ps, prefixer, "array", offsetAdd);
|
||||
|
||||
ps.print("[");
|
||||
ps.print(arrayLength);
|
||||
|
@ -64,8 +64,8 @@ public class ScalarLayout extends Layout {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer) {
|
||||
printBeforeArrayLength(ps, prefixer, "scalar");
|
||||
protected void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer, int offsetAdd) {
|
||||
printBeforeArrayLength(ps, prefixer, "scalar", offsetAdd);
|
||||
printAfterArrayLength(ps);
|
||||
}
|
||||
|
||||
|
|
|
@ -31,10 +31,10 @@ public class StringLayout extends Layout {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer) {
|
||||
protected void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer, int offsetAdd) {
|
||||
ps.print(prefixer.get(this.name));
|
||||
ps.print(" = string, ASCII, ");
|
||||
ps.print(this.offset);
|
||||
ps.print(this.offset + offsetAdd);
|
||||
ps.print(", ");
|
||||
ps.print(size);
|
||||
|
||||
|
|
|
@ -156,13 +156,13 @@ public class StructLayout extends Layout {
|
|||
|
||||
|
||||
@Override
|
||||
public void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer) {
|
||||
protected void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer, int offsetAdd) {
|
||||
if (!this.noPrefix) {
|
||||
prefixer.push(this.name);
|
||||
}
|
||||
|
||||
// print all children in sequence
|
||||
this.children.forEach(c -> c.writeTunerstudioLayout(ps, prefixer));
|
||||
this.children.forEach(c -> c.writeTunerstudioLayout(ps, prefixer, offsetAdd));
|
||||
|
||||
if (!this.noPrefix) {
|
||||
prefixer.pop();
|
||||
|
|
|
@ -5,26 +5,44 @@ import java.util.stream.Collectors;
|
|||
|
||||
public class StructNamePrefixer {
|
||||
private Stack<String> stack = new Stack<>();
|
||||
private int idx = -1;
|
||||
|
||||
public void pop() {
|
||||
stack.pop();
|
||||
}
|
||||
|
||||
public void push(String name) {
|
||||
if (this.idx != -1) {
|
||||
// If we push with an index set, it means we're inside an array of structs.
|
||||
// In that case, we embed the index (of the struct in the array) within the array's name
|
||||
name = name + this.idx;
|
||||
this.idx = -1;
|
||||
}
|
||||
|
||||
stack.push(name + "_");
|
||||
}
|
||||
|
||||
private String suffix = new String();
|
||||
|
||||
public void setSuffix(String suffix) {
|
||||
this.suffix = suffix;
|
||||
void setIndex(int idx) {
|
||||
if (idx >= 0) {
|
||||
this.idx = idx + 1;
|
||||
} else {
|
||||
throw new RuntimeException("Invalid StructNamePrefixer index: " + idx);
|
||||
}
|
||||
}
|
||||
|
||||
public void resetSuffix() {
|
||||
this.suffix = new String();
|
||||
void clearIndex() {
|
||||
this.idx = -1;
|
||||
}
|
||||
|
||||
String get(String name) {
|
||||
return stack.stream().collect(Collectors.joining()) + name + suffix;
|
||||
// stack has no prefixes, just return the plain name (no join necessary)
|
||||
name = stack.stream().collect(Collectors.joining()) + name;
|
||||
|
||||
// Append the array index if necessary (for iterated arrays)
|
||||
if (this.idx != -1) {
|
||||
return name + idx;
|
||||
} else {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -56,9 +56,9 @@ public class UnionLayout extends Layout {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer) {
|
||||
protected void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer, int offsetAdd) {
|
||||
// Simply write out all children - no container necessary as fields can overlap in TS
|
||||
this.children.forEach(c -> c.writeTunerstudioLayout(ps, prefixer));
|
||||
this.children.forEach(c -> c.writeTunerstudioLayout(ps, prefixer, offsetAdd));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -34,8 +34,8 @@ public class UnusedLayout extends Layout {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer) {
|
||||
ps.println("; unused " + this.size + " bytes at offset " + this.offset);
|
||||
protected void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer, int offsetAdd) {
|
||||
ps.println("; unused " + this.size + " bytes at offset " + (this.offset + offsetAdd));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
Loading…
Reference in New Issue