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

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

Powered by Google App Engine
This is Rietveld 408576698