Skip to content

Commit

Permalink
Clean up VariableDeclaration and MemberDeclaration APIs.
Browse files Browse the repository at this point in the history
- Adds hasInitializer and hasConst to VariableDeclaration.
- Renames isStatic to hasStatic to be consistent with other members.
- Improves serialization tests by randomizing some values, which
  should help to catch ordering errors in serialization.

Change-Id: I44199b1b058444510b9fa55afe0611187b90fc95
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/352540
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Chloe Stefantsova <cstefantsova@google.com>
Reviewed-by: Morgan :) <davidmorgan@google.com>
Reviewed-by: Chloe Stefantsova <cstefantsova@google.com>
Auto-Submit: Jake Macdonald <jakemac@google.com>
  • Loading branch information
jakemac53 authored and Commit Queue committed Feb 16, 2024
1 parent 5394052 commit 1e0223e
Show file tree
Hide file tree
Showing 11 changed files with 201 additions and 141 deletions.
10 changes: 8 additions & 2 deletions pkg/_fe_analyzer_shared/lib/src/macros/api/introspection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ abstract interface class MemberDeclaration implements Declaration {
/// The type that defines this member.
Identifier get definingType;

/// Whether or not this is a static member.
bool get isStatic;
/// Whether or not member has the `static` keyword.
bool get hasStatic;
}

/// Marker interface for a declaration that defines a new type in the program.
Expand Down Expand Up @@ -288,12 +288,18 @@ abstract interface class ConstructorDeclaration implements MethodDeclaration {

/// Variable introspection information.
abstract interface class VariableDeclaration implements Declaration {
/// Whether this variable has a `const` modifier.
bool get hasConst;

/// Whether this variable has an `external` modifier.
bool get hasExternal;

/// Whether this variable has a `final` modifier.
bool get hasFinal;

/// Whether this variable has an initializer at its declaration.
bool get hasInitializer;

/// Whether this variable has a `late` modifier.
bool get hasLate;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -532,15 +532,15 @@ List<DeclarationCode> _buildVariableAugmentations(
augmentations.add(new DeclarationCode.fromParts([
if (declaration is FieldDeclaration) ' ',
'augment ',
if (declaration is FieldDeclaration && declaration.isStatic) 'static ',
if (declaration is FieldDeclaration && declaration.hasStatic) 'static ',
getter,
]));
}
if (setter != null) {
augmentations.add(new DeclarationCode.fromParts([
if (declaration is FieldDeclaration) ' ',
'augment ',
if (declaration is FieldDeclaration && declaration.isStatic) 'static ',
if (declaration is FieldDeclaration && declaration.hasStatic) 'static ',
setter,
]));
}
Expand All @@ -549,7 +549,7 @@ List<DeclarationCode> _buildVariableAugmentations(
if (initializerDocComments != null) initializerDocComments,
if (declaration is FieldDeclaration) ' ',
'augment ',
if (declaration is FieldDeclaration && declaration.isStatic) 'static ',
if (declaration is FieldDeclaration && declaration.hasStatic) 'static ',
if (declaration.hasFinal) 'final ',
declaration.type.code,
' ',
Expand Down Expand Up @@ -581,7 +581,7 @@ DeclarationCode _buildFunctionAugmentation(
declaration.definingType.name,
if (declaration.identifier.name.isNotEmpty) '.',
] else ...[
if (declaration is MethodDeclaration && declaration.isStatic) 'static ',
if (declaration is MethodDeclaration && declaration.hasStatic) 'static ',
declaration.returnType.code,
' ',
if (declaration.isOperator) 'operator ',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ class MethodDeclarationImpl extends FunctionDeclarationImpl
RemoteInstanceKind get kind => RemoteInstanceKind.methodDeclaration;

@override
final bool isStatic;
final bool hasStatic;

MethodDeclarationImpl({
// Declaration fields.
Expand All @@ -632,15 +632,15 @@ class MethodDeclarationImpl extends FunctionDeclarationImpl
required super.typeParameters,
// Method fields.
required this.definingType,
required this.isStatic,
required this.hasStatic,
});

@override
void serializeUncached(Serializer serializer, {bool isConstructor = false}) {
super.serializeUncached(serializer, isConstructor: isConstructor);

definingType.serialize(serializer);
if (!isConstructor) serializer.addBool(isStatic);
if (!isConstructor) serializer.addBool(hasStatic);
}
}

Expand Down Expand Up @@ -673,7 +673,7 @@ class ConstructorDeclarationImpl extends MethodDeclarationImpl
isGetter: false,
isOperator: false,
isSetter: false,
isStatic: true,
hasStatic: true,
);

@override
Expand All @@ -687,12 +687,18 @@ class ConstructorDeclarationImpl extends MethodDeclarationImpl

class VariableDeclarationImpl extends DeclarationImpl
implements VariableDeclaration {
@override
final bool hasConst;

@override
final bool hasExternal;

@override
final bool hasFinal;

@override
final bool hasInitializer;

@override
final bool hasLate;

Expand All @@ -707,8 +713,10 @@ class VariableDeclarationImpl extends DeclarationImpl
required super.identifier,
required super.library,
required super.metadata,
required this.hasConst,
required this.hasExternal,
required this.hasFinal,
required this.hasInitializer,
required this.hasLate,
required this.type,
});
Expand All @@ -718,8 +726,10 @@ class VariableDeclarationImpl extends DeclarationImpl
super.serializeUncached(serializer);

serializer
..addBool(hasConst)
..addBool(hasExternal)
..addBool(hasFinal)
..addBool(hasInitializer)
..addBool(hasLate);
type.serialize(serializer);
}
Expand All @@ -734,7 +744,7 @@ class FieldDeclarationImpl extends VariableDeclarationImpl
final bool hasAbstract;

