blob: a9220eb827566e94e7a126f1085bdc90d6c3a67c [file] [log] [blame]
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001// Copyright 2010 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 {
93 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
96 };
97
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000098 Scope(Scope* outer_scope, Type type);
99
100 virtual ~Scope() { }
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
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000107 // The scope name is only used for printing/debugging.
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000108 void SetScopeName(Handle<String> scope_name) { scope_name_ = scope_name; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000109
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +0000110 virtual void Initialize(bool inside_with);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000111
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +0000112 // Called just before leaving a scope.
113 virtual void Leave() {
114 // No cleanup or fixup necessary.
115 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000116
117 // ---------------------------------------------------------------------------
118 // Declarations
119
120 // Lookup a variable in this scope. Returns the variable or NULL if not found.
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000121 virtual Variable* LocalLookup(Handle<String> name);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000122
123 // Lookup a variable in this scope or outer scopes.
124 // Returns the variable or NULL if not found.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000125 virtual Variable* Lookup(Handle<String> name);
126
127 // Declare the function variable for a function literal. This variable
128 // is in an intermediate scope between this function scope and the the
129 // outer scope. Only possible for function scopes; at most one variable.
130 Variable* DeclareFunctionVar(Handle<String> name);
131
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000132 // Declare a local variable in this scope. If the variable has been
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000133 // declared before, the previously declared variable is returned.
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000134 virtual Variable* DeclareLocal(Handle<String> name, Variable::Mode mode);
135
136 // Declare an implicit global variable in this scope which must be a
137 // global scope. The variable was introduced (possibly from an inner
138 // scope) by a reference to an unresolved variable with no intervening
139 // with statements or eval calls.
140 Variable* DeclareGlobal(Handle<String> name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000141
142 // Add a parameter to the parameter list. The parameter must have been
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000143 // declared via Declare. The same parameter may occur more than once in
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000144 // the parameter list; they must be added in source order, from left to
145 // right.
146 void AddParameter(Variable* var);
147
148 // Create a new unresolved variable.
149 virtual VariableProxy* NewUnresolved(Handle<String> name, bool inside_with);
150
151 // Remove a unresolved variable. During parsing, an unresolved variable
152 // may have been added optimistically, but then only the variable name
153 // was used (typically for labels). If the variable was not declared, the
154 // addition introduced a new unresolved variable which may end up being
155 // allocated globally as a "ghost" variable. RemoveUnresolved removes
156 // such a variable again if it was added; otherwise this is a no-op.
157 void RemoveUnresolved(VariableProxy* var);
158
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000159 // Creates a new temporary variable in this scope. The name is only used
160 // for printing and cannot be used to find the variable. In particular,
161 // the only way to get hold of the temporary is by keeping the Variable*
162 // around.
163 virtual Variable* NewTemporary(Handle<String> name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000164
165 // Adds the specific declaration node to the list of declarations in
166 // this scope. The declarations are processed as part of entering
167 // the scope; see codegen.cc:ProcessDeclarations.
168 void AddDeclaration(Declaration* declaration);
169
170 // ---------------------------------------------------------------------------
171 // Illegal redeclaration support.
172
173 // Set an expression node that will be executed when the scope is
174 // entered. We only keep track of one illegal redeclaration node per
175 // scope - the first one - so if you try to set it multiple times
176 // the additional requests will be silently ignored.
177 void SetIllegalRedeclaration(Expression* expression);
178
179 // Visit the illegal redeclaration expression. Do not call if the
180 // scope doesn't have an illegal redeclaration node.
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000181 void VisitIllegalRedeclaration(AstVisitor* visitor);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000182
183 // Check if the scope has (at least) one illegal redeclaration.
184 bool HasIllegalRedeclaration() const { return illegal_redecl_ != NULL; }
185
186
187 // ---------------------------------------------------------------------------
188 // Scope-specific info.
189
190 // Inform the scope that the corresponding code contains a with statement.
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000191 void RecordWithStatement() { scope_contains_with_ = true; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000192
193 // Inform the scope that the corresponding code contains an eval call.
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000194 void RecordEvalCall() { scope_calls_eval_ = true; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000195
196
197 // ---------------------------------------------------------------------------
198 // Predicates.
199
200 // Specific scope types.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000201 bool is_eval_scope() const { return type_ == EVAL_SCOPE; }
202 bool is_function_scope() const { return type_ == FUNCTION_SCOPE; }
203 bool is_global_scope() const { return type_ == GLOBAL_SCOPE; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000204
ager@chromium.org381abbb2009-02-25 13:23:22 +0000205 // Information about which scopes calls eval.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000206 bool calls_eval() const { return scope_calls_eval_; }
207 bool outer_scope_calls_eval() const { return outer_scope_calls_eval_; }
ager@chromium.org381abbb2009-02-25 13:23:22 +0000208
209 // Is this scope inside a with statement.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000210 bool inside_with() const { return scope_inside_with_; }
ager@chromium.org381abbb2009-02-25 13:23:22 +0000211 // Does this scope contain a with statement.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000212 bool contains_with() const { return scope_contains_with_; }
ager@chromium.org381abbb2009-02-25 13:23:22 +0000213
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000214 // The scope immediately surrounding this scope, or NULL.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000215 Scope* outer_scope() const { return outer_scope_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000216
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000217 // ---------------------------------------------------------------------------
218 // Accessors.
219
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000220 // A new variable proxy corresponding to the (function) receiver.
221 VariableProxy* receiver() const {
222 VariableProxy* proxy =
223 new VariableProxy(Factory::this_symbol(), true, false);
224 proxy->BindTo(receiver_);
225 return proxy;
226 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000227
228 // The variable holding the function literal for named function
229 // literals, or NULL.
230 // Only valid for function scopes.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000231 Variable* function() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000232 ASSERT(is_function_scope());
233 return function_;
234 }
235
236 // Parameters. The left-most parameter has index 0.
237 // Only valid for function scopes.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000238 Variable* parameter(int index) const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000239 ASSERT(is_function_scope());
240 return params_[index];
241 }
242
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000243 int num_parameters() const { return params_.length(); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000244
245 // The local variable 'arguments' if we need to allocate it; NULL otherwise.
246 // If arguments() exist, arguments_shadow() exists, too.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000247 Variable* arguments() const { return arguments_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000248
249 // The '.arguments' shadow variable if we need to allocate it; NULL otherwise.
250 // If arguments_shadow() exist, arguments() exists, too.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000251 Variable* arguments_shadow() const { return arguments_shadow_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000252
253 // Declarations list.
254 ZoneList<Declaration*>* declarations() { return &decls_; }
255
256
257
258 // ---------------------------------------------------------------------------
259 // Variable allocation.
260
261 // Collect all used locals in this scope.
262 template<class Allocator>
263 void CollectUsedVariables(List<Variable*, Allocator>* locals);
264
ager@chromium.org381abbb2009-02-25 13:23:22 +0000265 // Resolve and fill in the allocation information for all variables
266 // in this scopes. Must be called *after* all scopes have been
267 // processed (parsed) to ensure that unresolved variables can be
268 // resolved properly.
269 //
270 // In the case of code compiled and run using 'eval', the context
271 // parameter is the context in which eval was called. In all other
272 // cases the context parameter is an empty handle.
273 void AllocateVariables(Handle<Context> context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000274
275 // Result of variable allocation.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000276 int num_stack_slots() const { return num_stack_slots_; }
277 int num_heap_slots() const { return num_heap_slots_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000278
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000279 // Make sure this scope and all outer scopes are eagerly compiled.
280 void ForceEagerCompilation() { force_eager_compilation_ = true; }
281
282 // Determine if we can use lazy compilation for this scope.
283 bool AllowsLazyCompilation() const;
284
285 // True if the outer context of this scope is always the global context.
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +0000286 virtual bool HasTrivialOuterContext() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000287
288 // The number of contexts between this and scope; zero if this == scope.
289 int ContextChainLength(Scope* scope);
290
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000291 // ---------------------------------------------------------------------------
ager@chromium.org378b34e2011-01-28 08:04:38 +0000292 // Strict mode support.
293 bool IsDeclared(Handle<String> name) {
294 // During formal parameter list parsing the scope only contains
295 // two variables inserted at initialization: "this" and "arguments".
296 // "this" is an invalid parameter name and "arguments" is invalid parameter
297 // name in strict mode. Therefore looking up with the map which includes
298 // "this" and "arguments" in addition to all formal parameters is safe.
299 return variables_.Lookup(name) != NULL;
300 }
301
302 // ---------------------------------------------------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000303 // Debugging.
304
305#ifdef DEBUG
306 void Print(int n = 0); // n = indentation; n < 0 => don't print recursively
307#endif
308
309 // ---------------------------------------------------------------------------
310 // Implementation.
311 protected:
312 friend class ParserFactory;
313
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000314 explicit Scope(Type type);
315
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000316 void InsertAfterScope(Scope* scope) {
317 inner_scopes_.Add(scope);
318 outer_scope_ = scope->outer_scope_;
319 outer_scope_->inner_scopes_.RemoveElement(scope);
320 outer_scope_->inner_scopes_.Add(this);
321 scope->outer_scope_ = this;
322 }
323
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000324 // Scope tree.
325 Scope* outer_scope_; // the immediately enclosing outer scope, or NULL
326 ZoneList<Scope*> inner_scopes_; // the immediately enclosed inner scopes
327
328 // The scope type.
329 Type type_;
330
331 // Debugging support.
332 Handle<String> scope_name_;
333
334 // The variables declared in this scope:
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000335 //
336 // All user-declared variables (incl. parameters). For global scopes
337 // variables may be implicitly 'declared' by being used (possibly in
338 // an inner scope) with no intervening with statements or eval calls.
339 VariableMap variables_;
340 // Compiler-allocated (user-invisible) temporaries.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000341 ZoneList<Variable*> temps_;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000342 // Parameter list in source order.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000343 ZoneList<Variable*> params_;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000344 // Variables that must be looked up dynamically.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000345 DynamicScopePart* dynamics_;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000346 // Unresolved variables referred to from this scope.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000347 ZoneList<VariableProxy*> unresolved_;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000348 // Declarations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000349 ZoneList<Declaration*> decls_;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000350 // Convenience variable.
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000351 Variable* receiver_;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000352 // Function variable, if any; function scopes only.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000353 Variable* function_;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000354 // Convenience variable; function scopes only.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000355 Variable* arguments_;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000356 // Convenience variable; function scopes only.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000357 Variable* arguments_shadow_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000358
359 // Illegal redeclaration.
360 Expression* illegal_redecl_;
361
362 // Scope-specific information.
363 bool scope_inside_with_; // this scope is inside a 'with' of some outer scope
364 bool scope_contains_with_; // this scope contains a 'with' statement
365 bool scope_calls_eval_; // this scope contains an 'eval' call
366
367 // Computed via PropagateScopeInfo.
368 bool outer_scope_calls_eval_;
369 bool inner_scope_calls_eval_;
ager@chromium.org381abbb2009-02-25 13:23:22 +0000370 bool outer_scope_is_eval_scope_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000371 bool force_eager_compilation_;
372
373 // Computed via AllocateVariables; function scopes only.
374 int num_stack_slots_;
375 int num_heap_slots_;
376
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000377 // Serialized scopes support.
378 SerializedScopeInfo* scope_info_;
379 bool resolved() { return scope_info_ != NULL; }
380
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000381 // Create a non-local variable with a given name.
382 // These variables are looked up dynamically at runtime.
ager@chromium.org381abbb2009-02-25 13:23:22 +0000383 Variable* NonLocal(Handle<String> name, Variable::Mode mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000384
385 // Variable resolution.
ager@chromium.org381abbb2009-02-25 13:23:22 +0000386 Variable* LookupRecursive(Handle<String> name,
387 bool inner_lookup,
388 Variable** invalidated_local);
389 void ResolveVariable(Scope* global_scope,
390 Handle<Context> context,
391 VariableProxy* proxy);
392 void ResolveVariablesRecursively(Scope* global_scope,
393 Handle<Context> context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000394
395 // Scope analysis.
ager@chromium.org381abbb2009-02-25 13:23:22 +0000396 bool PropagateScopeInfo(bool outer_scope_calls_eval,
397 bool outer_scope_is_eval_scope);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000398 bool HasTrivialContext() const;
399
400 // Predicates.
401 bool MustAllocate(Variable* var);
402 bool MustAllocateInContext(Variable* var);
403 bool HasArgumentsParameter();
404
405 // Variable allocation.
406 void AllocateStackSlot(Variable* var);
407 void AllocateHeapSlot(Variable* var);
408 void AllocateParameterLocals();
409 void AllocateNonParameterLocal(Variable* var);
410 void AllocateNonParameterLocals();
411 void AllocateVariablesRecursively();
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000412
413 private:
414 Scope(Scope* inner_scope, SerializedScopeInfo* scope_info);
415
416 void SetDefaults(Type type,
417 Scope* outer_scope,
418 SerializedScopeInfo* scope_info) {
419 outer_scope_ = outer_scope;
420 type_ = type;
421 scope_name_ = Factory::empty_symbol();
422 dynamics_ = NULL;
423 receiver_ = NULL;
424 function_ = NULL;
425 arguments_ = NULL;
426 arguments_shadow_ = NULL;
427 illegal_redecl_ = NULL;
428 scope_inside_with_ = false;
429 scope_contains_with_ = false;
430 scope_calls_eval_ = false;
431 outer_scope_calls_eval_ = false;
432 inner_scope_calls_eval_ = false;
433 outer_scope_is_eval_scope_ = false;
434 force_eager_compilation_ = false;
435 num_stack_slots_ = 0;
436 num_heap_slots_ = 0;
437 scope_info_ = scope_info;
438 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000439};
440
441
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +0000442// Scope used during pre-parsing.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000443class DummyScope : public Scope {
444 public:
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +0000445 DummyScope()
446 : Scope(GLOBAL_SCOPE),
447 nesting_level_(1), // Allows us to Leave the initial scope.
448 inside_with_level_(kNotInsideWith) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000449 outer_scope_ = this;
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +0000450 scope_inside_with_ = false;
451 }
452
453 virtual void Initialize(bool inside_with) {
454 nesting_level_++;
455 if (inside_with && inside_with_level_ == kNotInsideWith) {
456 inside_with_level_ = nesting_level_;
457 }
458 ASSERT(inside_with_level_ <= nesting_level_);
459 }
460
461 virtual void Leave() {
462 nesting_level_--;
463 ASSERT(nesting_level_ >= 0);
464 if (nesting_level_ < inside_with_level_) {
465 inside_with_level_ = kNotInsideWith;
466 }
467 ASSERT(inside_with_level_ <= nesting_level_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000468 }
469
470 virtual Variable* Lookup(Handle<String> name) { return NULL; }
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +0000471
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000472 virtual VariableProxy* NewUnresolved(Handle<String> name, bool inside_with) {
473 return NULL;
474 }
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +0000475
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000476 virtual Variable* NewTemporary(Handle<String> name) { return NULL; }
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +0000477
478 virtual bool HasTrivialOuterContext() const {
479 return (nesting_level_ == 0 || inside_with_level_ <= 0);
480 }
481
482 private:
483 static const int kNotInsideWith = -1;
484 // Number of surrounding scopes of the current scope.
485 int nesting_level_;
486 // Nesting level of outermost scope that is contained in a with statement,
487 // or kNotInsideWith if there are no with's around the current scope.
488 int inside_with_level_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000489};
490
491
492} } // namespace v8::internal
493
494#endif // V8_SCOPES_H_