Skip to content

Commit de0e432

Browse files
stereotype441commit-bot@chromium.org
authored andcommitted
Create an abstract layer to customize querying element types.
This will be needed for the NNBD migration engine. Currently only used in checker.dart. Later CLs will convert the resolver to use this abstraction layer as well. The runtime overhead of this abstraction should be negligible, since under normal analyzer operation, there will only be a single const instance of this class, and no instances of any subclasses. So the VM should be able to trivially inline all uses of the abstraction layer. Change-Id: I0132b76238606e6e6b789169a62806d09f10f71d Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/127490 Commit-Queue: Paul Berry <paulberry@google.com> Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
1 parent 5f7177d commit de0e432

File tree

2 files changed

+53
-6
lines changed

2 files changed

+53
-6
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:analyzer/dart/element/element.dart';
6+
import 'package:analyzer/dart/element/type.dart';
7+
8+
/// Abstraction layer allowing the mechanism for looking up the types of
9+
/// elements to be customized.
10+
///
11+
/// This is needed for the NNBD migration engine, which needs to analyze
12+
/// NNBD-disabled code as though it has NNBD enabled.
13+
///
14+
/// This base class implementation gets types directly from the elements; for
15+
/// other behaviors, create a class that extends or implements this class.
16+
class ElementTypeProvider {
17+
const ElementTypeProvider();
18+
19+
/// Queries the parameters of an executable element's signature.
20+
///
21+
/// Equivalent to `getExecutableType(...).parameters`.
22+
List<ParameterElement> getExecutableParameters(ExecutableElement element) =>
23+
element.parameters;
24+
25+
/// Queries the return type of an executable element.
26+
///
27+
/// Equivalent to `getExecutableType(...).returnType`.
28+
DartType getExecutableReturnType(FunctionTypedElement element) =>
29+
element.returnType;
30+
31+
/// Queries the full type of an executable element.
32+
///
33+
/// Guaranteed to be a function type.
34+
FunctionType getExecutableType(FunctionTypedElement element) => element.type;
35+
36+
/// Queries the type of a variable element.
37+
DartType getVariableType(VariableElement variable) => variable.type;
38+
}

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import 'package:analyzer/src/dart/element/type.dart';
2525
import 'package:analyzer/src/dart/element/type_algebra.dart';
2626
import 'package:analyzer/src/dart/resolver/variance.dart';
2727
import 'package:analyzer/src/error/codes.dart' show StrongModeCode;
28+
import 'package:analyzer/src/generated/element_type_provider.dart';
2829
import 'package:analyzer/src/generated/engine.dart' show AnalysisOptionsImpl;
2930
import 'package:analyzer/src/generated/resolver.dart' show TypeProvider;
3031
import 'package:analyzer/src/generated/type_system.dart';
@@ -36,20 +37,25 @@ import 'ast_properties.dart';
3637
/// gets the known static type of the expression.
3738
DartType getExpressionType(
3839
Expression expression, TypeSystemImpl typeSystem, TypeProvider typeProvider,
39-
{bool read = false}) {
40+
{bool read = false,
41+
ElementTypeProvider elementTypeProvider = const ElementTypeProvider()}) {
4042
DartType type;
4143
if (read) {
42-
type = getReadType(expression);
44+
type = getReadType(expression, elementTypeProvider: elementTypeProvider);
4345
} else {
4446
type = expression.staticType;
4547
}
4648
type ??= DynamicTypeImpl.instance;
4749
return type;
4850
}
4951

50-
DartType getReadType(Expression expression) {
52+
DartType getReadType(Expression expression,
53+
{ElementTypeProvider elementTypeProvider = const ElementTypeProvider()}) {
5154
if (expression is IndexExpression) {
52-
return expression.auxiliaryElements?.staticElement?.returnType;
55+
var staticElement = expression.auxiliaryElements?.staticElement;
56+
return staticElement == null
57+
? null
58+
: elementTypeProvider.getExecutableReturnType(staticElement);
5359
}
5460
{
5561
Element setter;
@@ -63,14 +69,17 @@ DartType getReadType(Expression expression) {
6369
if (setter is PropertyAccessorElement && setter.isSetter) {
6470
var getter = setter.variable.getter;
6571
if (getter != null) {
66-
return getter.returnType;
72+
return elementTypeProvider.getExecutableReturnType(getter);
6773
}
6874
}
6975
}
7076
if (expression is SimpleIdentifier) {
7177
var aux = expression.auxiliaryElements;
7278
if (aux != null) {
73-
return aux.staticElement?.returnType;
79+
var staticElement = aux.staticElement;
80+
return staticElement == null
81+
? null
82+
: elementTypeProvider.getExecutableReturnType(staticElement);
7483
}
7584
}
7685
return expression.staticType;

0 commit comments

Comments
 (0)