blob: 68cf5e51792e7a75a37fe51d0c834592069ff700 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
2// 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
37
38// A hash map to support fast variable declaration and lookup.
39class VariableMap: public HashMap {
40 public:
41 VariableMap();
42
43 // Dummy constructor. This constructor doesn't set up the map
44 // properly so don't use it unless you have a good reason.
45 explicit VariableMap(bool gotta_love_static_overloading);
46
47 virtual ~VariableMap();
48
49 Variable* Declare(Scope* scope,
50 Handle<String> name,
51 Variable::Mode mode,
52 bool is_valid_lhs,
53 Variable::Kind kind);
54
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:
65 VariableMap* GetMap(Variable::Mode mode) {
66 int index = mode - Variable::DYNAMIC;
67 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
90 enum Type {
91 EVAL_SCOPE, // the top-level scope for an 'eval' source
92 FUNCTION_SCOPE, // the top-level scope for a function
93 GLOBAL_SCOPE // the top-level scope for a program or a top-level eval
94 };
95
96 Scope(Scope* outer_scope, Type type);
97
98 virtual ~Scope() { }
99
100 // The scope name is only used for printing/debugging.
101 void SetScopeName(Handle<String> scope_name) { scope_name_ = scope_name; }
102
103 void Initialize(bool inside_with);
104
105
106 // ---------------------------------------------------------------------------
107 // Declarations
108
109 // Lookup a variable in this scope. Returns the variable or NULL if not found.
110 virtual Variable* LocalLookup(Handle<String> name);
111
112 // Lookup a variable in this scope or outer scopes.
113 // Returns the variable or NULL if not found.
114 virtual Variable* Lookup(Handle<String> name);
115
116 // Declare the function variable for a function literal. This variable
117 // is in an intermediate scope between this function scope and the the
118 // outer scope. Only possible for function scopes; at most one variable.
119 Variable* DeclareFunctionVar(Handle<String> name);
120
121 // Declare a local variable in this scope. If the variable has been
122 // declared before, the previously declared variable is returned.
123 virtual Variable* DeclareLocal(Handle<String> name, Variable::Mode mode);
124
125 // Declare an implicit global variable in this scope which must be a
126 // global scope. The variable was introduced (possibly from an inner
127 // scope) by a reference to an unresolved variable with no intervening
128 // with statements or eval calls.
129 Variable* DeclareGlobal(Handle<String> name);
130
131 // Add a parameter to the parameter list. The parameter must have been
132 // declared via Declare. The same parameter may occur more than once in
133 // the parameter list; they must be added in source order, from left to
134 // right.
135 void AddParameter(Variable* var);
136
137 // Create a new unresolved variable.
138 virtual VariableProxy* NewUnresolved(Handle<String> name, bool inside_with);
139
140 // Remove a unresolved variable. During parsing, an unresolved variable
141 // may have been added optimistically, but then only the variable name
142 // was used (typically for labels). If the variable was not declared, the
143 // addition introduced a new unresolved variable which may end up being
144 // allocated globally as a "ghost" variable. RemoveUnresolved removes
145 // such a variable again if it was added; otherwise this is a no-op.
146 void RemoveUnresolved(VariableProxy* var);
147
148 // Creates a new temporary variable in this scope and binds a proxy to it.
149 // The name is only used for printing and cannot be used to find the variable.
150 // In particular, the only way to get hold of the temporary is by keeping the
151 // VariableProxy* around.
152 virtual VariableProxy* NewTemporary(Handle<String> name);
153
154 // Adds the specific declaration node to the list of declarations in
155 // this scope. The declarations are processed as part of entering
156 // the scope; see codegen.cc:ProcessDeclarations.
157 void AddDeclaration(Declaration* declaration);
158
159 // ---------------------------------------------------------------------------
160 // Illegal redeclaration support.
161
162 // Set an expression node that will be executed when the scope is
163 // entered. We only keep track of one illegal redeclaration node per
164 // scope - the first one - so if you try to set it multiple times
165 // the additional requests will be silently ignored.
166 void SetIllegalRedeclaration(Expression* expression);
167
168 // Visit the illegal redeclaration expression. Do not call if the
169 // scope doesn't have an illegal redeclaration node.
170 void VisitIllegalRedeclaration(AstVisitor* visitor);
171
172 // Check if the scope has (at least) one illegal redeclaration.
173 bool HasIllegalRedeclaration() const { return illegal_redecl_ != NULL; }
174
175
176 // ---------------------------------------------------------------------------
177 // Scope-specific info.
178
179 // Inform the scope that the corresponding code contains a with statement.
180 void RecordWithStatement() { scope_contains_with_ = true; }
181
182 // Inform the scope that the corresponding code contains an eval call.
183 void RecordEvalCall() { scope_calls_eval_ = true; }
184
185
186 // ---------------------------------------------------------------------------
187 // Predicates.
188
189 // Specific scope types.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100190 bool is_eval_scope() const { return type_ == EVAL_SCOPE; }
191 bool is_function_scope() const { return type_ == FUNCTION_SCOPE; }
192 bool is_global_scope() const { return type_ == GLOBAL_SCOPE; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000193
194 // Information about which scopes calls eval.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100195 bool calls_eval() const { return scope_calls_eval_; }
196 bool outer_scope_calls_eval() const { return outer_scope_calls_eval_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000197
198 // Is this scope inside a with statement.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100199 bool inside_with() const { return scope_inside_with_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000200 // Does this scope contain a with statement.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100201 bool contains_with() const { return scope_contains_with_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000202
203 // The scope immediately surrounding this scope, or NULL.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100204 Scope* outer_scope() const { return outer_scope_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000205
206 // ---------------------------------------------------------------------------
207 // Accessors.
208
Leon Clarkee46be812010-01-19 14:06:41 +0000209 // A new variable proxy corresponding to the (function) receiver.
210 VariableProxy* receiver() const {
211 VariableProxy* proxy =
212 new VariableProxy(Factory::this_symbol(), true, false);
213 proxy->BindTo(receiver_);
214 return proxy;
215 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000216
217 // The variable holding the function literal for named function
218 // literals, or NULL.
219 // Only valid for function scopes.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100220 Variable* function() const {
Steve Blocka7e24c12009-10-30 11:49:00 +0000221 ASSERT(is_function_scope());
222 return function_;
223 }
224
225 // Parameters. The left-most parameter has index 0.
226 // Only valid for function scopes.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100227 Variable* parameter(int index) const {
Steve Blocka7e24c12009-10-30 11:49:00 +0000228 ASSERT(is_function_scope());
229 return params_[index];
230 }
231
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100232 int num_parameters() const { return params_.length(); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000233
234 // The local variable 'arguments' if we need to allocate it; NULL otherwise.
235 // If arguments() exist, arguments_shadow() exists, too.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100236 Variable* arguments() const { return arguments_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000237
238 // The '.arguments' shadow variable if we need to allocate it; NULL otherwise.
239 // If arguments_shadow() exist, arguments() exists, too.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100240 Variable* arguments_shadow() const { return arguments_shadow_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000241
242 // Declarations list.
243 ZoneList<Declaration*>* declarations() { return &decls_; }
244
245
246
247 // ---------------------------------------------------------------------------
248 // Variable allocation.
249
250 // Collect all used locals in this scope.
251 template<class Allocator>
252 void CollectUsedVariables(List<Variable*, Allocator>* locals);
253
254 // Resolve and fill in the allocation information for all variables
255 // in this scopes. Must be called *after* all scopes have been
256 // processed (parsed) to ensure that unresolved variables can be
257 // resolved properly.
258 //
259 // In the case of code compiled and run using 'eval', the context
260 // parameter is the context in which eval was called. In all other
261 // cases the context parameter is an empty handle.
262 void AllocateVariables(Handle<Context> context);
263
264 // Result of variable allocation.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100265 int num_stack_slots() const { return num_stack_slots_; }
266 int num_heap_slots() const { return num_heap_slots_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000267
268 // Make sure this scope and all outer scopes are eagerly compiled.
269 void ForceEagerCompilation() { force_eager_compilation_ = true; }
270
271 // Determine if we can use lazy compilation for this scope.
272 bool AllowsLazyCompilation() const;
273
274 // True if the outer context of this scope is always the global context.
275 bool HasTrivialOuterContext() const;
276
277 // The number of contexts between this and scope; zero if this == scope.
278 int ContextChainLength(Scope* scope);
279
Steve Blocka7e24c12009-10-30 11:49:00 +0000280 // ---------------------------------------------------------------------------
281 // Debugging.
282
283#ifdef DEBUG
284 void Print(int n = 0); // n = indentation; n < 0 => don't print recursively
285#endif
286
287 // ---------------------------------------------------------------------------
288 // Implementation.
289 protected:
290 friend class ParserFactory;
291
292 explicit Scope(Type type);
293
294 // Scope tree.
295 Scope* outer_scope_; // the immediately enclosing outer scope, or NULL
296 ZoneList<Scope*> inner_scopes_; // the immediately enclosed inner scopes
297
298 // The scope type.
299 Type type_;
300
301 // Debugging support.
302 Handle<String> scope_name_;
303
304 // The variables declared in this scope:
305 //
306 // All user-declared variables (incl. parameters). For global scopes
307 // variables may be implicitly 'declared' by being used (possibly in
308 // an inner scope) with no intervening with statements or eval calls.
309 VariableMap variables_;
310 // Compiler-allocated (user-invisible) temporaries.
311 ZoneList<Variable*> temps_;
312 // Parameter list in source order.
313 ZoneList<Variable*> params_;
314 // Variables that must be looked up dynamically.
315 DynamicScopePart* dynamics_;
316 // Unresolved variables referred to from this scope.
317 ZoneList<VariableProxy*> unresolved_;
318 // Declarations.
319 ZoneList<Declaration*> decls_;
320 // Convenience variable.
Leon Clarkee46be812010-01-19 14:06:41 +0000321 Variable* receiver_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000322 // Function variable, if any; function scopes only.
323 Variable* function_;
324 // Convenience variable; function scopes only.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100325 Variable* arguments_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000326 // Convenience variable; function scopes only.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100327 Variable* arguments_shadow_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000328
329 // Illegal redeclaration.
330 Expression* illegal_redecl_;
331
332 // Scope-specific information.
333 bool scope_inside_with_; // this scope is inside a 'with' of some outer scope
334 bool scope_contains_with_; // this scope contains a 'with' statement
335 bool scope_calls_eval_; // this scope contains an 'eval' call
336
337 // Computed via PropagateScopeInfo.
338 bool outer_scope_calls_eval_;
339 bool inner_scope_calls_eval_;
340 bool outer_scope_is_eval_scope_;
341 bool force_eager_compilation_;
342
343 // Computed via AllocateVariables; function scopes only.
344 int num_stack_slots_;
345 int num_heap_slots_;
346
347 // Create a non-local variable with a given name.
348 // These variables are looked up dynamically at runtime.
349 Variable* NonLocal(Handle<String> name, Variable::Mode mode);
350
351 // Variable resolution.
352 Variable* LookupRecursive(Handle<String> name,
353 bool inner_lookup,
354 Variable** invalidated_local);
355 void ResolveVariable(Scope* global_scope,
356 Handle<Context> context,
357 VariableProxy* proxy);
358 void ResolveVariablesRecursively(Scope* global_scope,
359 Handle<Context> context);
360
361 // Scope analysis.
362 bool PropagateScopeInfo(bool outer_scope_calls_eval,
363 bool outer_scope_is_eval_scope);
364 bool HasTrivialContext() const;
365
366 // Predicates.
367 bool MustAllocate(Variable* var);
368 bool MustAllocateInContext(Variable* var);
369 bool HasArgumentsParameter();
370
371 // Variable allocation.
372 void AllocateStackSlot(Variable* var);
373 void AllocateHeapSlot(Variable* var);
374 void AllocateParameterLocals();
375 void AllocateNonParameterLocal(Variable* var);
376 void AllocateNonParameterLocals();
377 void AllocateVariablesRecursively();
378};
379
380
381class DummyScope : public Scope {
382 public:
383 DummyScope() : Scope(GLOBAL_SCOPE) {
384 outer_scope_ = this;
385 }
386
387 virtual Variable* Lookup(Handle<String> name) { return NULL; }
388 virtual Variable* Declare(Handle<String> name, Variable::Mode mode) {
389 return NULL;
390 }
391 virtual VariableProxy* NewUnresolved(Handle<String> name, bool inside_with) {
392 return NULL;
393 }
394 virtual VariableProxy* NewTemporary(Handle<String> name) { return NULL; }
395};
396
397
398} } // namespace v8::internal
399
400#endif // V8_SCOPES_H_