scopes.h 25.3 KB
Newer Older
1
// Copyright 2012 the V8 project authors. All rights reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#ifndef V8_SCOPES_H_
#define V8_SCOPES_H_

#include "ast.h"
32
#include "zone.h"
33

34 35
namespace v8 {
namespace internal {
36

37 38 39
class CompilationInfo;


40
// A hash map to support fast variable declaration and lookup.
41
class VariableMap: public ZoneHashMap {
42
 public:
43
  explicit VariableMap(Zone* zone);
44

45
  virtual ~VariableMap();
46

47 48
  Variable* Declare(Scope* scope,
                    Handle<String> name,
49
                    VariableMode mode,
50
                    bool is_valid_lhs,
51
                    Variable::Kind kind,
52 53
                    InitializationFlag initialization_flag,
                    Interface* interface = Interface::NewValue());
54 55

  Variable* Lookup(Handle<String> name);
56 57 58 59 60

  Zone* zone() const { return zone_; }

 private:
  Zone* zone_;
61 62 63
};


64 65 66 67 68 69
// The dynamic scope part holds hash maps for the variables that will
// be looked up dynamically from within eval and with scopes. The objects
// are allocated on-demand from Scope::NonLocal to avoid wasting memory
// and setup time for scopes that don't need them.
class DynamicScopePart : public ZoneObject {
 public:
70 71 72 73 74
  explicit DynamicScopePart(Zone* zone) {
    for (int i = 0; i < 3; i++)
      maps_[i] = new(zone->New(sizeof(VariableMap))) VariableMap(zone);
  }

75 76
  VariableMap* GetMap(VariableMode mode) {
    int index = mode - DYNAMIC;
77
    ASSERT(index >= 0 && index < 3);
78
    return maps_[index];
79 80 81
  }

 private:
82
  VariableMap *maps_[3];
83 84 85
};


86 87 88 89 90 91 92 93 94 95 96 97 98 99
// Global invariants after AST construction: Each reference (i.e. identifier)
// to a JavaScript variable (including global properties) is represented by a
// VariableProxy node. Immediately after AST construction and before variable
// allocation, most VariableProxy nodes are "unresolved", i.e. not bound to a
// corresponding variable (though some are bound during parse time). Variable
// allocation binds each unresolved VariableProxy to one Variable and assigns
// a location. Note that many VariableProxy nodes may refer to the same Java-
// Script variable.

class Scope: public ZoneObject {
 public:
  // ---------------------------------------------------------------------------
  // Construction

100
  Scope(Scope* outer_scope, ScopeType scope_type, Zone* zone);
101

102 103 104 105 106
  // Compute top scope and allocate variables. For lazy compilation the top
  // scope only contains the single lazily compiled function, so this
  // doesn't re-allocate variables repeatedly.
  static bool Analyze(CompilationInfo* info);

107 108
  static Scope* DeserializeScopeChain(Context* context, Scope* global_scope,
                                      Zone* zone);
109

110
  // The scope name is only used for printing/debugging.
111
  void SetScopeName(Handle<String> scope_name) { scope_name_ = scope_name; }
112

113
  void Initialize();
114

115 116 117 118 119
  // Checks if the block scope is redundant, i.e. it does not contain any
  // block scoped declarations. In that case it is removed from the scope
  // tree and its children are reparented.
  Scope* FinalizeBlockScope();

120 121
  Zone* zone() const { return zone_; }

122 123 124 125
  // ---------------------------------------------------------------------------
  // Declarations

  // Lookup a variable in this scope. Returns the variable or NULL if not found.
126
  Variable* LocalLookup(Handle<String> name);
127

128 129 130 131
  // This lookup corresponds to a lookup in the "intermediate" scope sitting
  // between this scope and the outer scope. (ECMA-262, 3rd., requires that
  // the name of named function literal is kept in an intermediate scope
  // in between this scope and the next outer scope.)
132 133
  Variable* LookupFunctionVar(Handle<String> name,
                              AstNodeFactory<AstNullVisitor>* factory);
134

135 136
  // Lookup a variable in this scope or outer scopes.
  // Returns the variable or NULL if not found.
137
  Variable* Lookup(Handle<String> name);
138 139 140 141

