Skip to content

Commit f37e852

Browse files
scheglovCommit Queue
authored andcommitted
Elements. Add 'FragmentName? get name2', to be used instead of name/nameOffset in Fragment.
Change-Id: I3493cf24be8ee877af0a5d01ebe76999ee079c71 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/390301 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent db7dc80 commit f37e852

33 files changed

+1599
-1316
lines changed

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,9 @@ abstract class ConstructorFragment implements ExecutableFragment {
369369
@override
370370
InstanceFragment? get enclosingFragment;
371371

372+
@override
373+
ConstructorFragmentName? get name2;
374+
372375
/// The offset of the end of the name in this fragment.
373376
///
374377
/// Returns `null` if the fragment has no name.
@@ -386,6 +389,12 @@ abstract class ConstructorFragment implements ExecutableFragment {
386389
ConstructorFragment? get previousFragment;
387390
}
388391

392+
/// The name of a [ConstructorFragment].
393+
abstract class ConstructorFragmentName extends FragmentName {
394+
/// The offset of the `.` before the name.
395+
int? get periodOffset;
396+
}
397+
389398
/// The base class for all of the elements in the element model.
390399
///
391400
/// Generally speaking, the element model is a semantic model of the program
@@ -949,6 +958,18 @@ abstract class Fragment {
949958
/// Returns `null` if this fragment doesn't have a name.
950959
String? get name;
951960

961+
/// The name of this fragment.
962+
///
963+
/// Returns `null` if the fragment does not have a name, e.g. an unnamed
964+
/// [ExtensionFragment].
965+
///
966+
/// Returns `null` if the fragment is an unnamed [ConstructorFragment],
967+
/// even if its [ConstructorElement2] has the name `new`.
968+
///
969+
/// Returns `null` if the fragment declaration node does not have the name
970+
/// specified, and the parser inserted a synthetic identifier.
971+
FragmentName? get name2;
972+
952973
/// The offset of the name in this fragment.
953974
///
954975
/// Returns `null` if the fragment has no name.
@@ -975,6 +996,32 @@ abstract class FragmentedElement {
975996
Fragment? get firstFragment;
976997
}
977998

999+
/// The name of a [Fragment].
1000+
abstract class FragmentName {
1001+
/// The name of the fragment.
1002+
///
1003+
/// Never empty.
1004+
///
1005+
/// If a fragment, e.g. an [ExtensionFragment], does not have a name,
1006+
/// then the whole [FragmentName] is `null`.
1007+
///
1008+
/// Similarly, an unnamed [ConstructorFragment] does not have a name, even
1009+
/// if the [ConstructorElement2] has the name `new`.
1010+
///
1011+
/// If the fragment declaration node does not have the name specified, and
1012+
/// the parser inserted a synthetic token, then the whole [FragmentName]
1013+
/// is `null`.
1014+
///
1015+
/// For a [SetterFragment] this is the identifier, without `=` at the end.
1016+
String get name;
1017+
1018+
/// The offset of the end of the name.
1019+
int? get nameEnd;
1020+
1021+
/// The offset of the name in the file.
1022+
int get nameOffset;
1023+
}
1024+
9781025
/// An element that has a [FunctionType] as its [type].
9791026
///
9801027
/// This also provides convenient access to the parameters and return type.

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 = 391;
99+
static const int DATA_VERSION = 393;
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/element/element.dart

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,6 +1041,9 @@ class CompilationUnitElementImpl extends UriReferencedElementImpl
10411041
@override
10421042
List<MixinFragment> get mixins2 => mixins.cast<MixinFragment>();
10431043

1044+
@override
1045+
FragmentName? get name2 => null;
1046+
10441047
@override
10451048
LibraryFragment? get nextFragment {
10461049
var units = library.units;
@@ -1332,6 +1335,9 @@ class ConstructorElementImpl extends ExecutableElementImpl
13321335
@override
13331336
int? nameEnd;
13341337

1338+
@override
1339+
ConstructorFragmentNameImpl? name2;
1340+
13351341
/// For every constructor we initially set this flag to `true`, and then
13361342
/// set it to `false` during computing constant values if we detect that it
13371343
/// is a part of a cycle.
@@ -1637,6 +1643,18 @@ mixin ConstructorElementMixin implements ConstructorElement {
16371643
}
16381644
}
16391645

1646+
class ConstructorFragmentNameImpl extends FragmentNameImpl
1647+
implements ConstructorFragmentName {
1648+
@override
1649+
int periodOffset;
1650+
1651+
ConstructorFragmentNameImpl({
1652+
required super.name,
1653+
required super.nameOffset,
1654+
required this.periodOffset,
1655+
});
1656+
}
1657+
16401658
/// A [TopLevelVariableElement] for a top-level 'const' variable that has an
16411659
/// initializer.
16421660
class ConstTopLevelVariableElementImpl extends TopLevelVariableElementImpl
@@ -3562,6 +3580,21 @@ abstract class ExecutableElementImpl extends _ExistingElementImpl
35623580
return super.name!;
35633581
}
35643582

3583+
@override
3584+
FragmentName? get name2 {
3585+
var name = this.name;
3586+
3587+
// If synthetic name.
3588+
if (name.isEmpty) {
3589+
return null;
3590+
}
3591+
3592+
return FragmentNameImpl(
3593+
name: name,
3594+
nameOffset: nameOffset,
3595+
);
3596+
}
3597+
35653598
@override
35663599
List<ParameterElement> get parameters {
35673600
linkedData?.read(this);
@@ -4704,6 +4737,22 @@ mixin FragmentedTypeParameterizedElementMixin<
47044737
}
47054738
}
47064739

