ast.h 105 KB
Newer Older
1
// Copyright 2012 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4 5 6 7

#ifndef V8_AST_H_
#define V8_AST_H_

8
#include "src/v8.h"
9

10
#include "src/assembler.h"
11
#include "src/ast-value-factory.h"
12 13
#include "src/factory.h"
#include "src/feedback-slots.h"
14
#include "src/interface.h"
15 16 17
#include "src/isolate.h"
#include "src/jsregexp.h"
#include "src/list-inl.h"
18
#include "src/ostreams.h"
19 20 21 22 23 24 25 26
#include "src/runtime.h"
#include "src/small-pointer-list.h"
#include "src/smart-pointers.h"
#include "src/token.h"
#include "src/types.h"
#include "src/utils.h"
#include "src/variables.h"
#include "src/zone-inl.h"
27

28 29
namespace v8 {
namespace internal {
30 31 32 33 34 35 36 37 38 39 40 41 42 43

// 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.

44 45
#define DECLARATION_NODE_LIST(V)                \
  V(VariableDeclaration)                        \
46
  V(FunctionDeclaration)                        \
47
  V(ModuleDeclaration)                          \
48 49
  V(ImportDeclaration)                          \
  V(ExportDeclaration)                          \
50 51 52 53 54 55

#define MODULE_NODE_LIST(V)                     \
  V(ModuleLiteral)                              \
  V(ModuleVariable)                             \
  V(ModulePath)                                 \
  V(ModuleUrl)
56

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

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

100
#define AST_NODE_LIST(V)                        \
101
  DECLARATION_NODE_LIST(V)                      \
102
  MODULE_NODE_LIST(V)                           \
103
  STATEMENT_NODE_LIST(V)                        \
104
  EXPRESSION_NODE_LIST(V)
105

106
// Forward declarations
107 108
class AstConstructionVisitor;
template<class> class AstNodeFactory;
109
class AstVisitor;
110
class Declaration;
111
class Module;
112 113 114
class BreakableStatement;
class Expression;
class IterationStatement;
115
class MaterializedLiteral;
116
class Statement;
117 118
class TargetCollector;
class TypeFeedbackOracle;
119

120 121 122 123 124 125 126 127 128 129 130 131 132
class RegExpAlternative;
class RegExpAssertion;
class RegExpAtom;
class RegExpBackReference;
class RegExpCapture;
class RegExpCharacterClass;
class RegExpCompiler;
class RegExpDisjunction;
class RegExpEmpty;
class RegExpLookahead;
class RegExpQuantifier;
class RegExpText;

133
#define DEF_FORWARD_DECLARATION(type) class type;
134
AST_NODE_LIST(DEF_FORWARD_DECLARATION)
135 136 137 138 139 140
#undef DEF_FORWARD_DECLARATION


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


144 145 146 147 148
#define DECLARE_NODE_TYPE(type)                                 \
  virtual void Accept(AstVisitor* v) V8_OVERRIDE;                  \
  virtual AstNode::NodeType node_type() const V8_FINAL V8_OVERRIDE {  \
    return AstNode::k##type;                                    \
  }                                                             \
149
  template<class> friend class AstNodeFactory;
150 151 152 153


enum AstPropertiesFlag {
  kDontSelfOptimize,
154 155
  kDontSoftInline,
  kDontCache
156 157 158
};


159
class AstProperties V8_FINAL BASE_EMBEDDED {
160 161 162
 public:
  class Flags : public EnumSet<AstPropertiesFlag, int> {};

163
AstProperties() : node_count_(0), feedback_slots_(0) {}
164 165 166 167 168

  Flags* flags() { return &flags_; }
  int node_count() { return node_count_; }
  void add_node_count(int count) { node_count_ += count; }

169 170 171 172 173
  int feedback_slots() const { return feedback_slots_; }
  void increase_feedback_slots(int count) {
    feedback_slots_ += count;
  }

174 175 176
 private:
  Flags flags_;
  int node_count_;
177
  int feedback_slots_;
178 179 180
};


181
class AstNode: public ZoneObject {
182
 public:
183
#define DECLARE_TYPE_ENUM(type) k##type,
184
  enum NodeType {
185 186 187 188 189
    AST_NODE_LIST(DECLARE_TYPE_ENUM)
    kInvalid = -1
  };
#undef DECLARE_TYPE_ENUM

190
  void* operator new(size_t size, Zone* zone) {
vitalyr@chromium.org's avatar
vitalyr@chromium.org committed
191
    return zone->New(static_cast<int>(size));
192
  }
193

194
  explicit AstNode(int position): position_(position) {}
195
  virtual ~AstNode() {}
196

197
  virtual void Accept(AstVisitor* v) = 0;
198
  virtual NodeType node_type() const = 0;
199
  int position() const { return position_; }
200 201

  // Type testing & conversion functions overridden by concrete subclasses.
202 203 204 205 206 207 208 209
#define DECLARE_NODE_FUNCTIONS(type) \
  bool Is##type() const { return node_type() == AstNode::k##type; } \
  type* As##type() { \
    return Is##type() ? reinterpret_cast<type*>(this) : NULL; \
  } \
  const type* As##type() const { \
    return Is##type() ? reinterpret_cast<const type*>(this) : NULL; \
  }
210 211
  AST_NODE_LIST(DECLARE_NODE_FUNCTIONS)
#undef DECLARE_NODE_FUNCTIONS
212

213
  virtual TargetCollector* AsTargetCollector() { return NULL; }
214 215
  virtual BreakableStatement* AsBreakableStatement() { return NULL; }
  virtual IterationStatement* AsIterationStatement() { return NULL; }
216
  virtual MaterializedLiteral* AsMaterializedLiteral() { return NULL; }
217 218

 protected:
219 220
  static int GetNextId(Zone* zone) {
    return ReserveIdRange(zone, 1);
221 222
  }

223 224 225
  static int ReserveIdRange(Zone* zone, int n) {
    int tmp = zone->isolate()->ast_node_id();
    zone->isolate()->set_ast_node_id(tmp + n);
226 227 228
    return tmp;
  }

229 230 231 232 233 234
  // Some nodes re-use bailout IDs for type feedback.
  static TypeFeedbackId reuse(BailoutId id) {
    return TypeFeedbackId(id.ToInt());
  }


235 236 237 238 239
 private:
  // Hidden to prevent accidental usage. It would have to load the
  // current zone from the TLS.
  void* operator new(size_t size);

240
  friend class CaseClause;  // Generates AST IDs.
241 242

  int position_;
243 244 245
};


246
class Statement : public AstNode {
247
 public:
248
  explicit Statement(Zone* zone, int position) : AstNode(position) {}
249

250
  bool IsEmpty() { return AsEmptyStatement() != NULL; }
251
  virtual bool IsJump() const { return false; }
252 253 254
};


255
class SmallMapList V8_FINAL {
256 257
 public:
  SmallMapList() {}
258
  SmallMapList(int capacity, Zone* zone) : list_(capacity, zone) {}
259

260
  void Reserve(int capacity, Zone* zone) { list_.Reserve(capacity, zone); }
261
  void Clear() { list_.Clear(); }
262
  void Sort() { list_.Sort(); }
263 264 265 266

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

267
  void AddMapIfMissing(Handle<Map> map, Zone* zone) {
268
    if (!Map::TryUpdate(map).ToHandle(&map)) return;
269 270 271 272 273 274
    for (int i = 0; i < length(); ++i) {
      if (at(i).is_identical_to(map)) return;
    }
    Add(map, zone);
  }

275 276 277 278 279 280 281 282
  void FilterForPossibleTransitions(Map* root_map) {
    for (int i = list_.length() - 1; i >= 0; i--) {
      if (at(i)->FindRootMap() != root_map) {
        list_.RemoveElement(list_.at(i));
      }
    }
  }

283 284
  void Add(Handle<Map> handle, Zone* zone) {
    list_.Add(handle.location(), zone);
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
  }

  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);
};


302
class Expression : public AstNode {
303
 public:
304 305 306 307 308 309 310 311 312 313 314 315
  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
  };

316
  virtual bool IsValidReferenceExpression() const { return false; }
317

318
  // Helpers for ToBoolean conversion.
319 320
  virtual bool ToBooleanIsTrue() const { return false; }
  virtual bool ToBooleanIsFalse() const { return false; }
321

322 323 324
  // 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.
325
  virtual bool IsPropertyName() const { return false; }
326

327 328
  // True iff the result can be safely overwritten (to avoid allocation).
  // False for operations that can return one of their operands.
329
  virtual bool ResultOverwriteAllowed() const { return false; }
330

331
  // True iff the expression is a literal represented as a smi.
332
  bool IsSmiLiteral() const;
333

334
  // True iff the expression is a string literal.
335
  bool IsStringLiteral() const;
336 337

  // True iff the expression is the null literal.
338
  bool IsNullLiteral() const;
339

340
  // True if we can prove that the expression is the undefined literal.
341
  bool IsUndefinedLiteral(Isolate* isolate) const;
342

343
  // Expression type bounds
344
  Bounds bounds() const { return bounds_; }
345
  void set_bounds(Bounds bounds) { bounds_ = bounds; }
346

347 348 349 350 351
  // Whether the expression is parenthesized
  unsigned parenthesization_level() const { return parenthesization_level_; }
  bool is_parenthesized() const { return parenthesization_level_ > 0; }
  void increase_parenthesization_level() { ++parenthesization_level_; }

352 353 354 355 356
  // Type feedback information for assignments and properties.
  virtual bool IsMonomorphic() {
    UNREACHABLE();
    return false;
  }
357
  virtual SmallMapList* GetReceiverTypes() {
358 359 360
    UNREACHABLE();
    return NULL;
  }
361 362 363 364
  virtual KeyedAccessStoreMode GetStoreMode() {
    UNREACHABLE();
    return STANDARD_STORE;
  }
365

366
  // TODO(rossberg): this should move to its own AST node eventually.
367
  virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle);
368 369
  byte to_boolean_types() const { return to_boolean_types_; }

370 371
  BailoutId id() const { return id_; }
  TypeFeedbackId test_id() const { return test_id_; }
372

373
 protected:
374
  Expression(Zone* zone, int pos)
375
      : AstNode(pos),
376
        zone_(zone),
377
        bounds_(Bounds::Unbounded(zone)),
378
        parenthesization_level_(0),
379 380
        id_(GetNextId(zone)),
        test_id_(GetNextId(zone)) {}
381
  void set_to_boolean_types(byte types) { to_boolean_types_ = types; }
382

383 384
  Zone* zone_;

385
 private:
386
  Bounds bounds_;
387
  byte to_boolean_types_;
388
  unsigned parenthesization_level_;
389

390 391
  const BailoutId id_;
  const TypeFeedbackId test_id_;
392 393 394
};


395
class BreakableStatement : public Statement {
396
 public:
397
  enum BreakableType {
398 399 400 401 402 403
    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.
404
  ZoneList<const AstRawString*>* labels() const { return labels_; }
405 406

  // Type testing & conversion.
407 408 409
  virtual BreakableStatement* AsBreakableStatement() V8_FINAL V8_OVERRIDE {
    return this;
  }
410 411

  // Code generation
412
  Label* break_target() { return &break_target_; }
413 414

  // Testers.
415 416 417
  bool is_target_for_anonymous() const {
    return breakable_type_ == TARGET_FOR_ANONYMOUS;
  }
418

419 420
  BailoutId EntryId() const { return entry_id_; }
  BailoutId ExitId() const { return exit_id_; }
421

422
 protected:
423
  BreakableStatement(
424
      Zone* zone, ZoneList<const AstRawString*>* labels,
425
      BreakableType breakable_type, int position)
426
      : Statement(zone, position),
427
        labels_(labels),
428
        breakable_type_(breakable_type),
429 430
        entry_id_(GetNextId(zone)),
        exit_id_(GetNextId(zone)) {
431 432 433
    ASSERT(labels == NULL || labels->length() > 0);
  }

434 435

 private:
436
  ZoneList<const AstRawString*>* labels_;
437
  BreakableType breakable_type_;
438
  Label break_target_;
439 440
  const BailoutId entry_id_;
  const BailoutId exit_id_;
441 442 443
};


444
class Block V8_FINAL : public BreakableStatement {
445
 public:
446
  DECLARE_NODE_TYPE(Block)
447

448 449 450
  void AddStatement(Statement* statement, Zone* zone) {
    statements_.Add(statement, zone);
  }
451 452

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

455 456
  BailoutId DeclsId() const { return decls_id_; }

457
  virtual bool IsJump() const V8_OVERRIDE {
458 459 460 461
    return !statements_.is_empty() && statements_.last()->IsJump()
        && labels() == NULL;  // Good enough as an approximation...
  }

462 463
  Scope* scope() const { return scope_; }
  void set_scope(Scope* scope) { scope_ = scope; }
464

465
 protected:
466
  Block(Zone* zone,
467
        ZoneList<const AstRawString*>* labels,
468
        int capacity,
469
        bool is_initializer_block,
470 471
        int pos)
      : BreakableStatement(zone, labels, TARGET_FOR_NAMED_ONLY, pos),
472
        statements_(capacity, zone),
473
        is_initializer_block_(is_initializer_block),
474
        decls_id_(GetNextId(zone)),
475
        scope_(NULL) {
476 477
  }

478 479 480
 private:
  ZoneList<Statement*> statements_;
  bool is_initializer_block_;
481
  const BailoutId decls_id_;
482
  Scope* scope_;
483 484 485
};


486
class Declaration : public AstNode {
487
 public:
488 489 490
  VariableProxy* proxy() const { return proxy_; }
  VariableMode mode() const { return mode_; }
  Scope* scope() const { return scope_; }
491
  virtual InitializationFlag initialization() const = 0;
492
  virtual bool IsInlineable() const;
493

494
 protected:
495 496
  Declaration(Zone* zone,
              VariableProxy* proxy,
497
              VariableMode mode,
498 499 500 501
              Scope* scope,
              int pos)
      : AstNode(pos),
        proxy_(proxy),
502
        mode_(mode),
503
        scope_(scope) {
504
    ASSERT(IsDeclaredVariableMode(mode));
505 506 507 508
  }

 private:
  VariableProxy* proxy_;
509
  VariableMode mode_;
510 511 512

  // Nested scope from which the declaration originated.
  Scope* scope_;
513 514 515
};


516
class VariableDeclaration V8_FINAL : public Declaration {
517 518 519
 public:
  DECLARE_NODE_TYPE(VariableDeclaration)

520
  virtual InitializationFlag initialization() const V8_OVERRIDE {
521 522 523 524
    return mode() == VAR ? kCreatedInitialized : kNeedsInitialization;
  }

 protected:
525 526
  VariableDeclaration(Zone* zone,
                      VariableProxy* proxy,
527
                      VariableMode mode,
528 529
                      Scope* scope,
                      int pos)
530
      : Declaration(zone, proxy, mode, scope, pos) {
531 532 533
  }
};

534

535
class FunctionDeclaration V8_FINAL : public Declaration {
536 537 538 539
 public:
  DECLARE_NODE_TYPE(FunctionDeclaration)

  FunctionLiteral* fun() const { return fun_; }
540
  virtual InitializationFlag initialization() const V8_OVERRIDE {
541 542
    return kCreatedInitialized;
  }
543
  virtual bool IsInlineable() const V8_OVERRIDE;
544 545

 protected:
546 547
  FunctionDeclaration(Zone* zone,
                      VariableProxy* proxy,
548 549
                      VariableMode mode,
                      FunctionLiteral* fun,
550 551
                      Scope* scope,
                      int pos)
552
      : Declaration(zone, proxy, mode, scope, pos),
553
        fun_(fun) {
554 555 556
    // At the moment there are no "const functions" in JavaScript...
    ASSERT(mode == VAR || mode == LET);
    ASSERT(fun != NULL);
557 558 559 560 561 562 563
  }

