variables.cc 2.56 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
#include "src/variables.h"
6

7 8
#include "src/ast.h"
#include "src/scopes.h"
9

10 11
namespace v8 {
namespace internal {
12 13 14 15

// ----------------------------------------------------------------------------
// Implementation Variable.

16
const char* Variable::Mode2String(VariableMode mode) {
17 18
  switch (mode) {
    case VAR: return "VAR";
19
    case CONST_LEGACY: return "CONST_LEGACY";
20
    case LET: return "LET";
21
    case CONST: return "CONST";
22
    case IMPORT: return "IMPORT";
23
    case DYNAMIC: return "DYNAMIC";
24 25
    case DYNAMIC_GLOBAL: return "DYNAMIC_GLOBAL";
    case DYNAMIC_LOCAL: return "DYNAMIC_LOCAL";
26 27 28 29 30 31 32
    case TEMPORARY: return "TEMPORARY";
  }
  UNREACHABLE();
  return NULL;
}


33
Variable::Variable(Scope* scope, const AstRawString* name, VariableMode mode,
34
                   Kind kind, InitializationFlag initialization_flag,
35
                   MaybeAssignedFlag maybe_assigned_flag)
36 37 38 39
    : scope_(scope),
      name_(name),
      mode_(mode),
      kind_(kind),
40
      location_(VariableLocation::UNALLOCATED),
41 42
      index_(-1),
      initializer_position_(RelocInfo::kNoPosition),
43 44 45
      has_strong_mode_reference_(false),
      strong_mode_reference_start_position_(RelocInfo::kNoPosition),
      strong_mode_reference_end_position_(RelocInfo::kNoPosition),
46 47 48 49
      local_if_not_shadowed_(NULL),
      force_context_allocation_(false),
      is_used_(false),
      initialization_flag_(initialization_flag),
50
      maybe_assigned_(maybe_assigned_flag) {
51
  // Var declared variables never need initialization.
52
  DCHECK(!(mode == VAR && initialization_flag == kNeedsInitialization));
53 54 55
}


56
bool Variable::IsGlobalObjectProperty() const {
57 58
  // Temporaries are never global, they must always be allocated in the
  // activation frame.
59 60 61
  return (IsDynamicVariableMode(mode_) ||
          (IsDeclaredVariableMode(mode_) && !IsLexicalVariableMode(mode_))) &&
         scope_ != NULL && scope_->is_script_scope() && !is_this();
62 63 64 65 66 67 68
}


bool Variable::IsStaticGlobalObjectProperty() const {
  // Temporaries are never global, they must always be allocated in the
  // activation frame.
  return (IsDeclaredVariableMode(mode_) && !IsLexicalVariableMode(mode_)) &&
69
         scope_ != NULL && scope_->is_script_scope() && !is_this();
70 71
}

72 73 74 75 76 77 78 79

int Variable::CompareIndex(Variable* const* v, Variable* const* w) {
  int x = (*v)->index();
  int y = (*w)->index();
  // Consider sorting them according to type as well?
  return x - y;
}

80 81
}  // namespace internal
}  // namespace v8