  // Declare the function variable for a function literal. This variable
  // is in an intermediate scope between this function scope and the the
  // outer scope. Only possible for function scopes; at most one variable.
142 143 144
  void DeclareFunctionVar(VariableDeclaration* declaration) {
    ASSERT(is_function_scope());
    function_ = declaration;
145
  }
146

147 148 149
  // Declare a parameter in this scope.  When there are duplicated
  // parameters the rightmost one 'wins'.  However, the implementation
  // expects all parameters to be declared and from left to right.
150
  void DeclareParameter(Handle<String> name, VariableMode mode);
151

152
  // Declare a local variable in this scope. If the variable has been
153
  // declared before, the previously declared variable is returned.
154 155
  Variable* DeclareLocal(Handle<String> name,
                         VariableMode mode,
156 157
                         InitializationFlag init_flag,
                         Interface* interface = Interface::NewValue());
158 159 160 161 162

  // Declare an implicit global variable in this scope which must be a
  // global scope.  The variable was introduced (possibly from an inner
  // scope) by a reference to an unresolved variable with no intervening
  // with statements or eval calls.
163
  Variable* DeclareDynamicGlobal(Handle<String> name);
164 165

  // Create a new unresolved variable.
166 167 168
  template<class Visitor>
  VariableProxy* NewUnresolved(AstNodeFactory<Visitor>* factory,
                               Handle<String> name,
169 170
                               Interface* interface = Interface::NewValue(),
                               int position = RelocInfo::kNoPosition) {
171 172 173 174
    // Note that we must not share the unresolved variables with
    // the same name because they may be removed selectively via
    // RemoveUnresolved().
    ASSERT(!already_resolved());
175
    VariableProxy* proxy =
176
        factory->NewVariableProxy(name, false, interface, position);
177
    unresolved_.Add(proxy, zone_);
178 179
    return proxy;
  }
180 181 182 183 184 185 186 187 188

  // Remove a unresolved variable. During parsing, an unresolved variable
  // may have been added optimistically, but then only the variable name
  // was used (typically for labels). If the variable was not declared, the
  // addition introduced a new unresolved variable which may end up being
  // allocated globally as a "ghost" variable. RemoveUnresolved removes
  // such a variable again if it was added; otherwise this is a no-op.
  void RemoveUnresolved(VariableProxy* var);

189 190 191 192 193 194
  // Creates a new internal variable in this scope.  The name is only used
  // for printing and cannot be used to find the variable.  In particular,
  // the only way to get hold of the temporary is by keeping the Variable*
  // around.
  Variable* NewInternal(Handle<String> name);

195 196 197
  // Creates a new temporary variable in this scope.  The name is only used
  // for printing and cannot be used to find the variable.  In particular,
  // the only way to get hold of the temporary is by keeping the Variable*
198
  // around.  The name should not clash with a legitimate variable names.
199
  Variable* NewTemporary(Handle<String> name);
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216

  // Adds the specific declaration node to the list of declarations in
  // this scope. The declarations are processed as part of entering
  // the scope; see codegen.cc:ProcessDeclarations.
  void AddDeclaration(Declaration* declaration);

  // ---------------------------------------------------------------------------
  // Illegal redeclaration support.

  // Set an expression node that will be executed when the scope is
  // entered. We only keep track of one illegal redeclaration node per
  // scope - the first one - so if you try to set it multiple times
  // the additional requests will be silently ignored.
  void SetIllegalRedeclaration(Expression* expression);

  // Visit the illegal redeclaration expression. Do not call if the
  // scope doesn't have an illegal redeclaration node.
217
  void VisitIllegalRedeclaration(AstVisitor* visitor);
218 219 220 221

  // Check if the scope has (at least) one illegal redeclaration.
  bool HasIllegalRedeclaration() const { return illegal_redecl_ != NULL; }

222 223 224 225
  // For harmony block scoping mode: Check if the scope has conflicting var
  // declarations, i.e. a var declaration that has been hoisted from a nested
  // scope over a let binding of the same name.
  Declaration* CheckConflictingVarDeclarations();
226 227 228 229 230

  // ---------------------------------------------------------------------------
  // Scope-specific info.

