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_PARSING_PREPARSER_H_ 6 #define V8_PARSING_PREPARSER_H_ 7 8 #include "src/ast/ast-value-factory.h" 9 #include "src/ast/ast.h" 10 #include "src/ast/scopes.h" 11 #include "src/parsing/parse-info.h" 12 #include "src/parsing/parser-base.h" 13 #include "src/parsing/pending-compilation-error-handler.h" 14 #include "src/parsing/preparser-logger.h" 15 16 namespace v8 { 17 namespace internal { 18 19 // Whereas the Parser generates AST during the recursive descent, 20 // the PreParser doesn't create a tree. Instead, it passes around minimal 21 // data objects (PreParserExpression, PreParserIdentifier etc.) which contain 22 // just enough data for the upper layer functions. PreParserFactory is 23 // responsible for creating these dummy objects. It provides a similar kind of 24 // interface as AstNodeFactory, so ParserBase doesn't need to care which one is 25 // used. 26 27 class PreparseDataBuilder; 28 29 class PreParserIdentifier { 30 public: PreParserIdentifier()31 PreParserIdentifier() : type_(kUnknownIdentifier) {} Default()32 static PreParserIdentifier Default() { 33 return PreParserIdentifier(kUnknownIdentifier); 34 } Null()35 static PreParserIdentifier Null() { 36 return PreParserIdentifier(kNullIdentifier); 37 } Eval()38 static PreParserIdentifier Eval() { 39 return PreParserIdentifier(kEvalIdentifier); 40 } Arguments()41 static PreParserIdentifier Arguments() { 42 return PreParserIdentifier(kArgumentsIdentifier); 43 } Constructor()44 static PreParserIdentifier Constructor() { 45 return PreParserIdentifier(kConstructorIdentifier); 46 } Await()47 static PreParserIdentifier Await() { 48 return PreParserIdentifier(kAwaitIdentifier); 49 } Async()50 static PreParserIdentifier Async() { 51 return PreParserIdentifier(kAsyncIdentifier); 52 } Name()53 static PreParserIdentifier Name() { 54 return PreParserIdentifier(kNameIdentifier); 55 } PrivateName()56 static PreParserIdentifier PrivateName() { 57 return PreParserIdentifier(kPrivateNameIdentifier); 58 } IsNull()59 bool IsNull() const { return type_ == kNullIdentifier; } IsEval()60 bool IsEval() const { return type_ == kEvalIdentifier; } IsAsync()61 bool IsAsync() const { return type_ == kAsyncIdentifier; } IsArguments()62 bool IsArguments() const { return type_ == kArgumentsIdentifier; } IsEvalOrArguments()63 bool IsEvalOrArguments() const { 64 STATIC_ASSERT(kEvalIdentifier + 1 == kArgumentsIdentifier); 65 return base::IsInRange(type_, kEvalIdentifier, kArgumentsIdentifier); 66 } IsConstructor()67 bool IsConstructor() const { return type_ == kConstructorIdentifier; } IsAwait()68 bool IsAwait() const { return type_ == kAwaitIdentifier; } IsName()69 bool IsName() const { return type_ == kNameIdentifier; } IsPrivateName()70 bool IsPrivateName() const { return type_ == kPrivateNameIdentifier; } 71 72 private: 73 enum Type : uint8_t { 74 kNullIdentifier, 75 kUnknownIdentifier, 76 kEvalIdentifier, 77 kArgumentsIdentifier, 78 kConstructorIdentifier, 79 kAwaitIdentifier, 80 kAsyncIdentifier, 81 kNameIdentifier, 82 kPrivateNameIdentifier 83 }; 84 PreParserIdentifier(Type type)85 explicit PreParserIdentifier(Type type) : string_(nullptr), type_(type) {} 86 const AstRawString* string_; 87 88 Type type_; 89 friend class PreParserExpression; 90 friend class PreParser; 91 friend class PreParserFactory; 92 }; 93 94 class PreParserExpression { 95 public: PreParserExpression()96 PreParserExpression() : code_(TypeField::encode(kNull)) {} 97 Null()98 static PreParserExpression Null() { return PreParserExpression(); } Failure()99 static PreParserExpression Failure() { 100 return PreParserExpression(TypeField::encode(kFailure)); 101 } 102 Default()103 static PreParserExpression Default() { 104 return PreParserExpression(TypeField::encode(kExpression)); 105 } 106 Spread(const PreParserExpression & expression)107 static PreParserExpression Spread(const PreParserExpression& expression) { 108 return PreParserExpression(TypeField::encode(kSpreadExpression)); 109 } 110 FromIdentifier(const PreParserIdentifier & id)111 static PreParserExpression FromIdentifier(const PreParserIdentifier& id) { 112 return PreParserExpression(TypeField::encode(kIdentifierExpression) | 113 IdentifierTypeField::encode(id.type_)); 114 } 115 BinaryOperation(const PreParserExpression & left,Token::Value op,const PreParserExpression & right,Zone * zone)116 static PreParserExpression BinaryOperation(const PreParserExpression& left, 117 Token::Value op, 118 const PreParserExpression& right, 119 Zone* zone) { 120 return PreParserExpression(TypeField::encode(kExpression)); 121 } 122 Assignment()123 static PreParserExpression Assignment() { 124 return PreParserExpression(TypeField::encode(kExpression) | 125 ExpressionTypeField::encode(kAssignment)); 126 } 127 NewTargetExpression()128 static PreParserExpression NewTargetExpression() { 129 return PreParserExpression::Default(); 130 } 131 ObjectLiteral()132 static PreParserExpression ObjectLiteral() { 133 return PreParserExpression(TypeField::encode(kObjectLiteralExpression)); 134 } 135 ArrayLiteral()136 static PreParserExpression ArrayLiteral() { 137 return PreParserExpression(TypeField::encode(kArrayLiteralExpression)); 138 } 139 StringLiteral()140 static PreParserExpression StringLiteral() { 141 return PreParserExpression(TypeField::encode(kStringLiteralExpression)); 142 } 143 This()144 static PreParserExpression This() { 145 return PreParserExpression(TypeField::encode(kExpression) | 146 ExpressionTypeField::encode(kThisExpression)); 147 } 148 ThisPrivateReference()149 static PreParserExpression ThisPrivateReference() { 150 return PreParserExpression( 151 TypeField::encode(kExpression) | 152 ExpressionTypeField::encode(kThisPrivateReferenceExpression)); 153 } 154 ThisProperty()155 static PreParserExpression ThisProperty() { 156 return PreParserExpression( 157 TypeField::encode(kExpression) | 158 ExpressionTypeField::encode(kThisPropertyExpression)); 159 } 160 Property()161 static PreParserExpression Property() { 162 return PreParserExpression( 163 TypeField::encode(kExpression) | 164 ExpressionTypeField::encode(kPropertyExpression)); 165 } 166 PrivateReference()167 static PreParserExpression PrivateReference() { 168 return PreParserExpression( 169 TypeField::encode(kExpression) | 170 ExpressionTypeField::encode(kPrivateReferenceExpression)); 171 } 172 Call()173 static PreParserExpression Call() { 174 return PreParserExpression(TypeField::encode(kExpression) | 175 ExpressionTypeField::encode(kCallExpression)); 176 } 177 CallEval()178 static PreParserExpression CallEval() { 179 return PreParserExpression( 180 TypeField::encode(kExpression) | 181 ExpressionTypeField::encode(kCallEvalExpression)); 182 } 183 CallTaggedTemplate()184 static PreParserExpression CallTaggedTemplate() { 185 return PreParserExpression( 186 TypeField::encode(kExpression) | 187 ExpressionTypeField::encode(kCallTaggedTemplateExpression)); 188 } 189 is_tagged_template()190 bool is_tagged_template() const { 191 DCHECK(IsCall()); 192 return ExpressionTypeField::decode(code_) == kCallTaggedTemplateExpression; 193 } 194 SuperCallReference()195 static PreParserExpression SuperCallReference() { 196 return PreParserExpression( 197 TypeField::encode(kExpression) | 198 ExpressionTypeField::encode(kSuperCallReference)); 199 } 200 IsNull()201 bool IsNull() const { return TypeField::decode(code_) == kNull; } IsFailureExpression()202 bool IsFailureExpression() const { 203 return TypeField::decode(code_) == kFailure; 204 } 205 IsIdentifier()206 bool IsIdentifier() const { 207 return TypeField::decode(code_) == kIdentifierExpression; 208 } 209 AsIdentifier()210 PreParserIdentifier AsIdentifier() const { 211 DCHECK(IsIdentifier()); 212 return PreParserIdentifier(IdentifierTypeField::decode(code_)); 213 } 214 IsAssignment()215 bool IsAssignment() const { 216 return TypeField::decode(code_) == kExpression && 217 ExpressionTypeField::decode(code_) == kAssignment; 218 } 219 IsObjectLiteral()220 bool IsObjectLiteral() const { 221 return TypeField::decode(code_) == kObjectLiteralExpression; 222 } 223 IsArrayLiteral()224 bool IsArrayLiteral() const { 225 return TypeField::decode(code_) == kArrayLiteralExpression; 226 } 227 IsPattern()228 bool IsPattern() const { 229 STATIC_ASSERT(kObjectLiteralExpression + 1 == kArrayLiteralExpression); 230 return base::IsInRange(TypeField::decode(code_), kObjectLiteralExpression, 231 kArrayLiteralExpression); 232 } 233 IsStringLiteral()234 bool IsStringLiteral() const { 235 return TypeField::decode(code_) == kStringLiteralExpression; 236 } 237 IsThis()238 bool IsThis() const { 239 return TypeField::decode(code_) == kExpression && 240 ExpressionTypeField::decode(code_) == kThisExpression; 241 } 242 IsThisProperty()243 bool IsThisProperty() const { 244 return TypeField::decode(code_) == kExpression && 245 (ExpressionTypeField::decode(code_) == kThisPropertyExpression || 246 ExpressionTypeField::decode(code_) == 247 kThisPrivateReferenceExpression); 248 } 249 IsProperty()250 bool IsProperty() const { 251 return TypeField::decode(code_) == kExpression && 252 (ExpressionTypeField::decode(code_) == kPropertyExpression || 253 ExpressionTypeField::decode(code_) == kThisPropertyExpression || 254 ExpressionTypeField::decode(code_) == kPrivateReferenceExpression || 255 ExpressionTypeField::decode(code_) == 256 kThisPrivateReferenceExpression); 257 } 258 IsPrivateReference()259 bool IsPrivateReference() const { 260 return TypeField::decode(code_) == kExpression && 261 (ExpressionTypeField::decode(code_) == kPrivateReferenceExpression || 262 ExpressionTypeField::decode(code_) == 263 kThisPrivateReferenceExpression); 264 } 265 IsCall()266 bool IsCall() const { 267 return TypeField::decode(code_) == kExpression && 268 (ExpressionTypeField::decode(code_) == kCallExpression || 269 ExpressionTypeField::decode(code_) == kCallEvalExpression || 270 ExpressionTypeField::decode(code_) == 271 kCallTaggedTemplateExpression); 272 } AsCall()273 PreParserExpression* AsCall() { 274 if (IsCall()) return this; 275 return nullptr; 276 } 277 IsSuperCallReference()278 bool IsSuperCallReference() const { 279 return TypeField::decode(code_) == kExpression && 280 ExpressionTypeField::decode(code_) == kSuperCallReference; 281 } 282 IsValidReferenceExpression()283 bool IsValidReferenceExpression() const { 284 return IsIdentifier() || IsProperty(); 285 } 286 287 // At the moment PreParser doesn't track these expression types. IsFunctionLiteral()288 bool IsFunctionLiteral() const { return false; } IsCallNew()289 bool IsCallNew() const { return false; } 290 IsSpread()291 bool IsSpread() const { 292 return TypeField::decode(code_) == kSpreadExpression; 293 } 294 is_parenthesized()295 bool is_parenthesized() const { return IsParenthesizedField::decode(code_); } 296 mark_parenthesized()297 void mark_parenthesized() { 298 code_ = IsParenthesizedField::update(code_, true); 299 } 300 clear_parenthesized()301 void clear_parenthesized() { 302 code_ = IsParenthesizedField::update(code_, false); 303 } 304 AsFunctionLiteral()305 PreParserExpression AsFunctionLiteral() { return *this; } 306 307 // Dummy implementation for making expression->somefunc() work in both Parser 308 // and PreParser. 309 PreParserExpression* operator->() { return this; } 310 311 // More dummy implementations of things PreParser doesn't need to track: SetShouldEagerCompile()312 void SetShouldEagerCompile() {} 313 position()314 int position() const { return kNoSourcePosition; } set_function_token_position(int position)315 void set_function_token_position(int position) {} set_scope(Scope * scope)316 void set_scope(Scope* scope) {} set_suspend_count(int suspend_count)317 void set_suspend_count(int suspend_count) {} 318 319 private: 320 enum Type { 321 kNull, 322 kFailure, 323 kExpression, 324 kIdentifierExpression, 325 kStringLiteralExpression, 326 kSpreadExpression, 327 kObjectLiteralExpression, 328 kArrayLiteralExpression 329 }; 330 331 enum ExpressionType { 332 kThisExpression, 333 kThisPropertyExpression, 334 kThisPrivateReferenceExpression, 335 kPropertyExpression, 336 kPrivateReferenceExpression, 337 kCallExpression, 338 kCallEvalExpression, 339 kCallTaggedTemplateExpression, 340 kSuperCallReference, 341 kAssignment 342 }; 343 PreParserExpression(uint32_t expression_code)344 explicit PreParserExpression(uint32_t expression_code) 345 : code_(expression_code) {} 346 347 // The first three bits are for the Type. 348 using TypeField = base::BitField<Type, 0, 3>; 349 350 // The high order bit applies only to nodes which would inherit from the 351 // Expression ASTNode --- This is by necessity, due to the fact that 352 // Expression nodes may be represented as multiple Types, not exclusively 353 // through kExpression. 354 // TODO(caitp, adamk): clean up PreParserExpression bitfields. 355 using IsParenthesizedField = TypeField::Next<bool, 1>; 356 357 // The rest of the bits are interpreted depending on the value 358 // of the Type field, so they can share the storage. 359 using ExpressionTypeField = IsParenthesizedField::Next<ExpressionType, 4>; 360 using IdentifierTypeField = 361 IsParenthesizedField::Next<PreParserIdentifier::Type, 8>; 362 using HasCoverInitializedNameField = IsParenthesizedField::Next<bool, 1>; 363 364 uint32_t code_; 365 friend class PreParser; 366 friend class PreParserFactory; 367 friend class PreParserExpressionList; 368 }; 369 370 class PreParserStatement; 371 class PreParserStatementList { 372 public: PreParserStatementList()373 PreParserStatementList() : PreParserStatementList(false) {} 374 PreParserStatementList* operator->() { return this; } Add(const PreParserStatement & element,Zone * zone)375 void Add(const PreParserStatement& element, Zone* zone) {} Null()376 static PreParserStatementList Null() { return PreParserStatementList(true); } IsNull()377 bool IsNull() const { return is_null_; } 378 379 private: PreParserStatementList(bool is_null)380 explicit PreParserStatementList(bool is_null) : is_null_(is_null) {} 381 bool is_null_; 382 }; 383 384 class PreParserScopedStatementList { 385 public: PreParserScopedStatementList(std::vector<void * > * buffer)386 explicit PreParserScopedStatementList(std::vector<void*>* buffer) {} Rewind()387 void Rewind() {} MergeInto(const PreParserScopedStatementList * other)388 void MergeInto(const PreParserScopedStatementList* other) {} Add(const PreParserStatement & element)389 void Add(const PreParserStatement& element) {} length()390 int length() { return 0; } 391 }; 392 393 // The pre-parser doesn't need to build lists of expressions, identifiers, or 394 // the like. If the PreParser is used in variable tracking mode, it needs to 395 // build lists of variables though. 396 class PreParserExpressionList { 397 public: PreParserExpressionList(std::vector<void * > * buffer)398 explicit PreParserExpressionList(std::vector<void*>* buffer) : length_(0) {} 399 length()400 int length() const { return length_; } 401 Add(const PreParserExpression & expression)402 void Add(const PreParserExpression& expression) { 403 ++length_; 404 } 405 406 private: 407 int length_; 408 409 friend class PreParser; 410 friend class PreParserFactory; 411 }; 412 413 class PreParserStatement { 414 public: Default()415 static PreParserStatement Default() { 416 return PreParserStatement(kUnknownStatement); 417 } 418 Iteration()419 static PreParserStatement Iteration() { 420 return PreParserStatement(kIterationStatement); 421 } 422 Null()423 static PreParserStatement Null() { 424 return PreParserStatement(kNullStatement); 425 } 426 Empty()427 static PreParserStatement Empty() { 428 return PreParserStatement(kEmptyStatement); 429 } 430 Jump()431 static PreParserStatement Jump() { 432 return PreParserStatement(kJumpStatement); 433 } 434 InitializeStatements(const PreParserScopedStatementList & statements,Zone * zone)435 void InitializeStatements(const PreParserScopedStatementList& statements, 436 Zone* zone) {} 437 438 // Creates expression statement from expression. 439 // Preserves being an unparenthesized string literal, possibly 440 // "use strict". ExpressionStatement(const PreParserExpression & expression)441 static PreParserStatement ExpressionStatement( 442 const PreParserExpression& expression) { 443 if (expression.IsStringLiteral()) { 444 return PreParserStatement(kStringLiteralExpressionStatement); 445 } 446 return Default(); 447 } 448 IsStringLiteral()449 bool IsStringLiteral() { return code_ == kStringLiteralExpressionStatement; } 450 IsJumpStatement()451 bool IsJumpStatement() { 452 return code_ == kJumpStatement; 453 } 454 IsNull()455 bool IsNull() { return code_ == kNullStatement; } 456 IsIterationStatement()457 bool IsIterationStatement() { return code_ == kIterationStatement; } 458 IsEmptyStatement()459 bool IsEmptyStatement() { 460 DCHECK(!IsNull()); 461 return code_ == kEmptyStatement; 462 } 463 464 // Dummy implementation for making statement->somefunc() work in both Parser 465 // and PreParser. 466 PreParserStatement* operator->() { return this; } 467 statements()468 PreParserStatementList statements() { return PreParserStatementList(); } cases()469 PreParserStatementList cases() { return PreParserStatementList(); } 470 set_scope(Scope * scope)471 void set_scope(Scope* scope) {} 472 void Initialize(const PreParserExpression& cond, PreParserStatement body, 473 const SourceRange& body_range = {}) {} 474 void Initialize(PreParserStatement init, const PreParserExpression& cond, 475 PreParserStatement next, PreParserStatement body, 476 const SourceRange& body_range = {}) {} 477 void Initialize(PreParserExpression each, const PreParserExpression& subject, 478 PreParserStatement body, const SourceRange& body_range = {}) { 479 } 480 481 protected: 482 enum Type { 483 kNullStatement, 484 kEmptyStatement, 485 kUnknownStatement, 486 kJumpStatement, 487 kIterationStatement, 488 kStringLiteralExpressionStatement, 489 }; 490 PreParserStatement(Type code)491 explicit PreParserStatement(Type code) : code_(code) {} 492 493 private: 494 Type code_; 495 }; 496 497 // A PreParserBlock extends statement with a place to store the scope. 498 // The scope is dropped as the block is returned as a statement. 499 class PreParserBlock : public PreParserStatement { 500 public: set_scope(Scope * scope)501 void set_scope(Scope* scope) { scope_ = scope; } scope()502 Scope* scope() const { return scope_; } Default()503 static PreParserBlock Default() { 504 return PreParserBlock(PreParserStatement::kUnknownStatement); 505 } Null()506 static PreParserBlock Null() { 507 return PreParserBlock(PreParserStatement::kNullStatement); 508 } 509 // Dummy implementation for making block->somefunc() work in both Parser and 510 // PreParser. 511 PreParserBlock* operator->() { return this; } 512 513 private: PreParserBlock(PreParserStatement::Type type)514 explicit PreParserBlock(PreParserStatement::Type type) 515 : PreParserStatement(type), scope_(nullptr) {} 516 Scope* scope_; 517 }; 518 519 class PreParserFactory { 520 public: PreParserFactory(AstValueFactory * ast_value_factory,Zone * zone)521 explicit PreParserFactory(AstValueFactory* ast_value_factory, Zone* zone) 522 : ast_node_factory_(ast_value_factory, zone), zone_(zone) {} 523 ast_node_factory()524 AstNodeFactory* ast_node_factory() { return &ast_node_factory_; } 525 NewStringLiteral(const PreParserIdentifier & identifier,int pos)526 PreParserExpression NewStringLiteral(const PreParserIdentifier& identifier, 527 int pos) { 528 return PreParserExpression::Default(); 529 } NewNumberLiteral(double number,int pos)530 PreParserExpression NewNumberLiteral(double number, 531 int pos) { 532 return PreParserExpression::Default(); 533 } NewUndefinedLiteral(int pos)534 PreParserExpression NewUndefinedLiteral(int pos) { 535 return PreParserExpression::Default(); 536 } NewTheHoleLiteral()537 PreParserExpression NewTheHoleLiteral() { 538 return PreParserExpression::Default(); 539 } NewRegExpLiteral(const AstRawString * js_pattern,int js_flags,int pos)540 PreParserExpression NewRegExpLiteral(const AstRawString* js_pattern, 541 int js_flags, int pos) { 542 return PreParserExpression::Default(); 543 } NewArrayLiteral(const PreParserExpressionList & values,int first_spread_index,int pos)544 PreParserExpression NewArrayLiteral(const PreParserExpressionList& values, 545 int first_spread_index, int pos) { 546 return PreParserExpression::ArrayLiteral(); 547 } NewClassLiteralProperty(const PreParserExpression & key,const PreParserExpression & value,ClassLiteralProperty::Kind kind,bool is_static,bool is_computed_name,bool is_private)548 PreParserExpression NewClassLiteralProperty(const PreParserExpression& key, 549 const PreParserExpression& value, 550 ClassLiteralProperty::Kind kind, 551 bool is_static, 552 bool is_computed_name, 553 bool is_private) { 554 return PreParserExpression::Default(); 555 } NewObjectLiteralProperty(const PreParserExpression & key,const PreParserExpression & value,ObjectLiteralProperty::Kind kind,bool is_computed_name)556 PreParserExpression NewObjectLiteralProperty(const PreParserExpression& key, 557 const PreParserExpression& value, 558 ObjectLiteralProperty::Kind kind, 559 bool is_computed_name) { 560 return PreParserExpression::Default(); 561 } NewObjectLiteralProperty(const PreParserExpression & key,const PreParserExpression & value,bool is_computed_name)562 PreParserExpression NewObjectLiteralProperty(const PreParserExpression& key, 563 const PreParserExpression& value, 564 bool is_computed_name) { 565 return PreParserExpression::Default(); 566 } 567 PreParserExpression NewObjectLiteral( 568 const PreParserExpressionList& properties, int boilerplate_properties, 569 int pos, bool has_rest_property, Variable* home_object = nullptr) { 570 return PreParserExpression::ObjectLiteral(); 571 } NewVariableProxy(void * variable)572 PreParserExpression NewVariableProxy(void* variable) { 573 return PreParserExpression::Default(); 574 } 575 NewOptionalChain(const PreParserExpression & expr)576 PreParserExpression NewOptionalChain(const PreParserExpression& expr) { 577 // Needed to track `delete a?.#b` early errors 578 if (expr.IsPrivateReference()) { 579 return PreParserExpression::PrivateReference(); 580 } 581 return PreParserExpression::Default(); 582 } 583 584 PreParserExpression NewProperty(const PreParserExpression& obj, 585 const PreParserExpression& key, int pos, 586 bool optional_chain = false) { 587 if (key.IsIdentifier() && key.AsIdentifier().IsPrivateName()) { 588 if (obj.IsThis()) { 589 return PreParserExpression::ThisPrivateReference(); 590 } 591 return PreParserExpression::PrivateReference(); 592 } 593 594 if (obj.IsThis()) { 595 return PreParserExpression::ThisProperty(); 596 } 597 return PreParserExpression::Property(); 598 } NewUnaryOperation(Token::Value op,const PreParserExpression & expression,int pos)599 PreParserExpression NewUnaryOperation(Token::Value op, 600 const PreParserExpression& expression, 601 int pos) { 602 return PreParserExpression::Default(); 603 } NewBinaryOperation(Token::Value op,const PreParserExpression & left,const PreParserExpression & right,int pos)604 PreParserExpression NewBinaryOperation(Token::Value op, 605 const PreParserExpression& left, 606 const PreParserExpression& right, 607 int pos) { 608 return PreParserExpression::BinaryOperation(left, op, right, zone_); 609 } NewCompareOperation(Token::Value op,const PreParserExpression & left,const PreParserExpression & right,int pos)610 PreParserExpression NewCompareOperation(Token::Value op, 611 const PreParserExpression& left, 612 const PreParserExpression& right, 613 int pos) { 614 return PreParserExpression::Default(); 615 } NewAssignment(Token::Value op,const PreParserExpression & left,const PreParserExpression & right,int pos)616 PreParserExpression NewAssignment(Token::Value op, 617 const PreParserExpression& left, 618 const PreParserExpression& right, int pos) { 619 // Identifiers need to be tracked since this might be a parameter with a 620 // default value inside an arrow function parameter list. 621 return PreParserExpression::Assignment(); 622 } NewYield(const PreParserExpression & expression,int pos,Suspend::OnAbruptResume on_abrupt_resume)623 PreParserExpression NewYield(const PreParserExpression& expression, int pos, 624 Suspend::OnAbruptResume on_abrupt_resume) { 625 return PreParserExpression::Default(); 626 } NewAwait(const PreParserExpression & expression,int pos)627 PreParserExpression NewAwait(const PreParserExpression& expression, int pos) { 628 return PreParserExpression::Default(); 629 } NewYieldStar(const PreParserExpression & iterable,int pos)630 PreParserExpression NewYieldStar(const PreParserExpression& iterable, 631 int pos) { 632 return PreParserExpression::Default(); 633 } NewConditional(const PreParserExpression & condition,const PreParserExpression & then_expression,const PreParserExpression & else_expression,int pos)634 PreParserExpression NewConditional(const PreParserExpression& condition, 635 const PreParserExpression& then_expression, 636 const PreParserExpression& else_expression, 637 int pos) { 638 return PreParserExpression::Default(); 639 } NewCountOperation(Token::Value op,bool is_prefix,const PreParserExpression & expression,int pos)640 PreParserExpression NewCountOperation(Token::Value op, bool is_prefix, 641 const PreParserExpression& expression, 642 int pos) { 643 return PreParserExpression::Default(); 644 } 645 PreParserExpression NewCall(PreParserExpression expression, 646 const PreParserExpressionList& arguments, int pos, 647 bool has_spread, 648 Call::PossiblyEval possibly_eval = Call::NOT_EVAL, 649 bool optional_chain = false) { 650 if (possibly_eval == Call::IS_POSSIBLY_EVAL) { 651 DCHECK(expression.IsIdentifier() && expression.AsIdentifier().IsEval()); 652 DCHECK(!optional_chain); 653 return PreParserExpression::CallEval(); 654 } 655 return PreParserExpression::Call(); 656 } NewTaggedTemplate(PreParserExpression expression,const PreParserExpressionList & arguments,int pos)657 PreParserExpression NewTaggedTemplate( 658 PreParserExpression expression, const PreParserExpressionList& arguments, 659 int pos) { 660 return PreParserExpression::CallTaggedTemplate(); 661 } NewCallNew(const PreParserExpression & expression,const PreParserExpressionList & arguments,int pos,bool has_spread)662 PreParserExpression NewCallNew(const PreParserExpression& expression, 663 const PreParserExpressionList& arguments, 664 int pos, bool has_spread) { 665 return PreParserExpression::Default(); 666 } 667 PreParserStatement NewReturnStatement( 668 const PreParserExpression& expression, int pos, 669 int continuation_pos = kNoSourcePosition) { 670 return PreParserStatement::Jump(); 671 } 672 PreParserStatement NewAsyncReturnStatement( 673 const PreParserExpression& expression, int pos, 674 int continuation_pos = kNoSourcePosition) { 675 return PreParserStatement::Jump(); 676 } 677 PreParserExpression NewFunctionLiteral( 678 const PreParserIdentifier& name, Scope* scope, 679 const PreParserScopedStatementList& body, int expected_property_count, 680 int parameter_count, int function_length, 681 FunctionLiteral::ParameterFlag has_duplicate_parameters, 682 FunctionSyntaxKind function_syntax_kind, 683 FunctionLiteral::EagerCompileHint eager_compile_hint, int position, 684 bool has_braces, int function_literal_id, 685 ProducedPreparseData* produced_preparse_data = nullptr) { 686 DCHECK_NULL(produced_preparse_data); 687 return PreParserExpression::Default(); 688 } 689 NewSpread(const PreParserExpression & expression,int pos,int expr_pos)690 PreParserExpression NewSpread(const PreParserExpression& expression, int pos, 691 int expr_pos) { 692 return PreParserExpression::Spread(expression); 693 } 694 NewEmptyParentheses(int pos)695 PreParserExpression NewEmptyParentheses(int pos) { 696 PreParserExpression result = PreParserExpression::Default(); 697 result.mark_parenthesized(); 698 return result; 699 } 700 EmptyStatement()701 PreParserStatement EmptyStatement() { return PreParserStatement::Default(); } 702 NewBlock(int capacity,bool ignore_completion_value)703 PreParserBlock NewBlock(int capacity, bool ignore_completion_value) { 704 return PreParserBlock::Default(); 705 } 706 NewBlock(bool ignore_completion_value,bool is_breakable)707 PreParserBlock NewBlock(bool ignore_completion_value, bool is_breakable) { 708 return PreParserBlock::Default(); 709 } 710 NewBlock(bool ignore_completion_value,const PreParserScopedStatementList & list)711 PreParserBlock NewBlock(bool ignore_completion_value, 712 const PreParserScopedStatementList& list) { 713 return PreParserBlock::Default(); 714 } 715 NewDebuggerStatement(int pos)716 PreParserStatement NewDebuggerStatement(int pos) { 717 return PreParserStatement::Default(); 718 } 719 NewExpressionStatement(const PreParserExpression & expr,int pos)720 PreParserStatement NewExpressionStatement(const PreParserExpression& expr, 721 int pos) { 722 return PreParserStatement::ExpressionStatement(expr); 723 } 724 725 PreParserStatement NewIfStatement(const PreParserExpression& condition, 726 PreParserStatement then_statement, 727 PreParserStatement else_statement, int pos, 728 SourceRange then_range = {}, 729 SourceRange else_range = {}) { 730 // This must return a jump statement iff both clauses are jump statements. 731 return else_statement.IsJumpStatement() ? then_statement : else_statement; 732 } 733 734 PreParserStatement NewBreakStatement( 735 PreParserStatement target, int pos, 736 int continuation_pos = kNoSourcePosition) { 737 return PreParserStatement::Jump(); 738 } 739 740 PreParserStatement NewContinueStatement( 741 PreParserStatement target, int pos, 742 int continuation_pos = kNoSourcePosition) { 743 return PreParserStatement::Jump(); 744 } 745 NewWithStatement(Scope * scope,const PreParserExpression & expression,PreParserStatement statement,int pos)746 PreParserStatement NewWithStatement(Scope* scope, 747 const PreParserExpression& expression, 748 PreParserStatement statement, int pos) { 749 return PreParserStatement::Default(); 750 } 751 NewDoWhileStatement(int pos)752 PreParserStatement NewDoWhileStatement(int pos) { 753 return PreParserStatement::Iteration(); 754 } 755 NewWhileStatement(int pos)756 PreParserStatement NewWhileStatement(int pos) { 757 return PreParserStatement::Iteration(); 758 } 759 NewSwitchStatement(const PreParserExpression & tag,int pos)760 PreParserStatement NewSwitchStatement(const PreParserExpression& tag, 761 int pos) { 762 return PreParserStatement::Default(); 763 } 764 NewCaseClause(const PreParserExpression & label,const PreParserScopedStatementList & statements)765 PreParserStatement NewCaseClause( 766 const PreParserExpression& label, 767 const PreParserScopedStatementList& statements) { 768 return PreParserStatement::Default(); 769 } 770 NewForStatement(int pos)771 PreParserStatement NewForStatement(int pos) { 772 return PreParserStatement::Iteration(); 773 } 774 NewForEachStatement(ForEachStatement::VisitMode visit_mode,int pos)775 PreParserStatement NewForEachStatement(ForEachStatement::VisitMode visit_mode, 776 int pos) { 777 return PreParserStatement::Iteration(); 778 } 779 NewForOfStatement(int pos,IteratorType type)780 PreParserStatement NewForOfStatement(int pos, IteratorType type) { 781 return PreParserStatement::Iteration(); 782 } 783 NewCallRuntime(Runtime::FunctionId id,ZoneChunkList<PreParserExpression> * arguments,int pos)784 PreParserExpression NewCallRuntime( 785 Runtime::FunctionId id, ZoneChunkList<PreParserExpression>* arguments, 786 int pos) { 787 return PreParserExpression::Default(); 788 } 789 NewImportCallExpression(const PreParserExpression & args,int pos)790 PreParserExpression NewImportCallExpression(const PreParserExpression& args, 791 int pos) { 792 return PreParserExpression::Default(); 793 } 794 NewImportCallExpression(const PreParserExpression & specifier,const PreParserExpression & import_assertions,int pos)795 PreParserExpression NewImportCallExpression( 796 const PreParserExpression& specifier, 797 const PreParserExpression& import_assertions, int pos) { 798 return PreParserExpression::Default(); 799 } 800 801 private: 802 // For creating VariableProxy objects to track unresolved variables. 803 AstNodeFactory ast_node_factory_; 804 Zone* zone_; 805 }; 806 807 class PreParser; 808 809 class PreParserFormalParameters : public FormalParametersBase { 810 public: PreParserFormalParameters(DeclarationScope * scope)811 explicit PreParserFormalParameters(DeclarationScope* scope) 812 : FormalParametersBase(scope) {} 813 set_has_duplicate()814 void set_has_duplicate() { has_duplicate_ = true; } has_duplicate()815 bool has_duplicate() { return has_duplicate_; } 816 void ValidateDuplicate(PreParser* preparser) const; 817 set_strict_parameter_error(const Scanner::Location & loc,MessageTemplate message)818 void set_strict_parameter_error(const Scanner::Location& loc, 819 MessageTemplate message) { 820 strict_parameter_error_ = loc.IsValid(); 821 } 822 void ValidateStrictMode(PreParser* preparser) const; 823 824 private: 825 bool has_duplicate_ = false; 826 bool strict_parameter_error_ = false; 827 }; 828 829 class PreParserFuncNameInferrer { 830 public: PreParserFuncNameInferrer(AstValueFactory * avf)831 explicit PreParserFuncNameInferrer(AstValueFactory* avf) {} 832 PreParserFuncNameInferrer(const PreParserFuncNameInferrer&) = delete; 833 PreParserFuncNameInferrer& operator=(const PreParserFuncNameInferrer&) = 834 delete; RemoveAsyncKeywordFromEnd()835 void RemoveAsyncKeywordFromEnd() const {} Infer()836 void Infer() const {} RemoveLastFunction()837 void RemoveLastFunction() const {} 838 839 class State { 840 public: State(PreParserFuncNameInferrer * fni)841 explicit State(PreParserFuncNameInferrer* fni) {} 842 State(const State&) = delete; 843 State& operator=(const State&) = delete; 844 }; 845 }; 846 847 class PreParserSourceRange { 848 public: 849 PreParserSourceRange() = default; PreParserSourceRange(int start,int end)850 PreParserSourceRange(int start, int end) {} Empty()851 static PreParserSourceRange Empty() { return PreParserSourceRange(); } OpenEnded(int32_t start)852 static PreParserSourceRange OpenEnded(int32_t start) { return Empty(); } ContinuationOf(const PreParserSourceRange & that,int end)853 static const PreParserSourceRange& ContinuationOf( 854 const PreParserSourceRange& that, int end) { 855 return that; 856 } 857 }; 858 859 class PreParserSourceRangeScope { 860 public: PreParserSourceRangeScope(Scanner * scanner,PreParserSourceRange * range)861 PreParserSourceRangeScope(Scanner* scanner, PreParserSourceRange* range) {} Finalize()862 const PreParserSourceRange& Finalize() const { return range_; } 863 864 private: 865 PreParserSourceRange range_; 866 867 DISALLOW_IMPLICIT_CONSTRUCTORS(PreParserSourceRangeScope); 868 }; 869 870 class PreParserPropertyList {}; 871 872 template <> 873 struct ParserTypes<PreParser> { 874 using Base = ParserBase<PreParser>; 875 using Impl = PreParser; 876 877 // Return types for traversing functions. 878 using ClassLiteralProperty = PreParserExpression; 879 using ClassLiteralStaticElement = PreParserExpression; 880 using Expression = PreParserExpression; 881 using FunctionLiteral = PreParserExpression; 882 using ObjectLiteralProperty = PreParserExpression; 883 using Suspend = PreParserExpression; 884 using ExpressionList = PreParserExpressionList; 885 using ObjectPropertyList = PreParserExpressionList; 886 using FormalParameters = PreParserFormalParameters; 887 using Identifier = PreParserIdentifier; 888 using ClassPropertyList = PreParserPropertyList; 889 using ClassStaticElementList = PreParserPropertyList; 890 using StatementList = PreParserScopedStatementList; 891 using Block = PreParserBlock; 892 using BreakableStatement = PreParserStatement; 893 using ForStatement = PreParserStatement; 894 using IterationStatement = PreParserStatement; 895 using Statement = PreParserStatement; 896 897 // For constructing objects returned by the traversing functions. 898 using Factory = PreParserFactory; 899 900 // Other implementation-specific tasks. 901 using FuncNameInferrer = PreParserFuncNameInferrer; 902 using SourceRange = PreParserSourceRange; 903 using SourceRangeScope = PreParserSourceRangeScope; 904 }; 905 906 907 // Preparsing checks a JavaScript program and emits preparse-data that helps 908 // a later parsing to be faster. 909 // See preparse-data-format.h for the data format. 910 911 // The PreParser checks that the syntax follows the grammar for JavaScript, 912 // and collects some information about the program along the way. 913 // The grammar check is only performed in order to understand the program 914 // sufficiently to deduce some information about it, that can be used 915 // to speed up later parsing. Finding errors is not the goal of pre-parsing, 916 // rather it is to speed up properly written and correct programs. 917 // That means that contextual checks (like a label being declared where 918 // it is used) are generally omitted. 919 class PreParser : public ParserBase<PreParser> { 920 friend class ParserBase<PreParser>; 921 922 public: 923 using Identifier = PreParserIdentifier; 924 using Expression = PreParserExpression; 925 using Statement = PreParserStatement; 926 927 enum PreParseResult { 928 kPreParseStackOverflow, 929 kPreParseNotIdentifiableError, 930 kPreParseSuccess 931 }; 932 933 PreParser(Zone* zone, Scanner* scanner, uintptr_t stack_limit, 934 AstValueFactory* ast_value_factory, 935 PendingCompilationErrorHandler* pending_error_handler, 936 RuntimeCallStats* runtime_call_stats, Logger* logger, 937 UnoptimizedCompileFlags flags, bool parsing_on_main_thread = true) 938 : ParserBase<PreParser>(zone, scanner, stack_limit, ast_value_factory, 939 pending_error_handler, runtime_call_stats, logger, 940 flags, parsing_on_main_thread), 941 use_counts_(nullptr), 942 preparse_data_builder_(nullptr), 943 preparse_data_builder_buffer_() { 944 preparse_data_builder_buffer_.reserve(16); 945 } 946 947 static bool IsPreParser() { return true; } 948 949 PreParserLogger* logger() { return &log_; } 950 951 // Pre-parse the program from the character stream; returns true on 952 // success (even if parsing failed, the pre-parse data successfully 953 // captured the syntax error), and false if a stack-overflow happened 954 // during parsing. 955 V8_EXPORT_PRIVATE PreParseResult PreParseProgram(); 956 957 // Parses a single function literal, from the opening parentheses before 958 // parameters to the closing brace after the body. 959 // Returns a FunctionEntry describing the body of the function in enough 960 // detail that it can be lazily compiled. 961 // The scanner is expected to have matched the "function" or "function*" 962 // keyword and parameters, and have consumed the initial '{'. 963 // At return, unless an error occurred, the scanner is positioned before the 964 // the final '}'. 965 PreParseResult PreParseFunction( 966 const AstRawString* function_name, FunctionKind kind, 967 FunctionSyntaxKind function_syntax_kind, DeclarationScope* function_scope, 968 int* use_counts, ProducedPreparseData** produced_preparser_scope_data); 969 970 PreparseDataBuilder* preparse_data_builder() const { 971 return preparse_data_builder_; 972 } 973 974 void set_preparse_data_builder(PreparseDataBuilder* preparse_data_builder) { 975 preparse_data_builder_ = preparse_data_builder; 976 } 977 978 std::vector<void*>* preparse_data_builder_buffer() { 979 return &preparse_data_builder_buffer_; 980 } 981 982 private: 983 friend class i::ExpressionScope<ParserTypes<PreParser>>; 984 friend class i::VariableDeclarationParsingScope<ParserTypes<PreParser>>; 985 friend class i::ParameterDeclarationParsingScope<ParserTypes<PreParser>>; 986 friend class i::ArrowHeadParsingScope<ParserTypes<PreParser>>; 987 friend class PreParserFormalParameters; 988 // These types form an algebra over syntactic categories that is just 989 // rich enough to let us recognize and propagate the constructs that 990 // are either being counted in the preparser data, or is important 991 // to throw the correct syntax error exceptions. 992 993 // All ParseXXX functions take as the last argument an *ok parameter 994 // which is set to false if parsing failed; it is unchanged otherwise. 995 // By making the 'exception handling' explicit, we are forced to check 996 // for failure at the call sites. 997 998 // Indicates that we won't switch from the preparser to the preparser; we'll 999 // just stay where we are. 1000 bool AllowsLazyParsingWithoutUnresolvedVariables() const { return false; } 1001 bool parse_lazily() const { return false; } 1002 1003 PendingCompilationErrorHandler* pending_error_handler() { 1004 return pending_error_handler_; 1005 } 1006 1007 V8_INLINE bool SkipFunction(const AstRawString* name, FunctionKind kind, 1008 FunctionSyntaxKind function_syntax_kind, 1009 DeclarationScope* function_scope, 1010 int* num_parameters, int* function_length, 1011 ProducedPreparseData** produced_preparse_data) { 1012 UNREACHABLE(); 1013 } 1014 1015 Expression ParseFunctionLiteral( 1016 Identifier name, Scanner::Location function_name_location, 1017 FunctionNameValidity function_name_validity, FunctionKind kind, 1018 int function_token_pos, FunctionSyntaxKind function_syntax_kind, 1019 LanguageMode language_mode, 1020 ZonePtrList<const AstRawString>* arguments_for_wrapped_function); 1021 1022 PreParserExpression InitializeObjectLiteral(PreParserExpression literal) { 1023 return literal; 1024 } 1025 1026 bool HasCheckedSyntax() { return false; } 1027 1028 void ParseStatementListAndLogFunction(PreParserFormalParameters* formals); 1029 1030 struct TemplateLiteralState {}; 1031 1032 V8_INLINE TemplateLiteralState OpenTemplateLiteral(int pos) { 1033 return TemplateLiteralState(); 1034 } 1035 V8_INLINE void AddTemplateExpression(TemplateLiteralState* state, 1036 const PreParserExpression& expression) {} 1037 V8_INLINE void AddTemplateSpan(TemplateLiteralState* state, bool should_cook, 1038 bool tail) {} 1039 V8_INLINE PreParserExpression CloseTemplateLiteral( 1040 TemplateLiteralState* state, int start, const PreParserExpression& tag) { 1041 return PreParserExpression::Default(); 1042 } 1043 V8_INLINE bool IsPrivateReference(const PreParserExpression& expression) { 1044 return expression.IsPrivateReference(); 1045 } 1046 V8_INLINE void SetLanguageMode(Scope* scope, LanguageMode mode) { 1047 scope->SetLanguageMode(mode); 1048 } 1049 V8_INLINE void SetAsmModule() {} 1050 1051 V8_INLINE void PrepareGeneratorVariables() {} 1052 V8_INLINE void RewriteAsyncFunctionBody( 1053 const PreParserScopedStatementList* body, PreParserStatement block, 1054 const PreParserExpression& return_value) {} 1055 1056 V8_INLINE PreParserExpression 1057 RewriteReturn(const PreParserExpression& return_value, int pos) { 1058 return return_value; 1059 } 1060 V8_INLINE PreParserStatement 1061 RewriteSwitchStatement(PreParserStatement switch_statement, Scope* scope) { 1062 return PreParserStatement::Default(); 1063 } 1064 1065 Variable* DeclareVariable(const AstRawString* name, VariableKind kind, 1066 VariableMode mode, InitializationFlag init, 1067 Scope* scope, bool* was_added, int position) { 1068 return DeclareVariableName(name, mode, scope, was_added, position, kind); 1069 } 1070 1071 void DeclareAndBindVariable(const VariableProxy* proxy, VariableKind kind, 1072 VariableMode mode, Scope* scope, bool* was_added, 1073 int initializer_position) { 1074 Variable* var = DeclareVariableName(proxy->raw_name(), mode, scope, 1075 was_added, proxy->position(), kind); 1076 var->set_initializer_position(initializer_position); 1077 // Don't bother actually binding the proxy. 1078 } 1079 1080 Variable* DeclarePrivateVariableName(const AstRawString* name, 1081 ClassScope* scope, VariableMode mode, 1082 IsStaticFlag is_static_flag, 1083 bool* was_added) { 1084 DCHECK(IsConstVariableMode(mode)); 1085 return scope->DeclarePrivateName(name, mode, is_static_flag, was_added); 1086 } 1087 1088 Variable* DeclareVariableName(const AstRawString* name, VariableMode mode, 1089 Scope* scope, bool* was_added, 1090 int position = kNoSourcePosition, 1091 VariableKind kind = NORMAL_VARIABLE) { 1092 DCHECK(!IsPrivateMethodOrAccessorVariableMode(mode)); 1093 Variable* var = scope->DeclareVariableName(name, mode, was_added, kind); 1094 if (var == nullptr) { 1095 ReportUnidentifiableError(); 1096 if (!IsLexicalVariableMode(mode)) scope = scope->GetDeclarationScope(); 1097 var = scope->LookupLocal(name); 1098 } else if (var->scope() != scope) { 1099 DCHECK_NE(kNoSourcePosition, position); 1100 DCHECK_EQ(VariableMode::kVar, mode); 1101 Declaration* nested_declaration = 1102 factory()->ast_node_factory()->NewNestedVariableDeclaration(scope, 1103 position); 1104 nested_declaration->set_var(var); 1105 var->scope()->declarations()->Add(nested_declaration); 1106 } 1107 return var; 1108 } 1109 1110 V8_INLINE PreParserBlock RewriteCatchPattern(CatchInfo* catch_info) { 1111 return PreParserBlock::Default(); 1112 } 1113 1114 V8_INLINE void ReportVarRedeclarationIn(const AstRawString* name, 1115 Scope* scope) { 1116 ReportUnidentifiableError(); 1117 } 1118 1119 V8_INLINE PreParserStatement RewriteTryStatement( 1120 PreParserStatement try_block, PreParserStatement catch_block, 1121 const SourceRange& catch_range, PreParserStatement finally_block, 1122 const SourceRange& finally_range, const CatchInfo& catch_info, int pos) { 1123 return PreParserStatement::Default(); 1124 } 1125 1126 V8_INLINE void ReportUnexpectedTokenAt( 1127 Scanner::Location location, Token::Value token, 1128 MessageTemplate message = MessageTemplate::kUnexpectedToken) { 1129 ReportUnidentifiableError(); 1130 } 1131 V8_INLINE void ParseAndRewriteGeneratorFunctionBody( 1132 int pos, FunctionKind kind, PreParserScopedStatementList* body) { 1133 ParseStatementList(body, Token::RBRACE); 1134 } 1135 V8_INLINE void ParseAndRewriteAsyncGeneratorFunctionBody( 1136 int pos, FunctionKind kind, PreParserScopedStatementList* body) { 1137 ParseStatementList(body, Token::RBRACE); 1138 } 1139 V8_INLINE void DeclareFunctionNameVar(const AstRawString* function_name, 1140 FunctionSyntaxKind function_syntax_kind, 1141 DeclarationScope* function_scope) { 1142 if (function_syntax_kind == FunctionSyntaxKind::kNamedExpression && 1143 function_scope->LookupLocal(function_name) == nullptr) { 1144 DCHECK_EQ(function_scope, scope()); 1145 function_scope->DeclareFunctionVar(function_name); 1146 } 1147 } 1148 1149 V8_INLINE void DeclareFunctionNameVar( 1150 const PreParserIdentifier& function_name, 1151 FunctionSyntaxKind function_syntax_kind, 1152 DeclarationScope* function_scope) { 1153 DeclareFunctionNameVar(function_name.string_, function_syntax_kind, 1154 function_scope); 1155 } 1156 1157 bool IdentifierEquals(const PreParserIdentifier& identifier, 1158 const AstRawString* other); 1159 1160 V8_INLINE PreParserStatement DeclareFunction( 1161 const PreParserIdentifier& variable_name, 1162 const PreParserExpression& function, VariableMode mode, VariableKind kind, 1163 int beg_pos, int end_pos, ZonePtrList<const AstRawString>* names) { 1164 DCHECK_NULL(names); 1165 bool was_added; 1166 Variable* var = DeclareVariableName(variable_name.string_, mode, scope(), 1167 &was_added, beg_pos, kind); 1168 if (kind == SLOPPY_BLOCK_FUNCTION_VARIABLE) { 1169 Token::Value init = 1170 loop_nesting_depth() > 0 ? Token::ASSIGN : Token::INIT; 1171 SloppyBlockFunctionStatement* statement = 1172 factory()->ast_node_factory()->NewSloppyBlockFunctionStatement( 1173 end_pos, var, init); 1174 GetDeclarationScope()->DeclareSloppyBlockFunction(statement); 1175 } 1176 return Statement::Default(); 1177 } 1178 1179 V8_INLINE PreParserStatement DeclareClass( 1180 const PreParserIdentifier& variable_name, 1181 const PreParserExpression& value, ZonePtrList<const AstRawString>* names, 1182 int class_token_pos, int end_pos) { 1183 // Preparser shouldn't be used in contexts where we need to track the names. 1184 DCHECK_NULL(names); 1185 bool was_added; 1186 DeclareVariableName(variable_name.string_, VariableMode::kLet, scope(), 1187 &was_added); 1188 return PreParserStatement::Default(); 1189 } 1190 V8_INLINE void DeclareClassVariable(ClassScope* scope, 1191 const PreParserIdentifier& name, 1192 ClassInfo* class_info, 1193 int class_token_pos) { 1194 DCHECK_IMPLIES(IsNull(name), class_info->is_anonymous); 1195 // Declare a special class variable for anonymous classes with the dot 1196 // if we need to save it for static private method access. 1197 scope->DeclareClassVariable(ast_value_factory(), name.string_, 1198 class_token_pos); 1199 } 1200 V8_INLINE void DeclarePublicClassMethod(const PreParserIdentifier& class_name, 1201 const PreParserExpression& property, 1202 bool is_constructor, 1203 ClassInfo* class_info) {} 1204 V8_INLINE void DeclarePublicClassField(ClassScope* scope, 1205 const PreParserExpression& property, 1206 bool is_static, bool is_computed_name, 1207 ClassInfo* class_info) { 1208 if (is_computed_name) { 1209 bool was_added; 1210 DeclareVariableName( 1211 ClassFieldVariableName(ast_value_factory(), 1212 class_info->computed_field_count), 1213 VariableMode::kConst, scope, &was_added); 1214 } 1215 } 1216 1217 V8_INLINE void DeclarePrivateClassMember( 1218 ClassScope* scope, const PreParserIdentifier& property_name, 1219 const PreParserExpression& property, ClassLiteralProperty::Kind kind, 1220 bool is_static, ClassInfo* class_info) { 1221 bool was_added; 1222 1223 DeclarePrivateVariableName( 1224 property_name.string_, scope, GetVariableMode(kind), 1225 is_static ? IsStaticFlag::kStatic : IsStaticFlag::kNotStatic, 1226 &was_added); 1227 if (!was_added) { 1228 Scanner::Location loc(property.position(), property.position() + 1); 1229 ReportMessageAt(loc, MessageTemplate::kVarRedeclaration, 1230 property_name.string_); 1231 } 1232 } 1233 1234 V8_INLINE void AddClassStaticBlock(PreParserBlock block, 1235 ClassInfo* class_info) { 1236 DCHECK(class_info->has_static_elements); 1237 } 1238 1239 V8_INLINE PreParserExpression 1240 RewriteClassLiteral(ClassScope* scope, const PreParserIdentifier& name, 1241 ClassInfo* class_info, int pos, int end_pos) { 1242 bool has_default_constructor = !class_info->has_seen_constructor; 1243 // Account for the default constructor. 1244 if (has_default_constructor) { 1245 // Creating and disposing of a FunctionState makes tracking of 1246 // next_function_is_likely_called match what Parser does. TODO(marja): 1247 // Make the lazy function + next_function_is_likely_called + default ctor 1248 // logic less surprising. Default ctors shouldn't affect the laziness of 1249 // functions. 1250 bool has_extends = class_info->extends.IsNull(); 1251 FunctionKind kind = has_extends ? FunctionKind::kDefaultDerivedConstructor 1252 : FunctionKind::kDefaultBaseConstructor; 1253 DeclarationScope* function_scope = NewFunctionScope(kind); 1254 SetLanguageMode(function_scope, LanguageMode::kStrict); 1255 function_scope->set_start_position(pos); 1256 function_scope->set_end_position(pos); 1257 FunctionState function_state(&function_state_, &scope_, function_scope); 1258 GetNextFunctionLiteralId(); 1259 } 1260 if (class_info->has_static_elements) { 1261 GetNextFunctionLiteralId(); 1262 } 1263 if (class_info->has_instance_members) { 1264 GetNextFunctionLiteralId(); 1265 } 1266 return PreParserExpression::Default(); 1267 } 1268 1269 V8_INLINE PreParserStatement DeclareNative(const PreParserIdentifier& name, 1270 int pos) { 1271 return PreParserStatement::Default(); 1272 } 1273 1274 V8_INLINE void QueueDestructuringAssignmentForRewriting( 1275 PreParserExpression assignment) {} 1276 1277 // Helper functions for recursive descent. 1278 V8_INLINE bool IsEval(const PreParserIdentifier& identifier) const { 1279 return identifier.IsEval(); 1280 } 1281 1282 V8_INLINE bool IsAsync(const PreParserIdentifier& identifier) const { 1283 return identifier.IsAsync(); 1284 } 1285 1286 V8_INLINE bool IsArguments(const PreParserIdentifier& identifier) const { 1287 return identifier.IsArguments(); 1288 } 1289 1290 V8_INLINE bool IsEvalOrArguments( 1291 const PreParserIdentifier& identifier) const { 1292 return identifier.IsEvalOrArguments(); 1293 } 1294 1295 // Returns true if the expression is of type "this.foo". 1296 V8_INLINE static bool IsThisProperty(const PreParserExpression& expression) { 1297 return expression.IsThisProperty(); 1298 } 1299 1300 V8_INLINE static bool IsIdentifier(const PreParserExpression& expression) { 1301 return expression.IsIdentifier(); 1302 } 1303 1304 V8_INLINE static PreParserIdentifier AsIdentifier( 1305 const PreParserExpression& expression) { 1306 return expression.AsIdentifier(); 1307 } 1308 1309 V8_INLINE static PreParserExpression AsIdentifierExpression( 1310 const PreParserExpression& expression) { 1311 return expression; 1312 } 1313 1314 V8_INLINE bool IsConstructor(const PreParserIdentifier& identifier) const { 1315 return identifier.IsConstructor(); 1316 } 1317 1318 V8_INLINE bool IsName(const PreParserIdentifier& identifier) const { 1319 return identifier.IsName(); 1320 } 1321 1322 V8_INLINE static bool IsBoilerplateProperty( 1323 const PreParserExpression& property) { 1324 // PreParser doesn't count boilerplate properties. 1325 return false; 1326 } 1327 1328 V8_INLINE bool ParsingExtension() const { 1329 // Preparsing is disabled for extensions (because the extension 1330 // details aren't passed to lazily compiled functions), so we 1331 // don't accept "native function" in the preparser and there is 1332 // no need to keep track of "native". 1333 return false; 1334 } 1335 1336 V8_INLINE bool IsNative(const PreParserExpression& expr) const { 1337 // Preparsing is disabled for extensions (because the extension 1338 // details aren't passed to lazily compiled functions), so we 1339 // don't accept "native function" in the preparser and there is 1340 // no need to keep track of "native". 1341 return false; 1342 } 1343 1344 V8_INLINE static bool IsArrayIndex(const PreParserIdentifier& string, 1345 uint32_t* index) { 1346 return false; 1347 } 1348 1349 V8_INLINE bool IsStringLiteral(PreParserStatement statement) const { 1350 return statement.IsStringLiteral(); 1351 } 1352 1353 V8_INLINE static void GetDefaultStrings( 1354 PreParserIdentifier* default_string, 1355 PreParserIdentifier* dot_default_string) {} 1356 1357 // Functions for encapsulating the differences between parsing and preparsing; 1358 // operations interleaved with the recursive descent. 1359 V8_INLINE static void PushLiteralName(const PreParserIdentifier& id) {} 1360 V8_INLINE static void PushVariableName(const PreParserIdentifier& id) {} 1361 V8_INLINE void PushPropertyName(const PreParserExpression& expression) {} 1362 V8_INLINE void PushEnclosingName(const PreParserIdentifier& name) {} 1363 V8_INLINE static void AddFunctionForNameInference( 1364 const PreParserExpression& expression) {} 1365 V8_INLINE static void InferFunctionName() {} 1366 1367 V8_INLINE static void CheckAssigningFunctionLiteralToProperty( 1368 const PreParserExpression& left, const PreParserExpression& right) {} 1369 1370 V8_INLINE bool ShortcutNumericLiteralBinaryExpression( 1371 PreParserExpression* x, const PreParserExpression& y, Token::Value op, 1372 int pos) { 1373 return false; 1374 } 1375 1376 V8_INLINE NaryOperation* CollapseNaryExpression(PreParserExpression* x, 1377 PreParserExpression y, 1378 Token::Value op, int pos, 1379 const SourceRange& range) { 1380 x->clear_parenthesized(); 1381 return nullptr; 1382 } 1383 1384 V8_INLINE PreParserExpression BuildUnaryExpression( 1385 const PreParserExpression& expression, Token::Value op, int pos) { 1386 return PreParserExpression::Default(); 1387 } 1388 1389 V8_INLINE PreParserStatement 1390 BuildInitializationBlock(DeclarationParsingResult* parsing_result) { 1391 return PreParserStatement::Default(); 1392 } 1393 1394 V8_INLINE PreParserBlock RewriteForVarInLegacy(const ForInfo& for_info) { 1395 return PreParserBlock::Null(); 1396 } 1397 1398 V8_INLINE void DesugarBindingInForEachStatement( 1399 ForInfo* for_info, PreParserStatement* body_block, 1400 PreParserExpression* each_variable) { 1401 } 1402 1403 V8_INLINE PreParserBlock CreateForEachStatementTDZ(PreParserBlock init_block, 1404 const ForInfo& for_info) { 1405 if (IsLexicalVariableMode(for_info.parsing_result.descriptor.mode)) { 1406 for (auto name : for_info.bound_names) { 1407 bool was_added; 1408 DeclareVariableName(name, VariableMode::kLet, scope(), &was_added); 1409 } 1410 return PreParserBlock::Default(); 1411 } 1412 return init_block; 1413 } 1414 1415 V8_INLINE StatementT DesugarLexicalBindingsInForStatement( 1416 PreParserStatement loop, PreParserStatement init, 1417 const PreParserExpression& cond, PreParserStatement next, 1418 PreParserStatement body, Scope* inner_scope, const ForInfo& for_info) { 1419 // See Parser::DesugarLexicalBindingsInForStatement. 1420 for (auto name : for_info.bound_names) { 1421 bool was_added; 1422 DeclareVariableName(name, for_info.parsing_result.descriptor.mode, 1423 inner_scope, &was_added); 1424 } 1425 return loop; 1426 } 1427 1428 PreParserBlock BuildParameterInitializationBlock( 1429 const PreParserFormalParameters& parameters); 1430 1431 V8_INLINE PreParserBlock 1432 BuildRejectPromiseOnException(PreParserStatement init_block) { 1433 return PreParserBlock::Default(); 1434 } 1435 1436 V8_INLINE void InsertSloppyBlockFunctionVarBindings(DeclarationScope* scope) { 1437 scope->HoistSloppyBlockFunctions(nullptr); 1438 } 1439 1440 V8_INLINE void InsertShadowingVarBindingInitializers( 1441 PreParserStatement block) {} 1442 1443 V8_INLINE PreParserExpression NewThrowReferenceError(MessageTemplate message, 1444 int pos) { 1445 return PreParserExpression::Default(); 1446 } 1447 1448 V8_INLINE PreParserExpression NewThrowSyntaxError( 1449 MessageTemplate message, const PreParserIdentifier& arg, int pos) { 1450 return PreParserExpression::Default(); 1451 } 1452 1453 V8_INLINE PreParserExpression NewThrowTypeError( 1454 MessageTemplate message, const PreParserIdentifier& arg, int pos) { 1455 return PreParserExpression::Default(); 1456 } 1457 1458 V8_INLINE const AstRawString* PreParserIdentifierToAstRawString( 1459 const PreParserIdentifier& x) { 1460 return x.string_; 1461 } 1462 1463 V8_INLINE void ReportUnidentifiableError() { 1464 pending_error_handler()->set_unidentifiable_error(); 1465 scanner()->set_parser_error(); 1466 } 1467 1468 const AstRawString* GetRawNameFromIdentifier(const PreParserIdentifier& arg) { 1469 return arg.string_; 1470 } 1471 1472 PreParserStatement AsIterationStatement(PreParserStatement s) { return s; } 1473 1474 // "null" return type creators. 1475 V8_INLINE static PreParserIdentifier NullIdentifier() { 1476 return PreParserIdentifier::Null(); 1477 } 1478 V8_INLINE static PreParserExpression NullExpression() { 1479 return PreParserExpression::Null(); 1480 } 1481 V8_INLINE static PreParserExpression FailureExpression() { 1482 return PreParserExpression::Failure(); 1483 } 1484 V8_INLINE static PreParserExpression NullLiteralProperty() { 1485 return PreParserExpression::Null(); 1486 } 1487 V8_INLINE static PreParserStatementList NullStatementList() { 1488 return PreParserStatementList::Null(); 1489 } 1490 V8_INLINE static PreParserStatement NullStatement() { 1491 return PreParserStatement::Null(); 1492 } 1493 V8_INLINE static PreParserBlock NullBlock() { return PreParserBlock::Null(); } 1494 1495 template <typename T> 1496 V8_INLINE static bool IsNull(T subject) { 1497 return subject.IsNull(); 1498 } 1499 1500 V8_INLINE static bool IsIterationStatement(PreParserStatement subject) { 1501 return subject.IsIterationStatement(); 1502 } 1503 1504 V8_INLINE PreParserIdentifier EmptyIdentifierString() const { 1505 PreParserIdentifier result = PreParserIdentifier::Default(); 1506 result.string_ = ast_value_factory()->empty_string(); 1507 return result; 1508 } 1509 1510 // Producing data during the recursive descent. 1511 PreParserIdentifier GetSymbol() const { 1512 return PreParserIdentifier::Default(); 1513 } 1514 1515 PreParserIdentifier GetIdentifier() const; 1516 1517 V8_INLINE PreParserIdentifier GetNextSymbol() const { 1518 return PreParserIdentifier::Default(); 1519 } 1520 1521 V8_INLINE PreParserIdentifier GetNumberAsSymbol() const { 1522 return PreParserIdentifier::Default(); 1523 } 1524 1525 V8_INLINE PreParserIdentifier GetBigIntAsSymbol() const { 1526 return PreParserIdentifier::Default(); 1527 } 1528 1529 V8_INLINE PreParserExpression ThisExpression() { 1530 UseThis(); 1531 return PreParserExpression::This(); 1532 } 1533 1534 V8_INLINE PreParserExpression NewThisExpression(int pos) { 1535 UseThis(); 1536 return PreParserExpression::This(); 1537 } 1538 1539 V8_INLINE PreParserExpression 1540 NewSuperPropertyReference(Scope* home_object_scope, int pos) { 1541 return PreParserExpression::Default(); 1542 } 1543 1544 V8_INLINE PreParserExpression NewSuperCallReference(int pos) { 1545 scope()->NewUnresolved(factory()->ast_node_factory(), 1546 ast_value_factory()->this_function_string(), pos, 1547 NORMAL_VARIABLE); 1548 scope()->NewUnresolved(factory()->ast_node_factory(), 1549 ast_value_factory()->new_target_string(), pos, 1550 NORMAL_VARIABLE); 1551 return PreParserExpression::SuperCallReference(); 1552 } 1553 1554 V8_INLINE PreParserExpression NewTargetExpression(int pos) { 1555 return PreParserExpression::NewTargetExpression(); 1556 } 1557 1558 V8_INLINE PreParserExpression ImportMetaExpression(int pos) { 1559 return PreParserExpression::Default(); 1560 } 1561 1562 V8_INLINE PreParserExpression ExpressionFromLiteral(Token::Value token, 1563 int pos) { 1564 if (token != Token::STRING) return PreParserExpression::Default(); 1565 return PreParserExpression::StringLiteral(); 1566 } 1567 1568 PreParserExpression ExpressionFromPrivateName( 1569 PrivateNameScopeIterator* private_name_scope, 1570 const PreParserIdentifier& name, int start_position) { 1571 VariableProxy* proxy = factory()->ast_node_factory()->NewVariableProxy( 1572 name.string_, NORMAL_VARIABLE, start_position); 1573 private_name_scope->AddUnresolvedPrivateName(proxy); 1574 return PreParserExpression::FromIdentifier(name); 1575 } 1576 1577 PreParserExpression ExpressionFromIdentifier( 1578 const PreParserIdentifier& name, int start_position, 1579 InferName infer = InferName::kYes) { 1580 expression_scope()->NewVariable(name.string_, start_position); 1581 return PreParserExpression::FromIdentifier(name); 1582 } 1583 1584 V8_INLINE void DeclareIdentifier(const PreParserIdentifier& name, 1585 int start_position) { 1586 expression_scope()->Declare(name.string_, start_position); 1587 } 1588 1589 V8_INLINE Variable* DeclareCatchVariableName( 1590 Scope* scope, const PreParserIdentifier& identifier) { 1591 return scope->DeclareCatchVariableName(identifier.string_); 1592 } 1593 1594 V8_INLINE PreParserPropertyList NewClassPropertyList(int size) const { 1595 return PreParserPropertyList(); 1596 } 1597 1598 V8_INLINE PreParserPropertyList NewClassStaticElementList(int size) const { 1599 return PreParserPropertyList(); 1600 } 1601 1602 V8_INLINE PreParserStatementList NewStatementList(int size) const { 1603 return PreParserStatementList(); 1604 } 1605 1606 V8_INLINE PreParserExpression 1607 NewV8Intrinsic(const PreParserIdentifier& name, 1608 const PreParserExpressionList& arguments, int pos) { 1609 return PreParserExpression::Default(); 1610 } 1611 1612 V8_INLINE PreParserStatement 1613 NewThrowStatement(const PreParserExpression& exception, int pos) { 1614 return PreParserStatement::Jump(); 1615 } 1616 1617 V8_INLINE void AddFormalParameter(PreParserFormalParameters* parameters, 1618 const PreParserExpression& pattern, 1619 const PreParserExpression& initializer, 1620 int initializer_end_position, 1621 bool is_rest) { 1622 DeclarationScope* scope = parameters->scope; 1623 scope->RecordParameter(is_rest); 1624 parameters->UpdateArityAndFunctionLength(!initializer.IsNull(), is_rest); 1625 } 1626 1627 V8_INLINE void DeclareFormalParameters( 1628 const PreParserFormalParameters* parameters) { 1629 if (!parameters->is_simple) parameters->scope->SetHasNonSimpleParameters(); 1630 } 1631 1632 V8_INLINE void DeclareArrowFunctionFormalParameters( 1633 PreParserFormalParameters* parameters, const PreParserExpression& params, 1634 const Scanner::Location& params_loc) { 1635 } 1636 1637 V8_INLINE PreParserExpression 1638 ExpressionListToExpression(const PreParserExpressionList& args) { 1639 return PreParserExpression::Default(); 1640 } 1641 1642 V8_INLINE void SetFunctionNameFromPropertyName( 1643 const PreParserExpression& property, const PreParserIdentifier& name, 1644 const AstRawString* prefix = nullptr) {} 1645 V8_INLINE void SetFunctionNameFromIdentifierRef( 1646 const PreParserExpression& value, const PreParserExpression& identifier) { 1647 } 1648 1649 V8_INLINE void CountUsage(v8::Isolate::UseCounterFeature feature) { 1650 if (use_counts_ != nullptr) ++use_counts_[feature]; 1651 } 1652 1653 V8_INLINE bool ParsingDynamicFunctionDeclaration() const { return false; } 1654 1655 // Generate empty functions here as the preparser does not collect source 1656 // ranges for block coverage. 1657 #define DEFINE_RECORD_SOURCE_RANGE(Name) \ 1658 template <typename... Ts> \ 1659 V8_INLINE void Record##Name##SourceRange(Ts... args) {} 1660 AST_SOURCE_RANGE_LIST(DEFINE_RECORD_SOURCE_RANGE) 1661 #undef DEFINE_RECORD_SOURCE_RANGE 1662 1663 // Preparser's private field members. 1664 1665 int* use_counts_; 1666 PreParserLogger log_; 1667 1668 PreparseDataBuilder* preparse_data_builder_; 1669 std::vector<void*> preparse_data_builder_buffer_; 1670 }; 1671 1672 } // namespace internal 1673 } // namespace v8 1674 1675 #endif // V8_PARSING_PREPARSER_H_ 1676