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