ctf: more restrictive parameters accepted as sequence lengths
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / internal / ctf / core / event / metadata / DeclarationScope.java
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
13 package org.eclipse.linuxtools.internal.ctf.core.event.metadata;
14
15 import java.util.HashMap;
16 import java.util.Map;
17
18 import org.eclipse.linuxtools.ctf.core.event.types.EnumDeclaration;
19 import org.eclipse.linuxtools.ctf.core.event.types.IDeclaration;
20 import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration;
21 import org.eclipse.linuxtools.ctf.core.event.types.VariantDeclaration;
22 import org.eclipse.linuxtools.internal.ctf.core.event.metadata.exceptions.ParseException;
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 *
30 * TODO: The notion of "symbols" and the notion of "scope" are misused in this
31 * parser, which leads to inefficient tree management. It should be cleaned up.
32 *
33 * @author Matthew Khouzam
34 * @author Simon Marchi
35 *
36 */
37 class DeclarationScope {
38
39 // ------------------------------------------------------------------------
40 // Attributes
41 // ------------------------------------------------------------------------
42
43 private DeclarationScope parentScope = null;
44
45 private final Map<String, StructDeclaration> structs = new HashMap<String, StructDeclaration>();
46 private final Map<String, EnumDeclaration> enums = new HashMap<String, EnumDeclaration>();
47 private final Map<String, VariantDeclaration> variants = new HashMap<String, VariantDeclaration>();
48 private final Map<String, IDeclaration> types = new HashMap<String, IDeclaration>();
49 private final Map<String, IDeclaration> identifiers = new HashMap<String, IDeclaration>();
50
51 // ------------------------------------------------------------------------
52 // Constructors
53 // ------------------------------------------------------------------------
54
55 /**
56 * Creates a declaration scope with no parent.
57 */
58 public DeclarationScope() {
59 }
60
61 /**
62 * Creates a declaration scope with the specified parent.
63 *
64 * @param parentScope
65 * The parent of the newly created scope.
66 */
67 public DeclarationScope(DeclarationScope parentScope) {
68 this.parentScope = parentScope;
69 }
70
71 // ------------------------------------------------------------------------
72 // Getters/Setters/Predicates
73 // ------------------------------------------------------------------------
74
75 /**
76 * Returns the parent of the current scope.
77 *
78 * @return The parent scope.
79 */
80 public DeclarationScope getParentScope() {
81 return parentScope;
82 }
83
84 // ------------------------------------------------------------------------
85 // Registration operations
86 // ------------------------------------------------------------------------
87
88 /**
89 * Registers a type declaration.
90 *
91 * @param name
92 * The name of the type.
93 * @param declaration
94 * The type declaration.
95 * @throws ParseException
96 * if a type with the same name has already been defined.
97 */
98 public void registerType(String name, IDeclaration declaration)
99 throws ParseException {
100 /* Check if the type has been defined in the current scope */
101 if (types.containsKey(name)) {
102 throw new ParseException("Type has already been defined:" + name); //$NON-NLS-1$
103 }
104
105 /* Add it to the register. */
106 types.put(name, declaration);
107 }
108
109 /**
110 * Registers an identifier declaration.
111 *
112 * @param name
113 * name of the identifier
114 * @param declaration
115 * the identfier's declaration
116 * @throws ParseException
117 * if an identifier with the same name has already been defined.
118 */
119 public void registerIdentifier(String name, IDeclaration declaration) throws ParseException {
120 /* Check if the type has been defined in the current scope */
121 if (identifiers.containsKey(name)) {
122 throw new ParseException("Identifier has already been defined:" + name); //$NON-NLS-1$
123 }
124
125 /* Add it to the register. */
126 identifiers.put(name, declaration);
127 }
128
129 /**
130 * Registers a struct declaration.
131 *
132 * @param name
133 * The name of the struct.
134 * @param declaration
135 * The declaration of the struct.
136 * @throws ParseException
137 * if a struct with the same name has already been registered.
138 */
139 public void registerStruct(String name, StructDeclaration declaration)
140 throws ParseException {
141 /* Check if the struct has been defined in the current scope. */
142 if (structs.containsKey(name)) {
143 throw new ParseException("Struct has already been defined:" + name); //$NON-NLS-1$
144 }
145
146 /* Add it to the register. */
147 structs.put(name, declaration);
148
149 /* It also defined a new type, so add it to the type declarations. */
150 String structPrefix = "struct "; //$NON-NLS-1$
151 registerType(structPrefix + name, declaration);
152 }
153
154 /**
155 * Registers an enum declaration.
156 *
157 * @param name
158 * The name of the enum.
159 * @param declaration
160 * The declaration of the enum.
161 * @throws ParseException
162 * if an enum with the same name has already been registered.
163 */
164 public void registerEnum(String name, EnumDeclaration declaration)
165 throws ParseException {
166 /* Check if the enum has been defined in the current scope. */
167 if (lookupEnum(name) != null) {
168 throw new ParseException("Enum has already been defined:" + name); //$NON-NLS-1$
169 }
170
171 /* Add it to the register. */
172 enums.put(name, declaration);
173
174 /* It also defined a new type, so add it to the type declarations. */
175 String enumPrefix = "enum "; //$NON-NLS-1$
176 registerType(enumPrefix + name, declaration);
177 }
178
179 /**
180 * Registers a variant declaration.
181 *
182 * @param name
183 * The name of the variant.
184 * @param declaration
185 * The declaration of the variant.
186 * @throws ParseException
187 * if a variant with the same name has already been registered.
188 */
189 public void registerVariant(String name, VariantDeclaration declaration)
190 throws ParseException {
191 /* Check if the variant has been defined in the current scope. */
192 if (lookupVariant(name) != null) {
193 throw new ParseException("Variant has already been defined:" + name); //$NON-NLS-1$
194 }
195
196 /* Add it to the register. */
197 variants.put(name, declaration);
198
199 /* It also defined a new type, so add it to the type declarations. */
200 String variantPrefix = "variant "; //$NON-NLS-1$
201 registerType(variantPrefix + name, declaration);
202 }
203
204 // ------------------------------------------------------------------------
205 // Lookup operations
206 // ------------------------------------------------------------------------
207
208 /**
209 * Looks up a type declaration in the current scope.
210 *
211 * @param name
212 * The name of the type to search for.
213 * @return The type declaration, or null if no type with that name has been
214 * defined.
215 */
216 public IDeclaration lookupType(String name) {
217 return types.get(name);
218 }
219
220 /**
221 * Looks up a type declaration in the current scope and recursively in the
222 * parent scopes.
223 *
224 * @param name
225 * The name of the type to search for.
226 * @return The type declaration, or null if no type with that name has been
227 * defined.
228 */
229 public IDeclaration lookupTypeRecursive(String name) {
230 IDeclaration declaration = lookupType(name);
231 if (declaration != null) {
232 return declaration;
233 } else if (parentScope != null) {
234 return parentScope.lookupTypeRecursive(name);
235 } else {
236 return null;
237 }
238 }
239
240 /**
241 * Looks up a struct declaration.
242 *
243 * @param name
244 * The name of the struct to search for.
245 * @return The struct declaration, or null if no struct with that name has
246 * been defined.
247 */
248 public StructDeclaration lookupStruct(String name) {
249 return structs.get(name);
250 }
251
252 /**
253 * Looks up a struct declaration in the current scope and recursively in the
254 * parent scopes.
255 *
256 * @param name
257 * The name of the struct to search for.
258 * @return The struct declaration, or null if no struct with that name has
259 * been defined.
260 */
261 public StructDeclaration lookupStructRecursive(String name) {
262 StructDeclaration declaration = lookupStruct(name);
263 if (declaration != null) {
264 return declaration;
265 } else if (parentScope != null) {
266 return parentScope.lookupStructRecursive(name);
267 } else {
268 return null;
269 }
270 }
271
272 /**
273 * Looks up a enum declaration.
274 *
275 * @param name
276 * The name of the enum to search for.
277 * @return The enum declaration, or null if no enum with that name has been
278 * defined.
279 */
280 public EnumDeclaration lookupEnum(String name) {
281 return enums.get(name);
282 }
283
284 /**
285 * Looks up an enum declaration in the current scope and recursively in the
286 * parent scopes.
287 *
288 * @param name
289 * The name of the enum to search for.
290 * @return The enum declaration, or null if no enum with that name has been
291 * defined.
292 */
293 public EnumDeclaration lookupEnumRecursive(String name) {
294 EnumDeclaration declaration = lookupEnum(name);
295 if (declaration != null) {
296 return declaration;
297 } else if (parentScope != null) {
298 return parentScope.lookupEnumRecursive(name);
299 } else {
300 return null;
301 }
302 }
303
304 /**
305 * Looks up a variant declaration.
306 *
307 * @param name
308 * The name of the variant to search for.
309 * @return The variant declaration, or null if no variant with that name has
310 * been defined.
311 */
312 public VariantDeclaration lookupVariant(String name) {
313 return variants.get(name);
314 }
315
316 /**
317 * Looks up a variant declaration in the current scope and recursively in
318 * the parent scopes.
319 *
320 * @param name
321 * The name of the variant to search for.
322 * @return The variant declaration, or null if no variant with that name has
323 * been defined.
324 */
325 public VariantDeclaration lookupVariantRecursive(String name) {
326 VariantDeclaration declaration = lookupVariant(name);
327 if (declaration != null) {
328 return declaration;
329 } else if (parentScope != null) {
330 return parentScope.lookupVariantRecursive(name);
331 } else {
332 return null;
333 }
334 }
335
336 /**
337 * Looks through the list of identifiers of a scope to find if it exists.
338 *
339 * @param identifier
340 * the name of the identifier to search for. In the case of int
341 * x; it would be "x"
342 * @return the declaration of the type associated to that identifier
343 */
344 public IDeclaration lookupIdentifier(String identifier) {
345 return identifiers.get(identifier);
346 }
347
348 /**
349 * Recursively looks through the list of identifiers of a scope to find if
350 * it exists.
351 *
352 * @param identifier
353 * the name of the identifier to search for. In the case of int
354 * x; it would be "x"
355 * @return the declaration of the type associated to that identifier
356 */
357 public IDeclaration lookupIdentifierRecursive(String identifier) {
358 IDeclaration declaration = lookupIdentifier(identifier);
359 if (declaration != null) {
360 return declaration;
361 } else if (parentScope != null) {
362 return parentScope.lookupIdentifierRecursive(identifier);
363 }
364 return null;
365 }
366
367 /**
368 * Get all the type names of this scope.
369 *
370 * @return The type names
371 */
372 public String[] getTypeNames() {
373 String[] keys = new String[types.keySet().size()];
374 return types.keySet().toArray(keys);
375 }
376
377 /**
378 * Replace a type with a new one.
379 *
380 * @param name
381 * The name of the type
382 * @param newType
383 * The type
384 * @throws ParseException
385 * If the type does not exist.
386 */
387 public void replaceType(String name, IDeclaration newType) throws ParseException {
388 if (types.containsKey(name)) {
389 types.put(name, newType);
390 } else {
391 throw new ParseException("Trace does not contain type:" + name); //$NON-NLS-1$
392 }
393 }
394
395 }
This page took 0.043116 seconds and 6 git commands to generate.