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
.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
.trace
.CTFTrace
;
20 import org
.eclipse
.tracecompass
.ctf
.parser
.CTFParser
;
21 import org
.eclipse
.tracecompass
.internal
.ctf
.core
.event
.metadata
.AbstractScopedCommonTreeParser
;
22 import org
.eclipse
.tracecompass
.internal
.ctf
.core
.event
.metadata
.ParseException
;
25 * Type alias targets are the nodes of the declaration of the typealiases.
26 * Typealiases are a superset of typedef defined in TSDL.
29 * @author Matthew Khouzam
32 public final class TypeAliasTargetParser
extends AbstractScopedCommonTreeParser
{
35 * A parameter object with a trace and a scope
37 * @author Matthew Khouzam
41 public static final class Param
implements ICommonTreeParserParameter
{
42 private final DeclarationScope fDeclarationScope
;
43 private final CTFTrace fTrace
;
53 public Param(CTFTrace trace
, DeclarationScope scope
) {
55 fDeclarationScope
= scope
;
62 public static final TypeAliasTargetParser INSTANCE
= new TypeAliasTargetParser();
64 private TypeAliasTargetParser() {
68 * Parses the target part of a typealias and gets the corresponding
69 * declaration. In typealias integer{ blabla } := int, the alias is the
70 * <em>integer{ blabla }</em> part, and the target is the <em>int</em> part.
72 * Typealiases only allow one declarator.
74 * eg: "typealias uint8_t *, ** := puint8_t;" is not permitted, otherwise
75 * the new type puint8_t would maps to two different types.
78 * A TYPEALIAS_TARGET node.
80 * @return The corresponding declaration.
81 * @throws ParseException
82 * an invalid child in the tree
85 public IDeclaration
parse(CommonTree target
, ICommonTreeParserParameter param
) throws ParseException
{
86 if (!(param
instanceof Param
)) {
87 throw new IllegalArgumentException("Param must be a " + Param
.class.getCanonicalName()); //$NON-NLS-1$
89 DeclarationScope scope
= ((Param
) param
).fDeclarationScope
;
91 List
<CommonTree
> children
= target
.getChildren();
93 CommonTree typeSpecifierList
= null;
94 CommonTree typeDeclaratorList
= null;
95 CommonTree typeDeclarator
= null;
96 StringBuilder identifierSB
= new StringBuilder();
98 for (CommonTree child
: children
) {
99 switch (child
.getType()) {
100 case CTFParser
.TYPE_SPECIFIER_LIST
:
101 typeSpecifierList
= child
;
103 case CTFParser
.TYPE_DECLARATOR_LIST
:
104 typeDeclaratorList
= child
;
107 throw childTypeError(child
);
111 if (typeDeclaratorList
!= null) {
113 * Only allow one declarator
115 * eg: "typealias uint8_t *, ** := puint8_t;" is not permitted,
116 * otherwise the new type puint8_t would maps to two different
119 if (typeDeclaratorList
.getChildCount() != 1) {
120 throw new ParseException("Only one type declarator is allowed in the typealias target"); //$NON-NLS-1$
123 typeDeclarator
= (CommonTree
) typeDeclaratorList
.getChild(0);
125 if (typeSpecifierList
== null) {
126 throw new ParseException("Cannot have a typealias with no specifiers"); //$NON-NLS-1$
128 CTFTrace trace
= ((Param
) param
).fTrace
;
129 /* Parse the target type and get the declaration */
130 IDeclaration targetDeclaration
= TypeDeclaratorParser
.INSTANCE
.parse(typeDeclarator
,
131 new TypeDeclaratorParser
.Param(trace
, typeSpecifierList
, scope
, identifierSB
));
134 * We don't allow identifier in the target
136 * eg: "typealias uint8_t* hello := puint8_t;", the "hello" is not
139 if (identifierSB
.length() > 0) {
140 throw new ParseException("Identifier (" + identifierSB
.toString() //$NON-NLS-1$
141 + ") not expected in the typealias target"); //$NON-NLS-1$
144 return targetDeclaration
;