OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library dart2js.js_emitter.program_builder; | 5 library dart2js.js_emitter.program_builder; |
6 | 6 |
7 import 'js_emitter.dart' show computeMixinClass; | 7 import 'js_emitter.dart' show computeMixinClass; |
8 import 'model.dart'; | 8 import 'model.dart'; |
9 | 9 |
10 import '../common.dart'; | 10 import '../common.dart'; |
11 import '../js/js.dart' as js; | 11 import '../js/js.dart' as js; |
12 | 12 |
13 import '../js_backend/js_backend.dart' show | 13 import '../js_backend/js_backend.dart' show |
14 Namer, | 14 Namer, |
15 JavaScriptBackend, | 15 JavaScriptBackend, |
16 JavaScriptConstantCompiler; | 16 JavaScriptConstantCompiler; |
17 | 17 |
18 import 'js_emitter.dart' show | 18 import 'js_emitter.dart' show |
19 ClassStubGenerator, | 19 ClassStubGenerator, |
20 CodeEmitterTask, | 20 CodeEmitterTask, |
21 InterceptorStubGenerator, | 21 InterceptorStubGenerator, |
22 MainCallStubGenerator, | 22 MainCallStubGenerator, |
23 ParameterStubGenerator, | 23 ParameterStubGenerator, |
24 RuntimeTypeGenerator, | 24 RuntimeTypeGenerator, |
25 TypeTestProperties; | 25 TypeTestProperties; |
26 | 26 |
| 27 import '../elements/elements.dart' show ParameterElement; |
| 28 |
27 import '../universe/universe.dart' show Universe; | 29 import '../universe/universe.dart' show Universe; |
28 import '../deferred_load.dart' show DeferredLoadTask, OutputUnit; | 30 import '../deferred_load.dart' show DeferredLoadTask, OutputUnit; |
| 31 import '../constants/expressions.dart' show ConstantExpression, ConstantValue; |
29 | 32 |
30 part 'registry.dart'; | 33 part 'registry.dart'; |
31 | 34 |
32 class ProgramBuilder { | 35 class ProgramBuilder { |
33 final Compiler _compiler; | 36 final Compiler _compiler; |
34 final Namer namer; | 37 final Namer namer; |
35 final CodeEmitterTask _task; | 38 final CodeEmitterTask _task; |
36 | 39 |
37 final Registry _registry; | 40 final Registry _registry; |
38 | 41 |
(...skipping 412 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
451 // TODO(herhut): Refactor incremental compilation and remove method. | 454 // TODO(herhut): Refactor incremental compilation and remove method. |
452 Method buildMethodHackForIncrementalCompilation(FunctionElement element) { | 455 Method buildMethodHackForIncrementalCompilation(FunctionElement element) { |
453 assert(_compiler.hasIncrementalSupport); | 456 assert(_compiler.hasIncrementalSupport); |
454 if (element.isInstanceMember) { | 457 if (element.isInstanceMember) { |
455 return _buildMethod(element); | 458 return _buildMethod(element); |
456 } else { | 459 } else { |
457 return _buildStaticMethod(element); | 460 return _buildStaticMethod(element); |
458 } | 461 } |
459 } | 462 } |
460 | 463 |
| 464 /* Map | List */ _computeParameterDefaultValues(FunctionSignature signature) { |
| 465 var /* Map | List */ optionalParameterDefaultValues; |
| 466 if (signature.optionalParametersAreNamed) { |
| 467 optionalParameterDefaultValues = new Map<String, ConstantValue>(); |
| 468 signature.forEachOptionalParameter((ParameterElement parameter) { |
| 469 ConstantExpression def = |
| 470 backend.constants.getConstantForVariable(parameter); |
| 471 optionalParameterDefaultValues[parameter.name] = def.value; |
| 472 }); |
| 473 } else { |
| 474 optionalParameterDefaultValues = <ConstantValue>[]; |
| 475 signature.forEachOptionalParameter((ParameterElement parameter) { |
| 476 ConstantExpression def = |
| 477 backend.constants.getConstantForVariable(parameter); |
| 478 optionalParameterDefaultValues.add(def.value); |
| 479 }); |
| 480 } |
| 481 return optionalParameterDefaultValues; |
| 482 } |
| 483 |
461 DartMethod _buildMethod(FunctionElement element) { | 484 DartMethod _buildMethod(FunctionElement element) { |
462 String name = namer.getNameOfInstanceMember(element); | 485 String name = namer.getNameOfInstanceMember(element); |
463 js.Expression code = backend.generatedCode[element]; | 486 js.Expression code = backend.generatedCode[element]; |
464 | 487 |
465 // TODO(kasperl): Figure out under which conditions code is null. | 488 // TODO(kasperl): Figure out under which conditions code is null. |
466 if (code == null) return null; | 489 if (code == null) return null; |
467 | 490 |
468 bool canTearOff = false; | 491 bool canTearOff = false; |
469 String tearOffName; | 492 String tearOffName; |
470 bool isClosure = false; | 493 bool isClosure = false; |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
511 if (element.isGenerativeConstructorBody) { | 534 if (element.isGenerativeConstructorBody) { |
512 // TODO(herhut): Why does this need to be normalized away? We never need | 535 // TODO(herhut): Why does this need to be normalized away? We never need |
513 // this information anyway as they cannot be torn off or | 536 // this information anyway as they cannot be torn off or |
514 // reflected. | 537 // reflected. |
515 var body = element; | 538 var body = element; |
516 memberType = body.constructor.type; | 539 memberType = body.constructor.type; |
517 } else { | 540 } else { |
518 memberType = element.type; | 541 memberType = element.type; |
519 } | 542 } |
520 | 543 |
| 544 int requiredParameterCount; |
| 545 var /* List | Map */ optionalParameterDefaultValues; |
| 546 if (canBeApplied || canBeReflected) { |
| 547 FunctionSignature signature = element.functionSignature; |
| 548 requiredParameterCount = signature.requiredParameterCount; |
| 549 optionalParameterDefaultValues = |
| 550 _computeParameterDefaultValues(signature); |
| 551 } |
| 552 |
521 return new InstanceMethod(element, name, code, | 553 return new InstanceMethod(element, name, code, |
522 _generateParameterStubs(element, canTearOff), callName, memberType, | 554 _generateParameterStubs(element, canTearOff), callName, memberType, |
523 needsTearOff: canTearOff, tearOffName: tearOffName, | 555 needsTearOff: canTearOff, tearOffName: tearOffName, |
524 isClosure: isClosure, aliasName: aliasName, | 556 isClosure: isClosure, aliasName: aliasName, |
525 canBeApplied: canBeApplied, canBeReflected: canBeReflected); | 557 canBeApplied: canBeApplied, canBeReflected: canBeReflected, |
| 558 requiredParameterCount: requiredParameterCount, |
| 559 optionalParameterDefaultValues: optionalParameterDefaultValues); |
526 } | 560 } |
527 | 561 |
528 List<ParameterStubMethod> _generateParameterStubs(FunctionElement element, | 562 List<ParameterStubMethod> _generateParameterStubs(FunctionElement element, |
529 bool canTearOff) { | 563 bool canTearOff) { |
530 | 564 |
531 if (!_methodNeedsStubs(element)) return const <ParameterStubMethod>[]; | 565 if (!_methodNeedsStubs(element)) return const <ParameterStubMethod>[]; |
532 | 566 |
533 ParameterStubGenerator generator = | 567 ParameterStubGenerator generator = |
534 new ParameterStubGenerator(_compiler, namer, backend); | 568 new ParameterStubGenerator(_compiler, namer, backend); |
535 return generator.generateParameterStubs(element, canTearOff: canTearOff); | 569 return generator.generateParameterStubs(element, canTearOff: canTearOff); |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
655 String tearOffName = | 689 String tearOffName = |
656 needsTearOff ? namer.getStaticClosureName(element) : null; | 690 needsTearOff ? namer.getStaticClosureName(element) : null; |
657 | 691 |
658 String callName = null; | 692 String callName = null; |
659 if (needsTearOff) { | 693 if (needsTearOff) { |
660 Selector callSelector = | 694 Selector callSelector = |
661 new Selector.fromElement(element).toCallSelector(); | 695 new Selector.fromElement(element).toCallSelector(); |
662 callName = namer.invocationName(callSelector); | 696 callName = namer.invocationName(callSelector); |
663 } | 697 } |
664 | 698 |
| 699 int requiredParameterCount; |
| 700 var /* List | Map */ optionalParameterDefaultValues; |
| 701 if (canBeApplied || canBeReflected) { |
| 702 FunctionSignature signature = element.functionSignature; |
| 703 requiredParameterCount = signature.requiredParameterCount; |
| 704 optionalParameterDefaultValues = |
| 705 _computeParameterDefaultValues(signature); |
| 706 } |
| 707 |
665 // TODO(floitsch): we shouldn't update the registry in the middle of | 708 // TODO(floitsch): we shouldn't update the registry in the middle of |
666 // building a static method. | 709 // building a static method. |
667 return new StaticDartMethod(element, | 710 return new StaticDartMethod(element, |
668 name, _registry.registerHolder(holder), code, | 711 name, _registry.registerHolder(holder), code, |
669 _generateParameterStubs(element, needsTearOff), | 712 _generateParameterStubs(element, needsTearOff), |
670 callName, element.type, | 713 callName, element.type, |
671 needsTearOff: needsTearOff, | 714 needsTearOff: needsTearOff, |
672 tearOffName: tearOffName, | 715 tearOffName: tearOffName, |
673 canBeApplied: canBeApplied, | 716 canBeApplied: canBeApplied, |
674 canBeReflected: canBeReflected); | 717 canBeReflected: canBeReflected, |
| 718 requiredParameterCount: requiredParameterCount, |
| 719 optionalParameterDefaultValues: |
| 720 optionalParameterDefaultValues); |
675 } | 721 } |
676 | 722 |
677 void _registerConstants(OutputUnit outputUnit, | 723 void _registerConstants(OutputUnit outputUnit, |
678 Iterable<ConstantValue> constantValues) { | 724 Iterable<ConstantValue> constantValues) { |
679 // `constantValues` is null if an outputUnit doesn't contain any constants. | 725 // `constantValues` is null if an outputUnit doesn't contain any constants. |
680 if (constantValues == null) return; | 726 if (constantValues == null) return; |
681 for (ConstantValue constantValue in constantValues) { | 727 for (ConstantValue constantValue in constantValues) { |
682 _registry.registerConstant(outputUnit, constantValue); | 728 _registry.registerConstant(outputUnit, constantValue); |
683 assert(!_constants.containsKey(constantValue)); | 729 assert(!_constants.containsKey(constantValue)); |
684 String name = namer.constantName(constantValue); | 730 String name = namer.constantName(constantValue); |
685 String constantObject = namer.globalObjectForConstant(constantValue); | 731 String constantObject = namer.globalObjectForConstant(constantValue); |
686 Holder holder = _registry.registerHolder(constantObject); | 732 Holder holder = _registry.registerHolder(constantObject); |
687 Constant constant = new Constant(name, holder, constantValue); | 733 Constant constant = new Constant(name, holder, constantValue); |
688 _constants[constantValue] = constant; | 734 _constants[constantValue] = constant; |
689 } | 735 } |
690 } | 736 } |
691 } | 737 } |
OLD | NEW |