| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 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 |
| 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 | |
| 33 | namespace v8 { |
| 34 | namespace internal { |
| 35 | |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 36 | // Variables and AST expression nodes can track their "type" to enable |
| 37 | // optimizations and removal of redundant checks when generating code. |
| 38 | |
| Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 39 | class StaticType { |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 40 | public: |
| 41 | enum Kind { |
| 42 | UNKNOWN, |
| 43 | LIKELY_SMI |
| 44 | }; |
| 45 | |
| Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 46 | StaticType() : kind_(UNKNOWN) {} |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 47 | |
| 48 | bool Is(Kind kind) const { return kind_ == kind; } |
| 49 | |
| 50 | bool IsKnown() const { return !Is(UNKNOWN); } |
| 51 | bool IsUnknown() const { return Is(UNKNOWN); } |
| 52 | bool IsLikelySmi() const { return Is(LIKELY_SMI); } |
| 53 | |
| Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 54 | void CopyFrom(StaticType* other) { |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 55 | kind_ = other->kind_; |
| 56 | } |
| 57 | |
| Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 58 | static const char* Type2String(StaticType* type); |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 59 | |
| 60 | // LIKELY_SMI accessors |
| 61 | void SetAsLikelySmi() { |
| 62 | kind_ = LIKELY_SMI; |
| 63 | } |
| 64 | |
| 65 | void SetAsLikelySmiIfUnknown() { |
| 66 | if (IsUnknown()) { |
| 67 | SetAsLikelySmi(); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | private: |
| 72 | Kind kind_; |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | |
| 76 | // The AST refers to variables via VariableProxies - placeholders for the actual |
| 77 | // variables. Variables themselves are never directly referred to from the AST, |
| 78 | // they are maintained by scopes, and referred to from VariableProxies and Slots |
| 79 | // after binding and variable allocation. |
| 80 | |
| 81 | class Variable: public ZoneObject { |
| 82 | public: |
| 83 | enum Mode { |
| 84 | // User declared variables: |
| 85 | VAR, // declared via 'var', and 'function' declarations |
| 86 | |
| 87 | CONST, // declared via 'const' declarations |
| 88 | |
| 89 | // Variables introduced by the compiler: |
| 90 | DYNAMIC, // always require dynamic lookup (we don't know |
| 91 | // the declaration) |
| 92 | |
| 93 | DYNAMIC_GLOBAL, // requires dynamic lookup, but we know that the |
| 94 | // variable is global unless it has been shadowed |
| 95 | // by an eval-introduced variable |
| 96 | |
| 97 | DYNAMIC_LOCAL, // requires dynamic lookup, but we know that the |
| 98 | // variable is local and where it is unless it |
| 99 | // has been shadowed by an eval-introduced |
| 100 | // variable |
| 101 | |
| 102 | INTERNAL, // like VAR, but not user-visible (may or may not |
| 103 | // be in a context) |
| 104 | |
| 105 | TEMPORARY // temporary variables (not user-visible), never |
| 106 | // in a context |
| 107 | }; |
| 108 | |
| 109 | enum Kind { |
| 110 | NORMAL, |
| 111 | THIS, |
| 112 | ARGUMENTS |
| 113 | }; |
| 114 | |
| 115 | Variable(Scope* scope, |
| 116 | Handle<String> name, |
| 117 | Mode mode, |
| 118 | bool is_valid_lhs, |
| 119 | Kind kind); |
| 120 | |
| 121 | // Printing support |
| 122 | static const char* Mode2String(Mode mode); |
| 123 | |
| 124 | // Type testing & conversion |
| Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame^] | 125 | Property* AsProperty() const; |
| 126 | Slot* AsSlot() const; |
| 127 | |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 128 | bool IsValidLeftHandSide() { return is_valid_LHS_; } |
| 129 | |
| 130 | // The source code for an eval() call may refer to a variable that is |
| 131 | // in an outer scope about which we don't know anything (it may not |
| 132 | // be the global scope). scope() is NULL in that case. Currently the |
| 133 | // scope is only used to follow the context chain length. |
| Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame^] | 134 | Scope* scope() const { return scope_; } |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 135 | |
| Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame^] | 136 | Handle<String> name() const { return name_; } |
| 137 | Mode mode() const { return mode_; } |
| 138 | bool is_accessed_from_inner_scope() const { |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 139 | return is_accessed_from_inner_scope_; |
| 140 | } |
| Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 141 | bool is_used() { return is_used_; } |
| 142 | void set_is_used(bool flag) { is_used_ = flag; } |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 143 | |
| 144 | bool IsVariable(Handle<String> n) const { |
| 145 | return !is_this() && name().is_identical_to(n); |
| 146 | } |
| 147 | |
| Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 148 | bool IsStackAllocated() const; |
| 149 | |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 150 | bool is_dynamic() const { |
| 151 | return (mode_ == DYNAMIC || |
| 152 | mode_ == DYNAMIC_GLOBAL || |
| 153 | mode_ == DYNAMIC_LOCAL); |
| 154 | } |
| 155 | |
| 156 | bool is_global() const; |
| 157 | bool is_this() const { return kind_ == THIS; } |
| 158 | bool is_arguments() const { return kind_ == ARGUMENTS; } |
| 159 | |
| 160 | // True if the variable is named eval and not known to be shadowed. |
| 161 | bool is_possibly_eval() const { |
| 162 | return IsVariable(Factory::eval_symbol()) && |
| 163 | (mode_ == DYNAMIC || mode_ == DYNAMIC_GLOBAL); |
| 164 | } |
| 165 | |
| 166 | Variable* local_if_not_shadowed() const { |
| 167 | ASSERT(mode_ == DYNAMIC_LOCAL && local_if_not_shadowed_ != NULL); |
| 168 | return local_if_not_shadowed_; |
| 169 | } |
| 170 | |
| 171 | void set_local_if_not_shadowed(Variable* local) { |
| 172 | local_if_not_shadowed_ = local; |
| 173 | } |
| 174 | |
| Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame^] | 175 | Expression* rewrite() const { return rewrite_; } |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 176 | |
| Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 177 | StaticType* type() { return &type_; } |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 178 | |
| 179 | private: |
| 180 | Scope* scope_; |
| 181 | Handle<String> name_; |
| 182 | Mode mode_; |
| 183 | bool is_valid_LHS_; |
| 184 | Kind kind_; |
| 185 | |
| 186 | Variable* local_if_not_shadowed_; |
| 187 | |
| 188 | // Usage info. |
| 189 | bool is_accessed_from_inner_scope_; // set by variable resolver |
| Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 190 | bool is_used_; |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 191 | |
| 192 | // Static type information |
| Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 193 | StaticType type_; |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 194 | |
| 195 | // Code generation. |
| 196 | // rewrite_ is usually a Slot or a Property, but may be any expression. |
| 197 | Expression* rewrite_; |
| 198 | |
| 199 | friend class Scope; // Has explicit access to rewrite_. |
| 200 | }; |
| 201 | |
| 202 | |
| 203 | } } // namespace v8::internal |
| 204 | |
| 205 | #endif // V8_VARIABLES_H_ |