OLD | NEW |
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 521 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 | 541 |
542 struct ParserFormalParameterParsingState | 542 struct ParserFormalParameters : public PreParserFormalParameters { |
543 : public PreParserFormalParameterParsingState { | |
544 struct Parameter { | 543 struct Parameter { |
545 Parameter(Variable* var, Expression* pattern) | 544 Parameter(Variable* var, Expression* pattern) |
546 : var(var), pattern(pattern) {} | 545 : var(var), pattern(pattern) {} |
547 Variable* var; | 546 Variable* var; |
548 Expression* pattern; | 547 Expression* pattern; |
549 }; | 548 }; |
550 | 549 |
551 explicit ParserFormalParameterParsingState(Scope* scope) | 550 explicit ParserFormalParameters(Scope* scope) |
552 : PreParserFormalParameterParsingState(scope), params(4, scope->zone()) {} | 551 : PreParserFormalParameters(scope), params(4, scope->zone()) {} |
553 | 552 |
554 ZoneList<Parameter> params; | 553 ZoneList<Parameter> params; |
555 | 554 |
556 void AddParameter(Variable* var, Expression* pattern) { | 555 void AddParameter(Variable* var, Expression* pattern) { |
557 params.Add(Parameter(var, pattern), scope->zone()); | 556 params.Add(Parameter(var, pattern), scope->zone()); |
558 } | 557 } |
559 }; | 558 }; |
560 | 559 |
561 | 560 |
562 class ParserTraits { | 561 class ParserTraits { |
(...skipping 11 matching lines...) Expand all Loading... |
574 typedef const AstRawString* Identifier; | 573 typedef const AstRawString* Identifier; |
575 typedef v8::internal::Expression* Expression; | 574 typedef v8::internal::Expression* Expression; |
576 typedef Yield* YieldExpression; | 575 typedef Yield* YieldExpression; |
577 typedef v8::internal::FunctionLiteral* FunctionLiteral; | 576 typedef v8::internal::FunctionLiteral* FunctionLiteral; |
578 typedef v8::internal::ClassLiteral* ClassLiteral; | 577 typedef v8::internal::ClassLiteral* ClassLiteral; |
579 typedef v8::internal::Literal* Literal; | 578 typedef v8::internal::Literal* Literal; |
580 typedef ObjectLiteral::Property* ObjectLiteralProperty; | 579 typedef ObjectLiteral::Property* ObjectLiteralProperty; |
581 typedef ZoneList<v8::internal::Expression*>* ExpressionList; | 580 typedef ZoneList<v8::internal::Expression*>* ExpressionList; |
582 typedef ZoneList<ObjectLiteral::Property*>* PropertyList; | 581 typedef ZoneList<ObjectLiteral::Property*>* PropertyList; |
583 typedef const v8::internal::AstRawString* FormalParameter; | 582 typedef const v8::internal::AstRawString* FormalParameter; |
584 typedef ParserFormalParameterParsingState FormalParameterParsingState; | 583 typedef ParserFormalParameters FormalParameters; |
585 typedef ZoneList<v8::internal::Statement*>* StatementList; | 584 typedef ZoneList<v8::internal::Statement*>* StatementList; |
586 | 585 |
587 // For constructing objects returned by the traversing functions. | 586 // For constructing objects returned by the traversing functions. |
588 typedef AstNodeFactory Factory; | 587 typedef AstNodeFactory Factory; |
589 }; | 588 }; |
590 | 589 |
591 explicit ParserTraits(Parser* parser) : parser_(parser) {} | 590 explicit ParserTraits(Parser* parser) : parser_(parser) {} |
592 | 591 |
593 // Helper functions for recursive descent. | 592 // Helper functions for recursive descent. |
594 bool IsEval(const AstRawString* identifier) const; | 593 bool IsEval(const AstRawString* identifier) const; |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
767 return new(zone) ZoneList<v8::internal::Expression*>(size, zone); | 766 return new(zone) ZoneList<v8::internal::Expression*>(size, zone); |
768 } | 767 } |
769 ZoneList<ObjectLiteral::Property*>* NewPropertyList(int size, Zone* zone) { | 768 ZoneList<ObjectLiteral::Property*>* NewPropertyList(int size, Zone* zone) { |
770 return new(zone) ZoneList<ObjectLiteral::Property*>(size, zone); | 769 return new(zone) ZoneList<ObjectLiteral::Property*>(size, zone); |
771 } | 770 } |
772 ZoneList<v8::internal::Statement*>* NewStatementList(int size, Zone* zone) { | 771 ZoneList<v8::internal::Statement*>* NewStatementList(int size, Zone* zone) { |
773 return new(zone) ZoneList<v8::internal::Statement*>(size, zone); | 772 return new(zone) ZoneList<v8::internal::Statement*>(size, zone); |
774 } | 773 } |
775 | 774 |
776 V8_INLINE void AddParameterInitializationBlock( | 775 V8_INLINE void AddParameterInitializationBlock( |
777 const ParserFormalParameterParsingState& formal_parameters, | 776 const ParserFormalParameters& parameters, |
778 ZoneList<v8::internal::Statement*>* body, bool* ok); | 777 ZoneList<v8::internal::Statement*>* body, bool* ok); |
779 | 778 |
780 V8_INLINE Scope* NewScope(Scope* parent_scope, ScopeType scope_type, | 779 V8_INLINE Scope* NewScope(Scope* parent_scope, ScopeType scope_type, |
781 FunctionKind kind = kNormalFunction); | 780 FunctionKind kind = kNormalFunction); |
782 | 781 |
783 V8_INLINE void DeclareFormalParameter( | 782 V8_INLINE void DeclareFormalParameter( |
784 ParserFormalParameterParsingState* parsing_state, Expression* name, | 783 ParserFormalParameters* parameters, Expression* pattern, bool is_rest, |
785 ExpressionClassifier* classifier, bool is_rest); | 784 ExpressionClassifier* classifier); |
786 void ParseArrowFunctionFormalParameters( | 785 void ParseArrowFunctionFormalParameters( |
787 ParserFormalParameterParsingState* scope, Expression* params, | 786 ParserFormalParameters* parameters, Expression* params, |
788 const Scanner::Location& params_loc, Scanner::Location* duplicate_loc, | 787 const Scanner::Location& params_loc, |
789 bool* ok); | 788 Scanner::Location* duplicate_loc, bool* ok); |
790 | 789 |
791 void ReindexLiterals(const ParserFormalParameterParsingState& parsing_state); | 790 void ReindexLiterals(const ParserFormalParameters& parameters); |
792 | 791 |
793 // Temporary glue; these functions will move to ParserBase. | 792 // Temporary glue; these functions will move to ParserBase. |
794 Expression* ParseV8Intrinsic(bool* ok); | 793 Expression* ParseV8Intrinsic(bool* ok); |
795 FunctionLiteral* ParseFunctionLiteral( | 794 FunctionLiteral* ParseFunctionLiteral( |
796 const AstRawString* name, Scanner::Location function_name_location, | 795 const AstRawString* name, Scanner::Location function_name_location, |
797 FunctionNameValidity function_name_validity, FunctionKind kind, | 796 FunctionNameValidity function_name_validity, FunctionKind kind, |
798 int function_token_position, FunctionLiteral::FunctionType type, | 797 int function_token_position, FunctionLiteral::FunctionType type, |
799 FunctionLiteral::ArityRestriction arity_restriction, | 798 FunctionLiteral::ArityRestriction arity_restriction, |
800 LanguageMode language_mode, bool* ok); | 799 LanguageMode language_mode, bool* ok); |
801 V8_INLINE void SkipLazyFunctionBody( | 800 V8_INLINE void SkipLazyFunctionBody( |
802 int* materialized_literal_count, int* expected_property_count, bool* ok, | 801 int* materialized_literal_count, int* expected_property_count, bool* ok, |
803 Scanner::BookmarkScope* bookmark = nullptr); | 802 Scanner::BookmarkScope* bookmark = nullptr); |
804 V8_INLINE ZoneList<Statement*>* ParseEagerFunctionBody( | 803 V8_INLINE ZoneList<Statement*>* ParseEagerFunctionBody( |
805 const AstRawString* name, int pos, | 804 const AstRawString* name, int pos, |
806 const ParserFormalParameterParsingState& formal_parameters, | 805 const ParserFormalParameters& parameters, |
807 Variable* fvar, Token::Value fvar_init_op, FunctionKind kind, bool* ok); | 806 Variable* fvar, Token::Value fvar_init_op, FunctionKind kind, bool* ok); |
808 | 807 |
809 ClassLiteral* ParseClassLiteral(const AstRawString* name, | 808 ClassLiteral* ParseClassLiteral(const AstRawString* name, |
810 Scanner::Location class_name_location, | 809 Scanner::Location class_name_location, |
811 bool name_is_strict_reserved, int pos, | 810 bool name_is_strict_reserved, int pos, |
812 bool* ok); | 811 bool* ok); |
813 | 812 |
814 V8_INLINE void CheckConflictingVarDeclarations(v8::internal::Scope* scope, | 813 V8_INLINE void CheckConflictingVarDeclarations(v8::internal::Scope* scope, |
815 bool* ok); | 814 bool* ok); |
816 | 815 |
(...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1148 // If bookmark is set, the (pre-)parser may decide to abort skipping | 1147 // If bookmark is set, the (pre-)parser may decide to abort skipping |
1149 // in order to force the function to be eagerly parsed, after all. | 1148 // in order to force the function to be eagerly parsed, after all. |
1150 // In this case, it'll reset the scanner using the bookmark. | 1149 // In this case, it'll reset the scanner using the bookmark. |
1151 void SkipLazyFunctionBody(int* materialized_literal_count, | 1150 void SkipLazyFunctionBody(int* materialized_literal_count, |
1152 int* expected_property_count, bool* ok, | 1151 int* expected_property_count, bool* ok, |
1153 Scanner::BookmarkScope* bookmark = nullptr); | 1152 Scanner::BookmarkScope* bookmark = nullptr); |
1154 | 1153 |
1155 PreParser::PreParseResult ParseLazyFunctionBodyWithPreParser( | 1154 PreParser::PreParseResult ParseLazyFunctionBodyWithPreParser( |
1156 SingletonLogger* logger, Scanner::BookmarkScope* bookmark = nullptr); | 1155 SingletonLogger* logger, Scanner::BookmarkScope* bookmark = nullptr); |
1157 | 1156 |
1158 bool IsSimpleParameterList( | 1157 bool IsSimpleParameterList(const ParserFormalParameters& parameters); |
1159 const ParserFormalParameterParsingState& formal_parameters); | |
1160 Block* BuildParameterInitializationBlock( | 1158 Block* BuildParameterInitializationBlock( |
1161 const ParserFormalParameterParsingState& formal_parameters, bool* ok); | 1159 const ParserFormalParameters& parameters, bool* ok); |
1162 | 1160 |
1163 // Consumes the ending }. | 1161 // Consumes the ending }. |
1164 ZoneList<Statement*>* ParseEagerFunctionBody( | 1162 ZoneList<Statement*>* ParseEagerFunctionBody( |
1165 const AstRawString* function_name, int pos, | 1163 const AstRawString* function_name, int pos, |
1166 const ParserFormalParameterParsingState& formal_parameters, | 1164 const ParserFormalParameters& parameters, |
1167 Variable* fvar, Token::Value fvar_init_op, FunctionKind kind, bool* ok); | 1165 Variable* fvar, Token::Value fvar_init_op, FunctionKind kind, bool* ok); |
1168 | 1166 |
1169 void ThrowPendingError(Isolate* isolate, Handle<Script> script); | 1167 void ThrowPendingError(Isolate* isolate, Handle<Script> script); |
1170 | 1168 |
1171 TemplateLiteralState OpenTemplateLiteral(int pos); | 1169 TemplateLiteralState OpenTemplateLiteral(int pos); |
1172 void AddTemplateSpan(TemplateLiteralState* state, bool tail); | 1170 void AddTemplateSpan(TemplateLiteralState* state, bool tail); |
1173 void AddTemplateExpression(TemplateLiteralState* state, | 1171 void AddTemplateExpression(TemplateLiteralState* state, |
1174 Expression* expression); | 1172 Expression* expression); |
1175 Expression* CloseTemplateLiteral(TemplateLiteralState* state, int start, | 1173 Expression* CloseTemplateLiteral(TemplateLiteralState* state, int start, |
1176 Expression* tag); | 1174 Expression* tag); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1222 void ParserTraits::SkipLazyFunctionBody(int* materialized_literal_count, | 1220 void ParserTraits::SkipLazyFunctionBody(int* materialized_literal_count, |
1223 int* expected_property_count, bool* ok, | 1221 int* expected_property_count, bool* ok, |
1224 Scanner::BookmarkScope* bookmark) { | 1222 Scanner::BookmarkScope* bookmark) { |
1225 return parser_->SkipLazyFunctionBody(materialized_literal_count, | 1223 return parser_->SkipLazyFunctionBody(materialized_literal_count, |
1226 expected_property_count, ok, bookmark); | 1224 expected_property_count, ok, bookmark); |
1227 } | 1225 } |
1228 | 1226 |
1229 | 1227 |
1230 ZoneList<Statement*>* ParserTraits::ParseEagerFunctionBody( | 1228 ZoneList<Statement*>* ParserTraits::ParseEagerFunctionBody( |
1231 const AstRawString* name, int pos, | 1229 const AstRawString* name, int pos, |
1232 const ParserFormalParameterParsingState& formal_parameters, Variable* fvar, | 1230 const ParserFormalParameters& parameters, Variable* fvar, |
1233 Token::Value fvar_init_op, FunctionKind kind, bool* ok) { | 1231 Token::Value fvar_init_op, FunctionKind kind, bool* ok) { |
1234 return parser_->ParseEagerFunctionBody(name, pos, formal_parameters, fvar, | 1232 return parser_->ParseEagerFunctionBody(name, pos, parameters, fvar, |
1235 fvar_init_op, kind, ok); | 1233 fvar_init_op, kind, ok); |
1236 } | 1234 } |
1237 | 1235 |
1238 void ParserTraits::CheckConflictingVarDeclarations(v8::internal::Scope* scope, | 1236 void ParserTraits::CheckConflictingVarDeclarations(v8::internal::Scope* scope, |
1239 bool* ok) { | 1237 bool* ok) { |
1240 parser_->CheckConflictingVarDeclarations(scope, ok); | 1238 parser_->CheckConflictingVarDeclarations(scope, ok); |
1241 } | 1239 } |
1242 | 1240 |
1243 | 1241 |
1244 // Support for handling complex values (array and object literals) that | 1242 // Support for handling complex values (array and object literals) that |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1305 } | 1303 } |
1306 | 1304 |
1307 | 1305 |
1308 Expression* ParserTraits::SpreadCallNew( | 1306 Expression* ParserTraits::SpreadCallNew( |
1309 Expression* function, ZoneList<v8::internal::Expression*>* args, int pos) { | 1307 Expression* function, ZoneList<v8::internal::Expression*>* args, int pos) { |
1310 return parser_->SpreadCallNew(function, args, pos); | 1308 return parser_->SpreadCallNew(function, args, pos); |
1311 } | 1309 } |
1312 | 1310 |
1313 | 1311 |
1314 void ParserTraits::DeclareFormalParameter( | 1312 void ParserTraits::DeclareFormalParameter( |
1315 ParserFormalParameterParsingState* parsing_state, Expression* pattern, | 1313 ParserFormalParameters* parameters, Expression* pattern, bool is_rest, |
1316 ExpressionClassifier* classifier, bool is_rest) { | 1314 ExpressionClassifier* classifier) { |
1317 bool is_duplicate = false; | 1315 bool is_duplicate = false; |
1318 bool is_simple_name = pattern->IsVariableProxy(); | 1316 bool is_simple = pattern->IsVariableProxy(); |
1319 DCHECK(parser_->allow_harmony_destructuring() || is_simple_name); | 1317 DCHECK(parser_->allow_harmony_destructuring() || is_simple); |
1320 | 1318 |
1321 const AstRawString* name = is_simple_name | 1319 const AstRawString* name = is_simple |
1322 ? pattern->AsVariableProxy()->raw_name() | 1320 ? pattern->AsVariableProxy()->raw_name() |
1323 : parser_->ast_value_factory()->empty_string(); | 1321 : parser_->ast_value_factory()->empty_string(); |
1324 Variable* var = | 1322 Variable* var = |
1325 parsing_state->scope->DeclareParameter(name, VAR, is_rest, &is_duplicate); | 1323 parameters->scope->DeclareParameter(name, VAR, is_rest, &is_duplicate); |
1326 parsing_state->AddParameter(var, is_simple_name ? nullptr : pattern); | 1324 parameters->AddParameter(var, is_simple ? nullptr : pattern); |
1327 if (is_sloppy(parsing_state->scope->language_mode())) { | 1325 if (is_duplicate) { |
| 1326 classifier->RecordDuplicateFormalParameterError( |
| 1327 parser_->scanner()->location()); |
| 1328 } |
| 1329 if (is_sloppy(parameters->scope->language_mode())) { |
1328 // TODO(sigurds) Mark every parameter as maybe assigned. This is a | 1330 // TODO(sigurds) Mark every parameter as maybe assigned. This is a |
1329 // conservative approximation necessary to account for parameters | 1331 // conservative approximation necessary to account for parameters |
1330 // that are assigned via the arguments array. | 1332 // that are assigned via the arguments array. |
1331 var->set_maybe_assigned(); | 1333 var->set_maybe_assigned(); |
1332 } | 1334 } |
1333 if (is_duplicate) { | |
1334 classifier->RecordDuplicateFormalParameterError( | |
1335 parser_->scanner()->location()); | |
1336 } | |
1337 } | 1335 } |
1338 | 1336 |
1339 | 1337 |
1340 void ParserTraits::AddParameterInitializationBlock( | 1338 void ParserTraits::AddParameterInitializationBlock( |
1341 const ParserFormalParameterParsingState& formal_parameters, | 1339 const ParserFormalParameters& parameters, |
1342 ZoneList<v8::internal::Statement*>* body, bool* ok) { | 1340 ZoneList<v8::internal::Statement*>* body, bool* ok) { |
1343 if (parser_->IsSimpleParameterList(formal_parameters)) return; | 1341 if (!parser_->IsSimpleParameterList(parameters)) { |
1344 auto* init_block = | 1342 auto* init_block = |
1345 parser_->BuildParameterInitializationBlock(formal_parameters, ok); | 1343 parser_->BuildParameterInitializationBlock(parameters, ok); |
1346 if (!*ok) return; | 1344 if (!*ok) return; |
1347 if (init_block != nullptr) { | 1345 if (init_block != nullptr) { |
1348 body->Add(init_block, parser_->zone()); | 1346 body->Add(init_block, parser_->zone()); |
| 1347 } |
1349 } | 1348 } |
1350 } | 1349 } |
1351 } } // namespace v8::internal | 1350 } } // namespace v8::internal |
1352 | 1351 |
1353 #endif // V8_PARSER_H_ | 1352 #endif // V8_PARSER_H_ |
OLD | NEW |