prettyprinter.h 5 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
  enum class ErrorHint {
33 34 35 36 37 38
    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(char c);
56
  void Print(const char* str);
57
  void Print(Handle<String> str);
58 59 60

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

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

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


87 88
#ifdef DEBUG

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

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

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

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

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

 private:
110
  friend class IndentedScope;
111 112 113

  void Init();

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

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

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

146 147 148 149 150
  DEFINE_AST_VISITOR_SUBCLASS_MEMBERS();

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

#endif  // DEBUG

156 157
}  // namespace internal
}  // namespace v8
158

159
#endif  // V8_AST_PRETTYPRINTER_H_