aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/syntax/LValue.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/syntax/LValue.java')
-rw-r--r--src/main/java/com/google/devtools/build/lib/syntax/LValue.java30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/syntax/LValue.java b/src/main/java/com/google/devtools/build/lib/syntax/LValue.java
index 22d686bf4e..ecc72073fe 100644
--- a/src/main/java/com/google/devtools/build/lib/syntax/LValue.java
+++ b/src/main/java/com/google/devtools/build/lib/syntax/LValue.java
@@ -14,6 +14,7 @@
package com.google.devtools.build.lib.syntax;
+import com.google.common.collect.ImmutableSet;
import com.google.devtools.build.lib.events.Location;
import com.google.devtools.build.lib.util.Preconditions;
import java.io.Serializable;
@@ -73,6 +74,35 @@ public class LValue implements Serializable {
}
}
+ /**
+ * Returns all names bound by this LValue.
+ *
+ * Examples:
+ * <ul>
+ * <li><{@code x = ...} binds x.</li>
+ * <li><{@code x, [y,z] = ..} binds x, y, z.</li>
+ * <li><{@code x[5] = ..} does not bind any names.</li>
+ * </ul>
+ */
+ public ImmutableSet<String> boundNames() {
+ ImmutableSet.Builder<String> result = ImmutableSet.builder();
+ collectBoundNames(expr, result);
+ return result.build();
+ }
+
+ private static void collectBoundNames(Expression lhs, ImmutableSet.Builder<String> result) {
+ if (lhs instanceof Identifier) {
+ result.add(((Identifier) lhs).getName());
+ return;
+ }
+ if (lhs instanceof ListLiteral) {
+ ListLiteral variables = (ListLiteral) lhs;
+ for (Expression expression : variables.getElements()) {
+ collectBoundNames(expression, result);
+ }
+ }
+ }
+
private static void doAssign(
Environment env, Location loc, Expression lhs, Object result)
throws EvalException, InterruptedException {