Chromium Code Reviews| 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 failedAt(spannable, 'Too many name annotations.'); | |
|
Siggi Cherem (dart-lang)
2017/08/08 00:06:46
should this be a warning/error to report to the us
Johnni Winther
2017/08/09 08:34:36
It should. Adding a TODO.
| |
| 47 } | |
| 48 } | |
| 49 return annotationName; | |
| 50 } | |
| 51 | |
| 52 void checkFunctionParameters(FunctionEntity function) { | |
| 53 if (function.parameterStructure.namedParameters.isNotEmpty) { | |
| 54 elementMap.reporter.reportErrorMessage( | |
| 55 function, | |
| 56 MessageKind.JS_INTEROP_METHOD_WITH_NAMED_ARGUMENTS, | |
| 57 {'method': function.name}); | |
| 58 } | |
| 59 } | |
| 60 | |
| 37 void extractJsInteropAnnotations(LibraryEntity library) { | 61 void extractJsInteropAnnotations(LibraryEntity library) { |
| 38 // TODO(redemption): Implement this. | 62 DiagnosticReporter reporter = elementMap.reporter; |
| 63 ElementEnvironment elementEnvironment = elementMap.elementEnvironment; | |
| 64 CommonElements commonElements = elementMap.commonElements; | |
| 65 | |
| 66 // TODO(johnniwinther): Mark library directly when .dill supports library | |
|
Siggi Cherem (dart-lang)
2017/08/08 00:06:46
I wasn't sure I followed this withotu looking at t
Johnni Winther
2017/08/09 08:34:36
Done.
| |
| 67 // metadata. | |
| 68 bool isJsLibrary = false; | |
| 69 String libraryName = getJsInteropName( | |
| 70 library, elementEnvironment.getLibraryMetadata(library)); | |
| 71 if (libraryName != null) { | |
| 72 isJsLibrary = true; | |
| 73 } | |
| 74 elementEnvironment.forEachLibraryMember(library, (MemberEntity member) { | |
| 75 if (member.isField) return; | |
| 76 String memberName = getJsInteropName( | |
| 77 library, elementEnvironment.getMemberMetadata(member)); | |
| 78 if (memberName != null) { | |
| 79 _nativeBasicDataBuilder.markAsJsInteropMember(member, memberName); | |
| 80 checkFunctionParameters(member); | |
| 81 } | |
| 82 }); | |
| 83 | |
| 84 elementEnvironment.forEachClass(library, (ClassEntity cls) { | |
| 85 Iterable<ConstantValue> metadata = | |
| 86 elementEnvironment.getClassMetadata(cls); | |
| 87 String className = getJsInteropName(cls, metadata); | |
| 88 if (className != null) { | |
| 89 bool isAnonymous = false; | |
| 90 for (ConstantValue value in metadata) { | |
| 91 if (isAnnotation(cls, value, commonElements.jsAnonymousClass)) { | |
| 92 isAnonymous = true; | |
| 93 break; | |
| 94 } | |
| 95 } | |
| 96 _nativeBasicDataBuilder.markAsJsInteropClass(cls, | |
| 97 name: className, isAnonymous: isAnonymous); | |
| 98 // For now, assume the library is a js-interop library. | |
| 99 isJsLibrary = true; | |
| 100 | |
| 101 ClassEntity superclass = elementEnvironment.getSuperClass(cls); | |
| 102 if (superclass != commonElements.jsJavaScriptObjectClass) { | |
| 103 reporter.reportErrorMessage( | |
| 104 cls, | |
| 105 MessageKind.JS_INTEROP_CLASS_CANNOT_EXTEND_DART_CLASS, | |
| 106 {'cls': cls.name, 'superclass': superclass.name}); | |
| 107 } | |
| 108 | |
| 109 elementEnvironment.forEachClassMember(cls, | |
| 110 (ClassEntity declarer, MemberEntity member) { | |
| 111 if (declarer != cls) return; | |
| 112 if (member.isField) return; | |
| 113 FunctionEntity function = member; | |
| 114 | |
| 115 String memberName = getJsInteropName( | |
| 116 library, elementEnvironment.getMemberMetadata(function)); | |
| 117 if (memberName != null) { | |
| 118 _nativeBasicDataBuilder.markAsJsInteropMember(function, memberName); | |
| 119 } | |
| 120 | |
| 121 if (!function.isExternal && | |
| 122 !function.isAbstract && | |
| 123 !function.isConstructor && | |
| 124 !function.isStatic) { | |
| 125 reporter.reportErrorMessage( | |
| 126 function, | |
| 127 MessageKind.JS_INTEROP_CLASS_NON_EXTERNAL_MEMBER, | |
| 128 {'cls': cls.name, 'member': member.name}); | |
| 129 } | |
| 130 | |
| 131 if (function is ConstructorEntity && | |
| 132 function.isFactoryConstructor && | |
| 133 isAnonymous) { | |
| 134 if (function.parameterStructure.requiredParameters > 0) { | |
| 135 reporter.reportErrorMessage( | |
| 136 function, | |
| 137 MessageKind | |
| 138 .JS_OBJECT_LITERAL_CONSTRUCTOR_WITH_POSITIONAL_ARGUMENTS, | |
| 139 {'cls': cls.name}); | |
| 140 } | |
| 141 } else { | |
| 142 checkFunctionParameters(function); | |
| 143 } | |
| 144 }); | |
| 145 } | |
| 146 }); | |
| 147 if (isJsLibrary) { | |
| 148 // Assume the empty name. | |
| 149 libraryName ??= ''; | |
|
Siggi Cherem (dart-lang)
2017/08/08 00:06:46
I guess this line will also have a TODO delete?
Johnni Winther
2017/08/09 08:34:36
Done.
| |
| 150 _nativeBasicDataBuilder.markAsJsInteropLibrary(library, | |
| 151 name: libraryName); | |
| 152 } | |
| 39 } | 153 } |
| 40 | 154 |
| 41 @override | 155 @override |
| 42 void processJsInteropAnnotations( | 156 void processJsInteropAnnotations( |
| 43 NativeBasicData nativeData, NativeDataBuilder nativeDataBuilder) { | 157 NativeBasicData nativeBasicData, NativeDataBuilder nativeDataBuilder) { |
| 44 // TODO(redemption): Implement this. | 158 // Nothing to do; all is computed in [extractJsInteropAnnotations]. |
| 45 } | 159 } |
| 46 } | 160 } |
| OLD | NEW |