/** * @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 namedElement named element * @return ancestors of the named element, beginning with the root namespace and ending with the * named element itself, skipping local scopes */ public static Iterable<? extends INamedElement> ancestors(final INamedElement namedElement) { final Deque<INamedElement> ancestors = new LinkedList<>(); INamedElement ancestor = namedElement; while (membership(ancestor) != ScopeMembership.Root) { ancestors.addFirst(ancestor); ancestor = parentNamedElementOfScope(ancestor.parent()); } ancestors.addFirst(ancestor); return ancestors; }