OSDN Git Service

2de1e5c66729d55b5fc76a2921ed52986c01fa7c
[pf3gnuchains/gcc-fork.git] / gcc / c-typeck.c
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 \f
113 /* Return true if EXP is a null pointer constant, false otherwise.  */
114
115 static bool
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
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
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 \f/* 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
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
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
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
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
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
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 \f
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
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
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
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 \f
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
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
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
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
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 \f
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
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
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 \f
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
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 *
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
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
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
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
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 \f
1720 /* Compute the size to increment a pointer by.  */
1721
1722 static tree
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 \f
1742 /* Return either DECL or its known constant value (if it has one).  */
1743
1744 tree
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
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   adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
1789   return convert (ptrtype, adr);
1790 }
1791
1792 /* Convert the function expression EXP to a pointer.  */
1793 static tree
1794 function_to_pointer_conversion (location_t loc, tree exp)
1795 {
1796   tree orig_exp = exp;
1797
1798   gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1799
1800   STRIP_TYPE_NOPS (exp);
1801
1802   if (TREE_NO_WARNING (orig_exp))
1803     TREE_NO_WARNING (exp) = 1;
1804
1805   return build_unary_op (loc, ADDR_EXPR, exp, 0);
1806 }
1807
1808 /* Mark EXP as read, not just set, for set but not used -Wunused
1809    warning purposes.  */
1810
1811 void
1812 mark_exp_read (tree exp)
1813 {
1814   switch (TREE_CODE (exp))
1815     {
1816     case VAR_DECL:
1817     case PARM_DECL:
1818       DECL_READ_P (exp) = 1;
1819       break;
1820     case ARRAY_REF:
1821     case COMPONENT_REF:
1822     case MODIFY_EXPR:
1823     case REALPART_EXPR:
1824     case IMAGPART_EXPR:
1825     CASE_CONVERT:
1826     case ADDR_EXPR:
1827       mark_exp_read (TREE_OPERAND (exp, 0));
1828       break;
1829     case COMPOUND_EXPR:
1830     case C_MAYBE_CONST_EXPR:
1831       mark_exp_read (TREE_OPERAND (exp, 1));
1832       break;
1833     default:
1834       break;
1835     }
1836 }
1837
1838 /* Perform the default conversion of arrays and functions to pointers.
1839    Return the result of converting EXP.  For any other expression, just
1840    return EXP.
1841
1842    LOC is the location of the expression.  */
1843
1844 struct c_expr
1845 default_function_array_conversion (location_t loc, struct c_expr exp)
1846 {
1847   tree orig_exp = exp.value;
1848   tree type = TREE_TYPE (exp.value);
1849   enum tree_code code = TREE_CODE (type);
1850
1851   switch (code)
1852     {
1853     case ARRAY_TYPE:
1854       {
1855         bool not_lvalue = false;
1856         bool lvalue_array_p;
1857
1858         while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1859                 || CONVERT_EXPR_P (exp.value))
1860                && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1861           {
1862             if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1863               not_lvalue = true;
1864             exp.value = TREE_OPERAND (exp.value, 0);
1865           }
1866
1867         if (TREE_NO_WARNING (orig_exp))
1868           TREE_NO_WARNING (exp.value) = 1;
1869
1870         lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1871         if (!flag_isoc99 && !lvalue_array_p)
1872           {
1873             /* Before C99, non-lvalue arrays do not decay to pointers.
1874                Normally, using such an array would be invalid; but it can
1875                be used correctly inside sizeof or as a statement expression.
1876                Thus, do not give an error here; an error will result later.  */
1877             return exp;
1878           }
1879
1880         exp.value = array_to_pointer_conversion (loc, exp.value);
1881       }
1882       break;
1883     case FUNCTION_TYPE:
1884       exp.value = function_to_pointer_conversion (loc, exp.value);
1885       break;
1886     default:
1887       break;
1888     }
1889
1890   return exp;
1891 }
1892
1893 struct c_expr
1894 default_function_array_read_conversion (location_t loc, struct c_expr exp)
1895 {
1896   mark_exp_read (exp.value);
1897   return default_function_array_conversion (loc, exp);
1898 }
1899
1900 /* EXP is an expression of integer type.  Apply the integer promotions
1901    to it and return the promoted value.  */
1902
1903 tree
1904 perform_integral_promotions (tree exp)
1905 {
1906   tree type = TREE_TYPE (exp);
1907   enum tree_code code = TREE_CODE (type);
1908
1909   gcc_assert (INTEGRAL_TYPE_P (type));
1910
1911   /* Normally convert enums to int,
1912      but convert wide enums to something wider.  */
1913   if (code == ENUMERAL_TYPE)
1914     {
1915       type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1916                                           TYPE_PRECISION (integer_type_node)),
1917                                      ((TYPE_PRECISION (type)
1918                                        >= TYPE_PRECISION (integer_type_node))
1919                                       && TYPE_UNSIGNED (type)));
1920
1921       return convert (type, exp);
1922     }
1923
1924   /* ??? This should no longer be needed now bit-fields have their
1925      proper types.  */
1926   if (TREE_CODE (exp) == COMPONENT_REF
1927       && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1928       /* If it's thinner than an int, promote it like a
1929          c_promoting_integer_type_p, otherwise leave it alone.  */
1930       && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1931                                TYPE_PRECISION (integer_type_node)))
1932     return convert (integer_type_node, exp);
1933
1934   if (c_promoting_integer_type_p (type))
1935     {
1936       /* Preserve unsignedness if not really getting any wider.  */
1937       if (TYPE_UNSIGNED (type)
1938           && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1939         return convert (unsigned_type_node, exp);
1940
1941       return convert (integer_type_node, exp);
1942     }
1943
1944   return exp;
1945 }
1946
1947
1948 /* Perform default promotions for C data used in expressions.
1949    Enumeral types or short or char are converted to int.
1950    In addition, manifest constants symbols are replaced by their values.  */
1951
1952 tree
1953 default_conversion (tree exp)
1954 {
1955   tree orig_exp;
1956   tree type = TREE_TYPE (exp);
1957   enum tree_code code = TREE_CODE (type);
1958   tree promoted_type;
1959
1960   mark_exp_read (exp);
1961
1962   /* Functions and arrays have been converted during parsing.  */
1963   gcc_assert (code != FUNCTION_TYPE);
1964   if (code == ARRAY_TYPE)
1965     return exp;
1966
1967   /* Constants can be used directly unless they're not loadable.  */
1968   if (TREE_CODE (exp) == CONST_DECL)
1969     exp = DECL_INITIAL (exp);
1970
1971   /* Strip no-op conversions.  */
1972   orig_exp = exp;
1973   STRIP_TYPE_NOPS (exp);
1974
1975   if (TREE_NO_WARNING (orig_exp))
1976     TREE_NO_WARNING (exp) = 1;
1977
1978   if (code == VOID_TYPE)
1979     {
1980       error ("void value not ignored as it ought to be");
1981       return error_mark_node;
1982     }
1983
1984   exp = require_complete_type (exp);
1985   if (exp == error_mark_node)
1986     return error_mark_node;
1987
1988   promoted_type = targetm.promoted_type (type);
1989   if (promoted_type)
1990     return convert (promoted_type, exp);
1991
1992   if (INTEGRAL_TYPE_P (type))
1993     return perform_integral_promotions (exp);
1994
1995   return exp;
1996 }
1997 \f
1998 /* Look up COMPONENT in a structure or union TYPE.
1999
2000    If the component name is not found, returns NULL_TREE.  Otherwise,
2001    the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2002    stepping down the chain to the component, which is in the last
2003    TREE_VALUE of the list.  Normally the list is of length one, but if
2004    the component is embedded within (nested) anonymous structures or
2005    unions, the list steps down the chain to the component.  */
2006
2007 static tree
2008 lookup_field (tree type, tree component)
2009 {
2010   tree field;
2011
2012   /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2013      to the field elements.  Use a binary search on this array to quickly
2014      find the element.  Otherwise, do a linear search.  TYPE_LANG_SPECIFIC
2015      will always be set for structures which have many elements.  */
2016
2017   if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2018     {
2019       int bot, top, half;
2020       tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2021
2022       field = TYPE_FIELDS (type);
2023       bot = 0;
2024       top = TYPE_LANG_SPECIFIC (type)->s->len;
2025       while (top - bot > 1)
2026         {
2027           half = (top - bot + 1) >> 1;
2028           field = field_array[bot+half];
2029
2030           if (DECL_NAME (field) == NULL_TREE)
2031             {
2032               /* Step through all anon unions in linear fashion.  */
2033               while (DECL_NAME (field_array[bot]) == NULL_TREE)
2034                 {
2035                   field = field_array[bot++];
2036                   if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2037                       || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
2038                     {
2039                       tree anon = lookup_field (TREE_TYPE (field), component);
2040
2041                       if (anon)
2042                         return tree_cons (NULL_TREE, field, anon);
2043
2044                       /* The Plan 9 compiler permits referring
2045                          directly to an anonymous struct/union field
2046                          using a typedef name.  */
2047                       if (flag_plan9_extensions
2048                           && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2049                           && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2050                               == TYPE_DECL)
2051                           && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2052                               == component))
2053                         break;
2054                     }
2055                 }
2056
2057               /* Entire record is only anon unions.  */
2058               if (bot > top)
2059                 return NULL_TREE;
2060
2061               /* Restart the binary search, with new lower bound.  */
2062               continue;
2063             }
2064
2065           if (DECL_NAME (field) == component)
2066             break;
2067           if (DECL_NAME (field) < component)
2068             bot += half;
2069           else
2070             top = bot + half;
2071         }
2072
2073       if (DECL_NAME (field_array[bot]) == component)
2074         field = field_array[bot];
2075       else if (DECL_NAME (field) != component)
2076         return NULL_TREE;
2077     }
2078   else
2079     {
2080       for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2081         {
2082           if (DECL_NAME (field) == NULL_TREE
2083               && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2084                   || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2085             {
2086               tree anon = lookup_field (TREE_TYPE (field), component);
2087
2088               if (anon)
2089                 return tree_cons (NULL_TREE, field, anon);
2090
2091               /* The Plan 9 compiler permits referring directly to an
2092                  anonymous struct/union field using a typedef
2093                  name.  */
2094               if (flag_plan9_extensions
2095                   && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2096                   && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2097                   && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2098                       == component))
2099                 break;
2100             }
2101
2102           if (DECL_NAME (field) == component)
2103             break;
2104         }
2105
2106       if (field == NULL_TREE)
2107         return NULL_TREE;
2108     }
2109
2110   return tree_cons (NULL_TREE, field, NULL_TREE);
2111 }
2112
2113 /* Make an expression to refer to the COMPONENT field of structure or
2114    union value DATUM.  COMPONENT is an IDENTIFIER_NODE.  LOC is the
2115    location of the COMPONENT_REF.  */
2116
2117 tree
2118 build_component_ref (location_t loc, tree datum, tree component)
2119 {
2120   tree type = TREE_TYPE (datum);
2121   enum tree_code code = TREE_CODE (type);
2122   tree field = NULL;
2123   tree ref;
2124   bool datum_lvalue = lvalue_p (datum);
2125
2126   if (!objc_is_public (datum, component))
2127     return error_mark_node;
2128
2129   /* Detect Objective-C property syntax object.property.  */
2130   if (c_dialect_objc ()
2131       && (ref = objc_maybe_build_component_ref (datum, component)))
2132     return ref;
2133
2134   /* See if there is a field or component with name COMPONENT.  */
2135
2136   if (code == RECORD_TYPE || code == UNION_TYPE)
2137     {
2138       if (!COMPLETE_TYPE_P (type))
2139         {
2140           c_incomplete_type_error (NULL_TREE, type);
2141           return error_mark_node;
2142         }
2143
2144       field = lookup_field (type, component);
2145
2146       if (!field)
2147         {
2148           error_at (loc, "%qT has no member named %qE", type, component);
2149           return error_mark_node;
2150         }
2151
2152       /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2153          This might be better solved in future the way the C++ front
2154          end does it - by giving the anonymous entities each a
2155          separate name and type, and then have build_component_ref
2156          recursively call itself.  We can't do that here.  */
2157       do
2158         {
2159           tree subdatum = TREE_VALUE (field);
2160           int quals;
2161           tree subtype;
2162           bool use_datum_quals;
2163
2164           if (TREE_TYPE (subdatum) == error_mark_node)
2165             return error_mark_node;
2166
2167           /* If this is an rvalue, it does not have qualifiers in C
2168              standard terms and we must avoid propagating such
2169              qualifiers down to a non-lvalue array that is then
2170              converted to a pointer.  */
2171           use_datum_quals = (datum_lvalue
2172                              || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2173
2174           quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2175           if (use_datum_quals)
2176             quals |= TYPE_QUALS (TREE_TYPE (datum));
2177           subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2178
2179           ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2180                         NULL_TREE);
2181           SET_EXPR_LOCATION (ref, loc);
2182           if (TREE_READONLY (subdatum)
2183               || (use_datum_quals && TREE_READONLY (datum)))
2184             TREE_READONLY (ref) = 1;
2185           if (TREE_THIS_VOLATILE (subdatum)
2186               || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2187             TREE_THIS_VOLATILE (ref) = 1;
2188
2189           if (TREE_DEPRECATED (subdatum))
2190             warn_deprecated_use (subdatum, NULL_TREE);
2191
2192           datum = ref;
2193
2194           field = TREE_CHAIN (field);
2195         }
2196       while (field);
2197
2198       return ref;
2199     }
2200   else if (code != ERROR_MARK)
2201     error_at (loc,
2202               "request for member %qE in something not a structure or union",
2203               component);
2204
2205   return error_mark_node;
2206 }
2207 \f
2208 /* Given an expression PTR for a pointer, return an expression
2209    for the value pointed to.
2210    ERRORSTRING is the name of the operator to appear in error messages.
2211
2212    LOC is the location to use for the generated tree.  */
2213
2214 tree
2215 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2216 {
2217   tree pointer = default_conversion (ptr);
2218   tree type = TREE_TYPE (pointer);
2219   tree ref;
2220
2221   if (TREE_CODE (type) == POINTER_TYPE)
2222     {
2223       if (CONVERT_EXPR_P (pointer)
2224           || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2225         {
2226           /* If a warning is issued, mark it to avoid duplicates from
2227              the backend.  This only needs to be done at
2228              warn_strict_aliasing > 2.  */
2229           if (warn_strict_aliasing > 2)
2230             if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2231                                          type, TREE_OPERAND (pointer, 0)))
2232               TREE_NO_WARNING (pointer) = 1;
2233         }
2234
2235       if (TREE_CODE (pointer) == ADDR_EXPR
2236           && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2237               == TREE_TYPE (type)))
2238         {
2239           ref = TREE_OPERAND (pointer, 0);
2240           protected_set_expr_location (ref, loc);
2241           return ref;
2242         }
2243       else
2244         {
2245           tree t = TREE_TYPE (type);
2246
2247           ref = build1 (INDIRECT_REF, t, pointer);
2248
2249           if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2250             {
2251               error_at (loc, "dereferencing pointer to incomplete type");
2252               return error_mark_node;
2253             }
2254           if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2255             warning_at (loc, 0, "dereferencing %<void *%> pointer");
2256
2257           /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2258              so that we get the proper error message if the result is used
2259              to assign to.  Also, &* is supposed to be a no-op.
2260              And ANSI C seems to specify that the type of the result
2261              should be the const type.  */
2262           /* A de-reference of a pointer to const is not a const.  It is valid
2263              to change it via some other pointer.  */
2264           TREE_READONLY (ref) = TYPE_READONLY (t);
2265           TREE_SIDE_EFFECTS (ref)
2266             = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2267           TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2268           protected_set_expr_location (ref, loc);
2269           return ref;
2270         }
2271     }
2272   else if (TREE_CODE (pointer) != ERROR_MARK)
2273     invalid_indirection_error (loc, type, errstring);
2274
2275   return error_mark_node;
2276 }
2277
2278 /* This handles expressions of the form "a[i]", which denotes
2279    an array reference.
2280
2281    This is logically equivalent in C to *(a+i), but we may do it differently.
2282    If A is a variable or a member, we generate a primitive ARRAY_REF.
2283    This avoids forcing the array out of registers, and can work on
2284    arrays that are not lvalues (for example, members of structures returned
2285    by functions).
2286
2287    For vector types, allow vector[i] but not i[vector], and create
2288    *(((type*)&vectortype) + i) for the expression.
2289
2290    LOC is the location to use for the returned expression.  */
2291
2292 tree
2293 build_array_ref (location_t loc, tree array, tree index)
2294 {
2295   tree ret;
2296   bool swapped = false;
2297   if (TREE_TYPE (array) == error_mark_node
2298       || TREE_TYPE (index) == error_mark_node)
2299     return error_mark_node;
2300
2301   if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2302       && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2303       /* Allow vector[index] but not index[vector].  */
2304       && TREE_CODE (TREE_TYPE (array)) != VECTOR_TYPE)
2305     {
2306       tree temp;
2307       if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2308           && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2309         {
2310           error_at (loc,
2311             "subscripted value is neither array nor pointer nor vector");
2312
2313           return error_mark_node;
2314         }
2315       temp = array;
2316       array = index;
2317       index = temp;
2318       swapped = true;
2319     }
2320
2321   if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2322     {
2323       error_at (loc, "array subscript is not an integer");
2324       return error_mark_node;
2325     }
2326
2327   if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2328     {
2329       error_at (loc, "subscripted value is pointer to function");
2330       return error_mark_node;
2331     }
2332
2333   /* ??? Existing practice has been to warn only when the char
2334      index is syntactically the index, not for char[array].  */
2335   if (!swapped)
2336      warn_array_subscript_with_type_char (index);
2337
2338   /* Apply default promotions *after* noticing character types.  */
2339   index = default_conversion (index);
2340
2341   gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2342
2343   /* For vector[index], convert the vector to a
2344      pointer of the underlying type.  */
2345   if (TREE_CODE (TREE_TYPE (array)) == VECTOR_TYPE)
2346     {
2347       tree type = TREE_TYPE (array);
2348       tree type1;
2349
2350       if (TREE_CODE (index) == INTEGER_CST)
2351         if (!host_integerp (index, 1)
2352             || ((unsigned HOST_WIDE_INT) tree_low_cst (index, 1)
2353                >= TYPE_VECTOR_SUBPARTS (TREE_TYPE (array))))
2354           warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
2355
2356       c_common_mark_addressable_vec (array);
2357       type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
2358       type = build_pointer_type (type);
2359       type1 = build_pointer_type (TREE_TYPE (array));
2360       array = build1 (ADDR_EXPR, type1, array);
2361       array = convert (type, array);
2362     }
2363
2364   if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2365     {
2366       tree rval, type;
2367
2368       /* An array that is indexed by a non-constant
2369          cannot be stored in a register; we must be able to do
2370          address arithmetic on its address.
2371          Likewise an array of elements of variable size.  */
2372       if (TREE_CODE (index) != INTEGER_CST
2373           || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2374               && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2375         {
2376           if (!c_mark_addressable (array))
2377             return error_mark_node;
2378         }
2379       /* An array that is indexed by a constant value which is not within
2380          the array bounds cannot be stored in a register either; because we
2381          would get a crash in store_bit_field/extract_bit_field when trying
2382          to access a non-existent part of the register.  */
2383       if (TREE_CODE (index) == INTEGER_CST
2384           && TYPE_DOMAIN (TREE_TYPE (array))
2385           && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2386         {
2387           if (!c_mark_addressable (array))
2388             return error_mark_node;
2389         }
2390
2391       if (pedantic)
2392         {
2393           tree foo = array;
2394           while (TREE_CODE (foo) == COMPONENT_REF)
2395             foo = TREE_OPERAND (foo, 0);
2396           if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
2397             pedwarn (loc, OPT_pedantic,
2398                      "ISO C forbids subscripting %<register%> array");
2399           else if (!flag_isoc99 && !lvalue_p (foo))
2400             pedwarn (loc, OPT_pedantic,
2401                      "ISO C90 forbids subscripting non-lvalue array");
2402         }
2403
2404       type = TREE_TYPE (TREE_TYPE (array));
2405       rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2406       /* Array ref is const/volatile if the array elements are
2407          or if the array is.  */
2408       TREE_READONLY (rval)
2409         |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2410             | TREE_READONLY (array));
2411       TREE_SIDE_EFFECTS (rval)
2412         |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2413             | TREE_SIDE_EFFECTS (array));
2414       TREE_THIS_VOLATILE (rval)
2415         |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2416             /* This was added by rms on 16 Nov 91.
2417                It fixes  vol struct foo *a;  a->elts[1]
2418                in an inline function.
2419                Hope it doesn't break something else.  */
2420             | TREE_THIS_VOLATILE (array));
2421       ret = require_complete_type (rval);
2422       protected_set_expr_location (ret, loc);
2423       return ret;
2424     }
2425   else
2426     {
2427       tree ar = default_conversion (array);
2428
2429       if (ar == error_mark_node)
2430         return ar;
2431
2432       gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2433       gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2434
2435       return build_indirect_ref
2436         (loc, build_binary_op (loc, PLUS_EXPR, ar, index, 0),
2437          RO_ARRAY_INDEXING);
2438     }
2439 }
2440 \f
2441 /* Build an external reference to identifier ID.  FUN indicates
2442    whether this will be used for a function call.  LOC is the source
2443    location of the identifier.  This sets *TYPE to the type of the
2444    identifier, which is not the same as the type of the returned value
2445    for CONST_DECLs defined as enum constants.  If the type of the
2446    identifier is not available, *TYPE is set to NULL.  */
2447 tree
2448 build_external_ref (location_t loc, tree id, int fun, tree *type)
2449 {
2450   tree ref;
2451   tree decl = lookup_name (id);
2452
2453   /* In Objective-C, an instance variable (ivar) may be preferred to
2454      whatever lookup_name() found.  */
2455   decl = objc_lookup_ivar (decl, id);
2456
2457   *type = NULL;
2458   if (decl && decl != error_mark_node)
2459     {
2460       ref = decl;
2461       *type = TREE_TYPE (ref);
2462     }
2463   else if (fun)
2464     /* Implicit function declaration.  */
2465     ref = implicitly_declare (loc, id);
2466   else if (decl == error_mark_node)
2467     /* Don't complain about something that's already been
2468        complained about.  */
2469     return error_mark_node;
2470   else
2471     {
2472       undeclared_variable (loc, id);
2473       return error_mark_node;
2474     }
2475
2476   if (TREE_TYPE (ref) == error_mark_node)
2477     return error_mark_node;
2478
2479   if (TREE_DEPRECATED (ref))
2480     warn_deprecated_use (ref, NULL_TREE);
2481
2482   /* Recursive call does not count as usage.  */
2483   if (ref != current_function_decl)
2484     {
2485       TREE_USED (ref) = 1;
2486     }
2487
2488   if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2489     {
2490       if (!in_sizeof && !in_typeof)
2491         C_DECL_USED (ref) = 1;
2492       else if (DECL_INITIAL (ref) == 0
2493                && DECL_EXTERNAL (ref)
2494                && !TREE_PUBLIC (ref))
2495         record_maybe_used_decl (ref);
2496     }
2497
2498   if (TREE_CODE (ref) == CONST_DECL)
2499     {
2500       used_types_insert (TREE_TYPE (ref));
2501
2502       if (warn_cxx_compat
2503           && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2504           && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2505         {
2506           warning_at (loc, OPT_Wc___compat,
2507                       ("enum constant defined in struct or union "
2508                        "is not visible in C++"));
2509           inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2510         }
2511
2512       ref = DECL_INITIAL (ref);
2513       TREE_CONSTANT (ref) = 1;
2514     }
2515   else if (current_function_decl != 0
2516            && !DECL_FILE_SCOPE_P (current_function_decl)
2517            && (TREE_CODE (ref) == VAR_DECL
2518                || TREE_CODE (ref) == PARM_DECL
2519                || TREE_CODE (ref) == FUNCTION_DECL))
2520     {
2521       tree context = decl_function_context (ref);
2522
2523       if (context != 0 && context != current_function_decl)
2524         DECL_NONLOCAL (ref) = 1;
2525     }
2526   /* C99 6.7.4p3: An inline definition of a function with external
2527      linkage ... shall not contain a reference to an identifier with
2528      internal linkage.  */
2529   else if (current_function_decl != 0
2530            && DECL_DECLARED_INLINE_P (current_function_decl)
2531            && DECL_EXTERNAL (current_function_decl)
2532            && VAR_OR_FUNCTION_DECL_P (ref)
2533            && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
2534            && ! TREE_PUBLIC (ref)
2535            && DECL_CONTEXT (ref) != current_function_decl)
2536     record_inline_static (loc, current_function_decl, ref,
2537                           csi_internal);
2538
2539   return ref;
2540 }
2541
2542 /* Record details of decls possibly used inside sizeof or typeof.  */
2543 struct maybe_used_decl
2544 {
2545   /* The decl.  */
2546   tree decl;
2547   /* The level seen at (in_sizeof + in_typeof).  */
2548   int level;
2549   /* The next one at this level or above, or NULL.  */
2550   struct maybe_used_decl *next;
2551 };
2552
2553 static struct maybe_used_decl *maybe_used_decls;
2554
2555 /* Record that DECL, an undefined static function reference seen
2556    inside sizeof or typeof, might be used if the operand of sizeof is
2557    a VLA type or the operand of typeof is a variably modified
2558    type.  */
2559
2560 static void
2561 record_maybe_used_decl (tree decl)
2562 {
2563   struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2564   t->decl = decl;
2565   t->level = in_sizeof + in_typeof;
2566   t->next = maybe_used_decls;
2567   maybe_used_decls = t;
2568 }
2569
2570 /* Pop the stack of decls possibly used inside sizeof or typeof.  If
2571    USED is false, just discard them.  If it is true, mark them used
2572    (if no longer inside sizeof or typeof) or move them to the next
2573    level up (if still inside sizeof or typeof).  */
2574
2575 void
2576 pop_maybe_used (bool used)
2577 {
2578   struct maybe_used_decl *p = maybe_used_decls;
2579   int cur_level = in_sizeof + in_typeof;
2580   while (p && p->level > cur_level)
2581     {
2582       if (used)
2583         {
2584           if (cur_level == 0)
2585             C_DECL_USED (p->decl) = 1;
2586           else
2587             p->level = cur_level;
2588         }
2589       p = p->next;
2590     }
2591   if (!used || cur_level == 0)
2592     maybe_used_decls = p;
2593 }
2594
2595 /* Return the result of sizeof applied to EXPR.  */
2596
2597 struct c_expr
2598 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2599 {
2600   struct c_expr ret;
2601   if (expr.value == error_mark_node)
2602     {
2603       ret.value = error_mark_node;
2604       ret.original_code = ERROR_MARK;
2605       ret.original_type = NULL;
2606       pop_maybe_used (false);
2607     }
2608   else
2609     {
2610       bool expr_const_operands = true;
2611       tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2612                                        &expr_const_operands);
2613       ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2614       ret.original_code = ERROR_MARK;
2615       ret.original_type = NULL;
2616       if (c_vla_type_p (TREE_TYPE (folded_expr)))
2617         {
2618           /* sizeof is evaluated when given a vla (C99 6.5.3.4p2).  */
2619           ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2620                               folded_expr, ret.value);
2621           C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2622           SET_EXPR_LOCATION (ret.value, loc);
2623         }
2624       pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2625     }
2626   return ret;
2627 }
2628
2629 /* Return the result of sizeof applied to T, a structure for the type
2630    name passed to sizeof (rather than the type itself).  LOC is the
2631    location of the original expression.  */
2632
2633 struct c_expr
2634 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2635 {
2636   tree type;
2637   struct c_expr ret;
2638   tree type_expr = NULL_TREE;
2639   bool type_expr_const = true;
2640   type = groktypename (t, &type_expr, &type_expr_const);
2641   ret.value = c_sizeof (loc, type);
2642   ret.original_code = ERROR_MARK;
2643   ret.original_type = NULL;
2644   if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2645       && c_vla_type_p (type))
2646     {
2647       /* If the type is a [*] array, it is a VLA but is represented as
2648          having a size of zero.  In such a case we must ensure that
2649          the result of sizeof does not get folded to a constant by
2650          c_fully_fold, because if the size is evaluated the result is
2651          not constant and so constraints on zero or negative size
2652          arrays must not be applied when this sizeof call is inside
2653          another array declarator.  */
2654       if (!type_expr)
2655         type_expr = integer_zero_node;
2656       ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2657                           type_expr, ret.value);
2658       C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2659     }
2660   pop_maybe_used (type != error_mark_node
2661                   ? C_TYPE_VARIABLE_SIZE (type) : false);
2662   return ret;
2663 }
2664
2665 /* Build a function call to function FUNCTION with parameters PARAMS.
2666    The function call is at LOC.
2667    PARAMS is a list--a chain of TREE_LIST nodes--in which the
2668    TREE_VALUE of each node is a parameter-expression.
2669    FUNCTION's data type may be a function type or a pointer-to-function.  */
2670
2671 tree
2672 build_function_call (location_t loc, tree function, tree params)
2673 {
2674   VEC(tree,gc) *vec;
2675   tree ret;
2676
2677   vec = VEC_alloc (tree, gc, list_length (params));
2678   for (; params; params = TREE_CHAIN (params))
2679     VEC_quick_push (tree, vec, TREE_VALUE (params));
2680   ret = build_function_call_vec (loc, function, vec, NULL);
2681   VEC_free (tree, gc, vec);
2682   return ret;
2683 }
2684
2685 /* Build a function call to function FUNCTION with parameters PARAMS.
2686    ORIGTYPES, if not NULL, is a vector of types; each element is
2687    either NULL or the original type of the corresponding element in
2688    PARAMS.  The original type may differ from TREE_TYPE of the
2689    parameter for enums.  FUNCTION's data type may be a function type
2690    or pointer-to-function.  This function changes the elements of
2691    PARAMS.  */
2692
2693 tree
2694 build_function_call_vec (location_t loc, tree function, VEC(tree,gc) *params,
2695                          VEC(tree,gc) *origtypes)
2696 {
2697   tree fntype, fundecl = 0;
2698   tree name = NULL_TREE, result;
2699   tree tem;
2700   int nargs;
2701   tree *argarray;
2702
2703
2704   /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue.  */
2705   STRIP_TYPE_NOPS (function);
2706
2707   /* Convert anything with function type to a pointer-to-function.  */
2708   if (TREE_CODE (function) == FUNCTION_DECL)
2709     {
2710       /* Implement type-directed function overloading for builtins.
2711          resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2712          handle all the type checking.  The result is a complete expression
2713          that implements this function call.  */
2714       tem = resolve_overloaded_builtin (loc, function, params);
2715       if (tem)
2716         return tem;
2717
2718       name = DECL_NAME (function);
2719
2720       if (flag_tm)
2721         tm_malloc_replacement (function);
2722       fundecl = function;
2723       /* Atomic functions have type checking/casting already done.  They are 
2724          often rewritten and don't match the original parameter list.  */
2725       if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
2726         origtypes = NULL;
2727     }
2728   if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2729     function = function_to_pointer_conversion (loc, function);
2730
2731   /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2732      expressions, like those used for ObjC messenger dispatches.  */
2733   if (!VEC_empty (tree, params))
2734     function = objc_rewrite_function_call (function,
2735                                            VEC_index (tree, params, 0));
2736
2737   function = c_fully_fold (function, false, NULL);
2738
2739   fntype = TREE_TYPE (function);
2740
2741   if (TREE_CODE (fntype) == ERROR_MARK)
2742     return error_mark_node;
2743
2744   if (!(TREE_CODE (fntype) == POINTER_TYPE
2745         && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2746     {
2747       error_at (loc, "called object %qE is not a function", function);
2748       return error_mark_node;
2749     }
2750
2751   if (fundecl && TREE_THIS_VOLATILE (fundecl))
2752     current_function_returns_abnormally = 1;
2753
2754   /* fntype now gets the type of function pointed to.  */
2755   fntype = TREE_TYPE (fntype);
2756
2757   /* Convert the parameters to the types declared in the
2758      function prototype, or apply default promotions.  */
2759
2760   nargs = convert_arguments (TYPE_ARG_TYPES (fntype), params, origtypes,
2761                              function, fundecl);
2762   if (nargs < 0)
2763     return error_mark_node;
2764
2765   /* Check that the function is called through a compatible prototype.
2766      If it is not, replace the call by a trap, wrapped up in a compound
2767      expression if necessary.  This has the nice side-effect to prevent
2768      the tree-inliner from generating invalid assignment trees which may
2769      blow up in the RTL expander later.  */
2770   if (CONVERT_EXPR_P (function)
2771       && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2772       && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2773       && !comptypes (fntype, TREE_TYPE (tem)))
2774     {
2775       tree return_type = TREE_TYPE (fntype);
2776       tree trap = build_function_call (loc,
2777                                        builtin_decl_explicit (BUILT_IN_TRAP),
2778                                        NULL_TREE);
2779       int i;
2780
2781       /* This situation leads to run-time undefined behavior.  We can't,
2782          therefore, simply error unless we can prove that all possible
2783          executions of the program must execute the code.  */
2784       if (warning_at (loc, 0, "function called through a non-compatible type"))
2785         /* We can, however, treat "undefined" any way we please.
2786            Call abort to encourage the user to fix the program.  */
2787         inform (loc, "if this code is reached, the program will abort");
2788       /* Before the abort, allow the function arguments to exit or
2789          call longjmp.  */
2790       for (i = 0; i < nargs; i++)
2791         trap = build2 (COMPOUND_EXPR, void_type_node,
2792                        VEC_index (tree, params, i), trap);
2793
2794       if (VOID_TYPE_P (return_type))
2795         {
2796           if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2797             pedwarn (loc, 0,
2798                      "function with qualified void return type called");
2799           return trap;
2800         }
2801       else
2802         {
2803           tree rhs;
2804
2805           if (AGGREGATE_TYPE_P (return_type))
2806             rhs = build_compound_literal (loc, return_type,
2807                                           build_constructor (return_type, 0),
2808                                           false);
2809           else
2810             rhs = build_zero_cst (return_type);
2811
2812           return require_complete_type (build2 (COMPOUND_EXPR, return_type,
2813                                                 trap, rhs));
2814         }
2815     }
2816
2817   argarray = VEC_address (tree, params);
2818
2819   /* Check that arguments to builtin functions match the expectations.  */
2820   if (fundecl
2821       && DECL_BUILT_IN (fundecl)
2822       && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2823       && !check_builtin_function_arguments (fundecl, nargs, argarray))
2824     return error_mark_node;
2825
2826   /* Check that the arguments to the function are valid.  */
2827   check_function_arguments (fntype, nargs, argarray);
2828
2829   if (name != NULL_TREE
2830       && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
2831     {
2832       if (require_constant_value)
2833         result =
2834           fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
2835                                                  function, nargs, argarray);
2836       else
2837         result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
2838                                             function, nargs, argarray);
2839       if (TREE_CODE (result) == NOP_EXPR
2840           && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2841         STRIP_TYPE_NOPS (result);
2842     }
2843   else
2844     result = build_call_array_loc (loc, TREE_TYPE (fntype),
2845                                    function, nargs, argarray);
2846
2847   if (VOID_TYPE_P (TREE_TYPE (result)))
2848     {
2849       if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
2850         pedwarn (loc, 0,
2851                  "function with qualified void return type called");
2852       return result;
2853     }
2854   return require_complete_type (result);
2855 }
2856
2857 /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2858    and have vector types, V0 has the same type as V1, and the number of
2859    elements of V0, V1, MASK is the same.
2860
2861    In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2862    called with two arguments.  In this case implementation passes the
2863    first argument twice in order to share the same tree code.  This fact
2864    could enable the mask-values being twice the vector length.  This is
2865    an implementation accident and this semantics is not guaranteed to
2866    the user.  */
2867 tree
2868 c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask)
2869 {
2870   tree ret;
2871   bool wrap = true;
2872   bool maybe_const = false;
2873   bool two_arguments = false;
2874
2875   if (v1 == NULL_TREE)
2876     {
2877       two_arguments = true;
2878       v1 = v0;
2879     }
2880
2881   if (v0 == error_mark_node || v1 == error_mark_node
2882       || mask == error_mark_node)
2883     return error_mark_node;
2884
2885   if (TREE_CODE (TREE_TYPE (mask)) != VECTOR_TYPE
2886       || TREE_CODE (TREE_TYPE (TREE_TYPE (mask))) != INTEGER_TYPE)
2887     {
2888       error_at (loc, "__builtin_shuffle last argument must "
2889                      "be an integer vector");
2890       return error_mark_node;
2891     }
2892
2893   if (TREE_CODE (TREE_TYPE (v0)) != VECTOR_TYPE
2894       || TREE_CODE (TREE_TYPE (v1)) != VECTOR_TYPE)
2895     {
2896       error_at (loc, "__builtin_shuffle arguments must be vectors");
2897       return error_mark_node;
2898     }
2899
2900   if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2901     {
2902       error_at (loc, "__builtin_shuffle argument vectors must be of "
2903                      "the same type");
2904       return error_mark_node;
2905     }
2906
2907   if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2908       != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2909       && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2910          != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2911     {
2912       error_at (loc, "__builtin_shuffle number of elements of the "
2913                      "argument vector(s) and the mask vector should "
2914                      "be the same");
2915       return error_mark_node;
2916     }
2917
2918   if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2919       != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2920     {
2921       error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2922                      "must have the same size as inner type of the mask");
2923       return error_mark_node;
2924     }
2925
2926   /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR.  */
2927   v0 = c_fully_fold (v0, false, &maybe_const);
2928   wrap &= maybe_const;
2929
2930   if (two_arguments)
2931     v1 = v0 = save_expr (v0);
2932   else
2933     {
2934       v1 = c_fully_fold (v1, false, &maybe_const);
2935       wrap &= maybe_const;
2936     }
2937
2938   mask = c_fully_fold (mask, false, &maybe_const);
2939   wrap &= maybe_const;
2940
2941   ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2942
2943   if (!wrap)
2944     ret = c_wrap_maybe_const (ret, true);
2945
2946   return ret;
2947 }
2948 \f
2949 /* Convert the argument expressions in the vector VALUES
2950    to the types in the list TYPELIST.
2951
2952    If TYPELIST is exhausted, or when an element has NULL as its type,
2953    perform the default conversions.
2954
2955    ORIGTYPES is the original types of the expressions in VALUES.  This
2956    holds the type of enum values which have been converted to integral
2957    types.  It may be NULL.
2958
2959    FUNCTION is a tree for the called function.  It is used only for
2960    error messages, where it is formatted with %qE.
2961
2962    This is also where warnings about wrong number of args are generated.
2963
2964    Returns the actual number of arguments processed (which may be less
2965    than the length of VALUES in some error situations), or -1 on
2966    failure.  */
2967
2968 static int
2969 convert_arguments (tree typelist, VEC(tree,gc) *values,
2970                    VEC(tree,gc) *origtypes, tree function, tree fundecl)
2971 {
2972   tree typetail, val;
2973   unsigned int parmnum;
2974   bool error_args = false;
2975   const bool type_generic = fundecl
2976     && lookup_attribute ("type generic", TYPE_ATTRIBUTES(TREE_TYPE (fundecl)));
2977   bool type_generic_remove_excess_precision = false;
2978   tree selector;
2979
2980   /* Change pointer to function to the function itself for
2981      diagnostics.  */
2982   if (TREE_CODE (function) == ADDR_EXPR
2983       && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2984     function = TREE_OPERAND (function, 0);
2985
2986   /* Handle an ObjC selector specially for diagnostics.  */
2987   selector = objc_message_selector ();
2988
2989   /* For type-generic built-in functions, determine whether excess
2990      precision should be removed (classification) or not
2991      (comparison).  */
2992   if (type_generic
2993       && DECL_BUILT_IN (fundecl)
2994       && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
2995     {
2996       switch (DECL_FUNCTION_CODE (fundecl))
2997         {
2998         case BUILT_IN_ISFINITE:
2999         case BUILT_IN_ISINF:
3000         case BUILT_IN_ISINF_SIGN:
3001         case BUILT_IN_ISNAN:
3002         case BUILT_IN_ISNORMAL:
3003         case BUILT_IN_FPCLASSIFY:
3004           type_generic_remove_excess_precision = true;
3005           break;
3006
3007         default:
3008           type_generic_remove_excess_precision = false;
3009           break;
3010         }
3011     }
3012
3013   /* Scan the given expressions and types, producing individual
3014      converted arguments.  */
3015
3016   for (typetail = typelist, parmnum = 0;
3017        VEC_iterate (tree, values, parmnum, val);
3018        ++parmnum)
3019     {
3020       tree type = typetail ? TREE_VALUE (typetail) : 0;
3021       tree valtype = TREE_TYPE (val);
3022       tree rname = function;
3023       int argnum = parmnum + 1;
3024       const char *invalid_func_diag;
3025       bool excess_precision = false;
3026       bool npc;
3027       tree parmval;
3028
3029       if (type == void_type_node)
3030         {
3031           if (selector)
3032             error_at (input_location,
3033                       "too many arguments to method %qE", selector);
3034           else
3035             error_at (input_location,
3036                       "too many arguments to function %qE", function);
3037
3038           if (fundecl && !DECL_BUILT_IN (fundecl))
3039             inform (DECL_SOURCE_LOCATION (fundecl), "declared here");
3040           return parmnum;
3041         }
3042
3043       if (selector && argnum > 2)
3044         {
3045           rname = selector;
3046           argnum -= 2;
3047         }
3048
3049       npc = null_pointer_constant_p (val);
3050
3051       /* If there is excess precision and a prototype, convert once to
3052          the required type rather than converting via the semantic
3053          type.  Likewise without a prototype a float value represented
3054          as long double should be converted once to double.  But for
3055          type-generic classification functions excess precision must
3056          be removed here.  */
3057       if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3058           && (type || !type_generic || !type_generic_remove_excess_precision))
3059         {
3060           val = TREE_OPERAND (val, 0);
3061           excess_precision = true;
3062         }
3063       val = c_fully_fold (val, false, NULL);
3064       STRIP_TYPE_NOPS (val);
3065
3066       val = require_complete_type (val);
3067
3068       if (type != 0)
3069         {
3070           /* Formal parm type is specified by a function prototype.  */
3071
3072           if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3073             {
3074               error ("type of formal parameter %d is incomplete", parmnum + 1);
3075               parmval = val;
3076             }
3077           else
3078             {
3079               tree origtype;
3080
3081               /* Optionally warn about conversions that
3082                  differ from the default conversions.  */
3083               if (warn_traditional_conversion || warn_traditional)
3084                 {
3085                   unsigned int formal_prec = TYPE_PRECISION (type);
3086
3087                   if (INTEGRAL_TYPE_P (type)
3088                       && TREE_CODE (valtype) == REAL_TYPE)
3089                     warning (0, "passing argument %d of %qE as integer "
3090                              "rather than floating due to prototype",
3091                              argnum, rname);
3092                   if (INTEGRAL_TYPE_P (type)
3093                       && TREE_CODE (valtype) == COMPLEX_TYPE)
3094                     warning (0, "passing argument %d of %qE as integer "
3095                              "rather than complex due to prototype",
3096                              argnum, rname);
3097                   else if (TREE_CODE (type) == COMPLEX_TYPE
3098                            && TREE_CODE (valtype) == REAL_TYPE)
3099                     warning (0, "passing argument %d of %qE as complex "
3100                              "rather than floating due to prototype",
3101                              argnum, rname);
3102                   else if (TREE_CODE (type) == REAL_TYPE
3103                            && INTEGRAL_TYPE_P (valtype))
3104                     warning (0, "passing argument %d of %qE as floating "
3105                              "rather than integer due to prototype",
3106                              argnum, rname);
3107                   else if (TREE_CODE (type) == COMPLEX_TYPE
3108                            && INTEGRAL_TYPE_P (valtype))
3109                     warning (0, "passing argument %d of %qE as complex "
3110                              "rather than integer due to prototype",
3111                              argnum, rname);
3112                   else if (TREE_CODE (type) == REAL_TYPE
3113                            && TREE_CODE (valtype) == COMPLEX_TYPE)
3114                     warning (0, "passing argument %d of %qE as floating "
3115                              "rather than complex due to prototype",
3116                              argnum, rname);
3117                   /* ??? At some point, messages should be written about
3118                      conversions between complex types, but that's too messy
3119                      to do now.  */
3120                   else if (TREE_CODE (type) == REAL_TYPE
3121                            && TREE_CODE (valtype) == REAL_TYPE)
3122                     {
3123                       /* Warn if any argument is passed as `float',
3124                          since without a prototype it would be `double'.  */
3125                       if (formal_prec == TYPE_PRECISION (float_type_node)
3126                           && type != dfloat32_type_node)
3127                         warning (0, "passing argument %d of %qE as %<float%> "
3128                                  "rather than %<double%> due to prototype",
3129                                  argnum, rname);
3130
3131                       /* Warn if mismatch between argument and prototype
3132                          for decimal float types.  Warn of conversions with
3133                          binary float types and of precision narrowing due to
3134                          prototype. */
3135                       else if (type != valtype
3136                                && (type == dfloat32_type_node
3137                                    || type == dfloat64_type_node
3138                                    || type == dfloat128_type_node
3139                                    || valtype == dfloat32_type_node
3140                                    || valtype == dfloat64_type_node
3141                                    || valtype == dfloat128_type_node)
3142                                && (formal_prec
3143                                    <= TYPE_PRECISION (valtype)
3144                                    || (type == dfloat128_type_node
3145                                        && (valtype
3146                                            != dfloat64_type_node
3147                                            && (valtype
3148                                                != dfloat32_type_node)))
3149                                    || (type == dfloat64_type_node
3150                                        && (valtype
3151                                            != dfloat32_type_node))))
3152                         warning (0, "passing argument %d of %qE as %qT "
3153                                  "rather than %qT due to prototype",
3154                                  argnum, rname, type, valtype);
3155
3156                     }
3157                   /* Detect integer changing in width or signedness.
3158                      These warnings are only activated with
3159                      -Wtraditional-conversion, not with -Wtraditional.  */
3160                   else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
3161                            && INTEGRAL_TYPE_P (valtype))
3162                     {
3163                       tree would_have_been = default_conversion (val);
3164                       tree type1 = TREE_TYPE (would_have_been);
3165
3166                       if (TREE_CODE (type) == ENUMERAL_TYPE
3167                           && (TYPE_MAIN_VARIANT (type)
3168                               == TYPE_MAIN_VARIANT (valtype)))
3169                         /* No warning if function asks for enum
3170                            and the actual arg is that enum type.  */
3171                         ;
3172                       else if (formal_prec != TYPE_PRECISION (type1))
3173                         warning (OPT_Wtraditional_conversion,
3174                                  "passing argument %d of %qE "
3175                                  "with different width due to prototype",
3176                                  argnum, rname);
3177                       else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3178                         ;
3179                       /* Don't complain if the formal parameter type
3180                          is an enum, because we can't tell now whether
3181                          the value was an enum--even the same enum.  */
3182                       else if (TREE_CODE (type) == ENUMERAL_TYPE)
3183                         ;
3184                       else if (TREE_CODE (val) == INTEGER_CST
3185                                && int_fits_type_p (val, type))
3186                         /* Change in signedness doesn't matter
3187                            if a constant value is unaffected.  */
3188                         ;
3189                       /* If the value is extended from a narrower
3190                          unsigned type, it doesn't matter whether we
3191                          pass it as signed or unsigned; the value
3192                          certainly is the same either way.  */
3193                       else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3194                                && TYPE_UNSIGNED (valtype))
3195                         ;
3196                       else if (TYPE_UNSIGNED (type))
3197                         warning (OPT_Wtraditional_conversion,
3198                                  "passing argument %d of %qE "
3199                                  "as unsigned due to prototype",
3200                                  argnum, rname);
3201                       else
3202                         warning (OPT_Wtraditional_conversion,
3203                                  "passing argument %d of %qE "
3204                                  "as signed due to prototype", argnum, rname);
3205                     }
3206                 }
3207
3208               /* Possibly restore an EXCESS_PRECISION_EXPR for the
3209                  sake of better warnings from convert_and_check.  */
3210               if (excess_precision)
3211                 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3212               origtype = (origtypes == NULL
3213                           ? NULL_TREE
3214                           : VEC_index (tree, origtypes, parmnum));
3215               parmval = convert_for_assignment (input_location, type, val,
3216                                                 origtype, ic_argpass, npc,
3217                                                 fundecl, function,
3218                                                 parmnum + 1);
3219
3220               if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3221                   && INTEGRAL_TYPE_P (type)
3222                   && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3223                 parmval = default_conversion (parmval);
3224             }
3225         }
3226       else if (TREE_CODE (valtype) == REAL_TYPE
3227                && (TYPE_PRECISION (valtype)
3228                    < TYPE_PRECISION (double_type_node))
3229                && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3230         {
3231           if (type_generic)
3232             parmval = val;
3233           else
3234             {
3235               /* Convert `float' to `double'.  */
3236               if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3237                 warning (OPT_Wdouble_promotion,
3238                          "implicit conversion from %qT to %qT when passing "
3239                          "argument to function",
3240                          valtype, double_type_node);
3241               parmval = convert (double_type_node, val);
3242             }
3243         }
3244       else if (excess_precision && !type_generic)
3245         /* A "double" argument with excess precision being passed
3246            without a prototype or in variable arguments.  */
3247         parmval = convert (valtype, val);
3248       else if ((invalid_func_diag =
3249                 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3250         {
3251           error (invalid_func_diag);
3252           return -1;
3253         }
3254       else
3255         /* Convert `short' and `char' to full-size `int'.  */
3256         parmval = default_conversion (val);
3257
3258       VEC_replace (tree, values, parmnum, parmval);
3259       if (parmval == error_mark_node)
3260         error_args = true;
3261
3262       if (typetail)
3263         typetail = TREE_CHAIN (typetail);
3264     }
3265
3266   gcc_assert (parmnum == VEC_length (tree, values));
3267
3268   if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3269     {
3270       error_at (input_location,
3271                 "too few arguments to function %qE", function);
3272       if (fundecl && !DECL_BUILT_IN (fundecl))
3273         inform (DECL_SOURCE_LOCATION (fundecl), "declared here");
3274       return -1;
3275     }
3276
3277   return error_args ? -1 : (int) parmnum;
3278 }
3279 \f
3280 /* This is the entry point used by the parser to build unary operators
3281    in the input.  CODE, a tree_code, specifies the unary operator, and
3282    ARG is the operand.  For unary plus, the C parser currently uses
3283    CONVERT_EXPR for code.
3284
3285    LOC is the location to use for the tree generated.
3286 */
3287
3288 struct c_expr
3289 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3290 {
3291   struct c_expr result;
3292
3293   result.value = build_unary_op (loc, code, arg.value, 0);
3294   result.original_code = code;
3295   result.original_type = NULL;
3296
3297   if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3298     overflow_warning (loc, result.value);
3299
3300   return result;
3301 }
3302
3303 /* This is the entry point used by the parser to build binary operators
3304    in the input.  CODE, a tree_code, specifies the binary operator, and
3305    ARG1 and ARG2 are the operands.  In addition to constructing the
3306    expression, we check for operands that were written with other binary
3307    operators in a way that is likely to confuse the user.
3308
3309    LOCATION is the location of the binary operator.  */
3310
3311 struct c_expr
3312 parser_build_binary_op (location_t location, enum tree_code code,
3313                         struct c_expr arg1, struct c_expr arg2)
3314 {
3315   struct c_expr result;
3316
3317   enum tree_code code1 = arg1.original_code;
3318   enum tree_code code2 = arg2.original_code;
3319   tree type1 = (arg1.original_type
3320                 ? arg1.original_type
3321                 : TREE_TYPE (arg1.value));
3322   tree type2 = (arg2.original_type
3323                 ? arg2.original_type
3324                 : TREE_TYPE (arg2.value));
3325
3326   result.value = build_binary_op (location, code,
3327                                   arg1.value, arg2.value, 1);
3328   result.original_code = code;
3329   result.original_type = NULL;
3330
3331   if (TREE_CODE (result.value) == ERROR_MARK)
3332     return result;
3333
3334   if (location != UNKNOWN_LOCATION)
3335     protected_set_expr_location (result.value, location);
3336
3337   /* Check for cases such as x+y<<z which users are likely
3338      to misinterpret.  */
3339   if (warn_parentheses)
3340     warn_about_parentheses (code, code1, arg1.value, code2, arg2.value);
3341
3342   if (warn_logical_op)
3343     warn_logical_operator (input_location, code, TREE_TYPE (result.value),
3344                            code1, arg1.value, code2, arg2.value);
3345
3346   /* Warn about comparisons against string literals, with the exception
3347      of testing for equality or inequality of a string literal with NULL.  */
3348   if (code == EQ_EXPR || code == NE_EXPR)
3349     {
3350       if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3351           || (code2 == STRING_CST && !integer_zerop (arg1.value)))
3352         warning_at (location, OPT_Waddress,
3353                     "comparison with string literal results in unspecified behavior");
3354     }
3355   else if (TREE_CODE_CLASS (code) == tcc_comparison
3356            && (code1 == STRING_CST || code2 == STRING_CST))
3357     warning_at (location, OPT_Waddress,
3358                 "comparison with string literal results in unspecified behavior");
3359
3360   if (TREE_OVERFLOW_P (result.value)
3361       && !TREE_OVERFLOW_P (arg1.value)
3362       && !TREE_OVERFLOW_P (arg2.value))
3363     overflow_warning (location, result.value);
3364
3365   /* Warn about comparisons of different enum types.  */
3366   if (warn_enum_compare
3367       && TREE_CODE_CLASS (code) == tcc_comparison
3368       && TREE_CODE (type1) == ENUMERAL_TYPE
3369       && TREE_CODE (type2) == ENUMERAL_TYPE
3370       && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3371     warning_at (location, OPT_Wenum_compare,
3372                 "comparison between %qT and %qT",
3373                 type1, type2);
3374
3375   return result;
3376 }
3377 \f
3378 /* Return a tree for the difference of pointers OP0 and OP1.
3379    The resulting tree has type int.  */
3380
3381 static tree
3382 pointer_diff (location_t loc, tree op0, tree op1)
3383 {
3384   tree restype = ptrdiff_type_node;
3385   tree result, inttype;
3386
3387   addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3388   addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3389   tree target_type = TREE_TYPE (TREE_TYPE (op0));
3390   tree con0, con1, lit0, lit1;
3391   tree orig_op1 = op1;
3392
3393   /* If the operands point into different address spaces, we need to
3394      explicitly convert them to pointers into the common address space
3395      before we can subtract the numerical address values.  */
3396   if (as0 != as1)
3397     {
3398       addr_space_t as_common;
3399       tree common_type;
3400
3401       /* Determine the common superset address space.  This is guaranteed
3402          to exist because the caller verified that comp_target_types
3403          returned non-zero.  */
3404       if (!addr_space_superset (as0, as1, &as_common))
3405         gcc_unreachable ();
3406
3407       common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3408       op0 = convert (common_type, op0);
3409       op1 = convert (common_type, op1);
3410     }
3411
3412   /* Determine integer type to perform computations in.  This will usually
3413      be the same as the result type (ptrdiff_t), but may need to be a wider
3414      type if pointers for the address space are wider than ptrdiff_t.  */
3415   if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3416     inttype = lang_hooks.types.type_for_size
3417                 (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3418   else
3419     inttype = restype;
3420
3421
3422   if (TREE_CODE (target_type) == VOID_TYPE)
3423     pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3424              "pointer of type %<void *%> used in subtraction");
3425   if (TREE_CODE (target_type) == FUNCTION_TYPE)
3426     pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3427              "pointer to a function used in subtraction");
3428
3429   /* If the conversion to ptrdiff_type does anything like widening or
3430      converting a partial to an integral mode, we get a convert_expression
3431      that is in the way to do any simplifications.
3432      (fold-const.c doesn't know that the extra bits won't be needed.
3433      split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
3434      different mode in place.)
3435      So first try to find a common term here 'by hand'; we want to cover
3436      at least the cases that occur in legal static initializers.  */
3437   if (CONVERT_EXPR_P (op0)
3438       && (TYPE_PRECISION (TREE_TYPE (op0))
3439           == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
3440     con0 = TREE_OPERAND (op0, 0);
3441   else
3442     con0 = op0;
3443   if (CONVERT_EXPR_P (op1)
3444       && (TYPE_PRECISION (TREE_TYPE (op1))
3445           == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))))
3446     con1 = TREE_OPERAND (op1, 0);
3447   else
3448     con1 = op1;
3449
3450   if (TREE_CODE (con0) == PLUS_EXPR)
3451     {
3452       lit0 = TREE_OPERAND (con0, 1);
3453       con0 = TREE_OPERAND (con0, 0);
3454     }
3455   else
3456     lit0 = integer_zero_node;
3457
3458   if (TREE_CODE (con1) == PLUS_EXPR)
3459     {
3460       lit1 = TREE_OPERAND (con1, 1);
3461       con1 = TREE_OPERAND (con1, 0);
3462     }
3463   else
3464     lit1 = integer_zero_node;
3465
3466   if (operand_equal_p (con0, con1, 0))
3467     {
3468       op0 = lit0;
3469       op1 = lit1;
3470     }
3471
3472
3473   /* First do the subtraction as integers;
3474      then drop through to build the divide operator.
3475      Do not do default conversions on the minus operator
3476      in case restype is a short type.  */
3477
3478   op0 = build_binary_op (loc,
3479                          MINUS_EXPR, convert (inttype, op0),
3480                          convert (inttype, op1), 0);
3481   /* This generates an error if op1 is pointer to incomplete type.  */
3482   if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3483     error_at (loc, "arithmetic on pointer to an incomplete type");
3484
3485   /* This generates an error if op0 is pointer to incomplete type.  */
3486   op1 = c_size_in_bytes (target_type);
3487
3488   /* Divide by the size, in easiest possible way.  */
3489   result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3490                             op0, convert (inttype, op1));
3491
3492   /* Convert to final result type if necessary.  */
3493   return convert (restype, result);
3494 }
3495 \f
3496 /* Construct and perhaps optimize a tree representation
3497    for a unary operation.  CODE, a tree_code, specifies the operation
3498    and XARG is the operand.
3499    For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3500    the default promotions (such as from short to int).
3501    For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3502    allows non-lvalues; this is only used to handle conversion of non-lvalue
3503    arrays to pointers in C99.
3504
3505    LOCATION is the location of the operator.  */
3506
3507 tree
3508 build_unary_op (location_t location,
3509                 enum tree_code code, tree xarg, int flag)
3510 {
3511   /* No default_conversion here.  It causes trouble for ADDR_EXPR.  */
3512   tree arg = xarg;
3513   tree argtype = 0;
3514   enum tree_code typecode;
3515   tree val;
3516   tree ret = error_mark_node;
3517   tree eptype = NULL_TREE;
3518   int noconvert = flag;
3519   const char *invalid_op_diag;
3520   bool int_operands;
3521
3522   int_operands = EXPR_INT_CONST_OPERANDS (xarg);
3523   if (int_operands)
3524     arg = remove_c_maybe_const_expr (arg);
3525
3526   if (code != ADDR_EXPR)
3527     arg = require_complete_type (arg);
3528
3529   typecode = TREE_CODE (TREE_TYPE (arg));
3530   if (typecode == ERROR_MARK)
3531     return error_mark_node;
3532   if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3533     typecode = INTEGER_TYPE;
3534
3535   if ((invalid_op_diag
3536        = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3537     {
3538       error_at (location, invalid_op_diag);
3539       return error_mark_node;
3540     }
3541
3542   if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3543     {
3544       eptype = TREE_TYPE (arg);
3545       arg = TREE_OPERAND (arg, 0);
3546     }
3547
3548   switch (code)
3549     {
3550     case CONVERT_EXPR:
3551       /* This is used for unary plus, because a CONVERT_EXPR
3552          is enough to prevent anybody from looking inside for
3553          associativity, but won't generate any code.  */
3554       if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3555             || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3556             || typecode == VECTOR_TYPE))
3557         {
3558           error_at (location, "wrong type argument to unary plus");
3559           return error_mark_node;
3560         }
3561       else if (!noconvert)
3562         arg = default_conversion (arg);
3563       arg = non_lvalue_loc (location, arg);
3564       break;
3565
3566     case NEGATE_EXPR:
3567       if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3568             || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3569             || typecode == VECTOR_TYPE))
3570         {
3571           error_at (location, "wrong type argument to unary minus");
3572           return error_mark_node;
3573         }
3574       else if (!noconvert)
3575         arg = default_conversion (arg);
3576       break;
3577
3578     case BIT_NOT_EXPR:
3579       /* ~ works on integer types and non float vectors. */
3580       if (typecode == INTEGER_TYPE
3581           || (typecode == VECTOR_TYPE
3582               && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
3583         {
3584           if (!noconvert)
3585             arg = default_conversion (arg);
3586         }
3587       else if (typecode == COMPLEX_TYPE)
3588         {
3589           code = CONJ_EXPR;
3590           pedwarn (location, OPT_pedantic,
3591                    "ISO C does not support %<~%> for complex conjugation");
3592           if (!noconvert)
3593             arg = default_conversion (arg);
3594         }
3595       else
3596         {
3597           error_at (location, "wrong type argument to bit-complement");
3598           return error_mark_node;
3599         }
3600       break;
3601
3602     case ABS_EXPR:
3603       if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
3604         {
3605           error_at (location, "wrong type argument to abs");
3606           return error_mark_node;
3607         }
3608       else if (!noconvert)
3609         arg = default_conversion (arg);
3610       break;
3611
3612     case CONJ_EXPR:
3613       /* Conjugating a real value is a no-op, but allow it anyway.  */
3614       if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3615             || typecode == COMPLEX_TYPE))
3616         {
3617           error_at (location, "wrong type argument to conjugation");
3618           return error_mark_node;
3619         }
3620       else if (!noconvert)
3621         arg = default_conversion (arg);
3622       break;
3623
3624     case TRUTH_NOT_EXPR:
3625       if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3626           && typecode != REAL_TYPE && typecode != POINTER_TYPE
3627           && typecode != COMPLEX_TYPE)
3628         {
3629           error_at (location,
3630                     "wrong type argument to unary exclamation mark");
3631           return error_mark_node;
3632         }
3633       arg = c_objc_common_truthvalue_conversion (location, arg);
3634       ret = invert_truthvalue_loc (location, arg);
3635       /* If the TRUTH_NOT_EXPR has been folded, reset the location.  */
3636       if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3637         location = EXPR_LOCATION (ret);
3638       goto return_build_unary_op;
3639
3640     case REALPART_EXPR:
3641     case IMAGPART_EXPR:
3642       ret = build_real_imag_expr (location, code, arg);
3643       if (ret == error_mark_node)
3644         return error_mark_node;
3645       if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3646         eptype = TREE_TYPE (eptype);
3647       goto return_build_unary_op;
3648
3649     case PREINCREMENT_EXPR:
3650     case POSTINCREMENT_EXPR:
3651     case PREDECREMENT_EXPR:
3652     case POSTDECREMENT_EXPR:
3653
3654       if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3655         {
3656           tree inner = build_unary_op (location, code,
3657                                        C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3658           if (inner == error_mark_node)
3659             return error_mark_node;
3660           ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3661                         C_MAYBE_CONST_EXPR_PRE (arg), inner);
3662           gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3663           C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3664           goto return_build_unary_op;
3665         }
3666
3667       /* Complain about anything that is not a true lvalue.  In
3668          Objective-C, skip this check for property_refs.  */
3669       if (!objc_is_property_ref (arg)
3670           && !lvalue_or_else (location,
3671                               arg, ((code == PREINCREMENT_EXPR
3672                                      || code == POSTINCREMENT_EXPR)
3673                                     ? lv_increment
3674                                     : lv_decrement)))
3675         return error_mark_node;
3676
3677       if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3678         {
3679           if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3680             warning_at (location, OPT_Wc___compat,
3681                         "increment of enumeration value is invalid in C++");
3682           else
3683             warning_at (location, OPT_Wc___compat,
3684                         "decrement of enumeration value is invalid in C++");
3685         }
3686
3687       /* Ensure the argument is fully folded inside any SAVE_EXPR.  */
3688       arg = c_fully_fold (arg, false, NULL);
3689
3690       /* Increment or decrement the real part of the value,
3691          and don't change the imaginary part.  */
3692       if (typecode == COMPLEX_TYPE)
3693         {
3694           tree real, imag;
3695
3696           pedwarn (location, OPT_pedantic,
3697                    "ISO C does not support %<++%> and %<--%> on complex types");
3698
3699           arg = stabilize_reference (arg);
3700           real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
3701           imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
3702           real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
3703           if (real == error_mark_node || imag == error_mark_node)
3704             return error_mark_node;
3705           ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
3706                         real, imag);
3707           goto return_build_unary_op;
3708         }
3709
3710       /* Report invalid types.  */
3711
3712       if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
3713           && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
3714         {
3715           if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3716             error_at (location, "wrong type argument to increment");
3717           else
3718             error_at (location, "wrong type argument to decrement");
3719
3720           return error_mark_node;
3721         }
3722
3723       {
3724         tree inc;
3725
3726         argtype = TREE_TYPE (arg);
3727
3728         /* Compute the increment.  */
3729
3730         if (typecode == POINTER_TYPE)
3731           {
3732             /* If pointer target is an undefined struct,
3733                we just cannot know how to do the arithmetic.  */
3734             if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3735               {
3736                 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3737                   error_at (location,
3738                             "increment of pointer to unknown structure");
3739                 else
3740                   error_at (location,
3741                             "decrement of pointer to unknown structure");
3742               }
3743             else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
3744                      || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
3745               {
3746                 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3747                   pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3748                            "wrong type argument to increment");
3749                 else
3750                   pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3751                            "wrong type argument to decrement");
3752               }
3753
3754             inc = c_size_in_bytes (TREE_TYPE (argtype));
3755             inc = convert_to_ptrofftype_loc (location, inc);
3756           }
3757         else if (FRACT_MODE_P (TYPE_MODE (argtype)))
3758           {
3759             /* For signed fract types, we invert ++ to -- or
3760                -- to ++, and change inc from 1 to -1, because
3761                it is not possible to represent 1 in signed fract constants.
3762                For unsigned fract types, the result always overflows and
3763                we get an undefined (original) or the maximum value.  */
3764             if (code == PREINCREMENT_EXPR)
3765               code = PREDECREMENT_EXPR;
3766             else if (code == PREDECREMENT_EXPR)
3767               code = PREINCREMENT_EXPR;
3768             else if (code == POSTINCREMENT_EXPR)
3769               code = POSTDECREMENT_EXPR;
3770             else /* code == POSTDECREMENT_EXPR  */
3771               code = POSTINCREMENT_EXPR;
3772
3773             inc = integer_minus_one_node;
3774             inc = convert (argtype, inc);
3775           }
3776         else
3777           {
3778             inc = integer_one_node;
3779             inc = convert (argtype, inc);
3780           }
3781
3782         /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
3783            need to ask Objective-C to build the increment or decrement
3784            expression for it.  */
3785         if (objc_is_property_ref (arg))
3786           return objc_build_incr_expr_for_property_ref (location, code,
3787                                                         arg, inc);
3788
3789         /* Report a read-only lvalue.  */
3790         if (TYPE_READONLY (argtype))
3791           {
3792             readonly_error (arg,
3793                             ((code == PREINCREMENT_EXPR
3794                               || code == POSTINCREMENT_EXPR)
3795                              ? lv_increment : lv_decrement));
3796             return error_mark_node;
3797           }
3798         else if (TREE_READONLY (arg))
3799           readonly_warning (arg,
3800                             ((code == PREINCREMENT_EXPR
3801                               || code == POSTINCREMENT_EXPR)
3802                              ? lv_increment : lv_decrement));
3803
3804         if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3805           val = boolean_increment (code, arg);
3806         else
3807           val = build2 (code, TREE_TYPE (arg), arg, inc);
3808         TREE_SIDE_EFFECTS (val) = 1;
3809         if (TREE_CODE (val) != code)
3810           TREE_NO_WARNING (val) = 1;
3811         ret = val;
3812         goto return_build_unary_op;
3813       }
3814
3815     case ADDR_EXPR:
3816       /* Note that this operation never does default_conversion.  */
3817
3818       /* The operand of unary '&' must be an lvalue (which excludes
3819          expressions of type void), or, in C99, the result of a [] or
3820          unary '*' operator.  */
3821       if (VOID_TYPE_P (TREE_TYPE (arg))
3822           && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
3823           && (TREE_CODE (arg) != INDIRECT_REF
3824               || !flag_isoc99))
3825         pedwarn (location, 0, "taking address of expression of type %<void%>");
3826
3827       /* Let &* cancel out to simplify resulting code.  */
3828       if (TREE_CODE (arg) == INDIRECT_REF)
3829         {
3830           /* Don't let this be an lvalue.  */
3831           if (lvalue_p (TREE_OPERAND (arg, 0)))
3832             return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
3833           ret = TREE_OPERAND (arg, 0);
3834           goto return_build_unary_op;
3835         }
3836
3837       /* For &x[y], return x+y */
3838       if (TREE_CODE (arg) == ARRAY_REF)
3839         {
3840           tree op0 = TREE_OPERAND (arg, 0);
3841           if (!c_mark_addressable (op0))
3842             return error_mark_node;
3843         }
3844
3845       /* Anything not already handled and not a true memory reference
3846          or a non-lvalue array is an error.  */
3847       else if (typecode != FUNCTION_TYPE && !flag
3848                && !lvalue_or_else (location, arg, lv_addressof))
3849         return error_mark_node;
3850
3851       /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
3852          folding later.  */
3853       if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3854         {
3855           tree inner = build_unary_op (location, code,
3856                                        C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3857           ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3858                         C_MAYBE_CONST_EXPR_PRE (arg), inner);
3859           gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3860           C_MAYBE_CONST_EXPR_NON_CONST (ret)
3861             = C_MAYBE_CONST_EXPR_NON_CONST (arg);
3862           goto return_build_unary_op;
3863         }
3864
3865       /* Ordinary case; arg is a COMPONENT_REF or a decl.  */
3866       argtype = TREE_TYPE (arg);
3867
3868       /* If the lvalue is const or volatile, merge that into the type
3869          to which the address will point.  This is only needed
3870          for function types.  */
3871       if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
3872           && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
3873           && TREE_CODE (argtype) == FUNCTION_TYPE)
3874         {
3875           int orig_quals = TYPE_QUALS (strip_array_types (argtype));
3876           int quals = orig_quals;
3877
3878           if (TREE_READONLY (arg))
3879             quals |= TYPE_QUAL_CONST;
3880           if (TREE_THIS_VOLATILE (arg))
3881             quals |= TYPE_QUAL_VOLATILE;
3882
3883           argtype = c_build_qualified_type (argtype, quals);
3884         }
3885
3886       if (!c_mark_addressable (arg))
3887         return error_mark_node;
3888
3889       gcc_assert (TREE_CODE (arg) != COMPONENT_REF
3890                   || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
3891
3892       argtype = build_pointer_type (argtype);
3893
3894       /* ??? Cope with user tricks that amount to offsetof.  Delete this
3895          when we have proper support for integer constant expressions.  */
3896       val = get_base_address (arg);
3897       if (val && TREE_CODE (val) == INDIRECT_REF
3898           && TREE_CONSTANT (TREE_OPERAND (val, 0)))
3899         {
3900           ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
3901           goto return_build_unary_op;
3902         }
3903
3904       val = build1 (ADDR_EXPR, argtype, arg);
3905
3906       ret = val;
3907       goto return_build_unary_op;
3908
3909     default:
3910       gcc_unreachable ();
3911     }
3912
3913   if (argtype == 0)
3914     argtype = TREE_TYPE (arg);
3915   if (TREE_CODE (arg) == INTEGER_CST)
3916     ret = (require_constant_value
3917            ? fold_build1_initializer_loc (location, code, argtype, arg)
3918            : fold_build1_loc (location, code, argtype, arg));
3919   else
3920     ret = build1 (code, argtype, arg);
3921  return_build_unary_op:
3922   gcc_assert (ret != error_mark_node);
3923   if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
3924       && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
3925     ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
3926   else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
3927     ret = note_integer_operands (ret);
3928   if (eptype)
3929     ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
3930   protected_set_expr_location (ret, location);
3931   return ret;
3932 }
3933
3934 /* Return nonzero if REF is an lvalue valid for this language.
3935    Lvalues can be assigned, unless their type has TYPE_READONLY.
3936    Lvalues can have their address taken, unless they have C_DECL_REGISTER.  */
3937
3938 bool
3939 lvalue_p (const_tree ref)
3940 {
3941   const enum tree_code code = TREE_CODE (ref);
3942
3943   switch (code)
3944     {
3945     case REALPART_EXPR:
3946     case IMAGPART_EXPR:
3947     case COMPONENT_REF:
3948       return lvalue_p (TREE_OPERAND (ref, 0));
3949
3950     case C_MAYBE_CONST_EXPR:
3951       return lvalue_p (TREE_OPERAND (ref, 1));
3952
3953     case COMPOUND_LITERAL_EXPR:
3954     case STRING_CST:
3955       return 1;
3956
3957     case INDIRECT_REF:
3958     case ARRAY_REF:
3959     case VAR_DECL:
3960     case PARM_DECL:
3961     case RESULT_DECL:
3962     case ERROR_MARK:
3963       return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3964               && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3965
3966     case BIND_EXPR:
3967       return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3968
3969     default:
3970       return 0;
3971     }
3972 }
3973 \f
3974 /* Give a warning for storing in something that is read-only in GCC
3975    terms but not const in ISO C terms.  */
3976
3977 static void
3978 readonly_warning (tree arg, enum lvalue_use use)
3979 {
3980   switch (use)
3981     {
3982     case lv_assign:
3983       warning (0, "assignment of read-only location %qE", arg);
3984       break;
3985     case lv_increment:
3986       warning (0, "increment of read-only location %qE", arg);
3987       break;
3988     case lv_decrement:
3989       warning (0, "decrement of read-only location %qE", arg);
3990       break;
3991     default:
3992       gcc_unreachable ();
3993     }
3994   return;
3995 }
3996
3997
3998 /* Return nonzero if REF is an lvalue valid for this language;
3999    otherwise, print an error message and return zero.  USE says
4000    how the lvalue is being used and so selects the error message.
4001    LOCATION is the location at which any error should be reported.  */
4002
4003 static int
4004 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
4005 {
4006   int win = lvalue_p (ref);
4007
4008   if (!win)