]>
Commit | Line | Data |
---|---|---|
8d08fdba | 1 | /* Functions related to invoking methods and overloaded functions. |
e5e809f4 | 2 | Copyright (C) 1987, 92-97, 1998 Free Software Foundation, Inc. |
8d08fdba | 3 | Contributed by Michael Tiemann (tiemann@cygnus.com) and |
e5e809f4 | 4 | modified by Brendan Kehoe (brendan@cygnus.com). |
8d08fdba MS |
5 | |
6 | This file is part of GNU CC. | |
7 | ||
8 | GNU CC is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2, or (at your option) | |
11 | any later version. | |
12 | ||
13 | GNU CC is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with GNU CC; see the file COPYING. If not, write to | |
e9fa0c7c RK |
20 | the Free Software Foundation, 59 Temple Place - Suite 330, |
21 | Boston, MA 02111-1307, USA. */ | |
8d08fdba MS |
22 | |
23 | ||
e92cc029 | 24 | /* High-level class interface. */ |
8d08fdba MS |
25 | |
26 | #include "config.h" | |
8d052bc7 | 27 | #include "system.h" |
570221c2 | 28 | #include "tree.h" |
8d08fdba | 29 | #include "cp-tree.h" |
e8abc66f | 30 | #include "output.h" |
8d08fdba | 31 | #include "flags.h" |
570221c2 | 32 | #include "rtl.h" |
8d08fdba MS |
33 | |
34 | #include "obstack.h" | |
35 | #define obstack_chunk_alloc xmalloc | |
36 | #define obstack_chunk_free free | |
37 | ||
8d08fdba | 38 | extern int inhibit_warnings; |
8d08fdba MS |
39 | extern tree ctor_label, dtor_label; |
40 | ||
277294d7 | 41 | static tree build_new_method_call PROTO((tree, tree, tree, tree, int)); |
e92cc029 | 42 | |
49c249e1 JM |
43 | static tree build_field_call PROTO((tree, tree, tree, tree)); |
44 | static tree find_scoped_type PROTO((tree, tree, tree)); | |
49c249e1 | 45 | static struct z_candidate * tourney PROTO((struct z_candidate *)); |
5ffe581d | 46 | static int joust PROTO((struct z_candidate *, struct z_candidate *, int)); |
49c249e1 JM |
47 | static int compare_qual PROTO((tree, tree)); |
48 | static int compare_ics PROTO((tree, tree)); | |
5ffe581d | 49 | static tree build_over_call PROTO((struct z_candidate *, tree, int)); |
49c249e1 | 50 | static tree convert_default_arg PROTO((tree, tree)); |
49c249e1 JM |
51 | static tree convert_like PROTO((tree, tree)); |
52 | static void op_error PROTO((enum tree_code, enum tree_code, tree, tree, | |
53 | tree, char *)); | |
54 | static tree build_object_call PROTO((tree, tree)); | |
55 | static tree resolve_args PROTO((tree)); | |
56 | static struct z_candidate * build_user_type_conversion_1 | |
57 | PROTO ((tree, tree, int)); | |
58 | static void print_z_candidates PROTO((struct z_candidate *)); | |
59 | static tree build_this PROTO((tree)); | |
60 | static struct z_candidate * splice_viable PROTO((struct z_candidate *)); | |
61 | static int any_viable PROTO((struct z_candidate *)); | |
62 | static struct z_candidate * add_template_candidate | |
386b8a85 | 63 | PROTO((struct z_candidate *, tree, tree, tree, tree, int)); |
69ac77ce JL |
64 | static struct z_candidate * add_template_candidate_real |
65 | PROTO((struct z_candidate *, tree, tree, tree, tree, int, tree)); | |
786b5245 MM |
66 | static struct z_candidate * add_template_conv_candidate |
67 | PROTO((struct z_candidate *, tree, tree, tree, tree)); | |
49c249e1 JM |
68 | static struct z_candidate * add_builtin_candidates |
69 | PROTO((struct z_candidate *, enum tree_code, enum tree_code, | |
70 | tree, tree *, int)); | |
71 | static struct z_candidate * add_builtin_candidate | |
72 | PROTO((struct z_candidate *, enum tree_code, enum tree_code, | |
73 | tree, tree, tree, tree *, tree *, int)); | |
74 | static int is_complete PROTO((tree)); | |
75 | static struct z_candidate * build_builtin_candidate | |
76 | PROTO((struct z_candidate *, tree, tree, tree, tree *, tree *, | |
77 | int)); | |
78 | static struct z_candidate * add_conv_candidate | |
79 | PROTO((struct z_candidate *, tree, tree, tree)); | |
80 | static struct z_candidate * add_function_candidate | |
81 | PROTO((struct z_candidate *, tree, tree, int)); | |
82 | static tree implicit_conversion PROTO((tree, tree, tree, int)); | |
83 | static tree standard_conversion PROTO((tree, tree, tree)); | |
84 | static tree reference_binding PROTO((tree, tree, tree, int)); | |
85 | static tree strip_top_quals PROTO((tree)); | |
86 | static tree non_reference PROTO((tree)); | |
87 | static tree build_conv PROTO((enum tree_code, tree, tree)); | |
9f617717 | 88 | static int is_subseq PROTO((tree, tree)); |
49c249e1 | 89 | |
277294d7 JM |
90 | tree |
91 | build_vfield_ref (datum, type) | |
92 | tree datum, type; | |
8d08fdba | 93 | { |
277294d7 JM |
94 | tree rval; |
95 | int old_assume_nonnull_objects = flag_assume_nonnull_objects; | |
8d08fdba | 96 | |
277294d7 JM |
97 | if (datum == error_mark_node) |
98 | return error_mark_node; | |
e92cc029 | 99 | |
277294d7 JM |
100 | /* Vtable references are always made from non-null objects. */ |
101 | flag_assume_nonnull_objects = 1; | |
102 | if (TREE_CODE (TREE_TYPE (datum)) == REFERENCE_TYPE) | |
103 | datum = convert_from_reference (datum); | |
8d08fdba | 104 | |
277294d7 JM |
105 | if (! TYPE_USES_COMPLEX_INHERITANCE (type)) |
106 | rval = build (COMPONENT_REF, TREE_TYPE (CLASSTYPE_VFIELD (type)), | |
107 | datum, CLASSTYPE_VFIELD (type)); | |
108 | else | |
109 | rval = build_component_ref (datum, DECL_NAME (CLASSTYPE_VFIELD (type)), NULL_TREE, 0); | |
110 | flag_assume_nonnull_objects = old_assume_nonnull_objects; | |
8d08fdba | 111 | |
277294d7 | 112 | return rval; |
8d08fdba MS |
113 | } |
114 | ||
277294d7 JM |
115 | /* Build a call to a member of an object. I.e., one that overloads |
116 | operator ()(), or is a pointer-to-function or pointer-to-method. */ | |
e92cc029 | 117 | |
277294d7 JM |
118 | static tree |
119 | build_field_call (basetype_path, instance_ptr, name, parms) | |
120 | tree basetype_path, instance_ptr, name, parms; | |
8d08fdba | 121 | { |
277294d7 | 122 | tree field, instance; |
8d08fdba | 123 | |
277294d7 JM |
124 | if (name == ctor_identifier || name == dtor_identifier) |
125 | return NULL_TREE; | |
8d08fdba | 126 | |
277294d7 | 127 | if (instance_ptr == current_class_ptr) |
f30432d7 | 128 | { |
277294d7 JM |
129 | /* Check to see if we really have a reference to an instance variable |
130 | with `operator()()' overloaded. */ | |
131 | field = IDENTIFIER_CLASS_VALUE (name); | |
f30432d7 | 132 | |
277294d7 | 133 | if (field == NULL_TREE) |
f30432d7 | 134 | { |
277294d7 JM |
135 | cp_error ("`this' has no member named `%D'", name); |
136 | return error_mark_node; | |
f30432d7 | 137 | } |
8d08fdba | 138 | |
277294d7 | 139 | if (TREE_CODE (field) == FIELD_DECL) |
8d08fdba | 140 | { |
277294d7 JM |
141 | /* If it's a field, try overloading operator (), |
142 | or calling if the field is a pointer-to-function. */ | |
143 | instance = build_component_ref_1 (current_class_ref, field, 0); | |
144 | if (instance == error_mark_node) | |
145 | return error_mark_node; | |
8d08fdba | 146 | |
277294d7 JM |
147 | if (TYPE_LANG_SPECIFIC (TREE_TYPE (instance))) |
148 | return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, instance, parms, NULL_TREE); | |
863adfc0 | 149 | |
277294d7 | 150 | if (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE) |
863adfc0 | 151 | { |
277294d7 JM |
152 | if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance))) == FUNCTION_TYPE) |
153 | return build_function_call (instance, parms); | |
154 | else if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance))) == METHOD_TYPE) | |
155 | return build_function_call (instance, expr_tree_cons (NULL_TREE, current_class_ptr, parms)); | |
863adfc0 | 156 | } |
863adfc0 | 157 | } |
277294d7 JM |
158 | return NULL_TREE; |
159 | } | |
863adfc0 | 160 | |
277294d7 JM |
161 | /* Check to see if this is not really a reference to an instance variable |
162 | with `operator()()' overloaded. */ | |
163 | field = lookup_field (basetype_path, name, 1, 0); | |
8d08fdba | 164 | |
277294d7 JM |
165 | /* This can happen if the reference was ambiguous or for access |
166 | violations. */ | |
167 | if (field == error_mark_node) | |
168 | return error_mark_node; | |
8ccc31eb | 169 | |
297dcfb3 | 170 | if (field && TREE_CODE (field) == FIELD_DECL) |
277294d7 JM |
171 | { |
172 | tree basetype; | |
173 | tree ftype = TREE_TYPE (field); | |
8d08fdba | 174 | |
277294d7 JM |
175 | if (TREE_CODE (ftype) == REFERENCE_TYPE) |
176 | ftype = TREE_TYPE (ftype); | |
8d08fdba | 177 | |
277294d7 | 178 | if (TYPE_LANG_SPECIFIC (ftype)) |
8d08fdba | 179 | { |
277294d7 JM |
180 | /* Make the next search for this field very short. */ |
181 | basetype = DECL_FIELD_CONTEXT (field); | |
182 | instance_ptr = convert_pointer_to (basetype, instance_ptr); | |
8d08fdba | 183 | |
277294d7 JM |
184 | instance = build_indirect_ref (instance_ptr, NULL_PTR); |
185 | return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, | |
186 | build_component_ref_1 (instance, field, 0), | |
187 | parms, NULL_TREE); | |
8d08fdba | 188 | } |
277294d7 | 189 | if (TREE_CODE (ftype) == POINTER_TYPE) |
8d08fdba | 190 | { |
277294d7 JM |
191 | if (TREE_CODE (TREE_TYPE (ftype)) == FUNCTION_TYPE |
192 | || TREE_CODE (TREE_TYPE (ftype)) == METHOD_TYPE) | |
8d08fdba | 193 | { |
277294d7 JM |
194 | /* This is a member which is a pointer to function. */ |
195 | tree ref | |
196 | = build_component_ref_1 (build_indirect_ref (instance_ptr, | |
197 | NULL_PTR), | |
198 | field, LOOKUP_COMPLAIN); | |
199 | if (ref == error_mark_node) | |
200 | return error_mark_node; | |
201 | return build_function_call (ref, parms); | |
8d08fdba | 202 | } |
8d08fdba | 203 | } |
277294d7 | 204 | else if (TREE_CODE (ftype) == METHOD_TYPE) |
8d08fdba | 205 | { |
277294d7 JM |
206 | error ("invalid call via pointer-to-member function"); |
207 | return error_mark_node; | |
8d08fdba | 208 | } |
863adfc0 | 209 | else |
277294d7 | 210 | return NULL_TREE; |
8d08fdba | 211 | } |
277294d7 JM |
212 | return NULL_TREE; |
213 | } | |
8d08fdba | 214 | |
277294d7 JM |
215 | static tree |
216 | find_scoped_type (type, inner_name, inner_types) | |
217 | tree type, inner_name, inner_types; | |
218 | { | |
219 | tree tags = CLASSTYPE_TAGS (type); | |
8d08fdba | 220 | |
277294d7 | 221 | while (tags) |
e1cd6e56 | 222 | { |
277294d7 JM |
223 | /* The TREE_PURPOSE of an enum tag (which becomes a member of the |
224 | enclosing class) is set to the name for the enum type. So, if | |
225 | inner_name is `bar', and we strike `baz' for `enum bar { baz }', | |
226 | then this test will be true. */ | |
227 | if (TREE_PURPOSE (tags) == inner_name) | |
d18c083e | 228 | { |
277294d7 JM |
229 | if (inner_types == NULL_TREE) |
230 | return TYPE_MAIN_DECL (TREE_VALUE (tags)); | |
231 | return resolve_scope_to_name (TREE_VALUE (tags), inner_types); | |
d18c083e | 232 | } |
277294d7 | 233 | tags = TREE_CHAIN (tags); |
e1cd6e56 MS |
234 | } |
235 | ||
277294d7 JM |
236 | /* Look for a TYPE_DECL. */ |
237 | for (tags = TYPE_FIELDS (type); tags; tags = TREE_CHAIN (tags)) | |
238 | if (TREE_CODE (tags) == TYPE_DECL && DECL_NAME (tags) == inner_name) | |
239 | { | |
240 | /* Code by raeburn. */ | |
241 | if (inner_types == NULL_TREE) | |
242 | return tags; | |
243 | return resolve_scope_to_name (TREE_TYPE (tags), inner_types); | |
244 | } | |
8d08fdba | 245 | |
277294d7 JM |
246 | return NULL_TREE; |
247 | } | |
248 | ||
249 | /* Resolve an expression NAME1::NAME2::...::NAMEn to | |
250 | the name that names the above nested type. INNER_TYPES | |
251 | is a chain of nested type names (held together by SCOPE_REFs); | |
252 | OUTER_TYPE is the type we know to enclose INNER_TYPES. | |
253 | Returns NULL_TREE if there is an error. */ | |
8d08fdba | 254 | |
277294d7 JM |
255 | tree |
256 | resolve_scope_to_name (outer_type, inner_stuff) | |
257 | tree outer_type, inner_stuff; | |
258 | { | |
259 | register tree tmp; | |
260 | tree inner_name, inner_type; | |
8d08fdba | 261 | |
277294d7 JM |
262 | if (outer_type == NULL_TREE && current_class_type != NULL_TREE) |
263 | { | |
264 | /* We first try to look for a nesting in our current class context, | |
265 | then try any enclosing classes. */ | |
266 | tree type = current_class_type; | |
267 | ||
268 | while (type && (TREE_CODE (type) == RECORD_TYPE | |
269 | || TREE_CODE (type) == UNION_TYPE)) | |
270 | { | |
271 | tree rval = resolve_scope_to_name (type, inner_stuff); | |
272 | ||
273 | if (rval != NULL_TREE) | |
274 | return rval; | |
275 | type = DECL_CONTEXT (TYPE_MAIN_DECL (type)); | |
8d08fdba MS |
276 | } |
277 | } | |
278 | ||
277294d7 | 279 | if (TREE_CODE (inner_stuff) == SCOPE_REF) |
8d08fdba | 280 | { |
277294d7 JM |
281 | inner_name = TREE_OPERAND (inner_stuff, 0); |
282 | inner_type = TREE_OPERAND (inner_stuff, 1); | |
8d08fdba | 283 | } |
277294d7 | 284 | else |
f30432d7 | 285 | { |
277294d7 JM |
286 | inner_name = inner_stuff; |
287 | inner_type = NULL_TREE; | |
f30432d7 | 288 | } |
8d08fdba | 289 | |
277294d7 | 290 | if (outer_type == NULL_TREE) |
8d08fdba | 291 | { |
277294d7 JM |
292 | tree x; |
293 | /* If we have something that's already a type by itself, | |
294 | use that. */ | |
295 | if (IDENTIFIER_HAS_TYPE_VALUE (inner_name)) | |
8d08fdba | 296 | { |
277294d7 JM |
297 | if (inner_type) |
298 | return resolve_scope_to_name (IDENTIFIER_TYPE_VALUE (inner_name), | |
299 | inner_type); | |
300 | return inner_name; | |
8d08fdba | 301 | } |
277294d7 JM |
302 | |
303 | x = lookup_name (inner_name, 0); | |
8d08fdba | 304 | |
277294d7 | 305 | if (x && TREE_CODE (x) == NAMESPACE_DECL) |
8d08fdba | 306 | { |
277294d7 JM |
307 | x = lookup_namespace_name (x, inner_type); |
308 | return x; | |
8d08fdba | 309 | } |
277294d7 JM |
310 | return NULL_TREE; |
311 | } | |
8d08fdba | 312 | |
277294d7 JM |
313 | if (! IS_AGGR_TYPE (outer_type)) |
314 | return NULL_TREE; | |
8d08fdba | 315 | |
277294d7 JM |
316 | /* Look for member classes or enums. */ |
317 | tmp = find_scoped_type (outer_type, inner_name, inner_type); | |
8d08fdba | 318 | |
277294d7 JM |
319 | /* If it's not a type in this class, then go down into the |
320 | base classes and search there. */ | |
321 | if (! tmp && TYPE_BINFO (outer_type)) | |
8d08fdba | 322 | { |
277294d7 JM |
323 | tree binfos = TYPE_BINFO_BASETYPES (outer_type); |
324 | int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0; | |
8d08fdba | 325 | |
277294d7 | 326 | for (i = 0; i < n_baselinks; i++) |
8d08fdba | 327 | { |
277294d7 JM |
328 | tree base_binfo = TREE_VEC_ELT (binfos, i); |
329 | tmp = resolve_scope_to_name (BINFO_TYPE (base_binfo), inner_stuff); | |
330 | if (tmp) | |
331 | return tmp; | |
8d08fdba | 332 | } |
277294d7 | 333 | tmp = NULL_TREE; |
8d08fdba | 334 | } |
8926095f | 335 | |
277294d7 JM |
336 | return tmp; |
337 | } | |
8926095f | 338 | |
277294d7 JM |
339 | /* Build a method call of the form `EXP->SCOPES::NAME (PARMS)'. |
340 | This is how virtual function calls are avoided. */ | |
8d08fdba | 341 | |
277294d7 JM |
342 | tree |
343 | build_scoped_method_call (exp, basetype, name, parms) | |
344 | tree exp, basetype, name, parms; | |
345 | { | |
346 | /* Because this syntactic form does not allow | |
347 | a pointer to a base class to be `stolen', | |
348 | we need not protect the derived->base conversion | |
349 | that happens here. | |
350 | ||
351 | @@ But we do have to check access privileges later. */ | |
352 | tree binfo, decl; | |
353 | tree type = TREE_TYPE (exp); | |
8d08fdba | 354 | |
277294d7 JM |
355 | if (type == error_mark_node |
356 | || basetype == error_mark_node) | |
8d08fdba MS |
357 | return error_mark_node; |
358 | ||
277294d7 | 359 | if (processing_template_decl) |
8d08fdba | 360 | { |
277294d7 | 361 | if (TREE_CODE (name) == BIT_NOT_EXPR) |
ce122a86 | 362 | { |
277294d7 JM |
363 | tree type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0); |
364 | if (type) | |
365 | name = build_min_nt (BIT_NOT_EXPR, type); | |
8d08fdba | 366 | } |
277294d7 JM |
367 | name = build_min_nt (SCOPE_REF, basetype, name); |
368 | return build_min_nt (METHOD_CALL_EXPR, name, exp, parms, NULL_TREE); | |
ce122a86 | 369 | } |
8d08fdba | 370 | |
277294d7 JM |
371 | if (TREE_CODE (type) == REFERENCE_TYPE) |
372 | type = TREE_TYPE (type); | |
8d08fdba | 373 | |
277294d7 | 374 | if (TREE_CODE (basetype) == TREE_VEC) |
8d08fdba | 375 | { |
277294d7 JM |
376 | binfo = basetype; |
377 | basetype = BINFO_TYPE (binfo); | |
8d08fdba | 378 | } |
277294d7 JM |
379 | else |
380 | binfo = NULL_TREE; | |
8d08fdba | 381 | |
277294d7 JM |
382 | /* Destructors can be "called" for simple types; see 5.2.4 and 12.4 Note |
383 | that explicit ~int is caught in the parser; this deals with typedefs | |
384 | and template parms. */ | |
385 | if (TREE_CODE (name) == BIT_NOT_EXPR && ! IS_AGGR_TYPE (basetype)) | |
8d08fdba | 386 | { |
a1774733 | 387 | if (TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (basetype)) |
277294d7 JM |
388 | cp_error ("type of `%E' does not match destructor type `%T' (type was `%T')", |
389 | exp, basetype, type); | |
390 | name = TREE_OPERAND (name, 0); | |
a1774733 BK |
391 | if (TYPE_MAIN_VARIANT (basetype) != name |
392 | && basetype != get_type_value (name)) | |
277294d7 JM |
393 | cp_error ("qualified type `%T' does not match destructor name `~%T'", |
394 | basetype, name); | |
395 | return cp_convert (void_type_node, exp); | |
8d08fdba MS |
396 | } |
397 | ||
277294d7 JM |
398 | if (! is_aggr_type (basetype, 1)) |
399 | return error_mark_node; | |
8d08fdba | 400 | |
277294d7 | 401 | if (! IS_AGGR_TYPE (type)) |
8d08fdba | 402 | { |
277294d7 JM |
403 | cp_error ("base object `%E' of scoped method call is of non-aggregate type `%T'", |
404 | exp, type); | |
8d08fdba MS |
405 | return error_mark_node; |
406 | } | |
407 | ||
277294d7 | 408 | if (! binfo) |
8d08fdba | 409 | { |
277294d7 JM |
410 | binfo = get_binfo (basetype, type, 1); |
411 | if (binfo == error_mark_node) | |
412 | return error_mark_node; | |
413 | if (! binfo) | |
414 | error_not_base_type (basetype, type); | |
8d08fdba | 415 | } |
277294d7 JM |
416 | |
417 | if (binfo) | |
8d08fdba | 418 | { |
277294d7 JM |
419 | if (TREE_CODE (exp) == INDIRECT_REF) |
420 | decl = build_indirect_ref | |
421 | (convert_pointer_to_real | |
422 | (binfo, build_unary_op (ADDR_EXPR, exp, 0)), NULL_PTR); | |
423 | else | |
424 | decl = build_scoped_ref (exp, basetype); | |
8d08fdba | 425 | |
277294d7 JM |
426 | /* Call to a destructor. */ |
427 | if (TREE_CODE (name) == BIT_NOT_EXPR) | |
8d08fdba | 428 | { |
277294d7 JM |
429 | /* Explicit call to destructor. */ |
430 | name = TREE_OPERAND (name, 0); | |
431 | if (! (name == TYPE_MAIN_VARIANT (TREE_TYPE (decl)) | |
432 | || name == constructor_name (TREE_TYPE (decl)) | |
433 | || TREE_TYPE (decl) == get_type_value (name))) | |
8d08fdba | 434 | { |
277294d7 JM |
435 | cp_error |
436 | ("qualified type `%T' does not match destructor name `~%T'", | |
437 | TREE_TYPE (decl), name); | |
438 | return error_mark_node; | |
8d08fdba | 439 | } |
277294d7 JM |
440 | if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (decl))) |
441 | return cp_convert (void_type_node, exp); | |
442 | ||
443 | return build_delete (TREE_TYPE (decl), decl, integer_two_node, | |
444 | LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, | |
445 | 0); | |
8d08fdba | 446 | } |
277294d7 JM |
447 | |
448 | /* Call to a method. */ | |
449 | return build_method_call (decl, name, parms, binfo, | |
450 | LOOKUP_NORMAL|LOOKUP_NONVIRTUAL); | |
8d08fdba | 451 | } |
277294d7 JM |
452 | return error_mark_node; |
453 | } | |
8d08fdba | 454 | |
277294d7 JM |
455 | /* We want the address of a function or method. We avoid creating a |
456 | pointer-to-member function. */ | |
457 | ||
458 | tree | |
459 | build_addr_func (function) | |
460 | tree function; | |
461 | { | |
462 | tree type = TREE_TYPE (function); | |
878cd289 | 463 | |
277294d7 JM |
464 | /* We have to do these by hand to avoid real pointer to member |
465 | functions. */ | |
466 | if (TREE_CODE (type) == METHOD_TYPE) | |
8d08fdba | 467 | { |
277294d7 | 468 | tree addr; |
8d08fdba | 469 | |
277294d7 | 470 | type = build_pointer_type (type); |
8d08fdba | 471 | |
277294d7 JM |
472 | if (mark_addressable (function) == 0) |
473 | return error_mark_node; | |
8d08fdba | 474 | |
277294d7 | 475 | addr = build1 (ADDR_EXPR, type, function); |
8d08fdba | 476 | |
277294d7 JM |
477 | /* Address of a static or external variable or function counts |
478 | as a constant */ | |
479 | if (staticp (function)) | |
480 | TREE_CONSTANT (addr) = 1; | |
8d08fdba | 481 | |
277294d7 JM |
482 | function = addr; |
483 | } | |
484 | else | |
485 | function = default_conversion (function); | |
8d08fdba | 486 | |
277294d7 JM |
487 | return function; |
488 | } | |
8d08fdba | 489 | |
277294d7 JM |
490 | /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or |
491 | POINTER_TYPE to those. Note, pointer to member function types | |
492 | (TYPE_PTRMEMFUNC_P) must be handled by our callers. */ | |
8d08fdba MS |
493 | |
494 | tree | |
277294d7 JM |
495 | build_call (function, result_type, parms) |
496 | tree function, result_type, parms; | |
8d08fdba | 497 | { |
277294d7 | 498 | int is_constructor = 0; |
570221c2 | 499 | tree tmp; |
7c76b292 | 500 | tree decl; |
8d08fdba | 501 | |
277294d7 | 502 | function = build_addr_func (function); |
8d08fdba | 503 | |
277294d7 | 504 | if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function))) |
8d08fdba | 505 | { |
277294d7 | 506 | sorry ("unable to call pointer to member function here"); |
ce122a86 | 507 | return error_mark_node; |
8d08fdba | 508 | } |
ce122a86 | 509 | |
277294d7 | 510 | if (TREE_CODE (function) == ADDR_EXPR |
7c76b292 JM |
511 | && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL) |
512 | decl = TREE_OPERAND (function, 0); | |
513 | else | |
514 | decl = NULL_TREE; | |
515 | ||
516 | if (decl && DECL_CONSTRUCTOR_P (decl)) | |
277294d7 | 517 | is_constructor = 1; |
8d08fdba | 518 | |
7c76b292 | 519 | /* Don't pass empty class objects by value. This is useful |
570221c2 JM |
520 | for tags in STL, which are used to control overload resolution. |
521 | We don't need to handle other cases of copying empty classes. */ | |
7c76b292 JM |
522 | if (! decl || ! DECL_BUILT_IN (decl)) |
523 | for (tmp = parms; tmp; tmp = TREE_CHAIN (tmp)) | |
524 | if (is_empty_class (TREE_TYPE (TREE_VALUE (tmp))) | |
525 | && ! TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (tmp)))) | |
526 | { | |
527 | tree t = make_node (RTL_EXPR); | |
528 | TREE_TYPE (t) = TREE_TYPE (TREE_VALUE (tmp)); | |
529 | RTL_EXPR_RTL (t) = const0_rtx; | |
530 | RTL_EXPR_SEQUENCE (t) = NULL_RTX; | |
a59ca936 JM |
531 | TREE_VALUE (tmp) = build (COMPOUND_EXPR, TREE_TYPE (t), |
532 | TREE_VALUE (tmp), t); | |
7c76b292 | 533 | } |
570221c2 | 534 | |
277294d7 JM |
535 | function = build_nt (CALL_EXPR, function, parms, NULL_TREE); |
536 | TREE_HAS_CONSTRUCTOR (function) = is_constructor; | |
537 | TREE_TYPE (function) = result_type; | |
538 | TREE_SIDE_EFFECTS (function) = 1; | |
539 | ||
540 | return function; | |
541 | } | |
8d08fdba | 542 | |
277294d7 JM |
543 | /* Build something of the form ptr->method (args) |
544 | or object.method (args). This can also build | |
545 | calls to constructors, and find friends. | |
8d08fdba | 546 | |
277294d7 JM |
547 | Member functions always take their class variable |
548 | as a pointer. | |
8d08fdba | 549 | |
277294d7 | 550 | INSTANCE is a class instance. |
8d08fdba | 551 | |
277294d7 | 552 | NAME is the name of the method desired, usually an IDENTIFIER_NODE. |
8d08fdba | 553 | |
277294d7 | 554 | PARMS help to figure out what that NAME really refers to. |
8d08fdba | 555 | |
277294d7 JM |
556 | BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE |
557 | down to the real instance type to use for access checking. We need this | |
558 | information to get protected accesses correct. This parameter is used | |
559 | by build_member_call. | |
8d08fdba | 560 | |
277294d7 JM |
561 | FLAGS is the logical disjunction of zero or more LOOKUP_ |
562 | flags. See cp-tree.h for more info. | |
8d08fdba | 563 | |
277294d7 JM |
564 | If this is all OK, calls build_function_call with the resolved |
565 | member function. | |
a4443a08 | 566 | |
277294d7 JM |
567 | This function must also handle being called to perform |
568 | initialization, promotion/coercion of arguments, and | |
569 | instantiation of default parameters. | |
a4443a08 | 570 | |
277294d7 JM |
571 | Note that NAME may refer to an instance variable name. If |
572 | `operator()()' is defined for the type of that field, then we return | |
573 | that result. */ | |
8d08fdba | 574 | |
277294d7 JM |
575 | tree |
576 | build_method_call (instance, name, parms, basetype_path, flags) | |
577 | tree instance, name, parms, basetype_path; | |
578 | int flags; | |
579 | { | |
56c5d8bf | 580 | tree basetype, instance_ptr; |
8d08fdba | 581 | |
277294d7 JM |
582 | #ifdef GATHER_STATISTICS |
583 | n_build_method_call++; | |
584 | #endif | |
8d08fdba | 585 | |
277294d7 JM |
586 | if (instance == error_mark_node |
587 | || name == error_mark_node | |
588 | || parms == error_mark_node | |
589 | || (instance != NULL_TREE && TREE_TYPE (instance) == error_mark_node)) | |
590 | return error_mark_node; | |
8d08fdba | 591 | |
277294d7 JM |
592 | if (processing_template_decl) |
593 | { | |
594 | if (TREE_CODE (name) == BIT_NOT_EXPR) | |
595 | { | |
7bae46f4 JM |
596 | tree type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0); |
597 | if (type) | |
598 | name = build_min_nt (BIT_NOT_EXPR, type); | |
8d08fdba | 599 | } |
277294d7 JM |
600 | |
601 | return build_min_nt (METHOD_CALL_EXPR, name, instance, parms, NULL_TREE); | |
8d08fdba MS |
602 | } |
603 | ||
277294d7 JM |
604 | /* This is the logic that magically deletes the second argument to |
605 | operator delete, if it is not needed. */ | |
606 | if (name == ansi_opname[(int) DELETE_EXPR] && list_length (parms)==2) | |
8d08fdba | 607 | { |
277294d7 | 608 | tree save_last = TREE_CHAIN (parms); |
56c5d8bf | 609 | |
277294d7 JM |
610 | /* get rid of unneeded argument */ |
611 | TREE_CHAIN (parms) = NULL_TREE; | |
56c5d8bf MH |
612 | if (build_method_call (instance, name, parms, basetype_path, |
613 | (LOOKUP_SPECULATIVELY|flags) & ~LOOKUP_COMPLAIN)) | |
614 | { | |
615 | /* If it finds a match, return it. */ | |
616 | return build_method_call (instance, name, parms, basetype_path, flags); | |
617 | } | |
277294d7 JM |
618 | /* If it doesn't work, two argument delete must work */ |
619 | TREE_CHAIN (parms) = save_last; | |
620 | } | |
621 | /* We already know whether it's needed or not for vec delete. */ | |
622 | else if (name == ansi_opname[(int) VEC_DELETE_EXPR] | |
623 | && TYPE_LANG_SPECIFIC (TREE_TYPE (instance)) | |
624 | && ! TYPE_VEC_DELETE_TAKES_SIZE (TREE_TYPE (instance))) | |
625 | TREE_CHAIN (parms) = NULL_TREE; | |
8d08fdba | 626 | |
277294d7 JM |
627 | if (TREE_CODE (name) == BIT_NOT_EXPR) |
628 | { | |
629 | flags |= LOOKUP_DESTRUCTOR; | |
630 | name = TREE_OPERAND (name, 0); | |
631 | if (parms) | |
632 | error ("destructors take no parameters"); | |
633 | basetype = TREE_TYPE (instance); | |
634 | if (TREE_CODE (basetype) == REFERENCE_TYPE) | |
635 | basetype = TREE_TYPE (basetype); | |
a1774733 | 636 | if (! (name == TYPE_MAIN_VARIANT (basetype) |
277294d7 JM |
637 | || (IS_AGGR_TYPE (basetype) |
638 | && name == constructor_name (basetype)) | |
639 | || basetype == get_type_value (name))) | |
8d08fdba | 640 | { |
277294d7 JM |
641 | cp_error ("destructor name `~%D' does not match type `%T' of expression", |
642 | name, basetype); | |
643 | return cp_convert (void_type_node, instance); | |
8d08fdba MS |
644 | } |
645 | ||
277294d7 JM |
646 | if (! TYPE_HAS_DESTRUCTOR (complete_type (basetype))) |
647 | return cp_convert (void_type_node, instance); | |
648 | instance = default_conversion (instance); | |
649 | instance_ptr = build_unary_op (ADDR_EXPR, instance, 0); | |
650 | return build_delete (build_pointer_type (basetype), | |
651 | instance_ptr, integer_two_node, | |
652 | LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0); | |
8d08fdba MS |
653 | } |
654 | ||
277294d7 | 655 | return build_new_method_call (instance, name, parms, basetype_path, flags); |
8d08fdba | 656 | } |
c73964b2 MS |
657 | |
658 | /* New overloading code. */ | |
659 | ||
660 | struct z_candidate { | |
661 | tree fn; | |
662 | tree convs; | |
663 | tree second_conv; | |
664 | int viable; | |
665 | tree basetype_path; | |
666 | tree template; | |
5ffe581d | 667 | tree warnings; |
c73964b2 MS |
668 | struct z_candidate *next; |
669 | }; | |
670 | ||
671 | #define IDENTITY_RANK 0 | |
672 | #define EXACT_RANK 1 | |
673 | #define PROMO_RANK 2 | |
674 | #define STD_RANK 3 | |
675 | #define PBOOL_RANK 4 | |
676 | #define USER_RANK 5 | |
677 | #define ELLIPSIS_RANK 6 | |
d11ad92e | 678 | #define BAD_RANK 7 |
c73964b2 MS |
679 | |
680 | #define ICS_RANK(NODE) \ | |
d11ad92e MS |
681 | (ICS_BAD_FLAG (NODE) ? BAD_RANK \ |
682 | : ICS_ELLIPSIS_FLAG (NODE) ? ELLIPSIS_RANK \ | |
c73964b2 MS |
683 | : ICS_USER_FLAG (NODE) ? USER_RANK \ |
684 | : ICS_STD_RANK (NODE)) | |
685 | ||
686 | #define ICS_STD_RANK(NODE) TREE_COMPLEXITY (NODE) | |
687 | ||
688 | #define ICS_USER_FLAG(NODE) TREE_LANG_FLAG_0 (NODE) | |
689 | #define ICS_ELLIPSIS_FLAG(NODE) TREE_LANG_FLAG_1 (NODE) | |
d11ad92e MS |
690 | #define ICS_THIS_FLAG(NODE) TREE_LANG_FLAG_2 (NODE) |
691 | #define ICS_BAD_FLAG(NODE) TREE_LANG_FLAG_3 (NODE) | |
c73964b2 | 692 | |
5ffe581d JM |
693 | #define USER_CONV_CAND(NODE) \ |
694 | ((struct z_candidate *)WRAPPER_PTR (TREE_OPERAND (NODE, 1))) | |
695 | #define USER_CONV_FN(NODE) (USER_CONV_CAND (NODE)->fn) | |
c73964b2 | 696 | |
c73964b2 MS |
697 | int |
698 | null_ptr_cst_p (t) | |
699 | tree t; | |
700 | { | |
d11ad92e | 701 | if (t == null_node |
a703fb38 | 702 | || (integer_zerop (t) && TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)) |
c73964b2 | 703 | return 1; |
c73964b2 MS |
704 | return 0; |
705 | } | |
706 | ||
824b9a4c | 707 | static tree |
c73964b2 MS |
708 | build_conv (code, type, from) |
709 | enum tree_code code; | |
710 | tree type, from; | |
711 | { | |
712 | tree t = build1 (code, type, from); | |
713 | int rank = ICS_STD_RANK (from); | |
714 | switch (code) | |
715 | { | |
716 | case PTR_CONV: | |
717 | case PMEM_CONV: | |
718 | case BASE_CONV: | |
719 | case STD_CONV: | |
720 | if (rank < STD_RANK) | |
721 | rank = STD_RANK; | |
722 | break; | |
723 | ||
c73964b2 | 724 | case QUAL_CONV: |
c73964b2 MS |
725 | if (rank < EXACT_RANK) |
726 | rank = EXACT_RANK; | |
727 | ||
728 | default: | |
729 | break; | |
730 | } | |
731 | ICS_STD_RANK (t) = rank; | |
732 | ICS_USER_FLAG (t) = ICS_USER_FLAG (from); | |
d11ad92e | 733 | ICS_BAD_FLAG (t) = ICS_BAD_FLAG (from); |
c73964b2 MS |
734 | return t; |
735 | } | |
736 | ||
824b9a4c | 737 | static tree |
c73964b2 MS |
738 | non_reference (t) |
739 | tree t; | |
740 | { | |
741 | if (TREE_CODE (t) == REFERENCE_TYPE) | |
742 | t = TREE_TYPE (t); | |
743 | return t; | |
744 | } | |
745 | ||
824b9a4c | 746 | static tree |
de22184b MS |
747 | strip_top_quals (t) |
748 | tree t; | |
749 | { | |
750 | if (TREE_CODE (t) == ARRAY_TYPE) | |
751 | return t; | |
752 | return TYPE_MAIN_VARIANT (t); | |
753 | } | |
754 | ||
c73964b2 MS |
755 | /* Returns the standard conversion path (see [conv]) from type FROM to type |
756 | TO, if any. For proper handling of null pointer constants, you must | |
757 | also pass the expression EXPR to convert from. */ | |
758 | ||
824b9a4c | 759 | static tree |
c73964b2 MS |
760 | standard_conversion (to, from, expr) |
761 | tree to, from, expr; | |
762 | { | |
763 | enum tree_code fcode, tcode; | |
764 | tree conv; | |
de22184b MS |
765 | int fromref = 0; |
766 | ||
767 | if (TREE_CODE (to) == REFERENCE_TYPE) | |
768 | to = TREE_TYPE (to); | |
769 | if (TREE_CODE (from) == REFERENCE_TYPE) | |
770 | { | |
771 | fromref = 1; | |
772 | from = TREE_TYPE (from); | |
773 | } | |
774 | to = strip_top_quals (to); | |
775 | from = strip_top_quals (from); | |
c73964b2 MS |
776 | |
777 | fcode = TREE_CODE (from); | |
778 | tcode = TREE_CODE (to); | |
779 | ||
780 | conv = build1 (IDENTITY_CONV, from, expr); | |
781 | ||
c73964b2 MS |
782 | if (fcode == FUNCTION_TYPE) |
783 | { | |
784 | from = build_pointer_type (from); | |
785 | fcode = TREE_CODE (from); | |
786 | conv = build_conv (LVALUE_CONV, from, conv); | |
787 | } | |
788 | else if (fcode == ARRAY_TYPE) | |
789 | { | |
790 | from = build_pointer_type (TREE_TYPE (from)); | |
791 | fcode = TREE_CODE (from); | |
792 | conv = build_conv (LVALUE_CONV, from, conv); | |
793 | } | |
de22184b MS |
794 | else if (fromref || (expr && real_lvalue_p (expr))) |
795 | conv = build_conv (RVALUE_CONV, from, conv); | |
796 | ||
797 | if (from == to) | |
798 | return conv; | |
c73964b2 MS |
799 | |
800 | if ((tcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (to)) | |
801 | && expr && null_ptr_cst_p (expr)) | |
802 | { | |
803 | conv = build_conv (STD_CONV, to, conv); | |
804 | } | |
805 | else if (tcode == POINTER_TYPE && fcode == POINTER_TYPE) | |
806 | { | |
807 | enum tree_code ufcode = TREE_CODE (TREE_TYPE (from)); | |
808 | enum tree_code utcode = TREE_CODE (TREE_TYPE (to)); | |
809 | ||
4d50dd69 JM |
810 | if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (from)), |
811 | TYPE_MAIN_VARIANT (TREE_TYPE (to)), 1)) | |
812 | ; | |
813 | else if (utcode == VOID_TYPE && ufcode != OFFSET_TYPE | |
814 | && ufcode != FUNCTION_TYPE) | |
c73964b2 MS |
815 | { |
816 | from = build_pointer_type | |
817 | (cp_build_type_variant (void_type_node, | |
818 | TYPE_READONLY (TREE_TYPE (from)), | |
819 | TYPE_VOLATILE (TREE_TYPE (from)))); | |
798eed5e | 820 | conv = build_conv (PTR_CONV, from, conv); |
c73964b2 MS |
821 | } |
822 | else if (ufcode == OFFSET_TYPE && utcode == OFFSET_TYPE) | |
823 | { | |
824 | tree fbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (from)); | |
825 | tree tbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (to)); | |
826 | ||
827 | if (DERIVED_FROM_P (fbase, tbase) | |
828 | && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (from))), | |
829 | TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (to))), | |
830 | 1))) | |
831 | { | |
832 | from = build_offset_type (tbase, TREE_TYPE (TREE_TYPE (from))); | |
833 | from = build_pointer_type (from); | |
798eed5e | 834 | conv = build_conv (PMEM_CONV, from, conv); |
c73964b2 | 835 | } |
c73964b2 MS |
836 | } |
837 | else if (IS_AGGR_TYPE (TREE_TYPE (from)) | |
838 | && IS_AGGR_TYPE (TREE_TYPE (to))) | |
839 | { | |
840 | if (DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from))) | |
841 | { | |
842 | from = cp_build_type_variant (TREE_TYPE (to), | |
843 | TYPE_READONLY (TREE_TYPE (from)), | |
844 | TYPE_VOLATILE (TREE_TYPE (from))); | |
845 | from = build_pointer_type (from); | |
798eed5e | 846 | conv = build_conv (PTR_CONV, from, conv); |
c73964b2 | 847 | } |
c73964b2 | 848 | } |
c73964b2 | 849 | |
798eed5e JM |
850 | if (comptypes (from, to, 1)) |
851 | /* OK */; | |
852 | else if (comp_ptr_ttypes (TREE_TYPE (to), TREE_TYPE (from))) | |
853 | conv = build_conv (QUAL_CONV, to, conv); | |
d11ad92e | 854 | else if (ptr_reasonably_similar (TREE_TYPE (to), TREE_TYPE (from))) |
c73964b2 | 855 | { |
d11ad92e MS |
856 | conv = build_conv (PTR_CONV, to, conv); |
857 | ICS_BAD_FLAG (conv) = 1; | |
c73964b2 | 858 | } |
d11ad92e MS |
859 | else |
860 | return 0; | |
861 | ||
862 | from = to; | |
c73964b2 MS |
863 | } |
864 | else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from)) | |
865 | { | |
866 | tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from)); | |
867 | tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to)); | |
868 | tree fbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn))); | |
869 | tree tbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn))); | |
870 | ||
871 | if (! DERIVED_FROM_P (fbase, tbase) | |
872 | || ! comptypes (TREE_TYPE (fromfn), TREE_TYPE (tofn), 1) | |
873 | || ! compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn)), | |
874 | TREE_CHAIN (TYPE_ARG_TYPES (tofn)), 1) | |
875 | || TYPE_READONLY (fbase) != TYPE_READONLY (tbase) | |
876 | || TYPE_VOLATILE (fbase) != TYPE_VOLATILE (tbase)) | |
877 | return 0; | |
878 | ||
879 | from = cp_build_type_variant (tbase, TYPE_READONLY (fbase), | |
880 | TYPE_VOLATILE (fbase)); | |
881 | from = build_cplus_method_type (from, TREE_TYPE (fromfn), | |
882 | TREE_CHAIN (TYPE_ARG_TYPES (fromfn))); | |
883 | from = build_ptrmemfunc_type (build_pointer_type (from)); | |
884 | conv = build_conv (PMEM_CONV, from, conv); | |
885 | } | |
886 | else if (tcode == BOOLEAN_TYPE) | |
887 | { | |
888 | if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE | |
889 | || fcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (from))) | |
890 | return 0; | |
891 | ||
892 | conv = build_conv (STD_CONV, to, conv); | |
a703fb38 KG |
893 | if (fcode == POINTER_TYPE |
894 | || (TYPE_PTRMEMFUNC_P (from) && ICS_STD_RANK (conv) < PBOOL_RANK)) | |
c73964b2 MS |
895 | ICS_STD_RANK (conv) = PBOOL_RANK; |
896 | } | |
897 | /* We don't check for ENUMERAL_TYPE here because there are no standard | |
898 | conversions to enum type. */ | |
899 | else if (tcode == INTEGER_TYPE || tcode == BOOLEAN_TYPE | |
900 | || tcode == REAL_TYPE) | |
901 | { | |
902 | if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE)) | |
903 | return 0; | |
904 | conv = build_conv (STD_CONV, to, conv); | |
905 | ||
906 | /* Give this a better rank if it's a promotion. */ | |
907 | if (to == type_promotes_to (from) | |
908 | && ICS_STD_RANK (TREE_OPERAND (conv, 0)) <= PROMO_RANK) | |
909 | ICS_STD_RANK (conv) = PROMO_RANK; | |
910 | } | |
911 | else if (IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from) | |
912 | && DERIVED_FROM_P (to, from)) | |
2dbfb418 JM |
913 | { |
914 | if (TREE_CODE (conv) == RVALUE_CONV) | |
915 | conv = TREE_OPERAND (conv, 0); | |
916 | conv = build_conv (BASE_CONV, to, conv); | |
917 | } | |
c73964b2 MS |
918 | else |
919 | return 0; | |
920 | ||
921 | return conv; | |
922 | } | |
923 | ||
c73964b2 MS |
924 | /* Returns the conversion path from type FROM to reference type TO for |
925 | purposes of reference binding. For lvalue binding, either pass a | |
926 | reference type to FROM or an lvalue expression to EXPR. | |
927 | ||
928 | Currently does not distinguish in the generated trees between binding to | |
929 | an lvalue and a temporary. Should it? */ | |
930 | ||
824b9a4c | 931 | static tree |
de22184b MS |
932 | reference_binding (rto, rfrom, expr, flags) |
933 | tree rto, rfrom, expr; | |
c73964b2 MS |
934 | int flags; |
935 | { | |
936 | tree conv; | |
937 | int lvalue = 1; | |
938 | tree to = TREE_TYPE (rto); | |
de22184b | 939 | tree from = rfrom; |
eb66be0e | 940 | int related; |
c73964b2 MS |
941 | |
942 | if (TREE_CODE (from) == REFERENCE_TYPE) | |
943 | from = TREE_TYPE (from); | |
944 | else if (! expr || ! real_lvalue_p (expr)) | |
945 | lvalue = 0; | |
946 | ||
3317aae9 MM |
947 | related = (comptypes (TYPE_MAIN_VARIANT (to), |
948 | TYPE_MAIN_VARIANT (from), 1) | |
eb66be0e MS |
949 | || (IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from) |
950 | && DERIVED_FROM_P (to, from))); | |
951 | ||
952 | if (lvalue && related | |
c73964b2 MS |
953 | && TYPE_READONLY (to) >= TYPE_READONLY (from) |
954 | && TYPE_VOLATILE (to) >= TYPE_VOLATILE (from)) | |
955 | { | |
956 | conv = build1 (IDENTITY_CONV, from, expr); | |
957 | ||
3317aae9 MM |
958 | if (comptypes (TYPE_MAIN_VARIANT (to), |
959 | TYPE_MAIN_VARIANT (from), 1)) | |
c73964b2 | 960 | conv = build_conv (REF_BIND, rto, conv); |
eb66be0e | 961 | else |
c73964b2 MS |
962 | { |
963 | conv = build_conv (REF_BIND, rto, conv); | |
964 | ICS_STD_RANK (conv) = STD_RANK; | |
965 | } | |
c73964b2 MS |
966 | } |
967 | else | |
968 | conv = NULL_TREE; | |
969 | ||
faf5394a | 970 | if (! conv) |
c73964b2 | 971 | { |
de22184b | 972 | conv = standard_conversion (to, rfrom, expr); |
c73964b2 MS |
973 | if (conv) |
974 | { | |
975 | conv = build_conv (REF_BIND, rto, conv); | |
976 | ||
de22184b MS |
977 | /* Bind directly to a base subobject of a class rvalue. Do it |
978 | after building the conversion for proper handling of ICS_RANK. */ | |
c73964b2 MS |
979 | if (TREE_CODE (TREE_OPERAND (conv, 0)) == BASE_CONV) |
980 | TREE_OPERAND (conv, 0) = TREE_OPERAND (TREE_OPERAND (conv, 0), 0); | |
981 | } | |
eb66be0e MS |
982 | if (conv |
983 | && ((! (TYPE_READONLY (to) && ! TYPE_VOLATILE (to) | |
984 | && (flags & LOOKUP_NO_TEMP_BIND) == 0)) | |
985 | /* If T1 is reference-related to T2, cv1 must be the same | |
986 | cv-qualification as, or greater cv-qualification than, | |
987 | cv2; otherwise, the program is ill-formed. */ | |
988 | || (related | |
989 | && (TYPE_READONLY (to) < TYPE_READONLY (from) | |
990 | || TYPE_VOLATILE (to) < TYPE_VOLATILE (from))))) | |
d11ad92e | 991 | ICS_BAD_FLAG (conv) = 1; |
faf5394a | 992 | } |
d11ad92e | 993 | |
c73964b2 MS |
994 | return conv; |
995 | } | |
996 | ||
997 | /* Returns the implicit conversion sequence (see [over.ics]) from type FROM | |
998 | to type TO. The optional expression EXPR may affect the conversion. | |
999 | FLAGS are the usual overloading flags. Only LOOKUP_NO_CONVERSION is | |
1000 | significant. */ | |
1001 | ||
824b9a4c | 1002 | static tree |
c73964b2 MS |
1003 | implicit_conversion (to, from, expr, flags) |
1004 | tree to, from, expr; | |
1005 | int flags; | |
1006 | { | |
1007 | tree conv; | |
1008 | struct z_candidate *cand; | |
1009 | ||
1010 | if (expr && type_unknown_p (expr)) | |
1011 | { | |
1012 | expr = instantiate_type (to, expr, 0); | |
1013 | if (expr == error_mark_node) | |
1014 | return 0; | |
1015 | from = TREE_TYPE (expr); | |
1016 | } | |
1017 | ||
1018 | if (TREE_CODE (to) == REFERENCE_TYPE) | |
1019 | conv = reference_binding (to, from, expr, flags); | |
1020 | else | |
de22184b | 1021 | conv = standard_conversion (to, from, expr); |
c73964b2 MS |
1022 | |
1023 | if (conv) | |
de22184b | 1024 | ; |
3042d5be MM |
1025 | else if (expr != NULL_TREE |
1026 | && (IS_AGGR_TYPE (non_reference (from)) | |
c73964b2 MS |
1027 | || IS_AGGR_TYPE (non_reference (to))) |
1028 | && (flags & LOOKUP_NO_CONVERSION) == 0) | |
1029 | { | |
eb66be0e MS |
1030 | cand = build_user_type_conversion_1 |
1031 | (to, expr, LOOKUP_ONLYCONVERTING); | |
1032 | if (cand) | |
1033 | conv = cand->second_conv; | |
1034 | if ((! conv || ICS_BAD_FLAG (conv)) | |
1035 | && TREE_CODE (to) == REFERENCE_TYPE | |
c73964b2 MS |
1036 | && (flags & LOOKUP_NO_TEMP_BIND) == 0) |
1037 | { | |
1038 | cand = build_user_type_conversion_1 | |
1039 | (TYPE_MAIN_VARIANT (TREE_TYPE (to)), expr, LOOKUP_ONLYCONVERTING); | |
1040 | if (cand) | |
42976354 BK |
1041 | { |
1042 | if (! TYPE_READONLY (TREE_TYPE (to)) | |
1043 | || TYPE_VOLATILE (TREE_TYPE (to))) | |
1044 | ICS_BAD_FLAG (cand->second_conv) = 1; | |
1045 | if (!conv || (ICS_BAD_FLAG (conv) | |
1046 | > ICS_BAD_FLAG (cand->second_conv))) | |
1047 | conv = build_conv (REF_BIND, to, cand->second_conv); | |
1048 | } | |
c73964b2 | 1049 | } |
c73964b2 MS |
1050 | } |
1051 | ||
1052 | return conv; | |
1053 | } | |
1054 | ||
5ffe581d JM |
1055 | /* Add a new entry to the list of candidates. Used by the add_*_candidate |
1056 | functions. */ | |
1057 | ||
1058 | static struct z_candidate * | |
1059 | add_candidate (candidates, fn, convs, viable) | |
1060 | struct z_candidate *candidates; | |
1061 | tree fn, convs; | |
1062 | int viable; | |
1063 | { | |
1064 | struct z_candidate *cand | |
1065 | = (struct z_candidate *) scratchalloc (sizeof (struct z_candidate)); | |
1066 | ||
1067 | cand->fn = fn; | |
1068 | cand->convs = convs; | |
1069 | cand->second_conv = NULL_TREE; | |
1070 | cand->viable = viable; | |
1071 | cand->basetype_path = NULL_TREE; | |
1072 | cand->template = NULL_TREE; | |
1073 | cand->warnings = NULL_TREE; | |
1074 | cand->next = candidates; | |
1075 | ||
1076 | return cand; | |
1077 | } | |
1078 | ||
c73964b2 MS |
1079 | /* Create an overload candidate for the function or method FN called with |
1080 | the argument list ARGLIST and add it to CANDIDATES. FLAGS is passed on | |
1081 | to implicit_conversion. */ | |
1082 | ||
1083 | static struct z_candidate * | |
1084 | add_function_candidate (candidates, fn, arglist, flags) | |
1085 | struct z_candidate *candidates; | |
1086 | tree fn, arglist; | |
1087 | int flags; | |
1088 | { | |
1089 | tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn)); | |
1090 | int i, len; | |
1091 | tree convs; | |
1092 | tree parmnode = parmlist; | |
1093 | tree argnode = arglist; | |
1094 | int viable = 1; | |
1095 | struct z_candidate *cand; | |
1096 | ||
1097 | /* The `this' and `in_chrg' arguments to constructors are not considered | |
1098 | in overload resolution. */ | |
1099 | if (DECL_CONSTRUCTOR_P (fn)) | |
1100 | { | |
1101 | parmnode = TREE_CHAIN (parmnode); | |
1102 | argnode = TREE_CHAIN (argnode); | |
1103 | if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn))) | |
1104 | { | |
1105 | parmnode = TREE_CHAIN (parmnode); | |
1106 | argnode = TREE_CHAIN (argnode); | |
1107 | } | |
1108 | } | |
1109 | ||
1110 | len = list_length (argnode); | |
e66d884e | 1111 | convs = make_scratch_vec (len); |
c73964b2 MS |
1112 | |
1113 | for (i = 0; i < len; ++i) | |
1114 | { | |
1115 | tree arg = TREE_VALUE (argnode); | |
1116 | tree argtype = TREE_TYPE (arg); | |
1117 | tree t; | |
1118 | ||
1119 | argtype = cp_build_type_variant | |
1120 | (argtype, TREE_READONLY (arg), TREE_THIS_VOLATILE (arg)); | |
1121 | ||
1122 | if (parmnode == void_list_node) | |
1123 | break; | |
1124 | else if (parmnode) | |
1125 | t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags); | |
1126 | else | |
1127 | { | |
1128 | t = build1 (IDENTITY_CONV, argtype, arg); | |
1129 | ICS_ELLIPSIS_FLAG (t) = 1; | |
1130 | } | |
1131 | ||
d11ad92e MS |
1132 | if (i == 0 && t && TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE |
1133 | && ! DECL_CONSTRUCTOR_P (fn)) | |
1134 | ICS_THIS_FLAG (t) = 1; | |
1135 | ||
c73964b2 MS |
1136 | TREE_VEC_ELT (convs, i) = t; |
1137 | if (! t) | |
1138 | break; | |
1139 | ||
d11ad92e MS |
1140 | if (ICS_BAD_FLAG (t)) |
1141 | viable = -1; | |
1142 | ||
c73964b2 MS |
1143 | if (parmnode) |
1144 | parmnode = TREE_CHAIN (parmnode); | |
1145 | argnode = TREE_CHAIN (argnode); | |
1146 | } | |
1147 | ||
1148 | if (i < len) | |
1149 | viable = 0; | |
1150 | ||
1151 | /* Make sure there are default args for the rest of the parms. */ | |
1152 | for (; parmnode && parmnode != void_list_node; | |
1153 | parmnode = TREE_CHAIN (parmnode)) | |
1154 | if (! TREE_PURPOSE (parmnode)) | |
1155 | { | |
1156 | viable = 0; | |
1157 | break; | |
1158 | } | |
1159 | ||
5ffe581d | 1160 | return add_candidate (candidates, fn, convs, viable); |
c73964b2 MS |
1161 | } |
1162 | ||
1163 | /* Create an overload candidate for the conversion function FN which will | |
1164 | be invoked for expression OBJ, producing a pointer-to-function which | |
1165 | will in turn be called with the argument list ARGLIST, and add it to | |
1166 | CANDIDATES. FLAGS is passed on to implicit_conversion. */ | |
1167 | ||
1168 | static struct z_candidate * | |
1169 | add_conv_candidate (candidates, fn, obj, arglist) | |
1170 | struct z_candidate *candidates; | |
1171 | tree fn, obj, arglist; | |
1172 | { | |
1173 | tree totype = TREE_TYPE (TREE_TYPE (fn)); | |
1174 | tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (totype)); | |
1175 | int i, len = list_length (arglist) + 1; | |
e66d884e | 1176 | tree convs = make_scratch_vec (len); |
c73964b2 MS |
1177 | tree parmnode = parmlist; |
1178 | tree argnode = arglist; | |
1179 | int viable = 1; | |
1180 | struct z_candidate *cand; | |
1181 | int flags = LOOKUP_NORMAL; | |
1182 | ||
1183 | for (i = 0; i < len; ++i) | |
1184 | { | |
1185 | tree arg = i == 0 ? obj : TREE_VALUE (argnode); | |
d11ad92e | 1186 | tree argtype = lvalue_type (arg); |
c73964b2 MS |
1187 | tree t; |
1188 | ||
c73964b2 MS |
1189 | if (i == 0) |
1190 | t = implicit_conversion (totype, argtype, arg, flags); | |
1191 | else if (parmnode == void_list_node) | |
1192 | break; | |
1193 | else if (parmnode) | |
1194 | t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags); | |
1195 | else | |
1196 | { | |
1197 | t = build1 (IDENTITY_CONV, argtype, arg); | |
1198 | ICS_ELLIPSIS_FLAG (t) = 1; | |
1199 | } | |
1200 | ||
1201 | TREE_VEC_ELT (convs, i) = t; | |
1202 | if (! t) | |
1203 | break; | |
1204 | ||
d11ad92e MS |
1205 | if (ICS_BAD_FLAG (t)) |
1206 | viable = -1; | |
1207 | ||
c73964b2 MS |
1208 | if (i == 0) |
1209 | continue; | |
1210 | ||
1211 | if (parmnode) | |
1212 | parmnode = TREE_CHAIN (parmnode); | |
1213 | argnode = TREE_CHAIN (argnode); | |
1214 | } | |
1215 | ||
1216 | if (i < len) | |
1217 | viable = 0; | |
1218 | ||
1219 | for (; parmnode && parmnode != void_list_node; | |
1220 | parmnode = TREE_CHAIN (parmnode)) | |
1221 | if (! TREE_PURPOSE (parmnode)) | |
1222 | { | |
1223 | viable = 0; | |
1224 | break; | |
1225 | } | |
1226 | ||
5ffe581d | 1227 | return add_candidate (candidates, fn, convs, viable); |
c73964b2 MS |
1228 | } |
1229 | ||
c73964b2 MS |
1230 | static struct z_candidate * |
1231 | build_builtin_candidate (candidates, fnname, type1, type2, | |
1232 | args, argtypes, flags) | |
1233 | struct z_candidate *candidates; | |
1234 | tree fnname, type1, type2, *args, *argtypes; | |
1235 | int flags; | |
1236 | ||
1237 | { | |
1238 | tree t, convs; | |
1239 | int viable = 1, i; | |
1240 | struct z_candidate *cand; | |
1241 | tree types[2]; | |
1242 | ||
1243 | types[0] = type1; | |
1244 | types[1] = type2; | |
1245 | ||
e66d884e | 1246 | convs = make_scratch_vec (args[2] ? 3 : (args[1] ? 2 : 1)); |
c73964b2 MS |
1247 | |
1248 | for (i = 0; i < 2; ++i) | |
1249 | { | |
1250 | if (! args[i]) | |
1251 | break; | |
1252 | ||
1253 | t = implicit_conversion (types[i], argtypes[i], args[i], flags); | |
1254 | if (! t) | |
1255 | { | |
1256 | viable = 0; | |
1257 | /* We need something for printing the candidate. */ | |
1258 | t = build1 (IDENTITY_CONV, types[i], NULL_TREE); | |
1259 | } | |
d11ad92e MS |
1260 | else if (ICS_BAD_FLAG (t)) |
1261 | viable = 0; | |
c73964b2 MS |
1262 | TREE_VEC_ELT (convs, i) = t; |
1263 | } | |
1264 | ||
1265 | /* For COND_EXPR we rearranged the arguments; undo that now. */ | |
1266 | if (args[2]) | |
1267 | { | |
1268 | TREE_VEC_ELT (convs, 2) = TREE_VEC_ELT (convs, 1); | |
1269 | TREE_VEC_ELT (convs, 1) = TREE_VEC_ELT (convs, 0); | |
1270 | t = implicit_conversion (boolean_type_node, argtypes[2], args[2], flags); | |
1271 | if (t) | |
1272 | TREE_VEC_ELT (convs, 0) = t; | |
1273 | else | |
1274 | viable = 0; | |
1275 | } | |
1276 | ||
5ffe581d | 1277 | return add_candidate (candidates, fnname, convs, viable); |
c73964b2 MS |
1278 | } |
1279 | ||
824b9a4c | 1280 | static int |
c73964b2 MS |
1281 | is_complete (t) |
1282 | tree t; | |
1283 | { | |
1284 | return TYPE_SIZE (complete_type (t)) != NULL_TREE; | |
1285 | } | |
1286 | ||
1287 | /* Create any builtin operator overload candidates for the operator in | |
1288 | question given the converted operand types TYPE1 and TYPE2. The other | |
1289 | args are passed through from add_builtin_candidates to | |
1290 | build_builtin_candidate. */ | |
1291 | ||
1292 | static struct z_candidate * | |
1293 | add_builtin_candidate (candidates, code, code2, fnname, type1, type2, | |
1294 | args, argtypes, flags) | |
1295 | struct z_candidate *candidates; | |
1296 | enum tree_code code, code2; | |
1297 | tree fnname, type1, type2, *args, *argtypes; | |
1298 | int flags; | |
1299 | { | |
1300 | switch (code) | |
1301 | { | |
1302 | case POSTINCREMENT_EXPR: | |
1303 | case POSTDECREMENT_EXPR: | |
1304 | args[1] = integer_zero_node; | |
1305 | type2 = integer_type_node; | |
7f85441b KG |
1306 | break; |
1307 | default: | |
1308 | break; | |
c73964b2 MS |
1309 | } |
1310 | ||
1311 | switch (code) | |
1312 | { | |
1313 | ||
1314 | /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type, | |
1315 | and VQ is either volatile or empty, there exist candidate operator | |
1316 | functions of the form | |
1317 | VQ T& operator++(VQ T&); | |
1318 | T operator++(VQ T&, int); | |
1319 | 5 For every pair T, VQ), where T is an enumeration type or an arithmetic | |
1320 | type other than bool, and VQ is either volatile or empty, there exist | |
1321 | candidate operator functions of the form | |
1322 | VQ T& operator--(VQ T&); | |
1323 | T operator--(VQ T&, int); | |
1324 | 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified | |
1325 | complete object type, and VQ is either volatile or empty, there exist | |
1326 | candidate operator functions of the form | |
1327 | T*VQ& operator++(T*VQ&); | |
1328 | T*VQ& operator--(T*VQ&); | |
1329 | T* operator++(T*VQ&, int); | |
1330 | T* operator--(T*VQ&, int); */ | |
1331 | ||
1332 | case POSTDECREMENT_EXPR: | |
1333 | case PREDECREMENT_EXPR: | |
1334 | if (TREE_CODE (type1) == BOOLEAN_TYPE) | |
1335 | return candidates; | |
1336 | case POSTINCREMENT_EXPR: | |
1337 | case PREINCREMENT_EXPR: | |
1338 | if ((ARITHMETIC_TYPE_P (type1) && TREE_CODE (type1) != ENUMERAL_TYPE) | |
c11b6f21 | 1339 | || TYPE_PTROB_P (type1)) |
c73964b2 MS |
1340 | { |
1341 | type1 = build_reference_type (type1); | |
1342 | break; | |
1343 | } | |
1344 | return candidates; | |
1345 | ||
1346 | /* 7 For every cv-qualified or cv-unqualified complete object type T, there | |
1347 | exist candidate operator functions of the form | |
1348 | ||
1349 | T& operator*(T*); | |
1350 | ||
1351 | 8 For every function type T, there exist candidate operator functions of | |
1352 | the form | |
1353 | T& operator*(T*); */ | |
1354 | ||
1355 | case INDIRECT_REF: | |
1356 | if (TREE_CODE (type1) == POINTER_TYPE | |
c11b6f21 | 1357 | && (TYPE_PTROB_P (type1) |
c73964b2 MS |
1358 | || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)) |
1359 | break; | |
1360 | return candidates; | |
1361 | ||
1362 | /* 9 For every type T, there exist candidate operator functions of the form | |
1363 | T* operator+(T*); | |
1364 | ||
1365 | 10For every promoted arithmetic type T, there exist candidate operator | |
1366 | functions of the form | |
1367 | T operator+(T); | |
1368 | T operator-(T); */ | |
1369 | ||
1370 | case CONVERT_EXPR: /* unary + */ | |
1371 | if (TREE_CODE (type1) == POINTER_TYPE | |
1372 | && TREE_CODE (TREE_TYPE (type1)) != OFFSET_TYPE) | |
1373 | break; | |
1374 | case NEGATE_EXPR: | |
1375 | if (ARITHMETIC_TYPE_P (type1)) | |
1376 | break; | |
1377 | return candidates; | |
1378 | ||
1379 | /* 11For every promoted integral type T, there exist candidate operator | |
1380 | functions of the form | |
1381 | T operator~(T); */ | |
1382 | ||
1383 | case BIT_NOT_EXPR: | |
1384 | if (INTEGRAL_TYPE_P (type1)) | |
1385 | break; | |
1386 | return candidates; | |
1387 | ||
1388 | /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1 | |
1389 | is the same type as C2 or is a derived class of C2, T is a complete | |
1390 | object type or a function type, and CV1 and CV2 are cv-qualifier-seqs, | |
1391 | there exist candidate operator functions of the form | |
1392 | CV12 T& operator->*(CV1 C1*, CV2 T C2::*); | |
1393 | where CV12 is the union of CV1 and CV2. */ | |
1394 | ||
1395 | case MEMBER_REF: | |
1396 | if (TREE_CODE (type1) == POINTER_TYPE | |
1397 | && (TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2))) | |
1398 | { | |
1399 | tree c1 = TREE_TYPE (type1); | |
1400 | tree c2 = (TYPE_PTRMEMFUNC_P (type2) | |
8e69329a | 1401 | ? TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type2))) |
c73964b2 MS |
1402 | : TYPE_OFFSET_BASETYPE (TREE_TYPE (type2))); |
1403 | ||
1404 | if (IS_AGGR_TYPE (c1) && DERIVED_FROM_P (c2, c1) | |
1405 | && (TYPE_PTRMEMFUNC_P (type2) | |
1406 | || is_complete (TREE_TYPE (TREE_TYPE (type2))))) | |
1407 | break; | |
1408 | } | |
1409 | return candidates; | |
1410 | ||
1411 | /* 13For every pair of promoted arithmetic types L and R, there exist can- | |
1412 | didate operator functions of the form | |
1413 | LR operator*(L, R); | |
1414 | LR operator/(L, R); | |
1415 | LR operator+(L, R); | |
1416 | LR operator-(L, R); | |
1417 | bool operator<(L, R); | |
1418 | bool operator>(L, R); | |
1419 | bool operator<=(L, R); | |
1420 | bool operator>=(L, R); | |
1421 | bool operator==(L, R); | |
1422 | bool operator!=(L, R); | |
1423 | where LR is the result of the usual arithmetic conversions between | |
1424 | types L and R. | |
1425 | ||
1426 | 14For every pair of types T and I, where T is a cv-qualified or cv- | |
1427 | unqualified complete object type and I is a promoted integral type, | |
1428 | there exist candidate operator functions of the form | |
1429 | T* operator+(T*, I); | |
1430 | T& operator[](T*, I); | |
1431 | T* operator-(T*, I); | |
1432 | T* operator+(I, T*); | |
1433 | T& operator[](I, T*); | |
1434 | ||
1435 | 15For every T, where T is a pointer to complete object type, there exist | |
1436 | candidate operator functions of the form112) | |
1437 | ptrdiff_t operator-(T, T); | |
1438 | ||
1439 | 16For every pointer type T, there exist candidate operator functions of | |
1440 | the form | |
1441 | bool operator<(T, T); | |
1442 | bool operator>(T, T); | |
1443 | bool operator<=(T, T); | |
1444 | bool operator>=(T, T); | |
1445 | bool operator==(T, T); | |
1446 | bool operator!=(T, T); | |
1447 | ||
1448 | 17For every pointer to member type T, there exist candidate operator | |
1449 | functions of the form | |
1450 | bool operator==(T, T); | |
1451 | bool operator!=(T, T); */ | |
1452 | ||
1453 | case MINUS_EXPR: | |
c11b6f21 | 1454 | if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2)) |
c73964b2 | 1455 | break; |
c11b6f21 | 1456 | if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2)) |
c73964b2 MS |
1457 | { |
1458 | type2 = ptrdiff_type_node; | |
1459 | break; | |
1460 | } | |
1461 | case MULT_EXPR: | |
1462 | case TRUNC_DIV_EXPR: | |
1463 | if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2)) | |
1464 | break; | |
1465 | return candidates; | |
1466 | ||
1467 | case EQ_EXPR: | |
1468 | case NE_EXPR: | |
a703fb38 KG |
1469 | if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2)) |
1470 | || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))) | |
c73964b2 MS |
1471 | break; |
1472 | if ((TYPE_PTRMEMFUNC_P (type1) || TYPE_PTRMEM_P (type1)) | |
1473 | && null_ptr_cst_p (args[1])) | |
1474 | { | |
1475 | type2 = type1; | |
1476 | break; | |
1477 | } | |
1478 | if ((TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2)) | |
1479 | && null_ptr_cst_p (args[0])) | |
1480 | { | |
1481 | type1 = type2; | |
1482 | break; | |
1483 | } | |
1484 | case LT_EXPR: | |
1485 | case GT_EXPR: | |
1486 | case LE_EXPR: | |
1487 | case GE_EXPR: | |
1488 | case MAX_EXPR: | |
1489 | case MIN_EXPR: | |
a703fb38 KG |
1490 | if ((ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2)) |
1491 | || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))) | |
c73964b2 MS |
1492 | break; |
1493 | if (TYPE_PTR_P (type1) && null_ptr_cst_p (args[1])) | |
1494 | { | |
1495 | type2 = type1; | |
1496 | break; | |
1497 | } | |
1498 | if (null_ptr_cst_p (args[0]) && TYPE_PTR_P (type2)) | |
1499 | { | |
1500 | type1 = type2; | |
1501 | break; | |
1502 | } | |
1503 | return candidates; | |
1504 | ||
1505 | case PLUS_EXPR: | |
1506 | if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2)) | |
1507 | break; | |
1508 | case ARRAY_REF: | |
c11b6f21 | 1509 | if (INTEGRAL_TYPE_P (type1) && TYPE_PTROB_P (type2)) |
c73964b2 MS |
1510 | { |
1511 | type1 = ptrdiff_type_node; | |
1512 | break; | |
1513 | } | |
c11b6f21 | 1514 | if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2)) |
c73964b2 MS |
1515 | { |
1516 | type2 = ptrdiff_type_node; | |
1517 | break; | |
1518 | } | |
1519 | return candidates; | |
1520 | ||
1521 | /* 18For every pair of promoted integral types L and R, there exist candi- | |
1522 | date operator functions of the form | |
1523 | LR operator%(L, R); | |
1524 | LR operator&(L, R); | |
1525 | LR operator^(L, R); | |
1526 | LR operator|(L, R); | |
1527 | L operator<<(L, R); | |
1528 | L operator>>(L, R); | |
1529 | where LR is the result of the usual arithmetic conversions between | |
1530 | types L and R. */ | |
1531 | ||
1532 | case TRUNC_MOD_EXPR: | |
1533 | case BIT_AND_EXPR: | |
1534 | case BIT_IOR_EXPR: | |
1535 | case BIT_XOR_EXPR: | |
1536 | case LSHIFT_EXPR: | |
1537 | case RSHIFT_EXPR: | |
1538 | if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2)) | |
1539 | break; | |
1540 | return candidates; | |
1541 | ||
1542 | /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration | |
1543 | type, VQ is either volatile or empty, and R is a promoted arithmetic | |
1544 | type, there exist candidate operator functions of the form | |
1545 | VQ L& operator=(VQ L&, R); | |
1546 | VQ L& operator*=(VQ L&, R); | |
1547 | VQ L& operator/=(VQ L&, R); | |
1548 | VQ L& operator+=(VQ L&, R); | |
1549 | VQ L& operator-=(VQ L&, R); | |
1550 | ||
1551 | 20For every pair T, VQ), where T is any type and VQ is either volatile | |
1552 | or empty, there exist candidate operator functions of the form | |
1553 | T*VQ& operator=(T*VQ&, T*); | |
1554 | ||
1555 | 21For every pair T, VQ), where T is a pointer to member type and VQ is | |
1556 | either volatile or empty, there exist candidate operator functions of | |
1557 | the form | |
1558 | VQ T& operator=(VQ T&, T); | |
1559 | ||
1560 | 22For every triple T, VQ, I), where T is a cv-qualified or cv- | |
1561 | unqualified complete object type, VQ is either volatile or empty, and | |
1562 | I is a promoted integral type, there exist candidate operator func- | |
1563 | tions of the form | |
1564 | T*VQ& operator+=(T*VQ&, I); | |
1565 | T*VQ& operator-=(T*VQ&, I); | |
1566 | ||
1567 | 23For every triple L, VQ, R), where L is an integral or enumeration | |
1568 | type, VQ is either volatile or empty, and R is a promoted integral | |
1569 | type, there exist candidate operator functions of the form | |
1570 | ||
1571 | VQ L& operator%=(VQ L&, R); | |
1572 | VQ L& operator<<=(VQ L&, R); | |
1573 | VQ L& operator>>=(VQ L&, R); | |
1574 | VQ L& operator&=(VQ L&, R); | |
1575 | VQ L& operator^=(VQ L&, R); | |
1576 | VQ L& operator|=(VQ L&, R); */ | |
1577 | ||
1578 | case MODIFY_EXPR: | |
1579 | switch (code2) | |
1580 | { | |
1581 | case PLUS_EXPR: | |
1582 | case MINUS_EXPR: | |
c11b6f21 | 1583 | if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2)) |
c73964b2 MS |
1584 | { |
1585 | type2 = ptrdiff_type_node; | |
1586 | break; | |
1587 | } | |
1588 | case MULT_EXPR: | |
1589 | case TRUNC_DIV_EXPR: | |
1590 | if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2)) | |
1591 | break; | |
1592 | return candidates; | |
1593 | ||
1594 | case TRUNC_MOD_EXPR: | |
1595 | case BIT_AND_EXPR: | |
1596 | case BIT_IOR_EXPR: | |
1597 | case BIT_XOR_EXPR: | |
1598 | case LSHIFT_EXPR: | |
1599 | case RSHIFT_EXPR: | |
1600 | if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2)) | |
1601 | break; | |
1602 | return candidates; | |
1603 | ||
1604 | case NOP_EXPR: | |
1605 | if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2)) | |
1606 | break; | |
1607 | if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2)) | |
1608 | || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2)) | |
1609 | || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2)) | |
1610 | || ((TYPE_PTRMEMFUNC_P (type1) | |
1611 | || TREE_CODE (type1) == POINTER_TYPE) | |
1612 | && null_ptr_cst_p (args[1]))) | |
1613 | { | |
1614 | type2 = type1; | |
1615 | break; | |
1616 | } | |
1617 | return candidates; | |
1618 | ||
1619 | default: | |
1620 | my_friendly_abort (367); | |
1621 | } | |
1622 | type1 = build_reference_type (type1); | |
1623 | break; | |
1624 | ||
1625 | case COND_EXPR: | |
1626 | /* Kludge around broken overloading rules whereby | |
de22184b MS |
1627 | bool ? const char& : enum is ambiguous |
1628 | (between int and const char&). */ | |
c73964b2 | 1629 | flags |= LOOKUP_NO_TEMP_BIND; |
de22184b MS |
1630 | |
1631 | /* Extension: Support ?: of enumeral type. Hopefully this will not | |
1632 | be an extension for long. */ | |
c73964b2 MS |
1633 | if (TREE_CODE (type1) == ENUMERAL_TYPE && type1 == type2) |
1634 | break; | |
1635 | else if (TREE_CODE (type1) == ENUMERAL_TYPE | |
1636 | || TREE_CODE (type2) == ENUMERAL_TYPE) | |
1637 | return candidates; | |
1638 | if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2)) | |
1639 | break; | |
1640 | if (TREE_CODE (type1) == TREE_CODE (type2) | |
1641 | && (TREE_CODE (type1) == REFERENCE_TYPE | |
1642 | || TREE_CODE (type1) == POINTER_TYPE | |
1643 | || TYPE_PTRMEMFUNC_P (type1) | |
1644 | || IS_AGGR_TYPE (type1))) | |
1645 | break; | |
1646 | if (TREE_CODE (type1) == REFERENCE_TYPE | |
1647 | || TREE_CODE (type2) == REFERENCE_TYPE) | |
1648 | return candidates; | |
1649 | if (((TYPE_PTRMEMFUNC_P (type1) || TREE_CODE (type1) == POINTER_TYPE) | |
1650 | && null_ptr_cst_p (args[1])) | |
1651 | || IS_AGGR_TYPE (type1)) | |
1652 | { | |
1653 | type2 = type1; | |
1654 | break; | |
1655 | } | |
1656 | if (((TYPE_PTRMEMFUNC_P (type2) || TREE_CODE (type2) == POINTER_TYPE) | |
1657 | && null_ptr_cst_p (args[0])) | |
1658 | || IS_AGGR_TYPE (type2)) | |
1659 | { | |
1660 | type1 = type2; | |
1661 | break; | |
1662 | } | |
1663 | return candidates; | |
1664 | ||
1665 | default: | |
1666 | my_friendly_abort (367); | |
1667 | } | |
1668 | ||
1669 | /* If we're dealing with two pointer types, we need candidates | |
1670 | for both of them. */ | |
1671 | if (type2 && type1 != type2 | |
1672 | && TREE_CODE (type1) == TREE_CODE (type2) | |
1673 | && (TREE_CODE (type1) == REFERENCE_TYPE | |
1674 | || (TREE_CODE (type1) == POINTER_TYPE | |
1675 | && TYPE_PTRMEM_P (type1) == TYPE_PTRMEM_P (type2)) | |
1676 | || TYPE_PTRMEMFUNC_P (type1) | |
1677 | || IS_AGGR_TYPE (type1))) | |
1678 | { | |
1679 | candidates = build_builtin_candidate | |
1680 | (candidates, fnname, type1, type1, args, argtypes, flags); | |
1681 | return build_builtin_candidate | |
1682 | (candidates, fnname, type2, type2, args, argtypes, flags); | |
1683 | } | |
1684 | ||
1685 | return build_builtin_candidate | |
1686 | (candidates, fnname, type1, type2, args, argtypes, flags); | |
1687 | } | |
1688 | ||
1689 | tree | |
1690 | type_decays_to (type) | |
1691 | tree type; | |
1692 | { | |
1693 | if (TREE_CODE (type) == ARRAY_TYPE) | |
1694 | return build_pointer_type (TREE_TYPE (type)); | |
1695 | if (TREE_CODE (type) == FUNCTION_TYPE) | |
1696 | return build_pointer_type (type); | |
1697 | return type; | |
1698 | } | |
1699 | ||
1700 | /* There are three conditions of builtin candidates: | |
1701 | ||
1702 | 1) bool-taking candidates. These are the same regardless of the input. | |
1703 | 2) pointer-pair taking candidates. These are generated for each type | |
1704 | one of the input types converts to. | |
1705 | 3) arithmetic candidates. According to the WP, we should generate | |
1706 | all of these, but I'm trying not to... */ | |
1707 | ||
1708 | static struct z_candidate * | |
1709 | add_builtin_candidates (candidates, code, code2, fnname, args, flags) | |
1710 | struct z_candidate *candidates; | |
1711 | enum tree_code code, code2; | |
1712 | tree fnname, *args; | |
1713 | int flags; | |
1714 | { | |
1715 | int ref1, i; | |
1716 | tree type, argtypes[3], types[2]; | |
1717 | ||
1718 | for (i = 0; i < 3; ++i) | |
1719 | { | |
1720 | if (args[i]) | |
d11ad92e | 1721 | argtypes[i] = lvalue_type (args[i]); |
c73964b2 MS |
1722 | else |
1723 | argtypes[i] = NULL_TREE; | |
1724 | } | |
1725 | ||
1726 | switch (code) | |
1727 | { | |
1728 | /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type, | |
1729 | and VQ is either volatile or empty, there exist candidate operator | |
1730 | functions of the form | |
1731 | VQ T& operator++(VQ T&); */ | |
1732 | ||
1733 | case POSTINCREMENT_EXPR: | |
1734 | case PREINCREMENT_EXPR: | |
1735 | case POSTDECREMENT_EXPR: | |
1736 | case PREDECREMENT_EXPR: | |
1737 | case MODIFY_EXPR: | |
1738 | ref1 = 1; | |
1739 | break; | |
1740 | ||
1741 | /* 24There also exist candidate operator functions of the form | |
1742 | bool operator!(bool); | |
1743 | bool operator&&(bool, bool); | |
1744 | bool operator||(bool, bool); */ | |
1745 | ||
1746 | case TRUTH_NOT_EXPR: | |
1747 | return build_builtin_candidate | |
1748 | (candidates, fnname, boolean_type_node, | |
1749 | NULL_TREE, args, argtypes, flags); | |
1750 | ||
1751 | case TRUTH_ORIF_EXPR: | |
1752 | case TRUTH_ANDIF_EXPR: | |
1753 | return build_builtin_candidate | |
1754 | (candidates, fnname, boolean_type_node, | |
1755 | boolean_type_node, args, argtypes, flags); | |
1756 | ||
1757 | case ADDR_EXPR: | |
1758 | case COMPOUND_EXPR: | |
1759 | case COMPONENT_REF: | |
1760 | return candidates; | |
1761 | ||
1762 | default: | |
1763 | ref1 = 0; | |
1764 | } | |
1765 | ||
1766 | types[0] = types[1] = NULL_TREE; | |
1767 | ||
1768 | for (i = 0; i < 2; ++i) | |
1769 | { | |
1770 | if (! args[i]) | |
1771 | ; | |
1772 | else if (IS_AGGR_TYPE (argtypes[i])) | |
1773 | { | |
1774 | tree convs = lookup_conversions (argtypes[i]); | |
1775 | ||
1776 | if (code == COND_EXPR) | |
1777 | { | |
1778 | if (real_lvalue_p (args[i])) | |
e66d884e | 1779 | types[i] = scratch_tree_cons |
c73964b2 MS |
1780 | (NULL_TREE, build_reference_type (argtypes[i]), types[i]); |
1781 | ||
e66d884e | 1782 | types[i] = scratch_tree_cons |
c73964b2 MS |
1783 | (NULL_TREE, TYPE_MAIN_VARIANT (argtypes[i]), types[i]); |
1784 | } | |
1785 | ||
1786 | else if (! convs || (i == 0 && code == MODIFY_EXPR | |
1787 | && code2 == NOP_EXPR)) | |
1788 | return candidates; | |
1789 | ||
1790 | for (; convs; convs = TREE_CHAIN (convs)) | |
1791 | { | |
1792 | type = TREE_TYPE (TREE_TYPE (TREE_VALUE (convs))); | |
1793 | ||
1794 | if (i == 0 && ref1 | |
1795 | && (TREE_CODE (type) != REFERENCE_TYPE | |
1796 | || TYPE_READONLY (TREE_TYPE (type)))) | |
1797 | continue; | |
1798 | ||
1799 | if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE) | |
e66d884e | 1800 | types[i] = scratch_tree_cons (NULL_TREE, type, types[i]); |
c73964b2 MS |
1801 | |
1802 | type = non_reference (type); | |
1803 | if (i != 0 || ! ref1) | |
1804 | { | |
1805 | type = TYPE_MAIN_VARIANT (type_decays_to (type)); | |
1806 | if (code == COND_EXPR && TREE_CODE (type) == ENUMERAL_TYPE) | |
e66d884e | 1807 | types[i] = scratch_tree_cons (NULL_TREE, type, types[i]); |
c73964b2 MS |
1808 | if (INTEGRAL_TYPE_P (type)) |
1809 | type = type_promotes_to (type); | |
1810 | } | |
1811 | ||
1812 | if (! value_member (type, types[i])) | |
e66d884e | 1813 | types[i] = scratch_tree_cons (NULL_TREE, type, types[i]); |
c73964b2 MS |
1814 | } |
1815 | } | |
1816 | else | |
1817 | { | |
1818 | if (code == COND_EXPR && real_lvalue_p (args[i])) | |
e66d884e | 1819 | types[i] = scratch_tree_cons |
c73964b2 MS |
1820 | (NULL_TREE, build_reference_type (argtypes[i]), types[i]); |
1821 | type = non_reference (argtypes[i]); | |
1822 | if (i != 0 || ! ref1) | |
1823 | { | |
1824 | type = TYPE_MAIN_VARIANT (type_decays_to (type)); | |
1825 | if (code == COND_EXPR && TREE_CODE (type) == ENUMERAL_TYPE) | |
e66d884e | 1826 | types[i] = scratch_tree_cons (NULL_TREE, type, types[i]); |
c73964b2 MS |
1827 | if (INTEGRAL_TYPE_P (type)) |
1828 | type = type_promotes_to (type); | |
1829 | } | |
e66d884e | 1830 | types[i] = scratch_tree_cons (NULL_TREE, type, types[i]); |
c73964b2 MS |
1831 | } |
1832 | } | |
1833 | ||
1834 | for (; types[0]; types[0] = TREE_CHAIN (types[0])) | |
1835 | { | |
1836 | if (types[1]) | |
1837 | for (type = types[1]; type; type = TREE_CHAIN (type)) | |
1838 | candidates = add_builtin_candidate | |
1839 | (candidates, code, code2, fnname, TREE_VALUE (types[0]), | |
1840 | TREE_VALUE (type), args, argtypes, flags); | |
1841 | else | |
1842 | candidates = add_builtin_candidate | |
1843 | (candidates, code, code2, fnname, TREE_VALUE (types[0]), | |
1844 | NULL_TREE, args, argtypes, flags); | |
1845 | } | |
1846 | ||
1847 | return candidates; | |
1848 | } | |
1849 | ||
e1467ff2 | 1850 | |
386b8a85 JM |
1851 | /* If TMPL can be successfully instantiated as indicated by |
1852 | EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES. | |
1853 | ||
e1467ff2 MM |
1854 | TMPL is the template. EXPLICIT_TARGS are any explicit template |
1855 | arguments. ARGLIST is the arguments provided at the call-site. | |
1856 | The RETURN_TYPE is the desired type for conversion operators. If | |
1857 | OBJ is NULL_TREE, FLAGS are as for add_function_candidate. If an | |
1858 | OBJ is supplied, FLAGS are ignored, and OBJ is as for | |
1859 | add_conv_candidate. */ | |
1860 | ||
1861 | static struct z_candidate* | |
1862 | add_template_candidate_real (candidates, tmpl, explicit_targs, | |
1863 | arglist, return_type, flags, | |
1864 | obj) | |
c73964b2 | 1865 | struct z_candidate *candidates; |
386b8a85 | 1866 | tree tmpl, explicit_targs, arglist, return_type; |
c73964b2 | 1867 | int flags; |
e1467ff2 | 1868 | tree obj; |
c73964b2 | 1869 | { |
98c1c668 | 1870 | int ntparms = DECL_NTPARMS (tmpl); |
e66d884e | 1871 | tree targs = make_scratch_vec (ntparms); |
c73964b2 | 1872 | struct z_candidate *cand; |
98c1c668 | 1873 | int i; |
c73964b2 MS |
1874 | tree fn; |
1875 | ||
386b8a85 | 1876 | i = fn_type_unification (tmpl, explicit_targs, targs, arglist, |
d7684f2d | 1877 | return_type, 0, NULL_TREE); |
98c1c668 | 1878 | |
c73964b2 MS |
1879 | if (i != 0) |
1880 | return candidates; | |
1881 | ||
1882 | fn = instantiate_template (tmpl, targs); | |
1883 | if (fn == error_mark_node) | |
1884 | return candidates; | |
1885 | ||
e1467ff2 MM |
1886 | if (obj != NULL_TREE) |
1887 | /* Aha, this is a conversion function. */ | |
1888 | cand = add_conv_candidate (candidates, fn, obj, arglist); | |
1889 | else | |
1890 | cand = add_function_candidate (candidates, fn, arglist, flags); | |
1891 | if (DECL_TI_TEMPLATE (fn) != tmpl) | |
1892 | /* This situation can occur if a member template of a template | |
1893 | class is specialized. Then, instantiate_template might return | |
1894 | an instantiation of the specialization, in which case the | |
1895 | DECL_TI_TEMPLATE field will point at the original | |
1896 | specialization. For example: | |
1897 | ||
1898 | template <class T> struct S { template <class U> void f(U); | |
1899 | template <> void f(int) {}; }; | |
1900 | S<double> sd; | |
1901 | sd.f(3); | |
1902 | ||
1903 | Here, TMPL will be template <class U> S<double>::f(U). | |
1904 | And, instantiate template will give us the specialization | |
1905 | template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field | |
1906 | for this will point at template <class T> template <> S<T>::f(int), | |
1907 | so that we can find the definition. For the purposes of | |
1908 | overload resolution, however, we want the original TMPL. */ | |
1909 | cand->template = tree_cons (tmpl, targs, NULL_TREE); | |
1910 | else | |
1911 | cand->template = DECL_TEMPLATE_INFO (fn); | |
1912 | ||
c73964b2 MS |
1913 | return cand; |
1914 | } | |
1915 | ||
786b5245 MM |
1916 | |
1917 | static struct z_candidate * | |
e1467ff2 MM |
1918 | add_template_candidate (candidates, tmpl, explicit_targs, |
1919 | arglist, return_type, flags) | |
786b5245 | 1920 | struct z_candidate *candidates; |
e1467ff2 MM |
1921 | tree tmpl, explicit_targs, arglist, return_type; |
1922 | int flags; | |
786b5245 | 1923 | { |
e1467ff2 MM |
1924 | return |
1925 | add_template_candidate_real (candidates, tmpl, explicit_targs, | |
1926 | arglist, return_type, flags, NULL_TREE); | |
1927 | } | |
786b5245 | 1928 | |
786b5245 | 1929 | |
e1467ff2 MM |
1930 | static struct z_candidate * |
1931 | add_template_conv_candidate (candidates, tmpl, obj, arglist, return_type) | |
1932 | struct z_candidate *candidates; | |
1933 | tree tmpl, obj, arglist, return_type; | |
1934 | { | |
1935 | return | |
1936 | add_template_candidate_real (candidates, tmpl, NULL_TREE, arglist, | |
1937 | return_type, 0, obj); | |
786b5245 MM |
1938 | } |
1939 | ||
1940 | ||
c73964b2 MS |
1941 | static int |
1942 | any_viable (cands) | |
1943 | struct z_candidate *cands; | |
1944 | { | |
1945 | for (; cands; cands = cands->next) | |
d11ad92e | 1946 | if (pedantic ? cands->viable == 1 : cands->viable) |
c73964b2 MS |
1947 | return 1; |
1948 | return 0; | |
1949 | } | |
1950 | ||
1951 | static struct z_candidate * | |
1952 | splice_viable (cands) | |
1953 | struct z_candidate *cands; | |
1954 | { | |
1955 | struct z_candidate **p = &cands; | |
1956 | ||
1957 | for (; *p; ) | |
1958 | { | |
d11ad92e | 1959 | if (pedantic ? (*p)->viable == 1 : (*p)->viable) |
c73964b2 MS |
1960 | p = &((*p)->next); |
1961 | else | |
1962 | *p = (*p)->next; | |
1963 | } | |
1964 | ||
1965 | return cands; | |
1966 | } | |
1967 | ||
824b9a4c | 1968 | static tree |
c73964b2 MS |
1969 | build_this (obj) |
1970 | tree obj; | |
1971 | { | |
1972 | /* Fix this to work on non-lvalues. */ | |
c11b6f21 MS |
1973 | if (IS_SIGNATURE_POINTER (TREE_TYPE (obj)) |
1974 | || IS_SIGNATURE_REFERENCE (TREE_TYPE (obj))) | |
1975 | return obj; | |
1976 | else | |
1977 | return build_unary_op (ADDR_EXPR, obj, 0); | |
c73964b2 MS |
1978 | } |
1979 | ||
1980 | static void | |
1981 | print_z_candidates (candidates) | |
1982 | struct z_candidate *candidates; | |
1983 | { | |
73aad9b9 | 1984 | char *str = "candidates are:"; |
c73964b2 MS |
1985 | for (; candidates; candidates = candidates->next) |
1986 | { | |
1987 | if (TREE_CODE (candidates->fn) == IDENTIFIER_NODE) | |
1988 | { | |
1989 | if (candidates->fn == ansi_opname [COND_EXPR]) | |
73aad9b9 | 1990 | cp_error ("%s %D(%T, %T, %T) <builtin>", str, candidates->fn, |
c73964b2 MS |
1991 | TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)), |
1992 | TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)), | |
1993 | TREE_TYPE (TREE_VEC_ELT (candidates->convs, 2))); | |
1994 | else if (TREE_VEC_LENGTH (candidates->convs) == 2) | |
73aad9b9 | 1995 | cp_error ("%s %D(%T, %T) <builtin>", str, candidates->fn, |
c73964b2 MS |
1996 | TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)), |
1997 | TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1))); | |
1998 | else | |
73aad9b9 | 1999 | cp_error ("%s %D(%T) <builtin>", str, candidates->fn, |
c73964b2 MS |
2000 | TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0))); |
2001 | } | |
2002 | else | |
d11ad92e | 2003 | cp_error_at ("%s %+D%s", str, candidates->fn, |
faf5394a | 2004 | candidates->viable == -1 ? " <near match>" : ""); |
73aad9b9 | 2005 | str = " "; |
c73964b2 MS |
2006 | } |
2007 | } | |
2008 | ||
2009 | /* Returns the best overload candidate to perform the requested | |
eb66be0e MS |
2010 | conversion. This function is used for three the overloading situations |
2011 | described in [over.match.copy], [over.match.conv], and [over.match.ref]. | |
2012 | If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as | |
2013 | per [dcl.init.ref], so we ignore temporary bindings. */ | |
c73964b2 MS |
2014 | |
2015 | static struct z_candidate * | |
2016 | build_user_type_conversion_1 (totype, expr, flags) | |
2017 | tree totype, expr; | |
2018 | int flags; | |
2019 | { | |
2020 | struct z_candidate *candidates, *cand; | |
2021 | tree fromtype = TREE_TYPE (expr); | |
2022 | tree ctors = NULL_TREE, convs = NULL_TREE, *p; | |
a703fb38 | 2023 | tree args = NULL_TREE; |
98c1c668 | 2024 | tree templates = NULL_TREE; |
c73964b2 MS |
2025 | |
2026 | if (IS_AGGR_TYPE (totype)) | |
2027 | ctors = lookup_fnfields (TYPE_BINFO (totype), ctor_identifier, 0); | |
2028 | if (IS_AGGR_TYPE (fromtype) | |
2029 | && (! IS_AGGR_TYPE (totype) || ! DERIVED_FROM_P (totype, fromtype))) | |
2030 | convs = lookup_conversions (fromtype); | |
2031 | ||
2032 | candidates = 0; | |
2033 | flags |= LOOKUP_NO_CONVERSION; | |
2034 | ||
2035 | if (ctors) | |
2036 | { | |
2037 | tree t = build_int_2 (0, 0); | |
2038 | TREE_TYPE (t) = build_pointer_type (totype); | |
e66d884e | 2039 | args = build_scratch_list (NULL_TREE, expr); |
c73964b2 | 2040 | if (TYPE_USES_VIRTUAL_BASECLASSES (totype)) |
e66d884e JM |
2041 | args = scratch_tree_cons (NULL_TREE, integer_one_node, args); |
2042 | args = scratch_tree_cons (NULL_TREE, t, args); | |
c73964b2 MS |
2043 | |
2044 | ctors = TREE_VALUE (ctors); | |
2045 | } | |
2046 | for (; ctors; ctors = DECL_CHAIN (ctors)) | |
2047 | { | |
faf5394a | 2048 | if (DECL_NONCONVERTING_P (ctors)) |
c73964b2 MS |
2049 | continue; |
2050 | ||
98c1c668 JM |
2051 | if (TREE_CODE (ctors) == TEMPLATE_DECL) |
2052 | { | |
e66d884e | 2053 | templates = scratch_tree_cons (NULL_TREE, ctors, templates); |
98c1c668 JM |
2054 | candidates = |
2055 | add_template_candidate (candidates, ctors, | |
386b8a85 | 2056 | NULL_TREE, args, NULL_TREE, flags); |
98c1c668 JM |
2057 | } |
2058 | else | |
2059 | candidates = add_function_candidate (candidates, ctors, | |
2060 | args, flags); | |
2061 | ||
2062 | if (candidates) | |
2063 | { | |
2064 | candidates->second_conv = build1 (IDENTITY_CONV, totype, NULL_TREE); | |
2065 | candidates->basetype_path = TYPE_BINFO (totype); | |
2066 | } | |
c73964b2 MS |
2067 | } |
2068 | ||
2069 | if (convs) | |
e66d884e | 2070 | args = build_scratch_list (NULL_TREE, build_this (expr)); |
c73964b2 MS |
2071 | |
2072 | for (; convs; convs = TREE_CHAIN (convs)) | |
2073 | { | |
2074 | tree fn = TREE_VALUE (convs); | |
eb66be0e MS |
2075 | int convflags = LOOKUP_NO_CONVERSION; |
2076 | tree ics; | |
2077 | ||
2078 | /* If we are called to convert to a reference type, we are trying to | |
2079 | find an lvalue binding, so don't even consider temporaries. If | |
2080 | we don't find an lvalue binding, the caller will try again to | |
2081 | look for a temporary binding. */ | |
2082 | if (TREE_CODE (totype) == REFERENCE_TYPE) | |
2083 | convflags |= LOOKUP_NO_TEMP_BIND; | |
2084 | ||
75650646 MM |
2085 | if (TREE_CODE (fn) != TEMPLATE_DECL) |
2086 | ics = implicit_conversion | |
2087 | (totype, TREE_TYPE (TREE_TYPE (fn)), 0, convflags); | |
2088 | else | |
e1467ff2 MM |
2089 | /* Here, the template conversion operator result must |
2090 | precisely match the TOTYPE. (FIXME: Actually, we're | |
2091 | supposed to do some simple conversions here; see | |
2092 | [temp.deduct.conv].). If the result of the conversion | |
2093 | operator is not actually TOTYPE, then | |
2094 | add_template_candidate will fail below. */ | |
75650646 | 2095 | ics = implicit_conversion (totype, totype, 0, convflags); |
eb66be0e MS |
2096 | |
2097 | if (TREE_CODE (totype) == REFERENCE_TYPE && ics && ICS_BAD_FLAG (ics)) | |
2098 | /* ignore the near match. */; | |
2099 | else if (ics) | |
c73964b2 MS |
2100 | for (; fn; fn = DECL_CHAIN (fn)) |
2101 | { | |
98c1c668 JM |
2102 | if (TREE_CODE (fn) == TEMPLATE_DECL) |
2103 | { | |
e66d884e | 2104 | templates = scratch_tree_cons (NULL_TREE, fn, templates); |
98c1c668 | 2105 | candidates = |
386b8a85 JM |
2106 | add_template_candidate (candidates, fn, NULL_TREE, |
2107 | args, totype, flags); | |
98c1c668 JM |
2108 | } |
2109 | else | |
2110 | candidates = add_function_candidate (candidates, fn, | |
2111 | args, flags); | |
2112 | ||
2113 | if (candidates) | |
2114 | { | |
2115 | candidates->second_conv = ics; | |
2116 | candidates->basetype_path = TREE_PURPOSE (convs); | |
2117 | if (candidates->viable == 1 && ICS_BAD_FLAG (ics)) | |
2118 | candidates->viable = -1; | |
2119 | } | |
c73964b2 MS |
2120 | } |
2121 | } | |
2122 | ||
2123 | if (! any_viable (candidates)) | |
2124 | { | |
2125 | #if 0 | |
2126 | if (flags & LOOKUP_COMPLAIN) | |
2127 | { | |
2128 | if (candidates && ! candidates->next) | |
2129 | /* say why this one won't work or try to be loose */; | |
2130 | else | |
2131 | cp_error ("no viable candidates"); | |
2132 | } | |
2133 | #endif | |
2134 | ||
2135 | return 0; | |
2136 | } | |
2137 | ||
2138 | candidates = splice_viable (candidates); | |
da20811c | 2139 | cand = tourney (candidates); |
c73964b2 MS |
2140 | |
2141 | if (cand == 0) | |
2142 | { | |
2143 | if (flags & LOOKUP_COMPLAIN) | |
2144 | { | |
2145 | cp_error ("conversion from `%T' to `%T' is ambiguous", | |
2146 | fromtype, totype); | |
2147 | print_z_candidates (candidates); | |
2148 | } | |
2149 | ||
2150 | cand = candidates; /* any one will do */ | |
2151 | cand->second_conv = build1 (AMBIG_CONV, totype, expr); | |
2152 | ICS_USER_FLAG (cand->second_conv) = 1; | |
d2e5ee5c | 2153 | ICS_BAD_FLAG (cand->second_conv) = 1; |
c73964b2 MS |
2154 | |
2155 | return cand; | |
2156 | } | |
2157 | ||
2158 | for (p = &(cand->second_conv); TREE_CODE (*p) != IDENTITY_CONV; ) | |
2159 | p = &(TREE_OPERAND (*p, 0)); | |
2160 | ||
98c1c668 | 2161 | /* Pedantically, normal function declarations are never considered |
386b8a85 JM |
2162 | to refer to template instantiations, so we only do this with |
2163 | -fguiding-decls. */ | |
2164 | if (flag_guiding_decls && templates && ! cand->template | |
2165 | && !DECL_INITIAL (cand->fn) | |
98c1c668 JM |
2166 | && TREE_CODE (TREE_TYPE (cand->fn)) != METHOD_TYPE) |
2167 | add_maybe_template (cand->fn, templates); | |
2168 | ||
c73964b2 MS |
2169 | *p = build |
2170 | (USER_CONV, | |
2171 | (DECL_CONSTRUCTOR_P (cand->fn) | |
2172 | ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))), | |
5ffe581d | 2173 | expr, build_expr_ptr_wrapper (cand)); |
c73964b2 | 2174 | ICS_USER_FLAG (cand->second_conv) = 1; |
faf5394a MS |
2175 | if (cand->viable == -1) |
2176 | ICS_BAD_FLAG (cand->second_conv) = 1; | |
c73964b2 MS |
2177 | |
2178 | return cand; | |
2179 | } | |
2180 | ||
2181 | tree | |
2182 | build_user_type_conversion (totype, expr, flags) | |
faf5394a MS |
2183 | tree totype, expr; |
2184 | int flags; | |
c73964b2 MS |
2185 | { |
2186 | struct z_candidate *cand | |
2187 | = build_user_type_conversion_1 (totype, expr, flags); | |
2188 | ||
2189 | if (cand) | |
2190 | { | |
2191 | if (TREE_CODE (cand->second_conv) == AMBIG_CONV) | |
2192 | return error_mark_node; | |
2193 | return convert_from_reference (convert_like (cand->second_conv, expr)); | |
2194 | } | |
2195 | return NULL_TREE; | |
2196 | } | |
2197 | ||
86e6f22f JM |
2198 | /* Do any initial processing on the arguments to a function call. */ |
2199 | ||
2200 | static tree | |
2201 | resolve_args (args) | |
2202 | tree args; | |
2203 | { | |
2204 | tree t; | |
2205 | for (t = args; t; t = TREE_CHAIN (t)) | |
2206 | { | |
2207 | if (TREE_VALUE (t) == error_mark_node) | |
2208 | return error_mark_node; | |
2209 | else if (TREE_CODE (TREE_TYPE (TREE_VALUE (t))) == VOID_TYPE) | |
2210 | { | |
2211 | error ("invalid use of void expression"); | |
2212 | return error_mark_node; | |
2213 | } | |
2214 | else if (TREE_CODE (TREE_VALUE (t)) == OFFSET_REF) | |
2215 | TREE_VALUE (t) = resolve_offset_ref (TREE_VALUE (t)); | |
2216 | } | |
2217 | return args; | |
2218 | } | |
2219 | ||
c73964b2 | 2220 | tree |
9babbf20 JM |
2221 | build_new_function_call (fn, args) |
2222 | tree fn, args; | |
c73964b2 MS |
2223 | { |
2224 | struct z_candidate *candidates = 0, *cand; | |
386b8a85 | 2225 | tree explicit_targs = NULL_TREE; |
c32381b1 | 2226 | int template_only = 0; |
386b8a85 JM |
2227 | |
2228 | if (TREE_CODE (fn) == TEMPLATE_ID_EXPR) | |
2229 | { | |
2230 | explicit_targs = TREE_OPERAND (fn, 1); | |
2231 | fn = TREE_OPERAND (fn, 0); | |
c32381b1 | 2232 | template_only = 1; |
386b8a85 JM |
2233 | } |
2234 | ||
9babbf20 | 2235 | if (really_overloaded_fn (fn)) |
c73964b2 MS |
2236 | { |
2237 | tree t; | |
73aad9b9 | 2238 | tree templates = NULL_TREE; |
c73964b2 | 2239 | |
86e6f22f JM |
2240 | args = resolve_args (args); |
2241 | ||
2242 | if (args == error_mark_node) | |
2243 | return error_mark_node; | |
2244 | ||
c73964b2 MS |
2245 | for (t = TREE_VALUE (fn); t; t = DECL_CHAIN (t)) |
2246 | { | |
2247 | if (TREE_CODE (t) == TEMPLATE_DECL) | |
73aad9b9 | 2248 | { |
e66d884e | 2249 | templates = scratch_tree_cons (NULL_TREE, t, templates); |
73aad9b9 | 2250 | candidates = add_template_candidate |
386b8a85 JM |
2251 | (candidates, t, explicit_targs, args, NULL_TREE, |
2252 | LOOKUP_NORMAL); | |
73aad9b9 | 2253 | } |
c32381b1 | 2254 | else if (! template_only) |
c73964b2 MS |
2255 | candidates = add_function_candidate |
2256 | (candidates, t, args, LOOKUP_NORMAL); | |
2257 | } | |
2258 | ||
2259 | if (! any_viable (candidates)) | |
2260 | { | |
2261 | if (candidates && ! candidates->next) | |
2262 | return build_function_call (candidates->fn, args); | |
eb66be0e MS |
2263 | cp_error ("no matching function for call to `%D (%A)'", |
2264 | TREE_PURPOSE (fn), args); | |
2265 | if (candidates) | |
2266 | print_z_candidates (candidates); | |
c73964b2 MS |
2267 | return error_mark_node; |
2268 | } | |
2269 | candidates = splice_viable (candidates); | |
da20811c | 2270 | cand = tourney (candidates); |
c73964b2 MS |
2271 | |
2272 | if (cand == 0) | |
2273 | { | |
2274 | cp_error ("call of overloaded `%D (%A)' is ambiguous", | |
2275 | TREE_PURPOSE (fn), args); | |
2276 | print_z_candidates (candidates); | |
2277 | return error_mark_node; | |
2278 | } | |
2279 | ||
056c014d | 2280 | /* Pedantically, normal function declarations are never considered |
386b8a85 JM |
2281 | to refer to template instantiations, so we only do this with |
2282 | -fguiding-decls. */ | |
2283 | if (flag_guiding_decls && templates && ! cand->template | |
2284 | && ! DECL_INITIAL (cand->fn)) | |
73aad9b9 JM |
2285 | add_maybe_template (cand->fn, templates); |
2286 | ||
5ffe581d | 2287 | return build_over_call (cand, args, LOOKUP_NORMAL); |
c73964b2 MS |
2288 | } |
2289 | ||
2290 | return build_function_call (fn, args); | |
2291 | } | |
2292 | ||
bd6dd845 | 2293 | static tree |
c73964b2 MS |
2294 | build_object_call (obj, args) |
2295 | tree obj, args; | |
2296 | { | |
2297 | struct z_candidate *candidates = 0, *cand; | |
a703fb38 | 2298 | tree fns, convs, mem_args = NULL_TREE; |
c73964b2 | 2299 | tree type = TREE_TYPE (obj); |
786b5245 | 2300 | tree templates = NULL_TREE; |
c73964b2 | 2301 | |
297dcfb3 MM |
2302 | if (TYPE_PTRMEMFUNC_P (type)) |
2303 | { | |
2304 | /* It's no good looking for an overloaded operator() on a | |
2305 | pointer-to-member-function. */ | |
2306 | cp_error ("pointer-to-member function %E cannot be called", obj); | |
2307 | cp_error ("without an object; consider using .* or ->*"); | |
2308 | return error_mark_node; | |
2309 | } | |
2310 | ||
734e8cc5 MM |
2311 | fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname [CALL_EXPR], 1); |
2312 | if (fns == error_mark_node) | |
2313 | return error_mark_node; | |
c73964b2 | 2314 | |
86e6f22f JM |
2315 | args = resolve_args (args); |
2316 | ||
2317 | if (args == error_mark_node) | |
2318 | return error_mark_node; | |
2319 | ||
c73964b2 MS |
2320 | if (fns) |
2321 | { | |
2322 | tree fn = TREE_VALUE (fns); | |
e66d884e | 2323 | mem_args = scratch_tree_cons (NULL_TREE, build_this (obj), args); |
c73964b2 MS |
2324 | |
2325 | for (; fn; fn = DECL_CHAIN (fn)) | |
2326 | { | |
786b5245 MM |
2327 | if (TREE_CODE (fn) == TEMPLATE_DECL) |
2328 | { | |
e66d884e | 2329 | templates = scratch_tree_cons (NULL_TREE, fn, templates); |
386b8a85 JM |
2330 | candidates |
2331 | = add_template_candidate (candidates, fn, NULL_TREE, | |
2332 | mem_args, NULL_TREE, | |
2333 | LOOKUP_NORMAL); | |
786b5245 MM |
2334 | } |
2335 | else | |
2336 | candidates = add_function_candidate | |
2337 | (candidates, fn, mem_args, LOOKUP_NORMAL); | |
2338 | ||
2339 | if (candidates) | |
2340 | candidates->basetype_path = TREE_PURPOSE (fns); | |
c73964b2 MS |
2341 | } |
2342 | } | |
2343 | ||
2344 | convs = lookup_conversions (type); | |
2345 | ||
2346 | for (; convs; convs = TREE_CHAIN (convs)) | |
2347 | { | |
2348 | tree fn = TREE_VALUE (convs); | |
2349 | tree totype = TREE_TYPE (TREE_TYPE (fn)); | |
2350 | ||
2351 | if (TREE_CODE (totype) == POINTER_TYPE | |
2352 | && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE) | |
2353 | for (; fn; fn = DECL_CHAIN (fn)) | |
2354 | { | |
786b5245 MM |
2355 | if (TREE_CODE (fn) == TEMPLATE_DECL) |
2356 | { | |
e66d884e | 2357 | templates = scratch_tree_cons (NULL_TREE, fn, templates); |
786b5245 MM |
2358 | candidates = add_template_conv_candidate (candidates, |
2359 | fn, | |
2360 | obj, | |
2361 | args, | |
2362 | totype); | |
2363 | } | |
2364 | else | |
2365 | candidates = add_conv_candidate (candidates, fn, obj, args); | |
2366 | ||
2367 | if (candidates) | |
2368 | candidates->basetype_path = TREE_PURPOSE (convs); | |
c73964b2 MS |
2369 | } |
2370 | } | |
2371 | ||
2372 | if (! any_viable (candidates)) | |
2373 | { | |
d11ad92e | 2374 | cp_error ("no match for call to `(%T) (%A)'", TREE_TYPE (obj), args); |
c73964b2 MS |
2375 | print_z_candidates (candidates); |
2376 | return error_mark_node; | |
2377 | } | |
2378 | ||
2379 | candidates = splice_viable (candidates); | |
da20811c | 2380 | cand = tourney (candidates); |
c73964b2 MS |
2381 | |
2382 | if (cand == 0) | |
2383 | { | |
2384 | cp_error ("call of `(%T) (%A)' is ambiguous", TREE_TYPE (obj), args); | |
2385 | print_z_candidates (candidates); | |
2386 | return error_mark_node; | |
2387 | } | |
2388 | ||
2389 | if (DECL_NAME (cand->fn) == ansi_opname [CALL_EXPR]) | |
5ffe581d | 2390 | return build_over_call (cand, mem_args, LOOKUP_NORMAL); |
c73964b2 MS |
2391 | |
2392 | obj = convert_like (TREE_VEC_ELT (cand->convs, 0), obj); | |
2393 | ||
2394 | /* FIXME */ | |
2395 | return build_function_call (obj, args); | |
2396 | } | |
2397 | ||
2398 | static void | |
2399 | op_error (code, code2, arg1, arg2, arg3, problem) | |
2400 | enum tree_code code, code2; | |
2401 | tree arg1, arg2, arg3; | |
2402 | char *problem; | |
2403 | { | |
2404 | char * opname | |
2405 | = (code == MODIFY_EXPR ? assignop_tab [code2] : opname_tab [code]); | |
2406 | ||
2407 | switch (code) | |
2408 | { | |
2409 | case COND_EXPR: | |
2410 | cp_error ("%s for `%T ? %T : %T'", problem, | |
d11ad92e | 2411 | error_type (arg1), error_type (arg2), error_type (arg3)); |
c73964b2 MS |
2412 | break; |
2413 | case POSTINCREMENT_EXPR: | |
2414 | case POSTDECREMENT_EXPR: | |
d11ad92e | 2415 | cp_error ("%s for `%T%s'", problem, error_type (arg1), opname); |
c73964b2 MS |
2416 | break; |
2417 | case ARRAY_REF: | |
2418 | cp_error ("%s for `%T[%T]'", problem, | |
d11ad92e | 2419 | error_type (arg1), error_type (arg2)); |
c73964b2 MS |
2420 | break; |
2421 | default: | |
2422 | if (arg2) | |
2423 | cp_error ("%s for `%T %s %T'", problem, | |
d11ad92e | 2424 | error_type (arg1), opname, error_type (arg2)); |
c73964b2 | 2425 | else |
d11ad92e | 2426 | cp_error ("%s for `%s%T'", problem, opname, error_type (arg1)); |
c73964b2 MS |
2427 | } |
2428 | } | |
2429 | ||
2430 | tree | |
2431 | build_new_op (code, flags, arg1, arg2, arg3) | |
2432 | enum tree_code code; | |
2433 | int flags; | |
2434 | tree arg1, arg2, arg3; | |
2435 | { | |
2436 | struct z_candidate *candidates = 0, *cand; | |
a703fb38 | 2437 | tree fns, mem_arglist = NULL_TREE, arglist, fnname; |
c73964b2 | 2438 | enum tree_code code2 = NOP_EXPR; |
73aad9b9 | 2439 | tree templates = NULL_TREE; |
7e3af374 | 2440 | tree conv; |
c73964b2 | 2441 | |
d11ad92e MS |
2442 | if (arg1 == error_mark_node |
2443 | || arg2 == error_mark_node | |
2444 | || arg3 == error_mark_node) | |
c73964b2 MS |
2445 | return error_mark_node; |
2446 | ||
d5467e20 JM |
2447 | /* This can happen if a template takes all non-type parameters, e.g. |
2448 | undeclared_template<1, 5, 72>a; */ | |
2449 | if (code == LT_EXPR && TREE_CODE (arg1) == TEMPLATE_DECL) | |
2450 | { | |
2451 | cp_error ("`%D' must be declared before use", arg1); | |
2452 | return error_mark_node; | |
2453 | } | |
2454 | ||
c73964b2 MS |
2455 | if (code == MODIFY_EXPR) |
2456 | { | |
2457 | code2 = TREE_CODE (arg3); | |
2458 | arg3 = NULL_TREE; | |
2459 | fnname = ansi_assopname[code2]; | |
2460 | } | |
2461 | else | |
2462 | fnname = ansi_opname[code]; | |
2463 | ||
2464 | switch (code) | |
2465 | { | |
2466 | case NEW_EXPR: | |
2467 | case VEC_NEW_EXPR: | |
2468 | { | |
2469 | tree rval; | |
2470 | ||
e66d884e | 2471 | arglist = scratch_tree_cons (NULL_TREE, arg2, arg3); |
c73964b2 MS |
2472 | if (flags & LOOKUP_GLOBAL) |
2473 | return build_new_function_call | |
9babbf20 | 2474 | (lookup_name_nonclass (fnname), arglist); |
c73964b2 MS |
2475 | |
2476 | /* FIXME */ | |
2477 | rval = build_method_call | |
2478 | (build_indirect_ref (build1 (NOP_EXPR, arg1, error_mark_node), | |
2479 | "new"), | |
2480 | fnname, arglist, NULL_TREE, flags); | |
2481 | if (rval == error_mark_node) | |
2482 | /* User might declare fancy operator new, but invoke it | |
2483 | like standard one. */ | |
2484 | return rval; | |
2485 | ||
2486 | TREE_TYPE (rval) = arg1; | |
c73964b2 MS |
2487 | return rval; |
2488 | } | |
2489 | ||
2490 | case VEC_DELETE_EXPR: | |
2491 | case DELETE_EXPR: | |
2492 | { | |
2493 | tree rval; | |
2494 | ||
2495 | if (flags & LOOKUP_GLOBAL) | |
2496 | return build_new_function_call | |
2497 | (lookup_name_nonclass (fnname), | |
9babbf20 | 2498 | build_scratch_list (NULL_TREE, arg1)); |
c73964b2 | 2499 | |
e66d884e | 2500 | arglist = scratch_tree_cons (NULL_TREE, arg1, build_scratch_list (NULL_TREE, arg2)); |
c73964b2 MS |
2501 | |
2502 | arg1 = TREE_TYPE (arg1); | |
2503 | ||
2504 | /* This handles the case where we're trying to delete | |
2505 | X (*a)[10]; | |
2506 | a=new X[5][10]; | |
2507 | delete[] a; */ | |
2508 | ||
2509 | if (TREE_CODE (TREE_TYPE (arg1)) == ARRAY_TYPE) | |
2510 | { | |
2511 | /* Strip off the pointer and the array. */ | |
2512 | arg1 = TREE_TYPE (TREE_TYPE (arg1)); | |
2513 | ||
2514 | while (TREE_CODE (arg1) == ARRAY_TYPE) | |
2515 | arg1 = (TREE_TYPE (arg1)); | |
2516 | ||
2517 | arg1 = build_pointer_type (arg1); | |
2518 | } | |
2519 | ||
2520 | /* FIXME */ | |
2521 | rval = build_method_call | |
2522 | (build_indirect_ref (build1 (NOP_EXPR, arg1, | |
2523 | error_mark_node), | |
2524 | NULL_PTR), | |
2525 | fnname, arglist, NULL_TREE, flags); | |
2526 | #if 0 | |
2527 | /* This can happen when operator delete is protected. */ | |
2528 | my_friendly_assert (rval != error_mark_node, 250); | |
2529 | TREE_TYPE (rval) = void_type_node; | |
2530 | #endif | |
2531 | return rval; | |
2532 | } | |
2533 | ||
2534 | case CALL_EXPR: | |
2535 | return build_object_call (arg1, arg2); | |
7f85441b KG |
2536 | |
2537 | default: | |
2538 | break; | |
c73964b2 MS |
2539 | } |
2540 | ||
2541 | /* The comma operator can have void args. */ | |
2542 | if (TREE_CODE (arg1) == OFFSET_REF) | |
2543 | arg1 = resolve_offset_ref (arg1); | |
2544 | if (arg2 && TREE_CODE (arg2) == OFFSET_REF) | |
2545 | arg2 = resolve_offset_ref (arg2); | |
2546 | if (arg3 && TREE_CODE (arg3) == OFFSET_REF) | |
2547 | arg3 = resolve_offset_ref (arg3); | |
2548 | ||
5156628f MS |
2549 | if (code == COND_EXPR) |
2550 | { | |
beb53fb8 JM |
2551 | if (arg2 == NULL_TREE |
2552 | || TREE_CODE (TREE_TYPE (arg2)) == VOID_TYPE | |
5156628f MS |
2553 | || TREE_CODE (TREE_TYPE (arg3)) == VOID_TYPE |
2554 | || (! IS_OVERLOAD_TYPE (TREE_TYPE (arg2)) | |
2555 | && ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3)))) | |
2556 | goto builtin; | |
2557 | } | |
2558 | else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1)) | |
2559 | && (! arg2 || ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2)))) | |
c73964b2 MS |
2560 | goto builtin; |
2561 | ||
2562 | if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR) | |
2563 | arg2 = integer_zero_node; | |
2564 | ||
c73964b2 | 2565 | if (arg2 && arg3) |
e66d884e JM |
2566 | arglist = scratch_tree_cons (NULL_TREE, arg1, scratch_tree_cons |
2567 | (NULL_TREE, arg2, build_scratch_list (NULL_TREE, arg3))); | |
c73964b2 | 2568 | else if (arg2) |
e66d884e | 2569 | arglist = scratch_tree_cons (NULL_TREE, arg1, build_scratch_list (NULL_TREE, arg2)); |
c73964b2 | 2570 | else |
e66d884e | 2571 | arglist = build_scratch_list (NULL_TREE, arg1); |
c73964b2 | 2572 | |
1f109f0f JM |
2573 | fns = lookup_name_nonclass (fnname); |
2574 | /* + Koenig lookup */ | |
2575 | ||
c73964b2 MS |
2576 | if (fns && TREE_CODE (fns) == TREE_LIST) |
2577 | fns = TREE_VALUE (fns); | |
2578 | for (; fns; fns = DECL_CHAIN (fns)) | |
2579 | { | |
2580 | if (TREE_CODE (fns) == TEMPLATE_DECL) | |
73aad9b9 | 2581 | { |
e66d884e | 2582 | templates = scratch_tree_cons (NULL_TREE, fns, templates); |
386b8a85 JM |
2583 | candidates |
2584 | = add_template_candidate (candidates, fns, NULL_TREE, | |
2585 | arglist, TREE_TYPE (fnname), | |
2586 | flags); | |
73aad9b9 | 2587 | } |
c73964b2 MS |
2588 | else |
2589 | candidates = add_function_candidate (candidates, fns, arglist, flags); | |
2590 | } | |
2591 | ||
2592 | if (IS_AGGR_TYPE (TREE_TYPE (arg1))) | |
734e8cc5 MM |
2593 | { |
2594 | fns = lookup_fnfields (TYPE_BINFO (TREE_TYPE (arg1)), fnname, 1); | |
2595 | if (fns == error_mark_node) | |
2596 | return fns; | |
2597 | } | |
c73964b2 MS |
2598 | else |
2599 | fns = NULL_TREE; | |
2600 | ||
2601 | if (fns) | |
2602 | { | |
2603 | tree fn = TREE_VALUE (fns); | |
e66d884e | 2604 | mem_arglist = scratch_tree_cons (NULL_TREE, build_this (arg1), TREE_CHAIN (arglist)); |
c73964b2 MS |
2605 | for (; fn; fn = DECL_CHAIN (fn)) |
2606 | { | |
98c1c668 JM |
2607 | tree this_arglist; |
2608 | ||
c73964b2 | 2609 | if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE) |
98c1c668 | 2610 | this_arglist = mem_arglist; |
c73964b2 | 2611 | else |
98c1c668 JM |
2612 | this_arglist = arglist; |
2613 | ||
2614 | if (TREE_CODE (fn) == TEMPLATE_DECL) | |
2615 | { | |
2616 | /* A member template. */ | |
e66d884e | 2617 | templates = scratch_tree_cons (NULL_TREE, fn, templates); |
386b8a85 JM |
2618 | candidates |
2619 | = add_template_candidate (candidates, fn, NULL_TREE, | |
da4768fe JM |
2620 | this_arglist, TREE_TYPE (fnname), |
2621 | flags); | |
98c1c668 JM |
2622 | } |
2623 | else | |
2624 | candidates = add_function_candidate | |
2625 | (candidates, fn, this_arglist, flags); | |
2626 | ||
2627 | if (candidates) | |
2628 | candidates->basetype_path = TREE_PURPOSE (fns); | |
c73964b2 MS |
2629 | } |
2630 | } | |
2631 | ||
2632 | { | |
2633 | tree args[3]; | |
2634 | ||
2635 | /* Rearrange the arguments for ?: so that add_builtin_candidate only has | |
2636 | to know about two args; a builtin candidate will always have a first | |
2637 | parameter of type bool. We'll handle that in | |
2638 | build_builtin_candidate. */ | |
2639 | if (code == COND_EXPR) | |
2640 | { | |
2641 | args[0] = arg2; | |
2642 | args[1] = arg3; | |
2643 | args[2] = arg1; | |
2644 | } | |
2645 | else | |
2646 | { | |
2647 | args[0] = arg1; | |
2648 | args[1] = arg2; | |
2649 | args[2] = NULL_TREE; | |
2650 | } | |
2651 | ||
2652 | candidates = add_builtin_candidates | |
2653 | (candidates, code, code2, fnname, args, flags); | |
2654 | } | |
2655 | ||
2656 | if (! any_viable (candidates)) | |
2657 | { | |
2658 | switch (code) | |
2659 | { | |
2660 | case POSTINCREMENT_EXPR: | |
2661 | case POSTDECREMENT_EXPR: | |
2662 | /* Look for an `operator++ (int)'. If they didn't have | |
2663 | one, then we fall back to the old way of doing things. */ | |
2664 | if (flags & LOOKUP_COMPLAIN) | |
2665 | cp_pedwarn ("no `%D (int)' declared for postfix `%s', trying prefix operator instead", | |
2666 | fnname, opname_tab [code]); | |
2667 | if (code == POSTINCREMENT_EXPR) | |
2668 | code = PREINCREMENT_EXPR; | |
2669 | else | |
2670 | code = PREDECREMENT_EXPR; | |
2671 | return build_new_op (code, flags, arg1, NULL_TREE, NULL_TREE); | |
2672 | ||
2673 | /* The caller will deal with these. */ | |
2674 | case ADDR_EXPR: | |
2675 | case COMPOUND_EXPR: | |
2676 | case COMPONENT_REF: | |
2677 | return NULL_TREE; | |
7f85441b KG |
2678 | |
2679 | default: | |
2680 | break; | |
c73964b2 MS |
2681 | } |
2682 | if (flags & LOOKUP_COMPLAIN) | |
2683 | { | |
2684 | op_error (code, code2, arg1, arg2, arg3, "no match"); | |
2685 | print_z_candidates (candidates); | |
2686 | } | |
2687 | return error_mark_node; | |
2688 | } | |
2689 | candidates = splice_viable (candidates); | |
da20811c | 2690 | cand = tourney (candidates); |
c73964b2 MS |
2691 | |
2692 | if (cand == 0) | |
2693 | { | |
2694 | if (flags & LOOKUP_COMPLAIN) | |
2695 | { | |
2696 | op_error (code, code2, arg1, arg2, arg3, "ambiguous overload"); | |
2697 | print_z_candidates (candidates); | |
2698 | } | |
2699 | return error_mark_node; | |
2700 | } | |
2701 | ||
2702 | if (TREE_CODE (cand->fn) == FUNCTION_DECL) | |
2703 | { | |
2704 | extern int warn_synth; | |
2705 | if (warn_synth | |
2706 | && fnname == ansi_opname[MODIFY_EXPR] | |
2707 | && DECL_ARTIFICIAL (cand->fn) | |
2708 | && candidates->next | |
2709 | && ! candidates->next->next) | |
2710 | { | |
2711 | cp_warning ("using synthesized `%#D' for copy assignment", | |
2712 | cand->fn); | |
2713 | cp_warning_at (" where cfront would use `%#D'", | |
2714 | cand == candidates | |
2715 | ? candidates->next->fn | |
2716 | : candidates->fn); | |
2717 | } | |
2718 | ||
98c1c668 | 2719 | /* Pedantically, normal function declarations are never considered |
386b8a85 JM |
2720 | to refer to template instantiations, so we only do this with |
2721 | -fguiding-decls. */ | |
2722 | if (flag_guiding_decls && templates && ! cand->template | |
2723 | && ! DECL_INITIAL (cand->fn) | |
73aad9b9 JM |
2724 | && TREE_CODE (TREE_TYPE (cand->fn)) != METHOD_TYPE) |
2725 | add_maybe_template (cand->fn, templates); | |
2726 | ||
c73964b2 | 2727 | return build_over_call |
5ffe581d | 2728 | (cand, |
c73964b2 MS |
2729 | TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE |
2730 | ? mem_arglist : arglist, | |
2731 | LOOKUP_NORMAL); | |
2732 | } | |
2733 | ||
d11ad92e MS |
2734 | /* Check for comparison of different enum types. */ |
2735 | switch (code) | |
2736 | { | |
2737 | case GT_EXPR: | |
2738 | case LT_EXPR: | |
2739 | case GE_EXPR: | |
2740 | case LE_EXPR: | |
2741 | case EQ_EXPR: | |
2742 | case NE_EXPR: | |
2743 | if (flag_int_enum_equivalence == 0 | |
2744 | && TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE | |
2745 | && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE | |
2746 | && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) | |
2747 | != TYPE_MAIN_VARIANT (TREE_TYPE (arg2)))) | |
2748 | { | |
2749 | cp_warning ("comparison between `%#T' and `%#T'", | |
2750 | TREE_TYPE (arg1), TREE_TYPE (arg2)); | |
2751 | } | |
7f85441b KG |
2752 | break; |
2753 | default: | |
2754 | break; | |
d11ad92e MS |
2755 | } |
2756 | ||
7e3af374 JM |
2757 | /* We need to strip any leading REF_BIND so that bitfields don't cause |
2758 | errors. This should not remove any important conversions, because | |
2759 | builtins don't apply to class objects directly. */ | |
2760 | conv = TREE_VEC_ELT (cand->convs, 0); | |
2761 | if (TREE_CODE (conv) == REF_BIND) | |
2762 | conv = TREE_OPERAND (conv, 0); | |
2763 | arg1 = convert_like (conv, arg1); | |
c73964b2 MS |
2764 | if (arg2) |
2765 | arg2 = convert_like (TREE_VEC_ELT (cand->convs, 1), arg2); | |
2766 | if (arg3) | |
2767 | arg3 = convert_like (TREE_VEC_ELT (cand->convs, 2), arg3); | |
2768 | ||
2769 | builtin: | |
2770 | switch (code) | |
2771 | { | |
2772 | case MODIFY_EXPR: | |
2773 | return build_modify_expr (arg1, code2, arg2); | |
2774 | ||
2775 | case INDIRECT_REF: | |
2776 | return build_indirect_ref (arg1, "unary *"); | |
2777 | ||
2778 | case PLUS_EXPR: | |
2779 | case MINUS_EXPR: | |
2780 | case MULT_EXPR: | |
2781 | case TRUNC_DIV_EXPR: | |
2782 | case GT_EXPR: | |
2783 | case LT_EXPR: | |
2784 | case GE_EXPR: | |
2785 | case LE_EXPR: | |
2786 | case EQ_EXPR: | |
2787 | case NE_EXPR: | |
2788 | case MAX_EXPR: | |
2789 | case MIN_EXPR: | |
2790 | case LSHIFT_EXPR: | |
2791 | case RSHIFT_EXPR: | |
2792 | case TRUNC_MOD_EXPR: | |
2793 | case BIT_AND_EXPR: | |
2794 | case BIT_IOR_EXPR: | |
2795 | case BIT_XOR_EXPR: | |
2796 | case TRUTH_ANDIF_EXPR: | |
2797 | case TRUTH_ORIF_EXPR: | |
2798 | return build_binary_op_nodefault (code, arg1, arg2, code); | |
2799 | ||
2800 | case CONVERT_EXPR: | |
2801 | case NEGATE_EXPR: | |
2802 | case BIT_NOT_EXPR: | |
2803 | case TRUTH_NOT_EXPR: | |
2804 | case PREINCREMENT_EXPR: | |
2805 | case POSTINCREMENT_EXPR: | |
2806 | case PREDECREMENT_EXPR: | |
2807 | case POSTDECREMENT_EXPR: | |
37c46b43 MS |
2808 | case REALPART_EXPR: |
2809 | case IMAGPART_EXPR: | |
c73964b2 MS |
2810 | return build_unary_op (code, arg1, candidates != 0); |
2811 | ||
2812 | case ARRAY_REF: | |
2813 | return build_array_ref (arg1, arg2); | |
2814 | ||
2815 | case COND_EXPR: | |
2816 | return build_conditional_expr (arg1, arg2, arg3); | |
2817 | ||
2818 | case MEMBER_REF: | |
2819 | return build_m_component_ref | |
2820 | (build_indirect_ref (arg1, NULL_PTR), arg2); | |
2821 | ||
2822 | /* The caller will deal with these. */ | |
2823 | case ADDR_EXPR: | |
2824 | case COMPONENT_REF: | |
2825 | case COMPOUND_EXPR: | |
2826 | return NULL_TREE; | |
2827 | ||
2828 | default: | |
2829 | my_friendly_abort (367); | |
a703fb38 | 2830 | return NULL_TREE; |
c73964b2 MS |
2831 | } |
2832 | } | |
2833 | ||
da4768fe JM |
2834 | /* Build up a call to operator new. This has to be handled differently |
2835 | from other operators in the way lookup is handled; first members are | |
2836 | considered, then globals. CODE is either NEW_EXPR or VEC_NEW_EXPR. | |
2837 | TYPE is the type to be created. ARGS are any new-placement args. | |
2838 | FLAGS are the usual overloading flags. */ | |
2839 | ||
2840 | tree | |
2841 | build_op_new_call (code, type, args, flags) | |
2842 | enum tree_code code; | |
2843 | tree type, args; | |
2844 | int flags; | |
2845 | { | |
2846 | tree fnname = ansi_opname[code]; | |
2847 | ||
2848 | if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL) | |
2849 | && (TYPE_GETS_NEW (type) & (1 << (code == VEC_NEW_EXPR)))) | |
2850 | { | |
2851 | tree dummy = build1 (NOP_EXPR, build_pointer_type (type), | |
2852 | error_mark_node); | |
2853 | dummy = build_indirect_ref (dummy, "new"); | |
2854 | return build_method_call (dummy, fnname, args, NULL_TREE, flags); | |
2855 | } | |
2856 | else | |
2857 | return build_new_function_call (lookup_name_nonclass (fnname), args); | |
2858 | } | |
2859 | ||
2860 | /* Build a call to operator delete. This has to be handled very specially, | |
2861 | because the restrictions on what signatures match are different from all | |
2862 | other call instances. For a normal delete, only a delete taking (void *) | |
2863 | or (void *, size_t) is accepted. For a placement delete, only an exact | |
2864 | match with the placement new is accepted. | |
2865 | ||
2866 | CODE is either DELETE_EXPR or VEC_DELETE_EXPR. | |
2867 | ADDR is the pointer to be deleted. For placement delete, it is also | |
2868 | used to determine what the corresponding new looked like. | |
2869 | SIZE is the size of the memory block to be deleted. | |
2870 | FLAGS are the usual overloading flags. */ | |
2871 | ||
2872 | tree | |
2873 | build_op_delete_call (code, addr, size, flags) | |
2874 | enum tree_code code; | |
2875 | tree addr, size; | |
2876 | int flags; | |
2877 | { | |
2878 | tree fn, fns, fnname, fntype, argtypes, args, type; | |
2879 | int placement; | |
2880 | ||
2881 | if (addr == error_mark_node) | |
2882 | return error_mark_node; | |
2883 | ||
2884 | type = TREE_TYPE (TREE_TYPE (addr)); | |
2885 | fnname = ansi_opname[code]; | |
2886 | ||
2887 | if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL)) | |
734e8cc5 MM |
2888 | /* In [class.free] |
2889 | ||
2890 | If the result of the lookup is ambiguous or inaccessible, or if | |
2891 | the lookup selects a placement deallocation function, the | |
2892 | program is ill-formed. | |
2893 | ||
2894 | Therefore, we ask lookup_fnfields to complain ambout ambiguity. */ | |
2895 | { | |
2896 | fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1); | |
2897 | if (fns == error_mark_node) | |
2898 | return error_mark_node; | |
2899 | } | |
da4768fe JM |
2900 | else |
2901 | fns = NULL_TREE; | |
2902 | ||
37130ac3 JM |
2903 | if (fns) |
2904 | { | |
2905 | /* Build this up like build_offset_ref does. */ | |
2906 | fns = build_tree_list (error_mark_node, fns); | |
2907 | TREE_TYPE (fns) = build_offset_type (type, unknown_type_node); | |
2908 | } | |
2909 | else | |
da4768fe JM |
2910 | fns = lookup_name_nonclass (fnname); |
2911 | ||
2912 | /* We can recognize a placement delete because of LOOKUP_SPECULATIVELY; | |
2913 | if we are doing placement delete we do nothing if we don't find a | |
2914 | matching op delete. */ | |
2915 | placement = !!(flags & LOOKUP_SPECULATIVELY); | |
2916 | if (placement) | |
2917 | { | |
2918 | /* If placement, we are coming from build_new, and we know that addr | |
2919 | is the allocation expression, so extract the info we need from it. | |
2920 | Obviously, if the build_new process changes this may have to | |
2921 | change as well. */ | |
2face519 JM |
2922 | |
2923 | /* The NOP_EXPR. */ | |
2924 | tree t = TREE_OPERAND (addr, 1); | |
da4768fe JM |
2925 | /* The CALL_EXPR. */ |
2926 | t = TREE_OPERAND (t, 0); | |
2927 | /* The function. */ | |
2928 | argtypes = TREE_OPERAND (TREE_OPERAND (t, 0), 0); | |
2929 | /* The second parm type. */ | |
2930 | argtypes = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (argtypes))); | |
2931 | /* The second argument. */ | |
2932 | args = TREE_CHAIN (TREE_OPERAND (t, 1)); | |
2face519 JM |
2933 | |
2934 | /* Pull the dummy var out of the TARGET_EXPR for use in our call. */ | |
2935 | addr = TREE_OPERAND (addr, 0); | |
da4768fe JM |
2936 | } |
2937 | else | |
2938 | { | |
2939 | /* First try it without the size argument. */ | |
2940 | argtypes = void_list_node; | |
2941 | args = NULL_TREE; | |
2942 | } | |
2943 | ||
2944 | argtypes = tree_cons (NULL_TREE, ptr_type_node, argtypes); | |
2945 | fntype = build_function_type (void_type_node, argtypes); | |
2946 | ||
2947 | /* Strip const and volatile from addr. */ | |
2948 | if (type != TYPE_MAIN_VARIANT (type)) | |
2949 | addr = cp_convert (build_pointer_type (TYPE_MAIN_VARIANT (type)), addr); | |
2950 | ||
2951 | /* instantiate_type will always return a plain function; pretend it's | |
2952 | overloaded. */ | |
2953 | if (TREE_CODE (fns) == FUNCTION_DECL) | |
2954 | fns = scratch_tree_cons (NULL_TREE, fns, NULL_TREE); | |
2955 | ||
2956 | fn = instantiate_type (fntype, fns, 0); | |
2957 | ||
2958 | if (fn != error_mark_node) | |
2959 | { | |
047f64a3 JM |
2960 | if (TREE_CODE (TREE_VALUE (fns)) == TREE_LIST) |
2961 | /* Member functions. */ | |
37130ac3 | 2962 | enforce_access (TREE_PURPOSE (TREE_VALUE (fns)), fn); |
da4768fe JM |
2963 | return build_function_call (fn, expr_tree_cons (NULL_TREE, addr, args)); |
2964 | } | |
2965 | ||
2966 | if (placement) | |
2967 | return NULL_TREE; | |
2968 | ||
2969 | /* Normal delete; now try to find a match including the size argument. */ | |
2970 | argtypes = tree_cons (NULL_TREE, ptr_type_node, | |
2971 | tree_cons (NULL_TREE, sizetype, void_list_node)); | |
2972 | fntype = build_function_type (void_type_node, argtypes); | |
2973 | ||
2974 | fn = instantiate_type (fntype, fns, 0); | |
2975 | ||
2976 | if (fn != error_mark_node) | |
2977 | return build_function_call | |
2978 | (fn, expr_tree_cons (NULL_TREE, addr, | |
2979 | build_expr_list (NULL_TREE, size))); | |
2980 | ||
2981 | cp_error ("no suitable operator delete for `%T'", type); | |
2982 | return error_mark_node; | |
2983 | } | |
2984 | ||
38afd588 | 2985 | /* If the current scope isn't allowed to access DECL along |
da4768fe JM |
2986 | BASETYPE_PATH, give an error. */ |
2987 | ||
38afd588 MM |
2988 | void |
2989 | enforce_access (basetype_path, decl) | |
2990 | tree basetype_path, decl; | |
c73964b2 | 2991 | { |
38afd588 | 2992 | tree access = compute_access (basetype_path, decl); |
c73964b2 MS |
2993 | |
2994 | if (access == access_private_node) | |
2995 | { | |
38afd588 MM |
2996 | cp_error_at ("`%+#D' is %s", decl, |
2997 | TREE_PRIVATE (decl) ? "private" | |
c73964b2 MS |
2998 | : "from private base class"); |
2999 | error ("within this context"); | |
3000 | } | |
3001 | else if (access == access_protected_node) | |
3002 | { | |
38afd588 MM |
3003 | cp_error_at ("`%+#D' %s", decl, |
3004 | TREE_PROTECTED (decl) ? "is protected" | |
c73964b2 MS |
3005 | : "has protected accessibility"); |
3006 | error ("within this context"); | |
3007 | } | |
3008 | } | |
3009 | ||
3010 | /* Perform the conversions in CONVS on the expression EXPR. */ | |
3011 | ||
3012 | static tree | |
3013 | convert_like (convs, expr) | |
3014 | tree convs, expr; | |
3015 | { | |
d2e5ee5c MS |
3016 | if (ICS_BAD_FLAG (convs) |
3017 | && TREE_CODE (convs) != USER_CONV | |
3018 | && TREE_CODE (convs) != AMBIG_CONV) | |
d11ad92e MS |
3019 | { |
3020 | tree t = convs; | |
3021 | for (; t; t = TREE_OPERAND (t, 0)) | |
3022 | { | |
3023 | if (TREE_CODE (t) == USER_CONV) | |
3024 | { | |
3025 | expr = convert_like (t, expr); | |
3026 | break; | |
3027 | } | |
3028 | else if (TREE_CODE (t) == AMBIG_CONV) | |
3029 | return convert_like (t, expr); | |
3030 | else if (TREE_CODE (t) == IDENTITY_CONV) | |
3031 | break; | |
3032 | } | |
3033 | return convert_for_initialization | |
3034 | (NULL_TREE, TREE_TYPE (convs), expr, LOOKUP_NORMAL, | |
3035 | "conversion", NULL_TREE, 0); | |
3036 | } | |
3037 | ||
c73964b2 MS |
3038 | switch (TREE_CODE (convs)) |
3039 | { | |
3040 | case USER_CONV: | |
3041 | { | |
5ffe581d JM |
3042 | struct z_candidate *cand |
3043 | = WRAPPER_PTR (TREE_OPERAND (convs, 1)); | |
3044 | tree fn = cand->fn; | |
c73964b2 | 3045 | tree args; |
c73964b2 MS |
3046 | |
3047 | if (DECL_CONSTRUCTOR_P (fn)) | |
3048 | { | |
3049 | tree t = build_int_2 (0, 0); | |
3050 | TREE_TYPE (t) = build_pointer_type (DECL_CONTEXT (fn)); | |
3051 | ||
e66d884e | 3052 | args = build_scratch_list (NULL_TREE, expr); |
c73964b2 | 3053 | if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn))) |
e66d884e JM |
3054 | args = scratch_tree_cons (NULL_TREE, integer_one_node, args); |
3055 | args = scratch_tree_cons (NULL_TREE, t, args); | |
c73964b2 MS |
3056 | } |
3057 | else | |
3058 | args = build_this (expr); | |
5ffe581d | 3059 | expr = build_over_call (cand, args, LOOKUP_NORMAL); |
c73964b2 MS |
3060 | |
3061 | /* If this is a constructor or a function returning an aggr type, | |
3062 | we need to build up a TARGET_EXPR. */ | |
3063 | if (DECL_CONSTRUCTOR_P (fn)) | |
3064 | expr = build_cplus_new (TREE_TYPE (convs), expr); | |
3065 | ||
3066 | return expr; | |
3067 | } | |
3068 | case IDENTITY_CONV: | |
3069 | if (type_unknown_p (expr)) | |
3070 | expr = instantiate_type (TREE_TYPE (convs), expr, 1); | |
5156628f MS |
3071 | if (TREE_READONLY_DECL_P (expr)) |
3072 | expr = decl_constant_value (expr); | |
c73964b2 MS |
3073 | return expr; |
3074 | case AMBIG_CONV: | |
3075 | /* Call build_user_type_conversion again for the error. */ | |
3076 | return build_user_type_conversion | |
3077 | (TREE_TYPE (convs), TREE_OPERAND (convs, 0), LOOKUP_NORMAL); | |
7f85441b KG |
3078 | |
3079 | default: | |
3080 | break; | |
c73964b2 MS |
3081 | }; |
3082 | ||
3083 | expr = convert_like (TREE_OPERAND (convs, 0), expr); | |
3084 | if (expr == error_mark_node) | |
3085 | return error_mark_node; | |
3086 | ||
3087 | switch (TREE_CODE (convs)) | |
3088 | { | |
c73964b2 | 3089 | case RVALUE_CONV: |
de22184b MS |
3090 | if (! IS_AGGR_TYPE (TREE_TYPE (convs))) |
3091 | return expr; | |
3092 | /* else fall through */ | |
3093 | case BASE_CONV: | |
c73964b2 MS |
3094 | return build_user_type_conversion |
3095 | (TREE_TYPE (convs), expr, LOOKUP_NORMAL); | |
3096 | case REF_BIND: | |
3097 | return convert_to_reference | |
3098 | (TREE_TYPE (convs), expr, | |
9a3b49ac | 3099 | CONV_IMPLICIT, LOOKUP_NORMAL|LOOKUP_NO_CONVERSION, |
c73964b2 MS |
3100 | error_mark_node); |
3101 | case LVALUE_CONV: | |
3102 | return decay_conversion (expr); | |
7f85441b KG |
3103 | |
3104 | default: | |
3105 | break; | |
c73964b2 | 3106 | } |
37c46b43 MS |
3107 | return ocp_convert (TREE_TYPE (convs), expr, CONV_IMPLICIT, |
3108 | LOOKUP_NORMAL|LOOKUP_NO_CONVERSION); | |
c73964b2 MS |
3109 | } |
3110 | ||
3111 | static tree | |
3112 | convert_default_arg (type, arg) | |
3113 | tree type, arg; | |
3114 | { | |
3115 | arg = break_out_target_exprs (arg); | |
3116 | ||
3117 | if (TREE_CODE (arg) == CONSTRUCTOR) | |
3118 | { | |
3119 | arg = digest_init (type, arg, 0); | |
3120 | arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL, | |
3121 | "default argument", 0, 0); | |
3122 | } | |
3123 | else | |
3124 | { | |
3125 | /* This could get clobbered by the following call. */ | |
3126 | if (TREE_HAS_CONSTRUCTOR (arg)) | |
3127 | arg = copy_node (arg); | |
3128 | ||
3129 | arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL, | |
3130 | "default argument", 0, 0); | |
3131 | #ifdef PROMOTE_PROTOTYPES | |
3132 | if ((TREE_CODE (type) == INTEGER_TYPE | |
3133 | || TREE_CODE (type) == ENUMERAL_TYPE) | |
3134 | && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))) | |
3135 | arg = default_conversion (arg); | |
3136 | #endif | |
3137 | } | |
3138 | ||
3139 | return arg; | |
3140 | } | |
3141 | ||
3142 | static tree | |
5ffe581d JM |
3143 | build_over_call (cand, args, flags) |
3144 | struct z_candidate *cand; | |
3145 | tree args; | |
c73964b2 MS |
3146 | int flags; |
3147 | { | |
5ffe581d JM |
3148 | tree fn = cand->fn; |
3149 | tree convs = cand->convs; | |
c73964b2 MS |
3150 | tree converted_args = NULL_TREE; |
3151 | tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn)); | |
3152 | tree conv, arg, val; | |
3153 | int i = 0; | |
d11ad92e | 3154 | int is_method = 0; |
c73964b2 | 3155 | |
5ffe581d JM |
3156 | /* Give any warnings we noticed during overload resolution. */ |
3157 | if (cand->warnings) | |
3158 | for (val = cand->warnings; val; val = TREE_CHAIN (val)) | |
3159 | joust (cand, WRAPPER_PTR (TREE_VALUE (val)), 1); | |
3160 | ||
3161 | if (DECL_FUNCTION_MEMBER_P (fn)) | |
3162 | enforce_access (cand->basetype_path, fn); | |
3163 | ||
c73964b2 | 3164 | if (args && TREE_CODE (args) != TREE_LIST) |
e66d884e | 3165 | args = build_scratch_list (NULL_TREE, args); |
c73964b2 MS |
3166 | arg = args; |
3167 | ||
3168 | /* The implicit parameters to a constructor are not considered by overload | |
3169 | resolution, and must be of the proper type. */ | |
3170 | if (DECL_CONSTRUCTOR_P (fn)) | |
3171 | { | |
e66d884e | 3172 | converted_args = expr_tree_cons (NULL_TREE, TREE_VALUE (arg), converted_args); |
c73964b2 MS |
3173 | arg = TREE_CHAIN (arg); |
3174 | parm = TREE_CHAIN (parm); | |
3175 | if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn))) | |
3176 | { | |
e66d884e | 3177 | converted_args = expr_tree_cons |
c73964b2 MS |
3178 | (NULL_TREE, TREE_VALUE (arg), converted_args); |
3179 | arg = TREE_CHAIN (arg); | |
3180 | parm = TREE_CHAIN (parm); | |
3181 | } | |
3182 | } | |
3183 | /* Bypass access control for 'this' parameter. */ | |
3184 | else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE) | |
3185 | { | |
d11ad92e MS |
3186 | tree parmtype = TREE_VALUE (parm); |
3187 | tree argtype = TREE_TYPE (TREE_VALUE (arg)); | |
3188 | if (ICS_BAD_FLAG (TREE_VEC_ELT (convs, i))) | |
3189 | { | |
3190 | int dv = (TYPE_VOLATILE (TREE_TYPE (parmtype)) | |
3191 | < TYPE_VOLATILE (TREE_TYPE (argtype))); | |
3192 | int dc = (TYPE_READONLY (TREE_TYPE (parmtype)) | |
3193 | < TYPE_READONLY (TREE_TYPE (argtype))); | |
beb53fb8 JM |
3194 | char *p = (dv && dc ? "const and volatile" |
3195 | : dc ? "const" : dv ? "volatile" : ""); | |
d11ad92e MS |
3196 | |
3197 | cp_pedwarn ("passing `%T' as `this' argument of `%#D' discards %s", | |
3198 | TREE_TYPE (argtype), fn, p); | |
3199 | } | |
e66d884e | 3200 | converted_args = expr_tree_cons |
c73964b2 MS |
3201 | (NULL_TREE, convert_force (TREE_VALUE (parm), TREE_VALUE (arg), CONV_C_CAST), |
3202 | converted_args); | |
3203 | parm = TREE_CHAIN (parm); | |
3204 | arg = TREE_CHAIN (arg); | |
3205 | ++i; | |
d11ad92e | 3206 | is_method = 1; |
c73964b2 MS |
3207 | } |
3208 | ||
eb66be0e | 3209 | for (; arg && parm; |
c73964b2 MS |
3210 | parm = TREE_CHAIN (parm), arg = TREE_CHAIN (arg), ++i) |
3211 | { | |
3212 | tree type = TREE_VALUE (parm); | |
d11ad92e | 3213 | |
eb66be0e | 3214 | conv = TREE_VEC_ELT (convs, i); |
d11ad92e MS |
3215 | if (ICS_BAD_FLAG (conv)) |
3216 | { | |
3217 | tree t = conv; | |
3218 | val = TREE_VALUE (arg); | |
3219 | ||
3220 | for (; t; t = TREE_OPERAND (t, 0)) | |
3221 | { | |
3222 | if (TREE_CODE (t) == USER_CONV | |
3223 | || TREE_CODE (t) == AMBIG_CONV) | |
3224 | { | |
3225 | val = convert_like (t, val); | |
3226 | break; | |
3227 | } | |
3228 | else if (TREE_CODE (t) == IDENTITY_CONV) | |
3229 | break; | |
3230 | } | |
3231 | val = convert_for_initialization | |
3232 | (NULL_TREE, type, val, LOOKUP_NORMAL, | |
3233 | "argument passing", fn, i - is_method); | |
3234 | } | |
3235 | else | |
3236 | val = convert_like (conv, TREE_VALUE (arg)); | |
c73964b2 MS |
3237 | |
3238 | #ifdef PROMOTE_PROTOTYPES | |
3239 | if ((TREE_CODE (type) == INTEGER_TYPE | |
3240 | || TREE_CODE (type) == ENUMERAL_TYPE) | |
3241 | && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))) | |
3242 | val = default_conversion (val); | |
3243 | #endif | |
e66d884e | 3244 | converted_args = expr_tree_cons (NULL_TREE, val, converted_args); |
c73964b2 MS |
3245 | } |
3246 | ||
3247 | /* Default arguments */ | |
3248 | for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm)) | |
de22184b MS |
3249 | { |
3250 | tree arg = TREE_PURPOSE (parm); | |
3251 | ||
86e6f22f | 3252 | if (DECL_TEMPLATE_INFO (fn)) |
59581da6 MM |
3253 | { |
3254 | /* This came from a template. Instantiate the default arg here, | |
3255 | not in tsubst. In the case of something like: | |
3256 | ||
3257 | template <class T> | |
3258 | struct S { | |
3259 | static T t(); | |
3260 | void f(T = t()); | |
3261 | }; | |
3262 | ||
3263 | we must be careful to do name lookup in the scope of | |
3264 | S<T>, rather than in the current class. */ | |
3265 | if (DECL_REAL_CONTEXT (fn)) | |
3266 | pushclass (DECL_REAL_CONTEXT (fn), 2); | |
3267 | ||
3268 | arg = tsubst_expr (arg, DECL_TI_ARGS (fn), NULL_TREE); | |
3269 | ||
3270 | if (DECL_REAL_CONTEXT (fn)) | |
3271 | popclass (0); | |
3272 | } | |
e66d884e | 3273 | converted_args = expr_tree_cons |
de22184b MS |
3274 | (NULL_TREE, convert_default_arg (TREE_VALUE (parm), arg), |
3275 | converted_args); | |
3276 | } | |
c73964b2 MS |
3277 | |
3278 | /* Ellipsis */ | |
3279 | for (; arg; arg = TREE_CHAIN (arg)) | |
3280 | { | |
3281 | val = TREE_VALUE (arg); | |
3282 | ||
3283 | if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE | |
3284 | && (TYPE_PRECISION (TREE_TYPE (val)) | |
3285 | < TYPE_PRECISION (double_type_node))) | |
3286 | /* Convert `float' to `double'. */ | |
37c46b43 | 3287 | val = cp_convert (double_type_node, val); |
b3e4ff59 | 3288 | else if (IS_AGGR_TYPE (TREE_TYPE (val)) |
c73964b2 MS |
3289 | && ! TYPE_HAS_TRIVIAL_INIT_REF (TREE_TYPE (val))) |
3290 | cp_warning ("cannot pass objects of type `%T' through `...'", | |
3291 | TREE_TYPE (val)); | |
3292 | else | |
3293 | /* Convert `short' and `char' to full-size `int'. */ | |
3294 | val = default_conversion (val); | |
3295 | ||
e66d884e | 3296 | converted_args = expr_tree_cons (NULL_TREE, val, converted_args); |
c73964b2 MS |
3297 | } |
3298 | ||
3299 | converted_args = nreverse (converted_args); | |
3300 | ||
c11b6f21 MS |
3301 | /* Avoid actually calling copy constructors and copy assignment operators, |
3302 | if possible. */ | |
3303 | if (DECL_CONSTRUCTOR_P (fn) | |
3304 | && TREE_VEC_LENGTH (convs) == 1 | |
3305 | && copy_args_p (fn)) | |
3306 | { | |
eb66be0e | 3307 | tree targ; |
67437d5b JM |
3308 | arg = TREE_CHAIN (converted_args); |
3309 | if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn))) | |
3310 | arg = TREE_CHAIN (arg); | |
3311 | arg = TREE_VALUE (arg); | |
c11b6f21 MS |
3312 | |
3313 | /* Pull out the real argument, disregarding const-correctness. */ | |
eb66be0e MS |
3314 | targ = arg; |
3315 | while (TREE_CODE (targ) == NOP_EXPR | |
3316 | || TREE_CODE (targ) == NON_LVALUE_EXPR | |
3317 | || TREE_CODE (targ) == CONVERT_EXPR) | |
3318 | targ = TREE_OPERAND (targ, 0); | |
3319 | if (TREE_CODE (targ) == ADDR_EXPR) | |
3320 | { | |
3321 | targ = TREE_OPERAND (targ, 0); | |
c11b6f21 MS |
3322 | if (! comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg))), |
3323 | TYPE_MAIN_VARIANT (TREE_TYPE (targ)), 1)) | |
3324 | targ = NULL_TREE; | |
3325 | } | |
eb66be0e MS |
3326 | else |
3327 | targ = NULL_TREE; | |
c11b6f21 MS |
3328 | |
3329 | if (targ) | |
3330 | arg = targ; | |
3331 | else | |
3332 | arg = build_indirect_ref (arg, 0); | |
3333 | ||
bd6dd845 MS |
3334 | /* [class.copy]: the copy constructor is implicitly defined even if |
3335 | the implementation elided its use. */ | |
3336 | if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn))) | |
3337 | mark_used (fn); | |
3338 | ||
c11b6f21 MS |
3339 | /* If we're creating a temp and we already have one, don't create a |
3340 | new one. If we're not creating a temp but we get one, use | |
3341 | INIT_EXPR to collapse the temp into our target. Otherwise, if the | |
3342 | ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a | |
3343 | temp or an INIT_EXPR otherwise. */ | |
3344 | if (integer_zerop (TREE_VALUE (args))) | |
3345 | { | |
3346 | if (! real_lvalue_p (arg)) | |
3347 | return arg; | |
3348 | else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn))) | |
3349 | { | |
3350 | val = build (VAR_DECL, DECL_CONTEXT (fn)); | |
3351 | layout_decl (val, 0); | |
691c003d MS |
3352 | val = build (TARGET_EXPR, DECL_CONTEXT (fn), val, arg, 0, 0); |
3353 | TREE_SIDE_EFFECTS (val) = 1; | |
3354 | return val; | |
c11b6f21 MS |
3355 | } |
3356 | } | |
3357 | else if (! real_lvalue_p (arg) | |
3358 | || TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn))) | |
3359 | { | |
3360 | tree to = stabilize_reference | |
3361 | (build_indirect_ref (TREE_VALUE (args), 0)); | |
a59ca936 JM |
3362 | |
3363 | /* Don't copy the padding byte; it might not have been allocated | |
3364 | if to is a base subobject. */ | |
3365 | if (is_empty_class (DECL_CLASS_CONTEXT (fn))) | |
959d8796 JM |
3366 | return build (COMPOUND_EXPR, TREE_TYPE (to), |
3367 | cp_convert (void_type_node, arg), to); | |
a59ca936 | 3368 | |
c11b6f21 MS |
3369 | val = build (INIT_EXPR, DECL_CONTEXT (fn), to, arg); |
3370 | TREE_SIDE_EFFECTS (val) = 1; | |
3371 | return build_unary_op (ADDR_EXPR, val, 0); | |
3372 | } | |
3373 | } | |
3374 | else if (DECL_NAME (fn) == ansi_opname[MODIFY_EXPR] | |
3375 | && copy_args_p (fn) | |
a59ca936 | 3376 | && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CLASS_CONTEXT (fn))) |
c11b6f21 MS |
3377 | { |
3378 | tree to = stabilize_reference | |
3379 | (build_indirect_ref (TREE_VALUE (converted_args), 0)); | |
a59ca936 | 3380 | |
959d8796 JM |
3381 | arg = build_indirect_ref (TREE_VALUE (TREE_CHAIN (converted_args)), 0); |
3382 | ||
a59ca936 JM |
3383 | /* Don't copy the padding byte; it might not have been allocated |
3384 | if to is a base subobject. */ | |
3385 | if (is_empty_class (DECL_CLASS_CONTEXT (fn))) | |
959d8796 JM |
3386 | return build (COMPOUND_EXPR, TREE_TYPE (to), |
3387 | cp_convert (void_type_node, arg), to); | |
a59ca936 | 3388 | |
c11b6f21 MS |
3389 | val = build (MODIFY_EXPR, TREE_TYPE (to), to, arg); |
3390 | TREE_SIDE_EFFECTS (val) = 1; | |
3391 | return val; | |
3392 | } | |
3393 | ||
bd6dd845 MS |
3394 | mark_used (fn); |
3395 | ||
c11b6f21 MS |
3396 | if (DECL_CONTEXT (fn) && IS_SIGNATURE (DECL_CONTEXT (fn))) |
3397 | return build_signature_method_call (fn, converted_args); | |
3398 | else if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0) | |
c73964b2 MS |
3399 | { |
3400 | tree t, *p = &TREE_VALUE (converted_args); | |
3401 | tree binfo = get_binfo | |
3402 | (DECL_CONTEXT (fn), TREE_TYPE (TREE_TYPE (*p)), 0); | |
3403 | *p = convert_pointer_to_real (binfo, *p); | |
3404 | if (TREE_SIDE_EFFECTS (*p)) | |
3405 | *p = save_expr (*p); | |
3406 | t = build_pointer_type (TREE_TYPE (fn)); | |
3407 | fn = build_vfn_ref (p, build_indirect_ref (*p, 0), DECL_VINDEX (fn)); | |
3408 | TREE_TYPE (fn) = t; | |
3409 | } | |
3410 | else if (DECL_INLINE (fn)) | |
3411 | fn = inline_conversion (fn); | |
3412 | else | |
3413 | fn = build_addr_func (fn); | |
3414 | ||
387c47a3 AS |
3415 | /* Recognize certain built-in functions so we can make tree-codes |
3416 | other than CALL_EXPR. We do this when it enables fold-const.c | |
3417 | to do something useful. */ | |
3418 | ||
3419 | if (TREE_CODE (fn) == ADDR_EXPR | |
3420 | && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL | |
3421 | && DECL_BUILT_IN (TREE_OPERAND (fn, 0))) | |
3422 | switch (DECL_FUNCTION_CODE (TREE_OPERAND (fn, 0))) | |
3423 | { | |
3424 | case BUILT_IN_ABS: | |
3425 | case BUILT_IN_LABS: | |
3426 | case BUILT_IN_FABS: | |
3427 | if (converted_args == 0) | |
3428 | return integer_zero_node; | |
3429 | return build_unary_op (ABS_EXPR, TREE_VALUE (converted_args), 0); | |
4fce8e83 MH |
3430 | default: |
3431 | break; | |
387c47a3 AS |
3432 | } |
3433 | ||
c73964b2 MS |
3434 | fn = build_call (fn, TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))), converted_args); |
3435 | if (TREE_TYPE (fn) == void_type_node) | |
3436 | return fn; | |
b82b76c6 | 3437 | fn = require_complete_type (fn); |
c73964b2 MS |
3438 | if (IS_AGGR_TYPE (TREE_TYPE (fn))) |
3439 | fn = build_cplus_new (TREE_TYPE (fn), fn); | |
b82b76c6 | 3440 | return convert_from_reference (fn); |
c73964b2 MS |
3441 | } |
3442 | ||
bd6dd845 | 3443 | static tree |
c73964b2 MS |
3444 | build_new_method_call (instance, name, args, basetype_path, flags) |
3445 | tree instance, name, args, basetype_path; | |
3446 | int flags; | |
3447 | { | |
3448 | struct z_candidate *candidates = 0, *cand; | |
386b8a85 | 3449 | tree explicit_targs = NULL_TREE; |
a703fb38 | 3450 | tree basetype, mem_args = NULL_TREE, fns, instance_ptr; |
c73964b2 | 3451 | tree pretty_name; |
824b9a4c | 3452 | tree user_args = args; |
98c1c668 | 3453 | tree templates = NULL_TREE; |
c32381b1 | 3454 | int template_only = 0; |
824b9a4c | 3455 | |
386b8a85 JM |
3456 | if (TREE_CODE (name) == TEMPLATE_ID_EXPR) |
3457 | { | |
3458 | explicit_targs = TREE_OPERAND (name, 1); | |
3459 | name = TREE_OPERAND (name, 0); | |
00d3396f JM |
3460 | if (TREE_CODE (name) == TEMPLATE_DECL) |
3461 | name = DECL_NAME (name); | |
c32381b1 | 3462 | template_only = 1; |
386b8a85 JM |
3463 | } |
3464 | ||
824b9a4c MS |
3465 | /* If there is an extra argument for controlling virtual bases, |
3466 | remove it for error reporting. */ | |
3467 | if (flags & LOOKUP_HAS_IN_CHARGE) | |
3468 | user_args = TREE_CHAIN (args); | |
c73964b2 | 3469 | |
86e6f22f JM |
3470 | args = resolve_args (args); |
3471 | ||
3472 | if (args == error_mark_node) | |
3473 | return error_mark_node; | |
d11ad92e | 3474 | |
c73964b2 MS |
3475 | if (instance == NULL_TREE) |
3476 | basetype = BINFO_TYPE (basetype_path); | |
3477 | else | |
3478 | { | |
3479 | if (TREE_CODE (instance) == OFFSET_REF) | |
3480 | instance = resolve_offset_ref (instance); | |
3481 | if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE) | |
3482 | instance = convert_from_reference (instance); | |
3483 | basetype = TREE_TYPE (instance); | |
3484 | ||
3485 | /* XXX this should be handled before we get here. */ | |
c11b6f21 MS |
3486 | if (! IS_AGGR_TYPE (basetype) |
3487 | && ! (TYPE_LANG_SPECIFIC (basetype) | |
3488 | && (IS_SIGNATURE_POINTER (basetype) | |
3489 | || IS_SIGNATURE_REFERENCE (basetype)))) | |
c73964b2 MS |
3490 | { |
3491 | if ((flags & LOOKUP_COMPLAIN) && basetype != error_mark_node) | |
3492 | cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'", | |
3493 | name, instance, basetype); | |
3494 | ||
3495 | return error_mark_node; | |
3496 | } | |
c11b6f21 MS |
3497 | |
3498 | /* If `instance' is a signature pointer/reference and `name' is | |
3499 | not a constructor, we are calling a signature member function. | |
3500 | In that case set the `basetype' to the signature type. */ | |
3501 | if ((IS_SIGNATURE_POINTER (basetype) | |
3502 | || IS_SIGNATURE_REFERENCE (basetype)) | |
3503 | && TYPE_IDENTIFIER (basetype) != name) | |
3504 | basetype = SIGNATURE_TYPE (basetype); | |
c73964b2 MS |
3505 | } |
3506 | ||
3507 | if (basetype_path == NULL_TREE) | |
3508 | basetype_path = TYPE_BINFO (basetype); | |
3509 | ||
3510 | if (instance) | |
3511 | { | |
3512 | instance_ptr = build_this (instance); | |
3513 | ||
00d3396f JM |
3514 | if (! template_only) |
3515 | { | |
3516 | /* XXX this should be handled before we get here. */ | |
3517 | fns = build_field_call (basetype_path, instance_ptr, name, args); | |
3518 | if (fns) | |
3519 | return fns; | |
3520 | } | |
c73964b2 MS |
3521 | } |
3522 | else | |
3523 | { | |
3524 | instance_ptr = build_int_2 (0, 0); | |
3525 | TREE_TYPE (instance_ptr) = build_pointer_type (basetype); | |
3526 | } | |
3527 | ||
beb53fb8 | 3528 | pretty_name |
fee23f54 | 3529 | = (name == ctor_identifier ? constructor_name (basetype) : name); |
c73964b2 MS |
3530 | |
3531 | fns = lookup_fnfields (basetype_path, name, 1); | |
3532 | ||
3533 | if (fns == error_mark_node) | |
3534 | return error_mark_node; | |
3535 | if (fns) | |
3536 | { | |
3537 | tree t = TREE_VALUE (fns); | |
3538 | if (name == ctor_identifier && TYPE_USES_VIRTUAL_BASECLASSES (basetype) | |
3539 | && ! (flags & LOOKUP_HAS_IN_CHARGE)) | |
3540 | { | |
3541 | flags |= LOOKUP_HAS_IN_CHARGE; | |
e66d884e | 3542 | args = scratch_tree_cons (NULL_TREE, integer_one_node, args); |
c73964b2 | 3543 | } |
e66d884e | 3544 | mem_args = scratch_tree_cons (NULL_TREE, instance_ptr, args); |
c73964b2 MS |
3545 | for (; t; t = DECL_CHAIN (t)) |
3546 | { | |
98c1c668 JM |
3547 | tree this_arglist; |
3548 | ||
faf5394a | 3549 | /* We can end up here for copy-init of same or base class. */ |
c73964b2 MS |
3550 | if (name == ctor_identifier |
3551 | && (flags & LOOKUP_ONLYCONVERTING) | |
3552 | && DECL_NONCONVERTING_P (t)) | |
3553 | continue; | |
3554 | if (TREE_CODE (TREE_TYPE (t)) == METHOD_TYPE) | |
98c1c668 | 3555 | this_arglist = mem_args; |
c73964b2 | 3556 | else |
98c1c668 JM |
3557 | this_arglist = args; |
3558 | ||
3559 | if (TREE_CODE (t) == TEMPLATE_DECL) | |
3560 | { | |
3561 | /* A member template. */ | |
e66d884e | 3562 | templates = scratch_tree_cons (NULL_TREE, t, templates); |
98c1c668 | 3563 | candidates = |
386b8a85 | 3564 | add_template_candidate (candidates, t, explicit_targs, |
98c1c668 | 3565 | this_arglist, |
1f109f0f | 3566 | TREE_TYPE (name), flags); |
98c1c668 | 3567 | } |
c32381b1 | 3568 | else if (! template_only) |
98c1c668 JM |
3569 | candidates = add_function_candidate (candidates, t, |
3570 | this_arglist, flags); | |
3571 | ||
3572 | if (candidates) | |
3573 | candidates->basetype_path = TREE_PURPOSE (fns); | |
c73964b2 MS |
3574 | } |
3575 | } | |
3576 | ||
3577 | if (! any_viable (candidates)) | |
3578 | { | |
3579 | /* XXX will LOOKUP_SPECULATIVELY be needed when this is done? */ | |
3580 | if (flags & LOOKUP_SPECULATIVELY) | |
3581 | return NULL_TREE; | |
3582 | cp_error ("no matching function for call to `%T::%D (%A)%V'", basetype, | |
824b9a4c | 3583 | pretty_name, user_args, TREE_TYPE (TREE_TYPE (instance_ptr))); |
c73964b2 MS |
3584 | print_z_candidates (candidates); |
3585 | return error_mark_node; | |
3586 | } | |
3587 | candidates = splice_viable (candidates); | |
da20811c | 3588 | cand = tourney (candidates); |
c73964b2 MS |
3589 | |
3590 | if (cand == 0) | |
3591 | { | |
824b9a4c MS |
3592 | cp_error ("call of overloaded `%D(%A)' is ambiguous", pretty_name, |
3593 | user_args); | |
c73964b2 MS |
3594 | print_z_candidates (candidates); |
3595 | return error_mark_node; | |
3596 | } | |
3597 | ||
c73964b2 MS |
3598 | if (DECL_ABSTRACT_VIRTUAL_P (cand->fn) |
3599 | && instance == current_class_ref | |
3600 | && DECL_CONSTRUCTOR_P (current_function_decl) | |
3601 | && ! (flags & LOOKUP_NONVIRTUAL) | |
3602 | && value_member (cand->fn, get_abstract_virtuals (basetype))) | |
3603 | cp_error ("abstract virtual `%#D' called from constructor", cand->fn); | |
3604 | if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE | |
3605 | && TREE_CODE (instance_ptr) == NOP_EXPR | |
3606 | && TREE_OPERAND (instance_ptr, 0) == error_mark_node) | |
3607 | cp_error ("cannot call member function `%D' without object", cand->fn); | |
3608 | ||
3609 | if (DECL_VINDEX (cand->fn) && ! (flags & LOOKUP_NONVIRTUAL) | |
3610 | && ((instance == current_class_ref && (dtor_label || ctor_label)) | |
3611 | || resolves_to_fixed_type_p (instance, 0))) | |
3612 | flags |= LOOKUP_NONVIRTUAL; | |
3613 | ||
98c1c668 | 3614 | /* Pedantically, normal function declarations are never considered |
386b8a85 JM |
3615 | to refer to template instantiations, so we only do this with |
3616 | -fguiding-decls. */ | |
3617 | if (flag_guiding_decls && templates && ! cand->template | |
3618 | && ! DECL_INITIAL (cand->fn)) | |
98c1c668 JM |
3619 | add_maybe_template (cand->fn, templates); |
3620 | ||
c73964b2 | 3621 | return build_over_call |
5ffe581d | 3622 | (cand, |
c73964b2 MS |
3623 | TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE ? mem_args : args, |
3624 | flags); | |
3625 | } | |
3626 | ||
3627 | /* Compare two implicit conversion sequences that differ only in their | |
3628 | qualification conversion. Subroutine of compare_ics. */ | |
3629 | ||
3630 | static int | |
3631 | compare_qual (ics1, ics2) | |
3632 | tree ics1, ics2; | |
3633 | { | |
3634 | tree to1 = TREE_TYPE (ics1); | |
3635 | tree to2 = TREE_TYPE (ics2); | |
3636 | ||
549121cd JM |
3637 | if (TYPE_PTRMEMFUNC_P (to1)) |
3638 | to1 = TYPE_PTRMEMFUNC_FN_TYPE (to1); | |
3639 | if (TYPE_PTRMEMFUNC_P (to2)) | |
3640 | to2 = TYPE_PTRMEMFUNC_FN_TYPE (to2); | |
3641 | ||
c73964b2 MS |
3642 | to1 = TREE_TYPE (to1); |
3643 | to2 = TREE_TYPE (to2); | |
3644 | ||
3645 | if (TREE_CODE (to1) == OFFSET_TYPE) | |
3646 | { | |
3647 | to1 = TREE_TYPE (to1); | |
3648 | to2 = TREE_TYPE (to2); | |
3649 | } | |
3650 | ||
3651 | if (TYPE_READONLY (to1) >= TYPE_READONLY (to2) | |
3652 | && TYPE_VOLATILE (to1) > TYPE_VOLATILE (to2)) | |
3653 | return -1; | |
3654 | else if (TYPE_READONLY (to1) > TYPE_READONLY (to2) | |
3655 | && TYPE_VOLATILE (to1) == TYPE_VOLATILE (to2)) | |
3656 | return -1; | |
3657 | else if (TYPE_READONLY (to1) <= TYPE_READONLY (to2) | |
3658 | && TYPE_VOLATILE (to1) < TYPE_VOLATILE (to2)) | |
3659 | return 1; | |
3660 | else if (TYPE_READONLY (to1) < TYPE_READONLY (to2) | |
3661 | && TYPE_VOLATILE (to1) == TYPE_VOLATILE (to2)) | |
3662 | return 1; | |
3663 | return 0; | |
3664 | } | |
3665 | ||
f62dbf03 JM |
3666 | /* Determine whether standard conversion sequence ICS1 is a proper |
3667 | subsequence of ICS2. We assume that a conversion of the same code | |
3668 | between the same types indicates a subsequence. */ | |
3669 | ||
3670 | static int | |
3671 | is_subseq (ics1, ics2) | |
3672 | tree ics1, ics2; | |
3673 | { | |
653cc74a JM |
3674 | /* Do not consider lvalue transformations here. */ |
3675 | if (TREE_CODE (ics2) == RVALUE_CONV | |
3676 | || TREE_CODE (ics2) == LVALUE_CONV) | |
3677 | return 0; | |
3678 | ||
49c249e1 | 3679 | for (;; ics2 = TREE_OPERAND (ics2, 0)) |
f62dbf03 | 3680 | { |
f62dbf03 JM |
3681 | if (TREE_CODE (ics2) == TREE_CODE (ics1) |
3682 | && comptypes (TREE_TYPE (ics2), TREE_TYPE (ics1), 1) | |
3683 | && comptypes (TREE_TYPE (TREE_OPERAND (ics2, 0)), | |
3684 | TREE_TYPE (TREE_OPERAND (ics1, 0)), 1)) | |
3685 | return 1; | |
3686 | ||
3687 | if (TREE_CODE (ics2) == USER_CONV | |
3688 | || TREE_CODE (ics2) == AMBIG_CONV | |
3689 | || TREE_CODE (ics2) == IDENTITY_CONV) | |
3690 | return 0; | |
3691 | } | |
3692 | } | |
3693 | ||
c73964b2 MS |
3694 | /* Compare two implicit conversion sequences according to the rules set out in |
3695 | [over.ics.rank]. Return values: | |
3696 | ||
3697 | 1: ics1 is better than ics2 | |
3698 | -1: ics2 is better than ics1 | |
3699 | 0: ics1 and ics2 are indistinguishable */ | |
3700 | ||
3701 | static int | |
3702 | compare_ics (ics1, ics2) | |
3703 | tree ics1, ics2; | |
3704 | { | |
3705 | tree main1, main2; | |
3706 | ||
d11ad92e MS |
3707 | if (TREE_CODE (ics1) == QUAL_CONV) |
3708 | main1 = TREE_OPERAND (ics1, 0); | |
3709 | else | |
3710 | main1 = ics1; | |
3711 | ||
3712 | if (TREE_CODE (ics2) == QUAL_CONV) | |
3713 | main2 = TREE_OPERAND (ics2, 0); | |
3714 | else | |
3715 | main2 = ics2; | |
3716 | ||
3717 | /* Conversions for `this' are PTR_CONVs, but we compare them as though | |
3718 | they were REF_BINDs. */ | |
3719 | if (ICS_THIS_FLAG (ics1)) | |
3720 | { | |
6633d636 MS |
3721 | tree t = main1; |
3722 | if (TREE_CODE (t) == PTR_CONV) | |
3723 | t = TREE_OPERAND (t, 0); | |
3724 | t = build1 (IDENTITY_CONV, TREE_TYPE (TREE_TYPE (t)), NULL_TREE); | |
3725 | t = build_conv (REF_BIND, TREE_TYPE (ics1), t); | |
3726 | ICS_STD_RANK (t) = ICS_STD_RANK (main1); | |
3727 | main1 = ics1 = t; | |
d11ad92e MS |
3728 | } |
3729 | if (ICS_THIS_FLAG (ics2)) | |
3730 | { | |
6633d636 MS |
3731 | tree t = main2; |
3732 | if (TREE_CODE (t) == PTR_CONV) | |
3733 | t = TREE_OPERAND (t, 0); | |
3734 | t = build1 (IDENTITY_CONV, TREE_TYPE (TREE_TYPE (t)), NULL_TREE); | |
3735 | t = build_conv (REF_BIND, TREE_TYPE (ics2), t); | |
3736 | ICS_STD_RANK (t) = ICS_STD_RANK (main2); | |
3737 | main2 = ics2 = t; | |
d11ad92e MS |
3738 | } |
3739 | ||
c73964b2 MS |
3740 | if (ICS_RANK (ics1) > ICS_RANK (ics2)) |
3741 | return -1; | |
3742 | else if (ICS_RANK (ics1) < ICS_RANK (ics2)) | |
3743 | return 1; | |
3744 | ||
d11ad92e MS |
3745 | if (ICS_RANK (ics1) == BAD_RANK) |
3746 | { | |
3747 | if (ICS_USER_FLAG (ics1) > ICS_USER_FLAG (ics2) | |
3748 | || ICS_STD_RANK (ics1) > ICS_STD_RANK (ics2)) | |
3749 | return -1; | |
3750 | else if (ICS_USER_FLAG (ics1) < ICS_USER_FLAG (ics2) | |
3751 | || ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2)) | |
3752 | return 1; | |
3753 | ||
3754 | /* else fall through */ | |
3755 | } | |
3756 | ||
c73964b2 MS |
3757 | /* User-defined conversion sequence U1 is a better conversion sequence |
3758 | than another user-defined conversion sequence U2 if they contain the | |
3759 | same user-defined conversion operator or constructor and if the sec- | |
3760 | ond standard conversion sequence of U1 is better than the second | |
3761 | standard conversion sequence of U2. */ | |
3762 | ||
d11ad92e | 3763 | if (ICS_USER_FLAG (ics1)) |
c73964b2 MS |
3764 | { |
3765 | tree t1, t2; | |
3766 | ||
3767 | for (t1 = ics1; TREE_CODE (t1) != USER_CONV; t1 = TREE_OPERAND (t1, 0)) | |
3768 | if (TREE_CODE (t1) == AMBIG_CONV) | |
3769 | return 0; | |
3770 | for (t2 = ics2; TREE_CODE (t2) != USER_CONV; t2 = TREE_OPERAND (t2, 0)) | |
3771 | if (TREE_CODE (t2) == AMBIG_CONV) | |
3772 | return 0; | |
3773 | ||
3774 | if (USER_CONV_FN (t1) != USER_CONV_FN (t2)) | |
3775 | return 0; | |
3776 | else if (ICS_STD_RANK (ics1) > ICS_STD_RANK (ics2)) | |
3777 | return -1; | |
3778 | else if (ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2)) | |
3779 | return 1; | |
3780 | ||
3781 | /* else fall through */ | |
3782 | } | |
3783 | ||
3784 | #if 0 /* Handled by ranking */ | |
3785 | /* A conversion that is not a conversion of a pointer, or pointer to | |
3786 | member, to bool is better than another conversion that is such a | |
3787 | conversion. */ | |
3788 | #endif | |
3789 | ||
c73964b2 | 3790 | if (TREE_CODE (main1) != TREE_CODE (main2)) |
f62dbf03 JM |
3791 | { |
3792 | /* ...if S1 is a proper subsequence of S2 */ | |
3793 | if (is_subseq (main1, main2)) | |
3794 | return 1; | |
3795 | if (is_subseq (main2, main1)) | |
3796 | return -1; | |
3797 | return 0; | |
3798 | } | |
c73964b2 MS |
3799 | |
3800 | if (TREE_CODE (main1) == PTR_CONV || TREE_CODE (main1) == PMEM_CONV | |
3801 | || TREE_CODE (main1) == REF_BIND || TREE_CODE (main1) == BASE_CONV) | |
3802 | { | |
3803 | tree to1 = TREE_TYPE (main1); | |
3804 | tree from1 = TREE_TYPE (TREE_OPERAND (main1, 0)); | |
3805 | tree to2 = TREE_TYPE (main2); | |
3806 | tree from2 = TREE_TYPE (TREE_OPERAND (main2, 0)); | |
3807 | int distf, distt; | |
3808 | ||
3809 | /* Standard conversion sequence S1 is a better conversion sequence than | |
3810 | standard conversion sequence S2 if... | |
3811 | ||
3812 | S1 and S2 differ only in their qualification conversion and they | |
3813 | yield types identical except for cv-qualifiers and S2 adds all the | |
3814 | qualifiers that S1 adds (and in the same places) and S2 adds yet | |
3815 | more cv-qualifiers than S1, or the similar case with reference | |
3816 | binding15). */ | |
3817 | if (TREE_CODE (main1) == REF_BIND) | |
3818 | { | |
3819 | if (TYPE_MAIN_VARIANT (TREE_TYPE (to1)) | |
3820 | == TYPE_MAIN_VARIANT (TREE_TYPE (to2))) | |
3821 | return compare_qual (ics1, ics2); | |
3822 | } | |
3823 | else if (TREE_CODE (main1) != BASE_CONV && from1 == from2 && to1 == to2) | |
3824 | return compare_qual (ics1, ics2); | |
3825 | ||
3826 | if (TYPE_PTRMEMFUNC_P (to1)) | |
3827 | { | |
8e69329a JM |
3828 | to1 = TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to1))); |
3829 | from1 = TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from1))); | |
c73964b2 MS |
3830 | } |
3831 | else if (TREE_CODE (main1) != BASE_CONV) | |
3832 | { | |
3833 | to1 = TREE_TYPE (to1); | |
3834 | if (TREE_CODE (main1) != REF_BIND) | |
3835 | from1 = TREE_TYPE (from1); | |
3836 | ||
3837 | if (TREE_CODE (to1) == OFFSET_TYPE) | |
3838 | { | |
3839 | to1 = TYPE_OFFSET_BASETYPE (to1); | |
3840 | from1 = TYPE_OFFSET_BASETYPE (from1); | |
3841 | } | |
3842 | } | |
3843 | ||
3844 | if (TYPE_PTRMEMFUNC_P (to2)) | |
3845 | { | |
8e69329a JM |
3846 | to2 = TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to2))); |
3847 | from2 = TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from2))); | |
c73964b2 MS |
3848 | } |
3849 | else if (TREE_CODE (main1) != BASE_CONV) | |
3850 | { | |
3851 | to2 = TREE_TYPE (to2); | |
3852 | if (TREE_CODE (main1) != REF_BIND) | |
3853 | from2 = TREE_TYPE (from2); | |
3854 | ||
3855 | if (TREE_CODE (to2) == OFFSET_TYPE) | |
3856 | { | |
3857 | to2 = TYPE_OFFSET_BASETYPE (to2); | |
3858 | from2 = TYPE_OFFSET_BASETYPE (from2); | |
3859 | } | |
3860 | } | |
3861 | ||
3862 | if (! (IS_AGGR_TYPE (from1) && IS_AGGR_TYPE (from2))) | |
3863 | return 0; | |
3864 | ||
3865 | /* The sense of pmem conversions is reversed from that of the other | |
3866 | conversions. */ | |
3867 | if (TREE_CODE (main1) == PMEM_CONV) | |
3868 | { | |
3869 | tree t = from1; from1 = from2; from2 = t; | |
3870 | t = to1; to1 = to2; to2 = t; | |
3871 | } | |
3872 | ||
3873 | distf = get_base_distance (from1, from2, 0, 0); | |
3874 | if (distf == -1) | |
3875 | { | |
3876 | distf = -get_base_distance (from2, from1, 0, 0); | |
3877 | if (distf == 1) | |
3878 | return 0; | |
3879 | } | |
3880 | ||
3881 | /* If class B is derived directly or indirectly from class A, | |
3882 | conver- sion of B* to A* is better than conversion of B* to | |
3883 | void*, and conversion of A* to void* is better than | |
3884 | conversion of B* to void*. */ | |
3885 | ||
3886 | if (TREE_CODE (to1) == VOID_TYPE && TREE_CODE (to2) == VOID_TYPE) | |
3887 | { | |
3888 | if (distf > 0) | |
3889 | return 1; | |
3890 | else if (distf < 0) | |
3891 | return -1; | |
3892 | } | |
3893 | else if (TREE_CODE (to2) == VOID_TYPE && IS_AGGR_TYPE (to1) | |
3894 | && get_base_distance (to1, from1, 0, 0) != -1) | |
3895 | return 1; | |
3896 | else if (TREE_CODE (to1) == VOID_TYPE && IS_AGGR_TYPE (to2) | |
3897 | && get_base_distance (to2, from2, 0, 0) != -1) | |
3898 | return -1; | |
3899 | ||
3900 | if (! (IS_AGGR_TYPE (to1) && IS_AGGR_TYPE (to2))) | |
3901 | return 0; | |
3902 | ||
3903 | /* If class B is derived directly or indirectly from class A and class | |
3904 | C is derived directly or indirectly from B */ | |
3905 | ||
3906 | distt = get_base_distance (to1, to2, 0, 0); | |
3907 | if (distt == -1) | |
3908 | { | |
3909 | distt = -get_base_distance (to2, to1, 0, 0); | |
3910 | if (distt == 1) | |
3911 | return 0; | |
3912 | } | |
3913 | ||
3914 | /* --conversion of C* to B* is better than conversion of C* to A*, */ | |
3915 | if (distf == 0) | |
3916 | { | |
3917 | if (distt > 0) | |
3918 | return -1; | |
3919 | else if (distt < 0) | |
3920 | return 1; | |
3921 | } | |
3922 | /* --conversion of B* to A* is better than conversion of C* to A*, */ | |
3923 | else if (distt == 0) | |
3924 | { | |
3925 | if (distf > 0) | |
3926 | return 1; | |
3927 | else if (distf < 0) | |
3928 | return -1; | |
3929 | } | |
3930 | } | |
3931 | else if (TREE_CODE (TREE_TYPE (main1)) == POINTER_TYPE | |
3932 | || TYPE_PTRMEMFUNC_P (TREE_TYPE (main1))) | |
3933 | { | |
3934 | if (TREE_TYPE (main1) == TREE_TYPE (main2)) | |
3935 | return compare_qual (ics1, ics2); | |
3936 | ||
3937 | #if 0 /* This is now handled by making identity better than anything else. */ | |
3938 | /* existing practice, not WP-endorsed: const char * -> const char * | |
3939 | is better than char * -> const char *. (jason 6/29/96) */ | |
3940 | if (TREE_TYPE (ics1) == TREE_TYPE (ics2)) | |
3941 | return -compare_qual (main1, main2); | |
3942 | #endif | |
3943 | } | |
3944 | ||
3945 | return 0; | |
3946 | } | |
3947 | ||
03e70705 JM |
3948 | /* The source type for this standard conversion sequence. */ |
3949 | ||
8e69329a JM |
3950 | static tree |
3951 | source_type (t) | |
3952 | tree t; | |
3953 | { | |
3954 | for (;; t = TREE_OPERAND (t, 0)) | |
3955 | { | |
3956 | if (TREE_CODE (t) == USER_CONV | |
3957 | || TREE_CODE (t) == AMBIG_CONV | |
3958 | || TREE_CODE (t) == IDENTITY_CONV) | |
3959 | return TREE_TYPE (t); | |
3960 | } | |
3961 | my_friendly_abort (1823); | |
3962 | } | |
5ffe581d JM |
3963 | |
3964 | /* Note a warning about preferring WINNER to LOSER. We do this by storing | |
3965 | a pointer to LOSER and re-running joust to produce the warning if WINNER | |
3966 | is actually used. */ | |
3967 | ||
3968 | static void | |
3969 | add_warning (winner, loser) | |
3970 | struct z_candidate *winner, *loser; | |
3971 | { | |
3972 | winner->warnings = expr_tree_cons (NULL_PTR, | |
3973 | build_expr_ptr_wrapper (loser), | |
3974 | winner->warnings); | |
3975 | } | |
8e69329a | 3976 | |
c73964b2 MS |
3977 | /* Compare two candidates for overloading as described in |
3978 | [over.match.best]. Return values: | |
3979 | ||
3980 | 1: cand1 is better than cand2 | |
3981 | -1: cand2 is better than cand1 | |
3982 | 0: cand1 and cand2 are indistinguishable */ | |
3983 | ||
3984 | static int | |
5ffe581d | 3985 | joust (cand1, cand2, warn) |
c73964b2 | 3986 | struct z_candidate *cand1, *cand2; |
5ffe581d | 3987 | int warn; |
c73964b2 MS |
3988 | { |
3989 | int winner = 0; | |
3990 | int i, off1 = 0, off2 = 0, len; | |
3991 | ||
d11ad92e MS |
3992 | /* Candidates that involve bad conversions are always worse than those |
3993 | that don't. */ | |
3994 | if (cand1->viable > cand2->viable) | |
3995 | return 1; | |
3996 | if (cand1->viable < cand2->viable) | |
3997 | return -1; | |
3998 | ||
c73964b2 MS |
3999 | /* a viable function F1 |
4000 | is defined to be a better function than another viable function F2 if | |
4001 | for all arguments i, ICSi(F1) is not a worse conversion sequence than | |
4002 | ICSi(F2), and then */ | |
4003 | ||
4004 | /* for some argument j, ICSj(F1) is a better conversion sequence than | |
4005 | ICSj(F2) */ | |
4006 | ||
4007 | /* For comparing static and non-static member functions, we ignore the | |
4008 | implicit object parameter of the non-static function. The WP says to | |
4009 | pretend that the static function has an object parm, but that won't | |
4010 | work with operator overloading. */ | |
4011 | len = TREE_VEC_LENGTH (cand1->convs); | |
4012 | if (len != TREE_VEC_LENGTH (cand2->convs)) | |
4013 | { | |
4014 | if (DECL_STATIC_FUNCTION_P (cand1->fn) | |
4015 | && ! DECL_STATIC_FUNCTION_P (cand2->fn)) | |
4016 | off2 = 1; | |
4017 | else if (! DECL_STATIC_FUNCTION_P (cand1->fn) | |
4018 | && DECL_STATIC_FUNCTION_P (cand2->fn)) | |
4019 | { | |
4020 | off1 = 1; | |
4021 | --len; | |
4022 | } | |
4023 | else | |
4024 | my_friendly_abort (42); | |
4025 | } | |
4026 | ||
4027 | for (i = 0; i < len; ++i) | |
4028 | { | |
da20811c JM |
4029 | tree t1 = TREE_VEC_ELT (cand1->convs, i+off1); |
4030 | tree t2 = TREE_VEC_ELT (cand2->convs, i+off2); | |
4031 | int comp = compare_ics (t1, t2); | |
c73964b2 MS |
4032 | |
4033 | if (comp != 0) | |
4034 | { | |
da20811c JM |
4035 | if (warn_sign_promo |
4036 | && ICS_RANK (t1) + ICS_RANK (t2) == STD_RANK + PROMO_RANK | |
4037 | && TREE_CODE (t1) == STD_CONV | |
4038 | && TREE_CODE (t2) == STD_CONV | |
4039 | && TREE_CODE (TREE_TYPE (t1)) == INTEGER_TYPE | |
4040 | && TREE_CODE (TREE_TYPE (t2)) == INTEGER_TYPE | |
4041 | && (TYPE_PRECISION (TREE_TYPE (t1)) | |
4042 | == TYPE_PRECISION (TREE_TYPE (t2))) | |
4043 | && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t1, 0))) | |
4044 | || (TREE_CODE (TREE_TYPE (TREE_OPERAND (t1, 0))) | |
4045 | == ENUMERAL_TYPE))) | |
4046 | { | |
4047 | tree type = TREE_TYPE (TREE_OPERAND (t1, 0)); | |
4048 | tree type1, type2; | |
5ffe581d | 4049 | struct z_candidate *w, *l; |
da20811c | 4050 | if (comp > 0) |
5ffe581d JM |
4051 | type1 = TREE_TYPE (t1), type2 = TREE_TYPE (t2), |
4052 | w = cand1, l = cand2; | |
da20811c | 4053 | else |
5ffe581d JM |
4054 | type1 = TREE_TYPE (t2), type2 = TREE_TYPE (t1), |
4055 | w = cand2, l = cand1; | |
da20811c | 4056 | |
5ffe581d JM |
4057 | if (warn) |
4058 | { | |
4059 | cp_warning ("passing `%T' chooses `%T' over `%T'", | |
4060 | type, type1, type2); | |
4061 | cp_warning (" in call to `%D'", w->fn); | |
4062 | } | |
4063 | else | |
4064 | add_warning (w, l); | |
da20811c JM |
4065 | } |
4066 | ||
c73964b2 | 4067 | if (winner && comp != winner) |
c11b6f21 MS |
4068 | { |
4069 | winner = 0; | |
4070 | goto tweak; | |
4071 | } | |
c73964b2 MS |
4072 | winner = comp; |
4073 | } | |
4074 | } | |
4075 | ||
8e69329a JM |
4076 | /* warn about confusing overload resolution */ |
4077 | if (winner && cand1->second_conv | |
4078 | && ! DECL_CONSTRUCTOR_P (cand1->fn) | |
4079 | && ! DECL_CONSTRUCTOR_P (cand2->fn)) | |
4080 | { | |
4081 | int comp = compare_ics (cand1->second_conv, cand2->second_conv); | |
4082 | if (comp && comp != winner) | |
4083 | { | |
4084 | struct z_candidate *w, *l; | |
4085 | if (winner == 1) | |
4086 | w = cand1, l = cand2; | |
4087 | else | |
4088 | w = cand2, l = cand1; | |
5ffe581d JM |
4089 | if (warn) |
4090 | { | |
4091 | cp_warning ("choosing `%D' over `%D'", w->fn, l->fn); | |
4092 | cp_warning (" for conversion from `%T' to `%T'", | |
4093 | TREE_TYPE (source_type (TREE_VEC_ELT (w->convs, 0))), | |
4094 | TREE_TYPE (w->second_conv)); | |
4095 | cp_warning (" because conversion sequence for `this' argument is better"); | |
4096 | } | |
4097 | else | |
4098 | add_warning (w, l); | |
8e69329a JM |
4099 | } |
4100 | } | |
4101 | ||
c73964b2 MS |
4102 | if (winner) |
4103 | return winner; | |
4104 | ||
4105 | /* or, if not that, | |
4106 | F1 is a non-template function and F2 is a template function */ | |
4107 | ||
4108 | if (! cand1->template && cand2->template) | |
4109 | return 1; | |
4110 | else if (cand1->template && ! cand2->template) | |
4111 | return -1; | |
4112 | else if (cand1->template && cand2->template) | |
4113 | winner = more_specialized | |
e1467ff2 MM |
4114 | (TI_TEMPLATE (cand1->template), TI_TEMPLATE (cand2->template), |
4115 | NULL_TREE); | |
c73964b2 MS |
4116 | |
4117 | /* or, if not that, | |
4118 | the context is an initialization by user-defined conversion (see | |
4119 | _dcl.init_ and _over.match.user_) and the standard conversion | |
4120 | sequence from the return type of F1 to the destination type (i.e., | |
4121 | the type of the entity being initialized) is a better conversion | |
4122 | sequence than the standard conversion sequence from the return type | |
4123 | of F2 to the destination type. */ | |
4124 | ||
4125 | if (! winner && cand1->second_conv) | |
4126 | winner = compare_ics (cand1->second_conv, cand2->second_conv); | |
4127 | ||
4128 | /* If the built-in candidates are the same, arbitrarily pick one. */ | |
4129 | if (! winner && cand1->fn == cand2->fn | |
4130 | && TREE_CODE (cand1->fn) == IDENTIFIER_NODE) | |
4131 | { | |
c11b6f21 | 4132 | for (i = 0; i < len; ++i) |
c73964b2 MS |
4133 | if (! comptypes (TREE_TYPE (TREE_VEC_ELT (cand1->convs, i)), |
4134 | TREE_TYPE (TREE_VEC_ELT (cand2->convs, i)), 1)) | |
4135 | break; | |
4136 | if (i == TREE_VEC_LENGTH (cand1->convs)) | |
4137 | return 1; | |
653cc74a | 4138 | |
c73964b2 | 4139 | /* Kludge around broken overloading rules whereby |
653cc74a JM |
4140 | Integer a, b; test ? a : b; is ambiguous, since there's a builtin |
4141 | that takes references and another that takes values. */ | |
c73964b2 MS |
4142 | if (cand1->fn == ansi_opname[COND_EXPR]) |
4143 | { | |
4144 | tree c1 = TREE_VEC_ELT (cand1->convs, 1); | |
4145 | tree c2 = TREE_VEC_ELT (cand2->convs, 1); | |
4146 | tree t1 = strip_top_quals (non_reference (TREE_TYPE (c1))); | |
4147 | tree t2 = strip_top_quals (non_reference (TREE_TYPE (c2))); | |
4148 | ||
4149 | if (comptypes (t1, t2, 1)) | |
4150 | { | |
4151 | if (TREE_CODE (c1) == REF_BIND && TREE_CODE (c2) != REF_BIND) | |
4152 | return 1; | |
4153 | if (TREE_CODE (c1) != REF_BIND && TREE_CODE (c2) == REF_BIND) | |
4154 | return -1; | |
4155 | } | |
4156 | } | |
4157 | } | |
4158 | ||
c11b6f21 MS |
4159 | tweak: |
4160 | ||
4161 | /* Extension: If the worst conversion for one candidate is worse than the | |
4162 | worst conversion for the other, take the first. */ | |
4163 | if (! winner && ! pedantic) | |
4164 | { | |
4165 | int rank1 = IDENTITY_RANK, rank2 = IDENTITY_RANK; | |
4166 | ||
4167 | for (i = 0; i < len; ++i) | |
4168 | { | |
4169 | if (ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1)) > rank1) | |
4170 | rank1 = ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1)); | |
4171 | if (ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2)) > rank2) | |
4172 | rank2 = ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2)); | |
4173 | } | |
4174 | ||
4175 | if (rank1 < rank2) | |
4176 | return 1; | |
4177 | if (rank1 > rank2) | |
4178 | return -1; | |
4179 | } | |
4180 | ||
c73964b2 MS |
4181 | return winner; |
4182 | } | |
4183 | ||
4184 | /* Given a list of candidates for overloading, find the best one, if any. | |
4185 | This algorithm has a worst case of O(2n) (winner is last), and a best | |
4186 | case of O(n/2) (totally ambiguous); much better than a sorting | |
4187 | algorithm. */ | |
4188 | ||
4189 | static struct z_candidate * | |
4190 | tourney (candidates) | |
4191 | struct z_candidate *candidates; | |
4192 | { | |
4193 | struct z_candidate *champ = candidates, *challenger; | |
4194 | int fate; | |
4195 | ||
4196 | /* Walk through the list once, comparing each current champ to the next | |
4197 | candidate, knocking out a candidate or two with each comparison. */ | |
4198 | ||
4199 | for (challenger = champ->next; challenger; ) | |
4200 | { | |
5ffe581d | 4201 | fate = joust (champ, challenger, 0); |
c73964b2 MS |
4202 | if (fate == 1) |
4203 | challenger = challenger->next; | |
4204 | else | |
4205 | { | |
4206 | if (fate == 0) | |
4207 | { | |
4208 | champ = challenger->next; | |
4209 | if (champ == 0) | |
4210 | return 0; | |
4211 | } | |
4212 | else | |
4213 | champ = challenger; | |
4214 | ||
4215 | challenger = champ->next; | |
4216 | } | |
4217 | } | |
4218 | ||
4219 | /* Make sure the champ is better than all the candidates it hasn't yet | |
4220 | been compared to. This may do one more comparison than necessary. Oh | |
4221 | well. */ | |
4222 | ||
4223 | for (challenger = candidates; challenger != champ; | |
4224 | challenger = challenger->next) | |
4225 | { | |
5ffe581d | 4226 | fate = joust (champ, challenger, 0); |
c73964b2 MS |
4227 | if (fate != 1) |
4228 | return 0; | |
4229 | } | |
4230 | ||
4231 | return champ; | |
4232 | } | |
c11b6f21 MS |
4233 | |
4234 | int | |
4235 | can_convert (to, from) | |
4236 | tree to, from; | |
4237 | { | |
277294d7 JM |
4238 | tree t = implicit_conversion (to, from, NULL_TREE, LOOKUP_NORMAL); |
4239 | return (t && ! ICS_BAD_FLAG (t)); | |
c11b6f21 MS |
4240 | } |
4241 | ||
4242 | int | |
4243 | can_convert_arg (to, from, arg) | |
4244 | tree to, from, arg; | |
4245 | { | |
277294d7 JM |
4246 | tree t = implicit_conversion (to, from, arg, LOOKUP_NORMAL); |
4247 | return (t && ! ICS_BAD_FLAG (t)); | |
c11b6f21 | 4248 | } |