Skip to content

Commit 43df18a

Browse files
natebiggsCommit Queue
authored andcommitted
[dart2js] Use existing J-model entities where possible when converting from kernel to JS entity maps.
Change-Id: I1bcfecb313fab1e7dc03bf1d8c14b8410226f431 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/309140 Commit-Queue: Nate Biggs <[email protected]> Reviewed-by: Sigmund Cherem <[email protected]>
1 parent dbd34a4 commit 43df18a

File tree

7 files changed

+445
-501
lines changed

7 files changed

+445
-501
lines changed

pkg/compiler/lib/src/elements/indexed.dart

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,20 @@ class EntityDataMap<E extends _Indexed, D> extends EntityDataMapBase<E, D> {
132132
///
133133
/// The index of [entity] is set to match its index in the entity and data
134134
/// lists in this map.
135-
E0 register<E0 extends E, D0 extends D>(E0 entity, D0 data) {
135+
E0 register<E0 extends E, D0 extends D>(E0 entity, D0 data,
136+
{bool setEntityIndex = true}) {
136137
assert(
137138
!_closed, "Trying to register $entity @ ${_list.length} when closed.");
138139
assert(
139140
_list.length == _data.length,
140141
'Data list length ${_data.length} inconsistent '
141142
'with entity list length ${_list.length}.');
142-
entity._index = _list.length;
143+
if (setEntityIndex) {
144+
entity._index = _list.length;
145+
} else {
146+
assert(entity._index == _list.length,
147+
'Trying to register entity with in incorrect order. ');
148+
}
143149
_list.add(entity);
144150
_size++;
145151
assert(data != null);
@@ -197,7 +203,8 @@ class EntityDataEnvMap<E extends _Indexed, D, V>
197203
/// The index of [entity] is set to match its index in the entity, data and
198204
/// environment lists in this map.
199205
E0 register<E0 extends E, D0 extends D, V0 extends V>(
200-
E0 entity, D0 data, V0 env) {
206+
E0 entity, D0 data, V0 env,
207+
{bool setEntityIndex = true}) {
201208
assert(
202209
!_closed, "Trying to register $entity @ ${_list.length} when closed.");
203210
assert(
@@ -208,7 +215,12 @@ class EntityDataEnvMap<E extends _Indexed, D, V>
208215
_list.length == _env.length,
209216
'Env list length ${_env.length} inconsistent '
210217
'with entity list length ${_list.length}.');
211-
entity._index = _list.length;
218+
if (setEntityIndex) {
219+
entity._index = _list.length;
220+
} else {
221+
assert(entity._index == _list.length,
222+
'Trying to register entity with in incorrect order. ');
223+
}
212224
_list.add(entity);
213225
_size++;
214226
assert(data != null);

pkg/compiler/lib/src/js_backend/native_data.dart

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -362,10 +362,8 @@ class NativeBasicData {
362362
Map<ClassEntity, NativeClassTag> nativeClassTagInfo =
363363
<ClassEntity, NativeClassTag>{};
364364
_nativeClassTagInfo.forEach((ClassEntity cls, NativeClassTag tag) {
365-
ClassEntity? backendClass = map.toBackendClass(cls);
366-
if (backendClass != null) {
367-
nativeClassTagInfo[backendClass] = tag;
368-
}
365+
final backendClass = map.toBackendClass(cls);
366+
nativeClassTagInfo[backendClass] = tag;
369367
});
370368
Map<LibraryEntity, String> jsInteropLibraries =
371369
map.toBackendLibraryMap(_jsInteropLibraries, identity);

pkg/compiler/lib/src/js_model/element_map_impl.dart

Lines changed: 66 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -142,35 +142,29 @@ class JsKernelToElementMap implements JsToElementMap, IrToElementMap {
142142
for (int libraryIndex = 0;
143143
libraryIndex < _elementMap.libraries.length;
144144
libraryIndex++) {
145-
IndexedLibrary oldLibrary =
146-
_elementMap.libraries.getEntity(libraryIndex)!;
147-
KLibraryEnv oldEnv = _elementMap.libraries.getEnv(oldLibrary);
148-
KLibraryData data = _elementMap.libraries.getData(oldLibrary);
149-
IndexedLibrary newLibrary = JLibrary(oldLibrary.name!,
150-
oldLibrary.canonicalUri, oldLibrary.isNonNullableByDefault);
145+
IndexedLibrary library = _elementMap.libraries.getEntity(libraryIndex)!;
146+
KLibraryEnv oldEnv = _elementMap.libraries.getEnv(library);
147+
KLibraryData data = _elementMap.libraries.getData(library);
151148
JLibraryEnv newEnv = oldEnv.convert(_elementMap, liveMemberUsage);
152149
libraryMap[oldEnv.library] =
153150
libraries.register<IndexedLibrary, JLibraryData, JLibraryEnv>(
154-
newLibrary, data.convert(), newEnv);
155-
assert(newLibrary.libraryIndex == oldLibrary.libraryIndex);
151+
library, data.convert(), newEnv,
152+
setEntityIndex: false);
156153
programEnv.registerLibrary(newEnv);
157154
}
158155
// TODO(johnniwinther): Filter unused classes.
159156
for (int classIndex = 0;
160157
classIndex < _elementMap.classes.length;
161158
classIndex++) {
162-
IndexedClass oldClass = _elementMap.classes.getEntity(classIndex)!;
163-
KClassEnv env = _elementMap.classes.getEnv(oldClass);
164-
KClassData data = _elementMap.classes.getData(oldClass);
165-
final oldLibrary = oldClass.library as IndexedLibrary;
166-
LibraryEntity newLibrary = libraries.getEntity(oldLibrary.libraryIndex)!;
167-
IndexedClass newClass = JClass(newLibrary as JLibrary, oldClass.name,
168-
isAbstract: oldClass.isAbstract);
159+
IndexedClass cls = _elementMap.classes.getEntity(classIndex)!;
160+
KClassEnv env = _elementMap.classes.getEnv(cls);
161+
KClassData data = _elementMap.classes.getData(cls);
162+
final library = cls.library as JLibrary;
169163
JClassEnv newEnv = env.convert(_elementMap, liveMemberUsage,
170164
liveAbstractMembers, (ir.Library library) => libraryMap[library]!);
171-
classMap[env.cls] = classes.register(newClass, data.convert(), newEnv);
172-
assert(newClass.classIndex == oldClass.classIndex);
173-
libraries.getEnv(newLibrary).registerClass(newClass.name, newEnv);
165+
classMap[env.cls] =
166+
classes.register(cls, data.convert(), newEnv, setEntityIndex: false);
167+
libraries.getEnv(library).registerClass(cls.name, newEnv);
174168
}
175169

176170
for (int memberIndex = 0;
@@ -183,68 +177,45 @@ class JsKernelToElementMap implements JsToElementMap, IrToElementMap {
183177
continue;
184178
}
185179
KMemberData data = _elementMap.members.getData(oldMember);
186-
final oldLibrary = oldMember.library as IndexedLibrary;
187-
final oldClass = oldMember.enclosingClass as IndexedClass?;
188-
JLibrary newLibrary =
189-
libraries.getEntity(oldLibrary.libraryIndex) as JLibrary;
190-
JClass? newClass = oldClass != null
191-
? classes.getEntity(oldClass.classIndex) as JClass?
192-
: null;
193-
IndexedMember newMember;
180+
final library = oldMember.library as JLibrary;
181+
final cls = oldMember.enclosingClass as JClass?;
182+
IndexedMember? newMember;
194183
Name memberName = oldMember.memberName;
195-
if (oldMember is IndexedField) {
196-
final field = oldMember;
197-
newMember = JField(newLibrary, newClass, memberName,
198-
isStatic: field.isStatic,
199-
isAssignable: field.isAssignable,
200-
isConst: field.isConst);
201-
} else if (oldMember is ConstructorEntity) {
202-
final constructor = oldMember as IndexedConstructor;
203-
ParameterStructure parameterStructure =
204-
annotations.hasNoElision(constructor) || memberUsage == null
205-
? constructor.parameterStructure
206-
: memberUsage.invokedParameters!;
207-
if (constructor.isFactoryConstructor) {
208-
// TODO(redemption): This should be a JFunction.
209-
newMember = JFactoryConstructor(
210-
newClass!, memberName, parameterStructure,
211-
isExternal: constructor.isExternal,
212-
isConst: constructor.isConst,
213-
isFromEnvironmentConstructor:
214-
constructor.isFromEnvironmentConstructor);
215-
} else {
216-
newMember = JGenerativeConstructor(
217-
newClass!, memberName, parameterStructure,
218-
isExternal: constructor.isExternal, isConst: constructor.isConst);
184+
185+
// Only create a new entity if some parameters are unused and can be
186+
// elided.
187+
if (!annotations.hasNoElision(oldMember) &&
188+
memberUsage != null &&
189+
oldMember is IndexedFunction &&
190+
!identical(
191+
oldMember.parameterStructure, memberUsage.invokedParameters)) {
192+
if (oldMember is ConstructorEntity) {
193+
final newParameters = memberUsage.invokedParameters!;
194+
final constructor = oldMember as ConstructorEntity;
195+
if (constructor.isFactoryConstructor) {
196+
// TODO(redemption): This should be a JFunction.
197+
newMember = JFactoryConstructor(cls!, memberName, newParameters,
198+
isExternal: constructor.isExternal,
199+
isConst: constructor.isConst,
200+
isFromEnvironmentConstructor:
201+
constructor.isFromEnvironmentConstructor);
202+
} else {
203+
newMember = JGenerativeConstructor(cls!, memberName, newParameters,
204+
isExternal: constructor.isExternal,
205+
isConst: constructor.isConst);
206+
}
207+
} else if (oldMember.isFunction && !oldMember.isAbstract) {
208+
final newParameters = memberUsage.invokedParameters!;
209+
newMember = JMethod(
210+
library, cls, memberName, newParameters, oldMember.asyncMarker,
211+
isStatic: oldMember.isStatic,
212+
isExternal: oldMember.isExternal,
213+
isAbstract: oldMember.isAbstract);
219214
}
220-
} else if (oldMember.isGetter) {
221-
final getter = oldMember as IndexedFunction;
222-
newMember = JGetter(
223-
newLibrary, newClass, memberName, getter.asyncMarker,
224-
isStatic: getter.isStatic,
225-
isExternal: getter.isExternal,
226-
isAbstract: getter.isAbstract);
227-
} else if (oldMember.isSetter) {
228-
final setter = oldMember as IndexedFunction;
229-
newMember = JSetter(newLibrary, newClass, memberName,
230-
isStatic: setter.isStatic,
231-
isExternal: setter.isExternal,
232-
isAbstract: setter.isAbstract);
233-
} else {
234-
final function = oldMember as IndexedFunction;
235-
ParameterStructure parameterStructure =
236-
annotations.hasNoElision(function) ||
237-
memberUsage == null ||
238-
function.isAbstract
239-
? function.parameterStructure
240-
: memberUsage.invokedParameters!;
241-
newMember = JMethod(newLibrary, newClass, memberName,
242-
parameterStructure, function.asyncMarker,
243-
isStatic: function.isStatic,
244-
isExternal: function.isExternal,
245-
isAbstract: function.isAbstract);
246215
}
247-
members.register(newMember, data.convert());
216+
members.register(newMember ?? oldMember, data.convert(),
217+
setEntityIndex: newMember != null);
218+
newMember ??= oldMember;
248219
assert(
249220
newMember.memberIndex == oldMember.memberIndex,
250221
"Member index mismatch: "
@@ -265,28 +236,33 @@ class JsKernelToElementMap implements JsToElementMap, IrToElementMap {
265236
_elementMap.typeVariables.getEntity(typeVariableIndex)!;
266237
KTypeVariableData oldTypeVariableData =
267238
_elementMap.typeVariables.getData(oldTypeVariable);
268-
Entity? newTypeDeclaration;
269-
if (oldTypeVariable.typeDeclaration is ClassEntity) {
270-
final cls = oldTypeVariable.typeDeclaration as IndexedClass;
271-
newTypeDeclaration = classes.getEntity(cls.classIndex);
272-
// TODO(johnniwinther): Skip type variables of unused classes.
273-
} else {
274-
assert(oldTypeVariable.typeDeclaration is MemberEntity);
239+
240+
// [JLocalTypeVariable] can have [Local] as a `typeDeclaration` but those
241+
// should never be inserted into the TypeVariable entity map.
242+
assert(oldTypeVariable.typeDeclaration is ClassEntity ||
243+
oldTypeVariable.typeDeclaration is MemberEntity);
244+
245+
MemberEntity? newTypeDeclaration;
246+
// TODO(johnniwinther): Skip type variables of unused classes.
247+
if (oldTypeVariable.typeDeclaration is MemberEntity) {
275248
final member = oldTypeVariable.typeDeclaration as IndexedMember;
276249
newTypeDeclaration = members.getEntity(member.memberIndex);
277250
if (newTypeDeclaration == null) {
278251
typeVariables.skipIndex(typeVariableIndex);
279252
continue;
280253
}
281254
}
282-
IndexedTypeVariable newTypeVariable = createTypeVariable(
283-
newTypeDeclaration!, oldTypeVariable.name!, oldTypeVariable.index)
284-
as IndexedTypeVariable;
255+
IndexedTypeVariable? newTypeVariable;
256+
if (newTypeDeclaration != null) {
257+
newTypeVariable = createTypeVariable(
258+
newTypeDeclaration,
259+
oldTypeVariable.name!,
260+
oldTypeVariable.index) as IndexedTypeVariable;
261+
}
285262
typeVariableMap[oldTypeVariableData.node] =
286263
typeVariables.register<IndexedTypeVariable, JTypeVariableData>(
287-
newTypeVariable, oldTypeVariableData.copy());
288-
assert(newTypeVariable.typeVariableIndex ==
289-
oldTypeVariable.typeVariableIndex);
264+
newTypeVariable ?? oldTypeVariable, oldTypeVariableData.copy(),
265+
setEntityIndex: newTypeVariable != null);
290266
}
291267
// TODO(johnniwinther): We should close the environment in the beginning of
292268
// this constructor but currently we need the [MemberEntity] to query if the

pkg/compiler/lib/src/js_model/elements.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,7 @@ class JTypeVariable extends IndexedTypeVariable {
779779
factory JTypeVariable.readFromDataSource(DataSourceReader source) {
780780
source.begin(tag);
781781
JTypeVariableKind kind = source.readEnum(JTypeVariableKind.values);
782-
Entity? typeDeclaration;
782+
Entity typeDeclaration;
783783
switch (kind) {
784784
case JTypeVariableKind.cls:
785785
typeDeclaration = source.readClass();

0 commit comments

Comments
 (0)