aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/analysis/constraints/ConstraintSemantics.java
diff options
context:
space:
mode:
authorGravatar Michael Staib <mstaib@google.com>2017-02-14 15:50:04 +0000
committerGravatar Dmitry Lomov <dslomov@google.com>2017-02-14 15:52:37 +0000
commita751f92b9fc21930547ea67347604fca0d0ed1e6 (patch)
treeb78c4a4d1f359391f5c17092dcd64ed4d9efb016 /src/main/java/com/google/devtools/build/lib/analysis/constraints/ConstraintSemantics.java
parent83c0a60b5c4b8ecc266e7a736ec154b9d256e2c1 (diff)
Refactoring: Types report what class of labels they contain.
Currently label-type attributes are detected in many places across the codebase by simply reference-comparing against each of the label types. This CL aims to generalize most of these cases, moving the encoding of this logic into a single place (Type/BuildType itself). Not all of these cases can be made general without further refactoring, and some perhaps shouldn't be - serialization and Skylark rule context, for example, need to do exotic things based on the type. But most sites can avoid having to enumerate all the types they work with explicitly. This causes LABEL_DICT_UNARY to start being treated like the other label types, which means that CcToolchainSuiteRule and JavaRuntimeSuiteRule need to include a set of allowed file types (none, in their case). Skylark will continue treating it as a dictionary from String to Label in its rule context, however, to avoid visible behavior changes. -- PiperOrigin-RevId: 147471542 MOS_MIGRATED_REVID=147471542
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/analysis/constraints/ConstraintSemantics.java')
-rw-r--r--src/main/java/com/google/devtools/build/lib/analysis/constraints/ConstraintSemantics.java29
1 files changed, 16 insertions, 13 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/constraints/ConstraintSemantics.java b/src/main/java/com/google/devtools/build/lib/analysis/constraints/ConstraintSemantics.java
index 86d736d60a..33f7d348f6 100644
--- a/src/main/java/com/google/devtools/build/lib/analysis/constraints/ConstraintSemantics.java
+++ b/src/main/java/com/google/devtools/build/lib/analysis/constraints/ConstraintSemantics.java
@@ -36,6 +36,8 @@ import com.google.devtools.build.lib.packages.Rule;
import com.google.devtools.build.lib.packages.RuleClass;
import com.google.devtools.build.lib.packages.Target;
import com.google.devtools.build.lib.syntax.Type;
+import com.google.devtools.build.lib.syntax.Type.LabelClass;
+import com.google.devtools.build.lib.syntax.Type.LabelVisitor;
import com.google.devtools.build.lib.util.Preconditions;
import java.util.Collection;
@@ -739,7 +741,7 @@ public class ConstraintSemantics {
AttributeMap attributes = ruleContext.attributes();
for (String attr : attributes.getAttributeNames()) {
Attribute attrDef = attributes.getAttributeDefinition(attr);
- if ((attrDef.getType() != BuildType.LABEL && attrDef.getType() != BuildType.LABEL_LIST)
+ if (attrDef.getType().getLabelClass() != LabelClass.DEPENDENCY
|| attrDef.skipConstraintsOverride()) {
continue;
}
@@ -819,21 +821,22 @@ public class ConstraintSemantics {
* Adds all label values from the given select to the given set. Automatically handles different
* value types (e.g. labels vs. label lists).
*/
- private static void addSelectValuesToSet(BuildType.Selector<?> select, Set<Label> set) {
+ private static void addSelectValuesToSet(BuildType.Selector<?> select, final Set<Label> set) {
Type<?> type = select.getOriginalType();
- if (type == BuildType.LABEL || type == BuildType.NODEP_LABEL) {
- set.addAll(((BuildType.Selector<Label>) select).getEntries().values());
- } else if (type == BuildType.LABEL_LIST || type == BuildType.NODEP_LABEL_LIST) {
- for (List<Label> labels : ((BuildType.Selector<List<Label>>) select).getEntries().values()) {
- set.addAll(labels);
+ LabelVisitor visitor = new LabelVisitor() {
+ @Override
+ public void visit(Label label) {
+ set.add(label);
}
- } else if (type == BuildType.LABEL_DICT_UNARY) {
- for (Map<String, Label> mapEntry :
- ((BuildType.Selector<Map<String, Label>>) select).getEntries().values()) {
- set.addAll(mapEntry.values());
+ };
+ for (Object value : select.getEntries().values()) {
+ try {
+ type.visitLabels(visitor, value);
+ } catch (InterruptedException ex) {
+ // Because the LabelVisitor does not throw InterruptedException, it should not be thrown
+ // by visitLabels here.
+ throw new AssertionError(ex);
}
- } else {
- throw new IllegalStateException("Expected a label-based type for this select");
}
}
}