  // Inform the scope that the corresponding code contains a with statement.
231
  void RecordWithStatement() { scope_contains_with_ = true; }
232 233

  // Inform the scope that the corresponding code contains an eval call.
234
  void RecordEvalCall() { if (!is_global_scope()) scope_calls_eval_ = true; }
235

236
  // Set the strict mode flag (unless disabled by a global flag).
237 238
  void SetLanguageMode(LanguageMode language_mode) {
    language_mode_ = language_mode;
239
  }
240

241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
  // Position in the source where this scope begins and ends.
  //
  // * For the scope of a with statement
  //     with (obj) stmt
  //   start position: start position of first token of 'stmt'
  //   end position: end position of last token of 'stmt'
  // * For the scope of a block
  //     { stmts }
  //   start position: start position of '{'
  //   end position: end position of '}'
  // * For the scope of a function literal or decalaration
  //     function fun(a,b) { stmts }
  //   start position: start position of '('
  //   end position: end position of '}'
  // * For the scope of a catch block
  //     try { stms } catch(e) { stmts }
  //   start position: start position of '('
  //   end position: end position of ')'
  // * For the scope of a for-statement
  //     for (let x ...) stmt
  //   start position: start position of '('
  //   end position: end position of last token of 'stmt'
  int start_position() const { return start_position_; }
  void set_start_position(int statement_pos) {
    start_position_ = statement_pos;
  }
  int end_position() const { return end_position_; }
  void set_end_position(int statement_pos) {
    end_position_ = statement_pos;
  }

272 273 274 275 276 277 278 279 280
  // In some cases we want to force context allocation for a whole scope.
  void ForceContextAllocation() {
    ASSERT(!already_resolved());
    force_context_allocation_ = true;
  }
  bool has_forced_context_allocation() const {
    return force_context_allocation_;
  }

281 282 283 284
  // ---------------------------------------------------------------------------
  // Predicates.

  // Specific scope types.
285 286 287 288 289 290 291
  bool is_eval_scope() const { return scope_type_ == EVAL_SCOPE; }
  bool is_function_scope() const { return scope_type_ == FUNCTION_SCOPE; }
  bool is_module_scope() const { return scope_type_ == MODULE_SCOPE; }
  bool is_global_scope() const { return scope_type_ == GLOBAL_SCOPE; }
  bool is_catch_scope() const { return scope_type_ == CATCH_SCOPE; }
  bool is_block_scope() const { return scope_type_ == BLOCK_SCOPE; }
  bool is_with_scope() const { return scope_type_ == WITH_SCOPE; }
292
  bool is_declaration_scope() const {
293 294
    return is_eval_scope() || is_function_scope() ||
        is_module_scope() || is_global_scope();
295
  }
296 297 298 299 300 301 302 303
  bool is_classic_mode() const {
    return language_mode() == CLASSIC_MODE;
  }
  bool is_extended_mode() const {
    return language_mode() == EXTENDED_MODE;
  }
  bool is_strict_or_extended_eval_scope() const {
    return is_eval_scope() && !is_classic_mode();
304
  }
305

306
  // Information about which scopes calls eval.
307
  bool calls_eval() const { return scope_calls_eval_; }
308
  bool calls_non_strict_eval() {
309
    return scope_calls_eval_ && is_classic_mode();
310
  }
311 312 313
  bool outer_scope_calls_non_strict_eval() const {
    return outer_scope_calls_non_strict_eval_;
  }
314

315
  // Is this scope inside a with statement.
316
  bool inside_with() const { return scope_inside_with_; }
317
  // Does this scope contain a with statement.
318
  bool contains_with() const { return scope_contains_with_; }
319

320 321 322
  // ---------------------------------------------------------------------------
  // Accessors.

323
  // The type of this scope.
324
  ScopeType scope_type() const { return scope_type_; }
325

326 327
  // The language mode of this scope.
  LanguageMode language_mode() const { return language_mode_; }
328

329 330
  // The variable corresponding the 'this' value.
  Variable* receiver() { return receiver_; }
331 332

  // The variable holding the function literal for named function
333 334
  // literals, or NULL.  Only valid for function scopes.
  VariableDeclaration* function() const {
335 336 337 338 339 340
    ASSERT(is_function_scope());
    return function_;
  }

