Skip to content

Commit d2105a9

Browse files
johnniwintherCommit Bot
authored and
Commit Bot
committed
[cfe] Add initial support for applying phase 1+2 macros
Change-Id: I04964684b355e50abb4a0d9bcfa0558297e667c2 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/230080 Reviewed-by: Jens Johansen <[email protected]> Commit-Queue: Johnni Winther <[email protected]>
1 parent 208e7bc commit d2105a9

File tree

8 files changed

+135
-35
lines changed

8 files changed

+135
-35
lines changed

pkg/front_end/lib/src/fasta/kernel/kernel_target.dart

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -398,11 +398,9 @@ class KernelTarget extends TargetImplementation {
398398
loader.computeVariances();
399399
loader.computeDefaultTypes(
400400
dynamicType, nullType, bottomType, objectClassBuilder);
401-
// TODO(johnniwinther): Enable this when supported in the isolate-based
402-
// macro executor.
403-
/*if (macroApplications != null) {
401+
if (macroApplications != null) {
404402
await macroApplications.applyTypeMacros();
405-
}*/
403+
}
406404
List<SourceClassBuilder> sourceClassBuilders =
407405
loader.checkSemantics(objectClassBuilder);
408406
loader.finishTypeVariables(objectClassBuilder, dynamicType);
@@ -416,11 +414,9 @@ class KernelTarget extends TargetImplementation {
416414
computeCoreTypes();
417415
loader.buildClassHierarchy(sourceClassBuilders, objectClassBuilder);
418416
loader.checkSupertypes(sourceClassBuilders, enumClass);
419-
// TODO(johnniwinther): Enable this when supported in the isolate-based
420-
// macro executor.
421-
/*if (macroApplications != null) {
417+
if (macroApplications != null) {
422418
await macroApplications.applyDeclarationMacros();
423-
}*/
419+
}
424420
loader.buildClassHierarchyMembers(sourceClassBuilders);
425421
loader.computeHierarchy();
426422
loader.computeShowHideElements();

pkg/front_end/lib/src/fasta/kernel/macro.dart

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ class MacroApplication {
6666

6767
class MacroApplicationDataForTesting {
6868
Map<SourceLibraryBuilder, LibraryMacroApplicationData> libraryData = {};
69+
Map<MemberBuilder, List<MacroExecutionResult>> memberTypesResults = {};
70+
Map<MemberBuilder, List<MacroExecutionResult>> memberDeclarationsResults = {};
6971
Map<MemberBuilder, List<MacroExecutionResult>> memberDefinitionsResults = {};
7072
}
7173

@@ -155,12 +157,20 @@ class MacroApplications {
155157
}
156158
}
157159

158-
Future<void> _applyTypeMacros(
160+
Future<List<MacroExecutionResult>> _applyTypeMacros(
159161
Declaration declaration, List<MacroApplication> macroApplications) async {
162+
List<MacroExecutionResult> results = [];
160163
for (MacroApplication macroApplication in macroApplications) {
161-
await _macroExecutor.executeTypesPhase(
162-
macroApplication.instanceIdentifier, declaration);
164+
if (macroApplication.instanceIdentifier.shouldExecute(
165+
// TODO(johnniwinther): Get the declaration kind from [declaration].
166+
DeclarationKind.function,
167+
Phase.types)) {
168+
MacroExecutionResult result = await _macroExecutor.executeTypesPhase(
169+
macroApplication.instanceIdentifier, declaration);
170+
results.add(result);
171+
}
163172
}
173+
return results;
164174
}
165175

166176
Future<void> applyTypeMacros() async {
@@ -173,24 +183,35 @@ class MacroApplications {
173183
MemberBuilder memberBuilder = memberEntry.key;
174184
Declaration? declaration = _getMemberDeclaration(memberBuilder);
175185
if (declaration != null) {
176-
await _applyTypeMacros(declaration, memberEntry.value);
186+
List<MacroExecutionResult> results =
187+
await _applyTypeMacros(declaration, memberEntry.value);
188+
dataForTesting?.memberTypesResults[memberBuilder] = results;
177189
}
178190
}
179191
}
180192
}
181193

