|
| 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/generated/resolver.dart'; |
| 10 | + |
| 11 | +/// A resolver for [InstanceCreationExpression] nodes. |
| 12 | +/// |
| 13 | +/// This resolver is responsible for rewriting a given |
| 14 | +/// [InstanceCreationExpression] as a [MethodInvocation] if the parsed |
| 15 | +/// [ConstructorName]'s `type` resolves to a [FunctionReference] or |
| 16 | +/// [ConstructorReference], instead of a [TypeName]. |
| 17 | +class InstanceCreationExpressionResolver { |
| 18 | + /// The resolver driving this participant. |
| 19 | + final ResolverVisitor _resolver; |
| 20 | + |
| 21 | + InstanceCreationExpressionResolver(this._resolver); |
| 22 | + |
| 23 | + void resolve(InstanceCreationExpressionImpl node) { |
| 24 | + // The parser can parse certain code as [InstanceCreationExpression] when it |
| 25 | + // might be an invocation of a method on a [FunctionReference] or |
| 26 | + // [ConstructorReference]. In such a case, it is this resolver's |
| 27 | + // responsibility to rewrite. For example, given: |
| 28 | + // |
| 29 | + // a.m<int>.apply(); |
| 30 | + // |
| 31 | + // the parser will give an InstanceCreationExpression (`a.m<int>.apply()`) |
| 32 | + // with a name of `a.m<int>.apply` (ConstructorName) with a type of |
| 33 | + // `a.m<int>` (TypeName with a name of `a.m` (PrefixedIdentifier) and |
| 34 | + // typeArguments of `<int>`) and a name of `apply` (SimpleIdentifier). If |
| 35 | + // `a.m<int>` is actually a function reference, then the |
| 36 | + // InstanceCreationExpression needs to be rewritten as a MethodInvocation |
| 37 | + // with a target of `a.m<int>` (a FunctionReference) and a name of `apply`. |
| 38 | + if (node.keyword == null) { |
| 39 | + var typeNameTypeArguments = node.constructorName.type.typeArguments; |
| 40 | + if (typeNameTypeArguments != null) { |
| 41 | + // This could be a method call on a function reference or a constructor |
| 42 | + // reference. |
| 43 | + _resolveWithTypeNameWithTypeArguments(node, typeNameTypeArguments); |
| 44 | + return; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + _resolveInstanceCreationExpression(node); |
| 49 | + } |
| 50 | + |
| 51 | + void _inferArgumentTypes(covariant InstanceCreationExpressionImpl node) { |
| 52 | + var constructorName = node.constructorName; |
| 53 | + var typeName = constructorName.type; |
| 54 | + var typeArguments = typeName.typeArguments; |
| 55 | + var elementToInfer = _resolver.inferenceHelper.constructorElementToInfer( |
| 56 | + constructorName: constructorName, |
| 57 | + definingLibrary: _resolver.definingLibrary, |
| 58 | + ); |
| 59 | + FunctionType? inferred; |
| 60 | + |
| 61 | + // If the constructor is generic, we'll have a ConstructorMember that |
| 62 | + // substitutes in type arguments (possibly `dynamic`) from earlier in |
| 63 | + // resolution. |
| 64 | + // |
| 65 | + // Otherwise we'll have a ConstructorElement, and we can skip inference |
| 66 | + // because there's nothing to infer in a non-generic type. |
| 67 | + if (elementToInfer != null) { |
| 68 | + // TODO(leafp): Currently, we may re-infer types here, since we |
| 69 | + // sometimes resolve multiple times. We should really check that we |
| 70 | + // have not already inferred something. However, the obvious ways to |
| 71 | + // check this don't work, since we may have been instantiated |
| 72 | + // to bounds in an earlier phase, and we *do* want to do inference |
| 73 | + // in that case. |
| 74 | + |
| 75 | + // Get back to the uninstantiated generic constructor. |
| 76 | + // TODO(jmesserly): should we store this earlier in resolution? |
| 77 | + // Or look it up, instead of jumping backwards through the Member? |
| 78 | + var rawElement = elementToInfer.element; |
| 79 | + var constructorType = elementToInfer.asType; |
| 80 | + |
| 81 | + inferred = _resolver.inferenceHelper.inferArgumentTypesForGeneric( |
| 82 | + node, constructorType, typeArguments, |
| 83 | + isConst: node.isConst, errorNode: node.constructorName); |
| 84 | + |
| 85 | + if (inferred != null) { |
| 86 | + var arguments = node.argumentList; |
| 87 | + InferenceContext.setType(arguments, inferred); |
| 88 | + // Fix up the parameter elements based on inferred method. |
| 89 | + arguments.correspondingStaticParameters = |
| 90 | + ResolverVisitor.resolveArgumentsToParameters( |
| 91 | + arguments, inferred.parameters, null); |
| 92 | + |
| 93 | + constructorName.type.type = inferred.returnType; |
| 94 | + |
| 95 | + // Update the static element as well. This is used in some cases, such |
| 96 | + // as computing constant values. It is stored in two places. |
| 97 | + var constructorElement = ConstructorMember.from( |
| 98 | + rawElement, |
| 99 | + inferred.returnType as InterfaceType, |
| 100 | + ); |
| 101 | + constructorName.staticElement = constructorElement; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + if (inferred == null) { |
| 106 | + var constructorElement = constructorName.staticElement; |
| 107 | + if (constructorElement != null) { |
| 108 | + var type = constructorElement.type; |
| 109 | + type = _resolver.toLegacyTypeIfOptOut(type) as FunctionType; |
| 110 | + InferenceContext.setType(node.argumentList, type); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + void _resolveInstanceCreationExpression(InstanceCreationExpressionImpl node) { |
| 116 | + var whyNotPromotedList = <WhyNotPromotedGetter>[]; |
| 117 | + node.constructorName.accept(_resolver); |
| 118 | + _inferArgumentTypes(node); |
| 119 | + _resolver.visitArgumentList(node.argumentList, |
| 120 | + whyNotPromotedList: whyNotPromotedList); |
| 121 | + node.accept(_resolver.elementResolver); |
| 122 | + node.accept(_resolver.typeAnalyzer); |
| 123 | + _resolver.checkForArgumentTypesNotAssignableInList( |
| 124 | + node.argumentList, whyNotPromotedList); |
| 125 | + } |
| 126 | + |
| 127 | + /// Resolve [node] which has a [TypeName] with type arguments (given as |
| 128 | + /// [typeNameTypeArguments]). |
| 129 | + /// |
| 130 | + /// The instance creation expression may actually be a method call on a |
| 131 | + /// type-instantiated function reference or constructor reference. |
| 132 | + void _resolveWithTypeNameWithTypeArguments( |
| 133 | + InstanceCreationExpressionImpl node, |
| 134 | + TypeArgumentListImpl typeNameTypeArguments, |
| 135 | + ) { |
| 136 | + var typeNameName = node.constructorName.type.name; |
| 137 | + if (typeNameName is SimpleIdentifierImpl) { |
| 138 | + // TODO(srawlins): Lookup the name and potentially rewrite `node` as a |
| 139 | + // [MethodInvocation]. |
| 140 | + _resolveInstanceCreationExpression(node); |
| 141 | + return; |
| 142 | + } else if (typeNameName is PrefixedIdentifierImpl) { |
| 143 | + // TODO(srawlins): Lookup the name and potentially rewrite `node` as a |
| 144 | + // [MethodInvocation]. |
| 145 | + _resolveInstanceCreationExpression(node); |
| 146 | + } else { |
| 147 | + assert( |
| 148 | + false, 'Unexpected typeNameName type: ${typeNameName.runtimeType}'); |
| 149 | + _resolveInstanceCreationExpression(node); |
| 150 | + } |
| 151 | + } |
| 152 | +} |
0 commit comments