| OLD | NEW |
| 1 // Copyright 2011 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 |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 CATCH_SCOPE // The scope introduced by catch. | 96 CATCH_SCOPE, // The scope introduced by catch. |
| 97 WITH_SCOPE |
| 97 }; | 98 }; |
| 98 | 99 |
| 99 Scope(Scope* outer_scope, Type type); | 100 Scope(Scope* outer_scope, Type type); |
| 100 | 101 |
| 101 // Compute top scope and allocate variables. For lazy compilation the top | 102 // Compute top scope and allocate variables. For lazy compilation the top |
| 102 // scope only contains the single lazily compiled function, so this | 103 // scope only contains the single lazily compiled function, so this |
| 103 // doesn't re-allocate variables repeatedly. | 104 // doesn't re-allocate variables repeatedly. |
| 104 static bool Analyze(CompilationInfo* info); | 105 static bool Analyze(CompilationInfo* info); |
| 105 | 106 |
| 106 static Scope* DeserializeScopeChain(CompilationInfo* info, | 107 static Scope* DeserializeScopeChain(CompilationInfo* info, |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 | 194 |
| 194 // Enable strict mode for the scope (unless disabled by a global flag). | 195 // Enable strict mode for the scope (unless disabled by a global flag). |
| 195 void EnableStrictMode() { | 196 void EnableStrictMode() { |
| 196 strict_mode_ = FLAG_strict_mode; | 197 strict_mode_ = FLAG_strict_mode; |
| 197 } | 198 } |
| 198 | 199 |
| 199 // --------------------------------------------------------------------------- | 200 // --------------------------------------------------------------------------- |
| 200 // Predicates. | 201 // Predicates. |
| 201 | 202 |
| 202 // Specific scope types. | 203 // Specific scope types. |
| 204 Type type() const { return type_; } |
| 203 bool is_eval_scope() const { return type_ == EVAL_SCOPE; } | 205 bool is_eval_scope() const { return type_ == EVAL_SCOPE; } |
| 204 bool is_function_scope() const { return type_ == FUNCTION_SCOPE; } | 206 bool is_function_scope() const { return type_ == FUNCTION_SCOPE; } |
| 205 bool is_global_scope() const { return type_ == GLOBAL_SCOPE; } | 207 bool is_global_scope() const { return type_ == GLOBAL_SCOPE; } |
| 206 bool is_catch_scope() const { return type_ == CATCH_SCOPE; } | 208 bool is_catch_scope() const { return type_ == CATCH_SCOPE; } |
| 209 bool is_with_scope() const { return type_ == WITH_SCOPE; } |
| 207 bool is_strict_mode() const { return strict_mode_; } | 210 bool is_strict_mode() const { return strict_mode_; } |
| 208 bool is_strict_mode_eval_scope() const { | 211 bool is_strict_mode_eval_scope() const { |
| 209 return is_eval_scope() && is_strict_mode(); | 212 return is_eval_scope() && is_strict_mode(); |
| 210 } | 213 } |
| 211 | 214 |
| 212 // Information about which scopes calls eval. | 215 // Information about which scopes calls eval. |
| 213 bool calls_eval() const { return scope_calls_eval_; } | 216 bool calls_eval() const { return scope_calls_eval_; } |
| 214 bool outer_scope_calls_eval() const { return outer_scope_calls_eval_; } | 217 bool outer_scope_calls_eval() const { return outer_scope_calls_eval_; } |
| 215 bool outer_scope_calls_non_strict_eval() const { | 218 bool outer_scope_calls_non_strict_eval() const { |
| 216 return outer_scope_calls_non_strict_eval_; | 219 return outer_scope_calls_non_strict_eval_; |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 268 // | 271 // |
| 269 // In the case of code compiled and run using 'eval', the context | 272 // In the case of code compiled and run using 'eval', the context |
| 270 // parameter is the context in which eval was called. In all other | 273 // parameter is the context in which eval was called. In all other |
| 271 // cases the context parameter is an empty handle. | 274 // cases the context parameter is an empty handle. |
| 272 void AllocateVariables(Handle<Context> context); | 275 void AllocateVariables(Handle<Context> context); |
| 273 | 276 |
| 274 // Current number of var or const locals. | 277 // Current number of var or const locals. |
| 275 int num_var_or_const() { return num_var_or_const_; } | 278 int num_var_or_const() { return num_var_or_const_; } |
| 276 | 279 |
| 277 // Result of variable allocation. | 280 // Result of variable allocation. |
| 281 int num_stack_allocs() const { |
| 282 ASSERT(num_stack_allocs_ >= num_stack_slots_); |
| 283 return num_stack_allocs_; |
| 284 } |
| 278 int num_stack_slots() const { return num_stack_slots_; } | 285 int num_stack_slots() const { return num_stack_slots_; } |
| 279 int num_heap_slots() const { return num_heap_slots_; } | 286 int num_heap_slots() const { return num_heap_slots_; } |
| 280 | 287 |
| 288 int SourceBegStatementPos() const { return source_beg_statement_pos_; } |
| 289 void SetSourceBegStatementPos(int statement_pos) { |
| 290 source_beg_statement_pos_ = statement_pos; |
| 291 } |
| 292 int SourceEndStatementPos() const { return source_end_statement_pos_; } |
| 293 void SetSourceEndStatementPos(int statement_pos) { |
| 294 source_end_statement_pos_ = statement_pos; |
| 295 } |
| 296 |
| 297 ZoneList<Scope*>* InnerScopes() { return &inner_scopes_; } |
| 298 |
| 281 // Make sure this scope and all outer scopes are eagerly compiled. | 299 // Make sure this scope and all outer scopes are eagerly compiled. |
| 282 void ForceEagerCompilation() { force_eager_compilation_ = true; } | 300 void ForceEagerCompilation() { force_eager_compilation_ = true; } |
| 283 | 301 |
| 284 // Determine if we can use lazy compilation for this scope. | 302 // Determine if we can use lazy compilation for this scope. |
| 285 bool AllowsLazyCompilation() const; | 303 bool AllowsLazyCompilation() const; |
| 286 | 304 |
| 287 // True if the outer context of this scope is always the global context. | 305 // True if the outer context of this scope is always the global context. |
| 288 bool HasTrivialOuterContext() const; | 306 bool HasTrivialOuterContext() const; |
| 289 | 307 |
| 290 // The number of contexts between this and scope; zero if this == scope. | 308 // The number of contexts between this and scope; zero if this == scope. |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 350 // Convenience variable. | 368 // Convenience variable. |
| 351 Variable* receiver_; | 369 Variable* receiver_; |
| 352 // Function variable, if any; function scopes only. | 370 // Function variable, if any; function scopes only. |
| 353 Variable* function_; | 371 Variable* function_; |
| 354 // Convenience variable; function scopes only. | 372 // Convenience variable; function scopes only. |
| 355 Variable* arguments_; | 373 Variable* arguments_; |
| 356 | 374 |
| 357 // Illegal redeclaration. | 375 // Illegal redeclaration. |
| 358 Expression* illegal_redecl_; | 376 Expression* illegal_redecl_; |
| 359 | 377 |
| 360 // Scope-specific information. | 378 // Scope-specific information computed during parsing. |
| 361 bool scope_inside_with_; // this scope is inside a 'with' of some outer scope | 379 // |
| 362 bool scope_contains_with_; // this scope contains a 'with' statement | 380 // This scope is inside a 'with' of some outer scope. |
| 363 bool scope_calls_eval_; // this scope contains an 'eval' call | 381 bool scope_inside_with_; |
| 364 bool strict_mode_; // this scope is a strict mode scope | 382 // This scope contains a 'with' statement. |
| 383 bool scope_contains_with_; |
| 384 // This scope or a nested catch scope or with scope contain an 'eval' call. At |
| 385 // the 'eval' call site this scope is the declaration scope. |
| 386 bool scope_calls_eval_; |
| 387 // This scope is a strict mode scope. |
| 388 bool strict_mode_; |
| 365 | 389 |
| 366 // Computed via PropagateScopeInfo. | 390 // Computed via PropagateScopeInfo. |
| 367 bool outer_scope_calls_eval_; | 391 bool outer_scope_calls_eval_; |
| 368 bool outer_scope_calls_non_strict_eval_; | 392 bool outer_scope_calls_non_strict_eval_; |
| 369 bool inner_scope_calls_eval_; | 393 bool inner_scope_calls_eval_; |
| 370 bool outer_scope_is_eval_scope_; | 394 bool outer_scope_is_eval_scope_; |
| 371 bool force_eager_compilation_; | 395 bool force_eager_compilation_; |
| 372 | 396 |
| 373 // True if it doesn't need scope resolution (e.g., if the scope was | 397 // True if it doesn't need scope resolution (e.g., if the scope was |
| 374 // constructed based on a serialized scope info or a catch context). | 398 // constructed based on a serialized scope info or a catch context). |
| 375 bool already_resolved_; | 399 bool already_resolved_; |
| 376 | 400 |
| 377 // Computed as variables are declared. | 401 // Computed as variables are declared. |
| 378 int num_var_or_const_; | 402 int num_var_or_const_; |
| 379 | 403 |
| 380 // Computed via AllocateVariables; function scopes only. | 404 // Computed via AllocateVariables; function scopes only. |
| 405 int num_stack_allocs_; |
| 381 int num_stack_slots_; | 406 int num_stack_slots_; |
| 382 int num_heap_slots_; | 407 int num_heap_slots_; |
| 383 | 408 |
| 409 // Source information. |
| 410 int source_beg_statement_pos_; |
| 411 int source_end_statement_pos_; |
| 412 |
| 384 // Serialized scopes support. | 413 // Serialized scopes support. |
| 385 Handle<SerializedScopeInfo> scope_info_; | 414 Handle<SerializedScopeInfo> scope_info_; |
| 386 bool already_resolved() { return already_resolved_; } | 415 bool already_resolved() { return already_resolved_; } |
| 387 | 416 |
| 388 // Create a non-local variable with a given name. | 417 // Create a non-local variable with a given name. |
| 389 // These variables are looked up dynamically at runtime. | 418 // These variables are looked up dynamically at runtime. |
| 390 Variable* NonLocal(Handle<String> name, Variable::Mode mode); | 419 Variable* NonLocal(Handle<String> name, Variable::Mode mode); |
| 391 | 420 |
| 392 // Variable resolution. | 421 // Variable resolution. |
| 393 Variable* LookupRecursive(Handle<String> name, | 422 Variable* LookupRecursive(Handle<String> name, |
| 394 bool inner_lookup, | 423 bool inner_lookup, |
| 395 Variable** invalidated_local); | 424 Variable** invalidated_local); |
| 396 void ResolveVariable(Scope* global_scope, | 425 void ResolveVariable(Scope* global_scope, |
| 397 Handle<Context> context, | 426 Handle<Context> context, |
| 398 VariableProxy* proxy); | 427 VariableProxy* proxy); |
| 399 void ResolveVariablesRecursively(Scope* global_scope, | 428 void ResolveVariablesRecursively(Scope* global_scope, |
| 400 Handle<Context> context); | 429 Handle<Context> context); |
| 401 | 430 |
| 402 // Scope analysis. | 431 // Scope analysis. |
| 403 bool PropagateScopeInfo(bool outer_scope_calls_eval, | 432 bool PropagateScopeInfo(bool outer_scope_calls_eval, |
| 404 bool outer_scope_calls_non_strict_eval, | 433 bool outer_scope_calls_non_strict_eval, |
| 405 bool outer_scope_is_eval_scope); | 434 bool outer_scope_is_eval_scope); |
| 406 bool HasTrivialContext() const; | 435 bool HasTrivialContext() const; |
| 436 bool HasContext() const; |
| 407 | 437 |
| 408 // Predicates. | 438 // Predicates. |
| 409 bool MustAllocate(Variable* var); | 439 bool MustAllocate(Variable* var); |
| 410 bool MustAllocateInContext(Variable* var); | 440 bool MustAllocateInContext(Variable* var); |
| 411 bool HasArgumentsParameter(); | 441 bool HasArgumentsParameter(); |
| 412 | 442 |
| 413 // Variable allocation. | 443 // Variable allocation. |
| 414 void AllocateStackSlot(Variable* var); | 444 void AllocateStackSlot(Variable* var); |
| 415 void AllocateHeapSlot(Variable* var); | 445 void AllocateHeapSlot(Variable* var); |
| 416 void AllocateParameterLocals(); | 446 void AllocateParameterLocals(); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 437 } | 467 } |
| 438 | 468 |
| 439 void SetDefaults(Type type, | 469 void SetDefaults(Type type, |
| 440 Scope* outer_scope, | 470 Scope* outer_scope, |
| 441 Handle<SerializedScopeInfo> scope_info); | 471 Handle<SerializedScopeInfo> scope_info); |
| 442 }; | 472 }; |
| 443 | 473 |
| 444 } } // namespace v8::internal | 474 } } // namespace v8::internal |
| 445 | 475 |
| 446 #endif // V8_SCOPES_H_ | 476 #endif // V8_SCOPES_H_ |
| OLD | NEW |