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