pending-compilation-error-handler.h 3.59 KB
Newer Older
1 2 3 4 5 6 7
// 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.

#ifndef V8_PENDING_COMPILATION_ERROR_HANDLER_H_
#define V8_PENDING_COMPILATION_ERROR_HANDLER_H_

8 9
#include <forward_list>

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

namespace v8 {
namespace internal {

class AstRawString;
19
class AstValueFactory;
20 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()
      : has_pending_error_(false),
29
        stack_overflow_(false),
30 31 32
        error_type_(kSyntaxError) {}

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

  void ReportMessageAt(int start_position, int end_position,
37
                       MessageTemplate message, const AstRawString* arg,
38
                       ParseErrorType error_type = kSyntaxError);
39

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

43 44 45 46 47 48 49
  bool stack_overflow() const { return stack_overflow_; }

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

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

53 54 55 56
  // Handle errors detected during parsing.
  void ReportErrors(Isolate* isolate, Handle<Script> script,
                    AstValueFactory* ast_value_factory);

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

  Handle<String> FormatErrorMessageForTest(Isolate* isolate) const;
61

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

74
 private:
75 76 77 78 79 80 81 82 83 84
  class MessageDetails {
   public:
    MOVE_ONLY_NO_DEFAULT_CONSTRUCTOR(MessageDetails);
    MessageDetails()
        : start_position_(-1),
          end_position_(-1),
          message_(MessageTemplate::kNone),
          arg_(nullptr),
          char_arg_(nullptr) {}
    MessageDetails(int start_position, int end_position,
85
                   MessageTemplate message, const AstRawString* arg,
86 87 88 89 90 91 92 93 94
                   const char* char_arg)
        : start_position_(start_position),
          end_position_(end_position),
          message_(message),
          arg_(arg),
          char_arg_(char_arg) {}

    Handle<String> ArgumentString(Isolate* isolate) const;
    MessageLocation GetLocation(Handle<Script> script) const;
95
    MessageTemplate message() const { return message_; }
96 97 98 99

   private:
    int start_position_;
    int end_position_;
100
    MessageTemplate message_;
101 102 103 104
    const AstRawString* arg_;
    const char* char_arg_;
  };

105
  void ThrowPendingError(Isolate* isolate, Handle<Script> script);
106

107
  bool has_pending_error_;
108
  bool stack_overflow_;
109
  bool unidentifiable_error_ = false;
110 111

  MessageDetails error_details_;
112 113
  ParseErrorType error_type_;

114 115
  std::forward_list<MessageDetails> warning_messages_;

116 117 118 119 120 121
  DISALLOW_COPY_AND_ASSIGN(PendingCompilationErrorHandler);
};

}  // namespace internal
}  // namespace v8
#endif  // V8_PENDING_COMPILATION_ERROR_HANDLER_H_