diff options
author | 2015-08-26 22:30:38 +0000 | |
---|---|---|
committer | 2015-08-27 14:44:53 +0000 | |
commit | 3a8ddcc6fa655ca9a3cc8f02ef7ba13f06cba14e (patch) | |
tree | 6a7d8b89a22b77b89db0bd7ee72fc90eb49d4aa3 /src/main/java/com/google/devtools/build/lib/syntax | |
parent | 6c44c581f1e394e7244c572f7247ef7552e1e37b (diff) |
Add a isLoadingPhase flag to Environment
Have loadingPhase-only methods check that flag.
It's no use removing the initial bindings to these methods
when they may have been copied anyway.
--
MOS_MIGRATED_REVID=101624770
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/syntax')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/syntax/Environment.java | 33 | ||||
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/syntax/SkylarkEnvironment.java | 4 |
2 files changed, 37 insertions, 0 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 3acbec1cab..b6acb050a9 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 @@ -20,6 +20,7 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable; import com.google.devtools.build.lib.events.EventHandler; +import com.google.devtools.build.lib.events.Location; import com.google.devtools.build.lib.vfs.PathFragment; import java.util.ArrayList; @@ -100,6 +101,38 @@ public class Environment { */ protected Set<String> propagatingVariables = new HashSet<>(); + // Only used in the global environment. + // TODO(bazel-team): make this a final field part of constructor. + private boolean isLoadingPhase = false; + + /** + * Is this Environment being evaluated during the loading phase? + * This is fixed during environment setup, and enables various functions + * that are not available during the analysis phase. + * @return true if this environment corresponds to code during the loading phase. + */ + boolean isLoadingPhase() { + return isLoadingPhase; + } + + /** + * Enable loading phase only functions in this Environment. + * This should only be done during setup before code is evaluated. + */ + public void setLoadingPhase() { + isLoadingPhase = true; + } + + /** + * Checks that the current Evaluation context is in loading phase. + * @param symbol name of the function being only authorized thus. + */ + public void checkLoadingPhase(String symbol, Location loc) throws EvalException { + if (!isLoadingPhase()) { + throw new EvalException(loc, symbol + "() can only be called during the loading phase"); + } + } + /** * Is this a global environment? * @return true if this is a global (top-level) environment 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 8e2a5c3acc..c200b839a2 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 @@ -69,6 +69,10 @@ public class SkylarkEnvironment extends Environment implements Serializable { // Always use the caller Environment's EventHandler. We cannot assume that the // definition Environment's EventHandler is still working properly. new SkylarkEnvironment(definitionEnv, stackTrace, callerEnv.eventHandler); + if (callerEnv.isLoadingPhase()) { + childEnv.setLoadingPhase(); + } + try { for (String varname : callerEnv.propagatingVariables) { childEnv.updateAndPropagate(varname, callerEnv.lookup(varname)); |