blob: 4e90294ca987a9d4b707fb8519608671613219ae [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// Copyright 2012 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_AST_PRETTYPRINTER_H_
6#define V8_AST_PRETTYPRINTER_H_
7
8#include "src/allocation.h"
9#include "src/ast/ast.h"
Ben Murdochc5610432016-08-08 18:44:38 +010010#include "src/base/compiler-specific.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000011
12namespace v8 {
13namespace internal {
14
15class CallPrinter : public AstVisitor {
16 public:
17 explicit CallPrinter(Isolate* isolate, bool is_builtin);
18 virtual ~CallPrinter();
19
20 // The following routine prints the node with position |position| into a
21 // string. The result string is alive as long as the CallPrinter is alive.
22 const char* Print(FunctionLiteral* program, int position);
23
Ben Murdochc5610432016-08-08 18:44:38 +010024 void PRINTF_FORMAT(2, 3) Print(const char* format, ...);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000025
26 void Find(AstNode* node, bool print = false);
27
28// Individual nodes
29#define DECLARE_VISIT(type) void Visit##type(type* node) override;
30 AST_NODE_LIST(DECLARE_VISIT)
31#undef DECLARE_VISIT
32
33 private:
34 void Init();
35 char* output_; // output string buffer
36 int size_; // output_ size
37 int pos_; // current printing position
38 int position_; // position of ast node to print
39 bool found_;
40 bool done_;
41 bool is_builtin_;
42
43 DEFINE_AST_VISITOR_SUBCLASS_MEMBERS();
44
45 protected:
46 void PrintLiteral(Object* value, bool quote);
47 void PrintLiteral(const AstRawString* value, bool quote);
48 void FindStatements(ZoneList<Statement*>* statements);
49 void FindArguments(ZoneList<Expression*>* arguments);
50};
51
52
53#ifdef DEBUG
54
55class PrettyPrinter: public AstVisitor {
56 public:
57 explicit PrettyPrinter(Isolate* isolate);
58 virtual ~PrettyPrinter();
59
60 // The following routines print a node into a string.
61 // The result string is alive as long as the PrettyPrinter is alive.
62 const char* Print(AstNode* node);
63 const char* PrintExpression(FunctionLiteral* program);
64 const char* PrintProgram(FunctionLiteral* program);
65
Ben Murdochc5610432016-08-08 18:44:38 +010066 void PRINTF_FORMAT(2, 3) Print(const char* format, ...);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000067
68 // Print a node to stdout.
69 static void PrintOut(Isolate* isolate, AstNode* node);
70
71 // Individual nodes
72#define DECLARE_VISIT(type) void Visit##type(type* node) override;
73 AST_NODE_LIST(DECLARE_VISIT)
74#undef DECLARE_VISIT
75
76 private:
77 char* output_; // output string buffer
78 int size_; // output_ size
79 int pos_; // current printing position
80
81 protected:
82 void Init();
83 const char* Output() const { return output_; }
84
85 virtual void PrintStatements(ZoneList<Statement*>* statements);
86 void PrintLabels(ZoneList<const AstRawString*>* labels);
87 virtual void PrintArguments(ZoneList<Expression*>* arguments);
88 void PrintLiteral(Handle<Object> value, bool quote);
89 void PrintLiteral(const AstRawString* value, bool quote);
90 void PrintParameters(Scope* scope);
91 void PrintDeclarations(ZoneList<Declaration*>* declarations);
92 void PrintFunctionLiteral(FunctionLiteral* function);
93 void PrintCaseClause(CaseClause* clause);
94 void PrintObjectLiteralProperty(ObjectLiteralProperty* property);
95
96 DEFINE_AST_VISITOR_SUBCLASS_MEMBERS();
97};
98
99
100// Prints the AST structure
101class AstPrinter: public PrettyPrinter {
102 public:
103 explicit AstPrinter(Isolate* isolate);
104 virtual ~AstPrinter();
105
106 const char* PrintProgram(FunctionLiteral* program);
107
Ben Murdoch097c5b22016-05-18 11:27:45 +0100108 // Print a node to stdout.
109 static void PrintOut(Isolate* isolate, AstNode* node);
110
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000111 // Individual nodes
112#define DECLARE_VISIT(type) virtual void Visit##type(type* node);
113 AST_NODE_LIST(DECLARE_VISIT)
114#undef DECLARE_VISIT
115
116 private:
117 friend class IndentedScope;
118 void PrintIndented(const char* txt);
119 void PrintIndentedVisit(const char* s, AstNode* node);
120
121 void PrintStatements(ZoneList<Statement*>* statements);
122 void PrintDeclarations(ZoneList<Declaration*>* declarations);
123 void PrintParameters(Scope* scope);
124 void PrintArguments(ZoneList<Expression*>* arguments);
125 void PrintCaseClause(CaseClause* clause);
126 void PrintLiteralIndented(const char* info, Handle<Object> value, bool quote);
127 void PrintLiteralWithModeIndented(const char* info,
128 Variable* var,
129 Handle<Object> value);
130 void PrintLabelsIndented(ZoneList<const AstRawString*>* labels);
131 void PrintProperties(ZoneList<ObjectLiteral::Property*>* properties);
132
133 void inc_indent() { indent_++; }
134 void dec_indent() { indent_--; }
135
136 int indent_;
137};
138
139#endif // DEBUG
140
141} // namespace internal
142} // namespace v8
143
144#endif // V8_AST_PRETTYPRINTER_H_