OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 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 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 | 111 |
112 // Typedef only introduced to avoid unreadable code. | 112 // Typedef only introduced to avoid unreadable code. |
113 // Please do appreciate the required space in "> >". | 113 // Please do appreciate the required space in "> >". |
114 typedef ZoneList<Handle<String> > ZoneStringList; | 114 typedef ZoneList<Handle<String> > ZoneStringList; |
115 typedef ZoneList<Handle<Object> > ZoneObjectList; | 115 typedef ZoneList<Handle<Object> > ZoneObjectList; |
116 | 116 |
117 | 117 |
118 #define DECLARE_NODE_TYPE(type) \ | 118 #define DECLARE_NODE_TYPE(type) \ |
119 virtual void Accept(AstVisitor* v); \ | 119 virtual void Accept(AstVisitor* v); \ |
120 virtual AstNode::Type node_type() const { return AstNode::k##type; } \ | 120 virtual AstNode::Type node_type() const { return AstNode::k##type; } \ |
121 virtual type* As##type() { return this; } | |
122 | 121 |
123 | 122 |
124 class AstNode: public ZoneObject { | 123 class AstNode: public ZoneObject { |
125 public: | 124 public: |
126 #define DECLARE_TYPE_ENUM(type) k##type, | 125 #define DECLARE_TYPE_ENUM(type) k##type, |
127 enum Type { | 126 enum Type { |
128 AST_NODE_LIST(DECLARE_TYPE_ENUM) | 127 AST_NODE_LIST(DECLARE_TYPE_ENUM) |
129 kInvalid = -1 | 128 kInvalid = -1 |
130 }; | 129 }; |
131 #undef DECLARE_TYPE_ENUM | 130 #undef DECLARE_TYPE_ENUM |
(...skipping 14 matching lines...) Expand all Loading... |
146 | 145 |
147 AstNode() {} | 146 AstNode() {} |
148 | 147 |
149 virtual ~AstNode() { } | 148 virtual ~AstNode() { } |
150 | 149 |
151 virtual void Accept(AstVisitor* v) = 0; | 150 virtual void Accept(AstVisitor* v) = 0; |
152 virtual Type node_type() const { return kInvalid; } | 151 virtual Type node_type() const { return kInvalid; } |
153 | 152 |
154 // Type testing & conversion functions overridden by concrete subclasses. | 153 // Type testing & conversion functions overridden by concrete subclasses. |
155 #define DECLARE_NODE_FUNCTIONS(type) \ | 154 #define DECLARE_NODE_FUNCTIONS(type) \ |
156 virtual type* As##type() { return NULL; } | 155 bool Is##type() { return node_type() == AstNode::k##type; } \ |
| 156 type* As##type() { return Is##type() ? reinterpret_cast<type*>(this) : NULL; } |
157 AST_NODE_LIST(DECLARE_NODE_FUNCTIONS) | 157 AST_NODE_LIST(DECLARE_NODE_FUNCTIONS) |
158 #undef DECLARE_NODE_FUNCTIONS | 158 #undef DECLARE_NODE_FUNCTIONS |
159 | 159 |
160 virtual Statement* AsStatement() { return NULL; } | 160 virtual Statement* AsStatement() { return NULL; } |
161 virtual Expression* AsExpression() { return NULL; } | 161 virtual Expression* AsExpression() { return NULL; } |
162 virtual TargetCollector* AsTargetCollector() { return NULL; } | 162 virtual TargetCollector* AsTargetCollector() { return NULL; } |
163 virtual BreakableStatement* AsBreakableStatement() { return NULL; } | 163 virtual BreakableStatement* AsBreakableStatement() { return NULL; } |
164 virtual IterationStatement* AsIterationStatement() { return NULL; } | 164 virtual IterationStatement* AsIterationStatement() { return NULL; } |
165 virtual MaterializedLiteral* AsMaterializedLiteral() { return NULL; } | 165 virtual MaterializedLiteral* AsMaterializedLiteral() { return NULL; } |
166 | 166 |
(...skipping 22 matching lines...) Expand all Loading... |
189 friend class CaseClause; // Generates AST IDs. | 189 friend class CaseClause; // Generates AST IDs. |
190 }; | 190 }; |
191 | 191 |
192 | 192 |
193 class Statement: public AstNode { | 193 class Statement: public AstNode { |
194 public: | 194 public: |
195 Statement() : statement_pos_(RelocInfo::kNoPosition) {} | 195 Statement() : statement_pos_(RelocInfo::kNoPosition) {} |
196 | 196 |
197 virtual Statement* AsStatement() { return this; } | 197 virtual Statement* AsStatement() { return this; } |
198 | 198 |
199 virtual Assignment* StatementAsSimpleAssignment() { return NULL; } | |
200 virtual CountOperation* StatementAsCountOperation() { return NULL; } | |
201 | |
202 bool IsEmpty() { return AsEmptyStatement() != NULL; } | 199 bool IsEmpty() { return AsEmptyStatement() != NULL; } |
203 | 200 |
204 void set_statement_pos(int statement_pos) { statement_pos_ = statement_pos; } | 201 void set_statement_pos(int statement_pos) { statement_pos_ = statement_pos; } |
205 int statement_pos() const { return statement_pos_; } | 202 int statement_pos() const { return statement_pos_; } |
206 | 203 |
207 private: | 204 private: |
208 int statement_pos_; | 205 int statement_pos_; |
209 }; | 206 }; |
210 | 207 |
211 | 208 |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
257 : id_(GetNextId(isolate)), | 254 : id_(GetNextId(isolate)), |
258 test_id_(GetNextId(isolate)) {} | 255 test_id_(GetNextId(isolate)) {} |
259 | 256 |
260 virtual int position() const { | 257 virtual int position() const { |
261 UNREACHABLE(); | 258 UNREACHABLE(); |
262 return 0; | 259 return 0; |
263 } | 260 } |
264 | 261 |
265 virtual Expression* AsExpression() { return this; } | 262 virtual Expression* AsExpression() { return this; } |
266 | 263 |
267 virtual bool IsTrivial() { return false; } | |
268 virtual bool IsValidLeftHandSide() { return false; } | 264 virtual bool IsValidLeftHandSide() { return false; } |
269 | 265 |
270 // Helpers for ToBoolean conversion. | 266 // Helpers for ToBoolean conversion. |
271 virtual bool ToBooleanIsTrue() { return false; } | 267 virtual bool ToBooleanIsTrue() { return false; } |
272 virtual bool ToBooleanIsFalse() { return false; } | 268 virtual bool ToBooleanIsFalse() { return false; } |
273 | 269 |
274 // Symbols that cannot be parsed as array indices are considered property | 270 // Symbols that cannot be parsed as array indices are considered property |
275 // names. We do not treat symbols that can be array indexes as property | 271 // names. We do not treat symbols that can be array indexes as property |
276 // names because [] for string objects is handled only by keyed ICs. | 272 // names because [] for string objects is handled only by keyed ICs. |
277 virtual bool IsPropertyName() { return false; } | 273 virtual bool IsPropertyName() { return false; } |
278 | 274 |
279 // Mark the expression as being compiled as an expression | |
280 // statement. This is used to transform postfix increments to | |
281 // (faster) prefix increments. | |
282 virtual void MarkAsStatement() { /* do nothing */ } | |
283 | |
284 // True iff the result can be safely overwritten (to avoid allocation). | 275 // True iff the result can be safely overwritten (to avoid allocation). |
285 // False for operations that can return one of their operands. | 276 // False for operations that can return one of their operands. |
286 virtual bool ResultOverwriteAllowed() { return false; } | 277 virtual bool ResultOverwriteAllowed() { return false; } |
287 | 278 |
288 // True iff the expression is a literal represented as a smi. | 279 // True iff the expression is a literal represented as a smi. |
289 virtual bool IsSmiLiteral() { return false; } | 280 bool IsSmiLiteral(); |
290 | 281 |
291 // True iff the expression is a string literal. | 282 // True iff the expression is a string literal. |
292 virtual bool IsStringLiteral() { return false; } | 283 bool IsStringLiteral(); |
293 | 284 |
294 // True iff the expression is the null literal. | 285 // True iff the expression is the null literal. |
295 virtual bool IsNullLiteral() { return false; } | 286 bool IsNullLiteral(); |
296 | 287 |
297 // Type feedback information for assignments and properties. | 288 // Type feedback information for assignments and properties. |
298 virtual bool IsMonomorphic() { | 289 virtual bool IsMonomorphic() { |
299 UNREACHABLE(); | 290 UNREACHABLE(); |
300 return false; | 291 return false; |
301 } | 292 } |
302 virtual bool IsArrayLength() { | |
303 UNREACHABLE(); | |
304 return false; | |
305 } | |
306 virtual SmallMapList* GetReceiverTypes() { | 293 virtual SmallMapList* GetReceiverTypes() { |
307 UNREACHABLE(); | 294 UNREACHABLE(); |
308 return NULL; | 295 return NULL; |
309 } | 296 } |
310 Handle<Map> GetMonomorphicReceiverType() { | 297 Handle<Map> GetMonomorphicReceiverType() { |
311 ASSERT(IsMonomorphic()); | 298 ASSERT(IsMonomorphic()); |
312 SmallMapList* types = GetReceiverTypes(); | 299 SmallMapList* types = GetReceiverTypes(); |
313 ASSERT(types != NULL && types->length() == 1); | 300 ASSERT(types != NULL && types->length() == 1); |
314 return types->at(0); | 301 return types->at(0); |
315 } | 302 } |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
361 | 348 |
362 class Block: public BreakableStatement { | 349 class Block: public BreakableStatement { |
363 public: | 350 public: |
364 inline Block(Isolate* isolate, | 351 inline Block(Isolate* isolate, |
365 ZoneStringList* labels, | 352 ZoneStringList* labels, |
366 int capacity, | 353 int capacity, |
367 bool is_initializer_block); | 354 bool is_initializer_block); |
368 | 355 |
369 DECLARE_NODE_TYPE(Block) | 356 DECLARE_NODE_TYPE(Block) |
370 | 357 |
371 virtual Assignment* StatementAsSimpleAssignment() { | |
372 if (statements_.length() != 1) return NULL; | |
373 return statements_[0]->StatementAsSimpleAssignment(); | |
374 } | |
375 | |
376 virtual CountOperation* StatementAsCountOperation() { | |
377 if (statements_.length() != 1) return NULL; | |
378 return statements_[0]->StatementAsCountOperation(); | |
379 } | |
380 | |
381 virtual bool IsInlineable() const; | 358 virtual bool IsInlineable() const; |
382 | 359 |
383 void AddStatement(Statement* statement) { statements_.Add(statement); } | 360 void AddStatement(Statement* statement) { statements_.Add(statement); } |
384 | 361 |
385 ZoneList<Statement*>* statements() { return &statements_; } | 362 ZoneList<Statement*>* statements() { return &statements_; } |
386 bool is_initializer_block() const { return is_initializer_block_; } | 363 bool is_initializer_block() const { return is_initializer_block_; } |
387 | 364 |
388 Scope* block_scope() const { return block_scope_; } | 365 Scope* block_scope() const { return block_scope_; } |
389 void set_block_scope(Scope* block_scope) { block_scope_ = block_scope; } | 366 void set_block_scope(Scope* block_scope) { block_scope_ = block_scope; } |
390 | 367 |
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
605 | 582 |
606 class ExpressionStatement: public Statement { | 583 class ExpressionStatement: public Statement { |
607 public: | 584 public: |
608 explicit ExpressionStatement(Expression* expression) | 585 explicit ExpressionStatement(Expression* expression) |
609 : expression_(expression) { } | 586 : expression_(expression) { } |
610 | 587 |
611 DECLARE_NODE_TYPE(ExpressionStatement) | 588 DECLARE_NODE_TYPE(ExpressionStatement) |
612 | 589 |
613 virtual bool IsInlineable() const; | 590 virtual bool IsInlineable() const; |
614 | 591 |
615 virtual Assignment* StatementAsSimpleAssignment(); | |
616 virtual CountOperation* StatementAsCountOperation(); | |
617 | |
618 void set_expression(Expression* e) { expression_ = e; } | 592 void set_expression(Expression* e) { expression_ = e; } |
619 Expression* expression() const { return expression_; } | 593 Expression* expression() const { return expression_; } |
620 | 594 |
621 private: | 595 private: |
622 Expression* expression_; | 596 Expression* expression_; |
623 }; | 597 }; |
624 | 598 |
625 | 599 |
626 class ContinueStatement: public Statement { | 600 class ContinueStatement: public Statement { |
627 public: | 601 public: |
(...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
888 }; | 862 }; |
889 | 863 |
890 | 864 |
891 class Literal: public Expression { | 865 class Literal: public Expression { |
892 public: | 866 public: |
893 Literal(Isolate* isolate, Handle<Object> handle) | 867 Literal(Isolate* isolate, Handle<Object> handle) |
894 : Expression(isolate), handle_(handle) { } | 868 : Expression(isolate), handle_(handle) { } |
895 | 869 |
896 DECLARE_NODE_TYPE(Literal) | 870 DECLARE_NODE_TYPE(Literal) |
897 | 871 |
898 virtual bool IsTrivial() { return true; } | |
899 virtual bool IsSmiLiteral() { return handle_->IsSmi(); } | |
900 virtual bool IsStringLiteral() { return handle_->IsString(); } | |
901 virtual bool IsNullLiteral() { return handle_->IsNull(); } | |
902 | |
903 // Check if this literal is identical to the other literal. | 872 // Check if this literal is identical to the other literal. |
904 bool IsIdenticalTo(const Literal* other) const { | 873 bool IsIdenticalTo(const Literal* other) const { |
905 return handle_.is_identical_to(other->handle_); | 874 return handle_.is_identical_to(other->handle_); |
906 } | 875 } |
907 | 876 |
908 virtual bool IsPropertyName() { | 877 virtual bool IsPropertyName() { |
909 if (handle_->IsSymbol()) { | 878 if (handle_->IsSymbol()) { |
910 uint32_t ignored; | 879 uint32_t ignored; |
911 return !String::cast(*handle_)->AsArrayIndex(&ignored); | 880 return !String::cast(*handle_)->AsArrayIndex(&ignored); |
912 } | 881 } |
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1107 class VariableProxy: public Expression { | 1076 class VariableProxy: public Expression { |
1108 public: | 1077 public: |
1109 VariableProxy(Isolate* isolate, Variable* var); | 1078 VariableProxy(Isolate* isolate, Variable* var); |
1110 | 1079 |
1111 DECLARE_NODE_TYPE(VariableProxy) | 1080 DECLARE_NODE_TYPE(VariableProxy) |
1112 | 1081 |
1113 virtual bool IsValidLeftHandSide() { | 1082 virtual bool IsValidLeftHandSide() { |
1114 return var_ == NULL ? true : var_->IsValidLeftHandSide(); | 1083 return var_ == NULL ? true : var_->IsValidLeftHandSide(); |
1115 } | 1084 } |
1116 | 1085 |
1117 virtual bool IsTrivial() { | |
1118 // Reading from a mutable variable is a side effect, but the | |
1119 // variable for 'this' is immutable. | |
1120 return is_this_ || is_trivial_; | |
1121 } | |
1122 | |
1123 virtual bool IsInlineable() const; | 1086 virtual bool IsInlineable() const; |
1124 | 1087 |
1125 bool IsVariable(Handle<String> n) { | 1088 bool IsVariable(Handle<String> n) { |
1126 return !is_this() && name().is_identical_to(n); | 1089 return !is_this() && name().is_identical_to(n); |
1127 } | 1090 } |
1128 | 1091 |
1129 bool IsArguments() { return var_ != NULL && var_->is_arguments(); } | 1092 bool IsArguments() { return var_ != NULL && var_->is_arguments(); } |
1130 | 1093 |
1131 Handle<String> name() const { return name_; } | 1094 Handle<String> name() const { return name_; } |
1132 Variable* var() const { return var_; } | 1095 Variable* var() const { return var_; } |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1180 virtual int position() const { return pos_; } | 1143 virtual int position() const { return pos_; } |
1181 | 1144 |
1182 bool IsStringLength() const { return is_string_length_; } | 1145 bool IsStringLength() const { return is_string_length_; } |
1183 bool IsStringAccess() const { return is_string_access_; } | 1146 bool IsStringAccess() const { return is_string_access_; } |
1184 bool IsFunctionPrototype() const { return is_function_prototype_; } | 1147 bool IsFunctionPrototype() const { return is_function_prototype_; } |
1185 | 1148 |
1186 // Type feedback information. | 1149 // Type feedback information. |
1187 void RecordTypeFeedback(TypeFeedbackOracle* oracle); | 1150 void RecordTypeFeedback(TypeFeedbackOracle* oracle); |
1188 virtual bool IsMonomorphic() { return is_monomorphic_; } | 1151 virtual bool IsMonomorphic() { return is_monomorphic_; } |
1189 virtual SmallMapList* GetReceiverTypes() { return &receiver_types_; } | 1152 virtual SmallMapList* GetReceiverTypes() { return &receiver_types_; } |
1190 virtual bool IsArrayLength() { return is_array_length_; } | 1153 bool IsArrayLength() { return is_array_length_; } |
1191 | 1154 |
1192 private: | 1155 private: |
1193 Expression* obj_; | 1156 Expression* obj_; |
1194 Expression* key_; | 1157 Expression* key_; |
1195 int pos_; | 1158 int pos_; |
1196 | 1159 |
1197 SmallMapList receiver_types_; | 1160 SmallMapList receiver_types_; |
1198 bool is_monomorphic_ : 1; | 1161 bool is_monomorphic_ : 1; |
1199 bool is_array_length_ : 1; | 1162 bool is_array_length_ : 1; |
1200 bool is_string_length_ : 1; | 1163 bool is_string_length_ : 1; |
(...skipping 923 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2124 | 2087 |
2125 private: | 2088 private: |
2126 Isolate* isolate_; | 2089 Isolate* isolate_; |
2127 bool stack_overflow_; | 2090 bool stack_overflow_; |
2128 }; | 2091 }; |
2129 | 2092 |
2130 | 2093 |
2131 } } // namespace v8::internal | 2094 } } // namespace v8::internal |
2132 | 2095 |
2133 #endif // V8_AST_H_ | 2096 #endif // V8_AST_H_ |
OLD | NEW |