Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / v8 / src / prettyprinter.h
1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef V8_PRETTYPRINTER_H_
6 #define V8_PRETTYPRINTER_H_
7
8 #include "src/allocation.h"
9 #include "src/ast.h"
10
11 namespace v8 {
12 namespace internal {
13
14 #ifdef DEBUG
15
16 class PrettyPrinter: public AstVisitor {
17  public:
18   explicit PrettyPrinter(Zone* zone);
19   virtual ~PrettyPrinter();
20
21   // The following routines print a node into a string.
22   // The result string is alive as long as the PrettyPrinter is alive.
23   const char* Print(AstNode* node);
24   const char* PrintExpression(FunctionLiteral* program);
25   const char* PrintProgram(FunctionLiteral* program);
26
27   void Print(const char* format, ...);
28
29   // Print a node to stdout.
30   static void PrintOut(Zone* zone, AstNode* node);
31
32   // Individual nodes
33 #define DECLARE_VISIT(type) virtual void Visit##type(type* node);
34   AST_NODE_LIST(DECLARE_VISIT)
35 #undef DECLARE_VISIT
36
37  private:
38   char* output_;  // output string buffer
39   int size_;  // output_ size
40   int pos_;  // current printing position
41
42  protected:
43   void Init();
44   const char* Output() const { return output_; }
45
46   virtual void PrintStatements(ZoneList<Statement*>* statements);
47   void PrintLabels(ZoneList<const AstRawString*>* labels);
48   virtual void PrintArguments(ZoneList<Expression*>* arguments);
49   void PrintLiteral(Handle<Object> value, bool quote);
50   void PrintLiteral(const AstRawString* value, bool quote);
51   void PrintParameters(Scope* scope);
52   void PrintDeclarations(ZoneList<Declaration*>* declarations);
53   void PrintFunctionLiteral(FunctionLiteral* function);
54   void PrintCaseClause(CaseClause* clause);
55
56   DEFINE_AST_VISITOR_SUBCLASS_MEMBERS();
57 };
58
59
60 // Prints the AST structure
61 class AstPrinter: public PrettyPrinter {
62  public:
63   explicit AstPrinter(Zone* zone);
64   virtual ~AstPrinter();
65
66   const char* PrintProgram(FunctionLiteral* program);
67
68   // Individual nodes
69 #define DECLARE_VISIT(type) virtual void Visit##type(type* node);
70   AST_NODE_LIST(DECLARE_VISIT)
71 #undef DECLARE_VISIT
72
73  private:
74   friend class IndentedScope;
75   void PrintIndented(const char* txt);
76   void PrintIndentedVisit(const char* s, AstNode* node);
77
78   void PrintStatements(ZoneList<Statement*>* statements);
79   void PrintDeclarations(ZoneList<Declaration*>* declarations);
80   void PrintParameters(Scope* scope);
81   void PrintArguments(ZoneList<Expression*>* arguments);
82   void PrintCaseClause(CaseClause* clause);
83   void PrintLiteralIndented(const char* info, Handle<Object> value, bool quote);
84   void PrintLiteralWithModeIndented(const char* info,
85                                     Variable* var,
86                                     Handle<Object> value);
87   void PrintLabelsIndented(ZoneList<const AstRawString*>* labels);
88
89   void inc_indent() { indent_++; }
90   void dec_indent() { indent_--; }
91
92   int indent_;
93 };
94
95 #endif  // DEBUG
96
97 } }  // namespace v8::internal
98
99 #endif  // V8_PRETTYPRINTER_H_