aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib
diff options
context:
space:
mode:
authorGravatar laurentlb <laurentlb@google.com>2018-05-24 06:11:07 -0700
committerGravatar Copybara-Service <copybara-piper@google.com>2018-05-24 06:12:21 -0700
commit2cb2ac6e9402ffa27f32880f52c13be60c631ff6 (patch)
tree9101eb1e62bbd93d48c69440ceff7a019636acdb /src/main/java/com/google/devtools/build/lib
parent70733df000fe72b8d06abdb92ce129be934d622b (diff)
Delete GlobList
RELNOTES: None. PiperOrigin-RevId: 197881012
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib')
-rw-r--r--src/main/java/com/google/devtools/build/lib/syntax/GlobCriteria.java215
-rw-r--r--src/main/java/com/google/devtools/build/lib/syntax/GlobList.java132
-rw-r--r--src/main/java/com/google/devtools/build/lib/syntax/SelectorList.java4
-rw-r--r--src/main/java/com/google/devtools/build/lib/syntax/SkylarkList.java77
4 files changed, 16 insertions, 412 deletions
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.)
- *
- * <p>For example, the expression
- * <code>glob(['*.java']) + ['x.properties']</code>
- * 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<String> 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<String> 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<String> 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<String> 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<String> include, ImmutableList<String> exclude) {
- return new GlobCriteria(include, exclude, true);
- }
-
- /**
- * Constructs a glob call with include and exclude list.
- */
- private GlobCriteria(ImmutableList<String> include, ImmutableList<String> 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<String> getIncludePatterns() {
- return include;
- }
-
- /**
- * Returns the patterns that were excluded in this {@code glob()} call.
- */
- public ImmutableList<String> 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, <code>glob(['abc', 'def'], exclude=['uvw', 'xyz'])</code>
- * or <code>['foo', 'bar', 'baz']</code>.
- */
- 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<String> 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<String> parseList(@Nullable String text) {
- if (text == null) {
- return ImmutableList.of();
- }
- Iterable<String> split = Splitter.on(", ").split(text);
- ImmutableList.Builder<String> 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 <E> the element this List contains (generally either String or Label)
- */
-@SkylarkModule(
- name = "glob list",
- doc = "",
- documented = false)
-public final class GlobList<E> extends ForwardingList<E> implements SkylarkValue {
-
- /** Include/exclude criteria. */
- private final ImmutableList<GlobCriteria> criteria;
-
- /** Matching files (usually either String or Label). */
- private final ImmutableList<E> 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 <T> GlobList<T> captureResults(List<String> includes,
- List<String> excludes, List<T> 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<String> parse(String text) {
- List<GlobCriteria> criteria = new ArrayList<>();
- Iterable<String> globs = Splitter.on(" + ").split(text);
- for (String glob : globs) {
- criteria.add(GlobCriteria.parse(glob));
- }
- return new GlobList<>(criteria, ImmutableList.<String>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 <T> GlobList<T> concat(
- List<? extends T> list1, List<? extends T> list2) {
- // we add the list to both includes and matches, preserving order
- ImmutableList.Builder<GlobCriteria> 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<T> matches = ImmutableList.copyOf(Iterables.concat(list1, list2));
- return new GlobList<>(criteriaBuilder.build(), matches);
- }
-
- /**
- * Constructs a list with given criteria and matches.
- */
- public GlobList(List<GlobCriteria> criteria, List<E> 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<GlobCriteria> 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<E> 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<E> extends BaseMutableList<E>
private final ArrayList<E> 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<E> globList;
-
/** Final except for {@link #unsafeShallowFreeze}; must not be modified any other way. */
private Mutability mutability;
private MutableList(
ArrayList<E> rawContents,
- @Nullable GlobList<E> 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 <T> MutableList<T> wrapUnsafe(@Nullable Environment env, ArrayList<T> rawContents) {
@@ -257,13 +247,12 @@ public abstract class SkylarkList<E> extends BaseMutableList<E>
/**
* 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 <T> MutableList<T> wrapUnsafe(
@Nullable Mutability mutability, ArrayList<T> rawContents) {
- return new MutableList<>(rawContents, /*globList=*/ null, mutability);
+ return new MutableList<>(rawContents, mutability);
}
/**
@@ -286,12 +275,10 @@ public abstract class SkylarkList<E> extends BaseMutableList<E>
* 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 <T> MutableList<T> copyOf(
@Nullable Mutability mutability, Iterable<? extends T> contents) {
return new MutableList<>(
Lists.newArrayList(contents),
- contents instanceof GlobList ? (GlobList<T>) contents : null,
mutability);
}
@@ -312,10 +299,9 @@ public abstract class SkylarkList<E> extends BaseMutableList<E>
* {@link Environment}. If {@code env} is null, the list is immutable.
*/
public static <T> MutableList<T> 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
@@ -330,17 +316,6 @@ public abstract class SkylarkList<E> extends BaseMutableList<E>
}
@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<E> getGlobList() {
- return globList;
- }
-
- @Override
public boolean isTuple() {
return false;
}
@@ -355,11 +330,6 @@ public abstract class SkylarkList<E> extends BaseMutableList<E>
return contents;
}
- /** Returns the {@link GlobList} if there is one, otherwise the regular contents. */
- private List<E> 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<E> extends BaseMutableList<E>
MutableList<? extends T> left,
MutableList<? extends T> right,
Mutability mutability) {
- if (left.getGlobList() == null && right.getGlobList() == null) {
- ArrayList<T> 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<T> newGlobList = GlobList.concat(
- left.getGlobListOrContentsUnsafe(),
- right.getGlobListOrContentsUnsafe());
- return new MutableList<>(new ArrayList<>(newGlobList), newGlobList, mutability);
- }
+
+ ArrayList<T> 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<E> extends BaseMutableList<E>
return MutableList.wrapUnsafe(mutability, new ArrayList<>());
}
- if (getGlobList() == null) {
- ArrayList<E> 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<? extends E> globs = getGlobListOrContentsUnsafe();
- List<? extends E> original = globs;
- for (int i = 1; i < times; i++) {
- globs = GlobList.concat(globs, original);
- }
- return MutableList.copyOf(mutability, globs);
+ ArrayList<E> repeated = new ArrayList<>(this.size() * times);
+ for (int i = 0; i < times; i++) {
+ repeated.addAll(this);
}
+ return MutableList.wrapUnsafe(mutability, repeated);
}
@Override