|
| 1 | +package dotty.tools.dotc.util |
| 2 | + |
| 3 | +import dotty.tools.dotc.ast.Trees._ |
| 4 | +import dotty.tools.dotc.ast.tpd |
| 5 | +import dotty.tools.dotc.core.Constants.Constant |
| 6 | +import dotty.tools.dotc.core.Contexts.Context |
| 7 | +import dotty.tools.dotc.core.Denotations.SingleDenotation |
| 8 | +import dotty.tools.dotc.core.Flags.Implicit |
| 9 | +import dotty.tools.dotc.core.Names.TermName |
| 10 | +import dotty.tools.dotc.util.Positions.Position |
| 11 | +import dotty.tools.dotc.core.Types.{ErrorType, MethodType, PolyType} |
| 12 | +import dotty.tools.dotc.reporting.diagnostic.messages |
| 13 | + |
| 14 | +import scala.collection.JavaConverters._ |
| 15 | + |
| 16 | +object Signatures { |
| 17 | + |
| 18 | + /** |
| 19 | + * Represent a method signature. |
| 20 | + * |
| 21 | + * @param name The name of the method |
| 22 | + * @param tparams The type parameters and their bounds |
| 23 | + * @param paramss The parameter lists of this method |
| 24 | + * @param returnType The return type of this method, if this is not a constructor. |
| 25 | + * @param doc The documentation for this method. |
| 26 | + */ |
| 27 | + case class Signature(name: String, tparams: List[String], paramss: List[List[Param]], returnType: Option[String], doc: Option[String] = None) { |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Represent a method's parameter. |
| 32 | + * |
| 33 | + * @param name The name of the parameter |
| 34 | + * @param tpe The type of the parameter |
| 35 | + * @param doc The documentation of this parameter |
| 36 | + * @param isImplicit Is this parameter implicit? |
| 37 | + */ |
| 38 | + case class Param(name: String, tpe: String, doc: Option[String] = None, isImplicit: Boolean = false) { |
| 39 | + def show: String = |
| 40 | + s"$name: $tpe" |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Extract (current parameter index, function index, functions) out of a method call. |
| 45 | + * |
| 46 | + * @param path The path to the function application |
| 47 | + * @param pos The position of the cursor |
| 48 | + * @return A triple containing the index of the parameter being edited, the index of the function |
| 49 | + * being called, the list of overloads of this function). |
| 50 | + */ |
| 51 | + def callInfo(path: List[tpd.Tree], pos: Position)(implicit ctx: Context): (Int, Int, List[SingleDenotation]) = { |
| 52 | + path match { |
| 53 | + case Apply(fun, params) :: _ => |
| 54 | + val alreadyAppliedCount = Signatures.countParams(fun) |
| 55 | + val paramIndex = params.indexWhere(_.pos.contains(pos)) match { |
| 56 | + case -1 => (params.length - 1 max 0) + alreadyAppliedCount |
| 57 | + case n => n + alreadyAppliedCount |
| 58 | + } |
| 59 | + |
| 60 | + val (alternativeIndex, alternatives) = fun.tpe match { |
| 61 | + case err: ErrorType => |
| 62 | + val (alternativeIndex, alternatives) = alternativesFromError(err, params) |
| 63 | + (alternativeIndex, alternatives) |
| 64 | + |
| 65 | + case _ => |
| 66 | + val funSymbol = fun.symbol |
| 67 | + val alternatives = funSymbol.owner.info.member(funSymbol.name).alternatives |
| 68 | + val alternativeIndex = alternatives.indexOf(funSymbol.denot) max 0 |
| 69 | + (alternativeIndex, alternatives) |
| 70 | + } |
| 71 | + |
| 72 | + (paramIndex, alternativeIndex, alternatives) |
| 73 | + |
| 74 | + case _ => |
| 75 | + (0, 0, Nil) |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + def toSignature(denot: SingleDenotation)(implicit ctx: Context): Option[Signature] = { |
| 80 | + val symbol = denot.symbol |
| 81 | + val docComment = ParsedComment.docOf(symbol) |
| 82 | + val classTree = symbol.topLevelClass.asClass.rootTree |
| 83 | + val isImplicit: TermName => Boolean = tpd.defPath(symbol, classTree).lastOption match { |
| 84 | + case Some(DefDef(_, _, paramss, _, _)) => |
| 85 | + val flatParams = paramss.flatten |
| 86 | + name => flatParams.find(_.name == name).map(_.symbol.is(Implicit)).getOrElse(false) |
| 87 | + case _ => |
| 88 | + _ => false |
| 89 | + } |
| 90 | + |
| 91 | + denot.info.stripPoly match { |
| 92 | + case tpe: MethodType => |
| 93 | + val infos = { |
| 94 | + tpe.paramInfoss.zip(tpe.paramNamess).map { case (infos, names) => |
| 95 | + infos.zip(names).map { case (info, name) => |
| 96 | + Signatures.Param(name.show, |
| 97 | + info.widenTermRefExpr.show, |
| 98 | + docComment.flatMap(_.paramDoc(name)), |
| 99 | + isImplicit = isImplicit(name)) |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + val typeParams = denot.info match { |
| 105 | + case poly: PolyType => |
| 106 | + poly.paramNames.zip(poly.paramInfos).map { case (x, y) => x.show + y.show } |
| 107 | + case _ => |
| 108 | + Nil |
| 109 | + } |
| 110 | + |
| 111 | + val (name, returnType) = |
| 112 | + if (symbol.isConstructor) (symbol.owner.name.show, None) |
| 113 | + else (denot.name.show, Some(tpe.finalResultType.widenTermRefExpr.show)) |
| 114 | + |
| 115 | + val signature = |
| 116 | + Signatures.Signature(name, |
| 117 | + typeParams, |
| 118 | + infos, |
| 119 | + returnType, |
| 120 | + docComment.map(_.mainDoc)) |
| 121 | + |
| 122 | + Some(signature) |
| 123 | + |
| 124 | + case other => |
| 125 | + None |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * The number of parameters that are applied in `tree`. |
| 131 | + * |
| 132 | + * This handles currying, so for an application such as `foo(1, 2)(3)`, the result of |
| 133 | + * `countParams` should be 3. |
| 134 | + * |
| 135 | + * @param tree The tree to inspect. |
| 136 | + * @return The number of parameters that are passed. |
| 137 | + */ |
| 138 | + private def countParams(tree: tpd.Tree): Int = { |
| 139 | + tree match { |
| 140 | + case Apply(fun, params) => countParams(fun) + params.length |
| 141 | + case _ => 0 |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Inspect `err` to determine, if it is an error related to application of an overloaded |
| 147 | + * function, what were the possible alternatives. |
| 148 | + * |
| 149 | + * If several alternatives are found, determines what is the best suited alternatives |
| 150 | + * given the parameters `params`: The alternative that has the most formal parameters |
| 151 | + * matching the given arguments is chosen. |
| 152 | + * |
| 153 | + * @param err The error message to inspect. |
| 154 | + * @param params The parameters that were given at the call site. |
| 155 | + * @return A pair composed of the index of the best alternative (0 if no alternatives |
| 156 | + * were found), and the list of alternatives. |
| 157 | + */ |
| 158 | + private def alternativesFromError(err: ErrorType, params: List[tpd.Tree])(implicit ctx: Context): (Int, List[SingleDenotation]) = { |
| 159 | + val alternatives = |
| 160 | + err.msg match { |
| 161 | + case messages.AmbiguousOverload(_, alternatives, _) => |
| 162 | + alternatives |
| 163 | + case messages.NoMatchingOverload(alternatives, _) => |
| 164 | + alternatives |
| 165 | + case _ => |
| 166 | + Nil |
| 167 | + } |
| 168 | + |
| 169 | + // If the user writes `foo(bar, <cursor>)`, the typer will insert a synthetic |
| 170 | + // `null` parameter: `foo(bar, null)`. This may influence what's the "best" |
| 171 | + // alternative, so we discard it. |
| 172 | + val userParams = params match { |
| 173 | + case xs :+ (nul @ Literal(Constant(null))) if nul.pos.isZeroExtent => xs |
| 174 | + case _ => params |
| 175 | + } |
| 176 | + val userParamsTypes = userParams.map(_.tpe) |
| 177 | + |
| 178 | + // Assign a score to each alternative (how many parameters are correct so far), and |
| 179 | + // use that to determine what is the current active signature. |
| 180 | + val alternativesScores = alternatives.map { alt => |
| 181 | + alt.info.stripPoly match { |
| 182 | + case tpe: MethodType => |
| 183 | + userParamsTypes.zip(tpe.paramInfos).takeWhile{ case (t0, t1) => t0 <:< t1 }.size |
| 184 | + case _ => |
| 185 | + 0 |
| 186 | + } |
| 187 | + } |
| 188 | + val bestAlternative = |
| 189 | + if (alternativesScores.isEmpty) 0 |
| 190 | + else alternativesScores.zipWithIndex.maxBy(_._1)._2 |
| 191 | + |
| 192 | + (bestAlternative, alternatives) |
| 193 | + } |
| 194 | + |
| 195 | +} |
| 196 | + |
0 commit comments