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