1 // Copyright 2006-2008 the V8 project authors. All rights reserved. 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 31 #include "ast.h" 32 33 namespace v8 { 34 namespace internal { 35 36 #ifdef DEBUG 37 38 class PrettyPrinter: public AstVisitor { 39 public: 40 PrettyPrinter(); 41 virtual ~PrettyPrinter(); 42 43 // The following routines print a node into a string. 44 // The result string is alive as long as the PrettyPrinter is alive. 45 const char* Print(AstNode* node); 46 const char* PrintExpression(FunctionLiteral* program); 47 const char* PrintProgram(FunctionLiteral* program); 48 49 // Print a node to stdout. 50 static void PrintOut(AstNode* node); 51 52 // Individual nodes 53 #define DEF_VISIT(type) \ 54 virtual void Visit##type(type* node); 55 AST_NODE_LIST(DEF_VISIT) 56 #undef DEF_VISIT 57 58 private: 59 char* output_; // output string buffer 60 int size_; // output_ size 61 int pos_; // current printing position 62 63 protected: 64 void Init(); 65 void Print(const char* format, ...); Output()66 const char* Output() const { return output_; } 67 68 virtual void PrintStatements(ZoneList<Statement*>* statements); 69 void PrintLabels(ZoneStringList* labels); 70 virtual void PrintArguments(ZoneList<Expression*>* arguments); 71 void PrintLiteral(Handle<Object> value, bool quote); 72 void PrintParameters(Scope* scope); 73 void PrintDeclarations(ZoneList<Declaration*>* declarations); 74 void PrintFunctionLiteral(FunctionLiteral* function); 75 void PrintCaseClause(CaseClause* clause); 76 }; 77 78 79 // Prints the AST structure 80 class AstPrinter: public PrettyPrinter { 81 public: 82 AstPrinter(); 83 virtual ~AstPrinter(); 84 85 const char* PrintProgram(FunctionLiteral* program); 86 87 // Individual nodes 88 #define DEF_VISIT(type) \ 89 virtual void Visit##type(type* node); 90 AST_NODE_LIST(DEF_VISIT) 91 #undef DEF_VISIT 92 private: 93 friend class IndentedScope; 94 void PrintIndented(const char* txt); 95 void PrintIndentedVisit(const char* s, AstNode* node); 96 97 void PrintStatements(ZoneList<Statement*>* statements); 98 void PrintDeclarations(ZoneList<Declaration*>* declarations); 99 void PrintParameters(Scope* scope); 100 void PrintArguments(ZoneList<Expression*>* arguments); 101 void PrintCaseClause(CaseClause* clause); 102 void PrintLiteralIndented(const char* info, Handle<Object> value, bool quote); 103 void PrintLiteralWithModeIndented(const char* info, 104 Variable* var, 105 Handle<Object> value, 106 SmiAnalysis* type); 107 void PrintLabelsIndented(const char* info, ZoneStringList* labels); 108 inc_indent()109 void inc_indent() { indent_++; } dec_indent()110 void dec_indent() { indent_--; } 111 112 static int indent_; 113 }; 114 115 #endif // DEBUG 116 117 } } // namespace v8::internal 118 119 #endif // V8_PRETTYPRINTER_H_ 120