pending-compilation-error-handler.h 4.93 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/macros.h"
11
#include "src/common/globals.h"
12
#include "src/common/message-template.h"
13
#include "src/execution/off-thread-isolate.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

  void ReportMessageAt(int start_position, int end_position,
32
                       MessageTemplate message, const char* arg = nullptr);
33 34

  void ReportMessageAt(int start_position, int end_position,
35
                       MessageTemplate message, const AstRawString* arg);
36

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

40 41 42 43 44 45 46
  bool stack_overflow() const { return stack_overflow_; }

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

47
  bool has_pending_error() const { return has_pending_error_; }
48
  bool has_pending_warnings() const { return !warning_messages_.empty(); }
49

50
  // Handle errors detected during parsing.
51
  template <typename LocalIsolate>
52
  EXPORT_TEMPLATE_DECLARE(V8_EXPORT_PRIVATE)
53
  void PrepareErrors(LocalIsolate* isolate, AstValueFactory* ast_value_factory);
54 55
  V8_EXPORT_PRIVATE void ReportErrors(Isolate* isolate,
                                      Handle<Script> script) const;
56

57
  // Handle warnings detected during compilation.
58 59 60
  template <typename LocalIsolate>
  void PrepareWarnings(LocalIsolate* isolate);
  void ReportWarnings(Isolate* isolate, Handle<Script> script) const;
61

62
  V8_EXPORT_PRIVATE Handle<String> FormatErrorMessageForTest(Isolate* isolate);
63

64 65 66 67 68 69 70 71
  void set_unidentifiable_error() {
    has_pending_error_ = true;
    unidentifiable_error_ = true;
  }
  void clear_unidentifiable_error() {
    has_pending_error_ = false;
    unidentifiable_error_ = false;
  }
72 73 74
  bool has_error_unidentifiable_by_preparser() const {
    return unidentifiable_error_;
  }
75

76
 private:
77 78 79 80 81 82 83
  class MessageDetails {
   public:
    MOVE_ONLY_NO_DEFAULT_CONSTRUCTOR(MessageDetails);
    MessageDetails()
        : start_position_(-1),
          end_position_(-1),
          message_(MessageTemplate::kNone),
84
          type_(kNone) {}
85
    MessageDetails(int start_position, int end_position,
86
                   MessageTemplate message, const AstRawString* arg)
87 88 89 90
        : start_position_(start_position),
          end_position_(end_position),
          message_(message),
          arg_(arg),
91 92 93 94 95 96 97 98
          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) {}
99 100 101

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

104 105
    template <typename LocalIsolate>
    void Prepare(LocalIsolate* isolate);
106

107
   private:
108 109 110 111
    enum Type {
      kNone,
      kAstRawString,
      kConstCharString,
112 113
      kMainThreadHandle,
      kOffThreadTransferHandle
114 115
    };

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

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

131
  void ThrowPendingError(Isolate* isolate, Handle<Script> script) const;
132

133
  bool has_pending_error_;
134
  bool stack_overflow_;
135
  bool unidentifiable_error_ = false;
136 137

  MessageDetails error_details_;
138

139 140
  std::forward_list<MessageDetails> warning_messages_;

141 142 143
  DISALLOW_COPY_AND_ASSIGN(PendingCompilationErrorHandler);
};

144 145 146 147 148 149 150 151 152
extern template void PendingCompilationErrorHandler::PrepareErrors(
    Isolate* isolate, AstValueFactory* ast_value_factory);
extern template void PendingCompilationErrorHandler::PrepareErrors(
    OffThreadIsolate* isolate, AstValueFactory* ast_value_factory);
extern template void PendingCompilationErrorHandler::PrepareWarnings(
    Isolate* isolate);
extern template void PendingCompilationErrorHandler::PrepareWarnings(
    OffThreadIsolate* isolate);

153 154
}  // namespace internal
}  // namespace v8
155
#endif  // V8_PARSING_PENDING_COMPILATION_ERROR_HANDLER_H_