| OLD | NEW |
| (Empty) |
| 1 // Copyright 2012 the V8 project authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef V8_SCOPES_H_ | |
| 6 #define V8_SCOPES_H_ | |
| 7 | |
| 8 #include "src/ast.h" | |
| 9 #include "src/pending-compilation-error-handler.h" | |
| 10 #include "src/zone.h" | |
| 11 | |
| 12 namespace v8 { | |
| 13 namespace internal { | |
| 14 | |
| 15 class ParseInfo; | |
| 16 | |
| 17 // A hash map to support fast variable declaration and lookup. | |
| 18 class VariableMap: public ZoneHashMap { | |
| 19 public: | |
| 20 explicit VariableMap(Zone* zone); | |
| 21 | |
| 22 virtual ~VariableMap(); | |
| 23 | |
| 24 Variable* Declare(Scope* scope, const AstRawString* name, VariableMode mode, | |
| 25 Variable::Kind kind, InitializationFlag initialization_flag, | |
| 26 MaybeAssignedFlag maybe_assigned_flag = kNotAssigned, | |
| 27 int declaration_group_start = -1); | |
| 28 | |
| 29 Variable* Lookup(const AstRawString* name); | |
| 30 | |
| 31 Zone* zone() const { return zone_; } | |
| 32 | |
| 33 private: | |
| 34 Zone* zone_; | |
| 35 }; | |
| 36 | |
| 37 | |
| 38 // The dynamic scope part holds hash maps for the variables that will | |
| 39 // be looked up dynamically from within eval and with scopes. The objects | |
| 40 // are allocated on-demand from Scope::NonLocal to avoid wasting memory | |
| 41 // and setup time for scopes that don't need them. | |
| 42 class DynamicScopePart : public ZoneObject { | |
| 43 public: | |
| 44 explicit DynamicScopePart(Zone* zone) { | |
| 45 for (int i = 0; i < 3; i++) | |
| 46 maps_[i] = new(zone->New(sizeof(VariableMap))) VariableMap(zone); | |
| 47 } | |
| 48 | |
| 49 VariableMap* GetMap(VariableMode mode) { | |
| 50 int index = mode - DYNAMIC; | |
| 51 DCHECK(index >= 0 && index < 3); | |
| 52 return maps_[index]; | |
| 53 } | |
| 54 | |
| 55 private: | |
| 56 VariableMap *maps_[3]; | |
| 57 }; | |
| 58 | |
| 59 | |
| 60 // Sloppy block-scoped function declarations to var-bind | |
| 61 class SloppyBlockFunctionMap : public ZoneHashMap { | |
| 62 public: | |
| 63 explicit SloppyBlockFunctionMap(Zone* zone); | |
| 64 | |
| 65 virtual ~SloppyBlockFunctionMap(); | |
| 66 | |
| 67 void Declare(const AstRawString* name, | |
| 68 SloppyBlockFunctionStatement* statement); | |
| 69 | |
| 70 typedef ZoneVector<SloppyBlockFunctionStatement*> Vector; | |
| 71 | |
| 72 private: | |
| 73 Zone* zone_; | |
| 74 }; | |
| 75 | |
| 76 | |
| 77 // Global invariants after AST construction: Each reference (i.e. identifier) | |
| 78 // to a JavaScript variable (including global properties) is represented by a | |
| 79 // VariableProxy node. Immediately after AST construction and before variable | |
| 80 // allocation, most VariableProxy nodes are "unresolved", i.e. not bound to a | |
| 81 // corresponding variable (though some are bound during parse time). Variable | |
| 82 // allocation binds each unresolved VariableProxy to one Variable and assigns | |
| 83 // a location. Note that many VariableProxy nodes may refer to the same Java- | |
| 84 // Script variable. | |
| 85 | |
| 86 class Scope: public ZoneObject { | |
| 87 public: | |
| 88 // --------------------------------------------------------------------------- | |
| 89 // Construction | |
| 90 | |
| 91 Scope(Zone* zone, Scope* outer_scope, ScopeType scope_type, | |
| 92 AstValueFactory* value_factory, | |
| 93 FunctionKind function_kind = kNormalFunction); | |
| 94 | |
| 95 // Compute top scope and allocate variables. For lazy compilation the top | |
| 96 // scope only contains the single lazily compiled function, so this | |
| 97 // doesn't re-allocate variables repeatedly. | |
| 98 static bool Analyze(ParseInfo* info); | |
| 99 | |
| 100 static Scope* DeserializeScopeChain(Isolate* isolate, Zone* zone, | |
| 101 Context* context, Scope* script_scope); | |
| 102 | |
| 103 // The scope name is only used for printing/debugging. | |
| 104 void SetScopeName(const AstRawString* scope_name) { | |
| 105 scope_name_ = scope_name; | |
| 106 } | |
| 107 | |
| 108 void Initialize(); | |
| 109 | |
| 110 // Checks if the block scope is redundant, i.e. it does not contain any | |
| 111 // block scoped declarations. In that case it is removed from the scope | |
| 112 // tree and its children are reparented. | |
| 113 Scope* FinalizeBlockScope(); | |
| 114 | |
| 115 // Inserts outer_scope into this scope's scope chain (and removes this | |
| 116 // from the current outer_scope_'s inner_scopes_). | |
| 117 // Assumes outer_scope_ is non-null. | |
| 118 void ReplaceOuterScope(Scope* outer_scope); | |
| 119 | |
| 120 // Propagates any eagerly-gathered scope usage flags (such as calls_eval()) | |
| 121 // to the passed-in scope. | |
| 122 void PropagateUsageFlagsToScope(Scope* other); | |
| 123 | |
| 124 Zone* zone() const { return zone_; } | |
| 125 | |
| 126 // --------------------------------------------------------------------------- | |
| 127 // Declarations | |
| 128 | |
| 129 // Lookup a variable in this scope. Returns the variable or NULL if not found. | |
| 130 Variable* LookupLocal(const AstRawString* name); | |
| 131 | |
| 132 // This lookup corresponds to a lookup in the "intermediate" scope sitting | |
| 133 // between this scope and the outer scope. (ECMA-262, 3rd., requires that | |
| 134 // the name of named function literal is kept in an intermediate scope | |
| 135 // in between this scope and the next outer scope.) | |
| 136 Variable* LookupFunctionVar(const AstRawString* name, | |
| 137 AstNodeFactory* factory); | |
| 138 | |
| 139 // Lookup a variable in this scope or outer scopes. | |
| 140 // Returns the variable or NULL if not found. | |
| 141 Variable* Lookup(const AstRawString* name); | |
| 142 | |
| 143 // Declare the function variable for a function literal. This variable | |
| 144 // is in an intermediate scope between this function scope and the the | |
| 145 // outer scope. Only possible for function scopes; at most one variable. | |
| 146 void DeclareFunctionVar(VariableDeclaration* declaration) { | |
| 147 DCHECK(is_function_scope()); | |
| 148 // Handle implicit declaration of the function name in named function | |
| 149 // expressions before other declarations. | |
| 150 decls_.InsertAt(0, declaration, zone()); | |
| 151 function_ = declaration; | |
| 152 } | |
| 153 | |
| 154 // Declare a parameter in this scope. When there are duplicated | |
| 155 // parameters the rightmost one 'wins'. However, the implementation | |
| 156 // expects all parameters to be declared and from left to right. | |
| 157 Variable* DeclareParameter( | |
| 158 const AstRawString* name, VariableMode mode, | |
| 159 bool is_optional, bool is_rest, bool* is_duplicate); | |
| 160 | |
| 161 // Declare a local variable in this scope. If the variable has been | |
| 162 // declared before, the previously declared variable is returned. | |
| 163 Variable* DeclareLocal(const AstRawString* name, VariableMode mode, | |
| 164 InitializationFlag init_flag, Variable::Kind kind, | |
| 165 MaybeAssignedFlag maybe_assigned_flag = kNotAssigned, | |
| 166 int declaration_group_start = -1); | |
| 167 | |
| 168 // Declare an implicit global variable in this scope which must be a | |
| 169 // script scope. The variable was introduced (possibly from an inner | |
| 170 // scope) by a reference to an unresolved variable with no intervening | |
| 171 // with statements or eval calls. | |
| 172 Variable* DeclareDynamicGlobal(const AstRawString* name); | |
| 173 | |
| 174 // Create a new unresolved variable. | |
| 175 VariableProxy* NewUnresolved(AstNodeFactory* factory, | |
| 176 const AstRawString* name, | |
| 177 Variable::Kind kind = Variable::NORMAL, | |
| 178 int start_position = RelocInfo::kNoPosition, | |
| 179 int end_position = RelocInfo::kNoPosition) { | |
| 180 // Note that we must not share the unresolved variables with | |
| 181 // the same name because they may be removed selectively via | |
| 182 // RemoveUnresolved(). | |
| 183 DCHECK(!already_resolved()); | |
| 184 VariableProxy* proxy = | |
| 185 factory->NewVariableProxy(name, kind, start_position, end_position); | |
| 186 unresolved_.Add(proxy, zone_); | |
| 187 return proxy; | |
| 188 } | |
| 189 | |
| 190 void AddUnresolved(VariableProxy* proxy) { | |
| 191 DCHECK(!already_resolved()); | |
| 192 DCHECK(!proxy->is_resolved()); | |
| 193 unresolved_.Add(proxy, zone_); | |
| 194 } | |
| 195 | |
| 196 // Remove a unresolved variable. During parsing, an unresolved variable | |
| 197 // may have been added optimistically, but then only the variable name | |
| 198 // was used (typically for labels). If the variable was not declared, the | |
| 199 // addition introduced a new unresolved variable which may end up being | |
| 200 // allocated globally as a "ghost" variable. RemoveUnresolved removes | |
| 201 // such a variable again if it was added; otherwise this is a no-op. | |
| 202 bool RemoveUnresolved(VariableProxy* var); | |
| 203 | |
| 204 // Creates a new temporary variable in this scope's TemporaryScope. The | |
| 205 // name is only used for printing and cannot be used to find the variable. | |
| 206 // In particular, the only way to get hold of the temporary is by keeping the | |
| 207 // Variable* around. The name should not clash with a legitimate variable | |
| 208 // names. | |
| 209 Variable* NewTemporary(const AstRawString* name); | |
| 210 | |
| 211 // Adds the specific declaration node to the list of declarations in | |
| 212 // this scope. The declarations are processed as part of entering | |
| 213 // the scope; see codegen.cc:ProcessDeclarations. | |
| 214 void AddDeclaration(Declaration* declaration); | |
| 215 | |
| 216 // --------------------------------------------------------------------------- | |
| 217 // Illegal redeclaration support. | |
| 218 | |
| 219 // Set an expression node that will be executed when the scope is | |
| 220 // entered. We only keep track of one illegal redeclaration node per | |
| 221 // scope - the first one - so if you try to set it multiple times | |
| 222 // the additional requests will be silently ignored. | |
| 223 void SetIllegalRedeclaration(Expression* expression); | |
| 224 | |
| 225 // Retrieve the illegal redeclaration expression. Do not call if the | |
| 226 // scope doesn't have an illegal redeclaration node. | |
| 227 Expression* GetIllegalRedeclaration(); | |
| 228 | |
| 229 // Check if the scope has (at least) one illegal redeclaration. | |
| 230 bool HasIllegalRedeclaration() const { return illegal_redecl_ != NULL; } | |
| 231 | |
| 232 // For harmony block scoping mode: Check if the scope has conflicting var | |
| 233 // declarations, i.e. a var declaration that has been hoisted from a nested | |
| 234 // scope over a let binding of the same name. | |
| 235 Declaration* CheckConflictingVarDeclarations(); | |
| 236 | |
| 237 // --------------------------------------------------------------------------- | |
| 238 // Scope-specific info. | |
| 239 | |
| 240 // Inform the scope that the corresponding code contains a with statement. | |
| 241 void RecordWithStatement() { scope_contains_with_ = true; } | |
| 242 | |
| 243 // Inform the scope that the corresponding code contains an eval call. | |
| 244 void RecordEvalCall() { scope_calls_eval_ = true; } | |
| 245 | |
| 246 // Inform the scope that the corresponding code uses "arguments". | |
| 247 void RecordArgumentsUsage() { scope_uses_arguments_ = true; } | |
| 248 | |
| 249 // Inform the scope that the corresponding code uses "super". | |
| 250 void RecordSuperPropertyUsage() { scope_uses_super_property_ = true; } | |
| 251 | |
| 252 // Set the language mode flag (unless disabled by a global flag). | |
| 253 void SetLanguageMode(LanguageMode language_mode) { | |
| 254 language_mode_ = language_mode; | |
| 255 } | |
| 256 | |
| 257 // Set the ASM module flag. | |
| 258 void SetAsmModule() { asm_module_ = true; } | |
| 259 | |
| 260 // Inform the scope that the scope may execute declarations nonlinearly. | |
| 261 // Currently, the only nonlinear scope is a switch statement. The name is | |
| 262 // more general in case something else comes up with similar control flow, | |
| 263 // for example the ability to break out of something which does not have | |
| 264 // its own lexical scope. | |
| 265 // The bit does not need to be stored on the ScopeInfo because none of | |
| 266 // the three compilers will perform hole check elimination on a variable | |
| 267 // located in VariableLocation::CONTEXT. So, direct eval and closures | |
| 268 // will not expose holes. | |
| 269 void SetNonlinear() { scope_nonlinear_ = true; } | |
| 270 | |
| 271 // Position in the source where this scope begins and ends. | |
| 272 // | |
| 273 // * For the scope of a with statement | |
| 274 // with (obj) stmt | |
| 275 // start position: start position of first token of 'stmt' | |
| 276 // end position: end position of last token of 'stmt' | |
| 277 // * For the scope of a block | |
| 278 // { stmts } | |
| 279 // start position: start position of '{' | |
| 280 // end position: end position of '}' | |
| 281 // * For the scope of a function literal or decalaration | |
| 282 // function fun(a,b) { stmts } | |
| 283 // start position: start position of '(' | |
| 284 // end position: end position of '}' | |
| 285 // * For the scope of a catch block | |
| 286 // try { stms } catch(e) { stmts } | |
| 287 // start position: start position of '(' | |
| 288 // end position: end position of ')' | |
| 289 // * For the scope of a for-statement | |
| 290 // for (let x ...) stmt | |
| 291 // start position: start position of '(' | |
| 292 // end position: end position of last token of 'stmt' | |
| 293 // * For the scope of a switch statement | |
| 294 // switch (tag) { cases } | |
| 295 // start position: start position of '{' | |
| 296 // end position: end position of '}' | |
| 297 int start_position() const { return start_position_; } | |
| 298 void set_start_position(int statement_pos) { | |
| 299 start_position_ = statement_pos; | |
| 300 } | |
| 301 int end_position() const { return end_position_; } | |
| 302 void set_end_position(int statement_pos) { | |
| 303 end_position_ = statement_pos; | |
| 304 } | |
| 305 | |
| 306 // In some cases we want to force context allocation for a whole scope. | |
| 307 void ForceContextAllocation() { | |
| 308 DCHECK(!already_resolved()); | |
| 309 force_context_allocation_ = true; | |
| 310 } | |
| 311 bool has_forced_context_allocation() const { | |
| 312 return force_context_allocation_; | |
| 313 } | |
| 314 | |
| 315 // --------------------------------------------------------------------------- | |
| 316 // Predicates. | |
| 317 | |
| 318 // Specific scope types. | |
| 319 bool is_eval_scope() const { return scope_type_ == EVAL_SCOPE; } | |
| 320 bool is_function_scope() const { return scope_type_ == FUNCTION_SCOPE; } | |
| 321 bool is_module_scope() const { return scope_type_ == MODULE_SCOPE; } | |
| 322 bool is_script_scope() const { return scope_type_ == SCRIPT_SCOPE; } | |
| 323 bool is_catch_scope() const { return scope_type_ == CATCH_SCOPE; } | |
| 324 bool is_block_scope() const { return scope_type_ == BLOCK_SCOPE; } | |
| 325 bool is_with_scope() const { return scope_type_ == WITH_SCOPE; } | |
| 326 bool is_arrow_scope() const { | |
| 327 return is_function_scope() && IsArrowFunction(function_kind_); | |
| 328 } | |
| 329 bool is_declaration_scope() const { return is_declaration_scope_; } | |
| 330 | |
| 331 void set_is_declaration_scope() { is_declaration_scope_ = true; } | |
| 332 | |
| 333 // Information about which scopes calls eval. | |
| 334 bool calls_eval() const { return scope_calls_eval_; } | |
| 335 bool calls_sloppy_eval() const { | |
| 336 return scope_calls_eval_ && is_sloppy(language_mode_); | |
| 337 } | |
| 338 bool outer_scope_calls_sloppy_eval() const { | |
| 339 return outer_scope_calls_sloppy_eval_; | |
| 340 } | |
| 341 bool asm_module() const { return asm_module_; } | |
| 342 bool asm_function() const { return asm_function_; } | |
| 343 | |
| 344 // Is this scope inside a with statement. | |
| 345 bool inside_with() const { return scope_inside_with_; } | |
| 346 | |
| 347 // Does this scope access "arguments". | |
| 348 bool uses_arguments() const { return scope_uses_arguments_; } | |
| 349 // Does this scope access "super" property (super.foo). | |
| 350 bool uses_super_property() const { return scope_uses_super_property_; } | |
| 351 // Does this scope have the potential to execute declarations non-linearly? | |
| 352 bool is_nonlinear() const { return scope_nonlinear_; } | |
| 353 | |
| 354 // Whether this needs to be represented by a runtime context. | |
| 355 bool NeedsContext() const { | |
| 356 // Catch and module scopes always have heap slots. | |
| 357 DCHECK(!is_catch_scope() || num_heap_slots() > 0); | |
| 358 DCHECK(!is_module_scope() || num_heap_slots() > 0); | |
| 359 return is_with_scope() || num_heap_slots() > 0; | |
| 360 } | |
| 361 | |
| 362 bool NeedsHomeObject() const { | |
| 363 return scope_uses_super_property_ || | |
| 364 ((scope_calls_eval_ || inner_scope_calls_eval_) && | |
| 365 (IsConciseMethod(function_kind()) || | |
| 366 IsAccessorFunction(function_kind()) || | |
| 367 IsClassConstructor(function_kind()))); | |
| 368 } | |
| 369 | |
| 370 const Scope* NearestOuterEvalScope() const { | |
| 371 if (is_eval_scope()) return this; | |
| 372 if (outer_scope() == nullptr) return nullptr; | |
| 373 return outer_scope()->NearestOuterEvalScope(); | |
| 374 } | |
| 375 | |
| 376 // --------------------------------------------------------------------------- | |
| 377 // Accessors. | |
| 378 | |
| 379 // The type of this scope. | |
| 380 ScopeType scope_type() const { return scope_type_; } | |
| 381 | |
| 382 FunctionKind function_kind() const { return function_kind_; } | |
| 383 | |
| 384 // The language mode of this scope. | |
| 385 LanguageMode language_mode() const { return language_mode_; } | |
| 386 | |
| 387 // The variable corresponding to the 'this' value. | |
| 388 Variable* receiver() { | |
| 389 DCHECK(has_this_declaration()); | |
| 390 DCHECK_NOT_NULL(receiver_); | |
| 391 return receiver_; | |
| 392 } | |
| 393 | |
| 394 // TODO(wingo): Add a GLOBAL_SCOPE scope type which will lexically allocate | |
| 395 // "this" (and no other variable) on the native context. Script scopes then | |
| 396 // will not have a "this" declaration. | |
| 397 bool has_this_declaration() const { | |
| 398 return (is_function_scope() && !is_arrow_scope()) || is_module_scope(); | |
| 399 } | |
| 400 | |
| 401 // The variable corresponding to the 'new.target' value. | |
| 402 Variable* new_target_var() { return new_target_; } | |
| 403 | |
| 404 // The variable holding the function literal for named function | |
| 405 // literals, or NULL. Only valid for function scopes. | |
| 406 VariableDeclaration* function() const { | |
| 407 DCHECK(is_function_scope()); | |
| 408 return function_; | |
| 409 } | |
| 410 | |
| 411 // Parameters. The left-most parameter has index 0. | |
| 412 // Only valid for function scopes. | |
| 413 Variable* parameter(int index) const { | |
| 414 DCHECK(is_function_scope()); | |
| 415 return params_[index]; | |
| 416 } | |
| 417 | |
| 418 // Returns the default function arity excluding default or rest parameters. | |
| 419 int default_function_length() const { return arity_; } | |
| 420 | |
| 421 int num_parameters() const { return params_.length(); } | |
| 422 | |
| 423 // A function can have at most one rest parameter. Returns Variable* or NULL. | |
| 424 Variable* rest_parameter(int* index) const { | |
| 425 *index = rest_index_; | |
| 426 if (rest_index_ < 0) return NULL; | |
| 427 return rest_parameter_; | |
| 428 } | |
| 429 | |
| 430 bool has_rest_parameter() const { | |
| 431 return rest_index_ >= 0; | |
| 432 } | |
| 433 | |
| 434 bool has_simple_parameters() const { | |
| 435 return has_simple_parameters_; | |
| 436 } | |
| 437 | |
| 438 // TODO(caitp): manage this state in a better way. PreParser must be able to | |
| 439 // communicate that the scope is non-simple, without allocating any parameters | |
| 440 // as the Parser does. This is necessary to ensure that TC39's proposed early | |
| 441 // error can be reported consistently regardless of whether lazily parsed or | |
| 442 // not. | |
| 443 void SetHasNonSimpleParameters() { | |
| 444 DCHECK(is_function_scope()); | |
| 445 has_simple_parameters_ = false; | |
| 446 } | |
| 447 | |
| 448 // Retrieve `IsSimpleParameterList` of current or outer function. | |
| 449 bool HasSimpleParameters() { | |
| 450 Scope* scope = ClosureScope(); | |
| 451 return !scope->is_function_scope() || scope->has_simple_parameters(); | |
| 452 } | |
| 453 | |
| 454 // The local variable 'arguments' if we need to allocate it; NULL otherwise. | |
| 455 Variable* arguments() const { | |
| 456 DCHECK(!is_arrow_scope() || arguments_ == nullptr); | |
| 457 return arguments_; | |
| 458 } | |
| 459 | |
| 460 Variable* this_function_var() const { | |
| 461 // This is only used in derived constructors atm. | |
| 462 DCHECK(this_function_ == nullptr || | |
| 463 (is_function_scope() && (IsClassConstructor(function_kind()) || | |
| 464 IsConciseMethod(function_kind()) || | |
| 465 IsAccessorFunction(function_kind())))); | |
| 466 return this_function_; | |
| 467 } | |
| 468 | |
| 469 // Declarations list. | |
| 470 ZoneList<Declaration*>* declarations() { return &decls_; } | |
| 471 | |
| 472 // Inner scope list. | |
| 473 ZoneList<Scope*>* inner_scopes() { return &inner_scopes_; } | |
| 474 | |
| 475 // The scope immediately surrounding this scope, or NULL. | |
| 476 Scope* outer_scope() const { return outer_scope_; } | |
| 477 | |
| 478 // The ModuleDescriptor for this scope; only for module scopes. | |
| 479 ModuleDescriptor* module() const { return module_descriptor_; } | |
| 480 | |
| 481 | |
| 482 void set_class_declaration_group_start(int position) { | |
| 483 class_declaration_group_start_ = position; | |
| 484 } | |
| 485 | |
| 486 int class_declaration_group_start() const { | |
| 487 return class_declaration_group_start_; | |
| 488 } | |
| 489 | |
| 490 // --------------------------------------------------------------------------- | |
| 491 // Variable allocation. | |
| 492 | |
| 493 // Collect stack and context allocated local variables in this scope. Note | |
| 494 // that the function variable - if present - is not collected and should be | |
| 495 // handled separately. | |
| 496 void CollectStackAndContextLocals( | |
| 497 ZoneList<Variable*>* stack_locals, ZoneList<Variable*>* context_locals, | |
| 498 ZoneList<Variable*>* context_globals, | |
| 499 ZoneList<Variable*>* strong_mode_free_variables = nullptr); | |
| 500 | |
| 501 // Current number of var or const locals. | |
| 502 int num_var_or_const() { return num_var_or_const_; } | |
| 503 | |
| 504 // Result of variable allocation. | |
| 505 int num_stack_slots() const { return num_stack_slots_; } | |
| 506 int num_heap_slots() const { return num_heap_slots_; } | |
| 507 int num_global_slots() const { return num_global_slots_; } | |
| 508 | |
| 509 int StackLocalCount() const; | |
| 510 int ContextLocalCount() const; | |
| 511 int ContextGlobalCount() const; | |
| 512 | |
| 513 // For script scopes, the number of module literals (including nested ones). | |
| 514 int num_modules() const { return num_modules_; } | |
| 515 | |
| 516 // For module scopes, the host scope's internal variable binding this module. | |
| 517 Variable* module_var() const { return module_var_; } | |
| 518 | |
| 519 // Make sure this scope and all outer scopes are eagerly compiled. | |
| 520 void ForceEagerCompilation() { force_eager_compilation_ = true; } | |
| 521 | |
| 522 // Determine if we can parse a function literal in this scope lazily. | |
| 523 bool AllowsLazyParsing() const; | |
| 524 | |
| 525 // Determine if we can use lazy compilation for this scope. | |
| 526 bool AllowsLazyCompilation() const; | |
| 527 | |
| 528 // Determine if we can use lazy compilation for this scope without a context. | |
| 529 bool AllowsLazyCompilationWithoutContext() const; | |
| 530 | |
| 531 // True if the outer context of this scope is always the native context. | |
| 532 bool HasTrivialOuterContext() const; | |
| 533 | |
| 534 // The number of contexts between this and scope; zero if this == scope. | |
| 535 int ContextChainLength(Scope* scope); | |
| 536 | |
| 537 // The maximum number of nested contexts required for this scope and any inner | |
| 538 // scopes. | |
| 539 int MaxNestedContextChainLength(); | |
| 540 | |
| 541 // Find the first function, script, eval or (declaration) block scope. This is | |
| 542 // the scope where var declarations will be hoisted to in the implementation. | |
| 543 Scope* DeclarationScope(); | |
| 544 | |
| 545 // Find the first non-block declaration scope. This should be either a script, | |
| 546 // function, or eval scope. Same as DeclarationScope(), but skips | |
| 547 // declaration "block" scopes. Used for differentiating associated | |
| 548 // function objects (i.e., the scope for which a function prologue allocates | |
| 549 // a context) or declaring temporaries. | |
| 550 Scope* ClosureScope(); | |
| 551 | |
| 552 // Find the first (non-arrow) function or script scope. This is where | |
| 553 // 'this' is bound, and what determines the function kind. | |
| 554 Scope* ReceiverScope(); | |
| 555 | |
| 556 Handle<ScopeInfo> GetScopeInfo(Isolate* isolate); | |
| 557 | |
| 558 // Get the chain of nested scopes within this scope for the source statement | |
| 559 // position. The scopes will be added to the list from the outermost scope to | |
| 560 // the innermost scope. Only nested block, catch or with scopes are tracked | |
| 561 // and will be returned, but no inner function scopes. | |
| 562 void GetNestedScopeChain(Isolate* isolate, List<Handle<ScopeInfo> >* chain, | |
| 563 int statement_position); | |
| 564 | |
| 565 // --------------------------------------------------------------------------- | |
| 566 // Strict mode support. | |
| 567 bool IsDeclared(const AstRawString* name) { | |
| 568 // During formal parameter list parsing the scope only contains | |
| 569 // two variables inserted at initialization: "this" and "arguments". | |
| 570 // "this" is an invalid parameter name and "arguments" is invalid parameter | |
| 571 // name in strict mode. Therefore looking up with the map which includes | |
| 572 // "this" and "arguments" in addition to all formal parameters is safe. | |
| 573 return variables_.Lookup(name) != NULL; | |
| 574 } | |
| 575 | |
| 576 bool IsDeclaredParameter(const AstRawString* name) { | |
| 577 // If IsSimpleParameterList is false, duplicate parameters are not allowed, | |
| 578 // however `arguments` may be allowed if function is not strict code. Thus, | |
| 579 // the assumptions explained above do not hold. | |
| 580 return params_.Contains(variables_.Lookup(name)); | |
| 581 } | |
| 582 | |
| 583 SloppyBlockFunctionMap* sloppy_block_function_map() { | |
| 584 return &sloppy_block_function_map_; | |
| 585 } | |
| 586 | |
| 587 // Error handling. | |
| 588 void ReportMessage(int start_position, int end_position, | |
| 589 MessageTemplate::Template message, | |
| 590 const AstRawString* arg); | |
| 591 | |
| 592 // --------------------------------------------------------------------------- | |
| 593 // Debugging. | |
| 594 | |
| 595 #ifdef DEBUG | |
| 596 void Print(int n = 0); // n = indentation; n < 0 => don't print recursively | |
| 597 #endif | |
| 598 | |
| 599 // --------------------------------------------------------------------------- | |
| 600 // Implementation. | |
| 601 private: | |
| 602 // Scope tree. | |
| 603 Scope* outer_scope_; // the immediately enclosing outer scope, or NULL | |
| 604 ZoneList<Scope*> inner_scopes_; // the immediately enclosed inner scopes | |
| 605 | |
| 606 // The scope type. | |
| 607 ScopeType scope_type_; | |
| 608 // If the scope is a function scope, this is the function kind. | |
| 609 FunctionKind function_kind_; | |
| 610 | |
| 611 // Debugging support. | |
| 612 const AstRawString* scope_name_; | |
| 613 | |
| 614 // The variables declared in this scope: | |
| 615 // | |
| 616 // All user-declared variables (incl. parameters). For script scopes | |
| 617 // variables may be implicitly 'declared' by being used (possibly in | |
| 618 // an inner scope) with no intervening with statements or eval calls. | |
| 619 VariableMap variables_; | |
| 620 // Compiler-allocated (user-invisible) temporaries. | |
| 621 ZoneList<Variable*> temps_; | |
| 622 // Parameter list in source order. | |
| 623 ZoneList<Variable*> params_; | |
| 624 // Variables that must be looked up dynamically. | |
| 625 DynamicScopePart* dynamics_; | |
| 626 // Unresolved variables referred to from this scope. | |
| 627 ZoneList<VariableProxy*> unresolved_; | |
| 628 // Declarations. | |
| 629 ZoneList<Declaration*> decls_; | |
| 630 // Convenience variable. | |
| 631 Variable* receiver_; | |
| 632 // Function variable, if any; function scopes only. | |
| 633 VariableDeclaration* function_; | |
| 634 // new.target variable, function scopes only. | |
| 635 Variable* new_target_; | |
| 636 // Convenience variable; function scopes only. | |
| 637 Variable* arguments_; | |
| 638 // Convenience variable; Subclass constructor only | |
| 639 Variable* this_function_; | |
| 640 // Module descriptor; module scopes only. | |
| 641 ModuleDescriptor* module_descriptor_; | |
| 642 | |
| 643 // Map of function names to lists of functions defined in sloppy blocks | |
| 644 SloppyBlockFunctionMap sloppy_block_function_map_; | |
| 645 | |
| 646 // Illegal redeclaration. | |
| 647 Expression* illegal_redecl_; | |
| 648 | |
| 649 // Scope-specific information computed during parsing. | |
| 650 // | |
| 651 // This scope is inside a 'with' of some outer scope. | |
| 652 bool scope_inside_with_; | |
| 653 // This scope contains a 'with' statement. | |
| 654 bool scope_contains_with_; | |
| 655 // This scope or a nested catch scope or with scope contain an 'eval' call. At | |
| 656 // the 'eval' call site this scope is the declaration scope. | |
| 657 bool scope_calls_eval_; | |
| 658 // This scope uses "arguments". | |
| 659 bool scope_uses_arguments_; | |
| 660 // This scope uses "super" property ('super.foo'). | |
| 661 bool scope_uses_super_property_; | |
| 662 // This scope contains an "use asm" annotation. | |
| 663 bool asm_module_; | |
| 664 // This scope's outer context is an asm module. | |
| 665 bool asm_function_; | |
| 666 // This scope's declarations might not be executed in order (e.g., switch). | |
| 667 bool scope_nonlinear_; | |
| 668 // The language mode of this scope. | |
| 669 LanguageMode language_mode_; | |
| 670 // Source positions. | |
| 671 int start_position_; | |
| 672 int end_position_; | |
| 673 | |
| 674 // Computed via PropagateScopeInfo. | |
| 675 bool outer_scope_calls_sloppy_eval_; | |
| 676 bool inner_scope_calls_eval_; | |
| 677 bool force_eager_compilation_; | |
| 678 bool force_context_allocation_; | |
| 679 | |
| 680 // True if it doesn't need scope resolution (e.g., if the scope was | |
| 681 // constructed based on a serialized scope info or a catch context). | |
| 682 bool already_resolved_; | |
| 683 | |
| 684 // True if it holds 'var' declarations. | |
| 685 bool is_declaration_scope_; | |
| 686 | |
| 687 // Computed as variables are declared. | |
| 688 int num_var_or_const_; | |
| 689 | |
| 690 // Computed via AllocateVariables; function, block and catch scopes only. | |
| 691 int num_stack_slots_; | |
| 692 int num_heap_slots_; | |
| 693 int num_global_slots_; | |
| 694 | |
| 695 // The number of modules (including nested ones). | |
| 696 int num_modules_; | |
| 697 | |
| 698 // For module scopes, the host scope's temporary variable binding this module. | |
| 699 Variable* module_var_; | |
| 700 | |
| 701 // Info about the parameter list of a function. | |
| 702 int arity_; | |
| 703 bool has_simple_parameters_; | |
| 704 Variable* rest_parameter_; | |
| 705 int rest_index_; | |
| 706 | |
| 707 // Serialized scope info support. | |
| 708 Handle<ScopeInfo> scope_info_; | |
| 709 bool already_resolved() { return already_resolved_; } | |
| 710 | |
| 711 // Create a non-local variable with a given name. | |
| 712 // These variables are looked up dynamically at runtime. | |
| 713 Variable* NonLocal(const AstRawString* name, VariableMode mode); | |
| 714 | |
| 715 // Variable resolution. | |
| 716 // Possible results of a recursive variable lookup telling if and how a | |
| 717 // variable is bound. These are returned in the output parameter *binding_kind | |
| 718 // of the LookupRecursive function. | |
| 719 enum BindingKind { | |
| 720 // The variable reference could be statically resolved to a variable binding | |
| 721 // which is returned. There is no 'with' statement between the reference and | |
| 722 // the binding and no scope between the reference scope (inclusive) and | |
| 723 // binding scope (exclusive) makes a sloppy 'eval' call. | |
| 724 BOUND, | |
| 725 | |
| 726 // The variable reference could be statically resolved to a variable binding | |
| 727 // which is returned. There is no 'with' statement between the reference and | |
| 728 // the binding, but some scope between the reference scope (inclusive) and | |
| 729 // binding scope (exclusive) makes a sloppy 'eval' call, that might | |
| 730 // possibly introduce variable bindings shadowing the found one. Thus the | |
| 731 // found variable binding is just a guess. | |
| 732 BOUND_EVAL_SHADOWED, | |
| 733 | |
| 734 // The variable reference could not be statically resolved to any binding | |
| 735 // and thus should be considered referencing a global variable. NULL is | |
| 736 // returned. The variable reference is not inside any 'with' statement and | |
| 737 // no scope between the reference scope (inclusive) and script scope | |
| 738 // (exclusive) makes a sloppy 'eval' call. | |
| 739 UNBOUND, | |
| 740 | |
| 741 // The variable reference could not be statically resolved to any binding | |
| 742 // NULL is returned. The variable reference is not inside any 'with' | |
| 743 // statement, but some scope between the reference scope (inclusive) and | |
| 744 // script scope (exclusive) makes a sloppy 'eval' call, that might | |
| 745 // possibly introduce a variable binding. Thus the reference should be | |
| 746 // considered referencing a global variable unless it is shadowed by an | |
| 747 // 'eval' introduced binding. | |
| 748 UNBOUND_EVAL_SHADOWED, | |
| 749 | |
| 750 // The variable could not be statically resolved and needs to be looked up | |
| 751 // dynamically. NULL is returned. There are two possible reasons: | |
| 752 // * A 'with' statement has been encountered and there is no variable | |
| 753 // binding for the name between the variable reference and the 'with'. | |
| 754 // The variable potentially references a property of the 'with' object. | |
| 755 // * The code is being executed as part of a call to 'eval' and the calling | |
| 756 // context chain contains either a variable binding for the name or it | |
| 757 // contains a 'with' context. | |
| 758 DYNAMIC_LOOKUP | |
| 759 }; | |
| 760 | |
| 761 // Lookup a variable reference given by name recursively starting with this | |
| 762 // scope. If the code is executed because of a call to 'eval', the context | |
| 763 // parameter should be set to the calling context of 'eval'. | |
| 764 Variable* LookupRecursive(VariableProxy* proxy, BindingKind* binding_kind, | |
| 765 AstNodeFactory* factory); | |
| 766 MUST_USE_RESULT | |
| 767 bool ResolveVariable(ParseInfo* info, VariableProxy* proxy, | |
| 768 AstNodeFactory* factory); | |
| 769 MUST_USE_RESULT | |
| 770 bool ResolveVariablesRecursively(ParseInfo* info, AstNodeFactory* factory); | |
| 771 | |
| 772 bool CheckStrongModeDeclaration(VariableProxy* proxy, Variable* var); | |
| 773 | |
| 774 // If this scope is a method scope of a class, return the corresponding | |
| 775 // class variable, otherwise nullptr. | |
| 776 ClassVariable* ClassVariableForMethod() const; | |
| 777 | |
| 778 // Scope analysis. | |
| 779 void PropagateScopeInfo(bool outer_scope_calls_sloppy_eval); | |
| 780 bool HasTrivialContext() const; | |
| 781 | |
| 782 // Predicates. | |
| 783 bool MustAllocate(Variable* var); | |
| 784 bool MustAllocateInContext(Variable* var); | |
| 785 bool HasArgumentsParameter(Isolate* isolate); | |
| 786 | |
| 787 // Variable allocation. | |
| 788 void AllocateStackSlot(Variable* var); | |
| 789 void AllocateHeapSlot(Variable* var); | |
| 790 void AllocateParameterLocals(Isolate* isolate); | |
| 791 void AllocateNonParameterLocal(Isolate* isolate, Variable* var); | |
| 792 void AllocateDeclaredGlobal(Isolate* isolate, Variable* var); | |
| 793 void AllocateNonParameterLocalsAndDeclaredGlobals(Isolate* isolate); | |
| 794 void AllocateVariablesRecursively(Isolate* isolate); | |
| 795 void AllocateParameter(Variable* var, int index); | |
| 796 void AllocateReceiver(); | |
| 797 void AllocateModules(); | |
| 798 | |
| 799 // Resolve and fill in the allocation information for all variables | |
| 800 // in this scopes. Must be called *after* all scopes have been | |
| 801 // processed (parsed) to ensure that unresolved variables can be | |
| 802 // resolved properly. | |
| 803 // | |
| 804 // In the case of code compiled and run using 'eval', the context | |
| 805 // parameter is the context in which eval was called. In all other | |
| 806 // cases the context parameter is an empty handle. | |
| 807 MUST_USE_RESULT | |
| 808 bool AllocateVariables(ParseInfo* info, AstNodeFactory* factory); | |
| 809 | |
| 810 // Construct a scope based on the scope info. | |
| 811 Scope(Zone* zone, Scope* inner_scope, ScopeType type, | |
| 812 Handle<ScopeInfo> scope_info, AstValueFactory* value_factory); | |
| 813 | |
| 814 // Construct a catch scope with a binding for the name. | |
| 815 Scope(Zone* zone, Scope* inner_scope, const AstRawString* catch_variable_name, | |
| 816 AstValueFactory* value_factory); | |
| 817 | |
| 818 void AddInnerScope(Scope* inner_scope) { | |
| 819 if (inner_scope != NULL) { | |
| 820 inner_scopes_.Add(inner_scope, zone_); | |
| 821 inner_scope->outer_scope_ = this; | |
| 822 } | |
| 823 } | |
| 824 | |
| 825 void RemoveInnerScope(Scope* inner_scope) { | |
| 826 DCHECK_NOT_NULL(inner_scope); | |
| 827 for (int i = 0; i < inner_scopes_.length(); i++) { | |
| 828 if (inner_scopes_[i] == inner_scope) { | |
| 829 inner_scopes_.Remove(i); | |
| 830 break; | |
| 831 } | |
| 832 } | |
| 833 } | |
| 834 | |
| 835 void SetDefaults(ScopeType type, Scope* outer_scope, | |
| 836 Handle<ScopeInfo> scope_info, | |
| 837 FunctionKind function_kind = kNormalFunction); | |
| 838 | |
| 839 AstValueFactory* ast_value_factory_; | |
| 840 Zone* zone_; | |
| 841 | |
| 842 PendingCompilationErrorHandler pending_error_handler_; | |
| 843 | |
| 844 // For tracking which classes are declared consecutively. Needed for strong | |
| 845 // mode. | |
| 846 int class_declaration_group_start_; | |
| 847 }; | |
| 848 | |
| 849 } // namespace internal | |
| 850 } // namespace v8 | |
| 851 | |
| 852 #endif // V8_SCOPES_H_ | |
| OLD | NEW |