182-
Future<void> _applyDeclarationMacros(
194+
Future<List<MacroExecutionResult>> _applyDeclarationMacros(
183195
Declaration declaration,
184196
List<MacroApplication> macroApplications,
185197
TypeResolver typeResolver,
186198
ClassIntrospector classIntrospector) async {
199+
List<MacroExecutionResult> results = [];
187200
for (MacroApplication macroApplication in macroApplications) {
188-
await _macroExecutor.executeDeclarationsPhase(
189-
macroApplication.instanceIdentifier,
190-
declaration,
191-
typeResolver,
192-
classIntrospector);
201+
if (macroApplication.instanceIdentifier.shouldExecute(
202+
// TODO(johnniwinther): Get the declaration kind from [declaration].
203+
DeclarationKind.function,
204+
Phase.declarations)) {
205+
MacroExecutionResult result =
206+
await _macroExecutor.executeDeclarationsPhase(
207+
macroApplication.instanceIdentifier,
208+
declaration,
209+
typeResolver,
210+
classIntrospector);
211+
results.add(result);
212+
}
193213
}
214+
return results;
194215
}
195216

196217
Future<void> applyDeclarationMacros() async {
@@ -205,8 +226,9 @@ class MacroApplications {
205226
MemberBuilder memberBuilder = memberEntry.key;
206227
Declaration? declaration = _getMemberDeclaration(memberBuilder);
207228
if (declaration != null) {
208-
await _applyDeclarationMacros(
229+
List<MacroExecutionResult> results = await _applyDeclarationMacros(
209230
declaration, memberEntry.value, typeResolver, classIntrospector);
231+
dataForTesting?.memberDeclarationsResults[memberBuilder] = results;
210232
}
211233
}
212234
}
@@ -220,14 +242,19 @@ class MacroApplications {
220242
TypeDeclarationResolver typeDeclarationResolver) async {
221243
List<MacroExecutionResult> results = [];
222244
for (MacroApplication macroApplication in macroApplications) {
223-
MacroExecutionResult result =
224-
await _macroExecutor.executeDefinitionsPhase(
225-
macroApplication.instanceIdentifier,
226-
declaration,
227-
typeResolver,
228-
classIntrospector,
229-
typeDeclarationResolver);
230-
results.add(result);
245+
if (macroApplication.instanceIdentifier.shouldExecute(
246+
// TODO(johnniwinther): Get the declaration kind from [declaration].
247+
DeclarationKind.function,
248+
Phase.definitions)) {
249+
MacroExecutionResult result =
250+
await _macroExecutor.executeDefinitionsPhase(
251+
macroApplication.instanceIdentifier,
252+
declaration,
253+
typeResolver,
254+
classIntrospector,
255+
typeDeclarationResolver);
256+
results.add(result);
257+
}
231258
}
232259
return results;
233260
}

pkg/front_end/test/macro_application/data/pkgs/macro/lib/macro.dart

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,26 @@ macro class FunctionDefinitionMacro2 implements FunctionDefinitionMacro {
3333
}'''));
3434
}
3535
}
36+
37+
38+
macro class FunctionTypesMacro1 implements FunctionTypesMacro {
39+
const FunctionTypesMacro1();
40+
41+
FutureOr<void> buildTypesForFunction(
42+
FunctionDeclaration function, TypeBuilder builder) {
43+
builder.declareType(new DeclarationCode.fromString('''
44+
class ${function.name}GeneratedClass {}
45+
'''));
46+
}
47+
}
48+
49+
macro class FunctionDeclarationsMacro1 implements FunctionDeclarationsMacro {
50+
const FunctionDeclarationsMacro1();
51+
52+
FutureOr<void> buildDeclarationsForFunction(
53+
FunctionDeclaration function, DeclarationBuilder builder) {
54+
builder.declareInLibrary(new DeclarationCode.fromString('''
55+
void ${function.name}GeneratedMethod() {}
56+
'''));
57+
}
58+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:macro/macro.dart';
6+
7+
/*member: topLevelFunction1:
8+
void topLevelFunction1GeneratedMethod() {}
9+
*/
10+
@FunctionDeclarationsMacro1()
11+
void topLevelFunction1() {}
12+
13+
@FunctionDeclarationsMacro1()
14+
/*member: topLevelFunction2:
15+
void topLevelFunction2GeneratedMethod() {}
16+
*/
17+
void topLevelFunction2() {}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:macro/macro.dart';
6+
7+
@FunctionTypesMacro1()
8+
/*member: topLevelFunction1:
9+
class topLevelFunction1GeneratedClass {}
10+
*/
11+
void topLevelFunction1() {}
12+
13+
@FunctionTypesMacro1()
14+
/*member: topLevelFunction2:
15+
class topLevelFunction2GeneratedClass {}
16+
*/
17+
void topLevelFunction2() {}

pkg/front_end/test/macro_application/macro_application_test.dart

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ const Map<String, Map<String, List<String>>> macroDeclarations = {
2929
'package:macro/macro.dart': {
3030
'FunctionDefinitionMacro1': [''],
3131
'FunctionDefinitionMacro2': [''],
32+
'FunctionTypesMacro1': [''],
33+
'FunctionDeclarationsMacro1': [''],
3234
}
3335
};
3436

@@ -110,18 +112,36 @@ class MacroDataComputer extends DataComputer<String> {
110112
.loader
111113
.dataForTesting!
112114
.macroApplicationData;
115+
StringBuffer sb = new StringBuffer();
116+
for (MapEntry<MemberBuilder, List<MacroExecutionResult>> entry
117+
in macroApplicationData.memberTypesResults.entries) {
118+
if (entry.key.member == member) {
119+
for (MacroExecutionResult result in entry.value) {
120+
sb.write('\n${codeToString(result.augmentations.first)}');
121+
}
122+
}
123+
}
124+
for (MapEntry<MemberBuilder, List<MacroExecutionResult>> entry
125+
in macroApplicationData.memberDeclarationsResults.entries) {
126+
if (entry.key.member == member) {
127+
for (MacroExecutionResult result in entry.value) {
128+
sb.write('\n${codeToString(result.augmentations.first)}');
129+
}
130+
}
131+
}
113132
for (MapEntry<MemberBuilder, List<MacroExecutionResult>> entry
114133
in macroApplicationData.memberDefinitionsResults.entries) {
115134
if (entry.key.member == member) {
116-
StringBuffer sb = new StringBuffer();
117135
for (MacroExecutionResult result in entry.value) {
118136
sb.write('\n${codeToString(result.augmentations.first)}');
119137
}
120-
Id id = computeMemberId(member);
121-
registry.registerValue(
122-
member.fileUri, member.fileOffset, id, sb.toString(), member);
123138
}
124139
}
140+
if (sb.isNotEmpty) {
141+
Id id = computeMemberId(member);
142+
registry.registerValue(
143+
member.fileUri, member.fileOffset, id, sb.toString(), member);
144+
}
125145
}
126146
}
127147

pkg/front_end/test/macros/macro_test.dart

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -375,12 +375,10 @@ class _MacroInstanceIdentifier implements MacroInstanceIdentifier {
375375
void serialize(Serializer serializer) => throw UnimplementedError();
376376

377377
@override
378-
bool shouldExecute(DeclarationKind declarationKind, Phase phase) =>
379-
throw new UnimplementedError();
378+
bool shouldExecute(DeclarationKind declarationKind, Phase phase) => false;
380379

381380
@override
382-
bool supportsDeclarationKind(DeclarationKind declarationKind) =>
383-
throw new UnimplementedError();
381+
bool supportsDeclarationKind(DeclarationKind declarationKind) => false;
384382
}
385383

386384
class _MacroExecutionResult implements MacroExecutionResult {

pkg/front_end/test/spell_checking_list_tests.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,8 @@ frequency
468468
frozen
469469
fulfill
470470
func
471+
function1
472+
function2
471473
futu
472474
futures
473475
fuzz

0 commit comments

Comments
 (0)