mirror of
https://github.com/dart-lang/sdk
synced 2024-09-04 16:03:44 +00:00
Port type_representation_test to work with kernel
Change-Id: Icfc60f741fd00386d781c1d6dd1a4b874478a1fb Reviewed-on: https://dart-review.googlesource.com/48601 Reviewed-by: Sigmund Cherem <sigmund@google.com>
This commit is contained in:
parent
8504808ce3
commit
85d5812a93
48
tests/compiler/dart2js/helpers/element_lookup.dart
Normal file
48
tests/compiler/dart2js/helpers/element_lookup.dart
Normal file
|
@ -0,0 +1,48 @@
|
|||
// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
import 'package:compiler/src/common_elements.dart' show ElementEnvironment;
|
||||
import 'package:compiler/src/elements/entities.dart';
|
||||
import 'package:compiler/src/elements/types.dart';
|
||||
import 'package:compiler/src/world.dart' show ClosedWorld;
|
||||
|
||||
ClassEntity findClass(ClosedWorld closedWorld, String name) {
|
||||
ElementEnvironment elementEnvironment = closedWorld.elementEnvironment;
|
||||
ClassEntity cls =
|
||||
elementEnvironment.lookupClass(elementEnvironment.mainLibrary, name);
|
||||
cls ??= elementEnvironment.lookupClass(
|
||||
closedWorld.commonElements.coreLibrary, name);
|
||||
assert(cls != null, "Class '$name' not found.");
|
||||
return cls;
|
||||
}
|
||||
|
||||
MemberEntity findClassMember(
|
||||
ClosedWorld closedWorld, String className, String memberName) {
|
||||
ElementEnvironment elementEnvironment = closedWorld.elementEnvironment;
|
||||
ClassEntity cls = findClass(closedWorld, className);
|
||||
assert(cls != null, "Class '$className' not found.");
|
||||
MemberEntity member = elementEnvironment.lookupClassMember(cls, memberName);
|
||||
assert(member != null, "Member '$memberName' not found in $cls.");
|
||||
return member;
|
||||
}
|
||||
|
||||
MemberEntity findMember(ClosedWorld closedWorld, String name) {
|
||||
ElementEnvironment elementEnvironment = closedWorld.elementEnvironment;
|
||||
MemberEntity member = elementEnvironment.lookupLibraryMember(
|
||||
elementEnvironment.mainLibrary, name);
|
||||
member ??= elementEnvironment.lookupLibraryMember(
|
||||
closedWorld.commonElements.coreLibrary, name);
|
||||
assert(member != null, "Member '$name' not found.");
|
||||
return member;
|
||||
}
|
||||
|
||||
FunctionType findFunctionType(ClosedWorld closedWorld, String name) {
|
||||
FunctionEntity function = findMember(closedWorld, name);
|
||||
return closedWorld.elementEnvironment.getFunctionType(function);
|
||||
}
|
||||
|
||||
DartType findFieldType(ClosedWorld closedWorld, String name) {
|
||||
FieldEntity field = findMember(closedWorld, name);
|
||||
return closedWorld.elementEnvironment.getFieldType(field);
|
||||
}
|
|
@ -8,6 +8,7 @@ import 'package:compiler/src/commandline_options.dart';
|
|||
import 'package:expect/expect.dart';
|
||||
|
||||
import 'type_mask_test_helper.dart';
|
||||
import '../helpers/element_lookup.dart';
|
||||
import '../memory_compiler.dart';
|
||||
|
||||
String generateTest(String listAllocation) {
|
||||
|
|
|
@ -12,6 +12,7 @@ import 'package:compiler/src/world.dart';
|
|||
import 'package:expect/expect.dart';
|
||||
|
||||
import 'type_mask_test_helper.dart';
|
||||
import '../helpers/element_lookup.dart';
|
||||
import '../memory_compiler.dart';
|
||||
|
||||
String generateTest(String mapAllocation) {
|
||||
|
|
|
@ -4,9 +4,6 @@
|
|||
|
||||
library type_mask_test_helper;
|
||||
|
||||
import 'package:compiler/src/common_elements.dart' show ElementEnvironment;
|
||||
import 'package:compiler/src/elements/entities.dart'
|
||||
show ClassEntity, MemberEntity;
|
||||
import 'package:compiler/src/types/types.dart';
|
||||
import 'package:compiler/src/world.dart' show ClosedWorld;
|
||||
|
||||
|
@ -40,30 +37,3 @@ TypeMask interceptorOrComparable(ClosedWorld closedWorld,
|
|||
closedWorld);
|
||||
}
|
||||
}
|
||||
|
||||
ClassEntity findClass(ClosedWorld closedWorld, String name) {
|
||||
ElementEnvironment elementEnvironment = closedWorld.elementEnvironment;
|
||||
ClassEntity cls =
|
||||
elementEnvironment.lookupClass(elementEnvironment.mainLibrary, name);
|
||||
assert(cls != null, "Class '$name' not found.");
|
||||
return cls;
|
||||
}
|
||||
|
||||
MemberEntity findClassMember(
|
||||
ClosedWorld closedWorld, String className, String memberName) {
|
||||
ElementEnvironment elementEnvironment = closedWorld.elementEnvironment;
|
||||
ClassEntity cls =
|
||||
elementEnvironment.lookupClass(elementEnvironment.mainLibrary, className);
|
||||
assert(cls != null, "Class '$className' not found.");
|
||||
MemberEntity member = elementEnvironment.lookupClassMember(cls, memberName);
|
||||
assert(member != null, "Member '$memberName' not found in $cls.");
|
||||
return member;
|
||||
}
|
||||
|
||||
MemberEntity findMember(ClosedWorld closedWorld, String name) {
|
||||
ElementEnvironment elementEnvironment = closedWorld.elementEnvironment;
|
||||
MemberEntity member = elementEnvironment.lookupLibraryMember(
|
||||
elementEnvironment.mainLibrary, name);
|
||||
assert(member != null, "Member '$name' not found.");
|
||||
return member;
|
||||
}
|
||||
|
|
|
@ -6,282 +6,296 @@
|
|||
|
||||
library type_representation_test;
|
||||
|
||||
import 'package:expect/expect.dart';
|
||||
import "package:async_helper/async_helper.dart";
|
||||
import 'type_test_helper.dart';
|
||||
import 'package:compiler/src/elements/resolution_types.dart';
|
||||
import 'package:async_helper/async_helper.dart';
|
||||
import 'package:compiler/src/common_elements.dart';
|
||||
import 'package:compiler/src/commandline_options.dart';
|
||||
import 'package:compiler/src/compiler.dart';
|
||||
import 'package:compiler/src/elements/types.dart';
|
||||
import 'package:compiler/src/js/js.dart';
|
||||
import 'package:compiler/src/elements/elements.dart' show Element, ClassElement;
|
||||
import 'package:compiler/src/elements/entities.dart';
|
||||
import 'package:compiler/src/js_backend/backend.dart' show JavaScriptBackend;
|
||||
import 'package:compiler/src/js_backend/runtime_types.dart'
|
||||
show TypeRepresentationGenerator;
|
||||
import 'package:compiler/src/world.dart';
|
||||
import 'package:expect/expect.dart';
|
||||
import 'helpers/element_lookup.dart';
|
||||
import 'memory_compiler.dart';
|
||||
import 'type_test_helper.dart';
|
||||
|
||||
void main() {
|
||||
testTypeRepresentations();
|
||||
asyncTest(() async {
|
||||
print('--test from ast---------------------------------------------------');
|
||||
await testAll(useKernel: false);
|
||||
print('--test from kernel------------------------------------------------');
|
||||
await testAll(useKernel: true);
|
||||
print('--test from kernel (strong)---------------------------------------');
|
||||
await testAll(useKernel: true, strongMode: true);
|
||||
});
|
||||
}
|
||||
|
||||
void testTypeRepresentations() {
|
||||
asyncTest(() => TypeEnvironment.create(r"""
|
||||
typedef void Typedef();
|
||||
typedef int Typedef2();
|
||||
typedef List<int> Typedef3();
|
||||
typedef Typedef4();
|
||||
typedef Typedef5(int a, String b);
|
||||
typedef Typedef6(int a, [String b]);
|
||||
typedef Typedef7(int a, String b, [List<int> c, d]);
|
||||
typedef Typedef8(int a, {String b});
|
||||
typedef Typedef9(int a, String b, {List<int> c, d});
|
||||
typedef Typedef10(void f(int a, [b]));
|
||||
|
||||
void m1() {}
|
||||
int m2() => 0;
|
||||
List<int> m3() => null;
|
||||
m4() {}
|
||||
m5(int a, String b) {}
|
||||
m6(int a, [String b]) {}
|
||||
m7(int a, String b, [List<int> c, d]) {}
|
||||
m8(int a, {String b}) {}
|
||||
m9(int a, String b, {List<int> c, d}) {}
|
||||
m10(void f(int a, [b])) {}
|
||||
""").then((env) {
|
||||
var elementEnvironment =
|
||||
env.compiler.frontendStrategy.elementEnvironment;
|
||||
var closedWorldRefiner =
|
||||
env.compiler.closeResolution(elementEnvironment.mainFunction);
|
||||
var closedWorld = closedWorldRefiner.closedWorld;
|
||||
env.compiler.startCodegen(closedWorld);
|
||||
TypeRepresentationGenerator typeRepresentation =
|
||||
new TypeRepresentationGenerator(env.compiler.backend.namer, false);
|
||||
|
||||
Expression onVariable(TypeVariableType _variable) {
|
||||
ResolutionTypeVariableType variable = _variable;
|
||||
return new VariableUse(variable.name);
|
||||
}
|
||||
|
||||
String stringify(Expression expression) {
|
||||
return prettyPrint(expression,
|
||||
enableMinification: env.compiler.options.enableMinification);
|
||||
}
|
||||
|
||||
void expect(ResolutionDartType type, String expectedRepresentation,
|
||||
[String expectedTypedefRepresentation]) {
|
||||
bool encodeTypedefName = false;
|
||||
Expression expression = typeRepresentation.getTypeRepresentation(
|
||||
env.compiler.backend.emitter.emitter,
|
||||
type,
|
||||
onVariable,
|
||||
(x) => encodeTypedefName);
|
||||
Expect.stringEquals(expectedRepresentation, stringify(expression));
|
||||
|
||||
encodeTypedefName = true;
|
||||
expression = typeRepresentation.getTypeRepresentation(
|
||||
env.compiler.backend.emitter.emitter,
|
||||
type,
|
||||
onVariable,
|
||||
(x) => encodeTypedefName);
|
||||
if (expectedTypedefRepresentation == null) {
|
||||
expectedTypedefRepresentation = expectedRepresentation;
|
||||
}
|
||||
Expect.stringEquals(
|
||||
expectedTypedefRepresentation, stringify(expression));
|
||||
}
|
||||
|
||||
String getJsName(Element cls) {
|
||||
Expression name = typeRepresentation.getJavaScriptClassName(
|
||||
cls, env.compiler.backend.emitter.emitter);
|
||||
return stringify(name);
|
||||
}
|
||||
|
||||
JavaScriptBackend backend = env.compiler.backend;
|
||||
String func = backend.namer.functionTypeTag;
|
||||
String retvoid = backend.namer.functionTypeVoidReturnTag;
|
||||
String ret = backend.namer.functionTypeReturnTypeTag;
|
||||
String args = backend.namer.functionTypeRequiredParametersTag;
|
||||
String opt = backend.namer.functionTypeOptionalParametersTag;
|
||||
String named = backend.namer.functionTypeNamedParametersTag;
|
||||
String typedefTag = backend.namer.typedefTag;
|
||||
|
||||
ClassElement List_ = env.getElement('List');
|
||||
ResolutionTypeVariableType List_E = List_.typeVariables[0];
|
||||
ClassElement Map_ = env.getElement('Map');
|
||||
ResolutionTypeVariableType Map_K = Map_.typeVariables[0];
|
||||
ResolutionTypeVariableType Map_V = Map_.typeVariables[1];
|
||||
|
||||
ResolutionDartType int_ = env['int'];
|
||||
ResolutionDartType String_ = env['String'];
|
||||
ResolutionDartType dynamic_ = env['dynamic'];
|
||||
ResolutionDartType Typedef_ = env['Typedef'];
|
||||
ResolutionDartType Typedef2_ = env['Typedef2'];
|
||||
ResolutionDartType Typedef3_ = env['Typedef3'];
|
||||
ResolutionDartType Typedef4_ = env['Typedef4'];
|
||||
ResolutionDartType Typedef5_ = env['Typedef5'];
|
||||
ResolutionDartType Typedef6_ = env['Typedef6'];
|
||||
ResolutionDartType Typedef7_ = env['Typedef7'];
|
||||
ResolutionDartType Typedef8_ = env['Typedef8'];
|
||||
ResolutionDartType Typedef9_ = env['Typedef9'];
|
||||
ResolutionDartType Typedef10_ = env['Typedef10'];
|
||||
|
||||
String List_rep = getJsName(List_);
|
||||
String List_E_rep = stringify(onVariable(List_E));
|
||||
String Map_rep = getJsName(Map_);
|
||||
String Map_K_rep = stringify(onVariable(Map_K));
|
||||
String Map_V_rep = stringify(onVariable(Map_V));
|
||||
|
||||
String int_rep = getJsName(int_.element);
|
||||
String String_rep = getJsName(String_.element);
|
||||
|
||||
String Typedef_rep = getJsName(Typedef_.element);
|
||||
String Typedef2_rep = getJsName(Typedef2_.element);
|
||||
String Typedef3_rep = getJsName(Typedef3_.element);
|
||||
String Typedef4_rep = getJsName(Typedef4_.element);
|
||||
String Typedef5_rep = getJsName(Typedef5_.element);
|
||||
String Typedef6_rep = getJsName(Typedef6_.element);
|
||||
String Typedef7_rep = getJsName(Typedef7_.element);
|
||||
String Typedef8_rep = getJsName(Typedef8_.element);
|
||||
String Typedef9_rep = getJsName(Typedef9_.element);
|
||||
String Typedef10_rep = getJsName(Typedef10_.element);
|
||||
|
||||
expect(int_, '$int_rep');
|
||||
expect(String_, '$String_rep');
|
||||
expect(dynamic_, 'null');
|
||||
|
||||
// List<E>
|
||||
expect(List_.computeType(env.compiler.resolution),
|
||||
'[$List_rep, $List_E_rep]');
|
||||
// List
|
||||
expect(List_.rawType, '$List_rep');
|
||||
// List<dynamic>
|
||||
expect(instantiate(List_, [dynamic_]), '$List_rep');
|
||||
// List<int>
|
||||
expect(instantiate(List_, [int_]), '[$List_rep, $int_rep]');
|
||||
// List<Typedef>
|
||||
expect(
|
||||
instantiate(List_, [Typedef_]),
|
||||
'[$List_rep, {$func: 1, $retvoid: true}]',
|
||||
'[$List_rep, {$func: 1, $retvoid: true,'
|
||||
' $typedefTag: $Typedef_rep}]');
|
||||
expect(
|
||||
instantiate(List_, [Typedef2_]),
|
||||
'[$List_rep, {$func: 1, $ret: $int_rep}]',
|
||||
'[$List_rep, {$func: 1, $ret: $int_rep,'
|
||||
' $typedefTag: $Typedef2_rep}]');
|
||||
expect(
|
||||
instantiate(List_, [Typedef3_]),
|
||||
'[$List_rep, {$func: 1, $ret: [$List_rep, $int_rep]}]',
|
||||
'[$List_rep, {$func: 1, $ret: [$List_rep, $int_rep],'
|
||||
' $typedefTag: $Typedef3_rep}]');
|
||||
expect(instantiate(List_, [Typedef4_]), '[$List_rep, {$func: 1}]',
|
||||
'[$List_rep, {$func: 1, $typedefTag: $Typedef4_rep}]');
|
||||
expect(
|
||||
instantiate(List_, [Typedef5_]),
|
||||
'[$List_rep, {$func: 1,'
|
||||
' $args: [$int_rep, $String_rep]}]',
|
||||
'[$List_rep, {$func: 1,'
|
||||
' $args: [$int_rep, $String_rep], $typedefTag: $Typedef5_rep}]');
|
||||
expect(
|
||||
instantiate(List_, [Typedef6_]),
|
||||
'[$List_rep, {$func: 1,'
|
||||
' $args: [$int_rep], $opt: [$String_rep]}]',
|
||||
'[$List_rep, {$func: 1,'
|
||||
' $args: [$int_rep], $opt: [$String_rep],'
|
||||
' $typedefTag: $Typedef6_rep}]');
|
||||
expect(
|
||||
instantiate(List_, [Typedef7_]),
|
||||
'[$List_rep, {$func: 1, $args: '
|
||||
'[$int_rep, $String_rep], $opt: [[$List_rep, $int_rep],,]}]',
|
||||
'[$List_rep, {$func: 1, $args: '
|
||||
'[$int_rep, $String_rep], $opt: [[$List_rep, $int_rep],,], '
|
||||
'$typedefTag: $Typedef7_rep}]');
|
||||
expect(
|
||||
instantiate(List_, [Typedef8_]),
|
||||
'[$List_rep, {$func: 1, $args: [$int_rep],'
|
||||
' $named: {b: $String_rep}}]',
|
||||
'[$List_rep, {$func: 1, $args: [$int_rep],'
|
||||
' $named: {b: $String_rep}, $typedefTag: $Typedef8_rep}]');
|
||||
expect(
|
||||
instantiate(List_, [Typedef9_]),
|
||||
'[$List_rep, {$func: 1, '
|
||||
'$args: [$int_rep, $String_rep], $named: '
|
||||
'{c: [$List_rep, $int_rep], d: null}}]',
|
||||
'[$List_rep, {$func: 1, '
|
||||
'$args: [$int_rep, $String_rep], $named: {c: [$List_rep, $int_rep],'
|
||||
' d: null}, $typedefTag: $Typedef9_rep}]');
|
||||
expect(
|
||||
instantiate(List_, [Typedef10_]),
|
||||
'[$List_rep, {$func: 1, '
|
||||
'$args: [{$func: 1, $retvoid: true, '
|
||||
'$args: [$int_rep], $opt: [,]}]}]',
|
||||
'[$List_rep, {$func: 1, '
|
||||
'$args: [{$func: 1, $retvoid: true, '
|
||||
'$args: [$int_rep], $opt: [,]}], $typedefTag: $Typedef10_rep}]');
|
||||
|
||||
// Map<K,V>
|
||||
expect(Map_.computeType(env.compiler.resolution),
|
||||
'[$Map_rep, $Map_K_rep, $Map_V_rep]');
|
||||
// Map
|
||||
expect(Map_.rawType, '$Map_rep');
|
||||
// Map<dynamic,dynamic>
|
||||
expect(instantiate(Map_, [dynamic_, dynamic_]), '$Map_rep');
|
||||
// Map<int,String>
|
||||
expect(instantiate(Map_, [int_, String_]),
|
||||
'[$Map_rep, $int_rep, $String_rep]');
|
||||
|
||||
// void m1() {}
|
||||
expect(computeType(env.getElement('m1'), env.compiler.resolution),
|
||||
'{$func: 1, $retvoid: true}');
|
||||
|
||||
// int m2() => 0;
|
||||
expect(computeType(env.getElement('m2'), env.compiler.resolution),
|
||||
'{$func: 1, $ret: $int_rep}');
|
||||
|
||||
// List<int> m3() => null;
|
||||
expect(computeType(env.getElement('m3'), env.compiler.resolution),
|
||||
'{$func: 1, $ret: [$List_rep, $int_rep]}');
|
||||
|
||||
// m4() {}
|
||||
expect(computeType(env.getElement('m4'), env.compiler.resolution),
|
||||
'{$func: 1}');
|
||||
|
||||
// m5(int a, String b) {}
|
||||
expect(computeType(env.getElement('m5'), env.compiler.resolution),
|
||||
'{$func: 1, $args: [$int_rep, $String_rep]}');
|
||||
|
||||
// m6(int a, [String b]) {}
|
||||
expect(
|
||||
computeType(env.getElement('m6'), env.compiler.resolution),
|
||||
'{$func: 1, $args: [$int_rep],'
|
||||
' $opt: [$String_rep]}');
|
||||
|
||||
// m7(int a, String b, [List<int> c, d]) {}
|
||||
expect(
|
||||
computeType(env.getElement('m7'), env.compiler.resolution),
|
||||
'{$func: 1,'
|
||||
' $args: [$int_rep, $String_rep],'
|
||||
' $opt: [[$List_rep, $int_rep],,]}');
|
||||
|
||||
// m8(int a, {String b}) {}
|
||||
expect(
|
||||
computeType(env.getElement('m8'), env.compiler.resolution),
|
||||
'{$func: 1,'
|
||||
' $args: [$int_rep], $named: {b: $String_rep}}');
|
||||
|
||||
// m9(int a, String b, {List<int> c, d}) {}
|
||||
expect(
|
||||
computeType(env.getElement('m9'), env.compiler.resolution),
|
||||
'{$func: 1,'
|
||||
' $args: [$int_rep, $String_rep],'
|
||||
' $named: {c: [$List_rep, $int_rep], d: null}}');
|
||||
|
||||
// m10(void f(int a, [b])) {}
|
||||
expect(
|
||||
computeType(env.getElement('m10'), env.compiler.resolution),
|
||||
'{$func: 1, $args:'
|
||||
' [{$func: 1,'
|
||||
' $retvoid: true, $args: [$int_rep], $opt: [,]}]}');
|
||||
}));
|
||||
testAll({bool useKernel, bool strongMode: false}) async {
|
||||
await testTypeRepresentations(useKernel: useKernel, strongMode: strongMode);
|
||||
}
|
||||
|
||||
computeType(element, resolution) {
|
||||
return element.computeType(resolution);
|
||||
List<FunctionTypeData> signatures = const <FunctionTypeData>[
|
||||
const FunctionTypeData("void", "1", "()"),
|
||||
const FunctionTypeData("int", "2", "()"),
|
||||
const FunctionTypeData("List<int>", "3", "()"),
|
||||
const FunctionTypeData("dynamic", "4", "()"),
|
||||
const FunctionTypeData("dynamic", "5", "(int a, String b)"),
|
||||
const FunctionTypeData("dynamic", "6", "(int a, [String b])"),
|
||||
const FunctionTypeData(
|
||||
"dynamic", "7", "(int a, String b, [List<int> c, dynamic d])"),
|
||||
const FunctionTypeData("dynamic", "8", "(int a, {String b})"),
|
||||
const FunctionTypeData(
|
||||
"dynamic", "9", "(int a, String b, {List<int> c, dynamic d})"),
|
||||
const FunctionTypeData(
|
||||
"dynamic", "10", "(void Function(int a, [dynamic b]) f)"),
|
||||
];
|
||||
|
||||
testTypeRepresentations({bool useKernel, bool strongMode}) async {
|
||||
String source = '''
|
||||
${createTypedefs(signatures, prefix: 'Typedef')}
|
||||
${createMethods(signatures, prefix: 'm')}
|
||||
|
||||
main() {
|
||||
${createUses(signatures, prefix: 'Typedef')}
|
||||
${createUses(signatures, prefix: 'm')}
|
||||
}
|
||||
''';
|
||||
CompilationResult result = await runCompiler(
|
||||
memorySourceFiles: {'main.dart': source},
|
||||
options: useKernel
|
||||
? (strongMode ? [Flags.strongMode] : [])
|
||||
: [Flags.useOldFrontend]);
|
||||
Expect.isTrue(result.isSuccess);
|
||||
Compiler compiler = result.compiler;
|
||||
JavaScriptBackend backend = compiler.backend;
|
||||
|
||||
TypeRepresentationGenerator typeRepresentation =
|
||||
new TypeRepresentationGenerator(backend.namer, false);
|
||||
|
||||
Expression onVariable(TypeVariableType _variable) {
|
||||
TypeVariableType variable = _variable;
|
||||
return new VariableUse(variable.element.name);
|
||||
}
|
||||
|
||||
String stringify(Expression expression) {
|
||||
return prettyPrint(expression,
|
||||
enableMinification: compiler.options.enableMinification);
|
||||
}
|
||||
|
||||
void expect(DartType type, String expectedRepresentation,
|
||||
[String expectedTypedefRepresentation]) {
|
||||
bool encodeTypedefName = false;
|
||||
Expression expression = typeRepresentation.getTypeRepresentation(
|
||||
backend.emitter.emitter, type, onVariable, (x) => encodeTypedefName);
|
||||
Expect.stringEquals(expectedRepresentation, stringify(expression));
|
||||
|
||||
encodeTypedefName = true;
|
||||
expression = typeRepresentation.getTypeRepresentation(
|
||||
backend.emitter.emitter, type, onVariable, (x) => encodeTypedefName);
|
||||
if (expectedTypedefRepresentation == null) {
|
||||
expectedTypedefRepresentation = expectedRepresentation;
|
||||
}
|
||||
Expect.stringEquals(expectedTypedefRepresentation, stringify(expression));
|
||||
}
|
||||
|
||||
String getJsName(Entity cls) {
|
||||
Expression name =
|
||||
typeRepresentation.getJavaScriptClassName(cls, backend.emitter.emitter);
|
||||
return stringify(name);
|
||||
}
|
||||
|
||||
ClosedWorld closedWorld = compiler.backendClosedWorldForTesting;
|
||||
ElementEnvironment elementEnvironment = closedWorld.elementEnvironment;
|
||||
String func = backend.namer.functionTypeTag;
|
||||
String retvoid = backend.namer.functionTypeVoidReturnTag;
|
||||
String ret = backend.namer.functionTypeReturnTypeTag;
|
||||
String args = backend.namer.functionTypeRequiredParametersTag;
|
||||
String opt = backend.namer.functionTypeOptionalParametersTag;
|
||||
String named = backend.namer.functionTypeNamedParametersTag;
|
||||
String typedefTag = backend.namer.typedefTag;
|
||||
|
||||
ClassEntity List_ = findClass(closedWorld, 'List');
|
||||
TypeVariableType List_E =
|
||||
elementEnvironment.getThisType(List_).typeArguments[0];
|
||||
ClassEntity Map_ = findClass(closedWorld, 'Map');
|
||||
TypeVariableType Map_K =
|
||||
elementEnvironment.getThisType(Map_).typeArguments[0];
|
||||
TypeVariableType Map_V =
|
||||
elementEnvironment.getThisType(Map_).typeArguments[1];
|
||||
|
||||
InterfaceType int_ = closedWorld.commonElements.intType;
|
||||
InterfaceType String_ = closedWorld.commonElements.stringType;
|
||||
DartType dynamic_ = closedWorld.commonElements.dynamicType;
|
||||
DartType Typedef1_ = findFieldType(closedWorld, 'Typedef1');
|
||||
DartType Typedef2_ = findFieldType(closedWorld, 'Typedef2');
|
||||
DartType Typedef3_ = findFieldType(closedWorld, 'Typedef3');
|
||||
DartType Typedef4_ = findFieldType(closedWorld, 'Typedef4');
|
||||
DartType Typedef5_ = findFieldType(closedWorld, 'Typedef5');
|
||||
DartType Typedef6_ = findFieldType(closedWorld, 'Typedef6');
|
||||
DartType Typedef7_ = findFieldType(closedWorld, 'Typedef7');
|
||||
DartType Typedef8_ = findFieldType(closedWorld, 'Typedef8');
|
||||
DartType Typedef9_ = findFieldType(closedWorld, 'Typedef9');
|
||||
DartType Typedef10_ = findFieldType(closedWorld, 'Typedef10');
|
||||
|
||||
String List_rep = getJsName(List_);
|
||||
String List_E_rep = stringify(onVariable(List_E));
|
||||
String Map_rep = getJsName(Map_);
|
||||
String Map_K_rep = stringify(onVariable(Map_K));
|
||||
String Map_V_rep = stringify(onVariable(Map_V));
|
||||
|
||||
String int_rep = getJsName(int_.element);
|
||||
String String_rep = getJsName(String_.element);
|
||||
|
||||
String getTypedefTag(DartType type) {
|
||||
if (useKernel) {
|
||||
// TODO(johnniwinther): Should/can we preserve typedef names from kernel?
|
||||
return '';
|
||||
}
|
||||
TypedefType typedef = type;
|
||||
return ', $typedefTag: ${getJsName(typedef.element)}';
|
||||
}
|
||||
|
||||
String Typedef1_tag = getTypedefTag(Typedef1_);
|
||||
String Typedef2_tag = getTypedefTag(Typedef2_);
|
||||
String Typedef3_tag = getTypedefTag(Typedef3_);
|
||||
String Typedef4_tag = getTypedefTag(Typedef4_);
|
||||
String Typedef5_tag = getTypedefTag(Typedef5_);
|
||||
String Typedef6_tag = getTypedefTag(Typedef6_);
|
||||
String Typedef7_tag = getTypedefTag(Typedef7_);
|
||||
String Typedef8_tag = getTypedefTag(Typedef8_);
|
||||
String Typedef9_tag = getTypedefTag(Typedef9_);
|
||||
String Typedef10_tag = getTypedefTag(Typedef10_);
|
||||
|
||||
expect(int_, '$int_rep');
|
||||
expect(String_, '$String_rep');
|
||||
expect(dynamic_, 'null');
|
||||
|
||||
// List<E>
|
||||
expect(elementEnvironment.getThisType(List_), '[$List_rep, $List_E_rep]');
|
||||
// List
|
||||
expect(elementEnvironment.getRawType(List_), '$List_rep');
|
||||
// List<dynamic>
|
||||
expect(instantiate(List_, [dynamic_]), '$List_rep');
|
||||
// List<int>
|
||||
expect(instantiate(List_, [int_]), '[$List_rep, $int_rep]');
|
||||
// List<Typedef>
|
||||
expect(
|
||||
instantiate(List_, [Typedef1_]),
|
||||
'[$List_rep, {$func: 1, $retvoid: true}]',
|
||||
'[$List_rep, {$func: 1, $retvoid: true$Typedef1_tag}]');
|
||||
expect(
|
||||
instantiate(List_, [Typedef2_]),
|
||||
'[$List_rep, {$func: 1, $ret: $int_rep}]',
|
||||
'[$List_rep, {$func: 1, $ret: $int_rep$Typedef2_tag}]');
|
||||
expect(
|
||||
instantiate(List_, [Typedef3_]),
|
||||
'[$List_rep, {$func: 1, $ret: [$List_rep, $int_rep]}]',
|
||||
'[$List_rep, {$func: 1, $ret: [$List_rep, $int_rep]$Typedef3_tag}]');
|
||||
expect(instantiate(List_, [Typedef4_]), '[$List_rep, {$func: 1}]',
|
||||
'[$List_rep, {$func: 1$Typedef4_tag}]');
|
||||
expect(
|
||||
instantiate(List_, [Typedef5_]),
|
||||
'[$List_rep, {$func: 1,'
|
||||
' $args: [$int_rep, $String_rep]}]',
|
||||
'[$List_rep, {$func: 1,'
|
||||
' $args: [$int_rep, $String_rep]$Typedef5_tag}]');
|
||||
expect(
|
||||
instantiate(List_, [Typedef6_]),
|
||||
'[$List_rep, {$func: 1,'
|
||||
' $args: [$int_rep], $opt: [$String_rep]}]',
|
||||
'[$List_rep, {$func: 1,'
|
||||
' $args: [$int_rep], $opt: [$String_rep]$Typedef6_tag}]');
|
||||
expect(
|
||||
instantiate(List_, [Typedef7_]),
|
||||
'[$List_rep, {$func: 1, $args: '
|
||||
'[$int_rep, $String_rep], $opt: [[$List_rep, $int_rep],,]}]',
|
||||
'[$List_rep, {$func: 1, $args: '
|
||||
'[$int_rep, $String_rep], $opt: [[$List_rep, $int_rep],,]'
|
||||
'$Typedef7_tag}]');
|
||||
expect(
|
||||
instantiate(List_, [Typedef8_]),
|
||||
'[$List_rep, {$func: 1, $args: [$int_rep],'
|
||||
' $named: {b: $String_rep}}]',
|
||||
'[$List_rep, {$func: 1, $args: [$int_rep],'
|
||||
' $named: {b: $String_rep}$Typedef8_tag}]');
|
||||
expect(
|
||||
instantiate(List_, [Typedef9_]),
|
||||
'[$List_rep, {$func: 1, '
|
||||
'$args: [$int_rep, $String_rep], $named: '
|
||||
'{c: [$List_rep, $int_rep], d: null}}]',
|
||||
'[$List_rep, {$func: 1, '
|
||||
'$args: [$int_rep, $String_rep], $named: {c: [$List_rep, $int_rep],'
|
||||
' d: null}$Typedef9_tag}]');
|
||||
expect(
|
||||
instantiate(List_, [Typedef10_]),
|
||||
'[$List_rep, {$func: 1, '
|
||||
'$args: [{$func: 1, $retvoid: true, '
|
||||
'$args: [$int_rep], $opt: [,]}]}]',
|
||||
'[$List_rep, {$func: 1, '
|
||||
'$args: [{$func: 1, $retvoid: true, '
|
||||
'$args: [$int_rep], $opt: [,]}]$Typedef10_tag}]');
|
||||
|
||||
// Map<K,V>
|
||||
expect(elementEnvironment.getThisType(Map_),
|
||||
'[$Map_rep, $Map_K_rep, $Map_V_rep]');
|
||||
// Map
|
||||
expect(elementEnvironment.getRawType(Map_), '$Map_rep');
|
||||
// Map<dynamic,dynamic>
|
||||
expect(instantiate(Map_, [dynamic_, dynamic_]), '$Map_rep');
|
||||
// Map<int,String>
|
||||
expect(
|
||||
instantiate(Map_, [int_, String_]), '[$Map_rep, $int_rep, $String_rep]');
|
||||
|
||||
// void m1() {}
|
||||
expect(findFunctionType(closedWorld, 'm1'), '{$func: 1, $retvoid: true}');
|
||||
|
||||
// int m2() => 0;
|
||||
expect(findFunctionType(closedWorld, 'm2'), '{$func: 1, $ret: $int_rep}');
|
||||
|
||||
// List<int> m3() => null;
|
||||
expect(findFunctionType(closedWorld, 'm3'),
|
||||
'{$func: 1, $ret: [$List_rep, $int_rep]}');
|
||||
|
||||
// m4() {}
|
||||
expect(findFunctionType(closedWorld, 'm4'), '{$func: 1}');
|
||||
|
||||
// m5(int a, String b) {}
|
||||
expect(findFunctionType(closedWorld, 'm5'),
|
||||
'{$func: 1, $args: [$int_rep, $String_rep]}');
|
||||
|
||||
// m6(int a, [String b]) {}
|
||||
expect(
|
||||
findFunctionType(closedWorld, 'm6'),
|
||||
'{$func: 1, $args: [$int_rep],'
|
||||
' $opt: [$String_rep]}');
|
||||
|
||||
// m7(int a, String b, [List<int> c, d]) {}
|
||||
expect(
|
||||
findFunctionType(closedWorld, 'm7'),
|
||||
'{$func: 1,'
|
||||
' $args: [$int_rep, $String_rep],'
|
||||
' $opt: [[$List_rep, $int_rep],,]}');
|
||||
|
||||
// m8(int a, {String b}) {}
|
||||
expect(
|
||||
findFunctionType(closedWorld, 'm8'),
|
||||
'{$func: 1,'
|
||||
' $args: [$int_rep], $named: {b: $String_rep}}');
|
||||
|
||||
// m9(int a, String b, {List<int> c, d}) {}
|
||||
expect(
|
||||
findFunctionType(closedWorld, 'm9'),
|
||||
'{$func: 1,'
|
||||
' $args: [$int_rep, $String_rep],'
|
||||
' $named: {c: [$List_rep, $int_rep], d: null}}');
|
||||
|
||||
// m10(void f(int a, [b])) {}
|
||||
expect(
|
||||
findFunctionType(closedWorld, 'm10'),
|
||||
'{$func: 1, $args:'
|
||||
' [{$func: 1,'
|
||||
' $retvoid: true, $args: [$int_rep], $opt: [,]}]}');
|
||||
}
|
||||
|
|
|
@ -351,3 +351,13 @@ String createTypedefs(List<FunctionTypeData> dataList,
|
|||
sb.write(additionalData);
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/// Return source code that uses the function types in [dataList].
|
||||
String createUses(List<FunctionTypeData> dataList, {String prefix: ''}) {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
for (int index = 0; index < dataList.length; index++) {
|
||||
FunctionTypeData data = dataList[index];
|
||||
sb.writeln('$prefix${data.name};');
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue