tmf: Bug 473195: Invalid thread access closing editors in non-UI thread
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.ctf.core / src / org / eclipse / tracecompass / ctf / core / event / types / StructDeclaration.java
CommitLineData
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 13package org.eclipse.tracecompass.ctf.core.event.types;
866e5b51 14
5db5a3a4
AM
15import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
16
e00e6663 17import java.util.ArrayList;
2db699c2 18import java.util.Iterator;
a4fa4e36 19import java.util.LinkedHashMap;
e00e6663 20import java.util.List;
0594c61c 21import java.util.Map;
e00e6663 22import java.util.Map.Entry;
94c255ef 23import java.util.regex.Pattern;
866e5b51 24
a4fa4e36 25import org.eclipse.jdt.annotation.NonNull;
2db699c2 26import org.eclipse.jdt.annotation.Nullable;
680f9173 27import org.eclipse.tracecompass.ctf.core.CTFException;
f357bcd4
AM
28import org.eclipse.tracecompass.ctf.core.event.io.BitBuffer;
29import org.eclipse.tracecompass.ctf.core.event.scope.IDefinitionScope;
fbe6fa6f 30import 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 43public class StructDeclaration extends Declaration {
866e5b51
FC
44
45 // ------------------------------------------------------------------------
46 // Attributes
47 // ------------------------------------------------------------------------
48
a4fa4e36 49 /** linked list of field names. So fieldName->fieldValue */
2db699c2 50 private final @NonNull Map<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
9ac2eb62 95 /**
2db699c2 96 * Get the fields of the struct as a map.
a4fa4e36 97 *
2db699c2 98 * @return a Map of the fields (key is the name)
9ac2eb62 99 */
0594c61c 100 public Map<String, IDeclaration> getFields() {
a4fa4e36 101 return fFieldMap;
866e5b51
FC
102 }
103
2db699c2
AM
104 /**
105 * Get the field declaration corresponding to a field name.
106 *
107 * @param fieldName
108 * The field name
109 * @return The declaration of the field, or null if there is no such field.
2db699c2
AM
110 */
111 @Nullable
112 public IDeclaration getField(String fieldName) {
113 return fFieldMap.get(fieldName);
114 }
115
9ac2eb62 116 /**
a4fa4e36
MK
117 * Gets the field list. Very important since the map of fields does not
118 * retain the order of the fields.
119 *
9ac2eb62
MK
120 * @return the field list.
121 */
a4fa4e36
MK
122 public Iterable<String> getFieldsList() {
123 return fFieldMap.keySet();
866e5b51
FC
124 }
125
fd74e6c1
MK
126 @Override
127 public long getAlignment() {
a4fa4e36
MK
128 return this.fMaxAlign;
129 }
130
a4fa4e36
MK
131 @Override
132 public int getMaximumSize() {
133 int maxSize = 0;
2db699c2
AM
134 for (IDeclaration field : fFieldMap.values()) {
135 maxSize += field.getMaximumSize();
a4fa4e36
MK
136 }
137 return Math.min(maxSize, Integer.MAX_VALUE);
fd74e6c1 138 }
9ac2eb62 139
866e5b51
FC
140 // ------------------------------------------------------------------------
141 // Operations
142 // ------------------------------------------------------------------------
143
144 @Override
145 public StructDefinition createDefinition(IDefinitionScope definitionScope,
680f9173 146 String fieldName, BitBuffer input) throws CTFException {
a4fa4e36 147 alignRead(input);
2db699c2 148 final Definition[] myFields = new Definition[fFieldMap.size()];
94c255ef
MK
149 StructDefinition structDefinition = null;
150 if (definitionScope == null) {
151 InternalDef localDefinitionScope = new InternalDef(null, null);
152 structDefinition = new StructDefinition(this, localDefinitionScope, fieldName, myFields);
153 localDefinitionScope.setDefinition(structDefinition);
154 } else {
155 structDefinition = new StructDefinition(this, definitionScope, fieldName, myFields);
156 }
cc575f45 157 fillStruct(input, myFields, structDefinition);
a4fa4e36 158 return structDefinition;
866e5b51
FC
159 }
160
70f60307 161 /**
6c7592e1
MK
162 * Create a definition from this declaration. This is a faster constructor
163 * as it has a lexical scope and this does not need to look it up.
70f60307
MK
164 *
165 * @param definitionScope
6c7592e1
MK
166 * the definition scope, the parent where the definition will be
167 * placed
70f60307 168 * @param fieldScope
6c7592e1 169 * the scope of the definition
70f60307 170 * @param input
6c7592e1
MK
171 * a bitbuffer to read from
172 * @return a reference to the definition
680f9173 173 * @throws CTFException
6c7592e1 174 * error in reading
fbe6fa6f 175 * @since 1.0
70f60307
MK
176 */
177 public StructDefinition createDefinition(IDefinitionScope definitionScope,
680f9173 178 ILexicalScope fieldScope, @NonNull BitBuffer input) throws CTFException {
70f60307
MK
179 alignRead(input);
180 final Definition[] myFields = new Definition[fFieldMap.size()];
5db5a3a4 181
94c255ef 182 StructDefinition structDefinition = new StructDefinition(this, definitionScope,
5db5a3a4 183 fieldScope, fieldScope.getName(), checkNotNull(fFieldMap.keySet()), myFields);
cc575f45 184 fillStruct(input, myFields, structDefinition);
70f60307
MK
185 return structDefinition;
186 }
187
9ac2eb62
MK
188 /**
189 * Add a field to the struct
a4fa4e36
MK
190 *
191 * @param name
192 * the name of the field, scopeless
193 * @param declaration
194 * the declaration of the field
9ac2eb62 195 */
866e5b51 196 public void addField(String name, IDeclaration declaration) {
a4fa4e36
MK
197 fFieldMap.put(name, declaration);
198 fMaxAlign = Math.max(fMaxAlign, declaration.getAlignment());
866e5b51
FC
199 }
200
680f9173 201 private void fillStruct(@NonNull BitBuffer input, final Definition[] myFields, StructDefinition structDefinition) throws CTFException {
cc575f45
MK
202 Iterator<Map.Entry<String, IDeclaration>> iter = fFieldMap.entrySet().iterator();
203 for (int i = 0; i < fFieldMap.size(); i++) {
204 Map.Entry<String, IDeclaration> entry = iter.next();
5db5a3a4
AM
205 /* We should not have inserted null keys... */
206 String key = checkNotNull(entry.getKey());
207 myFields[i] = entry.getValue().createDefinition(structDefinition, key, input);
cc575f45
MK
208 }
209 }
210
94c255ef
MK
211 /**
212 * Special constructor for fields
213 *
214 * @param eventHeaderDef
215 * the event header, used for scopes
216 * @param definitionScope
217 * the definition scope, in this case, the trace
218 * @param fields
219 * event fields
220 * @param input
221 * the input {@link BitBuffer}
222 * @return the fields definition
223 * @throws CTFException
224 * something went wrong
225 * @since 1.1
226 */
227 public StructDefinition createFieldDefinition(ICompositeDefinition eventHeaderDef, IDefinitionScope definitionScope, ILexicalScope fields, @NonNull BitBuffer input) throws CTFException {
228 alignRead(input);
229 final Definition[] myFields = new Definition[fFieldMap.size()];
230 IDefinitionScope merged = definitionScope;
231 if (eventHeaderDef != null) {
232 merged = new InternalDef(definitionScope, eventHeaderDef);
233 }
234 StructDefinition structDefinition = new StructDefinition(this, merged,
235 fields, fields.getName(), checkNotNull(fFieldMap.keySet()), myFields);
236 if (merged instanceof InternalDef) {
237 InternalDef internalDef = (InternalDef) merged;
238 internalDef.setDefinition(structDefinition);
239 }
240 fillStruct(input, myFields, structDefinition);
241 return structDefinition;
242 }
243
244 private static final Pattern EVENT_HEADER = Pattern.compile(ILexicalScope.EVENT_HEADER.getPath().replaceAll("\\.", "\\\\.") + "\\."); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
245
246 private class InternalDef implements IDefinitionScope {
247
248 private final ICompositeDefinition fEventHeaderDef;
249 private final IDefinitionScope fTraceDef;
250 private StructDefinition fDefinition;
251
252 public InternalDef(IDefinitionScope definitionScope, ICompositeDefinition eventHeaderDef) {
253 fTraceDef = definitionScope;
254 fEventHeaderDef = eventHeaderDef;
255 }
256
257 @Override
258 public ILexicalScope getScopePath() {
259 return ILexicalScope.EVENT;
260 }
261
262 @Override
263 public IDefinition lookupDefinition(String lookupPath) {
264 IDefinition lookupDefinition = null;
265 if (fTraceDef != null) {
266 lookupDefinition = fTraceDef.lookupDefinition(lookupPath);
267 }
268 if (lookupDefinition == null) {
269 if (fEventHeaderDef != null) {
270 String[] paths = EVENT_HEADER.split(lookupPath);
271 if (paths.length > 1) {
272 String[] childLookup = paths[1].split("\\."); //$NON-NLS-1$
273 return getRecursiveDef(fEventHeaderDef.getDefinition(childLookup[0]), childLookup, 1);
274 }
275 if (fDefinition != null) {
276 return fDefinition.lookupDefinition(lookupPath);
277 }
278 }
279 }
280 return lookupDefinition;
281 }
282
283 private IDefinition getRecursiveDef(Definition definition, String[] childLookup, int i) {
284 if (i == childLookup.length) {
285 return definition;
286 }
287 if (definition instanceof ICompositeDefinition) {
288 ICompositeDefinition compositeDefinition = (ICompositeDefinition) definition;
289 return getRecursiveDef(compositeDefinition.getDefinition(childLookup[i]), childLookup, i + 1);
290 }
291 return null;
292 }
293
294 public void setDefinition(StructDefinition definition) {
295 fDefinition = definition;
296 }
297
298 }
299
866e5b51
FC
300 @Override
301 public String toString() {
302 /* Only used for debugging */
66aa25f0
MK
303 StringBuilder sb = new StringBuilder();
304 sb.append("[declaration] struct["); //$NON-NLS-1$
305 for (Entry<String, IDeclaration> field : fFieldMap.entrySet()) {
306 sb.append(field.getKey()).append(':').append(field.getValue());
307 }
308 sb.append(']');
309 return sb.toString();
866e5b51
FC
310 }
311
4dd0eaed
MK
312 @Override
313 public int hashCode() {
314 final int prime = 31;
315 int result = 1;
e00e6663
MK
316 for (Entry<String, IDeclaration> field : fFieldMap.entrySet()) {
317 result = prime * result + field.getKey().hashCode();
318 result = prime * result + field.getValue().hashCode();
319 }
a4fa4e36 320 result = (prime * result) + (int) (fMaxAlign ^ (fMaxAlign >>> 32));
4dd0eaed
MK
321 return result;
322 }
323
4dd0eaed
MK
324 @Override
325 public boolean equals(Object obj) {
326 if (this == obj) {
327 return true;
328 }
329 if (obj == null) {
330 return false;
331 }
332 if (!(obj instanceof StructDeclaration)) {
333 return false;
334 }
335 StructDeclaration other = (StructDeclaration) obj;
e00e6663 336 if (fFieldMap.size() != other.fFieldMap.size()) {
4dd0eaed
MK
337 return false;
338 }
e00e6663
MK
339
340 List<String> localFieldNames = new ArrayList<>();
341 localFieldNames.addAll(fFieldMap.keySet());
342
343 List<IDeclaration> localDecs = new ArrayList<>();
344 localDecs.addAll(fFieldMap.values());
345
346 List<String> otherFieldNames = new ArrayList<>();
347 otherFieldNames.addAll(other.fFieldMap.keySet());
348
349 List<IDeclaration> otherDecs = new ArrayList<>();
350 otherDecs.addAll(other.fFieldMap.values());
351
94c255ef 352 // check fields in order
e00e6663
MK
353 for (int i = 0; i < fFieldMap.size(); i++) {
354 if ((!localFieldNames.get(i).equals(otherFieldNames.get(i))) ||
355 (!otherDecs.get(i).equals(localDecs.get(i)))) {
356 return false;
357 }
358 }
359
a4fa4e36 360 if (fMaxAlign != other.fMaxAlign) {
4dd0eaed
MK
361 return false;
362 }
363 return true;
364 }
365
66aa25f0
MK
366 @Override
367 public boolean isBinaryEquivalent(IDeclaration obj) {
368 if (this == obj) {
369 return true;
370 }
371 if (obj == null) {
372 return false;
373 }
374 if (!(obj instanceof StructDeclaration)) {
375 return false;
376 }
377 StructDeclaration other = (StructDeclaration) obj;
378 if (fFieldMap.size() != other.fFieldMap.size()) {
379 return false;
380 }
381 List<IDeclaration> localDecs = new ArrayList<>();
382 localDecs.addAll(fFieldMap.values());
383 List<IDeclaration> otherDecs = new ArrayList<>();
384 otherDecs.addAll(other.fFieldMap.values());
385 for (int i = 0; i < fFieldMap.size(); i++) {
386 if (!otherDecs.get(i).isBinaryEquivalent(localDecs.get(i))) {
387 return false;
388 }
389 }
390
391 if (fMaxAlign != other.fMaxAlign) {
392 return false;
393 }
394 return true;
395 }
396
866e5b51 397}
This page took 0.099681 seconds and 5 git commands to generate.