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