Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1694)

Side by Side Diff: src/scopes.h

Issue 7348008: Merge up to 8597 to experimental/gc from the bleeding edge. (Closed) Base URL: http://v8.googlecode.com/svn/branches/experimental/gc/
Patch Set: '' Created 9 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/scopeinfo.h ('k') | src/scopes.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2010 the V8 project authors. All rights reserved. 1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution. 11 // with the distribution.
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
83 // allocation binds each unresolved VariableProxy to one Variable and assigns 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- 84 // a location. Note that many VariableProxy nodes may refer to the same Java-
85 // Script variable. 85 // Script variable.
86 86
87 class Scope: public ZoneObject { 87 class Scope: public ZoneObject {
88 public: 88 public:
89 // --------------------------------------------------------------------------- 89 // ---------------------------------------------------------------------------
90 // Construction 90 // Construction
91 91
92 enum Type { 92 enum Type {
93 EVAL_SCOPE, // the top-level scope for an 'eval' source 93 EVAL_SCOPE, // The top-level scope for an eval source.
94 FUNCTION_SCOPE, // the top-level scope for a function 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 95 GLOBAL_SCOPE, // The top-level scope for a program or a top-level eval.
96 }; 96 CATCH_SCOPE // The scope introduced by catch.
97
98 enum LocalType {
99 PARAMETER,
100 VAR_OR_CONST
101 }; 97 };
102 98
103 Scope(Scope* outer_scope, Type type); 99 Scope(Scope* outer_scope, Type type);
104 100
105 virtual ~Scope() { }
106
107 // Compute top scope and allocate variables. For lazy compilation the top 101 // Compute top scope and allocate variables. For lazy compilation the top
108 // scope only contains the single lazily compiled function, so this 102 // scope only contains the single lazily compiled function, so this
109 // doesn't re-allocate variables repeatedly. 103 // doesn't re-allocate variables repeatedly.
110 static bool Analyze(CompilationInfo* info); 104 static bool Analyze(CompilationInfo* info);
111 105
112 static Scope* DeserializeScopeChain(CompilationInfo* info, 106 static Scope* DeserializeScopeChain(CompilationInfo* info,
113 Scope* innermost_scope); 107 Scope* innermost_scope);
114 108
115 // The scope name is only used for printing/debugging. 109 // The scope name is only used for printing/debugging.
116 void SetScopeName(Handle<String> scope_name) { scope_name_ = scope_name; } 110 void SetScopeName(Handle<String> scope_name) { scope_name_ = scope_name; }
117 111
118 virtual void Initialize(bool inside_with); 112 void Initialize(bool inside_with);
119
120 // Called just before leaving a scope.
121 virtual void Leave() {
122 // No cleanup or fixup necessary.
123 }
124 113
125 // --------------------------------------------------------------------------- 114 // ---------------------------------------------------------------------------
126 // Declarations 115 // Declarations
127 116
128 // Lookup a variable in this scope. Returns the variable or NULL if not found. 117 // Lookup a variable in this scope. Returns the variable or NULL if not found.
129 virtual Variable* LocalLookup(Handle<String> name); 118 Variable* LocalLookup(Handle<String> name);
130 119
131 // Lookup a variable in this scope or outer scopes. 120 // Lookup a variable in this scope or outer scopes.
132 // Returns the variable or NULL if not found. 121 // Returns the variable or NULL if not found.
133 virtual Variable* Lookup(Handle<String> name); 122 Variable* Lookup(Handle<String> name);
134 123
135 // Declare the function variable for a function literal. This variable 124 // Declare the function variable for a function literal. This variable
136 // is in an intermediate scope between this function scope and the the 125 // is in an intermediate scope between this function scope and the the
137 // outer scope. Only possible for function scopes; at most one variable. 126 // outer scope. Only possible for function scopes; at most one variable.
138 Variable* DeclareFunctionVar(Handle<String> name); 127 Variable* DeclareFunctionVar(Handle<String> name);
139 128
129 // Declare a parameter in this scope. When there are duplicated
130 // parameters the rightmost one 'wins'. However, the implementation
131 // expects all parameters to be declared and from left to right.
132 void DeclareParameter(Handle<String> name);
133
140 // Declare a local variable in this scope. If the variable has been 134 // Declare a local variable in this scope. If the variable has been
141 // declared before, the previously declared variable is returned. 135 // declared before, the previously declared variable is returned.
142 virtual Variable* DeclareLocal(Handle<String> name, 136 Variable* DeclareLocal(Handle<String> name, Variable::Mode mode);
143 Variable::Mode mode,
144 LocalType type);
145 137
146 // Declare an implicit global variable in this scope which must be a 138 // Declare an implicit global variable in this scope which must be a
147 // global scope. The variable was introduced (possibly from an inner 139 // global scope. The variable was introduced (possibly from an inner
148 // scope) by a reference to an unresolved variable with no intervening 140 // scope) by a reference to an unresolved variable with no intervening
149 // with statements or eval calls. 141 // with statements or eval calls.
150 Variable* DeclareGlobal(Handle<String> name); 142 Variable* DeclareGlobal(Handle<String> name);
151 143
152 // Add a parameter to the parameter list. The parameter must have been
153 // declared via Declare. The same parameter may occur more than once in
154 // the parameter list; they must be added in source order, from left to
155 // right.
156 void AddParameter(Variable* var);
157
158 // Create a new unresolved variable. 144 // Create a new unresolved variable.
159 virtual VariableProxy* NewUnresolved(Handle<String> name, 145 VariableProxy* NewUnresolved(Handle<String> name,
160 bool inside_with, 146 bool inside_with,
161 int position = RelocInfo::kNoPosition); 147 int position = RelocInfo::kNoPosition);
162 148
163 // Remove a unresolved variable. During parsing, an unresolved variable 149 // Remove a unresolved variable. During parsing, an unresolved variable
164 // may have been added optimistically, but then only the variable name 150 // may have been added optimistically, but then only the variable name
165 // was used (typically for labels). If the variable was not declared, the 151 // was used (typically for labels). If the variable was not declared, the
166 // addition introduced a new unresolved variable which may end up being 152 // addition introduced a new unresolved variable which may end up being
167 // allocated globally as a "ghost" variable. RemoveUnresolved removes 153 // allocated globally as a "ghost" variable. RemoveUnresolved removes
168 // such a variable again if it was added; otherwise this is a no-op. 154 // such a variable again if it was added; otherwise this is a no-op.
169 void RemoveUnresolved(VariableProxy* var); 155 void RemoveUnresolved(VariableProxy* var);
170 156
171 // Creates a new temporary variable in this scope. The name is only used 157 // Creates a new temporary variable in this scope. The name is only used
172 // for printing and cannot be used to find the variable. In particular, 158 // for printing and cannot be used to find the variable. In particular,
173 // the only way to get hold of the temporary is by keeping the Variable* 159 // the only way to get hold of the temporary is by keeping the Variable*
174 // around. 160 // around.
175 virtual Variable* NewTemporary(Handle<String> name); 161 Variable* NewTemporary(Handle<String> name);
176 162
177 // Adds the specific declaration node to the list of declarations in 163 // Adds the specific declaration node to the list of declarations in
178 // this scope. The declarations are processed as part of entering 164 // this scope. The declarations are processed as part of entering
179 // the scope; see codegen.cc:ProcessDeclarations. 165 // the scope; see codegen.cc:ProcessDeclarations.
180 void AddDeclaration(Declaration* declaration); 166 void AddDeclaration(Declaration* declaration);
181 167
182 // --------------------------------------------------------------------------- 168 // ---------------------------------------------------------------------------
183 // Illegal redeclaration support. 169 // Illegal redeclaration support.
184 170
185 // Set an expression node that will be executed when the scope is 171 // Set an expression node that will be executed when the scope is
(...skipping 24 matching lines...) Expand all
210 strict_mode_ = FLAG_strict_mode; 196 strict_mode_ = FLAG_strict_mode;
211 } 197 }
212 198
213 // --------------------------------------------------------------------------- 199 // ---------------------------------------------------------------------------
214 // Predicates. 200 // Predicates.
215 201
216 // Specific scope types. 202 // Specific scope types.
217 bool is_eval_scope() const { return type_ == EVAL_SCOPE; } 203 bool is_eval_scope() const { return type_ == EVAL_SCOPE; }
218 bool is_function_scope() const { return type_ == FUNCTION_SCOPE; } 204 bool is_function_scope() const { return type_ == FUNCTION_SCOPE; }
219 bool is_global_scope() const { return type_ == GLOBAL_SCOPE; } 205 bool is_global_scope() const { return type_ == GLOBAL_SCOPE; }
206 bool is_catch_scope() const { return type_ == CATCH_SCOPE; }
220 bool is_strict_mode() const { return strict_mode_; } 207 bool is_strict_mode() const { return strict_mode_; }
221 bool is_strict_mode_eval_scope() const { 208 bool is_strict_mode_eval_scope() const {
222 return is_eval_scope() && is_strict_mode(); 209 return is_eval_scope() && is_strict_mode();
223 } 210 }
224 211
225 // Information about which scopes calls eval. 212 // Information about which scopes calls eval.
226 bool calls_eval() const { return scope_calls_eval_; } 213 bool calls_eval() const { return scope_calls_eval_; }
227 bool outer_scope_calls_eval() const { return outer_scope_calls_eval_; } 214 bool outer_scope_calls_eval() const { return outer_scope_calls_eval_; }
228 bool outer_scope_calls_non_strict_eval() const { 215 bool outer_scope_calls_non_strict_eval() const {
229 return outer_scope_calls_non_strict_eval_; 216 return outer_scope_calls_non_strict_eval_;
230 } 217 }
231 218
232 // Is this scope inside a with statement. 219 // Is this scope inside a with statement.
233 bool inside_with() const { return scope_inside_with_; } 220 bool inside_with() const { return scope_inside_with_; }
234 // Does this scope contain a with statement. 221 // Does this scope contain a with statement.
235 bool contains_with() const { return scope_contains_with_; } 222 bool contains_with() const { return scope_contains_with_; }
236 223
237 // The scope immediately surrounding this scope, or NULL. 224 // The scope immediately surrounding this scope, or NULL.
238 Scope* outer_scope() const { return outer_scope_; } 225 Scope* outer_scope() const { return outer_scope_; }
239 226
240 // --------------------------------------------------------------------------- 227 // ---------------------------------------------------------------------------
241 // Accessors. 228 // Accessors.
242 229
243 // A new variable proxy corresponding to the (function) receiver. 230 // The variable corresponding the 'this' value.
244 VariableProxy* receiver() const { 231 Variable* receiver() { return receiver_; }
245 VariableProxy* proxy =
246 new VariableProxy(FACTORY->this_symbol(), true, false);
247 proxy->BindTo(receiver_);
248 return proxy;
249 }
250 232
251 // The variable holding the function literal for named function 233 // The variable holding the function literal for named function
252 // literals, or NULL. 234 // literals, or NULL.
253 // Only valid for function scopes. 235 // Only valid for function scopes.
254 Variable* function() const { 236 Variable* function() const {
255 ASSERT(is_function_scope()); 237 ASSERT(is_function_scope());
256 return function_; 238 return function_;
257 } 239 }
258 240
259 // Parameters. The left-most parameter has index 0. 241 // Parameters. The left-most parameter has index 0.
260 // Only valid for function scopes. 242 // Only valid for function scopes.
261 Variable* parameter(int index) const { 243 Variable* parameter(int index) const {
262 ASSERT(is_function_scope()); 244 ASSERT(is_function_scope());
263 return params_[index]; 245 return params_[index];
264 } 246 }
265 247
266 int num_parameters() const { return params_.length(); } 248 int num_parameters() const { return params_.length(); }
267 249
268 // The local variable 'arguments' if we need to allocate it; NULL otherwise. 250 // The local variable 'arguments' if we need to allocate it; NULL otherwise.
269 // If arguments() exist, arguments_shadow() exists, too.
270 Variable* arguments() const { return arguments_; } 251 Variable* arguments() const { return arguments_; }
271 252
272 // The '.arguments' shadow variable if we need to allocate it; NULL otherwise.
273 // If arguments_shadow() exist, arguments() exists, too.
274 Variable* arguments_shadow() const { return arguments_shadow_; }
275
276 // Declarations list. 253 // Declarations list.
277 ZoneList<Declaration*>* declarations() { return &decls_; } 254 ZoneList<Declaration*>* declarations() { return &decls_; }
278 255
279 256
280
281 // --------------------------------------------------------------------------- 257 // ---------------------------------------------------------------------------
282 // Variable allocation. 258 // Variable allocation.
283 259
284 // Collect all used locals in this scope. 260 // Collect all used locals in this scope.
285 template<class Allocator> 261 template<class Allocator>
286 void CollectUsedVariables(List<Variable*, Allocator>* locals); 262 void CollectUsedVariables(List<Variable*, Allocator>* locals);
287 263
288 // Resolve and fill in the allocation information for all variables 264 // Resolve and fill in the allocation information for all variables
289 // in this scopes. Must be called *after* all scopes have been 265 // in this scopes. Must be called *after* all scopes have been
290 // processed (parsed) to ensure that unresolved variables can be 266 // processed (parsed) to ensure that unresolved variables can be
(...skipping 11 matching lines...) Expand all
302 int num_stack_slots() const { return num_stack_slots_; } 278 int num_stack_slots() const { return num_stack_slots_; }
303 int num_heap_slots() const { return num_heap_slots_; } 279 int num_heap_slots() const { return num_heap_slots_; }
304 280
305 // Make sure this scope and all outer scopes are eagerly compiled. 281 // Make sure this scope and all outer scopes are eagerly compiled.
306 void ForceEagerCompilation() { force_eager_compilation_ = true; } 282 void ForceEagerCompilation() { force_eager_compilation_ = true; }
307 283
308 // Determine if we can use lazy compilation for this scope. 284 // Determine if we can use lazy compilation for this scope.
309 bool AllowsLazyCompilation() const; 285 bool AllowsLazyCompilation() const;
310 286
311 // True if the outer context of this scope is always the global context. 287 // True if the outer context of this scope is always the global context.
312 virtual bool HasTrivialOuterContext() const; 288 bool HasTrivialOuterContext() const;
313 289
314 // The number of contexts between this and scope; zero if this == scope. 290 // The number of contexts between this and scope; zero if this == scope.
315 int ContextChainLength(Scope* scope); 291 int ContextChainLength(Scope* scope);
316 292
293 // Find the first function, global, or eval scope. This is the scope
294 // where var declarations will be hoisted to in the implementation.
295 Scope* DeclarationScope();
296
317 // --------------------------------------------------------------------------- 297 // ---------------------------------------------------------------------------
318 // Strict mode support. 298 // Strict mode support.
319 bool IsDeclared(Handle<String> name) { 299 bool IsDeclared(Handle<String> name) {
320 // During formal parameter list parsing the scope only contains 300 // During formal parameter list parsing the scope only contains
321 // two variables inserted at initialization: "this" and "arguments". 301 // two variables inserted at initialization: "this" and "arguments".
322 // "this" is an invalid parameter name and "arguments" is invalid parameter 302 // "this" is an invalid parameter name and "arguments" is invalid parameter
323 // name in strict mode. Therefore looking up with the map which includes 303 // name in strict mode. Therefore looking up with the map which includes
324 // "this" and "arguments" in addition to all formal parameters is safe. 304 // "this" and "arguments" in addition to all formal parameters is safe.
325 return variables_.Lookup(name) != NULL; 305 return variables_.Lookup(name) != NULL;
326 } 306 }
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
364 // Unresolved variables referred to from this scope. 344 // Unresolved variables referred to from this scope.
365 ZoneList<VariableProxy*> unresolved_; 345 ZoneList<VariableProxy*> unresolved_;
366 // Declarations. 346 // Declarations.
367 ZoneList<Declaration*> decls_; 347 ZoneList<Declaration*> decls_;
368 // Convenience variable. 348 // Convenience variable.
369 Variable* receiver_; 349 Variable* receiver_;
370 // Function variable, if any; function scopes only. 350 // Function variable, if any; function scopes only.
371 Variable* function_; 351 Variable* function_;
372 // Convenience variable; function scopes only. 352 // Convenience variable; function scopes only.
373 Variable* arguments_; 353 Variable* arguments_;
374 // Convenience variable; function scopes only.
375 Variable* arguments_shadow_;
376 354
377 // Illegal redeclaration. 355 // Illegal redeclaration.
378 Expression* illegal_redecl_; 356 Expression* illegal_redecl_;
379 357
380 // Scope-specific information. 358 // Scope-specific information.
381 bool scope_inside_with_; // this scope is inside a 'with' of some outer scope 359 bool scope_inside_with_; // this scope is inside a 'with' of some outer scope
382 bool scope_contains_with_; // this scope contains a 'with' statement 360 bool scope_contains_with_; // this scope contains a 'with' statement
383 bool scope_calls_eval_; // this scope contains an 'eval' call 361 bool scope_calls_eval_; // this scope contains an 'eval' call
384 bool strict_mode_; // this scope is a strict mode scope 362 bool strict_mode_; // this scope is a strict mode scope
385 363
386 // Computed via PropagateScopeInfo. 364 // Computed via PropagateScopeInfo.
387 bool outer_scope_calls_eval_; 365 bool outer_scope_calls_eval_;
388 bool outer_scope_calls_non_strict_eval_; 366 bool outer_scope_calls_non_strict_eval_;
389 bool inner_scope_calls_eval_; 367 bool inner_scope_calls_eval_;
390 bool outer_scope_is_eval_scope_; 368 bool outer_scope_is_eval_scope_;
391 bool force_eager_compilation_; 369 bool force_eager_compilation_;
392 370
371 // True if it doesn't need scope resolution (e.g., if the scope was
372 // constructed based on a serialized scope info or a catch context).
373 bool already_resolved_;
374
393 // Computed as variables are declared. 375 // Computed as variables are declared.
394 int num_var_or_const_; 376 int num_var_or_const_;
395 377
396 // Computed via AllocateVariables; function scopes only. 378 // Computed via AllocateVariables; function scopes only.
397 int num_stack_slots_; 379 int num_stack_slots_;
398 int num_heap_slots_; 380 int num_heap_slots_;
399 381
400 // Serialized scopes support. 382 // Serialized scopes support.
401 Handle<SerializedScopeInfo> scope_info_; 383 Handle<SerializedScopeInfo> scope_info_;
402 bool resolved() { return !scope_info_.is_null(); } 384 bool already_resolved() { return already_resolved_; }
403 385
404 // Create a non-local variable with a given name. 386 // Create a non-local variable with a given name.
405 // These variables are looked up dynamically at runtime. 387 // These variables are looked up dynamically at runtime.
406 Variable* NonLocal(Handle<String> name, Variable::Mode mode); 388 Variable* NonLocal(Handle<String> name, Variable::Mode mode);
407 389
408 // Variable resolution. 390 // Variable resolution.
409 Variable* LookupRecursive(Handle<String> name, 391 Variable* LookupRecursive(Handle<String> name,
410 bool inner_lookup, 392 bool inner_lookup,
411 Variable** invalidated_local); 393 Variable** invalidated_local);
412 void ResolveVariable(Scope* global_scope, 394 void ResolveVariable(Scope* global_scope,
(...skipping 15 matching lines...) Expand all
428 410
429 // Variable allocation. 411 // Variable allocation.
430 void AllocateStackSlot(Variable* var); 412 void AllocateStackSlot(Variable* var);
431 void AllocateHeapSlot(Variable* var); 413 void AllocateHeapSlot(Variable* var);
432 void AllocateParameterLocals(); 414 void AllocateParameterLocals();
433 void AllocateNonParameterLocal(Variable* var); 415 void AllocateNonParameterLocal(Variable* var);
434 void AllocateNonParameterLocals(); 416 void AllocateNonParameterLocals();
435 void AllocateVariablesRecursively(); 417 void AllocateVariablesRecursively();
436 418
437 private: 419 private:
420 // Construct a function scope based on the scope info.
438 Scope(Scope* inner_scope, Handle<SerializedScopeInfo> scope_info); 421 Scope(Scope* inner_scope, Handle<SerializedScopeInfo> scope_info);
439 422
423 // Construct a catch scope with a binding for the name.
424 Scope(Scope* inner_scope, Handle<String> catch_variable_name);
425
440 void AddInnerScope(Scope* inner_scope) { 426 void AddInnerScope(Scope* inner_scope) {
441 if (inner_scope != NULL) { 427 if (inner_scope != NULL) {
442 inner_scopes_.Add(inner_scope); 428 inner_scopes_.Add(inner_scope);
443 inner_scope->outer_scope_ = this; 429 inner_scope->outer_scope_ = this;
444 } 430 }
445 } 431 }
446 432
447 void SetDefaults(Type type, 433 void SetDefaults(Type type,
448 Scope* outer_scope, 434 Scope* outer_scope,
449 Handle<SerializedScopeInfo> scope_info); 435 Handle<SerializedScopeInfo> scope_info);
450 }; 436 };
451 437
452
453 // Scope used during pre-parsing.
454 class DummyScope : public Scope {
455 public:
456 DummyScope()
457 : Scope(GLOBAL_SCOPE),
458 nesting_level_(1), // Allows us to Leave the initial scope.
459 inside_with_level_(kNotInsideWith) {
460 outer_scope_ = this;
461 scope_inside_with_ = false;
462 }
463
464 virtual void Initialize(bool inside_with) {
465 nesting_level_++;
466 if (inside_with && inside_with_level_ == kNotInsideWith) {
467 inside_with_level_ = nesting_level_;
468 }
469 ASSERT(inside_with_level_ <= nesting_level_);
470 }
471
472 virtual void Leave() {
473 nesting_level_--;
474 ASSERT(nesting_level_ >= 0);
475 if (nesting_level_ < inside_with_level_) {
476 inside_with_level_ = kNotInsideWith;
477 }
478 ASSERT(inside_with_level_ <= nesting_level_);
479 }
480
481 virtual Variable* Lookup(Handle<String> name) { return NULL; }
482
483 virtual VariableProxy* NewUnresolved(Handle<String> name,
484 bool inside_with,
485 int position = RelocInfo::kNoPosition) {
486 return NULL;
487 }
488
489 virtual Variable* NewTemporary(Handle<String> name) { return NULL; }
490
491 virtual bool HasTrivialOuterContext() const {
492 return (nesting_level_ == 0 || inside_with_level_ <= 0);
493 }
494
495 private:
496 static const int kNotInsideWith = -1;
497 // Number of surrounding scopes of the current scope.
498 int nesting_level_;
499 // Nesting level of outermost scope that is contained in a with statement,
500 // or kNotInsideWith if there are no with's around the current scope.
501 int inside_with_level_;
502 };
503
504
505 } } // namespace v8::internal 438 } } // namespace v8::internal
506 439
507 #endif // V8_SCOPES_H_ 440 #endif // V8_SCOPES_H_
OLDNEW
« no previous file with comments | « src/scopeinfo.h ('k') | src/scopes.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698