  // Parameters. The left-most parameter has index 0.
  // Only valid for function scopes.
341
  Variable* parameter(int index) const {
342 343 344 345
    ASSERT(is_function_scope());
    return params_[index];
  }

346
  int num_parameters() const { return params_.length(); }
347 348

  // The local variable 'arguments' if we need to allocate it; NULL otherwise.
349
  Variable* arguments() const { return arguments_; }
350 351 352 353

  // Declarations list.
  ZoneList<Declaration*>* declarations() { return &decls_; }

354 355
  // Inner scope list.
  ZoneList<Scope*>* inner_scopes() { return &inner_scopes_; }
356

357 358 359 360 361 362
  // The scope immediately surrounding this scope, or NULL.
  Scope* outer_scope() const { return outer_scope_; }

  // The interface as inferred so far; only for module scopes.
  Interface* interface() const { return interface_; }

363 364 365
  // ---------------------------------------------------------------------------
  // Variable allocation.

366 367 368 369 370
  // Collect stack and context allocated local variables in this scope. Note
  // that the function variable - if present - is not collected and should be
  // handled separately.
  void CollectStackAndContextLocals(ZoneList<Variable*>* stack_locals,
                                    ZoneList<Variable*>* context_locals);
371

372 373 374
  // Current number of var or const locals.
  int num_var_or_const() { return num_var_or_const_; }

375
  // Result of variable allocation.
376 377
  int num_stack_slots() const { return num_stack_slots_; }
  int num_heap_slots() const { return num_heap_slots_; }
378

379 380 381
  int StackLocalCount() const;
  int ContextLocalCount() const;

382 383 384 385 386 387
  // For global scopes, the number of module literals (including nested ones).
  int num_modules() const { return num_modules_; }

  // For module scopes, the host scope's internal variable binding this module.
  Variable* module_var() const { return module_var_; }

388 389 390 391 392 393
  // Make sure this scope and all outer scopes are eagerly compiled.
  void ForceEagerCompilation()  { force_eager_compilation_ = true; }

  // Determine if we can use lazy compilation for this scope.
  bool AllowsLazyCompilation() const;

394 395
  // Determine if we can use lazy compilation for this scope without a context.
  bool AllowsLazyCompilationWithoutContext() const;
396

397
  // True if the outer context of this scope is always the native context.
398
  bool HasTrivialOuterContext() const;
399

400 401
  // True if the outer context allows lazy compilation of this scope.
  bool HasLazyCompilableOuterContext() const;
402

403 404 405
  // The number of contexts between this and scope; zero if this == scope.
  int ContextChainLength(Scope* scope);

406 407 408
  // Find the innermost global scope.
  Scope* GlobalScope();

409 410 411 412
  // Find the first function, global, or eval scope.  This is the scope
  // where var declarations will be hoisted to in the implementation.
  Scope* DeclarationScope();

413
  Handle<ScopeInfo> GetScopeInfo();
414

415 416 417 418
  // Get the chain of nested scopes within this scope for the source statement
  // position. The scopes will be added to the list from the outermost scope to
  // the innermost scope. Only nested block, catch or with scopes are tracked
  // and will be returned, but no inner function scopes.
419
  void GetNestedScopeChain(List<Handle<ScopeInfo> >* chain,
420 421
                           int statement_position);

422 423 424 425 426 427 428 429 430 431 432
  // ---------------------------------------------------------------------------
  // Strict mode support.
  bool IsDeclared(Handle<String> name) {
    // During formal parameter list parsing the scope only contains
    // two variables inserted at initialization: "this" and "arguments".
    // "this" is an invalid parameter name and "arguments" is invalid parameter
    // name in strict mode. Therefore looking up with the map which includes
    // "this" and "arguments" in addition to all formal parameters is safe.
    return variables_.Lookup(name) != NULL;
  }

433 434 435 436 437 438 439 440 441 442 443 444
  // ---------------------------------------------------------------------------
  // Debugging.

#ifdef DEBUG
  void Print(int n = 0);  // n = indentation; n < 0 => don't print recursively
#endif

