• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/messages.h"
14 
15 namespace v8 {
16 namespace internal {
17 
18 class AstRawString;
19 class AstValueFactory;
20 class Isolate;
21 class Script;
22 
23 // Helper class for handling pending compilation errors consistently in various
24 // compilation phases.
25 class PendingCompilationErrorHandler {
26  public:
PendingCompilationErrorHandler()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::Template message,
34                        const char* arg = nullptr,
35                        ParseErrorType error_type = kSyntaxError);
36 
37   void ReportMessageAt(int start_position, int end_position,
38                        MessageTemplate::Template message,
39                        const AstRawString* arg,
40                        ParseErrorType error_type = kSyntaxError);
41 
42   void ReportWarningAt(int start_position, int end_position,
43                        MessageTemplate::Template message,
44                        const char* arg = nullptr);
45 
stack_overflow()46   bool stack_overflow() const { return stack_overflow_; }
47 
set_stack_overflow()48   void set_stack_overflow() {
49     has_pending_error_ = true;
50     stack_overflow_ = true;
51   }
52 
has_pending_error()53   bool has_pending_error() const { return has_pending_error_; }
has_pending_warnings()54   bool has_pending_warnings() const { return !warning_messages_.empty(); }
55 
56   // Handle errors detected during parsing.
57   void ReportErrors(Isolate* isolate, Handle<Script> script,
58                     AstValueFactory* ast_value_factory);
59 
60   // Handle warnings detected during compilation.
61   void ReportWarnings(Isolate* isolate, Handle<Script> script);
62 
63   Handle<String> FormatErrorMessageForTest(Isolate* isolate) const;
64 
65  private:
66   class MessageDetails {
67    public:
68     MOVE_ONLY_NO_DEFAULT_CONSTRUCTOR(MessageDetails);
MessageDetails()69     MessageDetails()
70         : start_position_(-1),
71           end_position_(-1),
72           message_(MessageTemplate::kNone),
73           arg_(nullptr),
74           char_arg_(nullptr) {}
MessageDetails(int start_position,int end_position,MessageTemplate::Template message,const AstRawString * arg,const char * char_arg)75     MessageDetails(int start_position, int end_position,
76                    MessageTemplate::Template message, const AstRawString* arg,
77                    const char* char_arg)
78         : start_position_(start_position),
79           end_position_(end_position),
80           message_(message),
81           arg_(arg),
82           char_arg_(char_arg) {}
83 
84     Handle<String> ArgumentString(Isolate* isolate) const;
85     MessageLocation GetLocation(Handle<Script> script) const;
message()86     MessageTemplate::Template message() const { return message_; }
87 
88    private:
89     int start_position_;
90     int end_position_;
91     MessageTemplate::Template message_;
92     const AstRawString* arg_;
93     const char* char_arg_;
94   };
95 
96   void ThrowPendingError(Isolate* isolate, Handle<Script> script);
97 
98   bool has_pending_error_;
99   bool stack_overflow_;
100 
101   MessageDetails error_details_;
102   ParseErrorType error_type_;
103 
104   std::forward_list<MessageDetails> warning_messages_;
105 
106   DISALLOW_COPY_AND_ASSIGN(PendingCompilationErrorHandler);
107 };
108 
109 }  // namespace internal
110 }  // namespace v8
111 #endif  // V8_PENDING_COMPILATION_ERROR_HANDLER_H_
112