Skip to content

Commit ec733c3

Browse files
johnniwinthercommit-bot@chromium.org
authored andcommitted
Support more type arguments in generic instantiation
The real solution requires more work, so this is to buy us some time. Change-Id: I033a77d05c0ca7658475d1fe59760ce4e5919f7e Reviewed-on: https://dart-review.googlesource.com/65541 Reviewed-by: Sigmund Cherem <[email protected]>
1 parent 7074704 commit ec733c3

File tree

11 files changed

+616
-64
lines changed

11 files changed

+616
-64
lines changed

pkg/compiler/lib/src/common_elements.dart

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,18 +1115,39 @@ class CommonElements {
11151115
FunctionEntity get hashCodeForNativeObject =>
11161116
_findHelperFunction('hashCodeForNativeObject');
11171117

1118-
ClassEntity get instantiation1Class => _findHelperClass('Instantiation1');
1119-
ClassEntity get instantiation2Class => _findHelperClass('Instantiation2');
1120-
ClassEntity get instantiation3Class => _findHelperClass('Instantiation3');
1121-
FunctionEntity get instantiate1 => _findHelperFunction('instantiate1');
1122-
FunctionEntity get instantiate2 => _findHelperFunction('instantiate2');
1123-
FunctionEntity get instantiate3 => _findHelperFunction('instantiate3');
1118+
// TODO(johnniwinther,sra): Support arbitrary type argument count.
1119+
void _checkTypeArgumentCount(int typeArgumentCount) {
1120+
assert(typeArgumentCount > 0);
1121+
if (typeArgumentCount > 20) {
1122+
failedAt(
1123+
NO_LOCATION_SPANNABLE,
1124+
"Unsupported instantiation argument count: "
1125+
"${typeArgumentCount}");
1126+
}
1127+
}
1128+
1129+
ClassEntity getInstantiationClass(int typeArgumentCount) {
1130+
_checkTypeArgumentCount(typeArgumentCount);
1131+
return _findHelperClass('Instantiation$typeArgumentCount');
1132+
}
1133+
1134+
FunctionEntity getInstantiateFunction(int typeArgumentCount) {
1135+
_checkTypeArgumentCount(typeArgumentCount);
1136+
return _findHelperFunction('instantiate$typeArgumentCount');
1137+
}
1138+
11241139
FunctionEntity get instantiatedGenericFunctionType =>
11251140
_findHelperFunction('instantiatedGenericFunctionType');
11261141

11271142
FunctionEntity get extractFunctionTypeObjectFromInternal =>
11281143
_findHelperFunction('extractFunctionTypeObjectFromInternal');
11291144

1145+
bool isInstantiationClass(ClassEntity cls) {
1146+
return cls.library == _jsHelperLibrary &&
1147+
cls.name != 'Instantiation' &&
1148+
cls.name.startsWith('Instantiation');
1149+
}
1150+
11301151
// From dart:_internal
11311152

11321153
ClassEntity _symbolImplementationClass;

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -768,18 +768,15 @@ class BackendImpacts {
768768
]);
769769
}
770770

771-
BackendImpact _genericInstantiation;
771+
Map<int, BackendImpact> _genericInstantiation = <int, BackendImpact>{};
772772

