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