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