prettyprinter.h 4.97 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 9
#include <memory>

10
#include "src/ast/ast.h"
jfb's avatar
jfb committed
11
#include "src/base/compiler-specific.h"
12
#include "src/objects/function-kind.h"
13
#include "src/utils/allocation.h"
14

15 16
namespace v8 {
namespace internal {
17

18 19
class IncrementalStringBuilder;  // to avoid including string-builder-inl.h

20
class CallPrinter final : public AstVisitor<CallPrinter> {
21
 public:
22 23 24 25 26
  enum class SpreadErrorInArgsHint { kErrorInArgs, kNoErrorInArgs };

  explicit CallPrinter(Isolate* isolate, bool is_user_js,
                       SpreadErrorInArgsHint error_in_spread_args =
                           SpreadErrorInArgsHint::kNoErrorInArgs);
27
  ~CallPrinter();
28 29

  // The following routine prints the node with position |position| into a
30 31
  // string.
  Handle<String> Print(FunctionLiteral* program, int position);
32 33 34 35 36 37 38
  enum ErrorHint {
    kNone,
    kNormalIterator,
    kAsyncIterator,
    kCallAndNormalIterator,
    kCallAndAsyncIterator
  };
39

40
  ErrorHint GetErrorHint() const;
41
  Expression* spread_arg() const { return spread_arg_; }
42 43 44 45 46 47
  ObjectLiteralProperty* destructuring_prop() const {
    return destructuring_prop_;
  }
  Assignment* destructuring_assignment() const {
    return destructuring_assignment_;
  }
48 49

// Individual nodes
50
#define DECLARE_VISIT(type) void Visit##type(type* node);
51 52 53 54
  AST_NODE_LIST(DECLARE_VISIT)
#undef DECLARE_VISIT

 private:
55
  void Print(const char* str);
56
  void Print(Handle<String> str);
57 58 59

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

60
  Isolate* isolate_;
61
  int num_prints_;
62 63
  // Allocate the builder on the heap simply because it's forward declared.
  std::unique_ptr<IncrementalStringBuilder> builder_;
64 65 66
  int position_;  // position of ast node to print
  bool found_;
  bool done_;
67
  bool is_user_js_;
68 69 70
  bool is_iterator_error_;
  bool is_async_iterator_error_;
  bool is_call_error_;
71
  SpreadErrorInArgsHint error_in_spread_args_;
72 73
  ObjectLiteralProperty* destructuring_prop_;
  Assignment* destructuring_assignment_;
74
  Expression* spread_arg_;
75
  FunctionKind function_kind_;
76 77 78
  DEFINE_AST_VISITOR_SUBCLASS_MEMBERS();

 protected:
79
  void PrintLiteral(Handle<Object> value, bool quote);
80
  void PrintLiteral(const AstRawString* value, bool quote);
81 82
  void FindStatements(const ZonePtrList<Statement>* statements);
  void FindArguments(const ZonePtrList<Expression>* arguments);
83 84 85
};


86 87
#ifdef DEBUG

88
class AstPrinter final : public AstVisitor<AstPrinter> {
89
 public:
90
  explicit AstPrinter(uintptr_t stack_limit);
91
  ~AstPrinter();
92 93

  // The following routines print a node into a string.
94
  // The result string is alive as long as the AstPrinter is alive.
95
  const char* Print(AstNode* node);
96 97
  const char* PrintProgram(FunctionLiteral* program);

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

100
  // Print a node to stdout.
101
  static void PrintOut(Isolate* isolate, AstNode* node);
102 103

  // Individual nodes
104
#define DECLARE_VISIT(type) void Visit##type(type* node);
105 106
  AST_NODE_LIST(DECLARE_VISIT)
#undef DECLARE_VISIT
107 108

 private:
109
  friend class IndentedScope;
110 111 112

  void Init();

113
  void PrintLabels(ZonePtrList<const AstRawString>* labels);
114
  void PrintLiteral(const AstRawString* value, bool quote);
115 116
  void PrintLiteral(const AstConsString* value, bool quote);
  void PrintLiteral(Literal* literal, bool quote);
117
  void PrintIndented(const char* txt);
118
  void PrintIndentedVisit(const char* s, AstNode* node);
119

120
  void PrintStatements(const ZonePtrList<Statement>* statements);
121
  void PrintDeclarations(Declaration::List* declarations);
122
  void PrintParameters(DeclarationScope* scope);
123
  void PrintArguments(const ZonePtrList<Expression>* arguments);
124
  void PrintCaseClause(CaseClause* clause);
125 126
  void PrintLiteralIndented(const char* info, Literal* literal, bool quote);
  void PrintLiteralIndented(const char* info, const AstRawString* value,
127
                            bool quote);
128 129 130 131
  void PrintLiteralIndented(const char* info, const AstConsString* value,
                            bool quote);
  void PrintLiteralWithModeIndented(const char* info, Variable* var,
                                    const AstRawString* value);
132 133
  void PrintLabelsIndented(ZonePtrList<const AstRawString>* labels,
                           const char* prefix = "");
134 135
  void PrintObjectProperties(
      const ZonePtrList<ObjectLiteral::Property>* properties);
136
  void PrintClassProperty(ClassLiteral::Property* property);
137 138
  void PrintClassProperties(
      const ZonePtrList<ClassLiteral::Property>* properties);
139 140
  void PrintClassStaticElements(
      const ZonePtrList<ClassLiteral::StaticElement>* static_elements);
141 142 143 144

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

145 146 147 148 149
  DEFINE_AST_VISITOR_SUBCLASS_MEMBERS();

  char* output_;  // output string buffer
  int size_;      // output_ size
  int pos_;       // current printing position
150
  int indent_;
151 152 153 154
};

#endif  // DEBUG

155 156
}  // namespace internal
}  // namespace v8
157

158
#endif  // V8_AST_PRETTYPRINTER_H_