Skip to content

Commit 3f74be8

Browse files
scheglovCommit Queue
authored andcommitted
Elements. Make PrefixFragmentImpl.name2 nullable.
Change-Id: Ifddd477184333e62622615c44da58e51b94f0b92 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/390809 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent e8a9af4 commit 3f74be8

File tree

12 files changed

+139
-42
lines changed

12 files changed

+139
-42
lines changed

pkg/analyzer/lib/dart/element/element2.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -917,7 +917,7 @@ abstract class FragmentName {
917917
String get name;
918918

919919
/// The offset of the end of the name.
920-
int? get nameEnd;
920+
int get nameEnd;
921921

922922
/// The offset of the name in the file.
923923
int get nameOffset;

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

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

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

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1219,11 +1219,19 @@ class FileState {
12191219
UnlinkedLibraryImportPrefix? unlinkedPrefix;
12201220
var prefix = node.prefix;
12211221
if (prefix != null) {
1222+
UnlinkedLibraryImportPrefixName? name;
1223+
if (!prefix.isSynthetic) {
1224+
name = UnlinkedLibraryImportPrefixName(
1225+
name: prefix.name,
1226+
nameOffset: prefix.offset,
1227+
);
1228+
}
1229+
12221230
unlinkedPrefix = UnlinkedLibraryImportPrefix(
12231231
deferredOffset: node.deferredKeyword?.offset,
12241232
asOffset: node.asKeyword!.offset,
1225-
name: prefix.name,
12261233
nameOffset: prefix.offset,
1234+
name: name,
12271235
);
12281236
}
12291237

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

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,28 +260,54 @@ class UnlinkedLibraryImportDirective extends UnlinkedNamespaceDirective {
260260
class UnlinkedLibraryImportPrefix {
261261
final int? deferredOffset;
262262
final int asOffset;
263-
final String name;
264263
final int nameOffset;
264+
final UnlinkedLibraryImportPrefixName? name;
265265

266266
UnlinkedLibraryImportPrefix({
267267
required this.deferredOffset,
268268
required this.asOffset,
269-
required this.name,
270269
required this.nameOffset,
270+
required this.name,
271271
});
272272

273273
factory UnlinkedLibraryImportPrefix.read(SummaryDataReader reader) {
274274
return UnlinkedLibraryImportPrefix(
275275
deferredOffset: reader.readOptionalUInt30(),
276276
asOffset: reader.readUInt30(),
277-
name: reader.readStringUtf8(),
278277
nameOffset: reader.readUInt30(),
278+
name: reader.readOptionalObject((reader) {
279+
return UnlinkedLibraryImportPrefixName.read(reader);
280+
}),
279281
);
280282
}
281283

282284
void write(BufferedSink sink) {
283285
sink.writeOptionalUInt30(deferredOffset);
284286
sink.writeUInt30(asOffset);
287+
sink.writeUInt30(nameOffset);
288+
sink.writeOptionalObject(name, (name) {
289+
name.write(sink);
290+
});
291+
}
292+
}
293+
294+
class UnlinkedLibraryImportPrefixName {
295+
final String name;
296+
final int nameOffset;
297+
298+
UnlinkedLibraryImportPrefixName({
299+
required this.name,
300+
required this.nameOffset,
301+
});
302+
303+
factory UnlinkedLibraryImportPrefixName.read(SummaryDataReader reader) {
304+
return UnlinkedLibraryImportPrefixName(
305+
name: reader.readStringUtf8(),
306+
nameOffset: reader.readUInt30(),
307+
);
308+
}
309+
310+
void write(BufferedSink sink) {
285311
sink.writeStringUtf8(name);
286312
sink.writeUInt30(nameOffset);
287313
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4738,7 +4738,7 @@ class FragmentNameImpl implements FragmentName {
47384738
});
47394739

47404740
@override
4741-
int? get nameEnd => nameOffset + name.length;
4741+
int get nameEnd => nameOffset + name.length;
47424742
}
47434743

47444744
/// A concrete implementation of a [FunctionElement].
@@ -9152,7 +9152,7 @@ class PrefixFragmentImpl implements PrefixFragment {
91529152
int nameOffset;
91539153

91549154
@override
9155-
FragmentNameImpl name2;
9155+
FragmentNameImpl? name2;
91569156

91579157
@override
91589158
final bool isDeferred;

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,10 +1270,8 @@ class LibraryReader {
12701270
required LibraryOrAugmentationElementImpl containerLibrary,
12711271
required CompilationUnitElementImpl containerUnit,
12721272
}) {
1273-
PrefixElementImpl buildElement(String name) {
1273+
PrefixElementImpl buildElement(String name, Reference reference) {
12741274
// TODO(scheglov): Make reference required.
1275-
var containerRef = containerUnit.reference!;
1276-
var reference = containerRef.getChild('@prefix').getChild(name);
12771275
var existing = reference.element;
12781276
if (existing is PrefixElementImpl) {
12791277
return existing;
@@ -1290,13 +1288,15 @@ class LibraryReader {
12901288
switch (kind) {
12911289
case ImportElementPrefixKind.isDeferred:
12921290
var name = _reader.readStringReference();
1291+
var reference = _readReference();
12931292
return DeferredImportElementPrefixImpl(
1294-
element: buildElement(name),
1293+
element: buildElement(name, reference),
12951294
);
12961295
case ImportElementPrefixKind.isNotDeferred:
12971296
var name = _reader.readStringReference();
1297+
var reference = _readReference();
12981298
return ImportElementPrefixImpl(
1299-
element: buildElement(name),
1299+
element: buildElement(name, reference),
13001300
);
13011301
case ImportElementPrefixKind.isNull:
13021302
return null;
@@ -1322,20 +1322,19 @@ class LibraryReader {
13221322
required CompilationUnitElementImpl libraryFragment,
13231323
}) {
13241324
return _reader.readOptionalObject((reader) {
1325+
var fragmentName = _readFragmentName();
1326+
var reference = _readReference();
13251327
var name = _reader.readStringReference();
13261328
var isDeferred = _reader.readBool();
13271329
var fragment = PrefixFragmentImpl(
13281330
enclosingFragment: libraryFragment,
13291331
name: name,
13301332
nameOffset: -1,
1331-
name2: FragmentNameImpl(name: name, nameOffset: -1),
1333+
name2: fragmentName,
13321334
isDeferred: isDeferred,
13331335
);
13341336

1335-
var containerRef = libraryFragment.reference!;
1336-
var reference = containerRef.getChild('@prefix2').getChild(name);
13371337
var element = reference.element2 as PrefixElementImpl2?;
1338-
13391338
if (element == null) {
13401339
element = PrefixElementImpl2(
13411340
reference: reference,

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -474,13 +474,15 @@ class BundleWriter {
474474
LibraryImportElementFlags.write(_sink, element);
475475
}
476476

477-
void _writeImportElementPrefix(ImportElementPrefix? prefix) {
478-
if (prefix is DeferredImportElementPrefix) {
477+
void _writeImportElementPrefix(ImportElementPrefixImpl? prefix) {
478+
if (prefix is DeferredImportElementPrefixImpl) {
479479
_sink.writeByte(ImportElementPrefixKind.isDeferred.index);
480480
_sink._writeStringReference(prefix.element.name);
481-
} else if (prefix is ImportElementPrefix) {
481+
_writeReference(prefix.element);
482+
} else if (prefix is ImportElementPrefixImpl) {
482483
_sink.writeByte(ImportElementPrefixKind.isNotDeferred.index);
483484
_sink._writeStringReference(prefix.element.name);
485+
_writeReference(prefix.element);
484486
} else {
485487
_sink.writeByte(ImportElementPrefixKind.isNull.index);
486488
}
@@ -502,6 +504,8 @@ class BundleWriter {
502504

503505
void _writeLibraryImportPrefixFragment(PrefixFragmentImpl? fragment) {
504506
_sink.writeOptionalObject(fragment, (fragment) {
507+
_writeFragmentName(fragment.name2);
508+
_writeReference2(fragment.element.reference);
505509
_sink._writeStringReference(fragment.name);
506510
_sink.writeBool(fragment.isDeferred);
507511
});
@@ -676,7 +680,10 @@ class BundleWriter {
676680

677681
/// Write the reference of a non-local element.
678682
void _writeReference(ElementImpl element) {
679-
var reference = element.reference;
683+
_writeReference2(element.reference);
684+
}
685+
686+
void _writeReference2(Reference? reference) {
680687
var index = _references._indexOfReference(reference);
681688
_sink.writeUInt30(index);
682689
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class ElementBuilder extends ThrowingAstVisitor<void> {
3030
var _partDirectiveIndex = 0;
3131

3232
_EnclosingContext _enclosingContext;
33-
var _nextUnnamedId = 0;
33+
int _nextUnnamedId = 0;
3434

3535
ElementBuilder({
3636
required LibraryBuilder libraryBuilder,

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,9 @@ class InformativeDataApplier {
674674

675675
if (element.prefix2 case var prefixFragment?) {
676676
prefixFragment.nameOffset = info.prefixOffset;
677-
prefixFragment.name2.nameOffset = info.prefixOffset;
677+
if (prefixFragment.name2 case var name?) {
678+
name.nameOffset = info.prefixOffset;
679+
}
678680
}
679681
_applyToCombinators(element.combinators, info.combinators);
680682
},
@@ -1369,12 +1371,14 @@ class _InfoGenericTypeAlias {
13691371
class _InfoImport {
13701372
final int nameOffset;
13711373
final int prefixOffset;
1374+
final int? prefixOffset2;
13721375
final List<_InfoCombinator> combinators;
13731376

13741377
factory _InfoImport(SummaryDataReader reader) {
13751378
return _InfoImport._(
13761379
nameOffset: reader.readUInt30(),
13771380
prefixOffset: reader.readUInt30() - 1,
1381+
prefixOffset2: reader.readOptionalUInt30(),
13781382
combinators: reader.readTypedList(
13791383
() => _InfoCombinator(reader),
13801384
),
@@ -1384,6 +1388,7 @@ class _InfoImport {
13841388
_InfoImport._({
13851389
required this.nameOffset,
13861390
required this.prefixOffset,
1391+
required this.prefixOffset2,
13871392
required this.combinators,
13881393
});
13891394
}
@@ -1477,6 +1482,7 @@ class _InformativeDataWriter {
14771482
sink.writeList2<ImportDirective>(unit.directives, (directive) {
14781483
sink.writeUInt30(directive.importKeyword.offset);
14791484
sink.writeUInt30(1 + (directive.prefix?.offset ?? -1));
1485+
sink.writeOptionalUInt30(directive.prefix?.token.offsetIfNotEmpty);
14801486
_writeCombinators(directive.combinators);
14811487
});
14821488

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

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ class LibraryBuilder with MacroApplicationsContainer {
102102
/// The `export` directives that export this library.
103103
final List<Export> exports = [];
104104

105+
/// The identifier of the reference used for unnamed fragments.
106+
int _nextUnnamedId = 0;
107+
105108
/// The fields that were speculatively created as [ConstFieldElementImpl],
106109
/// but we want to clear [ConstVariableElement.constantInitializer] for it
107110
/// if the class will not end up with a `const` constructor. We don't know
@@ -1074,8 +1077,8 @@ class LibraryBuilder with MacroApplicationsContainer {
10741077
}) {
10751078
var importPrefix = state.unlinked.prefix.mapOrNull((unlinked) {
10761079
var prefix = _buildLibraryImportPrefix(
1077-
name: unlinked.name,
10781080
nameOffset: unlinked.nameOffset,
1081+
name: unlinked.name,
10791082
containerLibrary: containerLibrary,
10801083
containerUnit: containerUnit,
10811084
);
@@ -1093,8 +1096,7 @@ class LibraryBuilder with MacroApplicationsContainer {
10931096
var prefixFragment = state.unlinked.prefix.mapOrNull((unlinked) {
10941097
return _buildLibraryImportPrefixFragment(
10951098
libraryFragment: containerUnit,
1096-
name: unlinked.name,
1097-
nameOffset: unlinked.nameOffset,
1099+
unlinkedName: unlinked.name,
10981100
isDeferred: unlinked.deferredOffset != null,
10991101
);
11001102
});
@@ -1171,20 +1173,21 @@ class LibraryBuilder with MacroApplicationsContainer {
11711173
}
11721174

11731175
PrefixElementImpl _buildLibraryImportPrefix({
1174-
required String name,
11751176
required int nameOffset,
1177+
required UnlinkedLibraryImportPrefixName? name,
11761178
required LibraryOrAugmentationElementImpl containerLibrary,
11771179
required CompilationUnitElementImpl containerUnit,
11781180
}) {
11791181
// TODO(scheglov): Make reference required.
11801182
var containerRef = containerUnit.reference!;
1181-
var reference = containerRef.getChild('@prefix').getChild(name);
1183+
var refName = name?.name ?? '${_nextUnnamedId++}';
1184+
var reference = containerRef.getChild('@prefix').getChild(refName);
11821185
var existing = reference.element;
11831186
if (existing is PrefixElementImpl) {
11841187
return existing;
11851188
} else {
11861189
var result = PrefixElementImpl(
1187-
name,
1190+
name?.name ?? '',
11881191
nameOffset,
11891192
reference: reference,
11901193
);
@@ -1196,23 +1199,28 @@ class LibraryBuilder with MacroApplicationsContainer {
11961199

11971200
PrefixFragmentImpl _buildLibraryImportPrefixFragment({
11981201
required CompilationUnitElementImpl libraryFragment,
1199-
required String name,
1200-
required int nameOffset,
1202+
required UnlinkedLibraryImportPrefixName? unlinkedName,
12011203
required bool isDeferred,
12021204
}) {
1205+
FragmentNameImpl? fragmentName;
1206+
if (unlinkedName != null) {
1207+
fragmentName = FragmentNameImpl(
1208+
name: unlinkedName.name,
1209+
nameOffset: unlinkedName.nameOffset,
1210+
);
1211+
}
1212+
12031213
var fragment = PrefixFragmentImpl(
12041214
enclosingFragment: libraryFragment,
1205-
name: name,
1206-
name2: FragmentNameImpl(
1207-
name: name,
1208-
nameOffset: nameOffset,
1209-
),
1210-
nameOffset: nameOffset,
1215+
name: unlinkedName?.name ?? '',
1216+
nameOffset: unlinkedName?.nameOffset ?? -1,
1217+
name2: fragmentName,
12111218
isDeferred: isDeferred,
12121219
);
12131220

12141221
var containerRef = libraryFragment.reference!;
1215-
var reference = containerRef.getChild('@prefix2').getChild(name);
1222+
var refName = unlinkedName?.name ?? '${_nextUnnamedId++}';
1223+
var reference = containerRef.getChild('@prefix2').getChild(refName);
12161224
var element = reference.element2 as PrefixElementImpl2?;
12171225

12181226
if (element == null) {

0 commit comments

Comments
 (0)