Skip to content

Commit 2726227

Browse files
scheglovCommit Queue
authored andcommitted
Elements. Add ConstructorFragment.typeName and typeNameOffset.
Change-Id: I62f82e96fc14cfd601ed87fefd932d84412186c6 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/394002 Commit-Queue: Konstantin Shcheglov <[email protected]> Reviewed-by: Samuel Rawlins <[email protected]>
1 parent 25c72bc commit 2726227

35 files changed

+1880
-2
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,21 @@ abstract class ConstructorFragment implements ExecutableFragment {
320320

321321
@override
322322
ConstructorFragment? get previousFragment;
323+
324+
/// The specified name of the type (e.g. class).
325+
///
326+
/// In valid code it is the name of the [enclosingFragment], however it
327+
/// could be anything in invalid code, e.g. `class A { A2.named(); }`.
328+
///
329+
/// If the fragment is synthetic, the type name is the name of the
330+
/// enclosing type, which itself can be `null` if its name token is
331+
/// synthetic.
332+
String? get typeName;
333+
334+
/// The offset of the type (e.g. class) name.
335+
///
336+
/// It is `null` if the fragment is synthetic.
337+
int? get typeNameOffset;
323338
}
324339

325340
/// The base class for all of the elements in the element model.

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

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

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

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,7 @@ class ClassElementImpl extends ClassOrMixinElementImpl
534534
var name = superclassConstructor.name;
535535
var implicitConstructor = ConstructorElementImpl(name, -1);
536536
implicitConstructor.isSynthetic = true;
537+
implicitConstructor.typeName = name2;
537538
implicitConstructor.name = name;
538539
implicitConstructor.nameOffset = -1;
539540
implicitConstructor.name2 = superclassConstructor.name2;
@@ -1321,6 +1322,12 @@ class ConstructorElementImpl extends ExecutableElementImpl
13211322
/// instance creation expressions).
13221323
List<ConstructorInitializer> _constantInitializers = const [];
13231324

