Skip to content

Commit

Permalink
Elements. Migrate ThisLookup and LexicalLookup.
Browse files Browse the repository at this point in the history
Change-Id: I29d36ed43907871a0ffed8bd71619ef55a336530
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/404240
Commit-Queue: Konstantin Shcheglov <[email protected]>
Reviewed-by: Phil Quitslund <[email protected]>
  • Loading branch information
scheglov authored and Commit Queue committed Jan 13, 2025
1 parent 0f82db5 commit 22fe194
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 46 deletions.
35 changes: 32 additions & 3 deletions pkg/analyzer/lib/src/dart/element/element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1661,6 +1661,11 @@ class ConstructorElementImpl2 extends ExecutableElementImpl2
@override
ElementKind get kind => ElementKind.CONSTRUCTOR;

@override
ConstructorElementImpl get lastFragment {
return super.lastFragment as ConstructorElementImpl;
}

@override
Element2 get nonSynthetic2 {
if (isSynthetic) {
Expand Down Expand Up @@ -3855,6 +3860,17 @@ abstract class ExecutableElementImpl2 extends FunctionTypedElementImpl2
return firstFragment.hasModifier(Modifier.INVOKES_SUPER_SELF);
}

ExecutableElementImpl get lastFragment {
var result = firstFragment as ExecutableElementImpl;
while (true) {
if (result.nextFragment case ExecutableElementImpl nextFragment) {
result = nextFragment;
} else {
return result;
}
}
}

@override
LibraryElement2 get library2 {
var firstFragment = this.firstFragment as ExecutableElementImpl;
Expand Down Expand Up @@ -5601,9 +5617,7 @@ abstract class InstanceElementImpl2 extends ElementImpl2

@override
List<TypeParameterElementImpl2> get typeParameters2 =>
firstFragment.typeParameters
.map((fragment) => fragment.element)
.toList();
firstFragment.typeParameters.map((fragment) => fragment.element).toList();

@override
String displayString2(
Expand Down Expand Up @@ -8124,6 +8138,11 @@ class MethodElementImpl2 extends ExecutableElementImpl2
@override
ElementKind get kind => ElementKind.METHOD;

@override
MethodElementImpl get lastFragment {
return super.lastFragment as MethodElementImpl;
}

@override
String? get lookupName {
if (name3 == '-' && formalParameters.isEmpty) {
Expand Down Expand Up @@ -9762,6 +9781,11 @@ abstract class PropertyAccessorElementImpl2 extends ExecutableElementImpl2
@override
bool get isExternal => firstFragment.isExternal;

@override
PropertyAccessorElementImpl get lastFragment {
return super.lastFragment as PropertyAccessorElementImpl;
}

@override
String? get name3 => firstFragment.name2;

Expand Down Expand Up @@ -10320,6 +10344,11 @@ class TopLevelFunctionElementImpl extends ExecutableElementImpl2
@override
ElementKind get kind => ElementKind.FUNCTION;

@override
FunctionElementImpl get lastFragment {
return super.lastFragment as FunctionElementImpl;
}

@override
LibraryElement2 get library2 {
return firstFragment.library2!;
Expand Down
18 changes: 8 additions & 10 deletions pkg/analyzer/lib/src/dart/resolver/lexical_lookup.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// ignore_for_file: analyzer_use_new_elements

import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/dart/element/scope.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer/src/dart/element/extensions.dart';
Expand All @@ -21,8 +19,8 @@ class LexicalLookup {
/// a getter. If a matching element is found, a [LexicalLookupResult] is
/// returned. Otherwise `null` is returned.
static LexicalLookupResult? resolveGetter(ScopeLookupResult scopeResult) {
var scopeGetter = scopeResult.getter;
var scopeSetter = scopeResult.setter;
var scopeGetter = scopeResult.getter2;
var scopeSetter = scopeResult.setter2;
if (scopeGetter != null || scopeSetter != null) {
if (scopeGetter != null) {
return LexicalLookupResult(requested: scopeGetter);
Expand All @@ -39,10 +37,10 @@ class LexicalLookup {
/// a setter. If a matching element is found, a [LexicalLookupResult] is
/// returned. Otherwise `null` is returned.
static LexicalLookupResult? resolveSetter(ScopeLookupResult scopeResult) {
var scopeGetter = scopeResult.getter;
var scopeSetter = scopeResult.setter;
var scopeGetter = scopeResult.getter2;
var scopeSetter = scopeResult.setter2;
if (scopeGetter != null || scopeSetter != null) {
if (scopeGetter is VariableElement) {
if (scopeGetter is VariableElement2) {
return LexicalLookupResult(requested: scopeGetter);
}
if (scopeSetter != null) {
Expand All @@ -58,8 +56,8 @@ class LexicalLookup {
}

class LexicalLookupResult {
final Element? requested;
final Element? recovery;
final Element2? requested;
final Element2? recovery;

/// The type, usually [FunctionType] referenced with `call`.
final DartType? callFunctionType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ class PropertyElementResolver with ScopeHelpers {
);
}

readElementRequested = readLookup?.requested;
readElementRequested = readLookup?.requested.asElement;
if (readElementRequested is PropertyAccessorElement &&
!readElementRequested.isStatic) {
var unpromotedType = readElementRequested.returnType;
Expand All @@ -285,8 +285,8 @@ class PropertyElementResolver with ScopeHelpers {
if (hasWrite) {
var writeLookup = LexicalLookup.resolveSetter(scopeLookupResult) ??
_resolver.thisLookupSetter(node);
writeElementRequested = writeLookup?.requested;
writeElementRecovery = writeLookup?.recovery;
writeElementRequested = writeLookup?.requested.asElement;
writeElementRecovery = writeLookup?.recovery.asElement;

AssignmentVerifier(errorReporter).verify(
node: node,
Expand Down
10 changes: 4 additions & 6 deletions pkg/analyzer/lib/src/dart/resolver/this_lookup.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// ignore_for_file: analyzer_use_new_elements

import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/src/dart/resolver/lexical_lookup.dart';
import 'package:analyzer/src/generated/resolver.dart';
Expand Down Expand Up @@ -51,11 +49,11 @@ class ThisLookup {
);
}

var getterElement = propertyResult.getter;
var getterElement = propertyResult.getter2;
if (getterElement != null) {
return LexicalLookupResult(requested: getterElement);
} else {
return LexicalLookupResult(recovery: propertyResult.setter);
return LexicalLookupResult(recovery: propertyResult.setter2);
}
}

Expand All @@ -80,11 +78,11 @@ class ThisLookup {
nameErrorEntity: node,
);

var setterElement = propertyResult.setter;
var setterElement = propertyResult.setter2;
if (setterElement != null) {
return LexicalLookupResult(requested: setterElement);
} else {
return LexicalLookupResult(recovery: propertyResult.getter);
return LexicalLookupResult(recovery: propertyResult.getter2);
}
}
}
48 changes: 24 additions & 24 deletions pkg/analyzer/lib/src/utilities/extensions/element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ extension ConstructorElement2Extension on ConstructorElement2 {
if (this case ConstructorMember member) {
return member;
}
return baseElement.firstFragment as ConstructorElement;
return (this as ConstructorElementImpl2).lastFragment;
}
}

Expand Down Expand Up @@ -142,46 +142,46 @@ extension Element2OrNullExtension on Element2? {
switch (self) {
case null:
return null;
case ConstructorElementImpl2():
return self.firstFragment as Element;
case ConstructorElementImpl2 element2:
return element2.asElement;
case DynamicElementImpl2():
return self.firstFragment;
case ExecutableMember():
return self.declaration as Element;
case ExtensionElementImpl2():
return self.firstFragment as Element;
case FieldElementImpl2():
return self.firstFragment as Element;
case ExecutableMember element2:
return element2.asElement;
case ExtensionElementImpl2 element2:
return element2.asElement;
case FieldElementImpl2 element2:
return element2.asElement;
case FieldMember():
return self.declaration as Element;
case FormalParameterElement element2:
return element2.asElement;
case GetterElementImpl():
return self.firstFragment as Element;
case GetterElementImpl element2:
return element2.asElement;
case LabelElementImpl2 element2:
return element2.asElement;
case LibraryElementImpl():
return self as Element;
case LibraryImportElementImpl():
return self as Element;
case LocalFunctionElementImpl():
return self.wrappedElement as Element;
case LocalFunctionElementImpl element2:
return element2.asElement;
case LocalVariableElementImpl2():
return self.wrappedElement as Element;
case MethodElementImpl2():
return self.firstFragment as Element;
case MethodElementImpl2 element2:
return element2.asElement;
case MultiplyDefinedElementImpl2 element2:
return element2.asElement;
case NeverElementImpl2():
return NeverElementImpl.instance;
case PrefixElement2 element2:
return element2.asElement;
case SetterElementImpl():
return self.firstFragment as Element;
case TopLevelFunctionElementImpl():
return self.firstFragment as Element;
case TopLevelVariableElementImpl2():
return self.firstFragment as Element;
case SetterElementImpl element2:
return element2.asElement;
case TopLevelFunctionElementImpl element2:
return element2.asElement;
case TopLevelVariableElementImpl2 element2:
return element2.asElement;
case TypeDefiningElement2():
return self.firstFragment as Element;
default:
Expand Down Expand Up @@ -454,7 +454,7 @@ extension MethodElement2Extension on MethodElement2 {
if (this case MethodMember member) {
return member;
}
return baseElement.firstFragment as MethodElement;
return (this as MethodElementImpl2).lastFragment;
}
}

Expand Down Expand Up @@ -511,7 +511,7 @@ extension PropertyAccessorElement2Extension on PropertyAccessorElement2 {
if (this case PropertyAccessorMember member) {
return member;
}
return firstFragment as PropertyAccessorElement;
return (this as PropertyAccessorElementImpl2).lastFragment;
}
}

Expand All @@ -527,7 +527,7 @@ extension PropertyAccessorElementExtension on PropertyAccessorElement {

extension TopLevelFunctionElementExtension on TopLevelFunctionElement {
FunctionElement get asElement {
return firstFragment as FunctionElement;
return (this as TopLevelFunctionElementImpl).lastFragment;
}
}

Expand Down

0 comments on commit 22fe194

Please sign in to comment.