| OLD | NEW |
| 1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 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 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 } | 179 } |
| 180 | 180 |
| 181 // Remove a unresolved variable. During parsing, an unresolved variable | 181 // Remove a unresolved variable. During parsing, an unresolved variable |
| 182 // may have been added optimistically, but then only the variable name | 182 // may have been added optimistically, but then only the variable name |
| 183 // was used (typically for labels). If the variable was not declared, the | 183 // was used (typically for labels). If the variable was not declared, the |
| 184 // addition introduced a new unresolved variable which may end up being | 184 // addition introduced a new unresolved variable which may end up being |
| 185 // allocated globally as a "ghost" variable. RemoveUnresolved removes | 185 // allocated globally as a "ghost" variable. RemoveUnresolved removes |
| 186 // such a variable again if it was added; otherwise this is a no-op. | 186 // such a variable again if it was added; otherwise this is a no-op. |
| 187 void RemoveUnresolved(VariableProxy* var); | 187 void RemoveUnresolved(VariableProxy* var); |
| 188 | 188 |
| 189 // Creates a new internal variable in this scope. The name is only used |
| 190 // for printing and cannot be used to find the variable. In particular, |
| 191 // the only way to get hold of the temporary is by keeping the Variable* |
| 192 // around. |
| 193 Variable* NewInternal(Handle<String> name); |
| 194 |
| 189 // Creates a new temporary variable in this scope. The name is only used | 195 // Creates a new temporary variable in this scope. The name is only used |
| 190 // for printing and cannot be used to find the variable. In particular, | 196 // for printing and cannot be used to find the variable. In particular, |
| 191 // the only way to get hold of the temporary is by keeping the Variable* | 197 // the only way to get hold of the temporary is by keeping the Variable* |
| 192 // around. The name should not clash with a legitimate variable names. | 198 // around. The name should not clash with a legitimate variable names. |
| 193 Variable* NewTemporary(Handle<String> name); | 199 Variable* NewTemporary(Handle<String> name); |
| 194 | 200 |
| 195 // Adds the specific declaration node to the list of declarations in | 201 // Adds the specific declaration node to the list of declarations in |
| 196 // this scope. The declarations are processed as part of entering | 202 // this scope. The declarations are processed as part of entering |
| 197 // the scope; see codegen.cc:ProcessDeclarations. | 203 // the scope; see codegen.cc:ProcessDeclarations. |
| 198 void AddDeclaration(Declaration* declaration); | 204 void AddDeclaration(Declaration* declaration); |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 362 // Current number of var or const locals. | 368 // Current number of var or const locals. |
| 363 int num_var_or_const() { return num_var_or_const_; } | 369 int num_var_or_const() { return num_var_or_const_; } |
| 364 | 370 |
| 365 // Result of variable allocation. | 371 // Result of variable allocation. |
| 366 int num_stack_slots() const { return num_stack_slots_; } | 372 int num_stack_slots() const { return num_stack_slots_; } |
| 367 int num_heap_slots() const { return num_heap_slots_; } | 373 int num_heap_slots() const { return num_heap_slots_; } |
| 368 | 374 |
| 369 int StackLocalCount() const; | 375 int StackLocalCount() const; |
| 370 int ContextLocalCount() const; | 376 int ContextLocalCount() const; |
| 371 | 377 |
| 378 // For global scopes, the number of module literals (including nested ones). |
| 379 int num_modules() const { return num_modules_; } |
| 380 |
| 381 // For module scopes, the host scope's internal variable binding this module. |
| 382 Variable* module_var() const { return module_var_; } |
| 383 |
| 372 // Make sure this scope and all outer scopes are eagerly compiled. | 384 // Make sure this scope and all outer scopes are eagerly compiled. |
| 373 void ForceEagerCompilation() { force_eager_compilation_ = true; } | 385 void ForceEagerCompilation() { force_eager_compilation_ = true; } |
| 374 | 386 |
| 375 // Determine if we can use lazy compilation for this scope. | 387 // Determine if we can use lazy compilation for this scope. |
| 376 bool AllowsLazyCompilation() const; | 388 bool AllowsLazyCompilation() const; |
| 377 | 389 |
| 378 // Determine if we can use lazy compilation for this scope without a context. | 390 // Determine if we can use lazy compilation for this scope without a context. |
| 379 bool AllowsLazyCompilationWithoutContext() const; | 391 bool AllowsLazyCompilationWithoutContext() const; |
| 380 | 392 |
| 381 // True if the outer context of this scope is always the native context. | 393 // True if the outer context of this scope is always the native context. |
| 382 bool HasTrivialOuterContext() const; | 394 bool HasTrivialOuterContext() const; |
| 383 | 395 |
| 384 // True if the outer context allows lazy compilation of this scope. | 396 // True if the outer context allows lazy compilation of this scope. |
| 385 bool HasLazyCompilableOuterContext() const; | 397 bool HasLazyCompilableOuterContext() const; |
| 386 | 398 |
| 387 // The number of contexts between this and scope; zero if this == scope. | 399 // The number of contexts between this and scope; zero if this == scope. |
| 388 int ContextChainLength(Scope* scope); | 400 int ContextChainLength(Scope* scope); |
| 389 | 401 |
| 402 // Find the innermost global scope. |
| 403 Scope* GlobalScope(); |
| 404 |
| 390 // Find the first function, global, or eval scope. This is the scope | 405 // Find the first function, global, or eval scope. This is the scope |
| 391 // where var declarations will be hoisted to in the implementation. | 406 // where var declarations will be hoisted to in the implementation. |
| 392 Scope* DeclarationScope(); | 407 Scope* DeclarationScope(); |
| 393 | 408 |
| 394 Handle<ScopeInfo> GetScopeInfo(); | 409 Handle<ScopeInfo> GetScopeInfo(); |
| 395 | 410 |
| 396 // Get the chain of nested scopes within this scope for the source statement | 411 // Get the chain of nested scopes within this scope for the source statement |
| 397 // position. The scopes will be added to the list from the outermost scope to | 412 // position. The scopes will be added to the list from the outermost scope to |
| 398 // the innermost scope. Only nested block, catch or with scopes are tracked | 413 // the innermost scope. Only nested block, catch or with scopes are tracked |
| 399 // and will be returned, but no inner function scopes. | 414 // and will be returned, but no inner function scopes. |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 434 | 449 |
| 435 // Debugging support. | 450 // Debugging support. |
| 436 Handle<String> scope_name_; | 451 Handle<String> scope_name_; |
| 437 | 452 |
| 438 // The variables declared in this scope: | 453 // The variables declared in this scope: |
| 439 // | 454 // |
| 440 // All user-declared variables (incl. parameters). For global scopes | 455 // All user-declared variables (incl. parameters). For global scopes |
| 441 // variables may be implicitly 'declared' by being used (possibly in | 456 // variables may be implicitly 'declared' by being used (possibly in |
| 442 // an inner scope) with no intervening with statements or eval calls. | 457 // an inner scope) with no intervening with statements or eval calls. |
| 443 VariableMap variables_; | 458 VariableMap variables_; |
| 459 // Compiler-allocated (user-invisible) internals. |
| 460 ZoneList<Variable*> internals_; |
| 444 // Compiler-allocated (user-invisible) temporaries. | 461 // Compiler-allocated (user-invisible) temporaries. |
| 445 ZoneList<Variable*> temps_; | 462 ZoneList<Variable*> temps_; |
| 446 // Parameter list in source order. | 463 // Parameter list in source order. |
| 447 ZoneList<Variable*> params_; | 464 ZoneList<Variable*> params_; |
| 448 // Variables that must be looked up dynamically. | 465 // Variables that must be looked up dynamically. |
| 449 DynamicScopePart* dynamics_; | 466 DynamicScopePart* dynamics_; |
| 450 // Unresolved variables referred to from this scope. | 467 // Unresolved variables referred to from this scope. |
| 451 ZoneList<VariableProxy*> unresolved_; | 468 ZoneList<VariableProxy*> unresolved_; |
| 452 // Declarations. | 469 // Declarations. |
| 453 ZoneList<Declaration*> decls_; | 470 ZoneList<Declaration*> decls_; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 487 // constructed based on a serialized scope info or a catch context). | 504 // constructed based on a serialized scope info or a catch context). |
| 488 bool already_resolved_; | 505 bool already_resolved_; |
| 489 | 506 |
| 490 // Computed as variables are declared. | 507 // Computed as variables are declared. |
| 491 int num_var_or_const_; | 508 int num_var_or_const_; |
| 492 | 509 |
| 493 // Computed via AllocateVariables; function, block and catch scopes only. | 510 // Computed via AllocateVariables; function, block and catch scopes only. |
| 494 int num_stack_slots_; | 511 int num_stack_slots_; |
| 495 int num_heap_slots_; | 512 int num_heap_slots_; |
| 496 | 513 |
| 514 // The number of modules (including nested ones). |
| 515 int num_modules_; |
| 516 |
| 517 // For module scopes, the host scope's internal variable binding this module. |
| 518 Variable* module_var_; |
| 519 |
| 497 // Serialized scope info support. | 520 // Serialized scope info support. |
| 498 Handle<ScopeInfo> scope_info_; | 521 Handle<ScopeInfo> scope_info_; |
| 499 bool already_resolved() { return already_resolved_; } | 522 bool already_resolved() { return already_resolved_; } |
| 500 | 523 |
| 501 // Create a non-local variable with a given name. | 524 // Create a non-local variable with a given name. |
| 502 // These variables are looked up dynamically at runtime. | 525 // These variables are looked up dynamically at runtime. |
| 503 Variable* NonLocal(Handle<String> name, VariableMode mode); | 526 Variable* NonLocal(Handle<String> name, VariableMode mode); |
| 504 | 527 |
| 505 // Variable resolution. | 528 // Variable resolution. |
| 506 // Possible results of a recursive variable lookup telling if and how a | 529 // Possible results of a recursive variable lookup telling if and how a |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 571 bool MustAllocateInContext(Variable* var); | 594 bool MustAllocateInContext(Variable* var); |
| 572 bool HasArgumentsParameter(); | 595 bool HasArgumentsParameter(); |
| 573 | 596 |
| 574 // Variable allocation. | 597 // Variable allocation. |
| 575 void AllocateStackSlot(Variable* var); | 598 void AllocateStackSlot(Variable* var); |
| 576 void AllocateHeapSlot(Variable* var); | 599 void AllocateHeapSlot(Variable* var); |
| 577 void AllocateParameterLocals(); | 600 void AllocateParameterLocals(); |
| 578 void AllocateNonParameterLocal(Variable* var); | 601 void AllocateNonParameterLocal(Variable* var); |
| 579 void AllocateNonParameterLocals(); | 602 void AllocateNonParameterLocals(); |
| 580 void AllocateVariablesRecursively(); | 603 void AllocateVariablesRecursively(); |
| 604 void AllocateModulesRecursively(Scope* host_scope); |
| 581 | 605 |
| 582 // Resolve and fill in the allocation information for all variables | 606 // Resolve and fill in the allocation information for all variables |
| 583 // in this scopes. Must be called *after* all scopes have been | 607 // in this scopes. Must be called *after* all scopes have been |
| 584 // processed (parsed) to ensure that unresolved variables can be | 608 // processed (parsed) to ensure that unresolved variables can be |
| 585 // resolved properly. | 609 // resolved properly. |
| 586 // | 610 // |
| 587 // In the case of code compiled and run using 'eval', the context | 611 // In the case of code compiled and run using 'eval', the context |
| 588 // parameter is the context in which eval was called. In all other | 612 // parameter is the context in which eval was called. In all other |
| 589 // cases the context parameter is an empty handle. | 613 // cases the context parameter is an empty handle. |
| 590 MUST_USE_RESULT | 614 MUST_USE_RESULT |
| 591 bool AllocateVariables(CompilationInfo* info, | 615 bool AllocateVariables(CompilationInfo* info, |
| 592 AstNodeFactory<AstNullVisitor>* factory); | 616 AstNodeFactory<AstNullVisitor>* factory); |
| 593 | 617 |
| 594 // Instance objects have to be created ahead of time (before code generation) | |
| 595 // because of potentially cyclic references between them. | |
| 596 // Linking also has to be a separate stage, since populating one object may | |
| 597 // potentially require (forward) references to others. | |
| 598 void AllocateModules(CompilationInfo* info); | |
| 599 void LinkModules(CompilationInfo* info); | |
| 600 | |
| 601 private: | 618 private: |
| 602 // Construct a scope based on the scope info. | 619 // Construct a scope based on the scope info. |
| 603 Scope(Scope* inner_scope, ScopeType type, Handle<ScopeInfo> scope_info, | 620 Scope(Scope* inner_scope, ScopeType type, Handle<ScopeInfo> scope_info, |
| 604 Zone* zone); | 621 Zone* zone); |
| 605 | 622 |
| 606 // Construct a catch scope with a binding for the name. | 623 // Construct a catch scope with a binding for the name. |
| 607 Scope(Scope* inner_scope, Handle<String> catch_variable_name, Zone* zone); | 624 Scope(Scope* inner_scope, Handle<String> catch_variable_name, Zone* zone); |
| 608 | 625 |
| 609 void AddInnerScope(Scope* inner_scope) { | 626 void AddInnerScope(Scope* inner_scope) { |
| 610 if (inner_scope != NULL) { | 627 if (inner_scope != NULL) { |
| 611 inner_scopes_.Add(inner_scope, zone_); | 628 inner_scopes_.Add(inner_scope, zone_); |
| 612 inner_scope->outer_scope_ = this; | 629 inner_scope->outer_scope_ = this; |
| 613 } | 630 } |
| 614 } | 631 } |
| 615 | 632 |
| 616 void SetDefaults(ScopeType type, | 633 void SetDefaults(ScopeType type, |
| 617 Scope* outer_scope, | 634 Scope* outer_scope, |
| 618 Handle<ScopeInfo> scope_info); | 635 Handle<ScopeInfo> scope_info); |
| 619 | 636 |
| 620 Zone* zone_; | 637 Zone* zone_; |
| 621 }; | 638 }; |
| 622 | 639 |
| 623 } } // namespace v8::internal | 640 } } // namespace v8::internal |
| 624 | 641 |
| 625 #endif // V8_SCOPES_H_ | 642 #endif // V8_SCOPES_H_ |
| OLD | NEW |