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_.location = Scanner::Location::invalid(); |
| 264 } |
| 265 |
235 void Accumulate(const ExpressionClassifier& inner, | 266 void Accumulate(const ExpressionClassifier& inner, |
236 unsigned productions = StandardProductions) { | 267 unsigned productions = StandardProductions) { |
237 // Propagate errors from inner, but don't overwrite already recorded | 268 // Propagate errors from inner, but don't overwrite already recorded |
238 // errors. | 269 // errors. |
239 unsigned non_arrow_inner_invalid_productions = | 270 unsigned non_arrow_inner_invalid_productions = |
240 inner.invalid_productions_ & ~ArrowFormalParametersProduction; | 271 inner.invalid_productions_ & ~ArrowFormalParametersProduction; |
241 if (non_arrow_inner_invalid_productions == 0) return; | 272 if (non_arrow_inner_invalid_productions == 0) return; |
242 unsigned non_arrow_productions = | 273 unsigned non_arrow_productions = |
243 productions & ~ArrowFormalParametersProduction; | 274 productions & ~ArrowFormalParametersProduction; |
244 unsigned errors = | 275 unsigned errors = |
(...skipping 14 matching lines...) Expand all Loading... |
259 duplicate_formal_parameter_error_ = | 290 duplicate_formal_parameter_error_ = |
260 inner.duplicate_formal_parameter_error_; | 291 inner.duplicate_formal_parameter_error_; |
261 if (errors & StrictModeFormalParametersProduction) | 292 if (errors & StrictModeFormalParametersProduction) |
262 strict_mode_formal_parameter_error_ = | 293 strict_mode_formal_parameter_error_ = |
263 inner.strict_mode_formal_parameter_error_; | 294 inner.strict_mode_formal_parameter_error_; |
264 if (errors & StrongModeFormalParametersProduction) | 295 if (errors & StrongModeFormalParametersProduction) |
265 strong_mode_formal_parameter_error_ = | 296 strong_mode_formal_parameter_error_ = |
266 inner.strong_mode_formal_parameter_error_; | 297 inner.strong_mode_formal_parameter_error_; |
267 if (errors & LetPatternProduction) | 298 if (errors & LetPatternProduction) |
268 let_pattern_error_ = inner.let_pattern_error_; | 299 let_pattern_error_ = inner.let_pattern_error_; |
| 300 if (errors & CoverInitializedNameProduction) |
| 301 cover_initialized_name_error_ = inner.cover_initialized_name_error_; |
269 } | 302 } |
270 | 303 |
271 // As an exception to the above, the result continues to be a valid arrow | 304 // 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. | 305 // formal parameters if the inner expression is a valid binding pattern. |
273 if (productions & ArrowFormalParametersProduction && | 306 if (productions & ArrowFormalParametersProduction && |
274 is_valid_arrow_formal_parameters()) { | 307 is_valid_arrow_formal_parameters()) { |
275 // Also copy function properties if expecting an arrow function | 308 // Also copy function properties if expecting an arrow function |
276 // parameter. | 309 // parameter. |
277 function_properties_ |= inner.function_properties_; | 310 function_properties_ |= inner.function_properties_; |
278 | 311 |
279 if (!inner.is_valid_binding_pattern()) { | 312 if (!inner.is_valid_binding_pattern()) { |
280 invalid_productions_ |= ArrowFormalParametersProduction; | 313 invalid_productions_ |= ArrowFormalParametersProduction; |
281 arrow_formal_parameters_error_ = inner.binding_pattern_error_; | 314 arrow_formal_parameters_error_ = inner.binding_pattern_error_; |
282 } | 315 } |
283 } | 316 } |
284 } | 317 } |
285 | 318 |
| 319 static const Error& FirdRecorded(const Error& a, const Error& b) { |
| 320 // Determine message which occurred earlier in source. Used by |
| 321 // ParserBase::ValidateExpression() |
| 322 DCHECK(a.location.IsValid() || b.location.IsValid()); |
| 323 if (a.location.beg_pos < 0) return b; |
| 324 if (b.location.beg_pos < 0 || b.location.beg_pos >= a.location.beg_pos) { |
| 325 return a; |
| 326 } |
| 327 return b; |
| 328 } |
| 329 |
286 private: | 330 private: |
287 unsigned invalid_productions_; | 331 unsigned invalid_productions_; |
288 unsigned function_properties_; | 332 unsigned function_properties_; |
289 Error expression_error_; | 333 Error expression_error_; |
290 Error formal_parameter_initializer_error_; | 334 Error formal_parameter_initializer_error_; |
291 Error binding_pattern_error_; | 335 Error binding_pattern_error_; |
292 Error assignment_pattern_error_; | 336 Error assignment_pattern_error_; |
293 Error arrow_formal_parameters_error_; | 337 Error arrow_formal_parameters_error_; |
294 Error duplicate_formal_parameter_error_; | 338 Error duplicate_formal_parameter_error_; |
295 Error strict_mode_formal_parameter_error_; | 339 Error strict_mode_formal_parameter_error_; |
296 Error strong_mode_formal_parameter_error_; | 340 Error strong_mode_formal_parameter_error_; |
297 Error let_pattern_error_; | 341 Error let_pattern_error_; |
| 342 Error cover_initialized_name_error_; |
298 DuplicateFinder* duplicate_finder_; | 343 DuplicateFinder* duplicate_finder_; |
299 }; | 344 }; |
300 | 345 |
301 } // namespace internal | 346 } // namespace internal |
302 } // namespace v8 | 347 } // namespace v8 |
303 | 348 |
304 #endif // V8_EXPRESSION_CLASSIFIER_H | 349 #endif // V8_EXPRESSION_CLASSIFIER_H |
OLD | NEW |