variables.h 11 KB
Newer Older
1
// Copyright 2011 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5 6
#ifndef V8_AST_VARIABLES_H_
#define V8_AST_VARIABLES_H_
7

8
#include "src/ast/ast-value-factory.h"
9
#include "src/base/threaded-list.h"
10
#include "src/common/globals.h"
11
#include "src/execution/isolate.h"
12
#include "src/zone/zone.h"
13

14 15
namespace v8 {
namespace internal {
16 17 18 19 20

// The AST refers to variables via VariableProxies - placeholders for the actual
// variables. Variables themselves are never directly referred to from the AST,
// they are maintained by scopes, and referred to from VariableProxies and Slots
// after binding and variable allocation.
21
class Variable final : public ZoneObject {
22
 public:
23 24
  Variable(Scope* scope, const AstRawString* name, VariableMode mode,
           VariableKind kind, InitializationFlag initialization_flag,
25 26
           MaybeAssignedFlag maybe_assigned_flag = kNotAssigned,
           IsStaticFlag is_static_flag = IsStaticFlag::kNotStatic)
27 28 29 30 31 32 33 34 35 36
      : scope_(scope),
        name_(name),
        local_if_not_shadowed_(nullptr),
        next_(nullptr),
        index_(-1),
        initializer_position_(kNoSourcePosition),
        bit_field_(MaybeAssignedFlagField::encode(maybe_assigned_flag) |
                   InitializationFlagField::encode(initialization_flag) |
                   VariableModeField::encode(mode) |
                   IsUsedField::encode(false) |
37
                   ForceContextAllocationBit::encode(false) |
38 39
                   ForceHoleInitializationField::encode(false) |
                   LocationField::encode(VariableLocation::UNALLOCATED) |
40 41
                   VariableKindField::encode(kind) |
                   IsStaticFlagField::encode(is_static_flag)) {
42
    // Var declared variables never need initialization.
43 44
    DCHECK(!(mode == VariableMode::kVar &&
             initialization_flag == kNeedsInitialization));
45 46
    DCHECK_IMPLIES(is_static_flag == IsStaticFlag::kStatic,
                   IsConstVariableMode(mode));
47
  }
48

49 50
  explicit Variable(Variable* other);

51 52
  // The source code for an eval() call may refer to a variable that is
  // in an outer scope about which we don't know anything (it may not
53
  // be the script scope). scope() is nullptr in that case. Currently the
54
  // scope is only used to follow the context chain length.
55
  Scope* scope() const { return scope_; }
56

57 58 59 60
  // This is for adjusting the scope of temporaries used when desugaring
  // parameter initializers.
  void set_scope(Scope* scope) { scope_ = scope; }

61
  Handle<String> name() const { return name_->string(); }
62
  const AstRawString* raw_name() const { return name_; }
63
  VariableMode mode() const { return VariableModeField::decode(bit_field_); }
64 65 66
  void set_mode(VariableMode mode) {
    bit_field_ = VariableModeField::update(bit_field_, mode);
  }
67 68 69 70 71 72 73 74
  void set_is_static_flag(IsStaticFlag is_static_flag) {
    bit_field_ = IsStaticFlagField::update(bit_field_, is_static_flag);
  }
  IsStaticFlag is_static_flag() const {
    return IsStaticFlagField::decode(bit_field_);
  }
  bool is_static() const { return is_static_flag() == IsStaticFlag::kStatic; }

75
  bool has_forced_context_allocation() const {
76
    return ForceContextAllocationBit::decode(bit_field_);
77
  }
78
  void ForceContextAllocation() {
79
    DCHECK(IsUnallocated() || IsContextSlot() || IsLookupSlot() ||
80
           location() == VariableLocation::MODULE);
81
    bit_field_ = ForceContextAllocationBit::update(bit_field_, true);
82 83 84 85 86 87
  }
  bool is_used() { return IsUsedField::decode(bit_field_); }
  void set_is_used() { bit_field_ = IsUsedField::update(bit_field_, true); }
  MaybeAssignedFlag maybe_assigned() const {
    return MaybeAssignedFlagField::decode(bit_field_);
  }
88 89 90
  void clear_maybe_assigned() {
    bit_field_ = MaybeAssignedFlagField::update(bit_field_, kNotAssigned);
  }
91
  void SetMaybeAssigned() {
92
    if (mode() == VariableMode::kConst) return;
93 94 95 96
    // Private names are only initialized once by us.
    if (name_->IsPrivateName()) {
      return;
    }
97 98 99 100 101 102 103 104
    // If this variable is dynamically shadowing another variable, then that
    // variable could also be assigned (in the non-shadowing case).
    if (has_local_if_not_shadowed()) {
      // Avoid repeatedly marking the same tree of variables by only recursing
      // when this variable's maybe_assigned status actually changes.
      if (!maybe_assigned()) {
        local_if_not_shadowed()->SetMaybeAssigned();
      }
105 106
      DCHECK_IMPLIES(local_if_not_shadowed()->mode() != VariableMode::kConst,
                     local_if_not_shadowed()->maybe_assigned());
107 108
    }
    set_maybe_assigned();
109
  }
110

111
  bool requires_brand_check() const {
112
    return IsPrivateMethodOrAccessorVariableMode(mode());
113 114
  }

115 116 117
  int initializer_position() { return initializer_position_; }
  void set_initializer_position(int pos) { initializer_position_ = pos; }

118
  bool IsUnallocated() const {
119
    return location() == VariableLocation::UNALLOCATED;
120
  }
121 122
  bool IsParameter() const { return location() == VariableLocation::PARAMETER; }
  bool IsStackLocal() const { return location() == VariableLocation::LOCAL; }
123
  bool IsStackAllocated() const { return IsParameter() || IsStackLocal(); }
124 125
  bool IsContextSlot() const { return location() == VariableLocation::CONTEXT; }
  bool IsLookupSlot() const { return location() == VariableLocation::LOOKUP; }
126
  bool IsGlobalObjectProperty() const;
127

128 129 130
  // True for 'let' and 'const' variables declared in the script scope of a REPL
  // script.
  bool IsReplGlobal() const;
Simon Zünd's avatar
Simon Zünd committed
131

132
  bool is_dynamic() const { return IsDynamicVariableMode(mode()); }
133 134 135 136 137 138 139 140 141 142 143

