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