|
| 1 | +// Copyright (c) 2021, 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/ast/ast.dart'; |
| 6 | +import 'package:analyzer/dart/element/type.dart'; |
| 7 | +import 'package:analyzer/src/dart/ast/ast.dart'; |
| 8 | +import 'package:analyzer/src/dart/element/member.dart'; |
| 9 | +import 'package:analyzer/src/error/codes.dart'; |
| 10 | +import 'package:analyzer/src/generated/resolver.dart'; |
| 11 | + |
| 12 | +/// A resolver for [ConstructorReference] nodes. |
| 13 | +class ConstructorReferenceResolver { |
| 14 | + /// The resolver driving this participant. |
| 15 | + final ResolverVisitor _resolver; |
| 16 | + |
| 17 | + ConstructorReferenceResolver(this._resolver); |
| 18 | + |
| 19 | + void resolve(ConstructorReferenceImpl node) { |
| 20 | + if (!_resolver.isConstructorTearoffsEnabled) { |
| 21 | + _resolver.errorReporter.reportErrorForNode( |
| 22 | + CompileTimeErrorCode.CONSTRUCTOR_TEAROFFS_NOT_ENABLED, node, []); |
| 23 | + } |
| 24 | + node.constructorName.accept(_resolver); |
| 25 | + _inferArgumentTypes(node); |
| 26 | + } |
| 27 | + |
| 28 | + void _inferArgumentTypes(ConstructorReferenceImpl node) { |
| 29 | + var constructorName = node.constructorName; |
| 30 | + var typeName = constructorName.type; |
| 31 | + var elementToInfer = _resolver.inferenceHelper.constructorElementToInfer( |
| 32 | + constructorName: constructorName, |
| 33 | + definingLibrary: _resolver.definingLibrary, |
| 34 | + ); |
| 35 | + |
| 36 | + // If the constructor is generic, we'll have a ConstructorMember that |
| 37 | + // substitutes in type arguments (possibly `dynamic`) from earlier in |
| 38 | + // resolution. |
| 39 | + // |
| 40 | + // Otherwise we'll have a ConstructorElement, and we can skip inference |
| 41 | + // because there's nothing to infer in a non-generic type. |
| 42 | + if (elementToInfer != null) { |
| 43 | + // TODO(leafp): Currently, we may re-infer types here, since we |
| 44 | + // sometimes resolve multiple times. We should really check that we |
| 45 | + // have not already inferred something. However, the obvious ways to |
| 46 | + // check this don't work, since we may have been instantiated |
| 47 | + // to bounds in an earlier phase, and we *do* want to do inference |
| 48 | + // in that case. |
| 49 | + |
| 50 | + // Get back to the uninstantiated generic constructor. |
| 51 | + // TODO(jmesserly): should we store this earlier in resolution? |
| 52 | + // Or look it up, instead of jumping backwards through the Member? |
| 53 | + var rawElement = elementToInfer.element; |
| 54 | + var constructorType = elementToInfer.asType; |
| 55 | + |
| 56 | + var inferred = _resolver.inferenceHelper.inferTearOff( |
| 57 | + node, constructorName.name!, constructorType) as FunctionType?; |
| 58 | + |
| 59 | + if (inferred != null) { |
| 60 | + typeName.type = inferred.returnType; |
| 61 | + |
| 62 | + // Update the static element as well. This is used in some cases, such |
| 63 | + // as computing constant values. It is stored in two places. |
| 64 | + var constructorElement = ConstructorMember.from( |
| 65 | + rawElement, |
| 66 | + inferred.returnType as InterfaceType, |
| 67 | + ); |
| 68 | + constructorName.staticElement = constructorElement; |
| 69 | + constructorName.name?.staticElement = constructorElement; |
| 70 | + node.staticType = inferred; |
| 71 | + } |
| 72 | + } else { |
| 73 | + node.staticType = node.constructorName.staticElement!.type; |
| 74 | + } |
| 75 | + } |
| 76 | +} |
0 commit comments