Remove ParserNamespace type captures
[yangtools.git] / parser / yang-parser-reactor / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / reactor / ReactorStmtCtx.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.parser.stmt.reactor;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Verify.verify;
12
13 import com.google.common.base.MoreObjects;
14 import com.google.common.base.MoreObjects.ToStringHelper;
15 import com.google.common.base.VerifyException;
16 import java.util.Collection;
17 import java.util.Map;
18 import java.util.Optional;
19 import java.util.Set;
20 import java.util.stream.Stream;
21 import org.eclipse.jdt.annotation.NonNull;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.opendaylight.yangtools.yang.common.Empty;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.common.QNameModule;
26 import org.opendaylight.yangtools.yang.common.YangVersion;
27 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
28 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
29 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
30 import org.opendaylight.yangtools.yang.model.api.stmt.AugmentStatement;
31 import org.opendaylight.yangtools.yang.model.api.stmt.ConfigEffectiveStatement;
32 import org.opendaylight.yangtools.yang.model.api.stmt.DeviationStatement;
33 import org.opendaylight.yangtools.yang.model.api.stmt.RefineStatement;
34 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
35 import org.opendaylight.yangtools.yang.model.api.stmt.UsesStatement;
36 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
38 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStatementState;
39 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
40 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
41 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
42 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
43 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase.ExecutionOrder;
44 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.Registry;
45 import org.opendaylight.yangtools.yang.parser.spi.meta.ParserNamespace;
46 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementFactory;
47 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
48 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
49 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
50 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
51 import org.opendaylight.yangtools.yang.parser.spi.source.SourceParserNamespaces;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54
55 /**
56  * Real "core" reactor statement implementation of {@link Mutable}, supporting basic reactor lifecycle.
57  *
58  * @param <A> Argument type
59  * @param <D> Declared Statement representation
60  * @param <E> Effective Statement representation
61  */
62 abstract class ReactorStmtCtx<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
63         extends NamespaceStorageSupport implements Mutable<A, D, E>, Current<A, D> {
64     private static final Logger LOG = LoggerFactory.getLogger(ReactorStmtCtx.class);
65
66     /**
67      * Substatement refcount tracking. This mechanics deals with retaining substatements for the purposes of
68      * instantiating their lazy copies in InferredStatementContext. It works in concert with {@link #buildEffective()}
69      * and {@link #declared()}: declared/effective statement views hold an implicit reference and refcount-based
70      * sweep is not activated until they are done (or this statement is not {@link #isSupportedToBuildEffective}).
71      *
72      * <p>
73      * Reference count is hierarchical in that parent references also pin down their child statements and do not allow
74      * them to be swept.
75      *
76      * <p>
77      * The counter's positive values are tracking incoming references via {@link #incRef()}/{@link #decRef()} methods.
78      * Once we transition to sweeping, this value becomes negative counting upwards to {@link #REFCOUNT_NONE} based on
79      * {@link #sweepOnChildDone()}. Once we reach that, we transition to {@link #REFCOUNT_SWEPT}.
80      */
81     private int refcount = REFCOUNT_NONE;
82     /**
83      * No outstanding references, this statement is a potential candidate for sweeping, provided it has populated its
84      * declared and effective views and {@link #parentRef} is known to be absent.
85      */
86     private static final int REFCOUNT_NONE = 0;
87     /**
88      * Reference count overflow or some other recoverable logic error. Do not rely on refcounts and do not sweep
89      * anything.
90      *
91      * <p>
92      * Note on value assignment:
93      * This allow our incRef() to naturally progress to being saturated. Others jump there directly.
94      * It also makes it  it impossible to observe {@code Interger.MAX_VALUE} children, which we take advantage of for
95      * {@link #REFCOUNT_SWEEPING}.
96      */
97     private static final int REFCOUNT_DEFUNCT = Integer.MAX_VALUE;
98     /**
99      * This statement is being actively swept. This is a transient value set when we are sweeping our children, so that
100      * we prevent re-entering this statement.
101      *
102      * <p>
103      * Note on value assignment:
104      * The value is lower than any legal child refcount due to {@link #REFCOUNT_DEFUNCT} while still being higher than
105      * {@link #REFCOUNT_SWEPT}.
106      */
107     private static final int REFCOUNT_SWEEPING = -Integer.MAX_VALUE;
108     /**
109      * This statement, along with its entire subtree has been swept and we positively know all our children have reached
110      * this state. We {@link #sweepNamespaces()} upon reaching this state.
111      *
112      * <p>
113      * Note on value assignment:
114      * This is the lowest value observable, making it easier on checking others on equality.
115      */
116     private static final int REFCOUNT_SWEPT = Integer.MIN_VALUE;
117
118     /**
119      * Effective instance built from this context. This field as dual types. Under normal circumstances in matches the
120      * {@link #buildEffective()} instance. If this context is reused, it can be inflated to {@link EffectiveInstances}
121      * and also act as a common instance reuse site.
122      */
123     private @Nullable Object effectiveInstance;
124
125     // Master flag controlling whether this context can yield an effective statement
126     // FIXME: investigate the mechanics that are being supported by this, as it would be beneficial if we can get rid
127     //        of this flag -- eliminating the initial alignment shadow used by below gap-filler fields.
128     private boolean isSupportedToBuildEffective = true;
129
130     // EffectiveConfig mapping
131     private static final int MASK_CONFIG                = 0x03;
132     private static final int HAVE_CONFIG                = 0x04;
133     // Effective instantiation mechanics for StatementContextBase: if this flag is set all substatements are known not
134     // change when instantiated. This includes context-independent statements as well as any statements which are
135     // ignored during copy instantiation.
136     private static final int ALL_INDEPENDENT            = 0x08;
137     // Flag bit assignments
138     private static final int IS_SUPPORTED_BY_FEATURES   = 0x10;
139     private static final int HAVE_SUPPORTED_BY_FEATURES = 0x20;
140     private static final int IS_IGNORE_IF_FEATURE       = 0x40;
141     private static final int HAVE_IGNORE_IF_FEATURE     = 0x80;
142     // Have-and-set flag constants, also used as masks
143     private static final int SET_SUPPORTED_BY_FEATURES  = HAVE_SUPPORTED_BY_FEATURES | IS_SUPPORTED_BY_FEATURES;
144     private static final int SET_IGNORE_IF_FEATURE      = HAVE_IGNORE_IF_FEATURE | IS_IGNORE_IF_FEATURE;
145
146     private static final EffectiveConfig[] EFFECTIVE_CONFIGS;
147
148     static {
149         final EffectiveConfig[] values = EffectiveConfig.values();
150         final int length = values.length;
151         verify(length == 4, "Unexpected EffectiveConfig cardinality %s", length);
152         EFFECTIVE_CONFIGS = values;
153     }
154
155     // Flags for use with SubstatementContext. These are hiding in the alignment shadow created by above boolean and
156     // hence improve memory layout.
157     private byte flags;
158
159     // Flag for use by AbstractResumedStatement, ReplicaStatementContext and InferredStatementContext. Each of them
160     // uses it to indicated a different condition. This is hiding in the alignment shadow created by
161     // 'isSupportedToBuildEffective'.
162     // FIXME: move this out once we have JDK15+
163     private boolean boolFlag;
164
165     ReactorStmtCtx() {
166         // Empty on purpose
167     }
168
169     ReactorStmtCtx(final ReactorStmtCtx<A, D, E> original) {
170         isSupportedToBuildEffective = original.isSupportedToBuildEffective;
171         boolFlag = original.boolFlag;
172         flags = original.flags;
173     }
174
175     // Used by ReplicaStatementContext only
176     ReactorStmtCtx(final ReactorStmtCtx<A, D, E> original, final Void dummy) {
177         boolFlag = isSupportedToBuildEffective = original.isSupportedToBuildEffective;
178         flags = original.flags;
179     }
180
181     //
182     //
183     // Common public interface contracts with simple mechanics. Please keep this in one logical block, so we do not end
184     // up mixing concerns and simple details with more complex logic.
185     //
186     //
187
188     @Override
189     public abstract StatementContextBase<?, ?, ?> getParentContext();
190
191     @Override
192     public abstract RootStatementContext<?, ?, ?> getRoot();
193
194     @Override
195     public abstract Collection<? extends @NonNull StatementContextBase<?, ?, ?>> mutableDeclaredSubstatements();
196
197     @Override
198     public final Registry getBehaviourRegistry() {
199         return getRoot().getBehaviourRegistryImpl();
200     }
201
202     @Override
203     public final YangVersion yangVersion() {
204         return getRoot().getRootVersionImpl();
205     }
206
207     @Override
208     public final void setRootVersion(final YangVersion version) {
209         getRoot().setRootVersionImpl(version);
210     }
211
212     @Override
213     public final void addRequiredSource(final SourceIdentifier dependency) {
214         getRoot().addRequiredSourceImpl(dependency);
215     }
216
217     @Override
218     public final void setRootIdentifier(final SourceIdentifier identifier) {
219         getRoot().setRootIdentifierImpl(identifier);
220     }
221
222     @Override
223     public final ModelActionBuilder newInferenceAction(final ModelProcessingPhase phase) {
224         return getRoot().getSourceContext().newInferenceAction(phase);
225     }
226
227     @Override
228     public final StatementDefinition publicDefinition() {
229         return definition().getPublicView();
230     }
231
232     @Override
233     public final Parent effectiveParent() {
234         return getParentContext();
235     }
236
237     @Override
238     public final QName moduleName() {
239         final var root = getRoot();
240         return QName.create(StmtContextUtils.getModuleQName(root), root.getRawArgument());
241     }
242
243     //
244     // In the next two methods we are looking for an effective statement. If we already have an effective instance,
245     // defer to it's implementation of the equivalent search. Otherwise we search our substatement contexts.
246     //
247     // Note that the search function is split, so as to allow InferredStatementContext to do its own thing first.
248     //
249
250     @Override
251     public final <X, Z extends EffectiveStatement<X, ?>> @NonNull Optional<X> findSubstatementArgument(
252             final @NonNull Class<Z> type) {
253         final E existing = effectiveInstance();
254         return existing != null ? existing.findFirstEffectiveSubstatementArgument(type)
255             : findSubstatementArgumentImpl(type);
256     }
257
258     @Override
259     public final boolean hasSubstatement(final @NonNull Class<? extends EffectiveStatement<?, ?>> type) {
260         final E existing = effectiveInstance();
261         return existing != null ? existing.findFirstEffectiveSubstatement(type).isPresent() : hasSubstatementImpl(type);
262     }
263
264     private E effectiveInstance() {
265         final Object existing = effectiveInstance;
266         return existing != null ? EffectiveInstances.local(existing) : null;
267     }
268
269     // Visible due to InferredStatementContext's override. At this point we do not have an effective instance available.
270     <X, Z extends EffectiveStatement<X, ?>> @NonNull Optional<X> findSubstatementArgumentImpl(
271             final @NonNull Class<Z> type) {
272         return allSubstatementsStream()
273             .filter(ctx -> ctx.isSupportedToBuildEffective() && ctx.producesEffective(type))
274             .findAny()
275             .map(ctx -> (X) ctx.getArgument());
276     }
277
278     // Visible due to InferredStatementContext's override. At this point we do not have an effective instance available.
279     boolean hasSubstatementImpl(final @NonNull Class<? extends EffectiveStatement<?, ?>> type) {
280         return allSubstatementsStream()
281             .anyMatch(ctx -> ctx.isSupportedToBuildEffective() && ctx.producesEffective(type));
282     }
283
284     @Override
285     @Deprecated
286     @SuppressWarnings("unchecked")
287     public final <Z extends EffectiveStatement<A, D>> StmtContext<A, D, Z> caerbannog() {
288         return (StmtContext<A, D, Z>) this;
289     }
290
291     @Override
292     public final String toString() {
293         return addToStringAttributes(MoreObjects.toStringHelper(this).omitNullValues()).toString();
294     }
295
296     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
297         return toStringHelper.add("definition", definition()).add("argument", argument()).add("refCount", refString());
298     }
299
300     private String refString() {
301         final int current = refcount;
302         return switch (current) {
303             case REFCOUNT_DEFUNCT -> "DEFUNCT";
304             case REFCOUNT_SWEEPING -> "SWEEPING";
305             case REFCOUNT_SWEPT -> "SWEPT";
306             default -> String.valueOf(refcount);
307         };
308     }
309
310     /**
311      * Return the context in which this statement was defined.
312      *
313      * @return statement definition
314      */
315     abstract @NonNull StatementDefinitionContext<A, D, E> definition();
316
317     //
318     //
319     // NamespaceStorageSupport/Mutable integration methods. Keep these together.
320     //
321     //
322
323     @Override
324     public final <K, V, T extends K> V namespaceItem(final ParserNamespace<K, V> type, final T key) {
325         return getBehaviourRegistry().getNamespaceBehaviour(type).getFrom(this, key);
326     }
327
328     @Override
329     public final <K, V> Map<K, V> namespace(final ParserNamespace<K, V> type) {
330         return getNamespace(type);
331     }
332
333     @Override
334     public final <K, V> Map<K, V> localNamespacePortion(final ParserNamespace<K, V> type) {
335         return getLocalNamespace(type);
336     }
337
338     @Override
339     protected <K, V> void onNamespaceElementAdded(final ParserNamespace<K, V> type, final K key, final V value) {
340         // definition().onNamespaceElementAdded(this, type, key, value);
341     }
342
343     /**
344      * Return the effective statement view of a copy operation. This method may return one of:
345      * <ul>
346      *   <li>{@code this}, when the effective view did not change</li>
347      *   <li>an InferredStatementContext, when there is a need for inference-equivalent copy</li>
348      *   <li>{@code null}, when the statement failed to materialize</li>
349      * </ul>
350      *
351      * @param parent Proposed new parent
352      * @param type Copy operation type
353      * @param targetModule New target module
354      * @return {@link ReactorStmtCtx} holding effective view
355      */
356     abstract @Nullable ReactorStmtCtx<?, ?, ?> asEffectiveChildOf(StatementContextBase<?, ?, ?> parent, CopyType type,
357         QNameModule targetModule);
358
359     @Override
360     public final ReplicaStatementContext<A, D, E> replicaAsChildOf(final Mutable<?, ?, ?> parent) {
361         checkArgument(parent instanceof StatementContextBase, "Unsupported parent %s", parent);
362         final var ret = replicaAsChildOf((StatementContextBase<?, ?, ?>) parent);
363         definition().onStatementAdded(ret);
364         return ret;
365     }
366
367     abstract @NonNull ReplicaStatementContext<A, D, E> replicaAsChildOf(@NonNull StatementContextBase<?, ?, ?> parent);
368
369     //
370     //
371     // Statement build entry points -- both public and package-private.
372     //
373     //
374
375     @Override
376     public final E buildEffective() {
377         final Object existing;
378         return (existing = effectiveInstance) != null ? EffectiveInstances.local(existing) : loadEffective();
379     }
380
381     private @NonNull E loadEffective() {
382         final E ret = createEffective();
383         effectiveInstance = ret;
384         // we have called createEffective(), substatements are no longer guarded by us. Let's see if we can clear up
385         // some residue.
386         if (refcount == REFCOUNT_NONE) {
387             sweepOnDecrement();
388         }
389         return ret;
390     }
391
392     abstract @NonNull E createEffective();
393
394     /**
395      * Routing of the request to build an effective statement from {@link InferredStatementContext} towards the original
396      * definition site. This is needed to pick the correct instantiation method: for declared statements we will
397      * eventually land in {@link AbstractResumedStatement}, for underclared statements that will be
398      * {@link UndeclaredStmtCtx}.
399      *
400      * @param factory Statement factory
401      * @param ctx Inferred statement context, i.e. where the effective statement is instantiated
402      * @return Built effective stateue
403      */
404     abstract @NonNull E createInferredEffective(@NonNull StatementFactory<A, D, E> factory,
405         @NonNull InferredStatementContext<A, D, E> ctx, Stream<? extends StmtContext<?, ?, ?>> declared,
406         Stream<? extends StmtContext<?, ?, ?>> effective);
407
408     /**
409      * Attach an effective copy of this statement. This essentially acts as a map, where we make a few assumptions:
410      * <ul>
411      *   <li>{@code copy} and {@code this} statement share {@link #getOriginalCtx()} if it exists</li>
412      *   <li>{@code copy} did not modify any statements relative to {@code this}</li>
413      * </ul>
414      *
415      * @param state effective statement state, acting as a lookup key
416      * @param stmt New copy to append
417      * @return {@code stmt} or a previously-created instances with the same {@code state}
418      */
419     @SuppressWarnings("unchecked")
420     final @NonNull E attachEffectiveCopy(final @NonNull EffectiveStatementState state, final @NonNull E stmt) {
421         final Object local = effectiveInstance;
422         final EffectiveInstances<E> instances;
423         if (local instanceof EffectiveInstances) {
424             instances = (EffectiveInstances<E>) local;
425         } else {
426             effectiveInstance = instances = new EffectiveInstances<>((E) local);
427         }
428         return instances.attachCopy(state, stmt);
429     }
430
431     /**
432      * Walk this statement's copy history and return the statement closest to original which has not had its effective
433      * statements modified. This statement and returned substatement logically have the same set of substatements, hence
434      * share substatement-derived state.
435      *
436      * @return Closest {@link ReactorStmtCtx} with equivalent effective substatements
437      */
438     abstract @NonNull ReactorStmtCtx<A, D, E> unmodifiedEffectiveSource();
439
440     @Override
441     public final ModelProcessingPhase getCompletedPhase() {
442         return ModelProcessingPhase.ofExecutionOrder(executionOrder());
443     }
444
445     abstract byte executionOrder();
446
447     /**
448      * Try to execute current {@link ModelProcessingPhase} of source parsing. If the phase has already been executed,
449      * this method does nothing. This must not be called with {@link ExecutionOrder#NULL}.
450      *
451      * @param phase to be executed (completed)
452      * @return true if phase was successfully completed
453      * @throws SourceException when an error occurred in source parsing
454      */
455     final boolean tryToCompletePhase(final byte executionOrder) {
456         return executionOrder() >= executionOrder || doTryToCompletePhase(executionOrder);
457     }
458
459     abstract boolean doTryToCompletePhase(byte targetOrder);
460
461     //
462     //
463     // Flags-based mechanics. These include public interfaces as well as all the crud we have lurking in our alignment
464     // shadow.
465     //
466     //
467
468     // Non-final form ImplicitStmtCtx
469     @Override
470     public boolean isSupportedToBuildEffective() {
471         return isSupportedToBuildEffective;
472     }
473
474     @Override
475     public final void setUnsupported() {
476         this.isSupportedToBuildEffective = false;
477     }
478
479     @Override
480     public final boolean isSupportedByFeatures() {
481         final int fl = flags & SET_SUPPORTED_BY_FEATURES;
482         if (fl != 0) {
483             return fl == SET_SUPPORTED_BY_FEATURES;
484         }
485         if (isIgnoringIfFeatures()) {
486             flags |= SET_SUPPORTED_BY_FEATURES;
487             return true;
488         }
489
490         /*
491          * If parent is supported, we need to check if-features statements of this context.
492          */
493         if (isParentSupportedByFeatures()) {
494             // If the set of supported features has not been provided, all features are supported by default.
495             final Set<QName> supportedFeatures = getFromNamespace(SourceParserNamespaces.SUPPORTED_FEATURES,
496                 Empty.value());
497             if (supportedFeatures == null || StmtContextUtils.checkFeatureSupport(this, supportedFeatures)) {
498                 flags |= SET_SUPPORTED_BY_FEATURES;
499                 return true;
500             }
501         }
502
503         // Either parent is not supported or this statement is not supported
504         flags |= HAVE_SUPPORTED_BY_FEATURES;
505         return false;
506     }
507
508     protected abstract boolean isParentSupportedByFeatures();
509
510     /**
511      * Config statements are not all that common which means we are performing a recursive search towards the root
512      * every time {@link #effectiveConfig()} is invoked. This is quite expensive because it causes a linear search
513      * for the (usually non-existent) config statement.
514      *
515      * <p>
516      * This method maintains a resolution cache, so once we have returned a result, we will keep on returning the same
517      * result without performing any lookups, solely to support {@link #effectiveConfig()}.
518      *
519      * <p>
520      * Note: use of this method implies that {@link #isIgnoringConfig()} is realized with
521      *       {@link #isIgnoringConfig(StatementContextBase)}.
522      */
523     final @NonNull EffectiveConfig effectiveConfig(final ReactorStmtCtx<?, ?, ?> parent) {
524         return (flags & HAVE_CONFIG) != 0 ? EFFECTIVE_CONFIGS[flags & MASK_CONFIG] : loadEffectiveConfig(parent);
525     }
526
527     private @NonNull EffectiveConfig loadEffectiveConfig(final ReactorStmtCtx<?, ?, ?> parent) {
528         final EffectiveConfig parentConfig = parent.effectiveConfig();
529
530         final EffectiveConfig myConfig;
531         if (parentConfig != EffectiveConfig.IGNORED && !definition().support().isIgnoringConfig()) {
532             final Optional<Boolean> optConfig = findSubstatementArgument(ConfigEffectiveStatement.class);
533             if (optConfig.isPresent()) {
534                 if (optConfig.orElseThrow()) {
535                     // Validity check: if parent is config=false this cannot be a config=true
536                     InferenceException.throwIf(parentConfig == EffectiveConfig.FALSE, this,
537                         "Parent node has config=false, this node must not be specifed as config=true");
538                     myConfig = EffectiveConfig.TRUE;
539                 } else {
540                     myConfig = EffectiveConfig.FALSE;
541                 }
542             } else {
543                 // If "config" statement is not specified, the default is the same as the parent's "config" value.
544                 myConfig = parentConfig;
545             }
546         } else {
547             myConfig = EffectiveConfig.IGNORED;
548         }
549
550         flags = (byte) (flags & ~MASK_CONFIG | HAVE_CONFIG | myConfig.ordinal());
551         return myConfig;
552     }
553
554     protected abstract boolean isIgnoringConfig();
555
556     /**
557      * This method maintains a resolution cache for ignore config, so once we have returned a result, we will
558      * keep on returning the same result without performing any lookups. Exists only to support
559      * {@link SubstatementContext#isIgnoringConfig()}.
560      *
561      * <p>
562      * Note: use of this method implies that {@link #isConfiguration()} is realized with
563      *       {@link #effectiveConfig(StatementContextBase)}.
564      */
565     final boolean isIgnoringConfig(final StatementContextBase<?, ?, ?> parent) {
566         return EffectiveConfig.IGNORED == effectiveConfig(parent);
567     }
568
569     protected abstract boolean isIgnoringIfFeatures();
570
571     /**
572      * This method maintains a resolution cache for ignore if-feature, so once we have returned a result, we will
573      * keep on returning the same result without performing any lookups. Exists only to support
574      * {@link SubstatementContext#isIgnoringIfFeatures()}.
575      */
576     final boolean isIgnoringIfFeatures(final StatementContextBase<?, ?, ?> parent) {
577         final int fl = flags & SET_IGNORE_IF_FEATURE;
578         if (fl != 0) {
579             return fl == SET_IGNORE_IF_FEATURE;
580         }
581         if (definition().support().isIgnoringIfFeatures() || parent.isIgnoringIfFeatures()) {
582             flags |= SET_IGNORE_IF_FEATURE;
583             return true;
584         }
585
586         flags |= HAVE_IGNORE_IF_FEATURE;
587         return false;
588     }
589
590     // These two exist only due to memory optimization, should live in AbstractResumedStatement.
591     final boolean fullyDefined() {
592         return boolFlag;
593     }
594
595     final void setFullyDefined() {
596         boolFlag = true;
597     }
598
599     // This exists only due to memory optimization, should live in ReplicaStatementContext. In this context the flag
600     // indicates the need to drop source's reference count when we are being swept.
601     final boolean haveSourceReference() {
602         return boolFlag;
603     }
604
605     // These three exist due to memory optimization, should live in InferredStatementContext. In this context the flag
606     // indicates whether or not this statement's substatement file was modified, i.e. it is not quite the same as the
607     // prototype's file.
608     final boolean isModified() {
609         return boolFlag;
610     }
611
612     final void setModified() {
613         boolFlag = true;
614     }
615
616     final void setUnmodified() {
617         boolFlag = false;
618     }
619
620     // These two exist only for StatementContextBase. Since we are squeezed for size, with only a single bit available
621     // in flags, we default to 'false' and only set the flag to true when we are absolutely sure -- and all other cases
622     // err on the side of caution by taking the time to evaluate each substatement separately.
623     final boolean allSubstatementsContextIndependent() {
624         return (flags & ALL_INDEPENDENT) != 0;
625     }
626
627     final void setAllSubstatementsContextIndependent() {
628         flags |= ALL_INDEPENDENT;
629     }
630
631     //
632     //
633     // Various functionality from AbstractTypeStatementSupport. This used to work on top of SchemaPath, now it still
634     // lives here. Ultimate future is either proper graduation or (more likely) move to AbstractTypeStatementSupport.
635     //
636     //
637
638     @Override
639     public final QName argumentAsTypeQName() {
640         // FIXME: This may yield illegal argument exceptions
641         return StmtContextUtils.qnameFromArgument(getOriginalCtx().orElse(this), getRawArgument());
642     }
643
644     @Override
645     public final QNameModule effectiveNamespace() {
646         if (StmtContextUtils.isUnknownStatement(this)) {
647             return publicDefinition().getStatementName().getModule();
648         }
649         if (producesDeclared(UsesStatement.class)) {
650             return coerceParent().effectiveNamespace();
651         }
652
653         final Object argument = argument();
654         if (argument instanceof QName qname) {
655             return qname.getModule();
656         }
657         if (argument instanceof String str) {
658             // FIXME: This may yield illegal argument exceptions
659             return StmtContextUtils.qnameFromArgument(getOriginalCtx().orElse(this), str).getModule();
660         }
661         if (argument instanceof SchemaNodeIdentifier sni
662                 && (producesDeclared(AugmentStatement.class) || producesDeclared(RefineStatement.class)
663                         || producesDeclared(DeviationStatement.class))) {
664             return sni.lastNodeIdentifier().getModule();
665         }
666
667         return coerceParent().effectiveNamespace();
668     }
669
670     private ReactorStmtCtx<?, ?, ?> coerceParent() {
671         return (ReactorStmtCtx<?, ?, ?>) coerceParentContext();
672     }
673
674     //
675     //
676     // Reference counting mechanics start. Please keep these methods in one block for clarity. Note this does not
677     // contribute to state visible outside of this package.
678     //
679     //
680
681     /**
682      * Local knowledge of {@link #refcount} values up to statement root. We use this field to prevent recursive lookups
683      * in {@link #noParentRefs(StatementContextBase)} -- once we discover a parent reference once, we keep that
684      * knowledge and update it when {@link #sweep()} is invoked.
685      */
686     private byte parentRef = PARENTREF_UNKNOWN;
687     private static final byte PARENTREF_UNKNOWN = -1;
688     private static final byte PARENTREF_ABSENT  = 0;
689     private static final byte PARENTREF_PRESENT = 1;
690
691     /**
692      * Acquire a reference on this context. As long as there is at least one reference outstanding,
693      * {@link #buildEffective()} will not result in {@link #effectiveSubstatements()} being discarded.
694      *
695      * @throws VerifyException if {@link #effectiveSubstatements()} has already been discarded
696      */
697     final void incRef() {
698         final int current = refcount;
699         verify(current >= REFCOUNT_NONE, "Attempted to access reference count of %s", this);
700         if (current != REFCOUNT_DEFUNCT) {
701             // Note: can end up becoming REFCOUNT_DEFUNCT on overflow
702             refcount = current + 1;
703         } else {
704             LOG.debug("Disabled refcount increment of {}", this);
705         }
706     }
707
708     /**
709      * Release a reference on this context. This call may result in {@link #effectiveSubstatements()} becoming
710      * unavailable.
711      */
712     final void decRef() {
713         final int current = refcount;
714         if (current == REFCOUNT_DEFUNCT) {
715             // no-op
716             LOG.debug("Disabled refcount decrement of {}", this);
717             return;
718         }
719         if (current <= REFCOUNT_NONE) {
720             // Underflow, become defunct
721             // FIXME: add a global 'warn once' flag
722             LOG.warn("Statement refcount underflow, reference counting disabled for {}", this, new Throwable());
723             refcount = REFCOUNT_DEFUNCT;
724             return;
725         }
726
727         refcount = current - 1;
728         LOG.trace("Refcount {} on {}", refcount, this);
729
730         if (refcount == REFCOUNT_NONE) {
731             lastDecRef();
732         }
733     }
734
735     /**
736      * Return {@code true} if this context has no outstanding references.
737      *
738      * @return True if this context has no outstanding references.
739      */
740     final boolean noRefs() {
741         final int local = refcount;
742         return local < REFCOUNT_NONE || local == REFCOUNT_NONE && noParentRef();
743     }
744
745     private void lastDecRef() {
746         if (noImplictRef()) {
747             // We are no longer guarded by effective instance
748             sweepOnDecrement();
749             return;
750         }
751
752         final byte prevRefs = parentRef;
753         if (prevRefs == PARENTREF_ABSENT) {
754             // We are the last reference towards root, any children who observed PARENTREF_PRESENT from us need to be
755             // updated
756             markNoParentRef();
757         } else if (prevRefs == PARENTREF_UNKNOWN) {
758             // Noone observed our parentRef, just update it
759             loadParentRefcount();
760         }
761     }
762
763     static final void markNoParentRef(final Collection<? extends ReactorStmtCtx<?, ?, ?>> substatements) {
764         for (ReactorStmtCtx<?, ?, ?> stmt : substatements) {
765             final byte prevRef = stmt.parentRef;
766             stmt.parentRef = PARENTREF_ABSENT;
767             if (prevRef == PARENTREF_PRESENT && stmt.refcount == REFCOUNT_NONE) {
768                 // Child thinks it is pinned down, update its perspective
769                 stmt.markNoParentRef();
770             }
771         }
772     }
773
774     abstract void markNoParentRef();
775
776     static final void sweep(final Collection<? extends ReactorStmtCtx<?, ?, ?>> substatements) {
777         for (ReactorStmtCtx<?, ?, ?> stmt : substatements) {
778             stmt.sweep();
779         }
780     }
781
782     /**
783      * Sweep this statement context as a result of {@link #sweepSubstatements()}, i.e. when parent is also being swept.
784      */
785     private void sweep() {
786         parentRef = PARENTREF_ABSENT;
787         if (refcount == REFCOUNT_NONE && noImplictRef()) {
788             LOG.trace("Releasing {}", this);
789             sweepState();
790         }
791     }
792
793     static final int countUnswept(final Collection<? extends ReactorStmtCtx<?, ?, ?>> substatements) {
794         int result = 0;
795         for (ReactorStmtCtx<?, ?, ?> stmt : substatements) {
796             if (stmt.refcount > REFCOUNT_NONE || !stmt.noImplictRef()) {
797                 result++;
798             }
799         }
800         return result;
801     }
802
803     /**
804      * Implementation-specific sweep action. This is expected to perform a recursive {@link #sweep(Collection)} on all
805      * {@link #declaredSubstatements()} and {@link #effectiveSubstatements()} and report the result of the sweep
806      * operation.
807      *
808      * <p>
809      * {@link #effectiveSubstatements()} as well as namespaces may become inoperable as a result of this operation.
810      *
811      * @return True if the entire tree has been completely swept, false otherwise.
812      */
813     abstract int sweepSubstatements();
814
815     // Called when this statement does not have an implicit reference and have reached REFCOUNT_NONE
816     private void sweepOnDecrement() {
817         LOG.trace("Sweeping on decrement {}", this);
818         if (noParentRef()) {
819             // No further parent references, sweep our state.
820             sweepState();
821         }
822
823         // Propagate towards parent if there is one
824         sweepParent();
825     }
826
827     private void sweepParent() {
828         final ReactorStmtCtx<?, ?, ?> parent = getParentContext();
829         if (parent != null) {
830             parent.sweepOnChildDecrement();
831         }
832     }
833
834     // Called from child when it has lost its final reference
835     private void sweepOnChildDecrement() {
836         if (isAwaitingChildren()) {
837             // We are a child for which our parent is waiting. Notify it and we are done.
838             sweepOnChildDone();
839             return;
840         }
841
842         // Check parent reference count
843         final int refs = refcount;
844         if (refs > REFCOUNT_NONE || refs <= REFCOUNT_SWEEPING || !noImplictRef()) {
845             // No-op
846             return;
847         }
848
849         // parent is potentially reclaimable
850         if (noParentRef()) {
851             LOG.trace("Cleanup {} of parent {}", refs, this);
852             if (sweepState()) {
853                 sweepParent();
854             }
855         }
856     }
857
858     private boolean noImplictRef() {
859         return effectiveInstance != null || !isSupportedToBuildEffective();
860     }
861
862     private boolean noParentRef() {
863         return parentRefcount() == PARENTREF_ABSENT;
864     }
865
866     private byte parentRefcount() {
867         final byte refs;
868         return (refs = parentRef) != PARENTREF_UNKNOWN ? refs : loadParentRefcount();
869     }
870
871     private byte loadParentRefcount() {
872         return parentRef = calculateParentRefcount();
873     }
874
875     private byte calculateParentRefcount() {
876         final ReactorStmtCtx<?, ?, ?> parent = getParentContext();
877         if (parent == null) {
878             return PARENTREF_ABSENT;
879         }
880
881         // A slight wrinkle here is that our machinery handles only PRESENT -> ABSENT invalidation and we can reach here
882         // while inference is still ongoing and hence we may not have a complete picture about existing references. We
883         // could therefore end up caching an ABSENT result and then that information becoming stale as a new reference
884         // is introduced.
885         if (parent.executionOrder() < ExecutionOrder.EFFECTIVE_MODEL) {
886             return PARENTREF_UNKNOWN;
887         }
888
889         // There are three possibilities:
890         // - REFCOUNT_NONE, in which case we need to search next parent
891         // - negative (< REFCOUNT_NONE), meaning parent is in some stage of sweeping, hence it does not have
892         //   a reference to us
893         // - positive (> REFCOUNT_NONE), meaning parent has an explicit refcount which is holding us down
894         final int refs = parent.refcount;
895         if (refs == REFCOUNT_NONE) {
896             return parent.parentRefcount();
897         }
898         return refs < REFCOUNT_NONE ? PARENTREF_ABSENT : PARENTREF_PRESENT;
899     }
900
901     private boolean isAwaitingChildren() {
902         return refcount > REFCOUNT_SWEEPING && refcount < REFCOUNT_NONE;
903     }
904
905     private void sweepOnChildDone() {
906         LOG.trace("Sweeping on child done {}", this);
907         final int current = refcount;
908         if (current >= REFCOUNT_NONE) {
909             // no-op, perhaps we want to handle some cases differently?
910             LOG.trace("Ignoring child sweep of {} for {}", this, current);
911             return;
912         }
913         verify(current != REFCOUNT_SWEPT, "Attempt to sweep a child of swept %s", this);
914
915         refcount = current + 1;
916         LOG.trace("Child refcount {}", refcount);
917         if (refcount == REFCOUNT_NONE) {
918             sweepDone();
919             final ReactorStmtCtx<?, ?, ?> parent = getParentContext();
920             LOG.trace("Propagating to parent {}", parent);
921             if (parent != null && parent.isAwaitingChildren()) {
922                 parent.sweepOnChildDone();
923             }
924         }
925     }
926
927     private void sweepDone() {
928         LOG.trace("Sweep done for {}", this);
929         refcount = REFCOUNT_SWEPT;
930         sweepNamespaces();
931     }
932
933     private boolean sweepState() {
934         refcount = REFCOUNT_SWEEPING;
935         final int childRefs = sweepSubstatements();
936         if (childRefs == 0) {
937             sweepDone();
938             return true;
939         }
940         if (childRefs < 0 || childRefs >= REFCOUNT_DEFUNCT) {
941             // FIXME: add a global 'warn once' flag
942             LOG.warn("Negative child refcount {} cannot be stored, reference counting disabled for {}", childRefs, this,
943                 new Throwable());
944             refcount = REFCOUNT_DEFUNCT;
945         } else {
946             LOG.trace("Still {} outstanding children of {}", childRefs, this);
947             refcount = -childRefs;
948         }
949         return false;
950     }
951 }