-
Notifications
You must be signed in to change notification settings - Fork 11
/
StructBodyParser.java
158 lines (145 loc) · 5.69 KB
/
StructBodyParser.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*******************************************************************************
* Copyright (c) 2015, 2023 Ericsson
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.struct;
import static org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.TsdlUtils.childTypeError;
import java.util.Collections;
import java.util.List;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.tracecompass.ctf.core.event.metadata.DeclarationScope;
import org.eclipse.tracecompass.ctf.core.event.types.StructDeclaration;
import org.eclipse.tracecompass.ctf.core.trace.CTFTrace;
import org.eclipse.tracecompass.ctf.parser.CTFParser;
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.AbstractScopedCommonTreeParser;
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.MetadataStrings;
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.ParseException;
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.TypeAliasParser;
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.TypedefParser;
import org.eclipse.tracecompass.internal.ctf.core.event.types.ICTFMetadataNode;
/**
* A struct body can have any of the following elements, even though some of
* them do not make sense:
* <ul>
* <li>align</li>
* <li>callsite</li>
* <li>const</li>
* <li>char</li>
* <li>clock</li>
* <li>double</li>
* <li>enum</li>
* <li>env</li>
* <li>event</li>
* <li>floating_point</li>
* <li>float</li>
* <li>integer</li>
* <li>int</li>
* <li>long</li>
* <li>short</li>
* <li>signed</li>
* <li>stream</li>
* <li>string</li>
* <li>struct</li>
* <li>trace</li>
* <li>typealias</li>
* <li>typedef</li>
* <li>unsigned</li>
* <li>variant</li>
* <li>void</li>
* <li>_Bool</li>
* <li>_Complex</li>
* <li>_Imaginary</li>
* </ul>
*
* @author Matthew Khouzam
*
*/
public final class StructBodyParser extends AbstractScopedCommonTreeParser {
/**
* The parameter object
*
* @author Matthew Khouzam
*
*/
@NonNullByDefault
public static final class Param implements ICommonTreeParserParameter {
private final DeclarationScope fDeclarationScope;
private final @Nullable String fName;
private final StructDeclaration fStructDeclaration;
private final CTFTrace fTrace;
/**
* Constructor
*
* @param structDeclaration
* struct declaration to populate
* @param trace
* the trace
* @param name
* the struct name
* @param scope
* the current scope
*/
public Param(StructDeclaration structDeclaration, CTFTrace trace, @Nullable String name, DeclarationScope scope) {
fStructDeclaration = structDeclaration;
fTrace = trace;
fDeclarationScope = scope;
fName = name;
}
}
/**
* The instance
*/
public static final StructBodyParser INSTANCE = new StructBodyParser();
private StructBodyParser() {
}
/**
* Parse the body of a struct, so anything between the '{' '}'
*
* @param structBody
* the struct body AST node
* @param param
* the struct body parameters
* @return {@link StructDeclaration} that is now populated
* @throws ParseException
* The AST is malformed
*/
@Override
public StructDeclaration parse(ICTFMetadataNode structBody, ICommonTreeParserParameter param) throws ParseException {
if (!(param instanceof Param)) {
throw new IllegalArgumentException("Param must be a " + Param.class.getCanonicalName()); //$NON-NLS-1$
}
String structName = ((Param) param).fName;
final DeclarationScope scope = new DeclarationScope(((Param) param).fDeclarationScope, structName == null ? MetadataStrings.STRUCT : structName);
StructDeclaration structDeclaration = ((Param) param).fStructDeclaration;
List<ICTFMetadataNode> structDeclarations = structBody.getChildren();
if (structDeclarations == null) {
structDeclarations = Collections.emptyList();
}
/*
* If structDeclaration is null, structBody has no children and the
* struct body is empty.
*/
CTFTrace trace = ((Param) param).fTrace;
for (ICTFMetadataNode declarationNode : structDeclarations) {
String type = declarationNode.getType();
if (CTFParser.tokenNames[CTFParser.TYPEALIAS].equals(type)) {
TypeAliasParser.INSTANCE.parse(declarationNode, new TypeAliasParser.Param(trace, scope));
} else if (CTFParser.tokenNames[CTFParser.TYPEDEF].equals(type)) {
TypedefParser.INSTANCE.parse(declarationNode, new TypedefParser.Param(trace, scope));
StructDeclarationParser.INSTANCE.parse(declarationNode, new StructDeclarationParser.Param(structDeclaration, trace, scope));
} else if (CTFParser.tokenNames[CTFParser.SV_DECLARATION].equals(type)) {
StructDeclarationParser.INSTANCE.parse(declarationNode, new StructDeclarationParser.Param(structDeclaration, trace, scope));
} else {
throw childTypeError(declarationNode);
}
}
return structDeclaration;
}
}