Skip to content

Commit eb95543

Browse files
scheglovCommit Bot
authored and
Commit Bot
committed
Run macro types phase.
The very first version, many improvements to be done later. Change-Id: If78efba1ab0d06aaea6f3c7525fcd00a39c18459 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/237768 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent ccb4157 commit eb95543

12 files changed

+432
-16
lines changed

pkg/analyzer/lib/src/dart/analysis/analysis_context_collection.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'package:_fe_analyzer_shared/src/macros/executor.dart' as macro;
56
import 'package:analyzer/dart/analysis/analysis_context_collection.dart';
67
import 'package:analyzer/dart/analysis/context_locator.dart';
78
import 'package:analyzer/dart/analysis/declared_variables.dart';
@@ -22,6 +23,9 @@ class AnalysisContextCollectionImpl implements AnalysisContextCollection {
2223
/// The resource provider used to access the file system.
2324
final ResourceProvider resourceProvider;
2425

26+
/// The instance of macro executor that is used for all macros.
27+
final macro.MacroExecutor? macroExecutor;
28+
2529
/// The list of analysis contexts.
2630
@override
2731
final List<DriverBasedAnalysisContext> contexts = [];
@@ -44,6 +48,7 @@ class AnalysisContextCollectionImpl implements AnalysisContextCollection {
4448
FileContentCache? fileContentCache,
4549
void Function(AnalysisOptionsImpl)? updateAnalysisOptions,
4650
MacroKernelBuilder? macroKernelBuilder,
51+
this.macroExecutor,
4752
}) : resourceProvider =
4853
resourceProvider ?? PhysicalResourceProvider.INSTANCE {
4954
sdkPath ??= getSdkPath();
@@ -77,6 +82,7 @@ class AnalysisContextCollectionImpl implements AnalysisContextCollection {
7782
updateAnalysisOptions: updateAnalysisOptions,
7883
fileContentCache: fileContentCache,
7984
macroKernelBuilder: macroKernelBuilder,
85+
macroExecutor: macroExecutor,
8086
);
8187
contexts.add(context);
8288
}
@@ -109,6 +115,13 @@ class AnalysisContextCollectionImpl implements AnalysisContextCollection {
109115
throw StateError('Unable to find the context to $path');
110116
}
111117

