blob: 523a251fae2ffd18f5bb1148a2c17b575b6faf8b [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
190 // ---------------------------------------------------------------------------
191 // Scope-specific info.
192
193 // Inform the scope that the corresponding code contains a with statement.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100194 void RecordWithStatement() { scope_contains_with_ = true; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000195
196 // Inform the scope that the corresponding code contains an eval call.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000197 void RecordEvalCall() { if (!is_global_scope()) scope_calls_eval_ = true; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000198
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000199 // Set the strict mode flag (unless disabled by a global flag).
200 void SetLanguageMode(LanguageMode language_mode) {
201 language_mode_ = language_mode;
202 }
203
204 // Position in the source where this scope begins and ends.
205 //
206 // * For the scope of a with statement
207 // with (obj) stmt
208 // start position: start position of first token of 'stmt'
209 // end position: end position of last token of 'stmt'
210 // * For the scope of a block
211 // { stmts }
212 // start position: start position of '{'
213 // end position: end position of '}'
214 // * For the scope of a function literal or decalaration
215 // function fun(a,b) { stmts }
216 // start position: start position of '('
217 // end position: end position of '}'
218 // * For the scope of a catch block
219 // try { stms } catch(e) { stmts }
220 // start position: start position of '('
221 // end position: end position of ')'
222 // * For the scope of a for-statement
223 // for (let x ...) stmt
224 // start position: start position of '('
225 // end position: end position of last token of 'stmt'
226 int start_position() const { return start_position_; }
227 void set_start_position(int statement_pos) {
228 start_position_ = statement_pos;
229 }
230 int end_position() const { return end_position_; }
231 void set_end_position(int statement_pos) {
232 end_position_ = statement_pos;
Steve Block44f0eee2011-05-26 01:26:41 +0100233 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000234
235 // ---------------------------------------------------------------------------
236 // Predicates.
237
238 // Specific scope types.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100239 bool is_eval_scope() const { return type_ == EVAL_SCOPE; }
240 bool is_function_scope() const { return type_ == FUNCTION_SCOPE; }
241 bool is_global_scope() const { return type_ == GLOBAL_SCOPE; }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000242 bool is_catch_scope() const { return type_ == CATCH_SCOPE; }
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000243 bool is_block_scope() const { return type_ == BLOCK_SCOPE; }
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000244 bool is_with_scope() const { return type_ == WITH_SCOPE; }
245 bool is_declaration_scope() const {
246 return is_eval_scope() || is_function_scope() || is_global_scope();
247 }
248 bool is_classic_mode() const {
249 return language_mode() == CLASSIC_MODE;
250 }
251 bool is_extended_mode() const {
252 return language_mode() == EXTENDED_MODE;
253 }
254 bool is_strict_or_extended_eval_scope() const {
255 return is_eval_scope() && !is_classic_mode();
Ben Murdoch257744e2011-11-30 15:57:28 +0000256 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000257
258 // Information about which scopes calls eval.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100259 bool calls_eval() const { return scope_calls_eval_; }
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000260 bool calls_non_strict_eval() {
261 return scope_calls_eval_ && is_classic_mode();
262 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000263 bool outer_scope_calls_non_strict_eval() const {
264 return outer_scope_calls_non_strict_eval_;
265 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000266
267 // Is this scope inside a with statement.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100268 bool inside_with() const { return scope_inside_with_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000269 // Does this scope contain a with statement.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100270 bool contains_with() const { return scope_contains_with_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000271
272 // The scope immediately surrounding this scope, or NULL.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100273 Scope* outer_scope() const { return outer_scope_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000274
275 // ---------------------------------------------------------------------------
276 // Accessors.
277
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000278 // The type of this scope.
279 ScopeType type() const { return type_; }
280
281 // The language mode of this scope.
282 LanguageMode language_mode() const { return language_mode_; }
283
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000284 // The variable corresponding the 'this' value.
285 Variable* receiver() { return receiver_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000286
287 // The variable holding the function literal for named function
288 // literals, or NULL.
289 // Only valid for function scopes.
Ben Murdoch589d6972011-11-30 16:04:58 +0000290 VariableProxy* function() const {
Steve Blocka7e24c12009-10-30 11:49:00 +0000291 ASSERT(is_function_scope());
292 return function_;
293 }
294
295 // Parameters. The left-most parameter has index 0.
296 // Only valid for function scopes.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100297 Variable* parameter(int index) const {
Steve Blocka7e24c12009-10-30 11:49:00 +0000298 ASSERT(is_function_scope());
299 return params_[index];
300 }
301
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100302 int num_parameters() const { return params_.length(); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000303
304 // The local variable 'arguments' if we need to allocate it; NULL otherwise.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100305 Variable* arguments() const { return arguments_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000306
Steve Blocka7e24c12009-10-30 11:49:00 +0000307 // Declarations list.
308 ZoneList<Declaration*>* declarations() { return &decls_; }
309
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000310 // Inner scope list.
311 ZoneList<Scope*>* inner_scopes() { return &inner_scopes_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000312
Steve Blocka7e24c12009-10-30 11:49:00 +0000313 // ---------------------------------------------------------------------------
314 // Variable allocation.
315
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000316 // Collect stack and context allocated local variables in this scope. Note
317 // that the function variable - if present - is not collected and should be
318 // handled separately.
319 void CollectStackAndContextLocals(ZoneList<Variable*>* stack_locals,
320 ZoneList<Variable*>* context_locals);
Steve Blocka7e24c12009-10-30 11:49:00 +0000321
322 // Resolve and fill in the allocation information for all variables
323 // in this scopes. Must be called *after* all scopes have been
324 // processed (parsed) to ensure that unresolved variables can be
325 // resolved properly.
326 //
327 // In the case of code compiled and run using 'eval', the context
328 // parameter is the context in which eval was called. In all other
329 // cases the context parameter is an empty handle.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000330 void AllocateVariables(Scope* global_scope);
Steve Blocka7e24c12009-10-30 11:49:00 +0000331
Steve Block053d10c2011-06-13 19:13:29 +0100332 // Current number of var or const locals.
333 int num_var_or_const() { return num_var_or_const_; }
334
Steve Blocka7e24c12009-10-30 11:49:00 +0000335 // Result of variable allocation.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100336 int num_stack_slots() const { return num_stack_slots_; }
337 int num_heap_slots() const { return num_heap_slots_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000338
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000339 int StackLocalCount() const;
340 int ContextLocalCount() const;
341
Steve Blocka7e24c12009-10-30 11:49:00 +0000342 // Make sure this scope and all outer scopes are eagerly compiled.
343 void ForceEagerCompilation() { force_eager_compilation_ = true; }
344
345 // Determine if we can use lazy compilation for this scope.
346 bool AllowsLazyCompilation() const;
347
348 // True if the outer context of this scope is always the global context.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000349 bool HasTrivialOuterContext() const;
Steve Blocka7e24c12009-10-30 11:49:00 +0000350
351 // The number of contexts between this and scope; zero if this == scope.
352 int ContextChainLength(Scope* scope);
353
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000354 // Find the first function, global, or eval scope. This is the scope
355 // where var declarations will be hoisted to in the implementation.
356 Scope* DeclarationScope();
357
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000358 Handle<ScopeInfo> GetScopeInfo();
359
360 // Get the chain of nested scopes within this scope for the source statement
361 // position. The scopes will be added to the list from the outermost scope to
362 // the innermost scope. Only nested block, catch or with scopes are tracked
363 // and will be returned, but no inner function scopes.
364 void GetNestedScopeChain(List<Handle<ScopeInfo> >* chain,
365 int statement_position);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000366
Steve Blocka7e24c12009-10-30 11:49:00 +0000367 // ---------------------------------------------------------------------------
Steve Block1e0659c2011-05-24 12:43:12 +0100368 // Strict mode support.
369 bool IsDeclared(Handle<String> name) {
370 // During formal parameter list parsing the scope only contains
371 // two variables inserted at initialization: "this" and "arguments".
372 // "this" is an invalid parameter name and "arguments" is invalid parameter
373 // name in strict mode. Therefore looking up with the map which includes
374 // "this" and "arguments" in addition to all formal parameters is safe.
375 return variables_.Lookup(name) != NULL;
376 }
377
378 // ---------------------------------------------------------------------------
Steve Blocka7e24c12009-10-30 11:49:00 +0000379 // Debugging.
380
381#ifdef DEBUG
382 void Print(int n = 0); // n = indentation; n < 0 => don't print recursively
383#endif
384
385 // ---------------------------------------------------------------------------
386 // Implementation.
387 protected:
388 friend class ParserFactory;
389
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000390 Isolate* const isolate_;
391
Steve Blocka7e24c12009-10-30 11:49:00 +0000392 // Scope tree.
393 Scope* outer_scope_; // the immediately enclosing outer scope, or NULL
394 ZoneList<Scope*> inner_scopes_; // the immediately enclosed inner scopes
395
396 // The scope type.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000397 ScopeType type_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000398
399 // Debugging support.
400 Handle<String> scope_name_;
401
402 // The variables declared in this scope:
403 //
404 // All user-declared variables (incl. parameters). For global scopes
405 // variables may be implicitly 'declared' by being used (possibly in
406 // an inner scope) with no intervening with statements or eval calls.
407 VariableMap variables_;
408 // Compiler-allocated (user-invisible) temporaries.
409 ZoneList<Variable*> temps_;
410 // Parameter list in source order.
411 ZoneList<Variable*> params_;
412 // Variables that must be looked up dynamically.
413 DynamicScopePart* dynamics_;
414 // Unresolved variables referred to from this scope.
415 ZoneList<VariableProxy*> unresolved_;
416 // Declarations.
417 ZoneList<Declaration*> decls_;
418 // Convenience variable.
Leon Clarkee46be812010-01-19 14:06:41 +0000419 Variable* receiver_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000420 // Function variable, if any; function scopes only.
Ben Murdoch589d6972011-11-30 16:04:58 +0000421 VariableProxy* function_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000422 // Convenience variable; function scopes only.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100423 Variable* arguments_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000424
425 // Illegal redeclaration.
426 Expression* illegal_redecl_;
427
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000428 // Scope-specific information computed during parsing.
429 //
430 // This scope is inside a 'with' of some outer scope.
431 bool scope_inside_with_;
432 // This scope contains a 'with' statement.
433 bool scope_contains_with_;
434 // This scope or a nested catch scope or with scope contain an 'eval' call. At
435 // the 'eval' call site this scope is the declaration scope.
436 bool scope_calls_eval_;
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000437 // The language mode of this scope.
438 LanguageMode language_mode_;
439 // Source positions.
440 int start_position_;
441 int end_position_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000442
443 // Computed via PropagateScopeInfo.
Ben Murdoch257744e2011-11-30 15:57:28 +0000444 bool outer_scope_calls_non_strict_eval_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000445 bool inner_scope_calls_eval_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000446 bool force_eager_compilation_;
447
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000448 // True if it doesn't need scope resolution (e.g., if the scope was
449 // constructed based on a serialized scope info or a catch context).
450 bool already_resolved_;
451
Steve Block053d10c2011-06-13 19:13:29 +0100452 // Computed as variables are declared.
453 int num_var_or_const_;
454
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000455 // Computed via AllocateVariables; function, block and catch scopes only.
Steve Blocka7e24c12009-10-30 11:49:00 +0000456 int num_stack_slots_;
457 int num_heap_slots_;
458
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000459 // Serialized scope info support.
460 Handle<ScopeInfo> scope_info_;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000461 bool already_resolved() { return already_resolved_; }
Ben Murdochb8e0da22011-05-16 14:20:40 +0100462
Steve Blocka7e24c12009-10-30 11:49:00 +0000463 // Create a non-local variable with a given name.
464 // These variables are looked up dynamically at runtime.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000465 Variable* NonLocal(Handle<String> name, VariableMode mode);
Steve Blocka7e24c12009-10-30 11:49:00 +0000466
467 // Variable resolution.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000468 // Possible results of a recursive variable lookup telling if and how a
469 // variable is bound. These are returned in the output parameter *binding_kind
470 // of the LookupRecursive function.
471 enum BindingKind {
472 // The variable reference could be statically resolved to a variable binding
473 // which is returned. There is no 'with' statement between the reference and
474 // the binding and no scope between the reference scope (inclusive) and
475 // binding scope (exclusive) makes a non-strict 'eval' call.
476 BOUND,
477
478 // The variable reference could be statically resolved to a variable binding
479 // which is returned. There is no 'with' statement between the reference and
480 // the binding, but some scope between the reference scope (inclusive) and
481 // binding scope (exclusive) makes a non-strict 'eval' call, that might
482 // possibly introduce variable bindings shadowing the found one. Thus the
483 // found variable binding is just a guess.
484 BOUND_EVAL_SHADOWED,
485
486 // The variable reference could not be statically resolved to any binding
487 // and thus should be considered referencing a global variable. NULL is
488 // returned. The variable reference is not inside any 'with' statement and
489 // no scope between the reference scope (inclusive) and global scope
490 // (exclusive) makes a non-strict 'eval' call.
491 UNBOUND,
492
493 // The variable reference could not be statically resolved to any binding
494 // NULL is returned. The variable reference is not inside any 'with'
495 // statement, but some scope between the reference scope (inclusive) and
496 // global scope (exclusive) makes a non-strict 'eval' call, that might
497 // possibly introduce a variable binding. Thus the reference should be
498 // considered referencing a global variable unless it is shadowed by an
499 // 'eval' introduced binding.
500 UNBOUND_EVAL_SHADOWED,
501
502 // The variable could not be statically resolved and needs to be looked up
503 // dynamically. NULL is returned. There are two possible reasons:
504 // * A 'with' statement has been encountered and there is no variable
505 // binding for the name between the variable reference and the 'with'.
506 // The variable potentially references a property of the 'with' object.
507 // * The code is being executed as part of a call to 'eval' and the calling
508 // context chain contains either a variable binding for the name or it
509 // contains a 'with' context.
510 DYNAMIC_LOOKUP
511 };
512
513 // Lookup a variable reference given by name recursively starting with this
514 // scope. If the code is executed because of a call to 'eval', the context
515 // parameter should be set to the calling context of 'eval'.
Steve Blocka7e24c12009-10-30 11:49:00 +0000516 Variable* LookupRecursive(Handle<String> name,
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000517 BindingKind* binding_kind);
Steve Blocka7e24c12009-10-30 11:49:00 +0000518 void ResolveVariable(Scope* global_scope,
Steve Blocka7e24c12009-10-30 11:49:00 +0000519 VariableProxy* proxy);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000520 void ResolveVariablesRecursively(Scope* global_scope);
Steve Blocka7e24c12009-10-30 11:49:00 +0000521
522 // Scope analysis.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000523 bool PropagateScopeInfo(bool outer_scope_calls_non_strict_eval);
Steve Blocka7e24c12009-10-30 11:49:00 +0000524 bool HasTrivialContext() const;
525
526 // Predicates.
527 bool MustAllocate(Variable* var);
528 bool MustAllocateInContext(Variable* var);
529 bool HasArgumentsParameter();
530
531 // Variable allocation.
532 void AllocateStackSlot(Variable* var);
533 void AllocateHeapSlot(Variable* var);
534 void AllocateParameterLocals();
535 void AllocateNonParameterLocal(Variable* var);
536 void AllocateNonParameterLocals();
537 void AllocateVariablesRecursively();
Ben Murdochb8e0da22011-05-16 14:20:40 +0100538
539 private:
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000540 // Construct a scope based on the scope info.
541 Scope(Scope* inner_scope, ScopeType type, Handle<ScopeInfo> scope_info);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100542
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000543 // Construct a catch scope with a binding for the name.
544 Scope(Scope* inner_scope, Handle<String> catch_variable_name);
545
Steve Block44f0eee2011-05-26 01:26:41 +0100546 void AddInnerScope(Scope* inner_scope) {
547 if (inner_scope != NULL) {
548 inner_scopes_.Add(inner_scope);
549 inner_scope->outer_scope_ = this;
550 }
551 }
552
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000553 void SetDefaults(ScopeType type,
Ben Murdochb8e0da22011-05-16 14:20:40 +0100554 Scope* outer_scope,
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000555 Handle<ScopeInfo> scope_info);
Steve Blocka7e24c12009-10-30 11:49:00 +0000556};
557
Steve Blocka7e24c12009-10-30 11:49:00 +0000558} } // namespace v8::internal
559
560#endif // V8_SCOPES_H_