blob: 2950db4ae590458454c6cc07eaa6e810cfa24e61 [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// 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
10namespace v8 {
11namespace internal {
12
13// ----------------------------------------------------------------------------
14// Implementation Variable.
15
16const 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 Murdoch4a90d5f2016-03-22 12:00:34 +000022 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
31
32Variable::Variable(Scope* scope, const AstRawString* name, VariableMode mode,
33 Kind kind, InitializationFlag initialization_flag,
34 MaybeAssignedFlag maybe_assigned_flag)
35 : scope_(scope),
36 name_(name),
37 mode_(mode),
38 kind_(kind),
39 location_(VariableLocation::UNALLOCATED),
40 index_(-1),
41 initializer_position_(RelocInfo::kNoPosition),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000042 local_if_not_shadowed_(NULL),
43 is_from_eval_(false),
44 force_context_allocation_(false),
45 is_used_(false),
46 initialization_flag_(initialization_flag),
47 maybe_assigned_(maybe_assigned_flag) {
48 // Var declared variables never need initialization.
49 DCHECK(!(mode == VAR && initialization_flag == kNeedsInitialization));
50}
51
52
53bool Variable::IsGlobalObjectProperty() const {
54 // Temporaries are never global, they must always be allocated in the
55 // activation frame.
56 return (IsDynamicVariableMode(mode_) ||
57 (IsDeclaredVariableMode(mode_) && !IsLexicalVariableMode(mode_))) &&
58 scope_ != NULL && scope_->is_script_scope() && !is_this();
59}
60
61
62bool Variable::IsStaticGlobalObjectProperty() const {
63 // Temporaries are never global, they must always be allocated in the
64 // activation frame.
65 return (IsDeclaredVariableMode(mode_) && !IsLexicalVariableMode(mode_)) &&
66 scope_ != NULL && scope_->is_script_scope() && !is_this();
67}
68
69
70int Variable::CompareIndex(Variable* const* v, Variable* const* w) {
71 int x = (*v)->index();
72 int y = (*w)->index();
73 // Consider sorting them according to type as well?
74 return x - y;
75}
76
77} // namespace internal
78} // namespace v8