Skip to content

Commit

Permalink
Add API to AST nodes and session result objects to access the new ele…
Browse files Browse the repository at this point in the history
…ment APIs

There will undoubtedly be other places where we need hooks, but I thought
that these would provide a good starting point for attempts to start
converting existing code to the new API.

Change-Id: If04aea4244efaca5a81553fbd88066b24c169290
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/385303
Reviewed-by: Konstantin Shcheglov <[email protected]>
Commit-Queue: Brian Wilkerson <[email protected]>
  • Loading branch information
bwilkerson authored and Commit Queue committed Sep 13, 2024
1 parent 351e103 commit a6a2c57
Show file tree
Hide file tree
Showing 3 changed files with 785 additions and 21 deletions.
30 changes: 30 additions & 0 deletions pkg/analyzer/lib/dart/analysis/results.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import 'package:analyzer/dart/analysis/analysis_options.dart';
import 'package:analyzer/dart/analysis/session.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/dart/element/type_provider.dart';
import 'package:analyzer/dart/element/type_system.dart';
import 'package:analyzer/error/error.dart';
import 'package:analyzer/file_system/file_system.dart';
import 'package:analyzer/source/line_info.dart';
import 'package:meta/meta.dart';

/// The result of performing some kind of analysis on a single file. Every
/// result that implements this interface will also implement a sub-interface.
Expand Down Expand Up @@ -59,6 +61,10 @@ abstract class ElementDeclarationResult {
/// The [Element] that this object describes.
Element get element;

/// The element that this object describes.
@experimental
Element2 get element2;

/// The node that declares the [element]. Depending on whether it is returned
/// from [ResolvedLibraryResult] or [ParsedLibraryResult] it might be resolved
/// or just parsed.
Expand Down Expand Up @@ -152,6 +158,10 @@ abstract class InvalidResult {}
abstract class LibraryElementResult implements SomeLibraryElementResult {
/// The element of the library.
LibraryElement get element;

/// The element representing the library.
@experimental
LibraryElement2 get element2;
}

/// The type of [InvalidResult] returned when the given element was not
Expand Down Expand Up @@ -204,6 +214,14 @@ abstract class ParsedLibraryResult
/// is synthetic. Throw [ArgumentError] if the [element] is not defined in
/// this library.
ElementDeclarationResult? getElementDeclaration(Element element);

/// Returns the declaration of the [fragment].
///
/// Returns `null` if the [fragment] is synthetic.
///
/// Throws [ArgumentError] if the [fragment] is not defined in this library.
@experimental
ElementDeclarationResult? getElementDeclaration2(Fragment fragment);
}

/// The result of parsing of a single file. The errors returned include only
Expand Down Expand Up @@ -245,6 +263,10 @@ abstract class ResolvedLibraryResult
/// The element representing this library.
LibraryElement get element;

/// The element representing this library.
@experimental
LibraryElement2 get element2;

/// The type provider used when resolving the library.
TypeProvider get typeProvider;

Expand All @@ -269,6 +291,10 @@ abstract class ResolvedUnitResult
/// The element representing the library containing the compilation [unit].
LibraryElement get libraryElement;

/// The element representing the library containing the compilation [unit].
@experimental
LibraryElement2 get libraryElement2;

/// The type provider used when resolving the compilation [unit].
TypeProvider get typeProvider;

Expand Down Expand Up @@ -352,6 +378,10 @@ abstract class SomeUnitElementResult {}
abstract class UnitElementResult implements SomeUnitElementResult, FileResult {
/// The element of the file.
CompilationUnitElement get element;

/// The fragment representing the content of the file.
@experimental
LibraryFragment get fragment;
}

/// The type of [InvalidResult] returned when something is wrong, but we
Expand Down
39 changes: 39 additions & 0 deletions pkg/analyzer/lib/src/dart/analysis/results.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/token.dart';
import 'package:analyzer/dart/ast/visitor.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/dart/element/type_provider.dart';
import 'package:analyzer/error/error.dart';
import 'package:analyzer/file_system/file_system.dart';
Expand Down Expand Up @@ -162,6 +163,16 @@ class ElementDeclarationResultImpl implements ElementDeclarationResult {

ElementDeclarationResultImpl(
this.element, this.node, this.parsedUnit, this.resolvedUnit);

@override
Element2 get element2 {
if (element case Fragment fragment) {
return fragment.element;
} else if (element case Element2 element) {
return element;
}
throw UnimplementedError('Could not compute and element');
}
}

class ErrorsResultImpl implements ErrorsResult {
Expand Down Expand Up @@ -258,6 +269,9 @@ class LibraryElementResultImpl implements LibraryElementResult {
final LibraryElement element;

LibraryElementResultImpl(this.element);

@override
LibraryElement2 get element2 => element as LibraryElement2;
}

class ParsedLibraryResultImpl extends AnalysisResultImpl
Expand Down Expand Up @@ -299,6 +313,14 @@ class ParsedLibraryResultImpl extends AnalysisResultImpl

return ElementDeclarationResultImpl(element, declaration, unitResult, null);
}

@override
ElementDeclarationResult? getElementDeclaration2(Fragment fragment) {
if (fragment case Element element) {
return getElementDeclaration(element);
}
throw UnimplementedError();
}
}

class ParsedUnitResultImpl extends FileResultImpl implements ParsedUnitResult {
Expand Down Expand Up @@ -392,6 +414,9 @@ class ResolvedLibraryResultImpl extends AnalysisResultImpl
required this.units,
});

@override
LibraryElement2 get element2 => element as LibraryElement2;

@override
TypeProvider get typeProvider => element.typeProvider;

Expand Down Expand Up @@ -425,6 +450,14 @@ class ResolvedLibraryResultImpl extends AnalysisResultImpl
return ElementDeclarationResultImpl(element, declaration, null, unitResult);
}

@override
ElementDeclarationResult? getElementDeclaration2(Fragment fragment) {
if (fragment case Element element) {
return getElementDeclaration(element);
}
throw UnimplementedError();
}

@override
ResolvedUnitResult? unitWithPath(String path) {
for (var unit in units) {
Expand Down Expand Up @@ -459,6 +492,9 @@ class ResolvedUnitResultImpl extends FileResultImpl
return unit.declaredElement!.library;
}

@override
LibraryElement2 get libraryElement2 => libraryElement as LibraryElement2;

@override
TypeProvider get typeProvider => libraryElement.typeProvider;

Expand All @@ -476,4 +512,7 @@ class UnitElementResultImpl extends FileResultImpl
required super.fileState,
required this.element,
});

@override
LibraryFragment get fragment => element as LibraryFragment;
}
Loading

0 comments on commit a6a2c57

Please sign in to comment.