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

Generated by: LCOV version 1.10