| OLD | NEW |
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, 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 // TODO(johnniwinther): Make this a separate library. | 5 // TODO(johnniwinther): Make this a separate library. |
| 6 part of dart2js.kernel.element_map; | 6 part of dart2js.kernel.element_map; |
| 7 | 7 |
| 8 class KernelAnnotationProcessor implements AnnotationProcessor { | 8 class KernelAnnotationProcessor implements AnnotationProcessor { |
| 9 final KernelToElementMapForImpactImpl elementMap; | 9 final KernelToElementMapForImpactImpl elementMap; |
| 10 final NativeBasicDataBuilder _nativeBasicDataBuilder; | 10 final NativeBasicDataBuilder _nativeBasicDataBuilder; |
| 11 | 11 |
| 12 KernelAnnotationProcessor(this.elementMap, this._nativeBasicDataBuilder); | 12 KernelAnnotationProcessor(this.elementMap, this._nativeBasicDataBuilder); |
| 13 | 13 |
| 14 void extractNativeAnnotations(LibraryEntity library) { | 14 void extractNativeAnnotations(LibraryEntity library) { |
| 15 ElementEnvironment elementEnvironment = elementMap.elementEnvironment; | 15 ElementEnvironment elementEnvironment = elementMap.elementEnvironment; |
| 16 CommonElements commonElements = elementMap.commonElements; | 16 CommonElements commonElements = elementMap.commonElements; |
| 17 | 17 |
| 18 elementEnvironment.forEachClass(library, (ClassEntity cls) { | 18 elementEnvironment.forEachClass(library, (ClassEntity cls) { |
| 19 String annotationName; | 19 String annotationName; |
| 20 // TODO(johnniwinther): Make [_getClassMetadata] public and at test to | 20 for (ConstantValue value in elementEnvironment.getClassMetadata(cls)) { |
| 21 // guard against misuse. | |
| 22 for (ConstantValue value in elementMap._getClassMetadata(cls)) { | |
| 23 String name = readAnnotationName( | 21 String name = readAnnotationName( |
| 24 cls, value, commonElements.nativeAnnotationClass); | 22 cls, value, commonElements.nativeAnnotationClass); |
| 25 if (annotationName == null) { | 23 if (annotationName == null) { |
| 26 annotationName = name; | 24 annotationName = name; |
| 27 } else if (name != null) { | 25 } else if (name != null) { |
| 28 failedAt(cls, 'Too many name annotations.'); | 26 failedAt(cls, 'Too many name annotations.'); |
| 29 } | 27 } |
| 30 } | 28 } |
| 31 if (annotationName != null) { | 29 if (annotationName != null) { |
| 32 _nativeBasicDataBuilder.setNativeClassTagInfo(cls, annotationName); | 30 _nativeBasicDataBuilder.setNativeClassTagInfo(cls, annotationName); |
| 33 } | 31 } |
| 34 }); | 32 }); |
| 35 } | 33 } |
| 36 | 34 |
| 35 String getJsInteropName( |
| 36 Spannable spannable, Iterable<ConstantValue> metadata) { |
| 37 CommonElements commonElements = elementMap.commonElements; |
| 38 String annotationName; |
| 39 for (ConstantValue value in metadata) { |
| 40 String name = readAnnotationName( |
| 41 spannable, value, commonElements.jsAnnotationClass, |
| 42 defaultValue: ''); |
| 43 if (annotationName == null) { |
| 44 annotationName = name; |
| 45 } else if (name != null) { |
| 46 // TODO(johnniwinther): This should be an error, not a crash. |
| 47 failedAt(spannable, 'Too many name annotations.'); |
| 48 } |
| 49 } |
| 50 return annotationName; |
| 51 } |
| 52 |
| 53 void checkFunctionParameters(FunctionEntity function) { |
| 54 if (function.parameterStructure.namedParameters.isNotEmpty) { |
| 55 elementMap.reporter.reportErrorMessage( |
| 56 function, |
| 57 MessageKind.JS_INTEROP_METHOD_WITH_NAMED_ARGUMENTS, |
| 58 {'method': function.name}); |
| 59 } |
| 60 } |
| 61 |
| 37 void extractJsInteropAnnotations(LibraryEntity library) { | 62 void extractJsInteropAnnotations(LibraryEntity library) { |
| 38 // TODO(redemption): Implement this. | 63 DiagnosticReporter reporter = elementMap.reporter; |
| 64 ElementEnvironment elementEnvironment = elementMap.elementEnvironment; |
| 65 CommonElements commonElements = elementMap.commonElements; |
| 66 |
| 67 String libraryName = getJsInteropName( |
| 68 library, elementEnvironment.getLibraryMetadata(library)); |
| 69 bool isJsLibrary = libraryName != null; |
| 70 |
| 71 elementEnvironment.forEachLibraryMember(library, (MemberEntity member) { |
| 72 if (member.isField) return; |
| 73 String memberName = getJsInteropName( |
| 74 library, elementEnvironment.getMemberMetadata(member)); |
| 75 if (memberName != null) { |
| 76 _nativeBasicDataBuilder.markAsJsInteropMember(member, memberName); |
| 77 checkFunctionParameters(member); |
| 78 } |
| 79 }); |
| 80 |
| 81 elementEnvironment.forEachClass(library, (ClassEntity cls) { |
| 82 Iterable<ConstantValue> metadata = |
| 83 elementEnvironment.getClassMetadata(cls); |
| 84 String className = getJsInteropName(cls, metadata); |
| 85 if (className != null) { |
| 86 bool isAnonymous = false; |
| 87 for (ConstantValue value in metadata) { |
| 88 if (isAnnotation(cls, value, commonElements.jsAnonymousClass)) { |
| 89 isAnonymous = true; |
| 90 break; |
| 91 } |
| 92 } |
| 93 // TODO(johnniwinther): Report an error if the class is anonymous but |
| 94 // has a non-empty name. |
| 95 _nativeBasicDataBuilder.markAsJsInteropClass(cls, |
| 96 name: className, isAnonymous: isAnonymous); |
| 97 // TODO(johnniwinther): When fasta supports library metadata, report |
| 98 // and error if [isJsLibrary] is false. |
| 99 // For now, assume the library is a js-interop library. |
| 100 isJsLibrary = true; |
| 101 |
| 102 ClassEntity superclass = elementEnvironment.getSuperClass(cls); |
| 103 if (superclass != commonElements.jsJavaScriptObjectClass) { |
| 104 reporter.reportErrorMessage( |
| 105 cls, |
| 106 MessageKind.JS_INTEROP_CLASS_CANNOT_EXTEND_DART_CLASS, |
| 107 {'cls': cls.name, 'superclass': superclass.name}); |
| 108 } |
| 109 |
| 110 elementEnvironment.forEachClassMember(cls, |
| 111 (ClassEntity declarer, MemberEntity member) { |
| 112 if (declarer != cls) return; |
| 113 if (member.isField) return; |
| 114 FunctionEntity function = member; |
| 115 |
| 116 String memberName = getJsInteropName( |
| 117 library, elementEnvironment.getMemberMetadata(function)); |
| 118 if (memberName != null) { |
| 119 _nativeBasicDataBuilder.markAsJsInteropMember(function, memberName); |
| 120 } |
| 121 |
| 122 if (!function.isExternal && |
| 123 !function.isAbstract && |
| 124 !function.isConstructor && |
| 125 !function.isStatic) { |
| 126 reporter.reportErrorMessage( |
| 127 function, |
| 128 MessageKind.JS_INTEROP_CLASS_NON_EXTERNAL_MEMBER, |
| 129 {'cls': cls.name, 'member': member.name}); |
| 130 } |
| 131 |
| 132 if (function is ConstructorEntity && |
| 133 function.isFactoryConstructor && |
| 134 isAnonymous) { |
| 135 if (function.parameterStructure.requiredParameters > 0) { |
| 136 reporter.reportErrorMessage( |
| 137 function, |
| 138 MessageKind |
| 139 .JS_OBJECT_LITERAL_CONSTRUCTOR_WITH_POSITIONAL_ARGUMENTS, |
| 140 {'cls': cls.name}); |
| 141 } |
| 142 } else { |
| 143 checkFunctionParameters(function); |
| 144 } |
| 145 }); |
| 146 } |
| 147 }); |
| 148 if (isJsLibrary) { |
| 149 // TODO(johnniwinther): Remove this when fasta supports library metadata. |
| 150 // For now, assume the empty name. |
| 151 libraryName ??= ''; |
| 152 _nativeBasicDataBuilder.markAsJsInteropLibrary(library, |
| 153 name: libraryName); |
| 154 } |
| 39 } | 155 } |
| 40 | 156 |
| 41 @override | 157 @override |
| 42 void processJsInteropAnnotations( | 158 void processJsInteropAnnotations( |
| 43 NativeBasicData nativeData, NativeDataBuilder nativeDataBuilder) { | 159 NativeBasicData nativeBasicData, NativeDataBuilder nativeDataBuilder) { |
| 44 // TODO(redemption): Implement this. | 160 // Nothing to do; all is computed in [extractJsInteropAnnotations]. |
| 45 } | 161 } |
| 46 } | 162 } |
| OLD | NEW |