Commit | Line | Data |
---|---|---|
866e5b51 | 1 | /******************************************************************************* |
4bd7f2db | 2 | * Copyright (c) 2011, 2013 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 | ||
13 | package org.eclipse.linuxtools.ctf.core.event.types; | |
14 | ||
15 | import java.util.HashMap; | |
16 | import java.util.LinkedList; | |
17 | import java.util.List; | |
0594c61c | 18 | import java.util.Map; |
866e5b51 FC |
19 | |
20 | /** | |
d37aaa7f | 21 | * A CTF structure declaration. |
77fdc5df | 22 | * |
d37aaa7f FC |
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. | |
26 | * | |
27 | * @version 1.0 | |
28 | * @author Matthew Khouzam | |
29 | * @author Simon Marchi | |
866e5b51 FC |
30 | */ |
31 | public class StructDeclaration implements IDeclaration { | |
32 | ||
33 | // ------------------------------------------------------------------------ | |
34 | // Attributes | |
35 | // ------------------------------------------------------------------------ | |
36 | ||
3de23137 AM |
37 | private final Map<String, IDeclaration> fields = new HashMap<>(); |
38 | private final List<String> fieldsList = new LinkedList<>(); | |
2b7f6f09 | 39 | private long maxAlign; |
866e5b51 FC |
40 | |
41 | // ------------------------------------------------------------------------ | |
42 | // Constructors | |
43 | // ------------------------------------------------------------------------ | |
44 | ||
9ac2eb62 MK |
45 | /** |
46 | * The struct declaration, add fields later | |
47 | * | |
48 | * @param align | |
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 | |
51 | * bit aligned. | |
52 | */ | |
2b7f6f09 MK |
53 | public StructDeclaration(long align) { |
54 | this.maxAlign = Math.max(align, 1); | |
866e5b51 FC |
55 | } |
56 | ||
57 | // ------------------------------------------------------------------------ | |
58 | // Getters/Setters/Predicates | |
59 | // ------------------------------------------------------------------------ | |
60 | ||
9ac2eb62 MK |
61 | /** |
62 | * Get current alignment | |
63 | * @return the alignment of the struct and all its fields | |
64 | */ | |
2b7f6f09 MK |
65 | public long getMaxAlign() { |
66 | return maxAlign; | |
866e5b51 FC |
67 | } |
68 | ||
9ac2eb62 MK |
69 | /** |
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? | |
73 | */ | |
866e5b51 FC |
74 | public boolean hasField(String name) { |
75 | return this.fields.containsKey(name); | |
76 | } | |
77 | ||
9ac2eb62 MK |
78 | /** |
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) | |
0594c61c | 81 | * @since 2.0 |
9ac2eb62 | 82 | */ |
0594c61c | 83 | public Map<String, IDeclaration> getFields() { |
866e5b51 FC |
84 | return this.fields; |
85 | } | |
86 | ||
9ac2eb62 MK |
87 | /** |
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. | |
90 | */ | |
866e5b51 FC |
91 | public List<String> getFieldsList() { |
92 | return this.fieldsList; | |
93 | } | |
94 | ||
fd74e6c1 MK |
95 | @Override |
96 | public long getAlignment() { | |
2b7f6f09 | 97 | return this.maxAlign; |
fd74e6c1 | 98 | } |
9ac2eb62 | 99 | |
866e5b51 FC |
100 | // ------------------------------------------------------------------------ |
101 | // Operations | |
102 | // ------------------------------------------------------------------------ | |
103 | ||
104 | @Override | |
105 | public StructDefinition createDefinition(IDefinitionScope definitionScope, | |
106 | String fieldName) { | |
107 | return new StructDefinition(this, definitionScope, fieldName); | |
108 | } | |
109 | ||
9ac2eb62 MK |
110 | /** |
111 | * Add a field to the struct | |
112 | * @param name the name of the field, scopeless | |
113 | * @param declaration the declaration of the field | |
114 | */ | |
866e5b51 | 115 | public void addField(String name, IDeclaration declaration) { |
866e5b51 FC |
116 | this.fields.put(name, declaration); |
117 | this.fieldsList.add(name); | |
2b7f6f09 | 118 | maxAlign = Math.max(maxAlign, declaration.getAlignment()); |
866e5b51 FC |
119 | } |
120 | ||
121 | @Override | |
122 | public String toString() { | |
123 | /* Only used for debugging */ | |
124 | return "[declaration] struct[" + Integer.toHexString(hashCode()) + ']'; //$NON-NLS-1$ | |
125 | } | |
126 | ||
4dd0eaed MK |
127 | @Override |
128 | public int hashCode() { | |
129 | final int prime = 31; | |
130 | int result = 1; | |
77fdc5df | 131 | result = (prime * result) + fieldsList.hashCode(); |
4dd0eaed MK |
132 | result = (prime * result) + (int) (maxAlign ^ (maxAlign >>> 32)); |
133 | return result; | |
134 | } | |
135 | ||
4dd0eaed MK |
136 | @Override |
137 | public boolean equals(Object obj) { | |
138 | if (this == obj) { | |
139 | return true; | |
140 | } | |
141 | if (obj == null) { | |
142 | return false; | |
143 | } | |
144 | if (!(obj instanceof StructDeclaration)) { | |
145 | return false; | |
146 | } | |
147 | StructDeclaration other = (StructDeclaration) obj; | |
77fdc5df | 148 | if (!fieldsList.equals(other.fieldsList)) { |
4dd0eaed MK |
149 | return false; |
150 | } | |
151 | if (maxAlign != other.maxAlign) { | |
152 | return false; | |
153 | } | |
154 | return true; | |
155 | } | |
156 | ||
866e5b51 | 157 | } |