blob: dfff49a45a1629ae4b3ac84a206c0c7b241985de [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
kasperl@chromium.org71affb52009-05-26 05:44:31 +000033namespace v8 {
34namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000035
36#ifdef DEBUG
37
ager@chromium.orga74f0da2008-12-03 16:05:52 +000038class PrettyPrinter: public AstVisitor {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000039 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.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +000045 const char* Print(AstNode* node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000046 const char* PrintExpression(FunctionLiteral* program);
47 const char* PrintProgram(FunctionLiteral* program);
48
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +000049 void Print(const char* format, ...);
50
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000051 // Print a node to stdout.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +000052 static void PrintOut(AstNode* node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000053
54 // Individual nodes
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +000055#define DECLARE_VISIT(type) virtual void Visit##type(type* node);
56 AST_NODE_LIST(DECLARE_VISIT)
57#undef DECLARE_VISIT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000058
59 private:
60 char* output_; // output string buffer
61 int size_; // output_ size
62 int pos_; // current printing position
63
64 protected:
65 void Init();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000066 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
80class AstPrinter: public PrettyPrinter {
81 public:
82 AstPrinter();
83 virtual ~AstPrinter();
84
85 const char* PrintProgram(FunctionLiteral* program);
86
87 // Individual nodes
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +000088#define DECLARE_VISIT(type) virtual void Visit##type(type* node);
89 AST_NODE_LIST(DECLARE_VISIT)
90#undef DECLARE_VISIT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000091 private:
92 friend class IndentedScope;
93 void PrintIndented(const char* txt);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +000094 void PrintIndentedVisit(const char* s, AstNode* node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000095
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,
ricow@chromium.org65fae842010-08-25 15:26:24 +0000105 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
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000114
115// Forward declaration of helper classes.
116class TagScope;
117class AttributesScope;
118
119// Build a C string containing a JSON representation of a function's
120// AST. The representation is based on JsonML (www.jsonml.org).
121class JsonAstBuilder: public PrettyPrinter {
122 public:
123 JsonAstBuilder()
124 : indent_(0), top_tag_scope_(NULL), attributes_scope_(NULL) {
125 }
126 virtual ~JsonAstBuilder() {}
127
128 // Controls the indentation of subsequent lines of a tag body after
129 // the first line.
130 static const int kTagIndentSize = 2;
131
132 // Controls the indentation of subsequent lines of an attributes
133 // blocks's body after the first line.
134 static const int kAttributesIndentSize = 1;
135
136 // Construct a JSON representation of a function literal.
137 const char* BuildProgram(FunctionLiteral* program);
138
139 // Print text indented by the current indentation level.
140 void PrintIndented(const char* text) { Print("%*s%s", indent_, "", text); }
141
142 // Change the indentation level.
143 void increase_indent(int amount) { indent_ += amount; }
144 void decrease_indent(int amount) { indent_ -= amount; }
145
146 // The builder maintains a stack of opened AST node constructors.
147 // Each node constructor corresponds to a JsonML tag.
148 TagScope* tag() { return top_tag_scope_; }
149 void set_tag(TagScope* scope) { top_tag_scope_ = scope; }
150
151 // The builder maintains a pointer to the currently opened attributes
152 // of current AST node or NULL if the attributes are not opened.
153 AttributesScope* attributes() { return attributes_scope_; }
154 void set_attributes(AttributesScope* scope) { attributes_scope_ = scope; }
155
156 // Add an attribute to the currently opened attributes.
157 void AddAttribute(const char* name, Handle<String> value);
158 void AddAttribute(const char* name, const char* value);
159 void AddAttribute(const char* name, int value);
160 void AddAttribute(const char* name, bool value);
161
162 // AST node visit functions.
163#define DECLARE_VISIT(type) virtual void Visit##type(type* node);
164 AST_NODE_LIST(DECLARE_VISIT)
165#undef DECLARE_VISIT
166
167 private:
168 int indent_;
169 TagScope* top_tag_scope_;
170 AttributesScope* attributes_scope_;
171
172 // Utility function used by AddAttribute implementations.
173 void AddAttributePrefix(const char* name);
174};
175
176
177// The JSON AST builder keeps a stack of open element tags (AST node
178// constructors from the current iteration point to the root of the
179// AST). TagScope is a helper class to manage the opening and closing
180// of tags, the indentation of their bodies, and comma separating their
181// contents.
182class TagScope BASE_EMBEDDED {
183 public:
184 TagScope(JsonAstBuilder* builder, const char* name);
185 ~TagScope();
186
187 void use() { has_body_ = true; }
188
189 private:
190 JsonAstBuilder* builder_;
191 TagScope* next_;
192 bool has_body_;
193};
194
195
196// AttributesScope is a helper class to manage the opening and closing
197// of attribute blocks, the indentation of their bodies, and comma
198// separating their contents. JsonAstBuilder::AddAttribute adds an
199// attribute to the currently open AttributesScope. They cannot be
200// nested so the builder keeps an optional single scope rather than a
201// stack.
202class AttributesScope BASE_EMBEDDED {
203 public:
204 explicit AttributesScope(JsonAstBuilder* builder);
205 ~AttributesScope();
206
207 bool is_used() { return attribute_count_ > 0; }
208 void use() { ++attribute_count_; }
209
210 private:
211 JsonAstBuilder* builder_;
212 int attribute_count_;
213};
214
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000215#endif // DEBUG
216
217} } // namespace v8::internal
218
219#endif // V8_PRETTYPRINTER_H_