OLD | NEW |
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 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 |
(...skipping 16 matching lines...) Expand all Loading... |
27 | 27 |
28 #ifndef V8_SCOPES_H_ | 28 #ifndef V8_SCOPES_H_ |
29 #define V8_SCOPES_H_ | 29 #define V8_SCOPES_H_ |
30 | 30 |
31 #include "ast.h" | 31 #include "ast.h" |
32 #include "hashmap.h" | 32 #include "hashmap.h" |
33 | 33 |
34 namespace v8 { | 34 namespace v8 { |
35 namespace internal { | 35 namespace internal { |
36 | 36 |
37 | |
38 // A hash map to support fast variable declaration and lookup. | 37 // A hash map to support fast variable declaration and lookup. |
39 class VariableMap: public HashMap { | 38 class VariableMap: public HashMap { |
40 public: | 39 public: |
41 VariableMap(); | 40 VariableMap(); |
42 | 41 |
43 // Dummy constructor. This constructor doesn't set up the map | 42 // Dummy constructor. This constructor doesn't set up the map |
44 // properly so don't use it unless you have a good reason. | 43 // properly so don't use it unless you have a good reason. |
45 explicit VariableMap(bool gotta_love_static_overloading); | 44 explicit VariableMap(bool gotta_love_static_overloading); |
46 | 45 |
47 virtual ~VariableMap(); | 46 virtual ~VariableMap(); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 GLOBAL_SCOPE // the top-level scope for a program or a top-level eval | 92 GLOBAL_SCOPE // the top-level scope for a program or a top-level eval |
94 }; | 93 }; |
95 | 94 |
96 Scope(Scope* outer_scope, Type type); | 95 Scope(Scope* outer_scope, Type type); |
97 | 96 |
98 virtual ~Scope() { } | 97 virtual ~Scope() { } |
99 | 98 |
100 // The scope name is only used for printing/debugging. | 99 // The scope name is only used for printing/debugging. |
101 void SetScopeName(Handle<String> scope_name) { scope_name_ = scope_name; } | 100 void SetScopeName(Handle<String> scope_name) { scope_name_ = scope_name; } |
102 | 101 |
103 void Initialize(bool inside_with); | 102 virtual void Initialize(bool inside_with); |
104 | 103 |
| 104 // Called just before leaving a scope. |
| 105 virtual void Leave() { |
| 106 // No cleanup or fixup necessary. |
| 107 } |
105 | 108 |
106 // --------------------------------------------------------------------------- | 109 // --------------------------------------------------------------------------- |
107 // Declarations | 110 // Declarations |
108 | 111 |
109 // Lookup a variable in this scope. Returns the variable or NULL if not found. | 112 // Lookup a variable in this scope. Returns the variable or NULL if not found. |
110 virtual Variable* LocalLookup(Handle<String> name); | 113 virtual Variable* LocalLookup(Handle<String> name); |
111 | 114 |
112 // Lookup a variable in this scope or outer scopes. | 115 // Lookup a variable in this scope or outer scopes. |
113 // Returns the variable or NULL if not found. | 116 // Returns the variable or NULL if not found. |
114 virtual Variable* Lookup(Handle<String> name); | 117 virtual Variable* Lookup(Handle<String> name); |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 // Information about which scopes calls eval. | 197 // Information about which scopes calls eval. |
195 bool calls_eval() const { return scope_calls_eval_; } | 198 bool calls_eval() const { return scope_calls_eval_; } |
196 bool outer_scope_calls_eval() const { return outer_scope_calls_eval_; } | 199 bool outer_scope_calls_eval() const { return outer_scope_calls_eval_; } |
197 | 200 |
198 // Is this scope inside a with statement. | 201 // Is this scope inside a with statement. |
199 bool inside_with() const { return scope_inside_with_; } | 202 bool inside_with() const { return scope_inside_with_; } |
200 // Does this scope contain a with statement. | 203 // Does this scope contain a with statement. |
201 bool contains_with() const { return scope_contains_with_; } | 204 bool contains_with() const { return scope_contains_with_; } |
202 | 205 |
203 // The scope immediately surrounding this scope, or NULL. | 206 // The scope immediately surrounding this scope, or NULL. |
204 Scope* outer_scope() const { return outer_scope_; } | 207 virtual Scope* outer_scope() const { return outer_scope_; } |
205 | 208 |
206 // --------------------------------------------------------------------------- | 209 // --------------------------------------------------------------------------- |
207 // Accessors. | 210 // Accessors. |
208 | 211 |
209 // A new variable proxy corresponding to the (function) receiver. | 212 // A new variable proxy corresponding to the (function) receiver. |
210 VariableProxy* receiver() const { | 213 VariableProxy* receiver() const { |
211 VariableProxy* proxy = | 214 VariableProxy* proxy = |
212 new VariableProxy(Factory::this_symbol(), true, false); | 215 new VariableProxy(Factory::this_symbol(), true, false); |
213 proxy->BindTo(receiver_); | 216 proxy->BindTo(receiver_); |
214 return proxy; | 217 return proxy; |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 int num_stack_slots() const { return num_stack_slots_; } | 268 int num_stack_slots() const { return num_stack_slots_; } |
266 int num_heap_slots() const { return num_heap_slots_; } | 269 int num_heap_slots() const { return num_heap_slots_; } |
267 | 270 |
268 // Make sure this scope and all outer scopes are eagerly compiled. | 271 // Make sure this scope and all outer scopes are eagerly compiled. |
269 void ForceEagerCompilation() { force_eager_compilation_ = true; } | 272 void ForceEagerCompilation() { force_eager_compilation_ = true; } |
270 | 273 |
271 // Determine if we can use lazy compilation for this scope. | 274 // Determine if we can use lazy compilation for this scope. |
272 bool AllowsLazyCompilation() const; | 275 bool AllowsLazyCompilation() const; |
273 | 276 |
274 // True if the outer context of this scope is always the global context. | 277 // True if the outer context of this scope is always the global context. |
275 bool HasTrivialOuterContext() const; | 278 virtual bool HasTrivialOuterContext() const; |
276 | 279 |
277 // The number of contexts between this and scope; zero if this == scope. | 280 // The number of contexts between this and scope; zero if this == scope. |
278 int ContextChainLength(Scope* scope); | 281 int ContextChainLength(Scope* scope); |
279 | 282 |
280 // --------------------------------------------------------------------------- | 283 // --------------------------------------------------------------------------- |
281 // Debugging. | 284 // Debugging. |
282 | 285 |
283 #ifdef DEBUG | 286 #ifdef DEBUG |
284 void Print(int n = 0); // n = indentation; n < 0 => don't print recursively | 287 void Print(int n = 0); // n = indentation; n < 0 => don't print recursively |
285 #endif | 288 #endif |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
371 // Variable allocation. | 374 // Variable allocation. |
372 void AllocateStackSlot(Variable* var); | 375 void AllocateStackSlot(Variable* var); |
373 void AllocateHeapSlot(Variable* var); | 376 void AllocateHeapSlot(Variable* var); |
374 void AllocateParameterLocals(); | 377 void AllocateParameterLocals(); |
375 void AllocateNonParameterLocal(Variable* var); | 378 void AllocateNonParameterLocal(Variable* var); |
376 void AllocateNonParameterLocals(); | 379 void AllocateNonParameterLocals(); |
377 void AllocateVariablesRecursively(); | 380 void AllocateVariablesRecursively(); |
378 }; | 381 }; |
379 | 382 |
380 | 383 |
| 384 // Scope used during pre-parsing. |
381 class DummyScope : public Scope { | 385 class DummyScope : public Scope { |
382 public: | 386 public: |
383 DummyScope() : Scope(GLOBAL_SCOPE) { | 387 DummyScope() |
| 388 : Scope(GLOBAL_SCOPE), |
| 389 nesting_level_(1), // Allows us to Leave the initial scope. |
| 390 inside_with_level_(kNotInsideWith) { |
384 outer_scope_ = this; | 391 outer_scope_ = this; |
| 392 scope_inside_with_ = false; |
| 393 } |
| 394 |
| 395 virtual void Initialize(bool inside_with) { |
| 396 nesting_level_++; |
| 397 if (inside_with && inside_with_level_ == kNotInsideWith) { |
| 398 inside_with_level_ = nesting_level_; |
| 399 } |
| 400 ASSERT(inside_with_level_ <= nesting_level_); |
| 401 } |
| 402 |
| 403 virtual void Leave() { |
| 404 nesting_level_--; |
| 405 ASSERT(nesting_level_ >= 0); |
| 406 if (nesting_level_ < inside_with_level_) { |
| 407 inside_with_level_ = kNotInsideWith; |
| 408 } |
| 409 ASSERT(inside_with_level_ <= nesting_level_); |
385 } | 410 } |
386 | 411 |
387 virtual Variable* Lookup(Handle<String> name) { return NULL; } | 412 virtual Variable* Lookup(Handle<String> name) { return NULL; } |
388 virtual Variable* Declare(Handle<String> name, Variable::Mode mode) { | 413 |
389 return NULL; | |
390 } | |
391 virtual VariableProxy* NewUnresolved(Handle<String> name, bool inside_with) { | 414 virtual VariableProxy* NewUnresolved(Handle<String> name, bool inside_with) { |
392 return NULL; | 415 return NULL; |
393 } | 416 } |
| 417 |
394 virtual VariableProxy* NewTemporary(Handle<String> name) { return NULL; } | 418 virtual VariableProxy* NewTemporary(Handle<String> name) { return NULL; } |
| 419 |
| 420 virtual bool HasTrivialOuterContext() const { |
| 421 return (nesting_level_ == 0 || inside_with_level_ <= 0); |
| 422 } |
| 423 |
| 424 private: |
| 425 static const int kNotInsideWith = -1; |
| 426 // Number of surrounding scopes of the current scope. |
| 427 int nesting_level_; |
| 428 // Nesting level of outermost scope that is contained in a with statement, |
| 429 // or kNotInsideWith if there are no with's around the current scope. |
| 430 int inside_with_level_; |
395 }; | 431 }; |
396 | 432 |
397 | 433 |
398 } } // namespace v8::internal | 434 } } // namespace v8::internal |
399 | 435 |
400 #endif // V8_SCOPES_H_ | 436 #endif // V8_SCOPES_H_ |
OLD | NEW |