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