pending-compilation-error-handler.h 6.17 KB
Newer Older
1 2 3 4
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
#ifndef V8_PARSING_PENDING_COMPILATION_ERROR_HANDLER_H_
#define V8_PARSING_PENDING_COMPILATION_ERROR_HANDLER_H_
7

8 9
#include <forward_list>

10
#include "src/base/export-template.h"
11
#include "src/base/macros.h"
12
#include "src/common/globals.h"
13
#include "src/common/message-template.h"
14
#include "src/handles/handles.h"
15 16 17 18 19

namespace v8 {
namespace internal {

class AstRawString;
20
class AstValueFactory;
21 22 23 24 25 26 27
class Isolate;
class Script;

// Helper class for handling pending compilation errors consistently in various
// compilation phases.
class PendingCompilationErrorHandler {
 public:
28
  PendingCompilationErrorHandler() = default;
29 30 31 32 33
  PendingCompilationErrorHandler(const PendingCompilationErrorHandler&) =
      delete;
  PendingCompilationErrorHandler& operator=(
      const PendingCompilationErrorHandler&) = delete;

34
  void ReportMessageAt(int start_position, int end_position,
35
                       MessageTemplate message, const char* arg = nullptr);
36 37

  void ReportMessageAt(int start_position, int end_position,
38
                       MessageTemplate message, const AstRawString* arg);
39

40 41 42 43
  void ReportMessageAt(int start_position, int end_position,
                       MessageTemplate message, const AstRawString* arg0,
                       const char* arg1);

44
  void ReportWarningAt(int start_position, int end_position,
45
                       MessageTemplate message, const char* arg = nullptr);
46

47 48 49 50 51 52 53
  bool stack_overflow() const { return stack_overflow_; }

  void set_stack_overflow() {
    has_pending_error_ = true;
    stack_overflow_ = true;
  }

54
  bool has_pending_error() const { return has_pending_error_; }
55
  bool has_pending_warnings() const { return !warning_messages_.empty(); }
56

57
  // Handle errors detected during parsing.
58
  template <typename IsolateT>
59
  EXPORT_TEMPLATE_DECLARE(V8_EXPORT_PRIVATE)
60
  void PrepareErrors(IsolateT* isolate, AstValueFactory* ast_value_factory);
61 62
  V8_EXPORT_PRIVATE void ReportErrors(Isolate* isolate,
                                      Handle<Script> script) const;
63

64
  // Handle warnings detected during compilation.
65 66
  template <typename IsolateT>
  void PrepareWarnings(IsolateT* isolate);
67
  void ReportWarnings(Isolate* isolate, Handle<Script> script) const;
68

69
  V8_EXPORT_PRIVATE Handle<String> FormatErrorMessageForTest(Isolate* isolate);
70

71 72 73 74 75 76 77 78
  void set_unidentifiable_error() {
    has_pending_error_ = true;
    unidentifiable_error_ = true;
  }
  void clear_unidentifiable_error() {
    has_pending_error_ = false;
    unidentifiable_error_ = false;
  }
79 80 81
  bool has_error_unidentifiable_by_preparser() const {
    return unidentifiable_error_;
  }
82

83
 private:
84 85 86 87 88 89
  class MessageDetails {
   public:
    MOVE_ONLY_NO_DEFAULT_CONSTRUCTOR(MessageDetails);
    MessageDetails()
        : start_position_(-1),
          end_position_(-1),
90
          message_(MessageTemplate::kNone) {}
91
    MessageDetails(int start_position, int end_position,
92
                   MessageTemplate message, const AstRawString* arg0)
93 94 95
        : start_position_(start_position),
          end_position_(end_position),
          message_(message),
96
          args_{MessageArgument{arg0}, MessageArgument{}} {}
97 98 99 100 101 102
    MessageDetails(int start_position, int end_position,
                   MessageTemplate message, const AstRawString* arg0,
                   const char* arg1)
        : start_position_(start_position),
          end_position_(end_position),
          message_(message),
103
          args_{MessageArgument{arg0}, MessageArgument{arg1}} {
104 105 106
      DCHECK_NOT_NULL(arg0);
      DCHECK_NOT_NULL(arg1);
    }
107
    MessageDetails(int start_position, int end_position,
108
                   MessageTemplate message, const char* arg0)
109 110 111
        : start_position_(start_position),
          end_position_(end_position),
          message_(message),
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
          args_{MessageArgument{arg0}, MessageArgument{}} {}

    Handle<String> ArgString(Isolate* isolate, int index) const;
    int ArgCount() const {
      int argc = 0;
      for (int i = 0; i < kMaxArgumentCount; i++) {
        if (args_[i].type == kNone) break;
        argc++;
      }
#ifdef DEBUG
      for (int i = argc; i < kMaxArgumentCount; i++) {
        DCHECK_EQ(args_[i].type, kNone);
      }
#endif  // DEBUG
      return argc;
    }
128 129

    MessageLocation GetLocation(Handle<Script> script) const;
130
    MessageTemplate message() const { return message_; }
131

132 133
    template <typename IsolateT>
    void Prepare(IsolateT* isolate);
134

135
   private:
136
    enum Type { kNone, kAstRawString, kConstCharString, kMainThreadHandle };
137

138
    void SetString(Handle<String> string, Isolate* isolate);
139
    void SetString(Handle<String> string, LocalIsolate* isolate);
140

141 142
    int start_position_;
    int end_position_;
143

144
    MessageTemplate message_;
145 146 147 148 149 150 151 152 153 154 155 156 157 158

    struct MessageArgument final {
      constexpr MessageArgument() : ast_string(nullptr), type(kNone) {}
      explicit constexpr MessageArgument(const AstRawString* s)
          : ast_string(s), type(s == nullptr ? kNone : kAstRawString) {}
      explicit constexpr MessageArgument(const char* s)
          : c_string(s), type(s == nullptr ? kNone : kConstCharString) {}

      union {
        const AstRawString* ast_string;
        const char* c_string;
        Handle<String> js_string;
      };
      Type type;
159
    };
160 161 162

    static constexpr int kMaxArgumentCount = 2;
    MessageArgument args_[kMaxArgumentCount];
163 164
  };

165
  void ThrowPendingError(Isolate* isolate, Handle<Script> script) const;
166

167 168
  bool has_pending_error_ = false;
  bool stack_overflow_ = false;
169
  bool unidentifiable_error_ = false;
170 171

  MessageDetails error_details_;
172

173
  std::forward_list<MessageDetails> warning_messages_;
174 175
};

176 177 178
extern template void PendingCompilationErrorHandler::PrepareErrors(
    Isolate* isolate, AstValueFactory* ast_value_factory);
extern template void PendingCompilationErrorHandler::PrepareErrors(
179
    LocalIsolate* isolate, AstValueFactory* ast_value_factory);
180 181 182
extern template void PendingCompilationErrorHandler::PrepareWarnings(
    Isolate* isolate);
extern template void PendingCompilationErrorHandler::PrepareWarnings(
183
    LocalIsolate* isolate);
184

185 186
}  // namespace internal
}  // namespace v8
187
#endif  // V8_PARSING_PENDING_COMPILATION_ERROR_HANDLER_H_