  // Returns the InitializationFlag this Variable was created with.
  // Scope analysis may allow us to relax this initialization
  // requirement, which will be reflected in the return value of
  // binding_needs_init().
  InitializationFlag initialization_flag() const {
    return InitializationFlagField::decode(bit_field_);
  }

  // Whether this variable needs to be initialized with the hole at
  // declaration time. Only returns valid results after scope analysis.
144
  bool binding_needs_init() const {
145
    DCHECK_IMPLIES(initialization_flag() == kNeedsInitialization,
146 147
                   IsLexicalVariableMode(mode()) ||
                       IsPrivateMethodOrAccessorVariableMode(mode()));
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
    DCHECK_IMPLIES(ForceHoleInitializationField::decode(bit_field_),
                   initialization_flag() == kNeedsInitialization);

    // Always initialize if hole initialization was forced during
    // scope analysis.
    if (ForceHoleInitializationField::decode(bit_field_)) return true;

    // If initialization was not forced, no need for initialization
    // for stack allocated variables, since UpdateNeedsHoleCheck()
    // in scopes.cc has proven that no VariableProxy refers to
    // this variable in such a way that a runtime hole check
    // would be generated.
    if (IsStackAllocated()) return false;

    // Otherwise, defer to the flag set when this Variable was constructed.
163
    return initialization_flag() == kNeedsInitialization;
164
  }
165 166 167 168 169 170

