Skip to content

Commit

Permalink
Elements. Build more Element2 objects.
Browse files Browse the repository at this point in the history
Change-Id: I1856610e12c19c2e0fb502fbc324b562cbbe0006
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/394400
Reviewed-by: Brian Wilkerson <[email protected]>
Commit-Queue: Konstantin Shcheglov <[email protected]>
  • Loading branch information
scheglov authored and Commit Queue committed Nov 11, 2024
1 parent 0095085 commit f33de62
Show file tree
Hide file tree
Showing 55 changed files with 2,701 additions and 1,506 deletions.
2 changes: 1 addition & 1 deletion pkg/analyzer/lib/src/dart/analysis/driver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ import 'package:meta/meta.dart';
// TODO(scheglov): Clean up the list of implicitly analyzed files.
class AnalysisDriver {
/// The version of data format, should be incremented on every format change.
static const int DATA_VERSION = 417;
static const int DATA_VERSION = 418;

/// The number of exception contexts allowed to write. Once this field is
/// zero, we stop writing any new exception contexts in this process.
Expand Down
143 changes: 35 additions & 108 deletions pkg/analyzer/lib/src/dart/element/element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5338,6 +5338,9 @@ abstract class InstanceElementImpl2 extends ElementImpl2
@override
List<MethodElement> methods = [];

@override
List<MethodElementImpl2> methods2 = [];

@override
InstanceElement2 get baseElement => this;

Expand Down Expand Up @@ -5401,10 +5404,6 @@ abstract class InstanceElementImpl2 extends ElementImpl2
@override
Metadata get metadata2 => firstFragment.metadata2;

@override
List<MethodElement2> get methods2 =>
methods.map((e) => e.asElement2 as MethodElement2?).nonNulls.toList();

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

Expand Down Expand Up @@ -6437,6 +6436,12 @@ class LibraryElementImpl extends ElementImpl
@override
List<MixinElementImpl2> mixins = [];

@override
List<TopLevelVariableElementImpl2> topLevelVariables = [];

@override
List<TypeAliasElementImpl2> typeAliases = [];

/// The export [Namespace] of this library, `null` if it has not been
/// computed yet.
Namespace? _exportNamespace;
Expand Down Expand Up @@ -6715,26 +6720,6 @@ class LibraryElementImpl extends ElementImpl
}
}

@override
List<TopLevelVariableElement2> get topLevelVariables {
var declarations = <TopLevelVariableElement2>{};
for (var unit in units) {
declarations.addAll(unit._variables
.map((fragment) => (fragment as TopLevelVariableFragment).element));
}
return declarations.toList();
}

@override
List<TypeAliasElement2> get typeAliases {
var declarations = <TypeAliasElement2>{};
for (var unit in units) {
declarations.addAll(unit._typeAliases
.map((fragment) => (fragment as TypeAliasFragment).element));
}
return declarations.toList();
}

