Skip to content

Commit 105711a

Browse files
fshcheglovCommit Queue
authored andcommitted
Generate getters and setters for modifiers in fragments.
I noticed that modifiers were always getter/setter pairs and had names that were derived from the modifier names. Because of this, the entire code could be reduced to just the modifier's name. For now I only annotated and generated for `ClassFragmentImpl`, `ConstructorFragmentImpl`, and `VariableFragmentImpl`. WDYT? If this looks good, I will follow with a CL to generate all modifiers for all fragments. Change-Id: I02d4243706afb49f0cab15198816f6d0e33e165b Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/443461 Reviewed-by: Johnni Winther <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]>
1 parent 41709a9 commit 105711a

File tree

6 files changed

+507
-178
lines changed

6 files changed

+507
-178
lines changed

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

Lines changed: 66 additions & 178 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ import 'package:analyzer/src/utilities/extensions/object.dart';
5858
import 'package:collection/collection.dart';
5959
import 'package:pub_semver/pub_semver.dart';
6060

61+
part 'element.g.dart';
62+
6163
// TODO(fshcheglov): Remove after third_party/pkg/dartdoc stops using it.
6264
// https://github.com/dart-lang/dartdoc/issues/4066
6365
@Deprecated('Use VariableFragmentImpl instead')
@@ -607,87 +609,26 @@ class ClassElementImpl extends InterfaceElementImpl implements ClassElement {
607609
}
608610

