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

Side by Side Diff: pkg/compiler/lib/src/compiler.dart

Issue 2654293002: Extract reusable parts of _CompilerCommonElements in a CommonElementsMixin (Closed)
Patch Set: Cleanup Created 3 years, 10 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
« no previous file with comments | « no previous file | pkg/compiler/lib/src/core_types.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 library dart2js.compiler_base; 5 library dart2js.compiler_base;
6 6
7 import 'dart:async' show EventSink, Future; 7 import 'dart:async' show EventSink, Future;
8 8
9 import '../compiler_new.dart' as api; 9 import '../compiler_new.dart' as api;
10 import 'cache_strategy.dart' show CacheStrategy; 10 import 'cache_strategy.dart' show CacheStrategy;
11 import 'closure.dart' as closureMapping show ClosureTask; 11 import 'closure.dart' as closureMapping show ClosureTask;
12 import 'common/backend_api.dart' show Backend; 12 import 'common/backend_api.dart' show Backend;
13 import 'common/names.dart' show Selectors; 13 import 'common/names.dart' show Selectors;
14 import 'common/names.dart' show Identifiers, Uris; 14 import 'common/names.dart' show Identifiers, Uris;
15 import 'common/resolution.dart' 15 import 'common/resolution.dart'
16 show 16 show
17 ParsingContext, 17 ParsingContext,
18 Resolution, 18 Resolution,
19 ResolutionWorkItem, 19 ResolutionWorkItem,
20 ResolutionImpact, 20 ResolutionImpact,
21 Target; 21 Target;
22 import 'common/tasks.dart' show CompilerTask, GenericTask, Measurer; 22 import 'common/tasks.dart' show CompilerTask, GenericTask, Measurer;
23 import 'common/work.dart' show WorkItem; 23 import 'common/work.dart' show WorkItem;
24 import 'common.dart'; 24 import 'common.dart';
25 import 'compile_time_constants.dart'; 25 import 'compile_time_constants.dart';
26 import 'constants/values.dart'; 26 import 'constants/values.dart';
27 import 'core_types.dart' show CommonElements; 27 import 'core_types.dart' show CommonElements, CommonElementsMixin;
28 import 'elements/resolution_types.dart' 28 import 'elements/resolution_types.dart'
29 show 29 show
30 ResolutionDartType, 30 ResolutionDartType,
31 ResolutionDynamicType, 31 ResolutionDynamicType,
32 ResolutionInterfaceType, 32 ResolutionInterfaceType,
33 Types; 33 Types;
34 import 'deferred_load.dart' show DeferredLoadTask; 34 import 'deferred_load.dart' show DeferredLoadTask;
35 import 'diagnostics/code_location.dart'; 35 import 'diagnostics/code_location.dart';
36 import 'diagnostics/diagnostic_listener.dart' show DiagnosticReporter; 36 import 'diagnostics/diagnostic_listener.dart' show DiagnosticReporter;
37 import 'diagnostics/invariant.dart' show REPORT_EXCESS_RESOLUTION; 37 import 'diagnostics/invariant.dart' show REPORT_EXCESS_RESOLUTION;
(...skipping 1078 matching lines...) Expand 10 before | Expand all | Expand 10 after
1116 return userOutputProvider.createEventSink(name, extension); 1116 return userOutputProvider.createEventSink(name, extension);
1117 } 1117 }
1118 } 1118 }
1119 1119
1120 /// Information about suppressed warnings and hints for a given library. 1120 /// Information about suppressed warnings and hints for a given library.
1121 class SuppressionInfo { 1121 class SuppressionInfo {
1122 int warnings = 0; 1122 int warnings = 0;
1123 int hints = 0; 1123 int hints = 0;
1124 } 1124 }
1125 1125
1126 class _CompilerCommonElements implements CommonElements { 1126 class _CompilerCommonElements extends CommonElementsMixin {
1127 final Resolution resolution; 1127 final Resolution resolution;
1128 final DiagnosticReporter reporter; 1128 final DiagnosticReporter reporter;
1129 1129
1130 LibraryElement coreLibrary; 1130 LibraryElement coreLibrary;
1131 LibraryElement asyncLibrary; 1131 LibraryElement asyncLibrary;
1132 LibraryElement mirrorsLibrary; 1132 LibraryElement mirrorsLibrary;
1133 LibraryElement typedDataLibrary; 1133 LibraryElement typedDataLibrary;
1134 1134
1135 // TODO(sigmund): possibly move this to target-specific collection of 1135 // TODO(sigmund): possibly move this to target-specific collection of
1136 // elements, or refactor the library so that the helpers we need are in a 1136 // elements, or refactor the library so that the helpers we need are in a
1137 // target-agnostic place. Currently we are using @patch and @Native from 1137 // target-agnostic place. Currently we are using @patch and @Native from
1138 // here. We hope we can make those independent of the backend and generic 1138 // here. We hope we can make those independent of the backend and generic
1139 // enough so the patching algorithm can work without being configured for a 1139 // enough so the patching algorithm can work without being configured for a
1140 // specific backend. 1140 // specific backend.
1141 LibraryElement jsHelperLibrary; 1141 LibraryElement jsHelperLibrary;
1142 1142
1143 _CompilerCommonElements(this.resolution, this.reporter); 1143 _CompilerCommonElements(this.resolution, this.reporter);
1144 1144
1145 // From dart:core
1146
1147 ClassElement _objectClass;
1148 ClassElement get objectClass =>
1149 _objectClass ??= _findRequired(coreLibrary, 'Object');
1150
1151 ClassElement _boolClass;
1152 ClassElement get boolClass =>
1153 _boolClass ??= _findRequired(coreLibrary, 'bool');
1154
1155 ClassElement _numClass;
1156 ClassElement get numClass => _numClass ??= _findRequired(coreLibrary, 'num');
1157
1158 ClassElement _intClass;
1159 ClassElement get intClass => _intClass ??= _findRequired(coreLibrary, 'int');
1160
1161 ClassElement _doubleClass;
1162 ClassElement get doubleClass =>
1163 _doubleClass ??= _findRequired(coreLibrary, 'double');
1164
1165 ClassElement _stringClass;
1166 ClassElement get stringClass =>
1167 _stringClass ??= _findRequired(coreLibrary, 'String');
1168
1169 ClassElement _functionClass;
1170 ClassElement get functionClass =>
1171 _functionClass ??= _findRequired(coreLibrary, 'Function');
1172
1173 MethodElement _functionApplyMethod;
1174 MethodElement get functionApplyMethod {
1175 if (_functionApplyMethod == null) {
1176 functionClass.ensureResolved(resolution);
1177 _functionApplyMethod = functionClass.lookupLocalMember('apply');
1178 assert(invariant(functionClass, _functionApplyMethod != null,
1179 message: "Member `apply` not found in ${functionClass}."));
1180 }
1181 return _functionApplyMethod;
1182 }
1183
1184 bool isFunctionApplyMethod(MemberElement element) =>
1185 element.name == 'apply' && element.enclosingClass == functionClass;
1186
1187 ClassElement _nullClass;
1188 ClassElement get nullClass =>
1189 _nullClass ??= _findRequired(coreLibrary, 'Null');
1190
1191 ClassElement _listClass;
1192 ClassElement get listClass =>
1193 _listClass ??= _findRequired(coreLibrary, 'List');
1194
1195 ClassElement _typeClass;
1196 ClassElement get typeClass =>
1197 _typeClass ??= _findRequired(coreLibrary, 'Type');
1198
1199 ClassElement _mapClass;
1200 ClassElement get mapClass => _mapClass ??= _findRequired(coreLibrary, 'Map');
1201
1202 ClassElement _symbolClass;
1203 ClassElement get symbolClass =>
1204 _symbolClass ??= _findRequired(coreLibrary, 'Symbol');
1205
1206 ConstructorElement _symbolConstructor;
1207 ConstructorElement get symbolConstructor {
1208 if (_symbolConstructor == null) {
1209 symbolClass.ensureResolved(resolution);
1210 _symbolConstructor = symbolClass.lookupConstructor('');
1211 assert(invariant(symbolClass, _symbolConstructor != null,
1212 message: "Default constructor not found ${symbolClass}."));
1213 }
1214 return _symbolConstructor;
1215 }
1216
1217 bool isSymbolConstructor(Element e) =>
1218 e.enclosingClass == symbolClass && e == symbolConstructor;
1219
1220 ClassElement _stackTraceClass;
1221 ClassElement get stackTraceClass =>
1222 _stackTraceClass ??= _findRequired(coreLibrary, 'StackTrace');
1223
1224 ClassElement _iterableClass;
1225 ClassElement get iterableClass =>
1226 _iterableClass ??= _findRequired(coreLibrary, 'Iterable');
1227
1228 ClassElement _resourceClass;
1229 ClassElement get resourceClass =>
1230 _resourceClass ??= _findRequired(coreLibrary, 'Resource');
1231
1232 MethodElement _identicalFunction;
1233 MethodElement get identicalFunction =>
1234 _identicalFunction ??= coreLibrary.find('identical');
1235
1236 // From dart:async
1237
1238 ClassElement _futureClass;
1239 ClassElement get futureClass =>
1240 _futureClass ??= _findRequired(asyncLibrary, 'Future');
1241
1242 ClassElement _streamClass;
1243 ClassElement get streamClass =>
1244 _streamClass ??= _findRequired(asyncLibrary, 'Stream');
1245
1246 ClassElement _deferredLibraryClass;
1247 ClassElement get deferredLibraryClass =>
1248 _deferredLibraryClass ??= _findRequired(asyncLibrary, "DeferredLibrary");
1249
1250 // From dart:mirrors
1251
1252 ClassElement _mirrorSystemClass;
1253 ClassElement get mirrorSystemClass =>
1254 _mirrorSystemClass ??= _findRequired(mirrorsLibrary, 'MirrorSystem');
1255
1256 FunctionElement _mirrorSystemGetNameFunction;
1257 bool isMirrorSystemGetNameFunction(MemberElement element) {
1258 if (_mirrorSystemGetNameFunction == null) {
1259 if (!element.isFunction || mirrorsLibrary == null) return false;
1260 ClassElement cls = mirrorSystemClass;
1261 if (element.enclosingClass != cls) return false;
1262 if (cls != null) {
1263 cls.ensureResolved(resolution);
1264 _mirrorSystemGetNameFunction = cls.lookupLocalMember('getName');
1265 }
1266 }
1267 return element == _mirrorSystemGetNameFunction;
1268 }
1269
1270 ClassElement _mirrorsUsedClass;
1271 ClassElement get mirrorsUsedClass =>
1272 _mirrorsUsedClass ??= _findRequired(mirrorsLibrary, 'MirrorsUsed');
1273
1274 bool isMirrorsUsedConstructor(ConstructorElement element) =>
1275 mirrorsLibrary != null && mirrorsUsedClass == element.enclosingClass;
1276
1277 ConstructorElement _mirrorsUsedConstructor;
1278 @override
1279 ConstructorElement get mirrorsUsedConstructor {
1280 if (_mirrorsUsedConstructor == null) {
1281 ClassElement cls = mirrorsUsedClass;
1282 if (cls != null) {
1283 cls.ensureResolved(resolution);
1284 _mirrorsUsedConstructor = cls.constructors.head;
1285 }
1286 }
1287 return _mirrorsUsedConstructor;
1288 }
1289
1290 // From dart:typed_data
1291
1292 ClassElement _typedDataClass;
1293 ClassElement get typedDataClass =>
1294 _typedDataClass ??= _findRequired(typedDataLibrary, 'NativeTypedData');
1295
1296 // From dart:_js_helper 1145 // From dart:_js_helper
1297 // TODO(sigmund,johnniwinther): refactor needed: either these move to a 1146 // TODO(sigmund,johnniwinther): refactor needed: either these move to a
1298 // backend-specific collection of helpers, or the helper code moves to a 1147 // backend-specific collection of helpers, or the helper code moves to a
1299 // backend agnostic library (see commend above on [jsHelperLibrary]. 1148 // backend agnostic library (see commend above on [jsHelperLibrary].
1300 1149
1301 ClassElement _patchAnnotationClass; 1150 ClassElement _patchAnnotationClass;
1302 ClassElement get patchAnnotationClass => 1151 ClassElement get patchAnnotationClass =>
1303 _patchAnnotationClass ??= _findRequired(jsHelperLibrary, '_Patch'); 1152 _patchAnnotationClass ??= _findLibraryMember(jsHelperLibrary, '_Patch');
1304 1153
1305 ClassElement _nativeAnnotationClass; 1154 ClassElement _nativeAnnotationClass;
1306 ClassElement get nativeAnnotationClass => 1155 ClassElement get nativeAnnotationClass =>
1307 _nativeAnnotationClass ??= _findRequired(jsHelperLibrary, 'Native'); 1156 _nativeAnnotationClass ??= _findLibraryMember(jsHelperLibrary, 'Native');
1308 1157
1309 @override 1158 @override
1310 ResolutionDynamicType get dynamicType => const ResolutionDynamicType(); 1159 ResolutionDynamicType get dynamicType => const ResolutionDynamicType();
1311 1160
1312 @override
1313 ResolutionInterfaceType get objectType {
1314 objectClass.ensureResolved(resolution);
1315 return objectClass.rawType;
1316 }
1317
1318 @override
1319 ResolutionInterfaceType get boolType {
1320 boolClass.ensureResolved(resolution);
1321 return boolClass.rawType;
1322 }
1323
1324 @override
1325 ResolutionInterfaceType get doubleType {
1326 doubleClass.ensureResolved(resolution);
1327 return doubleClass.rawType;
1328 }
1329
1330 @override
1331 ResolutionInterfaceType get functionType {
1332 functionClass.ensureResolved(resolution);
1333 return functionClass.rawType;
1334 }
1335
1336 @override
1337 ResolutionInterfaceType get intType {
1338 intClass.ensureResolved(resolution);
1339 return intClass.rawType;
1340 }
1341
1342 @override
1343 ResolutionInterfaceType get resourceType {
1344 resourceClass.ensureResolved(resolution);
1345 return resourceClass.rawType;
1346 }
1347
1348 @override
1349 ResolutionInterfaceType listType([ResolutionDartType elementType]) {
1350 listClass.ensureResolved(resolution);
1351 ResolutionInterfaceType type = listClass.rawType;
1352 if (elementType == null) {
1353 return type;
1354 }
1355 return type.createInstantiation([elementType]);
1356 }
1357
1358 @override
1359 ResolutionInterfaceType mapType(
1360 [ResolutionDartType keyType, ResolutionDartType valueType]) {
1361 mapClass.ensureResolved(resolution);
1362 ResolutionInterfaceType type = mapClass.rawType;
1363 if (keyType == null && valueType == null) {
1364 return type;
1365 } else if (keyType == null) {
1366 keyType = const ResolutionDynamicType();
1367 } else if (valueType == null) {
1368 valueType = const ResolutionDynamicType();
1369 }
1370 return type.createInstantiation([keyType, valueType]);
1371 }
1372
1373 @override
1374 ResolutionInterfaceType get nullType {
1375 nullClass.ensureResolved(resolution);
1376 return nullClass.rawType;
1377 }
1378
1379 @override
1380 ResolutionInterfaceType get numType {
1381 numClass.ensureResolved(resolution);
1382 return numClass.rawType;
1383 }
1384
1385 @override
1386 ResolutionInterfaceType get stringType {
1387 stringClass.ensureResolved(resolution);
1388 return stringClass.rawType;
1389 }
1390
1391 @override
1392 ResolutionInterfaceType get symbolType {
1393 symbolClass.ensureResolved(resolution);
1394 return symbolClass.rawType;
1395 }
1396
1397 @override
1398 ResolutionInterfaceType get typeType {
1399 typeClass.ensureResolved(resolution);
1400 return typeClass.rawType;
1401 }
1402
1403 @override
1404 ResolutionInterfaceType get stackTraceType {
1405 stackTraceClass.ensureResolved(resolution);
1406 return stackTraceClass.rawType;
1407 }
1408
1409 @override
1410 ResolutionInterfaceType iterableType([ResolutionDartType elementType]) {
1411 iterableClass.ensureResolved(resolution);
1412 ResolutionInterfaceType type = iterableClass.rawType;
1413 if (elementType == null) {
1414 return type;
1415 }
1416 return type.createInstantiation([elementType]);
1417 }
1418
1419 @override
1420 ResolutionInterfaceType futureType([ResolutionDartType elementType]) {
1421 futureClass.ensureResolved(resolution);
1422 ResolutionInterfaceType type = futureClass.rawType;
1423 if (elementType == null) {
1424 return type;
1425 }
1426 return type.createInstantiation([elementType]);
1427 }
1428
1429 @override
1430 ResolutionInterfaceType streamType([ResolutionDartType elementType]) {
1431 streamClass.ensureResolved(resolution);
1432 ResolutionInterfaceType type = streamClass.rawType;
1433 if (elementType == null) {
1434 return type;
1435 }
1436 return type.createInstantiation([elementType]);
1437 }
1438
1439 void onLibraryCreated(LibraryElement library) { 1161 void onLibraryCreated(LibraryElement library) {
1440 Uri uri = library.canonicalUri; 1162 Uri uri = library.canonicalUri;
1441 if (uri == Uris.dart_core) { 1163 if (uri == Uris.dart_core) {
1442 coreLibrary = library; 1164 coreLibrary = library;
1443 } else if (uri == Uris.dart_async) { 1165 } else if (uri == Uris.dart_async) {
1444 asyncLibrary = library; 1166 asyncLibrary = library;
1445 } else if (uri == Uris.dart__native_typed_data) { 1167 } else if (uri == Uris.dart__native_typed_data) {
1446 typedDataLibrary = library; 1168 typedDataLibrary = library;
1447 } else if (uri == Uris.dart_mirrors) { 1169 } else if (uri == Uris.dart_mirrors) {
1448 mirrorsLibrary = library; 1170 mirrorsLibrary = library;
1449 } else if (uri == js_backend.BackendHelpers.DART_JS_HELPER) { 1171 } else if (uri == js_backend.BackendHelpers.DART_JS_HELPER) {
1450 jsHelperLibrary = library; 1172 jsHelperLibrary = library;
1451 } 1173 }
1452 } 1174 }
1453 1175
1454 Element _findRequired(LibraryElement library, String name) { 1176 @override
1177 MemberElement findLibraryMember(LibraryElement library, String name,
1178 {bool required: true}) {
1179 return _findLibraryMember(library, name, required: required);
1180 }
1181
1182 @override
1183 MemberElement findClassMember(ClassElement cls, String name,
1184 {bool required: true}) {
1185 cls.ensureResolved(resolution);
1186 MemberElement member = cls.lookupLocalMember(name);
1187 if (member == null && required) {
1188 reporter.internalError(
1189 cls,
1190 "The class '${cls}' in '${cls.library.canonicalUri}' does not "
1191 "contain required member: '$name'.");
1192 }
1193 return member;
1194 }
1195
1196 @override
1197 ConstructorElement findConstructor(ClassElement cls, String name,
1198 {bool required: true}) {
1199 cls.ensureResolved(resolution);
1200 ConstructorElement constructor = cls.lookupConstructor(name);
1201 if (constructor == null && required) {
1202 reporter.internalError(
1203 cls,
1204 "The class '${cls}' in '${cls.library.canonicalUri}' does not "
1205 "contain required constructor: '$name'.");
1206 }
1207 return constructor;
1208 }
1209
1210 @override
1211 ClassElement findClass(LibraryElement library, String name,
1212 {bool required: true}) {
1213 return _findLibraryMember(library, name, required: required);
1214 }
1215
1216 Element _findRequired(LibraryElement library, String name) => _findLibraryMemb er(library, name);
1217
1218 Element _findLibraryMember(LibraryElement library, String name,
1219 {bool required: true}) {
1455 // If the script of the library is synthesized, the library does not exist 1220 // If the script of the library is synthesized, the library does not exist
1456 // and we do not try to load the helpers. 1221 // and we do not try to load the helpers.
1457 // 1222 //
1458 // This could for example happen if dart:async is disabled, then loading it 1223 // This could for example happen if dart:async is disabled, then loading it
1459 // should not try to find the given element. 1224 // should not try to find the given element.
1460 if (library == null || library.isSynthesized) return null; 1225 if (library == null || library.isSynthesized) return null;
1461 1226
1462 var element = library.find(name); 1227 Element element = library.find(name);
1463 if (element == null) { 1228 if (element == null && required) {
1464 reporter.internalError( 1229 reporter.internalError(
1465 library, 1230 library,
1466 "The library '${library.canonicalUri}' does not contain required " 1231 "The library '${library.canonicalUri}' does not contain required "
1467 "element: '$name'."); 1232 "element: '$name'.");
1468 } 1233 }
1469 return element; 1234 return element;
1470 } 1235 }
1471 1236
1472 ConstructorElement _unnamedListConstructor; 1237 @override
1473 ConstructorElement get unnamedListConstructor => 1238 ResolutionInterfaceType createInterfaceType(
1474 _unnamedListConstructor ??= listClass.lookupDefaultConstructor(); 1239 ClassElement cls, List<ResolutionDartType> typeArguments) {
1475 1240 cls.ensureResolved(resolution);
1476 ConstructorElement _filledListConstructor; 1241 return new ResolutionInterfaceType(cls, typeArguments);
1477 ConstructorElement get filledListConstructor =>
1478 _filledListConstructor ??= listClass.lookupConstructor("filled");
1479
1480 // TODO(johnniwinther): Change types to `ClassElement` when these are not
1481 // called with unrelated elements.
1482 bool isNumberOrStringSupertype(/*Class*/ Entity element) {
1483 return element == coreLibrary.find('Comparable');
1484 } 1242 }
1485 1243
1486 bool isStringOnlySupertype(/*Class*/ Entity element) { 1244 @override
1487 return element == coreLibrary.find('Pattern'); 1245 ResolutionInterfaceType getRawType(ClassElement cls) {
1246 cls.ensureResolved(resolution);
1247 return cls.rawType;
1488 } 1248 }
1489
1490 bool isListSupertype(/*Class*/ Entity element) => element == iterableClass;
1491 } 1249 }
1492 1250
1493 class CompilerDiagnosticReporter extends DiagnosticReporter { 1251 class CompilerDiagnosticReporter extends DiagnosticReporter {
1494 final Compiler compiler; 1252 final Compiler compiler;
1495 final DiagnosticOptions options; 1253 final DiagnosticOptions options;
1496 1254
1497 Element _currentElement; 1255 Element _currentElement;
1498 bool hasCrashed = false; 1256 bool hasCrashed = false;
1499 1257
1500 /// `true` if the last diagnostic was filtered, in which case the 1258 /// `true` if the last diagnostic was filtered, in which case the
(...skipping 726 matching lines...) Expand 10 before | Expand all | Expand 10 after
2227 _ElementScanner(this.scanner); 1985 _ElementScanner(this.scanner);
2228 void scanLibrary(LibraryElement library) => scanner.scanLibrary(library); 1986 void scanLibrary(LibraryElement library) => scanner.scanLibrary(library);
2229 void scanUnit(CompilationUnitElement unit) => scanner.scan(unit); 1987 void scanUnit(CompilationUnitElement unit) => scanner.scan(unit);
2230 } 1988 }
2231 1989
2232 class _EmptyEnvironment implements Environment { 1990 class _EmptyEnvironment implements Environment {
2233 const _EmptyEnvironment(); 1991 const _EmptyEnvironment();
2234 1992
2235 String valueOf(String key) => null; 1993 String valueOf(String key) => null;
2236 } 1994 }
OLDNEW
« no previous file with comments | « no previous file | pkg/compiler/lib/src/core_types.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698