118+
void dispose() {
119+
for (var analysisContext in contexts) {
120+
analysisContext.driver.dispose();
121+
}
122+
macroExecutor?.close();
123+
}
124+
112125
/// Check every element with [_throwIfNotAbsoluteNormalizedPath].
113126
void _throwIfAnyNotAbsoluteNormalizedPath(List<String> paths) {
114127
for (var path in paths) {

pkg/analyzer/lib/src/dart/analysis/context_builder.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'package:_fe_analyzer_shared/src/macros/executor.dart' as macro;
56
import 'package:analyzer/dart/analysis/context_builder.dart';
67
import 'package:analyzer/dart/analysis/context_root.dart';
78
import 'package:analyzer/dart/analysis/declared_variables.dart';
@@ -60,6 +61,7 @@ class ContextBuilderImpl implements ContextBuilder {
6061
void Function(AnalysisOptionsImpl)? updateAnalysisOptions,
6162
FileContentCache? fileContentCache,
6263
MacroKernelBuilder? macroKernelBuilder,
64+
macro.MacroExecutor? macroExecutor,
6365
}) {
6466
// TODO(scheglov) Remove this, and make `sdkPath` required.
6567
sdkPath ??= getSdkPath();
@@ -118,6 +120,7 @@ class ContextBuilderImpl implements ContextBuilder {
118120
retainDataForTesting: retainDataForTesting,
119121
fileContentCache: fileContentCache,
120122
macroKernelBuilder: macroKernelBuilder,
123+
macroExecutor: macroExecutor,
121124
);
122125

123126
if (declaredVariables != null) {

pkg/analyzer/lib/src/dart/analysis/driver.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import 'dart:async';
66
import 'dart:typed_data';
77

8+
import 'package:_fe_analyzer_shared/src/macros/executor.dart' as macro;
89
import 'package:analyzer/dart/analysis/analysis_context.dart' as api;
910
import 'package:analyzer/dart/analysis/declared_variables.dart';
1011
import 'package:analyzer/dart/analysis/results.dart';
@@ -127,6 +128,9 @@ class AnalysisDriver implements AnalysisDriverGeneric {
127128

128129
final MacroKernelBuilder? macroKernelBuilder;
129130

131+
/// The instance of macro executor that is used for all macros.
132+
final macro.MacroExecutor? macroExecutor;
133+
130134
/// The declared environment variables.
131135
DeclaredVariables declaredVariables = DeclaredVariables();
132136

@@ -261,6 +265,7 @@ class AnalysisDriver implements AnalysisDriverGeneric {
261265
required AnalysisOptionsImpl analysisOptions,
262266
required Packages packages,
263267
this.macroKernelBuilder,
268+
this.macroExecutor,
264269
FileContentCache? fileContentCache,
265270
bool enableIndex = false,
266271
SummaryDataStore? externalSummaries,
@@ -327,6 +332,7 @@ class AnalysisDriver implements AnalysisDriverGeneric {
327332
declaredVariables: declaredVariables,
328333
sourceFactory: _sourceFactory,
329334
macroKernelBuilder: macroKernelBuilder,
335+
macroExecutor: macroExecutor,
330336
externalSummaries: _externalSummaries,
331337
fileSystemState: _fsState,
332338
);
@@ -537,6 +543,7 @@ class AnalysisDriver implements AnalysisDriverGeneric {
537543
/// periodically.
538544
@visibleForTesting
539545
void clearLibraryContext() {
546+
_libraryContext?.dispose();
540547
_libraryContext = null;
541548
}
542549

@@ -590,6 +597,7 @@ class AnalysisDriver implements AnalysisDriverGeneric {
590597
@override
591598
void dispose() {
592599
_scheduler.remove(this);
600+
clearLibraryContext();
593601
}
594602

595603
/// Return the cached [ResolvedUnitResult] for the Dart file with the given

pkg/analyzer/lib/src/dart/analysis/library_context.dart

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'dart:typed_data';
66

7+
import 'package:_fe_analyzer_shared/src/macros/executor.dart' as macro;
78
import 'package:analyzer/dart/analysis/declared_variables.dart';
89
import 'package:analyzer/dart/element/element.dart'
910
show CompilationUnitElement, LibraryElement;
@@ -43,6 +44,7 @@ class LibraryContext {
4344
final ByteStore byteStore;
4445
final FileSystemState fileSystemState;
4546
final MacroKernelBuilder? macroKernelBuilder;
47+
final macro.MacroExecutor? macroExecutor;
4648
final SummaryDataStore store = SummaryDataStore();
4749

4850
late final AnalysisContextImpl analysisContext;
@@ -57,7 +59,8 @@ class LibraryContext {
5759
required AnalysisOptionsImpl analysisOptions,
5860
required DeclaredVariables declaredVariables,
5961
required SourceFactory sourceFactory,
60-
this.macroKernelBuilder,
62+
required this.macroKernelBuilder,
63+
required this.macroExecutor,
6164
required SummaryDataStore? externalSummaries,
6265
}) : logger = logger,
6366
byteStore = byteStore {
@@ -93,6 +96,11 @@ class LibraryContext {
9396
return element as CompilationUnitElement;
9497
}
9598

99+
/// TODO(scheglov) call it
100+
void dispose() {
101+
elementFactory.dispose();
102+
}
103+
96104
/// Get the [LibraryElement] for the given library.
97105
LibraryElement getLibraryElement(Uri uri) {
98106
_createElementFactoryTypeProvider();
@@ -205,7 +213,8 @@ class LibraryContext {
205213
link2.LinkResult linkResult;
206214
try {
207215
timerLinking.start();
208-
linkResult = link2.link(elementFactory, inputLibraries);
216+
linkResult = link2.link(elementFactory, inputLibraries,
217+
macroExecutor: macroExecutor);
209218
librariesLinked += cycle.libraries.length;
210219
counterLinkedLibraries += inputLibraries.length;
211220
timerLinking.stop();
@@ -235,12 +244,30 @@ class LibraryContext {
235244
final macroKernelBuilder = this.macroKernelBuilder;
236245
if (macroKernelBuilder != null && macroLibraries.isNotEmpty) {
237246
var macroKernelKey = cycle.transitiveSignature + '.macro_kernel';
238-
var macroKernelBytes = macroKernelBuilder.build(
239-
fileSystem: _MacroFileSystem(fileSystemState),
240-
libraries: macroLibraries,
241-
);
242-
byteStore.put(macroKernelKey, macroKernelBytes);
243-
bytesPut += macroKernelBytes.length;
247+
var macroKernelBytes = byteStore.get(macroKernelKey);
248+
if (macroKernelBytes == null) {
249+
macroKernelBytes = macroKernelBuilder.build(
250+
fileSystem: _MacroFileSystem(fileSystemState),
251+
libraries: macroLibraries,
252+
);
253+
byteStore.put(macroKernelKey, macroKernelBytes);
254+
bytesPut += macroKernelBytes.length;
255+
} else {
256+
bytesGet += macroKernelBytes.length;
257+
}
258+
259+
final macroExecutor = this.macroExecutor;
260+
if (macroExecutor != null) {
261+
var bundleMacroExecutor = BundleMacroExecutor(
262+
executor: macroExecutor,
263+
kernelBytes: macroKernelBytes,
264+
);
265+
for (var libraryFile in cycle.libraries) {
266+
var libraryUriStr = libraryFile.uriStr;
267+
var libraryElement = elementFactory.libraryOfUri2(libraryUriStr);
268+
libraryElement.bundleMacroExecutor = bundleMacroExecutor;
269+
}
270+
}
244271
}
245272
}
246273

pkg/analyzer/lib/src/dart/element/element.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import 'package:analyzer/src/generated/utilities_collection.dart';
4040
import 'package:analyzer/src/generated/utilities_dart.dart';
4141
import 'package:analyzer/src/summary2/ast_binary_tokens.dart';
4242
import 'package:analyzer/src/summary2/bundle_reader.dart';
43+
import 'package:analyzer/src/summary2/macro.dart';
4344
import 'package:analyzer/src/summary2/reference.dart';
4445
import 'package:analyzer/src/task/inference_error.dart';
4546
import 'package:collection/collection.dart';
@@ -526,6 +527,10 @@ class ClassElementImpl extends AbstractClassElementImpl {
526527

527528
ElementLinkedData? linkedData;
528529

530+
/// When a macro class is used the first time, we load it, and remember
531+
/// its identifier here.
532+
MacroClassIdentifier? macroIdentifier;
533+
529534
/// Initialize a newly created class element to have the given [name] at the
530535
/// given [offset] in the file that contains the declaration of this element.
531536
ClassElementImpl(String name, int offset) : super(name, offset);
@@ -3693,6 +3698,9 @@ class LibraryElementImpl extends _ExistingElementImpl
36933698
/// The scope of this library, `null` if it has not been created yet.
36943699
LibraryScope? _scope;
36953700

3701+
/// The macro executor for the bundle to which this library belongs.
3702+
BundleMacroExecutor? bundleMacroExecutor;
3703+
36963704
/// Initialize a newly created library element in the given [context] to have
36973705
/// the given [name] and [offset].
36983706
LibraryElementImpl(this.context, this.session, String name, int offset,

pkg/analyzer/lib/src/summary2/library_builder.dart

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,21 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'package:analyzer/dart/analysis/utilities.dart';
56
import 'package:analyzer/dart/ast/ast.dart' as ast;
67
import 'package:analyzer/dart/element/element.dart';
78
import 'package:analyzer/src/dart/ast/ast.dart' as ast;
89
import 'package:analyzer/src/dart/ast/mixin_super_invoked_names.dart';
910
import 'package:analyzer/src/dart/element/element.dart';
1011
import 'package:analyzer/src/dart/resolver/scope.dart';
12+
import 'package:analyzer/src/generated/source.dart';
1113
import 'package:analyzer/src/summary2/combinator.dart';
1214
import 'package:analyzer/src/summary2/constructor_initializer_resolver.dart';
1315
import 'package:analyzer/src/summary2/default_value_resolver.dart';
1416
import 'package:analyzer/src/summary2/element_builder.dart';
1517
import 'package:analyzer/src/summary2/export.dart';
1618
import 'package:analyzer/src/summary2/link.dart';
19+
import 'package:analyzer/src/summary2/macro_application.dart';
1720
import 'package:analyzer/src/summary2/metadata_resolver.dart';
1821
import 'package:analyzer/src/summary2/reference.dart';
1922
import 'package:analyzer/src/summary2/reference_resolver.dart';
@@ -58,6 +61,10 @@ class LibraryBuilder {
5861
required this.units,
5962
});
6063

64+
SourceFactory get _sourceFactory {
65+
return linker.elementFactory.analysisContext.sourceFactory;
66+
}
67+
6168
void addExporters() {
6269
for (var element in element.exports) {
6370
var exportedLibrary = element.exportedLibrary;
@@ -167,6 +174,46 @@ class LibraryBuilder {
167174
}
168175
}
169176

177+
void executeMacroTypesPhase() {
178+
var applier = LibraryMacroApplier(this);
179+
var augmentationLibrary = applier.executeMacroTypesPhase();
180+
if (augmentationLibrary == null) {
181+
return;
182+
}
183+
184+
var parseResult = parseString(
185+
content: augmentationLibrary,
186+
featureSet: element.featureSet,
187+
throwIfDiagnostics: false,
188+
);
189+
190+
// For now we model augmentation libraries as parts.
191+
var unitUri = uri.resolve('_macro_types.dart');
192+
var unitElement = CompilationUnitElementImpl()
193+
..enclosingElement = element
194+
..isSynthetic = true
195+
..librarySource = element.source
196+
..lineInfo = parseResult.lineInfo
197+
..source = _sourceFactory.forUri2(unitUri)!;
198+
199+
var unitReference = reference.getChild('@unit').getChild('$unitUri');
200+
_bindReference(unitReference, unitElement);
201+
202+
element.parts.add(unitElement);
203+
204+
var elementBuilder = ElementBuilder(
205+
libraryBuilder: this,
206+
unitReference: unitReference,
207+
unitElement: unitElement,
208+
);
209+
elementBuilder.buildDeclarationElements(parseResult.unit);
210+
211+
// TODO(scheglov) unify with `resolveTypes`.
212+
var nodesToBuildType = NodesToBuildType();
213+
var resolver = ReferenceResolver(linker, nodesToBuildType, element);
214+
parseResult.unit.accept(resolver);
215+
}
216+
170217
void resolveConstructors() {
171218
ConstructorInitializerResolver(linker, element).resolve();
172219
}

pkg/analyzer/lib/src/summary2/link.dart

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'dart:typed_data';
66

7+
import 'package:_fe_analyzer_shared/src/macros/executor.dart' as macro;
78
import 'package:analyzer/dart/analysis/declared_variables.dart';
89
import 'package:analyzer/dart/ast/ast.dart' as ast;
910
import 'package:analyzer/dart/element/element.dart';
@@ -28,9 +29,10 @@ var timerLinkingLinkingBundle = Stopwatch();
2829
/// Note that AST units and tokens of [inputLibraries] will be damaged.
2930
LinkResult link(
3031
LinkedElementFactory elementFactory,
31-
List<LinkInputLibrary> inputLibraries,
32-
) {
33-
var linker = Linker(elementFactory);
32+
List<LinkInputLibrary> inputLibraries, {
33+
macro.MacroExecutor? macroExecutor,
34+
}) {
35+
var linker = Linker(elementFactory, macroExecutor);
3436
linker.link(inputLibraries);
3537
return LinkResult(
3638
resolutionBytes: linker.resolutionBytes,
@@ -39,6 +41,7 @@ LinkResult link(
3941

4042
class Linker {
4143
final LinkedElementFactory elementFactory;
44+
final macro.MacroExecutor? macroExecutor;
4245

4346
/// Libraries that are being linked.
4447
final Map<Uri, LibraryBuilder> builders = {};
@@ -49,7 +52,7 @@ class Linker {
4952

5053
late Uint8List resolutionBytes;
5154

52-
Linker(this.elementFactory);
55+
Linker(this.elementFactory, this.macroExecutor);
5356

5457
AnalysisContextImpl get analysisContext {
5558
return elementFactory.analysisContext;
@@ -111,6 +114,10 @@ class Linker {
111114
library.buildElements();
112115
}
113116

117+
for (var library in builders.values) {
118+
library.executeMacroTypesPhase();
119+
}
120+
114121
for (var library in builders.values) {
115122
library.buildInitialExportScope();
116123
}

pkg/analyzer/lib/src/summary2/linked_element_factory.dart

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@ class LinkedElementFactory {
116116
}
117117
}
118118

119+
void dispose() {
120+
for (var libraryReference in rootReference.children) {
121+
_disposeLibrary(libraryReference.element);
122+
}
123+
}
124+
119125
Element? elementOfReference(Reference reference) {
120126
if (reference.element != null) {
121127
return reference.element;
@@ -198,7 +204,8 @@ class LinkedElementFactory {
198204
for (var uriStr in uriStrSet) {
199205
_exportsOfLibrary.remove(uriStr);
200206
_libraryReaders.remove(uriStr);
201-
rootReference.removeChild(uriStr);
207+
var libraryReference = rootReference.removeChild(uriStr);
208+
_disposeLibrary(libraryReference?.element);
202209
}
203210

204211
analysisSession.classHierarchy.removeOfLibraries(uriStrSet);
@@ -258,4 +265,10 @@ class LinkedElementFactory {
258265

259266
libraryElement.createLoadLibraryFunction();
260267
}
268+
269+
void _disposeLibrary(Element? libraryElement) {
270+
if (libraryElement is LibraryElementImpl) {
271+
libraryElement.bundleMacroExecutor?.dispose();
272+
}
273+
}
261274
}

0 commit comments

Comments
 (0)