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

Unified Diff: src/preparser.h

Issue 1127063003: [es6] implement default parameters via desugaring (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Add more context-allocation tests Created 5 years, 7 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 side-by-side diff with in-line comments
Download patch
« src/parser.cc ('K') | « src/parser.cc ('k') | src/preparser.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/preparser.h
diff --git a/src/preparser.h b/src/preparser.h
index d38aaaaa26e7f1fa3876c041b708d123c054660a..3b9c58ce9e687e68a669b2b9bece4d89b22e8514 100644
--- a/src/preparser.h
+++ b/src/preparser.h
@@ -66,6 +66,7 @@ class ParserBase : public Traits {
public:
// Shorten type names defined by Traits.
typedef typename Traits::Type::Expression ExpressionT;
+ typedef typename Traits::Type::ExpressionList ExpressionListT;
typedef typename Traits::Type::Identifier IdentifierT;
typedef typename Traits::Type::FormalParameter FormalParameterT;
typedef typename Traits::Type::FormalParameterScope FormalParameterScopeT;
@@ -97,6 +98,7 @@ class ParserBase : public Traits {
allow_harmony_computed_property_names_(false),
allow_harmony_rest_params_(false),
allow_harmony_spreadcalls_(false),
+ allow_harmony_default_parameters_(false),
allow_strong_mode_(false) {}
// Getters that indicate whether certain syntactical constructs are
@@ -126,6 +128,9 @@ class ParserBase : public Traits {
bool allow_harmony_spread_arrays() const {
return allow_harmony_spread_arrays_;
}
+ bool allow_harmony_default_parameters() const {
+ return allow_harmony_default_parameters_;
+ }
bool allow_strong_mode() const { return allow_strong_mode_; }
@@ -167,6 +172,10 @@ class ParserBase : public Traits {
void set_allow_harmony_spread_arrays(bool allow) {
allow_harmony_spread_arrays_ = allow;
}
+ void set_allow_harmony_default_parameters(bool allow) {
+ allow_harmony_default_parameters_ = allow;
+ }
+
protected:
enum AllowRestrictedIdentifiers {
@@ -914,7 +923,9 @@ class ParserBase : public Traits {
void ParseFormalParameter(FormalParameterScopeT* scope, bool is_rest,
ExpressionClassifier* classifier, bool* ok);
- int ParseFormalParameterList(FormalParameterScopeT* scope, bool* has_rest,
+ int ParseFormalParameterList(FormalParameterScopeT* scope,
+ ExpressionListT initializers,
+ bool* hasParameterExpressions, bool* has_rest,
ExpressionClassifier* classifier, bool* ok);
void CheckArityRestrictions(
int param_count, FunctionLiteral::ArityRestriction arity_restriction,
@@ -1017,6 +1028,7 @@ class ParserBase : public Traits {
bool allow_harmony_spreadcalls_;
bool allow_harmony_destructuring_;
bool allow_harmony_spread_arrays_;
+ bool allow_harmony_default_parameters_;
bool allow_strong_mode_;
};
@@ -1793,8 +1805,8 @@ class PreParserTraits {
}
V8_INLINE bool DeclareFormalParameter(DuplicateFinder* scope,
- PreParserIdentifier param,
- bool is_rest);
+ PreParserIdentifier param, bool is_rest,
+ int pos);
void CheckConflictingVarDeclarations(Scope* scope, bool* ok) {}
@@ -1990,7 +2002,7 @@ PreParserExpression PreParserTraits::SpreadCallNew(PreParserExpression function,
bool PreParserTraits::DeclareFormalParameter(
DuplicateFinder* duplicate_finder, PreParserIdentifier current_identifier,
- bool is_rest) {
+ bool is_rest, int pos) {
return pre_parser_->scanner()->FindSymbol(duplicate_finder, 1) != 0;
}
@@ -3661,10 +3673,11 @@ void ParserBase<Traits>::ParseFormalParameter(FormalParameterScopeT* scope,
bool* ok) {
// FormalParameter[Yield,GeneratorParameter] :
// BindingElement[?Yield, ?GeneratorParameter]
+ int pos = peek_position();
IdentifierT name = ParseAndClassifyIdentifier(classifier, ok);
if (!*ok) return;
- bool was_declared = Traits::DeclareFormalParameter(scope, name, is_rest);
+ bool was_declared = Traits::DeclareFormalParameter(scope, name, is_rest, pos);
if (was_declared) {
classifier->RecordDuplicateFormalParameterError(scanner()->location());
}
@@ -3673,7 +3686,8 @@ void ParserBase<Traits>::ParseFormalParameter(FormalParameterScopeT* scope,
template <class Traits>
int ParserBase<Traits>::ParseFormalParameterList(
- FormalParameterScopeT* scope, bool* is_rest,
+ FormalParameterScopeT* scope, ExpressionListT initializers,
+ bool* hasParameterExpressions, bool* is_rest,
ExpressionClassifier* classifier, bool* ok) {
// FormalParameters[Yield,GeneratorParameter] :
// [empty]
@@ -3693,14 +3707,43 @@ int ParserBase<Traits>::ParseFormalParameterList(
if (peek() != Token::RPAREN) {
do {
+ Scope* param_scope = NewScope(scope_, BLOCK_SCOPE);
+ BlockState param_state(&scope_, param_scope);
+ param_scope->set_start_position(peek_position());
if (++parameter_count > Code::kMaxArguments) {
ReportMessage(MessageTemplate::kTooManyParameters);
*ok = false;
return -1;
}
+
+ int start_pos = peek_position();
*is_rest = allow_harmony_rest_params() && Check(Token::ELLIPSIS);
ParseFormalParameter(scope, *is_rest, classifier, ok);
if (!*ok) return -1;
+
+ // TODO(caitp, dslomov): set *hasParameterExpressions to true if
+ // formal parameter is an ObjectBindingPattern containing computed
+ // property keys
+
+ ExpressionT initializer = this->EmptyExpression();
+ if (allow_harmony_default_parameters() && Check(Token::ASSIGN)) {
+ // Default parameter initializer
+ static const bool accept_IN = true;
+ ExpressionClassifier classifier;
+ initializer = ParseAssignmentExpression(accept_IN, &classifier, ok);
+ if (!*ok) return -1;
+ *hasParameterExpressions = true;
+
+ // A rest parameter cannot be initialized.
+ if (*is_rest) {
+ Scanner::Location loc(start_pos, scanner()->location().end_pos);
+ ReportMessageAt(loc, MessageTemplate::kBadRestParameterInitializer);
+ *ok = false;
+ return -1;
+ }
+ }
+ param_scope->set_end_position(scanner()->location().end_pos);
+ initializers->Add(initializer, zone());
} while (!*is_rest && Check(Token::COMMA));
if (*is_rest && peek() == Token::COMMA) {
« src/parser.cc ('K') | « src/parser.cc ('k') | src/preparser.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698