4740+
class FragmentNameImpl implements FragmentName {
4741+
@override
4742+
final String name;
4743+
4744+
@override
4745+
int nameOffset;
4746+
4747+
FragmentNameImpl({
4748+
required this.name,
4749+
required this.nameOffset,
4750+
});
4751+
4752+
@override
4753+
int? get nameEnd => nameOffset + name.length;
4754+
}
4755+
47074756
/// A concrete implementation of a [FunctionElement].
47084757
class FunctionElementImpl extends ExecutableElementImpl
47094758
with AugmentableElement<FunctionElementImpl>
@@ -4884,6 +4933,9 @@ class GenericFunctionTypeElementImpl extends _ExistingElementImpl
48844933
@override
48854934
ElementLinkedData<ElementImpl>? get linkedData => null;
48864935

4936+
@override
4937+
FragmentName? get name2 => null;
4938+
48874939
@override
48884940
GenericFunctionTypeFragment? get nextFragment =>
48894941
throw UnsupportedError('This is not a fragment');
@@ -5142,6 +5194,26 @@ abstract class InstanceElementImpl extends _ExistingElementImpl
51425194
@override
51435195
List<MethodFragment> get methods2 => methods.cast<MethodFragment>();
51445196

5197+
@override
5198+
FragmentName? get name2 {
5199+
var name = this.name;
5200+
5201+
// If unnamed extension.
5202+
if (name == null) {
5203+
return null;
5204+
}
5205+
5206+
// If synthetic name.
5207+
if (name.isEmpty) {
5208+
return null;
5209+
}
5210+
5211+
return FragmentNameImpl(
5212+
name: name,
5213+
nameOffset: nameOffset,
5214+
);
5215+
}
5216+
51455217
@override
51465218
InstanceFragment? get nextFragment => augmentation as InstanceFragment?;
51475219

@@ -8502,6 +8574,19 @@ class ParameterElementImpl extends VariableElementImpl
85028574
LibraryFragment get libraryFragment =>
85038575
thisOrAncestorOfType<CompilationUnitElementImpl>() as LibraryFragment;
85048576

8577+
@override
8578+
FragmentName? get name2 {
8579+
var name = this.name;
8580+
if (name.isEmpty) {
8581+
return null;
8582+
}
8583+
8584+
return FragmentNameImpl(
8585+
name: name,
8586+
nameOffset: nameOffset,
8587+
);
8588+
}
8589+
85058590
@override
85068591
// TODO(augmentations): Support chaining between the fragments.
85078592
FormalParameterFragment? get nextFragment => null;
@@ -8932,6 +9017,14 @@ class PrefixFragmentImpl implements PrefixFragment {
89329017

89339018
@override
89349019
CompilationUnitElementImpl get libraryFragment => enclosingFragment;
9020+
9021+
@override
9022+
FragmentName? get name2 {
9023+
return FragmentNameImpl(
9024+
name: name,
9025+
nameOffset: nameOffset,
9026+
);
9027+
}
89359028
}
89369029

