Macro. Return List<ElementAnnotationImpl> from 'ElementImpl.metadata'

So, we can remove casts in a few places.

Change-Id: I3ffa0e081633560f51a72327fd8fdd9e02ac46ac
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/354380
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
Konstantin Shcheglov 2024-02-26 18:45:54 +00:00 committed by Commit Queue
parent 8a62f4ac1a
commit 287d424339
8 changed files with 55 additions and 49 deletions

View file

@ -929,7 +929,7 @@ class LibraryAnalyzer {
void _resolveLibraryExportDirective({
required ExportDirectiveImpl directive,
required LibraryExportElement element,
required LibraryExportElementImpl element,
required LibraryExportState state,
required ErrorReporter errorReporter,
}) {
@ -983,7 +983,7 @@ class LibraryAnalyzer {
void _resolveLibraryImportDirective({
required ImportDirectiveImpl directive,
required LibraryImportElement element,
required LibraryImportElementImpl element,
required LibraryImportState state,
required ErrorReporter errorReporter,
}) {
@ -1013,7 +1013,7 @@ class LibraryAnalyzer {
void _resolvePartDirective({
required PartDirectiveImpl directive,
required PartState partState,
required PartElement partElement,
required PartElementImpl partElement,
required ErrorReporter errorReporter,
required LibraryIdentifier? libraryNameNode,
required Set<Source> seenPartSources,

View file

@ -303,7 +303,7 @@ final class AnnotationImpl extends AstNodeImpl implements Annotation {
/// The element annotation representing this annotation in the element model.
@override
ElementAnnotation? elementAnnotation;
ElementAnnotationImpl? elementAnnotation;
/// Initialize a newly created annotation. Both the [period] and the
/// [constructorName] can be `null` if the annotation is not referencing a
@ -5323,7 +5323,7 @@ sealed class Directive implements AnnotatedNode {
sealed class DirectiveImpl extends AnnotatedNodeImpl implements Directive {
/// The element associated with this directive, or `null` if the AST structure
/// has not been resolved or if this directive could not be resolved.
Element? _element;
ElementImpl? _element;
/// Initialize a newly create directive. Either or both of the [comment] and
/// [metadata] can be `null` if the directive does not have the corresponding
@ -5334,10 +5334,10 @@ sealed class DirectiveImpl extends AnnotatedNodeImpl implements Directive {
});
@override
Element? get element => _element;
ElementImpl? get element => _element;
/// Set the element associated with this directive to be the given [element].
set element(Element? element) {
set element(ElementImpl? element) {
_element = element;
}
}
@ -13805,8 +13805,8 @@ final class PartDirectiveImpl extends UriBasedDirectiveImpl
});
@override
PartElement? get element {
return super.element as PartElement?;
PartElementImpl? get element {
return super.element as PartElementImpl?;
}
@override
@ -15458,7 +15458,7 @@ final class RepresentationDeclarationImpl extends AstNodeImpl
final Token leftParenthesis;
@override
final NodeListImpl<Annotation> fieldMetadata = NodeListImpl._();
final NodeListImpl<AnnotationImpl> fieldMetadata = NodeListImpl._();
@override
final TypeAnnotationImpl fieldType;

View file

@ -173,7 +173,7 @@ abstract class AugmentedInstanceElementImpl
@override
// TODO(scheglov): implement metadata
List<ElementAnnotation> get metadata => throw UnimplementedError();
List<ElementAnnotationImpl> get metadata => throw UnimplementedError();
}
abstract class AugmentedInterfaceElementImpl
@ -860,7 +860,7 @@ class CompilationUnitElementImpl extends UriReferencedElementImpl
ElementKind get kind => ElementKind.COMPILATION_UNIT;
@override
List<ElementAnnotation> get metadata {
List<ElementAnnotationImpl> get metadata {
linkedData?.read(this);
return super.metadata;
}
@ -1913,7 +1913,7 @@ abstract class ElementImpl implements Element {
int _modifiers = 0;
/// A list containing all of the metadata associated with this element.
List<ElementAnnotation> _metadata = const [];
List<ElementAnnotationImpl> _metadata = const [];
/// Cached flags denoting presence of specific annotations in [_metadata].
int _metadataFlags = 0;
@ -2162,8 +2162,7 @@ abstract class ElementImpl implements Element {
final metadata = this.metadata;
for (var i = 0; i < metadata.length; i++) {
var annotation = metadata[i];
if (annotation is ElementAnnotationImpl &&
annotation.isPragmaVmEntryPoint) {
if (annotation.isPragmaVmEntryPoint) {
return true;
}
}
@ -2340,11 +2339,11 @@ abstract class ElementImpl implements Element {
}
@override
List<ElementAnnotation> get metadata {
List<ElementAnnotationImpl> get metadata {
return _metadata;
}
set metadata(List<ElementAnnotation> metadata) {
set metadata(List<ElementAnnotationImpl> metadata) {
_metadata = metadata;
}
@ -2804,7 +2803,7 @@ abstract class ExecutableElementImpl extends _ExistingElementImpl
bool get isSynchronous => !isAsynchronous;
@override
List<ElementAnnotation> get metadata {
List<ElementAnnotationImpl> get metadata {
linkedData?.read(this);
return super.metadata;
}
@ -2942,7 +2941,7 @@ class ExtensionElementImpl extends InstanceElementImpl
ElementKind get kind => ElementKind.EXTENSION;
@override
List<ElementAnnotation> get metadata {
List<ElementAnnotationImpl> get metadata {
linkedData?.read(this);
return super.metadata;
}
@ -3122,7 +3121,7 @@ class FieldElementImpl extends PropertyInducingElementImpl
ElementKind get kind => ElementKind.FIELD;
@override
List<ElementAnnotation> get metadata {
List<ElementAnnotationImpl> get metadata {
linkedData?.read(this);
return super.metadata;
}
@ -3402,7 +3401,7 @@ abstract class InstanceElementImpl extends _ExistingElementImpl
}
@override
List<ElementAnnotation> get metadata {
List<ElementAnnotationImpl> get metadata {
linkedData?.read(this);
return super.metadata;
}
@ -4326,7 +4325,7 @@ class LibraryElementImpl extends LibraryOrAugmentationElementImpl
}
@override
List<ElementAnnotation> get metadata {
List<ElementAnnotationImpl> get metadata {
_readLinkedData();
return super.metadata;
}
@ -5635,7 +5634,9 @@ class MultiplyDefinedElementImpl implements MultiplyDefinedElement {
ElementLocation? get location => null;
@override
List<ElementAnnotation> get metadata => const <ElementAnnotation>[];
List<ElementAnnotationImpl> get metadata {
return const <ElementAnnotationImpl>[];
}
@override
int get nameLength => 0;
@ -5843,7 +5844,7 @@ abstract class NotAugmentedInstanceElementImpl
}
@override
List<ElementAnnotation> get metadata {
List<ElementAnnotationImpl> get metadata {
return element.metadata;
}
@ -6319,7 +6320,7 @@ class PropertyAccessorElementImpl extends ExecutableElementImpl
}
@override
List<ElementAnnotation> get metadata {
List<ElementAnnotationImpl> get metadata {
linkedData?.read(this);
return super.metadata;
}
@ -6727,7 +6728,7 @@ class TopLevelVariableElementImpl extends PropertyInducingElementImpl
ElementKind get kind => ElementKind.TOP_LEVEL_VARIABLE;
@override
List<ElementAnnotation> get metadata {
List<ElementAnnotationImpl> get metadata {
linkedData?.read(this);
return super.metadata;
}
@ -6834,7 +6835,7 @@ class TypeAliasElementImpl extends _ExistingElementImpl
}
@override
List<ElementAnnotation> get metadata {
List<ElementAnnotationImpl> get metadata {
linkedData?.read(this);
return super.metadata;
}

View file

@ -52,7 +52,6 @@ class SinceSdkVersionComputer {
static Version? _specifiedVersion(ElementImpl element) {
Version? result;
for (final annotation in element.metadata) {
annotation as ElementAnnotationImpl;
if (annotation.isDartInternalSince) {
final arguments = annotation.annotationAst.arguments?.arguments;
final versionNode = arguments?.singleOrNull;

View file

@ -201,7 +201,9 @@ class ResolutionVisitor extends RecursiveAstVisitor<void> {
}
@override
void visitAugmentationImportDirective(AugmentationImportDirective node) {
void visitAugmentationImportDirective(
covariant AugmentationImportDirectiveImpl node,
) {
final element = node.element;
if (element is AugmentationImportElementImpl) {
_setOrCreateMetadataElements(element, node.metadata);
@ -547,7 +549,7 @@ class ResolutionVisitor extends RecursiveAstVisitor<void> {
}
@override
void visitExportDirective(ExportDirective node) {
void visitExportDirective(covariant ExportDirectiveImpl node) {
var element = node.element;
if (element is LibraryExportElementImpl) {
_setOrCreateMetadataElements(element, node.metadata);
@ -938,7 +940,7 @@ class ResolutionVisitor extends RecursiveAstVisitor<void> {
}
@override
void visitImportDirective(ImportDirective node) {
void visitImportDirective(covariant ImportDirectiveImpl node) {
var element = node.element;
if (element is LibraryImportElementImpl) {
_setOrCreateMetadataElements(element, node.metadata);
@ -1000,7 +1002,9 @@ class ResolutionVisitor extends RecursiveAstVisitor<void> {
}
@override
void visitLibraryAugmentationDirective(LibraryAugmentationDirective node) {
void visitLibraryAugmentationDirective(
covariant LibraryAugmentationDirectiveImpl node,
) {
final element = node.element;
if (element is LibraryOrAugmentationElementImpl) {
_setOrCreateMetadataElements(element, node.metadata);
@ -1012,7 +1016,7 @@ class ResolutionVisitor extends RecursiveAstVisitor<void> {
}
@override
void visitLibraryDirective(LibraryDirective node) {
void visitLibraryDirective(covariant LibraryDirectiveImpl node) {
++_libraryDirectiveIndex;
var element = node.element;
if (element is LibraryElementImpl && _libraryDirectiveIndex == 1) {
@ -1117,7 +1121,7 @@ class ResolutionVisitor extends RecursiveAstVisitor<void> {
}
@override
void visitPartDirective(PartDirective node) {
void visitPartDirective(covariant PartDirectiveImpl node) {
var element = node.element;
if (element is PartElementImpl) {
_setOrCreateMetadataElements(element, node.metadata);
@ -1374,7 +1378,7 @@ class ResolutionVisitor extends RecursiveAstVisitor<void> {
}
@override
void visitTypeParameter(TypeParameter node) {
void visitTypeParameter(covariant TypeParameterImpl node) {
var element = node.declaredElement as TypeParameterElementImpl;
_setOrCreateMetadataElements(element, node.metadata);
@ -1416,7 +1420,9 @@ class ResolutionVisitor extends RecursiveAstVisitor<void> {
}
@override
void visitVariableDeclarationList(VariableDeclarationList node) {
void visitVariableDeclarationList(
covariant VariableDeclarationListImpl node,
) {
var parent = node.parent;
if (parent is ForPartsWithDeclarations ||
parent is VariableDeclarationStatement &&
@ -1426,10 +1432,10 @@ class ResolutionVisitor extends RecursiveAstVisitor<void> {
node.visitChildren(this);
NodeList<Annotation> annotations;
if (parent is FieldDeclaration) {
NodeList<AnnotationImpl> annotations;
if (parent is FieldDeclarationImpl) {
annotations = parent.metadata;
} else if (parent is TopLevelVariableDeclaration) {
} else if (parent is TopLevelVariableDeclarationImpl) {
annotations = parent.metadata;
} else {
// Local variable declaration
@ -1764,7 +1770,7 @@ class ResolutionVisitor extends RecursiveAstVisitor<void> {
void _setOrCreateMetadataElements(
ElementImpl element,
NodeList<Annotation> annotations, {
NodeList<AnnotationImpl> annotations, {
bool visitNodes = true,
}) {
if (visitNodes) {
@ -1888,7 +1894,9 @@ class ResolutionVisitor extends RecursiveAstVisitor<void> {
/// Associate each of the annotation [nodes] with the corresponding
/// [ElementAnnotation] in [annotations].
static void _setElementAnnotations(
List<Annotation> nodes, List<ElementAnnotation> annotations) {
List<AnnotationImpl> nodes,
List<ElementAnnotationImpl> annotations,
) {
int nodeCount = nodes.length;
if (nodeCount != annotations.length) {
throw StateError(
@ -1897,7 +1905,7 @@ class ResolutionVisitor extends RecursiveAstVisitor<void> {
);
}
for (int i = 0; i < nodeCount; i++) {
(nodes[i] as AnnotationImpl).elementAnnotation = annotations[i];
nodes[i].elementAnnotation = annotations[i];
}
}
}

View file

@ -6373,7 +6373,6 @@ class _MacroDiagnosticsReporter {
static AnnotationImpl _annotationNode(ElementImpl element, int index) {
var annotation = element.metadata[index];
annotation as ElementAnnotationImpl;
return annotation.annotationAst;
}

View file

@ -58,7 +58,7 @@ class ElementBuilder extends ThrowingAstVisitor<void> {
}
/// Build exports and imports, metadata into [_container].
void buildLibraryElementChildren(CompilationUnit unit) {
void buildLibraryElementChildren(CompilationUnitImpl unit) {
unit.directives.accept(this);
if (_isFirstLibraryDirective) {
@ -1246,7 +1246,7 @@ class ElementBuilder extends ThrowingAstVisitor<void> {
node.mixinTypes.accept(this);
}
List<ElementAnnotation> _buildAnnotations(List<Annotation> nodeList) {
List<ElementAnnotationImpl> _buildAnnotations(List<Annotation> nodeList) {
return _buildAnnotationsWithUnit(_unitElement, nodeList);
}
@ -1465,16 +1465,16 @@ class ElementBuilder extends ThrowingAstVisitor<void> {
}
}
static List<ElementAnnotation> _buildAnnotationsWithUnit(
static List<ElementAnnotationImpl> _buildAnnotationsWithUnit(
CompilationUnitElementImpl unitElement,
List<Annotation> nodeList,
) {
var length = nodeList.length;
if (length == 0) {
return const <ElementAnnotation>[];
return const <ElementAnnotationImpl>[];
}
return List<ElementAnnotation>.generate(length, (index) {
return List<ElementAnnotationImpl>.generate(length, (index) {
var ast = nodeList[index] as AnnotationImpl;
var element = ElementAnnotationImpl(unitElement);
element.annotationAst = ast;

View file

@ -324,7 +324,6 @@ class MacroUpdateConstantsForOptimizedCode {
void addElement(ElementImpl element) {
for (var annotation in element.metadata) {
annotation as ElementAnnotationImpl;
result.add((element, annotation.annotationAst));
}