OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 | 52 |
53 // Nodes are allocated in a separate zone, which allows faster | 53 // Nodes are allocated in a separate zone, which allows faster |
54 // allocation and constant-time deallocation of the entire syntax | 54 // allocation and constant-time deallocation of the entire syntax |
55 // tree. | 55 // tree. |
56 | 56 |
57 | 57 |
58 // ---------------------------------------------------------------------------- | 58 // ---------------------------------------------------------------------------- |
59 // Nodes of the abstract syntax tree. Only concrete classes are | 59 // Nodes of the abstract syntax tree. Only concrete classes are |
60 // enumerated here. | 60 // enumerated here. |
61 | 61 |
| 62 #define DECLARATION_NODE_LIST(V) \ |
| 63 V(VariableDeclaration) \ |
| 64 |
62 #define STATEMENT_NODE_LIST(V) \ | 65 #define STATEMENT_NODE_LIST(V) \ |
63 V(Block) \ | 66 V(Block) \ |
64 V(ExpressionStatement) \ | 67 V(ExpressionStatement) \ |
65 V(EmptyStatement) \ | 68 V(EmptyStatement) \ |
66 V(IfStatement) \ | 69 V(IfStatement) \ |
67 V(ContinueStatement) \ | 70 V(ContinueStatement) \ |
68 V(BreakStatement) \ | 71 V(BreakStatement) \ |
69 V(ReturnStatement) \ | 72 V(ReturnStatement) \ |
70 V(WithStatement) \ | 73 V(WithStatement) \ |
71 V(SwitchStatement) \ | 74 V(SwitchStatement) \ |
(...skipping 20 matching lines...) Expand all Loading... |
92 V(Call) \ | 95 V(Call) \ |
93 V(CallNew) \ | 96 V(CallNew) \ |
94 V(CallRuntime) \ | 97 V(CallRuntime) \ |
95 V(UnaryOperation) \ | 98 V(UnaryOperation) \ |
96 V(CountOperation) \ | 99 V(CountOperation) \ |
97 V(BinaryOperation) \ | 100 V(BinaryOperation) \ |
98 V(CompareOperation) \ | 101 V(CompareOperation) \ |
99 V(ThisFunction) | 102 V(ThisFunction) |
100 | 103 |
101 #define AST_NODE_LIST(V) \ | 104 #define AST_NODE_LIST(V) \ |
102 V(Declaration) \ | 105 DECLARATION_NODE_LIST(V) \ |
103 STATEMENT_NODE_LIST(V) \ | 106 STATEMENT_NODE_LIST(V) \ |
104 EXPRESSION_NODE_LIST(V) | 107 EXPRESSION_NODE_LIST(V) |
105 | 108 |
106 // Forward declarations | 109 // Forward declarations |
107 class AstConstructionVisitor; | 110 class AstConstructionVisitor; |
108 template<class> class AstNodeFactory; | 111 template<class> class AstNodeFactory; |
109 class AstVisitor; | 112 class AstVisitor; |
| 113 class Declaration; |
110 class BreakableStatement; | 114 class BreakableStatement; |
111 class Expression; | 115 class Expression; |
112 class IterationStatement; | 116 class IterationStatement; |
113 class MaterializedLiteral; | 117 class MaterializedLiteral; |
114 class Statement; | 118 class Statement; |
115 class TargetCollector; | 119 class TargetCollector; |
116 class TypeFeedbackOracle; | 120 class TypeFeedbackOracle; |
117 | 121 |
118 class RegExpAlternative; | 122 class RegExpAlternative; |
119 class RegExpAssertion; | 123 class RegExpAssertion; |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 virtual void Accept(AstVisitor* v) = 0; | 199 virtual void Accept(AstVisitor* v) = 0; |
196 virtual Type node_type() const { return kInvalid; } | 200 virtual Type node_type() const { return kInvalid; } |
197 | 201 |
198 // Type testing & conversion functions overridden by concrete subclasses. | 202 // Type testing & conversion functions overridden by concrete subclasses. |
199 #define DECLARE_NODE_FUNCTIONS(type) \ | 203 #define DECLARE_NODE_FUNCTIONS(type) \ |
200 bool Is##type() { return node_type() == AstNode::k##type; } \ | 204 bool Is##type() { return node_type() == AstNode::k##type; } \ |
201 type* As##type() { return Is##type() ? reinterpret_cast<type*>(this) : NULL; } | 205 type* As##type() { return Is##type() ? reinterpret_cast<type*>(this) : NULL; } |
202 AST_NODE_LIST(DECLARE_NODE_FUNCTIONS) | 206 AST_NODE_LIST(DECLARE_NODE_FUNCTIONS) |
203 #undef DECLARE_NODE_FUNCTIONS | 207 #undef DECLARE_NODE_FUNCTIONS |
204 | 208 |
| 209 virtual Declaration* AsDeclaration() { return NULL; } |
205 virtual Statement* AsStatement() { return NULL; } | 210 virtual Statement* AsStatement() { return NULL; } |
206 virtual Expression* AsExpression() { return NULL; } | 211 virtual Expression* AsExpression() { return NULL; } |
207 virtual TargetCollector* AsTargetCollector() { return NULL; } | 212 virtual TargetCollector* AsTargetCollector() { return NULL; } |
208 virtual BreakableStatement* AsBreakableStatement() { return NULL; } | 213 virtual BreakableStatement* AsBreakableStatement() { return NULL; } |
209 virtual IterationStatement* AsIterationStatement() { return NULL; } | 214 virtual IterationStatement* AsIterationStatement() { return NULL; } |
210 virtual MaterializedLiteral* AsMaterializedLiteral() { return NULL; } | 215 virtual MaterializedLiteral* AsMaterializedLiteral() { return NULL; } |
211 | 216 |
212 static void ResetIds() { Isolate::Current()->set_ast_node_id(0); } | 217 static void ResetIds() { Isolate::Current()->set_ast_node_id(0); } |
213 | 218 |
214 protected: | 219 protected: |
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
426 | 431 |
427 private: | 432 private: |
428 ZoneList<Statement*> statements_; | 433 ZoneList<Statement*> statements_; |
429 bool is_initializer_block_; | 434 bool is_initializer_block_; |
430 Scope* block_scope_; | 435 Scope* block_scope_; |
431 }; | 436 }; |
432 | 437 |
433 | 438 |
434 class Declaration: public AstNode { | 439 class Declaration: public AstNode { |
435 public: | 440 public: |
436 DECLARE_NODE_TYPE(Declaration) | |
437 | |
438 VariableProxy* proxy() const { return proxy_; } | 441 VariableProxy* proxy() const { return proxy_; } |
439 VariableMode mode() const { return mode_; } | 442 VariableMode mode() const { return mode_; } |
440 FunctionLiteral* fun() const { return fun_; } // may be NULL | |
441 bool IsInlineable() const; | |
442 Scope* scope() const { return scope_; } | 443 Scope* scope() const { return scope_; } |
| 444 virtual bool IsInlineable() const; |
| 445 |
| 446 virtual Declaration* AsDeclaration() { return this; } |
| 447 virtual VariableDeclaration* AsVariableDeclaration() { return NULL; } |
443 | 448 |
444 protected: | 449 protected: |
445 template<class> friend class AstNodeFactory; | |
446 | |
447 Declaration(VariableProxy* proxy, | 450 Declaration(VariableProxy* proxy, |
448 VariableMode mode, | 451 VariableMode mode, |
449 FunctionLiteral* fun, | |
450 Scope* scope) | 452 Scope* scope) |
451 : proxy_(proxy), | 453 : proxy_(proxy), |
452 mode_(mode), | 454 mode_(mode), |
453 fun_(fun), | |
454 scope_(scope) { | 455 scope_(scope) { |
455 ASSERT(mode == VAR || | 456 ASSERT(mode == VAR || |
456 mode == CONST || | 457 mode == CONST || |
457 mode == CONST_HARMONY || | 458 mode == CONST_HARMONY || |
458 mode == LET); | 459 mode == LET); |
459 // At the moment there are no "const functions"'s in JavaScript... | |
460 ASSERT(fun == NULL || mode == VAR || mode == LET); | |
461 } | 460 } |
462 | 461 |
463 private: | 462 private: |
464 VariableProxy* proxy_; | 463 VariableProxy* proxy_; |
465 VariableMode mode_; | 464 VariableMode mode_; |
466 FunctionLiteral* fun_; | |
467 | 465 |
468 // Nested scope from which the declaration originated. | 466 // Nested scope from which the declaration originated. |
469 Scope* scope_; | 467 Scope* scope_; |
470 }; | 468 }; |
471 | 469 |
472 | 470 |
| 471 class VariableDeclaration: public Declaration { |
| 472 public: |
| 473 DECLARE_NODE_TYPE(VariableDeclaration) |
| 474 |
| 475 virtual VariableDeclaration* AsVariableDeclaration() { return this; } |
| 476 |
| 477 FunctionLiteral* fun() const { return fun_; } // may be NULL |
| 478 virtual bool IsInlineable() const; |
| 479 |
| 480 protected: |
| 481 template<class> friend class AstNodeFactory; |
| 482 |
| 483 VariableDeclaration(VariableProxy* proxy, |
| 484 VariableMode mode, |
| 485 FunctionLiteral* fun, |
| 486 Scope* scope) |
| 487 : Declaration(proxy, mode, scope), |
| 488 fun_(fun) { |
| 489 // At the moment there are no "const functions"'s in JavaScript... |
| 490 ASSERT(fun == NULL || mode == VAR || mode == LET); |
| 491 } |
| 492 |
| 493 private: |
| 494 FunctionLiteral* fun_; |
| 495 }; |
| 496 |
| 497 |
473 class IterationStatement: public BreakableStatement { | 498 class IterationStatement: public BreakableStatement { |
474 public: | 499 public: |
475 // Type testing & conversion. | 500 // Type testing & conversion. |
476 virtual IterationStatement* AsIterationStatement() { return this; } | 501 virtual IterationStatement* AsIterationStatement() { return this; } |
477 | 502 |
478 Statement* body() const { return body_; } | 503 Statement* body() const { return body_; } |
479 | 504 |
480 // Bailout support. | 505 // Bailout support. |
481 int OsrEntryId() const { return osr_entry_id_; } | 506 int OsrEntryId() const { return osr_entry_id_; } |
482 virtual int ContinueId() const = 0; | 507 virtual int ContinueId() const = 0; |
(...skipping 1878 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2361 explicit AstNodeFactory(Isolate* isolate) | 2386 explicit AstNodeFactory(Isolate* isolate) |
2362 : isolate_(isolate), | 2387 : isolate_(isolate), |
2363 zone_(isolate_->zone()) { } | 2388 zone_(isolate_->zone()) { } |
2364 | 2389 |
2365 Visitor* visitor() { return &visitor_; } | 2390 Visitor* visitor() { return &visitor_; } |
2366 | 2391 |
2367 #define VISIT_AND_RETURN(NodeType, node) \ | 2392 #define VISIT_AND_RETURN(NodeType, node) \ |
2368 visitor_.Visit##NodeType((node)); \ | 2393 visitor_.Visit##NodeType((node)); \ |
2369 return node; | 2394 return node; |
2370 | 2395 |
| 2396 VariableDeclaration* NewVariableDeclaration(VariableProxy* proxy, |
| 2397 VariableMode mode, |
| 2398 FunctionLiteral* fun, |
| 2399 Scope* scope) { |
| 2400 VariableDeclaration* decl = |
| 2401 new(zone_) VariableDeclaration(proxy, mode, fun, scope); |
| 2402 VISIT_AND_RETURN(VariableDeclaration, decl) |
| 2403 } |
| 2404 |
2371 Block* NewBlock(ZoneStringList* labels, | 2405 Block* NewBlock(ZoneStringList* labels, |
2372 int capacity, | 2406 int capacity, |
2373 bool is_initializer_block) { | 2407 bool is_initializer_block) { |
2374 Block* block = new(zone_) Block( | 2408 Block* block = new(zone_) Block( |
2375 isolate_, labels, capacity, is_initializer_block); | 2409 isolate_, labels, capacity, is_initializer_block); |
2376 VISIT_AND_RETURN(Block, block) | 2410 VISIT_AND_RETURN(Block, block) |
2377 } | 2411 } |
2378 | 2412 |
2379 Declaration* NewDeclaration(VariableProxy* proxy, | |
2380 VariableMode mode, | |
2381 FunctionLiteral* fun, | |
2382 Scope* scope) { | |
2383 Declaration* decl = new(zone_) Declaration(proxy, mode, fun, scope); | |
2384 VISIT_AND_RETURN(Declaration, decl) | |
2385 } | |
2386 | |
2387 #define STATEMENT_WITH_LABELS(NodeType) \ | 2413 #define STATEMENT_WITH_LABELS(NodeType) \ |
2388 NodeType* New##NodeType(ZoneStringList* labels) { \ | 2414 NodeType* New##NodeType(ZoneStringList* labels) { \ |
2389 NodeType* stmt = new(zone_) NodeType(isolate_, labels); \ | 2415 NodeType* stmt = new(zone_) NodeType(isolate_, labels); \ |
2390 VISIT_AND_RETURN(NodeType, stmt); \ | 2416 VISIT_AND_RETURN(NodeType, stmt); \ |
2391 } | 2417 } |
2392 STATEMENT_WITH_LABELS(DoWhileStatement) | 2418 STATEMENT_WITH_LABELS(DoWhileStatement) |
2393 STATEMENT_WITH_LABELS(WhileStatement) | 2419 STATEMENT_WITH_LABELS(WhileStatement) |
2394 STATEMENT_WITH_LABELS(ForStatement) | 2420 STATEMENT_WITH_LABELS(ForStatement) |
2395 STATEMENT_WITH_LABELS(ForInStatement) | 2421 STATEMENT_WITH_LABELS(ForInStatement) |
2396 STATEMENT_WITH_LABELS(SwitchStatement) | 2422 STATEMENT_WITH_LABELS(SwitchStatement) |
(...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2648 private: | 2674 private: |
2649 Isolate* isolate_; | 2675 Isolate* isolate_; |
2650 Zone* zone_; | 2676 Zone* zone_; |
2651 Visitor visitor_; | 2677 Visitor visitor_; |
2652 }; | 2678 }; |
2653 | 2679 |
2654 | 2680 |
2655 } } // namespace v8::internal | 2681 } } // namespace v8::internal |
2656 | 2682 |
2657 #endif // V8_AST_H_ | 2683 #endif // V8_AST_H_ |
OLD | NEW |