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() {} mark_as_oneshot_iife()313 void mark_as_oneshot_iife() {} 314 position()315 int position() const { return kNoSourcePosition; } set_function_token_position(int position)316 void set_function_token_position(int position) {} set_scope(Scope * scope)317 void set_scope(Scope* scope) {} set_suspend_count(int suspend_count)318 void set_suspend_count(int suspend_count) {} 319 320 private: 321 enum Type { 322 kNull, 323 kFailure, 324 kExpression, 325 kIdentifierExpression, 326 kStringLiteralExpression, 327 kSpreadExpression, 328 kObjectLiteralExpression, 329 kArrayLiteralExpression 330 }; 331 332 enum ExpressionType { 333 kThisExpression, 334 kThisPropertyExpression, 335 kThisPrivateReferenceExpression, 336 kPropertyExpression, 337 kPrivateReferenceExpression, 338 kCallExpression, 339 kCallEvalExpression, 340 kCallTaggedTemplateExpression, 341 kSuperCallReference, 342 kAssignment 343 }; 344 PreParserExpression(uint32_t expression_code)345 explicit PreParserExpression(uint32_t expression_code) 346 : code_(expression_code) {} 347 348 // The first three bits are for the Type. 349 using TypeField = base::BitField<Type, 0, 3>; 350 351 // The high order bit applies only to nodes which would inherit from the 352 // Expression ASTNode --- This is by necessity, due to the fact that 353 // Expression nodes may be represented as multiple Types, not exclusively 354 // through kExpression. 355 // TODO(caitp, adamk): clean up PreParserExpression bitfields. 356 using IsParenthesizedField = TypeField::Next<bool, 1>; 357 358 // The rest of the bits are interpreted depending on the value 359 // of the Type field, so they can share the storage. 360 using ExpressionTypeField = IsParenthesizedField::Next<ExpressionType, 4>; 361 using IdentifierTypeField = 362 IsParenthesizedField::Next<PreParserIdentifier::Type, 8>; 363 using HasCoverInitializedNameField = IsParenthesizedField::Next<bool, 1>; 364 365 uint32_t code_; 366 friend class PreParser; 367 friend class PreParserFactory; 368 friend class PreParserExpressionList; 369 }; 370 371 class PreParserStatement; 372 class PreParserStatementList { 373 public: PreParserStatementList()374 PreParserStatementList() : PreParserStatementList(false) {} 375 PreParserStatementList* operator->() { return this; } Add(const PreParserStatement & element,Zone * zone)376 void Add(const PreParserStatement& element, Zone* zone) {} Null()377 static PreParserStatementList Null() { return PreParserStatementList(true); } IsNull()378 bool IsNull() const { return is_null_; } 379 380 private: PreParserStatementList(bool is_null)381 explicit PreParserStatementList(bool is_null) : is_null_(is_null) {} 382 bool is_null_; 383 }; 384 385 class PreParserScopedStatementList { 386 public: PreParserScopedStatementList(std::vector<void * > * buffer)387 explicit PreParserScopedStatementList(std::vector<void*>* buffer) {} Rewind()388 void Rewind() {} MergeInto(const PreParserScopedStatementList * other)389 void MergeInto(const PreParserScopedStatementList* other) {} Add(const PreParserStatement & element)390 void Add(const PreParserStatement& element) {} length()391 int length() { return 0; } 392 }; 393 394 // The pre-parser doesn't need to build lists of expressions, identifiers, or 395 // the like. If the PreParser is used in variable tracking mode, it needs to 396 // build lists of variables though. 397 class PreParserExpressionList { 398 public: PreParserExpressionList(std::vector<void * > * buffer)399 explicit PreParserExpressionList(std::vector<void*>* buffer) : length_(0) {} 400 length()401 int length() const { return length_; } 402 Add(const PreParserExpression & expression)403 void Add(const PreParserExpression& expression) { 404 ++length_; 405 } 406 407 private: 408 int length_; 409 410 friend class PreParser; 411 friend class PreParserFactory; 412 }; 413 414 class PreParserStatement { 415 public: Default()416 static PreParserStatement Default() { 417 return PreParserStatement(kUnknownStatement); 418 } 419 Iteration()420 static PreParserStatement Iteration() { 421 return PreParserStatement(kIterationStatement); 422 } 423 Null()424 static PreParserStatement Null() { 425 return PreParserStatement(kNullStatement); 426 } 427 Empty()428 static PreParserStatement Empty() { 429 return PreParserStatement(kEmptyStatement); 430 } 431 Jump()432 static PreParserStatement Jump() { 433 return PreParserStatement(kJumpStatement); 434 } 435 InitializeStatements(const PreParserScopedStatementList & statements,Zone * zone)436 void InitializeStatements(const PreParserScopedStatementList& statements, 437 Zone* zone) {} 438 439 // Creates expression statement from expression. 440 // Preserves being an unparenthesized string literal, possibly 441 // "use strict". ExpressionStatement(const PreParserExpression & expression)442 static PreParserStatement ExpressionStatement( 443 const PreParserExpression& expression) { 444 if (expression.IsStringLiteral()) { 445 return PreParserStatement(kStringLiteralExpressionStatement); 446 } 447 return Default(); 448 } 449 IsStringLiteral()450 bool IsStringLiteral() { return code_ == kStringLiteralExpressionStatement; } 451 IsJumpStatement()452 bool IsJumpStatement() { 453 return code_ == kJumpStatement; 454 } 455 IsNull()456 bool IsNull() { return code_ == kNullStatement; } 457 IsIterationStatement()458 bool IsIterationStatement() { return code_ == kIterationStatement; } 459 IsEmptyStatement()460 bool IsEmptyStatement() { 461 DCHECK(!IsNull()); 462 return code_ == kEmptyStatement; 463 } 464 465 // Dummy implementation for making statement->somefunc() work in both Parser 466 // and PreParser. 467 PreParserStatement* operator->() { return this; } 468 statements()469 PreParserStatementList statements() { return PreParserStatementList(); } cases()470 PreParserStatementList cases() { return PreParserStatementList(); } 471 set_scope(Scope * scope)472 void set_scope(Scope* scope) {} 473 void Initialize(const PreParserExpression& cond, PreParserStatement body, 474 const SourceRange& body_range = {}) {} 475 void Initialize(PreParserStatement init, const PreParserExpression& cond, 476 PreParserStatement next, PreParserStatement body, 477 const SourceRange& body_range = {}) {} 478 void Initialize(PreParserExpression each, const PreParserExpression& subject, 479 PreParserStatement body, const SourceRange& body_range = {}) { 480 } 481 482 protected: 483 enum Type { 484 kNullStatement, 485 kEmptyStatement, 486 kUnknownStatement, 487 kJumpStatement, 488 kIterationStatement, 489 kStringLiteralExpressionStatement, 490 }; 491 PreParserStatement(Type code)492 explicit PreParserStatement(Type code) : code_(code) {} 493 494 private: 495 Type code_; 496 }; 497 498 // A PreParserBlock extends statement with a place to store the scope. 499 // The scope is dropped as the block is returned as a statement. 500 class PreParserBlock : public PreParserStatement { 501 public: set_scope(Scope * scope)502 void set_scope(Scope* scope) { scope_ = scope; } scope()503 Scope* scope() const { return scope_; } Default()504 static PreParserBlock Default() { 505 return PreParserBlock(PreParserStatement::kUnknownStatement); 506 } Null()507 static PreParserBlock Null() { 508 return PreParserBlock(PreParserStatement::kNullStatement); 509 } 510 // Dummy implementation for making block->somefunc() work in both Parser and 511 // PreParser. 512 PreParserBlock* operator->() { return this; } 513 514 private: PreParserBlock(PreParserStatement::Type type)515 explicit PreParserBlock(PreParserStatement::Type type) 516 : PreParserStatement(type), scope_(nullptr) {} 517 Scope* scope_; 518 }; 519 520 class PreParserFactory { 521 public: PreParserFactory(AstValueFactory * ast_value_factory,Zone * zone)522 explicit PreParserFactory(AstValueFactory* ast_value_factory, Zone* zone) 523 : ast_node_factory_(ast_value_factory, zone), zone_(zone) {} 524 ast_node_factory()525 AstNodeFactory* ast_node_factory() { return &ast_node_factory_; } 526 NewStringLiteral(const PreParserIdentifier & identifier,int pos)527 PreParserExpression NewStringLiteral(const PreParserIdentifier& identifier, 528 int pos) { 529 return PreParserExpression::Default(); 530 } NewNumberLiteral(double number,int pos)531 PreParserExpression NewNumberLiteral(double number, 532 int pos) { 533 return PreParserExpression::Default(); 534 } NewUndefinedLiteral(int pos)535 PreParserExpression NewUndefinedLiteral(int pos) { 536 return PreParserExpression::Default(); 537 } NewTheHoleLiteral()538 PreParserExpression NewTheHoleLiteral() { 539 return PreParserExpression::Default(); 540 } NewRegExpLiteral(const PreParserIdentifier & js_pattern,int js_flags,int pos)541 PreParserExpression NewRegExpLiteral(const PreParserIdentifier& js_pattern, 542 int js_flags, int pos) { 543 return PreParserExpression::Default(); 544 } NewArrayLiteral(const PreParserExpressionList & values,int first_spread_index,int pos)545 PreParserExpression NewArrayLiteral(const PreParserExpressionList& values, 546 int first_spread_index, int pos) { 547 return PreParserExpression::ArrayLiteral(); 548 } NewClassLiteralProperty(const PreParserExpression & key,const PreParserExpression & value,ClassLiteralProperty::Kind kind,bool is_static,bool is_computed_name,bool is_private)549 PreParserExpression NewClassLiteralProperty(const PreParserExpression& key, 550 const PreParserExpression& value, 551 ClassLiteralProperty::Kind kind, 552 bool is_static, 553 bool is_computed_name, 554 bool is_private) { 555 return PreParserExpression::Default(); 556 } NewObjectLiteralProperty(const PreParserExpression & key,const PreParserExpression & value,ObjectLiteralProperty::Kind kind,bool is_computed_name)557 PreParserExpression NewObjectLiteralProperty(const PreParserExpression& key, 558 const PreParserExpression& value, 559 ObjectLiteralProperty::Kind kind, 560 bool is_computed_name) { 561 return PreParserExpression::Default(); 562 } NewObjectLiteralProperty(const PreParserExpression & key,const PreParserExpression & value,bool is_computed_name)563 PreParserExpression NewObjectLiteralProperty(const PreParserExpression& key, 564 const PreParserExpression& value, 565 bool is_computed_name) { 566 return PreParserExpression::Default(); 567 } NewObjectLiteral(const PreParserExpressionList & properties,int boilerplate_properties,int pos,bool has_rest_property)568 PreParserExpression NewObjectLiteral( 569 const PreParserExpressionList& properties, int boilerplate_properties, 570 int pos, bool has_rest_property) { 571 return PreParserExpression::ObjectLiteral(); 572 } NewVariableProxy(void * variable)573 PreParserExpression NewVariableProxy(void* variable) { 574 return PreParserExpression::Default(); 575 } 576 NewOptionalChain(const PreParserExpression & expr)577 PreParserExpression NewOptionalChain(const PreParserExpression& expr) { 578 // Needed to track `delete a?.#b` early errors 579 if (expr.IsPrivateReference()) { 580 return PreParserExpression::PrivateReference(); 581 } 582 return PreParserExpression::Default(); 583 } 584 585 PreParserExpression NewProperty(const PreParserExpression& obj, 586 const PreParserExpression& key, int pos, 587 bool optional_chain = false) { 588 if (key.IsIdentifier() && key.AsIdentifier().IsPrivateName()) { 589 if (obj.IsThis()) { 590 return PreParserExpression::ThisPrivateReference(); 591 } 592 return PreParserExpression::PrivateReference(); 593 } 594 595 if (obj.IsThis()) { 596 return PreParserExpression::ThisProperty(); 597 } 598 return PreParserExpression::Property(); 599 } NewUnaryOperation(Token::Value op,const PreParserExpression & expression,int pos)600 PreParserExpression NewUnaryOperation(Token::Value op, 601 const PreParserExpression& expression, 602 int pos) { 603 return PreParserExpression::Default(); 604 } NewBinaryOperation(Token::Value op,const PreParserExpression & left,const PreParserExpression & right,int pos)605 PreParserExpression NewBinaryOperation(Token::Value op, 606 const PreParserExpression& left, 607 const PreParserExpression& right, 608 int pos) { 609 return PreParserExpression::BinaryOperation(left, op, right, zone_); 610 } NewCompareOperation(Token::Value op,const PreParserExpression & left,const PreParserExpression & right,int pos)611 PreParserExpression NewCompareOperation(Token::Value op, 612 const PreParserExpression& left, 613 const PreParserExpression& right, 614 int pos) { 615 return PreParserExpression::Default(); 616 } NewAssignment(Token::Value op,const PreParserExpression & left,const PreParserExpression & right,int pos)617 PreParserExpression NewAssignment(Token::Value op, 618 const PreParserExpression& left, 619 const PreParserExpression& right, int pos) { 620 // Identifiers need to be tracked since this might be a parameter with a 621 // default value inside an arrow function parameter list. 622 return PreParserExpression::Assignment(); 623 } NewYield(const PreParserExpression & expression,int pos,Suspend::OnAbruptResume on_abrupt_resume)624 PreParserExpression NewYield(const PreParserExpression& expression, int pos, 625 Suspend::OnAbruptResume on_abrupt_resume) { 626 return PreParserExpression::Default(); 627 } NewAwait(const PreParserExpression & expression,int pos)628 PreParserExpression NewAwait(const PreParserExpression& expression, int pos) { 629 return PreParserExpression::Default(); 630 } NewYieldStar(const PreParserExpression & iterable,int pos)631 PreParserExpression NewYieldStar(const PreParserExpression& iterable, 632 int pos) { 633 return PreParserExpression::Default(); 634 } NewConditional(const PreParserExpression & condition,const PreParserExpression & then_expression,const PreParserExpression & else_expression,int pos)635 PreParserExpression NewConditional(const PreParserExpression& condition, 636 const PreParserExpression& then_expression, 637 const PreParserExpression& else_expression, 638 int pos) { 639 return PreParserExpression::Default(); 640 } NewCountOperation(Token::Value op,bool is_prefix,const PreParserExpression & expression,int pos)641 PreParserExpression NewCountOperation(Token::Value op, bool is_prefix, 642 const PreParserExpression& expression, 643 int pos) { 644 return PreParserExpression::Default(); 645 } 646 PreParserExpression NewCall(PreParserExpression expression, 647 const PreParserExpressionList& arguments, int pos, 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)662 PreParserExpression NewCallNew(const PreParserExpression& expression, 663 const PreParserExpressionList& arguments, 664 int pos) { 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 795 private: 796 // For creating VariableProxy objects to track unresolved variables. 797 AstNodeFactory ast_node_factory_; 798 Zone* zone_; 799 }; 800 801 class PreParser; 802 803 class PreParserFormalParameters : public FormalParametersBase { 804 public: PreParserFormalParameters(DeclarationScope * scope)805 explicit PreParserFormalParameters(DeclarationScope* scope) 806 : FormalParametersBase(scope) {} 807 set_has_duplicate()808 void set_has_duplicate() { has_duplicate_ = true; } has_duplicate()809 bool has_duplicate() { return has_duplicate_; } 810 void ValidateDuplicate(PreParser* preparser) const; 811 set_strict_parameter_error(const Scanner::Location & loc,MessageTemplate message)812 void set_strict_parameter_error(const Scanner::Location& loc, 813 MessageTemplate message) { 814 strict_parameter_error_ = loc.IsValid(); 815 } 816 void ValidateStrictMode(PreParser* preparser) const; 817 818 private: 819 bool has_duplicate_ = false; 820 bool strict_parameter_error_ = false; 821 }; 822 823 class PreParserFuncNameInferrer { 824 public: PreParserFuncNameInferrer(AstValueFactory * avf)825 explicit PreParserFuncNameInferrer(AstValueFactory* avf) {} RemoveAsyncKeywordFromEnd()826 void RemoveAsyncKeywordFromEnd() const {} Infer()827 void Infer() const {} RemoveLastFunction()828 void RemoveLastFunction() const {} 829 830 class State { 831 public: State(PreParserFuncNameInferrer * fni)832 explicit State(PreParserFuncNameInferrer* fni) {} 833 834 private: 835 DISALLOW_COPY_AND_ASSIGN(State); 836 }; 837 838 private: 839 DISALLOW_COPY_AND_ASSIGN(PreParserFuncNameInferrer); 840 }; 841 842 class PreParserSourceRange { 843 public: 844 PreParserSourceRange() = default; PreParserSourceRange(int start,int end)845 PreParserSourceRange(int start, int end) {} Empty()846 static PreParserSourceRange Empty() { return PreParserSourceRange(); } OpenEnded(int32_t start)847 static PreParserSourceRange OpenEnded(int32_t start) { return Empty(); } ContinuationOf(const PreParserSourceRange & that,int end)848 static const PreParserSourceRange& ContinuationOf( 849 const PreParserSourceRange& that, int end) { 850 return that; 851 } 852 }; 853 854 class PreParserSourceRangeScope { 855 public: PreParserSourceRangeScope(Scanner * scanner,PreParserSourceRange * range)856 PreParserSourceRangeScope(Scanner* scanner, PreParserSourceRange* range) {} Finalize()857 const PreParserSourceRange& Finalize() const { return range_; } 858 859 private: 860 PreParserSourceRange range_; 861 862 DISALLOW_IMPLICIT_CONSTRUCTORS(PreParserSourceRangeScope); 863 }; 864 865 class PreParserPropertyList {}; 866 867 template <> 868 struct ParserTypes<PreParser> { 869 using Base = ParserBase<PreParser>; 870 using Impl = PreParser; 871 872 // Return types for traversing functions. 873 using ClassLiteralProperty = PreParserExpression; 874 using Expression = PreParserExpression; 875 using FunctionLiteral = PreParserExpression; 876 using ObjectLiteralProperty = PreParserExpression; 877 using Suspend = PreParserExpression; 878 using ExpressionList = PreParserExpressionList; 879 using ObjectPropertyList = PreParserExpressionList; 880 using FormalParameters = PreParserFormalParameters; 881 using Identifier = PreParserIdentifier; 882 using ClassPropertyList = PreParserPropertyList; 883 using StatementList = PreParserScopedStatementList; 884 using Block = PreParserBlock; 885 using BreakableStatement = PreParserStatement; 886 using ForStatement = PreParserStatement; 887 using IterationStatement = PreParserStatement; 888 using Statement = PreParserStatement; 889 890 // For constructing objects returned by the traversing functions. 891 using Factory = PreParserFactory; 892 893 // Other implementation-specific tasks. 894 using FuncNameInferrer = PreParserFuncNameInferrer; 895 using SourceRange = PreParserSourceRange; 896 using SourceRangeScope = PreParserSourceRangeScope; 897 }; 898 899 900 // Preparsing checks a JavaScript program and emits preparse-data that helps 901 // a later parsing to be faster. 902 // See preparse-data-format.h for the data format. 903 904 // The PreParser checks that the syntax follows the grammar for JavaScript, 905 // and collects some information about the program along the way. 906 // The grammar check is only performed in order to understand the program 907 // sufficiently to deduce some information about it, that can be used 908 // to speed up later parsing. Finding errors is not the goal of pre-parsing, 909 // rather it is to speed up properly written and correct programs. 910 // That means that contextual checks (like a label being declared where 911 // it is used) are generally omitted. 912 class PreParser : public ParserBase<PreParser> { 913 friend class ParserBase<PreParser>; 914 915 public: 916 using Identifier = PreParserIdentifier; 917 using Expression = PreParserExpression; 918 using Statement = PreParserStatement; 919 920 enum PreParseResult { 921 kPreParseStackOverflow, 922 kPreParseNotIdentifiableError, 923 kPreParseSuccess 924 }; 925 926 PreParser(Zone* zone, Scanner* scanner, uintptr_t stack_limit, 927 AstValueFactory* ast_value_factory, 928 PendingCompilationErrorHandler* pending_error_handler, 929 RuntimeCallStats* runtime_call_stats, Logger* logger, 930 UnoptimizedCompileFlags flags, bool parsing_on_main_thread = true) 931 : ParserBase<PreParser>(zone, scanner, stack_limit, nullptr, 932 ast_value_factory, pending_error_handler, 933 runtime_call_stats, logger, flags, 934 parsing_on_main_thread), 935 use_counts_(nullptr), 936 preparse_data_builder_(nullptr), 937 preparse_data_builder_buffer_() { 938 preparse_data_builder_buffer_.reserve(16); 939 } 940 941 static bool IsPreParser() { return true; } 942 943 PreParserLogger* logger() { return &log_; } 944 945 // Pre-parse the program from the character stream; returns true on 946 // success (even if parsing failed, the pre-parse data successfully 947 // captured the syntax error), and false if a stack-overflow happened 948 // during parsing. 949 V8_EXPORT_PRIVATE PreParseResult PreParseProgram(); 950 951 // Parses a single function literal, from the opening parentheses before 952 // parameters to the closing brace after the body. 953 // Returns a FunctionEntry describing the body of the function in enough 954 // detail that it can be lazily compiled. 955 // The scanner is expected to have matched the "function" or "function*" 956 // keyword and parameters, and have consumed the initial '{'. 957 // At return, unless an error occurred, the scanner is positioned before the 958 // the final '}'. 959 PreParseResult PreParseFunction( 960 const AstRawString* function_name, FunctionKind kind, 961 FunctionSyntaxKind function_syntax_kind, DeclarationScope* function_scope, 962 int* use_counts, ProducedPreparseData** produced_preparser_scope_data); 963 964 PreparseDataBuilder* preparse_data_builder() const { 965 return preparse_data_builder_; 966 } 967 968 void set_preparse_data_builder(PreparseDataBuilder* preparse_data_builder) { 969 preparse_data_builder_ = preparse_data_builder; 970 } 971 972 std::vector<void*>* preparse_data_builder_buffer() { 973 return &preparse_data_builder_buffer_; 974 } 975 976 private: 977 friend class i::ExpressionScope<ParserTypes<PreParser>>; 978 friend class i::VariableDeclarationParsingScope<ParserTypes<PreParser>>; 979 friend class i::ParameterDeclarationParsingScope<ParserTypes<PreParser>>; 980 friend class i::ArrowHeadParsingScope<ParserTypes<PreParser>>; 981 friend class PreParserFormalParameters; 982 // These types form an algebra over syntactic categories that is just 983 // rich enough to let us recognize and propagate the constructs that 984 // are either being counted in the preparser data, or is important 985 // to throw the correct syntax error exceptions. 986 987 // All ParseXXX functions take as the last argument an *ok parameter 988 // which is set to false if parsing failed; it is unchanged otherwise. 989 // By making the 'exception handling' explicit, we are forced to check 990 // for failure at the call sites. 991 992 // Indicates that we won't switch from the preparser to the preparser; we'll 993 // just stay where we are. 994 bool AllowsLazyParsingWithoutUnresolvedVariables() const { return false; } 995 bool parse_lazily() const { return false; } 996 997 PendingCompilationErrorHandler* pending_error_handler() { 998 return pending_error_handler_; 999 } 1000 1001 V8_INLINE bool SkipFunction(const AstRawString* name, FunctionKind kind, 1002 FunctionSyntaxKind function_syntax_kind, 1003 DeclarationScope* function_scope, 1004 int* num_parameters, int* function_length, 1005 ProducedPreparseData** produced_preparse_data) { 1006 UNREACHABLE(); 1007 } 1008 1009 Expression ParseFunctionLiteral( 1010 Identifier name, Scanner::Location function_name_location, 1011 FunctionNameValidity function_name_validity, FunctionKind kind, 1012 int function_token_pos, FunctionSyntaxKind function_syntax_kind, 1013 LanguageMode language_mode, 1014 ZonePtrList<const AstRawString>* arguments_for_wrapped_function); 1015 1016 PreParserExpression InitializeObjectLiteral(PreParserExpression literal) { 1017 return literal; 1018 } 1019 1020 bool HasCheckedSyntax() { return false; } 1021 1022 void ParseStatementListAndLogFunction(PreParserFormalParameters* formals); 1023 1024 struct TemplateLiteralState {}; 1025 1026 V8_INLINE TemplateLiteralState OpenTemplateLiteral(int pos) { 1027 return TemplateLiteralState(); 1028 } 1029 V8_INLINE void AddTemplateExpression(TemplateLiteralState* state, 1030 const PreParserExpression& expression) {} 1031 V8_INLINE void AddTemplateSpan(TemplateLiteralState* state, bool should_cook, 1032 bool tail) {} 1033 V8_INLINE PreParserExpression CloseTemplateLiteral( 1034 TemplateLiteralState* state, int start, const PreParserExpression& tag) { 1035 return PreParserExpression::Default(); 1036 } 1037 V8_INLINE bool IsPrivateReference(const PreParserExpression& expression) { 1038 return expression.IsPrivateReference(); 1039 } 1040 V8_INLINE void SetLanguageMode(Scope* scope, LanguageMode mode) { 1041 scope->SetLanguageMode(mode); 1042 } 1043 V8_INLINE void SetAsmModule() {} 1044 1045 V8_INLINE PreParserExpression SpreadCall(const PreParserExpression& function, 1046 const PreParserExpressionList& args, 1047 int pos, 1048 Call::PossiblyEval possibly_eval, 1049 bool optional_chain); 1050 V8_INLINE PreParserExpression 1051 SpreadCallNew(const PreParserExpression& function, 1052 const PreParserExpressionList& args, int pos); 1053 1054 V8_INLINE void PrepareGeneratorVariables() {} 1055 V8_INLINE void RewriteAsyncFunctionBody( 1056 const PreParserScopedStatementList* body, PreParserStatement block, 1057 const PreParserExpression& return_value) {} 1058 1059 V8_INLINE PreParserExpression 1060 RewriteReturn(const PreParserExpression& return_value, int pos) { 1061 return return_value; 1062 } 1063 V8_INLINE PreParserStatement 1064 RewriteSwitchStatement(PreParserStatement switch_statement, Scope* scope) { 1065 return PreParserStatement::Default(); 1066 } 1067 1068 Variable* DeclareVariable(const AstRawString* name, VariableKind kind, 1069 VariableMode mode, InitializationFlag init, 1070 Scope* scope, bool* was_added, int position) { 1071 return DeclareVariableName(name, mode, scope, was_added, position, kind); 1072 } 1073 1074 void DeclareAndBindVariable(const VariableProxy* proxy, VariableKind kind, 1075 VariableMode mode, Scope* scope, bool* was_added, 1076 int initializer_position) { 1077 Variable* var = DeclareVariableName(proxy->raw_name(), mode, scope, 1078 was_added, proxy->position(), kind); 1079 var->set_initializer_position(initializer_position); 1080 // Don't bother actually binding the proxy. 1081 } 1082 1083 Variable* DeclarePrivateVariableName(const AstRawString* name, 1084 ClassScope* scope, VariableMode mode, 1085 IsStaticFlag is_static_flag, 1086 bool* was_added) { 1087 DCHECK(IsConstVariableMode(mode)); 1088 return scope->DeclarePrivateName(name, mode, is_static_flag, was_added); 1089 } 1090 1091 Variable* DeclareVariableName(const AstRawString* name, VariableMode mode, 1092 Scope* scope, bool* was_added, 1093 int position = kNoSourcePosition, 1094 VariableKind kind = NORMAL_VARIABLE) { 1095 DCHECK(!IsPrivateMethodOrAccessorVariableMode(mode)); 1096 Variable* var = scope->DeclareVariableName(name, mode, was_added, kind); 1097 if (var == nullptr) { 1098 ReportUnidentifiableError(); 1099 if (!IsLexicalVariableMode(mode)) scope = scope->GetDeclarationScope(); 1100 var = scope->LookupLocal(name); 1101 } else if (var->scope() != scope) { 1102 DCHECK_NE(kNoSourcePosition, position); 1103 DCHECK_EQ(VariableMode::kVar, mode); 1104 Declaration* nested_declaration = 1105 factory()->ast_node_factory()->NewNestedVariableDeclaration(scope, 1106 position); 1107 nested_declaration->set_var(var); 1108 var->scope()->declarations()->Add(nested_declaration); 1109 } 1110 return var; 1111 } 1112 1113 V8_INLINE PreParserBlock RewriteCatchPattern(CatchInfo* catch_info) { 1114 return PreParserBlock::Default(); 1115 } 1116 1117 V8_INLINE void ReportVarRedeclarationIn(const AstRawString* name, 1118 Scope* scope) { 1119 ReportUnidentifiableError(); 1120 } 1121 1122 V8_INLINE PreParserStatement RewriteTryStatement( 1123 PreParserStatement try_block, PreParserStatement catch_block, 1124 const SourceRange& catch_range, PreParserStatement finally_block, 1125 const SourceRange& finally_range, const CatchInfo& catch_info, int pos) { 1126 return PreParserStatement::Default(); 1127 } 1128 1129 V8_INLINE void ReportUnexpectedTokenAt( 1130 Scanner::Location location, Token::Value token, 1131 MessageTemplate message = MessageTemplate::kUnexpectedToken) { 1132 ReportUnidentifiableError(); 1133 } 1134 V8_INLINE void ParseAndRewriteGeneratorFunctionBody( 1135 int pos, FunctionKind kind, PreParserScopedStatementList* body) { 1136 ParseStatementList(body, Token::RBRACE); 1137 } 1138 V8_INLINE void ParseAndRewriteAsyncGeneratorFunctionBody( 1139 int pos, FunctionKind kind, PreParserScopedStatementList* body) { 1140 ParseStatementList(body, Token::RBRACE); 1141 } 1142 V8_INLINE void DeclareFunctionNameVar(const AstRawString* function_name, 1143 FunctionSyntaxKind function_syntax_kind, 1144 DeclarationScope* function_scope) { 1145 if (function_syntax_kind == FunctionSyntaxKind::kNamedExpression && 1146 function_scope->LookupLocal(function_name) == nullptr) { 1147 DCHECK_EQ(function_scope, scope()); 1148 function_scope->DeclareFunctionVar(function_name); 1149 } 1150 } 1151 1152 V8_INLINE void DeclareFunctionNameVar( 1153 const PreParserIdentifier& function_name, 1154 FunctionSyntaxKind function_syntax_kind, 1155 DeclarationScope* function_scope) { 1156 DeclareFunctionNameVar(function_name.string_, function_syntax_kind, 1157 function_scope); 1158 } 1159 1160 bool IdentifierEquals(const PreParserIdentifier& identifier, 1161 const AstRawString* other); 1162 1163 V8_INLINE PreParserStatement DeclareFunction( 1164 const PreParserIdentifier& variable_name, 1165 const PreParserExpression& function, VariableMode mode, VariableKind kind, 1166 int beg_pos, int end_pos, ZonePtrList<const AstRawString>* names) { 1167 DCHECK_NULL(names); 1168 bool was_added; 1169 Variable* var = DeclareVariableName(variable_name.string_, mode, scope(), 1170 &was_added, beg_pos, kind); 1171 if (kind == SLOPPY_BLOCK_FUNCTION_VARIABLE) { 1172 Token::Value init = 1173 loop_nesting_depth() > 0 ? Token::ASSIGN : Token::INIT; 1174 SloppyBlockFunctionStatement* statement = 1175 factory()->ast_node_factory()->NewSloppyBlockFunctionStatement( 1176 end_pos, var, init); 1177 GetDeclarationScope()->DeclareSloppyBlockFunction(statement); 1178 } 1179 return Statement::Default(); 1180 } 1181 1182 V8_INLINE PreParserStatement DeclareClass( 1183 const PreParserIdentifier& variable_name, 1184 const PreParserExpression& value, ZonePtrList<const AstRawString>* names, 1185 int class_token_pos, int end_pos) { 1186 // Preparser shouldn't be used in contexts where we need to track the names. 1187 DCHECK_NULL(names); 1188 bool was_added; 1189 DeclareVariableName(variable_name.string_, VariableMode::kLet, scope(), 1190 &was_added); 1191 return PreParserStatement::Default(); 1192 } 1193 V8_INLINE void DeclareClassVariable(ClassScope* scope, 1194 const PreParserIdentifier& name, 1195 ClassInfo* class_info, 1196 int class_token_pos) { 1197 DCHECK_IMPLIES(IsNull(name), class_info->is_anonymous); 1198 // Declare a special class variable for anonymous classes with the dot 1199 // if we need to save it for static private method access. 1200 scope->DeclareClassVariable(ast_value_factory(), name.string_, 1201 class_token_pos); 1202 } 1203 V8_INLINE void DeclarePublicClassMethod(const PreParserIdentifier& class_name, 1204 const PreParserExpression& property, 1205 bool is_constructor, 1206 ClassInfo* class_info) {} 1207 V8_INLINE void DeclarePublicClassField(ClassScope* scope, 1208 const PreParserExpression& property, 1209 bool is_static, bool is_computed_name, 1210 ClassInfo* class_info) { 1211 if (is_computed_name) { 1212 bool was_added; 1213 DeclareVariableName( 1214 ClassFieldVariableName(ast_value_factory(), 1215 class_info->computed_field_count), 1216 VariableMode::kConst, scope, &was_added); 1217 } 1218 } 1219 1220 V8_INLINE void DeclarePrivateClassMember( 1221 ClassScope* scope, const PreParserIdentifier& property_name, 1222 const PreParserExpression& property, ClassLiteralProperty::Kind kind, 1223 bool is_static, ClassInfo* class_info) { 1224 bool was_added; 1225 1226 DeclarePrivateVariableName( 1227 property_name.string_, scope, GetVariableMode(kind), 1228 is_static ? IsStaticFlag::kStatic : IsStaticFlag::kNotStatic, 1229 &was_added); 1230 if (!was_added) { 1231 Scanner::Location loc(property.position(), property.position() + 1); 1232 ReportMessageAt(loc, MessageTemplate::kVarRedeclaration, 1233 property_name.string_); 1234 } 1235 } 1236 1237 V8_INLINE PreParserExpression 1238 RewriteClassLiteral(ClassScope* scope, const PreParserIdentifier& name, 1239 ClassInfo* class_info, int pos, int end_pos) { 1240 bool has_default_constructor = !class_info->has_seen_constructor; 1241 // Account for the default constructor. 1242 if (has_default_constructor) { 1243 // Creating and disposing of a FunctionState makes tracking of 1244 // next_function_is_likely_called match what Parser does. TODO(marja): 1245 // Make the lazy function + next_function_is_likely_called + default ctor 1246 // logic less surprising. Default ctors shouldn't affect the laziness of 1247 // functions. 1248 bool has_extends = class_info->extends.IsNull(); 1249 FunctionKind kind = has_extends ? FunctionKind::kDefaultDerivedConstructor 1250 : FunctionKind::kDefaultBaseConstructor; 1251 DeclarationScope* function_scope = NewFunctionScope(kind); 1252 SetLanguageMode(function_scope, LanguageMode::kStrict); 1253 function_scope->set_start_position(pos); 1254 function_scope->set_end_position(pos); 1255 FunctionState function_state(&function_state_, &scope_, function_scope); 1256 GetNextFunctionLiteralId(); 1257 } 1258 if (class_info->has_static_class_fields) { 1259 GetNextFunctionLiteralId(); 1260 } 1261 if (class_info->has_instance_members) { 1262 GetNextFunctionLiteralId(); 1263 } 1264 return PreParserExpression::Default(); 1265 } 1266 1267 V8_INLINE PreParserStatement DeclareNative(const PreParserIdentifier& name, 1268 int pos) { 1269 return PreParserStatement::Default(); 1270 } 1271 1272 V8_INLINE void QueueDestructuringAssignmentForRewriting( 1273 PreParserExpression assignment) {} 1274 1275 // Helper functions for recursive descent. 1276 V8_INLINE bool IsEval(const PreParserIdentifier& identifier) const { 1277 return identifier.IsEval(); 1278 } 1279 1280 V8_INLINE bool IsAsync(const PreParserIdentifier& identifier) const { 1281 return identifier.IsAsync(); 1282 } 1283 1284 V8_INLINE bool IsArguments(const PreParserIdentifier& identifier) const { 1285 return identifier.IsArguments(); 1286 } 1287 1288 V8_INLINE bool IsEvalOrArguments( 1289 const PreParserIdentifier& identifier) const { 1290 return identifier.IsEvalOrArguments(); 1291 } 1292 1293 // Returns true if the expression is of type "this.foo". 1294 V8_INLINE static bool IsThisProperty(const PreParserExpression& expression) { 1295 return expression.IsThisProperty(); 1296 } 1297 1298 V8_INLINE static bool IsIdentifier(const PreParserExpression& expression) { 1299 return expression.IsIdentifier(); 1300 } 1301 1302 V8_INLINE static PreParserIdentifier AsIdentifier( 1303 const PreParserExpression& expression) { 1304 return expression.AsIdentifier(); 1305 } 1306 1307 V8_INLINE static PreParserExpression AsIdentifierExpression( 1308 const PreParserExpression& expression) { 1309 return expression; 1310 } 1311 1312 V8_INLINE bool IsConstructor(const PreParserIdentifier& identifier) const { 1313 return identifier.IsConstructor(); 1314 } 1315 1316 V8_INLINE bool IsName(const PreParserIdentifier& identifier) const { 1317 return identifier.IsName(); 1318 } 1319 1320 V8_INLINE static bool IsBoilerplateProperty( 1321 const PreParserExpression& property) { 1322 // PreParser doesn't count boilerplate properties. 1323 return false; 1324 } 1325 1326 V8_INLINE bool IsNative(const PreParserExpression& expr) const { 1327 // Preparsing is disabled for extensions (because the extension 1328 // details aren't passed to lazily compiled functions), so we 1329 // don't accept "native function" in the preparser and there is 1330 // no need to keep track of "native". 1331 return false; 1332 } 1333 1334 V8_INLINE static bool IsArrayIndex(const PreParserIdentifier& string, 1335 uint32_t* index) { 1336 return false; 1337 } 1338 1339 V8_INLINE bool IsStringLiteral(PreParserStatement statement) const { 1340 return statement.IsStringLiteral(); 1341 } 1342 1343 V8_INLINE static void GetDefaultStrings( 1344 PreParserIdentifier* default_string, 1345 PreParserIdentifier* dot_default_string) {} 1346 1347 // Functions for encapsulating the differences between parsing and preparsing; 1348 // operations interleaved with the recursive descent. 1349 V8_INLINE static void PushLiteralName(const PreParserIdentifier& id) {} 1350 V8_INLINE static void PushVariableName(const PreParserIdentifier& id) {} 1351 V8_INLINE void PushPropertyName(const PreParserExpression& expression) {} 1352 V8_INLINE void PushEnclosingName(const PreParserIdentifier& name) {} 1353 V8_INLINE static void AddFunctionForNameInference( 1354 const PreParserExpression& expression) {} 1355 V8_INLINE static void InferFunctionName() {} 1356 1357 V8_INLINE static void CheckAssigningFunctionLiteralToProperty( 1358 const PreParserExpression& left, const PreParserExpression& right) {} 1359 1360 V8_INLINE bool ShortcutNumericLiteralBinaryExpression( 1361 PreParserExpression* x, const PreParserExpression& y, Token::Value op, 1362 int pos) { 1363 return false; 1364 } 1365 1366 V8_INLINE NaryOperation* CollapseNaryExpression(PreParserExpression* x, 1367 PreParserExpression y, 1368 Token::Value op, int pos, 1369 const SourceRange& range) { 1370 x->clear_parenthesized(); 1371 return nullptr; 1372 } 1373 1374 V8_INLINE PreParserExpression BuildUnaryExpression( 1375 const PreParserExpression& expression, Token::Value op, int pos) { 1376 return PreParserExpression::Default(); 1377 } 1378 1379 V8_INLINE PreParserStatement 1380 BuildInitializationBlock(DeclarationParsingResult* parsing_result) { 1381 return PreParserStatement::Default(); 1382 } 1383 1384 V8_INLINE PreParserBlock RewriteForVarInLegacy(const ForInfo& for_info) { 1385 return PreParserBlock::Null(); 1386 } 1387 1388 V8_INLINE void DesugarBindingInForEachStatement( 1389 ForInfo* for_info, PreParserStatement* body_block, 1390 PreParserExpression* each_variable) { 1391 } 1392 1393 V8_INLINE PreParserBlock CreateForEachStatementTDZ(PreParserBlock init_block, 1394 const ForInfo& for_info) { 1395 if (IsLexicalVariableMode(for_info.parsing_result.descriptor.mode)) { 1396 for (auto name : for_info.bound_names) { 1397 bool was_added; 1398 DeclareVariableName(name, VariableMode::kLet, scope(), &was_added); 1399 } 1400 return PreParserBlock::Default(); 1401 } 1402 return init_block; 1403 } 1404 1405 V8_INLINE StatementT DesugarLexicalBindingsInForStatement( 1406 PreParserStatement loop, PreParserStatement init, 1407 const PreParserExpression& cond, PreParserStatement next, 1408 PreParserStatement body, Scope* inner_scope, const ForInfo& for_info) { 1409 // See Parser::DesugarLexicalBindingsInForStatement. 1410 for (auto name : for_info.bound_names) { 1411 bool was_added; 1412 DeclareVariableName(name, for_info.parsing_result.descriptor.mode, 1413 inner_scope, &was_added); 1414 } 1415 return loop; 1416 } 1417 1418 PreParserBlock BuildParameterInitializationBlock( 1419 const PreParserFormalParameters& parameters); 1420 1421 V8_INLINE PreParserBlock 1422 BuildRejectPromiseOnException(PreParserStatement init_block) { 1423 return PreParserBlock::Default(); 1424 } 1425 1426 V8_INLINE void InsertSloppyBlockFunctionVarBindings(DeclarationScope* scope) { 1427 scope->HoistSloppyBlockFunctions(nullptr); 1428 } 1429 1430 V8_INLINE void InsertShadowingVarBindingInitializers( 1431 PreParserStatement block) {} 1432 1433 V8_INLINE PreParserExpression NewThrowReferenceError(MessageTemplate message, 1434 int pos) { 1435 return PreParserExpression::Default(); 1436 } 1437 1438 V8_INLINE PreParserExpression NewThrowSyntaxError( 1439 MessageTemplate message, const PreParserIdentifier& arg, int pos) { 1440 return PreParserExpression::Default(); 1441 } 1442 1443 V8_INLINE PreParserExpression NewThrowTypeError( 1444 MessageTemplate message, const PreParserIdentifier& arg, int pos) { 1445 return PreParserExpression::Default(); 1446 } 1447 1448 // Reporting errors. 1449 void ReportMessageAt(Scanner::Location source_location, 1450 MessageTemplate message, const char* arg = nullptr) { 1451 pending_error_handler()->ReportMessageAt( 1452 source_location.beg_pos, source_location.end_pos, message, arg); 1453 scanner()->set_parser_error(); 1454 } 1455 1456 V8_INLINE void ReportUnidentifiableError() { 1457 pending_error_handler()->set_unidentifiable_error(); 1458 scanner()->set_parser_error(); 1459 } 1460 1461 V8_INLINE void ReportMessageAt(Scanner::Location source_location, 1462 MessageTemplate message, 1463 const PreParserIdentifier& arg) { 1464 ReportMessageAt(source_location, message, arg.string_); 1465 } 1466 1467 void ReportMessageAt(Scanner::Location source_location, 1468 MessageTemplate message, const AstRawString* arg) { 1469 pending_error_handler()->ReportMessageAt( 1470 source_location.beg_pos, source_location.end_pos, message, arg); 1471 scanner()->set_parser_error(); 1472 } 1473 1474 const AstRawString* GetRawNameFromIdentifier(const PreParserIdentifier& arg) { 1475 return arg.string_; 1476 } 1477 1478 PreParserStatement AsIterationStatement(PreParserStatement s) { return s; } 1479 1480 // "null" return type creators. 1481 V8_INLINE static PreParserIdentifier NullIdentifier() { 1482 return PreParserIdentifier::Null(); 1483 } 1484 V8_INLINE static PreParserExpression NullExpression() { 1485 return PreParserExpression::Null(); 1486 } 1487 V8_INLINE static PreParserExpression FailureExpression() { 1488 return PreParserExpression::Failure(); 1489 } 1490 V8_INLINE static PreParserExpression NullLiteralProperty() { 1491 return PreParserExpression::Null(); 1492 } 1493 V8_INLINE static PreParserStatementList NullStatementList() { 1494 return PreParserStatementList::Null(); 1495 } 1496 V8_INLINE static PreParserStatement NullStatement() { 1497 return PreParserStatement::Null(); 1498 } 1499 V8_INLINE static PreParserBlock NullBlock() { return PreParserBlock::Null(); } 1500 1501 template <typename T> 1502 V8_INLINE static bool IsNull(T subject) { 1503 return subject.IsNull(); 1504 } 1505 1506 V8_INLINE static bool IsIterationStatement(PreParserStatement subject) { 1507 return subject.IsIterationStatement(); 1508 } 1509 1510 V8_INLINE PreParserIdentifier EmptyIdentifierString() const { 1511 PreParserIdentifier result = PreParserIdentifier::Default(); 1512 result.string_ = ast_value_factory()->empty_string(); 1513 return result; 1514 } 1515 1516 // Producing data during the recursive descent. 1517 PreParserIdentifier GetSymbol() const { 1518 return PreParserIdentifier::Default(); 1519 } 1520 1521 PreParserIdentifier GetIdentifier() const; 1522 1523 V8_INLINE PreParserIdentifier GetNextSymbol() const { 1524 return PreParserIdentifier::Default(); 1525 } 1526 1527 V8_INLINE PreParserIdentifier GetNumberAsSymbol() const { 1528 return PreParserIdentifier::Default(); 1529 } 1530 1531 V8_INLINE PreParserExpression ThisExpression() { 1532 UseThis(); 1533 return PreParserExpression::This(); 1534 } 1535 1536 V8_INLINE PreParserExpression NewThisExpression(int pos) { 1537 UseThis(); 1538 return PreParserExpression::This(); 1539 } 1540 1541 V8_INLINE PreParserExpression NewSuperPropertyReference(int pos) { 1542 scope()->NewUnresolved(factory()->ast_node_factory(), 1543 ast_value_factory()->this_function_string(), pos, 1544 NORMAL_VARIABLE); 1545 return PreParserExpression::Default(); 1546 } 1547 1548 V8_INLINE PreParserExpression NewSuperCallReference(int pos) { 1549 scope()->NewUnresolved(factory()->ast_node_factory(), 1550 ast_value_factory()->this_function_string(), pos, 1551 NORMAL_VARIABLE); 1552 scope()->NewUnresolved(factory()->ast_node_factory(), 1553 ast_value_factory()->new_target_string(), pos, 1554 NORMAL_VARIABLE); 1555 return PreParserExpression::SuperCallReference(); 1556 } 1557 1558 V8_INLINE PreParserExpression NewTargetExpression(int pos) { 1559 return PreParserExpression::NewTargetExpression(); 1560 } 1561 1562 V8_INLINE PreParserExpression ImportMetaExpression(int pos) { 1563 return PreParserExpression::Default(); 1564 } 1565 1566 V8_INLINE PreParserExpression ExpressionFromLiteral(Token::Value token, 1567 int pos) { 1568 if (token != Token::STRING) return PreParserExpression::Default(); 1569 return PreParserExpression::StringLiteral(); 1570 } 1571 1572 PreParserExpression ExpressionFromPrivateName( 1573 PrivateNameScopeIterator* private_name_scope, 1574 const PreParserIdentifier& name, int start_position) { 1575 VariableProxy* proxy = factory()->ast_node_factory()->NewVariableProxy( 1576 name.string_, NORMAL_VARIABLE, start_position); 1577 private_name_scope->AddUnresolvedPrivateName(proxy); 1578 return PreParserExpression::FromIdentifier(name); 1579 } 1580 1581 PreParserExpression ExpressionFromIdentifier( 1582 const PreParserIdentifier& name, int start_position, 1583 InferName infer = InferName::kYes) { 1584 expression_scope()->NewVariable(name.string_, start_position); 1585 return PreParserExpression::FromIdentifier(name); 1586 } 1587 1588 V8_INLINE void DeclareIdentifier(const PreParserIdentifier& name, 1589 int start_position) { 1590 expression_scope()->Declare(name.string_, start_position); 1591 } 1592 1593 V8_INLINE Variable* DeclareCatchVariableName( 1594 Scope* scope, const PreParserIdentifier& identifier) { 1595 return scope->DeclareCatchVariableName(identifier.string_); 1596 } 1597 1598 V8_INLINE PreParserPropertyList NewClassPropertyList(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 PreParserExpression PreParser::SpreadCall(const PreParserExpression& function, 1673 const PreParserExpressionList& args, 1674 int pos, 1675 Call::PossiblyEval possibly_eval, 1676 bool optional_chain) { 1677 return factory()->NewCall(function, args, pos, possibly_eval, optional_chain); 1678 } 1679 1680 PreParserExpression PreParser::SpreadCallNew( 1681 const PreParserExpression& function, const PreParserExpressionList& args, 1682 int pos) { 1683 return factory()->NewCallNew(function, args, pos); 1684 } 1685 1686 } // namespace internal 1687 } // namespace v8 1688 1689 #endif // V8_PARSING_PREPARSER_H_ 1690