Deprecate StmtContext.getSchemaPath()
[yangtools.git] / yang / yang-parser-reactor / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / reactor / InferredStatementContext.java
index f6245602b23c70622843563c0b47673c676f6e21..03c6a49f5a98cac3a3abdb7efb0a5a012c789243 100644 (file)
@@ -43,6 +43,7 @@ final class InferredStatementContext<A, D extends DeclaredStatement<A>, E extend
 
     private final @NonNull StatementContextBase<A, D, E> prototype;
     private final @NonNull StatementContextBase<?, ?, ?> parent;
+    private final @NonNull StmtContext<A, D, E> originalCtx;
     private final @NonNull CopyType childCopyType;
     private final QNameModule targetModule;
     private final A argument;
@@ -54,6 +55,7 @@ final class InferredStatementContext<A, D extends DeclaredStatement<A>, E extend
         this.childCopyType = original.childCopyType;
         this.targetModule = original.targetModule;
         this.prototype = original.prototype;
+        this.originalCtx = original.originalCtx;
         this.argument = original.argument;
     }
 
@@ -66,6 +68,7 @@ final class InferredStatementContext<A, D extends DeclaredStatement<A>, E extend
                 : prototype.definition().adaptArgumentValue(prototype, targetModule);
         this.childCopyType = requireNonNull(childCopyType);
         this.targetModule = targetModule;
+        this.originalCtx = prototype.getOriginalCtx().orElse(prototype);
 
         // FIXME: YANGTOOLS-784: instantiate these lazily
         addEffectiveSubstatements(createEffective());
@@ -90,30 +93,43 @@ final class InferredStatementContext<A, D extends DeclaredStatement<A>, E extend
 
     @Override
     public StatementSourceReference getStatementSourceReference() {
-        return prototype.getStatementSourceReference();
+        return originalCtx.getStatementSourceReference();
     }
 
     @Override
     public String rawStatementArgument() {
-        return prototype.rawStatementArgument();
+        return originalCtx.rawStatementArgument();
     }
 
     @Override
-    public Optional<StmtContext<?, ?, ?>> getOriginalCtx() {
-        final Optional<StmtContext<?, ?, ?>> orig = prototype.getOriginalCtx();
-        return orig.isPresent() ? orig : Optional.of(prototype);
+    public Optional<StmtContext<A, D, E>> getOriginalCtx() {
+        return Optional.of(originalCtx);
     }
 
     @Override
-    public Optional<? extends StmtContext<?, ?, ?>> getPreviousCopyCtx() {
+    public Optional<StmtContext<A, D, E>> getPreviousCopyCtx() {
         return Optional.of(prototype);
     }
 
+    @Override
+    public D buildDeclared() {
+        /*
+         * Share original instance of declared statement between all effective statements which have been copied or
+         * derived from this original declared statement.
+         */
+        return originalCtx.buildDeclared();
+    }
+
     @Override
     InferredStatementContext<A, D, E> reparent(final StatementContextBase<?, ?, ?> newParent) {
         return new InferredStatementContext<>(this, newParent);
     }
 
+    @Override
+    boolean hasEmptySubstatements() {
+        return hasEmptyEffectiveSubstatements();
+    }
+
     // Instantiate this statement's effective substatements. Note this method has side-effects in namespaces and overall
     // BuildGlobalContext, hence it must be called at most once.
     private List<Mutable<?, ?, ?>> createEffective() {
@@ -138,15 +154,11 @@ final class InferredStatementContext<A, D extends DeclaredStatement<A>, E extend
     // FIXME: This is messy and is probably wrong in some corner case. Even if it is correct, the way how it is correct
     //        relies on hard-coded maps. At the end of the day, the logic needs to be controlled by statement's
     //        StatementSupport.
-    // FIXME: YANGTOOLS-652: these maps look very much like those in UsesStatementImpl
+    // FIXME: YANGTOOLS-652: this map looks very much like UsesStatementSupport.TOP_REUSED_DEF_SET
     private static final ImmutableSet<YangStmtMapping> REUSED_DEF_SET = ImmutableSet.of(
         YangStmtMapping.TYPE,
         YangStmtMapping.TYPEDEF,
         YangStmtMapping.USES);
-    private static final ImmutableSet<YangStmtMapping> NOCOPY_FROM_GROUPING_SET = ImmutableSet.of(
-        YangStmtMapping.DESCRIPTION,
-        YangStmtMapping.REFERENCE,
-        YangStmtMapping.STATUS);
 
     private void copySubstatement(final Mutable<?, ?, ?> substatement, final Collection<Mutable<?, ?, ?>> buffer) {
         final StatementDefinition def = substatement.getPublicDefinition();
@@ -157,22 +169,8 @@ final class InferredStatementContext<A, D extends DeclaredStatement<A>, E extend
             buffer.add(substatement);
             return;
         }
-        // FIXME: YANGTOOLS-652: formerly known as "needToCopyByUses" (note inverted check, though)
-        if (NOCOPY_FROM_GROUPING_SET.contains(def)) {
-            // This is to say: if parent of source context is a grouping, ignore this statement.
-            if (YangStmtMapping.GROUPING.equals(substatement.coerceParentContext().getPublicDefinition())) {
-                LOG.debug("Skipping grouping statement {}", substatement);
-                return;
-            }
-        }
 
-        // FIXME: YANGTOOLS-694: we are forcing a copy here, hence even statements not affected by parent, copyType
-        //                       or targetModule (and don't forget its substatements!). This really should be a callout
-        //                       to StatementSupport. Note if that callout is allowed to return an Optional, it can
-        //                       take care at least of the 'grouping from uses' case above.
-        final Mutable<?, ?, ?> copy = childCopyOf(substatement, childCopyType, targetModule);
-        LOG.debug("Copying substatement {} for {} as {}", substatement, this, copy);
-        buffer.add(copy);
+        substatement.copyAsChildOf(this, childCopyType, targetModule).ifPresent(buffer::add);
     }
 
     // Statement copy mess ends here
@@ -184,6 +182,7 @@ final class InferredStatementContext<A, D extends DeclaredStatement<A>, E extend
      * properly updated there.
      */
     @Override
+    @Deprecated
     public Optional<SchemaPath> getSchemaPath() {
         return substatementGetSchemaPath();
     }