769830433f7ff6ebd94c6e29b5ef573818c9cdbc
[mdsal.git] / binding / mdsal-binding-generator / src / main / java / org / opendaylight / mdsal / binding / generator / impl / reactor / AbstractCompositeGenerator.java
1 /*
2  * Copyright (c) 2021 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.mdsal.binding.generator.impl.reactor;
9
10 import static com.google.common.base.Verify.verify;
11 import static com.google.common.base.Verify.verifyNotNull;
12 import static java.util.Objects.requireNonNull;
13
14 import java.util.ArrayList;
15 import java.util.Iterator;
16 import java.util.List;
17 import java.util.stream.Collectors;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.opendaylight.mdsal.binding.model.api.Enumeration;
21 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
22 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
23 import org.opendaylight.mdsal.binding.model.api.Type;
24 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilder;
25 import org.opendaylight.mdsal.binding.model.ri.BindingTypes;
26 import org.opendaylight.mdsal.binding.runtime.api.CompositeRuntimeType;
27 import org.opendaylight.mdsal.binding.runtime.api.RuntimeType;
28 import org.opendaylight.yangtools.yang.common.QName;
29 import org.opendaylight.yangtools.yang.model.api.AddedByUsesAware;
30 import org.opendaylight.yangtools.yang.model.api.CopyableNode;
31 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
32 import org.opendaylight.yangtools.yang.model.api.stmt.ActionEffectiveStatement;
33 import org.opendaylight.yangtools.yang.model.api.stmt.AnydataEffectiveStatement;
34 import org.opendaylight.yangtools.yang.model.api.stmt.AnyxmlEffectiveStatement;
35 import org.opendaylight.yangtools.yang.model.api.stmt.AugmentEffectiveStatement;
36 import org.opendaylight.yangtools.yang.model.api.stmt.CaseEffectiveStatement;
37 import org.opendaylight.yangtools.yang.model.api.stmt.ChoiceEffectiveStatement;
38 import org.opendaylight.yangtools.yang.model.api.stmt.ContainerEffectiveStatement;
39 import org.opendaylight.yangtools.yang.model.api.stmt.GroupingEffectiveStatement;
40 import org.opendaylight.yangtools.yang.model.api.stmt.IdentityEffectiveStatement;
41 import org.opendaylight.yangtools.yang.model.api.stmt.InputEffectiveStatement;
42 import org.opendaylight.yangtools.yang.model.api.stmt.LeafEffectiveStatement;
43 import org.opendaylight.yangtools.yang.model.api.stmt.LeafListEffectiveStatement;
44 import org.opendaylight.yangtools.yang.model.api.stmt.ListEffectiveStatement;
45 import org.opendaylight.yangtools.yang.model.api.stmt.NotificationEffectiveStatement;
46 import org.opendaylight.yangtools.yang.model.api.stmt.OutputEffectiveStatement;
47 import org.opendaylight.yangtools.yang.model.api.stmt.RpcEffectiveStatement;
48 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStatement;
49 import org.opendaylight.yangtools.yang.model.api.stmt.TypedefEffectiveStatement;
50 import org.opendaylight.yangtools.yang.model.api.stmt.UsesEffectiveStatement;
51 import org.opendaylight.yangtools.yang.model.ri.type.TypeBuilder;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54
55 /**
56  * A composite generator. Composite generators may contain additional children, which end up being mapped into
57  * the naming hierarchy 'under' the composite generator. To support this use case, each composite has a Java package
58  * name assigned.
59  *
60  * <p>
61  * State tracking for resolution of children to their original declaration, i.e. back along the 'uses' and 'augment'
62  * axis. This is quite convoluted because we are traversing the generator tree recursively in the iteration order of
63  * children, but actual dependencies may require resolution in a different order, for example in the case of:
64  * <pre>
65  *   container foo {
66  *     uses bar {             // A
67  *       augment bar {        // B
68  *         container xyzzy;   // C
69  *       }
70  *     }
71  *
72  *     grouping bar {
73  *       container bar {      // D
74  *         uses baz;          // E
75  *       }
76  *     }
77  *
78  *     grouping baz {
79  *       leaf baz {           // F
80  *         type string;
81  *       }
82  *     }
83  *   }
84  *
85  *   augment /foo/bar/xyzzy { // G
86  *     leaf xyzzy {           // H
87  *       type string;
88  *     }
89  *   }
90  * </pre>
91  *
92  * <p>
93  * In this case we have three manifestations of 'leaf baz' -- marked A, E and F in the child iteration order. In order
94  * to perform a resolution, we first have to determine that F is the original definition, then establish that E is using
95  * the definition made by F and finally establish that A is using the definition made by F.
96  *
97  * <p>
98  * Dealing with augmentations is harder still, because we need to attach them to the original definition, hence for the
99  * /foo/bar container at A, we need to understand that its original definition is at D and we need to attach the augment
100  * at B to D. Futhermore we also need to establish that the augmentation at G attaches to container defined in C, so
101  * that the 'leaf xyzzy' existing as /foo/bar/xyzzy/xyzzy under C has its original definition at H.
102  *
103  * <p>
104  * Finally realize that the augment at G can actually exist in a different module and is shown in this example only
105  * the simplified form. That also means we could encounter G well before 'container foo' as well as we can have multiple
106  * such augments sprinkled across multiple modules having the same dependency rules as between C and G -- but they still
107  * have to form a directed acyclic graph and we partially deal with those complexities by having modules sorted by their
108  * dependencies.
109  *
110  * <p>
111  * For further details see {@link #linkOriginalGenerator()} and {@link #linkOriginalGeneratorRecursive()}, which deal
112  * with linking original instances in the tree iteration order. The part dealing with augment attachment lives mostly
113  * in {@link AugmentRequirement}.
114  */
115 public abstract class AbstractCompositeGenerator<S extends EffectiveStatement<?, ?>, R extends CompositeRuntimeType>
116         extends AbstractExplicitGenerator<S, R> {
117     private static final Logger LOG = LoggerFactory.getLogger(AbstractCompositeGenerator.class);
118
119     // FIXME: we want to allocate this lazily to lower memory footprint
120     private final @NonNull CollisionDomain domain = new CollisionDomain(this);
121     private final @NonNull List<Generator> childGenerators;
122
123     /**
124      * List of {@code augment} statements targeting this generator. This list is maintained only for the primary
125      * incarnation. This list is an evolving entity until after we have finished linkage of original statements. It is
126      * expected to be stable at the start of {@code step 2} in {@link GeneratorReactor#execute(TypeBuilderFactory)}.
127      */
128     private @NonNull List<AbstractAugmentGenerator> augments = List.of();
129
130     /**
131      * List of {@code grouping} statements this statement references. This field is set once by
132      * {@link #linkUsesDependencies(GeneratorContext)}.
133      */
134     private List<GroupingGenerator> groupings;
135
136     /**
137      * List of composite children which have not been recursively processed. This may become a mutable list when we
138      * have some children which have not completed linking. Once we have completed linking of all children, including
139      * {@link #unlinkedChildren}, this will be set to {@code null}.
140      */
141     private List<AbstractCompositeGenerator<?, ?>> unlinkedComposites = List.of();
142     /**
143      * List of children which have not had their original linked. This list starts of as null. When we first attempt
144      * linkage, it becomes non-null.
145      */
146     private List<Generator> unlinkedChildren;
147
148     AbstractCompositeGenerator(final S statement) {
149         super(statement);
150         childGenerators = createChildren(statement);
151     }
152
153     AbstractCompositeGenerator(final S statement, final AbstractCompositeGenerator<?, ?> parent) {
154         super(statement, parent);
155         childGenerators = createChildren(statement);
156     }
157
158     @Override
159     public final Iterator<Generator> iterator() {
160         return childGenerators.iterator();
161     }
162
163     @Override
164     final GeneratedType runtimeJavaType() {
165         return generatedType().orElse(null);
166     }
167
168     final @NonNull List<AbstractAugmentGenerator> augments() {
169         return augments;
170     }
171
172     final @NonNull List<GroupingGenerator> groupings() {
173         return verifyNotNull(groupings, "Groupings not initialized in %s", this);
174     }
175
176     @Override
177     final R createExternalRuntimeType(final Type type) {
178         verify(type instanceof GeneratedType, "Unexpected type %s", type);
179
180         // FIXME: we should be able to use internal cache IFF when all augments end up being local to our statement
181         final var statement = statement();
182         return createBuilder(statement)
183             .fillTypes(ChildLookup.of(statement), this)
184             .build((GeneratedType) type);
185     }
186
187     abstract @NonNull CompositeRuntimeTypeBuilder<S, R> createBuilder(S statement);
188
189     @Override
190     final R createInternalRuntimeType(final ChildLookup lookup, final S statement, final Type type) {
191         verify(type instanceof GeneratedType, "Unexpected type %s", type);
192         return createBuilder(statement)
193             .fillTypes(lookup.inStatement(statement), this)
194             .build((GeneratedType) type);
195     }
196
197     @Override
198     final boolean isEmpty() {
199         return childGenerators.isEmpty();
200     }
201
202     final @Nullable AbstractExplicitGenerator<?, ?> findGenerator(final List<EffectiveStatement<?, ?>> stmtPath) {
203         return findGenerator(MatchStrategy.identity(), stmtPath, 0);
204     }
205
206     final @Nullable AbstractExplicitGenerator<?, ?> findGenerator(final MatchStrategy childStrategy,
207             // TODO: Wouldn't this method be nicer with Deque<EffectiveStatement<?, ?>> ?
208             final List<EffectiveStatement<?, ?>> stmtPath, final int offset) {
209         final EffectiveStatement<?, ?> stmt = stmtPath.get(offset);
210
211         // Try direct children first, which is simple
212         AbstractExplicitGenerator<?, ?> ret = childStrategy.findGenerator(stmt, childGenerators);
213         if (ret != null) {
214             final int next = offset + 1;
215             if (stmtPath.size() == next) {
216                 // Final step, return child
217                 return ret;
218             }
219             if (ret instanceof AbstractCompositeGenerator) {
220                 // We know how to descend down
221                 return ((AbstractCompositeGenerator<?, ?>) ret).findGenerator(childStrategy, stmtPath, next);
222             }
223             // Yeah, don't know how to continue here
224             return null;
225         }
226
227         // At this point we are about to fork for augments or groupings. In either case only schema tree statements can
228         // be found this way. The fun part is that if we find a match and need to continue, we will use the same
229         // strategy for children as well. We now know that this (and subsequent) statements need to have a QName
230         // argument.
231         if (stmt instanceof SchemaTreeEffectiveStatement) {
232             // grouping -> uses instantiation changes the namespace to the local namespace of the uses site. We are
233             // going the opposite direction, hence we are changing namespace from local to the grouping's namespace.
234             for (GroupingGenerator gen : groupings) {
235                 final MatchStrategy strat = MatchStrategy.grouping(gen);
236                 ret = gen.findGenerator(strat, stmtPath, offset);
237                 if (ret != null) {
238                     return ret;
239                 }
240             }
241
242             // All augments are dead simple: they need to match on argument (which we expect to be a QName)
243             final MatchStrategy strat = MatchStrategy.augment();
244             for (AbstractAugmentGenerator gen : augments) {
245                 ret = gen.findGenerator(strat, stmtPath, offset);
246                 if (ret != null) {
247                     return ret;
248                 }
249             }
250         }
251         return null;
252     }
253
254     final @NonNull CollisionDomain domain() {
255         return domain;
256     }
257
258     final void linkUsesDependencies(final GeneratorContext context) {
259         // We are establishing two linkages here:
260         // - we are resolving 'uses' statements to their corresponding 'grouping' definitions
261         // - we propagate those groupings as anchors to any augment statements, which takes out some amount of guesswork
262         //   from augment+uses resolution case, as groupings know about their immediate augments as soon as uses linkage
263         //   is resolved
264         final List<GroupingGenerator> tmp = new ArrayList<>();
265         for (EffectiveStatement<?, ?> stmt : statement().effectiveSubstatements()) {
266             if (stmt instanceof UsesEffectiveStatement) {
267                 final UsesEffectiveStatement uses = (UsesEffectiveStatement) stmt;
268                 final GroupingGenerator grouping = context.resolveTreeScoped(GroupingGenerator.class, uses.argument());
269                 tmp.add(grouping);
270
271                 // Trigger resolution of uses/augment statements. This looks like guesswork, but there may be multiple
272                 // 'augment' statements in a 'uses' statement and keeping a ListMultimap here seems wasteful.
273                 for (Generator gen : this) {
274                     if (gen instanceof UsesAugmentGenerator) {
275                         ((UsesAugmentGenerator) gen).resolveGrouping(uses, grouping);
276                     }
277                 }
278             }
279         }
280         groupings = List.copyOf(tmp);
281     }
282
283     final void startUsesAugmentLinkage(final List<AugmentRequirement> requirements) {
284         for (Generator child : childGenerators) {
285             if (child instanceof UsesAugmentGenerator) {
286                 requirements.add(((UsesAugmentGenerator) child).startLinkage());
287             }
288             if (child instanceof AbstractCompositeGenerator) {
289                 ((AbstractCompositeGenerator<?, ?>) child).startUsesAugmentLinkage(requirements);
290             }
291         }
292     }
293
294     final void addAugment(final AbstractAugmentGenerator augment) {
295         if (augments.isEmpty()) {
296             augments = new ArrayList<>(2);
297         }
298         augments.add(requireNonNull(augment));
299     }
300
301     /**
302      * Attempt to link the generator corresponding to the original definition for this generator's statements as well as
303      * to all child generators.
304      *
305      * @return Progress indication
306      */
307     final @NonNull LinkageProgress linkOriginalGeneratorRecursive() {
308         if (unlinkedComposites == null) {
309             // We have unset this list (see below), and there is nothing left to do
310             return LinkageProgress.DONE;
311         }
312
313         if (unlinkedChildren == null) {
314             unlinkedChildren = childGenerators.stream()
315                 .filter(AbstractExplicitGenerator.class::isInstance)
316                 .map(child -> (AbstractExplicitGenerator<?, ?>) child)
317                 .collect(Collectors.toList());
318         }
319
320         var progress = LinkageProgress.NONE;
321         if (!unlinkedChildren.isEmpty()) {
322             // Attempt to make progress on child linkage
323             final var it = unlinkedChildren.iterator();
324             while (it.hasNext()) {
325                 final var child = it.next();
326                 if (child instanceof AbstractExplicitGenerator) {
327                     if (((AbstractExplicitGenerator<?, ?>) child).linkOriginalGenerator()) {
328                         progress = LinkageProgress.SOME;
329                         it.remove();
330
331                         // If this is a composite generator we need to process is further
332                         if (child instanceof AbstractCompositeGenerator) {
333                             if (unlinkedComposites.isEmpty()) {
334                                 unlinkedComposites = new ArrayList<>();
335                             }
336                             unlinkedComposites.add((AbstractCompositeGenerator<?, ?>) child);
337                         }
338                     }
339                 }
340             }
341
342             if (unlinkedChildren.isEmpty()) {
343                 // Nothing left to do, make sure any previously-allocated list can be scavenged
344                 unlinkedChildren = List.of();
345             }
346         }
347
348         // Process children of any composite children we have.
349         final var it = unlinkedComposites.iterator();
350         while (it.hasNext()) {
351             final var tmp = it.next().linkOriginalGeneratorRecursive();
352             if (tmp != LinkageProgress.NONE) {
353                 progress = LinkageProgress.SOME;
354             }
355             if (tmp == LinkageProgress.DONE) {
356                 it.remove();
357             }
358         }
359
360         if (unlinkedChildren.isEmpty() && unlinkedComposites.isEmpty()) {
361             // All done, set the list to null to indicate there is nothing left to do in this generator or any of our
362             // children.
363             unlinkedComposites = null;
364             return LinkageProgress.DONE;
365         }
366
367         return progress;
368     }
369
370     @Override
371     final AbstractCompositeGenerator<S, R> getOriginal() {
372         return (AbstractCompositeGenerator<S, R>) super.getOriginal();
373     }
374
375     @Override
376     final AbstractCompositeGenerator<S, R> tryOriginal() {
377         return (AbstractCompositeGenerator<S, R>) super.tryOriginal();
378     }
379
380     final <X extends EffectiveStatement<?, ?>, Y extends RuntimeType> @Nullable OriginalLink<X, Y> originalChild(
381             final QName childQName) {
382         // First try groupings/augments ...
383         var found = findInferredGenerator(childQName);
384         if (found != null) {
385             return (OriginalLink<X, Y>) OriginalLink.partial(found);
386         }
387
388         // ... no luck, we really need to start looking at our origin
389         final var prev = previous();
390         if (prev != null) {
391             final QName prevQName = childQName.bindTo(prev.getQName().getModule());
392             found = prev.findSchemaTreeGenerator(prevQName);
393             if (found != null) {
394                 return (OriginalLink<X, Y>) found.originalLink();
395             }
396         }
397
398         return null;
399     }
400
401     @Override
402     final AbstractExplicitGenerator<?, ?> findSchemaTreeGenerator(final QName qname) {
403         final AbstractExplicitGenerator<?, ?> found = super.findSchemaTreeGenerator(qname);
404         return found != null ? found : findInferredGenerator(qname);
405     }
406
407     final @Nullable AbstractAugmentGenerator findAugmentForGenerator(final QName qname) {
408         for (var augment : augments) {
409             final var gen = augment.findSchemaTreeGenerator(qname);
410             if (gen != null) {
411                 return augment;
412             }
413         }
414         return null;
415     }
416
417     final @Nullable GroupingGenerator findGroupingForGenerator(final QName qname) {
418         for (GroupingGenerator grouping : groupings) {
419             final var gen = grouping.findSchemaTreeGenerator(qname.bindTo(grouping.statement().argument().getModule()));
420             if (gen != null) {
421                 return grouping;
422             }
423         }
424         return null;
425     }
426
427     private @Nullable AbstractExplicitGenerator<?, ?> findInferredGenerator(final QName qname) {
428         // First search our local groupings ...
429         for (var grouping : groupings) {
430             final var gen = grouping.findSchemaTreeGenerator(qname.bindTo(grouping.statement().argument().getModule()));
431             if (gen != null) {
432                 return gen;
433             }
434         }
435         // ... next try local augments, which may have groupings themselves
436         for (var augment : augments) {
437             final var gen = augment.findSchemaTreeGenerator(qname);
438             if (gen != null) {
439                 return gen;
440             }
441         }
442         return null;
443     }
444
445     /**
446      * Update the specified builder to implement interfaces generated for the {@code grouping} statements this generator
447      * is using.
448      *
449      * @param builder Target builder
450      * @param builderFactory factory for creating {@link TypeBuilder}s
451      * @return The number of groupings this type uses.
452      */
453     final int addUsesInterfaces(final GeneratedTypeBuilder builder, final TypeBuilderFactory builderFactory) {
454         for (GroupingGenerator grp : groupings) {
455             builder.addImplementsType(grp.getGeneratedType(builderFactory));
456         }
457         return groupings.size();
458     }
459
460     static final void addAugmentable(final GeneratedTypeBuilder builder) {
461         builder.addImplementsType(BindingTypes.augmentable(builder));
462     }
463
464     final void addGetterMethods(final GeneratedTypeBuilder builder, final TypeBuilderFactory builderFactory) {
465         for (Generator child : this) {
466             // Only process explicit generators here
467             if (child instanceof AbstractExplicitGenerator) {
468                 ((AbstractExplicitGenerator<?, ?>) child).addAsGetterMethod(builder, builderFactory);
469             }
470
471             final GeneratedType enclosedType = child.enclosedType(builderFactory);
472             if (enclosedType instanceof GeneratedTransferObject) {
473                 builder.addEnclosingTransferObject((GeneratedTransferObject) enclosedType);
474             } else if (enclosedType instanceof Enumeration) {
475                 builder.addEnumeration((Enumeration) enclosedType);
476             } else {
477                 verify(enclosedType == null, "Unhandled enclosed type %s in %s", enclosedType, child);
478             }
479         }
480     }
481
482     private @NonNull List<Generator> createChildren(final EffectiveStatement<?, ?> statement) {
483         final var tmp = new ArrayList<Generator>();
484         final var tmpAug = new ArrayList<AbstractAugmentGenerator>();
485
486         for (var stmt : statement.effectiveSubstatements()) {
487             if (stmt instanceof ActionEffectiveStatement) {
488                 if (!isAugmenting(stmt)) {
489                     tmp.add(new ActionGenerator((ActionEffectiveStatement) stmt, this));
490                 }
491             } else if (stmt instanceof AnydataEffectiveStatement) {
492                 if (!isAugmenting(stmt)) {
493                     tmp.add(new OpaqueObjectGenerator.Anydata((AnydataEffectiveStatement) stmt, this));
494                 }
495             } else if (stmt instanceof AnyxmlEffectiveStatement) {
496                 if (!isAugmenting(stmt)) {
497                     tmp.add(new OpaqueObjectGenerator.Anyxml((AnyxmlEffectiveStatement) stmt, this));
498                 }
499             } else if (stmt instanceof CaseEffectiveStatement) {
500                 tmp.add(new CaseGenerator((CaseEffectiveStatement) stmt, this));
501             } else if (stmt instanceof ChoiceEffectiveStatement) {
502                 // FIXME: use isOriginalDeclaration() ?
503                 if (!isAddedByUses(stmt)) {
504                     tmp.add(new ChoiceGenerator((ChoiceEffectiveStatement) stmt, this));
505                 }
506             } else if (stmt instanceof ContainerEffectiveStatement) {
507                 if (isOriginalDeclaration(stmt)) {
508                     tmp.add(new ContainerGenerator((ContainerEffectiveStatement) stmt, this));
509                 }
510             } else if (stmt instanceof GroupingEffectiveStatement) {
511                 tmp.add(new GroupingGenerator((GroupingEffectiveStatement) stmt, this));
512             } else if (stmt instanceof IdentityEffectiveStatement) {
513                 tmp.add(new IdentityGenerator((IdentityEffectiveStatement) stmt, this));
514             } else if (stmt instanceof InputEffectiveStatement) {
515                 final var cast = (InputEffectiveStatement) stmt;
516                 // FIXME: do not generate legacy RPC layout
517                 tmp.add(this instanceof RpcGenerator ? new RpcInputGenerator(cast, this)
518                     : new InputGenerator(cast, this));
519             } else if (stmt instanceof LeafEffectiveStatement) {
520                 if (!isAugmenting(stmt)) {
521                     tmp.add(new LeafGenerator((LeafEffectiveStatement) stmt, this));
522                 }
523             } else if (stmt instanceof LeafListEffectiveStatement) {
524                 if (!isAugmenting(stmt)) {
525                     tmp.add(new LeafListGenerator((LeafListEffectiveStatement) stmt, this));
526                 }
527             } else if (stmt instanceof ListEffectiveStatement) {
528                 if (isOriginalDeclaration(stmt)) {
529                     final ListGenerator listGen = new ListGenerator((ListEffectiveStatement) stmt, this);
530                     tmp.add(listGen);
531
532                     final KeyGenerator keyGen = listGen.keyGenerator();
533                     if (keyGen != null) {
534                         tmp.add(keyGen);
535                     }
536                 }
537             } else if (stmt instanceof NotificationEffectiveStatement) {
538                 if (!isAugmenting(stmt)) {
539                     tmp.add(new NotificationGenerator((NotificationEffectiveStatement) stmt, this));
540                 }
541             } else if (stmt instanceof OutputEffectiveStatement) {
542                 final var cast = (OutputEffectiveStatement) stmt;
543                 // FIXME: do not generate legacy RPC layout
544                 tmp.add(this instanceof RpcGenerator ? new RpcOutputGenerator(cast, this)
545                     : new OutputGenerator(cast, this));
546             } else if (stmt instanceof RpcEffectiveStatement) {
547                 tmp.add(new RpcGenerator((RpcEffectiveStatement) stmt, this));
548             } else if (stmt instanceof TypedefEffectiveStatement) {
549                 tmp.add(new TypedefGenerator((TypedefEffectiveStatement) stmt, this));
550             } else if (stmt instanceof AugmentEffectiveStatement) {
551                 // FIXME: MDSAL-695: So here we are ignoring any augment which is not in a module, while the 'uses'
552                 //                   processing takes care of the rest. There are two problems here:
553                 //
554                 //                   1) this could be an augment introduced through uses -- in this case we are picking
555                 //                      confusing it with this being its declaration site, we should probably be
556                 //                      ignoring it, but then
557                 //
558                 //                   2) we are losing track of AugmentEffectiveStatement for which we do not generate
559                 //                      interfaces -- and recover it at runtime through explicit walk along the
560                 //                      corresponding AugmentationSchemaNode.getOriginalDefinition() pointer
561                 //
562                 //                   So here is where we should decide how to handle this augment, and make sure we
563                 //                   retain information about this being an alias. That will serve as the base for keys
564                 //                   in the augment -> original map we provide to BindingRuntimeTypes.
565                 if (this instanceof ModuleGenerator) {
566                     tmpAug.add(new ModuleAugmentGenerator((AugmentEffectiveStatement) stmt, this));
567                 }
568             } else if (stmt instanceof UsesEffectiveStatement) {
569                 final UsesEffectiveStatement uses = (UsesEffectiveStatement) stmt;
570                 for (EffectiveStatement<?, ?> usesSub : uses.effectiveSubstatements()) {
571                     if (usesSub instanceof AugmentEffectiveStatement) {
572                         tmpAug.add(new UsesAugmentGenerator((AugmentEffectiveStatement) usesSub, uses, this));
573                     }
574                 }
575             } else {
576                 LOG.trace("Ignoring statement {}", stmt);
577             }
578         }
579
580         // Sort augments and add them last. This ensures child iteration order always reflects potential
581         // interdependencies, hence we do not need to worry about them. This is extremely important, as there are a
582         // number of places where we would have to either move the logic to parent statement and explicitly filter/sort
583         // substatements to establish this order.
584         tmpAug.sort(AbstractAugmentGenerator.COMPARATOR);
585         tmp.addAll(tmpAug);
586
587         // Compatibility FooService and FooListener interfaces, only generated for modules.
588         if (this instanceof ModuleGenerator) {
589             final ModuleGenerator moduleGen = (ModuleGenerator) this;
590
591             final List<NotificationGenerator> notifs = tmp.stream()
592                 .filter(NotificationGenerator.class::isInstance)
593                 .map(NotificationGenerator.class::cast)
594                 .collect(Collectors.toUnmodifiableList());
595             if (!notifs.isEmpty()) {
596                 tmp.add(new NotificationServiceGenerator(moduleGen, notifs));
597             }
598
599             final List<RpcGenerator> rpcs = tmp.stream()
600                 .filter(RpcGenerator.class::isInstance)
601                 .map(RpcGenerator.class::cast)
602                 .collect(Collectors.toUnmodifiableList());
603             if (!rpcs.isEmpty()) {
604                 tmp.add(new RpcServiceGenerator(moduleGen, rpcs));
605             }
606         }
607
608         return List.copyOf(tmp);
609     }
610
611     // Utility equivalent of (!isAddedByUses(stmt) && !isAugmenting(stmt)). Takes advantage of relationship between
612     // CopyableNode and AddedByUsesAware
613     private static boolean isOriginalDeclaration(final EffectiveStatement<?, ?> stmt) {
614         if (stmt instanceof AddedByUsesAware) {
615             if (((AddedByUsesAware) stmt).isAddedByUses()
616                 || stmt instanceof CopyableNode && ((CopyableNode) stmt).isAugmenting()) {
617                 return false;
618             }
619         }
620         return true;
621     }
622
623     private static boolean isAddedByUses(final EffectiveStatement<?, ?> stmt) {
624         return stmt instanceof AddedByUsesAware && ((AddedByUsesAware) stmt).isAddedByUses();
625     }
626
627     private static boolean isAugmenting(final EffectiveStatement<?, ?> stmt) {
628         return stmt instanceof CopyableNode && ((CopyableNode) stmt).isAugmenting();
629     }
630 }