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 java.util.List; | |
12 | ||
13 | import org.antlr.runtime.tree.CommonTree; | |
14 | import org.eclipse.jdt.annotation.NonNullByDefault; | |
15 | import org.eclipse.tracecompass.ctf.core.event.metadata.DeclarationScope; | |
16 | import org.eclipse.tracecompass.ctf.core.event.types.IDeclaration; | |
17 | import org.eclipse.tracecompass.ctf.core.event.types.StructDeclaration; | |
18 | import org.eclipse.tracecompass.ctf.core.trace.CTFTrace; | |
19 | import org.eclipse.tracecompass.ctf.parser.CTFParser; | |
20 | import org.eclipse.tracecompass.internal.ctf.core.event.metadata.AbstractScopedCommonTreeParser; | |
21 | import org.eclipse.tracecompass.internal.ctf.core.event.metadata.ParseException; | |
22 | import org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.TypeDeclaratorParser; | |
23 | ||
24 | /** | |
25 | * Structures follow the ISO/C standard for structures | |
26 | * | |
27 | * @author Matthew Khouzam | |
28 | * | |
29 | */ | |
4055c3a1 | 30 | public final class StructDeclarationParser extends AbstractScopedCommonTreeParser { |
b1ea73b5 MK |
31 | |
32 | /** | |
33 | * Parameter object | |
34 | * | |
35 | * @author Matthew Khouzam | |
36 | * | |
37 | */ | |
38 | @NonNullByDefault | |
39 | public static final class Param implements ICommonTreeParserParameter { | |
40 | private final StructDeclaration fStruct; | |
41 | private final DeclarationScope fDeclarationScope; | |
42 | private final CTFTrace fTrace; | |
43 | ||
44 | /** | |
45 | * Constructor | |
46 | * | |
47 | * @param struct | |
48 | * the structure declaration | |
49 | * @param trace | |
50 | * the trace | |
51 | * @param scope | |
52 | * the current scope | |
53 | */ | |
54 | public Param(StructDeclaration struct, CTFTrace trace, DeclarationScope scope) { | |
55 | fStruct = struct; | |
56 | fTrace = trace; | |
57 | fDeclarationScope = scope; | |
58 | } | |
59 | } | |
60 | ||
61 | /** | |
62 | * The instance | |
63 | */ | |
5c50e284 | 64 | public static final StructDeclarationParser INSTANCE = new StructDeclarationParser(); |
b1ea73b5 MK |
65 | |
66 | private StructDeclarationParser() { | |
67 | } | |
68 | ||
69 | /** | |
70 | * Parses a declaration found in a struct. | |
71 | * | |
72 | * @param declaration | |
73 | * A SV_DECLARATION node. | |
74 | * @param param | |
75 | * The parameter object | |
76 | * @throws ParseException | |
77 | * for poorly formed structs (duplicate fields, etc...) | |
78 | */ | |
79 | @Override | |
80 | public StructDeclaration parse(CommonTree declaration, ICommonTreeParserParameter param) throws ParseException { | |
81 | if (!(param instanceof Param)) { | |
82 | throw new IllegalArgumentException("Param must be a " + Param.class.getCanonicalName()); //$NON-NLS-1$ | |
83 | } | |
84 | DeclarationScope scope = ((Param) param).fDeclarationScope; | |
85 | StructDeclaration struct = ((Param) param).fStruct; | |
86 | /* Get the type specifier list node */ | |
87 | CommonTree typeSpecifierListNode = (CommonTree) declaration.getFirstChildWithType(CTFParser.TYPE_SPECIFIER_LIST); | |
88 | ||
89 | if (typeSpecifierListNode == null) { | |
90 | throw new ParseException("Cannot have an struct without a type specifier"); //$NON-NLS-1$ | |
91 | } | |
92 | ||
93 | /* Get the type declarator list node */ | |
94 | CommonTree typeDeclaratorListNode = (CommonTree) declaration.getFirstChildWithType(CTFParser.TYPE_DECLARATOR_LIST); | |
95 | ||
96 | if (typeDeclaratorListNode == null) { | |
97 | throw new ParseException("Cannot have an struct without a declarator"); //$NON-NLS-1$ | |
98 | } | |
99 | ||
100 | /* Get the type declarator list */ | |
101 | List<CommonTree> typeDeclaratorList = typeDeclaratorListNode.getChildren(); | |
102 | ||
103 | /* | |
104 | * For each type declarator, parse the declaration and add a field to | |
105 | * the struct | |
106 | */ | |
107 | for (CommonTree typeDeclaratorNode : typeDeclaratorList) { | |
108 | ||
109 | StringBuilder identifierSB = new StringBuilder(); | |
110 | ||
111 | CTFTrace trace = ((Param) param).fTrace; | |
112 | IDeclaration decl = TypeDeclaratorParser.INSTANCE.parse(typeDeclaratorNode, new TypeDeclaratorParser.Param(trace, typeSpecifierListNode, scope, identifierSB)); | |
113 | String fieldName = identifierSB.toString(); | |
114 | scope.registerIdentifier(fieldName, decl); | |
115 | ||
116 | if (struct.hasField(fieldName)) { | |
117 | throw new ParseException("struct: duplicate field " //$NON-NLS-1$ | |
118 | + fieldName); | |
119 | } | |
120 | ||
121 | struct.addField(fieldName, decl); | |
122 | ||
123 | } | |
124 | return struct; | |
125 | } | |
126 | ||
127 | } |