 private:
  FunctionLiteral* fun_;
};


564
class ModuleDeclaration V8_FINAL : public Declaration {
565 566 567 568
 public:
  DECLARE_NODE_TYPE(ModuleDeclaration)

  Module* module() const { return module_; }
569
  virtual InitializationFlag initialization() const V8_OVERRIDE {
570 571
    return kCreatedInitialized;
  }
572 573

 protected:
574 575
  ModuleDeclaration(Zone* zone,
                    VariableProxy* proxy,
576
                    Module* module,
577 578
                    Scope* scope,
                    int pos)
579
      : Declaration(zone, proxy, MODULE, scope, pos),
580 581 582 583 584 585 586 587
        module_(module) {
  }

 private:
  Module* module_;
};


588
class ImportDeclaration V8_FINAL : public Declaration {
589 590 591 592
 public:
  DECLARE_NODE_TYPE(ImportDeclaration)

  Module* module() const { return module_; }
593
  virtual InitializationFlag initialization() const V8_OVERRIDE {
594 595 596 597
    return kCreatedInitialized;
  }

 protected:
598 599
  ImportDeclaration(Zone* zone,
                    VariableProxy* proxy,
600
                    Module* module,
601 602
                    Scope* scope,
                    int pos)
603
      : Declaration(zone, proxy, LET, scope, pos),
604 605 606 607 608 609 610 611
        module_(module) {
  }

 private:
  Module* module_;
};


612
class ExportDeclaration V8_FINAL : public Declaration {
613 614 615
 public:
  DECLARE_NODE_TYPE(ExportDeclaration)

616
  virtual InitializationFlag initialization() const V8_OVERRIDE {
617 618 619 620
    return kCreatedInitialized;
  }

 protected:
621 622
  ExportDeclaration(Zone* zone, VariableProxy* proxy, Scope* scope, int pos)
      : Declaration(zone, proxy, LET, scope, pos) {}
623 624 625
};


626
class Module : public AstNode {
627 628
 public:
  Interface* interface() const { return interface_; }
629
  Block* body() const { return body_; }
630

631
 protected:
632 633 634
  Module(Zone* zone, int pos)
      : AstNode(pos),
        interface_(Interface::NewModule(zone)),
635
        body_(NULL) {}
636
  Module(Zone* zone, Interface* interface, int pos, Block* body = NULL)
637 638
      : AstNode(pos),
        interface_(interface),
639
        body_(body) {}
640 641 642

 private:
  Interface* interface_;
643
  Block* body_;
644 645 646
};


647
class ModuleLiteral V8_FINAL : public Module {
648 649 650 651
 public:
  DECLARE_NODE_TYPE(ModuleLiteral)

 protected:
652 653
  ModuleLiteral(Zone* zone, Block* body, Interface* interface, int pos)
      : Module(zone, interface, pos, body) {}
654 655 656
};


657
class ModuleVariable V8_FINAL : public Module {
658 659 660
 public:
  DECLARE_NODE_TYPE(ModuleVariable)

661
  VariableProxy* proxy() const { return proxy_; }
662 663

 protected:
664
  inline ModuleVariable(Zone* zone, VariableProxy* proxy, int pos);
665 666

 private:
667
  VariableProxy* proxy_;
668 669 670
};


671
class ModulePath V8_FINAL : public Module {
672 673 674 675
 public:
  DECLARE_NODE_TYPE(ModulePath)

  Module* module() const { return module_; }
676
  Handle<String> name() const { return name_->string(); }
677 678

 protected:
679 680
  ModulePath(Zone* zone, Module* module, const AstRawString* name, int pos)
      : Module(zone, pos), module_(module), name_(name) {}
681 682 683

 private:
  Module* module_;
684
  const AstRawString* name_;
685 686 687
};


688
class ModuleUrl V8_FINAL : public Module {
689 690 691 692 693 694
 public:
  DECLARE_NODE_TYPE(ModuleUrl)

  Handle<String> url() const { return url_; }

 protected:
695
  ModuleUrl(Zone* zone, Handle<String> url, int pos)
696
      : Module(zone, pos), url_(url) {
697 698 699 700 701 702 703
  }

 private:
  Handle<String> url_;
};


704
class ModuleStatement V8_FINAL : public Statement {
705 706 707 708 709 710 711
 public:
  DECLARE_NODE_TYPE(ModuleStatement)

  VariableProxy* proxy() const { return proxy_; }
  Block* body() const { return body_; }

 protected:
712 713
  ModuleStatement(Zone* zone, VariableProxy* proxy, Block* body, int pos)
      : Statement(zone, pos),
714
        proxy_(proxy),
715 716 717 718 719 720 721 722 723
        body_(body) {
  }

 private:
  VariableProxy* proxy_;
  Block* body_;
};


724
class IterationStatement : public BreakableStatement {
725 726
 public:
  // Type testing & conversion.
727 728 729
  virtual IterationStatement* AsIterationStatement() V8_FINAL V8_OVERRIDE {
    return this;
  }
730 731 732

  Statement* body() const { return body_; }

733 734 735
  BailoutId OsrEntryId() const { return osr_entry_id_; }
  virtual BailoutId ContinueId() const = 0;
  virtual BailoutId StackCheckId() const = 0;
736

737
  // Code generation
738
  Label* continue_target()  { return &continue_target_; }
739 740

 protected:
741
  IterationStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
742
      : BreakableStatement(zone, labels, TARGET_FOR_ANONYMOUS, pos),
743
        body_(NULL),
744
        osr_entry_id_(GetNextId(zone)) {
745
  }
746 747 748 749 750 751 752

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

 private:
  Statement* body_;
753
  Label continue_target_;
754

755
  const BailoutId osr_entry_id_;
756 757 758
};


759
class DoWhileStatement V8_FINAL : public IterationStatement {
760
 public:
761 762
  DECLARE_NODE_TYPE(DoWhileStatement)

763 764 765 766
  void Initialize(Expression* cond, Statement* body) {
    IterationStatement::Initialize(body);
    cond_ = cond;
  }
767

768 769
  Expression* cond() const { return cond_; }

770 771
  virtual BailoutId ContinueId() const V8_OVERRIDE { return continue_id_; }
  virtual BailoutId StackCheckId() const V8_OVERRIDE { return back_edge_id_; }
772
  BailoutId BackEdgeId() const { return back_edge_id_; }
773

774
 protected:
775
  DoWhileStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
776
      : IterationStatement(zone, labels, pos),
777
        cond_(NULL),
778 779
        continue_id_(GetNextId(zone)),
        back_edge_id_(GetNextId(zone)) {
780
  }
781

782 783
 private:
  Expression* cond_;
784

785 786
  const BailoutId continue_id_;
  const BailoutId back_edge_id_;
787 788 789
};


790
class WhileStatement V8_FINAL : public IterationStatement {
791
 public:
792 793
  DECLARE_NODE_TYPE(WhileStatement)

794 795 796 797 798 799 800 801 802
  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_;
  }
803 804 805
  void set_may_have_function_literal(bool value) {
    may_have_function_literal_ = value;
  }
806

807 808
  virtual BailoutId ContinueId() const V8_OVERRIDE { return EntryId(); }
  virtual BailoutId StackCheckId() const V8_OVERRIDE { return body_id_; }
809
  BailoutId BodyId() const { return body_id_; }
810

811
 protected:
812
  WhileStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
813
      : IterationStatement(zone, labels, pos),
814 815
        cond_(NULL),
        may_have_function_literal_(true),
816
        body_id_(GetNextId(zone)) {
817 818
  }

819 820
 private:
  Expression* cond_;
821

822 823
  // True if there is a function literal subexpression in the condition.
  bool may_have_function_literal_;
824

825
  const BailoutId body_id_;
826 827 828
};


829
class ForStatement V8_FINAL : public IterationStatement {
830
 public:
831
  DECLARE_NODE_TYPE(ForStatement)
832 833 834 835 836 837 838 839 840 841 842

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

843 844 845
  Statement* init() const { return init_; }
  Expression* cond() const { return cond_; }
  Statement* next() const { return next_; }
846

847 848 849
  bool may_have_function_literal() const {
    return may_have_function_literal_;
  }
850 851 852
  void set_may_have_function_literal(bool value) {
    may_have_function_literal_ = value;
  }
853

854 855
  virtual BailoutId ContinueId() const V8_OVERRIDE { return continue_id_; }
  virtual BailoutId StackCheckId() const V8_OVERRIDE { return body_id_; }
856
  BailoutId BodyId() const { return body_id_; }
857

858 859 860
  bool is_fast_smi_loop() { return loop_variable_ != NULL; }
  Variable* loop_variable() { return loop_variable_; }
  void set_loop_variable(Variable* var) { loop_variable_ = var; }
861 862

 protected:
863
  ForStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
864
      : IterationStatement(zone, labels, pos),
865 866 867 868 869
        init_(NULL),
        cond_(NULL),
        next_(NULL),
        may_have_function_literal_(true),
        loop_variable_(NULL),
870 871
        continue_id_(GetNextId(zone)),
        body_id_(GetNextId(zone)) {
872
  }
873

874 875 876 877
 private:
  Statement* init_;
  Expression* cond_;
  Statement* next_;
878

879
  // True if there is a function literal subexpression in the condition.
880
  bool may_have_function_literal_;
881
  Variable* loop_variable_;
882

883 884
  const BailoutId continue_id_;
  const BailoutId body_id_;
885 886 887
};


888
class ForEachStatement : public IterationStatement {
889
 public:
890 891 892 893
  enum VisitMode {
    ENUMERATE,   // for (each in subject) body;
    ITERATE      // for (each of subject) body;
  };
894

895
  void Initialize(Expression* each, Expression* subject, Statement* body) {
896 897
    IterationStatement::Initialize(body);
    each_ = each;
898
    subject_ = subject;
899 900 901
  }

  Expression* each() const { return each_; }
902
  Expression* subject() const { return subject_; }
903

904
 protected:
905 906
  ForEachStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
      : IterationStatement(zone, labels, pos), each_(NULL), subject_(NULL) {}
907

908 909
 private:
  Expression* each_;
910 911 912 913
  Expression* subject_;
};


914 915
class ForInStatement V8_FINAL : public ForEachStatement,
    public FeedbackSlotInterface {
916 917 918 919 920 921 922
 public:
  DECLARE_NODE_TYPE(ForInStatement)

  Expression* enumerable() const {
    return subject();
  }

923
  // Type feedback information.
924
  virtual int ComputeFeedbackSlotCount() { return 1; }
925 926 927 928 929 930 931
  virtual void SetFirstFeedbackSlot(int slot) { for_in_feedback_slot_ = slot; }

  int ForInFeedbackSlot() {
    ASSERT(for_in_feedback_slot_ != kInvalidFeedbackSlot);
    return for_in_feedback_slot_;
  }

932 933
  enum ForInType { FAST_FOR_IN, SLOW_FOR_IN };
  ForInType for_in_type() const { return for_in_type_; }
934
  void set_for_in_type(ForInType type) { for_in_type_ = type; }
935

936 937
  BailoutId BodyId() const { return body_id_; }
  BailoutId PrepareId() const { return prepare_id_; }
938 939
  virtual BailoutId ContinueId() const V8_OVERRIDE { return EntryId(); }
  virtual BailoutId StackCheckId() const V8_OVERRIDE { return body_id_; }
940

941
 protected:
942
  ForInStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
943
      : ForEachStatement(zone, labels, pos),
944
        for_in_type_(SLOW_FOR_IN),
945
        for_in_feedback_slot_(kInvalidFeedbackSlot),
946 947
        body_id_(GetNextId(zone)),
        prepare_id_(GetNextId(zone)) {
948
  }
949 950

  ForInType for_in_type_;
951
  int for_in_feedback_slot_;
952 953
  const BailoutId body_id_;
  const BailoutId prepare_id_;
954
};
955

956

957
class ForOfStatement V8_FINAL : public ForEachStatement {
958 959 960
 public:
  DECLARE_NODE_TYPE(ForOfStatement)

961 962 963 964 965 966 967 968 969 970 971 972 973 974
  void Initialize(Expression* each,
                  Expression* subject,
                  Statement* body,
                  Expression* assign_iterator,
                  Expression* next_result,
                  Expression* result_done,
                  Expression* assign_each) {
    ForEachStatement::Initialize(each, subject, body);
    assign_iterator_ = assign_iterator;
    next_result_ = next_result;
    result_done_ = result_done;
    assign_each_ = assign_each;
  }

975 976 977 978
  Expression* iterable() const {
    return subject();
  }

979
  // var iterator = subject[Symbol.iterator]();
980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998
  Expression* assign_iterator() const {
    return assign_iterator_;
  }

  // var result = iterator.next();
  Expression* next_result() const {
    return next_result_;
  }

  // result.done
  Expression* result_done() const {
    return result_done_;
  }

  // each = result.value
  Expression* assign_each() const {
    return assign_each_;
  }

999 1000
  virtual BailoutId ContinueId() const V8_OVERRIDE { return EntryId(); }
  virtual BailoutId StackCheckId() const V8_OVERRIDE { return BackEdgeId(); }
1001 1002 1003

  BailoutId BackEdgeId() const { return back_edge_id_; }

1004
 protected:
1005
  ForOfStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
1006
      : ForEachStatement(zone, labels, pos),
1007 1008 1009 1010
        assign_iterator_(NULL),
        next_result_(NULL),
        result_done_(NULL),
        assign_each_(NULL),
1011
        back_edge_id_(GetNextId(zone)) {
1012
  }
1013 1014 1015 1016 1017 1018

  Expression* assign_iterator_;
  Expression* next_result_;
  Expression* result_done_;
  Expression* assign_each_;
  const BailoutId back_edge_id_;
1019 1020 1021
};


1022
class ExpressionStatement V8_FINAL : public Statement {
1023
 public:
1024
  DECLARE_NODE_TYPE(ExpressionStatement)
1025 1026

  void set_expression(Expression* e) { expression_ = e; }
1027
  Expression* expression() const { return expression_; }
1028
  virtual bool IsJump() const V8_OVERRIDE { return expression_->IsThrow(); }
1029

1030
 protected:
1031 1032
  ExpressionStatement(Zone* zone, Expression* expression, int pos)
      : Statement(zone, pos), expression_(expression) { }
1033

1034 1035 1036 1037 1038
 private:
  Expression* expression_;
};


1039
class JumpStatement : public Statement {
1040
 public:
1041
  virtual bool IsJump() const V8_FINAL V8_OVERRIDE { return true; }
1042 1043

 protected:
1044
  explicit JumpStatement(Zone* zone, int pos) : Statement(zone, pos) {}
1045 1046 1047
};


1048
class ContinueStatement V8_FINAL : public JumpStatement {
1049
 public:
1050
  DECLARE_NODE_TYPE(ContinueStatement)
1051

1052
  IterationStatement* target() const { return target_; }
1053 1054

 protected:
1055 1056
  explicit ContinueStatement(Zone* zone, IterationStatement* target, int pos)
      : JumpStatement(zone, pos), target_(target) { }
1057 1058 1059 1060 1061 1062

 private:
  IterationStatement* target_;
};


1063
class BreakStatement V8_FINAL : public JumpStatement {
1064
 public:
1065
  DECLARE_NODE_TYPE(BreakStatement)
1066

1067
  BreakableStatement* target() const { return target_; }
1068 1069

 protected:
1070 1071
  explicit BreakStatement(Zone* zone, BreakableStatement* target, int pos)
      : JumpStatement(zone, pos), target_(target) { }
1072 1073 1074 1075 1076 1077

 private:
  BreakableStatement* target_;
};


1078
class ReturnStatement V8_FINAL : public JumpStatement {
1079
 public:
1080
  DECLARE_NODE_TYPE(ReturnStatement)
1081

1082
  Expression* expression() const { return expression_; }
1083 1084

