178d1ad443658709d6c27809001a0e71efd8b898fyangguo@chromium.org// Copyright 2012 the V8 project authors. All rights reserved.
243d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen// Redistribution and use in source and binary forms, with or without
343d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen// modification, are permitted provided that the following conditions are
443d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen// met:
543d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen//
643d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen//     * Redistributions of source code must retain the above copyright
743d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen//       notice, this list of conditions and the following disclaimer.
843d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen//     * Redistributions in binary form must reproduce the above
943d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen//       copyright notice, this list of conditions and the following
1043d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen//       disclaimer in the documentation and/or other materials provided
1143d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen//       with the distribution.
1243d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen//     * Neither the name of Google Inc. nor the names of its
1343d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen//       contributors may be used to endorse or promote products derived
1443d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen//       from this software without specific prior written permission.
1543d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen//
1643d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1743d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1843d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1943d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2043d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2143d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2243d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2343d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2443d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2543d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2643d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2743d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen
2843d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen#ifndef V8_PRETTYPRINTER_H_
2943d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen#define V8_PRETTYPRINTER_H_
3043d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen
311c09276ce2ac5214e81ca554360b9f101187893blrn@chromium.org#include "allocation.h"
3243d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen#include "ast.h"
3343d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen
3471affb54842da76b24f0bb3184e9f0960523f89dkasperl@chromium.orgnamespace v8 {
3571affb54842da76b24f0bb3184e9f0960523f89dkasperl@chromium.orgnamespace internal {
3643d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen
3743d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen#ifdef DEBUG
3843d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen
39a74f0daeb278665869b4b6a3bc2739e88fed93b1ager@chromium.orgclass PrettyPrinter: public AstVisitor {
4043d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen public:
418fa5bd929d2f128e3d554398bd085b3c2f98e23bjkummerow@chromium.org  explicit PrettyPrinter(Isolate* isolate);
4243d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen  virtual ~PrettyPrinter();
4343d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen
4443d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen  // The following routines print a node into a string.
4543d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen  // The result string is alive as long as the PrettyPrinter is alive.
460b6db5975a9d1ebcf3de7b18603380d99f789e66sgjesse@chromium.org  const char* Print(AstNode* node);
4743d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen  const char* PrintExpression(FunctionLiteral* program);
4843d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen  const char* PrintProgram(FunctionLiteral* program);
4943d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen
509d58c2b1c27d8b2890b9bd46e57d3842b09e0292christian.plesner.hansen@gmail.com  void Print(const char* format, ...);
519d58c2b1c27d8b2890b9bd46e57d3842b09e0292christian.plesner.hansen@gmail.com
5243d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen  // Print a node to stdout.
538fa5bd929d2f128e3d554398bd085b3c2f98e23bjkummerow@chromium.org  static void PrintOut(Isolate* isolate, AstNode* node);
5443d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen
5543d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen  // Individual nodes
569d58c2b1c27d8b2890b9bd46e57d3842b09e0292christian.plesner.hansen@gmail.com#define DECLARE_VISIT(type) virtual void Visit##type(type* node);
579d58c2b1c27d8b2890b9bd46e57d3842b09e0292christian.plesner.hansen@gmail.com  AST_NODE_LIST(DECLARE_VISIT)
589d58c2b1c27d8b2890b9bd46e57d3842b09e0292christian.plesner.hansen@gmail.com#undef DECLARE_VISIT
5943d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen
6043d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen private:
6143d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen  char* output_;  // output string buffer
6243d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen  int size_;  // output_ size
6343d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen  int pos_;  // current printing position
6443d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen
6543d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen protected:
6643d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen  void Init();
6743d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen  const char* Output() const { return output_; }
6843d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen
6943d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen  virtual void PrintStatements(ZoneList<Statement*>* statements);
7043d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen  void PrintLabels(ZoneStringList* labels);
7143d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen  virtual void PrintArguments(ZoneList<Expression*>* arguments);
7243d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen  void PrintLiteral(Handle<Object> value, bool quote);
7343d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen  void PrintParameters(Scope* scope);
7443d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen  void PrintDeclarations(ZoneList<Declaration*>* declarations);
7543d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen  void PrintFunctionLiteral(FunctionLiteral* function);
7643d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen  void PrintCaseClause(CaseClause* clause);
77a6bbcc801f63c451f814d6da77a1a48fba3d36c6yangguo@chromium.org
78a6bbcc801f63c451f814d6da77a1a48fba3d36c6yangguo@chromium.org  DEFINE_AST_VISITOR_SUBCLASS_MEMBERS();
7943d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen};
8043d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen
8143d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen
8243d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen// Prints the AST structure
8343d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansenclass AstPrinter: public PrettyPrinter {
8443d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen public:
858fa5bd929d2f128e3d554398bd085b3c2f98e23bjkummerow@chromium.org  explicit AstPrinter(Isolate* isolate);
8643d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen  virtual ~AstPrinter();
8743d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen
8843d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen  const char* PrintProgram(FunctionLiteral* program);
8943d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen
9043d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen  // Individual nodes
919d58c2b1c27d8b2890b9bd46e57d3842b09e0292christian.plesner.hansen@gmail.com#define DECLARE_VISIT(type) virtual void Visit##type(type* node);
929d58c2b1c27d8b2890b9bd46e57d3842b09e0292christian.plesner.hansen@gmail.com  AST_NODE_LIST(DECLARE_VISIT)
939d58c2b1c27d8b2890b9bd46e57d3842b09e0292christian.plesner.hansen@gmail.com#undef DECLARE_VISIT
94a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org
9543d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen private:
9643d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen  friend class IndentedScope;
9743d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen  void PrintIndented(const char* txt);
980b6db5975a9d1ebcf3de7b18603380d99f789e66sgjesse@chromium.org  void PrintIndentedVisit(const char* s, AstNode* node);
9943d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen
10043d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen  void PrintStatements(ZoneList<Statement*>* statements);
10143d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen  void PrintDeclarations(ZoneList<Declaration*>* declarations);
10243d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen  void PrintParameters(Scope* scope);
10343d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen  void PrintArguments(ZoneList<Expression*>* arguments);
10443d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen  void PrintCaseClause(CaseClause* clause);
10543d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen  void PrintLiteralIndented(const char* info, Handle<Object> value, bool quote);
10643d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen  void PrintLiteralWithModeIndented(const char* info,
10743d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen                                    Variable* var,
10844bc7080a85cc25bff3b9b77cd53f7beffab711bkarlklose@chromium.org                                    Handle<Object> value);
10957ff881caeb2e15b46ac9e4dfc00e378f7c5f929ulan@chromium.org  void PrintLabelsIndented(ZoneStringList* labels);
11043d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen
11143d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen  void inc_indent() { indent_++; }
11243d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen  void dec_indent() { indent_--; }
11343d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen
114ea88ce93dcb41a9200ec8747ae7642a5db1f4ce7sgjesse@chromium.org  int indent_;
11543d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen};
11643d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen
11743d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen#endif  // DEBUG
11843d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen
11943d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen} }  // namespace v8::internal
12043d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen
12143d26ecc3563a46f62a0224030667c8f8f3f6cebchristian.plesner.hansen#endif  // V8_PRETTYPRINTER_H_
122