Skip to content

Commit e94acd3

Browse files
johnniwintherCommit Queue
authored andcommitted
[cfe] Extract GetterFragment and SetterFragment from MethodFragment
This prepares for creating a PropertyBuilder that uses FieldFragment, GetterFragment, and SetterFragment. Change-Id: I31b9b6c3c1561ef17a561871aea9873f10f4fddc Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/389840 Reviewed-by: Chloe Stefantsova <[email protected]> Commit-Queue: Johnni Winther <[email protected]>
1 parent b18ae2e commit e94acd3

File tree

10 files changed

+608
-134
lines changed

10 files changed

+608
-134
lines changed

pkg/_fe_analyzer_shared/lib/src/scanner/scanner.dart

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,23 @@
44

55
library _fe_analyzer_shared.scanner;
66

7-
import 'dart:typed_data' show Uint8List;
8-
97
import 'dart:convert' show Utf8Encoder, unicodeReplacementCharacterRune;
10-
11-
import 'token.dart' show Token;
8+
import 'dart:typed_data' show Uint8List;
129

1310
import 'abstract_scanner.dart'
1411
show LanguageVersionChanged, ScannerConfiguration;
15-
12+
import 'recover.dart' show scannerRecovery;
1613
import 'string_scanner.dart' show StringScanner;
17-
14+
import 'token.dart' show Token;
1815
import 'utf8_bytes_scanner.dart' show Utf8BytesScanner;
1916

20-
import 'recover.dart' show scannerRecovery;
21-
2217
export 'abstract_scanner.dart'
2318
show LanguageVersionChanged, ScannerConfiguration;
24-
19+
export 'error_token.dart' show ErrorToken, buildUnexpectedCharacterToken;
20+
export 'string_scanner.dart' show StringScanner;
21+
export 'token.dart'
22+
show LanguageVersionToken, Keyword, Token, TokenIsAExtension;
23+
export 'token_constants.dart' show EOF_TOKEN;
2524
export 'token_impl.dart'
2625
show
2726
StringTokenImpl,
@@ -30,19 +29,8 @@ export 'token_impl.dart'
3029
isTernaryOperator,
3130
isUnaryOperator,
3231
isUserDefinableOperator;
33-
34-
export 'error_token.dart' show ErrorToken, buildUnexpectedCharacterToken;
35-
36-
export 'token.dart' show LanguageVersionToken;
37-
38-
export 'token_constants.dart' show EOF_TOKEN;
39-
4032
export 'utf8_bytes_scanner.dart' show Utf8BytesScanner;
4133

42-
export 'string_scanner.dart' show StringScanner;
43-
44-
export 'token.dart' show Keyword, Token;
45-
4634
const int unicodeReplacementCharacter = unicodeReplacementCharacterRune;
4735

4836
typedef Token Recover(List<int> bytes, Token tokens, List<int> lineStarts);

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

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,100 @@ class FieldFragment implements Fragment {
423423
String toString() => '$runtimeType($name,$fileUri,$charOffset)';
424424
}
425425