 protected:
1085 1086
  explicit ReturnStatement(Zone* zone, Expression* expression, int pos)
      : JumpStatement(zone, pos), expression_(expression) { }
1087 1088 1089 1090 1091 1092

 private:
  Expression* expression_;
};


1093
class WithStatement V8_FINAL : public Statement {
1094
 public:
1095
  DECLARE_NODE_TYPE(WithStatement)
1096

1097
  Scope* scope() { return scope_; }
1098
  Expression* expression() const { return expression_; }
1099
  Statement* statement() const { return statement_; }
1100

1101
 protected:
1102
  WithStatement(
1103 1104 1105
      Zone* zone, Scope* scope,
      Expression* expression, Statement* statement, int pos)
      : Statement(zone, pos),
1106
        scope_(scope),
1107
        expression_(expression),
1108
        statement_(statement) { }
1109

1110
 private:
1111
  Scope* scope_;
1112
  Expression* expression_;
1113
  Statement* statement_;
1114 1115 1116
};


1117
class CaseClause V8_FINAL : public Expression {
1118
 public:
1119
  DECLARE_NODE_TYPE(CaseClause)
1120

1121 1122
  bool is_default() const { return label_ == NULL; }
  Expression* label() const {
1123 1124 1125
    CHECK(!is_default());
    return label_;
  }
1126
  Label* body_target() { return &body_target_; }
1127
  ZoneList<Statement*>* statements() const { return statements_; }
1128

1129
  BailoutId EntryId() const { return entry_id_; }
1130

1131
  // Type feedback information.
1132
  TypeFeedbackId CompareId() { return compare_id_; }
1133 1134
  Type* compare_type() { return compare_type_; }
  void set_compare_type(Type* type) { compare_type_ = type; }
1135

1136
 private:
1137
  CaseClause(Zone* zone,
1138 1139 1140 1141
             Expression* label,
             ZoneList<Statement*>* statements,
             int pos);

1142
  Expression* label_;
1143
  Label body_target_;
1144
  ZoneList<Statement*>* statements_;
1145
  Type* compare_type_;
1146

1147 1148
  const TypeFeedbackId compare_id_;
  const BailoutId entry_id_;
1149 1150 1151
};


1152
class SwitchStatement V8_FINAL : public BreakableStatement {
1153
 public:
1154 1155
  DECLARE_NODE_TYPE(SwitchStatement)

1156 1157 1158 1159 1160
  void Initialize(Expression* tag, ZoneList<CaseClause*>* cases) {
    tag_ = tag;
    cases_ = cases;
  }

1161 1162
  Expression* tag() const { return tag_; }
  ZoneList<CaseClause*>* cases() const { return cases_; }
1163 1164

 protected:
1165
  SwitchStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
1166
      : BreakableStatement(zone, labels, TARGET_FOR_ANONYMOUS, pos),
1167 1168
        tag_(NULL),
        cases_(NULL) { }
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180

 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.
1181
class IfStatement V8_FINAL : public Statement {
1182
 public:
1183
  DECLARE_NODE_TYPE(IfStatement)
1184 1185 1186 1187 1188 1189 1190 1191

  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_; }

1192
  virtual bool IsJump() const V8_OVERRIDE {
1193 1194 1195 1196
    return HasThenStatement() && then_statement()->IsJump()
        && HasElseStatement() && else_statement()->IsJump();
  }

1197 1198 1199
  BailoutId IfId() const { return if_id_; }
  BailoutId ThenId() const { return then_id_; }
  BailoutId ElseId() const { return else_id_; }
1200

1201
 protected:
1202
  IfStatement(Zone* zone,
1203 1204
              Expression* condition,
              Statement* then_statement,
1205 1206
              Statement* else_statement,
              int pos)
1207
      : Statement(zone, pos),
1208
        condition_(condition),
1209 1210
        then_statement_(then_statement),
        else_statement_(else_statement),
1211 1212 1213
        if_id_(GetNextId(zone)),
        then_id_(GetNextId(zone)),
        else_id_(GetNextId(zone)) {
1214 1215
  }

1216 1217 1218 1219
 private:
  Expression* condition_;
  Statement* then_statement_;
  Statement* else_statement_;
1220 1221 1222
  const BailoutId if_id_;
  const BailoutId then_id_;
  const BailoutId else_id_;
1223 1224 1225
};


1226
// NOTE: TargetCollectors are represented as nodes to fit in the target
1227
// stack in the compiler; this should probably be reworked.
1228
class TargetCollector V8_FINAL : public AstNode {
1229
 public:
1230 1231
  explicit TargetCollector(Zone* zone)
      : AstNode(RelocInfo::kNoPosition), targets_(0, zone) { }
1232

1233 1234 1235
  // 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.
1236
  void AddTarget(Label* target, Zone* zone);
1237

1238
  // Virtual behaviour. TargetCollectors are never part of the AST.
1239 1240 1241
  virtual void Accept(AstVisitor* v) V8_OVERRIDE { UNREACHABLE(); }
  virtual NodeType node_type() const V8_OVERRIDE { return kInvalid; }
  virtual TargetCollector* AsTargetCollector() V8_OVERRIDE { return this; }
1242

1243
  ZoneList<Label*>* targets() { return &targets_; }
1244 1245

 private:
1246
  ZoneList<Label*> targets_;
1247 1248 1249
};


1250
class TryStatement : public Statement {
1251
 public:
1252
  void set_escaping_targets(ZoneList<Label*>* targets) {
1253
    escaping_targets_ = targets;
1254 1255
  }

1256
  int index() const { return index_; }
1257
  Block* try_block() const { return try_block_; }
1258
  ZoneList<Label*>* escaping_targets() const { return escaping_targets_; }
1259 1260

 protected:
1261 1262
  TryStatement(Zone* zone, int index, Block* try_block, int pos)
      : Statement(zone, pos),
1263
        index_(index),
1264 1265
        try_block_(try_block),
        escaping_targets_(NULL) { }
1266 1267

 private:
1268 1269 1270
  // Unique (per-function) index of this handler.  This is not an AST ID.
  int index_;

1271
  Block* try_block_;
1272
  ZoneList<Label*>* escaping_targets_;
1273 1274 1275
};


1276
class TryCatchStatement V8_FINAL : public TryStatement {
1277
 public:
1278 1279 1280 1281 1282 1283 1284
  DECLARE_NODE_TYPE(TryCatchStatement)

  Scope* scope() { return scope_; }
  Variable* variable() { return variable_; }
  Block* catch_block() const { return catch_block_; }

 protected:
1285 1286
  TryCatchStatement(Zone* zone,
                    int index,
1287
                    Block* try_block,
1288 1289
                    Scope* scope,
                    Variable* variable,
1290 1291
                    Block* catch_block,
                    int pos)
1292
      : TryStatement(zone, index, try_block, pos),
1293 1294
        scope_(scope),
        variable_(variable),
1295 1296 1297 1298
        catch_block_(catch_block) {
  }

 private:
1299 1300
  Scope* scope_;
  Variable* variable_;
1301 1302 1303 1304
  Block* catch_block_;
};


1305
class TryFinallyStatement V8_FINAL : public TryStatement {
1306
 public:
1307
  DECLARE_NODE_TYPE(TryFinallyStatement)
1308 1309

  Block* finally_block() const { return finally_block_; }
1310 1311

 protected:
1312
  TryFinallyStatement(
1313 1314
      Zone* zone, int index, Block* try_block, Block* finally_block, int pos)
      : TryStatement(zone, index, try_block, pos),
1315
        finally_block_(finally_block) { }
1316 1317 1318 1319 1320 1321

 private:
  Block* finally_block_;
};


1322
class DebuggerStatement V8_FINAL : public Statement {
1323
 public:
1324
  DECLARE_NODE_TYPE(DebuggerStatement)
1325 1326

 protected:
1327
  explicit DebuggerStatement(Zone* zone, int pos): Statement(zone, pos) {}
1328 1329 1330
};


1331
class EmptyStatement V8_FINAL : public Statement {
1332
 public:
1333
  DECLARE_NODE_TYPE(EmptyStatement)
1334

1335
 protected:
1336
  explicit EmptyStatement(Zone* zone, int pos): Statement(zone, pos) {}
1337 1338 1339
};


1340
class Literal V8_FINAL : public Expression {
1341
 public:
1342 1343
  DECLARE_NODE_TYPE(Literal)

1344
  virtual bool IsPropertyName() const V8_OVERRIDE {
1345
    return value_->IsPropertyName();
1346 1347
  }

1348 1349
  Handle<String> AsPropertyName() {
    ASSERT(IsPropertyName());
1350 1351 1352 1353 1354 1355
    return Handle<String>::cast(value());
  }

  const AstRawString* AsRawPropertyName() {
    ASSERT(IsPropertyName());
    return value_->AsString();
1356 1357
  }

1358
  virtual bool ToBooleanIsTrue() const V8_OVERRIDE {
1359
    return value()->BooleanValue();
1360
  }
1361
  virtual bool ToBooleanIsFalse() const V8_OVERRIDE {
1362
    return !value()->BooleanValue();
1363
  }
1364

1365 1366
  Handle<Object> value() const { return value_->value(); }
  const AstValue* raw_value() const { return value_; }
1367

1368 1369 1370 1371 1372 1373 1374
  // Support for using Literal as a HashMap key. NOTE: Currently, this works
  // only for string and number literals!
  uint32_t Hash() { return ToString()->Hash(); }

  static bool Match(void* literal1, void* literal2) {
    Handle<String> s1 = static_cast<Literal*>(literal1)->ToString();
    Handle<String> s2 = static_cast<Literal*>(literal2)->ToString();
1375
    return String::Equals(s1, s2);
1376 1377
  }

1378 1379
  TypeFeedbackId LiteralFeedbackId() const { return reuse(id()); }

1380
 protected:
1381
  Literal(Zone* zone, const AstValue* value, int position)
1382
      : Expression(zone, position),
1383
        value_(value),
1384
        isolate_(zone->isolate()) { }
1385 1386

 private:
1387 1388
  Handle<String> ToString();

1389
  const AstValue* value_;
1390 1391
  // TODO(dcarney): remove.  this is only needed for Match and Hash.
  Isolate* isolate_;
1392 1393 1394 1395
};


// Base class for literals that needs space in the corresponding JSFunction.
1396
class MaterializedLiteral : public Expression {
1397
 public:
1398 1399
  virtual MaterializedLiteral* AsMaterializedLiteral() { return this; }

1400
  int literal_index() { return literal_index_; }
1401

1402 1403 1404 1405 1406 1407
  int depth() const {
    // only callable after initialization.
    ASSERT(depth_ >= 1);
    return depth_;
  }

1408
 protected:
1409
  MaterializedLiteral(Zone* zone,
1410
                      int literal_index,
1411
                      int pos)
1412
      : Expression(zone, pos),
1413
        literal_index_(literal_index),
1414 1415
        is_simple_(false),
        depth_(0) {}
1416 1417 1418 1419 1420 1421 1422

  // 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_; }
  void set_is_simple(bool is_simple) { is_simple_ = is_simple; }
  friend class CompileTimeValue;

1423 1424 1425 1426 1427
  void set_depth(int depth) {
    ASSERT(depth >= 1);
    depth_ = depth;
  }

1428
  // Populate the constant properties/elements fixed array.
1429
  void BuildConstants(Isolate* isolate);
1430 1431 1432 1433 1434 1435 1436 1437 1438
  friend class ArrayLiteral;
  friend class ObjectLiteral;

  // If the expression is a literal, return the literal value;
  // if the expression is a materialized literal and is simple return a
  // compile time value as encoded by CompileTimeValue::GetValue().
  // Otherwise, return undefined literal as the placeholder
  // in the object literal boilerplate.
  Handle<Object> GetBoilerplateValue(Expression* expression, Isolate* isolate);
1439

1440 1441
 private:
  int literal_index_;
1442
  bool is_simple_;
1443
  int depth_;
1444 1445 1446
};


1447 1448 1449
// Property is used for passing information
// about an object literal's properties from the parser
// to the code generator.
1450
class ObjectLiteralProperty V8_FINAL : public ZoneObject {
1451 1452 1453 1454 1455 1456 1457 1458 1459
 public:
  enum Kind {
    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__.
  };

1460 1461
  ObjectLiteralProperty(Zone* zone, AstValueFactory* ast_value_factory,
                        Literal* key, Expression* value);
1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479

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

  // Type feedback information.
  void RecordTypeFeedback(TypeFeedbackOracle* oracle);
  bool IsMonomorphic() { return !receiver_type_.is_null(); }
  Handle<Map> GetReceiverType() { return receiver_type_; }

  bool IsCompileTimeValue();

  void set_emit_store(bool emit_store);
  bool emit_store();

 protected:
  template<class> friend class AstNodeFactory;

1480
  ObjectLiteralProperty(Zone* zone, bool is_getter, FunctionLiteral* value);
1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491
  void set_key(Literal* key) { key_ = key; }

 private:
  Literal* key_;
  Expression* value_;
  Kind kind_;
  bool emit_store_;
  Handle<Map> receiver_type_;
};


1492 1493
// An object literal has a boilerplate object that is used
// for minimizing the work when constructing it at runtime.
1494
class ObjectLiteral V8_FINAL : public MaterializedLiteral {
1495
 public:
1496
  typedef ObjectLiteralProperty Property;
1497

1498
  DECLARE_NODE_TYPE(ObjectLiteral)
1499 1500 1501 1502 1503

  Handle<FixedArray> constant_properties() const {
    return constant_properties_;
  }
  ZoneList<Property*>* properties() const { return properties_; }
1504
  bool fast_elements() const { return fast_elements_; }
1505 1506
  bool may_store_doubles() const { return may_store_doubles_; }
  bool has_function() const { return has_function_; }
1507

1508 1509 1510 1511
  // Decide if a property should be in the object boilerplate.
  static bool IsBoilerplateProperty(Property* property);

  // Populate the constant properties fixed array.
1512
  void BuildConstantProperties(Isolate* isolate);
1513

1514 1515 1516
  // 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.
1517
  void CalculateEmitStore(Zone* zone);
1518

1519 1520 1521 1522 1523 1524
  enum Flags {
    kNoFlags = 0,
    kFastElements = 1,
    kHasFunction = 1 << 1
  };

1525 1526 1527 1528 1529 1530
  struct Accessors: public ZoneObject {
    Accessors() : getter(NULL), setter(NULL) { }
    Expression* getter;
    Expression* setter;
  };

1531
 protected:
1532
  ObjectLiteral(Zone* zone,
1533 1534
                ZoneList<Property*>* properties,
                int literal_index,
1535
                int boilerplate_properties,
1536 1537
                bool has_function,
                int pos)
1538
      : MaterializedLiteral(zone, literal_index, pos),
1539
        properties_(properties),
1540 1541 1542
        boilerplate_properties_(boilerplate_properties),
        fast_elements_(false),
        may_store_doubles_(false),
1543 1544
        has_function_(has_function) {}

1545 1546 1547
 private:
  Handle<FixedArray> constant_properties_;
  ZoneList<Property*>* properties_;
1548
  int boilerplate_properties_;
1549
  bool fast_elements_;
1550
  bool may_store_doubles_;
1551
  bool has_function_;
1552 1553 1554 1555
};


// Node for capturing a regexp literal.
1556
class RegExpLiteral V8_FINAL : public MaterializedLiteral {
1557
 public:
1558 1559
  DECLARE_NODE_TYPE(RegExpLiteral)

1560 1561
  Handle<String> pattern() const { return pattern_->string(); }
  Handle<String> flags() const { return flags_->string(); }
1562 1563

