Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(332)

Side by Side Diff: src/parsing/parser.h

Issue 2179423002: [parser] Refactor parser and preparser traits (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: More changes after reviewers' comments Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | src/parsing/parser.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef V8_PARSING_PARSER_H_ 5 #ifndef V8_PARSING_PARSER_H_
6 #define V8_PARSING_PARSER_H_ 6 #define V8_PARSING_PARSER_H_
7 7
8 #include "src/allocation.h" 8 #include "src/allocation.h"
9 #include "src/ast/ast.h" 9 #include "src/ast/ast.h"
10 #include "src/ast/scopes.h" 10 #include "src/ast/scopes.h"
(...skipping 476 matching lines...) Expand 10 before | Expand all | Expand 10 after
487 // type. The first argument may be null (in the handle sense) in 487 // type. The first argument may be null (in the handle sense) in
488 // which case no arguments are passed to the constructor. 488 // which case no arguments are passed to the constructor.
489 Expression* NewThrowSyntaxError(MessageTemplate::Template message, 489 Expression* NewThrowSyntaxError(MessageTemplate::Template message,
490 const AstRawString* arg, int pos); 490 const AstRawString* arg, int pos);
491 491
492 // Generate AST node that throws a TypeError with the given 492 // Generate AST node that throws a TypeError with the given
493 // type. Both arguments must be non-null (in the handle sense). 493 // type. Both arguments must be non-null (in the handle sense).
494 Expression* NewThrowTypeError(MessageTemplate::Template message, 494 Expression* NewThrowTypeError(MessageTemplate::Template message,
495 const AstRawString* arg, int pos); 495 const AstRawString* arg, int pos);
496 496
497 // Generic AST generator for throwing errors from compiled code.
498 Expression* NewThrowError(Runtime::FunctionId function_id,
499 MessageTemplate::Template message,
500 const AstRawString* arg, int pos);
501
502 void FinalizeIteratorUse(Variable* completion, Expression* condition,
503 Variable* iter, Block* iterator_use, Block* result);
504
505 Statement* FinalizeForOfStatement(ForOfStatement* loop, Variable* completion,
506 int pos);
507
508 // Reporting errors. 497 // Reporting errors.
509 void ReportMessageAt(Scanner::Location source_location, 498 void ReportMessageAt(Scanner::Location source_location,
510 MessageTemplate::Template message, 499 MessageTemplate::Template message,
511 const char* arg = NULL, 500 const char* arg = NULL,
512 ParseErrorType error_type = kSyntaxError); 501 ParseErrorType error_type = kSyntaxError);
513 void ReportMessage(MessageTemplate::Template message, const char* arg = NULL,
514 ParseErrorType error_type = kSyntaxError);
515 void ReportMessage(MessageTemplate::Template message, const AstRawString* arg,
516 ParseErrorType error_type = kSyntaxError);
517 void ReportMessageAt(Scanner::Location source_location, 502 void ReportMessageAt(Scanner::Location source_location,
518 MessageTemplate::Template message, 503 MessageTemplate::Template message,
519 const AstRawString* arg, 504 const AstRawString* arg,
520 ParseErrorType error_type = kSyntaxError); 505 ParseErrorType error_type = kSyntaxError);
521 506
522 // "null" return type creators. 507 // "null" return type creators.
523 static const AstRawString* EmptyIdentifier() { 508 static const AstRawString* EmptyIdentifier() { return nullptr; }
524 return NULL; 509 static Expression* EmptyExpression() { return nullptr; }
525 } 510 static Literal* EmptyLiteral() { return nullptr; }
526 static Expression* EmptyExpression() { 511 static ObjectLiteralProperty* EmptyObjectLiteralProperty() { return nullptr; }
527 return NULL; 512 static FunctionLiteral* EmptyFunctionLiteral() { return nullptr; }
528 }
529 static Literal* EmptyLiteral() {
530 return NULL;
531 }
532 static ObjectLiteralProperty* EmptyObjectLiteralProperty() { return NULL; }
533 static FunctionLiteral* EmptyFunctionLiteral() { return NULL; }
534 513
535 // Used in error return values. 514 // Used in error return values.
536 static ZoneList<Expression*>* NullExpressionList() { 515 static ZoneList<Expression*>* NullExpressionList() { return nullptr; }
537 return NULL;
538 }
539 static const AstRawString* EmptyFormalParameter() { return NULL; }
540 516
541 // Non-NULL empty string. 517 // Non-NULL empty string.
542 V8_INLINE const AstRawString* EmptyIdentifierString(); 518 V8_INLINE const AstRawString* EmptyIdentifierString();
543 519
544 // Odd-ball literal creators. 520 // Odd-ball literal creators.
545 Literal* GetLiteralTheHole(int position, AstNodeFactory* factory); 521 Literal* GetLiteralTheHole(int position, AstNodeFactory* factory);
546 522
547 // Producing data during the recursive descent. 523 // Producing data during the recursive descent.
548 const AstRawString* GetSymbol(Scanner* scanner); 524 const AstRawString* GetSymbol(Scanner* scanner);
549 const AstRawString* GetNextSymbol(Scanner* scanner); 525 const AstRawString* GetNextSymbol(Scanner* scanner);
(...skipping 24 matching lines...) Expand all
574 } 550 }
575 551
576 V8_INLINE void AddParameterInitializationBlock( 552 V8_INLINE void AddParameterInitializationBlock(
577 const ParserFormalParameters& parameters, 553 const ParserFormalParameters& parameters,
578 ZoneList<v8::internal::Statement*>* body, bool is_async, bool* ok); 554 ZoneList<v8::internal::Statement*>* body, bool is_async, bool* ok);
579 555
580 void ParseAsyncArrowSingleExpressionBody( 556 void ParseAsyncArrowSingleExpressionBody(
581 ZoneList<Statement*>* body, bool accept_IN, 557 ZoneList<Statement*>* body, bool accept_IN,
582 Type::ExpressionClassifier* classifier, int pos, bool* ok); 558 Type::ExpressionClassifier* classifier, int pos, bool* ok);
583 559
584 V8_INLINE Scope* NewScope(ScopeType scope_type);
585 V8_INLINE DeclarationScope* NewFunctionScope(FunctionKind kind);
586 V8_INLINE Scope* NewScopeWithParent(Scope* parent, ScopeType scope_type);
587
588 V8_INLINE void AddFormalParameter(ParserFormalParameters* parameters, 560 V8_INLINE void AddFormalParameter(ParserFormalParameters* parameters,
589 Expression* pattern, 561 Expression* pattern,
590 Expression* initializer, 562 Expression* initializer,
591 int initializer_end_position, bool is_rest); 563 int initializer_end_position, bool is_rest);
592 V8_INLINE void DeclareFormalParameter( 564 V8_INLINE void DeclareFormalParameter(
593 DeclarationScope* scope, 565 DeclarationScope* scope,
594 const ParserFormalParameters::Parameter& parameter, 566 const ParserFormalParameters::Parameter& parameter,
595 Type::ExpressionClassifier* classifier); 567 Type::ExpressionClassifier* classifier);
596 void ParseArrowFunctionFormalParameters(ParserFormalParameters* parameters,
597 Expression* params, int end_pos,
598 bool* ok);
599 void ParseArrowFunctionFormalParameterList( 568 void ParseArrowFunctionFormalParameterList(
600 ParserFormalParameters* parameters, Expression* params, 569 ParserFormalParameters* parameters, Expression* params,
601 const Scanner::Location& params_loc, Scanner::Location* duplicate_loc, 570 const Scanner::Location& params_loc, Scanner::Location* duplicate_loc,
602 const Scope::Snapshot& scope_snapshot, bool* ok); 571 const Scope::Snapshot& scope_snapshot, bool* ok);
603 572
604 V8_INLINE Expression* ParseAsyncFunctionExpression(bool* ok); 573 V8_INLINE Expression* ParseAsyncFunctionExpression(bool* ok);
605 574
606 V8_INLINE DoExpression* ParseDoExpression(bool* ok); 575 V8_INLINE DoExpression* ParseDoExpression(bool* ok);
607 576
608 void ReindexLiterals(const ParserFormalParameters& parameters); 577 void ReindexLiterals(const ParserFormalParameters& parameters);
(...skipping 15 matching lines...) Expand all
624 593
625 Expression* ParseClassLiteral(Type::ExpressionClassifier* classifier, 594 Expression* ParseClassLiteral(Type::ExpressionClassifier* classifier,
626 const AstRawString* name, 595 const AstRawString* name,
627 Scanner::Location class_name_location, 596 Scanner::Location class_name_location,
628 bool name_is_strict_reserved, int pos, 597 bool name_is_strict_reserved, int pos,
629 bool* ok); 598 bool* ok);
630 599
631 V8_INLINE void MarkCollectedTailCallExpressions(); 600 V8_INLINE void MarkCollectedTailCallExpressions();
632 V8_INLINE void MarkTailPosition(Expression* expression); 601 V8_INLINE void MarkTailPosition(Expression* expression);
633 602
634 V8_INLINE void CheckConflictingVarDeclarations(v8::internal::Scope* scope, 603 V8_INLINE void CheckConflictingVarDeclarations(Scope* scope, bool* ok);
635 bool* ok);
636 604
637 class TemplateLiteral : public ZoneObject { 605 class TemplateLiteral : public ZoneObject {
638 public: 606 public:
639 TemplateLiteral(Zone* zone, int pos) 607 TemplateLiteral(Zone* zone, int pos)
640 : cooked_(8, zone), raw_(8, zone), expressions_(8, zone), pos_(pos) {} 608 : cooked_(8, zone), raw_(8, zone), expressions_(8, zone), pos_(pos) {}
641 609
642 const ZoneList<Expression*>* cooked() const { return &cooked_; } 610 const ZoneList<Expression*>* cooked() const { return &cooked_; }
643 const ZoneList<Expression*>* raw() const { return &raw_; } 611 const ZoneList<Expression*>* raw() const { return &raw_; }
644 const ZoneList<Expression*>* expressions() const { return &expressions_; } 612 const ZoneList<Expression*>* expressions() const { return &expressions_; }
645 int position() const { return pos_; } 613 int position() const { return pos_; }
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
700 668
701 V8_INLINE void QueueDestructuringAssignmentForRewriting( 669 V8_INLINE void QueueDestructuringAssignmentForRewriting(
702 Expression* assignment); 670 Expression* assignment);
703 V8_INLINE void QueueNonPatternForRewriting(Expression* expr, bool* ok); 671 V8_INLINE void QueueNonPatternForRewriting(Expression* expr, bool* ok);
704 672
705 void SetFunctionNameFromPropertyName(ObjectLiteralProperty* property, 673 void SetFunctionNameFromPropertyName(ObjectLiteralProperty* property,
706 const AstRawString* name); 674 const AstRawString* name);
707 675
708 void SetFunctionNameFromIdentifierRef(Expression* value, 676 void SetFunctionNameFromIdentifierRef(Expression* value,
709 Expression* identifier); 677 Expression* identifier);
710 void SetFunctionName(Expression* value, const AstRawString* name);
711 678
712 // Rewrite expressions that are not used as patterns 679 // Rewrite expressions that are not used as patterns
713 V8_INLINE void RewriteNonPattern(Type::ExpressionClassifier* classifier, 680 V8_INLINE void RewriteNonPattern(Type::ExpressionClassifier* classifier,
714 bool* ok); 681 bool* ok);
715 682
716 V8_INLINE ZoneList<typename Type::ExpressionClassifier::Error>* 683 V8_INLINE ZoneList<typename Type::ExpressionClassifier::Error>*
717 GetReportedErrorList() const; 684 GetReportedErrorList() const;
718 V8_INLINE Zone* zone() const; 685 V8_INLINE Zone* zone() const;
719 686
720 V8_INLINE ZoneList<Expression*>* GetNonPatternList() const; 687 V8_INLINE ZoneList<Expression*>* GetNonPatternList() const;
721 688
722 Expression* RewriteYieldStar( 689 V8_INLINE Expression* RewriteYieldStar(Expression* generator,
723 Expression* generator, Expression* expression, int pos); 690 Expression* expression, int pos);
724 691
725 private: 692 private:
726 Parser* parser_; 693 Parser* parser_;
727
728 void BuildIteratorClose(ZoneList<Statement*>* statements, Variable* iterator,
729 Variable* input, Variable* output);
730 void BuildIteratorCloseForCompletion(ZoneList<Statement*>* statements,
731 Variable* iterator,
732 Expression* completion);
733 Statement* CheckCallable(Variable* var, Expression* error, int pos);
734 }; 694 };
735 695
736 696
737 class Parser : public ParserBase<ParserTraits> { 697 class Parser : public ParserBase<ParserTraits> {
738 public: 698 public:
739 explicit Parser(ParseInfo* info); 699 explicit Parser(ParseInfo* info);
740 ~Parser() { 700 ~Parser() {
741 delete reusable_preparser_; 701 delete reusable_preparser_;
742 reusable_preparser_ = NULL; 702 reusable_preparser_ = NULL;
743 delete cached_parse_data_; 703 delete cached_parse_data_;
(...skipping 424 matching lines...) Expand 10 before | Expand all | Expand 10 after
1168 1128
1169 V8_INLINE void RewriteNonPattern(ExpressionClassifier* classifier, bool* ok); 1129 V8_INLINE void RewriteNonPattern(ExpressionClassifier* classifier, bool* ok);
1170 1130
1171 friend class InitializerRewriter; 1131 friend class InitializerRewriter;
1172 void RewriteParameterInitializer(Expression* expr, Scope* scope); 1132 void RewriteParameterInitializer(Expression* expr, Scope* scope);
1173 1133
1174 Expression* BuildCreateJSGeneratorObject(int pos, FunctionKind kind); 1134 Expression* BuildCreateJSGeneratorObject(int pos, FunctionKind kind);
1175 Expression* BuildPromiseResolve(Expression* value, int pos); 1135 Expression* BuildPromiseResolve(Expression* value, int pos);
1176 Expression* BuildPromiseReject(Expression* value, int pos); 1136 Expression* BuildPromiseReject(Expression* value, int pos);
1177 1137
1138 // Generic AST generator for throwing errors from compiled code.
1139 Expression* NewThrowError(Runtime::FunctionId function_id,
1140 MessageTemplate::Template message,
1141 const AstRawString* arg, int pos);
1142
1143 void FinalizeIteratorUse(Variable* completion, Expression* condition,
1144 Variable* iter, Block* iterator_use, Block* result);
1145
1146 Statement* FinalizeForOfStatement(ForOfStatement* loop, Variable* completion,
1147 int pos);
1148 void BuildIteratorClose(ZoneList<Statement*>* statements, Variable* iterator,
1149 Variable* input, Variable* output);
1150 void BuildIteratorCloseForCompletion(ZoneList<Statement*>* statements,
1151 Variable* iterator,
1152 Expression* completion);
1153 Statement* CheckCallable(Variable* var, Expression* error, int pos);
1154
1155 Expression* RewriteYieldStar(Expression* generator, Expression* expression,
1156 int pos);
1157
1158 void ParseArrowFunctionFormalParameters(ParserFormalParameters* parameters,
1159 Expression* params, int end_pos,
1160 bool* ok);
1161 void SetFunctionName(Expression* value, const AstRawString* name);
1162
1178 Scanner scanner_; 1163 Scanner scanner_;
1179 PreParser* reusable_preparser_; 1164 PreParser* reusable_preparser_;
1180 Scope* original_scope_; // for ES5 function declarations in sloppy eval 1165 Scope* original_scope_; // for ES5 function declarations in sloppy eval
1181 Target* target_stack_; // for break, continue statements 1166 Target* target_stack_; // for break, continue statements
1182 ScriptCompiler::CompileOptions compile_options_; 1167 ScriptCompiler::CompileOptions compile_options_;
1183 ParseData* cached_parse_data_; 1168 ParseData* cached_parse_data_;
1184 1169
1185 PendingCompilationErrorHandler pending_error_handler_; 1170 PendingCompilationErrorHandler pending_error_handler_;
1186 1171
1187 // Other information which will be stored in Parser and moved to Isolate after 1172 // Other information which will be stored in Parser and moved to Isolate after
1188 // parsing. 1173 // parsing.
1189 int use_counts_[v8::Isolate::kUseCounterFeatureCount]; 1174 int use_counts_[v8::Isolate::kUseCounterFeatureCount];
1190 int total_preparse_skipped_; 1175 int total_preparse_skipped_;
1191 HistogramTimer* pre_parse_timer_; 1176 HistogramTimer* pre_parse_timer_;
1192 1177
1193 bool parsing_on_main_thread_; 1178 bool parsing_on_main_thread_;
1194 1179
1195 #ifdef DEBUG 1180 #ifdef DEBUG
1196 void Print(AstNode* node); 1181 void Print(AstNode* node);
1197 #endif // DEBUG 1182 #endif // DEBUG
1198 }; 1183 };
1199 1184
1200 1185
1201 bool ParserTraits::IsFutureStrictReserved( 1186 bool ParserTraits::IsFutureStrictReserved(
1202 const AstRawString* identifier) const { 1187 const AstRawString* identifier) const {
1203 return parser_->scanner()->IdentifierIsFutureStrictReserved(identifier); 1188 return parser_->scanner()->IdentifierIsFutureStrictReserved(identifier);
1204 } 1189 }
1205 1190
1206 Scope* ParserTraits::NewScopeWithParent(Scope* parent, ScopeType scope_type) {
1207 return parser_->NewScopeWithParent(parent, scope_type);
1208 }
1209
1210 Scope* ParserTraits::NewScope(ScopeType scope_type) {
1211 return parser_->NewScope(scope_type);
1212 }
1213
1214 DeclarationScope* ParserTraits::NewFunctionScope(FunctionKind kind) {
1215 return parser_->NewFunctionScope(kind);
1216 }
1217
1218 const AstRawString* ParserTraits::EmptyIdentifierString() { 1191 const AstRawString* ParserTraits::EmptyIdentifierString() {
1219 return parser_->ast_value_factory()->empty_string(); 1192 return parser_->ast_value_factory()->empty_string();
1220 } 1193 }
1221 1194
1222 1195
1223 void ParserTraits::SkipLazyFunctionBody(int* materialized_literal_count, 1196 void ParserTraits::SkipLazyFunctionBody(int* materialized_literal_count,
1224 int* expected_property_count, bool* ok, 1197 int* expected_property_count, bool* ok,
1225 Scanner::BookmarkScope* bookmark) { 1198 Scanner::BookmarkScope* bookmark) {
1226 return parser_->SkipLazyFunctionBody(materialized_literal_count, 1199 return parser_->SkipLazyFunctionBody(materialized_literal_count,
1227 expected_property_count, ok, bookmark); 1200 expected_property_count, ok, bookmark);
1228 } 1201 }
1229 1202
1230 1203
1231 ZoneList<Statement*>* ParserTraits::ParseEagerFunctionBody( 1204 ZoneList<Statement*>* ParserTraits::ParseEagerFunctionBody(
1232 const AstRawString* name, int pos, const ParserFormalParameters& parameters, 1205 const AstRawString* name, int pos, const ParserFormalParameters& parameters,
1233 FunctionKind kind, FunctionLiteral::FunctionType function_type, bool* ok) { 1206 FunctionKind kind, FunctionLiteral::FunctionType function_type, bool* ok) {
1234 return parser_->ParseEagerFunctionBody(name, pos, parameters, kind, 1207 return parser_->ParseEagerFunctionBody(name, pos, parameters, kind,
1235 function_type, ok); 1208 function_type, ok);
1236 } 1209 }
1237 1210
1238 1211 void ParserTraits::CheckConflictingVarDeclarations(Scope* scope, bool* ok) {
1239 void ParserTraits::CheckConflictingVarDeclarations(v8::internal::Scope* scope,
1240 bool* ok) {
1241 parser_->CheckConflictingVarDeclarations(scope, ok); 1212 parser_->CheckConflictingVarDeclarations(scope, ok);
1242 } 1213 }
1243 1214
1244 1215
1245 // Support for handling complex values (array and object literals) that 1216 // Support for handling complex values (array and object literals) that
1246 // can be fully handled at compile time. 1217 // can be fully handled at compile time.
1247 class CompileTimeValue: public AllStatic { 1218 class CompileTimeValue: public AllStatic {
1248 public: 1219 public:
1249 enum LiteralType { 1220 enum LiteralType {
1250 OBJECT_LITERAL_FAST_ELEMENTS, 1221 OBJECT_LITERAL_FAST_ELEMENTS,
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
1372 } 1343 }
1373 1344
1374 Expression* ParserTraits::ParseAsyncFunctionExpression(bool* ok) { 1345 Expression* ParserTraits::ParseAsyncFunctionExpression(bool* ok) {
1375 return parser_->ParseAsyncFunctionExpression(ok); 1346 return parser_->ParseAsyncFunctionExpression(ok);
1376 } 1347 }
1377 1348
1378 DoExpression* ParserTraits::ParseDoExpression(bool* ok) { 1349 DoExpression* ParserTraits::ParseDoExpression(bool* ok) {
1379 return parser_->ParseDoExpression(ok); 1350 return parser_->ParseDoExpression(ok);
1380 } 1351 }
1381 1352
1353 Expression* ParserTraits::RewriteYieldStar(Expression* generator,
1354 Expression* iterable, int pos) {
1355 return parser_->RewriteYieldStar(generator, iterable, pos);
1356 }
1382 1357
1383 } // namespace internal 1358 } // namespace internal
1384 } // namespace v8 1359 } // namespace v8
1385 1360
1386 #endif // V8_PARSING_PARSER_H_ 1361 #endif // V8_PARSING_PARSER_H_
OLDNEW
« no previous file with comments | « no previous file | src/parsing/parser.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698