89379030
abstract class PromotableElementImpl2 extends VariableElementImpl2
@@ -9330,6 +9423,21 @@ abstract class PropertyInducingElementImpl
93309423
LibraryFragment get libraryFragment =>
93319424
thisOrAncestorOfType<CompilationUnitElement>() as LibraryFragment;
93329425

9426+
@override
9427+
FragmentName? get name2 {
9428+
var name = this.name;
9429+
9430+
// If synthetic name.
9431+
if (name.isEmpty) {
9432+
return null;
9433+
}
9434+
9435+
return FragmentNameImpl(
9436+
name: name,
9437+
nameOffset: nameOffset,
9438+
);
9439+
}
9440+
93339441
@override
93349442
PropertyInducingFragment? get nextFragment =>
93359443
augmentation as PropertyInducingFragment?;
@@ -9927,6 +10035,21 @@ class TypeAliasElementImpl extends _ExistingElementImpl
992710035
return super.name!;
992810036
}
992910037

10038+
@override
10039+
FragmentName? get name2 {
10040+
var name = this.name;
10041+
10042+
// If synthetic name.
10043+
if (name.isEmpty) {
10044+
return null;
10045+
}
10046+
10047+
return FragmentNameImpl(
10048+
name: name,
10049+
nameOffset: nameOffset,
10050+
);
10051+
}
10052+
993010053
@override
993110054
// TODO(augmentations): Support the fragment chain.
993210055
TypeAliasFragment? get nextFragment => null;
@@ -10213,6 +10336,21 @@ class TypeParameterElementImpl extends ElementImpl
1021310336
return super.name!;
1021410337
}
1021510338

10339+
@override
10340+
FragmentName? get name2 {
10341+
var name = this.name;
10342+
10343+
// If synthetic name.
10344+
if (name.isEmpty) {
10345+
return null;
10346+
}
10347+
10348+
return FragmentNameImpl(
10349+
name: name,
10350+
nameOffset: nameOffset,
10351+
);
10352+
}
10353+
1021610354
@override
1021710355
// TODO(augmentations): Support chaining between the fragments.
1021810356
TypeParameterFragment? get nextFragment => null;

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,13 @@ class LibraryReader {
840840
var reference = _readReference();
841841
var name = reference.elementName.ifEqualThen('new', '');
842842
var element = ConstructorElementImpl(name, -1);
843+
element.name2 = _reader.readOptionalObject((reader) {
844+
return ConstructorFragmentNameImpl(
845+
name: _reader.readStringReference(),
846+
nameOffset: -1,
847+
periodOffset: -1,
848+
);
849+
});
843850
var linkedData = ConstructorElementLinkedData(
844851
reference: reference,
845852
libraryReader: this,

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,9 @@ class BundleWriter {
200200
void _writeConstructorElement(ConstructorElementImpl element) {
201201
_sink.writeUInt30(_resolutionSink.offset);
202202
_writeReference(element);
203+
_sink.writeOptionalObject(element.name2, (name) {
204+
_sink._writeStringReference(name.name);
205+
});
203206
ConstructorElementFlags.write(_sink, element);
204207
_resolutionSink._writeAnnotationList(element.metadata);
205208

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,13 @@ class ElementBuilder extends ThrowingAstVisitor<void> {
215215
element.metadata = _buildAnnotations(node.metadata);
216216
element.nameEnd = nameNode.end;
217217
element.periodOffset = node.period?.offset;
218+
if ((node.period, node.name) case (var period?, var name?)) {
219+
element.name2 = ConstructorFragmentNameImpl(
220+
name: name.lexeme,
221+
nameOffset: name.offset,
222+
periodOffset: period.offset,
223+
);
224+
}
218225
_setCodeRange(element, node);
219226
_setDocumentation(element, node);
220227

@@ -1505,6 +1512,13 @@ class ElementBuilder extends ThrowingAstVisitor<void> {
15051512
..nameEnd = nameEnd
15061513
..parameters = [formalParameterElement]
15071514
..periodOffset = periodOffset;
1515+
if (representation.constructorName case var constructorName?) {
1516+
constructorElement.name2 = ConstructorFragmentNameImpl(
1517+
name: constructorName.name.lexeme,
1518+
nameOffset: constructorName.name.offset,
1519+
periodOffset: constructorName.period.offset,
1520+
);
1521+
}
15081522
_setCodeRange(constructorElement, representation);
15091523

15101524
representation.constructorElement = constructorElement;

0 commit comments

Comments
 (0)