 protected:
1564
  RegExpLiteral(Zone* zone,
1565 1566
                const AstRawString* pattern,
                const AstRawString* flags,
1567 1568
                int literal_index,
                int pos)
1569
      : MaterializedLiteral(zone, literal_index, pos),
1570
        pattern_(pattern),
1571 1572 1573
        flags_(flags) {
    set_depth(1);
  }
1574 1575

 private:
1576 1577
  const AstRawString* pattern_;
  const AstRawString* flags_;
1578 1579
};

1580

1581
// An array literal has a literals object that is used
1582
// for minimizing the work when constructing it at runtime.
1583
class ArrayLiteral V8_FINAL : public MaterializedLiteral {
1584
 public:
1585 1586 1587 1588 1589 1590
  DECLARE_NODE_TYPE(ArrayLiteral)

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

  // Return an AST id for an element that is used in simulate instructions.
1591 1592 1593
  BailoutId GetIdForElement(int i) {
    return BailoutId(first_element_id_.ToInt() + i);
  }
1594

1595
  // Populate the constant elements fixed array.
1596
  void BuildConstantElements(Isolate* isolate);
1597

1598 1599 1600 1601 1602 1603
  enum Flags {
    kNoFlags = 0,
    kShallowElements = 1,
    kDisableMementos = 1 << 1
  };

1604
 protected:
1605
  ArrayLiteral(Zone* zone,
1606 1607
               ZoneList<Expression*>* values,
               int literal_index,
1608
               int pos)
1609
      : MaterializedLiteral(zone, literal_index, pos),
1610
        values_(values),
1611
        first_element_id_(ReserveIdRange(zone, values->length())) {}
1612 1613

 private:
1614
  Handle<FixedArray> constant_elements_;
1615
  ZoneList<Expression*>* values_;
1616
  const BailoutId first_element_id_;
1617 1618 1619
};


1620
class VariableProxy V8_FINAL : public Expression, public FeedbackSlotInterface {
1621
 public:
1622
  DECLARE_NODE_TYPE(VariableProxy)
1623

1624 1625
  virtual bool IsValidReferenceExpression() const V8_OVERRIDE {
    return var_ == NULL ? true : var_->IsValidReference();
1626
  }
1627

1628
  bool IsArguments() const { return var_ != NULL && var_->is_arguments(); }
1629

1630 1631
  Handle<String> name() const { return name_->string(); }
  const AstRawString* raw_name() const { return name_; }
1632 1633
  Variable* var() const { return var_; }
  bool is_this() const { return is_this_; }
1634 1635
  Interface* interface() const { return interface_; }

1636 1637
  bool is_assigned() const { return is_assigned_; }
  void set_is_assigned() { is_assigned_ = true; }
1638

1639
  // Bind this proxy to the variable var. Interfaces must match.
1640 1641
  void BindTo(Variable* var);

1642 1643 1644 1645 1646 1647 1648
  virtual int ComputeFeedbackSlotCount() { return FLAG_vector_ics ? 1 : 0; }
  virtual void SetFirstFeedbackSlot(int slot) {
    variable_feedback_slot_ = slot;
  }

  int VariableFeedbackSlot() { return variable_feedback_slot_; }

1649
 protected:
1650
  VariableProxy(Zone* zone, Variable* var, int position);
1651

1652
  VariableProxy(Zone* zone,
1653
                const AstRawString* name,
1654
                bool is_this,
1655 1656
                Interface* interface,
                int position);
1657

1658
  const AstRawString* name_;
1659 1660
  Variable* var_;  // resolved variable, or NULL
  bool is_this_;
1661
  bool is_assigned_;
1662
  Interface* interface_;
1663
  int variable_feedback_slot_;
1664 1665 1666
};


1667
class Property V8_FINAL : public Expression, public FeedbackSlotInterface {
1668
 public:
1669
  DECLARE_NODE_TYPE(Property)
1670

1671
  virtual bool IsValidReferenceExpression() const V8_OVERRIDE { return true; }
1672 1673 1674 1675

  Expression* obj() const { return obj_; }
  Expression* key() const { return key_; }

1676
  BailoutId LoadId() const { return load_id_; }
1677

1678
  bool IsStringAccess() const { return is_string_access_; }
1679

1680
  // Type feedback information.
1681 1682 1683
  virtual bool IsMonomorphic() V8_OVERRIDE {
    return receiver_types_.length() == 1;
  }
1684 1685 1686 1687
  virtual SmallMapList* GetReceiverTypes() V8_OVERRIDE {
    return &receiver_types_;
  }
  virtual KeyedAccessStoreMode GetStoreMode() V8_OVERRIDE {
1688 1689
    return STANDARD_STORE;
  }
1690
  bool IsUninitialized() { return !is_for_call_ && is_uninitialized_; }
1691
  bool HasNoTypeInformation() {
verwaest@chromium.org's avatar
verwaest@chromium.org committed
1692
    return is_uninitialized_;
1693
  }
1694 1695
  void set_is_uninitialized(bool b) { is_uninitialized_ = b; }
  void set_is_string_access(bool b) { is_string_access_ = b; }
verwaest@chromium.org's avatar
verwaest@chromium.org committed
1696 1697
  void mark_for_call() { is_for_call_ = true; }
  bool IsForCall() { return is_for_call_; }
1698

1699
  TypeFeedbackId PropertyFeedbackId() { return reuse(id()); }
1700

1701 1702 1703 1704 1705 1706 1707
  virtual int ComputeFeedbackSlotCount() { return FLAG_vector_ics ? 1 : 0; }
  virtual void SetFirstFeedbackSlot(int slot) {
    property_feedback_slot_ = slot;
  }

  int PropertyFeedbackSlot() const { return property_feedback_slot_; }

1708
 protected:
1709
  Property(Zone* zone, Expression* obj, Expression* key, int pos)
1710
      : Expression(zone, pos),
1711 1712
        obj_(obj),
        key_(key),
1713
        load_id_(GetNextId(zone)),
1714
        property_feedback_slot_(kInvalidFeedbackSlot),
verwaest@chromium.org's avatar
verwaest@chromium.org committed
1715
        is_for_call_(false),
1716
        is_uninitialized_(false),
1717
        is_string_access_(false) {}
1718

1719 1720 1721
 private:
  Expression* obj_;
  Expression* key_;
1722
  const BailoutId load_id_;
1723
  int property_feedback_slot_;
1724

1725
  SmallMapList receiver_types_;
verwaest@chromium.org's avatar
verwaest@chromium.org committed
1726
  bool is_for_call_ : 1;
1727
  bool is_uninitialized_ : 1;
1728
  bool is_string_access_ : 1;
1729 1730 1731
};


1732
class Call V8_FINAL : public Expression, public FeedbackSlotInterface {
1733
 public:
1734
  DECLARE_NODE_TYPE(Call)
1735 1736 1737 1738

  Expression* expression() const { return expression_; }
  ZoneList<Expression*>* arguments() const { return arguments_; }

1739
  // Type feedback information.
1740
  virtual int ComputeFeedbackSlotCount() { return 1; }
1741 1742 1743 1744 1745 1746 1747 1748
  virtual void SetFirstFeedbackSlot(int slot) {
    call_feedback_slot_ = slot;
  }

  bool HasCallFeedbackSlot() const {
    return call_feedback_slot_ != kInvalidFeedbackSlot;
  }
  int CallFeedbackSlot() const { return call_feedback_slot_; }
1749

verwaest@chromium.org's avatar
verwaest@chromium.org committed
1750 1751 1752 1753 1754
  virtual SmallMapList* GetReceiverTypes() V8_OVERRIDE {
    if (expression()->IsProperty()) {
      return expression()->AsProperty()->GetReceiverTypes();
    }
    return NULL;
1755 1756
  }

verwaest@chromium.org's avatar
verwaest@chromium.org committed
1757 1758 1759 1760 1761
  virtual bool IsMonomorphic() V8_OVERRIDE {
    if (expression()->IsProperty()) {
      return expression()->AsProperty()->IsMonomorphic();
    }
    return !target_.is_null();
1762 1763
  }

1764 1765 1766 1767 1768 1769 1770 1771 1772
  bool global_call() const {
    VariableProxy* proxy = expression_->AsVariableProxy();
    return proxy != NULL && proxy->var()->IsUnallocated();
  }

  bool known_global_function() const {
    return global_call() && !target_.is_null();
  }

1773
  Handle<JSFunction> target() { return target_; }
1774

1775
  Handle<Cell> cell() { return cell_; }
1776

1777 1778
  Handle<AllocationSite> allocation_site() { return allocation_site_; }

verwaest@chromium.org's avatar
verwaest@chromium.org committed
1779
  void set_target(Handle<JSFunction> target) { target_ = target; }
1780 1781 1782
  void set_allocation_site(Handle<AllocationSite> site) {
    allocation_site_ = site;
  }
1783
  bool ComputeGlobalTarget(Handle<GlobalObject> global, LookupResult* lookup);
1784

1785
  BailoutId ReturnId() const { return return_id_; }
1786

1787 1788 1789 1790 1791 1792 1793 1794 1795 1796
  enum CallType {
    POSSIBLY_EVAL_CALL,
    GLOBAL_CALL,
    LOOKUP_SLOT_CALL,
    PROPERTY_CALL,
    OTHER_CALL
  };

  // Helpers to determine how to handle the call.
  CallType GetCallType(Isolate* isolate) const;
1797
  bool IsUsingCallFeedbackSlot(Isolate* isolate) const;
1798

1799 1800 1801 1802 1803
#ifdef DEBUG
  // Used to assert that the FullCodeGenerator records the return site.
  bool return_is_recorded_;
#endif

1804
 protected:
1805
  Call(Zone* zone,
1806 1807 1808
       Expression* expression,
       ZoneList<Expression*>* arguments,
       int pos)
1809
      : Expression(zone, pos),
1810 1811
        expression_(expression),
        arguments_(arguments),
1812
        call_feedback_slot_(kInvalidFeedbackSlot),
verwaest@chromium.org's avatar
verwaest@chromium.org committed
1813 1814 1815 1816 1817
        return_id_(GetNextId(zone)) {
    if (expression->IsProperty()) {
      expression->AsProperty()->mark_for_call();
    }
  }
1818

1819 1820 1821 1822
 private:
  Expression* expression_;
  ZoneList<Expression*>* arguments_;

1823
  Handle<JSFunction> target_;
1824
  Handle<Cell> cell_;
1825
  Handle<AllocationSite> allocation_site_;
1826
  int call_feedback_slot_;
1827

1828
  const BailoutId return_id_;
1829
};
1830

1831

1832
class CallNew V8_FINAL : public Expression, public FeedbackSlotInterface {
1833
 public:
1834 1835 1836 1837 1838
  DECLARE_NODE_TYPE(CallNew)

  Expression* expression() const { return expression_; }
  ZoneList<Expression*>* arguments() const { return arguments_; }

1839
  // Type feedback information.
1840
  virtual int ComputeFeedbackSlotCount() {
1841 1842
    return FLAG_pretenuring_call_new ? 2 : 1;
  }
1843 1844 1845 1846 1847 1848 1849 1850
  virtual void SetFirstFeedbackSlot(int slot) {
    callnew_feedback_slot_ = slot;
  }

  int CallNewFeedbackSlot() {
    ASSERT(callnew_feedback_slot_ != kInvalidFeedbackSlot);
    return callnew_feedback_slot_;
  }
1851 1852 1853 1854 1855
  int AllocationSiteFeedbackSlot() {
    ASSERT(callnew_feedback_slot_ != kInvalidFeedbackSlot);
    ASSERT(FLAG_pretenuring_call_new);
    return callnew_feedback_slot_ + 1;
  }
1856

1857
  void RecordTypeFeedback(TypeFeedbackOracle* oracle);
1858
  virtual bool IsMonomorphic() V8_OVERRIDE { return is_monomorphic_; }
1859 1860
  Handle<JSFunction> target() const { return target_; }
  ElementsKind elements_kind() const { return elements_kind_; }
1861 1862
  Handle<AllocationSite> allocation_site() const {
    return allocation_site_;
1863
  }
1864

1865 1866
  static int feedback_slots() { return 1; }

1867
  BailoutId ReturnId() const { return return_id_; }
1868

1869
 protected:
1870
  CallNew(Zone* zone,
1871 1872 1873
          Expression* expression,
          ZoneList<Expression*>* arguments,
          int pos)
1874
      : Expression(zone, pos),
1875 1876
        expression_(expression),
        arguments_(arguments),
1877
        is_monomorphic_(false),
1878
        elements_kind_(GetInitialFastElementsKind()),
1879
        callnew_feedback_slot_(kInvalidFeedbackSlot),
1880
        return_id_(GetNextId(zone)) { }
1881

1882 1883 1884
 private:
  Expression* expression_;
  ZoneList<Expression*>* arguments_;
1885 1886 1887

  bool is_monomorphic_;
  Handle<JSFunction> target_;
1888
  ElementsKind elements_kind_;
1889
  Handle<AllocationSite> allocation_site_;
1890
  int callnew_feedback_slot_;
1891

1892
  const BailoutId return_id_;
1893 1894 1895
};


1896 1897 1898 1899
// 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").
1900
class CallRuntime V8_FINAL : public Expression, public FeedbackSlotInterface {
1901
 public:
1902 1903
  DECLARE_NODE_TYPE(CallRuntime)

1904 1905
  Handle<String> name() const { return raw_name_->string(); }
  const AstRawString* raw_name() const { return raw_name_; }
1906 1907 1908 1909
  const Runtime::Function* function() const { return function_; }
  ZoneList<Expression*>* arguments() const { return arguments_; }
  bool is_jsruntime() const { return function_ == NULL; }

1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923
  // Type feedback information.
  virtual int ComputeFeedbackSlotCount() {
    return (FLAG_vector_ics && is_jsruntime()) ? 1 : 0;
  }
  virtual void SetFirstFeedbackSlot(int slot) {
    callruntime_feedback_slot_ = slot;
  }

  int CallRuntimeFeedbackSlot() {
    ASSERT(!is_jsruntime() ||
           callruntime_feedback_slot_ != kInvalidFeedbackSlot);
    return callruntime_feedback_slot_;
  }

1924 1925
  TypeFeedbackId CallRuntimeFeedbackId() const { return reuse(id()); }

1926
 protected:
1927
  CallRuntime(Zone* zone,
1928
              const AstRawString* name,
1929
              const Runtime::Function* function,
1930 1931
              ZoneList<Expression*>* arguments,
              int pos)
1932
      : Expression(zone, pos),
1933
        raw_name_(name),
1934 1935
        function_(function),
        arguments_(arguments) { }
1936 1937

 private:
1938
  const AstRawString* raw_name_;
1939
  const Runtime::Function* function_;
1940
  ZoneList<Expression*>* arguments_;
1941
  int callruntime_feedback_slot_;
1942 1943 1944
};


1945
class UnaryOperation V8_FINAL : public Expression {
1946
 public:
1947 1948 1949 1950 1951
  DECLARE_NODE_TYPE(UnaryOperation)

  Token::Value op() const { return op_; }
  Expression* expression() const { return expression_; }

1952 1953 1954
  BailoutId MaterializeTrueId() { return materialize_true_id_; }
  BailoutId MaterializeFalseId() { return materialize_false_id_; }

1955 1956
  virtual void RecordToBooleanTypeFeedback(
      TypeFeedbackOracle* oracle) V8_OVERRIDE;
1957

1958
 protected:
1959
  UnaryOperation(Zone* zone,
1960 1961 1962
                 Token::Value op,
                 Expression* expression,
                 int pos)
1963
      : Expression(zone, pos),
1964 1965
        op_(op),
        expression_(expression),
1966 1967
        materialize_true_id_(GetNextId(zone)),
        materialize_false_id_(GetNextId(zone)) {
1968 1969 1970 1971 1972 1973
    ASSERT(Token::IsUnaryOp(op));
  }

