1 | /* Build expressions with type checking for C++ compiler. |
2 | Copyright (C) 1987-2017 Free Software Foundation, Inc. |
3 | Hacked by Michael Tiemann (tiemann@cygnus.com) |
4 | |
5 | This file is part of GCC. |
6 | |
7 | GCC is free software; you can redistribute it and/or modify |
8 | it under the terms of the GNU General Public License as published by |
9 | the Free Software Foundation; either version 3, or (at your option) |
10 | any later version. |
11 | |
12 | GCC is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | GNU General Public License for more details. |
16 | |
17 | You should have received a copy of the GNU General Public License |
18 | along with GCC; see the file COPYING3. If not see |
19 | <http://www.gnu.org/licenses/>. */ |
20 | |
21 | |
22 | /* This file is part of the C++ front end. |
23 | It contains routines to build C++ expressions given their operands, |
24 | including computing the types of the result, C and C++ specific error |
25 | checks, and some optimization. */ |
26 | |
27 | #include "config.h" |
28 | #include "system.h" |
29 | #include "coretypes.h" |
30 | #include "target.h" |
31 | #include "cp-tree.h" |
32 | #include "stor-layout.h" |
33 | #include "varasm.h" |
34 | #include "intl.h" |
35 | #include "convert.h" |
36 | #include "c-family/c-objc.h" |
37 | #include "c-family/c-ubsan.h" |
38 | #include "params.h" |
39 | #include "gcc-rich-location.h" |
40 | #include "stringpool.h" |
41 | #include "attribs.h" |
42 | #include "asan.h" |
43 | |
44 | static tree cp_build_addr_expr_strict (tree, tsubst_flags_t); |
45 | static tree cp_build_function_call (tree, tree, tsubst_flags_t); |
46 | static tree pfn_from_ptrmemfunc (tree); |
47 | static tree delta_from_ptrmemfunc (tree); |
48 | static tree convert_for_assignment (tree, tree, impl_conv_rhs, tree, int, |
49 | tsubst_flags_t, int); |
50 | static tree cp_pointer_int_sum (location_t, enum tree_code, tree, tree, |
51 | tsubst_flags_t); |
52 | static tree rationalize_conditional_expr (enum tree_code, tree, |
53 | tsubst_flags_t); |
54 | static int comp_ptr_ttypes_real (tree, tree, int); |
55 | static bool comp_except_types (tree, tree, bool); |
56 | static bool comp_array_types (const_tree, const_tree, bool); |
57 | static tree pointer_diff (location_t, tree, tree, tree, tsubst_flags_t, tree *); |
58 | static tree get_delta_difference (tree, tree, bool, bool, tsubst_flags_t); |
59 | static void casts_away_constness_r (tree *, tree *, tsubst_flags_t); |
60 | static bool casts_away_constness (tree, tree, tsubst_flags_t); |
61 | static bool maybe_warn_about_returning_address_of_local (tree); |
62 | static tree lookup_destructor (tree, tree, tree, tsubst_flags_t); |
63 | static void error_args_num (location_t, tree, bool); |
64 | static int convert_arguments (tree, vec<tree, va_gc> **, tree, int, |
65 | tsubst_flags_t); |
66 | |
67 | /* Do `exp = require_complete_type (exp);' to make sure exp |
68 | does not have an incomplete type. (That includes void types.) |
69 | Returns error_mark_node if the VALUE does not have |
70 | complete type when this function returns. */ |
71 | |
72 | tree |
73 | require_complete_type_sfinae (tree value, tsubst_flags_t complain) |
74 | { |
75 | tree type; |
76 | |
77 | if (processing_template_decl || value == error_mark_node) |
78 | return value; |
79 | |
80 | if (TREE_CODE (value) == OVERLOAD) |
81 | type = unknown_type_node; |
82 | else |
83 | type = TREE_TYPE (value); |
84 | |
85 | if (type == error_mark_node) |
86 | return error_mark_node; |
87 | |
88 | /* First, detect a valid value with a complete type. */ |
89 | if (COMPLETE_TYPE_P (type)) |
90 | return value; |
91 | |
92 | if (complete_type_or_maybe_complain (type, value, complain)) |
93 | return value; |
94 | else |
95 | return error_mark_node; |
96 | } |
97 | |
98 | tree |
99 | require_complete_type (tree value) |
100 | { |
101 | return require_complete_type_sfinae (value, tf_warning_or_error); |
102 | } |
103 | |
104 | /* Try to complete TYPE, if it is incomplete. For example, if TYPE is |
105 | a template instantiation, do the instantiation. Returns TYPE, |
106 | whether or not it could be completed, unless something goes |
107 | horribly wrong, in which case the error_mark_node is returned. */ |
108 | |
109 | tree |
110 | complete_type (tree type) |
111 | { |
112 | if (type == NULL_TREE) |
113 | /* Rather than crash, we return something sure to cause an error |
114 | at some point. */ |
115 | return error_mark_node; |
116 | |
117 | if (type == error_mark_node || COMPLETE_TYPE_P (type)) |
118 | ; |
119 | else if (TREE_CODE (type) == ARRAY_TYPE) |
120 | { |
121 | tree t = complete_type (TREE_TYPE (type)); |
122 | unsigned int needs_constructing, has_nontrivial_dtor; |
123 | if (COMPLETE_TYPE_P (t) && !dependent_type_p (type)) |
124 | layout_type (type); |
125 | needs_constructing |
126 | = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t)); |
127 | has_nontrivial_dtor |
128 | = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t)); |
129 | for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t)) |
130 | { |
131 | TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing; |
132 | TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor; |
133 | } |
134 | } |
135 | else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type)) |
136 | instantiate_class_template (TYPE_MAIN_VARIANT (type)); |
137 | |
138 | return type; |
139 | } |
140 | |
141 | /* Like complete_type, but issue an error if the TYPE cannot be completed. |
142 | VALUE is used for informative diagnostics. |
143 | Returns NULL_TREE if the type cannot be made complete. */ |
144 | |
145 | tree |
146 | complete_type_or_maybe_complain (tree type, tree value, tsubst_flags_t complain) |
147 | { |
148 | type = complete_type (type); |
149 | if (type == error_mark_node) |
150 | /* We already issued an error. */ |
151 | return NULL_TREE; |
152 | else if (!COMPLETE_TYPE_P (type)) |
153 | { |
154 | if (complain & tf_error) |
155 | cxx_incomplete_type_diagnostic (value, type, DK_ERROR); |
156 | return NULL_TREE; |
157 | } |
158 | else |
159 | return type; |
160 | } |
161 | |
162 | tree |
163 | complete_type_or_else (tree type, tree value) |
164 | { |
165 | return complete_type_or_maybe_complain (type, value, tf_warning_or_error); |
166 | } |
167 | |
168 | |
169 | /* Return the common type of two parameter lists. |
170 | We assume that comptypes has already been done and returned 1; |
171 | if that isn't so, this may crash. |
172 | |
173 | As an optimization, free the space we allocate if the parameter |
174 | lists are already common. */ |
175 | |
176 | static tree |
177 | commonparms (tree p1, tree p2) |
178 | { |
179 | tree oldargs = p1, newargs, n; |
180 | int i, len; |
181 | int any_change = 0; |
182 | |
183 | len = list_length (p1); |
184 | newargs = tree_last (p1); |
185 | |
186 | if (newargs == void_list_node) |
187 | i = 1; |
188 | else |
189 | { |
190 | i = 0; |
191 | newargs = 0; |
192 | } |
193 | |
194 | for (; i < len; i++) |
195 | newargs = tree_cons (NULL_TREE, NULL_TREE, newargs); |
196 | |
197 | n = newargs; |
198 | |
199 | for (i = 0; p1; |
200 | p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++) |
201 | { |
202 | if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2)) |
203 | { |
204 | TREE_PURPOSE (n) = TREE_PURPOSE (p1); |
205 | any_change = 1; |
206 | } |
207 | else if (! TREE_PURPOSE (p1)) |
208 | { |
209 | if (TREE_PURPOSE (p2)) |
210 | { |
211 | TREE_PURPOSE (n) = TREE_PURPOSE (p2); |
212 | any_change = 1; |
213 | } |
214 | } |
215 | else |
216 | { |
217 | if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2))) |
218 | any_change = 1; |
219 | TREE_PURPOSE (n) = TREE_PURPOSE (p2); |
220 | } |
221 | if (TREE_VALUE (p1) != TREE_VALUE (p2)) |
222 | { |
223 | any_change = 1; |
224 | TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2)); |
225 | } |
226 | else |
227 | TREE_VALUE (n) = TREE_VALUE (p1); |
228 | } |
229 | if (! any_change) |
230 | return oldargs; |
231 | |
232 | return newargs; |
233 | } |
234 | |
235 | /* Given a type, perhaps copied for a typedef, |
236 | find the "original" version of it. */ |
237 | static tree |
238 | original_type (tree t) |
239 | { |
240 | int quals = cp_type_quals (t); |
241 | while (t != error_mark_node |
242 | && TYPE_NAME (t) != NULL_TREE) |
243 | { |
244 | tree x = TYPE_NAME (t); |
245 | if (TREE_CODE (x) != TYPE_DECL) |
246 | break; |
247 | x = DECL_ORIGINAL_TYPE (x); |
248 | if (x == NULL_TREE) |
249 | break; |
250 | t = x; |
251 | } |
252 | return cp_build_qualified_type (t, quals); |
253 | } |
254 | |
255 | /* Return the common type for two arithmetic types T1 and T2 under the |
256 | usual arithmetic conversions. The default conversions have already |
257 | been applied, and enumerated types converted to their compatible |
258 | integer types. */ |
259 | |
260 | static tree |
261 | cp_common_type (tree t1, tree t2) |
262 | { |
263 | enum tree_code code1 = TREE_CODE (t1); |
264 | enum tree_code code2 = TREE_CODE (t2); |
265 | tree attributes; |
266 | int i; |
267 | |
268 | |
269 | /* In what follows, we slightly generalize the rules given in [expr] so |
270 | as to deal with `long long' and `complex'. First, merge the |
271 | attributes. */ |
272 | attributes = (*targetm.merge_type_attributes) (t1, t2); |
273 | |
274 | if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2)) |
275 | { |
276 | if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2)) |
277 | return build_type_attribute_variant (t1, attributes); |
278 | else |
279 | return NULL_TREE; |
280 | } |
281 | |
282 | /* FIXME: Attributes. */ |
283 | gcc_assert (ARITHMETIC_TYPE_P (t1) |
284 | || VECTOR_TYPE_P (t1) |
285 | || UNSCOPED_ENUM_P (t1)); |
286 | gcc_assert (ARITHMETIC_TYPE_P (t2) |
287 | || VECTOR_TYPE_P (t2) |
288 | || UNSCOPED_ENUM_P (t2)); |
289 | |
290 | /* If one type is complex, form the common type of the non-complex |
291 | components, then make that complex. Use T1 or T2 if it is the |
292 | required type. */ |
293 | if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE) |
294 | { |
295 | tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1; |
296 | tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2; |
297 | tree subtype |
298 | = type_after_usual_arithmetic_conversions (subtype1, subtype2); |
299 | |
300 | if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype) |
301 | return build_type_attribute_variant (t1, attributes); |
302 | else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype) |
303 | return build_type_attribute_variant (t2, attributes); |
304 | else |
305 | return build_type_attribute_variant (build_complex_type (subtype), |
306 | attributes); |
307 | } |
308 | |
309 | if (code1 == VECTOR_TYPE) |
310 | { |
311 | /* When we get here we should have two vectors of the same size. |
312 | Just prefer the unsigned one if present. */ |
313 | if (TYPE_UNSIGNED (t1)) |
314 | return build_type_attribute_variant (t1, attributes); |
315 | else |
316 | return build_type_attribute_variant (t2, attributes); |
317 | } |
318 | |
319 | /* If only one is real, use it as the result. */ |
320 | if (code1 == REAL_TYPE && code2 != REAL_TYPE) |
321 | return build_type_attribute_variant (t1, attributes); |
322 | if (code2 == REAL_TYPE && code1 != REAL_TYPE) |
323 | return build_type_attribute_variant (t2, attributes); |
324 | |
325 | /* Both real or both integers; use the one with greater precision. */ |
326 | if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2)) |
327 | return build_type_attribute_variant (t1, attributes); |
328 | else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1)) |
329 | return build_type_attribute_variant (t2, attributes); |
330 | |
331 | /* The types are the same; no need to do anything fancy. */ |
332 | if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2)) |
333 | return build_type_attribute_variant (t1, attributes); |
334 | |
335 | if (code1 != REAL_TYPE) |
336 | { |
337 | /* If one is unsigned long long, then convert the other to unsigned |
338 | long long. */ |
339 | if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node) |
340 | || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node)) |
341 | return build_type_attribute_variant (long_long_unsigned_type_node, |
342 | attributes); |
343 | /* If one is a long long, and the other is an unsigned long, and |
344 | long long can represent all the values of an unsigned long, then |
345 | convert to a long long. Otherwise, convert to an unsigned long |
346 | long. Otherwise, if either operand is long long, convert the |
347 | other to long long. |
348 | |
349 | Since we're here, we know the TYPE_PRECISION is the same; |
350 | therefore converting to long long cannot represent all the values |
351 | of an unsigned long, so we choose unsigned long long in that |
352 | case. */ |
353 | if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node) |
354 | || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node)) |
355 | { |
356 | tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2)) |
357 | ? long_long_unsigned_type_node |
358 | : long_long_integer_type_node); |
359 | return build_type_attribute_variant (t, attributes); |
360 | } |
361 | |
362 | /* Go through the same procedure, but for longs. */ |
363 | if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node) |
364 | || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node)) |
365 | return build_type_attribute_variant (long_unsigned_type_node, |
366 | attributes); |
367 | if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node) |
368 | || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node)) |
369 | { |
370 | tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2)) |
371 | ? long_unsigned_type_node : long_integer_type_node); |
372 | return build_type_attribute_variant (t, attributes); |
373 | } |
374 | |
375 | /* For __intN types, either the type is __int128 (and is lower |
376 | priority than the types checked above, but higher than other |
377 | 128-bit types) or it's known to not be the same size as other |
378 | types (enforced in toplev.c). Prefer the unsigned type. */ |
379 | for (i = 0; i < NUM_INT_N_ENTS; i ++) |
380 | { |
381 | if (int_n_enabled_p [i] |
382 | && (same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].signed_type) |
383 | || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].signed_type) |
384 | || same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].unsigned_type) |
385 | || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].unsigned_type))) |
386 | { |
387 | tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2)) |
388 | ? int_n_trees[i].unsigned_type |
389 | : int_n_trees[i].signed_type); |
390 | return build_type_attribute_variant (t, attributes); |
391 | } |
392 | } |
393 | |
394 | /* Otherwise prefer the unsigned one. */ |
395 | if (TYPE_UNSIGNED (t1)) |
396 | return build_type_attribute_variant (t1, attributes); |
397 | else |
398 | return build_type_attribute_variant (t2, attributes); |
399 | } |
400 | else |
401 | { |
402 | if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node) |
403 | || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node)) |
404 | return build_type_attribute_variant (long_double_type_node, |
405 | attributes); |
406 | if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node) |
407 | || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node)) |
408 | return build_type_attribute_variant (double_type_node, |
409 | attributes); |
410 | if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node) |
411 | || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node)) |
412 | return build_type_attribute_variant (float_type_node, |
413 | attributes); |
414 | |
415 | /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of |
416 | the standard C++ floating-point types. Logic earlier in this |
417 | function has already eliminated the possibility that |
418 | TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no |
419 | compelling reason to choose one or the other. */ |
420 | return build_type_attribute_variant (t1, attributes); |
421 | } |
422 | } |
423 | |
424 | /* T1 and T2 are arithmetic or enumeration types. Return the type |
425 | that will result from the "usual arithmetic conversions" on T1 and |
426 | T2 as described in [expr]. */ |
427 | |
428 | tree |
429 | type_after_usual_arithmetic_conversions (tree t1, tree t2) |
430 | { |
431 | gcc_assert (ARITHMETIC_TYPE_P (t1) |
432 | || VECTOR_TYPE_P (t1) |
433 | || UNSCOPED_ENUM_P (t1)); |
434 | gcc_assert (ARITHMETIC_TYPE_P (t2) |
435 | || VECTOR_TYPE_P (t2) |
436 | || UNSCOPED_ENUM_P (t2)); |
437 | |
438 | /* Perform the integral promotions. We do not promote real types here. */ |
439 | if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1) |
440 | && INTEGRAL_OR_ENUMERATION_TYPE_P (t2)) |
441 | { |
442 | t1 = type_promotes_to (t1); |
443 | t2 = type_promotes_to (t2); |
444 | } |
445 | |
446 | return cp_common_type (t1, t2); |
447 | } |
448 | |
449 | static void |
450 | composite_pointer_error (diagnostic_t kind, tree t1, tree t2, |
451 | composite_pointer_operation operation) |
452 | { |
453 | switch (operation) |
454 | { |
455 | case CPO_COMPARISON: |
456 | emit_diagnostic (kind, input_location, 0, |
457 | "comparison between " |
458 | "distinct pointer types %qT and %qT lacks a cast" , |
459 | t1, t2); |
460 | break; |
461 | case CPO_CONVERSION: |
462 | emit_diagnostic (kind, input_location, 0, |
463 | "conversion between " |
464 | "distinct pointer types %qT and %qT lacks a cast" , |
465 | t1, t2); |
466 | break; |
467 | case CPO_CONDITIONAL_EXPR: |
468 | emit_diagnostic (kind, input_location, 0, |
469 | "conditional expression between " |
470 | "distinct pointer types %qT and %qT lacks a cast" , |
471 | t1, t2); |
472 | break; |
473 | default: |
474 | gcc_unreachable (); |
475 | } |
476 | } |
477 | |
478 | /* Subroutine of composite_pointer_type to implement the recursive |
479 | case. See that function for documentation of the parameters. */ |
480 | |
481 | static tree |
482 | composite_pointer_type_r (tree t1, tree t2, |
483 | composite_pointer_operation operation, |
484 | tsubst_flags_t complain) |
485 | { |
486 | tree pointee1; |
487 | tree pointee2; |
488 | tree result_type; |
489 | tree attributes; |
490 | |
491 | /* Determine the types pointed to by T1 and T2. */ |
492 | if (TYPE_PTR_P (t1)) |
493 | { |
494 | pointee1 = TREE_TYPE (t1); |
495 | pointee2 = TREE_TYPE (t2); |
496 | } |
497 | else |
498 | { |
499 | pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1); |
500 | pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2); |
501 | } |
502 | |
503 | /* [expr.rel] |
504 | |
505 | Otherwise, the composite pointer type is a pointer type |
506 | similar (_conv.qual_) to the type of one of the operands, |
507 | with a cv-qualification signature (_conv.qual_) that is the |
508 | union of the cv-qualification signatures of the operand |
509 | types. */ |
510 | if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2)) |
511 | result_type = pointee1; |
512 | else if ((TYPE_PTR_P (pointee1) && TYPE_PTR_P (pointee2)) |
513 | || (TYPE_PTRMEM_P (pointee1) && TYPE_PTRMEM_P (pointee2))) |
514 | { |
515 | result_type = composite_pointer_type_r (pointee1, pointee2, operation, |
516 | complain); |
517 | if (result_type == error_mark_node) |
518 | return error_mark_node; |
519 | } |
520 | else |
521 | { |
522 | if (complain & tf_error) |
523 | composite_pointer_error (DK_PERMERROR, t1, t2, operation); |
524 | else |
525 | return error_mark_node; |
526 | result_type = void_type_node; |
527 | } |
528 | result_type = cp_build_qualified_type (result_type, |
529 | (cp_type_quals (pointee1) |
530 | | cp_type_quals (pointee2))); |
531 | /* If the original types were pointers to members, so is the |
532 | result. */ |
533 | if (TYPE_PTRMEM_P (t1)) |
534 | { |
535 | if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1), |
536 | TYPE_PTRMEM_CLASS_TYPE (t2))) |
537 | { |
538 | if (complain & tf_error) |
539 | composite_pointer_error (DK_PERMERROR, t1, t2, operation); |
540 | else |
541 | return error_mark_node; |
542 | } |
543 | result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1), |
544 | result_type); |
545 | } |
546 | else |
547 | result_type = build_pointer_type (result_type); |
548 | |
549 | /* Merge the attributes. */ |
550 | attributes = (*targetm.merge_type_attributes) (t1, t2); |
551 | return build_type_attribute_variant (result_type, attributes); |
552 | } |
553 | |
554 | /* Return the composite pointer type (see [expr.rel]) for T1 and T2. |
555 | ARG1 and ARG2 are the values with those types. The OPERATION is to |
556 | describe the operation between the pointer types, |
557 | in case an error occurs. |
558 | |
559 | This routine also implements the computation of a common type for |
560 | pointers-to-members as per [expr.eq]. */ |
561 | |
562 | tree |
563 | composite_pointer_type (tree t1, tree t2, tree arg1, tree arg2, |
564 | composite_pointer_operation operation, |
565 | tsubst_flags_t complain) |
566 | { |
567 | tree class1; |
568 | tree class2; |
569 | |
570 | /* [expr.rel] |
571 | |
572 | If one operand is a null pointer constant, the composite pointer |
573 | type is the type of the other operand. */ |
574 | if (null_ptr_cst_p (arg1)) |
575 | return t2; |
576 | if (null_ptr_cst_p (arg2)) |
577 | return t1; |
578 | |
579 | /* We have: |
580 | |
581 | [expr.rel] |
582 | |
583 | If one of the operands has type "pointer to cv1 void*", then |
584 | the other has type "pointer to cv2T", and the composite pointer |
585 | type is "pointer to cv12 void", where cv12 is the union of cv1 |
586 | and cv2. |
587 | |
588 | If either type is a pointer to void, make sure it is T1. */ |
589 | if (TYPE_PTR_P (t2) && VOID_TYPE_P (TREE_TYPE (t2))) |
590 | std::swap (t1, t2); |
591 | |
592 | /* Now, if T1 is a pointer to void, merge the qualifiers. */ |
593 | if (TYPE_PTR_P (t1) && VOID_TYPE_P (TREE_TYPE (t1))) |
594 | { |
595 | tree attributes; |
596 | tree result_type; |
597 | |
598 | if (TYPE_PTRFN_P (t2)) |
599 | { |
600 | if (complain & tf_error) |
601 | { |
602 | switch (operation) |
603 | { |
604 | case CPO_COMPARISON: |
605 | pedwarn (input_location, OPT_Wpedantic, |
606 | "ISO C++ forbids comparison between pointer " |
607 | "of type %<void *%> and pointer-to-function" ); |
608 | break; |
609 | case CPO_CONVERSION: |
610 | pedwarn (input_location, OPT_Wpedantic, |
611 | "ISO C++ forbids conversion between pointer " |
612 | "of type %<void *%> and pointer-to-function" ); |
613 | break; |
614 | case CPO_CONDITIONAL_EXPR: |
615 | pedwarn (input_location, OPT_Wpedantic, |
616 | "ISO C++ forbids conditional expression between " |
617 | "pointer of type %<void *%> and " |
618 | "pointer-to-function" ); |
619 | break; |
620 | default: |
621 | gcc_unreachable (); |
622 | } |
623 | } |
624 | else |
625 | return error_mark_node; |
626 | } |
627 | result_type |
628 | = cp_build_qualified_type (void_type_node, |
629 | (cp_type_quals (TREE_TYPE (t1)) |
630 | | cp_type_quals (TREE_TYPE (t2)))); |
631 | result_type = build_pointer_type (result_type); |
632 | /* Merge the attributes. */ |
633 | attributes = (*targetm.merge_type_attributes) (t1, t2); |
634 | return build_type_attribute_variant (result_type, attributes); |
635 | } |
636 | |
637 | if (c_dialect_objc () && TYPE_PTR_P (t1) |
638 | && TYPE_PTR_P (t2)) |
639 | { |
640 | if (objc_have_common_type (t1, t2, -3, NULL_TREE)) |
641 | return objc_common_type (t1, t2); |
642 | } |
643 | |
644 | /* if T1 or T2 is "pointer to noexcept function" and the other type is |
645 | "pointer to function", where the function types are otherwise the same, |
646 | "pointer to function" */ |
647 | if (fnptr_conv_p (t1, t2)) |
648 | return t1; |
649 | if (fnptr_conv_p (t2, t1)) |
650 | return t2; |
651 | |
652 | /* [expr.eq] permits the application of a pointer conversion to |
653 | bring the pointers to a common type. */ |
654 | if (TYPE_PTR_P (t1) && TYPE_PTR_P (t2) |
655 | && CLASS_TYPE_P (TREE_TYPE (t1)) |
656 | && CLASS_TYPE_P (TREE_TYPE (t2)) |
657 | && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1), |
658 | TREE_TYPE (t2))) |
659 | { |
660 | class1 = TREE_TYPE (t1); |
661 | class2 = TREE_TYPE (t2); |
662 | |
663 | if (DERIVED_FROM_P (class1, class2)) |
664 | t2 = (build_pointer_type |
665 | (cp_build_qualified_type (class1, cp_type_quals (class2)))); |
666 | else if (DERIVED_FROM_P (class2, class1)) |
667 | t1 = (build_pointer_type |
668 | (cp_build_qualified_type (class2, cp_type_quals (class1)))); |
669 | else |
670 | { |
671 | if (complain & tf_error) |
672 | composite_pointer_error (DK_ERROR, t1, t2, operation); |
673 | return error_mark_node; |
674 | } |
675 | } |
676 | /* [expr.eq] permits the application of a pointer-to-member |
677 | conversion to change the class type of one of the types. */ |
678 | else if (TYPE_PTRMEM_P (t1) |
679 | && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1), |
680 | TYPE_PTRMEM_CLASS_TYPE (t2))) |
681 | { |
682 | class1 = TYPE_PTRMEM_CLASS_TYPE (t1); |
683 | class2 = TYPE_PTRMEM_CLASS_TYPE (t2); |
684 | |
685 | if (DERIVED_FROM_P (class1, class2)) |
686 | t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1)); |
687 | else if (DERIVED_FROM_P (class2, class1)) |
688 | t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2)); |
689 | else |
690 | { |
691 | if (complain & tf_error) |
692 | switch (operation) |
693 | { |
694 | case CPO_COMPARISON: |
695 | error ("comparison between distinct " |
696 | "pointer-to-member types %qT and %qT lacks a cast" , |
697 | t1, t2); |
698 | break; |
699 | case CPO_CONVERSION: |
700 | error ("conversion between distinct " |
701 | "pointer-to-member types %qT and %qT lacks a cast" , |
702 | t1, t2); |
703 | break; |
704 | case CPO_CONDITIONAL_EXPR: |
705 | error ("conditional expression between distinct " |
706 | "pointer-to-member types %qT and %qT lacks a cast" , |
707 | t1, t2); |
708 | break; |
709 | default: |
710 | gcc_unreachable (); |
711 | } |
712 | return error_mark_node; |
713 | } |
714 | } |
715 | |
716 | return composite_pointer_type_r (t1, t2, operation, complain); |
717 | } |
718 | |
719 | /* Return the merged type of two types. |
720 | We assume that comptypes has already been done and returned 1; |
721 | if that isn't so, this may crash. |
722 | |
723 | This just combines attributes and default arguments; any other |
724 | differences would cause the two types to compare unalike. */ |
725 | |
726 | tree |
727 | merge_types (tree t1, tree t2) |
728 | { |
729 | enum tree_code code1; |
730 | enum tree_code code2; |
731 | tree attributes; |
732 | |
733 | /* Save time if the two types are the same. */ |
734 | if (t1 == t2) |
735 | return t1; |
736 | if (original_type (t1) == original_type (t2)) |
737 | return t1; |
738 | |
739 | /* If one type is nonsense, use the other. */ |
740 | if (t1 == error_mark_node) |
741 | return t2; |
742 | if (t2 == error_mark_node) |
743 | return t1; |
744 | |
745 | /* Handle merging an auto redeclaration with a previous deduced |
746 | return type. */ |
747 | if (is_auto (t1)) |
748 | return t2; |
749 | |
750 | /* Merge the attributes. */ |
751 | attributes = (*targetm.merge_type_attributes) (t1, t2); |
752 | |
753 | if (TYPE_PTRMEMFUNC_P (t1)) |
754 | t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1); |
755 | if (TYPE_PTRMEMFUNC_P (t2)) |
756 | t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2); |
757 | |
758 | code1 = TREE_CODE (t1); |
759 | code2 = TREE_CODE (t2); |
760 | if (code1 != code2) |
761 | { |
762 | gcc_assert (code1 == TYPENAME_TYPE || code2 == TYPENAME_TYPE); |
763 | if (code1 == TYPENAME_TYPE) |
764 | { |
765 | t1 = resolve_typename_type (t1, /*only_current_p=*/true); |
766 | code1 = TREE_CODE (t1); |
767 | } |
768 | else |
769 | { |
770 | t2 = resolve_typename_type (t2, /*only_current_p=*/true); |
771 | code2 = TREE_CODE (t2); |
772 | } |
773 | } |
774 | |
775 | switch (code1) |
776 | { |
777 | case POINTER_TYPE: |
778 | case REFERENCE_TYPE: |
779 | /* For two pointers, do this recursively on the target type. */ |
780 | { |
781 | tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2)); |
782 | int quals = cp_type_quals (t1); |
783 | |
784 | if (code1 == POINTER_TYPE) |
785 | { |
786 | t1 = build_pointer_type (target); |
787 | if (TREE_CODE (target) == METHOD_TYPE) |
788 | t1 = build_ptrmemfunc_type (t1); |
789 | } |
790 | else |
791 | t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1)); |
792 | t1 = build_type_attribute_variant (t1, attributes); |
793 | t1 = cp_build_qualified_type (t1, quals); |
794 | |
795 | return t1; |
796 | } |
797 | |
798 | case OFFSET_TYPE: |
799 | { |
800 | int quals; |
801 | tree pointee; |
802 | quals = cp_type_quals (t1); |
803 | pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1), |
804 | TYPE_PTRMEM_POINTED_TO_TYPE (t2)); |
805 | t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1), |
806 | pointee); |
807 | t1 = cp_build_qualified_type (t1, quals); |
808 | break; |
809 | } |
810 | |
811 | case ARRAY_TYPE: |
812 | { |
813 | tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2)); |
814 | /* Save space: see if the result is identical to one of the args. */ |
815 | if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)) |
816 | return build_type_attribute_variant (t1, attributes); |
817 | if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)) |
818 | return build_type_attribute_variant (t2, attributes); |
819 | /* Merge the element types, and have a size if either arg has one. */ |
820 | t1 = build_cplus_array_type |
821 | (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2)); |
822 | break; |
823 | } |
824 | |
825 | case FUNCTION_TYPE: |
826 | /* Function types: prefer the one that specified arg types. |
827 | If both do, merge the arg types. Also merge the return types. */ |
828 | { |
829 | tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2)); |
830 | tree p1 = TYPE_ARG_TYPES (t1); |
831 | tree p2 = TYPE_ARG_TYPES (t2); |
832 | tree parms; |
833 | tree rval, raises; |
834 | bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t1); |
835 | |
836 | /* Save space: see if the result is identical to one of the args. */ |
837 | if (valtype == TREE_TYPE (t1) && ! p2) |
838 | return cp_build_type_attribute_variant (t1, attributes); |
839 | if (valtype == TREE_TYPE (t2) && ! p1) |
840 | return cp_build_type_attribute_variant (t2, attributes); |
841 | |
842 | /* Simple way if one arg fails to specify argument types. */ |
843 | if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node) |
844 | parms = p2; |
845 | else if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node) |
846 | parms = p1; |
847 | else |
848 | parms = commonparms (p1, p2); |
849 | |
850 | rval = build_function_type (valtype, parms); |
851 | gcc_assert (type_memfn_quals (t1) == type_memfn_quals (t2)); |
852 | gcc_assert (type_memfn_rqual (t1) == type_memfn_rqual (t2)); |
853 | rval = apply_memfn_quals (rval, |
854 | type_memfn_quals (t1), |
855 | type_memfn_rqual (t1)); |
856 | raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1), |
857 | TYPE_RAISES_EXCEPTIONS (t2)); |
858 | t1 = build_exception_variant (rval, raises); |
859 | if (late_return_type_p) |
860 | TYPE_HAS_LATE_RETURN_TYPE (t1) = 1; |
861 | break; |
862 | } |
863 | |
864 | case METHOD_TYPE: |
865 | { |
866 | /* Get this value the long way, since TYPE_METHOD_BASETYPE |
867 | is just the main variant of this. */ |
868 | tree basetype = class_of_this_parm (t2); |
869 | tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1), |
870 | TYPE_RAISES_EXCEPTIONS (t2)); |
871 | cp_ref_qualifier rqual = type_memfn_rqual (t1); |
872 | tree t3; |
873 | bool late_return_type_1_p = TYPE_HAS_LATE_RETURN_TYPE (t1); |
874 | bool late_return_type_2_p = TYPE_HAS_LATE_RETURN_TYPE (t2); |
875 | |
876 | /* If this was a member function type, get back to the |
877 | original type of type member function (i.e., without |
878 | the class instance variable up front. */ |
879 | t1 = build_function_type (TREE_TYPE (t1), |
880 | TREE_CHAIN (TYPE_ARG_TYPES (t1))); |
881 | t2 = build_function_type (TREE_TYPE (t2), |
882 | TREE_CHAIN (TYPE_ARG_TYPES (t2))); |
883 | t3 = merge_types (t1, t2); |
884 | t3 = build_method_type_directly (basetype, TREE_TYPE (t3), |
885 | TYPE_ARG_TYPES (t3)); |
886 | t1 = build_exception_variant (t3, raises); |
887 | t1 = build_ref_qualified_type (t1, rqual); |
888 | if (late_return_type_1_p) |
889 | TYPE_HAS_LATE_RETURN_TYPE (t1) = 1; |
890 | if (late_return_type_2_p) |
891 | TYPE_HAS_LATE_RETURN_TYPE (t2) = 1; |
892 | break; |
893 | } |
894 | |
895 | case TYPENAME_TYPE: |
896 | /* There is no need to merge attributes into a TYPENAME_TYPE. |
897 | When the type is instantiated it will have whatever |
898 | attributes result from the instantiation. */ |
899 | return t1; |
900 | |
901 | default:; |
902 | } |
903 | |
904 | if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes)) |
905 | return t1; |
906 | else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes)) |
907 | return t2; |
908 | else |
909 | return cp_build_type_attribute_variant (t1, attributes); |
910 | } |
911 | |
912 | /* Return the ARRAY_TYPE type without its domain. */ |
913 | |
914 | tree |
915 | strip_array_domain (tree type) |
916 | { |
917 | tree t2; |
918 | gcc_assert (TREE_CODE (type) == ARRAY_TYPE); |
919 | if (TYPE_DOMAIN (type) == NULL_TREE) |
920 | return type; |
921 | t2 = build_cplus_array_type (TREE_TYPE (type), NULL_TREE); |
922 | return cp_build_type_attribute_variant (t2, TYPE_ATTRIBUTES (type)); |
923 | } |
924 | |
925 | /* Wrapper around cp_common_type that is used by c-common.c and other |
926 | front end optimizations that remove promotions. |
927 | |
928 | Return the common type for two arithmetic types T1 and T2 under the |
929 | usual arithmetic conversions. The default conversions have already |
930 | been applied, and enumerated types converted to their compatible |
931 | integer types. */ |
932 | |
933 | tree |
934 | common_type (tree t1, tree t2) |
935 | { |
936 | /* If one type is nonsense, use the other */ |
937 | if (t1 == error_mark_node) |
938 | return t2; |
939 | if (t2 == error_mark_node) |
940 | return t1; |
941 | |
942 | return cp_common_type (t1, t2); |
943 | } |
944 | |
945 | /* Return the common type of two pointer types T1 and T2. This is the |
946 | type for the result of most arithmetic operations if the operands |
947 | have the given two types. |
948 | |
949 | We assume that comp_target_types has already been done and returned |
950 | nonzero; if that isn't so, this may crash. */ |
951 | |
952 | tree |
953 | common_pointer_type (tree t1, tree t2) |
954 | { |
955 | gcc_assert ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2)) |
956 | || (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2)) |
957 | || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2))); |
958 | |
959 | return composite_pointer_type (t1, t2, error_mark_node, error_mark_node, |
960 | CPO_CONVERSION, tf_warning_or_error); |
961 | } |
962 | |
963 | /* Compare two exception specifier types for exactness or subsetness, if |
964 | allowed. Returns false for mismatch, true for match (same, or |
965 | derived and !exact). |
966 | |
967 | [except.spec] "If a class X ... objects of class X or any class publicly |
968 | and unambiguously derived from X. Similarly, if a pointer type Y * ... |
969 | exceptions of type Y * or that are pointers to any type publicly and |
970 | unambiguously derived from Y. Otherwise a function only allows exceptions |
971 | that have the same type ..." |
972 | This does not mention cv qualifiers and is different to what throw |
973 | [except.throw] and catch [except.catch] will do. They will ignore the |
974 | top level cv qualifiers, and allow qualifiers in the pointer to class |
975 | example. |
976 | |
977 | We implement the letter of the standard. */ |
978 | |
979 | static bool |
980 | comp_except_types (tree a, tree b, bool exact) |
981 | { |
982 | if (same_type_p (a, b)) |
983 | return true; |
984 | else if (!exact) |
985 | { |
986 | if (cp_type_quals (a) || cp_type_quals (b)) |
987 | return false; |
988 | |
989 | if (TYPE_PTR_P (a) && TYPE_PTR_P (b)) |
990 | { |
991 | a = TREE_TYPE (a); |
992 | b = TREE_TYPE (b); |
993 | if (cp_type_quals (a) || cp_type_quals (b)) |
994 | return false; |
995 | } |
996 | |
997 | if (TREE_CODE (a) != RECORD_TYPE |
998 | || TREE_CODE (b) != RECORD_TYPE) |
999 | return false; |
1000 | |
1001 | if (publicly_uniquely_derived_p (a, b)) |
1002 | return true; |
1003 | } |
1004 | return false; |
1005 | } |
1006 | |
1007 | /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers. |
1008 | If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5). |
1009 | If EXACT is ce_type, the C++17 type compatibility rules apply. |
1010 | If EXACT is ce_normal, the compatibility rules in 15.4/3 apply. |
1011 | If EXACT is ce_exact, the specs must be exactly the same. Exception lists |
1012 | are unordered, but we've already filtered out duplicates. Most lists will |
1013 | be in order, we should try to make use of that. */ |
1014 | |
1015 | bool |
1016 | comp_except_specs (const_tree t1, const_tree t2, int exact) |
1017 | { |
1018 | const_tree probe; |
1019 | const_tree base; |
1020 | int length = 0; |
1021 | |
1022 | if (t1 == t2) |
1023 | return true; |
1024 | |
1025 | /* First handle noexcept. */ |
1026 | if (exact < ce_exact) |
1027 | { |
1028 | if (exact == ce_type |
1029 | && (canonical_eh_spec (CONST_CAST_TREE (t1)) |
1030 | == canonical_eh_spec (CONST_CAST_TREE (t2)))) |
1031 | return true; |
1032 | |
1033 | /* noexcept(false) is compatible with no exception-specification, |
1034 | and less strict than any spec. */ |
1035 | if (t1 == noexcept_false_spec) |
1036 | return t2 == NULL_TREE || exact == ce_derived; |
1037 | /* Even a derived noexcept(false) is compatible with no |
1038 | exception-specification. */ |
1039 | if (t2 == noexcept_false_spec) |
1040 | return t1 == NULL_TREE; |
1041 | |
1042 | /* Otherwise, if we aren't looking for an exact match, noexcept is |
1043 | equivalent to throw(). */ |
1044 | if (t1 == noexcept_true_spec) |
1045 | t1 = empty_except_spec; |
1046 | if (t2 == noexcept_true_spec) |
1047 | t2 = empty_except_spec; |
1048 | } |
1049 | |
1050 | /* If any noexcept is left, it is only comparable to itself; |
1051 | either we're looking for an exact match or we're redeclaring a |
1052 | template with dependent noexcept. */ |
1053 | if ((t1 && TREE_PURPOSE (t1)) |
1054 | || (t2 && TREE_PURPOSE (t2))) |
1055 | return (t1 && t2 |
1056 | && cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))); |
1057 | |
1058 | if (t1 == NULL_TREE) /* T1 is ... */ |
1059 | return t2 == NULL_TREE || exact == ce_derived; |
1060 | if (!TREE_VALUE (t1)) /* t1 is EMPTY */ |
1061 | return t2 != NULL_TREE && !TREE_VALUE (t2); |
1062 | if (t2 == NULL_TREE) /* T2 is ... */ |
1063 | return false; |
1064 | if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */ |
1065 | return exact == ce_derived; |
1066 | |
1067 | /* Neither set is ... or EMPTY, make sure each part of T2 is in T1. |
1068 | Count how many we find, to determine exactness. For exact matching and |
1069 | ordered T1, T2, this is an O(n) operation, otherwise its worst case is |
1070 | O(nm). */ |
1071 | for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2)) |
1072 | { |
1073 | for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe)) |
1074 | { |
1075 | tree a = TREE_VALUE (probe); |
1076 | tree b = TREE_VALUE (t2); |
1077 | |
1078 | if (comp_except_types (a, b, exact)) |
1079 | { |
1080 | if (probe == base && exact > ce_derived) |
1081 | base = TREE_CHAIN (probe); |
1082 | length++; |
1083 | break; |
1084 | } |
1085 | } |
1086 | if (probe == NULL_TREE) |
1087 | return false; |
1088 | } |
1089 | return exact == ce_derived || base == NULL_TREE || length == list_length (t1); |
1090 | } |
1091 | |
1092 | /* Compare the array types T1 and T2. ALLOW_REDECLARATION is true if |
1093 | [] can match [size]. */ |
1094 | |
1095 | static bool |
1096 | comp_array_types (const_tree t1, const_tree t2, bool allow_redeclaration) |
1097 | { |
1098 | tree d1; |
1099 | tree d2; |
1100 | tree max1, max2; |
1101 | |
1102 | if (t1 == t2) |
1103 | return true; |
1104 | |
1105 | /* The type of the array elements must be the same. */ |
1106 | if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))) |
1107 | return false; |
1108 | |
1109 | d1 = TYPE_DOMAIN (t1); |
1110 | d2 = TYPE_DOMAIN (t2); |
1111 | |
1112 | if (d1 == d2) |
1113 | return true; |
1114 | |
1115 | /* If one of the arrays is dimensionless, and the other has a |
1116 | dimension, they are of different types. However, it is valid to |
1117 | write: |
1118 | |
1119 | extern int a[]; |
1120 | int a[3]; |
1121 | |
1122 | by [basic.link]: |
1123 | |
1124 | declarations for an array object can specify |
1125 | array types that differ by the presence or absence of a major |
1126 | array bound (_dcl.array_). */ |
1127 | if (!d1 || !d2) |
1128 | return allow_redeclaration; |
1129 | |
1130 | /* Check that the dimensions are the same. */ |
1131 | |
1132 | if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))) |
1133 | return false; |
1134 | max1 = TYPE_MAX_VALUE (d1); |
1135 | max2 = TYPE_MAX_VALUE (d2); |
1136 | |
1137 | if (!cp_tree_equal (max1, max2)) |
1138 | return false; |
1139 | |
1140 | return true; |
1141 | } |
1142 | |
1143 | /* Compare the relative position of T1 and T2 into their respective |
1144 | template parameter list. |
1145 | T1 and T2 must be template parameter types. |
1146 | Return TRUE if T1 and T2 have the same position, FALSE otherwise. */ |
1147 | |
1148 | static bool |
1149 | comp_template_parms_position (tree t1, tree t2) |
1150 | { |
1151 | tree index1, index2; |
1152 | gcc_assert (t1 && t2 |
1153 | && TREE_CODE (t1) == TREE_CODE (t2) |
1154 | && (TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM |
1155 | || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM |
1156 | || TREE_CODE (t1) == TEMPLATE_TYPE_PARM)); |
1157 | |
1158 | index1 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1)); |
1159 | index2 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2)); |
1160 | |
1161 | /* Then compare their relative position. */ |
1162 | if (TEMPLATE_PARM_IDX (index1) != TEMPLATE_PARM_IDX (index2) |
1163 | || TEMPLATE_PARM_LEVEL (index1) != TEMPLATE_PARM_LEVEL (index2) |
1164 | || (TEMPLATE_PARM_PARAMETER_PACK (index1) |
1165 | != TEMPLATE_PARM_PARAMETER_PACK (index2))) |
1166 | return false; |
1167 | |
1168 | /* In C++14 we can end up comparing 'auto' to a normal template |
1169 | parameter. Don't confuse them. */ |
1170 | if (cxx_dialect >= cxx14 && (is_auto (t1) || is_auto (t2))) |
1171 | return TYPE_IDENTIFIER (t1) == TYPE_IDENTIFIER (t2); |
1172 | |
1173 | return true; |
1174 | } |
1175 | |
1176 | /* Subroutine in comptypes. */ |
1177 | |
1178 | static bool |
1179 | structural_comptypes (tree t1, tree t2, int strict) |
1180 | { |
1181 | if (t1 == t2) |
1182 | return true; |
1183 | |
1184 | /* Suppress errors caused by previously reported errors. */ |
1185 | if (t1 == error_mark_node || t2 == error_mark_node) |
1186 | return false; |
1187 | |
1188 | gcc_assert (TYPE_P (t1) && TYPE_P (t2)); |
1189 | |
1190 | /* TYPENAME_TYPEs should be resolved if the qualifying scope is the |
1191 | current instantiation. */ |
1192 | if (TREE_CODE (t1) == TYPENAME_TYPE) |
1193 | t1 = resolve_typename_type (t1, /*only_current_p=*/true); |
1194 | |
1195 | if (TREE_CODE (t2) == TYPENAME_TYPE) |
1196 | t2 = resolve_typename_type (t2, /*only_current_p=*/true); |
1197 | |
1198 | if (TYPE_PTRMEMFUNC_P (t1)) |
1199 | t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1); |
1200 | if (TYPE_PTRMEMFUNC_P (t2)) |
1201 | t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2); |
1202 | |
1203 | /* Different classes of types can't be compatible. */ |
1204 | if (TREE_CODE (t1) != TREE_CODE (t2)) |
1205 | return false; |
1206 | |
1207 | /* Qualifiers must match. For array types, we will check when we |
1208 | recur on the array element types. */ |
1209 | if (TREE_CODE (t1) != ARRAY_TYPE |
1210 | && cp_type_quals (t1) != cp_type_quals (t2)) |
1211 | return false; |
1212 | if (TREE_CODE (t1) == FUNCTION_TYPE |
1213 | && type_memfn_quals (t1) != type_memfn_quals (t2)) |
1214 | return false; |
1215 | /* Need to check this before TYPE_MAIN_VARIANT. |
1216 | FIXME function qualifiers should really change the main variant. */ |
1217 | if (TREE_CODE (t1) == FUNCTION_TYPE |
1218 | || TREE_CODE (t1) == METHOD_TYPE) |
1219 | { |
1220 | if (type_memfn_rqual (t1) != type_memfn_rqual (t2)) |
1221 | return false; |
1222 | if (flag_noexcept_type |
1223 | && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1), |
1224 | TYPE_RAISES_EXCEPTIONS (t2), |
1225 | ce_type)) |
1226 | return false; |
1227 | } |
1228 | |
1229 | /* Allow for two different type nodes which have essentially the same |
1230 | definition. Note that we already checked for equality of the type |
1231 | qualifiers (just above). */ |
1232 | |
1233 | if (TREE_CODE (t1) != ARRAY_TYPE |
1234 | && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2)) |
1235 | return true; |
1236 | |
1237 | |
1238 | /* Compare the types. Break out if they could be the same. */ |
1239 | switch (TREE_CODE (t1)) |
1240 | { |
1241 | case VOID_TYPE: |
1242 | case BOOLEAN_TYPE: |
1243 | /* All void and bool types are the same. */ |
1244 | break; |
1245 | |
1246 | case INTEGER_TYPE: |
1247 | case FIXED_POINT_TYPE: |
1248 | case REAL_TYPE: |
1249 | /* With these nodes, we can't determine type equivalence by |
1250 | looking at what is stored in the nodes themselves, because |
1251 | two nodes might have different TYPE_MAIN_VARIANTs but still |
1252 | represent the same type. For example, wchar_t and int could |
1253 | have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE, |
1254 | TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs |
1255 | and are distinct types. On the other hand, int and the |
1256 | following typedef |
1257 | |
1258 | typedef int INT __attribute((may_alias)); |
1259 | |
1260 | have identical properties, different TYPE_MAIN_VARIANTs, but |
1261 | represent the same type. The canonical type system keeps |
1262 | track of equivalence in this case, so we fall back on it. */ |
1263 | return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2); |
1264 | |
1265 | case TEMPLATE_TEMPLATE_PARM: |
1266 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
1267 | if (!comp_template_parms_position (t1, t2)) |
1268 | return false; |
1269 | if (!comp_template_parms |
1270 | (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)), |
1271 | DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2)))) |
1272 | return false; |
1273 | if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM) |
1274 | break; |
1275 | /* Don't check inheritance. */ |
1276 | strict = COMPARE_STRICT; |
1277 | /* Fall through. */ |
1278 | |
1279 | case RECORD_TYPE: |
1280 | case UNION_TYPE: |
1281 | if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2) |
1282 | && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2) |
1283 | || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM) |
1284 | && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2))) |
1285 | break; |
1286 | |
1287 | if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2)) |
1288 | break; |
1289 | else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1)) |
1290 | break; |
1291 | |
1292 | return false; |
1293 | |
1294 | case OFFSET_TYPE: |
1295 | if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2), |
1296 | strict & ~COMPARE_REDECLARATION)) |
1297 | return false; |
1298 | if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))) |
1299 | return false; |
1300 | break; |
1301 | |
1302 | case REFERENCE_TYPE: |
1303 | if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2)) |
1304 | return false; |
1305 | /* fall through to checks for pointer types */ |
1306 | gcc_fallthrough (); |
1307 | |
1308 | case POINTER_TYPE: |
1309 | if (TYPE_MODE (t1) != TYPE_MODE (t2) |
1310 | || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))) |
1311 | return false; |
1312 | break; |
1313 | |
1314 | case METHOD_TYPE: |
1315 | case FUNCTION_TYPE: |
1316 | if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))) |
1317 | return false; |
1318 | if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2))) |
1319 | return false; |
1320 | break; |
1321 | |
1322 | case ARRAY_TYPE: |
1323 | /* Target types must match incl. qualifiers. */ |
1324 | if (!comp_array_types (t1, t2, !!(strict & COMPARE_REDECLARATION))) |
1325 | return false; |
1326 | break; |
1327 | |
1328 | case TEMPLATE_TYPE_PARM: |
1329 | /* If T1 and T2 don't have the same relative position in their |
1330 | template parameters set, they can't be equal. */ |
1331 | if (!comp_template_parms_position (t1, t2)) |
1332 | return false; |
1333 | /* Constrained 'auto's are distinct from parms that don't have the same |
1334 | constraints. */ |
1335 | if (!equivalent_placeholder_constraints (t1, t2)) |
1336 | return false; |
1337 | break; |
1338 | |
1339 | case TYPENAME_TYPE: |
1340 | if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1), |
1341 | TYPENAME_TYPE_FULLNAME (t2))) |
1342 | return false; |
1343 | /* Qualifiers don't matter on scopes. */ |
1344 | if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1), |
1345 | TYPE_CONTEXT (t2))) |
1346 | return false; |
1347 | break; |
1348 | |
1349 | case UNBOUND_CLASS_TEMPLATE: |
1350 | if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2))) |
1351 | return false; |
1352 | if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2))) |
1353 | return false; |
1354 | break; |
1355 | |
1356 | case COMPLEX_TYPE: |
1357 | if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))) |
1358 | return false; |
1359 | break; |
1360 | |
1361 | case VECTOR_TYPE: |
1362 | if (TYPE_VECTOR_SUBPARTS (t1) != TYPE_VECTOR_SUBPARTS (t2) |
1363 | || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))) |
1364 | return false; |
1365 | break; |
1366 | |
1367 | case TYPE_PACK_EXPANSION: |
1368 | return (same_type_p (PACK_EXPANSION_PATTERN (t1), |
1369 | PACK_EXPANSION_PATTERN (t2)) |
1370 | && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1), |
1371 | PACK_EXPANSION_EXTRA_ARGS (t2))); |
1372 | |
1373 | case DECLTYPE_TYPE: |
1374 | if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1) |
1375 | != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2) |
1376 | || (DECLTYPE_FOR_LAMBDA_CAPTURE (t1) |
1377 | != DECLTYPE_FOR_LAMBDA_CAPTURE (t2)) |
1378 | || (DECLTYPE_FOR_LAMBDA_PROXY (t1) |
1379 | != DECLTYPE_FOR_LAMBDA_PROXY (t2)) |
1380 | || !cp_tree_equal (DECLTYPE_TYPE_EXPR (t1), |
1381 | DECLTYPE_TYPE_EXPR (t2))) |
1382 | return false; |
1383 | break; |
1384 | |
1385 | case UNDERLYING_TYPE: |
1386 | return same_type_p (UNDERLYING_TYPE_TYPE (t1), |
1387 | UNDERLYING_TYPE_TYPE (t2)); |
1388 | |
1389 | default: |
1390 | return false; |
1391 | } |
1392 | |
1393 | /* If we get here, we know that from a target independent POV the |
1394 | types are the same. Make sure the target attributes are also |
1395 | the same. */ |
1396 | return comp_type_attributes (t1, t2); |
1397 | } |
1398 | |
1399 | /* Return true if T1 and T2 are related as allowed by STRICT. STRICT |
1400 | is a bitwise-or of the COMPARE_* flags. */ |
1401 | |
1402 | bool |
1403 | comptypes (tree t1, tree t2, int strict) |
1404 | { |
1405 | if (strict == COMPARE_STRICT) |
1406 | { |
1407 | if (t1 == t2) |
1408 | return true; |
1409 | |
1410 | if (t1 == error_mark_node || t2 == error_mark_node) |
1411 | return false; |
1412 | |
1413 | if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2)) |
1414 | /* At least one of the types requires structural equality, so |
1415 | perform a deep check. */ |
1416 | return structural_comptypes (t1, t2, strict); |
1417 | |
1418 | if (flag_checking && USE_CANONICAL_TYPES) |
1419 | { |
1420 | bool result = structural_comptypes (t1, t2, strict); |
1421 | |
1422 | if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2)) |
1423 | /* The two types are structurally equivalent, but their |
1424 | canonical types were different. This is a failure of the |
1425 | canonical type propagation code.*/ |
1426 | internal_error |
1427 | ("canonical types differ for identical types %qT and %qT" , |
1428 | t1, t2); |
1429 | else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2)) |
1430 | /* Two types are structurally different, but the canonical |
1431 | types are the same. This means we were over-eager in |
1432 | assigning canonical types. */ |
1433 | internal_error |
1434 | ("same canonical type node for different types %qT and %qT" , |
1435 | t1, t2); |
1436 | |
1437 | return result; |
1438 | } |
1439 | if (!flag_checking && USE_CANONICAL_TYPES) |
1440 | return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2); |
1441 | else |
1442 | return structural_comptypes (t1, t2, strict); |
1443 | } |
1444 | else if (strict == COMPARE_STRUCTURAL) |
1445 | return structural_comptypes (t1, t2, COMPARE_STRICT); |
1446 | else |
1447 | return structural_comptypes (t1, t2, strict); |
1448 | } |
1449 | |
1450 | /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring |
1451 | top-level qualifiers. */ |
1452 | |
1453 | bool |
1454 | same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2) |
1455 | { |
1456 | if (type1 == error_mark_node || type2 == error_mark_node) |
1457 | return false; |
1458 | |
1459 | type1 = cp_build_qualified_type (type1, TYPE_UNQUALIFIED); |
1460 | type2 = cp_build_qualified_type (type2, TYPE_UNQUALIFIED); |
1461 | return same_type_p (type1, type2); |
1462 | } |
1463 | |
1464 | /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */ |
1465 | |
1466 | bool |
1467 | at_least_as_qualified_p (const_tree type1, const_tree type2) |
1468 | { |
1469 | int q1 = cp_type_quals (type1); |
1470 | int q2 = cp_type_quals (type2); |
1471 | |
1472 | /* All qualifiers for TYPE2 must also appear in TYPE1. */ |
1473 | return (q1 & q2) == q2; |
1474 | } |
1475 | |
1476 | /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is |
1477 | more cv-qualified that TYPE1, and 0 otherwise. */ |
1478 | |
1479 | int |
1480 | comp_cv_qualification (int q1, int q2) |
1481 | { |
1482 | if (q1 == q2) |
1483 | return 0; |
1484 | |
1485 | if ((q1 & q2) == q2) |
1486 | return 1; |
1487 | else if ((q1 & q2) == q1) |
1488 | return -1; |
1489 | |
1490 | return 0; |
1491 | } |
1492 | |
1493 | int |
1494 | comp_cv_qualification (const_tree type1, const_tree type2) |
1495 | { |
1496 | int q1 = cp_type_quals (type1); |
1497 | int q2 = cp_type_quals (type2); |
1498 | return comp_cv_qualification (q1, q2); |
1499 | } |
1500 | |
1501 | /* Returns 1 if the cv-qualification signature of TYPE1 is a proper |
1502 | subset of the cv-qualification signature of TYPE2, and the types |
1503 | are similar. Returns -1 if the other way 'round, and 0 otherwise. */ |
1504 | |
1505 | int |
1506 | comp_cv_qual_signature (tree type1, tree type2) |
1507 | { |
1508 | if (comp_ptr_ttypes_real (type2, type1, -1)) |
1509 | return 1; |
1510 | else if (comp_ptr_ttypes_real (type1, type2, -1)) |
1511 | return -1; |
1512 | else |
1513 | return 0; |
1514 | } |
1515 | |
1516 | /* Subroutines of `comptypes'. */ |
1517 | |
1518 | /* Return true if two parameter type lists PARMS1 and PARMS2 are |
1519 | equivalent in the sense that functions with those parameter types |
1520 | can have equivalent types. The two lists must be equivalent, |
1521 | element by element. */ |
1522 | |
1523 | bool |
1524 | compparms (const_tree parms1, const_tree parms2) |
1525 | { |
1526 | const_tree t1, t2; |
1527 | |
1528 | /* An unspecified parmlist matches any specified parmlist |
1529 | whose argument types don't need default promotions. */ |
1530 | |
1531 | for (t1 = parms1, t2 = parms2; |
1532 | t1 || t2; |
1533 | t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2)) |
1534 | { |
1535 | /* If one parmlist is shorter than the other, |
1536 | they fail to match. */ |
1537 | if (!t1 || !t2) |
1538 | return false; |
1539 | if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2))) |
1540 | return false; |
1541 | } |
1542 | return true; |
1543 | } |
1544 | |
1545 | |
1546 | /* Process a sizeof or alignof expression where the operand is a |
1547 | type. */ |
1548 | |
1549 | tree |
1550 | cxx_sizeof_or_alignof_type (tree type, enum tree_code op, bool complain) |
1551 | { |
1552 | tree value; |
1553 | bool dependent_p; |
1554 | |
1555 | gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR); |
1556 | if (type == error_mark_node) |
1557 | return error_mark_node; |
1558 | |
1559 | type = non_reference (type); |
1560 | if (TREE_CODE (type) == METHOD_TYPE) |
1561 | { |
1562 | if (complain) |
1563 | pedwarn (input_location, OPT_Wpointer_arith, |
1564 | "invalid application of %qs to a member function" , |
1565 | OVL_OP_INFO (false, op)->name); |
1566 | else |
1567 | return error_mark_node; |
1568 | value = size_one_node; |
1569 | } |
1570 | |
1571 | dependent_p = dependent_type_p (type); |
1572 | if (!dependent_p) |
1573 | complete_type (type); |
1574 | if (dependent_p |
1575 | /* VLA types will have a non-constant size. In the body of an |
1576 | uninstantiated template, we don't need to try to compute the |
1577 | value, because the sizeof expression is not an integral |
1578 | constant expression in that case. And, if we do try to |
1579 | compute the value, we'll likely end up with SAVE_EXPRs, which |
1580 | the template substitution machinery does not expect to see. */ |
1581 | || (processing_template_decl |
1582 | && COMPLETE_TYPE_P (type) |
1583 | && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)) |
1584 | { |
1585 | value = build_min (op, size_type_node, type); |
1586 | TREE_READONLY (value) = 1; |
1587 | return value; |
1588 | } |
1589 | |
1590 | return c_sizeof_or_alignof_type (input_location, complete_type (type), |
1591 | op == SIZEOF_EXPR, false, |
1592 | complain); |
1593 | } |
1594 | |
1595 | /* Return the size of the type, without producing any warnings for |
1596 | types whose size cannot be taken. This routine should be used only |
1597 | in some other routine that has already produced a diagnostic about |
1598 | using the size of such a type. */ |
1599 | tree |
1600 | cxx_sizeof_nowarn (tree type) |
1601 | { |
1602 | if (TREE_CODE (type) == FUNCTION_TYPE |
1603 | || VOID_TYPE_P (type) |
1604 | || TREE_CODE (type) == ERROR_MARK) |
1605 | return size_one_node; |
1606 | else if (!COMPLETE_TYPE_P (type)) |
1607 | return size_zero_node; |
1608 | else |
1609 | return cxx_sizeof_or_alignof_type (type, SIZEOF_EXPR, false); |
1610 | } |
1611 | |
1612 | /* Process a sizeof expression where the operand is an expression. */ |
1613 | |
1614 | static tree |
1615 | cxx_sizeof_expr (tree e, tsubst_flags_t complain) |
1616 | { |
1617 | if (e == error_mark_node) |
1618 | return error_mark_node; |
1619 | |
1620 | if (processing_template_decl) |
1621 | { |
1622 | e = build_min (SIZEOF_EXPR, size_type_node, e); |
1623 | TREE_SIDE_EFFECTS (e) = 0; |
1624 | TREE_READONLY (e) = 1; |
1625 | |
1626 | return e; |
1627 | } |
1628 | |
1629 | /* To get the size of a static data member declared as an array of |
1630 | unknown bound, we need to instantiate it. */ |
1631 | if (VAR_P (e) |
1632 | && VAR_HAD_UNKNOWN_BOUND (e) |
1633 | && DECL_TEMPLATE_INSTANTIATION (e)) |
1634 | instantiate_decl (e, /*defer_ok*/true, /*expl_inst_mem*/false); |
1635 | |
1636 | if (TREE_CODE (e) == PARM_DECL |
1637 | && DECL_ARRAY_PARAMETER_P (e) |
1638 | && (complain & tf_warning)) |
1639 | { |
1640 | if (warning (OPT_Wsizeof_array_argument, "%<sizeof%> on array function " |
1641 | "parameter %qE will return size of %qT" , e, TREE_TYPE (e))) |
1642 | inform (DECL_SOURCE_LOCATION (e), "declared here" ); |
1643 | } |
1644 | |
1645 | e = mark_type_use (e); |
1646 | |
1647 | if (bitfield_p (e)) |
1648 | { |
1649 | if (complain & tf_error) |
1650 | error ("invalid application of %<sizeof%> to a bit-field" ); |
1651 | else |
1652 | return error_mark_node; |
1653 | e = char_type_node; |
1654 | } |
1655 | else if (is_overloaded_fn (e)) |
1656 | { |
1657 | if (complain & tf_error) |
1658 | permerror (input_location, "ISO C++ forbids applying %<sizeof%> to an expression of " |
1659 | "function type" ); |
1660 | else |
1661 | return error_mark_node; |
1662 | e = char_type_node; |
1663 | } |
1664 | else if (type_unknown_p (e)) |
1665 | { |
1666 | if (complain & tf_error) |
1667 | cxx_incomplete_type_error (e, TREE_TYPE (e)); |
1668 | else |
1669 | return error_mark_node; |
1670 | e = char_type_node; |
1671 | } |
1672 | else |
1673 | e = TREE_TYPE (e); |
1674 | |
1675 | return cxx_sizeof_or_alignof_type (e, SIZEOF_EXPR, complain & tf_error); |
1676 | } |
1677 | |
1678 | /* Implement the __alignof keyword: Return the minimum required |
1679 | alignment of E, measured in bytes. For VAR_DECL's and |
1680 | FIELD_DECL's return DECL_ALIGN (which can be set from an |
1681 | "aligned" __attribute__ specification). */ |
1682 | |
1683 | static tree |
1684 | cxx_alignof_expr (tree e, tsubst_flags_t complain) |
1685 | { |
1686 | tree t; |
1687 | |
1688 | if (e == error_mark_node) |
1689 | return error_mark_node; |
1690 | |
1691 | if (processing_template_decl) |
1692 | { |
1693 | e = build_min (ALIGNOF_EXPR, size_type_node, e); |
1694 | TREE_SIDE_EFFECTS (e) = 0; |
1695 | TREE_READONLY (e) = 1; |
1696 | |
1697 | return e; |
1698 | } |
1699 | |
1700 | e = mark_type_use (e); |
1701 | |
1702 | if (VAR_P (e)) |
1703 | t = size_int (DECL_ALIGN_UNIT (e)); |
1704 | else if (bitfield_p (e)) |
1705 | { |
1706 | if (complain & tf_error) |
1707 | error ("invalid application of %<__alignof%> to a bit-field" ); |
1708 | else |
1709 | return error_mark_node; |
1710 | t = size_one_node; |
1711 | } |
1712 | else if (TREE_CODE (e) == COMPONENT_REF |
1713 | && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL) |
1714 | t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1))); |
1715 | else if (is_overloaded_fn (e)) |
1716 | { |
1717 | if (complain & tf_error) |
1718 | permerror (input_location, "ISO C++ forbids applying %<__alignof%> to an expression of " |
1719 | "function type" ); |
1720 | else |
1721 | return error_mark_node; |
1722 | if (TREE_CODE (e) == FUNCTION_DECL) |
1723 | t = size_int (DECL_ALIGN_UNIT (e)); |
1724 | else |
1725 | t = size_one_node; |
1726 | } |
1727 | else if (type_unknown_p (e)) |
1728 | { |
1729 | if (complain & tf_error) |
1730 | cxx_incomplete_type_error (e, TREE_TYPE (e)); |
1731 | else |
1732 | return error_mark_node; |
1733 | t = size_one_node; |
1734 | } |
1735 | else |
1736 | return cxx_sizeof_or_alignof_type (TREE_TYPE (e), ALIGNOF_EXPR, |
1737 | complain & tf_error); |
1738 | |
1739 | return fold_convert (size_type_node, t); |
1740 | } |
1741 | |
1742 | /* Process a sizeof or alignof expression E with code OP where the operand |
1743 | is an expression. */ |
1744 | |
1745 | tree |
1746 | cxx_sizeof_or_alignof_expr (tree e, enum tree_code op, bool complain) |
1747 | { |
1748 | if (op == SIZEOF_EXPR) |
1749 | return cxx_sizeof_expr (e, complain? tf_warning_or_error : tf_none); |
1750 | else |
1751 | return cxx_alignof_expr (e, complain? tf_warning_or_error : tf_none); |
1752 | } |
1753 | |
1754 | /* Build a representation of an expression 'alignas(E).' Return the |
1755 | folded integer value of E if it is an integral constant expression |
1756 | that resolves to a valid alignment. If E depends on a template |
1757 | parameter, return a syntactic representation tree of kind |
1758 | ALIGNOF_EXPR. Otherwise, return an error_mark_node if the |
1759 | expression is ill formed, or NULL_TREE if E is NULL_TREE. */ |
1760 | |
1761 | tree |
1762 | cxx_alignas_expr (tree e) |
1763 | { |
1764 | if (e == NULL_TREE || e == error_mark_node |
1765 | || (!TYPE_P (e) && !require_potential_rvalue_constant_expression (e))) |
1766 | return e; |
1767 | |
1768 | if (TYPE_P (e)) |
1769 | /* [dcl.align]/3: |
1770 | |
1771 | When the alignment-specifier is of the form |
1772 | alignas(type-id ), it shall have the same effect as |
1773 | alignas(alignof(type-id )). */ |
1774 | |
1775 | return cxx_sizeof_or_alignof_type (e, ALIGNOF_EXPR, false); |
1776 | |
1777 | /* If we reach this point, it means the alignas expression if of |
1778 | the form "alignas(assignment-expression)", so we should follow |
1779 | what is stated by [dcl.align]/2. */ |
1780 | |
1781 | if (value_dependent_expression_p (e)) |
1782 | /* Leave value-dependent expression alone for now. */ |
1783 | return e; |
1784 | |
1785 | e = instantiate_non_dependent_expr (e); |
1786 | e = mark_rvalue_use (e); |
1787 | |
1788 | /* [dcl.align]/2 says: |
1789 | |
1790 | the assignment-expression shall be an integral constant |
1791 | expression. */ |
1792 | |
1793 | if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (e))) |
1794 | { |
1795 | error ("%<alignas%> argument has non-integral type %qT" , TREE_TYPE (e)); |
1796 | return error_mark_node; |
1797 | } |
1798 | |
1799 | return cxx_constant_value (e); |
1800 | } |
1801 | |
1802 | |
1803 | /* EXPR is being used in a context that is not a function call. |
1804 | Enforce: |
1805 | |
1806 | [expr.ref] |
1807 | |
1808 | The expression can be used only as the left-hand operand of a |
1809 | member function call. |
1810 | |
1811 | [expr.mptr.operator] |
1812 | |
1813 | If the result of .* or ->* is a function, then that result can be |
1814 | used only as the operand for the function call operator (). |
1815 | |
1816 | by issuing an error message if appropriate. Returns true iff EXPR |
1817 | violates these rules. */ |
1818 | |
1819 | bool |
1820 | invalid_nonstatic_memfn_p (location_t loc, tree expr, tsubst_flags_t complain) |
1821 | { |
1822 | if (expr == NULL_TREE) |
1823 | return false; |
1824 | /* Don't enforce this in MS mode. */ |
1825 | if (flag_ms_extensions) |
1826 | return false; |
1827 | if (is_overloaded_fn (expr) && !really_overloaded_fn (expr)) |
1828 | expr = get_first_fn (expr); |
1829 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (expr)) |
1830 | { |
1831 | if (complain & tf_error) |
1832 | { |
1833 | if (DECL_P (expr)) |
1834 | { |
1835 | error_at (loc, "invalid use of non-static member function %qD" , |
1836 | expr); |
1837 | inform (DECL_SOURCE_LOCATION (expr), "declared here" ); |
1838 | } |
1839 | else |
1840 | error_at (loc, "invalid use of non-static member function of " |
1841 | "type %qT" , TREE_TYPE (expr)); |
1842 | } |
1843 | return true; |
1844 | } |
1845 | return false; |
1846 | } |
1847 | |
1848 | /* If EXP is a reference to a bitfield, and the type of EXP does not |
1849 | match the declared type of the bitfield, return the declared type |
1850 | of the bitfield. Otherwise, return NULL_TREE. */ |
1851 | |
1852 | tree |
1853 | is_bitfield_expr_with_lowered_type (const_tree exp) |
1854 | { |
1855 | switch (TREE_CODE (exp)) |
1856 | { |
1857 | case COND_EXPR: |
1858 | if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1) |
1859 | ? TREE_OPERAND (exp, 1) |
1860 | : TREE_OPERAND (exp, 0))) |
1861 | return NULL_TREE; |
1862 | return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2)); |
1863 | |
1864 | case COMPOUND_EXPR: |
1865 | return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)); |
1866 | |
1867 | case MODIFY_EXPR: |
1868 | case SAVE_EXPR: |
1869 | return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0)); |
1870 | |
1871 | case COMPONENT_REF: |
1872 | { |
1873 | tree field; |
1874 | |
1875 | field = TREE_OPERAND (exp, 1); |
1876 | if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field)) |
1877 | return NULL_TREE; |
1878 | if (same_type_ignoring_top_level_qualifiers_p |
1879 | (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field))) |
1880 | return NULL_TREE; |
1881 | return DECL_BIT_FIELD_TYPE (field); |
1882 | } |
1883 | |
1884 | case VAR_DECL: |
1885 | if (DECL_HAS_VALUE_EXPR_P (exp)) |
1886 | return is_bitfield_expr_with_lowered_type (DECL_VALUE_EXPR |
1887 | (CONST_CAST_TREE (exp))); |
1888 | return NULL_TREE; |
1889 | |
1890 | CASE_CONVERT: |
1891 | if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp, 0))) |
1892 | == TYPE_MAIN_VARIANT (TREE_TYPE (exp))) |
1893 | return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0)); |
1894 | /* Fallthrough. */ |
1895 | |
1896 | default: |
1897 | return NULL_TREE; |
1898 | } |
1899 | } |
1900 | |
1901 | /* Like is_bitfield_with_lowered_type, except that if EXP is not a |
1902 | bitfield with a lowered type, the type of EXP is returned, rather |
1903 | than NULL_TREE. */ |
1904 | |
1905 | tree |
1906 | unlowered_expr_type (const_tree exp) |
1907 | { |
1908 | tree type; |
1909 | tree etype = TREE_TYPE (exp); |
1910 | |
1911 | type = is_bitfield_expr_with_lowered_type (exp); |
1912 | if (type) |
1913 | type = cp_build_qualified_type (type, cp_type_quals (etype)); |
1914 | else |
1915 | type = etype; |
1916 | |
1917 | return type; |
1918 | } |
1919 | |
1920 | /* Perform the conversions in [expr] that apply when an lvalue appears |
1921 | in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and |
1922 | function-to-pointer conversions. In addition, bitfield references are |
1923 | converted to their declared types. Note that this function does not perform |
1924 | the lvalue-to-rvalue conversion for class types. If you need that conversion |
1925 | for class types, then you probably need to use force_rvalue. |
1926 | |
1927 | Although the returned value is being used as an rvalue, this |
1928 | function does not wrap the returned expression in a |
1929 | NON_LVALUE_EXPR; the caller is expected to be mindful of the fact |
1930 | that the return value is no longer an lvalue. */ |
1931 | |
1932 | tree |
1933 | decay_conversion (tree exp, |
1934 | tsubst_flags_t complain, |
1935 | bool reject_builtin /* = true */) |
1936 | { |
1937 | tree type; |
1938 | enum tree_code code; |
1939 | location_t loc = EXPR_LOC_OR_LOC (exp, input_location); |
1940 | |
1941 | type = TREE_TYPE (exp); |
1942 | if (type == error_mark_node) |
1943 | return error_mark_node; |
1944 | |
1945 | exp = resolve_nondeduced_context (exp, complain); |
1946 | if (type_unknown_p (exp)) |
1947 | { |
1948 | if (complain & tf_error) |
1949 | cxx_incomplete_type_error (exp, TREE_TYPE (exp)); |
1950 | return error_mark_node; |
1951 | } |
1952 | |
1953 | code = TREE_CODE (type); |
1954 | |
1955 | if (error_operand_p (exp)) |
1956 | return error_mark_node; |
1957 | |
1958 | if (NULLPTR_TYPE_P (type) && !TREE_SIDE_EFFECTS (exp)) |
1959 | return nullptr_node; |
1960 | |
1961 | /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue. |
1962 | Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */ |
1963 | if (code == VOID_TYPE) |
1964 | { |
1965 | if (complain & tf_error) |
1966 | error_at (loc, "void value not ignored as it ought to be" ); |
1967 | return error_mark_node; |
1968 | } |
1969 | if (invalid_nonstatic_memfn_p (loc, exp, complain)) |
1970 | return error_mark_node; |
1971 | if (code == FUNCTION_TYPE || is_overloaded_fn (exp)) |
1972 | { |
1973 | exp = mark_lvalue_use (exp); |
1974 | if (reject_builtin && reject_gcc_builtin (exp, loc)) |
1975 | return error_mark_node; |
1976 | return cp_build_addr_expr (exp, complain); |
1977 | } |
1978 | if (code == ARRAY_TYPE) |
1979 | { |
1980 | tree adr; |
1981 | tree ptrtype; |
1982 | |
1983 | exp = mark_lvalue_use (exp); |
1984 | |
1985 | if (INDIRECT_REF_P (exp)) |
1986 | return build_nop (build_pointer_type (TREE_TYPE (type)), |
1987 | TREE_OPERAND (exp, 0)); |
1988 | |
1989 | if (TREE_CODE (exp) == COMPOUND_EXPR) |
1990 | { |
1991 | tree op1 = decay_conversion (TREE_OPERAND (exp, 1), complain); |
1992 | if (op1 == error_mark_node) |
1993 | return error_mark_node; |
1994 | return build2 (COMPOUND_EXPR, TREE_TYPE (op1), |
1995 | TREE_OPERAND (exp, 0), op1); |
1996 | } |
1997 | |
1998 | if (!obvalue_p (exp) |
1999 | && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp))) |
2000 | { |
2001 | if (complain & tf_error) |
2002 | error_at (loc, "invalid use of non-lvalue array" ); |
2003 | return error_mark_node; |
2004 | } |
2005 | |
2006 | /* Don't let an array compound literal decay to a pointer. It can |
2007 | still be used to initialize an array or bind to a reference. */ |
2008 | if (TREE_CODE (exp) == TARGET_EXPR) |
2009 | { |
2010 | if (complain & tf_error) |
2011 | error_at (loc, "taking address of temporary array" ); |
2012 | return error_mark_node; |
2013 | } |
2014 | |
2015 | ptrtype = build_pointer_type (TREE_TYPE (type)); |
2016 | |
2017 | if (VAR_P (exp)) |
2018 | { |
2019 | if (!cxx_mark_addressable (exp)) |
2020 | return error_mark_node; |
2021 | adr = build_nop (ptrtype, build_address (exp)); |
2022 | return adr; |
2023 | } |
2024 | /* This way is better for a COMPONENT_REF since it can |
2025 | simplify the offset for a component. */ |
2026 | adr = cp_build_addr_expr (exp, complain); |
2027 | return cp_convert (ptrtype, adr, complain); |
2028 | } |
2029 | |
2030 | /* Otherwise, it's the lvalue-to-rvalue conversion. */ |
2031 | exp = mark_rvalue_use (exp, loc, reject_builtin); |
2032 | |
2033 | /* If a bitfield is used in a context where integral promotion |
2034 | applies, then the caller is expected to have used |
2035 | default_conversion. That function promotes bitfields correctly |
2036 | before calling this function. At this point, if we have a |
2037 | bitfield referenced, we may assume that is not subject to |
2038 | promotion, and that, therefore, the type of the resulting rvalue |
2039 | is the declared type of the bitfield. */ |
2040 | exp = convert_bitfield_to_declared_type (exp); |
2041 | |
2042 | /* We do not call rvalue() here because we do not want to wrap EXP |
2043 | in a NON_LVALUE_EXPR. */ |
2044 | |
2045 | /* [basic.lval] |
2046 | |
2047 | Non-class rvalues always have cv-unqualified types. */ |
2048 | type = TREE_TYPE (exp); |
2049 | if (!CLASS_TYPE_P (type) && cv_qualified_p (type)) |
2050 | exp = build_nop (cv_unqualified (type), exp); |
2051 | |
2052 | if (!complete_type_or_maybe_complain (type, exp, complain)) |
2053 | return error_mark_node; |
2054 | |
2055 | return exp; |
2056 | } |
2057 | |
2058 | /* Perform preparatory conversions, as part of the "usual arithmetic |
2059 | conversions". In particular, as per [expr]: |
2060 | |
2061 | Whenever an lvalue expression appears as an operand of an |
2062 | operator that expects the rvalue for that operand, the |
2063 | lvalue-to-rvalue, array-to-pointer, or function-to-pointer |
2064 | standard conversions are applied to convert the expression to an |
2065 | rvalue. |
2066 | |
2067 | In addition, we perform integral promotions here, as those are |
2068 | applied to both operands to a binary operator before determining |
2069 | what additional conversions should apply. */ |
2070 | |
2071 | static tree |
2072 | cp_default_conversion (tree exp, tsubst_flags_t complain) |
2073 | { |
2074 | /* Check for target-specific promotions. */ |
2075 | tree promoted_type = targetm.promoted_type (TREE_TYPE (exp)); |
2076 | if (promoted_type) |
2077 | exp = cp_convert (promoted_type, exp, complain); |
2078 | /* Perform the integral promotions first so that bitfield |
2079 | expressions (which may promote to "int", even if the bitfield is |
2080 | declared "unsigned") are promoted correctly. */ |
2081 | else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp))) |
2082 | exp = cp_perform_integral_promotions (exp, complain); |
2083 | /* Perform the other conversions. */ |
2084 | exp = decay_conversion (exp, complain); |
2085 | |
2086 | return exp; |
2087 | } |
2088 | |
2089 | /* C version. */ |
2090 | |
2091 | tree |
2092 | default_conversion (tree exp) |
2093 | { |
2094 | return cp_default_conversion (exp, tf_warning_or_error); |
2095 | } |
2096 | |
2097 | /* EXPR is an expression with an integral or enumeration type. |
2098 | Perform the integral promotions in [conv.prom], and return the |
2099 | converted value. */ |
2100 | |
2101 | tree |
2102 | cp_perform_integral_promotions (tree expr, tsubst_flags_t complain) |
2103 | { |
2104 | tree type; |
2105 | tree promoted_type; |
2106 | |
2107 | expr = mark_rvalue_use (expr); |
2108 | |
2109 | /* [conv.prom] |
2110 | |
2111 | If the bitfield has an enumerated type, it is treated as any |
2112 | other value of that type for promotion purposes. */ |
2113 | type = is_bitfield_expr_with_lowered_type (expr); |
2114 | if (!type || TREE_CODE (type) != ENUMERAL_TYPE) |
2115 | type = TREE_TYPE (expr); |
2116 | gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type)); |
2117 | /* Scoped enums don't promote. */ |
2118 | if (SCOPED_ENUM_P (type)) |
2119 | return expr; |
2120 | promoted_type = type_promotes_to (type); |
2121 | if (type != promoted_type) |
2122 | expr = cp_convert (promoted_type, expr, complain); |
2123 | return expr; |
2124 | } |
2125 | |
2126 | /* C version. */ |
2127 | |
2128 | tree |
2129 | perform_integral_promotions (tree expr) |
2130 | { |
2131 | return cp_perform_integral_promotions (expr, tf_warning_or_error); |
2132 | } |
2133 | |
2134 | /* Returns nonzero iff exp is a STRING_CST or the result of applying |
2135 | decay_conversion to one. */ |
2136 | |
2137 | int |
2138 | string_conv_p (const_tree totype, const_tree exp, int warn) |
2139 | { |
2140 | tree t; |
2141 | |
2142 | if (!TYPE_PTR_P (totype)) |
2143 | return 0; |
2144 | |
2145 | t = TREE_TYPE (totype); |
2146 | if (!same_type_p (t, char_type_node) |
2147 | && !same_type_p (t, char16_type_node) |
2148 | && !same_type_p (t, char32_type_node) |
2149 | && !same_type_p (t, wchar_type_node)) |
2150 | return 0; |
2151 | |
2152 | if (TREE_CODE (exp) == STRING_CST) |
2153 | { |
2154 | /* Make sure that we don't try to convert between char and wide chars. */ |
2155 | if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t)) |
2156 | return 0; |
2157 | } |
2158 | else |
2159 | { |
2160 | /* Is this a string constant which has decayed to 'const char *'? */ |
2161 | t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST)); |
2162 | if (!same_type_p (TREE_TYPE (exp), t)) |
2163 | return 0; |
2164 | STRIP_NOPS (exp); |
2165 | if (TREE_CODE (exp) != ADDR_EXPR |
2166 | || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST) |
2167 | return 0; |
2168 | } |
2169 | if (warn) |
2170 | { |
2171 | if (cxx_dialect >= cxx11) |
2172 | pedwarn (input_location, OPT_Wwrite_strings, |
2173 | "ISO C++ forbids converting a string constant to %qT" , |
2174 | totype); |
2175 | else |
2176 | warning (OPT_Wwrite_strings, |
2177 | "deprecated conversion from string constant to %qT" , |
2178 | totype); |
2179 | } |
2180 | |
2181 | return 1; |
2182 | } |
2183 | |
2184 | /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we |
2185 | can, for example, use as an lvalue. This code used to be in |
2186 | unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c' |
2187 | expressions, where we're dealing with aggregates. But now it's again only |
2188 | called from unary_complex_lvalue. The case (in particular) that led to |
2189 | this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd |
2190 | get it there. */ |
2191 | |
2192 | static tree |
2193 | rationalize_conditional_expr (enum tree_code code, tree t, |
2194 | tsubst_flags_t complain) |
2195 | { |
2196 | location_t loc = EXPR_LOC_OR_LOC (t, input_location); |
2197 | |
2198 | /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that |
2199 | the first operand is always the one to be used if both operands |
2200 | are equal, so we know what conditional expression this used to be. */ |
2201 | if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR) |
2202 | { |
2203 | tree op0 = TREE_OPERAND (t, 0); |
2204 | tree op1 = TREE_OPERAND (t, 1); |
2205 | |
2206 | /* The following code is incorrect if either operand side-effects. */ |
2207 | gcc_assert (!TREE_SIDE_EFFECTS (op0) |
2208 | && !TREE_SIDE_EFFECTS (op1)); |
2209 | return |
2210 | build_conditional_expr (loc, |
2211 | build_x_binary_op (loc, |
2212 | (TREE_CODE (t) == MIN_EXPR |
2213 | ? LE_EXPR : GE_EXPR), |
2214 | op0, TREE_CODE (op0), |
2215 | op1, TREE_CODE (op1), |
2216 | /*overload=*/NULL, |
2217 | complain), |
2218 | cp_build_unary_op (code, op0, false, complain), |
2219 | cp_build_unary_op (code, op1, false, complain), |
2220 | complain); |
2221 | } |
2222 | |
2223 | return |
2224 | build_conditional_expr (loc, TREE_OPERAND (t, 0), |
2225 | cp_build_unary_op (code, TREE_OPERAND (t, 1), false, |
2226 | complain), |
2227 | cp_build_unary_op (code, TREE_OPERAND (t, 2), false, |
2228 | complain), |
2229 | complain); |
2230 | } |
2231 | |
2232 | /* Given the TYPE of an anonymous union field inside T, return the |
2233 | FIELD_DECL for the field. If not found return NULL_TREE. Because |
2234 | anonymous unions can nest, we must also search all anonymous unions |
2235 | that are directly reachable. */ |
2236 | |
2237 | tree |
2238 | lookup_anon_field (tree t, tree type) |
2239 | { |
2240 | tree field; |
2241 | |
2242 | t = TYPE_MAIN_VARIANT (t); |
2243 | |
2244 | for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field)) |
2245 | { |
2246 | if (TREE_STATIC (field)) |
2247 | continue; |
2248 | if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field)) |
2249 | continue; |
2250 | |
2251 | /* If we find it directly, return the field. */ |
2252 | if (DECL_NAME (field) == NULL_TREE |
2253 | && type == TYPE_MAIN_VARIANT (TREE_TYPE (field))) |
2254 | { |
2255 | return field; |
2256 | } |
2257 | |
2258 | /* Otherwise, it could be nested, search harder. */ |
2259 | if (DECL_NAME (field) == NULL_TREE |
2260 | && ANON_AGGR_TYPE_P (TREE_TYPE (field))) |
2261 | { |
2262 | tree subfield = lookup_anon_field (TREE_TYPE (field), type); |
2263 | if (subfield) |
2264 | return subfield; |
2265 | } |
2266 | } |
2267 | return NULL_TREE; |
2268 | } |
2269 | |
2270 | /* Build an expression representing OBJECT.MEMBER. OBJECT is an |
2271 | expression; MEMBER is a DECL or baselink. If ACCESS_PATH is |
2272 | non-NULL, it indicates the path to the base used to name MEMBER. |
2273 | If PRESERVE_REFERENCE is true, the expression returned will have |
2274 | REFERENCE_TYPE if the MEMBER does. Otherwise, the expression |
2275 | returned will have the type referred to by the reference. |
2276 | |
2277 | This function does not perform access control; that is either done |
2278 | earlier by the parser when the name of MEMBER is resolved to MEMBER |
2279 | itself, or later when overload resolution selects one of the |
2280 | functions indicated by MEMBER. */ |
2281 | |
2282 | tree |
2283 | build_class_member_access_expr (cp_expr object, tree member, |
2284 | tree access_path, bool preserve_reference, |
2285 | tsubst_flags_t complain) |
2286 | { |
2287 | tree object_type; |
2288 | tree member_scope; |
2289 | tree result = NULL_TREE; |
2290 | tree using_decl = NULL_TREE; |
2291 | |
2292 | if (error_operand_p (object) || error_operand_p (member)) |
2293 | return error_mark_node; |
2294 | |
2295 | gcc_assert (DECL_P (member) || BASELINK_P (member)); |
2296 | |
2297 | /* [expr.ref] |
2298 | |
2299 | The type of the first expression shall be "class object" (of a |
2300 | complete type). */ |
2301 | object_type = TREE_TYPE (object); |
2302 | if (!currently_open_class (object_type) |
2303 | && !complete_type_or_maybe_complain (object_type, object, complain)) |
2304 | return error_mark_node; |
2305 | if (!CLASS_TYPE_P (object_type)) |
2306 | { |
2307 | if (complain & tf_error) |
2308 | { |
2309 | if (POINTER_TYPE_P (object_type) |
2310 | && CLASS_TYPE_P (TREE_TYPE (object_type))) |
2311 | error ("request for member %qD in %qE, which is of pointer " |
2312 | "type %qT (maybe you meant to use %<->%> ?)" , |
2313 | member, object.get_value (), object_type); |
2314 | else |
2315 | error ("request for member %qD in %qE, which is of non-class " |
2316 | "type %qT" , member, object.get_value (), object_type); |
2317 | } |
2318 | return error_mark_node; |
2319 | } |
2320 | |
2321 | /* The standard does not seem to actually say that MEMBER must be a |
2322 | member of OBJECT_TYPE. However, that is clearly what is |
2323 | intended. */ |
2324 | if (DECL_P (member)) |
2325 | { |
2326 | member_scope = DECL_CLASS_CONTEXT (member); |
2327 | if (!mark_used (member, complain) && !(complain & tf_error)) |
2328 | return error_mark_node; |
2329 | if (TREE_DEPRECATED (member)) |
2330 | warn_deprecated_use (member, NULL_TREE); |
2331 | } |
2332 | else |
2333 | member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member)); |
2334 | /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will |
2335 | presently be the anonymous union. Go outwards until we find a |
2336 | type related to OBJECT_TYPE. */ |
2337 | while ((ANON_AGGR_TYPE_P (member_scope) || UNSCOPED_ENUM_P (member_scope)) |
2338 | && !same_type_ignoring_top_level_qualifiers_p (member_scope, |
2339 | object_type)) |
2340 | member_scope = TYPE_CONTEXT (member_scope); |
2341 | if (!member_scope || !DERIVED_FROM_P (member_scope, object_type)) |
2342 | { |
2343 | if (complain & tf_error) |
2344 | { |
2345 | if (TREE_CODE (member) == FIELD_DECL) |
2346 | error ("invalid use of nonstatic data member %qE" , member); |
2347 | else |
2348 | error ("%qD is not a member of %qT" , member, object_type); |
2349 | } |
2350 | return error_mark_node; |
2351 | } |
2352 | |
2353 | /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into |
2354 | `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue |
2355 | in the front end; only _DECLs and _REFs are lvalues in the back end. */ |
2356 | { |
2357 | tree temp = unary_complex_lvalue (ADDR_EXPR, object); |
2358 | if (temp) |
2359 | object = cp_build_fold_indirect_ref (temp); |
2360 | } |
2361 | |
2362 | /* In [expr.ref], there is an explicit list of the valid choices for |
2363 | MEMBER. We check for each of those cases here. */ |
2364 | if (VAR_P (member)) |
2365 | { |
2366 | /* A static data member. */ |
2367 | result = member; |
2368 | mark_exp_read (object); |
2369 | /* If OBJECT has side-effects, they are supposed to occur. */ |
2370 | if (TREE_SIDE_EFFECTS (object)) |
2371 | result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result); |
2372 | } |
2373 | else if (TREE_CODE (member) == FIELD_DECL) |
2374 | { |
2375 | /* A non-static data member. */ |
2376 | bool null_object_p; |
2377 | int type_quals; |
2378 | tree member_type; |
2379 | |
2380 | if (INDIRECT_REF_P (object)) |
2381 | null_object_p = |
2382 | integer_zerop (tree_strip_nop_conversions (TREE_OPERAND (object, 0))); |
2383 | else |
2384 | null_object_p = false; |
2385 | |
2386 | /* Convert OBJECT to the type of MEMBER. */ |
2387 | if (!same_type_p (TYPE_MAIN_VARIANT (object_type), |
2388 | TYPE_MAIN_VARIANT (member_scope))) |
2389 | { |
2390 | tree binfo; |
2391 | base_kind kind; |
2392 | |
2393 | binfo = lookup_base (access_path ? access_path : object_type, |
2394 | member_scope, ba_unique, &kind, complain); |
2395 | if (binfo == error_mark_node) |
2396 | return error_mark_node; |
2397 | |
2398 | /* It is invalid to try to get to a virtual base of a |
2399 | NULL object. The most common cause is invalid use of |
2400 | offsetof macro. */ |
2401 | if (null_object_p && kind == bk_via_virtual) |
2402 | { |
2403 | if (complain & tf_error) |
2404 | { |
2405 | error ("invalid access to non-static data member %qD in " |
2406 | "virtual base of NULL object" , member); |
2407 | } |
2408 | return error_mark_node; |
2409 | } |
2410 | |
2411 | /* Convert to the base. */ |
2412 | object = build_base_path (PLUS_EXPR, object, binfo, |
2413 | /*nonnull=*/1, complain); |
2414 | /* If we found the base successfully then we should be able |
2415 | to convert to it successfully. */ |
2416 | gcc_assert (object != error_mark_node); |
2417 | } |
2418 | |
2419 | /* If MEMBER is from an anonymous aggregate, we have converted |
2420 | OBJECT so that it refers to the class containing the |
2421 | anonymous union. Generate a reference to the anonymous union |
2422 | itself, and recur to find MEMBER. */ |
2423 | if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member)) |
2424 | /* When this code is called from build_field_call, the |
2425 | object already has the type of the anonymous union. |
2426 | That is because the COMPONENT_REF was already |
2427 | constructed, and was then disassembled before calling |
2428 | build_field_call. After the function-call code is |
2429 | cleaned up, this waste can be eliminated. */ |
2430 | && (!same_type_ignoring_top_level_qualifiers_p |
2431 | (TREE_TYPE (object), DECL_CONTEXT (member)))) |
2432 | { |
2433 | tree anonymous_union; |
2434 | |
2435 | anonymous_union = lookup_anon_field (TREE_TYPE (object), |
2436 | DECL_CONTEXT (member)); |
2437 | object = build_class_member_access_expr (object, |
2438 | anonymous_union, |
2439 | /*access_path=*/NULL_TREE, |
2440 | preserve_reference, |
2441 | complain); |
2442 | } |
2443 | |
2444 | /* Compute the type of the field, as described in [expr.ref]. */ |
2445 | type_quals = TYPE_UNQUALIFIED; |
2446 | member_type = TREE_TYPE (member); |
2447 | if (TREE_CODE (member_type) != REFERENCE_TYPE) |
2448 | { |
2449 | type_quals = (cp_type_quals (member_type) |
2450 | | cp_type_quals (object_type)); |
2451 | |
2452 | /* A field is const (volatile) if the enclosing object, or the |
2453 | field itself, is const (volatile). But, a mutable field is |
2454 | not const, even within a const object. */ |
2455 | if (DECL_MUTABLE_P (member)) |
2456 | type_quals &= ~TYPE_QUAL_CONST; |
2457 | member_type = cp_build_qualified_type (member_type, type_quals); |
2458 | } |
2459 | |
2460 | result = build3_loc (input_location, COMPONENT_REF, member_type, |
2461 | object, member, NULL_TREE); |
2462 | |
2463 | /* Mark the expression const or volatile, as appropriate. Even |
2464 | though we've dealt with the type above, we still have to mark the |
2465 | expression itself. */ |
2466 | if (type_quals & TYPE_QUAL_CONST) |
2467 | TREE_READONLY (result) = 1; |
2468 | if (type_quals & TYPE_QUAL_VOLATILE) |
2469 | TREE_THIS_VOLATILE (result) = 1; |
2470 | } |
2471 | else if (BASELINK_P (member)) |
2472 | { |
2473 | /* The member is a (possibly overloaded) member function. */ |
2474 | tree functions; |
2475 | tree type; |
2476 | |
2477 | /* If the MEMBER is exactly one static member function, then we |
2478 | know the type of the expression. Otherwise, we must wait |
2479 | until overload resolution has been performed. */ |
2480 | functions = BASELINK_FUNCTIONS (member); |
2481 | if (TREE_CODE (functions) == FUNCTION_DECL |
2482 | && DECL_STATIC_FUNCTION_P (functions)) |
2483 | type = TREE_TYPE (functions); |
2484 | else |
2485 | type = unknown_type_node; |
2486 | /* Note that we do not convert OBJECT to the BASELINK_BINFO |
2487 | base. That will happen when the function is called. */ |
2488 | result = build3 (COMPONENT_REF, type, object, member, NULL_TREE); |
2489 | } |
2490 | else if (TREE_CODE (member) == CONST_DECL) |
2491 | { |
2492 | /* The member is an enumerator. */ |
2493 | result = member; |
2494 | /* If OBJECT has side-effects, they are supposed to occur. */ |
2495 | if (TREE_SIDE_EFFECTS (object)) |
2496 | result = build2 (COMPOUND_EXPR, TREE_TYPE (result), |
2497 | object, result); |
2498 | } |
2499 | else if ((using_decl = strip_using_decl (member)) != member) |
2500 | result = build_class_member_access_expr (object, |
2501 | using_decl, |
2502 | access_path, preserve_reference, |
2503 | complain); |
2504 | else |
2505 | { |
2506 | if (complain & tf_error) |
2507 | error ("invalid use of %qD" , member); |
2508 | return error_mark_node; |
2509 | } |
2510 | |
2511 | if (!preserve_reference) |
2512 | /* [expr.ref] |
2513 | |
2514 | If E2 is declared to have type "reference to T", then ... the |
2515 | type of E1.E2 is T. */ |
2516 | result = convert_from_reference (result); |
2517 | |
2518 | return result; |
2519 | } |
2520 | |
2521 | /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if |
2522 | SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */ |
2523 | |
2524 | static tree |
2525 | lookup_destructor (tree object, tree scope, tree dtor_name, |
2526 | tsubst_flags_t complain) |
2527 | { |
2528 | tree object_type = TREE_TYPE (object); |
2529 | tree dtor_type = TREE_OPERAND (dtor_name, 0); |
2530 | tree expr; |
2531 | |
2532 | /* We've already complained about this destructor. */ |
2533 | if (dtor_type == error_mark_node) |
2534 | return error_mark_node; |
2535 | |
2536 | if (scope && !check_dtor_name (scope, dtor_type)) |
2537 | { |
2538 | if (complain & tf_error) |
2539 | error ("qualified type %qT does not match destructor name ~%qT" , |
2540 | scope, dtor_type); |
2541 | return error_mark_node; |
2542 | } |
2543 | if (is_auto (dtor_type)) |
2544 | dtor_type = object_type; |
2545 | else if (identifier_p (dtor_type)) |
2546 | { |
2547 | /* In a template, names we can't find a match for are still accepted |
2548 | destructor names, and we check them here. */ |
2549 | if (check_dtor_name (object_type, dtor_type)) |
2550 | dtor_type = object_type; |
2551 | else |
2552 | { |
2553 | if (complain & tf_error) |
2554 | error ("object type %qT does not match destructor name ~%qT" , |
2555 | object_type, dtor_type); |
2556 | return error_mark_node; |
2557 | } |
2558 | |
2559 | } |
2560 | else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type))) |
2561 | { |
2562 | if (complain & tf_error) |
2563 | error ("the type being destroyed is %qT, but the destructor " |
2564 | "refers to %qT" , TYPE_MAIN_VARIANT (object_type), dtor_type); |
2565 | return error_mark_node; |
2566 | } |
2567 | expr = lookup_member (dtor_type, complete_dtor_identifier, |
2568 | /*protect=*/1, /*want_type=*/false, |
2569 | tf_warning_or_error); |
2570 | if (!expr) |
2571 | { |
2572 | if (complain & tf_error) |
2573 | cxx_incomplete_type_error (dtor_name, dtor_type); |
2574 | return error_mark_node; |
2575 | } |
2576 | expr = (adjust_result_of_qualified_name_lookup |
2577 | (expr, dtor_type, object_type)); |
2578 | if (scope == NULL_TREE) |
2579 | /* We need to call adjust_result_of_qualified_name_lookup in case the |
2580 | destructor names a base class, but we unset BASELINK_QUALIFIED_P so |
2581 | that we still get virtual function binding. */ |
2582 | BASELINK_QUALIFIED_P (expr) = false; |
2583 | return expr; |
2584 | } |
2585 | |
2586 | /* An expression of the form "A::template B" has been resolved to |
2587 | DECL. Issue a diagnostic if B is not a template or template |
2588 | specialization. */ |
2589 | |
2590 | void |
2591 | check_template_keyword (tree decl) |
2592 | { |
2593 | /* The standard says: |
2594 | |
2595 | [temp.names] |
2596 | |
2597 | If a name prefixed by the keyword template is not a member |
2598 | template, the program is ill-formed. |
2599 | |
2600 | DR 228 removed the restriction that the template be a member |
2601 | template. |
2602 | |
2603 | DR 96, if accepted would add the further restriction that explicit |
2604 | template arguments must be provided if the template keyword is |
2605 | used, but, as of 2005-10-16, that DR is still in "drafting". If |
2606 | this DR is accepted, then the semantic checks here can be |
2607 | simplified, as the entity named must in fact be a template |
2608 | specialization, rather than, as at present, a set of overloaded |
2609 | functions containing at least one template function. */ |
2610 | if (TREE_CODE (decl) != TEMPLATE_DECL |
2611 | && TREE_CODE (decl) != TEMPLATE_ID_EXPR) |
2612 | { |
2613 | if (VAR_P (decl)) |
2614 | { |
2615 | if (DECL_USE_TEMPLATE (decl) |
2616 | && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl))) |
2617 | ; |
2618 | else |
2619 | permerror (input_location, "%qD is not a template" , decl); |
2620 | } |
2621 | else if (!is_overloaded_fn (decl)) |
2622 | permerror (input_location, "%qD is not a template" , decl); |
2623 | else |
2624 | { |
2625 | bool found = false; |
2626 | |
2627 | for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl)); |
2628 | !found && iter; ++iter) |
2629 | { |
2630 | tree fn = *iter; |
2631 | if (TREE_CODE (fn) == TEMPLATE_DECL |
2632 | || TREE_CODE (fn) == TEMPLATE_ID_EXPR |
2633 | || (TREE_CODE (fn) == FUNCTION_DECL |
2634 | && DECL_USE_TEMPLATE (fn) |
2635 | && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))) |
2636 | found = true; |
2637 | } |
2638 | if (!found) |
2639 | permerror (input_location, "%qD is not a template" , decl); |
2640 | } |
2641 | } |
2642 | } |
2643 | |
2644 | /* Record that an access failure occurred on BASETYPE_PATH attempting |
2645 | to access FIELD_DECL. */ |
2646 | |
2647 | void |
2648 | access_failure_info::record_access_failure (tree basetype_path, |
2649 | tree field_decl) |
2650 | { |
2651 | m_was_inaccessible = true; |
2652 | m_basetype_path = basetype_path; |
2653 | m_field_decl = field_decl; |
2654 | } |
2655 | |
2656 | /* If an access failure was recorded, then attempt to locate an |
2657 | accessor function for the pertinent field, and if one is |
2658 | available, add a note and fix-it hint suggesting using it. */ |
2659 | |
2660 | void |
2661 | access_failure_info::maybe_suggest_accessor (bool const_p) const |
2662 | { |
2663 | if (!m_was_inaccessible) |
2664 | return; |
2665 | |
2666 | tree accessor |
2667 | = locate_field_accessor (m_basetype_path, m_field_decl, const_p); |
2668 | if (!accessor) |
2669 | return; |
2670 | |
2671 | /* The accessor must itself be accessible for it to be a reasonable |
2672 | suggestion. */ |
2673 | if (!accessible_p (m_basetype_path, accessor, true)) |
2674 | return; |
2675 | |
2676 | rich_location richloc (line_table, input_location); |
2677 | pretty_printer pp; |
2678 | pp_printf (&pp, "%s()" , IDENTIFIER_POINTER (DECL_NAME (accessor))); |
2679 | richloc.add_fixit_replace (pp_formatted_text (&pp)); |
2680 | inform (&richloc, "field %q#D can be accessed via %q#D" , |
2681 | m_field_decl, accessor); |
2682 | } |
2683 | |
2684 | /* This function is called by the parser to process a class member |
2685 | access expression of the form OBJECT.NAME. NAME is a node used by |
2686 | the parser to represent a name; it is not yet a DECL. It may, |
2687 | however, be a BASELINK where the BASELINK_FUNCTIONS is a |
2688 | TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and |
2689 | there is no reason to do the lookup twice, so the parser keeps the |
2690 | BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to |
2691 | be a template via the use of the "A::template B" syntax. */ |
2692 | |
2693 | tree |
2694 | finish_class_member_access_expr (cp_expr object, tree name, bool template_p, |
2695 | tsubst_flags_t complain) |
2696 | { |
2697 | tree expr; |
2698 | tree object_type; |
2699 | tree member; |
2700 | tree access_path = NULL_TREE; |
2701 | tree orig_object = object; |
2702 | tree orig_name = name; |
2703 | |
2704 | if (object == error_mark_node || name == error_mark_node) |
2705 | return error_mark_node; |
2706 | |
2707 | /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */ |
2708 | if (!objc_is_public (object, name)) |
2709 | return error_mark_node; |
2710 | |
2711 | object_type = TREE_TYPE (object); |
2712 | |
2713 | if (processing_template_decl) |
2714 | { |
2715 | if (/* If OBJECT is dependent, so is OBJECT.NAME. */ |
2716 | type_dependent_object_expression_p (object) |
2717 | /* If NAME is "f<args>", where either 'f' or 'args' is |
2718 | dependent, then the expression is dependent. */ |
2719 | || (TREE_CODE (name) == TEMPLATE_ID_EXPR |
2720 | && dependent_template_id_p (TREE_OPERAND (name, 0), |
2721 | TREE_OPERAND (name, 1))) |
2722 | /* If NAME is "T::X" where "T" is dependent, then the |
2723 | expression is dependent. */ |
2724 | || (TREE_CODE (name) == SCOPE_REF |
2725 | && TYPE_P (TREE_OPERAND (name, 0)) |
2726 | && dependent_scope_p (TREE_OPERAND (name, 0)))) |
2727 | { |
2728 | dependent: |
2729 | return build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, |
2730 | orig_object, orig_name, NULL_TREE); |
2731 | } |
2732 | object = build_non_dependent_expr (object); |
2733 | } |
2734 | else if (c_dialect_objc () |
2735 | && identifier_p (name) |
2736 | && (expr = objc_maybe_build_component_ref (object, name))) |
2737 | return expr; |
2738 | |
2739 | /* [expr.ref] |
2740 | |
2741 | The type of the first expression shall be "class object" (of a |
2742 | complete type). */ |
2743 | if (!currently_open_class (object_type) |
2744 | && !complete_type_or_maybe_complain (object_type, object, complain)) |
2745 | return error_mark_node; |
2746 | if (!CLASS_TYPE_P (object_type)) |
2747 | { |
2748 | if (complain & tf_error) |
2749 | { |
2750 | if (POINTER_TYPE_P (object_type) |
2751 | && CLASS_TYPE_P (TREE_TYPE (object_type))) |
2752 | error ("request for member %qD in %qE, which is of pointer " |
2753 | "type %qT (maybe you meant to use %<->%> ?)" , |
2754 | name, object.get_value (), object_type); |
2755 | else |
2756 | error ("request for member %qD in %qE, which is of non-class " |
2757 | "type %qT" , name, object.get_value (), object_type); |
2758 | } |
2759 | return error_mark_node; |
2760 | } |
2761 | |
2762 | if (BASELINK_P (name)) |
2763 | /* A member function that has already been looked up. */ |
2764 | member = name; |
2765 | else |
2766 | { |
2767 | bool is_template_id = false; |
2768 | tree template_args = NULL_TREE; |
2769 | tree scope = NULL_TREE; |
2770 | |
2771 | access_path = object_type; |
2772 | |
2773 | if (TREE_CODE (name) == SCOPE_REF) |
2774 | { |
2775 | /* A qualified name. The qualifying class or namespace `S' |
2776 | has already been looked up; it is either a TYPE or a |
2777 | NAMESPACE_DECL. */ |
2778 | scope = TREE_OPERAND (name, 0); |
2779 | name = TREE_OPERAND (name, 1); |
2780 | |
2781 | /* If SCOPE is a namespace, then the qualified name does not |
2782 | name a member of OBJECT_TYPE. */ |
2783 | if (TREE_CODE (scope) == NAMESPACE_DECL) |
2784 | { |
2785 | if (complain & tf_error) |
2786 | error ("%<%D::%D%> is not a member of %qT" , |
2787 | scope, name, object_type); |
2788 | return error_mark_node; |
2789 | } |
2790 | } |
2791 | |
2792 | if (TREE_CODE (name) == TEMPLATE_ID_EXPR) |
2793 | { |
2794 | is_template_id = true; |
2795 | template_args = TREE_OPERAND (name, 1); |
2796 | name = TREE_OPERAND (name, 0); |
2797 | |
2798 | if (!identifier_p (name)) |
2799 | name = OVL_NAME (name); |
2800 | } |
2801 | |
2802 | if (scope) |
2803 | { |
2804 | if (TREE_CODE (scope) == ENUMERAL_TYPE) |
2805 | { |
2806 | gcc_assert (!is_template_id); |
2807 | /* Looking up a member enumerator (c++/56793). */ |
2808 | if (!TYPE_CLASS_SCOPE_P (scope) |
2809 | || !DERIVED_FROM_P (TYPE_CONTEXT (scope), object_type)) |
2810 | { |
2811 | if (complain & tf_error) |
2812 | error ("%<%D::%D%> is not a member of %qT" , |
2813 | scope, name, object_type); |
2814 | return error_mark_node; |
2815 | } |
2816 | tree val = lookup_enumerator (scope, name); |
2817 | if (!val) |
2818 | { |
2819 | if (complain & tf_error) |
2820 | error ("%qD is not a member of %qD" , |
2821 | name, scope); |
2822 | return error_mark_node; |
2823 | } |
2824 | |
2825 | if (TREE_SIDE_EFFECTS (object)) |
2826 | val = build2 (COMPOUND_EXPR, TREE_TYPE (val), object, val); |
2827 | return val; |
2828 | } |
2829 | |
2830 | gcc_assert (CLASS_TYPE_P (scope)); |
2831 | gcc_assert (identifier_p (name) || TREE_CODE (name) == BIT_NOT_EXPR); |
2832 | |
2833 | if (constructor_name_p (name, scope)) |
2834 | { |
2835 | if (complain & tf_error) |
2836 | error ("cannot call constructor %<%T::%D%> directly" , |
2837 | scope, name); |
2838 | return error_mark_node; |
2839 | } |
2840 | |
2841 | /* Find the base of OBJECT_TYPE corresponding to SCOPE. */ |
2842 | access_path = lookup_base (object_type, scope, ba_check, |
2843 | NULL, complain); |
2844 | if (access_path == error_mark_node) |
2845 | return error_mark_node; |
2846 | if (!access_path) |
2847 | { |
2848 | if (any_dependent_bases_p (object_type)) |
2849 | goto dependent; |
2850 | if (complain & tf_error) |
2851 | error ("%qT is not a base of %qT" , scope, object_type); |
2852 | return error_mark_node; |
2853 | } |
2854 | } |
2855 | |
2856 | if (TREE_CODE (name) == BIT_NOT_EXPR) |
2857 | { |
2858 | if (dependent_type_p (object_type)) |
2859 | /* The destructor isn't declared yet. */ |
2860 | goto dependent; |
2861 | member = lookup_destructor (object, scope, name, complain); |
2862 | } |
2863 | else |
2864 | { |
2865 | /* Look up the member. */ |
2866 | access_failure_info afi; |
2867 | member = lookup_member (access_path, name, /*protect=*/1, |
2868 | /*want_type=*/false, complain, |
2869 | &afi); |
2870 | afi.maybe_suggest_accessor (TYPE_READONLY (object_type)); |
2871 | if (member == NULL_TREE) |
2872 | { |
2873 | if (dependent_type_p (object_type)) |
2874 | /* Try again at instantiation time. */ |
2875 | goto dependent; |
2876 | if (complain & tf_error) |
2877 | { |
2878 | tree guessed_id = lookup_member_fuzzy (access_path, name, |
2879 | /*want_type=*/false); |
2880 | if (guessed_id) |
2881 | { |
2882 | location_t bogus_component_loc = input_location; |
2883 | gcc_rich_location rich_loc (bogus_component_loc); |
2884 | rich_loc.add_fixit_misspelled_id (bogus_component_loc, |
2885 | guessed_id); |
2886 | error_at (&rich_loc, |
2887 | "%q#T has no member named %qE;" |
2888 | " did you mean %qE?" , |
2889 | TREE_CODE (access_path) == TREE_BINFO |
2890 | ? TREE_TYPE (access_path) : object_type, |
2891 | name, guessed_id); |
2892 | } |
2893 | else |
2894 | error ("%q#T has no member named %qE" , |
2895 | TREE_CODE (access_path) == TREE_BINFO |
2896 | ? TREE_TYPE (access_path) : object_type, name); |
2897 | } |
2898 | return error_mark_node; |
2899 | } |
2900 | if (member == error_mark_node) |
2901 | return error_mark_node; |
2902 | if (DECL_P (member) |
2903 | && any_dependent_type_attributes_p (DECL_ATTRIBUTES (member))) |
2904 | /* Dependent type attributes on the decl mean that the TREE_TYPE is |
2905 | wrong, so don't use it. */ |
2906 | goto dependent; |
2907 | if (TREE_CODE (member) == USING_DECL && DECL_DEPENDENT_P (member)) |
2908 | goto dependent; |
2909 | } |
2910 | |
2911 | if (is_template_id) |
2912 | { |
2913 | tree templ = member; |
2914 | |
2915 | if (BASELINK_P (templ)) |
2916 | member = lookup_template_function (templ, template_args); |
2917 | else if (variable_template_p (templ)) |
2918 | member = (lookup_and_finish_template_variable |
2919 | (templ, template_args, complain)); |
2920 | else |
2921 | { |
2922 | if (complain & tf_error) |
2923 | error ("%qD is not a member template function" , name); |
2924 | return error_mark_node; |
2925 | } |
2926 | } |
2927 | } |
2928 | |
2929 | if (TREE_DEPRECATED (member)) |
2930 | warn_deprecated_use (member, NULL_TREE); |
2931 | |
2932 | if (template_p) |
2933 | check_template_keyword (member); |
2934 | |
2935 | expr = build_class_member_access_expr (object, member, access_path, |
2936 | /*preserve_reference=*/false, |
2937 | complain); |
2938 | if (processing_template_decl && expr != error_mark_node) |
2939 | { |
2940 | if (BASELINK_P (member)) |
2941 | { |
2942 | if (TREE_CODE (orig_name) == SCOPE_REF) |
2943 | BASELINK_QUALIFIED_P (member) = 1; |
2944 | orig_name = member; |
2945 | } |
2946 | return build_min_non_dep (COMPONENT_REF, expr, |
2947 | orig_object, orig_name, |
2948 | NULL_TREE); |
2949 | } |
2950 | |
2951 | return expr; |
2952 | } |
2953 | |
2954 | /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate |
2955 | type. */ |
2956 | |
2957 | tree |
2958 | build_simple_component_ref (tree object, tree member) |
2959 | { |
2960 | tree type = cp_build_qualified_type (TREE_TYPE (member), |
2961 | cp_type_quals (TREE_TYPE (object))); |
2962 | return build3_loc (input_location, |
2963 | COMPONENT_REF, type, |
2964 | object, member, NULL_TREE); |
2965 | } |
2966 | |
2967 | /* Return an expression for the MEMBER_NAME field in the internal |
2968 | representation of PTRMEM, a pointer-to-member function. (Each |
2969 | pointer-to-member function type gets its own RECORD_TYPE so it is |
2970 | more convenient to access the fields by name than by FIELD_DECL.) |
2971 | This routine converts the NAME to a FIELD_DECL and then creates the |
2972 | node for the complete expression. */ |
2973 | |
2974 | tree |
2975 | build_ptrmemfunc_access_expr (tree ptrmem, tree member_name) |
2976 | { |
2977 | tree ptrmem_type; |
2978 | tree member; |
2979 | |
2980 | /* This code is a stripped down version of |
2981 | build_class_member_access_expr. It does not work to use that |
2982 | routine directly because it expects the object to be of class |
2983 | type. */ |
2984 | ptrmem_type = TREE_TYPE (ptrmem); |
2985 | gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type)); |
2986 | for (member = TYPE_FIELDS (ptrmem_type); member; |
2987 | member = DECL_CHAIN (member)) |
2988 | if (DECL_NAME (member) == member_name) |
2989 | break; |
2990 | tree res = build_simple_component_ref (ptrmem, member); |
2991 | |
2992 | TREE_NO_WARNING (res) = 1; |
2993 | return res; |
2994 | } |
2995 | |
2996 | /* Given an expression PTR for a pointer, return an expression |
2997 | for the value pointed to. |
2998 | ERRORSTRING is the name of the operator to appear in error messages. |
2999 | |
3000 | This function may need to overload OPERATOR_FNNAME. |
3001 | Must also handle REFERENCE_TYPEs for C++. */ |
3002 | |
3003 | tree |
3004 | build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring, |
3005 | tsubst_flags_t complain) |
3006 | { |
3007 | tree orig_expr = expr; |
3008 | tree rval; |
3009 | tree overload = NULL_TREE; |
3010 | |
3011 | if (processing_template_decl) |
3012 | { |
3013 | /* Retain the type if we know the operand is a pointer. */ |
3014 | if (TREE_TYPE (expr) && POINTER_TYPE_P (TREE_TYPE (expr))) |
3015 | return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr); |
3016 | if (type_dependent_expression_p (expr)) |
3017 | return build_min_nt_loc (loc, INDIRECT_REF, expr); |
3018 | expr = build_non_dependent_expr (expr); |
3019 | } |
3020 | |
3021 | rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr, |
3022 | NULL_TREE, NULL_TREE, &overload, complain); |
3023 | if (!rval) |
3024 | rval = cp_build_indirect_ref (expr, errorstring, complain); |
3025 | |
3026 | if (processing_template_decl && rval != error_mark_node) |
3027 | { |
3028 | if (overload != NULL_TREE) |
3029 | return (build_min_non_dep_op_overload |
3030 | (INDIRECT_REF, rval, overload, orig_expr)); |
3031 | |
3032 | return build_min_non_dep (INDIRECT_REF, rval, orig_expr); |
3033 | } |
3034 | else |
3035 | return rval; |
3036 | } |
3037 | |
3038 | /* The implementation of the above, and of indirection implied by other |
3039 | constructs. If DO_FOLD is true, fold away INDIRECT_REF of ADDR_EXPR. */ |
3040 | |
3041 | static tree |
3042 | cp_build_indirect_ref_1 (tree ptr, ref_operator errorstring, |
3043 | tsubst_flags_t complain, bool do_fold) |
3044 | { |
3045 | tree pointer, type; |
3046 | |
3047 | /* RO_NULL should only be used with the folding entry points below, not |
3048 | cp_build_indirect_ref. */ |
3049 | gcc_checking_assert (errorstring != RO_NULL || do_fold); |
3050 | |
3051 | if (ptr == current_class_ptr |
3052 | || (TREE_CODE (ptr) == NOP_EXPR |
3053 | && TREE_OPERAND (ptr, 0) == current_class_ptr |
3054 | && (same_type_ignoring_top_level_qualifiers_p |
3055 | (TREE_TYPE (ptr), TREE_TYPE (current_class_ptr))))) |
3056 | return current_class_ref; |
3057 | |
3058 | pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE |
3059 | ? ptr : decay_conversion (ptr, complain)); |
3060 | if (pointer == error_mark_node) |
3061 | return error_mark_node; |
3062 | |
3063 | type = TREE_TYPE (pointer); |
3064 | |
3065 | if (POINTER_TYPE_P (type)) |
3066 | { |
3067 | /* [expr.unary.op] |
3068 | |
3069 | If the type of the expression is "pointer to T," the type |
3070 | of the result is "T." */ |
3071 | tree t = TREE_TYPE (type); |
3072 | |
3073 | if ((CONVERT_EXPR_P (ptr) |
3074 | || TREE_CODE (ptr) == VIEW_CONVERT_EXPR) |
3075 | && (!CLASS_TYPE_P (t) || !CLASSTYPE_EMPTY_P (t))) |
3076 | { |
3077 | /* If a warning is issued, mark it to avoid duplicates from |
3078 | the backend. This only needs to be done at |
3079 | warn_strict_aliasing > 2. */ |
3080 | if (warn_strict_aliasing > 2) |
3081 | if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (ptr, 0)), |
3082 | type, TREE_OPERAND (ptr, 0))) |
3083 | TREE_NO_WARNING (ptr) = 1; |
3084 | } |
3085 | |
3086 | if (VOID_TYPE_P (t)) |
3087 | { |
3088 | /* A pointer to incomplete type (other than cv void) can be |
3089 | dereferenced [expr.unary.op]/1 */ |
3090 | if (complain & tf_error) |
3091 | error ("%qT is not a pointer-to-object type" , type); |
3092 | return error_mark_node; |
3093 | } |
3094 | else if (do_fold && TREE_CODE (pointer) == ADDR_EXPR |
3095 | && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0)))) |
3096 | /* The POINTER was something like `&x'. We simplify `*&x' to |
3097 | `x'. */ |
3098 | return TREE_OPERAND (pointer, 0); |
3099 | else |
3100 | { |
3101 | tree ref = build1 (INDIRECT_REF, t, pointer); |
3102 | |
3103 | /* We *must* set TREE_READONLY when dereferencing a pointer to const, |
3104 | so that we get the proper error message if the result is used |
3105 | to assign to. Also, &* is supposed to be a no-op. */ |
3106 | TREE_READONLY (ref) = CP_TYPE_CONST_P (t); |
3107 | TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t); |
3108 | TREE_SIDE_EFFECTS (ref) |
3109 | = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer)); |
3110 | return ref; |
3111 | } |
3112 | } |
3113 | else if (!(complain & tf_error)) |
3114 | /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */ |
3115 | ; |
3116 | /* `pointer' won't be an error_mark_node if we were given a |
3117 | pointer to member, so it's cool to check for this here. */ |
3118 | else if (TYPE_PTRMEM_P (type)) |
3119 | switch (errorstring) |
3120 | { |
3121 | case RO_ARRAY_INDEXING: |
3122 | error ("invalid use of array indexing on pointer to member" ); |
3123 | break; |
3124 | case RO_UNARY_STAR: |
3125 | error ("invalid use of unary %<*%> on pointer to member" ); |
3126 | break; |
3127 | case RO_IMPLICIT_CONVERSION: |
3128 | error ("invalid use of implicit conversion on pointer to member" ); |
3129 | break; |
3130 | case RO_ARROW_STAR: |
3131 | error ("left hand operand of %<->*%> must be a pointer to class, " |
3132 | "but is a pointer to member of type %qT" , type); |
3133 | break; |
3134 | default: |
3135 | gcc_unreachable (); |
3136 | } |
3137 | else if (pointer != error_mark_node) |
3138 | invalid_indirection_error (input_location, type, errorstring); |
3139 | |
3140 | return error_mark_node; |
3141 | } |
3142 | |
3143 | /* Entry point used by c-common, which expects folding. */ |
3144 | |
3145 | tree |
3146 | build_indirect_ref (location_t /*loc*/, |
3147 | tree ptr, ref_operator errorstring) |
3148 | { |
3149 | return cp_build_indirect_ref_1 (ptr, errorstring, tf_warning_or_error, true); |
3150 | } |
3151 | |
3152 | /* Entry point used by internal indirection needs that don't correspond to any |
3153 | syntactic construct. */ |
3154 | |
3155 | tree |
3156 | cp_build_fold_indirect_ref (tree pointer) |
3157 | { |
3158 | return cp_build_indirect_ref_1 (pointer, RO_NULL, tf_warning_or_error, true); |
3159 | } |
3160 | |
3161 | /* Entry point used by indirection needs that correspond to some syntactic |
3162 | construct. */ |
3163 | |
3164 | tree |
3165 | cp_build_indirect_ref (tree ptr, ref_operator errorstring, |
3166 | tsubst_flags_t complain) |
3167 | { |
3168 | return cp_build_indirect_ref_1 (ptr, errorstring, complain, false); |
3169 | } |
3170 | |
3171 | /* This handles expressions of the form "a[i]", which denotes |
3172 | an array reference. |
3173 | |
3174 | This is logically equivalent in C to *(a+i), but we may do it differently. |
3175 | If A is a variable or a member, we generate a primitive ARRAY_REF. |
3176 | This avoids forcing the array out of registers, and can work on |
3177 | arrays that are not lvalues (for example, members of structures returned |
3178 | by functions). |
3179 | |
3180 | If INDEX is of some user-defined type, it must be converted to |
3181 | integer type. Otherwise, to make a compatible PLUS_EXPR, it |
3182 | will inherit the type of the array, which will be some pointer type. |
3183 | |
3184 | LOC is the location to use in building the array reference. */ |
3185 | |
3186 | tree |
3187 | cp_build_array_ref (location_t loc, tree array, tree idx, |
3188 | tsubst_flags_t complain) |
3189 | { |
3190 | tree ret; |
3191 | |
3192 | if (idx == 0) |
3193 | { |
3194 | if (complain & tf_error) |
3195 | error_at (loc, "subscript missing in array reference" ); |
3196 | return error_mark_node; |
3197 | } |
3198 | |
3199 | if (TREE_TYPE (array) == error_mark_node |
3200 | || TREE_TYPE (idx) == error_mark_node) |
3201 | return error_mark_node; |
3202 | |
3203 | /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference |
3204 | inside it. */ |
3205 | switch (TREE_CODE (array)) |
3206 | { |
3207 | case COMPOUND_EXPR: |
3208 | { |
3209 | tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx, |
3210 | complain); |
3211 | ret = build2 (COMPOUND_EXPR, TREE_TYPE (value), |
3212 | TREE_OPERAND (array, 0), value); |
3213 | SET_EXPR_LOCATION (ret, loc); |
3214 | return ret; |
3215 | } |
3216 | |
3217 | case COND_EXPR: |
3218 | ret = build_conditional_expr |
3219 | (loc, TREE_OPERAND (array, 0), |
3220 | cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx, |
3221 | complain), |
3222 | cp_build_array_ref (loc, TREE_OPERAND (array, 2), idx, |
3223 | complain), |
3224 | complain); |
3225 | protected_set_expr_location (ret, loc); |
3226 | return ret; |
3227 | |
3228 | default: |
3229 | break; |
3230 | } |
3231 | |
3232 | bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, idx); |
3233 | |
3234 | if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE) |
3235 | { |
3236 | tree rval, type; |
3237 | |
3238 | warn_array_subscript_with_type_char (loc, idx); |
3239 | |
3240 | if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx))) |
3241 | { |
3242 | if (complain & tf_error) |
3243 | error_at (loc, "array subscript is not an integer" ); |
3244 | return error_mark_node; |
3245 | } |
3246 | |
3247 | /* Apply integral promotions *after* noticing character types. |
3248 | (It is unclear why we do these promotions -- the standard |
3249 | does not say that we should. In fact, the natural thing would |
3250 | seem to be to convert IDX to ptrdiff_t; we're performing |
3251 | pointer arithmetic.) */ |
3252 | idx = cp_perform_integral_promotions (idx, complain); |
3253 | |
3254 | /* An array that is indexed by a non-constant |
3255 | cannot be stored in a register; we must be able to do |
3256 | address arithmetic on its address. |
3257 | Likewise an array of elements of variable size. */ |
3258 | if (TREE_CODE (idx) != INTEGER_CST |
3259 | || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array))) |
3260 | && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) |
3261 | != INTEGER_CST))) |
3262 | { |
3263 | if (!cxx_mark_addressable (array, true)) |
3264 | return error_mark_node; |
3265 | } |
3266 | |
3267 | /* An array that is indexed by a constant value which is not within |
3268 | the array bounds cannot be stored in a register either; because we |
3269 | would get a crash in store_bit_field/extract_bit_field when trying |
3270 | to access a non-existent part of the register. */ |
3271 | if (TREE_CODE (idx) == INTEGER_CST |
3272 | && TYPE_DOMAIN (TREE_TYPE (array)) |
3273 | && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array)))) |
3274 | { |
3275 | if (!cxx_mark_addressable (array)) |
3276 | return error_mark_node; |
3277 | } |
3278 | |
3279 | /* Note in C++ it is valid to subscript a `register' array, since |
3280 | it is valid to take the address of something with that |
3281 | storage specification. */ |
3282 | if (extra_warnings) |
3283 | { |
3284 | tree foo = array; |
3285 | while (TREE_CODE (foo) == COMPONENT_REF) |
3286 | foo = TREE_OPERAND (foo, 0); |
3287 | if (VAR_P (foo) && DECL_REGISTER (foo) |
3288 | && (complain & tf_warning)) |
3289 | warning_at (loc, OPT_Wextra, |
3290 | "subscripting array declared %<register%>" ); |
3291 | } |
3292 | |
3293 | type = TREE_TYPE (TREE_TYPE (array)); |
3294 | rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE); |
3295 | /* Array ref is const/volatile if the array elements are |
3296 | or if the array is.. */ |
3297 | TREE_READONLY (rval) |
3298 | |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array)); |
3299 | TREE_SIDE_EFFECTS (rval) |
3300 | |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array)); |
3301 | TREE_THIS_VOLATILE (rval) |
3302 | |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array)); |
3303 | ret = require_complete_type_sfinae (rval, complain); |
3304 | protected_set_expr_location (ret, loc); |
3305 | if (non_lvalue) |
3306 | ret = non_lvalue_loc (loc, ret); |
3307 | return ret; |
3308 | } |
3309 | |
3310 | { |
3311 | tree ar = cp_default_conversion (array, complain); |
3312 | tree ind = cp_default_conversion (idx, complain); |
3313 | |
3314 | /* Put the integer in IND to simplify error checking. */ |
3315 | if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE) |
3316 | std::swap (ar, ind); |
3317 | |
3318 | if (ar == error_mark_node || ind == error_mark_node) |
3319 | return error_mark_node; |
3320 | |
3321 | if (!TYPE_PTR_P (TREE_TYPE (ar))) |
3322 | { |
3323 | if (complain & tf_error) |
3324 | error_at (loc, "subscripted value is neither array nor pointer" ); |
3325 | return error_mark_node; |
3326 | } |
3327 | if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE) |
3328 | { |
3329 | if (complain & tf_error) |
3330 | error_at (loc, "array subscript is not an integer" ); |
3331 | return error_mark_node; |
3332 | } |
3333 | |
3334 | warn_array_subscript_with_type_char (loc, idx); |
3335 | |
3336 | ret = cp_build_indirect_ref (cp_build_binary_op (input_location, |
3337 | PLUS_EXPR, ar, ind, |
3338 | complain), |
3339 | RO_ARRAY_INDEXING, |
3340 | complain); |
3341 | protected_set_expr_location (ret, loc); |
3342 | if (non_lvalue) |
3343 | ret = non_lvalue_loc (loc, ret); |
3344 | return ret; |
3345 | } |
3346 | } |
3347 | |
3348 | /* Entry point for Obj-C++. */ |
3349 | |
3350 | tree |
3351 | build_array_ref (location_t loc, tree array, tree idx) |
3352 | { |
3353 | return cp_build_array_ref (loc, array, idx, tf_warning_or_error); |
3354 | } |
3355 | |
3356 | /* Resolve a pointer to member function. INSTANCE is the object |
3357 | instance to use, if the member points to a virtual member. |
3358 | |
3359 | This used to avoid checking for virtual functions if basetype |
3360 | has no virtual functions, according to an earlier ANSI draft. |
3361 | With the final ISO C++ rules, such an optimization is |
3362 | incorrect: A pointer to a derived member can be static_cast |
3363 | to pointer-to-base-member, as long as the dynamic object |
3364 | later has the right member. So now we only do this optimization |
3365 | when we know the dynamic type of the object. */ |
3366 | |
3367 | tree |
3368 | get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function, |
3369 | tsubst_flags_t complain) |
3370 | { |
3371 | if (TREE_CODE (function) == OFFSET_REF) |
3372 | function = TREE_OPERAND (function, 1); |
3373 | |
3374 | if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function))) |
3375 | { |
3376 | tree idx, delta, e1, e2, e3, vtbl; |
3377 | bool nonvirtual; |
3378 | tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function)); |
3379 | tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype)); |
3380 | |
3381 | tree instance_ptr = *instance_ptrptr; |
3382 | tree instance_save_expr = 0; |
3383 | if (instance_ptr == error_mark_node) |
3384 | { |
3385 | if (TREE_CODE (function) == PTRMEM_CST) |
3386 | { |
3387 | /* Extracting the function address from a pmf is only |
3388 | allowed with -Wno-pmf-conversions. It only works for |
3389 | pmf constants. */ |
3390 | e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain); |
3391 | e1 = convert (fntype, e1); |
3392 | return e1; |
3393 | } |
3394 | else |
3395 | { |
3396 | if (complain & tf_error) |
3397 | error ("object missing in use of %qE" , function); |
3398 | return error_mark_node; |
3399 | } |
3400 | } |
3401 | |
3402 | /* True if we know that the dynamic type of the object doesn't have |
3403 | virtual functions, so we can assume the PFN field is a pointer. */ |
3404 | nonvirtual = (COMPLETE_TYPE_P (basetype) |
3405 | && !TYPE_POLYMORPHIC_P (basetype) |
3406 | && resolves_to_fixed_type_p (instance_ptr, 0)); |
3407 | |
3408 | /* If we don't really have an object (i.e. in an ill-formed |
3409 | conversion from PMF to pointer), we can't resolve virtual |
3410 | functions anyway. */ |
3411 | if (!nonvirtual && is_dummy_object (instance_ptr)) |
3412 | nonvirtual = true; |
3413 | |
3414 | if (TREE_SIDE_EFFECTS (instance_ptr)) |
3415 | instance_ptr = instance_save_expr = save_expr (instance_ptr); |
3416 | |
3417 | if (TREE_SIDE_EFFECTS (function)) |
3418 | function = save_expr (function); |
3419 | |
3420 | /* Start by extracting all the information from the PMF itself. */ |
3421 | e3 = pfn_from_ptrmemfunc (function); |
3422 | delta = delta_from_ptrmemfunc (function); |
3423 | idx = build1 (NOP_EXPR, vtable_index_type, e3); |
3424 | switch (TARGET_PTRMEMFUNC_VBIT_LOCATION) |
3425 | { |
3426 | int flag_sanitize_save; |
3427 | case ptrmemfunc_vbit_in_pfn: |
3428 | e1 = cp_build_binary_op (input_location, |
3429 | BIT_AND_EXPR, idx, integer_one_node, |
3430 | complain); |
3431 | idx = cp_build_binary_op (input_location, |
3432 | MINUS_EXPR, idx, integer_one_node, |
3433 | complain); |
3434 | if (idx == error_mark_node) |
3435 | return error_mark_node; |
3436 | break; |
3437 | |
3438 | case ptrmemfunc_vbit_in_delta: |
3439 | e1 = cp_build_binary_op (input_location, |
3440 | BIT_AND_EXPR, delta, integer_one_node, |
3441 | complain); |
3442 | /* Don't instrument the RSHIFT_EXPR we're about to create because |
3443 | we're going to use DELTA number of times, and that wouldn't play |
3444 | well with SAVE_EXPRs therein. */ |
3445 | flag_sanitize_save = flag_sanitize; |
3446 | flag_sanitize = 0; |
3447 | delta = cp_build_binary_op (input_location, |
3448 | RSHIFT_EXPR, delta, integer_one_node, |
3449 | complain); |
3450 | flag_sanitize = flag_sanitize_save; |
3451 | if (delta == error_mark_node) |
3452 | return error_mark_node; |
3453 | break; |
3454 | |
3455 | default: |
3456 | gcc_unreachable (); |
3457 | } |
3458 | |
3459 | if (e1 == error_mark_node) |
3460 | return error_mark_node; |
3461 | |
3462 | /* Convert down to the right base before using the instance. A |
3463 | special case is that in a pointer to member of class C, C may |
3464 | be incomplete. In that case, the function will of course be |
3465 | a member of C, and no conversion is required. In fact, |
3466 | lookup_base will fail in that case, because incomplete |
3467 | classes do not have BINFOs. */ |
3468 | if (!same_type_ignoring_top_level_qualifiers_p |
3469 | (basetype, TREE_TYPE (TREE_TYPE (instance_ptr)))) |
3470 | { |
3471 | basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)), |
3472 | basetype, ba_check, NULL, complain); |
3473 | instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype, |
3474 | 1, complain); |
3475 | if (instance_ptr == error_mark_node) |
3476 | return error_mark_node; |
3477 | } |
3478 | /* ...and then the delta in the PMF. */ |
3479 | instance_ptr = fold_build_pointer_plus (instance_ptr, delta); |
3480 | |
3481 | /* Hand back the adjusted 'this' argument to our caller. */ |
3482 | *instance_ptrptr = instance_ptr; |
3483 | |
3484 | if (nonvirtual) |
3485 | /* Now just return the pointer. */ |
3486 | return e3; |
3487 | |
3488 | /* Next extract the vtable pointer from the object. */ |
3489 | vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node), |
3490 | instance_ptr); |
3491 | vtbl = cp_build_fold_indirect_ref (vtbl); |
3492 | if (vtbl == error_mark_node) |
3493 | return error_mark_node; |
3494 | |
3495 | /* Finally, extract the function pointer from the vtable. */ |
3496 | e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx); |
3497 | e2 = cp_build_fold_indirect_ref (e2); |
3498 | if (e2 == error_mark_node) |
3499 | return error_mark_node; |
3500 | TREE_CONSTANT (e2) = 1; |
3501 | |
3502 | /* When using function descriptors, the address of the |
3503 | vtable entry is treated as a function pointer. */ |
3504 | if (TARGET_VTABLE_USES_DESCRIPTORS) |
3505 | e2 = build1 (NOP_EXPR, TREE_TYPE (e2), |
3506 | cp_build_addr_expr (e2, complain)); |
3507 | |
3508 | e2 = fold_convert (TREE_TYPE (e3), e2); |
3509 | e1 = build_conditional_expr (input_location, e1, e2, e3, complain); |
3510 | if (e1 == error_mark_node) |
3511 | return error_mark_node; |
3512 | |
3513 | /* Make sure this doesn't get evaluated first inside one of the |
3514 | branches of the COND_EXPR. */ |
3515 | if (instance_save_expr) |
3516 | e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1), |
3517 | instance_save_expr, e1); |
3518 | |
3519 | function = e1; |
3520 | } |
3521 | return function; |
3522 | } |
3523 | |
3524 | /* Used by the C-common bits. */ |
3525 | tree |
3526 | build_function_call (location_t /*loc*/, |
3527 | tree function, tree params) |
3528 | { |
3529 | return cp_build_function_call (function, params, tf_warning_or_error); |
3530 | } |
3531 | |
3532 | /* Used by the C-common bits. */ |
3533 | tree |
3534 | build_function_call_vec (location_t /*loc*/, vec<location_t> /*arg_loc*/, |
3535 | tree function, vec<tree, va_gc> *params, |
3536 | vec<tree, va_gc> * /*origtypes*/) |
3537 | { |
3538 | vec<tree, va_gc> *orig_params = params; |
3539 | tree ret = cp_build_function_call_vec (function, ¶ms, |
3540 | tf_warning_or_error); |
3541 | |
3542 | /* cp_build_function_call_vec can reallocate PARAMS by adding |
3543 | default arguments. That should never happen here. Verify |
3544 | that. */ |
3545 | gcc_assert (params == orig_params); |
3546 | |
3547 | return ret; |
3548 | } |
3549 | |
3550 | /* Build a function call using a tree list of arguments. */ |
3551 | |
3552 | static tree |
3553 | cp_build_function_call (tree function, tree params, tsubst_flags_t complain) |
3554 | { |
3555 | vec<tree, va_gc> *vec; |
3556 | tree ret; |
3557 | |
3558 | vec = make_tree_vector (); |
3559 | for (; params != NULL_TREE; params = TREE_CHAIN (params)) |
3560 | vec_safe_push (vec, TREE_VALUE (params)); |
3561 | ret = cp_build_function_call_vec (function, &vec, complain); |
3562 | release_tree_vector (vec); |
3563 | return ret; |
3564 | } |
3565 | |
3566 | /* Build a function call using varargs. */ |
3567 | |
3568 | tree |
3569 | cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...) |
3570 | { |
3571 | vec<tree, va_gc> *vec; |
3572 | va_list args; |
3573 | tree ret, t; |
3574 | |
3575 | vec = make_tree_vector (); |
3576 | va_start (args, complain); |
3577 | for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree)) |
3578 | vec_safe_push (vec, t); |
3579 | va_end (args); |
3580 | ret = cp_build_function_call_vec (function, &vec, complain); |
3581 | release_tree_vector (vec); |
3582 | return ret; |
3583 | } |
3584 | |
3585 | /* Build a function call using a vector of arguments. PARAMS may be |
3586 | NULL if there are no parameters. This changes the contents of |
3587 | PARAMS. */ |
3588 | |
3589 | tree |
3590 | cp_build_function_call_vec (tree function, vec<tree, va_gc> **params, |
3591 | tsubst_flags_t complain) |
3592 | { |
3593 | tree fntype, fndecl; |
3594 | int is_method; |
3595 | tree original = function; |
3596 | int nargs; |
3597 | tree *argarray; |
3598 | tree parm_types; |
3599 | vec<tree, va_gc> *allocated = NULL; |
3600 | tree ret; |
3601 | |
3602 | /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF |
3603 | expressions, like those used for ObjC messenger dispatches. */ |
3604 | if (params != NULL && !vec_safe_is_empty (*params)) |
3605 | function = objc_rewrite_function_call (function, (**params)[0]); |
3606 | |
3607 | /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue. |
3608 | Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */ |
3609 | if (TREE_CODE (function) == NOP_EXPR |
3610 | && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0))) |
3611 | function = TREE_OPERAND (function, 0); |
3612 | |
3613 | if (TREE_CODE (function) == FUNCTION_DECL) |
3614 | { |
3615 | /* If the function is a non-template member function |
3616 | or a non-template friend, then we need to check the |
3617 | constraints. |
3618 | |
3619 | Note that if overload resolution failed with a single |
3620 | candidate this function will be used to explicitly diagnose |
3621 | the failure for the single call expression. The check is |
3622 | technically redundant since we also would have failed in |
3623 | add_function_candidate. */ |
3624 | if (flag_concepts |
3625 | && (complain & tf_error) |
3626 | && !constraints_satisfied_p (function)) |
3627 | { |
3628 | error ("cannot call function %qD" , function); |
3629 | location_t loc = DECL_SOURCE_LOCATION (function); |
3630 | diagnose_constraints (loc, function, NULL_TREE); |
3631 | return error_mark_node; |
3632 | } |
3633 | |
3634 | if (!mark_used (function, complain) && !(complain & tf_error)) |
3635 | return error_mark_node; |
3636 | fndecl = function; |
3637 | |
3638 | /* Convert anything with function type to a pointer-to-function. */ |
3639 | if (DECL_MAIN_P (function)) |
3640 | { |
3641 | if (complain & tf_error) |
3642 | pedwarn (input_location, OPT_Wpedantic, |
3643 | "ISO C++ forbids calling %<::main%> from within program" ); |
3644 | else |
3645 | return error_mark_node; |
3646 | } |
3647 | function = build_addr_func (function, complain); |
3648 | } |
3649 | else |
3650 | { |
3651 | fndecl = NULL_TREE; |
3652 | |
3653 | function = build_addr_func (function, complain); |
3654 | } |
3655 | |
3656 | if (function == error_mark_node) |
3657 | return error_mark_node; |
3658 | |
3659 | fntype = TREE_TYPE (function); |
3660 | |
3661 | if (TYPE_PTRMEMFUNC_P (fntype)) |
3662 | { |
3663 | if (complain & tf_error) |
3664 | error ("must use %<.*%> or %<->*%> to call pointer-to-member " |
3665 | "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>" , |
3666 | original, original); |
3667 | return error_mark_node; |
3668 | } |
3669 | |
3670 | is_method = (TYPE_PTR_P (fntype) |
3671 | && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE); |
3672 | |
3673 | if (!(TYPE_PTRFN_P (fntype) |
3674 | || is_method |
3675 | || TREE_CODE (function) == TEMPLATE_ID_EXPR)) |
3676 | { |
3677 | if (complain & tf_error) |
3678 | { |
3679 | if (!flag_diagnostics_show_caret) |
3680 | error_at (input_location, |
3681 | "%qE cannot be used as a function" , original); |
3682 | else if (DECL_P (original)) |
3683 | error_at (input_location, |
3684 | "%qD cannot be used as a function" , original); |
3685 | else |
3686 | error_at (input_location, |
3687 | "expression cannot be used as a function" ); |
3688 | } |
3689 | |
3690 | return error_mark_node; |
3691 | } |
3692 | |
3693 | /* fntype now gets the type of function pointed to. */ |
3694 | fntype = TREE_TYPE (fntype); |
3695 | parm_types = TYPE_ARG_TYPES (fntype); |
3696 | |
3697 | if (params == NULL) |
3698 | { |
3699 | allocated = make_tree_vector (); |
3700 | params = &allocated; |
3701 | } |
3702 | |
3703 | nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL, |
3704 | complain); |
3705 | if (nargs < 0) |
3706 | return error_mark_node; |
3707 | |
3708 | argarray = (*params)->address (); |
3709 | |
3710 | /* Check for errors in format strings and inappropriately |
3711 | null parameters. */ |
3712 | bool warned_p = check_function_arguments (input_location, fndecl, fntype, |
3713 | nargs, argarray, NULL); |
3714 | |
3715 | ret = build_cxx_call (function, nargs, argarray, complain); |
3716 | |
3717 | if (warned_p) |
3718 | { |
3719 | tree c = extract_call_expr (ret); |
3720 | if (TREE_CODE (c) == CALL_EXPR) |
3721 | TREE_NO_WARNING (c) = 1; |
3722 | } |
3723 | |
3724 | if (allocated != NULL) |
3725 | release_tree_vector (allocated); |
3726 | |
3727 | return ret; |
3728 | } |
3729 | |
3730 | /* Subroutine of convert_arguments. |
3731 | Print an error message about a wrong number of arguments. */ |
3732 | |
3733 | static void |
3734 | error_args_num (location_t loc, tree fndecl, bool too_many_p) |
3735 | { |
3736 | if (fndecl) |
3737 | { |
3738 | if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE) |
3739 | { |
3740 | if (DECL_NAME (fndecl) == NULL_TREE |
3741 | || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl))) |
3742 | error_at (loc, |
3743 | too_many_p |
3744 | ? G_("too many arguments to constructor %q#D" ) |
3745 | : G_("too few arguments to constructor %q#D" ), |
3746 | fndecl); |
3747 | else |
3748 | error_at (loc, |
3749 | too_many_p |
3750 | ? G_("too many arguments to member function %q#D" ) |
3751 | : G_("too few arguments to member function %q#D" ), |
3752 | fndecl); |
3753 | } |
3754 | else |
3755 | error_at (loc, |
3756 | too_many_p |
3757 | ? G_("too many arguments to function %q#D" ) |
3758 | : G_("too few arguments to function %q#D" ), |
3759 | fndecl); |
3760 | if (!DECL_IS_BUILTIN (fndecl)) |
3761 | inform (DECL_SOURCE_LOCATION (fndecl), "declared here" ); |
3762 | } |
3763 | else |
3764 | { |
3765 | if (c_dialect_objc () && objc_message_selector ()) |
3766 | error_at (loc, |
3767 | too_many_p |
3768 | ? G_("too many arguments to method %q#D" ) |
3769 | : G_("too few arguments to method %q#D" ), |
3770 | objc_message_selector ()); |
3771 | else |
3772 | error_at (loc, too_many_p ? G_("too many arguments to function" ) |
3773 | : G_("too few arguments to function" )); |
3774 | } |
3775 | } |
3776 | |
3777 | /* Convert the actual parameter expressions in the list VALUES to the |
3778 | types in the list TYPELIST. The converted expressions are stored |
3779 | back in the VALUES vector. |
3780 | If parmdecls is exhausted, or when an element has NULL as its type, |
3781 | perform the default conversions. |
3782 | |
3783 | NAME is an IDENTIFIER_NODE or 0. It is used only for error messages. |
3784 | |
3785 | This is also where warnings about wrong number of args are generated. |
3786 | |
3787 | Returns the actual number of arguments processed (which might be less |
3788 | than the length of the vector), or -1 on error. |
3789 | |
3790 | In C++, unspecified trailing parameters can be filled in with their |
3791 | default arguments, if such were specified. Do so here. */ |
3792 | |
3793 | static int |
3794 | convert_arguments (tree typelist, vec<tree, va_gc> **values, tree fndecl, |
3795 | int flags, tsubst_flags_t complain) |
3796 | { |
3797 | tree typetail; |
3798 | unsigned int i; |
3799 | |
3800 | /* Argument passing is always copy-initialization. */ |
3801 | flags |= LOOKUP_ONLYCONVERTING; |
3802 | |
3803 | for (i = 0, typetail = typelist; |
3804 | i < vec_safe_length (*values); |
3805 | i++) |
3806 | { |
3807 | tree type = typetail ? TREE_VALUE (typetail) : 0; |
3808 | tree val = (**values)[i]; |
3809 | |
3810 | if (val == error_mark_node || type == error_mark_node) |
3811 | return -1; |
3812 | |
3813 | if (type == void_type_node) |
3814 | { |
3815 | if (complain & tf_error) |
3816 | { |
3817 | error_args_num (input_location, fndecl, /*too_many_p=*/true); |
3818 | return i; |
3819 | } |
3820 | else |
3821 | return -1; |
3822 | } |
3823 | |
3824 | /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue. |
3825 | Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */ |
3826 | if (TREE_CODE (val) == NOP_EXPR |
3827 | && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)) |
3828 | && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)) |
3829 | val = TREE_OPERAND (val, 0); |
3830 | |
3831 | if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE) |
3832 | { |
3833 | if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE |
3834 | || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE |
3835 | || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE) |
3836 | val = decay_conversion (val, complain); |
3837 | } |
3838 | |
3839 | if (val == error_mark_node) |
3840 | return -1; |
3841 | |
3842 | if (type != 0) |
3843 | { |
3844 | /* Formal parm type is specified by a function prototype. */ |
3845 | tree parmval; |
3846 | |
3847 | if (!COMPLETE_TYPE_P (complete_type (type))) |
3848 | { |
3849 | if (complain & tf_error) |
3850 | { |
3851 | if (fndecl) |
3852 | error ("parameter %P of %qD has incomplete type %qT" , |
3853 | i, fndecl, type); |
3854 | else |
3855 | error ("parameter %P has incomplete type %qT" , i, type); |
3856 | } |
3857 | parmval = error_mark_node; |
3858 | } |
3859 | else |
3860 | { |
3861 | parmval = convert_for_initialization |
3862 | (NULL_TREE, type, val, flags, |
3863 | ICR_ARGPASS, fndecl, i, complain); |
3864 | parmval = convert_for_arg_passing (type, parmval, complain); |
3865 | } |
3866 | |
3867 | if (parmval == error_mark_node) |
3868 | return -1; |
3869 | |
3870 | (**values)[i] = parmval; |
3871 | } |
3872 | else |
3873 | { |
3874 | if (fndecl && magic_varargs_p (fndecl)) |
3875 | /* Don't do ellipsis conversion for __built_in_constant_p |
3876 | as this will result in spurious errors for non-trivial |
3877 | types. */ |
3878 | val = require_complete_type_sfinae (val, complain); |
3879 | else |
3880 | val = convert_arg_to_ellipsis (val, complain); |
3881 | |
3882 | (**values)[i] = val; |
3883 | } |
3884 | |
3885 | if (typetail) |
3886 | typetail = TREE_CHAIN (typetail); |
3887 | } |
3888 | |
3889 | if (typetail != 0 && typetail != void_list_node) |
3890 | { |
3891 | /* See if there are default arguments that can be used. Because |
3892 | we hold default arguments in the FUNCTION_TYPE (which is so |
3893 | wrong), we can see default parameters here from deduced |
3894 | contexts (and via typeof) for indirect function calls. |
3895 | Fortunately we know whether we have a function decl to |
3896 | provide default arguments in a language conformant |
3897 | manner. */ |
3898 | if (fndecl && TREE_PURPOSE (typetail) |
3899 | && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG) |
3900 | { |
3901 | for (; typetail != void_list_node; ++i) |
3902 | { |
3903 | /* After DR777, with explicit template args we can end up with a |
3904 | default argument followed by no default argument. */ |
3905 | if (!TREE_PURPOSE (typetail)) |
3906 | break; |
3907 | tree parmval |
3908 | = convert_default_arg (TREE_VALUE (typetail), |
3909 | TREE_PURPOSE (typetail), |
3910 | fndecl, i, complain); |
3911 | |
3912 | if (parmval == error_mark_node) |
3913 | return -1; |
3914 | |
3915 | vec_safe_push (*values, parmval); |
3916 | typetail = TREE_CHAIN (typetail); |
3917 | /* ends with `...'. */ |
3918 | if (typetail == NULL_TREE) |
3919 | break; |
3920 | } |
3921 | } |
3922 | |
3923 | if (typetail && typetail != void_list_node) |
3924 | { |
3925 | if (complain & tf_error) |
3926 | error_args_num (input_location, fndecl, /*too_many_p=*/false); |
3927 | return -1; |
3928 | } |
3929 | } |
3930 | |
3931 | return (int) i; |
3932 | } |
3933 | |
3934 | /* Build a binary-operation expression, after performing default |
3935 | conversions on the operands. CODE is the kind of expression to |
3936 | build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE |
3937 | are the tree codes which correspond to ARG1 and ARG2 when issuing |
3938 | warnings about possibly misplaced parentheses. They may differ |
3939 | from the TREE_CODE of ARG1 and ARG2 if the parser has done constant |
3940 | folding (e.g., if the parser sees "a | 1 + 1", it may call this |
3941 | routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR). |
3942 | To avoid issuing any parentheses warnings, pass ARG1_CODE and/or |
3943 | ARG2_CODE as ERROR_MARK. */ |
3944 | |
3945 | tree |
3946 | build_x_binary_op (location_t loc, enum tree_code code, tree arg1, |
3947 | enum tree_code arg1_code, tree arg2, |
3948 | enum tree_code arg2_code, tree *overload_p, |
3949 | tsubst_flags_t complain) |
3950 | { |
3951 | tree orig_arg1; |
3952 | tree orig_arg2; |
3953 | tree expr; |
3954 | tree overload = NULL_TREE; |
3955 | |
3956 | orig_arg1 = arg1; |
3957 | orig_arg2 = arg2; |
3958 | |
3959 | if (processing_template_decl) |
3960 | { |
3961 | if (type_dependent_expression_p (arg1) |
3962 | || type_dependent_expression_p (arg2)) |
3963 | return build_min_nt_loc (loc, code, arg1, arg2); |
3964 | arg1 = build_non_dependent_expr (arg1); |
3965 | arg2 = build_non_dependent_expr (arg2); |
3966 | } |
3967 | |
3968 | if (code == DOTSTAR_EXPR) |
3969 | expr = build_m_component_ref (arg1, arg2, complain); |
3970 | else |
3971 | expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE, |
3972 | &overload, complain); |
3973 | |
3974 | if (overload_p != NULL) |
3975 | *overload_p = overload; |
3976 | |
3977 | /* Check for cases such as x+y<<z which users are likely to |
3978 | misinterpret. But don't warn about obj << x + y, since that is a |
3979 | common idiom for I/O. */ |
3980 | if (warn_parentheses |
3981 | && (complain & tf_warning) |
3982 | && !processing_template_decl |
3983 | && !error_operand_p (arg1) |
3984 | && !error_operand_p (arg2) |
3985 | && (code != LSHIFT_EXPR |
3986 | || !CLASS_TYPE_P (TREE_TYPE (arg1)))) |
3987 | warn_about_parentheses (loc, code, arg1_code, orig_arg1, |
3988 | arg2_code, orig_arg2); |
3989 | |
3990 | if (processing_template_decl && expr != error_mark_node) |
3991 | { |
3992 | if (overload != NULL_TREE) |
3993 | return (build_min_non_dep_op_overload |
3994 | (code, expr, overload, orig_arg1, orig_arg2)); |
3995 | |
3996 | return build_min_non_dep (code, expr, orig_arg1, orig_arg2); |
3997 | } |
3998 | |
3999 | return expr; |
4000 | } |
4001 | |
4002 | /* Build and return an ARRAY_REF expression. */ |
4003 | |
4004 | tree |
4005 | build_x_array_ref (location_t loc, tree arg1, tree arg2, |
4006 | tsubst_flags_t complain) |
4007 | { |
4008 | tree orig_arg1 = arg1; |
4009 | tree orig_arg2 = arg2; |
4010 | tree expr; |
4011 | tree overload = NULL_TREE; |
4012 | |
4013 | if (processing_template_decl) |
4014 | { |
4015 | if (type_dependent_expression_p (arg1) |
4016 | || type_dependent_expression_p (arg2)) |
4017 | return build_min_nt_loc (loc, ARRAY_REF, arg1, arg2, |
4018 | NULL_TREE, NULL_TREE); |
4019 | arg1 = build_non_dependent_expr (arg1); |
4020 | arg2 = build_non_dependent_expr (arg2); |
4021 | } |
4022 | |
4023 | expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, arg1, arg2, |
4024 | NULL_TREE, &overload, complain); |
4025 | |
4026 | if (processing_template_decl && expr != error_mark_node) |
4027 | { |
4028 | if (overload != NULL_TREE) |
4029 | return (build_min_non_dep_op_overload |
4030 | (ARRAY_REF, expr, overload, orig_arg1, orig_arg2)); |
4031 | |
4032 | return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2, |
4033 | NULL_TREE, NULL_TREE); |
4034 | } |
4035 | return expr; |
4036 | } |
4037 | |
4038 | /* Return whether OP is an expression of enum type cast to integer |
4039 | type. In C++ even unsigned enum types are cast to signed integer |
4040 | types. We do not want to issue warnings about comparisons between |
4041 | signed and unsigned types when one of the types is an enum type. |
4042 | Those warnings are always false positives in practice. */ |
4043 | |
4044 | static bool |
4045 | enum_cast_to_int (tree op) |
4046 | { |
4047 | if (CONVERT_EXPR_P (op) |
4048 | && TREE_TYPE (op) == integer_type_node |
4049 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE |
4050 | && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0)))) |
4051 | return true; |
4052 | |
4053 | /* The cast may have been pushed into a COND_EXPR. */ |
4054 | if (TREE_CODE (op) == COND_EXPR) |
4055 | return (enum_cast_to_int (TREE_OPERAND (op, 1)) |
4056 | || enum_cast_to_int (TREE_OPERAND (op, 2))); |
4057 | |
4058 | return false; |
4059 | } |
4060 | |
4061 | /* For the c-common bits. */ |
4062 | tree |
4063 | build_binary_op (location_t location, enum tree_code code, tree op0, tree op1, |
4064 | bool /*convert_p*/) |
4065 | { |
4066 | return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error); |
4067 | } |
4068 | |
4069 | /* Build a vector comparison of ARG0 and ARG1 using CODE opcode |
4070 | into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */ |
4071 | |
4072 | static tree |
4073 | build_vec_cmp (tree_code code, tree type, |
4074 | tree arg0, tree arg1) |
4075 | { |
4076 | tree zero_vec = build_zero_cst (type); |
4077 | tree minus_one_vec = build_minus_one_cst (type); |
4078 | tree cmp_type = build_same_sized_truth_vector_type(type); |
4079 | tree cmp = build2 (code, cmp_type, arg0, arg1); |
4080 | return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec); |
4081 | } |
4082 | |
4083 | /* Possibly warn about an address never being NULL. */ |
4084 | |
4085 | static void |
4086 | warn_for_null_address (location_t location, tree op, tsubst_flags_t complain) |
4087 | { |
4088 | if (!warn_address |
4089 | || (complain & tf_warning) == 0 |
4090 | || c_inhibit_evaluation_warnings != 0 |
4091 | || TREE_NO_WARNING (op)) |
4092 | return; |
4093 | |
4094 | tree cop = fold_non_dependent_expr (op); |
4095 | |
4096 | if (TREE_CODE (cop) == ADDR_EXPR |
4097 | && decl_with_nonnull_addr_p (TREE_OPERAND (cop, 0)) |
4098 | && !TREE_NO_WARNING (cop)) |
4099 | warning_at (location, OPT_Waddress, "the address of %qD will never " |
4100 | "be NULL" , TREE_OPERAND (cop, 0)); |
4101 | |
4102 | if (CONVERT_EXPR_P (op) |
4103 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == REFERENCE_TYPE) |
4104 | { |
4105 | tree inner_op = op; |
4106 | STRIP_NOPS (inner_op); |
4107 | |
4108 | if (DECL_P (inner_op)) |
4109 | warning_at (location, OPT_Waddress, |
4110 | "the compiler can assume that the address of " |
4111 | "%qD will never be NULL" , inner_op); |
4112 | } |
4113 | } |
4114 | |
4115 | /* Build a binary-operation expression without default conversions. |
4116 | CODE is the kind of expression to build. |
4117 | LOCATION is the location_t of the operator in the source code. |
4118 | This function differs from `build' in several ways: |
4119 | the data type of the result is computed and recorded in it, |
4120 | warnings are generated if arg data types are invalid, |
4121 | special handling for addition and subtraction of pointers is known, |
4122 | and some optimization is done (operations on narrow ints |
4123 | are done in the narrower type when that gives the same result). |
4124 | Constant folding is also done before the result is returned. |
4125 | |
4126 | Note that the operands will never have enumeral types |
4127 | because either they have just had the default conversions performed |
4128 | or they have both just been converted to some other type in which |
4129 | the arithmetic is to be done. |
4130 | |
4131 | C++: must do special pointer arithmetic when implementing |
4132 | multiple inheritance, and deal with pointer to member functions. */ |
4133 | |
4134 | tree |
4135 | cp_build_binary_op (location_t location, |
4136 | enum tree_code code, tree orig_op0, tree orig_op1, |
4137 | tsubst_flags_t complain) |
4138 | { |
4139 | tree op0, op1; |
4140 | enum tree_code code0, code1; |
4141 | tree type0, type1; |
4142 | const char *invalid_op_diag; |
4143 | |
4144 | /* Expression code to give to the expression when it is built. |
4145 | Normally this is CODE, which is what the caller asked for, |
4146 | but in some special cases we change it. */ |
4147 | enum tree_code resultcode = code; |
4148 | |
4149 | /* Data type in which the computation is to be performed. |
4150 | In the simplest cases this is the common type of the arguments. */ |
4151 | tree result_type = NULL_TREE; |
4152 | |
4153 | /* Nonzero means operands have already been type-converted |
4154 | in whatever way is necessary. |
4155 | Zero means they need to be converted to RESULT_TYPE. */ |
4156 | int converted = 0; |
4157 | |
4158 | /* Nonzero means create the expression with this type, rather than |
4159 | RESULT_TYPE. */ |
4160 | tree build_type = 0; |
4161 | |
4162 | /* Nonzero means after finally constructing the expression |
4163 | convert it to this type. */ |
4164 | tree final_type = 0; |
4165 | |
4166 | tree result, result_ovl; |
4167 | |
4168 | /* Nonzero if this is an operation like MIN or MAX which can |
4169 | safely be computed in short if both args are promoted shorts. |
4170 | Also implies COMMON. |
4171 | -1 indicates a bitwise operation; this makes a difference |
4172 | in the exact conditions for when it is safe to do the operation |
4173 | in a narrower mode. */ |
4174 | int shorten = 0; |
4175 | |
4176 | /* Nonzero if this is a comparison operation; |
4177 | if both args are promoted shorts, compare the original shorts. |
4178 | Also implies COMMON. */ |
4179 | int short_compare = 0; |
4180 | |
4181 | /* Nonzero means set RESULT_TYPE to the common type of the args. */ |
4182 | int common = 0; |
4183 | |
4184 | /* True if both operands have arithmetic type. */ |
4185 | bool |
---|