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
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 3acbec1..b6acb05 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.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 @@
    */
   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