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

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

Issue 3004433002: Encapsulate the index based maps for entities. (Closed)
Patch Set: Created 3 years, 4 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
OLDNEW
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 '../constants/constructors.dart'; 12 import '../constants/constructors.dart';
13 import '../constants/expressions.dart'; 13 import '../constants/expressions.dart';
14 import '../constants/values.dart'; 14 import '../constants/values.dart';
15 import '../elements/entities.dart'; 15 import '../elements/entities.dart';
16 import '../elements/types.dart'; 16 import '../elements/types.dart';
17 import '../ordered_typeset.dart'; 17 import '../ordered_typeset.dart';
18 import 'element_map.dart'; 18 import 'element_map.dart';
19 import 'element_map_impl.dart'; 19 import 'element_map_impl.dart';
20 import 'element_map_mixins.dart'; 20 import 'element_map_mixins.dart';
21 21
22 /// Base implementation for an index based map of entities of type [E].
23 abstract class EntityMapBase<E> {
24 List<E> _list = <E>[];
25
26 /// Returns the [index]th entity in the map.
27 E getEntity(int index) => _list[index];
28
29 /// Returns the number entities in the map.
30 int get length => _list.length;
31 }
32
33 /// Index based map of entities of type [E].
34 class EntityMap<E> extends EntityMapBase<E> {
35 /// Registers a new entity.
36 ///
37 /// [createEntity] is called to create the entity with the given index.
38 E0 register<E0 extends E>(E0 createEntity(int index)) {
39 E0 entity = createEntity(_list.length);
40 assert(entity != null);
41 _list.add(entity);
42 return entity;
43 }
44 }
45
46 /// Base implementation of an index based map of entities of type [E] with a
47 /// corresponding data object of type [D].
48 abstract class EntityDataMapBase<E, D> extends EntityMapBase<E> {
49 List<D> _data = <D>[];
50
51 /// Returns the data object stored for the [index]th entity.
52 D getData(int index) {
Siggi Cherem (dart-lang) 2017/08/22 19:12:22 what about making a generic Indexable interface an
53 if (index < length && index >= _data.length) {
54 throw new StateError(
55 'Data is in the process of being created for ${_list[index]}.');
56 }
57 return _data[index];
58 }
59 }
60
61 /// Index based map of entities of type [E] with a corresponding data object
62 /// of type [D].
63 class EntityDataMap<E, D> extends EntityDataMapBase<E, D> {
64 /// Registers a new entity with an associated data object.
65 ///
66 /// Firstly, [createEntity] is called to create the entity with the given
67 /// index. Secondly, [createData] is called with the newly created entity to
68 /// create the associated data object.
69 E0 register<E0 extends E, D0 extends D>(
70 E0 createEntity(int index), D0 createData(E0 entity)) {
71 E0 entity = createEntity(_list.length);
72 assert(entity != null);
73 _list.add(entity);
74 D0 data = createData(entity);
75 assert(data != null);
76 _data.add(data);
77 return entity;
78 }
79 }
80
81 /// Base implementation for an index based of entities of type [E] with a
82 /// corresponding data object of type [D] and an environment of type [V].
83 abstract class EntityDataEnvMapBase<E, D, V> extends EntityDataMapBase<E, D> {
84 List<V> _env = <V>[];
85
86 /// Returns the environment object stored for the [index]th entity.
87 V getEnv(int index) {
88 if (index < length && index >= _env.length) {
89 throw new StateError(
90 'Env is in the process of being created for ${_list[index]}.');
91 }
92 return _env[index];
93 }
94 }
95
96 /// Index based of entities of type [E] with a corresponding data object of
97 /// type [D] and an environment of type [V].
98 class EntityDataEnvMap<E, D, V> extends EntityDataEnvMapBase<E, D, V> {
99 /// Registers a new entity with an associated data object and environment.
100 ///
101 /// Firstly, [createEntity] is called to create the entity with the given
102 /// index. Secondly, [createData] is called with the newly created entity to
103 /// create the associated data object. Thirdly, [createEnv] is called with
104 /// the newly created entity to create the associated environment object.
105 E0 register<E0 extends E, D0 extends D, V0 extends V>(
106 E0 createEntity(int index),
Siggi Cherem (dart-lang) 2017/08/22 19:12:22 can we switch to take the actual values instead of
107 D0 createData(E0 entity),
108 V0 createEnv(E0 entity)) {
109 E0 entity = createEntity(_list.length);
110 assert(entity != null);
111 _list.add(entity);
112 D0 data = createData(entity);
113 assert(data != null);
114 _data.add(data);
115 V0 env = createEnv(entity);
116 assert(env != null);
117 _env.add(env);
118 return entity;
119 }
120 }
121
22 /// Environment for fast lookup of program libraries. 122 /// Environment for fast lookup of program libraries.
23 class ProgramEnv { 123 class ProgramEnv {
24 final Set<ir.Program> _programs = new Set<ir.Program>(); 124 final Set<ir.Program> _programs = new Set<ir.Program>();
25 125
26 Map<Uri, LibraryEnv> _libraryMap; 126 Map<Uri, LibraryEnv> _libraryMap;
27 127
28 /// TODO(johnniwinther): Handle arbitrary load order if needed. 128 /// TODO(johnniwinther): Handle arbitrary load order if needed.
29 ir.Member get mainMethod => _programs.first?.mainMethod; 129 ir.Member get mainMethod => _programs.first?.mainMethod;
30 130
31 void addProgram(ir.Program program) { 131 void addProgram(ir.Program program) {
(...skipping 571 matching lines...) Expand 10 before | Expand all | Expand 10 after
603 } 703 }
604 } 704 }
605 705
606 class TypedefData { 706 class TypedefData {
607 final ir.Typedef node; 707 final ir.Typedef node;
608 final TypedefEntity element; 708 final TypedefEntity element;
609 final TypedefType rawType; 709 final TypedefType rawType;
610 710
611 TypedefData(this.node, this.element, this.rawType); 711 TypedefData(this.node, this.element, this.rawType);
612 } 712 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698