OLD | NEW |
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 // The AST refers to variables via VariableProxies - placeholders for the actual | 106 // The AST refers to variables via VariableProxies - placeholders for the actual |
107 // variables. Variables themselves are never directly referred to from the AST, | 107 // variables. Variables themselves are never directly referred to from the AST, |
108 // they are maintained by scopes, and referred to from VariableProxies and Slots | 108 // they are maintained by scopes, and referred to from VariableProxies and Slots |
109 // after binding and variable allocation. | 109 // after binding and variable allocation. |
110 | 110 |
111 class Variable: public ZoneObject { | 111 class Variable: public ZoneObject { |
112 public: | 112 public: |
113 enum Mode { | 113 enum Mode { |
114 // User declared variables: | 114 // User declared variables: |
115 VAR, // declared via 'var', and 'function' declarations | 115 VAR, // declared via 'var', and 'function' declarations |
| 116 |
116 CONST, // declared via 'const' declarations | 117 CONST, // declared via 'const' declarations |
117 | 118 |
118 // Variables introduced by the compiler: | 119 // Variables introduced by the compiler: |
119 DYNAMIC, // always require dynamic lookup (we don't know the declaration) | 120 DYNAMIC, // always require dynamic lookup (we don't know |
120 INTERNAL, // like VAR, but not user-visible (may or may not be in a | 121 // the declaration) |
121 // context) | 122 |
122 TEMPORARY // temporary variables (not user-visible), never in a context | 123 DYNAMIC_GLOBAL, // requires dynamic lookup, but we know that the |
| 124 // variable is global unless it has been shadowed |
| 125 // by an eval-introduced variable |
| 126 |
| 127 DYNAMIC_LOCAL, // requires dynamic lookup, but we know that the |
| 128 // variable is local and where it is unless it |
| 129 // has been shadowed by an eval-introduced |
| 130 // variable |
| 131 |
| 132 INTERNAL, // like VAR, but not user-visible (may or may not |
| 133 // be in a context) |
| 134 |
| 135 TEMPORARY // temporary variables (not user-visible), never |
| 136 // in a context |
123 }; | 137 }; |
124 | 138 |
125 // Printing support | 139 // Printing support |
126 static const char* Mode2String(Mode mode); | 140 static const char* Mode2String(Mode mode); |
127 | 141 |
128 // Type testing & conversion | 142 // Type testing & conversion |
129 Property* AsProperty(); | 143 Property* AsProperty(); |
130 Variable* AsVariable(); | 144 Variable* AsVariable(); |
131 bool IsValidLeftHandSide() { return is_valid_LHS_; } | 145 bool IsValidLeftHandSide() { return is_valid_LHS_; } |
132 | 146 |
(...skipping 10 matching lines...) Expand all Loading... |
143 bool is_accessed_from_inner_scope() const { | 157 bool is_accessed_from_inner_scope() const { |
144 return is_accessed_from_inner_scope_; | 158 return is_accessed_from_inner_scope_; |
145 } | 159 } |
146 UseCount* var_uses() { return &var_uses_; } | 160 UseCount* var_uses() { return &var_uses_; } |
147 UseCount* obj_uses() { return &obj_uses_; } | 161 UseCount* obj_uses() { return &obj_uses_; } |
148 | 162 |
149 bool IsVariable(Handle<String> n) { | 163 bool IsVariable(Handle<String> n) { |
150 return !is_this() && name().is_identical_to(n); | 164 return !is_this() && name().is_identical_to(n); |
151 } | 165 } |
152 | 166 |
| 167 bool is_dynamic() const { |
| 168 return (mode_ == DYNAMIC || |
| 169 mode_ == DYNAMIC_GLOBAL || |
| 170 mode_ == DYNAMIC_LOCAL); |
| 171 } |
| 172 |
153 bool is_global() const; | 173 bool is_global() const; |
154 bool is_this() const { return is_this_; } | 174 bool is_this() const { return is_this_; } |
155 | 175 |
| 176 Variable* local_if_not_shadowed() const { |
| 177 ASSERT(mode_ == DYNAMIC_LOCAL && local_if_not_shadowed_ != NULL); |
| 178 return local_if_not_shadowed_; |
| 179 } |
| 180 |
| 181 void set_local_if_not_shadowed(Variable* local) { |
| 182 local_if_not_shadowed_ = local; |
| 183 } |
| 184 |
156 Expression* rewrite() const { return rewrite_; } | 185 Expression* rewrite() const { return rewrite_; } |
157 Slot* slot() const; | 186 Slot* slot() const; |
158 | 187 |
159 StaticType* type() { return &type_; } | 188 StaticType* type() { return &type_; } |
160 | 189 |
161 private: | 190 private: |
162 Variable(Scope* scope, Handle<String> name, Mode mode, bool is_valid_LHS, | 191 Variable(Scope* scope, Handle<String> name, Mode mode, bool is_valid_LHS, |
163 bool is_this); | 192 bool is_this); |
164 | 193 |
165 Scope* scope_; | 194 Scope* scope_; |
166 Handle<String> name_; | 195 Handle<String> name_; |
167 Mode mode_; | 196 Mode mode_; |
168 bool is_valid_LHS_; | 197 bool is_valid_LHS_; |
169 bool is_this_; | 198 bool is_this_; |
170 | 199 |
| 200 Variable* local_if_not_shadowed_; |
| 201 |
171 // Usage info. | 202 // Usage info. |
172 bool is_accessed_from_inner_scope_; // set by variable resolver | 203 bool is_accessed_from_inner_scope_; // set by variable resolver |
173 UseCount var_uses_; // uses of the variable value | 204 UseCount var_uses_; // uses of the variable value |
174 UseCount obj_uses_; // uses of the object the variable points to | 205 UseCount obj_uses_; // uses of the object the variable points to |
175 | 206 |
176 // Static type information | 207 // Static type information |
177 StaticType type_; | 208 StaticType type_; |
178 | 209 |
179 // Code generation. | 210 // Code generation. |
180 // rewrite_ is usually a Slot or a Property, but maybe any expression. | 211 // rewrite_ is usually a Slot or a Property, but maybe any expression. |
181 Expression* rewrite_; | 212 Expression* rewrite_; |
182 | 213 |
183 friend class VariableProxy; | 214 friend class VariableProxy; |
184 friend class Scope; | 215 friend class Scope; |
185 friend class LocalsMap; | 216 friend class LocalsMap; |
186 friend class AstBuildingParser; | 217 friend class AstBuildingParser; |
187 }; | 218 }; |
188 | 219 |
189 | 220 |
190 } } // namespace v8::internal | 221 } } // namespace v8::internal |
191 | 222 |
192 #endif // V8_VARIABLES_H_ | 223 #endif // V8_VARIABLES_H_ |
OLD | NEW |