Home | History | Annotate | Download | only in src
      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 #include "src/v8.h"
      6 
      7 #include "src/ast.h"
      8 #include "src/scopes.h"
      9 #include "src/variables.h"
     10 
     11 namespace v8 {
     12 namespace internal {
     13 
     14 // ----------------------------------------------------------------------------
     15 // Implementation Variable.
     16 
     17 const char* Variable::Mode2String(VariableMode mode) {
     18   switch (mode) {
     19     case VAR: return "VAR";
     20     case CONST_LEGACY: return "CONST_LEGACY";
     21     case LET: return "LET";
     22     case CONST: return "CONST";
     23     case MODULE: return "MODULE";
     24     case DYNAMIC: return "DYNAMIC";
     25     case DYNAMIC_GLOBAL: return "DYNAMIC_GLOBAL";
     26     case DYNAMIC_LOCAL: return "DYNAMIC_LOCAL";
     27     case INTERNAL: return "INTERNAL";
     28     case TEMPORARY: return "TEMPORARY";
     29   }
     30   UNREACHABLE();
     31   return NULL;
     32 }
     33 
     34 
     35 Variable::Variable(Scope* scope,
     36                    Handle<String> name,
     37                    VariableMode mode,
     38                    bool is_valid_ref,
     39                    Kind kind,
     40                    InitializationFlag initialization_flag,
     41                    Interface* interface)
     42   : scope_(scope),
     43     name_(name),
     44     mode_(mode),
     45     kind_(kind),
     46     location_(UNALLOCATED),
     47     index_(-1),
     48     initializer_position_(RelocInfo::kNoPosition),
     49     local_if_not_shadowed_(NULL),
     50     is_valid_ref_(is_valid_ref),
     51     force_context_allocation_(false),
     52     is_used_(false),
     53     initialization_flag_(initialization_flag),
     54     interface_(interface) {
     55   // Names must be canonicalized for fast equality checks.
     56   ASSERT(name->IsInternalizedString());
     57   // Var declared variables never need initialization.
     58   ASSERT(!(mode == VAR && initialization_flag == kNeedsInitialization));
     59 }
     60 
     61 
     62 bool Variable::IsGlobalObjectProperty() const {
     63   // Temporaries are never global, they must always be allocated in the
     64   // activation frame.
     65   return (IsDynamicVariableMode(mode_) ||
     66           (IsDeclaredVariableMode(mode_) && !IsLexicalVariableMode(mode_)))
     67       && scope_ != NULL && scope_->is_global_scope();
     68 }
     69 
     70 
     71 int Variable::CompareIndex(Variable* const* v, Variable* const* w) {
     72   int x = (*v)->index();
     73   int y = (*w)->index();
     74   // Consider sorting them according to type as well?
     75   return x - y;
     76 }
     77 
     78 } }  // namespace v8::internal
     79