• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/zone.h"
9 #include "src/interface.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 {
22     NORMAL,
23     THIS,
24     ARGUMENTS
25   };
26 
27   enum Location {
28     // Before and during variable allocation, a variable whose location is
29     // not yet determined.  After allocation, a variable looked up as a
30     // property on the global object (and possibly absent).  name() is the
31     // variable name, index() is invalid.
32     UNALLOCATED,
33 
34     // A slot in the parameter section on the stack.  index() is the
35     // parameter index, counting left-to-right.  The receiver is index -1;
36     // the first parameter is index 0.
37     PARAMETER,
38 
39     // A slot in the local section on the stack.  index() is the variable
40     // index in the stack frame, starting at 0.
41     LOCAL,
42 
43     // An indexed slot in a heap context.  index() is the variable index in
44     // the context object on the heap, starting at 0.  scope() is the
45     // corresponding scope.
46     CONTEXT,
47 
48     // A named slot in a heap context.  name() is the variable name in the
49     // context object on the heap, with lookup starting at the current
50     // context.  index() is invalid.
51     LOOKUP
52   };
53 
54   Variable(Scope* scope,
55            Handle<String> name,
56            VariableMode mode,
57            bool is_valid_ref,
58            Kind kind,
59            InitializationFlag initialization_flag,
60            Interface* interface = Interface::NewValue());
61 
62   // Printing support
63   static const char* Mode2String(VariableMode mode);
64 
IsValidReference()65   bool IsValidReference() { return is_valid_ref_; }
66 
67   // The source code for an eval() call may refer to a variable that is
68   // in an outer scope about which we don't know anything (it may not
69   // be the global scope). scope() is NULL in that case. Currently the
70   // scope is only used to follow the context chain length.
scope()71   Scope* scope() const { return scope_; }
72 
name()73   Handle<String> name() const { return name_; }
mode()74   VariableMode mode() const { return mode_; }
has_forced_context_allocation()75   bool has_forced_context_allocation() const {
76     return force_context_allocation_;
77   }
ForceContextAllocation()78   void ForceContextAllocation() {
79     ASSERT(mode_ != TEMPORARY);
80     force_context_allocation_ = true;
81   }
is_used()82   bool is_used() { return is_used_; }
set_is_used(bool flag)83   void set_is_used(bool flag) { is_used_ = flag; }
84 
initializer_position()85   int initializer_position() { return initializer_position_; }
set_initializer_position(int pos)86   void set_initializer_position(int pos) { initializer_position_ = pos; }
87 
IsVariable(Handle<String> n)88   bool IsVariable(Handle<String> n) const {
89     return !is_this() && name().is_identical_to(n);
90   }
91 
IsUnallocated()92   bool IsUnallocated() const { return location_ == UNALLOCATED; }
IsParameter()93   bool IsParameter() const { return location_ == PARAMETER; }
IsStackLocal()94   bool IsStackLocal() const { return location_ == LOCAL; }
IsStackAllocated()95   bool IsStackAllocated() const { return IsParameter() || IsStackLocal(); }
IsContextSlot()96   bool IsContextSlot() const { return location_ == CONTEXT; }
IsLookupSlot()97   bool IsLookupSlot() const { return location_ == LOOKUP; }
98   bool IsGlobalObjectProperty() const;
99 
is_dynamic()100   bool is_dynamic() const { return IsDynamicVariableMode(mode_); }
is_const_mode()101   bool is_const_mode() const { return IsImmutableVariableMode(mode_); }
binding_needs_init()102   bool binding_needs_init() const {
103     return initialization_flag_ == kNeedsInitialization;
104   }
105 
is_this()106   bool is_this() const { return kind_ == THIS; }
is_arguments()107   bool is_arguments() const { return kind_ == ARGUMENTS; }
108 
109   // True if the variable is named eval and not known to be shadowed.
is_possibly_eval(Isolate * isolate)110   bool is_possibly_eval(Isolate* isolate) const {
111     return IsVariable(isolate->factory()->eval_string());
112   }
113 
local_if_not_shadowed()114   Variable* local_if_not_shadowed() const {
115     ASSERT(mode_ == DYNAMIC_LOCAL && local_if_not_shadowed_ != NULL);
116     return local_if_not_shadowed_;
117   }
118 
set_local_if_not_shadowed(Variable * local)119   void set_local_if_not_shadowed(Variable* local) {
120     local_if_not_shadowed_ = local;
121   }
122 
location()123   Location location() const { return location_; }
index()124   int index() const { return index_; }
initialization_flag()125   InitializationFlag initialization_flag() const {
126     return initialization_flag_;
127   }
interface()128   Interface* interface() const { return interface_; }
129 
AllocateTo(Location location,int index)130   void AllocateTo(Location location, int index) {
131     location_ = location;
132     index_ = index;
133   }
134 
135   static int CompareIndex(Variable* const* v, Variable* const* w);
136 
137  private:
138   Scope* scope_;
139   Handle<String> name_;
140   VariableMode mode_;
141   Kind kind_;
142   Location location_;
143   int index_;
144   int initializer_position_;
145 
146   // If this field is set, this variable references the stored locally bound
147   // variable, but it might be shadowed by variable bindings introduced by
148   // sloppy 'eval' calls between the reference scope (inclusive) and the
149   // binding scope (exclusive).
150   Variable* local_if_not_shadowed_;
151 
152   // Valid as a reference? (const and this are not valid, for example)
153   bool is_valid_ref_;
154 
155   // Usage info.
156   bool force_context_allocation_;  // set by variable resolver
157   bool is_used_;
158   InitializationFlag initialization_flag_;
159 
160   // Module type info.
161   Interface* interface_;
162 };
163 
164 
165 } }  // namespace v8::internal
166 
167 #endif  // V8_VARIABLES_H_
168