  // ---------------------------------------------------------------------------
  // Implementation.
 protected:
  friend class ParserFactory;

445 446
  Isolate* const isolate_;

447 448 449 450 451
  // Scope tree.
  Scope* outer_scope_;  // the immediately enclosing outer scope, or NULL
  ZoneList<Scope*> inner_scopes_;  // the immediately enclosed inner scopes

  // The scope type.
452
  ScopeType scope_type_;
453 454 455 456 457

  // Debugging support.
  Handle<String> scope_name_;

  // The variables declared in this scope:
458 459 460 461 462
  //
  // All user-declared variables (incl. parameters).  For global scopes
  // variables may be implicitly 'declared' by being used (possibly in
  // an inner scope) with no intervening with statements or eval calls.
  VariableMap variables_;
463 464
  // Compiler-allocated (user-invisible) internals.
  ZoneList<Variable*> internals_;
465
  // Compiler-allocated (user-invisible) temporaries.
466
  ZoneList<Variable*> temps_;
467
  // Parameter list in source order.
468
  ZoneList<Variable*> params_;
469
  // Variables that must be looked up dynamically.
470
  DynamicScopePart* dynamics_;
471
  // Unresolved variables referred to from this scope.
472
  ZoneList<VariableProxy*> unresolved_;
473
  // Declarations.
474
  ZoneList<Declaration*> decls_;
475
  // Convenience variable.
476
  Variable* receiver_;
477
  // Function variable, if any; function scopes only.
478
  VariableDeclaration* function_;
479
  // Convenience variable; function scopes only.
480
  Variable* arguments_;
481 482
  // Interface; module scopes only.
  Interface* interface_;
483 484 485 486

  // Illegal redeclaration.
  Expression* illegal_redecl_;

487 488 489 490 491 492 493 494 495
  // Scope-specific information computed during parsing.
  //
  // This scope is inside a 'with' of some outer scope.
  bool scope_inside_with_;
  // This scope contains a 'with' statement.
  bool scope_contains_with_;
  // This scope or a nested catch scope or with scope contain an 'eval' call. At
  // the 'eval' call site this scope is the declaration scope.
  bool scope_calls_eval_;
496 497
  // The language mode of this scope.
  LanguageMode language_mode_;
498 499 500
  // Source positions.
  int start_position_;
  int end_position_;
501 502

  // Computed via PropagateScopeInfo.
503
  bool outer_scope_calls_non_strict_eval_;
504 505
  bool inner_scope_calls_eval_;
  bool force_eager_compilation_;
506
  bool force_context_allocation_;
507

508 509 510 511
  // True if it doesn't need scope resolution (e.g., if the scope was
  // constructed based on a serialized scope info or a catch context).
  bool already_resolved_;

512 513 514
  // Computed as variables are declared.
  int num_var_or_const_;

515
  // Computed via AllocateVariables; function, block and catch scopes only.
516 517 518
  int num_stack_slots_;
  int num_heap_slots_;

519 520 521 522 523 524
  // The number of modules (including nested ones).
  int num_modules_;

  // For module scopes, the host scope's internal variable binding this module.
  Variable* module_var_;

525 526
  // Serialized scope info support.
  Handle<ScopeInfo> scope_info_;
527
  bool already_resolved() { return already_resolved_; }
528

529 530
  // Create a non-local variable with a given name.
  // These variables are looked up dynamically at runtime.
531
  Variable* NonLocal(Handle<String> name, VariableMode mode);
532 533

  // Variable resolution.
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
  // Possible results of a recursive variable lookup telling if and how a
  // variable is bound. These are returned in the output parameter *binding_kind
  // of the LookupRecursive function.
  enum BindingKind {
    // The variable reference could be statically resolved to a variable binding
    // which is returned. There is no 'with' statement between the reference and
    // the binding and no scope between the reference scope (inclusive) and
    // binding scope (exclusive) makes a non-strict 'eval' call.
    BOUND,

    // The variable reference could be statically resolved to a variable binding
    // which is returned. There is no 'with' statement between the reference and
    // the binding, but some scope between the reference scope (inclusive) and
    // binding scope (exclusive) makes a non-strict 'eval' call, that might
    // possibly introduce variable bindings shadowing the found one. Thus the
    // found variable binding is just a guess.
    BOUND_EVAL_SHADOWED,