 private:
  Token::Value op_;
  Expression* expression_;
1974 1975 1976

  // For unary not (Token::NOT), the AST ids where true and false will
  // actually be materialized, respectively.
1977 1978
  const BailoutId materialize_true_id_;
  const BailoutId materialize_false_id_;
1979 1980 1981
};


1982
class BinaryOperation V8_FINAL : public Expression {
1983
 public:
1984
  DECLARE_NODE_TYPE(BinaryOperation)
1985

1986
  virtual bool ResultOverwriteAllowed() const V8_OVERRIDE;
1987 1988 1989 1990

  Token::Value op() const { return op_; }
  Expression* left() const { return left_; }
  Expression* right() const { return right_; }
1991 1992 1993 1994
  Handle<AllocationSite> allocation_site() const { return allocation_site_; }
  void set_allocation_site(Handle<AllocationSite> allocation_site) {
    allocation_site_ = allocation_site;
  }
1995

1996 1997 1998
  BailoutId RightId() const { return right_id_; }

  TypeFeedbackId BinaryOperationFeedbackId() const { return reuse(id()); }
1999 2000
  Maybe<int> fixed_right_arg() const { return fixed_right_arg_; }
  void set_fixed_right_arg(Maybe<int> arg) { fixed_right_arg_ = arg; }
2001

2002 2003
  virtual void RecordToBooleanTypeFeedback(
      TypeFeedbackOracle* oracle) V8_OVERRIDE;
2004

2005
 protected:
2006
  BinaryOperation(Zone* zone,
2007 2008 2009 2010
                  Token::Value op,
                  Expression* left,
                  Expression* right,
                  int pos)
2011
      : Expression(zone, pos),
2012 2013 2014
        op_(op),
        left_(left),
        right_(right),
2015
        right_id_(GetNextId(zone)) {
2016 2017 2018
    ASSERT(Token::IsBinaryOp(op));
  }

2019 2020 2021 2022
 private:
  Token::Value op_;
  Expression* left_;
  Expression* right_;
2023
  Handle<AllocationSite> allocation_site_;
2024

2025 2026 2027 2028
  // TODO(rossberg): the fixed arg should probably be represented as a Constant
  // type for the RHS.
  Maybe<int> fixed_right_arg_;

2029
  // The short-circuit logical operations need an AST ID for their
2030
  // right-hand subexpression.
2031
  const BailoutId right_id_;
2032 2033 2034
};


2035
class CountOperation V8_FINAL : public Expression {
2036
 public:
2037
  DECLARE_NODE_TYPE(CountOperation)
2038

2039 2040
  bool is_prefix() const { return is_prefix_; }
  bool is_postfix() const { return !is_prefix_; }
2041

2042
  Token::Value op() const { return op_; }
2043
  Token::Value binary_op() {
2044
    return (op() == Token::INC) ? Token::ADD : Token::SUB;
2045
  }
2046

2047
  Expression* expression() const { return expression_; }
2048

2049 2050 2051
  virtual bool IsMonomorphic() V8_OVERRIDE {
    return receiver_types_.length() == 1;
  }
2052 2053 2054 2055
  virtual SmallMapList* GetReceiverTypes() V8_OVERRIDE {
    return &receiver_types_;
  }
  virtual KeyedAccessStoreMode GetStoreMode() V8_OVERRIDE {
2056 2057
    return store_mode_;
  }
2058
  Type* type() const { return type_; }
2059
  void set_store_mode(KeyedAccessStoreMode mode) { store_mode_ = mode; }
2060
  void set_type(Type* type) { type_ = type; }
2061

2062 2063
  BailoutId AssignmentId() const { return assignment_id_; }

2064
  TypeFeedbackId CountBinOpFeedbackId() const { return count_id_; }
2065 2066
  TypeFeedbackId CountStoreFeedbackId() const { return reuse(id()); }

2067
 protected:
2068
  CountOperation(Zone* zone,
2069 2070 2071 2072
                 Token::Value op,
                 bool is_prefix,
                 Expression* expr,
                 int pos)
2073
      : Expression(zone, pos),
2074 2075
        op_(op),
        is_prefix_(is_prefix),
2076
        store_mode_(STANDARD_STORE),
2077
        expression_(expr),
2078 2079
        assignment_id_(GetNextId(zone)),
        count_id_(GetNextId(zone)) {}
2080

2081
 private:
2082
  Token::Value op_;
2083
  bool is_prefix_ : 1;
2084 2085
  KeyedAccessStoreMode store_mode_ : 5;  // Windows treats as signed,
                                         // must have extra bit.
2086
  Type* type_;
2087

2088
  Expression* expression_;
2089
  const BailoutId assignment_id_;
2090
  const TypeFeedbackId count_id_;
2091
  SmallMapList receiver_types_;
2092 2093 2094
};


2095
class CompareOperation V8_FINAL : public Expression {
2096
 public:
2097
  DECLARE_NODE_TYPE(CompareOperation)
2098 2099 2100 2101 2102

  Token::Value op() const { return op_; }
  Expression* left() const { return left_; }
  Expression* right() const { return right_; }

2103
  // Type feedback information.
2104
  TypeFeedbackId CompareOperationFeedbackId() const { return reuse(id()); }
2105 2106
  Type* combined_type() const { return combined_type_; }
  void set_combined_type(Type* type) { combined_type_ = type; }
2107

2108 2109
  // Match special cases.
  bool IsLiteralCompareTypeof(Expression** expr, Handle<String>* check);
2110
  bool IsLiteralCompareUndefined(Expression** expr, Isolate* isolate);
2111
  bool IsLiteralCompareNull(Expression** expr);
2112

2113
 protected:
2114
  CompareOperation(Zone* zone,
2115 2116 2117 2118
                   Token::Value op,
                   Expression* left,
                   Expression* right,
                   int pos)
2119
      : Expression(zone, pos),
2120 2121 2122
        op_(op),
        left_(left),
        right_(right),
2123
        combined_type_(Type::None(zone)) {
2124 2125 2126
    ASSERT(Token::IsCompareOp(op));
  }

2127 2128 2129 2130
 private:
  Token::Value op_;
  Expression* left_;
  Expression* right_;
2131

2132
  Type* combined_type_;
2133 2134 2135
};


2136
class Conditional V8_FINAL : public Expression {
2137
 public:
2138 2139 2140 2141 2142 2143
  DECLARE_NODE_TYPE(Conditional)

  Expression* condition() const { return condition_; }
  Expression* then_expression() const { return then_expression_; }
  Expression* else_expression() const { return else_expression_; }

2144 2145
  BailoutId ThenId() const { return then_id_; }
  BailoutId ElseId() const { return else_id_; }
2146 2147

 protected:
2148
  Conditional(Zone* zone,
2149
              Expression* condition,
2150
              Expression* then_expression,
2151
              Expression* else_expression,
2152
              int position)
2153
      : Expression(zone, position),
2154
        condition_(condition),
2155
        then_expression_(then_expression),
2156
        else_expression_(else_expression),
2157 2158
        then_id_(GetNextId(zone)),
        else_id_(GetNextId(zone)) { }
2159

2160 2161 2162 2163
 private:
  Expression* condition_;
  Expression* then_expression_;
  Expression* else_expression_;
2164 2165
  const BailoutId then_id_;
  const BailoutId else_id_;
2166 2167 2168
};


2169
class Assignment V8_FINAL : public Expression {
2170
 public:
2171
  DECLARE_NODE_TYPE(Assignment)
2172

2173 2174
  Assignment* AsSimpleAssignment() { return !is_compound() ? this : NULL; }

2175 2176 2177 2178 2179
  Token::Value binary_op() const;

  Token::Value op() const { return op_; }
  Expression* target() const { return target_; }
  Expression* value() const { return value_; }
2180 2181
  BinaryOperation* binary_operation() const { return binary_operation_; }

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

2185 2186
  BailoutId AssignmentId() const { return assignment_id_; }

2187
  // Type feedback information.
2188
  TypeFeedbackId AssignmentFeedbackId() { return reuse(id()); }
2189 2190 2191
  virtual bool IsMonomorphic() V8_OVERRIDE {
    return receiver_types_.length() == 1;
  }
2192
  bool IsUninitialized() { return is_uninitialized_; }
2193
  bool HasNoTypeInformation() {
verwaest@chromium.org's avatar
verwaest@chromium.org committed
2194
    return is_uninitialized_;
2195
  }
2196 2197 2198 2199
  virtual SmallMapList* GetReceiverTypes() V8_OVERRIDE {
    return &receiver_types_;
  }
  virtual KeyedAccessStoreMode GetStoreMode() V8_OVERRIDE {
2200 2201
    return store_mode_;
  }
2202 2203
  void set_is_uninitialized(bool b) { is_uninitialized_ = b; }
  void set_store_mode(KeyedAccessStoreMode mode) { store_mode_ = mode; }
2204

2205
 protected:
2206
  Assignment(Zone* zone,
2207 2208 2209 2210 2211 2212
             Token::Value op,
             Expression* target,
             Expression* value,
             int pos);

  template<class Visitor>
2213
  void Init(Zone* zone, AstNodeFactory<Visitor>* factory) {
2214 2215
    ASSERT(Token::IsAssignmentOp(op_));
    if (is_compound()) {
2216 2217
      binary_operation_ = factory->NewBinaryOperation(
          binary_op(), target_, value_, position() + 1);
2218 2219 2220
    }
  }

2221 2222 2223 2224
 private:
  Token::Value op_;
  Expression* target_;
  Expression* value_;
2225
  BinaryOperation* binary_operation_;
2226
  const BailoutId assignment_id_;
2227

2228
  bool is_uninitialized_ : 1;
2229 2230
  KeyedAccessStoreMode store_mode_ : 5;  // Windows treats as signed,
                                         // must have extra bit.
2231
  SmallMapList receiver_types_;
2232 2233 2234
};


2235
class Yield V8_FINAL : public Expression, public FeedbackSlotInterface {
2236 2237 2238
 public:
  DECLARE_NODE_TYPE(Yield)

2239
  enum Kind {
2240 2241 2242 2243
    INITIAL,     // The initial yield that returns the unboxed generator object.
    SUSPEND,     // A normal yield: { value: EXPRESSION, done: false }
    DELEGATING,  // A yield*.
    FINAL        // A return: { value: EXPRESSION, done: true }
2244 2245
  };

2246
  Expression* generator_object() const { return generator_object_; }
2247
  Expression* expression() const { return expression_; }
2248
  Kind yield_kind() const { return yield_kind_; }
2249

2250 2251 2252 2253
  // Delegating yield surrounds the "yield" in a "try/catch".  This index
  // locates the catch handler in the handler table, and is equivalent to
  // TryCatchStatement::index().
  int index() const {
2254
    ASSERT(yield_kind() == DELEGATING);
2255 2256 2257
    return index_;
  }
  void set_index(int index) {
2258
    ASSERT(yield_kind() == DELEGATING);
2259 2260 2261
    index_ = index;
  }

2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284
  // Type feedback information.
  virtual int ComputeFeedbackSlotCount() {
    return (FLAG_vector_ics && yield_kind() == DELEGATING) ? 3 : 0;
  }
  virtual void SetFirstFeedbackSlot(int slot) {
    yield_first_feedback_slot_ = slot;
  }

  int KeyedLoadFeedbackSlot() {
    ASSERT(yield_first_feedback_slot_ != kInvalidFeedbackSlot);
    return yield_first_feedback_slot_;
  }

  int DoneFeedbackSlot() {
    ASSERT(yield_first_feedback_slot_ != kInvalidFeedbackSlot);
    return yield_first_feedback_slot_ + 1;
  }

  int ValueFeedbackSlot() {
    ASSERT(yield_first_feedback_slot_ != kInvalidFeedbackSlot);
    return yield_first_feedback_slot_ + 2;
  }

2285
 protected:
2286
  Yield(Zone* zone,
2287
        Expression* generator_object,
2288
        Expression* expression,
2289
        Kind yield_kind,
2290
        int pos)
2291
      : Expression(zone, pos),
2292
        generator_object_(generator_object),
2293
        expression_(expression),
2294
        yield_kind_(yield_kind),
2295 2296
        index_(-1),
        yield_first_feedback_slot_(kInvalidFeedbackSlot) { }
2297 2298

 private:
2299
  Expression* generator_object_;
2300
  Expression* expression_;
2301
  Kind yield_kind_;
2302
  int index_;
2303
  int yield_first_feedback_slot_;
2304 2305 2306
};


2307
class Throw V8_FINAL : public Expression {
2308
 public:
2309
  DECLARE_NODE_TYPE(Throw)
2310

2311
  Expression* exception() const { return exception_; }
2312 2313

 protected:
2314 2315
  Throw(Zone* zone, Expression* exception, int pos)
      : Expression(zone, pos), exception_(exception) {}
2316 2317 2318 2319 2320 2321

 private:
  Expression* exception_;
};


2322
class FunctionLiteral V8_FINAL : public Expression {
2323
 public:
2324
  enum FunctionType {
2325 2326 2327 2328 2329
    ANONYMOUS_EXPRESSION,
    NAMED_EXPRESSION,
    DECLARATION
  };

2330 2331 2332 2333 2334 2335 2336 2337 2338 2339
  enum ParameterFlag {
    kNoDuplicateParameters = 0,
    kHasDuplicateParameters = 1
  };

  enum IsFunctionFlag {
    kGlobalOrEval,
    kIsFunction
  };

2340 2341 2342 2343 2344
  enum IsParenthesizedFlag {
    kIsParenthesized,
    kNotParenthesized
  };

2345 2346 2347 2348
  enum KindFlag {
    kNormalFunction,
    kArrowFunction,
    kGeneratorFunction
2349 2350
  };

2351 2352 2353 2354 2355 2356
  enum ArityRestriction {
    NORMAL_ARITY,
    GETTER_ARITY,
    SETTER_ARITY
  };

2357
  DECLARE_NODE_TYPE(FunctionLiteral)
2358

2359 2360
  Handle<String> name() const { return raw_name_->string(); }
  const AstRawString* raw_name() const { return raw_name_; }
2361 2362
  Scope* scope() const { return scope_; }
  ZoneList<Statement*>* body() const { return body_; }
2363 2364
  void set_function_token_position(int pos) { function_token_position_ = pos; }
  int function_token_position() const { return function_token_position_; }
2365 2366
  int start_position() const;
  int end_position() const;
2367
  int SourceSize() const { return end_position() - start_position(); }
2368 2369
  bool is_expression() const { return IsExpression::decode(bitfield_); }
  bool is_anonymous() const { return IsAnonymous::decode(bitfield_); }
2370
  StrictMode strict_mode() const;
2371 2372 2373

  int materialized_literal_count() { return materialized_literal_count_; }
  int expected_property_count() { return expected_property_count_; }
2374
  int handler_count() { return handler_count_; }
2375
  int parameter_count() { return parameter_count_; }
2376 2377

  bool AllowsLazyCompilation();
2378
  bool AllowsLazyCompilationWithoutContext();
2379

2380 2381
  void InitializeSharedInfo(Handle<Code> code);

2382
  Handle<String> debug_name() const {
2383 2384 2385
    if (raw_name_ != NULL && !raw_name_->IsEmpty()) {
      return raw_name_->string();
    }
2386 2387 2388
    return inferred_name();
  }

2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401
  Handle<String> inferred_name() const {
    if (!inferred_name_.is_null()) {
      ASSERT(raw_inferred_name_ == NULL);
      return inferred_name_;
    }
    if (raw_inferred_name_ != NULL) {
      return raw_inferred_name_->string();
    }
    UNREACHABLE();
    return Handle<String>();
  }

