blob: af0449e93c7dea6895f06d42cf7a95ca7624071c [file] [log] [blame]
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001// Copyright 2011 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +00002// 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 "hashmap.h"
33
34namespace v8 {
35namespace internal {
36
Ben Murdochf87a2032010-10-22 12:50:53 +010037class CompilationInfo;
38
Steve Blocka7e24c12009-10-30 11:49:00 +000039
40// A hash map to support fast variable declaration and lookup.
41class VariableMap: public HashMap {
42 public:
43 VariableMap();
44
Steve Blocka7e24c12009-10-30 11:49:00 +000045 virtual ~VariableMap();
46
47 Variable* Declare(Scope* scope,
48 Handle<String> name,
Ben Murdoch592a9fc2012-03-05 11:04:45 +000049 VariableMode mode,
Steve Blocka7e24c12009-10-30 11:49:00 +000050 bool is_valid_lhs,
Ben Murdoch592a9fc2012-03-05 11:04:45 +000051 Variable::Kind kind,
52 InitializationFlag initialization_flag);
Steve Blocka7e24c12009-10-30 11:49:00 +000053
54 Variable* Lookup(Handle<String> name);
55};
56
57
58// The dynamic scope part holds hash maps for the variables that will
59// be looked up dynamically from within eval and with scopes. The objects
60// are allocated on-demand from Scope::NonLocal to avoid wasting memory
61// and setup time for scopes that don't need them.
62class DynamicScopePart : public ZoneObject {
63 public:
Ben Murdoch592a9fc2012-03-05 11:04:45 +000064 VariableMap* GetMap(VariableMode mode) {
65 int index = mode - DYNAMIC;
Steve Blocka7e24c12009-10-30 11:49:00 +000066 ASSERT(index >= 0 && index < 3);
67 return &maps_[index];
68 }
69
70 private:
71 VariableMap maps_[3];
72};
73
74
75// Global invariants after AST construction: Each reference (i.e. identifier)
76// to a JavaScript variable (including global properties) is represented by a
77// VariableProxy node. Immediately after AST construction and before variable
78// allocation, most VariableProxy nodes are "unresolved", i.e. not bound to a
79// corresponding variable (though some are bound during parse time). Variable
80// allocation binds each unresolved VariableProxy to one Variable and assigns
81// a location. Note that many VariableProxy nodes may refer to the same Java-
82// Script variable.
83
84class Scope: public ZoneObject {
85 public:
86 // ---------------------------------------------------------------------------
87 // Construction
88
Ben Murdoch592a9fc2012-03-05 11:04:45 +000089 Scope(Scope* outer_scope, ScopeType type);
Steve Blocka7e24c12009-10-30 11:49:00 +000090
Ben Murdochf87a2032010-10-22 12:50:53 +010091 // Compute top scope and allocate variables. For lazy compilation the top
92 // scope only contains the single lazily compiled function, so this
93 // doesn't re-allocate variables repeatedly.
94 static bool Analyze(CompilationInfo* info);
95
Ben Murdoch592a9fc2012-03-05 11:04:45 +000096 static Scope* DeserializeScopeChain(Context* context, Scope* global_scope);
Steve Block44f0eee2011-05-26 01:26:41 +010097
Steve Blocka7e24c12009-10-30 11:49:00 +000098 // The scope name is only used for printing/debugging.
Ben Murdochb0fe1622011-05-05 13:52:32 +010099 void SetScopeName(Handle<String> scope_name) { scope_name_ = scope_name; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000100
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000101 void Initialize();
Steve Blocka7e24c12009-10-30 11:49:00 +0000102
Ben Murdoch589d6972011-11-30 16:04:58 +0000103 // Checks if the block scope is redundant, i.e. it does not contain any
104 // block scoped declarations. In that case it is removed from the scope
105 // tree and its children are reparented.
106 Scope* FinalizeBlockScope();
107
Steve Blocka7e24c12009-10-30 11:49:00 +0000108 // ---------------------------------------------------------------------------
109 // Declarations
110
111 // Lookup a variable in this scope. Returns the variable or NULL if not found.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000112 Variable* LocalLookup(Handle<String> name);
Steve Blocka7e24c12009-10-30 11:49:00 +0000113
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000114 // This lookup corresponds to a lookup in the "intermediate" scope sitting
115 // between this scope and the outer scope. (ECMA-262, 3rd., requires that
116 // the name of named function literal is kept in an intermediate scope
117 // in between this scope and the next outer scope.)
118 Variable* LookupFunctionVar(Handle<String> name);
119
Steve Blocka7e24c12009-10-30 11:49:00 +0000120 // Lookup a variable in this scope or outer scopes.
121 // Returns the variable or NULL if not found.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000122 Variable* Lookup(Handle<String> name);
Steve Blocka7e24c12009-10-30 11:49:00 +0000123
124 // Declare the function variable for a function literal. This variable
125 // is in an intermediate scope between this function scope and the the
126 // outer scope. Only possible for function scopes; at most one variable.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000127 Variable* DeclareFunctionVar(Handle<String> name, VariableMode mode);
Steve Blocka7e24c12009-10-30 11:49:00 +0000128
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000129 // Declare a parameter in this scope. When there are duplicated
130 // parameters the rightmost one 'wins'. However, the implementation
131 // expects all parameters to be declared and from left to right.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000132 void DeclareParameter(Handle<String> name, VariableMode mode);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000133
Steve Blocka7e24c12009-10-30 11:49:00 +0000134 // Declare a local variable in this scope. If the variable has been
135 // declared before, the previously declared variable is returned.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000136 Variable* DeclareLocal(Handle<String> name,
137 VariableMode mode,
138 InitializationFlag init_flag);
Steve Blocka7e24c12009-10-30 11:49:00 +0000139
140 // Declare an implicit global variable in this scope which must be a
141 // global scope. The variable was introduced (possibly from an inner
142 // scope) by a reference to an unresolved variable with no intervening
143 // with statements or eval calls.
144 Variable* DeclareGlobal(Handle<String> name);
145
Steve Blocka7e24c12009-10-30 11:49:00 +0000146 // Create a new unresolved variable.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000147 VariableProxy* NewUnresolved(Handle<String> name,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000148 int position = RelocInfo::kNoPosition);
Steve Blocka7e24c12009-10-30 11:49:00 +0000149
150 // Remove a unresolved variable. During parsing, an unresolved variable
151 // may have been added optimistically, but then only the variable name
152 // was used (typically for labels). If the variable was not declared, the
153 // addition introduced a new unresolved variable which may end up being
154 // allocated globally as a "ghost" variable. RemoveUnresolved removes
155 // such a variable again if it was added; otherwise this is a no-op.
156 void RemoveUnresolved(VariableProxy* var);
157
Ben Murdochb0fe1622011-05-05 13:52:32 +0100158 // Creates a new temporary variable in this scope. The name is only used
159 // for printing and cannot be used to find the variable. In particular,
160 // the only way to get hold of the temporary is by keeping the Variable*
161 // around.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000162 Variable* NewTemporary(Handle<String> name);
Steve Blocka7e24c12009-10-30 11:49:00 +0000163
164 // Adds the specific declaration node to the list of declarations in
165 // this scope. The declarations are processed as part of entering
166 // the scope; see codegen.cc:ProcessDeclarations.
167 void AddDeclaration(Declaration* declaration);
168
169 // ---------------------------------------------------------------------------
170 // Illegal redeclaration support.
171
172 // Set an expression node that will be executed when the scope is
173 // entered. We only keep track of one illegal redeclaration node per
174 // scope - the first one - so if you try to set it multiple times
175 // the additional requests will be silently ignored.
176 void SetIllegalRedeclaration(Expression* expression);
177
178 // Visit the illegal redeclaration expression. Do not call if the
179 // scope doesn't have an illegal redeclaration node.
180 void VisitIllegalRedeclaration(AstVisitor* visitor);
181
182 // Check if the scope has (at least) one illegal redeclaration.
183 bool HasIllegalRedeclaration() const { return illegal_redecl_ != NULL; }
184
Ben Murdoch589d6972011-11-30 16:04:58 +0000185 // For harmony block scoping mode: Check if the scope has conflicting var
186 // declarations, i.e. a var declaration that has been hoisted from a nested
187 // scope over a let binding of the same name.
188 Declaration* CheckConflictingVarDeclarations();
Steve Blocka7e24c12009-10-30 11:49:00 +0000189
Ben Murdochc7cc0282012-03-05 14:35:55 +0000190 // For harmony block scoping mode: Check if the scope has variable proxies
191 // that are used as lvalues and point to const variables. Assumes that scopes
192 // have been analyzed and variables been resolved.
193 VariableProxy* CheckAssignmentToConst();
194
Steve Blocka7e24c12009-10-30 11:49:00 +0000195 // ---------------------------------------------------------------------------
196 // Scope-specific info.
197
198 // Inform the scope that the corresponding code contains a with statement.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100199 void RecordWithStatement() { scope_contains_with_ = true; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000200
201 // Inform the scope that the corresponding code contains an eval call.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000202 void RecordEvalCall() { if (!is_global_scope()) scope_calls_eval_ = true; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000203
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000204 // Set the strict mode flag (unless disabled by a global flag).
205 void SetLanguageMode(LanguageMode language_mode) {
206 language_mode_ = language_mode;
207 }
208
209 // Position in the source where this scope begins and ends.
210 //
211 // * For the scope of a with statement
212 // with (obj) stmt
213 // start position: start position of first token of 'stmt'
214 // end position: end position of last token of 'stmt'
215 // * For the scope of a block
216 // { stmts }
217 // start position: start position of '{'
218 // end position: end position of '}'
219 // * For the scope of a function literal or decalaration
220 // function fun(a,b) { stmts }
221 // start position: start position of '('
222 // end position: end position of '}'
223 // * For the scope of a catch block
224 // try { stms } catch(e) { stmts }
225 // start position: start position of '('
226 // end position: end position of ')'
227 // * For the scope of a for-statement
228 // for (let x ...) stmt
229 // start position: start position of '('
230 // end position: end position of last token of 'stmt'
231 int start_position() const { return start_position_; }
232 void set_start_position(int statement_pos) {
233 start_position_ = statement_pos;
234 }
235 int end_position() const { return end_position_; }
236 void set_end_position(int statement_pos) {
237 end_position_ = statement_pos;
Steve Block44f0eee2011-05-26 01:26:41 +0100238 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000239
240 // ---------------------------------------------------------------------------
241 // Predicates.
242
243 // Specific scope types.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100244 bool is_eval_scope() const { return type_ == EVAL_SCOPE; }
245 bool is_function_scope() const { return type_ == FUNCTION_SCOPE; }
246 bool is_global_scope() const { return type_ == GLOBAL_SCOPE; }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000247 bool is_catch_scope() const { return type_ == CATCH_SCOPE; }
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000248 bool is_block_scope() const { return type_ == BLOCK_SCOPE; }
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000249 bool is_with_scope() const { return type_ == WITH_SCOPE; }
250 bool is_declaration_scope() const {
251 return is_eval_scope() || is_function_scope() || is_global_scope();
252 }
253 bool is_classic_mode() const {
254 return language_mode() == CLASSIC_MODE;
255 }
256 bool is_extended_mode() const {
257 return language_mode() == EXTENDED_MODE;
258 }
259 bool is_strict_or_extended_eval_scope() const {
260 return is_eval_scope() && !is_classic_mode();
Ben Murdoch257744e2011-11-30 15:57:28 +0000261 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000262
263 // Information about which scopes calls eval.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100264 bool calls_eval() const { return scope_calls_eval_; }
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000265 bool calls_non_strict_eval() {
266 return scope_calls_eval_ && is_classic_mode();
267 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000268 bool outer_scope_calls_non_strict_eval() const {
269 return outer_scope_calls_non_strict_eval_;
270 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000271
272 // Is this scope inside a with statement.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100273 bool inside_with() const { return scope_inside_with_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000274 // Does this scope contain a with statement.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100275 bool contains_with() const { return scope_contains_with_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000276
277 // The scope immediately surrounding this scope, or NULL.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100278 Scope* outer_scope() const { return outer_scope_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000279
280 // ---------------------------------------------------------------------------
281 // Accessors.
282
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000283 // The type of this scope.
284 ScopeType type() const { return type_; }
285
286 // The language mode of this scope.
287 LanguageMode language_mode() const { return language_mode_; }
288
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000289 // The variable corresponding the 'this' value.
290 Variable* receiver() { return receiver_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000291
292 // The variable holding the function literal for named function
293 // literals, or NULL.
294 // Only valid for function scopes.
Ben Murdoch589d6972011-11-30 16:04:58 +0000295 VariableProxy* function() const {
Steve Blocka7e24c12009-10-30 11:49:00 +0000296 ASSERT(is_function_scope());
297 return function_;
298 }
299
300 // Parameters. The left-most parameter has index 0.
301 // Only valid for function scopes.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100302 Variable* parameter(int index) const {
Steve Blocka7e24c12009-10-30 11:49:00 +0000303 ASSERT(is_function_scope());
304 return params_[index];
305 }
306
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100307 int num_parameters() const { return params_.length(); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000308
309 // The local variable 'arguments' if we need to allocate it; NULL otherwise.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100310 Variable* arguments() const { return arguments_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000311
Steve Blocka7e24c12009-10-30 11:49:00 +0000312 // Declarations list.
313 ZoneList<Declaration*>* declarations() { return &decls_; }
314
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000315 // Inner scope list.
316 ZoneList<Scope*>* inner_scopes() { return &inner_scopes_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000317
Steve Blocka7e24c12009-10-30 11:49:00 +0000318 // ---------------------------------------------------------------------------
319 // Variable allocation.
320
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000321 // Collect stack and context allocated local variables in this scope. Note
322 // that the function variable - if present - is not collected and should be
323 // handled separately.
324 void CollectStackAndContextLocals(ZoneList<Variable*>* stack_locals,
325 ZoneList<Variable*>* context_locals);
Steve Blocka7e24c12009-10-30 11:49:00 +0000326
327 // Resolve and fill in the allocation information for all variables
328 // in this scopes. Must be called *after* all scopes have been
329 // processed (parsed) to ensure that unresolved variables can be
330 // resolved properly.
331 //
332 // In the case of code compiled and run using 'eval', the context
333 // parameter is the context in which eval was called. In all other
334 // cases the context parameter is an empty handle.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000335 void AllocateVariables(Scope* global_scope);
Steve Blocka7e24c12009-10-30 11:49:00 +0000336
Steve Block053d10c2011-06-13 19:13:29 +0100337 // Current number of var or const locals.
338 int num_var_or_const() { return num_var_or_const_; }
339
Steve Blocka7e24c12009-10-30 11:49:00 +0000340 // Result of variable allocation.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100341 int num_stack_slots() const { return num_stack_slots_; }
342 int num_heap_slots() const { return num_heap_slots_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000343
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000344 int StackLocalCount() const;
345 int ContextLocalCount() const;
346
Steve Blocka7e24c12009-10-30 11:49:00 +0000347 // Make sure this scope and all outer scopes are eagerly compiled.
348 void ForceEagerCompilation() { force_eager_compilation_ = true; }
349
350 // Determine if we can use lazy compilation for this scope.
351 bool AllowsLazyCompilation() const;
352
353 // True if the outer context of this scope is always the global context.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000354 bool HasTrivialOuterContext() const;
Steve Blocka7e24c12009-10-30 11:49:00 +0000355
356 // The number of contexts between this and scope; zero if this == scope.
357 int ContextChainLength(Scope* scope);
358
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000359 // Find the first function, global, or eval scope. This is the scope
360 // where var declarations will be hoisted to in the implementation.
361 Scope* DeclarationScope();
362
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000363 Handle<ScopeInfo> GetScopeInfo();
364
365 // Get the chain of nested scopes within this scope for the source statement
366 // position. The scopes will be added to the list from the outermost scope to
367 // the innermost scope. Only nested block, catch or with scopes are tracked
368 // and will be returned, but no inner function scopes.
369 void GetNestedScopeChain(List<Handle<ScopeInfo> >* chain,
370 int statement_position);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000371
Steve Blocka7e24c12009-10-30 11:49:00 +0000372 // ---------------------------------------------------------------------------
Steve Block1e0659c2011-05-24 12:43:12 +0100373 // Strict mode support.
374 bool IsDeclared(Handle<String> name) {
375 // During formal parameter list parsing the scope only contains
376 // two variables inserted at initialization: "this" and "arguments".
377 // "this" is an invalid parameter name and "arguments" is invalid parameter
378 // name in strict mode. Therefore looking up with the map which includes
379 // "this" and "arguments" in addition to all formal parameters is safe.
380 return variables_.Lookup(name) != NULL;
381 }
382
383 // ---------------------------------------------------------------------------
Steve Blocka7e24c12009-10-30 11:49:00 +0000384 // Debugging.
385
386#ifdef DEBUG
387 void Print(int n = 0); // n = indentation; n < 0 => don't print recursively
388#endif
389
390 // ---------------------------------------------------------------------------
391 // Implementation.
392 protected:
393 friend class ParserFactory;
394
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000395 Isolate* const isolate_;
396
Steve Blocka7e24c12009-10-30 11:49:00 +0000397 // Scope tree.
398 Scope* outer_scope_; // the immediately enclosing outer scope, or NULL
399 ZoneList<Scope*> inner_scopes_; // the immediately enclosed inner scopes
400
401 // The scope type.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000402 ScopeType type_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000403
404 // Debugging support.
405 Handle<String> scope_name_;
406
407 // The variables declared in this scope:
408 //
409 // All user-declared variables (incl. parameters). For global scopes
410 // variables may be implicitly 'declared' by being used (possibly in
411 // an inner scope) with no intervening with statements or eval calls.
412 VariableMap variables_;
413 // Compiler-allocated (user-invisible) temporaries.
414 ZoneList<Variable*> temps_;
415 // Parameter list in source order.
416 ZoneList<Variable*> params_;
417 // Variables that must be looked up dynamically.
418 DynamicScopePart* dynamics_;
419 // Unresolved variables referred to from this scope.
420 ZoneList<VariableProxy*> unresolved_;
421 // Declarations.
422 ZoneList<Declaration*> decls_;
423 // Convenience variable.
Leon Clarkee46be812010-01-19 14:06:41 +0000424 Variable* receiver_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000425 // Function variable, if any; function scopes only.
Ben Murdoch589d6972011-11-30 16:04:58 +0000426 VariableProxy* function_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000427 // Convenience variable; function scopes only.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100428 Variable* arguments_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000429
430 // Illegal redeclaration.
431 Expression* illegal_redecl_;
432
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000433 // Scope-specific information computed during parsing.
434 //
435 // This scope is inside a 'with' of some outer scope.
436 bool scope_inside_with_;
437 // This scope contains a 'with' statement.
438 bool scope_contains_with_;
439 // This scope or a nested catch scope or with scope contain an 'eval' call. At
440 // the 'eval' call site this scope is the declaration scope.
441 bool scope_calls_eval_;
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000442 // The language mode of this scope.
443 LanguageMode language_mode_;
444 // Source positions.
445 int start_position_;
446 int end_position_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000447
448 // Computed via PropagateScopeInfo.
Ben Murdoch257744e2011-11-30 15:57:28 +0000449 bool outer_scope_calls_non_strict_eval_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000450 bool inner_scope_calls_eval_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000451 bool force_eager_compilation_;
452
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000453 // True if it doesn't need scope resolution (e.g., if the scope was
454 // constructed based on a serialized scope info or a catch context).
455 bool already_resolved_;
456
Steve Block053d10c2011-06-13 19:13:29 +0100457 // Computed as variables are declared.
458 int num_var_or_const_;
459
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000460 // Computed via AllocateVariables; function, block and catch scopes only.
Steve Blocka7e24c12009-10-30 11:49:00 +0000461 int num_stack_slots_;
462 int num_heap_slots_;
463
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000464 // Serialized scope info support.
465 Handle<ScopeInfo> scope_info_;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000466 bool already_resolved() { return already_resolved_; }
Ben Murdochb8e0da22011-05-16 14:20:40 +0100467
Steve Blocka7e24c12009-10-30 11:49:00 +0000468 // Create a non-local variable with a given name.
469 // These variables are looked up dynamically at runtime.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000470 Variable* NonLocal(Handle<String> name, VariableMode mode);
Steve Blocka7e24c12009-10-30 11:49:00 +0000471
472 // Variable resolution.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000473 // Possible results of a recursive variable lookup telling if and how a
474 // variable is bound. These are returned in the output parameter *binding_kind
475 // of the LookupRecursive function.
476 enum BindingKind {
477 // The variable reference could be statically resolved to a variable binding
478 // which is returned. There is no 'with' statement between the reference and
479 // the binding and no scope between the reference scope (inclusive) and
480 // binding scope (exclusive) makes a non-strict 'eval' call.
481 BOUND,
482
483 // The variable reference could be statically resolved to a variable binding
484 // which is returned. There is no 'with' statement between the reference and
485 // the binding, but some scope between the reference scope (inclusive) and
486 // binding scope (exclusive) makes a non-strict 'eval' call, that might
487 // possibly introduce variable bindings shadowing the found one. Thus the
488 // found variable binding is just a guess.
489 BOUND_EVAL_SHADOWED,
490
491 // The variable reference could not be statically resolved to any binding
492 // and thus should be considered referencing a global variable. NULL is
493 // returned. The variable reference is not inside any 'with' statement and
494 // no scope between the reference scope (inclusive) and global scope
495 // (exclusive) makes a non-strict 'eval' call.
496 UNBOUND,
497
498 // The variable reference could not be statically resolved to any binding
499 // NULL is returned. The variable reference is not inside any 'with'
500 // statement, but some scope between the reference scope (inclusive) and
501 // global scope (exclusive) makes a non-strict 'eval' call, that might
502 // possibly introduce a variable binding. Thus the reference should be
503 // considered referencing a global variable unless it is shadowed by an
504 // 'eval' introduced binding.
505 UNBOUND_EVAL_SHADOWED,
506
507 // The variable could not be statically resolved and needs to be looked up
508 // dynamically. NULL is returned. There are two possible reasons:
509 // * A 'with' statement has been encountered and there is no variable
510 // binding for the name between the variable reference and the 'with'.
511 // The variable potentially references a property of the 'with' object.
512 // * The code is being executed as part of a call to 'eval' and the calling
513 // context chain contains either a variable binding for the name or it
514 // contains a 'with' context.
515 DYNAMIC_LOOKUP
516 };
517
518 // Lookup a variable reference given by name recursively starting with this
519 // scope. If the code is executed because of a call to 'eval', the context
520 // parameter should be set to the calling context of 'eval'.
Steve Blocka7e24c12009-10-30 11:49:00 +0000521 Variable* LookupRecursive(Handle<String> name,
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000522 BindingKind* binding_kind);
Steve Blocka7e24c12009-10-30 11:49:00 +0000523 void ResolveVariable(Scope* global_scope,
Steve Blocka7e24c12009-10-30 11:49:00 +0000524 VariableProxy* proxy);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000525 void ResolveVariablesRecursively(Scope* global_scope);
Steve Blocka7e24c12009-10-30 11:49:00 +0000526
527 // Scope analysis.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000528 bool PropagateScopeInfo(bool outer_scope_calls_non_strict_eval);
Steve Blocka7e24c12009-10-30 11:49:00 +0000529 bool HasTrivialContext() const;
530
531 // Predicates.
532 bool MustAllocate(Variable* var);
533 bool MustAllocateInContext(Variable* var);
534 bool HasArgumentsParameter();
535
536 // Variable allocation.
537 void AllocateStackSlot(Variable* var);
538 void AllocateHeapSlot(Variable* var);
539 void AllocateParameterLocals();
540 void AllocateNonParameterLocal(Variable* var);
541 void AllocateNonParameterLocals();
542 void AllocateVariablesRecursively();
Ben Murdochb8e0da22011-05-16 14:20:40 +0100543
544 private:
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000545 // Construct a scope based on the scope info.
546 Scope(Scope* inner_scope, ScopeType type, Handle<ScopeInfo> scope_info);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100547
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000548 // Construct a catch scope with a binding for the name.
549 Scope(Scope* inner_scope, Handle<String> catch_variable_name);
550
Steve Block44f0eee2011-05-26 01:26:41 +0100551 void AddInnerScope(Scope* inner_scope) {
552 if (inner_scope != NULL) {
553 inner_scopes_.Add(inner_scope);
554 inner_scope->outer_scope_ = this;
555 }
556 }
557
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000558 void SetDefaults(ScopeType type,
Ben Murdochb8e0da22011-05-16 14:20:40 +0100559 Scope* outer_scope,
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000560 Handle<ScopeInfo> scope_info);
Steve Blocka7e24c12009-10-30 11:49:00 +0000561};
562
Steve Blocka7e24c12009-10-30 11:49:00 +0000563} } // namespace v8::internal
564
565#endif // V8_SCOPES_H_