add TypeParameter interface, for function type parameters which are not Declarations

Bug: https://github.com/dart-lang/language/issues/3559
Change-Id: I98d20e9f3b84d89c985fddb10ed1dc5e7c651b7e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/346640
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Johnni Winther <johnniwinther@google.com>
Auto-Submit: Jake Macdonald <jakemac@google.com>
This commit is contained in:
Jake Macdonald 2024-02-09 08:35:28 +00:00 committed by Commit Queue
parent d82be109a2
commit 22264a24c8
11 changed files with 288 additions and 156 deletions

View file

@ -45,13 +45,13 @@ abstract interface class FunctionTypeAnnotation implements TypeAnnotation {
TypeAnnotation get returnType;
/// The positional parameters for this function.
Iterable<FunctionTypeParameter> get positionalParameters;
Iterable<FormalParameter> get positionalParameters;
/// The named parameters for this function.
Iterable<FunctionTypeParameter> get namedParameters;
Iterable<FormalParameter> get namedParameters;
/// The type parameters for this function.
Iterable<TypeParameterDeclaration> get typeParameters;
Iterable<TypeParameter> get typeParameters;
}
/// An unresolved reference to a type.
@ -267,10 +267,10 @@ abstract interface class FunctionDeclaration implements Declaration {
TypeAnnotation get returnType;
/// The positional parameters for this function.
Iterable<ParameterDeclaration> get positionalParameters;
Iterable<FormalParameterDeclaration> get positionalParameters;
/// The named parameters for this function.
Iterable<ParameterDeclaration> get namedParameters;
Iterable<FormalParameterDeclaration> get namedParameters;
/// The type parameters for this function.
Iterable<TypeParameterDeclaration> get typeParameters;
@ -308,9 +308,12 @@ abstract interface class FieldDeclaration
bool get hasAbstract;
}
/// General parameter introspection information, see the subtypes
/// [FunctionTypeParameter] and [ParameterDeclaration].
abstract interface class Parameter implements Annotatable {
/// General parameter introspection information, for both function type
/// parameters and regular parameters.
///
/// See the subtype [FormalParameterDeclaration] as well, for regular
/// parameters which are not a part of a function type.
abstract interface class FormalParameter implements Annotatable {
/// The type of this parameter.
TypeAnnotation get type;
@ -321,6 +324,11 @@ abstract interface class Parameter implements Annotatable {
/// parameter or an optional parameter with the `required` keyword.
bool get isRequired;
/// The name of this parameter, if present.
///
/// Specifically, function type parameters may not have a name.
String? get name;
/// A convenience method to get a `code` object equivalent to this parameter.
///
/// Note that the original default value will not be included, as it is not a
@ -328,27 +336,38 @@ abstract interface class Parameter implements Annotatable {
ParameterCode get code;
}
/// Parameters of normal functions/methods, which always have an identifier.
abstract interface class ParameterDeclaration
implements Parameter, Declaration {}
/// Function type parameters don't always have names, and it is never useful to
/// get an [Identifier] for them, so they do not implement [Declaration] and
/// instead have an optional name.
abstract interface class FunctionTypeParameter implements Parameter {
String? get name;
/// Parameters of normal functions/methods, which always have an identifier, and
/// declare a new variable in scope.
abstract interface class FormalParameterDeclaration
implements FormalParameter, Declaration {
@override
String get name;
}
/// Generic type parameter introspection information.
abstract interface class TypeParameterDeclaration implements TypeDeclaration {
///
/// Not all type parameters introduce new declarations that can be referenced,
/// but those that do will implement the [TypeParameterDeclaration] interface.
abstract interface class TypeParameter implements Annotatable {
/// The bound for this type parameter, if it has any.
TypeAnnotation? get bound;
/// The name of this type parameter.
String get name;
/// A convenience method to get a `code` object equivalent to this type
/// parameter.
TypeParameterCode get code;
}
/// Generic type parameter introspection information for type parameters which
/// introduce a true type declaration that can be referenced.
///
/// Note that type parameters for function types cannot be referenced and only
/// implement [TypeParameter].
abstract interface class TypeParameterDeclaration
implements TypeDeclaration, TypeParameter {}
/// Introspection information for a field declaration on a Record type.
///
/// Note that for positional fields the [identifier] will be the synthesized

View file

@ -601,7 +601,7 @@ DeclarationCode _buildFunctionAugmentation(
'>',
],
'(',
for (ParameterDeclaration positionalRequired in declaration
for (FormalParameterDeclaration positionalRequired in declaration
.positionalParameters
.takeWhile((p) => p.isRequired)) ...[
positionalRequired.code,
@ -609,7 +609,7 @@ DeclarationCode _buildFunctionAugmentation(
],
if (declaration.positionalParameters.any((p) => !p.isRequired)) ...[
'[',
for (ParameterDeclaration positionalOptional in declaration
for (FormalParameterDeclaration positionalOptional in declaration
.positionalParameters
.where((p) => !p.isRequired)) ...[
positionalOptional.code,
@ -619,7 +619,8 @@ DeclarationCode _buildFunctionAugmentation(
],
if (declaration.namedParameters.isNotEmpty) ...[
'{',
for (ParameterDeclaration named in declaration.namedParameters) ...[
for (FormalParameterDeclaration named
in declaration.namedParameters) ...[
named.code,
', ',
],

View file

@ -179,35 +179,34 @@ class FunctionTypeAnnotationImpl extends TypeAnnotationImpl
FunctionTypeAnnotationCode underlyingType = new FunctionTypeAnnotationCode(
returnType: returnType.code,
typeParameters: [
for (TypeParameterDeclaration typeParam in typeParameters)
typeParam.code,
for (TypeParameter typeParam in typeParameters) typeParam.code,
],
positionalParameters: [
for (FunctionTypeParameter positional in positionalParameters)
for (FormalParameter positional in positionalParameters)
if (positional.isRequired) positional.code,
],
optionalPositionalParameters: [
for (FunctionTypeParameter positional in positionalParameters)
for (FormalParameter positional in positionalParameters)
if (!positional.isRequired) positional.code,
],
namedParameters: [
for (FunctionTypeParameter named in namedParameters) named.code,
for (FormalParameter named in namedParameters) named.code,
],
);
return isNullable ? underlyingType.asNullable : underlyingType;
}
@override
final List<FunctionTypeParameterImpl> namedParameters;
final List<FormalParameterImpl> namedParameters;
@override
final List<FunctionTypeParameterImpl> positionalParameters;
final List<FormalParameterImpl> positionalParameters;
@override
final TypeAnnotationImpl returnType;
@override
final List<TypeParameterDeclarationImpl> typeParameters;
final List<TypeParameterImpl> typeParameters;
@override
RemoteInstanceKind get kind => RemoteInstanceKind.functionTypeAnnotation;
@ -228,19 +227,19 @@ class FunctionTypeAnnotationImpl extends TypeAnnotationImpl
returnType.serialize(serializer);
serializer.startList();
for (FunctionTypeParameterImpl param in positionalParameters) {
for (FormalParameterImpl param in positionalParameters) {
param.serialize(serializer);
}
serializer.endList();
serializer.startList();
for (FunctionTypeParameterImpl param in namedParameters) {
for (FormalParameterImpl param in namedParameters) {
param.serialize(serializer);
}
serializer.endList();
serializer.startList();
for (TypeParameterDeclarationImpl typeParam in typeParameters) {
for (TypeParameterImpl typeParam in typeParameters) {
typeParam.serialize(serializer);
}
serializer.endList();
@ -360,8 +359,8 @@ abstract class DeclarationImpl extends RemoteInstance implements Declaration {
}
}
class ParameterDeclarationImpl extends DeclarationImpl
implements ParameterDeclaration {
class FormalParameterDeclarationImpl extends DeclarationImpl
implements FormalParameterDeclaration {
@override
final bool isNamed;
@ -372,9 +371,12 @@ class ParameterDeclarationImpl extends DeclarationImpl
final TypeAnnotationImpl type;
@override
RemoteInstanceKind get kind => RemoteInstanceKind.parameterDeclaration;
RemoteInstanceKind get kind => RemoteInstanceKind.formalParameterDeclaration;
ParameterDeclarationImpl({
@override
String get name => identifier.name;
FormalParameterDeclarationImpl({
required super.id,
required super.identifier,
required super.library,
@ -400,8 +402,7 @@ class ParameterDeclarationImpl extends DeclarationImpl
]);
}
class FunctionTypeParameterImpl extends RemoteInstance
implements FunctionTypeParameter {
class FormalParameterImpl extends RemoteInstance implements FormalParameter {
@override
final bool isNamed;
@ -418,9 +419,9 @@ class FunctionTypeParameterImpl extends RemoteInstance
final TypeAnnotationImpl type;
@override
RemoteInstanceKind get kind => RemoteInstanceKind.functionTypeParameter;
RemoteInstanceKind get kind => RemoteInstanceKind.formalParameter;
FunctionTypeParameterImpl({
FormalParameterImpl({
required int id,
required this.isNamed,
required this.isRequired,
@ -452,6 +453,44 @@ class FunctionTypeParameterImpl extends RemoteInstance
]);
}
class TypeParameterImpl extends RemoteInstance implements TypeParameter {
@override
final TypeAnnotationImpl? bound;
@override
final List<MetadataAnnotationImpl> metadata;
@override
final String name;
@override
TypeParameterCode get code =>
new TypeParameterCode(name: name, bound: bound?.code);
@override
RemoteInstanceKind get kind => RemoteInstanceKind.typeParameter;
TypeParameterImpl({
required int id,
required this.bound,
required this.metadata,
required this.name,
}) : super(id);
@override
void serializeUncached(Serializer serializer) {
super.serializeUncached(serializer);
bound.serializeNullable(serializer);
serializer.startList();
for (MetadataAnnotationImpl annotation in metadata) {
annotation.serialize(serializer);
}
serializer.endList();
serializer.addString(name);
}
}
class TypeParameterDeclarationImpl extends DeclarationImpl
implements TypeParameterDeclaration {
@override
@ -460,6 +499,9 @@ class TypeParameterDeclarationImpl extends DeclarationImpl
@override
RemoteInstanceKind get kind => RemoteInstanceKind.typeParameterDeclaration;
@override
String get name => identifier.name;
TypeParameterDeclarationImpl({
required super.id,
required super.identifier,
@ -472,12 +514,7 @@ class TypeParameterDeclarationImpl extends DeclarationImpl
void serializeUncached(Serializer serializer) {
super.serializeUncached(serializer);
TypeAnnotationImpl? bound = this.bound;
if (bound == null) {
serializer.addNull();
} else {
bound.serialize(serializer);
}
bound.serializeNullable(serializer);
}
@override
@ -503,10 +540,10 @@ class FunctionDeclarationImpl extends DeclarationImpl
final bool isSetter;
@override
final List<ParameterDeclarationImpl> namedParameters;
final List<FormalParameterDeclarationImpl> namedParameters;
@override
final List<ParameterDeclarationImpl> positionalParameters;
final List<FormalParameterDeclarationImpl> positionalParameters;
@override
final TypeAnnotationImpl returnType;
@ -547,13 +584,13 @@ class FunctionDeclarationImpl extends DeclarationImpl
..addBool(isSetter);
}
serializer.startList();
for (ParameterDeclarationImpl named in namedParameters) {
for (FormalParameterDeclarationImpl named in namedParameters) {
named.serialize(serializer);
}
serializer
..endList()
..startList();
for (ParameterDeclarationImpl positional in positionalParameters) {
for (FormalParameterDeclarationImpl positional in positionalParameters) {
positional.serialize(serializer);
}
serializer.endList();

View file

@ -109,9 +109,10 @@ enum RemoteInstanceKind {
extensionDeclaration,
extensionTypeDeclaration,
fieldDeclaration,
formalParameter,
formalParameterDeclaration,
functionDeclaration,
functionTypeAnnotation,
functionTypeParameter,
identifier,
identifierMetadataAnnotation,
library,
@ -120,11 +121,11 @@ enum RemoteInstanceKind {
namedStaticType,
namedTypeAnnotation,
omittedTypeAnnotation,
parameterDeclaration,
recordFieldDeclaration,
recordTypeAnnotation,
staticType,
typeAliasDeclaration,
typeParameter,
typeParameterDeclaration,
typePhaseIntrospector,
variableDeclaration,

View file

@ -47,8 +47,8 @@ extension DeserializerExtensions on Deserializer {
(this..moveNext())._expectFunctionDeclaration(id),
RemoteInstanceKind.functionTypeAnnotation =>
(this..moveNext())._expectFunctionTypeAnnotation(id),
RemoteInstanceKind.functionTypeParameter =>
(this..moveNext())._expectFunctionTypeParameter(id),
RemoteInstanceKind.formalParameter =>
(this..moveNext())._expectFormalParameter(id),
RemoteInstanceKind.identifier => (this..moveNext())._expectIdentifier(id),
RemoteInstanceKind.identifierMetadataAnnotation =>
(this..moveNext())._expectIdentifierMetadataAnnotation(id),
@ -59,14 +59,16 @@ extension DeserializerExtensions on Deserializer {
(this..moveNext())._expectNamedTypeAnnotation(id),
RemoteInstanceKind.omittedTypeAnnotation =>
(this..moveNext())._expectOmittedTypeAnnotation(id),
RemoteInstanceKind.parameterDeclaration =>
(this..moveNext())._expectParameterDeclaration(id),
RemoteInstanceKind.formalParameterDeclaration =>
(this..moveNext())._expectFormalParameterDeclaration(id),
RemoteInstanceKind.recordFieldDeclaration =>
(this..moveNext())._expectRecordFieldDeclaration(id),
RemoteInstanceKind.recordTypeAnnotation =>
(this..moveNext())._expectRecordTypeAnnotation(id),
RemoteInstanceKind.typeAliasDeclaration =>
(this..moveNext())._expectTypeAliasDeclaration(id),
RemoteInstanceKind.typeParameter =>
(this..moveNext())._expectTypeParameter(id),
RemoteInstanceKind.typeParameterDeclaration =>
(this..moveNext())._expectTypeParameterDeclaration(id),
RemoteInstanceKind.variableDeclaration =>
@ -136,8 +138,7 @@ extension DeserializerExtensions on Deserializer {
typeParameters: (this..moveNext())._expectRemoteInstanceList(),
);
FunctionTypeParameterImpl _expectFunctionTypeParameter(int id) =>
new FunctionTypeParameterImpl(
FormalParameterImpl _expectFormalParameter(int id) => new FormalParameterImpl(
id: id,
isNamed: expectBool(),
isRequired: (this..moveNext()).expectBool(),
@ -151,8 +152,8 @@ extension DeserializerExtensions on Deserializer {
name: expectString(),
);
ParameterDeclarationImpl _expectParameterDeclaration(int id) =>
new ParameterDeclarationImpl(
FormalParameterDeclarationImpl _expectFormalParameterDeclaration(int id) =>
new FormalParameterDeclarationImpl(
id: id,
identifier: expectRemoteInstance(),
library: RemoteInstance.deserialize(this),
@ -179,6 +180,13 @@ extension DeserializerExtensions on Deserializer {
positionalFields: (this..moveNext())._expectRemoteInstanceList(),
);
TypeParameterImpl _expectTypeParameter(int id) => new TypeParameterImpl(
id: id,
bound: checkNull() ? null : expectRemoteInstance(),
metadata: (this..moveNext())._expectRemoteInstanceList(),
name: (this..moveNext()).expectString(),
);
TypeParameterDeclarationImpl _expectTypeParameterDeclaration(int id) =>
new TypeParameterDeclarationImpl(
id: id,

View file

@ -107,8 +107,8 @@ void checkTypeAnnotation(
}
}
void checkParameterDeclaration(
ParameterData expected, ParameterDeclaration declaration, String context) {
void checkParameterDeclaration(ParameterData expected,
FormalParameterDeclaration declaration, String context) {
expect(
expected.name, declaration.identifier.name, '$context.identifier.name');
expect(expected.isNamed, declaration.isNamed, '$context.isNamed');

View file

@ -155,7 +155,7 @@ void main() {
fooType, mode, RemoteInstance.deserialize);
});
final fooNamedParam = ParameterDeclarationImpl(
final fooNamedParam = FormalParameterDeclarationImpl(
id: RemoteInstance.uniqueId,
isNamed: true,
isRequired: true,
@ -164,7 +164,7 @@ void main() {
library: Fixtures.library,
metadata: [],
type: fooType);
final fooNamedFunctionTypeParam = FunctionTypeParameterImpl(
final fooNamedFunctionTypeParam = FormalParameterImpl(
id: RemoteInstance.uniqueId,
isNamed: true,
isRequired: true,
@ -172,7 +172,7 @@ void main() {
name: 'foo',
type: fooType);
final barPositionalParam = ParameterDeclarationImpl(
final barPositionalParam = FormalParameterDeclarationImpl(
id: RemoteInstance.uniqueId,
isNamed: false,
isRequired: false,
@ -181,7 +181,7 @@ void main() {
library: Fixtures.library,
metadata: [],
type: barType);
final barPositionalFunctionTypeParam = FunctionTypeParameterImpl(
final barPositionalFunctionTypeParam = FormalParameterImpl(
id: RemoteInstance.uniqueId,
isNamed: true,
isRequired: true,
@ -189,7 +189,7 @@ void main() {
name: 'bar',
type: fooType);
final unnamedFunctionTypeParam = FunctionTypeParameterImpl(
final unnamedFunctionTypeParam = FormalParameterImpl(
id: RemoteInstance.uniqueId,
isNamed: true,
isRequired: true,
@ -217,7 +217,13 @@ void main() {
],
positionalParameters: [barPositionalFunctionTypeParam],
returnType: fooType,
typeParameters: [zapTypeParam],
typeParameters: [
TypeParameterImpl(
id: RemoteInstance.uniqueId,
metadata: [],
name: 'Zip',
bound: barType)
],
);
expectSerializationEquality<TypeAnnotationImpl>(
functionType, mode, RemoteInstance.deserialize);

View file

@ -433,7 +433,7 @@ class Fixtures {
isSetter: true,
namedParameters: [],
positionalParameters: [
ParameterDeclarationImpl(
FormalParameterDeclarationImpl(
id: RemoteInstance.uniqueId,
identifier:
IdentifierImpl(id: RemoteInstance.uniqueId, name: 'value'),
@ -510,7 +510,7 @@ class Fixtures {
hasExternal: false,
namedParameters: [],
positionalParameters: [
ParameterDeclarationImpl(
FormalParameterDeclarationImpl(
id: RemoteInstance.uniqueId,
identifier:
IdentifierImpl(id: RemoteInstance.uniqueId, name: 'myField'),
@ -620,7 +620,7 @@ class Fixtures {
hasExternal: false,
namedParameters: [],
positionalParameters: [
ParameterDeclarationImpl(
FormalParameterDeclarationImpl(
id: RemoteInstance.uniqueId,
identifier:
IdentifierImpl(id: RemoteInstance.uniqueId, name: 'myField'),

View file

@ -545,7 +545,10 @@ class DeclarationBuilderFromElement {
Map.identity();
final Map<TypeParameterElement, macro.TypeParameterDeclarationImpl>
_typeParameterMap = Map.identity();
_typeParameterDeclarationMap = Map.identity();
final Map<TypeParameterElement, macro.TypeParameterImpl> _typeParameterMap =
Map.identity();
final Map<TopLevelVariableElement, VariableDeclarationImpl> _variableMap =
Map.identity();
@ -689,12 +692,19 @@ class DeclarationBuilderFromElement {
}
}
macro.TypeParameterDeclarationImpl typeParameter(
macro.TypeParameterImpl typeParameter(
TypeParameterElement element,
) {
return _typeParameterMap[element] ??= _typeParameter(element);
}
macro.TypeParameterDeclarationImpl typeParameterDeclaration(
TypeParameterElement element,
) {
return _typeParameterDeclarationMap[element] ??=
_typeParameterDeclaration(element);
}
List<macro.MetadataAnnotationImpl> _buildMetadata(Element element) {
return builder._buildMetadata(element);
}
@ -707,7 +717,7 @@ class DeclarationBuilderFromElement {
identifier: identifier(element),
library: library(element),
metadata: _buildMetadata(element),
typeParameters: _typeParameters(element.typeParameters),
typeParameters: _typeParameterDeclarations(element.typeParameters),
interfaces: element.interfaces.map(_interfaceType).toList(),
hasAbstract: element.isAbstract,
hasBase: element.isBase,
@ -738,7 +748,7 @@ class DeclarationBuilderFromElement {
namedParameters: _namedFormalParameters(element.parameters),
positionalParameters: _positionalFormalParameters(element.parameters),
returnType: _dartType(element.returnType),
typeParameters: _typeParameters(element.typeParameters),
typeParameters: _typeParameterDeclarations(element.typeParameters),
definingType: identifier(enclosing),
);
}
@ -813,7 +823,7 @@ class DeclarationBuilderFromElement {
identifier: identifier(element),
library: library(element),
metadata: _buildMetadata(element),
typeParameters: _typeParameters(element.typeParameters),
typeParameters: _typeParameterDeclarations(element.typeParameters),
interfaces: element.interfaces.map(_interfaceType).toList(),
mixins: element.mixins.map(_interfaceType).toList(),
element: element,
@ -828,7 +838,7 @@ class DeclarationBuilderFromElement {
identifier: identifier(element),
library: library(element),
metadata: _buildMetadata(element),
typeParameters: _typeParameters(element.typeParameters),
typeParameters: _typeParameterDeclarations(element.typeParameters),
onType: _dartType(element.extendedType),
element: element,
);
@ -842,7 +852,7 @@ class DeclarationBuilderFromElement {
identifier: identifier(element),
library: library(element),
metadata: _buildMetadata(element),
typeParameters: _typeParameters(element.typeParameters),
typeParameters: _typeParameterDeclarations(element.typeParameters),
representationType: _dartType(element.representation.type),
element: element,
);
@ -866,8 +876,9 @@ class DeclarationBuilderFromElement {
);
}
macro.ParameterDeclarationImpl _formalParameter(ParameterElement element) {
return macro.ParameterDeclarationImpl(
macro.FormalParameterDeclarationImpl _formalParameter(
ParameterElement element) {
return macro.FormalParameterDeclarationImpl(
id: macro.RemoteInstance.uniqueId,
identifier: identifier(element),
isNamed: element.isNamed,
@ -893,14 +904,14 @@ class DeclarationBuilderFromElement {
namedParameters: _namedFormalParameters(element.parameters),
positionalParameters: _positionalFormalParameters(element.parameters),
returnType: _dartType(element.returnType),
typeParameters: _typeParameters(element.typeParameters),
typeParameters: _typeParameterDeclarations(element.typeParameters),
);
}
macro.FunctionTypeParameterImpl _functionTypeFormalParameter(
macro.FormalParameterImpl _functionTypeFormalParameter(
ParameterElement element,
) {
return macro.FunctionTypeParameterImpl(
return macro.FormalParameterImpl(
id: macro.RemoteInstance.uniqueId,
isNamed: element.isNamed,
isRequired: element.isRequired,
@ -936,7 +947,7 @@ class DeclarationBuilderFromElement {
namedParameters: _namedFormalParameters(element.parameters),
positionalParameters: _positionalFormalParameters(element.parameters),
returnType: _dartType(element.returnType),
typeParameters: _typeParameters(element.typeParameters),
typeParameters: _typeParameterDeclarations(element.typeParameters),
definingType: identifier(enclosing),
);
}
@ -949,7 +960,7 @@ class DeclarationBuilderFromElement {
identifier: identifier(element),
library: library(element),
metadata: _buildMetadata(element),
typeParameters: _typeParameters(element.typeParameters),
typeParameters: _typeParameterDeclarations(element.typeParameters),
hasBase: element.isBase,
interfaces: element.interfaces.map(_interfaceType).toList(),
superclassConstraints:
@ -958,7 +969,7 @@ class DeclarationBuilderFromElement {
);
}
List<macro.ParameterDeclarationImpl> _namedFormalParameters(
List<macro.FormalParameterDeclarationImpl> _namedFormalParameters(
List<ParameterElement> elements,
) {
return elements
@ -967,7 +978,7 @@ class DeclarationBuilderFromElement {
.toList();
}
List<macro.ParameterDeclarationImpl> _positionalFormalParameters(
List<macro.FormalParameterDeclarationImpl> _positionalFormalParameters(
List<ParameterElement> elements,
) {
return elements
@ -992,7 +1003,18 @@ class DeclarationBuilderFromElement {
);
}
macro.TypeParameterDeclarationImpl _typeParameter(
macro.TypeParameterImpl _typeParameter(
TypeParameterElement element,
) {
return macro.TypeParameterImpl(
id: macro.RemoteInstance.uniqueId,
name: identifier(element).name,
metadata: _buildMetadata(element),
bound: element.bound.mapOrNull(_dartType),
);
}
macro.TypeParameterDeclarationImpl _typeParameterDeclaration(
TypeParameterElement element,
) {
return macro.TypeParameterDeclarationImpl(
@ -1004,7 +1026,13 @@ class DeclarationBuilderFromElement {
);
}
List<macro.TypeParameterDeclarationImpl> _typeParameters(
List<macro.TypeParameterDeclarationImpl> _typeParameterDeclarations(
List<TypeParameterElement> elements,
) {
return elements.map(typeParameterDeclaration).toList();
}
List<macro.TypeParameterImpl> _typeParameters(
List<TypeParameterElement> elements,
) {
return elements.map(typeParameter).toList();
@ -1043,7 +1071,7 @@ class DeclarationBuilderFromNode {
identifier: _declaredIdentifier(node.name, element),
library: library(element),
metadata: _buildMetadata(element),
typeParameters: _typeParameters(node.typeParameters),
typeParameters: _typeParameterDeclarations(node.typeParameters),
interfaces: _namedTypes(
interfaceNodes,
ImplementsClauseTypeLocation(classTypeLocation),
@ -1090,7 +1118,7 @@ class DeclarationBuilderFromNode {
identifier: _declaredIdentifier(node.name, element),
library: library(element),
metadata: _buildMetadata(element),
typeParameters: _typeParameters(node.typeParameters),
typeParameters: _typeParameterDeclarations(node.typeParameters),
interfaces: _namedTypes(
interfaceNodes,
ImplementsClauseTypeLocation(classTypeLocation),
@ -1196,7 +1224,7 @@ class DeclarationBuilderFromNode {
identifier: _declaredIdentifier(node.name, element),
library: library(element),
metadata: _buildMetadata(element),
typeParameters: _typeParameters(node.typeParameters),
typeParameters: _typeParameterDeclarations(node.typeParameters),
interfaces: _namedTypes(
interfaceNodes,
ImplementsClauseTypeLocation(enumTypeLocation),
@ -1219,7 +1247,7 @@ class DeclarationBuilderFromNode {
identifier: _declaredIdentifier2(node.name?.lexeme ?? '', element),
library: library(element),
metadata: _buildMetadata(element),
typeParameters: _typeParameters(node.typeParameters),
typeParameters: _typeParameterDeclarations(node.typeParameters),
onType: _typeAnnotation(
node.extendedType,
ExtensionElementOnTypeLocation(element),
@ -1238,7 +1266,7 @@ class DeclarationBuilderFromNode {
identifier: _declaredIdentifier2(node.name.lexeme, element),
library: library(element),
metadata: _buildMetadata(element),
typeParameters: _typeParameters(node.typeParameters),
typeParameters: _typeParameterDeclarations(node.typeParameters),
representationType: _typeAnnotation(
node.representation.fieldType,
ExtensionTypeElementRepresentationTypeLocation(element),
@ -1270,7 +1298,7 @@ class DeclarationBuilderFromNode {
namedParameters: namedParameters,
positionalParameters: positionalParameters,
returnType: _typeAnnotationFunctionReturnType(node),
typeParameters: _typeParameters(function.typeParameters),
typeParameters: _typeParameterDeclarations(function.typeParameters),
);
}
@ -1332,7 +1360,7 @@ class DeclarationBuilderFromNode {
identifier: _declaredIdentifier(node.name, element),
library: library(element),
metadata: _buildMetadata(element),
typeParameters: _typeParameters(node.typeParameters),
typeParameters: _typeParameterDeclarations(node.typeParameters),
hasBase: node.baseKeyword != null,
interfaces: _namedTypes(
interfaceNodes,
@ -1513,13 +1541,15 @@ class DeclarationBuilderFromNode {
);
}
(List<macro.ParameterDeclarationImpl>, List<macro.ParameterDeclarationImpl>)
_executableFormalParameters(
(
List<macro.FormalParameterDeclarationImpl>,
List<macro.FormalParameterDeclarationImpl>
) _executableFormalParameters(
ExecutableElement element,
ast.FormalParameterList? node,
) {
var named = <macro.ParameterDeclarationImpl>[];
var positional = <macro.ParameterDeclarationImpl>[];
var named = <macro.FormalParameterDeclarationImpl>[];
var positional = <macro.FormalParameterDeclarationImpl>[];
if (node != null) {
var elementLocation = ElementTypeLocation(element);
for (var (index, node) in node.parameters.indexed) {
@ -1537,7 +1567,7 @@ class DeclarationBuilderFromNode {
return (named, positional);
}
macro.ParameterDeclarationImpl _formalParameter(
macro.FormalParameterDeclarationImpl _formalParameter(
ast.FormalParameter node,
TypeAnnotationLocation location,
) {
@ -1557,7 +1587,7 @@ class DeclarationBuilderFromNode {
throw UnimplementedError('(${node.runtimeType}) $node');
}
return macro.ParameterDeclarationImpl(
return macro.FormalParameterDeclarationImpl(
id: macro.RemoteInstance.uniqueId,
identifier: _declaredIdentifier(node.name!, element),
isNamed: node.isNamed,
@ -1568,7 +1598,7 @@ class DeclarationBuilderFromNode {
);
}
macro.FunctionTypeParameterImpl _functionTypeFormalParameter(
macro.FormalParameterImpl _functionTypeFormalParameter(
ast.FormalParameter node,
TypeAnnotationLocation location,
) {
@ -1585,7 +1615,7 @@ class DeclarationBuilderFromNode {
throw UnimplementedError('(${node.runtimeType}) $node');
}
return macro.FunctionTypeParameterImpl(
return macro.FormalParameterImpl(
id: macro.RemoteInstance.uniqueId,
isNamed: node.isNamed,
isRequired: node.isRequired,
@ -1620,7 +1650,7 @@ class DeclarationBuilderFromNode {
namedParameters: namedParameters,
positionalParameters: positionalParameters,
returnType: _typeAnnotationMethodReturnType(node),
typeParameters: _typeParameters(node.typeParameters),
typeParameters: _typeParameterDeclarations(node.typeParameters),
);
}
@ -1675,8 +1705,8 @@ class DeclarationBuilderFromNode {
node as ast.TypeAnnotationImpl;
switch (node) {
case ast.GenericFunctionTypeImpl():
var namedParameters = <macro.FunctionTypeParameterImpl>[];
var positionalParameters = <macro.FunctionTypeParameterImpl>[];
var namedParameters = <macro.FormalParameterImpl>[];
var positionalParameters = <macro.FormalParameterImpl>[];
var formalParameters = node.parameters.parameters;
for (var (index, node) in formalParameters.indexed) {
var formalParameter = _functionTypeFormalParameter(
@ -1827,7 +1857,24 @@ class DeclarationBuilderFromNode {
return _typeAnnotation(type, location);
}
macro.TypeParameterDeclarationImpl _typeParameter(
macro.TypeParameterImpl _typeParameter(
ast.TypeParameter node,
) {
final element = node.declaredElement!;
return macro.TypeParameterImpl(
id: macro.RemoteInstance.uniqueId,
name: node.name.lexeme,
metadata: _buildMetadata(element),
bound: node.bound.mapOrNull((type) {
return _typeAnnotation(
type,
TypeParameterBoundLocation(),
);
}),
);
}
macro.TypeParameterDeclarationImpl _typeParameterDeclaration(
ast.TypeParameter node,
) {
final element = node.declaredElement!;
@ -1845,7 +1892,19 @@ class DeclarationBuilderFromNode {
);
}
List<macro.TypeParameterDeclarationImpl> _typeParameters(
List<macro.TypeParameterDeclarationImpl> _typeParameterDeclarations(
ast.TypeParameterList? typeParameterList,
) {
if (typeParameterList != null) {
return typeParameterList.typeParameters
.map(_typeParameterDeclaration)
.toList();
} else {
return const [];
}
}
List<macro.TypeParameterImpl> _typeParameters(
ast.TypeParameterList? typeParameterList,
) {
if (typeParameterList != null) {

View file

@ -689,7 +689,8 @@ class _Printer {
});
}
Future<void> _writeFormalParameter(ParameterDeclaration e) async {
Future<void> _writeFormalParameterDeclaration(
FormalParameterDeclaration e) async {
sink.writelnWithIndent(e.identifier.name);
await sink.withIndent(() async {
await sink.writeFlags({
@ -760,12 +761,12 @@ class _Printer {
}
Future<void> _writeNamedFormalParameters(
Iterable<ParameterDeclaration> elements,
Iterable<FormalParameterDeclaration> elements,
) async {
await sink.writeElements(
'namedParameters',
elements,
_writeFormalParameter,
_writeFormalParameterDeclaration,
);
}
@ -778,12 +779,12 @@ class _Printer {
}
Future<void> _writePositionalFormalParameters(
Iterable<ParameterDeclaration> elements,
Iterable<FormalParameterDeclaration> elements,
) async {
await sink.writeElements(
'positionalParameters',
elements,
_writeFormalParameter,
_writeFormalParameterDeclaration,
);
}
@ -910,38 +911,7 @@ class _TypeAnnotationStringBuilder {
}
}
void _writeFunctionTypeAnnotation(FunctionTypeAnnotation type) {
write(type.returnType);
_sink.write(' Function');
_sink.writeList(
elements: type.typeParameters,
write: _writeTypeParameter,
separator: ', ',
open: '<',
close: '>',
);
_sink.write('(');
var hasFormalParameter = false;
for (final formalParameter in type.positionalParameters) {
if (hasFormalParameter) {
_sink.write(', ');
}
_writeFunctionTypeParameter(formalParameter);
hasFormalParameter = true;
}
for (final formalParameter in type.namedParameters) {
if (hasFormalParameter) {
_sink.write(', ');
}
_writeFunctionTypeParameter(formalParameter);
hasFormalParameter = true;
}
_sink.write(')');
}
void _writeFunctionTypeParameter(FunctionTypeParameter node) {
void _writeFormalParameter(FormalParameter node) {
final String closeSeparator;
if (node.isNamed) {
_sink.write('{');
@ -965,6 +935,37 @@ class _TypeAnnotationStringBuilder {
_sink.write(closeSeparator);
}
void _writeFunctionTypeAnnotation(FunctionTypeAnnotation type) {
write(type.returnType);
_sink.write(' Function');
_sink.writeList(
elements: type.typeParameters,
write: _writeTypeParameter,
separator: ', ',
open: '<',
close: '>',
);
_sink.write('(');
var hasFormalParameter = false;
for (final formalParameter in type.positionalParameters) {
if (hasFormalParameter) {
_sink.write(', ');
}
_writeFormalParameter(formalParameter);
hasFormalParameter = true;
}
for (final formalParameter in type.namedParameters) {
if (hasFormalParameter) {
_sink.write(', ');
}
_writeFormalParameter(formalParameter);
hasFormalParameter = true;
}
_sink.write(')');
}
void _writeNamedTypeAnnotation(NamedTypeAnnotation type) {
_sink.write(type.identifier.name);
_sink.writeList(
@ -976,8 +977,8 @@ class _TypeAnnotationStringBuilder {
);
}
void _writeTypeParameter(TypeParameterDeclaration node) {
_sink.write(node.identifier.name);
void _writeTypeParameter(TypeParameter node) {
_sink.write(node.name);
final bound = node.bound;
if (bound != null) {

View file

@ -246,10 +246,10 @@ class MacroIntrospection {
return declaration;
}
List<List<macro.ParameterDeclarationImpl>> _createParameters(
List<List<macro.FormalParameterDeclarationImpl>> _createParameters(
MemberBuilder builder, List<FormalParameterBuilder>? formals) {
List<macro.ParameterDeclarationImpl>? positionalParameters;
List<macro.ParameterDeclarationImpl>? namedParameters;
List<macro.FormalParameterDeclarationImpl>? positionalParameters;
List<macro.FormalParameterDeclarationImpl>? namedParameters;
if (formals == null) {
positionalParameters = namedParameters = const [];
} else {
@ -265,7 +265,7 @@ class MacroIntrospection {
parameterBuilder: formal,
libraryBuilder: builder.libraryBuilder);
if (formal.isNamed) {
namedParameters.add(new macro.ParameterDeclarationImpl(
namedParameters.add(new macro.FormalParameterDeclarationImpl(
id: macro.RemoteInstance.uniqueId,
identifier: identifier,
library: library,
@ -276,7 +276,7 @@ class MacroIntrospection {
type: type,
));
} else {
positionalParameters.add(new macro.ParameterDeclarationImpl(
positionalParameters.add(new macro.FormalParameterDeclarationImpl(
id: macro.RemoteInstance.uniqueId,
identifier: identifier,
library: library,
@ -299,7 +299,7 @@ class MacroIntrospection {
if (builder is DeclaredSourceConstructorBuilder) {
formals = builder.formals;
}
List<List<macro.ParameterDeclarationImpl>> parameters =
List<List<macro.FormalParameterDeclarationImpl>> parameters =
_createParameters(builder, formals);
macro.ParameterizedTypeDeclaration definingClass =
getClassDeclaration(builder.classBuilder!);
@ -328,7 +328,7 @@ class MacroIntrospection {
macro.ConstructorDeclaration _createFactoryDeclaration(
SourceFactoryBuilder builder) {
List<List<macro.ParameterDeclarationImpl>> parameters =
List<List<macro.FormalParameterDeclarationImpl>> parameters =
_createParameters(builder, builder.formals);
macro.ParameterizedTypeDeclaration definingClass =
// TODO(johnniwinther): Support extension type factories.
@ -359,7 +359,7 @@ class MacroIntrospection {
macro.FunctionDeclaration _createFunctionDeclaration(
SourceProcedureBuilder builder) {
List<List<macro.ParameterDeclarationImpl>> parameters =
List<List<macro.FormalParameterDeclarationImpl>> parameters =
_createParameters(builder, builder.formals);
macro.ParameterizedTypeDeclaration? definingClass = null;