1/* Subroutines shared by all languages that are variants of C.
2   Copyright (C) 1992, 93-98, 1999 Free Software Foundation, Inc.
3
4This file is part of GNU CC.
5
6GNU CC is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU CC is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU CC; see the file COPYING.  If not, write to
18the Free Software Foundation, 59 Temple Place - Suite 330,
19Boston, MA 02111-1307, USA.  */
20
21#include "config.h"
22#include "system.h"
23#include "tree.h"
24#include "c-lex.h"
25#include "c-tree.h"
26#include "flags.h"
27#include "obstack.h"
28#include "toplev.h"
29#include "output.h"
30#include "c-pragma.h"
31#include "rtl.h"
32
33#if USE_CPPLIB
34#include "cpplib.h"
35cpp_reader  parse_in;
36cpp_options parse_options;
37static enum cpp_token cpp_token;
38#endif
39
40#ifndef WCHAR_TYPE_SIZE
41#ifdef INT_TYPE_SIZE
42#define WCHAR_TYPE_SIZE INT_TYPE_SIZE
43#else
44#define WCHAR_TYPE_SIZE	BITS_PER_WORD
45#endif
46#endif
47
48extern struct obstack permanent_obstack;
49
50/* Nonzero means the expression being parsed will never be evaluated.
51   This is a count, since unevaluated expressions can nest.  */
52int skip_evaluation;
53
54enum attrs {A_PACKED, A_NOCOMMON, A_COMMON, A_NORETURN, A_CONST, A_T_UNION,
55	    A_NO_CHECK_MEMORY_USAGE, A_NO_INSTRUMENT_FUNCTION,
56	    A_CONSTRUCTOR, A_DESTRUCTOR, A_MODE, A_SECTION, A_ALIGNED,
57	    A_UNUSED, A_FORMAT, A_FORMAT_ARG, A_WEAK, A_ALIAS};
58
59enum format_type { printf_format_type, scanf_format_type,
60		   strftime_format_type };
61
62static void declare_hidden_char_array	PROTO((const char *, const char *));
63static void add_attribute		PROTO((enum attrs, const char *,
64					       int, int, int));
65static void init_attributes		PROTO((void));
66static void record_function_format	PROTO((tree, tree, enum format_type,
67					       int, int));
68static void record_international_format	PROTO((tree, tree, int));
69static tree c_find_base_decl            PROTO((tree));
70static int default_valid_lang_attribute PROTO ((tree, tree, tree, tree));
71
72/* Keep a stack of if statements.  We record the number of compound
73   statements seen up to the if keyword, as well as the line number
74   and file of the if.  If a potentially ambiguous else is seen, that
75   fact is recorded; the warning is issued when we can be sure that
76   the enclosing if statement does not have an else branch.  */
77typedef struct
78{
79  int compstmt_count;
80  int line;
81  const char *file;
82  int needs_warning;
83} if_elt;
84static void tfaff			PROTO((void));
85
86static if_elt *if_stack;
87
88/* Amount of space in the if statement stack.  */
89static int if_stack_space = 0;
90
91/* Stack pointer.  */
92static int if_stack_pointer = 0;
93
94/* Generate RTL for the start of an if-then, and record the start of it
95   for ambiguous else detection.  */
96
97void
98c_expand_start_cond (cond, exitflag, compstmt_count)
99     tree cond;
100     int exitflag;
101     int compstmt_count;
102{
103  /* Make sure there is enough space on the stack.  */
104  if (if_stack_space == 0)
105    {
106      if_stack_space = 10;
107      if_stack = (if_elt *)xmalloc (10 * sizeof (if_elt));
108    }
109  else if (if_stack_space == if_stack_pointer)
110    {
111      if_stack_space += 10;
112      if_stack = (if_elt *)xrealloc (if_stack, if_stack_space * sizeof (if_elt));
113    }
114
115  /* Record this if statement.  */
116  if_stack[if_stack_pointer].compstmt_count = compstmt_count;
117  if_stack[if_stack_pointer].file = input_filename;
118  if_stack[if_stack_pointer].line = lineno;
119  if_stack[if_stack_pointer].needs_warning = 0;
120  if_stack_pointer++;
121
122  expand_start_cond (cond, exitflag);
123}
124
125/* Generate RTL for the end of an if-then.  Optionally warn if a nested
126   if statement had an ambiguous else clause.  */
127
128void
129c_expand_end_cond ()
130{
131  if_stack_pointer--;
132  if (if_stack[if_stack_pointer].needs_warning)
133    warning_with_file_and_line (if_stack[if_stack_pointer].file,
134				if_stack[if_stack_pointer].line,
135				"suggest explicit braces to avoid ambiguous `else'");
136  expand_end_cond ();
137}
138
139/* Generate RTL between the then-clause and the else-clause
140   of an if-then-else.  */
141
142void
143c_expand_start_else ()
144{
145  /* An ambiguous else warning must be generated for the enclosing if
146     statement, unless we see an else branch for that one, too.  */
147  if (warn_parentheses
148      && if_stack_pointer > 1
149      && (if_stack[if_stack_pointer - 1].compstmt_count
150	  == if_stack[if_stack_pointer - 2].compstmt_count))
151    if_stack[if_stack_pointer - 2].needs_warning = 1;
152
153  /* Even if a nested if statement had an else branch, it can't be
154     ambiguous if this one also has an else.  So don't warn in that
155     case.  Also don't warn for any if statements nested in this else.  */
156  if_stack[if_stack_pointer - 1].needs_warning = 0;
157  if_stack[if_stack_pointer - 1].compstmt_count--;
158
159  expand_start_else ();
160}
161
162/* Make bindings for __FUNCTION__, __PRETTY_FUNCTION__, and __func__.  */
163
164void
165declare_function_name ()
166{
167  const char *name, *printable_name;
168
169  if (current_function_decl == NULL)
170    {
171      name = "";
172      printable_name = "top level";
173    }
174  else
175    {
176      /* Allow functions to be nameless (such as artificial ones).  */
177      if (DECL_NAME (current_function_decl))
178        name = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
179      else
180	name = "";
181      printable_name = (*decl_printable_name) (current_function_decl, 2);
182    }
183
184  declare_hidden_char_array ("__FUNCTION__", name);
185  declare_hidden_char_array ("__PRETTY_FUNCTION__", printable_name);
186  /* The ISO C people "of course" couldn't use __FUNCTION__ in the
187     ISO C 9x standard; instead a new variable is invented.  */
188  declare_hidden_char_array ("__func__", name);
189}
190
191static void
192declare_hidden_char_array (name, value)
193     const char *name, *value;
194{
195  tree decl, type, init;
196  int vlen;
197
198  /* If the default size of char arrays isn't big enough for the name,
199     or if we want to give warnings for large objects, make a bigger one.  */
200  vlen = strlen (value) + 1;
201  type = char_array_type_node;
202  if (TREE_INT_CST_LOW (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) < vlen
203      || warn_larger_than)
204    type = build_array_type (char_type_node,
205			     build_index_type (build_int_2 (vlen, 0)));
206  push_obstacks_nochange ();
207  decl = build_decl (VAR_DECL, get_identifier (name), type);
208  TREE_STATIC (decl) = 1;
209  TREE_READONLY (decl) = 1;
210  TREE_ASM_WRITTEN (decl) = 1;
211  DECL_SOURCE_LINE (decl) = 0;
212  DECL_ARTIFICIAL (decl) = 1;
213  DECL_IN_SYSTEM_HEADER (decl) = 1;
214  DECL_IGNORED_P (decl) = 1;
215  init = build_string (vlen, value);
216  TREE_TYPE (init) = type;
217  DECL_INITIAL (decl) = init;
218  finish_decl (pushdecl (decl), init, NULL_TREE);
219}
220
221/* Given a chain of STRING_CST nodes,
222   concatenate them into one STRING_CST
223   and give it a suitable array-of-chars data type.  */
224
225tree
226combine_strings (strings)
227     tree strings;
228{
229  register tree value, t;
230  register int length = 1;
231  int wide_length = 0;
232  int wide_flag = 0;
233  int wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
234  int nchars;
235
236  if (TREE_CHAIN (strings))
237    {
238      /* More than one in the chain, so concatenate.  */
239      register char *p, *q;
240
241      /* Don't include the \0 at the end of each substring,
242	 except for the last one.
243	 Count wide strings and ordinary strings separately.  */
244      for (t = strings; t; t = TREE_CHAIN (t))
245	{
246	  if (TREE_TYPE (t) == wchar_array_type_node)
247	    {
248	      wide_length += (TREE_STRING_LENGTH (t) - wchar_bytes);
249	      wide_flag = 1;
250	    }
251	  else
252	    length += (TREE_STRING_LENGTH (t) - 1);
253	}
254
255      /* If anything is wide, the non-wides will be converted,
256	 which makes them take more space.  */
257      if (wide_flag)
258	length = length * wchar_bytes + wide_length;
259
260      p = savealloc (length);
261
262      /* Copy the individual strings into the new combined string.
263	 If the combined string is wide, convert the chars to ints
264	 for any individual strings that are not wide.  */
265
266      q = p;
267      for (t = strings; t; t = TREE_CHAIN (t))
268	{
269	  int len = (TREE_STRING_LENGTH (t)
270		     - ((TREE_TYPE (t) == wchar_array_type_node)
271			? wchar_bytes : 1));
272	  if ((TREE_TYPE (t) == wchar_array_type_node) == wide_flag)
273	    {
274	      memcpy (q, TREE_STRING_POINTER (t), len);
275	      q += len;
276	    }
277	  else
278	    {
279	      int i;
280	      for (i = 0; i < len; i++)
281		{
282		  if (WCHAR_TYPE_SIZE == HOST_BITS_PER_SHORT)
283		    ((short *) q)[i] = TREE_STRING_POINTER (t)[i];
284		  else
285		    ((int *) q)[i] = TREE_STRING_POINTER (t)[i];
286		}
287	      q += len * wchar_bytes;
288	    }
289	}
290      if (wide_flag)
291	{
292	  int i;
293	  for (i = 0; i < wchar_bytes; i++)
294	    *q++ = 0;
295	}
296      else
297	*q = 0;
298
299      value = make_node (STRING_CST);
300      TREE_STRING_POINTER (value) = p;
301      TREE_STRING_LENGTH (value) = length;
302    }
303  else
304    {
305      value = strings;
306      length = TREE_STRING_LENGTH (value);
307      if (TREE_TYPE (value) == wchar_array_type_node)
308	wide_flag = 1;
309    }
310
311  /* Compute the number of elements, for the array type.  */
312  nchars = wide_flag ? length / wchar_bytes : length;
313
314  /* Create the array type for the string constant.
315     -Wwrite-strings says make the string constant an array of const char
316     so that copying it to a non-const pointer will get a warning.
317     For C++, this is the standard behavior.  */
318  if (flag_const_strings
319      && (! flag_traditional  && ! flag_writable_strings))
320    {
321      tree elements
322	= build_type_variant (wide_flag ? wchar_type_node : char_type_node,
323			      1, 0);
324      TREE_TYPE (value)
325	= build_array_type (elements,
326			    build_index_type (build_int_2 (nchars - 1, 0)));
327    }
328  else
329    TREE_TYPE (value)
330      = build_array_type (wide_flag ? wchar_type_node : char_type_node,
331			  build_index_type (build_int_2 (nchars - 1, 0)));
332
333  TREE_CONSTANT (value) = 1;
334  TREE_READONLY (value) = ! flag_writable_strings;
335  TREE_STATIC (value) = 1;
336  return value;
337}
338
339/* To speed up processing of attributes, we maintain an array of
340   IDENTIFIER_NODES and the corresponding attribute types.  */
341
342/* Array to hold attribute information.  */
343
344static struct {enum attrs id; tree name; int min, max, decl_req;} attrtab[50];
345
346static int attrtab_idx = 0;
347
348/* Add an entry to the attribute table above.  */
349
350static void
351add_attribute (id, string, min_len, max_len, decl_req)
352     enum attrs id;
353     const char *string;
354     int min_len, max_len;
355     int decl_req;
356{
357  char buf[100];
358
359  attrtab[attrtab_idx].id = id;
360  attrtab[attrtab_idx].name = get_identifier (string);
361  attrtab[attrtab_idx].min = min_len;
362  attrtab[attrtab_idx].max = max_len;
363  attrtab[attrtab_idx++].decl_req = decl_req;
364
365  sprintf (buf, "__%s__", string);
366
367  attrtab[attrtab_idx].id = id;
368  attrtab[attrtab_idx].name = get_identifier (buf);
369  attrtab[attrtab_idx].min = min_len;
370  attrtab[attrtab_idx].max = max_len;
371  attrtab[attrtab_idx++].decl_req = decl_req;
372}
373
374/* Initialize attribute table.  */
375
376static void
377init_attributes ()
378{
379  add_attribute (A_PACKED, "packed", 0, 0, 0);
380  add_attribute (A_NOCOMMON, "nocommon", 0, 0, 1);
381  add_attribute (A_COMMON, "common", 0, 0, 1);
382  add_attribute (A_NORETURN, "noreturn", 0, 0, 1);
383  add_attribute (A_NORETURN, "volatile", 0, 0, 1);
384  add_attribute (A_UNUSED, "unused", 0, 0, 0);
385  add_attribute (A_CONST, "const", 0, 0, 1);
386  add_attribute (A_T_UNION, "transparent_union", 0, 0, 0);
387  add_attribute (A_CONSTRUCTOR, "constructor", 0, 0, 1);
388  add_attribute (A_DESTRUCTOR, "destructor", 0, 0, 1);
389  add_attribute (A_MODE, "mode", 1, 1, 1);
390  add_attribute (A_SECTION, "section", 1, 1, 1);
391  add_attribute (A_ALIGNED, "aligned", 0, 1, 0);
392  add_attribute (A_FORMAT, "format", 3, 3, 1);
393  add_attribute (A_FORMAT_ARG, "format_arg", 1, 1, 1);
394  add_attribute (A_WEAK, "weak", 0, 0, 1);
395  add_attribute (A_ALIAS, "alias", 1, 1, 1);
396  add_attribute (A_NO_INSTRUMENT_FUNCTION, "no_instrument_function", 0, 0, 1);
397  add_attribute (A_NO_CHECK_MEMORY_USAGE, "no_check_memory_usage", 0, 0, 1);
398}
399
400/* Default implementation of valid_lang_attribute, below.  By default, there
401   are no language-specific attributes.  */
402
403static int
404default_valid_lang_attribute (attr_name, attr_args, decl, type)
405  tree attr_name ATTRIBUTE_UNUSED;
406  tree attr_args ATTRIBUTE_UNUSED;
407  tree decl ATTRIBUTE_UNUSED;
408  tree type ATTRIBUTE_UNUSED;
409{
410  return 0;
411}
412
413/* Return a 1 if ATTR_NAME and ATTR_ARGS denote a valid language-specific
414   attribute for either declaration DECL or type TYPE and 0 otherwise.  */
415
416int (*valid_lang_attribute) PROTO ((tree, tree, tree, tree))
417     = default_valid_lang_attribute;
418
419/* Process the attributes listed in ATTRIBUTES and PREFIX_ATTRIBUTES
420   and install them in NODE, which is either a DECL (including a TYPE_DECL)
421   or a TYPE.  PREFIX_ATTRIBUTES can appear after the declaration specifiers
422   and declaration modifiers but before the declaration proper.  */
423
424void
425decl_attributes (node, attributes, prefix_attributes)
426     tree node, attributes, prefix_attributes;
427{
428  tree decl = 0, type = 0;
429  int is_type = 0;
430  tree a;
431
432  if (attrtab_idx == 0)
433    init_attributes ();
434
435  if (TREE_CODE_CLASS (TREE_CODE (node)) == 'd')
436    {
437      decl = node;
438      type = TREE_TYPE (decl);
439      is_type = TREE_CODE (node) == TYPE_DECL;
440    }
441  else if (TREE_CODE_CLASS (TREE_CODE (node)) == 't')
442    type = node, is_type = 1;
443
444#ifdef PRAGMA_INSERT_ATTRIBUTES
445  /* If the code in c-pragma.c wants to insert some attributes then
446     allow it to do so.  Do this before allowing machine back ends to
447     insert attributes, so that they have the opportunity to override
448     anything done here.  */
449  PRAGMA_INSERT_ATTRIBUTES (node, & attributes, & prefix_attributes);
450#endif
451
452#ifdef INSERT_ATTRIBUTES
453  INSERT_ATTRIBUTES (node, & attributes, & prefix_attributes);
454#endif
455
456  attributes = chainon (prefix_attributes, attributes);
457
458  for (a = attributes; a; a = TREE_CHAIN (a))
459    {
460      tree name = TREE_PURPOSE (a);
461      tree args = TREE_VALUE (a);
462      int i;
463      enum attrs id;
464
465      for (i = 0; i < attrtab_idx; i++)
466	if (attrtab[i].name == name)
467	  break;
468
469      if (i == attrtab_idx)
470	{
471	  if (! valid_machine_attribute (name, args, decl, type)
472	      && ! (* valid_lang_attribute) (name, args, decl, type))
473	    warning ("`%s' attribute directive ignored",
474		     IDENTIFIER_POINTER (name));
475	  else if (decl != 0)
476	    type = TREE_TYPE (decl);
477	  continue;
478	}
479      else if (attrtab[i].decl_req && decl == 0)
480	{
481	  warning ("`%s' attribute does not apply to types",
482		   IDENTIFIER_POINTER (name));
483	  continue;
484	}
485      else if (list_length (args) < attrtab[i].min
486	       || list_length (args) > attrtab[i].max)
487	{
488	  error ("wrong number of arguments specified for `%s' attribute",
489		 IDENTIFIER_POINTER (name));
490	  continue;
491	}
492
493      id = attrtab[i].id;
494      switch (id)
495	{
496	case A_PACKED:
497	  if (is_type)
498	    TYPE_PACKED (type) = 1;
499	  else if (TREE_CODE (decl) == FIELD_DECL)
500	    DECL_PACKED (decl) = 1;
501	  /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
502	     used for DECL_REGISTER.  It wouldn't mean anything anyway.  */
503	  else
504	    warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
505	  break;
506
507	case A_NOCOMMON:
508	  if (TREE_CODE (decl) == VAR_DECL)
509	    DECL_COMMON (decl) = 0;
510	  else
511	    warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
512	  break;
513
514	case A_COMMON:
515	  if (TREE_CODE (decl) == VAR_DECL)
516	    DECL_COMMON (decl) = 1;
517	  else
518	    warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
519	  break;
520
521	case A_NORETURN:
522	  if (TREE_CODE (decl) == FUNCTION_DECL)
523	    TREE_THIS_VOLATILE (decl) = 1;
524	  else if (TREE_CODE (type) == POINTER_TYPE
525		   && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
526	    TREE_TYPE (decl) = type
527	      = build_pointer_type
528		(build_type_variant (TREE_TYPE (type),
529				     TREE_READONLY (TREE_TYPE (type)), 1));
530	  else
531	    warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
532	  break;
533
534	case A_UNUSED:
535	  if (is_type)
536	    TREE_USED (type) = 1;
537	  else if (TREE_CODE (decl) == PARM_DECL
538		   || TREE_CODE (decl) == VAR_DECL
539		   || TREE_CODE (decl) == FUNCTION_DECL
540		   || TREE_CODE (decl) == LABEL_DECL)
541	    TREE_USED (decl) = 1;
542	  else
543	    warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
544	  break;
545
546	case A_CONST:
547	  if (TREE_CODE (decl) == FUNCTION_DECL)
548	    TREE_READONLY (decl) = 1;
549	  else if (TREE_CODE (type) == POINTER_TYPE
550		   && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
551	    TREE_TYPE (decl) = type
552	      = build_pointer_type
553		(build_type_variant (TREE_TYPE (type), 1,
554				     TREE_THIS_VOLATILE (TREE_TYPE (type))));
555	  else
556	    warning ( "`%s' attribute ignored", IDENTIFIER_POINTER (name));
557	  break;
558
559	case A_T_UNION:
560	  if (is_type
561	      && TREE_CODE (type) == UNION_TYPE
562	      && (decl == 0
563		  || (TYPE_FIELDS (type) != 0
564		      && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))))
565	    TYPE_TRANSPARENT_UNION (type) = 1;
566	  else if (decl != 0 && TREE_CODE (decl) == PARM_DECL
567		   && TREE_CODE (type) == UNION_TYPE
568		   && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))
569	    DECL_TRANSPARENT_UNION (decl) = 1;
570	  else
571	    warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
572	  break;
573
574	case A_CONSTRUCTOR:
575	  if (TREE_CODE (decl) == FUNCTION_DECL
576	      && TREE_CODE (type) == FUNCTION_TYPE
577	      && decl_function_context (decl) == 0)
578	    {
579	      DECL_STATIC_CONSTRUCTOR (decl) = 1;
580	      TREE_USED (decl) = 1;
581	    }
582	  else
583	    warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
584	  break;
585
586	case A_DESTRUCTOR:
587	  if (TREE_CODE (decl) == FUNCTION_DECL
588	      && TREE_CODE (type) == FUNCTION_TYPE
589	      && decl_function_context (decl) == 0)
590	    {
591	      DECL_STATIC_DESTRUCTOR (decl) = 1;
592	      TREE_USED (decl) = 1;
593	    }
594	  else
595	    warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
596	  break;
597
598	case A_MODE:
599	  if (TREE_CODE (TREE_VALUE (args)) != IDENTIFIER_NODE)
600	    warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
601	  else
602	    {
603	      int j;
604	      const char *p = IDENTIFIER_POINTER (TREE_VALUE (args));
605	      int len = strlen (p);
606	      enum machine_mode mode = VOIDmode;
607	      tree typefm;
608
609	      if (len > 4 && p[0] == '_' && p[1] == '_'
610		  && p[len - 1] == '_' && p[len - 2] == '_')
611		{
612		  char *newp = (char *) alloca (len - 1);
613
614		  strcpy (newp, &p[2]);
615		  newp[len - 4] = '\0';
616		  p = newp;
617		}
618
619	      /* Give this decl a type with the specified mode.
620		 First check for the special modes.  */
621	      if (! strcmp (p, "byte"))
622		mode = byte_mode;
623	      else if (!strcmp (p, "word"))
624		mode = word_mode;
625	      else if (! strcmp (p, "pointer"))
626		mode = ptr_mode;
627	      else
628		for (j = 0; j < NUM_MACHINE_MODES; j++)
629		  if (!strcmp (p, GET_MODE_NAME (j)))
630		    mode = (enum machine_mode) j;
631
632	      if (mode == VOIDmode)
633		error ("unknown machine mode `%s'", p);
634	      else if (0 == (typefm = type_for_mode (mode,
635						     TREE_UNSIGNED (type))))
636		error ("no data type for mode `%s'", p);
637	      else
638		{
639		  TREE_TYPE (decl) = type = typefm;
640		  DECL_SIZE (decl) = 0;
641		  layout_decl (decl, 0);
642		}
643	    }
644	  break;
645
646	case A_SECTION:
647#ifdef ASM_OUTPUT_SECTION_NAME
648	  if ((TREE_CODE (decl) == FUNCTION_DECL
649	       || TREE_CODE (decl) == VAR_DECL)
650	      && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
651	    {
652	      if (TREE_CODE (decl) == VAR_DECL
653		  && current_function_decl != NULL_TREE
654		  && ! TREE_STATIC (decl))
655		error_with_decl (decl,
656		  "section attribute cannot be specified for local variables");
657	      /* The decl may have already been given a section attribute from
658		 a previous declaration.  Ensure they match.  */
659	      else if (DECL_SECTION_NAME (decl) != NULL_TREE
660		       && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
661				  TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
662		error_with_decl (node,
663				 "section of `%s' conflicts with previous declaration");
664	      else
665		DECL_SECTION_NAME (decl) = TREE_VALUE (args);
666	    }
667	  else
668	    error_with_decl (node,
669			   "section attribute not allowed for `%s'");
670#else
671	  error_with_decl (node,
672		  "section attributes are not supported for this target");
673#endif
674	  break;
675
676	case A_ALIGNED:
677	  {
678	    tree align_expr
679	      = (args ? TREE_VALUE (args)
680		 : size_int (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
681	    int align;
682
683	    /* Strip any NOPs of any kind.  */
684	    while (TREE_CODE (align_expr) == NOP_EXPR
685		   || TREE_CODE (align_expr) == CONVERT_EXPR
686		   || TREE_CODE (align_expr) == NON_LVALUE_EXPR)
687	      align_expr = TREE_OPERAND (align_expr, 0);
688
689	    if (TREE_CODE (align_expr) != INTEGER_CST)
690	      {
691		error ("requested alignment is not a constant");
692		continue;
693	      }
694
695	    align = TREE_INT_CST_LOW (align_expr) * BITS_PER_UNIT;
696
697	    if (exact_log2 (align) == -1)
698	      error ("requested alignment is not a power of 2");
699	    else if (is_type)
700	      TYPE_ALIGN (type) = align;
701	    else if (TREE_CODE (decl) != VAR_DECL
702		     && TREE_CODE (decl) != FIELD_DECL)
703	      error_with_decl (decl,
704			       "alignment may not be specified for `%s'");
705	    else
706	      DECL_ALIGN (decl) = align;
707	  }
708	  break;
709
710	case A_FORMAT:
711	  {
712	    tree format_type_id = TREE_VALUE (args);
713	    tree format_num_expr = TREE_VALUE (TREE_CHAIN (args));
714	    tree first_arg_num_expr
715	      = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)));
716	    int format_num;
717	    int first_arg_num;
718	    enum format_type format_type;
719	    tree argument;
720	    int arg_num;
721
722	    if (TREE_CODE (decl) != FUNCTION_DECL)
723	      {
724		error_with_decl (decl,
725			 "argument format specified for non-function `%s'");
726		continue;
727	      }
728
729	    if (TREE_CODE (format_type_id) != IDENTIFIER_NODE)
730	      {
731		error ("unrecognized format specifier");
732		continue;
733	      }
734	    else
735	      {
736		const char *p = IDENTIFIER_POINTER (format_type_id);
737
738		if (!strcmp (p, "printf") || !strcmp (p, "__printf__"))
739		  format_type = printf_format_type;
740		else if (!strcmp (p, "scanf") || !strcmp (p, "__scanf__"))
741		  format_type = scanf_format_type;
742		else if (!strcmp (p, "strftime")
743			 || !strcmp (p, "__strftime__"))
744		  format_type = strftime_format_type;
745		else
746		  {
747		    warning ("`%s' is an unrecognized format function type", p);
748		    continue;
749		  }
750	      }
751
752	    /* Strip any conversions from the string index and first arg number
753	       and verify they are constants.  */
754	    while (TREE_CODE (format_num_expr) == NOP_EXPR
755		   || TREE_CODE (format_num_expr) == CONVERT_EXPR
756		   || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
757	      format_num_expr = TREE_OPERAND (format_num_expr, 0);
758
759	    while (TREE_CODE (first_arg_num_expr) == NOP_EXPR
760		   || TREE_CODE (first_arg_num_expr) == CONVERT_EXPR
761		   || TREE_CODE (first_arg_num_expr) == NON_LVALUE_EXPR)
762	      first_arg_num_expr = TREE_OPERAND (first_arg_num_expr, 0);
763
764	    if (TREE_CODE (format_num_expr) != INTEGER_CST
765		|| TREE_CODE (first_arg_num_expr) != INTEGER_CST)
766	      {
767		error ("format string has non-constant operand number");
768		continue;
769	      }
770
771	    format_num = TREE_INT_CST_LOW (format_num_expr);
772	    first_arg_num = TREE_INT_CST_LOW (first_arg_num_expr);
773	    if (first_arg_num != 0 && first_arg_num <= format_num)
774	      {
775		error ("format string arg follows the args to be formatted");
776		continue;
777	      }
778
779	    /* If a parameter list is specified, verify that the format_num
780	       argument is actually a string, in case the format attribute
781	       is in error.  */
782	    argument = TYPE_ARG_TYPES (type);
783	    if (argument)
784	      {
785		for (arg_num = 1; ; ++arg_num)
786		  {
787		    if (argument == 0 || arg_num == format_num)
788		      break;
789		    argument = TREE_CHAIN (argument);
790		  }
791		if (! argument
792		    || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
793		  || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
794		      != char_type_node))
795		  {
796		    error ("format string arg not a string type");
797		    continue;
798		  }
799		if (first_arg_num != 0)
800		  {
801		    /* Verify that first_arg_num points to the last arg,
802		       the ...  */
803		    while (argument)
804		      arg_num++, argument = TREE_CHAIN (argument);
805		  if (arg_num != first_arg_num)
806		    {
807		      error ("args to be formatted is not ...");
808		      continue;
809		    }
810		  }
811	      }
812
813	    record_function_format (DECL_NAME (decl),
814				    DECL_ASSEMBLER_NAME (decl),
815				    format_type, format_num, first_arg_num);
816	    break;
817	  }
818
819	case A_FORMAT_ARG:
820	  {
821	    tree format_num_expr = TREE_VALUE (args);
822	    int format_num, arg_num;
823	    tree argument;
824
825	    if (TREE_CODE (decl) != FUNCTION_DECL)
826	      {
827		error_with_decl (decl,
828			 "argument format specified for non-function `%s'");
829		continue;
830	      }
831
832	    /* Strip any conversions from the first arg number and verify it
833	       is a constant.  */
834	    while (TREE_CODE (format_num_expr) == NOP_EXPR
835		   || TREE_CODE (format_num_expr) == CONVERT_EXPR
836		   || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
837	      format_num_expr = TREE_OPERAND (format_num_expr, 0);
838
839	    if (TREE_CODE (format_num_expr) != INTEGER_CST)
840	      {
841		error ("format string has non-constant operand number");
842		continue;
843	      }
844
845	    format_num = TREE_INT_CST_LOW (format_num_expr);
846
847	    /* If a parameter list is specified, verify that the format_num
848	       argument is actually a string, in case the format attribute
849	       is in error.  */
850	    argument = TYPE_ARG_TYPES (type);
851	    if (argument)
852	      {
853		for (arg_num = 1; ; ++arg_num)
854		  {
855		    if (argument == 0 || arg_num == format_num)
856		      break;
857		    argument = TREE_CHAIN (argument);
858		  }
859		if (! argument
860		    || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
861		  || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
862		      != char_type_node))
863		  {
864		    error ("format string arg not a string type");
865		    continue;
866		  }
867	      }
868
869	    if (TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) != POINTER_TYPE
870		|| (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (TREE_TYPE (decl))))
871		    != char_type_node))
872	      {
873		error ("function does not return string type");
874		continue;
875	      }
876
877	    record_international_format (DECL_NAME (decl),
878					 DECL_ASSEMBLER_NAME (decl),
879					 format_num);
880	    break;
881	  }
882
883	case A_WEAK:
884	  declare_weak (decl);
885	  break;
886
887	case A_ALIAS:
888	  if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
889	      || (TREE_CODE (decl) != FUNCTION_DECL && ! DECL_EXTERNAL (decl)))
890	    error_with_decl (decl,
891			     "`%s' defined both normally and as an alias");
892	  else if (decl_function_context (decl) == 0)
893	    {
894	      tree id;
895
896	      id = TREE_VALUE (args);
897	      if (TREE_CODE (id) != STRING_CST)
898		{
899		  error ("alias arg not a string");
900		  break;
901		}
902	      id = get_identifier (TREE_STRING_POINTER (id));
903
904	      if (TREE_CODE (decl) == FUNCTION_DECL)
905		DECL_INITIAL (decl) = error_mark_node;
906	      else
907		DECL_EXTERNAL (decl) = 0;
908	      assemble_alias (decl, id);
909	    }
910	  else
911	    warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
912	  break;
913
914	case A_NO_CHECK_MEMORY_USAGE:
915	  if (TREE_CODE (decl) != FUNCTION_DECL)
916	    {
917	      error_with_decl (decl,
918			       "`%s' attribute applies only to functions",
919			       IDENTIFIER_POINTER (name));
920	    }
921	  else if (DECL_INITIAL (decl))
922	    {
923	      error_with_decl (decl,
924			       "can't set `%s' attribute after definition",
925			       IDENTIFIER_POINTER (name));
926	    }
927	  else
928	    DECL_NO_CHECK_MEMORY_USAGE (decl) = 1;
929	  break;
930
931	case A_NO_INSTRUMENT_FUNCTION:
932	  if (TREE_CODE (decl) != FUNCTION_DECL)
933	    {
934	      error_with_decl (decl,
935			       "`%s' attribute applies only to functions",
936			       IDENTIFIER_POINTER (name));
937	    }
938	  else if (DECL_INITIAL (decl))
939	    {
940	      error_with_decl (decl,
941			       "can't set `%s' attribute after definition",
942			       IDENTIFIER_POINTER (name));
943	    }
944	  else
945	    DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
946	  break;
947	}
948    }
949}
950
951/* Split SPECS_ATTRS, a list of declspecs and prefix attributes, into two
952   lists.  SPECS_ATTRS may also be just a typespec (eg: RECORD_TYPE).
953
954   The head of the declspec list is stored in DECLSPECS.
955   The head of the attribute list is stored in PREFIX_ATTRIBUTES.
956
957   Note that attributes in SPECS_ATTRS are stored in the TREE_PURPOSE of
958   the list elements.  We drop the containing TREE_LIST nodes and link the
959   resulting attributes together the way decl_attributes expects them.  */
960
961void
962split_specs_attrs (specs_attrs, declspecs, prefix_attributes)
963     tree specs_attrs;
964     tree *declspecs, *prefix_attributes;
965{
966  tree t, s, a, next, specs, attrs;
967
968  /* This can happen after an __extension__ in pedantic mode.  */
969  if (specs_attrs != NULL_TREE
970      && TREE_CODE (specs_attrs) == INTEGER_CST)
971    {
972      *declspecs = NULL_TREE;
973      *prefix_attributes = NULL_TREE;
974      return;
975    }
976
977  /* This can happen in c++ (eg: decl: typespec initdecls ';').  */
978  if (specs_attrs != NULL_TREE
979      && TREE_CODE (specs_attrs) != TREE_LIST)
980    {
981      *declspecs = specs_attrs;
982      *prefix_attributes = NULL_TREE;
983      return;
984    }
985
986  /* Remember to keep the lists in the same order, element-wise.  */
987
988  specs = s = NULL_TREE;
989  attrs = a = NULL_TREE;
990  for (t = specs_attrs; t; t = next)
991    {
992      next = TREE_CHAIN (t);
993      /* Declspecs have a non-NULL TREE_VALUE.  */
994      if (TREE_VALUE (t) != NULL_TREE)
995	{
996	  if (specs == NULL_TREE)
997	    specs = s = t;
998	  else
999	    {
1000	      TREE_CHAIN (s) = t;
1001	      s = t;
1002	    }
1003	}
1004      else
1005	{
1006	  if (attrs == NULL_TREE)
1007	    attrs = a = TREE_PURPOSE (t);
1008	  else
1009	    {
1010	      TREE_CHAIN (a) = TREE_PURPOSE (t);
1011	      a = TREE_PURPOSE (t);
1012	    }
1013	  /* More attrs can be linked here, move A to the end.  */
1014	  while (TREE_CHAIN (a) != NULL_TREE)
1015	    a = TREE_CHAIN (a);
1016	}
1017    }
1018
1019  /* Terminate the lists.  */
1020  if (s != NULL_TREE)
1021    TREE_CHAIN (s) = NULL_TREE;
1022  if (a != NULL_TREE)
1023    TREE_CHAIN (a) = NULL_TREE;
1024
1025  /* All done.  */
1026  *declspecs = specs;
1027  *prefix_attributes = attrs;
1028}
1029
1030/* Strip attributes from SPECS_ATTRS, a list of declspecs and attributes.
1031   This function is used by the parser when a rule will accept attributes
1032   in a particular position, but we don't want to support that just yet.
1033
1034   A warning is issued for every ignored attribute.  */
1035
1036tree
1037strip_attrs (specs_attrs)
1038     tree specs_attrs;
1039{
1040  tree specs, attrs;
1041
1042  split_specs_attrs (specs_attrs, &specs, &attrs);
1043
1044  while (attrs)
1045    {
1046      warning ("`%s' attribute ignored",
1047	       IDENTIFIER_POINTER (TREE_PURPOSE (attrs)));
1048      attrs = TREE_CHAIN (attrs);
1049    }
1050
1051  return specs;
1052}
1053
1054/* Check a printf/fprintf/sprintf/scanf/fscanf/sscanf format against
1055   a parameter list.  */
1056
1057#define T_I	&integer_type_node
1058#define T_L	&long_integer_type_node
1059#define T_LL	&long_long_integer_type_node
1060#define T_S	&short_integer_type_node
1061#define T_UI	&unsigned_type_node
1062#define T_UL	&long_unsigned_type_node
1063#define T_ULL	&long_long_unsigned_type_node
1064#define T_US	&short_unsigned_type_node
1065#define T_F	&float_type_node
1066#define T_D	&double_type_node
1067#define T_LD	&long_double_type_node
1068#define T_C	&char_type_node
1069#define T_UC	&unsigned_char_type_node
1070#define T_V	&void_type_node
1071#define T_W	&wchar_type_node
1072#define T_ST    &sizetype
1073
1074typedef struct {
1075  const char *format_chars;
1076  int pointer_count;
1077  /* Type of argument if no length modifier is used.  */
1078  tree *nolen;
1079  /* Type of argument if length modifier for shortening to byte is used.
1080     If NULL, then this modifier is not allowed.  */
1081  tree *hhlen;
1082  /* Type of argument if length modifier for shortening is used.
1083     If NULL, then this modifier is not allowed.  */
1084  tree *hlen;
1085  /* Type of argument if length modifier `l' is used.
1086     If NULL, then this modifier is not allowed.  */
1087  tree *llen;
1088  /* Type of argument if length modifier `q' or `ll' is used.
1089     If NULL, then this modifier is not allowed.  */
1090  tree *qlen;
1091  /* Type of argument if length modifier `L' is used.
1092     If NULL, then this modifier is not allowed.  */
1093  tree *bigllen;
1094  /* Type of argument if length modifier `Z' or 'z' is used.
1095     If NULL, then this modifier is not allowed.  */
1096  tree *zlen;
1097  /* List of other modifier characters allowed with these options.  */
1098  const char *flag_chars;
1099} format_char_info;
1100
1101static format_char_info print_char_table[] = {
1102  { "di",	0,	T_I,	T_I,	T_I,	T_L,	T_LL,	T_LL,	T_ST,	"-wp0 +"	},
1103  { "oxX",	0,	T_UI,	T_UI,	T_UI,	T_UL,	T_ULL,	T_ULL,	T_ST,	"-wp0#"		},
1104  { "u",	0,	T_UI,	T_UI,	T_UI,	T_UL,	T_ULL,	T_ULL,	T_ST,	"-wp0"		},
1105/* A GNU extension.  */
1106  { "m",	0,	T_V,	NULL,	NULL,	NULL,	NULL,	NULL,	NULL,	"-wp"		},
1107  { "feEgGaA",	0,	T_D,	NULL,	NULL,	NULL,	NULL,	T_LD,	NULL,	"-wp0 +#"	},
1108  { "c",	0,	T_I,	NULL,	NULL,	T_W,	NULL,	NULL,	NULL,	"-w"		},
1109  { "C",	0,	T_W,	NULL,	NULL,	NULL,	NULL,	NULL,	NULL,	"-w"		},
1110  { "s",	1,	T_C,	NULL,	NULL,	T_W,	NULL,	NULL,	NULL,	"-wp"		},
1111  { "S",	1,	T_W,	NULL,	NULL,	NULL,	NULL,	NULL,	NULL,	"-wp"		},
1112  { "p",	1,	T_V,	NULL,	NULL,	NULL,	NULL,	NULL,	NULL,	"-w"		},
1113  { "n",	1,	T_I,	NULL,	T_S,	T_L,	T_LL,	NULL,	NULL,	""		},
1114  { NULL,	0,	NULL,	NULL,	NULL,	NULL,	NULL,	NULL,	NULL,	NULL		}
1115};
1116
1117static format_char_info scan_char_table[] = {
1118  { "di",	1,	T_I,	T_C,	T_S,	T_L,	T_LL,	T_LL,	NULL,	"*"	},
1119  { "ouxX",	1,	T_UI,	T_UC,	T_US,	T_UL,	T_ULL,	T_ULL,	NULL,	"*"	},
1120  { "efgEGaA",	1,	T_F,	NULL,	NULL,	T_D,	NULL,	T_LD,	NULL,	"*"	},
1121  { "c",	1,	T_C,	NULL,	NULL,	T_W,	NULL,	NULL,	NULL,	"*"	},
1122  { "s",	1,	T_C,	NULL,	NULL,	T_W,	NULL,	NULL,	NULL,	"*a"	},
1123  { "[",	1,	T_C,	NULL,	NULL,	NULL,	NULL,	NULL,	NULL,	"*a"	},
1124  { "C",	1,	T_W,	NULL,	NULL,	NULL,	NULL,	NULL,	NULL,	"*"	},
1125  { "S",	1,	T_W,	NULL,	NULL,	NULL,	NULL,	NULL,	NULL,	"*a"	},
1126  { "p",	2,	T_V,	NULL,	NULL,	NULL,	NULL,	NULL,	NULL,	"*"	},
1127  { "n",	1,	T_I,	T_C,	T_S,	T_L,	T_LL,	NULL,	NULL,	""	},
1128  { NULL,	0,	NULL,	NULL,	NULL,	NULL,	NULL,	NULL,	NULL,	NULL	}
1129};
1130
1131/* Handle format characters recognized by glibc's strftime.c.
1132   '2' - MUST do years as only two digits
1133   '3' - MAY do years as only two digits (depending on locale)
1134   'E' - E modifier is acceptable
1135   'O' - O modifier is acceptable to Standard C
1136   'o' - O modifier is acceptable as a GNU extension
1137   'G' - other GNU extensions  */
1138
1139static format_char_info time_char_table[] = {
1140  { "y", 		0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "2EO-_0w" },
1141  { "D", 		0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "2" },
1142  { "g", 		0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "2O-_0w" },
1143  { "cx", 		0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "3E" },
1144  { "%RTXnrt",		0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "" },
1145  { "P",		0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "G" },
1146  { "HIMSUWdemw",	0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0Ow" },
1147  { "Vju",		0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0Oow" },
1148  { "Gklsz",		0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0OGw" },
1149  { "ABZa",		0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "^#" },
1150  { "p",		0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "#" },
1151  { "bh",		0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "^" },
1152  { "CY",		0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0EOw" },
1153  { NULL,		0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
1154};
1155
1156typedef struct function_format_info
1157{
1158  struct function_format_info *next;  /* next structure on the list */
1159  tree name;			/* identifier such as "printf" */
1160  tree assembler_name;		/* optional mangled identifier (for C++) */
1161  enum format_type format_type;	/* type of format (printf, scanf, etc.) */
1162  int format_num;		/* number of format argument */
1163  int first_arg_num;		/* number of first arg (zero for varargs) */
1164} function_format_info;
1165
1166static function_format_info *function_format_list = NULL;
1167
1168typedef struct international_format_info
1169{
1170  struct international_format_info *next;  /* next structure on the list */
1171  tree name;			/* identifier such as "gettext" */
1172  tree assembler_name;		/* optional mangled identifier (for C++) */
1173  int format_num;		/* number of format argument */
1174} international_format_info;
1175
1176static international_format_info *international_format_list = NULL;
1177
1178static void check_format_info		PROTO((function_format_info *, tree));
1179
1180/* Initialize the table of functions to perform format checking on.
1181   The ANSI functions are always checked (whether <stdio.h> is
1182   included or not), since it is common to call printf without
1183   including <stdio.h>.  There shouldn't be a problem with this,
1184   since ANSI reserves these function names whether you include the
1185   header file or not.  In any case, the checking is harmless.
1186
1187   Also initialize the name of function that modify the format string for
1188   internationalization purposes.  */
1189
1190void
1191init_function_format_info ()
1192{
1193  record_function_format (get_identifier ("printf"), NULL_TREE,
1194			  printf_format_type, 1, 2);
1195  record_function_format (get_identifier ("fprintf"), NULL_TREE,
1196			  printf_format_type, 2, 3);
1197  record_function_format (get_identifier ("sprintf"), NULL_TREE,
1198			  printf_format_type, 2, 3);
1199  record_function_format (get_identifier ("scanf"), NULL_TREE,
1200			  scanf_format_type, 1, 2);
1201  record_function_format (get_identifier ("fscanf"), NULL_TREE,
1202			  scanf_format_type, 2, 3);
1203  record_function_format (get_identifier ("sscanf"), NULL_TREE,
1204			  scanf_format_type, 2, 3);
1205  record_function_format (get_identifier ("vprintf"), NULL_TREE,
1206			  printf_format_type, 1, 0);
1207  record_function_format (get_identifier ("vfprintf"), NULL_TREE,
1208			  printf_format_type, 2, 0);
1209  record_function_format (get_identifier ("vsprintf"), NULL_TREE,
1210			  printf_format_type, 2, 0);
1211  record_function_format (get_identifier ("strftime"), NULL_TREE,
1212			  strftime_format_type, 3, 0);
1213
1214  record_international_format (get_identifier ("gettext"), NULL_TREE, 1);
1215  record_international_format (get_identifier ("dgettext"), NULL_TREE, 2);
1216  record_international_format (get_identifier ("dcgettext"), NULL_TREE, 2);
1217}
1218
1219/* Record information for argument format checking.  FUNCTION_IDENT is
1220   the identifier node for the name of the function to check (its decl
1221   need not exist yet).
1222   FORMAT_TYPE specifies the type of format checking.  FORMAT_NUM is the number
1223   of the argument which is the format control string (starting from 1).
1224   FIRST_ARG_NUM is the number of the first actual argument to check
1225   against the format string, or zero if no checking is not be done
1226   (e.g. for varargs such as vfprintf).  */
1227
1228static void
1229record_function_format (name, assembler_name, format_type,
1230			format_num, first_arg_num)
1231      tree name;
1232      tree assembler_name;
1233      enum format_type format_type;
1234      int format_num;
1235      int first_arg_num;
1236{
1237  function_format_info *info;
1238
1239  /* Re-use existing structure if it's there.  */
1240
1241  for (info = function_format_list; info; info = info->next)
1242    {
1243      if (info->name == name && info->assembler_name == assembler_name)
1244	break;
1245    }
1246  if (! info)
1247    {
1248      info = (function_format_info *) xmalloc (sizeof (function_format_info));
1249      info->next = function_format_list;
1250      function_format_list = info;
1251
1252      info->name = name;
1253      info->assembler_name = assembler_name;
1254    }
1255
1256  info->format_type = format_type;
1257  info->format_num = format_num;
1258  info->first_arg_num = first_arg_num;
1259}
1260
1261/* Record information for the names of function that modify the format
1262   argument to format functions.  FUNCTION_IDENT is the identifier node for
1263   the name of the function (its decl need not exist yet) and FORMAT_NUM is
1264   the number of the argument which is the format control string (starting
1265   from 1).  */
1266
1267static void
1268record_international_format (name, assembler_name, format_num)
1269      tree name;
1270      tree assembler_name;
1271      int format_num;
1272{
1273  international_format_info *info;
1274
1275  /* Re-use existing structure if it's there.  */
1276
1277  for (info = international_format_list; info; info = info->next)
1278    {
1279      if (info->name == name && info->assembler_name == assembler_name)
1280	break;
1281    }
1282
1283  if (! info)
1284    {
1285      info
1286	= (international_format_info *)
1287	  xmalloc (sizeof (international_format_info));
1288      info->next = international_format_list;
1289      international_format_list = info;
1290
1291      info->name = name;
1292      info->assembler_name = assembler_name;
1293    }
1294
1295  info->format_num = format_num;
1296}
1297
1298static void
1299tfaff ()
1300{
1301  warning ("too few arguments for format");
1302}
1303
1304/* Check the argument list of a call to printf, scanf, etc.
1305   NAME is the function identifier.
1306   ASSEMBLER_NAME is the function's assembler identifier.
1307   (Either NAME or ASSEMBLER_NAME, but not both, may be NULL_TREE.)
1308   PARAMS is the list of argument values.  */
1309
1310void
1311check_function_format (name, assembler_name, params)
1312     tree name;
1313     tree assembler_name;
1314     tree params;
1315{
1316  function_format_info *info;
1317
1318  /* See if this function is a format function.  */
1319  for (info = function_format_list; info; info = info->next)
1320    {
1321      if (info->assembler_name
1322	  ? (info->assembler_name == assembler_name)
1323	  : (info->name == name))
1324	{
1325	  /* Yup; check it.  */
1326	  check_format_info (info, params);
1327	  break;
1328	}
1329    }
1330}
1331
1332/* Check the argument list of a call to printf, scanf, etc.
1333   INFO points to the function_format_info structure.
1334   PARAMS is the list of argument values.  */
1335
1336static void
1337check_format_info (info, params)
1338     function_format_info *info;
1339     tree params;
1340{
1341  int i;
1342  int arg_num;
1343  int suppressed, wide, precise;
1344  int length_char = 0;
1345  int format_char;
1346  int format_length;
1347  tree format_tree;
1348  tree cur_param;
1349  tree cur_type;
1350  tree wanted_type;
1351  tree first_fillin_param;
1352  const char *format_chars;
1353  format_char_info *fci = NULL;
1354  char flag_chars[8];
1355  int has_operand_number = 0;
1356
1357  /* Skip to format argument.  If the argument isn't available, there's
1358     no work for us to do; prototype checking will catch the problem.  */
1359  for (arg_num = 1; ; ++arg_num)
1360    {
1361      if (params == 0)
1362	return;
1363      if (arg_num == info->format_num)
1364	break;
1365      params = TREE_CHAIN (params);
1366    }
1367  format_tree = TREE_VALUE (params);
1368  params = TREE_CHAIN (params);
1369  if (format_tree == 0)
1370    return;
1371
1372  /* We can only check the format if it's a string constant.  */
1373  while (TREE_CODE (format_tree) == NOP_EXPR)
1374    format_tree = TREE_OPERAND (format_tree, 0); /* strip coercion */
1375
1376  if (TREE_CODE (format_tree) == CALL_EXPR
1377      && TREE_CODE (TREE_OPERAND (format_tree, 0)) == ADDR_EXPR
1378      && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (format_tree, 0), 0))
1379	  == FUNCTION_DECL))
1380    {
1381      tree function = TREE_OPERAND (TREE_OPERAND (format_tree, 0), 0);
1382
1383      /* See if this is a call to a known internationalization function
1384	 that modifies the format arg.  */
1385      international_format_info *info;
1386
1387      for (info = international_format_list; info; info = info->next)
1388	if (info->assembler_name
1389	    ? (info->assembler_name == DECL_ASSEMBLER_NAME (function))
1390	    : (info->name == DECL_NAME (function)))
1391	  {
1392	    tree inner_args;
1393	    int i;
1394
1395	    for (inner_args = TREE_OPERAND (format_tree, 1), i = 1;
1396		 inner_args != 0;
1397		 inner_args = TREE_CHAIN (inner_args), i++)
1398	      if (i == info->format_num)
1399		{
1400		  format_tree = TREE_VALUE (inner_args);
1401
1402		  while (TREE_CODE (format_tree) == NOP_EXPR)
1403		    format_tree = TREE_OPERAND (format_tree, 0);
1404		}
1405	  }
1406    }
1407
1408  if (integer_zerop (format_tree))
1409    {
1410      warning ("null format string");
1411      return;
1412    }
1413  if (TREE_CODE (format_tree) != ADDR_EXPR)
1414    return;
1415  format_tree = TREE_OPERAND (format_tree, 0);
1416  if (TREE_CODE (format_tree) != STRING_CST)
1417    return;
1418  format_chars = TREE_STRING_POINTER (format_tree);
1419  format_length = TREE_STRING_LENGTH (format_tree);
1420  if (format_length <= 1)
1421    warning ("zero-length format string");
1422  if (format_chars[--format_length] != 0)
1423    {
1424      warning ("unterminated format string");
1425      return;
1426    }
1427  /* Skip to first argument to check.  */
1428  while (arg_num + 1 < info->first_arg_num)
1429    {
1430      if (params == 0)
1431	return;
1432      params = TREE_CHAIN (params);
1433      ++arg_num;
1434    }
1435
1436  first_fillin_param = params;
1437  while (1)
1438    {
1439      int aflag;
1440      if (*format_chars == 0)
1441	{
1442	  if (format_chars - TREE_STRING_POINTER (format_tree) != format_length)
1443	    warning ("embedded `\\0' in format");
1444	  if (info->first_arg_num != 0 && params != 0 && ! has_operand_number)
1445	    warning ("too many arguments for format");
1446	  return;
1447	}
1448      if (*format_chars++ != '%')
1449	continue;
1450      if (*format_chars == 0)
1451	{
1452	  warning ("spurious trailing `%%' in format");
1453	  continue;
1454	}
1455      if (*format_chars == '%')
1456	{
1457	  ++format_chars;
1458	  continue;
1459	}
1460      flag_chars[0] = 0;
1461      suppressed = wide = precise = FALSE;
1462      if (info->format_type == scanf_format_type)
1463	{
1464	  suppressed = *format_chars == '*';
1465	  if (suppressed)
1466	    ++format_chars;
1467	  while (ISDIGIT (*format_chars))
1468	    ++format_chars;
1469	}
1470      else if (info->format_type == strftime_format_type)
1471        {
1472	  while (*format_chars != 0 && index ("_-0^#", *format_chars) != 0)
1473	    {
1474	      if (pedantic)
1475		warning ("ANSI C does not support the strftime `%c' flag",
1476			 *format_chars);
1477	      if (index (flag_chars, *format_chars) != 0)
1478		{
1479		  warning ("repeated `%c' flag in format",
1480			   *format_chars);
1481		  ++format_chars;
1482		}
1483	      else
1484		{
1485		  i = strlen (flag_chars);
1486		  flag_chars[i++] = *format_chars++;
1487		  flag_chars[i] = 0;
1488		}
1489	    }
1490	  while (ISDIGIT ((unsigned char) *format_chars))
1491	    {
1492	      wide = TRUE;
1493              ++format_chars;
1494	    }
1495	  if (wide && pedantic)
1496	    warning ("ANSI C does not support strftime format width");
1497	  if (*format_chars == 'E' || *format_chars == 'O')
1498	    {
1499	      i = strlen (flag_chars);
1500	      flag_chars[i++] = *format_chars++;
1501	      flag_chars[i] = 0;
1502	      if (*format_chars == 'E' || *format_chars == 'O')
1503	        {
1504		  warning ("multiple E/O modifiers in format");
1505		  while (*format_chars == 'E' || *format_chars == 'O')
1506		    ++format_chars;
1507		}
1508	    }
1509	}
1510      else if (info->format_type == printf_format_type)
1511	{
1512	  /* See if we have a number followed by a dollar sign.  If we do,
1513	     it is an operand number, so set PARAMS to that operand.  */
1514	  if (*format_chars >= '0' && *format_chars <= '9')
1515	    {
1516	      const char *p = format_chars;
1517
1518	      while (*p >= '0' && *p++ <= '9')
1519		;
1520
1521	      if (*p == '$')
1522		{
1523		  int opnum = atoi (format_chars);
1524
1525		  params = first_fillin_param;
1526		  format_chars = p + 1;
1527		  has_operand_number = 1;
1528
1529		  for (i = 1; i < opnum && params != 0; i++)
1530		    params = TREE_CHAIN (params);
1531
1532		  if (opnum == 0 || params == 0)
1533		    {
1534		      warning ("operand number out of range in format");
1535		      return;
1536		    }
1537		}
1538	    }
1539
1540	  while (*format_chars != 0 && index (" +#0-", *format_chars) != 0)
1541	    {
1542	      if (index (flag_chars, *format_chars) != 0)
1543		warning ("repeated `%c' flag in format", *format_chars++);
1544	      else
1545		{
1546		  i = strlen (flag_chars);
1547		  flag_chars[i++] = *format_chars++;
1548		  flag_chars[i] = 0;
1549		}
1550	    }
1551	  /* "If the space and + flags both appear,
1552	     the space flag will be ignored."  */
1553	  if (index (flag_chars, ' ') != 0
1554	      && index (flag_chars, '+') != 0)
1555	    warning ("use of both ` ' and `+' flags in format");
1556	  /* "If the 0 and - flags both appear,
1557	     the 0 flag will be ignored."  */
1558	  if (index (flag_chars, '0') != 0
1559	      && index (flag_chars, '-') != 0)
1560	    warning ("use of both `0' and `-' flags in format");
1561	  if (*format_chars == '*')
1562	    {
1563	      wide = TRUE;
1564	      /* "...a field width...may be indicated by an asterisk.
1565		 In this case, an int argument supplies the field width..."  */
1566	      ++format_chars;
1567	      if (params == 0)
1568		{
1569		  tfaff ();
1570		  return;
1571		}
1572	      if (info->first_arg_num != 0)
1573		{
1574		  cur_param = TREE_VALUE (params);
1575		  params = TREE_CHAIN (params);
1576		  ++arg_num;
1577		  /* size_t is generally not valid here.
1578		     It will work on most machines, because size_t and int
1579		     have the same mode.  But might as well warn anyway,
1580		     since it will fail on other machines.  */
1581		  if ((TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1582		       != integer_type_node)
1583		      &&
1584		      (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1585		       != unsigned_type_node))
1586		    warning ("field width is not type int (arg %d)", arg_num);
1587		}
1588	    }
1589	  else
1590	    {
1591	      while (ISDIGIT (*format_chars))
1592		{
1593		  wide = TRUE;
1594		  ++format_chars;
1595		}
1596	    }
1597	  if (*format_chars == '.')
1598	    {
1599	      precise = TRUE;
1600	      ++format_chars;
1601	      if (*format_chars != '*' && !ISDIGIT (*format_chars))
1602		warning ("`.' not followed by `*' or digit in format");
1603	      /* "...a...precision...may be indicated by an asterisk.
1604		 In this case, an int argument supplies the...precision."  */
1605	      if (*format_chars == '*')
1606		{
1607		  if (info->first_arg_num != 0)
1608		    {
1609		      ++format_chars;
1610		      if (params == 0)
1611		        {
1612			  tfaff ();
1613			  return;
1614			}
1615		      cur_param = TREE_VALUE (params);
1616		      params = TREE_CHAIN (params);
1617		      ++arg_num;
1618		      if (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1619			  != integer_type_node)
1620			warning ("field width is not type int (arg %d)",
1621				 arg_num);
1622		    }
1623		}
1624	      else
1625		{
1626		  while (ISDIGIT (*format_chars))
1627		    ++format_chars;
1628		}
1629	    }
1630	}
1631
1632      aflag = 0;
1633
1634      if (info->format_type != strftime_format_type)
1635	{
1636	  if (*format_chars == 'h' || *format_chars == 'l')
1637	    length_char = *format_chars++;
1638	  else if (*format_chars == 'q' || *format_chars == 'L')
1639	    {
1640	      length_char = *format_chars++;
1641	      if (pedantic)
1642		warning ("ANSI C does not support the `%c' length modifier",
1643			 length_char);
1644	    }
1645	  else if (*format_chars == 'Z')
1646	    {
1647	      length_char = *format_chars++;
1648	      if (pedantic)
1649		warning ("ANSI C does not support the `Z' length modifier");
1650	    }
1651	  else if (*format_chars == 'z')
1652	    {
1653	      length_char = *format_chars++;
1654	      if (pedantic)
1655		warning ("ANSI C does not support the `z' length modifier");
1656	    }
1657	  else
1658	    length_char = 0;
1659	  if (length_char == 'l' && *format_chars == 'l')
1660	    {
1661	      length_char = 'q', format_chars++;
1662	      /* FIXME: Is allowed in ISO C 9x.  */
1663	      if (pedantic)
1664		warning ("ANSI C does not support the `ll' length modifier");
1665	    }
1666	  else if (length_char == 'h' && *format_chars == 'h')
1667	    {
1668	      length_char = 'H', format_chars++;
1669	      /* FIXME: Is allowed in ISO C 9x.  */
1670	      if (pedantic)
1671		warning ("ANSI C does not support the `hh' length modifier");
1672	    }
1673	  if (*format_chars == 'a' && info->format_type == scanf_format_type)
1674	    {
1675	      if (format_chars[1] == 's' || format_chars[1] == 'S'
1676		  || format_chars[1] == '[')
1677		{
1678		  /* `a' is used as a flag.  */
1679		  aflag = 1;
1680		  format_chars++;
1681		}
1682	    }
1683	  if (suppressed && length_char != 0)
1684	    warning ("use of `*' and `%c' together in format", length_char);
1685	}
1686      format_char = *format_chars;
1687      if (format_char == 0
1688	  || (info->format_type != strftime_format_type && format_char == '%'))
1689	{
1690	  warning ("conversion lacks type at end of format");
1691	  continue;
1692	}
1693      /* The m, C, and S formats are GNU extensions.  */
1694      if (pedantic && info->format_type != strftime_format_type
1695	  && (format_char == 'm' || format_char == 'C' || format_char == 'S'))
1696	warning ("ANSI C does not support the `%c' format", format_char);
1697      /* ??? The a and A formats are C9X extensions, and should be allowed
1698	 when a C9X option is added.  */
1699      if (pedantic && info->format_type != strftime_format_type
1700	  && (format_char == 'a' || format_char == 'A'))
1701	warning ("ANSI C does not support the `%c' format", format_char);
1702      format_chars++;
1703      switch (info->format_type)
1704	{
1705	case printf_format_type:
1706	  fci = print_char_table;
1707	  break;
1708	case scanf_format_type:
1709	  fci = scan_char_table;
1710	  break;
1711	case strftime_format_type:
1712	  fci = time_char_table;
1713	  break;
1714	default:
1715	  abort ();
1716	}
1717      while (fci->format_chars != 0
1718	     && index (fci->format_chars, format_char) == 0)
1719	  ++fci;
1720      if (fci->format_chars == 0)
1721	{
1722	  if (format_char >= 040 && format_char < 0177)
1723	    warning ("unknown conversion type character `%c' in format",
1724		     format_char);
1725	  else
1726	    warning ("unknown conversion type character 0x%x in format",
1727		     format_char);
1728	  continue;
1729	}
1730      if (pedantic)
1731	{
1732	  if (index (fci->flag_chars, 'G') != 0)
1733	    warning ("ANSI C does not support `%%%c'", format_char);
1734	  if (index (fci->flag_chars, 'o') != 0
1735	      && index (flag_chars, 'O') != 0)
1736	    warning ("ANSI C does not support `%%O%c'", format_char);
1737	}
1738      if (wide && index (fci->flag_chars, 'w') == 0)
1739	warning ("width used with `%c' format", format_char);
1740      if (index (fci->flag_chars, '2') != 0)
1741	warning ("`%%%c' yields only last 2 digits of year", format_char);
1742      else if (index (fci->flag_chars, '3') != 0)
1743	warning ("`%%%c' yields only last 2 digits of year in some locales",
1744		 format_char);
1745      if (precise && index (fci->flag_chars, 'p') == 0)
1746	warning ("precision used with `%c' format", format_char);
1747      if (aflag && index (fci->flag_chars, 'a') == 0)
1748	{
1749	  warning ("`a' flag used with `%c' format", format_char);
1750	  /* To simplify the following code.  */
1751	  aflag = 0;
1752	}
1753      /* The a flag is a GNU extension.  */
1754      else if (pedantic && aflag)
1755	warning ("ANSI C does not support the `a' flag");
1756      if (info->format_type == scanf_format_type && format_char == '[')
1757	{
1758	  /* Skip over scan set, in case it happens to have '%' in it.  */
1759	  if (*format_chars == '^')
1760	    ++format_chars;
1761	  /* Find closing bracket; if one is hit immediately, then
1762	     it's part of the scan set rather than a terminator.  */
1763	  if (*format_chars == ']')
1764	    ++format_chars;
1765	  while (*format_chars && *format_chars != ']')
1766	    ++format_chars;
1767	  if (*format_chars != ']')
1768	    /* The end of the format string was reached.  */
1769	    warning ("no closing `]' for `%%[' format");
1770	}
1771      if (suppressed)
1772	{
1773	  if (index (fci->flag_chars, '*') == 0)
1774	    warning ("suppression of `%c' conversion in format", format_char);
1775	  continue;
1776	}
1777      for (i = 0; flag_chars[i] != 0; ++i)
1778	{
1779	  if (index (fci->flag_chars, flag_chars[i]) == 0)
1780	    warning ("flag `%c' used with type `%c'",
1781		     flag_chars[i], format_char);
1782	}
1783      if (info->format_type == strftime_format_type)
1784	continue;
1785      if (precise && index (flag_chars, '0') != 0
1786	  && (format_char == 'd' || format_char == 'i'
1787	      || format_char == 'o' || format_char == 'u'
1788	      || format_char == 'x' || format_char == 'X'))
1789	warning ("`0' flag ignored with precision specifier and `%c' format",
1790		 format_char);
1791      switch (length_char)
1792	{
1793	default: wanted_type = fci->nolen ? *(fci->nolen) : 0; break;
1794	case 'H': wanted_type = fci->hhlen ? *(fci->hhlen) : 0; break;
1795	case 'h': wanted_type = fci->hlen ? *(fci->hlen) : 0; break;
1796	case 'l': wanted_type = fci->llen ? *(fci->llen) : 0; break;
1797	case 'q': wanted_type = fci->qlen ? *(fci->qlen) : 0; break;
1798	case 'L': wanted_type = fci->bigllen ? *(fci->bigllen) : 0; break;
1799	case 'Z': wanted_type = fci->zlen ? *fci->zlen : 0; break;
1800	case 'z': wanted_type = fci->zlen ? *fci->zlen : 0; break;
1801	}
1802      if (wanted_type == 0)
1803	warning ("use of `%c' length character with `%c' type character",
1804		 length_char, format_char);
1805
1806      /* Finally. . .check type of argument against desired type!  */
1807      if (info->first_arg_num == 0)
1808	continue;
1809      if (fci->pointer_count == 0 && wanted_type == void_type_node)
1810	/* This specifier takes no argument.  */
1811	continue;
1812      if (params == 0)
1813	{
1814	  tfaff ();
1815	  return;
1816	}
1817      cur_param = TREE_VALUE (params);
1818      params = TREE_CHAIN (params);
1819      ++arg_num;
1820      cur_type = TREE_TYPE (cur_param);
1821
1822      STRIP_NOPS (cur_param);
1823
1824      /* Check the types of any additional pointer arguments
1825	 that precede the "real" argument.  */
1826      for (i = 0; i < fci->pointer_count + aflag; ++i)
1827	{
1828	  if (TREE_CODE (cur_type) == POINTER_TYPE)
1829	    {
1830	      cur_type = TREE_TYPE (cur_type);
1831
1832	      if (cur_param != 0 && TREE_CODE (cur_param) == ADDR_EXPR)
1833		cur_param = TREE_OPERAND (cur_param, 0);
1834	      else
1835		cur_param = 0;
1836
1837	      continue;
1838	    }
1839	  if (TREE_CODE (cur_type) != ERROR_MARK)
1840	    warning ((fci->pointer_count + aflag == 1
1841		      ? "format argument is not a pointer (arg %d)"
1842		      : "format argument is not a pointer to a pointer (arg %d)"),
1843		     arg_num);
1844	  break;
1845	}
1846
1847      /* See if this is an attempt to write into a const type with
1848	 scanf or with printf "%n".  */
1849      if ((info->format_type == scanf_format_type
1850	   || (info->format_type == printf_format_type
1851	       && format_char == 'n'))
1852	  && i == fci->pointer_count + aflag
1853	  && wanted_type != 0
1854	  && TREE_CODE (cur_type) != ERROR_MARK
1855	  && (TYPE_READONLY (cur_type)
1856	      || (cur_param != 0
1857		  && (TREE_CODE_CLASS (TREE_CODE (cur_param)) == 'c'
1858		      || (TREE_CODE_CLASS (TREE_CODE (cur_param)) == 'd'
1859			  && TREE_READONLY (cur_param))))))
1860	warning ("writing into constant object (arg %d)", arg_num);
1861
1862      /* Check the type of the "real" argument, if there's a type we want.  */
1863      if (i == fci->pointer_count + aflag && wanted_type != 0
1864	  && TREE_CODE (cur_type) != ERROR_MARK
1865	  && wanted_type != TYPE_MAIN_VARIANT (cur_type)
1866	  /* If we want `void *', allow any pointer type.
1867	     (Anything else would already have got a warning.)  */
1868	  && ! (wanted_type == void_type_node
1869		&& fci->pointer_count > 0)
1870	  /* Don't warn about differences merely in signedness.  */
1871	  && !(TREE_CODE (wanted_type) == INTEGER_TYPE
1872	       && TREE_CODE (TYPE_MAIN_VARIANT (cur_type)) == INTEGER_TYPE
1873	       && (TREE_UNSIGNED (wanted_type)
1874		   ? wanted_type == (cur_type = unsigned_type (cur_type))
1875		   : wanted_type == (cur_type = signed_type (cur_type))))
1876	  /* Likewise, "signed char", "unsigned char" and "char" are
1877	     equivalent but the above test won't consider them equivalent.  */
1878	  && ! (wanted_type == char_type_node
1879		&& (TYPE_MAIN_VARIANT (cur_type) == signed_char_type_node
1880		    || TYPE_MAIN_VARIANT (cur_type) == unsigned_char_type_node)))
1881	{
1882	  register const char *this;
1883	  register const char *that;
1884
1885	  this = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (wanted_type)));
1886	  that = 0;
1887	  if (TREE_CODE (cur_type) != ERROR_MARK
1888	      && TYPE_NAME (cur_type) != 0
1889	      && TREE_CODE (cur_type) != INTEGER_TYPE
1890	      && !(TREE_CODE (cur_type) == POINTER_TYPE
1891		   && TREE_CODE (TREE_TYPE (cur_type)) == INTEGER_TYPE))
1892	    {
1893	      if (TREE_CODE (TYPE_NAME (cur_type)) == TYPE_DECL
1894		  && DECL_NAME (TYPE_NAME (cur_type)) != 0)
1895		that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type)));
1896	      else
1897		that = IDENTIFIER_POINTER (TYPE_NAME (cur_type));
1898	    }
1899
1900	  /* A nameless type can't possibly match what the format wants.
1901	     So there will be a warning for it.
1902	     Make up a string to describe vaguely what it is.  */
1903	  if (that == 0)
1904	    {
1905	      if (TREE_CODE (cur_type) == POINTER_TYPE)
1906		that = "pointer";
1907	      else
1908		that = "different type";
1909	    }
1910
1911	  /* Make the warning better in case of mismatch of int vs long.  */
1912	  if (TREE_CODE (cur_type) == INTEGER_TYPE
1913	      && TREE_CODE (wanted_type) == INTEGER_TYPE
1914	      && TYPE_PRECISION (cur_type) == TYPE_PRECISION (wanted_type)
1915	      && TYPE_NAME (cur_type) != 0
1916	      && TREE_CODE (TYPE_NAME (cur_type)) == TYPE_DECL)
1917	    that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type)));
1918
1919	  if (strcmp (this, that) != 0)
1920	    warning ("%s format, %s arg (arg %d)", this, that, arg_num);
1921	}
1922    }
1923}
1924
1925/* Print a warning if a constant expression had overflow in folding.
1926   Invoke this function on every expression that the language
1927   requires to be a constant expression.
1928   Note the ANSI C standard says it is erroneous for a
1929   constant expression to overflow.  */
1930
1931void
1932constant_expression_warning (value)
1933     tree value;
1934{
1935  if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1936       || TREE_CODE (value) == COMPLEX_CST)
1937      && TREE_CONSTANT_OVERFLOW (value) && pedantic)
1938    pedwarn ("overflow in constant expression");
1939}
1940
1941/* Print a warning if an expression had overflow in folding.
1942   Invoke this function on every expression that
1943   (1) appears in the source code, and
1944   (2) might be a constant expression that overflowed, and
1945   (3) is not already checked by convert_and_check;
1946   however, do not invoke this function on operands of explicit casts.  */
1947
1948void
1949overflow_warning (value)
1950     tree value;
1951{
1952  if ((TREE_CODE (value) == INTEGER_CST
1953       || (TREE_CODE (value) == COMPLEX_CST
1954	   && TREE_CODE (TREE_REALPART (value)) == INTEGER_CST))
1955      && TREE_OVERFLOW (value))
1956    {
1957      TREE_OVERFLOW (value) = 0;
1958      if (skip_evaluation == 0)
1959	warning ("integer overflow in expression");
1960    }
1961  else if ((TREE_CODE (value) == REAL_CST
1962	    || (TREE_CODE (value) == COMPLEX_CST
1963		&& TREE_CODE (TREE_REALPART (value)) == REAL_CST))
1964	   && TREE_OVERFLOW (value))
1965    {
1966      TREE_OVERFLOW (value) = 0;
1967      if (skip_evaluation == 0)
1968	warning ("floating point overflow in expression");
1969    }
1970}
1971
1972/* Print a warning if a large constant is truncated to unsigned,
1973   or if -Wconversion is used and a constant < 0 is converted to unsigned.
1974   Invoke this function on every expression that might be implicitly
1975   converted to an unsigned type.  */
1976
1977void
1978unsigned_conversion_warning (result, operand)
1979     tree result, operand;
1980{
1981  if (TREE_CODE (operand) == INTEGER_CST
1982      && TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
1983      && TREE_UNSIGNED (TREE_TYPE (result))
1984      && skip_evaluation == 0
1985      && !int_fits_type_p (operand, TREE_TYPE (result)))
1986    {
1987      if (!int_fits_type_p (operand, signed_type (TREE_TYPE (result))))
1988	/* This detects cases like converting -129 or 256 to unsigned char.  */
1989	warning ("large integer implicitly truncated to unsigned type");
1990      else if (warn_conversion)
1991	warning ("negative integer implicitly converted to unsigned type");
1992    }
1993}
1994
1995/* Convert EXPR to TYPE, warning about conversion problems with constants.
1996   Invoke this function on every expression that is converted implicitly,
1997   i.e. because of language rules and not because of an explicit cast.  */
1998
1999tree
2000convert_and_check (type, expr)
2001     tree type, expr;
2002{
2003  tree t = convert (type, expr);
2004  if (TREE_CODE (t) == INTEGER_CST)
2005    {
2006      if (TREE_OVERFLOW (t))
2007	{
2008	  TREE_OVERFLOW (t) = 0;
2009
2010	  /* Do not diagnose overflow in a constant expression merely
2011	     because a conversion overflowed.  */
2012	  TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (expr);
2013
2014	  /* No warning for converting 0x80000000 to int.  */
2015	  if (!(TREE_UNSIGNED (type) < TREE_UNSIGNED (TREE_TYPE (expr))
2016		&& TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
2017		&& TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (expr))))
2018	    /* If EXPR fits in the unsigned version of TYPE,
2019	       don't warn unless pedantic.  */
2020	    if ((pedantic
2021		 || TREE_UNSIGNED (type)
2022		 || ! int_fits_type_p (expr, unsigned_type (type)))
2023	        && skip_evaluation == 0)
2024	      warning ("overflow in implicit constant conversion");
2025	}
2026      else
2027	unsigned_conversion_warning (t, expr);
2028    }
2029  return t;
2030}
2031
2032void
2033c_expand_expr_stmt (expr)
2034     tree expr;
2035{
2036  /* Do default conversion if safe and possibly important,
2037     in case within ({...}).  */
2038  if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE && lvalue_p (expr))
2039      || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
2040    expr = default_conversion (expr);
2041
2042  if (TREE_TYPE (expr) != error_mark_node
2043      && TYPE_SIZE (TREE_TYPE (expr)) == 0
2044      && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
2045    error ("expression statement has incomplete type");
2046
2047  expand_expr_stmt (expr);
2048}
2049
2050/* Validate the expression after `case' and apply default promotions.  */
2051
2052tree
2053check_case_value (value)
2054     tree value;
2055{
2056  if (value == NULL_TREE)
2057    return value;
2058
2059  /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue.  */
2060  STRIP_TYPE_NOPS (value);
2061
2062  if (TREE_CODE (value) != INTEGER_CST
2063      && value != error_mark_node)
2064    {
2065      error ("case label does not reduce to an integer constant");
2066      value = error_mark_node;
2067    }
2068  else
2069    /* Promote char or short to int.  */
2070    value = default_conversion (value);
2071
2072  constant_expression_warning (value);
2073
2074  return value;
2075}
2076
2077/* Return an integer type with BITS bits of precision,
2078   that is unsigned if UNSIGNEDP is nonzero, otherwise signed.  */
2079
2080tree
2081type_for_size (bits, unsignedp)
2082     unsigned bits;
2083     int unsignedp;
2084{
2085  if (bits == TYPE_PRECISION (integer_type_node))
2086    return unsignedp ? unsigned_type_node : integer_type_node;
2087
2088  if (bits == TYPE_PRECISION (signed_char_type_node))
2089    return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2090
2091  if (bits == TYPE_PRECISION (short_integer_type_node))
2092    return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2093
2094  if (bits == TYPE_PRECISION (long_integer_type_node))
2095    return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2096
2097  if (bits == TYPE_PRECISION (long_long_integer_type_node))
2098    return (unsignedp ? long_long_unsigned_type_node
2099	    : long_long_integer_type_node);
2100
2101  if (bits <= TYPE_PRECISION (intQI_type_node))
2102    return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2103
2104  if (bits <= TYPE_PRECISION (intHI_type_node))
2105    return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2106
2107  if (bits <= TYPE_PRECISION (intSI_type_node))
2108    return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2109
2110  if (bits <= TYPE_PRECISION (intDI_type_node))
2111    return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2112
2113  return 0;
2114}
2115
2116/* Return a data type that has machine mode MODE.
2117   If the mode is an integer,
2118   then UNSIGNEDP selects between signed and unsigned types.  */
2119
2120tree
2121type_for_mode (mode, unsignedp)
2122     enum machine_mode mode;
2123     int unsignedp;
2124{
2125  if (mode == TYPE_MODE (integer_type_node))
2126    return unsignedp ? unsigned_type_node : integer_type_node;
2127
2128  if (mode == TYPE_MODE (signed_char_type_node))
2129    return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2130
2131  if (mode == TYPE_MODE (short_integer_type_node))
2132    return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2133
2134  if (mode == TYPE_MODE (long_integer_type_node))
2135    return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2136
2137  if (mode == TYPE_MODE (long_long_integer_type_node))
2138    return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
2139
2140  if (mode == TYPE_MODE (intQI_type_node))
2141    return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2142
2143  if (mode == TYPE_MODE (intHI_type_node))
2144    return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2145
2146  if (mode == TYPE_MODE (intSI_type_node))
2147    return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2148
2149  if (mode == TYPE_MODE (intDI_type_node))
2150    return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2151
2152#if HOST_BITS_PER_WIDE_INT >= 64
2153  if (mode == TYPE_MODE (intTI_type_node))
2154    return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2155#endif
2156
2157  if (mode == TYPE_MODE (float_type_node))
2158    return float_type_node;
2159
2160  if (mode == TYPE_MODE (double_type_node))
2161    return double_type_node;
2162
2163  if (mode == TYPE_MODE (long_double_type_node))
2164    return long_double_type_node;
2165
2166  if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
2167    return build_pointer_type (char_type_node);
2168
2169  if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
2170    return build_pointer_type (integer_type_node);
2171
2172  return 0;
2173}
2174
2175/* Return the minimum number of bits needed to represent VALUE in a
2176   signed or unsigned type, UNSIGNEDP says which.  */
2177
2178int
2179min_precision (value, unsignedp)
2180     tree value;
2181     int unsignedp;
2182{
2183  int log;
2184
2185  /* If the value is negative, compute its negative minus 1.  The latter
2186     adjustment is because the absolute value of the largest negative value
2187     is one larger than the largest positive value.  This is equivalent to
2188     a bit-wise negation, so use that operation instead.  */
2189
2190  if (tree_int_cst_sgn (value) < 0)
2191    value = fold (build1 (BIT_NOT_EXPR, TREE_TYPE (value), value));
2192
2193  /* Return the number of bits needed, taking into account the fact
2194     that we need one more bit for a signed than unsigned type.  */
2195
2196  if (integer_zerop (value))
2197    log = 0;
2198  else if (TREE_INT_CST_HIGH (value) != 0)
2199    log = HOST_BITS_PER_WIDE_INT + floor_log2 (TREE_INT_CST_HIGH (value));
2200  else
2201    log = floor_log2 (TREE_INT_CST_LOW (value));
2202
2203  return log + 1 + ! unsignedp;
2204}
2205
2206/* Print an error message for invalid operands to arith operation CODE.
2207   NOP_EXPR is used as a special case (see truthvalue_conversion).  */
2208
2209void
2210binary_op_error (code)
2211     enum tree_code code;
2212{
2213  register const char *opname;
2214
2215  switch (code)
2216    {
2217    case NOP_EXPR:
2218      error ("invalid truth-value expression");
2219      return;
2220
2221    case PLUS_EXPR:
2222      opname = "+"; break;
2223    case MINUS_EXPR:
2224      opname = "-"; break;
2225    case MULT_EXPR:
2226      opname = "*"; break;
2227    case MAX_EXPR:
2228      opname = "max"; break;
2229    case MIN_EXPR:
2230      opname = "min"; break;
2231    case EQ_EXPR:
2232      opname = "=="; break;
2233    case NE_EXPR:
2234      opname = "!="; break;
2235    case LE_EXPR:
2236      opname = "<="; break;
2237    case GE_EXPR:
2238      opname = ">="; break;
2239    case LT_EXPR:
2240      opname = "<"; break;
2241    case GT_EXPR:
2242      opname = ">"; break;
2243    case LSHIFT_EXPR:
2244      opname = "<<"; break;
2245    case RSHIFT_EXPR:
2246      opname = ">>"; break;
2247    case TRUNC_MOD_EXPR:
2248    case FLOOR_MOD_EXPR:
2249      opname = "%"; break;
2250    case TRUNC_DIV_EXPR:
2251    case FLOOR_DIV_EXPR:
2252      opname = "/"; break;
2253    case BIT_AND_EXPR:
2254      opname = "&"; break;
2255    case BIT_IOR_EXPR:
2256      opname = "|"; break;
2257    case TRUTH_ANDIF_EXPR:
2258      opname = "&&"; break;
2259    case TRUTH_ORIF_EXPR:
2260      opname = "||"; break;
2261    case BIT_XOR_EXPR:
2262      opname = "^"; break;
2263    case LROTATE_EXPR:
2264    case RROTATE_EXPR:
2265      opname = "rotate"; break;
2266    default:
2267      opname = "unknown"; break;
2268    }
2269  error ("invalid operands to binary %s", opname);
2270}
2271
2272/* Subroutine of build_binary_op, used for comparison operations.
2273   See if the operands have both been converted from subword integer types
2274   and, if so, perhaps change them both back to their original type.
2275   This function is also responsible for converting the two operands
2276   to the proper common type for comparison.
2277
2278   The arguments of this function are all pointers to local variables
2279   of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
2280   RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
2281
2282   If this function returns nonzero, it means that the comparison has
2283   a constant value.  What this function returns is an expression for
2284   that value.  */
2285
2286tree
2287shorten_compare (op0_ptr, op1_ptr, restype_ptr, rescode_ptr)
2288     tree *op0_ptr, *op1_ptr;
2289     tree *restype_ptr;
2290     enum tree_code *rescode_ptr;
2291{
2292  register tree type;
2293  tree op0 = *op0_ptr;
2294  tree op1 = *op1_ptr;
2295  int unsignedp0, unsignedp1;
2296  int real1, real2;
2297  tree primop0, primop1;
2298  enum tree_code code = *rescode_ptr;
2299
2300  /* Throw away any conversions to wider types
2301     already present in the operands.  */
2302
2303  primop0 = get_narrower (op0, &unsignedp0);
2304  primop1 = get_narrower (op1, &unsignedp1);
2305
2306  /* Handle the case that OP0 does not *contain* a conversion
2307     but it *requires* conversion to FINAL_TYPE.  */
2308
2309  if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
2310    unsignedp0 = TREE_UNSIGNED (TREE_TYPE (op0));
2311  if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
2312    unsignedp1 = TREE_UNSIGNED (TREE_TYPE (op1));
2313
2314  /* If one of the operands must be floated, we cannot optimize.  */
2315  real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
2316  real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
2317
2318  /* If first arg is constant, swap the args (changing operation
2319     so value is preserved), for canonicalization.  Don't do this if
2320     the second arg is 0.  */
2321
2322  if (TREE_CONSTANT (primop0)
2323      && ! integer_zerop (primop1) && ! real_zerop (primop1))
2324    {
2325      register tree tem = primop0;
2326      register int temi = unsignedp0;
2327      primop0 = primop1;
2328      primop1 = tem;
2329      tem = op0;
2330      op0 = op1;
2331      op1 = tem;
2332      *op0_ptr = op0;
2333      *op1_ptr = op1;
2334      unsignedp0 = unsignedp1;
2335      unsignedp1 = temi;
2336      temi = real1;
2337      real1 = real2;
2338      real2 = temi;
2339
2340      switch (code)
2341	{
2342	case LT_EXPR:
2343	  code = GT_EXPR;
2344	  break;
2345	case GT_EXPR:
2346	  code = LT_EXPR;
2347	  break;
2348	case LE_EXPR:
2349	  code = GE_EXPR;
2350	  break;
2351	case GE_EXPR:
2352	  code = LE_EXPR;
2353	  break;
2354	default:
2355	  break;
2356	}
2357      *rescode_ptr = code;
2358    }
2359
2360  /* If comparing an integer against a constant more bits wide,
2361     maybe we can deduce a value of 1 or 0 independent of the data.
2362     Or else truncate the constant now
2363     rather than extend the variable at run time.
2364
2365     This is only interesting if the constant is the wider arg.
2366     Also, it is not safe if the constant is unsigned and the
2367     variable arg is signed, since in this case the variable
2368     would be sign-extended and then regarded as unsigned.
2369     Our technique fails in this case because the lowest/highest
2370     possible unsigned results don't follow naturally from the
2371     lowest/highest possible values of the variable operand.
2372     For just EQ_EXPR and NE_EXPR there is another technique that
2373     could be used: see if the constant can be faithfully represented
2374     in the other operand's type, by truncating it and reextending it
2375     and see if that preserves the constant's value.  */
2376
2377  if (!real1 && !real2
2378      && TREE_CODE (primop1) == INTEGER_CST
2379      && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
2380    {
2381      int min_gt, max_gt, min_lt, max_lt;
2382      tree maxval, minval;
2383      /* 1 if comparison is nominally unsigned.  */
2384      int unsignedp = TREE_UNSIGNED (*restype_ptr);
2385      tree val;
2386
2387      type = signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0));
2388
2389      /* If TYPE is an enumeration, then we need to get its min/max
2390	 values from it's underlying integral type, not the enumerated
2391	 type itself.  */
2392      if (TREE_CODE (type) == ENUMERAL_TYPE)
2393	type = type_for_size (TYPE_PRECISION (type), unsignedp0);
2394
2395      maxval = TYPE_MAX_VALUE (type);
2396      minval = TYPE_MIN_VALUE (type);
2397
2398      if (unsignedp && !unsignedp0)
2399	*restype_ptr = signed_type (*restype_ptr);
2400
2401      if (TREE_TYPE (primop1) != *restype_ptr)
2402	primop1 = convert (*restype_ptr, primop1);
2403      if (type != *restype_ptr)
2404	{
2405	  minval = convert (*restype_ptr, minval);
2406	  maxval = convert (*restype_ptr, maxval);
2407	}
2408
2409      if (unsignedp && unsignedp0)
2410	{
2411	  min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
2412	  max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
2413	  min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
2414	  max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
2415	}
2416      else
2417	{
2418	  min_gt = INT_CST_LT (primop1, minval);
2419	  max_gt = INT_CST_LT (primop1, maxval);
2420	  min_lt = INT_CST_LT (minval, primop1);
2421	  max_lt = INT_CST_LT (maxval, primop1);
2422	}
2423
2424      val = 0;
2425      /* This used to be a switch, but Genix compiler can't handle that.  */
2426      if (code == NE_EXPR)
2427	{
2428	  if (max_lt || min_gt)
2429	    val = boolean_true_node;
2430	}
2431      else if (code == EQ_EXPR)
2432	{
2433	  if (max_lt || min_gt)
2434	    val = boolean_false_node;
2435	}
2436      else if (code == LT_EXPR)
2437	{
2438	  if (max_lt)
2439	    val = boolean_true_node;
2440	  if (!min_lt)
2441	    val = boolean_false_node;
2442	}
2443      else if (code == GT_EXPR)
2444	{
2445	  if (min_gt)
2446	    val = boolean_true_node;
2447	  if (!max_gt)
2448	    val = boolean_false_node;
2449	}
2450      else if (code == LE_EXPR)
2451	{
2452	  if (!max_gt)
2453	    val = boolean_true_node;
2454	  if (min_gt)
2455	    val = boolean_false_node;
2456	}
2457      else if (code == GE_EXPR)
2458	{
2459	  if (!min_lt)
2460	    val = boolean_true_node;
2461	  if (max_lt)
2462	    val = boolean_false_node;
2463	}
2464
2465      /* If primop0 was sign-extended and unsigned comparison specd,
2466	 we did a signed comparison above using the signed type bounds.
2467	 But the comparison we output must be unsigned.
2468
2469	 Also, for inequalities, VAL is no good; but if the signed
2470	 comparison had *any* fixed result, it follows that the
2471	 unsigned comparison just tests the sign in reverse
2472	 (positive values are LE, negative ones GE).
2473	 So we can generate an unsigned comparison
2474	 against an extreme value of the signed type.  */
2475
2476      if (unsignedp && !unsignedp0)
2477	{
2478	  if (val != 0)
2479	    switch (code)
2480	      {
2481	      case LT_EXPR:
2482	      case GE_EXPR:
2483		primop1 = TYPE_MIN_VALUE (type);
2484		val = 0;
2485		break;
2486
2487	      case LE_EXPR:
2488	      case GT_EXPR:
2489		primop1 = TYPE_MAX_VALUE (type);
2490		val = 0;
2491		break;
2492
2493	      default:
2494		break;
2495	      }
2496	  type = unsigned_type (type);
2497	}
2498
2499      if (!max_gt && !unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
2500	{
2501	  /* This is the case of (char)x >?< 0x80, which people used to use
2502	     expecting old C compilers to change the 0x80 into -0x80.  */
2503	  if (val == boolean_false_node)
2504	    warning ("comparison is always false due to limited range of data type");
2505	  if (val == boolean_true_node)
2506	    warning ("comparison is always true due to limited range of data type");
2507	}
2508
2509      if (!min_lt && unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
2510	{
2511	  /* This is the case of (unsigned char)x >?< -1 or < 0.  */
2512	  if (val == boolean_false_node)
2513	    warning ("comparison is always false due to limited range of data type");
2514	  if (val == boolean_true_node)
2515	    warning ("comparison is always true due to limited range of data type");
2516	}
2517
2518      if (val != 0)
2519	{
2520	  /* Don't forget to evaluate PRIMOP0 if it has side effects.  */
2521	  if (TREE_SIDE_EFFECTS (primop0))
2522	    return build (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
2523	  return val;
2524	}
2525
2526      /* Value is not predetermined, but do the comparison
2527	 in the type of the operand that is not constant.
2528	 TYPE is already properly set.  */
2529    }
2530  else if (real1 && real2
2531	   && (TYPE_PRECISION (TREE_TYPE (primop0))
2532	       == TYPE_PRECISION (TREE_TYPE (primop1))))
2533    type = TREE_TYPE (primop0);
2534
2535  /* If args' natural types are both narrower than nominal type
2536     and both extend in the same manner, compare them
2537     in the type of the wider arg.
2538     Otherwise must actually extend both to the nominal
2539     common type lest different ways of extending
2540     alter the result.
2541     (eg, (short)-1 == (unsigned short)-1  should be 0.)  */
2542
2543  else if (unsignedp0 == unsignedp1 && real1 == real2
2544	   && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
2545	   && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
2546    {
2547      type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
2548      type = signed_or_unsigned_type (unsignedp0
2549				      || TREE_UNSIGNED (*restype_ptr),
2550				      type);
2551      /* Make sure shorter operand is extended the right way
2552	 to match the longer operand.  */
2553      primop0 = convert (signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0)),
2554			 primop0);
2555      primop1 = convert (signed_or_unsigned_type (unsignedp1, TREE_TYPE (primop1)),
2556			 primop1);
2557    }
2558  else
2559    {
2560      /* Here we must do the comparison on the nominal type
2561	 using the args exactly as we received them.  */
2562      type = *restype_ptr;
2563      primop0 = op0;
2564      primop1 = op1;
2565
2566      if (!real1 && !real2 && integer_zerop (primop1)
2567	  && TREE_UNSIGNED (*restype_ptr))
2568	{
2569	  tree value = 0;
2570	  switch (code)
2571	    {
2572	    case GE_EXPR:
2573	      /* All unsigned values are >= 0, so we warn if extra warnings
2574		 are requested.  However, if OP0 is a constant that is
2575		 >= 0, the signedness of the comparison isn't an issue,
2576		 so suppress the warning.  */
2577	      if (extra_warnings
2578		  && ! (TREE_CODE (primop0) == INTEGER_CST
2579			&& ! TREE_OVERFLOW (convert (signed_type (type),
2580						     primop0))))
2581		warning ("comparison of unsigned expression >= 0 is always true");
2582	      value = boolean_true_node;
2583	      break;
2584
2585	    case LT_EXPR:
2586	      if (extra_warnings
2587		  && ! (TREE_CODE (primop0) == INTEGER_CST
2588			&& ! TREE_OVERFLOW (convert (signed_type (type),
2589						     primop0))))
2590		warning ("comparison of unsigned expression < 0 is always false");
2591	      value = boolean_false_node;
2592	      break;
2593
2594	    default:
2595	      break;
2596	    }
2597
2598	  if (value != 0)
2599	    {
2600	      /* Don't forget to evaluate PRIMOP0 if it has side effects.  */
2601	      if (TREE_SIDE_EFFECTS (primop0))
2602		return build (COMPOUND_EXPR, TREE_TYPE (value),
2603			      primop0, value);
2604	      return value;
2605	    }
2606	}
2607    }
2608
2609  *op0_ptr = convert (type, primop0);
2610  *op1_ptr = convert (type, primop1);
2611
2612  *restype_ptr = boolean_type_node;
2613
2614  return 0;
2615}
2616
2617/* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
2618   or validate its data type for an `if' or `while' statement or ?..: exp.
2619
2620   This preparation consists of taking the ordinary
2621   representation of an expression expr and producing a valid tree
2622   boolean expression describing whether expr is nonzero.  We could
2623   simply always do build_binary_op (NE_EXPR, expr, boolean_false_node, 1),
2624   but we optimize comparisons, &&, ||, and !.
2625
2626   The resulting type should always be `boolean_type_node'.  */
2627
2628tree
2629truthvalue_conversion (expr)
2630     tree expr;
2631{
2632  if (TREE_CODE (expr) == ERROR_MARK)
2633    return expr;
2634
2635#if 0 /* This appears to be wrong for C++.  */
2636  /* These really should return error_mark_node after 2.4 is stable.
2637     But not all callers handle ERROR_MARK properly.  */
2638  switch (TREE_CODE (TREE_TYPE (expr)))
2639    {
2640    case RECORD_TYPE:
2641      error ("struct type value used where scalar is required");
2642      return boolean_false_node;
2643
2644    case UNION_TYPE:
2645      error ("union type value used where scalar is required");
2646      return boolean_false_node;
2647
2648    case ARRAY_TYPE:
2649      error ("array type value used where scalar is required");
2650      return boolean_false_node;
2651
2652    default:
2653      break;
2654    }
2655#endif /* 0 */
2656
2657  switch (TREE_CODE (expr))
2658    {
2659      /* It is simpler and generates better code to have only TRUTH_*_EXPR
2660	 or comparison expressions as truth values at this level.  */
2661#if 0
2662    case COMPONENT_REF:
2663      /* A one-bit unsigned bit-field is already acceptable.  */
2664      if (1 == TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (expr, 1)))
2665	  && TREE_UNSIGNED (TREE_OPERAND (expr, 1)))
2666	return expr;
2667      break;
2668#endif
2669
2670    case EQ_EXPR:
2671      /* It is simpler and generates better code to have only TRUTH_*_EXPR
2672	 or comparison expressions as truth values at this level.  */
2673#if 0
2674      if (integer_zerop (TREE_OPERAND (expr, 1)))
2675	return build_unary_op (TRUTH_NOT_EXPR, TREE_OPERAND (expr, 0), 0);
2676#endif
2677    case NE_EXPR: case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
2678    case TRUTH_ANDIF_EXPR:
2679    case TRUTH_ORIF_EXPR:
2680    case TRUTH_AND_EXPR:
2681    case TRUTH_OR_EXPR:
2682    case TRUTH_XOR_EXPR:
2683    case TRUTH_NOT_EXPR:
2684      TREE_TYPE (expr) = boolean_type_node;
2685      return expr;
2686
2687    case ERROR_MARK:
2688      return expr;
2689
2690    case INTEGER_CST:
2691      return integer_zerop (expr) ? boolean_false_node : boolean_true_node;
2692
2693    case REAL_CST:
2694      return real_zerop (expr) ? boolean_false_node : boolean_true_node;
2695
2696    case ADDR_EXPR:
2697      /* If we are taking the address of a external decl, it might be zero
2698	 if it is weak, so we cannot optimize.  */
2699      if (TREE_CODE_CLASS (TREE_CODE (TREE_OPERAND (expr, 0))) == 'd'
2700	  && DECL_EXTERNAL (TREE_OPERAND (expr, 0)))
2701	break;
2702
2703      if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 0)))
2704	return build (COMPOUND_EXPR, boolean_type_node,
2705		      TREE_OPERAND (expr, 0), boolean_true_node);
2706      else
2707	return boolean_true_node;
2708
2709    case COMPLEX_EXPR:
2710      return build_binary_op ((TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
2711			       ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
2712			      truthvalue_conversion (TREE_OPERAND (expr, 0)),
2713			      truthvalue_conversion (TREE_OPERAND (expr, 1)),
2714			      0);
2715
2716    case NEGATE_EXPR:
2717    case ABS_EXPR:
2718    case FLOAT_EXPR:
2719    case FFS_EXPR:
2720      /* These don't change whether an object is non-zero or zero.  */
2721      return truthvalue_conversion (TREE_OPERAND (expr, 0));
2722
2723    case LROTATE_EXPR:
2724    case RROTATE_EXPR:
2725      /* These don't change whether an object is zero or non-zero, but
2726	 we can't ignore them if their second arg has side-effects.  */
2727      if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
2728	return build (COMPOUND_EXPR, boolean_type_node, TREE_OPERAND (expr, 1),
2729		      truthvalue_conversion (TREE_OPERAND (expr, 0)));
2730      else
2731	return truthvalue_conversion (TREE_OPERAND (expr, 0));
2732
2733    case COND_EXPR:
2734      /* Distribute the conversion into the arms of a COND_EXPR.  */
2735      return fold (build (COND_EXPR, boolean_type_node, TREE_OPERAND (expr, 0),
2736			  truthvalue_conversion (TREE_OPERAND (expr, 1)),
2737			  truthvalue_conversion (TREE_OPERAND (expr, 2))));
2738
2739    case CONVERT_EXPR:
2740      /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
2741	 since that affects how `default_conversion' will behave.  */
2742      if (TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE
2743	  || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
2744	break;
2745      /* fall through...  */
2746    case NOP_EXPR:
2747      /* If this is widening the argument, we can ignore it.  */
2748      if (TYPE_PRECISION (TREE_TYPE (expr))
2749	  >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
2750	return truthvalue_conversion (TREE_OPERAND (expr, 0));
2751      break;
2752
2753    case MINUS_EXPR:
2754      /* With IEEE arithmetic, x - x may not equal 0, so we can't optimize
2755	 this case.  */
2756      if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
2757	  && TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE)
2758	break;
2759      /* fall through...  */
2760    case BIT_XOR_EXPR:
2761      /* This and MINUS_EXPR can be changed into a comparison of the
2762	 two objects.  */
2763      if (TREE_TYPE (TREE_OPERAND (expr, 0))
2764	  == TREE_TYPE (TREE_OPERAND (expr, 1)))
2765	return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2766				TREE_OPERAND (expr, 1), 1);
2767      return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2768			      fold (build1 (NOP_EXPR,
2769					    TREE_TYPE (TREE_OPERAND (expr, 0)),
2770					    TREE_OPERAND (expr, 1))), 1);
2771
2772    case BIT_AND_EXPR:
2773      if (integer_onep (TREE_OPERAND (expr, 1))
2774	  && TREE_TYPE (expr) != boolean_type_node)
2775	/* Using convert here would cause infinite recursion.  */
2776	return build1 (NOP_EXPR, boolean_type_node, expr);
2777      break;
2778
2779    case MODIFY_EXPR:
2780      if (warn_parentheses && C_EXP_ORIGINAL_CODE (expr) == MODIFY_EXPR)
2781	warning ("suggest parentheses around assignment used as truth value");
2782      break;
2783
2784    default:
2785      break;
2786    }
2787
2788  if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
2789    {
2790      tree tem = save_expr (expr);
2791      return (build_binary_op
2792	      ((TREE_SIDE_EFFECTS (expr)
2793		? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
2794	       truthvalue_conversion (build_unary_op (REALPART_EXPR, tem, 0)),
2795	       truthvalue_conversion (build_unary_op (IMAGPART_EXPR, tem, 0)),
2796	       0));
2797    }
2798
2799  return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
2800}
2801
2802#if USE_CPPLIB
2803/* Read the rest of a #-directive from input stream FINPUT.
2804   In normal use, the directive name and the white space after it
2805   have already been read, so they won't be included in the result.
2806   We allow for the fact that the directive line may contain
2807   a newline embedded within a character or string literal which forms
2808   a part of the directive.
2809
2810   The value is a string in a reusable buffer.  It remains valid
2811   only until the next time this function is called.  */
2812unsigned char *yy_cur, *yy_lim;
2813
2814#define GETC() (yy_cur < yy_lim ? *yy_cur++ : yy_get_token ())
2815#define UNGETC(c) ((c) == EOF ? 0 : yy_cur--)
2816
2817int
2818yy_get_token ()
2819{
2820  for (;;)
2821    {
2822      parse_in.limit = parse_in.token_buffer;
2823      cpp_token = cpp_get_token (&parse_in);
2824      if (cpp_token == CPP_EOF)
2825	return -1;
2826      yy_lim = CPP_PWRITTEN (&parse_in);
2827      yy_cur = parse_in.token_buffer;
2828      if (yy_cur < yy_lim)
2829	return *yy_cur++;
2830    }
2831}
2832
2833char *
2834get_directive_line ()
2835{
2836  static char *directive_buffer = NULL;
2837  static unsigned buffer_length = 0;
2838  register char *p;
2839  register char *buffer_limit;
2840  register int looking_for = 0;
2841  register int char_escaped = 0;
2842
2843  if (buffer_length == 0)
2844    {
2845      directive_buffer = (char *)xmalloc (128);
2846      buffer_length = 128;
2847    }
2848
2849  buffer_limit = &directive_buffer[buffer_length];
2850
2851  for (p = directive_buffer; ; )
2852    {
2853      int c;
2854
2855      /* Make buffer bigger if it is full.  */
2856      if (p >= buffer_limit)
2857        {
2858	  register unsigned bytes_used = (p - directive_buffer);
2859
2860	  buffer_length *= 2;
2861	  directive_buffer
2862	    = (char *)xrealloc (directive_buffer, buffer_length);
2863	  p = &directive_buffer[bytes_used];
2864	  buffer_limit = &directive_buffer[buffer_length];
2865        }
2866
2867      c = GETC ();
2868
2869      /* Discard initial whitespace.  */
2870      if ((c == ' ' || c == '\t') && p == directive_buffer)
2871	continue;
2872
2873      /* Detect the end of the directive.  */
2874      if (c == '\n' && looking_for == 0)
2875	{
2876          UNGETC (c);
2877	  c = '\0';
2878	}
2879
2880      *p++ = c;
2881
2882      if (c == 0)
2883	return directive_buffer;
2884
2885      /* Handle string and character constant syntax.  */
2886      if (looking_for)
2887	{
2888	  if (looking_for == c && !char_escaped)
2889	    looking_for = 0;	/* Found terminator... stop looking.  */
2890	}
2891      else
2892        if (c == '\'' || c == '"')
2893	  looking_for = c;	/* Don't stop buffering until we see another
2894				   another one of these (or an EOF).  */
2895
2896      /* Handle backslash.  */
2897      char_escaped = (c == '\\' && ! char_escaped);
2898    }
2899}
2900#else
2901/* Read the rest of a #-directive from input stream FINPUT.
2902   In normal use, the directive name and the white space after it
2903   have already been read, so they won't be included in the result.
2904   We allow for the fact that the directive line may contain
2905   a newline embedded within a character or string literal which forms
2906   a part of the directive.
2907
2908   The value is a string in a reusable buffer.  It remains valid
2909   only until the next time this function is called.
2910
2911   The terminating character ('\n' or EOF) is left in FINPUT for the
2912   caller to re-read.  */
2913
2914char *
2915get_directive_line (finput)
2916     register FILE *finput;
2917{
2918  static char *directive_buffer = NULL;
2919  static unsigned buffer_length = 0;
2920  register char *p;
2921  register char *buffer_limit;
2922  register int looking_for = 0;
2923  register int char_escaped = 0;
2924
2925  if (buffer_length == 0)
2926    {
2927      directive_buffer = (char *)xmalloc (128);
2928      buffer_length = 128;
2929    }
2930
2931  buffer_limit = &directive_buffer[buffer_length];
2932
2933  for (p = directive_buffer; ; )
2934    {
2935      int c;
2936
2937      /* Make buffer bigger if it is full.  */
2938      if (p >= buffer_limit)
2939        {
2940	  register unsigned bytes_used = (p - directive_buffer);
2941
2942	  buffer_length *= 2;
2943	  directive_buffer
2944	    = (char *)xrealloc (directive_buffer, buffer_length);
2945	  p = &directive_buffer[bytes_used];
2946	  buffer_limit = &directive_buffer[buffer_length];
2947        }
2948
2949      c = getc (finput);
2950
2951      /* Discard initial whitespace.  */
2952      if ((c == ' ' || c == '\t') && p == directive_buffer)
2953	continue;
2954
2955      /* Detect the end of the directive.  */
2956      if (looking_for == 0
2957	  && (c == '\n' || c == EOF))
2958	{
2959          ungetc (c, finput);
2960	  c = '\0';
2961	}
2962
2963      *p++ = c;
2964
2965      if (c == 0)
2966	return directive_buffer;
2967
2968      /* Handle string and character constant syntax.  */
2969      if (looking_for)
2970	{
2971	  if (looking_for == c && !char_escaped)
2972	    looking_for = 0;	/* Found terminator... stop looking.  */
2973	}
2974      else
2975        if (c == '\'' || c == '"')
2976	  looking_for = c;	/* Don't stop buffering until we see another
2977				   one of these (or an EOF).  */
2978
2979      /* Handle backslash.  */
2980      char_escaped = (c == '\\' && ! char_escaped);
2981    }
2982}
2983#endif /* !USE_CPPLIB */
2984
2985/* Make a variant type in the proper way for C/C++, propagating qualifiers
2986   down to the element type of an array.  */
2987
2988tree
2989c_build_qualified_type (type, type_quals)
2990     tree type;
2991     int type_quals;
2992{
2993  /* A restrict-qualified pointer type must be a pointer to object or
2994     incomplete type.  Note that the use of POINTER_TYPE_P also allows
2995     REFERENCE_TYPEs, which is appropriate for C++.  Unfortunately,
2996     the C++ front-end also use POINTER_TYPE for pointer-to-member
2997     values, so even though it should be illegal to use `restrict'
2998     with such an entity we don't flag that here.  Thus, special case
2999     code for that case is required in the C++ front-end.  */
3000  if ((type_quals & TYPE_QUAL_RESTRICT)
3001      && (!POINTER_TYPE_P (type)
3002	  || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
3003    {
3004      error ("invalid use of `restrict'");
3005      type_quals &= ~TYPE_QUAL_RESTRICT;
3006    }
3007
3008  if (TREE_CODE (type) == ARRAY_TYPE)
3009    return build_array_type (c_build_qualified_type (TREE_TYPE (type),
3010						     type_quals),
3011			     TYPE_DOMAIN (type));
3012  return build_qualified_type (type, type_quals);
3013}
3014
3015/* Apply the TYPE_QUALS to the new DECL.  */
3016
3017void
3018c_apply_type_quals_to_decl (type_quals, decl)
3019     int type_quals;
3020     tree decl;
3021{
3022  if (type_quals & TYPE_QUAL_CONST)
3023    TREE_READONLY (decl) = 1;
3024  if (type_quals & TYPE_QUAL_VOLATILE)
3025    {
3026      TREE_SIDE_EFFECTS (decl) = 1;
3027      TREE_THIS_VOLATILE (decl) = 1;
3028    }
3029  if (type_quals & TYPE_QUAL_RESTRICT)
3030    {
3031      if (!TREE_TYPE (decl)
3032	  || !POINTER_TYPE_P (TREE_TYPE (decl))
3033	  || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (TREE_TYPE (decl))))
3034	error ("invalid use of `restrict'");
3035      else if (flag_strict_aliasing)
3036	{
3037	  /* No two restricted pointers can point at the same thing.
3038	     However, a restricted pointer can point at the same thing
3039	     as an unrestricted pointer, if that unrestricted pointer
3040	     is based on the restricted pointer.  So, we make the
3041	     alias set for the restricted pointer a subset of the
3042	     alias set for the type pointed to by the type of the
3043	     decl.  */
3044
3045	  int pointed_to_alias_set
3046	    = get_alias_set (TREE_TYPE (TREE_TYPE (decl)));
3047
3048	  if (!pointed_to_alias_set)
3049	    /* It's not legal to make a subset of alias set zero.  */
3050	    ;
3051	  else
3052	    {
3053	      DECL_POINTER_ALIAS_SET (decl) = new_alias_set ();
3054	      record_alias_subset  (pointed_to_alias_set,
3055				    DECL_POINTER_ALIAS_SET (decl));
3056	    }
3057	}
3058    }
3059}
3060
3061/* T is an expression with pointer type.  Find the DECL on which this
3062   expression is based.  (For example, in `a[i]' this would be `a'.)
3063   If there is no such DECL, or a unique decl cannot be determined,
3064   NULL_TREE is retured.  */
3065
3066static tree
3067c_find_base_decl (t)
3068     tree t;
3069{
3070  int i;
3071  tree decl;
3072
3073  if (t == NULL_TREE || t == error_mark_node)
3074    return NULL_TREE;
3075
3076  if (!POINTER_TYPE_P (TREE_TYPE (t)))
3077    return NULL_TREE;
3078
3079  decl = NULL_TREE;
3080
3081  if (TREE_CODE (t) == FIELD_DECL
3082      || TREE_CODE (t) == PARM_DECL
3083      || TREE_CODE (t) == VAR_DECL)
3084    /* Aha, we found a pointer-typed declaration.  */
3085    return t;
3086
3087  /* It would be nice to deal with COMPONENT_REFs here.  If we could
3088     tell that `a' and `b' were the same, then `a->f' and `b->f' are
3089     also the same.  */
3090
3091  /* Handle general expressions.  */
3092  switch (TREE_CODE_CLASS (TREE_CODE (t)))
3093    {
3094    case '1':
3095    case '2':
3096    case '3':
3097      for (i = tree_code_length [(int) TREE_CODE (t)]; --i >= 0;)
3098	{
3099	  tree d = c_find_base_decl (TREE_OPERAND (t, i));
3100	  if (d)
3101	    {
3102	      if (!decl)
3103		decl = d;
3104	      else if (d && d != decl)
3105		/* Two different declarations.  That's confusing; let's
3106		   just assume we don't know what's going on.  */
3107		decl = NULL_TREE;
3108	    }
3109	}
3110      break;
3111
3112    default:
3113      break;
3114    }
3115
3116  return decl;
3117}
3118
3119/* Return the typed-based alias set for T, which may be an expression
3120   or a type.  */
3121
3122int
3123c_get_alias_set (t)
3124     tree t;
3125{
3126  tree type;
3127  tree u;
3128
3129  if (t == error_mark_node)
3130    return 0;
3131
3132  type = (TREE_CODE_CLASS (TREE_CODE (t)) == 't')
3133    ? t : TREE_TYPE (t);
3134
3135  if (type == error_mark_node)
3136    return 0;
3137
3138  /* Deal with special cases first; for certain kinds of references
3139     we're interested in more than just the type.  */
3140
3141  if (TREE_CODE (t) == BIT_FIELD_REF)
3142    /* Perhaps reads and writes to this piece of data alias fields
3143       neighboring the bitfield.  Perhaps that's impossible.  For now,
3144       let's just assume that bitfields can alias everything, which is
3145       the conservative assumption.  */
3146    return 0;
3147
3148  /* Permit type-punning when accessing a union, provided the access
3149     is directly through the union.  For example, this code does not
3150     permit taking the address of a union member and then storing
3151     through it.  Even the type-punning allowed here is a GCC
3152     extension, albeit a common and useful one; the C standard says
3153     that such accesses have implementation-defined behavior.  */
3154  for (u = t;
3155       TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
3156       u = TREE_OPERAND (u, 0))
3157    if (TREE_CODE (u) == COMPONENT_REF
3158	&& TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
3159      return 0;
3160
3161  if (TREE_CODE (t) == INDIRECT_REF)
3162    {
3163      /* Check for accesses through restrict-qualified pointers.  */
3164      tree decl = c_find_base_decl (TREE_OPERAND (t, 0));
3165
3166      if (decl && DECL_POINTER_ALIAS_SET_KNOWN_P (decl))
3167	/* We use the alias set indicated in the declaration.  */
3168	return DECL_POINTER_ALIAS_SET (decl);
3169    }
3170
3171  /* From here on, only the type matters.  */
3172
3173  if (TREE_CODE (t) == COMPONENT_REF
3174      && DECL_BIT_FIELD_TYPE (TREE_OPERAND (t, 1)))
3175    /* Since build_modify_expr calls get_unwidened for stores to
3176       component references, the type of a bit field can be changed
3177       from (say) `unsigned int : 16' to `unsigned short' or from
3178       `enum E : 16' to `short'.  We want the real type of the
3179       bit-field in this case, not some the integral equivalent.  */
3180    type = DECL_BIT_FIELD_TYPE (TREE_OPERAND (t, 1));
3181
3182  if (TYPE_ALIAS_SET_KNOWN_P (type))
3183    /* If we've already calculated the value, just return it.  */
3184    return TYPE_ALIAS_SET (type);
3185  else if (TYPE_MAIN_VARIANT (type) != type)
3186    /* The C standard specifically allows aliasing between
3187       cv-qualified variants of types.  */
3188    TYPE_ALIAS_SET (type) = c_get_alias_set (TYPE_MAIN_VARIANT (type));
3189  else if (TREE_CODE (type) == INTEGER_TYPE)
3190    {
3191      tree signed_variant;
3192
3193      /* The C standard specifically allows aliasing between signed and
3194	 unsigned variants of the same type.  We treat the signed
3195	 variant as canonical.  */
3196      signed_variant = signed_type (type);
3197
3198      if (signed_variant != type)
3199	TYPE_ALIAS_SET (type) = c_get_alias_set (signed_variant);
3200      else if (signed_variant == signed_char_type_node)
3201	/* The C standard guarantess that any object may be accessed
3202	   via an lvalue that has character type.  We don't have to
3203	   check for unsigned_char_type_node or char_type_node because
3204	   we are specifically looking at the signed variant.  */
3205	TYPE_ALIAS_SET (type) = 0;
3206    }
3207  else if (TREE_CODE (type) == ARRAY_TYPE)
3208    /* Anything that can alias one of the array elements can alias
3209       the entire array as well.  */
3210    TYPE_ALIAS_SET (type) = c_get_alias_set (TREE_TYPE (type));
3211  else if (TREE_CODE (type) == FUNCTION_TYPE)
3212    /* There are no objects of FUNCTION_TYPE, so there's no point in
3213       using up an alias set for them.  (There are, of course,
3214       pointers and references to functions, but that's
3215       different.)  */
3216    TYPE_ALIAS_SET (type) = 0;
3217  else if (TREE_CODE (type) == RECORD_TYPE
3218	   || TREE_CODE (type) == UNION_TYPE)
3219    /* If TYPE is a struct or union type then we're reading or
3220       writing an entire struct.  Thus, we don't know anything about
3221       aliasing.  (In theory, such an access can only alias objects
3222       whose type is the same as one of the fields, recursively, but
3223       we don't yet make any use of that information.)  */
3224    TYPE_ALIAS_SET (type) = 0;
3225  else if (TREE_CODE (type) == POINTER_TYPE
3226	   || TREE_CODE (type) == REFERENCE_TYPE)
3227    {
3228      tree t;
3229
3230      /* Unfortunately, there is no canonical form of a pointer type.
3231	 In particular, if we have `typedef int I', then `int *', and
3232	 `I *' are different types.  So, we have to pick a canonical
3233	 representative.  We do this below.
3234
3235	 Technically, this approach is actually more conservative that
3236	 it needs to be.  In particular, `const int *' and `int *'
3237	 chould be in different alias sets, according to the C and C++
3238	 standard, since their types are not the same, and so,
3239	 technically, an `int **' and `const int **' cannot point at
3240	 the same thing.
3241
3242         But, the standard is wrong.  In particular, this code is
3243	 legal C++:
3244
3245            int *ip;
3246            int **ipp = &ip;
3247            const int* const* cipp = &ip;
3248
3249         And, it doesn't make sense for that to be legal unless you
3250	 can dereference IPP and CIPP.  So, we ignore cv-qualifiers on
3251	 the pointed-to types.  This issue has been reported to the
3252	 C++ committee.  */
3253      t = TYPE_MAIN_VARIANT (TREE_TYPE (type));
3254      t = ((TREE_CODE (type) == POINTER_TYPE)
3255	   ? build_pointer_type (t) : build_reference_type (t));
3256      if (t != type)
3257	TYPE_ALIAS_SET (type) = c_get_alias_set (t);
3258    }
3259
3260  if (!TYPE_ALIAS_SET_KNOWN_P (type))
3261    {
3262      /* Types that are not allocated on the permanent obstack are not
3263	 placed in the type hash table.  Thus, there can be multiple
3264	 copies of identical types in local scopes.  In the long run,
3265	 all types should be permanent.  */
3266      if (! TREE_PERMANENT (type))
3267	TYPE_ALIAS_SET (type) = 0;
3268      else
3269	/* TYPE is something we haven't seen before.  Put it in a new
3270	   alias set.  */
3271	TYPE_ALIAS_SET (type) = new_alias_set ();
3272    }
3273
3274  return TYPE_ALIAS_SET (type);
3275}
3276