Skip to content

Commit 4096db4

Browse files
scheglovCommit Queue
authored andcommitted
Elements. Create loadLibrary() function with reference, lazily.
Related to https://dart-review.googlesource.com/c/sdk/+/393164 Change-Id: Ia448ff42c9de538be9a8f6f2101dff0c949671d3 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/393260 Commit-Queue: Konstantin Shcheglov <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Phil Quitslund <[email protected]>
1 parent 8519914 commit 4096db4

File tree

8 files changed

+85
-35
lines changed

8 files changed

+85
-35
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ import 'package:meta/meta.dart';
9898
// TODO(scheglov): Clean up the list of implicitly analyzed files.
9999
class AnalysisDriver {
100100
/// The version of data format, should be incremented on every format change.
101-
static const int DATA_VERSION = 412;
101+
static const int DATA_VERSION = 413;
102102

103103
/// The number of exception contexts allowed to write. Once this field is
104104
/// zero, we stop writing any new exception contexts in this process.

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

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6328,10 +6328,9 @@ class LibraryElementImpl extends ElementImpl
63286328
/// an entry point.
63296329
FunctionElement? _entryPoint;
63306330

6331-
/// The element representing the synthetic function `loadLibrary` that is
6332-
/// defined for this library, or `null` if the element has not yet been
6333-
/// created.
6334-
late FunctionElement _loadLibraryFunction;
6331+
/// The provider for the synthetic function `loadLibrary` that is defined
6332+
/// for this library.
6333+
late final LoadLibraryFunctionProvider loadLibraryProvider;
63356334

63366335
@override
63376336
int nameLength;
@@ -6605,13 +6604,14 @@ class LibraryElementImpl extends ElementImpl
66056604
}
66066605

