OSDN Git Service

04fa74540238c145aa19718f57777b4261ff3864
[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 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING.  If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.  */
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 "rtl.h"
33 #include "tree.h"
34 #include "langhooks.h"
35 #include "c-tree.h"
36 #include "tm_p.h"
37 #include "flags.h"
38 #include "output.h"
39 #include "expr.h"
40 #include "toplev.h"
41 #include "intl.h"
42 #include "ggc.h"
43 #include "target.h"
44 #include "tree-iterator.h"
45 #include "tree-gimple.h"
46 #include "tree-flow.h"
47
48 /* Possible cases of implicit bad conversions.  Used to select
49    diagnostic messages in convert_for_assignment.  */
50 enum impl_conv {
51   ic_argpass,
52   ic_argpass_nonproto,
53   ic_assign,
54   ic_init,
55   ic_return
56 };
57
58 /* The level of nesting inside "__alignof__".  */
59 int in_alignof;
60
61 /* The level of nesting inside "sizeof".  */
62 int in_sizeof;
63
64 /* The level of nesting inside "typeof".  */
65 int in_typeof;
66
67 struct c_label_context_se *label_context_stack_se;
68 struct c_label_context_vm *label_context_stack_vm;
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 tree qualify_type (tree, tree);
78 static int tagged_types_tu_compatible_p (tree, tree);
79 static int comp_target_types (tree, tree, int);
80 static int function_types_compatible_p (tree, tree);
81 static int type_lists_compatible_p (tree, tree);
82 static tree decl_constant_value_for_broken_optimization (tree);
83 static tree default_function_array_conversion (tree);
84 static tree lookup_field (tree, tree);
85 static tree convert_arguments (tree, tree, tree, tree);
86 static tree pointer_diff (tree, tree);
87 static tree convert_for_assignment (tree, tree, enum impl_conv, tree, tree,
88                                     int);
89 static tree valid_compound_expr_initializer (tree, tree);
90 static void push_string (const char *);
91 static void push_member_name (tree);
92 static void push_array_bounds (int);
93 static int spelling_length (void);
94 static char *print_spelling (char *);
95 static void warning_init (const char *);
96 static tree digest_init (tree, tree, bool, int);
97 static void output_init_element (tree, bool, tree, tree, int);
98 static void output_pending_init_elements (int);
99 static int set_designator (int);
100 static void push_range_stack (tree);
101 static void add_pending_init (tree, tree);
102 static void set_nonincremental_init (void);
103 static void set_nonincremental_init_from_string (tree);
104 static tree find_init_member (tree);
105 static void readonly_error (tree, enum lvalue_use);
106 static int lvalue_or_else (tree, enum lvalue_use);
107 static int lvalue_p (tree);
108 static void record_maybe_used_decl (tree);
109 \f
110 /* Do `exp = require_complete_type (exp);' to make sure exp
111    does not have an incomplete type.  (That includes void types.)  */
112
113 tree
114 require_complete_type (tree value)
115 {
116   tree type = TREE_TYPE (value);
117
118   if (value == error_mark_node || type == error_mark_node)
119     return error_mark_node;
120
121   /* First, detect a valid value with a complete type.  */
122   if (COMPLETE_TYPE_P (type))
123     return value;
124
125   c_incomplete_type_error (value, type);
126   return error_mark_node;
127 }
128
129 /* Print an error message for invalid use of an incomplete type.
130    VALUE is the expression that was used (or 0 if that isn't known)
131    and TYPE is the type that was invalid.  */
132
133 void
134 c_incomplete_type_error (tree value, tree type)
135 {
136   const char *type_code_string;
137
138   /* Avoid duplicate error message.  */
139   if (TREE_CODE (type) == ERROR_MARK)
140     return;
141
142   if (value != 0 && (TREE_CODE (value) == VAR_DECL
143                      || TREE_CODE (value) == PARM_DECL))
144     error ("%qD has an incomplete type", value);
145   else
146     {
147     retry:
148       /* We must print an error message.  Be clever about what it says.  */
149
150       switch (TREE_CODE (type))
151         {
152         case RECORD_TYPE:
153           type_code_string = "struct";
154           break;
155
156         case UNION_TYPE:
157           type_code_string = "union";
158           break;
159
160         case ENUMERAL_TYPE:
161           type_code_string = "enum";
162           break;
163
164         case VOID_TYPE:
165           error ("invalid use of void expression");
166           return;
167
168         case ARRAY_TYPE:
169           if (TYPE_DOMAIN (type))
170             {
171               if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
172                 {
173                   error ("invalid use of flexible array member");
174                   return;
175                 }
176               type = TREE_TYPE (type);
177               goto retry;
178             }
179           error ("invalid use of array with unspecified bounds");
180           return;
181
182         default:
183           gcc_unreachable ();
184         }
185
186       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
187         error ("invalid use of undefined type %<%s %E%>",
188                type_code_string, TYPE_NAME (type));
189       else
190         /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL.  */
191         error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
192     }
193 }
194
195 /* Given a type, apply default promotions wrt unnamed function
196    arguments and return the new type.  */
197
198 tree
199 c_type_promotes_to (tree type)
200 {
201   if (TYPE_MAIN_VARIANT (type) == float_type_node)
202     return double_type_node;
203
204   if (c_promoting_integer_type_p (type))
205     {
206       /* Preserve unsignedness if not really getting any wider.  */
207       if (TYPE_UNSIGNED (type)
208           && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
209         return unsigned_type_node;
210       return integer_type_node;
211     }
212
213   return type;
214 }
215
216 /* Return a variant of TYPE which has all the type qualifiers of LIKE
217    as well as those of TYPE.  */
218
219 static tree
220 qualify_type (tree type, tree like)
221 {
222   return c_build_qualified_type (type,
223                                  TYPE_QUALS (type) | TYPE_QUALS (like));
224 }
225 \f
226 /* Return the composite type of two compatible types.
227
228    We assume that comptypes has already been done and returned
229    nonzero; if that isn't so, this may crash.  In particular, we
230    assume that qualifiers match.  */
231
232 tree
233 composite_type (tree t1, tree t2)
234 {
235   enum tree_code code1;
236   enum tree_code code2;
237   tree attributes;
238
239   /* Save time if the two types are the same.  */
240
241   if (t1 == t2) return t1;
242
243   /* If one type is nonsense, use the other.  */
244   if (t1 == error_mark_node)
245     return t2;
246   if (t2 == error_mark_node)
247     return t1;
248
249   code1 = TREE_CODE (t1);
250   code2 = TREE_CODE (t2);
251
252   /* Merge the attributes.  */
253   attributes = targetm.merge_type_attributes (t1, t2);
254
255   /* If one is an enumerated type and the other is the compatible
256      integer type, the composite type might be either of the two
257      (DR#013 question 3).  For consistency, use the enumerated type as
258      the composite type.  */
259
260   if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
261     return t1;
262   if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
263     return t2;
264
265   gcc_assert (code1 == code2);
266
267   switch (code1)
268     {
269     case POINTER_TYPE:
270       /* For two pointers, do this recursively on the target type.  */
271       {
272         tree pointed_to_1 = TREE_TYPE (t1);
273         tree pointed_to_2 = TREE_TYPE (t2);
274         tree target = composite_type (pointed_to_1, pointed_to_2);
275         t1 = build_pointer_type (target);
276         t1 = build_type_attribute_variant (t1, attributes);
277         return qualify_type (t1, t2);
278       }
279
280     case ARRAY_TYPE:
281       {
282         tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
283         int quals;
284         tree unqual_elt;
285
286         /* We should not have any type quals on arrays at all.  */
287         gcc_assert (!TYPE_QUALS (t1) && !TYPE_QUALS (t2));
288         
289         /* Save space: see if the result is identical to one of the args.  */
290         if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
291           return build_type_attribute_variant (t1, attributes);
292         if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
293           return build_type_attribute_variant (t2, attributes);
294         
295         if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
296           return build_type_attribute_variant (t1, attributes);
297         if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
298           return build_type_attribute_variant (t2, attributes);
299         
300         /* Merge the element types, and have a size if either arg has
301            one.  We may have qualifiers on the element types.  To set
302            up TYPE_MAIN_VARIANT correctly, we need to form the
303            composite of the unqualified types and add the qualifiers
304            back at the end.  */
305         quals = TYPE_QUALS (strip_array_types (elt));
306         unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
307         t1 = build_array_type (unqual_elt,
308                                TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
309         t1 = c_build_qualified_type (t1, quals);
310         return build_type_attribute_variant (t1, attributes);
311       }
312
313     case FUNCTION_TYPE:
314       /* Function types: prefer the one that specified arg types.
315          If both do, merge the arg types.  Also merge the return types.  */
316       {
317         tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
318         tree p1 = TYPE_ARG_TYPES (t1);
319         tree p2 = TYPE_ARG_TYPES (t2);
320         int len;
321         tree newargs, n;
322         int i;
323
324         /* Save space: see if the result is identical to one of the args.  */
325         if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
326           return build_type_attribute_variant (t1, attributes);
327         if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
328           return build_type_attribute_variant (t2, attributes);
329
330         /* Simple way if one arg fails to specify argument types.  */
331         if (TYPE_ARG_TYPES (t1) == 0)
332          {
333             t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
334             t1 = build_type_attribute_variant (t1, attributes);
335             return qualify_type (t1, t2);
336          }
337         if (TYPE_ARG_TYPES (t2) == 0)
338          {
339            t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
340            t1 = build_type_attribute_variant (t1, attributes);
341            return qualify_type (t1, t2);
342          }
343
344         /* If both args specify argument types, we must merge the two
345            lists, argument by argument.  */
346         /* Tell global_bindings_p to return false so that variable_size
347            doesn't die on VLAs in parameter types.  */
348         c_override_global_bindings_to_false = true;
349
350         len = list_length (p1);
351         newargs = 0;
352
353         for (i = 0; i < len; i++)
354           newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
355
356         n = newargs;
357
358         for (; p1;
359              p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
360           {
361             /* A null type means arg type is not specified.
362                Take whatever the other function type has.  */
363             if (TREE_VALUE (p1) == 0)
364               {
365                 TREE_VALUE (n) = TREE_VALUE (p2);
366                 goto parm_done;
367               }
368             if (TREE_VALUE (p2) == 0)
369               {
370                 TREE_VALUE (n) = TREE_VALUE (p1);
371                 goto parm_done;
372               }
373
374             /* Given  wait (union {union wait *u; int *i} *)
375                and  wait (union wait *),
376                prefer  union wait *  as type of parm.  */
377             if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
378                 && TREE_VALUE (p1) != TREE_VALUE (p2))
379               {
380                 tree memb;
381                 tree mv2 = TREE_VALUE (p2);
382                 if (mv2 && mv2 != error_mark_node
383                     && TREE_CODE (mv2) != ARRAY_TYPE)
384                   mv2 = TYPE_MAIN_VARIANT (mv2);
385                 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
386                      memb; memb = TREE_CHAIN (memb))
387                   {
388                     tree mv3 = TREE_TYPE (memb);
389                     if (mv3 && mv3 != error_mark_node
390                         && TREE_CODE (mv3) != ARRAY_TYPE)
391                       mv3 = TYPE_MAIN_VARIANT (mv3);
392                     if (comptypes (mv3, mv2))
393                       {
394                         TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
395                                                          TREE_VALUE (p2));
396                         if (pedantic)
397                           pedwarn ("function types not truly compatible in ISO C");
398                         goto parm_done;
399                       }
400                   }
401               }
402             if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
403                 && TREE_VALUE (p2) != TREE_VALUE (p1))
404               {
405                 tree memb;
406                 tree mv1 = TREE_VALUE (p1);
407                 if (mv1 && mv1 != error_mark_node
408                     && TREE_CODE (mv1) != ARRAY_TYPE)
409                   mv1 = TYPE_MAIN_VARIANT (mv1);
410                 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
411                      memb; memb = TREE_CHAIN (memb))
412                   {
413                     tree mv3 = TREE_TYPE (memb);
414                     if (mv3 && mv3 != error_mark_node
415                         && TREE_CODE (mv3) != ARRAY_TYPE)
416                       mv3 = TYPE_MAIN_VARIANT (mv3);
417                     if (comptypes (mv3, mv1))
418                       {
419                         TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
420                                                          TREE_VALUE (p1));
421                         if (pedantic)
422                           pedwarn ("function types not truly compatible in ISO C");
423                         goto parm_done;
424                       }
425                   }
426               }
427             TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
428           parm_done: ;
429           }
430
431         c_override_global_bindings_to_false = false;
432         t1 = build_function_type (valtype, newargs);
433         t1 = qualify_type (t1, t2);
434         /* ... falls through ...  */
435       }
436
437     default:
438       return build_type_attribute_variant (t1, attributes);
439     }
440
441 }
442
443 /* Return the type of a conditional expression between pointers to
444    possibly differently qualified versions of compatible types.
445
446    We assume that comp_target_types has already been done and returned
447    nonzero; if that isn't so, this may crash.  */
448
449 static tree
450 common_pointer_type (tree t1, tree t2)
451 {
452   tree attributes;
453   tree pointed_to_1, mv1;
454   tree pointed_to_2, mv2;
455   tree target;
456
457   /* Save time if the two types are the same.  */
458
459   if (t1 == t2) return t1;
460
461   /* If one type is nonsense, use the other.  */
462   if (t1 == error_mark_node)
463     return t2;
464   if (t2 == error_mark_node)
465     return t1;
466
467   gcc_assert (TREE_CODE (t1) == POINTER_TYPE
468               && TREE_CODE (t2) == POINTER_TYPE);
469
470   /* Merge the attributes.  */
471   attributes = targetm.merge_type_attributes (t1, t2);
472
473   /* Find the composite type of the target types, and combine the
474      qualifiers of the two types' targets.  Do not lose qualifiers on
475      array element types by taking the TYPE_MAIN_VARIANT.  */
476   mv1 = pointed_to_1 = TREE_TYPE (t1);
477   mv2 = pointed_to_2 = TREE_TYPE (t2);
478   if (TREE_CODE (mv1) != ARRAY_TYPE)
479     mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
480   if (TREE_CODE (mv2) != ARRAY_TYPE)
481     mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
482   target = composite_type (mv1, mv2);
483   t1 = build_pointer_type (c_build_qualified_type
484                            (target,
485                             TYPE_QUALS (pointed_to_1) |
486                             TYPE_QUALS (pointed_to_2)));
487   return build_type_attribute_variant (t1, attributes);
488 }
489
490 /* Return the common type for two arithmetic types under the usual
491    arithmetic conversions.  The default conversions have already been
492    applied, and enumerated types converted to their compatible integer
493    types.  The resulting type is unqualified and has no attributes.
494
495    This is the type for the result of most arithmetic operations
496    if the operands have the given two types.  */
497
498 static tree
499 c_common_type (tree t1, tree t2)
500 {
501   enum tree_code code1;
502   enum tree_code code2;
503
504   /* If one type is nonsense, use the other.  */
505   if (t1 == error_mark_node)
506     return t2;
507   if (t2 == error_mark_node)
508     return t1;
509
510   if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
511     t1 = TYPE_MAIN_VARIANT (t1);
512
513   if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
514     t2 = TYPE_MAIN_VARIANT (t2);
515
516   if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
517     t1 = build_type_attribute_variant (t1, NULL_TREE);
518
519   if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
520     t2 = build_type_attribute_variant (t2, NULL_TREE);
521
522   /* Save time if the two types are the same.  */
523
524   if (t1 == t2) return t1;
525
526   code1 = TREE_CODE (t1);
527   code2 = TREE_CODE (t2);
528
529   gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
530               || code1 == REAL_TYPE || code1 == INTEGER_TYPE);
531   gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
532               || code2 == REAL_TYPE || code2 == INTEGER_TYPE);
533
534   /* If one type is a vector type, return that type.  (How the usual
535      arithmetic conversions apply to the vector types extension is not
536      precisely specified.)  */
537   if (code1 == VECTOR_TYPE)
538     return t1;
539
540   if (code2 == VECTOR_TYPE)
541     return t2;
542
543   /* If one type is complex, form the common type of the non-complex
544      components, then make that complex.  Use T1 or T2 if it is the
545      required type.  */
546   if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
547     {
548       tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
549       tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
550       tree subtype = c_common_type (subtype1, subtype2);
551
552       if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
553         return t1;
554       else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
555         return t2;
556       else
557         return build_complex_type (subtype);
558     }
559
560   /* If only one is real, use it as the result.  */
561
562   if (code1 == REAL_TYPE && code2 != REAL_TYPE)
563     return t1;
564
565   if (code2 == REAL_TYPE && code1 != REAL_TYPE)
566     return t2;
567
568   /* Both real or both integers; use the one with greater precision.  */
569
570   if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
571     return t1;
572   else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
573     return t2;
574
575   /* Same precision.  Prefer long longs to longs to ints when the
576      same precision, following the C99 rules on integer type rank
577      (which are equivalent to the C90 rules for C90 types).  */
578
579   if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
580       || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
581     return long_long_unsigned_type_node;
582
583   if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
584       || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
585     {
586       if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
587         return long_long_unsigned_type_node;
588       else
589         return long_long_integer_type_node;
590     }
591
592   if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
593       || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
594     return long_unsigned_type_node;
595
596   if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
597       || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
598     {
599       /* But preserve unsignedness from the other type,
600          since long cannot hold all the values of an unsigned int.  */
601       if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
602         return long_unsigned_type_node;
603       else
604         return long_integer_type_node;
605     }
606
607   /* Likewise, prefer long double to double even if same size.  */
608   if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
609       || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
610     return long_double_type_node;
611
612   /* Otherwise prefer the unsigned one.  */
613
614   if (TYPE_UNSIGNED (t1))
615     return t1;
616   else
617     return t2;
618 }
619 \f
620 /* Wrapper around c_common_type that is used by c-common.c.  ENUMERAL_TYPEs
621    are allowed here and are converted to their compatible integer types.
622    BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
623    preferably a non-Boolean type as the common type.  */
624 tree
625 common_type (tree t1, tree t2)
626 {
627   if (TREE_CODE (t1) == ENUMERAL_TYPE)
628     t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
629   if (TREE_CODE (t2) == ENUMERAL_TYPE)
630     t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
631
632   /* If both types are BOOLEAN_TYPE, then return boolean_type_node.  */
633   if (TREE_CODE (t1) == BOOLEAN_TYPE
634       && TREE_CODE (t2) == BOOLEAN_TYPE)
635     return boolean_type_node;
636
637   /* If either type is BOOLEAN_TYPE, then return the other.  */
638   if (TREE_CODE (t1) == BOOLEAN_TYPE)
639     return t2;
640   if (TREE_CODE (t2) == BOOLEAN_TYPE)
641     return t1;
642
643   return c_common_type (t1, t2);
644 }
645 \f
646 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
647    or various other operations.  Return 2 if they are compatible
648    but a warning may be needed if you use them together.  */
649
650 int
651 comptypes (tree type1, tree type2)
652 {
653   tree t1 = type1;
654   tree t2 = type2;
655   int attrval, val;
656
657   /* Suppress errors caused by previously reported errors.  */
658
659   if (t1 == t2 || !t1 || !t2
660       || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
661     return 1;
662
663   /* If either type is the internal version of sizetype, return the
664      language version.  */
665   if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
666       && TYPE_ORIG_SIZE_TYPE (t1))
667     t1 = TYPE_ORIG_SIZE_TYPE (t1);
668
669   if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
670       && TYPE_ORIG_SIZE_TYPE (t2))
671     t2 = TYPE_ORIG_SIZE_TYPE (t2);
672
673
674   /* Enumerated types are compatible with integer types, but this is
675      not transitive: two enumerated types in the same translation unit
676      are compatible with each other only if they are the same type.  */
677
678   if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
679     t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
680   else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
681     t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
682
683   if (t1 == t2)
684     return 1;
685
686   /* Different classes of types can't be compatible.  */
687
688   if (TREE_CODE (t1) != TREE_CODE (t2))
689     return 0;
690
691   /* Qualifiers must match. C99 6.7.3p9 */
692
693   if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
694     return 0;
695
696   /* Allow for two different type nodes which have essentially the same
697      definition.  Note that we already checked for equality of the type
698      qualifiers (just above).  */
699
700   if (TREE_CODE (t1) != ARRAY_TYPE
701       && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
702     return 1;
703
704   /* 1 if no need for warning yet, 2 if warning cause has been seen.  */
705   if (!(attrval = targetm.comp_type_attributes (t1, t2)))
706      return 0;
707
708   /* 1 if no need for warning yet, 2 if warning cause has been seen.  */
709   val = 0;
710
711   switch (TREE_CODE (t1))
712     {
713     case POINTER_TYPE:
714       /* We must give ObjC the first crack at comparing pointers, since
715            protocol qualifiers may be involved.  */
716       if (c_dialect_objc () && (val = objc_comptypes (t1, t2, 0)) >= 0)
717         break;
718       /* Do not remove mode or aliasing information.  */
719       if (TYPE_MODE (t1) != TYPE_MODE (t2)
720           || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
721         break;
722       val = (TREE_TYPE (t1) == TREE_TYPE (t2)
723              ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2)));
724       break;
725
726     case FUNCTION_TYPE:
727       val = function_types_compatible_p (t1, t2);
728       break;
729
730     case ARRAY_TYPE:
731       {
732         tree d1 = TYPE_DOMAIN (t1);
733         tree d2 = TYPE_DOMAIN (t2);
734         bool d1_variable, d2_variable;
735         bool d1_zero, d2_zero;
736         val = 1;
737
738         /* Target types must match incl. qualifiers.  */
739         if (TREE_TYPE (t1) != TREE_TYPE (t2)
740             && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2))))
741           return 0;
742
743         /* Sizes must match unless one is missing or variable.  */
744         if (d1 == 0 || d2 == 0 || d1 == d2)
745           break;
746
747         d1_zero = !TYPE_MAX_VALUE (d1);
748         d2_zero = !TYPE_MAX_VALUE (d2);
749
750         d1_variable = (!d1_zero
751                        && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
752                            || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
753         d2_variable = (!d2_zero
754                        && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
755                            || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
756
757         if (d1_variable || d2_variable)
758           break;
759         if (d1_zero && d2_zero)
760           break;
761         if (d1_zero || d2_zero
762             || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
763             || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
764           val = 0;
765
766         break;
767       }
768
769     case RECORD_TYPE:
770       /* We are dealing with two distinct structs.  In assorted Objective-C
771          corner cases, however, these can still be deemed equivalent.  */
772       if (c_dialect_objc () && objc_comptypes (t1, t2, 0) == 1)
773         val = 1;
774
775     case ENUMERAL_TYPE:
776     case UNION_TYPE:
777       if (val != 1 && !same_translation_unit_p (t1, t2))
778         val = tagged_types_tu_compatible_p (t1, t2);
779       break;
780
781     case VECTOR_TYPE:
782       val = TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
783             && comptypes (TREE_TYPE (t1), TREE_TYPE (t2));
784       break;
785
786     default:
787       break;
788     }
789   return attrval == 2 && val == 1 ? 2 : val;
790 }
791
792 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
793    ignoring their qualifiers.  REFLEXIVE is only used by ObjC - set it
794    to 1 or 0 depending if the check of the pointer types is meant to
795    be reflexive or not (typically, assignments are not reflexive,
796    while comparisons are reflexive).
797 */
798
799 static int
800 comp_target_types (tree ttl, tree ttr, int reflexive)
801 {
802   int val;
803   tree mvl, mvr;
804
805   /* Give objc_comptypes a crack at letting these types through.  */
806   if ((val = objc_comptypes (ttl, ttr, reflexive)) >= 0)
807     return val;
808
809   /* Do not lose qualifiers on element types of array types that are
810      pointer targets by taking their TYPE_MAIN_VARIANT.  */
811   mvl = TREE_TYPE (ttl);
812   mvr = TREE_TYPE (ttr);
813   if (TREE_CODE (mvl) != ARRAY_TYPE)
814     mvl = TYPE_MAIN_VARIANT (mvl);
815   if (TREE_CODE (mvr) != ARRAY_TYPE)
816     mvr = TYPE_MAIN_VARIANT (mvr);
817   val = comptypes (mvl, mvr);
818
819   if (val == 2 && pedantic)
820     pedwarn ("types are not quite compatible");
821   return val;
822 }
823 \f
824 /* Subroutines of `comptypes'.  */
825
826 /* Determine whether two trees derive from the same translation unit.
827    If the CONTEXT chain ends in a null, that tree's context is still
828    being parsed, so if two trees have context chains ending in null,
829    they're in the same translation unit.  */
830 int
831 same_translation_unit_p (tree t1, tree t2)
832 {
833   while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
834     switch (TREE_CODE_CLASS (TREE_CODE (t1)))
835       {
836       case tcc_declaration:
837         t1 = DECL_CONTEXT (t1); break;
838       case tcc_type:
839         t1 = TYPE_CONTEXT (t1); break;
840       case tcc_exceptional:
841         t1 = BLOCK_SUPERCONTEXT (t1); break;  /* assume block */
842       default: gcc_unreachable ();
843       }
844
845   while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
846     switch (TREE_CODE_CLASS (TREE_CODE (t2)))
847       {
848       case tcc_declaration:
849         t2 = DECL_CONTEXT (t2); break;
850       case tcc_type:
851         t2 = TYPE_CONTEXT (t2); break;
852       case tcc_exceptional:
853         t2 = BLOCK_SUPERCONTEXT (t2); break;  /* assume block */
854       default: gcc_unreachable ();
855       }
856
857   return t1 == t2;
858 }
859
860 /* The C standard says that two structures in different translation
861    units are compatible with each other only if the types of their
862    fields are compatible (among other things).  So, consider two copies
863    of this structure:  */
864
865 struct tagged_tu_seen {
866   const struct tagged_tu_seen * next;
867   tree t1;
868   tree t2;
869 };
870
871 /* Can they be compatible with each other?  We choose to break the
872    recursion by allowing those types to be compatible.  */
873
874 static const struct tagged_tu_seen * tagged_tu_seen_base;
875
876 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
877    compatible.  If the two types are not the same (which has been
878    checked earlier), this can only happen when multiple translation
879    units are being compiled.  See C99 6.2.7 paragraph 1 for the exact
880    rules.  */
881
882 static int
883 tagged_types_tu_compatible_p (tree t1, tree t2)
884 {
885   tree s1, s2;
886   bool needs_warning = false;
887
888   /* We have to verify that the tags of the types are the same.  This
889      is harder than it looks because this may be a typedef, so we have
890      to go look at the original type.  It may even be a typedef of a
891      typedef...
892      In the case of compiler-created builtin structs the TYPE_DECL
893      may be a dummy, with no DECL_ORIGINAL_TYPE.  Don't fault.  */
894   while (TYPE_NAME (t1)
895          && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
896          && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
897     t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
898
899   while (TYPE_NAME (t2)
900          && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
901          && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
902     t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
903
904   /* C90 didn't have the requirement that the two tags be the same.  */
905   if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
906     return 0;
907
908   /* C90 didn't say what happened if one or both of the types were
909      incomplete; we choose to follow C99 rules here, which is that they
910      are compatible.  */
911   if (TYPE_SIZE (t1) == NULL
912       || TYPE_SIZE (t2) == NULL)
913     return 1;
914
915   {
916     const struct tagged_tu_seen * tts_i;
917     for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
918       if (tts_i->t1 == t1 && tts_i->t2 == t2)
919         return 1;
920   }
921
922   switch (TREE_CODE (t1))
923     {
924     case ENUMERAL_TYPE:
925       {
926
927         /* Speed up the case where the type values are in the same order.  */
928         tree tv1 = TYPE_VALUES (t1);
929         tree tv2 = TYPE_VALUES (t2);
930
931         if (tv1 == tv2)
932           return 1;
933
934         for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
935           {
936             if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
937               break;
938             if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
939               return 0;
940           }
941
942         if (tv1 == NULL_TREE && tv2 == NULL_TREE)
943           return 1;
944         if (tv1 == NULL_TREE || tv2 == NULL_TREE)
945           return 0;
946
947         if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
948           return 0;
949
950         for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
951           {
952             s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
953             if (s2 == NULL
954                 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
955               return 0;
956           }
957         return 1;
958       }
959
960     case UNION_TYPE:
961       {
962         if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
963           return 0;
964
965         for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
966           {
967             bool ok = false;
968             struct tagged_tu_seen tts;
969
970             tts.next = tagged_tu_seen_base;
971             tts.t1 = t1;
972             tts.t2 = t2;
973             tagged_tu_seen_base = &tts;
974
975             if (DECL_NAME (s1) != NULL)
976               for (s2 = TYPE_FIELDS (t2); s2; s2 = TREE_CHAIN (s2))
977                 if (DECL_NAME (s1) == DECL_NAME (s2))
978                   {
979                     int result;
980                     result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2));
981                     if (result == 0)
982                       break;
983                     if (result == 2)
984                       needs_warning = true;
985
986                     if (TREE_CODE (s1) == FIELD_DECL
987                         && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
988                                              DECL_FIELD_BIT_OFFSET (s2)) != 1)
989                       break;
990
991                     ok = true;
992                     break;
993                   }
994             tagged_tu_seen_base = tts.next;
995             if (!ok)
996               return 0;
997           }
998         return needs_warning ? 2 : 1;
999       }
1000
1001     case RECORD_TYPE:
1002       {
1003         struct tagged_tu_seen tts;
1004
1005         tts.next = tagged_tu_seen_base;
1006         tts.t1 = t1;
1007         tts.t2 = t2;
1008         tagged_tu_seen_base = &tts;
1009
1010         for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1011              s1 && s2;
1012              s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
1013           {
1014             int result;
1015             if (TREE_CODE (s1) != TREE_CODE (s2)
1016                 || DECL_NAME (s1) != DECL_NAME (s2))
1017               break;
1018             result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2));
1019             if (result == 0)
1020               break;
1021             if (result == 2)
1022               needs_warning = true;
1023
1024             if (TREE_CODE (s1) == FIELD_DECL
1025                 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1026                                      DECL_FIELD_BIT_OFFSET (s2)) != 1)
1027               break;
1028           }
1029         tagged_tu_seen_base = tts.next;
1030         if (s1 && s2)
1031           return 0;
1032         return needs_warning ? 2 : 1;
1033       }
1034
1035     default:
1036       gcc_unreachable ();
1037     }
1038 }
1039
1040 /* Return 1 if two function types F1 and F2 are compatible.
1041    If either type specifies no argument types,
1042    the other must specify a fixed number of self-promoting arg types.
1043    Otherwise, if one type specifies only the number of arguments,
1044    the other must specify that number of self-promoting arg types.
1045    Otherwise, the argument types must match.  */
1046
1047 static int
1048 function_types_compatible_p (tree f1, tree f2)
1049 {
1050   tree args1, args2;
1051   /* 1 if no need for warning yet, 2 if warning cause has been seen.  */
1052   int val = 1;
1053   int val1;
1054   tree ret1, ret2;
1055
1056   ret1 = TREE_TYPE (f1);
1057   ret2 = TREE_TYPE (f2);
1058
1059   /* 'volatile' qualifiers on a function's return type used to mean
1060      the function is noreturn.  */
1061   if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1062     pedwarn ("function return types not compatible due to %<volatile%>");
1063   if (TYPE_VOLATILE (ret1))
1064     ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1065                                  TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1066   if (TYPE_VOLATILE (ret2))
1067     ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1068                                  TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1069   val = comptypes (ret1, ret2);
1070   if (val == 0)
1071     return 0;
1072
1073   args1 = TYPE_ARG_TYPES (f1);
1074   args2 = TYPE_ARG_TYPES (f2);
1075
1076   /* An unspecified parmlist matches any specified parmlist
1077      whose argument types don't need default promotions.  */
1078
1079   if (args1 == 0)
1080     {
1081       if (!self_promoting_args_p (args2))
1082         return 0;
1083       /* If one of these types comes from a non-prototype fn definition,
1084          compare that with the other type's arglist.
1085          If they don't match, ask for a warning (0, but no error).  */
1086       if (TYPE_ACTUAL_ARG_TYPES (f1)
1087           && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
1088         val = 2;
1089       return val;
1090     }
1091   if (args2 == 0)
1092     {
1093       if (!self_promoting_args_p (args1))
1094         return 0;
1095       if (TYPE_ACTUAL_ARG_TYPES (f2)
1096           && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
1097         val = 2;
1098       return val;
1099     }
1100
1101   /* Both types have argument lists: compare them and propagate results.  */
1102   val1 = type_lists_compatible_p (args1, args2);
1103   return val1 != 1 ? val1 : val;
1104 }
1105
1106 /* Check two lists of types for compatibility,
1107    returning 0 for incompatible, 1 for compatible,
1108    or 2 for compatible with warning.  */
1109
1110 static int
1111 type_lists_compatible_p (tree args1, tree args2)
1112 {
1113   /* 1 if no need for warning yet, 2 if warning cause has been seen.  */
1114   int val = 1;
1115   int newval = 0;
1116
1117   while (1)
1118     {
1119       tree a1, mv1, a2, mv2;
1120       if (args1 == 0 && args2 == 0)
1121         return val;
1122       /* If one list is shorter than the other,
1123          they fail to match.  */
1124       if (args1 == 0 || args2 == 0)
1125         return 0;
1126       mv1 = a1 = TREE_VALUE (args1);
1127       mv2 = a2 = TREE_VALUE (args2);
1128       if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1129         mv1 = TYPE_MAIN_VARIANT (mv1);
1130       if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1131         mv2 = TYPE_MAIN_VARIANT (mv2);
1132       /* A null pointer instead of a type
1133          means there is supposed to be an argument
1134          but nothing is specified about what type it has.
1135          So match anything that self-promotes.  */
1136       if (a1 == 0)
1137         {
1138           if (c_type_promotes_to (a2) != a2)
1139             return 0;
1140         }
1141       else if (a2 == 0)
1142         {
1143           if (c_type_promotes_to (a1) != a1)
1144             return 0;
1145         }
1146       /* If one of the lists has an error marker, ignore this arg.  */
1147       else if (TREE_CODE (a1) == ERROR_MARK
1148                || TREE_CODE (a2) == ERROR_MARK)
1149         ;
1150       else if (!(newval = comptypes (mv1, mv2)))
1151         {
1152           /* Allow  wait (union {union wait *u; int *i} *)
1153              and  wait (union wait *)  to be compatible.  */
1154           if (TREE_CODE (a1) == UNION_TYPE
1155               && (TYPE_NAME (a1) == 0
1156                   || TYPE_TRANSPARENT_UNION (a1))
1157               && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1158               && tree_int_cst_equal (TYPE_SIZE (a1),
1159                                      TYPE_SIZE (a2)))
1160             {
1161               tree memb;
1162               for (memb = TYPE_FIELDS (a1);
1163                    memb; memb = TREE_CHAIN (memb))
1164                 {
1165                   tree mv3 = TREE_TYPE (memb);
1166                   if (mv3 && mv3 != error_mark_node
1167                       && TREE_CODE (mv3) != ARRAY_TYPE)
1168                     mv3 = TYPE_MAIN_VARIANT (mv3);
1169                   if (comptypes (mv3, mv2))
1170                     break;
1171                 }
1172               if (memb == 0)
1173                 return 0;
1174             }
1175           else if (TREE_CODE (a2) == UNION_TYPE
1176                    && (TYPE_NAME (a2) == 0
1177                        || TYPE_TRANSPARENT_UNION (a2))
1178                    && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1179                    && tree_int_cst_equal (TYPE_SIZE (a2),
1180                                           TYPE_SIZE (a1)))
1181             {
1182               tree memb;
1183               for (memb = TYPE_FIELDS (a2);
1184                    memb; memb = TREE_CHAIN (memb))
1185                 {
1186                   tree mv3 = TREE_TYPE (memb);
1187                   if (mv3 && mv3 != error_mark_node
1188                       && TREE_CODE (mv3) != ARRAY_TYPE)
1189                     mv3 = TYPE_MAIN_VARIANT (mv3);
1190                   if (comptypes (mv3, mv1))
1191                     break;
1192                 }
1193               if (memb == 0)
1194                 return 0;
1195             }
1196           else
1197             return 0;
1198         }
1199
1200       /* comptypes said ok, but record if it said to warn.  */
1201       if (newval > val)
1202         val = newval;
1203
1204       args1 = TREE_CHAIN (args1);
1205       args2 = TREE_CHAIN (args2);
1206     }
1207 }
1208 \f
1209 /* Compute the size to increment a pointer by.  */
1210
1211 static tree
1212 c_size_in_bytes (tree type)
1213 {
1214   enum tree_code code = TREE_CODE (type);
1215
1216   if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1217     return size_one_node;
1218
1219   if (!COMPLETE_OR_VOID_TYPE_P (type))
1220     {
1221       error ("arithmetic on pointer to an incomplete type");
1222       return size_one_node;
1223     }
1224
1225   /* Convert in case a char is more than one unit.  */
1226   return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1227                      size_int (TYPE_PRECISION (char_type_node)
1228                                / BITS_PER_UNIT));
1229 }
1230 \f
1231 /* Return either DECL or its known constant value (if it has one).  */
1232
1233 tree
1234 decl_constant_value (tree decl)
1235 {
1236   if (/* Don't change a variable array bound or initial value to a constant
1237          in a place where a variable is invalid.  Note that DECL_INITIAL
1238          isn't valid for a PARM_DECL.  */
1239       current_function_decl != 0
1240       && TREE_CODE (decl) != PARM_DECL
1241       && !TREE_THIS_VOLATILE (decl)
1242       && TREE_READONLY (decl)
1243       && DECL_INITIAL (decl) != 0
1244       && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1245       /* This is invalid if initial value is not constant.
1246          If it has either a function call, a memory reference,
1247          or a variable, then re-evaluating it could give different results.  */
1248       && TREE_CONSTANT (DECL_INITIAL (decl))
1249       /* Check for cases where this is sub-optimal, even though valid.  */
1250       && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1251     return DECL_INITIAL (decl);
1252   return decl;
1253 }
1254
1255 /* Return either DECL or its known constant value (if it has one), but
1256    return DECL if pedantic or DECL has mode BLKmode.  This is for
1257    bug-compatibility with the old behavior of decl_constant_value
1258    (before GCC 3.0); every use of this function is a bug and it should
1259    be removed before GCC 3.1.  It is not appropriate to use pedantic
1260    in a way that affects optimization, and BLKmode is probably not the
1261    right test for avoiding misoptimizations either.  */
1262
1263 static tree
1264 decl_constant_value_for_broken_optimization (tree decl)
1265 {
1266   tree ret;
1267
1268   if (pedantic || DECL_MODE (decl) == BLKmode)
1269     return decl;
1270
1271   ret = decl_constant_value (decl);
1272   /* Avoid unwanted tree sharing between the initializer and current
1273      function's body where the tree can be modified e.g. by the
1274      gimplifier.  */
1275   if (ret != decl && TREE_STATIC (decl))
1276     ret = unshare_expr (ret);
1277   return ret;
1278 }
1279
1280
1281 /* Perform the default conversion of arrays and functions to pointers.
1282    Return the result of converting EXP.  For any other expression, just
1283    return EXP.  */
1284
1285 static tree
1286 default_function_array_conversion (tree exp)
1287 {
1288   tree orig_exp;
1289   tree type = TREE_TYPE (exp);
1290   enum tree_code code = TREE_CODE (type);
1291   int not_lvalue = 0;
1292
1293   /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1294      an lvalue.
1295
1296      Do not use STRIP_NOPS here!  It will remove conversions from pointer
1297      to integer and cause infinite recursion.  */
1298   orig_exp = exp;
1299   while (TREE_CODE (exp) == NON_LVALUE_EXPR
1300          || (TREE_CODE (exp) == NOP_EXPR
1301              && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1302     {
1303       if (TREE_CODE (exp) == NON_LVALUE_EXPR)
1304         not_lvalue = 1;
1305       exp = TREE_OPERAND (exp, 0);
1306     }
1307
1308   if (TREE_NO_WARNING (orig_exp))
1309     TREE_NO_WARNING (exp) = 1;
1310
1311   if (code == FUNCTION_TYPE)
1312     {
1313       return build_unary_op (ADDR_EXPR, exp, 0);
1314     }
1315   if (code == ARRAY_TYPE)
1316     {
1317       tree adr;
1318       tree restype = TREE_TYPE (type);
1319       tree ptrtype;
1320       int constp = 0;
1321       int volatilep = 0;
1322       int lvalue_array_p;
1323
1324       if (REFERENCE_CLASS_P (exp) || DECL_P (exp))
1325         {
1326           constp = TREE_READONLY (exp);
1327           volatilep = TREE_THIS_VOLATILE (exp);
1328         }
1329
1330       if (TYPE_QUALS (type) || constp || volatilep)
1331         restype
1332           = c_build_qualified_type (restype,
1333                                     TYPE_QUALS (type)
1334                                     | (constp * TYPE_QUAL_CONST)
1335                                     | (volatilep * TYPE_QUAL_VOLATILE));
1336
1337       if (TREE_CODE (exp) == INDIRECT_REF)
1338         return convert (build_pointer_type (restype),
1339                         TREE_OPERAND (exp, 0));
1340
1341       if (TREE_CODE (exp) == COMPOUND_EXPR)
1342         {
1343           tree op1 = default_conversion (TREE_OPERAND (exp, 1));
1344           return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
1345                          TREE_OPERAND (exp, 0), op1);
1346         }
1347
1348       lvalue_array_p = !not_lvalue && lvalue_p (exp);
1349       if (!flag_isoc99 && !lvalue_array_p)
1350         {
1351           /* Before C99, non-lvalue arrays do not decay to pointers.
1352              Normally, using such an array would be invalid; but it can
1353              be used correctly inside sizeof or as a statement expression.
1354              Thus, do not give an error here; an error will result later.  */
1355           return exp;
1356         }
1357
1358       ptrtype = build_pointer_type (restype);
1359
1360       if (TREE_CODE (exp) == VAR_DECL)
1361         {
1362           /* We are making an ADDR_EXPR of ptrtype.  This is a valid
1363              ADDR_EXPR because it's the best way of representing what
1364              happens in C when we take the address of an array and place
1365              it in a pointer to the element type.  */
1366           adr = build1 (ADDR_EXPR, ptrtype, exp);
1367           if (!c_mark_addressable (exp))
1368             return error_mark_node;
1369           TREE_SIDE_EFFECTS (adr) = 0;   /* Default would be, same as EXP.  */
1370           return adr;
1371         }
1372       /* This way is better for a COMPONENT_REF since it can
1373          simplify the offset for a component.  */
1374       adr = build_unary_op (ADDR_EXPR, exp, 1);
1375       return convert (ptrtype, adr);
1376     }
1377   return exp;
1378 }
1379
1380
1381 /* EXP is an expression of integer type.  Apply the integer promotions
1382    to it and return the promoted value.  */
1383
1384 tree
1385 perform_integral_promotions (tree exp)
1386 {
1387   tree type = TREE_TYPE (exp);
1388   enum tree_code code = TREE_CODE (type);
1389
1390   gcc_assert (INTEGRAL_TYPE_P (type));
1391
1392   /* Normally convert enums to int,
1393      but convert wide enums to something wider.  */
1394   if (code == ENUMERAL_TYPE)
1395     {
1396       type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1397                                           TYPE_PRECISION (integer_type_node)),
1398                                      ((TYPE_PRECISION (type)
1399                                        >= TYPE_PRECISION (integer_type_node))
1400                                       && TYPE_UNSIGNED (type)));
1401
1402       return convert (type, exp);
1403     }
1404
1405   /* ??? This should no longer be needed now bit-fields have their
1406      proper types.  */
1407   if (TREE_CODE (exp) == COMPONENT_REF
1408       && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1409       /* If it's thinner than an int, promote it like a
1410          c_promoting_integer_type_p, otherwise leave it alone.  */
1411       && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1412                                TYPE_PRECISION (integer_type_node)))
1413     return convert (integer_type_node, exp);
1414
1415   if (c_promoting_integer_type_p (type))
1416     {
1417       /* Preserve unsignedness if not really getting any wider.  */
1418       if (TYPE_UNSIGNED (type)
1419           && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1420         return convert (unsigned_type_node, exp);
1421
1422       return convert (integer_type_node, exp);
1423     }
1424
1425   return exp;
1426 }
1427
1428
1429 /* Perform default promotions for C data used in expressions.
1430    Arrays and functions are converted to pointers;
1431    enumeral types or short or char, to int.
1432    In addition, manifest constants symbols are replaced by their values.  */
1433
1434 tree
1435 default_conversion (tree exp)
1436 {
1437   tree orig_exp;
1438   tree type = TREE_TYPE (exp);
1439   enum tree_code code = TREE_CODE (type);
1440
1441   if (code == FUNCTION_TYPE || code == ARRAY_TYPE)
1442     return default_function_array_conversion (exp);
1443
1444   /* Constants can be used directly unless they're not loadable.  */
1445   if (TREE_CODE (exp) == CONST_DECL)
1446     exp = DECL_INITIAL (exp);
1447
1448   /* Replace a nonvolatile const static variable with its value unless
1449      it is an array, in which case we must be sure that taking the
1450      address of the array produces consistent results.  */
1451   else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
1452     {
1453       exp = decl_constant_value_for_broken_optimization (exp);
1454       type = TREE_TYPE (exp);
1455     }
1456
1457   /* Strip no-op conversions.  */
1458   orig_exp = exp;
1459   STRIP_TYPE_NOPS (exp);
1460
1461   if (TREE_NO_WARNING (orig_exp))
1462     TREE_NO_WARNING (exp) = 1;
1463
1464   if (INTEGRAL_TYPE_P (type))
1465     return perform_integral_promotions (exp);
1466
1467   if (code == VOID_TYPE)
1468     {
1469       error ("void value not ignored as it ought to be");
1470       return error_mark_node;
1471     }
1472   return exp;
1473 }
1474 \f
1475 /* Look up COMPONENT in a structure or union DECL.
1476
1477    If the component name is not found, returns NULL_TREE.  Otherwise,
1478    the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1479    stepping down the chain to the component, which is in the last
1480    TREE_VALUE of the list.  Normally the list is of length one, but if
1481    the component is embedded within (nested) anonymous structures or
1482    unions, the list steps down the chain to the component.  */
1483
1484 static tree
1485 lookup_field (tree decl, tree component)
1486 {
1487   tree type = TREE_TYPE (decl);
1488   tree field;
1489
1490   /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1491      to the field elements.  Use a binary search on this array to quickly
1492      find the element.  Otherwise, do a linear search.  TYPE_LANG_SPECIFIC
1493      will always be set for structures which have many elements.  */
1494
1495   if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
1496     {
1497       int bot, top, half;
1498       tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
1499
1500       field = TYPE_FIELDS (type);
1501       bot = 0;
1502       top = TYPE_LANG_SPECIFIC (type)->s->len;
1503       while (top - bot > 1)
1504         {
1505           half = (top - bot + 1) >> 1;
1506           field = field_array[bot+half];
1507
1508           if (DECL_NAME (field) == NULL_TREE)
1509             {
1510               /* Step through all anon unions in linear fashion.  */
1511               while (DECL_NAME (field_array[bot]) == NULL_TREE)
1512                 {
1513                   field = field_array[bot++];
1514                   if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1515                       || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1516                     {
1517                       tree anon = lookup_field (field, component);
1518
1519                       if (anon)
1520                         return tree_cons (NULL_TREE, field, anon);
1521                     }
1522                 }
1523
1524               /* Entire record is only anon unions.  */
1525               if (bot > top)
1526                 return NULL_TREE;
1527
1528               /* Restart the binary search, with new lower bound.  */
1529               continue;
1530             }
1531
1532           if (DECL_NAME (field) == component)
1533             break;
1534           if (DECL_NAME (field) < component)
1535             bot += half;
1536           else
1537             top = bot + half;
1538         }
1539
1540       if (DECL_NAME (field_array[bot]) == component)
1541         field = field_array[bot];
1542       else if (DECL_NAME (field) != component)
1543         return NULL_TREE;
1544     }
1545   else
1546     {
1547       for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1548         {
1549           if (DECL_NAME (field) == NULL_TREE
1550               && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1551                   || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1552             {
1553               tree anon = lookup_field (field, component);
1554
1555               if (anon)
1556                 return tree_cons (NULL_TREE, field, anon);
1557             }
1558
1559           if (DECL_NAME (field) == component)
1560             break;
1561         }
1562
1563       if (field == NULL_TREE)
1564         return NULL_TREE;
1565     }
1566
1567   return tree_cons (NULL_TREE, field, NULL_TREE);
1568 }
1569
1570 /* Make an expression to refer to the COMPONENT field of
1571    structure or union value DATUM.  COMPONENT is an IDENTIFIER_NODE.  */
1572
1573 tree
1574 build_component_ref (tree datum, tree component)
1575 {
1576   tree type = TREE_TYPE (datum);
1577   enum tree_code code = TREE_CODE (type);
1578   tree field = NULL;
1579   tree ref;
1580
1581   if (!objc_is_public (datum, component))
1582     return error_mark_node;
1583
1584   /* See if there is a field or component with name COMPONENT.  */
1585
1586   if (code == RECORD_TYPE || code == UNION_TYPE)
1587     {
1588       if (!COMPLETE_TYPE_P (type))
1589         {
1590           c_incomplete_type_error (NULL_TREE, type);
1591           return error_mark_node;
1592         }
1593
1594       field = lookup_field (datum, component);
1595
1596       if (!field)
1597         {
1598           error ("%qT has no member named %qE", type, component);
1599           return error_mark_node;
1600         }
1601
1602       /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1603          This might be better solved in future the way the C++ front
1604          end does it - by giving the anonymous entities each a
1605          separate name and type, and then have build_component_ref
1606          recursively call itself.  We can't do that here.  */
1607       do
1608         {
1609           tree subdatum = TREE_VALUE (field);
1610
1611           if (TREE_TYPE (subdatum) == error_mark_node)
1612             return error_mark_node;
1613
1614           ref = build3 (COMPONENT_REF, TREE_TYPE (subdatum), datum, subdatum,
1615                         NULL_TREE);
1616           if (TREE_READONLY (datum) || TREE_READONLY (subdatum))
1617             TREE_READONLY (ref) = 1;
1618           if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum))
1619             TREE_THIS_VOLATILE (ref) = 1;
1620
1621           if (TREE_DEPRECATED (subdatum))
1622             warn_deprecated_use (subdatum);
1623
1624           datum = ref;
1625
1626           field = TREE_CHAIN (field);
1627         }
1628       while (field);
1629
1630       return ref;
1631     }
1632   else if (code != ERROR_MARK)
1633     error ("request for member %qE in something not a structure or union",
1634            component);
1635
1636   return error_mark_node;
1637 }
1638 \f
1639 /* Given an expression PTR for a pointer, return an expression
1640    for the value pointed to.
1641    ERRORSTRING is the name of the operator to appear in error messages.  */
1642
1643 tree
1644 build_indirect_ref (tree ptr, const char *errorstring)
1645 {
1646   tree pointer = default_conversion (ptr);
1647   tree type = TREE_TYPE (pointer);
1648
1649   if (TREE_CODE (type) == POINTER_TYPE)
1650     {
1651       if (TREE_CODE (pointer) == ADDR_EXPR
1652           && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1653               == TREE_TYPE (type)))
1654         return TREE_OPERAND (pointer, 0);
1655       else
1656         {
1657           tree t = TREE_TYPE (type);
1658           tree mvt = t;
1659           tree ref;
1660
1661           if (TREE_CODE (mvt) != ARRAY_TYPE)
1662             mvt = TYPE_MAIN_VARIANT (mvt);
1663           ref = build1 (INDIRECT_REF, mvt, pointer);
1664
1665           if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
1666             {
1667               error ("dereferencing pointer to incomplete type");
1668               return error_mark_node;
1669             }
1670           if (VOID_TYPE_P (t) && skip_evaluation == 0)
1671             warning (0, "dereferencing %<void *%> pointer");
1672
1673           /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1674              so that we get the proper error message if the result is used
1675              to assign to.  Also, &* is supposed to be a no-op.
1676              And ANSI C seems to specify that the type of the result
1677              should be the const type.  */
1678           /* A de-reference of a pointer to const is not a const.  It is valid
1679              to change it via some other pointer.  */
1680           TREE_READONLY (ref) = TYPE_READONLY (t);
1681           TREE_SIDE_EFFECTS (ref)
1682             = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
1683           TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1684           return ref;
1685         }
1686     }
1687   else if (TREE_CODE (pointer) != ERROR_MARK)
1688     error ("invalid type argument of %qs", errorstring);
1689   return error_mark_node;
1690 }
1691
1692 /* This handles expressions of the form "a[i]", which denotes
1693    an array reference.
1694
1695    This is logically equivalent in C to *(a+i), but we may do it differently.
1696    If A is a variable or a member, we generate a primitive ARRAY_REF.
1697    This avoids forcing the array out of registers, and can work on
1698    arrays that are not lvalues (for example, members of structures returned
1699    by functions).  */
1700
1701 tree
1702 build_array_ref (tree array, tree index)
1703 {
1704   bool swapped = false;
1705   if (TREE_TYPE (array) == error_mark_node
1706       || TREE_TYPE (index) == error_mark_node)
1707     return error_mark_node;
1708
1709   if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
1710       && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE)
1711     {
1712       tree temp;
1713       if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
1714           && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
1715         {
1716           error ("subscripted value is neither array nor pointer");
1717           return error_mark_node;
1718         }
1719       temp = array;
1720       array = index;
1721       index = temp;
1722       swapped = true;
1723     }
1724
1725   if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
1726     {
1727       error ("array subscript is not an integer");
1728       return error_mark_node;
1729     }
1730
1731   if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
1732     {
1733       error ("subscripted value is pointer to function");
1734       return error_mark_node;
1735     }
1736
1737   /* Subscripting with type char is likely to lose on a machine where
1738      chars are signed.  So warn on any machine, but optionally.  Don't
1739      warn for unsigned char since that type is safe.  Don't warn for
1740      signed char because anyone who uses that must have done so
1741      deliberately.  ??? Existing practice has also been to warn only
1742      when the char index is syntactically the index, not for
1743      char[array].  */
1744   if (warn_char_subscripts && !swapped
1745       && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1746     warning (0, "array subscript has type %<char%>");
1747
1748   /* Apply default promotions *after* noticing character types.  */
1749   index = default_conversion (index);
1750
1751   gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
1752
1753   if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
1754     {
1755       tree rval, type;
1756
1757       /* An array that is indexed by a non-constant
1758          cannot be stored in a register; we must be able to do
1759          address arithmetic on its address.
1760          Likewise an array of elements of variable size.  */
1761       if (TREE_CODE (index) != INTEGER_CST
1762           || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
1763               && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1764         {
1765           if (!c_mark_addressable (array))
1766             return error_mark_node;
1767         }
1768       /* An array that is indexed by a constant value which is not within
1769          the array bounds cannot be stored in a register either; because we
1770          would get a crash in store_bit_field/extract_bit_field when trying
1771          to access a non-existent part of the register.  */
1772       if (TREE_CODE (index) == INTEGER_CST
1773           && TYPE_DOMAIN (TREE_TYPE (array))
1774           && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
1775         {
1776           if (!c_mark_addressable (array))
1777             return error_mark_node;
1778         }
1779
1780       if (pedantic)
1781         {
1782           tree foo = array;
1783           while (TREE_CODE (foo) == COMPONENT_REF)
1784             foo = TREE_OPERAND (foo, 0);
1785           if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
1786             pedwarn ("ISO C forbids subscripting %<register%> array");
1787           else if (!flag_isoc99 && !lvalue_p (foo))
1788             pedwarn ("ISO C90 forbids subscripting non-lvalue array");
1789         }
1790
1791       type = TREE_TYPE (TREE_TYPE (array));
1792       if (TREE_CODE (type) != ARRAY_TYPE)
1793         type = TYPE_MAIN_VARIANT (type);
1794       rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
1795       /* Array ref is const/volatile if the array elements are
1796          or if the array is.  */
1797       TREE_READONLY (rval)
1798         |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1799             | TREE_READONLY (array));
1800       TREE_SIDE_EFFECTS (rval)
1801         |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1802             | TREE_SIDE_EFFECTS (array));
1803       TREE_THIS_VOLATILE (rval)
1804         |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1805             /* This was added by rms on 16 Nov 91.
1806                It fixes  vol struct foo *a;  a->elts[1]
1807                in an inline function.
1808                Hope it doesn't break something else.  */
1809             | TREE_THIS_VOLATILE (array));
1810       return require_complete_type (fold (rval));
1811     }
1812   else
1813     {
1814       tree ar = default_conversion (array);
1815
1816       if (ar == error_mark_node)
1817         return ar;
1818
1819       gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
1820       gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
1821
1822       return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, index, 0),
1823                                  "array indexing");
1824     }
1825 }
1826 \f
1827 /* Build an external reference to identifier ID.  FUN indicates
1828    whether this will be used for a function call.  LOC is the source
1829    location of the identifier.  */
1830 tree
1831 build_external_ref (tree id, int fun, location_t loc)
1832 {
1833   tree ref;
1834   tree decl = lookup_name (id);
1835
1836   /* In Objective-C, an instance variable (ivar) may be preferred to
1837      whatever lookup_name() found.  */
1838   decl = objc_lookup_ivar (decl, id);
1839
1840   if (decl && decl != error_mark_node)
1841     ref = decl;
1842   else if (fun)
1843     /* Implicit function declaration.  */
1844     ref = implicitly_declare (id);
1845   else if (decl == error_mark_node)
1846     /* Don't complain about something that's already been
1847        complained about.  */
1848     return error_mark_node;
1849   else
1850     {
1851       undeclared_variable (id, loc);
1852       return error_mark_node;
1853     }
1854
1855   if (TREE_TYPE (ref) == error_mark_node)
1856     return error_mark_node;
1857
1858   if (TREE_DEPRECATED (ref))
1859     warn_deprecated_use (ref);
1860
1861   if (!skip_evaluation)
1862     assemble_external (ref);
1863   TREE_USED (ref) = 1;
1864
1865   if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
1866     {
1867       if (!in_sizeof && !in_typeof)
1868         C_DECL_USED (ref) = 1;
1869       else if (DECL_INITIAL (ref) == 0
1870                && DECL_EXTERNAL (ref)
1871                && !TREE_PUBLIC (ref))
1872         record_maybe_used_decl (ref);
1873     }
1874
1875   if (TREE_CODE (ref) == CONST_DECL)
1876     {
1877       ref = DECL_INITIAL (ref);
1878       TREE_CONSTANT (ref) = 1;
1879       TREE_INVARIANT (ref) = 1;
1880     }
1881   else if (current_function_decl != 0
1882            && !DECL_FILE_SCOPE_P (current_function_decl)
1883            && (TREE_CODE (ref) == VAR_DECL
1884                || TREE_CODE (ref) == PARM_DECL
1885                || TREE_CODE (ref) == FUNCTION_DECL))
1886     {
1887       tree context = decl_function_context (ref);
1888
1889       if (context != 0 && context != current_function_decl)
1890         DECL_NONLOCAL (ref) = 1;
1891     }
1892
1893   return ref;
1894 }
1895
1896 /* Record details of decls possibly used inside sizeof or typeof.  */
1897 struct maybe_used_decl
1898 {
1899   /* The decl.  */
1900   tree decl;
1901   /* The level seen at (in_sizeof + in_typeof).  */
1902   int level;
1903   /* The next one at this level or above, or NULL.  */
1904   struct maybe_used_decl *next;
1905 };
1906
1907 static struct maybe_used_decl *maybe_used_decls;
1908
1909 /* Record that DECL, an undefined static function reference seen
1910    inside sizeof or typeof, might be used if the operand of sizeof is
1911    a VLA type or the operand of typeof is a variably modified
1912    type.  */
1913
1914 static void
1915 record_maybe_used_decl (tree decl)
1916 {
1917   struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
1918   t->decl = decl;
1919   t->level = in_sizeof + in_typeof;
1920   t->next = maybe_used_decls;
1921   maybe_used_decls = t;
1922 }
1923
1924 /* Pop the stack of decls possibly used inside sizeof or typeof.  If
1925    USED is false, just discard them.  If it is true, mark them used
1926    (if no longer inside sizeof or typeof) or move them to the next
1927    level up (if still inside sizeof or typeof).  */
1928
1929 void
1930 pop_maybe_used (bool used)
1931 {
1932   struct maybe_used_decl *p = maybe_used_decls;
1933   int cur_level = in_sizeof + in_typeof;
1934   while (p && p->level > cur_level)
1935     {
1936       if (used)
1937         {
1938           if (cur_level == 0)
1939             C_DECL_USED (p->decl) = 1;
1940           else
1941             p->level = cur_level;
1942         }
1943       p = p->next;
1944     }
1945   if (!used || cur_level == 0)
1946     maybe_used_decls = p;
1947 }
1948
1949 /* Return the result of sizeof applied to EXPR.  */
1950
1951 struct c_expr
1952 c_expr_sizeof_expr (struct c_expr expr)
1953 {
1954   struct c_expr ret;
1955   if (expr.value == error_mark_node)
1956     {
1957       ret.value = error_mark_node;
1958       ret.original_code = ERROR_MARK;
1959       pop_maybe_used (false);
1960     }
1961   else
1962     {
1963       ret.value = c_sizeof (TREE_TYPE (expr.value));
1964       ret.original_code = ERROR_MARK;
1965       pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (expr.value)));
1966     }
1967   return ret;
1968 }
1969
1970 /* Return the result of sizeof applied to T, a structure for the type
1971    name passed to sizeof (rather than the type itself).  */
1972
1973 struct c_expr
1974 c_expr_sizeof_type (struct c_type_name *t)
1975 {
1976   tree type;
1977   struct c_expr ret;
1978   type = groktypename (t);
1979   ret.value = c_sizeof (type);
1980   ret.original_code = ERROR_MARK;
1981   pop_maybe_used (C_TYPE_VARIABLE_SIZE (type));
1982   return ret;
1983 }
1984
1985 /* Build a function call to function FUNCTION with parameters PARAMS.
1986    PARAMS is a list--a chain of TREE_LIST nodes--in which the
1987    TREE_VALUE of each node is a parameter-expression.
1988    FUNCTION's data type may be a function type or a pointer-to-function.  */
1989
1990 tree
1991 build_function_call (tree function, tree params)
1992 {
1993   tree fntype, fundecl = 0;
1994   tree coerced_params;
1995   tree name = NULL_TREE, result;
1996   tree tem;
1997
1998   /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue.  */
1999   STRIP_TYPE_NOPS (function);
2000
2001   /* Convert anything with function type to a pointer-to-function.  */
2002   if (TREE_CODE (function) == FUNCTION_DECL)
2003     {
2004       /* Implement type-directed function overloading for builtins.
2005          resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2006          handle all the type checking.  The result is a complete expression
2007          that implements this function call.  */
2008       tem = resolve_overloaded_builtin (function, params);
2009       if (tem)
2010         return tem;
2011
2012       name = DECL_NAME (function);
2013
2014       /* Differs from default_conversion by not setting TREE_ADDRESSABLE
2015          (because calling an inline function does not mean the function
2016          needs to be separately compiled).  */
2017       fntype = build_type_variant (TREE_TYPE (function),
2018                                    TREE_READONLY (function),
2019                                    TREE_THIS_VOLATILE (function));
2020       fundecl = function;
2021       function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
2022     }
2023   else
2024     function = default_conversion (function);
2025
2026   fntype = TREE_TYPE (function);
2027
2028   if (TREE_CODE (fntype) == ERROR_MARK)
2029     return error_mark_node;
2030
2031   if (!(TREE_CODE (fntype) == POINTER_TYPE
2032         && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2033     {
2034       error ("called object %qE is not a function", function);
2035       return error_mark_node;
2036     }
2037
2038   if (fundecl && TREE_THIS_VOLATILE (fundecl))
2039     current_function_returns_abnormally = 1;
2040
2041   /* fntype now gets the type of function pointed to.  */
2042   fntype = TREE_TYPE (fntype);
2043
2044   /* Check that the function is called through a compatible prototype.
2045      If it is not, replace the call by a trap, wrapped up in a compound
2046      expression if necessary.  This has the nice side-effect to prevent
2047      the tree-inliner from generating invalid assignment trees which may
2048      blow up in the RTL expander later.
2049
2050      ??? This doesn't work for Objective-C because objc_comptypes
2051      refuses to compare function prototypes, yet the compiler appears
2052      to build calls that are flagged as invalid by C's comptypes.  */
2053   if (!c_dialect_objc ()
2054       && TREE_CODE (function) == NOP_EXPR
2055       && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2056       && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2057       && !comptypes (fntype, TREE_TYPE (tem)))
2058     {
2059       tree return_type = TREE_TYPE (fntype);
2060       tree trap = build_function_call (built_in_decls[BUILT_IN_TRAP],
2061                                        NULL_TREE);
2062
2063       /* This situation leads to run-time undefined behavior.  We can't,
2064          therefore, simply error unless we can prove that all possible
2065          executions of the program must execute the code.  */
2066       warning (0, "function called through a non-compatible type");
2067
2068       /* We can, however, treat "undefined" any way we please.
2069          Call abort to encourage the user to fix the program.  */
2070       inform ("if this code is reached, the program will abort");
2071
2072       if (VOID_TYPE_P (return_type))
2073         return trap;
2074       else
2075         {
2076           tree rhs;
2077
2078           if (AGGREGATE_TYPE_P (return_type))
2079             rhs = build_compound_literal (return_type,
2080                                           build_constructor (return_type,
2081                                                              NULL_TREE));
2082           else
2083             rhs = fold (build1 (NOP_EXPR, return_type, integer_zero_node));
2084
2085           return build2 (COMPOUND_EXPR, return_type, trap, rhs);
2086         }
2087     }
2088
2089   /* Convert the parameters to the types declared in the
2090      function prototype, or apply default promotions.  */
2091
2092   coerced_params
2093     = convert_arguments (TYPE_ARG_TYPES (fntype), params, function, fundecl);
2094
2095   if (coerced_params == error_mark_node)
2096     return error_mark_node;
2097
2098   /* Check that the arguments to the function are valid.  */
2099
2100   check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params);
2101
2102   result = build3 (CALL_EXPR, TREE_TYPE (fntype),
2103                    function, coerced_params, NULL_TREE);
2104   TREE_SIDE_EFFECTS (result) = 1;
2105
2106   if (require_constant_value)
2107     {
2108       result = fold_initializer (result);
2109
2110       if (TREE_CONSTANT (result)
2111           && (name == NULL_TREE
2112               || strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10) != 0))
2113         pedwarn_init ("initializer element is not constant");
2114     }
2115   else
2116     result = fold (result);
2117
2118   if (VOID_TYPE_P (TREE_TYPE (result)))
2119     return result;
2120   return require_complete_type (result);
2121 }
2122 \f
2123 /* Convert the argument expressions in the list VALUES
2124    to the types in the list TYPELIST.  The result is a list of converted
2125    argument expressions, unless there are too few arguments in which
2126    case it is error_mark_node.
2127
2128    If TYPELIST is exhausted, or when an element has NULL as its type,
2129    perform the default conversions.
2130
2131    PARMLIST is the chain of parm decls for the function being called.
2132    It may be 0, if that info is not available.
2133    It is used only for generating error messages.
2134
2135    FUNCTION is a tree for the called function.  It is used only for
2136    error messages, where it is formatted with %qE.
2137
2138    This is also where warnings about wrong number of args are generated.
2139
2140    Both VALUES and the returned value are chains of TREE_LIST nodes
2141    with the elements of the list in the TREE_VALUE slots of those nodes.  */
2142
2143 static tree
2144 convert_arguments (tree typelist, tree values, tree function, tree fundecl)
2145 {
2146   tree typetail, valtail;
2147   tree result = NULL;
2148   int parmnum;
2149   tree selector;
2150
2151   /* Change pointer to function to the function itself for
2152      diagnostics.  */
2153   if (TREE_CODE (function) == ADDR_EXPR
2154       && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2155     function = TREE_OPERAND (function, 0);
2156
2157   /* Handle an ObjC selector specially for diagnostics.  */
2158   selector = objc_message_selector ();
2159
2160   /* Scan the given expressions and types, producing individual
2161      converted arguments and pushing them on RESULT in reverse order.  */
2162
2163   for (valtail = values, typetail = typelist, parmnum = 0;
2164        valtail;
2165        valtail = TREE_CHAIN (valtail), parmnum++)
2166     {
2167       tree type = typetail ? TREE_VALUE (typetail) : 0;
2168       tree val = TREE_VALUE (valtail);
2169       tree rname = function;
2170       int argnum = parmnum + 1;
2171       const char *invalid_func_diag;
2172
2173       if (type == void_type_node)
2174         {
2175           error ("too many arguments to function %qE", function);
2176           break;
2177         }
2178
2179       if (selector && argnum > 2)
2180         {
2181           rname = selector;
2182           argnum -= 2;
2183         }
2184
2185       STRIP_TYPE_NOPS (val);
2186
2187       val = default_function_array_conversion (val);
2188
2189       val = require_complete_type (val);
2190
2191       if (type != 0)
2192         {
2193           /* Formal parm type is specified by a function prototype.  */
2194           tree parmval;
2195
2196           if (type == error_mark_node || !COMPLETE_TYPE_P (type))
2197             {
2198               error ("type of formal parameter %d is incomplete", parmnum + 1);
2199               parmval = val;
2200             }
2201           else
2202             {
2203               /* Optionally warn about conversions that
2204                  differ from the default conversions.  */
2205               if (warn_conversion || warn_traditional)
2206                 {
2207                   unsigned int formal_prec = TYPE_PRECISION (type);
2208
2209                   if (INTEGRAL_TYPE_P (type)
2210                       && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2211                     warning (0, "passing argument %d of %qE as integer "
2212                              "rather than floating due to prototype",
2213                              argnum, rname);
2214                   if (INTEGRAL_TYPE_P (type)
2215                       && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
2216                     warning (0, "passing argument %d of %qE as integer "
2217                              "rather than complex due to prototype",
2218                              argnum, rname);
2219                   else if (TREE_CODE (type) == COMPLEX_TYPE
2220                            && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2221                     warning (0, "passing argument %d of %qE as complex "
2222                              "rather than floating due to prototype",
2223                              argnum, rname);
2224                   else if (TREE_CODE (type) == REAL_TYPE
2225                            && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2226                     warning (0, "passing argument %d of %qE as floating "
2227                              "rather than integer due to prototype",
2228                              argnum, rname);
2229                   else if (TREE_CODE (type) == COMPLEX_TYPE
2230                            && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2231                     warning (0, "passing argument %d of %qE as complex "
2232                              "rather than integer due to prototype",
2233                              argnum, rname);
2234                   else if (TREE_CODE (type) == REAL_TYPE
2235                            && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
2236                     warning (0, "passing argument %d of %qE as floating "
2237                              "rather than complex due to prototype",
2238                              argnum, rname);
2239                   /* ??? At some point, messages should be written about
2240                      conversions between complex types, but that's too messy
2241                      to do now.  */
2242                   else if (TREE_CODE (type) == REAL_TYPE
2243                            && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2244                     {
2245                       /* Warn if any argument is passed as `float',
2246                          since without a prototype it would be `double'.  */
2247                       if (formal_prec == TYPE_PRECISION (float_type_node))
2248                         warning (0, "passing argument %d of %qE as %<float%> "
2249                                  "rather than %<double%> due to prototype",
2250                                  argnum, rname);
2251                     }
2252                   /* Detect integer changing in width or signedness.
2253                      These warnings are only activated with
2254                      -Wconversion, not with -Wtraditional.  */
2255                   else if (warn_conversion && INTEGRAL_TYPE_P (type)
2256                            && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2257                     {
2258                       tree would_have_been = default_conversion (val);
2259                       tree type1 = TREE_TYPE (would_have_been);
2260
2261                       if (TREE_CODE (type) == ENUMERAL_TYPE
2262                           && (TYPE_MAIN_VARIANT (type)
2263                               == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
2264                         /* No warning if function asks for enum
2265                            and the actual arg is that enum type.  */
2266                         ;
2267                       else if (formal_prec != TYPE_PRECISION (type1))
2268                         warning (0, "passing argument %d of %qE with different "
2269                                  "width due to prototype", argnum, rname);
2270                       else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
2271                         ;
2272                       /* Don't complain if the formal parameter type
2273                          is an enum, because we can't tell now whether
2274                          the value was an enum--even the same enum.  */
2275                       else if (TREE_CODE (type) == ENUMERAL_TYPE)
2276                         ;
2277                       else if (TREE_CODE (val) == INTEGER_CST
2278                                && int_fits_type_p (val, type))
2279                         /* Change in signedness doesn't matter
2280                            if a constant value is unaffected.  */
2281                         ;
2282                       /* If the value is extended from a narrower
2283                          unsigned type, it doesn't matter whether we
2284                          pass it as signed or unsigned; the value
2285                          certainly is the same either way.  */
2286                       else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
2287                                && TYPE_UNSIGNED (TREE_TYPE (val)))
2288                         ;
2289                       else if (TYPE_UNSIGNED (type))
2290                         warning (0, "passing argument %d of %qE as unsigned "
2291                                  "due to prototype", argnum, rname);
2292                       else
2293                         warning (0, "passing argument %d of %qE as signed "
2294                                  "due to prototype", argnum, rname);
2295                     }
2296                 }
2297
2298               parmval = convert_for_assignment (type, val, ic_argpass,
2299                                                 fundecl, function,
2300                                                 parmnum + 1);
2301
2302               if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
2303                   && INTEGRAL_TYPE_P (type)
2304                   && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2305                 parmval = default_conversion (parmval);
2306             }
2307           result = tree_cons (NULL_TREE, parmval, result);
2308         }
2309       else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
2310                && (TYPE_PRECISION (TREE_TYPE (val))
2311                    < TYPE_PRECISION (double_type_node)))
2312         /* Convert `float' to `double'.  */
2313         result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
2314       else if ((invalid_func_diag = 
2315                 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
2316         {
2317           error (invalid_func_diag);
2318           return error_mark_node; 
2319         }
2320       else
2321         /* Convert `short' and `char' to full-size `int'.  */
2322         result = tree_cons (NULL_TREE, default_conversion (val), result);
2323
2324       if (typetail)
2325         typetail = TREE_CHAIN (typetail);
2326     }
2327
2328   if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
2329     {
2330       error ("too few arguments to function %qE", function);
2331       return error_mark_node;
2332     }
2333
2334   return nreverse (result);
2335 }
2336 \f
2337 /* This is the entry point used by the parser to build unary operators
2338    in the input.  CODE, a tree_code, specifies the unary operator, and
2339    ARG is the operand.  For unary plus, the C parser currently uses
2340    CONVERT_EXPR for code.  */
2341
2342 struct c_expr
2343 parser_build_unary_op (enum tree_code code, struct c_expr arg)
2344 {
2345   struct c_expr result;
2346
2347   result.original_code = ERROR_MARK;
2348   result.value = build_unary_op (code, arg.value, 0);
2349   overflow_warning (result.value);
2350   return result;
2351 }
2352
2353 /* This is the entry point used by the parser to build binary operators
2354    in the input.  CODE, a tree_code, specifies the binary operator, and
2355    ARG1 and ARG2 are the operands.  In addition to constructing the
2356    expression, we check for operands that were written with other binary
2357    operators in a way that is likely to confuse the user.  */
2358
2359 struct c_expr
2360 parser_build_binary_op (enum tree_code code, struct c_expr arg1,
2361                         struct c_expr arg2)
2362 {
2363   struct c_expr result;
2364
2365   enum tree_code code1 = arg1.original_code;
2366   enum tree_code code2 = arg2.original_code;
2367
2368   result.value = build_binary_op (code, arg1.value, arg2.value, 1);
2369   result.original_code = code;
2370
2371   if (TREE_CODE (result.value) == ERROR_MARK)
2372     return result;
2373
2374   /* Check for cases such as x+y<<z which users are likely
2375      to misinterpret.  */
2376   if (warn_parentheses)
2377     {
2378       if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
2379         {
2380           if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2381               || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2382             warning (0, "suggest parentheses around + or - inside shift");
2383         }
2384
2385       if (code == TRUTH_ORIF_EXPR)
2386         {
2387           if (code1 == TRUTH_ANDIF_EXPR
2388               || code2 == TRUTH_ANDIF_EXPR)
2389             warning (0, "suggest parentheses around && within ||");
2390         }
2391
2392       if (code == BIT_IOR_EXPR)
2393         {
2394           if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
2395               || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2396               || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
2397               || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2398             warning (0, "suggest parentheses around arithmetic in operand of |");
2399           /* Check cases like x|y==z */
2400           if (TREE_CODE_CLASS (code1) == tcc_comparison
2401               || TREE_CODE_CLASS (code2) == tcc_comparison)
2402             warning (0, "suggest parentheses around comparison in operand of |");
2403         }
2404
2405       if (code == BIT_XOR_EXPR)
2406         {
2407           if (code1 == BIT_AND_EXPR
2408               || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2409               || code2 == BIT_AND_EXPR
2410               || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2411             warning (0, "suggest parentheses around arithmetic in operand of ^");
2412           /* Check cases like x^y==z */
2413           if (TREE_CODE_CLASS (code1) == tcc_comparison
2414               || TREE_CODE_CLASS (code2) == tcc_comparison)
2415             warning (0, "suggest parentheses around comparison in operand of ^");
2416         }
2417
2418       if (code == BIT_AND_EXPR)
2419         {
2420           if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2421               || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2422             warning (0, "suggest parentheses around + or - in operand of &");
2423           /* Check cases like x&y==z */
2424           if (TREE_CODE_CLASS (code1) == tcc_comparison
2425               || TREE_CODE_CLASS (code2) == tcc_comparison)
2426             warning (0, "suggest parentheses around comparison in operand of &");
2427         }
2428       /* Similarly, check for cases like 1<=i<=10 that are probably errors.  */
2429       if (TREE_CODE_CLASS (code) == tcc_comparison
2430           && (TREE_CODE_CLASS (code1) == tcc_comparison
2431               || TREE_CODE_CLASS (code2) == tcc_comparison))
2432         warning (0, "comparisons like X<=Y<=Z do not have their mathematical meaning");
2433
2434     }
2435
2436   unsigned_conversion_warning (result.value, arg1.value);
2437   unsigned_conversion_warning (result.value, arg2.value);
2438   overflow_warning (result.value);
2439
2440   return result;
2441 }
2442 \f
2443 /* Return a tree for the difference of pointers OP0 and OP1.
2444    The resulting tree has type int.  */
2445
2446 static tree
2447 pointer_diff (tree op0, tree op1)
2448 {
2449   tree restype = ptrdiff_type_node;
2450
2451   tree target_type = TREE_TYPE (TREE_TYPE (op0));
2452   tree con0, con1, lit0, lit1;
2453   tree orig_op1 = op1;
2454
2455   if (pedantic || warn_pointer_arith)
2456     {
2457       if (TREE_CODE (target_type) == VOID_TYPE)
2458         pedwarn ("pointer of type %<void *%> used in subtraction");
2459       if (TREE_CODE (target_type) == FUNCTION_TYPE)
2460         pedwarn ("pointer to a function used in subtraction");
2461     }
2462
2463   /* If the conversion to ptrdiff_type does anything like widening or
2464      converting a partial to an integral mode, we get a convert_expression
2465      that is in the way to do any simplifications.
2466      (fold-const.c doesn't know that the extra bits won't be needed.
2467      split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2468      different mode in place.)
2469      So first try to find a common term here 'by hand'; we want to cover
2470      at least the cases that occur in legal static initializers.  */
2471   con0 = TREE_CODE (op0) == NOP_EXPR ? TREE_OPERAND (op0, 0) : op0;
2472   con1 = TREE_CODE (op1) == NOP_EXPR ? TREE_OPERAND (op1, 0) : op1;
2473
2474   if (TREE_CODE (con0) == PLUS_EXPR)
2475     {
2476       lit0 = TREE_OPERAND (con0, 1);
2477       con0 = TREE_OPERAND (con0, 0);
2478     }
2479   else
2480     lit0 = integer_zero_node;
2481
2482   if (TREE_CODE (con1) == PLUS_EXPR)
2483     {
2484       lit1 = TREE_OPERAND (con1, 1);
2485       con1 = TREE_OPERAND (con1, 0);
2486     }
2487   else
2488     lit1 = integer_zero_node;
2489
2490   if (operand_equal_p (con0, con1, 0))
2491     {
2492       op0 = lit0;
2493       op1 = lit1;
2494     }
2495
2496
2497   /* First do the subtraction as integers;
2498      then drop through to build the divide operator.
2499      Do not do default conversions on the minus operator
2500      in case restype is a short type.  */
2501
2502   op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2503                          convert (restype, op1), 0);
2504   /* This generates an error if op1 is pointer to incomplete type.  */
2505   if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2506     error ("arithmetic on pointer to an incomplete type");
2507
2508   /* This generates an error if op0 is pointer to incomplete type.  */
2509   op1 = c_size_in_bytes (target_type);
2510
2511   /* Divide by the size, in easiest possible way.  */
2512   return fold (build2 (EXACT_DIV_EXPR, restype, op0, convert (restype, op1)));
2513 }
2514 \f
2515 /* Construct and perhaps optimize a tree representation
2516    for a unary operation.  CODE, a tree_code, specifies the operation
2517    and XARG is the operand.
2518    For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2519    the default promotions (such as from short to int).
2520    For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2521    allows non-lvalues; this is only used to handle conversion of non-lvalue
2522    arrays to pointers in C99.  */
2523
2524 tree
2525 build_unary_op (enum tree_code code, tree xarg, int flag)
2526 {
2527   /* No default_conversion here.  It causes trouble for ADDR_EXPR.  */
2528   tree arg = xarg;
2529   tree argtype = 0;
2530   enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2531   tree val;
2532   int noconvert = flag;
2533
2534   if (typecode == ERROR_MARK)
2535     return error_mark_node;
2536   if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2537     typecode = INTEGER_TYPE;
2538
2539   switch (code)
2540     {
2541     case CONVERT_EXPR:
2542       /* This is used for unary plus, because a CONVERT_EXPR
2543          is enough to prevent anybody from looking inside for
2544          associativity, but won't generate any code.  */
2545       if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2546             || typecode == COMPLEX_TYPE
2547             || typecode == VECTOR_TYPE))
2548         {
2549           error ("wrong type argument to unary plus");
2550           return error_mark_node;
2551         }
2552       else if (!noconvert)
2553         arg = default_conversion (arg);
2554       arg = non_lvalue (arg);
2555       break;
2556
2557     case NEGATE_EXPR:
2558       if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2559             || typecode == COMPLEX_TYPE
2560             || typecode == VECTOR_TYPE))
2561         {
2562           error ("wrong type argument to unary minus");
2563           return error_mark_node;
2564         }
2565       else if (!noconvert)
2566         arg = default_conversion (arg);
2567       break;
2568
2569     case BIT_NOT_EXPR:
2570       if (typecode == INTEGER_TYPE || typecode == VECTOR_TYPE)
2571         {
2572           if (!noconvert)
2573             arg = default_conversion (arg);
2574         }
2575       else if (typecode == COMPLEX_TYPE)
2576         {
2577           code = CONJ_EXPR;
2578           if (pedantic)
2579             pedwarn ("ISO C does not support %<~%> for complex conjugation");
2580           if (!noconvert)
2581             arg = default_conversion (arg);
2582         }
2583       else
2584         {
2585           error ("wrong type argument to bit-complement");
2586           return error_mark_node;
2587         }
2588       break;
2589
2590     case ABS_EXPR:
2591       if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
2592         {
2593           error ("wrong type argument to abs");
2594           return error_mark_node;
2595         }
2596       else if (!noconvert)
2597         arg = default_conversion (arg);
2598       break;
2599
2600     case CONJ_EXPR:
2601       /* Conjugating a real value is a no-op, but allow it anyway.  */
2602       if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2603             || typecode == COMPLEX_TYPE))
2604         {
2605           error ("wrong type argument to conjugation");
2606           return error_mark_node;
2607         }
2608       else if (!noconvert)
2609         arg = default_conversion (arg);
2610       break;
2611
2612     case TRUTH_NOT_EXPR:
2613       /* ??? Why do most validation here but that for non-lvalue arrays
2614          in c_objc_common_truthvalue_conversion?  */
2615       if (typecode != INTEGER_TYPE
2616           && typecode != REAL_TYPE && typecode != POINTER_TYPE
2617           && typecode != COMPLEX_TYPE
2618           /* These will convert to a pointer.  */
2619           && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2620         {
2621           error ("wrong type argument to unary exclamation mark");
2622           return error_mark_node;
2623         }
2624       arg = c_objc_common_truthvalue_conversion (arg);
2625       return invert_truthvalue (arg);
2626
2627     case NOP_EXPR:
2628       break;
2629
2630     case REALPART_EXPR:
2631       if (TREE_CODE (arg) == COMPLEX_CST)
2632         return TREE_REALPART (arg);
2633       else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2634         return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2635       else
2636         return arg;
2637
2638     case IMAGPART_EXPR:
2639       if (TREE_CODE (arg) == COMPLEX_CST)
2640         return TREE_IMAGPART (arg);
2641       else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2642         return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2643       else
2644         return convert (TREE_TYPE (arg), integer_zero_node);
2645
2646     case PREINCREMENT_EXPR:
2647     case POSTINCREMENT_EXPR:
2648     case PREDECREMENT_EXPR:
2649     case POSTDECREMENT_EXPR:
2650
2651       /* Increment or decrement the real part of the value,
2652          and don't change the imaginary part.  */
2653       if (typecode == COMPLEX_TYPE)
2654         {
2655           tree real, imag;
2656
2657           if (pedantic)
2658             pedwarn ("ISO C does not support %<++%> and %<--%>"
2659                      " on complex types");
2660
2661           arg = stabilize_reference (arg);
2662           real = build_unary_op (REALPART_EXPR, arg, 1);
2663           imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2664           return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
2665                          build_unary_op (code, real, 1), imag);
2666         }
2667
2668       /* Report invalid types.  */
2669
2670       if (typecode != POINTER_TYPE
2671           && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2672         {
2673           if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2674             error ("wrong type argument to increment");
2675           else
2676             error ("wrong type argument to decrement");
2677
2678           return error_mark_node;
2679         }
2680
2681       {
2682         tree inc;
2683         tree result_type = TREE_TYPE (arg);
2684
2685         arg = get_unwidened (arg, 0);
2686         argtype = TREE_TYPE (arg);
2687
2688         /* Compute the increment.  */
2689
2690         if (typecode == POINTER_TYPE)
2691           {
2692             /* If pointer target is an undefined struct,
2693                we just cannot know how to do the arithmetic.  */
2694             if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
2695               {
2696                 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2697                   error ("increment of pointer to unknown structure");
2698                 else
2699                   error ("decrement of pointer to unknown structure");
2700               }
2701             else if ((pedantic || warn_pointer_arith)
2702                      && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2703                          || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2704               {
2705                 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2706                   pedwarn ("wrong type argument to increment");
2707                 else
2708                   pedwarn ("wrong type argument to decrement");
2709               }
2710
2711             inc = c_size_in_bytes (TREE_TYPE (result_type));
2712           }
2713         else
2714           inc = integer_one_node;
2715
2716         inc = convert (argtype, inc);
2717
2718         /* Complain about anything else that is not a true lvalue.  */
2719         if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2720                                     || code == POSTINCREMENT_EXPR)
2721                                    ? lv_increment
2722                                    : lv_decrement)))
2723           return error_mark_node;
2724
2725         /* Report a read-only lvalue.  */
2726         if (TREE_READONLY (arg))
2727           readonly_error (arg,
2728                           ((code == PREINCREMENT_EXPR
2729                             || code == POSTINCREMENT_EXPR)
2730                            ? lv_increment : lv_decrement));
2731
2732         if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2733           val = boolean_increment (code, arg);
2734         else
2735           val = build2 (code, TREE_TYPE (arg), arg, inc);
2736         TREE_SIDE_EFFECTS (val) = 1;
2737         val = convert (result_type, val);
2738         if (TREE_CODE (val) != code)
2739           TREE_NO_WARNING (val) = 1;
2740         return val;
2741       }
2742
2743     case ADDR_EXPR:
2744       /* Note that this operation never does default_conversion.  */
2745
2746       /* Let &* cancel out to simplify resulting code.  */
2747       if (TREE_CODE (arg) == INDIRECT_REF)
2748         {
2749           /* Don't let this be an lvalue.  */
2750           if (lvalue_p (TREE_OPERAND (arg, 0)))
2751             return non_lvalue (TREE_OPERAND (arg, 0));
2752           return TREE_OPERAND (arg, 0);
2753         }
2754
2755       /* For &x[y], return x+y */
2756       if (TREE_CODE (arg) == ARRAY_REF)
2757         {
2758           if (!c_mark_addressable (TREE_OPERAND (arg, 0)))
2759             return error_mark_node;
2760           return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
2761                                   TREE_OPERAND (arg, 1), 1);
2762         }
2763
2764       /* Anything not already handled and not a true memory reference
2765          or a non-lvalue array is an error.  */
2766       else if (typecode != FUNCTION_TYPE && !flag
2767                && !lvalue_or_else (arg, lv_addressof))
2768         return error_mark_node;
2769
2770       /* Ordinary case; arg is a COMPONENT_REF or a decl.  */
2771       argtype = TREE_TYPE (arg);
2772
2773       /* If the lvalue is const or volatile, merge that into the type
2774          to which the address will point.  Note that you can't get a
2775          restricted pointer by taking the address of something, so we
2776          only have to deal with `const' and `volatile' here.  */
2777       if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
2778           && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
2779           argtype = c_build_type_variant (argtype,
2780                                           TREE_READONLY (arg),
2781                                           TREE_THIS_VOLATILE (arg));
2782
2783       if (!c_mark_addressable (arg))
2784         return error_mark_node;
2785
2786       gcc_assert (TREE_CODE (arg) != COMPONENT_REF
2787                   || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
2788
2789       argtype = build_pointer_type (argtype);
2790
2791       /* ??? Cope with user tricks that amount to offsetof.  Delete this
2792          when we have proper support for integer constant expressions.  */
2793       val = get_base_address (arg);
2794       if (val && TREE_CODE (val) == INDIRECT_REF
2795           && integer_zerop (TREE_OPERAND (val, 0)))
2796         return fold_convert (argtype, fold_offsetof (arg));
2797
2798       val = build1 (ADDR_EXPR, argtype, arg);
2799
2800       if (TREE_CODE (arg) == COMPOUND_LITERAL_EXPR)
2801         TREE_INVARIANT (val) = TREE_CONSTANT (val) = 1;
2802
2803       return val;
2804
2805     default:
2806       break;
2807     }
2808
2809   if (argtype == 0)
2810     argtype = TREE_TYPE (arg);
2811   val = build1 (code, argtype, arg);
2812   return require_constant_value ? fold_initializer (val) : fold (val);
2813 }
2814
2815 /* Return nonzero if REF is an lvalue valid for this language.
2816    Lvalues can be assigned, unless their type has TYPE_READONLY.
2817    Lvalues can have their address taken, unless they have C_DECL_REGISTER.  */
2818
2819 static int
2820 lvalue_p (tree ref)
2821 {
2822   enum tree_code code = TREE_CODE (ref);
2823
2824   switch (code)
2825     {
2826     case REALPART_EXPR:
2827     case IMAGPART_EXPR:
2828     case COMPONENT_REF:
2829       return lvalue_p (TREE_OPERAND (ref, 0));
2830
2831     case COMPOUND_LITERAL_EXPR:
2832     case STRING_CST:
2833       return 1;
2834
2835     case INDIRECT_REF:
2836     case ARRAY_REF:
2837     case VAR_DECL:
2838     case PARM_DECL:
2839     case RESULT_DECL:
2840     case ERROR_MARK:
2841       return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
2842               && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
2843
2844     case BIND_EXPR:
2845       return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
2846
2847     default:
2848       return 0;
2849     }
2850 }
2851 \f
2852 /* Give an error for storing in something that is 'const'.  */
2853
2854 static void
2855 readonly_error (tree arg, enum lvalue_use use)
2856 {
2857   gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement);
2858   /* Using this macro rather than (for example) arrays of messages
2859      ensures that all the format strings are checked at compile
2860      time.  */
2861 #define READONLY_MSG(A, I, D) (use == lv_assign                         \
2862                                ? (A)                                    \
2863                                : (use == lv_increment ? (I) : (D)))
2864   if (TREE_CODE (arg) == COMPONENT_REF)
2865     {
2866       if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
2867         readonly_error (TREE_OPERAND (arg, 0), use);
2868       else
2869         error (READONLY_MSG (N_("assignment of read-only member %qD"),
2870                              N_("increment of read-only member %qD"),
2871                              N_("decrement of read-only member %qD")),
2872                TREE_OPERAND (arg, 1));
2873     }
2874   else if (TREE_CODE (arg) == VAR_DECL)
2875     error (READONLY_MSG (N_("assignment of read-only variable %qD"),
2876                          N_("increment of read-only variable %qD"),
2877                          N_("decrement of read-only variable %qD")),
2878            arg);
2879   else
2880     error (READONLY_MSG (N_("assignment of read-only location"),
2881                          N_("increment of read-only location"),
2882                          N_("decrement of read-only location")));
2883 }
2884
2885
2886 /* Return nonzero if REF is an lvalue valid for this language;
2887    otherwise, print an error message and return zero.  USE says
2888    how the lvalue is being used and so selects the error message.  */
2889
2890 static int
2891 lvalue_or_else (tree ref, enum lvalue_use use)
2892 {
2893   int win = lvalue_p (ref);
2894
2895   if (!win)
2896     lvalue_error (use);
2897
2898   return win;
2899 }
2900 \f
2901 /* Mark EXP saying that we need to be able to take the
2902    address of it; it should not be allocated in a register.
2903    Returns true if successful.  */
2904
2905 bool
2906 c_mark_addressable (tree exp)
2907 {
2908   tree x = exp;
2909
2910   while (1)
2911     switch (TREE_CODE (x))
2912       {
2913       case COMPONENT_REF:
2914         if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
2915           {
2916             error
2917               ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
2918             return false;
2919           }
2920
2921         /* ... fall through ...  */
2922
2923       case ADDR_EXPR:
2924       case ARRAY_REF:
2925       case REALPART_EXPR:
2926       case IMAGPART_EXPR:
2927         x = TREE_OPERAND (x, 0);
2928         break;
2929
2930       case COMPOUND_LITERAL_EXPR:
2931       case CONSTRUCTOR:
2932         TREE_ADDRESSABLE (x) = 1;
2933         return true;
2934
2935       case VAR_DECL:
2936       case CONST_DECL:
2937       case PARM_DECL:
2938       case RESULT_DECL:
2939         if (C_DECL_REGISTER (x)
2940             && DECL_NONLOCAL (x))
2941           {
2942             if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
2943               {
2944                 error
2945                   ("global register variable %qD used in nested function", x);
2946                 return false;
2947               }
2948             pedwarn ("register variable %qD used in nested function", x);
2949           }
2950         else if (C_DECL_REGISTER (x))
2951           {
2952             if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
2953               error ("address of global register variable %qD requested", x);
2954             else
2955               error ("address of register variable %qD requested", x);
2956             return false;
2957           }
2958
2959         /* drops in */
2960       case FUNCTION_DECL:
2961         TREE_ADDRESSABLE (x) = 1;
2962         /* drops out */
2963       default:
2964         return true;
2965     }
2966 }
2967 \f
2968 /* Build and return a conditional expression IFEXP ? OP1 : OP2.  */
2969
2970 tree
2971 build_conditional_expr (tree ifexp, tree op1, tree op2)
2972 {
2973   tree type1;
2974   tree type2;
2975   enum tree_code code1;
2976   enum tree_code code2;
2977   tree result_type = NULL;
2978   tree orig_op1 = op1, orig_op2 = op2;
2979
2980   /* Promote both alternatives.  */
2981
2982   if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
2983     op1 = default_conversion (op1);
2984   if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
2985     op2 = default_conversion (op2);
2986
2987   if (TREE_CODE (ifexp) == ERROR_MARK
2988       || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
2989       || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
2990     return error_mark_node;
2991
2992   type1 = TREE_TYPE (op1);
2993   code1 = TREE_CODE (type1);
2994   type2 = TREE_TYPE (op2);
2995   code2 = TREE_CODE (type2);
2996
2997   /* C90 does not permit non-lvalue arrays in conditional expressions.
2998      In C99 they will be pointers by now.  */
2999   if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
3000     {
3001       error ("non-lvalue array in conditional expression");
3002       return error_mark_node;
3003     }
3004
3005   /* Quickly detect the usual case where op1 and op2 have the same type
3006      after promotion.  */
3007   if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
3008     {
3009       if (type1 == type2)
3010         result_type = type1;
3011       else
3012         result_type = TYPE_MAIN_VARIANT (type1);
3013     }
3014   else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
3015             || code1 == COMPLEX_TYPE)
3016            && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
3017                || code2 == COMPLEX_TYPE))
3018     {
3019       result_type = c_common_type (type1, type2);
3020
3021       /* If -Wsign-compare, warn here if type1 and type2 have
3022          different signedness.  We'll promote the signed to unsigned
3023          and later code won't know it used to be different.
3024          Do this check on the original types, so that explicit casts
3025          will be considered, but default promotions won't.  */
3026       if (warn_sign_compare && !skip_evaluation)
3027         {
3028           int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
3029           int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
3030
3031           if (unsigned_op1 ^ unsigned_op2)
3032             {
3033               /* Do not warn if the result type is signed, since the
3034                  signed type will only be chosen if it can represent
3035                  all the values of the unsigned type.  */
3036               if (!TYPE_UNSIGNED (result_type))
3037                 /* OK */;
3038               /* Do not warn if the signed quantity is an unsuffixed
3039                  integer literal (or some static constant expression
3040                  involving such literals) and it is non-negative.  */
3041               else if ((unsigned_op2 && tree_expr_nonnegative_p (op1))
3042                        || (unsigned_op1 && tree_expr_nonnegative_p (op2)))
3043                 /* OK */;
3044               else
3045                 warning (0, "signed and unsigned type in conditional expression");
3046             }
3047         }
3048     }
3049   else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3050     {
3051       if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
3052         pedwarn ("ISO C forbids conditional expr with only one void side");
3053       result_type = void_type_node;
3054     }
3055   else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3056     {
3057       if (comp_target_types (type1, type2, 1))
3058         result_type = common_pointer_type (type1, type2);
3059       else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
3060                && TREE_CODE (orig_op1) != NOP_EXPR)
3061         result_type = qualify_type (type2, type1);
3062       else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
3063                && TREE_CODE (orig_op2) != NOP_EXPR)
3064         result_type = qualify_type (type1, type2);
3065       else if (VOID_TYPE_P (TREE_TYPE (type1)))
3066         {
3067           if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
3068             pedwarn ("ISO C forbids conditional expr between "
3069                      "%<void *%> and function pointer");
3070           result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
3071                                                           TREE_TYPE (type2)));
3072         }
3073       else if (VOID_TYPE_P (TREE_TYPE (type2)))
3074         {
3075           if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3076             pedwarn ("ISO C forbids conditional expr between "
3077                      "%<void *%> and function pointer");
3078           result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
3079                                                           TREE_TYPE (type1)));
3080         }
3081       else
3082         {
3083           pedwarn ("pointer type mismatch in conditional expression");
3084           result_type = build_pointer_type (void_type_node);
3085         }
3086     }
3087   else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3088     {
3089       if (!integer_zerop (op2))
3090         pedwarn ("pointer/integer type mismatch in conditional expression");
3091       else
3092         {
3093           op2 = null_pointer_node;
3094         }
3095       result_type = type1;
3096     }
3097   else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3098     {
3099       if (!integer_zerop (op1))
3100         pedwarn ("pointer/integer type mismatch in conditional expression");
3101       else
3102         {
3103           op1 = null_pointer_node;
3104         }
3105       result_type = type2;
3106     }
3107
3108   if (!result_type)
3109     {
3110       if (flag_cond_mismatch)
3111         result_type = void_type_node;
3112       else
3113         {
3114           error ("type mismatch in conditional expression");
3115           return error_mark_node;
3116         }
3117     }
3118
3119   /* Merge const and volatile flags of the incoming types.  */
3120   result_type
3121     = build_type_variant (result_type,
3122                           TREE_READONLY (op1) || TREE_READONLY (op2),
3123                           TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3124
3125   if (result_type != TREE_TYPE (op1))
3126     op1 = convert_and_check (result_type, op1);
3127   if (result_type != TREE_TYPE (op2))
3128     op2 = convert_and_check (result_type, op2);
3129
3130   return fold_build3 (COND_EXPR, result_type, ifexp, op1, op2);
3131 }
3132 \f
3133 /* Return a compound expression that performs two expressions and
3134    returns the value of the second of them.  */
3135
3136 tree
3137 build_compound_expr (tree expr1, tree expr2)
3138 {
3139   /* Convert arrays and functions to pointers.  */
3140   expr2 = default_function_array_conversion (expr2);
3141
3142   if (!TREE_SIDE_EFFECTS (expr1))
3143     {
3144       /* The left-hand operand of a comma expression is like an expression
3145          statement: with -Wextra or -Wunused, we should warn if it doesn't have
3146          any side-effects, unless it was explicitly cast to (void).  */
3147       if (warn_unused_value)
3148         {
3149           if (VOID_TYPE_P (TREE_TYPE (expr1))
3150               && TREE_CODE (expr1) == CONVERT_EXPR)
3151             ; /* (void) a, b */
3152           else if (VOID_TYPE_P (TREE_TYPE (expr1))
3153                    && TREE_CODE (expr1) == COMPOUND_EXPR
3154                    && TREE_CODE (TREE_OPERAND (expr1, 1)) == CONVERT_EXPR)
3155             ; /* (void) a, (void) b, c */
3156           else
3157             warning (0, "left-hand operand of comma expression has no effect");
3158         }
3159     }
3160
3161   /* With -Wunused, we should also warn if the left-hand operand does have
3162      side-effects, but computes a value which is not used.  For example, in
3163      `foo() + bar(), baz()' the result of the `+' operator is not used,
3164      so we should issue a warning.  */
3165   else if (warn_unused_value)
3166     warn_if_unused_value (expr1, input_location);
3167
3168   return build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
3169 }
3170
3171 /* Build an expression representing a cast to type TYPE of expression EXPR.  */
3172
3173 tree
3174 build_c_cast (tree type, tree expr)
3175 {
3176   tree value = expr;
3177
3178   if (type == error_mark_node || expr == error_mark_node)
3179     return error_mark_node;
3180
3181   /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
3182      only in <protocol> qualifications.  But when constructing cast expressions,
3183      the protocols do matter and must be kept around.  */
3184   if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
3185     return build1 (NOP_EXPR, type, expr);
3186
3187   type = TYPE_MAIN_VARIANT (type);
3188
3189   if (TREE_CODE (type) == ARRAY_TYPE)
3190     {
3191       error ("cast specifies array type");
3192       return error_mark_node;
3193     }
3194
3195   if (TREE_CODE (type) == FUNCTION_TYPE)
3196     {
3197       error ("cast specifies function type");
3198       return error_mark_node;
3199     }
3200
3201   if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
3202     {
3203       if (pedantic)
3204         {
3205           if (TREE_CODE (type) == RECORD_TYPE
3206               || TREE_CODE (type) == UNION_TYPE)
3207             pedwarn ("ISO C forbids casting nonscalar to the same type");
3208         }
3209     }
3210   else if (TREE_CODE (type) == UNION_TYPE)
3211     {
3212       tree field;
3213       value = default_function_array_conversion (value);
3214
3215       for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3216         if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3217                        TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3218           break;
3219
3220       if (field)
3221         {
3222           tree t;
3223
3224           if (pedantic)
3225             pedwarn ("ISO C forbids casts to union type");
3226           t = digest_init (type,
3227                            build_constructor (type,
3228                                               build_tree_list (field, value)),
3229                            true, 0);
3230           TREE_CONSTANT (t) = TREE_CONSTANT (value);
3231           TREE_INVARIANT (t) = TREE_INVARIANT (value);
3232           return t;
3233         }
3234       error ("cast to union type from type not present in union");
3235       return error_mark_node;
3236     }
3237   else
3238     {
3239       tree otype, ovalue;
3240
3241       /* If casting to void, avoid the error that would come
3242          from default_conversion in the case of a non-lvalue array.  */
3243       if (type == void_type_node)
3244         return build1 (CONVERT_EXPR, type, value);
3245
3246       /* Convert functions and arrays to pointers,
3247          but don't convert any other types.  */
3248       value = default_function_array_conversion (value);
3249       otype = TREE_TYPE (value);
3250
3251       /* Optionally warn about potentially worrisome casts.  */
3252
3253       if (warn_cast_qual
3254           && TREE_CODE (type) == POINTER_TYPE
3255           && TREE_CODE (otype) == POINTER_TYPE)
3256         {
3257           tree in_type = type;
3258           tree in_otype = otype;
3259           int added = 0;
3260           int discarded = 0;
3261
3262           /* Check that the qualifiers on IN_TYPE are a superset of
3263              the qualifiers of IN_OTYPE.  The outermost level of
3264              POINTER_TYPE nodes is uninteresting and we stop as soon
3265              as we hit a non-POINTER_TYPE node on either type.  */
3266           do
3267             {
3268               in_otype = TREE_TYPE (in_otype);
3269               in_type = TREE_TYPE (in_type);
3270
3271               /* GNU C allows cv-qualified function types.  'const'
3272                  means the function is very pure, 'volatile' means it
3273                  can't return.  We need to warn when such qualifiers
3274                  are added, not when they're taken away.  */
3275               if (TREE_CODE (in_otype) == FUNCTION_TYPE
3276                   && TREE_CODE (in_type) == FUNCTION_TYPE)
3277                 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
3278               else
3279                 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
3280             }
3281           while (TREE_CODE (in_type) == POINTER_TYPE
3282                  && TREE_CODE (in_otype) == POINTER_TYPE);
3283
3284           if (added)
3285             warning (0, "cast adds new qualifiers to function type");
3286
3287           if (discarded)
3288             /* There are qualifiers present in IN_OTYPE that are not
3289                present in IN_TYPE.  */
3290             warning (0, "cast discards qualifiers from pointer target type");
3291         }
3292
3293       /* Warn about possible alignment problems.  */
3294       if (STRICT_ALIGNMENT && warn_cast_align
3295           && TREE_CODE (type) == POINTER_TYPE
3296           && TREE_CODE (otype) == POINTER_TYPE
3297           && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3298           && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3299           /* Don't warn about opaque types, where the actual alignment
3300              restriction is unknown.  */
3301           && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3302                 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3303                && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3304           && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3305         warning (0, "cast increases required alignment of target type");
3306
3307       if (warn_pointer_to_int_cast
3308           && TREE_CODE (type) == INTEGER_TYPE
3309           && TREE_CODE (otype) == POINTER_TYPE
3310           && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3311           && !TREE_CONSTANT (value))
3312         warning (0, "cast from pointer to integer of different size");
3313
3314       if (warn_bad_function_cast
3315           && TREE_CODE (value) == CALL_EXPR
3316           && TREE_CODE (type) != TREE_CODE (otype))
3317         warning (0, "cast from function call of type %qT to non-matching "
3318                  "type %qT", otype, type);
3319
3320       if (warn_int_to_pointer_cast
3321           && TREE_CODE (type) == POINTER_TYPE
3322           && TREE_CODE (otype) == INTEGER_TYPE
3323           && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3324           /* Don't warn about converting any constant.  */
3325           && !TREE_CONSTANT (value))
3326         warning (0, "cast to pointer from integer of different size");
3327
3328       if (TREE_CODE (type) == POINTER_TYPE
3329           && TREE_CODE (otype) == POINTER_TYPE
3330           && TREE_CODE (expr) == ADDR_EXPR
3331           && DECL_P (TREE_OPERAND (expr, 0))
3332           && flag_strict_aliasing && warn_strict_aliasing
3333           && !VOID_TYPE_P (TREE_TYPE (type)))
3334         {
3335           /* Casting the address of a decl to non void pointer. Warn
3336              if the cast breaks type based aliasing.  */
3337           if (!COMPLETE_TYPE_P (TREE_TYPE (type)))
3338             warning (0, "type-punning to incomplete type might break strict-aliasing rules");
3339           else
3340             {
3341               HOST_WIDE_INT set1 = get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
3342               HOST_WIDE_INT set2 = get_alias_set (TREE_TYPE (type));
3343
3344               if (!alias_sets_conflict_p (set1, set2))
3345                 warning (0, "dereferencing type-punned pointer will break strict-aliasing rules");
3346               else if (warn_strict_aliasing > 1
3347                        && !alias_sets_might_conflict_p (set1, set2))
3348                 warning (0, "dereferencing type-punned pointer might break strict-aliasing rules");
3349             }
3350         }
3351
3352       /* If pedantic, warn for conversions between function and object
3353          pointer types, except for converting a null pointer constant
3354          to function pointer type.  */
3355       if (pedantic
3356           && TREE_CODE (type) == POINTER_TYPE
3357           && TREE_CODE (otype) == POINTER_TYPE
3358           && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
3359           && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
3360         pedwarn ("ISO C forbids conversion of function pointer to object pointer type");
3361
3362       if (pedantic
3363           && TREE_CODE (type) == POINTER_TYPE
3364           && TREE_CODE (otype) == POINTER_TYPE
3365           && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
3366           && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3367           && !(integer_zerop (value) && TREE_TYPE (otype) == void_type_node
3368                && TREE_CODE (expr) != NOP_EXPR))
3369         pedwarn ("ISO C forbids conversion of object pointer to function pointer type");
3370
3371       ovalue = value;
3372       value = convert (type, value);
3373
3374       /* Ignore any integer overflow caused by the cast.  */
3375       if (TREE_CODE (value) == INTEGER_CST)
3376         {
3377           if (EXPR_P (ovalue))
3378             /* If OVALUE had overflow set, then so will VALUE, so it
3379                is safe to overwrite.  */
3380             TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3381           else
3382             TREE_OVERFLOW (value) = 0;
3383           
3384           if (CONSTANT_CLASS_P (ovalue))
3385             /* Similarly, constant_overflow cannot have become
3386                cleared.  */
3387             TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3388         }
3389     }
3390
3391   /* Don't let a cast be an lvalue.  */
3392   if (value == expr)
3393     value = non_lvalue (value);
3394
3395   return value;
3396 }
3397
3398 /* Interpret a cast of expression EXPR to type TYPE.  */
3399 tree
3400 c_cast_expr (struct c_type_name *type_name, tree expr)
3401 {
3402   tree type;
3403   int saved_wsp = warn_strict_prototypes;
3404
3405   /* This avoids warnings about unprototyped casts on
3406      integers.  E.g. "#define SIG_DFL (void(*)())0".  */
3407   if (TREE_CODE (expr) == INTEGER_CST)
3408     warn_strict_prototypes = 0;
3409   type = groktypename (type_name);
3410   warn_strict_prototypes = saved_wsp;
3411
3412   return build_c_cast (type, expr);
3413 }
3414
3415 \f
3416 /* Build an assignment expression of lvalue LHS from value RHS.
3417    MODIFYCODE is the code for a binary operator that we use
3418    to combine the old value of LHS with RHS to get the new value.
3419    Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.  */
3420
3421 tree
3422 build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
3423 {
3424   tree result;
3425   tree newrhs;
3426   tree lhstype = TREE_TYPE (lhs);
3427   tree olhstype = lhstype;
3428
3429   /* Types that aren't fully specified cannot be used in assignments.  */
3430   lhs = require_complete_type (lhs);
3431
3432   /* Avoid duplicate error messages from operands that had errors.  */
3433   if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3434     return error_mark_node;
3435
3436   STRIP_TYPE_NOPS (rhs);
3437
3438   newrhs = rhs;
3439
3440   /* If a binary op has been requested, combine the old LHS value with the RHS
3441      producing the value we should actually store into the LHS.  */
3442
3443   if (modifycode != NOP_EXPR)
3444     {
3445       lhs = stabilize_reference (lhs);
3446       newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3447     }
3448
3449   if (!lvalue_or_else (lhs, lv_assign))
3450     return error_mark_node;
3451
3452   /* Give an error for storing in something that is 'const'.  */
3453
3454   if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3455       || ((TREE_CODE (lhstype) == RECORD_TYPE
3456            || TREE_CODE (lhstype) == UNION_TYPE)
3457           && C_TYPE_FIELDS_READONLY (lhstype)))
3458     readonly_error (lhs, lv_assign);
3459
3460   /* If storing into a structure or union member,
3461      it has probably been given type `int'.
3462      Compute the type that would go with
3463      the actual amount of storage the member occupies.  */
3464
3465   if (TREE_CODE (lhs) == COMPONENT_REF
3466       && (TREE_CODE (lhstype) == INTEGER_TYPE
3467           || TREE_CODE (lhstype) == BOOLEAN_TYPE
3468           || TREE_CODE (lhstype) == REAL_TYPE
3469           || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3470     lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3471
3472   /* If storing in a field that is in actuality a short or narrower than one,
3473      we must store in the field in its actual type.  */
3474
3475   if (lhstype != TREE_TYPE (lhs))
3476     {
3477       lhs = copy_node (lhs);
3478       TREE_TYPE (lhs) = lhstype;
3479     }
3480
3481   /* Convert new value to destination type.  */
3482
3483   newrhs = convert_for_assignment (lhstype, newrhs, ic_assign,
3484                                    NULL_TREE, NULL_TREE, 0);
3485   if (TREE_CODE (newrhs) == ERROR_MARK)
3486     return error_mark_node;
3487
3488   /* Scan operands.  */
3489
3490   result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
3491   TREE_SIDE_EFFECTS (result) = 1;
3492
3493   /* If we got the LHS in a different type for storing in,
3494      convert the result back to the nominal type of LHS
3495      so that the value we return always has the same type
3496      as the LHS argument.  */
3497
3498   if (olhstype == TREE_TYPE (result))
3499     return result;
3500   return convert_for_assignment (olhstype, result, ic_assign,
3501                                  NULL_TREE, NULL_TREE, 0);
3502 }
3503 \f
3504 /* Convert value RHS to type TYPE as preparation for an assignment
3505    to an lvalue of type TYPE.
3506    The real work of conversion is done by `convert'.
3507    The purpose of this function is to generate error messages
3508    for assignments that are not allowed in C.
3509    ERRTYPE says whether it is argument passing, assignment,
3510    initialization or return.
3511
3512    FUNCTION is a tree for the function being called.
3513    PARMNUM is the number of the argument, for printing in error messages.  */
3514
3515 static tree
3516 convert_for_assignment (tree type, tree rhs, enum impl_conv errtype,
3517                         tree fundecl, tree function, int parmnum)
3518 {
3519   enum tree_code codel = TREE_CODE (type);
3520   tree rhstype;
3521   enum tree_code coder;
3522   tree rname = NULL_TREE;
3523
3524   if (errtype == ic_argpass || errtype == ic_argpass_nonproto)
3525     {
3526       tree selector;
3527       /* Change pointer to function to the function itself for
3528          diagnostics.  */
3529       if (TREE_CODE (function) == ADDR_EXPR
3530           && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3531         function = TREE_OPERAND (function, 0);
3532
3533       /* Handle an ObjC selector specially for diagnostics.  */
3534       selector = objc_message_selector ();
3535       rname = function;
3536       if (selector && parmnum > 2)
3537         {
3538           rname = selector;
3539           parmnum -= 2;
3540         }
3541     }
3542
3543   /* This macro is used to emit diagnostics to ensure that all format
3544      strings are complete sentences, visible to gettext and checked at
3545      compile time.  */
3546 #define WARN_FOR_ASSIGNMENT(AR, AS, IN, RE)     \
3547   do {                                          \
3548     switch (errtype)                            \
3549       {                                         \
3550       case ic_argpass:                          \
3551         pedwarn (AR, parmnum, rname);           \
3552         break;                                  \
3553       case ic_argpass_nonproto:                 \
3554         warning (0, AR, parmnum, rname);                \
3555         break;                                  \
3556       case ic_assign:                           \
3557         pedwarn (AS);                           \
3558         break;                                  \
3559       case ic_init:                             \
3560         pedwarn (IN);                           \
3561         break;                                  \
3562       case ic_return:                           \
3563         pedwarn (RE);                           \
3564         break;                                  \
3565       default:                                  \
3566         gcc_unreachable ();                     \
3567       }                                         \
3568   } while (0)
3569
3570   STRIP_TYPE_NOPS (rhs);
3571
3572   if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
3573       || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
3574     rhs = default_conversion (rhs);
3575   else if (optimize && TREE_CODE (rhs) == VAR_DECL)
3576     rhs = decl_constant_value_for_broken_optimization (rhs);
3577
3578   rhstype = TREE_TYPE (rhs);
3579   coder = TREE_CODE (rhstype);
3580
3581   if (coder == ERROR_MARK)
3582     return error_mark_node;
3583
3584   if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
3585     {
3586       overflow_warning (rhs);
3587       /* Check for Objective-C protocols.  This will automatically
3588          issue a warning if there are protocol violations.  No need to
3589          use the return value.  */
3590       if (c_dialect_objc ())
3591         objc_comptypes (type, rhstype, 0);
3592       return rhs;
3593     }
3594
3595   if (coder == VOID_TYPE)
3596     {
3597       /* Except for passing an argument to an unprototyped function,
3598          this is a constraint violation.  When passing an argument to
3599          an unprototyped function, it is compile-time undefined;
3600          making it a constraint in that case was rejected in
3601          DR#252.  */
3602       error ("void value not ignored as it ought to be");
3603       return error_mark_node;
3604     }
3605   /* A type converts to a reference to it.
3606      This code doesn't fully support references, it's just for the
3607      special case of va_start and va_copy.  */
3608   if (codel == REFERENCE_TYPE
3609       && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
3610     {
3611       if (!lvalue_p (rhs))
3612         {
3613           error ("cannot pass rvalue to reference parameter");
3614           return error_mark_node;
3615         }
3616       if (!c_mark_addressable (rhs))
3617         return error_mark_node;
3618       rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
3619
3620       /* We already know that these two types are compatible, but they
3621          may not be exactly identical.  In fact, `TREE_TYPE (type)' is
3622          likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
3623          likely to be va_list, a typedef to __builtin_va_list, which
3624          is different enough that it will cause problems later.  */
3625       if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
3626         rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
3627
3628       rhs = build1 (NOP_EXPR, type, rhs);
3629       return rhs;
3630     }
3631   /* Some types can interconvert without explicit casts.  */
3632   else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
3633            && vector_types_convertible_p (type, TREE_TYPE (rhs)))
3634     return convert (type, rhs);
3635   /* Arithmetic types all interconvert, and enum is treated like int.  */
3636   else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
3637             || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
3638             || codel == BOOLEAN_TYPE)
3639            && (coder == INTEGER_TYPE || coder == REAL_TYPE
3640                || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
3641                || coder == BOOLEAN_TYPE))
3642     return convert_and_check (type, rhs);
3643
3644   /* Conversion to a transparent union from its member types.
3645      This applies only to function arguments.  */
3646   else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type)
3647            && (errtype == ic_argpass || errtype == ic_argpass_nonproto))
3648     {
3649       tree memb_types;
3650       tree marginal_memb_type = 0;
3651
3652       for (memb_types = TYPE_FIELDS (type); memb_types;
3653            memb_types = TREE_CHAIN (memb_types))
3654         {
3655           tree memb_type = TREE_TYPE (memb_types);
3656
3657           if (comptypes (TYPE_MAIN_VARIANT (memb_type),
3658                          TYPE_MAIN_VARIANT (rhstype)))
3659             break;
3660
3661           if (TREE_CODE (memb_type) != POINTER_TYPE)
3662             continue;
3663
3664           if (coder == POINTER_TYPE)
3665             {
3666               tree ttl = TREE_TYPE (memb_type);
3667               tree ttr = TREE_TYPE (rhstype);
3668
3669               /* Any non-function converts to a [const][volatile] void *
3670                  and vice versa; otherwise, targets must be the same.
3671                  Meanwhile, the lhs target must have all the qualifiers of
3672                  the rhs.  */
3673               if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3674                   || comp_target_types (memb_type, rhstype, 0))
3675                 {
3676                   /* If this type won't generate any warnings, use it.  */
3677                   if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
3678                       || ((TREE_CODE (ttr) == FUNCTION_TYPE
3679                            && TREE_CODE (ttl) == FUNCTION_TYPE)
3680                           ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3681                              == TYPE_QUALS (ttr))
3682                           : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3683                              == TYPE_QUALS (ttl))))
3684                     break;
3685
3686                   /* Keep looking for a better type, but remember this one.  */
3687                   if (!marginal_memb_type)
3688                     marginal_memb_type = memb_type;
3689                 }
3690             }
3691
3692           /* Can convert integer zero to any pointer type.  */
3693           if (integer_zerop (rhs)
3694               || (TREE_CODE (rhs) == NOP_EXPR
3695                   && integer_zerop (TREE_OPERAND (rhs, 0))))
3696             {
3697               rhs = null_pointer_node;
3698               break;
3699             }
3700         }
3701
3702       if (memb_types || marginal_memb_type)
3703         {
3704           if (!memb_types)
3705             {
3706               /* We have only a marginally acceptable member type;
3707                  it needs a warning.  */
3708               tree ttl = TREE_TYPE (marginal_memb_type);
3709               tree ttr = TREE_TYPE (rhstype);
3710
3711               /* Const and volatile mean something different for function
3712                  types, so the usual warnings are not appropriate.  */
3713               if (TREE_CODE (ttr) == FUNCTION_TYPE
3714                   && TREE_CODE (ttl) == FUNCTION_TYPE)
3715                 {
3716                   /* Because const and volatile on functions are
3717                      restrictions that say the function will not do
3718                      certain things, it is okay to use a const or volatile
3719                      function where an ordinary one is wanted, but not
3720                      vice-versa.  */
3721                   if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3722                     WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE "
3723                                             "makes qualified function "
3724                                             "pointer from unqualified"),
3725                                          N_("assignment makes qualified "
3726                                             "function pointer from "
3727                                             "unqualified"),
3728                                          N_("initialization makes qualified "
3729                                             "function pointer from "
3730                                             "unqualified"),
3731                                          N_("return makes qualified function "
3732                                             "pointer from unqualified"));
3733                 }
3734               else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3735                 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE discards "
3736                                         "qualifiers from pointer target type"),
3737                                      N_("assignment discards qualifiers "
3738                                         "from pointer target type"),
3739                                      N_("initialization discards qualifiers "
3740                                         "from pointer target type"),
3741                                      N_("return discards qualifiers from "
3742                                         "pointer target type"));
3743             }
3744
3745           if (pedantic && !DECL_IN_SYSTEM_HEADER (fundecl))
3746             pedwarn ("ISO C prohibits argument conversion to union type");
3747
3748           return build1 (NOP_EXPR, type, rhs);
3749         }
3750     }
3751
3752   /* Conversions among pointers */
3753   else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
3754            && (coder == codel))
3755     {
3756       tree ttl = TREE_TYPE (type);
3757       tree ttr = TREE_TYPE (rhstype);
3758       tree mvl = ttl;
3759       tree mvr = ttr;
3760       bool is_opaque_pointer;
3761       int target_cmp = 0;   /* Cache comp_target_types () result.  */
3762
3763       if (TREE_CODE (mvl) != ARRAY_TYPE)
3764         mvl = TYPE_MAIN_VARIANT (mvl);
3765       if (TREE_CODE (mvr) != ARRAY_TYPE)
3766         mvr = TYPE_MAIN_VARIANT (mvr);
3767       /* Opaque pointers are treated like void pointers.  */
3768       is_opaque_pointer = (targetm.vector_opaque_p (type)
3769                            || targetm.vector_opaque_p (rhstype))
3770         && TREE_CODE (ttl) == VECTOR_TYPE
3771         && TREE_CODE (ttr) == VECTOR_TYPE;
3772
3773       /* Any non-function converts to a [const][volatile] void *
3774          and vice versa; otherwise, targets must be the same.
3775          Meanwhile, the lhs target must have all the qualifiers of the rhs.  */
3776       if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3777           || (target_cmp = comp_target_types (type, rhstype, 0))
3778           || is_opaque_pointer
3779           || (c_common_unsigned_type (mvl)
3780               == c_common_unsigned_type (mvr)))
3781         {
3782           if (pedantic
3783               && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
3784                   ||
3785                   (VOID_TYPE_P (ttr)
3786                    /* Check TREE_CODE to catch cases like (void *) (char *) 0
3787                       which are not ANSI null ptr constants.  */
3788                    && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
3789                    && TREE_CODE (ttl) == FUNCTION_TYPE)))
3790             WARN_FOR_ASSIGNMENT (N_("ISO C forbids passing argument %d of "
3791                                     "%qE between function pointer "
3792                                     "and %<void *%>"),
3793                                  N_("ISO C forbids assignment between "
3794                                     "function pointer and %<void *%>"),
3795                                  N_("ISO C forbids initialization between "
3796                                     "function pointer and %<void *%>"),
3797                                  N_("ISO C forbids return between function "
3798                                     "pointer and %<void *%>"));
3799           /* Const and volatile mean something different for function types,
3800              so the usual warnings are not appropriate.  */
3801           else if (TREE_CODE (ttr) != FUNCTION_TYPE
3802                    && TREE_CODE (ttl) != FUNCTION_TYPE)
3803             {
3804               if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3805                 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE discards "
3806                                         "qualifiers from pointer target type"),
3807                                      N_("assignment discards qualifiers "
3808                                         "from pointer target type"),
3809                                      N_("initialization discards qualifiers "
3810                                         "from pointer target type"),
3811                                      N_("return discards qualifiers from "
3812                                         "pointer target type"));
3813               /* If this is not a case of ignoring a mismatch in signedness,
3814                  no warning.  */
3815               else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3816                        || target_cmp)
3817                 ;
3818               /* If there is a mismatch, do warn.  */
3819               else if (warn_pointer_sign)
3820                 WARN_FOR_ASSIGNMENT (N_("pointer targets in passing argument "
3821                                         "%d of %qE differ in signedness"),
3822                                      N_("pointer targets in assignment "
3823                                         "differ in signedness"),
3824                                      N_("pointer targets in initialization "
3825                                         "differ in signedness"),
3826                                      N_("pointer targets in return differ "
3827                                         "in signedness"));
3828             }
3829           else if (TREE_CODE (ttl) == FUNCTION_TYPE
3830                    && TREE_CODE (ttr) == FUNCTION_TYPE)
3831             {
3832               /* Because const and volatile on functions are restrictions
3833                  that say the function will not do certain things,
3834                  it is okay to use a const or volatile function
3835                  where an ordinary one is wanted, but not vice-versa.  */
3836               if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3837                 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE makes "
3838                                         "qualified function pointer "
3839                                         "from unqualified"),
3840                                      N_("assignment makes qualified function "
3841                                         "pointer from unqualified"),
3842                                      N_("initialization makes qualified "
3843                                         "function pointer from unqualified"),
3844                                      N_("return makes qualified function "
3845                                         "pointer from unqualified"));
3846             }
3847         }
3848       else
3849         WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE from "
3850                                 "incompatible pointer type"),
3851                              N_("assignment from incompatible pointer type"),
3852                              N_("initialization from incompatible "
3853                                 "pointer type"),
3854                              N_("return from incompatible pointer type"));
3855       return convert (type, rhs);
3856     }
3857   else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
3858     {
3859       /* ??? This should not be an error when inlining calls to
3860          unprototyped functions.  */
3861       error ("invalid use of non-lvalue array");
3862       return error_mark_node;
3863     }
3864   else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
3865     {
3866       /* An explicit constant 0 can convert to a pointer,
3867          or one that results from arithmetic, even including
3868          a cast to integer type.  */
3869       if (!(TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
3870           &&
3871           !(TREE_CODE (rhs) == NOP_EXPR
3872             && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
3873             && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
3874             && integer_zerop (TREE_OPERAND (rhs, 0))))
3875         WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE makes "
3876                                 "pointer from integer without a cast"),
3877                              N_("assignment makes pointer from integer "
3878                                 "without a cast"),
3879                              N_("initialization makes pointer from "
3880                                 "integer without a cast"),
3881                              N_("return makes pointer from integer "
3882                                 "without a cast"));
3883
3884       return convert (type, rhs);
3885     }
3886   else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
3887     {
3888       WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE makes integer "
3889                               "from pointer without a cast"),
3890                            N_("assignment makes integer from pointer "
3891                               "without a cast"),
3892                            N_("initialization makes integer from pointer "
3893                               "without a cast"),
3894                            N_("return makes integer from pointer "
3895                               "without a cast"));
3896       return convert (type, rhs);
3897     }
3898   else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
3899     return convert (type, rhs);
3900
3901   switch (errtype)
3902     {
3903     case ic_argpass:
3904     case ic_argpass_nonproto:
3905       /* ??? This should not be an error when inlining calls to
3906          unprototyped functions.  */
3907       error ("incompatible type for argument %d of %qE", parmnum, rname);
3908       break;
3909     case ic_assign:
3910       error ("incompatible types in assignment");
3911       break;
3912     case ic_init:
3913       error ("incompatible types in initialization");
3914       break;
3915     case ic_return:
3916       error ("incompatible types in return");
3917       break;
3918     default:
3919       gcc_unreachable ();
3920     }
3921
3922   return error_mark_node;
3923 }
3924
3925 /* Convert VALUE for assignment into inlined parameter PARM.  ARGNUM
3926    is used for error and waring reporting and indicates which argument
3927    is being processed.  */
3928
3929 tree
3930 c_convert_parm_for_inlining (tree parm, tree value, tree fn, int argnum)
3931 {
3932   tree ret, type;
3933
3934   /* If FN was prototyped, the value has been converted already
3935      in convert_arguments.  */
3936   if (!value || TYPE_ARG_TYPES (TREE_TYPE (fn)))
3937     return value;
3938
3939   type = TREE_TYPE (parm);
3940   ret = convert_for_assignment (type, value,
3941                                 ic_argpass_nonproto, fn,
3942                                 fn, argnum);
3943   if (targetm.calls.promote_prototypes (TREE_TYPE (fn))
3944       && INTEGRAL_TYPE_P (type)
3945       && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3946     ret = default_conversion (ret);
3947   return ret;
3948 }
3949 \f
3950 /* If VALUE is a compound expr all of whose expressions are constant, then
3951    return its value.  Otherwise, return error_mark_node.
3952
3953    This is for handling COMPOUND_EXPRs as initializer elements
3954    which is allowed with a warning when -pedantic is specified.  */
3955
3956 static tree
3957 valid_compound_expr_initializer (tree value, tree endtype)
3958