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
3 // found in the LICENSE file.
9 #include "src/pending-compilation-error-handler.h"
17 // A hash map to support fast variable declaration and lookup.
18 class VariableMap: public ZoneHashMap {
20 explicit VariableMap(Zone* zone);
22 virtual ~VariableMap();
24 Variable* Declare(Scope* scope, const AstRawString* name, VariableMode mode,
25 Variable::Kind kind, InitializationFlag initialization_flag,
26 MaybeAssignedFlag maybe_assigned_flag = kNotAssigned,
27 int declaration_group_start = -1);
29 Variable* Lookup(const AstRawString* name);
31 Zone* zone() const { return zone_; }
38 // The dynamic scope part holds hash maps for the variables that will
39 // be looked up dynamically from within eval and with scopes. The objects
40 // are allocated on-demand from Scope::NonLocal to avoid wasting memory
41 // and setup time for scopes that don't need them.
42 class DynamicScopePart : public ZoneObject {
44 explicit DynamicScopePart(Zone* zone) {
45 for (int i = 0; i < 3; i++)
46 maps_[i] = new(zone->New(sizeof(VariableMap))) VariableMap(zone);
49 VariableMap* GetMap(VariableMode mode) {
50 int index = mode - DYNAMIC;
51 DCHECK(index >= 0 && index < 3);
56 VariableMap *maps_[3];
60 // Sloppy block-scoped function declarations to var-bind
61 class SloppyBlockFunctionMap : public ZoneHashMap {
63 explicit SloppyBlockFunctionMap(Zone* zone);
65 virtual ~SloppyBlockFunctionMap();
67 void Declare(const AstRawString* name,
68 SloppyBlockFunctionStatement* statement);
70 typedef ZoneVector<SloppyBlockFunctionStatement*> Vector;
77 // Global invariants after AST construction: Each reference (i.e. identifier)
78 // to a JavaScript variable (including global properties) is represented by a
79 // VariableProxy node. Immediately after AST construction and before variable
80 // allocation, most VariableProxy nodes are "unresolved", i.e. not bound to a
81 // corresponding variable (though some are bound during parse time). Variable
82 // allocation binds each unresolved VariableProxy to one Variable and assigns
83 // a location. Note that many VariableProxy nodes may refer to the same Java-
86 class Scope: public ZoneObject {
88 // ---------------------------------------------------------------------------
91 Scope(Zone* zone, Scope* outer_scope, ScopeType scope_type,
92 AstValueFactory* value_factory,
93 FunctionKind function_kind = kNormalFunction);
95 // Compute top scope and allocate variables. For lazy compilation the top
96 // scope only contains the single lazily compiled function, so this
97 // doesn't re-allocate variables repeatedly.
98 static bool Analyze(ParseInfo* info);
100 static Scope* DeserializeScopeChain(Isolate* isolate, Zone* zone,
101 Context* context, Scope* script_scope);
103 // The scope name is only used for printing/debugging.
104 void SetScopeName(const AstRawString* scope_name) {
105 scope_name_ = scope_name;
110 // Checks if the block scope is redundant, i.e. it does not contain any
111 // block scoped declarations. In that case it is removed from the scope
112 // tree and its children are reparented.
113 Scope* FinalizeBlockScope();
115 Zone* zone() const { return zone_; }
117 // ---------------------------------------------------------------------------
120 // Lookup a variable in this scope. Returns the variable or NULL if not found.
121 Variable* LookupLocal(const AstRawString* name);
123 // This lookup corresponds to a lookup in the "intermediate" scope sitting
124 // between this scope and the outer scope. (ECMA-262, 3rd., requires that
125 // the name of named function literal is kept in an intermediate scope
126 // in between this scope and the next outer scope.)
127 Variable* LookupFunctionVar(const AstRawString* name,
128 AstNodeFactory* factory);
130 // Lookup a variable in this scope or outer scopes.
131 // Returns the variable or NULL if not found.
132 Variable* Lookup(const AstRawString* name);
134 // Declare the function variable for a function literal. This variable
135 // is in an intermediate scope between this function scope and the the
136 // outer scope. Only possible for function scopes; at most one variable.
137 void DeclareFunctionVar(VariableDeclaration* declaration) {
138 DCHECK(is_function_scope());
139 // Handle implicit declaration of the function name in named function
140 // expressions before other declarations.
141 decls_.InsertAt(0, declaration, zone());
142 function_ = declaration;
145 // Declare a parameter in this scope. When there are duplicated
146 // parameters the rightmost one 'wins'. However, the implementation
147 // expects all parameters to be declared and from left to right.
148 Variable* DeclareParameter(
149 const AstRawString* name, VariableMode mode,
150 bool is_optional, bool is_rest, bool* is_duplicate);
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(const AstRawString* name, VariableMode mode,
155 InitializationFlag init_flag, Variable::Kind kind,
156 MaybeAssignedFlag maybe_assigned_flag = kNotAssigned,
157 int declaration_group_start = -1);
159 // Declare an implicit global variable in this scope which must be a
160 // script 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(const AstRawString* name);
165 // Create a new unresolved variable.
166 VariableProxy* NewUnresolved(AstNodeFactory* factory,
167 const AstRawString* name,
168 Variable::Kind kind = Variable::NORMAL,
169 int start_position = RelocInfo::kNoPosition,
170 int end_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 DCHECK(!already_resolved());
175 VariableProxy* proxy =
176 factory->NewVariableProxy(name, kind, start_position, end_position);
177 unresolved_.Add(proxy, zone_);
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);
189 // Creates a new temporary variable in this scope's TemporaryScope. The
190 // name is only used for printing and cannot be used to find the variable.
191 // In particular, the only way to get hold of the temporary is by keeping the
192 // Variable* around. The name should not clash with a legitimate variable
194 Variable* NewTemporary(const AstRawString* name);
196 // Adds the specific declaration node to the list of declarations in
197 // this scope. The declarations are processed as part of entering
198 // the scope; see codegen.cc:ProcessDeclarations.
199 void AddDeclaration(Declaration* declaration);
201 // ---------------------------------------------------------------------------
202 // Illegal redeclaration support.
204 // Set an expression node that will be executed when the scope is
205 // entered. We only keep track of one illegal redeclaration node per
206 // scope - the first one - so if you try to set it multiple times
207 // the additional requests will be silently ignored.
208 void SetIllegalRedeclaration(Expression* expression);
210 // Retrieve the illegal redeclaration expression. Do not call if the
211 // scope doesn't have an illegal redeclaration node.
212 Expression* GetIllegalRedeclaration();
214 // Check if the scope has (at least) one illegal redeclaration.
215 bool HasIllegalRedeclaration() const { return illegal_redecl_ != NULL; }
217 // For harmony block scoping mode: Check if the scope has conflicting var
218 // declarations, i.e. a var declaration that has been hoisted from a nested
219 // scope over a let binding of the same name.
220 Declaration* CheckConflictingVarDeclarations();
222 // ---------------------------------------------------------------------------
223 // Scope-specific info.
225 // Inform the scope that the corresponding code contains a with statement.
226 void RecordWithStatement() { scope_contains_with_ = true; }
228 // Inform the scope that the corresponding code contains an eval call.
229 void RecordEvalCall() { if (!is_script_scope()) scope_calls_eval_ = true; }
231 // Inform the scope that the corresponding code uses "arguments".
232 void RecordArgumentsUsage() { scope_uses_arguments_ = true; }
234 // Inform the scope that the corresponding code uses "super".
235 void RecordSuperPropertyUsage() { scope_uses_super_property_ = true; }
237 // Set the language mode flag (unless disabled by a global flag).
238 void SetLanguageMode(LanguageMode language_mode) {
239 language_mode_ = language_mode;
242 // Set the ASM module flag.
243 void SetAsmModule() { asm_module_ = true; }
245 // Inform the scope that the scope may execute declarations nonlinearly.
246 // Currently, the only nonlinear scope is a switch statement. The name is
247 // more general in case something else comes up with similar control flow,
248 // for example the ability to break out of something which does not have
249 // its own lexical scope.
250 // The bit does not need to be stored on the ScopeInfo because none of
251 // the three compilers will perform hole check elimination on a variable
252 // located in VariableLocation::CONTEXT. So, direct eval and closures
253 // will not expose holes.
254 void SetNonlinear() { scope_nonlinear_ = true; }
256 // Position in the source where this scope begins and ends.
258 // * For the scope of a with statement
260 // start position: start position of first token of 'stmt'
261 // end position: end position of last token of 'stmt'
262 // * For the scope of a block
264 // start position: start position of '{'
265 // end position: end position of '}'
266 // * For the scope of a function literal or decalaration
267 // function fun(a,b) { stmts }
268 // start position: start position of '('
269 // end position: end position of '}'
270 // * For the scope of a catch block
271 // try { stms } catch(e) { stmts }
272 // start position: start position of '('
273 // end position: end position of ')'
274 // * For the scope of a for-statement
275 // for (let x ...) stmt
276 // start position: start position of '('
277 // end position: end position of last token of 'stmt'
278 // * For the scope of a switch statement
279 // switch (tag) { cases }
280 // start position: start position of '{'
281 // end position: end position of '}'
282 int start_position() const { return start_position_; }
283 void set_start_position(int statement_pos) {
284 start_position_ = statement_pos;
286 int end_position() const { return end_position_; }
287 void set_end_position(int statement_pos) {
288 end_position_ = statement_pos;
291 // In some cases we want to force context allocation for a whole scope.
292 void ForceContextAllocation() {
293 DCHECK(!already_resolved());
294 force_context_allocation_ = true;
296 bool has_forced_context_allocation() const {
297 return force_context_allocation_;
300 // ---------------------------------------------------------------------------
303 // Specific scope types.
304 bool is_eval_scope() const { return scope_type_ == EVAL_SCOPE; }
305 bool is_function_scope() const {
306 return scope_type_ == FUNCTION_SCOPE || scope_type_ == ARROW_SCOPE;
308 bool is_module_scope() const { return scope_type_ == MODULE_SCOPE; }
309 bool is_script_scope() const { return scope_type_ == SCRIPT_SCOPE; }
310 bool is_catch_scope() const { return scope_type_ == CATCH_SCOPE; }
311 bool is_block_scope() const { return scope_type_ == BLOCK_SCOPE; }
312 bool is_with_scope() const { return scope_type_ == WITH_SCOPE; }
313 bool is_arrow_scope() const { return scope_type_ == ARROW_SCOPE; }
314 bool is_declaration_scope() const { return is_declaration_scope_; }
316 void set_is_declaration_scope() { is_declaration_scope_ = true; }
318 // Information about which scopes calls eval.
319 bool calls_eval() const { return scope_calls_eval_; }
320 bool calls_sloppy_eval() const {
321 return scope_calls_eval_ && is_sloppy(language_mode_);
323 bool outer_scope_calls_sloppy_eval() const {
324 return outer_scope_calls_sloppy_eval_;
326 bool asm_module() const { return asm_module_; }
327 bool asm_function() const { return asm_function_; }
329 // Is this scope inside a with statement.
330 bool inside_with() const { return scope_inside_with_; }
331 // Does this scope contain a with statement.
332 bool contains_with() const { return scope_contains_with_; }
334 // Does this scope access "arguments".
335 bool uses_arguments() const { return scope_uses_arguments_; }
336 // Does any inner scope access "arguments".
337 bool inner_uses_arguments() const { return inner_scope_uses_arguments_; }
338 // Does this scope access "super" property (super.foo).
339 bool uses_super_property() const { return scope_uses_super_property_; }
340 // Does this scope have the potential to execute declarations non-linearly?
341 bool is_nonlinear() const { return scope_nonlinear_; }
343 // Whether this needs to be represented by a runtime context.
344 bool NeedsContext() const { return num_heap_slots() > 0; }
346 bool NeedsHomeObject() const {
347 return scope_uses_super_property_ ||
348 (scope_calls_eval_ && (IsConciseMethod(function_kind()) ||
349 IsAccessorFunction(function_kind()) ||
350 IsClassConstructor(function_kind())));
353 const Scope* NearestOuterEvalScope() const {
354 if (is_eval_scope()) return this;
355 if (outer_scope() == nullptr) return nullptr;
356 return outer_scope()->NearestOuterEvalScope();
359 // ---------------------------------------------------------------------------
362 // The type of this scope.
363 ScopeType scope_type() const { return scope_type_; }
365 FunctionKind function_kind() const { return function_kind_; }
367 // The language mode of this scope.
368 LanguageMode language_mode() const { return language_mode_; }
370 // The variable corresponding to the 'this' value.
371 Variable* receiver() {
372 DCHECK(has_this_declaration());
373 DCHECK_NOT_NULL(receiver_);
377 Variable* LookupThis() { return Lookup(ast_value_factory_->this_string()); }
379 // TODO(wingo): Add a GLOBAL_SCOPE scope type which will lexically allocate
380 // "this" (and no other variable) on the native context. Script scopes then
381 // will not have a "this" declaration.
382 bool has_this_declaration() const {
383 return (is_function_scope() && !is_arrow_scope()) || is_module_scope();
386 // The variable corresponding to the 'new.target' value.
387 Variable* new_target_var() { return new_target_; }
389 // The variable holding the function literal for named function
390 // literals, or NULL. Only valid for function scopes.
391 VariableDeclaration* function() const {
392 DCHECK(is_function_scope());
396 // Parameters. The left-most parameter has index 0.
397 // Only valid for function scopes.
398 Variable* parameter(int index) const {
399 DCHECK(is_function_scope());
400 return params_[index];
403 // Returns the default function arity excluding default or rest parameters.
404 int default_function_length() const { return arity_; }
406 int num_parameters() const { return params_.length(); }
408 // A function can have at most one rest parameter. Returns Variable* or NULL.
409 Variable* rest_parameter(int* index) const {
410 *index = rest_index_;
411 if (rest_index_ < 0) return NULL;
412 return rest_parameter_;
415 bool has_rest_parameter() const {
416 return rest_index_ >= 0;
419 bool has_simple_parameters() const {
420 return has_simple_parameters_;
423 // TODO(caitp): manage this state in a better way. PreParser must be able to
424 // communicate that the scope is non-simple, without allocating any parameters
425 // as the Parser does. This is necessary to ensure that TC39's proposed early
426 // error can be reported consistently regardless of whether lazily parsed or
428 void SetHasNonSimpleParameters() {
429 DCHECK(is_function_scope());
430 has_simple_parameters_ = false;
433 // Retrieve `IsSimpleParameterList` of current or outer function.
434 bool HasSimpleParameters() {
435 Scope* scope = ClosureScope();
436 return !scope->is_function_scope() || scope->has_simple_parameters();
439 // The local variable 'arguments' if we need to allocate it; NULL otherwise.
440 Variable* arguments() const {
441 DCHECK(!is_arrow_scope() || arguments_ == nullptr);
445 Variable* this_function_var() const {
446 // This is only used in derived constructors atm.
447 DCHECK(this_function_ == nullptr ||
448 (is_function_scope() && (IsClassConstructor(function_kind()) ||
449 IsConciseMethod(function_kind()) ||
450 IsAccessorFunction(function_kind()))));
451 return this_function_;
454 // Declarations list.
455 ZoneList<Declaration*>* declarations() { return &decls_; }
458 ZoneList<Scope*>* inner_scopes() { return &inner_scopes_; }
460 // The scope immediately surrounding this scope, or NULL.
461 Scope* outer_scope() const { return outer_scope_; }
463 // The ModuleDescriptor for this scope; only for module scopes.
464 ModuleDescriptor* module() const { return module_descriptor_; }
467 void set_class_declaration_group_start(int position) {
468 class_declaration_group_start_ = position;
471 int class_declaration_group_start() const {
472 return class_declaration_group_start_;
475 // ---------------------------------------------------------------------------
476 // Variable allocation.
478 // Collect stack and context allocated local variables in this scope. Note
479 // that the function variable - if present - is not collected and should be
480 // handled separately.
481 void CollectStackAndContextLocals(
482 ZoneList<Variable*>* stack_locals, ZoneList<Variable*>* context_locals,
483 ZoneList<Variable*>* context_globals,
484 ZoneList<Variable*>* strong_mode_free_variables = nullptr);
486 // Current number of var or const locals.
487 int num_var_or_const() { return num_var_or_const_; }
489 // Result of variable allocation.
490 int num_stack_slots() const { return num_stack_slots_; }
491 int num_heap_slots() const { return num_heap_slots_; }
492 int num_global_slots() const { return num_global_slots_; }
494 int StackLocalCount() const;
495 int ContextLocalCount() const;
496 int ContextGlobalCount() const;
498 // For script scopes, the number of module literals (including nested ones).
499 int num_modules() const { return num_modules_; }
501 // For module scopes, the host scope's internal variable binding this module.
502 Variable* module_var() const { return module_var_; }
504 // Make sure this scope and all outer scopes are eagerly compiled.
505 void ForceEagerCompilation() { force_eager_compilation_ = true; }
507 // Determine if we can parse a function literal in this scope lazily.
508 bool AllowsLazyParsing() const;
510 // Determine if we can use lazy compilation for this scope.
511 bool AllowsLazyCompilation() const;
513 // Determine if we can use lazy compilation for this scope without a context.
514 bool AllowsLazyCompilationWithoutContext() const;
516 // True if the outer context of this scope is always the native context.
517 bool HasTrivialOuterContext() const;
519 // The number of contexts between this and scope; zero if this == scope.
520 int ContextChainLength(Scope* scope);
522 // Find the first function, script, eval or (declaration) block scope. This is
523 // the scope where var declarations will be hoisted to in the implementation.
524 Scope* DeclarationScope();
526 // Find the first non-block declaration scope. This should be either a script,
527 // function, or eval scope. Same as DeclarationScope(), but skips
528 // declaration "block" scopes. Used for differentiating associated
529 // function objects (i.e., the scope for which a function prologue allocates
530 // a context) or declaring temporaries.
531 Scope* ClosureScope();
533 // Find the first (non-arrow) function or script scope. This is where
534 // 'this' is bound, and what determines the function kind.
535 Scope* ReceiverScope();
537 Handle<ScopeInfo> GetScopeInfo(Isolate* isolate);
539 // Get the chain of nested scopes within this scope for the source statement
540 // position. The scopes will be added to the list from the outermost scope to
541 // the innermost scope. Only nested block, catch or with scopes are tracked
542 // and will be returned, but no inner function scopes.
543 void GetNestedScopeChain(Isolate* isolate, List<Handle<ScopeInfo> >* chain,
544 int statement_position);
546 // ---------------------------------------------------------------------------
547 // Strict mode support.
548 bool IsDeclared(const AstRawString* name) {
549 // During formal parameter list parsing the scope only contains
550 // two variables inserted at initialization: "this" and "arguments".
551 // "this" is an invalid parameter name and "arguments" is invalid parameter
552 // name in strict mode. Therefore looking up with the map which includes
553 // "this" and "arguments" in addition to all formal parameters is safe.
554 return variables_.Lookup(name) != NULL;
557 bool IsDeclaredParameter(const AstRawString* name) {
558 // If IsSimpleParameterList is false, duplicate parameters are not allowed,
559 // however `arguments` may be allowed if function is not strict code. Thus,
560 // the assumptions explained above do not hold.
561 return params_.Contains(variables_.Lookup(name));
564 SloppyBlockFunctionMap* sloppy_block_function_map() {
565 return &sloppy_block_function_map_;
569 void ReportMessage(int start_position, int end_position,
570 MessageTemplate::Template message,
571 const AstRawString* arg);
573 // ---------------------------------------------------------------------------
577 void Print(int n = 0); // n = indentation; n < 0 => don't print recursively
580 // ---------------------------------------------------------------------------
583 friend class ParserFactory;
586 Scope* outer_scope_; // the immediately enclosing outer scope, or NULL
587 ZoneList<Scope*> inner_scopes_; // the immediately enclosed inner scopes
590 ScopeType scope_type_;
591 // If the scope is a function scope, this is the function kind.
592 FunctionKind function_kind_;
594 // Debugging support.
595 const AstRawString* scope_name_;
597 // The variables declared in this scope:
599 // All user-declared variables (incl. parameters). For script scopes
600 // variables may be implicitly 'declared' by being used (possibly in
601 // an inner scope) with no intervening with statements or eval calls.
602 VariableMap variables_;
603 // Compiler-allocated (user-invisible) temporaries.
604 ZoneList<Variable*> temps_;
605 // Parameter list in source order.
606 ZoneList<Variable*> params_;
607 // Variables that must be looked up dynamically.
608 DynamicScopePart* dynamics_;
609 // Unresolved variables referred to from this scope.
610 ZoneList<VariableProxy*> unresolved_;
612 ZoneList<Declaration*> decls_;
613 // Convenience variable.
615 // Function variable, if any; function scopes only.
616 VariableDeclaration* function_;
617 // new.target variable, function scopes only.
618 Variable* new_target_;
619 // Convenience variable; function scopes only.
620 Variable* arguments_;
621 // Convenience variable; Subclass constructor only
622 Variable* this_function_;
623 // Module descriptor; module scopes only.
624 ModuleDescriptor* module_descriptor_;
626 // Map of function names to lists of functions defined in sloppy blocks
627 SloppyBlockFunctionMap sloppy_block_function_map_;
629 // Illegal redeclaration.
630 Expression* illegal_redecl_;
632 // Scope-specific information computed during parsing.
634 // This scope is inside a 'with' of some outer scope.
635 bool scope_inside_with_;
636 // This scope contains a 'with' statement.
637 bool scope_contains_with_;
638 // This scope or a nested catch scope or with scope contain an 'eval' call. At
639 // the 'eval' call site this scope is the declaration scope.
640 bool scope_calls_eval_;
641 // This scope uses "arguments".
642 bool scope_uses_arguments_;
643 // This scope uses "super" property ('super.foo').
644 bool scope_uses_super_property_;
645 // This scope contains an "use asm" annotation.
647 // This scope's outer context is an asm module.
649 // This scope's declarations might not be executed in order (e.g., switch).
650 bool scope_nonlinear_;
651 // The language mode of this scope.
652 LanguageMode language_mode_;
657 // Computed via PropagateScopeInfo.
658 bool outer_scope_calls_sloppy_eval_;
659 bool inner_scope_calls_eval_;
660 bool inner_scope_uses_arguments_;
661 bool force_eager_compilation_;
662 bool force_context_allocation_;
664 // True if it doesn't need scope resolution (e.g., if the scope was
665 // constructed based on a serialized scope info or a catch context).
666 bool already_resolved_;
668 // True if it holds 'var' declarations.
669 bool is_declaration_scope_;
671 // Computed as variables are declared.
672 int num_var_or_const_;
674 // Computed via AllocateVariables; function, block and catch scopes only.
675 int num_stack_slots_;
677 int num_global_slots_;
679 // The number of modules (including nested ones).
682 // For module scopes, the host scope's temporary variable binding this module.
683 Variable* module_var_;
685 // Info about the parameter list of a function.
687 bool has_simple_parameters_;
688 Variable* rest_parameter_;
691 // Serialized scope info support.
692 Handle<ScopeInfo> scope_info_;
693 bool already_resolved() { return already_resolved_; }
695 // Create a non-local variable with a given name.
696 // These variables are looked up dynamically at runtime.
697 Variable* NonLocal(const AstRawString* name, VariableMode mode);
699 // Variable resolution.
700 // Possible results of a recursive variable lookup telling if and how a
701 // variable is bound. These are returned in the output parameter *binding_kind
702 // of the LookupRecursive function.
704 // The variable reference could be statically resolved to a variable binding
705 // which is returned. There is no 'with' statement between the reference and
706 // the binding and no scope between the reference scope (inclusive) and
707 // binding scope (exclusive) makes a sloppy 'eval' call.
710 // The variable reference could be statically resolved to a variable binding
711 // which is returned. There is no 'with' statement between the reference and
712 // the binding, but some scope between the reference scope (inclusive) and
713 // binding scope (exclusive) makes a sloppy 'eval' call, that might
714 // possibly introduce variable bindings shadowing the found one. Thus the
715 // found variable binding is just a guess.
718 // The variable reference could not be statically resolved to any binding
719 // and thus should be considered referencing a global variable. NULL is
720 // returned. The variable reference is not inside any 'with' statement and
721 // no scope between the reference scope (inclusive) and script scope
722 // (exclusive) makes a sloppy 'eval' call.
725 // The variable reference could not be statically resolved to any binding
726 // NULL is returned. The variable reference is not inside any 'with'
727 // statement, but some scope between the reference scope (inclusive) and
728 // script scope (exclusive) makes a sloppy 'eval' call, that might
729 // possibly introduce a variable binding. Thus the reference should be
730 // considered referencing a global variable unless it is shadowed by an
731 // 'eval' introduced binding.
732 UNBOUND_EVAL_SHADOWED,
734 // The variable could not be statically resolved and needs to be looked up
735 // dynamically. NULL is returned. There are two possible reasons:
736 // * A 'with' statement has been encountered and there is no variable
737 // binding for the name between the variable reference and the 'with'.
738 // The variable potentially references a property of the 'with' object.
739 // * The code is being executed as part of a call to 'eval' and the calling
740 // context chain contains either a variable binding for the name or it
741 // contains a 'with' context.
745 // Lookup a variable reference given by name recursively starting with this
746 // scope. If the code is executed because of a call to 'eval', the context
747 // parameter should be set to the calling context of 'eval'.
748 Variable* LookupRecursive(VariableProxy* proxy, BindingKind* binding_kind,
749 AstNodeFactory* factory);
751 bool ResolveVariable(ParseInfo* info, VariableProxy* proxy,
752 AstNodeFactory* factory);
754 bool ResolveVariablesRecursively(ParseInfo* info, AstNodeFactory* factory);
756 bool CheckStrongModeDeclaration(VariableProxy* proxy, Variable* var);
758 // If this scope is a method scope of a class, return the corresponding
759 // class variable, otherwise nullptr.
760 ClassVariable* ClassVariableForMethod() const;
763 void PropagateScopeInfo(bool outer_scope_calls_sloppy_eval);
764 bool HasTrivialContext() const;
767 bool MustAllocate(Variable* var);
768 bool MustAllocateInContext(Variable* var);
769 bool HasArgumentsParameter(Isolate* isolate);
771 // Variable allocation.
772 void AllocateStackSlot(Variable* var);
773 void AllocateHeapSlot(Variable* var);
774 void AllocateParameterLocals(Isolate* isolate);
775 void AllocateNonParameterLocal(Isolate* isolate, Variable* var);
776 void AllocateDeclaredGlobal(Isolate* isolate, Variable* var);
777 void AllocateNonParameterLocalsAndDeclaredGlobals(Isolate* isolate);
778 void AllocateVariablesRecursively(Isolate* isolate);
779 void AllocateParameter(Variable* var, int index);
780 void AllocateReceiver();
781 void AllocateModules();
783 // Resolve and fill in the allocation information for all variables
784 // in this scopes. Must be called *after* all scopes have been
785 // processed (parsed) to ensure that unresolved variables can be
786 // resolved properly.
788 // In the case of code compiled and run using 'eval', the context
789 // parameter is the context in which eval was called. In all other
790 // cases the context parameter is an empty handle.
792 bool AllocateVariables(ParseInfo* info, AstNodeFactory* factory);
795 // Construct a scope based on the scope info.
796 Scope(Zone* zone, Scope* inner_scope, ScopeType type,
797 Handle<ScopeInfo> scope_info, AstValueFactory* value_factory);
799 // Construct a catch scope with a binding for the name.
800 Scope(Zone* zone, Scope* inner_scope, const AstRawString* catch_variable_name,
801 AstValueFactory* value_factory);
803 void AddInnerScope(Scope* inner_scope) {
804 if (inner_scope != NULL) {
805 inner_scopes_.Add(inner_scope, zone_);
806 inner_scope->outer_scope_ = this;
810 void SetDefaults(ScopeType type, Scope* outer_scope,
811 Handle<ScopeInfo> scope_info,
812 FunctionKind function_kind = kNormalFunction);
814 AstValueFactory* ast_value_factory_;
817 PendingCompilationErrorHandler pending_error_handler_;
819 // For tracking which classes are declared consecutively. Needed for strong
821 int class_declaration_group_start_;
824 } // namespace internal
827 #endif // V8_SCOPES_H_