yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #ifndef V8_PRETTYPRINTER_H_ |
| 29 | #define V8_PRETTYPRINTER_H_ |
| 30 | |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 31 | #include "allocation.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 32 | #include "ast.h" |
| 33 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 34 | namespace v8 { |
| 35 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 36 | |
| 37 | #ifdef DEBUG |
| 38 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 39 | class PrettyPrinter: public AstVisitor { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 40 | public: |
| 41 | PrettyPrinter(); |
| 42 | virtual ~PrettyPrinter(); |
| 43 | |
| 44 | // The following routines print a node into a string. |
| 45 | // The result string is alive as long as the PrettyPrinter is alive. |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 46 | const char* Print(AstNode* node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 47 | const char* PrintExpression(FunctionLiteral* program); |
| 48 | const char* PrintProgram(FunctionLiteral* program); |
| 49 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 50 | void Print(const char* format, ...); |
| 51 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 52 | // Print a node to stdout. |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 53 | static void PrintOut(AstNode* node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 54 | |
| 55 | // Individual nodes |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 56 | #define DECLARE_VISIT(type) virtual void Visit##type(type* node); |
| 57 | AST_NODE_LIST(DECLARE_VISIT) |
| 58 | #undef DECLARE_VISIT |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 59 | |
| 60 | private: |
| 61 | char* output_; // output string buffer |
| 62 | int size_; // output_ size |
| 63 | int pos_; // current printing position |
| 64 | |
| 65 | protected: |
| 66 | void Init(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 67 | const char* Output() const { return output_; } |
| 68 | |
| 69 | virtual void PrintStatements(ZoneList<Statement*>* statements); |
| 70 | void PrintLabels(ZoneStringList* labels); |
| 71 | virtual void PrintArguments(ZoneList<Expression*>* arguments); |
| 72 | void PrintLiteral(Handle<Object> value, bool quote); |
| 73 | void PrintParameters(Scope* scope); |
| 74 | void PrintDeclarations(ZoneList<Declaration*>* declarations); |
| 75 | void PrintFunctionLiteral(FunctionLiteral* function); |
| 76 | void PrintCaseClause(CaseClause* clause); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 77 | |
| 78 | DEFINE_AST_VISITOR_SUBCLASS_MEMBERS(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | |
| 82 | // Prints the AST structure |
| 83 | class AstPrinter: public PrettyPrinter { |
| 84 | public: |
| 85 | AstPrinter(); |
| 86 | virtual ~AstPrinter(); |
| 87 | |
| 88 | const char* PrintProgram(FunctionLiteral* program); |
| 89 | |
| 90 | // Individual nodes |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 91 | #define DECLARE_VISIT(type) virtual void Visit##type(type* node); |
| 92 | AST_NODE_LIST(DECLARE_VISIT) |
| 93 | #undef DECLARE_VISIT |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 94 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 95 | private: |
| 96 | friend class IndentedScope; |
| 97 | void PrintIndented(const char* txt); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 98 | void PrintIndentedVisit(const char* s, AstNode* node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 99 | |
| 100 | void PrintStatements(ZoneList<Statement*>* statements); |
| 101 | void PrintDeclarations(ZoneList<Declaration*>* declarations); |
| 102 | void PrintParameters(Scope* scope); |
| 103 | void PrintArguments(ZoneList<Expression*>* arguments); |
| 104 | void PrintCaseClause(CaseClause* clause); |
| 105 | void PrintLiteralIndented(const char* info, Handle<Object> value, bool quote); |
| 106 | void PrintLiteralWithModeIndented(const char* info, |
| 107 | Variable* var, |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 108 | Handle<Object> value); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 109 | void PrintLabelsIndented(ZoneStringList* labels); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 110 | |
| 111 | void inc_indent() { indent_++; } |
| 112 | void dec_indent() { indent_--; } |
| 113 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 114 | int indent_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 115 | }; |
| 116 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 117 | #endif // DEBUG |
| 118 | |
| 119 | } } // namespace v8::internal |
| 120 | |
| 121 | #endif // V8_PRETTYPRINTER_H_ |