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

Side by Side Diff: src/parser.h

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