Skip to content

Commit 5078a20

Browse files
johnniwintherCommit Queue
authored andcommitted
[cfe] Use OffsetMap for BodyBuilder.finishFields
Change-Id: I1bc39b3bbd8a78466b90856017b0af4ed1c67d2a Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/401061 Commit-Queue: Johnni Winther <[email protected]> Reviewed-by: Chloe Stefantsova <[email protected]>
1 parent a6b0fce commit 5078a20

File tree

4 files changed

+11
-28
lines changed

4 files changed

+11
-28
lines changed

pkg/front_end/lib/src/kernel/body_builder.dart

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ import '../codes/cfe_codes.dart'
9292
import '../codes/cfe_codes.dart' as cfe;
9393
import '../dill/dill_library_builder.dart' show DillLibraryBuilder;
9494
import '../source/diet_parser.dart';
95+
import '../source/offset_map.dart';
9596
import '../source/source_field_builder.dart';
9697
import '../source/source_library_builder.dart';
9798
import '../source/source_member_builder.dart';
@@ -965,7 +966,7 @@ class BodyBuilder extends StackListenerImpl
965966
assert(checkState(beginToken, [ValueKinds.Integer]));
966967
}
967968

968-
void finishFields() {
969+
void finishFields(OffsetMap offsetMap) {
969970
debugEvent("finishFields");
970971
assert(checkState(null, [/*field count*/ ValueKinds.Integer]));
971972
int count = pop() as int;
@@ -976,27 +977,7 @@ class BodyBuilder extends StackListenerImpl
976977
]));
977978
Expression? initializer = pop() as Expression?;
978979
Identifier identifier = pop() as Identifier;
979-
String name = identifier.name;
980-
Builder declaration = _context.lookupLocalMember(name, required: true)!;
981-
int fileOffset = identifier.nameOffset;
982-
while (declaration.next != null) {
983-
// If we have duplicates, we try to find the right declaration.
984-
if (declaration.fileUri == uri &&
985-
declaration.fileOffset == fileOffset) {
986-
break;
987-
}
988-
declaration = declaration.next!;
989-
}
990-
if (declaration.fileUri != uri || declaration.fileOffset != fileOffset) {
991-
// If we don't have the right declaration, skip the initializer.
992-
continue;
993-
}
994-
SourceFieldBuilder fieldBuilder;
995-
if (declaration.isField) {
996-
fieldBuilder = declaration as SourceFieldBuilder;
997-
} else {
998-
continue;
999-
}
980+
SourceFieldBuilder fieldBuilder = offsetMap.lookupField(identifier);
1000981
if (initializer != null) {
1001982
if (!fieldBuilder.hasBodyBeenBuilt) {
1002983
initializer = typeInferrer

pkg/front_end/lib/src/kernel/body_builder_context.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,7 @@ class _TopLevelBodyBuilderDeclarationContext
655655
: super._(libraryBuilder);
656656

657657
@override
658+
// Coverage-ignore(suite): Not run.
658659
Builder? lookupLocalMember(String name, {bool required = false}) {
659660
return _libraryBuilder.lookupLocalMember(name, required: required);
660661
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -936,6 +936,7 @@ class DietListener extends StackListenerImpl {
936936
// TODO(paulberry): don't re-parse the field if we've already parsed it
937937
// for type inference.
938938
_parseFields(
939+
_offsetMap,
939940
createListener(declaration.createBodyBuilderContext(), memberScope,
940941
inferenceDataForTesting: declaration
941942
.dataForTesting
@@ -1297,8 +1298,8 @@ class DietListener extends StackListenerImpl {
12971298
}
12981299
}
12991300

1300-
void _parseFields(BodyBuilder bodyBuilder, Token startToken, Token? metadata,
1301-
bool isTopLevel) {
1301+
void _parseFields(OffsetMap offsetMap, BodyBuilder bodyBuilder,
1302+
Token startToken, Token? metadata, bool isTopLevel) {
13021303
Token token = startToken;
13031304
Parser parser = new Parser(bodyBuilder,
13041305
useImplicitCreationExpression: useImplicitCreationExpressionInCfe,
@@ -1309,7 +1310,7 @@ class DietListener extends StackListenerImpl {
13091310
// TODO(danrubel): disambiguate between class/mixin/extension members
13101311
token = parser.parseClassMember(metadata ?? token, null).next!;
13111312
}
1312-
bodyBuilder.finishFields();
1313+
bodyBuilder.finishFields(_offsetMap);
13131314

13141315
bodyBuilder.checkEmpty(token.charOffset);
13151316
}

pkg/front_end/test/coverage_suite_expected.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -560,12 +560,12 @@ const Map<String, ({int hitCount, int missCount})> _expect = {
560560
),
561561
// 100.0%.
562562
"package:front_end/src/kernel/body_builder.dart": (
563-
hitCount: 7196,
563+
hitCount: 7180,
564564
missCount: 0,
565565
),
566566
// 100.0%.
567567
"package:front_end/src/kernel/body_builder_context.dart": (
568-
hitCount: 346,
568+
hitCount: 343,
569569
missCount: 0,
570570
),
571571
// 100.0%.
@@ -850,7 +850,7 @@ const Map<String, ({int hitCount, int missCount})> _expect = {
850850
),
851851
// 100.0%.
852852
"package:front_end/src/source/diet_listener.dart": (
853-
hitCount: 657,
853+
hitCount: 659,
854854
missCount: 0,
855855
),
856856
// 100.0%.

0 commit comments

Comments
 (0)