Home | History | Annotate | Download | only in ast
      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_AST_PRETTYPRINTER_H_
      6 #define V8_AST_PRETTYPRINTER_H_
      7 
      8 #include "src/allocation.h"
      9 #include "src/ast/ast.h"
     10 
     11 namespace v8 {
     12 namespace internal {
     13 
     14 class CallPrinter : public AstVisitor {
     15  public:
     16   explicit CallPrinter(Isolate* isolate, bool is_builtin);
     17   virtual ~CallPrinter();
     18 
     19   // The following routine prints the node with position |position| into a
     20   // string. The result string is alive as long as the CallPrinter is alive.
     21   const char* Print(FunctionLiteral* program, int position);
     22 
     23   void Print(const char* format, ...);
     24 
     25   void Find(AstNode* node, bool print = false);
     26 
     27 // Individual nodes
     28 #define DECLARE_VISIT(type) void Visit##type(type* node) override;
     29   AST_NODE_LIST(DECLARE_VISIT)
     30 #undef DECLARE_VISIT
     31 
     32  private:
     33   void Init();
     34   char* output_;  // output string buffer
     35   int size_;      // output_ size
     36   int pos_;       // current printing position
     37   int position_;  // position of ast node to print
     38   bool found_;
     39   bool done_;
     40   bool is_builtin_;
     41 
     42   DEFINE_AST_VISITOR_SUBCLASS_MEMBERS();
     43 
     44  protected:
     45   void PrintLiteral(Object* value, bool quote);
     46   void PrintLiteral(const AstRawString* value, bool quote);
     47   void FindStatements(ZoneList<Statement*>* statements);
     48   void FindArguments(ZoneList<Expression*>* arguments);
     49 };
     50 
     51 
     52 #ifdef DEBUG
     53 
     54 class PrettyPrinter: public AstVisitor {
     55  public:
     56   explicit PrettyPrinter(Isolate* isolate);
     57   virtual ~PrettyPrinter();
     58 
     59   // The following routines print a node into a string.
     60   // The result string is alive as long as the PrettyPrinter is alive.
     61   const char* Print(AstNode* node);
     62   const char* PrintExpression(FunctionLiteral* program);
     63   const char* PrintProgram(FunctionLiteral* program);
     64 
     65   void Print(const char* format, ...);
     66 
     67   // Print a node to stdout.
     68   static void PrintOut(Isolate* isolate, AstNode* node);
     69 
     70   // Individual nodes
     71 #define DECLARE_VISIT(type) void Visit##type(type* node) override;
     72   AST_NODE_LIST(DECLARE_VISIT)
     73 #undef DECLARE_VISIT
     74 
     75  private:
     76   char* output_;  // output string buffer
     77   int size_;  // output_ size
     78   int pos_;  // current printing position
     79 
     80  protected:
     81   void Init();
     82   const char* Output() const { return output_; }
     83 
     84   virtual void PrintStatements(ZoneList<Statement*>* statements);
     85   void PrintLabels(ZoneList<const AstRawString*>* labels);
     86   virtual void PrintArguments(ZoneList<Expression*>* arguments);
     87   void PrintLiteral(Handle<Object> value, bool quote);
     88   void PrintLiteral(const AstRawString* value, bool quote);
     89   void PrintParameters(Scope* scope);
     90   void PrintDeclarations(ZoneList<Declaration*>* declarations);
     91   void PrintFunctionLiteral(FunctionLiteral* function);
     92   void PrintCaseClause(CaseClause* clause);
     93   void PrintObjectLiteralProperty(ObjectLiteralProperty* property);
     94 
     95   DEFINE_AST_VISITOR_SUBCLASS_MEMBERS();
     96 };
     97 
     98 
     99 // Prints the AST structure
    100 class AstPrinter: public PrettyPrinter {
    101  public:
    102   explicit AstPrinter(Isolate* isolate);
    103   virtual ~AstPrinter();
    104 
    105   const char* PrintProgram(FunctionLiteral* program);
    106 
    107   // Individual nodes
    108 #define DECLARE_VISIT(type) virtual void Visit##type(type* node);
    109   AST_NODE_LIST(DECLARE_VISIT)
    110 #undef DECLARE_VISIT
    111 
    112  private:
    113   friend class IndentedScope;
    114   void PrintIndented(const char* txt);
    115   void PrintIndentedVisit(const char* s, AstNode* node);
    116 
    117   void PrintStatements(ZoneList<Statement*>* statements);
    118   void PrintDeclarations(ZoneList<Declaration*>* declarations);
    119   void PrintParameters(Scope* scope);
    120   void PrintArguments(ZoneList<Expression*>* arguments);
    121   void PrintCaseClause(CaseClause* clause);
    122   void PrintLiteralIndented(const char* info, Handle<Object> value, bool quote);
    123   void PrintLiteralWithModeIndented(const char* info,
    124                                     Variable* var,
    125                                     Handle<Object> value);
    126   void PrintLabelsIndented(ZoneList<const AstRawString*>* labels);
    127   void PrintProperties(ZoneList<ObjectLiteral::Property*>* properties);
    128 
    129   void inc_indent() { indent_++; }
    130   void dec_indent() { indent_--; }
    131 
    132   int indent_;
    133 };
    134 
    135 #endif  // DEBUG
    136 
    137 }  // namespace internal
    138 }  // namespace v8
    139 
    140 #endif  // V8_AST_PRETTYPRINTER_H_
    141