blob: 658831239cf79c2c4c753cf6ed820d0657f2abca [file] [log] [blame]
Ben Murdoch8b112d22011-06-08 16:22:53 +01001// Copyright 2011 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005#include "src/v8.h"
Steve Blocka7e24c12009-10-30 11:49:00 +00006
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007#include "src/ast.h"
8#include "src/scopes.h"
9#include "src/variables.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000010
11namespace v8 {
12namespace internal {
13
14// ----------------------------------------------------------------------------
Steve Blocka7e24c12009-10-30 11:49:00 +000015// Implementation Variable.
16
Ben Murdoch3ef787d2012-04-12 10:51:47 +010017const char* Variable::Mode2String(VariableMode mode) {
Steve Blocka7e24c12009-10-30 11:49:00 +000018 switch (mode) {
19 case VAR: return "VAR";
Ben Murdochb8a8cc12014-11-26 15:28:44 +000020 case CONST_LEGACY: return "CONST_LEGACY";
Ben Murdoch69a99ed2011-11-30 16:03:39 +000021 case LET: return "LET";
Ben Murdochb8a8cc12014-11-26 15:28:44 +000022 case CONST: return "CONST";
23 case MODULE: return "MODULE";
Steve Blocka7e24c12009-10-30 11:49:00 +000024 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 Murdochb8a8cc12014-11-26 15:28:44 +000035Variable::Variable(Scope* scope, const AstRawString* name, VariableMode mode,
36 bool is_valid_ref, Kind kind,
Ben Murdoch3ef787d2012-04-12 10:51:47 +010037 InitializationFlag initialization_flag,
Ben Murdochb8a8cc12014-11-26 15:28:44 +000038 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 Murdoch3ef787d2012-04-12 10:51:47 +010053 // Var declared variables never need initialization.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000054 DCHECK(!(mode == VAR && initialization_flag == kNeedsInitialization));
Steve Blocka7e24c12009-10-30 11:49:00 +000055}
56
57
Ben Murdochb8a8cc12014-11-26 15:28:44 +000058bool Variable::IsGlobalObjectProperty() const {
Steve Blocka7e24c12009-10-30 11:49:00 +000059 // Temporaries are never global, they must always be allocated in the
60 // activation frame.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000061 return (IsDynamicVariableMode(mode_) ||
62 (IsDeclaredVariableMode(mode_) && !IsLexicalVariableMode(mode_)))
63 && scope_ != NULL && scope_->is_global_scope();
Steve Blocka7e24c12009-10-30 11:49:00 +000064}
65
Ben Murdoch3ef787d2012-04-12 10:51:47 +010066
67int 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 Blocka7e24c12009-10-30 11:49:00 +000074} } // namespace v8::internal