5.13.1.1.2.2. Determine the best
After collecting all candidates, it needs pick out the viable ones (during collection, we only consider types, no more) and determine which is the best by ranking the conversions involved. Below code does the processing.
build_user_type_conversion_1 (continue)
2478 candidates = splice_viable (candidates, pedantic , &any_viable_p);
2479 if (!any_viable_p)
2480 return 0;
2481
2482 cand = tourney (candidates);
5.13.1.1.2.2.1. Pick out viable
In below function, its parameter strict_p is passed with the value of pedantic , which if nonzero asks for the compiler to complain anything that standard forbids. Recall that if implicit conversion doesn’t work, implicit_conversion would return NULL; and for conversion forbidded by standard, ICS_BAD_FLAG flag would be set.
2165 static struct z_candidate *
2166 splice_viable (struct z_candidate *cands, in call.c
2167 bool strict_p,
2168 bool *any_viable_p)
2169 {
2170 struct z_candidate *viable;
2171 struct z_candidate **last_viable;
2172 struct z_candidate **cand;
2173
2174 viable = NULL;
2175 last_viable = &viable;
2176 *any_viable_p = false;
2177
2178 cand = &cands;
2179 while (*cand)
2180 {
2181 struct z_candidate *c = *cand;
2182 if (strict_p ? c->viable == 1 : c->viable)
2183 {
2184 *last_viable = c;
2185 *cand = c->next;
2186 c ->next = NULL;
2187 last_viable = &c->next;
2188 *any_viable_p = true;
2189 }
2190 else
2191 cand = &c->next;
2192 }
2193
2194 return viable ? viable : cands;
2195 }
5.13.1.1.2.2.2. Select out the best
In [3], clause 13.3.3.2 “Ranking implicit conversion sequences”, gives the rules for selecting the best candidate as below [over.ics.rank ].
And implicit_conversion realizes the first part of rule 2 below by trying standard conversion or reference binding first, and only trying user-defined conversion when it fails.
1. 13.3 .3.2 defines a partial ordering of implicit conversion sequences based on the relationships better conversion sequence and better conversion. If an implicit conversion sequence S1 is defined by these rules to be a better conversion sequence than S2, then it is also the case that S2 is a worse conversion sequence than S1. If conversion sequence S1 is neither better than nor worse than conversion sequence S2, S1 and S2 are said to be indistinguishable conversion sequences. 2. When comparing the basic forms of implicit conversion sequences (as defined in 13.3.3.1) — a standard conversion sequence (13.3.3.1.1) is a better conversion sequence than a user-defined conversion sequence or an ellipsis conversion sequence, and — a user-defined conversion sequence (13.3.3.1.2) is a better conversion sequence than an ellipsis conversion sequence (13.3.3.1.3). 3. Two implicit conversion sequences of the same form are indistinguishable conversion sequences unless one of the following rules apply: — Standard conversion sequence S1 is a better conversion sequence than standard conversion sequence S2 if — S1 is a proper subsequence of S2 (comparing the conversion sequences in the canonical form defined by 13.3.3.1.1, excluding any Lvalue Transformation; the identity conversion sequence is considered to be a subsequence of any non-identity conversion sequence) or, if not that, — the rank of S1 is better than the rank of S2, or S1 and S2 have the same rank and are distinguishable by the rules in the paragraph below, or, if not that, — S1 and S2 differ only in their qualification conversion and yield similar types T1 and T2 (4.4), respectively, and the cv-qualification signature of type T1 is a proper subset of the cv-qualification signature of type T2, and S1 is not the deprecated string literal array-to-pointer conversion (4.2). [Example: int f(const int *); int f(int *); int i; int j = f(&i); // Calls f(int *) —end example] or, if not that, — S1 and S2 are reference bindings (8.5.3), and the types to which the references refer are the same type except for top-level cv-qualifiers, and the type to which the reference initialized by S2 refers is more cv-qualified than the type to which the reference initialized by S1 refers. [Example: int f(const int &); int f(int &); int g(const int &); int g(int); int i; int j = f(i); // Calls f(int &) int k = g(i); // ambiguous class X { public : void f() const ; void f(); }; void g(const X& a, X b) { a.f(); //Calls X::f() const b.f(); //Calls X::f() } —end example] — User-defined conversion sequence U1 is a better conversion sequence than another user-defined conversion sequence U2 if they contain the same user-defined conversion function or constructor and if the second standard conversion sequence of U1 is better than the second standard conversion sequence of U2. [Example: struct A { operator short(); } a; int f(int); int f(float); int i = f(a); // Calls f(int), because short → int is better than short → float. —end example] 4. Standard conversion sequences are ordered by their ranks: an Exact Match is a better conversion than a Promotion, which is a better conversion than a Conversion. Two conversion sequences with the same rank are indistinguishable unless one of the following rules applies: — A conversion that is not a conversion of a pointer, or pointer to member, to bool is better than another conversion that is such a conversion. — If class B is derived directly or indirectly from class A, conversion of B* to A* is better than conversion of B* to void*, and conversion of A* to void* is better than conversion of B* to void*. — If class B is derived directly or indirectly from class A and class C is derived directly or indirectly from B, conversion of C* to B* is better than conversion of C* to A*, [Example: struct A {}; struct B : public A {}; struct C : public B {}; C *pc; int f(A *); int f(B *); int i = f(pc); // Calls f(B *) —end example] — binding of an expression of type C to a reference of type B& is better than binding an expression of type C to a reference of type A&, — conversion of A::* to B::* is better than conversion of A::* to C::*, — conversion of C to B is better than conversion of C to A, — conversion of B* to A* is better than conversion of C* to A*, — binding of an expression of type B to a reference of type A& is better than binding an expression of type C to a reference of type A&, — conversion of B::* to C::* is better than conversion of A::* to C::*, and — conversion of B to A is better than conversion of C to A. [Note: compared conversion sequences will have different source types only in the context of comparing the second standard conversion sequence of an initialization by user-defined conversion (see 13.3.3); in all other contexts, the source types will be the same and the target types will be different. ] |
5936 static struct z_candidate *
5937 tourney (struct z_candidate *candidates) in call.c
5938 {
5939 struct z_candidate *champ = candidates, *challenger;
5940 int fate;
5941 int champ_compared_to_predecessor = 0;
5942
5943 /* Walk through the list once, comparing each current champ to the next
5944 candidate, knocking out a candidate or two with each comparison. */
5945
5946 for (challenger = champ->next; challenger; )
5947 {
5948 fate = joust (champ, challenger, 0);
5949 if (fate == 1)
5950 challenger = challenger->next;
5951 else
5952 {
5953 if (fate == 0)
5954 {
5955 champ = challenger->next;
5956 if (champ == 0)
5957 return 0;
5958 champ_compared_to_predecessor = 0;
5959 }
5960 else
5961 {
5962 champ = challenger;
5963 champ_compared_to_predecessor = 1;
5964 }
5965
5966 challenger = champ->next;
5967 }
5968 }
5969
5970 /* Make sure the champ is better than all the candidates it hasn't yet
5971 been compared to. */
5972
5973 for (challenger = candidates;
5974 challenger != champ
5975 && !(champ_compared_to_predecessor && challenger->next == champ);
5976 challenger = challenger->next)
5977 {
5978 fate = joust (champ, challenger, 0);
5979 if (fate != 1)
5980 return 0;
5981 }
5982
5983 return champ;
5984 }
Candidates are compared one by one to select out the best by joust , which returns 1 if champ is better, -1 if challenger is better, or 0 if ambiguous. Note that champ always points to the candidate former in the list, and challenger is the one following champ . See that FOR block at line 5946 champ always points to the better of two comparators, if it can be determined.
In FOR block at line 5973, if champ_compared_to_predecessor is 0, then the exitting condition becomes: “challenger != champ ”; otherwise, it becomes: “challenger != champ && challenger->next != champ ”. Note that champ_compared_to_predecessor can be 1, only when last time challenger is found better than champ is not followed with ambiguous found (and then champ is updated with challenger ); and the flag is 0, only the last better selected is champ over chanlleger . So for the flag of 1, candidate before champ needesn’t be compared, but including it when flag is 0. Why needs the second FOR loop? First note that if the best candidate is found, the last time of comparison must not lead to ambiguity, otherwise the function will return 0 at line 5957. Next see that if ambiguity is found, both candidates under comparison are skipped, and the comparison resumes with candidates that follows. So it is possible the candidates found ambiguous may be better than the best candidate found in first FOR loop, and which is an error case with 0 returned at line 5980 in the second FOR loop.
See in above, viable candidates have been selected out with associated viable slot nonzero. And remember that if the conversion is not allowed by standard, this field will hold value of -1. As a quick pick, different value in viable tells out which is better. No doubt, conversion permitted by standard is always super.
5653 static int
5654 joust (struct z_candidate *cand1, struct z_candidate *cand2, bool warn) in call.c
5655 {
5656 int winner = 0;
5657 int i, off1 = 0, off2 = 0, len;
5658
5659 /* Candidates that involve bad conversions are always worse than those
5660 that don't. */
5661 if (cand1->viable > cand2->viable)
5662 return 1;
5663 if (cand1->viable < cand2->viable)
5664 return -1;
5665
5666 /* If we have two pseudo-candidates for conversions to the same type,
5667 or two candidates for the same function, arbitrarily pick one. */
5668 if (cand1->fn == cand2->fn
5669 && (TYPE_P (cand1->fn) || DECL_P (cand1->fn)))
5670 return 1;
5671
5672 /* a viable function F1
5673 is defined to be a better function than another viable function F2 if
5674 for all arguments i, ICSi(F1) is not a worse conversion sequence than
5675 ICSi(F2), and then */
5676
5677 /* for some argument j, ICSj(F1) is a better conversion sequence than
5678 ICSj(F2) */
5679
5680 /* For comparing static and non-static member functions, we ignore
5681 the implicit object parameter of the non-static function. The
5682 standard says to pretend that the static function has an object
5683 parm, but that won't work with operator overloading. */
5684 len = TREE_VEC_LENGTH (cand1->convs);
5685 if (len != TREE_VEC_LENGTH (cand2->convs))
5686 {
5687 if (DECL_STATIC_FUNCTION_P (cand1->fn)
5688 && ! DECL_STATIC_FUNCTION_P (cand2->fn))
5689 off2 = 1;
5690 else if (! DECL_STATIC_FUNCTION_P (cand1->fn)
5691 && DECL_STATIC_FUNCTION_P (cand2->fn))
5692 {
5693 off1 = 1;
5694 --len;
5695 }
5696 else
5697 abort ();
5698 }
5699
5700 for (i = 0; i < len; ++i)
5701 {
5702 tree t1 = TREE_VEC_ELT (cand1->convs, i+off1);
5703 tree t2 = TREE_VEC_ELT (cand2->convs, i+off2);
5704 int comp = compare_ics (t1, t2);
5705
5706 if (comp != 0)
5707 {
5708 if (warn_sign_promo
5709 && ICS_RANK (t1) + ICS_RANK (t2) == STD_RANK + PROMO_RANK
5710 && TREE_CODE (t1) == STD_CONV
5711 && TREE_CODE (t2) == STD_CONV
5712 && TREE_CODE (TREE_TYPE (t1)) == INTEGER_TYPE
5713 && TREE_CODE (TREE_TYPE (t2)) == INTEGER_TYPE
5714 && (TYPE_PRECISION (TREE_TYPE (t1))
5715 == TYPE_PRECISION (TREE_TYPE (t2)))
5716 && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t1, 0)))
5717 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (t1, 0)))
5718 == ENUMERAL_TYPE)))
5719 {
5720 tree type = TREE_TYPE (TREE_OPERAND (t1, 0));
5721 tree type1, type2;
5722 struct z_candidate *w, *l;
5723 if (comp > 0)
5724 type1 = TREE_TYPE (t1), type2 = TREE_TYPE (t2),
5725 w = cand1, l = cand2;
5726 else
5727 type1 = TREE_TYPE (t2), type2 = TREE_TYPE (t1),
5728 w = cand2, l = cand1;
5729
5730 if (warn)
5731 {
5732 warning ("passing `%T' chooses `%T' over `%T'",
5733 type, type1, type2);
5734 warning (" in call to `%D'", w->fn);
5735 }
5736 else
5737 add_warning (w, l);
5738 }
5739
5740 if (winner && comp != winner)
5741 {
5742 winner = 0;
5743 goto tweak;
5744 }
5745 winner = comp;
5746 }
5747 }
It is possible static conversion function are included tegother with non-static conversion function as candidates. If one of the two candidates is static method, and the other is not; it needs skip the implicit ‘this’ pointer during comparison. So t1 , and t2 at line 5702 and 5703 copy out the necessary arguments, and are passed to below function.
5293 static int
5294 compare_ics (tree ics1, tree ics2) in call.c
5295 {
5296 tree from_type1;
5297 tree from_type2;
5298 tree to_type1;
5299 tree to_type2;
5300 tree deref_from_type1 = NULL_TREE;
5301 tree deref_from_type2 = NULL_TREE;
5302 tree deref_to_type1 = NULL_TREE;
5303 tree deref_to_type2 = NULL_TREE;
5304 int rank1, rank2;
5305
5306 /* REF_BINDING is nonzero if the result of the conversion sequence
5307 is a reference type. In that case TARGET_TYPE is the
5308 type referred to by the reference. */
5309 tree target_type1;
5310 tree target_type2;
5311
5312 /* Handle implicit object parameters. */
5313 maybe_handle_implicit_object (&ics1);
5314 maybe_handle_implicit_object (&ics2);
5315
5316 /* Handle reference parameters. */
5317 target_type1 = maybe_handle_ref_bind (&ics1);
5318 target_type2 = maybe_handle_ref_bind (&ics2);
5319
5320 /* [over.ics.rank]
5321
5322 When comparing the basic forms of implicit conversion sequences (as
5323 defined in _over.best.ics_)
5324
5325 --a standard conversion sequence (_over.ics.scs_) is a better
5326 conversion sequence than a user-defined conversion sequence
5327 or an ellipsis conversion sequence, and
5328
5329 --a user-defined conversion sequence (_over.ics.user_) is a
5330 better conversion sequence than an ellipsis conversion sequence
5331 (_over.ics.ellipsis_). */
5332 rank1 = ICS_RANK (ics1);
5333 rank2 = ICS_RANK (ics2);
Below comment at line 5258 comes from [3], clause 13.3.1 “candidate functions and argument lists”, terms 4. It says now it needs convert ‘this’ pointer into reference with matching cv-qualifier.
5253 static void
5254 maybe_handle_implicit_object (tree *ics) in call.c
5255 {
5256 if (ICS_THIS_FLAG (*ics))
5257 {
5258 /* [over.match.funcs]
5259
5260 For non-static member functions, the type of the
5261 implicit object parameter is "reference to cv X"
5262 where X is the class of which the function is a
5263 member and cv is the cv-qualification on the member
5264 function declaration. */
5265 tree t = *ics;
5266 tree reference_type;
5267
5268 /* The `this' parameter is a pointer to a class type. Make the
5269 implicit conversion talk about a reference to that same class
5270 type. */
5271 reference_type = TREE_TYPE (TREE_TYPE (*ics));
5272 reference_type = build_reference_type (reference_type);
5273
5274 if (TREE_CODE (t) == QUAL_CONV)
5275 t = TREE_OPERAND (t, 0);
5276 if (TREE_CODE (t) == PTR_CONV)
5277 t = TREE_OPERAND (t, 0);
5278 t = build1 (IDENTITY_CONV, TREE_TYPE (TREE_TYPE (t)), NULL_TREE);
5279 t = direct_reference_binding (reference_type, t);
5280 *ics = t;
5281 }
5282 }
As now we know exactly the type referred, we can build the conversion sequence directly. The conversion we need to add shoud be REF_BIND solely. And note the code from line 5274 to 5277 above, see in standard_conversion line 603, PTR_CONV is always built in QUAL_CONV if they go tegother.
895 static tree
896 direct_reference_binding (tree type, tree conv) in call.c
897 {
898 tree t;
899
900 my_friendly_assert (TREE_CODE (type) == REFERENCE_TYPE, 20030306);
901 my_friendly_assert (TREE_CODE (TREE_TYPE (conv)) != REFERENCE_TYPE,
902 20030306);
903
904 t = TREE_TYPE (type);
905
906 /* [over.ics.rank]
907
908 When a parameter of reference type binds directly
909 (_dcl.init.ref_) to an argument expression, the implicit
910 conversion sequence is the identity conversion, unless the
911 argument expression has a type that is a derived class of the
912 parameter type, in which case the implicit conversion sequence is
913 a derived-to-base Conversion.
914
915 If the parameter binds directly to the result of applying a
916 conversion function to the argument expression, the implicit
917 conversion sequence is a user-defined conversion sequence
918 (_over.ics.user_), with the second standard conversion sequence
919 either an identity conversion or, if the conversion function
920 returns an entity of a type that is a derived class of the
921 parameter type, a derived-to-base conversion. */
922 if (!same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (conv)))
923 {
924 /* Represent the derived-to-base conversion. */
925 conv = build_conv (BASE_CONV, t, conv);
926 /* We will actually be binding to the base-class subobject in
927 the derived class, so we mark this conversion appropriately.
928 That way, convert_like knows not to generate a temporary. */
929 NEED_TEMPORARY_P (conv) = 0;
930 }
931 return build_conv (REF_BIND, type, conv);
932 }
Then at line 5317 above, maybe_handle_ref_bind fetches the remainder of the conversion, and return the type to which the reference refers by sequence of REF_BIND.
5270 static tree
5271 maybe_handle_ref_bind (tree *ics) in call.c
5272 {
5273 if (TREE_CODE (*ics) == REF_BIND)
5274 {
5275 tree old_ics = *ics;
5276 tree type = TREE_TYPE (TREE_TYPE (old_ics));
5277 *ics = TREE_OPERAND (old_ics, 0);
5278 ICS_USER_FLAG (*ics) = ICS_USER_FLAG (old_ics);
5279 ICS_BAD_FLAG (*ics) = ICS_BAD_FLAG (old_ics);
5280 return type;
5281 }
5282
5283 return NULL_TREE;
5284 }
ICS_RANK is defined as below. Before we have seen that C++ standard defines ranks for the standard conversions, which is saved in ICS_STD_RANK. Conversions other than standard conversion only have single rank assigned.
352 #define ICS_RANK (NODE) / in call.c
353 (ICS_BAD_FLAG (NODE) ? BAD_RANK /
354 : ICS_ELLIPSIS_FLAG (NODE) ? ELLIPSIS_RANK /
355 : ICS_USER_FLAG (NODE) ? USER_RANK /
356 : ICS_STD_RANK (NODE))
Remember if we can come here, BAD_RANK just indicates the conversion not permitted by standard, but OK as extension.
compare_ics (continue)
5335 if (rank1 > rank2)
5336 return -1;
5337 else if (rank1 < rank2)
5338 return 1;
5339
5340 if (rank1 == BAD_RANK)
5341 {
5342 /* XXX Isn't this an extension? */
5343 /* Both ICS are bad. We try to make a decision based on what
5344 would have happened if they'd been good. */
5345 if (ICS_USER_FLAG (ics1) > ICS_USER_FLAG (ics2)
5346 || ICS_STD_RANK (ics1) > ICS_STD_RANK (ics2))
5347 return -1;
5348 else if (ICS_USER_FLAG (ics1) < ICS_USER_FLAG (ics2)
5349 || ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
5350 return 1;
5351
5352 /* We couldn't make up our minds; try to figure it out below. */
5353 }
5354
5355 if (ICS_ELLIPSIS_FLAG (ics1))
5356 /* Both conversions are ellipsis conversions. */
5357 return 0;
5358
5359 /* User-defined conversion sequence U1 is a better conversion sequence
5360 than another user-defined conversion sequence U2 if they contain the
5361 same user-defined conversion operator or constructor and if the sec-
5362 ond standard conversion sequence of U1 is better than the second
5363 standard conversion sequence of U2. */
5364
5365 if (ICS_USER_FLAG (ics1))
5366 {
5367 tree t1, t2;
5368
5369 for (t1 = ics1; TREE_CODE (t1) != USER_CONV; t1 = TREE_OPERAND (t1, 0))
5370 if (TREE_CODE (t1) == AMBIG_CONV)
5371 return 0;
5372 for (t2 = ics2; TREE_CODE (t2) != USER_CONV; t2 = TREE_OPERAND (t2, 0))
5373 if (TREE_CODE (t2) == AMBIG_CONV)
5374 return 0;
5375
5376 if (USER_CONV_FN (t1) != USER_CONV_FN (t2))
5377 return 0;
5378
5379 /* We can just fall through here, after setting up
5380 FROM_TYPE1 and FROM_TYPE2. */
5381 from_type1 = TREE_TYPE (t1);
5382 from_type2 = TREE_TYPE (t2);
5383 }
5384 else
5385 {
5386 /* We're dealing with two standard conversion sequences.
5387
5388 [over.ics.rank]
5389
5390 Standard conversion sequence S1 is a better conversion
5391 sequence than standard conversion sequence S2 if
5392
5393 --S1 is a proper subsequence of S2 (comparing the conversion
5394 sequences in the canonical form defined by _over.ics.scs_,
5395 excluding any Lvalue Transformation; the identity
5396 conversion sequence is considered to be a subsequence of
5397 any non-identity conversion sequence */
5398
5399 from_type1 = ics1;
5400 while (TREE_CODE (from_type1) != IDENTITY_CONV)
5401 from_type1 = TREE_OPERAND (from_type1, 0);
5402 from_type1 = TREE_TYPE (from_type1);
5403
5404 from_type2 = ics2;
5405 while (TREE_CODE (from_type2) != IDENTITY_CONV)
5406 from_type2 = TREE_OPERAND (from_type2, 0);
5407 from_type2 = TREE_TYPE (from_type2);
5408 }
See above at line 5365, if the condition is satisfied, it indicates that the sequence contains user-defined conversion, and at that point, ics1 and ics2 have the same rank, so they either both contain user-defined conversion, or both not. USER_CONV_FN at line 5376 holds the conversion function had been found for the USER_CONV. If the two candidates have argument that converted by different user-defined conversion function, they are indistinguishable.
While for standard conversion sequence, in [3], clause 13.3.3.1.1, terms 2, it emphasises that the conversion should be applied in canonical order of: Lvalue Transformation , Promotion or Conversion , Qualifier Adjustment ; go back to standard_conversion , see that it generates the sequence exactly in that order but reversely.
compare_ics (continue)
5410 if (same_type_p (from_type1, from_type2))
5411 {
5412 if (is_subseq (ics1, ics2))
5413 return 1;
5414 if (is_subseq (ics2, ics1))
5415 return -1;
5416 }
Further in [3], clause 13.3.3.2, terms 3 (comment at line 5386), during ranking, the Lvalue Transformation should be excluded; in the front-end, this corresponds to LVALUE_CONV and RVALUE_CONV. So is_subseq returns true if ics1 is a proper subsequence of ics2 following the standard.
5177 static bool
5178 is_subseq (tree ics1, tree ics2) in call.c
5179 {
5180 /* We can assume that a conversion of the same code
5181 between the same types indicates a subsequence since we only get
5182 here if the types we are converting from are the same. */
5183
5184 while (TREE_CODE (ics1) == RVALUE_CONV
5185 || TREE_CODE (ics1) == LVALUE_CONV)
5186 ics1 = TREE_OPERAND (ics1, 0);
5187
5188 while (1)
5189 {
5190 while (TREE_CODE (ics2) == RVALUE_CONV
5191 || TREE_CODE (ics2) == LVALUE_CONV)
5192 ics2 = TREE_OPERAND (ics2, 0);
5193
5194 if (TREE_CODE (ics2) == USER_CONV
5195 || TREE_CODE (ics2) == AMBIG_CONV
5196 || TREE_CODE (ics2) == IDENTITY_CONV)
5197 /* At this point, ICS1 cannot be a proper subsequence of
5198 ICS2. We can get a USER_CONV when we are comparing the
5199 second standard conversion sequence of two user conversion
5200 sequences. */
5201 return false;
5202
5203 ics2 = TREE_OPERAND (ics2, 0);
5204
5205 if (TREE_CODE (ics2) == TREE_CODE (ics1)
5206 && same_type_p (TREE_TYPE (ics2), TREE_TYPE (ics1))
5207 && same_type_p (TREE_TYPE (TREE_OPERAND (ics2, 0)),
5208 TREE_TYPE (TREE_OPERAND (ics1, 0))))
5209 return true;
5210 }
5211 }