66076606
@override
6608-
FunctionElement get loadLibraryFunction {
6609-
return _loadLibraryFunction;
6607+
FunctionElementImpl get loadLibraryFunction {
6608+
return loadLibraryFunction2.firstFragment;
66106609
}
66116610

66126611
@override
6613-
TopLevelFunctionElement get loadLibraryFunction2 =>
6614-
loadLibraryFunction as TopLevelFunctionElement;
6612+
TopLevelFunctionElementImpl get loadLibraryFunction2 {
6613+
return loadLibraryProvider.getElement(this);
6614+
}
66156615

66166616
@override
66176617
List<ElementAnnotationImpl> get metadata {
@@ -6732,18 +6732,6 @@ class LibraryElementImpl extends ElementImpl
67326732
builder.writeLibraryElement(this);
67336733
}
67346734

6735-
/// Create the [FunctionElement] to be returned by [loadLibraryFunction].
6736-
/// The [typeProvider] must be already set.
6737-
void createLoadLibraryFunction() {
6738-
var fragment = FunctionElementImpl(FunctionElement.LOAD_LIBRARY_NAME, -1)
6739-
..enclosingElement3 = library
6740-
..isSynthetic = true
6741-
..returnType = typeProvider.futureDynamicType;
6742-
_loadLibraryFunction = fragment;
6743-
// TODO(scheglov): create it sooner, with actual reference.
6744-
TopLevelFunctionElementImpl(Reference.root(), fragment);
6745-
}
6746-
67476735
@override
67486736
ClassElement? getClass(String name) {
67496737
for (var unitElement in units) {
@@ -6959,6 +6947,38 @@ class LibraryImportElementImpl extends _ExistingElementImpl
69596947
}
69606948
}
69616949

6950+
/// The provider for the lazily created `loadLibrary` function.
6951+
final class LoadLibraryFunctionProvider {
6952+
final Reference fragmentReference;
6953+
final Reference elementReference;
6954+
TopLevelFunctionElementImpl? _element;
6955+
6956+
LoadLibraryFunctionProvider({
6957+
required this.fragmentReference,
6958+
required this.elementReference,
6959+
});
6960+
6961+
TopLevelFunctionElementImpl getElement(LibraryElementImpl library) {
6962+
return _element ??= _create(library);
6963+
}
6964+
6965+
TopLevelFunctionElementImpl _create(LibraryElementImpl library) {
6966+
var name = FunctionElement.LOAD_LIBRARY_NAME;
6967+
6968+
var fragment = FunctionElementImpl(name, -1);
6969+
fragment.name2 = name;
6970+
fragment.isSynthetic = true;
6971+
fragment.isStatic = true;
6972+
fragment.returnType = library.typeProvider.futureDynamicType;
6973+
fragment.enclosingElement3 = library.definingCompilationUnit;
6974+
6975+
fragment.reference = fragmentReference;
6976+
fragmentReference.element = fragment;
6977+
6978+
return TopLevelFunctionElementImpl(elementReference, fragment);
6979+
}
6980+
}
6981+
69626982
class LocalFunctionElementImpl extends ExecutableElementImpl2
69636983
with WrappedElementMixin
69646984
implements LocalFunctionElement {

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,11 @@ class LibraryReader {
661661
_reader.readUInt30List(),
662662
);
663663

664+
libraryElement.loadLibraryProvider = LoadLibraryFunctionProvider(
665+
fragmentReference: _readReference(),
666+
elementReference: _readReference(),
667+
);
668+
664669
// Read the library units.
665670
libraryElement.definingCompilationUnit = _readUnitElement(
666671
libraryElement: libraryElement,
@@ -1182,7 +1187,6 @@ class LibraryReader {
11821187
TopLevelFunctionElementImpl(reference2, fragment);
11831188
}
11841189

1185-
11861190
var linkedData = FunctionElementLinkedData(
11871191
reference: reference,
11881192
libraryReader: this,

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ class BundleWriter {
104104
_writeLanguageVersion(libraryElement.languageVersion);
105105
_writeExportedReferences(libraryElement.exportedReferences);
106106
_sink.writeUint30List(libraryElement.nameUnion.mask);
107+
_writeLoadLibraryFunctionReferences(libraryElement);
107108

108109
// Write the library units.
109110
// This will write also resolution data, e.g. for classes.
@@ -483,6 +484,12 @@ class BundleWriter {
483484
}
484485
}
485486

487+
void _writeLoadLibraryFunctionReferences(LibraryElementImpl library) {
488+
var element = library.loadLibraryFunction2;
489+
_writeReference2(element.firstFragment.reference);
490+
_writeReference2(element.reference);
491+
}
492+
486493
void _writeMethodElement(MethodElementImpl element) {
487494
_sink.writeUInt30(_resolutionSink.offset);
488495
_writeReference(element);

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1258,6 +1258,25 @@ class LibraryBuilder with MacroApplicationsContainer {
12581258
);
12591259
}
12601260

1261+
/// We want to have stable references for `loadLibrary` function. But we
1262+
/// cannot create the function itself right now, because the type provider
1263+
/// might be not available yet. So, we create references together with the
1264+
/// library, and create the fragment and element later.
1265+
void _createLoadLibraryReferences() {
1266+
var name = FunctionElement.LOAD_LIBRARY_NAME;
1267+
1268+
var fragmentContainer = units[0].reference.getChild('@function');
1269+
var fragmentReference = fragmentContainer.addChild(name);
1270+
1271+
var elementContainer = reference.getChild('@function');
1272+
var elementReference = elementContainer.addChild(name);
1273+
1274+
element.loadLibraryProvider = LoadLibraryFunctionProvider(
1275+
fragmentReference: fragmentReference,
1276+
elementReference: elementReference,
1277+
);
1278+
}
1279+
12611280
/// These elements are implicitly declared in `dart:core`.
12621281
void _declareDartCoreDynamicNever() {
12631282
if (reference.name == 'dart:core') {
@@ -1316,7 +1335,6 @@ class LibraryBuilder with MacroApplicationsContainer {
13161335
libraryElement.isSynthetic = !libraryFile.exists;
13171336
libraryElement.languageVersion = libraryUnitNode.languageVersion;
13181337
_bindReference(libraryReference, libraryElement);
1319-
elementFactory.setLibraryTypeSystem(libraryElement);
13201338

13211339
var unitContainerRef = libraryReference.getChild('@fragment');
13221340

@@ -1354,6 +1372,9 @@ class LibraryBuilder with MacroApplicationsContainer {
13541372
units: linkingUnits,
13551373
);
13561374

1375+
builder._createLoadLibraryReferences();
1376+
elementFactory.setLibraryTypeSystem(libraryElement);
1377+
13571378
if (inputMacroResult != null) {
13581379
var import = inputLibrary.addMacroPart(
13591380
inputMacroResult.code,

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,8 +310,6 @@ class LinkedElementFactory {
310310
libraryElement.typeProvider = analysisContext.typeProvider;
311311
libraryElement.typeSystem = analysisContext.typeSystem;
312312
libraryElement.hasTypeProviderSystemSet = true;
313-
314-
libraryElement.createLoadLibraryFunction();
315313
}
316314

317315
void _disposeLibrary(Element? libraryElement) {

pkg/analyzer/test/src/dart/resolution/function_reference_test.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2804,11 +2804,11 @@ ExpressionStatement
28042804
period: .
28052805
identifier: SimpleIdentifier
28062806
token: loadLibrary
2807-
staticElement: loadLibrary@-1
2808-
element: <exception>
2807+
staticElement: package:test/a.dart::<fragment>::@function::loadLibrary
2808+
element: package:test/a.dart::@function::loadLibrary
28092809
staticType: Future<dynamic> Function()
2810-
staticElement: loadLibrary@-1
2811-
element: <exception>
2810+
staticElement: package:test/a.dart::<fragment>::@function::loadLibrary
2811+
element: package:test/a.dart::@function::loadLibrary
28122812
staticType: Future<dynamic> Function()
28132813
semicolon: ;
28142814
''');

pkg/analyzer/test/src/dart/resolution/method_invocation_test.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2286,8 +2286,8 @@ MethodInvocation
22862286
operator: ?.
22872287
methodName: SimpleIdentifier
22882288
token: loadLibrary
2289-
staticElement: loadLibrary@-1
2290-
element: <exception>
2289+
staticElement: dart:math::<fragment>::@function::loadLibrary
2290+
element: dart:math::@function::loadLibrary
22912291
staticType: Future<dynamic> Function()
22922292
argumentList: ArgumentList
22932293
leftParenthesis: (
@@ -3554,8 +3554,8 @@ MethodInvocation
35543554
operator: .
35553555
methodName: SimpleIdentifier
35563556
token: loadLibrary
3557-
staticElement: loadLibrary@-1
3558-
element: <exception>
3557+
staticElement: dart:math::<fragment>::@function::loadLibrary
3558+
element: dart:math::@function::loadLibrary
35593559
staticType: Future<dynamic> Function()
35603560
argumentList: ArgumentList
35613561
leftParenthesis: (
@@ -3588,8 +3588,8 @@ MethodInvocation
35883588
operator: .
35893589
methodName: SimpleIdentifier
35903590
token: loadLibrary
3591-
staticElement: loadLibrary@-1
3592-
element: <exception>
3591+
staticElement: dart:math::<fragment>::@function::loadLibrary
3592+
element: dart:math::@function::loadLibrary
35933593
staticType: Future<dynamic> Function()
35943594
argumentList: ArgumentList
35953595
leftParenthesis: (

0 commit comments

Comments
 (0)