Commit | Line | Data |
---|---|---|
b1ea73b5 MK |
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.util.List; | |
14 | ||
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; | |
24 | ||
25 | /** | |
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 | |
30 | * type. | |
31 | * | |
32 | * @author Matthew Khouzam - Inital API and implementation | |
33 | * @author Efficios - Documentation | |
34 | * | |
35 | */ | |
4055c3a1 | 36 | public final class TypeAliasParser extends AbstractScopedCommonTreeParser { |
b1ea73b5 MK |
37 | |
38 | /** | |
39 | * Parameters for the typealias parser | |
40 | * | |
41 | * @author Matthew Khouzam | |
42 | * | |
43 | */ | |
44 | @NonNullByDefault | |
45 | public static final class Param implements ICommonTreeParserParameter { | |
46 | private final DeclarationScope fDeclarationScope; | |
47 | private final CTFTrace fTrace; | |
48 | ||
49 | /** | |
50 | * Parameter constructor | |
51 | * | |
52 | * @param trace | |
53 | * the trace | |
54 | * @param scope | |
55 | * the scope | |
56 | */ | |
57 | public Param(CTFTrace trace, DeclarationScope scope) { | |
58 | fTrace = trace; | |
59 | fDeclarationScope = scope; | |
60 | } | |
61 | } | |
62 | ||
63 | /** | |
64 | * Instance | |
65 | */ | |
5c50e284 | 66 | public static final TypeAliasParser INSTANCE = new TypeAliasParser(); |
b1ea73b5 MK |
67 | |
68 | private TypeAliasParser() { | |
69 | } | |
70 | ||
71 | @Override | |
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$ | |
75 | } | |
76 | DeclarationScope scope = ((Param) param).fDeclarationScope; | |
77 | ||
78 | List<CommonTree> children = typealias.getChildren(); | |
79 | ||
80 | CommonTree target = null; | |
81 | CommonTree alias = null; | |
82 | ||
83 | for (CommonTree child : children) { | |
84 | switch (child.getType()) { | |
85 | case CTFParser.TYPEALIAS_TARGET: | |
86 | target = child; | |
87 | break; | |
88 | case CTFParser.TYPEALIAS_ALIAS: | |
89 | alias = child; | |
90 | break; | |
91 | default: | |
92 | throw childTypeError(child); | |
93 | } | |
94 | } | |
95 | CTFTrace trace = ((Param) param).fTrace; | |
96 | IDeclaration targetDeclaration = TypeAliasTargetParser.INSTANCE.parse(target, new TypeAliasTargetParser.Param(trace, scope)); | |
97 | ||
98 | if ((targetDeclaration instanceof VariantDeclaration) | |
99 | && ((VariantDeclaration) targetDeclaration).isTagged()) { | |
100 | throw new ParseException("Typealias of untagged variant is not permitted"); //$NON-NLS-1$ | |
101 | } | |
102 | ||
103 | String aliasString = TypeAliasAliasParser.INSTANCE.parse(alias, null); | |
104 | ||
105 | scope.registerType(aliasString, targetDeclaration); | |
106 | return targetDeclaration; | |
107 | } | |
108 | ||
109 | } |