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.struct; | |
10 | ||
11 | import static org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.TsdlUtils.childTypeError; | |
12 | ||
13 | import java.util.Collections; | |
14 | import java.util.List; | |
15 | ||
16 | import org.antlr.runtime.tree.CommonTree; | |
17 | import org.eclipse.jdt.annotation.NonNullByDefault; | |
18 | import org.eclipse.jdt.annotation.Nullable; | |
19 | import org.eclipse.tracecompass.ctf.core.event.metadata.DeclarationScope; | |
20 | import org.eclipse.tracecompass.ctf.core.event.types.StructDeclaration; | |
21 | import org.eclipse.tracecompass.ctf.core.trace.CTFTrace; | |
22 | import org.eclipse.tracecompass.ctf.parser.CTFParser; | |
23 | import org.eclipse.tracecompass.internal.ctf.core.event.metadata.AbstractScopedCommonTreeParser; | |
24 | import org.eclipse.tracecompass.internal.ctf.core.event.metadata.MetadataStrings; | |
25 | import org.eclipse.tracecompass.internal.ctf.core.event.metadata.ParseException; | |
26 | import org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.TypeAliasParser; | |
27 | import org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.TypedefParser; | |
28 | ||
29 | /** | |
30 | * A struct body can have any of the following elements, even though some of | |
31 | * them do not make sense: | |
32 | * <ul> | |
33 | * <li>align</li> | |
34 | * <li>callsite</li> | |
35 | * <li>const</li> | |
36 | * <li>char</li> | |
37 | * <li>clock</li> | |
38 | * <li>double</li> | |
39 | * <li>enum</li> | |
40 | * <li>env</li> | |
41 | * <li>event</li> | |
42 | * <li>floating_point</li> | |
43 | * <li>float</li> | |
44 | * <li>integer</li> | |
45 | * <li>int</li> | |
46 | * <li>long</li> | |
47 | * <li>short</li> | |
48 | * <li>signed</li> | |
49 | * <li>stream</li> | |
50 | * <li>string</li> | |
51 | * <li>struct</li> | |
52 | * <li>trace</li> | |
53 | * <li>typealias</li> | |
54 | * <li>typedef</li> | |
55 | * <li>unsigned</li> | |
56 | * <li>variant</li> | |
57 | * <li>void</li> | |
58 | * <li>_Bool</li> | |
59 | * <li>_Complex</li> | |
60 | * <li>_Imaginary</li> | |
61 | * </ul> | |
62 | * | |
63 | * @author Matthew Khouzam | |
64 | * | |
65 | */ | |
4055c3a1 | 66 | public final class StructBodyParser extends AbstractScopedCommonTreeParser { |
b1ea73b5 MK |
67 | |
68 | /** | |
69 | * The parameter object | |
70 | * | |
71 | * @author Matthew Khouzam | |
72 | * | |
73 | */ | |
74 | @NonNullByDefault | |
75 | public static final class Param implements ICommonTreeParserParameter { | |
76 | private final DeclarationScope fDeclarationScope; | |
77 | private final @Nullable String fName; | |
78 | private final StructDeclaration fStructDeclaration; | |
79 | private final CTFTrace fTrace; | |
80 | ||
81 | /** | |
82 | * Constructor | |
83 | * | |
84 | * @param structDeclaration | |
85 | * struct declaration to populate | |
86 | * @param trace | |
87 | * the trace | |
88 | * @param name | |
89 | * the struct name | |
90 | * @param scope | |
91 | * the current scope | |
92 | */ | |
93 | public Param(StructDeclaration structDeclaration, CTFTrace trace, @Nullable String name, DeclarationScope scope) { | |
94 | fStructDeclaration = structDeclaration; | |
95 | fTrace = trace; | |
96 | fDeclarationScope = scope; | |
97 | fName = name; | |
98 | } | |
99 | } | |
100 | ||
101 | /** | |
102 | * The instance | |
103 | */ | |
104 | public final static StructBodyParser INSTANCE = new StructBodyParser(); | |
105 | ||
106 | private StructBodyParser() { | |
107 | } | |
108 | ||
109 | /** | |
110 | * Parse the body of a struct, so anything between the '{' '}' | |
111 | * | |
112 | * @param structBody | |
113 | * the struct body AST node | |
114 | * @param param | |
115 | * the struct body parameters | |
116 | * @return {@link StructDeclaration} that is now populated | |
117 | * @throws ParseException | |
118 | * The AST is malformed | |
119 | */ | |
120 | @Override | |
121 | public StructDeclaration parse(CommonTree structBody, ICommonTreeParserParameter param) throws ParseException { | |
122 | if (!(param instanceof Param)) { | |
123 | throw new IllegalArgumentException("Param must be a " + Param.class.getCanonicalName()); //$NON-NLS-1$ | |
124 | } | |
125 | String structName = ((Param) param).fName; | |
126 | final DeclarationScope scope = new DeclarationScope(((Param) param).fDeclarationScope, structName == null ? MetadataStrings.STRUCT : structName); | |
127 | StructDeclaration structDeclaration = ((Param) param).fStructDeclaration; | |
128 | List<CommonTree> structDeclarations = structBody.getChildren(); | |
129 | if (structDeclarations == null) { | |
130 | structDeclarations = Collections.emptyList(); | |
131 | } | |
132 | ||
133 | /* | |
134 | * If structDeclaration is null, structBody has no children and the | |
135 | * struct body is empty. | |
136 | */ | |
137 | ||
138 | CTFTrace trace = ((Param) param).fTrace; | |
139 | ||
140 | for (CommonTree declarationNode : structDeclarations) { | |
141 | switch (declarationNode.getType()) { | |
142 | case CTFParser.TYPEALIAS: | |
143 | TypeAliasParser.INSTANCE.parse(declarationNode, new TypeAliasParser.Param(trace, scope)); | |
144 | break; | |
145 | case CTFParser.TYPEDEF: | |
146 | TypedefParser.INSTANCE.parse(declarationNode, new TypedefParser.Param(trace, scope)); | |
147 | StructDeclarationParser.INSTANCE.parse(declarationNode, new StructDeclarationParser.Param(structDeclaration, trace, scope)); | |
148 | break; | |
149 | case CTFParser.SV_DECLARATION: | |
150 | StructDeclarationParser.INSTANCE.parse(declarationNode, new StructDeclarationParser.Param(structDeclaration, trace, scope)); | |
151 | break; | |
152 | default: | |
153 | throw childTypeError(declarationNode); | |
154 | } | |
155 | } | |
156 | return structDeclaration; | |
157 | } | |
158 | ||
159 | } |