karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 33 | namespace v8 { |
| 34 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 35 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 36 | // 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 | |
| 41 | class Variable: public ZoneObject { |
| 42 | public: |
| 43 | enum Mode { |
| 44 | // User declared variables: |
| 45 | VAR, // declared via 'var', and 'function' declarations |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 46 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 47 | CONST, // declared via 'const' declarations |
| 48 | |
| 49 | // Variables introduced by the compiler: |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 50 | DYNAMIC, // always require dynamic lookup (we don't know |
| 51 | // the declaration) |
| 52 | |
| 53 | DYNAMIC_GLOBAL, // requires dynamic lookup, but we know that the |
| 54 | // variable is global unless it has been shadowed |
| 55 | // by an eval-introduced variable |
| 56 | |
| 57 | DYNAMIC_LOCAL, // requires dynamic lookup, but we know that the |
| 58 | // variable is local and where it is unless it |
| 59 | // has been shadowed by an eval-introduced |
| 60 | // variable |
| 61 | |
| 62 | INTERNAL, // like VAR, but not user-visible (may or may not |
| 63 | // be in a context) |
| 64 | |
| 65 | TEMPORARY // temporary variables (not user-visible), never |
| 66 | // in a context |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 67 | }; |
| 68 | |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 69 | enum Kind { |
| 70 | NORMAL, |
| 71 | THIS, |
| 72 | ARGUMENTS |
| 73 | }; |
| 74 | |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 75 | Variable(Scope* scope, |
| 76 | Handle<String> name, |
| 77 | Mode mode, |
| 78 | bool is_valid_lhs, |
| 79 | Kind kind); |
| 80 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 81 | // Printing support |
| 82 | static const char* Mode2String(Mode mode); |
| 83 | |
| 84 | // Type testing & conversion |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 85 | Property* AsProperty() const; |
| 86 | Slot* AsSlot() const; |
| 87 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 88 | bool IsValidLeftHandSide() { return is_valid_LHS_; } |
| 89 | |
| 90 | // The source code for an eval() call may refer to a variable that is |
| 91 | // in an outer scope about which we don't know anything (it may not |
| 92 | // be the global scope). scope() is NULL in that case. Currently the |
| 93 | // scope is only used to follow the context chain length. |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 94 | Scope* scope() const { return scope_; } |
sgjesse@chromium.org | b9d7da1 | 2009-08-05 08:38:10 +0000 | [diff] [blame] | 95 | |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 96 | Handle<String> name() const { return name_; } |
| 97 | Mode mode() const { return mode_; } |
| 98 | bool is_accessed_from_inner_scope() const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 99 | return is_accessed_from_inner_scope_; |
| 100 | } |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 101 | void MarkAsAccessedFromInnerScope() { |
| 102 | is_accessed_from_inner_scope_ = true; |
| 103 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 104 | bool is_used() { return is_used_; } |
| 105 | void set_is_used(bool flag) { is_used_ = flag; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 106 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 107 | bool IsVariable(Handle<String> n) const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 108 | return !is_this() && name().is_identical_to(n); |
| 109 | } |
| 110 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 111 | bool IsStackAllocated() const; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 112 | bool IsParameter() const; // Includes 'this'. |
| 113 | bool IsStackLocal() const; |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 114 | bool IsContextSlot() const; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 115 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 116 | bool is_dynamic() const { |
| 117 | return (mode_ == DYNAMIC || |
| 118 | mode_ == DYNAMIC_GLOBAL || |
| 119 | mode_ == DYNAMIC_LOCAL); |
| 120 | } |
| 121 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 122 | bool is_global() const; |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 123 | bool is_this() const { return kind_ == THIS; } |
| 124 | bool is_arguments() const { return kind_ == ARGUMENTS; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 125 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 126 | // True if the variable is named eval and not known to be shadowed. |
| 127 | bool is_possibly_eval() const { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 128 | return IsVariable(FACTORY->eval_symbol()) && |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 129 | (mode_ == DYNAMIC || mode_ == DYNAMIC_GLOBAL); |
| 130 | } |
| 131 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 132 | Variable* local_if_not_shadowed() const { |
| 133 | ASSERT(mode_ == DYNAMIC_LOCAL && local_if_not_shadowed_ != NULL); |
| 134 | return local_if_not_shadowed_; |
| 135 | } |
| 136 | |
| 137 | void set_local_if_not_shadowed(Variable* local) { |
| 138 | local_if_not_shadowed_ = local; |
| 139 | } |
| 140 | |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 141 | Expression* rewrite() const { return rewrite_; } |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 142 | void set_rewrite(Expression* expr) { rewrite_ = expr; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 143 | |
| 144 | private: |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 145 | Scope* scope_; |
| 146 | Handle<String> name_; |
| 147 | Mode mode_; |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 148 | Kind kind_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 149 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 150 | Variable* local_if_not_shadowed_; |
| 151 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 152 | // Code generation. |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 153 | // rewrite_ is usually a Slot or a Property, but may be any expression. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 154 | Expression* rewrite_; |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 155 | |
| 156 | // Valid as a LHS? (const and this are not valid LHS, for example) |
| 157 | bool is_valid_LHS_; |
| 158 | |
| 159 | // Usage info. |
| 160 | bool is_accessed_from_inner_scope_; // set by variable resolver |
| 161 | bool is_used_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 162 | }; |
| 163 | |
| 164 | |
| 165 | } } // namespace v8::internal |
| 166 | |
| 167 | #endif // V8_VARIABLES_H_ |