Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass target model in AugmentRequest #104

Merged
merged 8 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions pkgs/_analyzer_macros/lib/macro_implementation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,17 @@ class AnalyzerRunningMacro implements injected.RunningMacro {
return AnalyzerRunningMacro._(impl, name, implementation);
}

/// Queries for [target] and returns the [Model] representing the result.
///
/// TODO: Make this a more limited query which doesn't fetch members.
Future<Model> _queryTarget(QualifiedName target) =>
Scope.query.run(() async => (await _impl._host.hostService.handle(
MacroRequest.queryRequest(
QueryRequest(query: Query(target: target)),
id: nextRequestId)))
.asQueryResponse
.model);

@override
Future<AnalyzerMacroExecutionResult> executeDeclarationsPhase(
macros_api_v1.MacroTarget target,
Expand All @@ -91,7 +102,11 @@ class AnalyzerRunningMacro implements injected.RunningMacro {
return await AnalyzerMacroExecutionResult.dartModelToInjected(
target,
await _impl._host.augment(
name, AugmentRequest(phase: 2, target: target.qualifiedName)));
name,
AugmentRequest(
phase: 2,
target: target.qualifiedName,
model: await _queryTarget(target.qualifiedName))));
}

@override
Expand All @@ -104,7 +119,11 @@ class AnalyzerRunningMacro implements injected.RunningMacro {
return await AnalyzerMacroExecutionResult.dartModelToInjected(
target,
await _impl._host.augment(
name, AugmentRequest(phase: 3, target: target.qualifiedName)));
name,
AugmentRequest(
phase: 3,
target: target.qualifiedName,
model: await _queryTarget(target.qualifiedName))));
}

@override
Expand All @@ -116,7 +135,11 @@ class AnalyzerRunningMacro implements injected.RunningMacro {
return await AnalyzerMacroExecutionResult.dartModelToInjected(
target,
await _impl._host.augment(
name, AugmentRequest(phase: 1, target: target.qualifiedName)));
name,
AugmentRequest(
phase: 1,
target: target.qualifiedName,
model: await _queryTarget(target.qualifiedName))));
}
}

