Shortcut statement completion
[yangtools.git] / parser / yang-parser-reactor / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / reactor / StatementContextBase.java
index f5f85c37c412a94cd80f795e8546c8a84e5548c4..570e7a54f6a82b53c9ca5fb308a177989c848e6a 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;
@@ -30,6 +29,7 @@ import java.util.Map.Entry;
 import java.util.Optional;
 import java.util.stream.Stream;
 import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.yangtools.yang.common.QNameModule;
 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
@@ -149,14 +149,17 @@ 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 buildEffective()
+    //       during InferredStatementContext.tryToReusePrototype(). We usually end up being routed to
+    //       copyAsChildOfImpl() -- which performs an eager instantiation and checks for changes afterwards. We should
+    //       be able to capture how parent scope affects the copy in a few bits. If we can do that, than we can reap
+    //       the benefits by just examining new parent context and old parent context contribution to the state. If
+    //       their impact is the same, we can skip instantiation of statements and directly reuse them (individually,
+    //       or as a complete file).
+    //
+    //       Whatever we end up tracking, we need to track two views of that -- for the statement itself
+    //       (sans substatements) and a summary of substatements. I think it should be possible to get this working
+    //       with 2x5bits -- we have up to 15 mutable bits available if we share the field with implicitDeclaredFlag.
 
     // Copy constructor used by subclasses to implement reparent()
     StatementContextBase(final StatementContextBase<A, D, E> original) {
@@ -188,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
@@ -321,13 +318,12 @@ abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E extends
 
     @Override
     public final <X, Y extends DeclaredStatement<X>, Z extends EffectiveStatement<X, Y>>
-            Mutable<X, Y, Z> addUndeclaredSubstatement(final StatementSupport<X, Y, Z> support, final X arg) {
+            Mutable<X, Y, Z> createUndeclaredSubstatement(final StatementSupport<X, Y, Z> support, final X arg) {
         requireNonNull(support);
         checkArgument(support instanceof UndeclaredStatementFactory, "Unsupported statement support %s", support);
 
         final var ret = new UndeclaredStmtCtx<>(this, support, arg);
         support.onStatementAdded(ret);
-        addEffectiveSubstatement(ret);
         return ret;
     }
 
@@ -342,6 +338,19 @@ abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E extends
         return resized;
     }
 
+    static final void afterAddEffectiveSubstatement(final Mutable<?, ?, ?> substatement) {
+        // Undeclared statements still need to have 'onDeclarationFinished()' triggered
+        if (substatement instanceof UndeclaredStmtCtx) {
+            finishDeclaration((UndeclaredStmtCtx<?, ?, ?>) substatement);
+        }
+    }
+
+    // Split out to keep generics working without a warning
+    private static <X, Y extends DeclaredStatement<X>, Z extends EffectiveStatement<X, Y>> void finishDeclaration(
+            final UndeclaredStmtCtx<X, Y, Z> substatement) {
+        substatement.definition().onDeclarationFinished(substatement, ModelProcessingPhase.FULL_DECLARATION);
+    }
+
     @Override
     public final void addEffectiveSubstatements(final Collection<? extends Mutable<?, ?, ?>> statements) {
         if (!statements.isEmpty()) {
@@ -423,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.
@@ -734,7 +730,7 @@ abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E extends
         return Optional.ofNullable(copyAsChildOfImpl(parent, type, targetModule));
     }
 
-    private ReactorStmtCtx<A, D, E> copyAsChildOfImpl(final Mutable<?, ?, ?> parent, final CopyType type,
+    private @Nullable ReactorStmtCtx<A, D, E> copyAsChildOfImpl(final Mutable<?, ?, ?> parent, final CopyType type,
             final QNameModule targetModule) {
         final StatementSupport<A, D, E> support = definition.support();
         final CopyPolicy policy = support.copyPolicy();
@@ -768,11 +764,11 @@ abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E extends
             return null;
         }
 
-        parent.ensureCompletedPhase(copy);
+        parent.ensureCompletedExecution(copy);
         return canReuseCurrent(copy) ? this : copy;
     }
 
-    private boolean canReuseCurrent(final ReactorStmtCtx<A, D, E> copy) {
+    private boolean canReuseCurrent(final @NonNull ReactorStmtCtx<A, D, E> copy) {
         // Defer to statement factory to see if we can reuse this object. If we can and have only context-independent
         // substatements we can reuse the object. More complex cases are handled indirectly via the copy.
         return definition.getFactory().canReuseCurrent(copy, this, buildEffective().effectiveSubstatements())
@@ -802,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);
         }
@@ -867,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;
-    }
 }