Create ValidationEnvironment from Environment
Allow ValidationEnvironment to be created from initial Environment so that
there is no need to manually keep two different sets of constructors in synch.
--
MOS_MIGRATED_REVID=101588695
diff --git a/src/main/java/com/google/devtools/build/lib/syntax/Environment.java b/src/main/java/com/google/devtools/build/lib/syntax/Environment.java
index 5357d4d..3acbec1 100644
--- a/src/main/java/com/google/devtools/build/lib/syntax/Environment.java
+++ b/src/main/java/com/google/devtools/build/lib/syntax/Environment.java
@@ -101,6 +101,15 @@
protected Set<String> propagatingVariables = new HashSet<>();
/**
+ * Is this a global environment?
+ * @return true if this is a global (top-level) environment
+ * as opposed to inside the body of a function
+ */
+ public boolean isGlobal() {
+ return true;
+ }
+
+ /**
* An EventHandler for errors and warnings. This is not used in the BUILD language,
* however it might be used in Skylark code called from the BUILD language.
*/
@@ -191,9 +200,10 @@
* Updates the value of variable "varname" in the environment, corresponding
* to an {@link AssignmentStatement}.
*/
- public void update(String varname, Object value) {
+ public Environment update(String varname, Object value) {
Preconditions.checkNotNull(value, "update(value == null)");
env.put(varname, value);
+ return this;
}
/**
diff --git a/src/main/java/com/google/devtools/build/lib/syntax/SkylarkEnvironment.java b/src/main/java/com/google/devtools/build/lib/syntax/SkylarkEnvironment.java
index c292389..8e2a5c3 100644
--- a/src/main/java/com/google/devtools/build/lib/syntax/SkylarkEnvironment.java
+++ b/src/main/java/com/google/devtools/build/lib/syntax/SkylarkEnvironment.java
@@ -119,7 +119,7 @@
* Clones this Skylark global environment.
*/
public SkylarkEnvironment cloneEnv(EventHandler eventHandler) {
- Preconditions.checkArgument(isGlobalEnvironment());
+ Preconditions.checkArgument(isGlobal());
SkylarkEnvironment newEnv = new SkylarkEnvironment(eventHandler, this.fileContentHashCode);
for (Entry<String, Object> entry : env.entrySet()) {
newEnv.env.put(entry.getKey(), entry.getValue());
@@ -142,7 +142,8 @@
/**
* Returns true if this is a Skylark global environment.
*/
- public boolean isGlobalEnvironment() {
+ @Override
+ public boolean isGlobal() {
return parent == null;
}
diff --git a/src/main/java/com/google/devtools/build/lib/syntax/ValidationEnvironment.java b/src/main/java/com/google/devtools/build/lib/syntax/ValidationEnvironment.java
index 7aedca2..9b71f96 100644
--- a/src/main/java/com/google/devtools/build/lib/syntax/ValidationEnvironment.java
+++ b/src/main/java/com/google/devtools/build/lib/syntax/ValidationEnvironment.java
@@ -47,13 +47,21 @@
// Whether this validation environment is not modified therefore clonable or not.
private boolean clonable;
-
+
/**
* Tracks the number of nested for loops that contain the statement that is currently being
* validated
*/
private int loopCount = 0;
+ /**
+ * Create a ValidationEnvironment for a given global Environment
+ */
+ public ValidationEnvironment(Environment env) {
+ this(env.getVariableNames());
+ Preconditions.checkArgument(env.isGlobal());
+ }
+
public ValidationEnvironment(Set<String> builtinVariables) {
parent = null;
variables.addAll(builtinVariables);
@@ -135,7 +143,7 @@
/**
* Starts a session with temporarily disabled readonly checking for variables between branches.
* This is useful to validate control flows like if-else when we know that certain parts of the
- * code cannot both be executed.
+ * code cannot both be executed.
*/
public void startTemporarilyDisableReadonlyCheckSession() {
futureReadOnlyVariables.add(new HashSet<String>());
@@ -190,14 +198,14 @@
public boolean isInsideLoop() {
return (loopCount > 0);
}
-
+
/**
* Signals that the block of a for loop was entered
*/
public void enterLoop() {
++loopCount;
}
-
+
/**
* Signals that the block of a for loop was left
*