blob: c2c41799b93fdb94590bff2eb7e01a8d7016d700 [file] [log] [blame]
ricow@chromium.orgc54d3652011-05-30 09:20:16 +00001// Copyright 2011 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +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
kasperl@chromium.org71affb52009-05-26 05:44:31 +000034namespace v8 {
35namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000036
ager@chromium.orgb61a0d12010-10-13 08:35:23 +000037class CompilationInfo;
38
39
kasperl@chromium.org68ac0092009-07-09 06:00:35 +000040// A hash map to support fast variable declaration and lookup.
41class VariableMap: public HashMap {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000042 public:
kasperl@chromium.org68ac0092009-07-09 06:00:35 +000043 VariableMap();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000044
45 // Dummy constructor. This constructor doesn't set up the map
46 // properly so don't use it unless you have a good reason.
kasperl@chromium.org68ac0092009-07-09 06:00:35 +000047 explicit VariableMap(bool gotta_love_static_overloading);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000048
kasperl@chromium.org68ac0092009-07-09 06:00:35 +000049 virtual ~VariableMap();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000050
kasperl@chromium.org68ac0092009-07-09 06:00:35 +000051 Variable* Declare(Scope* scope,
52 Handle<String> name,
53 Variable::Mode mode,
54 bool is_valid_lhs,
55 Variable::Kind kind);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000056
57 Variable* Lookup(Handle<String> name);
58};
59
60
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +000061// The dynamic scope part holds hash maps for the variables that will
62// be looked up dynamically from within eval and with scopes. The objects
63// are allocated on-demand from Scope::NonLocal to avoid wasting memory
64// and setup time for scopes that don't need them.
65class DynamicScopePart : public ZoneObject {
66 public:
kasperl@chromium.org68ac0092009-07-09 06:00:35 +000067 VariableMap* GetMap(Variable::Mode mode) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +000068 int index = mode - Variable::DYNAMIC;
69 ASSERT(index >= 0 && index < 3);
70 return &maps_[index];
71 }
72
73 private:
kasperl@chromium.org68ac0092009-07-09 06:00:35 +000074 VariableMap maps_[3];
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +000075};
76
77
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000078// Global invariants after AST construction: Each reference (i.e. identifier)
79// to a JavaScript variable (including global properties) is represented by a
80// VariableProxy node. Immediately after AST construction and before variable
81// allocation, most VariableProxy nodes are "unresolved", i.e. not bound to a
82// corresponding variable (though some are bound during parse time). Variable
83// allocation binds each unresolved VariableProxy to one Variable and assigns
84// a location. Note that many VariableProxy nodes may refer to the same Java-
85// Script variable.
86
87class Scope: public ZoneObject {
88 public:
89 // ---------------------------------------------------------------------------
90 // Construction
91
92 enum Type {
ricow@chromium.org4f693d62011-07-04 14:01:31 +000093 EVAL_SCOPE, // The top-level scope for an eval source.
94 FUNCTION_SCOPE, // The top-level scope for a function.
95 GLOBAL_SCOPE, // The top-level scope for a program or a top-level eval.
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +000096 CATCH_SCOPE, // The scope introduced by catch.
97 BLOCK_SCOPE // The scope introduced by a new block.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000098 };
99
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000100 Scope(Scope* outer_scope, Type type);
101
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000102 // Compute top scope and allocate variables. For lazy compilation the top
103 // scope only contains the single lazily compiled function, so this
104 // doesn't re-allocate variables repeatedly.
105 static bool Analyze(CompilationInfo* info);
106
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000107 static Scope* DeserializeScopeChain(CompilationInfo* info,
108 Scope* innermost_scope);
109
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000110 // The scope name is only used for printing/debugging.
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000111 void SetScopeName(Handle<String> scope_name) { scope_name_ = scope_name; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000112
ricow@chromium.orgc54d3652011-05-30 09:20:16 +0000113 void Initialize(bool inside_with);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000114
115 // ---------------------------------------------------------------------------
116 // Declarations
117
118 // Lookup a variable in this scope. Returns the variable or NULL if not found.
ricow@chromium.orgc54d3652011-05-30 09:20:16 +0000119 Variable* LocalLookup(Handle<String> name);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000120
121 // Lookup a variable in this scope or outer scopes.
122 // Returns the variable or NULL if not found.
ricow@chromium.orgc54d3652011-05-30 09:20:16 +0000123 Variable* Lookup(Handle<String> name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000124
125 // Declare the function variable for a function literal. This variable
126 // is in an intermediate scope between this function scope and the the
127 // outer scope. Only possible for function scopes; at most one variable.
128 Variable* DeclareFunctionVar(Handle<String> name);
129
ricow@chromium.orgc54d3652011-05-30 09:20:16 +0000130 // Declare a parameter in this scope. When there are duplicated
131 // parameters the rightmost one 'wins'. However, the implementation
132 // expects all parameters to be declared and from left to right.
133 void DeclareParameter(Handle<String> name);
134
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000135 // Declare a local variable in this scope. If the variable has been
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000136 // declared before, the previously declared variable is returned.
ricow@chromium.orgc54d3652011-05-30 09:20:16 +0000137 Variable* DeclareLocal(Handle<String> name, Variable::Mode mode);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000138
139 // Declare an implicit global variable in this scope which must be a
140 // global scope. The variable was introduced (possibly from an inner
141 // scope) by a reference to an unresolved variable with no intervening
142 // with statements or eval calls.
143 Variable* DeclareGlobal(Handle<String> name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000144
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000145 // Create a new unresolved variable.
ricow@chromium.orgc54d3652011-05-30 09:20:16 +0000146 VariableProxy* NewUnresolved(Handle<String> name,
147 bool inside_with,
148 int position = RelocInfo::kNoPosition);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +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
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000158 // 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.
ricow@chromium.orgc54d3652011-05-30 09:20:16 +0000162 Variable* NewTemporary(Handle<String> name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +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.
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000180 void VisitIllegalRedeclaration(AstVisitor* visitor);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000181
182 // Check if the scope has (at least) one illegal redeclaration.
183 bool HasIllegalRedeclaration() const { return illegal_redecl_ != NULL; }
184
185
186 // ---------------------------------------------------------------------------
187 // Scope-specific info.
188
189 // Inform the scope that the corresponding code contains a with statement.
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000190 void RecordWithStatement() { scope_contains_with_ = true; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000191
192 // Inform the scope that the corresponding code contains an eval call.
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000193 void RecordEvalCall() { scope_calls_eval_ = true; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000194
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000195 // Enable strict mode for the scope (unless disabled by a global flag).
196 void EnableStrictMode() {
197 strict_mode_ = FLAG_strict_mode;
198 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000199
200 // ---------------------------------------------------------------------------
201 // Predicates.
202
203 // Specific scope types.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000204 bool is_eval_scope() const { return type_ == EVAL_SCOPE; }
205 bool is_function_scope() const { return type_ == FUNCTION_SCOPE; }
206 bool is_global_scope() const { return type_ == GLOBAL_SCOPE; }
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000207 bool is_catch_scope() const { return type_ == CATCH_SCOPE; }
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000208 bool is_block_scope() const { return type_ == BLOCK_SCOPE; }
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000209 bool is_strict_mode() const { return strict_mode_; }
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000210 bool is_strict_mode_eval_scope() const {
211 return is_eval_scope() && is_strict_mode();
212 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000213
ager@chromium.org381abbb2009-02-25 13:23:22 +0000214 // Information about which scopes calls eval.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000215 bool calls_eval() const { return scope_calls_eval_; }
216 bool outer_scope_calls_eval() const { return outer_scope_calls_eval_; }
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000217 bool outer_scope_calls_non_strict_eval() const {
218 return outer_scope_calls_non_strict_eval_;
219 }
ager@chromium.org381abbb2009-02-25 13:23:22 +0000220
221 // Is this scope inside a with statement.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000222 bool inside_with() const { return scope_inside_with_; }
ager@chromium.org381abbb2009-02-25 13:23:22 +0000223 // Does this scope contain a with statement.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000224 bool contains_with() const { return scope_contains_with_; }
ager@chromium.org381abbb2009-02-25 13:23:22 +0000225
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000226 // The scope immediately surrounding this scope, or NULL.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000227 Scope* outer_scope() const { return outer_scope_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000228
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000229 // ---------------------------------------------------------------------------
230 // Accessors.
231
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000232 // The variable corresponding the 'this' value.
233 Variable* receiver() { return receiver_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000234
235 // The variable holding the function literal for named function
236 // literals, or NULL.
237 // Only valid for function scopes.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000238 Variable* function() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000239 ASSERT(is_function_scope());
240 return function_;
241 }
242
243 // Parameters. The left-most parameter has index 0.
244 // Only valid for function scopes.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000245 Variable* parameter(int index) const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000246 ASSERT(is_function_scope());
247 return params_[index];
248 }
249
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000250 int num_parameters() const { return params_.length(); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000251
252 // The local variable 'arguments' if we need to allocate it; NULL otherwise.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000253 Variable* arguments() const { return arguments_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000254
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000255 // Declarations list.
256 ZoneList<Declaration*>* declarations() { return &decls_; }
257
258
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000259 // ---------------------------------------------------------------------------
260 // Variable allocation.
261
262 // Collect all used locals in this scope.
263 template<class Allocator>
264 void CollectUsedVariables(List<Variable*, Allocator>* locals);
265
ager@chromium.org381abbb2009-02-25 13:23:22 +0000266 // Resolve and fill in the allocation information for all variables
267 // in this scopes. Must be called *after* all scopes have been
268 // processed (parsed) to ensure that unresolved variables can be
269 // resolved properly.
270 //
271 // In the case of code compiled and run using 'eval', the context
272 // parameter is the context in which eval was called. In all other
273 // cases the context parameter is an empty handle.
274 void AllocateVariables(Handle<Context> context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000275
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000276 // Current number of var or const locals.
277 int num_var_or_const() { return num_var_or_const_; }
278
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000279 // Result of variable allocation.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000280 int num_stack_slots() const { return num_stack_slots_; }
281 int num_heap_slots() const { return num_heap_slots_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000282
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000283 // Make sure this scope and all outer scopes are eagerly compiled.
284 void ForceEagerCompilation() { force_eager_compilation_ = true; }
285
286 // Determine if we can use lazy compilation for this scope.
287 bool AllowsLazyCompilation() const;
288
289 // True if the outer context of this scope is always the global context.
ricow@chromium.orgc54d3652011-05-30 09:20:16 +0000290 bool HasTrivialOuterContext() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000291
292 // The number of contexts between this and scope; zero if this == scope.
293 int ContextChainLength(Scope* scope);
294
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000295 // Find the first function, global, or eval scope. This is the scope
296 // where var declarations will be hoisted to in the implementation.
297 Scope* DeclarationScope();
298
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000299 Handle<SerializedScopeInfo> GetSerializedScopeInfo();
300
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000301 // ---------------------------------------------------------------------------
ager@chromium.org378b34e2011-01-28 08:04:38 +0000302 // Strict mode support.
303 bool IsDeclared(Handle<String> name) {
304 // During formal parameter list parsing the scope only contains
305 // two variables inserted at initialization: "this" and "arguments".
306 // "this" is an invalid parameter name and "arguments" is invalid parameter
307 // name in strict mode. Therefore looking up with the map which includes
308 // "this" and "arguments" in addition to all formal parameters is safe.
309 return variables_.Lookup(name) != NULL;
310 }
311
312 // ---------------------------------------------------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000313 // Debugging.
314
315#ifdef DEBUG
316 void Print(int n = 0); // n = indentation; n < 0 => don't print recursively
317#endif
318
319 // ---------------------------------------------------------------------------
320 // Implementation.
321 protected:
322 friend class ParserFactory;
323
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000324 explicit Scope(Type type);
325
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000326 Isolate* const isolate_;
327
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000328 // Scope tree.
329 Scope* outer_scope_; // the immediately enclosing outer scope, or NULL
330 ZoneList<Scope*> inner_scopes_; // the immediately enclosed inner scopes
331
332 // The scope type.
333 Type type_;
334
335 // Debugging support.
336 Handle<String> scope_name_;
337
338 // The variables declared in this scope:
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000339 //
340 // All user-declared variables (incl. parameters). For global scopes
341 // variables may be implicitly 'declared' by being used (possibly in
342 // an inner scope) with no intervening with statements or eval calls.
343 VariableMap variables_;
344 // Compiler-allocated (user-invisible) temporaries.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000345 ZoneList<Variable*> temps_;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000346 // Parameter list in source order.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000347 ZoneList<Variable*> params_;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000348 // Variables that must be looked up dynamically.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000349 DynamicScopePart* dynamics_;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000350 // Unresolved variables referred to from this scope.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000351 ZoneList<VariableProxy*> unresolved_;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000352 // Declarations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000353 ZoneList<Declaration*> decls_;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000354 // Convenience variable.
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000355 Variable* receiver_;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000356 // Function variable, if any; function scopes only.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000357 Variable* function_;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000358 // Convenience variable; function scopes only.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000359 Variable* arguments_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000360
361 // Illegal redeclaration.
362 Expression* illegal_redecl_;
363
vegorov@chromium.org7943d462011-08-01 11:41:52 +0000364 // Scope-specific information computed during parsing.
365 //
366 // This scope is inside a 'with' of some outer scope.
367 bool scope_inside_with_;
368 // This scope contains a 'with' statement.
369 bool scope_contains_with_;
370 // This scope or a nested catch scope or with scope contain an 'eval' call. At
371 // the 'eval' call site this scope is the declaration scope.
372 bool scope_calls_eval_;
373 // This scope is a strict mode scope.
374 bool strict_mode_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000375
376 // Computed via PropagateScopeInfo.
377 bool outer_scope_calls_eval_;
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000378 bool outer_scope_calls_non_strict_eval_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000379 bool inner_scope_calls_eval_;
ager@chromium.org381abbb2009-02-25 13:23:22 +0000380 bool outer_scope_is_eval_scope_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000381 bool force_eager_compilation_;
382
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000383 // True if it doesn't need scope resolution (e.g., if the scope was
384 // constructed based on a serialized scope info or a catch context).
385 bool already_resolved_;
386
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000387 // Computed as variables are declared.
388 int num_var_or_const_;
389
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000390 // Computed via AllocateVariables; function scopes only.
391 int num_stack_slots_;
392 int num_heap_slots_;
393
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000394 // Serialized scopes support.
sgjesse@chromium.org34755092011-04-07 08:41:03 +0000395 Handle<SerializedScopeInfo> scope_info_;
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000396 bool already_resolved() { return already_resolved_; }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000397
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000398 // Create a non-local variable with a given name.
399 // These variables are looked up dynamically at runtime.
ager@chromium.org381abbb2009-02-25 13:23:22 +0000400 Variable* NonLocal(Handle<String> name, Variable::Mode mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000401
402 // Variable resolution.
ager@chromium.org381abbb2009-02-25 13:23:22 +0000403 Variable* LookupRecursive(Handle<String> name,
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000404 bool from_inner_function,
ager@chromium.org381abbb2009-02-25 13:23:22 +0000405 Variable** invalidated_local);
406 void ResolveVariable(Scope* global_scope,
407 Handle<Context> context,
408 VariableProxy* proxy);
409 void ResolveVariablesRecursively(Scope* global_scope,
410 Handle<Context> context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000411
412 // Scope analysis.
ager@chromium.org381abbb2009-02-25 13:23:22 +0000413 bool PropagateScopeInfo(bool outer_scope_calls_eval,
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000414 bool outer_scope_calls_non_strict_eval,
ager@chromium.org381abbb2009-02-25 13:23:22 +0000415 bool outer_scope_is_eval_scope);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000416 bool HasTrivialContext() const;
417
418 // Predicates.
419 bool MustAllocate(Variable* var);
420 bool MustAllocateInContext(Variable* var);
421 bool HasArgumentsParameter();
422
423 // Variable allocation.
424 void AllocateStackSlot(Variable* var);
425 void AllocateHeapSlot(Variable* var);
426 void AllocateParameterLocals();
427 void AllocateNonParameterLocal(Variable* var);
428 void AllocateNonParameterLocals();
429 void AllocateVariablesRecursively();
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000430
431 private:
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000432 // Construct a function or block scope based on the scope info.
433 Scope(Scope* inner_scope, Type type, Handle<SerializedScopeInfo> scope_info);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000434
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000435 // Construct a catch scope with a binding for the name.
436 Scope(Scope* inner_scope, Handle<String> catch_variable_name);
437
rossberg@chromium.org717967f2011-07-20 13:44:42 +0000438 inline Slot* NewSlot(Variable* var, Slot::Type type, int index) {
439 return new(isolate_->zone()) Slot(isolate_, var, type, index);
440 }
441
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000442 void AddInnerScope(Scope* inner_scope) {
443 if (inner_scope != NULL) {
444 inner_scopes_.Add(inner_scope);
445 inner_scope->outer_scope_ = this;
446 }
447 }
448
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000449 void SetDefaults(Type type,
450 Scope* outer_scope,
sgjesse@chromium.org34755092011-04-07 08:41:03 +0000451 Handle<SerializedScopeInfo> scope_info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000452};
453
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000454} } // namespace v8::internal
455
456#endif // V8_SCOPES_H_