1 /*******************************************************************************
2 * Copyright (c) 2011, 2013 Ericsson, Ecole Polytechnique de Montreal and others
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
9 * Contributors: Matthew Khouzam - Initial API and implementation
10 * Contributors: Simon Marchi - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.ctf
.core
.event
.types
;
15 import java
.util
.HashMap
;
16 import java
.util
.LinkedList
;
17 import java
.util
.List
;
20 * A CTF structure declaration.
22 * A structure is similar to a C structure, it is a compound data type that
23 * contains other datatypes in fields. they are stored in an hashmap and indexed
24 * by names which are strings.
27 * @author Matthew Khouzam
28 * @author Simon Marchi
30 public class StructDeclaration
implements IDeclaration
{
32 // ------------------------------------------------------------------------
34 // ------------------------------------------------------------------------
36 private final HashMap
<String
, IDeclaration
> fields
= new HashMap
<String
, IDeclaration
>();
37 private final List
<String
> fieldsList
= new LinkedList
<String
>();
38 private long maxAlign
;
40 // ------------------------------------------------------------------------
42 // ------------------------------------------------------------------------
45 * The struct declaration, add fields later
48 * the minimum alignment of the struct. (if a struct is 8bit
49 * aligned and has a 32 bit aligned field, the struct becomes 32
52 public StructDeclaration(long align
) {
53 this.maxAlign
= Math
.max(align
, 1);
56 // ------------------------------------------------------------------------
57 // Getters/Setters/Predicates
58 // ------------------------------------------------------------------------
61 * Get current alignment
62 * @return the alignment of the struct and all its fields
64 public long getMaxAlign() {
69 * Query if the struct has a given field
70 * @param name the name of the field, scopeless please
71 * @return does the field exist?
73 public boolean hasField(String name
) {
74 return this.fields
.containsKey(name
);
78 * get the fields of the struct in a map. Faster access time than a list.
79 * @return a HashMap of the fields (key is the name)
81 public HashMap
<String
, IDeclaration
> getFields() {
86 * Gets the field list. Very important since the map of fields does not retain the order of the fields.
87 * @return the field list.
89 public List
<String
> getFieldsList() {
90 return this.fieldsList
;
94 public long getAlignment() {
98 // ------------------------------------------------------------------------
100 // ------------------------------------------------------------------------
103 public StructDefinition
createDefinition(IDefinitionScope definitionScope
,
105 return new StructDefinition(this, definitionScope
, fieldName
);
109 * Add a field to the struct
110 * @param name the name of the field, scopeless
111 * @param declaration the declaration of the field
113 public void addField(String name
, IDeclaration declaration
) {
114 this.fields
.put(name
, declaration
);
115 this.fieldsList
.add(name
);
116 maxAlign
= Math
.max(maxAlign
, declaration
.getAlignment());
123 public String
toString() {
124 /* Only used for debugging */
125 return "[declaration] struct[" + Integer
.toHexString(hashCode()) + ']'; //$NON-NLS-1$
131 * @see java.lang.Object#hashCode()
134 public int hashCode() {
135 final int prime
= 31;
137 result
= (prime
* result
) + fieldsList
.hashCode();
138 result
= (prime
* result
) + (int) (maxAlign ^
(maxAlign
>>> 32));
145 * @see java.lang.Object#equals(java.lang.Object)
148 public boolean equals(Object obj
) {
155 if (!(obj
instanceof StructDeclaration
)) {
158 StructDeclaration other
= (StructDeclaration
) obj
;
159 if (!fieldsList
.equals(other
.fieldsList
)) {
162 if (maxAlign
!= other
.maxAlign
) {