Skip to content

Commit

Permalink
Use AugmentationExecutableElement, AugmentationMethodElement.
Browse files Browse the repository at this point in the history
These become a single property of ExecutableElement, MethodElement.
No new visitors, no new nodes and elements.

Change-Id: Ic523e9d51356e5a8d2a6cd62cd508344f561ffb0
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/312960
Reviewed-by: Samuel Rawlins <srawlins@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
  • Loading branch information
scheglov authored and Commit Queue committed Jul 11, 2023
1 parent 03689db commit 3e80d29
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 157 deletions.
48 changes: 32 additions & 16 deletions pkg/analyzer/lib/dart/element/element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,19 @@ import 'package:analyzer/src/task/api/model.dart' show AnalysisTarget;
import 'package:meta/meta.dart';
import 'package:pub_semver/pub_semver.dart';

/// Information about [ExecutableElement] that is an augmentation.
///
/// Clients may not extend, implement or mix-in this class.
abstract class AugmentationExecutableElement {
/// The element that is augmented by this augmentation.
///
/// The chain of augmentations should normally end with a [ExecutableElement]
/// that is not augmentation, but might end with `null` immediately or
/// after a few intermediate augmentations in case of invalid code when an
/// augmentation is declared without the corresponding declaration.
ExecutableElement? get augmentationTarget;
}

/// A library augmentation import directive within a library.
///
/// Clients may not extend, implement or mix-in this class.
Expand All @@ -77,6 +90,15 @@ abstract class AugmentationImportElement implements _ExistingElement {
DirectiveUri get uri;
}

/// Information about [MethodElement] that is an augmentation.
///
/// Clients may not extend, implement or mix-in this class.
abstract class AugmentationMethodElement
implements AugmentationExecutableElement {
@override
MethodElement? get augmentationTarget;
}

/// The result of applying augmentations to a [ClassElement].
///
/// Clients may not extend, implement or mix-in this class.
Expand Down Expand Up @@ -1294,6 +1316,11 @@ abstract class ExecutableElement implements FunctionTypedElement {
/// Whether the executable element has a body marked as being synchronous.
bool get isSynchronous;

/// The augmentation specific information.
///
/// Not `null` if this [ExecutableElement] is an augmentation.
AugmentationExecutableElement? get maybeAugmentation;

@override
String get name;
}
Expand Down Expand Up @@ -2119,33 +2146,22 @@ abstract class LocalVariableElement implements PromotableElement {
String get name;
}

/// An element that represents a method augmentation defined within a class.
///
/// Clients may not extend, implement or mix-in this class.
abstract class MethodAugmentationElement implements MethodElement {
/// The element that is augmented by this augmentation.
///
/// The chain of augmentations should normally end with a [MethodElement]
/// that is not [MethodAugmentationElement], but might end with `null`
/// immediately or after a few intermediate [MethodAugmentationElement]s in
/// case of invalid code when an augmentation is declared without the
/// corresponding method declaration.
MethodElement? get augmentationTarget;
}

/// An element that represents a method defined within a class.
///
/// Clients may not extend, implement or mix-in this class.
abstract class MethodElement implements ClassMemberElement, ExecutableElement {
/// The immediate augmentation of this element, or `null` if there are no
/// augmentations.
///
/// [MethodAugmentationElement.augmentationTarget] is the back pointer that
/// [AugmentationMethodElement.augmentationTarget] is the back pointer that
/// will point at this element.
MethodAugmentationElement? get augmentation;
MethodElement? get augmentation;

@override
MethodElement get declaration;

@override
AugmentationMethodElement? get maybeAugmentation;
}