1325+
@override
1326+
String? typeName;
1327+
1328+
@override
1329+
int? typeNameOffset;
1330+
13241331
@override
13251332
int? periodOffset;
13261333

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,7 @@ class LibraryReader {
781781
var reference = _readReference();
782782
var name = reference.elementName.ifEqualThen('new', '');
783783
var element = ConstructorElementImpl(name, -1);
784+
element.typeName = _reader.readOptionalStringReference();
784785
element.name2 = _reader.readStringReference();
785786
var linkedData = ConstructorElementLinkedData(
786787
reference: reference,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ class BundleWriter {
185185
void _writeConstructorElement(ConstructorElementImpl element) {
186186
_sink.writeUInt30(_resolutionSink.offset);
187187
_writeReference(element);
188+
_sink._writeOptionalStringReference(element.typeName);
188189
_sink._writeStringReference(element.name2);
189190
ConstructorElementFlags.write(_sink, element);
190191
_writeAugmentationTargetAny(element);

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,8 @@ class ElementBuilder extends ThrowingAstVisitor<void> {
227227
element.isExternal = node.externalKeyword != null;
228228
element.isFactory = node.factoryKeyword != null;
229229
element.metadata = _buildAnnotations(node.metadata);
230+
element.typeName = node.returnType.name;
231+
element.typeNameOffset = node.returnType.offset;
230232
element.periodOffset = node.period?.offset;
231233
element.nameEnd = nameNode.end;
232234
if ((node.period, node.name) case (var _?, var name?)) {
@@ -1687,6 +1689,8 @@ class ElementBuilder extends ThrowingAstVisitor<void> {
16871689
..periodOffset = periodOffset
16881690
..nameEnd = nameEnd
16891691
..parameters = [formalParameterElement];
1692+
constructorElement.typeName = extensionElement.name2;
1693+
constructorElement.typeNameOffset = extensionElement.nameOffset2;
16901694
if (representation.constructorName case var constructorName?) {
16911695
constructorElement.name2 = constructorName.name.lexeme;
16921696
constructorElement.nameOffset2 = constructorName.name.offset;

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ class InformativeDataApplier {
306306
(element, info) {
307307
element as ConstructorElementImpl;
308308
element.setCodeRange(info.codeOffset, info.codeLength);
309+
element.typeNameOffset = info.typeNameOffset;
309310
element.periodOffset = info.periodOffset;
310311
element.nameOffset = info.nameOffset;
311312
element.nameEnd = info.nameEnd;
@@ -462,6 +463,7 @@ class InformativeDataApplier {
462463
infoRep.constructorCodeOffset,
463464
infoRep.constructorCodeLength,
464465
);
466+
primaryConstructor.typeNameOffset = infoRep.typeNameOffset;
465467
primaryConstructor.periodOffset = infoRep.constructorPeriodOffset;
466468
primaryConstructor.nameOffset = infoRep.constructorNameOffset;
467469
primaryConstructor.nameEnd = infoRep.constructorNameEnd;
@@ -976,6 +978,7 @@ class _InfoCombinator {
976978
class _InfoConstructorDeclaration {
977979
final int codeOffset;
978980
final int codeLength;
981+
final int? typeNameOffset;
979982
final int? periodOffset;
980983
final int nameOffset;
981984
final int? nameEnd;
@@ -988,6 +991,7 @@ class _InfoConstructorDeclaration {
988991
return _InfoConstructorDeclaration._(
989992
codeOffset: reader.readUInt30(),
990993
codeLength: reader.readUInt30(),
994+
typeNameOffset: reader.readUInt30(),
991995
periodOffset: reader.readOptionalUInt30(),
992996
nameOffset: reader.readUInt30(),
993997
nameEnd: reader.readOptionalUInt30(),
@@ -1003,6 +1007,7 @@ class _InfoConstructorDeclaration {
10031007
_InfoConstructorDeclaration._({
10041008
required this.codeOffset,
10051009
required this.codeLength,
1010+
required this.typeNameOffset,
10061011
required this.periodOffset,
10071012
required this.nameOffset,
10081013
required this.nameEnd,
@@ -1092,6 +1097,7 @@ class _InfoExtensionTypeDeclaration {
10921097
class _InfoExtensionTypeRepresentation {
10931098
final int constructorCodeOffset;
10941099
final int constructorCodeLength;
1100+
final int? typeNameOffset;
10951101
final int? constructorPeriodOffset;
10961102
final int constructorNameOffset;
10971103
final int? constructorNameEnd;
@@ -1106,6 +1112,7 @@ class _InfoExtensionTypeRepresentation {
11061112
return _InfoExtensionTypeRepresentation._(
11071113
constructorCodeOffset: reader.readUInt30(),
11081114
constructorCodeLength: reader.readUInt30(),
1115+
typeNameOffset: reader.readOptionalUInt30(),
11091116
constructorPeriodOffset: reader.readOptionalUInt30(),
11101117
constructorNameOffset: reader.readUInt30(),
11111118
constructorNameEnd: reader.readOptionalUInt30(),
@@ -1121,6 +1128,7 @@ class _InfoExtensionTypeRepresentation {
11211128
_InfoExtensionTypeRepresentation._({
11221129
required this.constructorCodeOffset,
11231130
required this.constructorCodeLength,
1131+
required this.typeNameOffset,
11241132
required this.constructorPeriodOffset,
11251133
required this.constructorNameOffset,
11261134
required this.constructorNameEnd,
@@ -1650,6 +1658,7 @@ class _InformativeDataWriter {
16501658
sink.writeList2<ConstructorDeclaration>(members, (node) {
16511659
sink.writeUInt30(node.offset);
16521660
sink.writeUInt30(node.length);
1661+
sink.writeUInt30(node.returnType.offset);
16531662
sink.writeOptionalUInt30(node.period?.offset);
16541663
var nameNode = node.name ?? node.returnType;
16551664
sink.writeUInt30(nameNode.offset);
@@ -1903,9 +1912,10 @@ class _InformativeDataWriter {
19031912
// Constructor code range.
19041913
sink.writeUInt30(node.offset);
19051914
sink.writeUInt30(node.length);
1915+
sink.writeOptionalUInt30(declaration.name.offsetIfNotEmpty);
19061916

19071917
var constructorName = node.constructorName;
1908-
if (constructorName != null) {
1918+
if (constructorName != null) {
19091919
sink.writeOptionalUInt30(constructorName.period.offset);
19101920
sink.writeUInt30(constructorName.name.offset);
19111921
sink.writeOptionalUInt30(constructorName.name.end);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ class LibraryBuilder with MacroApplicationsContainer {
180180
var reference = containerRef.getChild('new');
181181
reference.element = constructor;
182182
constructor.reference = reference;
183+
constructor.typeName = classElement.name2;
183184
constructor.name2 = 'new';
184185

185186
classElement.constructors = [constructor].toFixedList();
@@ -252,6 +253,7 @@ class LibraryBuilder with MacroApplicationsContainer {
252253
var reference = containerRef.getChild('new');
253254
reference.element = constructor;
254255
constructor.reference = reference;
256+
constructor.typeName = enumElement.name2;
255257
constructor.name2 = 'new';
256258

257259
enumElement.constructors = [

pkg/analyzer/test/src/summary/element_text.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,11 @@ class _Element2Writer extends _AbstractElementWriter {
356356
_writeFragmentCodeRange(f);
357357
// _writeDisplayName(f);
358358

359+
_sink.writelnWithIndent('typeName: ${f.typeName}');
360+
if (f.typeNameOffset case var typeNameOffset?) {
361+
_sink.writelnWithIndent('typeNameOffset: $typeNameOffset');
362+
}
363+
359364
if (f.periodOffset case var periodOffset?) {
360365
_sink.writelnWithIndent('periodOffset: $periodOffset');
361366
}

0 commit comments

Comments
 (0)