Expand Down
96 changes: 50 additions & 46 deletions pkgs/_analyzer_macros/lib/query_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,52 +36,56 @@ class AnalyzerQueryService implements QueryService {
..addInterfaceElement(clazz);

final interface = Interface(properties: Properties(isClass: true));
for (final constructor in clazz.constructors) {
interface.members[constructor.name] = Member(
requiredPositionalParameters: constructor
.requiredPositionalParameters(types.translator, types.context),
optionalPositionalParameters: constructor
.optionalPositionalParameters(types.translator, types.context),
namedParameters:
constructor.namedParameters(types.translator, types.context),
properties: Properties(
isAbstract: constructor.isAbstract,
isConstructor: true,
isGetter: false,
isField: false,
isMethod: false,
isStatic: false,
));
}
for (final field in clazz.fields) {
interface.members[field.name] = Member(
properties: Properties(
isAbstract: field.isAbstract,
isConstructor: false,
isGetter: false,
isField: true,
isMethod: false,
isStatic: field.isStatic,
),
returnType: types.addDartType(field.type));
}
for (final method in clazz.methods) {
interface.members[method.name] = Member(
requiredPositionalParameters: method.requiredPositionalParameters(
types.translator, types.context),
optionalPositionalParameters: method.optionalPositionalParameters(
types.translator, types.context),
namedParameters:
method.namedParameters(types.translator, types.context),
properties: Properties(
isAbstract: method.isAbstract,
isConstructor: false,
isGetter: false,
isField: false,
isMethod: true,
isStatic: method.isStatic,
),
returnType: types.addDartType(method.returnType));
try {
for (final constructor in clazz.constructors) {
interface.members[constructor.name] = Member(
requiredPositionalParameters: constructor
.requiredPositionalParameters(types.translator, types.context),
optionalPositionalParameters: constructor
.optionalPositionalParameters(types.translator, types.context),
namedParameters:
constructor.namedParameters(types.translator, types.context),
properties: Properties(
isAbstract: constructor.isAbstract,
isConstructor: true,
isGetter: false,
isField: false,
isMethod: false,
isStatic: false,
));
}
for (final field in clazz.fields) {
interface.members[field.name] = Member(
properties: Properties(
isAbstract: field.isAbstract,
isConstructor: false,
isGetter: false,
isField: true,
isMethod: false,
isStatic: field.isStatic,
),
returnType: types.addDartType(field.type));
}
for (final method in clazz.methods) {
interface.members[method.name] = Member(
requiredPositionalParameters: method.requiredPositionalParameters(
types.translator, types.context),
optionalPositionalParameters: method.optionalPositionalParameters(
types.translator, types.context),
namedParameters:
method.namedParameters(types.translator, types.context),
properties: Properties(
isAbstract: method.isAbstract,
isConstructor: false,
isGetter: false,
isField: false,
isMethod: true,
isStatic: method.isStatic,
),
returnType: types.addDartType(method.returnType));
}
} catch (_) {
// TODO: Fails in types phase, implement fine grained queries.
}
return Model(types: types.typeHierarchy)
..uris[uri] = (Library()..scopes[clazz.name] = interface);
Expand Down
29 changes: 26 additions & 3 deletions pkgs/_cfe_macros/lib/macro_implementation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,17 @@ class CfeRunningMacro implements injected.RunningMacro {
return CfeRunningMacro._(impl, name, implementation);
}

/// Queries for [target] and returns the [Model] representing the result.
///
/// TODO: Make this a more limited query which doesn't fetch members.
Future<Model> _queryTarget(QualifiedName target) =>
Scope.query.run(() async => (await _impl._host.hostService.handle(
MacroRequest.queryRequest(
QueryRequest(query: Query(target: target)),
id: nextRequestId)))
.asQueryResponse
.model);

@override
Future<CfeMacroExecutionResult> executeDeclarationsPhase(
macros_api_v1.MacroTarget target,
Expand All @@ -91,7 +102,11 @@ class CfeRunningMacro implements injected.RunningMacro {
return await CfeMacroExecutionResult.dartModelToInjected(
target,
await _impl._host.augment(
name, AugmentRequest(phase: 2, target: target.qualifiedName)));
name,
AugmentRequest(
phase: 2,
target: target.qualifiedName,
model: await _queryTarget(target.qualifiedName))));
}

@override
Expand All @@ -104,7 +119,11 @@ class CfeRunningMacro implements injected.RunningMacro {
return await CfeMacroExecutionResult.dartModelToInjected(
target,
await _impl._host.augment(
name, AugmentRequest(phase: 3, target: target.qualifiedName)));
name,
AugmentRequest(
phase: 3,
target: target.qualifiedName,
model: await _queryTarget(target.qualifiedName))));
}

@override
Expand All @@ -116,7 +135,11 @@ class CfeRunningMacro implements injected.RunningMacro {
return await CfeMacroExecutionResult.dartModelToInjected(
target,
await _impl._host.augment(
name, AugmentRequest(phase: 1, target: target.qualifiedName)));
name,
AugmentRequest(
phase: 1,
target: target.qualifiedName,
model: await _queryTarget(target.qualifiedName))));
}
}

Expand Down
9 changes: 7 additions & 2 deletions pkgs/_cfe_macros/lib/query_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,13 @@ class CfeQueryService implements QueryService {
isStatic: current.isStatic,
));
}

