Skip to content

Commit f033010

Browse files
scheglovCommit Queue
authored andcommitted
API. Add DartObject.constructorInvocation
The internal (see deprecation) accessor is currently used by source_gen and Angular. And it was also requested by Flutter Preview. Change-Id: I50b4491024629ee9f0cb647b71aa60b83642f6da Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/441001 Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Paul Berry <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent 4751222 commit f033010

File tree

12 files changed

+719
-43
lines changed

12 files changed

+719
-43
lines changed

pkg/analyzer/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 8.1.0-dev
2+
* Add `DartObject.constructorInvocation` with the constructor and arguments.
3+
14
## 8.0.0
25
* Remove deprecated element model V1.
36
* Remove deprecated `DartType.isStructurallyEqualTo`.

pkg/analyzer/api.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3241,8 +3241,15 @@ package:analyzer/dart/ast/visitor.dart:
32413241
visitWithClause (method: R? Function(WithClause))
32423242
visitYieldStatement (method: R? Function(YieldStatement))
32433243
package:analyzer/dart/constant/value.dart:
3244+
ConstructorInvocation (class extends Object):
3245+
new (constructor: ConstructorInvocation Function())
3246+
constructor (getter: ConstructorElement)
3247+
constructor2 (getter: ConstructorElement, deprecated)
3248+
namedArguments (getter: Map<String, DartObject>)
3249+
positionalArguments (getter: List<DartObject>)
32443250
DartObject (class extends Object):
32453251
new (constructor: DartObject Function())
3252+
constructorInvocation (getter: ConstructorInvocation?)
32463253
hasKnownValue (getter: bool)
32473254
isNull (getter: bool)
32483255
type (getter: DartType?)

pkg/analyzer/lib/dart/constant/value.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,22 @@ import 'package:analyzer/dart/element/element.dart';
1616
import 'package:analyzer/dart/element/type.dart';
1717
import 'package:meta/meta.dart';
1818

