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_AST_AST_H_ | 5 #ifndef V8_AST_AST_H_ |
6 #define V8_AST_AST_H_ | 6 #define V8_AST_AST_H_ |
7 | 7 |
8 #include "src/assembler.h" | 8 #include "src/assembler.h" |
9 #include "src/ast/ast-value-factory.h" | 9 #include "src/ast/ast-value-factory.h" |
10 #include "src/ast/modules.h" | 10 #include "src/ast/modules.h" |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
84 V(CountOperation) \ | 84 V(CountOperation) \ |
85 V(BinaryOperation) \ | 85 V(BinaryOperation) \ |
86 V(CompareOperation) \ | 86 V(CompareOperation) \ |
87 V(Spread) \ | 87 V(Spread) \ |
88 V(ThisFunction) \ | 88 V(ThisFunction) \ |
89 V(SuperPropertyReference) \ | 89 V(SuperPropertyReference) \ |
90 V(SuperCallReference) \ | 90 V(SuperCallReference) \ |
91 V(CaseClause) \ | 91 V(CaseClause) \ |
92 V(EmptyParentheses) \ | 92 V(EmptyParentheses) \ |
93 V(DoExpression) \ | 93 V(DoExpression) \ |
94 V(RewritableAssignmentExpression) | 94 V(RewritableExpression) |
95 | 95 |
96 #define AST_NODE_LIST(V) \ | 96 #define AST_NODE_LIST(V) \ |
97 DECLARATION_NODE_LIST(V) \ | 97 DECLARATION_NODE_LIST(V) \ |
98 STATEMENT_NODE_LIST(V) \ | 98 STATEMENT_NODE_LIST(V) \ |
99 EXPRESSION_NODE_LIST(V) | 99 EXPRESSION_NODE_LIST(V) |
100 | 100 |
101 // Forward declarations | 101 // Forward declarations |
102 class AstNodeFactory; | 102 class AstNodeFactory; |
103 class AstVisitor; | 103 class AstVisitor; |
104 class Declaration; | 104 class Declaration; |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
198 | 198 |
199 #ifdef DEBUG | 199 #ifdef DEBUG |
200 void PrettyPrint(Isolate* isolate); | 200 void PrettyPrint(Isolate* isolate); |
201 void PrettyPrint(); | 201 void PrettyPrint(); |
202 void PrintAst(Isolate* isolate); | 202 void PrintAst(Isolate* isolate); |
203 void PrintAst(); | 203 void PrintAst(); |
204 #endif // DEBUG | 204 #endif // DEBUG |
205 | 205 |
206 // Type testing & conversion functions overridden by concrete subclasses. | 206 // Type testing & conversion functions overridden by concrete subclasses. |
207 #define DECLARE_NODE_FUNCTIONS(type) \ | 207 #define DECLARE_NODE_FUNCTIONS(type) \ |
208 bool Is##type() const { return node_type() == AstNode::k##type; } \ | 208 V8_INLINE bool Is##type() const; \ |
209 type* As##type() { \ | 209 V8_INLINE type* As##type(); \ |
210 return Is##type() ? reinterpret_cast<type*>(this) : NULL; \ | 210 V8_INLINE const type* As##type() const; |
211 } \ | |
212 const type* As##type() const { \ | |
213 return Is##type() ? reinterpret_cast<const type*>(this) : NULL; \ | |
214 } | |
215 AST_NODE_LIST(DECLARE_NODE_FUNCTIONS) | 211 AST_NODE_LIST(DECLARE_NODE_FUNCTIONS) |
216 #undef DECLARE_NODE_FUNCTIONS | 212 #undef DECLARE_NODE_FUNCTIONS |
217 | 213 |
218 virtual BreakableStatement* AsBreakableStatement() { return NULL; } | 214 virtual BreakableStatement* AsBreakableStatement() { return NULL; } |
219 virtual IterationStatement* AsIterationStatement() { return NULL; } | 215 virtual IterationStatement* AsIterationStatement() { return NULL; } |
220 virtual MaterializedLiteral* AsMaterializedLiteral() { return NULL; } | 216 virtual MaterializedLiteral* AsMaterializedLiteral() { return NULL; } |
221 | 217 |
222 // The interface for feedback slots, with default no-op implementations for | 218 // The interface for feedback slots, with default no-op implementations for |
223 // node types which don't actually have this. Note that this is conceptually | 219 // node types which don't actually have this. Note that this is conceptually |
224 // not really nice, but multiple inheritance would introduce yet another | 220 // not really nice, but multiple inheritance would introduce yet another |
(...skipping 2277 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2502 // Expression's trailing 16-bit field. | 2498 // Expression's trailing 16-bit field. |
2503 uint16_t bit_field_; | 2499 uint16_t bit_field_; |
2504 Expression* target_; | 2500 Expression* target_; |
2505 Expression* value_; | 2501 Expression* value_; |
2506 BinaryOperation* binary_operation_; | 2502 BinaryOperation* binary_operation_; |
2507 SmallMapList receiver_types_; | 2503 SmallMapList receiver_types_; |
2508 FeedbackVectorSlot slot_; | 2504 FeedbackVectorSlot slot_; |
2509 }; | 2505 }; |
2510 | 2506 |
2511 | 2507 |
2512 class RewritableAssignmentExpression : public Expression { | 2508 // The RewritableExpression class is a wrapper for AST nodes that wait |
2509 // for some potential rewriting. However, even if such nodes are indeed | |
2510 // rewritten, the RewritableExpression wrapper nodes will survive in the | |
2511 // final AST and should be just ignored, i.e., they should be treated as | |
2512 // equivalent to the wrapped nodes. For this reason and to simplify later | |
2513 // phases, RewritableExpressions are considered as exceptions of AST nodes | |
2514 // in the following sense: | |
2515 // | |
2516 // 1. IsRewritableExpression and AsRewritableExpression behave as usual. | |
2517 // 2. All other Is* and As* methods are practically delegated to the | |
2518 // wrapped node, i.e. IsArrayLiteral() will return true iff the | |
2519 // wrapped node is an array literal. | |
2520 // | |
2521 // Furthermore, an invariant that should be respected is that the wrapped | |
2522 // node is not a RewritableExpression. | |
2523 class RewritableExpression : public Expression { | |
2513 public: | 2524 public: |
2514 DECLARE_NODE_TYPE(RewritableAssignmentExpression) | 2525 DECLARE_NODE_TYPE(RewritableExpression) |
2515 | 2526 |
2516 Expression* expression() { return expr_; } | 2527 Expression* expression() const { return expr_; } |
2517 bool is_rewritten() const { return is_rewritten_; } | 2528 bool is_rewritten() const { return is_rewritten_; } |
2518 | 2529 |
2519 void set_expression(Expression* e) { expr_ = e; } | |
2520 | |
2521 void Rewrite(Expression* new_expression) { | 2530 void Rewrite(Expression* new_expression) { |
2522 DCHECK(!is_rewritten()); | 2531 DCHECK(!is_rewritten()); |
2523 DCHECK_NOT_NULL(new_expression); | 2532 DCHECK_NOT_NULL(new_expression); |
2533 DCHECK(!new_expression->IsRewritableExpression()); | |
2524 expr_ = new_expression; | 2534 expr_ = new_expression; |
2525 is_rewritten_ = true; | 2535 is_rewritten_ = true; |
2526 } | 2536 } |
2527 | 2537 |
2528 static int num_ids() { return parent_num_ids(); } | 2538 static int num_ids() { return parent_num_ids(); } |
2529 | 2539 |
2530 protected: | 2540 protected: |
2531 RewritableAssignmentExpression(Zone* zone, Expression* expression) | 2541 RewritableExpression(Zone* zone, Expression* expression) |
2532 : Expression(zone, expression->position()), | 2542 : Expression(zone, expression->position()), |
2533 is_rewritten_(false), | 2543 is_rewritten_(false), |
2534 expr_(expression) {} | 2544 expr_(expression) { |
2545 DCHECK(!expression->IsRewritableExpression()); | |
2546 } | |
2535 | 2547 |
2536 private: | 2548 private: |
2537 int local_id(int n) const { return base_id() + parent_num_ids() + n; } | 2549 int local_id(int n) const { return base_id() + parent_num_ids() + n; } |
2538 | 2550 |
2539 bool is_rewritten_; | 2551 bool is_rewritten_; |
2540 Expression* expr_; | 2552 Expression* expr_; |
2541 }; | 2553 }; |
2542 | 2554 |
2543 | 2555 |
2544 class Yield final : public Expression { | 2556 class Yield final : public Expression { |
(...skipping 835 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3380 } | 3392 } |
3381 | 3393 |
3382 Conditional* NewConditional(Expression* condition, | 3394 Conditional* NewConditional(Expression* condition, |
3383 Expression* then_expression, | 3395 Expression* then_expression, |
3384 Expression* else_expression, | 3396 Expression* else_expression, |
3385 int position) { | 3397 int position) { |
3386 return new (local_zone_) Conditional( | 3398 return new (local_zone_) Conditional( |
3387 local_zone_, condition, then_expression, else_expression, position); | 3399 local_zone_, condition, then_expression, else_expression, position); |
3388 } | 3400 } |
3389 | 3401 |
3390 RewritableAssignmentExpression* NewRewritableAssignmentExpression( | 3402 RewritableExpression* NewRewritableExpression(Expression* expression) { |
3391 Expression* expression) { | |
3392 DCHECK_NOT_NULL(expression); | 3403 DCHECK_NOT_NULL(expression); |
3393 DCHECK(expression->IsAssignment()); | 3404 return new (local_zone_) RewritableExpression(local_zone_, expression); |
3394 return new (local_zone_) | |
3395 RewritableAssignmentExpression(local_zone_, expression); | |
3396 } | 3405 } |
3397 | 3406 |
3398 Assignment* NewAssignment(Token::Value op, | 3407 Assignment* NewAssignment(Token::Value op, |
3399 Expression* target, | 3408 Expression* target, |
3400 Expression* value, | 3409 Expression* value, |
3401 int pos) { | 3410 int pos) { |
3402 DCHECK(Token::IsAssignmentOp(op)); | 3411 DCHECK(Token::IsAssignmentOp(op)); |
3403 Assignment* assign = | 3412 Assignment* assign = |
3404 new (local_zone_) Assignment(local_zone_, op, target, value, pos); | 3413 new (local_zone_) Assignment(local_zone_, op, target, value, pos); |
3405 if (assign->is_compound()) { | 3414 if (assign->is_compound()) { |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3508 // inspected. | 3517 // inspected. |
3509 // See ParseFunctionLiteral in parser.cc for preconditions. | 3518 // See ParseFunctionLiteral in parser.cc for preconditions. |
3510 Zone* local_zone_; | 3519 Zone* local_zone_; |
3511 // ZoneObjects which need to persist until scope analysis must be allocated in | 3520 // ZoneObjects which need to persist until scope analysis must be allocated in |
3512 // the parser-level zone. | 3521 // the parser-level zone. |
3513 Zone* parser_zone_; | 3522 Zone* parser_zone_; |
3514 AstValueFactory* ast_value_factory_; | 3523 AstValueFactory* ast_value_factory_; |
3515 }; | 3524 }; |
3516 | 3525 |
3517 | 3526 |
3527 // Type testing & conversion functions overridden by concrete subclasses. | |
3528 // Inline functions for AstNode. | |
3529 | |
3530 #define DECLARE_NODE_FUNCTIONS(type) \ | |
3531 bool AstNode::Is##type() const { \ | |
3532 NodeType mine = node_type(); \ | |
3533 if (mine == AstNode::kRewritableExpression && \ | |
3534 AstNode::k##type != AstNode::kRewritableExpression) \ | |
rossberg
2016/02/18 12:11:31
I assume compilers are smart enough to fold away t
nickie
2016/02/18 12:53:17
Yes, I also assumed this. If we have reason to be
| |
3535 mine = reinterpret_cast<const RewritableExpression*>(this) \ | |
3536 ->expression() \ | |
3537 ->node_type(); \ | |
3538 return mine == AstNode::k##type; \ | |
3539 } \ | |
3540 type* AstNode::As##type() { \ | |
3541 NodeType mine = node_type(); \ | |
3542 AstNode* result = this; \ | |
3543 if (mine == AstNode::kRewritableExpression && \ | |
3544 AstNode::k##type != AstNode::kRewritableExpression) { \ | |
3545 result = \ | |
3546 reinterpret_cast<const RewritableExpression*>(this)->expression(); \ | |
rossberg
2016/02/18 12:11:32
I'm surprised C++ does not barf on the cast in lin
nickie
2016/02/18 12:53:17
I don't see why it should, but anyway. The const
| |
3547 mine = result->node_type(); \ | |
3548 } \ | |
3549 return mine == AstNode::k##type ? reinterpret_cast<type*>(result) : NULL; \ | |
3550 } \ | |
3551 const type* AstNode::As##type() const { \ | |
3552 NodeType mine = node_type(); \ | |
3553 const AstNode* result = this; \ | |
3554 if (mine == AstNode::kRewritableExpression && \ | |
3555 AstNode::k##type != AstNode::kRewritableExpression) { \ | |
3556 result = \ | |
3557 reinterpret_cast<const RewritableExpression*>(this)->expression(); \ | |
3558 mine = result->node_type(); \ | |
3559 } \ | |
3560 return mine == AstNode::k##type ? reinterpret_cast<const type*>(result) \ | |
3561 : NULL; \ | |
3562 } | |
3563 AST_NODE_LIST(DECLARE_NODE_FUNCTIONS) | |
3564 #undef DECLARE_NODE_FUNCTIONS | |
3565 | |
3566 | |
3518 } // namespace internal | 3567 } // namespace internal |
3519 } // namespace v8 | 3568 } // namespace v8 |
3520 | 3569 |
3521 #endif // V8_AST_AST_H_ | 3570 #endif // V8_AST_AST_H_ |
OLD | NEW |