OSDN Git Service

* c.opt (Wdouble-promotion): New.
[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
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 "toplev.h"
39 #include "intl.h"
40 #include "target.h"
41 #include "tree-iterator.h"
42 #include "bitmap.h"
43 #include "gimple.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 /* Whether we are building a boolean conversion inside
55    convert_for_assignment, or some other late binary operation.  If
56    build_binary_op is called (from code shared with C++) in this case,
57    then the operands have already been folded and the result will not
58    be folded again, so C_MAYBE_CONST_EXPR should not be generated.  */
59 bool in_late_binary_op;
60
61 /* The level of nesting inside "__alignof__".  */
62 int in_alignof;
63
64 /* The level of nesting inside "sizeof".  */
65 int in_sizeof;
66
67 /* The level of nesting inside "typeof".  */
68 int in_typeof;
69
70 /* Nonzero if we've already printed a "missing braces around initializer"
71    message within this initializer.  */
72 static int missing_braces_mentioned;
73
74 static int require_constant_value;
75 static int require_constant_elements;
76
77 static bool null_pointer_constant_p (const_tree);
78 static tree qualify_type (tree, tree);
79 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
80                                          bool *);
81 static int comp_target_types (location_t, tree, tree);
82 static int function_types_compatible_p (const_tree, const_tree, bool *,
83                                         bool *);
84 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
85 static tree lookup_field (tree, tree);
86 static int convert_arguments (tree, VEC(tree,gc) *, VEC(tree,gc) *, tree,
87                               tree);
88 static tree pointer_diff (location_t, tree, tree);
89 static tree convert_for_assignment (location_t, tree, tree, tree,
90                                     enum impl_conv, bool, tree, tree, int);
91 static tree valid_compound_expr_initializer (tree, tree);
92 static void push_string (const char *);
93 static void push_member_name (tree);
94 static int spelling_length (void);
95 static char *print_spelling (char *);
96 static void warning_init (int, const char *);
97 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
98 static void output_init_element (tree, tree, bool, tree, tree, int, bool,
99                                  struct obstack *);
100 static void output_pending_init_elements (int, struct obstack *);
101 static int set_designator (int, struct obstack *);
102 static void push_range_stack (tree, struct obstack *);
103 static void add_pending_init (tree, tree, tree, bool, struct obstack *);
104 static void set_nonincremental_init (struct obstack *);
105 static void set_nonincremental_init_from_string (tree, struct obstack *);
106 static tree find_init_member (tree, struct obstack *);
107 static void readonly_error (tree, enum lvalue_use);
108 static void readonly_warning (tree, enum lvalue_use);
109 static int lvalue_or_else (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 (target);
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         /* Tell global_bindings_p to return false so that variable_size
519            doesn't die on VLAs in parameter types.  */
520         c_override_global_bindings_to_false = true;
521
522         len = list_length (p1);
523         newargs = 0;
524
525         for (i = 0; i < len; i++)
526           newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
527
528         n = newargs;
529
530         for (; p1;
531              p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
532           {
533             /* A null type means arg type is not specified.
534                Take whatever the other function type has.  */
535             if (TREE_VALUE (p1) == 0)
536               {
537                 TREE_VALUE (n) = TREE_VALUE (p2);
538                 goto parm_done;
539               }
540             if (TREE_VALUE (p2) == 0)
541               {
542                 TREE_VALUE (n) = TREE_VALUE (p1);
543                 goto parm_done;
544               }
545
546             /* Given  wait (union {union wait *u; int *i} *)
547                and  wait (union wait *),
548                prefer  union wait *  as type of parm.  */
549             if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
550                 && TREE_VALUE (p1) != TREE_VALUE (p2))
551               {
552                 tree memb;
553                 tree mv2 = TREE_VALUE (p2);
554                 if (mv2 && mv2 != error_mark_node
555                     && TREE_CODE (mv2) != ARRAY_TYPE)
556                   mv2 = TYPE_MAIN_VARIANT (mv2);
557                 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
558                      memb; memb = DECL_CHAIN (memb))
559                   {
560                     tree mv3 = TREE_TYPE (memb);
561                     if (mv3 && mv3 != error_mark_node
562                         && TREE_CODE (mv3) != ARRAY_TYPE)
563                       mv3 = TYPE_MAIN_VARIANT (mv3);
564                     if (comptypes (mv3, mv2))
565                       {
566                         TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
567                                                          TREE_VALUE (p2));
568                         pedwarn (input_location, OPT_pedantic,
569                                  "function types not truly compatible in ISO C");
570                         goto parm_done;
571                       }
572                   }
573               }
574             if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
575                 && TREE_VALUE (p2) != TREE_VALUE (p1))
576               {
577                 tree memb;
578                 tree mv1 = TREE_VALUE (p1);
579                 if (mv1 && mv1 != error_mark_node
580                     && TREE_CODE (mv1) != ARRAY_TYPE)
581                   mv1 = TYPE_MAIN_VARIANT (mv1);
582                 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
583                      memb; memb = DECL_CHAIN (memb))
584                   {
585                     tree mv3 = TREE_TYPE (memb);
586                     if (mv3 && mv3 != error_mark_node
587                         && TREE_CODE (mv3) != ARRAY_TYPE)
588                       mv3 = TYPE_MAIN_VARIANT (mv3);
589                     if (comptypes (mv3, mv1))
590                       {
591                         TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
592                                                          TREE_VALUE (p1));
593                         pedwarn (input_location, OPT_pedantic,
594                                  "function types not truly compatible in ISO C");
595                         goto parm_done;
596                       }
597                   }
598               }
599             TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
600           parm_done: ;
601           }
602
603         c_override_global_bindings_to_false = false;
604         t1 = build_function_type (valtype, newargs);
605         t1 = qualify_type (t1, t2);
606         /* ... falls through ...  */
607       }
608
609     default:
610       return build_type_attribute_variant (t1, attributes);
611     }
612
613 }
614
615 /* Return the type of a conditional expression between pointers to
616    possibly differently qualified versions of compatible types.
617
618    We assume that comp_target_types has already been done and returned
619    nonzero; if that isn't so, this may crash.  */
620
621 static tree
622 common_pointer_type (tree t1, tree t2)
623 {
624   tree attributes;
625   tree pointed_to_1, mv1;
626   tree pointed_to_2, mv2;
627   tree target;
628   unsigned target_quals;
629   addr_space_t as1, as2, as_common;
630   int quals1, quals2;
631
632   /* Save time if the two types are the same.  */
633
634   if (t1 == t2) return t1;
635
636   /* If one type is nonsense, use the other.  */
637   if (t1 == error_mark_node)
638     return t2;
639   if (t2 == error_mark_node)
640     return t1;
641
642   gcc_assert (TREE_CODE (t1) == POINTER_TYPE
643               && TREE_CODE (t2) == POINTER_TYPE);
644
645   /* Merge the attributes.  */
646   attributes = targetm.merge_type_attributes (t1, t2);
647
648   /* Find the composite type of the target types, and combine the
649      qualifiers of the two types' targets.  Do not lose qualifiers on
650      array element types by taking the TYPE_MAIN_VARIANT.  */
651   mv1 = pointed_to_1 = TREE_TYPE (t1);
652   mv2 = pointed_to_2 = TREE_TYPE (t2);
653   if (TREE_CODE (mv1) != ARRAY_TYPE)
654     mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
655   if (TREE_CODE (mv2) != ARRAY_TYPE)
656     mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
657   target = composite_type (mv1, mv2);
658
659   /* For function types do not merge const qualifiers, but drop them
660      if used inconsistently.  The middle-end uses these to mark const
661      and noreturn functions.  */
662   quals1 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_1);
663   quals2 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_2);
664
665   if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
666     target_quals = (quals1 & quals2);
667   else
668     target_quals = (quals1 | quals2);
669
670   /* If the two named address spaces are different, determine the common
671      superset address space.  This is guaranteed to exist due to the
672      assumption that comp_target_type returned non-zero.  */
673   as1 = TYPE_ADDR_SPACE (pointed_to_1);
674   as2 = TYPE_ADDR_SPACE (pointed_to_2);
675   if (!addr_space_superset (as1, as2, &as_common))
676     gcc_unreachable ();
677
678   target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
679
680   t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
681   return build_type_attribute_variant (t1, attributes);
682 }
683
684 /* Return the common type for two arithmetic types under the usual
685    arithmetic conversions.  The default conversions have already been
686    applied, and enumerated types converted to their compatible integer
687    types.  The resulting type is unqualified and has no attributes.
688
689    This is the type for the result of most arithmetic operations
690    if the operands have the given two types.  */
691
692 static tree
693 c_common_type (tree t1, tree t2)
694 {
695   enum tree_code code1;
696   enum tree_code code2;
697
698   /* If one type is nonsense, use the other.  */
699   if (t1 == error_mark_node)
700     return t2;
701   if (t2 == error_mark_node)
702     return t1;
703
704   if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
705     t1 = TYPE_MAIN_VARIANT (t1);
706
707   if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
708     t2 = TYPE_MAIN_VARIANT (t2);
709
710   if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
711     t1 = build_type_attribute_variant (t1, NULL_TREE);
712
713   if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
714     t2 = build_type_attribute_variant (t2, NULL_TREE);
715
716   /* Save time if the two types are the same.  */
717
718   if (t1 == t2) return t1;
719
720   code1 = TREE_CODE (t1);
721   code2 = TREE_CODE (t2);
722
723   gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
724               || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
725               || code1 == INTEGER_TYPE);
726   gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
727               || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
728               || code2 == INTEGER_TYPE);
729
730   /* When one operand is a decimal float type, the other operand cannot be
731      a generic float type or a complex type.  We also disallow vector types
732      here.  */
733   if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
734       && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
735     {
736       if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
737         {
738           error ("can%'t mix operands of decimal float and vector types");
739           return error_mark_node;
740         }
741       if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
742         {
743           error ("can%'t mix operands of decimal float and complex types");
744           return error_mark_node;
745         }
746       if (code1 == REAL_TYPE && code2 == REAL_TYPE)
747         {
748           error ("can%'t mix operands of decimal float and other float types");
749           return error_mark_node;
750         }
751     }
752
753   /* If one type is a vector type, return that type.  (How the usual
754      arithmetic conversions apply to the vector types extension is not
755      precisely specified.)  */
756   if (code1 == VECTOR_TYPE)
757     return t1;
758
759   if (code2 == VECTOR_TYPE)
760     return t2;
761
762   /* If one type is complex, form the common type of the non-complex
763      components, then make that complex.  Use T1 or T2 if it is the
764      required type.  */
765   if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
766     {
767       tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
768       tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
769       tree subtype = c_common_type (subtype1, subtype2);
770
771       if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
772         return t1;
773       else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
774         return t2;
775       else
776         return build_complex_type (subtype);
777     }
778
779   /* If only one is real, use it as the result.  */
780
781   if (code1 == REAL_TYPE && code2 != REAL_TYPE)
782     return t1;
783
784   if (code2 == REAL_TYPE && code1 != REAL_TYPE)
785     return t2;
786
787   /* If both are real and either are decimal floating point types, use
788      the decimal floating point type with the greater precision. */
789
790   if (code1 == REAL_TYPE && code2 == REAL_TYPE)
791     {
792       if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
793           || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
794         return dfloat128_type_node;
795       else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
796                || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
797         return dfloat64_type_node;
798       else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
799                || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
800         return dfloat32_type_node;
801     }
802
803   /* Deal with fixed-point types.  */
804   if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
805     {
806       unsigned int unsignedp = 0, satp = 0;
807       enum machine_mode m1, m2;
808       unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
809
810       m1 = TYPE_MODE (t1);
811       m2 = TYPE_MODE (t2);
812
813       /* If one input type is saturating, the result type is saturating.  */
814       if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
815         satp = 1;
816
817       /* If both fixed-point types are unsigned, the result type is unsigned.
818          When mixing fixed-point and integer types, follow the sign of the
819          fixed-point type.
820          Otherwise, the result type is signed.  */
821       if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
822            && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
823           || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
824               && TYPE_UNSIGNED (t1))
825           || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
826               && TYPE_UNSIGNED (t2)))
827         unsignedp = 1;
828
829       /* The result type is signed.  */
830       if (unsignedp == 0)
831         {
832           /* If the input type is unsigned, we need to convert to the
833              signed type.  */
834           if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
835             {
836               enum mode_class mclass = (enum mode_class) 0;
837               if (GET_MODE_CLASS (m1) == MODE_UFRACT)
838                 mclass = MODE_FRACT;
839               else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
840                 mclass = MODE_ACCUM;
841               else
842                 gcc_unreachable ();
843               m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
844             }
845           if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
846             {
847               enum mode_class mclass = (enum mode_class) 0;
848               if (GET_MODE_CLASS (m2) == MODE_UFRACT)
849                 mclass = MODE_FRACT;
850               else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
851                 mclass = MODE_ACCUM;
852               else
853                 gcc_unreachable ();
854               m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
855             }
856         }
857
858       if (code1 == FIXED_POINT_TYPE)
859         {
860           fbit1 = GET_MODE_FBIT (m1);
861           ibit1 = GET_MODE_IBIT (m1);
862         }
863       else
864         {
865           fbit1 = 0;
866           /* Signed integers need to subtract one sign bit.  */
867           ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
868         }
869
870       if (code2 == FIXED_POINT_TYPE)
871         {
872           fbit2 = GET_MODE_FBIT (m2);
873           ibit2 = GET_MODE_IBIT (m2);
874         }
875       else
876         {
877           fbit2 = 0;
878           /* Signed integers need to subtract one sign bit.  */
879           ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
880         }
881
882       max_ibit = ibit1 >= ibit2 ?  ibit1 : ibit2;
883       max_fbit = fbit1 >= fbit2 ?  fbit1 : fbit2;
884       return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
885                                                  satp);
886     }
887
888   /* Both real or both integers; use the one with greater precision.  */
889
890   if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
891     return t1;
892   else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
893     return t2;
894
895   /* Same precision.  Prefer long longs to longs to ints when the
896      same precision, following the C99 rules on integer type rank
897      (which are equivalent to the C90 rules for C90 types).  */
898
899   if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
900       || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
901     return long_long_unsigned_type_node;
902
903   if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
904       || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
905     {
906       if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
907         return long_long_unsigned_type_node;
908       else
909         return long_long_integer_type_node;
910     }
911
912   if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
913       || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
914     return long_unsigned_type_node;
915
916   if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
917       || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
918     {
919       /* But preserve unsignedness from the other type,
920          since long cannot hold all the values of an unsigned int.  */
921       if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
922         return long_unsigned_type_node;
923       else
924         return long_integer_type_node;
925     }
926
927   /* Likewise, prefer long double to double even if same size.  */
928   if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
929       || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
930     return long_double_type_node;
931
932   /* Otherwise prefer the unsigned one.  */
933
934   if (TYPE_UNSIGNED (t1))
935     return t1;
936   else
937     return t2;
938 }
939 \f
940 /* Wrapper around c_common_type that is used by c-common.c and other
941    front end optimizations that remove promotions.  ENUMERAL_TYPEs
942    are allowed here and are converted to their compatible integer types.
943    BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
944    preferably a non-Boolean type as the common type.  */
945 tree
946 common_type (tree t1, tree t2)
947 {
948   if (TREE_CODE (t1) == ENUMERAL_TYPE)
949     t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
950   if (TREE_CODE (t2) == ENUMERAL_TYPE)
951     t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
952
953   /* If both types are BOOLEAN_TYPE, then return boolean_type_node.  */
954   if (TREE_CODE (t1) == BOOLEAN_TYPE
955       && TREE_CODE (t2) == BOOLEAN_TYPE)
956     return boolean_type_node;
957
958   /* If either type is BOOLEAN_TYPE, then return the other.  */
959   if (TREE_CODE (t1) == BOOLEAN_TYPE)
960     return t2;
961   if (TREE_CODE (t2) == BOOLEAN_TYPE)
962     return t1;
963
964   return c_common_type (t1, t2);
965 }
966
967 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
968    or various other operations.  Return 2 if they are compatible
969    but a warning may be needed if you use them together.  */
970
971 int
972 comptypes (tree type1, tree type2)
973 {
974   const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
975   int val;
976
977   val = comptypes_internal (type1, type2, NULL, NULL);
978   free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
979
980   return val;
981 }
982
983 /* Like comptypes, but if it returns non-zero because enum and int are
984    compatible, it sets *ENUM_AND_INT_P to true.  */
985
986 static int
987 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
988 {
989   const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
990   int val;
991
992   val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
993   free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
994
995   return val;
996 }
997
998 /* Like comptypes, but if it returns nonzero for different types, it
999    sets *DIFFERENT_TYPES_P to true.  */
1000
1001 int
1002 comptypes_check_different_types (tree type1, tree type2,
1003                                  bool *different_types_p)
1004 {
1005   const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1006   int val;
1007
1008   val = comptypes_internal (type1, type2, NULL, different_types_p);
1009   free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1010
1011   return val;
1012 }
1013 \f
1014 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1015    or various other operations.  Return 2 if they are compatible
1016    but a warning may be needed if you use them together.  If
1017    ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1018    compatible integer type, then this sets *ENUM_AND_INT_P to true;
1019    *ENUM_AND_INT_P is never set to false.  If DIFFERENT_TYPES_P is not
1020    NULL, and the types are compatible but different enough not to be
1021    permitted in C1X typedef redeclarations, then this sets
1022    *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1023    false, but may or may not be set if the types are incompatible.
1024    This differs from comptypes, in that we don't free the seen
1025    types.  */
1026
1027 static int
1028 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1029                     bool *different_types_p)
1030 {
1031   const_tree t1 = type1;
1032   const_tree t2 = type2;
1033   int attrval, val;
1034
1035   /* Suppress errors caused by previously reported errors.  */
1036
1037   if (t1 == t2 || !t1 || !t2
1038       || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1039     return 1;
1040
1041   /* If either type is the internal version of sizetype, return the
1042      language version.  */
1043   if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
1044       && TYPE_ORIG_SIZE_TYPE (t1))
1045     t1 = TYPE_ORIG_SIZE_TYPE (t1);
1046
1047   if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
1048       && TYPE_ORIG_SIZE_TYPE (t2))
1049     t2 = TYPE_ORIG_SIZE_TYPE (t2);
1050
1051
1052   /* Enumerated types are compatible with integer types, but this is
1053      not transitive: two enumerated types in the same translation unit
1054      are compatible with each other only if they are the same type.  */
1055
1056   if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1057     {
1058       t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1059       if (TREE_CODE (t2) != VOID_TYPE)
1060         {
1061           if (enum_and_int_p != NULL)
1062             *enum_and_int_p = true;
1063           if (different_types_p != NULL)
1064             *different_types_p = true;
1065         }
1066     }
1067   else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1068     {
1069       t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1070       if (TREE_CODE (t1) != VOID_TYPE)
1071         {
1072           if (enum_and_int_p != NULL)
1073             *enum_and_int_p = true;
1074           if (different_types_p != NULL)
1075             *different_types_p = true;
1076         }
1077     }
1078
1079   if (t1 == t2)
1080     return 1;
1081
1082   /* Different classes of types can't be compatible.  */
1083
1084   if (TREE_CODE (t1) != TREE_CODE (t2))
1085     return 0;
1086
1087   /* Qualifiers must match. C99 6.7.3p9 */
1088
1089   if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1090     return 0;
1091
1092   /* Allow for two different type nodes which have essentially the same
1093      definition.  Note that we already checked for equality of the type
1094      qualifiers (just above).  */
1095
1096   if (TREE_CODE (t1) != ARRAY_TYPE
1097       && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1098     return 1;
1099
1100   /* 1 if no need for warning yet, 2 if warning cause has been seen.  */
1101   if (!(attrval = targetm.comp_type_attributes (t1, t2)))
1102      return 0;
1103
1104   /* 1 if no need for warning yet, 2 if warning cause has been seen.  */
1105   val = 0;
1106
1107   switch (TREE_CODE (t1))
1108     {
1109     case POINTER_TYPE:
1110       /* Do not remove mode or aliasing information.  */
1111       if (TYPE_MODE (t1) != TYPE_MODE (t2)
1112           || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1113         break;
1114       val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1115              ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1116                                        enum_and_int_p, different_types_p));
1117       break;
1118
1119     case FUNCTION_TYPE:
1120       val = function_types_compatible_p (t1, t2, enum_and_int_p,
1121                                          different_types_p);
1122       break;
1123
1124     case ARRAY_TYPE:
1125       {
1126         tree d1 = TYPE_DOMAIN (t1);
1127         tree d2 = TYPE_DOMAIN (t2);
1128         bool d1_variable, d2_variable;
1129         bool d1_zero, d2_zero;
1130         val = 1;
1131
1132         /* Target types must match incl. qualifiers.  */
1133         if (TREE_TYPE (t1) != TREE_TYPE (t2)
1134             && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1135                                                enum_and_int_p,
1136                                                different_types_p)))
1137           return 0;
1138
1139         if (different_types_p != NULL
1140             && (d1 == 0) != (d2 == 0))
1141           *different_types_p = true;
1142         /* Sizes must match unless one is missing or variable.  */
1143         if (d1 == 0 || d2 == 0 || d1 == d2)
1144           break;
1145
1146         d1_zero = !TYPE_MAX_VALUE (d1);
1147         d2_zero = !TYPE_MAX_VALUE (d2);
1148
1149         d1_variable = (!d1_zero
1150                        && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1151                            || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1152         d2_variable = (!d2_zero
1153                        && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1154                            || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1155         d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1156         d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1157
1158         if (different_types_p != NULL
1159             && d1_variable != d2_variable)
1160           *different_types_p = true;
1161         if (d1_variable || d2_variable)
1162           break;
1163         if (d1_zero && d2_zero)
1164           break;
1165         if (d1_zero || d2_zero
1166             || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1167             || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1168           val = 0;
1169
1170         break;
1171       }
1172
1173     case ENUMERAL_TYPE:
1174     case RECORD_TYPE:
1175     case UNION_TYPE:
1176       if (val != 1 && !same_translation_unit_p (t1, t2))
1177         {
1178           tree a1 = TYPE_ATTRIBUTES (t1);
1179           tree a2 = TYPE_ATTRIBUTES (t2);
1180
1181           if (! attribute_list_contained (a1, a2)
1182               && ! attribute_list_contained (a2, a1))
1183             break;
1184
1185           if (attrval != 2)
1186             return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1187                                                  different_types_p);
1188           val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1189                                               different_types_p);
1190         }
1191       break;
1192
1193     case VECTOR_TYPE:
1194       val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1195              && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1196                                     enum_and_int_p, different_types_p));
1197       break;
1198
1199     default:
1200       break;
1201     }
1202   return attrval == 2 && val == 1 ? 2 : val;
1203 }
1204
1205 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1206    their qualifiers, except for named address spaces.  If the pointers point to
1207    different named addresses, then we must determine if one address space is a
1208    subset of the other.  */
1209
1210 static int
1211 comp_target_types (location_t location, tree ttl, tree ttr)
1212 {
1213   int val;
1214   tree mvl = TREE_TYPE (ttl);
1215   tree mvr = TREE_TYPE (ttr);
1216   addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1217   addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1218   addr_space_t as_common;
1219   bool enum_and_int_p;
1220
1221   /* Fail if pointers point to incompatible address spaces.  */
1222   if (!addr_space_superset (asl, asr, &as_common))
1223     return 0;
1224
1225   /* Do not lose qualifiers on element types of array types that are
1226      pointer targets by taking their TYPE_MAIN_VARIANT.  */
1227   if (TREE_CODE (mvl) != ARRAY_TYPE)
1228     mvl = TYPE_MAIN_VARIANT (mvl);
1229   if (TREE_CODE (mvr) != ARRAY_TYPE)
1230     mvr = TYPE_MAIN_VARIANT (mvr);
1231   enum_and_int_p = false;
1232   val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1233
1234   if (val == 2)
1235     pedwarn (location, OPT_pedantic, "types are not quite compatible");
1236
1237   if (val == 1 && enum_and_int_p && warn_cxx_compat)
1238     warning_at (location, OPT_Wc___compat,
1239                 "pointer target types incompatible in C++");
1240
1241   return val;
1242 }
1243 \f
1244 /* Subroutines of `comptypes'.  */
1245
1246 /* Determine whether two trees derive from the same translation unit.
1247    If the CONTEXT chain ends in a null, that tree's context is still
1248    being parsed, so if two trees have context chains ending in null,
1249    they're in the same translation unit.  */
1250 int
1251 same_translation_unit_p (const_tree t1, const_tree t2)
1252 {
1253   while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1254     switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1255       {
1256       case tcc_declaration:
1257         t1 = DECL_CONTEXT (t1); break;
1258       case tcc_type:
1259         t1 = TYPE_CONTEXT (t1); break;
1260       case tcc_exceptional:
1261         t1 = BLOCK_SUPERCONTEXT (t1); break;  /* assume block */
1262       default: gcc_unreachable ();
1263       }
1264
1265   while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1266     switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1267       {
1268       case tcc_declaration:
1269         t2 = DECL_CONTEXT (t2); break;
1270       case tcc_type:
1271         t2 = TYPE_CONTEXT (t2); break;
1272       case tcc_exceptional:
1273         t2 = BLOCK_SUPERCONTEXT (t2); break;  /* assume block */
1274       default: gcc_unreachable ();
1275       }
1276
1277   return t1 == t2;
1278 }
1279
1280 /* Allocate the seen two types, assuming that they are compatible. */
1281
1282 static struct tagged_tu_seen_cache *
1283 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1284 {
1285   struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1286   tu->next = tagged_tu_seen_base;
1287   tu->t1 = t1;
1288   tu->t2 = t2;
1289
1290   tagged_tu_seen_base = tu;
1291
1292   /* The C standard says that two structures in different translation
1293      units are compatible with each other only if the types of their
1294      fields are compatible (among other things).  We assume that they
1295      are compatible until proven otherwise when building the cache.
1296      An example where this can occur is:
1297      struct a
1298      {
1299        struct a *next;
1300      };
1301      If we are comparing this against a similar struct in another TU,
1302      and did not assume they were compatible, we end up with an infinite
1303      loop.  */
1304   tu->val = 1;
1305   return tu;
1306 }
1307
1308 /* Free the seen types until we get to TU_TIL. */
1309
1310 static void
1311 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1312 {
1313   const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1314   while (tu != tu_til)
1315     {
1316       const struct tagged_tu_seen_cache *const tu1
1317         = (const struct tagged_tu_seen_cache *) tu;
1318       tu = tu1->next;
1319       free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1320     }
1321   tagged_tu_seen_base = tu_til;
1322 }
1323
1324 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1325    compatible.  If the two types are not the same (which has been
1326    checked earlier), this can only happen when multiple translation
1327    units are being compiled.  See C99 6.2.7 paragraph 1 for the exact
1328    rules.  ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1329    comptypes_internal.  */
1330
1331 static int
1332 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1333                               bool *enum_and_int_p, bool *different_types_p)
1334 {
1335   tree s1, s2;
1336   bool needs_warning = false;
1337
1338   /* We have to verify that the tags of the types are the same.  This
1339      is harder than it looks because this may be a typedef, so we have
1340      to go look at the original type.  It may even be a typedef of a
1341      typedef...
1342      In the case of compiler-created builtin structs the TYPE_DECL
1343      may be a dummy, with no DECL_ORIGINAL_TYPE.  Don't fault.  */
1344   while (TYPE_NAME (t1)
1345          && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1346          && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1347     t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1348
1349   while (TYPE_NAME (t2)
1350          && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1351          && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1352     t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1353
1354   /* C90 didn't have the requirement that the two tags be the same.  */
1355   if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1356     return 0;
1357
1358   /* C90 didn't say what happened if one or both of the types were
1359      incomplete; we choose to follow C99 rules here, which is that they
1360      are compatible.  */
1361   if (TYPE_SIZE (t1) == NULL
1362       || TYPE_SIZE (t2) == NULL)
1363     return 1;
1364
1365   {
1366     const struct tagged_tu_seen_cache * tts_i;
1367     for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1368       if (tts_i->t1 == t1 && tts_i->t2 == t2)
1369         return tts_i->val;
1370   }
1371
1372   switch (TREE_CODE (t1))
1373     {
1374     case ENUMERAL_TYPE:
1375       {
1376         struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1377         /* Speed up the case where the type values are in the same order.  */
1378         tree tv1 = TYPE_VALUES (t1);
1379         tree tv2 = TYPE_VALUES (t2);
1380
1381         if (tv1 == tv2)
1382           {
1383             return 1;
1384           }
1385
1386         for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1387           {
1388             if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1389               break;
1390             if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1391               {
1392                 tu->val = 0;
1393                 return 0;
1394               }
1395           }
1396
1397         if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1398           {
1399             return 1;
1400           }
1401         if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1402           {
1403             tu->val = 0;
1404             return 0;
1405           }
1406
1407         if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1408           {
1409             tu->val = 0;
1410             return 0;
1411           }
1412
1413         for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1414           {
1415             s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1416             if (s2 == NULL
1417                 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1418               {
1419                 tu->val = 0;
1420                 return 0;
1421               }
1422           }
1423         return 1;
1424       }
1425
1426     case UNION_TYPE:
1427       {
1428         struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1429         if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1430           {
1431             tu->val = 0;
1432             return 0;
1433           }
1434
1435         /*  Speed up the common case where the fields are in the same order. */
1436         for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1437              s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1438           {
1439             int result;
1440
1441             if (DECL_NAME (s1) != DECL_NAME (s2))
1442               break;
1443             result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1444                                          enum_and_int_p, different_types_p);
1445
1446             if (result != 1 && !DECL_NAME (s1))
1447               break;
1448             if (result == 0)
1449               {
1450                 tu->val = 0;
1451                 return 0;
1452               }
1453             if (result == 2)
1454               needs_warning = true;
1455
1456             if (TREE_CODE (s1) == FIELD_DECL
1457                 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1458                                      DECL_FIELD_BIT_OFFSET (s2)) != 1)
1459               {
1460                 tu->val = 0;
1461                 return 0;
1462               }
1463           }
1464         if (!s1 && !s2)
1465           {
1466             tu->val = needs_warning ? 2 : 1;
1467             return tu->val;
1468           }
1469
1470         for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1471           {
1472             bool ok = false;
1473
1474             for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1475               if (DECL_NAME (s1) == DECL_NAME (s2))
1476                 {
1477                   int result;
1478
1479                   result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1480                                                enum_and_int_p,
1481                                                different_types_p);
1482
1483                   if (result != 1 && !DECL_NAME (s1))
1484                     continue;
1485                   if (result == 0)
1486                     {
1487                       tu->val = 0;
1488                       return 0;
1489                     }
1490                   if (result == 2)
1491                     needs_warning = true;
1492
1493                   if (TREE_CODE (s1) == FIELD_DECL
1494                       && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1495                                            DECL_FIELD_BIT_OFFSET (s2)) != 1)
1496                     break;
1497
1498                   ok = true;
1499                   break;
1500                 }
1501             if (!ok)
1502               {
1503                 tu->val = 0;
1504                 return 0;
1505               }
1506           }
1507         tu->val = needs_warning ? 2 : 10;
1508         return tu->val;
1509       }
1510
1511     case RECORD_TYPE:
1512       {
1513         struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1514
1515         for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1516              s1 && s2;
1517              s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1518           {
1519             int result;
1520             if (TREE_CODE (s1) != TREE_CODE (s2)
1521                 || DECL_NAME (s1) != DECL_NAME (s2))
1522               break;
1523             result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1524                                          enum_and_int_p, different_types_p);
1525             if (result == 0)
1526               break;
1527             if (result == 2)
1528               needs_warning = true;
1529
1530             if (TREE_CODE (s1) == FIELD_DECL
1531                 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1532                                      DECL_FIELD_BIT_OFFSET (s2)) != 1)
1533               break;
1534           }
1535         if (s1 && s2)
1536           tu->val = 0;
1537         else
1538           tu->val = needs_warning ? 2 : 1;
1539         return tu->val;
1540       }
1541
1542     default:
1543       gcc_unreachable ();
1544     }
1545 }
1546
1547 /* Return 1 if two function types F1 and F2 are compatible.
1548    If either type specifies no argument types,
1549    the other must specify a fixed number of self-promoting arg types.
1550    Otherwise, if one type specifies only the number of arguments,
1551    the other must specify that number of self-promoting arg types.
1552    Otherwise, the argument types must match.
1553    ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal.  */
1554
1555 static int
1556 function_types_compatible_p (const_tree f1, const_tree f2,
1557                              bool *enum_and_int_p, bool *different_types_p)
1558 {
1559   tree args1, args2;
1560   /* 1 if no need for warning yet, 2 if warning cause has been seen.  */
1561   int val = 1;
1562   int val1;
1563   tree ret1, ret2;
1564
1565   ret1 = TREE_TYPE (f1);
1566   ret2 = TREE_TYPE (f2);
1567
1568   /* 'volatile' qualifiers on a function's return type used to mean
1569      the function is noreturn.  */
1570   if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1571     pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1572   if (TYPE_VOLATILE (ret1))
1573     ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1574                                  TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1575   if (TYPE_VOLATILE (ret2))
1576     ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1577                                  TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1578   val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1579   if (val == 0)
1580     return 0;
1581
1582   args1 = TYPE_ARG_TYPES (f1);
1583   args2 = TYPE_ARG_TYPES (f2);
1584
1585   if (different_types_p != NULL
1586       && (args1 == 0) != (args2 == 0))
1587     *different_types_p = true;
1588
1589   /* An unspecified parmlist matches any specified parmlist
1590      whose argument types don't need default promotions.  */
1591
1592   if (args1 == 0)
1593     {
1594       if (!self_promoting_args_p (args2))
1595         return 0;
1596       /* If one of these types comes from a non-prototype fn definition,
1597          compare that with the other type's arglist.
1598          If they don't match, ask for a warning (but no error).  */
1599       if (TYPE_ACTUAL_ARG_TYPES (f1)
1600           && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1601                                            enum_and_int_p, different_types_p))
1602         val = 2;
1603       return val;
1604     }
1605   if (args2 == 0)
1606     {
1607       if (!self_promoting_args_p (args1))
1608         return 0;
1609       if (TYPE_ACTUAL_ARG_TYPES (f2)
1610           && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1611                                            enum_and_int_p, different_types_p))
1612         val = 2;
1613       return val;
1614     }
1615
1616   /* Both types have argument lists: compare them and propagate results.  */
1617   val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1618                                   different_types_p);
1619   return val1 != 1 ? val1 : val;
1620 }
1621
1622 /* Check two lists of types for compatibility, returning 0 for
1623    incompatible, 1 for compatible, or 2 for compatible with
1624    warning.  ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1625    comptypes_internal.  */
1626
1627 static int
1628 type_lists_compatible_p (const_tree args1, const_tree args2,
1629                          bool *enum_and_int_p, bool *different_types_p)
1630 {
1631   /* 1 if no need for warning yet, 2 if warning cause has been seen.  */
1632   int val = 1;
1633   int newval = 0;
1634
1635   while (1)
1636     {
1637       tree a1, mv1, a2, mv2;
1638       if (args1 == 0 && args2 == 0)
1639         return val;
1640       /* If one list is shorter than the other,
1641          they fail to match.  */
1642       if (args1 == 0 || args2 == 0)
1643         return 0;
1644       mv1 = a1 = TREE_VALUE (args1);
1645       mv2 = a2 = TREE_VALUE (args2);
1646       if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1647         mv1 = TYPE_MAIN_VARIANT (mv1);
1648       if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1649         mv2 = TYPE_MAIN_VARIANT (mv2);
1650       /* A null pointer instead of a type
1651          means there is supposed to be an argument
1652          but nothing is specified about what type it has.
1653          So match anything that self-promotes.  */
1654       if (different_types_p != NULL
1655           && (a1 == 0) != (a2 == 0))
1656         *different_types_p = true;
1657       if (a1 == 0)
1658         {
1659           if (c_type_promotes_to (a2) != a2)
1660             return 0;
1661         }
1662       else if (a2 == 0)
1663         {
1664           if (c_type_promotes_to (a1) != a1)
1665             return 0;
1666         }
1667       /* If one of the lists has an error marker, ignore this arg.  */
1668       else if (TREE_CODE (a1) == ERROR_MARK
1669                || TREE_CODE (a2) == ERROR_MARK)
1670         ;
1671       else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1672                                               different_types_p)))
1673         {
1674           if (different_types_p != NULL)
1675             *different_types_p = true;
1676           /* Allow  wait (union {union wait *u; int *i} *)
1677              and  wait (union wait *)  to be compatible.  */
1678           if (TREE_CODE (a1) == UNION_TYPE
1679               && (TYPE_NAME (a1) == 0
1680                   || TYPE_TRANSPARENT_AGGR (a1))
1681               && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1682               && tree_int_cst_equal (TYPE_SIZE (a1),
1683                                      TYPE_SIZE (a2)))
1684             {
1685               tree memb;
1686               for (memb = TYPE_FIELDS (a1);
1687                    memb; memb = DECL_CHAIN (memb))
1688                 {
1689                   tree mv3 = TREE_TYPE (memb);
1690                   if (mv3 && mv3 != error_mark_node
1691                       && TREE_CODE (mv3) != ARRAY_TYPE)
1692                     mv3 = TYPE_MAIN_VARIANT (mv3);
1693                   if (comptypes_internal (mv3, mv2, enum_and_int_p,
1694                                           different_types_p))
1695                     break;
1696                 }
1697               if (memb == 0)
1698                 return 0;
1699             }
1700           else if (TREE_CODE (a2) == UNION_TYPE
1701                    && (TYPE_NAME (a2) == 0
1702                        || TYPE_TRANSPARENT_AGGR (a2))
1703                    && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1704                    && tree_int_cst_equal (TYPE_SIZE (a2),
1705                                           TYPE_SIZE (a1)))
1706             {
1707               tree memb;
1708               for (memb = TYPE_FIELDS (a2);
1709                    memb; memb = DECL_CHAIN (memb))
1710                 {
1711                   tree mv3 = TREE_TYPE (memb);
1712                   if (mv3 && mv3 != error_mark_node
1713                       && TREE_CODE (mv3) != ARRAY_TYPE)
1714                     mv3 = TYPE_MAIN_VARIANT (mv3);
1715                   if (comptypes_internal (mv3, mv1, enum_and_int_p,
1716                                           different_types_p))
1717                     break;
1718                 }
1719               if (memb == 0)
1720                 return 0;
1721             }
1722           else
1723             return 0;
1724         }
1725
1726       /* comptypes said ok, but record if it said to warn.  */
1727       if (newval > val)
1728         val = newval;
1729
1730       args1 = TREE_CHAIN (args1);
1731       args2 = TREE_CHAIN (args2);
1732     }
1733 }
1734 \f
1735 /* Compute the size to increment a pointer by.  */
1736
1737 static tree
1738 c_size_in_bytes (const_tree type)
1739 {
1740   enum tree_code code = TREE_CODE (type);
1741
1742   if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1743     return size_one_node;
1744
1745   if (!COMPLETE_OR_VOID_TYPE_P (type))
1746     {
1747       error ("arithmetic on pointer to an incomplete type");
1748       return size_one_node;
1749     }
1750
1751   /* Convert in case a char is more than one unit.  */
1752   return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1753                          size_int (TYPE_PRECISION (char_type_node)
1754                                    / BITS_PER_UNIT));
1755 }
1756 \f
1757 /* Return either DECL or its known constant value (if it has one).  */
1758
1759 tree
1760 decl_constant_value (tree decl)
1761 {
1762   if (/* Don't change a variable array bound or initial value to a constant
1763          in a place where a variable is invalid.  Note that DECL_INITIAL
1764          isn't valid for a PARM_DECL.  */
1765       current_function_decl != 0
1766       && TREE_CODE (decl) != PARM_DECL
1767       && !TREE_THIS_VOLATILE (decl)
1768       && TREE_READONLY (decl)
1769       && DECL_INITIAL (decl) != 0
1770       && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1771       /* This is invalid if initial value is not constant.
1772          If it has either a function call, a memory reference,
1773          or a variable, then re-evaluating it could give different results.  */
1774       && TREE_CONSTANT (DECL_INITIAL (decl))
1775       /* Check for cases where this is sub-optimal, even though valid.  */
1776       && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1777     return DECL_INITIAL (decl);
1778   return decl;
1779 }
1780
1781 /* Convert the array expression EXP to a pointer.  */
1782 static tree
1783 array_to_pointer_conversion (location_t loc, tree exp)
1784 {
1785   tree orig_exp = exp;
1786   tree type = TREE_TYPE (exp);
1787   tree adr;
1788   tree restype = TREE_TYPE (type);
1789   tree ptrtype;
1790
1791   gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1792
1793   STRIP_TYPE_NOPS (exp);
1794
1795   if (TREE_NO_WARNING (orig_exp))
1796     TREE_NO_WARNING (exp) = 1;
1797
1798   ptrtype = build_pointer_type (restype);
1799
1800   if (TREE_CODE (exp) == INDIRECT_REF)
1801     return convert (ptrtype, TREE_OPERAND (exp, 0));
1802
1803   adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
1804   return convert (ptrtype, adr);
1805 }
1806
1807 /* Convert the function expression EXP to a pointer.  */
1808 static tree
1809 function_to_pointer_conversion (location_t loc, tree exp)
1810 {
1811   tree orig_exp = exp;
1812
1813   gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1814
1815   STRIP_TYPE_NOPS (exp);
1816
1817   if (TREE_NO_WARNING (orig_exp))
1818     TREE_NO_WARNING (exp) = 1;
1819
1820   return build_unary_op (loc, ADDR_EXPR, exp, 0);
1821 }
1822
1823 /* Mark EXP as read, not just set, for set but not used -Wunused
1824    warning purposes.  */
1825
1826 void
1827 mark_exp_read (tree exp)
1828 {
1829   switch (TREE_CODE (exp))
1830     {
1831     case VAR_DECL:
1832     case PARM_DECL:
1833       DECL_READ_P (exp) = 1;
1834       break;
1835     case ARRAY_REF:
1836     case COMPONENT_REF:
1837     case MODIFY_EXPR:
1838     case REALPART_EXPR:
1839     case IMAGPART_EXPR:
1840     CASE_CONVERT:
1841     case ADDR_EXPR:
1842       mark_exp_read (TREE_OPERAND (exp, 0));
1843       break;
1844     case COMPOUND_EXPR:
1845     case C_MAYBE_CONST_EXPR:
1846       mark_exp_read (TREE_OPERAND (exp, 1));
1847       break;
1848     default:
1849       break;
1850     }
1851 }
1852
1853 /* Perform the default conversion of arrays and functions to pointers.
1854    Return the result of converting EXP.  For any other expression, just
1855    return EXP.
1856
1857    LOC is the location of the expression.  */
1858
1859 struct c_expr
1860 default_function_array_conversion (location_t loc, struct c_expr exp)
1861 {
1862   tree orig_exp = exp.value;
1863   tree type = TREE_TYPE (exp.value);
1864   enum tree_code code = TREE_CODE (type);
1865
1866   switch (code)
1867     {
1868     case ARRAY_TYPE:
1869       {
1870         bool not_lvalue = false;
1871         bool lvalue_array_p;
1872
1873         while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1874                 || CONVERT_EXPR_P (exp.value))
1875                && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1876           {
1877             if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1878               not_lvalue = true;
1879             exp.value = TREE_OPERAND (exp.value, 0);
1880           }
1881
1882         if (TREE_NO_WARNING (orig_exp))
1883           TREE_NO_WARNING (exp.value) = 1;
1884
1885         lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1886         if (!flag_isoc99 && !lvalue_array_p)
1887           {
1888             /* Before C99, non-lvalue arrays do not decay to pointers.
1889                Normally, using such an array would be invalid; but it can
1890                be used correctly inside sizeof or as a statement expression.
1891                Thus, do not give an error here; an error will result later.  */
1892             return exp;
1893           }
1894
1895         exp.value = array_to_pointer_conversion (loc, exp.value);
1896       }
1897       break;
1898     case FUNCTION_TYPE:
1899       exp.value = function_to_pointer_conversion (loc, exp.value);
1900       break;
1901     default:
1902       break;
1903     }
1904
1905   return exp;
1906 }
1907
1908 struct c_expr
1909 default_function_array_read_conversion (location_t loc, struct c_expr exp)
1910 {
1911   mark_exp_read (exp.value);
1912   return default_function_array_conversion (loc, exp);
1913 }
1914
1915 /* EXP is an expression of integer type.  Apply the integer promotions
1916    to it and return the promoted value.  */
1917
1918 tree
1919 perform_integral_promotions (tree exp)
1920 {
1921   tree type = TREE_TYPE (exp);
1922   enum tree_code code = TREE_CODE (type);
1923
1924   gcc_assert (INTEGRAL_TYPE_P (type));
1925
1926   /* Normally convert enums to int,
1927      but convert wide enums to something wider.  */
1928   if (code == ENUMERAL_TYPE)
1929     {
1930       type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1931                                           TYPE_PRECISION (integer_type_node)),
1932                                      ((TYPE_PRECISION (type)
1933                                        >= TYPE_PRECISION (integer_type_node))
1934                                       && TYPE_UNSIGNED (type)));
1935
1936       return convert (type, exp);
1937     }
1938
1939   /* ??? This should no longer be needed now bit-fields have their
1940      proper types.  */
1941   if (TREE_CODE (exp) == COMPONENT_REF
1942       && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1943       /* If it's thinner than an int, promote it like a
1944          c_promoting_integer_type_p, otherwise leave it alone.  */
1945       && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1946                                TYPE_PRECISION (integer_type_node)))
1947     return convert (integer_type_node, exp);
1948
1949   if (c_promoting_integer_type_p (type))
1950     {
1951       /* Preserve unsignedness if not really getting any wider.  */
1952       if (TYPE_UNSIGNED (type)
1953           && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1954         return convert (unsigned_type_node, exp);
1955
1956       return convert (integer_type_node, exp);
1957     }
1958
1959   return exp;
1960 }
1961
1962
1963 /* Perform default promotions for C data used in expressions.
1964    Enumeral types or short or char are converted to int.
1965    In addition, manifest constants symbols are replaced by their values.  */
1966
1967 tree
1968 default_conversion (tree exp)
1969 {
1970   tree orig_exp;
1971   tree type = TREE_TYPE (exp);
1972   enum tree_code code = TREE_CODE (type);
1973   tree promoted_type;
1974
1975   mark_exp_read (exp);
1976
1977   /* Functions and arrays have been converted during parsing.  */
1978   gcc_assert (code != FUNCTION_TYPE);
1979   if (code == ARRAY_TYPE)
1980     return exp;
1981
1982   /* Constants can be used directly unless they're not loadable.  */
1983   if (TREE_CODE (exp) == CONST_DECL)
1984     exp = DECL_INITIAL (exp);
1985
1986   /* Strip no-op conversions.  */
1987   orig_exp = exp;
1988   STRIP_TYPE_NOPS (exp);
1989
1990   if (TREE_NO_WARNING (orig_exp))
1991     TREE_NO_WARNING (exp) = 1;
1992
1993   if (code == VOID_TYPE)
1994     {
1995       error ("void value not ignored as it ought to be");
1996       return error_mark_node;
1997     }
1998
1999   exp = require_complete_type (exp);
2000   if (exp == error_mark_node)
2001     return error_mark_node;
2002
2003   promoted_type = targetm.promoted_type (type);
2004   if (promoted_type)
2005     return convert (promoted_type, exp);
2006
2007   if (INTEGRAL_TYPE_P (type))
2008     return perform_integral_promotions (exp);
2009
2010   return exp;
2011 }
2012 \f
2013 /* Look up COMPONENT in a structure or union TYPE.
2014
2015    If the component name is not found, returns NULL_TREE.  Otherwise,
2016    the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2017    stepping down the chain to the component, which is in the last
2018    TREE_VALUE of the list.  Normally the list is of length one, but if
2019    the component is embedded within (nested) anonymous structures or
2020    unions, the list steps down the chain to the component.  */
2021
2022 static tree
2023 lookup_field (tree type, tree component)
2024 {
2025   tree field;
2026
2027   /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2028      to the field elements.  Use a binary search on this array to quickly
2029      find the element.  Otherwise, do a linear search.  TYPE_LANG_SPECIFIC
2030      will always be set for structures which have many elements.  */
2031
2032   if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2033     {
2034       int bot, top, half;
2035       tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2036
2037       field = TYPE_FIELDS (type);
2038       bot = 0;
2039       top = TYPE_LANG_SPECIFIC (type)->s->len;
2040       while (top - bot > 1)
2041         {
2042           half = (top - bot + 1) >> 1;
2043           field = field_array[bot+half];
2044
2045           if (DECL_NAME (field) == NULL_TREE)
2046             {
2047               /* Step through all anon unions in linear fashion.  */
2048               while (DECL_NAME (field_array[bot]) == NULL_TREE)
2049                 {
2050                   field = field_array[bot++];
2051                   if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2052                       || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
2053                     {
2054                       tree anon = lookup_field (TREE_TYPE (field), component);
2055
2056                       if (anon)
2057                         return tree_cons (NULL_TREE, field, anon);
2058                     }
2059                 }
2060
2061               /* Entire record is only anon unions.  */
2062               if (bot > top)
2063                 return NULL_TREE;
2064
2065               /* Restart the binary search, with new lower bound.  */
2066               continue;
2067             }
2068
2069           if (DECL_NAME (field) == component)
2070             break;
2071           if (DECL_NAME (field) < component)
2072             bot += half;
2073           else
2074             top = bot + half;
2075         }
2076
2077       if (DECL_NAME (field_array[bot]) == component)
2078         field = field_array[bot];
2079       else if (DECL_NAME (field) != component)
2080         return NULL_TREE;
2081     }
2082   else
2083     {
2084       for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2085         {
2086           if (DECL_NAME (field) == NULL_TREE
2087               && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2088                   || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2089             {
2090               tree anon = lookup_field (TREE_TYPE (field), component);
2091
2092               if (anon)
2093                 return tree_cons (NULL_TREE, field, anon);
2094             }
2095
2096           if (DECL_NAME (field) == component)
2097             break;
2098         }
2099
2100       if (field == NULL_TREE)
2101         return NULL_TREE;
2102     }
2103
2104   return tree_cons (NULL_TREE, field, NULL_TREE);
2105 }
2106
2107 /* Make an expression to refer to the COMPONENT field of structure or
2108    union value DATUM.  COMPONENT is an IDENTIFIER_NODE.  LOC is the
2109    location of the COMPONENT_REF.  */
2110
2111 tree
2112 build_component_ref (location_t loc, tree datum, tree component)
2113 {
2114   tree type = TREE_TYPE (datum);
2115   enum tree_code code = TREE_CODE (type);
2116   tree field = NULL;
2117   tree ref;
2118   bool datum_lvalue = lvalue_p (datum);
2119
2120   if (!objc_is_public (datum, component))
2121     return error_mark_node;
2122
2123   /* See if there is a field or component with name COMPONENT.  */
2124
2125   if (code == RECORD_TYPE || code == UNION_TYPE)
2126     {
2127       if (!COMPLETE_TYPE_P (type))
2128         {
2129           c_incomplete_type_error (NULL_TREE, type);
2130           return error_mark_node;
2131         }
2132
2133       field = lookup_field (type, component);
2134
2135       if (!field)
2136         {
2137           error_at (loc, "%qT has no member named %qE", type, component);
2138           return error_mark_node;
2139         }
2140
2141       /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2142          This might be better solved in future the way the C++ front
2143          end does it - by giving the anonymous entities each a
2144          separate name and type, and then have build_component_ref
2145          recursively call itself.  We can't do that here.  */
2146       do
2147         {
2148           tree subdatum = TREE_VALUE (field);
2149           int quals;
2150           tree subtype;
2151           bool use_datum_quals;
2152
2153           if (TREE_TYPE (subdatum) == error_mark_node)
2154             return error_mark_node;
2155
2156           /* If this is an rvalue, it does not have qualifiers in C
2157              standard terms and we must avoid propagating such
2158              qualifiers down to a non-lvalue array that is then
2159              converted to a pointer.  */
2160           use_datum_quals = (datum_lvalue
2161                              || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2162
2163           quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2164           if (use_datum_quals)
2165             quals |= TYPE_QUALS (TREE_TYPE (datum));
2166           subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2167
2168           ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2169                         NULL_TREE);
2170           SET_EXPR_LOCATION (ref, loc);
2171           if (TREE_READONLY (subdatum)
2172               || (use_datum_quals && TREE_READONLY (datum)))
2173             TREE_READONLY (ref) = 1;
2174           if (TREE_THIS_VOLATILE (subdatum)
2175               || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2176             TREE_THIS_VOLATILE (ref) = 1;
2177
2178           if (TREE_DEPRECATED (subdatum))
2179             warn_deprecated_use (subdatum, NULL_TREE);
2180
2181           datum = ref;
2182
2183           field = TREE_CHAIN (field);
2184         }
2185       while (field);
2186
2187       return ref;
2188     }
2189   else if (code != ERROR_MARK)
2190     error_at (loc,
2191               "request for member %qE in something not a structure or union",
2192               component);
2193
2194   return error_mark_node;
2195 }
2196 \f
2197 /* Given an expression PTR for a pointer, return an expression
2198    for the value pointed to.
2199    ERRORSTRING is the name of the operator to appear in error messages.
2200
2201    LOC is the location to use for the generated tree.  */
2202
2203 tree
2204 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2205 {
2206   tree pointer = default_conversion (ptr);
2207   tree type = TREE_TYPE (pointer);
2208   tree ref;
2209
2210   if (TREE_CODE (type) == POINTER_TYPE)
2211     {
2212       if (CONVERT_EXPR_P (pointer)
2213           || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2214         {
2215           /* If a warning is issued, mark it to avoid duplicates from
2216              the backend.  This only needs to be done at
2217              warn_strict_aliasing > 2.  */
2218           if (warn_strict_aliasing > 2)
2219             if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2220                                          type, TREE_OPERAND (pointer, 0)))
2221               TREE_NO_WARNING (pointer) = 1;
2222         }
2223
2224       if (TREE_CODE (pointer) == ADDR_EXPR
2225           && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2226               == TREE_TYPE (type)))
2227         {
2228           ref = TREE_OPERAND (pointer, 0);
2229           protected_set_expr_location (ref, loc);
2230           return ref;
2231         }
2232       else
2233         {
2234           tree t = TREE_TYPE (type);
2235
2236           ref = build1 (INDIRECT_REF, t, pointer);
2237
2238           if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2239             {
2240               error_at (loc, "dereferencing pointer to incomplete type");
2241               return error_mark_node;
2242             }
2243           if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2244             warning_at (loc, 0, "dereferencing %<void *%> pointer");
2245
2246           /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2247              so that we get the proper error message if the result is used
2248              to assign to.  Also, &* is supposed to be a no-op.
2249              And ANSI C seems to specify that the type of the result
2250              should be the const type.  */
2251           /* A de-reference of a pointer to const is not a const.  It is valid
2252              to change it via some other pointer.  */
2253           TREE_READONLY (ref) = TYPE_READONLY (t);
2254           TREE_SIDE_EFFECTS (ref)
2255             = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2256           TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2257           protected_set_expr_location (ref, loc);
2258           return ref;
2259         }
2260     }
2261   else if (TREE_CODE (pointer) != ERROR_MARK)
2262     switch (errstring)
2263       {
2264          case RO_ARRAY_INDEXING:
2265            error_at (loc,
2266                      "invalid type argument of array indexing (have %qT)",
2267                      type);
2268            break;
2269          case RO_UNARY_STAR:
2270            error_at (loc,
2271                      "invalid type argument of unary %<*%> (have %qT)",
2272                      type);
2273            break;
2274          case RO_ARROW:
2275            error_at (loc,
2276                      "invalid type argument of %<->%> (have %qT)",
2277                      type);
2278            break;
2279          default:
2280            gcc_unreachable ();
2281       }
2282   return error_mark_node;
2283 }
2284
2285 /* This handles expressions of the form "a[i]", which denotes
2286    an array reference.
2287
2288    This is logically equivalent in C to *(a+i), but we may do it differently.
2289    If A is a variable or a member, we generate a primitive ARRAY_REF.
2290    This avoids forcing the array out of registers, and can work on
2291    arrays that are not lvalues (for example, members of structures returned
2292    by functions).
2293
2294    LOC is the location to use for the returned expression.  */
2295
2296 tree
2297 build_array_ref (location_t loc, tree array, tree index)
2298 {
2299   tree ret;
2300   bool swapped = false;
2301   if (TREE_TYPE (array) == error_mark_node
2302       || TREE_TYPE (index) == error_mark_node)
2303     return error_mark_node;
2304
2305   if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2306       && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE)
2307     {
2308       tree temp;
2309       if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2310           && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2311         {
2312           error_at (loc, "subscripted value is neither array nor pointer");
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   if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2344     {
2345       tree rval, type;
2346
2347       /* An array that is indexed by a non-constant
2348          cannot be stored in a register; we must be able to do
2349          address arithmetic on its address.
2350          Likewise an array of elements of variable size.  */
2351       if (TREE_CODE (index) != INTEGER_CST
2352           || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2353               && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2354         {
2355           if (!c_mark_addressable (array))
2356             return error_mark_node;
2357         }
2358       /* An array that is indexed by a constant value which is not within
2359          the array bounds cannot be stored in a register either; because we
2360          would get a crash in store_bit_field/extract_bit_field when trying
2361          to access a non-existent part of the register.  */
2362       if (TREE_CODE (index) == INTEGER_CST
2363           && TYPE_DOMAIN (TREE_TYPE (array))
2364           && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2365         {
2366           if (!c_mark_addressable (array))
2367             return error_mark_node;
2368         }
2369
2370       if (pedantic)
2371         {
2372           tree foo = array;
2373           while (TREE_CODE (foo) == COMPONENT_REF)
2374             foo = TREE_OPERAND (foo, 0);
2375           if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
2376             pedwarn (loc, OPT_pedantic,
2377                      "ISO C forbids subscripting %<register%> array");
2378           else if (!flag_isoc99 && !lvalue_p (foo))
2379             pedwarn (loc, OPT_pedantic,
2380                      "ISO C90 forbids subscripting non-lvalue array");
2381         }
2382
2383       type = TREE_TYPE (TREE_TYPE (array));
2384       rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2385       /* Array ref is const/volatile if the array elements are
2386          or if the array is.  */
2387       TREE_READONLY (rval)
2388         |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2389             | TREE_READONLY (array));
2390       TREE_SIDE_EFFECTS (rval)
2391         |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2392             | TREE_SIDE_EFFECTS (array));
2393       TREE_THIS_VOLATILE (rval)
2394         |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2395             /* This was added by rms on 16 Nov 91.
2396                It fixes  vol struct foo *a;  a->elts[1]
2397                in an inline function.
2398                Hope it doesn't break something else.  */
2399             | TREE_THIS_VOLATILE (array));
2400       ret = require_complete_type (rval);
2401       protected_set_expr_location (ret, loc);
2402       return ret;
2403     }
2404   else
2405     {
2406       tree ar = default_conversion (array);
2407
2408       if (ar == error_mark_node)
2409         return ar;
2410
2411       gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2412       gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2413
2414       return build_indirect_ref
2415         (loc, build_binary_op (loc, PLUS_EXPR, ar, index, 0),
2416          RO_ARRAY_INDEXING);
2417     }
2418 }
2419 \f
2420 /* Build an external reference to identifier ID.  FUN indicates
2421    whether this will be used for a function call.  LOC is the source
2422    location of the identifier.  This sets *TYPE to the type of the
2423    identifier, which is not the same as the type of the returned value
2424    for CONST_DECLs defined as enum constants.  If the type of the
2425    identifier is not available, *TYPE is set to NULL.  */
2426 tree
2427 build_external_ref (location_t loc, tree id, int fun, tree *type)
2428 {
2429   tree ref;
2430   tree decl = lookup_name (id);
2431
2432   /* In Objective-C, an instance variable (ivar) may be preferred to
2433      whatever lookup_name() found.  */
2434   decl = objc_lookup_ivar (decl, id);
2435
2436   *type = NULL;
2437   if (decl && decl != error_mark_node)
2438     {
2439       ref = decl;
2440       *type = TREE_TYPE (ref);
2441     }
2442   else if (fun)
2443     /* Implicit function declaration.  */
2444     ref = implicitly_declare (loc, id);
2445   else if (decl == error_mark_node)
2446     /* Don't complain about something that's already been
2447        complained about.  */
2448     return error_mark_node;
2449   else
2450     {
2451       undeclared_variable (loc, id);
2452       return error_mark_node;
2453     }
2454
2455   if (TREE_TYPE (ref) == error_mark_node)
2456     return error_mark_node;
2457
2458   if (TREE_DEPRECATED (ref))
2459     warn_deprecated_use (ref, NULL_TREE);
2460
2461   /* Recursive call does not count as usage.  */
2462   if (ref != current_function_decl)
2463     {
2464       TREE_USED (ref) = 1;
2465     }
2466
2467   if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2468     {
2469       if (!in_sizeof && !in_typeof)
2470         C_DECL_USED (ref) = 1;
2471       else if (DECL_INITIAL (ref) == 0
2472                && DECL_EXTERNAL (ref)
2473                && !TREE_PUBLIC (ref))
2474         record_maybe_used_decl (ref);
2475     }
2476
2477   if (TREE_CODE (ref) == CONST_DECL)
2478     {
2479       used_types_insert (TREE_TYPE (ref));
2480
2481       if (warn_cxx_compat
2482           && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2483           && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2484         {
2485           warning_at (loc, OPT_Wc___compat,
2486                       ("enum constant defined in struct or union "
2487                        "is not visible in C++"));
2488           inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2489         }
2490
2491       ref = DECL_INITIAL (ref);
2492       TREE_CONSTANT (ref) = 1;
2493     }
2494   else if (current_function_decl != 0
2495            && !DECL_FILE_SCOPE_P (current_function_decl)
2496            && (TREE_CODE (ref) == VAR_DECL
2497                || TREE_CODE (ref) == PARM_DECL
2498                || TREE_CODE (ref) == FUNCTION_DECL))
2499     {
2500       tree context = decl_function_context (ref);
2501
2502       if (context != 0 && context != current_function_decl)
2503         DECL_NONLOCAL (ref) = 1;
2504     }
2505   /* C99 6.7.4p3: An inline definition of a function with external
2506      linkage ... shall not contain a reference to an identifier with
2507      internal linkage.  */
2508   else if (current_function_decl != 0
2509            && DECL_DECLARED_INLINE_P (current_function_decl)
2510            && DECL_EXTERNAL (current_function_decl)
2511            && VAR_OR_FUNCTION_DECL_P (ref)
2512            && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
2513            && ! TREE_PUBLIC (ref)
2514            && DECL_CONTEXT (ref) != current_function_decl)
2515     record_inline_static (loc, current_function_decl, ref,
2516                           csi_internal);
2517
2518   return ref;
2519 }
2520
2521 /* Record details of decls possibly used inside sizeof or typeof.  */
2522 struct maybe_used_decl
2523 {
2524   /* The decl.  */
2525   tree decl;
2526   /* The level seen at (in_sizeof + in_typeof).  */
2527   int level;
2528   /* The next one at this level or above, or NULL.  */
2529   struct maybe_used_decl *next;
2530 };
2531
2532 static struct maybe_used_decl *maybe_used_decls;
2533
2534 /* Record that DECL, an undefined static function reference seen
2535    inside sizeof or typeof, might be used if the operand of sizeof is
2536    a VLA type or the operand of typeof is a variably modified
2537    type.  */
2538
2539 static void
2540 record_maybe_used_decl (tree decl)
2541 {
2542   struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2543   t->decl = decl;
2544   t->level = in_sizeof + in_typeof;
2545   t->next = maybe_used_decls;
2546   maybe_used_decls = t;
2547 }
2548
2549 /* Pop the stack of decls possibly used inside sizeof or typeof.  If
2550    USED is false, just discard them.  If it is true, mark them used
2551    (if no longer inside sizeof or typeof) or move them to the next
2552    level up (if still inside sizeof or typeof).  */
2553
2554 void
2555 pop_maybe_used (bool used)
2556 {
2557   struct maybe_used_decl *p = maybe_used_decls;
2558   int cur_level = in_sizeof + in_typeof;
2559   while (p && p->level > cur_level)
2560     {
2561       if (used)
2562         {
2563           if (cur_level == 0)
2564             C_DECL_USED (p->decl) = 1;
2565           else
2566             p->level = cur_level;
2567         }
2568       p = p->next;
2569     }
2570   if (!used || cur_level == 0)
2571     maybe_used_decls = p;
2572 }
2573
2574 /* Return the result of sizeof applied to EXPR.  */
2575
2576 struct c_expr
2577 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2578 {
2579   struct c_expr ret;
2580   if (expr.value == error_mark_node)
2581     {
2582       ret.value = error_mark_node;
2583       ret.original_code = ERROR_MARK;
2584       ret.original_type = NULL;
2585       pop_maybe_used (false);
2586     }
2587   else
2588     {
2589       bool expr_const_operands = true;
2590       tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2591                                        &expr_const_operands);
2592       ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2593       ret.original_code = ERROR_MARK;
2594       ret.original_type = NULL;
2595       if (c_vla_type_p (TREE_TYPE (folded_expr)))
2596         {
2597           /* sizeof is evaluated when given a vla (C99 6.5.3.4p2).  */
2598           ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2599                               folded_expr, ret.value);
2600           C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2601           SET_EXPR_LOCATION (ret.value, loc);
2602         }
2603       pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2604     }
2605   return ret;
2606 }
2607
2608 /* Return the result of sizeof applied to T, a structure for the type
2609    name passed to sizeof (rather than the type itself).  LOC is the
2610    location of the original expression.  */
2611
2612 struct c_expr
2613 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2614 {
2615   tree type;
2616   struct c_expr ret;
2617   tree type_expr = NULL_TREE;
2618   bool type_expr_const = true;
2619   type = groktypename (t, &type_expr, &type_expr_const);
2620   ret.value = c_sizeof (loc, type);
2621   ret.original_code = ERROR_MARK;
2622   ret.original_type = NULL;
2623   if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2624       && c_vla_type_p (type))
2625     {
2626       /* If the type is a [*] array, it is a VLA but is represented as
2627          having a size of zero.  In such a case we must ensure that
2628          the result of sizeof does not get folded to a constant by
2629          c_fully_fold, because if the size is evaluated the result is
2630          not constant and so constraints on zero or negative size
2631          arrays must not be applied when this sizeof call is inside
2632          another array declarator.  */
2633       if (!type_expr)
2634         type_expr = integer_zero_node;
2635       ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2636                           type_expr, ret.value);
2637       C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2638     }
2639   pop_maybe_used (type != error_mark_node
2640                   ? C_TYPE_VARIABLE_SIZE (type) : false);
2641   return ret;
2642 }
2643
2644 /* Build a function call to function FUNCTION with parameters PARAMS.
2645    The function call is at LOC.
2646    PARAMS is a list--a chain of TREE_LIST nodes--in which the
2647    TREE_VALUE of each node is a parameter-expression.
2648    FUNCTION's data type may be a function type or a pointer-to-function.  */
2649
2650 tree
2651 build_function_call (location_t loc, tree function, tree params)
2652 {
2653   VEC(tree,gc) *vec;
2654   tree ret;
2655
2656   vec = VEC_alloc (tree, gc, list_length (params));
2657   for (; params; params = TREE_CHAIN (params))
2658     VEC_quick_push (tree, vec, TREE_VALUE (params));
2659   ret = build_function_call_vec (loc, function, vec, NULL);
2660   VEC_free (tree, gc, vec);
2661   return ret;
2662 }
2663
2664 /* Build a function call to function FUNCTION with parameters PARAMS.
2665    ORIGTYPES, if not NULL, is a vector of types; each element is
2666    either NULL or the original type of the corresponding element in
2667    PARAMS.  The original type may differ from TREE_TYPE of the
2668    parameter for enums.  FUNCTION's data type may be a function type
2669    or pointer-to-function.  This function changes the elements of
2670    PARAMS.  */
2671
2672 tree
2673 build_function_call_vec (location_t loc, tree function, VEC(tree,gc) *params,
2674                          VEC(tree,gc) *origtypes)
2675 {
2676   tree fntype, fundecl = 0;
2677   tree name = NULL_TREE, result;
2678   tree tem;
2679   int nargs;
2680   tree *argarray;
2681
2682
2683   /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue.  */
2684   STRIP_TYPE_NOPS (function);
2685
2686   /* Convert anything with function type to a pointer-to-function.  */
2687   if (TREE_CODE (function) == FUNCTION_DECL)
2688     {
2689       /* Implement type-directed function overloading for builtins.
2690          resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2691          handle all the type checking.  The result is a complete expression
2692          that implements this function call.  */
2693       tem = resolve_overloaded_builtin (loc, function, params);
2694       if (tem)
2695         return tem;
2696
2697       name = DECL_NAME (function);
2698       fundecl = function;
2699     }
2700   if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2701     function = function_to_pointer_conversion (loc, function);
2702
2703   /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2704      expressions, like those used for ObjC messenger dispatches.  */
2705   if (!VEC_empty (tree, params))
2706     function = objc_rewrite_function_call (function,
2707                                            VEC_index (tree, params, 0));
2708
2709   function = c_fully_fold (function, false, NULL);
2710
2711   fntype = TREE_TYPE (function);
2712
2713   if (TREE_CODE (fntype) == ERROR_MARK)
2714     return error_mark_node;
2715
2716   if (!(TREE_CODE (fntype) == POINTER_TYPE
2717         && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2718     {
2719       error_at (loc, "called object %qE is not a function", function);
2720       return error_mark_node;
2721     }
2722
2723   if (fundecl && TREE_THIS_VOLATILE (fundecl))
2724     current_function_returns_abnormally = 1;
2725
2726   /* fntype now gets the type of function pointed to.  */
2727   fntype = TREE_TYPE (fntype);
2728
2729   /* Convert the parameters to the types declared in the
2730      function prototype, or apply default promotions.  */
2731
2732   nargs = convert_arguments (TYPE_ARG_TYPES (fntype), params, origtypes,
2733                              function, fundecl);
2734   if (nargs < 0)
2735     return error_mark_node;
2736
2737   /* Check that the function is called through a compatible prototype.
2738      If it is not, replace the call by a trap, wrapped up in a compound
2739      expression if necessary.  This has the nice side-effect to prevent
2740      the tree-inliner from generating invalid assignment trees which may
2741      blow up in the RTL expander later.  */
2742   if (CONVERT_EXPR_P (function)
2743       && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2744       && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2745       && !comptypes (fntype, TREE_TYPE (tem)))
2746     {
2747       tree return_type = TREE_TYPE (fntype);
2748       tree trap = build_function_call (loc, built_in_decls[BUILT_IN_TRAP],
2749                                        NULL_TREE);
2750       int i;
2751
2752       /* This situation leads to run-time undefined behavior.  We can't,
2753          therefore, simply error unless we can prove that all possible
2754          executions of the program must execute the code.  */
2755       if (warning_at (loc, 0, "function called through a non-compatible type"))
2756         /* We can, however, treat "undefined" any way we please.
2757            Call abort to encourage the user to fix the program.  */
2758         inform (loc, "if this code is reached, the program will abort");
2759       /* Before the abort, allow the function arguments to exit or
2760          call longjmp.  */
2761       for (i = 0; i < nargs; i++)
2762         trap = build2 (COMPOUND_EXPR, void_type_node,
2763                        VEC_index (tree, params, i), trap);
2764
2765       if (VOID_TYPE_P (return_type))
2766         {
2767           if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2768             pedwarn (loc, 0,
2769                      "function with qualified void return type called");
2770           return trap;
2771         }
2772       else
2773         {
2774           tree rhs;
2775
2776           if (AGGREGATE_TYPE_P (return_type))
2777             rhs = build_compound_literal (loc, return_type,
2778                                           build_constructor (return_type, 0),
2779                                           false);
2780           else
2781             rhs = fold_convert_loc (loc, return_type, integer_zero_node);
2782
2783           return require_complete_type (build2 (COMPOUND_EXPR, return_type,
2784                                                 trap, rhs));
2785         }
2786     }
2787
2788   argarray = VEC_address (tree, params);
2789
2790   /* Check that arguments to builtin functions match the expectations.  */
2791   if (fundecl
2792       && DECL_BUILT_IN (fundecl)
2793       && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2794       && !check_builtin_function_arguments (fundecl, nargs, argarray))
2795     return error_mark_node;
2796
2797   /* Check that the arguments to the function are valid.  */
2798   check_function_arguments (TYPE_ATTRIBUTES (fntype), nargs, argarray,
2799                             TYPE_ARG_TYPES (fntype));
2800
2801   if (name != NULL_TREE
2802       && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
2803     {
2804       if (require_constant_value)
2805         result =
2806           fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
2807                                                  function, nargs, argarray);
2808       else
2809         result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
2810                                             function, nargs, argarray);
2811       if (TREE_CODE (result) == NOP_EXPR
2812           && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2813         STRIP_TYPE_NOPS (result);
2814     }
2815   else
2816     result = build_call_array_loc (loc, TREE_TYPE (fntype),
2817                                    function, nargs, argarray);
2818
2819   if (VOID_TYPE_P (TREE_TYPE (result)))
2820     {
2821       if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
2822         pedwarn (loc, 0,
2823                  "function with qualified void return type called");
2824       return result;
2825     }
2826   return require_complete_type (result);
2827 }
2828 \f
2829 /* Convert the argument expressions in the vector VALUES
2830    to the types in the list TYPELIST.
2831
2832    If TYPELIST is exhausted, or when an element has NULL as its type,
2833    perform the default conversions.
2834
2835    ORIGTYPES is the original types of the expressions in VALUES.  This
2836    holds the type of enum values which have been converted to integral
2837    types.  It may be NULL.
2838
2839    FUNCTION is a tree for the called function.  It is used only for
2840    error messages, where it is formatted with %qE.
2841
2842    This is also where warnings about wrong number of args are generated.
2843
2844    Returns the actual number of arguments processed (which may be less
2845    than the length of VALUES in some error situations), or -1 on
2846    failure.  */
2847
2848 static int
2849 convert_arguments (tree typelist, VEC(tree,gc) *values,
2850                    VEC(tree,gc) *origtypes, tree function, tree fundecl)
2851 {
2852   tree typetail, val;
2853   unsigned int parmnum;
2854   bool error_args = false;
2855   const bool type_generic = fundecl
2856     && lookup_attribute ("type generic", TYPE_ATTRIBUTES(TREE_TYPE (fundecl)));
2857   bool type_generic_remove_excess_precision = false;
2858   tree selector;
2859
2860   /* Change pointer to function to the function itself for
2861      diagnostics.  */
2862   if (TREE_CODE (function) == ADDR_EXPR
2863       && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2864     function = TREE_OPERAND (function, 0);
2865
2866   /* Handle an ObjC selector specially for diagnostics.  */
2867   selector = objc_message_selector ();
2868
2869   /* For type-generic built-in functions, determine whether excess
2870      precision should be removed (classification) or not
2871      (comparison).  */
2872   if (type_generic
2873       && DECL_BUILT_IN (fundecl)
2874       && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
2875     {
2876       switch (DECL_FUNCTION_CODE (fundecl))
2877         {
2878         case BUILT_IN_ISFINITE:
2879         case BUILT_IN_ISINF:
2880         case BUILT_IN_ISINF_SIGN:
2881         case BUILT_IN_ISNAN:
2882         case BUILT_IN_ISNORMAL:
2883         case BUILT_IN_FPCLASSIFY:
2884           type_generic_remove_excess_precision = true;
2885           break;
2886
2887         default:
2888           type_generic_remove_excess_precision = false;
2889           break;
2890         }
2891     }
2892
2893   /* Scan the given expressions and types, producing individual
2894      converted arguments.  */
2895
2896   for (typetail = typelist, parmnum = 0;
2897        VEC_iterate (tree, values, parmnum, val);
2898        ++parmnum)
2899     {
2900       tree type = typetail ? TREE_VALUE (typetail) : 0;
2901       tree valtype = TREE_TYPE (val);
2902       tree rname = function;
2903       int argnum = parmnum + 1;
2904       const char *invalid_func_diag;
2905       bool excess_precision = false;
2906       bool npc;
2907       tree parmval;
2908
2909       if (type == void_type_node)
2910         {
2911           error_at (input_location,
2912                     "too many arguments to function %qE", function);
2913           if (fundecl && !DECL_BUILT_IN (fundecl))
2914             inform (DECL_SOURCE_LOCATION (fundecl), "declared here");
2915           return parmnum;
2916         }
2917
2918       if (selector && argnum > 2)
2919         {
2920           rname = selector;
2921           argnum -= 2;
2922         }
2923
2924       npc = null_pointer_constant_p (val);
2925
2926       /* If there is excess precision and a prototype, convert once to
2927          the required type rather than converting via the semantic
2928          type.  Likewise without a prototype a float value represented
2929          as long double should be converted once to double.  But for
2930          type-generic classification functions excess precision must
2931          be removed here.  */
2932       if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
2933           && (type || !type_generic || !type_generic_remove_excess_precision))
2934         {
2935           val = TREE_OPERAND (val, 0);
2936           excess_precision = true;
2937         }
2938       val = c_fully_fold (val, false, NULL);
2939       STRIP_TYPE_NOPS (val);
2940
2941       val = require_complete_type (val);
2942
2943       if (type != 0)
2944         {
2945           /* Formal parm type is specified by a function prototype.  */
2946
2947           if (type == error_mark_node || !COMPLETE_TYPE_P (type))
2948             {
2949               error ("type of formal parameter %d is incomplete", parmnum + 1);
2950               parmval = val;
2951             }
2952           else
2953             {
2954               tree origtype;
2955
2956               /* Optionally warn about conversions that
2957                  differ from the default conversions.  */
2958               if (warn_traditional_conversion || warn_traditional)
2959                 {
2960                   unsigned int formal_prec = TYPE_PRECISION (type);
2961
2962                   if (INTEGRAL_TYPE_P (type)
2963                       && TREE_CODE (valtype) == REAL_TYPE)
2964                     warning (0, "passing argument %d of %qE as integer "
2965                              "rather than floating due to prototype",
2966                              argnum, rname);
2967                   if (INTEGRAL_TYPE_P (type)
2968                       && TREE_CODE (valtype) == COMPLEX_TYPE)
2969                     warning (0, "passing argument %d of %qE as integer "
2970                              "rather than complex due to prototype",
2971                              argnum, rname);
2972                   else if (TREE_CODE (type) == COMPLEX_TYPE
2973                            && TREE_CODE (valtype) == REAL_TYPE)
2974                     warning (0, "passing argument %d of %qE as complex "
2975                              "rather than floating due to prototype",
2976                              argnum, rname);
2977                   else if (TREE_CODE (type) == REAL_TYPE
2978                            && INTEGRAL_TYPE_P (valtype))
2979                     warning (0, "passing argument %d of %qE as floating "
2980                              "rather than integer due to prototype",
2981                              argnum, rname);
2982                   else if (TREE_CODE (type) == COMPLEX_TYPE
2983                            && INTEGRAL_TYPE_P (valtype))
2984                     warning (0, "passing argument %d of %qE as complex "
2985                              "rather than integer due to prototype",
2986                              argnum, rname);
2987                   else if (TREE_CODE (type) == REAL_TYPE
2988                            && TREE_CODE (valtype) == COMPLEX_TYPE)
2989                     warning (0, "passing argument %d of %qE as floating "
2990                              "rather than complex due to prototype",
2991                              argnum, rname);
2992                   /* ??? At some point, messages should be written about
2993                      conversions between complex types, but that's too messy
2994                      to do now.  */
2995                   else if (TREE_CODE (type) == REAL_TYPE
2996                            && TREE_CODE (valtype) == REAL_TYPE)
2997                     {
2998                       /* Warn if any argument is passed as `float',
2999                          since without a prototype it would be `double'.  */
3000                       if (formal_prec == TYPE_PRECISION (float_type_node)
3001                           && type != dfloat32_type_node)
3002                         warning (0, "passing argument %d of %qE as %<float%> "
3003                                  "rather than %<double%> due to prototype",
3004                                  argnum, rname);
3005
3006                       /* Warn if mismatch between argument and prototype
3007                          for decimal float types.  Warn of conversions with
3008                          binary float types and of precision narrowing due to
3009                          prototype. */
3010                       else if (type != valtype
3011                                && (type == dfloat32_type_node
3012                                    || type == dfloat64_type_node
3013                                    || type == dfloat128_type_node
3014                                    || valtype == dfloat32_type_node
3015                                    || valtype == dfloat64_type_node
3016                                    || valtype == dfloat128_type_node)
3017                                && (formal_prec
3018                                    <= TYPE_PRECISION (valtype)
3019                                    || (type == dfloat128_type_node
3020                                        && (valtype
3021                                            != dfloat64_type_node
3022                                            && (valtype
3023                                                != dfloat32_type_node)))
3024                                    || (type == dfloat64_type_node
3025                                        && (valtype
3026                                            != dfloat32_type_node))))
3027                         warning (0, "passing argument %d of %qE as %qT "
3028                                  "rather than %qT due to prototype",
3029                                  argnum, rname, type, valtype);
3030
3031                     }
3032                   /* Detect integer changing in width or signedness.
3033                      These warnings are only activated with
3034                      -Wtraditional-conversion, not with -Wtraditional.  */
3035                   else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
3036                            && INTEGRAL_TYPE_P (valtype))
3037                     {
3038                       tree would_have_been = default_conversion (val);
3039                       tree type1 = TREE_TYPE (would_have_been);
3040
3041                       if (TREE_CODE (type) == ENUMERAL_TYPE
3042                           && (TYPE_MAIN_VARIANT (type)
3043                               == TYPE_MAIN_VARIANT (valtype)))
3044                         /* No warning if function asks for enum
3045                            and the actual arg is that enum type.  */
3046                         ;
3047                       else if (formal_prec != TYPE_PRECISION (type1))
3048                         warning (OPT_Wtraditional_conversion,
3049                                  "passing argument %d of %qE "
3050                                  "with different width due to prototype",
3051                                  argnum, rname);
3052                       else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3053                         ;
3054                       /* Don't complain if the formal parameter type
3055                          is an enum, because we can't tell now whether
3056                          the value was an enum--even the same enum.  */
3057                       else if (TREE_CODE (type) == ENUMERAL_TYPE)
3058                         ;
3059                       else if (TREE_CODE (val) == INTEGER_CST
3060                                && int_fits_type_p (val, type))
3061                         /* Change in signedness doesn't matter
3062                            if a constant value is unaffected.  */
3063                         ;
3064                       /* If the value is extended from a narrower
3065                          unsigned type, it doesn't matter whether we
3066                          pass it as signed or unsigned; the value
3067                          certainly is the same either way.  */
3068                       else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3069                                && TYPE_UNSIGNED (valtype))
3070                         ;
3071                       else if (TYPE_UNSIGNED (type))
3072                         warning (OPT_Wtraditional_conversion,
3073                                  "passing argument %d of %qE "
3074                                  "as unsigned due to prototype",
3075                                  argnum, rname);
3076                       else
3077                         warning (OPT_Wtraditional_conversion,
3078                                  "passing argument %d of %qE "
3079                                  "as signed due to prototype", argnum, rname);
3080                     }
3081                 }
3082
3083               /* Possibly restore an EXCESS_PRECISION_EXPR for the
3084                  sake of better warnings from convert_and_check.  */
3085               if (excess_precision)
3086                 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3087               origtype = (origtypes == NULL
3088                           ? NULL_TREE
3089                           : VEC_index (tree, origtypes, parmnum));
3090               parmval = convert_for_assignment (input_location, type, val,
3091                                                 origtype, ic_argpass, npc,
3092                                                 fundecl, function,
3093                                                 parmnum + 1);
3094
3095               if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3096                   && INTEGRAL_TYPE_P (type)
3097                   && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3098                 parmval = default_conversion (parmval);
3099             }
3100         }
3101       else if (TREE_CODE (valtype) == REAL_TYPE
3102                && (TYPE_PRECISION (valtype)
3103                    < TYPE_PRECISION (double_type_node))
3104                && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3105         {
3106           if (type_generic)
3107             parmval = val;
3108           else
3109             {
3110               /* Convert `float' to `double'.  */
3111               if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3112                 warning (OPT_Wdouble_promotion,
3113                          "implicit conversion from %qT to %qT when passing "
3114                          "argument to function",
3115                          valtype, double_type_node);
3116               parmval = convert (double_type_node, val);
3117             }
3118         }
3119       else if (excess_precision && !type_generic)
3120         /* A "double" argument with excess precision being passed
3121            without a prototype or in variable arguments.  */
3122         parmval = convert (valtype, val);
3123       else if ((invalid_func_diag =
3124                 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3125         {
3126           error (invalid_func_diag);
3127           return -1;
3128         }
3129       else
3130         /* Convert `short' and `char' to full-size `int'.  */
3131         parmval = default_conversion (val);
3132
3133       VEC_replace (tree, values, parmnum, parmval);
3134       if (parmval == error_mark_node)
3135         error_args = true;
3136
3137       if (typetail)
3138         typetail = TREE_CHAIN (typetail);
3139     }
3140
3141   gcc_assert (parmnum == VEC_length (tree, values));
3142
3143   if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3144     {
3145       error_at (input_location, 
3146                 "too few arguments to function %qE", function);
3147       if (fundecl && !DECL_BUILT_IN (fundecl))
3148         inform (DECL_SOURCE_LOCATION (fundecl), "declared here");
3149       return -1;
3150     }
3151
3152   return error_args ? -1 : (int) parmnum;
3153 }
3154 \f
3155 /* This is the entry point used by the parser to build unary operators
3156    in the input.  CODE, a tree_code, specifies the unary operator, and
3157    ARG is the operand.  For unary plus, the C parser currently uses
3158    CONVERT_EXPR for code.
3159
3160    LOC is the location to use for the tree generated.
3161 */
3162
3163 struct c_expr
3164 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3165 {
3166   struct c_expr result;
3167
3168   result.value = build_unary_op (loc, code, arg.value, 0);
3169   result.original_code = code;
3170   result.original_type = NULL;
3171
3172   if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3173     overflow_warning (loc, result.value);
3174
3175   return result;
3176 }
3177
3178 /* This is the entry point used by the parser to build binary operators
3179    in the input.  CODE, a tree_code, specifies the binary operator, and
3180    ARG1 and ARG2 are the operands.  In addition to constructing the
3181    expression, we check for operands that were written with other binary
3182    operators in a way that is likely to confuse the user.
3183
3184    LOCATION is the location of the binary operator.  */
3185
3186 struct c_expr
3187 parser_build_binary_op (location_t location, enum tree_code code,
3188                         struct c_expr arg1, struct c_expr arg2)
3189 {
3190   struct c_expr result;
3191
3192   enum tree_code code1 = arg1.original_code;
3193   enum tree_code code2 = arg2.original_code;
3194   tree type1 = (arg1.original_type
3195                 ? arg1.original_type
3196                 : TREE_TYPE (arg1.value));
3197   tree type2 = (arg2.original_type
3198                 ? arg2.original_type
3199                 : TREE_TYPE (arg2.value));
3200
3201   result.value = build_binary_op (location, code,
3202                                   arg1.value, arg2.value, 1);
3203   result.original_code = code;
3204   result.original_type = NULL;
3205
3206   if (TREE_CODE (result.value) == ERROR_MARK)
3207     return result;
3208
3209   if (location != UNKNOWN_LOCATION)
3210     protected_set_expr_location (result.value, location);
3211
3212   /* Check for cases such as x+y<<z which users are likely
3213      to misinterpret.  */
3214   if (warn_parentheses)
3215     warn_about_parentheses (code, code1, arg1.value, code2, arg2.value);
3216
3217   if (warn_logical_op)
3218     warn_logical_operator (input_location, code, TREE_TYPE (result.value),
3219                            code1, arg1.value, code2, arg2.value);
3220
3221   /* Warn about comparisons against string literals, with the exception
3222      of testing for equality or inequality of a string literal with NULL.  */
3223   if (code == EQ_EXPR || code == NE_EXPR)
3224     {
3225       if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3226           || (code2 == STRING_CST && !integer_zerop (arg1.value)))
3227         warning_at (location, OPT_Waddress,
3228                     "comparison with string literal results in unspecified behavior");
3229     }
3230   else if (TREE_CODE_CLASS (code) == tcc_comparison
3231            && (code1 == STRING_CST || code2 == STRING_CST))
3232     warning_at (location, OPT_Waddress,
3233                 "comparison with string literal results in unspecified behavior");
3234
3235   if (TREE_OVERFLOW_P (result.value)
3236       && !TREE_OVERFLOW_P (arg1.value)
3237       && !TREE_OVERFLOW_P (arg2.value))
3238     overflow_warning (location, result.value);
3239
3240   /* Warn about comparisons of different enum types.  */
3241   if (warn_enum_compare
3242       && TREE_CODE_CLASS (code) == tcc_comparison
3243       && TREE_CODE (type1) == ENUMERAL_TYPE
3244       && TREE_CODE (type2) == ENUMERAL_TYPE
3245       && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3246     warning_at (location, OPT_Wenum_compare,
3247                 "comparison between %qT and %qT",
3248                 type1, type2);
3249
3250   return result;
3251 }
3252 \f
3253 /* Return a tree for the difference of pointers OP0 and OP1.
3254    The resulting tree has type int.  */
3255
3256 static tree
3257 pointer_diff (location_t loc, tree op0, tree op1)
3258 {
3259   tree restype = ptrdiff_type_node;
3260   tree result, inttype;
3261
3262   addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3263   addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3264   tree target_type = TREE_TYPE (TREE_TYPE (op0));
3265   tree con0, con1, lit0, lit1;
3266   tree orig_op1 = op1;
3267
3268   /* If the operands point into different address spaces, we need to
3269      explicitly convert them to pointers into the common address space
3270      before we can subtract the numerical address values.  */
3271   if (as0 != as1)
3272     {
3273       addr_space_t as_common;
3274       tree common_type;
3275
3276       /* Determine the common superset address space.  This is guaranteed
3277          to exist because the caller verified that comp_target_types
3278          returned non-zero.  */
3279       if (!addr_space_superset (as0, as1, &as_common))
3280         gcc_unreachable ();
3281
3282       common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3283       op0 = convert (common_type, op0);
3284       op1 = convert (common_type, op1);
3285     }
3286
3287   /* Determine integer type to perform computations in.  This will usually
3288      be the same as the result type (ptrdiff_t), but may need to be a wider
3289      type if pointers for the address space are wider than ptrdiff_t.  */
3290   if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3291     inttype = lang_hooks.types.type_for_size
3292                 (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3293   else
3294     inttype = restype;
3295
3296
3297   if (TREE_CODE (target_type) == VOID_TYPE)
3298     pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3299              "pointer of type %<void *%> used in subtraction");
3300   if (TREE_CODE (target_type) == FUNCTION_TYPE)
3301     pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3302              "pointer to a function used in subtraction");
3303
3304   /* If the conversion to ptrdiff_type does anything like widening or
3305      converting a partial to an integral mode, we get a convert_expression
3306      that is in the way to do any simplifications.
3307      (fold-const.c doesn't know that the extra bits won't be needed.
3308      split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
3309      different mode in place.)
3310      So first try to find a common term here 'by hand'; we want to cover
3311      at least the cases that occur in legal static initializers.  */
3312   if (CONVERT_EXPR_P (op0)
3313       && (TYPE_PRECISION (TREE_TYPE (op0))
3314           == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
3315     con0 = TREE_OPERAND (op0, 0);
3316   else
3317     con0 = op0;
3318   if (CONVERT_EXPR_P (op1)
3319       && (TYPE_PRECISION (TREE_TYPE (op1))
3320           == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))))
3321     con1 = TREE_OPERAND (op1, 0);
3322   else
3323     con1 = op1;
3324
3325   if (TREE_CODE (con0) == PLUS_EXPR)
3326     {
3327       lit0 = TREE_OPERAND (con0, 1);
3328       con0 = TREE_OPERAND (con0, 0);
3329     }
3330   else
3331     lit0 = integer_zero_node;
3332
3333   if (TREE_CODE (con1) == PLUS_EXPR)
3334     {
3335       lit1 = TREE_OPERAND (con1, 1);
3336       con1 = TREE_OPERAND (con1, 0);
3337     }
3338   else
3339     lit1 = integer_zero_node;
3340
3341   if (operand_equal_p (con0, con1, 0))
3342     {
3343       op0 = lit0;
3344       op1 = lit1;
3345     }
3346
3347
3348   /* First do the subtraction as integers;
3349      then drop through to build the divide operator.
3350      Do not do default conversions on the minus operator
3351      in case restype is a short type.  */
3352
3353   op0 = build_binary_op (loc,
3354                          MINUS_EXPR, convert (inttype, op0),
3355                          convert (inttype, op1), 0);
3356   /* This generates an error if op1 is pointer to incomplete type.  */
3357   if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3358     error_at (loc, "arithmetic on pointer to an incomplete type");
3359
3360   /* This generates an error if op0 is pointer to incomplete type.  */
3361   op1 = c_size_in_bytes (target_type);
3362
3363   /* Divide by the size, in easiest possible way.  */
3364   result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3365                             op0, convert (inttype, op1));
3366
3367   /* Convert to final result type if necessary.  */
3368   return convert (restype, result);
3369 }
3370 \f
3371 /* Construct and perhaps optimize a tree representation
3372    for a unary operation.  CODE, a tree_code, specifies the operation
3373    and XARG is the operand.
3374    For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3375    the default promotions (such as from short to int).
3376    For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3377    allows non-lvalues; this is only used to handle conversion of non-lvalue
3378    arrays to pointers in C99.
3379
3380    LOCATION is the location of the operator.  */
3381
3382 tree
3383 build_unary_op (location_t location,
3384                 enum tree_code code, tree xarg, int flag)
3385 {
3386   /* No default_conversion here.  It causes trouble for ADDR_EXPR.  */
3387   tree arg = xarg;
3388   tree argtype = 0;
3389   enum tree_code typecode;
3390   tree val;
3391   tree ret = error_mark_node;
3392   tree eptype = NULL_TREE;
3393   int noconvert = flag;
3394   const char *invalid_op_diag;
3395   bool int_operands;
3396
3397   int_operands = EXPR_INT_CONST_OPERANDS (xarg);
3398   if (int_operands)
3399     arg = remove_c_maybe_const_expr (arg);
3400
3401   if (code != ADDR_EXPR)
3402     arg = require_complete_type (arg);
3403
3404   typecode = TREE_CODE (TREE_TYPE (arg));
3405   if (typecode == ERROR_MARK)
3406     return error_mark_node;
3407   if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3408     typecode = INTEGER_TYPE;
3409
3410   if ((invalid_op_diag
3411        = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3412     {
3413       error_at (location, invalid_op_diag);
3414       return error_mark_node;
3415     }
3416
3417   if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3418     {
3419       eptype = TREE_TYPE (arg);
3420       arg = TREE_OPERAND (arg, 0);
3421     }
3422
3423   switch (code)
3424     {
3425     case CONVERT_EXPR:
3426       /* This is used for unary plus, because a CONVERT_EXPR
3427          is enough to prevent anybody from looking inside for
3428          associativity, but won't generate any code.  */
3429       if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3430             || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3431             || typecode&nb