@override
final bool isStatic;
final bool hasStatic;

FieldDeclarationImpl({
// Declaration fields.
Expand All @@ -743,14 +753,16 @@ class FieldDeclarationImpl extends VariableDeclarationImpl
required super.library,
required super.metadata,
// Variable fields.
required super.hasConst,
required super.hasExternal,
required super.hasFinal,
required super.hasInitializer,
required super.hasLate,
required super.type,
// Field fields.
required this.definingType,
required this.hasAbstract,
required this.isStatic,
required this.hasStatic,
});

@override
Expand All @@ -763,7 +775,7 @@ class FieldDeclarationImpl extends VariableDeclarationImpl
definingType.serialize(serializer);
serializer
..addBool(hasAbstract)
..addBool(isStatic);
..addBool(hasStatic);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ extension DeserializerExtensions on Deserializer {
returnType: RemoteInstance.deserialize(this),
typeParameters: (this..moveNext())._expectRemoteInstanceList(),
definingType: RemoteInstance.deserialize(this),
isStatic: (this..moveNext()).expectBool(),
hasStatic: (this..moveNext()).expectBool(),
);

ConstructorDeclarationImpl _expectConstructorDeclaration(int id) =>
Expand All @@ -254,25 +254,32 @@ extension DeserializerExtensions on Deserializer {
identifier: expectRemoteInstance(),
library: RemoteInstance.deserialize(this),
metadata: (this..moveNext())._expectRemoteInstanceList(),
hasConst: (this..moveNext()).expectBool(),
hasExternal: (this..moveNext()).expectBool(),
hasFinal: (this..moveNext()).expectBool(),
hasInitializer: (this..moveNext()).expectBool(),
hasLate: (this..moveNext()).expectBool(),
type: RemoteInstance.deserialize(this),
);

FieldDeclarationImpl _expectFieldDeclaration(int id) =>
new FieldDeclarationImpl(
id: id,
// Declaration fields.
identifier: expectRemoteInstance(),
library: RemoteInstance.deserialize(this),
metadata: (this..moveNext())._expectRemoteInstanceList(),
// VariableDeclaration fields
hasConst: (this..moveNext()).expectBool(),
hasExternal: (this..moveNext()).expectBool(),
hasFinal: (this..moveNext()).expectBool(),
hasInitializer: (this..moveNext()).expectBool(),
hasLate: (this..moveNext()).expectBool(),
type: RemoteInstance.deserialize(this),
// FieldDeclaration fields
definingType: RemoteInstance.deserialize(this),
hasAbstract: (this..moveNext()).expectBool(),
isStatic: (this..moveNext()).expectBool(),
hasStatic: (this..moveNext()).expectBool(),
);

ClassDeclarationImpl _expectClassDeclaration(int id) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,10 +393,12 @@ void main() {
metadata: [],
definingType: myEnum.identifier,
hasAbstract: false,
hasConst: false,
hasExternal: false,
hasFinal: true,
hasInitializer: false,
hasLate: false,
isStatic: false,
hasStatic: false,
type: NamedTypeAnnotationImpl(
id: RemoteInstance.uniqueId,
isNullable: false,
Expand Down Expand Up @@ -470,7 +472,7 @@ void main() {
metadata: [],
definingType: myExtension.identifier,
hasExternal: false,
isStatic: false,
hasStatic: false,
returnType: NamedTypeAnnotationImpl(
id: RemoteInstance.uniqueId,
isNullable: false,
Expand Down
Loading

0 comments on commit 1e0223e

Please sign in to comment.