X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=parser%2Fyang-parser-reactor%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang%2Fparser%2Fstmt%2Freactor%2FStatementContextBase.java;h=d3ce39b693393568559d8179230e902ce683ebe1;hb=fe7aa9a567521a2aac3caf3f1076a263e1cdc6b4;hp=4663d2d5b15911fb3a6009731c6636b8c77fc94d;hpb=fc17f39c478a20907bec485d1b9b38fdb9eba0c8;p=yangtools.git diff --git a/parser/yang-parser-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/StatementContextBase.java b/parser/yang-parser-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/StatementContextBase.java index 4663d2d5b1..d3ce39b693 100644 --- a/parser/yang-parser-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/StatementContextBase.java +++ b/parser/yang-parser-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/StatementContextBase.java @@ -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; @@ -49,7 +49,7 @@ import org.opendaylight.yangtools.yang.parser.spi.meta.StatementNamespace; import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupport; import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupport.CopyPolicy; import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext; -import org.opendaylight.yangtools.yang.parser.spi.source.ImplicitSubstatement; +import org.opendaylight.yangtools.yang.parser.spi.meta.UndeclaredStatementFactory; import org.opendaylight.yangtools.yang.parser.spi.source.SourceException; import org.opendaylight.yangtools.yang.parser.stmt.reactor.NamespaceBehaviourWithListeners.KeyedValueAddedListener; import org.opendaylight.yangtools.yang.parser.stmt.reactor.NamespaceBehaviourWithListeners.PredicateValueAddedListener; @@ -149,6 +149,18 @@ abstract class StatementContextBase, E extends */ private byte executionOrder; + // 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 + // 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 original) { super(original); @@ -179,18 +191,12 @@ abstract class StatementContextBase, 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 @@ -312,13 +318,12 @@ abstract class StatementContextBase, E extends @Override public final , Z extends EffectiveStatement> - Mutable addEffectiveSubstatement(final StatementSupport support, final X arg) { - // FIXME: YANGTOOLS-652: This does not need to be a SubstatementContext, in can be a specialized - // StatementContextBase subclass. - final Mutable ret = new SubstatementContext<>(this, new StatementDefinitionContext<>(support), - ImplicitSubstatement.of(sourceReference()), arg); + Mutable createUndeclaredSubstatement(final StatementSupport 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; } @@ -333,6 +338,19 @@ abstract class StatementContextBase, 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 , Z extends EffectiveStatement> void finishDeclaration( + final UndeclaredStmtCtx substatement) { + substatement.definition().onDeclarationFinished(substatement, ModelProcessingPhase.FULL_DECLARATION); + } + @Override public final void addEffectiveSubstatements(final Collection> statements) { if (!statements.isEmpty()) { @@ -412,15 +430,7 @@ abstract class StatementContextBase, E extends return result; } - @NonNull E createEffective(final StatementFactory factory) { - return createEffective(factory, this); - } - - // Creates EffectiveStatement through full materialization - static , E extends EffectiveStatement> @NonNull E createEffective( - final StatementFactory factory, final StatementContextBase ctx) { - return factory.createEffective(ctx, ctx.streamDeclared(), ctx.streamEffective()); - } + abstract @NonNull E createEffective(@NonNull StatementFactory factory); /** * Return a stream of declared statements which can be built into an {@link EffectiveStatement}, as per @@ -720,7 +730,7 @@ abstract class StatementContextBase, E extends return Optional.ofNullable(copyAsChildOfImpl(parent, type, targetModule)); } - private ReactorStmtCtx copyAsChildOfImpl(final Mutable parent, final CopyType type, + private @Nullable ReactorStmtCtx copyAsChildOfImpl(final Mutable parent, final CopyType type, final QNameModule targetModule) { final StatementSupport support = definition.support(); final CopyPolicy policy = support.copyPolicy(); @@ -758,7 +768,7 @@ abstract class StatementContextBase, E extends return canReuseCurrent(copy) ? this : copy; } - private boolean canReuseCurrent(final ReactorStmtCtx copy) { + private boolean canReuseCurrent(final @NonNull ReactorStmtCtx 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()) @@ -786,26 +796,16 @@ abstract class StatementContextBase, E extends final InferredStatementContext copy; if (implicitParent.isPresent()) { - final StatementDefinitionContext def = new StatementDefinitionContext<>(implicitParent.get()); - result = new SubstatementContext(this, def, original.sourceReference(), original.rawArgument(), - original.argument(), 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; - } + result = new UndeclaredStmtCtx(this, implicitParent.orElseThrow(), original, 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); } @@ -837,16 +837,11 @@ abstract class StatementContextBase, E extends return original; } - final StatementDefinitionContext def = new StatementDefinitionContext<>(optImplicit.orElseThrow()); - final CopyType type = original.history().getLastOperation(); - checkArgument(original instanceof StatementContextBase, "Unsupported original %s", original); final var origBase = (StatementContextBase)original; - @SuppressWarnings({ "rawtypes", "unchecked"}) - final SubstatementContext result = new SubstatementContext(origBase.getParentContext(), def, - original.sourceReference(), original.rawArgument(), original.argument(), type); - + @SuppressWarnings({ "rawtypes", "unchecked" }) + final UndeclaredStmtCtx result = new UndeclaredStmtCtx(origBase, optImplicit.orElseThrow()); result.addEffectiveSubstatement(origBase.reparent(result)); result.setCompletedPhase(original.getCompletedPhase()); return result;