773-
BackendImpact get genericInstantiation =>
774-
_genericInstantiation ??= new BackendImpact(staticUses: [
775-
_commonElements.instantiate1,
776-
_commonElements.instantiate2,
777-
_commonElements.instantiate3,
773+
BackendImpact getGenericInstantiation(int typeArgumentCount) =>
774+
_genericInstantiation[typeArgumentCount] ??=
775+
new BackendImpact(staticUses: [
776+
_commonElements.getInstantiateFunction(typeArgumentCount),
778777
_commonElements.instantiatedGenericFunctionType,
779778
_commonElements.extractFunctionTypeObjectFromInternal,
780779
], instantiatedClasses: [
781-
_commonElements.instantiation1Class,
782-
_commonElements.instantiation2Class,
783-
_commonElements.instantiation3Class,
780+
_commonElements.getInstantiationClass(typeArgumentCount),
784781
]);
785782
}

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,8 @@ class CodegenEnqueuerListener extends EnqueuerListener {
157157
} else if (constant is InstantiationConstantValue) {
158158
// TODO(johnniwinther): Register these using `BackendImpact`.
159159
impactBuilder.registerTypeUse(new TypeUse.instantiation(
160-
_elementEnvironment
161-
.getThisType(_commonElements.instantiation1Class)));
162-
impactBuilder.registerTypeUse(new TypeUse.instantiation(
163-
_elementEnvironment
164-
.getThisType(_commonElements.instantiation2Class)));
165-
impactBuilder.registerTypeUse(new TypeUse.instantiation(
166-
_elementEnvironment
167-
.getThisType(_commonElements.instantiation2Class)));
160+
_elementEnvironment.getThisType(_commonElements
161+
.getInstantiationClass(constant.typeArguments.length))));
168162
impactBuilder.registerStaticUse(new StaticUse.staticInvoke(
169163
_commonElements.instantiatedGenericFunctionType,
170164
CallStructure.TWO_ARGS));

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

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -340,24 +340,8 @@ class ConstantEmitter implements ConstantValueVisitor<jsAst.Expression, Null> {
340340
@override
341341
jsAst.Expression visitInstantiation(InstantiationConstantValue constant,
342342
[_]) {
343-
// TODO(johnniwinther,sra): Support arbitrary type argument count.
344-
ClassEntity cls;
345-
switch (constant.typeArguments.length) {
346-
case 1:
347-
cls = _commonElements.instantiation1Class;
348-
break;
349-
case 2:
350-
cls = _commonElements.instantiation2Class;
351-
break;
352-
case 3:
353-
cls = _commonElements.instantiation3Class;
354-
break;
355-
default:
356-
failedAt(
357-
NO_LOCATION_SPANNABLE,
358-
"Unsupported instantiation argument count: "
359-
"${constant.typeArguments.length}");
360-
}
343+
ClassEntity cls =
344+
_commonElements.getInstantiationClass(constant.typeArguments.length);
361345
List<jsAst.Expression> fields = <jsAst.Expression>[
362346
constantReferenceGenerator(constant.function),
363347
_reifiedTypeArguments(constant, constant.typeArguments)

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,9 +301,10 @@ class JavaScriptImpactTransformer extends ImpactTransformer {
301301
}
302302

303303
if (worldImpact.genericInstantiations.isNotEmpty) {
304-
registerImpact(_impacts.genericInstantiation);
305304
for (GenericInstantiation instantiation
306305
in worldImpact.genericInstantiations) {
306+
registerImpact(_impacts
307+
.getGenericInstantiation(instantiation.typeArguments.length));
307308
_rtiNeedBuilder.registerGenericInstantiation(instantiation);
308309
}
309310
}

pkg/compiler/lib/src/js_emitter/program_builder/program_builder.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -718,9 +718,7 @@ class ProgramBuilder {
718718
callStubs.add(_buildStubMethod(name, function));
719719
}
720720

721-
if (cls == _commonElements.instantiation1Class ||
722-
cls == _commonElements.instantiation2Class ||
723-
cls == _commonElements.instantiation3Class) {
721+
if (_commonElements.isInstantiationClass(cls)) {
724722
callStubs.addAll(_generateInstantiationStubs(cls));
725723
}
726724

pkg/compiler/lib/src/ssa/builder_kernel.dart

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4364,7 +4364,8 @@ class KernelSsaGraphBuilder extends ir.Visitor
43644364
}
43654365
int typeArgumentCount = node.typeArguments.length;
43664366
bool targetCanThrow = false; // TODO(sra): Is this true?
4367-
FunctionEntity target = _instantiator(typeArgumentCount);
4367+
FunctionEntity target =
4368+
_commonElements.getInstantiateFunction(typeArgumentCount);
43684369
if (target == null) {
43694370
reporter.internalError(
43704371
_elementMap.getSpannable(targetElement, node),
@@ -4384,14 +4385,6 @@ class KernelSsaGraphBuilder extends ir.Visitor
43844385
push(instruction);
43854386
}
43864387

4387-
FunctionEntity _instantiator(int count) {
4388-
// TODO(johnniwinther,sra): Support arbitrary type argument count.
4389-
if (count == 1) return _commonElements.instantiate1;
4390-
if (count == 2) return _commonElements.instantiate2;
4391-
if (count == 3) return _commonElements.instantiate3;
4392-
return null;
4393-
}
4394-
43954388
@override
43964389
void visitMethodInvocation(ir.MethodInvocation node) {
43974390
node.receiver.accept(this);

0 commit comments

Comments
 (0)