ctf: Fix some Sonar warnings
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / internal / ctf / core / event / metadata / DeclarationScope.java
CommitLineData
866e5b51
FC
1/*******************************************************************************
2 * Copyright (c) 2011-2012 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 Design and Grammar
10 * Contributors: Simon Marchi - Initial API and implementation
11 *******************************************************************************/
12
ce2388e0 13package org.eclipse.linuxtools.internal.ctf.core.event.metadata;
866e5b51
FC
14
15import java.util.HashMap;
0594c61c 16import java.util.Map;
866e5b51 17
866e5b51
FC
18import org.eclipse.linuxtools.ctf.core.event.types.EnumDeclaration;
19import org.eclipse.linuxtools.ctf.core.event.types.IDeclaration;
20import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration;
21import org.eclipse.linuxtools.ctf.core.event.types.VariantDeclaration;
ce2388e0 22import org.eclipse.linuxtools.internal.ctf.core.event.metadata.exceptions.ParseException;
866e5b51
FC
23
24/**
25 * <b><u>DeclarationScope</u></b>
26 * <p>
27 * A DeclarationScope keeps track of the various CTF declarations for a given
28 * scope.
29 */
30public class DeclarationScope {
31
32 // ------------------------------------------------------------------------
33 // Attributes
34 // ------------------------------------------------------------------------
35
36 private DeclarationScope parentScope = null;
37
0594c61c
AM
38 private final Map<String, StructDeclaration> structs = new HashMap<String, StructDeclaration>();
39 private final Map<String, EnumDeclaration> enums = new HashMap<String, EnumDeclaration>();
40 private final Map<String, VariantDeclaration> variants = new HashMap<String, VariantDeclaration>();
41 private final Map<String, IDeclaration> types = new HashMap<String, IDeclaration>();
866e5b51
FC
42
43 // ------------------------------------------------------------------------
44 // Constructors
45 // ------------------------------------------------------------------------
46
47 /**
48 * Creates a declaration scope with no parent.
49 */
50 public DeclarationScope() {
51 }
52
53 /**
54 * Creates a declaration scope with the specified parent.
55 *
56 * @param parentScope
57 * The parent of the newly created scope.
58 */
59 public DeclarationScope(DeclarationScope parentScope) {
60 this.parentScope = parentScope;
61 }
62
63 // ------------------------------------------------------------------------
64 // Getters/Setters/Predicates
65 // ------------------------------------------------------------------------
66
67 /**
68 * Returns the parent of the current scope.
69 *
70 * @return The parent scope.
71 */
72 public DeclarationScope getParentScope() {
73 return parentScope;
74 }
75
76 // ------------------------------------------------------------------------
77 // Registration operations
78 // ------------------------------------------------------------------------
79
80 /**
81 * Registers a type declaration.
82 *
83 * @param name
84 * The name of the type.
85 * @param declaration
86 * The type declaration.
87 * @throws ParseException
88 * if a type with the same name has already been defined.
89 */
90 public void registerType(String name, IDeclaration declaration)
91 throws ParseException {
92 /* Check if the type has been defined in the current scope */
93 if (types.containsKey(name)) {
0594c61c 94 throw new ParseException(Messages.TypeAlreadyDefined + ':' + name);
866e5b51
FC
95 }
96
97 /* Add it to the register. */
98 types.put(name, declaration);
99 }
100
101 /**
102 * Registers a struct declaration.
103 *
104 * @param name
105 * The name of the struct.
106 * @param declaration
107 * The declaration of the struct.
108 * @throws ParseException
109 * if a struct with the same name has already been registered.
110 */
111 public void registerStruct(String name, StructDeclaration declaration)
112 throws ParseException {
113 /* Check if the struct has been defined in the current scope. */
114 if (structs.containsKey(name)) {
0594c61c 115 throw new ParseException(Messages.StructAlreadyDefined + ':' + name);
866e5b51
FC
116 }
117
118 /* Add it to the register. */
119 structs.put(name, declaration);
120
121 /* It also defined a new type, so add it to the type declarations. */
0594c61c
AM
122 String structPrefix = "struct "; //$NON-NLS-1$
123 registerType(structPrefix + name, declaration);
866e5b51
FC
124 }
125
126 /**
127 * Registers an enum declaration.
128 *
129 * @param name
130 * The name of the enum.
131 * @param declaration
132 * The declaration of the enum.
133 * @throws ParseException
134 * if an enum with the same name has already been registered.
135 */
136 public void registerEnum(String name, EnumDeclaration declaration)
137 throws ParseException {
138 /* Check if the enum has been defined in the current scope. */
139 if (lookupEnum(name) != null) {
0594c61c 140 throw new ParseException(Messages.EnumAlreadyDefined + ':' + name);
866e5b51
FC
141 }
142
143 /* Add it to the register. */
144 enums.put(name, declaration);
145
146 /* It also defined a new type, so add it to the type declarations. */
0594c61c
AM
147 String enumPrefix = "enum "; //$NON-NLS-1$
148 registerType(enumPrefix + name, declaration);
866e5b51
FC
149 }
150
151 /**
152 * Registers a variant declaration.
153 *
154 * @param name
155 * The name of the variant.
156 * @param declaration
157 * The declaration of the variant.
158 * @throws ParseException
159 * if a variant with the same name has already been registered.
160 */
161 public void registerVariant(String name, VariantDeclaration declaration)
162 throws ParseException {
163 /* Check if the variant has been defined in the current scope. */
164 if (lookupVariant(name) != null) {
0594c61c 165 throw new ParseException(Messages.VariantAlreadyDefined + ':' + name);
866e5b51
FC
166 }
167
168 /* Add it to the register. */
169 variants.put(name, declaration);
170
171 /* It also defined a new type, so add it to the type declarations. */
0594c61c
AM
172 String variantPrefix = "variant "; //$NON-NLS-1$
173 registerType(variantPrefix + name, declaration);
866e5b51
FC
174 }
175
176 // ------------------------------------------------------------------------
177 // Lookup operations
178 // ------------------------------------------------------------------------
179
180 /**
181 * Looks up a type declaration in the current scope.
182 *
183 * @param name
184 * The name of the type to search for.
185 * @return The type declaration, or null if no type with that name has been
186 * defined.
187 */
188 public IDeclaration lookupType(String name) {
189 return types.get(name);
190 }
191
192 /**
193 * Looks up a type declaration in the current scope and recursively in the
194 * parent scopes.
195 *
196 * @param name
197 * The name of the type to search for.
198 * @return The type declaration, or null if no type with that name has been
199 * defined.
200 */
201 public IDeclaration rlookupType(String name) {
202 IDeclaration declaration = lookupType(name);
203 if (declaration != null) {
204 return declaration;
205 } else if (parentScope != null) {
206 return parentScope.rlookupType(name);
207 } else {
208 return null;
209 }
210 }
211
212 /**
213 * Looks up a struct declaration.
214 *
215 * @param name
216 * The name of the struct to search for.
217 * @return The struct declaration, or null if no struct with that name has
218 * been defined.
219 */
220 public StructDeclaration lookupStruct(String name) {
221 return structs.get(name);
222 }
223
224 /**
225 * Looks up a struct declaration in the current scope and recursively in the
226 * parent scopes.
227 *
228 * @param name
229 * The name of the struct to search for.
230 * @return The struct declaration, or null if no struct with that name has
231 * been defined.
232 */
233 public StructDeclaration rlookupStruct(String name) {
234 StructDeclaration declaration = lookupStruct(name);
235 if (declaration != null) {
236 return declaration;
237 } else if (parentScope != null) {
238 return parentScope.rlookupStruct(name);
239 } else {
240 return null;
241 }
242 }
243
244 /**
245 * Looks up a enum declaration.
246 *
247 * @param name
248 * The name of the enum to search for.
249 * @return The enum declaration, or null if no enum with that name has been
250 * defined.
251 */
252 public EnumDeclaration lookupEnum(String name) {
253 return enums.get(name);
254 }
255
256 /**
257 * Looks up an enum declaration in the current scope and recursively in the
258 * parent scopes.
259 *
260 * @param name
261 * The name of the enum to search for.
262 * @return The enum declaration, or null if no enum with that name has been
263 * defined.
264 */
265 public EnumDeclaration rlookupEnum(String name) {
266 EnumDeclaration declaration = lookupEnum(name);
267 if (declaration != null) {
268 return declaration;
269 } else if (parentScope != null) {
270 return parentScope.rlookupEnum(name);
271 } else {
272 return null;
273 }
274 }
275
276 /**
277 * Looks up a variant declaration.
278 *
279 * @param name
280 * The name of the variant to search for.
281 * @return The variant declaration, or null if no variant with that name has
282 * been defined.
283 */
284 public VariantDeclaration lookupVariant(String name) {
285 return variants.get(name);
286 }
287
288 /**
289 * Looks up a variant declaration in the current scope and recursively in
290 * the parent scopes.
291 *
292 * @param name
293 * The name of the variant to search for.
294 * @return The variant declaration, or null if no variant with that name has
295 * been defined.
296 */
297 public VariantDeclaration rlookupVariant(String name) {
298 VariantDeclaration declaration = lookupVariant(name);
299 if (declaration != null) {
300 return declaration;
301 } else if (parentScope != null) {
302 return parentScope.rlookupVariant(name);
303 } else {
304 return null;
305 }
306 }
307
72dbc4ac
MK
308
309 /**
310 * Get all the type names of this scope.
311 *
312 * @return The type names
313 */
314 public String[] getTypeNames() {
315 String[] keys = new String[types.keySet().size()];
316 return types.keySet().toArray(keys);
317 }
318
319 /**
320 * Replace a type with a new one.
321 *
322 * @param name
323 * The name of the type
324 * @param newType
325 * The type
326 * @throws ParseException
327 * If the type does not exist.
328 */
329 public void replaceType(String name, IDeclaration newType) throws ParseException{
330 if (types.containsKey(name)) {
331 types.put(name, newType);
332 } else {
0594c61c 333 throw new ParseException(Messages.TraceDoesNotContainType + ':' + name);
72dbc4ac
MK
334 }
335 }
336
866e5b51 337}
This page took 0.043133 seconds and 5 git commands to generate.