Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(43)

Side by Side Diff: sdk/lib/_internal/compiler/implementation/js_emitter/code_emitter_task.dart

Issue 266913017: Convert property methods into getters. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Rebased Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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 part of dart2js.js_emitter; 5 part of dart2js.js_emitter;
6 6
7 /** 7 /**
8 * Generates the code for all used classes in the program. Static fields (even 8 * Generates the code for all used classes in the program. Static fields (even
9 * in classes) are ignored, since they can be treated as non-class elements. 9 * in classes) are ignored, since they can be treated as non-class elements.
10 * 10 *
(...skipping 599 matching lines...) Expand 10 before | Expand all | Expand 10 after
610 ]; 610 ];
611 } 611 }
612 612
613 void emitFinishIsolateConstructorInvocation(CodeBuffer buffer) { 613 void emitFinishIsolateConstructorInvocation(CodeBuffer buffer) {
614 String isolate = namer.isolateName; 614 String isolate = namer.isolateName;
615 buffer.write("$isolate = $finishIsolateConstructorName($isolate)$N"); 615 buffer.write("$isolate = $finishIsolateConstructorName($isolate)$N");
616 } 616 }
617 617
618 /// In minified mode we want to keep the name for the most common core types. 618 /// In minified mode we want to keep the name for the most common core types.
619 bool _isNativeTypeNeedingReflectionName(Element element) { 619 bool _isNativeTypeNeedingReflectionName(Element element) {
620 if (!element.isClass()) return false; 620 if (!element.isClass) return false;
621 return (element == compiler.intClass || 621 return (element == compiler.intClass ||
622 element == compiler.doubleClass || 622 element == compiler.doubleClass ||
623 element == compiler.numClass || 623 element == compiler.numClass ||
624 element == compiler.stringClass || 624 element == compiler.stringClass ||
625 element == compiler.boolClass || 625 element == compiler.boolClass ||
626 element == compiler.nullClass || 626 element == compiler.nullClass ||
627 element == compiler.listClass); 627 element == compiler.listClass);
628 } 628 }
629 629
630 /// Returns the "reflection name" of an [Element] or [Selector]. 630 /// Returns the "reflection name" of an [Element] or [Selector].
(...skipping 20 matching lines...) Expand all
651 // an instance method and a global. They may have the same mangled name. 651 // an instance method and a global. They may have the same mangled name.
652 // if (recordedMangledNames.contains(mangledName)) return null; 652 // if (recordedMangledNames.contains(mangledName)) return null;
653 recordedMangledNames.add(mangledName); 653 recordedMangledNames.add(mangledName);
654 return getReflectionNameInternal(elementOrSelector, mangledName); 654 return getReflectionNameInternal(elementOrSelector, mangledName);
655 } 655 }
656 return null; 656 return null;
657 } 657 }
658 658
659 String getReflectionNameInternal(elementOrSelector, String mangledName) { 659 String getReflectionNameInternal(elementOrSelector, String mangledName) {
660 String name = elementOrSelector.name; 660 String name = elementOrSelector.name;
661 if (elementOrSelector.isGetter()) return name; 661 if (elementOrSelector.isGetter) return name;
662 if (elementOrSelector.isSetter()) { 662 if (elementOrSelector.isSetter) {
663 if (!mangledName.startsWith(namer.setterPrefix)) return '$name='; 663 if (!mangledName.startsWith(namer.setterPrefix)) return '$name=';
664 String base = mangledName.substring(namer.setterPrefix.length); 664 String base = mangledName.substring(namer.setterPrefix.length);
665 String getter = '${namer.getterPrefix}$base'; 665 String getter = '${namer.getterPrefix}$base';
666 mangledFieldNames[getter] = name; 666 mangledFieldNames[getter] = name;
667 recordedMangledNames.add(getter); 667 recordedMangledNames.add(getter);
668 // TODO(karlklose,ahe): we do not actually need to store information 668 // TODO(karlklose,ahe): we do not actually need to store information
669 // about the name of this setter in the output, but it is needed for 669 // about the name of this setter in the output, but it is needed for
670 // marking the function as invokable by reflection. 670 // marking the function as invokable by reflection.
671 return '$name='; 671 return '$name=';
672 } 672 }
673 if (elementOrSelector is Element && elementOrSelector.isClosure()) { 673 if (elementOrSelector is Element && elementOrSelector.isClosure) {
674 // Closures are synthesized and their name might conflict with existing 674 // Closures are synthesized and their name might conflict with existing
675 // globals. Assign an illegal name, and make sure they don't clash 675 // globals. Assign an illegal name, and make sure they don't clash
676 // with each other. 676 // with each other.
677 return " $mangledName"; 677 return " $mangledName";
678 } 678 }
679 if (elementOrSelector is Selector 679 if (elementOrSelector is Selector
680 || elementOrSelector.isFunction() 680 || elementOrSelector.isFunction
681 || elementOrSelector.isConstructor()) { 681 || elementOrSelector.isConstructor) {
682 int requiredParameterCount; 682 int requiredParameterCount;
683 int optionalParameterCount; 683 int optionalParameterCount;
684 String namedArguments = ''; 684 String namedArguments = '';
685 bool isConstructor = false; 685 bool isConstructor = false;
686 if (elementOrSelector is Selector) { 686 if (elementOrSelector is Selector) {
687 Selector selector = elementOrSelector; 687 Selector selector = elementOrSelector;
688 requiredParameterCount = selector.argumentCount; 688 requiredParameterCount = selector.argumentCount;
689 optionalParameterCount = 0; 689 optionalParameterCount = 0;
690 namedArguments = namedParametersAsReflectionNames(selector); 690 namedArguments = namedParametersAsReflectionNames(selector);
691 } else { 691 } else {
692 FunctionElement function = elementOrSelector; 692 FunctionElement function = elementOrSelector;
693 if (function.isConstructor()) { 693 if (function.isConstructor) {
694 isConstructor = true; 694 isConstructor = true;
695 name = Elements.reconstructConstructorName(function); 695 name = Elements.reconstructConstructorName(function);
696 } 696 }
697 FunctionSignature signature = function.functionSignature; 697 FunctionSignature signature = function.functionSignature;
698 requiredParameterCount = signature.requiredParameterCount; 698 requiredParameterCount = signature.requiredParameterCount;
699 optionalParameterCount = signature.optionalParameterCount; 699 optionalParameterCount = signature.optionalParameterCount;
700 if (signature.optionalParametersAreNamed) { 700 if (signature.optionalParametersAreNamed) {
701 var names = []; 701 var names = [];
702 for (Element e in signature.optionalParameters) { 702 for (Element e in signature.optionalParameters) {
703 names.add(e.name); 703 names.add(e.name);
704 } 704 }
705 Selector selector = new Selector.call( 705 Selector selector = new Selector.call(
706 function.name, 706 function.name,
707 function.getLibrary(), 707 function.library,
708 requiredParameterCount, 708 requiredParameterCount,
709 names); 709 names);
710 namedArguments = namedParametersAsReflectionNames(selector); 710 namedArguments = namedParametersAsReflectionNames(selector);
711 } else { 711 } else {
712 // Named parameters are handled differently by mirrors. For unnamed 712 // Named parameters are handled differently by mirrors. For unnamed
713 // parameters, they are actually required if invoked 713 // parameters, they are actually required if invoked
714 // reflectively. Also, if you have a method c(x) and c([x]) they both 714 // reflectively. Also, if you have a method c(x) and c([x]) they both
715 // get the same mangled name, so they must have the same reflection 715 // get the same mangled name, so they must have the same reflection
716 // name. 716 // name.
717 requiredParameterCount += optionalParameterCount; 717 requiredParameterCount += optionalParameterCount;
718 optionalParameterCount = 0; 718 optionalParameterCount = 0;
719 } 719 }
720 } 720 }
721 String suffix = 721 String suffix =
722 // TODO(ahe): We probably don't need optionalParameterCount in the 722 // TODO(ahe): We probably don't need optionalParameterCount in the
723 // reflection name. 723 // reflection name.
724 '$name:$requiredParameterCount:$optionalParameterCount' 724 '$name:$requiredParameterCount:$optionalParameterCount'
725 '$namedArguments'; 725 '$namedArguments';
726 return (isConstructor) ? 'new $suffix' : suffix; 726 return (isConstructor) ? 'new $suffix' : suffix;
727 } 727 }
728 Element element = elementOrSelector; 728 Element element = elementOrSelector;
729 if (element.isGenerativeConstructorBody()) { 729 if (element.isGenerativeConstructorBody) {
730 return null; 730 return null;
731 } else if (element.isClass()) { 731 } else if (element.isClass) {
732 ClassElement cls = element; 732 ClassElement cls = element;
733 if (cls.isUnnamedMixinApplication) return null; 733 if (cls.isUnnamedMixinApplication) return null;
734 return cls.name; 734 return cls.name;
735 } 735 }
736 throw compiler.internalError(element, 736 throw compiler.internalError(element,
737 'Do not know how to reflect on this $element.'); 737 'Do not know how to reflect on this $element.');
738 } 738 }
739 739
740 String namedParametersAsReflectionNames(Selector selector) { 740 String namedParametersAsReflectionNames(Selector selector) {
741 if (selector.getOrderedNamedArguments().isEmpty) return ''; 741 if (selector.getOrderedNamedArguments().isEmpty) return '';
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
812 '$_$isolateProperties,' 812 '$_$isolateProperties,'
813 '${_}null)$N'); 813 '${_}null)$N');
814 814
815 // Reset the map. 815 // Reset the map.
816 buffer.write("$classesCollector$_=${_}null$N$n"); 816 buffer.write("$classesCollector$_=${_}null$N$n");
817 } 817 }
818 } 818 }
819 819
820 void emitStaticFunctions() { 820 void emitStaticFunctions() {
821 bool isStaticFunction(Element element) => 821 bool isStaticFunction(Element element) =>
822 !element.isInstanceMember() && !element.isField(); 822 !element.isInstanceMember && !element.isField;
823 823
824 Iterable<Element> elements = 824 Iterable<Element> elements =
825 backend.generatedCode.keys.where(isStaticFunction); 825 backend.generatedCode.keys.where(isStaticFunction);
826 826
827 for (Element element in Elements.sortedByPosition(elements)) { 827 for (Element element in Elements.sortedByPosition(elements)) {
828 ClassBuilder builder = new ClassBuilder(namer); 828 ClassBuilder builder = new ClassBuilder(namer);
829 containerBuilder.addMember(element, builder); 829 containerBuilder.addMember(element, builder);
830 getElementDecriptor(element).properties.addAll(builder.properties); 830 getElementDecriptor(element).properties.addAll(builder.properties);
831 } 831 }
832 } 832 }
(...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after
1126 .toSet(); 1126 .toSet();
1127 neededClasses.addAll(mixinClasses); 1127 neededClasses.addAll(mixinClasses);
1128 1128
1129 // 3. If we need noSuchMethod support, we run through all needed 1129 // 3. If we need noSuchMethod support, we run through all needed
1130 // classes to figure out if we need the support on any native 1130 // classes to figure out if we need the support on any native
1131 // class. If so, we let the native emitter deal with it. 1131 // class. If so, we let the native emitter deal with it.
1132 if (compiler.enabledNoSuchMethod) { 1132 if (compiler.enabledNoSuchMethod) {
1133 String noSuchMethodName = Compiler.NO_SUCH_METHOD; 1133 String noSuchMethodName = Compiler.NO_SUCH_METHOD;
1134 Selector noSuchMethodSelector = compiler.noSuchMethodSelector; 1134 Selector noSuchMethodSelector = compiler.noSuchMethodSelector;
1135 for (ClassElement element in neededClasses) { 1135 for (ClassElement element in neededClasses) {
1136 if (!element.isNative()) continue; 1136 if (!element.isNative) continue;
1137 Element member = element.lookupLocalMember(noSuchMethodName); 1137 Element member = element.lookupLocalMember(noSuchMethodName);
1138 if (member == null) continue; 1138 if (member == null) continue;
1139 if (noSuchMethodSelector.applies(member, compiler)) { 1139 if (noSuchMethodSelector.applies(member, compiler)) {
1140 nativeEmitter.handleNoSuchMethod = true; 1140 nativeEmitter.handleNoSuchMethod = true;
1141 break; 1141 break;
1142 } 1142 }
1143 } 1143 }
1144 } 1144 }
1145 1145
1146 // 4. Find all classes needed for rti. 1146 // 4. Find all classes needed for rti.
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
1179 List<ClassElement> sortedClasses = Elements.sortedByPosition(neededClasses); 1179 List<ClassElement> sortedClasses = Elements.sortedByPosition(neededClasses);
1180 1180
1181 for (ClassElement element in sortedClasses) { 1181 for (ClassElement element in sortedClasses) {
1182 if (typeTestEmitter.rtiNeededClasses.contains(element)) { 1182 if (typeTestEmitter.rtiNeededClasses.contains(element)) {
1183 // TODO(sigurdm): We might be able to defer some of these. 1183 // TODO(sigurdm): We might be able to defer some of these.
1184 outputClassLists.putIfAbsent(compiler.deferredLoadTask.mainOutputUnit, 1184 outputClassLists.putIfAbsent(compiler.deferredLoadTask.mainOutputUnit,
1185 () => new List<ClassElement>()).add(element); 1185 () => new List<ClassElement>()).add(element);
1186 } else if (Elements.isNativeOrExtendsNative(element)) { 1186 } else if (Elements.isNativeOrExtendsNative(element)) {
1187 // For now, native classes and related classes cannot be deferred. 1187 // For now, native classes and related classes cannot be deferred.
1188 nativeClasses.add(element); 1188 nativeClasses.add(element);
1189 if (!element.isNative()) { 1189 if (!element.isNative) {
1190 assert(invariant(element, 1190 assert(invariant(element,
1191 !compiler.deferredLoadTask.isDeferred(element))); 1191 !compiler.deferredLoadTask.isDeferred(element)));
1192 outputClassLists.putIfAbsent(compiler.deferredLoadTask.mainOutputUnit, 1192 outputClassLists.putIfAbsent(compiler.deferredLoadTask.mainOutputUnit,
1193 () => new List<ClassElement>()).add(element); 1193 () => new List<ClassElement>()).add(element);
1194 } 1194 }
1195 } else { 1195 } else {
1196 outputClassLists.putIfAbsent( 1196 outputClassLists.putIfAbsent(
1197 compiler.deferredLoadTask.outputUnitForElement(element), 1197 compiler.deferredLoadTask.outputUnitForElement(element),
1198 () => new List<ClassElement>()) 1198 () => new List<ClassElement>())
1199 .add(element); 1199 .add(element);
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
1368 if (!elementDescriptors.isEmpty) { 1368 if (!elementDescriptors.isEmpty) {
1369 var oldClassesCollector = classesCollector; 1369 var oldClassesCollector = classesCollector;
1370 classesCollector = r"$$"; 1370 classesCollector = r"$$";
1371 if (compiler.enableMinification) { 1371 if (compiler.enableMinification) {
1372 mainBuffer.write(';'); 1372 mainBuffer.write(';');
1373 } 1373 }
1374 1374
1375 for (Element element in elementDescriptors.keys) { 1375 for (Element element in elementDescriptors.keys) {
1376 // TODO(ahe): Should iterate over all libraries. Otherwise, we will 1376 // TODO(ahe): Should iterate over all libraries. Otherwise, we will
1377 // not see libraries that only have fields. 1377 // not see libraries that only have fields.
1378 if (element.isLibrary()) { 1378 if (element.isLibrary) {
1379 LibraryElement library = element; 1379 LibraryElement library = element;
1380 ClassBuilder builder = new ClassBuilder(namer); 1380 ClassBuilder builder = new ClassBuilder(namer);
1381 if (classEmitter.emitFields( 1381 if (classEmitter.emitFields(
1382 library, builder, null, emitStatics: true)) { 1382 library, builder, null, emitStatics: true)) {
1383 OutputUnit mainUnit = compiler.deferredLoadTask.mainOutputUnit; 1383 OutputUnit mainUnit = compiler.deferredLoadTask.mainOutputUnit;
1384 getElementDescriptorForOutputUnit(library, mainUnit) 1384 getElementDescriptorForOutputUnit(library, mainUnit)
1385 .properties.addAll(builder.toObjectInitializer().properties); 1385 .properties.addAll(builder.toObjectInitializer().properties);
1386 } 1386 }
1387 } 1387 }
1388 } 1388 }
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
1426 jsAst.prettyPrint( 1426 jsAst.prettyPrint(
1427 getReflectionDataParser(classesCollector, backend), 1427 getReflectionDataParser(classesCollector, backend),
1428 compiler)) 1428 compiler))
1429 ..write(')') 1429 ..write(')')
1430 ..write('([$n'); 1430 ..write('([$n');
1431 1431
1432 List<Element> sortedElements = 1432 List<Element> sortedElements =
1433 Elements.sortedByPosition(elementDescriptors.keys); 1433 Elements.sortedByPosition(elementDescriptors.keys);
1434 1434
1435 Iterable<Element> pendingStatics = sortedElements.where((element) { 1435 Iterable<Element> pendingStatics = sortedElements.where((element) {
1436 return !element.isLibrary() && 1436 return !element.isLibrary &&
1437 elementDescriptors[element].values.any((descriptor) => 1437 elementDescriptors[element].values.any((descriptor) =>
1438 descriptor != null); 1438 descriptor != null);
1439 }); 1439 });
1440 1440
1441 pendingStatics.forEach((element) => 1441 pendingStatics.forEach((element) =>
1442 compiler.reportInfo( 1442 compiler.reportInfo(
1443 element, MessageKind.GENERIC, {'text': 'Pending statics.'})); 1443 element, MessageKind.GENERIC, {'text': 'Pending statics.'}));
1444 1444
1445 for (LibraryElement library in sortedElements.where((element) => 1445 for (LibraryElement library in sortedElements.where((element) =>
1446 element.isLibrary())) { 1446 element.isLibrary)) {
1447 writeLibraryDescriptors(library); 1447 writeLibraryDescriptors(library);
1448 elementDescriptors[library] = const {}; 1448 elementDescriptors[library] = const {};
1449 } 1449 }
1450 if (!pendingStatics.isEmpty) { 1450 if (!pendingStatics.isEmpty) {
1451 compiler.internalError(pendingStatics.first, 1451 compiler.internalError(pendingStatics.first,
1452 'Pending statics (see above).'); 1452 'Pending statics (see above).');
1453 } 1453 }
1454 mainBuffer.write('])$N'); 1454 mainBuffer.write('])$N');
1455 1455
1456 emitFinishClassesInvocationIfNecessary(mainBuffer); 1456 emitFinishClassesInvocationIfNecessary(mainBuffer);
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
1581 } 1581 }
1582 compiler.outputProvider('', 'js') 1582 compiler.outputProvider('', 'js')
1583 ..add(assembledCode) 1583 ..add(assembledCode)
1584 ..add(sourceMapTags) 1584 ..add(sourceMapTags)
1585 ..close(); 1585 ..close();
1586 compiler.assembledCode = assembledCode; 1586 compiler.assembledCode = assembledCode;
1587 1587
1588 if (!compiler.useContentSecurityPolicy) { 1588 if (!compiler.useContentSecurityPolicy) {
1589 mainBuffer.write(""" 1589 mainBuffer.write("""
1590 { 1590 {
1591 var message = 1591 var message =
1592 'Deprecation: Automatic generation of output for Content Security\\n' + 1592 'Deprecation: Automatic generation of output for Content Security\\n' +
1593 'Policy is deprecated and will be removed with the next development\\n' + 1593 'Policy is deprecated and will be removed with the next development\\n' +
1594 'release. Use the --csp option to generate CSP restricted output.'; 1594 'release. Use the --csp option to generate CSP restricted output.';
1595 if (typeof dartPrint == "function") { 1595 if (typeof dartPrint == "function") {
1596 dartPrint(message); 1596 dartPrint(message);
1597 } else if (typeof console == "object" && typeof console.log == "function") { 1597 } else if (typeof console == "object" && typeof console.log == "function") {
1598 console.log(message); 1598 console.log(message);
1599 } else if (typeof print == "function") { 1599 } else if (typeof print == "function") {
1600 print(message); 1600 print(message);
1601 } 1601 }
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
1638 ClassBuilder getElementDescriptorForOutputUnit(Element element, 1638 ClassBuilder getElementDescriptorForOutputUnit(Element element,
1639 OutputUnit outputUnit) { 1639 OutputUnit outputUnit) {
1640 Map<OutputUnit, ClassBuilder> descriptors = 1640 Map<OutputUnit, ClassBuilder> descriptors =
1641 elementDescriptors.putIfAbsent( 1641 elementDescriptors.putIfAbsent(
1642 element, () => new Map<OutputUnit, ClassBuilder>()); 1642 element, () => new Map<OutputUnit, ClassBuilder>());
1643 return descriptors.putIfAbsent(outputUnit, 1643 return descriptors.putIfAbsent(outputUnit,
1644 () => new ClassBuilder(namer)); 1644 () => new ClassBuilder(namer));
1645 } 1645 }
1646 1646
1647 ClassBuilder getElementDecriptor(Element element) { 1647 ClassBuilder getElementDecriptor(Element element) {
1648 Element owner = element.getLibrary(); 1648 Element owner = element.library;
1649 if (!element.isTopLevel() && !element.isNative()) { 1649 if (!element.isTopLevel && !element.isNative) {
1650 // For static (not top level) elements, record their code in a buffer 1650 // For static (not top level) elements, record their code in a buffer
1651 // specific to the class. For now, not supported for native classes and 1651 // specific to the class. For now, not supported for native classes and
1652 // native elements. 1652 // native elements.
1653 ClassElement cls = 1653 ClassElement cls =
1654 element.getEnclosingClassOrCompilationUnit().declaration; 1654 element.enclosingClassOrCompilationUnit.declaration;
1655 if (compiler.codegenWorld.instantiatedClasses.contains(cls) 1655 if (compiler.codegenWorld.instantiatedClasses.contains(cls)
1656 && !cls.isNative()) { 1656 && !cls.isNative) {
1657 owner = cls; 1657 owner = cls;
1658 } 1658 }
1659 } 1659 }
1660 if (owner == null) { 1660 if (owner == null) {
1661 compiler.internalError(element, 'Owner is null.'); 1661 compiler.internalError(element, 'Owner is null.');
1662 } 1662 }
1663 return getElementDescriptorForOutputUnit(owner, 1663 return getElementDescriptorForOutputUnit(owner,
1664 compiler.deferredLoadTask.outputUnitForElement(element)); 1664 compiler.deferredLoadTask.outputUnitForElement(element));
1665 } 1665 }
1666 1666
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
1739 String sourceMap = sourceMapBuilder.build(); 1739 String sourceMap = sourceMapBuilder.build();
1740 compiler.outputProvider(name, 'js.map') 1740 compiler.outputProvider(name, 'js.map')
1741 ..add(sourceMap) 1741 ..add(sourceMap)
1742 ..close(); 1742 ..close();
1743 } 1743 }
1744 1744
1745 void registerReadTypeVariable(TypeVariableElement element) { 1745 void registerReadTypeVariable(TypeVariableElement element) {
1746 readTypeVariables.add(element); 1746 readTypeVariables.add(element);
1747 } 1747 }
1748 } 1748 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698