Cleanup unrecognized statement wrapping
[yangtools.git] / yang / yang-parser-reactor / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / reactor / InferredStatementContext.java
index a3d5e9d4592a7016f03c23818d8925f07467db70..8c2c3db91512708aa24a7e826d16bd12718c1fc1 100644 (file)
@@ -34,7 +34,6 @@ import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStatement;
 import org.opendaylight.yangtools.yang.parser.spi.SchemaTreeNamespace;
-import org.opendaylight.yangtools.yang.parser.spi.meta.CopyHistory;
 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.OnDemandSchemaTreeStorageNode;
@@ -88,6 +87,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;
+    // TODO: consider encoding this in StatementContextBase fields, there should be plenty of room
     private final @NonNull CopyType childCopyType;
     private final QNameModule targetModule;
     private final A argument;
@@ -118,7 +118,7 @@ final class InferredStatementContext<A, D extends DeclaredStatement<A>, E extend
 
     InferredStatementContext(final StatementContextBase<?, ?, ?> parent, final StatementContextBase<A, D, E> prototype,
             final CopyType myCopyType, final CopyType childCopyType, final QNameModule targetModule) {
-        super(prototype.definition(), CopyHistory.of(myCopyType, prototype.history()));
+        super(prototype, myCopyType);
         this.parent = requireNonNull(parent);
         this.prototype = requireNonNull(prototype);
         this.argument = targetModule == null ? prototype.argument()
@@ -224,10 +224,12 @@ final class InferredStatementContext<A, D extends DeclaredStatement<A>, E extend
 
         // First check if we can reuse the entire prototype
         if (!factory.canReuseCurrent(this, prototype, origSubstatements)) {
+            // FIXME: YANGTOOLS-1214: deduplicate this return
             return tryToReuseSubstatements(factory, origEffective);
         }
 
-        // No substatements to deal with, we can freely reuse the original
+        // We can reuse this statement let's see if all statements agree...
+        // ... no substatements to deal with, we can freely reuse the original
         if (origSubstatements.isEmpty()) {
             LOG.debug("Reusing empty: {}", origEffective);
             substatements = ImmutableList.of();
@@ -235,7 +237,15 @@ final class InferredStatementContext<A, D extends DeclaredStatement<A>, E extend
             return origEffective;
         }
 
-        // We can reuse this statement let's see if all the statements agree
+        // ... all are context independent, reuse the original
+        if (allSubstatementsContextIndependent()) {
+            LOG.debug("Reusing context-independent: {}", origEffective);
+            substatements = noRefs() ? REUSED_SUBSTATEMENTS : reusePrototypeReplicas();
+            prototype.decRef();
+            return origEffective;
+        }
+
+        // ... copy-sensitive check
         final List<EffectiveCopy> declCopy = prototype.streamDeclared()
             .map(sub -> effectiveCopy((ReactorStmtCtx<?, ?, ?>) sub))
             .filter(Objects::nonNull)
@@ -245,6 +255,7 @@ final class InferredStatementContext<A, D extends DeclaredStatement<A>, E extend
             .filter(Objects::nonNull)
             .collect(Collectors.toUnmodifiableList());
 
+        // ... are any copy-sensitive?
         if (allReused(declCopy) && allReused(effCopy)) {
             LOG.debug("Reusing after substatement check: {}", origEffective);
             substatements = noRefs() ? REUSED_SUBSTATEMENTS
@@ -254,6 +265,7 @@ final class InferredStatementContext<A, D extends DeclaredStatement<A>, E extend
             return origEffective;
         }
 
+        // *sigh*, ok, heavy lifting through a shallow copy
         final List<ReactorStmtCtx<?, ?, ?>> declared = declCopy.stream()
             .map(copy -> copy.toChildContext(this))
             .collect(ImmutableList.toImmutableList());
@@ -265,6 +277,7 @@ final class InferredStatementContext<A, D extends DeclaredStatement<A>, E extend
         prototype.decRef();
 
         // Values are the effective copies, hence this efficiently deals with recursion.
+        // FIXME: YANGTOOLS-1214: deduplicate this return
         return factory.createEffective(this, declared.stream(), effective.stream());
     }
 
@@ -279,6 +292,9 @@ final class InferredStatementContext<A, D extends DeclaredStatement<A>, E extend
         // Fall back to full instantiation, which populates our substatements. Then check if we should be reusing
         // the substatement list, as this operation turned out to not affect them.
         final E effective = super.createEffective(factory);
+        // Since we have forced instantiation to deal with this case, we also need to reset the 'modified' flag
+        setUnmodified();
+
         if (sameSubstatements(original.effectiveSubstatements(), effective)) {
             LOG.debug("Reusing unchanged substatements of: {}", prototype);
             return factory.copyEffective(this, original);
@@ -287,8 +303,7 @@ final class InferredStatementContext<A, D extends DeclaredStatement<A>, E extend
     }
 
     private List<ReactorStmtCtx<?, ?, ?>> reusePrototypeReplicas() {
-        return reusePrototypeReplicas(Streams.concat(
-            prototype.streamDeclared(), prototype.streamEffective()));
+        return reusePrototypeReplicas(Streams.concat(prototype.streamDeclared(), prototype.streamEffective()));
     }
 
     private List<ReactorStmtCtx<?, ?, ?>> reusePrototypeReplicas(final Stream<StmtContext<?, ?, ?>> stream) {
@@ -326,6 +341,11 @@ final class InferredStatementContext<A, D extends DeclaredStatement<A>, E extend
         return entries.stream().allMatch(EffectiveCopy::isReused);
     }
 
+    @Override
+    ReactorStmtCtx<A, D, E> unmodifiedEffectiveSource() {
+        return isModified() ? this : prototype.unmodifiedEffectiveSource();
+    }
+
     @Override
     boolean hasEmptySubstatements() {
         if (substatements == null) {
@@ -506,6 +526,7 @@ final class InferredStatementContext<A, D extends DeclaredStatement<A>, E extend
         final List<ReactorStmtCtx<?, ?, ?>> ret = beforeAddEffectiveStatementUnsafe(ImmutableList.of(), buffer.size());
         ret.addAll((Collection) buffer);
         substatements = ret;
+        setModified();
 
         prototype.decRef();
         return ret;
@@ -552,6 +573,7 @@ final class InferredStatementContext<A, D extends DeclaredStatement<A>, E extend
             // resizing operation.
             materializedSchemaTree = new HashMap<>(4);
             substatements = materializedSchemaTree;
+            setModified();
         } else {
             verify(substatements instanceof HashMap, "Unexpected substatements %s", substatements);
             materializedSchemaTree = castMaterialized(substatements);