Commit | Line | Data |
---|---|---|
866e5b51 | 1 | /******************************************************************************* |
60ae41e1 | 2 | * Copyright (c) 2011, 2014 Ericsson, Ecole Polytechnique de Montreal and others |
866e5b51 FC |
3 | * |
4 | * All rights reserved. This program and the accompanying materials are made | |
5 | * available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: Matthew Khouzam - Initial API and implementation | |
10 | * Contributors: Simon Marchi - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
f357bcd4 | 13 | package org.eclipse.tracecompass.ctf.core.event.types; |
866e5b51 | 14 | |
5db5a3a4 AM |
15 | import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull; |
16 | ||
e00e6663 | 17 | import java.util.ArrayList; |
2db699c2 | 18 | import java.util.Iterator; |
a4fa4e36 | 19 | import java.util.LinkedHashMap; |
e00e6663 | 20 | import java.util.List; |
0594c61c | 21 | import java.util.Map; |
e00e6663 | 22 | import java.util.Map.Entry; |
94c255ef | 23 | import java.util.regex.Pattern; |
866e5b51 | 24 | |
a4fa4e36 | 25 | import org.eclipse.jdt.annotation.NonNull; |
2db699c2 | 26 | import org.eclipse.jdt.annotation.Nullable; |
680f9173 | 27 | import org.eclipse.tracecompass.ctf.core.CTFException; |
f357bcd4 AM |
28 | import org.eclipse.tracecompass.ctf.core.event.io.BitBuffer; |
29 | import org.eclipse.tracecompass.ctf.core.event.scope.IDefinitionScope; | |
fbe6fa6f | 30 | import org.eclipse.tracecompass.ctf.core.event.scope.ILexicalScope; |
a4fa4e36 | 31 | |
866e5b51 | 32 | /** |
d37aaa7f | 33 | * A CTF structure declaration. |
77fdc5df | 34 | * |
d37aaa7f FC |
35 | * A structure is similar to a C structure, it is a compound data type that |
36 | * contains other datatypes in fields. they are stored in an hashmap and indexed | |
37 | * by names which are strings. | |
38 | * | |
39 | * @version 1.0 | |
40 | * @author Matthew Khouzam | |
41 | * @author Simon Marchi | |
866e5b51 | 42 | */ |
a4fa4e36 | 43 | public class StructDeclaration extends Declaration { |
866e5b51 FC |
44 | |
45 | // ------------------------------------------------------------------------ | |
46 | // Attributes | |
47 | // ------------------------------------------------------------------------ | |
48 | ||
a4fa4e36 | 49 | /** linked list of field names. So fieldName->fieldValue */ |
367e2932 | 50 | private final @NonNull Map<@NonNull String, IDeclaration> fFieldMap = new LinkedHashMap<>(); |
a4fa4e36 MK |
51 | |
52 | /** maximum bit alignment */ | |
53 | private long fMaxAlign; | |
866e5b51 FC |
54 | |
55 | // ------------------------------------------------------------------------ | |
56 | // Constructors | |
57 | // ------------------------------------------------------------------------ | |
58 | ||
9ac2eb62 MK |
59 | /** |
60 | * The struct declaration, add fields later | |
61 | * | |
62 | * @param align | |
63 | * the minimum alignment of the struct. (if a struct is 8bit | |
64 | * aligned and has a 32 bit aligned field, the struct becomes 32 | |
65 | * bit aligned. | |
66 | */ | |
2b7f6f09 | 67 | public StructDeclaration(long align) { |
a4fa4e36 | 68 | fMaxAlign = Math.max(align, 1); |
a4fa4e36 MK |
69 | } |
70 | ||
866e5b51 FC |
71 | // ------------------------------------------------------------------------ |
72 | // Getters/Setters/Predicates | |
73 | // ------------------------------------------------------------------------ | |
74 | ||
9ac2eb62 MK |
75 | /** |
76 | * Get current alignment | |
a4fa4e36 | 77 | * |
9ac2eb62 MK |
78 | * @return the alignment of the struct and all its fields |
79 | */ | |
2b7f6f09 | 80 | public long getMaxAlign() { |
a4fa4e36 | 81 | return fMaxAlign; |
866e5b51 FC |
82 | } |
83 | ||
9ac2eb62 MK |
84 | /** |
85 | * Query if the struct has a given field | |
a4fa4e36 MK |
86 | * |
87 | * @param name | |
88 | * the name of the field, scopeless please | |
9ac2eb62 MK |
89 | * @return does the field exist? |
90 | */ | |
866e5b51 | 91 | public boolean hasField(String name) { |
a4fa4e36 | 92 | return fFieldMap.containsKey(name); |
866e5b51 FC |
93 | } |
94 | ||
2db699c2 AM |
95 | /** |
96 | * Get the field declaration corresponding to a field name. | |
97 | * | |
98 | * @param fieldName | |
99 | * The field name | |
100 | * @return The declaration of the field, or null if there is no such field. | |
2db699c2 AM |
101 | */ |
102 | @Nullable | |
103 | public IDeclaration getField(String fieldName) { | |
104 | return fFieldMap.get(fieldName); | |
105 | } | |
106 | ||
9ac2eb62 | 107 | /** |
a4fa4e36 MK |
108 | * Gets the field list. Very important since the map of fields does not |
109 | * retain the order of the fields. | |
110 | * | |
9ac2eb62 MK |
111 | * @return the field list. |
112 | */ | |
367e2932 | 113 | public @NonNull Iterable<@NonNull String> getFieldsList() { |
a4fa4e36 | 114 | return fFieldMap.keySet(); |
866e5b51 FC |
115 | } |
116 | ||
fd74e6c1 MK |
117 | @Override |
118 | public long getAlignment() { | |
a4fa4e36 MK |
119 | return this.fMaxAlign; |
120 | } | |
121 | ||
a4fa4e36 MK |
122 | @Override |
123 | public int getMaximumSize() { | |
124 | int maxSize = 0; | |
2db699c2 AM |
125 | for (IDeclaration field : fFieldMap.values()) { |
126 | maxSize += field.getMaximumSize(); | |
a4fa4e36 MK |
127 | } |
128 | return Math.min(maxSize, Integer.MAX_VALUE); | |
fd74e6c1 | 129 | } |
9ac2eb62 | 130 | |
866e5b51 FC |
131 | // ------------------------------------------------------------------------ |
132 | // Operations | |
133 | // ------------------------------------------------------------------------ | |
134 | ||
135 | @Override | |
136 | public StructDefinition createDefinition(IDefinitionScope definitionScope, | |
680f9173 | 137 | String fieldName, BitBuffer input) throws CTFException { |
a4fa4e36 | 138 | alignRead(input); |
2db699c2 | 139 | final Definition[] myFields = new Definition[fFieldMap.size()]; |
94c255ef MK |
140 | StructDefinition structDefinition = null; |
141 | if (definitionScope == null) { | |
142 | InternalDef localDefinitionScope = new InternalDef(null, null); | |
143 | structDefinition = new StructDefinition(this, localDefinitionScope, fieldName, myFields); | |
144 | localDefinitionScope.setDefinition(structDefinition); | |
145 | } else { | |
146 | structDefinition = new StructDefinition(this, definitionScope, fieldName, myFields); | |
147 | } | |
cc575f45 | 148 | fillStruct(input, myFields, structDefinition); |
a4fa4e36 | 149 | return structDefinition; |
866e5b51 FC |
150 | } |
151 | ||
70f60307 | 152 | /** |
6c7592e1 MK |
153 | * Create a definition from this declaration. This is a faster constructor |
154 | * as it has a lexical scope and this does not need to look it up. | |
70f60307 MK |
155 | * |
156 | * @param definitionScope | |
6c7592e1 MK |
157 | * the definition scope, the parent where the definition will be |
158 | * placed | |
70f60307 | 159 | * @param fieldScope |
6c7592e1 | 160 | * the scope of the definition |
70f60307 | 161 | * @param input |
6c7592e1 MK |
162 | * a bitbuffer to read from |
163 | * @return a reference to the definition | |
680f9173 | 164 | * @throws CTFException |
6c7592e1 | 165 | * error in reading |
fbe6fa6f | 166 | * @since 1.0 |
70f60307 MK |
167 | */ |
168 | public StructDefinition createDefinition(IDefinitionScope definitionScope, | |
680f9173 | 169 | ILexicalScope fieldScope, @NonNull BitBuffer input) throws CTFException { |
70f60307 MK |
170 | alignRead(input); |
171 | final Definition[] myFields = new Definition[fFieldMap.size()]; | |
5db5a3a4 | 172 | |
94c255ef | 173 | StructDefinition structDefinition = new StructDefinition(this, definitionScope, |
0e4f957e | 174 | fieldScope, fieldScope.getName(), fFieldMap.keySet(), myFields); |
cc575f45 | 175 | fillStruct(input, myFields, structDefinition); |
70f60307 MK |
176 | return structDefinition; |
177 | } | |
178 | ||
9ac2eb62 MK |
179 | /** |
180 | * Add a field to the struct | |
a4fa4e36 MK |
181 | * |
182 | * @param name | |
183 | * the name of the field, scopeless | |
184 | * @param declaration | |
185 | * the declaration of the field | |
9ac2eb62 | 186 | */ |
56b62d1a | 187 | public void addField(@NonNull String name, @NonNull IDeclaration declaration) { |
a4fa4e36 MK |
188 | fFieldMap.put(name, declaration); |
189 | fMaxAlign = Math.max(fMaxAlign, declaration.getAlignment()); | |
866e5b51 FC |
190 | } |
191 | ||
680f9173 | 192 | private void fillStruct(@NonNull BitBuffer input, final Definition[] myFields, StructDefinition structDefinition) throws CTFException { |
cc575f45 MK |
193 | Iterator<Map.Entry<String, IDeclaration>> iter = fFieldMap.entrySet().iterator(); |
194 | for (int i = 0; i < fFieldMap.size(); i++) { | |
195 | Map.Entry<String, IDeclaration> entry = iter.next(); | |
5db5a3a4 AM |
196 | /* We should not have inserted null keys... */ |
197 | String key = checkNotNull(entry.getKey()); | |
198 | myFields[i] = entry.getValue().createDefinition(structDefinition, key, input); | |
cc575f45 MK |
199 | } |
200 | } | |
201 | ||
94c255ef MK |
202 | /** |
203 | * Special constructor for fields | |
204 | * | |
205 | * @param eventHeaderDef | |
206 | * the event header, used for scopes | |
207 | * @param definitionScope | |
208 | * the definition scope, in this case, the trace | |
209 | * @param fields | |
210 | * event fields | |
211 | * @param input | |
212 | * the input {@link BitBuffer} | |
213 | * @return the fields definition | |
214 | * @throws CTFException | |
215 | * something went wrong | |
0336f981 | 216 | * @since 1.1 |
94c255ef MK |
217 | */ |
218 | public StructDefinition createFieldDefinition(ICompositeDefinition eventHeaderDef, IDefinitionScope definitionScope, ILexicalScope fields, @NonNull BitBuffer input) throws CTFException { | |
219 | alignRead(input); | |
220 | final Definition[] myFields = new Definition[fFieldMap.size()]; | |
221 | IDefinitionScope merged = definitionScope; | |
222 | if (eventHeaderDef != null) { | |
223 | merged = new InternalDef(definitionScope, eventHeaderDef); | |
224 | } | |
225 | StructDefinition structDefinition = new StructDefinition(this, merged, | |
0e4f957e | 226 | fields, fields.getName(), fFieldMap.keySet(), myFields); |
94c255ef MK |
227 | if (merged instanceof InternalDef) { |
228 | InternalDef internalDef = (InternalDef) merged; | |
229 | internalDef.setDefinition(structDefinition); | |
230 | } | |
231 | fillStruct(input, myFields, structDefinition); | |
232 | return structDefinition; | |
233 | } | |
234 | ||
235 | private static final Pattern EVENT_HEADER = Pattern.compile(ILexicalScope.EVENT_HEADER.getPath().replaceAll("\\.", "\\\\.") + "\\."); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ | |
236 | ||
5b341dc8 | 237 | static class InternalDef implements IDefinitionScope { |
94c255ef MK |
238 | |
239 | private final ICompositeDefinition fEventHeaderDef; | |
240 | private final IDefinitionScope fTraceDef; | |
241 | private StructDefinition fDefinition; | |
242 | ||
243 | public InternalDef(IDefinitionScope definitionScope, ICompositeDefinition eventHeaderDef) { | |
244 | fTraceDef = definitionScope; | |
245 | fEventHeaderDef = eventHeaderDef; | |
246 | } | |
247 | ||
248 | @Override | |
249 | public ILexicalScope getScopePath() { | |
250 | return ILexicalScope.EVENT; | |
251 | } | |
252 | ||
253 | @Override | |
254 | public IDefinition lookupDefinition(String lookupPath) { | |
255 | IDefinition lookupDefinition = null; | |
256 | if (fTraceDef != null) { | |
257 | lookupDefinition = fTraceDef.lookupDefinition(lookupPath); | |
258 | } | |
1dec66b2 MK |
259 | if (lookupDefinition == null && fEventHeaderDef != null) { |
260 | String[] paths = EVENT_HEADER.split(lookupPath); | |
261 | if (paths.length > 1) { | |
262 | String[] childLookup = paths[1].split("\\."); //$NON-NLS-1$ | |
263 | return getRecursiveDef(fEventHeaderDef.getDefinition(childLookup[0]), childLookup, 1); | |
264 | } | |
265 | if (fDefinition != null) { | |
266 | return fDefinition.lookupDefinition(lookupPath); | |
94c255ef MK |
267 | } |
268 | } | |
269 | return lookupDefinition; | |
270 | } | |
271 | ||
5b341dc8 MK |
272 | public IDefinition lookupDefinitionBreakLoop(String lookupPath) { |
273 | IDefinition lookupDefinition = null; | |
274 | if (fTraceDef != null) { | |
275 | lookupDefinition = fTraceDef.lookupDefinition(lookupPath); | |
276 | } | |
277 | if (lookupDefinition == null) { | |
278 | if (fEventHeaderDef != null) { | |
279 | String[] paths = EVENT_HEADER.split(lookupPath); | |
280 | if (paths.length > 1) { | |
281 | String[] childLookup = paths[1].split("\\."); //$NON-NLS-1$ | |
282 | return getRecursiveDef(fEventHeaderDef.getDefinition(childLookup[0]), childLookup, 1); | |
283 | } | |
284 | } | |
285 | } | |
286 | return lookupDefinition; | |
287 | } | |
288 | ||
94c255ef MK |
289 | private IDefinition getRecursiveDef(Definition definition, String[] childLookup, int i) { |
290 | if (i == childLookup.length) { | |
291 | return definition; | |
292 | } | |
293 | if (definition instanceof ICompositeDefinition) { | |
294 | ICompositeDefinition compositeDefinition = (ICompositeDefinition) definition; | |
295 | return getRecursiveDef(compositeDefinition.getDefinition(childLookup[i]), childLookup, i + 1); | |
296 | } | |
297 | return null; | |
298 | } | |
299 | ||
300 | public void setDefinition(StructDefinition definition) { | |
301 | fDefinition = definition; | |
302 | } | |
303 | ||
304 | } | |
305 | ||
866e5b51 FC |
306 | @Override |
307 | public String toString() { | |
308 | /* Only used for debugging */ | |
66aa25f0 MK |
309 | StringBuilder sb = new StringBuilder(); |
310 | sb.append("[declaration] struct["); //$NON-NLS-1$ | |
311 | for (Entry<String, IDeclaration> field : fFieldMap.entrySet()) { | |
312 | sb.append(field.getKey()).append(':').append(field.getValue()); | |
313 | } | |
314 | sb.append(']'); | |
315 | return sb.toString(); | |
866e5b51 FC |
316 | } |
317 | ||
4dd0eaed MK |
318 | @Override |
319 | public int hashCode() { | |
320 | final int prime = 31; | |
321 | int result = 1; | |
e00e6663 MK |
322 | for (Entry<String, IDeclaration> field : fFieldMap.entrySet()) { |
323 | result = prime * result + field.getKey().hashCode(); | |
324 | result = prime * result + field.getValue().hashCode(); | |
325 | } | |
a4fa4e36 | 326 | result = (prime * result) + (int) (fMaxAlign ^ (fMaxAlign >>> 32)); |
4dd0eaed MK |
327 | return result; |
328 | } | |
329 | ||
4dd0eaed MK |
330 | @Override |
331 | public boolean equals(Object obj) { | |
332 | if (this == obj) { | |
333 | return true; | |
334 | } | |
335 | if (obj == null) { | |
336 | return false; | |
337 | } | |
338 | if (!(obj instanceof StructDeclaration)) { | |
339 | return false; | |
340 | } | |
341 | StructDeclaration other = (StructDeclaration) obj; | |
e00e6663 | 342 | if (fFieldMap.size() != other.fFieldMap.size()) { |
4dd0eaed MK |
343 | return false; |
344 | } | |
e00e6663 MK |
345 | |
346 | List<String> localFieldNames = new ArrayList<>(); | |
347 | localFieldNames.addAll(fFieldMap.keySet()); | |
348 | ||
349 | List<IDeclaration> localDecs = new ArrayList<>(); | |
350 | localDecs.addAll(fFieldMap.values()); | |
351 | ||
352 | List<String> otherFieldNames = new ArrayList<>(); | |
353 | otherFieldNames.addAll(other.fFieldMap.keySet()); | |
354 | ||
355 | List<IDeclaration> otherDecs = new ArrayList<>(); | |
356 | otherDecs.addAll(other.fFieldMap.values()); | |
357 | ||
94c255ef | 358 | // check fields in order |
e00e6663 MK |
359 | for (int i = 0; i < fFieldMap.size(); i++) { |
360 | if ((!localFieldNames.get(i).equals(otherFieldNames.get(i))) || | |
361 | (!otherDecs.get(i).equals(localDecs.get(i)))) { | |
362 | return false; | |
363 | } | |
364 | } | |
365 | ||
a4fa4e36 | 366 | if (fMaxAlign != other.fMaxAlign) { |
4dd0eaed MK |
367 | return false; |
368 | } | |
369 | return true; | |
370 | } | |
371 | ||
66aa25f0 MK |
372 | @Override |
373 | public boolean isBinaryEquivalent(IDeclaration obj) { | |
374 | if (this == obj) { | |
375 | return true; | |
376 | } | |
377 | if (obj == null) { | |
378 | return false; | |
379 | } | |
380 | if (!(obj instanceof StructDeclaration)) { | |
381 | return false; | |
382 | } | |
383 | StructDeclaration other = (StructDeclaration) obj; | |
384 | if (fFieldMap.size() != other.fFieldMap.size()) { | |
385 | return false; | |
386 | } | |
387 | List<IDeclaration> localDecs = new ArrayList<>(); | |
388 | localDecs.addAll(fFieldMap.values()); | |
389 | List<IDeclaration> otherDecs = new ArrayList<>(); | |
390 | otherDecs.addAll(other.fFieldMap.values()); | |
391 | for (int i = 0; i < fFieldMap.size(); i++) { | |
392 | if (!otherDecs.get(i).isBinaryEquivalent(localDecs.get(i))) { | |
393 | return false; | |
394 | } | |
395 | } | |
396 | ||
397 | if (fMaxAlign != other.fMaxAlign) { | |
398 | return false; | |
399 | } | |
400 | return true; | |
401 | } | |
402 | ||
866e5b51 | 403 | } |