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 class 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(char c); 56 void Print(const char* str); 57 void Print(Handle<String> str); 58 59 void Find(AstNode* node, bool print = false); 60 61 Isolate* isolate_; 62 int num_prints_; 63 // Allocate the builder on the heap simply because it's forward declared. 64 std::unique_ptr<IncrementalStringBuilder> builder_; 65 int position_; // position of ast node to print 66 bool found_; 67 bool done_; 68 bool is_user_js_; 69 bool is_iterator_error_; 70 bool is_async_iterator_error_; 71 bool is_call_error_; 72 SpreadErrorInArgsHint error_in_spread_args_; 73 ObjectLiteralProperty* destructuring_prop_; 74 Assignment* destructuring_assignment_; 75 Expression* spread_arg_; 76 FunctionKind function_kind_; 77 DEFINE_AST_VISITOR_SUBCLASS_MEMBERS(); 78 79 protected: 80 void PrintLiteral(Handle<Object> value, bool quote); 81 void PrintLiteral(const AstRawString* value, bool quote); 82 void FindStatements(const ZonePtrList<Statement>* statements); 83 void FindArguments(const ZonePtrList<Expression>* arguments); 84 }; 85 86 87 #ifdef DEBUG 88 89 class AstPrinter final : public AstVisitor<AstPrinter> { 90 public: 91 explicit AstPrinter(uintptr_t stack_limit); 92 ~AstPrinter(); 93 94 // The following routines print a node into a string. 95 // The result string is alive as long as the AstPrinter is alive. 96 const char* Print(AstNode* node); 97 const char* PrintProgram(FunctionLiteral* program); 98 99 void PRINTF_FORMAT(2, 3) Print(const char* format, ...); 100 101 // Print a node to stdout. 102 static void PrintOut(Isolate* isolate, AstNode* node); 103 104 // Individual nodes 105 #define DECLARE_VISIT(type) void Visit##type(type* node); 106 AST_NODE_LIST(DECLARE_VISIT) 107 #undef DECLARE_VISIT 108 109 private: 110 friend class IndentedScope; 111 112 void Init(); 113 114 void PrintLabels(ZonePtrList<const AstRawString>* labels); 115 void PrintLiteral(const AstRawString* value, bool quote); 116 void PrintLiteral(const AstConsString* value, bool quote); 117 void PrintLiteral(Literal* literal, bool quote); 118 void PrintIndented(const char* txt); 119 void PrintIndentedVisit(const char* s, AstNode* node); 120 121 void PrintStatements(const ZonePtrList<Statement>* statements); 122 void PrintDeclarations(Declaration::List* declarations); 123 void PrintParameters(DeclarationScope* scope); 124 void PrintArguments(const ZonePtrList<Expression>* arguments); 125 void PrintCaseClause(CaseClause* clause); 126 void PrintLiteralIndented(const char* info, Literal* literal, bool quote); 127 void PrintLiteralIndented(const char* info, const AstRawString* value, 128 bool quote); 129 void PrintLiteralIndented(const char* info, const AstConsString* value, 130 bool quote); 131 void PrintLiteralWithModeIndented(const char* info, Variable* var, 132 const AstRawString* value); 133 void PrintLabelsIndented(ZonePtrList<const AstRawString>* labels, 134 const char* prefix = ""); 135 void PrintObjectProperties( 136 const ZonePtrList<ObjectLiteral::Property>* properties); 137 void PrintClassProperty(ClassLiteral::Property* property); 138 void PrintClassProperties( 139 const ZonePtrList<ClassLiteral::Property>* properties); 140 void PrintClassStaticElements( 141 const ZonePtrList<ClassLiteral::StaticElement>* static_elements); 142 inc_indent()143 void inc_indent() { indent_++; } dec_indent()144 void dec_indent() { indent_--; } 145 146 DEFINE_AST_VISITOR_SUBCLASS_MEMBERS(); 147 148 char* output_; // output string buffer 149 int size_; // output_ size 150 int pos_; // current printing position 151 int indent_; 152 }; 153 154 #endif // DEBUG 155 156 } // namespace internal 157 } // namespace v8 158 159 #endif // V8_AST_PRETTYPRINTER_H_ 160