Chromium Code Reviews| 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/v8.h" | 8 #include "src/v8.h" |
| 9 | 9 |
| 10 #include "src/messages.h" | 10 #include "src/messages.h" |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 41 (BindingPatternProduction | AssignmentPatternProduction), | 41 (BindingPatternProduction | AssignmentPatternProduction), |
| 42 FormalParametersProductions = (DistinctFormalParametersProduction | | 42 FormalParametersProductions = (DistinctFormalParametersProduction | |
| 43 StrictModeFormalParametersProduction | | 43 StrictModeFormalParametersProduction | |
| 44 StrongModeFormalParametersProduction), | 44 StrongModeFormalParametersProduction), |
| 45 StandardProductions = ExpressionProduction | PatternProductions, | 45 StandardProductions = ExpressionProduction | PatternProductions, |
| 46 AllProductions = (StandardProductions | FormalParametersProductions | | 46 AllProductions = (StandardProductions | FormalParametersProductions | |
| 47 ArrowFormalParametersProduction) | 47 ArrowFormalParametersProduction) |
| 48 }; | 48 }; |
| 49 | 49 |
| 50 ExpressionClassifier() | 50 ExpressionClassifier() |
| 51 : invalid_productions_(0), duplicate_finder_(nullptr) {} | 51 : lhs_type_(TARGET_NONE), |
| 52 assigned_(false), | |
| 53 invalid_productions_(0), | |
| 54 duplicate_finder_(nullptr) {} | |
| 52 | 55 |
| 53 explicit ExpressionClassifier(DuplicateFinder* duplicate_finder) | 56 explicit ExpressionClassifier(DuplicateFinder* duplicate_finder) |
| 54 : invalid_productions_(0), duplicate_finder_(duplicate_finder) {} | 57 : lhs_type_(TARGET_NONE), |
| 58 assigned_(false), | |
| 59 invalid_productions_(0), | |
| 60 duplicate_finder_(duplicate_finder) {} | |
| 61 | |
| 62 enum AssignmentTargetType { | |
| 63 TARGET_NONE = 0, | |
| 64 | |
| 65 // IdentifierReference | |
| 66 TARGET_IDENTIFIER, | |
| 67 | |
| 68 // MemberExpression . <property name> | |
| 69 // MemberExpression [<property name>] | |
| 70 TARGET_PROPERTY, | |
| 71 | |
| 72 // MemberExpression ( arguments ) | |
| 73 // new MemberExpression ( arguments ) | |
| 74 // MemberExpression TemplateLiteral | |
| 75 TARGET_CALL, | |
| 76 | |
| 77 // this | |
| 78 // Literal | |
| 79 // ArrayLiteral | |
| 80 // ObjectLiteral | |
| 81 // FunctionExpression | |
| 82 // ClassExpression | |
| 83 // GeneratorExpression | |
| 84 // RegularExpressionLiteral | |
| 85 // TemplateLiteral | |
| 86 // CoverParenthesizedExpressionAndArrowParameterList | |
| 87 TARGET_PRIMARY, | |
| 88 | |
| 89 // new.target | |
| 90 // yield (in generator) | |
| 91 TARGET_RESTRICTED, | |
|
noordhuis
2015/06/15 22:01:09
TARGET_RESTRICTED doesn't seem to be used anywhere
caitp (gmail)
2015/06/19 14:23:20
Done.
| |
| 92 | |
| 93 // UnaryExpression | |
| 94 // MultiplicativeExpression | |
| 95 // AdditiveExpression | |
| 96 // ShiftExpression | |
| 97 // RelationalExpression | |
| 98 // EqualityExpression | |
| 99 // BitwiseANDExpression | |
| 100 // BitwiseXORExpression | |
| 101 // BitwiseORExpression | |
| 102 // LogicalANDExpression | |
| 103 // LogicalORExpression | |
| 104 // ConditionalExpression | |
| 105 // AssignmentExpression | |
| 106 // ... Pretty much any binary operator | |
| 107 UNACCEPTABLE_TARGET, | |
| 108 | |
| 109 // BindingPattern | |
| 110 TARGET_PATTERN | |
| 111 }; | |
| 112 | |
| 113 inline AssignmentTargetType AssignmentTarget() const { return lhs_type_; } | |
| 114 | |
| 115 inline void AppendAssignmentTarget(AssignmentTargetType type) { | |
| 116 if (lhs_type_ == UNACCEPTABLE_TARGET) return; | |
| 117 lhs_type_ = type; | |
| 118 } | |
| 119 | |
| 120 inline void ReportInvalidSimpleAssignmentTarget() { | |
| 121 lhs_type_ = UNACCEPTABLE_TARGET; | |
| 122 } | |
| 123 | |
| 124 inline bool IsValidSimpleAssignmentTarget() const { | |
| 125 // Only identifiers and properties are valid targets. | |
| 126 return lhs_type_ >= TARGET_IDENTIFIER && lhs_type_ <= TARGET_PROPERTY; | |
|
noordhuis
2015/06/15 22:01:09
This code is perhaps a little easier to understand
caitp (gmail)
2015/06/19 14:23:20
Done.
| |
| 127 } | |
| 128 | |
| 129 inline bool IsPatternAssignmentElement() const { | |
| 130 // Any AssignmentElement for which IsValidSimpleAssignmentTarget is false. | |
| 131 // Used for nested AssignmentPatterns | |
| 132 return lhs_type_ == TARGET_PATTERN && is_valid_assignment_pattern(); | |
| 133 } | |
| 134 | |
| 135 inline bool IsValidPattern() const { return lhs_type_ == TARGET_PATTERN; } | |
| 136 | |
| 137 inline bool IsAssigned() const { return assigned_; } | |
| 138 | |
| 139 void set_assigned() { assigned_ = true; } | |
| 140 | |
| 141 bool is_destructuring_assignment() const { | |
| 142 return lhs_type_ == TARGET_PATTERN && assigned_; | |
|
noordhuis
2015/06/15 22:01:09
I think this can be written as `return IsValidPatt
caitp (gmail)
2015/06/19 14:23:20
Done.
| |
| 143 } | |
| 144 | |
| 55 | 145 |
| 56 bool is_valid(unsigned productions) const { | 146 bool is_valid(unsigned productions) const { |
| 57 return (invalid_productions_ & productions) == 0; | 147 return (invalid_productions_ & productions) == 0; |
| 58 } | 148 } |
| 59 | 149 |
| 60 DuplicateFinder* duplicate_finder() const { return duplicate_finder_; } | 150 DuplicateFinder* duplicate_finder() const { return duplicate_finder_; } |
| 61 | 151 |
| 62 bool is_valid_expression() const { return is_valid(ExpressionProduction); } | 152 bool is_valid_expression() const { return is_valid(ExpressionProduction); } |
| 63 | 153 |
| 64 bool is_valid_binding_pattern() const { | 154 bool is_valid_binding_pattern() const { |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 178 void RecordStrongModeFormalParameterError(const Scanner::Location& loc, | 268 void RecordStrongModeFormalParameterError(const Scanner::Location& loc, |
| 179 MessageTemplate::Template message, | 269 MessageTemplate::Template message, |
| 180 const char* arg = nullptr) { | 270 const char* arg = nullptr) { |
| 181 if (!is_valid_strong_mode_formal_parameters()) return; | 271 if (!is_valid_strong_mode_formal_parameters()) return; |
| 182 invalid_productions_ |= StrongModeFormalParametersProduction; | 272 invalid_productions_ |= StrongModeFormalParametersProduction; |
| 183 strong_mode_formal_parameter_error_.location = loc; | 273 strong_mode_formal_parameter_error_.location = loc; |
| 184 strong_mode_formal_parameter_error_.message = message; | 274 strong_mode_formal_parameter_error_.message = message; |
| 185 strong_mode_formal_parameter_error_.arg = arg; | 275 strong_mode_formal_parameter_error_.arg = arg; |
| 186 } | 276 } |
| 187 | 277 |
| 278 void AccumulateValidSimpleAssignmentTarget( | |
| 279 const ExpressionClassifier& inner) { | |
| 280 if (lhs_type_ != UNACCEPTABLE_TARGET) { | |
| 281 lhs_type_ = inner.lhs_type_; | |
| 282 } | |
| 283 } | |
| 284 | |
| 285 | |
| 188 void Accumulate(const ExpressionClassifier& inner, | 286 void Accumulate(const ExpressionClassifier& inner, |
| 189 unsigned productions = StandardProductions) { | 287 unsigned productions = StandardProductions) { |
| 190 // Propagate errors from inner, but don't overwrite already recorded | 288 // Propagate errors from inner, but don't overwrite already recorded |
| 191 // errors. | 289 // errors. |
| 192 unsigned non_arrow_inner_invalid_productions = | 290 unsigned non_arrow_inner_invalid_productions = |
| 193 inner.invalid_productions_ & ~ArrowFormalParametersProduction; | 291 inner.invalid_productions_ & ~ArrowFormalParametersProduction; |
| 194 if (non_arrow_inner_invalid_productions == 0) return; | 292 if (non_arrow_inner_invalid_productions == 0) return; |
| 195 unsigned non_arrow_productions = | 293 unsigned non_arrow_productions = |
| 196 productions & ~ArrowFormalParametersProduction; | 294 productions & ~ArrowFormalParametersProduction; |
| 197 unsigned errors = | 295 unsigned errors = |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 232 if (is_valid_binding_pattern()) { | 330 if (is_valid_binding_pattern()) { |
| 233 binding_pattern_error_ = inner.expression_error(); | 331 binding_pattern_error_ = inner.expression_error(); |
| 234 } | 332 } |
| 235 if (is_valid_assignment_pattern()) { | 333 if (is_valid_assignment_pattern()) { |
| 236 assignment_pattern_error_ = inner.expression_error(); | 334 assignment_pattern_error_ = inner.expression_error(); |
| 237 } | 335 } |
| 238 } | 336 } |
| 239 } | 337 } |
| 240 | 338 |
| 241 private: | 339 private: |
| 340 AssignmentTargetType lhs_type_; | |
| 341 bool assigned_; | |
| 242 unsigned invalid_productions_; | 342 unsigned invalid_productions_; |
| 243 Error expression_error_; | 343 Error expression_error_; |
| 244 Error binding_pattern_error_; | 344 Error binding_pattern_error_; |
| 245 Error assignment_pattern_error_; | 345 Error assignment_pattern_error_; |
| 246 Error arrow_formal_parameters_error_; | 346 Error arrow_formal_parameters_error_; |
| 247 Error duplicate_formal_parameter_error_; | 347 Error duplicate_formal_parameter_error_; |
| 248 Error strict_mode_formal_parameter_error_; | 348 Error strict_mode_formal_parameter_error_; |
| 249 Error strong_mode_formal_parameter_error_; | 349 Error strong_mode_formal_parameter_error_; |
| 250 DuplicateFinder* duplicate_finder_; | 350 DuplicateFinder* duplicate_finder_; |
| 251 }; | 351 }; |
| 252 } | 352 } |
| 253 } // v8::internal | 353 } // v8::internal |
| 254 | 354 |
| 255 #endif // V8_EXPRESSION_CLASSIFIER_H | 355 #endif // V8_EXPRESSION_CLASSIFIER_H |
| OLD | NEW |