return Model(types: _buildTypeHierarchy({classBuilder.actualCls}))
TypeHierarchy? types;
try {
types = _buildTypeHierarchy({classBuilder.actualCls});
} catch (_) {
// TODO: Fails in types phase, implement fine grained queries.
}
return Model(types: types)
..uris[uri] = (Library()
..
// TODO(davidmorgan): return more than just fields.
Expand Down
44 changes: 23 additions & 21 deletions pkgs/_macro_client/lib/macro_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -112,27 +112,29 @@ class MacroClient {
'${hostRequest.macroAnnotation.asString}')));
} else {
final augmentRequest = hostRequest.asAugmentRequest;
await Scope.macro
.runAsync(() async => _sendResponse(Response.augmentResponse(
switch (augmentRequest.phase) {
1 => macro.description.runsInPhases.contains(1)
? await executeTypesMacro(
macro, _host, augmentRequest)
: null,
2 => macro.description.runsInPhases.contains(2)
? await executeDeclarationsMacro(
macro, _host, augmentRequest)
: null,
3 => macro.description.runsInPhases.contains(3)
? await executeDefinitionsMacro(
macro, _host, augmentRequest)
: null,
_ => throw StateError(
'Unexpected phase ${augmentRequest.phase}, '
'expected 1, 2, or 3.')
} ??
AugmentResponse(augmentations: []),
requestId: hostRequest.id)));
await Scope.macro.runAsync(() async {
MacroScope.current.addModel(augmentRequest.model);
return _sendResponse(Response.augmentResponse(
switch (augmentRequest.phase) {
1 => macro.description.runsInPhases.contains(1)
? await executeTypesMacro(
macro, _host, augmentRequest)
: null,
2 => macro.description.runsInPhases.contains(2)
? await executeDeclarationsMacro(
macro, _host, augmentRequest)
: null,
3 => macro.description.runsInPhases.contains(3)
? await executeDefinitionsMacro(
macro, _host, augmentRequest)
: null,
_ => throw StateError(
'Unexpected phase ${augmentRequest.phase}, '
'expected 1, 2, or 3.')
} ??
AugmentResponse(augmentations: []),
requestId: hostRequest.id));
});
}
default:
// Ignore unknown request.
Expand Down
31 changes: 13 additions & 18 deletions pkgs/_macro_client/lib/src/execute_macro.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ import 'package:macro_service/macro_service.dart';
Future<AugmentResponse> executeTypesMacro(
Macro macro, Host host, AugmentRequest request) async {
final target = request.target;
// TODO: https://github.com/dart-lang/macros/issues/100.
final queryResult = await host.query(Query(target: target));
final properties =
queryResult.uris[target.uri]!.scopes[target.name]!.properties;
switch ((properties, macro)) {
final model = request.model;
final interface = model.uris[target.uri]!.scopes[target.name]!;

switch ((interface.properties, macro)) {
case (Properties(isClass: true), ClassTypesMacro macro):
return await macro.buildTypesForClass(host, request);
return await macro.buildTypesForClass(interface, model, host);
case (_, LibraryTypesMacro()):
case (_, ConstructorTypesMacro()):
case (_, MethodTypesMacro()):
Expand All @@ -42,14 +41,12 @@ Future<AugmentResponse> executeTypesMacro(
Future<AugmentResponse> executeDeclarationsMacro(
Macro macro, Host host, AugmentRequest request) async {
final target = request.target;
// TODO: https://github.com/dart-lang/macros/issues/100.
final queryResult = await host.query(Query(target: target));
final properties =
queryResult.uris[target.uri]!.scopes[target.name]!.properties;
final model = request.model;
final interface = model.uris[target.uri]!.scopes[target.name]!;

switch ((properties, macro)) {
switch ((interface.properties, macro)) {
case (Properties(isClass: true), ClassDeclarationsMacro macro):
return await macro.buildDeclarationsForClass(host, request);
return await macro.buildDeclarationsForClass(interface, model, host);
case (_, LibraryDeclarationsMacro()):
case (_, EnumDeclarationsMacro()):
case (_, ExtensionDeclarationsMacro()):
Expand All @@ -73,14 +70,12 @@ Future<AugmentResponse> executeDeclarationsMacro(
Future<AugmentResponse> executeDefinitionsMacro(
Macro macro, Host host, AugmentRequest request) async {
final target = request.target;
// TODO: https://github.com/dart-lang/macros/issues/100.
final queryResult = await host.query(Query(target: target));
final properties =
queryResult.uris[target.uri]!.scopes[target.name]!.properties;
final model = request.model;
final interface = model.uris[target.uri]!.scopes[target.name]!;

switch ((properties, macro)) {
switch ((interface.properties, macro)) {
case (Properties(isClass: true), ClassDefinitionsMacro macro):
return await macro.buildDefinitionsForClass(host, request);
return await macro.buildDefinitionsForClass(interface, model, host);
case (_, LibraryDefinitionsMacro()):
case (_, EnumDefinitionsMacro()):
case (_, ExtensionDefinitionsMacro()):
Expand Down
Loading