  // Only one of {set_inferred_name, set_raw_inferred_name} should be called.
2402
  void set_inferred_name(Handle<String> inferred_name) {
2403
    ASSERT(!inferred_name.is_null());
2404
    inferred_name_ = inferred_name;
2405 2406 2407 2408 2409 2410 2411 2412 2413
    ASSERT(raw_inferred_name_== NULL || raw_inferred_name_->IsEmpty());
    raw_inferred_name_ = NULL;
  }

  void set_raw_inferred_name(const AstString* raw_inferred_name) {
    ASSERT(raw_inferred_name != NULL);
    raw_inferred_name_ = raw_inferred_name;
    ASSERT(inferred_name_.is_null());
    inferred_name_ = Handle<String>();
2414 2415
  }

2416 2417 2418
  // shared_info may be null if it's not cached in full code.
  Handle<SharedFunctionInfo> shared_info() { return shared_info_; }

2419 2420
  bool pretenure() { return Pretenure::decode(bitfield_); }
  void set_pretenure() { bitfield_ |= Pretenure::encode(true); }
2421

2422 2423 2424
  bool has_duplicate_parameters() {
    return HasDuplicateParameters::decode(bitfield_);
  }
2425

2426 2427
  bool is_function() { return IsFunction::decode(bitfield_) == kIsFunction; }

2428 2429 2430 2431 2432
  // This is used as a heuristic on when to eagerly compile a function
  // literal. We consider the following constructs as hints that the
  // function will be called immediately:
  // - (function() { ... })();
  // - var x = function() { ... }();
2433 2434 2435
  bool is_parenthesized() {
    return IsParenthesized::decode(bitfield_) == kIsParenthesized;
  }
2436 2437 2438
  void set_parenthesized() {
    bitfield_ = IsParenthesized::update(bitfield_, kIsParenthesized);
  }
2439

2440 2441
  bool is_generator() { return IsGenerator::decode(bitfield_); }
  bool is_arrow() { return IsArrow::decode(bitfield_); }
2442

2443 2444 2445 2446 2447
  int ast_node_count() { return ast_properties_.node_count(); }
  AstProperties::Flags* flags() { return ast_properties_.flags(); }
  void set_ast_properties(AstProperties* ast_properties) {
    ast_properties_ = *ast_properties;
  }
2448
  int slot_count() {
2449
    return ast_properties_.feedback_slots();
2450
  }
2451 2452 2453 2454 2455 2456
  bool dont_optimize() { return dont_optimize_reason_ != kNoReason; }
  BailoutReason dont_optimize_reason() { return dont_optimize_reason_; }
  void set_dont_optimize_reason(BailoutReason reason) {
    dont_optimize_reason_ = reason;
  }

2457
 protected:
2458 2459 2460 2461 2462
  FunctionLiteral(Zone* zone, const AstRawString* name,
                  AstValueFactory* ast_value_factory, Scope* scope,
                  ZoneList<Statement*>* body, int materialized_literal_count,
                  int expected_property_count, int handler_count,
                  int parameter_count, FunctionType function_type,
2463
                  ParameterFlag has_duplicate_parameters,
2464
                  IsFunctionFlag is_function,
2465
                  IsParenthesizedFlag is_parenthesized, KindFlag kind,
2466
                  int position)
2467
      : Expression(zone, position),
2468
        raw_name_(name),
2469 2470
        scope_(scope),
        body_(body),
2471
        raw_inferred_name_(ast_value_factory->empty_string()),
2472
        dont_optimize_reason_(kNoReason),
2473 2474 2475 2476 2477
        materialized_literal_count_(materialized_literal_count),
        expected_property_count_(expected_property_count),
        handler_count_(handler_count),
        parameter_count_(parameter_count),
        function_token_position_(RelocInfo::kNoPosition) {
2478 2479 2480 2481 2482 2483 2484 2485
    bitfield_ = IsExpression::encode(function_type != DECLARATION) |
                IsAnonymous::encode(function_type == ANONYMOUS_EXPRESSION) |
                Pretenure::encode(false) |
                HasDuplicateParameters::encode(has_duplicate_parameters) |
                IsFunction::encode(is_function) |
                IsParenthesized::encode(is_parenthesized) |
                IsGenerator::encode(kind == kGeneratorFunction) |
                IsArrow::encode(kind == kArrowFunction);
2486 2487
  }

2488
 private:
2489
  const AstRawString* raw_name_;
2490
  Handle<String> name_;
2491
  Handle<SharedFunctionInfo> shared_info_;
2492 2493
  Scope* scope_;
  ZoneList<Statement*>* body_;
2494
  const AstString* raw_inferred_name_;
2495
  Handle<String> inferred_name_;
2496
  AstProperties ast_properties_;
2497
  BailoutReason dont_optimize_reason_;
2498

2499 2500
  int materialized_literal_count_;
  int expected_property_count_;
2501
  int handler_count_;
2502
  int parameter_count_;
2503
  int function_token_position_;
2504 2505

  unsigned bitfield_;
2506 2507 2508 2509 2510 2511
  class IsExpression: public BitField<bool, 0, 1> {};
  class IsAnonymous: public BitField<bool, 1, 1> {};
  class Pretenure: public BitField<bool, 2, 1> {};
  class HasDuplicateParameters: public BitField<ParameterFlag, 3, 1> {};
  class IsFunction: public BitField<IsFunctionFlag, 4, 1> {};
  class IsParenthesized: public BitField<IsParenthesizedFlag, 5, 1> {};
2512 2513
  class IsGenerator : public BitField<bool, 6, 1> {};
  class IsArrow : public BitField<bool, 7, 1> {};
2514 2515 2516
};


2517
class NativeFunctionLiteral V8_FINAL : public Expression {
2518
 public:
2519
  DECLARE_NODE_TYPE(NativeFunctionLiteral)
2520

2521
  Handle<String> name() const { return name_->string(); }
2522
  v8::Extension* extension() const { return extension_; }
2523 2524

 protected:
2525 2526
  NativeFunctionLiteral(Zone* zone, const AstRawString* name,
                        v8::Extension* extension, int pos)
2527
      : Expression(zone, pos), name_(name), extension_(extension) {}
2528 2529

 private:
2530
  const AstRawString* name_;
2531
  v8::Extension* extension_;
2532 2533 2534
};


2535
class ThisFunction V8_FINAL : public Expression {
2536
 public:
2537
  DECLARE_NODE_TYPE(ThisFunction)
2538 2539

 protected:
2540
  explicit ThisFunction(Zone* zone, int pos): Expression(zone, pos) {}
2541 2542
};

2543 2544
#undef DECLARE_NODE_TYPE

2545

2546 2547 2548 2549
// ----------------------------------------------------------------------------
// Regular expressions


2550 2551 2552 2553 2554 2555 2556 2557 2558 2559
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
};


2560
class RegExpTree : public ZoneObject {
2561
 public:
2562
  static const int kInfinity = kMaxInt;
2563
  virtual ~RegExpTree() {}
2564 2565
  virtual void* Accept(RegExpVisitor* visitor, void* data) = 0;
  virtual RegExpNode* ToNode(RegExpCompiler* compiler,
erik.corry@gmail.com's avatar
erik.corry@gmail.com committed
2566
                             RegExpNode* on_success) = 0;
2567
  virtual bool IsTextElement() { return false; }
2568 2569
  virtual bool IsAnchoredAtStart() { return false; }
  virtual bool IsAnchoredAtEnd() { return false; }
2570 2571
  virtual int min_match() = 0;
  virtual int max_match() = 0;
2572 2573 2574
  // Returns the interval of registers used for captures within this
  // expression.
  virtual Interval CaptureRegisters() { return Interval::Empty(); }
2575
  virtual void AppendToText(RegExpText* text, Zone* zone);
2576
  OStream& Print(OStream& os, Zone* zone);  // NOLINT
2577 2578 2579 2580 2581 2582 2583 2584
#define MAKE_ASTYPE(Name)                                                  \
  virtual RegExp##Name* As##Name();                                        \
  virtual bool Is##Name();
  FOR_EACH_REG_EXP_TREE_TYPE(MAKE_ASTYPE)
#undef MAKE_ASTYPE
};


2585
class RegExpDisjunction V8_FINAL : public RegExpTree {
2586
 public:
2587
  explicit RegExpDisjunction(ZoneList<RegExpTree*>* alternatives);
2588
  virtual void* Accept(RegExpVisitor* visitor, void* data) V8_OVERRIDE;
2589
  virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2590 2591 2592 2593 2594 2595 2596 2597
                             RegExpNode* on_success) V8_OVERRIDE;
  virtual RegExpDisjunction* AsDisjunction() V8_OVERRIDE;
  virtual Interval CaptureRegisters() V8_OVERRIDE;
  virtual bool IsDisjunction() V8_OVERRIDE;
  virtual bool IsAnchoredAtStart() V8_OVERRIDE;
  virtual bool IsAnchoredAtEnd() V8_OVERRIDE;
  virtual int min_match() V8_OVERRIDE { return min_match_; }
  virtual int max_match() V8_OVERRIDE { return max_match_; }
2598 2599 2600
  ZoneList<RegExpTree*>* alternatives() { return alternatives_; }
 private:
  ZoneList<RegExpTree*>* alternatives_;
2601 2602
  int min_match_;
  int max_match_;
2603 2604 2605
};


2606
class RegExpAlternative V8_FINAL : public RegExpTree {
2607
 public:
2608
  explicit RegExpAlternative(ZoneList<RegExpTree*>* nodes);
2609
  virtual void* Accept(RegExpVisitor* visitor, void* data) V8_OVERRIDE;
2610
  virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2611 2612 2613 2614 2615 2616 2617 2618
                             RegExpNode* on_success) V8_OVERRIDE;
  virtual RegExpAlternative* AsAlternative() V8_OVERRIDE;
  virtual Interval CaptureRegisters() V8_OVERRIDE;
  virtual bool IsAlternative() V8_OVERRIDE;
  virtual bool IsAnchoredAtStart() V8_OVERRIDE;
  virtual bool IsAnchoredAtEnd() V8_OVERRIDE;
  virtual int min_match() V8_OVERRIDE { return min_match_; }
  virtual int max_match() V8_OVERRIDE { return max_match_; }
2619 2620 2621
  ZoneList<RegExpTree*>* nodes() { return nodes_; }
 private:
  ZoneList<RegExpTree*>* nodes_;
2622 2623
  int min_match_;
  int max_match_;
2624 2625 2626
};


2627
class RegExpAssertion V8_FINAL : public RegExpTree {
2628
 public:
2629
  enum AssertionType {
2630 2631 2632 2633 2634 2635
    START_OF_LINE,
    START_OF_INPUT,
    END_OF_LINE,
    END_OF_INPUT,
    BOUNDARY,
    NON_BOUNDARY
2636
  };
2637
  explicit RegExpAssertion(AssertionType type) : assertion_type_(type) { }
2638
  virtual void* Accept(RegExpVisitor* visitor, void* data) V8_OVERRIDE;
2639
  virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2640 2641 2642 2643 2644 2645 2646
                             RegExpNode* on_success) V8_OVERRIDE;
  virtual RegExpAssertion* AsAssertion() V8_OVERRIDE;
  virtual bool IsAssertion() V8_OVERRIDE;
  virtual bool IsAnchoredAtStart() V8_OVERRIDE;
  virtual bool IsAnchoredAtEnd() V8_OVERRIDE;
  virtual int min_match() V8_OVERRIDE { return 0; }
  virtual int max_match() V8_OVERRIDE { return 0; }
2647
  AssertionType assertion_type() { return assertion_type_; }
2648
 private:
2649
  AssertionType assertion_type_;
2650 2651 2652
};


2653
class CharacterSet V8_FINAL BASE_EMBEDDED {
2654 2655 2656 2657 2658 2659 2660
 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) {}
2661
  ZoneList<CharacterRange>* ranges(Zone* zone);
2662 2663 2664 2665 2666
  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; }
2667
  void Canonicalize();
2668 2669 2670 2671 2672 2673 2674 2675
 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_;
};


2676
class RegExpCharacterClass V8_FINAL : public RegExpTree {
2677 2678
 public:
  RegExpCharacterClass(ZoneList<CharacterRange>* ranges, bool is_negated)
2679
      : set_(ranges),
2680
        is_negated_(is_negated) { }
2681
  explicit RegExpCharacterClass(uc16 type)
2682 2683
      : set_(type),
        is_negated_(false) { }
2684
  virtual void* Accept(RegExpVisitor* visitor, void* data) V8_OVERRIDE;
2685
  virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2686 2687 2688 2689 2690 2691 2692
                             RegExpNode* on_success) V8_OVERRIDE;
  virtual RegExpCharacterClass* AsCharacterClass() V8_OVERRIDE;
  virtual bool IsCharacterClass() V8_OVERRIDE;
  virtual bool IsTextElement() V8_OVERRIDE { return true; }
  virtual int min_match() V8_OVERRIDE { return 1; }
  virtual int max_match() V8_OVERRIDE { return 1; }
  virtual void AppendToText(RegExpText* text, Zone* zone) V8_OVERRIDE;
2693 2694 2695
  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(); }
2696
  bool is_standard(Zone* zone);
2697 2698 2699 2700 2701 2702 2703 2704 2705
  // 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
2706
  // . : non-unicode non-newline
2707 2708
  // * : All characters
  uc16 standard_type() { return set_.standard_set_type(); }
2709
  ZoneList<CharacterRange>* ranges(Zone* zone) { return set_.ranges(zone); }
2710
  bool is_negated() { return is_negated_; }
2711

2712
 private:
2713
  CharacterSet set_;
2714 2715 2716 2717
  bool is_negated_;
};


2718
class RegExpAtom V8_FINAL : public RegExpTree {
2719 2720
 public:
  explicit RegExpAtom(Vector<const uc16> data) : data_(data) { }
2721
  virtual void* Accept(RegExpVisitor* visitor, void* data) V8_OVERRIDE;
2722
  virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2723 2724 2725 2726 2727 2728 2729
                             RegExpNode* on_success) V8_OVERRIDE;
  virtual RegExpAtom* AsAtom() V8_OVERRIDE;
  virtual bool IsAtom() V8_OVERRIDE;
  virtual bool IsTextElement() V8_OVERRIDE { return true; }
  virtual int min_match() V8_OVERRIDE { return data_.length(); }
  virtual int max_match() V8_OVERRIDE { return data_.length(); }
  virtual void AppendToText(RegExpText* text, Zone* zone) V8_OVERRIDE;
2730
  Vector<const uc16> data() { return data_; }
2731
  int length() { return data_.length(); }
2732 2733 2734 2735 2736
 private:
  Vector<const uc16> data_;
};


2737
class RegExpText V8_FINAL : public RegExpTree {
2738
 public:
2739
  explicit RegExpText(Zone* zone) : elements_(2, zone), length_(0) {}
2740
  virtual void* Accept(RegExpVisitor* visitor, void* data) V8_OVERRIDE;
2741
  virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2742 2743 2744 2745 2746 2747 2748
                             RegExpNode* on_success) V8_OVERRIDE;
  virtual RegExpText* AsText() V8_OVERRIDE;
  virtual bool IsText() V8_OVERRIDE;
  virtual bool IsTextElement() V8_OVERRIDE { return true; }
  virtual int min_match() V8_OVERRIDE { return length_; }
  virtual int max_match() V8_OVERRIDE { return length_; }
  virtual void AppendToText(RegExpText* text, Zone* zone) V8_OVERRIDE;
2749 2750
  void AddElement(TextElement elm, Zone* zone)  {
    elements_.Add(elm, zone);
2751
    length_ += elm.length();
2752
  }
2753 2754 2755 2756 2757 2758 2759
  ZoneList<TextElement>* elements() { return &elements_; }
 private:
  ZoneList<TextElement> elements_;
  int length_;
};


