blob: 7029a59ce5d62ef9c36c897f02fdec08fda0dff4 [file] [log] [blame]
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00001// Copyright 2014 The Bazel Authors. All rights reserved.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01002//
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
15package com.google.devtools.build.lib.syntax;
16
17import com.google.common.annotations.VisibleForTesting;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010018import com.google.common.collect.ImmutableList;
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +000019import com.google.common.collect.ImmutableMap;
Kristina Chodorow6f153352016-03-21 16:20:06 +000020import com.google.devtools.build.lib.cmdline.Label;
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +000021import com.google.devtools.build.lib.events.Event;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010022import com.google.devtools.build.lib.events.EventHandler;
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +000023import com.google.devtools.build.lib.events.EventKind;
Francois-Rene Rideau3a8ddcc2015-08-26 22:30:38 +000024import com.google.devtools.build.lib.events.Location;
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +000025import com.google.devtools.build.lib.syntax.Mutability.Freezable;
26import com.google.devtools.build.lib.syntax.Mutability.MutabilityException;
27import com.google.devtools.build.lib.util.Fingerprint;
28import com.google.devtools.build.lib.util.Pair;
Mark Schaller6df81792015-12-10 18:47:47 +000029import com.google.devtools.build.lib.util.Preconditions;
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +000030import java.io.Serializable;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010031import java.util.HashMap;
32import java.util.HashSet;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010033import java.util.Map;
34import java.util.Set;
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +000035import java.util.TreeSet;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010036import javax.annotation.Nullable;
37
38/**
Laurent Le Bruna6d8fe42016-11-28 18:14:54 +000039 * 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 Rideau89312fb2015-09-10 18:53:03 +000048 *
49 * <p>Every Environment has a {@link Mutability} field, and must be used within a function that
Laurent Le Bruna6d8fe42016-11-28 18:14:54 +000050 * 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 Rideau89312fb2015-09-10 18:53:03 +000057 *
Laurent Le Bruna6d8fe42016-11-28 18:14:54 +000058 * <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 Rideau89312fb2015-09-10 18:53:03 +000064 *
65 * <p>Final fields of an Environment represent its dynamic state, i.e. state that remains the same
Laurent Le Bruna6d8fe42016-11-28 18:14:54 +000066 * 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 Nienhuysd08b27f2015-02-25 16:45:20 +010070 */
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +000071public final class Environment implements Freezable {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010072
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010073 /**
Luis Fernando Pino Duque3fedf9e2016-04-28 15:47:29 +000074 * A phase for enabling or disabling certain builtin functions
75 */
76 public enum Phase { WORKSPACE, LOADING, ANALYSIS }
77
78 /**
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +000079 * 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 Brandvein29826092016-11-10 15:21:25 +000095 * in any {@link Environment} in which this function is called, so it's important to
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +000096 * preserve the {@link Mutability} to make sure no Frame is modified after it's been finalized.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010097 */
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +000098 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 Chodorow6f153352016-03-21 16:20:06 +0000103 // The label for the target this frame is defined in (e.g., //foo:bar.bzl).
104 @Nullable
105 private Label label;
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000106
Kristina Chodorow6f153352016-03-21 16:20:06 +0000107 private Frame(Mutability mutability, Frame parent) {
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000108 this.mutability = mutability;
109 this.parent = parent;
Kristina Chodorow6f153352016-03-21 16:20:06 +0000110 this.label = parent == null ? null : parent.label;
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000111 }
112
113 @Override
114 public final Mutability mutability() {
115 return mutability;
116 }
117
118 /**
Kristina Chodorow6f153352016-03-21 16:20:06 +0000119 * 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 Silverman93c0fbb2016-04-11 14:31:31 +0000125 Frame result = new Frame(mutability, this);
126 result.label = label;
127 return result;
Kristina Chodorow6f153352016-03-21 16:20:06 +0000128 }
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 Rideau89312fb2015-09-10 18:53:03 +0000139 * 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 Ramakrishnan9b12f9c2016-05-20 16:33:02 +0000173 void addVariableNamesTo(Set<String> vars) {
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000174 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 Rideaubd0c7bb2015-09-21 16:54:19 +0000186 return String.format("<Frame%s>", mutability());
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000187 }
188 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100189
190 /**
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000191 * A Continuation contains data saved during a function call and restored when the function exits.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100192 */
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000193 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 Rideau89312fb2015-09-10 18:53:03 +0000212 Continuation(
213 Continuation continuation,
214 BaseFunction function,
215 FuncallExpression caller,
216 Frame lexicalFrame,
217 Frame globalFrame,
Laurent Le Brun4db50642016-10-26 14:25:37 +0000218 Set<String> knownGlobalVariables) {
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000219 this.continuation = continuation;
220 this.function = function;
221 this.caller = caller;
222 this.lexicalFrame = lexicalFrame;
223 this.globalFrame = globalFrame;
Jon Brandvein83e3acb2016-08-11 18:53:26 +0000224 this.knownGlobalVariables = knownGlobalVariables;
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000225 }
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 Ramakrishnanf494f3e2015-09-14 21:29:56 +0000244
245 // Hack to allow serialization.
246 BaseExtension() {
247 this.bindings = ImmutableMap.of();
248 }
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000249 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100250
251 /**
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000252 * An Extension to be imported with load() into a BUILD or .bzl file.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100253 */
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000254 public static final class Extension extends BaseExtension implements Serializable {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100255
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000256 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 Ramakrishnan9b12f9c2016-05-20 16:33:02 +0000268 String getTransitiveContentHashCode() {
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000269 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 Field1ea7fc32015-12-22 19:37:19 +0000311 * For each imported extension, a global Skylark frame from which to load() individual bindings.
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000312 */
John Field1ea7fc32015-12-22 19:37:19 +0000313 private final Map<String, Extension> importedExtensions;
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000314
315 /**
Laurent Le Brune51a4d22016-10-11 18:04:16 +0000316 * 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 Brun8e965b82016-08-03 11:50:24 +0000318 * TODO(laurentlb): Remove from Environment
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000319 */
Laurent Le Brune51a4d22016-10-11 18:04:16 +0000320 private final Phase phase;
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000321
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 Rideau89312fb2015-09-10 18:53:03 +0000339 * 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 Ramakrishnan9b12f9c2016-05-20 16:33:02 +0000345 continuation =
Laurent Le Brun4db50642016-10-26 14:25:37 +0000346 new Continuation(
347 continuation, function, caller, lexicalFrame, globalFrame, knownGlobalVariables);
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000348 lexicalFrame = new Frame(mutability(), null);
349 globalFrame = globals;
Janak Ramakrishnan9b12f9c2016-05-20 16:33:02 +0000350 knownGlobalVariables = new HashSet<>();
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000351 }
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 Rideau89312fb2015-09-10 18:53:03 +0000361 continuation = continuation.continuation;
362 }
363
Janak Ramakrishnan9b12f9c2016-05-20 16:33:02 +0000364 private final String transitiveHashCode;
Francois-Rene Rideau3a8ddcc2015-08-26 22:30:38 +0000365
366 /**
Damien Martin-Guillerez074b9572016-05-23 12:33:07 +0000367 * Checks that the current Environment is in the loading or the workspace phase.
Laurent Le Brun27d0b322016-11-22 14:48:44 +0000368 * TODO(laurentlb): Move to SkylarkUtils
369 *
Damien Martin-Guillerez074b9572016-05-23 12:33:07 +0000370 * @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 Rideau89312fb2015-09-10 18:53:03 +0000379 * Checks that the current Environment is in the loading phase.
Laurent Le Brun27d0b322016-11-22 14:48:44 +0000380 * TODO(laurentlb): Move to SkylarkUtils
381 *
Francois-Rene Rideau3a8ddcc2015-08-26 22:30:38 +0000382 * @param symbol name of the function being only authorized thus.
383 */
384 public void checkLoadingPhase(String symbol, Location loc) throws EvalException {
Luis Fernando Pino Duque3fedf9e2016-04-28 15:47:29 +0000385 if (phase != Phase.LOADING) {
Francois-Rene Rideau3a8ddcc2015-08-26 22:30:38 +0000386 throw new EvalException(loc, symbol + "() can only be called during the loading phase");
387 }
388 }
389
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100390 /**
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000391 * 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 Rideau6e7160d2015-08-26 17:22:35 +0000394 */
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000395 boolean isGlobal() {
396 return lexicalFrame == null;
Francois-Rene Rideau6e7160d2015-08-26 17:22:35 +0000397 }
398
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000399 @Override
400 public Mutability mutability() {
401 // the mutability of the environment is that of its dynamic frame.
402 return dynamicFrame.mutability();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100403 }
404
Janak Ramakrishnan9b12f9c2016-05-20 16:33:02 +0000405 /** @return the current Frame, in which variable side-effects happen. */
406 private Frame currentFrame() {
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000407 return isGlobal() ? globalFrame : lexicalFrame;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100408 }
409
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000410 /**
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 Rideaue6a46b82015-04-10 18:31:43 +0000422 public EventHandler getEventHandler() {
423 return eventHandler;
424 }
425
Janak Ramakrishnan9b12f9c2016-05-20 16:33:02 +0000426 /** @return the current stack trace as a list of functions. */
427 ImmutableList<BaseFunction> getStackTrace() {
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000428 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 Nienhuysd08b27f2015-02-25 16:45:20 +0100433 }
434
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000435
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 Nienhuysd08b27f2015-02-25 16:45:20 +0100448 }
449
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000450 /**
Laurent Le Bruna31bc4e2016-10-27 12:48:22 +0000451 * Constructs an Environment. This is the main, most basic constructor.
452 *
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000453 * @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 Rideau89312fb2015-09-10 18:53:03 +0000457 * @param fileContentHashCode a hash for the source file being evaluated, if any
Luis Fernando Pino Duque3fedf9e2016-04-28 15:47:29 +0000458 * @param phase the current phase
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000459 */
460 private Environment(
461 Frame globalFrame,
462 Frame dynamicFrame,
463 EventHandler eventHandler,
John Field1ea7fc32015-12-22 19:37:19 +0000464 Map<String, Extension> importedExtensions,
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000465 @Nullable String fileContentHashCode,
Laurent Le Brun7c4a8092016-12-01 13:24:41 +0000466 Phase phase) {
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000467 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 Duque3fedf9e2016-04-28 15:47:29 +0000473 this.phase = phase;
Janak Ramakrishnan9b12f9c2016-05-20 16:33:02 +0000474 this.transitiveHashCode =
475 computeTransitiveContentHashCode(fileContentHashCode, importedExtensions);
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000476 }
477
478 /**
479 * A Builder class for Environment
480 */
481 public static class Builder {
482 private final Mutability mutability;
Luis Fernando Pino Duque3fedf9e2016-04-28 15:47:29 +0000483 private Phase phase = Phase.ANALYSIS;
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000484 @Nullable private Frame parent;
485 @Nullable private EventHandler eventHandler;
John Field1ea7fc32015-12-22 19:37:19 +0000486 @Nullable private Map<String, Extension> importedExtensions;
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000487 @Nullable private String fileContentHashCode;
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000488
489 Builder(Mutability mutability) {
490 this.mutability = mutability;
491 }
492
Laurent Le Bruna6d8fe42016-11-28 18:14:54 +0000493 /**
494 * Obsolete, doesn't do anything.
495 * TODO(laurentlb): To be removed once call-sites have been updated
496 */
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000497 public Builder setSkylark() {
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000498 return this;
499 }
500
Luis Fernando Pino Duque3fedf9e2016-04-28 15:47:29 +0000501 /** 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 Rideau89312fb2015-09-10 18:53:03 +0000505 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 Field1ea7fc32015-12-22 19:37:19 +0000523 public Builder setImportedExtensions (Map<String, Extension> importMap) {
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000524 Preconditions.checkState(this.importedExtensions == null);
John Field1ea7fc32015-12-22 19:37:19 +0000525 this.importedExtensions = importMap;
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000526 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 Brun46171092015-12-23 14:04:23 +0000546 return new Environment(
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000547 globalFrame,
548 dynamicFrame,
549 eventHandler,
550 importedExtensions,
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000551 fileContentHashCode,
Laurent Le Brun7c4a8092016-12-01 13:24:41 +0000552 phase);
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000553 }
554 }
555
556 public static Builder builder(Mutability mutability) {
557 return new Builder(mutability);
558 }
559
560 /**
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000561 * 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 Brun1afb0b22016-10-26 12:46:42 +0000632 public boolean hasVariable(String varname) {
Vladimir Moskva4994cb32016-08-10 15:44:31 +0000633 return lookup(varname) != null;
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000634 }
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 Nienhuysd08b27f2015-02-25 16:45:20 +0100663 }
664
665 /**
Vladimir Moskva4994cb32016-08-10 15:44:31 +0000666 * @return the value from the environment whose name is "varname" if it exists, otherwise null.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100667 */
Vladimir Moskva4994cb32016-08-10 15:44:31 +0000668 public Object lookup(String varname) {
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000669 // 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 Nienhuysd08b27f2015-02-25 16:45:20 +0100674 }
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000675 }
676 Object globalValue = globalFrame.get(varname);
677 Object dynamicValue = dynamicFrame.get(varname);
678 if (globalValue == null && dynamicValue == null) {
Vladimir Moskva4994cb32016-08-10 15:44:31 +0000679 return null;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100680 }
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000681 if (knownGlobalVariables != null) {
682 knownGlobalVariables.add(varname);
683 }
684 if (globalValue != null) {
685 return globalValue;
686 }
687 return dynamicValue;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100688 }
689
690 /**
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000691 * @return true if varname is a known global variable,
692 * because it has been read in the context of the current function.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100693 */
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000694 boolean isKnownGlobalVariable(String varname) {
695 return knownGlobalVariables != null && knownGlobalVariables.contains(varname);
696 }
697
Vladimir Moskvadfad9e32016-09-15 18:22:01 +0000698 public void handleEvent(Event event) {
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000699 eventHandler.handle(event);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100700 }
701
702 /**
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000703 * @return the (immutable) set of names of all variables defined in this
704 * Environment. Exposed for testing.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100705 */
706 @VisibleForTesting
707 public Set<String> getVariableNames() {
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000708 Set<String> vars = new HashSet<>();
709 if (lexicalFrame != null) {
710 lexicalFrame.addVariableNamesTo(vars);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100711 }
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000712 globalFrame.addVariableNamesTo(vars);
713 dynamicFrame.addVariableNamesTo(vars);
714 return vars;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100715 }
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 Rideaubd0c7bb2015-09-21 16:54:19 +0000729 return String.format("<Environment%s>", mutability());
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100730 }
731
732 /**
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000733 * An Exception thrown when an attempt is made to import a symbol from a file
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100734 * that was not properly loaded.
735 */
Janak Ramakrishnan9b12f9c2016-05-20 16:33:02 +0000736 static class LoadFailedException extends Exception {
John Field1ea7fc32015-12-22 19:37:19 +0000737 LoadFailedException(String importString) {
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000738 super(String.format("file '%s' was not correctly loaded. "
739 + "Make sure the 'load' statement appears in the global scope in your file",
Vladimir Moskva4994cb32016-08-10 15:44:31 +0000740 importString));
741 }
742
743 LoadFailedException(String importString, String symbolString) {
744 super(String.format("file '%s' does not contain symbol '%s'", importString, symbolString));
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100745 }
746 }
747
Janak Ramakrishnan9b12f9c2016-05-20 16:33:02 +0000748 void importSymbol(String importString, Identifier symbol, String nameInLoadedFile)
Vladimir Moskva4994cb32016-08-10 15:44:31 +0000749 throws LoadFailedException {
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000750 Preconditions.checkState(isGlobal()); // loading is only allowed at global scope.
751
John Field1ea7fc32015-12-22 19:37:19 +0000752 if (!importedExtensions.containsKey(importString)) {
753 throw new LoadFailedException(importString);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100754 }
Florian Weikert9d659ad2015-07-23 14:44:36 +0000755
John Field1ea7fc32015-12-22 19:37:19 +0000756 Extension ext = importedExtensions.get(importString);
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000757
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000758 if (!ext.containsKey(nameInLoadedFile)) {
Vladimir Moskva4994cb32016-08-10 15:44:31 +0000759 throw new LoadFailedException(importString, nameInLoadedFile);
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000760 }
761
762 Object value = ext.get(nameInLoadedFile);
Florian Weikert9d659ad2015-07-23 14:44:36 +0000763
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000764 try {
765 update(symbol.getName(), value);
766 } catch (EvalException e) {
John Field1ea7fc32015-12-22 19:37:19 +0000767 throw new LoadFailedException(importString);
Francois-Rene Rideau5a94e592015-09-04 19:13:47 +0000768 }
Florian Weikert6a663392015-09-02 14:04:33 +0000769 }
770
Janak Ramakrishnan9b12f9c2016-05-20 16:33:02 +0000771 private static String computeTransitiveContentHashCode(
772 @Nullable String baseHashCode, Map<String, Extension> importedExtensions) {
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000773 // Calculate a new hash from the hash of the loaded Extension-s.
774 Fingerprint fingerprint = new Fingerprint();
Janak Ramakrishnan9b12f9c2016-05-20 16:33:02 +0000775 if (baseHashCode != null) {
776 fingerprint.addString(Preconditions.checkNotNull(baseHashCode));
777 }
John Field1ea7fc32015-12-22 19:37:19 +0000778 TreeSet<String> importStrings = new TreeSet<>(importedExtensions.keySet());
779 for (String importString : importStrings) {
780 fingerprint.addString(importedExtensions.get(importString).getTransitiveContentHashCode());
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000781 }
782 return fingerprint.hexDigestAndReset();
Janak Ramakrishnanb6e33bc2015-09-06 21:05:23 +0000783 }
784
Janak Ramakrishnan9b12f9c2016-05-20 16:33:02 +0000785 /**
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 Rideau89312fb2015-09-10 18:53:03 +0000792
793 /** A read-only Environment.Frame with global constants in it only */
Janak Ramakrishnan9b12f9c2016-05-20 16:33:02 +0000794 static final Frame CONSTANTS_ONLY = createConstantsGlobals();
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000795
Laurent Le Brun5e991982016-10-14 13:39:45 +0000796 /** A read-only Environment.Frame with initial globals */
797 public static final Frame DEFAULT_GLOBALS = createDefaultGlobals();
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000798
Laurent Le Brun5e991982016-10-14 13:39:45 +0000799 /** To be removed when all call-sites are updated. */
800 public static final Frame SKYLARK = DEFAULT_GLOBALS;
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000801
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 Brun5e991982016-10-14 13:39:45 +0000810 private static Environment.Frame createDefaultGlobals() {
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000811 try (Mutability mutability = Mutability.create("BUILD")) {
812 Environment env = Environment.builder(mutability).build();
813 Runtime.setupConstants(env);
Laurent Le Brun5e991982016-10-14 13:39:45 +0000814 Runtime.setupMethodEnvironment(env, MethodLibrary.defaultGlobalFunctions);
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000815 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 Nienhuysd08b27f2015-02-25 16:45:20 +0100830}