diff options
author | 2015-08-26 17:22:35 +0000 | |
---|---|---|
committer | 2015-08-27 14:44:50 +0000 | |
commit | 6e7160defc4c6531a496eb08f94d499c15dc226b (patch) | |
tree | 99cb9ce652fa16b21fa03ffa68500a607842238d /src/main/java/com/google/devtools/build/lib/syntax | |
parent | c6fa0c10cd92c03c30674f195e6aeea247db6cb2 (diff) |
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
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/syntax')
3 files changed, 26 insertions, 7 deletions
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 5357d4d4db..3acbec1cab 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 @@ public class Environment { 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 @@ public class Environment { * 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 c2923896fb..8e2a5c3acc 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 @@ public class SkylarkEnvironment extends Environment implements Serializable { * 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 @@ public class SkylarkEnvironment extends Environment implements Serializable { /** * 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 7aedca2c82..9b71f96acc 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 @@ public class ValidationEnvironment { // 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 @@ public class ValidationEnvironment { /** * 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 class ValidationEnvironment { 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 * |