Remove ReactorStmtCtx.checkLocalNamespaceAllowed()
[yangtools.git] / yang / yang-parser-reactor / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / reactor / ReactorStmtCtx.java
index 7b9afcb96f4e886e4e5f79f22b73bb654108d165..f292bab919fbef8adf8685190b89a4aca9161226 100644 (file)
@@ -38,6 +38,7 @@ import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
+import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase.ExecutionOrder;
 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.Registry;
 import org.opendaylight.yangtools.yang.parser.spi.meta.ParserNamespace;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
@@ -112,6 +113,11 @@ abstract class ReactorStmtCtx<A, D extends DeclaredStatement<A>, E extends Effec
      */
     private static final int REFCOUNT_SWEPT = Integer.MIN_VALUE;
 
+    /**
+     * Effective instance built from this context. This field as dual types. Under normal circumstances in matches the
+     * {@link #buildEffective()} instance. If this context is reused, it can be inflated to {@link EffectiveInstances}
+     * and also act as a common instance reuse site.
+     */
     private @Nullable E effectiveInstance;
 
     // Master flag controlling whether this context can yield an effective statement
@@ -148,10 +154,11 @@ abstract class ReactorStmtCtx<A, D extends DeclaredStatement<A>, E extends Effec
     // hence improve memory layout.
     private byte flags;
 
-    // Flag for use with AbstractResumedStatement and ReplicateStatementContext. This is hiding in the alignment shadow
-    // created by above boolean
+    // Flag for use by AbstractResumedStatement, ReplicaStatementContext and InferredStatementContext. Each of them
+    // uses it to indicated a different condition. This is hiding in the alignment shadow created by
+    // 'isSupportedToBuildEffective'.
     // FIXME: move this out once we have JDK15+
-    private boolean fullyDefined;
+    private boolean boolFlag;
 
     // SchemaPath cache for use with SubstatementContext and InferredStatementContext. This hurts RootStatementContext
     // a bit in terms of size -- but those are only a few and SchemaPath is on its way out anyway.
@@ -164,13 +171,13 @@ abstract class ReactorStmtCtx<A, D extends DeclaredStatement<A>, E extends Effec
 
     ReactorStmtCtx(final ReactorStmtCtx<A, D, E> original) {
         isSupportedToBuildEffective = original.isSupportedToBuildEffective;
-        fullyDefined = original.fullyDefined;
+        boolFlag = original.boolFlag;
         flags = original.flags;
     }
 
     // Used by ReplicaStatementContext only
     ReactorStmtCtx(final ReactorStmtCtx<A, D, E> original, final Void dummy) {
-        fullyDefined = isSupportedToBuildEffective = original.isSupportedToBuildEffective;
+        boolFlag = isSupportedToBuildEffective = original.isSupportedToBuildEffective;
         flags = original.flags;
     }
 
@@ -256,16 +263,15 @@ abstract class ReactorStmtCtx<A, D extends DeclaredStatement<A>, E extends Effec
     @Override
     public final <X, Z extends EffectiveStatement<X, ?>> @NonNull Optional<X> findSubstatementArgument(
             final @NonNull Class<Z> type) {
-        final E local = effectiveInstance;
-        return local != null ? local.findFirstEffectiveSubstatementArgument(type)
+        final E existing = effectiveInstance;
+        return existing != null ? existing.findFirstEffectiveSubstatementArgument(type)
             : findSubstatementArgumentImpl(type);
     }
 
     @Override
     public final boolean hasSubstatement(final @NonNull Class<? extends EffectiveStatement<?, ?>> type) {
-        final E local = effectiveInstance;
-        return local != null ? local.findFirstEffectiveSubstatement(type).isPresent()
-            : hasSubstatementImpl(type);
+        final E existing = effectiveInstance;
+        return existing != null ? existing.findFirstEffectiveSubstatement(type).isPresent() : hasSubstatementImpl(type);
     }
 
     // Visible due to InferredStatementContext's override. At this point we do not have an effective instance available.
@@ -329,11 +335,6 @@ abstract class ReactorStmtCtx<A, D extends DeclaredStatement<A>, E extends Effec
         return getLocalNamespace(type);
     }
 
