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_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 Loading... | |
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 Loading... | |
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 Loading... | |
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 PreParserExpression expression = PreParserExpression::Default(); |
542 expression.AddIdentifier(identifier.string_, zone_); | |
adamk
2016/10/07 18:35:55
Which case is this needed by? Does this have to do
marja
2016/10/07 19:00:27
Going to add a comment:
This is needed for object
marja
2016/10/10 08:53:22
Done.
| |
543 return expression; | |
487 } | 544 } |
488 PreParserExpression NewNumberLiteral(double number, | 545 PreParserExpression NewNumberLiteral(double number, |
489 int pos) { | 546 int pos) { |
490 return PreParserExpression::Default(); | 547 return PreParserExpression::Default(); |
491 } | 548 } |
492 PreParserExpression NewUndefinedLiteral(int pos) { | 549 PreParserExpression NewUndefinedLiteral(int pos) { |
493 return PreParserExpression::Default(); | 550 return PreParserExpression::Default(); |
494 } | 551 } |
495 PreParserExpression NewRegExpLiteral(PreParserIdentifier js_pattern, | 552 PreParserExpression NewRegExpLiteral(PreParserIdentifier js_pattern, |
496 int js_flags, int literal_index, | 553 int js_flags, int literal_index, |
497 int pos) { | 554 int pos) { |
498 return PreParserExpression::Default(); | 555 return PreParserExpression::Default(); |
499 } | 556 } |
500 PreParserExpression NewArrayLiteral(PreParserExpressionList values, | 557 PreParserExpression NewArrayLiteral(PreParserExpressionList values, |
501 int first_spread_index, int literal_index, | 558 int first_spread_index, int literal_index, |
502 int pos) { | 559 int pos) { |
503 return PreParserExpression::ArrayLiteral(); | 560 return PreParserExpression::ArrayLiteral(values.identifiers_); |
504 } | 561 } |
505 PreParserExpression NewClassLiteralProperty(PreParserExpression key, | 562 PreParserExpression NewClassLiteralProperty(PreParserExpression key, |
506 PreParserExpression value, | 563 PreParserExpression value, |
507 ClassLiteralProperty::Kind kind, | 564 ClassLiteralProperty::Kind kind, |
508 bool is_static, | 565 bool is_static, |
509 bool is_computed_name) { | 566 bool is_computed_name) { |
510 return PreParserExpression::Default(); | 567 return PreParserExpression::Default(); |
511 } | 568 } |
512 PreParserExpression NewObjectLiteralProperty(PreParserExpression key, | 569 PreParserExpression NewObjectLiteralProperty(PreParserExpression key, |
513 PreParserExpression value, | 570 PreParserExpression value, |
514 ObjectLiteralProperty::Kind kind, | 571 ObjectLiteralProperty::Kind kind, |
515 bool is_computed_name) { | 572 bool is_computed_name) { |
516 return PreParserExpression::Default(); | 573 return PreParserExpression::Default(value.identifiers_); |
517 } | 574 } |
518 PreParserExpression NewObjectLiteralProperty(PreParserExpression key, | 575 PreParserExpression NewObjectLiteralProperty(PreParserExpression key, |
519 PreParserExpression value, | 576 PreParserExpression value, |
520 bool is_computed_name) { | 577 bool is_computed_name) { |
521 return PreParserExpression::Default(); | 578 return PreParserExpression::Default(value.identifiers_); |
522 } | 579 } |
523 PreParserExpression NewObjectLiteral(PreParserExpressionList properties, | 580 PreParserExpression NewObjectLiteral(PreParserExpressionList properties, |
524 int literal_index, | 581 int literal_index, |
525 int boilerplate_properties, | 582 int boilerplate_properties, |
526 int pos) { | 583 int pos) { |
527 return PreParserExpression::ObjectLiteral(); | 584 return PreParserExpression::ObjectLiteral(properties.identifiers_); |
528 } | 585 } |
529 PreParserExpression NewVariableProxy(void* variable) { | 586 PreParserExpression NewVariableProxy(void* variable) { |
530 return PreParserExpression::Default(); | 587 return PreParserExpression::Default(); |
531 } | 588 } |
532 PreParserExpression NewProperty(PreParserExpression obj, | 589 PreParserExpression NewProperty(PreParserExpression obj, |
533 PreParserExpression key, | 590 PreParserExpression key, |
534 int pos) { | 591 int pos) { |
535 if (obj.IsThis()) { | 592 if (obj.IsThis()) { |
536 return PreParserExpression::ThisProperty(); | 593 return PreParserExpression::ThisProperty(); |
537 } | 594 } |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
686 return PreParserStatement::Default(); | 743 return PreParserStatement::Default(); |
687 } | 744 } |
688 | 745 |
689 // Return the object itself as AstVisitor and implement the needed | 746 // Return the object itself as AstVisitor and implement the needed |
690 // dummy method right in this class. | 747 // dummy method right in this class. |
691 PreParserFactory* visitor() { return this; } | 748 PreParserFactory* visitor() { return this; } |
692 int* ast_properties() { | 749 int* ast_properties() { |
693 static int dummy = 42; | 750 static int dummy = 42; |
694 return &dummy; | 751 return &dummy; |
695 } | 752 } |
753 | |
754 private: | |
755 Zone* zone_; | |
696 }; | 756 }; |
697 | 757 |
698 | 758 |
699 struct PreParserFormalParameters : FormalParametersBase { | 759 struct PreParserFormalParameters : FormalParametersBase { |
700 explicit PreParserFormalParameters(DeclarationScope* scope) | 760 explicit PreParserFormalParameters(DeclarationScope* scope) |
701 : FormalParametersBase(scope) {} | 761 : FormalParametersBase(scope) {} |
702 int arity = 0; | 762 int arity = 0; |
703 | 763 |
704 int Arity() const { return arity; } | 764 int Arity() const { return arity; } |
705 PreParserIdentifier at(int i) { return PreParserIdentifier(); } // Dummy | 765 PreParserIdentifier at(int i) { return PreParserIdentifier(); } // Dummy |
(...skipping 695 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1401 } | 1461 } |
1402 | 1462 |
1403 V8_INLINE void MaterializeUnspreadArgumentsLiterals(int count) { | 1463 V8_INLINE void MaterializeUnspreadArgumentsLiterals(int count) { |
1404 for (int i = 0; i < count; ++i) { | 1464 for (int i = 0; i < count; ++i) { |
1405 function_state_->NextMaterializedLiteralIndex(); | 1465 function_state_->NextMaterializedLiteralIndex(); |
1406 } | 1466 } |
1407 } | 1467 } |
1408 | 1468 |
1409 V8_INLINE PreParserExpression | 1469 V8_INLINE PreParserExpression |
1410 ExpressionListToExpression(PreParserExpressionList args) { | 1470 ExpressionListToExpression(PreParserExpressionList args) { |
1411 return PreParserExpression::Default(); | 1471 return PreParserExpression::Default(args.identifiers_); |
1412 } | 1472 } |
1413 | 1473 |
1414 V8_INLINE void AddAccessorPrefixToFunctionName(bool is_get, | 1474 V8_INLINE void AddAccessorPrefixToFunctionName(bool is_get, |
1415 PreParserExpression function, | 1475 PreParserExpression function, |
1416 PreParserIdentifier name) {} | 1476 PreParserIdentifier name) {} |
1417 V8_INLINE void SetFunctionNameFromPropertyName(PreParserExpression property, | 1477 V8_INLINE void SetFunctionNameFromPropertyName(PreParserExpression property, |
1418 PreParserIdentifier name) {} | 1478 PreParserIdentifier name) {} |
1419 V8_INLINE void SetFunctionNameFromIdentifierRef( | 1479 V8_INLINE void SetFunctionNameFromIdentifierRef( |
1420 PreParserExpression value, PreParserExpression identifier) {} | 1480 PreParserExpression value, PreParserExpression identifier) {} |
1421 | 1481 |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1479 function_state_->NextMaterializedLiteralIndex(); | 1539 function_state_->NextMaterializedLiteralIndex(); |
1480 function_state_->NextMaterializedLiteralIndex(); | 1540 function_state_->NextMaterializedLiteralIndex(); |
1481 } | 1541 } |
1482 return EmptyExpression(); | 1542 return EmptyExpression(); |
1483 } | 1543 } |
1484 | 1544 |
1485 } // namespace internal | 1545 } // namespace internal |
1486 } // namespace v8 | 1546 } // namespace v8 |
1487 | 1547 |
1488 #endif // V8_PARSING_PREPARSER_H | 1548 #endif // V8_PARSING_PREPARSER_H |
OLD | NEW |