From 2cb2ac6e9402ffa27f32880f52c13be60c631ff6 Mon Sep 17 00:00:00 2001 From: laurentlb Date: Thu, 24 May 2018 06:11:07 -0700 Subject: Delete GlobList RELNOTES: None. PiperOrigin-RevId: 197881012 --- .../devtools/build/lib/syntax/GlobCriteria.java | 215 --------------------- .../google/devtools/build/lib/syntax/GlobList.java | 132 ------------- .../devtools/build/lib/syntax/SelectorList.java | 4 +- .../devtools/build/lib/syntax/SkylarkList.java | 77 ++------ 4 files changed, 16 insertions(+), 412 deletions(-) delete mode 100644 src/main/java/com/google/devtools/build/lib/syntax/GlobCriteria.java delete mode 100644 src/main/java/com/google/devtools/build/lib/syntax/GlobList.java (limited to 'src/main/java/com/google') diff --git a/src/main/java/com/google/devtools/build/lib/syntax/GlobCriteria.java b/src/main/java/com/google/devtools/build/lib/syntax/GlobCriteria.java deleted file mode 100644 index 2cf0dc2794..0000000000 --- a/src/main/java/com/google/devtools/build/lib/syntax/GlobCriteria.java +++ /dev/null @@ -1,215 +0,0 @@ -// Copyright 2014 The Bazel Authors. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package com.google.devtools.build.lib.syntax; - -import static com.google.common.collect.ImmutableList.toImmutableList; - -import com.google.common.base.Functions; -import com.google.common.base.Preconditions; -import com.google.common.base.Splitter; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.Iterables; -import java.util.List; -import java.util.stream.Stream; -import javax.annotation.Nullable; - -/** - * Either the arguments to a glob call (the include and exclude lists) or the - * contents of a fixed list that was appended to a list of glob results. - * (The latter need to be stored by {@link GlobList} in order to fully - * reproduce the inputs that created the output list.) - * - *

