ast.h 65.4 KB
Newer Older
1
// Copyright 2011 the V8 project authors. All rights reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#ifndef V8_AST_H_
#define V8_AST_H_

31
#include "allocation.h"
32 33
#include "execution.h"
#include "factory.h"
34
#include "jsregexp.h"
35
#include "runtime.h"
36
#include "small-pointer-list.h"
37 38 39
#include "token.h"
#include "variables.h"

40 41
namespace v8 {
namespace internal {
42 43 44 45 46 47 48 49 50 51 52 53 54 55

// The abstract syntax tree is an intermediate, light-weight
// representation of the parsed JavaScript code suitable for
// compilation to native code.

// Nodes are allocated in a separate zone, which allows faster
// allocation and constant-time deallocation of the entire syntax
// tree.


// ----------------------------------------------------------------------------
// Nodes of the abstract syntax tree. Only concrete classes are
// enumerated here.

56
#define STATEMENT_NODE_LIST(V)                  \
57 58 59 60 61 62 63
  V(Block)                                      \
  V(ExpressionStatement)                        \
  V(EmptyStatement)                             \
  V(IfStatement)                                \
  V(ContinueStatement)                          \
  V(BreakStatement)                             \
  V(ReturnStatement)                            \
64
  V(WithStatement)                              \
65
  V(ExitContextStatement)                       \
66
  V(SwitchStatement)                            \
67 68 69
  V(DoWhileStatement)                           \
  V(WhileStatement)                             \
  V(ForStatement)                               \
70
  V(ForInStatement)                             \
71 72
  V(TryCatchStatement)                          \
  V(TryFinallyStatement)                        \
73 74 75
  V(DebuggerStatement)

#define EXPRESSION_NODE_LIST(V)                 \
76
  V(FunctionLiteral)                            \
77
  V(SharedFunctionInfoLiteral)                  \
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
  V(Conditional)                                \
  V(VariableProxy)                              \
  V(Literal)                                    \
  V(RegExpLiteral)                              \
  V(ObjectLiteral)                              \
  V(ArrayLiteral)                               \
  V(Assignment)                                 \
  V(Throw)                                      \
  V(Property)                                   \
  V(Call)                                       \
  V(CallNew)                                    \
  V(CallRuntime)                                \
  V(UnaryOperation)                             \
  V(CountOperation)                             \
  V(BinaryOperation)                            \
  V(CompareOperation)                           \
94
  V(CompareToNull)                              \
95 96
  V(ThisFunction)

97 98 99 100
#define AST_NODE_LIST(V)                        \
  V(Declaration)                                \
  STATEMENT_NODE_LIST(V)                        \
  EXPRESSION_NODE_LIST(V)
101

102
// Forward declarations
103
class BitVector;
104 105 106 107
class DefinitionInfo;
class MaterializedLiteral;
class TargetCollector;
class TypeFeedbackOracle;
108

109
#define DEF_FORWARD_DECLARATION(type) class type;
110
AST_NODE_LIST(DEF_FORWARD_DECLARATION)
111 112 113 114 115 116
#undef DEF_FORWARD_DECLARATION


// Typedef only introduced to avoid unreadable code.
// Please do appreciate the required space in "> >".
typedef ZoneList<Handle<String> > ZoneStringList;
117
typedef ZoneList<Handle<Object> > ZoneObjectList;
118 119


120 121 122 123 124 125
#define DECLARE_NODE_TYPE(type)                                         \
  virtual void Accept(AstVisitor* v);                                   \
  virtual AstNode::Type node_type() const { return AstNode::k##type; }  \
  virtual type* As##type() { return this; }


126
class AstNode: public ZoneObject {
127
 public:
128 129 130 131 132 133 134
#define DECLARE_TYPE_ENUM(type) k##type,
  enum Type {
    AST_NODE_LIST(DECLARE_TYPE_ENUM)
    kInvalid = -1
  };
#undef DECLARE_TYPE_ENUM

135
  static const int kNoNumber = -1;
136
  static const int kFunctionEntryId = 2;  // Using 0 could disguise errors.
137

138 139 140
  // Override ZoneObject's new to count allocated AST nodes.
  void* operator new(size_t size, Zone* zone) {
    Isolate* isolate = zone->isolate();
141
    isolate->set_ast_node_count(isolate->ast_node_count() + 1);
vitalyr@chromium.org's avatar
vitalyr@chromium.org committed
142
    return zone->New(static_cast<int>(size));
143
  }
144

145 146
  AstNode() {}

147
  virtual ~AstNode() { }
148

149
  virtual void Accept(AstVisitor* v) = 0;
150 151 152 153 154 155 156
  virtual Type node_type() const { return kInvalid; }

  // Type testing & conversion functions overridden by concrete subclasses.
#define DECLARE_NODE_FUNCTIONS(type)                  \
  virtual type* As##type() { return NULL; }
  AST_NODE_LIST(DECLARE_NODE_FUNCTIONS)
#undef DECLARE_NODE_FUNCTIONS
157 158 159

  virtual Statement* AsStatement() { return NULL; }
  virtual Expression* AsExpression() { return NULL; }
160
  virtual TargetCollector* AsTargetCollector() { return NULL; }
161 162
  virtual BreakableStatement* AsBreakableStatement() { return NULL; }
  virtual IterationStatement* AsIterationStatement() { return NULL; }
163
  virtual MaterializedLiteral* AsMaterializedLiteral() { return NULL; }
164 165 166
  virtual Slot* AsSlot() { return NULL; }

  // True if the node is simple enough for us to inline calls containing it.
167
  virtual bool IsInlineable() const = 0;
168

169 170
  static int Count() { return Isolate::Current()->ast_node_count(); }
  static void ResetIds() { Isolate::Current()->set_ast_node_id(0); }
171 172

 protected:
173 174 175 176 177
  static unsigned GetNextId(Isolate* isolate) {
    return ReserveIdRange(isolate, 1);
  }

  static unsigned ReserveIdRange(Isolate* isolate, int n) {
178 179
    unsigned tmp = isolate->ast_node_id();
    isolate->set_ast_node_id(tmp + n);
180 181 182
    return tmp;
  }

183 184 185 186 187
 private:
  // Hidden to prevent accidental usage. It would have to load the
  // current zone from the TLS.
  void* operator new(size_t size);

188
  friend class CaseClause;  // Generates AST IDs.
189 190 191
};


192
class Statement: public AstNode {
193
 public:
194 195
  Statement() : statement_pos_(RelocInfo::kNoPosition) {}

196 197
  virtual Statement* AsStatement()  { return this; }

198 199 200
  virtual Assignment* StatementAsSimpleAssignment() { return NULL; }
  virtual CountOperation* StatementAsCountOperation() { return NULL; }

201
  bool IsEmpty() { return AsEmptyStatement() != NULL; }
202 203 204 205 206 207

  void set_statement_pos(int statement_pos) { statement_pos_ = statement_pos; }
  int statement_pos() const { return statement_pos_; }

 private:
  int statement_pos_;
208 209 210
};


211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
class SmallMapList {
 public:
  SmallMapList() {}
  explicit SmallMapList(int capacity) : list_(capacity) {}

  void Reserve(int capacity) { list_.Reserve(capacity); }
  void Clear() { list_.Clear(); }

  bool is_empty() const { return list_.is_empty(); }
  int length() const { return list_.length(); }

  void Add(Handle<Map> handle) {
    list_.Add(handle.location());
  }

  Handle<Map> at(int i) const {
    return Handle<Map>(list_.at(i));
  }

  Handle<Map> first() const { return at(0); }
  Handle<Map> last() const { return at(length() - 1); }

 private:
  // The list stores pointers to Map*, that is Map**, so it's GC safe.
  SmallPointerList<Map*> list_;

  DISALLOW_COPY_AND_ASSIGN(SmallMapList);
};


241
class Expression: public AstNode {
242
 public:
243 244 245 246 247 248 249 250 251 252 253 254
  enum Context {
    // Not assigned a context yet, or else will not be visited during
    // code generation.
    kUninitialized,
    // Evaluated for its side effects.
    kEffect,
    // Evaluated for its value (and side effects).
    kValue,
    // Evaluated for control flow (and side effects).
    kTest
  };

255 256 257
  explicit Expression(Isolate* isolate)
      : id_(GetNextId(isolate)),
        test_id_(GetNextId(isolate)) {}
258

259 260 261 262 263
  virtual int position() const {
    UNREACHABLE();
    return 0;
  }

264 265
  virtual Expression* AsExpression()  { return this; }

266
  virtual bool IsTrivial() { return false; }
267 268
  virtual bool IsValidLeftHandSide() { return false; }

269 270 271 272
  // Helpers for ToBoolean conversion.
  virtual bool ToBooleanIsTrue() { return false; }
  virtual bool ToBooleanIsFalse() { return false; }

273 274 275 276 277
  // Symbols that cannot be parsed as array indices are considered property
  // names.  We do not treat symbols that can be array indexes as property
  // names because [] for string objects is handled only by keyed ICs.
  virtual bool IsPropertyName() { return false; }

278 279 280 281
  // Mark the expression as being compiled as an expression
  // statement. This is used to transform postfix increments to
  // (faster) prefix increments.
  virtual void MarkAsStatement() { /* do nothing */ }
282

283 284 285 286
  // True iff the result can be safely overwritten (to avoid allocation).
  // False for operations that can return one of their operands.
  virtual bool ResultOverwriteAllowed() { return false; }

287 288 289
  // True iff the expression is a literal represented as a smi.
  virtual bool IsSmiLiteral() { return false; }

290 291 292 293 294 295 296 297 298
  // Type feedback information for assignments and properties.
  virtual bool IsMonomorphic() {
    UNREACHABLE();
    return false;
  }
  virtual bool IsArrayLength() {
    UNREACHABLE();
    return false;
  }
299
  virtual SmallMapList* GetReceiverTypes() {
300 301 302
    UNREACHABLE();
    return NULL;
  }
303 304 305 306 307
  Handle<Map> GetMonomorphicReceiverType() {
    ASSERT(IsMonomorphic());
    SmallMapList* types = GetReceiverTypes();
    ASSERT(types != NULL && types->length() == 1);
    return types->at(0);
308 309
  }

310
  unsigned id() const { return id_; }
311
  unsigned test_id() const { return test_id_; }
312

313
 private:
314
  unsigned id_;
315
  unsigned test_id_;
316 317 318 319 320 321 322 323 324 325
};


/**
 * A sentinel used during pre parsing that represents some expression
 * that is a valid left hand side without having to actually build
 * the expression.
 */
class ValidLeftHandSideSentinel: public Expression {
 public:
326
  explicit ValidLeftHandSideSentinel(Isolate* isolate) : Expression(isolate) {}
327
  virtual bool IsValidLeftHandSide() { return true; }
328
  virtual void Accept(AstVisitor* v) { UNREACHABLE(); }
329
  virtual bool IsInlineable() const;
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
};


class BreakableStatement: public Statement {
 public:
  enum Type {
    TARGET_FOR_ANONYMOUS,
    TARGET_FOR_NAMED_ONLY
  };

  // The labels associated with this statement. May be NULL;
  // if it is != NULL, guaranteed to contain at least one entry.
  ZoneStringList* labels() const { return labels_; }

  // Type testing & conversion.
  virtual BreakableStatement* AsBreakableStatement() { return this; }

  // Code generation
348
  Label* break_target() { return &break_target_; }
349 350 351 352

  // Testers.
  bool is_target_for_anonymous() const { return type_ == TARGET_FOR_ANONYMOUS; }

353 354 355 356
  // Bailout support.
  int EntryId() const { return entry_id_; }
  int ExitId() const { return exit_id_; }

357
 protected:
358
  BreakableStatement(Isolate* isolate, ZoneStringList* labels, Type type);
359 360 361 362

 private:
  ZoneStringList* labels_;
  Type type_;
363
  Label break_target_;
364 365
  int entry_id_;
  int exit_id_;
366 367 368 369 370
};


class Block: public BreakableStatement {
 public:
371 372 373 374
  inline Block(Isolate* isolate,
               ZoneStringList* labels,
               int capacity,
               bool is_initializer_block);
375

376
  DECLARE_NODE_TYPE(Block)
377

378 379 380 381 382 383 384 385 386 387
  virtual Assignment* StatementAsSimpleAssignment() {
    if (statements_.length() != 1) return NULL;
    return statements_[0]->StatementAsSimpleAssignment();
  }

  virtual CountOperation* StatementAsCountOperation() {
    if (statements_.length() != 1) return NULL;
    return statements_[0]->StatementAsCountOperation();
  }

388 389
  virtual bool IsInlineable() const;

390 391 392
  void AddStatement(Statement* statement) { statements_.Add(statement); }

  ZoneList<Statement*>* statements() { return &statements_; }
393
  bool is_initializer_block() const { return is_initializer_block_; }
394

395 396 397
  Scope* block_scope() const { return block_scope_; }
  void set_block_scope(Scope* block_scope) { block_scope_ = block_scope; }

398 399 400
 private:
  ZoneList<Statement*> statements_;
  bool is_initializer_block_;
401
  Scope* block_scope_;
402 403 404
};


405
class Declaration: public AstNode {
406 407
 public:
  Declaration(VariableProxy* proxy, Variable::Mode mode, FunctionLiteral* fun)
408 409 410
      : proxy_(proxy),
        mode_(mode),
        fun_(fun) {
411 412 413
    ASSERT(mode == Variable::VAR ||
           mode == Variable::CONST ||
           mode == Variable::LET);
414
    // At the moment there are no "const functions"'s in JavaScript...
415
    ASSERT(fun == NULL || mode == Variable::VAR || mode == Variable::LET);
416 417
  }

418
  DECLARE_NODE_TYPE(Declaration)
419

420 421 422
  VariableProxy* proxy() const { return proxy_; }
  Variable::Mode mode() const { return mode_; }
  FunctionLiteral* fun() const { return fun_; }  // may be NULL
423
  virtual bool IsInlineable() const;
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438

 private:
  VariableProxy* proxy_;
  Variable::Mode mode_;
  FunctionLiteral* fun_;
};


class IterationStatement: public BreakableStatement {
 public:
  // Type testing & conversion.
  virtual IterationStatement* AsIterationStatement() { return this; }

  Statement* body() const { return body_; }

439 440 441
  // Bailout support.
  int OsrEntryId() const { return osr_entry_id_; }
  virtual int ContinueId() const = 0;
442
  virtual int StackCheckId() const = 0;
443

444
  // Code generation
445
  Label* continue_target()  { return &continue_target_; }
446 447

 protected:
448
  inline IterationStatement(Isolate* isolate, ZoneStringList* labels);
449 450 451 452 453 454 455

  void Initialize(Statement* body) {
    body_ = body;
  }

 private:
  Statement* body_;
456
  Label continue_target_;
457
  int osr_entry_id_;
458 459 460
};


461
class DoWhileStatement: public IterationStatement {
462
 public:
463
  inline DoWhileStatement(Isolate* isolate, ZoneStringList* labels);
464

465 466
  DECLARE_NODE_TYPE(DoWhileStatement)

467 468 469 470
  void Initialize(Expression* cond, Statement* body) {
    IterationStatement::Initialize(body);
    cond_ = cond;
  }
471

472 473
  Expression* cond() const { return cond_; }

474 475 476 477 478
  // Position where condition expression starts. We need it to make
  // the loop's condition a breakable location.
  int condition_position() { return condition_position_; }
  void set_condition_position(int pos) { condition_position_ = pos; }

479
  // Bailout support.
480
  virtual int ContinueId() const { return continue_id_; }
481
  virtual int StackCheckId() const { return back_edge_id_; }
482
  int BackEdgeId() const { return back_edge_id_; }
483

484 485
  virtual bool IsInlineable() const;

486 487
 private:
  Expression* cond_;
488
  int condition_position_;
489 490
  int continue_id_;
  int back_edge_id_;
491 492 493 494 495
};


class WhileStatement: public IterationStatement {
 public:
496
  inline WhileStatement(Isolate* isolate, ZoneStringList* labels);
497

498 499
  DECLARE_NODE_TYPE(WhileStatement)

500 501 502 503 504 505 506 507 508
  void Initialize(Expression* cond, Statement* body) {
    IterationStatement::Initialize(body);
    cond_ = cond;
  }

  Expression* cond() const { return cond_; }
  bool may_have_function_literal() const {
    return may_have_function_literal_;
  }
509 510 511
  void set_may_have_function_literal(bool value) {
    may_have_function_literal_ = value;
  }
512
  virtual bool IsInlineable() const;
513

514 515
  // Bailout support.
  virtual int ContinueId() const { return EntryId(); }
516
  virtual int StackCheckId() const { return body_id_; }
517
  int BodyId() const { return body_id_; }
518

519 520 521 522
 private:
  Expression* cond_;
  // True if there is a function literal subexpression in the condition.
  bool may_have_function_literal_;
523
  int body_id_;
524 525 526 527 528
};


class ForStatement: public IterationStatement {
 public:
529
  inline ForStatement(Isolate* isolate, ZoneStringList* labels);
530

531
  DECLARE_NODE_TYPE(ForStatement)
532 533 534 535 536 537 538 539 540 541 542

  void Initialize(Statement* init,
                  Expression* cond,
                  Statement* next,
                  Statement* body) {
    IterationStatement::Initialize(body);
    init_ = init;
    cond_ = cond;
    next_ = next;
  }

543 544 545
  Statement* init() const { return init_; }
  Expression* cond() const { return cond_; }
  Statement* next() const { return next_; }
546

547 548 549
  bool may_have_function_literal() const {
    return may_have_function_literal_;
  }
550 551 552
  void set_may_have_function_literal(bool value) {
    may_have_function_literal_ = value;
  }
553

554
  // Bailout support.
555
  virtual int ContinueId() const { return continue_id_; }
556
  virtual int StackCheckId() const { return body_id_; }
557
  int BodyId() const { return body_id_; }
558

559 560 561
  bool is_fast_smi_loop() { return loop_variable_ != NULL; }
  Variable* loop_variable() { return loop_variable_; }
  void set_loop_variable(Variable* var) { loop_variable_ = var; }
562
  virtual bool IsInlineable() const;
563

564 565 566 567
 private:
  Statement* init_;
  Expression* cond_;
  Statement* next_;
568
  // True if there is a function literal subexpression in the condition.
569
  bool may_have_function_literal_;
570
  Variable* loop_variable_;
571 572
  int continue_id_;
  int body_id_;
573 574 575 576 577
};


class ForInStatement: public IterationStatement {
 public:
578
  inline ForInStatement(Isolate* isolate, ZoneStringList* labels);
579

580 581
  DECLARE_NODE_TYPE(ForInStatement)

582 583 584 585 586 587 588 589
  void Initialize(Expression* each, Expression* enumerable, Statement* body) {
    IterationStatement::Initialize(body);
    each_ = each;
    enumerable_ = enumerable;
  }

  Expression* each() const { return each_; }
  Expression* enumerable() const { return enumerable_; }
590
  virtual bool IsInlineable() const;
591

592
  // Bailout support.
593
  int AssignmentId() const { return assignment_id_; }
594
  virtual int ContinueId() const { return EntryId(); }
595
  virtual int StackCheckId() const { return EntryId(); }
596

597 598 599
 private:
  Expression* each_;
  Expression* enumerable_;
600
  int assignment_id_;
601 602 603 604 605 606 607 608
};


class ExpressionStatement: public Statement {
 public:
  explicit ExpressionStatement(Expression* expression)
      : expression_(expression) { }

609
  DECLARE_NODE_TYPE(ExpressionStatement)
610

611 612
  virtual bool IsInlineable() const;

613 614 615
  virtual Assignment* StatementAsSimpleAssignment();
  virtual CountOperation* StatementAsCountOperation();

616
  void set_expression(Expression* e) { expression_ = e; }
617
  Expression* expression() const { return expression_; }
618 619 620 621 622 623 624 625 626 627 628

 private:
  Expression* expression_;
};


class ContinueStatement: public Statement {
 public:
  explicit ContinueStatement(IterationStatement* target)
      : target_(target) { }

629
  DECLARE_NODE_TYPE(ContinueStatement)
630

631
  IterationStatement* target() const { return target_; }
632
  virtual bool IsInlineable() const;
633 634 635 636 637 638 639 640 641 642 643

 private:
  IterationStatement* target_;
};


class BreakStatement: public Statement {
 public:
  explicit BreakStatement(BreakableStatement* target)
      : target_(target) { }

644
  DECLARE_NODE_TYPE(BreakStatement)
645

646
  BreakableStatement* target() const { return target_; }
647
  virtual bool IsInlineable() const;
648 649 650 651 652 653 654 655 656 657 658

 private:
  BreakableStatement* target_;
};


class ReturnStatement: public Statement {
 public:
  explicit ReturnStatement(Expression* expression)
      : expression_(expression) { }

659
  DECLARE_NODE_TYPE(ReturnStatement)
660

661 662
  Expression* expression() const { return expression_; }
  virtual bool IsInlineable() const;
663 664 665 666 667 668

 private:
  Expression* expression_;
};


669
class WithStatement: public Statement {
670
 public:
671 672
  WithStatement(Expression* expression, Statement* statement)
      : expression_(expression), statement_(statement) { }
673

674
  DECLARE_NODE_TYPE(WithStatement)
675

676
  Expression* expression() const { return expression_; }
677
  Statement* statement() const { return statement_; }
678

679
  virtual bool IsInlineable() const;
680

681 682
 private:
  Expression* expression_;
683
  Statement* statement_;
684 685 686
};


687
class ExitContextStatement: public Statement {
688
 public:
689 690
  virtual bool IsInlineable() const;

691
  DECLARE_NODE_TYPE(ExitContextStatement)
692 693 694 695 696
};


class CaseClause: public ZoneObject {
 public:
697 698 699 700
  CaseClause(Isolate* isolate,
             Expression* label,
             ZoneList<Statement*>* statements,
             int pos);
701

702 703
  bool is_default() const { return label_ == NULL; }
  Expression* label() const {
704 705 706
    CHECK(!is_default());
    return label_;
  }
707
  Label* body_target() { return &body_target_; }
708
  ZoneList<Statement*>* statements() const { return statements_; }
709

710
  int position() const { return position_; }
711 712
  void set_position(int pos) { position_ = pos; }

713
  int EntryId() { return entry_id_; }
714
  int CompareId() { return compare_id_; }
715

716 717 718 719 720
  // Type feedback information.
  void RecordTypeFeedback(TypeFeedbackOracle* oracle);
  bool IsSmiCompare() { return compare_type_ == SMI_ONLY; }
  bool IsObjectCompare() { return compare_type_ == OBJECT_ONLY; }

721 722
 private:
  Expression* label_;
723
  Label body_target_;
724
  ZoneList<Statement*>* statements_;
725 726 727
  int position_;
  enum CompareTypeFeedback { NONE, SMI_ONLY, OBJECT_ONLY };
  CompareTypeFeedback compare_type_;
728
  int compare_id_;
729
  int entry_id_;
730 731 732 733 734
};


class SwitchStatement: public BreakableStatement {
 public:
735
  inline SwitchStatement(Isolate* isolate, ZoneStringList* labels);
736

737 738
  DECLARE_NODE_TYPE(SwitchStatement)

739 740 741 742 743
  void Initialize(Expression* tag, ZoneList<CaseClause*>* cases) {
    tag_ = tag;
    cases_ = cases;
  }

744 745
  Expression* tag() const { return tag_; }
  ZoneList<CaseClause*>* cases() const { return cases_; }
746
  virtual bool IsInlineable() const;
747 748 749 750 751 752 753 754 755 756 757 758 759 760

 private:
  Expression* tag_;
  ZoneList<CaseClause*>* cases_;
};


// If-statements always have non-null references to their then- and
// else-parts. When parsing if-statements with no explicit else-part,
// the parser implicitly creates an empty statement. Use the
// HasThenStatement() and HasElseStatement() functions to check if a
// given if-statement has a then- or an else-part containing code.
class IfStatement: public Statement {
 public:
761 762
  IfStatement(Isolate* isolate,
              Expression* condition,
763 764 765 766
              Statement* then_statement,
              Statement* else_statement)
      : condition_(condition),
        then_statement_(then_statement),
767
        else_statement_(else_statement),
768 769 770
        if_id_(GetNextId(isolate)),
        then_id_(GetNextId(isolate)),
        else_id_(GetNextId(isolate)) {
771
  }
772

773
  DECLARE_NODE_TYPE(IfStatement)
774

775 776
  virtual bool IsInlineable() const;

777 778 779 780 781 782 783
  bool HasThenStatement() const { return !then_statement()->IsEmpty(); }
  bool HasElseStatement() const { return !else_statement()->IsEmpty(); }

  Expression* condition() const { return condition_; }
  Statement* then_statement() const { return then_statement_; }
  Statement* else_statement() const { return else_statement_; }

784
  int IfId() const { return if_id_; }
785 786 787
  int ThenId() const { return then_id_; }
  int ElseId() const { return else_id_; }

788 789 790 791
 private:
  Expression* condition_;
  Statement* then_statement_;
  Statement* else_statement_;
792
  int if_id_;
793 794
  int then_id_;
  int else_id_;
795 796 797
};


798
// NOTE: TargetCollectors are represented as nodes to fit in the target
799
// stack in the compiler; this should probably be reworked.
800
class TargetCollector: public AstNode {
801
 public:
802
  TargetCollector(): targets_(0) { }
803

804 805 806
  // Adds a jump target to the collector. The collector stores a pointer not
  // a copy of the target to make binding work, so make sure not to pass in
  // references to something on the stack.
807
  void AddTarget(Label* target);
808

809
  // Virtual behaviour. TargetCollectors are never part of the AST.
810
  virtual void Accept(AstVisitor* v) { UNREACHABLE(); }
811
  virtual TargetCollector* AsTargetCollector() { return this; }
812

813
  ZoneList<Label*>* targets() { return &targets_; }
814
  virtual bool IsInlineable() const;
815 816

 private:
817
  ZoneList<Label*> targets_;
818 819 820 821 822 823
};


class TryStatement: public Statement {
 public:
  explicit TryStatement(Block* try_block)
824
      : try_block_(try_block), escaping_targets_(NULL) { }
825

826
  void set_escaping_targets(ZoneList<Label*>* targets) {
827
    escaping_targets_ = targets;
828 829 830
  }

  Block* try_block() const { return try_block_; }
831
  ZoneList<Label*>* escaping_targets() const { return escaping_targets_; }
832
  virtual bool IsInlineable() const;
833 834 835

 private:
  Block* try_block_;
836
  ZoneList<Label*>* escaping_targets_;
837 838 839
};


840
class TryCatchStatement: public TryStatement {
841
 public:
842 843 844 845
  TryCatchStatement(Block* try_block,
                    Scope* scope,
                    Variable* variable,
                    Block* catch_block)
846
      : TryStatement(try_block),
847 848
        scope_(scope),
        variable_(variable),
849 850 851
        catch_block_(catch_block) {
  }

852
  DECLARE_NODE_TYPE(TryCatchStatement)
853

854 855
  Scope* scope() { return scope_; }
  Variable* variable() { return variable_; }
856
  Block* catch_block() const { return catch_block_; }
857
  virtual bool IsInlineable() const;
858 859

 private:
860 861
  Scope* scope_;
  Variable* variable_;
862 863 864 865
  Block* catch_block_;
};


866
class TryFinallyStatement: public TryStatement {
867
 public:
868
  TryFinallyStatement(Block* try_block, Block* finally_block)
869 870 871
      : TryStatement(try_block),
        finally_block_(finally_block) { }

872
  DECLARE_NODE_TYPE(TryFinallyStatement)
873 874

  Block* finally_block() const { return finally_block_; }
875
  virtual bool IsInlineable() const;
876 877 878 879 880 881 882 883

 private:
  Block* finally_block_;
};


class DebuggerStatement: public Statement {
 public:
884
  DECLARE_NODE_TYPE(DebuggerStatement)
885
  virtual bool IsInlineable() const;
886 887 888 889 890
};


class EmptyStatement: public Statement {
 public:
891
  DECLARE_NODE_TYPE(EmptyStatement)
892

893
  virtual bool IsInlineable() const;
894 895 896 897 898
};


class Literal: public Expression {
 public:
899 900
  Literal(Isolate* isolate, Handle<Object> handle)
      : Expression(isolate), handle_(handle) { }
901

902 903
  DECLARE_NODE_TYPE(Literal)

904
  virtual bool IsTrivial() { return true; }
905
  virtual bool IsSmiLiteral() { return handle_->IsSmi(); }
906 907 908 909 910 911

  // Check if this literal is identical to the other literal.
  bool IsIdenticalTo(const Literal* other) const {
    return handle_.is_identical_to(other->handle_);
  }

912 913 914 915 916 917 918 919
  virtual bool IsPropertyName() {
    if (handle_->IsSymbol()) {
      uint32_t ignored;
      return !String::cast(*handle_)->AsArrayIndex(&ignored);
    }
    return false;
  }

920 921 922 923 924 925 926 927
  Handle<String> AsPropertyName() {
    ASSERT(IsPropertyName());
    return Handle<String>::cast(handle_);
  }

  virtual bool ToBooleanIsTrue() { return handle_->ToBoolean()->IsTrue(); }
  virtual bool ToBooleanIsFalse() { return handle_->ToBoolean()->IsFalse(); }

928
  // Identity testers.
929 930 931 932 933 934 935 936
  bool IsNull() const {
    ASSERT(!handle_.is_null());
    return handle_->IsNull();
  }
  bool IsTrue() const {
    ASSERT(!handle_.is_null());
    return handle_->IsTrue();
  }
937
  bool IsFalse() const {
938 939
    ASSERT(!handle_.is_null());
    return handle_->IsFalse();
940 941 942
  }

  Handle<Object> handle() const { return handle_; }
943
  virtual bool IsInlineable() const;
944 945 946 947 948 949 950 951 952

 private:
  Handle<Object> handle_;
};


// Base class for literals that needs space in the corresponding JSFunction.
class MaterializedLiteral: public Expression {
 public:
953 954 955 956 957 958 959 960
  MaterializedLiteral(Isolate* isolate,
                      int literal_index,
                      bool is_simple,
                      int depth)
      : Expression(isolate),
        literal_index_(literal_index),
        is_simple_(is_simple),
        depth_(depth) {}
961 962 963

  virtual MaterializedLiteral* AsMaterializedLiteral() { return this; }

964
  int literal_index() { return literal_index_; }
965 966 967 968 969 970

  // A materialized literal is simple if the values consist of only
  // constants and simple object and array literals.
  bool is_simple() const { return is_simple_; }

  int depth() const { return depth_; }
971
  virtual bool IsInlineable() const;
972

973 974
 private:
  int literal_index_;
975 976
  bool is_simple_;
  int depth_;
977 978 979 980 981 982 983 984 985 986 987 988 989
};


// An object literal has a boilerplate object that is used
// for minimizing the work when constructing it at runtime.
class ObjectLiteral: public MaterializedLiteral {
 public:
  // Property is used for passing information
  // about an object literal's properties from the parser
  // to the code generator.
  class Property: public ZoneObject {
   public:
    enum Kind {
990 991 992 993 994
      CONSTANT,              // Property with constant value (compile time).
      COMPUTED,              // Property with computed value (execution time).
      MATERIALIZED_LITERAL,  // Property value is a materialized literal.
      GETTER, SETTER,        // Property is an accessor function.
      PROTOTYPE              // Property is __proto__.
995 996 997 998 999 1000 1001 1002 1003
    };

    Property(Literal* key, Expression* value);
    Property(bool is_getter, FunctionLiteral* value);

    Literal* key() { return key_; }
    Expression* value() { return value_; }
    Kind kind() { return kind_; }

1004 1005
    bool IsCompileTimeValue();

1006 1007 1008
    void set_emit_store(bool emit_store);
    bool emit_store();

1009 1010 1011 1012
   private:
    Literal* key_;
    Expression* value_;
    Kind kind_;
1013
    bool emit_store_;
1014 1015
  };

1016 1017
  ObjectLiteral(Isolate* isolate,
                Handle<FixedArray> constant_properties,
1018
                ZoneList<Property*>* properties,
1019 1020
                int literal_index,
                bool is_simple,
1021
                bool fast_elements,
1022 1023
                int depth,
                bool has_function)
1024
      : MaterializedLiteral(isolate, literal_index, is_simple, depth),
1025
        constant_properties_(constant_properties),
1026
        properties_(properties),
1027 1028
        fast_elements_(fast_elements),
        has_function_(has_function) {}
1029

1030
  DECLARE_NODE_TYPE(ObjectLiteral)
1031 1032 1033 1034 1035 1036

  Handle<FixedArray> constant_properties() const {
    return constant_properties_;
  }
  ZoneList<Property*>* properties() const { return properties_; }

1037 1038
  bool fast_elements() const { return fast_elements_; }

1039
  bool has_function() { return has_function_; }
1040 1041 1042 1043 1044 1045

  // Mark all computed expressions that are bound to a key that
  // is shadowed by a later occurrence of the same key. For the
  // marked expressions, no store code is emitted.
  void CalculateEmitStore();

1046 1047 1048 1049 1050 1051
  enum Flags {
    kNoFlags = 0,
    kFastElements = 1,
    kHasFunction = 1 << 1
  };

1052 1053 1054
 private:
  Handle<FixedArray> constant_properties_;
  ZoneList<Property*>* properties_;
1055
  bool fast_elements_;
1056
  bool has_function_;
1057 1058 1059 1060 1061 1062
};


// Node for capturing a regexp literal.
class RegExpLiteral: public MaterializedLiteral {
 public:
1063 1064
  RegExpLiteral(Isolate* isolate,
                Handle<String> pattern,
1065 1066
                Handle<String> flags,
                int literal_index)
1067
      : MaterializedLiteral(isolate, literal_index, false, 1),
1068 1069 1070
        pattern_(pattern),
        flags_(flags) {}

1071
  DECLARE_NODE_TYPE(RegExpLiteral)
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081

  Handle<String> pattern() const { return pattern_; }
  Handle<String> flags() const { return flags_; }

 private:
  Handle<String> pattern_;
  Handle<String> flags_;
};

// An array literal has a literals object that is used
1082
// for minimizing the work when constructing it at runtime.
1083
class ArrayLiteral: public MaterializedLiteral {
1084
 public:
1085 1086
  ArrayLiteral(Isolate* isolate,
               Handle<FixedArray> constant_elements,
1087 1088 1089 1090
               ZoneList<Expression*>* values,
               int literal_index,
               bool is_simple,
               int depth)
1091
      : MaterializedLiteral(isolate, literal_index, is_simple, depth),
1092
        constant_elements_(constant_elements),
1093
        values_(values),
1094
        first_element_id_(ReserveIdRange(isolate, values->length())) {}
1095

1096
  DECLARE_NODE_TYPE(ArrayLiteral)
1097

1098
  Handle<FixedArray> constant_elements() const { return constant_elements_; }
1099 1100
  ZoneList<Expression*>* values() const { return values_; }

1101 1102 1103
  // Return an AST id for an element that is used in simulate instructions.
  int GetIdForElement(int i) { return first_element_id_ + i; }

1104
 private:
1105
  Handle<FixedArray> constant_elements_;
1106
  ZoneList<Expression*>* values_;
1107
  int first_element_id_;
1108 1109 1110 1111 1112
};


class VariableProxy: public Expression {
 public:
1113
  VariableProxy(Isolate* isolate, Variable* var);
1114

1115
  DECLARE_NODE_TYPE(VariableProxy)
1116 1117

  // Type testing & conversion
1118
  Variable* AsVariable() { return (this == NULL) ? NULL : var_; }
1119

1120 1121 1122
  virtual bool IsValidLeftHandSide() {
    return var_ == NULL ? true : var_->IsValidLeftHandSide();
  }
1123

1124 1125 1126 1127
  virtual bool IsTrivial() {
    // Reading from a mutable variable is a side effect, but the
    // variable for 'this' is immutable.
    return is_this_ || is_trivial_;
1128 1129
  }

1130 1131
  virtual bool IsInlineable() const;

1132 1133 1134 1135
  bool IsVariable(Handle<String> n) {
    return !is_this() && name().is_identical_to(n);
  }

1136 1137 1138 1139 1140
  bool IsArguments() {
    Variable* variable = AsVariable();
    return (variable == NULL) ? false : variable->is_arguments();
  }

1141 1142 1143 1144
  Handle<String> name() const { return name_; }
  Variable* var() const { return var_; }
  bool is_this() const { return is_this_; }
  bool inside_with() const { return inside_with_; }
1145
  int position() const { return position_; }
1146

1147
  void MarkAsTrivial() { is_trivial_ = true; }
1148

1149 1150 1151 1152 1153 1154 1155 1156
  // Bind this proxy to the variable var.
  void BindTo(Variable* var);

 protected:
  Handle<String> name_;
  Variable* var_;  // resolved variable, or NULL
  bool is_this_;
  bool inside_with_;
1157
  bool is_trivial_;
1158
  int position_;
1159

1160 1161
  VariableProxy(Isolate* isolate,
                Handle<String> name,
1162 1163 1164
                bool is_this,
                bool inside_with,
                int position = RelocInfo::kNoPosition);
1165
  VariableProxy(Isolate* isolate, bool is_this);
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175

  friend class Scope;
};


class VariableProxySentinel: public VariableProxy {
 public:
  virtual bool IsValidLeftHandSide() { return !is_this(); }

 private:
1176 1177
  VariableProxySentinel(Isolate* isolate, bool is_this)
      : VariableProxy(isolate, is_this) { }
1178 1179

  friend class AstSentinels;
1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203
};


class Slot: public Expression {
 public:
  enum Type {
    // A slot in the parameter section on the stack. index() is
    // the parameter index, counting left-to-right, starting at 0.
    PARAMETER,

    // A slot in the local section on the stack. index() is
    // the variable index in the stack frame, starting at 0.
    LOCAL,

    // An indexed slot in a heap context. index() is the
    // variable index in the context object on the heap,
    // starting at 0. var()->scope() is the corresponding
    // scope.
    CONTEXT,

    // A named slot in a heap context. var()->name() is the
    // variable name in the context object on the heap,
    // with lookup starting at the current context. index()
    // is invalid.
1204
    LOOKUP
1205 1206
  };

1207 1208
  Slot(Isolate* isolate, Variable* var, Type type, int index)
      : Expression(isolate), var_(var), type_(type), index_(index) {
1209 1210 1211
    ASSERT(var != NULL);
  }

1212 1213 1214
  virtual void Accept(AstVisitor* v);

  virtual Slot* AsSlot() { return this; }
1215

1216 1217
  bool IsStackAllocated() { return type_ == PARAMETER || type_ == LOCAL; }

1218
  // Accessors
1219 1220 1221 1222
  Variable* var() const { return var_; }
  Type type() const { return type_; }
  int index() const { return index_; }
  bool is_arguments() const { return var_->is_arguments(); }
1223
  virtual bool IsInlineable() const;
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233

 private:
  Variable* var_;
  Type type_;
  int index_;
};


class Property: public Expression {
 public:
1234 1235 1236
  Property(Isolate* isolate,
           Expression* obj,
           Expression* key,
1237
           int pos)
1238 1239
      : Expression(isolate),
        obj_(obj),
1240 1241
        key_(key),
        pos_(pos),
1242
        is_monomorphic_(false),
1243
        is_array_length_(false),
1244
        is_string_length_(false),
1245
        is_string_access_(false),
1246
        is_function_prototype_(false) { }
1247

1248
  DECLARE_NODE_TYPE(Property)
1249 1250

  virtual bool IsValidLeftHandSide() { return true; }
1251
  virtual bool IsInlineable() const;
1252 1253 1254

  Expression* obj() const { return obj_; }
  Expression* key() const { return key_; }
1255
  virtual int position() const { return pos_; }
1256

1257
  bool IsStringLength() const { return is_string_length_; }
1258
  bool IsStringAccess() const { return is_string_access_; }
1259 1260
  bool IsFunctionPrototype() const { return is_function_prototype_; }

1261 1262 1263
  // Type feedback information.
  void RecordTypeFeedback(TypeFeedbackOracle* oracle);
  virtual bool IsMonomorphic() { return is_monomorphic_; }
1264
  virtual SmallMapList* GetReceiverTypes() { return &receiver_types_; }
1265 1266
  virtual bool IsArrayLength() { return is_array_length_; }

1267 1268 1269 1270 1271
 private:
  Expression* obj_;
  Expression* key_;
  int pos_;

1272
  SmallMapList receiver_types_;
1273 1274 1275
  bool is_monomorphic_ : 1;
  bool is_array_length_ : 1;
  bool is_string_length_ : 1;
1276
  bool is_string_access_ : 1;
1277
  bool is_function_prototype_ : 1;
1278 1279 1280 1281 1282
};


class Call: public Expression {
 public:
1283 1284 1285 1286 1287 1288
  Call(Isolate* isolate,
       Expression* expression,
       ZoneList<Expression*>* arguments,
       int pos)
      : Expression(isolate),
        expression_(expression),
1289 1290 1291
        arguments_(arguments),
        pos_(pos),
        is_monomorphic_(false),
1292
        check_type_(RECEIVER_MAP_CHECK),
1293
        return_id_(GetNextId(isolate)) {
1294
  }
1295

1296
  DECLARE_NODE_TYPE(Call)
1297

1298 1299
  virtual bool IsInlineable() const;

1300 1301
  Expression* expression() const { return expression_; }
  ZoneList<Expression*>* arguments() const { return arguments_; }
1302
  virtual int position() const { return pos_; }
1303

1304 1305
  void RecordTypeFeedback(TypeFeedbackOracle* oracle,
                          CallKind call_kind);
1306
  virtual SmallMapList* GetReceiverTypes() { return &receiver_types_; }
1307
  virtual bool IsMonomorphic() { return is_monomorphic_; }
1308
  CheckType check_type() const { return check_type_; }
1309 1310 1311 1312 1313
  Handle<JSFunction> target() { return target_; }
  Handle<JSObject> holder() { return holder_; }
  Handle<JSGlobalPropertyCell> cell() { return cell_; }

  bool ComputeTarget(Handle<Map> type, Handle<String> name);
1314
  bool ComputeGlobalTarget(Handle<GlobalObject> global, LookupResult* lookup);
1315 1316 1317 1318 1319 1320 1321 1322 1323

  // Bailout support.
  int ReturnId() const { return return_id_; }

#ifdef DEBUG
  // Used to assert that the FullCodeGenerator records the return site.
  bool return_is_recorded_;
#endif

1324 1325 1326 1327 1328
 private:
  Expression* expression_;
  ZoneList<Expression*>* arguments_;
  int pos_;

1329
  bool is_monomorphic_;
1330
  CheckType check_type_;
1331
  SmallMapList receiver_types_;
1332 1333 1334 1335 1336
  Handle<JSFunction> target_;
  Handle<JSObject> holder_;
  Handle<JSGlobalPropertyCell> cell_;

  int return_id_;
1337
};
1338

1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366

class AstSentinels {
 public:
  ~AstSentinels() { }

  // Returns a property singleton property access on 'this'.  Used
  // during preparsing.
  Property* this_property() { return &this_property_; }
  VariableProxySentinel* this_proxy() { return &this_proxy_; }
  VariableProxySentinel* identifier_proxy() { return &identifier_proxy_; }
  ValidLeftHandSideSentinel* valid_left_hand_side_sentinel() {
    return &valid_left_hand_side_sentinel_;
  }
  Call* call_sentinel() { return &call_sentinel_; }
  EmptyStatement* empty_statement() { return &empty_statement_; }

 private:
  AstSentinels();
  VariableProxySentinel this_proxy_;
  VariableProxySentinel identifier_proxy_;
  ValidLeftHandSideSentinel valid_left_hand_side_sentinel_;
  Property this_property_;
  Call call_sentinel_;
  EmptyStatement empty_statement_;

  friend class Isolate;

  DISALLOW_COPY_AND_ASSIGN(AstSentinels);
1367 1368 1369
};


1370
class CallNew: public Expression {
1371
 public:
1372 1373 1374 1375 1376 1377 1378 1379
  CallNew(Isolate* isolate,
          Expression* expression,
          ZoneList<Expression*>* arguments,
          int pos)
      : Expression(isolate),
        expression_(expression),
        arguments_(arguments),
        pos_(pos) { }
1380

1381
  DECLARE_NODE_TYPE(CallNew)
1382

1383 1384
  virtual bool IsInlineable() const;

1385 1386
  Expression* expression() const { return expression_; }
  ZoneList<Expression*>* arguments() const { return arguments_; }
1387
  virtual int position() const { return pos_; }
1388 1389 1390 1391 1392

 private:
  Expression* expression_;
  ZoneList<Expression*>* arguments_;
  int pos_;
1393 1394 1395
};


1396 1397 1398 1399 1400 1401
// The CallRuntime class does not represent any official JavaScript
// language construct. Instead it is used to call a C or JS function
// with a set of arguments. This is used from the builtins that are
// implemented in JavaScript (see "v8natives.js").
class CallRuntime: public Expression {
 public:
1402 1403
  CallRuntime(Isolate* isolate,
              Handle<String> name,
1404
              const Runtime::Function* function,
1405
              ZoneList<Expression*>* arguments)
1406 1407 1408 1409
      : Expression(isolate),
        name_(name),
        function_(function),
        arguments_(arguments) { }
1410

1411
  DECLARE_NODE_TYPE(CallRuntime)
1412

1413 1414
  virtual bool IsInlineable() const;

1415
  Handle<String> name() const { return name_; }
1416
  const Runtime::Function* function() const { return function_; }
1417
  ZoneList<Expression*>* arguments() const { return arguments_; }
1418
  bool is_jsruntime() const { return function_ == NULL; }
1419 1420 1421

 private:
  Handle<String> name_;
1422
  const Runtime::Function* function_;
1423 1424 1425 1426 1427 1428
  ZoneList<Expression*>* arguments_;
};


class UnaryOperation: public Expression {
 public:
1429 1430 1431 1432 1433
  UnaryOperation(Isolate* isolate,
                 Token::Value op,
                 Expression* expression,
                 int pos)
      : Expression(isolate), op_(op), expression_(expression), pos_(pos) {
1434 1435 1436
    ASSERT(Token::IsUnaryOp(op));
  }

1437
  DECLARE_NODE_TYPE(UnaryOperation)
1438

1439 1440
  virtual bool IsInlineable() const;

1441
  virtual bool ResultOverwriteAllowed();
1442 1443 1444

  Token::Value op() const { return op_; }
  Expression* expression() const { return expression_; }
1445
  virtual int position() const { return pos_; }
1446 1447 1448 1449

 private:
  Token::Value op_;
  Expression* expression_;
1450
  int pos_;
1451 1452 1453 1454 1455
};


class BinaryOperation: public Expression {
 public:
1456 1457
  BinaryOperation(Isolate* isolate,
                  Token::Value op,
1458 1459 1460
                  Expression* left,
                  Expression* right,
                  int pos)
1461
      : Expression(isolate), op_(op), left_(left), right_(right), pos_(pos) {
1462
    ASSERT(Token::IsBinaryOp(op));
1463
    right_id_ = (op == Token::AND || op == Token::OR)
1464
        ? static_cast<int>(GetNextId(isolate))
1465
        : AstNode::kNoNumber;
1466 1467
  }

1468
  DECLARE_NODE_TYPE(BinaryOperation)
1469

1470 1471
  virtual bool IsInlineable() const;

1472
  virtual bool ResultOverwriteAllowed();
1473 1474 1475 1476

  Token::Value op() const { return op_; }
  Expression* left() const { return left_; }
  Expression* right() const { return right_; }
1477
  virtual int position() const { return pos_; }
1478

1479 1480 1481
  // Bailout support.
  int RightId() const { return right_id_; }

1482 1483 1484 1485
 private:
  Token::Value op_;
  Expression* left_;
  Expression* right_;
1486
  int pos_;
1487 1488 1489
  // The short-circuit logical operations have an AST ID for their
  // right-hand subexpression.
  int right_id_;
1490 1491 1492
};


1493 1494
class CountOperation: public Expression {
 public:
1495 1496 1497 1498 1499 1500 1501
  CountOperation(Isolate* isolate,
                 Token::Value op,
                 bool is_prefix,
                 Expression* expr,
                 int pos)
      : Expression(isolate),
        op_(op),
1502 1503 1504
        is_prefix_(is_prefix),
        expression_(expr),
        pos_(pos),
1505
        assignment_id_(GetNextId(isolate)),
1506
        count_id_(GetNextId(isolate)) {}
1507

1508
  DECLARE_NODE_TYPE(CountOperation)
1509

1510 1511
  bool is_prefix() const { return is_prefix_; }
  bool is_postfix() const { return !is_prefix_; }
1512

1513
  Token::Value op() const { return op_; }
1514
  Token::Value binary_op() {
1515
    return (op() == Token::INC) ? Token::ADD : Token::SUB;
1516
  }
1517

1518
  Expression* expression() const { return expression_; }
1519
  virtual int position() const { return pos_; }
1520 1521 1522

  virtual void MarkAsStatement() { is_prefix_ = true; }

1523 1524
  virtual bool IsInlineable() const;

1525 1526
  void RecordTypeFeedback(TypeFeedbackOracle* oracle);
  virtual bool IsMonomorphic() { return is_monomorphic_; }
1527
  virtual SmallMapList* GetReceiverTypes() { return &receiver_types_; }
1528

1529 1530
  // Bailout support.
  int AssignmentId() const { return assignment_id_; }
1531
  int CountId() const { return count_id_; }
1532

1533
 private:
1534
  Token::Value op_;
1535
  bool is_prefix_;
1536
  bool is_monomorphic_;
1537
  Expression* expression_;
1538
  int pos_;
1539
  int assignment_id_;
1540
  int count_id_;
1541
  SmallMapList receiver_types_;
1542 1543 1544 1545 1546
};


class CompareOperation: public Expression {
 public:
1547 1548
  CompareOperation(Isolate* isolate,
                   Token::Value op,
1549 1550 1551
                   Expression* left,
                   Expression* right,
                   int pos)
1552 1553 1554 1555 1556 1557
      : Expression(isolate),
        op_(op),
        left_(left),
        right_(right),
        pos_(pos),
        compare_type_(NONE) {
1558 1559 1560
    ASSERT(Token::IsCompareOp(op));
  }

1561
  DECLARE_NODE_TYPE(CompareOperation)
1562 1563 1564 1565

  Token::Value op() const { return op_; }
  Expression* left() const { return left_; }
  Expression* right() const { return right_; }
1566
  virtual int position() const { return pos_; }
1567

1568 1569 1570 1571 1572 1573 1574
  virtual bool IsInlineable() const;

  // Type feedback information.
  void RecordTypeFeedback(TypeFeedbackOracle* oracle);
  bool IsSmiCompare() { return compare_type_ == SMI_ONLY; }
  bool IsObjectCompare() { return compare_type_ == OBJECT_ONLY; }

1575 1576 1577 1578
  // Match special cases.
  bool IsLiteralCompareTypeof(Expression** expr, Handle<String>* check);
  bool IsLiteralCompareUndefined(Expression** expr);

1579 1580 1581 1582
 private:
  Token::Value op_;
  Expression* left_;
  Expression* right_;
1583
  int pos_;
1584 1585 1586

  enum CompareTypeFeedback { NONE, SMI_ONLY, OBJECT_ONLY };
  CompareTypeFeedback compare_type_;
1587 1588 1589
};


1590 1591
class CompareToNull: public Expression {
 public:
1592 1593
  CompareToNull(Isolate* isolate, bool is_strict, Expression* expression)
      : Expression(isolate), is_strict_(is_strict), expression_(expression) { }
1594

1595
  DECLARE_NODE_TYPE(CompareToNull)
1596

1597 1598
  virtual bool IsInlineable() const;

1599 1600 1601 1602 1603 1604 1605 1606 1607 1608
  bool is_strict() const { return is_strict_; }
  Token::Value op() const { return is_strict_ ? Token::EQ_STRICT : Token::EQ; }
  Expression* expression() const { return expression_; }

 private:
  bool is_strict_;
  Expression* expression_;
};


1609 1610
class Conditional: public Expression {
 public:
1611 1612
  Conditional(Isolate* isolate,
              Expression* condition,
1613
              Expression* then_expression,
1614 1615 1616
              Expression* else_expression,
              int then_expression_position,
              int else_expression_position)
1617 1618
      : Expression(isolate),
        condition_(condition),
1619
        then_expression_(then_expression),
1620 1621
        else_expression_(else_expression),
        then_expression_position_(then_expression_position),
1622
        else_expression_position_(else_expression_position),
1623 1624
        then_id_(GetNextId(isolate)),
        else_id_(GetNextId(isolate)) {
1625
  }
1626

1627
  DECLARE_NODE_TYPE(Conditional)
1628

1629 1630
  virtual bool IsInlineable() const;

1631 1632 1633 1634
  Expression* condition() const { return condition_; }
  Expression* then_expression() const { return then_expression_; }
  Expression* else_expression() const { return else_expression_; }

1635 1636 1637 1638 1639
  int then_expression_position() const { return then_expression_position_; }
  int else_expression_position() const { return else_expression_position_; }

  int ThenId() const { return then_id_; }
  int ElseId() const { return else_id_; }
1640

1641 1642 1643 1644
 private:
  Expression* condition_;
  Expression* then_expression_;
  Expression* else_expression_;
1645 1646
  int then_expression_position_;
  int else_expression_position_;
1647 1648
  int then_id_;
  int else_id_;
1649 1650 1651 1652 1653
};


class Assignment: public Expression {
 public:
1654 1655 1656 1657 1658
  Assignment(Isolate* isolate,
             Token::Value op,
             Expression* target,
             Expression* value,
             int pos);
1659

1660
  DECLARE_NODE_TYPE(Assignment)
1661

1662 1663
  virtual bool IsInlineable() const;

1664 1665
  Assignment* AsSimpleAssignment() { return !is_compound() ? this : NULL; }

1666 1667 1668 1669 1670
  Token::Value binary_op() const;

  Token::Value op() const { return op_; }
  Expression* target() const { return target_; }
  Expression* value() const { return value_; }
1671
  virtual int position() const { return pos_; }
1672 1673
  BinaryOperation* binary_operation() const { return binary_operation_; }

1674 1675
  // This check relies on the definition order of token in token.h.
  bool is_compound() const { return op() > Token::ASSIGN; }
1676

1677 1678 1679 1680 1681 1682 1683 1684 1685
  // An initialization block is a series of statments of the form
  // x.y.z.a = ...; x.y.z.b = ...; etc. The parser marks the beginning and
  // ending of these blocks to allow for optimizations of initialization
  // blocks.
  bool starts_initialization_block() { return block_start_; }
  bool ends_initialization_block() { return block_end_; }
  void mark_block_start() { block_start_ = true; }
  void mark_block_end() { block_end_ = true; }

1686 1687 1688
  // Type feedback information.
  void RecordTypeFeedback(TypeFeedbackOracle* oracle);
  virtual bool IsMonomorphic() { return is_monomorphic_; }
1689
  virtual SmallMapList* GetReceiverTypes() { return &receiver_types_; }
1690 1691

  // Bailout support.
1692 1693
  int CompoundLoadId() const { return compound_load_id_; }
  int AssignmentId() const { return assignment_id_; }
1694

1695 1696 1697 1698 1699
 private:
  Token::Value op_;
  Expression* target_;
  Expression* value_;
  int pos_;
1700
  BinaryOperation* binary_operation_;
1701 1702
  int compound_load_id_;
  int assignment_id_;
1703

1704 1705
  bool block_start_;
  bool block_end_;
1706 1707

  bool is_monomorphic_;
1708
  SmallMapList receiver_types_;
1709 1710 1711 1712 1713
};


class Throw: public Expression {
 public:
1714 1715
  Throw(Isolate* isolate, Expression* exception, int pos)
      : Expression(isolate), exception_(exception), pos_(pos) {}
1716

1717
  DECLARE_NODE_TYPE(Throw)
1718

1719
  Expression* exception() const { return exception_; }
1720
  virtual int position() const { return pos_; }
1721
  virtual bool IsInlineable() const;
1722 1723 1724 1725 1726 1727 1728 1729 1730

 private:
  Expression* exception_;
  int pos_;
};


class FunctionLiteral: public Expression {
 public:
1731 1732 1733 1734 1735 1736
  enum Type {
    ANONYMOUS_EXPRESSION,
    NAMED_EXPRESSION,
    DECLARATION
  };

1737 1738
  FunctionLiteral(Isolate* isolate,
                  Handle<String> name,
1739 1740 1741 1742
                  Scope* scope,
                  ZoneList<Statement*>* body,
                  int materialized_literal_count,
                  int expected_property_count,
1743 1744
                  bool has_only_simple_this_property_assignments,
                  Handle<FixedArray> this_property_assignments,
1745 1746 1747
                  int num_parameters,
                  int start_position,
                  int end_position,
1748
                  Type type,
1749
                  bool has_duplicate_parameters)
1750 1751
      : Expression(isolate),
        name_(name),
1752 1753 1754 1755
        scope_(scope),
        body_(body),
        materialized_literal_count_(materialized_literal_count),
        expected_property_count_(expected_property_count),
1756 1757 1758
        has_only_simple_this_property_assignments_(
            has_only_simple_this_property_assignments),
        this_property_assignments_(this_property_assignments),
1759 1760 1761
        num_parameters_(num_parameters),
        start_position_(start_position),
        end_position_(end_position),
1762
        function_token_position_(RelocInfo::kNoPosition),
1763
        inferred_name_(HEAP->empty_string()),
1764 1765
        is_expression_(type != DECLARATION),
        is_anonymous_(type == ANONYMOUS_EXPRESSION),
1766 1767 1768
        pretenure_(false),
        has_duplicate_parameters_(has_duplicate_parameters) {
  }
1769

1770
  DECLARE_NODE_TYPE(FunctionLiteral)
1771

1772 1773 1774
  Handle<String> name() const { return name_; }
  Scope* scope() const { return scope_; }
  ZoneList<Statement*>* body() const { return body_; }
1775 1776 1777 1778 1779
  void set_function_token_position(int pos) { function_token_position_ = pos; }
  int function_token_position() const { return function_token_position_; }
  int start_position() const { return start_position_; }
  int end_position() const { return end_position_; }
  bool is_expression() const { return is_expression_; }
1780
  bool is_anonymous() const { return is_anonymous_; }
1781
  bool strict_mode() const;
1782 1783 1784

  int materialized_literal_count() { return materialized_literal_count_; }
  int expected_property_count() { return expected_property_count_; }
1785 1786 1787 1788 1789 1790
  bool has_only_simple_this_property_assignments() {
      return has_only_simple_this_property_assignments_;
  }
  Handle<FixedArray> this_property_assignments() {
      return this_property_assignments_;
  }
1791 1792 1793 1794
  int num_parameters() { return num_parameters_; }

  bool AllowsLazyCompilation();

1795 1796 1797 1798 1799
  Handle<String> debug_name() const {
    if (name_->length() > 0) return name_;
    return inferred_name();
  }

1800
  Handle<String> inferred_name() const { return inferred_name_; }
1801 1802 1803 1804
  void set_inferred_name(Handle<String> inferred_name) {
    inferred_name_ = inferred_name;
  }

1805 1806
  bool pretenure() { return pretenure_; }
  void set_pretenure(bool value) { pretenure_ = value; }
1807
  virtual bool IsInlineable() const;
1808

1809 1810
  bool has_duplicate_parameters() { return has_duplicate_parameters_; }

1811 1812 1813 1814 1815 1816
 private:
  Handle<String> name_;
  Scope* scope_;
  ZoneList<Statement*>* body_;
  int materialized_literal_count_;
  int expected_property_count_;
1817 1818
  bool has_only_simple_this_property_assignments_;
  Handle<FixedArray> this_property_assignments_;
1819 1820 1821 1822
  int num_parameters_;
  int start_position_;
  int end_position_;
  int function_token_position_;
1823
  Handle<String> inferred_name_;
1824
  bool is_expression_;
1825
  bool is_anonymous_;
1826
  bool pretenure_;
1827
  bool has_duplicate_parameters_;
1828 1829 1830
};


1831
class SharedFunctionInfoLiteral: public Expression {
1832
 public:
1833 1834
  SharedFunctionInfoLiteral(
      Isolate* isolate,
1835
      Handle<SharedFunctionInfo> shared_function_info)
1836
      : Expression(isolate), shared_function_info_(shared_function_info) { }
1837

1838 1839
  DECLARE_NODE_TYPE(SharedFunctionInfoLiteral)

1840 1841 1842
  Handle<SharedFunctionInfo> shared_function_info() const {
    return shared_function_info_;
  }
1843
  virtual bool IsInlineable() const;
1844 1845

 private:
1846
  Handle<SharedFunctionInfo> shared_function_info_;
1847 1848 1849 1850 1851
};


class ThisFunction: public Expression {
 public:
1852
  explicit ThisFunction(Isolate* isolate) : Expression(isolate) {}
1853
  DECLARE_NODE_TYPE(ThisFunction)
1854
  virtual bool IsInlineable() const;
1855 1856 1857
};


1858 1859 1860 1861
// ----------------------------------------------------------------------------
// Regular expressions


1862 1863 1864 1865 1866 1867 1868 1869 1870 1871
class RegExpVisitor BASE_EMBEDDED {
 public:
  virtual ~RegExpVisitor() { }
#define MAKE_CASE(Name)                                              \
  virtual void* Visit##Name(RegExp##Name*, void* data) = 0;
  FOR_EACH_REG_EXP_TREE_TYPE(MAKE_CASE)
#undef MAKE_CASE
};


1872 1873
class RegExpTree: public ZoneObject {
 public:
1874
  static const int kInfinity = kMaxInt;
1875 1876 1877
  virtual ~RegExpTree() { }
  virtual void* Accept(RegExpVisitor* visitor, void* data) = 0;
  virtual RegExpNode* ToNode(RegExpCompiler* compiler,
erik.corry@gmail.com's avatar
erik.corry@gmail.com committed
1878
                             RegExpNode* on_success) = 0;
1879
  virtual bool IsTextElement() { return false; }
1880 1881
  virtual bool IsAnchoredAtStart() { return false; }
  virtual bool IsAnchoredAtEnd() { return false; }
1882 1883
  virtual int min_match() = 0;
  virtual int max_match() = 0;
1884 1885 1886
  // Returns the interval of registers used for captures within this
  // expression.
  virtual Interval CaptureRegisters() { return Interval::Empty(); }
1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898
  virtual void AppendToText(RegExpText* text);
  SmartPointer<const char> ToString();
#define MAKE_ASTYPE(Name)                                                  \
  virtual RegExp##Name* As##Name();                                        \
  virtual bool Is##Name();
  FOR_EACH_REG_EXP_TREE_TYPE(MAKE_ASTYPE)
#undef MAKE_ASTYPE
};


class RegExpDisjunction: public RegExpTree {
 public:
1899
  explicit RegExpDisjunction(ZoneList<RegExpTree*>* alternatives);
1900 1901
  virtual void* Accept(RegExpVisitor* visitor, void* data);
  virtual RegExpNode* ToNode(RegExpCompiler* compiler,
erik.corry@gmail.com's avatar
erik.corry@gmail.com committed
1902
                             RegExpNode* on_success);
1903
  virtual RegExpDisjunction* AsDisjunction();
1904
  virtual Interval CaptureRegisters();
1905
  virtual bool IsDisjunction();
1906 1907
  virtual bool IsAnchoredAtStart();
  virtual bool IsAnchoredAtEnd();
1908 1909
  virtual int min_match() { return min_match_; }
  virtual int max_match() { return max_match_; }
1910 1911 1912
  ZoneList<RegExpTree*>* alternatives() { return alternatives_; }
 private:
  ZoneList<RegExpTree*>* alternatives_;
1913 1914
  int min_match_;
  int max_match_;
1915 1916 1917 1918 1919
};


class RegExpAlternative: public RegExpTree {
 public:
1920
  explicit RegExpAlternative(ZoneList<RegExpTree*>* nodes);
1921 1922
  virtual void* Accept(RegExpVisitor* visitor, void* data);
  virtual RegExpNode* ToNode(RegExpCompiler* compiler,
erik.corry@gmail.com's avatar
erik.corry@gmail.com committed
1923
                             RegExpNode* on_success);
1924
  virtual RegExpAlternative* AsAlternative();
1925
  virtual Interval CaptureRegisters();
1926
  virtual bool IsAlternative();
1927 1928
  virtual bool IsAnchoredAtStart();
  virtual bool IsAnchoredAtEnd();
1929 1930
  virtual int min_match() { return min_match_; }
  virtual int max_match() { return max_match_; }
1931 1932 1933
  ZoneList<RegExpTree*>* nodes() { return nodes_; }
 private:
  ZoneList<RegExpTree*>* nodes_;
1934 1935
  int min_match_;
  int max_match_;
1936 1937 1938 1939 1940 1941
};


class RegExpAssertion: public RegExpTree {
 public:
  enum Type {
1942 1943 1944 1945 1946 1947
    START_OF_LINE,
    START_OF_INPUT,
    END_OF_LINE,
    END_OF_INPUT,
    BOUNDARY,
    NON_BOUNDARY
1948 1949 1950 1951
  };
  explicit RegExpAssertion(Type type) : type_(type) { }
  virtual void* Accept(RegExpVisitor* visitor, void* data);
  virtual RegExpNode* ToNode(RegExpCompiler* compiler,
erik.corry@gmail.com's avatar
erik.corry@gmail.com committed
1952
                             RegExpNode* on_success);
1953 1954
  virtual RegExpAssertion* AsAssertion();
  virtual bool IsAssertion();
1955 1956
  virtual bool IsAnchoredAtStart();
  virtual bool IsAnchoredAtEnd();
1957 1958
  virtual int min_match() { return 0; }
  virtual int max_match() { return 0; }
1959 1960 1961 1962 1963 1964
  Type type() { return type_; }
 private:
  Type type_;
};


1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978
class CharacterSet BASE_EMBEDDED {
 public:
  explicit CharacterSet(uc16 standard_set_type)
      : ranges_(NULL),
        standard_set_type_(standard_set_type) {}
  explicit CharacterSet(ZoneList<CharacterRange>* ranges)
      : ranges_(ranges),
        standard_set_type_(0) {}
  ZoneList<CharacterRange>* ranges();
  uc16 standard_set_type() { return standard_set_type_; }
  void set_standard_set_type(uc16 special_set_type) {
    standard_set_type_ = special_set_type;
  }
  bool is_standard() { return standard_set_type_ != 0; }
1979
  void Canonicalize();
1980 1981 1982 1983 1984 1985 1986 1987
 private:
  ZoneList<CharacterRange>* ranges_;
  // If non-zero, the value represents a standard set (e.g., all whitespace
  // characters) without having to expand the ranges.
  uc16 standard_set_type_;
};


1988 1989 1990
class RegExpCharacterClass: public RegExpTree {
 public:
  RegExpCharacterClass(ZoneList<CharacterRange>* ranges, bool is_negated)
1991
      : set_(ranges),
1992
        is_negated_(is_negated) { }
1993
  explicit RegExpCharacterClass(uc16 type)
1994 1995
      : set_(type),
        is_negated_(false) { }
1996 1997
  virtual void* Accept(RegExpVisitor* visitor, void* data);
  virtual RegExpNode* ToNode(RegExpCompiler* compiler,
erik.corry@gmail.com's avatar
erik.corry@gmail.com committed
1998
                             RegExpNode* on_success);
1999 2000 2001
  virtual RegExpCharacterClass* AsCharacterClass();
  virtual bool IsCharacterClass();
  virtual bool IsTextElement() { return true; }
2002 2003
  virtual int min_match() { return 1; }
  virtual int max_match() { return 1; }
2004
  virtual void AppendToText(RegExpText* text);
2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017
  CharacterSet character_set() { return set_; }
  // TODO(lrn): Remove need for complex version if is_standard that
  // recognizes a mangled standard set and just do { return set_.is_special(); }
  bool is_standard();
  // Returns a value representing the standard character set if is_standard()
  // returns true.
  // Currently used values are:
  // s : unicode whitespace
  // S : unicode non-whitespace
  // w : ASCII word character (digit, letter, underscore)
  // W : non-ASCII word character
  // d : ASCII digit
  // D : non-ASCII digit
2018
  // . : non-unicode non-newline
2019 2020 2021
  // * : All characters
  uc16 standard_type() { return set_.standard_set_type(); }
  ZoneList<CharacterRange>* ranges() { return set_.ranges(); }
2022
  bool is_negated() { return is_negated_; }
2023

2024
 private:
2025
  CharacterSet set_;
2026 2027 2028 2029 2030 2031 2032 2033 2034
  bool is_negated_;
};


class RegExpAtom: public RegExpTree {
 public:
  explicit RegExpAtom(Vector<const uc16> data) : data_(data) { }
  virtual void* Accept(RegExpVisitor* visitor, void* data);
  virtual RegExpNode* ToNode(RegExpCompiler* compiler,
erik.corry@gmail.com's avatar
erik.corry@gmail.com committed
2035
                             RegExpNode* on_success);
2036 2037 2038
  virtual RegExpAtom* AsAtom();
  virtual bool IsAtom();
  virtual bool IsTextElement() { return true; }
2039 2040
  virtual int min_match() { return data_.length(); }
  virtual int max_match() { return data_.length(); }
2041 2042
  virtual void AppendToText(RegExpText* text);
  Vector<const uc16> data() { return data_; }
2043
  int length() { return data_.length(); }
2044 2045 2046 2047 2048
 private:
  Vector<const uc16> data_;
};


2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063
class RegExpText: public RegExpTree {
 public:
  RegExpText() : elements_(2), length_(0) {}
  virtual void* Accept(RegExpVisitor* visitor, void* data);
  virtual RegExpNode* ToNode(RegExpCompiler* compiler,
                             RegExpNode* on_success);
  virtual RegExpText* AsText();
  virtual bool IsText();
  virtual bool IsTextElement() { return true; }
  virtual int min_match() { return length_; }
  virtual int max_match() { return length_; }
  virtual void AppendToText(RegExpText* text);
  void AddElement(TextElement elm)  {
    elements_.Add(elm);
    length_ += elm.length();
2064
  }
2065 2066 2067 2068 2069 2070 2071
  ZoneList<TextElement>* elements() { return &elements_; }
 private:
  ZoneList<TextElement> elements_;
  int length_;
};


2072 2073
class RegExpQuantifier: public RegExpTree {
 public:
2074 2075 2076 2077
  enum Type { GREEDY, NON_GREEDY, POSSESSIVE };
  RegExpQuantifier(int min, int max, Type type, RegExpTree* body)
      : body_(body),
        min_(min),
2078
        max_(max),
2079
        min_match_(min * body->min_match()),
2080
        type_(type) {
2081 2082 2083 2084 2085 2086
    if (max > 0 && body->max_match() > kInfinity / max) {
      max_match_ = kInfinity;
    } else {
      max_match_ = max * body->max_match();
    }
  }
2087 2088
  virtual void* Accept(RegExpVisitor* visitor, void* data);
  virtual RegExpNode* ToNode(RegExpCompiler* compiler,
erik.corry@gmail.com's avatar
erik.corry@gmail.com committed
2089
                             RegExpNode* on_success);
2090 2091 2092 2093 2094
  static RegExpNode* ToNode(int min,
                            int max,
                            bool is_greedy,
                            RegExpTree* body,
                            RegExpCompiler* compiler,
2095 2096
                            RegExpNode* on_success,
                            bool not_at_start = false);
2097
  virtual RegExpQuantifier* AsQuantifier();
2098
  virtual Interval CaptureRegisters();
2099
  virtual bool IsQuantifier();
2100 2101
  virtual int min_match() { return min_match_; }
  virtual int max_match() { return max_match_; }
2102 2103
  int min() { return min_; }
  int max() { return max_; }
2104 2105 2106
  bool is_possessive() { return type_ == POSSESSIVE; }
  bool is_non_greedy() { return type_ == NON_GREEDY; }
  bool is_greedy() { return type_ == GREEDY; }
2107
  RegExpTree* body() { return body_; }
2108

2109
 private:
2110
  RegExpTree* body_;
2111 2112
  int min_;
  int max_;
2113 2114
  int min_match_;
  int max_match_;
2115
  Type type_;
2116 2117 2118 2119 2120 2121
};


class RegExpCapture: public RegExpTree {
 public:
  explicit RegExpCapture(RegExpTree* body, int index)
2122
      : body_(body), index_(index) { }
2123 2124
  virtual void* Accept(RegExpVisitor* visitor, void* data);
  virtual RegExpNode* ToNode(RegExpCompiler* compiler,
erik.corry@gmail.com's avatar
erik.corry@gmail.com committed
2125
                             RegExpNode* on_success);
2126 2127 2128
  static RegExpNode* ToNode(RegExpTree* body,
                            int index,
                            RegExpCompiler* compiler,
erik.corry@gmail.com's avatar
erik.corry@gmail.com committed
2129
                            RegExpNode* on_success);
2130
  virtual RegExpCapture* AsCapture();
2131 2132
  virtual bool IsAnchoredAtStart();
  virtual bool IsAnchoredAtEnd();
2133
  virtual Interval CaptureRegisters();
2134
  virtual bool IsCapture();
2135 2136
  virtual int min_match() { return body_->min_match(); }
  virtual int max_match() { return body_->max_match(); }
2137 2138 2139 2140
  RegExpTree* body() { return body_; }
  int index() { return index_; }
  static int StartRegister(int index) { return index * 2; }
  static int EndRegister(int index) { return index * 2 + 1; }
2141

2142 2143 2144 2145 2146 2147 2148 2149
 private:
  RegExpTree* body_;
  int index_;
};


class RegExpLookahead: public RegExpTree {
 public:
2150 2151 2152 2153
  RegExpLookahead(RegExpTree* body,
                  bool is_positive,
                  int capture_count,
                  int capture_from)
2154
      : body_(body),
2155 2156 2157 2158
        is_positive_(is_positive),
        capture_count_(capture_count),
        capture_from_(capture_from) { }

2159 2160
  virtual void* Accept(RegExpVisitor* visitor, void* data);
  virtual RegExpNode* ToNode(RegExpCompiler* compiler,
erik.corry@gmail.com's avatar
erik.corry@gmail.com committed
2161
                             RegExpNode* on_success);
2162
  virtual RegExpLookahead* AsLookahead();
2163
  virtual Interval CaptureRegisters();
2164
  virtual bool IsLookahead();
2165
  virtual bool IsAnchoredAtStart();
2166 2167
  virtual int min_match() { return 0; }
  virtual int max_match() { return 0; }
2168 2169
  RegExpTree* body() { return body_; }
  bool is_positive() { return is_positive_; }
2170 2171
  int capture_count() { return capture_count_; }
  int capture_from() { return capture_from_; }
2172

2173 2174 2175
 private:
  RegExpTree* body_;
  bool is_positive_;
2176 2177
  int capture_count_;
  int capture_from_;
2178 2179 2180 2181 2182 2183
};


class RegExpBackReference: public RegExpTree {
 public:
  explicit RegExpBackReference(RegExpCapture* capture)
2184
      : capture_(capture) { }
2185 2186
  virtual void* Accept(RegExpVisitor* visitor, void* data);
  virtual RegExpNode* ToNode(RegExpCompiler* compiler,
erik.corry@gmail.com's avatar
erik.corry@gmail.com committed
2187
                             RegExpNode* on_success);
2188 2189
  virtual RegExpBackReference* AsBackReference();
  virtual bool IsBackReference();
2190
  virtual int min_match() { return 0; }
2191
  virtual int max_match() { return capture_->max_match(); }
2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203
  int index() { return capture_->index(); }
  RegExpCapture* capture() { return capture_; }
 private:
  RegExpCapture* capture_;
};


class RegExpEmpty: public RegExpTree {
 public:
  RegExpEmpty() { }
  virtual void* Accept(RegExpVisitor* visitor, void* data);
  virtual RegExpNode* ToNode(RegExpCompiler* compiler,
erik.corry@gmail.com's avatar
erik.corry@gmail.com committed
2204
                             RegExpNode* on_success);
2205 2206
  virtual RegExpEmpty* AsEmpty();
  virtual bool IsEmpty();
2207 2208
  virtual int min_match() { return 0; }
  virtual int max_match() { return 0; }
2209 2210 2211 2212 2213 2214
  static RegExpEmpty* GetInstance() { return &kInstance; }
 private:
  static RegExpEmpty kInstance;
};


2215 2216 2217 2218
// ----------------------------------------------------------------------------
// Basic visitor
// - leaf node visitors are abstract.

2219
class AstVisitor BASE_EMBEDDED {
2220
 public:
2221
  AstVisitor() : isolate_(Isolate::Current()), stack_overflow_(false) { }
2222
  virtual ~AstVisitor() { }
2223

2224 2225
  // Stack overflow check and dynamic dispatch.
  void Visit(AstNode* node) { if (!CheckStackOverflow()) node->Accept(this); }
2226

2227
  // Iteration left-to-right.
2228
  virtual void VisitDeclarations(ZoneList<Declaration*>* declarations);
2229 2230 2231 2232 2233
  virtual void VisitStatements(ZoneList<Statement*>* statements);
  virtual void VisitExpressions(ZoneList<Expression*>* expressions);

  // Stack overflow tracking support.
  bool HasStackOverflow() const { return stack_overflow_; }
2234
  bool CheckStackOverflow();
2235

2236 2237 2238 2239
  // If a stack-overflow exception is encountered when visiting a
  // node, calling SetStackOverflow will make sure that the visitor
  // bails out without visiting more nodes.
  void SetStackOverflow() { stack_overflow_ = true; }
2240 2241 2242 2243
  void ClearStackOverflow() { stack_overflow_ = false; }

  // Nodes not appearing in the AST, including slots.
  virtual void VisitSlot(Slot* node) { UNREACHABLE(); }
2244

2245
  // Individual AST nodes.
2246 2247
#define DEF_VISIT(type)                         \
  virtual void Visit##type(type* node) = 0;
2248
  AST_NODE_LIST(DEF_VISIT)
2249 2250
#undef DEF_VISIT

2251 2252 2253
 protected:
  Isolate* isolate() { return isolate_; }

2254
 private:
2255
  Isolate* isolate_;
2256 2257 2258
  bool stack_overflow_;
};

2259

2260 2261 2262
} }  // namespace v8::internal

#endif  // V8_AST_H_