OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_EXPRESSION_CLASSIFIER_H | 5 #ifndef V8_EXPRESSION_CLASSIFIER_H |
6 #define V8_EXPRESSION_CLASSIFIER_H | 6 #define V8_EXPRESSION_CLASSIFIER_H |
7 | 7 |
8 #include "src/messages.h" | 8 #include "src/messages.h" |
9 #include "src/scanner.h" | 9 #include "src/scanner.h" |
10 #include "src/token.h" | 10 #include "src/token.h" |
(...skipping 18 matching lines...) Expand all Loading... |
29 enum TargetProduction { | 29 enum TargetProduction { |
30 ExpressionProduction = 1 << 0, | 30 ExpressionProduction = 1 << 0, |
31 FormalParameterInitializerProduction = 1 << 1, | 31 FormalParameterInitializerProduction = 1 << 1, |
32 BindingPatternProduction = 1 << 2, | 32 BindingPatternProduction = 1 << 2, |
33 AssignmentPatternProduction = 1 << 3, | 33 AssignmentPatternProduction = 1 << 3, |
34 DistinctFormalParametersProduction = 1 << 4, | 34 DistinctFormalParametersProduction = 1 << 4, |
35 StrictModeFormalParametersProduction = 1 << 5, | 35 StrictModeFormalParametersProduction = 1 << 5, |
36 StrongModeFormalParametersProduction = 1 << 6, | 36 StrongModeFormalParametersProduction = 1 << 6, |
37 ArrowFormalParametersProduction = 1 << 7, | 37 ArrowFormalParametersProduction = 1 << 7, |
38 LetPatternProduction = 1 << 8, | 38 LetPatternProduction = 1 << 8, |
| 39 CoverInitializedNameProduction = 1 << 9, |
39 | 40 |
40 ExpressionProductions = | 41 ExpressionProductions = |
41 (ExpressionProduction | FormalParameterInitializerProduction), | 42 (ExpressionProduction | FormalParameterInitializerProduction), |
42 PatternProductions = (BindingPatternProduction | | 43 PatternProductions = (BindingPatternProduction | |
43 AssignmentPatternProduction | LetPatternProduction), | 44 AssignmentPatternProduction | LetPatternProduction), |
44 FormalParametersProductions = (DistinctFormalParametersProduction | | 45 FormalParametersProductions = (DistinctFormalParametersProduction | |
45 StrictModeFormalParametersProduction | | 46 StrictModeFormalParametersProduction | |
46 StrongModeFormalParametersProduction), | 47 StrongModeFormalParametersProduction), |
47 StandardProductions = ExpressionProductions | PatternProductions, | 48 StandardProductions = ExpressionProductions | PatternProductions, |
48 AllProductions = (StandardProductions | FormalParametersProductions | | 49 AllProductions = |
49 ArrowFormalParametersProduction) | 50 (StandardProductions | FormalParametersProductions | |
| 51 ArrowFormalParametersProduction | CoverInitializedNameProduction) |
50 }; | 52 }; |
51 | 53 |
52 enum FunctionProperties { NonSimpleParameter = 1 << 0 }; | 54 enum FunctionProperties { NonSimpleParameter = 1 << 0 }; |
53 | 55 |
54 ExpressionClassifier() | 56 ExpressionClassifier() |
55 : invalid_productions_(0), | 57 : invalid_productions_(0), |
56 function_properties_(0), | 58 function_properties_(0), |
57 duplicate_finder_(nullptr) {} | 59 duplicate_finder_(nullptr) {} |
58 | 60 |
59 explicit ExpressionClassifier(DuplicateFinder* duplicate_finder) | 61 explicit ExpressionClassifier(DuplicateFinder* duplicate_finder) |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 const Error& strict_mode_formal_parameter_error() const { | 128 const Error& strict_mode_formal_parameter_error() const { |
127 return strict_mode_formal_parameter_error_; | 129 return strict_mode_formal_parameter_error_; |
128 } | 130 } |
129 | 131 |
130 const Error& strong_mode_formal_parameter_error() const { | 132 const Error& strong_mode_formal_parameter_error() const { |
131 return strong_mode_formal_parameter_error_; | 133 return strong_mode_formal_parameter_error_; |
132 } | 134 } |
133 | 135 |
134 const Error& let_pattern_error() const { return let_pattern_error_; } | 136 const Error& let_pattern_error() const { return let_pattern_error_; } |
135 | 137 |
| 138 bool has_cover_initialized_name() const { |
| 139 return !is_valid(CoverInitializedNameProduction); |
| 140 } |
| 141 const Error& cover_initialized_name_error() const { |
| 142 return cover_initialized_name_error_; |
| 143 } |
| 144 |
136 bool is_simple_parameter_list() const { | 145 bool is_simple_parameter_list() const { |
137 return !(function_properties_ & NonSimpleParameter); | 146 return !(function_properties_ & NonSimpleParameter); |
138 } | 147 } |
139 | 148 |
140 void RecordNonSimpleParameter() { | 149 void RecordNonSimpleParameter() { |
141 function_properties_ |= NonSimpleParameter; | 150 function_properties_ |= NonSimpleParameter; |
142 } | 151 } |
143 | 152 |
144 void RecordExpressionError(const Scanner::Location& loc, | 153 void RecordExpressionError(const Scanner::Location& loc, |
145 MessageTemplate::Template message, | 154 MessageTemplate::Template message, |
(...skipping 28 matching lines...) Expand all Loading... |
174 void RecordAssignmentPatternError(const Scanner::Location& loc, | 183 void RecordAssignmentPatternError(const Scanner::Location& loc, |
175 MessageTemplate::Template message, | 184 MessageTemplate::Template message, |
176 const char* arg = nullptr) { | 185 const char* arg = nullptr) { |
177 if (!is_valid_assignment_pattern()) return; | 186 if (!is_valid_assignment_pattern()) return; |
178 invalid_productions_ |= AssignmentPatternProduction; | 187 invalid_productions_ |= AssignmentPatternProduction; |
179 assignment_pattern_error_.location = loc; | 188 assignment_pattern_error_.location = loc; |
180 assignment_pattern_error_.message = message; | 189 assignment_pattern_error_.message = message; |
181 assignment_pattern_error_.arg = arg; | 190 assignment_pattern_error_.arg = arg; |
182 } | 191 } |
183 | 192 |
| 193 void RecordPatternError(const Scanner::Location& loc, |
| 194 MessageTemplate::Template message, |
| 195 const char* arg = nullptr) { |
| 196 RecordBindingPatternError(loc, message, arg); |
| 197 RecordAssignmentPatternError(loc, message, arg); |
| 198 } |
| 199 |
184 void RecordArrowFormalParametersError(const Scanner::Location& loc, | 200 void RecordArrowFormalParametersError(const Scanner::Location& loc, |
185 MessageTemplate::Template message, | 201 MessageTemplate::Template message, |
186 const char* arg = nullptr) { | 202 const char* arg = nullptr) { |
187 if (!is_valid_arrow_formal_parameters()) return; | 203 if (!is_valid_arrow_formal_parameters()) return; |
188 invalid_productions_ |= ArrowFormalParametersProduction; | 204 invalid_productions_ |= ArrowFormalParametersProduction; |
189 arrow_formal_parameters_error_.location = loc; | 205 arrow_formal_parameters_error_.location = loc; |
190 arrow_formal_parameters_error_.message = message; | 206 arrow_formal_parameters_error_.message = message; |
191 arrow_formal_parameters_error_.arg = arg; | 207 arrow_formal_parameters_error_.arg = arg; |
192 } | 208 } |
193 | 209 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 void RecordLetPatternError(const Scanner::Location& loc, | 241 void RecordLetPatternError(const Scanner::Location& loc, |
226 MessageTemplate::Template message, | 242 MessageTemplate::Template message, |
227 const char* arg = nullptr) { | 243 const char* arg = nullptr) { |
228 if (!is_valid_let_pattern()) return; | 244 if (!is_valid_let_pattern()) return; |
229 invalid_productions_ |= LetPatternProduction; | 245 invalid_productions_ |= LetPatternProduction; |
230 let_pattern_error_.location = loc; | 246 let_pattern_error_.location = loc; |
231 let_pattern_error_.message = message; | 247 let_pattern_error_.message = message; |
232 let_pattern_error_.arg = arg; | 248 let_pattern_error_.arg = arg; |
233 } | 249 } |
234 | 250 |
| 251 void RecordCoverInitializedNameError(const Scanner::Location& loc, |
| 252 MessageTemplate::Template message, |
| 253 const char* arg = nullptr) { |
| 254 if (has_cover_initialized_name()) return; |
| 255 invalid_productions_ |= CoverInitializedNameProduction; |
| 256 cover_initialized_name_error_.location = loc; |
| 257 cover_initialized_name_error_.message = message; |
| 258 cover_initialized_name_error_.arg = arg; |
| 259 } |
| 260 |
| 261 void ForgiveCoverInitializedNameError() { |
| 262 invalid_productions_ &= ~CoverInitializedNameProduction; |
| 263 cover_initialized_name_error_ = Error(); |
| 264 } |
| 265 |
| 266 void ForgiveAssignmentPatternError() { |
| 267 invalid_productions_ &= ~AssignmentPatternProduction; |
| 268 assignment_pattern_error_ = Error(); |
| 269 } |
| 270 |
235 void Accumulate(const ExpressionClassifier& inner, | 271 void Accumulate(const ExpressionClassifier& inner, |
236 unsigned productions = StandardProductions) { | 272 unsigned productions = StandardProductions) { |
237 // Propagate errors from inner, but don't overwrite already recorded | 273 // Propagate errors from inner, but don't overwrite already recorded |
238 // errors. | 274 // errors. |
239 unsigned non_arrow_inner_invalid_productions = | 275 unsigned non_arrow_inner_invalid_productions = |
240 inner.invalid_productions_ & ~ArrowFormalParametersProduction; | 276 inner.invalid_productions_ & ~ArrowFormalParametersProduction; |
241 if (non_arrow_inner_invalid_productions == 0) return; | 277 if (non_arrow_inner_invalid_productions == 0) return; |
242 unsigned non_arrow_productions = | 278 unsigned non_arrow_productions = |
243 productions & ~ArrowFormalParametersProduction; | 279 productions & ~ArrowFormalParametersProduction; |
244 unsigned errors = | 280 unsigned errors = |
(...skipping 14 matching lines...) Expand all Loading... |
259 duplicate_formal_parameter_error_ = | 295 duplicate_formal_parameter_error_ = |
260 inner.duplicate_formal_parameter_error_; | 296 inner.duplicate_formal_parameter_error_; |
261 if (errors & StrictModeFormalParametersProduction) | 297 if (errors & StrictModeFormalParametersProduction) |
262 strict_mode_formal_parameter_error_ = | 298 strict_mode_formal_parameter_error_ = |
263 inner.strict_mode_formal_parameter_error_; | 299 inner.strict_mode_formal_parameter_error_; |
264 if (errors & StrongModeFormalParametersProduction) | 300 if (errors & StrongModeFormalParametersProduction) |
265 strong_mode_formal_parameter_error_ = | 301 strong_mode_formal_parameter_error_ = |
266 inner.strong_mode_formal_parameter_error_; | 302 inner.strong_mode_formal_parameter_error_; |
267 if (errors & LetPatternProduction) | 303 if (errors & LetPatternProduction) |
268 let_pattern_error_ = inner.let_pattern_error_; | 304 let_pattern_error_ = inner.let_pattern_error_; |
| 305 if (errors & CoverInitializedNameProduction) |
| 306 cover_initialized_name_error_ = inner.cover_initialized_name_error_; |
269 } | 307 } |
270 | 308 |
271 // As an exception to the above, the result continues to be a valid arrow | 309 // As an exception to the above, the result continues to be a valid arrow |
272 // formal parameters if the inner expression is a valid binding pattern. | 310 // formal parameters if the inner expression is a valid binding pattern. |
273 if (productions & ArrowFormalParametersProduction && | 311 if (productions & ArrowFormalParametersProduction && |
274 is_valid_arrow_formal_parameters()) { | 312 is_valid_arrow_formal_parameters()) { |
275 // Also copy function properties if expecting an arrow function | 313 // Also copy function properties if expecting an arrow function |
276 // parameter. | 314 // parameter. |
277 function_properties_ |= inner.function_properties_; | 315 function_properties_ |= inner.function_properties_; |
278 | 316 |
279 if (!inner.is_valid_binding_pattern()) { | 317 if (!inner.is_valid_binding_pattern()) { |
280 invalid_productions_ |= ArrowFormalParametersProduction; | 318 invalid_productions_ |= ArrowFormalParametersProduction; |
281 arrow_formal_parameters_error_ = inner.binding_pattern_error_; | 319 arrow_formal_parameters_error_ = inner.binding_pattern_error_; |
282 } | 320 } |
283 } | 321 } |
284 } | 322 } |
285 | 323 |
286 private: | 324 private: |
287 unsigned invalid_productions_; | 325 unsigned invalid_productions_; |
288 unsigned function_properties_; | 326 unsigned function_properties_; |
289 Error expression_error_; | 327 Error expression_error_; |
290 Error formal_parameter_initializer_error_; | 328 Error formal_parameter_initializer_error_; |
291 Error binding_pattern_error_; | 329 Error binding_pattern_error_; |
292 Error assignment_pattern_error_; | 330 Error assignment_pattern_error_; |
293 Error arrow_formal_parameters_error_; | 331 Error arrow_formal_parameters_error_; |
294 Error duplicate_formal_parameter_error_; | 332 Error duplicate_formal_parameter_error_; |
295 Error strict_mode_formal_parameter_error_; | 333 Error strict_mode_formal_parameter_error_; |
296 Error strong_mode_formal_parameter_error_; | 334 Error strong_mode_formal_parameter_error_; |
297 Error let_pattern_error_; | 335 Error let_pattern_error_; |
| 336 Error cover_initialized_name_error_; |
298 DuplicateFinder* duplicate_finder_; | 337 DuplicateFinder* duplicate_finder_; |
299 }; | 338 }; |
300 | 339 |
301 } // namespace internal | 340 } // namespace internal |
302 } // namespace v8 | 341 } // namespace v8 |
303 | 342 |
304 #endif // V8_EXPRESSION_CLASSIFIER_H | 343 #endif // V8_EXPRESSION_CLASSIFIER_H |
OLD | NEW |