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
.struct
;
11 import java
.util
.List
;
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
;
25 * Structures follow the ISO/C standard for structures
27 * @author Matthew Khouzam
30 public final class StructDeclarationParser
extends AbstractScopedCommonTreeParser
{
35 * @author Matthew Khouzam
39 public static final class Param
implements ICommonTreeParserParameter
{
40 private final StructDeclaration fStruct
;
41 private final DeclarationScope fDeclarationScope
;
42 private final CTFTrace fTrace
;
48 * the structure declaration
54 public Param(StructDeclaration struct
, CTFTrace trace
, DeclarationScope scope
) {
57 fDeclarationScope
= scope
;
64 public final static StructDeclarationParser INSTANCE
= new StructDeclarationParser();
66 private StructDeclarationParser() {
70 * Parses a declaration found in a struct.
73 * A SV_DECLARATION node.
75 * The parameter object
76 * @throws ParseException
77 * for poorly formed structs (duplicate fields, etc...)
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$
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
);
89 if (typeSpecifierListNode
== null) {
90 throw new ParseException("Cannot have an struct without a type specifier"); //$NON-NLS-1$
93 /* Get the type declarator list node */
94 CommonTree typeDeclaratorListNode
= (CommonTree
) declaration
.getFirstChildWithType(CTFParser
.TYPE_DECLARATOR_LIST
);
96 if (typeDeclaratorListNode
== null) {
97 throw new ParseException("Cannot have an struct without a declarator"); //$NON-NLS-1$
100 /* Get the type declarator list */
101 List
<CommonTree
> typeDeclaratorList
= typeDeclaratorListNode
.getChildren();
104 * For each type declarator, parse the declaration and add a field to
107 for (CommonTree typeDeclaratorNode
: typeDeclaratorList
) {
109 StringBuilder identifierSB
= new StringBuilder();
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
);
116 if (struct
.hasField(fieldName
)) {
117 throw new ParseException("struct: duplicate field " //$NON-NLS-1$
121 struct
.addField(fieldName
, decl
);