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