For example, the expression - * glob(['*.java']) + ['x.properties'] - * will result in two GlobCriteria: one has include = ['*.java'], glob = true - * and the other, include = ['x.properties'], glob = false. - */ -public class GlobCriteria { - - /** - * A list of names or patterns that are included by this glob. They should - * consist of characters that are valid in labels in the BUILD language. - */ - private final ImmutableList include; - - /** - * A list of names or patterns that are excluded by this glob. They should - * consist of characters that are valid in labels in the BUILD language. - */ - private final ImmutableList exclude; - - /** True if the includes list was passed to glob(), false if not. */ - private final boolean glob; - - /** - * Parses criteria from its {@link #toExpression} form. - * Package-private for use by tests and GlobList. - * @throws IllegalArgumentException if the expression cannot be parsed - */ - public static GlobCriteria parse(String text) { - if (text.startsWith("glob([") && text.endsWith("])")) { - int excludeIndex = text.indexOf("], exclude=["); - if (excludeIndex == -1) { - String listText = text.substring(6, text.length() - 2); - return new GlobCriteria(parseList(listText), ImmutableList.of(), true); - } else { - String listText = text.substring(6, excludeIndex); - String excludeText = text.substring(excludeIndex + 12, text.length() - 2); - return new GlobCriteria(parseList(listText), parseList(excludeText), true); - } - } else if (text.startsWith("[") && text.endsWith("]")) { - String listText = text.substring(1, text.length() - 1); - return new GlobCriteria(parseList(listText), ImmutableList.of(), false); - } else { - throw new IllegalArgumentException( - "unrecognized format (not from toExpression?): " + text); - } - } - - /** - * Constructs a copy of a given glob critera object, with additional exclude patterns added. - * - * @param base a glob criteria object to copy. Must be an actual glob - * @param excludes a list of pattern strings indicating new excludes to provide - * @return a new glob criteria object which contains the same parameters as {@code base}, with - * the additional patterns in {@code excludes} added. - * @throws IllegalArgumentException if {@code base} is not a glob - */ - public static GlobCriteria createWithAdditionalExcludes(GlobCriteria base, - List excludes) { - Preconditions.checkArgument(base.isGlob()); - return fromGlobCall( - base.include, - Stream.concat(base.exclude.stream(), excludes.stream()).collect(toImmutableList())); - } - - /** - * Constructs a copy of a fixed list, converted to Strings. - */ - public static GlobCriteria fromList(Iterable list) { - Iterable strings = Iterables.transform(list, Functions.toStringFunction()); - return new GlobCriteria(ImmutableList.copyOf(strings), ImmutableList.of(), false); - } - - /** - * Constructs a glob call with include and exclude list. - * - * @param include list of included patterns - * @param exclude list of excluded patterns - */ - public static GlobCriteria fromGlobCall( - ImmutableList include, ImmutableList exclude) { - return new GlobCriteria(include, exclude, true); - } - - /** - * Constructs a glob call with include and exclude list. - */ - private GlobCriteria(ImmutableList include, ImmutableList exclude, boolean glob) { - this.include = include; - this.exclude = exclude; - this.glob = glob; - } - - /** - * Returns the patterns that were included in this {@code glob()} call. - */ - public ImmutableList getIncludePatterns() { - return include; - } - - /** - * Returns the patterns that were excluded in this {@code glob()} call. - */ - public ImmutableList getExcludePatterns() { - return exclude; - } - - /** - * Returns true if the include list was passed to {@code glob()}, false - * if it was a fixed list. If this returns false, the exclude list will - * always be empty. - */ - public boolean isGlob() { - return glob; - } - - /** - * Returns a String that represents this glob as a BUILD expression. - * For example, glob(['abc', 'def'], exclude=['uvw', 'xyz']) - * or ['foo', 'bar', 'baz']. - */ - public String toExpression() { - StringBuilder sb = new StringBuilder(); - if (glob) { - sb.append("glob("); - } - sb.append('['); - appendList(sb, include); - if (!exclude.isEmpty()) { - sb.append("], exclude=["); - appendList(sb, exclude); - } - sb.append(']'); - if (glob) { - sb.append(')'); - } - return sb.toString(); - } - - @Override - public String toString() { - return toExpression(); - } - - /** - * Takes a list of Strings, quotes them in single quotes, and appends them to - * a StringBuilder separated by a comma and space. This can be parsed back - * out by {@link #parseList}. - */ - private static void appendList(StringBuilder sb, List list) { - boolean first = true; - for (String content : list) { - if (!first) { - sb.append(", "); - } - sb.append('\'').append(content).append('\''); - first = false; - } - } - - /** - * Takes a String in the format created by {@link #appendList} and returns - * the original Strings. A null String (which may be returned when Pattern - * does not find a match) or the String "" (which will be captured in "[]") - * will result in an empty list. - */ - private static ImmutableList parseList(@Nullable String text) { - if (text == null) { - return ImmutableList.of(); - } - Iterable split = Splitter.on(", ").split(text); - ImmutableList.Builder listBuilder = ImmutableList.builder(); - for (String element : split) { - if (!element.isEmpty()) { - if ((element.length() < 2) || !element.startsWith("'") || !element.endsWith("'")) { - throw new IllegalArgumentException("expected a filename or pattern in quotes: " + text); - } - listBuilder.add(element.substring(1, element.length() - 1)); - } - } - return listBuilder.build(); - } -} diff --git a/src/main/java/com/google/devtools/build/lib/syntax/GlobList.java b/src/main/java/com/google/devtools/build/lib/syntax/GlobList.java deleted file mode 100644 index 5d03c0d303..0000000000 --- a/src/main/java/com/google/devtools/build/lib/syntax/GlobList.java +++ /dev/null @@ -1,132 +0,0 @@ -// Copyright 2014 The Bazel Authors. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package com.google.devtools.build.lib.syntax; - -import com.google.common.base.Joiner; -import com.google.common.base.Preconditions; -import com.google.common.base.Splitter; -import com.google.common.collect.ForwardingList; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.Iterables; -import com.google.devtools.build.lib.skylarkinterface.SkylarkModule; -import com.google.devtools.build.lib.skylarkinterface.SkylarkPrinter; -import com.google.devtools.build.lib.skylarkinterface.SkylarkValue; -import java.util.ArrayList; -import java.util.List; - -/** - * Glob matches and information about glob patterns, which are useful to - * ide_build_info. Its implementation of the List interface is as an immutable - * list of the matching files. Glob criteria can be retrieved through - * {@link #getCriteria}. - * - * @param the element this List contains (generally either String or Label) - */ -@SkylarkModule( - name = "glob list", - doc = "", - documented = false) -public final class GlobList extends ForwardingList implements SkylarkValue { - - /** Include/exclude criteria. */ - private final ImmutableList criteria; - - /** Matching files (usually either String or Label). */ - private final ImmutableList matches; - - /** - * Constructs a list with {@code glob()} call results. - * - * @param includes the patterns that the glob includes - * @param excludes the patterns that the glob excludes - * @param matches the filenames that matched the includes/excludes criteria - */ - public static GlobList captureResults(List includes, - List excludes, List matches) { - GlobCriteria criteria = GlobCriteria.fromGlobCall( - ImmutableList.copyOf(includes), ImmutableList.copyOf(excludes)); - return new GlobList<>(ImmutableList.of(criteria), matches); - } - - /** - * Parses a GlobInfo from its {@link #toExpression} representation. - */ - public static GlobList parse(String text) { - List criteria = new ArrayList<>(); - Iterable globs = Splitter.on(" + ").split(text); - for (String glob : globs) { - criteria.add(GlobCriteria.parse(glob)); - } - return new GlobList<>(criteria, ImmutableList.of()); - } - - /** - * Concatenates two lists into a new GlobList. If either of the lists is a - * GlobList, its GlobCriteria are preserved. Otherwise a simple GlobCriteria - * is created to represent the fixed list. - */ - public static GlobList concat( - List list1, List list2) { - // we add the list to both includes and matches, preserving order - ImmutableList.Builder criteriaBuilder = ImmutableList.builder(); - if (list1 instanceof GlobList) { - criteriaBuilder.addAll(((GlobList) list1).criteria); - } else { - criteriaBuilder.add(GlobCriteria.fromList(list1)); - } - if (list2 instanceof GlobList) { - criteriaBuilder.addAll(((GlobList) list2).criteria); - } else { - criteriaBuilder.add(GlobCriteria.fromList(list2)); - } - List matches = ImmutableList.copyOf(Iterables.concat(list1, list2)); - return new GlobList<>(criteriaBuilder.build(), matches); - } - - /** - * Constructs a list with given criteria and matches. - */ - public GlobList(List criteria, List matches) { - Preconditions.checkNotNull(criteria); - Preconditions.checkNotNull(matches); - this.criteria = ImmutableList.copyOf(criteria); - this.matches = ImmutableList.copyOf(matches); - } - - /** - * Returns the criteria used to create this list, from which the - * includes/excludes can be retrieved. - */ - public ImmutableList getCriteria() { - return criteria; - } - - /** - * Returns a String that represents this glob list as a BUILD expression. - */ - public String toExpression() { - return Joiner.on(" + ").join(criteria); - } - - @Override - protected ImmutableList delegate() { - return matches; - } - - @Override - public void repr(SkylarkPrinter printer) { - printer.printList(this, false); - } -} diff --git a/src/main/java/com/google/devtools/build/lib/syntax/SelectorList.java b/src/main/java/com/google/devtools/build/lib/syntax/SelectorList.java index f70a49884f..2a72bcdba1 100644 --- a/src/main/java/com/google/devtools/build/lib/syntax/SelectorList.java +++ b/src/main/java/com/google/devtools/build/lib/syntax/SelectorList.java @@ -149,9 +149,7 @@ public final class SelectorList implements SkylarkValue { } private static boolean isListType(Class type) { - return NATIVE_LIST_TYPE.isAssignableFrom(type) - || type.getSuperclass() == SkylarkList.class - || type == GlobList.class; + return NATIVE_LIST_TYPE.isAssignableFrom(type); } private static boolean canConcatenate(Class type1, Class type2) { diff --git a/src/main/java/com/google/devtools/build/lib/syntax/SkylarkList.java b/src/main/java/com/google/devtools/build/lib/syntax/SkylarkList.java index 50589dc59a..396e06457d 100644 --- a/src/main/java/com/google/devtools/build/lib/syntax/SkylarkList.java +++ b/src/main/java/com/google/devtools/build/lib/syntax/SkylarkList.java @@ -226,29 +226,19 @@ public abstract class SkylarkList extends BaseMutableList private final ArrayList contents; - // Treat GlobList specially: external code depends on it. - // TODO(bazel-team): make data structures *and binary operators* extensible - // (via e.g. interface classes for each binary operator) so that GlobList - // can be implemented outside of the core of Skylark. - // TODO(bazel-team): move GlobList out of Skylark, into an extension. - @Nullable private GlobList globList; - /** Final except for {@link #unsafeShallowFreeze}; must not be modified any other way. */ private Mutability mutability; private MutableList( ArrayList rawContents, - @Nullable GlobList globList, @Nullable Mutability mutability) { this.contents = Preconditions.checkNotNull(rawContents); - this.globList = globList; this.mutability = mutability == null ? Mutability.IMMUTABLE : mutability; } /** * Creates an instance, taking ownership of the supplied {@link ArrayList}. This is exposed for - * performance reasons. May be used when the supplied list is certainly not a {@link GlobList} - * (should be enforced by type system) and the calling code will not modify the supplied list + * performance reasons. May be used when the calling code will not modify the supplied list * after calling (honor system). */ static MutableList wrapUnsafe(@Nullable Environment env, ArrayList rawContents) { @@ -257,13 +247,12 @@ public abstract class SkylarkList extends BaseMutableList /** * Create an instance, taking ownership of the supplied {@link ArrayList}. This is exposed for - * performance reasons. May be used when the supplied list is certainly not a {@link GlobList} - * (enforced by type system as long as {@link GlobList} doesn't extend {@link ArrayList}) and - * the calling code will not modify the supplied list after calling (honor system). + * performance reasons. May be used when the calling code will not modify the supplied list + * after calling (honor system). */ static MutableList wrapUnsafe( @Nullable Mutability mutability, ArrayList rawContents) { - return new MutableList<>(rawContents, /*globList=*/ null, mutability); + return new MutableList<>(rawContents, mutability); } /** @@ -286,12 +275,10 @@ public abstract class SkylarkList extends BaseMutableList * Returns a {@code MutableList} whose items are given by an iterable and which has the given * {@link Mutability}. If {@code mutability} is null, the list is immutable. */ - @SuppressWarnings("unchecked") // GlobList cast. public static MutableList copyOf( @Nullable Mutability mutability, Iterable contents) { return new MutableList<>( Lists.newArrayList(contents), - contents instanceof GlobList ? (GlobList) contents : null, mutability); } @@ -312,10 +299,9 @@ public abstract class SkylarkList extends BaseMutableList * {@link Environment}. If {@code env} is null, the list is immutable. */ public static MutableList of(@Nullable Environment env, T... contents) { - // Safe since it's definitely not a GlobList, and we're taking a copy of the input. + // Safe since we're taking a copy of the input. return MutableList.wrapUnsafe( - env == null ? null : env.mutability(), - Lists.newArrayList(contents)); + env == null ? null : env.mutability(), Lists.newArrayList(contents)); } @Override @@ -329,17 +315,6 @@ public abstract class SkylarkList extends BaseMutableList this.mutability = Mutability.IMMUTABLE; } - @Override - protected void checkMutable(Location loc, Mutability mutability) throws EvalException { - super.checkMutable(loc, mutability); - globList = null; // If you're going to mutate it, invalidate the underlying GlobList. - } - - /** Returns the {@link GlobList} if there is one, or else null. */ - @Nullable public GlobList getGlobList() { - return globList; - } - @Override public boolean isTuple() { return false; @@ -355,11 +330,6 @@ public abstract class SkylarkList extends BaseMutableList return contents; } - /** Returns the {@link GlobList} if there is one, otherwise the regular contents. */ - private List getGlobListOrContentsUnsafe() { - return globList != null ? globList : contents; - } - /** * Returns a new {@code MutableList} that is the concatenation of two {@code MutableList}s. The * new list will have the given {@link Mutability}. @@ -368,18 +338,11 @@ public abstract class SkylarkList extends BaseMutableList MutableList left, MutableList right, Mutability mutability) { - if (left.getGlobList() == null && right.getGlobList() == null) { - ArrayList newContents = new ArrayList<>(left.size() + right.size()); - addAll(newContents, left.contents); - addAll(newContents, right.contents); - return new MutableList<>(newContents, /*globList=*/ null, mutability); - } else { - // Preserve glob criteria. - GlobList newGlobList = GlobList.concat( - left.getGlobListOrContentsUnsafe(), - right.getGlobListOrContentsUnsafe()); - return new MutableList<>(new ArrayList<>(newGlobList), newGlobList, mutability); - } + + ArrayList newContents = new ArrayList<>(left.size() + right.size()); + addAll(newContents, left.contents); + addAll(newContents, right.contents); + return new MutableList<>(newContents, mutability); } /** More efficient {@link List#addAll} replacement when both lists are {@link ArrayList}s. */ @@ -396,21 +359,11 @@ public abstract class SkylarkList extends BaseMutableList return MutableList.wrapUnsafe(mutability, new ArrayList<>()); } - if (getGlobList() == null) { - ArrayList repeated = new ArrayList<>(this.size() * times); - for (int i = 0; i < times; i++) { - repeated.addAll(this); - } - return MutableList.wrapUnsafe(mutability, repeated); - } else { - // Preserve glob criteria. - List globs = getGlobListOrContentsUnsafe(); - List original = globs; - for (int i = 1; i < times; i++) { - globs = GlobList.concat(globs, original); - } - return MutableList.copyOf(mutability, globs); + ArrayList repeated = new ArrayList<>(this.size() * times); + for (int i = 0; i < times; i++) { + repeated.addAll(this); } + return MutableList.wrapUnsafe(mutability, repeated); } @Override -- cgit v1.2.3