1 /*******************************************************************************
2 * Copyright (c) 2011-2012 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 * <b><u>StructDeclaration</u></b>
22 public class StructDeclaration
implements IDeclaration
{
24 // ------------------------------------------------------------------------
26 // ------------------------------------------------------------------------
28 private final HashMap
<String
, IDeclaration
> fields
= new HashMap
<String
, IDeclaration
>();
29 private final List
<String
> fieldsList
= new LinkedList
<String
>();
30 private long maxAlign
;
32 // ------------------------------------------------------------------------
34 // ------------------------------------------------------------------------
37 * The struct declaration, add fields later
40 * the minimum alignment of the struct. (if a struct is 8bit
41 * aligned and has a 32 bit aligned field, the struct becomes 32
44 public StructDeclaration(long align
) {
45 this.maxAlign
= Math
.max(align
, 1);
48 // ------------------------------------------------------------------------
49 // Getters/Setters/Predicates
50 // ------------------------------------------------------------------------
53 * Get current alignment
54 * @return the alignment of the struct and all its fields
56 public long getMaxAlign() {
61 * Query if the struct has a given field
62 * @param name the name of the field, scopeless please
63 * @return does the field exist?
65 public boolean hasField(String name
) {
66 return this.fields
.containsKey(name
);
70 * get the fields of the struct in a map. Faster access time than a list.
71 * @return a HashMap of the fields (key is the name)
73 public HashMap
<String
, IDeclaration
> getFields() {
78 * Gets the field list. Very important since the map of fields does not retain the order of the fields.
79 * @return the field list.
81 public List
<String
> getFieldsList() {
82 return this.fieldsList
;
86 public long getAlignment() {
90 // ------------------------------------------------------------------------
92 // ------------------------------------------------------------------------
95 public StructDefinition
createDefinition(IDefinitionScope definitionScope
,
97 return new StructDefinition(this, definitionScope
, fieldName
);
101 * Add a field to the struct
102 * @param name the name of the field, scopeless
103 * @param declaration the declaration of the field
105 public void addField(String name
, IDeclaration declaration
) {
106 this.fields
.put(name
, declaration
);
107 this.fieldsList
.add(name
);
108 maxAlign
= Math
.max(maxAlign
, declaration
.getAlignment());
115 public String
toString() {
116 /* Only used for debugging */
117 return "[declaration] struct[" + Integer
.toHexString(hashCode()) + ']'; //$NON-NLS-1$
123 * @see java.lang.Object#hashCode()
126 public int hashCode() {
127 final int prime
= 31;
129 result
= (prime
* result
)
130 + ((fieldsList
== null) ?
0 : fieldsList
.hashCode());
131 result
= (prime
* result
) + (int) (maxAlign ^
(maxAlign
>>> 32));
138 * @see java.lang.Object#equals(java.lang.Object)
141 public boolean equals(Object obj
) {
148 if (!(obj
instanceof StructDeclaration
)) {
151 StructDeclaration other
= (StructDeclaration
) obj
;
152 if (fieldsList
== null) {
153 if (other
.fieldsList
!= null) {
156 } else if (!fieldsList
.equals(other
.fieldsList
)) {
159 if (maxAlign
!= other
.maxAlign
) {