blob: 7489bd839fa64caa8ae7837fc6ebb60599e6c5bd [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
brandjon2c98fff2018-02-06 01:32:46 -080017import com.google.common.base.Joiner;
tomlua155b532017-11-08 20:12:47 +010018import com.google.common.base.Preconditions;
Googler29eafdf2018-05-23 12:32:07 -070019import com.google.common.collect.ImmutableList;
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +000020import com.google.common.collect.ImmutableMap;
Googlerfe544aa2019-12-03 14:05:06 -080021import com.google.common.collect.Iterables;
22import com.google.common.collect.Lists;
laurentlbe5894f02018-10-25 13:02:00 -070023import com.google.common.collect.Maps;
brandjon2c98fff2018-02-06 01:32:46 -080024import com.google.common.collect.Sets;
brandjon357ed7a2017-05-09 11:37:22 -040025import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +000026import com.google.devtools.build.lib.events.Event;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010027import com.google.devtools.build.lib.events.EventHandler;
Francois-Rene Rideau3a8ddcc2015-08-26 22:30:38 +000028import com.google.devtools.build.lib.events.Location;
janakr9ba46f82018-03-13 13:07:52 -070029import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec;
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +000030import com.google.devtools.build.lib.syntax.Mutability.Freezable;
31import com.google.devtools.build.lib.syntax.Mutability.MutabilityException;
32import com.google.devtools.build.lib.util.Fingerprint;
33import com.google.devtools.build.lib.util.Pair;
Laurent Le Bruna2501d52017-01-02 15:42:19 +000034import com.google.devtools.build.lib.util.SpellChecker;
brandjon2c98fff2018-02-06 01:32:46 -080035import java.util.ArrayList;
Googler0c8f22e2019-08-28 07:10:45 -070036import java.util.HashMap;
Jon Brandveinded4fbb2017-01-18 22:21:04 +000037import java.util.LinkedHashMap;
brandjon73e768e2018-02-13 12:26:52 -080038import java.util.LinkedHashSet;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010039import java.util.Map;
brandjone821fb92017-07-18 21:00:22 +020040import java.util.Objects;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010041import java.util.Set;
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +000042import java.util.TreeSet;
Googler59100932019-09-12 07:24:20 -070043import java.util.function.Predicate;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010044import javax.annotation.Nullable;
45
46/**
Googlera3421e22019-09-26 06:48:32 -070047 * An StarlarkThread represents a Starlark thread.
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +000048 *
Googler0c8f22e2019-08-28 07:10:45 -070049 * <p>It holds the stack of active Starlark and built-in function calls. In addition, it may hold
50 * per-thread application state (see {@link #setThreadLocal}) that passes through Starlark functions
51 * but does not directly affect them, such as information about the BUILD file being loaded.
brandjon0adb7842017-09-28 10:58:43 -040052 *
Googlera3421e22019-09-26 06:48:32 -070053 * <p>Every {@code StarlarkThread} has a {@link Mutability} field, and must be used within a
54 * function that creates and closes this {@link Mutability} with the try-with-resource pattern. This
55 * {@link Mutability} is also used when initializing mutable objects within that {@code
56 * StarlarkThread}. When the {@code Mutability} is closed at the end of the computation, it freezes
57 * the {@code StarlarkThread} along with all of those objects. This pattern enforces the discipline
58 * that there should be no dangling mutable {@code StarlarkThread}, or concurrency between
59 * interacting {@code StarlarkThread}s. It is a Skylark-level error to attempt to mutate a frozen
60 * {@code StarlarkThread} or its objects, but it is a Java-level error to attempt to mutate an
61 * unfrozen {@code StarlarkThread} or its objects from within a different {@code StarlarkThread}.
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +000062 *
Googlerbffb6872019-10-22 11:14:46 -070063 * <p>One creates an StarlarkThread using the {@link #builder} function, before evaluating code in
64 * it with {@link StarlarkFile#eval}, or with {@link StarlarkFile#exec} (where the AST was obtained
65 * by passing a {@link ValidationEnvironment} constructed from the StarlarkThread to {@link
66 * StarlarkFile#parse}. When the computation is over, the frozen StarlarkThread can still be queried
67 * with {@link #lookup}.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010068 */
Googlera3421e22019-09-26 06:48:32 -070069// TODO(adonovan): further steps for StarlarkThread remediation:
Googler0c8f22e2019-08-28 07:10:45 -070070// Its API should expose the following concepts, and no more:
71// 1) "thread local variables": this holds per-thread application
72// state such as the current Label, or BUILD package, for all the
73// native.* built-ins.
74// This may include any thread-specific behaviour relevant to the
75// load and print statements.
76// 2) a stack of call frames, each representing an active function call.
77// Only clients needing debugger-like powers of reflection should need
78// this, such as the debugger itself, and the ill-conceived
79// generator_name attribute. The API for call frames should not
80// expose an object of class CallFrame, because for efficiency we
81// will want to recycle objects in place rather than generate garbage
82// on every call.
83// So the API will look like getCallerLocation(depth),
84// not getCaller(depth).location, with one method per "public" CallFrame
85// attribute, such as location.
86// We must expose these basic CallFrame attributes, for stack traces and errors:
87// - function name
88// - PC location
89// Advanced clients such as the debugger, and the generator_name rule attribute, also need:
90// - the function value (Warning: careless clients can pin closures in memory)
91// - Object getLocalValue(Identifier parameter).
Googlera3421e22019-09-26 06:48:32 -070092// 3) Debugging support (thread name, profiling counters, etc).
Googler0c8f22e2019-08-28 07:10:45 -070093// And that is all. See go.starlark.net for the model.
94//
Googlerfe544aa2019-12-03 14:05:06 -080095// The Frame interface should eliminated.
Googler0c8f22e2019-08-28 07:10:45 -070096// As best I can tell, all the skyframe serialization
97// as it applies to LexicalFrames is redundant, as these are transient
98// and should not exist after loading.
99// We will remove the FuncallExpression parameter from StarlarkFunction.call.
100// Clients should use getCallerLocation instead.
Googlerfe544aa2019-12-03 14:05:06 -0800101// The only place that still needs an AST is Bazel's generator_name.
Googler0c8f22e2019-08-28 07:10:45 -0700102// Once the API is small and sound, we can start to represent all
103// the lexical frames within a single function using just an array,
104// indexed by a small integer computed during the validation pass.
Googlera3421e22019-09-26 06:48:32 -0700105public final class StarlarkThread implements Freezable {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100106
Luis Fernando Pino Duque3fedf9e2016-04-28 15:47:29 +0000107 /**
laurentlbcaafe302018-08-28 10:24:31 -0700108 * A mapping of bindings, either mutable or immutable according to an associated {@link
109 * Mutability}. The order of the bindings within a single {@link Frame} is deterministic but
110 * unspecified.
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000111 *
nharmata68cc06b2018-03-01 10:21:57 -0800112 * <p>Any non-frozen {@link Frame} must have the same {@link Mutability} as the current {@link
Googlera3421e22019-09-26 06:48:32 -0700113 * StarlarkThread}, to avoid interference from other evaluation contexts. For example, a {@link
114 * StarlarkFunction} will close over the global frame of the {@link StarlarkThread} in which it
115 * was defined. When the function is called from other {@link StarlarkThread}s (possibly
116 * simultaneously), that global frame must already be frozen; a new local {@link Frame} is created
117 * to represent the lexical scope of the function.
brandjonadda5122017-08-29 18:29:58 +0200118 *
nharmata68cc06b2018-03-01 10:21:57 -0800119 * <p>A {@link Frame} can have an associated "parent" {@link Frame}, which is used in {@link #get}
120 * and {@link #getTransitiveBindings()}
laurentlbc8e67962018-08-31 14:20:40 -0700121 *
122 * <p>TODO(laurentlb): "parent" should be named "universe" since it contains only the builtins.
123 * The "get" method shouldn't look at the universe (so that "moduleLookup" works as expected)
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100124 */
Googler41c333f2019-10-15 12:12:24 -0700125 interface Frame extends Freezable {
nharmata68cc06b2018-03-01 10:21:57 -0800126 /**
127 * Gets a binding from this {@link Frame} or one of its transitive parents.
128 *
129 * <p>In case of conflicts, the binding found in the {@link Frame} closest to the current one is
130 * used; the remaining bindings are shadowed.
131 *
132 * @param varname the name of the variable whose value should be retrieved
133 * @return the value bound to the variable, or null if no binding is found
134 */
135 @Nullable
136 Object get(String varname);
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000137
brandjonadda5122017-08-29 18:29:58 +0200138 /**
nharmata68cc06b2018-03-01 10:21:57 -0800139 * Assigns or reassigns a binding in the current {@code Frame}.
140 *
141 * <p>If the binding has the same name as one in a transitive parent, the parent binding is
142 * shadowed (i.e., the parent is unaffected).
143 *
nharmata68cc06b2018-03-01 10:21:57 -0800144 * @param varname the name of the variable to be bound
145 * @param value the value to bind to the variable
146 */
Googler48f6dda2019-10-16 14:10:09 -0700147 void put(String varname, Object value) throws MutabilityException;
nharmata68cc06b2018-03-01 10:21:57 -0800148
149 /**
laurentlbcaafe302018-08-28 10:24:31 -0700150 * TODO(laurentlb): Remove this method when possible. It should probably not be part of the
151 * public interface.
nharmata68cc06b2018-03-01 10:21:57 -0800152 */
Googler48f6dda2019-10-16 14:10:09 -0700153 void remove(String varname) throws MutabilityException;
nharmata68cc06b2018-03-01 10:21:57 -0800154
155 /**
laurentlbcaafe302018-08-28 10:24:31 -0700156 * Returns a map containing all bindings of this {@link Frame} and of its transitive parents,
157 * taking into account shadowing precedence.
nharmata68cc06b2018-03-01 10:21:57 -0800158 *
159 * <p>The bindings are returned in a deterministic order (for a given sequence of initial values
160 * and updates).
161 */
162 Map<String, Object> getTransitiveBindings();
163 }
164
165 interface LexicalFrame extends Frame {
nharmata68cc06b2018-03-01 10:21:57 -0800166 }
167
168 private static final class ImmutableEmptyLexicalFrame implements LexicalFrame {
169 private static final ImmutableEmptyLexicalFrame INSTANCE = new ImmutableEmptyLexicalFrame();
170
171 @Override
172 public Mutability mutability() {
173 return Mutability.IMMUTABLE;
174 }
175
176 @Nullable
177 @Override
178 public Object get(String varname) {
179 return null;
180 }
181
182 @Override
Googler48f6dda2019-10-16 14:10:09 -0700183 public void put(String varname, Object value) throws MutabilityException {
184 Mutability.checkMutable(this, mutability());
nharmata68cc06b2018-03-01 10:21:57 -0800185 throw new IllegalStateException();
186 }
187
188 @Override
Googler48f6dda2019-10-16 14:10:09 -0700189 public void remove(String varname) throws MutabilityException {
190 Mutability.checkMutable(this, mutability());
nharmata68cc06b2018-03-01 10:21:57 -0800191 throw new IllegalStateException();
192 }
193
194 @Override
195 public Map<String, Object> getTransitiveBindings() {
196 return ImmutableMap.of();
197 }
198
199 @Override
200 public String toString() {
201 return "<ImmutableEmptyLexicalFrame>";
202 }
203 }
204
205 private static final class MutableLexicalFrame implements LexicalFrame {
206 private final Mutability mutability;
207 /** Bindings are maintained in order of creation. */
nharmata62678a42018-03-08 16:43:45 -0800208 private final LinkedHashMap<String, Object> bindings;
nharmata68cc06b2018-03-01 10:21:57 -0800209
nharmata62678a42018-03-08 16:43:45 -0800210 private MutableLexicalFrame(Mutability mutability, int initialCapacity) {
nharmata68cc06b2018-03-01 10:21:57 -0800211 this.mutability = mutability;
laurentlbe5894f02018-10-25 13:02:00 -0700212 this.bindings = Maps.newLinkedHashMapWithExpectedSize(initialCapacity);
nharmata62678a42018-03-08 16:43:45 -0800213 }
214
215 private MutableLexicalFrame(Mutability mutability) {
216 this.mutability = mutability;
217 this.bindings = new LinkedHashMap<>();
nharmata68cc06b2018-03-01 10:21:57 -0800218 }
219
220 @Override
221 public Mutability mutability() {
222 return mutability;
223 }
224
225 @Nullable
226 @Override
227 public Object get(String varname) {
228 return bindings.get(varname);
229 }
230
231 @Override
Googler48f6dda2019-10-16 14:10:09 -0700232 public void put(String varname, Object value) throws MutabilityException {
233 Mutability.checkMutable(this, mutability());
nharmata68cc06b2018-03-01 10:21:57 -0800234 bindings.put(varname, value);
235 }
236
237 @Override
Googler48f6dda2019-10-16 14:10:09 -0700238 public void remove(String varname) throws MutabilityException {
239 Mutability.checkMutable(this, mutability());
nharmata68cc06b2018-03-01 10:21:57 -0800240 bindings.remove(varname);
241 }
242
243 @Override
244 public Map<String, Object> getTransitiveBindings() {
245 return bindings;
246 }
247
248 @Override
249 public String toString() {
250 return String.format("<MutableLexicalFrame%s>", mutability());
251 }
252 }
253
Googlera3421e22019-09-26 06:48:32 -0700254 // The mutability of the StarlarkThread comes from its initial global frame.
Googler0c8f22e2019-08-28 07:10:45 -0700255 private final Mutability mutability;
256
257 private final Map<Class<?>, Object> threadLocals = new HashMap<>();
258
259 /**
260 * setThreadLocal saves {@code value} as a thread-local variable of this Starlark thread, keyed by
261 * {@code key}, so that it can later be retrieved by {@code getThreadLocal(key)}.
262 */
263 public <T> void setThreadLocal(Class<T> key, T value) {
264 // The clazz parameter is redundant, but it makes the API clearer.
265 threadLocals.put(key, value);
266 }
267
268 /**
269 * getThreadLocal returns the value {@code v} supplied to the most recent {@code
270 * setThreadLocal(key, v)} call, or null if there was no prior call.
271 */
272 public <T> T getThreadLocal(Class<T> key) {
273 Object v = threadLocals.get(key);
274 return v == null ? null : key.cast(v);
275 }
276
Googlerfe544aa2019-12-03 14:05:06 -0800277 /** A CallFrame records information about an active function call. */
278 // TODO(adonovan): merge LexicalFrame into CallFrame. Every function call should have a frame,
279 // but only Starlark functions need local variables.
280 private static final class CallFrame {
281 final StarlarkCallable fn; // the called function
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000282
Googlerfe544aa2019-12-03 14:05:06 -0800283 // Note that the inherited design is off-by-one:
284 // the following fields are logically facts about the _enclosing_ frame.
285 // This is a consequence of not representing toplevel statements as a function.
286 // TODO(adonovan): fix that.
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000287
Googlerfe544aa2019-12-03 14:05:06 -0800288 final Location callerLoc; // location of the enclosing call (may be Location.BUILTIN)
289 @Nullable final FuncallExpression call; // syntax of the enclosing call
290 final Frame savedLexicals; // the saved lexicals of the parent
291 final Module savedModule; // the saved module of the parent (TODO(adonovan): eliminate)
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000292
Googlerfe544aa2019-12-03 14:05:06 -0800293 CallFrame(
294 StarlarkCallable fn,
295 Location callerLoc,
296 @Nullable FuncallExpression call,
297 Frame savedLexicals,
298 Module savedModule) {
299 this.fn = fn;
300 this.callerLoc = callerLoc;
301 this.call = call;
302 this.savedLexicals = savedLexicals;
303 this.savedModule = savedModule;
304 }
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000305
Googlerfe544aa2019-12-03 14:05:06 -0800306 @Override
307 public String toString() {
308 return fn.getName() + "@" + callerLoc;
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000309 }
310 }
311
brandjon357ed7a2017-05-09 11:37:22 -0400312 /** An Extension to be imported with load() into a BUILD or .bzl file. */
313 @Immutable
janakr9ba46f82018-03-13 13:07:52 -0700314 // TODO(janakr,brandjon): Do Extensions actually have to start their own memoization? Or can we
315 // have a node higher up in the hierarchy inject the mutability?
shahan9012e6f2018-04-02 13:26:14 -0700316 @AutoCodec
brandjone821fb92017-07-18 21:00:22 +0200317 public static final class Extension {
318
319 private final ImmutableMap<String, Object> bindings;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100320
brandjon357ed7a2017-05-09 11:37:22 -0400321 /**
322 * Cached hash code for the transitive content of this {@code Extension} and its dependencies.
brandjon2c98fff2018-02-06 01:32:46 -0800323 *
324 * <p>Note that "content" refers to the AST content, not the evaluated bindings.
brandjon357ed7a2017-05-09 11:37:22 -0400325 */
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000326 private final String transitiveContentHashCode;
327
brandjone821fb92017-07-18 21:00:22 +0200328 /** Constructs with the given hash code and bindings. */
janakr9ba46f82018-03-13 13:07:52 -0700329 @AutoCodec.Instantiator
brandjone821fb92017-07-18 21:00:22 +0200330 public Extension(ImmutableMap<String, Object> bindings, String transitiveContentHashCode) {
331 this.bindings = bindings;
brandjon357ed7a2017-05-09 11:37:22 -0400332 this.transitiveContentHashCode = transitiveContentHashCode;
333 }
334
335 /**
Googlera3421e22019-09-26 06:48:32 -0700336 * Constructs using the bindings from the global definitions of the given {@link
337 * StarlarkThread}, and that {@code StarlarkThread}'s transitive hash code.
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000338 */
Googlera3421e22019-09-26 06:48:32 -0700339 public Extension(StarlarkThread thread) {
laurentlb9d179e12018-09-27 08:15:42 -0700340 // Legacy behavior: all symbols from the global Frame are exported (including symbols
341 // introduced by load).
342 this(
laurentlbcdac07f2019-11-07 15:40:58 -0800343 ImmutableMap.copyOf(thread.globalFrame.getExportedBindings()),
Googlera3421e22019-09-26 06:48:32 -0700344 thread.getTransitiveContentHashCode());
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000345 }
346
Googler7fa0dd22019-09-24 20:07:53 -0700347 private String getTransitiveContentHashCode() {
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000348 return transitiveContentHashCode;
349 }
350
brandjon73e768e2018-02-13 12:26:52 -0800351 /** Retrieves all bindings, in a deterministic order. */
brandjon357ed7a2017-05-09 11:37:22 -0400352 public ImmutableMap<String, Object> getBindings() {
353 return bindings;
Laurent Le Bruna2501d52017-01-02 15:42:19 +0000354 }
brandjone821fb92017-07-18 21:00:22 +0200355
356 @Override
357 public boolean equals(Object obj) {
358 if (this == obj) {
359 return true;
360 }
361 if (!(obj instanceof Extension)) {
362 return false;
363 }
364 Extension other = (Extension) obj;
365 return transitiveContentHashCode.equals(other.getTransitiveContentHashCode())
366 && bindings.equals(other.getBindings());
367 }
368
janakr33439dc2018-03-22 13:44:05 -0700369 private static boolean skylarkObjectsProbablyEqual(Object obj1, Object obj2) {
370 // TODO(b/76154791): check this more carefully.
371 return obj1.equals(obj2)
Googler34f70582019-11-25 12:27:34 -0800372 || (obj1 instanceof StarlarkValue
373 && obj2 instanceof StarlarkValue
Googlerb1e232d2019-11-22 15:29:43 -0800374 && Starlark.repr(obj1).equals(Starlark.repr(obj2)));
janakr33439dc2018-03-22 13:44:05 -0700375 }
376
brandjon2c98fff2018-02-06 01:32:46 -0800377 /**
378 * Throws {@link IllegalStateException} if this {@link Extension} is not equal to {@code obj}.
379 *
380 * <p>The exception explains the reason for the inequality, including all unequal bindings.
381 */
382 public void checkStateEquals(Object obj) {
383 if (this == obj) {
384 return;
385 }
386 if (!(obj instanceof Extension)) {
laurentlbcaafe302018-08-28 10:24:31 -0700387 throw new IllegalStateException(
388 String.format(
389 "Expected an equal Extension, but got a %s instead of an Extension",
390 obj == null ? "null" : obj.getClass().getName()));
brandjon2c98fff2018-02-06 01:32:46 -0800391 }
392 Extension other = (Extension) obj;
393 ImmutableMap<String, Object> otherBindings = other.getBindings();
394
395 Set<String> names = bindings.keySet();
396 Set<String> otherNames = otherBindings.keySet();
397 if (!names.equals(otherNames)) {
laurentlbcaafe302018-08-28 10:24:31 -0700398 throw new IllegalStateException(
399 String.format(
400 "Expected Extensions to be equal, but they don't define the same bindings: "
401 + "in this one but not given one: [%s]; in given one but not this one: [%s]",
402 Joiner.on(", ").join(Sets.difference(names, otherNames)),
403 Joiner.on(", ").join(Sets.difference(otherNames, names))));
brandjon2c98fff2018-02-06 01:32:46 -0800404 }
405
406 ArrayList<String> badEntries = new ArrayList<>();
407 for (String name : names) {
408 Object value = bindings.get(name);
409 Object otherValue = otherBindings.get(name);
janakr889f5622018-03-16 17:49:11 -0700410 if (value.equals(otherValue)) {
411 continue;
brandjon2c98fff2018-02-06 01:32:46 -0800412 }
Googlerd21a0d12019-11-21 13:52:30 -0800413 if (value instanceof Depset) {
414 if (otherValue instanceof Depset
415 && ((Depset) value).toCollection().equals(((Depset) otherValue).toCollection())) {
janakr33439dc2018-03-22 13:44:05 -0700416 continue;
417 }
Googlera9c93632019-11-13 10:48:07 -0800418 } else if (value instanceof Dict) {
419 if (otherValue instanceof Dict) {
janakr33439dc2018-03-22 13:44:05 -0700420 @SuppressWarnings("unchecked")
Googlera9c93632019-11-13 10:48:07 -0800421 Dict<Object, Object> thisDict = (Dict<Object, Object>) value;
janakr33439dc2018-03-22 13:44:05 -0700422 @SuppressWarnings("unchecked")
Googlera9c93632019-11-13 10:48:07 -0800423 Dict<Object, Object> otherDict = (Dict<Object, Object>) otherValue;
janakr33439dc2018-03-22 13:44:05 -0700424 if (thisDict.size() == otherDict.size()
425 && thisDict.keySet().equals(otherDict.keySet())) {
426 boolean foundProblem = false;
427 for (Object key : thisDict.keySet()) {
428 if (!skylarkObjectsProbablyEqual(
429 Preconditions.checkNotNull(thisDict.get(key), key),
430 Preconditions.checkNotNull(otherDict.get(key), key))) {
431 foundProblem = true;
432 }
433 }
434 if (!foundProblem) {
435 continue;
436 }
437 }
438 }
439 } else if (skylarkObjectsProbablyEqual(value, otherValue)) {
janakr889f5622018-03-16 17:49:11 -0700440 continue;
441 }
442 badEntries.add(
443 String.format(
janakrcfc34322018-03-26 11:10:08 -0700444 "%s: this one has %s (class %s, %s), but given one has %s (class %s, %s)",
janakr889f5622018-03-16 17:49:11 -0700445 name,
Googlerb1e232d2019-11-22 15:29:43 -0800446 Starlark.repr(value),
janakr889f5622018-03-16 17:49:11 -0700447 value.getClass().getName(),
janakrcfc34322018-03-26 11:10:08 -0700448 value,
Googlerb1e232d2019-11-22 15:29:43 -0800449 Starlark.repr(otherValue),
janakrcfc34322018-03-26 11:10:08 -0700450 otherValue.getClass().getName(),
451 otherValue));
brandjon2c98fff2018-02-06 01:32:46 -0800452 }
453 if (!badEntries.isEmpty()) {
454 throw new IllegalStateException(
455 "Expected Extensions to be equal, but the following bindings are unequal: "
456 + Joiner.on("; ").join(badEntries));
457 }
458
459 if (!transitiveContentHashCode.equals(other.getTransitiveContentHashCode())) {
laurentlbcaafe302018-08-28 10:24:31 -0700460 throw new IllegalStateException(
461 String.format(
462 "Expected Extensions to be equal, but transitive content hashes don't match:"
463 + " %s != %s",
464 transitiveContentHashCode, other.getTransitiveContentHashCode()));
brandjon2c98fff2018-02-06 01:32:46 -0800465 }
466 }
467
brandjone821fb92017-07-18 21:00:22 +0200468 @Override
469 public int hashCode() {
470 return Objects.hash(bindings, transitiveContentHashCode);
471 }
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000472 }
473
Googlerfe544aa2019-12-03 14:05:06 -0800474 // Local environment of the current active call,
475 // or an alias for globalFrame if no calls are active.
476 // TODO(adonovan): redundant with callstack; eliminate once we fix off-by-one problem.
laurentlb5d26ef12018-08-27 12:23:15 -0700477 private Frame lexicalFrame;
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000478
Googlerfe544aa2019-12-03 14:05:06 -0800479 // Global environment of the current topmost call frame,
480 // or of the file about to be initialized if no calls are active.
481 // TODO(adonovan): eliminate once we represent even toplevel statements
482 // as a StarlarkFunction that closes over its Module.
Googler41c333f2019-10-15 12:12:24 -0700483 private Module globalFrame;
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000484
laurentlbcaafe302018-08-28 10:24:31 -0700485 /** The semantics options that affect how Skylark code is evaluated. */
laurentlb6659b4c2019-02-18 07:23:36 -0800486 private final StarlarkSemantics semantics;
brandjonb712f332017-04-29 16:03:32 +0200487
488 /**
laurentlbcaafe302018-08-28 10:24:31 -0700489 * An EventHandler for errors and warnings. This is not used in the BUILD language, however it
490 * might be used in Skylark code called from the BUILD language, so shouldn't be null.
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000491 */
492 private final EventHandler eventHandler;
493
494 /**
John Field1ea7fc32015-12-22 19:37:19 +0000495 * For each imported extension, a global Skylark frame from which to load() individual bindings.
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000496 */
John Field1ea7fc32015-12-22 19:37:19 +0000497 private final Map<String, Extension> importedExtensions;
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000498
Googlerfe544aa2019-12-03 14:05:06 -0800499 /** Stack of active function calls. */
500 // TODO(adonovan): currently off by one because top-level statements don't have a CallFrame.
501 private final ArrayList<CallFrame> callstack = new ArrayList<>();
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000502
Googlerba868542019-10-09 07:26:27 -0700503 /** A hook for notifications of assignments at top level. */
504 PostAssignHook postAssignHook;
505
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000506 /**
Googlerfe544aa2019-12-03 14:05:06 -0800507 * Pushes a function onto the call stack.
brendandouglasfe785e12018-06-12 09:39:38 -0700508 *
Googlerfe544aa2019-12-03 14:05:06 -0800509 * @param fn the function whose scope to enter
510 * @param loc the source location of the function call.
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000511 */
Googlerfe544aa2019-12-03 14:05:06 -0800512 void push(StarlarkCallable fn, Location loc, @Nullable FuncallExpression call) {
513 callstack.add(new CallFrame(fn, loc, call, this.lexicalFrame, this.globalFrame));
514
515 if (fn instanceof StarlarkFunction) {
516 StarlarkFunction sfn = (StarlarkFunction) fn;
517 this.lexicalFrame =
518 new MutableLexicalFrame(
519 this.mutability(), /*initialCapacity=*/ sfn.getSignature().numParameters());
520 this.globalFrame = sfn.getModule();
521 } else {
522 // built-in function
523 this.lexicalFrame = DUMMY_LEXICAL_FRAME;
524 // this.globalFrame is left as is.
525 // For built-ins, thread.globals() returns the module
526 // of the file from which the built-in was called.
527 // Really they have no business knowing about that.
528 }
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000529 }
530
Googlerfe544aa2019-12-03 14:05:06 -0800531 /** Pops a function off the call stack. */
532 void pop() {
533 int last = callstack.size() - 1;
534 CallFrame top = callstack.get(last);
535 callstack.remove(last); // pop
536 this.lexicalFrame = top.savedLexicals;
537 this.globalFrame = top.savedModule;
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000538 }
539
Googlerfe544aa2019-12-03 14:05:06 -0800540 // Builtins cannot create or modify variable bindings,
541 // so it's sufficient to use a shared instance.
542 private static final LexicalFrame DUMMY_LEXICAL_FRAME = ImmutableEmptyLexicalFrame.INSTANCE;
543
Janak Ramakrishnan9b12f9c2016-05-20 16:33:02 +0000544 private final String transitiveHashCode;
Francois-Rene Rideau3a8ddcc2015-08-26 22:30:38 +0000545
546 /**
Googlera3421e22019-09-26 06:48:32 -0700547 * Is this a global StarlarkThread?
laurentlbcaafe302018-08-28 10:24:31 -0700548 *
549 * @return true if the current code is being executed at the top-level, as opposed to inside the
550 * body of a function.
Francois-Rene Rideau6e7160d2015-08-26 17:22:35 +0000551 */
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000552 boolean isGlobal() {
Googler41c333f2019-10-15 12:12:24 -0700553 return lexicalFrame instanceof Module;
Francois-Rene Rideau6e7160d2015-08-26 17:22:35 +0000554 }
555
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000556 @Override
557 public Mutability mutability() {
Googler0c8f22e2019-08-28 07:10:45 -0700558 return mutability;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100559 }
560
Googlera3421e22019-09-26 06:48:32 -0700561 /** Returns the global variables for the StarlarkThread (not including dynamic bindings). */
Googler41c333f2019-10-15 12:12:24 -0700562 public Module getGlobals() {
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000563 return globalFrame;
564 }
565
566 /**
laurentlbcaafe302018-08-28 10:24:31 -0700567 * Returns an EventHandler for errors and warnings. The BUILD language doesn't use it directly,
568 * but can call Skylark code that does use it.
569 *
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000570 * @return an EventHandler
571 */
Googler7fa0dd22019-09-24 20:07:53 -0700572 // TODO(adonovan): turn this into a print handler and break dependency on EventHandler.
573 // First, we must report scan/parse/validation errors using an exception containing events.
Francois-Rene Rideaue6a46b82015-04-10 18:31:43 +0000574 public EventHandler getEventHandler() {
575 return eventHandler;
576 }
577
michajlocbb33472017-10-23 20:30:18 +0200578 /**
579 * Returns if calling the supplied function would be a recursive call, or in other words if the
580 * supplied function is already on the stack.
581 */
Googler3fcfbe12019-08-28 08:10:11 -0700582 boolean isRecursiveCall(StarlarkFunction function) {
Googlerfe544aa2019-12-03 14:05:06 -0800583 for (CallFrame fr : callstack) {
Googler0c8f22e2019-08-28 07:10:45 -0700584 // TODO(adonovan): compare code, not closure values, otherwise
585 // one can defeat this check by writing the Y combinator.
Googlerfe544aa2019-12-03 14:05:06 -0800586 if (fr.fn.equals(function)) {
michajlocbb33472017-10-23 20:30:18 +0200587 return true;
588 }
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000589 }
michajlocbb33472017-10-23 20:30:18 +0200590 return false;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100591 }
592
Googlerfe544aa2019-12-03 14:05:06 -0800593 /** Returns the current called function, which must exist. */
594 StarlarkCallable getCurrentFunction() {
595 return Iterables.getLast(callstack).fn;
michajlocbb33472017-10-23 20:30:18 +0200596 }
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000597
Googlerfe544aa2019-12-03 14:05:06 -0800598 /** Returns the call expression and called function for the outermost call being evaluated. */
599 // TODO(adonovan): replace this by an API for walking the call stack, then move to lib.packages.
600 public Pair<FuncallExpression, StarlarkCallable> getOutermostCall() {
601 if (callstack.isEmpty()) {
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000602 return null;
603 }
Googlerfe544aa2019-12-03 14:05:06 -0800604 CallFrame outermost = callstack.get(0);
605 return new Pair<>(outermost.call, outermost.fn);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100606 }
607
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000608 /**
Googlera3421e22019-09-26 06:48:32 -0700609 * Constructs an StarlarkThread. This is the main, most basic constructor.
Laurent Le Bruna31bc4e2016-10-27 12:48:22 +0000610 *
Googlera3421e22019-09-26 06:48:32 -0700611 * @param globalFrame a frame for the global StarlarkThread
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000612 * @param eventHandler an EventHandler for warnings, errors, etc
613 * @param importedExtensions Extension-s from which to import bindings with load()
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000614 * @param fileContentHashCode a hash for the source file being evaluated, if any
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000615 */
Googlera3421e22019-09-26 06:48:32 -0700616 private StarlarkThread(
Googler41c333f2019-10-15 12:12:24 -0700617 Module globalFrame,
laurentlb6659b4c2019-02-18 07:23:36 -0800618 StarlarkSemantics semantics,
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000619 EventHandler eventHandler,
John Field1ea7fc32015-12-22 19:37:19 +0000620 Map<String, Extension> importedExtensions,
Googler38be9dd2019-09-23 15:34:57 -0700621 @Nullable String fileContentHashCode) {
laurentlb5d26ef12018-08-27 12:23:15 -0700622 this.lexicalFrame = Preconditions.checkNotNull(globalFrame);
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000623 this.globalFrame = Preconditions.checkNotNull(globalFrame);
Googler0c8f22e2019-08-28 07:10:45 -0700624 this.mutability = globalFrame.mutability();
brandjon33b49432017-05-04 18:58:52 +0200625 Preconditions.checkArgument(!globalFrame.mutability().isFrozen());
brandjonb712f332017-04-29 16:03:32 +0200626 this.semantics = semantics;
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000627 this.eventHandler = eventHandler;
628 this.importedExtensions = importedExtensions;
Janak Ramakrishnan9b12f9c2016-05-20 16:33:02 +0000629 this.transitiveHashCode =
630 computeTransitiveContentHashCode(fileContentHashCode, importedExtensions);
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000631 }
632
633 /**
Googlera3421e22019-09-26 06:48:32 -0700634 * A Builder class for StarlarkThread.
brandjond9a1a432017-10-19 23:47:08 +0200635 *
636 * <p>The caller must explicitly set the semantics by calling either {@link #setSemantics} or
637 * {@link #useDefaultSemantics}.
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000638 */
639 public static class Builder {
640 private final Mutability mutability;
Googler41c333f2019-10-15 12:12:24 -0700641 @Nullable private Module parent;
laurentlb6659b4c2019-02-18 07:23:36 -0800642 @Nullable private StarlarkSemantics semantics;
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000643 @Nullable private EventHandler eventHandler;
John Field1ea7fc32015-12-22 19:37:19 +0000644 @Nullable private Map<String, Extension> importedExtensions;
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000645 @Nullable private String fileContentHashCode;
646
647 Builder(Mutability mutability) {
648 this.mutability = mutability;
649 }
650
laurentlbf6350622018-09-19 10:15:34 -0700651 /**
652 * Inherits global bindings from the given parent Frame.
653 *
654 * <p>TODO(laurentlb): this should be called setUniverse.
655 */
Googler41c333f2019-10-15 12:12:24 -0700656 public Builder setGlobals(Module parent) {
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000657 Preconditions.checkState(this.parent == null);
nharmata6070ba72018-03-01 13:35:33 -0800658 this.parent = parent;
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000659 return this;
660 }
661
laurentlb6659b4c2019-02-18 07:23:36 -0800662 public Builder setSemantics(StarlarkSemantics semantics) {
brandjonb712f332017-04-29 16:03:32 +0200663 this.semantics = semantics;
664 return this;
665 }
666
brandjond9a1a432017-10-19 23:47:08 +0200667 public Builder useDefaultSemantics() {
laurentlb6659b4c2019-02-18 07:23:36 -0800668 this.semantics = StarlarkSemantics.DEFAULT_SEMANTICS;
brandjond9a1a432017-10-19 23:47:08 +0200669 return this;
670 }
671
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000672 /** Sets an EventHandler for errors and warnings. */
673 public Builder setEventHandler(EventHandler eventHandler) {
674 Preconditions.checkState(this.eventHandler == null);
675 this.eventHandler = eventHandler;
676 return this;
677 }
678
679 /** Declares imported extensions for load() statements. */
laurentlbcaafe302018-08-28 10:24:31 -0700680 public Builder setImportedExtensions(Map<String, Extension> importMap) {
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000681 Preconditions.checkState(this.importedExtensions == null);
John Field1ea7fc32015-12-22 19:37:19 +0000682 this.importedExtensions = importMap;
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000683 return this;
684 }
685
Googlera3421e22019-09-26 06:48:32 -0700686 /** Declares content hash for the source file for this StarlarkThread. */
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000687 public Builder setFileContentHashCode(String fileContentHashCode) {
688 this.fileContentHashCode = fileContentHashCode;
689 return this;
690 }
691
Googlera3421e22019-09-26 06:48:32 -0700692 /** Builds the StarlarkThread. */
693 public StarlarkThread build() {
brandjon33b49432017-05-04 18:58:52 +0200694 Preconditions.checkArgument(!mutability.isFrozen());
cparsons6622e6f2018-10-17 15:00:09 -0700695 if (semantics == null) {
696 throw new IllegalArgumentException("must call either setSemantics or useDefaultSemantics");
697 }
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000698 if (parent != null) {
brandjon73e768e2018-02-13 12:26:52 -0800699 Preconditions.checkArgument(parent.mutability().isFrozen(), "parent frame must be frozen");
laurentlbf6350622018-09-19 10:15:34 -0700700 if (parent.universe != null) { // This code path doesn't happen in Bazel.
laurentlbc8e67962018-08-31 14:20:40 -0700701
702 // Flatten the frame, ensure all builtins are in the same frame.
703 parent =
Googler41c333f2019-10-15 12:12:24 -0700704 new Module(
laurentlbc8e67962018-08-31 14:20:40 -0700705 parent.mutability(),
706 null /* parent */,
707 parent.label,
cparsons6622e6f2018-10-17 15:00:09 -0700708 parent.getTransitiveBindings(),
709 parent.restrictedBindings);
laurentlbc8e67962018-08-31 14:20:40 -0700710 }
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000711 }
cparsons6622e6f2018-10-17 15:00:09 -0700712
713 // Filter out restricted objects from the universe scope. This cannot be done in-place in
714 // creation of the input global universe scope, because this environment's semantics may not
715 // have been available during its creation. Thus, create a new universe scope for this
716 // environment which is equivalent in every way except that restricted bindings are
717 // filtered out.
Googler41c333f2019-10-15 12:12:24 -0700718 parent = Module.filterOutRestrictedBindings(mutability, parent, semantics);
cparsons6622e6f2018-10-17 15:00:09 -0700719
Googler41c333f2019-10-15 12:12:24 -0700720 Module globalFrame = new Module(mutability, parent);
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000721 if (importedExtensions == null) {
722 importedExtensions = ImmutableMap.of();
723 }
Googlera3421e22019-09-26 06:48:32 -0700724 return new StarlarkThread(
725 globalFrame, semantics, eventHandler, importedExtensions, fileContentHashCode);
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000726 }
727 }
728
729 public static Builder builder(Mutability mutability) {
730 return new Builder(mutability);
731 }
732
laurentlb9d5c0a02017-06-13 23:08:06 +0200733 /** Remove variable from local bindings. */
734 void removeLocalBinding(String varname) {
735 try {
Googler48f6dda2019-10-16 14:10:09 -0700736 lexicalFrame.remove(varname);
laurentlb9d5c0a02017-06-13 23:08:06 +0200737 } catch (MutabilityException e) {
738 throw new AssertionError(e);
739 }
740 }
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000741
laurentlb9d179e12018-09-27 08:15:42 -0700742 /** Modifies a binding in the current Frame. If it is the module Frame, also export it. */
Googlera3421e22019-09-26 06:48:32 -0700743 StarlarkThread updateAndExport(String varname, Object value) throws EvalException {
laurentlb9d179e12018-09-27 08:15:42 -0700744 update(varname, value);
745 if (isGlobal()) {
746 globalFrame.exportedBindings.add(varname);
747 }
748 return this;
749 }
750
nharmataf1013482018-03-07 10:48:31 -0800751 /**
Googlerba868542019-10-09 07:26:27 -0700752 * Specifies a hook function to be run after each assignment at top level.
753 *
754 * <p>This is a short-term hack to allow us to consolidate all StarlarkFile execution in one place
755 * even while SkylarkImportLookupFunction implements the old "export" behavior, in which rules,
756 * aspects and providers are "exported" as soon as they are assigned, not at the end of file
757 * execution.
758 */
759 public void setPostAssignHook(PostAssignHook postAssignHook) {
760 this.postAssignHook = postAssignHook;
761 }
762
763 /** A hook for notifications of assignments at top level. */
764 public interface PostAssignHook {
765 void assign(String name, Object value);
766 }
767
768 /**
Googlera3421e22019-09-26 06:48:32 -0700769 * Modifies a binding in the current Frame of this StarlarkThread, as would an {@link
laurentlbcaafe302018-08-28 10:24:31 -0700770 * AssignmentStatement}. Does not try to modify an inherited binding. This will shadow any
771 * inherited binding, which may be an error that you want to guard against before calling this
772 * function.
773 *
nharmataf1013482018-03-07 10:48:31 -0800774 * @param varname the name of the variable to be bound
775 * @param value the value to bind to the variable
Googlera3421e22019-09-26 06:48:32 -0700776 * @return this StarlarkThread, in fluid style
nharmataf1013482018-03-07 10:48:31 -0800777 */
Googlerd54a4992019-10-18 12:35:06 -0700778 // TODO(adonovan): eliminate sole external call from EvaluationTestCase and make private.
779 public StarlarkThread update(String varname, Object value) {
laurentlb9dc4e202018-09-06 09:34:13 -0700780 Preconditions.checkNotNull(value, "trying to assign null to '%s'", varname);
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000781 try {
Googler48f6dda2019-10-16 14:10:09 -0700782 lexicalFrame.put(varname, value);
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000783 } catch (MutabilityException e) {
784 // Note that since at this time we don't accept the global keyword, and don't have closures,
Googlera3421e22019-09-26 06:48:32 -0700785 // end users should never be able to mutate a frozen StarlarkThread, and a MutabilityException
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000786 // is therefore a failed assertion for Bazel. However, it is possible to shadow a binding
Googlera3421e22019-09-26 06:48:32 -0700787 // imported from a parent StarlarkThread by updating the current StarlarkThread, which will
788 // not trigger a MutabilityException.
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000789 throw new AssertionError(
Googlerb1e232d2019-11-22 15:29:43 -0800790 Starlark.format("Can't update %s to %r in frozen environment", varname, value), e);
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000791 }
792 return this;
793 }
794
Stepan Koltsovc1fe6c52019-10-02 03:15:36 -0700795 // Used only for Eval.evalComprehension..
796 void updateInternal(String name, @Nullable Object value) {
797 try {
798 if (value != null) {
Googler48f6dda2019-10-16 14:10:09 -0700799 lexicalFrame.put(name, value);
Stepan Koltsovc1fe6c52019-10-02 03:15:36 -0700800 } else {
Googler48f6dda2019-10-16 14:10:09 -0700801 lexicalFrame.remove(name);
Stepan Koltsovc1fe6c52019-10-02 03:15:36 -0700802 }
803 } catch (MutabilityException ex) {
804 throw new IllegalStateException(ex);
805 }
806 }
807
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000808 /**
laurentlbebb40712018-08-29 11:54:27 -0700809 * Returns the value of a variable defined in Local scope. Do not search in any parent scope. This
810 * function should be used once the AST has been analysed and we know which variables are local.
laurentlbcaafe302018-08-28 10:24:31 -0700811 */
Googler7fa0dd22019-09-24 20:07:53 -0700812 Object localLookup(String varname) {
laurentlbcaafe302018-08-28 10:24:31 -0700813 return lexicalFrame.get(varname);
814 }
815
816 /**
laurentlbc8e67962018-08-31 14:20:40 -0700817 * Returns the value of a variable defined in the Module scope (e.g. global variables, functions).
laurentlbebb40712018-08-29 11:54:27 -0700818 */
819 public Object moduleLookup(String varname) {
laurentlbf6350622018-09-19 10:15:34 -0700820 return globalFrame.getDirectBindings(varname);
laurentlbebb40712018-08-29 11:54:27 -0700821 }
822
823 /** Returns the value of a variable defined in the Universe scope (builtins). */
824 public Object universeLookup(String varname) {
laurentlbf6350622018-09-19 10:15:34 -0700825 // TODO(laurentlb): look only at globalFrame.universe.
laurentlb2b3ad182018-12-05 05:49:45 -0800826 return globalFrame.get(varname);
laurentlbebb40712018-08-29 11:54:27 -0700827 }
828
laurentlbebb40712018-08-29 11:54:27 -0700829 /**
brandjon73e768e2018-02-13 12:26:52 -0800830 * Returns the value from the environment whose name is "varname" if it exists, otherwise null.
laurentlbebb40712018-08-29 11:54:27 -0700831 *
832 * <p>TODO(laurentlb): Remove this method. Callers should know where the value is defined and use
833 * the corresponding method (e.g. localLookup or moduleLookup).
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100834 */
laurentlbc3e58512018-09-04 10:11:32 -0700835 Object lookup(String varname) {
Googler0c8f22e2019-08-28 07:10:45 -0700836 // Lexical frame takes precedence, then globals.
laurentlb5d26ef12018-08-27 12:23:15 -0700837 Object lexicalValue = lexicalFrame.get(varname);
838 if (lexicalValue != null) {
839 return lexicalValue;
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000840 }
841 Object globalValue = globalFrame.get(varname);
laurentlb2b3ad182018-12-05 05:49:45 -0800842 if (globalValue == null) {
Vladimir Moskva4994cb32016-08-10 15:44:31 +0000843 return null;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100844 }
laurentlb2b3ad182018-12-05 05:49:45 -0800845 return globalValue;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100846 }
847
laurentlb6659b4c2019-02-18 07:23:36 -0800848 public StarlarkSemantics getSemantics() {
brandjonb712f332017-04-29 16:03:32 +0200849 return semantics;
850 }
851
Googler7fa0dd22019-09-24 20:07:53 -0700852 void handleEvent(Event event) {
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000853 eventHandler.handle(event);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100854 }
855
brandjon73e768e2018-02-13 12:26:52 -0800856 /**
Googlera3421e22019-09-26 06:48:32 -0700857 * Returns a set of all names of variables that are accessible in this {@code StarlarkThread}, in
858 * a deterministic order.
brandjon73e768e2018-02-13 12:26:52 -0800859 */
Googlerfe544aa2019-12-03 14:05:06 -0800860 Set<String> getVariableNames() {
brandjon73e768e2018-02-13 12:26:52 -0800861 LinkedHashSet<String> vars = new LinkedHashSet<>();
laurentlb5d26ef12018-08-27 12:23:15 -0700862 vars.addAll(lexicalFrame.getTransitiveBindings().keySet());
863 // No-op when globalFrame = lexicalFrame
brandjoncc0f6a62017-05-08 13:19:21 -0400864 vars.addAll(globalFrame.getTransitiveBindings().keySet());
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000865 return vars;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100866 }
867
Googlerf0890f02019-10-01 07:28:48 -0700868 /** Evaluates a Skylark statement in this thread. (Debugger API) This operation mutates expr. */
Googlera4f8b622019-09-13 11:08:29 -0700869 // TODO(adonovan): push this up into the debugger once the eval API is finalized.
Googlerf0890f02019-10-01 07:28:48 -0700870 public Object debugEval(Expression expr) throws EvalException, InterruptedException {
Googler698d77f2019-09-17 14:54:26 -0700871 return Eval.eval(this, expr);
Googlera4f8b622019-09-13 11:08:29 -0700872 }
873
Googler59100932019-09-12 07:24:20 -0700874 /**
875 * Returns the stack frames corresponding of the context's current (paused) state. (Debugger API)
876 *
877 * <p>For all stack frames except the innermost, location information is retrieved from the
878 * current context. The innermost frame's location must be supplied as {@code currentLocation} by
879 * the caller.
880 */
Googlerfe544aa2019-12-03 14:05:06 -0800881 public ImmutableList<DebugFrame> listFrames(Location loc) {
Googler29eafdf2018-05-23 12:32:07 -0700882 ImmutableList.Builder<DebugFrame> frameListBuilder = ImmutableList.builder();
883
Googlerfe544aa2019-12-03 14:05:06 -0800884 Frame lex = this.lexicalFrame;
885 for (CallFrame fr : Lists.reverse(callstack)) {
Googler29eafdf2018-05-23 12:32:07 -0700886 frameListBuilder.add(
887 DebugFrame.builder()
Googlerfe544aa2019-12-03 14:05:06 -0800888 .setLexicalFrameBindings(ImmutableMap.copyOf(lex.getTransitiveBindings()))
Googler29eafdf2018-05-23 12:32:07 -0700889 .setGlobalBindings(ImmutableMap.copyOf(getGlobals().getTransitiveBindings()))
Googlerfe544aa2019-12-03 14:05:06 -0800890 .setFunctionName(fr.fn.getName())
891 .setLocation(loc)
Googler29eafdf2018-05-23 12:32:07 -0700892 .build());
Googlerfe544aa2019-12-03 14:05:06 -0800893 lex = fr.savedLexicals;
894 loc = fr.callerLoc;
Googler29eafdf2018-05-23 12:32:07 -0700895 }
Googlerfe544aa2019-12-03 14:05:06 -0800896 // TODO(adonovan): simplify by fixing the callstack's off-by-one problem.
897 // We won't need to pass in 'loc' nor add a fake <top level> frame, nor
898 // suffer a loop-carried dependence.
Googler29eafdf2018-05-23 12:32:07 -0700899 frameListBuilder.add(
900 DebugFrame.builder()
901 .setGlobalBindings(ImmutableMap.copyOf(getGlobals().getTransitiveBindings()))
902 .setFunctionName("<top level>")
Googlerfe544aa2019-12-03 14:05:06 -0800903 .setLocation(loc)
Googler29eafdf2018-05-23 12:32:07 -0700904 .build());
905
906 return frameListBuilder.build();
907 }
908
Googler59100932019-09-12 07:24:20 -0700909 /**
910 * Given a requested stepping behavior, returns a predicate over the context that tells the
911 * debugger when to pause. (Debugger API)
912 *
913 * <p>The predicate will return true if we are at the next statement where execution should pause,
914 * and it will return false if we are not yet at that statement. No guarantee is made about the
915 * predicate's return value after we have reached the desired statement.
916 *
917 * <p>A null return value indicates that no further pausing should occur.
918 */
Googler29eafdf2018-05-23 12:32:07 -0700919 @Nullable
920 public ReadyToPause stepControl(Stepping stepping) {
Googlerfe544aa2019-12-03 14:05:06 -0800921 final int depth = callstack.size();
Googler29eafdf2018-05-23 12:32:07 -0700922 switch (stepping) {
923 case NONE:
924 return null;
925 case INTO:
926 // pause at the very next statement
Googlera3421e22019-09-26 06:48:32 -0700927 return thread -> true;
Googler29eafdf2018-05-23 12:32:07 -0700928 case OVER:
Googlerfe544aa2019-12-03 14:05:06 -0800929 return thread -> thread.callstack.size() <= depth;
Googler29eafdf2018-05-23 12:32:07 -0700930 case OUT:
Googlerfe544aa2019-12-03 14:05:06 -0800931 // if we're at the outermost frame, same as NONE
932 return depth == 0 ? null : thread -> thread.callstack.size() < depth;
Googler29eafdf2018-05-23 12:32:07 -0700933 }
934 throw new IllegalArgumentException("Unsupported stepping type: " + stepping);
935 }
936
Googler59100932019-09-12 07:24:20 -0700937 /** See stepControl (Debugger API) */
Googlera3421e22019-09-26 06:48:32 -0700938 public interface ReadyToPause extends Predicate<StarlarkThread> {}
Googler59100932019-09-12 07:24:20 -0700939
940 /**
941 * Describes the stepping behavior that should occur when execution of a thread is continued.
942 * (Debugger API)
943 */
944 public enum Stepping {
945 /** Continue execution without stepping. */
946 NONE,
947 /**
948 * If the thread is paused on a statement that contains a function call, step into that
949 * function. Otherwise, this is the same as OVER.
950 */
951 INTO,
952 /**
953 * Step over the current statement and any functions that it may call, stopping at the next
954 * statement in the same frame. If no more statements are available in the current frame, same
955 * as OUT.
956 */
957 OVER,
958 /**
959 * Continue execution until the current frame has been exited and then pause. If we are
960 * currently in the outer-most frame, same as NONE.
961 */
962 OUT,
963 }
964
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100965 @Override
966 public int hashCode() {
967 throw new UnsupportedOperationException(); // avoid nondeterminism
968 }
969
970 @Override
971 public boolean equals(Object that) {
972 throw new UnsupportedOperationException();
973 }
974
975 @Override
976 public String toString() {
Googlera3421e22019-09-26 06:48:32 -0700977 return String.format("<StarlarkThread%s>", mutability());
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100978 }
979
980 /**
laurentlbcaafe302018-08-28 10:24:31 -0700981 * An Exception thrown when an attempt is made to import a symbol from a file that was not
982 * properly loaded.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100983 */
Googler0c8f22e2019-08-28 07:10:45 -0700984 // TODO(adonovan): replace with plain EvalException.
Janak Ramakrishnan9b12f9c2016-05-20 16:33:02 +0000985 static class LoadFailedException extends Exception {
John Field1ea7fc32015-12-22 19:37:19 +0000986 LoadFailedException(String importString) {
laurentlbcaafe302018-08-28 10:24:31 -0700987 super(
988 String.format(
989 "file '%s' was not correctly loaded. "
990 + "Make sure the 'load' statement appears in the global scope in your file",
991 importString));
Vladimir Moskva4994cb32016-08-10 15:44:31 +0000992 }
993
Laurent Le Bruna2501d52017-01-02 15:42:19 +0000994 LoadFailedException(String importString, String symbolString, Iterable<String> allKeys) {
995 super(
996 String.format(
997 "file '%s' does not contain symbol '%s'%s",
998 importString, symbolString, SpellChecker.didYouMean(symbolString, allKeys)));
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100999 }
1000 }
1001
Janak Ramakrishnan9b12f9c2016-05-20 16:33:02 +00001002 void importSymbol(String importString, Identifier symbol, String nameInLoadedFile)
Vladimir Moskva4994cb32016-08-10 15:44:31 +00001003 throws LoadFailedException {
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +00001004 Preconditions.checkState(isGlobal()); // loading is only allowed at global scope.
1005
John Field1ea7fc32015-12-22 19:37:19 +00001006 if (!importedExtensions.containsKey(importString)) {
1007 throw new LoadFailedException(importString);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001008 }
Florian Weikert9d659ad2015-07-23 14:44:36 +00001009
John Field1ea7fc32015-12-22 19:37:19 +00001010 Extension ext = importedExtensions.get(importString);
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +00001011
brandjon357ed7a2017-05-09 11:37:22 -04001012 Map<String, Object> bindings = ext.getBindings();
1013 if (!bindings.containsKey(nameInLoadedFile)) {
1014 throw new LoadFailedException(importString, nameInLoadedFile, bindings.keySet());
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +00001015 }
1016
brandjon357ed7a2017-05-09 11:37:22 -04001017 Object value = bindings.get(nameInLoadedFile);
Florian Weikert9d659ad2015-07-23 14:44:36 +00001018
Googlerd54a4992019-10-18 12:35:06 -07001019 update(symbol.getName(), value);
Florian Weikert6a663392015-09-02 14:04:33 +00001020 }
1021
brandjon73e768e2018-02-13 12:26:52 -08001022 /**
1023 * Computes a deterministic hash for the given base hash code and extension map (the map's order
1024 * does not matter).
1025 */
Janak Ramakrishnan9b12f9c2016-05-20 16:33:02 +00001026 private static String computeTransitiveContentHashCode(
1027 @Nullable String baseHashCode, Map<String, Extension> importedExtensions) {
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +00001028 // Calculate a new hash from the hash of the loaded Extension-s.
1029 Fingerprint fingerprint = new Fingerprint();
Janak Ramakrishnan9b12f9c2016-05-20 16:33:02 +00001030 if (baseHashCode != null) {
1031 fingerprint.addString(Preconditions.checkNotNull(baseHashCode));
1032 }
John Field1ea7fc32015-12-22 19:37:19 +00001033 TreeSet<String> importStrings = new TreeSet<>(importedExtensions.keySet());
1034 for (String importString : importStrings) {
1035 fingerprint.addString(importedExtensions.get(importString).getTransitiveContentHashCode());
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +00001036 }
1037 return fingerprint.hexDigestAndReset();
Janak Ramakrishnanb6e33bc2015-09-06 21:05:23 +00001038 }
1039
Janak Ramakrishnan9b12f9c2016-05-20 16:33:02 +00001040 /**
Googlera3421e22019-09-26 06:48:32 -07001041 * Returns a hash code calculated from the hash code of this StarlarkThread and the transitive
1042 * closure of other StarlarkThreads it loads.
Janak Ramakrishnan9b12f9c2016-05-20 16:33:02 +00001043 */
1044 public String getTransitiveContentHashCode() {
1045 return transitiveHashCode;
1046 }
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +00001047
Googler6901c792019-11-07 18:01:49 -08001048 // legacy for copybara; to be inlined and deleted in Nov 2019.
1049 public static final Module SKYLARK = Module.createForBuiltins(Starlark.UNIVERSE);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001050}