1 /*******************************************************************************
2 * Copyright (c) 2011, 2014 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
;
21 * A CTF structure declaration.
23 * A structure is similar to a C structure, it is a compound data type that
24 * contains other datatypes in fields. they are stored in an hashmap and indexed
25 * by names which are strings.
28 * @author Matthew Khouzam
29 * @author Simon Marchi
31 public class StructDeclaration
implements IDeclaration
{
33 // ------------------------------------------------------------------------
35 // ------------------------------------------------------------------------
37 private final Map
<String
, IDeclaration
> fields
= new HashMap
<>();
38 private final List
<String
> fieldsList
= new LinkedList
<>();
39 private long maxAlign
;
41 // ------------------------------------------------------------------------
43 // ------------------------------------------------------------------------
46 * The struct declaration, add fields later
49 * the minimum alignment of the struct. (if a struct is 8bit
50 * aligned and has a 32 bit aligned field, the struct becomes 32
53 public StructDeclaration(long align
) {
54 this.maxAlign
= Math
.max(align
, 1);
57 // ------------------------------------------------------------------------
58 // Getters/Setters/Predicates
59 // ------------------------------------------------------------------------
62 * Get current alignment
63 * @return the alignment of the struct and all its fields
65 public long getMaxAlign() {
70 * Query if the struct has a given field
71 * @param name the name of the field, scopeless please
72 * @return does the field exist?
74 public boolean hasField(String name
) {
75 return this.fields
.containsKey(name
);
79 * get the fields of the struct in a map. Faster access time than a list.
80 * @return a HashMap of the fields (key is the name)
83 public Map
<String
, IDeclaration
> getFields() {
88 * Gets the field list. Very important since the map of fields does not retain the order of the fields.
89 * @return the field list.
91 public List
<String
> getFieldsList() {
92 return this.fieldsList
;
96 public long getAlignment() {
100 // ------------------------------------------------------------------------
102 // ------------------------------------------------------------------------
105 public StructDefinition
createDefinition(IDefinitionScope definitionScope
,
107 return new StructDefinition(this, definitionScope
, fieldName
);
111 * Add a field to the struct
112 * @param name the name of the field, scopeless
113 * @param declaration the declaration of the field
115 public void addField(String name
, IDeclaration declaration
) {
116 this.fields
.put(name
, declaration
);
117 this.fieldsList
.add(name
);
118 maxAlign
= Math
.max(maxAlign
, declaration
.getAlignment());
122 public String
toString() {
123 /* Only used for debugging */
124 return "[declaration] struct[" + Integer
.toHexString(hashCode()) + ']'; //$NON-NLS-1$
128 public int hashCode() {
129 final int prime
= 31;
131 result
= (prime
* result
) + fieldsList
.hashCode();
132 result
= (prime
* result
) + (int) (maxAlign ^
(maxAlign
>>> 32));
137 public boolean equals(Object obj
) {
144 if (!(obj
instanceof StructDeclaration
)) {
147 StructDeclaration other
= (StructDeclaration
) obj
;
148 if (!fieldsList
.equals(other
.fieldsList
)) {
151 if (maxAlign
!= other
.maxAlign
) {