2 * Copyright (c) 2017 Pantheon Technologies, s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.typedef;
10 import com.google.common.collect.ImmutableList;
11 import org.opendaylight.yangtools.yang.common.QName;
12 import org.opendaylight.yangtools.yang.model.api.Status;
13 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
14 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
15 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
16 import org.opendaylight.yangtools.yang.model.api.stmt.DefaultEffectiveStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.StatusEffectiveStatement;
18 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.TypedefEffectiveStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.TypedefStatement;
21 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseQNameStatementSupport;
22 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.EffectiveStatementMixins.EffectiveStatementWithFlags.FlagsBuilder;
23 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.EffectiveStmtUtils;
24 import org.opendaylight.yangtools.yang.parser.spi.TypeNamespace;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
29 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
31 public final class TypedefStatementSupport extends
32 BaseQNameStatementSupport<TypedefStatement, TypedefEffectiveStatement> {
33 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
34 YangStmtMapping.TYPEDEF)
35 .addOptional(YangStmtMapping.DEFAULT)
36 .addOptional(YangStmtMapping.DESCRIPTION)
37 .addOptional(YangStmtMapping.REFERENCE)
38 .addOptional(YangStmtMapping.STATUS)
39 .addMandatory(YangStmtMapping.TYPE)
40 .addOptional(YangStmtMapping.UNITS)
42 private static final TypedefStatementSupport INSTANCE = new TypedefStatementSupport();
44 private TypedefStatementSupport() {
45 super(YangStmtMapping.TYPEDEF);
48 public static TypedefStatementSupport getInstance() {
53 public QName parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
54 return StmtContextUtils.parseIdentifier(ctx, value);
58 public void onFullDefinitionDeclared(final Mutable<QName, TypedefStatement, TypedefEffectiveStatement> stmt) {
59 super.onFullDefinitionDeclared(stmt);
62 final Mutable<?, ?, ?> parent = stmt.getParentContext();
64 // Shadowing check: make sure we do not trample on pre-existing definitions. This catches sibling
65 // declarations and parent declarations which have already been declared.
66 checkConflict(parent, stmt);
67 parent.addContext(TypeNamespace.class, stmt.coerceStatementArgument(), stmt);
73 protected SubstatementValidator getSubstatementValidator() {
74 return SUBSTATEMENT_VALIDATOR;
78 protected TypedefStatement createDeclared(final StmtContext<QName, TypedefStatement, ?> ctx,
79 final ImmutableList<? extends DeclaredStatement<?>> substatements) {
81 return new RegularTypedefStatement(ctx.coerceStatementArgument(), substatements);
85 protected TypedefStatement createEmptyDeclared(final StmtContext<QName, TypedefStatement, ?> ctx) {
87 return new EmptyTypedefStatement(ctx.coerceStatementArgument());
91 protected TypedefEffectiveStatement createEffective(
92 final StmtContext<QName, TypedefStatement, TypedefEffectiveStatement> ctx,
93 final TypedefStatement declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
94 final TypeEffectiveStatement<?> typeEffectiveStmt = findFirstStatement(substatements,
95 TypeEffectiveStatement.class);
96 final String dflt = findFirstArgument(substatements, DefaultEffectiveStatement.class, null);
97 SourceException.throwIf(
98 EffectiveStmtUtils.hasDefaultValueMarkedWithIfFeature(ctx.getRootVersion(), typeEffectiveStmt, dflt),
99 ctx.getStatementSourceReference(),
100 "Typedef '%s' has default value '%s' marked with an if-feature statement.", ctx.getStatementArgument(),
103 return new TypedefEffectiveStatementImpl(declared, ctx.getSchemaPath().get(), computeFlags(substatements),
108 protected TypedefEffectiveStatement createEmptyEffective(
109 final StmtContext<QName, TypedefStatement, TypedefEffectiveStatement> ctx,
110 final TypedefStatement declared) {
111 throw new IllegalStateException("Refusing to create empty typedef for " + declared);
114 private static void checkConflict(final StmtContext<?, ?, ?> parent, final StmtContext<QName, ?, ?> stmt) {
115 final QName arg = stmt.coerceStatementArgument();
116 final StmtContext<?, ?, ?> existing = parent.getFromNamespace(TypeNamespace.class, arg);
117 // RFC7950 sections 5.5 and 6.2.1: identifiers must not be shadowed
118 SourceException.throwIf(existing != null, stmt.getStatementSourceReference(), "Duplicate name for typedef %s",
122 private static void checkDeclared(final StmtContext<QName, TypedefStatement, ?> ctx) {
123 // Shadowing check: make sure grandparent does not see a conflicting definition. This is required to ensure
124 // that a typedef in child scope does not shadow a typedef in parent scope which occurs later in the text.
125 final StmtContext<?, ?, ?> parent = ctx.getParentContext();
126 if (parent != null) {
127 final StmtContext<?, ?, ?> grandParent = parent.getParentContext();
128 if (grandParent != null) {
129 checkConflict(grandParent, ctx);
134 private static int computeFlags(final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
135 return new FlagsBuilder()
136 .setStatus(findFirstArgument(substatements, StatusEffectiveStatement.class, Status.CURRENT))