Skip to content

Commit 1172d43

Browse files
scheglovCommit Queue
authored andcommitted
Elements. Simplify applying constant offsets.
Use deferResolutionConstantOffsets() similar to deferReadResolution() instead of directly writing into the field. Use closure to store data intead of explicit ApplyConstantOffsets(). And because management of the "already applied" state it not inside `DeferredResolutionReadingMixin`, avoid null-ing fields. Change-Id: I4816a4f06bb47ec28f6eafa563af710104f15899 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/441400 Reviewed-by: Paul Berry <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent 9890e6a commit 1172d43

File tree

2 files changed

+72
-109
lines changed

2 files changed

+72
-109
lines changed

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ import 'package:analyzer/src/summary2/ast_binary_tokens.dart';
5050
import 'package:analyzer/src/summary2/data_reader.dart';
5151
import 'package:analyzer/src/summary2/data_writer.dart';
5252
import 'package:analyzer/src/summary2/export.dart';
53-
import 'package:analyzer/src/summary2/informative_data.dart';
5453
import 'package:analyzer/src/summary2/reference.dart';
5554
import 'package:analyzer/src/util/file_paths.dart' as file_paths;
5655
import 'package:analyzer/src/utilities/extensions/collection.dart';
@@ -1110,13 +1109,18 @@ mixin DeferredResolutionReadingMixin {
11101109
// TODO(scheglov): review whether we need this
11111110
int _lockResolutionLoading = 0;
11121111
void Function()? _readResolutionCallback;
1113-
ApplyConstantOffsets? applyConstantOffsets;
1112+
void Function()? _applyResolutionConstantOffsets;
11141113

11151114
void deferReadResolution(void Function()? callback) {
11161115
assert(_readResolutionCallback == null);
11171116
_readResolutionCallback = callback;
11181117
}
11191118

1119+
void deferResolutionConstantOffsets(void Function() callback) {
1120+
assert(_applyResolutionConstantOffsets == null);
1121+
_applyResolutionConstantOffsets = callback;
1122+
}
1123+
11201124
void withoutLoadingResolution(void Function() operation) {
11211125
_lockResolutionLoading++;
11221126
operation();
@@ -1133,8 +1137,10 @@ mixin DeferredResolutionReadingMixin {
11331137
callback();
11341138

11351139
// The callback read all AST nodes, apply offsets.
1136-
applyConstantOffsets?.perform();
1137-
applyConstantOffsets = null;
1140+
if (_applyResolutionConstantOffsets case var callback?) {
1141+
_applyResolutionConstantOffsets = null;
1142+
callback();
1143+
}
11381144
}
11391145
}
11401146
}

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

Lines changed: 62 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -25,31 +25,6 @@ Uint8List writeUnitInformative(CompilationUnit unit) {
2525
return sink.takeBytes();
2626
}
2727

