Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 48544cd

Browse files
scheglovcommit-bot@chromium.org
authored andcommitted
Use InheritanceManager2 in InstanceMemberInferrer.
R=brianwilkerson@google.com Change-Id: Ia075a1286446891e227327dad908557d7591766d Reviewed-on: https://dart-review.googlesource.com/c/79425 Commit-Queue: Konstantin Shcheglov <scheglov@google.com> Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
1 parent 29ff9e4 commit 48544cd

5 files changed

Lines changed: 123 additions & 115 deletions

File tree

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

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,10 @@ class InheritanceManager2 {
4343

4444
InheritanceManager2(this._typeSystem);
4545

46-
/// Return the member with the given [name] that the class inherits from the
46+
/// Return the member with the given [name] that the [type] inherits from the
4747
/// mixins, superclasses, or interfaces; or `null` if no member is inherited.
4848
FunctionType getInherited(InterfaceType type, Name name) {
49-
var interface = getInterface(type);
50-
return interface._inherited[name];
49+
return getOverridden(type, name)?.last;
5150
}
5251

5352
/// Return the interface of the given [type]. It might include private
@@ -121,16 +120,9 @@ class InheritanceManager2 {
121120
// signature becomes the signature of the class's interface.
122121
conflicts = _findMostSpecificFromNamedCandidates(map, namedCandidates);
123122

124-
// Get one candidate for each name.
125-
Map<Name, FunctionType> inherited = {};
126-
for (var name in namedCandidates.keys) {
127-
var candidates = namedCandidates[name];
128-
inherited[name] = candidates.last;
129-
}
130-
131123
var interface = new Interface._(
132124
map,
133-
inherited,
125+
namedCandidates,
134126
superImplemented,
135127
conflicts ?? const [],
136128
);
@@ -169,6 +161,14 @@ class InheritanceManager2 {
169161
return getInterface(type).map[name];
170162
}
171163

164+
/// Return all members of mixins, superclasses, and interfaces that a member
165+
/// with the given [name], defined in the [type], would override; or `null`
166+
/// if no members would be overridden.
167+
List<FunctionType> getOverridden(InterfaceType type, Name name) {
168+
var interface = getInterface(type);
169+
return interface._overridden[name];
170+
}
171+
172172
void _addCandidate(Map<Name, List<FunctionType>> namedCandidates, Name name,
173173
FunctionType candidate) {
174174
var candidates = namedCandidates[name];
@@ -182,9 +182,11 @@ class InheritanceManager2 {
182182

183183
void _addCandidates(
184184
Map<Name, List<FunctionType>> namedCandidates, InterfaceType type) {
185-
getInterface(type).map.forEach((name, candidate) {
185+
var map = getInterface(type).map;
186+
for (var name in map.keys) {
187+
var candidate = map[name];
186188
_addCandidate(namedCandidates, name, candidate);
187-
});
189+
}
188190
}
189191

190192
void _addTypeMembers(Map<Name, FunctionType> map, InterfaceType type) {
@@ -386,9 +388,9 @@ class Interface {
386388
/// The map of names to their signature in the interface.
387389
final Map<Name, FunctionType> map;
388390

389-
/// The map of names to their signature from the mixins, superclasses,
391+
/// The map of names to their signatures from the mixins, superclasses,
390392
/// or interfaces.
391-
final Map<Name, FunctionType> _inherited;
393+
final Map<Name, List<FunctionType>> _overridden;
392394

393395
/// Each item of this list maps names to their concrete implementations.
394396
/// The first item of the list is the nominal superclass, next the nominal
@@ -403,7 +405,7 @@ class Interface {
403405

404406
const Interface._(
405407
this.map,
406-
this._inherited,
408+
this._overridden,
407409
this._superImplemented,
408410
this.conflicts,
409411
);

pkg/analyzer/lib/src/generated/resolver.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3975,7 +3975,7 @@ class OverrideVerifier extends RecursiveAstVisitor {
39753975
/// Return `true` if the [member] overrides a member from a superinterface.
39763976
bool _isOverride(ExecutableElement member) {
39773977
var name = new Name(_libraryUri, member.name);
3978-
return _inheritance.getInherited(_currentType, name) != null;
3978+
return _inheritance.getOverridden(_currentType, name) != null;
39793979
}
39803980
}
39813981

pkg/analyzer/lib/src/summary/link.dart

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -801,8 +801,23 @@ class ClassElementForLink_Class extends ClassElementForLink
801801
String get name => _unlinkedClass.name;
802802

803803
@override
804-
List<InterfaceType> get superclassConstraints => _superclassConstraints ??=
805-
_unlinkedClass.superclassConstraints.map(_computeInterfaceType).toList();
804+
List<InterfaceType> get superclassConstraints {
805+
if (_superclassConstraints == null) {
806+
if (isMixin) {
807+
_superclassConstraints = _unlinkedClass.superclassConstraints
808+
.map(_computeInterfaceType)
809+
.toList();
810+
if (_superclassConstraints.isEmpty) {
811+
_superclassConstraints = [
812+
enclosingElement.enclosingElement._linker.typeProvider.objectType
813+
];
814+
}
815+
} else {
816+
_superclassConstraints = const <InterfaceType>[];
817+
}
818+
}
819+
return _superclassConstraints;
820+
}
806821

807822
@override
808823
InterfaceType get supertype {
@@ -1602,11 +1617,13 @@ class CompilationUnitElementInBuildUnit extends CompilationUnitElementForLink {
16021617
/// Perform type inference and const cycle detection on this
16031618
/// compilation unit.
16041619
void link() {
1620+
var typeSystem = library._linker.typeSystem;
1621+
// TODO(scheglov) Reuse InheritanceManager2 for the whole linking.
1622+
var inheritance = new InheritanceManager2(typeSystem);
16051623
new InstanceMemberInferrer(
1606-
enclosingElement._linker.typeProvider,
1607-
(clazz) =>
1608-
(clazz.library as LibraryElementInBuildUnit).inheritanceManager)
1609-
.inferCompilationUnit(this);
1624+
typeSystem.typeProvider,
1625+
inheritance,
1626+
).inferCompilationUnit(this);
16101627
for (TopLevelVariableElementForLink variable in topLevelVariables) {
16111628
variable.link(this);
16121629
}

pkg/analyzer/lib/src/task/dart.dart

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3153,11 +3153,9 @@ class InferInstanceMembersInUnitTask extends SourceBasedAnalysisTask {
31533153
//
31543154
// Infer instance members.
31553155
//
3156-
var inheritanceManager = new InheritanceManager(
3157-
resolutionMap.elementDeclaredByCompilationUnit(unit).library);
3158-
InstanceMemberInferrer inferrer = new InstanceMemberInferrer(
3159-
typeProvider, (_) => inheritanceManager,
3160-
typeSystem: context.typeSystem);
3156+
var inheritance = new InheritanceManager2(context.typeSystem);
3157+
InstanceMemberInferrer inferrer =
3158+
new InstanceMemberInferrer(typeProvider, inheritance);
31613159
inferrer.inferCompilationUnit(unit.declaredElement);
31623160
//
31633161
// Record outputs.

0 commit comments

Comments
 (0)