aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/syntax/LValue.java
diff options
context:
space:
mode:
authorGravatar Dmitry Lomov <dslomov@google.com>2017-03-01 17:45:12 +0000
committerGravatar Yue Gan <yueg@google.com>2017-03-02 13:31:45 +0000
commit950310ff911da6c26339f4dc0b124487adc0cdbb (patch)
treef8cd128c2506b79ee39021db80cca2d838ad1919 /src/main/java/com/google/devtools/build/lib/syntax/LValue.java
parent30490512eb0e48a3774cc4e4ef78680e77dd4e47 (diff)
*** Reason for rollback *** Roll-forward of commit 01120026dc313ee7ad9ea95069a29252eb19173b with fix. *** Original change description *** Automated [] rollback of commit 01120026dc313ee7ad9ea95069a29252eb19173b. -- PiperOrigin-RevId: 148897534 MOS_MIGRATED_REVID=148897534
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 {