2760
class RegExpQuantifier V8_FINAL : public RegExpTree {
2761
 public:
2762 2763
  enum QuantifierType { GREEDY, NON_GREEDY, POSSESSIVE };
  RegExpQuantifier(int min, int max, QuantifierType type, RegExpTree* body)
2764 2765
      : body_(body),
        min_(min),
2766
        max_(max),
2767
        min_match_(min * body->min_match()),
2768
        quantifier_type_(type) {
2769 2770 2771 2772 2773 2774
    if (max > 0 && body->max_match() > kInfinity / max) {
      max_match_ = kInfinity;
    } else {
      max_match_ = max * body->max_match();
    }
  }
2775
  virtual void* Accept(RegExpVisitor* visitor, void* data) V8_OVERRIDE;
2776
  virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2777
                             RegExpNode* on_success) V8_OVERRIDE;
2778 2779 2780 2781 2782
  static RegExpNode* ToNode(int min,
                            int max,
                            bool is_greedy,
                            RegExpTree* body,
                            RegExpCompiler* compiler,
2783 2784
                            RegExpNode* on_success,
                            bool not_at_start = false);
2785 2786 2787 2788 2789
  virtual RegExpQuantifier* AsQuantifier() V8_OVERRIDE;
  virtual Interval CaptureRegisters() V8_OVERRIDE;
  virtual bool IsQuantifier() V8_OVERRIDE;
  virtual int min_match() V8_OVERRIDE { return min_match_; }
  virtual int max_match() V8_OVERRIDE { return max_match_; }
2790 2791
  int min() { return min_; }
  int max() { return max_; }
2792 2793 2794
  bool is_possessive() { return quantifier_type_ == POSSESSIVE; }
  bool is_non_greedy() { return quantifier_type_ == NON_GREEDY; }
  bool is_greedy() { return quantifier_type_ == GREEDY; }
2795
  RegExpTree* body() { return body_; }
2796

2797
 private:
2798
  RegExpTree* body_;
2799 2800
  int min_;
  int max_;
2801 2802
  int min_match_;
  int max_match_;
2803
  QuantifierType quantifier_type_;
2804 2805 2806
};


2807
class RegExpCapture V8_FINAL : public RegExpTree {
2808 2809
 public:
  explicit RegExpCapture(RegExpTree* body, int index)
2810
      : body_(body), index_(index) { }
2811
  virtual void* Accept(RegExpVisitor* visitor, void* data) V8_OVERRIDE;
2812
  virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2813
                             RegExpNode* on_success) V8_OVERRIDE;
2814 2815 2816
  static RegExpNode* ToNode(RegExpTree* body,
                            int index,
                            RegExpCompiler* compiler,
erik.corry@gmail.com's avatar
erik.corry@gmail.com committed
2817
                            RegExpNode* on_success);
2818 2819 2820 2821 2822 2823 2824
  virtual RegExpCapture* AsCapture() V8_OVERRIDE;
  virtual bool IsAnchoredAtStart() V8_OVERRIDE;
  virtual bool IsAnchoredAtEnd() V8_OVERRIDE;
  virtual Interval CaptureRegisters() V8_OVERRIDE;
  virtual bool IsCapture() V8_OVERRIDE;
  virtual int min_match() V8_OVERRIDE { return body_->min_match(); }
  virtual int max_match() V8_OVERRIDE { return body_->max_match(); }
2825 2826 2827 2828
  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; }
2829

2830 2831 2832 2833 2834 2835
 private:
  RegExpTree* body_;
  int index_;
};


2836
class RegExpLookahead V8_FINAL : public RegExpTree {
2837
 public:
2838 2839 2840 2841
  RegExpLookahead(RegExpTree* body,
                  bool is_positive,
                  int capture_count,
                  int capture_from)
2842
      : body_(body),
2843 2844 2845 2846
        is_positive_(is_positive),
        capture_count_(capture_count),
        capture_from_(capture_from) { }

2847
  virtual void* Accept(RegExpVisitor* visitor, void* data) V8_OVERRIDE;
2848
  virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2849 2850 2851 2852 2853 2854 2855
                             RegExpNode* on_success) V8_OVERRIDE;
  virtual RegExpLookahead* AsLookahead() V8_OVERRIDE;
  virtual Interval CaptureRegisters() V8_OVERRIDE;
  virtual bool IsLookahead() V8_OVERRIDE;
  virtual bool IsAnchoredAtStart() V8_OVERRIDE;
  virtual int min_match() V8_OVERRIDE { return 0; }
  virtual int max_match() V8_OVERRIDE { return 0; }
2856 2857
  RegExpTree* body() { return body_; }
  bool is_positive() { return is_positive_; }
2858 2859
  int capture_count() { return capture_count_; }
  int capture_from() { return capture_from_; }
2860

2861 2862 2863
 private:
  RegExpTree* body_;
  bool is_positive_;
2864 2865
  int capture_count_;
  int capture_from_;
2866 2867 2868
};


2869
class RegExpBackReference V8_FINAL : public RegExpTree {
2870 2871
 public:
  explicit RegExpBackReference(RegExpCapture* capture)
2872
      : capture_(capture) { }
2873
  virtual void* Accept(RegExpVisitor* visitor, void* data) V8_OVERRIDE;
2874
  virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2875 2876 2877 2878 2879
                             RegExpNode* on_success) V8_OVERRIDE;
  virtual RegExpBackReference* AsBackReference() V8_OVERRIDE;
  virtual bool IsBackReference() V8_OVERRIDE;
  virtual int min_match() V8_OVERRIDE { return 0; }
  virtual int max_match() V8_OVERRIDE { return capture_->max_match(); }
2880 2881 2882 2883 2884 2885 2886
  int index() { return capture_->index(); }
  RegExpCapture* capture() { return capture_; }
 private:
  RegExpCapture* capture_;
};


2887
class RegExpEmpty V8_FINAL : public RegExpTree {
2888 2889
 public:
  RegExpEmpty() { }
2890
  virtual void* Accept(RegExpVisitor* visitor, void* data) V8_OVERRIDE;
2891
  virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2892 2893 2894 2895 2896
                             RegExpNode* on_success) V8_OVERRIDE;
  virtual RegExpEmpty* AsEmpty() V8_OVERRIDE;
  virtual bool IsEmpty() V8_OVERRIDE;
  virtual int min_match() V8_OVERRIDE { return 0; }
  virtual int max_match() V8_OVERRIDE { return 0; }
2897 2898 2899 2900
  static RegExpEmpty* GetInstance() {
    static RegExpEmpty* instance = ::new RegExpEmpty();
    return instance;
  }
2901 2902 2903
};


2904 2905 2906
// ----------------------------------------------------------------------------
// Out-of-line inline constructors (to side-step cyclic dependencies).

2907 2908
inline ModuleVariable::ModuleVariable(Zone* zone, VariableProxy* proxy, int pos)
    : Module(zone, proxy->interface(), pos),
2909 2910 2911 2912
      proxy_(proxy) {
}


2913 2914 2915 2916
// ----------------------------------------------------------------------------
// Basic visitor
// - leaf node visitors are abstract.

2917
class AstVisitor BASE_EMBEDDED {
2918
 public:
2919
  AstVisitor() {}
2920
  virtual ~AstVisitor() {}
2921

2922
  // Stack overflow check and dynamic dispatch.
2923
  virtual void Visit(AstNode* node) = 0;
2924

2925
  // Iteration left-to-right.
2926
  virtual void VisitDeclarations(ZoneList<Declaration*>* declarations);
2927 2928 2929
  virtual void VisitStatements(ZoneList<Statement*>* statements);
  virtual void VisitExpressions(ZoneList<Expression*>* expressions);

2930
  // Individual AST nodes.
2931 2932
#define DEF_VISIT(type)                         \
  virtual void Visit##type(type* node) = 0;
2933
  AST_NODE_LIST(DEF_VISIT)
2934
#undef DEF_VISIT
2935
};
2936

2937

2938 2939
#define DEFINE_AST_VISITOR_SUBCLASS_MEMBERS()                       \
public:                                                             \
2940
  virtual void Visit(AstNode* node) V8_FINAL V8_OVERRIDE {          \
2941 2942 2943 2944 2945 2946 2947 2948 2949
    if (!CheckStackOverflow()) node->Accept(this);                  \
  }                                                                 \
                                                                    \
  void SetStackOverflow() { stack_overflow_ = true; }               \
  void ClearStackOverflow() { stack_overflow_ = false; }            \
  bool HasStackOverflow() const { return stack_overflow_; }         \
                                                                    \
  bool CheckStackOverflow() {                                       \
    if (stack_overflow_) return true;                               \
2950
    StackLimitCheck check(zone_->isolate());                        \
2951 2952 2953 2954 2955
    if (!check.HasOverflowed()) return false;                       \
    return (stack_overflow_ = true);                                \
  }                                                                 \
                                                                    \
private:                                                            \
2956 2957
  void InitializeAstVisitor(Zone* zone) {                           \
    zone_ = zone;                                                   \
2958 2959
    stack_overflow_ = false;                                        \
  }                                                                 \
2960 2961
  Zone* zone() { return zone_; }                                    \
  Isolate* isolate() { return zone_->isolate(); }                   \
2962
                                                                    \
2963
  Zone* zone_;                                                      \
2964
  bool stack_overflow_
2965

2966

2967 2968 2969 2970 2971
// ----------------------------------------------------------------------------
// Construction time visitor.

class AstConstructionVisitor BASE_EMBEDDED {
 public:
2972
  AstConstructionVisitor() : dont_optimize_reason_(kNoReason) { }
2973 2974

  AstProperties* ast_properties() { return &properties_; }
2975
  BailoutReason dont_optimize_reason() { return dont_optimize_reason_; }
2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987

 private:
  template<class> friend class AstNodeFactory;

  // Node visitors.
#define DEF_VISIT(type) \
  void Visit##type(type* node);
  AST_NODE_LIST(DEF_VISIT)
#undef DEF_VISIT

  void increase_node_count() { properties_.add_node_count(1); }
  void add_flag(AstPropertiesFlag flag) { properties_.flags()->Add(flag); }
2988 2989 2990
  void set_dont_optimize_reason(BailoutReason reason) {
      dont_optimize_reason_ = reason;
  }
2991

2992
  void add_slot_node(FeedbackSlotInterface* slot_node) {
2993 2994 2995 2996 2997
    int count = slot_node->ComputeFeedbackSlotCount();
    if (count > 0) {
      slot_node->SetFirstFeedbackSlot(properties_.feedback_slots());
      properties_.increase_feedback_slots(count);
    }
2998 2999
  }

3000
  AstProperties properties_;
3001
  BailoutReason dont_optimize_reason_;
3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019
};


class AstNullVisitor BASE_EMBEDDED {
 public:
  // Node visitors.
#define DEF_VISIT(type) \
  void Visit##type(type* node) {}
  AST_NODE_LIST(DEF_VISIT)
#undef DEF_VISIT
};



// ----------------------------------------------------------------------------
// AstNode factory

template<class Visitor>
3020
class AstNodeFactory V8_FINAL BASE_EMBEDDED {
3021
 public:
3022 3023
  explicit AstNodeFactory(Zone* zone, AstValueFactory* ast_value_factory)
      : zone_(zone), ast_value_factory_(ast_value_factory) {}
3024 3025 3026 3027 3028 3029 3030

  Visitor* visitor() { return &visitor_; }

#define VISIT_AND_RETURN(NodeType, node) \
  visitor_.Visit##NodeType((node)); \
  return node;

3031 3032
  VariableDeclaration* NewVariableDeclaration(VariableProxy* proxy,
                                              VariableMode mode,
3033 3034
                                              Scope* scope,
                                              int pos) {
3035
    VariableDeclaration* decl =
3036
        new(zone_) VariableDeclaration(zone_, proxy, mode, scope, pos);
3037 3038 3039
    VISIT_AND_RETURN(VariableDeclaration, decl)
  }

3040 3041 3042
  FunctionDeclaration* NewFunctionDeclaration(VariableProxy* proxy,
                                              VariableMode mode,
                                              FunctionLiteral* fun,
3043 3044
                                              Scope* scope,
                                              int pos) {
3045
    FunctionDeclaration* decl =
3046
        new(zone_) FunctionDeclaration(zone_, proxy, mode, fun, scope, pos);
3047 3048 3049
    VISIT_AND_RETURN(FunctionDeclaration, decl)
  }

3050 3051
  ModuleDeclaration* NewModuleDeclaration(VariableProxy* proxy,
                                          Module* module,
3052 3053
                                          Scope* scope,
                                          int pos) {
3054
    ModuleDeclaration* decl =
3055
        new(zone_) ModuleDeclaration(zone_, proxy, module, scope, pos);
3056 3057 3058
    VISIT_AND_RETURN(ModuleDeclaration, decl)
  }

3059 3060
  ImportDeclaration* NewImportDeclaration(VariableProxy* proxy,
                                          Module* module,
3061 3062
                                          Scope* scope,
                                          int pos) {
3063
    ImportDeclaration* decl =
3064
        new(zone_) ImportDeclaration(zone_, proxy, module, scope, pos);
3065 3066 3067 3068
    VISIT_AND_RETURN(ImportDeclaration, decl)
  }

  ExportDeclaration* NewExportDeclaration(VariableProxy* proxy,
3069 3070
                                          Scope* scope,
                                          int pos) {
3071
    ExportDeclaration* decl =
3072
        new(zone_) ExportDeclaration(zone_, proxy, scope, pos);
3073 3074 3075
    VISIT_AND_RETURN(ExportDeclaration, decl)
  }

3076
  ModuleLiteral* NewModuleLiteral(Block* body, Interface* interface, int pos) {
3077 3078
    ModuleLiteral* module =
        new(zone_) ModuleLiteral(zone_, body, interface, pos);
3079 3080 3081
    VISIT_AND_RETURN(ModuleLiteral, module)
  }

3082
  ModuleVariable* NewModuleVariable(VariableProxy* proxy, int pos) {
3083
    ModuleVariable* module = new(zone_) ModuleVariable(zone_, proxy, pos);
3084
    VISIT_AND_RETURN(ModuleVariable, module)
3085 3086
  }

3087 3088
  ModulePath* NewModulePath(Module* origin, const AstRawString* name, int pos) {
    ModulePath* module = new (zone_) ModulePath(zone_, origin, name, pos);
3089
    VISIT_AND_RETURN(ModulePath, module)
3090 3091
  }

3092
  ModuleUrl* NewModuleUrl(Handle<String> url, int pos) {
3093
    ModuleUrl* module = new(zone_) ModuleUrl(zone_, url, pos);
3094
    VISIT_AND_RETURN(ModuleUrl, module)
3095 3096
  }

3097
  Block* NewBlock(ZoneList<const AstRawString*>* labels,
3098
                  int capacity,
3099 3100
                  bool is_initializer_block,
                  int pos) {
3101
    Block* block = new(zone_) Block(
3102
        zone_, labels, capacity, is_initializer_block, pos);
3103 3104 3105 3106
    VISIT_AND_RETURN(Block, block)
  }

#define STATEMENT_WITH_LABELS(NodeType) \
3107
  NodeType* New##NodeType(ZoneList<const AstRawString*>* labels, int pos) { \
3108
    NodeType* stmt = new(zone_) NodeType(zone_, labels, pos); \
3109 3110 3111 3112 3113 3114 3115 3116
    VISIT_AND_RETURN(NodeType, stmt); \
  }
  STATEMENT_WITH_LABELS(DoWhileStatement)
  STATEMENT_WITH_LABELS(WhileStatement)
  STATEMENT_WITH_LABELS(ForStatement)
  STATEMENT_WITH_LABELS(SwitchStatement)
#undef STATEMENT_WITH_LABELS

