blob: 85d0c4598b0c7ae0bb599d98f5edd5c906180bf5 [file] [log] [blame]
svenpanne@chromium.orgb1df11d2012-02-08 10:26:21 +00001// Copyright 2012 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"
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +000032#include "zone.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000033
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.
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +000041class VariableMap: public ZoneHashMap {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000042 public:
mmassi@chromium.org7028c052012-06-13 11:51:58 +000043 explicit VariableMap(Zone* zone);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000044
kasperl@chromium.org68ac0092009-07-09 06:00:35 +000045 virtual ~VariableMap();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000046
kasperl@chromium.org68ac0092009-07-09 06:00:35 +000047 Variable* Declare(Scope* scope,
48 Handle<String> name,
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +000049 VariableMode mode,
kasperl@chromium.org68ac0092009-07-09 06:00:35 +000050 bool is_valid_lhs,
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +000051 Variable::Kind kind,
erik.corry@gmail.combbceb572012-03-09 10:52:05 +000052 InitializationFlag initialization_flag,
53 Interface* interface = Interface::NewValue());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000054
55 Variable* Lookup(Handle<String> name);
mmassi@chromium.org7028c052012-06-13 11:51:58 +000056
57 Zone* zone() const { return zone_; }
58
59 private:
60 Zone* zone_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000061};
62
63
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +000064// The dynamic scope part holds hash maps for the variables that will
65// be looked up dynamically from within eval and with scopes. The objects
66// are allocated on-demand from Scope::NonLocal to avoid wasting memory
67// and setup time for scopes that don't need them.
68class DynamicScopePart : public ZoneObject {
69 public:
mmassi@chromium.org7028c052012-06-13 11:51:58 +000070 explicit DynamicScopePart(Zone* zone) {
71 for (int i = 0; i < 3; i++)
72 maps_[i] = new(zone->New(sizeof(VariableMap))) VariableMap(zone);
73 }
74
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +000075 VariableMap* GetMap(VariableMode mode) {
76 int index = mode - DYNAMIC;
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +000077 ASSERT(index >= 0 && index < 3);
mmassi@chromium.org7028c052012-06-13 11:51:58 +000078 return maps_[index];
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +000079 }
80
81 private:
mmassi@chromium.org7028c052012-06-13 11:51:58 +000082 VariableMap *maps_[3];
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +000083};
84
85
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000086// Global invariants after AST construction: Each reference (i.e. identifier)
87// to a JavaScript variable (including global properties) is represented by a
88// VariableProxy node. Immediately after AST construction and before variable
89// allocation, most VariableProxy nodes are "unresolved", i.e. not bound to a
90// corresponding variable (though some are bound during parse time). Variable
91// allocation binds each unresolved VariableProxy to one Variable and assigns
92// a location. Note that many VariableProxy nodes may refer to the same Java-
93// Script variable.
94
95class Scope: public ZoneObject {
96 public:
97 // ---------------------------------------------------------------------------
98 // Construction
99
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000100 Scope(Scope* outer_scope, ScopeType type, Zone* zone);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000101
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
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000107 static Scope* DeserializeScopeChain(Context* context, Scope* global_scope,
108 Zone* zone);
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000109
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
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000113 void Initialize();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000114
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000115 // Checks if the block scope is redundant, i.e. it does not contain any
116 // block scoped declarations. In that case it is removed from the scope
117 // tree and its children are reparented.
118 Scope* FinalizeBlockScope();
119
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000120 Zone* zone() const { return zone_; }
121
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000122 // ---------------------------------------------------------------------------
123 // Declarations
124
125 // Lookup a variable in this scope. Returns the variable or NULL if not found.
ricow@chromium.orgc54d3652011-05-30 09:20:16 +0000126 Variable* LocalLookup(Handle<String> name);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000127
ricow@chromium.org27bf2882011-11-17 08:34:43 +0000128 // This lookup corresponds to a lookup in the "intermediate" scope sitting
129 // between this scope and the outer scope. (ECMA-262, 3rd., requires that
130 // the name of named function literal is kept in an intermediate scope
131 // in between this scope and the next outer scope.)
svenpanne@chromium.orgb1df11d2012-02-08 10:26:21 +0000132 Variable* LookupFunctionVar(Handle<String> name,
133 AstNodeFactory<AstNullVisitor>* factory);
ricow@chromium.org27bf2882011-11-17 08:34:43 +0000134
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000135 // Lookup a variable in this scope or outer scopes.
136 // Returns the variable or NULL if not found.
ricow@chromium.orgc54d3652011-05-30 09:20:16 +0000137 Variable* Lookup(Handle<String> name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000138
139 // Declare the function variable for a function literal. This variable
140 // is in an intermediate scope between this function scope and the the
141 // outer scope. Only possible for function scopes; at most one variable.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000142 void DeclareFunctionVar(VariableDeclaration* declaration) {
143 ASSERT(is_function_scope());
144 function_ = declaration;
svenpanne@chromium.orgb1df11d2012-02-08 10:26:21 +0000145 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000146
ricow@chromium.orgc54d3652011-05-30 09:20:16 +0000147 // Declare a parameter in this scope. When there are duplicated
148 // parameters the rightmost one 'wins'. However, the implementation
149 // expects all parameters to be declared and from left to right.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000150 void DeclareParameter(Handle<String> name, VariableMode mode);
ricow@chromium.orgc54d3652011-05-30 09:20:16 +0000151
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000152 // Declare a local variable in this scope. If the variable has been
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000153 // declared before, the previously declared variable is returned.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000154 Variable* DeclareLocal(Handle<String> name,
155 VariableMode mode,
erik.corry@gmail.combbceb572012-03-09 10:52:05 +0000156 InitializationFlag init_flag,
157 Interface* interface = Interface::NewValue());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000158
159 // Declare an implicit global variable in this scope which must be a
160 // global scope. The variable was introduced (possibly from an inner
161 // scope) by a reference to an unresolved variable with no intervening
162 // with statements or eval calls.
163 Variable* DeclareGlobal(Handle<String> name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000164
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000165 // Create a new unresolved variable.
svenpanne@chromium.orgb1df11d2012-02-08 10:26:21 +0000166 template<class Visitor>
167 VariableProxy* NewUnresolved(AstNodeFactory<Visitor>* factory,
168 Handle<String> name,
jkummerow@chromium.org28583c92012-07-16 11:31:55 +0000169 Interface* interface = Interface::NewValue(),
170 int position = RelocInfo::kNoPosition) {
svenpanne@chromium.orgb1df11d2012-02-08 10:26:21 +0000171 // Note that we must not share the unresolved variables with
172 // the same name because they may be removed selectively via
173 // RemoveUnresolved().
174 ASSERT(!already_resolved());
erik.corry@gmail.combbceb572012-03-09 10:52:05 +0000175 VariableProxy* proxy =
jkummerow@chromium.org28583c92012-07-16 11:31:55 +0000176 factory->NewVariableProxy(name, false, interface, position);
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000177 unresolved_.Add(proxy, zone_);
svenpanne@chromium.orgb1df11d2012-02-08 10:26:21 +0000178 return proxy;
179 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000180
181 // Remove a unresolved variable. During parsing, an unresolved variable
182 // may have been added optimistically, but then only the variable name
183 // was used (typically for labels). If the variable was not declared, the
184 // addition introduced a new unresolved variable which may end up being
185 // allocated globally as a "ghost" variable. RemoveUnresolved removes
186 // such a variable again if it was added; otherwise this is a no-op.
187 void RemoveUnresolved(VariableProxy* var);
188
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000189 // Creates a new temporary variable in this scope. The name is only used
190 // for printing and cannot be used to find the variable. In particular,
191 // the only way to get hold of the temporary is by keeping the Variable*
192 // around.
ricow@chromium.orgc54d3652011-05-30 09:20:16 +0000193 Variable* NewTemporary(Handle<String> name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000194
195 // Adds the specific declaration node to the list of declarations in
196 // this scope. The declarations are processed as part of entering
197 // the scope; see codegen.cc:ProcessDeclarations.
198 void AddDeclaration(Declaration* declaration);
199
200 // ---------------------------------------------------------------------------
201 // Illegal redeclaration support.
202
203 // Set an expression node that will be executed when the scope is
204 // entered. We only keep track of one illegal redeclaration node per
205 // scope - the first one - so if you try to set it multiple times
206 // the additional requests will be silently ignored.
207 void SetIllegalRedeclaration(Expression* expression);
208
209 // Visit the illegal redeclaration expression. Do not call if the
210 // scope doesn't have an illegal redeclaration node.
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000211 void VisitIllegalRedeclaration(AstVisitor* visitor);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000212
213 // Check if the scope has (at least) one illegal redeclaration.
214 bool HasIllegalRedeclaration() const { return illegal_redecl_ != NULL; }
215
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000216 // For harmony block scoping mode: Check if the scope has conflicting var
217 // declarations, i.e. a var declaration that has been hoisted from a nested
218 // scope over a let binding of the same name.
219 Declaration* CheckConflictingVarDeclarations();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000220
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000221 // For harmony block scoping mode: Check if the scope has variable proxies
222 // that are used as lvalues and point to const variables. Assumes that scopes
223 // have been analyzed and variables been resolved.
224 VariableProxy* CheckAssignmentToConst();
225
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000226 // ---------------------------------------------------------------------------
227 // Scope-specific info.
228
229 // Inform the scope that the corresponding code contains a with statement.
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000230 void RecordWithStatement() { scope_contains_with_ = true; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000231
232 // Inform the scope that the corresponding code contains an eval call.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000233 void RecordEvalCall() { if (!is_global_scope()) scope_calls_eval_ = true; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000234
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000235 // Set the strict mode flag (unless disabled by a global flag).
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000236 void SetLanguageMode(LanguageMode language_mode) {
237 language_mode_ = language_mode;
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000238 }
239
240 // Position in the source where this scope begins and ends.
241 //
242 // * For the scope of a with statement
243 // with (obj) stmt
244 // start position: start position of first token of 'stmt'
245 // end position: end position of last token of 'stmt'
246 // * For the scope of a block
247 // { stmts }
248 // start position: start position of '{'
249 // end position: end position of '}'
250 // * For the scope of a function literal or decalaration
251 // function fun(a,b) { stmts }
252 // start position: start position of '('
253 // end position: end position of '}'
254 // * For the scope of a catch block
255 // try { stms } catch(e) { stmts }
256 // start position: start position of '('
257 // end position: end position of ')'
258 // * For the scope of a for-statement
259 // for (let x ...) stmt
260 // start position: start position of '('
261 // end position: end position of last token of 'stmt'
262 int start_position() const { return start_position_; }
263 void set_start_position(int statement_pos) {
264 start_position_ = statement_pos;
265 }
266 int end_position() const { return end_position_; }
267 void set_end_position(int statement_pos) {
268 end_position_ = statement_pos;
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000269 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000270
271 // ---------------------------------------------------------------------------
272 // Predicates.
273
274 // Specific scope types.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000275 bool is_eval_scope() const { return type_ == EVAL_SCOPE; }
276 bool is_function_scope() const { return type_ == FUNCTION_SCOPE; }
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000277 bool is_module_scope() const { return type_ == MODULE_SCOPE; }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000278 bool is_global_scope() const { return type_ == GLOBAL_SCOPE; }
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000279 bool is_catch_scope() const { return type_ == CATCH_SCOPE; }
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000280 bool is_block_scope() const { return type_ == BLOCK_SCOPE; }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000281 bool is_with_scope() const { return type_ == WITH_SCOPE; }
282 bool is_declaration_scope() const {
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000283 return is_eval_scope() || is_function_scope() ||
284 is_module_scope() || is_global_scope();
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000285 }
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000286 bool is_classic_mode() const {
287 return language_mode() == CLASSIC_MODE;
288 }
289 bool is_extended_mode() const {
290 return language_mode() == EXTENDED_MODE;
291 }
292 bool is_strict_or_extended_eval_scope() const {
293 return is_eval_scope() && !is_classic_mode();
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000294 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000295
ager@chromium.org381abbb2009-02-25 13:23:22 +0000296 // Information about which scopes calls eval.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000297 bool calls_eval() const { return scope_calls_eval_; }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000298 bool calls_non_strict_eval() {
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000299 return scope_calls_eval_ && is_classic_mode();
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000300 }
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000301 bool outer_scope_calls_non_strict_eval() const {
302 return outer_scope_calls_non_strict_eval_;
303 }
ager@chromium.org381abbb2009-02-25 13:23:22 +0000304
305 // Is this scope inside a with statement.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000306 bool inside_with() const { return scope_inside_with_; }
ager@chromium.org381abbb2009-02-25 13:23:22 +0000307 // Does this scope contain a with statement.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000308 bool contains_with() const { return scope_contains_with_; }
ager@chromium.org381abbb2009-02-25 13:23:22 +0000309
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000310 // ---------------------------------------------------------------------------
311 // Accessors.
312
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000313 // The type of this scope.
314 ScopeType type() const { return type_; }
315
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000316 // The language mode of this scope.
317 LanguageMode language_mode() const { return language_mode_; }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000318
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000319 // The variable corresponding the 'this' value.
320 Variable* receiver() { return receiver_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000321
322 // The variable holding the function literal for named function
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000323 // literals, or NULL. Only valid for function scopes.
324 VariableDeclaration* function() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000325 ASSERT(is_function_scope());
326 return function_;
327 }
328
329 // Parameters. The left-most parameter has index 0.
330 // Only valid for function scopes.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000331 Variable* parameter(int index) const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000332 ASSERT(is_function_scope());
333 return params_[index];
334 }
335
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000336 int num_parameters() const { return params_.length(); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000337
338 // The local variable 'arguments' if we need to allocate it; NULL otherwise.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000339 Variable* arguments() const { return arguments_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000340
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000341 // Declarations list.
342 ZoneList<Declaration*>* declarations() { return &decls_; }
343
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000344 // Inner scope list.
345 ZoneList<Scope*>* inner_scopes() { return &inner_scopes_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000346
erik.corry@gmail.combbceb572012-03-09 10:52:05 +0000347 // The scope immediately surrounding this scope, or NULL.
348 Scope* outer_scope() const { return outer_scope_; }
349
350 // The interface as inferred so far; only for module scopes.
351 Interface* interface() const { return interface_; }
352
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000353 // ---------------------------------------------------------------------------
354 // Variable allocation.
355
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000356 // Collect stack and context allocated local variables in this scope. Note
357 // that the function variable - if present - is not collected and should be
358 // handled separately.
359 void CollectStackAndContextLocals(ZoneList<Variable*>* stack_locals,
360 ZoneList<Variable*>* context_locals);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000361
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000362 // Current number of var or const locals.
363 int num_var_or_const() { return num_var_or_const_; }
364
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000365 // Result of variable allocation.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000366 int num_stack_slots() const { return num_stack_slots_; }
367 int num_heap_slots() const { return num_heap_slots_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000368
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000369 int StackLocalCount() const;
370 int ContextLocalCount() const;
371
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000372 // Make sure this scope and all outer scopes are eagerly compiled.
373 void ForceEagerCompilation() { force_eager_compilation_ = true; }
374
375 // Determine if we can use lazy compilation for this scope.
376 bool AllowsLazyCompilation() const;
377
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000378 // Determine if we can use lazy compilation for this scope without a context.
379 bool AllowsLazyCompilationWithoutContext() const;
ulan@chromium.org812308e2012-02-29 15:58:45 +0000380
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000381 // True if the outer context of this scope is always the global context.
ricow@chromium.orgc54d3652011-05-30 09:20:16 +0000382 bool HasTrivialOuterContext() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000383
mstarzinger@chromium.orgc6d9cee2012-07-03 10:03:19 +0000384 // True if the outer context allows lazy compilation of this scope.
385 bool HasLazyCompilableOuterContext() const;
ulan@chromium.orgd6899c32012-05-18 14:12:25 +0000386
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000387 // The number of contexts between this and scope; zero if this == scope.
388 int ContextChainLength(Scope* scope);
389
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000390 // Find the first function, global, or eval scope. This is the scope
391 // where var declarations will be hoisted to in the implementation.
392 Scope* DeclarationScope();
393
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000394 Handle<ScopeInfo> GetScopeInfo();
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000395
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000396 // Get the chain of nested scopes within this scope for the source statement
397 // position. The scopes will be added to the list from the outermost scope to
398 // the innermost scope. Only nested block, catch or with scopes are tracked
399 // and will be returned, but no inner function scopes.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000400 void GetNestedScopeChain(List<Handle<ScopeInfo> >* chain,
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000401 int statement_position);
402
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000403 // ---------------------------------------------------------------------------
ager@chromium.org378b34e2011-01-28 08:04:38 +0000404 // Strict mode support.
405 bool IsDeclared(Handle<String> name) {
406 // During formal parameter list parsing the scope only contains
407 // two variables inserted at initialization: "this" and "arguments".
408 // "this" is an invalid parameter name and "arguments" is invalid parameter
409 // name in strict mode. Therefore looking up with the map which includes
410 // "this" and "arguments" in addition to all formal parameters is safe.
411 return variables_.Lookup(name) != NULL;
412 }
413
414 // ---------------------------------------------------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000415 // Debugging.
416
417#ifdef DEBUG
418 void Print(int n = 0); // n = indentation; n < 0 => don't print recursively
419#endif
420
421 // ---------------------------------------------------------------------------
422 // Implementation.
423 protected:
424 friend class ParserFactory;
425
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000426 Isolate* const isolate_;
427
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000428 // Scope tree.
429 Scope* outer_scope_; // the immediately enclosing outer scope, or NULL
430 ZoneList<Scope*> inner_scopes_; // the immediately enclosed inner scopes
431
432 // The scope type.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000433 ScopeType type_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000434
435 // Debugging support.
436 Handle<String> scope_name_;
437
438 // The variables declared in this scope:
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000439 //
440 // All user-declared variables (incl. parameters). For global scopes
441 // variables may be implicitly 'declared' by being used (possibly in
442 // an inner scope) with no intervening with statements or eval calls.
443 VariableMap variables_;
444 // Compiler-allocated (user-invisible) temporaries.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000445 ZoneList<Variable*> temps_;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000446 // Parameter list in source order.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000447 ZoneList<Variable*> params_;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000448 // Variables that must be looked up dynamically.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000449 DynamicScopePart* dynamics_;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000450 // Unresolved variables referred to from this scope.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000451 ZoneList<VariableProxy*> unresolved_;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000452 // Declarations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000453 ZoneList<Declaration*> decls_;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000454 // Convenience variable.
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000455 Variable* receiver_;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000456 // Function variable, if any; function scopes only.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000457 VariableDeclaration* function_;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000458 // Convenience variable; function scopes only.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000459 Variable* arguments_;
erik.corry@gmail.combbceb572012-03-09 10:52:05 +0000460 // Interface; module scopes only.
461 Interface* interface_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000462
463 // Illegal redeclaration.
464 Expression* illegal_redecl_;
465
vegorov@chromium.org7943d462011-08-01 11:41:52 +0000466 // Scope-specific information computed during parsing.
467 //
468 // This scope is inside a 'with' of some outer scope.
469 bool scope_inside_with_;
470 // This scope contains a 'with' statement.
471 bool scope_contains_with_;
472 // This scope or a nested catch scope or with scope contain an 'eval' call. At
473 // the 'eval' call site this scope is the declaration scope.
474 bool scope_calls_eval_;
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000475 // The language mode of this scope.
476 LanguageMode language_mode_;
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000477 // Source positions.
478 int start_position_;
479 int end_position_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000480
481 // Computed via PropagateScopeInfo.
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000482 bool outer_scope_calls_non_strict_eval_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000483 bool inner_scope_calls_eval_;
484 bool force_eager_compilation_;
485
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000486 // True if it doesn't need scope resolution (e.g., if the scope was
487 // constructed based on a serialized scope info or a catch context).
488 bool already_resolved_;
489
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000490 // Computed as variables are declared.
491 int num_var_or_const_;
492
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000493 // Computed via AllocateVariables; function, block and catch scopes only.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000494 int num_stack_slots_;
495 int num_heap_slots_;
496
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000497 // Serialized scope info support.
498 Handle<ScopeInfo> scope_info_;
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000499 bool already_resolved() { return already_resolved_; }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000500
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000501 // Create a non-local variable with a given name.
502 // These variables are looked up dynamically at runtime.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000503 Variable* NonLocal(Handle<String> name, VariableMode mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000504
505 // Variable resolution.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000506 // Possible results of a recursive variable lookup telling if and how a
507 // variable is bound. These are returned in the output parameter *binding_kind
508 // of the LookupRecursive function.
509 enum BindingKind {
510 // The variable reference could be statically resolved to a variable binding
511 // which is returned. There is no 'with' statement between the reference and
512 // the binding and no scope between the reference scope (inclusive) and
513 // binding scope (exclusive) makes a non-strict 'eval' call.
514 BOUND,
515
516 // The variable reference could be statically resolved to a variable binding
517 // which is returned. There is no 'with' statement between the reference and
518 // the binding, but some scope between the reference scope (inclusive) and
519 // binding scope (exclusive) makes a non-strict 'eval' call, that might
520 // possibly introduce variable bindings shadowing the found one. Thus the
521 // found variable binding is just a guess.
522 BOUND_EVAL_SHADOWED,
523
524 // The variable reference could not be statically resolved to any binding
525 // and thus should be considered referencing a global variable. NULL is
526 // returned. The variable reference is not inside any 'with' statement and
527 // no scope between the reference scope (inclusive) and global scope
528 // (exclusive) makes a non-strict 'eval' call.
529 UNBOUND,
530
531 // The variable reference could not be statically resolved to any binding
532 // NULL is returned. The variable reference is not inside any 'with'
533 // statement, but some scope between the reference scope (inclusive) and
534 // global scope (exclusive) makes a non-strict 'eval' call, that might
535 // possibly introduce a variable binding. Thus the reference should be
536 // considered referencing a global variable unless it is shadowed by an
537 // 'eval' introduced binding.
538 UNBOUND_EVAL_SHADOWED,
539
540 // The variable could not be statically resolved and needs to be looked up
541 // dynamically. NULL is returned. There are two possible reasons:
542 // * A 'with' statement has been encountered and there is no variable
543 // binding for the name between the variable reference and the 'with'.
544 // The variable potentially references a property of the 'with' object.
545 // * The code is being executed as part of a call to 'eval' and the calling
546 // context chain contains either a variable binding for the name or it
547 // contains a 'with' context.
548 DYNAMIC_LOOKUP
549 };
550
551 // Lookup a variable reference given by name recursively starting with this
552 // scope. If the code is executed because of a call to 'eval', the context
553 // parameter should be set to the calling context of 'eval'.
ager@chromium.org381abbb2009-02-25 13:23:22 +0000554 Variable* LookupRecursive(Handle<String> name,
svenpanne@chromium.orgb1df11d2012-02-08 10:26:21 +0000555 BindingKind* binding_kind,
556 AstNodeFactory<AstNullVisitor>* factory);
erik.corry@gmail.combbceb572012-03-09 10:52:05 +0000557 MUST_USE_RESULT
558 bool ResolveVariable(CompilationInfo* info,
svenpanne@chromium.orgb1df11d2012-02-08 10:26:21 +0000559 VariableProxy* proxy,
560 AstNodeFactory<AstNullVisitor>* factory);
erik.corry@gmail.combbceb572012-03-09 10:52:05 +0000561 MUST_USE_RESULT
562 bool ResolveVariablesRecursively(CompilationInfo* info,
svenpanne@chromium.orgb1df11d2012-02-08 10:26:21 +0000563 AstNodeFactory<AstNullVisitor>* factory);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000564
565 // Scope analysis.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000566 bool PropagateScopeInfo(bool outer_scope_calls_non_strict_eval);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000567 bool HasTrivialContext() const;
568
569 // Predicates.
570 bool MustAllocate(Variable* var);
571 bool MustAllocateInContext(Variable* var);
572 bool HasArgumentsParameter();
573
574 // Variable allocation.
575 void AllocateStackSlot(Variable* var);
576 void AllocateHeapSlot(Variable* var);
577 void AllocateParameterLocals();
578 void AllocateNonParameterLocal(Variable* var);
579 void AllocateNonParameterLocals();
580 void AllocateVariablesRecursively();
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000581
erik.corry@gmail.combbceb572012-03-09 10:52:05 +0000582 // Resolve and fill in the allocation information for all variables
583 // in this scopes. Must be called *after* all scopes have been
584 // processed (parsed) to ensure that unresolved variables can be
585 // resolved properly.
586 //
587 // In the case of code compiled and run using 'eval', the context
588 // parameter is the context in which eval was called. In all other
589 // cases the context parameter is an empty handle.
590 MUST_USE_RESULT
591 bool AllocateVariables(CompilationInfo* info,
592 AstNodeFactory<AstNullVisitor>* factory);
593
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000594 // Instance objects have to be created ahead of time (before code generation)
595 // because of potentially cyclic references between them.
596 // Linking also has to be a separate stage, since populating one object may
597 // potentially require (forward) references to others.
598 void AllocateModules(CompilationInfo* info);
599 void LinkModules(CompilationInfo* info);
600
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000601 private:
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000602 // Construct a scope based on the scope info.
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000603 Scope(Scope* inner_scope, ScopeType type, Handle<ScopeInfo> scope_info,
604 Zone* zone);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000605
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000606 // Construct a catch scope with a binding for the name.
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000607 Scope(Scope* inner_scope, Handle<String> catch_variable_name, Zone* zone);
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000608
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000609 void AddInnerScope(Scope* inner_scope) {
610 if (inner_scope != NULL) {
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000611 inner_scopes_.Add(inner_scope, zone_);
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000612 inner_scope->outer_scope_ = this;
613 }
614 }
615
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000616 void SetDefaults(ScopeType type,
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000617 Scope* outer_scope,
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000618 Handle<ScopeInfo> scope_info);
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000619
620 Zone* zone_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000621};
622
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000623} } // namespace v8::internal
624
625#endif // V8_SCOPES_H_