blob: d315b7e5dcf806f883c9fcfe66124f23aa03c492 [file] [log] [blame]
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +01001// Copyright 2012 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"
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +010032#include "zone.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000033
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.
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +010041class VariableMap: public ZoneHashMap {
Steve Blocka7e24c12009-10-30 11:49:00 +000042 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,
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +010052 InitializationFlag initialization_flag,
53 Interface* interface = Interface::NewValue());
Steve Blocka7e24c12009-10-30 11:49:00 +000054
55 Variable* Lookup(Handle<String> name);
56};
57
58
59// The dynamic scope part holds hash maps for the variables that will
60// be looked up dynamically from within eval and with scopes. The objects
61// are allocated on-demand from Scope::NonLocal to avoid wasting memory
62// and setup time for scopes that don't need them.
63class DynamicScopePart : public ZoneObject {
64 public:
Ben Murdoch592a9fc2012-03-05 11:04:45 +000065 VariableMap* GetMap(VariableMode mode) {
66 int index = mode - DYNAMIC;
Steve Blocka7e24c12009-10-30 11:49:00 +000067 ASSERT(index >= 0 && index < 3);
68 return &maps_[index];
69 }
70
71 private:
72 VariableMap maps_[3];
73};
74
75
76// Global invariants after AST construction: Each reference (i.e. identifier)
77// to a JavaScript variable (including global properties) is represented by a
78// VariableProxy node. Immediately after AST construction and before variable
79// allocation, most VariableProxy nodes are "unresolved", i.e. not bound to a
80// corresponding variable (though some are bound during parse time). Variable
81// allocation binds each unresolved VariableProxy to one Variable and assigns
82// a location. Note that many VariableProxy nodes may refer to the same Java-
83// Script variable.
84
85class Scope: public ZoneObject {
86 public:
87 // ---------------------------------------------------------------------------
88 // Construction
89
Ben Murdoch592a9fc2012-03-05 11:04:45 +000090 Scope(Scope* outer_scope, ScopeType type);
Steve Blocka7e24c12009-10-30 11:49:00 +000091
Ben Murdochf87a2032010-10-22 12:50:53 +010092 // Compute top scope and allocate variables. For lazy compilation the top
93 // scope only contains the single lazily compiled function, so this
94 // doesn't re-allocate variables repeatedly.
95 static bool Analyze(CompilationInfo* info);
96
Ben Murdoch592a9fc2012-03-05 11:04:45 +000097 static Scope* DeserializeScopeChain(Context* context, Scope* global_scope);
Steve Block44f0eee2011-05-26 01:26:41 +010098
Steve Blocka7e24c12009-10-30 11:49:00 +000099 // The scope name is only used for printing/debugging.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100100 void SetScopeName(Handle<String> scope_name) { scope_name_ = scope_name; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000101
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000102 void Initialize();
Steve Blocka7e24c12009-10-30 11:49:00 +0000103
Ben Murdoch589d6972011-11-30 16:04:58 +0000104 // Checks if the block scope is redundant, i.e. it does not contain any
105 // block scoped declarations. In that case it is removed from the scope
106 // tree and its children are reparented.
107 Scope* FinalizeBlockScope();
108
Steve Blocka7e24c12009-10-30 11:49:00 +0000109 // ---------------------------------------------------------------------------
110 // Declarations
111
112 // Lookup a variable in this scope. Returns the variable or NULL if not found.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000113 Variable* LocalLookup(Handle<String> name);
Steve Blocka7e24c12009-10-30 11:49:00 +0000114
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000115 // This lookup corresponds to a lookup in the "intermediate" scope sitting
116 // between this scope and the outer scope. (ECMA-262, 3rd., requires that
117 // the name of named function literal is kept in an intermediate scope
118 // in between this scope and the next outer scope.)
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +0100119 Variable* LookupFunctionVar(Handle<String> name,
120 AstNodeFactory<AstNullVisitor>* factory);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000121
Steve Blocka7e24c12009-10-30 11:49:00 +0000122 // Lookup a variable in this scope or outer scopes.
123 // Returns the variable or NULL if not found.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000124 Variable* Lookup(Handle<String> name);
Steve Blocka7e24c12009-10-30 11:49:00 +0000125
126 // Declare the function variable for a function literal. This variable
127 // is in an intermediate scope between this function scope and the the
128 // outer scope. Only possible for function scopes; at most one variable.
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +0100129 template<class Visitor>
130 Variable* DeclareFunctionVar(Handle<String> name,
131 VariableMode mode,
132 AstNodeFactory<Visitor>* factory) {
133 ASSERT(is_function_scope() && function_ == NULL);
134 Variable* function_var = new Variable(
135 this, name, mode, true, Variable::NORMAL, kCreatedInitialized);
136 function_ = factory->NewVariableProxy(function_var);
137 return function_var;
138 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000139
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000140 // Declare a parameter in this scope. When there are duplicated
141 // parameters the rightmost one 'wins'. However, the implementation
142 // expects all parameters to be declared and from left to right.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000143 void DeclareParameter(Handle<String> name, VariableMode mode);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000144
Steve Blocka7e24c12009-10-30 11:49:00 +0000145 // Declare a local variable in this scope. If the variable has been
146 // declared before, the previously declared variable is returned.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000147 Variable* DeclareLocal(Handle<String> name,
148 VariableMode mode,
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +0100149 InitializationFlag init_flag,
150 Interface* interface = Interface::NewValue());
Steve Blocka7e24c12009-10-30 11:49:00 +0000151
152 // Declare an implicit global variable in this scope which must be a
153 // global scope. The variable was introduced (possibly from an inner
154 // scope) by a reference to an unresolved variable with no intervening
155 // with statements or eval calls.
156 Variable* DeclareGlobal(Handle<String> name);
157
Steve Blocka7e24c12009-10-30 11:49:00 +0000158 // Create a new unresolved variable.
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +0100159 template<class Visitor>
160 VariableProxy* NewUnresolved(AstNodeFactory<Visitor>* factory,
161 Handle<String> name,
162 int position = RelocInfo::kNoPosition,
163 Interface* interface = Interface::NewValue()) {
164 // Note that we must not share the unresolved variables with
165 // the same name because they may be removed selectively via
166 // RemoveUnresolved().
167 ASSERT(!already_resolved());
168 VariableProxy* proxy =
169 factory->NewVariableProxy(name, false, position, interface);
170 unresolved_.Add(proxy);
171 return proxy;
172 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000173
174 // Remove a unresolved variable. During parsing, an unresolved variable
175 // may have been added optimistically, but then only the variable name
176 // was used (typically for labels). If the variable was not declared, the
177 // addition introduced a new unresolved variable which may end up being
178 // allocated globally as a "ghost" variable. RemoveUnresolved removes
179 // such a variable again if it was added; otherwise this is a no-op.
180 void RemoveUnresolved(VariableProxy* var);
181
Ben Murdochb0fe1622011-05-05 13:52:32 +0100182 // Creates a new temporary variable in this scope. The name is only used
183 // for printing and cannot be used to find the variable. In particular,
184 // the only way to get hold of the temporary is by keeping the Variable*
185 // around.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000186 Variable* NewTemporary(Handle<String> name);
Steve Blocka7e24c12009-10-30 11:49:00 +0000187
188 // Adds the specific declaration node to the list of declarations in
189 // this scope. The declarations are processed as part of entering
190 // the scope; see codegen.cc:ProcessDeclarations.
191 void AddDeclaration(Declaration* declaration);
192
193 // ---------------------------------------------------------------------------
194 // Illegal redeclaration support.
195
196 // Set an expression node that will be executed when the scope is
197 // entered. We only keep track of one illegal redeclaration node per
198 // scope - the first one - so if you try to set it multiple times
199 // the additional requests will be silently ignored.
200 void SetIllegalRedeclaration(Expression* expression);
201
202 // Visit the illegal redeclaration expression. Do not call if the
203 // scope doesn't have an illegal redeclaration node.
204 void VisitIllegalRedeclaration(AstVisitor* visitor);
205
206 // Check if the scope has (at least) one illegal redeclaration.
207 bool HasIllegalRedeclaration() const { return illegal_redecl_ != NULL; }
208
Ben Murdoch589d6972011-11-30 16:04:58 +0000209 // For harmony block scoping mode: Check if the scope has conflicting var
210 // declarations, i.e. a var declaration that has been hoisted from a nested
211 // scope over a let binding of the same name.
212 Declaration* CheckConflictingVarDeclarations();
Steve Blocka7e24c12009-10-30 11:49:00 +0000213
Ben Murdochc7cc0282012-03-05 14:35:55 +0000214 // For harmony block scoping mode: Check if the scope has variable proxies
215 // that are used as lvalues and point to const variables. Assumes that scopes
216 // have been analyzed and variables been resolved.
217 VariableProxy* CheckAssignmentToConst();
218
Steve Blocka7e24c12009-10-30 11:49:00 +0000219 // ---------------------------------------------------------------------------
220 // Scope-specific info.
221
222 // Inform the scope that the corresponding code contains a with statement.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100223 void RecordWithStatement() { scope_contains_with_ = true; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000224
225 // Inform the scope that the corresponding code contains an eval call.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000226 void RecordEvalCall() { if (!is_global_scope()) scope_calls_eval_ = true; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000227
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000228 // Set the strict mode flag (unless disabled by a global flag).
229 void SetLanguageMode(LanguageMode language_mode) {
230 language_mode_ = language_mode;
231 }
232
233 // Position in the source where this scope begins and ends.
234 //
235 // * For the scope of a with statement
236 // with (obj) stmt
237 // start position: start position of first token of 'stmt'
238 // end position: end position of last token of 'stmt'
239 // * For the scope of a block
240 // { stmts }
241 // start position: start position of '{'
242 // end position: end position of '}'
243 // * For the scope of a function literal or decalaration
244 // function fun(a,b) { stmts }
245 // start position: start position of '('
246 // end position: end position of '}'
247 // * For the scope of a catch block
248 // try { stms } catch(e) { stmts }
249 // start position: start position of '('
250 // end position: end position of ')'
251 // * For the scope of a for-statement
252 // for (let x ...) stmt
253 // start position: start position of '('
254 // end position: end position of last token of 'stmt'
255 int start_position() const { return start_position_; }
256 void set_start_position(int statement_pos) {
257 start_position_ = statement_pos;
258 }
259 int end_position() const { return end_position_; }
260 void set_end_position(int statement_pos) {
261 end_position_ = statement_pos;
Steve Block44f0eee2011-05-26 01:26:41 +0100262 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000263
264 // ---------------------------------------------------------------------------
265 // Predicates.
266
267 // Specific scope types.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100268 bool is_eval_scope() const { return type_ == EVAL_SCOPE; }
269 bool is_function_scope() const { return type_ == FUNCTION_SCOPE; }
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +0100270 bool is_module_scope() const { return type_ == MODULE_SCOPE; }
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100271 bool is_global_scope() const { return type_ == GLOBAL_SCOPE; }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000272 bool is_catch_scope() const { return type_ == CATCH_SCOPE; }
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000273 bool is_block_scope() const { return type_ == BLOCK_SCOPE; }
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000274 bool is_with_scope() const { return type_ == WITH_SCOPE; }
275 bool is_declaration_scope() const {
276 return is_eval_scope() || is_function_scope() || is_global_scope();
277 }
278 bool is_classic_mode() const {
279 return language_mode() == CLASSIC_MODE;
280 }
281 bool is_extended_mode() const {
282 return language_mode() == EXTENDED_MODE;
283 }
284 bool is_strict_or_extended_eval_scope() const {
285 return is_eval_scope() && !is_classic_mode();
Ben Murdoch257744e2011-11-30 15:57:28 +0000286 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000287
288 // Information about which scopes calls eval.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100289 bool calls_eval() const { return scope_calls_eval_; }
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000290 bool calls_non_strict_eval() {
291 return scope_calls_eval_ && is_classic_mode();
292 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000293 bool outer_scope_calls_non_strict_eval() const {
294 return outer_scope_calls_non_strict_eval_;
295 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000296
297 // Is this scope inside a with statement.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100298 bool inside_with() const { return scope_inside_with_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000299 // Does this scope contain a with statement.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100300 bool contains_with() const { return scope_contains_with_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000301
Steve Blocka7e24c12009-10-30 11:49:00 +0000302 // ---------------------------------------------------------------------------
303 // Accessors.
304
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000305 // The type of this scope.
306 ScopeType type() const { return type_; }
307
308 // The language mode of this scope.
309 LanguageMode language_mode() const { return language_mode_; }
310
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000311 // The variable corresponding the 'this' value.
312 Variable* receiver() { return receiver_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000313
314 // The variable holding the function literal for named function
315 // literals, or NULL.
316 // Only valid for function scopes.
Ben Murdoch589d6972011-11-30 16:04:58 +0000317 VariableProxy* function() const {
Steve Blocka7e24c12009-10-30 11:49:00 +0000318 ASSERT(is_function_scope());
319 return function_;
320 }
321
322 // Parameters. The left-most parameter has index 0.
323 // Only valid for function scopes.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100324 Variable* parameter(int index) const {
Steve Blocka7e24c12009-10-30 11:49:00 +0000325 ASSERT(is_function_scope());
326 return params_[index];
327 }
328
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100329 int num_parameters() const { return params_.length(); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000330
331 // The local variable 'arguments' if we need to allocate it; NULL otherwise.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100332 Variable* arguments() const { return arguments_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000333
Steve Blocka7e24c12009-10-30 11:49:00 +0000334 // Declarations list.
335 ZoneList<Declaration*>* declarations() { return &decls_; }
336
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000337 // Inner scope list.
338 ZoneList<Scope*>* inner_scopes() { return &inner_scopes_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000339
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +0100340 // The scope immediately surrounding this scope, or NULL.
341 Scope* outer_scope() const { return outer_scope_; }
342
343 // The interface as inferred so far; only for module scopes.
344 Interface* interface() const { return interface_; }
345
Steve Blocka7e24c12009-10-30 11:49:00 +0000346 // ---------------------------------------------------------------------------
347 // Variable allocation.
348
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000349 // Collect stack and context allocated local variables in this scope. Note
350 // that the function variable - if present - is not collected and should be
351 // handled separately.
352 void CollectStackAndContextLocals(ZoneList<Variable*>* stack_locals,
353 ZoneList<Variable*>* context_locals);
Steve Blocka7e24c12009-10-30 11:49:00 +0000354
Steve Block053d10c2011-06-13 19:13:29 +0100355 // Current number of var or const locals.
356 int num_var_or_const() { return num_var_or_const_; }
357
Steve Blocka7e24c12009-10-30 11:49:00 +0000358 // Result of variable allocation.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100359 int num_stack_slots() const { return num_stack_slots_; }
360 int num_heap_slots() const { return num_heap_slots_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000361
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000362 int StackLocalCount() const;
363 int ContextLocalCount() const;
364
Steve Blocka7e24c12009-10-30 11:49:00 +0000365 // Make sure this scope and all outer scopes are eagerly compiled.
366 void ForceEagerCompilation() { force_eager_compilation_ = true; }
367
368 // Determine if we can use lazy compilation for this scope.
369 bool AllowsLazyCompilation() const;
370
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +0100371 // True if we can lazily recompile functions with this scope.
372 bool allows_lazy_recompilation() const {
373 return !force_eager_compilation_;
374 }
375
Steve Blocka7e24c12009-10-30 11:49:00 +0000376 // True if the outer context of this scope is always the global context.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000377 bool HasTrivialOuterContext() const;
Steve Blocka7e24c12009-10-30 11:49:00 +0000378
379 // The number of contexts between this and scope; zero if this == scope.
380 int ContextChainLength(Scope* scope);
381
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000382 // Find the first function, global, or eval scope. This is the scope
383 // where var declarations will be hoisted to in the implementation.
384 Scope* DeclarationScope();
385
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000386 Handle<ScopeInfo> GetScopeInfo();
387
388 // Get the chain of nested scopes within this scope for the source statement
389 // position. The scopes will be added to the list from the outermost scope to
390 // the innermost scope. Only nested block, catch or with scopes are tracked
391 // and will be returned, but no inner function scopes.
392 void GetNestedScopeChain(List<Handle<ScopeInfo> >* chain,
393 int statement_position);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000394
Steve Blocka7e24c12009-10-30 11:49:00 +0000395 // ---------------------------------------------------------------------------
Steve Block1e0659c2011-05-24 12:43:12 +0100396 // Strict mode support.
397 bool IsDeclared(Handle<String> name) {
398 // During formal parameter list parsing the scope only contains
399 // two variables inserted at initialization: "this" and "arguments".
400 // "this" is an invalid parameter name and "arguments" is invalid parameter
401 // name in strict mode. Therefore looking up with the map which includes
402 // "this" and "arguments" in addition to all formal parameters is safe.
403 return variables_.Lookup(name) != NULL;
404 }
405
406 // ---------------------------------------------------------------------------
Steve Blocka7e24c12009-10-30 11:49:00 +0000407 // Debugging.
408
409#ifdef DEBUG
410 void Print(int n = 0); // n = indentation; n < 0 => don't print recursively
411#endif
412
413 // ---------------------------------------------------------------------------
414 // Implementation.
415 protected:
416 friend class ParserFactory;
417
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000418 Isolate* const isolate_;
419
Steve Blocka7e24c12009-10-30 11:49:00 +0000420 // Scope tree.
421 Scope* outer_scope_; // the immediately enclosing outer scope, or NULL
422 ZoneList<Scope*> inner_scopes_; // the immediately enclosed inner scopes
423
424 // The scope type.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000425 ScopeType type_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000426
427 // Debugging support.
428 Handle<String> scope_name_;
429
430 // The variables declared in this scope:
431 //
432 // All user-declared variables (incl. parameters). For global scopes
433 // variables may be implicitly 'declared' by being used (possibly in
434 // an inner scope) with no intervening with statements or eval calls.
435 VariableMap variables_;
436 // Compiler-allocated (user-invisible) temporaries.
437 ZoneList<Variable*> temps_;
438 // Parameter list in source order.
439 ZoneList<Variable*> params_;
440 // Variables that must be looked up dynamically.
441 DynamicScopePart* dynamics_;
442 // Unresolved variables referred to from this scope.
443 ZoneList<VariableProxy*> unresolved_;
444 // Declarations.
445 ZoneList<Declaration*> decls_;
446 // Convenience variable.
Leon Clarkee46be812010-01-19 14:06:41 +0000447 Variable* receiver_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000448 // Function variable, if any; function scopes only.
Ben Murdoch589d6972011-11-30 16:04:58 +0000449 VariableProxy* function_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000450 // Convenience variable; function scopes only.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100451 Variable* arguments_;
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +0100452 // Interface; module scopes only.
453 Interface* interface_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000454
455 // Illegal redeclaration.
456 Expression* illegal_redecl_;
457
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000458 // Scope-specific information computed during parsing.
459 //
460 // This scope is inside a 'with' of some outer scope.
461 bool scope_inside_with_;
462 // This scope contains a 'with' statement.
463 bool scope_contains_with_;
464 // This scope or a nested catch scope or with scope contain an 'eval' call. At
465 // the 'eval' call site this scope is the declaration scope.
466 bool scope_calls_eval_;
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000467 // The language mode of this scope.
468 LanguageMode language_mode_;
469 // Source positions.
470 int start_position_;
471 int end_position_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000472
473 // Computed via PropagateScopeInfo.
Ben Murdoch257744e2011-11-30 15:57:28 +0000474 bool outer_scope_calls_non_strict_eval_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000475 bool inner_scope_calls_eval_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000476 bool force_eager_compilation_;
477
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000478 // True if it doesn't need scope resolution (e.g., if the scope was
479 // constructed based on a serialized scope info or a catch context).
480 bool already_resolved_;
481
Steve Block053d10c2011-06-13 19:13:29 +0100482 // Computed as variables are declared.
483 int num_var_or_const_;
484
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000485 // Computed via AllocateVariables; function, block and catch scopes only.
Steve Blocka7e24c12009-10-30 11:49:00 +0000486 int num_stack_slots_;
487 int num_heap_slots_;
488
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000489 // Serialized scope info support.
490 Handle<ScopeInfo> scope_info_;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000491 bool already_resolved() { return already_resolved_; }
Ben Murdochb8e0da22011-05-16 14:20:40 +0100492
Steve Blocka7e24c12009-10-30 11:49:00 +0000493 // Create a non-local variable with a given name.
494 // These variables are looked up dynamically at runtime.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000495 Variable* NonLocal(Handle<String> name, VariableMode mode);
Steve Blocka7e24c12009-10-30 11:49:00 +0000496
497 // Variable resolution.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000498 // Possible results of a recursive variable lookup telling if and how a
499 // variable is bound. These are returned in the output parameter *binding_kind
500 // of the LookupRecursive function.
501 enum BindingKind {
502 // The variable reference could be statically resolved to a variable binding
503 // which is returned. There is no 'with' statement between the reference and
504 // the binding and no scope between the reference scope (inclusive) and
505 // binding scope (exclusive) makes a non-strict 'eval' call.
506 BOUND,
507
508 // The variable reference could be statically resolved to a variable binding
509 // which is returned. There is no 'with' statement between the reference and
510 // the binding, but some scope between the reference scope (inclusive) and
511 // binding scope (exclusive) makes a non-strict 'eval' call, that might
512 // possibly introduce variable bindings shadowing the found one. Thus the
513 // found variable binding is just a guess.
514 BOUND_EVAL_SHADOWED,
515
516 // The variable reference could not be statically resolved to any binding
517 // and thus should be considered referencing a global variable. NULL is
518 // returned. The variable reference is not inside any 'with' statement and
519 // no scope between the reference scope (inclusive) and global scope
520 // (exclusive) makes a non-strict 'eval' call.
521 UNBOUND,
522
523 // The variable reference could not be statically resolved to any binding
524 // NULL is returned. The variable reference is not inside any 'with'
525 // statement, but some scope between the reference scope (inclusive) and
526 // global scope (exclusive) makes a non-strict 'eval' call, that might
527 // possibly introduce a variable binding. Thus the reference should be
528 // considered referencing a global variable unless it is shadowed by an
529 // 'eval' introduced binding.
530 UNBOUND_EVAL_SHADOWED,
531
532 // The variable could not be statically resolved and needs to be looked up
533 // dynamically. NULL is returned. There are two possible reasons:
534 // * A 'with' statement has been encountered and there is no variable
535 // binding for the name between the variable reference and the 'with'.
536 // The variable potentially references a property of the 'with' object.
537 // * The code is being executed as part of a call to 'eval' and the calling
538 // context chain contains either a variable binding for the name or it
539 // contains a 'with' context.
540 DYNAMIC_LOOKUP
541 };
542
543 // Lookup a variable reference given by name recursively starting with this
544 // scope. If the code is executed because of a call to 'eval', the context
545 // parameter should be set to the calling context of 'eval'.
Steve Blocka7e24c12009-10-30 11:49:00 +0000546 Variable* LookupRecursive(Handle<String> name,
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +0100547 BindingKind* binding_kind,
548 AstNodeFactory<AstNullVisitor>* factory);
549 MUST_USE_RESULT
550 bool ResolveVariable(CompilationInfo* info,
551 VariableProxy* proxy,
552 AstNodeFactory<AstNullVisitor>* factory);
553 MUST_USE_RESULT
554 bool ResolveVariablesRecursively(CompilationInfo* info,
555 AstNodeFactory<AstNullVisitor>* factory);
Steve Blocka7e24c12009-10-30 11:49:00 +0000556
557 // Scope analysis.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000558 bool PropagateScopeInfo(bool outer_scope_calls_non_strict_eval);
Steve Blocka7e24c12009-10-30 11:49:00 +0000559 bool HasTrivialContext() const;
560
561 // Predicates.
562 bool MustAllocate(Variable* var);
563 bool MustAllocateInContext(Variable* var);
564 bool HasArgumentsParameter();
565
566 // Variable allocation.
567 void AllocateStackSlot(Variable* var);
568 void AllocateHeapSlot(Variable* var);
569 void AllocateParameterLocals();
570 void AllocateNonParameterLocal(Variable* var);
571 void AllocateNonParameterLocals();
572 void AllocateVariablesRecursively();
Ben Murdochb8e0da22011-05-16 14:20:40 +0100573
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +0100574 // Resolve and fill in the allocation information for all variables
575 // in this scopes. Must be called *after* all scopes have been
576 // processed (parsed) to ensure that unresolved variables can be
577 // resolved properly.
578 //
579 // In the case of code compiled and run using 'eval', the context
580 // parameter is the context in which eval was called. In all other
581 // cases the context parameter is an empty handle.
582 MUST_USE_RESULT
583 bool AllocateVariables(CompilationInfo* info,
584 AstNodeFactory<AstNullVisitor>* factory);
585
Ben Murdochb8e0da22011-05-16 14:20:40 +0100586 private:
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000587 // Construct a scope based on the scope info.
588 Scope(Scope* inner_scope, ScopeType type, Handle<ScopeInfo> scope_info);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100589
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000590 // Construct a catch scope with a binding for the name.
591 Scope(Scope* inner_scope, Handle<String> catch_variable_name);
592
Steve Block44f0eee2011-05-26 01:26:41 +0100593 void AddInnerScope(Scope* inner_scope) {
594 if (inner_scope != NULL) {
595 inner_scopes_.Add(inner_scope);
596 inner_scope->outer_scope_ = this;
597 }
598 }
599
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000600 void SetDefaults(ScopeType type,
Ben Murdochb8e0da22011-05-16 14:20:40 +0100601 Scope* outer_scope,
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000602 Handle<ScopeInfo> scope_info);
Steve Blocka7e24c12009-10-30 11:49:00 +0000603};
604
Steve Blocka7e24c12009-10-30 11:49:00 +0000605} } // namespace v8::internal
606
607#endif // V8_SCOPES_H_