private void UnaryOperatorEasyOut(UnaryOperatorKind kind, BoundExpression operand, UnaryOperatorOverloadResolutionResult result) { var operandType = operand.Type; if (operandType == null) { return; } var easyOut = UnopEasyOut.OpKind(kind, operandType); if (easyOut == UnaryOperatorKind.Error) { return; } UnaryOperatorSignature signature = this.Compilation.builtInOperators.GetSignature(easyOut); Conversion? conversion = Conversions.FastClassifyConversion(operandType, signature.OperandType); Debug.Assert(conversion.HasValue && conversion.Value.IsImplicit); result.Results.Add(UnaryOperatorAnalysisResult.Applicable(signature, conversion.Value)); }
// Takes a list of candidates and mutates the list to throw out the ones that are worse than // another applicable candidate. private void UnaryOperatorOverloadResolution( BoundExpression operand, UnaryOperatorOverloadResolutionResult result) { // SPEC: Given the set of applicable candidate function members, the best function // member in that set is located. SPEC: If the set contains only one function member, // then that function member is the best function member. if (result.GetValidCount() == 1) { return; } // SPEC: Otherwise, the best function member is the one function member that is better than all other function // SPEC: members with respect to the given argument list, provided that each function member is compared to all // SPEC: other function members using the rules in 7.5.3.2. If there is not exactly one function member that is // SPEC: better than all other function members, then the function member invocation is ambiguous and a binding-time // SPEC: error occurs. // UNDONE: This is a naive quadratic algorithm; there is a linear algorithm that works. Consider using it. var candidates = result.Results; for (int i = 0; i < candidates.Count; ++i) { if (candidates[i].Kind != OperatorAnalysisResultKind.Applicable) { continue; } // Is this applicable operator better than every other applicable method? for (int j = 0; j < candidates.Count; ++j) { if (i == j) { continue; } if (candidates[j].Kind == OperatorAnalysisResultKind.Inapplicable) { continue; } var better = BetterConversionFromExpression(operand, candidates[i].Signature.OperandType, candidates[j].Signature.OperandType); if (better == BetterResult.Left) { candidates[j] = candidates[j].Worse(); } else if (better == BetterResult.Right) { candidates[i] = candidates[i].Worse(); } } } }
public void UnaryOperatorOverloadResolution(UnaryOperatorKind kind, BoundExpression operand, UnaryOperatorOverloadResolutionResult result) { Debug.Assert(operand != null); Debug.Assert(result.Results.Count == 0); // We can do a table lookup for well-known problems in overload resolution. UnaryOperatorEasyOut(kind, operand, result); if (result.Results.Count > 0) { return; } // SPEC: An operation of the form op x or x op, where op is an overloadable unary operator, // SPEC: and x is an expression of type X, is processed as follows: // NOTE: In fact, for a unary operator to apply the operand must always have a type. There are no // NOTE: unary operators that operate on lambdas, anonymous methods, method groups or null. // SPEC: The set of candidate user-defined operators provided by X for the operation operator // SPEC: op(x) is determined using the rules of 7.3.5. GetUserDefinedOperators(kind, operand, result.Results); // SPEC: If the set of candidate user-defined operators is not empty, then this becomes the // SPEC: set of candidate operators for the operation. Otherwise, the predefined unary operator // SPEC: implementations, including their lifted forms, become the set of candidate operators // SPEC: for the operation. var operators = result.Results; if (!result.AnyValid()) { GetAllBuiltInOperators(kind, operand, result.Results); } // SPEC: The overload resolution rules of 7.5.3 are applied to the set of candidate operators // SPEC: to select the best operator with respect to the argument list (x), and this operator // SPEC: becomes the result of the overload resolution process. If overload resolution fails // SPEC: to select a single best operator, a binding-time error occurs. // reduce candidates UnaryOperatorOverloadResolution(operand, result); }