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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | src/parser.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/parser.h
diff --git a/src/parser.h b/src/parser.h
index 39d0d9278b132fd4dfc5a9ebd104661f1f846205..103bafea3341595f4d08ef58d425567ce037da99 100644
--- a/src/parser.h
+++ b/src/parser.h
@@ -538,6 +538,27 @@ class RegExpParser BASE_EMBEDDED {
class Parser;
class SingletonLogger;
+
+struct ParserFormalParameterParsingState
+ : public PreParserFormalParameterParsingState {
+ struct Parameter {
+ Parameter(Variable* var, Expression* pattern)
+ : var(var), pattern(pattern) {}
+ Variable* var;
+ Expression* pattern;
+ };
+
+ explicit ParserFormalParameterParsingState(Scope* scope)
+ : PreParserFormalParameterParsingState(scope), params(4, scope->zone()) {}
+
+ ZoneList<Parameter> params;
+
+ void AddParameter(Variable* var, Expression* pattern) {
+ params.Add(Parameter(var, pattern), scope->zone());
+ }
+};
+
+
class ParserTraits {
public:
struct Type {
@@ -560,7 +581,7 @@ class ParserTraits {
typedef ZoneList<v8::internal::Expression*>* ExpressionList;
typedef ZoneList<ObjectLiteral::Property*>* PropertyList;
typedef const v8::internal::AstRawString* FormalParameter;
- typedef Scope FormalParameterScope;
+ typedef ParserFormalParameterParsingState FormalParameterParsingState;
typedef ZoneList<v8::internal::Statement*>* StatementList;
// For constructing objects returned by the traversing functions.
@@ -751,17 +772,21 @@ class ParserTraits {
ZoneList<v8::internal::Statement*>* NewStatementList(int size, Zone* zone) {
return new(zone) ZoneList<v8::internal::Statement*>(size, zone);
}
+
+ V8_INLINE void AddParameterInitializationBlock(
+ const ParserFormalParameterParsingState& formal_parameters,
+ ZoneList<v8::internal::Statement*>* body, bool* ok);
+
V8_INLINE Scope* NewScope(Scope* parent_scope, ScopeType scope_type,
FunctionKind kind = kNormalFunction);
- V8_INLINE void DeclareFormalParameter(Scope* scope, Expression* name,
- ExpressionClassifier* classifier,
- bool is_rest);
- void ParseArrowFunctionFormalParameters(Scope* scope, Expression* params,
- const Scanner::Location& params_loc,
- bool* has_rest,
- Scanner::Location* duplicate_loc,
- bool* ok);
+ V8_INLINE void DeclareFormalParameter(
+ ParserFormalParameterParsingState* parsing_state, Expression* name,
+ ExpressionClassifier* classifier, bool is_rest);
+ void ParseArrowFunctionFormalParameters(
+ ParserFormalParameterParsingState* scope, Expression* params,
+ const Scanner::Location& params_loc, Scanner::Location* duplicate_loc,
+ bool* ok);
// Temporary glue; these functions will move to ParserBase.
Expression* ParseV8Intrinsic(bool* ok);
@@ -774,8 +799,9 @@ class ParserTraits {
int* materialized_literal_count, int* expected_property_count, bool* ok,
Scanner::BookmarkScope* bookmark = nullptr);
V8_INLINE ZoneList<Statement*>* ParseEagerFunctionBody(
- const AstRawString* name, int pos, Variable* fvar,
- Token::Value fvar_init_op, FunctionKind kind, bool* ok);
+ const AstRawString* name, int pos,
+ const ParserFormalParameterParsingState& formal_parameters,
+ Variable* fvar, Token::Value fvar_init_op, FunctionKind kind, bool* ok);
ClassLiteral* ParseClassLiteral(const AstRawString* name,
Scanner::Location class_name_location,
@@ -936,6 +962,7 @@ class Parser : public ParserBase<ParserTraits> {
bool* ok);
struct DeclarationDescriptor {
+ enum Kind { NORMAL, PARAMETER };
Parser* parser;
Scope* declaration_scope;
Scope* scope;
@@ -945,6 +972,7 @@ class Parser : public ParserBase<ParserTraits> {
int declaration_pos;
int initialization_pos;
Token::Value init_op;
+ Kind declaration_kind;
};
struct DeclarationParsingResult {
@@ -1095,7 +1123,9 @@ class Parser : public ParserBase<ParserTraits> {
// Parser support
VariableProxy* NewUnresolved(const AstRawString* name, VariableMode mode);
- Variable* Declare(Declaration* declaration, bool resolve, bool* ok);
+ Variable* Declare(Declaration* declaration,
+ DeclarationDescriptor::Kind declaration_kind, bool resolve,
+ bool* ok);
bool TargetStackContainsLabel(const AstRawString* label);
BreakableStatement* LookupBreakTarget(const AstRawString* label, bool* ok);
@@ -1121,10 +1151,14 @@ class Parser : public ParserBase<ParserTraits> {
PreParser::PreParseResult ParseLazyFunctionBodyWithPreParser(
SingletonLogger* logger, Scanner::BookmarkScope* bookmark = nullptr);
+ Block* BuildParameterInitializationBlock(
+ const ParserFormalParameterParsingState& formal_parameters, bool* ok);
+
// Consumes the ending }.
ZoneList<Statement*>* ParseEagerFunctionBody(
- const AstRawString* function_name, int pos, Variable* fvar,
- Token::Value fvar_init_op, FunctionKind kind, bool* ok);
+ const AstRawString* function_name, int pos,
+ const ParserFormalParameterParsingState& formal_parameters,
+ Variable* fvar, Token::Value fvar_init_op, FunctionKind kind, bool* ok);
void ThrowPendingError(Isolate* isolate, Handle<Script> script);
@@ -1188,10 +1222,11 @@ void ParserTraits::SkipLazyFunctionBody(int* materialized_literal_count,
ZoneList<Statement*>* ParserTraits::ParseEagerFunctionBody(
- const AstRawString* name, int pos, Variable* fvar,
+ const AstRawString* name, int pos,
+ const ParserFormalParameterParsingState& formal_parameters, Variable* fvar,
Token::Value fvar_init_op, FunctionKind kind, bool* ok) {
- return parser_->ParseEagerFunctionBody(name, pos, fvar, fvar_init_op, kind,
- ok);
+ return parser_->ParseEagerFunctionBody(name, pos, formal_parameters, fvar,
+ fvar_init_op, kind, ok);
}
void ParserTraits::CheckConflictingVarDeclarations(v8::internal::Scope* scope,
@@ -1270,18 +1305,20 @@ Expression* ParserTraits::SpreadCallNew(
}
-void ParserTraits::DeclareFormalParameter(Scope* scope, Expression* pattern,
- ExpressionClassifier* classifier,
- bool is_rest) {
+void ParserTraits::DeclareFormalParameter(
+ ParserFormalParameterParsingState* parsing_state, Expression* pattern,
+ ExpressionClassifier* classifier, bool is_rest) {
bool is_duplicate = false;
- if (!pattern->IsVariableProxy()) {
- // TODO(dslomov): implement.
- DCHECK(parser_->allow_harmony_destructuring());
- return;
- }
- auto name = pattern->AsVariableProxy()->raw_name();
- Variable* var = scope->DeclareParameter(name, VAR, is_rest, &is_duplicate);
- if (is_sloppy(scope->language_mode())) {
+ bool is_simple_name = pattern->IsVariableProxy();
+ DCHECK(parser_->allow_harmony_destructuring() || is_simple_name);
+
+ const AstRawString* name = is_simple_name
+ ? pattern->AsVariableProxy()->raw_name()
+ : parser_->ast_value_factory()->empty_string();
+ Variable* var =
+ parsing_state->scope->DeclareParameter(name, VAR, is_rest, &is_duplicate);
+ parsing_state->AddParameter(var, is_simple_name ? nullptr : pattern);
+ if (is_sloppy(parsing_state->scope->language_mode())) {
// TODO(sigurds) Mark every parameter as maybe assigned. This is a
// conservative approximation necessary to account for parameters
// that are assigned via the arguments array.
@@ -1292,6 +1329,18 @@ void ParserTraits::DeclareFormalParameter(Scope* scope, Expression* pattern,
parser_->scanner()->location());
}
}
+
+
+void ParserTraits::AddParameterInitializationBlock(
+ const ParserFormalParameterParsingState& formal_parameters,
+ ZoneList<v8::internal::Statement*>* body, bool* ok) {
+ auto* init_block =
+ parser_->BuildParameterInitializationBlock(formal_parameters, ok);
+ if (!*ok) return;
+ if (init_block != nullptr) {
+ body->Add(init_block, parser_->zone());
+ }
+}
} } // namespace v8::internal
#endif // V8_PARSER_H_
« 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