Skip to content

Commit 1d7b790

Browse files
johnniwintherCommit Queue
authored andcommitted
[cfe] Add ConstructorName
This combines the information about a constructor name needed to refer to its simple and full name and the corresponding offsets into a single class which is then used on fragments. Change-Id: Ibed0456c4517d15fde59f591a05286c5dd2b3204 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/392423 Commit-Queue: Johnni Winther <[email protected]> Reviewed-by: Chloe Stefantsova <[email protected]>
1 parent e5602b3 commit 1d7b790

File tree

9 files changed

+204
-122
lines changed

9 files changed

+204
-122
lines changed

pkg/front_end/lib/src/fragment/constructor.dart

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@
55
part of 'fragment.dart';
66

77
class ConstructorFragment implements Fragment, FunctionFragment {
8-
@override
9-
final String name;
8+
final ConstructorName constructorName;
109

1110
final Uri fileUri;
1211
final int startOffset;
13-
final int nameOffset;
1412
final int formalsOffset;
1513
final int endOffset;
1614
final Modifiers modifiers;
@@ -26,10 +24,9 @@ class ConstructorFragment implements Fragment, FunctionFragment {
2624
AbstractSourceConstructorBuilder? _builder;
2725

2826
ConstructorFragment(
29-
{required this.name,
27+
{required this.constructorName,
3028
required this.fileUri,
3129
required this.startOffset,
32-
required this.nameOffset,
3330
required this.formalsOffset,
3431
required this.endOffset,
3532
required this.modifiers,
@@ -43,6 +40,11 @@ class ConstructorFragment implements Fragment, FunctionFragment {
4340
required Token? beginInitializers})
4441
: _beginInitializers = beginInitializers;
4542

43+
@override
44+
String get name => constructorName.name;
45+
46+
int get fullNameOffset => constructorName.fullNameOffset;
47+
4648
Token? get beginInitializers {
4749
Token? result = _beginInitializers;
4850
// Ensure that we don't hold onto the token.
@@ -67,7 +69,7 @@ class ConstructorFragment implements Fragment, FunctionFragment {
6769
}
6870

6971
@override
70-
String toString() => '$runtimeType($name,$fileUri,$nameOffset)';
72+
String toString() => '$runtimeType($name,$fileUri,$fullNameOffset)';
7173
}
7274

7375
class _ConstructorBodyBuildingContext implements FunctionBodyBuildingContext {

pkg/front_end/lib/src/fragment/factory.dart

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@
55
part of 'fragment.dart';
66

77
class FactoryFragment implements Fragment, FunctionFragment {
8-
@override
9-
final String name;
8+
final ConstructorName constructorName;
109

1110
final Uri fileUri;
1211
final int startOffset;
13-
final int nameOffset;
1412
final int formalsOffset;
1513
final int endOffset;
1614
final Modifiers modifiers;
@@ -26,10 +24,9 @@ class FactoryFragment implements Fragment, FunctionFragment {
2624
SourceFactoryBuilder? _builder;
2725

2826
FactoryFragment(
29-
{required this.name,
27+
{required this.constructorName,
3028
required this.fileUri,
3129
required this.startOffset,
32-
required this.nameOffset,
3330
required this.formalsOffset,
3431
required this.endOffset,
3532
required this.modifiers,
@@ -42,6 +39,11 @@ class FactoryFragment implements Fragment, FunctionFragment {
4239
required this.nativeMethodName,
4340
required this.redirectionTarget});
4441

42+
@override
43+
String get name => constructorName.name;
44+
45+
int get fullNameOffset => constructorName.fullNameOffset;
46+
4547
@override
4648
SourceFactoryBuilder get builder {
4749
assert(_builder != null, "Builder has not been computed for $this.");
@@ -59,7 +61,7 @@ class FactoryFragment implements Fragment, FunctionFragment {
5961
}
6062

6163
@override
62-
String toString() => '$runtimeType($name,$fileUri,$nameOffset)';
64+
String toString() => '$runtimeType($name,$fileUri,$fullNameOffset)';
6365
}
6466

6567
class _FactoryBodyBuildingContext implements FunctionBodyBuildingContext {

pkg/front_end/lib/src/fragment/fragment.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ part 'named_mixin_application.dart';
4545
part 'primary_constructor.dart';
4646
part 'setter.dart';
4747
part 'typedef.dart';
48+
part 'util.dart';
4849

4950
sealed class Fragment {
5051
/// The declared name of this fragment.

pkg/front_end/lib/src/fragment/primary_constructor.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@
55
part of 'fragment.dart';
66

77
class PrimaryConstructorFragment implements Fragment, FunctionFragment {
8-
@override
9-
final String name;
8+
final ConstructorName constructorName;
109

1110
final Uri fileUri;
1211
final int startOffset;
13-
final int? nameOffset;
1412
final int formalsOffset;
1513
final Modifiers modifiers;
1614
final OmittedTypeBuilder returnType;
@@ -23,10 +21,9 @@ class PrimaryConstructorFragment implements Fragment, FunctionFragment {
2321
AbstractSourceConstructorBuilder? _builder;
2422

2523
PrimaryConstructorFragment(
26-
{required this.name,
24+
{required this.constructorName,
2725
required this.fileUri,
2826
required this.startOffset,
29-
required this.nameOffset,
3027
required this.formalsOffset,
3128
required this.modifiers,
3229
required this.returnType,
@@ -37,7 +34,10 @@ class PrimaryConstructorFragment implements Fragment, FunctionFragment {
3734
required Token? beginInitializers})
3835
: _beginInitializers = beginInitializers;
3936

40-
int get fileOffset => nameOffset ?? formalsOffset;
37+
@override
38+
String get name => constructorName.name;
39+
40+
int get fileOffset => constructorName.nameOffset ?? formalsOffset;
4141

4242
Token? get beginInitializers {
4343
Token? result = _beginInitializers;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
part of 'fragment.dart';
6+
7+
class ConstructorName {
8+
/// The name of the constructor itself.
9+
///
10+
/// For an unnamed constructor, this is ''.
11+
final String name;
12+
13+
/// The offset of the name of the constructor, if the constructor is not
14+
/// unnamed.
15+
final int? nameOffset;
16+
17+
/// The name of the constructor including the enclosing declaration name.
18+
final String fullName;
19+
20+
/// The offset at which [fullName] occurs.
21+
///
22+
/// This is used in messages to put the `^` at the start of the [fullName].
23+
final int fullNameOffset;
24+
25+
/// The number of characters of [fullName] that occurs at [fullNameOffset].
26+
///
27+
/// This is used in messages to put the right amount of `^` under the name.
28+
final int fullNameLength;
29+
30+
ConstructorName(
31+
{required this.name,
32+
required this.nameOffset,
33+
required this.fullName,
34+
required this.fullNameOffset,
35+
required this.fullNameLength});
36+
}

pkg/front_end/lib/src/source/builder_factory.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import '../builder/named_type_builder.dart';
2121
import '../builder/nullability_builder.dart';
2222
import '../builder/omitted_type_builder.dart';
2323
import '../builder/type_builder.dart';
24+
import '../fragment/fragment.dart';
2425
import 'offset_map.dart';
2526
import 'source_class_builder.dart';
2627
import 'source_enum_builder.dart';
@@ -319,11 +320,10 @@ abstract class BuilderFactory {
319320
required List<MetadataBuilder>? metadata,
320321
required Modifiers modifiers,
321322
required Identifier identifier,
322-
required String constructorName,
323+
required ConstructorName constructorName,
323324
required List<NominalParameterBuilder>? typeParameters,
324325
required List<FormalParameterBuilder>? formals,
325326
required int startOffset,
326-
required int nameOffset,
327327
required int formalsOffset,
328328
required int endOffset,
329329
required String? nativeMethodName,
@@ -333,7 +333,7 @@ abstract class BuilderFactory {
333333
void addPrimaryConstructor(
334334
{required OffsetMap offsetMap,
335335
required Token beginToken,
336-
required String constructorName,
336+
required String? name,
337337
required List<FormalParameterBuilder>? formals,
338338
required int startOffset,
339339
required int? nameOffset,
@@ -360,7 +360,7 @@ abstract class BuilderFactory {
360360
required String? nativeMethodName,
361361
required AsyncMarker asyncModifier});
362362

363-
String? computeAndValidateConstructorName(
363+
ConstructorName computeAndValidateConstructorName(
364364
DeclarationFragment enclosingDeclaration, Identifier identifier,
365365
{isFactory = false});
366366

@@ -500,6 +500,6 @@ class FieldInfo {
500500
final Token? beforeLast;
501501
final int endOffset;
502502

503-
const FieldInfo(this.identifier, this.initializerToken, this.beforeLast,
504-
this.endOffset);
503+
const FieldInfo(
504+
this.identifier, this.initializerToken, this.beforeLast, this.endOffset);
505505
}

pkg/front_end/lib/src/source/outline_builder.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1588,12 +1588,12 @@ class OutlineBuilder extends StackListenerImpl {
15881588
int? nameOffset;
15891589
int formalsOffset = charOffset;
15901590

1591-
String constructorName = '';
1591+
String? name;
15921592
if (hasConstructorName) {
15931593
// TODO(johnniwinther): Handle [ParserRecovery].
15941594
Identifier identifier = pop() as Identifier;
15951595
nameOffset = charOffset = identifier.nameOffset;
1596-
constructorName = identifier.name;
1596+
name = identifier.name;
15971597
}
15981598

15991599
int? startOffset = constKeyword?.charOffset ?? nameOffset ?? formalsOffset;
@@ -1684,7 +1684,7 @@ class OutlineBuilder extends StackListenerImpl {
16841684
_builderFactory.addPrimaryConstructor(
16851685
offsetMap: _offsetMap,
16861686
beginToken: beginToken,
1687-
constructorName: constructorName == "new" ? "" : constructorName,
1687+
name: name,
16881688
startOffset: startOffset,
16891689
nameOffset: nameOffset,
16901690
formalsOffset: formalsOffset,

0 commit comments

Comments
 (0)