Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package com.google.devtools.build.lib.syntax; |
| 16 | |
brandjon | 2c98fff | 2018-02-06 01:32:46 -0800 | [diff] [blame] | 17 | import com.google.common.base.Joiner; |
tomlu | a155b53 | 2017-11-08 20:12:47 +0100 | [diff] [blame] | 18 | import com.google.common.base.Preconditions; |
Googler | 29eafdf | 2018-05-23 12:32:07 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableList; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableMap; |
brandjon | 2c98fff | 2018-02-06 01:32:46 -0800 | [diff] [blame] | 21 | import com.google.common.collect.Sets; |
Kristina Chodorow | 6f15335 | 2016-03-21 16:20:06 +0000 | [diff] [blame] | 22 | import com.google.devtools.build.lib.cmdline.Label; |
brandjon | 357ed7a | 2017-05-09 11:37:22 -0400 | [diff] [blame] | 23 | import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 24 | import com.google.devtools.build.lib.events.Event; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 25 | import com.google.devtools.build.lib.events.EventHandler; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 26 | import com.google.devtools.build.lib.events.EventKind; |
Francois-Rene Rideau | 3a8ddcc | 2015-08-26 22:30:38 +0000 | [diff] [blame] | 27 | import com.google.devtools.build.lib.events.Location; |
janakr | 9ba46f8 | 2018-03-13 13:07:52 -0700 | [diff] [blame] | 28 | import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec; |
janakr | 33439dc | 2018-03-22 13:44:05 -0700 | [diff] [blame] | 29 | import com.google.devtools.build.lib.skylarkinterface.SkylarkValue; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 30 | import com.google.devtools.build.lib.syntax.Mutability.Freezable; |
| 31 | import com.google.devtools.build.lib.syntax.Mutability.MutabilityException; |
brendandouglas | 01683d3 | 2018-06-26 08:53:10 -0700 | [diff] [blame] | 32 | import com.google.devtools.build.lib.syntax.Parser.ParsingLevel; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 33 | import com.google.devtools.build.lib.util.Fingerprint; |
| 34 | import com.google.devtools.build.lib.util.Pair; |
Laurent Le Brun | a2501d5 | 2017-01-02 15:42:19 +0000 | [diff] [blame] | 35 | import com.google.devtools.build.lib.util.SpellChecker; |
Googler | 29eafdf | 2018-05-23 12:32:07 -0700 | [diff] [blame] | 36 | import com.google.devtools.build.lib.vfs.PathFragment; |
brandjon | 2c98fff | 2018-02-06 01:32:46 -0800 | [diff] [blame] | 37 | import java.util.ArrayList; |
brandjon | cc0f6a6 | 2017-05-08 13:19:21 -0400 | [diff] [blame] | 38 | import java.util.Collections; |
Jon Brandvein | ded4fbb | 2017-01-18 22:21:04 +0000 | [diff] [blame] | 39 | import java.util.LinkedHashMap; |
brandjon | 73e768e | 2018-02-13 12:26:52 -0800 | [diff] [blame] | 40 | import java.util.LinkedHashSet; |
Googler | 29eafdf | 2018-05-23 12:32:07 -0700 | [diff] [blame] | 41 | import java.util.List; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 42 | import java.util.Map; |
brandjon | e821fb9 | 2017-07-18 21:00:22 +0200 | [diff] [blame] | 43 | import java.util.Objects; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 44 | import java.util.Set; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 45 | import java.util.TreeSet; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 46 | import javax.annotation.Nullable; |
| 47 | |
| 48 | /** |
brandjon | 0adb784 | 2017-09-28 10:58:43 -0400 | [diff] [blame] | 49 | * An {@code Environment} is the main entry point to evaluating Skylark code. It embodies all the |
| 50 | * state that is required to evaluate such code, except for the current instruction pointer, which |
| 51 | * is an {@link ASTNode} that is evaluated (for expressions) or executed (for statements) with |
| 52 | * respect to this {@code Environment}. |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 53 | * |
brandjon | 0adb784 | 2017-09-28 10:58:43 -0400 | [diff] [blame] | 54 | * <p>{@link Continuation}-s are explicitly represented, but only partly, with another part being |
| 55 | * implicit in a series of try-catch statements, to maintain the direct style. One notable trick is |
| 56 | * how a {@link UserDefinedFunction} implements returning values as the function catching a {@link |
| 57 | * ReturnStatement.ReturnException} thrown by a {@link ReturnStatement} in the body. |
| 58 | * |
| 59 | * <p>Every {@code Environment} has a {@link Mutability} field, and must be used within a function |
| 60 | * that creates and closes this {@link Mutability} with the try-with-resource pattern. This {@link |
| 61 | * Mutability} is also used when initializing mutable objects within that {@code Environment}. When |
| 62 | * the {@code Mutability} is closed at the end of the computation, it freezes the {@code |
| 63 | * Environment} along with all of those objects. This pattern enforces the discipline that there |
| 64 | * should be no dangling mutable {@code Environment}, or concurrency between interacting {@code |
| 65 | * Environment}s. It is a Skylark-level error to attempt to mutate a frozen {@code Environment} or |
| 66 | * its objects, but it is a Java-level error to attempt to mutate an unfrozen {@code Environment} or |
| 67 | * its objects from within a different {@code Environment}. |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 68 | * |
Laurent Le Brun | a6d8fe4 | 2016-11-28 18:14:54 +0000 | [diff] [blame] | 69 | * <p>One creates an Environment using the {@link #builder} function, then populates it with {@link |
| 70 | * #setup}, {@link #setupDynamic} and sometimes {@link #setupOverride}, before to evaluate code in |
| 71 | * it with {@link BuildFileAST#eval}, or with {@link BuildFileAST#exec} (where the AST was obtained |
| 72 | * by passing a {@link ValidationEnvironment} constructed from the Environment to {@link |
| 73 | * BuildFileAST#parseBuildFile} or {@link BuildFileAST#parseSkylarkFile}). When the computation is |
| 74 | * over, the frozen Environment can still be queried with {@link #lookup}. |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 75 | * |
| 76 | * <p>Final fields of an Environment represent its dynamic state, i.e. state that remains the same |
Laurent Le Brun | a6d8fe4 | 2016-11-28 18:14:54 +0000 | [diff] [blame] | 77 | * throughout a given evaluation context, and don't change with source code location, while mutable |
| 78 | * fields embody its static state, that change with source code location. The seeming paradox is |
| 79 | * that the words "dynamic" and "static" refer to the point of view of the source code, and here we |
| 80 | * have a dual point of view. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 81 | */ |
Googler | 29eafdf | 2018-05-23 12:32:07 -0700 | [diff] [blame] | 82 | public final class Environment implements Freezable, Debuggable { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 83 | |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 84 | /** A phase for enabling or disabling certain builtin functions */ |
| 85 | public enum Phase { |
| 86 | WORKSPACE, |
| 87 | LOADING, |
| 88 | ANALYSIS |
| 89 | } |
Luis Fernando Pino Duque | 3fedf9e | 2016-04-28 15:47:29 +0000 | [diff] [blame] | 90 | |
| 91 | /** |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 92 | * A mapping of bindings, either mutable or immutable according to an associated {@link |
| 93 | * Mutability}. The order of the bindings within a single {@link Frame} is deterministic but |
| 94 | * unspecified. |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 95 | * |
nharmata | 68cc06b | 2018-03-01 10:21:57 -0800 | [diff] [blame] | 96 | * <p>Any non-frozen {@link Frame} must have the same {@link Mutability} as the current {@link |
brandjon | cc0f6a6 | 2017-05-08 13:19:21 -0400 | [diff] [blame] | 97 | * Environment}, to avoid interference from other evaluation contexts. For example, a {@link |
nharmata | 68cc06b | 2018-03-01 10:21:57 -0800 | [diff] [blame] | 98 | * UserDefinedFunction} will close over the global frame of the {@link Environment} in which it |
| 99 | * was defined. When the function is called from other {@link Environment}s (possibly |
| 100 | * simultaneously), that global frame must already be frozen; a new local {@link Frame} is created |
brandjon | cc0f6a6 | 2017-05-08 13:19:21 -0400 | [diff] [blame] | 101 | * to represent the lexical scope of the function. |
brandjon | adda512 | 2017-08-29 18:29:58 +0200 | [diff] [blame] | 102 | * |
nharmata | 68cc06b | 2018-03-01 10:21:57 -0800 | [diff] [blame] | 103 | * <p>A {@link Frame} can have an associated "parent" {@link Frame}, which is used in {@link #get} |
| 104 | * and {@link #getTransitiveBindings()} |
laurentlb | c8e6796 | 2018-08-31 14:20:40 -0700 | [diff] [blame] | 105 | * |
| 106 | * <p>TODO(laurentlb): "parent" should be named "universe" since it contains only the builtins. |
| 107 | * The "get" method shouldn't look at the universe (so that "moduleLookup" works as expected) |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 108 | */ |
nharmata | 68cc06b | 2018-03-01 10:21:57 -0800 | [diff] [blame] | 109 | public interface Frame extends Freezable { |
| 110 | /** |
| 111 | * Gets a binding from this {@link Frame} or one of its transitive parents. |
| 112 | * |
| 113 | * <p>In case of conflicts, the binding found in the {@link Frame} closest to the current one is |
| 114 | * used; the remaining bindings are shadowed. |
| 115 | * |
| 116 | * @param varname the name of the variable whose value should be retrieved |
| 117 | * @return the value bound to the variable, or null if no binding is found |
| 118 | */ |
| 119 | @Nullable |
| 120 | Object get(String varname); |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 121 | |
brandjon | adda512 | 2017-08-29 18:29:58 +0200 | [diff] [blame] | 122 | /** |
nharmata | 68cc06b | 2018-03-01 10:21:57 -0800 | [diff] [blame] | 123 | * Assigns or reassigns a binding in the current {@code Frame}. |
| 124 | * |
| 125 | * <p>If the binding has the same name as one in a transitive parent, the parent binding is |
| 126 | * shadowed (i.e., the parent is unaffected). |
| 127 | * |
| 128 | * @param env the {@link Environment} attempting the mutation |
| 129 | * @param varname the name of the variable to be bound |
| 130 | * @param value the value to bind to the variable |
| 131 | */ |
| 132 | void put(Environment env, String varname, Object value) throws MutabilityException; |
| 133 | |
| 134 | /** |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 135 | * TODO(laurentlb): Remove this method when possible. It should probably not be part of the |
| 136 | * public interface. |
nharmata | 68cc06b | 2018-03-01 10:21:57 -0800 | [diff] [blame] | 137 | */ |
| 138 | void remove(Environment env, String varname) throws MutabilityException; |
| 139 | |
| 140 | /** |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 141 | * Returns a map containing all bindings of this {@link Frame} and of its transitive parents, |
| 142 | * taking into account shadowing precedence. |
nharmata | 68cc06b | 2018-03-01 10:21:57 -0800 | [diff] [blame] | 143 | * |
| 144 | * <p>The bindings are returned in a deterministic order (for a given sequence of initial values |
| 145 | * and updates). |
| 146 | */ |
| 147 | Map<String, Object> getTransitiveBindings(); |
| 148 | } |
| 149 | |
| 150 | interface LexicalFrame extends Frame { |
| 151 | static LexicalFrame create(Mutability mutability) { |
| 152 | return mutability.isFrozen() |
| 153 | ? ImmutableEmptyLexicalFrame.INSTANCE |
| 154 | : new MutableLexicalFrame(mutability); |
| 155 | } |
nharmata | 62678a4 | 2018-03-08 16:43:45 -0800 | [diff] [blame] | 156 | |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 157 | static LexicalFrame create(Mutability mutability, int numArgs) { |
nharmata | 62678a4 | 2018-03-08 16:43:45 -0800 | [diff] [blame] | 158 | Preconditions.checkState(!mutability.isFrozen()); |
| 159 | return new MutableLexicalFrame(mutability, /*initialCapacity=*/ numArgs); |
| 160 | } |
nharmata | 68cc06b | 2018-03-01 10:21:57 -0800 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | private static final class ImmutableEmptyLexicalFrame implements LexicalFrame { |
| 164 | private static final ImmutableEmptyLexicalFrame INSTANCE = new ImmutableEmptyLexicalFrame(); |
| 165 | |
| 166 | @Override |
| 167 | public Mutability mutability() { |
| 168 | return Mutability.IMMUTABLE; |
| 169 | } |
| 170 | |
| 171 | @Nullable |
| 172 | @Override |
| 173 | public Object get(String varname) { |
| 174 | return null; |
| 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public void put(Environment env, String varname, Object value) throws MutabilityException { |
| 179 | Mutability.checkMutable(this, env.mutability()); |
| 180 | throw new IllegalStateException(); |
| 181 | } |
| 182 | |
| 183 | @Override |
| 184 | public void remove(Environment env, String varname) throws MutabilityException { |
| 185 | Mutability.checkMutable(this, env.mutability()); |
| 186 | throw new IllegalStateException(); |
| 187 | } |
| 188 | |
| 189 | @Override |
| 190 | public Map<String, Object> getTransitiveBindings() { |
| 191 | return ImmutableMap.of(); |
| 192 | } |
| 193 | |
| 194 | @Override |
| 195 | public String toString() { |
| 196 | return "<ImmutableEmptyLexicalFrame>"; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | private static final class MutableLexicalFrame implements LexicalFrame { |
| 201 | private final Mutability mutability; |
| 202 | /** Bindings are maintained in order of creation. */ |
nharmata | 62678a4 | 2018-03-08 16:43:45 -0800 | [diff] [blame] | 203 | private final LinkedHashMap<String, Object> bindings; |
nharmata | 68cc06b | 2018-03-01 10:21:57 -0800 | [diff] [blame] | 204 | |
nharmata | 62678a4 | 2018-03-08 16:43:45 -0800 | [diff] [blame] | 205 | private MutableLexicalFrame(Mutability mutability, int initialCapacity) { |
nharmata | 68cc06b | 2018-03-01 10:21:57 -0800 | [diff] [blame] | 206 | this.mutability = mutability; |
nharmata | 62678a4 | 2018-03-08 16:43:45 -0800 | [diff] [blame] | 207 | this.bindings = new LinkedHashMap<>(initialCapacity); |
| 208 | } |
| 209 | |
| 210 | private MutableLexicalFrame(Mutability mutability) { |
| 211 | this.mutability = mutability; |
| 212 | this.bindings = new LinkedHashMap<>(); |
nharmata | 68cc06b | 2018-03-01 10:21:57 -0800 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | @Override |
| 216 | public Mutability mutability() { |
| 217 | return mutability; |
| 218 | } |
| 219 | |
| 220 | @Nullable |
| 221 | @Override |
| 222 | public Object get(String varname) { |
| 223 | return bindings.get(varname); |
| 224 | } |
| 225 | |
| 226 | @Override |
| 227 | public void put(Environment env, String varname, Object value) throws MutabilityException { |
| 228 | Mutability.checkMutable(this, env.mutability()); |
| 229 | bindings.put(varname, value); |
| 230 | } |
| 231 | |
| 232 | @Override |
| 233 | public void remove(Environment env, String varname) throws MutabilityException { |
| 234 | Mutability.checkMutable(this, env.mutability()); |
| 235 | bindings.remove(varname); |
| 236 | } |
| 237 | |
| 238 | @Override |
| 239 | public Map<String, Object> getTransitiveBindings() { |
| 240 | return bindings; |
| 241 | } |
| 242 | |
| 243 | @Override |
| 244 | public String toString() { |
| 245 | return String.format("<MutableLexicalFrame%s>", mutability()); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * A {@link Frame} that can have a parent {@link GlobalFrame} from which it inherits bindings. |
| 251 | * |
| 252 | * <p>Bindings in a {@link GlobalFrame} may shadow those inherited from its parents. A chain of |
| 253 | * {@link GlobalFrame}s can represent different builtin scopes with a linear precedence ordering. |
| 254 | * |
| 255 | * <p>A {@link GlobalFrame} can also be constructed in a two-phase process. To do this, call the |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 256 | * nullary constructor to create an uninitialized {@link GlobalFrame}, then call {@link |
| 257 | * #initialize}. It is illegal to use any other method in-between these two calls, or to call |
| 258 | * {@link #initialize} on an already initialized {@link GlobalFrame}. |
nharmata | 68cc06b | 2018-03-01 10:21:57 -0800 | [diff] [blame] | 259 | */ |
nharmata | 68cc06b | 2018-03-01 10:21:57 -0800 | [diff] [blame] | 260 | public static final class GlobalFrame implements Frame { |
| 261 | /** |
brandjon | adda512 | 2017-08-29 18:29:58 +0200 | [diff] [blame] | 262 | * Final, except that it may be initialized after instantiation. Null mutability indicates that |
| 263 | * this Frame is uninitialized. |
| 264 | */ |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 265 | @Nullable private Mutability mutability; |
brandjon | cc0f6a6 | 2017-05-08 13:19:21 -0400 | [diff] [blame] | 266 | |
brandjon | adda512 | 2017-08-29 18:29:58 +0200 | [diff] [blame] | 267 | /** Final, except that it may be initialized after instantiation. */ |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 268 | @Nullable private GlobalFrame parent; |
brandjon | adda512 | 2017-08-29 18:29:58 +0200 | [diff] [blame] | 269 | |
| 270 | /** |
| 271 | * If this frame is a global frame, the label for the corresponding target, e.g. {@code |
| 272 | * //foo:bar.bzl}. |
| 273 | * |
| 274 | * <p>Final, except that it may be initialized after instantiation. |
| 275 | */ |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 276 | @Nullable private Label label; |
brandjon | cc0f6a6 | 2017-05-08 13:19:21 -0400 | [diff] [blame] | 277 | |
brandjon | 73e768e | 2018-02-13 12:26:52 -0800 | [diff] [blame] | 278 | /** Bindings are maintained in order of creation. */ |
| 279 | private final LinkedHashMap<String, Object> bindings; |
brandjon | cc0f6a6 | 2017-05-08 13:19:21 -0400 | [diff] [blame] | 280 | |
brandjon | adda512 | 2017-08-29 18:29:58 +0200 | [diff] [blame] | 281 | /** Constructs an uninitialized instance; caller must call {@link #initialize} before use. */ |
nharmata | 68cc06b | 2018-03-01 10:21:57 -0800 | [diff] [blame] | 282 | public GlobalFrame() { |
brandjon | adda512 | 2017-08-29 18:29:58 +0200 | [diff] [blame] | 283 | this.mutability = null; |
| 284 | this.parent = null; |
| 285 | this.label = null; |
| 286 | this.bindings = new LinkedHashMap<>(); |
| 287 | } |
| 288 | |
brandjon | b80eb18 | 2018-05-01 10:54:58 -0700 | [diff] [blame] | 289 | public GlobalFrame( |
| 290 | Mutability mutability, |
| 291 | @Nullable GlobalFrame parent, |
| 292 | @Nullable Label label, |
| 293 | @Nullable Map<String, Object> bindings) { |
laurentlb | c8e6796 | 2018-08-31 14:20:40 -0700 | [diff] [blame] | 294 | Preconditions.checkState(parent == null || parent.parent == null); // no more than 1 parent |
brandjon | adda512 | 2017-08-29 18:29:58 +0200 | [diff] [blame] | 295 | this.mutability = Preconditions.checkNotNull(mutability); |
| 296 | this.parent = parent; |
| 297 | this.label = label; |
| 298 | this.bindings = new LinkedHashMap<>(); |
brandjon | b80eb18 | 2018-05-01 10:54:58 -0700 | [diff] [blame] | 299 | if (bindings != null) { |
| 300 | this.bindings.putAll(bindings); |
| 301 | } |
brandjon | adda512 | 2017-08-29 18:29:58 +0200 | [diff] [blame] | 302 | } |
| 303 | |
nharmata | 68cc06b | 2018-03-01 10:21:57 -0800 | [diff] [blame] | 304 | public GlobalFrame(Mutability mutability) { |
brandjon | b80eb18 | 2018-05-01 10:54:58 -0700 | [diff] [blame] | 305 | this(mutability, null, null, null); |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 306 | } |
| 307 | |
brandjon | b80eb18 | 2018-05-01 10:54:58 -0700 | [diff] [blame] | 308 | public GlobalFrame(Mutability mutability, @Nullable GlobalFrame parent) { |
| 309 | this(mutability, parent, null, null); |
| 310 | } |
| 311 | |
| 312 | public GlobalFrame(Mutability mutability, @Nullable GlobalFrame parent, @Nullable Label label) { |
| 313 | this(mutability, parent, label, null); |
| 314 | } |
| 315 | |
| 316 | /** Constructs a global frame for the given builtin bindings. */ |
| 317 | public static GlobalFrame createForBuiltins(Map<String, Object> bindings) { |
| 318 | Mutability mutability = Mutability.create("<builtins>").freeze(); |
| 319 | return new GlobalFrame(mutability, null, null, bindings); |
brandjon | cc0f6a6 | 2017-05-08 13:19:21 -0400 | [diff] [blame] | 320 | } |
| 321 | |
brandjon | adda512 | 2017-08-29 18:29:58 +0200 | [diff] [blame] | 322 | private void checkInitialized() { |
| 323 | Preconditions.checkNotNull(mutability, "Attempted to use Frame before initializing it"); |
brandjon | cc0f6a6 | 2017-05-08 13:19:21 -0400 | [diff] [blame] | 324 | } |
| 325 | |
brandjon | adda512 | 2017-08-29 18:29:58 +0200 | [diff] [blame] | 326 | public void initialize( |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 327 | Mutability mutability, |
| 328 | @Nullable GlobalFrame parent, |
| 329 | @Nullable Label label, |
| 330 | Map<String, Object> bindings) { |
| 331 | Preconditions.checkState( |
| 332 | this.mutability == null, "Attempted to initialize an already initialized Frame"); |
brandjon | adda512 | 2017-08-29 18:29:58 +0200 | [diff] [blame] | 333 | this.mutability = Preconditions.checkNotNull(mutability); |
| 334 | this.parent = parent; |
| 335 | this.label = label; |
brandjon | cc0f6a6 | 2017-05-08 13:19:21 -0400 | [diff] [blame] | 336 | this.bindings.putAll(bindings); |
| 337 | } |
| 338 | |
| 339 | /** |
laurentlb | c8e6796 | 2018-08-31 14:20:40 -0700 | [diff] [blame] | 340 | * Returns a new {@link GlobalFrame} with the same fields, except that {@link #label} is set to |
nharmata | 68cc06b | 2018-03-01 10:21:57 -0800 | [diff] [blame] | 341 | * the given value. |
brandjon | cc0f6a6 | 2017-05-08 13:19:21 -0400 | [diff] [blame] | 342 | */ |
nharmata | 68cc06b | 2018-03-01 10:21:57 -0800 | [diff] [blame] | 343 | public GlobalFrame withLabel(Label label) { |
brandjon | adda512 | 2017-08-29 18:29:58 +0200 | [diff] [blame] | 344 | checkInitialized(); |
laurentlb | c8e6796 | 2018-08-31 14:20:40 -0700 | [diff] [blame] | 345 | return new GlobalFrame(mutability, /*parent*/ null, label, bindings); |
brandjon | cc0f6a6 | 2017-05-08 13:19:21 -0400 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | /** |
nharmata | 68cc06b | 2018-03-01 10:21:57 -0800 | [diff] [blame] | 349 | * Returns the {@link Mutability} of this {@link GlobalFrame}, which may be different from its |
brandjon | cc0f6a6 | 2017-05-08 13:19:21 -0400 | [diff] [blame] | 350 | * parent's. |
| 351 | */ |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 352 | @Override |
brandjon | cc0f6a6 | 2017-05-08 13:19:21 -0400 | [diff] [blame] | 353 | public Mutability mutability() { |
brandjon | adda512 | 2017-08-29 18:29:58 +0200 | [diff] [blame] | 354 | checkInitialized(); |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 355 | return mutability; |
| 356 | } |
| 357 | |
nharmata | 68cc06b | 2018-03-01 10:21:57 -0800 | [diff] [blame] | 358 | /** Returns the parent {@link GlobalFrame}, if it exists. */ |
brandjon | cc0f6a6 | 2017-05-08 13:19:21 -0400 | [diff] [blame] | 359 | @Nullable |
nharmata | 68cc06b | 2018-03-01 10:21:57 -0800 | [diff] [blame] | 360 | public GlobalFrame getParent() { |
brandjon | adda512 | 2017-08-29 18:29:58 +0200 | [diff] [blame] | 361 | checkInitialized(); |
brandjon | cc0f6a6 | 2017-05-08 13:19:21 -0400 | [diff] [blame] | 362 | return parent; |
Kristina Chodorow | 6f15335 | 2016-03-21 16:20:06 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | /** |
brandjon | cc0f6a6 | 2017-05-08 13:19:21 -0400 | [diff] [blame] | 366 | * Returns the label of this {@code Frame}, which may be null. Parent labels are not consulted. |
| 367 | * |
| 368 | * <p>Usually you want to use {@link #getTransitiveLabel}; this is just an accessor for |
| 369 | * completeness. |
Kristina Chodorow | 6f15335 | 2016-03-21 16:20:06 +0000 | [diff] [blame] | 370 | */ |
| 371 | @Nullable |
brandjon | cc0f6a6 | 2017-05-08 13:19:21 -0400 | [diff] [blame] | 372 | public Label getLabel() { |
brandjon | adda512 | 2017-08-29 18:29:58 +0200 | [diff] [blame] | 373 | checkInitialized(); |
Kristina Chodorow | 6f15335 | 2016-03-21 16:20:06 +0000 | [diff] [blame] | 374 | return label; |
| 375 | } |
| 376 | |
| 377 | /** |
nharmata | 68cc06b | 2018-03-01 10:21:57 -0800 | [diff] [blame] | 378 | * Walks from this {@link GlobalFrame} up through transitive parents, and returns the first |
| 379 | * non-null label found, or null if all labels are null. |
brandjon | cc0f6a6 | 2017-05-08 13:19:21 -0400 | [diff] [blame] | 380 | */ |
| 381 | @Nullable |
| 382 | public Label getTransitiveLabel() { |
brandjon | adda512 | 2017-08-29 18:29:58 +0200 | [diff] [blame] | 383 | checkInitialized(); |
brandjon | cc0f6a6 | 2017-05-08 13:19:21 -0400 | [diff] [blame] | 384 | if (label != null) { |
| 385 | return label; |
| 386 | } else if (parent != null) { |
| 387 | return parent.getTransitiveLabel(); |
| 388 | } else { |
| 389 | return null; |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | /** |
nharmata | 68cc06b | 2018-03-01 10:21:57 -0800 | [diff] [blame] | 394 | * Returns a map of direct bindings of this {@link GlobalFrame}, ignoring parents. |
brandjon | cc0f6a6 | 2017-05-08 13:19:21 -0400 | [diff] [blame] | 395 | * |
brandjon | 73e768e | 2018-02-13 12:26:52 -0800 | [diff] [blame] | 396 | * <p>The bindings are returned in a deterministic order (for a given sequence of initial values |
| 397 | * and updates). |
| 398 | * |
brandjon | cc0f6a6 | 2017-05-08 13:19:21 -0400 | [diff] [blame] | 399 | * <p>For efficiency an unmodifiable view is returned. Callers should assume that the view is |
nharmata | 68cc06b | 2018-03-01 10:21:57 -0800 | [diff] [blame] | 400 | * invalidated by any subsequent modification to the {@link GlobalFrame}'s bindings. |
brandjon | cc0f6a6 | 2017-05-08 13:19:21 -0400 | [diff] [blame] | 401 | */ |
| 402 | public Map<String, Object> getBindings() { |
brandjon | adda512 | 2017-08-29 18:29:58 +0200 | [diff] [blame] | 403 | checkInitialized(); |
brandjon | cc0f6a6 | 2017-05-08 13:19:21 -0400 | [diff] [blame] | 404 | return Collections.unmodifiableMap(bindings); |
| 405 | } |
| 406 | |
nharmata | 68cc06b | 2018-03-01 10:21:57 -0800 | [diff] [blame] | 407 | @Override |
brandjon | cc0f6a6 | 2017-05-08 13:19:21 -0400 | [diff] [blame] | 408 | public Map<String, Object> getTransitiveBindings() { |
brandjon | adda512 | 2017-08-29 18:29:58 +0200 | [diff] [blame] | 409 | checkInitialized(); |
brandjon | cc0f6a6 | 2017-05-08 13:19:21 -0400 | [diff] [blame] | 410 | // Can't use ImmutableMap.Builder because it doesn't allow duplicates. |
brandjon | 73e768e | 2018-02-13 12:26:52 -0800 | [diff] [blame] | 411 | LinkedHashMap<String, Object> collectedBindings = new LinkedHashMap<>(); |
brandjon | cc0f6a6 | 2017-05-08 13:19:21 -0400 | [diff] [blame] | 412 | accumulateTransitiveBindings(collectedBindings); |
| 413 | return collectedBindings; |
| 414 | } |
| 415 | |
brandjon | 73e768e | 2018-02-13 12:26:52 -0800 | [diff] [blame] | 416 | private void accumulateTransitiveBindings(LinkedHashMap<String, Object> accumulator) { |
brandjon | adda512 | 2017-08-29 18:29:58 +0200 | [diff] [blame] | 417 | checkInitialized(); |
brandjon | cc0f6a6 | 2017-05-08 13:19:21 -0400 | [diff] [blame] | 418 | // Put parents first, so child bindings take precedence. |
| 419 | if (parent != null) { |
| 420 | parent.accumulateTransitiveBindings(accumulator); |
| 421 | } |
| 422 | accumulator.putAll(bindings); |
| 423 | } |
| 424 | |
nharmata | 68cc06b | 2018-03-01 10:21:57 -0800 | [diff] [blame] | 425 | @Override |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 426 | public Object get(String varname) { |
brandjon | adda512 | 2017-08-29 18:29:58 +0200 | [diff] [blame] | 427 | checkInitialized(); |
nharmata | 68cc06b | 2018-03-01 10:21:57 -0800 | [diff] [blame] | 428 | Object val = bindings.get(varname); |
| 429 | if (val != null) { |
| 430 | return val; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 431 | } |
| 432 | if (parent != null) { |
| 433 | return parent.get(varname); |
| 434 | } |
| 435 | return null; |
| 436 | } |
| 437 | |
nharmata | 68cc06b | 2018-03-01 10:21:57 -0800 | [diff] [blame] | 438 | @Override |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 439 | public void put(Environment env, String varname, Object value) throws MutabilityException { |
brandjon | adda512 | 2017-08-29 18:29:58 +0200 | [diff] [blame] | 440 | checkInitialized(); |
brandjon | f6316bf | 2017-08-02 16:42:56 +0200 | [diff] [blame] | 441 | Mutability.checkMutable(this, env.mutability()); |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 442 | bindings.put(varname, value); |
| 443 | } |
| 444 | |
nharmata | 68cc06b | 2018-03-01 10:21:57 -0800 | [diff] [blame] | 445 | @Override |
| 446 | public void remove(Environment env, String varname) throws MutabilityException { |
brandjon | adda512 | 2017-08-29 18:29:58 +0200 | [diff] [blame] | 447 | checkInitialized(); |
brandjon | f6316bf | 2017-08-02 16:42:56 +0200 | [diff] [blame] | 448 | Mutability.checkMutable(this, env.mutability()); |
laurentlb | 9d5c0a0 | 2017-06-13 23:08:06 +0200 | [diff] [blame] | 449 | bindings.remove(varname); |
| 450 | } |
| 451 | |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 452 | @Override |
| 453 | public String toString() { |
brandjon | adda512 | 2017-08-29 18:29:58 +0200 | [diff] [blame] | 454 | if (mutability == null) { |
nharmata | 68cc06b | 2018-03-01 10:21:57 -0800 | [diff] [blame] | 455 | return "<Uninitialized GlobalFrame>"; |
brandjon | adda512 | 2017-08-29 18:29:58 +0200 | [diff] [blame] | 456 | } else { |
nharmata | 68cc06b | 2018-03-01 10:21:57 -0800 | [diff] [blame] | 457 | return String.format("<GlobalFrame%s>", mutability()); |
brandjon | adda512 | 2017-08-29 18:29:58 +0200 | [diff] [blame] | 458 | } |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 459 | } |
| 460 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 461 | |
| 462 | /** |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 463 | * A Continuation contains data saved during a function call and restored when the function exits. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 464 | */ |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 465 | private static final class Continuation { |
| 466 | /** The {@link BaseFunction} being evaluated that will return into this Continuation. */ |
brandjon | c06e746 | 2017-07-11 20:54:58 +0200 | [diff] [blame] | 467 | final BaseFunction function; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 468 | |
| 469 | /** The {@link FuncallExpression} to which this Continuation will return. */ |
brendandouglas | fe785e1 | 2018-06-12 09:39:38 -0700 | [diff] [blame] | 470 | @Nullable final FuncallExpression caller; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 471 | |
| 472 | /** The next Continuation after this Continuation. */ |
brandjon | c06e746 | 2017-07-11 20:54:58 +0200 | [diff] [blame] | 473 | @Nullable final Continuation continuation; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 474 | |
| 475 | /** The lexical Frame of the caller. */ |
laurentlb | 5d26ef1 | 2018-08-27 12:23:15 -0700 | [diff] [blame] | 476 | final Frame lexicalFrame; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 477 | |
| 478 | /** The global Frame of the caller. */ |
nharmata | 68cc06b | 2018-03-01 10:21:57 -0800 | [diff] [blame] | 479 | final GlobalFrame globalFrame; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 480 | |
laurentlb | 9dc4e20 | 2018-09-06 09:34:13 -0700 | [diff] [blame] | 481 | /** |
| 482 | * The set of known global variables of the caller. |
| 483 | * |
| 484 | * <p>TODO(laurentlb): Remove this when we use static name resolution. |
| 485 | */ |
brandjon | 73e768e | 2018-02-13 12:26:52 -0800 | [diff] [blame] | 486 | @Nullable final LinkedHashSet<String> knownGlobalVariables; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 487 | |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 488 | Continuation( |
brendandouglas | fe785e1 | 2018-06-12 09:39:38 -0700 | [diff] [blame] | 489 | @Nullable Continuation continuation, |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 490 | BaseFunction function, |
brendandouglas | fe785e1 | 2018-06-12 09:39:38 -0700 | [diff] [blame] | 491 | @Nullable FuncallExpression caller, |
laurentlb | 5d26ef1 | 2018-08-27 12:23:15 -0700 | [diff] [blame] | 492 | Frame lexicalFrame, |
nharmata | 68cc06b | 2018-03-01 10:21:57 -0800 | [diff] [blame] | 493 | GlobalFrame globalFrame, |
brendandouglas | fe785e1 | 2018-06-12 09:39:38 -0700 | [diff] [blame] | 494 | @Nullable LinkedHashSet<String> knownGlobalVariables) { |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 495 | this.continuation = continuation; |
| 496 | this.function = function; |
| 497 | this.caller = caller; |
| 498 | this.lexicalFrame = lexicalFrame; |
| 499 | this.globalFrame = globalFrame; |
Jon Brandvein | 83e3acb | 2016-08-11 18:53:26 +0000 | [diff] [blame] | 500 | this.knownGlobalVariables = knownGlobalVariables; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 501 | } |
| 502 | } |
| 503 | |
brandjon | 357ed7a | 2017-05-09 11:37:22 -0400 | [diff] [blame] | 504 | /** An Extension to be imported with load() into a BUILD or .bzl file. */ |
| 505 | @Immutable |
janakr | 9ba46f8 | 2018-03-13 13:07:52 -0700 | [diff] [blame] | 506 | // TODO(janakr,brandjon): Do Extensions actually have to start their own memoization? Or can we |
| 507 | // have a node higher up in the hierarchy inject the mutability? |
shahan | 9012e6f | 2018-04-02 13:26:14 -0700 | [diff] [blame] | 508 | @AutoCodec |
brandjon | e821fb9 | 2017-07-18 21:00:22 +0200 | [diff] [blame] | 509 | public static final class Extension { |
| 510 | |
| 511 | private final ImmutableMap<String, Object> bindings; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 512 | |
brandjon | 357ed7a | 2017-05-09 11:37:22 -0400 | [diff] [blame] | 513 | /** |
| 514 | * Cached hash code for the transitive content of this {@code Extension} and its dependencies. |
brandjon | 2c98fff | 2018-02-06 01:32:46 -0800 | [diff] [blame] | 515 | * |
| 516 | * <p>Note that "content" refers to the AST content, not the evaluated bindings. |
brandjon | 357ed7a | 2017-05-09 11:37:22 -0400 | [diff] [blame] | 517 | */ |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 518 | private final String transitiveContentHashCode; |
| 519 | |
brandjon | e821fb9 | 2017-07-18 21:00:22 +0200 | [diff] [blame] | 520 | /** Constructs with the given hash code and bindings. */ |
janakr | 9ba46f8 | 2018-03-13 13:07:52 -0700 | [diff] [blame] | 521 | @AutoCodec.Instantiator |
brandjon | e821fb9 | 2017-07-18 21:00:22 +0200 | [diff] [blame] | 522 | public Extension(ImmutableMap<String, Object> bindings, String transitiveContentHashCode) { |
| 523 | this.bindings = bindings; |
brandjon | 357ed7a | 2017-05-09 11:37:22 -0400 | [diff] [blame] | 524 | this.transitiveContentHashCode = transitiveContentHashCode; |
| 525 | } |
| 526 | |
| 527 | /** |
| 528 | * Constructs using the bindings from the global definitions of the given {@link Environment}, |
| 529 | * and that {@code Environment}'s transitive hash code. |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 530 | */ |
| 531 | public Extension(Environment env) { |
brandjon | e821fb9 | 2017-07-18 21:00:22 +0200 | [diff] [blame] | 532 | this(ImmutableMap.copyOf(env.globalFrame.bindings), env.getTransitiveContentHashCode()); |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 533 | } |
| 534 | |
brandjon | 357ed7a | 2017-05-09 11:37:22 -0400 | [diff] [blame] | 535 | public String getTransitiveContentHashCode() { |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 536 | return transitiveContentHashCode; |
| 537 | } |
| 538 | |
brandjon | 73e768e | 2018-02-13 12:26:52 -0800 | [diff] [blame] | 539 | /** Retrieves all bindings, in a deterministic order. */ |
brandjon | 357ed7a | 2017-05-09 11:37:22 -0400 | [diff] [blame] | 540 | public ImmutableMap<String, Object> getBindings() { |
| 541 | return bindings; |
Laurent Le Brun | a2501d5 | 2017-01-02 15:42:19 +0000 | [diff] [blame] | 542 | } |
brandjon | e821fb9 | 2017-07-18 21:00:22 +0200 | [diff] [blame] | 543 | |
| 544 | @Override |
| 545 | public boolean equals(Object obj) { |
| 546 | if (this == obj) { |
| 547 | return true; |
| 548 | } |
| 549 | if (!(obj instanceof Extension)) { |
| 550 | return false; |
| 551 | } |
| 552 | Extension other = (Extension) obj; |
| 553 | return transitiveContentHashCode.equals(other.getTransitiveContentHashCode()) |
| 554 | && bindings.equals(other.getBindings()); |
| 555 | } |
| 556 | |
janakr | 33439dc | 2018-03-22 13:44:05 -0700 | [diff] [blame] | 557 | private static boolean skylarkObjectsProbablyEqual(Object obj1, Object obj2) { |
| 558 | // TODO(b/76154791): check this more carefully. |
| 559 | return obj1.equals(obj2) |
| 560 | || (obj1 instanceof SkylarkValue |
| 561 | && obj2 instanceof SkylarkValue |
| 562 | && Printer.repr(obj1).equals(Printer.repr(obj2))); |
| 563 | } |
| 564 | |
brandjon | 2c98fff | 2018-02-06 01:32:46 -0800 | [diff] [blame] | 565 | /** |
| 566 | * Throws {@link IllegalStateException} if this {@link Extension} is not equal to {@code obj}. |
| 567 | * |
| 568 | * <p>The exception explains the reason for the inequality, including all unequal bindings. |
| 569 | */ |
| 570 | public void checkStateEquals(Object obj) { |
| 571 | if (this == obj) { |
| 572 | return; |
| 573 | } |
| 574 | if (!(obj instanceof Extension)) { |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 575 | throw new IllegalStateException( |
| 576 | String.format( |
| 577 | "Expected an equal Extension, but got a %s instead of an Extension", |
| 578 | obj == null ? "null" : obj.getClass().getName())); |
brandjon | 2c98fff | 2018-02-06 01:32:46 -0800 | [diff] [blame] | 579 | } |
| 580 | Extension other = (Extension) obj; |
| 581 | ImmutableMap<String, Object> otherBindings = other.getBindings(); |
| 582 | |
| 583 | Set<String> names = bindings.keySet(); |
| 584 | Set<String> otherNames = otherBindings.keySet(); |
| 585 | if (!names.equals(otherNames)) { |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 586 | throw new IllegalStateException( |
| 587 | String.format( |
| 588 | "Expected Extensions to be equal, but they don't define the same bindings: " |
| 589 | + "in this one but not given one: [%s]; in given one but not this one: [%s]", |
| 590 | Joiner.on(", ").join(Sets.difference(names, otherNames)), |
| 591 | Joiner.on(", ").join(Sets.difference(otherNames, names)))); |
brandjon | 2c98fff | 2018-02-06 01:32:46 -0800 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | ArrayList<String> badEntries = new ArrayList<>(); |
| 595 | for (String name : names) { |
| 596 | Object value = bindings.get(name); |
| 597 | Object otherValue = otherBindings.get(name); |
janakr | 889f562 | 2018-03-16 17:49:11 -0700 | [diff] [blame] | 598 | if (value.equals(otherValue)) { |
| 599 | continue; |
brandjon | 2c98fff | 2018-02-06 01:32:46 -0800 | [diff] [blame] | 600 | } |
janakr | 33439dc | 2018-03-22 13:44:05 -0700 | [diff] [blame] | 601 | if (value instanceof SkylarkNestedSet) { |
| 602 | if (otherValue instanceof SkylarkNestedSet |
| 603 | && ((SkylarkNestedSet) value) |
| 604 | .toCollection() |
| 605 | .equals(((SkylarkNestedSet) otherValue).toCollection())) { |
| 606 | continue; |
| 607 | } |
| 608 | } else if (value instanceof SkylarkDict) { |
| 609 | if (otherValue instanceof SkylarkDict) { |
| 610 | @SuppressWarnings("unchecked") |
| 611 | SkylarkDict<Object, Object> thisDict = (SkylarkDict<Object, Object>) value; |
| 612 | @SuppressWarnings("unchecked") |
| 613 | SkylarkDict<Object, Object> otherDict = (SkylarkDict<Object, Object>) otherValue; |
| 614 | if (thisDict.size() == otherDict.size() |
| 615 | && thisDict.keySet().equals(otherDict.keySet())) { |
| 616 | boolean foundProblem = false; |
| 617 | for (Object key : thisDict.keySet()) { |
| 618 | if (!skylarkObjectsProbablyEqual( |
| 619 | Preconditions.checkNotNull(thisDict.get(key), key), |
| 620 | Preconditions.checkNotNull(otherDict.get(key), key))) { |
| 621 | foundProblem = true; |
| 622 | } |
| 623 | } |
| 624 | if (!foundProblem) { |
| 625 | continue; |
| 626 | } |
| 627 | } |
| 628 | } |
| 629 | } else if (skylarkObjectsProbablyEqual(value, otherValue)) { |
janakr | 889f562 | 2018-03-16 17:49:11 -0700 | [diff] [blame] | 630 | continue; |
| 631 | } |
| 632 | badEntries.add( |
| 633 | String.format( |
janakr | cfc3432 | 2018-03-26 11:10:08 -0700 | [diff] [blame] | 634 | "%s: this one has %s (class %s, %s), but given one has %s (class %s, %s)", |
janakr | 889f562 | 2018-03-16 17:49:11 -0700 | [diff] [blame] | 635 | name, |
| 636 | Printer.repr(value), |
| 637 | value.getClass().getName(), |
janakr | cfc3432 | 2018-03-26 11:10:08 -0700 | [diff] [blame] | 638 | value, |
janakr | 889f562 | 2018-03-16 17:49:11 -0700 | [diff] [blame] | 639 | Printer.repr(otherValue), |
janakr | cfc3432 | 2018-03-26 11:10:08 -0700 | [diff] [blame] | 640 | otherValue.getClass().getName(), |
| 641 | otherValue)); |
brandjon | 2c98fff | 2018-02-06 01:32:46 -0800 | [diff] [blame] | 642 | } |
| 643 | if (!badEntries.isEmpty()) { |
| 644 | throw new IllegalStateException( |
| 645 | "Expected Extensions to be equal, but the following bindings are unequal: " |
| 646 | + Joiner.on("; ").join(badEntries)); |
| 647 | } |
| 648 | |
| 649 | if (!transitiveContentHashCode.equals(other.getTransitiveContentHashCode())) { |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 650 | throw new IllegalStateException( |
| 651 | String.format( |
| 652 | "Expected Extensions to be equal, but transitive content hashes don't match:" |
| 653 | + " %s != %s", |
| 654 | transitiveContentHashCode, other.getTransitiveContentHashCode())); |
brandjon | 2c98fff | 2018-02-06 01:32:46 -0800 | [diff] [blame] | 655 | } |
| 656 | } |
| 657 | |
brandjon | e821fb9 | 2017-07-18 21:00:22 +0200 | [diff] [blame] | 658 | @Override |
| 659 | public int hashCode() { |
| 660 | return Objects.hash(bindings, transitiveContentHashCode); |
| 661 | } |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | /** |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 665 | * Static Frame for lexical variables that are always looked up in the current Environment or for |
| 666 | * the definition Environment of the function currently being evaluated. |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 667 | */ |
laurentlb | 5d26ef1 | 2018-08-27 12:23:15 -0700 | [diff] [blame] | 668 | private Frame lexicalFrame; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 669 | |
| 670 | /** |
| 671 | * Static Frame for global variables; either the current lexical Frame if evaluation is currently |
| 672 | * happening at the global scope of a BUILD file, or the global Frame at the time of function |
| 673 | * definition if evaluation is currently happening in the body of a function. Thus functions can |
| 674 | * close over other functions defined in the same file. |
| 675 | */ |
nharmata | 68cc06b | 2018-03-01 10:21:57 -0800 | [diff] [blame] | 676 | private GlobalFrame globalFrame; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 677 | |
| 678 | /** |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 679 | * Dynamic Frame for variables that are always looked up in the runtime Environment, and never in |
| 680 | * the lexical or "global" Environment as it was at the time of function definition. For instance, |
| 681 | * PACKAGE_NAME. |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 682 | */ |
| 683 | private final Frame dynamicFrame; |
| 684 | |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 685 | /** The semantics options that affect how Skylark code is evaluated. */ |
brandjon | 3c16191 | 2017-10-05 05:06:05 +0200 | [diff] [blame] | 686 | private final SkylarkSemantics semantics; |
brandjon | b712f33 | 2017-04-29 16:03:32 +0200 | [diff] [blame] | 687 | |
| 688 | /** |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 689 | * An EventHandler for errors and warnings. This is not used in the BUILD language, however it |
| 690 | * might be used in Skylark code called from the BUILD language, so shouldn't be null. |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 691 | */ |
| 692 | private final EventHandler eventHandler; |
| 693 | |
| 694 | /** |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 695 | * For each imported extension, a global Skylark frame from which to load() individual bindings. |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 696 | */ |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 697 | private final Map<String, Extension> importedExtensions; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 698 | |
| 699 | /** |
Laurent Le Brun | e51a4d2 | 2016-10-11 18:04:16 +0000 | [diff] [blame] | 700 | * Is this Environment being executed during the loading phase? Many builtin functions are only |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 701 | * enabled during the loading phase, and check this flag. TODO(laurentlb): Remove from Environment |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 702 | */ |
Laurent Le Brun | e51a4d2 | 2016-10-11 18:04:16 +0000 | [diff] [blame] | 703 | private final Phase phase; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 704 | |
| 705 | /** |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 706 | * When in a lexical (Skylark) Frame, this set contains the variable names that are global, as |
| 707 | * determined not by global declarations (not currently supported), but by previous lookups that |
| 708 | * ended being global or dynamic. This is necessary because if in a function definition something |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 709 | * reads a global variable after which a local variable with the same name is assigned an |
| 710 | * Exception needs to be thrown. |
| 711 | */ |
brandjon | 73e768e | 2018-02-13 12:26:52 -0800 | [diff] [blame] | 712 | @Nullable private LinkedHashSet<String> knownGlobalVariables; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 713 | |
| 714 | /** |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 715 | * When in a lexical (Skylark) frame, this lists the names of the functions in the call stack. We |
| 716 | * currently use it to artificially disable recursion. |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 717 | */ |
| 718 | @Nullable private Continuation continuation; |
| 719 | |
| 720 | /** |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 721 | * Gets the label of the BUILD file that is using this environment. For example, if a target //foo |
| 722 | * has a dependency on //bar which is a Skylark rule defined in //rules:my_rule.bzl being |
Kristina Chodorow | 704d8d9 | 2016-12-01 17:47:52 +0000 | [diff] [blame] | 723 | * evaluated in this environment, then this would return //foo. |
| 724 | */ |
| 725 | @Nullable private final Label callerLabel; |
| 726 | |
| 727 | /** |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 728 | * Enters a scope by saving state to a new Continuation |
brendandouglas | fe785e1 | 2018-06-12 09:39:38 -0700 | [diff] [blame] | 729 | * |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 730 | * @param function the function whose scope to enter |
nharmata | 68cc06b | 2018-03-01 10:21:57 -0800 | [diff] [blame] | 731 | * @param lexical the lexical frame to use |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 732 | * @param caller the source AST node for the caller |
| 733 | * @param globals the global Frame that this function closes over from its definition Environment |
| 734 | */ |
nharmata | 68cc06b | 2018-03-01 10:21:57 -0800 | [diff] [blame] | 735 | void enterScope( |
brendandouglas | fe785e1 | 2018-06-12 09:39:38 -0700 | [diff] [blame] | 736 | BaseFunction function, |
laurentlb | 5d26ef1 | 2018-08-27 12:23:15 -0700 | [diff] [blame] | 737 | Frame lexical, |
brendandouglas | fe785e1 | 2018-06-12 09:39:38 -0700 | [diff] [blame] | 738 | @Nullable FuncallExpression caller, |
| 739 | GlobalFrame globals) { |
Janak Ramakrishnan | 9b12f9c | 2016-05-20 16:33:02 +0000 | [diff] [blame] | 740 | continuation = |
Laurent Le Brun | 4db5064 | 2016-10-26 14:25:37 +0000 | [diff] [blame] | 741 | new Continuation( |
| 742 | continuation, function, caller, lexicalFrame, globalFrame, knownGlobalVariables); |
nharmata | 68cc06b | 2018-03-01 10:21:57 -0800 | [diff] [blame] | 743 | lexicalFrame = lexical; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 744 | globalFrame = globals; |
brandjon | 73e768e | 2018-02-13 12:26:52 -0800 | [diff] [blame] | 745 | knownGlobalVariables = new LinkedHashSet<>(); |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 746 | } |
| 747 | |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 748 | /** Exits a scope by restoring state from the current continuation */ |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 749 | void exitScope() { |
| 750 | Preconditions.checkNotNull(continuation); |
| 751 | lexicalFrame = continuation.lexicalFrame; |
| 752 | globalFrame = continuation.globalFrame; |
| 753 | knownGlobalVariables = continuation.knownGlobalVariables; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 754 | continuation = continuation.continuation; |
| 755 | } |
| 756 | |
Janak Ramakrishnan | 9b12f9c | 2016-05-20 16:33:02 +0000 | [diff] [blame] | 757 | private final String transitiveHashCode; |
Francois-Rene Rideau | 3a8ddcc | 2015-08-26 22:30:38 +0000 | [diff] [blame] | 758 | |
| 759 | /** |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 760 | * Checks that the current Environment is in the loading or the workspace phase. TODO(laurentlb): |
| 761 | * Move to SkylarkUtils |
Laurent Le Brun | 27d0b32 | 2016-11-22 14:48:44 +0000 | [diff] [blame] | 762 | * |
Damien Martin-Guillerez | 074b957 | 2016-05-23 12:33:07 +0000 | [diff] [blame] | 763 | * @param symbol name of the function being only authorized thus. |
| 764 | */ |
| 765 | public void checkLoadingOrWorkspacePhase(String symbol, Location loc) throws EvalException { |
| 766 | if (phase == Phase.ANALYSIS) { |
| 767 | throw new EvalException(loc, symbol + "() cannot be called during the analysis phase"); |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | /** |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 772 | * Checks that the current Environment is in the loading phase. TODO(laurentlb): Move to |
| 773 | * SkylarkUtils |
Laurent Le Brun | 27d0b32 | 2016-11-22 14:48:44 +0000 | [diff] [blame] | 774 | * |
Francois-Rene Rideau | 3a8ddcc | 2015-08-26 22:30:38 +0000 | [diff] [blame] | 775 | * @param symbol name of the function being only authorized thus. |
| 776 | */ |
| 777 | public void checkLoadingPhase(String symbol, Location loc) throws EvalException { |
Luis Fernando Pino Duque | 3fedf9e | 2016-04-28 15:47:29 +0000 | [diff] [blame] | 778 | if (phase != Phase.LOADING) { |
Francois-Rene Rideau | 3a8ddcc | 2015-08-26 22:30:38 +0000 | [diff] [blame] | 779 | throw new EvalException(loc, symbol + "() can only be called during the loading phase"); |
| 780 | } |
| 781 | } |
| 782 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 783 | /** |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 784 | * Is this a global Environment? |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 785 | * |
| 786 | * @return true if the current code is being executed at the top-level, as opposed to inside the |
| 787 | * body of a function. |
Francois-Rene Rideau | 6e7160d | 2015-08-26 17:22:35 +0000 | [diff] [blame] | 788 | */ |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 789 | boolean isGlobal() { |
laurentlb | 5d26ef1 | 2018-08-27 12:23:15 -0700 | [diff] [blame] | 790 | return lexicalFrame instanceof GlobalFrame; |
Francois-Rene Rideau | 6e7160d | 2015-08-26 17:22:35 +0000 | [diff] [blame] | 791 | } |
| 792 | |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 793 | @Override |
| 794 | public Mutability mutability() { |
| 795 | // the mutability of the environment is that of its dynamic frame. |
| 796 | return dynamicFrame.mutability(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 797 | } |
| 798 | |
brandjon | 73e768e | 2018-02-13 12:26:52 -0800 | [diff] [blame] | 799 | /** Returns the global variables for the Environment (not including dynamic bindings). */ |
nharmata | 68cc06b | 2018-03-01 10:21:57 -0800 | [diff] [blame] | 800 | public GlobalFrame getGlobals() { |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 801 | return globalFrame; |
| 802 | } |
| 803 | |
| 804 | /** |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 805 | * Returns an EventHandler for errors and warnings. The BUILD language doesn't use it directly, |
| 806 | * but can call Skylark code that does use it. |
| 807 | * |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 808 | * @return an EventHandler |
| 809 | */ |
Francois-Rene Rideau | e6a46b8 | 2015-04-10 18:31:43 +0000 | [diff] [blame] | 810 | public EventHandler getEventHandler() { |
| 811 | return eventHandler; |
| 812 | } |
| 813 | |
michajlo | cbb3347 | 2017-10-23 20:30:18 +0200 | [diff] [blame] | 814 | /** |
| 815 | * Returns if calling the supplied function would be a recursive call, or in other words if the |
| 816 | * supplied function is already on the stack. |
| 817 | */ |
| 818 | boolean isRecursiveCall(UserDefinedFunction function) { |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 819 | for (Continuation k = continuation; k != null; k = k.continuation) { |
michajlo | cbb3347 | 2017-10-23 20:30:18 +0200 | [diff] [blame] | 820 | if (k.function.equals(function)) { |
| 821 | return true; |
| 822 | } |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 823 | } |
michajlo | cbb3347 | 2017-10-23 20:30:18 +0200 | [diff] [blame] | 824 | return false; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 825 | } |
| 826 | |
michajlo | cbb3347 | 2017-10-23 20:30:18 +0200 | [diff] [blame] | 827 | /** Returns the current function call, if it exists. */ |
| 828 | @Nullable |
| 829 | BaseFunction getCurrentFunction() { |
| 830 | return continuation != null ? continuation.function : null; |
| 831 | } |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 832 | |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 833 | /** Returns the FuncallExpression and the BaseFunction for the top-level call being evaluated. */ |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 834 | public Pair<FuncallExpression, BaseFunction> getTopCall() { |
| 835 | Continuation continuation = this.continuation; |
| 836 | if (continuation == null) { |
| 837 | return null; |
| 838 | } |
| 839 | while (continuation.continuation != null) { |
| 840 | continuation = continuation.continuation; |
| 841 | } |
| 842 | return new Pair<>(continuation.caller, continuation.function); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 843 | } |
| 844 | |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 845 | /** |
Laurent Le Brun | a31bc4e | 2016-10-27 12:48:22 +0000 | [diff] [blame] | 846 | * Constructs an Environment. This is the main, most basic constructor. |
| 847 | * |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 848 | * @param globalFrame a frame for the global Environment |
| 849 | * @param dynamicFrame a frame for the dynamic Environment |
| 850 | * @param eventHandler an EventHandler for warnings, errors, etc |
| 851 | * @param importedExtensions Extension-s from which to import bindings with load() |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 852 | * @param fileContentHashCode a hash for the source file being evaluated, if any |
Luis Fernando Pino Duque | 3fedf9e | 2016-04-28 15:47:29 +0000 | [diff] [blame] | 853 | * @param phase the current phase |
Kristina Chodorow | 704d8d9 | 2016-12-01 17:47:52 +0000 | [diff] [blame] | 854 | * @param callerLabel the label this environment came from |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 855 | */ |
| 856 | private Environment( |
nharmata | 68cc06b | 2018-03-01 10:21:57 -0800 | [diff] [blame] | 857 | GlobalFrame globalFrame, |
| 858 | LexicalFrame dynamicFrame, |
brandjon | 3c16191 | 2017-10-05 05:06:05 +0200 | [diff] [blame] | 859 | SkylarkSemantics semantics, |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 860 | EventHandler eventHandler, |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 861 | Map<String, Extension> importedExtensions, |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 862 | @Nullable String fileContentHashCode, |
Kristina Chodorow | 704d8d9 | 2016-12-01 17:47:52 +0000 | [diff] [blame] | 863 | Phase phase, |
| 864 | @Nullable Label callerLabel) { |
laurentlb | 5d26ef1 | 2018-08-27 12:23:15 -0700 | [diff] [blame] | 865 | this.lexicalFrame = Preconditions.checkNotNull(globalFrame); |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 866 | this.globalFrame = Preconditions.checkNotNull(globalFrame); |
| 867 | this.dynamicFrame = Preconditions.checkNotNull(dynamicFrame); |
brandjon | 33b4943 | 2017-05-04 18:58:52 +0200 | [diff] [blame] | 868 | Preconditions.checkArgument(!globalFrame.mutability().isFrozen()); |
| 869 | Preconditions.checkArgument(!dynamicFrame.mutability().isFrozen()); |
brandjon | b712f33 | 2017-04-29 16:03:32 +0200 | [diff] [blame] | 870 | this.semantics = semantics; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 871 | this.eventHandler = eventHandler; |
| 872 | this.importedExtensions = importedExtensions; |
Luis Fernando Pino Duque | 3fedf9e | 2016-04-28 15:47:29 +0000 | [diff] [blame] | 873 | this.phase = phase; |
Kristina Chodorow | 704d8d9 | 2016-12-01 17:47:52 +0000 | [diff] [blame] | 874 | this.callerLabel = callerLabel; |
Janak Ramakrishnan | 9b12f9c | 2016-05-20 16:33:02 +0000 | [diff] [blame] | 875 | this.transitiveHashCode = |
| 876 | computeTransitiveContentHashCode(fileContentHashCode, importedExtensions); |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 877 | } |
| 878 | |
| 879 | /** |
brandjon | d9a1a43 | 2017-10-19 23:47:08 +0200 | [diff] [blame] | 880 | * A Builder class for Environment. |
| 881 | * |
| 882 | * <p>The caller must explicitly set the semantics by calling either {@link #setSemantics} or |
| 883 | * {@link #useDefaultSemantics}. |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 884 | */ |
| 885 | public static class Builder { |
| 886 | private final Mutability mutability; |
Luis Fernando Pino Duque | 3fedf9e | 2016-04-28 15:47:29 +0000 | [diff] [blame] | 887 | private Phase phase = Phase.ANALYSIS; |
nharmata | 68cc06b | 2018-03-01 10:21:57 -0800 | [diff] [blame] | 888 | @Nullable private GlobalFrame parent; |
brandjon | 3c16191 | 2017-10-05 05:06:05 +0200 | [diff] [blame] | 889 | @Nullable private SkylarkSemantics semantics; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 890 | @Nullable private EventHandler eventHandler; |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 891 | @Nullable private Map<String, Extension> importedExtensions; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 892 | @Nullable private String fileContentHashCode; |
Kristina Chodorow | 704d8d9 | 2016-12-01 17:47:52 +0000 | [diff] [blame] | 893 | private Label label; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 894 | |
| 895 | Builder(Mutability mutability) { |
| 896 | this.mutability = mutability; |
| 897 | } |
| 898 | |
Luis Fernando Pino Duque | 3fedf9e | 2016-04-28 15:47:29 +0000 | [diff] [blame] | 899 | /** Enables loading or workspace phase only functions in this Environment. */ |
| 900 | public Builder setPhase(Phase phase) { |
| 901 | Preconditions.checkState(this.phase == Phase.ANALYSIS); |
| 902 | this.phase = phase; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 903 | return this; |
| 904 | } |
| 905 | |
| 906 | /** Inherits global bindings from the given parent Frame. */ |
nharmata | 6070ba7 | 2018-03-01 13:35:33 -0800 | [diff] [blame] | 907 | public Builder setGlobals(GlobalFrame parent) { |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 908 | Preconditions.checkState(this.parent == null); |
laurentlb | c8e6796 | 2018-08-31 14:20:40 -0700 | [diff] [blame] | 909 | // Make sure that the global frame does at most two lookups: one for the module definitions |
| 910 | // and one for the builtins. |
nharmata | 6070ba7 | 2018-03-01 13:35:33 -0800 | [diff] [blame] | 911 | this.parent = parent; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 912 | return this; |
| 913 | } |
| 914 | |
brandjon | 3c16191 | 2017-10-05 05:06:05 +0200 | [diff] [blame] | 915 | public Builder setSemantics(SkylarkSemantics semantics) { |
brandjon | b712f33 | 2017-04-29 16:03:32 +0200 | [diff] [blame] | 916 | this.semantics = semantics; |
| 917 | return this; |
| 918 | } |
| 919 | |
brandjon | d9a1a43 | 2017-10-19 23:47:08 +0200 | [diff] [blame] | 920 | public Builder useDefaultSemantics() { |
| 921 | this.semantics = SkylarkSemantics.DEFAULT_SEMANTICS; |
| 922 | return this; |
| 923 | } |
| 924 | |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 925 | /** Sets an EventHandler for errors and warnings. */ |
| 926 | public Builder setEventHandler(EventHandler eventHandler) { |
| 927 | Preconditions.checkState(this.eventHandler == null); |
| 928 | this.eventHandler = eventHandler; |
| 929 | return this; |
| 930 | } |
| 931 | |
| 932 | /** Declares imported extensions for load() statements. */ |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 933 | public Builder setImportedExtensions(Map<String, Extension> importMap) { |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 934 | Preconditions.checkState(this.importedExtensions == null); |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 935 | this.importedExtensions = importMap; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 936 | return this; |
| 937 | } |
| 938 | |
| 939 | /** Declares content hash for the source file for this Environment. */ |
| 940 | public Builder setFileContentHashCode(String fileContentHashCode) { |
| 941 | this.fileContentHashCode = fileContentHashCode; |
| 942 | return this; |
| 943 | } |
| 944 | |
| 945 | /** Builds the Environment. */ |
| 946 | public Environment build() { |
brandjon | 33b4943 | 2017-05-04 18:58:52 +0200 | [diff] [blame] | 947 | Preconditions.checkArgument(!mutability.isFrozen()); |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 948 | if (parent != null) { |
brandjon | 73e768e | 2018-02-13 12:26:52 -0800 | [diff] [blame] | 949 | Preconditions.checkArgument(parent.mutability().isFrozen(), "parent frame must be frozen"); |
laurentlb | c8e6796 | 2018-08-31 14:20:40 -0700 | [diff] [blame] | 950 | if (parent.parent != null) { // This code path doesn't happen in Bazel. |
| 951 | |
| 952 | // Flatten the frame, ensure all builtins are in the same frame. |
| 953 | parent = |
| 954 | new GlobalFrame( |
| 955 | parent.mutability(), |
| 956 | null /* parent */, |
| 957 | parent.label, |
| 958 | parent.getTransitiveBindings()); |
| 959 | } |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 960 | } |
nharmata | 68cc06b | 2018-03-01 10:21:57 -0800 | [diff] [blame] | 961 | GlobalFrame globalFrame = new GlobalFrame(mutability, parent); |
| 962 | LexicalFrame dynamicFrame = LexicalFrame.create(mutability); |
brandjon | b712f33 | 2017-04-29 16:03:32 +0200 | [diff] [blame] | 963 | if (semantics == null) { |
brandjon | b368b39 | 2017-10-20 22:02:02 +0200 | [diff] [blame] | 964 | throw new IllegalArgumentException("must call either setSemantics or useDefaultSemantics"); |
brandjon | b712f33 | 2017-04-29 16:03:32 +0200 | [diff] [blame] | 965 | } |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 966 | if (importedExtensions == null) { |
| 967 | importedExtensions = ImmutableMap.of(); |
| 968 | } |
Laurent Le Brun | 4617109 | 2015-12-23 14:04:23 +0000 | [diff] [blame] | 969 | return new Environment( |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 970 | globalFrame, |
| 971 | dynamicFrame, |
brandjon | b712f33 | 2017-04-29 16:03:32 +0200 | [diff] [blame] | 972 | semantics, |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 973 | eventHandler, |
| 974 | importedExtensions, |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 975 | fileContentHashCode, |
Kristina Chodorow | 704d8d9 | 2016-12-01 17:47:52 +0000 | [diff] [blame] | 976 | phase, |
| 977 | label); |
| 978 | } |
| 979 | |
| 980 | public Builder setCallerLabel(Label label) { |
| 981 | this.label = label; |
| 982 | return this; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 983 | } |
| 984 | } |
| 985 | |
| 986 | public static Builder builder(Mutability mutability) { |
| 987 | return new Builder(mutability); |
| 988 | } |
| 989 | |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 990 | /** Returns the caller's label. */ |
Kristina Chodorow | 704d8d9 | 2016-12-01 17:47:52 +0000 | [diff] [blame] | 991 | public Label getCallerLabel() { |
| 992 | return callerLabel; |
| 993 | } |
| 994 | |
| 995 | /** |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 996 | * Sets a binding for a special dynamic variable in this Environment. This is not for end-users, |
| 997 | * and will throw an AssertionError in case of conflict. |
| 998 | * |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 999 | * @param varname the name of the dynamic variable to be bound |
| 1000 | * @param value a value to bind to the variable |
| 1001 | * @return this Environment, in fluid style |
| 1002 | */ |
| 1003 | public Environment setupDynamic(String varname, Object value) { |
laurentlb | 5d26ef1 | 2018-08-27 12:23:15 -0700 | [diff] [blame] | 1004 | if (lookup(varname) != null) { |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 1005 | throw new AssertionError( |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 1006 | String.format("Trying to bind dynamic variable '%s' but it is already bound", varname)); |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 1007 | } |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 1008 | try { |
| 1009 | dynamicFrame.put(this, varname, value); |
| 1010 | } catch (MutabilityException e) { |
| 1011 | // End users don't have access to setupDynamic, and it is an implementation error |
| 1012 | // if we encounter a mutability exception. |
| 1013 | throw new AssertionError( |
vladmos | 4690793 | 2017-06-30 14:01:45 +0200 | [diff] [blame] | 1014 | String.format( |
| 1015 | "Trying to bind dynamic variable '%s' in frozen environment %s", varname, this), |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 1016 | e); |
| 1017 | } |
| 1018 | return this; |
| 1019 | } |
| 1020 | |
laurentlb | 9d5c0a0 | 2017-06-13 23:08:06 +0200 | [diff] [blame] | 1021 | /** Remove variable from local bindings. */ |
| 1022 | void removeLocalBinding(String varname) { |
| 1023 | try { |
laurentlb | 5d26ef1 | 2018-08-27 12:23:15 -0700 | [diff] [blame] | 1024 | lexicalFrame.remove(this, varname); |
laurentlb | 9d5c0a0 | 2017-06-13 23:08:06 +0200 | [diff] [blame] | 1025 | } catch (MutabilityException e) { |
| 1026 | throw new AssertionError(e); |
| 1027 | } |
| 1028 | } |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 1029 | |
nharmata | f101348 | 2018-03-07 10:48:31 -0800 | [diff] [blame] | 1030 | /** |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 1031 | * Modifies a binding in the current Frame of this Environment, as would an {@link |
| 1032 | * AssignmentStatement}. Does not try to modify an inherited binding. This will shadow any |
| 1033 | * inherited binding, which may be an error that you want to guard against before calling this |
| 1034 | * function. |
| 1035 | * |
nharmata | f101348 | 2018-03-07 10:48:31 -0800 | [diff] [blame] | 1036 | * @param varname the name of the variable to be bound |
| 1037 | * @param value the value to bind to the variable |
| 1038 | * @return this Environment, in fluid style |
| 1039 | */ |
| 1040 | public Environment update(String varname, Object value) throws EvalException { |
laurentlb | 9dc4e20 | 2018-09-06 09:34:13 -0700 | [diff] [blame] | 1041 | Preconditions.checkNotNull(value, "trying to assign null to '%s'", varname); |
nharmata | 6e233f0 | 2018-03-07 16:02:22 -0800 | [diff] [blame] | 1042 | if (isKnownGlobalVariable(varname)) { |
| 1043 | throw new EvalException( |
laurentlb | 9dc4e20 | 2018-09-06 09:34:13 -0700 | [diff] [blame] | 1044 | null, |
| 1045 | String.format( |
| 1046 | "Variable '%s' is referenced before assignment. " |
| 1047 | + "The variable is defined in the global scope.", |
| 1048 | varname)); |
nharmata | 6e233f0 | 2018-03-07 16:02:22 -0800 | [diff] [blame] | 1049 | } |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 1050 | try { |
laurentlb | 9dc4e20 | 2018-09-06 09:34:13 -0700 | [diff] [blame] | 1051 | lexicalFrame.put(this, varname, value); |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 1052 | } catch (MutabilityException e) { |
| 1053 | // Note that since at this time we don't accept the global keyword, and don't have closures, |
| 1054 | // end users should never be able to mutate a frozen Environment, and a MutabilityException |
| 1055 | // is therefore a failed assertion for Bazel. However, it is possible to shadow a binding |
| 1056 | // imported from a parent Environment by updating the current Environment, which will not |
| 1057 | // trigger a MutabilityException. |
| 1058 | throw new AssertionError( |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 1059 | Printer.format("Can't update %s to %r in frozen environment", varname, value), e); |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 1060 | } |
| 1061 | return this; |
| 1062 | } |
| 1063 | |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 1064 | /** |
| 1065 | * Initializes a binding in this Environment. It is an error if the variable is already bound. |
| 1066 | * This is not for end-users, and will throw an AssertionError in case of conflict. |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 1067 | * |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 1068 | * @param varname the name of the variable to be bound |
| 1069 | * @param value the value to bind to the variable |
| 1070 | * @return this Environment, in fluid style |
| 1071 | */ |
| 1072 | public Environment setup(String varname, Object value) { |
laurentlb | 1434b17 | 2018-08-27 11:32:34 -0700 | [diff] [blame] | 1073 | if (lookup(varname) != null) { |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 1074 | throw new AssertionError(String.format("variable '%s' already bound", varname)); |
| 1075 | } |
| 1076 | return setupOverride(varname, value); |
| 1077 | } |
| 1078 | |
| 1079 | /** |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 1080 | * Initializes a binding in this environment. Overrides any previous binding. This is not for |
| 1081 | * end-users, and will throw an AssertionError in case of conflict. |
| 1082 | * |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 1083 | * @param varname the name of the variable to be bound |
| 1084 | * @param value the value to bind to the variable |
| 1085 | * @return this Environment, in fluid style |
| 1086 | */ |
| 1087 | public Environment setupOverride(String varname, Object value) { |
| 1088 | try { |
| 1089 | return update(varname, value); |
| 1090 | } catch (EvalException ee) { |
| 1091 | throw new AssertionError(ee); |
| 1092 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | /** |
laurentlb | ebb4071 | 2018-08-29 11:54:27 -0700 | [diff] [blame] | 1096 | * Returns the value of a variable defined in Local scope. Do not search in any parent scope. This |
| 1097 | * function should be used once the AST has been analysed and we know which variables are local. |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 1098 | */ |
| 1099 | public Object localLookup(String varname) { |
| 1100 | return lexicalFrame.get(varname); |
| 1101 | } |
| 1102 | |
| 1103 | /** |
laurentlb | c8e6796 | 2018-08-31 14:20:40 -0700 | [diff] [blame] | 1104 | * Returns the value of a variable defined in the Module scope (e.g. global variables, functions). |
| 1105 | * |
| 1106 | * <p>TODO(laurentlb): This method may also return values from the universe. We should fix that. |
laurentlb | ebb4071 | 2018-08-29 11:54:27 -0700 | [diff] [blame] | 1107 | */ |
| 1108 | public Object moduleLookup(String varname) { |
| 1109 | return globalFrame.get(varname); |
| 1110 | } |
| 1111 | |
| 1112 | /** Returns the value of a variable defined in the Universe scope (builtins). */ |
| 1113 | public Object universeLookup(String varname) { |
laurentlb | c8e6796 | 2018-08-31 14:20:40 -0700 | [diff] [blame] | 1114 | // TODO(laurentlb): look only at globalFrame.parent. |
laurentlb | ebb4071 | 2018-08-29 11:54:27 -0700 | [diff] [blame] | 1115 | return globalFrame.get(varname); |
| 1116 | } |
| 1117 | |
| 1118 | /** Returns the value of a variable defined with setupDynamic. */ |
| 1119 | public Object dynamicLookup(String varname) { |
| 1120 | return dynamicFrame.get(varname); |
| 1121 | } |
| 1122 | |
| 1123 | /** |
brandjon | 73e768e | 2018-02-13 12:26:52 -0800 | [diff] [blame] | 1124 | * Returns the value from the environment whose name is "varname" if it exists, otherwise null. |
laurentlb | ebb4071 | 2018-08-29 11:54:27 -0700 | [diff] [blame] | 1125 | * |
| 1126 | * <p>TODO(laurentlb): Remove this method. Callers should know where the value is defined and use |
| 1127 | * the corresponding method (e.g. localLookup or moduleLookup). |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1128 | */ |
laurentlb | c3e5851 | 2018-09-04 10:11:32 -0700 | [diff] [blame] | 1129 | Object lookup(String varname) { |
brandjon | cc0f6a6 | 2017-05-08 13:19:21 -0400 | [diff] [blame] | 1130 | // Lexical frame takes precedence, then globals, then dynamics. |
laurentlb | 5d26ef1 | 2018-08-27 12:23:15 -0700 | [diff] [blame] | 1131 | Object lexicalValue = lexicalFrame.get(varname); |
| 1132 | if (lexicalValue != null) { |
| 1133 | return lexicalValue; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 1134 | } |
| 1135 | Object globalValue = globalFrame.get(varname); |
| 1136 | Object dynamicValue = dynamicFrame.get(varname); |
| 1137 | if (globalValue == null && dynamicValue == null) { |
Vladimir Moskva | 4994cb3 | 2016-08-10 15:44:31 +0000 | [diff] [blame] | 1138 | return null; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1139 | } |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 1140 | if (knownGlobalVariables != null) { |
| 1141 | knownGlobalVariables.add(varname); |
| 1142 | } |
| 1143 | if (globalValue != null) { |
| 1144 | return globalValue; |
| 1145 | } |
| 1146 | return dynamicValue; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1147 | } |
| 1148 | |
| 1149 | /** |
brandjon | 73e768e | 2018-02-13 12:26:52 -0800 | [diff] [blame] | 1150 | * Returns true if varname is a known global variable (i.e., it has been read in the context of |
| 1151 | * the current function). |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1152 | */ |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 1153 | boolean isKnownGlobalVariable(String varname) { |
laurentlb | 9dc4e20 | 2018-09-06 09:34:13 -0700 | [diff] [blame] | 1154 | return !semantics.incompatibleStaticNameResolution() |
| 1155 | && knownGlobalVariables != null |
| 1156 | && knownGlobalVariables.contains(varname); |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 1157 | } |
| 1158 | |
brandjon | 3c16191 | 2017-10-05 05:06:05 +0200 | [diff] [blame] | 1159 | public SkylarkSemantics getSemantics() { |
brandjon | b712f33 | 2017-04-29 16:03:32 +0200 | [diff] [blame] | 1160 | return semantics; |
| 1161 | } |
| 1162 | |
Vladimir Moskva | dfad9e3 | 2016-09-15 18:22:01 +0000 | [diff] [blame] | 1163 | public void handleEvent(Event event) { |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 1164 | eventHandler.handle(event); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1165 | } |
| 1166 | |
brandjon | 73e768e | 2018-02-13 12:26:52 -0800 | [diff] [blame] | 1167 | /** |
| 1168 | * Returns a set of all names of variables that are accessible in this {@code Environment}, in a |
| 1169 | * deterministic order. |
| 1170 | */ |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1171 | public Set<String> getVariableNames() { |
brandjon | 73e768e | 2018-02-13 12:26:52 -0800 | [diff] [blame] | 1172 | LinkedHashSet<String> vars = new LinkedHashSet<>(); |
laurentlb | 5d26ef1 | 2018-08-27 12:23:15 -0700 | [diff] [blame] | 1173 | vars.addAll(lexicalFrame.getTransitiveBindings().keySet()); |
| 1174 | // No-op when globalFrame = lexicalFrame |
brandjon | cc0f6a6 | 2017-05-08 13:19:21 -0400 | [diff] [blame] | 1175 | vars.addAll(globalFrame.getTransitiveBindings().keySet()); |
| 1176 | vars.addAll(dynamicFrame.getTransitiveBindings().keySet()); |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 1177 | return vars; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1178 | } |
| 1179 | |
Googler | 29eafdf | 2018-05-23 12:32:07 -0700 | [diff] [blame] | 1180 | private static final class EvalEventHandler implements EventHandler { |
| 1181 | List<String> messages = new ArrayList<>(); |
| 1182 | |
| 1183 | @Override |
| 1184 | public void handle(Event event) { |
| 1185 | if (event.getKind() == EventKind.ERROR) { |
| 1186 | messages.add(event.getMessage()); |
| 1187 | } |
| 1188 | } |
| 1189 | } |
| 1190 | |
| 1191 | @Override |
brendandouglas | 01683d3 | 2018-06-26 08:53:10 -0700 | [diff] [blame] | 1192 | public Object evaluate(String contents) throws EvalException, InterruptedException { |
| 1193 | ParserInputSource input = |
| 1194 | ParserInputSource.create(contents, PathFragment.create("<debug eval>")); |
Googler | 29eafdf | 2018-05-23 12:32:07 -0700 | [diff] [blame] | 1195 | EvalEventHandler eventHandler = new EvalEventHandler(); |
brendandouglas | 01683d3 | 2018-06-26 08:53:10 -0700 | [diff] [blame] | 1196 | Statement statement = Parser.parseStatement(input, eventHandler, ParsingLevel.LOCAL_LEVEL); |
Googler | 29eafdf | 2018-05-23 12:32:07 -0700 | [diff] [blame] | 1197 | if (!eventHandler.messages.isEmpty()) { |
brendandouglas | 01683d3 | 2018-06-26 08:53:10 -0700 | [diff] [blame] | 1198 | throw new EvalException(statement.getLocation(), eventHandler.messages.get(0)); |
Googler | 29eafdf | 2018-05-23 12:32:07 -0700 | [diff] [blame] | 1199 | } |
brendandouglas | 01683d3 | 2018-06-26 08:53:10 -0700 | [diff] [blame] | 1200 | // TODO(bazel-team): move statement handling code to Eval |
| 1201 | // deal with the most common case first |
| 1202 | if (statement.kind() == Statement.Kind.EXPRESSION) { |
| 1203 | return ((ExpressionStatement) statement).getExpression().doEval(this); |
| 1204 | } |
| 1205 | // all other statement types are executed directly |
| 1206 | Eval.fromEnvironment(this).exec(statement); |
| 1207 | switch (statement.kind()) { |
| 1208 | case ASSIGNMENT: |
| 1209 | case AUGMENTED_ASSIGNMENT: |
| 1210 | return ((AssignmentStatement) statement).getLValue().getExpression().doEval(this); |
| 1211 | case RETURN: |
| 1212 | Expression expr = ((ReturnStatement) statement).getReturnExpression(); |
| 1213 | return expr != null ? expr.doEval(this) : Runtime.NONE; |
| 1214 | default: |
| 1215 | return Runtime.NONE; |
| 1216 | } |
Googler | 29eafdf | 2018-05-23 12:32:07 -0700 | [diff] [blame] | 1217 | } |
| 1218 | |
| 1219 | @Override |
| 1220 | public ImmutableList<DebugFrame> listFrames(Location currentLocation) { |
| 1221 | ImmutableList.Builder<DebugFrame> frameListBuilder = ImmutableList.builder(); |
| 1222 | |
| 1223 | Continuation currentContinuation = continuation; |
laurentlb | 5d26ef1 | 2018-08-27 12:23:15 -0700 | [diff] [blame] | 1224 | Frame currentFrame = lexicalFrame; |
Googler | 29eafdf | 2018-05-23 12:32:07 -0700 | [diff] [blame] | 1225 | |
| 1226 | // if there's a continuation then the current frame is a lexical frame |
| 1227 | while (currentContinuation != null) { |
| 1228 | frameListBuilder.add( |
| 1229 | DebugFrame.builder() |
| 1230 | .setLexicalFrameBindings(ImmutableMap.copyOf(currentFrame.getTransitiveBindings())) |
| 1231 | .setGlobalBindings(ImmutableMap.copyOf(getGlobals().getTransitiveBindings())) |
| 1232 | .setFunctionName(currentContinuation.function.getFullName()) |
| 1233 | .setLocation(currentLocation) |
| 1234 | .build()); |
| 1235 | |
| 1236 | currentFrame = currentContinuation.lexicalFrame; |
brendandouglas | fe785e1 | 2018-06-12 09:39:38 -0700 | [diff] [blame] | 1237 | currentLocation = |
| 1238 | currentContinuation.caller != null ? currentContinuation.caller.getLocation() : null; |
Googler | 29eafdf | 2018-05-23 12:32:07 -0700 | [diff] [blame] | 1239 | currentContinuation = currentContinuation.continuation; |
| 1240 | } |
| 1241 | |
| 1242 | frameListBuilder.add( |
| 1243 | DebugFrame.builder() |
| 1244 | .setGlobalBindings(ImmutableMap.copyOf(getGlobals().getTransitiveBindings())) |
| 1245 | .setFunctionName("<top level>") |
| 1246 | .setLocation(currentLocation) |
| 1247 | .build()); |
| 1248 | |
| 1249 | return frameListBuilder.build(); |
| 1250 | } |
| 1251 | |
| 1252 | @Override |
| 1253 | @Nullable |
| 1254 | public ReadyToPause stepControl(Stepping stepping) { |
| 1255 | final Continuation pausedContinuation = continuation; |
| 1256 | |
| 1257 | switch (stepping) { |
| 1258 | case NONE: |
| 1259 | return null; |
| 1260 | case INTO: |
| 1261 | // pause at the very next statement |
| 1262 | return env -> true; |
| 1263 | case OVER: |
| 1264 | return env -> isAt(env, pausedContinuation) || isOutside(env, pausedContinuation); |
| 1265 | case OUT: |
| 1266 | // if we're at the outer-most frame, same as NONE |
| 1267 | return pausedContinuation == null ? null : env -> isOutside(env, pausedContinuation); |
| 1268 | } |
| 1269 | throw new IllegalArgumentException("Unsupported stepping type: " + stepping); |
| 1270 | } |
| 1271 | |
| 1272 | /** Returns true if {@code env} is in a parent frame of {@code pausedContinuation}. */ |
| 1273 | private static boolean isOutside(Environment env, @Nullable Continuation pausedContinuation) { |
| 1274 | return pausedContinuation != null && env.continuation == pausedContinuation.continuation; |
| 1275 | } |
| 1276 | |
| 1277 | /** Returns true if {@code env} is at the same frame as {@code pausedContinuation. */ |
| 1278 | private static boolean isAt(Environment env, @Nullable Continuation pausedContinuation) { |
| 1279 | return env.continuation == pausedContinuation; |
| 1280 | } |
| 1281 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1282 | @Override |
| 1283 | public int hashCode() { |
| 1284 | throw new UnsupportedOperationException(); // avoid nondeterminism |
| 1285 | } |
| 1286 | |
| 1287 | @Override |
| 1288 | public boolean equals(Object that) { |
| 1289 | throw new UnsupportedOperationException(); |
| 1290 | } |
| 1291 | |
| 1292 | @Override |
| 1293 | public String toString() { |
Francois-Rene Rideau | bd0c7bb | 2015-09-21 16:54:19 +0000 | [diff] [blame] | 1294 | return String.format("<Environment%s>", mutability()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1295 | } |
| 1296 | |
| 1297 | /** |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 1298 | * An Exception thrown when an attempt is made to import a symbol from a file that was not |
| 1299 | * properly loaded. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1300 | */ |
Janak Ramakrishnan | 9b12f9c | 2016-05-20 16:33:02 +0000 | [diff] [blame] | 1301 | static class LoadFailedException extends Exception { |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 1302 | LoadFailedException(String importString) { |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 1303 | super( |
| 1304 | String.format( |
| 1305 | "file '%s' was not correctly loaded. " |
| 1306 | + "Make sure the 'load' statement appears in the global scope in your file", |
| 1307 | importString)); |
Vladimir Moskva | 4994cb3 | 2016-08-10 15:44:31 +0000 | [diff] [blame] | 1308 | } |
| 1309 | |
Laurent Le Brun | a2501d5 | 2017-01-02 15:42:19 +0000 | [diff] [blame] | 1310 | LoadFailedException(String importString, String symbolString, Iterable<String> allKeys) { |
| 1311 | super( |
| 1312 | String.format( |
| 1313 | "file '%s' does not contain symbol '%s'%s", |
| 1314 | importString, symbolString, SpellChecker.didYouMean(symbolString, allKeys))); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1315 | } |
| 1316 | } |
| 1317 | |
Janak Ramakrishnan | 9b12f9c | 2016-05-20 16:33:02 +0000 | [diff] [blame] | 1318 | void importSymbol(String importString, Identifier symbol, String nameInLoadedFile) |
Vladimir Moskva | 4994cb3 | 2016-08-10 15:44:31 +0000 | [diff] [blame] | 1319 | throws LoadFailedException { |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 1320 | Preconditions.checkState(isGlobal()); // loading is only allowed at global scope. |
| 1321 | |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 1322 | if (!importedExtensions.containsKey(importString)) { |
| 1323 | throw new LoadFailedException(importString); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1324 | } |
Florian Weikert | 9d659ad | 2015-07-23 14:44:36 +0000 | [diff] [blame] | 1325 | |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 1326 | Extension ext = importedExtensions.get(importString); |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 1327 | |
brandjon | 357ed7a | 2017-05-09 11:37:22 -0400 | [diff] [blame] | 1328 | Map<String, Object> bindings = ext.getBindings(); |
| 1329 | if (!bindings.containsKey(nameInLoadedFile)) { |
| 1330 | throw new LoadFailedException(importString, nameInLoadedFile, bindings.keySet()); |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 1331 | } |
| 1332 | |
brandjon | 357ed7a | 2017-05-09 11:37:22 -0400 | [diff] [blame] | 1333 | Object value = bindings.get(nameInLoadedFile); |
Florian Weikert | 9d659ad | 2015-07-23 14:44:36 +0000 | [diff] [blame] | 1334 | |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 1335 | try { |
| 1336 | update(symbol.getName(), value); |
| 1337 | } catch (EvalException e) { |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 1338 | throw new LoadFailedException(importString); |
Francois-Rene Rideau | 5a94e59 | 2015-09-04 19:13:47 +0000 | [diff] [blame] | 1339 | } |
Florian Weikert | 6a66339 | 2015-09-02 14:04:33 +0000 | [diff] [blame] | 1340 | } |
| 1341 | |
brandjon | 73e768e | 2018-02-13 12:26:52 -0800 | [diff] [blame] | 1342 | /** |
| 1343 | * Computes a deterministic hash for the given base hash code and extension map (the map's order |
| 1344 | * does not matter). |
| 1345 | */ |
Janak Ramakrishnan | 9b12f9c | 2016-05-20 16:33:02 +0000 | [diff] [blame] | 1346 | private static String computeTransitiveContentHashCode( |
| 1347 | @Nullable String baseHashCode, Map<String, Extension> importedExtensions) { |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 1348 | // Calculate a new hash from the hash of the loaded Extension-s. |
| 1349 | Fingerprint fingerprint = new Fingerprint(); |
Janak Ramakrishnan | 9b12f9c | 2016-05-20 16:33:02 +0000 | [diff] [blame] | 1350 | if (baseHashCode != null) { |
| 1351 | fingerprint.addString(Preconditions.checkNotNull(baseHashCode)); |
| 1352 | } |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 1353 | TreeSet<String> importStrings = new TreeSet<>(importedExtensions.keySet()); |
| 1354 | for (String importString : importStrings) { |
| 1355 | fingerprint.addString(importedExtensions.get(importString).getTransitiveContentHashCode()); |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 1356 | } |
| 1357 | return fingerprint.hexDigestAndReset(); |
Janak Ramakrishnan | b6e33bc | 2015-09-06 21:05:23 +0000 | [diff] [blame] | 1358 | } |
| 1359 | |
Janak Ramakrishnan | 9b12f9c | 2016-05-20 16:33:02 +0000 | [diff] [blame] | 1360 | /** |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 1361 | * Returns a hash code calculated from the hash code of this Environment and the transitive |
| 1362 | * closure of other Environments it loads. |
Janak Ramakrishnan | 9b12f9c | 2016-05-20 16:33:02 +0000 | [diff] [blame] | 1363 | */ |
| 1364 | public String getTransitiveContentHashCode() { |
| 1365 | return transitiveHashCode; |
| 1366 | } |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 1367 | |
brandjon | b80eb18 | 2018-05-01 10:54:58 -0700 | [diff] [blame] | 1368 | /** A read-only {@link Environment.GlobalFrame} with False/True/None constants only. */ |
janakr | a1f1761 | 2018-08-17 06:01:51 -0700 | [diff] [blame] | 1369 | @AutoCodec static final GlobalFrame CONSTANTS_ONLY = createConstantsGlobals(); |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 1370 | |
brandjon | b80eb18 | 2018-05-01 10:54:58 -0700 | [diff] [blame] | 1371 | /** |
janakr | a1f1761 | 2018-08-17 06:01:51 -0700 | [diff] [blame] | 1372 | * A read-only {@link Environment.GlobalFrame} with initial globals as defined in MethodLibrary. |
brandjon | b80eb18 | 2018-05-01 10:54:58 -0700 | [diff] [blame] | 1373 | */ |
janakr | a1f1761 | 2018-08-17 06:01:51 -0700 | [diff] [blame] | 1374 | @AutoCodec public static final GlobalFrame DEFAULT_GLOBALS = createDefaultGlobals(); |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 1375 | |
Laurent Le Brun | 5e99198 | 2016-10-14 13:39:45 +0000 | [diff] [blame] | 1376 | /** To be removed when all call-sites are updated. */ |
nharmata | 68cc06b | 2018-03-01 10:21:57 -0800 | [diff] [blame] | 1377 | public static final GlobalFrame SKYLARK = DEFAULT_GLOBALS; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 1378 | |
nharmata | 68cc06b | 2018-03-01 10:21:57 -0800 | [diff] [blame] | 1379 | private static Environment.GlobalFrame createConstantsGlobals() { |
brandjon | b80eb18 | 2018-05-01 10:54:58 -0700 | [diff] [blame] | 1380 | ImmutableMap.Builder<String, Object> builder = ImmutableMap.builder(); |
| 1381 | Runtime.addConstantsToBuilder(builder); |
| 1382 | return GlobalFrame.createForBuiltins(builder.build()); |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 1383 | } |
| 1384 | |
nharmata | 68cc06b | 2018-03-01 10:21:57 -0800 | [diff] [blame] | 1385 | private static Environment.GlobalFrame createDefaultGlobals() { |
brandjon | b80eb18 | 2018-05-01 10:54:58 -0700 | [diff] [blame] | 1386 | ImmutableMap.Builder<String, Object> builder = ImmutableMap.builder(); |
| 1387 | Runtime.addConstantsToBuilder(builder); |
| 1388 | MethodLibrary.addBindingsToBuilder(builder); |
| 1389 | return GlobalFrame.createForBuiltins(builder.build()); |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 1390 | } |
| 1391 | |
brandjon | e5d95fb | 2017-07-13 17:23:09 +0200 | [diff] [blame] | 1392 | /** An exception thrown by {@link #FAIL_FAST_HANDLER}. */ |
| 1393 | // TODO(bazel-team): Possibly extend RuntimeException instead of IllegalArgumentException. |
| 1394 | public static class FailFastException extends IllegalArgumentException { |
| 1395 | public FailFastException(String s) { |
| 1396 | super(s); |
| 1397 | } |
| 1398 | } |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 1399 | |
| 1400 | /** |
brandjon | e5d95fb | 2017-07-13 17:23:09 +0200 | [diff] [blame] | 1401 | * A handler that immediately throws {@link FailFastException} whenever an error or warning |
| 1402 | * occurs. |
| 1403 | * |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 1404 | * <p>We do not reuse an existing unchecked exception type, because callers (e.g., test |
| 1405 | * assertions) need to be able to distinguish between organically occurring exceptions and |
| 1406 | * exceptions thrown by this handler. |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 1407 | */ |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 1408 | public static final EventHandler FAIL_FAST_HANDLER = |
| 1409 | new EventHandler() { |
| 1410 | @Override |
| 1411 | public void handle(Event event) { |
| 1412 | if (EventKind.ERRORS_AND_WARNINGS.contains(event.getKind())) { |
| 1413 | throw new FailFastException(event.toString()); |
| 1414 | } |
| 1415 | } |
| 1416 | }; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1417 | } |