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 <memory> 9 10 #include "src/ast/ast.h" 11 #include "src/base/compiler-specific.h" 12 #include "src/objects/function-kind.h" 13 #include "src/utils/allocation.h" 14 15 namespace v8 { 16 namespace internal { 17 18 class IncrementalStringBuilder; // to avoid including string-builder-inl.h 19 20 class CallPrinter final : public AstVisitor<CallPrinter> { 21 public: 22 enum class SpreadErrorInArgsHint { kErrorInArgs, kNoErrorInArgs }; 23 24 explicit CallPrinter(Isolate* isolate, bool is_user_js, 25 SpreadErrorInArgsHint error_in_spread_args = 26 SpreadErrorInArgsHint::kNoErrorInArgs); 27 ~CallPrinter(); 28 29 // The following routine prints the node with position |position| into a 30 // string. 31 Handle<String> Print(FunctionLiteral* program, int position); 32 enum ErrorHint { 33 kNone, 34 kNormalIterator, 35 kAsyncIterator, 36 kCallAndNormalIterator, 37 kCallAndAsyncIterator 38 }; 39 40 ErrorHint GetErrorHint() const; spread_arg()41 Expression* spread_arg() const { return spread_arg_; } destructuring_prop()42 ObjectLiteralProperty* destructuring_prop() const { 43 return destructuring_prop_; 44 } destructuring_assignment()45 Assignment* destructuring_assignment() const { 46 return destructuring_assignment_; 47 } 48 49 // Individual nodes 50 #define DECLARE_VISIT(type) void Visit##type(type* node); 51 AST_NODE_LIST(DECLARE_VISIT) 52 #undef DECLARE_VISIT 53 54 private: 55 void Print(const char* str); 56 void Print(Handle<String> str); 57 58 void Find(AstNode* node, bool print = false); 59 60 Isolate* isolate_; 61 int num_prints_; 62 // Allocate the builder on the heap simply because it's forward declared. 63 std::unique_ptr<IncrementalStringBuilder> builder_; 64 int position_; // position of ast node to print 65 bool found_; 66 bool done_; 67 bool is_user_js_; 68 bool is_iterator_error_; 69 bool is_async_iterator_error_; 70 bool is_call_error_; 71 SpreadErrorInArgsHint error_in_spread_args_; 72 ObjectLiteralProperty* destructuring_prop_; 73 Assignment* destructuring_assignment_; 74 Expression* spread_arg_; 75 FunctionKind function_kind_; 76 DEFINE_AST_VISITOR_SUBCLASS_MEMBERS(); 77 78 protected: 79 void PrintLiteral(Handle<Object> value, bool quote); 80 void PrintLiteral(const AstRawString* value, bool quote); 81 void FindStatements(const ZonePtrList<Statement>* statements); 82 void FindArguments(const ZonePtrList<Expression>* arguments); 83 }; 84 85 86 #ifdef DEBUG 87 88 class AstPrinter final : public AstVisitor<AstPrinter> { 89 public: 90 explicit AstPrinter(uintptr_t stack_limit); 91 ~AstPrinter(); 92 93 // The following routines print a node into a string. 94 // The result string is alive as long as the AstPrinter is alive. 95 const char* Print(AstNode* node); 96 const char* PrintProgram(FunctionLiteral* program); 97 98 void PRINTF_FORMAT(2, 3) Print(const char* format, ...); 99 100 // Print a node to stdout. 101 static void PrintOut(Isolate* isolate, AstNode* node); 102 103 // Individual nodes 104 #define DECLARE_VISIT(type) void Visit##type(type* node); 105 AST_NODE_LIST(DECLARE_VISIT) 106 #undef DECLARE_VISIT 107 108 private: 109 friend class IndentedScope; 110 111 void Init(); 112 113 void PrintLabels(ZonePtrList<const AstRawString>* labels); 114 void PrintLiteral(const AstRawString* value, bool quote); 115 void PrintLiteral(const AstConsString* value, bool quote); 116 void PrintLiteral(Literal* literal, bool quote); 117 void PrintIndented(const char* txt); 118 void PrintIndentedVisit(const char* s, AstNode* node); 119 120 void PrintStatements(const ZonePtrList<Statement>* statements); 121 void PrintDeclarations(Declaration::List* declarations); 122 void PrintParameters(DeclarationScope* scope); 123 void PrintArguments(const ZonePtrList<Expression>* arguments); 124 void PrintCaseClause(CaseClause* clause); 125 void PrintLiteralIndented(const char* info, Literal* literal, bool quote); 126 void PrintLiteralIndented(const char* info, const AstRawString* value, 127 bool quote); 128 void PrintLiteralIndented(const char* info, const AstConsString* value, 129 bool quote); 130 void PrintLiteralWithModeIndented(const char* info, Variable* var, 131 const AstRawString* value); 132 void PrintLabelsIndented(ZonePtrList<const AstRawString>* labels, 133 const char* prefix = ""); 134 void PrintObjectProperties( 135 const ZonePtrList<ObjectLiteral::Property>* properties); 136 void PrintClassProperties( 137 const ZonePtrList<ClassLiteral::Property>* properties); 138 inc_indent()139 void inc_indent() { indent_++; } dec_indent()140 void dec_indent() { indent_--; } 141 142 DEFINE_AST_VISITOR_SUBCLASS_MEMBERS(); 143 144 char* output_; // output string buffer 145 int size_; // output_ size 146 int pos_; // current printing position 147 int indent_; 148 }; 149 150 #endif // DEBUG 151 152 } // namespace internal 153 } // namespace v8 154 155 #endif // V8_AST_PRETTYPRINTER_H_ 156