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_PARSING_PENDING_COMPILATION_ERROR_HANDLER_H_
6 #define V8_PARSING_PENDING_COMPILATION_ERROR_HANDLER_H_
7 
8 #include <forward_list>
9 
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 namespace v8 {
17 namespace internal {
18 
19 class AstRawString;
20 class AstValueFactory;
21 class Isolate;
22 class Script;
23 
24 // Helper class for handling pending compilation errors consistently in various
25 // compilation phases.
26 class PendingCompilationErrorHandler {
27  public:
PendingCompilationErrorHandler()28   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 
stack_overflow()40   bool stack_overflow() const { return stack_overflow_; }
41 
set_stack_overflow()42   void set_stack_overflow() {
43     has_pending_error_ = true;
44     stack_overflow_ = true;
45   }
46 
has_pending_error()47   bool has_pending_error() const { return has_pending_error_; }
has_pending_warnings()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   V8_EXPORT_PRIVATE void ReportErrors(Isolate* isolate,
55                                       Handle<Script> script) const;
56 
57   // Handle warnings detected during compilation.
58   template <typename LocalIsolate>
59   void PrepareWarnings(LocalIsolate* isolate);
60   void ReportWarnings(Isolate* isolate, Handle<Script> script) const;
61 
62   V8_EXPORT_PRIVATE Handle<String> FormatErrorMessageForTest(Isolate* isolate);
63 
set_unidentifiable_error()64   void set_unidentifiable_error() {
65     has_pending_error_ = true;
66     unidentifiable_error_ = true;
67   }
clear_unidentifiable_error()68   void clear_unidentifiable_error() {
69     has_pending_error_ = false;
70     unidentifiable_error_ = false;
71   }
has_error_unidentifiable_by_preparser()72   bool has_error_unidentifiable_by_preparser() const {
73     return unidentifiable_error_;
74   }
75 
76  private:
77   class MessageDetails {
78    public:
79     MOVE_ONLY_NO_DEFAULT_CONSTRUCTOR(MessageDetails);
MessageDetails()80     MessageDetails()
81         : start_position_(-1),
82           end_position_(-1),
83           message_(MessageTemplate::kNone),
84           type_(kNone) {}
MessageDetails(int start_position,int end_position,MessageTemplate message,const AstRawString * arg)85     MessageDetails(int start_position, int end_position,
86                    MessageTemplate message, const AstRawString* arg)
87         : start_position_(start_position),
88           end_position_(end_position),
89           message_(message),
90           arg_(arg),
91           type_(arg ? kAstRawString : kNone) {}
MessageDetails(int start_position,int end_position,MessageTemplate message,const char * char_arg)92     MessageDetails(int start_position, int end_position,
93                    MessageTemplate message, const char* char_arg)
94         : start_position_(start_position),
95           end_position_(end_position),
96           message_(message),
97           char_arg_(char_arg),
98           type_(char_arg_ ? kConstCharString : kNone) {}
99 
100     Handle<String> ArgumentString(Isolate* isolate) const;
101     MessageLocation GetLocation(Handle<Script> script) const;
message()102     MessageTemplate message() const { return message_; }
103 
104     template <typename LocalIsolate>
105     void Prepare(LocalIsolate* isolate);
106 
107    private:
108     enum Type { kNone, kAstRawString, kConstCharString, kMainThreadHandle };
109 
110     void SetString(Handle<String> string, Isolate* isolate);
111     void SetString(Handle<String> string, LocalIsolate* isolate);
112 
113     int start_position_;
114     int end_position_;
115     MessageTemplate message_;
116     union {
117       const AstRawString* arg_;
118       const char* char_arg_;
119       Handle<String> arg_handle_;
120     };
121     Type type_;
122   };
123 
124   void ThrowPendingError(Isolate* isolate, Handle<Script> script) const;
125 
126   bool has_pending_error_;
127   bool stack_overflow_;
128   bool unidentifiable_error_ = false;
129 
130   MessageDetails error_details_;
131 
132   std::forward_list<MessageDetails> warning_messages_;
133 
134   DISALLOW_COPY_AND_ASSIGN(PendingCompilationErrorHandler);
135 };
136 
137 extern template void PendingCompilationErrorHandler::PrepareErrors(
138     Isolate* isolate, AstValueFactory* ast_value_factory);
139 extern template void PendingCompilationErrorHandler::PrepareErrors(
140     LocalIsolate* isolate, AstValueFactory* ast_value_factory);
141 extern template void PendingCompilationErrorHandler::PrepareWarnings(
142     Isolate* isolate);
143 extern template void PendingCompilationErrorHandler::PrepareWarnings(
144     LocalIsolate* isolate);
145 
146 }  // namespace internal
147 }  // namespace v8
148 #endif  // V8_PARSING_PENDING_COMPILATION_ERROR_HANDLER_H_
149