/** * @param path path to first element * @param pathTo path to second element * @return environment in which the generic parameters in the path to the first element are bound * to those in the path to the second element, or null type on error */ public static ResolvedName mapGenericParameters( final Deque<? extends INamedElement> path, final Deque<? extends INamedElement> pathTo) { // Construct an environment in which the current function's generic parameters // are bound to those of the eventual override. final Deque<List<? extends ResolvedName>> tableau = new LinkedList<>(ResolvedName.fromNamedElement(path.getLast()).tableau()); final Iterator<? extends INamedElement> pathIt = path.descendingIterator(); while (pathIt.hasNext()) { pathIt.next(); tableau.removeLast(); } CachedIterator<? extends INamedElement> itPathTo = null; CachedIterator<? extends IGenericParameter> itGPTo = null; boolean end = false; { // Initialise iterators into direct override's generic parameters. boolean foundValid = false; itPathTo = Iterators.cached(pathTo.iterator()); while (!foundValid && itPathTo.hasItem()) { itGPTo = Iterators.cached(itPathTo.item().genericParameters().iterator()); while (!foundValid && itGPTo.hasItem()) { foundValid = true; if (!foundValid) itGPTo.next(); } if (!foundValid) itPathTo.next(); } if (!foundValid) end = true; } for (final INamedElement elt : path) { final List<ResolvedName> row = new ArrayList<>(); for (@SuppressWarnings("unused") final IGenericParameter genericParameter : elt.genericParameters()) { if (end) return null; row.add(ResolvedName.fromNamedElement(itGPTo.item())); { // Increment iterators into direct override's generic parameters. boolean init = true; boolean foundValid = false; if (!init) itPathTo = Iterators.cached(pathTo.iterator()); while (!foundValid && itPathTo.hasItem()) { if (!init) itGPTo = Iterators.cached(itPathTo.item().genericParameters().iterator()); while (!foundValid && itGPTo.hasItem()) { if (!init) foundValid = true; init = false; if (!foundValid) itGPTo.next(); } if (!foundValid) itPathTo.next(); } if (!foundValid) end = true; } } tableau.add(row); } if (!end) return null; return ResolvedName.newNameReference(path.getLast(), tableau); }
/** * @param fn function * @return eventual overrides of the function, unifying those differing only in generic derivation */ public static Iterable<? extends IFunction> eventualOverrides(final IFunction fn) { // Iterate over the eventually overridden functions, // unifying those differing only through generic derivation. final Set<IFunction> processedEO = new LinkedHashSet<>(); final Deque<IFunction> queueEO = new LinkedList<>(); queueEO.addLast(fn); while (!queueEO.isEmpty()) { final IFunction fnEO = queueEO.getFirst(); queueEO.removeFirst(); if (!processedEO.contains(fnEO)) { processedEO.add(fnEO); // Tail-recurse on the function's direct overrides. for (final ResolvedName directOverride : fnEO.directOverrides()) queueEO.addLast((IFunction) directOverride.namedElement()); } } return processedEO; }