| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 /// Indexed entity interfaces for modeling elements derived from Kernel IR. |
| 6 |
| 7 import '../elements/entities.dart'; |
| 8 |
| 9 abstract class _Indexed { |
| 10 int _index; |
| 11 } |
| 12 |
| 13 abstract class IndexedLibrary extends _Indexed implements LibraryEntity { |
| 14 /// Library index used for fast lookup in [KernelToElementMapBase]. |
| 15 int get libraryIndex => _index; |
| 16 } |
| 17 |
| 18 abstract class IndexedClass extends _Indexed implements ClassEntity { |
| 19 /// Class index used for fast lookup in [KernelToElementMapBase]. |
| 20 int get classIndex => _index; |
| 21 } |
| 22 |
| 23 abstract class IndexedMember extends _Indexed implements MemberEntity { |
| 24 /// Member index used for fast lookup in [KernelToElementMapBase]. |
| 25 int get memberIndex => _index; |
| 26 } |
| 27 |
| 28 abstract class IndexedFunction extends _Indexed |
| 29 implements IndexedMember, FunctionEntity {} |
| 30 |
| 31 abstract class IndexedConstructor |
| 32 implements IndexedFunction, ConstructorEntity {} |
| 33 |
| 34 abstract class IndexedField implements IndexedMember, FieldEntity {} |
| 35 |
| 36 abstract class IndexedTypeVariable extends _Indexed |
| 37 implements TypeVariableEntity { |
| 38 /// Type variable index used for fast lookup in [KernelToElementMapBase]. |
| 39 int get typeVariableIndex => _index; |
| 40 } |
| 41 |
| 42 abstract class IndexedTypedef extends _Indexed implements TypedefEntity { |
| 43 /// Typedef index used for fast lookup in [KernelToElementMapBase]. |
| 44 int get typedefIndex => _index; |
| 45 } |
| 46 |
| 47 /// Base implementation for an index based map of entities of type [E]. |
| 48 abstract class EntityMapBase<E extends _Indexed> { |
| 49 List<E> _list = <E>[]; |
| 50 |
| 51 /// Returns the [index]th entity in the map. |
| 52 E getEntity(int index) => _list[index]; |
| 53 |
| 54 /// Returns the number entities in the map. |
| 55 int get length => _list.length; |
| 56 } |
| 57 |
| 58 /// Index based map of entities of type [E]. |
| 59 class EntityMap<E extends _Indexed> extends EntityMapBase<E> { |
| 60 /// Registers a new entity. |
| 61 /// |
| 62 /// [createEntity] is called to create the entity with the given index. |
| 63 E0 register<E0 extends E>(E0 entity) { |
| 64 assert(entity != null); |
| 65 assert(entity._index == null); |
| 66 entity._index = _list.length; |
| 67 _list.add(entity); |
| 68 return entity; |
| 69 } |
| 70 } |
| 71 |
| 72 /// Base implementation of an index based map of entities of type [E] with a |
| 73 /// corresponding data object of type [D]. |
| 74 abstract class EntityDataMapBase<E extends _Indexed, D> |
| 75 extends EntityMapBase<E> { |
| 76 List<D> _data = <D>[]; |
| 77 |
| 78 /// Returns the data object stored for the [index]th entity. |
| 79 D getData(E entity) { |
| 80 int index = entity._index; |
| 81 if (index < length && index >= _data.length) { |
| 82 throw new StateError( |
| 83 'Data is in the process of being created for ${_list[index]}.'); |
| 84 } |
| 85 return _data[index]; |
| 86 } |
| 87 } |
| 88 |
| 89 /// Index based map of entities of type [E] with a corresponding data object |
| 90 /// of type [D]. |
| 91 class EntityDataMap<E extends _Indexed, D> extends EntityDataMapBase<E, D> { |
| 92 /// Registers a new entity with an associated data object. |
| 93 /// |
| 94 /// Firstly, [createEntity] is called to create the entity with the given |
| 95 /// index. Secondly, [createData] is called with the newly created entity to |
| 96 /// create the associated data object. |
| 97 E0 register<E0 extends E, D0 extends D>(E0 entity, D0 data) { |
| 98 assert(entity != null); |
| 99 assert(entity._index == null); |
| 100 entity._index = _list.length; |
| 101 _list.add(entity); |
| 102 assert(data != null); |
| 103 _data.add(data); |
| 104 return entity; |
| 105 } |
| 106 } |
| 107 |
| 108 /// Base implementation for an index based of entities of type [E] with a |
| 109 /// corresponding data object of type [D] and an environment of type [V]. |
| 110 abstract class EntityDataEnvMapBase<E extends _Indexed, D, V> |
| 111 extends EntityDataMapBase<E, D> { |
| 112 List<V> _env = <V>[]; |
| 113 |
| 114 /// Returns the environment object stored for the [index]th entity. |
| 115 V getEnv(E entity) { |
| 116 int index = entity._index; |
| 117 if (index < length && index >= _env.length) { |
| 118 throw new StateError( |
| 119 'Env is in the process of being created for ${_list[index]}.'); |
| 120 } |
| 121 return _env[index]; |
| 122 } |
| 123 } |
| 124 |
| 125 /// Index based of entities of type [E] with a corresponding data object of |
| 126 /// type [D] and an environment of type [V]. |
| 127 class EntityDataEnvMap<E extends _Indexed, D, V> |
| 128 extends EntityDataEnvMapBase<E, D, V> { |
| 129 /// Registers a new entity with an associated data object and environment. |
| 130 /// |
| 131 /// Firstly, [createEntity] is called to create the entity with the given |
| 132 /// index. Secondly, [createData] is called with the newly created entity to |
| 133 /// create the associated data object. Thirdly, [createEnv] is called with |
| 134 /// the newly created entity to create the associated environment object. |
| 135 E0 register<E0 extends E, D0 extends D, V0 extends V>( |
| 136 E0 entity, D0 data, V0 env) { |
| 137 assert(entity != null); |
| 138 assert(entity._index == null); |
| 139 entity._index = _list.length; |
| 140 _list.add(entity); |
| 141 assert(data != null); |
| 142 _data.add(data); |
| 143 assert(env != null); |
| 144 _env.add(env); |
| 145 return entity; |
| 146 } |
| 147 } |
| OLD | NEW |