Commit | Line | Data |
---|---|---|
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: Francis Giraldeau - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
ce2388e0 | 13 | package org.eclipse.linuxtools.internal.ctf.core.event.metadata; |
866e5b51 FC |
14 | |
15 | import java.io.FileNotFoundException; | |
16 | import java.io.IOException; | |
17 | import java.io.UnsupportedEncodingException; | |
18 | import java.nio.ByteOrder; | |
19 | import java.util.ArrayList; | |
20 | import java.util.Collections; | |
866e5b51 FC |
21 | import java.util.LinkedList; |
22 | import java.util.List; | |
866e5b51 FC |
23 | import java.util.UUID; |
24 | ||
25 | import org.antlr.runtime.tree.CommonTree; | |
866e5b51 FC |
26 | import org.eclipse.linuxtools.ctf.core.event.CTFClock; |
27 | import org.eclipse.linuxtools.ctf.core.event.EventDeclaration; | |
866e5b51 FC |
28 | import org.eclipse.linuxtools.ctf.core.event.types.ArrayDeclaration; |
29 | import org.eclipse.linuxtools.ctf.core.event.types.Encoding; | |
30 | import org.eclipse.linuxtools.ctf.core.event.types.EnumDeclaration; | |
53047a66 | 31 | import org.eclipse.linuxtools.ctf.core.event.types.FloatDeclaration; |
866e5b51 FC |
32 | import org.eclipse.linuxtools.ctf.core.event.types.IDeclaration; |
33 | import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration; | |
34 | import org.eclipse.linuxtools.ctf.core.event.types.SequenceDeclaration; | |
35 | import org.eclipse.linuxtools.ctf.core.event.types.StringDeclaration; | |
36 | import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration; | |
37 | import org.eclipse.linuxtools.ctf.core.event.types.VariantDeclaration; | |
38 | import org.eclipse.linuxtools.ctf.core.trace.CTFTrace; | |
866e5b51 | 39 | import org.eclipse.linuxtools.ctf.parser.CTFParser; |
ce2388e0 FC |
40 | import org.eclipse.linuxtools.internal.ctf.core.event.metadata.exceptions.ParseException; |
41 | import org.eclipse.linuxtools.internal.ctf.core.trace.Stream; | |
866e5b51 FC |
42 | |
43 | /* | |
44 | * Asserts throughout this class denote the assumptions we can make because of | |
45 | * the way the grammar generates the AST. | |
46 | * | |
47 | * There is also an assert at the beginning of each function that receives a | |
48 | * tree node to make sure that the node is indeed of the type the function is | |
49 | * expecting. | |
50 | */ | |
51 | /* | |
52 | * Suppress unchecked warnings for things like List<CommonTree> children = | |
53 | * root.getChildren(), because ANTLR doesn't know what generics are. | |
54 | */ | |
55 | /** | |
56 | * <b><u>IOStructGen</u></b> | |
57 | */ | |
58 | @SuppressWarnings("unchecked") | |
59 | public class IOStructGen { | |
60 | ||
61 | // ------------------------------------------------------------------------ | |
62 | // Attributes | |
63 | // ------------------------------------------------------------------------ | |
64 | ||
65 | static private final boolean DEBUG_ = false; | |
66 | ||
67 | /** | |
68 | * The trace | |
69 | */ | |
70 | private final CTFTrace trace; | |
71 | private final CommonTree tree; | |
72 | ||
73 | /** | |
74 | * The current declaration scope. | |
75 | */ | |
76 | private DeclarationScope scope = null; | |
77 | ||
78 | // ------------------------------------------------------------------------ | |
79 | // Constructor | |
80 | // ------------------------------------------------------------------------ | |
81 | ||
82 | public IOStructGen(CommonTree tree, CTFTrace trace) { | |
83 | this.trace = trace; | |
84 | this.tree = tree; | |
85 | } | |
86 | ||
87 | public void generate() throws ParseException { | |
88 | parseRoot(tree); | |
89 | } | |
90 | ||
91 | // ------------------------------------------------------------------------ | |
92 | // Operations | |
93 | // ------------------------------------------------------------------------ | |
94 | ||
95 | /** | |
96 | * Parse the root node. | |
97 | * | |
98 | * @param root | |
99 | * A ROOT node. | |
100 | * @throws ParseException | |
101 | */ | |
102 | private void parseRoot(CommonTree root) throws ParseException { | |
103 | assert (root.getType() == CTFParser.ROOT); | |
104 | ||
105 | List<CommonTree> children = root.getChildren(); | |
106 | assert (children != null); | |
107 | java.io.FileOutputStream fos = null; | |
108 | java.io.OutputStreamWriter out = null; | |
109 | if (DEBUG_) { | |
110 | try { | |
111 | fos = new java.io.FileOutputStream("/tmp/astInfo.txt"); //$NON-NLS-1$ | |
112 | out = new java.io.OutputStreamWriter(fos, "UTF-8"); //$NON-NLS-1$ | |
113 | } catch (FileNotFoundException e) { | |
114 | e.printStackTrace(); | |
115 | return; | |
116 | } catch (UnsupportedEncodingException e) { | |
117 | e.printStackTrace(); | |
118 | return; | |
119 | } | |
120 | } | |
121 | ||
122 | CommonTree traceNode = null; | |
123 | List<CommonTree> streams = new ArrayList<CommonTree>(); | |
124 | List<CommonTree> events = new ArrayList<CommonTree>(); | |
125 | List<CommonTree> declarations = new ArrayList<CommonTree>(); | |
126 | List<CommonTree> environments = new ArrayList<CommonTree>(); | |
127 | List<CommonTree> clocks = new ArrayList<CommonTree>(); | |
128 | /* Create a new declaration scope with no parent. */ | |
129 | pushScope(); | |
130 | ||
131 | try { | |
132 | for (CommonTree child : children) { | |
133 | if (DEBUG_) { | |
134 | out.write(child.toString() | |
135 | + " -> " + child.getType() + '\n'); //$NON-NLS-1$ | |
136 | } | |
137 | switch (child.getType()) { | |
138 | case CTFParser.DECLARATION: | |
139 | declarations.add(child); | |
140 | break; | |
141 | case CTFParser.TRACE: | |
142 | if (traceNode != null) { | |
143 | throw new ParseException( | |
144 | "Only one trace block is allowed"); //$NON-NLS-1$ | |
145 | } | |
146 | traceNode = child; | |
147 | break; | |
148 | case CTFParser.STREAM: | |
149 | streams.add(child); | |
150 | break; | |
151 | case CTFParser.EVENT: | |
152 | events.add(child); | |
153 | break; | |
154 | case CTFParser.CLOCK: | |
155 | clocks.add(child); | |
156 | break; | |
157 | case CTFParser.ENV: | |
158 | environments.add(child); | |
159 | break; | |
160 | default: | |
161 | childTypeError(child); | |
162 | } | |
163 | } | |
164 | ||
165 | if (DEBUG_) { | |
166 | out.write("Environments\n"); //$NON-NLS-1$ | |
167 | } | |
168 | for (CommonTree environment : environments) { | |
169 | parseEnvironment(environment); | |
170 | } | |
171 | if (DEBUG_) { | |
172 | out.write("Clocks\n"); //$NON-NLS-1$ | |
173 | } | |
174 | for (CommonTree clock : clocks) { | |
175 | parseClock(clock); | |
176 | } | |
177 | if (DEBUG_) { | |
178 | out.write("Declarations\n"); //$NON-NLS-1$ | |
179 | } | |
180 | for (CommonTree decl : declarations) { | |
181 | if (DEBUG_) { | |
182 | out.write(decl.toString() + '\n'); | |
183 | } | |
184 | parseRootDeclaration(decl); | |
185 | } | |
186 | ||
187 | if (traceNode == null) { | |
188 | throw new ParseException("Missing trace block"); //$NON-NLS-1$ | |
189 | } | |
190 | ||
191 | parseTrace(traceNode); | |
192 | ||
193 | if (DEBUG_) { | |
194 | out.write("Streams\n"); //$NON-NLS-1$ | |
195 | } | |
196 | if (streams.size() > 0) { | |
197 | for (CommonTree stream : streams) { | |
198 | if (DEBUG_) { | |
199 | try { | |
200 | out.write(stream.toString() + '\n'); | |
201 | } catch (IOException e) { | |
202 | e.printStackTrace(); | |
203 | } | |
204 | } | |
205 | parseStream(stream); | |
206 | } | |
207 | } else { | |
208 | /* Add an empty stream that will have a null id */ | |
209 | trace.addStream(new Stream(trace)); | |
210 | } | |
211 | ||
212 | if (DEBUG_) { | |
213 | out.write("Events\n"); //$NON-NLS-1$ | |
214 | } | |
215 | for (CommonTree event : events) { | |
216 | parseEvent(event); | |
217 | CommonTree name = (CommonTree) event.getChild(0).getChild(1) | |
218 | .getChild(0).getChild(0); | |
219 | if (DEBUG_) { | |
220 | out.write("Name = " + name); //$NON-NLS-1$ | |
221 | } | |
222 | CommonTree id = (CommonTree) event.getChild(1).getChild(1) | |
223 | .getChild(0).getChild(0); | |
224 | if (DEBUG_) { | |
225 | out.write("Name = " + name + " Id = " + id + '\n'); //$NON-NLS-1$ //$NON-NLS-2$ | |
226 | } | |
227 | } | |
228 | ||
229 | if (DEBUG_) { | |
230 | out.close(); | |
231 | fos.close(); | |
232 | } | |
233 | } catch (IOException e) { | |
234 | e.printStackTrace(); | |
235 | } | |
236 | popScope(); | |
237 | } | |
238 | ||
239 | private void parseEnvironment(CommonTree environment) { | |
240 | List<CommonTree> children = environment.getChildren(); | |
241 | for (CommonTree child : children) { | |
242 | String left; | |
243 | String right; | |
244 | left = child.getChild(0).getChild(0).getChild(0).getText(); | |
245 | right = child.getChild(1).getChild(0).getChild(0).getText(); | |
246 | trace.addEnvironmentVar(left, right); | |
247 | } | |
248 | } | |
249 | ||
250 | private void parseClock(CommonTree clock) { | |
251 | List<CommonTree> children = clock.getChildren(); | |
252 | CTFClock ctfClock = new CTFClock(); | |
253 | for (CommonTree child : children) { | |
254 | final String key = child.getChild(0).getChild(0).getChild(0) | |
255 | .getText(); | |
256 | final CommonTree value = (CommonTree) child.getChild(1).getChild(0).getChild(0); | |
257 | final int type = value.getType(); | |
258 | switch (type) { | |
259 | case CTFParser.INTEGER: | |
260 | case CTFParser.DECIMAL_LITERAL: | |
284fdee8 MK |
261 | /* |
262 | * Not a pretty hack, this is to make sure that there is no number | |
263 | * overflow due to 63 bit integers. The offset should only really | |
264 | * be an issue in the year 2262. the tracer in C/ASM can write an offset in | |
265 | * an unsigned 64 bit long. In java, the last bit, being set to 1 will | |
266 | * be read as a negative number, but since it is too big a positive it will | |
267 | * throw an exception. this will happen in 2^63 ns from 1970. | |
268 | * Therefore 293 years from 1970 | |
269 | */ | |
866e5b51 FC |
270 | Long numValue; |
271 | try{ | |
272 | numValue = Long.parseLong(value.getText()); | |
273 | } | |
274 | catch( Exception e) | |
275 | { | |
276 | numValue = 1330938566783103277L; | |
277 | } | |
278 | ctfClock.addAttribute(key, numValue); | |
279 | break; | |
280 | default: | |
281 | ctfClock.addAttribute(key, value.getText()); | |
282 | } | |
283 | ||
284 | } | |
285 | String NameValue = ctfClock.getName(); | |
286 | trace.addClock(NameValue, ctfClock); | |
287 | } | |
288 | ||
289 | private void parseTrace(CommonTree traceNode) throws ParseException { | |
290 | assert (traceNode.getType() == CTFParser.TRACE); | |
291 | ||
292 | List<CommonTree> children = traceNode.getChildren(); | |
293 | if (children == null) { | |
294 | throw new ParseException("Trace block is empty"); //$NON-NLS-1$ | |
295 | } | |
296 | ||
297 | pushScope(); | |
298 | ||
299 | for (CommonTree child : children) { | |
300 | switch (child.getType()) { | |
301 | case CTFParser.TYPEALIAS: | |
302 | parseTypealias(child); | |
303 | break; | |
304 | case CTFParser.TYPEDEF: | |
305 | parseTypedef(child); | |
306 | break; | |
307 | case CTFParser.CTF_EXPRESSION_TYPE: | |
308 | case CTFParser.CTF_EXPRESSION_VAL: | |
309 | parseTraceDeclaration(child); | |
310 | break; | |
311 | default: | |
312 | childTypeError(child); | |
313 | break; | |
314 | } | |
315 | } | |
316 | ||
317 | /* | |
318 | * If trace byte order was not specified and not using packet based | |
319 | * metadata | |
320 | */ | |
321 | if (trace.getByteOrder() == null) { | |
322 | throw new ParseException("Trace byte order not set"); //$NON-NLS-1$ | |
323 | } | |
324 | ||
325 | popScope(); | |
326 | } | |
327 | ||
328 | private void parseTraceDeclaration(CommonTree traceDecl) | |
329 | throws ParseException { | |
330 | assert ((traceDecl.getType() == CTFParser.CTF_EXPRESSION_TYPE) || (traceDecl | |
331 | .getType() == CTFParser.CTF_EXPRESSION_VAL)); | |
332 | ||
333 | /* There should be a left and right */ | |
334 | assert (traceDecl.getChildCount() == 2); | |
335 | ||
336 | CommonTree leftNode = (CommonTree) traceDecl.getChild(0); | |
337 | assert (leftNode.getType() == CTFParser.CTF_LEFT); | |
338 | CommonTree rightNode = (CommonTree) traceDecl.getChild(1); | |
339 | assert (rightNode.getType() == CTFParser.CTF_RIGHT); | |
340 | ||
341 | List<CommonTree> leftStrings = leftNode.getChildren(); | |
342 | assert (leftStrings != null); | |
343 | ||
344 | if (!isUnaryString(leftStrings.get(0))) { | |
345 | throw new ParseException( | |
346 | "Left side of CTF assignment must be a string"); //$NON-NLS-1$ | |
347 | } | |
348 | ||
349 | String left = concatenateUnaryStrings(leftStrings); | |
350 | ||
351 | if (left.equals("major")) { //$NON-NLS-1$ | |
352 | if (trace.majortIsSet()) { | |
353 | throw new ParseException("major is already set"); //$NON-NLS-1$ | |
354 | } | |
355 | ||
356 | trace.setMajor(getMajorOrMinor(rightNode)); | |
357 | } else if (left.equals("minor")) { //$NON-NLS-1$ | |
358 | if (trace.minorIsSet()) { | |
359 | throw new ParseException("minor is already set"); //$NON-NLS-1$ | |
360 | } | |
361 | ||
362 | trace.setMinor(getMajorOrMinor(rightNode)); | |
363 | } else if (left.equals("uuid")) { //$NON-NLS-1$ | |
364 | UUID uuid = getUUID(rightNode); | |
365 | ||
366 | /* | |
367 | * If uuid was already set by a metadata packet, compare it to see | |
368 | * if it matches | |
369 | */ | |
370 | if (trace.UUIDIsSet()) { | |
371 | if (trace.getUUID().compareTo(uuid) != 0) { | |
372 | throw new ParseException("UUID mismatch. Packet says " //$NON-NLS-1$ | |
373 | + trace.getUUID() + " but metadata says " + uuid); //$NON-NLS-1$ | |
374 | } | |
375 | } else { | |
376 | trace.setUUID(uuid); | |
377 | } | |
378 | ||
379 | } else if (left.equals("byte_order")) { //$NON-NLS-1$ | |
380 | ByteOrder byteOrder = getByteOrder(rightNode); | |
381 | ||
382 | /* | |
383 | * If byte order was already set by a metadata packet, compare it to | |
384 | * see if it matches | |
385 | */ | |
386 | if (trace.getByteOrder() != null) { | |
387 | if (trace.getByteOrder() != byteOrder) { | |
388 | throw new ParseException( | |
389 | "Endianness mismatch. Magic number says " //$NON-NLS-1$ | |
390 | + trace.getByteOrder() | |
391 | + " but metadata says " + byteOrder); //$NON-NLS-1$ | |
392 | } | |
393 | } else { | |
394 | trace.setByteOrder(byteOrder); | |
395 | } | |
396 | } else if (left.equals("packet.header")) { //$NON-NLS-1$ | |
397 | if (trace.packetHeaderIsSet()) { | |
398 | throw new ParseException("packet.header already defined"); //$NON-NLS-1$ | |
399 | } | |
400 | ||
401 | CommonTree typeSpecifier = (CommonTree) rightNode.getChild(0); | |
402 | ||
403 | if (typeSpecifier.getType() != CTFParser.TYPE_SPECIFIER_LIST) { | |
404 | throw new ParseException( | |
405 | "packet.header expects a type specifier"); //$NON-NLS-1$ | |
406 | } | |
407 | ||
408 | IDeclaration packetHeaderDecl = parseTypeSpecifierList( | |
409 | typeSpecifier, null); | |
410 | ||
411 | if (!(packetHeaderDecl instanceof StructDeclaration)) { | |
412 | throw new ParseException("packet.header expects a struct"); //$NON-NLS-1$ | |
413 | } | |
414 | ||
415 | trace.setPacketHeader((StructDeclaration) packetHeaderDecl); | |
416 | } else { | |
417 | throw new ParseException("Unknown trace attribute : " + left); //$NON-NLS-1$ | |
418 | } | |
419 | } | |
420 | ||
421 | private void parseStream(CommonTree streamNode) throws ParseException { | |
422 | assert (streamNode.getType() == CTFParser.STREAM); | |
423 | ||
424 | Stream stream = new Stream(trace); | |
425 | ||
426 | List<CommonTree> children = streamNode.getChildren(); | |
427 | if (children == null) { | |
428 | throw new ParseException("Empty stream block"); //$NON-NLS-1$ | |
429 | } | |
430 | ||
431 | pushScope(); | |
432 | ||
433 | for (CommonTree child : children) { | |
434 | switch (child.getType()) { | |
435 | case CTFParser.TYPEALIAS: | |
436 | parseTypealias(child); | |
437 | break; | |
438 | case CTFParser.TYPEDEF: | |
439 | parseTypedef(child); | |
440 | break; | |
441 | case CTFParser.CTF_EXPRESSION_TYPE: | |
442 | case CTFParser.CTF_EXPRESSION_VAL: | |
443 | parseStreamDeclaration(child, stream); | |
444 | break; | |
445 | default: | |
446 | childTypeError(child); | |
447 | break; | |
448 | } | |
449 | } | |
450 | ||
451 | if (stream.idIsSet()) { | |
452 | if (!trace.packetHeaderIsSet() | |
453 | || !trace.getPacketHeader().hasField("stream_id")) { //$NON-NLS-1$ | |
454 | throw new ParseException( | |
455 | "Stream has an ID, but there is no stream_id field in packet header."); //$NON-NLS-1$ | |
456 | } | |
457 | } | |
458 | ||
459 | trace.addStream(stream); | |
460 | ||
461 | popScope(); | |
462 | } | |
463 | ||
464 | private void parseStreamDeclaration(CommonTree streamDecl, Stream stream) | |
465 | throws ParseException { | |
466 | assert ((streamDecl.getType() == CTFParser.CTF_EXPRESSION_TYPE) || (streamDecl | |
467 | .getType() == CTFParser.CTF_EXPRESSION_VAL)); | |
468 | ||
469 | /* There should be a left and right */ | |
470 | assert (streamDecl.getChildCount() == 2); | |
471 | ||
472 | CommonTree leftNode = (CommonTree) streamDecl.getChild(0); | |
473 | assert (leftNode.getType() == CTFParser.CTF_LEFT); | |
474 | CommonTree rightNode = (CommonTree) streamDecl.getChild(1); | |
475 | assert (rightNode.getType() == CTFParser.CTF_RIGHT); | |
476 | ||
477 | List<CommonTree> leftStrings = leftNode.getChildren(); | |
478 | assert (leftStrings != null); | |
479 | ||
480 | if (!isUnaryString(leftStrings.get(0))) { | |
481 | throw new ParseException( | |
482 | "Left side of CTF assignment must be a string"); //$NON-NLS-1$ | |
483 | } | |
484 | ||
485 | String left = concatenateUnaryStrings(leftStrings); | |
486 | ||
487 | if (left.equals("id")) { //$NON-NLS-1$ | |
488 | if (stream.idIsSet()) { | |
489 | throw new ParseException("stream id already defined"); //$NON-NLS-1$ | |
490 | } | |
491 | ||
492 | long streamID = getStreamID(rightNode); | |
493 | ||
494 | stream.setId(streamID); | |
495 | } else if (left.equals("event.header")) { //$NON-NLS-1$ | |
496 | if (stream.eventHeaderIsSet()) { | |
497 | throw new ParseException("event.header already defined"); //$NON-NLS-1$ | |
498 | } | |
499 | ||
500 | CommonTree typeSpecifier = (CommonTree) rightNode.getChild(0); | |
501 | ||
502 | if (typeSpecifier.getType() != CTFParser.TYPE_SPECIFIER_LIST) { | |
503 | throw new ParseException( | |
504 | "event.header expects a type specifier"); //$NON-NLS-1$ | |
505 | } | |
506 | ||
507 | IDeclaration eventHeaderDecl = parseTypeSpecifierList( | |
508 | typeSpecifier, null); | |
509 | ||
510 | if (!(eventHeaderDecl instanceof StructDeclaration)) { | |
511 | throw new ParseException("event.header expects a struct"); //$NON-NLS-1$ | |
512 | } | |
513 | ||
514 | stream.setEventHeader((StructDeclaration) eventHeaderDecl); | |
515 | } else if (left.equals("event.context")) { //$NON-NLS-1$ | |
516 | if (stream.eventContextIsSet()) { | |
517 | throw new ParseException("event.context already defined"); //$NON-NLS-1$ | |
518 | } | |
519 | ||
520 | CommonTree typeSpecifier = (CommonTree) rightNode.getChild(0); | |
521 | ||
522 | if (typeSpecifier.getType() != CTFParser.TYPE_SPECIFIER_LIST) { | |
523 | throw new ParseException( | |
524 | "event.context expects a type specifier"); //$NON-NLS-1$ | |
525 | } | |
526 | ||
527 | IDeclaration eventContextDecl = parseTypeSpecifierList( | |
528 | typeSpecifier, null); | |
529 | ||
530 | if (!(eventContextDecl instanceof StructDeclaration)) { | |
531 | throw new ParseException("event.context expects a struct"); //$NON-NLS-1$ | |
532 | } | |
533 | ||
534 | stream.setEventContext((StructDeclaration) eventContextDecl); | |
535 | } else if (left.equals("packet.context")) { //$NON-NLS-1$ | |
536 | if (stream.packetContextIsSet()) { | |
537 | throw new ParseException("packet.context already defined"); //$NON-NLS-1$ | |
538 | } | |
539 | ||
540 | CommonTree typeSpecifier = (CommonTree) rightNode.getChild(0); | |
541 | ||
542 | if (typeSpecifier.getType() != CTFParser.TYPE_SPECIFIER_LIST) { | |
543 | throw new ParseException( | |
544 | "packet.context expects a type specifier"); //$NON-NLS-1$ | |
545 | } | |
546 | ||
547 | IDeclaration packetContextDecl = parseTypeSpecifierList( | |
548 | typeSpecifier, null); | |
549 | ||
550 | if (!(packetContextDecl instanceof StructDeclaration)) { | |
551 | throw new ParseException("packet.context expects a struct"); //$NON-NLS-1$ | |
552 | } | |
553 | ||
554 | stream.setPacketContext((StructDeclaration) packetContextDecl); | |
555 | } else { | |
556 | throw new ParseException("Unknown stream attribute : " + left); //$NON-NLS-1$ | |
557 | } | |
558 | } | |
559 | ||
560 | private void parseEvent(CommonTree eventNode) throws ParseException { | |
561 | assert (eventNode.getType() == CTFParser.EVENT); | |
562 | ||
563 | List<CommonTree> children = eventNode.getChildren(); | |
564 | if (children == null) { | |
565 | throw new ParseException("Empty event block"); //$NON-NLS-1$ | |
566 | } | |
567 | ||
568 | EventDeclaration event = new EventDeclaration(); | |
569 | ||
570 | pushScope(); | |
571 | ||
572 | for (CommonTree child : children) { | |
573 | switch (child.getType()) { | |
574 | case CTFParser.TYPEALIAS: | |
575 | parseTypealias(child); | |
576 | break; | |
577 | case CTFParser.TYPEDEF: | |
578 | parseTypedef(child); | |
579 | break; | |
580 | case CTFParser.CTF_EXPRESSION_TYPE: | |
581 | case CTFParser.CTF_EXPRESSION_VAL: | |
582 | parseEventDeclaration(child, event); | |
583 | break; | |
584 | default: | |
585 | childTypeError(child); | |
586 | break; | |
587 | } | |
588 | } | |
589 | ||
590 | if (!event.nameIsSet()) { | |
591 | throw new ParseException("Event name not set"); //$NON-NLS-1$ | |
592 | } | |
593 | ||
594 | /* | |
595 | * If the event did not specify a stream, then the trace must be single | |
596 | * stream | |
597 | */ | |
598 | if (!event.streamIsSet()) { | |
599 | if (trace.nbStreams() > 1) { | |
600 | throw new ParseException( | |
601 | "Event without stream_id with more than one stream"); //$NON-NLS-1$ | |
602 | } | |
603 | ||
604 | /* | |
605 | * If the event did not specify a stream, the only existing stream | |
606 | * must not have an id. Note: That behavior could be changed, it | |
607 | * could be possible to just get the only existing stream, whatever | |
608 | * is its id. | |
609 | */ | |
610 | Stream stream = trace.getStream(null); | |
611 | ||
612 | if (stream != null) { | |
613 | event.setStream(stream); | |
614 | } else { | |
615 | throw new ParseException( | |
616 | "Event without stream_id, but there is no stream without id"); //$NON-NLS-1$ | |
617 | } | |
618 | } | |
619 | ||
620 | /* | |
621 | * Add the event to the stream. | |
622 | */ | |
623 | event.getStream().addEvent(event); | |
624 | ||
625 | popScope(); | |
626 | } | |
627 | ||
628 | private void parseEventDeclaration(CommonTree eventDecl, | |
629 | EventDeclaration event) throws ParseException { | |
630 | assert ((eventDecl.getType() == CTFParser.CTF_EXPRESSION_TYPE) || (eventDecl | |
631 | .getType() == CTFParser.CTF_EXPRESSION_VAL)); | |
632 | ||
633 | /* There should be a left and right */ | |
634 | assert (eventDecl.getChildCount() == 2); | |
635 | ||
636 | CommonTree leftNode = (CommonTree) eventDecl.getChild(0); | |
637 | assert (leftNode.getType() == CTFParser.CTF_LEFT); | |
638 | CommonTree rightNode = (CommonTree) eventDecl.getChild(1); | |
639 | assert (rightNode.getType() == CTFParser.CTF_RIGHT); | |
640 | ||
641 | List<CommonTree> leftStrings = leftNode.getChildren(); | |
642 | assert (leftStrings != null); | |
643 | ||
644 | if (!isUnaryString(leftStrings.get(0))) { | |
645 | throw new ParseException( | |
646 | "Left side of CTF assignment must be a string"); //$NON-NLS-1$ | |
647 | } | |
648 | ||
649 | String left = concatenateUnaryStrings(leftStrings); | |
650 | ||
651 | if (left.equals("name")) { //$NON-NLS-1$ | |
652 | if (event.nameIsSet()) { | |
653 | throw new ParseException("name already defined"); //$NON-NLS-1$ | |
654 | } | |
655 | ||
656 | String name = getEventName(rightNode); | |
657 | ||
658 | event.setName(name); | |
659 | } else if (left.equals("id")) { //$NON-NLS-1$ | |
660 | if (event.idIsSet()) { | |
661 | throw new ParseException("id already defined"); //$NON-NLS-1$ | |
662 | } | |
663 | ||
664 | long id = getEventID(rightNode); | |
665 | ||
666 | event.setId(id); | |
667 | } else if (left.equals("stream_id")) { //$NON-NLS-1$ | |
668 | if (event.streamIsSet()) { | |
669 | throw new ParseException("stream id already defined"); //$NON-NLS-1$ | |
670 | } | |
671 | ||
672 | long streamId = getStreamID(rightNode); | |
673 | ||
674 | Stream stream = trace.getStream(streamId); | |
675 | ||
676 | if (stream == null) { | |
677 | throw new ParseException("Stream " + streamId + " not found"); //$NON-NLS-1$ //$NON-NLS-2$ | |
678 | } | |
679 | ||
680 | event.setStream(stream); | |
681 | } else if (left.equals("context")) { //$NON-NLS-1$ | |
682 | if (event.contextIsSet()) { | |
683 | throw new ParseException("context already defined"); //$NON-NLS-1$ | |
684 | } | |
685 | ||
686 | CommonTree typeSpecifier = (CommonTree) rightNode.getChild(0); | |
687 | ||
688 | if (typeSpecifier.getType() != CTFParser.TYPE_SPECIFIER_LIST) { | |
689 | throw new ParseException("context expects a type specifier"); //$NON-NLS-1$ | |
690 | } | |
691 | ||
692 | IDeclaration contextDecl = parseTypeSpecifierList(typeSpecifier, | |
693 | null); | |
694 | ||
695 | if (!(contextDecl instanceof StructDeclaration)) { | |
696 | throw new ParseException("context expects a struct"); //$NON-NLS-1$ | |
697 | } | |
698 | ||
699 | event.setContext((StructDeclaration) contextDecl); | |
700 | } else if (left.equals("fields")) { //$NON-NLS-1$ | |
701 | if (event.fieldsIsSet()) { | |
702 | throw new ParseException("fields already defined"); //$NON-NLS-1$ | |
703 | } | |
704 | ||
705 | CommonTree typeSpecifier = (CommonTree) rightNode.getChild(0); | |
706 | ||
707 | if (typeSpecifier.getType() != CTFParser.TYPE_SPECIFIER_LIST) { | |
708 | throw new ParseException("fields expects a type specifier"); //$NON-NLS-1$ | |
709 | } | |
710 | ||
13be1a8f AM |
711 | IDeclaration fieldsDecl; |
712 | fieldsDecl = parseTypeSpecifierList(typeSpecifier, null); | |
866e5b51 FC |
713 | |
714 | if (!(fieldsDecl instanceof StructDeclaration)) { | |
715 | throw new ParseException("fields expects a struct"); //$NON-NLS-1$ | |
716 | } | |
717 | /* | |
91847dfc FC |
718 | * The underscores in the event names. |
719 | * These underscores were added by the LTTng tracer. | |
866e5b51 | 720 | */ |
91847dfc | 721 | final StructDeclaration fields = (StructDeclaration) fieldsDecl; |
866e5b51 | 722 | event.setFields(fields); |
53047a66 MK |
723 | } |
724 | else if (left.equals("loglevel")){ //$NON-NLS-1$ | |
866e5b51 | 725 | |
53047a66 MK |
726 | long logLevel = parseUnaryInteger((CommonTree) rightNode.getChild(0)) ; |
727 | event.setLogLevel(logLevel); | |
866e5b51 FC |
728 | } else { |
729 | throw new ParseException("Unknown event attribute : " + left); //$NON-NLS-1$ | |
730 | } | |
731 | } | |
732 | ||
733 | /** | |
734 | * Parses a declaration at the root level. | |
735 | * | |
736 | * @param declaration | |
737 | * The declaration subtree. | |
738 | * @throws ParseException | |
739 | */ | |
740 | private void parseRootDeclaration(CommonTree declaration) | |
741 | throws ParseException { | |
742 | assert (declaration.getType() == CTFParser.DECLARATION); | |
743 | ||
744 | List<CommonTree> children = declaration.getChildren(); | |
745 | assert (children != null); | |
746 | ||
747 | for (CommonTree child : children) { | |
748 | switch (child.getType()) { | |
749 | case CTFParser.TYPEDEF: | |
750 | parseTypedef(child); | |
751 | break; | |
752 | case CTFParser.TYPEALIAS: | |
753 | parseTypealias(child); | |
754 | break; | |
755 | case CTFParser.TYPE_SPECIFIER_LIST: | |
756 | parseTypeSpecifierList(child, null); | |
757 | break; | |
758 | default: | |
759 | childTypeError(child); | |
760 | } | |
761 | } | |
762 | } | |
763 | ||
764 | /** | |
765 | * Parses a typealias node. It parses the target, the alias, and registers | |
766 | * the type in the current scope. | |
767 | * | |
768 | * @param typealias | |
769 | * A TYPEALIAS node. | |
770 | * @throws ParseException | |
771 | */ | |
772 | private void parseTypealias(CommonTree typealias) throws ParseException { | |
773 | assert (typealias.getType() == CTFParser.TYPEALIAS); | |
774 | ||
775 | List<CommonTree> children = typealias.getChildren(); | |
776 | assert (children != null); | |
777 | ||
778 | CommonTree target = null; | |
779 | CommonTree alias = null; | |
780 | ||
781 | for (CommonTree child : children) { | |
782 | switch (child.getType()) { | |
783 | case CTFParser.TYPEALIAS_TARGET: | |
784 | assert (target == null); | |
785 | target = child; | |
786 | break; | |
787 | case CTFParser.TYPEALIAS_ALIAS: | |
788 | assert (alias == null); | |
789 | alias = child; | |
790 | break; | |
791 | default: | |
792 | childTypeError(child); | |
793 | break; | |
794 | } | |
795 | } | |
796 | ||
797 | assert (target != null); | |
798 | assert (alias != null); | |
799 | ||
800 | IDeclaration targetDeclaration = parseTypealiasTarget(target); | |
801 | ||
802 | if (targetDeclaration instanceof VariantDeclaration) { | |
803 | if (((VariantDeclaration) targetDeclaration).isTagged()) { | |
804 | throw new ParseException( | |
805 | "Typealias of untagged variant is not permitted"); //$NON-NLS-1$ | |
806 | } | |
807 | } | |
808 | ||
809 | String aliasString = parseTypealiasAlias(alias); | |
810 | ||
811 | getCurrentScope().registerType(aliasString, targetDeclaration); | |
812 | } | |
813 | ||
814 | /** | |
815 | * Parses the target part of a typealias and gets the corresponding | |
816 | * declaration. | |
817 | * | |
818 | * @param target | |
819 | * A TYPEALIAS_TARGET node. | |
820 | * @return The corresponding declaration. | |
821 | * @throws ParseException | |
822 | */ | |
823 | private IDeclaration parseTypealiasTarget(CommonTree target) | |
824 | throws ParseException { | |
825 | assert (target.getType() == CTFParser.TYPEALIAS_TARGET); | |
826 | ||
827 | List<CommonTree> children = target.getChildren(); | |
828 | assert (children != null); | |
829 | ||
830 | CommonTree typeSpecifierList = null; | |
831 | CommonTree typeDeclaratorList = null; | |
832 | CommonTree typeDeclarator = null; | |
833 | StringBuilder identifierSB = new StringBuilder(); | |
834 | ||
835 | for (CommonTree child : children) { | |
836 | switch (child.getType()) { | |
837 | case CTFParser.TYPE_SPECIFIER_LIST: | |
838 | assert (typeSpecifierList == null); | |
839 | typeSpecifierList = child; | |
840 | break; | |
841 | case CTFParser.TYPE_DECLARATOR_LIST: | |
842 | assert (typeDeclaratorList == null); | |
843 | typeDeclaratorList = child; | |
844 | break; | |
845 | default: | |
846 | childTypeError(child); | |
847 | break; | |
848 | } | |
849 | } | |
850 | ||
851 | assert (typeSpecifierList != null); | |
852 | ||
853 | if (typeDeclaratorList != null) { | |
854 | /* | |
855 | * Only allow one declarator | |
856 | * | |
857 | * eg: "typealias uint8_t *, ** := puint8_t;" is not permitted, | |
858 | * otherwise the new type puint8_t would maps to two different | |
859 | * types. | |
860 | */ | |
861 | if (typeDeclaratorList.getChildCount() != 1) { | |
862 | throw new ParseException( | |
863 | "Only one type declarator is allowed in the typealias target"); //$NON-NLS-1$ | |
864 | } | |
865 | ||
866 | typeDeclarator = (CommonTree) typeDeclaratorList.getChild(0); | |
867 | } | |
868 | ||
869 | /* Parse the target type and get the declaration */ | |
870 | IDeclaration targetDeclaration = parseTypeDeclarator(typeDeclarator, | |
871 | typeSpecifierList, identifierSB); | |
872 | ||
873 | /* | |
874 | * We don't allow identifier in the target | |
875 | * | |
876 | * eg: "typealias uint8_t* hello := puint8_t;", the "hello" is not | |
877 | * permitted | |
878 | */ | |
879 | if (identifierSB.length() > 0) { | |
880 | throw new ParseException("Identifier (" + identifierSB.toString() //$NON-NLS-1$ | |
881 | + ") not expected in the typealias target"); //$NON-NLS-1$ | |
882 | } | |
883 | ||
884 | return targetDeclaration; | |
885 | } | |
886 | ||
887 | /** | |
888 | * Parses the alias part of a typealias. It parses the underlying specifier | |
889 | * list and declarator and creates the string representation that will be | |
890 | * used to register the type. | |
891 | * | |
892 | * @param alias | |
893 | * A TYPEALIAS_ALIAS node. | |
894 | * @return The string representation of the alias. | |
895 | * @throws ParseException | |
896 | */ | |
897 | private static String parseTypealiasAlias(CommonTree alias) | |
898 | throws ParseException { | |
899 | assert (alias.getType() == CTFParser.TYPEALIAS_ALIAS); | |
900 | ||
901 | List<CommonTree> children = alias.getChildren(); | |
902 | assert (children != null); | |
903 | ||
904 | CommonTree typeSpecifierList = null; | |
905 | CommonTree typeDeclaratorList = null; | |
906 | CommonTree typeDeclarator = null; | |
907 | List<CommonTree> pointers = new LinkedList<CommonTree>(); | |
908 | ||
909 | for (CommonTree child : children) { | |
910 | switch (child.getType()) { | |
911 | case CTFParser.TYPE_SPECIFIER_LIST: | |
912 | assert (typeSpecifierList == null); | |
913 | typeSpecifierList = child; | |
914 | break; | |
915 | case CTFParser.TYPE_DECLARATOR_LIST: | |
916 | assert (typeDeclaratorList == null); | |
917 | typeDeclaratorList = child; | |
918 | break; | |
919 | default: | |
920 | childTypeError(child); | |
921 | break; | |
922 | } | |
923 | } | |
924 | ||
925 | /* If there is a type declarator list, extract the pointers */ | |
926 | if (typeDeclaratorList != null) { | |
927 | /* | |
928 | * Only allow one declarator | |
929 | * | |
930 | * eg: "typealias uint8_t := puint8_t *, **;" is not permitted. | |
931 | */ | |
932 | if (typeDeclaratorList.getChildCount() != 1) { | |
933 | throw new ParseException( | |
934 | "Only one type declarator is allowed in the typealias alias"); //$NON-NLS-1$ | |
935 | } | |
936 | ||
937 | typeDeclarator = (CommonTree) typeDeclaratorList.getChild(0); | |
938 | ||
939 | List<CommonTree> typeDeclaratorChildren = typeDeclarator | |
940 | .getChildren(); | |
941 | assert (typeDeclaratorChildren != null); | |
942 | ||
943 | for (CommonTree child : typeDeclaratorChildren) { | |
944 | switch (child.getType()) { | |
945 | case CTFParser.POINTER: | |
946 | pointers.add(child); | |
947 | break; | |
948 | case CTFParser.IDENTIFIER: | |
949 | throw new ParseException("Identifier (" + child.getText() //$NON-NLS-1$ | |
950 | + ") not expected in the typealias target"); //$NON-NLS-1$ | |
951 | /* break; */ | |
952 | default: | |
953 | childTypeError(child); | |
954 | break; | |
955 | } | |
956 | } | |
957 | } | |
958 | ||
959 | return createTypeDeclarationString(typeSpecifierList, pointers); | |
960 | } | |
961 | ||
962 | /** | |
963 | * Parses a typedef node. This creates and registers a new declaration for | |
964 | * each declarator found in the typedef. | |
965 | * | |
966 | * @param typedef | |
967 | * A TYPEDEF node. | |
968 | * @throws ParseException | |
969 | * If there is an error creating the declaration. | |
970 | */ | |
971 | private void parseTypedef(CommonTree typedef) throws ParseException { | |
972 | assert (typedef.getType() == CTFParser.TYPEDEF); | |
973 | ||
974 | CommonTree typeDeclaratorListNode = (CommonTree) typedef | |
975 | .getFirstChildWithType(CTFParser.TYPE_DECLARATOR_LIST); | |
976 | assert (typeDeclaratorListNode != null); | |
977 | ||
978 | CommonTree typeSpecifierListNode = (CommonTree) typedef | |
979 | .getFirstChildWithType(CTFParser.TYPE_SPECIFIER_LIST); | |
980 | assert (typeSpecifierListNode != null); | |
981 | ||
982 | List<CommonTree> typeDeclaratorList = typeDeclaratorListNode | |
983 | .getChildren(); | |
984 | assert (typeDeclaratorList != null); | |
985 | ||
986 | for (CommonTree typeDeclaratorNode : typeDeclaratorList) { | |
987 | StringBuilder identifierSB = new StringBuilder(); | |
988 | ||
989 | IDeclaration type_declaration = parseTypeDeclarator( | |
990 | typeDeclaratorNode, typeSpecifierListNode, identifierSB); | |
991 | ||
992 | if (type_declaration instanceof VariantDeclaration) { | |
993 | if (((VariantDeclaration) type_declaration).isTagged()) { | |
994 | throw new ParseException( | |
995 | "Typealias of untagged variant is not permitted"); //$NON-NLS-1$ | |
996 | } | |
997 | } | |
998 | ||
999 | getCurrentScope().registerType(identifierSB.toString(), | |
1000 | type_declaration); | |
1001 | } | |
1002 | } | |
1003 | ||
1004 | /** | |
1005 | * Parses a pair type declarator / type specifier list and returns the | |
1006 | * corresponding declaration. If it is present, it also writes the | |
1007 | * identifier of the declarator in the given {@link StringBuilder}. | |
1008 | * | |
1009 | * @param typeDeclarator | |
1010 | * A TYPE_DECLARATOR node. | |
1011 | * @param typeSpecifierList | |
1012 | * A TYPE_SPECIFIER_LIST node. | |
1013 | * @param identifierSB | |
1014 | * A StringBuilder that will receive the identifier found in the | |
1015 | * declarator. | |
1016 | * @return The corresponding declaration. | |
1017 | * @throws ParseException | |
1018 | * If there is an error finding or creating the declaration. | |
1019 | */ | |
1020 | private IDeclaration parseTypeDeclarator(CommonTree typeDeclarator, | |
1021 | CommonTree typeSpecifierList, StringBuilder identifierSB) | |
1022 | throws ParseException { | |
1023 | if (typeDeclarator != null) { | |
1024 | assert (typeDeclarator.getType() == CTFParser.TYPE_DECLARATOR); | |
1025 | } | |
1026 | assert (typeSpecifierList.getType() == CTFParser.TYPE_SPECIFIER_LIST); | |
1027 | ||
1028 | IDeclaration declaration = null; | |
1029 | List<CommonTree> children = null; | |
1030 | List<CommonTree> pointers = new LinkedList<CommonTree>(); | |
1031 | List<CommonTree> lengths = new LinkedList<CommonTree>(); | |
1032 | CommonTree identifier = null; | |
1033 | ||
1034 | /* Separate the tokens by type */ | |
1035 | if (typeDeclarator != null) { | |
1036 | children = typeDeclarator.getChildren(); | |
1037 | assert (children != null); | |
1038 | for (CommonTree child : children) { | |
1039 | ||
1040 | switch (child.getType()) { | |
1041 | case CTFParser.POINTER: | |
1042 | pointers.add(child); | |
1043 | break; | |
1044 | case CTFParser.IDENTIFIER: | |
1045 | assert (identifier == null); | |
1046 | identifier = child; | |
1047 | break; | |
1048 | case CTFParser.LENGTH: | |
1049 | lengths.add(child); | |
1050 | break; | |
1051 | default: | |
1052 | childTypeError(child); | |
1053 | break; | |
1054 | } | |
1055 | } | |
1056 | ||
1057 | } | |
1058 | ||
1059 | /* | |
1060 | * Parse the type specifier list, which is the "base" type. For example, | |
1061 | * it would be int in int a[3][len]. | |
1062 | */ | |
1063 | declaration = parseTypeSpecifierList(typeSpecifierList, pointers); | |
1064 | ||
1065 | /* | |
1066 | * Each length subscript means that we must create a nested array or | |
1067 | * sequence. For example, int a[3][len] means that we have an array of 3 | |
1068 | * (sequences of length 'len' of (int)). | |
1069 | */ | |
284fdee8 | 1070 | if (lengths.size() > 0 ) { |
866e5b51 FC |
1071 | /* We begin at the end */ |
1072 | Collections.reverse(lengths); | |
1073 | ||
1074 | for (CommonTree length : lengths) { | |
1075 | /* | |
1076 | * By looking at the first expression, we can determine whether | |
1077 | * it is an array or a sequence. | |
1078 | */ | |
1079 | List<CommonTree> lengthChildren = length.getChildren(); | |
1080 | assert (lengthChildren != null); | |
1081 | ||
1082 | CommonTree first = lengthChildren.get(0); | |
1083 | if (isUnaryInteger(first)) { | |
1084 | /* Array */ | |
1085 | int arrayLength = (int) parseUnaryInteger(first); | |
1086 | ||
1087 | if (arrayLength < 1) { | |
1088 | throw new ParseException("Array length is negative"); //$NON-NLS-1$ | |
1089 | } | |
1090 | ||
1091 | /* Create the array declaration. */ | |
1092 | declaration = new ArrayDeclaration(arrayLength, declaration); | |
1093 | } else if (isUnaryString(first)) { | |
1094 | /* Sequence */ | |
1095 | String lengthName = concatenateUnaryStrings(lengthChildren); | |
1096 | ||
1097 | /* Create the sequence declaration. */ | |
1098 | declaration = new SequenceDeclaration(lengthName, | |
1099 | declaration); | |
1100 | } else { | |
1101 | childTypeError(first); | |
1102 | } | |
1103 | } | |
1104 | } | |
1105 | ||
1106 | if (identifier != null) { | |
1107 | identifierSB.append(identifier.getText()); | |
1108 | } | |
1109 | ||
1110 | return declaration; | |
1111 | } | |
1112 | ||
1113 | /** | |
1114 | * Parses a type specifier list and returns the corresponding declaration. | |
1115 | * | |
1116 | * @param typeSpecifierList | |
1117 | * A TYPE_SPECIFIER_LIST node. | |
1118 | * @param pointerList | |
1119 | * A list of POINTER nodes that apply to the specified type. | |
1120 | * @return The corresponding declaration. | |
1121 | * @throws ParseException | |
1122 | * If the type has not been defined or if there is an error | |
1123 | * creating the declaration. | |
1124 | */ | |
1125 | private IDeclaration parseTypeSpecifierList(CommonTree typeSpecifierList, | |
1126 | List<CommonTree> pointerList) throws ParseException { | |
1127 | assert (typeSpecifierList.getType() == CTFParser.TYPE_SPECIFIER_LIST); | |
1128 | IDeclaration declaration = null; | |
1129 | ||
1130 | /* | |
1131 | * By looking at the first element of the type specifier list, we can | |
1132 | * determine which type it belongs to. | |
1133 | */ | |
1134 | CommonTree firstChild = (CommonTree) typeSpecifierList.getChild(0); | |
1135 | assert (firstChild != null); /* grammar */ | |
1136 | ||
1137 | switch (firstChild.getType()) { | |
1138 | case CTFParser.FLOATING_POINT: | |
53047a66 MK |
1139 | // Activator |
1140 | // .getDefault() | |
1141 | // .log("parseTypeSpecifierList: floating_point not implemented yet"); //$NON-NLS-1$ | |
1142 | declaration = parseFloat(firstChild); | |
866e5b51 FC |
1143 | break; |
1144 | case CTFParser.INTEGER: | |
1145 | declaration = parseInteger(firstChild); | |
1146 | break; | |
1147 | case CTFParser.STRING: | |
1148 | declaration = parseString(firstChild); | |
1149 | break; | |
1150 | case CTFParser.STRUCT: | |
1151 | declaration = parseStruct(firstChild); | |
1152 | break; | |
1153 | case CTFParser.VARIANT: | |
1154 | declaration = parseVariant(firstChild); | |
1155 | break; | |
1156 | case CTFParser.ENUM: | |
1157 | declaration = parseEnum(firstChild); | |
1158 | break; | |
1159 | case CTFParser.IDENTIFIER: | |
1160 | case CTFParser.FLOATTOK: | |
1161 | case CTFParser.INTTOK: | |
1162 | case CTFParser.LONGTOK: | |
1163 | case CTFParser.SHORTTOK: | |
1164 | case CTFParser.SIGNEDTOK: | |
1165 | case CTFParser.UNSIGNEDTOK: | |
1166 | case CTFParser.CHARTOK: | |
1167 | case CTFParser.DOUBLETOK: | |
1168 | case CTFParser.VOIDTOK: | |
1169 | case CTFParser.BOOLTOK: | |
1170 | case CTFParser.COMPLEXTOK: | |
1171 | case CTFParser.IMAGINARYTOK: | |
1172 | declaration = parseTypeDeclaration(typeSpecifierList, pointerList); | |
1173 | break; | |
1174 | default: | |
1175 | childTypeError(firstChild); | |
1176 | } | |
1177 | ||
1178 | assert (declaration != null); | |
1179 | return declaration; | |
1180 | } | |
1181 | ||
53047a66 MK |
1182 | private IDeclaration parseFloat(CommonTree floatingPoint) throws ParseException { |
1183 | assert (floatingPoint.getType() == CTFParser.INTEGER); | |
1184 | ||
1185 | List<CommonTree> children = floatingPoint.getChildren(); | |
1186 | ||
1187 | /* | |
1188 | * If the integer has no attributes, then it is missing the size | |
1189 | * attribute which is required | |
1190 | */ | |
1191 | if (children == null) { | |
1192 | throw new ParseException("float: missing size attribute"); //$NON-NLS-1$ | |
1193 | } | |
1194 | ||
1195 | /* The return value */ | |
1196 | FloatDeclaration floatDeclaration = null; | |
1197 | ByteOrder byteOrder = trace.getByteOrder(); | |
1198 | long alignment = 0; | |
1199 | int base = 2; | |
1200 | int exponent = 8; | |
1201 | int mantissa = 24; | |
1202 | ||
1203 | Encoding encoding = Encoding.NONE; | |
1204 | ||
1205 | /* Iterate on all integer children */ | |
1206 | for (CommonTree child : children) { | |
1207 | switch (child.getType()) { | |
1208 | case CTFParser.CTF_EXPRESSION_VAL: | |
1209 | /* | |
1210 | * An assignment expression must have 2 children, left and right | |
1211 | */ | |
1212 | assert (child.getChildCount() == 2); | |
1213 | ||
1214 | CommonTree leftNode = (CommonTree) child.getChild(0); | |
1215 | assert (leftNode.getType() == CTFParser.CTF_LEFT); | |
1216 | CommonTree rightNode = (CommonTree) child.getChild(1); | |
1217 | assert (rightNode.getType() == CTFParser.CTF_RIGHT); | |
1218 | ||
1219 | List<CommonTree> leftStrings = leftNode.getChildren(); | |
1220 | assert (leftStrings != null); | |
1221 | ||
1222 | if (!isUnaryString(leftStrings.get(0))) { | |
1223 | throw new ParseException( | |
1224 | "Left side of ctf expression must be a string"); //$NON-NLS-1$ | |
1225 | } | |
1226 | String left = concatenateUnaryStrings(leftStrings); | |
1227 | ||
1228 | if (left.equals("exp_dig")) { //$NON-NLS-1$ | |
1229 | exponent = (int) parseUnaryInteger((CommonTree) rightNode.getChild(0)); | |
1230 | } else if (left.equals("byte_order")) { //$NON-NLS-1$ | |
1231 | byteOrder = getByteOrder(rightNode); | |
1232 | } else if (left.equals("mant_dig")) { //$NON-NLS-1$ | |
1233 | mantissa = (int) parseUnaryInteger((CommonTree) rightNode.getChild(0)); | |
1234 | } else if (left.equals("align")) { //$NON-NLS-1$ | |
1235 | alignment = getAlignment(rightNode); | |
1236 | } else { | |
1237 | throw new ParseException("Float: unknown attribute " + left); //$NON-NLS-1$ | |
1238 | } | |
1239 | ||
1240 | break; | |
1241 | default: | |
1242 | childTypeError(child); | |
1243 | break; | |
1244 | } | |
1245 | } | |
1246 | int size = mantissa + exponent; | |
1247 | if (size == 0) { | |
1248 | throw new ParseException("Float missing size attribute"); //$NON-NLS-1$ | |
1249 | } | |
1250 | ||
1251 | if (alignment == 0) { | |
1252 | if ((size % 8) == 0) { | |
1253 | alignment = 1; | |
1254 | } else { | |
1255 | alignment = 8; | |
1256 | } | |
1257 | } | |
1258 | ||
fd74e6c1 | 1259 | floatDeclaration = new FloatDeclaration(exponent, mantissa, byteOrder, encoding, alignment); |
53047a66 MK |
1260 | |
1261 | assert (floatDeclaration != null); | |
1262 | return floatDeclaration; | |
1263 | ||
1264 | } | |
1265 | ||
866e5b51 FC |
1266 | /** |
1267 | * Parses a type specifier list as a user-declared type. | |
1268 | * | |
1269 | * @param typeSpecifierList | |
1270 | * A TYPE_SPECIFIER_LIST node containing a user-declared type. | |
1271 | * @param pointerList | |
1272 | * A list of POINTER nodes that apply to the type specified in | |
1273 | * typeSpecifierList. | |
1274 | * @return The corresponding declaration. | |
1275 | * @throws ParseException | |
1276 | * If the type does not exist (has not been found). | |
1277 | */ | |
1278 | private IDeclaration parseTypeDeclaration(CommonTree typeSpecifierList, | |
1279 | List<CommonTree> pointerList) throws ParseException { | |
1280 | /* Create the string representation of the type declaration */ | |
1281 | String typeStringRepresentation = createTypeDeclarationString( | |
1282 | typeSpecifierList, pointerList); | |
1283 | ||
1284 | /* Use the string representation to search the type in the current scope */ | |
1285 | IDeclaration decl = getCurrentScope().rlookupType( | |
1286 | typeStringRepresentation); | |
1287 | ||
1288 | if (decl == null) { | |
1289 | throw new ParseException("Type " + typeStringRepresentation //$NON-NLS-1$ | |
1290 | + " has not been defined."); //$NON-NLS-1$ | |
1291 | } | |
1292 | ||
1293 | return decl; | |
1294 | } | |
1295 | ||
1296 | /** | |
1297 | * Parses an integer declaration node. | |
1298 | * | |
1299 | * @param integer | |
1300 | * An INTEGER node. | |
1301 | * @return The corresponding integer declaration. | |
1302 | * @throws ParseException | |
1303 | */ | |
1304 | private IntegerDeclaration parseInteger(CommonTree integer) | |
1305 | throws ParseException { | |
1306 | assert (integer.getType() == CTFParser.INTEGER); | |
1307 | ||
1308 | List<CommonTree> children = integer.getChildren(); | |
1309 | ||
1310 | /* | |
1311 | * If the integer has no attributes, then it is missing the size | |
1312 | * attribute which is required | |
1313 | */ | |
1314 | if (children == null) { | |
1315 | throw new ParseException("integer: missing size attribute"); //$NON-NLS-1$ | |
1316 | } | |
1317 | ||
1318 | /* The return value */ | |
1319 | IntegerDeclaration integerDeclaration = null; | |
1320 | boolean signed = false; | |
1321 | ByteOrder byteOrder = trace.getByteOrder(); | |
1322 | long size = 0; | |
1323 | long alignment = 0; | |
1324 | int base = 10; | |
284fdee8 | 1325 | String clock = null; |
866e5b51 FC |
1326 | |
1327 | Encoding encoding = Encoding.NONE; | |
1328 | ||
1329 | /* Iterate on all integer children */ | |
1330 | for (CommonTree child : children) { | |
1331 | switch (child.getType()) { | |
1332 | case CTFParser.CTF_EXPRESSION_VAL: | |
1333 | /* | |
1334 | * An assignment expression must have 2 children, left and right | |
1335 | */ | |
1336 | assert (child.getChildCount() == 2); | |
1337 | ||
1338 | CommonTree leftNode = (CommonTree) child.getChild(0); | |
1339 | assert (leftNode.getType() == CTFParser.CTF_LEFT); | |
1340 | CommonTree rightNode = (CommonTree) child.getChild(1); | |
1341 | assert (rightNode.getType() == CTFParser.CTF_RIGHT); | |
1342 | ||
1343 | List<CommonTree> leftStrings = leftNode.getChildren(); | |
1344 | assert (leftStrings != null); | |
1345 | ||
1346 | if (!isUnaryString(leftStrings.get(0))) { | |
1347 | throw new ParseException( | |
1348 | "Left side of ctf expression must be a string"); //$NON-NLS-1$ | |
1349 | } | |
1350 | String left = concatenateUnaryStrings(leftStrings); | |
1351 | ||
1352 | if (left.equals("signed")) { //$NON-NLS-1$ | |
1353 | signed = getSigned(rightNode); | |
1354 | } else if (left.equals("byte_order")) { //$NON-NLS-1$ | |
1355 | byteOrder = getByteOrder(rightNode); | |
1356 | } else if (left.equals("size")) { //$NON-NLS-1$ | |
1357 | size = getSize(rightNode); | |
1358 | } else if (left.equals("align")) { //$NON-NLS-1$ | |
1359 | alignment = getAlignment(rightNode); | |
1360 | } else if (left.equals("base")) { //$NON-NLS-1$ | |
1361 | base = getBase(rightNode); | |
1362 | } else if (left.equals("encoding")) { //$NON-NLS-1$ | |
1363 | encoding = getEncoding(rightNode); | |
1364 | } else if (left.equals("map")) { //$NON-NLS-1$ | |
1365 | clock = getClock(rightNode); | |
1366 | } else { | |
1367 | throw new ParseException("Integer: unknown attribute " + left); //$NON-NLS-1$ | |
1368 | } | |
1369 | ||
1370 | break; | |
1371 | default: | |
1372 | childTypeError(child); | |
1373 | break; | |
1374 | } | |
1375 | } | |
1376 | ||
1377 | if (size == 0) { | |
1378 | throw new ParseException("Integer missing size attribute"); //$NON-NLS-1$ | |
1379 | } | |
1380 | ||
1381 | if (alignment == 0) { | |
1382 | if ((size % 8) == 0) { | |
1383 | alignment = 1; | |
1384 | } else { | |
1385 | alignment = 8; | |
1386 | } | |
1387 | } | |
1388 | ||
1389 | integerDeclaration = new IntegerDeclaration((int) size, signed, base, | |
fd74e6c1 | 1390 | byteOrder, encoding, clock, alignment); |
866e5b51 FC |
1391 | |
1392 | assert (integerDeclaration != null); | |
1393 | return integerDeclaration; | |
1394 | } | |
1395 | ||
284fdee8 | 1396 | private static String getClock(CommonTree rightNode) { |
866e5b51 FC |
1397 | return rightNode.getChild(1).getChild(0).getChild(0).getText(); |
1398 | } | |
1399 | ||
1400 | private static StringDeclaration parseString(CommonTree string) | |
1401 | throws ParseException { | |
1402 | assert (string.getType() == CTFParser.STRING); | |
1403 | ||
1404 | List<CommonTree> children = string.getChildren(); | |
1405 | StringDeclaration stringDeclaration = null; | |
1406 | ||
1407 | if (children == null) { | |
1408 | stringDeclaration = new StringDeclaration(); | |
1409 | } else { | |
1410 | Encoding encoding = Encoding.UTF8; | |
1411 | for (CommonTree child : children) { | |
1412 | switch (child.getType()) { | |
1413 | case CTFParser.CTF_EXPRESSION_VAL: | |
1414 | /* | |
1415 | * An assignment expression must have 2 children, left and | |
1416 | * right | |
1417 | */ | |
1418 | assert (child.getChildCount() == 2); | |
1419 | ||
1420 | CommonTree leftNode = (CommonTree) child.getChild(0); | |
1421 | assert (leftNode.getType() == CTFParser.CTF_LEFT); | |
1422 | CommonTree rightNode = (CommonTree) child.getChild(1); | |
1423 | assert (rightNode.getType() == CTFParser.CTF_RIGHT); | |
1424 | ||
1425 | List<CommonTree> leftStrings = leftNode.getChildren(); | |
1426 | assert (leftStrings != null); | |
1427 | ||
1428 | if (!isUnaryString(leftStrings.get(0))) { | |
1429 | throw new ParseException( | |
1430 | "Left side of ctf expression must be a string"); //$NON-NLS-1$ | |
1431 | } | |
1432 | String left = concatenateUnaryStrings(leftStrings); | |
1433 | ||
1434 | if (left.equals("encoding")) { //$NON-NLS-1$ | |
1435 | encoding = getEncoding(rightNode); | |
1436 | } else { | |
1437 | throw new ParseException("String: unknown attribute " //$NON-NLS-1$ | |
1438 | + left); | |
1439 | } | |
1440 | ||
1441 | break; | |
1442 | default: | |
1443 | childTypeError(child); | |
1444 | break; | |
1445 | } | |
1446 | } | |
1447 | ||
1448 | stringDeclaration = new StringDeclaration(encoding); | |
1449 | } | |
1450 | ||
1451 | return stringDeclaration; | |
1452 | } | |
1453 | ||
1454 | /** | |
1455 | * Parses a struct declaration and returns the corresponding declaration. | |
1456 | * | |
1457 | * @param struct | |
1458 | * An STRUCT node. | |
1459 | * @return The corresponding struct declaration. | |
1460 | * @throws ParseException | |
1461 | */ | |
1462 | private StructDeclaration parseStruct(CommonTree struct) | |
1463 | throws ParseException { | |
1464 | assert (struct.getType() == CTFParser.STRUCT); | |
1465 | ||
1466 | List<CommonTree> children = struct.getChildren(); | |
1467 | assert (children != null); | |
1468 | ||
1469 | /* The return value */ | |
1470 | StructDeclaration structDeclaration = null; | |
1471 | ||
1472 | /* Name */ | |
1473 | String structName = null; | |
1474 | boolean hasName = false; | |
1475 | ||
1476 | /* Body */ | |
1477 | CommonTree structBody = null; | |
1478 | boolean hasBody = false; | |
1479 | ||
1480 | /* Align */ | |
1481 | long structAlign = 0; | |
1482 | ||
1483 | /* Loop on all children and identify what we have to work with. */ | |
1484 | for (CommonTree child : children) { | |
1485 | switch (child.getType()) { | |
1486 | case CTFParser.STRUCT_NAME: { | |
1487 | hasName = true; | |
1488 | ||
1489 | assert (child.getChildCount() == 1); | |
1490 | CommonTree structNameIdentifier = (CommonTree) child | |
1491 | .getChild(0); | |
1492 | ||
1493 | assert (structNameIdentifier.getType() == CTFParser.IDENTIFIER); | |
1494 | structName = structNameIdentifier.getText(); | |
1495 | ||
1496 | break; | |
1497 | } | |
1498 | case CTFParser.STRUCT_BODY: { | |
1499 | hasBody = true; | |
1500 | ||
1501 | structBody = child; | |
1502 | ||
1503 | break; | |
1504 | } | |
1505 | case CTFParser.ALIGN: { | |
1506 | assert (child.getChildCount() == 1); | |
1507 | CommonTree structAlignExpression = (CommonTree) child | |
1508 | .getChild(0); | |
1509 | ||
1510 | structAlign = getAlignment(structAlignExpression); | |
1511 | ||
1512 | break; | |
1513 | } | |
1514 | default: | |
1515 | childTypeError(child); | |
1516 | ||
1517 | break; | |
1518 | } | |
1519 | } | |
1520 | ||
1521 | /* | |
1522 | * If a struct has just a body and no name (just like the song, | |
1523 | * "A Struct With No Name" by America (sorry for that...)), it's a | |
1524 | * definition of a new type, so we create the type declaration and | |
1525 | * return it. We can't add it to the declaration scope since there is no | |
1526 | * name, but that's what we want because it won't be possible to use it | |
1527 | * again to declare another field. | |
1528 | * | |
1529 | * If it has just a name, we look it up in the declaration scope and | |
1530 | * return the associated declaration. If it is not found in the | |
1531 | * declaration scope, it means that a struct with that name has not been | |
1532 | * declared, which is an error. | |
1533 | * | |
1534 | * If it has both, then we create the type declaration and register it | |
1535 | * to the current scope. | |
1536 | * | |
1537 | * If it has none, then what are we doing here ? | |
1538 | */ | |
1539 | if (hasBody) { | |
1540 | /* | |
1541 | * If struct has a name, check if already defined in the current | |
1542 | * scope. | |
1543 | */ | |
1544 | if (hasName && (getCurrentScope().lookupStruct(structName) != null)) { | |
1545 | throw new ParseException("struct " + structName //$NON-NLS-1$ | |
1546 | + " already defined."); //$NON-NLS-1$ | |
1547 | } | |
866e5b51 FC |
1548 | /* Create the declaration */ |
1549 | structDeclaration = new StructDeclaration(structAlign); | |
1550 | ||
1551 | /* Parse the body */ | |
1552 | parseStructBody(structBody, structDeclaration); | |
1553 | ||
1554 | /* If struct has name, add it to the current scope. */ | |
1555 | if (hasName) { | |
1556 | getCurrentScope().registerStruct(structName, structDeclaration); | |
1557 | } | |
1558 | } else /* !hasBody */{ | |
1559 | if (hasName) { | |
1560 | /* Name and !body */ | |
1561 | ||
1562 | /* Lookup the name in the current scope. */ | |
1563 | structDeclaration = getCurrentScope().rlookupStruct(structName); | |
1564 | ||
1565 | /* | |
1566 | * If not found, it means that a struct with such name has not | |
1567 | * been defined | |
1568 | */ | |
1569 | if (structDeclaration == null) { | |
1570 | throw new ParseException("struct " + structName //$NON-NLS-1$ | |
1571 | + " is not defined"); //$NON-NLS-1$ | |
1572 | } | |
1573 | } else { | |
1574 | /* !Name and !body */ | |
1575 | ||
1576 | /* We can't do anything with that. */ | |
1577 | throw new ParseException("struct with no name and no body"); //$NON-NLS-1$ | |
1578 | } | |
1579 | } | |
1580 | ||
1581 | assert (structDeclaration != null); | |
1582 | return structDeclaration; | |
1583 | } | |
1584 | ||
1585 | /** | |
1586 | * Parses a struct body, adding the fields to specified structure | |
1587 | * declaration. | |
1588 | * | |
1589 | * @param structBody | |
1590 | * A STRUCT_BODY node. | |
1591 | * @param structDeclaration | |
1592 | * The struct declaration. | |
1593 | * @throws ParseException | |
1594 | */ | |
1595 | private void parseStructBody(CommonTree structBody, | |
1596 | StructDeclaration structDeclaration) throws ParseException { | |
1597 | assert (structBody.getType() == CTFParser.STRUCT_BODY); | |
1598 | ||
1599 | List<CommonTree> structDeclarations = structBody.getChildren(); | |
1600 | ||
1601 | /* | |
1602 | * If structDeclaration is null, structBody has no children and the | |
1603 | * struct body is empty. | |
1604 | */ | |
1605 | if (structDeclarations != null) { | |
1606 | pushScope(); | |
1607 | ||
1608 | for (CommonTree declarationNode : structDeclarations) { | |
1609 | switch (declarationNode.getType()) { | |
1610 | case CTFParser.TYPEALIAS: | |
1611 | parseTypealias(declarationNode); | |
1612 | break; | |
1613 | case CTFParser.TYPEDEF: | |
1614 | parseTypedef(declarationNode); | |
1615 | break; | |
1616 | case CTFParser.SV_DECLARATION: | |
1617 | parseStructDeclaration(declarationNode, structDeclaration); | |
1618 | break; | |
1619 | default: | |
1620 | childTypeError(declarationNode); | |
1621 | break; | |
1622 | } | |
1623 | } | |
1624 | popScope(); | |
1625 | } | |
1626 | } | |
1627 | ||
1628 | /** | |
1629 | * Parses a declaration found in a struct. | |
1630 | * | |
1631 | * @param declaration | |
1632 | * A SV_DECLARATION node. | |
1633 | * @param struct | |
1634 | * A struct declaration. (I know, little name clash here...) | |
1635 | * @throws ParseException | |
1636 | */ | |
1637 | private void parseStructDeclaration(CommonTree declaration, | |
1638 | StructDeclaration struct) throws ParseException { | |
1639 | assert (declaration.getType() == CTFParser.SV_DECLARATION); | |
1640 | ||
1641 | List<CommonTree> children = declaration.getChildren(); | |
1642 | assert (children != null); | |
1643 | ||
1644 | /* Get the type specifier list node */ | |
1645 | CommonTree typeSpecifierListNode = (CommonTree) declaration | |
1646 | .getFirstChildWithType(CTFParser.TYPE_SPECIFIER_LIST); | |
1647 | assert (typeSpecifierListNode != null); | |
1648 | ||
1649 | /* Get the type declarator list node */ | |
1650 | CommonTree typeDeclaratorListNode = (CommonTree) declaration | |
1651 | .getFirstChildWithType(CTFParser.TYPE_DECLARATOR_LIST); | |
1652 | assert (typeDeclaratorListNode != null); | |
1653 | ||
1654 | /* Get the type declarator list */ | |
1655 | List<CommonTree> typeDeclaratorList = typeDeclaratorListNode | |
1656 | .getChildren(); | |
1657 | assert (typeDeclaratorList != null); | |
1658 | ||
1659 | /* | |
1660 | * For each type declarator, parse the declaration and add a field to | |
1661 | * the struct | |
1662 | */ | |
1663 | for (CommonTree typeDeclaratorNode : typeDeclaratorList) { | |
1664 | assert (typeDeclaratorNode.getType() == CTFParser.TYPE_DECLARATOR); | |
1665 | ||
1666 | StringBuilder identifierSB = new StringBuilder(); | |
1667 | ||
1668 | IDeclaration decl = parseTypeDeclarator(typeDeclaratorNode, | |
1669 | typeSpecifierListNode, identifierSB); | |
1670 | String fieldName = identifierSB.toString(); | |
866e5b51 FC |
1671 | |
1672 | if (struct.hasField(fieldName)) { | |
1673 | throw new ParseException("struct: duplicate field " //$NON-NLS-1$ | |
1674 | + fieldName); | |
1675 | } | |
1676 | ||
1677 | struct.addField(fieldName, decl); | |
1678 | ||
1679 | } | |
1680 | } | |
1681 | ||
1682 | /** | |
1683 | * Parses an enum declaration and returns the corresponding declaration. | |
1684 | * | |
1685 | * @param _enum | |
1686 | * An ENUM node. | |
1687 | * @return The corresponding enum declaration. | |
1688 | * @throws ParseException | |
1689 | */ | |
1690 | private EnumDeclaration parseEnum(CommonTree _enum) throws ParseException { | |
1691 | assert (_enum.getType() == CTFParser.ENUM); | |
1692 | ||
1693 | List<CommonTree> children = _enum.getChildren(); | |
1694 | assert (children != null); | |
1695 | ||
1696 | /* The return value */ | |
1697 | EnumDeclaration enumDeclaration = null; | |
1698 | ||
1699 | /* Name */ | |
1700 | String enumName = null; | |
1701 | ||
1702 | /* Body */ | |
1703 | CommonTree enumBody = null; | |
1704 | ||
1705 | /* Container type */ | |
1706 | IntegerDeclaration containerTypeDeclaration = null; | |
1707 | ||
1708 | /* Loop on all children and identify what we have to work with. */ | |
1709 | for (CommonTree child : children) { | |
1710 | switch (child.getType()) { | |
1711 | case CTFParser.ENUM_NAME: { | |
1712 | assert (enumName == null); | |
1713 | ||
1714 | assert (child.getChildCount() == 1); | |
1715 | CommonTree enumNameIdentifier = (CommonTree) child.getChild(0); | |
1716 | ||
1717 | assert (enumNameIdentifier.getType() == CTFParser.IDENTIFIER); | |
1718 | enumName = enumNameIdentifier.getText(); | |
1719 | ||
1720 | break; | |
1721 | } | |
1722 | case CTFParser.ENUM_BODY: { | |
1723 | assert (enumBody == null); | |
1724 | ||
1725 | enumBody = child; | |
1726 | ||
1727 | break; | |
1728 | } | |
1729 | case CTFParser.ENUM_CONTAINER_TYPE: { | |
1730 | assert (containerTypeDeclaration == null); | |
1731 | ||
1732 | containerTypeDeclaration = parseEnumContainerType(child); | |
1733 | ||
1734 | break; | |
1735 | } | |
1736 | default: | |
1737 | childTypeError(child); | |
1738 | break; | |
1739 | } | |
1740 | } | |
1741 | ||
1742 | /* | |
1743 | * If the container type has not been defined explicitly, we assume it | |
1744 | * is "int". | |
1745 | */ | |
1746 | if (containerTypeDeclaration == null) { | |
1747 | IDeclaration decl = getCurrentScope().rlookupType("int"); //$NON-NLS-1$ | |
1748 | ||
1749 | if (decl == null) { | |
1750 | throw new ParseException( | |
1751 | "enum container type implicit and type int not defined"); //$NON-NLS-1$ | |
1752 | } else if (!(decl instanceof IntegerDeclaration)) { | |
1753 | throw new ParseException( | |
1754 | "enum container type implicit and type int not an integer"); //$NON-NLS-1$ | |
1755 | } | |
1756 | ||
1757 | containerTypeDeclaration = (IntegerDeclaration) decl; | |
1758 | } | |
1759 | ||
1760 | /* | |
1761 | * If it has a body, it's a new declaration, otherwise it's a reference | |
1762 | * to an existing declaration. Same logic as struct. | |
1763 | */ | |
1764 | if (enumBody != null) { | |
1765 | /* | |
1766 | * If enum has a name, check if already defined in the current | |
1767 | * scope. | |
1768 | */ | |
1769 | if ((enumName != null) | |
1770 | && (getCurrentScope().lookupEnum(enumName) != null)) { | |
1771 | throw new ParseException("enum " + enumName //$NON-NLS-1$ | |
1772 | + " already defined"); //$NON-NLS-1$ | |
1773 | } | |
1774 | ||
1775 | /* Create the declaration */ | |
1776 | enumDeclaration = new EnumDeclaration(containerTypeDeclaration); | |
1777 | ||
1778 | /* Parse the body */ | |
1779 | parseEnumBody(enumBody, enumDeclaration); | |
1780 | ||
1781 | /* If the enum has name, add it to the current scope. */ | |
1782 | if (enumName != null) { | |
1783 | getCurrentScope().registerEnum(enumName, enumDeclaration); | |
1784 | } | |
1785 | } else { | |
1786 | if (enumName != null) { | |
1787 | /* Name and !body */ | |
1788 | ||
1789 | /* Lookup the name in the current scope. */ | |
1790 | enumDeclaration = getCurrentScope().rlookupEnum(enumName); | |
1791 | ||
1792 | /* | |
1793 | * If not found, it means that an enum with such name has not | |
1794 | * been defined | |
1795 | */ | |
1796 | if (enumDeclaration == null) { | |
1797 | throw new ParseException("enum " + enumName //$NON-NLS-1$ | |
1798 | + " is not defined"); //$NON-NLS-1$ | |
1799 | } | |
1800 | } else { | |
1801 | /* !Name and !body */ | |
1802 | throw new ParseException("enum with no name and no body"); //$NON-NLS-1$ | |
1803 | } | |
1804 | } | |
1805 | ||
1806 | return enumDeclaration; | |
1807 | ||
1808 | } | |
1809 | ||
1810 | /** | |
1811 | * Parses an enum body, adding the enumerators to the specified enum | |
1812 | * declaration. | |
1813 | * | |
1814 | * @param enumBody | |
1815 | * An ENUM_BODY node. | |
1816 | * @param enumDeclaration | |
1817 | * The enum declaration. | |
1818 | * @throws ParseException | |
1819 | */ | |
1820 | private void parseEnumBody(CommonTree enumBody, | |
1821 | EnumDeclaration enumDeclaration) throws ParseException { | |
1822 | assert (enumBody.getType() == CTFParser.ENUM_BODY); | |
1823 | ||
1824 | List<CommonTree> enumerators = enumBody.getChildren(); | |
1825 | /* enum body can't be empty (unlike struct). */ | |
1826 | assert (enumerators != null); | |
1827 | ||
1828 | pushScope(); | |
1829 | ||
1830 | /* | |
1831 | * Start at -1, so that if the first enumrator has no explicit value, it | |
1832 | * will choose 0 | |
1833 | */ | |
1834 | long lastHigh = -1; | |
1835 | ||
1836 | for (CommonTree enumerator : enumerators) { | |
1837 | lastHigh = parseEnumEnumerator(enumerator, enumDeclaration, | |
1838 | lastHigh); | |
1839 | } | |
1840 | ||
1841 | popScope(); | |
1842 | ||
1843 | } | |
1844 | ||
1845 | /** | |
1846 | * Parses an enumerator node and adds an enumerator declaration to an | |
1847 | * enumeration declaration. | |
1848 | * | |
1849 | * The high value of the range of the last enumerator is needed in case the | |
1850 | * current enumerator does not specify its value. | |
1851 | * | |
1852 | * @param enumerator | |
1853 | * An ENUM_ENUMERATOR node. | |
1854 | * @param enumDeclaration | |
1855 | * en enumeration declaration to which will be added the | |
1856 | * enumerator. | |
1857 | * @param lastHigh | |
1858 | * The high value of the range of the last enumerator | |
1859 | * @return The high value of the value range of the current enumerator. | |
1860 | * @throws ParseException | |
1861 | */ | |
1862 | private static long parseEnumEnumerator(CommonTree enumerator, | |
1863 | EnumDeclaration enumDeclaration, long lastHigh) | |
1864 | throws ParseException { | |
1865 | assert (enumerator.getType() == CTFParser.ENUM_ENUMERATOR); | |
1866 | ||
1867 | List<CommonTree> children = enumerator.getChildren(); | |
1868 | assert (children != null); | |
1869 | ||
1870 | long low = 0, high = 0; | |
1871 | boolean valueSpecified = false; | |
1872 | String label = null; | |
1873 | ||
1874 | for (CommonTree child : children) { | |
1875 | if (isUnaryString(child)) { | |
1876 | label = parseUnaryString(child); | |
1877 | } else if (child.getType() == CTFParser.ENUM_VALUE) { | |
1878 | assert (child.getChildCount() == 1); | |
1879 | assert (isUnaryInteger((CommonTree) child.getChild(0))); | |
1880 | ||
1881 | valueSpecified = true; | |
1882 | ||
1883 | low = parseUnaryInteger((CommonTree) child.getChild(0)); | |
1884 | high = low; | |
1885 | } else if (child.getType() == CTFParser.ENUM_VALUE_RANGE) { | |
1886 | assert (child.getChildCount() == 2); | |
1887 | assert (isUnaryInteger((CommonTree) child.getChild(0))); | |
1888 | assert (isUnaryInteger((CommonTree) child.getChild(1))); | |
1889 | ||
1890 | valueSpecified = true; | |
1891 | ||
1892 | low = parseUnaryInteger((CommonTree) child.getChild(0)); | |
1893 | high = parseUnaryInteger((CommonTree) child.getChild(1)); | |
1894 | } else { | |
1895 | childTypeError(child); | |
1896 | } | |
1897 | } | |
1898 | ||
1899 | assert (label != null); | |
1900 | ||
1901 | if (!valueSpecified) { | |
1902 | low = lastHigh + 1; | |
1903 | high = low; | |
1904 | } | |
1905 | ||
1906 | if (low > high) { | |
1907 | throw new ParseException("enum low value greater than high value"); //$NON-NLS-1$ | |
1908 | } | |
1909 | ||
1910 | if (!enumDeclaration.add(low, high, label)) { | |
1911 | throw new ParseException("enum declarator values overlap."); //$NON-NLS-1$ | |
1912 | } | |
1913 | ||
1914 | return high; | |
1915 | } | |
1916 | ||
1917 | /** | |
1918 | * Parses an enum container type node and returns the corresponding integer | |
1919 | * type. | |
1920 | * | |
1921 | * @param enumContainerType | |
1922 | * An ENUM_CONTAINER_TYPE node. | |
1923 | * @return An integer declaration corresponding to the container type. | |
1924 | * @throws ParseException | |
1925 | * If the type does not parse correctly or if it is not an | |
1926 | * integer type. | |
1927 | */ | |
1928 | private IntegerDeclaration parseEnumContainerType( | |
1929 | CommonTree enumContainerType) throws ParseException { | |
1930 | assert (enumContainerType.getType() == CTFParser.ENUM_CONTAINER_TYPE); | |
1931 | ||
1932 | /* Get the child, which should be a type specifier list */ | |
1933 | assert (enumContainerType.getChildCount() == 1); | |
1934 | CommonTree typeSpecifierList = (CommonTree) enumContainerType | |
1935 | .getChild(0); | |
1936 | ||
1937 | /* Parse it and get the corresponding declaration */ | |
1938 | IDeclaration decl = parseTypeSpecifierList(typeSpecifierList, null); | |
1939 | ||
1940 | /* If is is an integer, return it, else throw an error */ | |
1941 | if (decl instanceof IntegerDeclaration) { | |
1942 | return (IntegerDeclaration) decl; | |
1943 | } | |
1944 | throw new ParseException("enum container type must be an integer"); //$NON-NLS-1$ | |
1945 | } | |
1946 | ||
1947 | private VariantDeclaration parseVariant(CommonTree variant) | |
1948 | throws ParseException { | |
1949 | assert (variant.getType() == CTFParser.VARIANT); | |
1950 | ||
1951 | List<CommonTree> children = variant.getChildren(); | |
1952 | VariantDeclaration variantDeclaration = null; | |
1953 | ||
1954 | boolean hasName = false; | |
1955 | String variantName = null; | |
1956 | ||
1957 | boolean hasBody = false; | |
1958 | CommonTree variantBody = null; | |
1959 | ||
1960 | boolean hasTag = false; | |
1961 | String variantTag = null; | |
1962 | ||
1963 | for (CommonTree child : children) { | |
1964 | switch (child.getType()) { | |
1965 | case CTFParser.VARIANT_NAME: | |
1966 | assert (variantName == null); | |
1967 | ||
1968 | hasName = true; | |
1969 | ||
1970 | assert (child.getChildCount() == 1); | |
1971 | CommonTree variantNameIdentifier = (CommonTree) child | |
1972 | .getChild(0); | |
1973 | ||
1974 | assert (variantNameIdentifier.getType() == CTFParser.IDENTIFIER); | |
1975 | variantName = variantNameIdentifier.getText(); | |
1976 | ||
1977 | break; | |
1978 | case CTFParser.VARIANT_TAG: | |
1979 | assert (variantTag == null); | |
1980 | ||
1981 | hasTag = true; | |
1982 | ||
1983 | assert (child.getChildCount() == 1); | |
1984 | CommonTree variantTagIdentifier = (CommonTree) child | |
1985 | .getChild(0); | |
1986 | ||
1987 | assert (variantTagIdentifier.getType() == CTFParser.IDENTIFIER); | |
1988 | variantTag = variantTagIdentifier.getText(); | |
1989 | ||
1990 | break; | |
1991 | case CTFParser.VARIANT_BODY: | |
1992 | assert (variantBody == null); | |
1993 | ||
1994 | hasBody = true; | |
1995 | ||
1996 | variantBody = child; | |
1997 | ||
1998 | break; | |
1999 | default: | |
2000 | childTypeError(child); | |
2001 | break; | |
2002 | } | |
2003 | } | |
2004 | ||
2005 | if (hasBody) { | |
2006 | /* | |
2007 | * If variant has a name, check if already defined in the current | |
2008 | * scope. | |
2009 | */ | |
2010 | if (hasName | |
2011 | && (getCurrentScope().lookupVariant(variantName) != null)) { | |
2012 | throw new ParseException("variant " + variantName //$NON-NLS-1$ | |
2013 | + " already defined."); //$NON-NLS-1$ | |
2014 | } | |
2015 | ||
2016 | /* Create the declaration */ | |
2017 | variantDeclaration = new VariantDeclaration(); | |
2018 | ||
2019 | /* Parse the body */ | |
2020 | parseVariantBody(variantBody, variantDeclaration); | |
2021 | ||
2022 | /* If variant has name, add it to the current scope. */ | |
2023 | if (hasName) { | |
2024 | getCurrentScope().registerVariant(variantName, | |
2025 | variantDeclaration); | |
2026 | } | |
2027 | } else /* !hasBody */{ | |
2028 | if (hasName) { | |
2029 | /* Name and !body */ | |
2030 | ||
2031 | /* Lookup the name in the current scope. */ | |
2032 | variantDeclaration = getCurrentScope().rlookupVariant( | |
2033 | variantName); | |
2034 | ||
2035 | /* | |
2036 | * If not found, it means that a struct with such name has not | |
2037 | * been defined | |
2038 | */ | |
2039 | if (variantDeclaration == null) { | |
2040 | throw new ParseException("variant " + variantName //$NON-NLS-1$ | |
2041 | + " is not defined"); //$NON-NLS-1$ | |
2042 | } | |
2043 | } else { | |
2044 | /* !Name and !body */ | |
2045 | ||
2046 | /* We can't do anything with that. */ | |
2047 | throw new ParseException("variant with no name and no body"); //$NON-NLS-1$ | |
2048 | } | |
2049 | } | |
2050 | ||
2051 | if (hasTag) { | |
2052 | variantDeclaration.setTag(variantTag); | |
2053 | } | |
2054 | ||
2055 | assert (variantDeclaration != null); | |
2056 | return variantDeclaration; | |
2057 | } | |
2058 | ||
2059 | private void parseVariantBody(CommonTree variantBody, | |
2060 | VariantDeclaration variantDeclaration) throws ParseException { | |
2061 | assert (variantBody.getType() == CTFParser.VARIANT_BODY); | |
2062 | ||
2063 | List<CommonTree> variantDeclarations = variantBody.getChildren(); | |
2064 | assert (variantDeclarations != null); | |
2065 | ||
2066 | pushScope(); | |
2067 | ||
2068 | for (CommonTree declarationNode : variantDeclarations) { | |
2069 | switch (declarationNode.getType()) { | |
2070 | case CTFParser.TYPEALIAS: | |
2071 | parseTypealias(declarationNode); | |
2072 | break; | |
2073 | case CTFParser.TYPEDEF: | |
2074 | parseTypedef(declarationNode); | |
2075 | break; | |
2076 | case CTFParser.SV_DECLARATION: | |
2077 | parseVariantDeclaration(declarationNode, variantDeclaration); | |
2078 | break; | |
2079 | default: | |
2080 | childTypeError(declarationNode); | |
2081 | break; | |
2082 | } | |
2083 | } | |
2084 | ||
2085 | popScope(); | |
2086 | } | |
2087 | ||
2088 | private void parseVariantDeclaration(CommonTree declaration, | |
2089 | VariantDeclaration variant) throws ParseException { | |
2090 | assert (declaration.getType() == CTFParser.SV_DECLARATION); | |
2091 | ||
2092 | List<CommonTree> children = declaration.getChildren(); | |
2093 | assert (children != null); | |
2094 | ||
2095 | /* Get the type specifier list node */ | |
2096 | CommonTree typeSpecifierListNode = (CommonTree) declaration | |
2097 | .getFirstChildWithType(CTFParser.TYPE_SPECIFIER_LIST); | |
2098 | assert (typeSpecifierListNode != null); | |
2099 | ||
2100 | /* Get the type declarator list node */ | |
2101 | CommonTree typeDeclaratorListNode = (CommonTree) declaration | |
2102 | .getFirstChildWithType(CTFParser.TYPE_DECLARATOR_LIST); | |
2103 | assert (typeDeclaratorListNode != null); | |
2104 | ||
2105 | /* Get the type declarator list */ | |
2106 | List<CommonTree> typeDeclaratorList = typeDeclaratorListNode | |
2107 | .getChildren(); | |
2108 | assert (typeDeclaratorList != null); | |
2109 | ||
2110 | /* | |
2111 | * For each type declarator, parse the declaration and add a field to | |
2112 | * the variant | |
2113 | */ | |
2114 | for (CommonTree typeDeclaratorNode : typeDeclaratorList) { | |
2115 | assert (typeDeclaratorNode.getType() == CTFParser.TYPE_DECLARATOR); | |
2116 | ||
2117 | StringBuilder identifierSB = new StringBuilder(); | |
2118 | ||
2119 | IDeclaration decl = parseTypeDeclarator(typeDeclaratorNode, | |
2120 | typeSpecifierListNode, identifierSB); | |
2121 | ||
2122 | if (variant.hasField(identifierSB.toString())) { | |
2123 | throw new ParseException("variant: duplicate field " //$NON-NLS-1$ | |
2124 | + identifierSB.toString()); | |
2125 | } | |
2126 | ||
2127 | variant.addField(identifierSB.toString(), decl); | |
2128 | } | |
2129 | } | |
2130 | ||
2131 | /** | |
2132 | * Creates the string representation of a type declaration (type specifier | |
2133 | * list + pointers). | |
2134 | * | |
2135 | * @param typeSpecifierList | |
2136 | * A TYPE_SPECIFIER_LIST node. | |
2137 | * @param pointers | |
2138 | * A list of POINTER nodes. | |
2139 | * @return The string representation. | |
2140 | * @throws ParseException | |
2141 | */ | |
2142 | private static String createTypeDeclarationString( | |
2143 | CommonTree typeSpecifierList, List<CommonTree> pointers) | |
2144 | throws ParseException { | |
2145 | StringBuilder sb = new StringBuilder(); | |
2146 | ||
2147 | createTypeSpecifierListString(typeSpecifierList, sb); | |
2148 | createPointerListString(pointers, sb); | |
2149 | ||
2150 | return sb.toString(); | |
2151 | } | |
2152 | ||
2153 | /** | |
2154 | * Creates the string representation of a list of type specifiers. | |
2155 | * | |
2156 | * @param typeSpecifierList | |
2157 | * A TYPE_SPECIFIER_LIST node. | |
2158 | * @param sb | |
2159 | * A StringBuilder to which will be appended the string. | |
2160 | * @throws ParseException | |
2161 | */ | |
2162 | private static void createTypeSpecifierListString( | |
2163 | CommonTree typeSpecifierList, StringBuilder sb) | |
2164 | throws ParseException { | |
2165 | assert (typeSpecifierList.getType() == CTFParser.TYPE_SPECIFIER_LIST); | |
2166 | ||
2167 | List<CommonTree> children = typeSpecifierList.getChildren(); | |
2168 | assert (children != null); | |
2169 | ||
2170 | boolean firstItem = true; | |
2171 | ||
2172 | for (CommonTree child : children) { | |
2173 | if (!firstItem) { | |
2174 | sb.append(' '); | |
2175 | ||
2176 | } | |
2177 | ||
2178 | firstItem = false; | |
2179 | ||
2180 | /* Append the string that represents this type specifier. */ | |
2181 | createTypeSpecifierString(child, sb); | |
2182 | } | |
2183 | } | |
2184 | ||
2185 | /** | |
2186 | * Creates the string representation of a type specifier. | |
2187 | * | |
2188 | * @param typeSpecifier | |
2189 | * A TYPE_SPECIFIER node. | |
2190 | * @param sb | |
2191 | * A StringBuilder to which will be appended the string. | |
2192 | * @throws ParseException | |
2193 | */ | |
2194 | private static void createTypeSpecifierString(CommonTree typeSpecifier, | |
2195 | StringBuilder sb) throws ParseException { | |
2196 | switch (typeSpecifier.getType()) { | |
2197 | case CTFParser.FLOATTOK: | |
2198 | case CTFParser.INTTOK: | |
2199 | case CTFParser.LONGTOK: | |
2200 | case CTFParser.SHORTTOK: | |
2201 | case CTFParser.SIGNEDTOK: | |
2202 | case CTFParser.UNSIGNEDTOK: | |
2203 | case CTFParser.CHARTOK: | |
2204 | case CTFParser.DOUBLETOK: | |
2205 | case CTFParser.VOIDTOK: | |
2206 | case CTFParser.BOOLTOK: | |
2207 | case CTFParser.COMPLEXTOK: | |
2208 | case CTFParser.IMAGINARYTOK: | |
2209 | case CTFParser.CONSTTOK: | |
2210 | case CTFParser.IDENTIFIER: | |
2211 | sb.append(typeSpecifier.getText()); | |
2212 | break; | |
2213 | case CTFParser.STRUCT: { | |
2214 | CommonTree structName = (CommonTree) typeSpecifier | |
2215 | .getFirstChildWithType(CTFParser.STRUCT_NAME); | |
2216 | if (structName == null) { | |
2217 | throw new ParseException( | |
2218 | "nameless struct found in createTypeSpecifierString"); //$NON-NLS-1$ | |
2219 | } | |
2220 | assert (structName.getChildCount() == 1); | |
2221 | ||
2222 | CommonTree structNameIdentifier = (CommonTree) structName | |
2223 | .getChild(0); | |
2224 | assert (structNameIdentifier.getType() == CTFParser.IDENTIFIER); | |
2225 | ||
2226 | sb.append(structNameIdentifier.getText()); | |
2227 | break; | |
2228 | } | |
2229 | case CTFParser.VARIANT: { | |
2230 | CommonTree variantName = (CommonTree) typeSpecifier | |
2231 | .getFirstChildWithType(CTFParser.VARIANT_NAME); | |
2232 | if (variantName == null) { | |
2233 | throw new ParseException( | |
2234 | "nameless variant found in createTypeSpecifierString"); //$NON-NLS-1$ | |
2235 | } | |
2236 | assert (variantName.getChildCount() == 1); | |
2237 | ||
2238 | CommonTree variantNameIdentifier = (CommonTree) variantName | |
2239 | .getChild(0); | |
2240 | assert (variantNameIdentifier.getType() == CTFParser.IDENTIFIER); | |
2241 | ||
2242 | sb.append(variantNameIdentifier.getText()); | |
2243 | break; | |
2244 | } | |
2245 | case CTFParser.ENUM: { | |
2246 | CommonTree enumName = (CommonTree) typeSpecifier | |
2247 | .getFirstChildWithType(CTFParser.ENUM_NAME); | |
2248 | if (enumName == null) { | |
2249 | throw new ParseException( | |
2250 | "nameless enum found in createTypeSpecifierString"); //$NON-NLS-1$ | |
2251 | } | |
2252 | assert (enumName.getChildCount() == 1); | |
2253 | ||
2254 | CommonTree enumNameIdentifier = (CommonTree) enumName.getChild(0); | |
2255 | assert (enumNameIdentifier.getType() == CTFParser.IDENTIFIER); | |
2256 | ||
2257 | sb.append(enumNameIdentifier.getText()); | |
2258 | break; | |
2259 | } | |
2260 | case CTFParser.FLOATING_POINT: | |
2261 | case CTFParser.INTEGER: | |
2262 | case CTFParser.STRING: | |
2263 | throw new ParseException( | |
2264 | "CTF type found in createTypeSpecifierString"); //$NON-NLS-1$ | |
2265 | /* break; */ | |
2266 | default: | |
2267 | childTypeError(typeSpecifier); | |
2268 | break; | |
2269 | } | |
2270 | } | |
2271 | ||
2272 | /** | |
2273 | * Creates the string representation of a list of pointers. | |
2274 | * | |
2275 | * @param pointerList | |
2276 | * A list of pointer nodes. If pointerList is null, this function | |
2277 | * does nothing. | |
2278 | * @param sb | |
2279 | * A stringbuilder to which will be appended the string. | |
2280 | */ | |
2281 | private static void createPointerListString(List<CommonTree> pointerList, | |
2282 | StringBuilder sb) { | |
2283 | if (pointerList == null) { | |
2284 | return; | |
2285 | } | |
2286 | ||
2287 | for (CommonTree pointer : pointerList) { | |
2288 | assert (pointer.getType() == CTFParser.POINTER); | |
2289 | ||
2290 | sb.append(" *"); //$NON-NLS-1$ | |
2291 | if (pointer.getChildCount() > 0) { | |
2292 | assert (pointer.getChildCount() == 1); | |
2293 | CommonTree constQualifier = (CommonTree) pointer.getChild(0); | |
2294 | assert (constQualifier.getType() == CTFParser.CONSTTOK); | |
2295 | ||
2296 | sb.append(" const"); //$NON-NLS-1$ | |
2297 | } | |
2298 | } | |
2299 | } | |
2300 | ||
2301 | /** | |
2302 | * @param node | |
2303 | * The node to check. | |
2304 | * @return True if the given node is an unary string or unary integer. | |
2305 | */ | |
2306 | private static boolean isUnaryExpression(CommonTree node) { | |
2307 | return isUnaryInteger(node) || isUnaryString(node); | |
2308 | } | |
2309 | ||
2310 | /** | |
2311 | * @param node | |
2312 | * The node to check. | |
2313 | * @return True if the given node is an unary string. | |
2314 | */ | |
2315 | private static boolean isUnaryString(CommonTree node) { | |
2316 | return ((node.getType() == CTFParser.UNARY_EXPRESSION_STRING) || (node | |
2317 | .getType() == CTFParser.UNARY_EXPRESSION_STRING_QUOTES)); | |
2318 | } | |
2319 | ||
2320 | /** | |
2321 | * @param node | |
2322 | * The node to check. | |
2323 | * @return True if the given node is an unary integer. | |
2324 | */ | |
2325 | private static boolean isUnaryInteger(CommonTree node) { | |
2326 | return ((node.getType() == CTFParser.UNARY_EXPRESSION_DEC) | |
2327 | || (node.getType() == CTFParser.UNARY_EXPRESSION_HEX) || (node | |
2328 | .getType() == CTFParser.UNARY_EXPRESSION_OCT)); | |
2329 | } | |
2330 | ||
2331 | /** | |
2332 | * Parses a unary string node and return the string value. | |
2333 | * | |
2334 | * @param unaryString | |
2335 | * The unary string node to parse (type UNARY_EXPRESSION_STRING | |
2336 | * or UNARY_EXPRESSION_STRING_QUOTES). | |
2337 | * @return The string value. | |
2338 | */ | |
2339 | /* | |
2340 | * It would be really nice to remove the quotes earlier, such as in the | |
2341 | * parser. | |
2342 | */ | |
2343 | private static String parseUnaryString(CommonTree unaryString) { | |
2344 | assert (isUnaryString(unaryString)); | |
2345 | ||
2346 | assert (unaryString.getChildCount() == 1); | |
2347 | CommonTree value = (CommonTree) unaryString.getChild(0); | |
2348 | assert (value != null); | |
2349 | String strval = value.getText(); | |
2350 | ||
2351 | /* Remove quotes */ | |
2352 | if (unaryString.getType() == CTFParser.UNARY_EXPRESSION_STRING_QUOTES) { | |
2353 | strval = strval.substring(1, strval.length() - 1); | |
2354 | } | |
2355 | ||
2356 | return strval; | |
2357 | } | |
2358 | ||
2359 | /** | |
2360 | * Parses an unary integer (dec, hex or oct). | |
2361 | * | |
2362 | * @param unaryInteger | |
2363 | * An unary integer node. | |
2364 | * @return The integer value. | |
2365 | */ | |
2366 | private static long parseUnaryInteger(CommonTree unaryInteger) { | |
2367 | assert (isUnaryInteger(unaryInteger)); | |
2368 | ||
2369 | assert (unaryInteger.getChildCount() >= 1); | |
2370 | ||
2371 | List<CommonTree> children = unaryInteger.getChildren(); | |
2372 | CommonTree value = children.get(0); | |
2373 | String strval = value.getText(); | |
2374 | ||
2375 | long intval; | |
2376 | ||
2377 | if (unaryInteger.getType() == CTFParser.UNARY_EXPRESSION_DEC) { | |
2378 | intval = Long.parseLong(strval, 10); | |
2379 | } else if (unaryInteger.getType() == CTFParser.UNARY_EXPRESSION_HEX) { | |
2380 | intval = Long.parseLong(strval, 0x10); | |
2381 | } else { /* unaryInteger.getType() == CTFParser.UNARY_EXPRESSION_OCT */ | |
2382 | intval = Long.parseLong(strval, 010); // 010 == 0x08 == 8 | |
2383 | } | |
2384 | ||
2385 | /* The rest of children are sign */ | |
2386 | if ((children.size() % 2) == 0) { | |
2387 | return -intval; | |
2388 | } | |
2389 | return intval; | |
2390 | } | |
2391 | ||
2392 | private static long getMajorOrMinor(CommonTree rightNode) | |
2393 | throws ParseException { | |
2394 | assert (rightNode.getType() == CTFParser.CTF_RIGHT); | |
2395 | assert (rightNode.getChildCount() > 0); | |
2396 | ||
2397 | CommonTree firstChild = (CommonTree) rightNode.getChild(0); | |
2398 | ||
2399 | if (isUnaryInteger(firstChild)) { | |
2400 | if (rightNode.getChildCount() > 1) { | |
2401 | throw new ParseException("Invalid value for major/minor"); //$NON-NLS-1$ | |
2402 | } | |
2403 | ||
2404 | long m = parseUnaryInteger(firstChild); | |
2405 | ||
2406 | if (m < 0) { | |
2407 | throw new ParseException("Invalid value for major/minor"); //$NON-NLS-1$ | |
2408 | } | |
2409 | ||
2410 | return m; | |
2411 | } | |
2412 | throw new ParseException("Invalid value for major/minor"); //$NON-NLS-1$ | |
2413 | } | |
2414 | ||
2415 | private static UUID getUUID(CommonTree rightNode) throws ParseException { | |
2416 | assert (rightNode.getType() == CTFParser.CTF_RIGHT); | |
2417 | assert (rightNode.getChildCount() > 0); | |
2418 | ||
2419 | CommonTree firstChild = (CommonTree) rightNode.getChild(0); | |
2420 | ||
2421 | if (isUnaryString(firstChild)) { | |
2422 | if (rightNode.getChildCount() > 1) { | |
2423 | throw new ParseException("Invalid value for UUID"); //$NON-NLS-1$ | |
2424 | } | |
2425 | ||
2426 | String uuidstr = parseUnaryString(firstChild); | |
2427 | ||
2428 | try { | |
2429 | UUID uuid = UUID.fromString(uuidstr); | |
2430 | return uuid; | |
2431 | } catch (IllegalArgumentException e) { | |
2432 | throw new ParseException("Invalid format for UUID"); //$NON-NLS-1$ | |
2433 | } | |
2434 | } | |
2435 | throw new ParseException("Invalid value for UUID"); //$NON-NLS-1$ | |
2436 | } | |
2437 | ||
2438 | /** | |
2439 | * Gets the value of a "signed" integer attribute. | |
2440 | * | |
2441 | * @param rightNode | |
2442 | * A CTF_RIGHT node. | |
2443 | * @return The "signed" value as a boolean. | |
2444 | * @throws ParseException | |
2445 | */ | |
2446 | private static boolean getSigned(CommonTree rightNode) | |
2447 | throws ParseException { | |
2448 | assert (rightNode.getType() == CTFParser.CTF_RIGHT); | |
2449 | assert (rightNode.getChildCount() > 0); | |
2450 | ||
2451 | boolean ret = false; | |
2452 | CommonTree firstChild = (CommonTree) rightNode.getChild(0); | |
2453 | ||
2454 | if (isUnaryString(firstChild)) { | |
2455 | String strval = concatenateUnaryStrings(rightNode.getChildren()); | |
2456 | ||
2457 | if (strval.equals("true") || strval.equals("TRUE")) { //$NON-NLS-1$ //$NON-NLS-2$ | |
2458 | ret = true; | |
2459 | } else if (strval.equals("false") || strval.equals("FALSE")) { //$NON-NLS-1$ //$NON-NLS-2$ | |
2460 | ret = false; | |
2461 | } else { | |
2462 | throw new ParseException("Invalid boolean value " //$NON-NLS-1$ | |
2463 | + firstChild.getChild(0).getText()); | |
2464 | } | |
2465 | } else if (isUnaryInteger(firstChild)) { | |
2466 | /* Happens if the value is something like "1234.hello" */ | |
2467 | if (rightNode.getChildCount() > 1) { | |
2468 | throw new ParseException("Invalid boolean value"); //$NON-NLS-1$ | |
2469 | } | |
2470 | ||
2471 | long intval = parseUnaryInteger(firstChild); | |
2472 | ||
2473 | if (intval == 1) { | |
2474 | ret = true; | |
2475 | } else if (intval == 0) { | |
2476 | ret = false; | |
2477 | } else { | |
2478 | throw new ParseException("Invalid boolean value " //$NON-NLS-1$ | |
2479 | + firstChild.getChild(0).getText()); | |
2480 | } | |
2481 | } else { | |
2482 | throw new ParseException(); | |
2483 | } | |
2484 | ||
2485 | return ret; | |
2486 | } | |
2487 | ||
2488 | /** | |
2489 | * Gets the value of a "byte_order" integer attribute. | |
2490 | * | |
2491 | * @param rightNode | |
2492 | * A CTF_RIGHT node. | |
2493 | * @return The "byte_order" value. | |
2494 | * @throws ParseException | |
2495 | */ | |
2496 | private ByteOrder getByteOrder(CommonTree rightNode) throws ParseException { | |
2497 | assert (rightNode.getType() == CTFParser.CTF_RIGHT); | |
2498 | assert (rightNode.getChildCount() > 0); | |
2499 | ||
2500 | CommonTree firstChild = (CommonTree) rightNode.getChild(0); | |
2501 | ||
2502 | if (isUnaryString(firstChild)) { | |
2503 | String strval = concatenateUnaryStrings(rightNode.getChildren()); | |
2504 | ||
2505 | if (strval.equals("le")) { //$NON-NLS-1$ | |
2506 | return ByteOrder.LITTLE_ENDIAN; | |
2507 | } else if (strval.equals("be") || strval.equals("network")) { //$NON-NLS-1$ //$NON-NLS-2$ | |
2508 | return ByteOrder.BIG_ENDIAN; | |
2509 | } else if (strval.equals("native")) { //$NON-NLS-1$ | |
2510 | return trace.getByteOrder(); | |
2511 | } else { | |
2512 | throw new ParseException("Invalid value for byte order"); //$NON-NLS-1$ | |
2513 | } | |
2514 | } | |
2515 | throw new ParseException("Invalid value for byte order"); //$NON-NLS-1$ | |
2516 | } | |
2517 | ||
2518 | /** | |
2519 | * Determines if the given value is a valid alignment value. | |
2520 | * | |
2521 | * @param alignment | |
2522 | * The value to check. | |
2523 | * @return True if it is valid. | |
2524 | */ | |
2525 | private static boolean isValidAlignment(long alignment) { | |
2526 | return !((alignment <= 0) || ((alignment & (alignment - 1)) != 0)); | |
2527 | } | |
2528 | ||
2529 | /** | |
2530 | * Gets the value of a "size" integer attribute. | |
2531 | * | |
2532 | * @param rightNode | |
2533 | * A CTF_RIGHT node. | |
2534 | * @return The "size" value. | |
2535 | * @throws ParseException | |
2536 | */ | |
2537 | private static long getSize(CommonTree rightNode) throws ParseException { | |
2538 | assert (rightNode.getType() == CTFParser.CTF_RIGHT); | |
2539 | assert (rightNode.getChildCount() > 0); | |
2540 | ||
2541 | CommonTree firstChild = (CommonTree) rightNode.getChild(0); | |
2542 | ||
2543 | if (isUnaryInteger(firstChild)) { | |
2544 | if (rightNode.getChildCount() > 1) { | |
2545 | throw new ParseException("Invalid value for size"); //$NON-NLS-1$ | |
2546 | } | |
2547 | ||
2548 | long size = parseUnaryInteger(firstChild); | |
2549 | ||
2550 | if (size < 1) { | |
2551 | throw new ParseException("Invalid value for size"); //$NON-NLS-1$ | |
2552 | } | |
2553 | ||
2554 | return size; | |
2555 | } | |
2556 | throw new ParseException("Invalid value for size"); //$NON-NLS-1$ | |
2557 | } | |
2558 | ||
2559 | /** | |
2560 | * Gets the value of a "align" integer or struct attribute. | |
2561 | * | |
2562 | * @param node | |
2563 | * A CTF_RIGHT node or directly an unary integer. | |
2564 | * @return The align value. | |
2565 | * @throws ParseException | |
2566 | */ | |
2567 | private static long getAlignment(CommonTree node) throws ParseException { | |
2568 | assert (isUnaryExpression(node) || (node.getType() == CTFParser.CTF_RIGHT)); | |
2569 | ||
2570 | /* | |
2571 | * If a CTF_RIGHT node was passed, call getAlignment with the first | |
2572 | * child | |
2573 | */ | |
2574 | if (node.getType() == CTFParser.CTF_RIGHT) { | |
2575 | if (node.getChildCount() > 1) { | |
2576 | throw new ParseException("Invalid alignment value"); //$NON-NLS-1$ | |
2577 | } | |
2578 | ||
2579 | return getAlignment((CommonTree) node.getChild(0)); | |
2580 | } else if (isUnaryInteger(node)) { | |
2581 | long alignment = parseUnaryInteger(node); | |
2582 | ||
2583 | if (!isValidAlignment(alignment)) { | |
2584 | throw new ParseException("Invalid value for alignment : " //$NON-NLS-1$ | |
2585 | + alignment); | |
2586 | } | |
2587 | ||
2588 | return alignment; | |
2589 | } | |
2590 | throw new ParseException("Invalid value for alignment"); //$NON-NLS-1$ | |
2591 | } | |
2592 | ||
2593 | /** | |
2594 | * Gets the value of a "base" integer attribute. | |
2595 | * | |
2596 | * @param rightNode | |
2597 | * An CTF_RIGHT node. | |
2598 | * @return The "base" value. | |
2599 | * @throws ParseException | |
2600 | */ | |
2601 | private static int getBase(CommonTree rightNode) throws ParseException { | |
2602 | assert (rightNode.getType() == CTFParser.CTF_RIGHT); | |
2603 | assert (rightNode.getChildCount() > 0); | |
2604 | ||
2605 | CommonTree firstChild = (CommonTree) rightNode.getChild(0); | |
2606 | ||
2607 | if (isUnaryInteger(firstChild)) { | |
2608 | if (rightNode.getChildCount() > 1) { | |
2609 | throw new ParseException("invalid base value"); //$NON-NLS-1$ | |
2610 | } | |
2611 | ||
2612 | long intval = parseUnaryInteger(firstChild); | |
2613 | if ((intval == 2) || (intval == 8) || (intval == 10) | |
2614 | || (intval == 16)) { | |
2615 | return (int) intval; | |
2616 | } | |
2617 | throw new ParseException("Invalid value for base"); //$NON-NLS-1$ | |
2618 | } else if (isUnaryString(firstChild)) { | |
2619 | String strval = concatenateUnaryStrings(rightNode.getChildren()); | |
2620 | ||
2621 | if (strval.equals("decimal") || strval.equals("dec") //$NON-NLS-1$ //$NON-NLS-2$ | |
2622 | || strval.equals("d") || strval.equals("i") //$NON-NLS-1$ //$NON-NLS-2$ | |
2623 | || strval.equals("u")) { //$NON-NLS-1$ | |
2624 | return 10; | |
2625 | } else if (strval.equals("hexadecimal") || strval.equals("hex") //$NON-NLS-1$ //$NON-NLS-2$ | |
2626 | || strval.equals("x") || strval.equals("X") //$NON-NLS-1$ //$NON-NLS-2$ | |
2627 | || strval.equals("p")) { //$NON-NLS-1$ | |
2628 | return 16; | |
2629 | } else if (strval.equals("octal") || strval.equals("oct") //$NON-NLS-1$ //$NON-NLS-2$ | |
2630 | || strval.equals("o")) { //$NON-NLS-1$ | |
2631 | return 8; | |
2632 | } else if (strval.equals("binary") || strval.equals("b")) { //$NON-NLS-1$ //$NON-NLS-2$ | |
2633 | return 2; | |
2634 | } else { | |
2635 | throw new ParseException("Invalid value for base"); //$NON-NLS-1$ | |
2636 | } | |
2637 | } else { | |
2638 | throw new ParseException("invalid value for base"); //$NON-NLS-1$ | |
2639 | } | |
2640 | } | |
2641 | ||
2642 | /** | |
2643 | * Gets the value of an "encoding" integer attribute. | |
2644 | * | |
2645 | * @param rightNode | |
2646 | * A CTF_RIGHT node. | |
2647 | * @return The "encoding" value. | |
2648 | * @throws ParseException | |
2649 | */ | |
2650 | private static Encoding getEncoding(CommonTree rightNode) | |
2651 | throws ParseException { | |
2652 | assert (rightNode.getType() == CTFParser.CTF_RIGHT); | |
2653 | ||
2654 | CommonTree firstChild = (CommonTree) rightNode.getChild(0); | |
2655 | ||
2656 | if (isUnaryString(firstChild)) { | |
2657 | String strval = concatenateUnaryStrings(rightNode.getChildren()); | |
2658 | ||
2659 | if (strval.equals("UTF8")) { //$NON-NLS-1$ | |
2660 | return Encoding.UTF8; | |
2661 | } else if (strval.equals("ASCII")) { //$NON-NLS-1$ | |
2662 | return Encoding.ASCII; | |
2663 | } else if (strval.equals("none")) { //$NON-NLS-1$ | |
2664 | return Encoding.NONE; | |
2665 | } else { | |
2666 | throw new ParseException("Invalid value for encoding"); //$NON-NLS-1$ | |
2667 | } | |
2668 | } | |
2669 | throw new ParseException("Invalid value for encoding"); //$NON-NLS-1$ | |
2670 | } | |
2671 | ||
2672 | private static long getStreamID(CommonTree rightNode) throws ParseException { | |
2673 | assert (rightNode.getType() == CTFParser.CTF_RIGHT); | |
2674 | assert (rightNode.getChildCount() > 0); | |
2675 | ||
2676 | CommonTree firstChild = (CommonTree) rightNode.getChild(0); | |
2677 | ||
2678 | if (isUnaryInteger(firstChild)) { | |
2679 | if (rightNode.getChildCount() > 1) { | |
2680 | throw new ParseException("invalid value for stream id"); //$NON-NLS-1$ | |
2681 | } | |
2682 | ||
2683 | long intval = parseUnaryInteger(firstChild); | |
2684 | ||
2685 | return intval; | |
2686 | } | |
2687 | throw new ParseException("invalid value for stream id"); //$NON-NLS-1$ | |
2688 | } | |
2689 | ||
2690 | private static String getEventName(CommonTree rightNode) | |
2691 | throws ParseException { | |
2692 | assert (rightNode.getType() == CTFParser.CTF_RIGHT); | |
2693 | assert (rightNode.getChildCount() > 0); | |
2694 | ||
2695 | CommonTree firstChild = (CommonTree) rightNode.getChild(0); | |
2696 | ||
2697 | if (isUnaryString(firstChild)) { | |
2698 | String str = concatenateUnaryStrings(rightNode.getChildren()); | |
2699 | ||
2700 | return str; | |
2701 | } | |
2702 | throw new ParseException("invalid value for event name"); //$NON-NLS-1$ | |
2703 | } | |
2704 | ||
2705 | private static long getEventID(CommonTree rightNode) throws ParseException { | |
2706 | assert (rightNode.getType() == CTFParser.CTF_RIGHT); | |
2707 | assert (rightNode.getChildCount() > 0); | |
2708 | ||
2709 | CommonTree firstChild = (CommonTree) rightNode.getChild(0); | |
2710 | ||
2711 | if (isUnaryInteger(firstChild)) { | |
2712 | if (rightNode.getChildCount() > 1) { | |
2713 | throw new ParseException("invalid value for event id"); //$NON-NLS-1$ | |
2714 | } | |
2715 | ||
2716 | long intval = parseUnaryInteger(firstChild); | |
2717 | ||
2718 | return intval; | |
2719 | } | |
2720 | throw new ParseException("invalid value for event id"); //$NON-NLS-1$ | |
2721 | } | |
2722 | ||
2723 | /** | |
2724 | * Concatenates a list of unary strings separated by arrows (->) or dots. | |
2725 | * | |
2726 | * @param strings | |
2727 | * A list, first element being an unary string, subsequent | |
2728 | * elements being ARROW or DOT nodes with unary strings as child. | |
2729 | * @return The string representation of the unary string chain. | |
2730 | */ | |
2731 | private static String concatenateUnaryStrings(List<CommonTree> strings) { | |
2732 | assert ((strings != null) && (strings.size() > 0)); | |
2733 | ||
2734 | StringBuilder sb = new StringBuilder(); | |
2735 | ||
2736 | CommonTree first = strings.get(0); | |
2737 | sb.append(parseUnaryString(first)); | |
2738 | ||
2739 | boolean isFirst = true; | |
2740 | ||
2741 | for (CommonTree ref : strings) { | |
2742 | if (isFirst) { | |
2743 | isFirst = false; | |
2744 | continue; | |
2745 | } | |
2746 | ||
2747 | assert ((ref.getType() == CTFParser.ARROW) || (ref.getType() == CTFParser.DOT)); | |
2748 | assert (ref.getChildCount() == 1); | |
2749 | ||
2750 | CommonTree id = (CommonTree) ref.getChild(0); | |
2751 | ||
2752 | if (ref.getType() == CTFParser.ARROW) { | |
2753 | sb.append("->"); //$NON-NLS-1$ | |
2754 | } else { /* DOT */ | |
2755 | sb.append('.'); | |
2756 | } | |
2757 | ||
2758 | sb.append(parseUnaryString(id)); | |
2759 | } | |
2760 | ||
2761 | return sb.toString(); | |
2762 | } | |
2763 | ||
2764 | /** | |
2765 | * Throws a ParseException stating that the parent-child relation between | |
2766 | * the given node and its parent is not valid. It means that the shape of | |
2767 | * the AST is unexpected. | |
2768 | * | |
2769 | * @param child | |
2770 | * The invalid child node. | |
2771 | * @throws ParseException | |
2772 | */ | |
2773 | private static void childTypeError(CommonTree child) throws ParseException { | |
2774 | CommonTree parent = (CommonTree) child.getParent(); | |
2775 | String error = "Parent " + CTFParser.tokenNames[parent.getType()] //$NON-NLS-1$ | |
2776 | + " can't have a child of type " //$NON-NLS-1$ | |
2777 | + CTFParser.tokenNames[child.getType()] + "."; //$NON-NLS-1$ | |
2778 | ||
2779 | throw new ParseException(error); | |
2780 | } | |
2781 | ||
2782 | // ------------------------------------------------------------------------ | |
2783 | // Scope management | |
2784 | // ------------------------------------------------------------------------ | |
2785 | ||
2786 | /** | |
2787 | * Adds a new declaration scope on the top of the scope stack. | |
2788 | */ | |
2789 | private void pushScope() { | |
2790 | scope = new DeclarationScope(scope); | |
2791 | } | |
2792 | ||
2793 | /** | |
2794 | * Removes the top declaration scope from the scope stack. | |
2795 | */ | |
2796 | private void popScope() { | |
2797 | assert (scope != null); | |
2798 | scope = scope.getParentScope(); | |
2799 | } | |
2800 | ||
2801 | /** | |
2802 | * Returns the current declaration scope. | |
2803 | * | |
2804 | * @return The current declaration scope. | |
2805 | */ | |
2806 | private DeclarationScope getCurrentScope() { | |
2807 | assert (scope != null); | |
2808 | return scope; | |
2809 | } | |
2810 | ||
2811 | } |