19+
/// Information about a const constructor invocation.
20+
abstract class ConstructorInvocation {
21+
/// The constructor that was called.
22+
ConstructorElement get constructor;
23+
24+
/// The constructor that was called.
25+
@Deprecated('Use constructor instead')
26+
ConstructorElement get constructor2;
27+
28+
/// The values of named arguments.
29+
Map<String, DartObject> get namedArguments;
30+
31+
/// The values of positional arguments.
32+
List<DartObject> get positionalArguments;
33+
}
34+
1935
/// A representation of the value of a compile-time constant expression.
2036
///
2137
/// Note that, unlike the mirrors system, the object being represented does *not*
@@ -26,6 +42,10 @@ import 'package:meta/meta.dart';
2642
///
2743
/// Clients may not extend, implement or mix-in this class.
2844
abstract class DartObject {
45+
/// If this object was created by invoking a const constructor, information
46+
/// about how the const constructor was invoked; otherwise `null`.
47+
ConstructorInvocation? get constructorInvocation;
48+
2949
/// Return `true` if the value of the object being represented is known.
3050
///
3151
/// This method will return `false` if

pkg/analyzer/lib/src/dart/constant/evaluation.dart

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ class ConstantEvaluationEngine {
362362
List<Expression> arguments,
363363
ConstructorElementMixin2 constructor,
364364
ConstantVisitor constantVisitor, {
365-
ConstructorInvocation? invocation,
365+
ConstructorInvocationImpl? invocation,
366366
}) {
367367
var result = _InstanceCreationEvaluator.evaluate(
368368
this,
@@ -411,7 +411,7 @@ class ConstantEvaluationEngine {
411411
List<Expression> arguments,
412412
ConstructorElementMixin2 constructor,
413413
ConstantVisitor constantVisitor, {
414-
ConstructorInvocation? invocation,
414+
ConstructorInvocationImpl? invocation,
415415
}) {
416416
return _InstanceCreationEvaluator.evaluate(
417417
this,
@@ -2848,7 +2848,7 @@ class _InstanceCreationEvaluator {
28482848

28492849
final List<TypeImpl>? _typeArguments;
28502850

2851-
final ConstructorInvocation _invocation;
2851+
final ConstructorInvocationImpl _invocation;
28522852

28532853
final Map<String, NamedExpression> _namedNodes;
28542854

@@ -2877,7 +2877,7 @@ class _InstanceCreationEvaluator {
28772877
required Map<String, NamedExpression> namedNodes,
28782878
required Map<String, DartObjectImpl> namedValues,
28792879
required List<DartObjectImpl> argumentValues,
2880-
required ConstructorInvocation invocation,
2880+
required ConstructorInvocationImpl invocation,
28812881
}) : _namedNodes = namedNodes,
28822882
_namedValues = namedValues,
28832883
_argumentValues = argumentValues,
@@ -3577,7 +3577,7 @@ class _InstanceCreationEvaluator {
35773577
List<TypeImpl>? typeArguments,
35783578
List<Expression> arguments,
35793579
ConstantVisitor constantVisitor, {
3580-
ConstructorInvocation? invocation,
3580+
ConstructorInvocationImpl? invocation,
35813581
}) {
35823582
if (!constructor.isConst) {
35833583
Token? keyword;
@@ -3604,7 +3604,7 @@ class _InstanceCreationEvaluator {
36043604
);
36053605
}
36063606

3607-
var argumentValues = <DartObjectImpl>[];
3607+
var positionalValues = <DartObjectImpl>[];
36083608
var namedNodes = <String, NamedExpression>{};
36093609
var namedValues = <String, DartObjectImpl>{};
36103610
for (var i = 0; i < arguments.length; i++) {
@@ -3639,13 +3639,13 @@ class _InstanceCreationEvaluator {
36393639
return argumentConstant;
36403640
}
36413641

3642-
argumentValues.add(argumentConstant);
3642+
positionalValues.add(argumentConstant);
36433643
}
36443644
}
36453645

3646-
invocation ??= ConstructorInvocation(
3646+
invocation ??= ConstructorInvocationImpl(
36473647
constructor,
3648-
argumentValues,
3648+
positionalValues,
36493649
namedValues,
36503650
);
36513651

@@ -3659,7 +3659,7 @@ class _InstanceCreationEvaluator {
36593659
typeArguments,
36603660
namedNodes: namedNodes,
36613661
namedValues: namedValues,
3662-
argumentValues: argumentValues,
3662+
argumentValues: positionalValues,
36633663
invocation: invocation,
36643664
);
36653665

pkg/analyzer/lib/src/dart/constant/value.dart

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import 'package:analyzer/src/dart/element/extensions.dart';
2424
import 'package:analyzer/src/dart/element/type.dart';
2525
import 'package:analyzer/src/dart/element/type_system.dart';
2626
import 'package:analyzer/src/error/codes.dart';
27+
import 'package:analyzer/src/utilities/extensions/object.dart';
2728
import 'package:meta/meta.dart';
2829

2930
/// The state of an object representing a boolean value.
@@ -140,36 +141,25 @@ class BoolState extends InstanceState {
140141
/// [InvalidConstant] represents an invalid result with error information.
141142
sealed class Constant {}
142143

143-
/// Information about a const constructor invocation.
144-
class ConstructorInvocation {
145-
/// The constructor that was called.
146-
final ConstructorElement constructor2;
144+
class ConstructorInvocationImpl implements ConstructorInvocation {
145+
@override
146+
final ConstructorElement constructor;
147147

148-
/// Values of specified arguments, actual values for positional, and `null`
149-
/// for named (which are provided as [namedArguments]).
150-
final List<DartObjectImpl?> _argumentValues;
148+
@override
149+
final List<DartObjectImpl> positionalArguments;
151150

152-
/// The named arguments passed to the constructor.
151+
@override
153152
final Map<String, DartObjectImpl> namedArguments;
154153

155-
ConstructorInvocation(
156-
this.constructor2,
157-
this._argumentValues,
154+
ConstructorInvocationImpl(
155+
this.constructor,
156+
this.positionalArguments,
158157
this.namedArguments,
159158
);
160159

161-
/// The positional arguments passed to the constructor.
162-
List<DartObjectImpl> get positionalArguments {
163-
var result = <DartObjectImpl>[];
164-
for (var argument in _argumentValues) {
165-
if (argument != null) {
166-
result.add(argument);
167-
} else {
168-
break;
169-
}
170-
}
171-
return result;
172-
}
160+
@Deprecated('Use constructor instead')
161+
@override
162+
ConstructorElement get constructor2 => constructor;
173163
}
174164

175165
/// A representation of an instance of a Dart class.
@@ -254,6 +244,11 @@ class DartObjectImpl implements DartObject, Constant {
254244
}
255245
}
256246

247+
@override
248+
ConstructorInvocationImpl? get constructorInvocation {
249+
return state.ifTypeOrNull<GenericState>()?.invocation;
250+
}
251+
257252
Map<String, DartObjectImpl>? get fields => state.fields;
258253

259254
@override
@@ -559,12 +554,9 @@ class DartObjectImpl implements DartObject, Constant {
559554

560555
/// Gets the constructor that was called to create this value, if this is a
561556
/// const constructor invocation. Otherwise returns null.
562-
ConstructorInvocation? getInvocation() {
563-
var state = this.state;
564-
if (state is GenericState) {
565-
return state.invocation;
566-
}
567-
return null;
557+
@Deprecated('Use constructorInvocation instead')
558+
ConstructorInvocationImpl? getInvocation() {
559+
return constructorInvocation;
568560
}
569561

570562
/// Return the result of invoking the '&gt;' operator on this object with the
@@ -1505,7 +1497,7 @@ class GenericState extends InstanceState {
15051497
final Map<String, DartObjectImpl> _fieldMap;
15061498

15071499
/// Information about the constructor invoked to generate this instance.
1508-
final ConstructorInvocation? invocation;
1500+
final ConstructorInvocationImpl? invocation;
15091501

15101502
@override
15111503
final bool isUnknown;

pkg/analyzer/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: analyzer
2-
version: 8.0.0
2+
version: 8.1.0-dev
33
description: >-
44
This package provides a library that performs static analysis of Dart code.
55
repository: https://github.com/dart-lang/sdk/tree/main/pkg/analyzer

0 commit comments

Comments
 (0)