deps: update v8 to 4.3.61.21
[platform/upstream/nodejs.git] / deps / v8 / src / variables.h
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 #ifndef V8_VARIABLES_H_
6 #define V8_VARIABLES_H_
7
8 #include "src/ast-value-factory.h"
9 #include "src/zone.h"
10
11 namespace v8 {
12 namespace internal {
13
14 // The AST refers to variables via VariableProxies - placeholders for the actual
15 // variables. Variables themselves are never directly referred to from the AST,
16 // they are maintained by scopes, and referred to from VariableProxies and Slots
17 // after binding and variable allocation.
18
19 class Variable: public ZoneObject {
20  public:
21   enum Kind { NORMAL, FUNCTION, THIS, NEW_TARGET, ARGUMENTS };
22
23   enum Location {
24     // Before and during variable allocation, a variable whose location is
25     // not yet determined.  After allocation, a variable looked up as a
26     // property on the global object (and possibly absent).  name() is the
27     // variable name, index() is invalid.
28     UNALLOCATED,
29
30     // A slot in the parameter section on the stack.  index() is the
31     // parameter index, counting left-to-right.  The receiver is index -1;
32     // the first parameter is index 0.
33     PARAMETER,
34
35     // A slot in the local section on the stack.  index() is the variable
36     // index in the stack frame, starting at 0.
37     LOCAL,
38
39     // An indexed slot in a heap context.  index() is the variable index in
40     // the context object on the heap, starting at 0.  scope() is the
41     // corresponding scope.
42     CONTEXT,
43
44     // A named slot in a heap context.  name() is the variable name in the
45     // context object on the heap, with lookup starting at the current
46     // context.  index() is invalid.
47     LOOKUP
48   };
49
50   Variable(Scope* scope, const AstRawString* name, VariableMode mode, Kind kind,
51            InitializationFlag initialization_flag,
52            MaybeAssignedFlag maybe_assigned_flag = kNotAssigned);
53
54   // Printing support
55   static const char* Mode2String(VariableMode mode);
56
57   // The source code for an eval() call may refer to a variable that is
58   // in an outer scope about which we don't know anything (it may not
59   // be the script scope). scope() is NULL in that case. Currently the
60   // scope is only used to follow the context chain length.
61   Scope* scope() const { return scope_; }
62
63   Handle<String> name() const { return name_->string(); }
64   const AstRawString* raw_name() const { return name_; }
65   VariableMode mode() const { return mode_; }
66   bool has_forced_context_allocation() const {
67     return force_context_allocation_;
68   }
69   void ForceContextAllocation() {
70     DCHECK(mode_ != TEMPORARY);
71     force_context_allocation_ = true;
72   }
73   bool is_used() { return is_used_; }
74   void set_is_used() { is_used_ = true; }
75   MaybeAssignedFlag maybe_assigned() const { return maybe_assigned_; }
76   void set_maybe_assigned() { maybe_assigned_ = kMaybeAssigned; }
77
78   int initializer_position() { return initializer_position_; }
79   void set_initializer_position(int pos) { initializer_position_ = pos; }
80
81   bool IsVariable(Handle<String> n) const {
82     return !is_this() && name().is_identical_to(n);
83   }
84
85   bool IsUnallocated() const { return location_ == UNALLOCATED; }
86   bool IsParameter() const { return location_ == PARAMETER; }
87   bool IsStackLocal() const { return location_ == LOCAL; }
88   bool IsStackAllocated() const { return IsParameter() || IsStackLocal(); }
89   bool IsContextSlot() const { return location_ == CONTEXT; }
90   bool IsLookupSlot() const { return location_ == LOOKUP; }
91   bool IsGlobalObjectProperty() const;
92
93   bool is_dynamic() const { return IsDynamicVariableMode(mode_); }
94   bool is_const_mode() const { return IsImmutableVariableMode(mode_); }
95   bool binding_needs_init() const {
96     return initialization_flag_ == kNeedsInitialization;
97   }
98
99   bool is_function() const { return kind_ == FUNCTION; }
100   bool is_this() const { return kind_ == THIS; }
101   bool is_new_target() const { return kind_ == NEW_TARGET; }
102   bool is_arguments() const { return kind_ == ARGUMENTS; }
103
104   // True if the variable is named eval and not known to be shadowed.
105   bool is_possibly_eval(Isolate* isolate) const {
106     return IsVariable(isolate->factory()->eval_string());
107   }
108
109   Variable* local_if_not_shadowed() const {
110     DCHECK(mode_ == DYNAMIC_LOCAL && local_if_not_shadowed_ != NULL);
111     return local_if_not_shadowed_;
112   }
113
114   void set_local_if_not_shadowed(Variable* local) {
115     local_if_not_shadowed_ = local;
116   }
117
118   Location location() const { return location_; }
119   int index() const { return index_; }
120   InitializationFlag initialization_flag() const {
121     return initialization_flag_;
122   }
123
124   void AllocateTo(Location location, int index) {
125     location_ = location;
126     index_ = index;
127   }
128
129   static int CompareIndex(Variable* const* v, Variable* const* w);
130
131   void RecordStrongModeReference(int start_position, int end_position) {
132     // Record the earliest reference to the variable. Used in error messages for
133     // strong mode references to undeclared variables.
134     if (has_strong_mode_reference_ &&
135         strong_mode_reference_start_position_ < start_position)
136       return;
137     has_strong_mode_reference_ = true;
138     strong_mode_reference_start_position_ = start_position;
139     strong_mode_reference_end_position_ = end_position;
140   }
141
142   bool has_strong_mode_reference() const { return has_strong_mode_reference_; }
143   int strong_mode_reference_start_position() const {
144     return strong_mode_reference_start_position_;
145   }
146   int strong_mode_reference_end_position() const {
147     return strong_mode_reference_end_position_;
148   }
149
150  private:
151   Scope* scope_;
152   const AstRawString* name_;
153   VariableMode mode_;
154   Kind kind_;
155   Location location_;
156   int index_;
157   int initializer_position_;
158   // Tracks whether the variable is bound to a VariableProxy which is in strong
159   // mode, and if yes, the source location of the reference.
160   bool has_strong_mode_reference_;
161   int strong_mode_reference_start_position_;
162   int strong_mode_reference_end_position_;
163
164   // If this field is set, this variable references the stored locally bound
165   // variable, but it might be shadowed by variable bindings introduced by
166   // sloppy 'eval' calls between the reference scope (inclusive) and the
167   // binding scope (exclusive).
168   Variable* local_if_not_shadowed_;
169
170   // Usage info.
171   bool force_context_allocation_;  // set by variable resolver
172   bool is_used_;
173   InitializationFlag initialization_flag_;
174   MaybeAssignedFlag maybe_assigned_;
175 };
176
177
178 } }  // namespace v8::internal
179
180 #endif  // V8_VARIABLES_H_