| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7 | #include "src/ast/scopes.h" |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame] | 8 | #include "src/globals.h" |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9 | |
| 10 | namespace v8 { |
| 11 | namespace internal { |
| 12 | |
| 13 | // ---------------------------------------------------------------------------- |
| 14 | // Implementation Variable. |
| 15 | |
| 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"; |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 22 | case DYNAMIC: return "DYNAMIC"; |
| 23 | case DYNAMIC_GLOBAL: return "DYNAMIC_GLOBAL"; |
| 24 | case DYNAMIC_LOCAL: return "DYNAMIC_LOCAL"; |
| 25 | case TEMPORARY: return "TEMPORARY"; |
| 26 | } |
| 27 | UNREACHABLE(); |
| 28 | return NULL; |
| 29 | } |
| 30 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 31 | Variable::Variable(Scope* scope, const AstRawString* name, VariableMode mode, |
| 32 | Kind kind, InitializationFlag initialization_flag, |
| 33 | MaybeAssignedFlag maybe_assigned_flag) |
| 34 | : scope_(scope), |
| 35 | name_(name), |
| 36 | mode_(mode), |
| 37 | kind_(kind), |
| 38 | location_(VariableLocation::UNALLOCATED), |
| 39 | index_(-1), |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame] | 40 | initializer_position_(kNoSourcePosition), |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 41 | local_if_not_shadowed_(NULL), |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 42 | force_context_allocation_(false), |
| 43 | is_used_(false), |
| 44 | initialization_flag_(initialization_flag), |
| 45 | maybe_assigned_(maybe_assigned_flag) { |
| 46 | // Var declared variables never need initialization. |
| 47 | DCHECK(!(mode == VAR && initialization_flag == kNeedsInitialization)); |
| 48 | } |
| 49 | |
| 50 | |
| 51 | bool Variable::IsGlobalObjectProperty() const { |
| 52 | // Temporaries are never global, they must always be allocated in the |
| 53 | // activation frame. |
| 54 | return (IsDynamicVariableMode(mode_) || |
| 55 | (IsDeclaredVariableMode(mode_) && !IsLexicalVariableMode(mode_))) && |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame] | 56 | scope_ != NULL && scope_->is_script_scope(); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | |
| 60 | bool Variable::IsStaticGlobalObjectProperty() const { |
| 61 | // Temporaries are never global, they must always be allocated in the |
| 62 | // activation frame. |
| 63 | return (IsDeclaredVariableMode(mode_) && !IsLexicalVariableMode(mode_)) && |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame] | 64 | scope_ != NULL && scope_->is_script_scope(); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | |
| 68 | int Variable::CompareIndex(Variable* const* v, Variable* const* w) { |
| 69 | int x = (*v)->index(); |
| 70 | int y = (*w)->index(); |
| 71 | // Consider sorting them according to type as well? |
| 72 | return x - y; |
| 73 | } |
| 74 | |
| 75 | } // namespace internal |
| 76 | } // namespace v8 |