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/ast/variables.h"
6
7 #include "src/ast/ast.h"
8 #include "src/ast/scopes.h"
9
10 namespace v8 {
11 namespace internal {
12
13 // ----------------------------------------------------------------------------
14 // Implementation Variable.
15
Mode2String(VariableMode mode)16 const char* Variable::Mode2String(VariableMode mode) {
17 switch (mode) {
18 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 case DYNAMIC_GLOBAL: return "DYNAMIC_GLOBAL";
25 case DYNAMIC_LOCAL: return "DYNAMIC_LOCAL";
26 case TEMPORARY: return "TEMPORARY";
27 }
28 UNREACHABLE();
29 return NULL;
30 }
31
32
Variable(Scope * scope,const AstRawString * name,VariableMode mode,Kind kind,InitializationFlag initialization_flag,MaybeAssignedFlag maybe_assigned_flag)33 Variable::Variable(Scope* scope, const AstRawString* name, VariableMode mode,
34 Kind kind, InitializationFlag initialization_flag,
35 MaybeAssignedFlag maybe_assigned_flag)
36 : scope_(scope),
37 name_(name),
38 mode_(mode),
39 kind_(kind),
40 location_(VariableLocation::UNALLOCATED),
41 index_(-1),
42 initializer_position_(RelocInfo::kNoPosition),
43 has_strong_mode_reference_(false),
44 strong_mode_reference_start_position_(RelocInfo::kNoPosition),
45 strong_mode_reference_end_position_(RelocInfo::kNoPosition),
46 local_if_not_shadowed_(NULL),
47 is_from_eval_(false),
48 force_context_allocation_(false),
49 is_used_(false),
50 initialization_flag_(initialization_flag),
51 maybe_assigned_(maybe_assigned_flag) {
52 // Var declared variables never need initialization.
53 DCHECK(!(mode == VAR && initialization_flag == kNeedsInitialization));
54 }
55
56
IsGlobalObjectProperty() const57 bool Variable::IsGlobalObjectProperty() const {
58 // Temporaries are never global, they must always be allocated in the
59 // activation frame.
60 return (IsDynamicVariableMode(mode_) ||
61 (IsDeclaredVariableMode(mode_) && !IsLexicalVariableMode(mode_))) &&
62 scope_ != NULL && scope_->is_script_scope() && !is_this();
63 }
64
65
IsStaticGlobalObjectProperty() const66 bool Variable::IsStaticGlobalObjectProperty() const {
67 // Temporaries are never global, they must always be allocated in the
68 // activation frame.
69 return (IsDeclaredVariableMode(mode_) && !IsLexicalVariableMode(mode_)) &&
70 scope_ != NULL && scope_->is_script_scope() && !is_this();
71 }
72
73
CompareIndex(Variable * const * v,Variable * const * w)74 int Variable::CompareIndex(Variable* const* v, Variable* const* w) {
75 int x = (*v)->index();
76 int y = (*w)->index();
77 // Consider sorting them according to type as well?
78 return x - y;
79 }
80
81 } // namespace internal
82 } // namespace v8
83