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

Side by Side Diff: src/parsing/preparser.h

Issue 2400613003: PreParsing inner functions: Fix declaration-only variables, part 2. (Closed)
Patch Set: rebased Created 4 years, 2 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 | « src/parsing/parser.cc ('k') | src/parsing/preparser.cc » ('j') | no next file with comments »
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_PARSING_PREPARSER_H 5 #ifndef V8_PARSING_PREPARSER_H
6 #define V8_PARSING_PREPARSER_H 6 #define V8_PARSING_PREPARSER_H
7 7
8 #include "src/ast/scopes.h" 8 #include "src/ast/scopes.h"
9 #include "src/parsing/parser-base.h" 9 #include "src/parsing/parser-base.h"
10 10
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
111 kAwaitIdentifier, 111 kAwaitIdentifier,
112 kAsyncIdentifier 112 kAsyncIdentifier
113 }; 113 };
114 114
115 explicit PreParserIdentifier(Type type) : type_(type), string_(nullptr) {} 115 explicit PreParserIdentifier(Type type) : type_(type), string_(nullptr) {}
116 Type type_; 116 Type type_;
117 // Only non-nullptr when PreParser.track_unresolved_variables_ is true. 117 // Only non-nullptr when PreParser.track_unresolved_variables_ is true.
118 const AstRawString* string_; 118 const AstRawString* string_;
119 friend class PreParserExpression; 119 friend class PreParserExpression;
120 friend class PreParser; 120 friend class PreParser;
121 friend class PreParserFactory;
121 }; 122 };
122 123
123 124
124 class PreParserExpression { 125 class PreParserExpression {
125 public: 126 public:
126 PreParserExpression() : code_(TypeField::encode(kEmpty)) {} 127 PreParserExpression()
128 : code_(TypeField::encode(kEmpty)), identifiers_(nullptr) {}
127 129
128 static PreParserExpression Empty() { return PreParserExpression(); } 130 static PreParserExpression Empty() { return PreParserExpression(); }
129 131
130 static PreParserExpression Default() { 132 static PreParserExpression Default(
131 return PreParserExpression(TypeField::encode(kExpression)); 133 ZoneList<const AstRawString*>* identifiers = nullptr) {
134 return PreParserExpression(TypeField::encode(kExpression), identifiers);
132 } 135 }
133 136
134 static PreParserExpression Spread(PreParserExpression expression) { 137 static PreParserExpression Spread(PreParserExpression expression) {
135 return PreParserExpression(TypeField::encode(kSpreadExpression)); 138 return PreParserExpression(TypeField::encode(kSpreadExpression),
139 expression.identifiers_);
136 } 140 }
137 141
138 static PreParserExpression FromIdentifier(PreParserIdentifier id) { 142 static PreParserExpression FromIdentifier(PreParserIdentifier id,
139 return PreParserExpression(TypeField::encode(kIdentifierExpression) | 143 Zone* zone) {
140 IdentifierTypeField::encode(id.type_), 144 PreParserExpression expression(TypeField::encode(kIdentifierExpression) |
141 id.string_); 145 IdentifierTypeField::encode(id.type_));
146 expression.AddIdentifier(id.string_, zone);
147 return expression;
142 } 148 }
143 149
144 static PreParserExpression BinaryOperation(PreParserExpression left, 150 static PreParserExpression BinaryOperation(PreParserExpression left,
145 Token::Value op, 151 Token::Value op,
146 PreParserExpression right) { 152 PreParserExpression right) {
147 return PreParserExpression(TypeField::encode(kBinaryOperationExpression)); 153 return PreParserExpression(TypeField::encode(kBinaryOperationExpression));
148 } 154 }
149 155
150 static PreParserExpression Assignment() { 156 static PreParserExpression Assignment() {
151 return PreParserExpression(TypeField::encode(kExpression) | 157 return PreParserExpression(TypeField::encode(kExpression) |
152 ExpressionTypeField::encode(kAssignment)); 158 ExpressionTypeField::encode(kAssignment));
153 } 159 }
154 160
155 static PreParserExpression ObjectLiteral() { 161 static PreParserExpression ObjectLiteral(
156 return PreParserExpression(TypeField::encode(kObjectLiteralExpression)); 162 ZoneList<const AstRawString*>* identifiers = nullptr) {
163 return PreParserExpression(TypeField::encode(kObjectLiteralExpression),
164 identifiers);
157 } 165 }
158 166
159 static PreParserExpression ArrayLiteral() { 167 static PreParserExpression ArrayLiteral(
160 return PreParserExpression(TypeField::encode(kArrayLiteralExpression)); 168 ZoneList<const AstRawString*>* identifiers = nullptr) {
169 return PreParserExpression(TypeField::encode(kArrayLiteralExpression),
170 identifiers);
161 } 171 }
162 172
163 static PreParserExpression StringLiteral() { 173 static PreParserExpression StringLiteral() {
164 return PreParserExpression(TypeField::encode(kStringLiteralExpression)); 174 return PreParserExpression(TypeField::encode(kStringLiteralExpression));
165 } 175 }
166 176
167 static PreParserExpression UseStrictStringLiteral() { 177 static PreParserExpression UseStrictStringLiteral() {
168 return PreParserExpression(TypeField::encode(kStringLiteralExpression) | 178 return PreParserExpression(TypeField::encode(kStringLiteralExpression) |
169 IsUseStrictField::encode(true)); 179 IsUseStrictField::encode(true));
170 } 180 }
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after
337 kThisExpression, 347 kThisExpression,
338 kThisPropertyExpression, 348 kThisPropertyExpression,
339 kPropertyExpression, 349 kPropertyExpression,
340 kCallExpression, 350 kCallExpression,
341 kCallEvalExpression, 351 kCallEvalExpression,
342 kSuperCallReference, 352 kSuperCallReference,
343 kNoTemplateTagExpression, 353 kNoTemplateTagExpression,
344 kAssignment 354 kAssignment
345 }; 355 };
346 356
347 explicit PreParserExpression(uint32_t expression_code, 357 explicit PreParserExpression(
348 const AstRawString* string = nullptr) 358 uint32_t expression_code,
349 : code_(expression_code), string_(string) {} 359 ZoneList<const AstRawString*>* identifiers = nullptr)
360 : code_(expression_code), identifiers_(identifiers) {}
361
362 void AddIdentifier(const AstRawString* identifier, Zone* zone) {
363 if (identifier == nullptr) {
364 return;
365 }
366 if (identifiers_ == nullptr) {
367 identifiers_ = new (zone) ZoneList<const AstRawString*>(1, zone);
368 }
369 identifiers_->Add(identifier, zone);
370 }
350 371
351 // The first three bits are for the Type. 372 // The first three bits are for the Type.
352 typedef BitField<Type, 0, 3> TypeField; 373 typedef BitField<Type, 0, 3> TypeField;
353 374
354 // The high order bit applies only to nodes which would inherit from the 375 // The high order bit applies only to nodes which would inherit from the
355 // Expression ASTNode --- This is by necessity, due to the fact that 376 // Expression ASTNode --- This is by necessity, due to the fact that
356 // Expression nodes may be represented as multiple Types, not exclusively 377 // Expression nodes may be represented as multiple Types, not exclusively
357 // through kExpression. 378 // through kExpression.
358 // TODO(caitp, adamk): clean up PreParserExpression bitfields. 379 // TODO(caitp, adamk): clean up PreParserExpression bitfields.
359 typedef BitField<bool, 31, 1> ParenthesizedField; 380 typedef BitField<bool, 31, 1> ParenthesizedField;
360 381
361 // The rest of the bits are interpreted depending on the value 382 // The rest of the bits are interpreted depending on the value
362 // of the Type field, so they can share the storage. 383 // of the Type field, so they can share the storage.
363 typedef BitField<ExpressionType, TypeField::kNext, 3> ExpressionTypeField; 384 typedef BitField<ExpressionType, TypeField::kNext, 3> ExpressionTypeField;
364 typedef BitField<bool, TypeField::kNext, 1> IsUseStrictField; 385 typedef BitField<bool, TypeField::kNext, 1> IsUseStrictField;
365 typedef BitField<bool, IsUseStrictField::kNext, 1> IsUseAsmField; 386 typedef BitField<bool, IsUseStrictField::kNext, 1> IsUseAsmField;
366 typedef BitField<PreParserIdentifier::Type, TypeField::kNext, 10> 387 typedef BitField<PreParserIdentifier::Type, TypeField::kNext, 10>
367 IdentifierTypeField; 388 IdentifierTypeField;
368 typedef BitField<bool, TypeField::kNext, 1> HasCoverInitializedNameField; 389 typedef BitField<bool, TypeField::kNext, 1> HasCoverInitializedNameField;
369 390
370 uint32_t code_; 391 uint32_t code_;
371 // Non-nullptr if the expression is one identifier. 392 // If the PreParser is used in the identifier tracking mode,
372 const AstRawString* string_; 393 // PreParserExpression accumulates identifiers in that expression.
394 ZoneList<const AstRawString*>* identifiers_;
373 395
374 friend class PreParser; 396 friend class PreParser;
397 friend class PreParserFactory;
398 template <typename T>
399 friend class PreParserList;
375 }; 400 };
376 401
377 402
378 // The pre-parser doesn't need to build lists of expressions, identifiers, or 403 // The pre-parser doesn't need to build lists of expressions, identifiers, or
379 // the like. 404 // the like. If the PreParser is used in identifier tracking mode, it needs to
405 // build lists of identifiers though.
380 template <typename T> 406 template <typename T>
381 class PreParserList { 407 class PreParserList {
382 public: 408 public:
383 // These functions make list->Add(some_expression) work (and do nothing). 409 // These functions make list->Add(some_expression) work (and do nothing).
384 PreParserList() : length_(0) {} 410 PreParserList() : length_(0), identifiers_(nullptr) {}
385 PreParserList* operator->() { return this; } 411 PreParserList* operator->() { return this; }
386 void Add(T, void*) { ++length_; } 412 void Add(T, Zone* zone);
387 int length() const { return length_; } 413 int length() const { return length_; }
388 static PreParserList Null() { return PreParserList(-1); } 414 static PreParserList Null() { return PreParserList(-1); }
389 bool IsNull() const { return length_ == -1; } 415 bool IsNull() const { return length_ == -1; }
390 416
391 private: 417 private:
392 explicit PreParserList(int n) : length_(n) {} 418 explicit PreParserList(int n) : length_(n), identifiers_(nullptr) {}
393 int length_; 419 int length_;
420 ZoneList<const AstRawString*>* identifiers_;
421
422 friend class PreParser;
423 friend class PreParserFactory;
394 }; 424 };
395 425
426 template <>
427 inline void PreParserList<PreParserExpression>::Add(
428 PreParserExpression expression, Zone* zone) {
429 if (expression.identifiers_ != nullptr) {
430 DCHECK(FLAG_lazy_inner_functions);
431 DCHECK(zone != nullptr);
432 if (identifiers_ == nullptr) {
433 identifiers_ = new (zone) ZoneList<const AstRawString*>(1, zone);
434 }
435 for (auto identifier : (*expression.identifiers_)) {
436 identifiers_->Add(identifier, zone);
437 }
438 }
439 ++length_;
440 }
441
442 template <typename T>
443 void PreParserList<T>::Add(T, Zone* zone) {
444 ++length_;
445 }
446
396 typedef PreParserList<PreParserExpression> PreParserExpressionList; 447 typedef PreParserList<PreParserExpression> PreParserExpressionList;
397 448
398 class PreParserStatement; 449 class PreParserStatement;
399 typedef PreParserList<PreParserStatement> PreParserStatementList; 450 typedef PreParserList<PreParserStatement> PreParserStatementList;
400 451
401 class PreParserStatement { 452 class PreParserStatement {
402 public: 453 public:
403 static PreParserStatement Default() { 454 static PreParserStatement Default() {
404 return PreParserStatement(kUnknownStatement); 455 return PreParserStatement(kUnknownStatement);
405 } 456 }
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
473 kUseAsmExpressionStatement, 524 kUseAsmExpressionStatement,
474 }; 525 };
475 526
476 explicit PreParserStatement(Type code) : code_(code) {} 527 explicit PreParserStatement(Type code) : code_(code) {}
477 Type code_; 528 Type code_;
478 }; 529 };
479 530
480 531
481 class PreParserFactory { 532 class PreParserFactory {
482 public: 533 public:
483 explicit PreParserFactory(void* unused_value_factory) {} 534 explicit PreParserFactory(AstValueFactory* ast_value_factory)
535 : zone_(ast_value_factory->zone()) {}
536
537 void set_zone(Zone* zone) { zone_ = zone; }
538
484 PreParserExpression NewStringLiteral(PreParserIdentifier identifier, 539 PreParserExpression NewStringLiteral(PreParserIdentifier identifier,
485 int pos) { 540 int pos) {
486 return PreParserExpression::Default(); 541 // This is needed for object literal property names. Property names are
542 // normalized to string literals during object literal parsing.
543 PreParserExpression expression = PreParserExpression::Default();
544 expression.AddIdentifier(identifier.string_, zone_);
545 return expression;
487 } 546 }
488 PreParserExpression NewNumberLiteral(double number, 547 PreParserExpression NewNumberLiteral(double number,
489 int pos) { 548 int pos) {
490 return PreParserExpression::Default(); 549 return PreParserExpression::Default();
491 } 550 }
492 PreParserExpression NewUndefinedLiteral(int pos) { 551 PreParserExpression NewUndefinedLiteral(int pos) {
493 return PreParserExpression::Default(); 552 return PreParserExpression::Default();
494 } 553 }
495 PreParserExpression NewRegExpLiteral(PreParserIdentifier js_pattern, 554 PreParserExpression NewRegExpLiteral(PreParserIdentifier js_pattern,
496 int js_flags, int literal_index, 555 int js_flags, int literal_index,
497 int pos) { 556 int pos) {
498 return PreParserExpression::Default(); 557 return PreParserExpression::Default();
499 } 558 }
500 PreParserExpression NewArrayLiteral(PreParserExpressionList values, 559 PreParserExpression NewArrayLiteral(PreParserExpressionList values,
501 int first_spread_index, int literal_index, 560 int first_spread_index, int literal_index,
502 int pos) { 561 int pos) {
503 return PreParserExpression::ArrayLiteral(); 562 return PreParserExpression::ArrayLiteral(values.identifiers_);
504 } 563 }
505 PreParserExpression NewClassLiteralProperty(PreParserExpression key, 564 PreParserExpression NewClassLiteralProperty(PreParserExpression key,
506 PreParserExpression value, 565 PreParserExpression value,
507 ClassLiteralProperty::Kind kind, 566 ClassLiteralProperty::Kind kind,
508 bool is_static, 567 bool is_static,
509 bool is_computed_name) { 568 bool is_computed_name) {
510 return PreParserExpression::Default(); 569 return PreParserExpression::Default();
511 } 570 }
512 PreParserExpression NewObjectLiteralProperty(PreParserExpression key, 571 PreParserExpression NewObjectLiteralProperty(PreParserExpression key,
513 PreParserExpression value, 572 PreParserExpression value,
514 ObjectLiteralProperty::Kind kind, 573 ObjectLiteralProperty::Kind kind,
515 bool is_computed_name) { 574 bool is_computed_name) {
516 return PreParserExpression::Default(); 575 return PreParserExpression::Default(value.identifiers_);
517 } 576 }
518 PreParserExpression NewObjectLiteralProperty(PreParserExpression key, 577 PreParserExpression NewObjectLiteralProperty(PreParserExpression key,
519 PreParserExpression value, 578 PreParserExpression value,
520 bool is_computed_name) { 579 bool is_computed_name) {
521 return PreParserExpression::Default(); 580 return PreParserExpression::Default(value.identifiers_);
522 } 581 }
523 PreParserExpression NewObjectLiteral(PreParserExpressionList properties, 582 PreParserExpression NewObjectLiteral(PreParserExpressionList properties,
524 int literal_index, 583 int literal_index,
525 int boilerplate_properties, 584 int boilerplate_properties,
526 int pos) { 585 int pos) {
527 return PreParserExpression::ObjectLiteral(); 586 return PreParserExpression::ObjectLiteral(properties.identifiers_);
528 } 587 }
529 PreParserExpression NewVariableProxy(void* variable) { 588 PreParserExpression NewVariableProxy(void* variable) {
530 return PreParserExpression::Default(); 589 return PreParserExpression::Default();
531 } 590 }
532 PreParserExpression NewProperty(PreParserExpression obj, 591 PreParserExpression NewProperty(PreParserExpression obj,
533 PreParserExpression key, 592 PreParserExpression key,
534 int pos) { 593 int pos) {
535 if (obj.IsThis()) { 594 if (obj.IsThis()) {
536 return PreParserExpression::ThisProperty(); 595 return PreParserExpression::ThisProperty();
537 } 596 }
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
686 return PreParserStatement::Default(); 745 return PreParserStatement::Default();
687 } 746 }
688 747
689 // Return the object itself as AstVisitor and implement the needed 748 // Return the object itself as AstVisitor and implement the needed
690 // dummy method right in this class. 749 // dummy method right in this class.
691 PreParserFactory* visitor() { return this; } 750 PreParserFactory* visitor() { return this; }
692 int* ast_properties() { 751 int* ast_properties() {
693 static int dummy = 42; 752 static int dummy = 42;
694 return &dummy; 753 return &dummy;
695 } 754 }
755
756 private:
757 Zone* zone_;
696 }; 758 };
697 759
698 760
699 struct PreParserFormalParameters : FormalParametersBase { 761 struct PreParserFormalParameters : FormalParametersBase {
700 explicit PreParserFormalParameters(DeclarationScope* scope) 762 explicit PreParserFormalParameters(DeclarationScope* scope)
701 : FormalParametersBase(scope) {} 763 : FormalParametersBase(scope) {}
702 int arity = 0; 764 int arity = 0;
703 765
704 int Arity() const { return arity; } 766 int Arity() const { return arity; }
705 PreParserIdentifier at(int i) { return PreParserIdentifier(); } // Dummy 767 PreParserIdentifier at(int i) { return PreParserIdentifier(); } // Dummy
(...skipping 695 matching lines...) Expand 10 before | Expand all | Expand 10 after
1401 } 1463 }
1402 1464
1403 V8_INLINE void MaterializeUnspreadArgumentsLiterals(int count) { 1465 V8_INLINE void MaterializeUnspreadArgumentsLiterals(int count) {
1404 for (int i = 0; i < count; ++i) { 1466 for (int i = 0; i < count; ++i) {
1405 function_state_->NextMaterializedLiteralIndex(); 1467 function_state_->NextMaterializedLiteralIndex();
1406 } 1468 }
1407 } 1469 }
1408 1470
1409 V8_INLINE PreParserExpression 1471 V8_INLINE PreParserExpression
1410 ExpressionListToExpression(PreParserExpressionList args) { 1472 ExpressionListToExpression(PreParserExpressionList args) {
1411 return PreParserExpression::Default(); 1473 return PreParserExpression::Default(args.identifiers_);
1412 } 1474 }
1413 1475
1414 V8_INLINE void AddAccessorPrefixToFunctionName(bool is_get, 1476 V8_INLINE void AddAccessorPrefixToFunctionName(bool is_get,
1415 PreParserExpression function, 1477 PreParserExpression function,
1416 PreParserIdentifier name) {} 1478 PreParserIdentifier name) {}
1417 V8_INLINE void SetFunctionNameFromPropertyName(PreParserExpression property, 1479 V8_INLINE void SetFunctionNameFromPropertyName(PreParserExpression property,
1418 PreParserIdentifier name) {} 1480 PreParserIdentifier name) {}
1419 V8_INLINE void SetFunctionNameFromIdentifierRef( 1481 V8_INLINE void SetFunctionNameFromIdentifierRef(
1420 PreParserExpression value, PreParserExpression identifier) {} 1482 PreParserExpression value, PreParserExpression identifier) {}
1421 1483
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
1479 function_state_->NextMaterializedLiteralIndex(); 1541 function_state_->NextMaterializedLiteralIndex();
1480 function_state_->NextMaterializedLiteralIndex(); 1542 function_state_->NextMaterializedLiteralIndex();
1481 } 1543 }
1482 return EmptyExpression(); 1544 return EmptyExpression();
1483 } 1545 }
1484 1546
1485 } // namespace internal 1547 } // namespace internal
1486 } // namespace v8 1548 } // namespace v8
1487 1549
1488 #endif // V8_PARSING_PREPARSER_H 1550 #endif // V8_PARSING_PREPARSER_H
OLDNEW
« no previous file with comments | « src/parsing/parser.cc ('k') | src/parsing/preparser.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698