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
.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
;
26 * The "typealias" declaration can be used to give a name (including pointer
27 * declarator specifier) to a type. It should also be used to map basic C types
28 * (float, int, unsigned long, ...) to a CTF type. Typealias is a superset of
29 * "typedef": it also allows assignment of a simple variable identifier to a
32 * @author Matthew Khouzam - Inital API and implementation
33 * @author Efficios - Documentation
36 public class TypeAliasParser
extends AbstractScopedCommonTreeParser
{
39 * Parameters for the typealias parser
41 * @author Matthew Khouzam
45 public static final class Param
implements ICommonTreeParserParameter
{
46 private final DeclarationScope fDeclarationScope
;
47 private final CTFTrace fTrace
;
50 * Parameter constructor
57 public Param(CTFTrace trace
, DeclarationScope scope
) {
59 fDeclarationScope
= scope
;
66 public final static TypeAliasParser INSTANCE
= new TypeAliasParser();
68 private TypeAliasParser() {
72 public IDeclaration
parse(CommonTree typealias
, ICommonTreeParserParameter param
) throws ParseException
{
73 if (!(param
instanceof Param
)) {
74 throw new IllegalArgumentException("Param must be a " + Param
.class.getCanonicalName()); //$NON-NLS-1$
76 DeclarationScope scope
= ((Param
) param
).fDeclarationScope
;
78 List
<CommonTree
> children
= typealias
.getChildren();
80 CommonTree target
= null;
81 CommonTree alias
= null;
83 for (CommonTree child
: children
) {
84 switch (child
.getType()) {
85 case CTFParser
.TYPEALIAS_TARGET
:
88 case CTFParser
.TYPEALIAS_ALIAS
:
92 throw childTypeError(child
);
95 CTFTrace trace
= ((Param
) param
).fTrace
;
96 IDeclaration targetDeclaration
= TypeAliasTargetParser
.INSTANCE
.parse(target
, new TypeAliasTargetParser
.Param(trace
, scope
));
98 if ((targetDeclaration
instanceof VariantDeclaration
)
99 && ((VariantDeclaration
) targetDeclaration
).isTagged()) {
100 throw new ParseException("Typealias of untagged variant is not permitted"); //$NON-NLS-1$
103 String aliasString
= TypeAliasAliasParser
.INSTANCE
.parse(alias
, null);
105 scope
.registerType(aliasString
, targetDeclaration
);
106 return targetDeclaration
;