blob: 2095555aa616b71bfed9fea9efcbb5f16a2b2c95 [file] [log] [blame]
Ben Murdoch8b112d22011-06-08 16:22:53 +01001// Copyright 2011 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#ifndef V8_VARIABLES_H_
29#define V8_VARIABLES_H_
30
31#include "zone.h"
32
33namespace v8 {
34namespace internal {
35
Steve Blocka7e24c12009-10-30 11:49:00 +000036// The AST refers to variables via VariableProxies - placeholders for the actual
37// variables. Variables themselves are never directly referred to from the AST,
38// they are maintained by scopes, and referred to from VariableProxies and Slots
39// after binding and variable allocation.
40
41class Variable: public ZoneObject {
42 public:
43 enum Mode {
44 // User declared variables:
45 VAR, // declared via 'var', and 'function' declarations
46
47 CONST, // declared via 'const' declarations
48
Ben Murdoch69a99ed2011-11-30 16:03:39 +000049 LET, // declared via 'let' declarations
50
Steve Blocka7e24c12009-10-30 11:49:00 +000051 // Variables introduced by the compiler:
52 DYNAMIC, // always require dynamic lookup (we don't know
53 // the declaration)
54
55 DYNAMIC_GLOBAL, // requires dynamic lookup, but we know that the
56 // variable is global unless it has been shadowed
57 // by an eval-introduced variable
58
59 DYNAMIC_LOCAL, // requires dynamic lookup, but we know that the
60 // variable is local and where it is unless it
61 // has been shadowed by an eval-introduced
62 // variable
63
64 INTERNAL, // like VAR, but not user-visible (may or may not
65 // be in a context)
66
67 TEMPORARY // temporary variables (not user-visible), never
68 // in a context
69 };
70
71 enum Kind {
72 NORMAL,
73 THIS,
74 ARGUMENTS
75 };
76
77 Variable(Scope* scope,
78 Handle<String> name,
79 Mode mode,
80 bool is_valid_lhs,
81 Kind kind);
82
83 // Printing support
84 static const char* Mode2String(Mode mode);
85
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000086 // Type testing & conversion. Global variables are not slots.
Kristian Monsen0d5e1162010-09-30 15:31:59 +010087 Property* AsProperty() const;
88 Slot* AsSlot() const;
89
Steve Blocka7e24c12009-10-30 11:49:00 +000090 bool IsValidLeftHandSide() { return is_valid_LHS_; }
91
92 // The source code for an eval() call may refer to a variable that is
93 // in an outer scope about which we don't know anything (it may not
94 // be the global scope). scope() is NULL in that case. Currently the
95 // scope is only used to follow the context chain length.
Kristian Monsen0d5e1162010-09-30 15:31:59 +010096 Scope* scope() const { return scope_; }
Steve Blocka7e24c12009-10-30 11:49:00 +000097
Kristian Monsen0d5e1162010-09-30 15:31:59 +010098 Handle<String> name() const { return name_; }
99 Mode mode() const { return mode_; }
100 bool is_accessed_from_inner_scope() const {
Steve Blocka7e24c12009-10-30 11:49:00 +0000101 return is_accessed_from_inner_scope_;
102 }
Ben Murdochb8e0da22011-05-16 14:20:40 +0100103 void MarkAsAccessedFromInnerScope() {
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000104 ASSERT(mode_ != TEMPORARY);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100105 is_accessed_from_inner_scope_ = true;
106 }
Steve Block6ded16b2010-05-10 14:33:55 +0100107 bool is_used() { return is_used_; }
108 void set_is_used(bool flag) { is_used_ = flag; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000109
110 bool IsVariable(Handle<String> n) const {
111 return !is_this() && name().is_identical_to(n);
112 }
113
Steve Block6ded16b2010-05-10 14:33:55 +0100114 bool IsStackAllocated() const;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100115 bool IsParameter() const; // Includes 'this'.
116 bool IsStackLocal() const;
Ben Murdochb8e0da22011-05-16 14:20:40 +0100117 bool IsContextSlot() const;
Steve Block6ded16b2010-05-10 14:33:55 +0100118
Steve Blocka7e24c12009-10-30 11:49:00 +0000119 bool is_dynamic() const {
120 return (mode_ == DYNAMIC ||
121 mode_ == DYNAMIC_GLOBAL ||
122 mode_ == DYNAMIC_LOCAL);
123 }
124
125 bool is_global() const;
126 bool is_this() const { return kind_ == THIS; }
127 bool is_arguments() const { return kind_ == ARGUMENTS; }
128
129 // True if the variable is named eval and not known to be shadowed.
130 bool is_possibly_eval() const {
Steve Block44f0eee2011-05-26 01:26:41 +0100131 return IsVariable(FACTORY->eval_symbol()) &&
Steve Blocka7e24c12009-10-30 11:49:00 +0000132 (mode_ == DYNAMIC || mode_ == DYNAMIC_GLOBAL);
133 }
134
135 Variable* local_if_not_shadowed() const {
136 ASSERT(mode_ == DYNAMIC_LOCAL && local_if_not_shadowed_ != NULL);
137 return local_if_not_shadowed_;
138 }
139
140 void set_local_if_not_shadowed(Variable* local) {
141 local_if_not_shadowed_ = local;
142 }
143
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000144 Slot* rewrite() const { return rewrite_; }
145 void set_rewrite(Slot* slot) { rewrite_ = slot; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000146
Steve Blocka7e24c12009-10-30 11:49:00 +0000147 private:
148 Scope* scope_;
149 Handle<String> name_;
150 Mode mode_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000151 Kind kind_;
152
153 Variable* local_if_not_shadowed_;
154
Steve Blocka7e24c12009-10-30 11:49:00 +0000155 // Code generation.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000156 Slot* rewrite_;
Steve Block1e0659c2011-05-24 12:43:12 +0100157
158 // Valid as a LHS? (const and this are not valid LHS, for example)
159 bool is_valid_LHS_;
160
161 // Usage info.
162 bool is_accessed_from_inner_scope_; // set by variable resolver
163 bool is_used_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000164};
165
166
167} } // namespace v8::internal
168
169#endif // V8_VARIABLES_H_