prettyprinter.h 4.05 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
#ifndef V8_AST_PRETTYPRINTER_H_
#define V8_AST_PRETTYPRINTER_H_
7

8
#include "src/allocation.h"
9
#include "src/ast/ast.h"
jfb's avatar
jfb committed
10
#include "src/base/compiler-specific.h"
11

12 13
namespace v8 {
namespace internal {
14

15 16
class IncrementalStringBuilder;  // to avoid including string-builder-inl.h

17
class CallPrinter final : public AstVisitor<CallPrinter> {
18
 public:
19
  explicit CallPrinter(Isolate* isolate, bool is_user_js);
20
  ~CallPrinter();
21 22

  // The following routine prints the node with position |position| into a
23 24
  // string.
  Handle<String> Print(FunctionLiteral* program, int position);
25 26 27 28 29 30 31 32
  enum ErrorHint {
    kNone,
    kNormalIterator,
    kAsyncIterator,
    kCallAndNormalIterator,
    kCallAndAsyncIterator
  };
  ErrorHint GetErrorHint() const;
33 34

// Individual nodes
35
#define DECLARE_VISIT(type) void Visit##type(type* node);
36 37 38 39
  AST_NODE_LIST(DECLARE_VISIT)
#undef DECLARE_VISIT

 private:
40 41 42 43 44
  void Print(const char* str);
  void Print(Handle<String> str);

  void Find(AstNode* node, bool print = false);

45
  Isolate* isolate_;
46
  int num_prints_;
47 48
  // Allocate the builder on the heap simply because it's forward declared.
  std::unique_ptr<IncrementalStringBuilder> builder_;
49 50 51
  int position_;  // position of ast node to print
  bool found_;
  bool done_;
52
  bool is_user_js_;
53 54 55
  bool is_iterator_error_;
  bool is_async_iterator_error_;
  bool is_call_error_;
56
  FunctionKind function_kind_;
57 58 59
  DEFINE_AST_VISITOR_SUBCLASS_MEMBERS();

 protected:
60
  void PrintLiteral(Handle<Object> value, bool quote);
61
  void PrintLiteral(const AstRawString* value, bool quote);
62 63
  void FindStatements(ZonePtrList<Statement>* statements);
  void FindArguments(ZonePtrList<Expression>* arguments);
64 65 66
};


67 68
#ifdef DEBUG

69
class AstPrinter final : public AstVisitor<AstPrinter> {
70
 public:
71
  explicit AstPrinter(uintptr_t stack_limit);
72
  ~AstPrinter();
73 74

  // The following routines print a node into a string.
75
  // The result string is alive as long as the AstPrinter is alive.
76
  const char* Print(AstNode* node);
77 78
  const char* PrintProgram(FunctionLiteral* program);

jfb's avatar
jfb committed
79
  void PRINTF_FORMAT(2, 3) Print(const char* format, ...);
80

81
  // Print a node to stdout.
82
  static void PrintOut(Isolate* isolate, AstNode* node);
83 84

  // Individual nodes
85
#define DECLARE_VISIT(type) void Visit##type(type* node);
86 87
  AST_NODE_LIST(DECLARE_VISIT)
#undef DECLARE_VISIT
88 89

 private:
90
  friend class IndentedScope;
91 92 93

  void Init();

94
  void PrintLabels(ZonePtrList<const AstRawString>* labels);
95
  void PrintLiteral(const AstRawString* value, bool quote);
96 97
  void PrintLiteral(const AstConsString* value, bool quote);
  void PrintLiteral(Literal* literal, bool quote);
98
  void PrintIndented(const char* txt);
99
  void PrintIndentedVisit(const char* s, AstNode* node);
100

101
  void PrintStatements(ZonePtrList<Statement>* statements);
102
  void PrintDeclarations(Declaration::List* declarations);
103
  void PrintParameters(DeclarationScope* scope);
104
  void PrintArguments(ZonePtrList<Expression>* arguments);
105
  void PrintCaseClause(CaseClause* clause);
106 107
  void PrintLiteralIndented(const char* info, Literal* literal, bool quote);
  void PrintLiteralIndented(const char* info, const AstRawString* value,
108
                            bool quote);
109 110 111 112
  void PrintLiteralIndented(const char* info, const AstConsString* value,
                            bool quote);
  void PrintLiteralWithModeIndented(const char* info, Variable* var,
                                    const AstRawString* value);
113 114 115
  void PrintLabelsIndented(ZonePtrList<const AstRawString>* labels);
  void PrintObjectProperties(ZonePtrList<ObjectLiteral::Property>* properties);
  void PrintClassProperties(ZonePtrList<ClassLiteral::Property>* properties);
116 117 118 119

  void inc_indent() { indent_++; }
  void dec_indent() { indent_--; }

120 121 122 123 124
  DEFINE_AST_VISITOR_SUBCLASS_MEMBERS();

  char* output_;  // output string buffer
  int size_;      // output_ size
  int pos_;       // current printing position
125
  int indent_;
126 127 128 129
};

#endif  // DEBUG

130 131
}  // namespace internal
}  // namespace v8
132

133
#endif  // V8_AST_PRETTYPRINTER_H_