609611
/// An [InterfaceFragmentImpl] which is a class.
610-
class ClassFragmentImpl extends InterfaceFragmentImpl implements ClassFragment {
612+
@GenerateFragmentImpl(modifiers: _ClassFragmentImplModifiers.values)
613+
class ClassFragmentImpl extends InterfaceFragmentImpl
614+
with _ClassFragmentImplMixin
615+
implements ClassFragment {
611616
@override
612617
late final ClassElementImpl element;
613618

614619
/// Initialize a newly created class element to have the given [name] at the
615620
/// given [offset] in the file that contains the declaration of this element.
616621
ClassFragmentImpl({required super.name});
617622

618-
bool get hasExtendsClause {
619-
return hasModifier(Modifier.HAS_EXTENDS_CLAUSE);
620-
}
621-
622-
set hasExtendsClause(bool value) {
623-
setModifier(Modifier.HAS_EXTENDS_CLAUSE, value);
624-
}
625-
626623
bool get hasGenerativeConstConstructor {
624+
_ClassFragmentImplModifiers.hasExtendsClause;
627625
return constructors.any((c) => !c.isFactory && c.isConst);
628626
}
629627

630-
bool get isAbstract {
631-
return hasModifier(Modifier.ABSTRACT);
632-
}
633-
634-
set isAbstract(bool isAbstract) {
635-
setModifier(Modifier.ABSTRACT, isAbstract);
636-
}
637-
638-
bool get isBase {
639-
return hasModifier(Modifier.BASE);
640-
}
641-
642-
set isBase(bool isBase) {
643-
setModifier(Modifier.BASE, isBase);
644-
}
645-
646628
bool get isConstructable => !isSealed && !isAbstract;
647629

648630
bool get isExhaustive => isSealed;
649631

650-
bool get isFinal {
651-
return hasModifier(Modifier.FINAL);
652-
}
653-
654-
set isFinal(bool isFinal) {
655-
setModifier(Modifier.FINAL, isFinal);
656-
}
657-
658-
bool get isInterface {
659-
return hasModifier(Modifier.INTERFACE);
660-
}
661-
662-
set isInterface(bool isInterface) {
663-
setModifier(Modifier.INTERFACE, isInterface);
664-
}
665-
666-
bool get isMixinApplication {
667-
return hasModifier(Modifier.MIXIN_APPLICATION);
668-
}
669-
670-
/// Set whether this class is a mixin application.
671-
set isMixinApplication(bool isMixinApplication) {
672-
setModifier(Modifier.MIXIN_APPLICATION, isMixinApplication);
673-
}
674-
675-
bool get isMixinClass {
676-
return hasModifier(Modifier.MIXIN_CLASS);
677-
}
678-
679-
set isMixinClass(bool isMixinClass) {
680-
setModifier(Modifier.MIXIN_CLASS, isMixinClass);
681-
}
682-
683-
bool get isSealed {
684-
return hasModifier(Modifier.SEALED);
685-
}
686-
687-
set isSealed(bool isSealed) {
688-
setModifier(Modifier.SEALED, isSealed);
689-
}
690-
691632
bool get isValidMixin {
692633
var supertype = this.supertype;
693634
if (supertype != null && !supertype.isDartCoreObject) {
@@ -924,7 +865,9 @@ class ConstructorElementImpl extends ExecutableElementImpl
924865
}
925866

926867
/// A concrete implementation of a [ConstructorFragment].
868+
@GenerateFragmentImpl(modifiers: _ConstructorFragmentImplModifiers.values)
927869
class ConstructorFragmentImpl extends ExecutableFragmentImpl
870+
with _ConstructorFragmentImplMixin
928871
implements ConstructorFragment {
929872
late final ConstructorElementImpl element;
930873

@@ -997,16 +940,6 @@ class ConstructorFragmentImpl extends ExecutableFragmentImpl
997940
InterfaceFragmentImpl get enclosingFragment =>
998941
super.enclosingFragment as InterfaceFragmentImpl;
999942

1000-
/// Whether the constructor is a const constructor.
1001-
bool get isConst {
1002-
return hasModifier(Modifier.CONST);
1003-
}
1004-
1005-
/// Set whether this constructor represents a 'const' constructor.
1006-
set isConst(bool isConst) {
1007-
setModifier(Modifier.CONST, isConst);
1008-
}
1009-
1010943
/// Whether the constructor can be used as a default constructor - unnamed,
1011944
/// and has no required parameters.
1012945
bool get isDefaultConstructor {
@@ -1024,16 +957,6 @@ class ConstructorFragmentImpl extends ExecutableFragmentImpl
1024957
return true;
1025958
}
1026959

1027-
/// Whether the constructor represents a factory constructor.
1028-
bool get isFactory {
1029-
return hasModifier(Modifier.FACTORY);
1030-
}
1031-
1032-
/// Set whether this constructor represents a factory method.
1033-
set isFactory(bool isFactory) {
1034-
setModifier(Modifier.FACTORY, isFactory);
1035-
}
1036-
1037960
/// Whether the constructor represents a generative constructor.
1038961
bool get isGenerative {
1039962
return !isFactory;
@@ -2383,12 +2306,12 @@ abstract class ExecutableFragmentImpl extends FragmentImpl
23832306
/// Whether the executable element did not have an explicit return type
23842307
/// specified for it in the original source.
23852308
bool get hasImplicitReturnType {
2386-
return hasModifier(Modifier.IMPLICIT_TYPE);
2309+
return hasModifier(Modifier.HAS_IMPLICIT_TYPE);
23872310
}
23882311

23892312
/// Set whether this executable element has an implicit return type.
23902313
set hasImplicitReturnType(bool hasImplicitReturnType) {
2391-
setModifier(Modifier.IMPLICIT_TYPE, hasImplicitReturnType);
2314+
setModifier(Modifier.HAS_IMPLICIT_TYPE, hasImplicitReturnType);
23922315
}
23932316

23942317
bool get invokesSuperSelf {
@@ -3001,14 +2924,6 @@ class FieldFragmentImpl extends PropertyInducingFragmentImpl
30012924
setModifier(Modifier.NO_ENCLOSING_TYPE_PARAMETER_REFERENCE, !value);
30022925
}
30032926

3004-
/// Whether the field is abstract.
3005-
///
3006-
/// Executable fields are abstract if they are declared with the `abstract`
3007-
/// keyword.
3008-
bool get isAbstract {
3009-
return hasModifier(Modifier.ABSTRACT);
3010-
}
3011-
30122927
/// Whether the field was explicitly marked as being covariant.
30132928
bool get isCovariant {
30142929
return hasModifier(Modifier.COVARIANT);
@@ -3028,11 +2943,6 @@ class FieldFragmentImpl extends PropertyInducingFragmentImpl
30282943
setModifier(Modifier.ENUM_CONSTANT, isEnumConstant);
30292944
}
30302945

3031-
/// Whether the field was explicitly marked as being external.
3032-
bool get isExternal {
3033-
return hasModifier(Modifier.EXTERNAL);
3034-
}
3035-
30362946
/// Whether the field can be type promoted.
30372947
bool get isPromotable {
30382948
return hasModifier(Modifier.PROMOTABLE);
@@ -3743,6 +3653,18 @@ abstract class FunctionTypedFragmentImpl implements FragmentImpl {
37433653
List<TypeParameterFragmentImpl> get typeParameters;
37443654
}
37453655

3656+
class GenerateFragmentImpl {
3657+
/// Modifiers to generate in the annotated class.
3658+
///
3659+
/// Should be a companion enum to reuse Dart syntax, and allow attaching
3660+
/// optional documentation comments. Theoretically it could be a type
3661+
/// literal, but then each enum constant is marked as unused, so we
3662+
/// use `_MyModifiersEnum.values` instead.
3663+
final List<Enum> modifiers;
3664+
3665+
const GenerateFragmentImpl({required this.modifiers});
3666+
}
3667+
37463668
/// The element used for a generic function type.
37473669
///
37483670
/// Clients may not extend, implement or mix-in this class.
@@ -7947,7 +7869,7 @@ enum Modifier {
79477869
/// Indicates that the associated element did not have an explicit type
79487870
/// associated with it. If the element is an [ExecutableElement], then the
79497871
/// type being referred to is the return type.
7950-
IMPLICIT_TYPE,
7872+
HAS_IMPLICIT_TYPE,
79517873

79527874
/// Indicates that the modifier 'interface' was applied to the element.
79537875
INTERFACE,
@@ -9545,10 +9467,6 @@ class TopLevelVariableFragmentImpl extends PropertyInducingFragmentImpl
95459467
@override
95469468
TopLevelVariableFragmentImpl get declaration => this;
95479469

9548-
bool get isExternal {
9549-
return hasModifier(Modifier.EXTERNAL);
9550-
}
9551-
95529470
@override
95539471
bool get isStatic => true;
95549472

@@ -10361,7 +10279,9 @@ abstract class VariableElementImpl extends ElementImpl
1036110279
}
1036210280
}
1036310281

10282+
@GenerateFragmentImpl(modifiers: _VariableFragmentImplModifiers.values)
1036410283
abstract class VariableFragmentImpl extends FragmentImpl
10284+
with _VariableFragmentImplMixin
1036510285
implements VariableFragment {
1036610286
/// If this element represents a constant variable, and it has an initializer,
1036710287
/// a copy of the initializer for the constant. Otherwise `null`.
@@ -10385,83 +10305,11 @@ abstract class VariableFragmentImpl extends FragmentImpl
1038510305
@override
1038610306
VariableElementImpl get element;
1038710307

10388-
/// Whether the variable element did not have an explicit type specified
10389-
/// for it.
10390-
bool get hasImplicitType {
10391-
return hasModifier(Modifier.IMPLICIT_TYPE);
10392-
}
10393-
10394-
/// Set whether this variable element has an implicit type.
10395-
set hasImplicitType(bool hasImplicitType) {
10396-
setModifier(Modifier.IMPLICIT_TYPE, hasImplicitType);
10397-
}
10398-
1039910308
// TODO(scheglov): remove this
1040010309
ExpressionImpl? get initializer {
1040110310
return constantInitializer;
1040210311
}
1040310312

10404-
/// Set whether this variable is abstract.
10405-
set isAbstract(bool isAbstract) {
10406-
setModifier(Modifier.ABSTRACT, isAbstract);
10407-
}
10408-
10409-
/// Whether the variable was declared with the 'const' modifier.
10410-
bool get isConst {
10411-
return hasModifier(Modifier.CONST);
10412-
}
10413-
10414-
/// Set whether this variable is const.
10415-
set isConst(bool isConst) {
10416-
setModifier(Modifier.CONST, isConst);
10417-
}
10418-
10419-
/// Set whether this variable is external.
10420-
set isExternal(bool isExternal) {
10421-
setModifier(Modifier.EXTERNAL, isExternal);
10422-
}
10423-
10424-
/// Whether the variable was declared with the 'final' modifier.
10425-
///
10426-
/// Variables that are declared with the 'const' modifier will return `false`
10427-
/// even though they are implicitly final.
10428-
bool get isFinal {
10429-
return hasModifier(Modifier.FINAL);
10430-
}
10431-
10432-
/// Set whether this variable is final.
10433-
set isFinal(bool isFinal) {
10434-
setModifier(Modifier.FINAL, isFinal);
10435-
}
10436-
10437-
/// Whether the variable uses late evaluation semantics.
10438-
///
10439-
/// This will always return `false` unless the experiment 'non-nullable' is
10440-
/// enabled.
10441-
bool get isLate {
10442-
return hasModifier(Modifier.LATE);
10443-
}
10444-
10445-
/// Set whether this variable is late.
10446-
set isLate(bool isLate) {
10447-
setModifier(Modifier.LATE, isLate);
10448-
}
10449-
10450-
/// Whether the element is a static variable, as per section 8 of the Dart
10451-
/// Language Specification:
10452-
///
10453-
/// > A static variable is a variable that is not associated with a particular
10454-
/// > instance, but rather with an entire library or class. Static variables
10455-
/// > include library variables and class variables. Class variables are
10456-
/// > variables whose declaration is immediately nested inside a class
10457-
/// > declaration and includes the modifier static. A library variable is
10458-
/// > implicitly static.
10459-
bool get isStatic => hasModifier(Modifier.STATIC);
10460-
10461-
set isStatic(bool isStatic) {
10462-
setModifier(Modifier.STATIC, isStatic);
10463-
}
10464-
1046510313
@override
1046610314
int get offset {
1046710315
if (nameOffset ?? firstTokenOffset case var result?) {
@@ -10479,6 +10327,19 @@ abstract class VariableFragmentImpl extends FragmentImpl
1047910327
}
1048010328
}
1048110329

10330+
enum _ClassFragmentImplModifiers {
10331+
hasExtendsClause,
10332+
isAbstract,
10333+
isBase,
10334+
isFinal,
10335+
isInterface,
10336+
isMixinApplication,
10337+
isMixinClass,
10338+
isSealed,
10339+
}
10340+
10341+
enum _ConstructorFragmentImplModifiers { isConst, isFactory }
10342+
1048210343
/// Instances of [List]s that are used as "not yet computed" values, they
1048310344
/// must be not `null`, and not identical to `const <T>[]`.
1048410345
class _Sentinel {
@@ -10508,3 +10369,30 @@ class _Sentinel {
1050810369
static final List<LibraryExportImpl> libraryExport = List.unmodifiable([]);
1050910370
static final List<LibraryImportImpl> libraryImport = List.unmodifiable([]);
1051010371
}
10372+
10373+
enum _VariableFragmentImplModifiers {
10374+
/// Whether the variable element did not have an explicit type specified
10375+
/// for it.
10376+
hasImplicitType,
10377+
isAbstract,
10378+
isConst,
10379+
isExternal,
10380+
10381+
/// Whether the variable was declared with the 'final' modifier.
10382+
///
10383+
/// Variables that are declared with the 'const' modifier will return `false`
10384+
/// even though they are implicitly final.
10385+
isFinal,
10386+
isLate,
10387+
10388+
/// Whether the element is a static variable, as per section 8 of the Dart
10389+
/// Language Specification:
10390+
///
10391+
/// > A static variable is a variable that is not associated with a particular
10392+
/// > instance, but rather with an entire library or class. Static variables
10393+
/// > include library variables and class variables. Class variables are
10394+
/// > variables whose declaration is immediately nested inside a class
10395+
/// > declaration and includes the modifier static. A library variable is
10396+
/// > implicitly static.
10397+
isStatic,
10398+
}

0 commit comments

Comments
 (0)