    // The variable reference could not be statically resolved to any binding
    // and thus should be considered referencing a global variable. NULL is
    // returned. The variable reference is not inside any 'with' statement and
    // no scope between the reference scope (inclusive) and global scope
    // (exclusive) makes a non-strict 'eval' call.
    UNBOUND,

    // The variable reference could not be statically resolved to any binding
    // NULL is returned. The variable reference is not inside any 'with'
    // statement, but some scope between the reference scope (inclusive) and
    // global scope (exclusive) makes a non-strict 'eval' call, that might
    // possibly introduce a variable binding. Thus the reference should be
    // considered referencing a global variable unless it is shadowed by an
    // 'eval' introduced binding.
    UNBOUND_EVAL_SHADOWED,

    // The variable could not be statically resolved and needs to be looked up
    // dynamically. NULL is returned. There are two possible reasons:
    // * A 'with' statement has been encountered and there is no variable
    //   binding for the name between the variable reference and the 'with'.
    //   The variable potentially references a property of the 'with' object.
    // * The code is being executed as part of a call to 'eval' and the calling
    //   context chain contains either a variable binding for the name or it
    //   contains a 'with' context.
    DYNAMIC_LOOKUP
  };

  // Lookup a variable reference given by name recursively starting with this
  // scope. If the code is executed because of a call to 'eval', the context
  // parameter should be set to the calling context of 'eval'.
582
  Variable* LookupRecursive(Handle<String> name,
583 584
                            BindingKind* binding_kind,
                            AstNodeFactory<AstNullVisitor>* factory);
585 586
  MUST_USE_RESULT
  bool ResolveVariable(CompilationInfo* info,
587 588
                       VariableProxy* proxy,
                       AstNodeFactory<AstNullVisitor>* factory);
589 590
  MUST_USE_RESULT
  bool ResolveVariablesRecursively(CompilationInfo* info,
591
                                   AstNodeFactory<AstNullVisitor>* factory);
592 593

  // Scope analysis.
594
  bool PropagateScopeInfo(bool outer_scope_calls_non_strict_eval);
595 596 597 598 599 600 601 602 603 604 605 606 607 608
  bool HasTrivialContext() const;

  // Predicates.
  bool MustAllocate(Variable* var);
  bool MustAllocateInContext(Variable* var);
  bool HasArgumentsParameter();

  // Variable allocation.
  void AllocateStackSlot(Variable* var);
  void AllocateHeapSlot(Variable* var);
  void AllocateParameterLocals();
  void AllocateNonParameterLocal(Variable* var);
  void AllocateNonParameterLocals();
  void AllocateVariablesRecursively();
609
  void AllocateModulesRecursively(Scope* host_scope);
610

611 612 613 614 615 616 617 618 619 620 621 622
  // Resolve and fill in the allocation information for all variables
  // in this scopes. Must be called *after* all scopes have been
  // processed (parsed) to ensure that unresolved variables can be
  // resolved properly.
  //
  // In the case of code compiled and run using 'eval', the context
  // parameter is the context in which eval was called.  In all other
  // cases the context parameter is an empty handle.
  MUST_USE_RESULT
  bool AllocateVariables(CompilationInfo* info,
                         AstNodeFactory<AstNullVisitor>* factory);

623
 private:
624
  // Construct a scope based on the scope info.
625 626
  Scope(Scope* inner_scope, ScopeType type, Handle<ScopeInfo> scope_info,
        Zone* zone);
627

628
  // Construct a catch scope with a binding for the name.
629
  Scope(Scope* inner_scope, Handle<String> catch_variable_name, Zone* zone);
630

631 632
  void AddInnerScope(Scope* inner_scope) {
    if (inner_scope != NULL) {
633
      inner_scopes_.Add(inner_scope, zone_);
634 635 636 637
      inner_scope->outer_scope_ = this;
    }
  }

638
  void SetDefaults(ScopeType type,
639
                   Scope* outer_scope,
640
                   Handle<ScopeInfo> scope_info);
641 642

  Zone* zone_;
643 644 645 646 647
};

} }  // namespace v8::internal

#endif  // V8_SCOPES_H_