Return ReactorStmtCtx from ensureCompletedPhase()
[yangtools.git] / parser / yang-parser-reactor / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / reactor / StatementContextBase.java
index 438c6e6365d87ad367c189b72323bcb47ccdfbf8..3ee005a363e0105327c909a1c9b33f2f6967c186 100644 (file)
@@ -13,7 +13,6 @@ import static com.google.common.base.Verify.verify;
 import static com.google.common.base.Verify.verifyNotNull;
 import static java.util.Objects.requireNonNull;
 
-import com.google.common.base.VerifyException;
 import com.google.common.collect.ImmutableCollection;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMultimap;
@@ -150,12 +149,6 @@ abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E extends
      */
     private byte executionOrder;
 
-    /**
-     * This field should live in AbstractResumedStatement, but is placed here for memory efficiency to squat in the
-     * alignment shadow of {@link #bitsAight} and {@link #executionOrder}.
-     */
-    private boolean implicitDeclaredFlag;
-
     // TODO: we a single byte of alignment shadow left, we should think how we can use it to cache information we build
     //       during InferredStatementContext.tryToReusePrototype(). We usually end up being routed to
     //       copyAsChildOfImpl() -- which performs an eager instantiation and checks for changes afterwards. We should
@@ -198,18 +191,12 @@ abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E extends
     }
 
     private static byte historyFlags(final CopyType copyType) {
-        switch (copyType) {
-            case ADDED_BY_AUGMENTATION:
-                return COPY_ADDED_BY_AUGMENTATION;
-            case ADDED_BY_USES:
-                return COPY_ADDED_BY_USES;
-            case ADDED_BY_USES_AUGMENTATION:
-                return COPY_ADDED_BY_AUGMENTATION | COPY_ADDED_BY_USES;
-            case ORIGINAL:
-                return COPY_ORIGINAL;
-            default:
-                throw new VerifyException("Unhandled type " + copyType);
-        }
+        return switch (copyType) {
+            case ADDED_BY_AUGMENTATION -> COPY_ADDED_BY_AUGMENTATION;
+            case ADDED_BY_USES -> COPY_ADDED_BY_USES;
+            case ADDED_BY_USES_AUGMENTATION -> COPY_ADDED_BY_AUGMENTATION | COPY_ADDED_BY_USES;
+            case ORIGINAL -> COPY_ORIGINAL;
+        };
     }
 
     @Override
@@ -342,10 +329,8 @@ abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E extends
 
     final List<ReactorStmtCtx<?, ?, ?>> addEffectiveSubstatement(final List<ReactorStmtCtx<?, ?, ?>> effective,
             final Mutable<?, ?, ?> substatement) {
-        verifyStatement(substatement);
-
+        final ReactorStmtCtx<?, ?, ?> stmt = verifyStatement(substatement);
         final List<ReactorStmtCtx<?, ?, ?>> resized = beforeAddEffectiveStatement(effective, 1);
-        final ReactorStmtCtx<?, ?, ?> stmt = (ReactorStmtCtx<?, ?, ?>) substatement;
         ensureCompletedExecution(stmt);
         resized.add(stmt);
         return resized;
@@ -392,9 +377,10 @@ abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E extends
     abstract Iterator<ReactorStmtCtx<?, ?, ?>> effectiveChildrenToComplete();
 
     // exposed for InferredStatementContext only
-    final void ensureCompletedPhase(final Mutable<?, ?, ?> stmt) {
-        verifyStatement(stmt);
-        ensureCompletedExecution((ReactorStmtCtx<?, ?, ?>) stmt);
+    final ReactorStmtCtx<?, ?, ?> ensureCompletedPhase(final Mutable<?, ?, ?> stmt) {
+        final var ret = verifyStatement(stmt);
+        ensureCompletedExecution(ret);
+        return ret;
     }
 
     // Make sure target statement has transitioned at least to our phase (if we have one). This method is just before we
@@ -410,8 +396,9 @@ abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E extends
         verify(stmt.tryToCompletePhase(executionOrder), "Statement %s cannot complete phase %s", stmt, executionOrder);
     }
 
-    private static void verifyStatement(final Mutable<?, ?, ?> stmt) {
+    private static ReactorStmtCtx<?, ?, ?> verifyStatement(final Mutable<?, ?, ?> stmt) {
         verify(stmt instanceof ReactorStmtCtx, "Unexpected statement %s", stmt);
+        return (ReactorStmtCtx<?, ?, ?>) stmt;
     }
 
     private List<ReactorStmtCtx<?, ?, ?>> beforeAddEffectiveStatement(final List<ReactorStmtCtx<?, ?, ?>> effective,
@@ -445,19 +432,6 @@ abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E extends
 
     abstract @NonNull E createEffective(@NonNull StatementFactory<A, D, E> factory);
 
-    /**
-     * Routing of the request to build an effective statement from {@link InferredStatementContext} towards the original
-     * definition site. This is needed to pick the correct instantiation method: for declared statements we will
-     * eventually land in {@link AbstractResumedStatement}, for underclared statements that will be
-     * {@link UndeclaredStmtCtx}.
-     *
-     * @param factory Statement factory
-     * @param ctx Inferred statement context, i.e. where the effective statement is instantiated
-     * @return Built effective stateue
-     */
-    abstract @NonNull E createInferredEffective(@NonNull StatementFactory<A, D, E> factory,
-        @NonNull InferredStatementContext<A, D, E> ctx);
-
     /**
      * Return a stream of declared statements which can be built into an {@link EffectiveStatement}, as per
      * {@link StmtContext#buildEffective()} contract.
@@ -790,7 +764,7 @@ abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E extends
             return null;
         }
 
-        parent.ensureCompletedPhase(copy);
+        parent.ensureCompletedExecution(copy);
         return canReuseCurrent(copy) ? this : copy;
     }
 
@@ -824,22 +798,14 @@ abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E extends
         if (implicitParent.isPresent()) {
             result = new UndeclaredStmtCtx(this, implicitParent.orElseThrow(), original, type);
 
-            final CopyType childCopyType;
-            switch (type) {
-                case ADDED_BY_AUGMENTATION:
-                    childCopyType = CopyType.ORIGINAL;
-                    break;
-                case ADDED_BY_USES_AUGMENTATION:
-                    childCopyType = CopyType.ADDED_BY_USES;
-                    break;
-                case ADDED_BY_USES:
-                case ORIGINAL:
-                default:
-                    childCopyType = type;
-            }
-
+            final CopyType childCopyType = switch (type) {
+                case ADDED_BY_AUGMENTATION -> CopyType.ORIGINAL;
+                case ADDED_BY_USES_AUGMENTATION -> CopyType.ADDED_BY_USES;
+                case ADDED_BY_USES, ORIGINAL -> type;
+            };
             copy = new InferredStatementContext<>(result, original, childCopyType, type, targetModule);
             result.addEffectiveSubstatement(copy);
+            result.definition.onStatementAdded(result);
         } else {
             result = copy = new InferredStatementContext<>(this, original, type, type, targetModule);
         }
@@ -889,13 +855,4 @@ abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E extends
      * @return True if {@link #allSubstatements()} and {@link #allSubstatementsStream()} would return an empty stream.
      */
     abstract boolean hasEmptySubstatements();
-
-    // Note: these two are exposed for AbstractResumedStatement only
-    final boolean getImplicitDeclaredFlag() {
-        return implicitDeclaredFlag;
-    }
-
-    final void setImplicitDeclaredFlag() {
-        implicitDeclaredFlag = true;
-    }
 }