| OLD | NEW |
| 1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef V8_SCOPES_H_ | 5 #ifndef V8_SCOPES_H_ |
| 6 #define V8_SCOPES_H_ | 6 #define V8_SCOPES_H_ |
| 7 | 7 |
| 8 #include "src/ast.h" | 8 #include "src/ast.h" |
| 9 #include "src/pending-compilation-error-handler.h" |
| 9 #include "src/zone.h" | 10 #include "src/zone.h" |
| 10 | 11 |
| 11 namespace v8 { | 12 namespace v8 { |
| 12 namespace internal { | 13 namespace internal { |
| 13 | 14 |
| 14 class CompilationInfo; | 15 class CompilationInfo; |
| 15 | 16 |
| 16 | 17 |
| 17 // A hash map to support fast variable declaration and lookup. | 18 // A hash map to support fast variable declaration and lookup. |
| 18 class VariableMap: public ZoneHashMap { | 19 class VariableMap: public ZoneHashMap { |
| 19 public: | 20 public: |
| 20 explicit VariableMap(Zone* zone); | 21 explicit VariableMap(Zone* zone); |
| 21 | 22 |
| 22 virtual ~VariableMap(); | 23 virtual ~VariableMap(); |
| 23 | 24 |
| 25 // Passing the real position is needed for variables where |
| 26 // use-before-declaration is possible. For many types of variables |
| 27 // (parameters, catch variable etc.) the use is fine whenever variable |
| 28 // resolution succeeds, and for those variables, we don't need to know the |
| 29 // positions. |
| 24 Variable* Declare(Scope* scope, const AstRawString* name, VariableMode mode, | 30 Variable* Declare(Scope* scope, const AstRawString* name, VariableMode mode, |
| 25 bool is_valid_lhs, Variable::Kind kind, | 31 bool is_valid_lhs, Variable::Kind kind, |
| 26 InitializationFlag initialization_flag, | 32 InitializationFlag initialization_flag, |
| 33 int position = RelocInfo::kNoPosition, |
| 27 MaybeAssignedFlag maybe_assigned_flag = kNotAssigned); | 34 MaybeAssignedFlag maybe_assigned_flag = kNotAssigned); |
| 28 | 35 |
| 29 Variable* Lookup(const AstRawString* name); | 36 Variable* Lookup(const AstRawString* name); |
| 30 | 37 |
| 31 Zone* zone() const { return zone_; } | 38 Zone* zone() const { return zone_; } |
| 32 | 39 |
| 33 private: | 40 private: |
| 34 Zone* zone_; | 41 Zone* zone_; |
| 35 }; | 42 }; |
| 36 | 43 |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 | 130 |
| 124 // Declare a parameter in this scope. When there are duplicated | 131 // Declare a parameter in this scope. When there are duplicated |
| 125 // parameters the rightmost one 'wins'. However, the implementation | 132 // parameters the rightmost one 'wins'. However, the implementation |
| 126 // expects all parameters to be declared and from left to right. | 133 // expects all parameters to be declared and from left to right. |
| 127 Variable* DeclareParameter(const AstRawString* name, VariableMode mode, | 134 Variable* DeclareParameter(const AstRawString* name, VariableMode mode, |
| 128 bool is_rest = false); | 135 bool is_rest = false); |
| 129 | 136 |
| 130 // Declare a local variable in this scope. If the variable has been | 137 // Declare a local variable in this scope. If the variable has been |
| 131 // declared before, the previously declared variable is returned. | 138 // declared before, the previously declared variable is returned. |
| 132 Variable* DeclareLocal(const AstRawString* name, VariableMode mode, | 139 Variable* DeclareLocal(const AstRawString* name, VariableMode mode, |
| 133 InitializationFlag init_flag, | 140 InitializationFlag init_flag, int position, |
| 141 bool is_function, |
| 134 MaybeAssignedFlag maybe_assigned_flag = kNotAssigned); | 142 MaybeAssignedFlag maybe_assigned_flag = kNotAssigned); |
| 135 | 143 |
| 136 // Declare an implicit global variable in this scope which must be a | 144 // Declare an implicit global variable in this scope which must be a |
| 137 // script scope. The variable was introduced (possibly from an inner | 145 // script scope. The variable was introduced (possibly from an inner |
| 138 // scope) by a reference to an unresolved variable with no intervening | 146 // scope) by a reference to an unresolved variable with no intervening |
| 139 // with statements or eval calls. | 147 // with statements or eval calls. |
| 140 Variable* DeclareDynamicGlobal(const AstRawString* name); | 148 Variable* DeclareDynamicGlobal(const AstRawString* name); |
| 141 | 149 |
| 142 // Create a new unresolved variable. | 150 // Create a new unresolved variable. |
| 143 VariableProxy* NewUnresolved(AstNodeFactory* factory, | 151 VariableProxy* NewUnresolved(AstNodeFactory* factory, |
| 144 const AstRawString* name, | 152 const AstRawString* name, |
| 145 int position = RelocInfo::kNoPosition) { | 153 int start_position = RelocInfo::kNoPosition, |
| 154 int end_position = RelocInfo::kNoPosition) { |
| 146 // Note that we must not share the unresolved variables with | 155 // Note that we must not share the unresolved variables with |
| 147 // the same name because they may be removed selectively via | 156 // the same name because they may be removed selectively via |
| 148 // RemoveUnresolved(). | 157 // RemoveUnresolved(). |
| 149 DCHECK(!already_resolved()); | 158 DCHECK(!already_resolved()); |
| 150 VariableProxy* proxy = factory->NewVariableProxy(name, false, position); | 159 VariableProxy* proxy = |
| 160 factory->NewVariableProxy(name, false, start_position, end_position); |
| 151 unresolved_.Add(proxy, zone_); | 161 unresolved_.Add(proxy, zone_); |
| 152 return proxy; | 162 return proxy; |
| 153 } | 163 } |
| 154 | 164 |
| 155 // Remove a unresolved variable. During parsing, an unresolved variable | 165 // Remove a unresolved variable. During parsing, an unresolved variable |
| 156 // may have been added optimistically, but then only the variable name | 166 // may have been added optimistically, but then only the variable name |
| 157 // was used (typically for labels). If the variable was not declared, the | 167 // was used (typically for labels). If the variable was not declared, the |
| 158 // addition introduced a new unresolved variable which may end up being | 168 // addition introduced a new unresolved variable which may end up being |
| 159 // allocated globally as a "ghost" variable. RemoveUnresolved removes | 169 // allocated globally as a "ghost" variable. RemoveUnresolved removes |
| 160 // such a variable again if it was added; otherwise this is a no-op. | 170 // such a variable again if it was added; otherwise this is a no-op. |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 310 bool uses_super_property() const { return scope_uses_super_property_; } | 320 bool uses_super_property() const { return scope_uses_super_property_; } |
| 311 // Does any inner scope access "super" property. | 321 // Does any inner scope access "super" property. |
| 312 bool inner_uses_super_property() const { | 322 bool inner_uses_super_property() const { |
| 313 return inner_scope_uses_super_property_; | 323 return inner_scope_uses_super_property_; |
| 314 } | 324 } |
| 315 // Does this scope access "this". | 325 // Does this scope access "this". |
| 316 bool uses_this() const { return scope_uses_this_; } | 326 bool uses_this() const { return scope_uses_this_; } |
| 317 // Does any inner scope access "this". | 327 // Does any inner scope access "this". |
| 318 bool inner_uses_this() const { return inner_scope_uses_this_; } | 328 bool inner_uses_this() const { return inner_scope_uses_this_; } |
| 319 | 329 |
| 330 const Scope* NearestOuterEvalScope() const { |
| 331 if (is_eval_scope()) return this; |
| 332 if (outer_scope() == nullptr) return nullptr; |
| 333 return outer_scope()->NearestOuterEvalScope(); |
| 334 } |
| 335 |
| 320 // --------------------------------------------------------------------------- | 336 // --------------------------------------------------------------------------- |
| 321 // Accessors. | 337 // Accessors. |
| 322 | 338 |
| 323 // The type of this scope. | 339 // The type of this scope. |
| 324 ScopeType scope_type() const { return scope_type_; } | 340 ScopeType scope_type() const { return scope_type_; } |
| 325 | 341 |
| 326 // The language mode of this scope. | 342 // The language mode of this scope. |
| 327 LanguageMode language_mode() const { return language_mode_; } | 343 LanguageMode language_mode() const { return language_mode_; } |
| 328 | 344 |
| 329 // The variable corresponding to the 'this' value. | 345 // The variable corresponding to the 'this' value. |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 463 return variables_.Lookup(name) != NULL; | 479 return variables_.Lookup(name) != NULL; |
| 464 } | 480 } |
| 465 | 481 |
| 466 bool IsDeclaredParameter(const AstRawString* name) { | 482 bool IsDeclaredParameter(const AstRawString* name) { |
| 467 // If IsSimpleParameterList is false, duplicate parameters are not allowed, | 483 // If IsSimpleParameterList is false, duplicate parameters are not allowed, |
| 468 // however `arguments` may be allowed if function is not strict code. Thus, | 484 // however `arguments` may be allowed if function is not strict code. Thus, |
| 469 // the assumptions explained above do not hold. | 485 // the assumptions explained above do not hold. |
| 470 return params_.Contains(variables_.Lookup(name)); | 486 return params_.Contains(variables_.Lookup(name)); |
| 471 } | 487 } |
| 472 | 488 |
| 489 // Error handling. |
| 490 void ReportMessage(int start_position, int end_position, const char* message, |
| 491 const AstRawString* arg); |
| 492 |
| 473 // --------------------------------------------------------------------------- | 493 // --------------------------------------------------------------------------- |
| 474 // Debugging. | 494 // Debugging. |
| 475 | 495 |
| 476 #ifdef DEBUG | 496 #ifdef DEBUG |
| 477 void Print(int n = 0); // n = indentation; n < 0 => don't print recursively | 497 void Print(int n = 0); // n = indentation; n < 0 => don't print recursively |
| 478 #endif | 498 #endif |
| 479 | 499 |
| 480 // --------------------------------------------------------------------------- | 500 // --------------------------------------------------------------------------- |
| 481 // Implementation. | 501 // Implementation. |
| 482 protected: | 502 protected: |
| (...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 689 inner_scope->outer_scope_ = this; | 709 inner_scope->outer_scope_ = this; |
| 690 } | 710 } |
| 691 } | 711 } |
| 692 | 712 |
| 693 void SetDefaults(ScopeType type, | 713 void SetDefaults(ScopeType type, |
| 694 Scope* outer_scope, | 714 Scope* outer_scope, |
| 695 Handle<ScopeInfo> scope_info); | 715 Handle<ScopeInfo> scope_info); |
| 696 | 716 |
| 697 AstValueFactory* ast_value_factory_; | 717 AstValueFactory* ast_value_factory_; |
| 698 Zone* zone_; | 718 Zone* zone_; |
| 719 |
| 720 PendingCompilationErrorHandler pending_error_handler_; |
| 699 }; | 721 }; |
| 700 | 722 |
| 701 } } // namespace v8::internal | 723 } } // namespace v8::internal |
| 702 | 724 |
| 703 #endif // V8_SCOPES_H_ | 725 #endif // V8_SCOPES_H_ |
| OLD | NEW |