0d784991c2a3e9024bdb9876a58b7dbba5ed0e06
[platform/upstream/nodejs.git] / deps / v8 / src / preparse-data.h
1 // Copyright 2011 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_PREPARSE_DATA_H_
6 #define V8_PREPARSE_DATA_H_
7
8 #include "src/allocation.h"
9 #include "src/hashmap.h"
10 #include "src/preparse-data-format.h"
11 #include "src/utils-inl.h"
12
13 namespace v8 {
14 namespace internal {
15
16 class ScriptData;
17
18
19 // Abstract interface for preparse data recorder.
20 class ParserRecorder {
21  public:
22   ParserRecorder() { }
23   virtual ~ParserRecorder() { }
24
25   // Logs the scope and some details of a function literal in the source.
26   virtual void LogFunction(int start, int end, int literals, int properties,
27                            LanguageMode language_mode,
28                            bool uses_super_property) = 0;
29
30   // Logs an error message and marks the log as containing an error.
31   // Further logging will be ignored, and ExtractData will return a vector
32   // representing the error only.
33   virtual void LogMessage(int start,
34                           int end,
35                           const char* message,
36                           const char* argument_opt,
37                           bool is_reference_error) = 0;
38  private:
39   DISALLOW_COPY_AND_ASSIGN(ParserRecorder);
40 };
41
42
43 class SingletonLogger : public ParserRecorder {
44  public:
45   SingletonLogger()
46       : has_error_(false), start_(-1), end_(-1), is_reference_error_(false) {}
47   virtual ~SingletonLogger() {}
48
49   void Reset() { has_error_ = false; }
50
51   virtual void LogFunction(int start, int end, int literals, int properties,
52                            LanguageMode language_mode,
53                            bool scope_uses_super_property) {
54     DCHECK(!has_error_);
55     start_ = start;
56     end_ = end;
57     literals_ = literals;
58     properties_ = properties;
59     language_mode_ = language_mode;
60     scope_uses_super_property_ = scope_uses_super_property;
61   }
62
63   // Logs an error message and marks the log as containing an error.
64   // Further logging will be ignored, and ExtractData will return a vector
65   // representing the error only.
66   virtual void LogMessage(int start,
67                           int end,
68                           const char* message,
69                           const char* argument_opt,
70                           bool is_reference_error) {
71     if (has_error_) return;
72     has_error_ = true;
73     start_ = start;
74     end_ = end;
75     message_ = message;
76     argument_opt_ = argument_opt;
77     is_reference_error_ = is_reference_error;
78   }
79
80   bool has_error() const { return has_error_; }
81
82   int start() const { return start_; }
83   int end() const { return end_; }
84   int literals() const {
85     DCHECK(!has_error_);
86     return literals_;
87   }
88   int properties() const {
89     DCHECK(!has_error_);
90     return properties_;
91   }
92   LanguageMode language_mode() const {
93     DCHECK(!has_error_);
94     return language_mode_;
95   }
96   bool scope_uses_super_property() const {
97     DCHECK(!has_error_);
98     return scope_uses_super_property_;
99   }
100   int is_reference_error() const { return is_reference_error_; }
101   const char* message() {
102     DCHECK(has_error_);
103     return message_;
104   }
105   const char* argument_opt() const {
106     DCHECK(has_error_);
107     return argument_opt_;
108   }
109
110  private:
111   bool has_error_;
112   int start_;
113   int end_;
114   // For function entries.
115   int literals_;
116   int properties_;
117   LanguageMode language_mode_;
118   bool scope_uses_super_property_;
119   // For error messages.
120   const char* message_;
121   const char* argument_opt_;
122   bool is_reference_error_;
123 };
124
125
126 class CompleteParserRecorder : public ParserRecorder {
127  public:
128   struct Key {
129     bool is_one_byte;
130     Vector<const byte> literal_bytes;
131   };
132
133   CompleteParserRecorder();
134   virtual ~CompleteParserRecorder() {}
135
136   virtual void LogFunction(int start, int end, int literals, int properties,
137                            LanguageMode language_mode,
138                            bool scope_uses_super_property) {
139     function_store_.Add(start);
140     function_store_.Add(end);
141     function_store_.Add(literals);
142     function_store_.Add(properties);
143     function_store_.Add(language_mode);
144     function_store_.Add(scope_uses_super_property);
145   }
146
147   // Logs an error message and marks the log as containing an error.
148   // Further logging will be ignored, and ExtractData will return a vector
149   // representing the error only.
150   virtual void LogMessage(int start,
151                           int end,
152                           const char* message,
153                           const char* argument_opt,
154                           bool is_reference_error_);
155   ScriptData* GetScriptData();
156
157   bool HasError() {
158     return static_cast<bool>(preamble_[PreparseDataConstants::kHasErrorOffset]);
159   }
160   Vector<unsigned> ErrorMessageData() {
161     DCHECK(HasError());
162     return function_store_.ToVector();
163   }
164
165  private:
166   void WriteString(Vector<const char> str);
167
168   // Write a non-negative number to the symbol store.
169   void WriteNumber(int number);
170
171   Collector<unsigned> function_store_;
172   unsigned preamble_[PreparseDataConstants::kHeaderSize];
173
174 #ifdef DEBUG
175   int prev_start_;
176 #endif
177 };
178
179
180 } }  // namespace v8::internal.
181
182 #endif  // V8_PREPARSE_DATA_H_