28-
/// We want to have actual offsets for tokens of various constants in the
29-
/// element model, such as metadata and constant initializers. But we read
30-
/// these additional pieces of resolution data later, on demand. So, these
31-
/// offsets are different from `nameOffset` for example, which are applied
32-
/// directly after creating corresponding elements during a library loading.
33-
class ApplyConstantOffsets {
34-
Uint32List? _offsets;
35-
void Function(_OffsetsApplier)? _function;
36-
37-
ApplyConstantOffsets(this._offsets, this._function);
38-
39-
void perform() {
40-
var offsets = _offsets;
41-
var function = _function;
42-
if (offsets != null && function != null) {
43-
var applier = _OffsetsApplier(_SafeListIterator(offsets));
44-
function.call(applier);
45-
// Clear the references to possible closure data.
46-
// TODO(scheglov): We want to null the whole `linkedData` instead.
47-
_offsets = null;
48-
_function = null;
49-
}
50-
}
51-
}
52-
5328
class InformativeDataApplier {
5429
final LinkedElementFactory _elementFactory;
5530
final Map<Uri, Uint8List> _unitsInformativeBytes2;
@@ -68,14 +43,13 @@ class InformativeDataApplier {
6843
_applyToImports(unitElement.libraryImports_unresolved, unitInfo);
6944
_applyToExports(unitElement.libraryExports_unresolved, unitInfo);
7045

71-
unitElement.applyConstantOffsets = ApplyConstantOffsets(
72-
unitInfo.libraryConstantOffsets,
73-
(applier) {
74-
applier.applyToImports(unitElement.libraryImports);
75-
applier.applyToExports(unitElement.libraryExports);
76-
applier.applyToParts(unitElement.parts);
77-
},
78-
);
46+
unitElement.deferConstantOffsets(unitInfo.libraryConstantOffsets, (
47+
applier,
48+
) {
49+
applier.applyToImports(unitElement.libraryImports);
50+
applier.applyToExports(unitElement.libraryExports);
51+
applier.applyToParts(unitElement.parts);
52+
});
7953

8054
_applyToAccessors(
8155
unitElement.getters.notSynthetic,
@@ -205,14 +179,11 @@ class InformativeDataApplier {
205179
element.documentationComment = info.documentationComment;
206180
_applyToFormalParameters(element.parameters_unresolved, info.parameters);
207181

208-
element.applyConstantOffsets = ApplyConstantOffsets(
209-
info.constantOffsets,
210-
(applier) {
211-
applier.applyToMetadata(element.metadata);
212-
applier.applyToTypeParameters(element.typeParameters);
213-
applier.applyToFormalParameters(element.parameters_unresolved);
214-
},
215-
);
182+
element.deferConstantOffsets(info.constantOffsets, (applier) {
183+
applier.applyToMetadata(element.metadata);
184+
applier.applyToTypeParameters(element.typeParameters);
185+
applier.applyToFormalParameters(element.parameters_unresolved);
186+
});
216187
});
217188
}
218189

@@ -229,9 +200,7 @@ class InformativeDataApplier {
229200
info.typeParameters,
230201
);
231202

232-
element.applyConstantOffsets = ApplyConstantOffsets(info.constantOffsets, (
233-
applier,
234-
) {
203+
element.deferConstantOffsets(info.constantOffsets, (applier) {
235204
applier.applyToMetadata(element.metadata);
236205
applier.applyToTypeParameters(element.typeParameters);
237206
});
@@ -260,9 +229,7 @@ class InformativeDataApplier {
260229
info.typeParameters,
261230
);
262231

263-
element.applyConstantOffsets = ApplyConstantOffsets(info.constantOffsets, (
264-
applier,
265-
) {
232+
element.deferConstantOffsets(info.constantOffsets, (applier) {
266233
applier.applyToMetadata(element.metadata);
267234
applier.applyToTypeParameters(element.typeParameters);
268235
});
@@ -303,14 +270,11 @@ class InformativeDataApplier {
303270

304271
_applyToFormalParameters(element.parameters_unresolved, info.parameters);
305272

306-
element.applyConstantOffsets = ApplyConstantOffsets(
307-
info.constantOffsets,
308-
(applier) {
309-
applier.applyToMetadata(element.metadata);
310-
applier.applyToFormalParameters(element.parameters);
311-
applier.applyToConstructorInitializers(element);
312-
},
313-
);
273+
element.deferConstantOffsets(info.constantOffsets, (applier) {
274+
applier.applyToMetadata(element.metadata);
275+
applier.applyToFormalParameters(element.parameters);
276+
applier.applyToConstructorInitializers(element);
277+
});
314278
});
315279
}
316280

@@ -333,9 +297,7 @@ class InformativeDataApplier {
333297
_applyToMethods(element.methods, info.methods);
334298
});
335299

336-
element.applyConstantOffsets = ApplyConstantOffsets(info.constantOffsets, (
337-
applier,
338-
) {
300+
element.deferConstantOffsets(info.constantOffsets, (applier) {
339301
applier.applyToMetadata(element.metadata);
340302
applier.applyToTypeParameters(element.typeParameters);
341303
});
@@ -365,9 +327,7 @@ class InformativeDataApplier {
365327
_applyToAccessors(element.setters, info.setters);
366328
_applyToMethods(element.methods, info.methods);
367329

368-
element.applyConstantOffsets = ApplyConstantOffsets(info.constantOffsets, (
369-
applier,
370-
) {
330+
element.deferConstantOffsets(info.constantOffsets, (applier) {
371331
applier.applyToMetadata(element.metadata);
372332
applier.applyToTypeParameters(element.typeParameters);
373333
});
@@ -392,13 +352,12 @@ class InformativeDataApplier {
392352
representationField.nameOffset = infoRep.fieldNameOffset2;
393353
representationField.setCodeRange(infoRep.codeOffset, infoRep.codeLength);
394354

395-
representationField.applyConstantOffsets = ApplyConstantOffsets(
396-
infoRep.fieldConstantOffsets,
397-
(applier) {
398-
_copyOffsetsIntoSyntheticGetterSetter(representationField);
399-
applier.applyToMetadata(representationField.metadata);
400-
},
401-
);
355+
representationField.deferConstantOffsets(infoRep.fieldConstantOffsets, (
356+
applier,
357+
) {
358+
_copyOffsetsIntoSyntheticGetterSetter(representationField);
359+
applier.applyToMetadata(representationField.metadata);
360+
});
402361

403362
element.withoutLoadingResolution(() {
404363
var primaryConstructor = element.constructors.first;
@@ -429,9 +388,7 @@ class InformativeDataApplier {
429388
_applyToMethods(element.methods, info.methods);
430389
});
431390

432-
element.applyConstantOffsets = ApplyConstantOffsets(info.constantOffsets, (
433-
applier,
434-
) {
391+
element.deferConstantOffsets(info.constantOffsets, (applier) {
435392
applier.applyToMetadata(element.metadata);
436393
applier.applyToTypeParameters(element.typeParameters);
437394
});
@@ -447,14 +404,11 @@ class InformativeDataApplier {
447404
element.nameOffset = info.nameOffset2;
448405
element.documentationComment = info.documentationComment;
449406

450-
element.applyConstantOffsets = ApplyConstantOffsets(
451-
info.constantOffsets,
452-
(applier) {
453-
_copyOffsetsIntoSyntheticGetterSetter(element);
454-
applier.applyToMetadata(element.metadata);
455-
applier.applyToConstantInitializer(element);
456-
},
457-
);
407+
element.deferConstantOffsets(info.constantOffsets, (applier) {
408+
_copyOffsetsIntoSyntheticGetterSetter(element);
409+
applier.applyToMetadata(element.metadata);
410+
applier.applyToConstantInitializer(element);
411+
});
458412
});
459413
}
460414

@@ -485,9 +439,7 @@ class InformativeDataApplier {
485439
);
486440
_applyToFormalParameters(element.parameters_unresolved, info.parameters);
487441

488-
element.applyConstantOffsets = ApplyConstantOffsets(info.constantOffsets, (
489-
applier,
490-
) {
442+
element.deferConstantOffsets(info.constantOffsets, (applier) {
491443
applier.applyToMetadata(element.metadata);
492444
applier.applyToTypeParameters(element.typeParameters);
493445
applier.applyToFormalParameters(element.parameters);
@@ -566,12 +518,9 @@ class InformativeDataApplier {
566518
element.nameLength = info.libraryName.length;
567519
element.documentationComment = info.docComment;
568520

569-
element.applyConstantOffsets = ApplyConstantOffsets(
570-
info.libraryConstantOffsets,
571-
(applier) {
572-
applier.applyToMetadata(element.metadata);
573-
},
574-
);
521+
element.deferConstantOffsets(info.libraryConstantOffsets, (applier) {
522+
applier.applyToMetadata(element.metadata);
523+
});
575524
}
576525

577526
void _applyToMethods(
@@ -589,14 +538,11 @@ class InformativeDataApplier {
589538
);
590539
_applyToFormalParameters(element.parameters_unresolved, info.parameters);
591540

592-
element.applyConstantOffsets = ApplyConstantOffsets(
593-
info.constantOffsets,
594-
(applier) {
595-
applier.applyToMetadata(element.metadata);
596-
applier.applyToTypeParameters(element.typeParameters);
597-
applier.applyToFormalParameters(element.parameters);
598-
},
599-
);
541+
element.deferConstantOffsets(info.constantOffsets, (applier) {
542+
applier.applyToMetadata(element.metadata);
543+
applier.applyToTypeParameters(element.typeParameters);
544+
applier.applyToFormalParameters(element.parameters);
545+
});
600546
});
601547
}
602548

@@ -619,9 +565,7 @@ class InformativeDataApplier {
619565
_applyToMethods(element.methods, info.methods);
620566
});
621567

622-
element.applyConstantOffsets = ApplyConstantOffsets(info.constantOffsets, (
623-
applier,
624-
) {
568+
element.deferConstantOffsets(info.constantOffsets, (applier) {
625569
applier.applyToMetadata(element.metadata);
626570
applier.applyToTypeParameters(element.typeParameters);
627571
});
@@ -636,9 +580,7 @@ class InformativeDataApplier {
636580
element.nameOffset = info.nameOffset2;
637581
element.documentationComment = info.documentationComment;
638582

639-
element.applyConstantOffsets = ApplyConstantOffsets(info.constantOffsets, (
640-
applier,
641-
) {
583+
element.deferConstantOffsets(info.constantOffsets, (applier) {
642584
_copyOffsetsIntoSyntheticGetterSetter(element);
643585
applier.applyToMetadata(element.metadata);
644586
applier.applyToConstantInitializer(element);
@@ -686,9 +628,7 @@ class InformativeDataApplier {
686628
List<_InfoFormalParameter>? aliasedFormalParameters,
687629
List<_InfoTypeParameter>? aliasedTypeParameters,
688630
}) {
689-
element.applyConstantOffsets = ApplyConstantOffsets(constantOffsets, (
690-
applier,
691-
) {
631+
element.deferConstantOffsets(constantOffsets, (applier) {
692632
applier.applyToMetadata(element.metadata);
693633
applier.applyToTypeParameters(element.typeParameters);
694634

@@ -2540,6 +2480,23 @@ extension on String {
25402480
}
25412481
}
25422482

2483+
extension on DeferredResolutionReadingMixin {
2484+
/// We want to have actual offsets for tokens of various constants in the
2485+
/// element model, such as metadata and constant initializers. But we read
2486+
/// these additional pieces of resolution data later, on demand. So, these
2487+
/// offsets are different from `nameOffset` for example, which are applied
2488+
/// directly after creating corresponding elements during a library loading.
2489+
void deferConstantOffsets(
2490+
Uint32List constantOffsets,
2491+
void Function(_OffsetsApplier applier) callback,
2492+
) {
2493+
deferResolutionConstantOffsets(() {
2494+
var applier = _OffsetsApplier(_SafeListIterator(constantOffsets));
2495+
callback(applier);
2496+
});
2497+
}
2498+
}
2499+
25432500
extension _ListOfElement<T extends FragmentImpl> on List<T> {
25442501
List<T> get notSynthetic {
25452502
return where((e) => !e.isSynthetic).toList();

0 commit comments

Comments
 (0)