-    @Override
-    protected final void checkLocalNamespaceAllowed(final Class<? extends ParserNamespace<?, ?>> type) {
-        definition().checkNamespaceAllowed(type);
-    }
-
     @Override
     protected <K, V, N extends ParserNamespace<K, V>> void onNamespaceElementAdded(final Class<N> type, final K key,
             final V value) {
@@ -383,7 +384,8 @@ abstract class ReactorStmtCtx<A, D extends DeclaredStatement<A>, E extends Effec
         // we attempt to create effective statement:
         declared();
 
-        final E ret = effectiveInstance = createEffective();
+        final E ret = createEffective();
+        effectiveInstance = ret;
         // we have called createEffective(), substatements are no longer guarded by us. Let's see if we can clear up
         // some residue.
         if (refcount == REFCOUNT_NONE) {
@@ -394,19 +396,35 @@ abstract class ReactorStmtCtx<A, D extends DeclaredStatement<A>, E extends Effec
 
     abstract @NonNull E createEffective();
 
+    /**
+     * Walk this statement's copy history and return the statement closest to original which has not had its effective
+     * statements modified. This statement and returned substatement logically have the same set of substatements, hence
+     * share substatement-derived state.
+     *
+     * @return Closest {@link ReactorStmtCtx} with equivalent effective substatements
+     */
+    abstract @NonNull ReactorStmtCtx<A, D, E> unmodifiedEffectiveSource();
+
+    @Override
+    public final ModelProcessingPhase getCompletedPhase() {
+        return ModelProcessingPhase.ofExecutionOrder(executionOrder());
+    }
+
+    abstract byte executionOrder();
+
     /**
      * Try to execute current {@link ModelProcessingPhase} of source parsing. If the phase has already been executed,
-     * this method does nothing.
+     * this method does nothing. This must not be called with {@link ExecutionOrder#NULL}.
      *
      * @param phase to be executed (completed)
      * @return true if phase was successfully completed
      * @throws SourceException when an error occurred in source parsing
      */
-    final boolean tryToCompletePhase(final ModelProcessingPhase phase) {
-        return phase.isCompletedBy(getCompletedPhase()) || doTryToCompletePhase(phase);
+    final boolean tryToCompletePhase(final byte executionOrder) {
+        return executionOrder() >= executionOrder || doTryToCompletePhase(executionOrder);
     }
 
-    abstract boolean doTryToCompletePhase(ModelProcessingPhase phase);
+    abstract boolean doTryToCompletePhase(byte targetOrder);
 
     //
     //
@@ -538,16 +556,32 @@ abstract class ReactorStmtCtx<A, D extends DeclaredStatement<A>, E extends Effec
 
     // These two exist only due to memory optimization, should live in AbstractResumedStatement.
     final boolean fullyDefined() {
-        return fullyDefined;
+        return boolFlag;
     }
 
     final void setFullyDefined() {
-        fullyDefined = true;
+        boolFlag = true;
     }
 
-    // This exists only due to memory optimization, should live in ReplicaStatementContext.
+    // This exists only due to memory optimization, should live in ReplicaStatementContext. In this context the flag
+    // indicates the need to drop source's reference count when we are being swept.
     final boolean haveSourceReference() {
-        return fullyDefined;
+        return boolFlag;
+    }
+
+    // These three exist due to memory optimization, should live in InferredStatementContext. In this context the flag
+    // indicates whether or not this statement's substatement file was modified, i.e. it is not quite the same as the
+    // prototype's file.
+    final boolean isModified() {
+        return boolFlag;
+    }
+
+    final void setModified() {
+        boolFlag = true;
+    }
+
+    final void setUnmodified() {
+        boolFlag = false;
     }
 
     // These two exist only for StatementContextBase. Since we are squeezed for size, with only a single bit available
@@ -570,9 +604,7 @@ abstract class ReactorStmtCtx<A, D extends DeclaredStatement<A>, E extends Effec
 
     @Override
     public final QName argumentAsTypeQName() {
-        final Object argument = argument();
-        verify(argument instanceof String, "Unexpected argument %s", argument);
-        return interpretAsQName((String) argument);
+        return interpretAsQName(getRawArgument());
     }
 
     @Override