| 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 import 'package:kernel/ast.dart' as ir; | 5 import 'package:kernel/ast.dart' as ir; |
| 6 | 6 |
| 7 import '../common.dart'; | 7 import '../common.dart'; |
| 8 import '../common/names.dart'; | 8 import '../common/names.dart'; |
| 9 import '../core_types.dart'; | 9 import '../core_types.dart'; |
| 10 import '../elements/elements.dart'; | 10 import '../elements/elements.dart'; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 | 23 |
| 24 DartTypeConverter _typeConverter; | 24 DartTypeConverter _typeConverter; |
| 25 | 25 |
| 26 /// Library environment. Used for fast lookup. | 26 /// Library environment. Used for fast lookup. |
| 27 KEnv _env; | 27 KEnv _env; |
| 28 | 28 |
| 29 /// List of library environments by `KLibrary.libraryIndex`. This is used for | 29 /// List of library environments by `KLibrary.libraryIndex`. This is used for |
| 30 /// fast lookup into library classes and members. | 30 /// fast lookup into library classes and members. |
| 31 List<KLibraryEnv> _libraryEnvs = <KLibraryEnv>[]; | 31 List<KLibraryEnv> _libraryEnvs = <KLibraryEnv>[]; |
| 32 | 32 |
| 33 /// List of class environments by `KClass.classIndex`. This is used for |
| 34 /// fast lookup into class members. |
| 35 List<KClassEnv> _classEnvs = <KClassEnv>[]; |
| 36 |
| 33 Map<ir.Library, KLibrary> _libraryMap = <ir.Library, KLibrary>{}; | 37 Map<ir.Library, KLibrary> _libraryMap = <ir.Library, KLibrary>{}; |
| 34 Map<ir.Class, KClass> _classMap = <ir.Class, KClass>{}; | 38 Map<ir.Class, KClass> _classMap = <ir.Class, KClass>{}; |
| 35 Map<ir.TypeParameter, KTypeVariable> _typeVariableMap = | 39 Map<ir.TypeParameter, KTypeVariable> _typeVariableMap = |
| 36 <ir.TypeParameter, KTypeVariable>{}; | 40 <ir.TypeParameter, KTypeVariable>{}; |
| 37 Map<ir.Member, KConstructor> _constructorMap = <ir.Member, KConstructor>{}; | 41 Map<ir.Member, KConstructor> _constructorMap = <ir.Member, KConstructor>{}; |
| 38 Map<ir.Procedure, KFunction> _methodMap = <ir.Procedure, KFunction>{}; | 42 Map<ir.Procedure, KFunction> _methodMap = <ir.Procedure, KFunction>{}; |
| 39 Map<ir.Field, KField> _fieldMap = <ir.Field, KField>{}; | 43 Map<ir.Field, KField> _fieldMap = <ir.Field, KField>{}; |
| 40 Map<ir.TreeNode, KLocalFunction> _localFunctionMap = | 44 Map<ir.TreeNode, KLocalFunction> _localFunctionMap = |
| 41 <ir.TreeNode, KLocalFunction>{}; | 45 <ir.TreeNode, KLocalFunction>{}; |
| 42 | 46 |
| 43 KernelWorldBuilder(this.reporter, ir.Program program) | 47 KernelWorldBuilder(this.reporter, ir.Program program) |
| 44 : _env = new KEnv(program) { | 48 : _env = new KEnv(program) { |
| 45 _commonElements = new KernelCommonElements(this); | 49 _commonElements = new KernelCommonElements(this); |
| 46 _typeConverter = new DartTypeConverter(this); | 50 _typeConverter = new DartTypeConverter(this); |
| 47 } | 51 } |
| 48 | 52 |
| 49 CommonElements get commonElements => _commonElements; | 53 CommonElements get commonElements => _commonElements; |
| 50 | 54 |
| 51 LibraryEntity lookupLibrary(Uri uri) { | 55 LibraryEntity lookupLibrary(Uri uri) { |
| 52 KLibraryEnv libraryEnv = _env.lookupLibrary(uri); | 56 KLibraryEnv libraryEnv = _env.lookupLibrary(uri); |
| 53 return _getLibrary(libraryEnv.library, libraryEnv); | 57 return _getLibrary(libraryEnv.library, libraryEnv); |
| 54 } | 58 } |
| 55 | 59 |
| 56 KLibrary _getLibrary(ir.Library node, [KLibraryEnv libraryEnv]) { | 60 KLibrary _getLibrary(ir.Library node, [KLibraryEnv libraryEnv]) { |
| 57 return _libraryMap.putIfAbsent(node, () { | 61 return _libraryMap.putIfAbsent(node, () { |
| 58 _libraryEnvs.add(libraryEnv ?? _env.lookupLibrary(node.importUri)); | 62 _libraryEnvs.add(libraryEnv ?? _env.lookupLibrary(node.importUri)); |
| 59 return new KLibrary(_libraryMap.length, node.name, node.fileUri); | 63 return new KLibrary(_libraryMap.length, node.name ?? '${node.importUri}', |
| 64 node.name ?? node.fileUri ?? '${node.importUri}'); |
| 60 }); | 65 }); |
| 61 } | 66 } |
| 62 | 67 |
| 63 ClassEntity lookupClass(KLibrary library, String name) { | 68 ClassEntity lookupClass(KLibrary library, String name) { |
| 64 KLibraryEnv libraryEnv = _libraryEnvs[library.libraryIndex]; | 69 KLibraryEnv libraryEnv = _libraryEnvs[library.libraryIndex]; |
| 65 KClassEnv classEnv = libraryEnv.lookupClass(name); | 70 KClassEnv classEnv = libraryEnv.lookupClass(name); |
| 66 return _getClass(classEnv.cls, classEnv); | 71 return _getClass(classEnv.cls, classEnv); |
| 67 } | 72 } |
| 68 | 73 |
| 69 KClass _getClass(ir.Class node, [KClassEnv classEnv]) { | 74 KClass _getClass(ir.Class node, [KClassEnv classEnv]) { |
| 70 return _classMap.putIfAbsent(node, () { | 75 return _classMap.putIfAbsent(node, () { |
| 71 return new KClass(node.name); | 76 if (classEnv == null) { |
| 77 KLibrary library = _getLibrary(node.enclosingLibrary); |
| 78 classEnv = _libraryEnvs[library.libraryIndex].lookupClass(node.name); |
| 79 } |
| 80 _classEnvs.add(classEnv); |
| 81 return new KClass(_classMap.length, node.name); |
| 72 }); | 82 }); |
| 73 } | 83 } |
| 74 | 84 |
| 75 KTypeVariable _getTypeVariable(ir.TypeParameter node) { | 85 KTypeVariable _getTypeVariable(ir.TypeParameter node) { |
| 76 return _typeVariableMap.putIfAbsent(node, () { | 86 return _typeVariableMap.putIfAbsent(node, () { |
| 77 if (node.parent is ir.Class) { | 87 if (node.parent is ir.Class) { |
| 78 ir.Class cls = node.parent; | 88 ir.Class cls = node.parent; |
| 79 int index = cls.typeParameters.indexOf(node); | 89 int index = cls.typeParameters.indexOf(node); |
| 80 return new KTypeVariable(_getClass(cls), node.name, index); | 90 return new KTypeVariable(_getClass(cls), node.name, index); |
| 81 } | 91 } |
| (...skipping 384 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 466 @override | 476 @override |
| 467 DartType visitInvalidType(ir.InvalidType node) { | 477 DartType visitInvalidType(ir.InvalidType node) { |
| 468 if (topLevel) { | 478 if (topLevel) { |
| 469 throw new UnimplementedError( | 479 throw new UnimplementedError( |
| 470 "Outermost invalid types not currently supported"); | 480 "Outermost invalid types not currently supported"); |
| 471 } | 481 } |
| 472 // Nested invalid types are treated as `dynamic`. | 482 // Nested invalid types are treated as `dynamic`. |
| 473 return const DynamicType(); | 483 return const DynamicType(); |
| 474 } | 484 } |
| 475 } | 485 } |
| 486 |
| 487 // Interface for testing equivalence of Kernel-based entities. |
| 488 class WorldDeconstructionForTesting { |
| 489 final KernelWorldBuilder builder; |
| 490 |
| 491 WorldDeconstructionForTesting(this.builder); |
| 492 |
| 493 Uri getLibraryUri(KLibrary library) { |
| 494 return builder._libraryEnvs[library.libraryIndex].library.importUri; |
| 495 } |
| 496 |
| 497 KLibrary getLibraryForClass(KClass cls) { |
| 498 KClassEnv env = builder._classEnvs[cls.classIndex]; |
| 499 return builder.getLibrary(env.cls.enclosingLibrary); |
| 500 } |
| 501 |
| 502 KLibrary _getLibrary<E>(E member, Map<ir.Member, E> map) { |
| 503 ir.Library library; |
| 504 map.forEach((ir.Member node, E other) { |
| 505 if (library == null && member == other) { |
| 506 library = node.enclosingLibrary; |
| 507 } |
| 508 }); |
| 509 if (library == null) { |
| 510 throw new ArgumentError("No library found for $member"); |
| 511 } |
| 512 return builder._getLibrary(library); |
| 513 } |
| 514 |
| 515 KLibrary getLibraryForFunction(KFunction function) => |
| 516 _getLibrary(function, builder._methodMap); |
| 517 |
| 518 KLibrary getLibraryForField(KField field) => |
| 519 _getLibrary(field, builder._fieldMap); |
| 520 } |
| OLD | NEW |