LCOV - code coverage report
Current view: top level - src/ast - variables.h (source / functions) Hit Total Coverage
Test: app.info Lines: 27 27 100.0 %
Date: 2019-01-20 Functions: 4 4 100.0 %

          Line data    Source code
       1             : // Copyright 2011 the V8 project authors. All rights reserved.
       2             : // Use of this source code is governed by a BSD-style license that can be
       3             : // found in the LICENSE file.
       4             : 
       5             : #ifndef V8_AST_VARIABLES_H_
       6             : #define V8_AST_VARIABLES_H_
       7             : 
       8             : #include "src/ast/ast-value-factory.h"
       9             : #include "src/globals.h"
      10             : #include "src/zone/zone.h"
      11             : 
      12             : namespace v8 {
      13             : namespace internal {
      14             : 
      15             : // The AST refers to variables via VariableProxies - placeholders for the actual
      16             : // variables. Variables themselves are never directly referred to from the AST,
      17             : // they are maintained by scopes, and referred to from VariableProxies and Slots
      18             : // after binding and variable allocation.
      19             : class Variable final : public ZoneObject {
      20             :  public:
      21             :   Variable(Scope* scope, const AstRawString* name, VariableMode mode,
      22             :            VariableKind kind, InitializationFlag initialization_flag,
      23             :            MaybeAssignedFlag maybe_assigned_flag = kNotAssigned)
      24             :       : scope_(scope),
      25             :         name_(name),
      26             :         local_if_not_shadowed_(nullptr),
      27             :         next_(nullptr),
      28             :         index_(-1),
      29             :         initializer_position_(kNoSourcePosition),
      30             :         bit_field_(MaybeAssignedFlagField::encode(maybe_assigned_flag) |
      31             :                    InitializationFlagField::encode(initialization_flag) |
      32             :                    VariableModeField::encode(mode) |
      33             :                    IsUsedField::encode(false) |
      34             :                    ForceContextAllocationField::encode(false) |
      35             :                    ForceHoleInitializationField::encode(false) |
      36             :                    LocationField::encode(VariableLocation::UNALLOCATED) |
      37   174439527 :                    VariableKindField::encode(kind)) {
      38             :     // Var declared variables never need initialization.
      39             :     DCHECK(!(mode == VariableMode::kVar &&
      40             :              initialization_flag == kNeedsInitialization));
      41             :   }
      42             : 
      43             :   explicit Variable(Variable* other);
      44             : 
      45             :   // The source code for an eval() call may refer to a variable that is
      46             :   // in an outer scope about which we don't know anything (it may not
      47             :   // be the script scope). scope() is nullptr in that case. Currently the
      48             :   // scope is only used to follow the context chain length.
      49             :   Scope* scope() const { return scope_; }
      50             : 
      51             :   // This is for adjusting the scope of temporaries used when desugaring
      52             :   // parameter initializers.
      53          12 :   void set_scope(Scope* scope) { scope_ = scope; }
      54             : 
      55    12672266 :   Handle<String> name() const { return name_->string(); }
      56        1120 :   const AstRawString* raw_name() const { return name_; }
      57             :   VariableMode mode() const { return VariableModeField::decode(bit_field_); }
      58             :   bool has_forced_context_allocation() const {
      59             :     return ForceContextAllocationField::decode(bit_field_);
      60             :   }
      61             :   void ForceContextAllocation() {
      62             :     DCHECK(IsUnallocated() || IsContextSlot() ||
      63             :            location() == VariableLocation::MODULE);
      64    13337389 :     bit_field_ = ForceContextAllocationField::update(bit_field_, true);
      65             :   }
      66             :   bool is_used() { return IsUsedField::decode(bit_field_); }
      67   138050456 :   void set_is_used() { bit_field_ = IsUsedField::update(bit_field_, true); }
      68             :   MaybeAssignedFlag maybe_assigned() const {
      69             :     return MaybeAssignedFlagField::decode(bit_field_);
      70             :   }
      71             :   void set_maybe_assigned() {
      72    16663480 :     bit_field_ = MaybeAssignedFlagField::update(bit_field_, kMaybeAssigned);
      73             :   }
      74             : 
      75             :   int initializer_position() { return initializer_position_; }
      76    12509534 :   void set_initializer_position(int pos) { initializer_position_ = pos; }
      77             : 
      78    24064177 :   bool IsUnallocated() const {
      79             :     return location() == VariableLocation::UNALLOCATED;
      80             :   }
      81             :   bool IsParameter() const { return location() == VariableLocation::PARAMETER; }
      82          35 :   bool IsStackLocal() const { return location() == VariableLocation::LOCAL; }
      83     2984479 :   bool IsStackAllocated() const { return IsParameter() || IsStackLocal(); }
      84      212024 :   bool IsContextSlot() const { return location() == VariableLocation::CONTEXT; }
      85             :   bool IsLookupSlot() const { return location() == VariableLocation::LOOKUP; }
      86             :   bool IsGlobalObjectProperty() const;
      87             : 
      88    15733166 :   bool is_dynamic() const { return IsDynamicVariableMode(mode()); }
      89             : 
      90             :   // Returns the InitializationFlag this Variable was created with.
      91             :   // Scope analysis may allow us to relax this initialization
      92             :   // requirement, which will be reflected in the return value of
      93             :   // binding_needs_init().
      94             :   InitializationFlag initialization_flag() const {
      95             :     return InitializationFlagField::decode(bit_field_);
      96             :   }
      97             : 
      98             :   // Whether this variable needs to be initialized with the hole at
      99             :   // declaration time. Only returns valid results after scope analysis.
     100     3028486 :   bool binding_needs_init() const {
     101             :     DCHECK_IMPLIES(initialization_flag() == kNeedsInitialization,
     102             :                    IsLexicalVariableMode(mode()));
     103             :     DCHECK_IMPLIES(ForceHoleInitializationField::decode(bit_field_),
     104             :                    initialization_flag() == kNeedsInitialization);
     105             : 
     106             :     // Always initialize if hole initialization was forced during
     107             :     // scope analysis.
     108     6056972 :     if (ForceHoleInitializationField::decode(bit_field_)) return true;
     109             : 
     110             :     // If initialization was not forced, no need for initialization
     111             :     // for stack allocated variables, since UpdateNeedsHoleCheck()
     112             :     // in scopes.cc has proven that no VariableProxy refers to
     113             :     // this variable in such a way that a runtime hole check
     114             :     // would be generated.
     115     2984479 :     if (IsStackAllocated()) return false;
     116             : 
     117             :     // Otherwise, defer to the flag set when this Variable was constructed.
     118     1460272 :     return initialization_flag() == kNeedsInitialization;
     119             :   }
     120             : 
     121             :   // Called during scope analysis when a VariableProxy is found to
     122             :   // reference this Variable in such a way that a hole check will
     123             :   // be required at runtime.
     124             :   void ForceHoleInitialization() {
     125             :     DCHECK_EQ(kNeedsInitialization, initialization_flag());
     126             :     DCHECK(IsLexicalVariableMode(mode()));
     127     5469612 :     bit_field_ = ForceHoleInitializationField::update(bit_field_, true);
     128             :   }
     129             : 
     130       32943 :   bool throw_on_const_assignment(LanguageMode language_mode) const {
     131       32943 :     return kind() != SLOPPY_FUNCTION_NAME_VARIABLE || is_strict(language_mode);
     132             :   }
     133             : 
     134    32926920 :   bool is_this() const { return kind() == THIS_VARIABLE; }
     135             :   bool is_sloppy_function_name() const {
     136             :     return kind() == SLOPPY_FUNCTION_NAME_VARIABLE;
     137             :   }
     138             : 
     139        2396 :   bool is_parameter() const { return kind() == PARAMETER_VARIABLE; }
     140             : 
     141             :   Variable* local_if_not_shadowed() const {
     142             :     DCHECK(mode() == VariableMode::kDynamicLocal &&
     143             :            local_if_not_shadowed_ != nullptr);
     144             :     return local_if_not_shadowed_;
     145             :   }
     146             : 
     147             :   void set_local_if_not_shadowed(Variable* local) {
     148        4618 :     local_if_not_shadowed_ = local;
     149             :   }
     150             : 
     151             :   VariableLocation location() const {
     152             :     return LocationField::decode(bit_field_);
     153             :   }
     154             :   VariableKind kind() const { return VariableKindField::decode(bit_field_); }
     155             : 
     156             :   int index() const { return index_; }
     157             : 
     158             :   bool IsReceiver() const {
     159             :     DCHECK(IsParameter());
     160             : 
     161             :     return index_ == -1;
     162             :   }
     163             : 
     164       19733 :   bool IsExport() const {
     165             :     DCHECK_EQ(location(), VariableLocation::MODULE);
     166             :     DCHECK_NE(index(), 0);
     167             :     return index() > 0;
     168             :   }
     169             : 
     170             :   void AllocateTo(VariableLocation location, int index) {
     171             :     DCHECK(IsUnallocated() ||
     172             :            (this->location() == location && this->index() == index));
     173             :     DCHECK_IMPLIES(location == VariableLocation::MODULE, index != 0);
     174    23440983 :     bit_field_ = LocationField::update(bit_field_, location);
     175             :     DCHECK_EQ(location, this->location());
     176    12970455 :     index_ = index;
     177             :   }
     178             : 
     179             :   static InitializationFlag DefaultInitializationFlag(VariableMode mode) {
     180             :     DCHECK(IsDeclaredVariableMode(mode));
     181             :     return mode == VariableMode::kVar ? kCreatedInitialized
     182     9372144 :                                       : kNeedsInitialization;
     183             :   }
     184             : 
     185             :   typedef base::ThreadedList<Variable> List;
     186             : 
     187             :  private:
     188             :   Scope* scope_;
     189             :   const AstRawString* name_;
     190             : 
     191             :   // If this field is set, this variable references the stored locally bound
     192             :   // variable, but it might be shadowed by variable bindings introduced by
     193             :   // sloppy 'eval' calls between the reference scope (inclusive) and the
     194             :   // binding scope (exclusive).
     195             :   Variable* local_if_not_shadowed_;
     196             :   Variable* next_;
     197             :   int index_;
     198             :   int initializer_position_;
     199             :   uint16_t bit_field_;
     200             : 
     201             :   class VariableModeField : public BitField16<VariableMode, 0, 3> {};
     202             :   class VariableKindField
     203             :       : public BitField16<VariableKind, VariableModeField::kNext, 2> {};
     204             :   class LocationField
     205             :       : public BitField16<VariableLocation, VariableKindField::kNext, 3> {};
     206             :   class ForceContextAllocationField
     207             :       : public BitField16<bool, LocationField::kNext, 1> {};
     208             :   class IsUsedField
     209             :       : public BitField16<bool, ForceContextAllocationField::kNext, 1> {};
     210             :   class InitializationFlagField
     211             :       : public BitField16<InitializationFlag, IsUsedField::kNext, 1> {};
     212             :   class ForceHoleInitializationField
     213             :       : public BitField16<bool, InitializationFlagField::kNext, 1> {};
     214             :   class MaybeAssignedFlagField
     215             :       : public BitField16<MaybeAssignedFlag,
     216             :                           ForceHoleInitializationField::kNext, 1> {};
     217             :   Variable** next() { return &next_; }
     218             :   friend List;
     219             :   friend base::ThreadedListTraits<Variable>;
     220             : };
     221             : }  // namespace internal
     222             : }  // namespace v8
     223             : 
     224             : #endif  // V8_AST_VARIABLES_H_

Generated by: LCOV version 1.10