• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 //     * Redistributions of source code must retain the above copyright
7 //       notice, this list of conditions and the following disclaimer.
8 //     * Redistributions in binary form must reproduce the above
9 //       copyright notice, this list of conditions and the following
10 //       disclaimer in the documentation and/or other materials provided
11 //       with the distribution.
12 //     * Neither the name of Google Inc. nor the names of its
13 //       contributors may be used to endorse or promote products derived
14 //       from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 #ifndef V8_SCOPES_H_
29 #define V8_SCOPES_H_
30 
31 #include "ast.h"
32 #include "zone.h"
33 
34 namespace v8 {
35 namespace internal {
36 
37 class CompilationInfo;
38 
39 
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   Variable* Declare(Scope* scope,
48                     Handle<String> name,
49                     VariableMode mode,
50                     bool is_valid_lhs,
51                     Variable::Kind kind,
52                     InitializationFlag initialization_flag,
53                     Interface* interface = Interface::NewValue());
54 
55   Variable* Lookup(Handle<String> name);
56 
zone()57   Zone* zone() const { return zone_; }
58 
59  private:
60   Zone* zone_;
61 };
62 
63 
64 // The dynamic scope part holds hash maps for the variables that will
65 // be looked up dynamically from within eval and with scopes. The objects
66 // are allocated on-demand from Scope::NonLocal to avoid wasting memory
67 // and setup time for scopes that don't need them.
68 class DynamicScopePart : public ZoneObject {
69  public:
DynamicScopePart(Zone * zone)70   explicit DynamicScopePart(Zone* zone) {
71     for (int i = 0; i < 3; i++)
72       maps_[i] = new(zone->New(sizeof(VariableMap))) VariableMap(zone);
73   }
74 
GetMap(VariableMode mode)75   VariableMap* GetMap(VariableMode mode) {
76     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 // Global invariants after AST construction: Each reference (i.e. identifier)
87 // to a JavaScript variable (including global properties) is represented by a
88 // VariableProxy node. Immediately after AST construction and before variable
89 // allocation, most VariableProxy nodes are "unresolved", i.e. not bound to a
90 // corresponding variable (though some are bound during parse time). Variable
91 // allocation binds each unresolved VariableProxy to one Variable and assigns
92 // a location. Note that many VariableProxy nodes may refer to the same Java-
93 // Script variable.
94 
95 class Scope: public ZoneObject {
96  public:
97   // ---------------------------------------------------------------------------
98   // Construction
99 
100   Scope(Scope* outer_scope, ScopeType scope_type, Zone* zone);
101 
102   // Compute top scope and allocate variables. For lazy compilation the top
103   // scope only contains the single lazily compiled function, so this
104   // doesn't re-allocate variables repeatedly.
105   static bool Analyze(CompilationInfo* info);
106 
107   static Scope* DeserializeScopeChain(Context* context, Scope* global_scope,
108                                       Zone* zone);
109 
110   // The scope name is only used for printing/debugging.
SetScopeName(Handle<String> scope_name)111   void SetScopeName(Handle<String> scope_name) { scope_name_ = scope_name; }
112 
113   void Initialize();
114 
115   // Checks if the block scope is redundant, i.e. it does not contain any
116   // block scoped declarations. In that case it is removed from the scope
117   // tree and its children are reparented.
118   Scope* FinalizeBlockScope();
119 
zone()120   Zone* zone() const { return zone_; }
121 
122   // ---------------------------------------------------------------------------
123   // Declarations
124 
125   // Lookup a variable in this scope. Returns the variable or NULL if not found.
126   Variable* LocalLookup(Handle<String> name);
127 
128   // This lookup corresponds to a lookup in the "intermediate" scope sitting
129   // between this scope and the outer scope. (ECMA-262, 3rd., requires that
130   // the name of named function literal is kept in an intermediate scope
131   // in between this scope and the next outer scope.)
132   Variable* LookupFunctionVar(Handle<String> name,
133                               AstNodeFactory<AstNullVisitor>* factory);
134 
135   // Lookup a variable in this scope or outer scopes.
136   // Returns the variable or NULL if not found.
137   Variable* Lookup(Handle<String> name);
138 
139   // Declare the function variable for a function literal. This variable
140   // is in an intermediate scope between this function scope and the the
141   // outer scope. Only possible for function scopes; at most one variable.
DeclareFunctionVar(VariableDeclaration * declaration)142   void DeclareFunctionVar(VariableDeclaration* declaration) {
143     ASSERT(is_function_scope());
144     function_ = declaration;
145   }
146 
147   // Declare a parameter in this scope.  When there are duplicated
148   // parameters the rightmost one 'wins'.  However, the implementation
149   // 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   Variable* DeclareLocal(Handle<String> name,
155                          VariableMode mode,
156                          InitializationFlag init_flag,
157                          Interface* interface = Interface::NewValue());
158 
159   // Declare an implicit global variable in this scope which must be a
160   // global scope.  The variable was introduced (possibly from an inner
161   // scope) by a reference to an unresolved variable with no intervening
162   // with statements or eval calls.
163   Variable* DeclareDynamicGlobal(Handle<String> name);
164 
165   // Create a new unresolved variable.
166   template<class Visitor>
167   VariableProxy* NewUnresolved(AstNodeFactory<Visitor>* factory,
168                                Handle<String> name,
169                                Interface* interface = Interface::NewValue(),
170                                int position = RelocInfo::kNoPosition) {
171     // Note that we must not share the unresolved variables with
172     // the same name because they may be removed selectively via
173     // RemoveUnresolved().
174     ASSERT(!already_resolved());
175     VariableProxy* proxy =
176         factory->NewVariableProxy(name, false, interface, position);
177     unresolved_.Add(proxy, zone_);
178     return proxy;
179   }
180 
181   // Remove a unresolved variable. During parsing, an unresolved variable
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
184   // addition introduced a new unresolved variable which may end up being
185   // allocated globally as a "ghost" variable. RemoveUnresolved removes
186   // such a variable again if it was added; otherwise this is a no-op.
187   void RemoveUnresolved(VariableProxy* var);
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 
195   // Creates a new temporary variable in this scope.  The name is only used
196   // for printing and cannot be used to find the variable.  In particular,
197   // 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   // Adds the specific declaration node to the list of declarations in
202   // this scope. The declarations are processed as part of entering
203   // the scope; see codegen.cc:ProcessDeclarations.
204   void AddDeclaration(Declaration* declaration);
205 
206   // ---------------------------------------------------------------------------
207   // Illegal redeclaration support.
208 
209   // Set an expression node that will be executed when the scope is
210   // entered. We only keep track of one illegal redeclaration node per
211   // scope - the first one - so if you try to set it multiple times
212   // the additional requests will be silently ignored.
213   void SetIllegalRedeclaration(Expression* expression);
214 
215   // Visit the illegal redeclaration expression. Do not call if the
216   // scope doesn't have an illegal redeclaration node.
217   void VisitIllegalRedeclaration(AstVisitor* visitor);
218 
219   // Check if the scope has (at least) one illegal redeclaration.
HasIllegalRedeclaration()220   bool HasIllegalRedeclaration() const { return illegal_redecl_ != NULL; }
221 
222   // For harmony block scoping mode: Check if the scope has conflicting var
223   // declarations, i.e. a var declaration that has been hoisted from a nested
224   // scope over a let binding of the same name.
225   Declaration* CheckConflictingVarDeclarations();
226 
227   // ---------------------------------------------------------------------------
228   // Scope-specific info.
229 
230   // Inform the scope that the corresponding code contains a with statement.
RecordWithStatement()231   void RecordWithStatement() { scope_contains_with_ = true; }
232 
233   // Inform the scope that the corresponding code contains an eval call.
RecordEvalCall()234   void RecordEvalCall() { if (!is_global_scope()) scope_calls_eval_ = true; }
235 
236   // Set the strict mode flag (unless disabled by a global flag).
SetLanguageMode(LanguageMode language_mode)237   void SetLanguageMode(LanguageMode language_mode) {
238     language_mode_ = language_mode;
239   }
240 
241   // Position in the source where this scope begins and ends.
242   //
243   // * For the scope of a with statement
244   //     with (obj) stmt
245   //   start position: start position of first token of 'stmt'
246   //   end position: end position of last token of 'stmt'
247   // * For the scope of a block
248   //     { stmts }
249   //   start position: start position of '{'
250   //   end position: end position of '}'
251   // * For the scope of a function literal or decalaration
252   //     function fun(a,b) { stmts }
253   //   start position: start position of '('
254   //   end position: end position of '}'
255   // * For the scope of a catch block
256   //     try { stms } catch(e) { stmts }
257   //   start position: start position of '('
258   //   end position: end position of ')'
259   // * For the scope of a for-statement
260   //     for (let x ...) stmt
261   //   start position: start position of '('
262   //   end position: end position of last token of 'stmt'
start_position()263   int start_position() const { return start_position_; }
set_start_position(int statement_pos)264   void set_start_position(int statement_pos) {
265     start_position_ = statement_pos;
266   }
end_position()267   int end_position() const { return end_position_; }
set_end_position(int statement_pos)268   void set_end_position(int statement_pos) {
269     end_position_ = statement_pos;
270   }
271 
272   // In some cases we want to force context allocation for a whole scope.
ForceContextAllocation()273   void ForceContextAllocation() {
274     ASSERT(!already_resolved());
275     force_context_allocation_ = true;
276   }
has_forced_context_allocation()277   bool has_forced_context_allocation() const {
278     return force_context_allocation_;
279   }
280 
281   // ---------------------------------------------------------------------------
282   // Predicates.
283 
284   // Specific scope types.
is_eval_scope()285   bool is_eval_scope() const { return scope_type_ == EVAL_SCOPE; }
is_function_scope()286   bool is_function_scope() const { return scope_type_ == FUNCTION_SCOPE; }
is_module_scope()287   bool is_module_scope() const { return scope_type_ == MODULE_SCOPE; }
is_global_scope()288   bool is_global_scope() const { return scope_type_ == GLOBAL_SCOPE; }
is_catch_scope()289   bool is_catch_scope() const { return scope_type_ == CATCH_SCOPE; }
is_block_scope()290   bool is_block_scope() const { return scope_type_ == BLOCK_SCOPE; }
is_with_scope()291   bool is_with_scope() const { return scope_type_ == WITH_SCOPE; }
is_declaration_scope()292   bool is_declaration_scope() const {
293     return is_eval_scope() || is_function_scope() ||
294         is_module_scope() || is_global_scope();
295   }
is_classic_mode()296   bool is_classic_mode() const {
297     return language_mode() == CLASSIC_MODE;
298   }
is_extended_mode()299   bool is_extended_mode() const {
300     return language_mode() == EXTENDED_MODE;
301   }
is_strict_or_extended_eval_scope()302   bool is_strict_or_extended_eval_scope() const {
303     return is_eval_scope() && !is_classic_mode();
304   }
305 
306   // Information about which scopes calls eval.
calls_eval()307   bool calls_eval() const { return scope_calls_eval_; }
calls_non_strict_eval()308   bool calls_non_strict_eval() {
309     return scope_calls_eval_ && is_classic_mode();
310   }
outer_scope_calls_non_strict_eval()311   bool outer_scope_calls_non_strict_eval() const {
312     return outer_scope_calls_non_strict_eval_;
313   }
314 
315   // Is this scope inside a with statement.
inside_with()316   bool inside_with() const { return scope_inside_with_; }
317   // Does this scope contain a with statement.
contains_with()318   bool contains_with() const { return scope_contains_with_; }
319 
320   // ---------------------------------------------------------------------------
321   // Accessors.
322 
323   // The type of this scope.
scope_type()324   ScopeType scope_type() const { return scope_type_; }
325 
326   // The language mode of this scope.
language_mode()327   LanguageMode language_mode() const { return language_mode_; }
328 
329   // The variable corresponding the 'this' value.
receiver()330   Variable* receiver() { return receiver_; }
331 
332   // The variable holding the function literal for named function
333   // literals, or NULL.  Only valid for function scopes.
function()334   VariableDeclaration* function() const {
335     ASSERT(is_function_scope());
336     return function_;
337   }
338 
339   // Parameters. The left-most parameter has index 0.
340   // Only valid for function scopes.
parameter(int index)341   Variable* parameter(int index) const {
342     ASSERT(is_function_scope());
343     return params_[index];
344   }
345 
num_parameters()346   int num_parameters() const { return params_.length(); }
347 
348   // The local variable 'arguments' if we need to allocate it; NULL otherwise.
arguments()349   Variable* arguments() const { return arguments_; }
350 
351   // Declarations list.
declarations()352   ZoneList<Declaration*>* declarations() { return &decls_; }
353 
354   // Inner scope list.
inner_scopes()355   ZoneList<Scope*>* inner_scopes() { return &inner_scopes_; }
356 
357   // The scope immediately surrounding this scope, or NULL.
outer_scope()358   Scope* outer_scope() const { return outer_scope_; }
359 
360   // The interface as inferred so far; only for module scopes.
interface()361   Interface* interface() const { return interface_; }
362 
363   // ---------------------------------------------------------------------------
364   // Variable allocation.
365 
366   // Collect stack and context allocated local variables in this scope. Note
367   // that the function variable - if present - is not collected and should be
368   // handled separately.
369   void CollectStackAndContextLocals(ZoneList<Variable*>* stack_locals,
370                                     ZoneList<Variable*>* context_locals);
371 
372   // Current number of var or const locals.
num_var_or_const()373   int num_var_or_const() { return num_var_or_const_; }
374 
375   // Result of variable allocation.
num_stack_slots()376   int num_stack_slots() const { return num_stack_slots_; }
num_heap_slots()377   int num_heap_slots() const { return num_heap_slots_; }
378 
379   int StackLocalCount() const;
380   int ContextLocalCount() const;
381 
382   // For global scopes, the number of module literals (including nested ones).
num_modules()383   int num_modules() const { return num_modules_; }
384 
385   // For module scopes, the host scope's internal variable binding this module.
module_var()386   Variable* module_var() const { return module_var_; }
387 
388   // Make sure this scope and all outer scopes are eagerly compiled.
ForceEagerCompilation()389   void ForceEagerCompilation()  { force_eager_compilation_ = true; }
390 
391   // Determine if we can use lazy compilation for this scope.
392   bool AllowsLazyCompilation() const;
393 
394   // Determine if we can use lazy compilation for this scope without a context.
395   bool AllowsLazyCompilationWithoutContext() const;
396 
397   // True if the outer context of this scope is always the native context.
398   bool HasTrivialOuterContext() const;
399 
400   // True if the outer context allows lazy compilation of this scope.
401   bool HasLazyCompilableOuterContext() const;
402 
403   // The number of contexts between this and scope; zero if this == scope.
404   int ContextChainLength(Scope* scope);
405 
406   // Find the innermost global scope.
407   Scope* GlobalScope();
408 
409   // Find the first function, global, or eval scope.  This is the scope
410   // where var declarations will be hoisted to in the implementation.
411   Scope* DeclarationScope();
412 
413   Handle<ScopeInfo> GetScopeInfo();
414 
415   // Get the chain of nested scopes within this scope for the source statement
416   // position. The scopes will be added to the list from the outermost scope to
417   // the innermost scope. Only nested block, catch or with scopes are tracked
418   // and will be returned, but no inner function scopes.
419   void GetNestedScopeChain(List<Handle<ScopeInfo> >* chain,
420                            int statement_position);
421 
422   // ---------------------------------------------------------------------------
423   // Strict mode support.
IsDeclared(Handle<String> name)424   bool IsDeclared(Handle<String> name) {
425     // During formal parameter list parsing the scope only contains
426     // two variables inserted at initialization: "this" and "arguments".
427     // "this" is an invalid parameter name and "arguments" is invalid parameter
428     // name in strict mode. Therefore looking up with the map which includes
429     // "this" and "arguments" in addition to all formal parameters is safe.
430     return variables_.Lookup(name) != NULL;
431   }
432 
433   // ---------------------------------------------------------------------------
434   // Debugging.
435 
436 #ifdef DEBUG
437   void Print(int n = 0);  // n = indentation; n < 0 => don't print recursively
438 #endif
439 
440   // ---------------------------------------------------------------------------
441   // Implementation.
442  protected:
443   friend class ParserFactory;
444 
445   Isolate* const isolate_;
446 
447   // Scope tree.
448   Scope* outer_scope_;  // the immediately enclosing outer scope, or NULL
449   ZoneList<Scope*> inner_scopes_;  // the immediately enclosed inner scopes
450 
451   // The scope type.
452   ScopeType scope_type_;
453 
454   // Debugging support.
455   Handle<String> scope_name_;
456 
457   // The variables declared in this scope:
458   //
459   // All user-declared variables (incl. parameters).  For global scopes
460   // variables may be implicitly 'declared' by being used (possibly in
461   // an inner scope) with no intervening with statements or eval calls.
462   VariableMap variables_;
463   // Compiler-allocated (user-invisible) internals.
464   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   // Interface; module scopes only.
482   Interface* interface_;
483 
484   // Illegal redeclaration.
485   Expression* illegal_redecl_;
486 
487   // Scope-specific information computed during parsing.
488   //
489   // This scope is inside a 'with' of some outer scope.
490   bool scope_inside_with_;
491   // This scope contains a 'with' statement.
492   bool scope_contains_with_;
493   // This scope or a nested catch scope or with scope contain an 'eval' call. At
494   // the 'eval' call site this scope is the declaration scope.
495   bool scope_calls_eval_;
496   // The language mode of this scope.
497   LanguageMode language_mode_;
498   // Source positions.
499   int start_position_;
500   int end_position_;
501 
502   // Computed via PropagateScopeInfo.
503   bool outer_scope_calls_non_strict_eval_;
504   bool inner_scope_calls_eval_;
505   bool force_eager_compilation_;
506   bool force_context_allocation_;
507 
508   // True if it doesn't need scope resolution (e.g., if the scope was
509   // constructed based on a serialized scope info or a catch context).
510   bool already_resolved_;
511 
512   // Computed as variables are declared.
513   int num_var_or_const_;
514 
515   // Computed via AllocateVariables; function, block and catch scopes only.
516   int num_stack_slots_;
517   int num_heap_slots_;
518 
519   // The number of modules (including nested ones).
520   int num_modules_;
521 
522   // For module scopes, the host scope's internal variable binding this module.
523   Variable* module_var_;
524 
525   // Serialized scope info support.
526   Handle<ScopeInfo> scope_info_;
already_resolved()527   bool already_resolved() { return already_resolved_; }
528 
529   // Create a non-local variable with a given name.
530   // These variables are looked up dynamically at runtime.
531   Variable* NonLocal(Handle<String> name, VariableMode mode);
532 
533   // Variable resolution.
534   // Possible results of a recursive variable lookup telling if and how a
535   // variable is bound. These are returned in the output parameter *binding_kind
536   // of the LookupRecursive function.
537   enum BindingKind {
538     // The variable reference could be statically resolved to a variable binding
539     // which is returned. There is no 'with' statement between the reference and
540     // the binding and no scope between the reference scope (inclusive) and
541     // binding scope (exclusive) makes a non-strict 'eval' call.
542     BOUND,
543 
544     // The variable reference could be statically resolved to a variable binding
545     // which is returned. There is no 'with' statement between the reference and
546     // the binding, but some scope between the reference scope (inclusive) and
547     // binding scope (exclusive) makes a non-strict 'eval' call, that might
548     // possibly introduce variable bindings shadowing the found one. Thus the
549     // found variable binding is just a guess.
550     BOUND_EVAL_SHADOWED,
551 
552     // The variable reference could not be statically resolved to any binding
553     // and thus should be considered referencing a global variable. NULL is
554     // returned. The variable reference is not inside any 'with' statement and
555     // no scope between the reference scope (inclusive) and global scope
556     // (exclusive) makes a non-strict 'eval' call.
557     UNBOUND,
558 
559     // The variable reference could not be statically resolved to any binding
560     // NULL is returned. The variable reference is not inside any 'with'
561     // statement, but some scope between the reference scope (inclusive) and
562     // global scope (exclusive) makes a non-strict 'eval' call, that might
563     // possibly introduce a variable binding. Thus the reference should be
564     // considered referencing a global variable unless it is shadowed by an
565     // 'eval' introduced binding.
566     UNBOUND_EVAL_SHADOWED,
567 
568     // The variable could not be statically resolved and needs to be looked up
569     // dynamically. NULL is returned. There are two possible reasons:
570     // * A 'with' statement has been encountered and there is no variable
571     //   binding for the name between the variable reference and the 'with'.
572     //   The variable potentially references a property of the 'with' object.
573     // * The code is being executed as part of a call to 'eval' and the calling
574     //   context chain contains either a variable binding for the name or it
575     //   contains a 'with' context.
576     DYNAMIC_LOOKUP
577   };
578 
579   // Lookup a variable reference given by name recursively starting with this
580   // scope. If the code is executed because of a call to 'eval', the context
581   // parameter should be set to the calling context of 'eval'.
582   Variable* LookupRecursive(Handle<String> name,
583                             BindingKind* binding_kind,
584                             AstNodeFactory<AstNullVisitor>* factory);
585   MUST_USE_RESULT
586   bool ResolveVariable(CompilationInfo* info,
587                        VariableProxy* proxy,
588                        AstNodeFactory<AstNullVisitor>* factory);
589   MUST_USE_RESULT
590   bool ResolveVariablesRecursively(CompilationInfo* info,
591                                    AstNodeFactory<AstNullVisitor>* factory);
592 
593   // Scope analysis.
594   bool PropagateScopeInfo(bool outer_scope_calls_non_strict_eval);
595   bool HasTrivialContext() const;
596 
597   // Predicates.
598   bool MustAllocate(Variable* var);
599   bool MustAllocateInContext(Variable* var);
600   bool HasArgumentsParameter();
601 
602   // Variable allocation.
603   void AllocateStackSlot(Variable* var);
604   void AllocateHeapSlot(Variable* var);
605   void AllocateParameterLocals();
606   void AllocateNonParameterLocal(Variable* var);
607   void AllocateNonParameterLocals();
608   void AllocateVariablesRecursively();
609   void AllocateModulesRecursively(Scope* host_scope);
610 
611   // Resolve and fill in the allocation information for all variables
612   // in this scopes. Must be called *after* all scopes have been
613   // processed (parsed) to ensure that unresolved variables can be
614   // resolved properly.
615   //
616   // In the case of code compiled and run using 'eval', the context
617   // parameter is the context in which eval was called.  In all other
618   // cases the context parameter is an empty handle.
619   MUST_USE_RESULT
620   bool AllocateVariables(CompilationInfo* info,
621                          AstNodeFactory<AstNullVisitor>* factory);
622 
623  private:
624   // Construct a scope based on the scope info.
625   Scope(Scope* inner_scope, ScopeType type, Handle<ScopeInfo> scope_info,
626         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 
AddInnerScope(Scope * inner_scope)631   void AddInnerScope(Scope* inner_scope) {
632     if (inner_scope != NULL) {
633       inner_scopes_.Add(inner_scope, zone_);
634       inner_scope->outer_scope_ = this;
635     }
636   }
637 
638   void SetDefaults(ScopeType type,
639                    Scope* outer_scope,
640                    Handle<ScopeInfo> scope_info);
641 
642   Zone* zone_;
643 };
644 
645 } }  // namespace v8::internal
646 
647 #endif  // V8_SCOPES_H_
648