Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 5 | #include "src/v8.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 7 | #include "src/ast.h" |
| 8 | #include "src/scopes.h" |
| 9 | #include "src/variables.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10 | |
| 11 | namespace v8 { |
| 12 | namespace internal { |
| 13 | |
| 14 | // ---------------------------------------------------------------------------- |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 15 | // Implementation Variable. |
| 16 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 17 | const char* Variable::Mode2String(VariableMode mode) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18 | switch (mode) { |
| 19 | case VAR: return "VAR"; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 20 | case CONST_LEGACY: return "CONST_LEGACY"; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 21 | case LET: return "LET"; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 22 | case CONST: return "CONST"; |
| 23 | case MODULE: return "MODULE"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 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 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 35 | Variable::Variable(Scope* scope, const AstRawString* name, VariableMode mode, |
| 36 | bool is_valid_ref, Kind kind, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 37 | InitializationFlag initialization_flag, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 38 | MaybeAssignedFlag maybe_assigned_flag, Interface* interface) |
| 39 | : scope_(scope), |
| 40 | name_(name), |
| 41 | mode_(mode), |
| 42 | kind_(kind), |
| 43 | location_(UNALLOCATED), |
| 44 | index_(-1), |
| 45 | initializer_position_(RelocInfo::kNoPosition), |
| 46 | local_if_not_shadowed_(NULL), |
| 47 | is_valid_ref_(is_valid_ref), |
| 48 | force_context_allocation_(false), |
| 49 | is_used_(false), |
| 50 | initialization_flag_(initialization_flag), |
| 51 | maybe_assigned_(maybe_assigned_flag), |
| 52 | interface_(interface) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 53 | // Var declared variables never need initialization. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 54 | DCHECK(!(mode == VAR && initialization_flag == kNeedsInitialization)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 58 | bool Variable::IsGlobalObjectProperty() const { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 59 | // Temporaries are never global, they must always be allocated in the |
| 60 | // activation frame. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 61 | return (IsDynamicVariableMode(mode_) || |
| 62 | (IsDeclaredVariableMode(mode_) && !IsLexicalVariableMode(mode_))) |
| 63 | && scope_ != NULL && scope_->is_global_scope(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 66 | |
| 67 | int Variable::CompareIndex(Variable* const* v, Variable* const* w) { |
| 68 | int x = (*v)->index(); |
| 69 | int y = (*w)->index(); |
| 70 | // Consider sorting them according to type as well? |
| 71 | return x - y; |
| 72 | } |
| 73 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 74 | } } // namespace v8::internal |