1 // Copyright 2006-2008 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
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.
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.
28 #ifndef V8_VARIABLES_H_
29 #define V8_VARIABLES_H_
36 // Variables and AST expression nodes can track their "type" to enable
37 // optimizations and removal of redundant checks when generating code.
46 StaticType() : kind_(UNKNOWN) {}
48 bool Is(Kind kind) const { return kind_ == kind; }
50 bool IsKnown() const { return !Is(UNKNOWN); }
51 bool IsUnknown() const { return Is(UNKNOWN); }
52 bool IsLikelySmi() const { return Is(LIKELY_SMI); }
54 void CopyFrom(StaticType* other) {
58 static const char* Type2String(StaticType* type);
60 // LIKELY_SMI accessors
61 void SetAsLikelySmi() {
65 void SetAsLikelySmiIfUnknown() {
74 DISALLOW_COPY_AND_ASSIGN(StaticType);
78 // The AST refers to variables via VariableProxies - placeholders for the actual
79 // variables. Variables themselves are never directly referred to from the AST,
80 // they are maintained by scopes, and referred to from VariableProxies and Slots
81 // after binding and variable allocation.
83 class Variable: public ZoneObject {
86 // User declared variables:
87 VAR, // declared via 'var', and 'function' declarations
89 CONST, // declared via 'const' declarations
91 // Variables introduced by the compiler:
92 DYNAMIC, // always require dynamic lookup (we don't know
95 DYNAMIC_GLOBAL, // requires dynamic lookup, but we know that the
96 // variable is global unless it has been shadowed
97 // by an eval-introduced variable
99 DYNAMIC_LOCAL, // requires dynamic lookup, but we know that the
100 // variable is local and where it is unless it
101 // has been shadowed by an eval-introduced
104 INTERNAL, // like VAR, but not user-visible (may or may not
107 TEMPORARY // temporary variables (not user-visible), never
117 Variable(Scope* scope,
124 static const char* Mode2String(Mode mode);
126 // Type testing & conversion
127 Property* AsProperty();
128 Variable* AsVariable();
129 bool IsValidLeftHandSide() { return is_valid_LHS_; }
131 // The source code for an eval() call may refer to a variable that is
132 // in an outer scope about which we don't know anything (it may not
133 // be the global scope). scope() is NULL in that case. Currently the
134 // scope is only used to follow the context chain length.
135 Scope* scope() const { return scope_; }
137 Handle<String> name() const { return name_; }
138 Mode mode() const { return mode_; }
139 bool is_accessed_from_inner_scope() const {
140 return is_accessed_from_inner_scope_;
142 bool is_used() { return is_used_; }
143 void set_is_used(bool flag) { is_used_ = flag; }
145 bool IsVariable(Handle<String> n) const {
146 return !is_this() && name().is_identical_to(n);
149 bool IsStackAllocated() const;
151 bool is_dynamic() const {
152 return (mode_ == DYNAMIC ||
153 mode_ == DYNAMIC_GLOBAL ||
154 mode_ == DYNAMIC_LOCAL);
157 bool is_global() const;
158 bool is_this() const { return kind_ == THIS; }
159 bool is_arguments() const { return kind_ == ARGUMENTS; }
161 // True if the variable is named eval and not known to be shadowed.
162 bool is_possibly_eval() const {
163 return IsVariable(Factory::eval_symbol()) &&
164 (mode_ == DYNAMIC || mode_ == DYNAMIC_GLOBAL);
167 Variable* local_if_not_shadowed() const {
168 ASSERT(mode_ == DYNAMIC_LOCAL && local_if_not_shadowed_ != NULL);
169 return local_if_not_shadowed_;
172 void set_local_if_not_shadowed(Variable* local) {
173 local_if_not_shadowed_ = local;
176 Expression* rewrite() const { return rewrite_; }
179 StaticType* type() { return &type_; }
183 Handle<String> name_;
188 Variable* local_if_not_shadowed_;
191 bool is_accessed_from_inner_scope_; // set by variable resolver
194 // Static type information
198 // rewrite_ is usually a Slot or a Property, but may be any expression.
199 Expression* rewrite_;
201 friend class Scope; // Has explicit access to rewrite_.
205 } } // namespace v8::internal
207 #endif // V8_VARIABLES_H_