Chromium Code Reviews| 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/pending-compilation-error-handler.h" |
| 10 #include "src/zone.h" | 10 #include "src/zone.h" |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 118 // is in an intermediate scope between this function scope and the the | 118 // is in an intermediate scope between this function scope and the the |
| 119 // outer scope. Only possible for function scopes; at most one variable. | 119 // outer scope. Only possible for function scopes; at most one variable. |
| 120 void DeclareFunctionVar(VariableDeclaration* declaration) { | 120 void DeclareFunctionVar(VariableDeclaration* declaration) { |
| 121 DCHECK(is_function_scope()); | 121 DCHECK(is_function_scope()); |
| 122 function_ = declaration; | 122 function_ = declaration; |
| 123 } | 123 } |
| 124 | 124 |
| 125 // Declare a parameter in this scope. When there are duplicated | 125 // Declare a parameter in this scope. When there are duplicated |
| 126 // parameters the rightmost one 'wins'. However, the implementation | 126 // parameters the rightmost one 'wins'. However, the implementation |
| 127 // expects all parameters to be declared and from left to right. | 127 // expects all parameters to be declared and from left to right. |
| 128 Variable* DeclareParameter(const AstRawString* name, VariableMode mode, | 128 Variable* DeclareParameter(const AstRawString* name, Expression* pattern, |
| 129 bool is_rest, bool* is_duplicate); | 129 VariableMode mode, bool is_rest, |
| 130 bool* is_duplicate); | |
| 130 | 131 |
| 131 // Declare a local variable in this scope. If the variable has been | 132 // Declare a local variable in this scope. If the variable has been |
| 132 // declared before, the previously declared variable is returned. | 133 // declared before, the previously declared variable is returned. |
| 133 Variable* DeclareLocal(const AstRawString* name, VariableMode mode, | 134 Variable* DeclareLocal(const AstRawString* name, VariableMode mode, |
| 134 InitializationFlag init_flag, Variable::Kind kind, | 135 InitializationFlag init_flag, Variable::Kind kind, |
| 135 MaybeAssignedFlag maybe_assigned_flag = kNotAssigned, | 136 MaybeAssignedFlag maybe_assigned_flag = kNotAssigned, |
| 136 int declaration_group_start = -1); | 137 int declaration_group_start = -1); |
| 137 | 138 |
| 138 // Declare an implicit global variable in this scope which must be a | 139 // Declare an implicit global variable in this scope which must be a |
| 139 // script scope. The variable was introduced (possibly from an inner | 140 // script scope. The variable was introduced (possibly from an inner |
| (...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 361 // The variable corresponding to the 'new.target' value. | 362 // The variable corresponding to the 'new.target' value. |
| 362 Variable* new_target_var() { return new_target_; } | 363 Variable* new_target_var() { return new_target_; } |
| 363 | 364 |
| 364 // The variable holding the function literal for named function | 365 // The variable holding the function literal for named function |
| 365 // literals, or NULL. Only valid for function scopes. | 366 // literals, or NULL. Only valid for function scopes. |
| 366 VariableDeclaration* function() const { | 367 VariableDeclaration* function() const { |
| 367 DCHECK(is_function_scope()); | 368 DCHECK(is_function_scope()); |
| 368 return function_; | 369 return function_; |
| 369 } | 370 } |
| 370 | 371 |
| 372 struct Parameter { | |
| 373 Parameter(Variable* var, Expression* pattern) | |
| 374 : var(var), pattern(pattern) {} | |
| 375 | |
| 376 Variable* var; | |
| 377 Expression* pattern; | |
|
rossberg
2015/06/16 17:02:13
Can we not abuse the scope abstraction to pass aro
Dmitry Lomov (no reviews)
2015/06/17 12:28:48
Good point. Addressed.
| |
| 378 }; | |
| 379 | |
| 371 // Parameters. The left-most parameter has index 0. | 380 // Parameters. The left-most parameter has index 0. |
| 372 // Only valid for function scopes. | 381 // Only valid for function scopes. |
| 373 Variable* parameter(int index) const { | 382 Variable* parameter_var(int index) const { return parameter(index).var; } |
| 383 | |
| 384 // Parameters. The left-most parameter has index 0. | |
| 385 // Only valid for function scopes. | |
| 386 const Parameter& parameter(int index) const { | |
| 374 DCHECK(is_function_scope()); | 387 DCHECK(is_function_scope()); |
| 375 return params_[index]; | 388 return params_[index]; |
| 376 } | 389 } |
| 377 | 390 |
| 378 // Returns the default function arity --- does not include rest parameters. | 391 // Returns the default function arity --- does not include rest parameters. |
| 379 int default_function_length() const { | 392 int default_function_length() const { |
| 380 int count = params_.length(); | 393 int count = params_.length(); |
| 381 if (rest_index_ >= 0) { | 394 if (rest_index_ >= 0) { |
| 382 DCHECK(count > 0); | 395 DCHECK(count > 0); |
| 383 DCHECK(is_function_scope()); | 396 DCHECK(is_function_scope()); |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 511 // "this" is an invalid parameter name and "arguments" is invalid parameter | 524 // "this" is an invalid parameter name and "arguments" is invalid parameter |
| 512 // name in strict mode. Therefore looking up with the map which includes | 525 // name in strict mode. Therefore looking up with the map which includes |
| 513 // "this" and "arguments" in addition to all formal parameters is safe. | 526 // "this" and "arguments" in addition to all formal parameters is safe. |
| 514 return variables_.Lookup(name) != NULL; | 527 return variables_.Lookup(name) != NULL; |
| 515 } | 528 } |
| 516 | 529 |
| 517 bool IsDeclaredParameter(const AstRawString* name) { | 530 bool IsDeclaredParameter(const AstRawString* name) { |
| 518 // If IsSimpleParameterList is false, duplicate parameters are not allowed, | 531 // If IsSimpleParameterList is false, duplicate parameters are not allowed, |
| 519 // however `arguments` may be allowed if function is not strict code. Thus, | 532 // however `arguments` may be allowed if function is not strict code. Thus, |
| 520 // the assumptions explained above do not hold. | 533 // the assumptions explained above do not hold. |
| 521 return params_.Contains(variables_.Lookup(name)); | 534 auto var = variables_.Lookup(name); |
| 535 for (auto p : params_) { | |
| 536 if (p.var == var) return true; | |
| 537 } | |
| 538 return false; | |
| 522 } | 539 } |
| 523 | 540 |
| 524 // Error handling. | 541 // Error handling. |
| 525 void ReportMessage(int start_position, int end_position, | 542 void ReportMessage(int start_position, int end_position, |
| 526 MessageTemplate::Template message, | 543 MessageTemplate::Template message, |
| 527 const AstRawString* arg); | 544 const AstRawString* arg); |
| 528 | 545 |
| 529 // --------------------------------------------------------------------------- | 546 // --------------------------------------------------------------------------- |
| 530 // Debugging. | 547 // Debugging. |
| 531 | 548 |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 555 // The variables declared in this scope: | 572 // The variables declared in this scope: |
| 556 // | 573 // |
| 557 // All user-declared variables (incl. parameters). For script scopes | 574 // All user-declared variables (incl. parameters). For script scopes |
| 558 // variables may be implicitly 'declared' by being used (possibly in | 575 // variables may be implicitly 'declared' by being used (possibly in |
| 559 // an inner scope) with no intervening with statements or eval calls. | 576 // an inner scope) with no intervening with statements or eval calls. |
| 560 VariableMap variables_; | 577 VariableMap variables_; |
| 561 // Compiler-allocated (user-invisible) internals. | 578 // Compiler-allocated (user-invisible) internals. |
| 562 ZoneList<Variable*> internals_; | 579 ZoneList<Variable*> internals_; |
| 563 // Compiler-allocated (user-invisible) temporaries. | 580 // Compiler-allocated (user-invisible) temporaries. |
| 564 ZoneList<Variable*> temps_; | 581 ZoneList<Variable*> temps_; |
| 582 | |
| 565 // Parameter list in source order. | 583 // Parameter list in source order. |
| 566 ZoneList<Variable*> params_; | 584 ZoneList<Parameter> params_; |
| 585 | |
| 567 // Variables that must be looked up dynamically. | 586 // Variables that must be looked up dynamically. |
| 568 DynamicScopePart* dynamics_; | 587 DynamicScopePart* dynamics_; |
| 569 // Unresolved variables referred to from this scope. | 588 // Unresolved variables referred to from this scope. |
| 570 ZoneList<VariableProxy*> unresolved_; | 589 ZoneList<VariableProxy*> unresolved_; |
| 571 // Declarations. | 590 // Declarations. |
| 572 ZoneList<Declaration*> decls_; | 591 ZoneList<Declaration*> decls_; |
| 573 // Convenience variable. | 592 // Convenience variable. |
| 574 Variable* receiver_; | 593 Variable* receiver_; |
| 575 // Function variable, if any; function scopes only. | 594 // Function variable, if any; function scopes only. |
| 576 VariableDeclaration* function_; | 595 VariableDeclaration* function_; |
| (...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 765 PendingCompilationErrorHandler pending_error_handler_; | 784 PendingCompilationErrorHandler pending_error_handler_; |
| 766 | 785 |
| 767 // For tracking which classes are declared consecutively. Needed for strong | 786 // For tracking which classes are declared consecutively. Needed for strong |
| 768 // mode. | 787 // mode. |
| 769 int class_declaration_group_start_; | 788 int class_declaration_group_start_; |
| 770 }; | 789 }; |
| 771 | 790 |
| 772 } } // namespace v8::internal | 791 } } // namespace v8::internal |
| 773 | 792 |
| 774 #endif // V8_SCOPES_H_ | 793 #endif // V8_SCOPES_H_ |
| OLD | NEW |