1 /*******************************************************************************
2 * Copyright (c) 2015 Ericsson
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
;
11 import static org
.eclipse
.tracecompass
.internal
.ctf
.core
.event
.metadata
.tsdl
.TsdlUtils
.childTypeError
;
13 import java
.nio
.ByteOrder
;
14 import java
.util
.List
;
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
;
38 * Parse the specifiers. Make sure they are known types. This can be seen as a
39 * declaration factory.
41 * @author Matthew Khouzam
44 public final class TypeSpecifierListParser
extends AbstractScopedCommonTreeParser
{
49 * @author Matthew Khouzam
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
;
65 * A list of POINTER nodes that apply to the specified type.
71 public Param(CTFTrace trace
, @Nullable List
<CommonTree
> listNode
, @Nullable CommonTree identifier
, DeclarationScope scope
) {
74 fIdentifier
= identifier
;
75 fDeclarationScope
= scope
;
82 public static final TypeSpecifierListParser INSTANCE
= new TypeSpecifierListParser();
84 private TypeSpecifierListParser() {
88 * Parses a type specifier list and returns the corresponding declaration.
90 * @param typeSpecifierList
91 * A TYPE_SPECIFIER_LIST node.
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.
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$
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;
112 * By looking at the first element of the type specifier list, we can
113 * determine which type it belongs to.
115 CommonTree firstChild
= (CommonTree
) typeSpecifierList
.getChild(0);
117 switch (firstChild
.getType()) {
118 case CTFParser
.FLOATING_POINT
:
119 declaration
= FloatDeclarationParser
.INSTANCE
.parse(firstChild
, new FloatDeclarationParser
.Param(trace
));
121 case CTFParser
.INTEGER
:
122 declaration
= IntegerDeclarationParser
.INSTANCE
.parse(firstChild
, new IntegerDeclarationParser
.Param(trace
));
124 case CTFParser
.STRING
:
125 declaration
= StringDeclarationParser
.INSTANCE
.parse(firstChild
, null);
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
);
141 case CTFParser
.VARIANT
:
142 declaration
= VariantParser
.INSTANCE
.parse(firstChild
, new VariantParser
.Param(trace
, scope
));
145 declaration
= EnumParser
.INSTANCE
.parse(firstChild
, new EnumParser
.Param(trace
, scope
));
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
));
163 throw childTypeError(firstChild
);