ctf: Replace StructDeclaration map with an array
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.ctf.core / src / org / eclipse / tracecompass / ctf / core / event / types / StructDeclaration.java
1 /*******************************************************************************
2 * Copyright (c) 2011, 2014 Ericsson, Ecole Polytechnique de Montreal and others
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
13 package org.eclipse.tracecompass.ctf.core.event.types;
14
15 import java.util.Arrays;
16 import java.util.List;
17 import java.util.regex.Pattern;
18
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.eclipse.tracecompass.ctf.core.CTFException;
23 import org.eclipse.tracecompass.ctf.core.event.io.BitBuffer;
24 import org.eclipse.tracecompass.ctf.core.event.scope.IDefinitionScope;
25 import org.eclipse.tracecompass.ctf.core.event.scope.ILexicalScope;
26 import org.eclipse.tracecompass.internal.ctf.core.Activator;
27
28 /**
29 * A CTF structure declaration.
30 *
31 * A structure is similar to a C structure, it is a compound data type that
32 * contains other datatypes in fields.
33 * <p>
34 * <strong>Note that this implementation is not synchronized.</strong> If
35 * multiple threads access an <tt>StructDeclaration</tt> instance concurrently,
36 * and at least one of the threads modifies the list structurally, by calling
37 * {@link #addField(String, IDeclaration)} it <i>must</i> be synchronized
38 * externally. This is typically not the case though as it would mean modifying
39 * the TSDL/Metadata while reading events.
40 * <p>
41 *
42 * @version 1.0
43 * @author Matthew Khouzam
44 * @author Simon Marchi
45 */
46 public class StructDeclaration extends Declaration {
47
48 // ------------------------------------------------------------------------
49 // Attributes
50 // ------------------------------------------------------------------------
51
52 /** Field names */
53 private @NonNull String[] fFieldNames;
54 /** Field declarations */
55 private @NonNull IDeclaration[] fFields;
56
57 /** maximum bit alignment */
58 private long fMaxAlign;
59
60 // ------------------------------------------------------------------------
61 // Constructors
62 // ------------------------------------------------------------------------
63
64 /**
65 * The struct declaration, add fields later
66 *
67 * @param align
68 * the minimum alignment of the struct. (if a struct is 8bit
69 * aligned and has a 32 bit aligned field, the struct becomes 32
70 * bit aligned.
71 */
72 public StructDeclaration(long align) {
73 fMaxAlign = Math.max(align, 1);
74 fFieldNames = new @NonNull String[0];
75 fFields = new @NonNull IDeclaration[0];
76 }
77
78 // ------------------------------------------------------------------------
79 // Getters/Setters/Predicates
80 // ------------------------------------------------------------------------
81
82 /**
83 * Get current alignment
84 *
85 * @return the alignment of the struct and all its fields
86 */
87 public long getMaxAlign() {
88 return fMaxAlign;
89 }
90
91 /**
92 * Query if the struct has a given field
93 *
94 * @param name
95 * the name of the field, scopeless please
96 * @return does the field exist?
97 */
98 public boolean hasField(String name) {
99 return Arrays.asList(fFieldNames).contains(name);
100 }
101
102 /**
103 * Get the field declaration corresponding to a field name.
104 *
105 * @param fieldName
106 * The field name
107 * @return The declaration of the field, or null if there is no such field.
108 */
109 @Nullable
110 public IDeclaration getField(String fieldName) {
111 final int indexOf = Arrays.asList(fFieldNames).indexOf(fieldName);
112 if (indexOf == -1) {
113 return null;
114 }
115 return fFields[indexOf];
116 }
117
118 /**
119 * Gets the field list.
120 *
121 * @return the field list.
122 */
123 public @NonNull Iterable<@NonNull String> getFieldsList() {
124 return Arrays.asList(fFieldNames);
125 }
126
127 @Override
128 public long getAlignment() {
129 return this.fMaxAlign;
130 }
131
132 @Override
133 public int getMaximumSize() {
134 long maxSize = 0;
135 for (IDeclaration field : fFields) {
136 maxSize += field.getMaximumSize();
137 }
138 return (int) Math.min(maxSize, Integer.MAX_VALUE);
139 }
140
141 // ------------------------------------------------------------------------
142 // Operations
143 // ------------------------------------------------------------------------
144
145 @Override
146 public StructDefinition createDefinition(IDefinitionScope definitionScope,
147 String fieldName, BitBuffer input) throws CTFException {
148 alignRead(input);
149 final Definition[] myFields = new Definition[fFields.length];
150 StructDefinition structDefinition = null;
151 if (definitionScope == null) {
152 InternalDef localDefinitionScope = new InternalDef(null, null);
153 structDefinition = new StructDefinition(this, localDefinitionScope, fieldName, myFields);
154 localDefinitionScope.setDefinition(structDefinition);
155 } else {
156 structDefinition = new StructDefinition(this, definitionScope, fieldName, myFields);
157 }
158 fillStruct(input, myFields, structDefinition);
159 return structDefinition;
160 }
161
162 /**
163 * Create a definition from this declaration. This is a faster constructor
164 * as it has a lexical scope and this does not need to look it up.
165 *
166 * @param definitionScope
167 * the definition scope, the parent where the definition will be
168 * placed
169 * @param fieldScope
170 * the scope of the definition
171 * @param input
172 * a bitbuffer to read from
173 * @return a reference to the definition
174 * @throws CTFException
175 * error in reading
176 * @since 1.0
177 */
178 public StructDefinition createDefinition(IDefinitionScope definitionScope,
179 ILexicalScope fieldScope, @NonNull BitBuffer input) throws CTFException {
180 alignRead(input);
181 final Definition[] myFields = new Definition[fFields.length];
182
183 StructDefinition structDefinition = new StructDefinition(this, definitionScope,
184 fieldScope, fieldScope.getName(), Arrays.asList(fFieldNames), myFields);
185 fillStruct(input, myFields, structDefinition);
186 return structDefinition;
187 }
188
189 /**
190 * Add a field to the struct, will not add a field that is already declared
191 *
192 * @param name
193 * the name of the field, scopeless
194 * @param declaration
195 * the declaration of the field
196 */
197 public void addField(@NonNull String name, @NonNull IDeclaration declaration) {
198 if (hasField(name)) {
199 Activator.log(IStatus.WARNING, "Struct already contains a field named " + name); //$NON-NLS-1$
200 return;
201 }
202 /* extend by one */
203 final int length = fFieldNames.length;
204 @NonNull String[] names = Arrays.copyOf(fFieldNames, length + 1);
205 @NonNull IDeclaration[] fields = Arrays.copyOf(fFields, length + 1);
206 /* set the value */
207 names[length] = name;
208 fields[length] = declaration;
209 fFieldNames = names;
210 fFields = fields;
211 fMaxAlign = Math.max(fMaxAlign, declaration.getAlignment());
212 }
213
214 private void fillStruct(@NonNull BitBuffer input, final IDefinition[] myFields, StructDefinition structDefinition) throws CTFException {
215 final @NonNull String[] fieldNames = fFieldNames;
216 final @NonNull IDeclaration[] fields = fFields;
217 for (int i = 0; i < fields.length; i++) {
218 /* We should not have inserted null keys... */
219 myFields[i] = fields[i].createDefinition(structDefinition, fieldNames[i], input);
220 }
221 }
222
223 /**
224 * Special constructor for fields
225 *
226 * @param eventHeaderDef
227 * the event header, used for scopes
228 * @param definitionScope
229 * the definition scope, in this case, the trace
230 * @param fields
231 * event fields
232 * @param input
233 * the input {@link BitBuffer}
234 * @return the fields definition
235 * @throws CTFException
236 * something went wrong
237 * @since 1.1
238 */
239 public StructDefinition createFieldDefinition(ICompositeDefinition eventHeaderDef, IDefinitionScope definitionScope, ILexicalScope fields, @NonNull BitBuffer input) throws CTFException {
240 alignRead(input);
241 final Definition[] myFields = new Definition[fFields.length];
242 IDefinitionScope merged = definitionScope;
243 if (eventHeaderDef != null) {
244 merged = new InternalDef(definitionScope, eventHeaderDef);
245 }
246 StructDefinition structDefinition = new StructDefinition(this, merged,
247 fields, fields.getName(), Arrays.asList(fFieldNames), myFields);
248 if (merged instanceof InternalDef) {
249 InternalDef internalDef = (InternalDef) merged;
250 internalDef.setDefinition(structDefinition);
251 }
252 fillStruct(input, myFields, structDefinition);
253 return structDefinition;
254 }
255
256 private static final Pattern EVENT_HEADER = Pattern.compile(ILexicalScope.EVENT_HEADER.getPath().replaceAll("\\.", "\\\\.") + "\\."); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
257
258 static class InternalDef implements IDefinitionScope {
259
260 private final ICompositeDefinition fEventHeaderDef;
261 private final IDefinitionScope fTraceDef;
262 private StructDefinition fDefinition;
263
264 public InternalDef(IDefinitionScope definitionScope, ICompositeDefinition eventHeaderDef) {
265 fTraceDef = definitionScope;
266 fEventHeaderDef = eventHeaderDef;
267 }
268
269 @Override
270 public ILexicalScope getScopePath() {
271 return ILexicalScope.EVENT;
272 }
273
274 @Override
275 public IDefinition lookupDefinition(String lookupPath) {
276 IDefinition lookupDefinition = null;
277 if (fTraceDef != null) {
278 lookupDefinition = fTraceDef.lookupDefinition(lookupPath);
279 }
280 if (lookupDefinition == null && fEventHeaderDef != null) {
281 String[] paths = EVENT_HEADER.split(lookupPath);
282 if (paths.length > 1) {
283 String[] childLookup = paths[1].split("\\."); //$NON-NLS-1$
284 return getRecursiveDef(fEventHeaderDef.getDefinition(childLookup[0]), childLookup, 1);
285 }
286 if (fDefinition != null) {
287 return fDefinition.lookupDefinition(lookupPath);
288 }
289 }
290 return lookupDefinition;
291 }
292
293 public IDefinition lookupDefinitionBreakLoop(String lookupPath) {
294 IDefinition lookupDefinition = null;
295 if (fTraceDef != null) {
296 lookupDefinition = fTraceDef.lookupDefinition(lookupPath);
297 }
298 if (lookupDefinition == null) {
299 if (fEventHeaderDef != null) {
300 String[] paths = EVENT_HEADER.split(lookupPath);
301 if (paths.length > 1) {
302 String[] childLookup = paths[1].split("\\."); //$NON-NLS-1$
303 return getRecursiveDef(fEventHeaderDef.getDefinition(childLookup[0]), childLookup, 1);
304 }
305 }
306 }
307 return lookupDefinition;
308 }
309
310 private IDefinition getRecursiveDef(Definition definition, String[] childLookup, int i) {
311 if (i == childLookup.length) {
312 return definition;
313 }
314 if (definition instanceof ICompositeDefinition) {
315 ICompositeDefinition compositeDefinition = (ICompositeDefinition) definition;
316 return getRecursiveDef(compositeDefinition.getDefinition(childLookup[i]), childLookup, i + 1);
317 }
318 return null;
319 }
320
321 public void setDefinition(StructDefinition definition) {
322 fDefinition = definition;
323 }
324
325 }
326
327 @Override
328 public String toString() {
329 /* Only used for debugging */
330 StringBuilder sb = new StringBuilder();
331 sb.append("[declaration] struct["); //$NON-NLS-1$
332 for (int i = 0; i < fFields.length; i++) {
333 sb.append(fFieldNames[i]).append(':').append(fFields[i]);
334 }
335 sb.append(']');
336 return sb.toString();
337 }
338
339 @Override
340 public int hashCode() {
341 final int prime = 31;
342 int result = 1;
343 for (int i = 0; i < fFields.length; i++) {
344 result = prime * result + fFieldNames[i].hashCode();
345 result = prime * result + fFields[i].hashCode();
346 }
347 result = (prime * result) + (int) (fMaxAlign ^ (fMaxAlign >>> 32));
348 return result;
349 }
350
351 @Override
352 public boolean equals(Object obj) {
353 if (this == obj) {
354 return true;
355 }
356 if (obj == null) {
357 return false;
358 }
359 if (!(obj instanceof StructDeclaration)) {
360 return false;
361 }
362 StructDeclaration other = (StructDeclaration) obj;
363 if (fFields.length != other.fFields.length) {
364 return false;
365 }
366
367 List<String> localFieldNames = Arrays.asList(fFieldNames);
368 List<IDeclaration> localDecs = Arrays.asList(fFields);
369 List<String> otherFieldNames = Arrays.asList(other.fFieldNames);
370 List<IDeclaration> otherDecs = Arrays.asList(other.fFields);
371
372 // check fields in order
373 for (int i = 0; i < fFields.length; i++) {
374 if ((!localFieldNames.get(i).equals(otherFieldNames.get(i))) ||
375 (!otherDecs.get(i).equals(localDecs.get(i)))) {
376 return false;
377 }
378 }
379
380 if (fMaxAlign != other.fMaxAlign) {
381 return false;
382 }
383 return true;
384 }
385
386 @Override
387 public boolean isBinaryEquivalent(IDeclaration obj) {
388 if (this == obj) {
389 return true;
390 }
391 if (obj == null) {
392 return false;
393 }
394 if (!(obj instanceof StructDeclaration)) {
395 return false;
396 }
397 StructDeclaration other = (StructDeclaration) obj;
398 if (fFields.length != other.fFields.length) {
399 return false;
400 }
401 List<IDeclaration> localDecs = Arrays.asList(fFields);
402 List<IDeclaration> otherDecs = Arrays.asList(other.fFields);
403 for (int i = 0; i < fFields.length; i++) {
404 if (!otherDecs.get(i).isBinaryEquivalent(localDecs.get(i))) {
405 return false;
406 }
407 }
408
409 if (fMaxAlign != other.fMaxAlign) {
410 return false;
411 }
412 return true;
413 }
414
415 }
This page took 0.066371 seconds and 5 git commands to generate.