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 java
.util
.HashMap
;
12 import java
.util
.List
;
15 import org
.antlr
.runtime
.tree
.CommonTree
;
16 import org
.eclipse
.jdt
.annotation
.NonNullByDefault
;
17 import org
.eclipse
.tracecompass
.ctf
.core
.event
.metadata
.DeclarationScope
;
18 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.IDeclaration
;
19 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.VariantDeclaration
;
20 import org
.eclipse
.tracecompass
.ctf
.core
.trace
.CTFTrace
;
21 import org
.eclipse
.tracecompass
.ctf
.parser
.CTFParser
;
22 import org
.eclipse
.tracecompass
.internal
.ctf
.core
.event
.metadata
.AbstractScopedCommonTreeParser
;
23 import org
.eclipse
.tracecompass
.internal
.ctf
.core
.event
.metadata
.ParseException
;
28 * @author Matthew Khouzam
31 public final class TypedefParser
extends AbstractScopedCommonTreeParser
{
34 * Parameter object with a trace and current scope
36 * @author Matthew Khouzam
40 public static final class Param
implements ICommonTreeParserParameter
{
41 private final DeclarationScope fDeclarationScope
;
42 private final CTFTrace fTrace
;
52 public Param(CTFTrace trace
, DeclarationScope scope
) {
54 fDeclarationScope
= scope
;
61 public static final TypedefParser INSTANCE
= new TypedefParser();
63 private TypedefParser() {
67 * Parses a typedef node. This creates and registers a new declaration for
68 * each declarator found in the typedef.
73 * @return map of type name to type declaration
74 * @throws ParseException
75 * If there is an error creating the declaration.
78 public Map
<String
, IDeclaration
> parse(CommonTree typedef
, ICommonTreeParserParameter param
) throws ParseException
{
79 if (!(param
instanceof Param
)) {
80 throw new IllegalArgumentException("Param must be a " + Param
.class.getCanonicalName()); //$NON-NLS-1$
82 DeclarationScope scope
= ((Param
) param
).fDeclarationScope
;
84 CommonTree typeDeclaratorListNode
= (CommonTree
) typedef
.getFirstChildWithType(CTFParser
.TYPE_DECLARATOR_LIST
);
85 if (typeDeclaratorListNode
== null) {
86 throw new ParseException("Cannot have a typedef without a declarator"); //$NON-NLS-1$
88 CommonTree typeSpecifierListNode
= (CommonTree
) typedef
.getFirstChildWithType(CTFParser
.TYPE_SPECIFIER_LIST
);
89 if (typeSpecifierListNode
== null) {
90 throw new ParseException("Cannot have a typedef without specifiers"); //$NON-NLS-1$
92 List
<CommonTree
> typeDeclaratorList
= typeDeclaratorListNode
.getChildren();
94 Map
<String
, IDeclaration
> declarations
= new HashMap
<>();
96 for (CommonTree typeDeclaratorNode
: typeDeclaratorList
) {
97 StringBuilder identifierSB
= new StringBuilder();
98 CTFTrace trace
= ((Param
) param
).fTrace
;
99 IDeclaration typeDeclaration
= TypeDeclaratorParser
.INSTANCE
.parse(typeDeclaratorNode
, new TypeDeclaratorParser
.Param(trace
, typeSpecifierListNode
, scope
, identifierSB
));
101 if ((typeDeclaration
instanceof VariantDeclaration
)
102 && !((VariantDeclaration
) typeDeclaration
).isTagged()) {
103 throw new ParseException("Typealias of untagged variant is not permitted"); //$NON-NLS-1$
106 scope
.registerType(identifierSB
.toString(), typeDeclaration
);
108 declarations
.put(identifierSB
.toString(), typeDeclaration
);