OLD | NEW |
1 // Copyright 2010 the V8 project authors. All rights reserved. | 1 // Copyright 2010 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 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 virtual Expression* AsExpression() { return NULL; } | 130 virtual Expression* AsExpression() { return NULL; } |
131 virtual Literal* AsLiteral() { return NULL; } | 131 virtual Literal* AsLiteral() { return NULL; } |
132 virtual Slot* AsSlot() { return NULL; } | 132 virtual Slot* AsSlot() { return NULL; } |
133 virtual VariableProxy* AsVariableProxy() { return NULL; } | 133 virtual VariableProxy* AsVariableProxy() { return NULL; } |
134 virtual Property* AsProperty() { return NULL; } | 134 virtual Property* AsProperty() { return NULL; } |
135 virtual Call* AsCall() { return NULL; } | 135 virtual Call* AsCall() { return NULL; } |
136 virtual TargetCollector* AsTargetCollector() { return NULL; } | 136 virtual TargetCollector* AsTargetCollector() { return NULL; } |
137 virtual BreakableStatement* AsBreakableStatement() { return NULL; } | 137 virtual BreakableStatement* AsBreakableStatement() { return NULL; } |
138 virtual IterationStatement* AsIterationStatement() { return NULL; } | 138 virtual IterationStatement* AsIterationStatement() { return NULL; } |
139 virtual UnaryOperation* AsUnaryOperation() { return NULL; } | 139 virtual UnaryOperation* AsUnaryOperation() { return NULL; } |
140 virtual CountOperation* AsCountOperation() { return NULL; } | |
141 virtual BinaryOperation* AsBinaryOperation() { return NULL; } | 140 virtual BinaryOperation* AsBinaryOperation() { return NULL; } |
142 virtual Assignment* AsAssignment() { return NULL; } | 141 virtual Assignment* AsAssignment() { return NULL; } |
143 virtual FunctionLiteral* AsFunctionLiteral() { return NULL; } | 142 virtual FunctionLiteral* AsFunctionLiteral() { return NULL; } |
144 virtual MaterializedLiteral* AsMaterializedLiteral() { return NULL; } | 143 virtual MaterializedLiteral* AsMaterializedLiteral() { return NULL; } |
145 virtual ObjectLiteral* AsObjectLiteral() { return NULL; } | 144 virtual ObjectLiteral* AsObjectLiteral() { return NULL; } |
146 virtual ArrayLiteral* AsArrayLiteral() { return NULL; } | 145 virtual ArrayLiteral* AsArrayLiteral() { return NULL; } |
147 virtual CompareOperation* AsCompareOperation() { return NULL; } | 146 virtual CompareOperation* AsCompareOperation() { return NULL; } |
148 | 147 |
149 int num() { return num_; } | 148 int num() { return num_; } |
150 void set_num(int n) { num_ = n; } | 149 void set_num(int n) { num_ = n; } |
151 | 150 |
152 private: | 151 private: |
153 // Support for ast node numbering. | 152 // Support for ast node numbering. |
154 int num_; | 153 int num_; |
155 }; | 154 }; |
156 | 155 |
157 | 156 |
158 class Statement: public AstNode { | 157 class Statement: public AstNode { |
159 public: | 158 public: |
160 Statement() : statement_pos_(RelocInfo::kNoPosition) {} | 159 Statement() : statement_pos_(RelocInfo::kNoPosition) {} |
161 | 160 |
162 virtual Statement* AsStatement() { return this; } | 161 virtual Statement* AsStatement() { return this; } |
163 virtual ReturnStatement* AsReturnStatement() { return NULL; } | 162 virtual ReturnStatement* AsReturnStatement() { return NULL; } |
164 | 163 |
165 virtual Assignment* StatementAsSimpleAssignment() { return NULL; } | |
166 virtual CountOperation* StatementAsCountOperation() { return NULL; } | |
167 | |
168 bool IsEmpty() { return AsEmptyStatement() != NULL; } | 164 bool IsEmpty() { return AsEmptyStatement() != NULL; } |
169 | 165 |
170 void set_statement_pos(int statement_pos) { statement_pos_ = statement_pos; } | 166 void set_statement_pos(int statement_pos) { statement_pos_ = statement_pos; } |
171 int statement_pos() const { return statement_pos_; } | 167 int statement_pos() const { return statement_pos_; } |
172 | 168 |
173 private: | 169 private: |
174 int statement_pos_; | 170 int statement_pos_; |
175 }; | 171 }; |
176 | 172 |
177 | 173 |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
318 | 314 |
319 class Block: public BreakableStatement { | 315 class Block: public BreakableStatement { |
320 public: | 316 public: |
321 Block(ZoneStringList* labels, int capacity, bool is_initializer_block) | 317 Block(ZoneStringList* labels, int capacity, bool is_initializer_block) |
322 : BreakableStatement(labels, TARGET_FOR_NAMED_ONLY), | 318 : BreakableStatement(labels, TARGET_FOR_NAMED_ONLY), |
323 statements_(capacity), | 319 statements_(capacity), |
324 is_initializer_block_(is_initializer_block) { } | 320 is_initializer_block_(is_initializer_block) { } |
325 | 321 |
326 virtual void Accept(AstVisitor* v); | 322 virtual void Accept(AstVisitor* v); |
327 | 323 |
328 virtual Assignment* StatementAsSimpleAssignment() { | |
329 if (statements_.length() != 1) return NULL; | |
330 return statements_[0]->StatementAsSimpleAssignment(); | |
331 } | |
332 | |
333 virtual CountOperation* StatementAsCountOperation() { | |
334 if (statements_.length() != 1) return NULL; | |
335 return statements_[0]->StatementAsCountOperation(); | |
336 } | |
337 | |
338 void AddStatement(Statement* statement) { statements_.Add(statement); } | 324 void AddStatement(Statement* statement) { statements_.Add(statement); } |
339 | 325 |
340 ZoneList<Statement*>* statements() { return &statements_; } | 326 ZoneList<Statement*>* statements() { return &statements_; } |
341 bool is_initializer_block() const { return is_initializer_block_; } | 327 bool is_initializer_block() const { return is_initializer_block_; } |
342 | 328 |
343 private: | 329 private: |
344 ZoneList<Statement*> statements_; | 330 ZoneList<Statement*> statements_; |
345 bool is_initializer_block_; | 331 bool is_initializer_block_; |
346 }; | 332 }; |
347 | 333 |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
449 }; | 435 }; |
450 | 436 |
451 | 437 |
452 class ForStatement: public IterationStatement { | 438 class ForStatement: public IterationStatement { |
453 public: | 439 public: |
454 explicit ForStatement(ZoneStringList* labels) | 440 explicit ForStatement(ZoneStringList* labels) |
455 : IterationStatement(labels), | 441 : IterationStatement(labels), |
456 init_(NULL), | 442 init_(NULL), |
457 cond_(NULL), | 443 cond_(NULL), |
458 next_(NULL), | 444 next_(NULL), |
459 may_have_function_literal_(true), | 445 may_have_function_literal_(true) { |
460 loop_variable_(NULL) {} | 446 } |
461 | 447 |
462 void Initialize(Statement* init, | 448 void Initialize(Statement* init, |
463 Expression* cond, | 449 Expression* cond, |
464 Statement* next, | 450 Statement* next, |
465 Statement* body) { | 451 Statement* body) { |
466 IterationStatement::Initialize(body); | 452 IterationStatement::Initialize(body); |
467 init_ = init; | 453 init_ = init; |
468 cond_ = cond; | 454 cond_ = cond; |
469 next_ = next; | 455 next_ = next; |
470 } | 456 } |
471 | 457 |
472 virtual void Accept(AstVisitor* v); | 458 virtual void Accept(AstVisitor* v); |
473 | 459 |
474 Statement* init() const { return init_; } | 460 Statement* init() const { return init_; } |
475 Expression* cond() const { return cond_; } | 461 Expression* cond() const { return cond_; } |
476 Statement* next() const { return next_; } | 462 Statement* next() const { return next_; } |
477 bool may_have_function_literal() const { | 463 bool may_have_function_literal() const { |
478 return may_have_function_literal_; | 464 return may_have_function_literal_; |
479 } | 465 } |
480 | 466 |
481 bool is_fast_smi_loop() { return loop_variable_ != NULL; } | |
482 Variable* loop_variable() { return loop_variable_; } | |
483 void set_loop_variable(Variable* var) { loop_variable_ = var; } | |
484 | |
485 private: | 467 private: |
486 Statement* init_; | 468 Statement* init_; |
487 Expression* cond_; | 469 Expression* cond_; |
488 Statement* next_; | 470 Statement* next_; |
489 // True if there is a function literal subexpression in the condition. | 471 // True if there is a function literal subexpression in the condition. |
490 bool may_have_function_literal_; | 472 bool may_have_function_literal_; |
491 Variable* loop_variable_; | |
492 | 473 |
493 friend class AstOptimizer; | 474 friend class AstOptimizer; |
494 }; | 475 }; |
495 | 476 |
496 | 477 |
497 class ForInStatement: public IterationStatement { | 478 class ForInStatement: public IterationStatement { |
498 public: | 479 public: |
499 explicit ForInStatement(ZoneStringList* labels) | 480 explicit ForInStatement(ZoneStringList* labels) |
500 : IterationStatement(labels), each_(NULL), enumerable_(NULL) { } | 481 : IterationStatement(labels), each_(NULL), enumerable_(NULL) { } |
501 | 482 |
(...skipping 17 matching lines...) Expand all Loading... |
519 class ExpressionStatement: public Statement { | 500 class ExpressionStatement: public Statement { |
520 public: | 501 public: |
521 explicit ExpressionStatement(Expression* expression) | 502 explicit ExpressionStatement(Expression* expression) |
522 : expression_(expression) { } | 503 : expression_(expression) { } |
523 | 504 |
524 virtual void Accept(AstVisitor* v); | 505 virtual void Accept(AstVisitor* v); |
525 | 506 |
526 // Type testing & conversion. | 507 // Type testing & conversion. |
527 virtual ExpressionStatement* AsExpressionStatement() { return this; } | 508 virtual ExpressionStatement* AsExpressionStatement() { return this; } |
528 | 509 |
529 virtual Assignment* StatementAsSimpleAssignment(); | |
530 virtual CountOperation* StatementAsCountOperation(); | |
531 | |
532 void set_expression(Expression* e) { expression_ = e; } | 510 void set_expression(Expression* e) { expression_ = e; } |
533 Expression* expression() { return expression_; } | 511 Expression* expression() { return expression_; } |
534 | 512 |
535 private: | 513 private: |
536 Expression* expression_; | 514 Expression* expression_; |
537 }; | 515 }; |
538 | 516 |
539 | 517 |
540 class ContinueStatement: public Statement { | 518 class ContinueStatement: public Statement { |
541 public: | 519 public: |
(...skipping 437 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
979 return var_ == NULL ? true : var_->IsValidLeftHandSide(); | 957 return var_ == NULL ? true : var_->IsValidLeftHandSide(); |
980 } | 958 } |
981 | 959 |
982 virtual bool IsLeaf() { | 960 virtual bool IsLeaf() { |
983 ASSERT(var_ != NULL); // Variable must be resolved. | 961 ASSERT(var_ != NULL); // Variable must be resolved. |
984 return var()->is_global() || var()->rewrite()->IsLeaf(); | 962 return var()->is_global() || var()->rewrite()->IsLeaf(); |
985 } | 963 } |
986 | 964 |
987 // Reading from a mutable variable is a side effect, but 'this' is | 965 // Reading from a mutable variable is a side effect, but 'this' is |
988 // immutable. | 966 // immutable. |
989 virtual bool IsTrivial() { return is_trivial_; } | 967 virtual bool IsTrivial() { return is_this(); } |
990 | 968 |
991 bool IsVariable(Handle<String> n) { | 969 bool IsVariable(Handle<String> n) { |
992 return !is_this() && name().is_identical_to(n); | 970 return !is_this() && name().is_identical_to(n); |
993 } | 971 } |
994 | 972 |
995 bool IsArguments() { | 973 bool IsArguments() { |
996 Variable* variable = AsVariable(); | 974 Variable* variable = AsVariable(); |
997 return (variable == NULL) ? false : variable->is_arguments(); | 975 return (variable == NULL) ? false : variable->is_arguments(); |
998 } | 976 } |
999 | 977 |
1000 Handle<String> name() const { return name_; } | 978 Handle<String> name() const { return name_; } |
1001 Variable* var() const { return var_; } | 979 Variable* var() const { return var_; } |
1002 bool is_this() const { return is_this_; } | 980 bool is_this() const { return is_this_; } |
1003 bool inside_with() const { return inside_with_; } | 981 bool inside_with() const { return inside_with_; } |
1004 bool is_trivial() { return is_trivial_; } | |
1005 void set_is_trivial(bool b) { is_trivial_ = b; } | |
1006 | 982 |
1007 // Bind this proxy to the variable var. | 983 // Bind this proxy to the variable var. |
1008 void BindTo(Variable* var); | 984 void BindTo(Variable* var); |
1009 | 985 |
1010 protected: | 986 protected: |
1011 Handle<String> name_; | 987 Handle<String> name_; |
1012 Variable* var_; // resolved variable, or NULL | 988 Variable* var_; // resolved variable, or NULL |
1013 bool is_this_; | 989 bool is_this_; |
1014 bool inside_with_; | 990 bool inside_with_; |
1015 bool is_trivial_; | |
1016 | 991 |
1017 VariableProxy(Handle<String> name, bool is_this, bool inside_with); | 992 VariableProxy(Handle<String> name, bool is_this, bool inside_with); |
1018 explicit VariableProxy(bool is_this); | 993 explicit VariableProxy(bool is_this); |
1019 | 994 |
1020 friend class Scope; | 995 friend class Scope; |
1021 }; | 996 }; |
1022 | 997 |
1023 | 998 |
1024 class VariableProxySentinel: public VariableProxy { | 999 class VariableProxySentinel: public VariableProxy { |
1025 public: | 1000 public: |
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1264 | 1239 |
1265 class CountOperation: public Expression { | 1240 class CountOperation: public Expression { |
1266 public: | 1241 public: |
1267 CountOperation(bool is_prefix, Token::Value op, Expression* expression) | 1242 CountOperation(bool is_prefix, Token::Value op, Expression* expression) |
1268 : is_prefix_(is_prefix), op_(op), expression_(expression) { | 1243 : is_prefix_(is_prefix), op_(op), expression_(expression) { |
1269 ASSERT(Token::IsCountOp(op)); | 1244 ASSERT(Token::IsCountOp(op)); |
1270 } | 1245 } |
1271 | 1246 |
1272 virtual void Accept(AstVisitor* v); | 1247 virtual void Accept(AstVisitor* v); |
1273 | 1248 |
1274 virtual CountOperation* AsCountOperation() { return this; } | |
1275 | |
1276 bool is_prefix() const { return is_prefix_; } | 1249 bool is_prefix() const { return is_prefix_; } |
1277 bool is_postfix() const { return !is_prefix_; } | 1250 bool is_postfix() const { return !is_prefix_; } |
1278 Token::Value op() const { return op_; } | 1251 Token::Value op() const { return op_; } |
1279 Token::Value binary_op() { | 1252 Token::Value binary_op() { |
1280 return op_ == Token::INC ? Token::ADD : Token::SUB; | 1253 return op_ == Token::INC ? Token::ADD : Token::SUB; |
1281 } | 1254 } |
1282 Expression* expression() const { return expression_; } | 1255 Expression* expression() const { return expression_; } |
1283 | 1256 |
1284 virtual void MarkAsStatement() { is_prefix_ = true; } | 1257 virtual void MarkAsStatement() { is_prefix_ = true; } |
1285 | 1258 |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1344 public: | 1317 public: |
1345 Assignment(Token::Value op, Expression* target, Expression* value, int pos) | 1318 Assignment(Token::Value op, Expression* target, Expression* value, int pos) |
1346 : op_(op), target_(target), value_(value), pos_(pos), | 1319 : op_(op), target_(target), value_(value), pos_(pos), |
1347 block_start_(false), block_end_(false) { | 1320 block_start_(false), block_end_(false) { |
1348 ASSERT(Token::IsAssignmentOp(op)); | 1321 ASSERT(Token::IsAssignmentOp(op)); |
1349 } | 1322 } |
1350 | 1323 |
1351 virtual void Accept(AstVisitor* v); | 1324 virtual void Accept(AstVisitor* v); |
1352 virtual Assignment* AsAssignment() { return this; } | 1325 virtual Assignment* AsAssignment() { return this; } |
1353 | 1326 |
1354 Assignment* AsSimpleAssignment() { return !is_compound() ? this : NULL; } | |
1355 | |
1356 Token::Value binary_op() const; | 1327 Token::Value binary_op() const; |
1357 | 1328 |
1358 Token::Value op() const { return op_; } | 1329 Token::Value op() const { return op_; } |
1359 Expression* target() const { return target_; } | 1330 Expression* target() const { return target_; } |
1360 Expression* value() const { return value_; } | 1331 Expression* value() const { return value_; } |
1361 int position() { return pos_; } | 1332 int position() { return pos_; } |
1362 // This check relies on the definition order of token in token.h. | 1333 // This check relies on the definition order of token in token.h. |
1363 bool is_compound() const { return op() > Token::ASSIGN; } | 1334 bool is_compound() const { return op() > Token::ASSIGN; } |
1364 | 1335 |
1365 // An initialization block is a series of statments of the form | 1336 // An initialization block is a series of statments of the form |
(...skipping 539 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1905 #undef DEF_VISIT | 1876 #undef DEF_VISIT |
1906 | 1877 |
1907 private: | 1878 private: |
1908 bool stack_overflow_; | 1879 bool stack_overflow_; |
1909 }; | 1880 }; |
1910 | 1881 |
1911 | 1882 |
1912 } } // namespace v8::internal | 1883 } } // namespace v8::internal |
1913 | 1884 |
1914 #endif // V8_AST_H_ | 1885 #endif // V8_AST_H_ |
OLD | NEW |