426+
class GetterFragment implements Fragment {
427+
final String name;
428+
final Uri fileUri;
429+
final int startCharOffset;
430+
final int charOffset;
431+
final int charOpenParenOffset;
432+
final int charEndOffset;
433+
final List<MetadataBuilder>? metadata;
434+
final Modifiers modifiers;
435+
final TypeBuilder returnType;
436+
final List<NominalVariableBuilder>? typeParameters;
437+
final List<FormalParameterBuilder>? formals;
438+
final AsyncMarker asyncModifier;
439+
final String? nativeMethodName;
440+
441+
SourceProcedureBuilder? _builder;
442+
443+
GetterFragment(
444+
{required this.name,
445+
required this.fileUri,
446+
required this.startCharOffset,
447+
required this.charOffset,
448+
required this.charOpenParenOffset,
449+
required this.charEndOffset,
450+
required this.metadata,
451+
required this.modifiers,
452+
required this.returnType,
453+
required this.typeParameters,
454+
required this.formals,
455+
required this.asyncModifier,
456+
required this.nativeMethodName});
457+
458+
@override
459+
SourceProcedureBuilder get builder {
460+
assert(_builder != null, "Builder has not been computed for $this.");
461+
return _builder!;
462+
}
463+
464+
void set builder(SourceProcedureBuilder value) {
465+
assert(_builder == null, "Builder has already been computed for $this.");
466+
_builder = value;
467+
}
468+
469+
@override
470+
String toString() => '$runtimeType($name,$fileUri,$charOffset)';
471+
}
472+
473+
class SetterFragment implements Fragment {
474+
final String name;
475+
final Uri fileUri;
476+
final int startCharOffset;
477+
final int charOffset;
478+
final int charOpenParenOffset;
479+
final int charEndOffset;
480+
final List<MetadataBuilder>? metadata;
481+
final Modifiers modifiers;
482+
final TypeBuilder returnType;
483+
final List<NominalVariableBuilder>? typeParameters;
484+
final List<FormalParameterBuilder>? formals;
485+
final AsyncMarker asyncModifier;
486+
final String? nativeMethodName;
487+
488+
SourceProcedureBuilder? _builder;
489+
490+
SetterFragment(
491+
{required this.name,
492+
required this.fileUri,
493+
required this.startCharOffset,
494+
required this.charOffset,
495+
required this.charOpenParenOffset,
496+
required this.charEndOffset,
497+
required this.metadata,
498+
required this.modifiers,
499+
required this.returnType,
500+
required this.typeParameters,
501+
required this.formals,
502+
required this.asyncModifier,
503+
required this.nativeMethodName});
504+
505+
@override
506+
SourceProcedureBuilder get builder {
507+
assert(_builder != null, "Builder has not been computed for $this.");
508+
return _builder!;
509+
}
510+
511+
void set builder(SourceProcedureBuilder value) {
512+
assert(_builder == null, "Builder has already been computed for $this.");
513+
_builder = value;
514+
}
515+
516+
@override
517+
String toString() => '$runtimeType($name,$fileUri,$charOffset)';
518+
}
519+
426520
class MethodFragment implements Fragment {
427521
final String name;
428522
final Uri fileUri;

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

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ abstract class BuilderFactory {
371371
DeclarationFragment enclosingDeclaration, Identifier identifier,
372372
{isFactory = false});
373373

374-
void addProcedure(
374+
void addMethod(
375375
OffsetMap offsetMap,
376376
List<MetadataBuilder>? metadata,
377377
Modifiers modifiers,
@@ -391,6 +391,44 @@ abstract class BuilderFactory {
391391
required bool isExtensionMember,
392392
required bool isExtensionTypeMember});
393393

394+
void addGetter(
395+
OffsetMap offsetMap,
396+
List<MetadataBuilder>? metadata,
397+
Modifiers modifiers,
398+
TypeBuilder? returnType,
399+
Identifier identifier,
400+
String name,
401+
List<NominalVariableBuilder>? typeVariables,
402+
List<FormalParameterBuilder>? formals,
403+
int startCharOffset,
404+
int charOffset,
405+
int charOpenParenOffset,
406+
int charEndOffset,
407+
String? nativeMethodName,
408+
AsyncMarker asyncModifier,
409+
{required bool isInstanceMember,
410+
required bool isExtensionMember,
411+
required bool isExtensionTypeMember});
412+
413+
void addSetter(
414+
OffsetMap offsetMap,
415+
List<MetadataBuilder>? metadata,
416+
Modifiers modifiers,
417+
TypeBuilder? returnType,
418+
Identifier identifier,
419+
String name,
420+
List<NominalVariableBuilder>? typeVariables,
421+
List<FormalParameterBuilder>? formals,
422+
int startCharOffset,
423+
int charOffset,
424+
int charOpenParenOffset,
425+
int charEndOffset,
426+
String? nativeMethodName,
427+
AsyncMarker asyncModifier,
428+
{required bool isInstanceMember,
429+
required bool isExtensionMember,
430+
required bool isExtensionTypeMember});
431+
394432
void addFields(
395433
OffsetMap offsetMap,
396434
List<MetadataBuilder>? metadata,

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

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import '../base/ignored_parser_errors.dart' show isIgnoredParserError;
3434
import '../base/local_scope.dart';
3535
import '../base/problems.dart' show DebugAbort;
3636
import '../base/scope.dart';
37-
import '../builder/builder.dart';
3837
import '../builder/declaration_builders.dart';
3938
import '../codes/cfe_codes.dart'
4039
show Code, LocatedMessage, Message, messageExpectedBlockToSkip;
@@ -65,7 +64,6 @@ class DietListener extends StackListenerImpl {
6564
final TypeInferenceEngine typeInferenceEngine;
6665

6766
DeclarationBuilder? _currentDeclaration;
68-
ClassBuilder? _currentClass;
6967
bool _inRedirectingFactory = false;
7068

7169
bool currentClassIsParserRecovery = false;
@@ -109,15 +107,12 @@ class DietListener extends StackListenerImpl {
109107

110108
void set currentDeclaration(DeclarationBuilder? builder) {
111109
if (builder == null) {
112-
_currentClass = _currentDeclaration = null;
110+
_currentDeclaration = null;
113111
} else {
114112
_currentDeclaration = builder;
115-
_currentClass = builder is ClassBuilder ? builder : null;
116113
}
117114
}
118115

119-
ClassBuilder? get currentClass => _currentClass;
120-
121116
@override
122117
void endMetadataStar(int count) {
123118
assert(checkState(null, repeatedKind(ValueKinds.Token, count)));
@@ -375,11 +370,22 @@ class DietListener extends StackListenerImpl {
375370
if (name is ParserRecovery) return;
376371

377372
Identifier identifier = name as Identifier;
378-
final BodyBuilder listener = createFunctionListener(
379-
_offsetMap.lookupProcedure(identifier),
380-
inOutlineBuildingPhase: false,
381-
inMetadata: false,
382-
inConstFields: false);
373+
ProcedureKind kind = computeProcedureKind(getOrSet);
374+
SourceFunctionBuilder builder;
375+
switch (kind) {
376+
case ProcedureKind.Method:
377+
case ProcedureKind.Operator:
378+
builder = _offsetMap.lookupMethod(identifier);
379+
case ProcedureKind.Getter:
380+
builder = _offsetMap.lookupGetter(identifier);
381+
case ProcedureKind.Setter:
382+
builder = _offsetMap.lookupSetter(identifier);
383+
// Coverage-ignore(suite): Not run.
384+
case ProcedureKind.Factory:
385+
throw new UnsupportedError("Unexpected procedure kind: $kind");
386+
}
387+
final BodyBuilder listener = createFunctionListener(builder,
388+
inOutlineBuildingPhase: false, inMetadata: false, inConstFields: false);
383389
buildFunctionBody(listener, bodyToken, metadata, MemberKind.TopLevelMethod);
384390
}
385391

@@ -796,18 +802,19 @@ class DietListener extends StackListenerImpl {
796802
if (isConstructor) {
797803
builder = _offsetMap.lookupConstructor(identifier);
798804
} else {
799-
Builder? memberBuilder = _offsetMap.lookupProcedure(identifier);
800-
if (currentClass?.isEnum == true &&
801-
memberBuilder is SourceFieldBuilder &&
802-
// Coverage-ignore(suite): Not run.
803-
memberBuilder.name == "values") {
804-
// This is the case of a method with the name 'values' declared in an
805-
// Enum. In that case the method is replaced with the synthesized field
806-
// in the outline building phase, and the error is reported there. At
807-
// this point we skip the member.
808-
return;
805+
ProcedureKind kind = computeProcedureKind(getOrSet);
806+
switch (kind) {
807+
case ProcedureKind.Method:
808+
case ProcedureKind.Operator:
809+
builder = _offsetMap.lookupMethod(identifier);
810+
case ProcedureKind.Getter:
811+
builder = _offsetMap.lookupGetter(identifier);
812+
case ProcedureKind.Setter:
813+
builder = _offsetMap.lookupSetter(identifier);
814+
// Coverage-ignore(suite): Not run.
815+
case ProcedureKind.Factory:
816+
throw new UnsupportedError("Unexpected procedure kind: $kind");
809817
}
810-
builder = memberBuilder as SourceFunctionBuilder;
811818
}
812819
if (!(builder is SourceExtensionTypeConstructorBuilder &&
813820
builder.isConst)) {

0 commit comments

Comments
 (0)