OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 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 #include "src/parsing/parser.h" | 5 #include "src/parsing/parser.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #include "src/api.h" | 9 #include "src/api.h" |
10 #include "src/ast/ast-expression-rewriter.h" | 10 #include "src/ast/ast-expression-rewriter.h" |
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
290 | 290 |
291 #define CHECK_FAILED /**/); \ | 291 #define CHECK_FAILED /**/); \ |
292 if (failed_) return nullptr; \ | 292 if (failed_) return nullptr; \ |
293 ((void)0 | 293 ((void)0 |
294 #define DUMMY ) // to make indentation work | 294 #define DUMMY ) // to make indentation work |
295 #undef DUMMY | 295 #undef DUMMY |
296 | 296 |
297 // ---------------------------------------------------------------------------- | 297 // ---------------------------------------------------------------------------- |
298 // Implementation of Parser | 298 // Implementation of Parser |
299 | 299 |
300 bool ParserBaseTraits<Parser>::IsEval(const AstRawString* identifier) const { | 300 bool Parser::ShortcutNumericLiteralBinaryExpression(Expression** x, |
301 return identifier == delegate()->ast_value_factory()->eval_string(); | 301 Expression* y, |
302 } | 302 Token::Value op, int pos) { |
303 | |
304 bool ParserBaseTraits<Parser>::IsArguments( | |
305 const AstRawString* identifier) const { | |
306 return identifier == delegate()->ast_value_factory()->arguments_string(); | |
307 } | |
308 | |
309 bool ParserBaseTraits<Parser>::IsEvalOrArguments( | |
310 const AstRawString* identifier) const { | |
311 return IsEval(identifier) || IsArguments(identifier); | |
312 } | |
313 | |
314 bool ParserBaseTraits<Parser>::IsUndefined( | |
315 const AstRawString* identifier) const { | |
316 return identifier == delegate()->ast_value_factory()->undefined_string(); | |
317 } | |
318 | |
319 bool ParserBaseTraits<Parser>::IsPrototype( | |
320 const AstRawString* identifier) const { | |
321 return identifier == delegate()->ast_value_factory()->prototype_string(); | |
322 } | |
323 | |
324 bool ParserBaseTraits<Parser>::IsConstructor( | |
325 const AstRawString* identifier) const { | |
326 return identifier == delegate()->ast_value_factory()->constructor_string(); | |
327 } | |
328 | |
329 bool ParserBaseTraits<Parser>::IsThisProperty(Expression* expression) { | |
330 DCHECK(expression != NULL); | |
331 Property* property = expression->AsProperty(); | |
332 return property != NULL && property->obj()->IsVariableProxy() && | |
333 property->obj()->AsVariableProxy()->is_this(); | |
334 } | |
335 | |
336 bool ParserBaseTraits<Parser>::IsIdentifier(Expression* expression) { | |
337 VariableProxy* operand = expression->AsVariableProxy(); | |
338 return operand != NULL && !operand->is_this(); | |
339 } | |
340 | |
341 void ParserBaseTraits<Parser>::PushPropertyName(FuncNameInferrer* fni, | |
342 Expression* expression) { | |
343 if (expression->IsPropertyName()) { | |
344 fni->PushLiteralName(expression->AsLiteral()->AsRawPropertyName()); | |
345 } else { | |
346 fni->PushLiteralName( | |
347 delegate()->ast_value_factory()->anonymous_function_string()); | |
348 } | |
349 } | |
350 | |
351 void ParserBaseTraits<Parser>::CheckAssigningFunctionLiteralToProperty( | |
352 Expression* left, Expression* right) { | |
353 DCHECK(left != NULL); | |
354 if (left->IsProperty() && right->IsFunctionLiteral()) { | |
355 right->AsFunctionLiteral()->set_pretenure(); | |
356 } | |
357 } | |
358 | |
359 Expression* ParserBaseTraits<Parser>::MarkExpressionAsAssigned( | |
360 Expression* expression) { | |
361 VariableProxy* proxy = | |
362 expression != NULL ? expression->AsVariableProxy() : NULL; | |
363 if (proxy != NULL) proxy->set_is_assigned(); | |
364 return expression; | |
365 } | |
366 | |
367 bool ParserBaseTraits<Parser>::ShortcutNumericLiteralBinaryExpression( | |
368 Expression** x, Expression* y, Token::Value op, int pos, | |
369 AstNodeFactory* factory) { | |
370 if ((*x)->AsLiteral() && (*x)->AsLiteral()->raw_value()->IsNumber() && | 303 if ((*x)->AsLiteral() && (*x)->AsLiteral()->raw_value()->IsNumber() && |
371 y->AsLiteral() && y->AsLiteral()->raw_value()->IsNumber()) { | 304 y->AsLiteral() && y->AsLiteral()->raw_value()->IsNumber()) { |
372 double x_val = (*x)->AsLiteral()->raw_value()->AsNumber(); | 305 double x_val = (*x)->AsLiteral()->raw_value()->AsNumber(); |
373 double y_val = y->AsLiteral()->raw_value()->AsNumber(); | 306 double y_val = y->AsLiteral()->raw_value()->AsNumber(); |
374 bool x_has_dot = (*x)->AsLiteral()->raw_value()->ContainsDot(); | 307 bool x_has_dot = (*x)->AsLiteral()->raw_value()->ContainsDot(); |
375 bool y_has_dot = y->AsLiteral()->raw_value()->ContainsDot(); | 308 bool y_has_dot = y->AsLiteral()->raw_value()->ContainsDot(); |
376 bool has_dot = x_has_dot || y_has_dot; | 309 bool has_dot = x_has_dot || y_has_dot; |
377 switch (op) { | 310 switch (op) { |
378 case Token::ADD: | 311 case Token::ADD: |
379 *x = factory->NewNumberLiteral(x_val + y_val, pos, has_dot); | 312 *x = factory()->NewNumberLiteral(x_val + y_val, pos, has_dot); |
380 return true; | 313 return true; |
381 case Token::SUB: | 314 case Token::SUB: |
382 *x = factory->NewNumberLiteral(x_val - y_val, pos, has_dot); | 315 *x = factory()->NewNumberLiteral(x_val - y_val, pos, has_dot); |
383 return true; | 316 return true; |
384 case Token::MUL: | 317 case Token::MUL: |
385 *x = factory->NewNumberLiteral(x_val * y_val, pos, has_dot); | 318 *x = factory()->NewNumberLiteral(x_val * y_val, pos, has_dot); |
386 return true; | 319 return true; |
387 case Token::DIV: | 320 case Token::DIV: |
388 *x = factory->NewNumberLiteral(x_val / y_val, pos, has_dot); | 321 *x = factory()->NewNumberLiteral(x_val / y_val, pos, has_dot); |
389 return true; | 322 return true; |
390 case Token::BIT_OR: { | 323 case Token::BIT_OR: { |
391 int value = DoubleToInt32(x_val) | DoubleToInt32(y_val); | 324 int value = DoubleToInt32(x_val) | DoubleToInt32(y_val); |
392 *x = factory->NewNumberLiteral(value, pos, has_dot); | 325 *x = factory()->NewNumberLiteral(value, pos, has_dot); |
393 return true; | 326 return true; |
394 } | 327 } |
395 case Token::BIT_AND: { | 328 case Token::BIT_AND: { |
396 int value = DoubleToInt32(x_val) & DoubleToInt32(y_val); | 329 int value = DoubleToInt32(x_val) & DoubleToInt32(y_val); |
397 *x = factory->NewNumberLiteral(value, pos, has_dot); | 330 *x = factory()->NewNumberLiteral(value, pos, has_dot); |
398 return true; | 331 return true; |
399 } | 332 } |
400 case Token::BIT_XOR: { | 333 case Token::BIT_XOR: { |
401 int value = DoubleToInt32(x_val) ^ DoubleToInt32(y_val); | 334 int value = DoubleToInt32(x_val) ^ DoubleToInt32(y_val); |
402 *x = factory->NewNumberLiteral(value, pos, has_dot); | 335 *x = factory()->NewNumberLiteral(value, pos, has_dot); |
403 return true; | 336 return true; |
404 } | 337 } |
405 case Token::SHL: { | 338 case Token::SHL: { |
406 int value = DoubleToInt32(x_val) << (DoubleToInt32(y_val) & 0x1f); | 339 int value = DoubleToInt32(x_val) << (DoubleToInt32(y_val) & 0x1f); |
407 *x = factory->NewNumberLiteral(value, pos, has_dot); | 340 *x = factory()->NewNumberLiteral(value, pos, has_dot); |
408 return true; | 341 return true; |
409 } | 342 } |
410 case Token::SHR: { | 343 case Token::SHR: { |
411 uint32_t shift = DoubleToInt32(y_val) & 0x1f; | 344 uint32_t shift = DoubleToInt32(y_val) & 0x1f; |
412 uint32_t value = DoubleToUint32(x_val) >> shift; | 345 uint32_t value = DoubleToUint32(x_val) >> shift; |
413 *x = factory->NewNumberLiteral(value, pos, has_dot); | 346 *x = factory()->NewNumberLiteral(value, pos, has_dot); |
414 return true; | 347 return true; |
415 } | 348 } |
416 case Token::SAR: { | 349 case Token::SAR: { |
417 uint32_t shift = DoubleToInt32(y_val) & 0x1f; | 350 uint32_t shift = DoubleToInt32(y_val) & 0x1f; |
418 int value = ArithmeticShiftRight(DoubleToInt32(x_val), shift); | 351 int value = ArithmeticShiftRight(DoubleToInt32(x_val), shift); |
419 *x = factory->NewNumberLiteral(value, pos, has_dot); | 352 *x = factory()->NewNumberLiteral(value, pos, has_dot); |
420 return true; | 353 return true; |
421 } | 354 } |
422 case Token::EXP: { | 355 case Token::EXP: { |
423 double value = Pow(x_val, y_val); | 356 double value = Pow(x_val, y_val); |
424 int int_value = static_cast<int>(value); | 357 int int_value = static_cast<int>(value); |
425 *x = factory->NewNumberLiteral( | 358 *x = factory()->NewNumberLiteral( |
426 int_value == value && value != -0.0 ? int_value : value, pos, | 359 int_value == value && value != -0.0 ? int_value : value, pos, |
427 has_dot); | 360 has_dot); |
428 return true; | 361 return true; |
429 } | 362 } |
430 default: | 363 default: |
431 break; | 364 break; |
432 } | 365 } |
433 } | 366 } |
434 return false; | 367 return false; |
435 } | 368 } |
(...skipping 6405 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6841 node->Print(Isolate::Current()); | 6774 node->Print(Isolate::Current()); |
6842 } | 6775 } |
6843 #endif // DEBUG | 6776 #endif // DEBUG |
6844 | 6777 |
6845 #undef CHECK_OK | 6778 #undef CHECK_OK |
6846 #undef CHECK_OK_VOID | 6779 #undef CHECK_OK_VOID |
6847 #undef CHECK_FAILED | 6780 #undef CHECK_FAILED |
6848 | 6781 |
6849 } // namespace internal | 6782 } // namespace internal |
6850 } // namespace v8 | 6783 } // namespace v8 |
OLD | NEW |