Skip to content

Commit d373eef

Browse files
johnniwintherCommit Queue
authored andcommitted
[cfe] Add FunctionFragment
This replaces the dependency of BodyBuilder on SourceFunctionBuilder with FunctionFragment. Change-Id: Id65b7697a92b9dd39febf1eb077d8cd45522e6d3 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/390602 Reviewed-by: Chloe Stefantsova <[email protected]> Commit-Queue: Johnni Winther <[email protected]>
1 parent a2a03d2 commit d373eef

File tree

12 files changed

+495
-88
lines changed

12 files changed

+495
-88
lines changed

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

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
part of 'fragment.dart';
66

7-
class ConstructorFragment implements Fragment {
7+
class ConstructorFragment implements Fragment, FunctionFragment {
88
final String name;
99
final Uri fileUri;
1010
final int startCharOffset;
@@ -57,6 +57,67 @@ class ConstructorFragment implements Fragment {
5757
_builder = value;
5858
}
5959

60+
@override
61+
FunctionBodyBuildingContext createFunctionBodyBuildingContext() {
62+
return new _ConstructorBodyBuildingContext(this);
63+
}
64+
6065
@override
6166
String toString() => '$runtimeType($name,$fileUri,$charOffset)';
6267
}
68+
69+
class _ConstructorBodyBuildingContext implements FunctionBodyBuildingContext {
70+
ConstructorFragment _fragment;
71+
72+
_ConstructorBodyBuildingContext(this._fragment);
73+
74+
@override
75+
// TODO(johnniwinther): This matches what is passed when parsing, but seems
76+
// odd given that it used to allow 'covariant' modifiers, which shouldn't be
77+
// allowed on constructors.
78+
MemberKind get memberKind => MemberKind.NonStaticMethod;
79+
80+
@override
81+
bool get shouldBuild =>
82+
// TODO(johnniwinther): Ensure building of const extension type
83+
// constructor body. An error is reported by the parser but we skip
84+
// the body here to avoid overwriting the already lowering const
85+
// constructor.
86+
!(_fragment.builder is SourceExtensionTypeConstructorBuilder &&
87+
_fragment.modifiers.isConst);
88+
89+
@override
90+
LocalScope computeFormalParameterScope(LookupScope typeParameterScope) {
91+
return _fragment.builder.computeFormalParameterScope(typeParameterScope);
92+
}
93+
94+
@override
95+
LookupScope computeTypeParameterScope(LookupScope enclosingScope) {
96+
return _fragment.builder.computeTypeParameterScope(enclosingScope);
97+
}
98+
99+
@override
100+
BodyBuilderContext createBodyBuilderContext(
101+
{required bool inOutlineBuildingPhase,
102+
required bool inMetadata,
103+
required bool inConstFields}) {
104+
return _fragment.builder.createBodyBuilderContext(
105+
inOutlineBuildingPhase: inOutlineBuildingPhase,
106+
inMetadata: inMetadata,
107+
inConstFields: inConstFields);
108+
}
109+
110+
@override
111+
InferenceDataForTesting? get inferenceDataForTesting => _fragment
112+
.builder
113+
.dataForTesting
114+
// Coverage-ignore(suite): Not run.
115+
?.inferenceData;
116+
117+
@override
118+
List<TypeParameter>? get thisTypeParameters =>
119+
_fragment.builder.thisTypeParameters;
120+
121+
@override
122+
VariableDeclaration? get thisVariable => _fragment.builder.thisVariable;
123+
}

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

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
part of 'fragment.dart';
66

7-
class FactoryFragment implements Fragment {
7+
class FactoryFragment implements Fragment, FunctionFragment {
88
final String name;
99
final Uri fileUri;
1010
final int startCharOffset;
@@ -49,6 +49,59 @@ class FactoryFragment implements Fragment {
4949
_builder = value;
5050
}
5151

52+
@override
53+
FunctionBodyBuildingContext createFunctionBodyBuildingContext() {
54+
return new _FactoryBodyBuildingContext(this);
55+
}
56+
5257
@override
5358
String toString() => '$runtimeType($name,$fileUri,$charOffset)';
5459
}
60+
61+
class _FactoryBodyBuildingContext implements FunctionBodyBuildingContext {
62+
FactoryFragment _fragment;
63+
64+
_FactoryBodyBuildingContext(this._fragment);
65+
66+
@override
67+
// Coverage-ignore(suite): Not run.
68+
MemberKind get memberKind => MemberKind.Factory;
69+
70+
@override
71+
bool get shouldBuild => true;
72+
73+
@override
74+
LocalScope computeFormalParameterScope(LookupScope typeParameterScope) {
75+
return _fragment.builder.computeFormalParameterScope(typeParameterScope);
76+
}
77+
78+
@override
79+
LookupScope computeTypeParameterScope(LookupScope enclosingScope) {
80+
return _fragment.builder.computeTypeParameterScope(enclosingScope);
81+
}
82+
83+
@override
84+
BodyBuilderContext createBodyBuilderContext(
85+
{required bool inOutlineBuildingPhase,
86+
required bool inMetadata,
87+
required bool inConstFields}) {
88+
return _fragment.builder.createBodyBuilderContext(
89+
inOutlineBuildingPhase: inOutlineBuildingPhase,
90+
inMetadata: inMetadata,
91+
inConstFields: inConstFields);
92+
}
93+
94+
@override
95+
InferenceDataForTesting? get inferenceDataForTesting => _fragment
96+
.builder
97+
.dataForTesting
98+
// Coverage-ignore(suite): Not run.
99+
?.inferenceData;
100+
101+
@override
102+
List<TypeParameter>? get thisTypeParameters =>
103+
_fragment.builder.thisTypeParameters;
104+
105+
@override
106+
VariableDeclaration? get thisVariable => _fragment.builder.thisVariable;
107+
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'package:_fe_analyzer_shared/src/scanner/scanner.dart' show Token;
6+
import 'package:_fe_analyzer_shared/src/parser/member_kind.dart';
67
import 'package:kernel/ast.dart';
78

9+
import '../base/local_scope.dart';
810
import '../base/modifiers.dart';
911
import '../base/scope.dart';
1012
import '../builder/builder.dart';
@@ -14,6 +16,7 @@ import '../builder/formal_parameter_builder.dart';
1416
import '../builder/metadata_builder.dart';
1517
import '../builder/mixin_application_builder.dart';
1618
import '../builder/type_builder.dart';
19+
import '../kernel/body_builder_context.dart';
1720
import '../source/name_scheme.dart';
1821
import '../source/source_class_builder.dart';
1922
import '../source/source_constructor_builder.dart';
@@ -25,6 +28,7 @@ import '../source/source_field_builder.dart';
2528
import '../source/source_procedure_builder.dart';
2629
import '../source/source_type_alias_builder.dart';
2730
import '../source/type_parameter_scope_builder.dart';
31+
import '../type_inference/type_inference_engine.dart';
2832

2933
part 'class.dart';
3034
part 'constructor.dart';
@@ -33,6 +37,7 @@ part 'extension.dart';
3337
part 'extension_type.dart';
3438
part 'factory.dart';
3539
part 'field.dart';
40+
part 'function.dart';
3641
part 'getter.dart';
3742
part 'method.dart';
3843
part 'mixin.dart';
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
/// A fragment with function syntax, i.e. a method, getter, setter, constructor,
8+
/// or factory.
9+
sealed class FunctionFragment {
10+
FunctionBodyBuildingContext createFunctionBodyBuildingContext();
11+
}
12+
13+
abstract class FunctionBodyBuildingContext {
14+
/// Returns the [MemberKind] for the function body being built.
15+
MemberKind get memberKind;
16+
17+
/// Returns `true` if the function body should be built.
18+
// TODO(johnniwinther): Avoid the need for this.
19+
bool get shouldBuild;
20+
21+
BodyBuilderContext createBodyBuilderContext(
22+
{required bool inOutlineBuildingPhase,
23+
required bool inMetadata,
24+
required bool inConstFields});
25+
26+
LookupScope computeTypeParameterScope(LookupScope enclosingScope);
27+
28+
LocalScope computeFormalParameterScope(LookupScope typeParameterScope);
29+
30+
VariableDeclaration? get thisVariable;
31+
32+
List<TypeParameter>? get thisTypeParameters;
33+
34+
InferenceDataForTesting? get inferenceDataForTesting;
35+
}

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

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
part of 'fragment.dart';
66

7-
class GetterFragment implements Fragment {
7+
class GetterFragment implements Fragment, FunctionFragment {
88
final String name;
99
final Uri fileUri;
1010
final int startCharOffset;
1111
final int charOffset;
1212
final int charOpenParenOffset;
1313
final int charEndOffset;
14+
final bool isTopLevel;
1415
final List<MetadataBuilder>? metadata;
1516
final Modifiers modifiers;
1617
final TypeBuilder returnType;
@@ -28,6 +29,7 @@ class GetterFragment implements Fragment {
2829
required this.charOffset,
2930
required this.charOpenParenOffset,
3031
required this.charEndOffset,
32+
required this.isTopLevel,
3133
required this.metadata,
3234
required this.modifiers,
3335
required this.returnType,
@@ -47,6 +49,62 @@ class GetterFragment implements Fragment {
4749
_builder = value;
4850
}
4951

52+
@override
53+
FunctionBodyBuildingContext createFunctionBodyBuildingContext() {
54+
return new _GetterBodyBuildingContext(this);
55+
}
56+
5057
@override
5158
String toString() => '$runtimeType($name,$fileUri,$charOffset)';
5259
}
60+
61+
class _GetterBodyBuildingContext implements FunctionBodyBuildingContext {
62+
GetterFragment _fragment;
63+
64+
_GetterBodyBuildingContext(this._fragment);
65+
66+
@override
67+
MemberKind get memberKind => _fragment.isTopLevel
68+
? MemberKind.TopLevelMethod
69+
: (_fragment.modifiers.isStatic
70+
? MemberKind.StaticMethod
71+
: MemberKind.NonStaticMethod);
72+
73+
@override
74+
bool get shouldBuild => true;
75+
76+
@override
77+
LocalScope computeFormalParameterScope(LookupScope typeParameterScope) {
78+
return _fragment.builder.computeFormalParameterScope(typeParameterScope);
79+
}
80+
81+
@override
82+
LookupScope computeTypeParameterScope(LookupScope enclosingScope) {
83+
return _fragment.builder.computeTypeParameterScope(enclosingScope);
84+
}
85+
86+
@override
87+
BodyBuilderContext createBodyBuilderContext(
88+
{required bool inOutlineBuildingPhase,
89+
required bool inMetadata,
90+
required bool inConstFields}) {
91+
return _fragment.builder.createBodyBuilderContext(
92+
inOutlineBuildingPhase: inOutlineBuildingPhase,
93+
inMetadata: inMetadata,
94+
inConstFields: inConstFields);
95+
}
96+
97+
@override
98+
InferenceDataForTesting? get inferenceDataForTesting => _fragment
99+
.builder
100+
.dataForTesting
101+
// Coverage-ignore(suite): Not run.
102+
?.inferenceData;
103+
104+
@override
105+
List<TypeParameter>? get thisTypeParameters =>
106+
_fragment.builder.thisTypeParameters;
107+
108+
@override
109+
VariableDeclaration? get thisVariable => _fragment.builder.thisVariable;
110+
}

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

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
part of 'fragment.dart';
66

7-
class MethodFragment implements Fragment {
7+
class MethodFragment implements Fragment, FunctionFragment {
88
final String name;
99
final Uri fileUri;
1010
final int startCharOffset;
1111
final int charOffset;
1212
final int charOpenParenOffset;
1313
final int charEndOffset;
14+
final bool isTopLevel;
1415
final List<MetadataBuilder>? metadata;
1516
final Modifiers modifiers;
1617
final TypeBuilder returnType;
@@ -29,6 +30,7 @@ class MethodFragment implements Fragment {
2930
required this.charOffset,
3031
required this.charOpenParenOffset,
3132
required this.charEndOffset,
33+
required this.isTopLevel,
3234
required this.metadata,
3335
required this.modifiers,
3436
required this.returnType,
@@ -49,6 +51,62 @@ class MethodFragment implements Fragment {
4951
_builder = value;
5052
}
5153

54+
@override
55+
FunctionBodyBuildingContext createFunctionBodyBuildingContext() {
56+
return new _MethodBodyBuildingContext(this);
57+
}
58+
5259
@override
5360
String toString() => '$runtimeType($name,$fileUri,$charOffset)';
5461
}
62+
63+
class _MethodBodyBuildingContext implements FunctionBodyBuildingContext {
64+
MethodFragment _fragment;
65+
66+
_MethodBodyBuildingContext(this._fragment);
67+
68+
@override
69+
MemberKind get memberKind => _fragment.isTopLevel
70+
? MemberKind.TopLevelMethod
71+
: (_fragment.modifiers.isStatic
72+
? MemberKind.StaticMethod
73+
: MemberKind.NonStaticMethod);
74+
75+
@override
76+
bool get shouldBuild => true;
77+
78+
@override
79+
LocalScope computeFormalParameterScope(LookupScope typeParameterScope) {
80+
return _fragment.builder.computeFormalParameterScope(typeParameterScope);
81+
}
82+
83+
@override
84+
LookupScope computeTypeParameterScope(LookupScope enclosingScope) {
85+
return _fragment.builder.computeTypeParameterScope(enclosingScope);
86+
}
87+
88+
@override
89+
BodyBuilderContext createBodyBuilderContext(
90+
{required bool inOutlineBuildingPhase,
91+
required bool inMetadata,
92+
required bool inConstFields}) {
93+
return _fragment.builder.createBodyBuilderContext(
94+
inOutlineBuildingPhase: inOutlineBuildingPhase,
95+
inMetadata: inMetadata,
96+
inConstFields: inConstFields);
97+
}
98+
99+
@override
100+
InferenceDataForTesting? get inferenceDataForTesting => _fragment
101+
.builder
102+
.dataForTesting
103+
// Coverage-ignore(suite): Not run.
104+
?.inferenceData;
105+
106+
@override
107+
List<TypeParameter>? get thisTypeParameters =>
108+
_fragment.builder.thisTypeParameters;
109+
110+
@override
111+
VariableDeclaration? get thisVariable => _fragment.builder.thisVariable;
112+
}

0 commit comments

Comments
 (0)