-
Notifications
You must be signed in to change notification settings - Fork 11
/
TypedefParser.java
115 lines (100 loc) · 4.33 KB
/
TypedefParser.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*******************************************************************************
* Copyright (c) 2015, 2023 Ericsson
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.tracecompass.ctf.core.event.metadata.DeclarationScope;
import org.eclipse.tracecompass.ctf.core.event.types.IDeclaration;
import org.eclipse.tracecompass.ctf.core.event.types.VariantDeclaration;
import org.eclipse.tracecompass.ctf.core.trace.CTFTrace;
import org.eclipse.tracecompass.ctf.parser.CTFParser;
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.AbstractScopedCommonTreeParser;
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.ParseException;
import org.eclipse.tracecompass.internal.ctf.core.event.types.ICTFMetadataNode;
/**
* C typedef parser
*
* @author Matthew Khouzam
*
*/
public final class TypedefParser extends AbstractScopedCommonTreeParser {
/**
* Parameter object with a trace and current scope
*
* @author Matthew Khouzam
*
*/
@NonNullByDefault
public static final class Param implements ICommonTreeParserParameter {
private final DeclarationScope fDeclarationScope;
private final CTFTrace fTrace;
/**
* Constructor
*
* @param trace
* the trace
* @param scope
* the current scope
*/
public Param(CTFTrace trace, DeclarationScope scope) {
fTrace = trace;
fDeclarationScope = scope;
}
}
/**
* The instance
*/
public static final TypedefParser INSTANCE = new TypedefParser();
private TypedefParser() {
}
/**
* Parses a typedef node. This creates and registers a new declaration for
* each declarator found in the typedef.
*
* @param typedef
* A TYPEDEF node.
*
* @return map of type name to type declaration
* @throws ParseException
* If there is an error creating the declaration.
*/
@Override
public Map<String, IDeclaration> parse(ICTFMetadataNode typedef, ICommonTreeParserParameter param) throws ParseException {
if (!(param instanceof Param)) {
throw new IllegalArgumentException("Param must be a " + Param.class.getCanonicalName()); //$NON-NLS-1$
}
DeclarationScope scope = ((Param) param).fDeclarationScope;
ICTFMetadataNode typeDeclaratorListNode = typedef.getFirstChildWithType(CTFParser.TYPE_DECLARATOR_LIST);
if (typeDeclaratorListNode == null) {
throw new ParseException("Cannot have a typedef without a declarator"); //$NON-NLS-1$
}
ICTFMetadataNode typeSpecifierListNode = typedef.getFirstChildWithType(CTFParser.TYPE_SPECIFIER_LIST);
if (typeSpecifierListNode == null) {
throw new ParseException("Cannot have a typedef without specifiers"); //$NON-NLS-1$
}
List<ICTFMetadataNode> typeDeclaratorList = typeDeclaratorListNode.getChildren();
Map<String, IDeclaration> declarations = new HashMap<>();
for (ICTFMetadataNode typeDeclaratorNode : typeDeclaratorList) {
StringBuilder identifierSB = new StringBuilder();
CTFTrace trace = ((Param) param).fTrace;
IDeclaration typeDeclaration = TypeDeclaratorParser.INSTANCE.parse(typeDeclaratorNode, new TypeDeclaratorParser.Param(trace, typeSpecifierListNode, scope, identifierSB));
if ((typeDeclaration instanceof VariantDeclaration)
&& !((VariantDeclaration) typeDeclaration).isTagged()) {
throw new ParseException("Typealias of untagged variant is not permitted"); //$NON-NLS-1$
}
scope.registerType(identifierSB.toString(), typeDeclaration);
declarations.put(identifierSB.toString(), typeDeclaration);
}
return declarations;
}
}