  // Called during scope analysis when a VariableProxy is found to
  // reference this Variable in such a way that a hole check will
  // be required at runtime.
  void ForceHoleInitialization() {
    DCHECK_EQ(kNeedsInitialization, initialization_flag());
171 172
    DCHECK(IsLexicalVariableMode(mode()) ||
           IsPrivateMethodOrAccessorVariableMode(mode()));
173 174 175
    bit_field_ = ForceHoleInitializationField::update(bit_field_, true);
  }

176
  bool throw_on_const_assignment(LanguageMode language_mode) const {
177
    return kind() != SLOPPY_FUNCTION_NAME_VARIABLE || is_strict(language_mode);
178
  }
179

180
  bool is_this() const { return kind() == THIS_VARIABLE; }
181
  bool is_sloppy_function_name() const {
182
    return kind() == SLOPPY_FUNCTION_NAME_VARIABLE;
183
  }
184

185
  bool is_parameter() const { return kind() == PARAMETER_VARIABLE; }
186 187 188
  bool is_sloppy_block_function() {
    return kind() == SLOPPY_BLOCK_FUNCTION_VARIABLE;
  }
189

190
  Variable* local_if_not_shadowed() const {
191 192 193
    DCHECK((mode() == VariableMode::kDynamicLocal ||
            mode() == VariableMode::kDynamic) &&
           has_local_if_not_shadowed());
194 195 196
    return local_if_not_shadowed_;
  }

197 198 199 200
  bool has_local_if_not_shadowed() const {
    return local_if_not_shadowed_ != nullptr;
  }

201 202 203 204
  void set_local_if_not_shadowed(Variable* local) {
    local_if_not_shadowed_ = local;
  }

205 206 207
  VariableLocation location() const {
    return LocationField::decode(bit_field_);
  }
208
  VariableKind kind() const { return VariableKindField::decode(bit_field_); }
209

210 211
  int index() const { return index_; }

212 213 214 215 216 217
  bool IsReceiver() const {
    DCHECK(IsParameter());

    return index_ == -1;
  }

218
  bool IsExport() const {
219 220 221
    DCHECK_EQ(location(), VariableLocation::MODULE);
    DCHECK_NE(index(), 0);
    return index() > 0;
222 223
  }

224
  void AllocateTo(VariableLocation location, int index) {
225 226
    DCHECK(IsUnallocated() ||
           (this->location() == location && this->index() == index));
227
    DCHECK_IMPLIES(location == VariableLocation::MODULE, index != 0);
228 229
    bit_field_ = LocationField::update(bit_field_, location);
    DCHECK_EQ(location, this->location());
230 231
    index_ = index;
  }
232

233
  void MakeParameterNonSimple() {
234 235 236 237 238 239
    DCHECK(is_parameter());
    bit_field_ = VariableModeField::update(bit_field_, VariableMode::kLet);
    bit_field_ =
        InitializationFlagField::update(bit_field_, kNeedsInitialization);
  }

240 241
  static InitializationFlag DefaultInitializationFlag(VariableMode mode) {
    DCHECK(IsDeclaredVariableMode(mode));
242 243
    return mode == VariableMode::kVar ? kCreatedInitialized
                                      : kNeedsInitialization;
244 245
  }

Simon Zünd's avatar
Simon Zünd committed
246 247 248
  // Rewrites the VariableLocation of repl script scope 'lets' to REPL_GLOBAL.
  void RewriteLocationForRepl();

249
  using List = base::ThreadedList<Variable>;
250

251 252
 private:
  Scope* scope_;
253
  const AstRawString* name_;
254

255
  // If this field is set, this variable references the stored locally bound
256 257 258
  // variable, but it might be shadowed by variable bindings introduced by with
  // blocks or sloppy 'eval' calls between the reference scope (inclusive) and
  // the binding scope (exclusive).
259
  Variable* local_if_not_shadowed_;
260
  Variable* next_;
261 262 263 264
  int index_;
  int initializer_position_;
  uint16_t bit_field_;

265 266 267 268
  void set_maybe_assigned() {
    bit_field_ = MaybeAssignedFlagField::update(bit_field_, kMaybeAssigned);
  }

269
  using VariableModeField = base::BitField16<VariableMode, 0, 4>;
270 271
  using VariableKindField = VariableModeField::Next<VariableKind, 3>;
  using LocationField = VariableKindField::Next<VariableLocation, 3>;
272 273
  using ForceContextAllocationBit = LocationField::Next<bool, 1>;
  using IsUsedField = ForceContextAllocationBit::Next<bool, 1>;
274 275
  using InitializationFlagField = IsUsedField::Next<InitializationFlag, 1>;
  using ForceHoleInitializationField = InitializationFlagField::Next<bool, 1>;
276
  using MaybeAssignedFlagField =
277
      ForceHoleInitializationField::Next<MaybeAssignedFlag, 1>;
278
  using IsStaticFlagField = MaybeAssignedFlagField::Next<IsStaticFlag, 1>;
279

280 281
  Variable** next() { return &next_; }
  friend List;
282
  friend base::ThreadedListTraits<Variable>;
283
};
284 285
}  // namespace internal
}  // namespace v8
286

287
#endif  // V8_AST_VARIABLES_H_