pending-compilation-error-handler.h 4.89 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 28
class Isolate;
class Script;

// Helper class for handling pending compilation errors consistently in various
// compilation phases.
class PendingCompilationErrorHandler {
 public:
  PendingCompilationErrorHandler()
29
      : has_pending_error_(false), stack_overflow_(false) {}
30

31 32 33 34 35
  PendingCompilationErrorHandler(const PendingCompilationErrorHandler&) =
      delete;
  PendingCompilationErrorHandler& operator=(
      const PendingCompilationErrorHandler&) = delete;

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

  void ReportMessageAt(int start_position, int end_position,
40
                       MessageTemplate message, const AstRawString* arg);
41

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

45 46 47 48 49 50 51
  bool stack_overflow() const { return stack_overflow_; }

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

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

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

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

67
  V8_EXPORT_PRIVATE Handle<String> FormatErrorMessageForTest(Isolate* isolate);
68

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

81
 private:
82 83 84 85 86 87 88
  class MessageDetails {
   public:
    MOVE_ONLY_NO_DEFAULT_CONSTRUCTOR(MessageDetails);
    MessageDetails()
        : start_position_(-1),
          end_position_(-1),
          message_(MessageTemplate::kNone),
89
          type_(kNone) {}
90
    MessageDetails(int start_position, int end_position,
91
                   MessageTemplate message, const AstRawString* arg)
92 93 94 95
        : start_position_(start_position),
          end_position_(end_position),
          message_(message),
          arg_(arg),
96 97 98 99 100 101 102 103
          type_(arg ? kAstRawString : kNone) {}
    MessageDetails(int start_position, int end_position,
                   MessageTemplate message, const char* char_arg)
        : start_position_(start_position),
          end_position_(end_position),
          message_(message),
          char_arg_(char_arg),
          type_(char_arg_ ? kConstCharString : kNone) {}
104 105 106

    Handle<String> ArgumentString(Isolate* isolate) const;
    MessageLocation GetLocation(Handle<Script> script) const;
107
    MessageTemplate message() const { return message_; }
108

109 110
    template <typename IsolateT>
    void Prepare(IsolateT* isolate);
111

112
   private:
113
    enum Type { kNone, kAstRawString, kConstCharString, kMainThreadHandle };
114

115
    void SetString(Handle<String> string, Isolate* isolate);
116
    void SetString(Handle<String> string, LocalIsolate* isolate);
117

118 119
    int start_position_;
    int end_position_;
120
    MessageTemplate message_;
121 122 123 124 125 126
    union {
      const AstRawString* arg_;
      const char* char_arg_;
      Handle<String> arg_handle_;
    };
    Type type_;
127 128
  };

129
  void ThrowPendingError(Isolate* isolate, Handle<Script> script) const;
130

131
  bool has_pending_error_;
132
  bool stack_overflow_;
133
  bool unidentifiable_error_ = false;
134 135

  MessageDetails error_details_;
136

137
  std::forward_list<MessageDetails> warning_messages_;
138 139
};

140 141 142
extern template void PendingCompilationErrorHandler::PrepareErrors(
    Isolate* isolate, AstValueFactory* ast_value_factory);
extern template void PendingCompilationErrorHandler::PrepareErrors(
143
    LocalIsolate* isolate, AstValueFactory* ast_value_factory);
144 145 146
extern template void PendingCompilationErrorHandler::PrepareWarnings(
    Isolate* isolate);
extern template void PendingCompilationErrorHandler::PrepareWarnings(
147
    LocalIsolate* isolate);
148

149 150
}  // namespace internal
}  // namespace v8
151
#endif  // V8_PARSING_PENDING_COMPILATION_ERROR_HANDLER_H_