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

Side by Side Diff: src/parser.h

Issue 1195163007: Revert of [destructuring] Implement parameter pattern matching. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 5 years, 6 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/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_PARSER_H_ 5 #ifndef V8_PARSER_H_
6 #define V8_PARSER_H_ 6 #define V8_PARSER_H_
7 7
8 #include "src/allocation.h" 8 #include "src/allocation.h"
9 #include "src/ast.h" 9 #include "src/ast.h"
10 #include "src/compiler.h" // TODO(titzer): remove this include dependency 10 #include "src/compiler.h" // TODO(titzer): remove this include dependency
(...skipping 520 matching lines...) Expand 10 before | Expand all | Expand 10 after
531 bool is_scanned_for_captures_; 531 bool is_scanned_for_captures_;
532 bool failed_; 532 bool failed_;
533 }; 533 };
534 534
535 // ---------------------------------------------------------------------------- 535 // ----------------------------------------------------------------------------
536 // JAVASCRIPT PARSING 536 // JAVASCRIPT PARSING
537 537
538 class Parser; 538 class Parser;
539 class SingletonLogger; 539 class SingletonLogger;
540 540
541
542 struct ParserFormalParameterParsingState
543 : public PreParserFormalParameterParsingState {
544 struct Parameter {
545 Parameter(Variable* var, Expression* pattern)
546 : var(var), pattern(pattern) {}
547 Variable* var;
548 Expression* pattern;
549 };
550
551 explicit ParserFormalParameterParsingState(Scope* scope)
552 : PreParserFormalParameterParsingState(scope), params(4, scope->zone()) {}
553
554 ZoneList<Parameter> params;
555
556 void AddParameter(Variable* var, Expression* pattern) {
557 params.Add(Parameter(var, pattern), scope->zone());
558 }
559 };
560
561
562 class ParserTraits { 541 class ParserTraits {
563 public: 542 public:
564 struct Type { 543 struct Type {
565 // TODO(marja): To be removed. The Traits object should contain all the data 544 // TODO(marja): To be removed. The Traits object should contain all the data
566 // it needs. 545 // it needs.
567 typedef v8::internal::Parser* Parser; 546 typedef v8::internal::Parser* Parser;
568 547
569 typedef Variable GeneratorVariable; 548 typedef Variable GeneratorVariable;
570 549
571 typedef v8::internal::AstProperties AstProperties; 550 typedef v8::internal::AstProperties AstProperties;
572 551
573 // Return types for traversing functions. 552 // Return types for traversing functions.
574 typedef const AstRawString* Identifier; 553 typedef const AstRawString* Identifier;
575 typedef v8::internal::Expression* Expression; 554 typedef v8::internal::Expression* Expression;
576 typedef Yield* YieldExpression; 555 typedef Yield* YieldExpression;
577 typedef v8::internal::FunctionLiteral* FunctionLiteral; 556 typedef v8::internal::FunctionLiteral* FunctionLiteral;
578 typedef v8::internal::ClassLiteral* ClassLiteral; 557 typedef v8::internal::ClassLiteral* ClassLiteral;
579 typedef v8::internal::Literal* Literal; 558 typedef v8::internal::Literal* Literal;
580 typedef ObjectLiteral::Property* ObjectLiteralProperty; 559 typedef ObjectLiteral::Property* ObjectLiteralProperty;
581 typedef ZoneList<v8::internal::Expression*>* ExpressionList; 560 typedef ZoneList<v8::internal::Expression*>* ExpressionList;
582 typedef ZoneList<ObjectLiteral::Property*>* PropertyList; 561 typedef ZoneList<ObjectLiteral::Property*>* PropertyList;
583 typedef const v8::internal::AstRawString* FormalParameter; 562 typedef const v8::internal::AstRawString* FormalParameter;
584 typedef ParserFormalParameterParsingState FormalParameterParsingState; 563 typedef Scope FormalParameterScope;
585 typedef ZoneList<v8::internal::Statement*>* StatementList; 564 typedef ZoneList<v8::internal::Statement*>* StatementList;
586 565
587 // For constructing objects returned by the traversing functions. 566 // For constructing objects returned by the traversing functions.
588 typedef AstNodeFactory Factory; 567 typedef AstNodeFactory Factory;
589 }; 568 };
590 569
591 explicit ParserTraits(Parser* parser) : parser_(parser) {} 570 explicit ParserTraits(Parser* parser) : parser_(parser) {}
592 571
593 // Helper functions for recursive descent. 572 // Helper functions for recursive descent.
594 bool IsEval(const AstRawString* identifier) const; 573 bool IsEval(const AstRawString* identifier) const;
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
765 Expression* GetIterator(Expression* iterable, AstNodeFactory* factory); 744 Expression* GetIterator(Expression* iterable, AstNodeFactory* factory);
766 ZoneList<v8::internal::Expression*>* NewExpressionList(int size, Zone* zone) { 745 ZoneList<v8::internal::Expression*>* NewExpressionList(int size, Zone* zone) {
767 return new(zone) ZoneList<v8::internal::Expression*>(size, zone); 746 return new(zone) ZoneList<v8::internal::Expression*>(size, zone);
768 } 747 }
769 ZoneList<ObjectLiteral::Property*>* NewPropertyList(int size, Zone* zone) { 748 ZoneList<ObjectLiteral::Property*>* NewPropertyList(int size, Zone* zone) {
770 return new(zone) ZoneList<ObjectLiteral::Property*>(size, zone); 749 return new(zone) ZoneList<ObjectLiteral::Property*>(size, zone);
771 } 750 }
772 ZoneList<v8::internal::Statement*>* NewStatementList(int size, Zone* zone) { 751 ZoneList<v8::internal::Statement*>* NewStatementList(int size, Zone* zone) {
773 return new(zone) ZoneList<v8::internal::Statement*>(size, zone); 752 return new(zone) ZoneList<v8::internal::Statement*>(size, zone);
774 } 753 }
775
776 V8_INLINE void AddParameterInitializationBlock(
777 const ParserFormalParameterParsingState& formal_parameters,
778 ZoneList<v8::internal::Statement*>* body, bool* ok);
779
780 V8_INLINE Scope* NewScope(Scope* parent_scope, ScopeType scope_type, 754 V8_INLINE Scope* NewScope(Scope* parent_scope, ScopeType scope_type,
781 FunctionKind kind = kNormalFunction); 755 FunctionKind kind = kNormalFunction);
782 756
783 V8_INLINE void DeclareFormalParameter( 757 V8_INLINE void DeclareFormalParameter(Scope* scope, Expression* name,
784 ParserFormalParameterParsingState* parsing_state, Expression* name, 758 ExpressionClassifier* classifier,
785 ExpressionClassifier* classifier, bool is_rest); 759 bool is_rest);
786 void ParseArrowFunctionFormalParameters( 760 void ParseArrowFunctionFormalParameters(Scope* scope, Expression* params,
787 ParserFormalParameterParsingState* scope, Expression* params, 761 const Scanner::Location& params_loc,
788 const Scanner::Location& params_loc, Scanner::Location* duplicate_loc, 762 bool* has_rest,
789 bool* ok); 763 Scanner::Location* duplicate_loc,
764 bool* ok);
790 765
791 // Temporary glue; these functions will move to ParserBase. 766 // Temporary glue; these functions will move to ParserBase.
792 Expression* ParseV8Intrinsic(bool* ok); 767 Expression* ParseV8Intrinsic(bool* ok);
793 FunctionLiteral* ParseFunctionLiteral( 768 FunctionLiteral* ParseFunctionLiteral(
794 const AstRawString* name, Scanner::Location function_name_location, 769 const AstRawString* name, Scanner::Location function_name_location,
795 bool name_is_strict_reserved, FunctionKind kind, 770 bool name_is_strict_reserved, FunctionKind kind,
796 int function_token_position, FunctionLiteral::FunctionType type, 771 int function_token_position, FunctionLiteral::FunctionType type,
797 FunctionLiteral::ArityRestriction arity_restriction, bool* ok); 772 FunctionLiteral::ArityRestriction arity_restriction, bool* ok);
798 V8_INLINE void SkipLazyFunctionBody( 773 V8_INLINE void SkipLazyFunctionBody(
799 int* materialized_literal_count, int* expected_property_count, bool* ok, 774 int* materialized_literal_count, int* expected_property_count, bool* ok,
800 Scanner::BookmarkScope* bookmark = nullptr); 775 Scanner::BookmarkScope* bookmark = nullptr);
801 V8_INLINE ZoneList<Statement*>* ParseEagerFunctionBody( 776 V8_INLINE ZoneList<Statement*>* ParseEagerFunctionBody(
802 const AstRawString* name, int pos, 777 const AstRawString* name, int pos, Variable* fvar,
803 const ParserFormalParameterParsingState& formal_parameters, 778 Token::Value fvar_init_op, FunctionKind kind, bool* ok);
804 Variable* fvar, Token::Value fvar_init_op, FunctionKind kind, bool* ok);
805 779
806 ClassLiteral* ParseClassLiteral(const AstRawString* name, 780 ClassLiteral* ParseClassLiteral(const AstRawString* name,
807 Scanner::Location class_name_location, 781 Scanner::Location class_name_location,
808 bool name_is_strict_reserved, int pos, 782 bool name_is_strict_reserved, int pos,
809 bool* ok); 783 bool* ok);
810 784
811 V8_INLINE void CheckConflictingVarDeclarations(v8::internal::Scope* scope, 785 V8_INLINE void CheckConflictingVarDeclarations(v8::internal::Scope* scope,
812 bool* ok); 786 bool* ok);
813 787
814 class TemplateLiteral : public ZoneObject { 788 class TemplateLiteral : public ZoneObject {
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
955 bool* ok); 929 bool* ok);
956 Statement* ParseClassDeclaration(ZoneList<const AstRawString*>* names, 930 Statement* ParseClassDeclaration(ZoneList<const AstRawString*>* names,
957 bool* ok); 931 bool* ok);
958 Statement* ParseNativeDeclaration(bool* ok); 932 Statement* ParseNativeDeclaration(bool* ok);
959 Block* ParseBlock(ZoneList<const AstRawString*>* labels, bool* ok); 933 Block* ParseBlock(ZoneList<const AstRawString*>* labels, bool* ok);
960 Block* ParseVariableStatement(VariableDeclarationContext var_context, 934 Block* ParseVariableStatement(VariableDeclarationContext var_context,
961 ZoneList<const AstRawString*>* names, 935 ZoneList<const AstRawString*>* names,
962 bool* ok); 936 bool* ok);
963 937
964 struct DeclarationDescriptor { 938 struct DeclarationDescriptor {
965 enum Kind { NORMAL, PARAMETER };
966 Parser* parser; 939 Parser* parser;
967 Scope* declaration_scope; 940 Scope* declaration_scope;
968 Scope* scope; 941 Scope* scope;
969 VariableMode mode; 942 VariableMode mode;
970 bool is_const; 943 bool is_const;
971 bool needs_init; 944 bool needs_init;
972 int declaration_pos; 945 int declaration_pos;
973 int initialization_pos; 946 int initialization_pos;
974 Token::Value init_op; 947 Token::Value init_op;
975 Kind declaration_kind;
976 }; 948 };
977 949
978 struct DeclarationParsingResult { 950 struct DeclarationParsingResult {
979 struct Declaration { 951 struct Declaration {
980 Declaration(Expression* pattern, int initializer_position, 952 Declaration(Expression* pattern, int initializer_position,
981 Expression* initializer) 953 Expression* initializer)
982 : pattern(pattern), 954 : pattern(pattern),
983 initializer_position(initializer_position), 955 initializer_position(initializer_position),
984 initializer(initializer) {} 956 initializer(initializer) {}
985 957
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
1116 // function f() { { { var x; } let x; } } 1088 // function f() { { { var x; } let x; } }
1117 // function g() { { var x; let x; } } 1089 // function g() { { var x; let x; } }
1118 // 1090 //
1119 // The var declarations are hoisted to the function scope, but originate from 1091 // The var declarations are hoisted to the function scope, but originate from
1120 // a scope where the name has also been let bound or the var declaration is 1092 // a scope where the name has also been let bound or the var declaration is
1121 // hoisted over such a scope. 1093 // hoisted over such a scope.
1122 void CheckConflictingVarDeclarations(Scope* scope, bool* ok); 1094 void CheckConflictingVarDeclarations(Scope* scope, bool* ok);
1123 1095
1124 // Parser support 1096 // Parser support
1125 VariableProxy* NewUnresolved(const AstRawString* name, VariableMode mode); 1097 VariableProxy* NewUnresolved(const AstRawString* name, VariableMode mode);
1126 Variable* Declare(Declaration* declaration, 1098 Variable* Declare(Declaration* declaration, bool resolve, bool* ok);
1127 DeclarationDescriptor::Kind declaration_kind, bool resolve,
1128 bool* ok);
1129 1099
1130 bool TargetStackContainsLabel(const AstRawString* label); 1100 bool TargetStackContainsLabel(const AstRawString* label);
1131 BreakableStatement* LookupBreakTarget(const AstRawString* label, bool* ok); 1101 BreakableStatement* LookupBreakTarget(const AstRawString* label, bool* ok);
1132 IterationStatement* LookupContinueTarget(const AstRawString* label, bool* ok); 1102 IterationStatement* LookupContinueTarget(const AstRawString* label, bool* ok);
1133 1103
1134 void AddAssertIsConstruct(ZoneList<Statement*>* body, int pos); 1104 void AddAssertIsConstruct(ZoneList<Statement*>* body, int pos);
1135 Statement* BuildAssertIsCoercible(Variable* var); 1105 Statement* BuildAssertIsCoercible(Variable* var);
1136 1106
1137 // Factory methods. 1107 // Factory methods.
1138 FunctionLiteral* DefaultConstructor(bool call_super, Scope* scope, int pos, 1108 FunctionLiteral* DefaultConstructor(bool call_super, Scope* scope, int pos,
1139 int end_pos); 1109 int end_pos);
1140 1110
1141 // Skip over a lazy function, either using cached data if we have it, or 1111 // Skip over a lazy function, either using cached data if we have it, or
1142 // by parsing the function with PreParser. Consumes the ending }. 1112 // by parsing the function with PreParser. Consumes the ending }.
1143 // 1113 //
1144 // If bookmark is set, the (pre-)parser may decide to abort skipping 1114 // If bookmark is set, the (pre-)parser may decide to abort skipping
1145 // in order to force the function to be eagerly parsed, after all. 1115 // in order to force the function to be eagerly parsed, after all.
1146 // In this case, it'll reset the scanner using the bookmark. 1116 // In this case, it'll reset the scanner using the bookmark.
1147 void SkipLazyFunctionBody(int* materialized_literal_count, 1117 void SkipLazyFunctionBody(int* materialized_literal_count,
1148 int* expected_property_count, bool* ok, 1118 int* expected_property_count, bool* ok,
1149 Scanner::BookmarkScope* bookmark = nullptr); 1119 Scanner::BookmarkScope* bookmark = nullptr);
1150 1120
1151 PreParser::PreParseResult ParseLazyFunctionBodyWithPreParser( 1121 PreParser::PreParseResult ParseLazyFunctionBodyWithPreParser(
1152 SingletonLogger* logger, Scanner::BookmarkScope* bookmark = nullptr); 1122 SingletonLogger* logger, Scanner::BookmarkScope* bookmark = nullptr);
1153 1123
1154 Block* BuildParameterInitializationBlock(
1155 const ParserFormalParameterParsingState& formal_parameters, bool* ok);
1156
1157 // Consumes the ending }. 1124 // Consumes the ending }.
1158 ZoneList<Statement*>* ParseEagerFunctionBody( 1125 ZoneList<Statement*>* ParseEagerFunctionBody(
1159 const AstRawString* function_name, int pos, 1126 const AstRawString* function_name, int pos, Variable* fvar,
1160 const ParserFormalParameterParsingState& formal_parameters, 1127 Token::Value fvar_init_op, FunctionKind kind, bool* ok);
1161 Variable* fvar, Token::Value fvar_init_op, FunctionKind kind, bool* ok);
1162 1128
1163 void ThrowPendingError(Isolate* isolate, Handle<Script> script); 1129 void ThrowPendingError(Isolate* isolate, Handle<Script> script);
1164 1130
1165 TemplateLiteralState OpenTemplateLiteral(int pos); 1131 TemplateLiteralState OpenTemplateLiteral(int pos);
1166 void AddTemplateSpan(TemplateLiteralState* state, bool tail); 1132 void AddTemplateSpan(TemplateLiteralState* state, bool tail);
1167 void AddTemplateExpression(TemplateLiteralState* state, 1133 void AddTemplateExpression(TemplateLiteralState* state,
1168 Expression* expression); 1134 Expression* expression);
1169 Expression* CloseTemplateLiteral(TemplateLiteralState* state, int start, 1135 Expression* CloseTemplateLiteral(TemplateLiteralState* state, int start,
1170 Expression* tag); 1136 Expression* tag);
1171 uint32_t ComputeTemplateLiteralHash(const TemplateLiteral* lit); 1137 uint32_t ComputeTemplateLiteralHash(const TemplateLiteral* lit);
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
1215 1181
1216 void ParserTraits::SkipLazyFunctionBody(int* materialized_literal_count, 1182 void ParserTraits::SkipLazyFunctionBody(int* materialized_literal_count,
1217 int* expected_property_count, bool* ok, 1183 int* expected_property_count, bool* ok,
1218 Scanner::BookmarkScope* bookmark) { 1184 Scanner::BookmarkScope* bookmark) {
1219 return parser_->SkipLazyFunctionBody(materialized_literal_count, 1185 return parser_->SkipLazyFunctionBody(materialized_literal_count,
1220 expected_property_count, ok, bookmark); 1186 expected_property_count, ok, bookmark);
1221 } 1187 }
1222 1188
1223 1189
1224 ZoneList<Statement*>* ParserTraits::ParseEagerFunctionBody( 1190 ZoneList<Statement*>* ParserTraits::ParseEagerFunctionBody(
1225 const AstRawString* name, int pos, 1191 const AstRawString* name, int pos, Variable* fvar,
1226 const ParserFormalParameterParsingState& formal_parameters, Variable* fvar,
1227 Token::Value fvar_init_op, FunctionKind kind, bool* ok) { 1192 Token::Value fvar_init_op, FunctionKind kind, bool* ok) {
1228 return parser_->ParseEagerFunctionBody(name, pos, formal_parameters, fvar, 1193 return parser_->ParseEagerFunctionBody(name, pos, fvar, fvar_init_op, kind,
1229 fvar_init_op, kind, ok); 1194 ok);
1230 } 1195 }
1231 1196
1232 void ParserTraits::CheckConflictingVarDeclarations(v8::internal::Scope* scope, 1197 void ParserTraits::CheckConflictingVarDeclarations(v8::internal::Scope* scope,
1233 bool* ok) { 1198 bool* ok) {
1234 parser_->CheckConflictingVarDeclarations(scope, ok); 1199 parser_->CheckConflictingVarDeclarations(scope, ok);
1235 } 1200 }
1236 1201
1237 1202
1238 // Support for handling complex values (array and object literals) that 1203 // Support for handling complex values (array and object literals) that
1239 // can be fully handled at compile time. 1204 // can be fully handled at compile time.
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
1298 return parser_->SpreadCall(function, args, pos); 1263 return parser_->SpreadCall(function, args, pos);
1299 } 1264 }
1300 1265
1301 1266
1302 Expression* ParserTraits::SpreadCallNew( 1267 Expression* ParserTraits::SpreadCallNew(
1303 Expression* function, ZoneList<v8::internal::Expression*>* args, int pos) { 1268 Expression* function, ZoneList<v8::internal::Expression*>* args, int pos) {
1304 return parser_->SpreadCallNew(function, args, pos); 1269 return parser_->SpreadCallNew(function, args, pos);
1305 } 1270 }
1306 1271
1307 1272
1308 void ParserTraits::DeclareFormalParameter( 1273 void ParserTraits::DeclareFormalParameter(Scope* scope, Expression* pattern,
1309 ParserFormalParameterParsingState* parsing_state, Expression* pattern, 1274 ExpressionClassifier* classifier,
1310 ExpressionClassifier* classifier, bool is_rest) { 1275 bool is_rest) {
1311 bool is_duplicate = false; 1276 bool is_duplicate = false;
1312 bool is_simple_name = pattern->IsVariableProxy(); 1277 if (!pattern->IsVariableProxy()) {
1313 DCHECK(parser_->allow_harmony_destructuring() || is_simple_name); 1278 // TODO(dslomov): implement.
1314 1279 DCHECK(parser_->allow_harmony_destructuring());
1315 const AstRawString* name = is_simple_name 1280 return;
1316 ? pattern->AsVariableProxy()->raw_name() 1281 }
1317 : parser_->ast_value_factory()->empty_string(); 1282 auto name = pattern->AsVariableProxy()->raw_name();
1318 Variable* var = 1283 Variable* var = scope->DeclareParameter(name, VAR, is_rest, &is_duplicate);
1319 parsing_state->scope->DeclareParameter(name, VAR, is_rest, &is_duplicate); 1284 if (is_sloppy(scope->language_mode())) {
1320 parsing_state->AddParameter(var, is_simple_name ? nullptr : pattern);
1321 if (is_sloppy(parsing_state->scope->language_mode())) {
1322 // TODO(sigurds) Mark every parameter as maybe assigned. This is a 1285 // TODO(sigurds) Mark every parameter as maybe assigned. This is a
1323 // conservative approximation necessary to account for parameters 1286 // conservative approximation necessary to account for parameters
1324 // that are assigned via the arguments array. 1287 // that are assigned via the arguments array.
1325 var->set_maybe_assigned(); 1288 var->set_maybe_assigned();
1326 } 1289 }
1327 if (is_duplicate) { 1290 if (is_duplicate) {
1328 classifier->RecordDuplicateFormalParameterError( 1291 classifier->RecordDuplicateFormalParameterError(
1329 parser_->scanner()->location()); 1292 parser_->scanner()->location());
1330 } 1293 }
1331 } 1294 }
1332
1333
1334 void ParserTraits::AddParameterInitializationBlock(
1335 const ParserFormalParameterParsingState& formal_parameters,
1336 ZoneList<v8::internal::Statement*>* body, bool* ok) {
1337 auto* init_block =
1338 parser_->BuildParameterInitializationBlock(formal_parameters, ok);
1339 if (!*ok) return;
1340 if (init_block != nullptr) {
1341 body->Add(init_block, parser_->zone());
1342 }
1343 }
1344 } } // namespace v8::internal 1295 } } // namespace v8::internal
1345 1296
1346 #endif // V8_PARSER_H_ 1297 #endif // V8_PARSER_H_
OLDNEW
« no previous file with comments | « no previous file | src/parser.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698