1// Copyright 2015 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_PENDING_COMPILATION_ERROR_HANDLER_H_
6#define V8_PENDING_COMPILATION_ERROR_HANDLER_H_
7
8#include <forward_list>
9
10#include "src/base/macros.h"
11#include "src/globals.h"
12#include "src/handles.h"
13#include "src/message-template.h"
14
15namespace v8 {
16namespace internal {
17
18class AstRawString;
19class AstValueFactory;
20class Isolate;
21class Script;
22
23// Helper class for handling pending compilation errors consistently in various
24// compilation phases.
25class PendingCompilationErrorHandler {
26 public:
27 PendingCompilationErrorHandler()
28 : has_pending_error_(false),
29 stack_overflow_(false),
30 error_type_(kSyntaxError) {}
31
32 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 bool stack_overflow() const { return stack_overflow_; }
44
45 void set_stack_overflow() {
46 has_pending_error_ = true;
47 stack_overflow_ = true;
48 }
49
50 bool has_pending_error() const { return has_pending_error_; }
51 bool has_pending_warnings() const { return !warning_messages_.empty(); }
52
53 // Handle errors detected during parsing.
54 void ReportErrors(Isolate* isolate, Handle<Script> script,
55 AstValueFactory* ast_value_factory);
56
57 // Handle warnings detected during compilation.
58 void ReportWarnings(Isolate* isolate, Handle<Script> script);
59
60 V8_EXPORT_PRIVATE Handle<String> FormatErrorMessageForTest(
61 Isolate* isolate) const;
62
63 void set_unidentifiable_error() {
64 has_pending_error_ = true;
65 unidentifiable_error_ = true;
66 }
67 void clear_unidentifiable_error() {
68 has_pending_error_ = false;
69 unidentifiable_error_ = false;
70 }
71 bool has_error_unidentifiable_by_preparser() const {
72 return unidentifiable_error_;
73 }
74
75 private:
76 class MessageDetails {
77 public:
78 MOVE_ONLY_NO_DEFAULT_CONSTRUCTOR(MessageDetails);
79 MessageDetails()
80 : start_position_(-1),
81 end_position_(-1),
82 message_(MessageTemplate::kNone),
83 arg_(nullptr),
84 char_arg_(nullptr) {}
85 MessageDetails(int start_position, int end_position,
86 MessageTemplate message, const AstRawString* arg,
87 const char* char_arg)
88 : start_position_(start_position),
89 end_position_(end_position),
90 message_(message),
91 arg_(arg),
92 char_arg_(char_arg) {}
93
94 Handle<String> ArgumentString(Isolate* isolate) const;
95 MessageLocation GetLocation(Handle<Script> script) const;
96 MessageTemplate message() const { return message_; }
97
98 private:
99 int start_position_;
100 int end_position_;
101 MessageTemplate message_;
102 const AstRawString* arg_;
103 const char* char_arg_;
104 };
105
106 void ThrowPendingError(Isolate* isolate, Handle<Script> script);
107
108 bool has_pending_error_;
109 bool stack_overflow_;
110 bool unidentifiable_error_ = false;
111
112 MessageDetails error_details_;
113 ParseErrorType error_type_;
114
115 std::forward_list<MessageDetails> warning_messages_;
116
117 DISALLOW_COPY_AND_ASSIGN(PendingCompilationErrorHandler);
118};
119
120} // namespace internal
121} // namespace v8
122#endif // V8_PENDING_COMPILATION_ERROR_HANDLER_H_
123