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 ParserFormalParameters : public PreParserFormalParameters { | 542 struct ParserFormalParameters : FormalParametersBase { |
543 struct Parameter { | 543 struct Parameter { |
544 Parameter(const AstRawString* name, Expression* pattern, bool is_rest) | 544 Parameter(const AstRawString* name, Expression* pattern, bool is_rest) |
545 : name(name), pattern(pattern), is_rest(is_rest) {} | 545 : name(name), pattern(pattern), is_rest(is_rest) {} |
546 const AstRawString* name; | 546 const AstRawString* name; |
547 Expression* pattern; | 547 Expression* pattern; |
548 bool is_rest; | 548 bool is_rest; |
549 }; | 549 }; |
550 | 550 |
551 explicit ParserFormalParameters(Scope* scope) | 551 explicit ParserFormalParameters(Scope* scope) |
552 : PreParserFormalParameters(scope), params(4, scope->zone()) {} | 552 : FormalParametersBase(scope), params(4, scope->zone()) {} |
553 | |
554 ZoneList<Parameter> params; | 553 ZoneList<Parameter> params; |
555 | 554 |
556 void AddParameter( | 555 int Arity() const { return params.length(); } |
557 const AstRawString* name, Expression* pattern, bool is_rest) { | 556 const Parameter& at(int i) const { return params[i]; } |
558 params.Add(Parameter(name, pattern, is_rest), scope->zone()); | |
559 DCHECK_EQ(arity, params.length()); | |
560 } | |
561 }; | 557 }; |
562 | 558 |
563 | 559 |
564 class ParserTraits { | 560 class ParserTraits { |
565 public: | 561 public: |
566 struct Type { | 562 struct Type { |
567 // TODO(marja): To be removed. The Traits object should contain all the data | 563 // TODO(marja): To be removed. The Traits object should contain all the data |
568 // it needs. | 564 // it needs. |
569 typedef v8::internal::Parser* Parser; | 565 typedef v8::internal::Parser* Parser; |
570 | 566 |
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
775 return new(zone) ZoneList<v8::internal::Statement*>(size, zone); | 771 return new(zone) ZoneList<v8::internal::Statement*>(size, zone); |
776 } | 772 } |
777 | 773 |
778 V8_INLINE void AddParameterInitializationBlock( | 774 V8_INLINE void AddParameterInitializationBlock( |
779 const ParserFormalParameters& parameters, | 775 const ParserFormalParameters& parameters, |
780 ZoneList<v8::internal::Statement*>* body, bool* ok); | 776 ZoneList<v8::internal::Statement*>* body, bool* ok); |
781 | 777 |
782 V8_INLINE Scope* NewScope(Scope* parent_scope, ScopeType scope_type, | 778 V8_INLINE Scope* NewScope(Scope* parent_scope, ScopeType scope_type, |
783 FunctionKind kind = kNormalFunction); | 779 FunctionKind kind = kNormalFunction); |
784 | 780 |
781 V8_INLINE void AddFormalParameter( | |
782 ParserFormalParameters* parameters, Expression* pattern, bool is_rest); | |
785 V8_INLINE void DeclareFormalParameter( | 783 V8_INLINE void DeclareFormalParameter( |
786 ParserFormalParameters* parameters, Expression* pattern, bool is_rest, | 784 Scope* scope, const ParserFormalParameters::Parameter& parameter, |
787 ExpressionClassifier* classifier); | 785 bool is_simple, ExpressionClassifier* classifier); |
788 void ParseArrowFunctionFormalParameters( | 786 void ParseArrowFunctionFormalParameters( |
789 ParserFormalParameters* parameters, Expression* params, | 787 ParserFormalParameters* parameters, Expression* params, |
790 const Scanner::Location& params_loc, | 788 const Scanner::Location& params_loc, |
791 Scanner::Location* duplicate_loc, bool* ok); | 789 Scanner::Location* duplicate_loc, bool* ok); |
790 void ParseArrowFunctionFormalParameterList( | |
791 ParserFormalParameters* parameters, Expression* params, | |
792 const Scanner::Location& params_loc, | |
793 Scanner::Location* duplicate_loc, bool* ok); | |
792 | 794 |
793 void ReindexLiterals(const ParserFormalParameters& parameters); | 795 void ReindexLiterals(const ParserFormalParameters& parameters); |
794 | 796 |
795 // Temporary glue; these functions will move to ParserBase. | 797 // Temporary glue; these functions will move to ParserBase. |
796 Expression* ParseV8Intrinsic(bool* ok); | 798 Expression* ParseV8Intrinsic(bool* ok); |
797 FunctionLiteral* ParseFunctionLiteral( | 799 FunctionLiteral* ParseFunctionLiteral( |
798 const AstRawString* name, Scanner::Location function_name_location, | 800 const AstRawString* name, Scanner::Location function_name_location, |
799 FunctionNameValidity function_name_validity, FunctionKind kind, | 801 FunctionNameValidity function_name_validity, FunctionKind kind, |
800 int function_token_position, FunctionLiteral::FunctionType type, | 802 int function_token_position, FunctionLiteral::FunctionType type, |
801 FunctionLiteral::ArityRestriction arity_restriction, | 803 FunctionLiteral::ArityRestriction arity_restriction, |
(...skipping 501 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1303 return parser_->SpreadCall(function, args, pos); | 1305 return parser_->SpreadCall(function, args, pos); |
1304 } | 1306 } |
1305 | 1307 |
1306 | 1308 |
1307 Expression* ParserTraits::SpreadCallNew( | 1309 Expression* ParserTraits::SpreadCallNew( |
1308 Expression* function, ZoneList<v8::internal::Expression*>* args, int pos) { | 1310 Expression* function, ZoneList<v8::internal::Expression*>* args, int pos) { |
1309 return parser_->SpreadCallNew(function, args, pos); | 1311 return parser_->SpreadCallNew(function, args, pos); |
1310 } | 1312 } |
1311 | 1313 |
1312 | 1314 |
1315 void ParserTraits::AddFormalParameter( | |
1316 ParserFormalParameters* parameters, Expression* pattern, bool is_rest) { | |
1317 bool is_simple = pattern->IsVariableProxy(); | |
1318 DCHECK(parser_->allow_harmony_destructuring() || | |
1319 parser_->allow_harmony_rest_parameters() || is_simple); | |
1320 const AstRawString* name = is_simple | |
1321 ? pattern->AsVariableProxy()->raw_name() | |
1322 : parser_->ast_value_factory()->empty_string(); | |
1323 parameters->params.Add( | |
1324 ParserFormalParameters::Parameter(name, pattern, is_rest), | |
1325 parameters->scope->zone()); | |
1326 } | |
1327 | |
1328 | |
1313 void ParserTraits::DeclareFormalParameter( | 1329 void ParserTraits::DeclareFormalParameter( |
1314 ParserFormalParameters* parameters, Expression* pattern, bool is_rest, | 1330 Scope* scope, const ParserFormalParameters::Parameter& parameter, |
1315 ExpressionClassifier* classifier) { | 1331 bool is_simple, ExpressionClassifier* classifier) { |
1316 bool is_duplicate = false; | 1332 bool is_duplicate = false; |
1317 bool is_simple = pattern->IsVariableProxy(); | 1333 // TODO(caitp): Remove special handling for rest once desugaring is in. |
1318 DCHECK(parser_->allow_harmony_destructuring() || is_simple); | 1334 auto name = is_simple || parameter.is_rest |
1319 | 1335 ? parameter.name : parser_->ast_value_factory()->empty_string(); |
1320 const AstRawString* name = is_simple | 1336 auto mode = is_simple || parameter.is_rest ? VAR : TEMPORARY; |
caitp (gmail)
2015/08/05 12:53:53
Another piece of potential cleanup, the name-picki
| |
1321 ? pattern->AsVariableProxy()->raw_name() | |
1322 : parser_->ast_value_factory()->empty_string(); | |
1323 VariableMode mode = is_simple ? VAR : TEMPORARY; | |
1324 Variable* var = | 1337 Variable* var = |
1325 parameters->scope->DeclareParameter(name, mode, is_rest, &is_duplicate); | 1338 scope->DeclareParameter(name, mode, parameter.is_rest, &is_duplicate); |
1326 parameters->AddParameter(name, is_simple ? nullptr : pattern, is_rest); | |
1327 if (is_duplicate) { | 1339 if (is_duplicate) { |
1328 classifier->RecordDuplicateFormalParameterError( | 1340 classifier->RecordDuplicateFormalParameterError( |
1329 parser_->scanner()->location()); | 1341 parser_->scanner()->location()); |
1330 } | 1342 } |
1331 if (is_sloppy(parameters->scope->language_mode())) { | 1343 if (is_sloppy(scope->language_mode())) { |
1332 // TODO(sigurds) Mark every parameter as maybe assigned. This is a | 1344 // TODO(sigurds) Mark every parameter as maybe assigned. This is a |
1333 // conservative approximation necessary to account for parameters | 1345 // conservative approximation necessary to account for parameters |
1334 // that are assigned via the arguments array. | 1346 // that are assigned via the arguments array. |
1335 var->set_maybe_assigned(); | 1347 var->set_maybe_assigned(); |
1336 } | 1348 } |
1337 } | 1349 } |
1338 | 1350 |
1339 | 1351 |
1340 void ParserTraits::AddParameterInitializationBlock( | 1352 void ParserTraits::AddParameterInitializationBlock( |
1341 const ParserFormalParameters& parameters, | 1353 const ParserFormalParameters& parameters, |
1342 ZoneList<v8::internal::Statement*>* body, bool* ok) { | 1354 ZoneList<v8::internal::Statement*>* body, bool* ok) { |
1343 if (!parameters.is_simple) { | 1355 if (!parameters.is_simple) { |
1344 auto* init_block = | 1356 auto* init_block = |
1345 parser_->BuildParameterInitializationBlock(parameters, ok); | 1357 parser_->BuildParameterInitializationBlock(parameters, ok); |
1346 if (!*ok) return; | 1358 if (!*ok) return; |
1347 if (init_block != nullptr) { | 1359 if (init_block != nullptr) { |
1348 body->Add(init_block, parser_->zone()); | 1360 body->Add(init_block, parser_->zone()); |
1349 } | 1361 } |
1350 } | 1362 } |
1351 } | 1363 } |
1352 } } // namespace v8::internal | 1364 } } // namespace v8::internal |
1353 | 1365 |
1354 #endif // V8_PARSER_H_ | 1366 #endif // V8_PARSER_H_ |
OLD | NEW |