| 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.trace.CTFTrace; |
| 20 | import org.eclipse.tracecompass.ctf.parser.CTFParser; |
| 21 | import org.eclipse.tracecompass.internal.ctf.core.event.metadata.AbstractScopedCommonTreeParser; |
| 22 | import org.eclipse.tracecompass.internal.ctf.core.event.metadata.ParseException; |
| 23 | |
| 24 | /** |
| 25 | * Type alias targets are the nodes of the declaration of the typealiases. |
| 26 | * Typealiases are a superset of typedef defined in TSDL. |
| 27 | * |
| 28 | * |
| 29 | * @author Matthew Khouzam |
| 30 | * |
| 31 | */ |
| 32 | public final class TypeAliasTargetParser extends AbstractScopedCommonTreeParser { |
| 33 | |
| 34 | /** |
| 35 | * A parameter object with a trace and a scope |
| 36 | * |
| 37 | * @author Matthew Khouzam |
| 38 | * |
| 39 | */ |
| 40 | @NonNullByDefault |
| 41 | public static final class Param implements ICommonTreeParserParameter { |
| 42 | private final DeclarationScope fDeclarationScope; |
| 43 | private final CTFTrace fTrace; |
| 44 | |
| 45 | /** |
| 46 | * Constructor |
| 47 | * |
| 48 | * @param trace |
| 49 | * the trace |
| 50 | * @param scope |
| 51 | * the current scope |
| 52 | */ |
| 53 | public Param(CTFTrace trace, DeclarationScope scope) { |
| 54 | fTrace = trace; |
| 55 | fDeclarationScope = scope; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * The instance |
| 61 | */ |
| 62 | public final static TypeAliasTargetParser INSTANCE = new TypeAliasTargetParser(); |
| 63 | |
| 64 | private TypeAliasTargetParser() { |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Parses the target part of a typealias and gets the corresponding |
| 69 | * declaration. In typealias integer{ blabla } := int, the alias is the |
| 70 | * <em>integer{ blabla }</em> part, and the target is the <em>int</em> part. |
| 71 | * |
| 72 | * Typealiases only allow one declarator. |
| 73 | * |
| 74 | * eg: "typealias uint8_t *, ** := puint8_t;" is not permitted, otherwise |
| 75 | * the new type puint8_t would maps to two different types. |
| 76 | * |
| 77 | * @param target |
| 78 | * A TYPEALIAS_TARGET node. |
| 79 | * |
| 80 | * @return The corresponding declaration. |
| 81 | * @throws ParseException |
| 82 | * an invalid child in the tree |
| 83 | */ |
| 84 | @Override |
| 85 | public IDeclaration parse(CommonTree target, ICommonTreeParserParameter param) throws ParseException { |
| 86 | if (!(param instanceof Param)) { |
| 87 | throw new IllegalArgumentException("Param must be a " + Param.class.getCanonicalName()); //$NON-NLS-1$ |
| 88 | } |
| 89 | DeclarationScope scope = ((Param) param).fDeclarationScope; |
| 90 | |
| 91 | List<CommonTree> children = target.getChildren(); |
| 92 | |
| 93 | CommonTree typeSpecifierList = null; |
| 94 | CommonTree typeDeclaratorList = null; |
| 95 | CommonTree typeDeclarator = null; |
| 96 | StringBuilder identifierSB = new StringBuilder(); |
| 97 | |
| 98 | for (CommonTree child : children) { |
| 99 | switch (child.getType()) { |
| 100 | case CTFParser.TYPE_SPECIFIER_LIST: |
| 101 | typeSpecifierList = child; |
| 102 | break; |
| 103 | case CTFParser.TYPE_DECLARATOR_LIST: |
| 104 | typeDeclaratorList = child; |
| 105 | break; |
| 106 | default: |
| 107 | throw childTypeError(child); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | if (typeDeclaratorList != null) { |
| 112 | /* |
| 113 | * Only allow one declarator |
| 114 | * |
| 115 | * eg: "typealias uint8_t *, ** := puint8_t;" is not permitted, |
| 116 | * otherwise the new type puint8_t would maps to two different |
| 117 | * types. |
| 118 | */ |
| 119 | if (typeDeclaratorList.getChildCount() != 1) { |
| 120 | throw new ParseException("Only one type declarator is allowed in the typealias target"); //$NON-NLS-1$ |
| 121 | } |
| 122 | |
| 123 | typeDeclarator = (CommonTree) typeDeclaratorList.getChild(0); |
| 124 | } |
| 125 | if (typeSpecifierList == null) { |
| 126 | throw new ParseException("Cannot have a typealias with no specifiers"); //$NON-NLS-1$ |
| 127 | } |
| 128 | CTFTrace trace = ((Param) param).fTrace; |
| 129 | /* Parse the target type and get the declaration */ |
| 130 | IDeclaration targetDeclaration = TypeDeclaratorParser.INSTANCE.parse(typeDeclarator, |
| 131 | new TypeDeclaratorParser.Param(trace, typeSpecifierList, scope, identifierSB)); |
| 132 | |
| 133 | /* |
| 134 | * We don't allow identifier in the target |
| 135 | * |
| 136 | * eg: "typealias uint8_t* hello := puint8_t;", the "hello" is not |
| 137 | * permitted |
| 138 | */ |
| 139 | if (identifierSB.length() > 0) { |
| 140 | throw new ParseException("Identifier (" + identifierSB.toString() //$NON-NLS-1$ |
| 141 | + ") not expected in the typealias target"); //$NON-NLS-1$ |
| 142 | } |
| 143 | |
| 144 | return targetDeclaration; |
| 145 | } |
| 146 | |
| 147 | } |