dcd2e6af6e02d5d90d9221580bcd38bd867e3736
[platform/upstream/v8.git] / 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 ClassVariable;
20
21 class Variable: public ZoneObject {
22  public:
23   enum Kind { NORMAL, FUNCTION, CLASS, THIS, ARGUMENTS };
24
25   Variable(Scope* scope, const AstRawString* name, VariableMode mode, Kind kind,
26            InitializationFlag initialization_flag,
27            MaybeAssignedFlag maybe_assigned_flag = kNotAssigned);
28
29   virtual ~Variable() {}
30
31   // Printing support
32   static const char* Mode2String(VariableMode mode);
33
34   // The source code for an eval() call may refer to a variable that is
35   // in an outer scope about which we don't know anything (it may not
36   // be the script scope). scope() is NULL in that case. Currently the
37   // scope is only used to follow the context chain length.
38   Scope* scope() const { return scope_; }
39
40   Handle<String> name() const { return name_->string(); }
41   const AstRawString* raw_name() const { return name_; }
42   VariableMode mode() const { return mode_; }
43   bool has_forced_context_allocation() const {
44     return force_context_allocation_;
45   }
46   void ForceContextAllocation() {
47     force_context_allocation_ = true;
48   }
49   bool is_used() { return is_used_; }
50   void set_is_used() { is_used_ = true; }
51   MaybeAssignedFlag maybe_assigned() const { return maybe_assigned_; }
52   void set_maybe_assigned() { maybe_assigned_ = kMaybeAssigned; }
53
54   int initializer_position() { return initializer_position_; }
55   void set_initializer_position(int pos) { initializer_position_ = pos; }
56
57   bool IsVariable(Handle<String> n) const {
58     return !is_this() && name().is_identical_to(n);
59   }
60
61   bool IsUnallocated() const {
62     return location_ == VariableLocation::UNALLOCATED;
63   }
64   bool IsParameter() const { return location_ == VariableLocation::PARAMETER; }
65   bool IsStackLocal() const { return location_ == VariableLocation::LOCAL; }
66   bool IsStackAllocated() const { return IsParameter() || IsStackLocal(); }
67   bool IsContextSlot() const { return location_ == VariableLocation::CONTEXT; }
68   bool IsGlobalSlot() const { return location_ == VariableLocation::GLOBAL; }
69   bool IsUnallocatedOrGlobalSlot() const {
70     return IsUnallocated() || IsGlobalSlot();
71   }
72   bool IsLookupSlot() const { return location_ == VariableLocation::LOOKUP; }
73   bool IsGlobalObjectProperty() const;
74   bool IsStaticGlobalObjectProperty() const;
75
76   bool is_dynamic() const { return IsDynamicVariableMode(mode_); }
77   bool is_const_mode() const { return IsImmutableVariableMode(mode_); }
78   bool binding_needs_init() const {
79     return initialization_flag_ == kNeedsInitialization;
80   }
81
82   bool is_function() const { return kind_ == FUNCTION; }
83   bool is_class() const { return kind_ == CLASS; }
84   bool is_this() const { return kind_ == THIS; }
85   bool is_arguments() const { return kind_ == ARGUMENTS; }
86
87   // For script scopes, the "this" binding is provided by a ScriptContext added
88   // to the global's ScriptContextTable.  This binding might not statically
89   // resolve to a Variable::THIS binding, instead being DYNAMIC_LOCAL.  However
90   // any variable named "this" does indeed refer to a Variable::THIS binding;
91   // the grammar ensures this to be the case.  So wherever a "this" binding
92   // might be provided by the global, use HasThisName instead of is_this().
93   bool HasThisName(Isolate* isolate) const {
94     return is_this() || *name() == *isolate->factory()->this_string();
95   }
96
97   ClassVariable* AsClassVariable() {
98     DCHECK(is_class());
99     return reinterpret_cast<ClassVariable*>(this);
100   }
101
102   // True if the variable is named eval and not known to be shadowed.
103   bool is_possibly_eval(Isolate* isolate) const {
104     return IsVariable(isolate->factory()->eval_string());
105   }
106
107   Variable* local_if_not_shadowed() const {
108     DCHECK(mode_ == DYNAMIC_LOCAL && local_if_not_shadowed_ != NULL);
109     return local_if_not_shadowed_;
110   }
111
112   void set_local_if_not_shadowed(Variable* local) {
113     local_if_not_shadowed_ = local;
114   }
115
116   VariableLocation location() const { return location_; }
117   int index() const { return index_; }
118   InitializationFlag initialization_flag() const {
119     return initialization_flag_;
120   }
121
122   void AllocateTo(VariableLocation location, int index) {
123     location_ = location;
124     index_ = index;
125   }
126
127   static int CompareIndex(Variable* const* v, Variable* const* w);
128
129   void RecordStrongModeReference(int start_position, int end_position) {
130     // Record the earliest reference to the variable. Used in error messages for
131     // strong mode references to undeclared variables.
132     if (has_strong_mode_reference_ &&
133         strong_mode_reference_start_position_ < start_position)
134       return;
135     has_strong_mode_reference_ = true;
136     strong_mode_reference_start_position_ = start_position;
137     strong_mode_reference_end_position_ = end_position;
138   }
139
140   bool has_strong_mode_reference() const { return has_strong_mode_reference_; }
141   int strong_mode_reference_start_position() const {
142     return strong_mode_reference_start_position_;
143   }
144   int strong_mode_reference_end_position() const {
145     return strong_mode_reference_end_position_;
146   }
147
148  private:
149   Scope* scope_;
150   const AstRawString* name_;
151   VariableMode mode_;
152   Kind kind_;
153   VariableLocation location_;
154   int index_;
155   int initializer_position_;
156   // Tracks whether the variable is bound to a VariableProxy which is in strong
157   // mode, and if yes, the source location of the reference.
158   bool has_strong_mode_reference_;
159   int strong_mode_reference_start_position_;
160   int strong_mode_reference_end_position_;
161
162   // If this field is set, this variable references the stored locally bound
163   // variable, but it might be shadowed by variable bindings introduced by
164   // sloppy 'eval' calls between the reference scope (inclusive) and the
165   // binding scope (exclusive).
166   Variable* local_if_not_shadowed_;
167
168   // Usage info.
169   bool force_context_allocation_;  // set by variable resolver
170   bool is_used_;
171   InitializationFlag initialization_flag_;
172   MaybeAssignedFlag maybe_assigned_;
173 };
174
175 class ClassVariable : public Variable {
176  public:
177   ClassVariable(Scope* scope, const AstRawString* name, VariableMode mode,
178                 InitializationFlag initialization_flag,
179                 MaybeAssignedFlag maybe_assigned_flag = kNotAssigned,
180                 int declaration_group_start = -1)
181       : Variable(scope, name, mode, Variable::CLASS, initialization_flag,
182                  maybe_assigned_flag),
183         declaration_group_start_(declaration_group_start) {}
184
185   int declaration_group_start() const { return declaration_group_start_; }
186   void set_declaration_group_start(int declaration_group_start) {
187     declaration_group_start_ = declaration_group_start;
188   }
189
190  private:
191   // For classes we keep track of consecutive groups of delcarations. They are
192   // needed for strong mode scoping checks. TODO(marja, rossberg): Implement
193   // checks for functions too.
194   int declaration_group_start_;
195 };
196 } }  // namespace v8::internal
197
198 #endif  // V8_VARIABLES_H_