3117
  ForEachStatement* NewForEachStatement(ForEachStatement::VisitMode visit_mode,
3118
                                        ZoneList<const AstRawString*>* labels,
3119
                                        int pos) {
3120 3121
    switch (visit_mode) {
      case ForEachStatement::ENUMERATE: {
3122
        ForInStatement* stmt = new(zone_) ForInStatement(zone_, labels, pos);
3123 3124 3125
        VISIT_AND_RETURN(ForInStatement, stmt);
      }
      case ForEachStatement::ITERATE: {
3126
        ForOfStatement* stmt = new(zone_) ForOfStatement(zone_, labels, pos);
3127 3128 3129 3130 3131 3132 3133
        VISIT_AND_RETURN(ForOfStatement, stmt);
      }
    }
    UNREACHABLE();
    return NULL;
  }

3134 3135
  ModuleStatement* NewModuleStatement(
      VariableProxy* proxy, Block* body, int pos) {
3136
    ModuleStatement* stmt = new(zone_) ModuleStatement(zone_, proxy, body, pos);
3137 3138 3139
    VISIT_AND_RETURN(ModuleStatement, stmt)
  }

3140
  ExpressionStatement* NewExpressionStatement(Expression* expression, int pos) {
3141 3142
    ExpressionStatement* stmt =
        new(zone_) ExpressionStatement(zone_, expression, pos);
3143 3144 3145
    VISIT_AND_RETURN(ExpressionStatement, stmt)
  }

3146
  ContinueStatement* NewContinueStatement(IterationStatement* target, int pos) {
3147
    ContinueStatement* stmt = new(zone_) ContinueStatement(zone_, target, pos);
3148 3149 3150
    VISIT_AND_RETURN(ContinueStatement, stmt)
  }

3151
  BreakStatement* NewBreakStatement(BreakableStatement* target, int pos) {
3152
    BreakStatement* stmt = new(zone_) BreakStatement(zone_, target, pos);
3153 3154 3155
    VISIT_AND_RETURN(BreakStatement, stmt)
  }

3156
  ReturnStatement* NewReturnStatement(Expression* expression, int pos) {
3157
    ReturnStatement* stmt = new(zone_) ReturnStatement(zone_, expression, pos);
3158 3159 3160
    VISIT_AND_RETURN(ReturnStatement, stmt)
  }

3161 3162
  WithStatement* NewWithStatement(Scope* scope,
                                  Expression* expression,
3163 3164
                                  Statement* statement,
                                  int pos) {
3165
    WithStatement* stmt = new(zone_) WithStatement(
3166
        zone_, scope, expression, statement, pos);
3167 3168 3169 3170 3171
    VISIT_AND_RETURN(WithStatement, stmt)
  }

  IfStatement* NewIfStatement(Expression* condition,
                              Statement* then_statement,
3172 3173
                              Statement* else_statement,
                              int pos) {
3174
    IfStatement* stmt = new(zone_) IfStatement(
3175
        zone_, condition, then_statement, else_statement, pos);
3176 3177 3178 3179 3180 3181 3182
    VISIT_AND_RETURN(IfStatement, stmt)
  }

  TryCatchStatement* NewTryCatchStatement(int index,
                                          Block* try_block,
                                          Scope* scope,
                                          Variable* variable,
3183 3184
                                          Block* catch_block,
                                          int pos) {
3185
    TryCatchStatement* stmt = new(zone_) TryCatchStatement(
3186
        zone_, index, try_block, scope, variable, catch_block, pos);
3187 3188 3189 3190 3191
    VISIT_AND_RETURN(TryCatchStatement, stmt)
  }

  TryFinallyStatement* NewTryFinallyStatement(int index,
                                              Block* try_block,
3192 3193
                                              Block* finally_block,
                                              int pos) {
3194 3195
    TryFinallyStatement* stmt = new(zone_) TryFinallyStatement(
        zone_, index, try_block, finally_block, pos);
3196 3197 3198
    VISIT_AND_RETURN(TryFinallyStatement, stmt)
  }

3199
  DebuggerStatement* NewDebuggerStatement(int pos) {
3200
    DebuggerStatement* stmt = new(zone_) DebuggerStatement(zone_, pos);
3201 3202 3203
    VISIT_AND_RETURN(DebuggerStatement, stmt)
  }

3204
  EmptyStatement* NewEmptyStatement(int pos) {
3205
    return new(zone_) EmptyStatement(zone_, pos);
3206 3207
  }

3208 3209 3210
  CaseClause* NewCaseClause(
      Expression* label, ZoneList<Statement*>* statements, int pos) {
    CaseClause* clause =
3211
        new(zone_) CaseClause(zone_, label, statements, pos);
3212 3213 3214
    VISIT_AND_RETURN(CaseClause, clause)
  }

3215 3216 3217 3218 3219 3220 3221 3222 3223 3224
  Literal* NewStringLiteral(const AstRawString* string, int pos) {
    Literal* lit =
        new (zone_) Literal(zone_, ast_value_factory_->NewString(string), pos);
    VISIT_AND_RETURN(Literal, lit)
  }

  // A JavaScript symbol (ECMA-262 edition 6).
  Literal* NewSymbolLiteral(const char* name, int pos) {
    Literal* lit =
        new (zone_) Literal(zone_, ast_value_factory_->NewSymbol(name), pos);
3225 3226 3227
    VISIT_AND_RETURN(Literal, lit)
  }

3228
  Literal* NewNumberLiteral(double number, int pos) {
3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268
    Literal* lit = new (zone_)
        Literal(zone_, ast_value_factory_->NewNumber(number), pos);
    VISIT_AND_RETURN(Literal, lit)
  }

  Literal* NewSmiLiteral(int number, int pos) {
    Literal* lit =
        new (zone_) Literal(zone_, ast_value_factory_->NewSmi(number), pos);
    VISIT_AND_RETURN(Literal, lit)
  }

  Literal* NewBooleanLiteral(bool b, int pos) {
    Literal* lit =
        new (zone_) Literal(zone_, ast_value_factory_->NewBoolean(b), pos);
    VISIT_AND_RETURN(Literal, lit)
  }

  Literal* NewStringListLiteral(ZoneList<const AstRawString*>* strings,
                                int pos) {
    Literal* lit = new (zone_)
        Literal(zone_, ast_value_factory_->NewStringList(strings), pos);
    VISIT_AND_RETURN(Literal, lit)
  }

  Literal* NewNullLiteral(int pos) {
    Literal* lit =
        new (zone_) Literal(zone_, ast_value_factory_->NewNull(), pos);
    VISIT_AND_RETURN(Literal, lit)
  }

  Literal* NewUndefinedLiteral(int pos) {
    Literal* lit =
        new (zone_) Literal(zone_, ast_value_factory_->NewUndefined(), pos);
    VISIT_AND_RETURN(Literal, lit)
  }

  Literal* NewTheHoleLiteral(int pos) {
    Literal* lit =
        new (zone_) Literal(zone_, ast_value_factory_->NewTheHole(), pos);
    VISIT_AND_RETURN(Literal, lit)
3269 3270 3271 3272 3273
  }

  ObjectLiteral* NewObjectLiteral(
      ZoneList<ObjectLiteral::Property*>* properties,
      int literal_index,
3274
      int boilerplate_properties,
3275 3276
      bool has_function,
      int pos) {
3277
    ObjectLiteral* lit = new(zone_) ObjectLiteral(
3278
        zone_, properties, literal_index, boilerplate_properties,
3279
        has_function, pos);
3280 3281 3282
    VISIT_AND_RETURN(ObjectLiteral, lit)
  }

3283 3284
  ObjectLiteral::Property* NewObjectLiteralProperty(Literal* key,
                                                    Expression* value) {
3285 3286
    return new (zone_)
        ObjectLiteral::Property(zone_, ast_value_factory_, key, value);
3287 3288
  }

3289
  ObjectLiteral::Property* NewObjectLiteralProperty(bool is_getter,
3290 3291
                                                    FunctionLiteral* value,
                                                    int pos) {
3292
    ObjectLiteral::Property* prop =
3293
        new(zone_) ObjectLiteral::Property(zone_, is_getter, value);
3294
    prop->set_key(NewStringLiteral(value->raw_name(), pos));
3295 3296 3297
    return prop;  // Not an AST node, will not be visited.
  }

3298 3299
  RegExpLiteral* NewRegExpLiteral(const AstRawString* pattern,
                                  const AstRawString* flags,
3300 3301
                                  int literal_index,
                                  int pos) {
3302
    RegExpLiteral* lit =
3303
        new(zone_) RegExpLiteral(zone_, pattern, flags, literal_index, pos);
3304 3305 3306
    VISIT_AND_RETURN(RegExpLiteral, lit);
  }

3307
  ArrayLiteral* NewArrayLiteral(ZoneList<Expression*>* values,
3308
                                int literal_index,
3309
                                int pos) {
3310
    ArrayLiteral* lit = new(zone_) ArrayLiteral(
3311
        zone_, values, literal_index, pos);
3312 3313 3314
    VISIT_AND_RETURN(ArrayLiteral, lit)
  }

3315 3316
  VariableProxy* NewVariableProxy(Variable* var,
                                  int pos = RelocInfo::kNoPosition) {
3317
    VariableProxy* proxy = new(zone_) VariableProxy(zone_, var, pos);
3318 3319 3320
    VISIT_AND_RETURN(VariableProxy, proxy)
  }

3321
  VariableProxy* NewVariableProxy(const AstRawString* name,
3322
                                  bool is_this,
3323 3324
                                  Interface* interface = Interface::NewValue(),
                                  int position = RelocInfo::kNoPosition) {
3325
    VariableProxy* proxy =
3326
        new(zone_) VariableProxy(zone_, name, is_this, interface, position);
3327 3328 3329 3330
    VISIT_AND_RETURN(VariableProxy, proxy)
  }

  Property* NewProperty(Expression* obj, Expression* key, int pos) {
3331
    Property* prop = new(zone_) Property(zone_, obj, key, pos);
3332 3333 3334 3335 3336 3337
    VISIT_AND_RETURN(Property, prop)
  }

  Call* NewCall(Expression* expression,
                ZoneList<Expression*>* arguments,
                int pos) {
3338
    Call* call = new(zone_) Call(zone_, expression, arguments, pos);
3339 3340 3341 3342 3343 3344
    VISIT_AND_RETURN(Call, call)
  }

  CallNew* NewCallNew(Expression* expression,
                      ZoneList<Expression*>* arguments,
                      int pos) {
3345
    CallNew* call = new(zone_) CallNew(zone_, expression, arguments, pos);
3346 3347 3348
    VISIT_AND_RETURN(CallNew, call)
  }

3349
  CallRuntime* NewCallRuntime(const AstRawString* name,
3350
                              const Runtime::Function* function,
3351 3352
                              ZoneList<Expression*>* arguments,
                              int pos) {
3353
    CallRuntime* call =
3354
        new(zone_) CallRuntime(zone_, name, function, arguments, pos);
3355 3356 3357 3358 3359 3360 3361
    VISIT_AND_RETURN(CallRuntime, call)
  }

  UnaryOperation* NewUnaryOperation(Token::Value op,
                                    Expression* expression,
                                    int pos) {
    UnaryOperation* node =
3362
        new(zone_) UnaryOperation(zone_, op, expression, pos);
3363 3364 3365 3366 3367 3368 3369 3370
    VISIT_AND_RETURN(UnaryOperation, node)
  }

  BinaryOperation* NewBinaryOperation(Token::Value op,
                                      Expression* left,
                                      Expression* right,
                                      int pos) {
    BinaryOperation* node =
3371
        new(zone_) BinaryOperation(zone_, op, left, right, pos);
3372 3373 3374 3375 3376 3377 3378 3379
    VISIT_AND_RETURN(BinaryOperation, node)
  }

  CountOperation* NewCountOperation(Token::Value op,
                                    bool is_prefix,
                                    Expression* expr,
                                    int pos) {
    CountOperation* node =
3380
        new(zone_) CountOperation(zone_, op, is_prefix, expr, pos);
3381 3382 3383 3384 3385 3386 3387 3388
    VISIT_AND_RETURN(CountOperation, node)
  }

  CompareOperation* NewCompareOperation(Token::Value op,
                                        Expression* left,
                                        Expression* right,
                                        int pos) {
    CompareOperation* node =
3389
        new(zone_) CompareOperation(zone_, op, left, right, pos);
3390 3391 3392 3393 3394 3395
    VISIT_AND_RETURN(CompareOperation, node)
  }

  Conditional* NewConditional(Expression* condition,
                              Expression* then_expression,
                              Expression* else_expression,
3396
                              int position) {
3397
    Conditional* cond = new(zone_) Conditional(
3398
        zone_, condition, then_expression, else_expression, position);
3399 3400 3401 3402 3403 3404 3405 3406
    VISIT_AND_RETURN(Conditional, cond)
  }

  Assignment* NewAssignment(Token::Value op,
                            Expression* target,
                            Expression* value,
                            int pos) {
    Assignment* assign =
3407 3408
        new(zone_) Assignment(zone_, op, target, value, pos);
    assign->Init(zone_, this);
3409 3410 3411
    VISIT_AND_RETURN(Assignment, assign)
  }

3412 3413
  Yield* NewYield(Expression *generator_object,
                  Expression* expression,
3414
                  Yield::Kind yield_kind,
3415
                  int pos) {
3416
    if (!expression) expression = NewUndefinedLiteral(pos);
3417
    Yield* yield = new(zone_) Yield(
3418
        zone_, generator_object, expression, yield_kind, pos);
3419 3420 3421
    VISIT_AND_RETURN(Yield, yield)
  }

3422
  Throw* NewThrow(Expression* exception, int pos) {
3423
    Throw* t = new(zone_) Throw(zone_, exception, pos);
3424 3425 3426 3427
    VISIT_AND_RETURN(Throw, t)
  }

  FunctionLiteral* NewFunctionLiteral(
3428 3429 3430
      const AstRawString* name, AstValueFactory* ast_value_factory,
      Scope* scope, ZoneList<Statement*>* body, int materialized_literal_count,
      int expected_property_count, int handler_count, int parameter_count,
3431
      FunctionLiteral::ParameterFlag has_duplicate_parameters,
3432
      FunctionLiteral::FunctionType function_type,
3433
      FunctionLiteral::IsFunctionFlag is_function,
3434
      FunctionLiteral::IsParenthesizedFlag is_parenthesized,
3435 3436 3437 3438 3439 3440
      FunctionLiteral::KindFlag kind, int position) {
    FunctionLiteral* lit = new (zone_) FunctionLiteral(
        zone_, name, ast_value_factory, scope, body, materialized_literal_count,
        expected_property_count, handler_count, parameter_count, function_type,
        has_duplicate_parameters, is_function, is_parenthesized, kind,
        position);
3441 3442
    // Top-level literal doesn't count for the AST's properties.
    if (is_function == FunctionLiteral::kIsFunction) {
3443 3444 3445 3446 3447
      visitor_.VisitFunctionLiteral(lit);
    }
    return lit;
  }

3448
  NativeFunctionLiteral* NewNativeFunctionLiteral(
3449 3450
      const AstRawString* name, v8::Extension* extension,
      int pos) {
3451
    NativeFunctionLiteral* lit =
3452
        new(zone_) NativeFunctionLiteral(zone_, name, extension, pos);
3453
    VISIT_AND_RETURN(NativeFunctionLiteral, lit)
3454 3455
  }

3456
  ThisFunction* NewThisFunction(int pos) {
3457
    ThisFunction* fun = new(zone_) ThisFunction(zone_, pos);
3458 3459 3460 3461 3462 3463 3464 3465
    VISIT_AND_RETURN(ThisFunction, fun)
  }

#undef VISIT_AND_RETURN

 private:
  Zone* zone_;
  Visitor visitor_;
3466
  AstValueFactory* ast_value_factory_;
3467 3468 3469
};


3470 3471 3472
} }  // namespace v8::internal

#endif  // V8_AST_H_