Skip to content

Commit 59e2e33

Browse files
ctf: add Dynamic Length Arrays
Change-Id: Icb35ac1c931e398be30debfa45c539f8c9fcd245 Signed-off-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
1 parent d5fcb5b commit 59e2e33

1 file changed

Lines changed: 121 additions & 0 deletions

File tree

  • ctf/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/internal/ctf/core/event/metadata/tsdl/dynamicarray
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 Ericsson
3+
*
4+
* All rights reserved. This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*******************************************************************************/
11+
package org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.dynamicarray;
12+
13+
import org.eclipse.jdt.annotation.NonNullByDefault;
14+
import org.eclipse.tracecompass.ctf.core.event.metadata.DeclarationScope;
15+
import org.eclipse.tracecompass.ctf.core.event.types.IDeclaration;
16+
import org.eclipse.tracecompass.ctf.core.trace.CTFTrace;
17+
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.AbstractScopedCommonTreeParser;
18+
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.JsonStructureFieldMemberMetadataNode;
19+
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.ParseException;
20+
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.TypeAliasParser;
21+
import org.eclipse.tracecompass.internal.ctf.core.event.types.ICTFMetadataNode;
22+
import org.eclipse.tracecompass.internal.ctf.core.event.types.SequenceDeclaration;
23+
import org.eclipse.tracecompass.internal.ctf.core.utils.JsonMetadataStrings;
24+
25+
import com.google.gson.JsonElement;
26+
import com.google.gson.JsonObject;
27+
28+
/**
29+
* A dynamic-length array field class describes dynamic-length array fields.
30+
*
31+
* @author Matthew Khouzam
32+
*/
33+
public final class DynamicLengthArrayParser extends AbstractScopedCommonTreeParser {
34+
35+
/**
36+
* Instance
37+
*/
38+
public static final DynamicLengthArrayParser INSTANCE = new DynamicLengthArrayParser();
39+
40+
private DynamicLengthArrayParser() {
41+
}
42+
43+
@Override
44+
public IDeclaration parse(ICTFMetadataNode node, ICommonTreeParserParameter param) throws ParseException {
45+
if (!(node instanceof JsonStructureFieldMemberMetadataNode)) {
46+
throw new ParseException("Dynamic-length array only supported in JSON metadata"); //$NON-NLS-1$
47+
}
48+
if (!(param instanceof Param)) {
49+
throw new IllegalArgumentException("Param must be a " + Param.class.getCanonicalName()); //$NON-NLS-1$
50+
}
51+
52+
JsonStructureFieldMemberMetadataNode member = (JsonStructureFieldMemberMetadataNode) node;
53+
JsonElement fieldClassElement = member.getFieldClass();
54+
if (fieldClassElement == null || !fieldClassElement.isJsonObject()) {
55+
throw new ParseException(getClass().getName() + " fieldclass must be a json object."); //$NON-NLS-1$
56+
}
57+
JsonObject fieldClass = member.getFieldClass().getAsJsonObject();
58+
59+
JsonElement lengthFieldLocation = fieldClass.get(JsonMetadataStrings.LENGTH_FIELD_LOCATION);
60+
if (lengthFieldLocation == null) {
61+
throw new ParseException("Dynamic-length array requires length-field-location property"); //$NON-NLS-1$
62+
}
63+
64+
JsonElement elementFieldClass = fieldClass.get(JsonMetadataStrings.ELEMENT_FIELD_CLASS);
65+
if (elementFieldClass == null) {
66+
throw new ParseException("Dynamic-length array requires element-field-class property"); //$NON-NLS-1$
67+
}
68+
69+
JsonElement pathElement = lengthFieldLocation.getAsJsonObject().get(JsonMetadataStrings.PATH);
70+
String lengthName = pathElement.isJsonArray() ?
71+
pathElement.getAsJsonArray().get(pathElement.getAsJsonArray().size() - 1).getAsString() :
72+
pathElement.getAsString();
73+
CTFTrace trace = ((Param) param).getTrace();
74+
DeclarationScope scope = ((Param) param).getScope();
75+
76+
JsonStructureFieldMemberMetadataNode elementNode = new JsonStructureFieldMemberMetadataNode(node, "", "", "", elementFieldClass.getAsJsonObject()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
77+
IDeclaration elementDeclaration = TypeAliasParser.INSTANCE.parse(elementNode, new TypeAliasParser.Param(trace, scope));
78+
79+
return new SequenceDeclaration(lengthName, elementDeclaration);
80+
}
81+
82+
/**
83+
* Parameters for the dynamic-length array parser
84+
*/
85+
@NonNullByDefault
86+
public static final class Param implements ICommonTreeParserParameter {
87+
private final CTFTrace fTrace;
88+
private final DeclarationScope fScope;
89+
90+
/**
91+
* Parameter constructor
92+
*
93+
* @param trace
94+
* the trace
95+
* @param scope
96+
* the declaration scope
97+
*/
98+
public Param(CTFTrace trace, DeclarationScope scope) {
99+
fTrace = trace;
100+
fScope = scope;
101+
}
102+
103+
/**
104+
* Get the trace
105+
*
106+
* @return the trace
107+
*/
108+
public CTFTrace getTrace() {
109+
return fTrace;
110+
}
111+
112+
/**
113+
* Get the scope
114+
*
115+
* @return the scope
116+
*/
117+
public DeclarationScope getScope() {
118+
return fScope;
119+
}
120+
}
121+
}

0 commit comments

Comments
 (0)