@override
List<CompilationUnitElementImpl> get units {
return [
Expand Down Expand Up @@ -7610,6 +7595,9 @@ final class MetadataImpl implements Metadata {
class MethodElementImpl extends ExecutableElementImpl
with AugmentableElement<MethodElementImpl>
implements MethodElement, MethodFragment {
@override
late MethodElementImpl2 element;

@override
String? name2;

Expand All @@ -7625,9 +7613,6 @@ class MethodElementImpl extends ExecutableElementImpl
/// this variable is not a subject of type inference, or there was no error.
TopLevelInferenceError? typeInferenceError;

/// The element corresponding to this fragment.
MethodElement2? _element;

/// Initialize a newly created method element to have the given [name] at the
/// given [offset].
MethodElementImpl(super.name, super.offset);
Expand All @@ -7649,27 +7634,6 @@ class MethodElementImpl extends ExecutableElementImpl
return displayName;
}

@override
MethodElement2 get element {
if (_element != null) {
return _element!;
}

MethodFragment firstFragment = this;
var previousFragment = firstFragment.previousFragment;
while (previousFragment != null) {
firstFragment = previousFragment;
previousFragment = firstFragment.previousFragment;
}
firstFragment as MethodElementImpl;

// As a side-effect of creating the element, all of the fragments in the
// chain will have their `_element` set to the newly created element.
return MethodElementImpl2(firstFragment.name2, firstFragment);
}

set element(MethodElement2 element) => _element = element;

@override
InstanceFragment? get enclosingFragment =>
enclosingElement3 as InstanceFragment;
Expand Down Expand Up @@ -7730,18 +7694,18 @@ class MethodElementImpl2 extends ExecutableElementImpl2
FragmentedAnnotatableElementMixin<MethodFragment>,
FragmentedElementMixin<MethodFragment>
implements MethodElement2 {
@override
final Reference reference;

@override
final String? name3;

@override
final MethodElementImpl firstFragment;

MethodElementImpl2(this.name3, this.firstFragment) {
MethodElementImpl? fragment = firstFragment;
while (fragment != null) {
fragment.element = this;
fragment = fragment.nextFragment as MethodElementImpl?;
}
MethodElementImpl2(this.reference, this.name3, this.firstFragment) {
reference.element2 = this;
firstFragment.element = this;
}

@override
Expand Down Expand Up @@ -9893,8 +9857,8 @@ class TopLevelFunctionElementImpl extends ExecutableElementImpl2
class TopLevelVariableElementImpl extends PropertyInducingElementImpl
with AugmentableElement<TopLevelVariableElementImpl>
implements TopLevelVariableElement, TopLevelVariableFragment {
/// The element corresponding to this fragment.
TopLevelVariableElement2? _element;
@override
late TopLevelVariableElementImpl2 element;

/// Initialize a newly created synthetic top-level variable element to have
/// the given [name] and [offset].
Expand All @@ -9908,25 +9872,6 @@ class TopLevelVariableElementImpl extends PropertyInducingElementImpl
@override
TopLevelVariableElement get declaration => this;

@override
TopLevelVariableElement2 get element {
if (_element != null) {
return _element!;
}
TopLevelVariableFragment firstFragment = this;
var previousFragment = firstFragment.previousFragment;
while (previousFragment != null) {
firstFragment = previousFragment;
previousFragment = firstFragment.previousFragment;
}
// As a side-effect of creating the element, all of the fragments in the
// chain will have their `_element` set to the newly created element.
return TopLevelVariableElementImpl2(
firstFragment as TopLevelVariableElementImpl);
}

set element(TopLevelVariableElement2 element) => _element = element;

@override
bool get isExternal {
return hasModifier(Modifier.EXTERNAL);
Expand Down Expand Up @@ -9962,15 +9907,15 @@ class TopLevelVariableElementImpl2 extends PropertyInducingElementImpl2
FragmentedAnnotatableElementMixin<TopLevelVariableFragment>,
FragmentedElementMixin<TopLevelVariableFragment>
implements TopLevelVariableElement2 {
@override
final Reference reference;

@override
final TopLevelVariableElementImpl firstFragment;

TopLevelVariableElementImpl2(this.firstFragment) {
TopLevelVariableElementImpl? fragment = firstFragment;
while (fragment != null) {
fragment.element = this;
fragment = fragment.nextFragment as TopLevelVariableElementImpl?;
}
TopLevelVariableElementImpl2(this.reference, this.firstFragment) {
reference.element2 = this;
firstFragment.element = this;
}

@override
Expand Down Expand Up @@ -10006,7 +9951,7 @@ class TopLevelVariableElementImpl2 extends PropertyInducingElementImpl2
ElementKind get kind => ElementKind.TOP_LEVEL_VARIABLE;

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

@override
SetterElement? get setter2 =>
Expand Down Expand Up @@ -10051,8 +9996,8 @@ class TypeAliasElementImpl extends _ExistingElementImpl
ElementImpl? _aliasedElement;
DartType? _aliasedType;

/// The element corresponding to this fragment.
TypeAliasElement2? _element;
@override
late TypeAliasElementImpl2 element;

TypeAliasElementImpl(String super.name, super.nameOffset);

Expand Down Expand Up @@ -10088,24 +10033,6 @@ class TypeAliasElementImpl extends _ExistingElementImpl
@override
String get displayName => name;

@override
TypeAliasElement2 get element {
if (_element != null) {
return _element!;
}
TypeAliasFragment firstFragment = this;
var previousFragment = firstFragment.previousFragment;
while (previousFragment != null) {
firstFragment = previousFragment;
previousFragment = firstFragment.previousFragment;
}
// As a side-effect of creating the element, all of the fragments in the
// chain will have their `_element` set to the newly created element.
return TypeAliasElementImpl2(firstFragment as TypeAliasElementImpl);
}

set element(TypeAliasElement2 element) => _element = element;

@override
CompilationUnitElement get enclosingElement3 =>
super.enclosingElement3 as CompilationUnitElement;
Expand Down Expand Up @@ -10296,15 +10223,15 @@ class TypeAliasElementImpl2 extends TypeDefiningElementImpl2
FragmentedAnnotatableElementMixin<TypeAliasFragment>,
FragmentedElementMixin<TypeAliasFragment>
implements TypeAliasElement2 {
@override
final Reference reference;

@override
final TypeAliasElementImpl firstFragment;

TypeAliasElementImpl2(this.firstFragment) {
TypeAliasElementImpl? fragment = firstFragment;
while (fragment != null) {
fragment.element = this;
fragment = fragment.nextFragment as TypeAliasElementImpl?;
}
TypeAliasElementImpl2(this.reference, this.firstFragment) {
reference.element2 = this;
firstFragment.element = this;
}

@override
Expand Down
7 changes: 6 additions & 1 deletion pkg/analyzer/lib/src/dart/element/inheritance_manager3.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'package:analyzer/src/dart/element/extensions.dart';
import 'package:analyzer/src/dart/element/member.dart';
import 'package:analyzer/src/dart/element/type_algebra.dart';
import 'package:analyzer/src/dart/element/type_system.dart';
import 'package:analyzer/src/summary2/reference.dart';
import 'package:analyzer/src/utilities/extensions/collection.dart';
import 'package:analyzer/src/utilities/extensions/element.dart';
import 'package:meta/meta.dart';
Expand Down Expand Up @@ -1136,7 +1137,11 @@ class InheritanceManager3 {
result.typeParameters = resultType.typeFormals;
result.returnType = resultType.returnType;
result.parameters = resultType.parameters;
result.element = MethodElementImpl2(firstMethod.name, result);
result.element = MethodElementImpl2(
Reference.root(), // TODO(scheglov): wrong
firstMethod.name,
result,
);
return result;
} else {
var firstAccessor = first as PropertyAccessorElement;
Expand Down
21 changes: 20 additions & 1 deletion pkg/analyzer/lib/src/summary2/augmentation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ abstract class InstanceElementBuilder<E extends InstanceElementImpl2,
final Map<String, PropertyAccessorElementImpl> setters = {};
final Map<String, MethodElementImpl> methods = {};

final Map<String, ElementImpl> fragmentGetters = {};
final Map<String, ElementImpl> fragmentSetters = {};
final List<MethodElementImpl2> methods2 = [];

InstanceElementBuilder({
required super.element,
required super.firstFragment,
Expand Down Expand Up @@ -244,6 +248,21 @@ abstract class InstanceElementBuilder<E extends InstanceElementImpl2,
}
}

ElementImpl? replaceGetter<T extends ElementImpl>(T fragment) {
var name = (fragment as Fragment).name2;
if (name == null) {
return null;
}

var lastFragment = fragmentGetters[name];
lastFragment ??= fragmentSetters[name];

fragmentGetters[name] = fragment;
fragmentSetters.remove(name);

return lastFragment;
}

void _addFirstFragment() {
var firstFragment = this.firstFragment;
var augmented = firstFragment.augmented;
Expand Down Expand Up @@ -436,7 +455,7 @@ class TypeAliasElementBuilder extends FragmentedElementBuilder<
if (!identical(fragment, firstFragment)) {
lastFragment.augmentation = fragment;
lastFragment = fragment;
// fragment.element = element;
fragment.element = element;
}
}
}
Expand Down
Loading

0 comments on commit f33de62

Please sign in to comment.