diff options
Diffstat (limited to 'deps/v8/src/scopes.h')
-rw-r--r-- | deps/v8/src/scopes.h | 219 |
1 files changed, 159 insertions, 60 deletions
diff --git a/deps/v8/src/scopes.h b/deps/v8/src/scopes.h index 2917a63bb..523a251fa 100644 --- a/deps/v8/src/scopes.h +++ b/deps/v8/src/scopes.h @@ -42,17 +42,14 @@ class VariableMap: public HashMap { public: VariableMap(); - // Dummy constructor. This constructor doesn't set up the map - // properly so don't use it unless you have a good reason. - explicit VariableMap(bool gotta_love_static_overloading); - virtual ~VariableMap(); Variable* Declare(Scope* scope, Handle<String> name, - Variable::Mode mode, + VariableMode mode, bool is_valid_lhs, - Variable::Kind kind); + Variable::Kind kind, + InitializationFlag initialization_flag); Variable* Lookup(Handle<String> name); }; @@ -64,8 +61,8 @@ class VariableMap: public HashMap { // and setup time for scopes that don't need them. class DynamicScopePart : public ZoneObject { public: - VariableMap* GetMap(Variable::Mode mode) { - int index = mode - Variable::DYNAMIC; + VariableMap* GetMap(VariableMode mode) { + int index = mode - DYNAMIC; ASSERT(index >= 0 && index < 3); return &maps_[index]; } @@ -89,28 +86,19 @@ class Scope: public ZoneObject { // --------------------------------------------------------------------------- // Construction - enum Type { - EVAL_SCOPE, // The top-level scope for an eval source. - FUNCTION_SCOPE, // The top-level scope for a function. - GLOBAL_SCOPE, // The top-level scope for a program or a top-level eval. - CATCH_SCOPE, // The scope introduced by catch. - BLOCK_SCOPE // The scope introduced by a new block. - }; - - Scope(Scope* outer_scope, Type type); + Scope(Scope* outer_scope, ScopeType type); // Compute top scope and allocate variables. For lazy compilation the top // scope only contains the single lazily compiled function, so this // doesn't re-allocate variables repeatedly. static bool Analyze(CompilationInfo* info); - static Scope* DeserializeScopeChain(CompilationInfo* info, - Scope* innermost_scope); + static Scope* DeserializeScopeChain(Context* context, Scope* global_scope); // The scope name is only used for printing/debugging. void SetScopeName(Handle<String> scope_name) { scope_name_ = scope_name; } - void Initialize(bool inside_with); + void Initialize(); // Checks if the block scope is redundant, i.e. it does not contain any // block scoped declarations. In that case it is removed from the scope @@ -123,6 +111,12 @@ class Scope: public ZoneObject { // Lookup a variable in this scope. Returns the variable or NULL if not found. Variable* LocalLookup(Handle<String> name); + // This lookup corresponds to a lookup in the "intermediate" scope sitting + // between this scope and the outer scope. (ECMA-262, 3rd., requires that + // the name of named function literal is kept in an intermediate scope + // in between this scope and the next outer scope.) + Variable* LookupFunctionVar(Handle<String> name); + // Lookup a variable in this scope or outer scopes. // Returns the variable or NULL if not found. Variable* Lookup(Handle<String> name); @@ -130,16 +124,18 @@ class Scope: public ZoneObject { // Declare the function variable for a function literal. This variable // is in an intermediate scope between this function scope and the the // outer scope. Only possible for function scopes; at most one variable. - Variable* DeclareFunctionVar(Handle<String> name); + Variable* DeclareFunctionVar(Handle<String> name, VariableMode mode); // Declare a parameter in this scope. When there are duplicated // parameters the rightmost one 'wins'. However, the implementation // expects all parameters to be declared and from left to right. - void DeclareParameter(Handle<String> name, Variable::Mode mode); + void DeclareParameter(Handle<String> name, VariableMode mode); // Declare a local variable in this scope. If the variable has been // declared before, the previously declared variable is returned. - Variable* DeclareLocal(Handle<String> name, Variable::Mode mode); + Variable* DeclareLocal(Handle<String> name, + VariableMode mode, + InitializationFlag init_flag); // Declare an implicit global variable in this scope which must be a // global scope. The variable was introduced (possibly from an inner @@ -149,7 +145,6 @@ class Scope: public ZoneObject { // Create a new unresolved variable. VariableProxy* NewUnresolved(Handle<String> name, - bool inside_with, int position = RelocInfo::kNoPosition); // Remove a unresolved variable. During parsing, an unresolved variable @@ -199,11 +194,42 @@ class Scope: public ZoneObject { void RecordWithStatement() { scope_contains_with_ = true; } // Inform the scope that the corresponding code contains an eval call. - void RecordEvalCall() { scope_calls_eval_ = true; } + void RecordEvalCall() { if (!is_global_scope()) scope_calls_eval_ = true; } + + // Set the strict mode flag (unless disabled by a global flag). + void SetLanguageMode(LanguageMode language_mode) { + language_mode_ = language_mode; + } - // Enable strict mode for the scope (unless disabled by a global flag). - void EnableStrictMode() { - strict_mode_ = FLAG_strict_mode; + // Position in the source where this scope begins and ends. + // + // * For the scope of a with statement + // with (obj) stmt + // start position: start position of first token of 'stmt' + // end position: end position of last token of 'stmt' + // * For the scope of a block + // { stmts } + // start position: start position of '{' + // end position: end position of '}' + // * For the scope of a function literal or decalaration + // function fun(a,b) { stmts } + // start position: start position of '(' + // end position: end position of '}' + // * For the scope of a catch block + // try { stms } catch(e) { stmts } + // start position: start position of '(' + // end position: end position of ')' + // * For the scope of a for-statement + // for (let x ...) stmt + // start position: start position of '(' + // end position: end position of last token of 'stmt' + int start_position() const { return start_position_; } + void set_start_position(int statement_pos) { + start_position_ = statement_pos; + } + int end_position() const { return end_position_; } + void set_end_position(int statement_pos) { + end_position_ = statement_pos; } // --------------------------------------------------------------------------- @@ -215,14 +241,25 @@ class Scope: public ZoneObject { bool is_global_scope() const { return type_ == GLOBAL_SCOPE; } bool is_catch_scope() const { return type_ == CATCH_SCOPE; } bool is_block_scope() const { return type_ == BLOCK_SCOPE; } - bool is_strict_mode() const { return strict_mode_; } - bool is_strict_mode_eval_scope() const { - return is_eval_scope() && is_strict_mode(); + bool is_with_scope() const { return type_ == WITH_SCOPE; } + bool is_declaration_scope() const { + return is_eval_scope() || is_function_scope() || is_global_scope(); + } + bool is_classic_mode() const { + return language_mode() == CLASSIC_MODE; + } + bool is_extended_mode() const { + return language_mode() == EXTENDED_MODE; + } + bool is_strict_or_extended_eval_scope() const { + return is_eval_scope() && !is_classic_mode(); } // Information about which scopes calls eval. bool calls_eval() const { return scope_calls_eval_; } - bool outer_scope_calls_eval() const { return outer_scope_calls_eval_; } + bool calls_non_strict_eval() { + return scope_calls_eval_ && is_classic_mode(); + } bool outer_scope_calls_non_strict_eval() const { return outer_scope_calls_non_strict_eval_; } @@ -238,6 +275,12 @@ class Scope: public ZoneObject { // --------------------------------------------------------------------------- // Accessors. + // The type of this scope. + ScopeType type() const { return type_; } + + // The language mode of this scope. + LanguageMode language_mode() const { return language_mode_; } + // The variable corresponding the 'this' value. Variable* receiver() { return receiver_; } @@ -264,13 +307,17 @@ class Scope: public ZoneObject { // Declarations list. ZoneList<Declaration*>* declarations() { return &decls_; } + // Inner scope list. + ZoneList<Scope*>* inner_scopes() { return &inner_scopes_; } // --------------------------------------------------------------------------- // Variable allocation. - // Collect all used locals in this scope. - template<class Allocator> - void CollectUsedVariables(List<Variable*, Allocator>* locals); + // Collect stack and context allocated local variables in this scope. Note + // that the function variable - if present - is not collected and should be + // handled separately. + void CollectStackAndContextLocals(ZoneList<Variable*>* stack_locals, + ZoneList<Variable*>* context_locals); // Resolve and fill in the allocation information for all variables // in this scopes. Must be called *after* all scopes have been @@ -280,7 +327,7 @@ class Scope: public ZoneObject { // In the case of code compiled and run using 'eval', the context // parameter is the context in which eval was called. In all other // cases the context parameter is an empty handle. - void AllocateVariables(Handle<Context> context); + void AllocateVariables(Scope* global_scope); // Current number of var or const locals. int num_var_or_const() { return num_var_or_const_; } @@ -289,6 +336,9 @@ class Scope: public ZoneObject { int num_stack_slots() const { return num_stack_slots_; } int num_heap_slots() const { return num_heap_slots_; } + int StackLocalCount() const; + int ContextLocalCount() const; + // Make sure this scope and all outer scopes are eagerly compiled. void ForceEagerCompilation() { force_eager_compilation_ = true; } @@ -305,7 +355,14 @@ class Scope: public ZoneObject { // where var declarations will be hoisted to in the implementation. Scope* DeclarationScope(); - Handle<SerializedScopeInfo> GetSerializedScopeInfo(); + Handle<ScopeInfo> GetScopeInfo(); + + // Get the chain of nested scopes within this scope for the source statement + // position. The scopes will be added to the list from the outermost scope to + // the innermost scope. Only nested block, catch or with scopes are tracked + // and will be returned, but no inner function scopes. + void GetNestedScopeChain(List<Handle<ScopeInfo> >* chain, + int statement_position); // --------------------------------------------------------------------------- // Strict mode support. @@ -330,8 +387,6 @@ class Scope: public ZoneObject { protected: friend class ParserFactory; - explicit Scope(Type type); - Isolate* const isolate_; // Scope tree. @@ -339,7 +394,7 @@ class Scope: public ZoneObject { ZoneList<Scope*> inner_scopes_; // the immediately enclosed inner scopes // The scope type. - Type type_; + ScopeType type_; // Debugging support. Handle<String> scope_name_; @@ -379,14 +434,15 @@ class Scope: public ZoneObject { // This scope or a nested catch scope or with scope contain an 'eval' call. At // the 'eval' call site this scope is the declaration scope. bool scope_calls_eval_; - // This scope is a strict mode scope. - bool strict_mode_; + // The language mode of this scope. + LanguageMode language_mode_; + // Source positions. + int start_position_; + int end_position_; // Computed via PropagateScopeInfo. - bool outer_scope_calls_eval_; bool outer_scope_calls_non_strict_eval_; bool inner_scope_calls_eval_; - bool outer_scope_is_eval_scope_; bool force_eager_compilation_; // True if it doesn't need scope resolution (e.g., if the scope was @@ -396,32 +452,75 @@ class Scope: public ZoneObject { // Computed as variables are declared. int num_var_or_const_; - // Computed via AllocateVariables; function scopes only. + // Computed via AllocateVariables; function, block and catch scopes only. int num_stack_slots_; int num_heap_slots_; - // Serialized scopes support. - Handle<SerializedScopeInfo> scope_info_; + // Serialized scope info support. + Handle<ScopeInfo> scope_info_; bool already_resolved() { return already_resolved_; } // Create a non-local variable with a given name. // These variables are looked up dynamically at runtime. - Variable* NonLocal(Handle<String> name, Variable::Mode mode); + Variable* NonLocal(Handle<String> name, VariableMode mode); // Variable resolution. + // Possible results of a recursive variable lookup telling if and how a + // variable is bound. These are returned in the output parameter *binding_kind + // of the LookupRecursive function. + enum BindingKind { + // The variable reference could be statically resolved to a variable binding + // which is returned. There is no 'with' statement between the reference and + // the binding and no scope between the reference scope (inclusive) and + // binding scope (exclusive) makes a non-strict 'eval' call. + BOUND, + + // The variable reference could be statically resolved to a variable binding + // which is returned. There is no 'with' statement between the reference and + // the binding, but some scope between the reference scope (inclusive) and + // binding scope (exclusive) makes a non-strict 'eval' call, that might + // possibly introduce variable bindings shadowing the found one. Thus the + // found variable binding is just a guess. + BOUND_EVAL_SHADOWED, + + // The variable reference could not be statically resolved to any binding + // and thus should be considered referencing a global variable. NULL is + // returned. The variable reference is not inside any 'with' statement and + // no scope between the reference scope (inclusive) and global scope + // (exclusive) makes a non-strict 'eval' call. + UNBOUND, + + // The variable reference could not be statically resolved to any binding + // NULL is returned. The variable reference is not inside any 'with' + // statement, but some scope between the reference scope (inclusive) and + // global scope (exclusive) makes a non-strict 'eval' call, that might + // possibly introduce a variable binding. Thus the reference should be + // considered referencing a global variable unless it is shadowed by an + // 'eval' introduced binding. + UNBOUND_EVAL_SHADOWED, + + // The variable could not be statically resolved and needs to be looked up + // dynamically. NULL is returned. There are two possible reasons: + // * A 'with' statement has been encountered and there is no variable + // binding for the name between the variable reference and the 'with'. + // The variable potentially references a property of the 'with' object. + // * The code is being executed as part of a call to 'eval' and the calling + // context chain contains either a variable binding for the name or it + // contains a 'with' context. + DYNAMIC_LOOKUP + }; + + // Lookup a variable reference given by name recursively starting with this + // scope. If the code is executed because of a call to 'eval', the context + // parameter should be set to the calling context of 'eval'. Variable* LookupRecursive(Handle<String> name, - bool from_inner_function, - Variable** invalidated_local); + BindingKind* binding_kind); void ResolveVariable(Scope* global_scope, - Handle<Context> context, VariableProxy* proxy); - void ResolveVariablesRecursively(Scope* global_scope, - Handle<Context> context); + void ResolveVariablesRecursively(Scope* global_scope); // Scope analysis. - bool PropagateScopeInfo(bool outer_scope_calls_eval, - bool outer_scope_calls_non_strict_eval, - bool outer_scope_is_eval_scope); + bool PropagateScopeInfo(bool outer_scope_calls_non_strict_eval); bool HasTrivialContext() const; // Predicates. @@ -438,8 +537,8 @@ class Scope: public ZoneObject { void AllocateVariablesRecursively(); private: - // Construct a function or block scope based on the scope info. - Scope(Scope* inner_scope, Type type, Handle<SerializedScopeInfo> scope_info); + // Construct a scope based on the scope info. + Scope(Scope* inner_scope, ScopeType type, Handle<ScopeInfo> scope_info); // Construct a catch scope with a binding for the name. Scope(Scope* inner_scope, Handle<String> catch_variable_name); @@ -451,9 +550,9 @@ class Scope: public ZoneObject { } } - void SetDefaults(Type type, + void SetDefaults(ScopeType type, Scope* outer_scope, - Handle<SerializedScopeInfo> scope_info); + Handle<ScopeInfo> scope_info); }; } } // namespace v8::internal |