ctf: fix Modifier order to always be static final
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.ctf.core / src / org / eclipse / tracecompass / internal / ctf / core / event / metadata / tsdl / TypeSpecifierListParser.java
1 /*******************************************************************************
2 * Copyright (c) 2015 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *******************************************************************************/
9 package org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl;
10
11 import static org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.TsdlUtils.childTypeError;
12
13 import java.nio.ByteOrder;
14 import java.util.List;
15
16 import org.antlr.runtime.tree.CommonTree;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.eclipse.tracecompass.ctf.core.event.metadata.DeclarationScope;
21 import org.eclipse.tracecompass.ctf.core.event.types.EnumDeclaration;
22 import org.eclipse.tracecompass.ctf.core.event.types.IDeclaration;
23 import org.eclipse.tracecompass.ctf.core.event.types.StructDeclaration;
24 import org.eclipse.tracecompass.ctf.core.trace.CTFTrace;
25 import org.eclipse.tracecompass.ctf.parser.CTFParser;
26 import org.eclipse.tracecompass.internal.ctf.core.event.metadata.AbstractScopedCommonTreeParser;
27 import org.eclipse.tracecompass.internal.ctf.core.event.metadata.ParseException;
28 import org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.enumeration.EnumParser;
29 import org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.floatingpoint.FloatDeclarationParser;
30 import org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.integer.IntegerDeclarationParser;
31 import org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.string.StringDeclarationParser;
32 import org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.struct.StructParser;
33 import org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.variant.VariantParser;
34 import org.eclipse.tracecompass.internal.ctf.core.event.types.composite.EventHeaderCompactDeclaration;
35 import org.eclipse.tracecompass.internal.ctf.core.event.types.composite.EventHeaderLargeDeclaration;
36
37 /**
38 * Parse the specifiers. Make sure they are known types. This can be seen as a
39 * declaration factory.
40 *
41 * @author Matthew Khouzam
42 *
43 */
44 public final class TypeSpecifierListParser extends AbstractScopedCommonTreeParser {
45
46 /**
47 * Parameter Object
48 *
49 * @author Matthew Khouzam
50 *
51 */
52 @NonNullByDefault
53 public static final class Param implements ICommonTreeParserParameter {
54 private final DeclarationScope fDeclarationScope;
55 private final @Nullable List<CommonTree> fListNode;
56 private final CTFTrace fTrace;
57 private final @Nullable CommonTree fIdentifier;
58
59 /**
60 * Constructor
61 *
62 * @param trace
63 * The trace
64 * @param listNode
65 * A list of POINTER nodes that apply to the specified type.
66 * @param identifier
67 * the identifier node
68 * @param scope
69 * the current scope
70 */
71 public Param(CTFTrace trace, @Nullable List<CommonTree> listNode, @Nullable CommonTree identifier, DeclarationScope scope) {
72 fTrace = trace;
73 fListNode = listNode;
74 fIdentifier = identifier;
75 fDeclarationScope = scope;
76 }
77 }
78
79 /**
80 * The instance
81 */
82 public static final TypeSpecifierListParser INSTANCE = new TypeSpecifierListParser();
83
84 private TypeSpecifierListParser() {
85 }
86
87 /**
88 * Parses a type specifier list and returns the corresponding declaration.
89 *
90 * @param typeSpecifierList
91 * A TYPE_SPECIFIER_LIST node.
92 * @param param
93 * The paramer object
94 *
95 * @return The corresponding declaration.
96 * @throws ParseException
97 * If the type has not been defined or if there is an error
98 * creating the declaration.
99 */
100 @Override
101 public IDeclaration parse(CommonTree typeSpecifierList, ICommonTreeParserParameter param) throws ParseException {
102 if (!(param instanceof Param)) {
103 throw new IllegalArgumentException("Param must be a " + Param.class.getCanonicalName()); //$NON-NLS-1$
104 }
105 final DeclarationScope scope = ((Param) param).fDeclarationScope;
106 List<@NonNull CommonTree> pointerList = ((Param) param).fListNode;
107 CTFTrace trace = ((Param) param).fTrace;
108 CommonTree identifier = ((Param) param).fIdentifier;
109 IDeclaration declaration = null;
110
111 /*
112 * By looking at the first element of the type specifier list, we can
113 * determine which type it belongs to.
114 */
115 CommonTree firstChild = (CommonTree) typeSpecifierList.getChild(0);
116
117 switch (firstChild.getType()) {
118 case CTFParser.FLOATING_POINT:
119 declaration = FloatDeclarationParser.INSTANCE.parse(firstChild, new FloatDeclarationParser.Param(trace));
120 break;
121 case CTFParser.INTEGER:
122 declaration = IntegerDeclarationParser.INSTANCE.parse(firstChild, new IntegerDeclarationParser.Param(trace));
123 break;
124 case CTFParser.STRING:
125 declaration = StringDeclarationParser.INSTANCE.parse(firstChild, null);
126 break;
127 case CTFParser.STRUCT:
128 declaration = StructParser.INSTANCE.parse(firstChild, new StructParser.Param(trace, identifier, scope));
129 StructDeclaration structDeclaration = (StructDeclaration) declaration;
130 IDeclaration idEnumDecl = structDeclaration.getFields().get("id"); //$NON-NLS-1$
131 if (idEnumDecl instanceof EnumDeclaration) {
132 EnumDeclaration enumDeclaration = (EnumDeclaration) idEnumDecl;
133 ByteOrder bo = enumDeclaration.getContainerType().getByteOrder();
134 if (EventHeaderCompactDeclaration.getEventHeader(bo).isCompactEventHeader(structDeclaration)) {
135 declaration = EventHeaderCompactDeclaration.getEventHeader(bo);
136 } else if (EventHeaderLargeDeclaration.getEventHeader(bo).isLargeEventHeader(structDeclaration)) {
137 declaration = EventHeaderLargeDeclaration.getEventHeader(bo);
138 }
139 }
140 break;
141 case CTFParser.VARIANT:
142 declaration = VariantParser.INSTANCE.parse(firstChild, new VariantParser.Param(trace, scope));
143 break;
144 case CTFParser.ENUM:
145 declaration = EnumParser.INSTANCE.parse(firstChild, new EnumParser.Param(trace, scope));
146 break;
147 case CTFParser.IDENTIFIER:
148 case CTFParser.FLOATTOK:
149 case CTFParser.INTTOK:
150 case CTFParser.LONGTOK:
151 case CTFParser.SHORTTOK:
152 case CTFParser.SIGNEDTOK:
153 case CTFParser.UNSIGNEDTOK:
154 case CTFParser.CHARTOK:
155 case CTFParser.DOUBLETOK:
156 case CTFParser.VOIDTOK:
157 case CTFParser.BOOLTOK:
158 case CTFParser.COMPLEXTOK:
159 case CTFParser.IMAGINARYTOK:
160 declaration = TypeDeclarationParser.INSTANCE.parse(typeSpecifierList, new TypeDeclarationParser.Param(pointerList, scope));
161 break;
162 default:
163 throw childTypeError(firstChild);
164 }
165
166 return declaration;
167 }
168 }
This page took 0.035222 seconds and 5 git commands to generate.