Line data Source code
1 : // 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"
10 : #include "src/base/compiler-specific.h"
11 : #include "src/function-kind.h"
12 :
13 : namespace v8 {
14 : namespace internal {
15 :
16 : class IncrementalStringBuilder; // to avoid including string-builder-inl.h
17 :
18 13488 : class CallPrinter final : public AstVisitor<CallPrinter> {
19 : public:
20 : explicit CallPrinter(Isolate* isolate, bool is_user_js);
21 : ~CallPrinter();
22 :
23 : // The following routine prints the node with position |position| into a
24 : // string.
25 : Handle<String> Print(FunctionLiteral* program, int position);
26 : enum ErrorHint {
27 : kNone,
28 : kNormalIterator,
29 : kAsyncIterator,
30 : kCallAndNormalIterator,
31 : kCallAndAsyncIterator
32 : };
33 : ErrorHint GetErrorHint() const;
34 :
35 : // Individual nodes
36 : #define DECLARE_VISIT(type) void Visit##type(type* node);
37 : AST_NODE_LIST(DECLARE_VISIT)
38 : #undef DECLARE_VISIT
39 :
40 : private:
41 : void Print(const char* str);
42 : void Print(Handle<String> str);
43 :
44 : void Find(AstNode* node, bool print = false);
45 :
46 : Isolate* isolate_;
47 : int num_prints_;
48 : // Allocate the builder on the heap simply because it's forward declared.
49 : std::unique_ptr<IncrementalStringBuilder> builder_;
50 : int position_; // position of ast node to print
51 : bool found_;
52 : bool done_;
53 : bool is_user_js_;
54 : bool is_iterator_error_;
55 : bool is_async_iterator_error_;
56 : bool is_call_error_;
57 : FunctionKind function_kind_;
58 599603 : DEFINE_AST_VISITOR_SUBCLASS_MEMBERS();
59 :
60 : protected:
61 : void PrintLiteral(Handle<Object> value, bool quote);
62 : void PrintLiteral(const AstRawString* value, bool quote);
63 : void FindStatements(const ZonePtrList<Statement>* statements);
64 : void FindArguments(const ZonePtrList<Expression>* arguments);
65 : };
66 :
67 :
68 : #ifdef DEBUG
69 :
70 : class AstPrinter final : public AstVisitor<AstPrinter> {
71 : public:
72 : explicit AstPrinter(uintptr_t stack_limit);
73 : ~AstPrinter();
74 :
75 : // The following routines print a node into a string.
76 : // The result string is alive as long as the AstPrinter is alive.
77 : const char* Print(AstNode* node);
78 : const char* PrintProgram(FunctionLiteral* program);
79 :
80 : void PRINTF_FORMAT(2, 3) Print(const char* format, ...);
81 :
82 : // Print a node to stdout.
83 : static void PrintOut(Isolate* isolate, AstNode* node);
84 :
85 : // Individual nodes
86 : #define DECLARE_VISIT(type) void Visit##type(type* node);
87 : AST_NODE_LIST(DECLARE_VISIT)
88 : #undef DECLARE_VISIT
89 :
90 : private:
91 : friend class IndentedScope;
92 :
93 : void Init();
94 :
95 : void PrintLabels(ZonePtrList<const AstRawString>* labels);
96 : void PrintLiteral(const AstRawString* value, bool quote);
97 : void PrintLiteral(const AstConsString* value, bool quote);
98 : void PrintLiteral(Literal* literal, bool quote);
99 : void PrintIndented(const char* txt);
100 : void PrintIndentedVisit(const char* s, AstNode* node);
101 :
102 : void PrintStatements(const ZonePtrList<Statement>* statements);
103 : void PrintDeclarations(Declaration::List* declarations);
104 : void PrintParameters(DeclarationScope* scope);
105 : void PrintArguments(const ZonePtrList<Expression>* arguments);
106 : void PrintCaseClause(CaseClause* clause);
107 : void PrintLiteralIndented(const char* info, Literal* literal, bool quote);
108 : void PrintLiteralIndented(const char* info, const AstRawString* value,
109 : bool quote);
110 : void PrintLiteralIndented(const char* info, const AstConsString* value,
111 : bool quote);
112 : void PrintLiteralWithModeIndented(const char* info, Variable* var,
113 : const AstRawString* value);
114 : void PrintLabelsIndented(ZonePtrList<const AstRawString>* labels,
115 : const char* prefix = "");
116 : void PrintObjectProperties(
117 : const ZonePtrList<ObjectLiteral::Property>* properties);
118 : void PrintClassProperties(
119 : const ZonePtrList<ClassLiteral::Property>* properties);
120 :
121 : void inc_indent() { indent_++; }
122 : void dec_indent() { indent_--; }
123 :
124 : DEFINE_AST_VISITOR_SUBCLASS_MEMBERS();
125 :
126 : char* output_; // output string buffer
127 : int size_; // output_ size
128 : int pos_; // current printing position
129 : int indent_;
130 : };
131 :
132 : #endif // DEBUG
133 :
134 : } // namespace internal
135 : } // namespace v8
136 :
137 : #endif // V8_AST_PRETTYPRINTER_H_
|