| 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 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 // doesn't re-allocate variables repeatedly. | 80 // doesn't re-allocate variables repeatedly. |
| 81 static bool Analyze(ParseInfo* info); | 81 static bool Analyze(ParseInfo* info); |
| 82 | 82 |
| 83 static Scope* DeserializeScopeChain(Isolate* isolate, Zone* zone, | 83 static Scope* DeserializeScopeChain(Isolate* isolate, Zone* zone, |
| 84 Context* context, Scope* script_scope); | 84 Context* context, Scope* script_scope); |
| 85 | 85 |
| 86 // The scope name is only used for printing/debugging. | 86 // The scope name is only used for printing/debugging. |
| 87 void SetScopeName(const AstRawString* scope_name) { | 87 void SetScopeName(const AstRawString* scope_name) { |
| 88 scope_name_ = scope_name; | 88 scope_name_ = scope_name; |
| 89 } | 89 } |
| 90 | 90 const AstRawString* scope_name() const { return scope_name_; } |
| 91 void Initialize(); | 91 void Initialize(); |
| 92 | 92 |
| 93 // Checks if the block scope is redundant, i.e. it does not contain any | 93 // Checks if the block scope is redundant, i.e. it does not contain any |
| 94 // block scoped declarations. In that case it is removed from the scope | 94 // block scoped declarations. In that case it is removed from the scope |
| 95 // tree and its children are reparented. | 95 // tree and its children are reparented. |
| 96 Scope* FinalizeBlockScope(); | 96 Scope* FinalizeBlockScope(); |
| 97 | 97 |
| 98 Zone* zone() const { return zone_; } | 98 Zone* zone() const { return zone_; } |
| 99 | 99 |
| 100 // --------------------------------------------------------------------------- | 100 // --------------------------------------------------------------------------- |
| (...skipping 18 matching lines...) Expand all Loading... |
| 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, VariableMode mode, |
| 129 bool is_rest, bool* is_duplicate); | 129 int pos, bool is_rest, bool* is_duplicate); |
| 130 | 130 |
| 131 // Declare a local variable in this scope. If the variable has been | 131 // Declare a local variable in this scope. If the variable has been |
| 132 // declared before, the previously declared variable is returned. | 132 // declared before, the previously declared variable is returned. |
| 133 Variable* DeclareLocal(const AstRawString* name, VariableMode mode, | 133 Variable* DeclareLocal(const AstRawString* name, VariableMode mode, |
| 134 InitializationFlag init_flag, Variable::Kind kind, | 134 InitializationFlag init_flag, Variable::Kind kind, |
| 135 MaybeAssignedFlag maybe_assigned_flag = kNotAssigned, | 135 MaybeAssignedFlag maybe_assigned_flag = kNotAssigned, |
| 136 int declaration_group_start = -1); | 136 int declaration_group_start = -1); |
| 137 | 137 Variable* RedeclareLocal(const AstRawString* name, VariableMode mode, |
| 138 InitializationFlag init_flag, Variable::Kind kind, |
| 139 MaybeAssignedFlag maybe_assigned_flag = kNotAssigned, |
| 140 int declaration_group_start = -1); |
| 138 // Declare an implicit global variable in this scope which must be a | 141 // Declare an implicit global variable in this scope which must be a |
| 139 // script scope. The variable was introduced (possibly from an inner | 142 // script scope. The variable was introduced (possibly from an inner |
| 140 // scope) by a reference to an unresolved variable with no intervening | 143 // scope) by a reference to an unresolved variable with no intervening |
| 141 // with statements or eval calls. | 144 // with statements or eval calls. |
| 142 Variable* DeclareDynamicGlobal(const AstRawString* name); | 145 Variable* DeclareDynamicGlobal(const AstRawString* name); |
| 143 | 146 |
| 144 // Create a new unresolved variable. | 147 // Create a new unresolved variable. |
| 145 VariableProxy* NewUnresolved(AstNodeFactory* factory, | 148 VariableProxy* NewUnresolved(AstNodeFactory* factory, |
| 146 const AstRawString* name, | 149 const AstRawString* name, |
| 147 int start_position = RelocInfo::kNoPosition, | 150 int start_position = RelocInfo::kNoPosition, |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 269 } | 272 } |
| 270 | 273 |
| 271 // --------------------------------------------------------------------------- | 274 // --------------------------------------------------------------------------- |
| 272 // Predicates. | 275 // Predicates. |
| 273 | 276 |
| 274 // Specific scope types. | 277 // Specific scope types. |
| 275 bool is_eval_scope() const { return scope_type_ == EVAL_SCOPE; } | 278 bool is_eval_scope() const { return scope_type_ == EVAL_SCOPE; } |
| 276 bool is_function_scope() const { | 279 bool is_function_scope() const { |
| 277 return scope_type_ == FUNCTION_SCOPE || scope_type_ == ARROW_SCOPE; | 280 return scope_type_ == FUNCTION_SCOPE || scope_type_ == ARROW_SCOPE; |
| 278 } | 281 } |
| 282 bool is_function_body_scope() const { |
| 283 return scope_type_ == FUNCTION_BODY_SCOPE; |
| 284 } |
| 279 bool is_module_scope() const { return scope_type_ == MODULE_SCOPE; } | 285 bool is_module_scope() const { return scope_type_ == MODULE_SCOPE; } |
| 280 bool is_script_scope() const { return scope_type_ == SCRIPT_SCOPE; } | 286 bool is_script_scope() const { return scope_type_ == SCRIPT_SCOPE; } |
| 281 bool is_catch_scope() const { return scope_type_ == CATCH_SCOPE; } | 287 bool is_catch_scope() const { return scope_type_ == CATCH_SCOPE; } |
| 282 bool is_block_scope() const { return scope_type_ == BLOCK_SCOPE; } | 288 bool is_block_scope() const { |
| 289 return scope_type_ == BLOCK_SCOPE || scope_type_ == FUNCTION_BODY_SCOPE; |
| 290 } |
| 283 bool is_with_scope() const { return scope_type_ == WITH_SCOPE; } | 291 bool is_with_scope() const { return scope_type_ == WITH_SCOPE; } |
| 284 bool is_arrow_scope() const { return scope_type_ == ARROW_SCOPE; } | 292 bool is_arrow_scope() const { return scope_type_ == ARROW_SCOPE; } |
| 285 void tag_as_class_scope() { | 293 void tag_as_class_scope() { |
| 286 DCHECK(is_block_scope()); | 294 DCHECK(is_block_scope()); |
| 287 block_scope_is_class_scope_ = true; | 295 block_scope_is_class_scope_ = true; |
| 288 } | 296 } |
| 289 bool is_class_scope() const { | 297 bool is_class_scope() const { |
| 290 return is_block_scope() && block_scope_is_class_scope_; | 298 return is_block_scope() && block_scope_is_class_scope_; |
| 291 } | 299 } |
| 292 bool is_declaration_scope() const { | 300 bool is_declaration_scope() const { |
| 293 return is_eval_scope() || is_function_scope() || | 301 return is_eval_scope() || is_function_scope() || is_module_scope() || |
| 294 is_module_scope() || is_script_scope(); | 302 is_script_scope() || is_function_body_scope(); |
| 295 } | 303 } |
| 296 bool is_strict_eval_scope() const { | 304 bool is_strict_eval_scope() const { |
| 297 return is_eval_scope() && is_strict(language_mode_); | 305 return is_eval_scope() && is_strict(language_mode_); |
| 298 } | 306 } |
| 299 | 307 |
| 300 // Information about which scopes calls eval. | 308 // Information about which scopes calls eval. |
| 301 bool calls_eval() const { return scope_calls_eval_; } | 309 bool calls_eval() const { return scope_calls_eval_; } |
| 302 bool calls_sloppy_eval() { | 310 bool calls_sloppy_eval() { |
| 303 return scope_calls_eval_ && is_sloppy(language_mode_); | 311 return scope_calls_eval_ && is_sloppy(language_mode_); |
| 304 } | 312 } |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 357 DCHECK(is_function_scope()); | 365 DCHECK(is_function_scope()); |
| 358 return function_; | 366 return function_; |
| 359 } | 367 } |
| 360 | 368 |
| 361 // Parameters. The left-most parameter has index 0. | 369 // Parameters. The left-most parameter has index 0. |
| 362 // Only valid for function scopes. | 370 // Only valid for function scopes. |
| 363 Variable* parameter(int index) const { | 371 Variable* parameter(int index) const { |
| 364 DCHECK(is_function_scope()); | 372 DCHECK(is_function_scope()); |
| 365 return params_[index]; | 373 return params_[index]; |
| 366 } | 374 } |
| 375 int parameter_position(int index) const { |
| 376 DCHECK(is_function_scope()); |
| 377 return param_positions_[index]; |
| 378 } |
| 367 | 379 |
| 368 // Returns the default function arity --- does not include rest parameters. | 380 // Returns the default function arity --- does not include rest parameters. |
| 369 int default_function_length() const { | 381 int default_function_length() const { |
| 370 int count = params_.length(); | 382 int count = params_.length(); |
| 371 if (rest_index_ >= 0) { | 383 if (rest_index_ >= 0) { |
| 372 DCHECK(count > 0); | 384 DCHECK(count > 0); |
| 373 DCHECK(is_function_scope()); | 385 DCHECK(is_function_scope()); |
| 374 --count; | 386 --count; |
| 375 } | 387 } |
| 376 return count; | 388 return count; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 403 | 415 |
| 404 // Declarations list. | 416 // Declarations list. |
| 405 ZoneList<Declaration*>* declarations() { return &decls_; } | 417 ZoneList<Declaration*>* declarations() { return &decls_; } |
| 406 | 418 |
| 407 // Inner scope list. | 419 // Inner scope list. |
| 408 ZoneList<Scope*>* inner_scopes() { return &inner_scopes_; } | 420 ZoneList<Scope*>* inner_scopes() { return &inner_scopes_; } |
| 409 | 421 |
| 410 // The scope immediately surrounding this scope, or NULL. | 422 // The scope immediately surrounding this scope, or NULL. |
| 411 Scope* outer_scope() const { return outer_scope_; } | 423 Scope* outer_scope() const { return outer_scope_; } |
| 412 | 424 |
| 425 // The inner scope which contains the main "body" of a function (may be null) |
| 426 Scope* function_body() const { return function_body_; } |
| 427 |
| 413 // The ModuleDescriptor for this scope; only for module scopes. | 428 // The ModuleDescriptor for this scope; only for module scopes. |
| 414 ModuleDescriptor* module() const { return module_descriptor_; } | 429 ModuleDescriptor* module() const { return module_descriptor_; } |
| 415 | 430 |
| 416 | 431 |
| 417 void set_class_declaration_group_start(int position) { | 432 void set_class_declaration_group_start(int position) { |
| 418 class_declaration_group_start_ = position; | 433 class_declaration_group_start_ = position; |
| 419 } | 434 } |
| 420 | 435 |
| 421 int class_declaration_group_start() const { | 436 int class_declaration_group_start() const { |
| 422 return class_declaration_group_start_; | 437 return class_declaration_group_start_; |
| 423 } | 438 } |
| 424 | 439 |
| 425 // --------------------------------------------------------------------------- | 440 // --------------------------------------------------------------------------- |
| 426 // Variable allocation. | 441 // Variable allocation. |
| 427 | 442 |
| 428 // Collect stack and context allocated local variables in this scope. Note | 443 // Collect stack and context allocated local variables in this scope. Note |
| 429 // that the function variable - if present - is not collected and should be | 444 // that the function variable - if present - is not collected and should be |
| 430 // handled separately. | 445 // handled separately. |
| 431 void CollectStackAndContextLocals( | 446 void CollectStackAndContextLocals( |
| 432 ZoneList<Variable*>* stack_locals, ZoneList<Variable*>* context_locals, | 447 ZoneList<Variable*>* stack_locals, ZoneList<Variable*>* context_locals, |
| 433 ZoneList<Variable*>* strong_mode_free_variables = nullptr); | 448 ZoneList<Variable*>* strong_mode_free_variables = nullptr); |
| 434 | 449 |
| 435 // Current number of var or const locals. | 450 // Current number of var or const locals. |
| 436 int num_var_or_const() { return num_var_or_const_; } | 451 int num_var_or_const() { return num_var_or_const_; } |
| 437 | 452 |
| 438 // Result of variable allocation. | 453 // Result of variable allocation. |
| 439 int num_stack_slots() const { return num_stack_slots_; } | 454 int num_stack_slots() const { return num_stack_slots_; } |
| 440 int num_heap_slots() const { return num_heap_slots_; } | 455 int num_heap_slots() const { return num_heap_slots_; } |
| 441 | |
| 442 int StackLocalCount() const; | 456 int StackLocalCount() const; |
| 443 int ContextLocalCount() const; | 457 int ContextLocalCount() const; |
| 444 | 458 |
| 445 // For script scopes, the number of module literals (including nested ones). | 459 // For script scopes, the number of module literals (including nested ones). |
| 446 int num_modules() const { return num_modules_; } | 460 int num_modules() const { return num_modules_; } |
| 447 | 461 |
| 448 // For module scopes, the host scope's internal variable binding this module. | 462 // For module scopes, the host scope's internal variable binding this module. |
| 449 Variable* module_var() const { return module_var_; } | 463 Variable* module_var() const { return module_var_; } |
| 450 | 464 |
| 451 // Make sure this scope and all outer scopes are eagerly compiled. | 465 // Make sure this scope and all outer scopes are eagerly compiled. |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 514 #endif | 528 #endif |
| 515 | 529 |
| 516 // --------------------------------------------------------------------------- | 530 // --------------------------------------------------------------------------- |
| 517 // Implementation. | 531 // Implementation. |
| 518 protected: | 532 protected: |
| 519 friend class ParserFactory; | 533 friend class ParserFactory; |
| 520 | 534 |
| 521 // Scope tree. | 535 // Scope tree. |
| 522 Scope* outer_scope_; // the immediately enclosing outer scope, or NULL | 536 Scope* outer_scope_; // the immediately enclosing outer scope, or NULL |
| 523 ZoneList<Scope*> inner_scopes_; // the immediately enclosed inner scopes | 537 ZoneList<Scope*> inner_scopes_; // the immediately enclosed inner scopes |
| 538 Scope* function_body_; // The scope for a function body |
| 524 | 539 |
| 525 // The scope type. | 540 // The scope type. |
| 526 ScopeType scope_type_; | 541 ScopeType scope_type_; |
| 527 // Some block scopes are tagged as class scopes. | 542 // Some block scopes are tagged as class scopes. |
| 528 bool block_scope_is_class_scope_; | 543 bool block_scope_is_class_scope_; |
| 529 // If the scope is a function scope, this is the function kind. | 544 // If the scope is a function scope, this is the function kind. |
| 530 FunctionKind function_kind_; | 545 FunctionKind function_kind_; |
| 531 | 546 |
| 532 // Debugging support. | 547 // Debugging support. |
| 533 const AstRawString* scope_name_; | 548 const AstRawString* scope_name_; |
| 534 | 549 |
| 535 // The variables declared in this scope: | 550 // The variables declared in this scope: |
| 536 // | 551 // |
| 537 // All user-declared variables (incl. parameters). For script scopes | 552 // All user-declared variables (incl. parameters). For script scopes |
| 538 // variables may be implicitly 'declared' by being used (possibly in | 553 // variables may be implicitly 'declared' by being used (possibly in |
| 539 // an inner scope) with no intervening with statements or eval calls. | 554 // an inner scope) with no intervening with statements or eval calls. |
| 540 VariableMap variables_; | 555 VariableMap variables_; |
| 541 // Compiler-allocated (user-invisible) internals. | 556 // Compiler-allocated (user-invisible) internals. |
| 542 ZoneList<Variable*> internals_; | 557 ZoneList<Variable*> internals_; |
| 543 // Compiler-allocated (user-invisible) temporaries. | 558 // Compiler-allocated (user-invisible) temporaries. |
| 544 ZoneList<Variable*> temps_; | 559 ZoneList<Variable*> temps_; |
| 545 // Parameter list in source order. | 560 // Parameter list in source order. |
| 546 ZoneList<Variable*> params_; | 561 ZoneList<Variable*> params_; |
| 562 // List of positions, in source order. |
| 563 ZoneList<int> param_positions_; |
| 547 // Variables that must be looked up dynamically. | 564 // Variables that must be looked up dynamically. |
| 548 DynamicScopePart* dynamics_; | 565 DynamicScopePart* dynamics_; |
| 549 // Unresolved variables referred to from this scope. | 566 // Unresolved variables referred to from this scope. |
| 550 ZoneList<VariableProxy*> unresolved_; | 567 ZoneList<VariableProxy*> unresolved_; |
| 551 // Declarations. | 568 // Declarations. |
| 552 ZoneList<Declaration*> decls_; | 569 ZoneList<Declaration*> decls_; |
| 553 // Convenience variable. | 570 // Convenience variable. |
| 554 Variable* receiver_; | 571 Variable* receiver_; |
| 555 // Function variable, if any; function scopes only. | 572 // Function variable, if any; function scopes only. |
| 556 VariableDeclaration* function_; | 573 VariableDeclaration* function_; |
| (...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 745 PendingCompilationErrorHandler pending_error_handler_; | 762 PendingCompilationErrorHandler pending_error_handler_; |
| 746 | 763 |
| 747 // For tracking which classes are declared consecutively. Needed for strong | 764 // For tracking which classes are declared consecutively. Needed for strong |
| 748 // mode. | 765 // mode. |
| 749 int class_declaration_group_start_; | 766 int class_declaration_group_start_; |
| 750 }; | 767 }; |
| 751 | 768 |
| 752 } } // namespace v8::internal | 769 } } // namespace v8::internal |
| 753 | 770 |
| 754 #endif // V8_SCOPES_H_ | 771 #endif // V8_SCOPES_H_ |
| OLD | NEW |