/// A class augmentation, defined by a mixin augmentation declaration.
Expand Down
128 changes: 35 additions & 93 deletions pkg/analyzer/lib/src/dart/ast/ast.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11913,6 +11913,9 @@ final class MapPatternImpl extends DartPatternImpl implements MapPattern {
/// children of a class declaration. When the experiment is enabled, these nodes
/// can also be children of an extension declaration.
abstract final class MethodDeclaration implements ClassMember {
/// The token for the 'augment' keyword.
Token? get augmentKeyword;

/// Return the body of the method.
FunctionBody get body;

Expand Down Expand Up @@ -11966,119 +11969,78 @@ abstract final class MethodDeclaration implements ClassMember {
TypeParameterList? get typeParameters;
}

/// A method declaration.
///
/// methodDeclaration ::=
/// methodSignature [FunctionBody]
///
/// methodSignature ::=
/// 'external'? ('abstract' | 'static')? [Type]? ('get' | 'set')?
/// methodName [TypeParameterList] [FormalParameterList]
///
/// methodName ::=
/// [SimpleIdentifier]
/// | 'operator' [SimpleIdentifier]
final class MethodDeclarationImpl extends ClassMemberImpl
implements MethodDeclaration {
/// The token for the 'external' keyword, or `null` if the constructor is not
/// external.
@override
final Token? augmentKeyword;

@override
final Token? externalKeyword;

/// The token representing the 'abstract' or 'static' keyword, or `null` if
/// neither modifier was specified.
@override
final Token? modifierKeyword;

/// The return type of the method, or `null` if no return type was declared.
TypeAnnotationImpl? _returnType;
@override
final TypeAnnotationImpl? returnType;

/// The token representing the 'get' or 'set' keyword, or `null` if this is a
/// method declaration rather than a property declaration.
@override
final Token? propertyKeyword;

/// The token representing the 'operator' keyword, or `null` if this method
/// does not declare an operator.
@override
final Token? operatorKeyword;

@override
final Token name;

/// The type parameters associated with the method, or `null` if the method is
/// not a generic method.
TypeParameterListImpl? _typeParameters;
@override
final TypeParameterListImpl? typeParameters;

/// The parameters associated with the method, or `null` if this method
/// declares a getter.
FormalParameterListImpl? _parameters;
@override
final FormalParameterListImpl? parameters;

/// The body of the method.
FunctionBodyImpl _body;
@override
final FunctionBodyImpl body;

/// Return the element associated with this method, or `null` if the AST
/// structure has not been resolved. The element can either be a
/// [MethodElement], if this represents the declaration of a normal method, or
/// a [PropertyAccessorElement] if this represents the declaration of either a
/// getter or a setter.
@override
ExecutableElementImpl? declaredElement;

/// Initialize a newly created method declaration. Either or both of the
/// [comment] and [metadata] can be `null` if the declaration does not have
/// the corresponding attribute. The [externalKeyword] can be `null` if the
/// method is not external. The [modifierKeyword] can be `null` if the method
/// is neither abstract nor static. The [returnType] can be `null` if no
/// return type was specified. The [propertyKeyword] can be `null` if the
/// method is neither a getter or a setter. The [operatorKeyword] can be
/// `null` if the method does not implement an operator. The [parameters] must
/// be `null` if this method declares a getter.
MethodDeclarationImpl({
required super.comment,
required super.metadata,
required this.augmentKeyword,
required this.externalKeyword,
required this.modifierKeyword,
required TypeAnnotationImpl? returnType,
required this.returnType,
required this.propertyKeyword,
required this.operatorKeyword,
required this.name,
required TypeParameterListImpl? typeParameters,
required FormalParameterListImpl? parameters,
required FunctionBodyImpl body,
}) : _returnType = returnType,
_typeParameters = typeParameters,
_parameters = parameters,
_body = body {
_becomeParentOf(_returnType);
_becomeParentOf(_typeParameters);
_becomeParentOf(_parameters);
_becomeParentOf(_body);
}

@override
FunctionBodyImpl get body => _body;

set body(FunctionBodyImpl functionBody) {
_body = _becomeParentOf(functionBody);
required this.typeParameters,
required this.parameters,
required this.body,
}) {
_becomeParentOf(returnType);
_becomeParentOf(typeParameters);
_becomeParentOf(parameters);
_becomeParentOf(body);
}

@override
Token get endToken => _body.endToken;
Token get endToken => body.endToken;

@override
Token get firstTokenAfterCommentAndMetadata {
return Token.lexicallyFirst(externalKeyword, modifierKeyword) ??
_returnType?.beginToken ??
return augmentKeyword ??
Token.lexicallyFirst(externalKeyword, modifierKeyword) ??
returnType?.beginToken ??
Token.lexicallyFirst(propertyKeyword, operatorKeyword) ??
name;
}

@override
bool get isAbstract {
FunctionBody body = _body;
final body = this.body;
return externalKeyword == null &&
(body is EmptyFunctionBody && !body.semicolon.isSynthetic);
(body is EmptyFunctionBodyImpl && !body.semicolon.isSynthetic);
}

@override
Expand All @@ -12093,29 +12055,9 @@ final class MethodDeclarationImpl extends ClassMemberImpl
@override
bool get isStatic => modifierKeyword?.keyword == Keyword.STATIC;

@override
FormalParameterListImpl? get parameters => _parameters;

set parameters(FormalParameterListImpl? parameters) {
_parameters = _becomeParentOf(parameters);
}

@override
TypeAnnotationImpl? get returnType => _returnType;

set returnType(TypeAnnotationImpl? type) {
_returnType = _becomeParentOf(type);
}

@override
TypeParameterListImpl? get typeParameters => _typeParameters;

set typeParameters(TypeParameterListImpl? typeParameters) {
_typeParameters = _becomeParentOf(typeParameters);
}

@override
ChildEntities get _childEntities => super._childEntities
..addToken('augmentKeyword', augmentKeyword)
..addToken('externalKeyword', externalKeyword)
..addToken('modifierKeyword', modifierKeyword)
..addNode('returnType', returnType)
Expand All @@ -12131,10 +12073,10 @@ final class MethodDeclarationImpl extends ClassMemberImpl
@override
void visitChildren(AstVisitor visitor) {
super.visitChildren(visitor);
_returnType?.accept(visitor);
_typeParameters?.accept(visitor);
_parameters?.accept(visitor);
_body.accept(visitor);
returnType?.accept(visitor);
typeParameters?.accept(visitor);
parameters?.accept(visitor);
body.accept(visitor);
}
}

Expand Down
1 change: 1 addition & 0 deletions pkg/analyzer/lib/src/dart/ast/to_source_visitor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,7 @@ class ToSourceVisitor implements AstVisitor<void> {
@override
void visitMethodDeclaration(MethodDeclaration node) {
_visitNodeList(node.metadata, separator: ' ', suffix: ' ');
_visitToken(node.augmentKeyword, suffix: ' ');
_visitToken(node.externalKeyword, suffix: ' ');
_visitToken(node.modifierKeyword, suffix: ' ');
_visitNode(node.returnType, suffix: ' ');
Expand Down
19 changes: 1 addition & 18 deletions pkg/analyzer/lib/src/dart/ast/utilities.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,7 @@ class AstComparator implements AstVisitor<bool> {
return isEqualNodes(
node.documentationComment, other.documentationComment) &&
_isEqualNodeLists(node.metadata, other.metadata) &&
isEqualTokens(node.augmentKeyword, other.augmentKeyword) &&
isEqualTokens(node.externalKeyword, other.externalKeyword) &&
isEqualTokens(node.modifierKeyword, other.modifierKeyword) &&
isEqualNodes(node.returnType, other.returnType) &&
Expand Down Expand Up @@ -2927,24 +2928,6 @@ class NodeReplacer extends ThrowingAstVisitor<bool> {
return visitNode(node);
}

@override
bool visitMethodDeclaration(covariant MethodDeclarationImpl node) {
if (identical(node.returnType, _oldNode)) {
node.returnType = _newNode as TypeAnnotationImpl;
return true;
} else if (identical(node.parameters, _oldNode)) {
node.parameters = _newNode as FormalParameterListImpl;
return true;
} else if (identical(node.typeParameters, _oldNode)) {
node.typeParameters = _newNode as TypeParameterListImpl;
return true;
} else if (identical(node.body, _oldNode)) {
node.body = _newNode as FunctionBodyImpl;
return true;
}
return visitAnnotatedNode(node);
}

@override
bool visitMethodInvocation(covariant MethodInvocationImpl node) {
if (identical(node.target, _oldNode)) {
Expand Down
Loading

0 comments on commit 3e80d29

Please sign in to comment.