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 library dart2js.kernel.env; | 5 library dart2js.kernel.env; |
6 | 6 |
7 import 'package:kernel/ast.dart' as ir; | 7 import 'package:kernel/ast.dart' as ir; |
8 import 'package:kernel/clone.dart'; | 8 import 'package:kernel/clone.dart'; |
9 import 'package:kernel/type_algebra.dart'; | 9 import 'package:kernel/type_algebra.dart'; |
10 | 10 |
11 import '../common.dart'; | 11 import '../common.dart'; |
12 import '../common/resolution.dart'; | 12 import '../common/resolution.dart'; |
13 import '../constants/constructors.dart'; | 13 import '../constants/constructors.dart'; |
14 import '../constants/expressions.dart'; | 14 import '../constants/expressions.dart'; |
15 import '../constants/values.dart'; | 15 import '../constants/values.dart'; |
16 import '../elements/entities.dart'; | 16 import '../elements/entities.dart'; |
17 import '../elements/types.dart'; | 17 import '../elements/types.dart'; |
18 import '../ordered_typeset.dart'; | 18 import '../ordered_typeset.dart'; |
19 import '../ssa/kernel_impact.dart'; | 19 import '../ssa/kernel_impact.dart'; |
20 import 'element_map.dart'; | 20 import 'element_map.dart'; |
21 import 'element_map_impl.dart'; | 21 import 'element_map_impl.dart'; |
22 import 'element_map_mixins.dart'; | 22 import 'element_map_mixins.dart'; |
23 | 23 |
24 /// Environment for fast lookup of program libraries. | 24 /// Environment for fast lookup of program libraries. |
25 class ProgramEnv { | 25 class ProgramEnv { |
26 final Set<ir.Program> programs = new Set<ir.Program>(); | 26 final Set<ir.Program> _programs = new Set<ir.Program>(); |
27 | 27 |
28 Map<Uri, LibraryEnv> _libraryMap; | 28 Map<Uri, LibraryEnv> _libraryMap; |
29 | 29 |
30 /// TODO(johnniwinther): Handle arbitrary load order if needed. | 30 /// TODO(johnniwinther): Handle arbitrary load order if needed. |
31 ir.Member get mainMethod => programs.first?.mainMethod; | 31 ir.Member get mainMethod => _programs.first?.mainMethod; |
32 | 32 |
33 void addProgram(ir.Program program) { | 33 void addProgram(ir.Program program) { |
34 if (programs.add(program)) { | 34 if (_programs.add(program)) { |
35 if (_libraryMap != null) { | 35 if (_libraryMap != null) { |
36 _addLibraries(program); | 36 _addLibraries(program); |
37 } | 37 } |
38 } | 38 } |
39 } | 39 } |
40 | 40 |
41 void _addLibraries(ir.Program program) { | 41 void _addLibraries(ir.Program program) { |
42 for (ir.Library library in program.libraries) { | 42 for (ir.Library library in program.libraries) { |
43 _libraryMap[library.importUri] = new LibraryEnv(library); | 43 _libraryMap[library.importUri] = new LibraryEnv(library); |
44 } | 44 } |
45 } | 45 } |
46 | 46 |
47 void _ensureLibraryMap() { | 47 void _ensureLibraryMap() { |
48 if (_libraryMap == null) { | 48 if (_libraryMap == null) { |
49 _libraryMap = <Uri, LibraryEnv>{}; | 49 _libraryMap = <Uri, LibraryEnv>{}; |
50 for (ir.Program program in programs) { | 50 for (ir.Program program in _programs) { |
51 _addLibraries(program); | 51 _addLibraries(program); |
52 } | 52 } |
53 } | 53 } |
54 } | 54 } |
55 | 55 |
56 /// Return the [LibraryEnv] for the library with the canonical [uri]. | 56 /// Return the [LibraryEnv] for the library with the canonical [uri]. |
57 LibraryEnv lookupLibrary(Uri uri) { | 57 LibraryEnv lookupLibrary(Uri uri) { |
58 _ensureLibraryMap(); | 58 _ensureLibraryMap(); |
59 return _libraryMap[uri]; | 59 return _libraryMap[uri]; |
60 } | 60 } |
61 | 61 |
62 /// Calls [f] for each library in this environment. | 62 /// Calls [f] for each library in this environment. |
63 void forEachLibrary(void f(LibraryEnv library)) { | 63 void forEachLibrary(void f(LibraryEnv library)) { |
64 _ensureLibraryMap(); | 64 _ensureLibraryMap(); |
65 _libraryMap.values.forEach(f); | 65 _libraryMap.values.forEach(f); |
66 } | 66 } |
67 | 67 |
68 /// Returns the number of libraries in this environment. | 68 /// Returns the number of libraries in this environment. |
69 int get length { | 69 int get length { |
70 _ensureLibraryMap(); | 70 _ensureLibraryMap(); |
71 return _libraryMap.length; | 71 return _libraryMap.length; |
72 } | 72 } |
| 73 |
| 74 /// Copy all [LibraryEnv]s from [other] to this environment. |
| 75 void copyFrom(ProgramEnv other) { |
| 76 if (other._libraryMap == null) return; |
| 77 _programs.addAll(other._programs); |
| 78 _libraryMap ??= <Uri, LibraryEnv>{}; |
| 79 _libraryMap.addAll(other._libraryMap); |
| 80 } |
73 } | 81 } |
74 | 82 |
75 /// Environment for fast lookup of library classes and members. | 83 /// Environment for fast lookup of library classes and members. |
76 class LibraryEnv { | 84 class LibraryEnv { |
77 final ir.Library library; | 85 final ir.Library library; |
78 | 86 |
79 Map<String, ClassEnv> _classMap; | 87 Map<String, ClassEnv> _classMap; |
80 Map<String, ir.Member> _memberMap; | 88 Map<String, ir.Member> _memberMap; |
81 Map<String, ir.Member> _setterMap; | 89 Map<String, ir.Member> _setterMap; |
82 | 90 |
(...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
400 } else { | 408 } else { |
401 throw new SpannableAssertionFailure( | 409 throw new SpannableAssertionFailure( |
402 field, | 410 field, |
403 "Unexpected field $field in " | 411 "Unexpected field $field in " |
404 "KernelWorldBuilder._getConstructorConstant"); | 412 "KernelWorldBuilder._getConstructorConstant"); |
405 } | 413 } |
406 } | 414 } |
407 return _constant; | 415 return _constant; |
408 } | 416 } |
409 } | 417 } |
OLD | NEW |