xref: /dragonfly/contrib/gcc-4.7/gcc/c-typeck.c (revision 8172914b41e0b50782e103828c8770bcdac543f1)
1 /* Build expressions with type checking for C compiler.
2    Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3    1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
4    Free Software Foundation, Inc.
5 
6 This file is part of GCC.
7 
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12 
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21 
22 
23 /* This file is part of the C front end.
24    It contains routines to build C expressions given their operands,
25    including computing the types of the result, C-specific error checks,
26    and some optimization.  */
27 
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tm.h"
32 #include "tree.h"
33 #include "langhooks.h"
34 #include "c-tree.h"
35 #include "c-lang.h"
36 #include "flags.h"
37 #include "output.h"
38 #include "intl.h"
39 #include "target.h"
40 #include "tree-iterator.h"
41 #include "bitmap.h"
42 #include "gimple.h"
43 #include "c-family/c-objc.h"
44 
45 /* Possible cases of implicit bad conversions.  Used to select
46    diagnostic messages in convert_for_assignment.  */
47 enum impl_conv {
48   ic_argpass,
49   ic_assign,
50   ic_init,
51   ic_return
52 };
53 
54 /* Possibe cases of scalar_to_vector conversion.  */
55 enum stv_conv {
56   stv_error,        /* Error occured.  */
57   stv_nothing,      /* Nothing happened.  */
58   stv_firstarg,     /* First argument must be expanded.  */
59   stv_secondarg     /* Second argument must be expanded.  */
60 };
61 
62 /* The level of nesting inside "__alignof__".  */
63 int in_alignof;
64 
65 /* The level of nesting inside "sizeof".  */
66 int in_sizeof;
67 
68 /* The level of nesting inside "typeof".  */
69 int in_typeof;
70 
71 /* Nonzero if we've already printed a "missing braces around initializer"
72    message within this initializer.  */
73 static int missing_braces_mentioned;
74 
75 static int require_constant_value;
76 static int require_constant_elements;
77 
78 static bool null_pointer_constant_p (const_tree);
79 static tree qualify_type (tree, tree);
80 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
81                                                    bool *);
82 static int comp_target_types (location_t, tree, tree);
83 static int function_types_compatible_p (const_tree, const_tree, bool *,
84                                                   bool *);
85 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
86 static tree lookup_field (tree, tree);
87 static int convert_arguments (tree, VEC(tree,gc) *, VEC(tree,gc) *, tree,
88                                     tree);
89 static tree pointer_diff (location_t, tree, tree);
90 static tree convert_for_assignment (location_t, tree, tree, tree,
91                                             enum impl_conv, bool, tree, tree, int);
92 static tree valid_compound_expr_initializer (tree, tree);
93 static void push_string (const char *);
94 static void push_member_name (tree);
95 static int spelling_length (void);
96 static char *print_spelling (char *);
97 static void warning_init (int, const char *);
98 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
99 static void output_init_element (tree, tree, bool, tree, tree, int, bool,
100                                          struct obstack *);
101 static void output_pending_init_elements (int, struct obstack *);
102 static int set_designator (int, struct obstack *);
103 static void push_range_stack (tree, struct obstack *);
104 static void add_pending_init (tree, tree, tree, bool, struct obstack *);
105 static void set_nonincremental_init (struct obstack *);
106 static void set_nonincremental_init_from_string (tree, struct obstack *);
107 static tree find_init_member (tree, struct obstack *);
108 static void readonly_warning (tree, enum lvalue_use);
109 static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
110 static void record_maybe_used_decl (tree);
111 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
112 
113 /* Return true if EXP is a null pointer constant, false otherwise.  */
114 
115 static bool
null_pointer_constant_p(const_tree expr)116 null_pointer_constant_p (const_tree expr)
117 {
118   /* This should really operate on c_expr structures, but they aren't
119      yet available everywhere required.  */
120   tree type = TREE_TYPE (expr);
121   return (TREE_CODE (expr) == INTEGER_CST
122             && !TREE_OVERFLOW (expr)
123             && integer_zerop (expr)
124             && (INTEGRAL_TYPE_P (type)
125                 || (TREE_CODE (type) == POINTER_TYPE
126                       && VOID_TYPE_P (TREE_TYPE (type))
127                       && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
128 }
129 
130 /* EXPR may appear in an unevaluated part of an integer constant
131    expression, but not in an evaluated part.  Wrap it in a
132    C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
133    INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR.  */
134 
135 static tree
note_integer_operands(tree expr)136 note_integer_operands (tree expr)
137 {
138   tree ret;
139   if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
140     {
141       ret = copy_node (expr);
142       TREE_OVERFLOW (ret) = 1;
143     }
144   else
145     {
146       ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
147       C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
148     }
149   return ret;
150 }
151 
152 /* Having checked whether EXPR may appear in an unevaluated part of an
153    integer constant expression and found that it may, remove any
154    C_MAYBE_CONST_EXPR noting this fact and return the resulting
155    expression.  */
156 
157 static inline tree
remove_c_maybe_const_expr(tree expr)158 remove_c_maybe_const_expr (tree expr)
159 {
160   if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
161     return C_MAYBE_CONST_EXPR_EXPR (expr);
162   else
163     return expr;
164 }
165 
166 /* This is a cache to hold if two types are compatible or not.  */
167 
168 struct tagged_tu_seen_cache {
169   const struct tagged_tu_seen_cache * next;
170   const_tree t1;
171   const_tree t2;
172   /* The return value of tagged_types_tu_compatible_p if we had seen
173      these two types already.  */
174   int val;
175 };
176 
177 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
178 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
179 
180 /* Do `exp = require_complete_type (exp);' to make sure exp
181    does not have an incomplete type.  (That includes void types.)  */
182 
183 tree
require_complete_type(tree value)184 require_complete_type (tree value)
185 {
186   tree type = TREE_TYPE (value);
187 
188   if (value == error_mark_node || type == error_mark_node)
189     return error_mark_node;
190 
191   /* First, detect a valid value with a complete type.  */
192   if (COMPLETE_TYPE_P (type))
193     return value;
194 
195   c_incomplete_type_error (value, type);
196   return error_mark_node;
197 }
198 
199 /* Print an error message for invalid use of an incomplete type.
200    VALUE is the expression that was used (or 0 if that isn't known)
201    and TYPE is the type that was invalid.  */
202 
203 void
c_incomplete_type_error(const_tree value,const_tree type)204 c_incomplete_type_error (const_tree value, const_tree type)
205 {
206   const char *type_code_string;
207 
208   /* Avoid duplicate error message.  */
209   if (TREE_CODE (type) == ERROR_MARK)
210     return;
211 
212   if (value != 0 && (TREE_CODE (value) == VAR_DECL
213                          || TREE_CODE (value) == PARM_DECL))
214     error ("%qD has an incomplete type", value);
215   else
216     {
217     retry:
218       /* We must print an error message.  Be clever about what it says.  */
219 
220       switch (TREE_CODE (type))
221           {
222           case RECORD_TYPE:
223             type_code_string = "struct";
224             break;
225 
226           case UNION_TYPE:
227             type_code_string = "union";
228             break;
229 
230           case ENUMERAL_TYPE:
231             type_code_string = "enum";
232             break;
233 
234           case VOID_TYPE:
235             error ("invalid use of void expression");
236             return;
237 
238           case ARRAY_TYPE:
239             if (TYPE_DOMAIN (type))
240               {
241                 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
242                     {
243                       error ("invalid use of flexible array member");
244                       return;
245                     }
246                 type = TREE_TYPE (type);
247                 goto retry;
248               }
249             error ("invalid use of array with unspecified bounds");
250             return;
251 
252           default:
253             gcc_unreachable ();
254           }
255 
256       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
257           error ("invalid use of undefined type %<%s %E%>",
258                  type_code_string, TYPE_NAME (type));
259       else
260           /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL.  */
261           error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
262     }
263 }
264 
265 /* Given a type, apply default promotions wrt unnamed function
266    arguments and return the new type.  */
267 
268 tree
c_type_promotes_to(tree type)269 c_type_promotes_to (tree type)
270 {
271   if (TYPE_MAIN_VARIANT (type) == float_type_node)
272     return double_type_node;
273 
274   if (c_promoting_integer_type_p (type))
275     {
276       /* Preserve unsignedness if not really getting any wider.  */
277       if (TYPE_UNSIGNED (type)
278             && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
279           return unsigned_type_node;
280       return integer_type_node;
281     }
282 
283   return type;
284 }
285 
286 /* Return true if between two named address spaces, whether there is a superset
287    named address space that encompasses both address spaces.  If there is a
288    superset, return which address space is the superset.  */
289 
290 static bool
addr_space_superset(addr_space_t as1,addr_space_t as2,addr_space_t * common)291 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
292 {
293   if (as1 == as2)
294     {
295       *common = as1;
296       return true;
297     }
298   else if (targetm.addr_space.subset_p (as1, as2))
299     {
300       *common = as2;
301       return true;
302     }
303   else if (targetm.addr_space.subset_p (as2, as1))
304     {
305       *common = as1;
306       return true;
307     }
308   else
309     return false;
310 }
311 
312 /* Return a variant of TYPE which has all the type qualifiers of LIKE
313    as well as those of TYPE.  */
314 
315 static tree
qualify_type(tree type,tree like)316 qualify_type (tree type, tree like)
317 {
318   addr_space_t as_type = TYPE_ADDR_SPACE (type);
319   addr_space_t as_like = TYPE_ADDR_SPACE (like);
320   addr_space_t as_common;
321 
322   /* If the two named address spaces are different, determine the common
323      superset address space.  If there isn't one, raise an error.  */
324   if (!addr_space_superset (as_type, as_like, &as_common))
325     {
326       as_common = as_type;
327       error ("%qT and %qT are in disjoint named address spaces",
328                type, like);
329     }
330 
331   return c_build_qualified_type (type,
332                                          TYPE_QUALS_NO_ADDR_SPACE (type)
333                                          | TYPE_QUALS_NO_ADDR_SPACE (like)
334                                          | ENCODE_QUAL_ADDR_SPACE (as_common));
335 }
336 
337 /* Return true iff the given tree T is a variable length array.  */
338 
339 bool
c_vla_type_p(const_tree t)340 c_vla_type_p (const_tree t)
341 {
342   if (TREE_CODE (t) == ARRAY_TYPE
343       && C_TYPE_VARIABLE_SIZE (t))
344     return true;
345   return false;
346 }
347 
348 /* Return the composite type of two compatible types.
349 
350    We assume that comptypes has already been done and returned
351    nonzero; if that isn't so, this may crash.  In particular, we
352    assume that qualifiers match.  */
353 
354 tree
composite_type(tree t1,tree t2)355 composite_type (tree t1, tree t2)
356 {
357   enum tree_code code1;
358   enum tree_code code2;
359   tree attributes;
360 
361   /* Save time if the two types are the same.  */
362 
363   if (t1 == t2) return t1;
364 
365   /* If one type is nonsense, use the other.  */
366   if (t1 == error_mark_node)
367     return t2;
368   if (t2 == error_mark_node)
369     return t1;
370 
371   code1 = TREE_CODE (t1);
372   code2 = TREE_CODE (t2);
373 
374   /* Merge the attributes.  */
375   attributes = targetm.merge_type_attributes (t1, t2);
376 
377   /* If one is an enumerated type and the other is the compatible
378      integer type, the composite type might be either of the two
379      (DR#013 question 3).  For consistency, use the enumerated type as
380      the composite type.  */
381 
382   if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
383     return t1;
384   if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
385     return t2;
386 
387   gcc_assert (code1 == code2);
388 
389   switch (code1)
390     {
391     case POINTER_TYPE:
392       /* For two pointers, do this recursively on the target type.  */
393       {
394           tree pointed_to_1 = TREE_TYPE (t1);
395           tree pointed_to_2 = TREE_TYPE (t2);
396           tree target = composite_type (pointed_to_1, pointed_to_2);
397         t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
398           t1 = build_type_attribute_variant (t1, attributes);
399           return qualify_type (t1, t2);
400       }
401 
402     case ARRAY_TYPE:
403       {
404           tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
405           int quals;
406           tree unqual_elt;
407           tree d1 = TYPE_DOMAIN (t1);
408           tree d2 = TYPE_DOMAIN (t2);
409           bool d1_variable, d2_variable;
410           bool d1_zero, d2_zero;
411           bool t1_complete, t2_complete;
412 
413           /* We should not have any type quals on arrays at all.  */
414           gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
415                         && !TYPE_QUALS_NO_ADDR_SPACE (t2));
416 
417           t1_complete = COMPLETE_TYPE_P (t1);
418           t2_complete = COMPLETE_TYPE_P (t2);
419 
420           d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
421           d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
422 
423           d1_variable = (!d1_zero
424                            && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
425                                  || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
426           d2_variable = (!d2_zero
427                            && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
428                                  || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
429           d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
430           d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
431 
432           /* Save space: see if the result is identical to one of the args.  */
433           if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
434               && (d2_variable || d2_zero || !d1_variable))
435             return build_type_attribute_variant (t1, attributes);
436           if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
437               && (d1_variable || d1_zero || !d2_variable))
438             return build_type_attribute_variant (t2, attributes);
439 
440           if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
441             return build_type_attribute_variant (t1, attributes);
442           if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
443             return build_type_attribute_variant (t2, attributes);
444 
445           /* Merge the element types, and have a size if either arg has
446              one.  We may have qualifiers on the element types.  To set
447              up TYPE_MAIN_VARIANT correctly, we need to form the
448              composite of the unqualified types and add the qualifiers
449              back at the end.  */
450           quals = TYPE_QUALS (strip_array_types (elt));
451           unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
452           t1 = build_array_type (unqual_elt,
453                                      TYPE_DOMAIN ((TYPE_DOMAIN (t1)
454                                                        && (d2_variable
455                                                              || d2_zero
456                                                              || !d1_variable))
457                                                       ? t1
458                                                       : t2));
459           /* Ensure a composite type involving a zero-length array type
460              is a zero-length type not an incomplete type.  */
461           if (d1_zero && d2_zero
462               && (t1_complete || t2_complete)
463               && !COMPLETE_TYPE_P (t1))
464             {
465               TYPE_SIZE (t1) = bitsize_zero_node;
466               TYPE_SIZE_UNIT (t1) = size_zero_node;
467             }
468           t1 = c_build_qualified_type (t1, quals);
469           return build_type_attribute_variant (t1, attributes);
470       }
471 
472     case ENUMERAL_TYPE:
473     case RECORD_TYPE:
474     case UNION_TYPE:
475       if (attributes != NULL)
476           {
477             /* Try harder not to create a new aggregate type.  */
478             if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
479               return t1;
480             if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
481               return t2;
482           }
483       return build_type_attribute_variant (t1, attributes);
484 
485     case FUNCTION_TYPE:
486       /* Function types: prefer the one that specified arg types.
487            If both do, merge the arg types.  Also merge the return types.  */
488       {
489           tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
490           tree p1 = TYPE_ARG_TYPES (t1);
491           tree p2 = TYPE_ARG_TYPES (t2);
492           int len;
493           tree newargs, n;
494           int i;
495 
496           /* Save space: see if the result is identical to one of the args.  */
497           if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
498             return build_type_attribute_variant (t1, attributes);
499           if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
500             return build_type_attribute_variant (t2, attributes);
501 
502           /* Simple way if one arg fails to specify argument types.  */
503           if (TYPE_ARG_TYPES (t1) == 0)
504            {
505               t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
506               t1 = build_type_attribute_variant (t1, attributes);
507               return qualify_type (t1, t2);
508            }
509           if (TYPE_ARG_TYPES (t2) == 0)
510            {
511              t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
512              t1 = build_type_attribute_variant (t1, attributes);
513              return qualify_type (t1, t2);
514            }
515 
516           /* If both args specify argument types, we must merge the two
517              lists, argument by argument.  */
518 
519           len = list_length (p1);
520           newargs = 0;
521 
522           for (i = 0; i < len; i++)
523             newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
524 
525           n = newargs;
526 
527           for (; p1;
528                p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
529             {
530               /* A null type means arg type is not specified.
531                  Take whatever the other function type has.  */
532               if (TREE_VALUE (p1) == 0)
533                 {
534                     TREE_VALUE (n) = TREE_VALUE (p2);
535                     goto parm_done;
536                 }
537               if (TREE_VALUE (p2) == 0)
538                 {
539                     TREE_VALUE (n) = TREE_VALUE (p1);
540                     goto parm_done;
541                 }
542 
543               /* Given  wait (union {union wait *u; int *i} *)
544                  and  wait (union wait *),
545                  prefer  union wait *  as type of parm.  */
546               if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
547                     && TREE_VALUE (p1) != TREE_VALUE (p2))
548                 {
549                     tree memb;
550                     tree mv2 = TREE_VALUE (p2);
551                     if (mv2 && mv2 != error_mark_node
552                         && TREE_CODE (mv2) != ARRAY_TYPE)
553                       mv2 = TYPE_MAIN_VARIANT (mv2);
554                     for (memb = TYPE_FIELDS (TREE_VALUE (p1));
555                          memb; memb = DECL_CHAIN (memb))
556                       {
557                         tree mv3 = TREE_TYPE (memb);
558                         if (mv3 && mv3 != error_mark_node
559                               && TREE_CODE (mv3) != ARRAY_TYPE)
560                           mv3 = TYPE_MAIN_VARIANT (mv3);
561                         if (comptypes (mv3, mv2))
562                           {
563                               TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
564                                                                        TREE_VALUE (p2));
565                               pedwarn (input_location, OPT_pedantic,
566                                          "function types not truly compatible in ISO C");
567                               goto parm_done;
568                           }
569                       }
570                 }
571               if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
572                     && TREE_VALUE (p2) != TREE_VALUE (p1))
573                 {
574                     tree memb;
575                     tree mv1 = TREE_VALUE (p1);
576                     if (mv1 && mv1 != error_mark_node
577                         && TREE_CODE (mv1) != ARRAY_TYPE)
578                       mv1 = TYPE_MAIN_VARIANT (mv1);
579                     for (memb = TYPE_FIELDS (TREE_VALUE (p2));
580                          memb; memb = DECL_CHAIN (memb))
581                       {
582                         tree mv3 = TREE_TYPE (memb);
583                         if (mv3 && mv3 != error_mark_node
584                               && TREE_CODE (mv3) != ARRAY_TYPE)
585                           mv3 = TYPE_MAIN_VARIANT (mv3);
586                         if (comptypes (mv3, mv1))
587                           {
588                               TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
589                                                                        TREE_VALUE (p1));
590                               pedwarn (input_location, OPT_pedantic,
591                                          "function types not truly compatible in ISO C");
592                               goto parm_done;
593                           }
594                       }
595                 }
596               TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
597             parm_done: ;
598             }
599 
600           t1 = build_function_type (valtype, newargs);
601           t1 = qualify_type (t1, t2);
602           /* ... falls through ...  */
603       }
604 
605     default:
606       return build_type_attribute_variant (t1, attributes);
607     }
608 
609 }
610 
611 /* Return the type of a conditional expression between pointers to
612    possibly differently qualified versions of compatible types.
613 
614    We assume that comp_target_types has already been done and returned
615    nonzero; if that isn't so, this may crash.  */
616 
617 static tree
common_pointer_type(tree t1,tree t2)618 common_pointer_type (tree t1, tree t2)
619 {
620   tree attributes;
621   tree pointed_to_1, mv1;
622   tree pointed_to_2, mv2;
623   tree target;
624   unsigned target_quals;
625   addr_space_t as1, as2, as_common;
626   int quals1, quals2;
627 
628   /* Save time if the two types are the same.  */
629 
630   if (t1 == t2) return t1;
631 
632   /* If one type is nonsense, use the other.  */
633   if (t1 == error_mark_node)
634     return t2;
635   if (t2 == error_mark_node)
636     return t1;
637 
638   gcc_assert (TREE_CODE (t1) == POINTER_TYPE
639                 && TREE_CODE (t2) == POINTER_TYPE);
640 
641   /* Merge the attributes.  */
642   attributes = targetm.merge_type_attributes (t1, t2);
643 
644   /* Find the composite type of the target types, and combine the
645      qualifiers of the two types' targets.  Do not lose qualifiers on
646      array element types by taking the TYPE_MAIN_VARIANT.  */
647   mv1 = pointed_to_1 = TREE_TYPE (t1);
648   mv2 = pointed_to_2 = TREE_TYPE (t2);
649   if (TREE_CODE (mv1) != ARRAY_TYPE)
650     mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
651   if (TREE_CODE (mv2) != ARRAY_TYPE)
652     mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
653   target = composite_type (mv1, mv2);
654 
655   /* For function types do not merge const qualifiers, but drop them
656      if used inconsistently.  The middle-end uses these to mark const
657      and noreturn functions.  */
658   quals1 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_1);
659   quals2 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_2);
660 
661   if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
662     target_quals = (quals1 & quals2);
663   else
664     target_quals = (quals1 | quals2);
665 
666   /* If the two named address spaces are different, determine the common
667      superset address space.  This is guaranteed to exist due to the
668      assumption that comp_target_type returned non-zero.  */
669   as1 = TYPE_ADDR_SPACE (pointed_to_1);
670   as2 = TYPE_ADDR_SPACE (pointed_to_2);
671   if (!addr_space_superset (as1, as2, &as_common))
672     gcc_unreachable ();
673 
674   target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
675 
676   t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
677   return build_type_attribute_variant (t1, attributes);
678 }
679 
680 /* Return the common type for two arithmetic types under the usual
681    arithmetic conversions.  The default conversions have already been
682    applied, and enumerated types converted to their compatible integer
683    types.  The resulting type is unqualified and has no attributes.
684 
685    This is the type for the result of most arithmetic operations
686    if the operands have the given two types.  */
687 
688 static tree
c_common_type(tree t1,tree t2)689 c_common_type (tree t1, tree t2)
690 {
691   enum tree_code code1;
692   enum tree_code code2;
693 
694   /* If one type is nonsense, use the other.  */
695   if (t1 == error_mark_node)
696     return t2;
697   if (t2 == error_mark_node)
698     return t1;
699 
700   if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
701     t1 = TYPE_MAIN_VARIANT (t1);
702 
703   if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
704     t2 = TYPE_MAIN_VARIANT (t2);
705 
706   if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
707     t1 = build_type_attribute_variant (t1, NULL_TREE);
708 
709   if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
710     t2 = build_type_attribute_variant (t2, NULL_TREE);
711 
712   /* Save time if the two types are the same.  */
713 
714   if (t1 == t2) return t1;
715 
716   code1 = TREE_CODE (t1);
717   code2 = TREE_CODE (t2);
718 
719   gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
720                 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
721                 || code1 == INTEGER_TYPE);
722   gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
723                 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
724                 || code2 == INTEGER_TYPE);
725 
726   /* When one operand is a decimal float type, the other operand cannot be
727      a generic float type or a complex type.  We also disallow vector types
728      here.  */
729   if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
730       && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
731     {
732       if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
733           {
734             error ("can%'t mix operands of decimal float and vector types");
735             return error_mark_node;
736           }
737       if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
738           {
739             error ("can%'t mix operands of decimal float and complex types");
740             return error_mark_node;
741           }
742       if (code1 == REAL_TYPE && code2 == REAL_TYPE)
743           {
744             error ("can%'t mix operands of decimal float and other float types");
745             return error_mark_node;
746           }
747     }
748 
749   /* If one type is a vector type, return that type.  (How the usual
750      arithmetic conversions apply to the vector types extension is not
751      precisely specified.)  */
752   if (code1 == VECTOR_TYPE)
753     return t1;
754 
755   if (code2 == VECTOR_TYPE)
756     return t2;
757 
758   /* If one type is complex, form the common type of the non-complex
759      components, then make that complex.  Use T1 or T2 if it is the
760      required type.  */
761   if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
762     {
763       tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
764       tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
765       tree subtype = c_common_type (subtype1, subtype2);
766 
767       if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
768           return t1;
769       else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
770           return t2;
771       else
772           return build_complex_type (subtype);
773     }
774 
775   /* If only one is real, use it as the result.  */
776 
777   if (code1 == REAL_TYPE && code2 != REAL_TYPE)
778     return t1;
779 
780   if (code2 == REAL_TYPE && code1 != REAL_TYPE)
781     return t2;
782 
783   /* If both are real and either are decimal floating point types, use
784      the decimal floating point type with the greater precision. */
785 
786   if (code1 == REAL_TYPE && code2 == REAL_TYPE)
787     {
788       if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
789             || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
790           return dfloat128_type_node;
791       else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
792                  || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
793           return dfloat64_type_node;
794       else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
795                  || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
796           return dfloat32_type_node;
797     }
798 
799   /* Deal with fixed-point types.  */
800   if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
801     {
802       unsigned int unsignedp = 0, satp = 0;
803       enum machine_mode m1, m2;
804       unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
805 
806       m1 = TYPE_MODE (t1);
807       m2 = TYPE_MODE (t2);
808 
809       /* If one input type is saturating, the result type is saturating.  */
810       if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
811           satp = 1;
812 
813       /* If both fixed-point types are unsigned, the result type is unsigned.
814            When mixing fixed-point and integer types, follow the sign of the
815            fixed-point type.
816            Otherwise, the result type is signed.  */
817       if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
818              && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
819             || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
820                 && TYPE_UNSIGNED (t1))
821             || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
822                 && TYPE_UNSIGNED (t2)))
823           unsignedp = 1;
824 
825       /* The result type is signed.  */
826       if (unsignedp == 0)
827           {
828             /* If the input type is unsigned, we need to convert to the
829                signed type.  */
830             if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
831               {
832                 enum mode_class mclass = (enum mode_class) 0;
833                 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
834                     mclass = MODE_FRACT;
835                 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
836                     mclass = MODE_ACCUM;
837                 else
838                     gcc_unreachable ();
839                 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
840               }
841             if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
842               {
843                 enum mode_class mclass = (enum mode_class) 0;
844                 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
845                     mclass = MODE_FRACT;
846                 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
847                     mclass = MODE_ACCUM;
848                 else
849                     gcc_unreachable ();
850                 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
851               }
852           }
853 
854       if (code1 == FIXED_POINT_TYPE)
855           {
856             fbit1 = GET_MODE_FBIT (m1);
857             ibit1 = GET_MODE_IBIT (m1);
858           }
859       else
860           {
861             fbit1 = 0;
862             /* Signed integers need to subtract one sign bit.  */
863             ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
864           }
865 
866       if (code2 == FIXED_POINT_TYPE)
867           {
868             fbit2 = GET_MODE_FBIT (m2);
869             ibit2 = GET_MODE_IBIT (m2);
870           }
871       else
872           {
873             fbit2 = 0;
874             /* Signed integers need to subtract one sign bit.  */
875             ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
876           }
877 
878       max_ibit = ibit1 >= ibit2 ?  ibit1 : ibit2;
879       max_fbit = fbit1 >= fbit2 ?  fbit1 : fbit2;
880       return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
881                                                              satp);
882     }
883 
884   /* Both real or both integers; use the one with greater precision.  */
885 
886   if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
887     return t1;
888   else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
889     return t2;
890 
891   /* Same precision.  Prefer long longs to longs to ints when the
892      same precision, following the C99 rules on integer type rank
893      (which are equivalent to the C90 rules for C90 types).  */
894 
895   if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
896       || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
897     return long_long_unsigned_type_node;
898 
899   if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
900       || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
901     {
902       if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
903           return long_long_unsigned_type_node;
904       else
905           return long_long_integer_type_node;
906     }
907 
908   if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
909       || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
910     return long_unsigned_type_node;
911 
912   if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
913       || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
914     {
915       /* But preserve unsignedness from the other type,
916            since long cannot hold all the values of an unsigned int.  */
917       if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
918           return long_unsigned_type_node;
919       else
920           return long_integer_type_node;
921     }
922 
923   /* Likewise, prefer long double to double even if same size.  */
924   if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
925       || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
926     return long_double_type_node;
927 
928   /* Otherwise prefer the unsigned one.  */
929 
930   if (TYPE_UNSIGNED (t1))
931     return t1;
932   else
933     return t2;
934 }
935 
936 /* Wrapper around c_common_type that is used by c-common.c and other
937    front end optimizations that remove promotions.  ENUMERAL_TYPEs
938    are allowed here and are converted to their compatible integer types.
939    BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
940    preferably a non-Boolean type as the common type.  */
941 tree
common_type(tree t1,tree t2)942 common_type (tree t1, tree t2)
943 {
944   if (TREE_CODE (t1) == ENUMERAL_TYPE)
945     t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
946   if (TREE_CODE (t2) == ENUMERAL_TYPE)
947     t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
948 
949   /* If both types are BOOLEAN_TYPE, then return boolean_type_node.  */
950   if (TREE_CODE (t1) == BOOLEAN_TYPE
951       && TREE_CODE (t2) == BOOLEAN_TYPE)
952     return boolean_type_node;
953 
954   /* If either type is BOOLEAN_TYPE, then return the other.  */
955   if (TREE_CODE (t1) == BOOLEAN_TYPE)
956     return t2;
957   if (TREE_CODE (t2) == BOOLEAN_TYPE)
958     return t1;
959 
960   return c_common_type (t1, t2);
961 }
962 
963 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
964    or various other operations.  Return 2 if they are compatible
965    but a warning may be needed if you use them together.  */
966 
967 int
comptypes(tree type1,tree type2)968 comptypes (tree type1, tree type2)
969 {
970   const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
971   int val;
972 
973   val = comptypes_internal (type1, type2, NULL, NULL);
974   free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
975 
976   return val;
977 }
978 
979 /* Like comptypes, but if it returns non-zero because enum and int are
980    compatible, it sets *ENUM_AND_INT_P to true.  */
981 
982 static int
comptypes_check_enum_int(tree type1,tree type2,bool * enum_and_int_p)983 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
984 {
985   const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
986   int val;
987 
988   val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
989   free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
990 
991   return val;
992 }
993 
994 /* Like comptypes, but if it returns nonzero for different types, it
995    sets *DIFFERENT_TYPES_P to true.  */
996 
997 int
comptypes_check_different_types(tree type1,tree type2,bool * different_types_p)998 comptypes_check_different_types (tree type1, tree type2,
999                                          bool *different_types_p)
1000 {
1001   const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1002   int val;
1003 
1004   val = comptypes_internal (type1, type2, NULL, different_types_p);
1005   free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1006 
1007   return val;
1008 }
1009 
1010 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1011    or various other operations.  Return 2 if they are compatible
1012    but a warning may be needed if you use them together.  If
1013    ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1014    compatible integer type, then this sets *ENUM_AND_INT_P to true;
1015    *ENUM_AND_INT_P is never set to false.  If DIFFERENT_TYPES_P is not
1016    NULL, and the types are compatible but different enough not to be
1017    permitted in C11 typedef redeclarations, then this sets
1018    *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1019    false, but may or may not be set if the types are incompatible.
1020    This differs from comptypes, in that we don't free the seen
1021    types.  */
1022 
1023 static int
comptypes_internal(const_tree type1,const_tree type2,bool * enum_and_int_p,bool * different_types_p)1024 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1025                         bool *different_types_p)
1026 {
1027   const_tree t1 = type1;
1028   const_tree t2 = type2;
1029   int attrval, val;
1030 
1031   /* Suppress errors caused by previously reported errors.  */
1032 
1033   if (t1 == t2 || !t1 || !t2
1034       || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1035     return 1;
1036 
1037   /* Enumerated types are compatible with integer types, but this is
1038      not transitive: two enumerated types in the same translation unit
1039      are compatible with each other only if they are the same type.  */
1040 
1041   if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1042     {
1043       t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1044       if (TREE_CODE (t2) != VOID_TYPE)
1045           {
1046             if (enum_and_int_p != NULL)
1047               *enum_and_int_p = true;
1048             if (different_types_p != NULL)
1049               *different_types_p = true;
1050           }
1051     }
1052   else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1053     {
1054       t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1055       if (TREE_CODE (t1) != VOID_TYPE)
1056           {
1057             if (enum_and_int_p != NULL)
1058               *enum_and_int_p = true;
1059             if (different_types_p != NULL)
1060               *different_types_p = true;
1061           }
1062     }
1063 
1064   if (t1 == t2)
1065     return 1;
1066 
1067   /* Different classes of types can't be compatible.  */
1068 
1069   if (TREE_CODE (t1) != TREE_CODE (t2))
1070     return 0;
1071 
1072   /* Qualifiers must match. C99 6.7.3p9 */
1073 
1074   if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1075     return 0;
1076 
1077   /* Allow for two different type nodes which have essentially the same
1078      definition.  Note that we already checked for equality of the type
1079      qualifiers (just above).  */
1080 
1081   if (TREE_CODE (t1) != ARRAY_TYPE
1082       && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1083     return 1;
1084 
1085   /* 1 if no need for warning yet, 2 if warning cause has been seen.  */
1086   if (!(attrval = comp_type_attributes (t1, t2)))
1087      return 0;
1088 
1089   /* 1 if no need for warning yet, 2 if warning cause has been seen.  */
1090   val = 0;
1091 
1092   switch (TREE_CODE (t1))
1093     {
1094     case POINTER_TYPE:
1095       /* Do not remove mode or aliasing information.  */
1096       if (TYPE_MODE (t1) != TYPE_MODE (t2)
1097             || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1098           break;
1099       val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1100                ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1101                                                enum_and_int_p, different_types_p));
1102       break;
1103 
1104     case FUNCTION_TYPE:
1105       val = function_types_compatible_p (t1, t2, enum_and_int_p,
1106                                                    different_types_p);
1107       break;
1108 
1109     case ARRAY_TYPE:
1110       {
1111           tree d1 = TYPE_DOMAIN (t1);
1112           tree d2 = TYPE_DOMAIN (t2);
1113           bool d1_variable, d2_variable;
1114           bool d1_zero, d2_zero;
1115           val = 1;
1116 
1117           /* Target types must match incl. qualifiers.  */
1118           if (TREE_TYPE (t1) != TREE_TYPE (t2)
1119               && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1120                                                          enum_and_int_p,
1121                                                          different_types_p)))
1122             return 0;
1123 
1124           if (different_types_p != NULL
1125               && (d1 == 0) != (d2 == 0))
1126             *different_types_p = true;
1127           /* Sizes must match unless one is missing or variable.  */
1128           if (d1 == 0 || d2 == 0 || d1 == d2)
1129             break;
1130 
1131           d1_zero = !TYPE_MAX_VALUE (d1);
1132           d2_zero = !TYPE_MAX_VALUE (d2);
1133 
1134           d1_variable = (!d1_zero
1135                            && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1136                                  || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1137           d2_variable = (!d2_zero
1138                            && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1139                                  || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1140           d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1141           d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1142 
1143           if (different_types_p != NULL
1144               && d1_variable != d2_variable)
1145             *different_types_p = true;
1146           if (d1_variable || d2_variable)
1147             break;
1148           if (d1_zero && d2_zero)
1149             break;
1150           if (d1_zero || d2_zero
1151               || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1152               || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1153             val = 0;
1154 
1155           break;
1156       }
1157 
1158     case ENUMERAL_TYPE:
1159     case RECORD_TYPE:
1160     case UNION_TYPE:
1161       if (val != 1 && !same_translation_unit_p (t1, t2))
1162           {
1163             tree a1 = TYPE_ATTRIBUTES (t1);
1164             tree a2 = TYPE_ATTRIBUTES (t2);
1165 
1166             if (! attribute_list_contained (a1, a2)
1167                 && ! attribute_list_contained (a2, a1))
1168               break;
1169 
1170             if (attrval != 2)
1171               return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1172                                                              different_types_p);
1173             val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1174                                                         different_types_p);
1175           }
1176       break;
1177 
1178     case VECTOR_TYPE:
1179       val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1180                && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1181                                             enum_and_int_p, different_types_p));
1182       break;
1183 
1184     default:
1185       break;
1186     }
1187   return attrval == 2 && val == 1 ? 2 : val;
1188 }
1189 
1190 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1191    their qualifiers, except for named address spaces.  If the pointers point to
1192    different named addresses, then we must determine if one address space is a
1193    subset of the other.  */
1194 
1195 static int
comp_target_types(location_t location,tree ttl,tree ttr)1196 comp_target_types (location_t location, tree ttl, tree ttr)
1197 {
1198   int val;
1199   tree mvl = TREE_TYPE (ttl);
1200   tree mvr = TREE_TYPE (ttr);
1201   addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1202   addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1203   addr_space_t as_common;
1204   bool enum_and_int_p;
1205 
1206   /* Fail if pointers point to incompatible address spaces.  */
1207   if (!addr_space_superset (asl, asr, &as_common))
1208     return 0;
1209 
1210   /* Do not lose qualifiers on element types of array types that are
1211      pointer targets by taking their TYPE_MAIN_VARIANT.  */
1212   if (TREE_CODE (mvl) != ARRAY_TYPE)
1213     mvl = TYPE_MAIN_VARIANT (mvl);
1214   if (TREE_CODE (mvr) != ARRAY_TYPE)
1215     mvr = TYPE_MAIN_VARIANT (mvr);
1216   enum_and_int_p = false;
1217   val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1218 
1219   if (val == 2)
1220     pedwarn (location, OPT_pedantic, "types are not quite compatible");
1221 
1222   if (val == 1 && enum_and_int_p && warn_cxx_compat)
1223     warning_at (location, OPT_Wc___compat,
1224                     "pointer target types incompatible in C++");
1225 
1226   return val;
1227 }
1228 
1229 /* Subroutines of `comptypes'.  */
1230 
1231 /* Determine whether two trees derive from the same translation unit.
1232    If the CONTEXT chain ends in a null, that tree's context is still
1233    being parsed, so if two trees have context chains ending in null,
1234    they're in the same translation unit.  */
1235 int
same_translation_unit_p(const_tree t1,const_tree t2)1236 same_translation_unit_p (const_tree t1, const_tree t2)
1237 {
1238   while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1239     switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1240       {
1241       case tcc_declaration:
1242           t1 = DECL_CONTEXT (t1); break;
1243       case tcc_type:
1244           t1 = TYPE_CONTEXT (t1); break;
1245       case tcc_exceptional:
1246           t1 = BLOCK_SUPERCONTEXT (t1); break;  /* assume block */
1247       default: gcc_unreachable ();
1248       }
1249 
1250   while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1251     switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1252       {
1253       case tcc_declaration:
1254           t2 = DECL_CONTEXT (t2); break;
1255       case tcc_type:
1256           t2 = TYPE_CONTEXT (t2); break;
1257       case tcc_exceptional:
1258           t2 = BLOCK_SUPERCONTEXT (t2); break;  /* assume block */
1259       default: gcc_unreachable ();
1260       }
1261 
1262   return t1 == t2;
1263 }
1264 
1265 /* Allocate the seen two types, assuming that they are compatible. */
1266 
1267 static struct tagged_tu_seen_cache *
alloc_tagged_tu_seen_cache(const_tree t1,const_tree t2)1268 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1269 {
1270   struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1271   tu->next = tagged_tu_seen_base;
1272   tu->t1 = t1;
1273   tu->t2 = t2;
1274 
1275   tagged_tu_seen_base = tu;
1276 
1277   /* The C standard says that two structures in different translation
1278      units are compatible with each other only if the types of their
1279      fields are compatible (among other things).  We assume that they
1280      are compatible until proven otherwise when building the cache.
1281      An example where this can occur is:
1282      struct a
1283      {
1284        struct a *next;
1285      };
1286      If we are comparing this against a similar struct in another TU,
1287      and did not assume they were compatible, we end up with an infinite
1288      loop.  */
1289   tu->val = 1;
1290   return tu;
1291 }
1292 
1293 /* Free the seen types until we get to TU_TIL. */
1294 
1295 static void
free_all_tagged_tu_seen_up_to(const struct tagged_tu_seen_cache * tu_til)1296 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1297 {
1298   const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1299   while (tu != tu_til)
1300     {
1301       const struct tagged_tu_seen_cache *const tu1
1302           = (const struct tagged_tu_seen_cache *) tu;
1303       tu = tu1->next;
1304       free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1305     }
1306   tagged_tu_seen_base = tu_til;
1307 }
1308 
1309 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1310    compatible.  If the two types are not the same (which has been
1311    checked earlier), this can only happen when multiple translation
1312    units are being compiled.  See C99 6.2.7 paragraph 1 for the exact
1313    rules.  ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1314    comptypes_internal.  */
1315 
1316 static int
tagged_types_tu_compatible_p(const_tree t1,const_tree t2,bool * enum_and_int_p,bool * different_types_p)1317 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1318                                     bool *enum_and_int_p, bool *different_types_p)
1319 {
1320   tree s1, s2;
1321   bool needs_warning = false;
1322 
1323   /* We have to verify that the tags of the types are the same.  This
1324      is harder than it looks because this may be a typedef, so we have
1325      to go look at the original type.  It may even be a typedef of a
1326      typedef...
1327      In the case of compiler-created builtin structs the TYPE_DECL
1328      may be a dummy, with no DECL_ORIGINAL_TYPE.  Don't fault.  */
1329   while (TYPE_NAME (t1)
1330            && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1331            && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1332     t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1333 
1334   while (TYPE_NAME (t2)
1335            && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1336            && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1337     t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1338 
1339   /* C90 didn't have the requirement that the two tags be the same.  */
1340   if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1341     return 0;
1342 
1343   /* C90 didn't say what happened if one or both of the types were
1344      incomplete; we choose to follow C99 rules here, which is that they
1345      are compatible.  */
1346   if (TYPE_SIZE (t1) == NULL
1347       || TYPE_SIZE (t2) == NULL)
1348     return 1;
1349 
1350   {
1351     const struct tagged_tu_seen_cache * tts_i;
1352     for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1353       if (tts_i->t1 == t1 && tts_i->t2 == t2)
1354           return tts_i->val;
1355   }
1356 
1357   switch (TREE_CODE (t1))
1358     {
1359     case ENUMERAL_TYPE:
1360       {
1361           struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1362           /* Speed up the case where the type values are in the same order.  */
1363           tree tv1 = TYPE_VALUES (t1);
1364           tree tv2 = TYPE_VALUES (t2);
1365 
1366           if (tv1 == tv2)
1367             {
1368               return 1;
1369             }
1370 
1371           for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1372             {
1373               if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1374                 break;
1375               if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1376                 {
1377                     tu->val = 0;
1378                     return 0;
1379                 }
1380             }
1381 
1382           if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1383             {
1384               return 1;
1385             }
1386           if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1387             {
1388               tu->val = 0;
1389               return 0;
1390             }
1391 
1392           if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1393             {
1394               tu->val = 0;
1395               return 0;
1396             }
1397 
1398           for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1399             {
1400               s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1401               if (s2 == NULL
1402                     || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1403                 {
1404                     tu->val = 0;
1405                     return 0;
1406                 }
1407             }
1408           return 1;
1409       }
1410 
1411     case UNION_TYPE:
1412       {
1413           struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1414           if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1415             {
1416               tu->val = 0;
1417               return 0;
1418             }
1419 
1420           /*  Speed up the common case where the fields are in the same order. */
1421           for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1422                s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1423             {
1424               int result;
1425 
1426               if (DECL_NAME (s1) != DECL_NAME (s2))
1427                 break;
1428               result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1429                                                    enum_and_int_p, different_types_p);
1430 
1431               if (result != 1 && !DECL_NAME (s1))
1432                 break;
1433               if (result == 0)
1434                 {
1435                     tu->val = 0;
1436                     return 0;
1437                 }
1438               if (result == 2)
1439                 needs_warning = true;
1440 
1441               if (TREE_CODE (s1) == FIELD_DECL
1442                     && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1443                                              DECL_FIELD_BIT_OFFSET (s2)) != 1)
1444                 {
1445                     tu->val = 0;
1446                     return 0;
1447                 }
1448             }
1449           if (!s1 && !s2)
1450             {
1451               tu->val = needs_warning ? 2 : 1;
1452               return tu->val;
1453             }
1454 
1455           for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1456             {
1457               bool ok = false;
1458 
1459               for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1460                 if (DECL_NAME (s1) == DECL_NAME (s2))
1461                     {
1462                       int result;
1463 
1464                       result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1465                                                          enum_and_int_p,
1466                                                          different_types_p);
1467 
1468                       if (result != 1 && !DECL_NAME (s1))
1469                         continue;
1470                       if (result == 0)
1471                         {
1472                           tu->val = 0;
1473                           return 0;
1474                         }
1475                       if (result == 2)
1476                         needs_warning = true;
1477 
1478                       if (TREE_CODE (s1) == FIELD_DECL
1479                           && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1480                                                      DECL_FIELD_BIT_OFFSET (s2)) != 1)
1481                         break;
1482 
1483                       ok = true;
1484                       break;
1485                     }
1486               if (!ok)
1487                 {
1488                     tu->val = 0;
1489                     return 0;
1490                 }
1491             }
1492           tu->val = needs_warning ? 2 : 10;
1493           return tu->val;
1494       }
1495 
1496     case RECORD_TYPE:
1497       {
1498           struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1499 
1500           for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1501                s1 && s2;
1502                s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1503             {
1504               int result;
1505               if (TREE_CODE (s1) != TREE_CODE (s2)
1506                     || DECL_NAME (s1) != DECL_NAME (s2))
1507                 break;
1508               result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1509                                                    enum_and_int_p, different_types_p);
1510               if (result == 0)
1511                 break;
1512               if (result == 2)
1513                 needs_warning = true;
1514 
1515               if (TREE_CODE (s1) == FIELD_DECL
1516                     && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1517                                              DECL_FIELD_BIT_OFFSET (s2)) != 1)
1518                 break;
1519             }
1520           if (s1 && s2)
1521             tu->val = 0;
1522           else
1523             tu->val = needs_warning ? 2 : 1;
1524           return tu->val;
1525       }
1526 
1527     default:
1528       gcc_unreachable ();
1529     }
1530 }
1531 
1532 /* Return 1 if two function types F1 and F2 are compatible.
1533    If either type specifies no argument types,
1534    the other must specify a fixed number of self-promoting arg types.
1535    Otherwise, if one type specifies only the number of arguments,
1536    the other must specify that number of self-promoting arg types.
1537    Otherwise, the argument types must match.
1538    ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal.  */
1539 
1540 static int
function_types_compatible_p(const_tree f1,const_tree f2,bool * enum_and_int_p,bool * different_types_p)1541 function_types_compatible_p (const_tree f1, const_tree f2,
1542                                    bool *enum_and_int_p, bool *different_types_p)
1543 {
1544   tree args1, args2;
1545   /* 1 if no need for warning yet, 2 if warning cause has been seen.  */
1546   int val = 1;
1547   int val1;
1548   tree ret1, ret2;
1549 
1550   ret1 = TREE_TYPE (f1);
1551   ret2 = TREE_TYPE (f2);
1552 
1553   /* 'volatile' qualifiers on a function's return type used to mean
1554      the function is noreturn.  */
1555   if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1556     pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1557   if (TYPE_VOLATILE (ret1))
1558     ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1559                                          TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1560   if (TYPE_VOLATILE (ret2))
1561     ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1562                                          TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1563   val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1564   if (val == 0)
1565     return 0;
1566 
1567   args1 = TYPE_ARG_TYPES (f1);
1568   args2 = TYPE_ARG_TYPES (f2);
1569 
1570   if (different_types_p != NULL
1571       && (args1 == 0) != (args2 == 0))
1572     *different_types_p = true;
1573 
1574   /* An unspecified parmlist matches any specified parmlist
1575      whose argument types don't need default promotions.  */
1576 
1577   if (args1 == 0)
1578     {
1579       if (!self_promoting_args_p (args2))
1580           return 0;
1581       /* If one of these types comes from a non-prototype fn definition,
1582            compare that with the other type's arglist.
1583            If they don't match, ask for a warning (but no error).  */
1584       if (TYPE_ACTUAL_ARG_TYPES (f1)
1585             && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1586                                                      enum_and_int_p, different_types_p))
1587           val = 2;
1588       return val;
1589     }
1590   if (args2 == 0)
1591     {
1592       if (!self_promoting_args_p (args1))
1593           return 0;
1594       if (TYPE_ACTUAL_ARG_TYPES (f2)
1595             && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1596                                                      enum_and_int_p, different_types_p))
1597           val = 2;
1598       return val;
1599     }
1600 
1601   /* Both types have argument lists: compare them and propagate results.  */
1602   val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1603                                           different_types_p);
1604   return val1 != 1 ? val1 : val;
1605 }
1606 
1607 /* Check two lists of types for compatibility, returning 0 for
1608    incompatible, 1 for compatible, or 2 for compatible with
1609    warning.  ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1610    comptypes_internal.  */
1611 
1612 static int
type_lists_compatible_p(const_tree args1,const_tree args2,bool * enum_and_int_p,bool * different_types_p)1613 type_lists_compatible_p (const_tree args1, const_tree args2,
1614                                bool *enum_and_int_p, bool *different_types_p)
1615 {
1616   /* 1 if no need for warning yet, 2 if warning cause has been seen.  */
1617   int val = 1;
1618   int newval = 0;
1619 
1620   while (1)
1621     {
1622       tree a1, mv1, a2, mv2;
1623       if (args1 == 0 && args2 == 0)
1624           return val;
1625       /* If one list is shorter than the other,
1626            they fail to match.  */
1627       if (args1 == 0 || args2 == 0)
1628           return 0;
1629       mv1 = a1 = TREE_VALUE (args1);
1630       mv2 = a2 = TREE_VALUE (args2);
1631       if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1632           mv1 = TYPE_MAIN_VARIANT (mv1);
1633       if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1634           mv2 = TYPE_MAIN_VARIANT (mv2);
1635       /* A null pointer instead of a type
1636            means there is supposed to be an argument
1637            but nothing is specified about what type it has.
1638            So match anything that self-promotes.  */
1639       if (different_types_p != NULL
1640             && (a1 == 0) != (a2 == 0))
1641           *different_types_p = true;
1642       if (a1 == 0)
1643           {
1644             if (c_type_promotes_to (a2) != a2)
1645               return 0;
1646           }
1647       else if (a2 == 0)
1648           {
1649             if (c_type_promotes_to (a1) != a1)
1650               return 0;
1651           }
1652       /* If one of the lists has an error marker, ignore this arg.  */
1653       else if (TREE_CODE (a1) == ERROR_MARK
1654                  || TREE_CODE (a2) == ERROR_MARK)
1655           ;
1656       else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1657                                                         different_types_p)))
1658           {
1659             if (different_types_p != NULL)
1660               *different_types_p = true;
1661             /* Allow  wait (union {union wait *u; int *i} *)
1662                and  wait (union wait *)  to be compatible.  */
1663             if (TREE_CODE (a1) == UNION_TYPE
1664                 && (TYPE_NAME (a1) == 0
1665                       || TYPE_TRANSPARENT_AGGR (a1))
1666                 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1667                 && tree_int_cst_equal (TYPE_SIZE (a1),
1668                                              TYPE_SIZE (a2)))
1669               {
1670                 tree memb;
1671                 for (memb = TYPE_FIELDS (a1);
1672                        memb; memb = DECL_CHAIN (memb))
1673                     {
1674                       tree mv3 = TREE_TYPE (memb);
1675                       if (mv3 && mv3 != error_mark_node
1676                           && TREE_CODE (mv3) != ARRAY_TYPE)
1677                         mv3 = TYPE_MAIN_VARIANT (mv3);
1678                       if (comptypes_internal (mv3, mv2, enum_and_int_p,
1679                                                     different_types_p))
1680                         break;
1681                     }
1682                 if (memb == 0)
1683                     return 0;
1684               }
1685             else if (TREE_CODE (a2) == UNION_TYPE
1686                        && (TYPE_NAME (a2) == 0
1687                            || TYPE_TRANSPARENT_AGGR (a2))
1688                        && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1689                        && tree_int_cst_equal (TYPE_SIZE (a2),
1690                                                     TYPE_SIZE (a1)))
1691               {
1692                 tree memb;
1693                 for (memb = TYPE_FIELDS (a2);
1694                        memb; memb = DECL_CHAIN (memb))
1695                     {
1696                       tree mv3 = TREE_TYPE (memb);
1697                       if (mv3 && mv3 != error_mark_node
1698                           && TREE_CODE (mv3) != ARRAY_TYPE)
1699                         mv3 = TYPE_MAIN_VARIANT (mv3);
1700                       if (comptypes_internal (mv3, mv1, enum_and_int_p,
1701                                                     different_types_p))
1702                         break;
1703                     }
1704                 if (memb == 0)
1705                     return 0;
1706               }
1707             else
1708               return 0;
1709           }
1710 
1711       /* comptypes said ok, but record if it said to warn.  */
1712       if (newval > val)
1713           val = newval;
1714 
1715       args1 = TREE_CHAIN (args1);
1716       args2 = TREE_CHAIN (args2);
1717     }
1718 }
1719 
1720 /* Compute the size to increment a pointer by.  */
1721 
1722 static tree
c_size_in_bytes(const_tree type)1723 c_size_in_bytes (const_tree type)
1724 {
1725   enum tree_code code = TREE_CODE (type);
1726 
1727   if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1728     return size_one_node;
1729 
1730   if (!COMPLETE_OR_VOID_TYPE_P (type))
1731     {
1732       error ("arithmetic on pointer to an incomplete type");
1733       return size_one_node;
1734     }
1735 
1736   /* Convert in case a char is more than one unit.  */
1737   return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1738                                size_int (TYPE_PRECISION (char_type_node)
1739                                            / BITS_PER_UNIT));
1740 }
1741 
1742 /* Return either DECL or its known constant value (if it has one).  */
1743 
1744 tree
decl_constant_value(tree decl)1745 decl_constant_value (tree decl)
1746 {
1747   if (/* Don't change a variable array bound or initial value to a constant
1748            in a place where a variable is invalid.  Note that DECL_INITIAL
1749            isn't valid for a PARM_DECL.  */
1750       current_function_decl != 0
1751       && TREE_CODE (decl) != PARM_DECL
1752       && !TREE_THIS_VOLATILE (decl)
1753       && TREE_READONLY (decl)
1754       && DECL_INITIAL (decl) != 0
1755       && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1756       /* This is invalid if initial value is not constant.
1757            If it has either a function call, a memory reference,
1758            or a variable, then re-evaluating it could give different results.  */
1759       && TREE_CONSTANT (DECL_INITIAL (decl))
1760       /* Check for cases where this is sub-optimal, even though valid.  */
1761       && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1762     return DECL_INITIAL (decl);
1763   return decl;
1764 }
1765 
1766 /* Convert the array expression EXP to a pointer.  */
1767 static tree
array_to_pointer_conversion(location_t loc,tree exp)1768 array_to_pointer_conversion (location_t loc, tree exp)
1769 {
1770   tree orig_exp = exp;
1771   tree type = TREE_TYPE (exp);
1772   tree adr;
1773   tree restype = TREE_TYPE (type);
1774   tree ptrtype;
1775 
1776   gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1777 
1778   STRIP_TYPE_NOPS (exp);
1779 
1780   if (TREE_NO_WARNING (orig_exp))
1781     TREE_NO_WARNING (exp) = 1;
1782 
1783   ptrtype = build_pointer_type (restype);
1784 
1785   if (TREE_CODE (exp) == INDIRECT_REF)
1786     return convert (ptrtype, TREE_OPERAND (exp, 0));
1787 
1788   /* In C++ array compound literals are temporary objects unless they are
1789      const or appear in namespace scope, so they are destroyed too soon
1790      to use them for much of anything  (c++/53220).  */
1791   if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1792     {
1793       tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1794       if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1795           warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1796                         "converting an array compound literal to a pointer "
1797                         "is ill-formed in C++");
1798     }
1799 
1800   adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
1801   return convert (ptrtype, adr);
1802 }
1803 
1804 /* Convert the function expression EXP to a pointer.  */
1805 static tree
function_to_pointer_conversion(location_t loc,tree exp)1806 function_to_pointer_conversion (location_t loc, tree exp)
1807 {
1808   tree orig_exp = exp;
1809 
1810   gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1811 
1812   STRIP_TYPE_NOPS (exp);
1813 
1814   if (TREE_NO_WARNING (orig_exp))
1815     TREE_NO_WARNING (exp) = 1;
1816 
1817   return build_unary_op (loc, ADDR_EXPR, exp, 0);
1818 }
1819 
1820 /* Mark EXP as read, not just set, for set but not used -Wunused
1821    warning purposes.  */
1822 
1823 void
mark_exp_read(tree exp)1824 mark_exp_read (tree exp)
1825 {
1826   switch (TREE_CODE (exp))
1827     {
1828     case VAR_DECL:
1829     case PARM_DECL:
1830       DECL_READ_P (exp) = 1;
1831       break;
1832     case ARRAY_REF:
1833     case COMPONENT_REF:
1834     case MODIFY_EXPR:
1835     case REALPART_EXPR:
1836     case IMAGPART_EXPR:
1837     CASE_CONVERT:
1838     case ADDR_EXPR:
1839       mark_exp_read (TREE_OPERAND (exp, 0));
1840       break;
1841     case COMPOUND_EXPR:
1842     case C_MAYBE_CONST_EXPR:
1843       mark_exp_read (TREE_OPERAND (exp, 1));
1844       break;
1845     default:
1846       break;
1847     }
1848 }
1849 
1850 /* Perform the default conversion of arrays and functions to pointers.
1851    Return the result of converting EXP.  For any other expression, just
1852    return EXP.
1853 
1854    LOC is the location of the expression.  */
1855 
1856 struct c_expr
default_function_array_conversion(location_t loc,struct c_expr exp)1857 default_function_array_conversion (location_t loc, struct c_expr exp)
1858 {
1859   tree orig_exp = exp.value;
1860   tree type = TREE_TYPE (exp.value);
1861   enum tree_code code = TREE_CODE (type);
1862 
1863   switch (code)
1864     {
1865     case ARRAY_TYPE:
1866       {
1867           bool not_lvalue = false;
1868           bool lvalue_array_p;
1869 
1870           while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1871                     || CONVERT_EXPR_P (exp.value))
1872                  && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1873             {
1874               if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1875                 not_lvalue = true;
1876               exp.value = TREE_OPERAND (exp.value, 0);
1877             }
1878 
1879           if (TREE_NO_WARNING (orig_exp))
1880             TREE_NO_WARNING (exp.value) = 1;
1881 
1882           lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1883           if (!flag_isoc99 && !lvalue_array_p)
1884             {
1885               /* Before C99, non-lvalue arrays do not decay to pointers.
1886                  Normally, using such an array would be invalid; but it can
1887                  be used correctly inside sizeof or as a statement expression.
1888                  Thus, do not give an error here; an error will result later.  */
1889               return exp;
1890             }
1891 
1892           exp.value = array_to_pointer_conversion (loc, exp.value);
1893       }
1894       break;
1895     case FUNCTION_TYPE:
1896       exp.value = function_to_pointer_conversion (loc, exp.value);
1897       break;
1898     default:
1899       break;
1900     }
1901 
1902   return exp;
1903 }
1904 
1905 struct c_expr
default_function_array_read_conversion(location_t loc,struct c_expr exp)1906 default_function_array_read_conversion (location_t loc, struct c_expr exp)
1907 {
1908   mark_exp_read (exp.value);
1909   return default_function_array_conversion (loc, exp);
1910 }
1911 
1912 /* EXP is an expression of integer type.  Apply the integer promotions
1913    to it and return the promoted value.  */
1914 
1915 tree
perform_integral_promotions(tree exp)1916 perform_integral_promotions (tree exp)
1917 {
1918   tree type = TREE_TYPE (exp);
1919   enum tree_code code = TREE_CODE (type);
1920 
1921   gcc_assert (INTEGRAL_TYPE_P (type));
1922 
1923   /* Normally convert enums to int,
1924      but convert wide enums to something wider.  */
1925   if (code == ENUMERAL_TYPE)
1926     {
1927       type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1928                                                     TYPE_PRECISION (integer_type_node)),
1929                                              ((TYPE_PRECISION (type)
1930                                                >= TYPE_PRECISION (integer_type_node))
1931                                               && TYPE_UNSIGNED (type)));
1932 
1933       return convert (type, exp);
1934     }
1935 
1936   /* ??? This should no longer be needed now bit-fields have their
1937      proper types.  */
1938   if (TREE_CODE (exp) == COMPONENT_REF
1939       && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1940       /* If it's thinner than an int, promote it like a
1941            c_promoting_integer_type_p, otherwise leave it alone.  */
1942       && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1943                                      TYPE_PRECISION (integer_type_node)))
1944     return convert (integer_type_node, exp);
1945 
1946   if (c_promoting_integer_type_p (type))
1947     {
1948       /* Preserve unsignedness if not really getting any wider.  */
1949       if (TYPE_UNSIGNED (type)
1950             && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1951           return convert (unsigned_type_node, exp);
1952 
1953       return convert (integer_type_node, exp);
1954     }
1955 
1956   return exp;
1957 }
1958 
1959 
1960 /* Perform default promotions for C data used in expressions.
1961    Enumeral types or short or char are converted to int.
1962    In addition, manifest constants symbols are replaced by their values.  */
1963 
1964 tree
default_conversion(tree exp)1965 default_conversion (tree exp)
1966 {
1967   tree orig_exp;
1968   tree type = TREE_TYPE (exp);
1969   enum tree_code code = TREE_CODE (type);
1970   tree promoted_type;
1971 
1972   mark_exp_read (exp);
1973 
1974   /* Functions and arrays have been converted during parsing.  */
1975   gcc_assert (code != FUNCTION_TYPE);
1976   if (code == ARRAY_TYPE)
1977     return exp;
1978 
1979   /* Constants can be used directly unless they're not loadable.  */
1980   if (TREE_CODE (exp) == CONST_DECL)
1981     exp = DECL_INITIAL (exp);
1982 
1983   /* Strip no-op conversions.  */
1984   orig_exp = exp;
1985   STRIP_TYPE_NOPS (exp);
1986 
1987   if (TREE_NO_WARNING (orig_exp))
1988     TREE_NO_WARNING (exp) = 1;
1989 
1990   if (code == VOID_TYPE)
1991     {
1992       error ("void value not ignored as it ought to be");
1993       return error_mark_node;
1994     }
1995 
1996   exp = require_complete_type (exp);
1997   if (exp == error_mark_node)
1998     return error_mark_node;
1999 
2000   promoted_type = targetm.promoted_type (type);
2001   if (promoted_type)
2002     return convert (promoted_type, exp);
2003 
2004   if (INTEGRAL_TYPE_P (type))
2005     return perform_integral_promotions (exp);
2006 
2007   return exp;
2008 }
2009 
2010 /* Look up COMPONENT in a structure or union TYPE.
2011 
2012    If the component name is not found, returns NULL_TREE.  Otherwise,
2013    the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2014    stepping down the chain to the component, which is in the last
2015    TREE_VALUE of the list.  Normally the list is of length one, but if
2016    the component is embedded within (nested) anonymous structures or
2017    unions, the list steps down the chain to the component.  */
2018 
2019 static tree
lookup_field(tree type,tree component)2020 lookup_field (tree type, tree component)
2021 {
2022   tree field;
2023 
2024   /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2025      to the field elements.  Use a binary search on this array to quickly
2026      find the element.  Otherwise, do a linear search.  TYPE_LANG_SPECIFIC
2027      will always be set for structures which have many elements.  */
2028 
2029   if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2030     {
2031       int bot, top, half;
2032       tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2033 
2034       field = TYPE_FIELDS (type);
2035       bot = 0;
2036       top = TYPE_LANG_SPECIFIC (type)->s->len;
2037       while (top - bot > 1)
2038           {
2039             half = (top - bot + 1) >> 1;
2040             field = field_array[bot+half];
2041 
2042             if (DECL_NAME (field) == NULL_TREE)
2043               {
2044                 /* Step through all anon unions in linear fashion.  */
2045                 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2046                     {
2047                       field = field_array[bot++];
2048                       if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2049                           || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
2050                         {
2051                           tree anon = lookup_field (TREE_TYPE (field), component);
2052 
2053                           if (anon)
2054                               return tree_cons (NULL_TREE, field, anon);
2055 
2056                           /* The Plan 9 compiler permits referring
2057                                directly to an anonymous struct/union field
2058                                using a typedef name.  */
2059                           if (flag_plan9_extensions
2060                                 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2061                                 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2062                                     == TYPE_DECL)
2063                                 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2064                                     == component))
2065                               break;
2066                         }
2067                     }
2068 
2069                 /* Entire record is only anon unions.  */
2070                 if (bot > top)
2071                     return NULL_TREE;
2072 
2073                 /* Restart the binary search, with new lower bound.  */
2074                 continue;
2075               }
2076 
2077             if (DECL_NAME (field) == component)
2078               break;
2079             if (DECL_NAME (field) < component)
2080               bot += half;
2081             else
2082               top = bot + half;
2083           }
2084 
2085       if (DECL_NAME (field_array[bot]) == component)
2086           field = field_array[bot];
2087       else if (DECL_NAME (field) != component)
2088           return NULL_TREE;
2089     }
2090   else
2091     {
2092       for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2093           {
2094             if (DECL_NAME (field) == NULL_TREE
2095                 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2096                       || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2097               {
2098                 tree anon = lookup_field (TREE_TYPE (field), component);
2099 
2100                 if (anon)
2101                     return tree_cons (NULL_TREE, field, anon);
2102 
2103                 /* The Plan 9 compiler permits referring directly to an
2104                      anonymous struct/union field using a typedef
2105                      name.  */
2106                 if (flag_plan9_extensions
2107                       && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2108                       && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2109                       && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2110                           == component))
2111                     break;
2112               }
2113 
2114             if (DECL_NAME (field) == component)
2115               break;
2116           }
2117 
2118       if (field == NULL_TREE)
2119           return NULL_TREE;
2120     }
2121 
2122   return tree_cons (NULL_TREE, field, NULL_TREE);
2123 }
2124 
2125 /* Make an expression to refer to the COMPONENT field of structure or
2126    union value DATUM.  COMPONENT is an IDENTIFIER_NODE.  LOC is the
2127    location of the COMPONENT_REF.  */
2128 
2129 tree
build_component_ref(location_t loc,tree datum,tree component)2130 build_component_ref (location_t loc, tree datum, tree component)
2131 {
2132   tree type = TREE_TYPE (datum);
2133   enum tree_code code = TREE_CODE (type);
2134   tree field = NULL;
2135   tree ref;
2136   bool datum_lvalue = lvalue_p (datum);
2137 
2138   if (!objc_is_public (datum, component))
2139     return error_mark_node;
2140 
2141   /* Detect Objective-C property syntax object.property.  */
2142   if (c_dialect_objc ()
2143       && (ref = objc_maybe_build_component_ref (datum, component)))
2144     return ref;
2145 
2146   /* See if there is a field or component with name COMPONENT.  */
2147 
2148   if (code == RECORD_TYPE || code == UNION_TYPE)
2149     {
2150       if (!COMPLETE_TYPE_P (type))
2151           {
2152             c_incomplete_type_error (NULL_TREE, type);
2153             return error_mark_node;
2154           }
2155 
2156       field = lookup_field (type, component);
2157 
2158       if (!field)
2159           {
2160             error_at (loc, "%qT has no member named %qE", type, component);
2161             return error_mark_node;
2162           }
2163 
2164       /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2165            This might be better solved in future the way the C++ front
2166            end does it - by giving the anonymous entities each a
2167            separate name and type, and then have build_component_ref
2168            recursively call itself.  We can't do that here.  */
2169       do
2170           {
2171             tree subdatum = TREE_VALUE (field);
2172             int quals;
2173             tree subtype;
2174             bool use_datum_quals;
2175 
2176             if (TREE_TYPE (subdatum) == error_mark_node)
2177               return error_mark_node;
2178 
2179             /* If this is an rvalue, it does not have qualifiers in C
2180                standard terms and we must avoid propagating such
2181                qualifiers down to a non-lvalue array that is then
2182                converted to a pointer.  */
2183             use_datum_quals = (datum_lvalue
2184                                    || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2185 
2186             quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2187             if (use_datum_quals)
2188               quals |= TYPE_QUALS (TREE_TYPE (datum));
2189             subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2190 
2191             ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2192                               NULL_TREE);
2193             SET_EXPR_LOCATION (ref, loc);
2194             if (TREE_READONLY (subdatum)
2195                 || (use_datum_quals && TREE_READONLY (datum)))
2196               TREE_READONLY (ref) = 1;
2197             if (TREE_THIS_VOLATILE (subdatum)
2198                 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2199               TREE_THIS_VOLATILE (ref) = 1;
2200 
2201             if (TREE_DEPRECATED (subdatum))
2202               warn_deprecated_use (subdatum, NULL_TREE);
2203 
2204             datum = ref;
2205 
2206             field = TREE_CHAIN (field);
2207           }
2208       while (field);
2209 
2210       return ref;
2211     }
2212   else if (code != ERROR_MARK)
2213     error_at (loc,
2214                 "request for member %qE in something not a structure or union",
2215                 component);
2216 
2217   return error_mark_node;
2218 }
2219 
2220 /* Given an expression PTR for a pointer, return an expression
2221    for the value pointed to.
2222    ERRORSTRING is the name of the operator to appear in error messages.
2223 
2224    LOC is the location to use for the generated tree.  */
2225 
2226 tree
build_indirect_ref(location_t loc,tree ptr,ref_operator errstring)2227 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2228 {
2229   tree pointer = default_conversion (ptr);
2230   tree type = TREE_TYPE (pointer);
2231   tree ref;
2232 
2233   if (TREE_CODE (type) == POINTER_TYPE)
2234     {
2235       if (CONVERT_EXPR_P (pointer)
2236           || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2237           {
2238             /* If a warning is issued, mark it to avoid duplicates from
2239                the backend.  This only needs to be done at
2240                warn_strict_aliasing > 2.  */
2241             if (warn_strict_aliasing > 2)
2242               if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2243                                                    type, TREE_OPERAND (pointer, 0)))
2244                 TREE_NO_WARNING (pointer) = 1;
2245           }
2246 
2247       if (TREE_CODE (pointer) == ADDR_EXPR
2248             && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2249                 == TREE_TYPE (type)))
2250           {
2251             ref = TREE_OPERAND (pointer, 0);
2252             protected_set_expr_location (ref, loc);
2253             return ref;
2254           }
2255       else
2256           {
2257             tree t = TREE_TYPE (type);
2258 
2259             ref = build1 (INDIRECT_REF, t, pointer);
2260 
2261             if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2262               {
2263                 error_at (loc, "dereferencing pointer to incomplete type");
2264                 return error_mark_node;
2265               }
2266             if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2267               warning_at (loc, 0, "dereferencing %<void *%> pointer");
2268 
2269             /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2270                so that we get the proper error message if the result is used
2271                to assign to.  Also, &* is supposed to be a no-op.
2272                And ANSI C seems to specify that the type of the result
2273                should be the const type.  */
2274             /* A de-reference of a pointer to const is not a const.  It is valid
2275                to change it via some other pointer.  */
2276             TREE_READONLY (ref) = TYPE_READONLY (t);
2277             TREE_SIDE_EFFECTS (ref)
2278               = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2279             TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2280             protected_set_expr_location (ref, loc);
2281             return ref;
2282           }
2283     }
2284   else if (TREE_CODE (pointer) != ERROR_MARK)
2285     invalid_indirection_error (loc, type, errstring);
2286 
2287   return error_mark_node;
2288 }
2289 
2290 /* This handles expressions of the form "a[i]", which denotes
2291    an array reference.
2292 
2293    This is logically equivalent in C to *(a+i), but we may do it differently.
2294    If A is a variable or a member, we generate a primitive ARRAY_REF.
2295    This avoids forcing the array out of registers, and can work on
2296    arrays that are not lvalues (for example, members of structures returned
2297    by functions).
2298 
2299    For vector types, allow vector[i] but not i[vector], and create
2300    *(((type*)&vectortype) + i) for the expression.
2301 
2302    LOC is the location to use for the returned expression.  */
2303 
2304 tree
build_array_ref(location_t loc,tree array,tree index)2305 build_array_ref (location_t loc, tree array, tree index)
2306 {
2307   tree ret;
2308   bool swapped = false;
2309   if (TREE_TYPE (array) == error_mark_node
2310       || TREE_TYPE (index) == error_mark_node)
2311     return error_mark_node;
2312 
2313   if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2314       && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2315       /* Allow vector[index] but not index[vector].  */
2316       && TREE_CODE (TREE_TYPE (array)) != VECTOR_TYPE)
2317     {
2318       tree temp;
2319       if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2320             && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2321           {
2322           error_at (loc,
2323             "subscripted value is neither array nor pointer nor vector");
2324 
2325             return error_mark_node;
2326           }
2327       temp = array;
2328       array = index;
2329       index = temp;
2330       swapped = true;
2331     }
2332 
2333   if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2334     {
2335       error_at (loc, "array subscript is not an integer");
2336       return error_mark_node;
2337     }
2338 
2339   if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2340     {
2341       error_at (loc, "subscripted value is pointer to function");
2342       return error_mark_node;
2343     }
2344 
2345   /* ??? Existing practice has been to warn only when the char
2346      index is syntactically the index, not for char[array].  */
2347   if (!swapped)
2348      warn_array_subscript_with_type_char (index);
2349 
2350   /* Apply default promotions *after* noticing character types.  */
2351   index = default_conversion (index);
2352 
2353   gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2354 
2355   /* For vector[index], convert the vector to a
2356      pointer of the underlying type.  */
2357   if (TREE_CODE (TREE_TYPE (array)) == VECTOR_TYPE)
2358     {
2359       tree type = TREE_TYPE (array);
2360       tree type1;
2361 
2362       if (TREE_CODE (index) == INTEGER_CST)
2363         if (!host_integerp (index, 1)
2364             || ((unsigned HOST_WIDE_INT) tree_low_cst (index, 1)
2365                >= TYPE_VECTOR_SUBPARTS (TREE_TYPE (array))))
2366           warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
2367 
2368       c_common_mark_addressable_vec (array);
2369       type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
2370       type = build_pointer_type (type);
2371       type1 = build_pointer_type (TREE_TYPE (array));
2372       array = build1 (ADDR_EXPR, type1, array);
2373       array = convert (type, array);
2374     }
2375 
2376   if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2377     {
2378       tree rval, type;
2379 
2380       /* An array that is indexed by a non-constant
2381            cannot be stored in a register; we must be able to do
2382            address arithmetic on its address.
2383            Likewise an array of elements of variable size.  */
2384       if (TREE_CODE (index) != INTEGER_CST
2385             || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2386                 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2387           {
2388             if (!c_mark_addressable (array))
2389               return error_mark_node;
2390           }
2391       /* An array that is indexed by a constant value which is not within
2392            the array bounds cannot be stored in a register either; because we
2393            would get a crash in store_bit_field/extract_bit_field when trying
2394            to access a non-existent part of the register.  */
2395       if (TREE_CODE (index) == INTEGER_CST
2396             && TYPE_DOMAIN (TREE_TYPE (array))
2397             && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2398           {
2399             if (!c_mark_addressable (array))
2400               return error_mark_node;
2401           }
2402 
2403       if (pedantic)
2404           {
2405             tree foo = array;
2406             while (TREE_CODE (foo) == COMPONENT_REF)
2407               foo = TREE_OPERAND (foo, 0);
2408             if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
2409               pedwarn (loc, OPT_pedantic,
2410                          "ISO C forbids subscripting %<register%> array");
2411             else if (!flag_isoc99 && !lvalue_p (foo))
2412               pedwarn (loc, OPT_pedantic,
2413                          "ISO C90 forbids subscripting non-lvalue array");
2414           }
2415 
2416       type = TREE_TYPE (TREE_TYPE (array));
2417       rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2418       /* Array ref is const/volatile if the array elements are
2419            or if the array is.  */
2420       TREE_READONLY (rval)
2421           |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2422               | TREE_READONLY (array));
2423       TREE_SIDE_EFFECTS (rval)
2424           |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2425               | TREE_SIDE_EFFECTS (array));
2426       TREE_THIS_VOLATILE (rval)
2427           |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2428               /* This was added by rms on 16 Nov 91.
2429                  It fixes  vol struct foo *a;  a->elts[1]
2430                  in an inline function.
2431                  Hope it doesn't break something else.  */
2432               | TREE_THIS_VOLATILE (array));
2433       ret = require_complete_type (rval);
2434       protected_set_expr_location (ret, loc);
2435       return ret;
2436     }
2437   else
2438     {
2439       tree ar = default_conversion (array);
2440 
2441       if (ar == error_mark_node)
2442           return ar;
2443 
2444       gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2445       gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2446 
2447       return build_indirect_ref
2448           (loc, build_binary_op (loc, PLUS_EXPR, ar, index, 0),
2449            RO_ARRAY_INDEXING);
2450     }
2451 }
2452 
2453 /* Build an external reference to identifier ID.  FUN indicates
2454    whether this will be used for a function call.  LOC is the source
2455    location of the identifier.  This sets *TYPE to the type of the
2456    identifier, which is not the same as the type of the returned value
2457    for CONST_DECLs defined as enum constants.  If the type of the
2458    identifier is not available, *TYPE is set to NULL.  */
2459 tree
build_external_ref(location_t loc,tree id,int fun,tree * type)2460 build_external_ref (location_t loc, tree id, int fun, tree *type)
2461 {
2462   tree ref;
2463   tree decl = lookup_name (id);
2464 
2465   /* In Objective-C, an instance variable (ivar) may be preferred to
2466      whatever lookup_name() found.  */
2467   decl = objc_lookup_ivar (decl, id);
2468 
2469   *type = NULL;
2470   if (decl && decl != error_mark_node)
2471     {
2472       ref = decl;
2473       *type = TREE_TYPE (ref);
2474     }
2475   else if (fun)
2476     /* Implicit function declaration.  */
2477     ref = implicitly_declare (loc, id);
2478   else if (decl == error_mark_node)
2479     /* Don't complain about something that's already been
2480        complained about.  */
2481     return error_mark_node;
2482   else
2483     {
2484       undeclared_variable (loc, id);
2485       return error_mark_node;
2486     }
2487 
2488   if (TREE_TYPE (ref) == error_mark_node)
2489     return error_mark_node;
2490 
2491   if (TREE_DEPRECATED (ref))
2492     warn_deprecated_use (ref, NULL_TREE);
2493 
2494   /* Recursive call does not count as usage.  */
2495   if (ref != current_function_decl)
2496     {
2497       TREE_USED (ref) = 1;
2498     }
2499 
2500   if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2501     {
2502       if (!in_sizeof && !in_typeof)
2503           C_DECL_USED (ref) = 1;
2504       else if (DECL_INITIAL (ref) == 0
2505                  && DECL_EXTERNAL (ref)
2506                  && !TREE_PUBLIC (ref))
2507           record_maybe_used_decl (ref);
2508     }
2509 
2510   if (TREE_CODE (ref) == CONST_DECL)
2511     {
2512       used_types_insert (TREE_TYPE (ref));
2513 
2514       if (warn_cxx_compat
2515             && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2516             && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2517           {
2518             warning_at (loc, OPT_Wc___compat,
2519                           ("enum constant defined in struct or union "
2520                            "is not visible in C++"));
2521             inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2522           }
2523 
2524       ref = DECL_INITIAL (ref);
2525       TREE_CONSTANT (ref) = 1;
2526     }
2527   else if (current_function_decl != 0
2528              && !DECL_FILE_SCOPE_P (current_function_decl)
2529              && (TREE_CODE (ref) == VAR_DECL
2530                  || TREE_CODE (ref) == PARM_DECL
2531                  || TREE_CODE (ref) == FUNCTION_DECL))
2532     {
2533       tree context = decl_function_context (ref);
2534 
2535       if (context != 0 && context != current_function_decl)
2536           DECL_NONLOCAL (ref) = 1;
2537     }
2538   /* C99 6.7.4p3: An inline definition of a function with external
2539      linkage ... shall not contain a reference to an identifier with
2540      internal linkage.  */
2541   else if (current_function_decl != 0
2542              && DECL_DECLARED_INLINE_P (current_function_decl)
2543              && DECL_EXTERNAL (current_function_decl)
2544              && VAR_OR_FUNCTION_DECL_P (ref)
2545              && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
2546              && ! TREE_PUBLIC (ref)
2547              && DECL_CONTEXT (ref) != current_function_decl)
2548     record_inline_static (loc, current_function_decl, ref,
2549                                 csi_internal);
2550 
2551   return ref;
2552 }
2553 
2554 /* Record details of decls possibly used inside sizeof or typeof.  */
2555 struct maybe_used_decl
2556 {
2557   /* The decl.  */
2558   tree decl;
2559   /* The level seen at (in_sizeof + in_typeof).  */
2560   int level;
2561   /* The next one at this level or above, or NULL.  */
2562   struct maybe_used_decl *next;
2563 };
2564 
2565 static struct maybe_used_decl *maybe_used_decls;
2566 
2567 /* Record that DECL, an undefined static function reference seen
2568    inside sizeof or typeof, might be used if the operand of sizeof is
2569    a VLA type or the operand of typeof is a variably modified
2570    type.  */
2571 
2572 static void
record_maybe_used_decl(tree decl)2573 record_maybe_used_decl (tree decl)
2574 {
2575   struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2576   t->decl = decl;
2577   t->level = in_sizeof + in_typeof;
2578   t->next = maybe_used_decls;
2579   maybe_used_decls = t;
2580 }
2581 
2582 /* Pop the stack of decls possibly used inside sizeof or typeof.  If
2583    USED is false, just discard them.  If it is true, mark them used
2584    (if no longer inside sizeof or typeof) or move them to the next
2585    level up (if still inside sizeof or typeof).  */
2586 
2587 void
pop_maybe_used(bool used)2588 pop_maybe_used (bool used)
2589 {
2590   struct maybe_used_decl *p = maybe_used_decls;
2591   int cur_level = in_sizeof + in_typeof;
2592   while (p && p->level > cur_level)
2593     {
2594       if (used)
2595           {
2596             if (cur_level == 0)
2597               C_DECL_USED (p->decl) = 1;
2598             else
2599               p->level = cur_level;
2600           }
2601       p = p->next;
2602     }
2603   if (!used || cur_level == 0)
2604     maybe_used_decls = p;
2605 }
2606 
2607 /* Return the result of sizeof applied to EXPR.  */
2608 
2609 struct c_expr
c_expr_sizeof_expr(location_t loc,struct c_expr expr)2610 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2611 {
2612   struct c_expr ret;
2613   if (expr.value == error_mark_node)
2614     {
2615       ret.value = error_mark_node;
2616       ret.original_code = ERROR_MARK;
2617       ret.original_type = NULL;
2618       pop_maybe_used (false);
2619     }
2620   else
2621     {
2622       bool expr_const_operands = true;
2623       tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2624                                                &expr_const_operands);
2625       ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2626       ret.original_code = ERROR_MARK;
2627       ret.original_type = NULL;
2628       if (c_vla_type_p (TREE_TYPE (folded_expr)))
2629           {
2630             /* sizeof is evaluated when given a vla (C99 6.5.3.4p2).  */
2631             ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2632                                     folded_expr, ret.value);
2633             C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2634             SET_EXPR_LOCATION (ret.value, loc);
2635           }
2636       pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2637     }
2638   return ret;
2639 }
2640 
2641 /* Return the result of sizeof applied to T, a structure for the type
2642    name passed to sizeof (rather than the type itself).  LOC is the
2643    location of the original expression.  */
2644 
2645 struct c_expr
c_expr_sizeof_type(location_t loc,struct c_type_name * t)2646 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2647 {
2648   tree type;
2649   struct c_expr ret;
2650   tree type_expr = NULL_TREE;
2651   bool type_expr_const = true;
2652   type = groktypename (t, &type_expr, &type_expr_const);
2653   ret.value = c_sizeof (loc, type);
2654   ret.original_code = ERROR_MARK;
2655   ret.original_type = NULL;
2656   if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2657       && c_vla_type_p (type))
2658     {
2659       /* If the type is a [*] array, it is a VLA but is represented as
2660            having a size of zero.  In such a case we must ensure that
2661            the result of sizeof does not get folded to a constant by
2662            c_fully_fold, because if the size is evaluated the result is
2663            not constant and so constraints on zero or negative size
2664            arrays must not be applied when this sizeof call is inside
2665            another array declarator.  */
2666       if (!type_expr)
2667           type_expr = integer_zero_node;
2668       ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2669                                 type_expr, ret.value);
2670       C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2671     }
2672   pop_maybe_used (type != error_mark_node
2673                       ? C_TYPE_VARIABLE_SIZE (type) : false);
2674   return ret;
2675 }
2676 
2677 /* Build a function call to function FUNCTION with parameters PARAMS.
2678    The function call is at LOC.
2679    PARAMS is a list--a chain of TREE_LIST nodes--in which the
2680    TREE_VALUE of each node is a parameter-expression.
2681    FUNCTION's data type may be a function type or a pointer-to-function.  */
2682 
2683 tree
build_function_call(location_t loc,tree function,tree params)2684 build_function_call (location_t loc, tree function, tree params)
2685 {
2686   VEC(tree,gc) *vec;
2687   tree ret;
2688 
2689   vec = VEC_alloc (tree, gc, list_length (params));
2690   for (; params; params = TREE_CHAIN (params))
2691     VEC_quick_push (tree, vec, TREE_VALUE (params));
2692   ret = build_function_call_vec (loc, function, vec, NULL);
2693   VEC_free (tree, gc, vec);
2694   return ret;
2695 }
2696 
2697 /* Build a function call to function FUNCTION with parameters PARAMS.
2698    ORIGTYPES, if not NULL, is a vector of types; each element is
2699    either NULL or the original type of the corresponding element in
2700    PARAMS.  The original type may differ from TREE_TYPE of the
2701    parameter for enums.  FUNCTION's data type may be a function type
2702    or pointer-to-function.  This function changes the elements of
2703    PARAMS.  */
2704 
2705 tree
build_function_call_vec(location_t loc,tree function,VEC (tree,gc)* params,VEC (tree,gc)* origtypes)2706 build_function_call_vec (location_t loc, tree function, VEC(tree,gc) *params,
2707                                VEC(tree,gc) *origtypes)
2708 {
2709   tree fntype, fundecl = 0;
2710   tree name = NULL_TREE, result;
2711   tree tem;
2712   int nargs;
2713   tree *argarray;
2714 
2715 
2716   /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue.  */
2717   STRIP_TYPE_NOPS (function);
2718 
2719   /* Convert anything with function type to a pointer-to-function.  */
2720   if (TREE_CODE (function) == FUNCTION_DECL)
2721     {
2722       /* Implement type-directed function overloading for builtins.
2723            resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2724            handle all the type checking.  The result is a complete expression
2725            that implements this function call.  */
2726       tem = resolve_overloaded_builtin (loc, function, params);
2727       if (tem)
2728           return tem;
2729 
2730       name = DECL_NAME (function);
2731 
2732       if (flag_tm)
2733           tm_malloc_replacement (function);
2734       fundecl = function;
2735       /* Atomic functions have type checking/casting already done.  They are
2736            often rewritten and don't match the original parameter list.  */
2737       if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
2738         origtypes = NULL;
2739     }
2740   if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2741     function = function_to_pointer_conversion (loc, function);
2742 
2743   /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2744      expressions, like those used for ObjC messenger dispatches.  */
2745   if (!VEC_empty (tree, params))
2746     function = objc_rewrite_function_call (function,
2747                                                      VEC_index (tree, params, 0));
2748 
2749   function = c_fully_fold (function, false, NULL);
2750 
2751   fntype = TREE_TYPE (function);
2752 
2753   if (TREE_CODE (fntype) == ERROR_MARK)
2754     return error_mark_node;
2755 
2756   if (!(TREE_CODE (fntype) == POINTER_TYPE
2757           && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2758     {
2759       error_at (loc, "called object %qE is not a function", function);
2760       return error_mark_node;
2761     }
2762 
2763   if (fundecl && TREE_THIS_VOLATILE (fundecl))
2764     current_function_returns_abnormally = 1;
2765 
2766   /* fntype now gets the type of function pointed to.  */
2767   fntype = TREE_TYPE (fntype);
2768 
2769   /* Convert the parameters to the types declared in the
2770      function prototype, or apply default promotions.  */
2771 
2772   nargs = convert_arguments (TYPE_ARG_TYPES (fntype), params, origtypes,
2773                                    function, fundecl);
2774   if (nargs < 0)
2775     return error_mark_node;
2776 
2777   /* Check that the function is called through a compatible prototype.
2778      If it is not, replace the call by a trap, wrapped up in a compound
2779      expression if necessary.  This has the nice side-effect to prevent
2780      the tree-inliner from generating invalid assignment trees which may
2781      blow up in the RTL expander later.  */
2782   if (CONVERT_EXPR_P (function)
2783       && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2784       && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2785       && !comptypes (fntype, TREE_TYPE (tem)))
2786     {
2787       tree return_type = TREE_TYPE (fntype);
2788       tree trap = build_function_call (loc,
2789                                                builtin_decl_explicit (BUILT_IN_TRAP),
2790                                                NULL_TREE);
2791       int i;
2792 
2793       /* This situation leads to run-time undefined behavior.  We can't,
2794            therefore, simply error unless we can prove that all possible
2795            executions of the program must execute the code.  */
2796       if (warning_at (loc, 0, "function called through a non-compatible type"))
2797           /* We can, however, treat "undefined" any way we please.
2798              Call abort to encourage the user to fix the program.  */
2799           inform (loc, "if this code is reached, the program will abort");
2800       /* Before the abort, allow the function arguments to exit or
2801            call longjmp.  */
2802       for (i = 0; i < nargs; i++)
2803           trap = build2 (COMPOUND_EXPR, void_type_node,
2804                            VEC_index (tree, params, i), trap);
2805 
2806       if (VOID_TYPE_P (return_type))
2807           {
2808             if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2809               pedwarn (loc, 0,
2810                          "function with qualified void return type called");
2811             return trap;
2812           }
2813       else
2814           {
2815             tree rhs;
2816 
2817             if (AGGREGATE_TYPE_P (return_type))
2818               rhs = build_compound_literal (loc, return_type,
2819                                                     build_constructor (return_type, 0),
2820                                                     false);
2821             else
2822               rhs = build_zero_cst (return_type);
2823 
2824             return require_complete_type (build2 (COMPOUND_EXPR, return_type,
2825                                                             trap, rhs));
2826           }
2827     }
2828 
2829   argarray = VEC_address (tree, params);
2830 
2831   /* Check that arguments to builtin functions match the expectations.  */
2832   if (fundecl
2833       && DECL_BUILT_IN (fundecl)
2834       && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2835       && !check_builtin_function_arguments (fundecl, nargs, argarray))
2836     return error_mark_node;
2837 
2838   /* Check that the arguments to the function are valid.  */
2839   check_function_arguments (fntype, nargs, argarray);
2840 
2841   if (name != NULL_TREE
2842       && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
2843     {
2844       if (require_constant_value)
2845           result =
2846             fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
2847                                                              function, nargs, argarray);
2848       else
2849           result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
2850                                                       function, nargs, argarray);
2851       if (TREE_CODE (result) == NOP_EXPR
2852             && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2853           STRIP_TYPE_NOPS (result);
2854     }
2855   else
2856     result = build_call_array_loc (loc, TREE_TYPE (fntype),
2857                                            function, nargs, argarray);
2858 
2859   if (VOID_TYPE_P (TREE_TYPE (result)))
2860     {
2861       if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
2862           pedwarn (loc, 0,
2863                      "function with qualified void return type called");
2864       return result;
2865     }
2866   return require_complete_type (result);
2867 }
2868 
2869 /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2870    and have vector types, V0 has the same type as V1, and the number of
2871    elements of V0, V1, MASK is the same.
2872 
2873    In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2874    called with two arguments.  In this case implementation passes the
2875    first argument twice in order to share the same tree code.  This fact
2876    could enable the mask-values being twice the vector length.  This is
2877    an implementation accident and this semantics is not guaranteed to
2878    the user.  */
2879 tree
c_build_vec_perm_expr(location_t loc,tree v0,tree v1,tree mask)2880 c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask)
2881 {
2882   tree ret;
2883   bool wrap = true;
2884   bool maybe_const = false;
2885   bool two_arguments = false;
2886 
2887   if (v1 == NULL_TREE)
2888     {
2889       two_arguments = true;
2890       v1 = v0;
2891     }
2892 
2893   if (v0 == error_mark_node || v1 == error_mark_node
2894       || mask == error_mark_node)
2895     return error_mark_node;
2896 
2897   if (TREE_CODE (TREE_TYPE (mask)) != VECTOR_TYPE
2898       || TREE_CODE (TREE_TYPE (TREE_TYPE (mask))) != INTEGER_TYPE)
2899     {
2900       error_at (loc, "__builtin_shuffle last argument must "
2901                          "be an integer vector");
2902       return error_mark_node;
2903     }
2904 
2905   if (TREE_CODE (TREE_TYPE (v0)) != VECTOR_TYPE
2906       || TREE_CODE (TREE_TYPE (v1)) != VECTOR_TYPE)
2907     {
2908       error_at (loc, "__builtin_shuffle arguments must be vectors");
2909       return error_mark_node;
2910     }
2911 
2912   if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2913     {
2914       error_at (loc, "__builtin_shuffle argument vectors must be of "
2915                          "the same type");
2916       return error_mark_node;
2917     }
2918 
2919   if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2920       != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2921       && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2922            != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2923     {
2924       error_at (loc, "__builtin_shuffle number of elements of the "
2925                          "argument vector(s) and the mask vector should "
2926                          "be the same");
2927       return error_mark_node;
2928     }
2929 
2930   if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2931       != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2932     {
2933       error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2934                          "must have the same size as inner type of the mask");
2935       return error_mark_node;
2936     }
2937 
2938   /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR.  */
2939   v0 = c_fully_fold (v0, false, &maybe_const);
2940   wrap &= maybe_const;
2941 
2942   if (two_arguments)
2943     v1 = v0 = save_expr (v0);
2944   else
2945     {
2946       v1 = c_fully_fold (v1, false, &maybe_const);
2947       wrap &= maybe_const;
2948     }
2949 
2950   mask = c_fully_fold (mask, false, &maybe_const);
2951   wrap &= maybe_const;
2952 
2953   ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2954 
2955   if (!wrap)
2956     ret = c_wrap_maybe_const (ret, true);
2957 
2958   return ret;
2959 }
2960 
2961 /* Convert the argument expressions in the vector VALUES
2962    to the types in the list TYPELIST.
2963 
2964    If TYPELIST is exhausted, or when an element has NULL as its type,
2965    perform the default conversions.
2966 
2967    ORIGTYPES is the original types of the expressions in VALUES.  This
2968    holds the type of enum values which have been converted to integral
2969    types.  It may be NULL.
2970 
2971    FUNCTION is a tree for the called function.  It is used only for
2972    error messages, where it is formatted with %qE.
2973 
2974    This is also where warnings about wrong number of args are generated.
2975 
2976    Returns the actual number of arguments processed (which may be less
2977    than the length of VALUES in some error situations), or -1 on
2978    failure.  */
2979 
2980 static int
convert_arguments(tree typelist,VEC (tree,gc)* values,VEC (tree,gc)* origtypes,tree function,tree fundecl)2981 convert_arguments (tree typelist, VEC(tree,gc) *values,
2982                        VEC(tree,gc) *origtypes, tree function, tree fundecl)
2983 {
2984   tree typetail, val;
2985   unsigned int parmnum;
2986   bool error_args = false;
2987   const bool type_generic = fundecl
2988     && lookup_attribute ("type generic", TYPE_ATTRIBUTES(TREE_TYPE (fundecl)));
2989   bool type_generic_remove_excess_precision = false;
2990   tree selector;
2991 
2992   /* Change pointer to function to the function itself for
2993      diagnostics.  */
2994   if (TREE_CODE (function) == ADDR_EXPR
2995       && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2996     function = TREE_OPERAND (function, 0);
2997 
2998   /* Handle an ObjC selector specially for diagnostics.  */
2999   selector = objc_message_selector ();
3000 
3001   /* For type-generic built-in functions, determine whether excess
3002      precision should be removed (classification) or not
3003      (comparison).  */
3004   if (type_generic
3005       && DECL_BUILT_IN (fundecl)
3006       && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3007     {
3008       switch (DECL_FUNCTION_CODE (fundecl))
3009           {
3010           case BUILT_IN_ISFINITE:
3011           case BUILT_IN_ISINF:
3012           case BUILT_IN_ISINF_SIGN:
3013           case BUILT_IN_ISNAN:
3014           case BUILT_IN_ISNORMAL:
3015           case BUILT_IN_FPCLASSIFY:
3016             type_generic_remove_excess_precision = true;
3017             break;
3018 
3019           default:
3020             type_generic_remove_excess_precision = false;
3021             break;
3022           }
3023     }
3024 
3025   /* Scan the given expressions and types, producing individual
3026      converted arguments.  */
3027 
3028   for (typetail = typelist, parmnum = 0;
3029        VEC_iterate (tree, values, parmnum, val);
3030        ++parmnum)
3031     {
3032       tree type = typetail ? TREE_VALUE (typetail) : 0;
3033       tree valtype = TREE_TYPE (val);
3034       tree rname = function;
3035       int argnum = parmnum + 1;
3036       const char *invalid_func_diag;
3037       bool excess_precision = false;
3038       bool npc;
3039       tree parmval;
3040 
3041       if (type == void_type_node)
3042           {
3043             if (selector)
3044               error_at (input_location,
3045                           "too many arguments to method %qE", selector);
3046             else
3047               error_at (input_location,
3048                           "too many arguments to function %qE", function);
3049 
3050             if (fundecl && !DECL_BUILT_IN (fundecl))
3051               inform (DECL_SOURCE_LOCATION (fundecl), "declared here");
3052             return parmnum;
3053           }
3054 
3055       if (selector && argnum > 2)
3056           {
3057             rname = selector;
3058             argnum -= 2;
3059           }
3060 
3061       npc = null_pointer_constant_p (val);
3062 
3063       /* If there is excess precision and a prototype, convert once to
3064            the required type rather than converting via the semantic
3065            type.  Likewise without a prototype a float value represented
3066            as long double should be converted once to double.  But for
3067            type-generic classification functions excess precision must
3068            be removed here.  */
3069       if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3070             && (type || !type_generic || !type_generic_remove_excess_precision))
3071           {
3072             val = TREE_OPERAND (val, 0);
3073             excess_precision = true;
3074           }
3075       val = c_fully_fold (val, false, NULL);
3076       STRIP_TYPE_NOPS (val);
3077 
3078       val = require_complete_type (val);
3079 
3080       if (type != 0)
3081           {
3082             /* Formal parm type is specified by a function prototype.  */
3083 
3084             if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3085               {
3086                 error ("type of formal parameter %d is incomplete", parmnum + 1);
3087                 parmval = val;
3088               }
3089             else
3090               {
3091                 tree origtype;
3092 
3093                 /* Optionally warn about conversions that
3094                      differ from the default conversions.  */
3095                 if (warn_traditional_conversion || warn_traditional)
3096                     {
3097                       unsigned int formal_prec = TYPE_PRECISION (type);
3098 
3099                       if (INTEGRAL_TYPE_P (type)
3100                           && TREE_CODE (valtype) == REAL_TYPE)
3101                         warning (0, "passing argument %d of %qE as integer "
3102                                    "rather than floating due to prototype",
3103                                    argnum, rname);
3104                       if (INTEGRAL_TYPE_P (type)
3105                           && TREE_CODE (valtype) == COMPLEX_TYPE)
3106                         warning (0, "passing argument %d of %qE as integer "
3107                                    "rather than complex due to prototype",
3108                                    argnum, rname);
3109                       else if (TREE_CODE (type) == COMPLEX_TYPE
3110                                  && TREE_CODE (valtype) == REAL_TYPE)
3111                         warning (0, "passing argument %d of %qE as complex "
3112                                    "rather than floating due to prototype",
3113                                    argnum, rname);
3114                       else if (TREE_CODE (type) == REAL_TYPE
3115                                  && INTEGRAL_TYPE_P (valtype))
3116                         warning (0, "passing argument %d of %qE as floating "
3117                                    "rather than integer due to prototype",
3118                                    argnum, rname);
3119                       else if (TREE_CODE (type) == COMPLEX_TYPE
3120                                  && INTEGRAL_TYPE_P (valtype))
3121                         warning (0, "passing argument %d of %qE as complex "
3122                                    "rather than integer due to prototype",
3123                                    argnum, rname);
3124                       else if (TREE_CODE (type) == REAL_TYPE
3125                                  && TREE_CODE (valtype) == COMPLEX_TYPE)
3126                         warning (0, "passing argument %d of %qE as floating "
3127                                    "rather than complex due to prototype",
3128                                    argnum, rname);
3129                       /* ??? At some point, messages should be written about
3130                          conversions between complex types, but that's too messy
3131                          to do now.  */
3132                       else if (TREE_CODE (type) == REAL_TYPE
3133                                  && TREE_CODE (valtype) == REAL_TYPE)
3134                         {
3135                           /* Warn if any argument is passed as `float',
3136                                since without a prototype it would be `double'.  */
3137                           if (formal_prec == TYPE_PRECISION (float_type_node)
3138                                 && type != dfloat32_type_node)
3139                               warning (0, "passing argument %d of %qE as %<float%> "
3140                                          "rather than %<double%> due to prototype",
3141                                          argnum, rname);
3142 
3143                           /* Warn if mismatch between argument and prototype
3144                                for decimal float types.  Warn of conversions with
3145                                binary float types and of precision narrowing due to
3146                                prototype. */
3147                           else if (type != valtype
3148                                      && (type == dfloat32_type_node
3149                                            || type == dfloat64_type_node
3150                                            || type == dfloat128_type_node
3151                                            || valtype == dfloat32_type_node
3152                                            || valtype == dfloat64_type_node
3153                                            || valtype == dfloat128_type_node)
3154                                      && (formal_prec
3155                                            <= TYPE_PRECISION (valtype)
3156                                            || (type == dfloat128_type_node
3157                                                && (valtype
3158                                                      != dfloat64_type_node
3159                                                      && (valtype
3160                                                          != dfloat32_type_node)))
3161                                            || (type == dfloat64_type_node
3162                                                && (valtype
3163                                                      != dfloat32_type_node))))
3164                               warning (0, "passing argument %d of %qE as %qT "
3165                                          "rather than %qT due to prototype",
3166                                          argnum, rname, type, valtype);
3167 
3168                         }
3169                       /* Detect integer changing in width or signedness.
3170                          These warnings are only activated with
3171                          -Wtraditional-conversion, not with -Wtraditional.  */
3172                       else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
3173                                  && INTEGRAL_TYPE_P (valtype))
3174                         {
3175                           tree would_have_been = default_conversion (val);
3176                           tree type1 = TREE_TYPE (would_have_been);
3177 
3178                           if (TREE_CODE (type) == ENUMERAL_TYPE
3179                                 && (TYPE_MAIN_VARIANT (type)
3180                                     == TYPE_MAIN_VARIANT (valtype)))
3181                               /* No warning if function asks for enum
3182                                  and the actual arg is that enum type.  */
3183                               ;
3184                           else if (formal_prec != TYPE_PRECISION (type1))
3185                               warning (OPT_Wtraditional_conversion,
3186                                          "passing argument %d of %qE "
3187                                          "with different width due to prototype",
3188                                          argnum, rname);
3189                           else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3190                               ;
3191                           /* Don't complain if the formal parameter type
3192                                is an enum, because we can't tell now whether
3193                                the value was an enum--even the same enum.  */
3194                           else if (TREE_CODE (type) == ENUMERAL_TYPE)
3195                               ;
3196                           else if (TREE_CODE (val) == INTEGER_CST
3197                                      && int_fits_type_p (val, type))
3198                               /* Change in signedness doesn't matter
3199                                  if a constant value is unaffected.  */
3200                               ;
3201                           /* If the value is extended from a narrower
3202                                unsigned type, it doesn't matter whether we
3203                                pass it as signed or unsigned; the value
3204                                certainly is the same either way.  */
3205                           else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3206                                      && TYPE_UNSIGNED (valtype))
3207                               ;
3208                           else if (TYPE_UNSIGNED (type))
3209                               warning (OPT_Wtraditional_conversion,
3210                                          "passing argument %d of %qE "
3211                                          "as unsigned due to prototype",
3212                                          argnum, rname);
3213                           else
3214                               warning (OPT_Wtraditional_conversion,
3215                                          "passing argument %d of %qE "
3216                                          "as signed due to prototype", argnum, rname);
3217                         }
3218                     }
3219 
3220                 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3221                      sake of better warnings from convert_and_check.  */
3222                 if (excess_precision)
3223                     val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3224                 origtype = (origtypes == NULL
3225                                 ? NULL_TREE
3226                                 : VEC_index (tree, origtypes, parmnum));
3227                 parmval = convert_for_assignment (input_location, type, val,
3228                                                             origtype, ic_argpass, npc,
3229                                                             fundecl, function,
3230                                                             parmnum + 1);
3231 
3232                 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3233                       && INTEGRAL_TYPE_P (type)
3234                       && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3235                     parmval = default_conversion (parmval);
3236               }
3237           }
3238       else if (TREE_CODE (valtype) == REAL_TYPE
3239                  && (TYPE_PRECISION (valtype)
3240                        < TYPE_PRECISION (double_type_node))
3241                  && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3242         {
3243             if (type_generic)
3244               parmval = val;
3245             else
3246               {
3247                 /* Convert `float' to `double'.  */
3248                 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3249                     warning (OPT_Wdouble_promotion,
3250                                "implicit conversion from %qT to %qT when passing "
3251                                "argument to function",
3252                                valtype, double_type_node);
3253                 parmval = convert (double_type_node, val);
3254               }
3255           }
3256       else if (excess_precision && !type_generic)
3257           /* A "double" argument with excess precision being passed
3258              without a prototype or in variable arguments.  */
3259           parmval = convert (valtype, val);
3260       else if ((invalid_func_diag =
3261                     targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3262           {
3263             error (invalid_func_diag);
3264             return -1;
3265           }
3266       else
3267           /* Convert `short' and `char' to full-size `int'.  */
3268           parmval = default_conversion (val);
3269 
3270       VEC_replace (tree, values, parmnum, parmval);
3271       if (parmval == error_mark_node)
3272           error_args = true;
3273 
3274       if (typetail)
3275           typetail = TREE_CHAIN (typetail);
3276     }
3277 
3278   gcc_assert (parmnum == VEC_length (tree, values));
3279 
3280   if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3281     {
3282       error_at (input_location,
3283                     "too few arguments to function %qE", function);
3284       if (fundecl && !DECL_BUILT_IN (fundecl))
3285           inform (DECL_SOURCE_LOCATION (fundecl), "declared here");
3286       return -1;
3287     }
3288 
3289   return error_args ? -1 : (int) parmnum;
3290 }
3291 
3292 /* This is the entry point used by the parser to build unary operators
3293    in the input.  CODE, a tree_code, specifies the unary operator, and
3294    ARG is the operand.  For unary plus, the C parser currently uses
3295    CONVERT_EXPR for code.
3296 
3297    LOC is the location to use for the tree generated.
3298 */
3299 
3300 struct c_expr
parser_build_unary_op(location_t loc,enum tree_code code,struct c_expr arg)3301 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3302 {
3303   struct c_expr result;
3304 
3305   result.value = build_unary_op (loc, code, arg.value, 0);
3306   result.original_code = code;
3307   result.original_type = NULL;
3308 
3309   if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3310     overflow_warning (loc, result.value);
3311 
3312   return result;
3313 }
3314 
3315 /* This is the entry point used by the parser to build binary operators
3316    in the input.  CODE, a tree_code, specifies the binary operator, and
3317    ARG1 and ARG2 are the operands.  In addition to constructing the
3318    expression, we check for operands that were written with other binary
3319    operators in a way that is likely to confuse the user.
3320 
3321    LOCATION is the location of the binary operator.  */
3322 
3323 struct c_expr
parser_build_binary_op(location_t location,enum tree_code code,struct c_expr arg1,struct c_expr arg2)3324 parser_build_binary_op (location_t location, enum tree_code code,
3325                               struct c_expr arg1, struct c_expr arg2)
3326 {
3327   struct c_expr result;
3328 
3329   enum tree_code code1 = arg1.original_code;
3330   enum tree_code code2 = arg2.original_code;
3331   tree type1 = (arg1.original_type
3332                 ? arg1.original_type
3333                 : TREE_TYPE (arg1.value));
3334   tree type2 = (arg2.original_type
3335                 ? arg2.original_type
3336                 : TREE_TYPE (arg2.value));
3337 
3338   result.value = build_binary_op (location, code,
3339                                           arg1.value, arg2.value, 1);
3340   result.original_code = code;
3341   result.original_type = NULL;
3342 
3343   if (TREE_CODE (result.value) == ERROR_MARK)
3344     return result;
3345 
3346   if (location != UNKNOWN_LOCATION)
3347     protected_set_expr_location (result.value, location);
3348 
3349   /* Check for cases such as x+y<<z which users are likely
3350      to misinterpret.  */
3351   if (warn_parentheses)
3352     warn_about_parentheses (code, code1, arg1.value, code2, arg2.value);
3353 
3354   if (warn_logical_op)
3355     warn_logical_operator (input_location, code, TREE_TYPE (result.value),
3356                                  code1, arg1.value, code2, arg2.value);
3357 
3358   /* Warn about comparisons against string literals, with the exception
3359      of testing for equality or inequality of a string literal with NULL.  */
3360   if (code == EQ_EXPR || code == NE_EXPR)
3361     {
3362       if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3363             || (code2 == STRING_CST && !integer_zerop (arg1.value)))
3364           warning_at (location, OPT_Waddress,
3365                         "comparison with string literal results in unspecified behavior");
3366     }
3367   else if (TREE_CODE_CLASS (code) == tcc_comparison
3368              && (code1 == STRING_CST || code2 == STRING_CST))
3369     warning_at (location, OPT_Waddress,
3370                     "comparison with string literal results in unspecified behavior");
3371 
3372   if (TREE_OVERFLOW_P (result.value)
3373       && !TREE_OVERFLOW_P (arg1.value)
3374       && !TREE_OVERFLOW_P (arg2.value))
3375     overflow_warning (location, result.value);
3376 
3377   /* Warn about comparisons of different enum types.  */
3378   if (warn_enum_compare
3379       && TREE_CODE_CLASS (code) == tcc_comparison
3380       && TREE_CODE (type1) == ENUMERAL_TYPE
3381       && TREE_CODE (type2) == ENUMERAL_TYPE
3382       && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3383     warning_at (location, OPT_Wenum_compare,
3384                     "comparison between %qT and %qT",
3385                     type1, type2);
3386 
3387   return result;
3388 }
3389 
3390 /* Return a tree for the difference of pointers OP0 and OP1.
3391    The resulting tree has type int.  */
3392 
3393 static tree
pointer_diff(location_t loc,tree op0,tree op1)3394 pointer_diff (location_t loc, tree op0, tree op1)
3395 {
3396   tree restype = ptrdiff_type_node;
3397   tree result, inttype;
3398 
3399   addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3400   addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3401   tree target_type = TREE_TYPE (TREE_TYPE (op0));
3402   tree con0, con1, lit0, lit1;
3403   tree orig_op1 = op1;
3404 
3405   /* If the operands point into different address spaces, we need to
3406      explicitly convert them to pointers into the common address space
3407      before we can subtract the numerical address values.  */
3408   if (as0 != as1)
3409     {
3410       addr_space_t as_common;
3411       tree common_type;
3412 
3413       /* Determine the common superset address space.  This is guaranteed
3414            to exist because the caller verified that comp_target_types
3415            returned non-zero.  */
3416       if (!addr_space_superset (as0, as1, &as_common))
3417           gcc_unreachable ();
3418 
3419       common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3420       op0 = convert (common_type, op0);
3421       op1 = convert (common_type, op1);
3422     }
3423 
3424   /* Determine integer type to perform computations in.  This will usually
3425      be the same as the result type (ptrdiff_t), but may need to be a wider
3426      type if pointers for the address space are wider than ptrdiff_t.  */
3427   if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3428     inttype = lang_hooks.types.type_for_size
3429                     (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3430   else
3431     inttype = restype;
3432 
3433 
3434   if (TREE_CODE (target_type) == VOID_TYPE)
3435     pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3436                "pointer of type %<void *%> used in subtraction");
3437   if (TREE_CODE (target_type) == FUNCTION_TYPE)
3438     pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3439                "pointer to a function used in subtraction");
3440 
3441   /* If the conversion to ptrdiff_type does anything like widening or
3442      converting a partial to an integral mode, we get a convert_expression
3443      that is in the way to do any simplifications.
3444      (fold-const.c doesn't know that the extra bits won't be needed.
3445      split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
3446      different mode in place.)
3447      So first try to find a common term here 'by hand'; we want to cover
3448      at least the cases that occur in legal static initializers.  */
3449   if (CONVERT_EXPR_P (op0)
3450       && (TYPE_PRECISION (TREE_TYPE (op0))
3451             == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
3452     con0 = TREE_OPERAND (op0, 0);
3453   else
3454     con0 = op0;
3455   if (CONVERT_EXPR_P (op1)
3456       && (TYPE_PRECISION (TREE_TYPE (op1))
3457             == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))))
3458     con1 = TREE_OPERAND (op1, 0);
3459   else
3460     con1 = op1;
3461 
3462   if (TREE_CODE (con0) == PLUS_EXPR)
3463     {
3464       lit0 = TREE_OPERAND (con0, 1);
3465       con0 = TREE_OPERAND (con0, 0);
3466     }
3467   else
3468     lit0 = integer_zero_node;
3469 
3470   if (TREE_CODE (con1) == PLUS_EXPR)
3471     {
3472       lit1 = TREE_OPERAND (con1, 1);
3473       con1 = TREE_OPERAND (con1, 0);
3474     }
3475   else
3476     lit1 = integer_zero_node;
3477 
3478   if (operand_equal_p (con0, con1, 0))
3479     {
3480       op0 = lit0;
3481       op1 = lit1;
3482     }
3483 
3484 
3485   /* First do the subtraction as integers;
3486      then drop through to build the divide operator.
3487      Do not do default conversions on the minus operator
3488      in case restype is a short type.  */
3489 
3490   op0 = build_binary_op (loc,
3491                                MINUS_EXPR, convert (inttype, op0),
3492                                convert (inttype, op1), 0);
3493   /* This generates an error if op1 is pointer to incomplete type.  */
3494   if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3495     error_at (loc, "arithmetic on pointer to an incomplete type");
3496 
3497   /* This generates an error if op0 is pointer to incomplete type.  */
3498   op1 = c_size_in_bytes (target_type);
3499 
3500   /* Divide by the size, in easiest possible way.  */
3501   result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3502                                   op0, convert (inttype, op1));
3503 
3504   /* Convert to final result type if necessary.  */
3505   return convert (restype, result);
3506 }
3507 
3508 /* Construct and perhaps optimize a tree representation
3509    for a unary operation.  CODE, a tree_code, specifies the operation
3510    and XARG is the operand.
3511    For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3512    the default promotions (such as from short to int).
3513    For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3514    allows non-lvalues; this is only used to handle conversion of non-lvalue
3515    arrays to pointers in C99.
3516 
3517    LOCATION is the location of the operator.  */
3518 
3519 tree
build_unary_op(location_t location,enum tree_code code,tree xarg,int flag)3520 build_unary_op (location_t location,
3521                     enum tree_code code, tree xarg, int flag)
3522 {
3523   /* No default_conversion here.  It causes trouble for ADDR_EXPR.  */
3524   tree arg = xarg;
3525   tree argtype = 0;
3526   enum tree_code typecode;
3527   tree val;
3528   tree ret = error_mark_node;
3529   tree eptype = NULL_TREE;
3530   int noconvert = flag;
3531   const char *invalid_op_diag;
3532   bool int_operands;
3533 
3534   int_operands = EXPR_INT_CONST_OPERANDS (xarg);
3535   if (int_operands)
3536     arg = remove_c_maybe_const_expr (arg);
3537 
3538   if (code != ADDR_EXPR)
3539     arg = require_complete_type (arg);
3540 
3541   typecode = TREE_CODE (TREE_TYPE (arg));
3542   if (typecode == ERROR_MARK)
3543     return error_mark_node;
3544   if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3545     typecode = INTEGER_TYPE;
3546 
3547   if ((invalid_op_diag
3548        = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3549     {
3550       error_at (location, invalid_op_diag);
3551       return error_mark_node;
3552     }
3553 
3554   if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3555     {
3556       eptype = TREE_TYPE (arg);
3557       arg = TREE_OPERAND (arg, 0);
3558     }
3559 
3560   switch (code)
3561     {
3562     case CONVERT_EXPR:
3563       /* This is used for unary plus, because a CONVERT_EXPR
3564            is enough to prevent anybody from looking inside for
3565            associativity, but won't generate any code.  */
3566       if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3567               || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3568               || typecode == VECTOR_TYPE))
3569           {
3570             error_at (location, "wrong type argument to unary plus");
3571             return error_mark_node;
3572           }
3573       else if (!noconvert)
3574           arg = default_conversion (arg);
3575       arg = non_lvalue_loc (location, arg);
3576       break;
3577 
3578     case NEGATE_EXPR:
3579       if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3580               || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3581               || typecode == VECTOR_TYPE))
3582           {
3583             error_at (location, "wrong type argument to unary minus");
3584             return error_mark_node;
3585           }
3586       else if (!noconvert)
3587           arg = default_conversion (arg);
3588       break;
3589 
3590     case BIT_NOT_EXPR:
3591       /* ~ works on integer types and non float vectors. */
3592       if (typecode == INTEGER_TYPE
3593             || (typecode == VECTOR_TYPE
3594                 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
3595           {
3596             if (!noconvert)
3597               arg = default_conversion (arg);
3598           }
3599       else if (typecode == COMPLEX_TYPE)
3600           {
3601             code = CONJ_EXPR;
3602             pedwarn (location, OPT_pedantic,
3603                        "ISO C does not support %<~%> for complex conjugation");
3604             if (!noconvert)
3605               arg = default_conversion (arg);
3606           }
3607       else
3608           {
3609             error_at (location, "wrong type argument to bit-complement");
3610             return error_mark_node;
3611           }
3612       break;
3613 
3614     case ABS_EXPR:
3615       if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
3616           {
3617             error_at (location, "wrong type argument to abs");
3618             return error_mark_node;
3619           }
3620       else if (!noconvert)
3621           arg = default_conversion (arg);
3622       break;
3623 
3624     case CONJ_EXPR:
3625       /* Conjugating a real value is a no-op, but allow it anyway.  */
3626       if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3627               || typecode == COMPLEX_TYPE))
3628           {
3629             error_at (location, "wrong type argument to conjugation");
3630             return error_mark_node;
3631           }
3632       else if (!noconvert)
3633           arg = default_conversion (arg);
3634       break;
3635 
3636     case TRUTH_NOT_EXPR:
3637       if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3638             && typecode != REAL_TYPE && typecode != POINTER_TYPE
3639             && typecode != COMPLEX_TYPE)
3640           {
3641             error_at (location,
3642                         "wrong type argument to unary exclamation mark");
3643             return error_mark_node;
3644           }
3645       if (int_operands)
3646           {
3647             arg = c_objc_common_truthvalue_conversion (location, xarg);
3648             arg = remove_c_maybe_const_expr (arg);
3649           }
3650       else
3651           arg = c_objc_common_truthvalue_conversion (location, arg);
3652       ret = invert_truthvalue_loc (location, arg);
3653       /* If the TRUTH_NOT_EXPR has been folded, reset the location.  */
3654       if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3655           location = EXPR_LOCATION (ret);
3656       goto return_build_unary_op;
3657 
3658     case REALPART_EXPR:
3659     case IMAGPART_EXPR:
3660       ret = build_real_imag_expr (location, code, arg);
3661       if (ret == error_mark_node)
3662           return error_mark_node;
3663       if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3664           eptype = TREE_TYPE (eptype);
3665       goto return_build_unary_op;
3666 
3667     case PREINCREMENT_EXPR:
3668     case POSTINCREMENT_EXPR:
3669     case PREDECREMENT_EXPR:
3670     case POSTDECREMENT_EXPR:
3671 
3672       if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3673           {
3674             tree inner = build_unary_op (location, code,
3675                                                C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3676             if (inner == error_mark_node)
3677               return error_mark_node;
3678             ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3679                               C_MAYBE_CONST_EXPR_PRE (arg), inner);
3680             gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3681             C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3682             goto return_build_unary_op;
3683           }
3684 
3685       /* Complain about anything that is not a true lvalue.  In
3686            Objective-C, skip this check for property_refs.  */
3687       if (!objc_is_property_ref (arg)
3688             && !lvalue_or_else (location,
3689                                     arg, ((code == PREINCREMENT_EXPR
3690                                              || code == POSTINCREMENT_EXPR)
3691                                             ? lv_increment
3692                                             : lv_decrement)))
3693           return error_mark_node;
3694 
3695       if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3696           {
3697             if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3698               warning_at (location, OPT_Wc___compat,
3699                               "increment of enumeration value is invalid in C++");
3700             else
3701               warning_at (location, OPT_Wc___compat,
3702                               "decrement of enumeration value is invalid in C++");
3703           }
3704 
3705       /* Ensure the argument is fully folded inside any SAVE_EXPR.  */
3706       arg = c_fully_fold (arg, false, NULL);
3707 
3708       /* Increment or decrement the real part of the value,
3709            and don't change the imaginary part.  */
3710       if (typecode == COMPLEX_TYPE)
3711           {
3712             tree real, imag;
3713 
3714             pedwarn (location, OPT_pedantic,
3715                        "ISO C does not support %<++%> and %<--%> on complex types");
3716 
3717             arg = stabilize_reference (arg);
3718             real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
3719             imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
3720             real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
3721             if (real == error_mark_node || imag == error_mark_node)
3722               return error_mark_node;
3723             ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
3724                               real, imag);
3725             goto return_build_unary_op;
3726           }
3727 
3728       /* Report invalid types.  */
3729 
3730       if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
3731             && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
3732           {
3733             if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3734               error_at (location, "wrong type argument to increment");
3735             else
3736               error_at (location, "wrong type argument to decrement");
3737 
3738             return error_mark_node;
3739           }
3740 
3741       {
3742           tree inc;
3743 
3744           argtype = TREE_TYPE (arg);
3745 
3746           /* Compute the increment.  */
3747 
3748           if (typecode == POINTER_TYPE)
3749             {
3750               /* If pointer target is an undefined struct,
3751                  we just cannot know how to do the arithmetic.  */
3752               if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3753                 {
3754                     if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3755                       error_at (location,
3756                                   "increment of pointer to unknown structure");
3757                     else
3758                       error_at (location,
3759                                   "decrement of pointer to unknown structure");
3760                 }
3761               else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
3762                          || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
3763                 {
3764                     if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3765                       pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3766                                  "wrong type argument to increment");
3767                     else
3768                       pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3769                                  "wrong type argument to decrement");
3770                 }
3771 
3772               inc = c_size_in_bytes (TREE_TYPE (argtype));
3773               inc = convert_to_ptrofftype_loc (location, inc);
3774             }
3775           else if (FRACT_MODE_P (TYPE_MODE (argtype)))
3776             {
3777               /* For signed fract types, we invert ++ to -- or
3778                  -- to ++, and change inc from 1 to -1, because
3779                  it is not possible to represent 1 in signed fract constants.
3780                  For unsigned fract types, the result always overflows and
3781                  we get an undefined (original) or the maximum value.  */
3782               if (code == PREINCREMENT_EXPR)
3783                 code = PREDECREMENT_EXPR;
3784               else if (code == PREDECREMENT_EXPR)
3785                 code = PREINCREMENT_EXPR;
3786               else if (code == POSTINCREMENT_EXPR)
3787                 code = POSTDECREMENT_EXPR;
3788               else /* code == POSTDECREMENT_EXPR  */
3789                 code = POSTINCREMENT_EXPR;
3790 
3791               inc = integer_minus_one_node;
3792               inc = convert (argtype, inc);
3793             }
3794           else
3795             {
3796               inc = integer_one_node;
3797               inc = convert (argtype, inc);
3798             }
3799 
3800           /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
3801              need to ask Objective-C to build the increment or decrement
3802              expression for it.  */
3803           if (objc_is_property_ref (arg))
3804             return objc_build_incr_expr_for_property_ref (location, code,
3805                                                                       arg, inc);
3806 
3807           /* Report a read-only lvalue.  */
3808           if (TYPE_READONLY (argtype))
3809             {
3810               readonly_error (arg,
3811                                   ((code == PREINCREMENT_EXPR
3812                                     || code == POSTINCREMENT_EXPR)
3813                                    ? lv_increment : lv_decrement));
3814               return error_mark_node;
3815             }
3816           else if (TREE_READONLY (arg))
3817             readonly_warning (arg,
3818                                   ((code == PREINCREMENT_EXPR
3819                                     || code == POSTINCREMENT_EXPR)
3820                                    ? lv_increment : lv_decrement));
3821 
3822           if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3823             val = boolean_increment (code, arg);
3824           else
3825             val = build2 (code, TREE_TYPE (arg), arg, inc);
3826           TREE_SIDE_EFFECTS (val) = 1;
3827           if (TREE_CODE (val) != code)
3828             TREE_NO_WARNING (val) = 1;
3829           ret = val;
3830           goto return_build_unary_op;
3831       }
3832 
3833     case ADDR_EXPR:
3834       /* Note that this operation never does default_conversion.  */
3835 
3836       /* The operand of unary '&' must be an lvalue (which excludes
3837            expressions of type void), or, in C99, the result of a [] or
3838            unary '*' operator.  */
3839       if (VOID_TYPE_P (TREE_TYPE (arg))
3840             && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
3841             && (TREE_CODE (arg) != INDIRECT_REF
3842                 || !flag_isoc99))
3843           pedwarn (location, 0, "taking address of expression of type %<void%>");
3844 
3845       /* Let &* cancel out to simplify resulting code.  */
3846       if (TREE_CODE (arg) == INDIRECT_REF)
3847           {
3848             /* Don't let this be an lvalue.  */
3849             if (lvalue_p (TREE_OPERAND (arg, 0)))
3850               return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
3851             ret = TREE_OPERAND (arg, 0);
3852             goto return_build_unary_op;
3853           }
3854 
3855       /* For &x[y], return x+y */
3856       if (TREE_CODE (arg) == ARRAY_REF)
3857           {
3858             tree op0 = TREE_OPERAND (arg, 0);
3859             if (!c_mark_addressable (op0))
3860               return error_mark_node;
3861           }
3862 
3863       /* Anything not already handled and not a true memory reference
3864            or a non-lvalue array is an error.  */
3865       else if (typecode != FUNCTION_TYPE && !flag
3866                  && !lvalue_or_else (location, arg, lv_addressof))
3867           return error_mark_node;
3868 
3869       /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
3870            folding later.  */
3871       if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3872           {
3873             tree inner = build_unary_op (location, code,
3874                                                C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3875             ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3876                               C_MAYBE_CONST_EXPR_PRE (arg), inner);
3877             gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3878             C_MAYBE_CONST_EXPR_NON_CONST (ret)
3879               = C_MAYBE_CONST_EXPR_NON_CONST (arg);
3880             goto return_build_unary_op;
3881           }
3882 
3883       /* Ordinary case; arg is a COMPONENT_REF or a decl.  */
3884       argtype = TREE_TYPE (arg);
3885 
3886       /* If the lvalue is const or volatile, merge that into the type
3887            to which the address will point.  This is only needed
3888            for function types.  */
3889       if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
3890             && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
3891             && TREE_CODE (argtype) == FUNCTION_TYPE)
3892           {
3893             int orig_quals = TYPE_QUALS (strip_array_types (argtype));
3894             int quals = orig_quals;
3895 
3896             if (TREE_READONLY (arg))
3897               quals |= TYPE_QUAL_CONST;
3898             if (TREE_THIS_VOLATILE (arg))
3899               quals |= TYPE_QUAL_VOLATILE;
3900 
3901             argtype = c_build_qualified_type (argtype, quals);
3902           }
3903 
3904       if (!c_mark_addressable (arg))
3905           return error_mark_node;
3906 
3907       gcc_assert (TREE_CODE (arg) != COMPONENT_REF
3908                       || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
3909 
3910       argtype = build_pointer_type (argtype);
3911 
3912       /* ??? Cope with user tricks that amount to offsetof.  Delete this
3913            when we have proper support for integer constant expressions.  */
3914       val = get_base_address (arg);
3915       if (val && TREE_CODE (val) == INDIRECT_REF
3916           && TREE_CONSTANT (TREE_OPERAND (val, 0)))
3917           {
3918             ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
3919             goto return_build_unary_op;
3920           }
3921 
3922       val = build1 (ADDR_EXPR, argtype, arg);
3923 
3924       ret = val;
3925       goto return_build_unary_op;
3926 
3927     default:
3928       gcc_unreachable ();
3929     }
3930 
3931   if (argtype == 0)
3932     argtype = TREE_TYPE (arg);
3933   if (TREE_CODE (arg) == INTEGER_CST)
3934     ret = (require_constant_value
3935              ? fold_build1_initializer_loc (location, code, argtype, arg)
3936              : fold_build1_loc (location, code, argtype, arg));
3937   else
3938     ret = build1 (code, argtype, arg);
3939  return_build_unary_op:
3940   gcc_assert (ret != error_mark_node);
3941   if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
3942       && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
3943     ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
3944   else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
3945     ret = note_integer_operands (ret);
3946   if (eptype)
3947     ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
3948   protected_set_expr_location (ret, location);
3949   return ret;
3950 }
3951 
3952 /* Return nonzero if REF is an lvalue valid for this language.
3953    Lvalues can be assigned, unless their type has TYPE_READONLY.
3954    Lvalues can have their address taken, unless they have C_DECL_REGISTER.  */
3955 
3956 bool
lvalue_p(const_tree ref)3957 lvalue_p (const_tree ref)
3958 {
3959   const enum tree_code code = TREE_CODE (ref);
3960 
3961   switch (code)
3962     {
3963     case REALPART_EXPR:
3964     case IMAGPART_EXPR:
3965     case COMPONENT_REF:
3966       return lvalue_p (TREE_OPERAND (ref, 0));
3967 
3968     case C_MAYBE_CONST_EXPR:
3969       return lvalue_p (TREE_OPERAND (ref, 1));
3970 
3971     case COMPOUND_LITERAL_EXPR:
3972     case STRING_CST:
3973       return 1;
3974 
3975     case INDIRECT_REF:
3976     case ARRAY_REF:
3977     case VAR_DECL:
3978     case PARM_DECL:
3979     case RESULT_DECL:
3980     case ERROR_MARK:
3981       return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3982                 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3983 
3984     case BIND_EXPR:
3985       return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3986 
3987     default:
3988       return 0;
3989     }
3990 }
3991 
3992 /* Give a warning for storing in something that is read-only in GCC
3993    terms but not const in ISO C terms.  */
3994 
3995 static void
readonly_warning(tree arg,enum lvalue_use use)3996 readonly_warning (tree arg, enum lvalue_use use)
3997 {
3998   switch (use)
3999     {
4000     case lv_assign:
4001       warning (0, "assignment of read-only location %qE", arg);
4002       break;
4003     case lv_increment:
4004       warning (0, "increment of read-only location %qE", arg);
4005       break;
4006     case lv_decrement:
4007       warning (0, "decrement of read-only location %qE", arg);
4008       break;
4009     default:
4010       gcc_unreachable ();
4011     }
4012   return;
4013 }
4014 
4015 
4016 /* Return nonzero if REF is an lvalue valid for this language;
4017    otherwise, print an error message and return zero.  USE says
4018    how the lvalue is being used and so selects the error message.
4019    LOCATION is the location at which any error should be reported.  */
4020 
4021 static int
lvalue_or_else(location_t loc,const_tree ref,enum lvalue_use use)4022 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
4023 {
4024   int win = lvalue_p (ref);
4025 
4026   if (!win)
4027     lvalue_error (loc, use);
4028 
4029   return win;
4030 }
4031 
4032 /* Mark EXP saying that we need to be able to take the
4033    address of it; it should not be allocated in a register.
4034    Returns true if successful.  */
4035 
4036 bool
c_mark_addressable(tree exp)4037 c_mark_addressable (tree exp)
4038 {
4039   tree x = exp;
4040 
4041   while (1)
4042     switch (TREE_CODE (x))
4043       {
4044       case COMPONENT_REF:
4045           if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
4046             {
4047               error
4048                 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
4049               return false;
4050             }
4051 
4052           /* ... fall through ...  */
4053 
4054       case ADDR_EXPR:
4055       case ARRAY_REF:
4056       case REALPART_EXPR:
4057       case IMAGPART_EXPR:
4058           x = TREE_OPERAND (x, 0);
4059           break;
4060 
4061       case COMPOUND_LITERAL_EXPR:
4062       case CONSTRUCTOR:
4063           TREE_ADDRESSABLE (x) = 1;
4064           return true;
4065 
4066       case VAR_DECL:
4067       case CONST_DECL:
4068       case PARM_DECL:
4069       case RESULT_DECL:
4070           if (C_DECL_REGISTER (x)
4071               && DECL_NONLOCAL (x))
4072             {
4073               if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4074                 {
4075                     error
4076                       ("global register variable %qD used in nested function", x);
4077                     return false;
4078                 }
4079               pedwarn (input_location, 0, "register variable %qD used in nested function", x);
4080             }
4081           else if (C_DECL_REGISTER (x))
4082             {
4083               if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4084                 error ("address of global register variable %qD requested", x);
4085               else
4086                 error ("address of register variable %qD requested", x);
4087               return false;
4088             }
4089 
4090           /* drops in */
4091       case FUNCTION_DECL:
4092           TREE_ADDRESSABLE (x) = 1;
4093           /* drops out */
4094       default:
4095           return true;
4096     }
4097 }
4098 
4099 /* Convert EXPR to TYPE, warning about conversion problems with
4100    constants.  SEMANTIC_TYPE is the type this conversion would use
4101    without excess precision. If SEMANTIC_TYPE is NULL, this function
4102    is equivalent to convert_and_check. This function is a wrapper that
4103    handles conversions that may be different than
4104    the usual ones because of excess precision.  */
4105 
4106 static tree
ep_convert_and_check(tree type,tree expr,tree semantic_type)4107 ep_convert_and_check (tree type, tree expr, tree semantic_type)
4108 {
4109   if (TREE_TYPE (expr) == type)
4110     return expr;
4111 
4112   if (!semantic_type)
4113     return convert_and_check (type, expr);
4114 
4115   if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4116       && TREE_TYPE (expr) != semantic_type)
4117     {
4118       /* For integers, we need to check the real conversion, not
4119            the conversion to the excess precision type.  */
4120       expr = convert_and_check (semantic_type, expr);
4121     }
4122   /* Result type is the excess precision type, which should be
4123      large enough, so do not check.  */
4124   return convert (type, expr);
4125 }
4126 
4127 /* Build and return a conditional expression IFEXP ? OP1 : OP2.  If
4128    IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4129    if folded to an integer constant then the unselected half may
4130    contain arbitrary operations not normally permitted in constant
4131    expressions.  Set the location of the expression to LOC.  */
4132 
4133 tree
build_conditional_expr(location_t colon_loc,tree ifexp,bool ifexp_bcp,tree op1,tree op1_original_type,tree op2,tree op2_original_type)4134 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4135                               tree op1, tree op1_original_type, tree op2,
4136                               tree op2_original_type)
4137 {
4138   tree type1;
4139   tree type2;
4140   enum tree_code code1;
4141   enum tree_code code2;
4142   tree result_type = NULL;
4143   tree semantic_result_type = NULL;
4144   tree orig_op1 = op1, orig_op2 = op2;
4145   bool int_const, op1_int_operands, op2_int_operands, int_operands;
4146   bool ifexp_int_operands;
4147   tree ret;
4148 
4149   op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4150   if (op1_int_operands)
4151     op1 = remove_c_maybe_const_expr (op1);
4152   op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4153   if (op2_int_operands)
4154     op2 = remove_c_maybe_const_expr (op2);
4155   ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4156   if (ifexp_int_operands)
4157     ifexp = remove_c_maybe_const_expr (ifexp);
4158 
4159   /* Promote both alternatives.  */
4160 
4161   if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4162     op1 = default_conversion (op1);
4163   if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4164     op2 = default_conversion (op2);
4165 
4166   if (TREE_CODE (ifexp) == ERROR_MARK
4167       || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4168       || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4169     return error_mark_node;
4170 
4171   type1 = TREE_TYPE (op1);
4172   code1 = TREE_CODE (type1);
4173   type2 = TREE_TYPE (op2);
4174   code2 = TREE_CODE (type2);
4175 
4176   /* C90 does not permit non-lvalue arrays in conditional expressions.
4177      In C99 they will be pointers by now.  */
4178   if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4179     {
4180       error_at (colon_loc, "non-lvalue array in conditional expression");
4181       return error_mark_node;
4182     }
4183 
4184   if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4185        || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4186       && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4187             || code1 == COMPLEX_TYPE)
4188       && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4189             || code2 == COMPLEX_TYPE))
4190     {
4191       semantic_result_type = c_common_type (type1, type2);
4192       if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4193           {
4194             op1 = TREE_OPERAND (op1, 0);
4195             type1 = TREE_TYPE (op1);
4196             gcc_assert (TREE_CODE (type1) == code1);
4197           }
4198       if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4199           {
4200             op2 = TREE_OPERAND (op2, 0);
4201             type2 = TREE_TYPE (op2);
4202             gcc_assert (TREE_CODE (type2) == code2);
4203           }
4204     }
4205 
4206   if (warn_cxx_compat)
4207     {
4208       tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4209       tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4210 
4211       if (TREE_CODE (t1) == ENUMERAL_TYPE
4212             && TREE_CODE (t2) == ENUMERAL_TYPE
4213             && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4214           warning_at (colon_loc, OPT_Wc___compat,
4215                         ("different enum types in conditional is "
4216                          "invalid in C++: %qT vs %qT"),
4217                         t1, t2);
4218     }
4219 
4220   /* Quickly detect the usual case where op1 and op2 have the same type
4221      after promotion.  */
4222   if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4223     {
4224       if (type1 == type2)
4225           result_type = type1;
4226       else
4227           result_type = TYPE_MAIN_VARIANT (type1);
4228     }
4229   else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
4230               || code1 == COMPLEX_TYPE)
4231              && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4232                  || code2 == COMPLEX_TYPE))
4233     {
4234       result_type = c_common_type (type1, type2);
4235       do_warn_double_promotion (result_type, type1, type2,
4236                                         "implicit conversion from %qT to %qT to "
4237                                         "match other result of conditional",
4238                                         colon_loc);
4239 
4240       /* If -Wsign-compare, warn here if type1 and type2 have
4241            different signedness.  We'll promote the signed to unsigned
4242            and later code won't know it used to be different.
4243            Do this check on the original types, so that explicit casts
4244            will be considered, but default promotions won't.  */
4245       if (c_inhibit_evaluation_warnings == 0)
4246           {
4247             int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4248             int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
4249 
4250             if (unsigned_op1 ^ unsigned_op2)
4251               {
4252                 bool ovf;
4253 
4254                 /* Do not warn if the result type is signed, since the
4255                      signed type will only be chosen if it can represent
4256                      all the values of the unsigned type.  */
4257                 if (!TYPE_UNSIGNED (result_type))
4258                     /* OK */;
4259                 else
4260                     {
4261                       bool op1_maybe_const = true;
4262                       bool op2_maybe_const = true;
4263 
4264                       /* Do not warn if the signed quantity is an
4265                          unsuffixed integer literal (or some static
4266                          constant expression involving such literals) and
4267                          it is non-negative.  This warning requires the
4268                          operands to be folded for best results, so do
4269                          that folding in this case even without
4270                          warn_sign_compare to avoid warning options
4271                          possibly affecting code generation.  */
4272                       c_inhibit_evaluation_warnings
4273                         += (ifexp == truthvalue_false_node);
4274                       op1 = c_fully_fold (op1, require_constant_value,
4275                                               &op1_maybe_const);
4276                       c_inhibit_evaluation_warnings
4277                         -= (ifexp == truthvalue_false_node);
4278 
4279                       c_inhibit_evaluation_warnings
4280                         += (ifexp == truthvalue_true_node);
4281                       op2 = c_fully_fold (op2, require_constant_value,
4282                                               &op2_maybe_const);
4283                       c_inhibit_evaluation_warnings
4284                         -= (ifexp == truthvalue_true_node);
4285 
4286                       if (warn_sign_compare)
4287                         {
4288                           if ((unsigned_op2
4289                                  && tree_expr_nonnegative_warnv_p (op1, &ovf))
4290                                 || (unsigned_op1
4291                                     && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4292                               /* OK */;
4293                           else
4294                               warning_at (colon_loc, OPT_Wsign_compare,
4295                                             ("signed and unsigned type in "
4296                                              "conditional expression"));
4297                         }
4298                       if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
4299                         op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
4300                       if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
4301                         op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
4302                     }
4303               }
4304           }
4305     }
4306   else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4307     {
4308       if (code1 != VOID_TYPE || code2 != VOID_TYPE)
4309           pedwarn (colon_loc, OPT_pedantic,
4310                      "ISO C forbids conditional expr with only one void side");
4311       result_type = void_type_node;
4312     }
4313   else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4314     {
4315       addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4316       addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4317       addr_space_t as_common;
4318 
4319       if (comp_target_types (colon_loc, type1, type2))
4320           result_type = common_pointer_type (type1, type2);
4321       else if (null_pointer_constant_p (orig_op1))
4322           result_type = type2;
4323       else if (null_pointer_constant_p (orig_op2))
4324           result_type = type1;
4325       else if (!addr_space_superset (as1, as2, &as_common))
4326           {
4327             error_at (colon_loc, "pointers to disjoint address spaces "
4328                         "used in conditional expression");
4329             return error_mark_node;
4330           }
4331       else if (VOID_TYPE_P (TREE_TYPE (type1)))
4332           {
4333             if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
4334               pedwarn (colon_loc, OPT_pedantic,
4335                          "ISO C forbids conditional expr between "
4336                          "%<void *%> and function pointer");
4337             result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4338                                                                         TREE_TYPE (type2)));
4339           }
4340       else if (VOID_TYPE_P (TREE_TYPE (type2)))
4341           {
4342             if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
4343               pedwarn (colon_loc, OPT_pedantic,
4344                          "ISO C forbids conditional expr between "
4345                          "%<void *%> and function pointer");
4346             result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4347                                                                         TREE_TYPE (type1)));
4348           }
4349       /* Objective-C pointer comparisons are a bit more lenient.  */
4350       else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4351           result_type = objc_common_type (type1, type2);
4352       else
4353           {
4354             int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4355 
4356             pedwarn (colon_loc, 0,
4357                        "pointer type mismatch in conditional expression");
4358             result_type = build_pointer_type
4359                                 (build_qualified_type (void_type_node, qual));
4360           }
4361     }
4362   else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4363     {
4364       if (!null_pointer_constant_p (orig_op2))
4365           pedwarn (colon_loc, 0,
4366                      "pointer/integer type mismatch in conditional expression");
4367       else
4368           {
4369             op2 = null_pointer_node;
4370           }
4371       result_type = type1;
4372     }
4373   else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4374     {
4375       if (!null_pointer_constant_p (orig_op1))
4376           pedwarn (colon_loc, 0,
4377                      "pointer/integer type mismatch in conditional expression");
4378       else
4379           {
4380             op1 = null_pointer_node;
4381           }
4382       result_type = type2;
4383     }
4384 
4385   if (!result_type)
4386     {
4387       if (flag_cond_mismatch)
4388           result_type = void_type_node;
4389       else
4390           {
4391             error_at (colon_loc, "type mismatch in conditional expression");
4392             return error_mark_node;
4393           }
4394     }
4395 
4396   /* Merge const and volatile flags of the incoming types.  */
4397   result_type
4398     = build_type_variant (result_type,
4399                                 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4400                                 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
4401 
4402   op1 = ep_convert_and_check (result_type, op1, semantic_result_type);
4403   op2 = ep_convert_and_check (result_type, op2, semantic_result_type);
4404 
4405   if (ifexp_bcp && ifexp == truthvalue_true_node)
4406     {
4407       op2_int_operands = true;
4408       op1 = c_fully_fold (op1, require_constant_value, NULL);
4409     }
4410   if (ifexp_bcp && ifexp == truthvalue_false_node)
4411     {
4412       op1_int_operands = true;
4413       op2 = c_fully_fold (op2, require_constant_value, NULL);
4414     }
4415   int_const = int_operands = (ifexp_int_operands
4416                                     && op1_int_operands
4417                                     && op2_int_operands);
4418   if (int_operands)
4419     {
4420       int_const = ((ifexp == truthvalue_true_node
4421                         && TREE_CODE (orig_op1) == INTEGER_CST
4422                         && !TREE_OVERFLOW (orig_op1))
4423                        || (ifexp == truthvalue_false_node
4424                            && TREE_CODE (orig_op2) == INTEGER_CST
4425                            && !TREE_OVERFLOW (orig_op2)));
4426     }
4427   if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
4428     ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
4429   else
4430     {
4431       if (int_operands)
4432           {
4433             op1 = remove_c_maybe_const_expr (op1);
4434             op2 = remove_c_maybe_const_expr (op2);
4435           }
4436       ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4437       if (int_operands)
4438           ret = note_integer_operands (ret);
4439     }
4440   if (semantic_result_type)
4441     ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
4442 
4443   protected_set_expr_location (ret, colon_loc);
4444   return ret;
4445 }
4446 
4447 /* Return a compound expression that performs two expressions and
4448    returns the value of the second of them.
4449 
4450    LOC is the location of the COMPOUND_EXPR.  */
4451 
4452 tree
build_compound_expr(location_t loc,tree expr1,tree expr2)4453 build_compound_expr (location_t loc, tree expr1, tree expr2)
4454 {
4455   bool expr1_int_operands, expr2_int_operands;
4456   tree eptype = NULL_TREE;
4457   tree ret;
4458 
4459   expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4460   if (expr1_int_operands)
4461     expr1 = remove_c_maybe_const_expr (expr1);
4462   expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4463   if (expr2_int_operands)
4464     expr2 = remove_c_maybe_const_expr (expr2);
4465 
4466   if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4467     expr1 = TREE_OPERAND (expr1, 0);
4468   if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4469     {
4470       eptype = TREE_TYPE (expr2);
4471       expr2 = TREE_OPERAND (expr2, 0);
4472     }
4473 
4474   if (!TREE_SIDE_EFFECTS (expr1))
4475     {
4476       /* The left-hand operand of a comma expression is like an expression
4477            statement: with -Wunused, we should warn if it doesn't have
4478            any side-effects, unless it was explicitly cast to (void).  */
4479       if (warn_unused_value)
4480           {
4481             if (VOID_TYPE_P (TREE_TYPE (expr1))
4482                 && CONVERT_EXPR_P (expr1))
4483               ; /* (void) a, b */
4484             else if (VOID_TYPE_P (TREE_TYPE (expr1))
4485                        && TREE_CODE (expr1) == COMPOUND_EXPR
4486                        && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
4487               ; /* (void) a, (void) b, c */
4488             else
4489               warning_at (loc, OPT_Wunused_value,
4490                               "left-hand operand of comma expression has no effect");
4491           }
4492     }
4493 
4494   /* With -Wunused, we should also warn if the left-hand operand does have
4495      side-effects, but computes a value which is not used.  For example, in
4496      `foo() + bar(), baz()' the result of the `+' operator is not used,
4497      so we should issue a warning.  */
4498   else if (warn_unused_value)
4499     warn_if_unused_value (expr1, loc);
4500 
4501   if (expr2 == error_mark_node)
4502     return error_mark_node;
4503 
4504   ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4505 
4506   if (flag_isoc99
4507       && expr1_int_operands
4508       && expr2_int_operands)
4509     ret = note_integer_operands (ret);
4510 
4511   if (eptype)
4512     ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4513 
4514   protected_set_expr_location (ret, loc);
4515   return ret;
4516 }
4517 
4518 /* Issue -Wcast-qual warnings when appropriate.  TYPE is the type to
4519    which we are casting.  OTYPE is the type of the expression being
4520    cast.  Both TYPE and OTYPE are pointer types.  LOC is the location
4521    of the cast.  -Wcast-qual appeared on the command line.  Named
4522    address space qualifiers are not handled here, because they result
4523    in different warnings.  */
4524 
4525 static void
handle_warn_cast_qual(location_t loc,tree type,tree otype)4526 handle_warn_cast_qual (location_t loc, tree type, tree otype)
4527 {
4528   tree in_type = type;
4529   tree in_otype = otype;
4530   int added = 0;
4531   int discarded = 0;
4532   bool is_const;
4533 
4534   /* Check that the qualifiers on IN_TYPE are a superset of the
4535      qualifiers of IN_OTYPE.  The outermost level of POINTER_TYPE
4536      nodes is uninteresting and we stop as soon as we hit a
4537      non-POINTER_TYPE node on either type.  */
4538   do
4539     {
4540       in_otype = TREE_TYPE (in_otype);
4541       in_type = TREE_TYPE (in_type);
4542 
4543       /* GNU C allows cv-qualified function types.  'const' means the
4544            function is very pure, 'volatile' means it can't return.  We
4545            need to warn when such qualifiers are added, not when they're
4546            taken away.  */
4547       if (TREE_CODE (in_otype) == FUNCTION_TYPE
4548             && TREE_CODE (in_type) == FUNCTION_TYPE)
4549           added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
4550                       & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
4551       else
4552           discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
4553                           & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
4554     }
4555   while (TREE_CODE (in_type) == POINTER_TYPE
4556            && TREE_CODE (in_otype) == POINTER_TYPE);
4557 
4558   if (added)
4559     warning_at (loc, OPT_Wcast_qual,
4560                     "cast adds %q#v qualifier to function type", added);
4561 
4562   if (discarded)
4563     /* There are qualifiers present in IN_OTYPE that are not present
4564        in IN_TYPE.  */
4565     warning_at (loc, OPT_Wcast_qual,
4566                     "cast discards %q#v qualifier from pointer target type",
4567                     discarded);
4568 
4569   if (added || discarded)
4570     return;
4571 
4572   /* A cast from **T to const **T is unsafe, because it can cause a
4573      const value to be changed with no additional warning.  We only
4574      issue this warning if T is the same on both sides, and we only
4575      issue the warning if there are the same number of pointers on
4576      both sides, as otherwise the cast is clearly unsafe anyhow.  A
4577      cast is unsafe when a qualifier is added at one level and const
4578      is not present at all outer levels.
4579 
4580      To issue this warning, we check at each level whether the cast
4581      adds new qualifiers not already seen.  We don't need to special
4582      case function types, as they won't have the same
4583      TYPE_MAIN_VARIANT.  */
4584 
4585   if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4586     return;
4587   if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4588     return;
4589 
4590   in_type = type;
4591   in_otype = otype;
4592   is_const = TYPE_READONLY (TREE_TYPE (in_type));
4593   do
4594     {
4595       in_type = TREE_TYPE (in_type);
4596       in_otype = TREE_TYPE (in_otype);
4597       if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4598             && !is_const)
4599           {
4600             warning_at (loc, OPT_Wcast_qual,
4601                           "to be safe all intermediate pointers in cast from "
4602                       "%qT to %qT must be %<const%> qualified",
4603                           otype, type);
4604             break;
4605           }
4606       if (is_const)
4607           is_const = TYPE_READONLY (in_type);
4608     }
4609   while (TREE_CODE (in_type) == POINTER_TYPE);
4610 }
4611 
4612 /* Build an expression representing a cast to type TYPE of expression EXPR.
4613    LOC is the location of the cast-- typically the open paren of the cast.  */
4614 
4615 tree
build_c_cast(location_t loc,tree type,tree expr)4616 build_c_cast (location_t loc, tree type, tree expr)
4617 {
4618   tree value;
4619 
4620   if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4621     expr = TREE_OPERAND (expr, 0);
4622 
4623   value = expr;
4624 
4625   if (type == error_mark_node || expr == error_mark_node)
4626     return error_mark_node;
4627 
4628   /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4629      only in <protocol> qualifications.  But when constructing cast expressions,
4630      the protocols do matter and must be kept around.  */
4631   if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
4632     return build1 (NOP_EXPR, type, expr);
4633 
4634   type = TYPE_MAIN_VARIANT (type);
4635 
4636   if (TREE_CODE (type) == ARRAY_TYPE)
4637     {
4638       error_at (loc, "cast specifies array type");
4639       return error_mark_node;
4640     }
4641 
4642   if (TREE_CODE (type) == FUNCTION_TYPE)
4643     {
4644       error_at (loc, "cast specifies function type");
4645       return error_mark_node;
4646     }
4647 
4648   if (!VOID_TYPE_P (type))
4649     {
4650       value = require_complete_type (value);
4651       if (value == error_mark_node)
4652           return error_mark_node;
4653     }
4654 
4655   if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
4656     {
4657       if (TREE_CODE (type) == RECORD_TYPE
4658             || TREE_CODE (type) == UNION_TYPE)
4659           pedwarn (loc, OPT_pedantic,
4660                      "ISO C forbids casting nonscalar to the same type");
4661     }
4662   else if (TREE_CODE (type) == UNION_TYPE)
4663     {
4664       tree field;
4665 
4666       for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4667           if (TREE_TYPE (field) != error_mark_node
4668               && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
4669                                 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
4670             break;
4671 
4672       if (field)
4673           {
4674             tree t;
4675             bool maybe_const = true;
4676 
4677             pedwarn (loc, OPT_pedantic, "ISO C forbids casts to union type");
4678             t = c_fully_fold (value, false, &maybe_const);
4679             t = build_constructor_single (type, field, t);
4680             if (!maybe_const)
4681               t = c_wrap_maybe_const (t, true);
4682             t = digest_init (loc, type, t,
4683                                  NULL_TREE, false, true, 0);
4684             TREE_CONSTANT (t) = TREE_CONSTANT (value);
4685             return t;
4686           }
4687       error_at (loc, "cast to union type from type not present in union");
4688       return error_mark_node;
4689     }
4690   else
4691     {
4692       tree otype, ovalue;
4693 
4694       if (type == void_type_node)
4695           {
4696             tree t = build1 (CONVERT_EXPR, type, value);
4697             SET_EXPR_LOCATION (t, loc);
4698             return t;
4699           }
4700 
4701       otype = TREE_TYPE (value);
4702 
4703       /* Optionally warn about potentially worrisome casts.  */
4704       if (warn_cast_qual
4705             && TREE_CODE (type) == POINTER_TYPE
4706             && TREE_CODE (otype) == POINTER_TYPE)
4707           handle_warn_cast_qual (loc, type, otype);
4708 
4709       /* Warn about conversions between pointers to disjoint
4710            address spaces.  */
4711       if (TREE_CODE (type) == POINTER_TYPE
4712             && TREE_CODE (otype) == POINTER_TYPE
4713             && !null_pointer_constant_p (value))
4714           {
4715             addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
4716             addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
4717             addr_space_t as_common;
4718 
4719             if (!addr_space_superset (as_to, as_from, &as_common))
4720               {
4721                 if (ADDR_SPACE_GENERIC_P (as_from))
4722                     warning_at (loc, 0, "cast to %s address space pointer "
4723                                   "from disjoint generic address space pointer",
4724                                   c_addr_space_name (as_to));
4725 
4726                 else if (ADDR_SPACE_GENERIC_P (as_to))
4727                     warning_at (loc, 0, "cast to generic address space pointer "
4728                                   "from disjoint %s address space pointer",
4729                                   c_addr_space_name (as_from));
4730 
4731                 else
4732                     warning_at (loc, 0, "cast to %s address space pointer "
4733                                   "from disjoint %s address space pointer",
4734                                   c_addr_space_name (as_to),
4735                                   c_addr_space_name (as_from));
4736               }
4737           }
4738 
4739       /* Warn about possible alignment problems.  */
4740       if (STRICT_ALIGNMENT
4741             && TREE_CODE (type) == POINTER_TYPE
4742             && TREE_CODE (otype) == POINTER_TYPE
4743             && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
4744             && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4745             /* Don't warn about opaque types, where the actual alignment
4746                restriction is unknown.  */
4747             && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
4748                     || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
4749                  && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
4750             && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
4751           warning_at (loc, OPT_Wcast_align,
4752                         "cast increases required alignment of target type");
4753 
4754       if (TREE_CODE (type) == INTEGER_TYPE
4755             && TREE_CODE (otype) == POINTER_TYPE
4756             && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
4757       /* Unlike conversion of integers to pointers, where the
4758          warning is disabled for converting constants because
4759          of cases such as SIG_*, warn about converting constant
4760          pointers to integers. In some cases it may cause unwanted
4761          sign extension, and a warning is appropriate.  */
4762           warning_at (loc, OPT_Wpointer_to_int_cast,
4763                         "cast from pointer to integer of different size");
4764 
4765       if (TREE_CODE (value) == CALL_EXPR
4766             && TREE_CODE (type) != TREE_CODE (otype))
4767           warning_at (loc, OPT_Wbad_function_cast,
4768                         "cast from function call of type %qT "
4769                         "to non-matching type %qT", otype, type);
4770 
4771       if (TREE_CODE (type) == POINTER_TYPE
4772             && TREE_CODE (otype) == INTEGER_TYPE
4773             && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
4774             /* Don't warn about converting any constant.  */
4775             && !TREE_CONSTANT (value))
4776           warning_at (loc,
4777                         OPT_Wint_to_pointer_cast, "cast to pointer from integer "
4778                         "of different size");
4779 
4780       if (warn_strict_aliasing <= 2)
4781         strict_aliasing_warning (otype, type, expr);
4782 
4783       /* If pedantic, warn for conversions between function and object
4784            pointer types, except for converting a null pointer constant
4785            to function pointer type.  */
4786       if (pedantic
4787             && TREE_CODE (type) == POINTER_TYPE
4788             && TREE_CODE (otype) == POINTER_TYPE
4789             && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
4790             && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
4791           pedwarn (loc, OPT_pedantic, "ISO C forbids "
4792                      "conversion of function pointer to object pointer type");
4793 
4794       if (pedantic
4795             && TREE_CODE (type) == POINTER_TYPE
4796             && TREE_CODE (otype) == POINTER_TYPE
4797             && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
4798             && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4799             && !null_pointer_constant_p (value))
4800           pedwarn (loc, OPT_pedantic, "ISO C forbids "
4801                      "conversion of object pointer to function pointer type");
4802 
4803       ovalue = value;
4804       value = convert (type, value);
4805 
4806       /* Ignore any integer overflow caused by the cast.  */
4807       if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
4808           {
4809             if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
4810               {
4811                 if (!TREE_OVERFLOW (value))
4812                     {
4813                       /* Avoid clobbering a shared constant.  */
4814                       value = copy_node (value);
4815                       TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
4816                     }
4817               }
4818             else if (TREE_OVERFLOW (value))
4819               /* Reset VALUE's overflow flags, ensuring constant sharing.  */
4820               value = build_int_cst_wide (TREE_TYPE (value),
4821                                                   TREE_INT_CST_LOW (value),
4822                                                   TREE_INT_CST_HIGH (value));
4823           }
4824     }
4825 
4826   /* Don't let a cast be an lvalue.  */
4827   if (value == expr)
4828     value = non_lvalue_loc (loc, value);
4829 
4830   /* Don't allow the results of casting to floating-point or complex
4831      types be confused with actual constants, or casts involving
4832      integer and pointer types other than direct integer-to-integer
4833      and integer-to-pointer be confused with integer constant
4834      expressions and null pointer constants.  */
4835   if (TREE_CODE (value) == REAL_CST
4836       || TREE_CODE (value) == COMPLEX_CST
4837       || (TREE_CODE (value) == INTEGER_CST
4838             && !((TREE_CODE (expr) == INTEGER_CST
4839                     && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
4840                  || TREE_CODE (expr) == REAL_CST
4841                  || TREE_CODE (expr) == COMPLEX_CST)))
4842       value = build1 (NOP_EXPR, type, value);
4843 
4844   if (CAN_HAVE_LOCATION_P (value))
4845     SET_EXPR_LOCATION (value, loc);
4846   return value;
4847 }
4848 
4849 /* Interpret a cast of expression EXPR to type TYPE.  LOC is the
4850    location of the open paren of the cast, or the position of the cast
4851    expr.  */
4852 tree
c_cast_expr(location_t loc,struct c_type_name * type_name,tree expr)4853 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
4854 {
4855   tree type;
4856   tree type_expr = NULL_TREE;
4857   bool type_expr_const = true;
4858   tree ret;
4859   int saved_wsp = warn_strict_prototypes;
4860 
4861   /* This avoids warnings about unprototyped casts on
4862      integers.  E.g. "#define SIG_DFL (void(*)())0".  */
4863   if (TREE_CODE (expr) == INTEGER_CST)
4864     warn_strict_prototypes = 0;
4865   type = groktypename (type_name, &type_expr, &type_expr_const);
4866   warn_strict_prototypes = saved_wsp;
4867 
4868   ret = build_c_cast (loc, type, expr);
4869   if (type_expr)
4870     {
4871       bool inner_expr_const = true;
4872       ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
4873       ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
4874       C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
4875                                                        && inner_expr_const);
4876       SET_EXPR_LOCATION (ret, loc);
4877     }
4878 
4879   if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
4880     SET_EXPR_LOCATION (ret, loc);
4881 
4882   /* C++ does not permits types to be defined in a cast, but it
4883      allows references to incomplete types.  */
4884   if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
4885     warning_at (loc, OPT_Wc___compat,
4886                     "defining a type in a cast is invalid in C++");
4887 
4888   return ret;
4889 }
4890 
4891 /* Build an assignment expression of lvalue LHS from value RHS.
4892    If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
4893    may differ from TREE_TYPE (LHS) for an enum bitfield.
4894    MODIFYCODE is the code for a binary operator that we use
4895    to combine the old value of LHS with RHS to get the new value.
4896    Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
4897    If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
4898    which may differ from TREE_TYPE (RHS) for an enum value.
4899 
4900    LOCATION is the location of the MODIFYCODE operator.
4901    RHS_LOC is the location of the RHS.  */
4902 
4903 tree
build_modify_expr(location_t location,tree lhs,tree lhs_origtype,enum tree_code modifycode,location_t rhs_loc,tree rhs,tree rhs_origtype)4904 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
4905                        enum tree_code modifycode,
4906                        location_t rhs_loc, tree rhs, tree rhs_origtype)
4907 {
4908   tree result;
4909   tree newrhs;
4910   tree rhs_semantic_type = NULL_TREE;
4911   tree lhstype = TREE_TYPE (lhs);
4912   tree olhstype = lhstype;
4913   bool npc;
4914 
4915   /* Types that aren't fully specified cannot be used in assignments.  */
4916   lhs = require_complete_type (lhs);
4917 
4918   /* Avoid duplicate error messages from operands that had errors.  */
4919   if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
4920     return error_mark_node;
4921 
4922   /* For ObjC properties, defer this check.  */
4923   if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
4924     return error_mark_node;
4925 
4926   if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4927     {
4928       rhs_semantic_type = TREE_TYPE (rhs);
4929       rhs = TREE_OPERAND (rhs, 0);
4930     }
4931 
4932   newrhs = rhs;
4933 
4934   if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
4935     {
4936       tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
4937                                               lhs_origtype, modifycode, rhs_loc, rhs,
4938                                               rhs_origtype);
4939       if (inner == error_mark_node)
4940           return error_mark_node;
4941       result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4942                            C_MAYBE_CONST_EXPR_PRE (lhs), inner);
4943       gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
4944       C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
4945       protected_set_expr_location (result, location);
4946       return result;
4947     }
4948 
4949   /* If a binary op has been requested, combine the old LHS value with the RHS
4950      producing the value we should actually store into the LHS.  */
4951 
4952   if (modifycode != NOP_EXPR)
4953     {
4954       lhs = c_fully_fold (lhs, false, NULL);
4955       lhs = stabilize_reference (lhs);
4956       newrhs = build_binary_op (location,
4957                                         modifycode, lhs, rhs, 1);
4958 
4959       /* The original type of the right hand side is no longer
4960            meaningful.  */
4961       rhs_origtype = NULL_TREE;
4962     }
4963 
4964   if (c_dialect_objc ())
4965     {
4966       /* Check if we are modifying an Objective-C property reference;
4967            if so, we need to generate setter calls.  */
4968       result = objc_maybe_build_modify_expr (lhs, newrhs);
4969       if (result)
4970           return result;
4971 
4972       /* Else, do the check that we postponed for Objective-C.  */
4973       if (!lvalue_or_else (location, lhs, lv_assign))
4974           return error_mark_node;
4975     }
4976 
4977   /* Give an error for storing in something that is 'const'.  */
4978 
4979   if (TYPE_READONLY (lhstype)
4980       || ((TREE_CODE (lhstype) == RECORD_TYPE
4981              || TREE_CODE (lhstype) == UNION_TYPE)
4982             && C_TYPE_FIELDS_READONLY (lhstype)))
4983     {
4984       readonly_error (lhs, lv_assign);
4985       return error_mark_node;
4986     }
4987   else if (TREE_READONLY (lhs))
4988     readonly_warning (lhs, lv_assign);
4989 
4990   /* If storing into a structure or union member,
4991      it has probably been given type `int'.
4992      Compute the type that would go with
4993      the actual amount of storage the member occupies.  */
4994 
4995   if (TREE_CODE (lhs) == COMPONENT_REF
4996       && (TREE_CODE (lhstype) == INTEGER_TYPE
4997             || TREE_CODE (lhstype) == BOOLEAN_TYPE
4998             || TREE_CODE (lhstype) == REAL_TYPE
4999             || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5000     lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5001 
5002   /* If storing in a field that is in actuality a short or narrower than one,
5003      we must store in the field in its actual type.  */
5004 
5005   if (lhstype != TREE_TYPE (lhs))
5006     {
5007       lhs = copy_node (lhs);
5008       TREE_TYPE (lhs) = lhstype;
5009     }
5010 
5011   /* Issue -Wc++-compat warnings about an assignment to an enum type
5012      when LHS does not have its original type.  This happens for,
5013      e.g., an enum bitfield in a struct.  */
5014   if (warn_cxx_compat
5015       && lhs_origtype != NULL_TREE
5016       && lhs_origtype != lhstype
5017       && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5018     {
5019       tree checktype = (rhs_origtype != NULL_TREE
5020                               ? rhs_origtype
5021                               : TREE_TYPE (rhs));
5022       if (checktype != error_mark_node
5023             && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype))
5024           warning_at (location, OPT_Wc___compat,
5025                         "enum conversion in assignment is invalid in C++");
5026     }
5027 
5028   /* Convert new value to destination type.  Fold it first, then
5029      restore any excess precision information, for the sake of
5030      conversion warnings.  */
5031 
5032   npc = null_pointer_constant_p (newrhs);
5033   newrhs = c_fully_fold (newrhs, false, NULL);
5034   if (rhs_semantic_type)
5035     newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
5036   newrhs = convert_for_assignment (location, lhstype, newrhs, rhs_origtype,
5037                                            ic_assign, npc, NULL_TREE, NULL_TREE, 0);
5038   if (TREE_CODE (newrhs) == ERROR_MARK)
5039     return error_mark_node;
5040 
5041   /* Emit ObjC write barrier, if necessary.  */
5042   if (c_dialect_objc () && flag_objc_gc)
5043     {
5044       result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5045       if (result)
5046           {
5047             protected_set_expr_location (result, location);
5048             return result;
5049           }
5050     }
5051 
5052   /* Scan operands.  */
5053 
5054   result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5055   TREE_SIDE_EFFECTS (result) = 1;
5056   protected_set_expr_location (result, location);
5057 
5058   /* If we got the LHS in a different type for storing in,
5059      convert the result back to the nominal type of LHS
5060      so that the value we return always has the same type
5061      as the LHS argument.  */
5062 
5063   if (olhstype == TREE_TYPE (result))
5064     return result;
5065 
5066   result = convert_for_assignment (location, olhstype, result, rhs_origtype,
5067                                            ic_assign, false, NULL_TREE, NULL_TREE, 0);
5068   protected_set_expr_location (result, location);
5069   return result;
5070 }
5071 
5072 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5073    This is used to implement -fplan9-extensions.  */
5074 
5075 static bool
find_anonymous_field_with_type(tree struct_type,tree type)5076 find_anonymous_field_with_type (tree struct_type, tree type)
5077 {
5078   tree field;
5079   bool found;
5080 
5081   gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE
5082                 || TREE_CODE (struct_type) == UNION_TYPE);
5083   found = false;
5084   for (field = TYPE_FIELDS (struct_type);
5085        field != NULL_TREE;
5086        field = TREE_CHAIN (field))
5087     {
5088       if (DECL_NAME (field) == NULL
5089             && comptypes (type, TYPE_MAIN_VARIANT (TREE_TYPE (field))))
5090           {
5091             if (found)
5092               return false;
5093             found = true;
5094           }
5095       else if (DECL_NAME (field) == NULL
5096                  && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
5097                        || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5098                  && find_anonymous_field_with_type (TREE_TYPE (field), type))
5099           {
5100             if (found)
5101               return false;
5102             found = true;
5103           }
5104     }
5105   return found;
5106 }
5107 
5108 /* RHS is an expression whose type is pointer to struct.  If there is
5109    an anonymous field in RHS with type TYPE, then return a pointer to
5110    that field in RHS.  This is used with -fplan9-extensions.  This
5111    returns NULL if no conversion could be found.  */
5112 
5113 static tree
convert_to_anonymous_field(location_t location,tree type,tree rhs)5114 convert_to_anonymous_field (location_t location, tree type, tree rhs)
5115 {
5116   tree rhs_struct_type, lhs_main_type;
5117   tree field, found_field;
5118   bool found_sub_field;
5119   tree ret;
5120 
5121   gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5122   rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5123   gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE
5124                 || TREE_CODE (rhs_struct_type) == UNION_TYPE);
5125 
5126   gcc_assert (POINTER_TYPE_P (type));
5127   lhs_main_type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
5128 
5129   found_field = NULL_TREE;
5130   found_sub_field = false;
5131   for (field = TYPE_FIELDS (rhs_struct_type);
5132        field != NULL_TREE;
5133        field = TREE_CHAIN (field))
5134     {
5135       if (DECL_NAME (field) != NULL_TREE
5136             || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE
5137                 && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE))
5138           continue;
5139       if (comptypes (lhs_main_type, TYPE_MAIN_VARIANT (TREE_TYPE (field))))
5140           {
5141             if (found_field != NULL_TREE)
5142               return NULL_TREE;
5143             found_field = field;
5144           }
5145       else if (find_anonymous_field_with_type (TREE_TYPE (field),
5146                                                          lhs_main_type))
5147           {
5148             if (found_field != NULL_TREE)
5149               return NULL_TREE;
5150             found_field = field;
5151             found_sub_field = true;
5152           }
5153     }
5154 
5155   if (found_field == NULL_TREE)
5156     return NULL_TREE;
5157 
5158   ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5159                                build_fold_indirect_ref (rhs), found_field,
5160                                NULL_TREE);
5161   ret = build_fold_addr_expr_loc (location, ret);
5162 
5163   if (found_sub_field)
5164     {
5165       ret = convert_to_anonymous_field (location, type, ret);
5166       gcc_assert (ret != NULL_TREE);
5167     }
5168 
5169   return ret;
5170 }
5171 
5172 /* Convert value RHS to type TYPE as preparation for an assignment to
5173    an lvalue of type TYPE.  If ORIGTYPE is not NULL_TREE, it is the
5174    original type of RHS; this differs from TREE_TYPE (RHS) for enum
5175    types.  NULL_POINTER_CONSTANT says whether RHS was a null pointer
5176    constant before any folding.
5177    The real work of conversion is done by `convert'.
5178    The purpose of this function is to generate error messages
5179    for assignments that are not allowed in C.
5180    ERRTYPE says whether it is argument passing, assignment,
5181    initialization or return.
5182 
5183    LOCATION is the location of the RHS.
5184    FUNCTION is a tree for the function being called.
5185    PARMNUM is the number of the argument, for printing in error messages.  */
5186 
5187 static tree
convert_for_assignment(location_t location,tree type,tree rhs,tree origtype,enum impl_conv errtype,bool null_pointer_constant,tree fundecl,tree function,int parmnum)5188 convert_for_assignment (location_t location, tree type, tree rhs,
5189                               tree origtype, enum impl_conv errtype,
5190                               bool null_pointer_constant, tree fundecl,
5191                               tree function, int parmnum)
5192 {
5193   enum tree_code codel = TREE_CODE (type);
5194   tree orig_rhs = rhs;
5195   tree rhstype;
5196   enum tree_code coder;
5197   tree rname = NULL_TREE;
5198   bool objc_ok = false;
5199 
5200   if (errtype == ic_argpass)
5201     {
5202       tree selector;
5203       /* Change pointer to function to the function itself for
5204            diagnostics.  */
5205       if (TREE_CODE (function) == ADDR_EXPR
5206             && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5207           function = TREE_OPERAND (function, 0);
5208 
5209       /* Handle an ObjC selector specially for diagnostics.  */
5210       selector = objc_message_selector ();
5211       rname = function;
5212       if (selector && parmnum > 2)
5213           {
5214             rname = selector;
5215             parmnum -= 2;
5216           }
5217     }
5218 
5219   /* This macro is used to emit diagnostics to ensure that all format
5220      strings are complete sentences, visible to gettext and checked at
5221      compile time.  */
5222 #define WARN_FOR_ASSIGNMENT(LOCATION, OPT, AR, AS, IN, RE)                       \
5223   do {                                                                   \
5224     switch (errtype)                                                     \
5225       {                                                                  \
5226       case ic_argpass:                                                   \
5227         if (pedwarn (LOCATION, OPT, AR, parmnum, rname))                 \
5228           inform ((fundecl && !DECL_IS_BUILTIN (fundecl))            \
5229                       ? DECL_SOURCE_LOCATION (fundecl) : LOCATION,               \
5230                   "expected %qT but argument is of type %qT",            \
5231                   type, rhstype);                                        \
5232         break;                                                           \
5233       case ic_assign:                                                    \
5234         pedwarn (LOCATION, OPT, AS);                                     \
5235         break;                                                           \
5236       case ic_init:                                                      \
5237         pedwarn_init (LOCATION, OPT, IN);                                \
5238         break;                                                           \
5239       case ic_return:                                                    \
5240         pedwarn (LOCATION, OPT, RE);                                   \
5241         break;                                                           \
5242       default:                                                           \
5243         gcc_unreachable ();                                              \
5244       }                                                                  \
5245   } while (0)
5246 
5247   /* This macro is used to emit diagnostics to ensure that all format
5248      strings are complete sentences, visible to gettext and checked at
5249      compile time.  It is the same as WARN_FOR_ASSIGNMENT but with an
5250      extra parameter to enumerate qualifiers.  */
5251 
5252 #define WARN_FOR_QUALIFIERS(LOCATION, OPT, AR, AS, IN, RE, QUALS)        \
5253   do {                                                                   \
5254     switch (errtype)                                                     \
5255       {                                                                  \
5256       case ic_argpass:                                                   \
5257         if (pedwarn (LOCATION, OPT, AR, parmnum, rname, QUALS))          \
5258           inform ((fundecl && !DECL_IS_BUILTIN (fundecl))            \
5259                       ? DECL_SOURCE_LOCATION (fundecl) : LOCATION,               \
5260                   "expected %qT but argument is of type %qT",            \
5261                   type, rhstype);                                        \
5262         break;                                                           \
5263       case ic_assign:                                                    \
5264         pedwarn (LOCATION, OPT, AS, QUALS);                          \
5265         break;                                                           \
5266       case ic_init:                                                      \
5267         pedwarn (LOCATION, OPT, IN, QUALS);                          \
5268         break;                                                           \
5269       case ic_return:                                                    \
5270         pedwarn (LOCATION, OPT, RE, QUALS);                            \
5271         break;                                                           \
5272       default:                                                           \
5273         gcc_unreachable ();                                              \
5274       }                                                                  \
5275   } while (0)
5276 
5277   if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5278     rhs = TREE_OPERAND (rhs, 0);
5279 
5280   rhstype = TREE_TYPE (rhs);
5281   coder = TREE_CODE (rhstype);
5282 
5283   if (coder == ERROR_MARK)
5284     return error_mark_node;
5285 
5286   if (c_dialect_objc ())
5287     {
5288       int parmno;
5289 
5290       switch (errtype)
5291           {
5292           case ic_return:
5293             parmno = 0;
5294             break;
5295 
5296           case ic_assign:
5297             parmno = -1;
5298             break;
5299 
5300           case ic_init:
5301             parmno = -2;
5302             break;
5303 
5304           default:
5305             parmno = parmnum;
5306             break;
5307           }
5308 
5309       objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5310     }
5311 
5312   if (warn_cxx_compat)
5313     {
5314       tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5315       if (checktype != error_mark_node
5316             && TREE_CODE (type) == ENUMERAL_TYPE
5317             && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5318           {
5319             WARN_FOR_ASSIGNMENT (input_location, OPT_Wc___compat,
5320                                      G_("enum conversion when passing argument "
5321                                           "%d of %qE is invalid in C++"),
5322                                      G_("enum conversion in assignment is "
5323                                           "invalid in C++"),
5324                                      G_("enum conversion in initialization is "
5325                                           "invalid in C++"),
5326                                      G_("enum conversion in return is "
5327                                           "invalid in C++"));
5328           }
5329     }
5330 
5331   if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
5332     return rhs;
5333 
5334   if (coder == VOID_TYPE)
5335     {
5336       /* Except for passing an argument to an unprototyped function,
5337            this is a constraint violation.  When passing an argument to
5338            an unprototyped function, it is compile-time undefined;
5339            making it a constraint in that case was rejected in
5340            DR#252.  */
5341       error_at (location, "void value not ignored as it ought to be");
5342       return error_mark_node;
5343     }
5344   rhs = require_complete_type (rhs);
5345   if (rhs == error_mark_node)
5346     return error_mark_node;
5347   /* A type converts to a reference to it.
5348      This code doesn't fully support references, it's just for the
5349      special case of va_start and va_copy.  */
5350   if (codel == REFERENCE_TYPE
5351       && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
5352     {
5353       if (!lvalue_p (rhs))
5354           {
5355             error_at (location, "cannot pass rvalue to reference parameter");
5356             return error_mark_node;
5357           }
5358       if (!c_mark_addressable (rhs))
5359           return error_mark_node;
5360       rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
5361       SET_EXPR_LOCATION (rhs, location);
5362 
5363       /* We already know that these two types are compatible, but they
5364            may not be exactly identical.  In fact, `TREE_TYPE (type)' is
5365            likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
5366            likely to be va_list, a typedef to __builtin_va_list, which
5367            is different enough that it will cause problems later.  */
5368       if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
5369           {
5370             rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
5371             SET_EXPR_LOCATION (rhs, location);
5372           }
5373 
5374       rhs = build1 (NOP_EXPR, type, rhs);
5375       SET_EXPR_LOCATION (rhs, location);
5376       return rhs;
5377     }
5378   /* Some types can interconvert without explicit casts.  */
5379   else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
5380              && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
5381     return convert (type, rhs);
5382   /* Arithmetic types all interconvert, and enum is treated like int.  */
5383   else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
5384               || codel == FIXED_POINT_TYPE
5385               || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
5386               || codel == BOOLEAN_TYPE)
5387              && (coder == INTEGER_TYPE || coder == REAL_TYPE
5388                  || coder == FIXED_POINT_TYPE
5389                  || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
5390                  || coder == BOOLEAN_TYPE))
5391     {
5392       tree ret;
5393       bool save = in_late_binary_op;
5394       if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
5395           in_late_binary_op = true;
5396       ret = convert_and_check (type, orig_rhs);
5397       if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
5398           in_late_binary_op = save;
5399       return ret;
5400     }
5401 
5402   /* Aggregates in different TUs might need conversion.  */
5403   if ((codel == RECORD_TYPE || codel == UNION_TYPE)
5404       && codel == coder
5405       && comptypes (type, rhstype))
5406     return convert_and_check (type, rhs);
5407 
5408   /* Conversion to a transparent union or record from its member types.
5409      This applies only to function arguments.  */
5410   if (((codel == UNION_TYPE || codel == RECORD_TYPE)
5411       && TYPE_TRANSPARENT_AGGR (type))
5412       && errtype == ic_argpass)
5413     {
5414       tree memb, marginal_memb = NULL_TREE;
5415 
5416       for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
5417           {
5418             tree memb_type = TREE_TYPE (memb);
5419 
5420             if (comptypes (TYPE_MAIN_VARIANT (memb_type),
5421                                TYPE_MAIN_VARIANT (rhstype)))
5422               break;
5423 
5424             if (TREE_CODE (memb_type) != POINTER_TYPE)
5425               continue;
5426 
5427             if (coder == POINTER_TYPE)
5428               {
5429                 tree ttl = TREE_TYPE (memb_type);
5430                 tree ttr = TREE_TYPE (rhstype);
5431 
5432                 /* Any non-function converts to a [const][volatile] void *
5433                      and vice versa; otherwise, targets must be the same.
5434                      Meanwhile, the lhs target must have all the qualifiers of
5435                      the rhs.  */
5436                 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5437                       || comp_target_types (location, memb_type, rhstype))
5438                     {
5439                       /* If this type won't generate any warnings, use it.  */
5440                       if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
5441                           || ((TREE_CODE (ttr) == FUNCTION_TYPE
5442                                  && TREE_CODE (ttl) == FUNCTION_TYPE)
5443                                 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
5444                                    == TYPE_QUALS (ttr))
5445                                 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
5446                                    == TYPE_QUALS (ttl))))
5447                         break;
5448 
5449                       /* Keep looking for a better type, but remember this one.  */
5450                       if (!marginal_memb)
5451                         marginal_memb = memb;
5452                     }
5453               }
5454 
5455             /* Can convert integer zero to any pointer type.  */
5456             if (null_pointer_constant)
5457               {
5458                 rhs = null_pointer_node;
5459                 break;
5460               }
5461           }
5462 
5463       if (memb || marginal_memb)
5464           {
5465             if (!memb)
5466               {
5467                 /* We have only a marginally acceptable member type;
5468                      it needs a warning.  */
5469                 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
5470                 tree ttr = TREE_TYPE (rhstype);
5471 
5472                 /* Const and volatile mean something different for function
5473                      types, so the usual warnings are not appropriate.  */
5474                 if (TREE_CODE (ttr) == FUNCTION_TYPE
5475                       && TREE_CODE (ttl) == FUNCTION_TYPE)
5476                     {
5477                       /* Because const and volatile on functions are
5478                          restrictions that say the function will not do
5479                          certain things, it is okay to use a const or volatile
5480                          function where an ordinary one is wanted, but not
5481                          vice-versa.  */
5482                       if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5483                           & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5484                         WARN_FOR_QUALIFIERS (location, 0,
5485                                                    G_("passing argument %d of %qE "
5486                                                       "makes %q#v qualified function "
5487                                                       "pointer from unqualified"),
5488                                                    G_("assignment makes %q#v qualified "
5489                                                       "function pointer from "
5490                                                       "unqualified"),
5491                                                    G_("initialization makes %q#v qualified "
5492                                                       "function pointer from "
5493                                                       "unqualified"),
5494                                                    G_("return makes %q#v qualified function "
5495                                                       "pointer from unqualified"),
5496                                                    TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
5497                     }
5498                 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5499                            & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
5500                     WARN_FOR_QUALIFIERS (location, 0,
5501                                              G_("passing argument %d of %qE discards "
5502                                                   "%qv qualifier from pointer target type"),
5503                                              G_("assignment discards %qv qualifier "
5504                                                   "from pointer target type"),
5505                                              G_("initialization discards %qv qualifier "
5506                                                   "from pointer target type"),
5507                                              G_("return discards %qv qualifier from "
5508                                                   "pointer target type"),
5509                                              TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
5510 
5511                 memb = marginal_memb;
5512               }
5513 
5514             if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
5515               pedwarn (location, OPT_pedantic,
5516                          "ISO C prohibits argument conversion to union type");
5517 
5518             rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
5519             return build_constructor_single (type, memb, rhs);
5520           }
5521     }
5522 
5523   /* Conversions among pointers */
5524   else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
5525              && (coder == codel))
5526     {
5527       tree ttl = TREE_TYPE (type);
5528       tree ttr = TREE_TYPE (rhstype);
5529       tree mvl = ttl;
5530       tree mvr = ttr;
5531       bool is_opaque_pointer;
5532       int target_cmp = 0;   /* Cache comp_target_types () result.  */
5533       addr_space_t asl;
5534       addr_space_t asr;
5535 
5536       if (TREE_CODE (mvl) != ARRAY_TYPE)
5537           mvl = TYPE_MAIN_VARIANT (mvl);
5538       if (TREE_CODE (mvr) != ARRAY_TYPE)
5539           mvr = TYPE_MAIN_VARIANT (mvr);
5540       /* Opaque pointers are treated like void pointers.  */
5541       is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
5542 
5543       /* The Plan 9 compiler permits a pointer to a struct to be
5544            automatically converted into a pointer to an anonymous field
5545            within the struct.  */
5546       if (flag_plan9_extensions
5547             && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE)
5548             && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE)
5549             && mvl != mvr)
5550           {
5551             tree new_rhs = convert_to_anonymous_field (location, type, rhs);
5552             if (new_rhs != NULL_TREE)
5553               {
5554                 rhs = new_rhs;
5555                 rhstype = TREE_TYPE (rhs);
5556                 coder = TREE_CODE (rhstype);
5557                 ttr = TREE_TYPE (rhstype);
5558                 mvr = TYPE_MAIN_VARIANT (ttr);
5559               }
5560           }
5561 
5562       /* C++ does not allow the implicit conversion void* -> T*.  However,
5563            for the purpose of reducing the number of false positives, we
5564            tolerate the special case of
5565 
5566                     int *p = NULL;
5567 
5568            where NULL is typically defined in C to be '(void *) 0'.  */
5569       if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
5570           warning_at (location, OPT_Wc___compat,
5571                         "request for implicit conversion "
5572                         "from %qT to %qT not permitted in C++", rhstype, type);
5573 
5574       /* See if the pointers point to incompatible address spaces.  */
5575       asl = TYPE_ADDR_SPACE (ttl);
5576       asr = TYPE_ADDR_SPACE (ttr);
5577       if (!null_pointer_constant_p (rhs)
5578             && asr != asl && !targetm.addr_space.subset_p (asr, asl))
5579           {
5580             switch (errtype)
5581               {
5582               case ic_argpass:
5583                 error_at (location, "passing argument %d of %qE from pointer to "
5584                               "non-enclosed address space", parmnum, rname);
5585                 break;
5586               case ic_assign:
5587                 error_at (location, "assignment from pointer to "
5588                               "non-enclosed address space");
5589                 break;
5590               case ic_init:
5591                 error_at (location, "initialization from pointer to "
5592                               "non-enclosed address space");
5593                 break;
5594               case ic_return:
5595                 error_at (location, "return from pointer to "
5596                               "non-enclosed address space");
5597                 break;
5598               default:
5599                 gcc_unreachable ();
5600               }
5601             return error_mark_node;
5602           }
5603 
5604       /* Check if the right-hand side has a format attribute but the
5605            left-hand side doesn't.  */
5606       if (warn_missing_format_attribute
5607             && check_missing_format_attribute (type, rhstype))
5608           {
5609             switch (errtype)
5610             {
5611             case ic_argpass:
5612               warning_at (location, OPT_Wmissing_format_attribute,
5613                               "argument %d of %qE might be "
5614                               "a candidate for a format attribute",
5615                               parmnum, rname);
5616               break;
5617             case ic_assign:
5618               warning_at (location, OPT_Wmissing_format_attribute,
5619                               "assignment left-hand side might be "
5620                               "a candidate for a format attribute");
5621               break;
5622             case ic_init:
5623               warning_at (location, OPT_Wmissing_format_attribute,
5624                               "initialization left-hand side might be "
5625                               "a candidate for a format attribute");
5626               break;
5627             case ic_return:
5628               warning_at (location, OPT_Wmissing_format_attribute,
5629                               "return type might be "
5630                               "a candidate for a format attribute");
5631               break;
5632             default:
5633               gcc_unreachable ();
5634             }
5635           }
5636 
5637       /* Any non-function converts to a [const][volatile] void *
5638            and vice versa; otherwise, targets must be the same.
5639            Meanwhile, the lhs target must have all the qualifiers of the rhs.  */
5640       if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5641             || (target_cmp = comp_target_types (location, type, rhstype))
5642             || is_opaque_pointer
5643             || (c_common_unsigned_type (mvl)
5644                 == c_common_unsigned_type (mvr)))
5645           {
5646             if (pedantic
5647                 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
5648                       ||
5649                       (VOID_TYPE_P (ttr)
5650                        && !null_pointer_constant
5651                        && TREE_CODE (ttl) == FUNCTION_TYPE)))
5652               WARN_FOR_ASSIGNMENT (location, OPT_pedantic,
5653                                          G_("ISO C forbids passing argument %d of "
5654                                             "%qE between function pointer "
5655                                             "and %<void *%>"),
5656                                          G_("ISO C forbids assignment between "
5657                                             "function pointer and %<void *%>"),
5658                                          G_("ISO C forbids initialization between "
5659                                             "function pointer and %<void *%>"),
5660                                          G_("ISO C forbids return between function "
5661                                             "pointer and %<void *%>"));
5662             /* Const and volatile mean something different for function types,
5663                so the usual warnings are not appropriate.  */
5664             else if (TREE_CODE (ttr) != FUNCTION_TYPE
5665                        && TREE_CODE (ttl) != FUNCTION_TYPE)
5666               {
5667                 if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5668                       & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
5669                     {
5670                       WARN_FOR_QUALIFIERS (location, 0,
5671                                                G_("passing argument %d of %qE discards "
5672                                                     "%qv qualifier from pointer target type"),
5673                                                G_("assignment discards %qv qualifier "
5674                                                     "from pointer target type"),
5675                                                G_("initialization discards %qv qualifier "
5676                                                     "from pointer target type"),
5677                                                G_("return discards %qv qualifier from "
5678                                                     "pointer target type"),
5679                                                TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
5680                     }
5681                 /* If this is not a case of ignoring a mismatch in signedness,
5682                      no warning.  */
5683                 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5684                            || target_cmp)
5685                     ;
5686                 /* If there is a mismatch, do warn.  */
5687                 else if (warn_pointer_sign)
5688                     WARN_FOR_ASSIGNMENT (location, OPT_Wpointer_sign,
5689                                              G_("pointer targets in passing argument "
5690                                                   "%d of %qE differ in signedness"),
5691                                              G_("pointer targets in assignment "
5692                                                   "differ in signedness"),
5693                                              G_("pointer targets in initialization "
5694                                                   "differ in signedness"),
5695                                              G_("pointer targets in return differ "
5696                                                   "in signedness"));
5697               }
5698             else if (TREE_CODE (ttl) == FUNCTION_TYPE
5699                        && TREE_CODE (ttr) == FUNCTION_TYPE)
5700               {
5701                 /* Because const and volatile on functions are restrictions
5702                      that say the function will not do certain things,
5703                      it is okay to use a const or volatile function
5704                      where an ordinary one is wanted, but not vice-versa.  */
5705                 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5706                       & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5707                     WARN_FOR_QUALIFIERS (location, 0,
5708                                              G_("passing argument %d of %qE makes "
5709                                                   "%q#v qualified function pointer "
5710                                                   "from unqualified"),
5711                                              G_("assignment makes %q#v qualified function "
5712                                                   "pointer from unqualified"),
5713                                              G_("initialization makes %q#v qualified "
5714                                                   "function pointer from unqualified"),
5715                                              G_("return makes %q#v qualified function "
5716                                                   "pointer from unqualified"),
5717                                              TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
5718               }
5719           }
5720       else
5721           /* Avoid warning about the volatile ObjC EH puts on decls.  */
5722           if (!objc_ok)
5723             WARN_FOR_ASSIGNMENT (location, 0,
5724                                      G_("passing argument %d of %qE from "
5725                                           "incompatible pointer type"),
5726                                      G_("assignment from incompatible pointer type"),
5727                                      G_("initialization from incompatible "
5728                                           "pointer type"),
5729                                      G_("return from incompatible pointer type"));
5730 
5731       return convert (type, rhs);
5732     }
5733   else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
5734     {
5735       /* ??? This should not be an error when inlining calls to
5736            unprototyped functions.  */
5737       error_at (location, "invalid use of non-lvalue array");
5738       return error_mark_node;
5739     }
5740   else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
5741     {
5742       /* An explicit constant 0 can convert to a pointer,
5743            or one that results from arithmetic, even including
5744            a cast to integer type.  */
5745       if (!null_pointer_constant)
5746           WARN_FOR_ASSIGNMENT (location, 0,
5747                                    G_("passing argument %d of %qE makes "
5748                                         "pointer from integer without a cast"),
5749                                    G_("assignment makes pointer from integer "
5750                                         "without a cast"),
5751                                    G_("initialization makes pointer from "
5752                                         "integer without a cast"),
5753                                    G_("return makes pointer from integer "
5754                                         "without a cast"));
5755 
5756       return convert (type, rhs);
5757     }
5758   else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
5759     {
5760       WARN_FOR_ASSIGNMENT (location, 0,
5761                                  G_("passing argument %d of %qE makes integer "
5762                                     "from pointer without a cast"),
5763                                  G_("assignment makes integer from pointer "
5764                                     "without a cast"),
5765                                  G_("initialization makes integer from pointer "
5766                                     "without a cast"),
5767                                  G_("return makes integer from pointer "
5768                                     "without a cast"));
5769       return convert (type, rhs);
5770     }
5771   else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
5772     {
5773       tree ret;
5774       bool save = in_late_binary_op;
5775       in_late_binary_op = true;
5776       ret = convert (type, rhs);
5777       in_late_binary_op = save;
5778       return ret;
5779     }
5780 
5781   switch (errtype)
5782     {
5783     case ic_argpass:
5784       error_at (location, "incompatible type for argument %d of %qE", parmnum, rname);
5785       inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
5786                 ? DECL_SOURCE_LOCATION (fundecl) : input_location,
5787                 "expected %qT but argument is of type %qT", type, rhstype);
5788       break;
5789     case ic_assign:
5790       error_at (location, "incompatible types when assigning to type %qT from "
5791                     "type %qT", type, rhstype);
5792       break;
5793     case ic_init:
5794       error_at (location,
5795                     "incompatible types when initializing type %qT using type %qT",
5796                     type, rhstype);
5797       break;
5798     case ic_return:
5799       error_at (location,
5800                     "incompatible types when returning type %qT but %qT was "
5801                     "expected", rhstype, type);
5802       break;
5803     default:
5804       gcc_unreachable ();
5805     }
5806 
5807   return error_mark_node;
5808 }
5809 
5810 /* If VALUE is a compound expr all of whose expressions are constant, then
5811    return its value.  Otherwise, return error_mark_node.
5812 
5813    This is for handling COMPOUND_EXPRs as initializer elements
5814    which is allowed with a warning when -pedantic is specified.  */
5815 
5816 static tree
valid_compound_expr_initializer(tree value,tree endtype)5817 valid_compound_expr_initializer (tree value, tree endtype)
5818 {
5819   if (TREE_CODE (value) == COMPOUND_EXPR)
5820     {
5821       if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
5822             == error_mark_node)
5823           return error_mark_node;
5824       return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
5825                                                         endtype);
5826     }
5827   else if (!initializer_constant_valid_p (value, endtype))
5828     return error_mark_node;
5829   else
5830     return value;
5831 }
5832 
5833 /* Perform appropriate conversions on the initial value of a variable,
5834    store it in the declaration DECL,
5835    and print any error messages that are appropriate.
5836    If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5837    If the init is invalid, store an ERROR_MARK.
5838 
5839    INIT_LOC is the location of the initial value.  */
5840 
5841 void
store_init_value(location_t init_loc,tree decl,tree init,tree origtype)5842 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
5843 {
5844   tree value, type;
5845   bool npc = false;
5846 
5847   /* If variable's type was invalidly declared, just ignore it.  */
5848 
5849   type = TREE_TYPE (decl);
5850   if (TREE_CODE (type) == ERROR_MARK)
5851     return;
5852 
5853   /* Digest the specified initializer into an expression.  */
5854 
5855   if (init)
5856     npc = null_pointer_constant_p (init);
5857   value = digest_init (init_loc, type, init, origtype, npc,
5858                            true, TREE_STATIC (decl));
5859 
5860   /* Store the expression if valid; else report error.  */
5861 
5862   if (!in_system_header
5863       && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
5864     warning (OPT_Wtraditional, "traditional C rejects automatic "
5865                "aggregate initialization");
5866 
5867   DECL_INITIAL (decl) = value;
5868 
5869   /* ANSI wants warnings about out-of-range constant initializers.  */
5870   STRIP_TYPE_NOPS (value);
5871   if (TREE_STATIC (decl))
5872     constant_expression_warning (value);
5873 
5874   /* Check if we need to set array size from compound literal size.  */
5875   if (TREE_CODE (type) == ARRAY_TYPE
5876       && TYPE_DOMAIN (type) == 0
5877       && value != error_mark_node)
5878     {
5879       tree inside_init = init;
5880 
5881       STRIP_TYPE_NOPS (inside_init);
5882       inside_init = fold (inside_init);
5883 
5884       if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
5885           {
5886             tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
5887 
5888             if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
5889               {
5890                 /* For int foo[] = (int [3]){1}; we need to set array size
5891                      now since later on array initializer will be just the
5892                      brace enclosed list of the compound literal.  */
5893                 tree etype = strip_array_types (TREE_TYPE (decl));
5894                 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
5895                 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
5896                 layout_type (type);
5897                 layout_decl (cldecl, 0);
5898                 TREE_TYPE (decl)
5899                     = c_build_qualified_type (type, TYPE_QUALS (etype));
5900               }
5901           }
5902     }
5903 }
5904 
5905 /* Methods for storing and printing names for error messages.  */
5906 
5907 /* Implement a spelling stack that allows components of a name to be pushed
5908    and popped.  Each element on the stack is this structure.  */
5909 
5910 struct spelling
5911 {
5912   int kind;
5913   union
5914     {
5915       unsigned HOST_WIDE_INT i;
5916       const char *s;
5917     } u;
5918 };
5919 
5920 #define SPELLING_STRING 1
5921 #define SPELLING_MEMBER 2
5922 #define SPELLING_BOUNDS 3
5923 
5924 static struct spelling *spelling;       /* Next stack element (unused).  */
5925 static struct spelling *spelling_base;  /* Spelling stack base.  */
5926 static int spelling_size;               /* Size of the spelling stack.  */
5927 
5928 /* Macros to save and restore the spelling stack around push_... functions.
5929    Alternative to SAVE_SPELLING_STACK.  */
5930 
5931 #define SPELLING_DEPTH() (spelling - spelling_base)
5932 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
5933 
5934 /* Push an element on the spelling stack with type KIND and assign VALUE
5935    to MEMBER.  */
5936 
5937 #define PUSH_SPELLING(KIND, VALUE, MEMBER)                                      \
5938 {                                                                                         \
5939   int depth = SPELLING_DEPTH ();                                                \
5940                                                                                           \
5941   if (depth >= spelling_size)                                                   \
5942     {                                                                                     \
5943       spelling_size += 10;                                                      \
5944       spelling_base = XRESIZEVEC (struct spelling, spelling_base,     \
5945                                           spelling_size);                       \
5946       RESTORE_SPELLING_DEPTH (depth);                                           \
5947     }                                                                                     \
5948                                                                                           \
5949   spelling->kind = (KIND);                                                      \
5950   spelling->MEMBER = (VALUE);                                                   \
5951   spelling++;                                                                             \
5952 }
5953 
5954 /* Push STRING on the stack.  Printed literally.  */
5955 
5956 static void
push_string(const char * string)5957 push_string (const char *string)
5958 {
5959   PUSH_SPELLING (SPELLING_STRING, string, u.s);
5960 }
5961 
5962 /* Push a member name on the stack.  Printed as '.' STRING.  */
5963 
5964 static void
push_member_name(tree decl)5965 push_member_name (tree decl)
5966 {
5967   const char *const string
5968     = (DECL_NAME (decl)
5969        ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
5970        : _("<anonymous>"));
5971   PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
5972 }
5973 
5974 /* Push an array bounds on the stack.  Printed as [BOUNDS].  */
5975 
5976 static void
push_array_bounds(unsigned HOST_WIDE_INT bounds)5977 push_array_bounds (unsigned HOST_WIDE_INT bounds)
5978 {
5979   PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
5980 }
5981 
5982 /* Compute the maximum size in bytes of the printed spelling.  */
5983 
5984 static int
spelling_length(void)5985 spelling_length (void)
5986 {
5987   int size = 0;
5988   struct spelling *p;
5989 
5990   for (p = spelling_base; p < spelling; p++)
5991     {
5992       if (p->kind == SPELLING_BOUNDS)
5993           size += 25;
5994       else
5995           size += strlen (p->u.s) + 1;
5996     }
5997 
5998   return size;
5999 }
6000 
6001 /* Print the spelling to BUFFER and return it.  */
6002 
6003 static char *
print_spelling(char * buffer)6004 print_spelling (char *buffer)
6005 {
6006   char *d = buffer;
6007   struct spelling *p;
6008 
6009   for (p = spelling_base; p < spelling; p++)
6010     if (p->kind == SPELLING_BOUNDS)
6011       {
6012           sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
6013           d += strlen (d);
6014       }
6015     else
6016       {
6017           const char *s;
6018           if (p->kind == SPELLING_MEMBER)
6019             *d++ = '.';
6020           for (s = p->u.s; (*d = *s++); d++)
6021             ;
6022       }
6023   *d++ = '\0';
6024   return buffer;
6025 }
6026 
6027 /* Issue an error message for a bad initializer component.
6028    GMSGID identifies the message.
6029    The component name is taken from the spelling stack.  */
6030 
6031 void
error_init(const char * gmsgid)6032 error_init (const char *gmsgid)
6033 {
6034   char *ofwhat;
6035 
6036   /* The gmsgid may be a format string with %< and %>. */
6037   error (gmsgid);
6038   ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6039   if (*ofwhat)
6040     error ("(near initialization for %qs)", ofwhat);
6041 }
6042 
6043 /* Issue a pedantic warning for a bad initializer component.  OPT is
6044    the option OPT_* (from options.h) controlling this warning or 0 if
6045    it is unconditionally given.  GMSGID identifies the message.  The
6046    component name is taken from the spelling stack.  */
6047 
6048 void
pedwarn_init(location_t location,int opt,const char * gmsgid)6049 pedwarn_init (location_t location, int opt, const char *gmsgid)
6050 {
6051   char *ofwhat;
6052 
6053   /* The gmsgid may be a format string with %< and %>. */
6054   pedwarn (location, opt, gmsgid);
6055   ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6056   if (*ofwhat)
6057     pedwarn (location, opt, "(near initialization for %qs)", ofwhat);
6058 }
6059 
6060 /* Issue a warning for a bad initializer component.
6061 
6062    OPT is the OPT_W* value corresponding to the warning option that
6063    controls this warning.  GMSGID identifies the message.  The
6064    component name is taken from the spelling stack.  */
6065 
6066 static void
warning_init(int opt,const char * gmsgid)6067 warning_init (int opt, const char *gmsgid)
6068 {
6069   char *ofwhat;
6070 
6071   /* The gmsgid may be a format string with %< and %>. */
6072   warning (opt, gmsgid);
6073   ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6074   if (*ofwhat)
6075     warning (opt, "(near initialization for %qs)", ofwhat);
6076 }
6077 
6078 /* If TYPE is an array type and EXPR is a parenthesized string
6079    constant, warn if pedantic that EXPR is being used to initialize an
6080    object of type TYPE.  */
6081 
6082 void
maybe_warn_string_init(tree type,struct c_expr expr)6083 maybe_warn_string_init (tree type, struct c_expr expr)
6084 {
6085   if (pedantic
6086       && TREE_CODE (type) == ARRAY_TYPE
6087       && TREE_CODE (expr.value) == STRING_CST
6088       && expr.original_code != STRING_CST)
6089     pedwarn_init (input_location, OPT_pedantic,
6090                       "array initialized from parenthesized string constant");
6091 }
6092 
6093 /* Digest the parser output INIT as an initializer for type TYPE.
6094    Return a C expression of type TYPE to represent the initial value.
6095 
6096    If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6097 
6098    NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6099 
6100    If INIT is a string constant, STRICT_STRING is true if it is
6101    unparenthesized or we should not warn here for it being parenthesized.
6102    For other types of INIT, STRICT_STRING is not used.
6103 
6104    INIT_LOC is the location of the INIT.
6105 
6106    REQUIRE_CONSTANT requests an error if non-constant initializers or
6107    elements are seen.  */
6108 
6109 static tree
digest_init(location_t init_loc,tree type,tree init,tree origtype,bool null_pointer_constant,bool strict_string,int require_constant)6110 digest_init (location_t init_loc, tree type, tree init, tree origtype,
6111                bool null_pointer_constant, bool strict_string,
6112                int require_constant)
6113 {
6114   enum tree_code code = TREE_CODE (type);
6115   tree inside_init = init;
6116   tree semantic_type = NULL_TREE;
6117   bool maybe_const = true;
6118 
6119   if (type == error_mark_node
6120       || !init
6121       || init == error_mark_node
6122       || TREE_TYPE (init) == error_mark_node)
6123     return error_mark_node;
6124 
6125   STRIP_TYPE_NOPS (inside_init);
6126 
6127   if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6128     {
6129       semantic_type = TREE_TYPE (inside_init);
6130       inside_init = TREE_OPERAND (inside_init, 0);
6131     }
6132   inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6133   inside_init = decl_constant_value_for_optimization (inside_init);
6134 
6135   /* Initialization of an array of chars from a string constant
6136      optionally enclosed in braces.  */
6137 
6138   if (code == ARRAY_TYPE && inside_init
6139       && TREE_CODE (inside_init) == STRING_CST)
6140     {
6141       tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
6142       /* Note that an array could be both an array of character type
6143            and an array of wchar_t if wchar_t is signed char or unsigned
6144            char.  */
6145       bool char_array = (typ1 == char_type_node
6146                                || typ1 == signed_char_type_node
6147                                || typ1 == unsigned_char_type_node);
6148       bool wchar_array = !!comptypes (typ1, wchar_type_node);
6149       bool char16_array = !!comptypes (typ1, char16_type_node);
6150       bool char32_array = !!comptypes (typ1, char32_type_node);
6151 
6152       if (char_array || wchar_array || char16_array || char32_array)
6153           {
6154             struct c_expr expr;
6155             tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
6156             expr.value = inside_init;
6157             expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6158             expr.original_type = NULL;
6159             maybe_warn_string_init (type, expr);
6160 
6161             if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
6162               pedwarn_init (init_loc, OPT_pedantic,
6163                                 "initialization of a flexible array member");
6164 
6165             if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6166                                TYPE_MAIN_VARIANT (type)))
6167               return inside_init;
6168 
6169             if (char_array)
6170               {
6171                 if (typ2 != char_type_node)
6172                     {
6173                       error_init ("char-array initialized from wide string");
6174                       return error_mark_node;
6175                     }
6176               }
6177             else
6178               {
6179                 if (typ2 == char_type_node)
6180                     {
6181                       error_init ("wide character array initialized from non-wide "
6182                                     "string");
6183                       return error_mark_node;
6184                     }
6185                 else if (!comptypes(typ1, typ2))
6186                     {
6187                       error_init ("wide character array initialized from "
6188                                     "incompatible wide string");
6189                       return error_mark_node;
6190                     }
6191               }
6192 
6193             TREE_TYPE (inside_init) = type;
6194             if (TYPE_DOMAIN (type) != 0
6195                 && TYPE_SIZE (type) != 0
6196                 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6197               {
6198                 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6199 
6200                 /* Subtract the size of a single (possibly wide) character
6201                      because it's ok to ignore the terminating null char
6202                      that is counted in the length of the constant.  */
6203                 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6204                                                   (len
6205                                                    - (TYPE_PRECISION (typ1)
6206                                                       / BITS_PER_UNIT))))
6207                     pedwarn_init (init_loc, 0,
6208                                     ("initializer-string for array of chars "
6209                                      "is too long"));
6210                 else if (warn_cxx_compat
6211                            && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6212                     warning_at (init_loc, OPT_Wc___compat,
6213                                   ("initializer-string for array chars "
6214                                    "is too long for C++"));
6215               }
6216 
6217             return inside_init;
6218           }
6219       else if (INTEGRAL_TYPE_P (typ1))
6220           {
6221             error_init ("array of inappropriate type initialized "
6222                           "from string constant");
6223             return error_mark_node;
6224           }
6225     }
6226 
6227   /* Build a VECTOR_CST from a *constant* vector constructor.  If the
6228      vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6229      below and handle as a constructor.  */
6230   if (code == VECTOR_TYPE
6231       && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
6232       && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
6233       && TREE_CONSTANT (inside_init))
6234     {
6235       if (TREE_CODE (inside_init) == VECTOR_CST
6236             && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6237                               TYPE_MAIN_VARIANT (type)))
6238           return inside_init;
6239 
6240       if (TREE_CODE (inside_init) == CONSTRUCTOR)
6241           {
6242             unsigned HOST_WIDE_INT ix;
6243             tree value;
6244             bool constant_p = true;
6245 
6246             /* Iterate through elements and check if all constructor
6247                elements are *_CSTs.  */
6248             FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6249               if (!CONSTANT_CLASS_P (value))
6250                 {
6251                     constant_p = false;
6252                     break;
6253                 }
6254 
6255             if (constant_p)
6256               return build_vector_from_ctor (type,
6257                                                      CONSTRUCTOR_ELTS (inside_init));
6258           }
6259     }
6260 
6261   if (warn_sequence_point)
6262     verify_sequence_points (inside_init);
6263 
6264   /* Any type can be initialized
6265      from an expression of the same type, optionally with braces.  */
6266 
6267   if (inside_init && TREE_TYPE (inside_init) != 0
6268       && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6269                          TYPE_MAIN_VARIANT (type))
6270             || (code == ARRAY_TYPE
6271                 && comptypes (TREE_TYPE (inside_init), type))
6272             || (code == VECTOR_TYPE
6273                 && comptypes (TREE_TYPE (inside_init), type))
6274             || (code == POINTER_TYPE
6275                 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
6276                 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
6277                                   TREE_TYPE (type)))))
6278     {
6279       if (code == POINTER_TYPE)
6280           {
6281             if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6282               {
6283                 if (TREE_CODE (inside_init) == STRING_CST
6284                       || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6285                     inside_init = array_to_pointer_conversion
6286                       (init_loc, inside_init);
6287                 else
6288                     {
6289                       error_init ("invalid use of non-lvalue array");
6290                       return error_mark_node;
6291                     }
6292               }
6293           }
6294 
6295       if (code == VECTOR_TYPE)
6296           /* Although the types are compatible, we may require a
6297              conversion.  */
6298           inside_init = convert (type, inside_init);
6299 
6300       if (require_constant
6301             && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6302           {
6303             /* As an extension, allow initializing objects with static storage
6304                duration with compound literals (which are then treated just as
6305                the brace enclosed list they contain).  Also allow this for
6306                vectors, as we can only assign them with compound literals.  */
6307             if (flag_isoc99 && code != VECTOR_TYPE)
6308               pedwarn_init (init_loc, OPT_pedantic, "initializer element "
6309                                 "is not constant");
6310             tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6311             inside_init = DECL_INITIAL (decl);
6312           }
6313 
6314       if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6315             && TREE_CODE (inside_init) != CONSTRUCTOR)
6316           {
6317             error_init ("array initialized from non-constant array expression");
6318             return error_mark_node;
6319           }
6320 
6321       /* Compound expressions can only occur here if -pedantic or
6322            -pedantic-errors is specified.  In the later case, we always want
6323            an error.  In the former case, we simply want a warning.  */
6324       if (require_constant && pedantic
6325             && TREE_CODE (inside_init) == COMPOUND_EXPR)
6326           {
6327             inside_init
6328               = valid_compound_expr_initializer (inside_init,
6329                                                          TREE_TYPE (inside_init));
6330             if (inside_init == error_mark_node)
6331               error_init ("initializer element is not constant");
6332             else
6333               pedwarn_init (init_loc, OPT_pedantic,
6334                                 "initializer element is not constant");
6335             if (flag_pedantic_errors)
6336               inside_init = error_mark_node;
6337           }
6338       else if (require_constant
6339                  && !initializer_constant_valid_p (inside_init,
6340                                                              TREE_TYPE (inside_init)))
6341           {
6342             error_init ("initializer element is not constant");
6343             inside_init = error_mark_node;
6344           }
6345       else if (require_constant && !maybe_const)
6346           pedwarn_init (init_loc, 0,
6347                           "initializer element is not a constant expression");
6348 
6349       /* Added to enable additional -Wmissing-format-attribute warnings.  */
6350       if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
6351           inside_init = convert_for_assignment (init_loc, type, inside_init,
6352                                                         origtype,
6353                                                         ic_init, null_pointer_constant,
6354                                                         NULL_TREE, NULL_TREE, 0);
6355       return inside_init;
6356     }
6357 
6358   /* Handle scalar types, including conversions.  */
6359 
6360   if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
6361       || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
6362       || code == COMPLEX_TYPE || code == VECTOR_TYPE)
6363     {
6364       if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
6365             && (TREE_CODE (init) == STRING_CST
6366                 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
6367           inside_init = init = array_to_pointer_conversion (init_loc, init);
6368       if (semantic_type)
6369           inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
6370                                     inside_init);
6371       inside_init
6372           = convert_for_assignment (init_loc, type, inside_init, origtype,
6373                                           ic_init, null_pointer_constant,
6374                                           NULL_TREE, NULL_TREE, 0);
6375 
6376       /* Check to see if we have already given an error message.  */
6377       if (inside_init == error_mark_node)
6378           ;
6379       else if (require_constant && !TREE_CONSTANT (inside_init))
6380           {
6381             error_init ("initializer element is not constant");
6382             inside_init = error_mark_node;
6383           }
6384       else if (require_constant
6385                  && !initializer_constant_valid_p (inside_init,
6386                                                              TREE_TYPE (inside_init)))
6387           {
6388             error_init ("initializer element is not computable at load time");
6389             inside_init = error_mark_node;
6390           }
6391       else if (require_constant && !maybe_const)
6392           pedwarn_init (init_loc, 0,
6393                           "initializer element is not a constant expression");
6394 
6395       return inside_init;
6396     }
6397 
6398   /* Come here only for records and arrays.  */
6399 
6400   if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
6401     {
6402       error_init ("variable-sized object may not be initialized");
6403       return error_mark_node;
6404     }
6405 
6406   error_init ("invalid initializer");
6407   return error_mark_node;
6408 }
6409 
6410 /* Handle initializers that use braces.  */
6411 
6412 /* Type of object we are accumulating a constructor for.
6413    This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE.  */
6414 static tree constructor_type;
6415 
6416 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6417    left to fill.  */
6418 static tree constructor_fields;
6419 
6420 /* For an ARRAY_TYPE, this is the specified index
6421    at which to store the next element we get.  */
6422 static tree constructor_index;
6423 
6424 /* For an ARRAY_TYPE, this is the maximum index.  */
6425 static tree constructor_max_index;
6426 
6427 /* For a RECORD_TYPE, this is the first field not yet written out.  */
6428 static tree constructor_unfilled_fields;
6429 
6430 /* For an ARRAY_TYPE, this is the index of the first element
6431    not yet written out.  */
6432 static tree constructor_unfilled_index;
6433 
6434 /* In a RECORD_TYPE, the byte index of the next consecutive field.
6435    This is so we can generate gaps between fields, when appropriate.  */
6436 static tree constructor_bit_index;
6437 
6438 /* If we are saving up the elements rather than allocating them,
6439    this is the list of elements so far (in reverse order,
6440    most recent first).  */
6441 static VEC(constructor_elt,gc) *constructor_elements;
6442 
6443 /* 1 if constructor should be incrementally stored into a constructor chain,
6444    0 if all the elements should be kept in AVL tree.  */
6445 static int constructor_incremental;
6446 
6447 /* 1 if so far this constructor's elements are all compile-time constants.  */
6448 static int constructor_constant;
6449 
6450 /* 1 if so far this constructor's elements are all valid address constants.  */
6451 static int constructor_simple;
6452 
6453 /* 1 if this constructor has an element that cannot be part of a
6454    constant expression.  */
6455 static int constructor_nonconst;
6456 
6457 /* 1 if this constructor is erroneous so far.  */
6458 static int constructor_erroneous;
6459 
6460 /* Structure for managing pending initializer elements, organized as an
6461    AVL tree.  */
6462 
6463 struct init_node
6464 {
6465   struct init_node *left, *right;
6466   struct init_node *parent;
6467   int balance;
6468   tree purpose;
6469   tree value;
6470   tree origtype;
6471 };
6472 
6473 /* Tree of pending elements at this constructor level.
6474    These are elements encountered out of order
6475    which belong at places we haven't reached yet in actually
6476    writing the output.
6477    Will never hold tree nodes across GC runs.  */
6478 static struct init_node *constructor_pending_elts;
6479 
6480 /* The SPELLING_DEPTH of this constructor.  */
6481 static int constructor_depth;
6482 
6483 /* DECL node for which an initializer is being read.
6484    0 means we are reading a constructor expression
6485    such as (struct foo) {...}.  */
6486 static tree constructor_decl;
6487 
6488 /* Nonzero if this is an initializer for a top-level decl.  */
6489 static int constructor_top_level;
6490 
6491 /* Nonzero if there were any member designators in this initializer.  */
6492 static int constructor_designated;
6493 
6494 /* Nesting depth of designator list.  */
6495 static int designator_depth;
6496 
6497 /* Nonzero if there were diagnosed errors in this designator list.  */
6498 static int designator_erroneous;
6499 
6500 
6501 /* This stack has a level for each implicit or explicit level of
6502    structuring in the initializer, including the outermost one.  It
6503    saves the values of most of the variables above.  */
6504 
6505 struct constructor_range_stack;
6506 
6507 struct constructor_stack
6508 {
6509   struct constructor_stack *next;
6510   tree type;
6511   tree fields;
6512   tree index;
6513   tree max_index;
6514   tree unfilled_index;
6515   tree unfilled_fields;
6516   tree bit_index;
6517   VEC(constructor_elt,gc) *elements;
6518   struct init_node *pending_elts;
6519   int offset;
6520   int depth;
6521   /* If value nonzero, this value should replace the entire
6522      constructor at this level.  */
6523   struct c_expr replacement_value;
6524   struct constructor_range_stack *range_stack;
6525   char constant;
6526   char simple;
6527   char nonconst;
6528   char implicit;
6529   char erroneous;
6530   char outer;
6531   char incremental;
6532   char designated;
6533 };
6534 
6535 static struct constructor_stack *constructor_stack;
6536 
6537 /* This stack represents designators from some range designator up to
6538    the last designator in the list.  */
6539 
6540 struct constructor_range_stack
6541 {
6542   struct constructor_range_stack *next, *prev;
6543   struct constructor_stack *stack;
6544   tree range_start;
6545   tree index;
6546   tree range_end;
6547   tree fields;
6548 };
6549 
6550 static struct constructor_range_stack *constructor_range_stack;
6551 
6552 /* This stack records separate initializers that are nested.
6553    Nested initializers can't happen in ANSI C, but GNU C allows them
6554    in cases like { ... (struct foo) { ... } ... }.  */
6555 
6556 struct initializer_stack
6557 {
6558   struct initializer_stack *next;
6559   tree decl;
6560   struct constructor_stack *constructor_stack;
6561   struct constructor_range_stack *constructor_range_stack;
6562   VEC(constructor_elt,gc) *elements;
6563   struct spelling *spelling;
6564   struct spelling *spelling_base;
6565   int spelling_size;
6566   char top_level;
6567   char require_constant_value;
6568   char require_constant_elements;
6569 };
6570 
6571 static struct initializer_stack *initializer_stack;
6572 
6573 /* Prepare to parse and output the initializer for variable DECL.  */
6574 
6575 void
start_init(tree decl,tree asmspec_tree ATTRIBUTE_UNUSED,int top_level)6576 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
6577 {
6578   const char *locus;
6579   struct initializer_stack *p = XNEW (struct initializer_stack);
6580 
6581   p->decl = constructor_decl;
6582   p->require_constant_value = require_constant_value;
6583   p->require_constant_elements = require_constant_elements;
6584   p->constructor_stack = constructor_stack;
6585   p->constructor_range_stack = constructor_range_stack;
6586   p->elements = constructor_elements;
6587   p->spelling = spelling;
6588   p->spelling_base = spelling_base;
6589   p->spelling_size = spelling_size;
6590   p->top_level = constructor_top_level;
6591   p->next = initializer_stack;
6592   initializer_stack = p;
6593 
6594   constructor_decl = decl;
6595   constructor_designated = 0;
6596   constructor_top_level = top_level;
6597 
6598   if (decl != 0 && decl != error_mark_node)
6599     {
6600       require_constant_value = TREE_STATIC (decl);
6601       require_constant_elements
6602           = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
6603              /* For a scalar, you can always use any value to initialize,
6604                 even within braces.  */
6605              && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
6606                  || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
6607                  || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
6608                  || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
6609       locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
6610     }
6611   else
6612     {
6613       require_constant_value = 0;
6614       require_constant_elements = 0;
6615       locus = _("(anonymous)");
6616     }
6617 
6618   constructor_stack = 0;
6619   constructor_range_stack = 0;
6620 
6621   missing_braces_mentioned = 0;
6622 
6623   spelling_base = 0;
6624   spelling_size = 0;
6625   RESTORE_SPELLING_DEPTH (0);
6626 
6627   if (locus)
6628     push_string (locus);
6629 }
6630 
6631 void
finish_init(void)6632 finish_init (void)
6633 {
6634   struct initializer_stack *p = initializer_stack;
6635 
6636   /* Free the whole constructor stack of this initializer.  */
6637   while (constructor_stack)
6638     {
6639       struct constructor_stack *q = constructor_stack;
6640       constructor_stack = q->next;
6641       free (q);
6642     }
6643 
6644   gcc_assert (!constructor_range_stack);
6645 
6646   /* Pop back to the data of the outer initializer (if any).  */
6647   free (spelling_base);
6648 
6649   constructor_decl = p->decl;
6650   require_constant_value = p->require_constant_value;
6651   require_constant_elements = p->require_constant_elements;
6652   constructor_stack = p->constructor_stack;
6653   constructor_range_stack = p->constructor_range_stack;
6654   constructor_elements = p->elements;
6655   spelling = p->spelling;
6656   spelling_base = p->spelling_base;
6657   spelling_size = p->spelling_size;
6658   constructor_top_level = p->top_level;
6659   initializer_stack = p->next;
6660   free (p);
6661 }
6662 
6663 /* Call here when we see the initializer is surrounded by braces.
6664    This is instead of a call to push_init_level;
6665    it is matched by a call to pop_init_level.
6666 
6667    TYPE is the type to initialize, for a constructor expression.
6668    For an initializer for a decl, TYPE is zero.  */
6669 
6670 void
really_start_incremental_init(tree type)6671 really_start_incremental_init (tree type)
6672 {
6673   struct constructor_stack *p = XNEW (struct constructor_stack);
6674 
6675   if (type == 0)
6676     type = TREE_TYPE (constructor_decl);
6677 
6678   if (TREE_CODE (type) == VECTOR_TYPE
6679       && TYPE_VECTOR_OPAQUE (type))
6680     error ("opaque vector types cannot be initialized");
6681 
6682   p->type = constructor_type;
6683   p->fields = constructor_fields;
6684   p->index = constructor_index;
6685   p->max_index = constructor_max_index;
6686   p->unfilled_index = constructor_unfilled_index;
6687   p->unfilled_fields = constructor_unfilled_fields;
6688   p->bit_index = constructor_bit_index;
6689   p->elements = constructor_elements;
6690   p->constant = constructor_constant;
6691   p->simple = constructor_simple;
6692   p->nonconst = constructor_nonconst;
6693   p->erroneous = constructor_erroneous;
6694   p->pending_elts = constructor_pending_elts;
6695   p->depth = constructor_depth;
6696   p->replacement_value.value = 0;
6697   p->replacement_value.original_code = ERROR_MARK;
6698   p->replacement_value.original_type = NULL;
6699   p->implicit = 0;
6700   p->range_stack = 0;
6701   p->outer = 0;
6702   p->incremental = constructor_incremental;
6703   p->designated = constructor_designated;
6704   p->next = 0;
6705   constructor_stack = p;
6706 
6707   constructor_constant = 1;
6708   constructor_simple = 1;
6709   constructor_nonconst = 0;
6710   constructor_depth = SPELLING_DEPTH ();
6711   constructor_elements = 0;
6712   constructor_pending_elts = 0;
6713   constructor_type = type;
6714   constructor_incremental = 1;
6715   constructor_designated = 0;
6716   designator_depth = 0;
6717   designator_erroneous = 0;
6718 
6719   if (TREE_CODE (constructor_type) == RECORD_TYPE
6720       || TREE_CODE (constructor_type) == UNION_TYPE)
6721     {
6722       constructor_fields = TYPE_FIELDS (constructor_type);
6723       /* Skip any nameless bit fields at the beginning.  */
6724       while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
6725                && DECL_NAME (constructor_fields) == 0)
6726           constructor_fields = DECL_CHAIN (constructor_fields);
6727 
6728       constructor_unfilled_fields = constructor_fields;
6729       constructor_bit_index = bitsize_zero_node;
6730     }
6731   else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6732     {
6733       if (TYPE_DOMAIN (constructor_type))
6734           {
6735             constructor_max_index
6736               = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
6737 
6738             /* Detect non-empty initializations of zero-length arrays.  */
6739             if (constructor_max_index == NULL_TREE
6740                 && TYPE_SIZE (constructor_type))
6741               constructor_max_index = integer_minus_one_node;
6742 
6743             /* constructor_max_index needs to be an INTEGER_CST.  Attempts
6744                to initialize VLAs will cause a proper error; avoid tree
6745                checking errors as well by setting a safe value.  */
6746             if (constructor_max_index
6747                 && TREE_CODE (constructor_max_index) != INTEGER_CST)
6748               constructor_max_index = integer_minus_one_node;
6749 
6750             constructor_index
6751               = convert (bitsizetype,
6752                            TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6753           }
6754       else
6755           {
6756             constructor_index = bitsize_zero_node;
6757             constructor_max_index = NULL_TREE;
6758           }
6759 
6760       constructor_unfilled_index = constructor_index;
6761     }
6762   else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6763     {
6764       /* Vectors are like simple fixed-size arrays.  */
6765       constructor_max_index =
6766           bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
6767       constructor_index = bitsize_zero_node;
6768       constructor_unfilled_index = constructor_index;
6769     }
6770   else
6771     {
6772       /* Handle the case of int x = {5}; */
6773       constructor_fields = constructor_type;
6774       constructor_unfilled_fields = constructor_type;
6775     }
6776 }
6777 
6778 /* Push down into a subobject, for initialization.
6779    If this is for an explicit set of braces, IMPLICIT is 0.
6780    If it is because the next element belongs at a lower level,
6781    IMPLICIT is 1 (or 2 if the push is because of designator list).  */
6782 
6783 void
push_init_level(int implicit,struct obstack * braced_init_obstack)6784 push_init_level (int implicit, struct obstack * braced_init_obstack)
6785 {
6786   struct constructor_stack *p;
6787   tree value = NULL_TREE;
6788 
6789   /* If we've exhausted any levels that didn't have braces,
6790      pop them now.  If implicit == 1, this will have been done in
6791      process_init_element; do not repeat it here because in the case
6792      of excess initializers for an empty aggregate this leads to an
6793      infinite cycle of popping a level and immediately recreating
6794      it.  */
6795   if (implicit != 1)
6796     {
6797       while (constructor_stack->implicit)
6798           {
6799             if ((TREE_CODE (constructor_type) == RECORD_TYPE
6800                  || TREE_CODE (constructor_type) == UNION_TYPE)
6801                 && constructor_fields == 0)
6802               process_init_element (pop_init_level (1, braced_init_obstack),
6803                                           true, braced_init_obstack);
6804             else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6805                        && constructor_max_index
6806                        && tree_int_cst_lt (constructor_max_index,
6807                                                constructor_index))
6808               process_init_element (pop_init_level (1, braced_init_obstack),
6809                                           true, braced_init_obstack);
6810             else
6811               break;
6812           }
6813     }
6814 
6815   /* Unless this is an explicit brace, we need to preserve previous
6816      content if any.  */
6817   if (implicit)
6818     {
6819       if ((TREE_CODE (constructor_type) == RECORD_TYPE
6820              || TREE_CODE (constructor_type) == UNION_TYPE)
6821             && constructor_fields)
6822           value = find_init_member (constructor_fields, braced_init_obstack);
6823       else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6824           value = find_init_member (constructor_index, braced_init_obstack);
6825     }
6826 
6827   p = XNEW (struct constructor_stack);
6828   p->type = constructor_type;
6829   p->fields = constructor_fields;
6830   p->index = constructor_index;
6831   p->max_index = constructor_max_index;
6832   p->unfilled_index = constructor_unfilled_index;
6833   p->unfilled_fields = constructor_unfilled_fields;
6834   p->bit_index = constructor_bit_index;
6835   p->elements = constructor_elements;
6836   p->constant = constructor_constant;
6837   p->simple = constructor_simple;
6838   p->nonconst = constructor_nonconst;
6839   p->erroneous = constructor_erroneous;
6840   p->pending_elts = constructor_pending_elts;
6841   p->depth = constructor_depth;
6842   p->replacement_value.value = 0;
6843   p->replacement_value.original_code = ERROR_MARK;
6844   p->replacement_value.original_type = NULL;
6845   p->implicit = implicit;
6846   p->outer = 0;
6847   p->incremental = constructor_incremental;
6848   p->designated = constructor_designated;
6849   p->next = constructor_stack;
6850   p->range_stack = 0;
6851   constructor_stack = p;
6852 
6853   constructor_constant = 1;
6854   constructor_simple = 1;
6855   constructor_nonconst = 0;
6856   constructor_depth = SPELLING_DEPTH ();
6857   constructor_elements = 0;
6858   constructor_incremental = 1;
6859   constructor_designated = 0;
6860   constructor_pending_elts = 0;
6861   if (!implicit)
6862     {
6863       p->range_stack = constructor_range_stack;
6864       constructor_range_stack = 0;
6865       designator_depth = 0;
6866       designator_erroneous = 0;
6867     }
6868 
6869   /* Don't die if an entire brace-pair level is superfluous
6870      in the containing level.  */
6871   if (constructor_type == 0)
6872     ;
6873   else if (TREE_CODE (constructor_type) == RECORD_TYPE
6874              || TREE_CODE (constructor_type) == UNION_TYPE)
6875     {
6876       /* Don't die if there are extra init elts at the end.  */
6877       if (constructor_fields == 0)
6878           constructor_type = 0;
6879       else
6880           {
6881             constructor_type = TREE_TYPE (constructor_fields);
6882             push_member_name (constructor_fields);
6883             constructor_depth++;
6884           }
6885     }
6886   else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6887     {
6888       constructor_type = TREE_TYPE (constructor_type);
6889       push_array_bounds (tree_low_cst (constructor_index, 1));
6890       constructor_depth++;
6891     }
6892 
6893   if (constructor_type == 0)
6894     {
6895       error_init ("extra brace group at end of initializer");
6896       constructor_fields = 0;
6897       constructor_unfilled_fields = 0;
6898       return;
6899     }
6900 
6901   if (value && TREE_CODE (value) == CONSTRUCTOR)
6902     {
6903       constructor_constant = TREE_CONSTANT (value);
6904       constructor_simple = TREE_STATIC (value);
6905       constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
6906       constructor_elements = CONSTRUCTOR_ELTS (value);
6907       if (!VEC_empty (constructor_elt, constructor_elements)
6908             && (TREE_CODE (constructor_type) == RECORD_TYPE
6909                 || TREE_CODE (constructor_type) == ARRAY_TYPE))
6910           set_nonincremental_init (braced_init_obstack);
6911     }
6912 
6913   if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
6914     {
6915       missing_braces_mentioned = 1;
6916       warning_init (OPT_Wmissing_braces, "missing braces around initializer");
6917     }
6918 
6919   if (TREE_CODE (constructor_type) == RECORD_TYPE
6920              || TREE_CODE (constructor_type) == UNION_TYPE)
6921     {
6922       constructor_fields = TYPE_FIELDS (constructor_type);
6923       /* Skip any nameless bit fields at the beginning.  */
6924       while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
6925                && DECL_NAME (constructor_fields) == 0)
6926           constructor_fields = DECL_CHAIN (constructor_fields);
6927 
6928       constructor_unfilled_fields = constructor_fields;
6929       constructor_bit_index = bitsize_zero_node;
6930     }
6931   else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6932     {
6933       /* Vectors are like simple fixed-size arrays.  */
6934       constructor_max_index =
6935           bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
6936       constructor_index = bitsize_int (0);
6937       constructor_unfilled_index = constructor_index;
6938     }
6939   else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6940     {
6941       if (TYPE_DOMAIN (constructor_type))
6942           {
6943             constructor_max_index
6944               = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
6945 
6946             /* Detect non-empty initializations of zero-length arrays.  */
6947             if (constructor_max_index == NULL_TREE
6948                 && TYPE_SIZE (constructor_type))
6949               constructor_max_index = integer_minus_one_node;
6950 
6951             /* constructor_max_index needs to be an INTEGER_CST.  Attempts
6952                to initialize VLAs will cause a proper error; avoid tree
6953                checking errors as well by setting a safe value.  */
6954             if (constructor_max_index
6955                 && TREE_CODE (constructor_max_index) != INTEGER_CST)
6956               constructor_max_index = integer_minus_one_node;
6957 
6958             constructor_index
6959               = convert (bitsizetype,
6960                            TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6961           }
6962       else
6963           constructor_index = bitsize_zero_node;
6964 
6965       constructor_unfilled_index = constructor_index;
6966       if (value && TREE_CODE (value) == STRING_CST)
6967           {
6968             /* We need to split the char/wchar array into individual
6969                characters, so that we don't have to special case it
6970                everywhere.  */
6971             set_nonincremental_init_from_string (value, braced_init_obstack);
6972           }
6973     }
6974   else
6975     {
6976       if (constructor_type != error_mark_node)
6977           warning_init (0, "braces around scalar initializer");
6978       constructor_fields = constructor_type;
6979       constructor_unfilled_fields = constructor_type;
6980     }
6981 }
6982 
6983 /* At the end of an implicit or explicit brace level,
6984    finish up that level of constructor.  If a single expression
6985    with redundant braces initialized that level, return the
6986    c_expr structure for that expression.  Otherwise, the original_code
6987    element is set to ERROR_MARK.
6988    If we were outputting the elements as they are read, return 0 as the value
6989    from inner levels (process_init_element ignores that),
6990    but return error_mark_node as the value from the outermost level
6991    (that's what we want to put in DECL_INITIAL).
6992    Otherwise, return a CONSTRUCTOR expression as the value.  */
6993 
6994 struct c_expr
pop_init_level(int implicit,struct obstack * braced_init_obstack)6995 pop_init_level (int implicit, struct obstack * braced_init_obstack)
6996 {
6997   struct constructor_stack *p;
6998   struct c_expr ret;
6999   ret.value = 0;
7000   ret.original_code = ERROR_MARK;
7001   ret.original_type = NULL;
7002 
7003   if (implicit == 0)
7004     {
7005       /* When we come to an explicit close brace,
7006            pop any inner levels that didn't have explicit braces.  */
7007       while (constructor_stack->implicit)
7008           {
7009             process_init_element (pop_init_level (1, braced_init_obstack),
7010                                         true, braced_init_obstack);
7011           }
7012       gcc_assert (!constructor_range_stack);
7013     }
7014 
7015   /* Now output all pending elements.  */
7016   constructor_incremental = 1;
7017   output_pending_init_elements (1, braced_init_obstack);
7018 
7019   p = constructor_stack;
7020 
7021   /* Error for initializing a flexible array member, or a zero-length
7022      array member in an inappropriate context.  */
7023   if (constructor_type && constructor_fields
7024       && TREE_CODE (constructor_type) == ARRAY_TYPE
7025       && TYPE_DOMAIN (constructor_type)
7026       && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
7027     {
7028       /* Silently discard empty initializations.  The parser will
7029            already have pedwarned for empty brackets.  */
7030       if (integer_zerop (constructor_unfilled_index))
7031           constructor_type = NULL_TREE;
7032       else
7033           {
7034             gcc_assert (!TYPE_SIZE (constructor_type));
7035 
7036             if (constructor_depth > 2)
7037               error_init ("initialization of flexible array member in a nested context");
7038             else
7039               pedwarn_init (input_location, OPT_pedantic,
7040                                 "initialization of a flexible array member");
7041 
7042             /* We have already issued an error message for the existence
7043                of a flexible array member not at the end of the structure.
7044                Discard the initializer so that we do not die later.  */
7045             if (DECL_CHAIN (constructor_fields) != NULL_TREE)
7046               constructor_type = NULL_TREE;
7047           }
7048     }
7049 
7050   /* Warn when some struct elements are implicitly initialized to zero.  */
7051   if (warn_missing_field_initializers
7052       && constructor_type
7053       && TREE_CODE (constructor_type) == RECORD_TYPE
7054       && constructor_unfilled_fields)
7055     {
7056           bool constructor_zeroinit =
7057            (VEC_length (constructor_elt, constructor_elements) == 1
7058             && integer_zerop
7059                 (VEC_index (constructor_elt, constructor_elements, 0)->value));
7060 
7061           /* Do not warn for flexible array members or zero-length arrays.  */
7062           while (constructor_unfilled_fields
7063                  && (!DECL_SIZE (constructor_unfilled_fields)
7064                        || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
7065             constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
7066 
7067           if (constructor_unfilled_fields
7068               /* Do not warn if this level of the initializer uses member
7069                  designators; it is likely to be deliberate.  */
7070               && !constructor_designated
7071               /* Do not warn about initializing with ` = {0}'.  */
7072               && !constructor_zeroinit)
7073             {
7074               push_member_name (constructor_unfilled_fields);
7075               warning_init (OPT_Wmissing_field_initializers,
7076                           "missing initializer");
7077               RESTORE_SPELLING_DEPTH (constructor_depth);
7078             }
7079     }
7080 
7081   /* Pad out the end of the structure.  */
7082   if (p->replacement_value.value)
7083     /* If this closes a superfluous brace pair,
7084        just pass out the element between them.  */
7085     ret = p->replacement_value;
7086   else if (constructor_type == 0)
7087     ;
7088   else if (TREE_CODE (constructor_type) != RECORD_TYPE
7089              && TREE_CODE (constructor_type) != UNION_TYPE
7090              && TREE_CODE (constructor_type) != ARRAY_TYPE
7091              && TREE_CODE (constructor_type) != VECTOR_TYPE)
7092     {
7093       /* A nonincremental scalar initializer--just return
7094            the element, after verifying there is just one.  */
7095       if (VEC_empty (constructor_elt,constructor_elements))
7096           {
7097             if (!constructor_erroneous)
7098               error_init ("empty scalar initializer");
7099             ret.value = error_mark_node;
7100           }
7101       else if (VEC_length (constructor_elt,constructor_elements) != 1)
7102           {
7103             error_init ("extra elements in scalar initializer");
7104             ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
7105           }
7106       else
7107           ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
7108     }
7109   else
7110     {
7111       if (constructor_erroneous)
7112           ret.value = error_mark_node;
7113       else
7114           {
7115             ret.value = build_constructor (constructor_type,
7116                                                    constructor_elements);
7117             if (constructor_constant)
7118               TREE_CONSTANT (ret.value) = 1;
7119             if (constructor_constant && constructor_simple)
7120               TREE_STATIC (ret.value) = 1;
7121             if (constructor_nonconst)
7122               CONSTRUCTOR_NON_CONST (ret.value) = 1;
7123           }
7124     }
7125 
7126   if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7127     {
7128       if (constructor_nonconst)
7129           ret.original_code = C_MAYBE_CONST_EXPR;
7130       else if (ret.original_code == C_MAYBE_CONST_EXPR)
7131           ret.original_code = ERROR_MARK;
7132     }
7133 
7134   constructor_type = p->type;
7135   constructor_fields = p->fields;
7136   constructor_index = p->index;
7137   constructor_max_index = p->max_index;
7138   constructor_unfilled_index = p->unfilled_index;
7139   constructor_unfilled_fields = p->unfilled_fields;
7140   constructor_bit_index = p->bit_index;
7141   constructor_elements = p->elements;
7142   constructor_constant = p->constant;
7143   constructor_simple = p->simple;
7144   constructor_nonconst = p->nonconst;
7145   constructor_erroneous = p->erroneous;
7146   constructor_incremental = p->incremental;
7147   constructor_designated = p->designated;
7148   constructor_pending_elts = p->pending_elts;
7149   constructor_depth = p->depth;
7150   if (!p->implicit)
7151     constructor_range_stack = p->range_stack;
7152   RESTORE_SPELLING_DEPTH (constructor_depth);
7153 
7154   constructor_stack = p->next;
7155   free (p);
7156 
7157   if (ret.value == 0 && constructor_stack == 0)
7158     ret.value = error_mark_node;
7159   return ret;
7160 }
7161 
7162 /* Common handling for both array range and field name designators.
7163    ARRAY argument is nonzero for array ranges.  Returns zero for success.  */
7164 
7165 static int
set_designator(int array,struct obstack * braced_init_obstack)7166 set_designator (int array, struct obstack * braced_init_obstack)
7167 {
7168   tree subtype;
7169   enum tree_code subcode;
7170 
7171   /* Don't die if an entire brace-pair level is superfluous
7172      in the containing level.  */
7173   if (constructor_type == 0)
7174     return 1;
7175 
7176   /* If there were errors in this designator list already, bail out
7177      silently.  */
7178   if (designator_erroneous)
7179     return 1;
7180 
7181   if (!designator_depth)
7182     {
7183       gcc_assert (!constructor_range_stack);
7184 
7185       /* Designator list starts at the level of closest explicit
7186            braces.  */
7187       while (constructor_stack->implicit)
7188           {
7189             process_init_element (pop_init_level (1, braced_init_obstack),
7190                                         true, braced_init_obstack);
7191           }
7192       constructor_designated = 1;
7193       return 0;
7194     }
7195 
7196   switch (TREE_CODE (constructor_type))
7197     {
7198     case  RECORD_TYPE:
7199     case  UNION_TYPE:
7200       subtype = TREE_TYPE (constructor_fields);
7201       if (subtype != error_mark_node)
7202           subtype = TYPE_MAIN_VARIANT (subtype);
7203       break;
7204     case ARRAY_TYPE:
7205       subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7206       break;
7207     default:
7208       gcc_unreachable ();
7209     }
7210 
7211   subcode = TREE_CODE (subtype);
7212   if (array && subcode != ARRAY_TYPE)
7213     {
7214       error_init ("array index in non-array initializer");
7215       return 1;
7216     }
7217   else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
7218     {
7219       error_init ("field name not in record or union initializer");
7220       return 1;
7221     }
7222 
7223   constructor_designated = 1;
7224   push_init_level (2, braced_init_obstack);
7225   return 0;
7226 }
7227 
7228 /* If there are range designators in designator list, push a new designator
7229    to constructor_range_stack.  RANGE_END is end of such stack range or
7230    NULL_TREE if there is no range designator at this level.  */
7231 
7232 static void
push_range_stack(tree range_end,struct obstack * braced_init_obstack)7233 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
7234 {
7235   struct constructor_range_stack *p;
7236 
7237   p = (struct constructor_range_stack *)
7238     obstack_alloc (braced_init_obstack,
7239                        sizeof (struct constructor_range_stack));
7240   p->prev = constructor_range_stack;
7241   p->next = 0;
7242   p->fields = constructor_fields;
7243   p->range_start = constructor_index;
7244   p->index = constructor_index;
7245   p->stack = constructor_stack;
7246   p->range_end = range_end;
7247   if (constructor_range_stack)
7248     constructor_range_stack->next = p;
7249   constructor_range_stack = p;
7250 }
7251 
7252 /* Within an array initializer, specify the next index to be initialized.
7253    FIRST is that index.  If LAST is nonzero, then initialize a range
7254    of indices, running from FIRST through LAST.  */
7255 
7256 void
set_init_index(tree first,tree last,struct obstack * braced_init_obstack)7257 set_init_index (tree first, tree last,
7258                     struct obstack * braced_init_obstack)
7259 {
7260   if (set_designator (1, braced_init_obstack))
7261     return;
7262 
7263   designator_erroneous = 1;
7264 
7265   if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7266       || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7267     {
7268       error_init ("array index in initializer not of integer type");
7269       return;
7270     }
7271 
7272   if (TREE_CODE (first) != INTEGER_CST)
7273     {
7274       first = c_fully_fold (first, false, NULL);
7275       if (TREE_CODE (first) == INTEGER_CST)
7276           pedwarn_init (input_location, OPT_pedantic,
7277                           "array index in initializer is not "
7278                           "an integer constant expression");
7279     }
7280 
7281   if (last && TREE_CODE (last) != INTEGER_CST)
7282     {
7283       last = c_fully_fold (last, false, NULL);
7284       if (TREE_CODE (last) == INTEGER_CST)
7285           pedwarn_init (input_location, OPT_pedantic,
7286                           "array index in initializer is not "
7287                           "an integer constant expression");
7288     }
7289 
7290   if (TREE_CODE (first) != INTEGER_CST)
7291     error_init ("nonconstant array index in initializer");
7292   else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
7293     error_init ("nonconstant array index in initializer");
7294   else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
7295     error_init ("array index in non-array initializer");
7296   else if (tree_int_cst_sgn (first) == -1)
7297     error_init ("array index in initializer exceeds array bounds");
7298   else if (constructor_max_index
7299              && tree_int_cst_lt (constructor_max_index, first))
7300     error_init ("array index in initializer exceeds array bounds");
7301   else
7302     {
7303       constant_expression_warning (first);
7304       if (last)
7305           constant_expression_warning (last);
7306       constructor_index = convert (bitsizetype, first);
7307 
7308       if (last)
7309           {
7310             if (tree_int_cst_equal (first, last))
7311               last = 0;
7312             else if (tree_int_cst_lt (last, first))
7313               {
7314                 error_init ("empty index range in initializer");
7315                 last = 0;
7316               }
7317             else
7318               {
7319                 last = convert (bitsizetype, last);
7320                 if (constructor_max_index != 0
7321                       && tree_int_cst_lt (constructor_max_index, last))
7322                     {
7323                       error_init ("array index range in initializer exceeds array bounds");
7324                       last = 0;
7325                     }
7326               }
7327           }
7328 
7329       designator_depth++;
7330       designator_erroneous = 0;
7331       if (constructor_range_stack || last)
7332           push_range_stack (last, braced_init_obstack);
7333     }
7334 }
7335 
7336 /* Within a struct initializer, specify the next field to be initialized.  */
7337 
7338 void
set_init_label(tree fieldname,struct obstack * braced_init_obstack)7339 set_init_label (tree fieldname, struct obstack * braced_init_obstack)
7340 {
7341   tree field;
7342 
7343   if (set_designator (0, braced_init_obstack))
7344     return;
7345 
7346   designator_erroneous = 1;
7347 
7348   if (TREE_CODE (constructor_type) != RECORD_TYPE
7349       && TREE_CODE (constructor_type) != UNION_TYPE)
7350     {
7351       error_init ("field name not in record or union initializer");
7352       return;
7353     }
7354 
7355   field = lookup_field (constructor_type, fieldname);
7356 
7357   if (field == 0)
7358     error ("unknown field %qE specified in initializer", fieldname);
7359   else
7360     do
7361       {
7362           constructor_fields = TREE_VALUE (field);
7363           designator_depth++;
7364           designator_erroneous = 0;
7365           if (constructor_range_stack)
7366             push_range_stack (NULL_TREE, braced_init_obstack);
7367           field = TREE_CHAIN (field);
7368           if (field)
7369             {
7370               if (set_designator (0, braced_init_obstack))
7371                 return;
7372             }
7373       }
7374     while (field != NULL_TREE);
7375 }
7376 
7377 /* Add a new initializer to the tree of pending initializers.  PURPOSE
7378    identifies the initializer, either array index or field in a structure.
7379    VALUE is the value of that index or field.  If ORIGTYPE is not
7380    NULL_TREE, it is the original type of VALUE.
7381 
7382    IMPLICIT is true if value comes from pop_init_level (1),
7383    the new initializer has been merged with the existing one
7384    and thus no warnings should be emitted about overriding an
7385    existing initializer.  */
7386 
7387 static void
add_pending_init(tree purpose,tree value,tree origtype,bool implicit,struct obstack * braced_init_obstack)7388 add_pending_init (tree purpose, tree value, tree origtype, bool implicit,
7389                       struct obstack * braced_init_obstack)
7390 {
7391   struct init_node *p, **q, *r;
7392 
7393   q = &constructor_pending_elts;
7394   p = 0;
7395 
7396   if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7397     {
7398       while (*q != 0)
7399           {
7400             p = *q;
7401             if (tree_int_cst_lt (purpose, p->purpose))
7402               q = &p->left;
7403             else if (tree_int_cst_lt (p->purpose, purpose))
7404               q = &p->right;
7405             else
7406               {
7407                 if (!implicit)
7408                     {
7409                       if (TREE_SIDE_EFFECTS (p->value))
7410                         warning_init (0, "initialized field with side-effects overwritten");
7411                       else if (warn_override_init)
7412                         warning_init (OPT_Woverride_init, "initialized field overwritten");
7413                     }
7414                 p->value = value;
7415                 p->origtype = origtype;
7416                 return;
7417               }
7418           }
7419     }
7420   else
7421     {
7422       tree bitpos;
7423 
7424       bitpos = bit_position (purpose);
7425       while (*q != NULL)
7426           {
7427             p = *q;
7428             if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7429               q = &p->left;
7430             else if (p->purpose != purpose)
7431               q = &p->right;
7432             else
7433               {
7434                 if (!implicit)
7435                     {
7436                       if (TREE_SIDE_EFFECTS (p->value))
7437                         warning_init (0, "initialized field with side-effects overwritten");
7438                       else if (warn_override_init)
7439                         warning_init (OPT_Woverride_init, "initialized field overwritten");
7440                     }
7441                 p->value = value;
7442                 p->origtype = origtype;
7443                 return;
7444               }
7445           }
7446     }
7447 
7448   r = (struct init_node *) obstack_alloc (braced_init_obstack,
7449                                                     sizeof (struct init_node));
7450   r->purpose = purpose;
7451   r->value = value;
7452   r->origtype = origtype;
7453 
7454   *q = r;
7455   r->parent = p;
7456   r->left = 0;
7457   r->right = 0;
7458   r->balance = 0;
7459 
7460   while (p)
7461     {
7462       struct init_node *s;
7463 
7464       if (r == p->left)
7465           {
7466             if (p->balance == 0)
7467               p->balance = -1;
7468             else if (p->balance < 0)
7469               {
7470                 if (r->balance < 0)
7471                     {
7472                       /* L rotation.  */
7473                       p->left = r->right;
7474                       if (p->left)
7475                         p->left->parent = p;
7476                       r->right = p;
7477 
7478                       p->balance = 0;
7479                       r->balance = 0;
7480 
7481                       s = p->parent;
7482                       p->parent = r;
7483                       r->parent = s;
7484                       if (s)
7485                         {
7486                           if (s->left == p)
7487                               s->left = r;
7488                           else
7489                               s->right = r;
7490                         }
7491                       else
7492                         constructor_pending_elts = r;
7493                     }
7494                 else
7495                     {
7496                       /* LR rotation.  */
7497                       struct init_node *t = r->right;
7498 
7499                       r->right = t->left;
7500                       if (r->right)
7501                         r->right->parent = r;
7502                       t->left = r;
7503 
7504                       p->left = t->right;
7505                       if (p->left)
7506                         p->left->parent = p;
7507                       t->right = p;
7508 
7509                       p->balance = t->balance < 0;
7510                       r->balance = -(t->balance > 0);
7511                       t->balance = 0;
7512 
7513                       s = p->parent;
7514                       p->parent = t;
7515                       r->parent = t;
7516                       t->parent = s;
7517                       if (s)
7518                         {
7519                           if (s->left == p)
7520                               s->left = t;
7521                           else
7522                               s->right = t;
7523                         }
7524                       else
7525                         constructor_pending_elts = t;
7526                     }
7527                 break;
7528               }
7529             else
7530               {
7531                 /* p->balance == +1; growth of left side balances the node.  */
7532                 p->balance = 0;
7533                 break;
7534               }
7535           }
7536       else /* r == p->right */
7537           {
7538             if (p->balance == 0)
7539               /* Growth propagation from right side.  */
7540               p->balance++;
7541             else if (p->balance > 0)
7542               {
7543                 if (r->balance > 0)
7544                     {
7545                       /* R rotation.  */
7546                       p->right = r->left;
7547                       if (p->right)
7548                         p->right->parent = p;
7549                       r->left = p;
7550 
7551                       p->balance = 0;
7552                       r->balance = 0;
7553 
7554                       s = p->parent;
7555                       p->parent = r;
7556                       r->parent = s;
7557                       if (s)
7558                         {
7559                           if (s->left == p)
7560                               s->left = r;
7561                           else
7562                               s->right = r;
7563                         }
7564                       else
7565                         constructor_pending_elts = r;
7566                     }
7567                 else /* r->balance == -1 */
7568                     {
7569                       /* RL rotation */
7570                       struct init_node *t = r->left;
7571 
7572                       r->left = t->right;
7573                       if (r->left)
7574                         r->left->parent = r;
7575                       t->right = r;
7576 
7577                       p->right = t->left;
7578                       if (p->right)
7579                         p->right->parent = p;
7580                       t->left = p;
7581 
7582                       r->balance = (t->balance < 0);
7583                       p->balance = -(t->balance > 0);
7584                       t->balance = 0;
7585 
7586                       s = p->parent;
7587                       p->parent = t;
7588                       r->parent = t;
7589                       t->parent = s;
7590                       if (s)
7591                         {
7592                           if (s->left == p)
7593                               s->left = t;
7594                           else
7595                               s->right = t;
7596                         }
7597                       else
7598                         constructor_pending_elts = t;
7599                     }
7600                 break;
7601               }
7602             else
7603               {
7604                 /* p->balance == -1; growth of right side balances the node.  */
7605                 p->balance = 0;
7606                 break;
7607               }
7608           }
7609 
7610       r = p;
7611       p = p->parent;
7612     }
7613 }
7614 
7615 /* Build AVL tree from a sorted chain.  */
7616 
7617 static void
set_nonincremental_init(struct obstack * braced_init_obstack)7618 set_nonincremental_init (struct obstack * braced_init_obstack)
7619 {
7620   unsigned HOST_WIDE_INT ix;
7621   tree index, value;
7622 
7623   if (TREE_CODE (constructor_type) != RECORD_TYPE
7624       && TREE_CODE (constructor_type) != ARRAY_TYPE)
7625     return;
7626 
7627   FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
7628     {
7629       add_pending_init (index, value, NULL_TREE, true,
7630                               braced_init_obstack);
7631     }
7632   constructor_elements = 0;
7633   if (TREE_CODE (constructor_type) == RECORD_TYPE)
7634     {
7635       constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
7636       /* Skip any nameless bit fields at the beginning.  */
7637       while (constructor_unfilled_fields != 0
7638                && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7639                && DECL_NAME (constructor_unfilled_fields) == 0)
7640           constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
7641 
7642     }
7643   else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7644     {
7645       if (TYPE_DOMAIN (constructor_type))
7646           constructor_unfilled_index
7647               = convert (bitsizetype,
7648                            TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7649       else
7650           constructor_unfilled_index = bitsize_zero_node;
7651     }
7652   constructor_incremental = 0;
7653 }
7654 
7655 /* Build AVL tree from a string constant.  */
7656 
7657 static void
set_nonincremental_init_from_string(tree str,struct obstack * braced_init_obstack)7658 set_nonincremental_init_from_string (tree str,
7659                                              struct obstack * braced_init_obstack)
7660 {
7661   tree value, purpose, type;
7662   HOST_WIDE_INT val[2];
7663   const char *p, *end;
7664   int byte, wchar_bytes, charwidth, bitpos;
7665 
7666   gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
7667 
7668   wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
7669   charwidth = TYPE_PRECISION (char_type_node);
7670   type = TREE_TYPE (constructor_type);
7671   p = TREE_STRING_POINTER (str);
7672   end = p + TREE_STRING_LENGTH (str);
7673 
7674   for (purpose = bitsize_zero_node;
7675        p < end && !tree_int_cst_lt (constructor_max_index, purpose);
7676        purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
7677     {
7678       if (wchar_bytes == 1)
7679           {
7680             val[1] = (unsigned char) *p++;
7681             val[0] = 0;
7682           }
7683       else
7684           {
7685             val[0] = 0;
7686             val[1] = 0;
7687             for (byte = 0; byte < wchar_bytes; byte++)
7688               {
7689                 if (BYTES_BIG_ENDIAN)
7690                     bitpos = (wchar_bytes - byte - 1) * charwidth;
7691                 else
7692                     bitpos = byte * charwidth;
7693                 val[bitpos < HOST_BITS_PER_WIDE_INT]
7694                     |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
7695                        << (bitpos % HOST_BITS_PER_WIDE_INT);
7696               }
7697           }
7698 
7699       if (!TYPE_UNSIGNED (type))
7700           {
7701             bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
7702             if (bitpos < HOST_BITS_PER_WIDE_INT)
7703               {
7704                 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
7705                     {
7706                       val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
7707                       val[0] = -1;
7708                     }
7709               }
7710             else if (bitpos == HOST_BITS_PER_WIDE_INT)
7711               {
7712                 if (val[1] < 0)
7713                     val[0] = -1;
7714               }
7715             else if (val[0] & (((HOST_WIDE_INT) 1)
7716                                    << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
7717               val[0] |= ((HOST_WIDE_INT) -1)
7718                           << (bitpos - HOST_BITS_PER_WIDE_INT);
7719           }
7720 
7721       value = build_int_cst_wide (type, val[1], val[0]);
7722       add_pending_init (purpose, value, NULL_TREE, true,
7723                         braced_init_obstack);
7724     }
7725 
7726   constructor_incremental = 0;
7727 }
7728 
7729 /* Return value of FIELD in pending initializer or zero if the field was
7730    not initialized yet.  */
7731 
7732 static tree
find_init_member(tree field,struct obstack * braced_init_obstack)7733 find_init_member (tree field, struct obstack * braced_init_obstack)
7734 {
7735   struct init_node *p;
7736 
7737   if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7738     {
7739       if (constructor_incremental
7740             && tree_int_cst_lt (field, constructor_unfilled_index))
7741           set_nonincremental_init (braced_init_obstack);
7742 
7743       p = constructor_pending_elts;
7744       while (p)
7745           {
7746             if (tree_int_cst_lt (field, p->purpose))
7747               p = p->left;
7748             else if (tree_int_cst_lt (p->purpose, field))
7749               p = p->right;
7750             else
7751               return p->value;
7752           }
7753     }
7754   else if (TREE_CODE (constructor_type) == RECORD_TYPE)
7755     {
7756       tree bitpos = bit_position (field);
7757 
7758       if (constructor_incremental
7759             && (!constructor_unfilled_fields
7760                 || tree_int_cst_lt (bitpos,
7761                                           bit_position (constructor_unfilled_fields))))
7762           set_nonincremental_init (braced_init_obstack);
7763 
7764       p = constructor_pending_elts;
7765       while (p)
7766           {
7767             if (field == p->purpose)
7768               return p->value;
7769             else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7770               p = p->left;
7771             else
7772               p = p->right;
7773           }
7774     }
7775   else if (TREE_CODE (constructor_type) == UNION_TYPE)
7776     {
7777       if (!VEC_empty (constructor_elt, constructor_elements)
7778             && (VEC_last (constructor_elt, constructor_elements)->index
7779                 == field))
7780           return VEC_last (constructor_elt, constructor_elements)->value;
7781     }
7782   return 0;
7783 }
7784 
7785 /* "Output" the next constructor element.
7786    At top level, really output it to assembler code now.
7787    Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
7788    If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
7789    TYPE is the data type that the containing data type wants here.
7790    FIELD is the field (a FIELD_DECL) or the index that this element fills.
7791    If VALUE is a string constant, STRICT_STRING is true if it is
7792    unparenthesized or we should not warn here for it being parenthesized.
7793    For other types of VALUE, STRICT_STRING is not used.
7794 
7795    PENDING if non-nil means output pending elements that belong
7796    right after this element.  (PENDING is normally 1;
7797    it is 0 while outputting pending elements, to avoid recursion.)
7798 
7799    IMPLICIT is true if value comes from pop_init_level (1),
7800    the new initializer has been merged with the existing one
7801    and thus no warnings should be emitted about overriding an
7802    existing initializer.  */
7803 
7804 static void
output_init_element(tree value,tree origtype,bool strict_string,tree type,tree field,int pending,bool implicit,struct obstack * braced_init_obstack)7805 output_init_element (tree value, tree origtype, bool strict_string, tree type,
7806                          tree field, int pending, bool implicit,
7807                          struct obstack * braced_init_obstack)
7808 {
7809   tree semantic_type = NULL_TREE;
7810   constructor_elt *celt;
7811   bool maybe_const = true;
7812   bool npc;
7813 
7814   if (type == error_mark_node || value == error_mark_node)
7815     {
7816       constructor_erroneous = 1;
7817       return;
7818     }
7819   if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
7820       && (TREE_CODE (value) == STRING_CST
7821             || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
7822       && !(TREE_CODE (value) == STRING_CST
7823              && TREE_CODE (type) == ARRAY_TYPE
7824              && INTEGRAL_TYPE_P (TREE_TYPE (type)))
7825       && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
7826                          TYPE_MAIN_VARIANT (type)))
7827     value = array_to_pointer_conversion (input_location, value);
7828 
7829   if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
7830       && require_constant_value && pending)
7831     {
7832       /* As an extension, allow initializing objects with static storage
7833            duration with compound literals (which are then treated just as
7834            the brace enclosed list they contain).  */
7835       if (flag_isoc99)
7836           pedwarn_init (input_location, OPT_pedantic, "initializer element is not "
7837                           "constant");
7838       tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
7839       value = DECL_INITIAL (decl);
7840     }
7841 
7842   npc = null_pointer_constant_p (value);
7843   if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
7844     {
7845       semantic_type = TREE_TYPE (value);
7846       value = TREE_OPERAND (value, 0);
7847     }
7848   value = c_fully_fold (value, require_constant_value, &maybe_const);
7849 
7850   if (value == error_mark_node)
7851     constructor_erroneous = 1;
7852   else if (!TREE_CONSTANT (value))
7853     constructor_constant = 0;
7854   else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
7855              || ((TREE_CODE (constructor_type) == RECORD_TYPE
7856                     || TREE_CODE (constructor_type) == UNION_TYPE)
7857                  && DECL_C_BIT_FIELD (field)
7858                  && TREE_CODE (value) != INTEGER_CST))
7859     constructor_simple = 0;
7860   if (!maybe_const)
7861     constructor_nonconst = 1;
7862 
7863   if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
7864     {
7865       if (require_constant_value)
7866           {
7867             error_init ("initializer element is not constant");
7868             value = error_mark_node;
7869           }
7870       else if (require_constant_elements)
7871           pedwarn (input_location, 0,
7872                      "initializer element is not computable at load time");
7873     }
7874   else if (!maybe_const
7875              && (require_constant_value || require_constant_elements))
7876     pedwarn_init (input_location, 0,
7877                       "initializer element is not a constant expression");
7878 
7879   /* Issue -Wc++-compat warnings about initializing a bitfield with
7880      enum type.  */
7881   if (warn_cxx_compat
7882       && field != NULL_TREE
7883       && TREE_CODE (field) == FIELD_DECL
7884       && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
7885       && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
7886             != TYPE_MAIN_VARIANT (type))
7887       && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
7888     {
7889       tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
7890       if (checktype != error_mark_node
7891             && (TYPE_MAIN_VARIANT (checktype)
7892                 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
7893           warning_init (OPT_Wc___compat,
7894                           "enum conversion in initialization is invalid in C++");
7895     }
7896 
7897   /* If this field is empty (and not at the end of structure),
7898      don't do anything other than checking the initializer.  */
7899   if (field
7900       && (TREE_TYPE (field) == error_mark_node
7901             || (COMPLETE_TYPE_P (TREE_TYPE (field))
7902                 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
7903                 && (TREE_CODE (constructor_type) == ARRAY_TYPE
7904                       || DECL_CHAIN (field)))))
7905     return;
7906 
7907   if (semantic_type)
7908     value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
7909   value = digest_init (input_location, type, value, origtype, npc,
7910                            strict_string, require_constant_value);
7911   if (value == error_mark_node)
7912     {
7913       constructor_erroneous = 1;
7914       return;
7915     }
7916   if (require_constant_value || require_constant_elements)
7917     constant_expression_warning (value);
7918 
7919   /* If this element doesn't come next in sequence,
7920      put it on constructor_pending_elts.  */
7921   if (TREE_CODE (constructor_type) == ARRAY_TYPE
7922       && (!constructor_incremental
7923             || !tree_int_cst_equal (field, constructor_unfilled_index)))
7924     {
7925       if (constructor_incremental
7926             && tree_int_cst_lt (field, constructor_unfilled_index))
7927           set_nonincremental_init (braced_init_obstack);
7928 
7929       add_pending_init (field, value, origtype, implicit,
7930                               braced_init_obstack);
7931       return;
7932     }
7933   else if (TREE_CODE (constructor_type) == RECORD_TYPE
7934              && (!constructor_incremental
7935                  || field != constructor_unfilled_fields))
7936     {
7937       /* We do this for records but not for unions.  In a union,
7938            no matter which field is specified, it can be initialized
7939            right away since it starts at the beginning of the union.  */
7940       if (constructor_incremental)
7941           {
7942             if (!constructor_unfilled_fields)
7943               set_nonincremental_init (braced_init_obstack);
7944             else
7945               {
7946                 tree bitpos, unfillpos;
7947 
7948                 bitpos = bit_position (field);
7949                 unfillpos = bit_position (constructor_unfilled_fields);
7950 
7951                 if (tree_int_cst_lt (bitpos, unfillpos))
7952                     set_nonincremental_init (braced_init_obstack);
7953               }
7954           }
7955 
7956       add_pending_init (field, value, origtype, implicit,
7957                               braced_init_obstack);
7958       return;
7959     }
7960   else if (TREE_CODE (constructor_type) == UNION_TYPE
7961              && !VEC_empty (constructor_elt, constructor_elements))
7962     {
7963       if (!implicit)
7964           {
7965             if (TREE_SIDE_EFFECTS (VEC_last (constructor_elt,
7966                                                      constructor_elements)->value))
7967               warning_init (0,
7968                                 "initialized field with side-effects overwritten");
7969             else if (warn_override_init)
7970               warning_init (OPT_Woverride_init, "initialized field overwritten");
7971           }
7972 
7973       /* We can have just one union field set.  */
7974       constructor_elements = 0;
7975     }
7976 
7977   /* Otherwise, output this element either to
7978      constructor_elements or to the assembler file.  */
7979 
7980   celt = VEC_safe_push (constructor_elt, gc, constructor_elements, NULL);
7981   celt->index = field;
7982   celt->value = value;
7983 
7984   /* Advance the variable that indicates sequential elements output.  */
7985   if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7986     constructor_unfilled_index
7987       = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
7988                               bitsize_one_node);
7989   else if (TREE_CODE (constructor_type) == RECORD_TYPE)
7990     {
7991       constructor_unfilled_fields
7992           = DECL_CHAIN (constructor_unfilled_fields);
7993 
7994       /* Skip any nameless bit fields.  */
7995       while (constructor_unfilled_fields != 0
7996                && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7997                && DECL_NAME (constructor_unfilled_fields) == 0)
7998           constructor_unfilled_fields =
7999             DECL_CHAIN (constructor_unfilled_fields);
8000     }
8001   else if (TREE_CODE (constructor_type) == UNION_TYPE)
8002     constructor_unfilled_fields = 0;
8003 
8004   /* Now output any pending elements which have become next.  */
8005   if (pending)
8006     output_pending_init_elements (0, braced_init_obstack);
8007 }
8008 
8009 /* Output any pending elements which have become next.
8010    As we output elements, constructor_unfilled_{fields,index}
8011    advances, which may cause other elements to become next;
8012    if so, they too are output.
8013 
8014    If ALL is 0, we return when there are
8015    no more pending elements to output now.
8016 
8017    If ALL is 1, we output space as necessary so that
8018    we can output all the pending elements.  */
8019 static void
output_pending_init_elements(int all,struct obstack * braced_init_obstack)8020 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
8021 {
8022   struct init_node *elt = constructor_pending_elts;
8023   tree next;
8024 
8025  retry:
8026 
8027   /* Look through the whole pending tree.
8028      If we find an element that should be output now,
8029      output it.  Otherwise, set NEXT to the element
8030      that comes first among those still pending.  */
8031 
8032   next = 0;
8033   while (elt)
8034     {
8035       if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8036           {
8037             if (tree_int_cst_equal (elt->purpose,
8038                                           constructor_unfilled_index))
8039               output_init_element (elt->value, elt->origtype, true,
8040                                          TREE_TYPE (constructor_type),
8041                                          constructor_unfilled_index, 0, false,
8042                                          braced_init_obstack);
8043             else if (tree_int_cst_lt (constructor_unfilled_index,
8044                                             elt->purpose))
8045               {
8046                 /* Advance to the next smaller node.  */
8047                 if (elt->left)
8048                     elt = elt->left;
8049                 else
8050                     {
8051                       /* We have reached the smallest node bigger than the
8052                          current unfilled index.  Fill the space first.  */
8053                       next = elt->purpose;
8054                       break;
8055                     }
8056               }
8057             else
8058               {
8059                 /* Advance to the next bigger node.  */
8060                 if (elt->right)
8061                     elt = elt->right;
8062                 else
8063                     {
8064                       /* We have reached the biggest node in a subtree.  Find
8065                          the parent of it, which is the next bigger node.  */
8066                       while (elt->parent && elt->parent->right == elt)
8067                         elt = elt->parent;
8068                       elt = elt->parent;
8069                       if (elt && tree_int_cst_lt (constructor_unfilled_index,
8070                                                         elt->purpose))
8071                         {
8072                           next = elt->purpose;
8073                           break;
8074                         }
8075                     }
8076               }
8077           }
8078       else if (TREE_CODE (constructor_type) == RECORD_TYPE
8079                  || TREE_CODE (constructor_type) == UNION_TYPE)
8080           {
8081             tree ctor_unfilled_bitpos, elt_bitpos;
8082 
8083             /* If the current record is complete we are done.  */
8084             if (constructor_unfilled_fields == 0)
8085               break;
8086 
8087             ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8088             elt_bitpos = bit_position (elt->purpose);
8089             /* We can't compare fields here because there might be empty
8090                fields in between.  */
8091             if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8092               {
8093                 constructor_unfilled_fields = elt->purpose;
8094                 output_init_element (elt->value, elt->origtype, true,
8095                                            TREE_TYPE (elt->purpose),
8096                                            elt->purpose, 0, false,
8097                                            braced_init_obstack);
8098               }
8099             else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8100               {
8101                 /* Advance to the next smaller node.  */
8102                 if (elt->left)
8103                     elt = elt->left;
8104                 else
8105                     {
8106                       /* We have reached the smallest node bigger than the
8107                          current unfilled field.  Fill the space first.  */
8108                       next = elt->purpose;
8109                       break;
8110                     }
8111               }
8112             else
8113               {
8114                 /* Advance to the next bigger node.  */
8115                 if (elt->right)
8116                     elt = elt->right;
8117                 else
8118                     {
8119                       /* We have reached the biggest node in a subtree.  Find
8120                          the parent of it, which is the next bigger node.  */
8121                       while (elt->parent && elt->parent->right == elt)
8122                         elt = elt->parent;
8123                       elt = elt->parent;
8124                       if (elt
8125                           && (tree_int_cst_lt (ctor_unfilled_bitpos,
8126                                                      bit_position (elt->purpose))))
8127                         {
8128                           next = elt->purpose;
8129                           break;
8130                         }
8131                     }
8132               }
8133           }
8134     }
8135 
8136   /* Ordinarily return, but not if we want to output all
8137      and there are elements left.  */
8138   if (!(all && next != 0))
8139     return;
8140 
8141   /* If it's not incremental, just skip over the gap, so that after
8142      jumping to retry we will output the next successive element.  */
8143   if (TREE_CODE (constructor_type) == RECORD_TYPE
8144       || TREE_CODE (constructor_type) == UNION_TYPE)
8145     constructor_unfilled_fields = next;
8146   else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8147     constructor_unfilled_index = next;
8148 
8149   /* ELT now points to the node in the pending tree with the next
8150      initializer to output.  */
8151   goto retry;
8152 }
8153 
8154 /* Add one non-braced element to the current constructor level.
8155    This adjusts the current position within the constructor's type.
8156    This may also start or terminate implicit levels
8157    to handle a partly-braced initializer.
8158 
8159    Once this has found the correct level for the new element,
8160    it calls output_init_element.
8161 
8162    IMPLICIT is true if value comes from pop_init_level (1),
8163    the new initializer has been merged with the existing one
8164    and thus no warnings should be emitted about overriding an
8165    existing initializer.  */
8166 
8167 void
process_init_element(struct c_expr value,bool implicit,struct obstack * braced_init_obstack)8168 process_init_element (struct c_expr value, bool implicit,
8169                           struct obstack * braced_init_obstack)
8170 {
8171   tree orig_value = value.value;
8172   int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8173   bool strict_string = value.original_code == STRING_CST;
8174 
8175   designator_depth = 0;
8176   designator_erroneous = 0;
8177 
8178   /* Handle superfluous braces around string cst as in
8179      char x[] = {"foo"}; */
8180   if (string_flag
8181       && constructor_type
8182       && TREE_CODE (constructor_type) == ARRAY_TYPE
8183       && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
8184       && integer_zerop (constructor_unfilled_index))
8185     {
8186       if (constructor_stack->replacement_value.value)
8187           error_init ("excess elements in char array initializer");
8188       constructor_stack->replacement_value = value;
8189       return;
8190     }
8191 
8192   if (constructor_stack->replacement_value.value != 0)
8193     {
8194       error_init ("excess elements in struct initializer");
8195       return;
8196     }
8197 
8198   /* Ignore elements of a brace group if it is entirely superfluous
8199      and has already been diagnosed.  */
8200   if (constructor_type == 0)
8201     return;
8202 
8203   /* If we've exhausted any levels that didn't have braces,
8204      pop them now.  */
8205   while (constructor_stack->implicit)
8206     {
8207       if ((TREE_CODE (constructor_type) == RECORD_TYPE
8208              || TREE_CODE (constructor_type) == UNION_TYPE)
8209             && constructor_fields == 0)
8210           process_init_element (pop_init_level (1, braced_init_obstack),
8211                                     true, braced_init_obstack);
8212       else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
8213                   || TREE_CODE (constructor_type) == VECTOR_TYPE)
8214                  && (constructor_max_index == 0
8215                        || tree_int_cst_lt (constructor_max_index,
8216                                                constructor_index)))
8217           process_init_element (pop_init_level (1, braced_init_obstack),
8218                                     true, braced_init_obstack);
8219       else
8220           break;
8221     }
8222 
8223   /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once.  */
8224   if (constructor_range_stack)
8225     {
8226       /* If value is a compound literal and we'll be just using its
8227            content, don't put it into a SAVE_EXPR.  */
8228       if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
8229             || !require_constant_value
8230             || flag_isoc99)
8231           {
8232             tree semantic_type = NULL_TREE;
8233             if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
8234               {
8235                 semantic_type = TREE_TYPE (value.value);
8236                 value.value = TREE_OPERAND (value.value, 0);
8237               }
8238             value.value = c_save_expr (value.value);
8239             if (semantic_type)
8240               value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8241                                           value.value);
8242           }
8243     }
8244 
8245   while (1)
8246     {
8247       if (TREE_CODE (constructor_type) == RECORD_TYPE)
8248           {
8249             tree fieldtype;
8250             enum tree_code fieldcode;
8251 
8252             if (constructor_fields == 0)
8253               {
8254                 pedwarn_init (input_location, 0,
8255                                   "excess elements in struct initializer");
8256                 break;
8257               }
8258 
8259             fieldtype = TREE_TYPE (constructor_fields);
8260             if (fieldtype != error_mark_node)
8261               fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8262             fieldcode = TREE_CODE (fieldtype);
8263 
8264             /* Error for non-static initialization of a flexible array member.  */
8265             if (fieldcode == ARRAY_TYPE
8266                 && !require_constant_value
8267                 && TYPE_SIZE (fieldtype) == NULL_TREE
8268                 && DECL_CHAIN (constructor_fields) == NULL_TREE)
8269               {
8270                 error_init ("non-static initialization of a flexible array member");
8271                 break;
8272               }
8273 
8274             /* Accept a string constant to initialize a subarray.  */
8275             if (value.value != 0
8276                 && fieldcode == ARRAY_TYPE
8277                 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8278                 && string_flag)
8279               value.value = orig_value;
8280             /* Otherwise, if we have come to a subaggregate,
8281                and we don't have an element of its type, push into it.  */
8282             else if (value.value != 0
8283                        && value.value != error_mark_node
8284                        && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8285                        && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8286                            || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8287               {
8288                 push_init_level (1, braced_init_obstack);
8289                 continue;
8290               }
8291 
8292             if (value.value)
8293               {
8294                 push_member_name (constructor_fields);
8295                 output_init_element (value.value, value.original_type,
8296                                            strict_string, fieldtype,
8297                                            constructor_fields, 1, implicit,
8298                                            braced_init_obstack);
8299                 RESTORE_SPELLING_DEPTH (constructor_depth);
8300               }
8301             else
8302               /* Do the bookkeeping for an element that was
8303                  directly output as a constructor.  */
8304               {
8305                 /* For a record, keep track of end position of last field.  */
8306                 if (DECL_SIZE (constructor_fields))
8307                     constructor_bit_index
8308                       = size_binop_loc (input_location, PLUS_EXPR,
8309                                             bit_position (constructor_fields),
8310                                             DECL_SIZE (constructor_fields));
8311 
8312                 /* If the current field was the first one not yet written out,
8313                      it isn't now, so update.  */
8314                 if (constructor_unfilled_fields == constructor_fields)
8315                     {
8316                       constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8317                       /* Skip any nameless bit fields.  */
8318                       while (constructor_unfilled_fields != 0
8319                                && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8320                                && DECL_NAME (constructor_unfilled_fields) == 0)
8321                         constructor_unfilled_fields =
8322                           DECL_CHAIN (constructor_unfilled_fields);
8323                     }
8324               }
8325 
8326             constructor_fields = DECL_CHAIN (constructor_fields);
8327             /* Skip any nameless bit fields at the beginning.  */
8328             while (constructor_fields != 0
8329                      && DECL_C_BIT_FIELD (constructor_fields)
8330                      && DECL_NAME (constructor_fields) == 0)
8331               constructor_fields = DECL_CHAIN (constructor_fields);
8332           }
8333       else if (TREE_CODE (constructor_type) == UNION_TYPE)
8334           {
8335             tree fieldtype;
8336             enum tree_code fieldcode;
8337 
8338             if (constructor_fields == 0)
8339               {
8340                 pedwarn_init (input_location, 0,
8341                                   "excess elements in union initializer");
8342                 break;
8343               }
8344 
8345             fieldtype = TREE_TYPE (constructor_fields);
8346             if (fieldtype != error_mark_node)
8347               fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8348             fieldcode = TREE_CODE (fieldtype);
8349 
8350             /* Warn that traditional C rejects initialization of unions.
8351                We skip the warning if the value is zero.  This is done
8352                under the assumption that the zero initializer in user
8353                code appears conditioned on e.g. __STDC__ to avoid
8354                "missing initializer" warnings and relies on default
8355                initialization to zero in the traditional C case.
8356                We also skip the warning if the initializer is designated,
8357                again on the assumption that this must be conditional on
8358                __STDC__ anyway (and we've already complained about the
8359                member-designator already).  */
8360             if (!in_system_header && !constructor_designated
8361                 && !(value.value && (integer_zerop (value.value)
8362                                            || real_zerop (value.value))))
8363               warning (OPT_Wtraditional, "traditional C rejects initialization "
8364                          "of unions");
8365 
8366             /* Accept a string constant to initialize a subarray.  */
8367             if (value.value != 0
8368                 && fieldcode == ARRAY_TYPE
8369                 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8370                 && string_flag)
8371               value.value = orig_value;
8372             /* Otherwise, if we have come to a subaggregate,
8373                and we don't have an element of its type, push into it.  */
8374             else if (value.value != 0
8375                        && value.value != error_mark_node
8376                        && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8377                        && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8378                            || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8379               {
8380                 push_init_level (1, braced_init_obstack);
8381                 continue;
8382               }
8383 
8384             if (value.value)
8385               {
8386                 push_member_name (constructor_fields);
8387                 output_init_element (value.value, value.original_type,
8388                                            strict_string, fieldtype,
8389                                            constructor_fields, 1, implicit,
8390                                            braced_init_obstack);
8391                 RESTORE_SPELLING_DEPTH (constructor_depth);
8392               }
8393             else
8394               /* Do the bookkeeping for an element that was
8395                  directly output as a constructor.  */
8396               {
8397                 constructor_bit_index = DECL_SIZE (constructor_fields);
8398                 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8399               }
8400 
8401             constructor_fields = 0;
8402           }
8403       else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8404           {
8405             tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8406             enum tree_code eltcode = TREE_CODE (elttype);
8407 
8408             /* Accept a string constant to initialize a subarray.  */
8409             if (value.value != 0
8410                 && eltcode == ARRAY_TYPE
8411                 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
8412                 && string_flag)
8413               value.value = orig_value;
8414             /* Otherwise, if we have come to a subaggregate,
8415                and we don't have an element of its type, push into it.  */
8416             else if (value.value != 0
8417                        && value.value != error_mark_node
8418                        && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
8419                        && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
8420                            || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
8421               {
8422                 push_init_level (1, braced_init_obstack);
8423                 continue;
8424               }
8425 
8426             if (constructor_max_index != 0
8427                 && (tree_int_cst_lt (constructor_max_index, constructor_index)
8428                       || integer_all_onesp (constructor_max_index)))
8429               {
8430                 pedwarn_init (input_location, 0,
8431                                   "excess elements in array initializer");
8432                 break;
8433               }
8434 
8435             /* Now output the actual element.  */
8436             if (value.value)
8437               {
8438                 push_array_bounds (tree_low_cst (constructor_index, 1));
8439                 output_init_element (value.value, value.original_type,
8440                                            strict_string, elttype,
8441                                            constructor_index, 1, implicit,
8442                                            braced_init_obstack);
8443                 RESTORE_SPELLING_DEPTH (constructor_depth);
8444               }
8445 
8446             constructor_index
8447               = size_binop_loc (input_location, PLUS_EXPR,
8448                                     constructor_index, bitsize_one_node);
8449 
8450             if (!value.value)
8451               /* If we are doing the bookkeeping for an element that was
8452                  directly output as a constructor, we must update
8453                  constructor_unfilled_index.  */
8454               constructor_unfilled_index = constructor_index;
8455           }
8456       else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
8457           {
8458             tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8459 
8460            /* Do a basic check of initializer size.  Note that vectors
8461               always have a fixed size derived from their type.  */
8462             if (tree_int_cst_lt (constructor_max_index, constructor_index))
8463               {
8464                 pedwarn_init (input_location, 0,
8465                                   "excess elements in vector initializer");
8466                 break;
8467               }
8468 
8469             /* Now output the actual element.  */
8470             if (value.value)
8471               {
8472                 if (TREE_CODE (value.value) == VECTOR_CST)
8473                     elttype = TYPE_MAIN_VARIANT (constructor_type);
8474                 output_init_element (value.value, value.original_type,
8475                                            strict_string, elttype,
8476                                            constructor_index, 1, implicit,
8477                                            braced_init_obstack);
8478               }
8479 
8480             constructor_index
8481               = size_binop_loc (input_location,
8482                                     PLUS_EXPR, constructor_index, bitsize_one_node);
8483 
8484             if (!value.value)
8485               /* If we are doing the bookkeeping for an element that was
8486                  directly output as a constructor, we must update
8487                  constructor_unfilled_index.  */
8488               constructor_unfilled_index = constructor_index;
8489           }
8490 
8491       /* Handle the sole element allowed in a braced initializer
8492            for a scalar variable.  */
8493       else if (constructor_type != error_mark_node
8494                  && constructor_fields == 0)
8495           {
8496             pedwarn_init (input_location, 0,
8497                               "excess elements in scalar initializer");
8498             break;
8499           }
8500       else
8501           {
8502             if (value.value)
8503               output_init_element (value.value, value.original_type,
8504                                          strict_string, constructor_type,
8505                                          NULL_TREE, 1, implicit,
8506                                          braced_init_obstack);
8507             constructor_fields = 0;
8508           }
8509 
8510       /* Handle range initializers either at this level or anywhere higher
8511            in the designator stack.  */
8512       if (constructor_range_stack)
8513           {
8514             struct constructor_range_stack *p, *range_stack;
8515             int finish = 0;
8516 
8517             range_stack = constructor_range_stack;
8518             constructor_range_stack = 0;
8519             while (constructor_stack != range_stack->stack)
8520               {
8521                 gcc_assert (constructor_stack->implicit);
8522                 process_init_element (pop_init_level (1,
8523                                                                 braced_init_obstack),
8524                                             true, braced_init_obstack);
8525               }
8526             for (p = range_stack;
8527                  !p->range_end || tree_int_cst_equal (p->index, p->range_end);
8528                  p = p->prev)
8529               {
8530                 gcc_assert (constructor_stack->implicit);
8531                 process_init_element (pop_init_level (1, braced_init_obstack),
8532                                             true, braced_init_obstack);
8533               }
8534 
8535             p->index = size_binop_loc (input_location,
8536                                              PLUS_EXPR, p->index, bitsize_one_node);
8537             if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
8538               finish = 1;
8539 
8540             while (1)
8541               {
8542                 constructor_index = p->index;
8543                 constructor_fields = p->fields;
8544                 if (finish && p->range_end && p->index == p->range_start)
8545                     {
8546                       finish = 0;
8547                       p->prev = 0;
8548                     }
8549                 p = p->next;
8550                 if (!p)
8551                     break;
8552                 push_init_level (2, braced_init_obstack);
8553                 p->stack = constructor_stack;
8554                 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
8555                     p->index = p->range_start;
8556               }
8557 
8558             if (!finish)
8559               constructor_range_stack = range_stack;
8560             continue;
8561           }
8562 
8563       break;
8564     }
8565 
8566   constructor_range_stack = 0;
8567 }
8568 
8569 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
8570    (guaranteed to be 'volatile' or null) and ARGS (represented using
8571    an ASM_EXPR node).  */
8572 tree
build_asm_stmt(tree cv_qualifier,tree args)8573 build_asm_stmt (tree cv_qualifier, tree args)
8574 {
8575   if (!ASM_VOLATILE_P (args) && cv_qualifier)
8576     ASM_VOLATILE_P (args) = 1;
8577   return add_stmt (args);
8578 }
8579 
8580 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
8581    some INPUTS, and some CLOBBERS.  The latter three may be NULL.
8582    SIMPLE indicates whether there was anything at all after the
8583    string in the asm expression -- asm("blah") and asm("blah" : )
8584    are subtly different.  We use a ASM_EXPR node to represent this.  */
8585 tree
build_asm_expr(location_t loc,tree string,tree outputs,tree inputs,tree clobbers,tree labels,bool simple)8586 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
8587                     tree clobbers, tree labels, bool simple)
8588 {
8589   tree tail;
8590   tree args;
8591   int i;
8592   const char *constraint;
8593   const char **oconstraints;
8594   bool allows_mem, allows_reg, is_inout;
8595   int ninputs, noutputs;
8596 
8597   ninputs = list_length (inputs);
8598   noutputs = list_length (outputs);
8599   oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
8600 
8601   string = resolve_asm_operand_names (string, outputs, inputs, labels);
8602 
8603   /* Remove output conversions that change the type but not the mode.  */
8604   for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
8605     {
8606       tree output = TREE_VALUE (tail);
8607 
8608       /* ??? Really, this should not be here.  Users should be using a
8609            proper lvalue, dammit.  But there's a long history of using casts
8610            in the output operands.  In cases like longlong.h, this becomes a
8611            primitive form of typechecking -- if the cast can be removed, then
8612            the output operand had a type of the proper width; otherwise we'll
8613            get an error.  Gross, but ...  */
8614       STRIP_NOPS (output);
8615 
8616       if (!lvalue_or_else (loc, output, lv_asm))
8617           output = error_mark_node;
8618 
8619       if (output != error_mark_node
8620             && (TREE_READONLY (output)
8621                 || TYPE_READONLY (TREE_TYPE (output))
8622                 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
8623                        || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
8624                       && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
8625           readonly_error (output, lv_asm);
8626 
8627       constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
8628       oconstraints[i] = constraint;
8629 
8630       if (parse_output_constraint (&constraint, i, ninputs, noutputs,
8631                                            &allows_mem, &allows_reg, &is_inout))
8632           {
8633             /* If the operand is going to end up in memory,
8634                mark it addressable.  */
8635             if (!allows_reg && !c_mark_addressable (output))
8636               output = error_mark_node;
8637             if (!(!allows_reg && allows_mem)
8638                 && output != error_mark_node
8639                 && VOID_TYPE_P (TREE_TYPE (output)))
8640               {
8641                 error_at (loc, "invalid use of void expression");
8642                 output = error_mark_node;
8643               }
8644           }
8645       else
8646           output = error_mark_node;
8647 
8648       TREE_VALUE (tail) = output;
8649     }
8650 
8651   for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
8652     {
8653       tree input;
8654 
8655       constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
8656       input = TREE_VALUE (tail);
8657 
8658       if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
8659                                           oconstraints, &allows_mem, &allows_reg))
8660           {
8661             /* If the operand is going to end up in memory,
8662                mark it addressable.  */
8663             if (!allows_reg && allows_mem)
8664               {
8665                 /* Strip the nops as we allow this case.  FIXME, this really
8666                      should be rejected or made deprecated.  */
8667                 STRIP_NOPS (input);
8668                 if (!c_mark_addressable (input))
8669                     input = error_mark_node;
8670               }
8671             else if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
8672               {
8673                 error_at (loc, "invalid use of void expression");
8674                 input = error_mark_node;
8675               }
8676           }
8677       else
8678           input = error_mark_node;
8679 
8680       TREE_VALUE (tail) = input;
8681     }
8682 
8683   /* ASMs with labels cannot have outputs.  This should have been
8684      enforced by the parser.  */
8685   gcc_assert (outputs == NULL || labels == NULL);
8686 
8687   args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
8688 
8689   /* asm statements without outputs, including simple ones, are treated
8690      as volatile.  */
8691   ASM_INPUT_P (args) = simple;
8692   ASM_VOLATILE_P (args) = (noutputs == 0);
8693 
8694   return args;
8695 }
8696 
8697 /* Generate a goto statement to LABEL.  LOC is the location of the
8698    GOTO.  */
8699 
8700 tree
c_finish_goto_label(location_t loc,tree label)8701 c_finish_goto_label (location_t loc, tree label)
8702 {
8703   tree decl = lookup_label_for_goto (loc, label);
8704   if (!decl)
8705     return NULL_TREE;
8706   TREE_USED (decl) = 1;
8707   {
8708     tree t = build1 (GOTO_EXPR, void_type_node, decl);
8709     SET_EXPR_LOCATION (t, loc);
8710     return add_stmt (t);
8711   }
8712 }
8713 
8714 /* Generate a computed goto statement to EXPR.  LOC is the location of
8715    the GOTO.  */
8716 
8717 tree
c_finish_goto_ptr(location_t loc,tree expr)8718 c_finish_goto_ptr (location_t loc, tree expr)
8719 {
8720   tree t;
8721   pedwarn (loc, OPT_pedantic, "ISO C forbids %<goto *expr;%>");
8722   expr = c_fully_fold (expr, false, NULL);
8723   expr = convert (ptr_type_node, expr);
8724   t = build1 (GOTO_EXPR, void_type_node, expr);
8725   SET_EXPR_LOCATION (t, loc);
8726   return add_stmt (t);
8727 }
8728 
8729 /* Generate a C `return' statement.  RETVAL is the expression for what
8730    to return, or a null pointer for `return;' with no value.  LOC is
8731    the location of the return statement.  If ORIGTYPE is not NULL_TREE, it
8732    is the original type of RETVAL.  */
8733 
8734 tree
c_finish_return(location_t loc,tree retval,tree origtype)8735 c_finish_return (location_t loc, tree retval, tree origtype)
8736 {
8737   tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
8738   bool no_warning = false;
8739   bool npc = false;
8740 
8741   if (TREE_THIS_VOLATILE (current_function_decl))
8742     warning_at (loc, 0,
8743                     "function declared %<noreturn%> has a %<return%> statement");
8744 
8745   if (retval)
8746     {
8747       tree semantic_type = NULL_TREE;
8748       npc = null_pointer_constant_p (retval);
8749       if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
8750           {
8751             semantic_type = TREE_TYPE (retval);
8752             retval = TREE_OPERAND (retval, 0);
8753           }
8754       retval = c_fully_fold (retval, false, NULL);
8755       if (semantic_type)
8756           retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
8757     }
8758 
8759   if (!retval)
8760     {
8761       current_function_returns_null = 1;
8762       if ((warn_return_type || flag_isoc99)
8763             && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
8764           {
8765             pedwarn_c99 (loc, flag_isoc99 ? 0 : OPT_Wreturn_type,
8766                            "%<return%> with no value, in "
8767                            "function returning non-void");
8768             no_warning = true;
8769           }
8770     }
8771   else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
8772     {
8773       current_function_returns_null = 1;
8774       if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
8775           pedwarn (loc, 0,
8776                      "%<return%> with a value, in function returning void");
8777       else
8778           pedwarn (loc, OPT_pedantic, "ISO C forbids "
8779                      "%<return%> with expression, in function returning void");
8780     }
8781   else
8782     {
8783       tree t = convert_for_assignment (loc, valtype, retval, origtype,
8784                                                ic_return,
8785                                                npc, NULL_TREE, NULL_TREE, 0);
8786       tree res = DECL_RESULT (current_function_decl);
8787       tree inner;
8788       bool save;
8789 
8790       current_function_returns_value = 1;
8791       if (t == error_mark_node)
8792           return NULL_TREE;
8793 
8794       save = in_late_binary_op;
8795       if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
8796           || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE)
8797         in_late_binary_op = true;
8798       inner = t = convert (TREE_TYPE (res), t);
8799       in_late_binary_op = save;
8800 
8801       /* Strip any conversions, additions, and subtractions, and see if
8802            we are returning the address of a local variable.  Warn if so.  */
8803       while (1)
8804           {
8805             switch (TREE_CODE (inner))
8806               {
8807               CASE_CONVERT:
8808               case NON_LVALUE_EXPR:
8809               case PLUS_EXPR:
8810               case POINTER_PLUS_EXPR:
8811                 inner = TREE_OPERAND (inner, 0);
8812                 continue;
8813 
8814               case MINUS_EXPR:
8815                 /* If the second operand of the MINUS_EXPR has a pointer
8816                      type (or is converted from it), this may be valid, so
8817                      don't give a warning.  */
8818                 {
8819                     tree op1 = TREE_OPERAND (inner, 1);
8820 
8821                     while (!POINTER_TYPE_P (TREE_TYPE (op1))
8822                            && (CONVERT_EXPR_P (op1)
8823                                  || TREE_CODE (op1) == NON_LVALUE_EXPR))
8824                       op1 = TREE_OPERAND (op1, 0);
8825 
8826                     if (POINTER_TYPE_P (TREE_TYPE (op1)))
8827                       break;
8828 
8829                     inner = TREE_OPERAND (inner, 0);
8830                     continue;
8831                 }
8832 
8833               case ADDR_EXPR:
8834                 inner = TREE_OPERAND (inner, 0);
8835 
8836                 while (REFERENCE_CLASS_P (inner)
8837                          && TREE_CODE (inner) != INDIRECT_REF)
8838                     inner = TREE_OPERAND (inner, 0);
8839 
8840                 if (DECL_P (inner)
8841                       && !DECL_EXTERNAL (inner)
8842                       && !TREE_STATIC (inner)
8843                       && DECL_CONTEXT (inner) == current_function_decl)
8844                     warning_at (loc,
8845                                   0, "function returns address of local variable");
8846                 break;
8847 
8848               default:
8849                 break;
8850               }
8851 
8852             break;
8853           }
8854 
8855       retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
8856       SET_EXPR_LOCATION (retval, loc);
8857 
8858       if (warn_sequence_point)
8859           verify_sequence_points (retval);
8860     }
8861 
8862   ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
8863   TREE_NO_WARNING (ret_stmt) |= no_warning;
8864   return add_stmt (ret_stmt);
8865 }
8866 
8867 struct c_switch {
8868   /* The SWITCH_EXPR being built.  */
8869   tree switch_expr;
8870 
8871   /* The original type of the testing expression, i.e. before the
8872      default conversion is applied.  */
8873   tree orig_type;
8874 
8875   /* A splay-tree mapping the low element of a case range to the high
8876      element, or NULL_TREE if there is no high element.  Used to
8877      determine whether or not a new case label duplicates an old case
8878      label.  We need a tree, rather than simply a hash table, because
8879      of the GNU case range extension.  */
8880   splay_tree cases;
8881 
8882   /* The bindings at the point of the switch.  This is used for
8883      warnings crossing decls when branching to a case label.  */
8884   struct c_spot_bindings *bindings;
8885 
8886   /* The next node on the stack.  */
8887   struct c_switch *next;
8888 };
8889 
8890 /* A stack of the currently active switch statements.  The innermost
8891    switch statement is on the top of the stack.  There is no need to
8892    mark the stack for garbage collection because it is only active
8893    during the processing of the body of a function, and we never
8894    collect at that point.  */
8895 
8896 struct c_switch *c_switch_stack;
8897 
8898 /* Start a C switch statement, testing expression EXP.  Return the new
8899    SWITCH_EXPR.  SWITCH_LOC is the location of the `switch'.
8900    SWITCH_COND_LOC is the location of the switch's condition.  */
8901 
8902 tree
c_start_case(location_t switch_loc,location_t switch_cond_loc,tree exp)8903 c_start_case (location_t switch_loc,
8904                 location_t switch_cond_loc,
8905                 tree exp)
8906 {
8907   tree orig_type = error_mark_node;
8908   struct c_switch *cs;
8909 
8910   if (exp != error_mark_node)
8911     {
8912       orig_type = TREE_TYPE (exp);
8913 
8914       if (!INTEGRAL_TYPE_P (orig_type))
8915           {
8916             if (orig_type != error_mark_node)
8917               {
8918                 error_at (switch_cond_loc, "switch quantity not an integer");
8919                 orig_type = error_mark_node;
8920               }
8921             exp = integer_zero_node;
8922           }
8923       else
8924           {
8925             tree type = TYPE_MAIN_VARIANT (orig_type);
8926 
8927             if (!in_system_header
8928                 && (type == long_integer_type_node
8929                       || type == long_unsigned_type_node))
8930               warning_at (switch_cond_loc,
8931                               OPT_Wtraditional, "%<long%> switch expression not "
8932                               "converted to %<int%> in ISO C");
8933 
8934             exp = c_fully_fold (exp, false, NULL);
8935             exp = default_conversion (exp);
8936 
8937             if (warn_sequence_point)
8938               verify_sequence_points (exp);
8939           }
8940     }
8941 
8942   /* Add this new SWITCH_EXPR to the stack.  */
8943   cs = XNEW (struct c_switch);
8944   cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
8945   SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
8946   cs->orig_type = orig_type;
8947   cs->cases = splay_tree_new (case_compare, NULL, NULL);
8948   cs->bindings = c_get_switch_bindings ();
8949   cs->next = c_switch_stack;
8950   c_switch_stack = cs;
8951 
8952   return add_stmt (cs->switch_expr);
8953 }
8954 
8955 /* Process a case label at location LOC.  */
8956 
8957 tree
do_case(location_t loc,tree low_value,tree high_value)8958 do_case (location_t loc, tree low_value, tree high_value)
8959 {
8960   tree label = NULL_TREE;
8961 
8962   if (low_value && TREE_CODE (low_value) != INTEGER_CST)
8963     {
8964       low_value = c_fully_fold (low_value, false, NULL);
8965       if (TREE_CODE (low_value) == INTEGER_CST)
8966           pedwarn (input_location, OPT_pedantic,
8967                      "case label is not an integer constant expression");
8968     }
8969 
8970   if (high_value && TREE_CODE (high_value) != INTEGER_CST)
8971     {
8972       high_value = c_fully_fold (high_value, false, NULL);
8973       if (TREE_CODE (high_value) == INTEGER_CST)
8974           pedwarn (input_location, OPT_pedantic,
8975                      "case label is not an integer constant expression");
8976     }
8977 
8978   if (c_switch_stack == NULL)
8979     {
8980       if (low_value)
8981           error_at (loc, "case label not within a switch statement");
8982       else
8983           error_at (loc, "%<default%> label not within a switch statement");
8984       return NULL_TREE;
8985     }
8986 
8987   if (c_check_switch_jump_warnings (c_switch_stack->bindings,
8988                                             EXPR_LOCATION (c_switch_stack->switch_expr),
8989                                             loc))
8990     return NULL_TREE;
8991 
8992   label = c_add_case_label (loc, c_switch_stack->cases,
8993                                   SWITCH_COND (c_switch_stack->switch_expr),
8994                                   c_switch_stack->orig_type,
8995                                   low_value, high_value);
8996   if (label == error_mark_node)
8997     label = NULL_TREE;
8998   return label;
8999 }
9000 
9001 /* Finish the switch statement.  */
9002 
9003 void
c_finish_case(tree body)9004 c_finish_case (tree body)
9005 {
9006   struct c_switch *cs = c_switch_stack;
9007   location_t switch_location;
9008 
9009   SWITCH_BODY (cs->switch_expr) = body;
9010 
9011   /* Emit warnings as needed.  */
9012   switch_location = EXPR_LOCATION (cs->switch_expr);
9013   c_do_switch_warnings (cs->cases, switch_location,
9014                               TREE_TYPE (cs->switch_expr),
9015                               SWITCH_COND (cs->switch_expr));
9016 
9017   /* Pop the stack.  */
9018   c_switch_stack = cs->next;
9019   splay_tree_delete (cs->cases);
9020   c_release_switch_bindings (cs->bindings);
9021   XDELETE (cs);
9022 }
9023 
9024 /* Emit an if statement.  IF_LOCUS is the location of the 'if'.  COND,
9025    THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
9026    may be null.  NESTED_IF is true if THEN_BLOCK contains another IF
9027    statement, and was not surrounded with parenthesis.  */
9028 
9029 void
c_finish_if_stmt(location_t if_locus,tree cond,tree then_block,tree else_block,bool nested_if)9030 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
9031                       tree else_block, bool nested_if)
9032 {
9033   tree stmt;
9034 
9035   /* Diagnose an ambiguous else if if-then-else is nested inside if-then.  */
9036   if (warn_parentheses && nested_if && else_block == NULL)
9037     {
9038       tree inner_if = then_block;
9039 
9040       /* We know from the grammar productions that there is an IF nested
9041            within THEN_BLOCK.  Due to labels and c99 conditional declarations,
9042            it might not be exactly THEN_BLOCK, but should be the last
9043            non-container statement within.  */
9044       while (1)
9045           switch (TREE_CODE (inner_if))
9046             {
9047             case COND_EXPR:
9048               goto found;
9049             case BIND_EXPR:
9050               inner_if = BIND_EXPR_BODY (inner_if);
9051               break;
9052             case STATEMENT_LIST:
9053               inner_if = expr_last (then_block);
9054               break;
9055             case TRY_FINALLY_EXPR:
9056             case TRY_CATCH_EXPR:
9057               inner_if = TREE_OPERAND (inner_if, 0);
9058               break;
9059             default:
9060               gcc_unreachable ();
9061             }
9062     found:
9063 
9064       if (COND_EXPR_ELSE (inner_if))
9065            warning_at (if_locus, OPT_Wparentheses,
9066                          "suggest explicit braces to avoid ambiguous %<else%>");
9067     }
9068 
9069   stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
9070   SET_EXPR_LOCATION (stmt, if_locus);
9071   add_stmt (stmt);
9072 }
9073 
9074 /* Emit a general-purpose loop construct.  START_LOCUS is the location of
9075    the beginning of the loop.  COND is the loop condition.  COND_IS_FIRST
9076    is false for DO loops.  INCR is the FOR increment expression.  BODY is
9077    the statement controlled by the loop.  BLAB is the break label.  CLAB is
9078    the continue label.  Everything is allowed to be NULL.  */
9079 
9080 void
c_finish_loop(location_t start_locus,tree cond,tree incr,tree body,tree blab,tree clab,bool cond_is_first)9081 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
9082                  tree blab, tree clab, bool cond_is_first)
9083 {
9084   tree entry = NULL, exit = NULL, t;
9085 
9086   /* If the condition is zero don't generate a loop construct.  */
9087   if (cond && integer_zerop (cond))
9088     {
9089       if (cond_is_first)
9090           {
9091             t = build_and_jump (&blab);
9092             SET_EXPR_LOCATION (t, start_locus);
9093             add_stmt (t);
9094           }
9095     }
9096   else
9097     {
9098       tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9099 
9100       /* If we have an exit condition, then we build an IF with gotos either
9101            out of the loop, or to the top of it.  If there's no exit condition,
9102            then we just build a jump back to the top.  */
9103       exit = build_and_jump (&LABEL_EXPR_LABEL (top));
9104 
9105       if (cond && !integer_nonzerop (cond))
9106           {
9107             /* Canonicalize the loop condition to the end.  This means
9108                generating a branch to the loop condition.  Reuse the
9109                continue label, if possible.  */
9110             if (cond_is_first)
9111               {
9112                 if (incr || !clab)
9113                     {
9114                       entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9115                       t = build_and_jump (&LABEL_EXPR_LABEL (entry));
9116                     }
9117                 else
9118                     t = build1 (GOTO_EXPR, void_type_node, clab);
9119                 SET_EXPR_LOCATION (t, start_locus);
9120                 add_stmt (t);
9121               }
9122 
9123             t = build_and_jump (&blab);
9124             if (cond_is_first)
9125               exit = fold_build3_loc (start_locus,
9126                                         COND_EXPR, void_type_node, cond, exit, t);
9127             else
9128               exit = fold_build3_loc (input_location,
9129                                         COND_EXPR, void_type_node, cond, exit, t);
9130           }
9131 
9132       add_stmt (top);
9133     }
9134 
9135   if (body)
9136     add_stmt (body);
9137   if (clab)
9138     add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
9139   if (incr)
9140     add_stmt (incr);
9141   if (entry)
9142     add_stmt (entry);
9143   if (exit)
9144     add_stmt (exit);
9145   if (blab)
9146     add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
9147 }
9148 
9149 tree
c_finish_bc_stmt(location_t loc,tree * label_p,bool is_break)9150 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
9151 {
9152   bool skip;
9153   tree label = *label_p;
9154 
9155   /* In switch statements break is sometimes stylistically used after
9156      a return statement.  This can lead to spurious warnings about
9157      control reaching the end of a non-void function when it is
9158      inlined.  Note that we are calling block_may_fallthru with
9159      language specific tree nodes; this works because
9160      block_may_fallthru returns true when given something it does not
9161      understand.  */
9162   skip = !block_may_fallthru (cur_stmt_list);
9163 
9164   if (!label)
9165     {
9166       if (!skip)
9167           *label_p = label = create_artificial_label (loc);
9168     }
9169   else if (TREE_CODE (label) == LABEL_DECL)
9170     ;
9171   else switch (TREE_INT_CST_LOW (label))
9172     {
9173     case 0:
9174       if (is_break)
9175           error_at (loc, "break statement not within loop or switch");
9176       else
9177           error_at (loc, "continue statement not within a loop");
9178       return NULL_TREE;
9179 
9180     case 1:
9181       gcc_assert (is_break);
9182       error_at (loc, "break statement used with OpenMP for loop");
9183       return NULL_TREE;
9184 
9185     default:
9186       gcc_unreachable ();
9187     }
9188 
9189   if (skip)
9190     return NULL_TREE;
9191 
9192   if (!is_break)
9193     add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
9194 
9195   return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
9196 }
9197 
9198 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr.  */
9199 
9200 static void
emit_side_effect_warnings(location_t loc,tree expr)9201 emit_side_effect_warnings (location_t loc, tree expr)
9202 {
9203   if (expr == error_mark_node)
9204     ;
9205   else if (!TREE_SIDE_EFFECTS (expr))
9206     {
9207       if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
9208           warning_at (loc, OPT_Wunused_value, "statement with no effect");
9209     }
9210   else
9211     warn_if_unused_value (expr, loc);
9212 }
9213 
9214 /* Process an expression as if it were a complete statement.  Emit
9215    diagnostics, but do not call ADD_STMT.  LOC is the location of the
9216    statement.  */
9217 
9218 tree
c_process_expr_stmt(location_t loc,tree expr)9219 c_process_expr_stmt (location_t loc, tree expr)
9220 {
9221   tree exprv;
9222 
9223   if (!expr)
9224     return NULL_TREE;
9225 
9226   expr = c_fully_fold (expr, false, NULL);
9227 
9228   if (warn_sequence_point)
9229     verify_sequence_points (expr);
9230 
9231   if (TREE_TYPE (expr) != error_mark_node
9232       && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
9233       && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
9234     error_at (loc, "expression statement has incomplete type");
9235 
9236   /* If we're not processing a statement expression, warn about unused values.
9237      Warnings for statement expressions will be emitted later, once we figure
9238      out which is the result.  */
9239   if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9240       && warn_unused_value)
9241     emit_side_effect_warnings (loc, expr);
9242 
9243   exprv = expr;
9244   while (TREE_CODE (exprv) == COMPOUND_EXPR)
9245     exprv = TREE_OPERAND (exprv, 1);
9246   while (CONVERT_EXPR_P (exprv))
9247     exprv = TREE_OPERAND (exprv, 0);
9248   if (DECL_P (exprv)
9249       || handled_component_p (exprv)
9250       || TREE_CODE (exprv) == ADDR_EXPR)
9251     mark_exp_read (exprv);
9252 
9253   /* If the expression is not of a type to which we cannot assign a line
9254      number, wrap the thing in a no-op NOP_EXPR.  */
9255   if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
9256     {
9257       expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9258       SET_EXPR_LOCATION (expr, loc);
9259     }
9260 
9261   return expr;
9262 }
9263 
9264 /* Emit an expression as a statement.  LOC is the location of the
9265    expression.  */
9266 
9267 tree
c_finish_expr_stmt(location_t loc,tree expr)9268 c_finish_expr_stmt (location_t loc, tree expr)
9269 {
9270   if (expr)
9271     return add_stmt (c_process_expr_stmt (loc, expr));
9272   else
9273     return NULL;
9274 }
9275 
9276 /* Do the opposite and emit a statement as an expression.  To begin,
9277    create a new binding level and return it.  */
9278 
9279 tree
c_begin_stmt_expr(void)9280 c_begin_stmt_expr (void)
9281 {
9282   tree ret;
9283 
9284   /* We must force a BLOCK for this level so that, if it is not expanded
9285      later, there is a way to turn off the entire subtree of blocks that
9286      are contained in it.  */
9287   keep_next_level ();
9288   ret = c_begin_compound_stmt (true);
9289 
9290   c_bindings_start_stmt_expr (c_switch_stack == NULL
9291                                     ? NULL
9292                                     : c_switch_stack->bindings);
9293 
9294   /* Mark the current statement list as belonging to a statement list.  */
9295   STATEMENT_LIST_STMT_EXPR (ret) = 1;
9296 
9297   return ret;
9298 }
9299 
9300 /* LOC is the location of the compound statement to which this body
9301    belongs.  */
9302 
9303 tree
c_finish_stmt_expr(location_t loc,tree body)9304 c_finish_stmt_expr (location_t loc, tree body)
9305 {
9306   tree last, type, tmp, val;
9307   tree *last_p;
9308 
9309   body = c_end_compound_stmt (loc, body, true);
9310 
9311   c_bindings_end_stmt_expr (c_switch_stack == NULL
9312                                   ? NULL
9313                                   : c_switch_stack->bindings);
9314 
9315   /* Locate the last statement in BODY.  See c_end_compound_stmt
9316      about always returning a BIND_EXPR.  */
9317   last_p = &BIND_EXPR_BODY (body);
9318   last = BIND_EXPR_BODY (body);
9319 
9320  continue_searching:
9321   if (TREE_CODE (last) == STATEMENT_LIST)
9322     {
9323       tree_stmt_iterator i;
9324 
9325       /* This can happen with degenerate cases like ({ }).  No value.  */
9326       if (!TREE_SIDE_EFFECTS (last))
9327           return body;
9328 
9329       /* If we're supposed to generate side effects warnings, process
9330            all of the statements except the last.  */
9331       if (warn_unused_value)
9332           {
9333             for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
9334               {
9335                 location_t tloc;
9336                 tree t = tsi_stmt (i);
9337 
9338                 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
9339                 emit_side_effect_warnings (tloc, t);
9340               }
9341           }
9342       else
9343           i = tsi_last (last);
9344       last_p = tsi_stmt_ptr (i);
9345       last = *last_p;
9346     }
9347 
9348   /* If the end of the list is exception related, then the list was split
9349      by a call to push_cleanup.  Continue searching.  */
9350   if (TREE_CODE (last) == TRY_FINALLY_EXPR
9351       || TREE_CODE (last) == TRY_CATCH_EXPR)
9352     {
9353       last_p = &TREE_OPERAND (last, 0);
9354       last = *last_p;
9355       goto continue_searching;
9356     }
9357 
9358   if (last == error_mark_node)
9359     return last;
9360 
9361   /* In the case that the BIND_EXPR is not necessary, return the
9362      expression out from inside it.  */
9363   if (last == BIND_EXPR_BODY (body)
9364       && BIND_EXPR_VARS (body) == NULL)
9365     {
9366       /* Even if this looks constant, do not allow it in a constant
9367            expression.  */
9368       last = c_wrap_maybe_const (last, true);
9369       /* Do not warn if the return value of a statement expression is
9370            unused.  */
9371       TREE_NO_WARNING (last) = 1;
9372       return last;
9373     }
9374 
9375   /* Extract the type of said expression.  */
9376   type = TREE_TYPE (last);
9377 
9378   /* If we're not returning a value at all, then the BIND_EXPR that
9379      we already have is a fine expression to return.  */
9380   if (!type || VOID_TYPE_P (type))
9381     return body;
9382 
9383   /* Now that we've located the expression containing the value, it seems
9384      silly to make voidify_wrapper_expr repeat the process.  Create a
9385      temporary of the appropriate type and stick it in a TARGET_EXPR.  */
9386   tmp = create_tmp_var_raw (type, NULL);
9387 
9388   /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt.  This avoids
9389      tree_expr_nonnegative_p giving up immediately.  */
9390   val = last;
9391   if (TREE_CODE (val) == NOP_EXPR
9392       && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
9393     val = TREE_OPERAND (val, 0);
9394 
9395   *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
9396   SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
9397 
9398   {
9399     tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
9400     SET_EXPR_LOCATION (t, loc);
9401     return t;
9402   }
9403 }
9404 
9405 /* Begin and end compound statements.  This is as simple as pushing
9406    and popping new statement lists from the tree.  */
9407 
9408 tree
c_begin_compound_stmt(bool do_scope)9409 c_begin_compound_stmt (bool do_scope)
9410 {
9411   tree stmt = push_stmt_list ();
9412   if (do_scope)
9413     push_scope ();
9414   return stmt;
9415 }
9416 
9417 /* End a compound statement.  STMT is the statement.  LOC is the
9418    location of the compound statement-- this is usually the location
9419    of the opening brace.  */
9420 
9421 tree
c_end_compound_stmt(location_t loc,tree stmt,bool do_scope)9422 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
9423 {
9424   tree block = NULL;
9425 
9426   if (do_scope)
9427     {
9428       if (c_dialect_objc ())
9429           objc_clear_super_receiver ();
9430       block = pop_scope ();
9431     }
9432 
9433   stmt = pop_stmt_list (stmt);
9434   stmt = c_build_bind_expr (loc, block, stmt);
9435 
9436   /* If this compound statement is nested immediately inside a statement
9437      expression, then force a BIND_EXPR to be created.  Otherwise we'll
9438      do the wrong thing for ({ { 1; } }) or ({ 1; { } }).  In particular,
9439      STATEMENT_LISTs merge, and thus we can lose track of what statement
9440      was really last.  */
9441   if (building_stmt_list_p ()
9442       && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9443       && TREE_CODE (stmt) != BIND_EXPR)
9444     {
9445       stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
9446       TREE_SIDE_EFFECTS (stmt) = 1;
9447       SET_EXPR_LOCATION (stmt, loc);
9448     }
9449 
9450   return stmt;
9451 }
9452 
9453 /* Queue a cleanup.  CLEANUP is an expression/statement to be executed
9454    when the current scope is exited.  EH_ONLY is true when this is not
9455    meant to apply to normal control flow transfer.  */
9456 
9457 void
push_cleanup(tree decl,tree cleanup,bool eh_only)9458 push_cleanup (tree decl, tree cleanup, bool eh_only)
9459 {
9460   enum tree_code code;
9461   tree stmt, list;
9462   bool stmt_expr;
9463 
9464   code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
9465   stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
9466   add_stmt (stmt);
9467   stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
9468   list = push_stmt_list ();
9469   TREE_OPERAND (stmt, 0) = list;
9470   STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
9471 }
9472 
9473 /* Convert scalar to vector for the range of operations.  */
9474 static enum stv_conv
scalar_to_vector(location_t loc,enum tree_code code,tree op0,tree op1)9475 scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1)
9476 {
9477   tree type0 = TREE_TYPE (op0);
9478   tree type1 = TREE_TYPE (op1);
9479   bool integer_only_op = false;
9480   enum stv_conv ret = stv_firstarg;
9481 
9482   gcc_assert (TREE_CODE (type0) == VECTOR_TYPE
9483                 || TREE_CODE (type1) == VECTOR_TYPE);
9484   switch (code)
9485     {
9486       case RSHIFT_EXPR:
9487       case LSHIFT_EXPR:
9488           if (TREE_CODE (type0) == INTEGER_TYPE
9489               && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
9490             {
9491               if (unsafe_conversion_p (TREE_TYPE (type1), op0, false))
9492                 {
9493                     error_at (loc, "conversion of scalar to vector "
9494                                      "involves truncation");
9495                     return stv_error;
9496                 }
9497               else
9498                 return stv_firstarg;
9499             }
9500           break;
9501 
9502       case BIT_IOR_EXPR:
9503       case BIT_XOR_EXPR:
9504       case BIT_AND_EXPR:
9505           integer_only_op = true;
9506           /* ... fall through ...  */
9507 
9508       case PLUS_EXPR:
9509       case MINUS_EXPR:
9510       case MULT_EXPR:
9511       case TRUNC_DIV_EXPR:
9512       case TRUNC_MOD_EXPR:
9513       case RDIV_EXPR:
9514           if (TREE_CODE (type0) == VECTOR_TYPE)
9515             {
9516               tree tmp;
9517               ret = stv_secondarg;
9518               /* Swap TYPE0 with TYPE1 and OP0 with OP1  */
9519               tmp = type0; type0 = type1; type1 = tmp;
9520               tmp = op0; op0 = op1; op1 = tmp;
9521             }
9522 
9523           if (TREE_CODE (type0) == INTEGER_TYPE
9524               && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
9525             {
9526               if (unsafe_conversion_p (TREE_TYPE (type1), op0, false))
9527                 {
9528                     error_at (loc, "conversion of scalar to vector "
9529                                      "involves truncation");
9530                     return stv_error;
9531                 }
9532               return ret;
9533             }
9534           else if (!integer_only_op
9535                         /* Allow integer --> real conversion if safe.  */
9536                      && (TREE_CODE (type0) == REAL_TYPE
9537                          || TREE_CODE (type0) == INTEGER_TYPE)
9538                      && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
9539             {
9540               if (unsafe_conversion_p (TREE_TYPE (type1), op0, false))
9541                 {
9542                     error_at (loc, "conversion of scalar to vector "
9543                                      "involves truncation");
9544                     return stv_error;
9545                 }
9546               return ret;
9547             }
9548       default:
9549           break;
9550     }
9551 
9552   return stv_nothing;
9553 }
9554 
9555 /* Build a binary-operation expression without default conversions.
9556    CODE is the kind of expression to build.
9557    LOCATION is the operator's location.
9558    This function differs from `build' in several ways:
9559    the data type of the result is computed and recorded in it,
9560    warnings are generated if arg data types are invalid,
9561    special handling for addition and subtraction of pointers is known,
9562    and some optimization is done (operations on narrow ints
9563    are done in the narrower type when that gives the same result).
9564    Constant folding is also done before the result is returned.
9565 
9566    Note that the operands will never have enumeral types, or function
9567    or array types, because either they will have the default conversions
9568    performed or they have both just been converted to some other type in which
9569    the arithmetic is to be done.  */
9570 
9571 tree
build_binary_op(location_t location,enum tree_code code,tree orig_op0,tree orig_op1,int convert_p)9572 build_binary_op (location_t location, enum tree_code code,
9573                      tree orig_op0, tree orig_op1, int convert_p)
9574 {
9575   tree type0, type1, orig_type0, orig_type1;
9576   tree eptype;
9577   enum tree_code code0, code1;
9578   tree op0, op1;
9579   tree ret = error_mark_node;
9580   const char *invalid_op_diag;
9581   bool op0_int_operands, op1_int_operands;
9582   bool int_const, int_const_or_overflow, int_operands;
9583 
9584   /* Expression code to give to the expression when it is built.
9585      Normally this is CODE, which is what the caller asked for,
9586      but in some special cases we change it.  */
9587   enum tree_code resultcode = code;
9588 
9589   /* Data type in which the computation is to be performed.
9590      In the simplest cases this is the common type of the arguments.  */
9591   tree result_type = NULL;
9592 
9593   /* When the computation is in excess precision, the type of the
9594      final EXCESS_PRECISION_EXPR.  */
9595   tree semantic_result_type = NULL;
9596 
9597   /* Nonzero means operands have already been type-converted
9598      in whatever way is necessary.
9599      Zero means they need to be converted to RESULT_TYPE.  */
9600   int converted = 0;
9601 
9602   /* Nonzero means create the expression with this type, rather than
9603      RESULT_TYPE.  */
9604   tree build_type = 0;
9605 
9606   /* Nonzero means after finally constructing the expression
9607      convert it to this type.  */
9608   tree final_type = 0;
9609 
9610   /* Nonzero if this is an operation like MIN or MAX which can
9611      safely be computed in short if both args are promoted shorts.
9612      Also implies COMMON.
9613      -1 indicates a bitwise operation; this makes a difference
9614      in the exact conditions for when it is safe to do the operation
9615      in a narrower mode.  */
9616   int shorten = 0;
9617 
9618   /* Nonzero if this is a comparison operation;
9619      if both args are promoted shorts, compare the original shorts.
9620      Also implies COMMON.  */
9621   int short_compare = 0;
9622 
9623   /* Nonzero if this is a right-shift operation, which can be computed on the
9624      original short and then promoted if the operand is a promoted short.  */
9625   int short_shift = 0;
9626 
9627   /* Nonzero means set RESULT_TYPE to the common type of the args.  */
9628   int common = 0;
9629 
9630   /* True means types are compatible as far as ObjC is concerned.  */
9631   bool objc_ok;
9632 
9633   /* True means this is an arithmetic operation that may need excess
9634      precision.  */
9635   bool may_need_excess_precision;
9636 
9637   /* True means this is a boolean operation that converts both its
9638      operands to truth-values.  */
9639   bool boolean_op = false;
9640 
9641   if (location == UNKNOWN_LOCATION)
9642     location = input_location;
9643 
9644   op0 = orig_op0;
9645   op1 = orig_op1;
9646 
9647   op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
9648   if (op0_int_operands)
9649     op0 = remove_c_maybe_const_expr (op0);
9650   op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
9651   if (op1_int_operands)
9652     op1 = remove_c_maybe_const_expr (op1);
9653   int_operands = (op0_int_operands && op1_int_operands);
9654   if (int_operands)
9655     {
9656       int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
9657                                      && TREE_CODE (orig_op1) == INTEGER_CST);
9658       int_const = (int_const_or_overflow
9659                        && !TREE_OVERFLOW (orig_op0)
9660                        && !TREE_OVERFLOW (orig_op1));
9661     }
9662   else
9663     int_const = int_const_or_overflow = false;
9664 
9665   /* Do not apply default conversion in mixed vector/scalar expression.  */
9666   if (convert_p
9667       && !((TREE_CODE (TREE_TYPE (op0)) == VECTOR_TYPE)
9668              != (TREE_CODE (TREE_TYPE (op1)) == VECTOR_TYPE)))
9669     {
9670       op0 = default_conversion (op0);
9671       op1 = default_conversion (op1);
9672     }
9673 
9674   orig_type0 = type0 = TREE_TYPE (op0);
9675   orig_type1 = type1 = TREE_TYPE (op1);
9676 
9677   /* The expression codes of the data types of the arguments tell us
9678      whether the arguments are integers, floating, pointers, etc.  */
9679   code0 = TREE_CODE (type0);
9680   code1 = TREE_CODE (type1);
9681 
9682   /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue.  */
9683   STRIP_TYPE_NOPS (op0);
9684   STRIP_TYPE_NOPS (op1);
9685 
9686   /* If an error was already reported for one of the arguments,
9687      avoid reporting another error.  */
9688 
9689   if (code0 == ERROR_MARK || code1 == ERROR_MARK)
9690     return error_mark_node;
9691 
9692   if ((invalid_op_diag
9693        = targetm.invalid_binary_op (code, type0, type1)))
9694     {
9695       error_at (location, invalid_op_diag);
9696       return error_mark_node;
9697     }
9698 
9699   switch (code)
9700     {
9701     case PLUS_EXPR:
9702     case MINUS_EXPR:
9703     case MULT_EXPR:
9704     case TRUNC_DIV_EXPR:
9705     case CEIL_DIV_EXPR:
9706     case FLOOR_DIV_EXPR:
9707     case ROUND_DIV_EXPR:
9708     case EXACT_DIV_EXPR:
9709       may_need_excess_precision = true;
9710       break;
9711     default:
9712       may_need_excess_precision = false;
9713       break;
9714     }
9715   if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
9716     {
9717       op0 = TREE_OPERAND (op0, 0);
9718       type0 = TREE_TYPE (op0);
9719     }
9720   else if (may_need_excess_precision
9721              && (eptype = excess_precision_type (type0)) != NULL_TREE)
9722     {
9723       type0 = eptype;
9724       op0 = convert (eptype, op0);
9725     }
9726   if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
9727     {
9728       op1 = TREE_OPERAND (op1, 0);
9729       type1 = TREE_TYPE (op1);
9730     }
9731   else if (may_need_excess_precision
9732              && (eptype = excess_precision_type (type1)) != NULL_TREE)
9733     {
9734       type1 = eptype;
9735       op1 = convert (eptype, op1);
9736     }
9737 
9738   objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
9739 
9740   /* In case when one of the operands of the binary operation is
9741      a vector and another is a scalar -- convert scalar to vector.  */
9742   if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
9743     {
9744       enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1);
9745 
9746       switch (convert_flag)
9747           {
9748             case stv_error:
9749               return error_mark_node;
9750             case stv_firstarg:
9751               {
9752               bool maybe_const = true;
9753               tree sc;
9754               sc = c_fully_fold (op0, false, &maybe_const);
9755               sc = save_expr (sc);
9756               sc = convert (TREE_TYPE (type1), sc);
9757               op0 = build_vector_from_val (type1, sc);
9758               if (!maybe_const)
9759                 op0 = c_wrap_maybe_const (op0, true);
9760               orig_type0 = type0 = TREE_TYPE (op0);
9761               code0 = TREE_CODE (type0);
9762               converted = 1;
9763               break;
9764               }
9765             case stv_secondarg:
9766               {
9767                 bool maybe_const = true;
9768                 tree sc;
9769                 sc = c_fully_fold (op1, false, &maybe_const);
9770                 sc = save_expr (sc);
9771                 sc = convert (TREE_TYPE (type0), sc);
9772                 op1 = build_vector_from_val (type0, sc);
9773                 if (!maybe_const)
9774                     op1 = c_wrap_maybe_const (op1, true);
9775                 orig_type1 = type1 = TREE_TYPE (op1);
9776                 code1 = TREE_CODE (type1);
9777                 converted = 1;
9778                 break;
9779               }
9780             default:
9781               break;
9782           }
9783     }
9784 
9785   switch (code)
9786     {
9787     case PLUS_EXPR:
9788       /* Handle the pointer + int case.  */
9789       if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9790           {
9791             ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
9792             goto return_build_binary_op;
9793           }
9794       else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
9795           {
9796             ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
9797             goto return_build_binary_op;
9798           }
9799       else
9800           common = 1;
9801       break;
9802 
9803     case MINUS_EXPR:
9804       /* Subtraction of two similar pointers.
9805            We must subtract them as integers, then divide by object size.  */
9806       if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
9807             && comp_target_types (location, type0, type1))
9808           {
9809             ret = pointer_diff (location, op0, op1);
9810             goto return_build_binary_op;
9811           }
9812       /* Handle pointer minus int.  Just like pointer plus int.  */
9813       else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9814           {
9815             ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
9816             goto return_build_binary_op;
9817           }
9818       else
9819           common = 1;
9820       break;
9821 
9822     case MULT_EXPR:
9823       common = 1;
9824       break;
9825 
9826     case TRUNC_DIV_EXPR:
9827     case CEIL_DIV_EXPR:
9828     case FLOOR_DIV_EXPR:
9829     case ROUND_DIV_EXPR:
9830     case EXACT_DIV_EXPR:
9831       warn_for_div_by_zero (location, op1);
9832 
9833       if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9834              || code0 == FIXED_POINT_TYPE
9835              || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
9836             && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9837                 || code1 == FIXED_POINT_TYPE
9838                 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
9839           {
9840             enum tree_code tcode0 = code0, tcode1 = code1;
9841 
9842             if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
9843               tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
9844             if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
9845               tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
9846 
9847             if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
9848                 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
9849               resultcode = RDIV_EXPR;
9850             else
9851               /* Although it would be tempting to shorten always here, that
9852                  loses on some targets, since the modulo instruction is
9853                  undefined if the quotient can't be represented in the
9854                  computation mode.  We shorten only if unsigned or if
9855                  dividing by something we know != -1.  */
9856               shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
9857                            || (TREE_CODE (op1) == INTEGER_CST
9858                                  && !integer_all_onesp (op1)));
9859             common = 1;
9860           }
9861       break;
9862 
9863     case BIT_AND_EXPR:
9864     case BIT_IOR_EXPR:
9865     case BIT_XOR_EXPR:
9866       if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
9867           shorten = -1;
9868       /* Allow vector types which are not floating point types.   */
9869       else if (code0 == VECTOR_TYPE
9870                  && code1 == VECTOR_TYPE
9871                  && !VECTOR_FLOAT_TYPE_P (type0)
9872                  && !VECTOR_FLOAT_TYPE_P (type1))
9873           common = 1;
9874       break;
9875 
9876     case TRUNC_MOD_EXPR:
9877     case FLOOR_MOD_EXPR:
9878       warn_for_div_by_zero (location, op1);
9879 
9880       if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9881             && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9882             && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
9883           common = 1;
9884       else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
9885           {
9886             /* Although it would be tempting to shorten always here, that loses
9887                on some targets, since the modulo instruction is undefined if the
9888                quotient can't be represented in the computation mode.  We shorten
9889                only if unsigned or if dividing by something we know != -1.  */
9890             shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
9891                          || (TREE_CODE (op1) == INTEGER_CST
9892                                && !integer_all_onesp (op1)));
9893             common = 1;
9894           }
9895       break;
9896 
9897     case TRUTH_ANDIF_EXPR:
9898     case TRUTH_ORIF_EXPR:
9899     case TRUTH_AND_EXPR:
9900     case TRUTH_OR_EXPR:
9901     case TRUTH_XOR_EXPR:
9902       if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
9903              || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
9904              || code0 == FIXED_POINT_TYPE)
9905             && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
9906                 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
9907                 || code1 == FIXED_POINT_TYPE))
9908           {
9909             /* Result of these operations is always an int,
9910                but that does not mean the operands should be
9911                converted to ints!  */
9912             result_type = integer_type_node;
9913             if (op0_int_operands)
9914               {
9915                 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
9916                 op0 = remove_c_maybe_const_expr (op0);
9917               }
9918             else
9919               op0 = c_objc_common_truthvalue_conversion (location, op0);
9920             if (op1_int_operands)
9921               {
9922                 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
9923                 op1 = remove_c_maybe_const_expr (op1);
9924               }
9925             else
9926               op1 = c_objc_common_truthvalue_conversion (location, op1);
9927             converted = 1;
9928             boolean_op = true;
9929           }
9930       if (code == TRUTH_ANDIF_EXPR)
9931           {
9932             int_const_or_overflow = (int_operands
9933                                            && TREE_CODE (orig_op0) == INTEGER_CST
9934                                            && (op0 == truthvalue_false_node
9935                                                || TREE_CODE (orig_op1) == INTEGER_CST));
9936             int_const = (int_const_or_overflow
9937                            && !TREE_OVERFLOW (orig_op0)
9938                            && (op0 == truthvalue_false_node
9939                                  || !TREE_OVERFLOW (orig_op1)));
9940           }
9941       else if (code == TRUTH_ORIF_EXPR)
9942           {
9943             int_const_or_overflow = (int_operands
9944                                            && TREE_CODE (orig_op0) == INTEGER_CST
9945                                            && (op0 == truthvalue_true_node
9946                                                || TREE_CODE (orig_op1) == INTEGER_CST));
9947             int_const = (int_const_or_overflow
9948                            && !TREE_OVERFLOW (orig_op0)
9949                            && (op0 == truthvalue_true_node
9950                                  || !TREE_OVERFLOW (orig_op1)));
9951           }
9952       break;
9953 
9954       /* Shift operations: result has same type as first operand;
9955            always convert second operand to int.
9956            Also set SHORT_SHIFT if shifting rightward.  */
9957 
9958     case RSHIFT_EXPR:
9959       if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
9960           && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
9961         {
9962           result_type = type0;
9963           converted = 1;
9964         }
9965       else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9966             && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9967           && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
9968           && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
9969           {
9970             result_type = type0;
9971             converted = 1;
9972           }
9973       else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
9974             && code1 == INTEGER_TYPE)
9975           {
9976             if (TREE_CODE (op1) == INTEGER_CST)
9977               {
9978                 if (tree_int_cst_sgn (op1) < 0)
9979                     {
9980                       int_const = false;
9981                       if (c_inhibit_evaluation_warnings == 0)
9982                         warning (0, "right shift count is negative");
9983                     }
9984                 else
9985                     {
9986                       if (!integer_zerop (op1))
9987                         short_shift = 1;
9988 
9989                       if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
9990                         {
9991                           int_const = false;
9992                           if (c_inhibit_evaluation_warnings == 0)
9993                               warning (0, "right shift count >= width of type");
9994                         }
9995                     }
9996               }
9997 
9998             /* Use the type of the value to be shifted.  */
9999             result_type = type0;
10000             /* Convert the non vector shift-count to an integer, regardless
10001                of size of value being shifted.  */
10002             if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
10003                 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
10004               op1 = convert (integer_type_node, op1);
10005             /* Avoid converting op1 to result_type later.  */
10006             converted = 1;
10007           }
10008       break;
10009 
10010     case LSHIFT_EXPR:
10011       if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10012           && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10013         {
10014           result_type = type0;
10015           converted = 1;
10016         }
10017       else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10018             && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10019           && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10020           && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10021           {
10022             result_type = type0;
10023             converted = 1;
10024           }
10025       else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
10026             && code1 == INTEGER_TYPE)
10027           {
10028             if (TREE_CODE (op1) == INTEGER_CST)
10029               {
10030                 if (tree_int_cst_sgn (op1) < 0)
10031                     {
10032                       int_const = false;
10033                       if (c_inhibit_evaluation_warnings == 0)
10034                         warning (0, "left shift count is negative");
10035                     }
10036 
10037                 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
10038                     {
10039                       int_const = false;
10040                       if (c_inhibit_evaluation_warnings == 0)
10041                         warning (0, "left shift count >= width of type");
10042                     }
10043               }
10044 
10045             /* Use the type of the value to be shifted.  */
10046             result_type = type0;
10047             /* Convert the non vector shift-count to an integer, regardless
10048                of size of value being shifted.  */
10049             if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
10050                 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
10051               op1 = convert (integer_type_node, op1);
10052             /* Avoid converting op1 to result_type later.  */
10053             converted = 1;
10054           }
10055       break;
10056 
10057     case EQ_EXPR:
10058     case NE_EXPR:
10059       if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10060         {
10061           tree intt;
10062           if (TREE_TYPE (type0) != TREE_TYPE (type1))
10063             {
10064               error_at (location, "comparing vectors with different "
10065                                   "element types");
10066               return error_mark_node;
10067             }
10068 
10069           if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10070             {
10071               error_at (location, "comparing vectors with different "
10072                                   "number of elements");
10073               return error_mark_node;
10074             }
10075 
10076           /* Always construct signed integer vector type.  */
10077           intt = c_common_type_for_size (GET_MODE_BITSIZE
10078                                                      (TYPE_MODE (TREE_TYPE (type0))), 0);
10079           result_type = build_opaque_vector_type (intt,
10080                                                               TYPE_VECTOR_SUBPARTS (type0));
10081           converted = 1;
10082           break;
10083         }
10084       if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
10085           warning_at (location,
10086                         OPT_Wfloat_equal,
10087                         "comparing floating point with == or != is unsafe");
10088       /* Result of comparison is always int,
10089            but don't convert the args to int!  */
10090       build_type = integer_type_node;
10091       if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10092              || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
10093             && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10094                 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
10095           short_compare = 1;
10096       else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10097           {
10098             if (TREE_CODE (op0) == ADDR_EXPR
10099                 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
10100               {
10101                 if (code == EQ_EXPR)
10102                     warning_at (location,
10103                                   OPT_Waddress,
10104                                   "the comparison will always evaluate as %<false%> "
10105                                   "for the address of %qD will never be NULL",
10106                                   TREE_OPERAND (op0, 0));
10107                 else
10108                     warning_at (location,
10109                                   OPT_Waddress,
10110                                   "the comparison will always evaluate as %<true%> "
10111                                   "for the address of %qD will never be NULL",
10112                                   TREE_OPERAND (op0, 0));
10113               }
10114             result_type = type0;
10115           }
10116       else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10117           {
10118             if (TREE_CODE (op1) == ADDR_EXPR
10119                 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
10120               {
10121                 if (code == EQ_EXPR)
10122                     warning_at (location,
10123                                   OPT_Waddress,
10124                                   "the comparison will always evaluate as %<false%> "
10125                                   "for the address of %qD will never be NULL",
10126                                   TREE_OPERAND (op1, 0));
10127                 else
10128                     warning_at (location,
10129                                   OPT_Waddress,
10130                                   "the comparison will always evaluate as %<true%> "
10131                                   "for the address of %qD will never be NULL",
10132                                   TREE_OPERAND (op1, 0));
10133               }
10134             result_type = type1;
10135           }
10136       else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10137           {
10138             tree tt0 = TREE_TYPE (type0);
10139             tree tt1 = TREE_TYPE (type1);
10140             addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
10141             addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
10142             addr_space_t as_common = ADDR_SPACE_GENERIC;
10143 
10144             /* Anything compares with void *.  void * compares with anything.
10145                Otherwise, the targets must be compatible
10146                and both must be object or both incomplete.  */
10147             if (comp_target_types (location, type0, type1))
10148               result_type = common_pointer_type (type0, type1);
10149             else if (!addr_space_superset (as0, as1, &as_common))
10150               {
10151                 error_at (location, "comparison of pointers to "
10152                               "disjoint address spaces");
10153                 return error_mark_node;
10154               }
10155             else if (VOID_TYPE_P (tt0))
10156               {
10157                 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
10158                     pedwarn (location, OPT_pedantic, "ISO C forbids "
10159                                "comparison of %<void *%> with function pointer");
10160               }
10161             else if (VOID_TYPE_P (tt1))
10162               {
10163                 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
10164                     pedwarn (location, OPT_pedantic, "ISO C forbids "
10165                                "comparison of %<void *%> with function pointer");
10166               }
10167             else
10168               /* Avoid warning about the volatile ObjC EH puts on decls.  */
10169               if (!objc_ok)
10170                 pedwarn (location, 0,
10171                            "comparison of distinct pointer types lacks a cast");
10172 
10173             if (result_type == NULL_TREE)
10174               {
10175                 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10176                 result_type = build_pointer_type
10177                                     (build_qualified_type (void_type_node, qual));
10178               }
10179           }
10180       else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10181           {
10182             result_type = type0;
10183             pedwarn (location, 0, "comparison between pointer and integer");
10184           }
10185       else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10186           {
10187             result_type = type1;
10188             pedwarn (location, 0, "comparison between pointer and integer");
10189           }
10190       break;
10191 
10192     case LE_EXPR:
10193     case GE_EXPR:
10194     case LT_EXPR:
10195     case GT_EXPR:
10196       if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10197         {
10198           tree intt;
10199           if (TREE_TYPE (type0) != TREE_TYPE (type1))
10200             {
10201               error_at (location, "comparing vectors with different "
10202                                   "element types");
10203               return error_mark_node;
10204             }
10205 
10206           if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10207             {
10208               error_at (location, "comparing vectors with different "
10209                                   "number of elements");
10210               return error_mark_node;
10211             }
10212 
10213           /* Always construct signed integer vector type.  */
10214           intt = c_common_type_for_size (GET_MODE_BITSIZE
10215                                                      (TYPE_MODE (TREE_TYPE (type0))), 0);
10216           result_type = build_opaque_vector_type (intt,
10217                                                               TYPE_VECTOR_SUBPARTS (type0));
10218           converted = 1;
10219           break;
10220         }
10221       build_type = integer_type_node;
10222       if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10223              || code0 == FIXED_POINT_TYPE)
10224             && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10225                 || code1 == FIXED_POINT_TYPE))
10226           short_compare = 1;
10227       else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10228           {
10229             addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
10230             addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
10231             addr_space_t as_common;
10232 
10233             if (comp_target_types (location, type0, type1))
10234               {
10235                 result_type = common_pointer_type (type0, type1);
10236                 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
10237                       != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
10238                     pedwarn (location, 0,
10239                                "comparison of complete and incomplete pointers");
10240                 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
10241                     pedwarn (location, OPT_pedantic, "ISO C forbids "
10242                                "ordered comparisons of pointers to functions");
10243                 else if (null_pointer_constant_p (orig_op0)
10244                            || null_pointer_constant_p (orig_op1))
10245                     warning_at (location, OPT_Wextra,
10246                                   "ordered comparison of pointer with null pointer");
10247 
10248               }
10249             else if (!addr_space_superset (as0, as1, &as_common))
10250               {
10251                 error_at (location, "comparison of pointers to "
10252                               "disjoint address spaces");
10253                 return error_mark_node;
10254               }
10255             else
10256               {
10257                 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10258                 result_type = build_pointer_type
10259                                     (build_qualified_type (void_type_node, qual));
10260                 pedwarn (location, 0,
10261                            "comparison of distinct pointer types lacks a cast");
10262               }
10263           }
10264       else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10265           {
10266             result_type = type0;
10267             if (pedantic)
10268               pedwarn (location, OPT_pedantic,
10269                          "ordered comparison of pointer with integer zero");
10270             else if (extra_warnings)
10271               warning_at (location, OPT_Wextra,
10272                               "ordered comparison of pointer with integer zero");
10273           }
10274       else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10275           {
10276             result_type = type1;
10277             if (pedantic)
10278               pedwarn (location, OPT_pedantic,
10279                          "ordered comparison of pointer with integer zero");
10280             else if (extra_warnings)
10281               warning_at (location, OPT_Wextra,
10282                               "ordered comparison of pointer with integer zero");
10283           }
10284       else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10285           {
10286             result_type = type0;
10287             pedwarn (location, 0, "comparison between pointer and integer");
10288           }
10289       else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10290           {
10291             result_type = type1;
10292             pedwarn (location, 0, "comparison between pointer and integer");
10293           }
10294       break;
10295 
10296     default:
10297       gcc_unreachable ();
10298     }
10299 
10300   if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10301     return error_mark_node;
10302 
10303   if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10304       && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
10305             || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0),
10306                                                                 TREE_TYPE (type1))))
10307     {
10308       binary_op_error (location, code, type0, type1);
10309       return error_mark_node;
10310     }
10311 
10312   if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10313        || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
10314       &&
10315       (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10316        || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
10317     {
10318       bool first_complex = (code0 == COMPLEX_TYPE);
10319       bool second_complex = (code1 == COMPLEX_TYPE);
10320       int none_complex = (!first_complex && !second_complex);
10321 
10322       if (shorten || common || short_compare)
10323           {
10324             result_type = c_common_type (type0, type1);
10325             do_warn_double_promotion (result_type, type0, type1,
10326                                             "implicit conversion from %qT to %qT "
10327                                             "to match other operand of binary "
10328                                             "expression",
10329                                             location);
10330             if (result_type == error_mark_node)
10331               return error_mark_node;
10332           }
10333 
10334       if (first_complex != second_complex
10335             && (code == PLUS_EXPR
10336                 || code == MINUS_EXPR
10337                 || code == MULT_EXPR
10338                 || (code == TRUNC_DIV_EXPR && first_complex))
10339             && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
10340             && flag_signed_zeros)
10341           {
10342             /* An operation on mixed real/complex operands must be
10343                handled specially, but the language-independent code can
10344                more easily optimize the plain complex arithmetic if
10345                -fno-signed-zeros.  */
10346             tree real_type = TREE_TYPE (result_type);
10347             tree real, imag;
10348             if (type0 != orig_type0 || type1 != orig_type1)
10349               {
10350                 gcc_assert (may_need_excess_precision && common);
10351                 semantic_result_type = c_common_type (orig_type0, orig_type1);
10352               }
10353             if (first_complex)
10354               {
10355                 if (TREE_TYPE (op0) != result_type)
10356                     op0 = convert_and_check (result_type, op0);
10357                 if (TREE_TYPE (op1) != real_type)
10358                     op1 = convert_and_check (real_type, op1);
10359               }
10360             else
10361               {
10362                 if (TREE_TYPE (op0) != real_type)
10363                     op0 = convert_and_check (real_type, op0);
10364                 if (TREE_TYPE (op1) != result_type)
10365                     op1 = convert_and_check (result_type, op1);
10366               }
10367             if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10368               return error_mark_node;
10369             if (first_complex)
10370               {
10371                 op0 = c_save_expr (op0);
10372                 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
10373                                              op0, 1);
10374                 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
10375                                              op0, 1);
10376                 switch (code)
10377                     {
10378                     case MULT_EXPR:
10379                     case TRUNC_DIV_EXPR:
10380                       op1 = c_save_expr (op1);
10381                       imag = build2 (resultcode, real_type, imag, op1);
10382                       /* Fall through.  */
10383                     case PLUS_EXPR:
10384                     case MINUS_EXPR:
10385                       real = build2 (resultcode, real_type, real, op1);
10386                       break;
10387                     default:
10388                       gcc_unreachable();
10389                     }
10390               }
10391             else
10392               {
10393                 op1 = c_save_expr (op1);
10394                 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
10395                                              op1, 1);
10396                 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
10397                                              op1, 1);
10398                 switch (code)
10399                     {
10400                     case MULT_EXPR:
10401                       op0 = c_save_expr (op0);
10402                       imag = build2 (resultcode, real_type, op0, imag);
10403                       /* Fall through.  */
10404                     case PLUS_EXPR:
10405                       real = build2 (resultcode, real_type, op0, real);
10406                       break;
10407                     case MINUS_EXPR:
10408                       real = build2 (resultcode, real_type, op0, real);
10409                       imag = build1 (NEGATE_EXPR, real_type, imag);
10410                       break;
10411                     default:
10412                       gcc_unreachable();
10413                     }
10414               }
10415             ret = build2 (COMPLEX_EXPR, result_type, real, imag);
10416             goto return_build_binary_op;
10417           }
10418 
10419       /* For certain operations (which identify themselves by shorten != 0)
10420            if both args were extended from the same smaller type,
10421            do the arithmetic in that type and then extend.
10422 
10423            shorten !=0 and !=1 indicates a bitwise operation.
10424            For them, this optimization is safe only if
10425            both args are zero-extended or both are sign-extended.
10426            Otherwise, we might change the result.
10427            Eg, (short)-1 | (unsigned short)-1 is (int)-1
10428            but calculated in (unsigned short) it would be (unsigned short)-1.  */
10429 
10430       if (shorten && none_complex)
10431           {
10432             final_type = result_type;
10433             result_type = shorten_binary_op (result_type, op0, op1,
10434                                                      shorten == -1);
10435           }
10436 
10437       /* Shifts can be shortened if shifting right.  */
10438 
10439       if (short_shift)
10440           {
10441             int unsigned_arg;
10442             tree arg0 = get_narrower (op0, &unsigned_arg);
10443 
10444             final_type = result_type;
10445 
10446             if (arg0 == op0 && final_type == TREE_TYPE (op0))
10447               unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
10448 
10449             if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
10450                 && tree_int_cst_sgn (op1) > 0
10451                 /* We can shorten only if the shift count is less than the
10452                      number of bits in the smaller type size.  */
10453                 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
10454                 /* We cannot drop an unsigned shift after sign-extension.  */
10455                 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
10456               {
10457                 /* Do an unsigned shift if the operand was zero-extended.  */
10458                 result_type
10459                     = c_common_signed_or_unsigned_type (unsigned_arg,
10460                                                                 TREE_TYPE (arg0));
10461                 /* Convert value-to-be-shifted to that type.  */
10462                 if (TREE_TYPE (op0) != result_type)
10463                     op0 = convert (result_type, op0);
10464                 converted = 1;
10465               }
10466           }
10467 
10468       /* Comparison operations are shortened too but differently.
10469            They identify themselves by setting short_compare = 1.  */
10470 
10471       if (short_compare)
10472           {
10473             /* Don't write &op0, etc., because that would prevent op0
10474                from being kept in a register.
10475                Instead, make copies of the our local variables and
10476                pass the copies by reference, then copy them back afterward.  */
10477             tree xop0 = op0, xop1 = op1, xresult_type = result_type;
10478             enum tree_code xresultcode = resultcode;
10479             tree val
10480               = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
10481 
10482             if (val != 0)
10483               {
10484                 ret = val;
10485                 goto return_build_binary_op;
10486               }
10487 
10488             op0 = xop0, op1 = xop1;
10489             converted = 1;
10490             resultcode = xresultcode;
10491 
10492             if (c_inhibit_evaluation_warnings == 0)
10493               {
10494                 bool op0_maybe_const = true;
10495                 bool op1_maybe_const = true;
10496                 tree orig_op0_folded, orig_op1_folded;
10497 
10498                 if (in_late_binary_op)
10499                     {
10500                       orig_op0_folded = orig_op0;
10501                       orig_op1_folded = orig_op1;
10502                     }
10503                 else
10504                     {
10505                       /* Fold for the sake of possible warnings, as in
10506                          build_conditional_expr.  This requires the
10507                          "original" values to be folded, not just op0 and
10508                          op1.  */
10509                       c_inhibit_evaluation_warnings++;
10510                       op0 = c_fully_fold (op0, require_constant_value,
10511                                               &op0_maybe_const);
10512                       op1 = c_fully_fold (op1, require_constant_value,
10513                                               &op1_maybe_const);
10514                       c_inhibit_evaluation_warnings--;
10515                       orig_op0_folded = c_fully_fold (orig_op0,
10516                                                               require_constant_value,
10517                                                               NULL);
10518                       orig_op1_folded = c_fully_fold (orig_op1,
10519                                                               require_constant_value,
10520                                                               NULL);
10521                     }
10522 
10523                 if (warn_sign_compare)
10524                     warn_for_sign_compare (location, orig_op0_folded,
10525                                                orig_op1_folded, op0, op1,
10526                                                result_type, resultcode);
10527                 if (!in_late_binary_op && !int_operands)
10528                     {
10529                       if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
10530                         op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
10531                       if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
10532                         op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
10533                     }
10534               }
10535           }
10536     }
10537 
10538   /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
10539      If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
10540      Then the expression will be built.
10541      It will be given type FINAL_TYPE if that is nonzero;
10542      otherwise, it will be given type RESULT_TYPE.  */
10543 
10544   if (!result_type)
10545     {
10546       binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
10547       return error_mark_node;
10548     }
10549 
10550   if (build_type == NULL_TREE)
10551     {
10552       build_type = result_type;
10553       if ((type0 != orig_type0 || type1 != orig_type1)
10554             && !boolean_op)
10555           {
10556             gcc_assert (may_need_excess_precision && common);
10557             semantic_result_type = c_common_type (orig_type0, orig_type1);
10558           }
10559     }
10560 
10561   if (!converted)
10562     {
10563       op0 = ep_convert_and_check (result_type, op0, semantic_result_type);
10564       op1 = ep_convert_and_check (result_type, op1, semantic_result_type);
10565 
10566       /* This can happen if one operand has a vector type, and the other
10567            has a different type.  */
10568       if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10569           return error_mark_node;
10570     }
10571 
10572   /* Treat expressions in initializers specially as they can't trap.  */
10573   if (int_const_or_overflow)
10574     ret = (require_constant_value
10575              ? fold_build2_initializer_loc (location, resultcode, build_type,
10576                                                     op0, op1)
10577              : fold_build2_loc (location, resultcode, build_type, op0, op1));
10578   else
10579     ret = build2 (resultcode, build_type, op0, op1);
10580   if (final_type != 0)
10581     ret = convert (final_type, ret);
10582 
10583  return_build_binary_op:
10584   gcc_assert (ret != error_mark_node);
10585   if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
10586     ret = (int_operands
10587              ? note_integer_operands (ret)
10588              : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
10589   else if (TREE_CODE (ret) != INTEGER_CST && int_operands
10590              && !in_late_binary_op)
10591     ret = note_integer_operands (ret);
10592   if (semantic_result_type)
10593     ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
10594   protected_set_expr_location (ret, location);
10595   return ret;
10596 }
10597 
10598 
10599 /* Convert EXPR to be a truth-value, validating its type for this
10600    purpose.  LOCATION is the source location for the expression.  */
10601 
10602 tree
c_objc_common_truthvalue_conversion(location_t location,tree expr)10603 c_objc_common_truthvalue_conversion (location_t location, tree expr)
10604 {
10605   bool int_const, int_operands;
10606 
10607   switch (TREE_CODE (TREE_TYPE (expr)))
10608     {
10609     case ARRAY_TYPE:
10610       error_at (location, "used array that cannot be converted to pointer where scalar is required");
10611       return error_mark_node;
10612 
10613     case RECORD_TYPE:
10614       error_at (location, "used struct type value where scalar is required");
10615       return error_mark_node;
10616 
10617     case UNION_TYPE:
10618       error_at (location, "used union type value where scalar is required");
10619       return error_mark_node;
10620 
10621     case VOID_TYPE:
10622       error_at (location, "void value not ignored as it ought to be");
10623       return error_mark_node;
10624 
10625     case FUNCTION_TYPE:
10626       gcc_unreachable ();
10627 
10628     case VECTOR_TYPE:
10629       error_at (location, "used vector type where scalar is required");
10630       return error_mark_node;
10631 
10632     default:
10633       break;
10634     }
10635 
10636   int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
10637   int_operands = EXPR_INT_CONST_OPERANDS (expr);
10638   if (int_operands && TREE_CODE (expr) != INTEGER_CST)
10639     {
10640       expr = remove_c_maybe_const_expr (expr);
10641       expr = build2 (NE_EXPR, integer_type_node, expr,
10642                          convert (TREE_TYPE (expr), integer_zero_node));
10643       expr = note_integer_operands (expr);
10644     }
10645   else
10646     /* ??? Should we also give an error for vectors rather than leaving
10647        those to give errors later?  */
10648     expr = c_common_truthvalue_conversion (location, expr);
10649 
10650   if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
10651     {
10652       if (TREE_OVERFLOW (expr))
10653           return expr;
10654       else
10655           return note_integer_operands (expr);
10656     }
10657   if (TREE_CODE (expr) == INTEGER_CST && !int_const)
10658     return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10659   return expr;
10660 }
10661 
10662 
10663 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
10664    required.  */
10665 
10666 tree
c_expr_to_decl(tree expr,bool * tc ATTRIBUTE_UNUSED,bool * se)10667 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
10668 {
10669   if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
10670     {
10671       tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
10672       /* Executing a compound literal inside a function reinitializes
10673            it.  */
10674       if (!TREE_STATIC (decl))
10675           *se = true;
10676       return decl;
10677     }
10678   else
10679     return expr;
10680 }
10681 
10682 /* Like c_begin_compound_stmt, except force the retention of the BLOCK.  */
10683 
10684 tree
c_begin_omp_parallel(void)10685 c_begin_omp_parallel (void)
10686 {
10687   tree block;
10688 
10689   keep_next_level ();
10690   block = c_begin_compound_stmt (true);
10691 
10692   return block;
10693 }
10694 
10695 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
10696    statement.  LOC is the location of the OMP_PARALLEL.  */
10697 
10698 tree
c_finish_omp_parallel(location_t loc,tree clauses,tree block)10699 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
10700 {
10701   tree stmt;
10702 
10703   block = c_end_compound_stmt (loc, block, true);
10704 
10705   stmt = make_node (OMP_PARALLEL);
10706   TREE_TYPE (stmt) = void_type_node;
10707   OMP_PARALLEL_CLAUSES (stmt) = clauses;
10708   OMP_PARALLEL_BODY (stmt) = block;
10709   SET_EXPR_LOCATION (stmt, loc);
10710 
10711   return add_stmt (stmt);
10712 }
10713 
10714 /* Like c_begin_compound_stmt, except force the retention of the BLOCK.  */
10715 
10716 tree
c_begin_omp_task(void)10717 c_begin_omp_task (void)
10718 {
10719   tree block;
10720 
10721   keep_next_level ();
10722   block = c_begin_compound_stmt (true);
10723 
10724   return block;
10725 }
10726 
10727 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
10728    statement.  LOC is the location of the #pragma.  */
10729 
10730 tree
c_finish_omp_task(location_t loc,tree clauses,tree block)10731 c_finish_omp_task (location_t loc, tree clauses, tree block)
10732 {
10733   tree stmt;
10734 
10735   block = c_end_compound_stmt (loc, block, true);
10736 
10737   stmt = make_node (OMP_TASK);
10738   TREE_TYPE (stmt) = void_type_node;
10739   OMP_TASK_CLAUSES (stmt) = clauses;
10740   OMP_TASK_BODY (stmt) = block;
10741   SET_EXPR_LOCATION (stmt, loc);
10742 
10743   return add_stmt (stmt);
10744 }
10745 
10746 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
10747    Remove any elements from the list that are invalid.  */
10748 
10749 tree
c_finish_omp_clauses(tree clauses)10750 c_finish_omp_clauses (tree clauses)
10751 {
10752   bitmap_head generic_head, firstprivate_head, lastprivate_head;
10753   tree c, t, *pc = &clauses;
10754   const char *name;
10755 
10756   bitmap_obstack_initialize (NULL);
10757   bitmap_initialize (&generic_head, &bitmap_default_obstack);
10758   bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
10759   bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
10760 
10761   for (pc = &clauses, c = clauses; c ; c = *pc)
10762     {
10763       bool remove = false;
10764       bool need_complete = false;
10765       bool need_implicitly_determined = false;
10766 
10767       switch (OMP_CLAUSE_CODE (c))
10768           {
10769           case OMP_CLAUSE_SHARED:
10770             name = "shared";
10771             need_implicitly_determined = true;
10772             goto check_dup_generic;
10773 
10774           case OMP_CLAUSE_PRIVATE:
10775             name = "private";
10776             need_complete = true;
10777             need_implicitly_determined = true;
10778             goto check_dup_generic;
10779 
10780           case OMP_CLAUSE_REDUCTION:
10781             name = "reduction";
10782             need_implicitly_determined = true;
10783             t = OMP_CLAUSE_DECL (c);
10784             if (AGGREGATE_TYPE_P (TREE_TYPE (t))
10785                 || POINTER_TYPE_P (TREE_TYPE (t)))
10786               {
10787                 error_at (OMP_CLAUSE_LOCATION (c),
10788                               "%qE has invalid type for %<reduction%>", t);
10789                 remove = true;
10790               }
10791             else if (FLOAT_TYPE_P (TREE_TYPE (t)))
10792               {
10793                 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
10794                 const char *r_name = NULL;
10795 
10796                 switch (r_code)
10797                     {
10798                     case PLUS_EXPR:
10799                     case MULT_EXPR:
10800                     case MINUS_EXPR:
10801                     case MIN_EXPR:
10802                     case MAX_EXPR:
10803                       break;
10804                     case BIT_AND_EXPR:
10805                       r_name = "&";
10806                       break;
10807                     case BIT_XOR_EXPR:
10808                       r_name = "^";
10809                       break;
10810                     case BIT_IOR_EXPR:
10811                       r_name = "|";
10812                       break;
10813                     case TRUTH_ANDIF_EXPR:
10814                       r_name = "&&";
10815                       break;
10816                     case TRUTH_ORIF_EXPR:
10817                       r_name = "||";
10818                       break;
10819                     default:
10820                       gcc_unreachable ();
10821                     }
10822                 if (r_name)
10823                     {
10824                       error_at (OMP_CLAUSE_LOCATION (c),
10825                                   "%qE has invalid type for %<reduction(%s)%>",
10826                                   t, r_name);
10827                       remove = true;
10828                     }
10829               }
10830             goto check_dup_generic;
10831 
10832           case OMP_CLAUSE_COPYPRIVATE:
10833             name = "copyprivate";
10834             goto check_dup_generic;
10835 
10836           case OMP_CLAUSE_COPYIN:
10837             name = "copyin";
10838             t = OMP_CLAUSE_DECL (c);
10839             if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
10840               {
10841                 error_at (OMP_CLAUSE_LOCATION (c),
10842                               "%qE must be %<threadprivate%> for %<copyin%>", t);
10843                 remove = true;
10844               }
10845             goto check_dup_generic;
10846 
10847           check_dup_generic:
10848             t = OMP_CLAUSE_DECL (c);
10849             if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10850               {
10851                 error_at (OMP_CLAUSE_LOCATION (c),
10852                               "%qE is not a variable in clause %qs", t, name);
10853                 remove = true;
10854               }
10855             else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10856                        || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
10857                        || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
10858               {
10859                 error_at (OMP_CLAUSE_LOCATION (c),
10860                               "%qE appears more than once in data clauses", t);
10861                 remove = true;
10862               }
10863             else
10864               bitmap_set_bit (&generic_head, DECL_UID (t));
10865             break;
10866 
10867           case OMP_CLAUSE_FIRSTPRIVATE:
10868             name = "firstprivate";
10869             t = OMP_CLAUSE_DECL (c);
10870             need_complete = true;
10871             need_implicitly_determined = true;
10872             if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10873               {
10874                 error_at (OMP_CLAUSE_LOCATION (c),
10875                               "%qE is not a variable in clause %<firstprivate%>", t);
10876                 remove = true;
10877               }
10878             else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10879                        || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
10880               {
10881                 error_at (OMP_CLAUSE_LOCATION (c),
10882                               "%qE appears more than once in data clauses", t);
10883                 remove = true;
10884               }
10885             else
10886               bitmap_set_bit (&firstprivate_head, DECL_UID (t));
10887             break;
10888 
10889           case OMP_CLAUSE_LASTPRIVATE:
10890             name = "lastprivate";
10891             t = OMP_CLAUSE_DECL (c);
10892             need_complete = true;
10893             need_implicitly_determined = true;
10894             if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10895               {
10896                 error_at (OMP_CLAUSE_LOCATION (c),
10897                               "%qE is not a variable in clause %<lastprivate%>", t);
10898                 remove = true;
10899               }
10900             else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10901                        || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
10902               {
10903                 error_at (OMP_CLAUSE_LOCATION (c),
10904                          "%qE appears more than once in data clauses", t);
10905                 remove = true;
10906               }
10907             else
10908               bitmap_set_bit (&lastprivate_head, DECL_UID (t));
10909             break;
10910 
10911           case OMP_CLAUSE_IF:
10912           case OMP_CLAUSE_NUM_THREADS:
10913           case OMP_CLAUSE_SCHEDULE:
10914           case OMP_CLAUSE_NOWAIT:
10915           case OMP_CLAUSE_ORDERED:
10916           case OMP_CLAUSE_DEFAULT:
10917           case OMP_CLAUSE_UNTIED:
10918           case OMP_CLAUSE_COLLAPSE:
10919           case OMP_CLAUSE_FINAL:
10920           case OMP_CLAUSE_MERGEABLE:
10921             pc = &OMP_CLAUSE_CHAIN (c);
10922             continue;
10923 
10924           default:
10925             gcc_unreachable ();
10926           }
10927 
10928       if (!remove)
10929           {
10930             t = OMP_CLAUSE_DECL (c);
10931 
10932             if (need_complete)
10933               {
10934                 t = require_complete_type (t);
10935                 if (t == error_mark_node)
10936                     remove = true;
10937               }
10938 
10939             if (need_implicitly_determined)
10940               {
10941                 const char *share_name = NULL;
10942 
10943                 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
10944                     share_name = "threadprivate";
10945                 else switch (c_omp_predetermined_sharing (t))
10946                     {
10947                     case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
10948                       break;
10949                     case OMP_CLAUSE_DEFAULT_SHARED:
10950                       /* const vars may be specified in firstprivate clause.  */
10951                       if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
10952                           && TREE_READONLY (t))
10953                         break;
10954                       share_name = "shared";
10955                       break;
10956                     case OMP_CLAUSE_DEFAULT_PRIVATE:
10957                       share_name = "private";
10958                       break;
10959                     default:
10960                       gcc_unreachable ();
10961                     }
10962                 if (share_name)
10963                     {
10964                       error_at (OMP_CLAUSE_LOCATION (c),
10965                                   "%qE is predetermined %qs for %qs",
10966                                   t, share_name, name);
10967                       remove = true;
10968                     }
10969               }
10970           }
10971 
10972       if (remove)
10973           *pc = OMP_CLAUSE_CHAIN (c);
10974       else
10975           pc = &OMP_CLAUSE_CHAIN (c);
10976     }
10977 
10978   bitmap_obstack_release (NULL);
10979   return clauses;
10980 }
10981 
10982 /* Create a transaction node.  */
10983 
10984 tree
c_finish_transaction(location_t loc,tree block,int flags)10985 c_finish_transaction (location_t loc, tree block, int flags)
10986 {
10987   tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
10988   if (flags & TM_STMT_ATTR_OUTER)
10989     TRANSACTION_EXPR_OUTER (stmt) = 1;
10990   if (flags & TM_STMT_ATTR_RELAXED)
10991     TRANSACTION_EXPR_RELAXED (stmt) = 1;
10992   return add_stmt (stmt);
10993 }
10994 
10995 /* Make a variant type in the proper way for C/C++, propagating qualifiers
10996    down to the element type of an array.  */
10997 
10998 tree
c_build_qualified_type(tree type,int type_quals)10999 c_build_qualified_type (tree type, int type_quals)
11000 {
11001   if (type == error_mark_node)
11002     return type;
11003 
11004   if (TREE_CODE (type) == ARRAY_TYPE)
11005     {
11006       tree t;
11007       tree element_type = c_build_qualified_type (TREE_TYPE (type),
11008                                                               type_quals);
11009 
11010       /* See if we already have an identically qualified type.  */
11011       for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
11012           {
11013             if (TYPE_QUALS (strip_array_types (t)) == type_quals
11014                 && TYPE_NAME (t) == TYPE_NAME (type)
11015                 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
11016                 && attribute_list_equal (TYPE_ATTRIBUTES (t),
11017                                                TYPE_ATTRIBUTES (type)))
11018               break;
11019           }
11020       if (!t)
11021           {
11022           tree domain = TYPE_DOMAIN (type);
11023 
11024             t = build_variant_type_copy (type);
11025             TREE_TYPE (t) = element_type;
11026 
11027           if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
11028               || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
11029             SET_TYPE_STRUCTURAL_EQUALITY (t);
11030           else if (TYPE_CANONICAL (element_type) != element_type
11031                    || (domain && TYPE_CANONICAL (domain) != domain))
11032             {
11033               tree unqualified_canon
11034                 = build_array_type (TYPE_CANONICAL (element_type),
11035                                     domain? TYPE_CANONICAL (domain)
11036                                           : NULL_TREE);
11037               TYPE_CANONICAL (t)
11038                 = c_build_qualified_type (unqualified_canon, type_quals);
11039             }
11040           else
11041             TYPE_CANONICAL (t) = t;
11042           }
11043       return t;
11044     }
11045 
11046   /* A restrict-qualified pointer type must be a pointer to object or
11047      incomplete type.  Note that the use of POINTER_TYPE_P also allows
11048      REFERENCE_TYPEs, which is appropriate for C++.  */
11049   if ((type_quals & TYPE_QUAL_RESTRICT)
11050       && (!POINTER_TYPE_P (type)
11051             || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
11052     {
11053       error ("invalid use of %<restrict%>");
11054       type_quals &= ~TYPE_QUAL_RESTRICT;
11055     }
11056 
11057   return build_qualified_type (type, type_quals);
11058 }
11059 
11060 /* Build a VA_ARG_EXPR for the C parser.  */
11061 
11062 tree
c_build_va_arg(location_t loc,tree expr,tree type)11063 c_build_va_arg (location_t loc, tree expr, tree type)
11064 {
11065   if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
11066     warning_at (loc, OPT_Wc___compat,
11067                     "C++ requires promoted type, not enum type, in %<va_arg%>");
11068   return build_va_arg (loc, expr, type);
11069 }
11070