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 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 24 void PRINTF_FORMAT(2, 3) Print(const char* format, ...); 25 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 Isolate* isolate_; 36 char* output_; // output string buffer 37 int size_; // output_ size 38 int pos_; // current printing position 39 int position_; // position of ast node to print 40 bool found_; 41 bool done_; 42 bool is_builtin_; 43 44 DEFINE_AST_VISITOR_SUBCLASS_MEMBERS(); 45 46 protected: 47 void PrintLiteral(Object* value, bool quote); 48 void PrintLiteral(const AstRawString* value, bool quote); 49 void FindStatements(ZoneList<Statement*>* statements); 50 void FindArguments(ZoneList<Expression*>* arguments); 51 }; 52 53 54 #ifdef DEBUG 55 56 class PrettyPrinter: public AstVisitor { 57 public: 58 explicit PrettyPrinter(Isolate* isolate); 59 virtual ~PrettyPrinter(); 60 61 // The following routines print a node into a string. 62 // The result string is alive as long as the PrettyPrinter is alive. 63 const char* Print(AstNode* node); 64 const char* PrintExpression(FunctionLiteral* program); 65 const char* PrintProgram(FunctionLiteral* program); 66 67 void PRINTF_FORMAT(2, 3) Print(const char* format, ...); 68 69 // Print a node to stdout. 70 static void PrintOut(Isolate* isolate, AstNode* node); 71 72 // Individual nodes 73 #define DECLARE_VISIT(type) void Visit##type(type* node) override; 74 AST_NODE_LIST(DECLARE_VISIT) 75 #undef DECLARE_VISIT 76 77 private: 78 Isolate* isolate_; 79 char* output_; // output string buffer 80 int size_; // output_ size 81 int pos_; // current printing position 82 83 protected: 84 void Init(); Output()85 const char* Output() const { return output_; } 86 87 virtual void PrintStatements(ZoneList<Statement*>* statements); 88 void PrintLabels(ZoneList<const AstRawString*>* labels); 89 virtual void PrintArguments(ZoneList<Expression*>* arguments); 90 void PrintLiteral(Handle<Object> value, bool quote); 91 void PrintLiteral(const AstRawString* value, bool quote); 92 void PrintParameters(Scope* scope); 93 void PrintDeclarations(ZoneList<Declaration*>* declarations); 94 void PrintFunctionLiteral(FunctionLiteral* function); 95 void PrintCaseClause(CaseClause* clause); 96 void PrintObjectLiteralProperty(ObjectLiteralProperty* property); 97 98 DEFINE_AST_VISITOR_SUBCLASS_MEMBERS(); 99 }; 100 101 102 // Prints the AST structure 103 class AstPrinter: public PrettyPrinter { 104 public: 105 explicit AstPrinter(Isolate* isolate); 106 virtual ~AstPrinter(); 107 108 const char* PrintProgram(FunctionLiteral* program); 109 110 // Print a node to stdout. 111 static void PrintOut(Isolate* isolate, AstNode* node); 112 113 // Individual nodes 114 #define DECLARE_VISIT(type) virtual void Visit##type(type* node); 115 AST_NODE_LIST(DECLARE_VISIT) 116 #undef DECLARE_VISIT 117 118 private: 119 friend class IndentedScope; 120 void PrintIndented(const char* txt); 121 void PrintIndentedVisit(const char* s, AstNode* node); 122 123 void PrintStatements(ZoneList<Statement*>* statements); 124 void PrintDeclarations(ZoneList<Declaration*>* declarations); 125 void PrintParameters(Scope* scope); 126 void PrintArguments(ZoneList<Expression*>* arguments); 127 void PrintCaseClause(CaseClause* clause); 128 void PrintLiteralIndented(const char* info, Handle<Object> value, bool quote); 129 void PrintLiteralWithModeIndented(const char* info, 130 Variable* var, 131 Handle<Object> value); 132 void PrintLabelsIndented(ZoneList<const AstRawString*>* labels); 133 void PrintProperties(ZoneList<ObjectLiteral::Property*>* properties); 134 inc_indent()135 void inc_indent() { indent_++; } dec_indent()136 void dec_indent() { indent_--; } 137 138 int indent_; 139 }; 140 141 #endif // DEBUG 142 143 } // namespace internal 144 } // namespace v8 145 146 #endif // V8_AST_PRETTYPRINTER_H_ 147