Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package com.google.devtools.build.lib.syntax; |
| 16 | |
| 17 | import com.google.common.annotations.VisibleForTesting; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableMap; |
Kristina Chodorow | 6f15335 | 2016-03-21 16:20:06 +0000 | [diff] [blame] | 20 | import com.google.devtools.build.lib.cmdline.Label; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 21 | import com.google.devtools.build.lib.events.Event; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 22 | import com.google.devtools.build.lib.events.EventHandler; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 23 | import com.google.devtools.build.lib.events.EventKind; |
Francois-Rene Rideau | 3a8ddcc | 2015-08-26 22:30:38 +0000 | [diff] [blame] | 24 | import com.google.devtools.build.lib.events.Location; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 25 | import com.google.devtools.build.lib.syntax.Mutability.Freezable; |
| 26 | import com.google.devtools.build.lib.syntax.Mutability.MutabilityException; |
| 27 | import com.google.devtools.build.lib.util.Fingerprint; |
| 28 | import com.google.devtools.build.lib.util.Pair; |
Mark Schaller | 6df8179 | 2015-12-10 18:47:47 +0000 | [diff] [blame] | 29 | import com.google.devtools.build.lib.util.Preconditions; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 30 | import java.io.Serializable; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 31 | import java.util.HashMap; |
| 32 | import java.util.HashSet; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 33 | import java.util.Map; |
| 34 | import java.util.Set; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 35 | import java.util.TreeSet; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 36 | import javax.annotation.Nullable; |
| 37 | |
| 38 | /** |
Laurent Le Brun | a6d8fe4 | 2016-11-28 18:14:54 +0000 | [diff] [blame] | 39 | * An Environment is the main entry point to evaluating code in the BUILD language or Skylark. It |
| 40 | * embodies all the state that is required to evaluate such code, except for the current instruction |
| 41 | * pointer, which is an {@link ASTNode} whose {@link Statement#exec exec} or {@link Expression#eval |
| 42 | * eval} method is invoked with this Environment, in a straightforward direct-style AST-walking |
| 43 | * interpreter. {@link Continuation}-s are explicitly represented, but only partly, with another |
| 44 | * part being implicit in a series of try-catch statements, to maintain the direct style. One |
| 45 | * notable trick is how a {@link UserDefinedFunction} implements returning values as the function |
| 46 | * catching a {@link ReturnStatement.ReturnException} thrown by a {@link ReturnStatement} in the |
| 47 | * body. |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 48 | * |
| 49 | * <p>Every Environment has a {@link Mutability} field, and must be used within a function that |
Laurent Le Brun | a6d8fe4 | 2016-11-28 18:14:54 +0000 | [diff] [blame] | 50 | * creates and closes this {@link Mutability} with the try-with-resource pattern. This {@link |
| 51 | * Mutability} is also used when initializing mutable objects within that Environment; when closed |
| 52 | * at the end of the computation freezes the Environment and all those objects that then become |
| 53 | * forever immutable. The pattern enforces the discipline that there should be no dangling mutable |
| 54 | * Environment, or concurrency between interacting Environment-s. It is also an error to try to |
| 55 | * mutate an Environment and its objects from another Environment, before the {@link Mutability} is |
| 56 | * closed. |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 57 | * |
Laurent Le Brun | a6d8fe4 | 2016-11-28 18:14:54 +0000 | [diff] [blame] | 58 | * <p>One creates an Environment using the {@link #builder} function, then populates it with {@link |
| 59 | * #setup}, {@link #setupDynamic} and sometimes {@link #setupOverride}, before to evaluate code in |
| 60 | * it with {@link BuildFileAST#eval}, or with {@link BuildFileAST#exec} (where the AST was obtained |
| 61 | * by passing a {@link ValidationEnvironment} constructed from the Environment to {@link |
| 62 | * BuildFileAST#parseBuildFile} or {@link BuildFileAST#parseSkylarkFile}). When the computation is |
| 63 | * over, the frozen Environment can still be queried with {@link #lookup}. |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 64 | * |
| 65 | * <p>Final fields of an Environment represent its dynamic state, i.e. state that remains the same |
Laurent Le Brun | a6d8fe4 | 2016-11-28 18:14:54 +0000 | [diff] [blame] | 66 | * throughout a given evaluation context, and don't change with source code location, while mutable |
| 67 | * fields embody its static state, that change with source code location. The seeming paradox is |
| 68 | * that the words "dynamic" and "static" refer to the point of view of the source code, and here we |
| 69 | * have a dual point of view. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 70 | */ |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 71 | public final class Environment implements Freezable { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 72 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 73 | /** |
Luis Fernando Pino Duque | 3fedf9e | 2016-04-28 15:47:29 +0000 | [diff] [blame] | 74 | * A phase for enabling or disabling certain builtin functions |
| 75 | */ |
| 76 | public enum Phase { WORKSPACE, LOADING, ANALYSIS } |
| 77 | |
| 78 | /** |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 79 | * A Frame is a Map of bindings, plus a {@link Mutability} and a parent Frame |
| 80 | * from which to inherit bindings. |
| 81 | * |
| 82 | * <p>A Frame contains bindings mapping variable name to variable value in a given scope. |
| 83 | * It may also inherit bindings from a parent Frame corresponding to a parent scope, |
| 84 | * which in turn may inherit bindings from its own parent, etc., transitively. |
| 85 | * Bindings may shadow bindings from the parent. In Skylark, you may only mutate |
| 86 | * bindings from the current Frame, which always got its {@link Mutability} with the |
| 87 | * current {@link Environment}; but future extensions may make it more like Python |
| 88 | * and allow mutation of bindings in outer Frame-s (or then again may not). |
| 89 | * |
| 90 | * <p>A Frame inherits the {@link Mutability} from the {@link Environment} in which it was |
| 91 | * originally created. When that {@link Environment} is finalized and its {@link Mutability} |
| 92 | * is closed, it becomes immutable, including the Frame, which can be shared in other |
| 93 | * {@link Environment}-s. Indeed, a {@link UserDefinedFunction} will close over the global |
| 94 | * Frame of its definition {@link Environment}, which will thus be reused (immutably) |
Jon Brandvein | 2982609 | 2016-11-10 15:21:25 +0000 | [diff] [blame] | 95 | * in any {@link Environment} in which this function is called, so it's important to |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 96 | * preserve the {@link Mutability} to make sure no Frame is modified after it's been finalized. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 97 | */ |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 98 | public static final class Frame implements Freezable { |
| 99 | |
| 100 | private final Mutability mutability; |
| 101 | final Frame parent; |
| 102 | final Map<String, Object> bindings = new HashMap<>(); |
Kristina Chodorow | 6f15335 | 2016-03-21 16:20:06 +0000 | [diff] [blame] | 103 | // The label for the target this frame is defined in (e.g., //foo:bar.bzl). |
| 104 | @Nullable |
| 105 | private Label label; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 106 | |
Kristina Chodorow | 6f15335 | 2016-03-21 16:20:06 +0000 | [diff] [blame] | 107 | private Frame(Mutability mutability, Frame parent) { |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 108 | this.mutability = mutability; |
| 109 | this.parent = parent; |
Kristina Chodorow | 6f15335 | 2016-03-21 16:20:06 +0000 | [diff] [blame] | 110 | this.label = parent == null ? null : parent.label; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public final Mutability mutability() { |
| 115 | return mutability; |
| 116 | } |
| 117 | |
| 118 | /** |
Kristina Chodorow | 6f15335 | 2016-03-21 16:20:06 +0000 | [diff] [blame] | 119 | * Attaches a label to an existing frame. This is used to get the repository a Skylark |
| 120 | * extension is actually defined in. |
| 121 | * @param label the label to attach. |
| 122 | * @return a new Frame with the existing frame's properties plus the label. |
| 123 | */ |
| 124 | public Frame setLabel(Label label) { |
Brian Silverman | 93c0fbb | 2016-04-11 14:31:31 +0000 | [diff] [blame] | 125 | Frame result = new Frame(mutability, this); |
| 126 | result.label = label; |
| 127 | return result; |
Kristina Chodorow | 6f15335 | 2016-03-21 16:20:06 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Returns the label for this frame. |
| 132 | */ |
| 133 | @Nullable |
| 134 | public final Label label() { |
| 135 | return label; |
| 136 | } |
| 137 | |
| 138 | /** |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 139 | * Gets a binding from the current frame or if not found its parent. |
| 140 | * @param varname the name of the variable to be bound |
| 141 | * @return the value bound to variable |
| 142 | */ |
| 143 | public Object get(String varname) { |
| 144 | if (bindings.containsKey(varname)) { |
| 145 | return bindings.get(varname); |
| 146 | } |
| 147 | if (parent != null) { |
| 148 | return parent.get(varname); |
| 149 | } |
| 150 | return null; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Modifies a binding in the current Frame. |
| 155 | * Does not try to modify an inherited binding. |
| 156 | * This will shadow any inherited binding, which may be an error |
| 157 | * that you want to guard against before calling this function. |
| 158 | * @param env the Environment attempting the mutation |
| 159 | * @param varname the name of the variable to be bound |
| 160 | * @param value the value to bind to the variable |
| 161 | */ |
| 162 | public void put(Environment env, String varname, Object value) |
| 163 | throws MutabilityException { |
| 164 | Mutability.checkMutable(this, env); |
| 165 | bindings.put(varname, value); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Adds the variable names of this Frame and its transitive parents to the given set. |
| 170 | * This provides a O(n) way of extracting the list of all variables visible in an Environment. |
| 171 | * @param vars the set of visible variables in the Environment, being computed. |
| 172 | */ |
Janak Ramakrishnan | 9b12f9c | 2016-05-20 16:33:02 +0000 | [diff] [blame] | 173 | void addVariableNamesTo(Set<String> vars) { |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 174 | vars.addAll(bindings.keySet()); |
| 175 | if (parent != null) { |
| 176 | parent.addVariableNamesTo(vars); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | public Set<String> getDirectVariableNames() { |
| 181 | return bindings.keySet(); |
| 182 | } |
| 183 | |
| 184 | @Override |
| 185 | public String toString() { |
Francois-Rene Rideau | bd0c7bb | 2015-09-21 16:54:19 +0000 | [diff] [blame] | 186 | return String.format("<Frame%s>", mutability()); |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 187 | } |
| 188 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 189 | |
| 190 | /** |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 191 | * A Continuation contains data saved during a function call and restored when the function exits. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 192 | */ |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 193 | private static final class Continuation { |
| 194 | /** The {@link BaseFunction} being evaluated that will return into this Continuation. */ |
| 195 | BaseFunction function; |
| 196 | |
| 197 | /** The {@link FuncallExpression} to which this Continuation will return. */ |
| 198 | FuncallExpression caller; |
| 199 | |
| 200 | /** The next Continuation after this Continuation. */ |
| 201 | @Nullable Continuation continuation; |
| 202 | |
| 203 | /** The lexical Frame of the caller. */ |
| 204 | Frame lexicalFrame; |
| 205 | |
| 206 | /** The global Frame of the caller. */ |
| 207 | Frame globalFrame; |
| 208 | |
| 209 | /** The set of known global variables of the caller. */ |
| 210 | @Nullable Set<String> knownGlobalVariables; |
| 211 | |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 212 | Continuation( |
| 213 | Continuation continuation, |
| 214 | BaseFunction function, |
| 215 | FuncallExpression caller, |
| 216 | Frame lexicalFrame, |
| 217 | Frame globalFrame, |
Laurent Le Brun | 4db5064 | 2016-10-26 14:25:37 +0000 | [diff] [blame] | 218 | Set<String> knownGlobalVariables) { |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 219 | this.continuation = continuation; |
| 220 | this.function = function; |
| 221 | this.caller = caller; |
| 222 | this.lexicalFrame = lexicalFrame; |
| 223 | this.globalFrame = globalFrame; |
Jon Brandvein | 83e3acb | 2016-08-11 18:53:26 +0000 | [diff] [blame] | 224 | this.knownGlobalVariables = knownGlobalVariables; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
| 228 | // TODO(bazel-team): Fix this scary failure of serializability. |
| 229 | // skyframe.SkylarkImportLookupFunction processes a .bzl and returns an Extension, |
| 230 | // for use by whoever imports the .bzl file. Skyframe may subsequently serialize the results. |
| 231 | // And it will fail to process these bindings, because they are inherited from a non-serializable |
| 232 | // class (in previous versions of the code the serializable SkylarkEnvironment was inheriting |
| 233 | // from the non-serializable Environment and being returned by said Function). |
| 234 | // If we try to merge this otherwise redundant superclass into Extension, though, |
| 235 | // skyframe experiences a massive failure to serialize things, and it's unclear how far |
| 236 | // reaching the need to make things Serializable goes, though clearly we'll need to make |
| 237 | // a whole lot of things Serializable, and for efficiency, we'll want to implement sharing |
| 238 | // of imported values rather than a code explosion. |
| 239 | private static class BaseExtension { |
| 240 | final ImmutableMap<String, Object> bindings; |
| 241 | BaseExtension(Environment env) { |
| 242 | this.bindings = ImmutableMap.copyOf(env.globalFrame.bindings); |
| 243 | } |
Janak Ramakrishnan | f494f3e | 2015-09-14 21:29:56 +0000 | [diff] [blame] | 244 | |
| 245 | // Hack to allow serialization. |
| 246 | BaseExtension() { |
| 247 | this.bindings = ImmutableMap.of(); |
| 248 | } |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 249 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 250 | |
| 251 | /** |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 252 | * An Extension to be imported with load() into a BUILD or .bzl file. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 253 | */ |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 254 | public static final class Extension extends BaseExtension implements Serializable { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 255 | |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 256 | private final String transitiveContentHashCode; |
| 257 | |
| 258 | /** |
| 259 | * Constructs an Extension by extracting the new global definitions from an Environment. |
| 260 | * Also caches a hash code for the transitive content of the file and its dependencies. |
| 261 | * @param env the Environment from which to extract an Extension. |
| 262 | */ |
| 263 | public Extension(Environment env) { |
| 264 | super(env); |
| 265 | this.transitiveContentHashCode = env.getTransitiveContentHashCode(); |
| 266 | } |
| 267 | |
Janak Ramakrishnan | 9b12f9c | 2016-05-20 16:33:02 +0000 | [diff] [blame] | 268 | String getTransitiveContentHashCode() { |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 269 | return transitiveContentHashCode; |
| 270 | } |
| 271 | |
| 272 | /** get the value bound to a variable in this Extension */ |
| 273 | public Object get(String varname) { |
| 274 | return bindings.get(varname); |
| 275 | } |
| 276 | |
| 277 | /** does this Extension contain a binding for the named variable? */ |
| 278 | public boolean containsKey(String varname) { |
| 279 | return bindings.containsKey(varname); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Static Frame for lexical variables that are always looked up in the current Environment |
| 285 | * or for the definition Environment of the function currently being evaluated. |
| 286 | */ |
| 287 | private Frame lexicalFrame; |
| 288 | |
| 289 | /** |
| 290 | * Static Frame for global variables; either the current lexical Frame if evaluation is currently |
| 291 | * happening at the global scope of a BUILD file, or the global Frame at the time of function |
| 292 | * definition if evaluation is currently happening in the body of a function. Thus functions can |
| 293 | * close over other functions defined in the same file. |
| 294 | */ |
| 295 | private Frame globalFrame; |
| 296 | |
| 297 | /** |
| 298 | * Dynamic Frame for variables that are always looked up in the runtime Environment, |
| 299 | * and never in the lexical or "global" Environment as it was at the time of function definition. |
| 300 | * For instance, PACKAGE_NAME. |
| 301 | */ |
| 302 | private final Frame dynamicFrame; |
| 303 | |
| 304 | /** |
| 305 | * An EventHandler for errors and warnings. This is not used in the BUILD language, |
| 306 | * however it might be used in Skylark code called from the BUILD language, so shouldn't be null. |
| 307 | */ |
| 308 | private final EventHandler eventHandler; |
| 309 | |
| 310 | /** |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 311 | * For each imported extension, a global Skylark frame from which to load() individual bindings. |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 312 | */ |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 313 | private final Map<String, Extension> importedExtensions; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 314 | |
| 315 | /** |
Laurent Le Brun | e51a4d2 | 2016-10-11 18:04:16 +0000 | [diff] [blame] | 316 | * Is this Environment being executed during the loading phase? Many builtin functions are only |
| 317 | * enabled during the loading phase, and check this flag. |
Laurent Le Brun | 8e965b8 | 2016-08-03 11:50:24 +0000 | [diff] [blame] | 318 | * TODO(laurentlb): Remove from Environment |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 319 | */ |
Laurent Le Brun | e51a4d2 | 2016-10-11 18:04:16 +0000 | [diff] [blame] | 320 | private final Phase phase; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 321 | |
| 322 | /** |
| 323 | * When in a lexical (Skylark) Frame, this set contains the variable names that are global, |
| 324 | * as determined not by global declarations (not currently supported), |
| 325 | * but by previous lookups that ended being global or dynamic. |
| 326 | * This is necessary because if in a function definition something |
| 327 | * reads a global variable after which a local variable with the same name is assigned an |
| 328 | * Exception needs to be thrown. |
| 329 | */ |
| 330 | @Nullable private Set<String> knownGlobalVariables; |
| 331 | |
| 332 | /** |
| 333 | * When in a lexical (Skylark) frame, this lists the names of the functions in the call stack. |
| 334 | * We currently use it to artificially disable recursion. |
| 335 | */ |
| 336 | @Nullable private Continuation continuation; |
| 337 | |
| 338 | /** |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 339 | * Enters a scope by saving state to a new Continuation |
| 340 | * @param function the function whose scope to enter |
| 341 | * @param caller the source AST node for the caller |
| 342 | * @param globals the global Frame that this function closes over from its definition Environment |
| 343 | */ |
| 344 | void enterScope(BaseFunction function, FuncallExpression caller, Frame globals) { |
Janak Ramakrishnan | 9b12f9c | 2016-05-20 16:33:02 +0000 | [diff] [blame] | 345 | continuation = |
Laurent Le Brun | 4db5064 | 2016-10-26 14:25:37 +0000 | [diff] [blame] | 346 | new Continuation( |
| 347 | continuation, function, caller, lexicalFrame, globalFrame, knownGlobalVariables); |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 348 | lexicalFrame = new Frame(mutability(), null); |
| 349 | globalFrame = globals; |
Janak Ramakrishnan | 9b12f9c | 2016-05-20 16:33:02 +0000 | [diff] [blame] | 350 | knownGlobalVariables = new HashSet<>(); |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | /** |
| 354 | * Exits a scope by restoring state from the current continuation |
| 355 | */ |
| 356 | void exitScope() { |
| 357 | Preconditions.checkNotNull(continuation); |
| 358 | lexicalFrame = continuation.lexicalFrame; |
| 359 | globalFrame = continuation.globalFrame; |
| 360 | knownGlobalVariables = continuation.knownGlobalVariables; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 361 | continuation = continuation.continuation; |
| 362 | } |
| 363 | |
Janak Ramakrishnan | 9b12f9c | 2016-05-20 16:33:02 +0000 | [diff] [blame] | 364 | private final String transitiveHashCode; |
Francois-Rene Rideau | 3a8ddcc | 2015-08-26 22:30:38 +0000 | [diff] [blame] | 365 | |
| 366 | /** |
Damien Martin-Guillerez | 074b957 | 2016-05-23 12:33:07 +0000 | [diff] [blame] | 367 | * Checks that the current Environment is in the loading or the workspace phase. |
Laurent Le Brun | 27d0b32 | 2016-11-22 14:48:44 +0000 | [diff] [blame] | 368 | * TODO(laurentlb): Move to SkylarkUtils |
| 369 | * |
Damien Martin-Guillerez | 074b957 | 2016-05-23 12:33:07 +0000 | [diff] [blame] | 370 | * @param symbol name of the function being only authorized thus. |
| 371 | */ |
| 372 | public void checkLoadingOrWorkspacePhase(String symbol, Location loc) throws EvalException { |
| 373 | if (phase == Phase.ANALYSIS) { |
| 374 | throw new EvalException(loc, symbol + "() cannot be called during the analysis phase"); |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | /** |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 379 | * Checks that the current Environment is in the loading phase. |
Laurent Le Brun | 27d0b32 | 2016-11-22 14:48:44 +0000 | [diff] [blame] | 380 | * TODO(laurentlb): Move to SkylarkUtils |
| 381 | * |
Francois-Rene Rideau | 3a8ddcc | 2015-08-26 22:30:38 +0000 | [diff] [blame] | 382 | * @param symbol name of the function being only authorized thus. |
| 383 | */ |
| 384 | public void checkLoadingPhase(String symbol, Location loc) throws EvalException { |
Luis Fernando Pino Duque | 3fedf9e | 2016-04-28 15:47:29 +0000 | [diff] [blame] | 385 | if (phase != Phase.LOADING) { |
Francois-Rene Rideau | 3a8ddcc | 2015-08-26 22:30:38 +0000 | [diff] [blame] | 386 | throw new EvalException(loc, symbol + "() can only be called during the loading phase"); |
| 387 | } |
| 388 | } |
| 389 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 390 | /** |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 391 | * Is this a global Environment? |
| 392 | * @return true if the current code is being executed at the top-level, |
| 393 | * as opposed to inside the body of a function. |
Francois-Rene Rideau | 6e7160d | 2015-08-26 17:22:35 +0000 | [diff] [blame] | 394 | */ |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 395 | boolean isGlobal() { |
| 396 | return lexicalFrame == null; |
Francois-Rene Rideau | 6e7160d | 2015-08-26 17:22:35 +0000 | [diff] [blame] | 397 | } |
| 398 | |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 399 | @Override |
| 400 | public Mutability mutability() { |
| 401 | // the mutability of the environment is that of its dynamic frame. |
| 402 | return dynamicFrame.mutability(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 403 | } |
| 404 | |
Janak Ramakrishnan | 9b12f9c | 2016-05-20 16:33:02 +0000 | [diff] [blame] | 405 | /** @return the current Frame, in which variable side-effects happen. */ |
| 406 | private Frame currentFrame() { |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 407 | return isGlobal() ? globalFrame : lexicalFrame; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 408 | } |
| 409 | |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 410 | /** |
| 411 | * @return the global variables for the Environment (not including dynamic bindings). |
| 412 | */ |
| 413 | public Frame getGlobals() { |
| 414 | return globalFrame; |
| 415 | } |
| 416 | |
| 417 | /** |
| 418 | * Returns an EventHandler for errors and warnings. |
| 419 | * The BUILD language doesn't use it directly, but can call Skylark code that does use it. |
| 420 | * @return an EventHandler |
| 421 | */ |
Francois-Rene Rideau | e6a46b8 | 2015-04-10 18:31:43 +0000 | [diff] [blame] | 422 | public EventHandler getEventHandler() { |
| 423 | return eventHandler; |
| 424 | } |
| 425 | |
Janak Ramakrishnan | 9b12f9c | 2016-05-20 16:33:02 +0000 | [diff] [blame] | 426 | /** @return the current stack trace as a list of functions. */ |
| 427 | ImmutableList<BaseFunction> getStackTrace() { |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 428 | ImmutableList.Builder<BaseFunction> builder = new ImmutableList.Builder<>(); |
| 429 | for (Continuation k = continuation; k != null; k = k.continuation) { |
| 430 | builder.add(k.function); |
| 431 | } |
| 432 | return builder.build().reverse(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 433 | } |
| 434 | |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 435 | |
| 436 | /** |
| 437 | * Returns the FuncallExpression and the BaseFunction for the top-level call being evaluated. |
| 438 | */ |
| 439 | public Pair<FuncallExpression, BaseFunction> getTopCall() { |
| 440 | Continuation continuation = this.continuation; |
| 441 | if (continuation == null) { |
| 442 | return null; |
| 443 | } |
| 444 | while (continuation.continuation != null) { |
| 445 | continuation = continuation.continuation; |
| 446 | } |
| 447 | return new Pair<>(continuation.caller, continuation.function); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 448 | } |
| 449 | |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 450 | /** |
Laurent Le Brun | a31bc4e | 2016-10-27 12:48:22 +0000 | [diff] [blame] | 451 | * Constructs an Environment. This is the main, most basic constructor. |
| 452 | * |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 453 | * @param globalFrame a frame for the global Environment |
| 454 | * @param dynamicFrame a frame for the dynamic Environment |
| 455 | * @param eventHandler an EventHandler for warnings, errors, etc |
| 456 | * @param importedExtensions Extension-s from which to import bindings with load() |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 457 | * @param fileContentHashCode a hash for the source file being evaluated, if any |
Luis Fernando Pino Duque | 3fedf9e | 2016-04-28 15:47:29 +0000 | [diff] [blame] | 458 | * @param phase the current phase |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 459 | */ |
| 460 | private Environment( |
| 461 | Frame globalFrame, |
| 462 | Frame dynamicFrame, |
| 463 | EventHandler eventHandler, |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 464 | Map<String, Extension> importedExtensions, |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 465 | @Nullable String fileContentHashCode, |
Laurent Le Brun | 7c4a809 | 2016-12-01 13:24:41 +0000 | [diff] [blame] | 466 | Phase phase) { |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 467 | this.globalFrame = Preconditions.checkNotNull(globalFrame); |
| 468 | this.dynamicFrame = Preconditions.checkNotNull(dynamicFrame); |
| 469 | Preconditions.checkArgument(globalFrame.mutability().isMutable()); |
| 470 | Preconditions.checkArgument(dynamicFrame.mutability().isMutable()); |
| 471 | this.eventHandler = eventHandler; |
| 472 | this.importedExtensions = importedExtensions; |
Luis Fernando Pino Duque | 3fedf9e | 2016-04-28 15:47:29 +0000 | [diff] [blame] | 473 | this.phase = phase; |
Janak Ramakrishnan | 9b12f9c | 2016-05-20 16:33:02 +0000 | [diff] [blame] | 474 | this.transitiveHashCode = |
| 475 | computeTransitiveContentHashCode(fileContentHashCode, importedExtensions); |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | /** |
| 479 | * A Builder class for Environment |
| 480 | */ |
| 481 | public static class Builder { |
| 482 | private final Mutability mutability; |
Luis Fernando Pino Duque | 3fedf9e | 2016-04-28 15:47:29 +0000 | [diff] [blame] | 483 | private Phase phase = Phase.ANALYSIS; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 484 | @Nullable private Frame parent; |
| 485 | @Nullable private EventHandler eventHandler; |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 486 | @Nullable private Map<String, Extension> importedExtensions; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 487 | @Nullable private String fileContentHashCode; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 488 | |
| 489 | Builder(Mutability mutability) { |
| 490 | this.mutability = mutability; |
| 491 | } |
| 492 | |
Laurent Le Brun | a6d8fe4 | 2016-11-28 18:14:54 +0000 | [diff] [blame] | 493 | /** |
| 494 | * Obsolete, doesn't do anything. |
| 495 | * TODO(laurentlb): To be removed once call-sites have been updated |
| 496 | */ |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 497 | public Builder setSkylark() { |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 498 | return this; |
| 499 | } |
| 500 | |
Luis Fernando Pino Duque | 3fedf9e | 2016-04-28 15:47:29 +0000 | [diff] [blame] | 501 | /** Enables loading or workspace phase only functions in this Environment. */ |
| 502 | public Builder setPhase(Phase phase) { |
| 503 | Preconditions.checkState(this.phase == Phase.ANALYSIS); |
| 504 | this.phase = phase; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 505 | return this; |
| 506 | } |
| 507 | |
| 508 | /** Inherits global bindings from the given parent Frame. */ |
| 509 | public Builder setGlobals(Frame parent) { |
| 510 | Preconditions.checkState(this.parent == null); |
| 511 | this.parent = parent; |
| 512 | return this; |
| 513 | } |
| 514 | |
| 515 | /** Sets an EventHandler for errors and warnings. */ |
| 516 | public Builder setEventHandler(EventHandler eventHandler) { |
| 517 | Preconditions.checkState(this.eventHandler == null); |
| 518 | this.eventHandler = eventHandler; |
| 519 | return this; |
| 520 | } |
| 521 | |
| 522 | /** Declares imported extensions for load() statements. */ |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 523 | public Builder setImportedExtensions (Map<String, Extension> importMap) { |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 524 | Preconditions.checkState(this.importedExtensions == null); |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 525 | this.importedExtensions = importMap; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 526 | return this; |
| 527 | } |
| 528 | |
| 529 | /** Declares content hash for the source file for this Environment. */ |
| 530 | public Builder setFileContentHashCode(String fileContentHashCode) { |
| 531 | this.fileContentHashCode = fileContentHashCode; |
| 532 | return this; |
| 533 | } |
| 534 | |
| 535 | /** Builds the Environment. */ |
| 536 | public Environment build() { |
| 537 | Preconditions.checkArgument(mutability.isMutable()); |
| 538 | if (parent != null) { |
| 539 | Preconditions.checkArgument(!parent.mutability().isMutable()); |
| 540 | } |
| 541 | Frame globalFrame = new Frame(mutability, parent); |
| 542 | Frame dynamicFrame = new Frame(mutability, null); |
| 543 | if (importedExtensions == null) { |
| 544 | importedExtensions = ImmutableMap.of(); |
| 545 | } |
Laurent Le Brun | 4617109 | 2015-12-23 14:04:23 +0000 | [diff] [blame] | 546 | return new Environment( |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 547 | globalFrame, |
| 548 | dynamicFrame, |
| 549 | eventHandler, |
| 550 | importedExtensions, |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 551 | fileContentHashCode, |
Laurent Le Brun | 7c4a809 | 2016-12-01 13:24:41 +0000 | [diff] [blame] | 552 | phase); |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 553 | } |
| 554 | } |
| 555 | |
| 556 | public static Builder builder(Mutability mutability) { |
| 557 | return new Builder(mutability); |
| 558 | } |
| 559 | |
| 560 | /** |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 561 | * Sets a binding for a special dynamic variable in this Environment. |
| 562 | * This is not for end-users, and will throw an AssertionError in case of conflict. |
| 563 | * @param varname the name of the dynamic variable to be bound |
| 564 | * @param value a value to bind to the variable |
| 565 | * @return this Environment, in fluid style |
| 566 | */ |
| 567 | public Environment setupDynamic(String varname, Object value) { |
| 568 | if (dynamicFrame.get(varname) != null) { |
| 569 | throw new AssertionError( |
| 570 | String.format("Trying to bind dynamic variable '%s' but it is already bound", |
| 571 | varname)); |
| 572 | } |
| 573 | if (lexicalFrame != null && lexicalFrame.get(varname) != null) { |
| 574 | throw new AssertionError( |
| 575 | String.format("Trying to bind dynamic variable '%s' but it is already bound lexically", |
| 576 | varname)); |
| 577 | } |
| 578 | if (globalFrame.get(varname) != null) { |
| 579 | throw new AssertionError( |
| 580 | String.format("Trying to bind dynamic variable '%s' but it is already bound globally", |
| 581 | varname)); |
| 582 | } |
| 583 | try { |
| 584 | dynamicFrame.put(this, varname, value); |
| 585 | } catch (MutabilityException e) { |
| 586 | // End users don't have access to setupDynamic, and it is an implementation error |
| 587 | // if we encounter a mutability exception. |
| 588 | throw new AssertionError( |
| 589 | Printer.format( |
| 590 | "Trying to bind dynamic variable '%s' in frozen environment %r", varname, this), |
| 591 | e); |
| 592 | } |
| 593 | return this; |
| 594 | } |
| 595 | |
| 596 | |
| 597 | /** |
| 598 | * Modifies a binding in the current Frame of this Environment, as would an |
| 599 | * {@link AssignmentStatement}. Does not try to modify an inherited binding. |
| 600 | * This will shadow any inherited binding, which may be an error |
| 601 | * that you want to guard against before calling this function. |
| 602 | * @param varname the name of the variable to be bound |
| 603 | * @param value the value to bind to the variable |
| 604 | * @return this Environment, in fluid style |
| 605 | */ |
| 606 | public Environment update(String varname, Object value) throws EvalException { |
| 607 | Preconditions.checkNotNull(value, "update(value == null)"); |
| 608 | // prevents clashes between static and dynamic variables. |
| 609 | if (dynamicFrame.get(varname) != null) { |
| 610 | throw new EvalException( |
| 611 | null, String.format("Trying to update special read-only global variable '%s'", varname)); |
| 612 | } |
| 613 | if (isKnownGlobalVariable(varname)) { |
| 614 | throw new EvalException( |
| 615 | null, String.format("Trying to update read-only global variable '%s'", varname)); |
| 616 | } |
| 617 | try { |
| 618 | currentFrame().put(this, varname, Preconditions.checkNotNull(value)); |
| 619 | } catch (MutabilityException e) { |
| 620 | // Note that since at this time we don't accept the global keyword, and don't have closures, |
| 621 | // end users should never be able to mutate a frozen Environment, and a MutabilityException |
| 622 | // is therefore a failed assertion for Bazel. However, it is possible to shadow a binding |
| 623 | // imported from a parent Environment by updating the current Environment, which will not |
| 624 | // trigger a MutabilityException. |
| 625 | throw new AssertionError( |
| 626 | Printer.format("Can't update %s to %r in frozen environment", varname, value), |
| 627 | e); |
| 628 | } |
| 629 | return this; |
| 630 | } |
| 631 | |
Laurent Le Brun | 1afb0b2 | 2016-10-26 12:46:42 +0000 | [diff] [blame] | 632 | public boolean hasVariable(String varname) { |
Vladimir Moskva | 4994cb3 | 2016-08-10 15:44:31 +0000 | [diff] [blame] | 633 | return lookup(varname) != null; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | /** |
| 637 | * Initializes a binding in this Environment. It is an error if the variable is already bound. |
| 638 | * This is not for end-users, and will throw an AssertionError in case of conflict. |
| 639 | * @param varname the name of the variable to be bound |
| 640 | * @param value the value to bind to the variable |
| 641 | * @return this Environment, in fluid style |
| 642 | */ |
| 643 | public Environment setup(String varname, Object value) { |
| 644 | if (hasVariable(varname)) { |
| 645 | throw new AssertionError(String.format("variable '%s' already bound", varname)); |
| 646 | } |
| 647 | return setupOverride(varname, value); |
| 648 | } |
| 649 | |
| 650 | /** |
| 651 | * Initializes a binding in this environment. Overrides any previous binding. |
| 652 | * This is not for end-users, and will throw an AssertionError in case of conflict. |
| 653 | * @param varname the name of the variable to be bound |
| 654 | * @param value the value to bind to the variable |
| 655 | * @return this Environment, in fluid style |
| 656 | */ |
| 657 | public Environment setupOverride(String varname, Object value) { |
| 658 | try { |
| 659 | return update(varname, value); |
| 660 | } catch (EvalException ee) { |
| 661 | throw new AssertionError(ee); |
| 662 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | /** |
Vladimir Moskva | 4994cb3 | 2016-08-10 15:44:31 +0000 | [diff] [blame] | 666 | * @return the value from the environment whose name is "varname" if it exists, otherwise null. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 667 | */ |
Vladimir Moskva | 4994cb3 | 2016-08-10 15:44:31 +0000 | [diff] [blame] | 668 | public Object lookup(String varname) { |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 669 | // Which Frame to lookup first doesn't matter because update prevents clashes. |
| 670 | if (lexicalFrame != null) { |
| 671 | Object lexicalValue = lexicalFrame.get(varname); |
| 672 | if (lexicalValue != null) { |
| 673 | return lexicalValue; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 674 | } |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 675 | } |
| 676 | Object globalValue = globalFrame.get(varname); |
| 677 | Object dynamicValue = dynamicFrame.get(varname); |
| 678 | if (globalValue == null && dynamicValue == null) { |
Vladimir Moskva | 4994cb3 | 2016-08-10 15:44:31 +0000 | [diff] [blame] | 679 | return null; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 680 | } |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 681 | if (knownGlobalVariables != null) { |
| 682 | knownGlobalVariables.add(varname); |
| 683 | } |
| 684 | if (globalValue != null) { |
| 685 | return globalValue; |
| 686 | } |
| 687 | return dynamicValue; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | /** |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 691 | * @return true if varname is a known global variable, |
| 692 | * because it has been read in the context of the current function. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 693 | */ |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 694 | boolean isKnownGlobalVariable(String varname) { |
| 695 | return knownGlobalVariables != null && knownGlobalVariables.contains(varname); |
| 696 | } |
| 697 | |
Vladimir Moskva | dfad9e3 | 2016-09-15 18:22:01 +0000 | [diff] [blame] | 698 | public void handleEvent(Event event) { |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 699 | eventHandler.handle(event); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | /** |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 703 | * @return the (immutable) set of names of all variables defined in this |
| 704 | * Environment. Exposed for testing. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 705 | */ |
| 706 | @VisibleForTesting |
| 707 | public Set<String> getVariableNames() { |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 708 | Set<String> vars = new HashSet<>(); |
| 709 | if (lexicalFrame != null) { |
| 710 | lexicalFrame.addVariableNamesTo(vars); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 711 | } |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 712 | globalFrame.addVariableNamesTo(vars); |
| 713 | dynamicFrame.addVariableNamesTo(vars); |
| 714 | return vars; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | @Override |
| 718 | public int hashCode() { |
| 719 | throw new UnsupportedOperationException(); // avoid nondeterminism |
| 720 | } |
| 721 | |
| 722 | @Override |
| 723 | public boolean equals(Object that) { |
| 724 | throw new UnsupportedOperationException(); |
| 725 | } |
| 726 | |
| 727 | @Override |
| 728 | public String toString() { |
Francois-Rene Rideau | bd0c7bb | 2015-09-21 16:54:19 +0000 | [diff] [blame] | 729 | return String.format("<Environment%s>", mutability()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | /** |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 733 | * An Exception thrown when an attempt is made to import a symbol from a file |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 734 | * that was not properly loaded. |
| 735 | */ |
Janak Ramakrishnan | 9b12f9c | 2016-05-20 16:33:02 +0000 | [diff] [blame] | 736 | static class LoadFailedException extends Exception { |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 737 | LoadFailedException(String importString) { |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 738 | super(String.format("file '%s' was not correctly loaded. " |
| 739 | + "Make sure the 'load' statement appears in the global scope in your file", |
Vladimir Moskva | 4994cb3 | 2016-08-10 15:44:31 +0000 | [diff] [blame] | 740 | importString)); |
| 741 | } |
| 742 | |
| 743 | LoadFailedException(String importString, String symbolString) { |
| 744 | super(String.format("file '%s' does not contain symbol '%s'", importString, symbolString)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 745 | } |
| 746 | } |
| 747 | |
Janak Ramakrishnan | 9b12f9c | 2016-05-20 16:33:02 +0000 | [diff] [blame] | 748 | void importSymbol(String importString, Identifier symbol, String nameInLoadedFile) |
Vladimir Moskva | 4994cb3 | 2016-08-10 15:44:31 +0000 | [diff] [blame] | 749 | throws LoadFailedException { |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 750 | Preconditions.checkState(isGlobal()); // loading is only allowed at global scope. |
| 751 | |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 752 | if (!importedExtensions.containsKey(importString)) { |
| 753 | throw new LoadFailedException(importString); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 754 | } |
Florian Weikert | 9d659ad | 2015-07-23 14:44:36 +0000 | [diff] [blame] | 755 | |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 756 | Extension ext = importedExtensions.get(importString); |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 757 | |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 758 | if (!ext.containsKey(nameInLoadedFile)) { |
Vladimir Moskva | 4994cb3 | 2016-08-10 15:44:31 +0000 | [diff] [blame] | 759 | throw new LoadFailedException(importString, nameInLoadedFile); |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | Object value = ext.get(nameInLoadedFile); |
Florian Weikert | 9d659ad | 2015-07-23 14:44:36 +0000 | [diff] [blame] | 763 | |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 764 | try { |
| 765 | update(symbol.getName(), value); |
| 766 | } catch (EvalException e) { |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 767 | throw new LoadFailedException(importString); |
Francois-Rene Rideau | 5a94e59 | 2015-09-04 19:13:47 +0000 | [diff] [blame] | 768 | } |
Florian Weikert | 6a66339 | 2015-09-02 14:04:33 +0000 | [diff] [blame] | 769 | } |
| 770 | |
Janak Ramakrishnan | 9b12f9c | 2016-05-20 16:33:02 +0000 | [diff] [blame] | 771 | private static String computeTransitiveContentHashCode( |
| 772 | @Nullable String baseHashCode, Map<String, Extension> importedExtensions) { |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 773 | // Calculate a new hash from the hash of the loaded Extension-s. |
| 774 | Fingerprint fingerprint = new Fingerprint(); |
Janak Ramakrishnan | 9b12f9c | 2016-05-20 16:33:02 +0000 | [diff] [blame] | 775 | if (baseHashCode != null) { |
| 776 | fingerprint.addString(Preconditions.checkNotNull(baseHashCode)); |
| 777 | } |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 778 | TreeSet<String> importStrings = new TreeSet<>(importedExtensions.keySet()); |
| 779 | for (String importString : importStrings) { |
| 780 | fingerprint.addString(importedExtensions.get(importString).getTransitiveContentHashCode()); |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 781 | } |
| 782 | return fingerprint.hexDigestAndReset(); |
Janak Ramakrishnan | b6e33bc | 2015-09-06 21:05:23 +0000 | [diff] [blame] | 783 | } |
| 784 | |
Janak Ramakrishnan | 9b12f9c | 2016-05-20 16:33:02 +0000 | [diff] [blame] | 785 | /** |
| 786 | * Returns a hash code calculated from the hash code of this Environment and the |
| 787 | * transitive closure of other Environments it loads. |
| 788 | */ |
| 789 | public String getTransitiveContentHashCode() { |
| 790 | return transitiveHashCode; |
| 791 | } |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 792 | |
| 793 | /** A read-only Environment.Frame with global constants in it only */ |
Janak Ramakrishnan | 9b12f9c | 2016-05-20 16:33:02 +0000 | [diff] [blame] | 794 | static final Frame CONSTANTS_ONLY = createConstantsGlobals(); |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 795 | |
Laurent Le Brun | 5e99198 | 2016-10-14 13:39:45 +0000 | [diff] [blame] | 796 | /** A read-only Environment.Frame with initial globals */ |
| 797 | public static final Frame DEFAULT_GLOBALS = createDefaultGlobals(); |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 798 | |
Laurent Le Brun | 5e99198 | 2016-10-14 13:39:45 +0000 | [diff] [blame] | 799 | /** To be removed when all call-sites are updated. */ |
| 800 | public static final Frame SKYLARK = DEFAULT_GLOBALS; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 801 | |
| 802 | private static Environment.Frame createConstantsGlobals() { |
| 803 | try (Mutability mutability = Mutability.create("CONSTANTS")) { |
| 804 | Environment env = Environment.builder(mutability).build(); |
| 805 | Runtime.setupConstants(env); |
| 806 | return env.getGlobals(); |
| 807 | } |
| 808 | } |
| 809 | |
Laurent Le Brun | 5e99198 | 2016-10-14 13:39:45 +0000 | [diff] [blame] | 810 | private static Environment.Frame createDefaultGlobals() { |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 811 | try (Mutability mutability = Mutability.create("BUILD")) { |
| 812 | Environment env = Environment.builder(mutability).build(); |
| 813 | Runtime.setupConstants(env); |
Laurent Le Brun | 5e99198 | 2016-10-14 13:39:45 +0000 | [diff] [blame] | 814 | Runtime.setupMethodEnvironment(env, MethodLibrary.defaultGlobalFunctions); |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 815 | return env.getGlobals(); |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | |
| 820 | /** |
| 821 | * The fail fast handler, which throws a AssertionError whenever an error or warning occurs. |
| 822 | */ |
| 823 | public static final EventHandler FAIL_FAST_HANDLER = new EventHandler() { |
| 824 | @Override |
| 825 | public void handle(Event event) { |
| 826 | Preconditions.checkArgument( |
| 827 | !EventKind.ERRORS_AND_WARNINGS.contains(event.getKind()), event); |
| 828 | } |
| 829 | }; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 830 | } |