ARM support for unordered FP operations.
[official-gcc.git] / gcc / c-common.c
blob2b4b0d5515f6c06749d57dadb0dc4d17c7555d84
1 /* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000
3 Free Software Foundation, Inc.
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 #include "config.h"
23 #include "system.h"
24 #include "tree.h"
25 #include "c-common.h"
26 #include "flags.h"
27 #include "toplev.h"
28 #include "output.h"
29 #include "c-pragma.h"
30 #include "rtl.h"
31 #include "ggc.h"
32 #include "expr.h"
33 #include "tm_p.h"
35 #if USE_CPPLIB
36 #include "cpplib.h"
37 cpp_reader parse_in;
38 cpp_options parse_options;
39 enum cpp_token cpp_token;
40 #endif
42 #undef WCHAR_TYPE_SIZE
43 #define WCHAR_TYPE_SIZE TYPE_PRECISION (wchar_type_node)
45 /* The following symbols are subsumed in the c_global_trees array, and
46 listed here individually for documentation purposes.
48 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
50 tree short_integer_type_node;
51 tree long_integer_type_node;
52 tree long_long_integer_type_node;
54 tree short_unsigned_type_node;
55 tree long_unsigned_type_node;
56 tree long_long_unsigned_type_node;
58 tree boolean_type_node;
59 tree boolean_false_node;
60 tree boolean_true_node;
62 tree ptrdiff_type_node;
64 tree unsigned_char_type_node;
65 tree signed_char_type_node;
66 tree wchar_type_node;
67 tree signed_wchar_type_node;
68 tree unsigned_wchar_type_node;
70 tree float_type_node;
71 tree double_type_node;
72 tree long_double_type_node;
74 tree complex_integer_type_node;
75 tree complex_float_type_node;
76 tree complex_double_type_node;
77 tree complex_long_double_type_node;
79 tree intQI_type_node;
80 tree intHI_type_node;
81 tree intSI_type_node;
82 tree intDI_type_node;
83 tree intTI_type_node;
85 tree unsigned_intQI_type_node;
86 tree unsigned_intHI_type_node;
87 tree unsigned_intSI_type_node;
88 tree unsigned_intDI_type_node;
89 tree unsigned_intTI_type_node;
91 tree widest_integer_literal_type_node;
92 tree widest_unsigned_literal_type_node;
94 Nodes for types `void *' and `const void *'.
96 tree ptr_type_node, const_ptr_type_node;
98 Nodes for types `char *' and `const char *'.
100 tree string_type_node, const_string_type_node;
102 Type `char[SOMENUMBER]'.
103 Used when an array of char is needed and the size is irrelevant.
105 tree char_array_type_node;
107 Type `int[SOMENUMBER]' or something like it.
108 Used when an array of int needed and the size is irrelevant.
110 tree int_array_type_node;
112 Type `wchar_t[SOMENUMBER]' or something like it.
113 Used when a wide string literal is created.
115 tree wchar_array_type_node;
117 Type `int ()' -- used for implicit declaration of functions.
119 tree default_function_type;
121 Function types `int (int)', etc.
123 tree int_ftype_int;
124 tree void_ftype;
125 tree void_ftype_ptr;
126 tree int_ftype_int;
127 tree ptr_ftype_sizetype;
129 A VOID_TYPE node, packaged in a TREE_LIST.
131 tree void_list_node;
135 tree c_global_trees[CTI_MAX];
137 /* The elements of `ridpointers' are identifier nodes for the reserved
138 type names and storage classes. It is indexed by a RID_... value. */
139 tree *ridpointers;
141 tree (*make_fname_decl) PARAMS ((tree, const char *, int));
143 /* Nonzero means the expression being parsed will never be evaluated.
144 This is a count, since unevaluated expressions can nest. */
145 int skip_evaluation;
147 enum attrs {A_PACKED, A_NOCOMMON, A_COMMON, A_NORETURN, A_CONST, A_T_UNION,
148 A_NO_CHECK_MEMORY_USAGE, A_NO_INSTRUMENT_FUNCTION,
149 A_CONSTRUCTOR, A_DESTRUCTOR, A_MODE, A_SECTION, A_ALIGNED,
150 A_UNUSED, A_FORMAT, A_FORMAT_ARG, A_WEAK, A_ALIAS, A_MALLOC,
151 A_NO_LIMIT_STACK, A_PURE};
153 enum format_type { printf_format_type, scanf_format_type,
154 strftime_format_type };
156 static void add_attribute PARAMS ((enum attrs, const char *,
157 int, int, int));
158 static void init_attributes PARAMS ((void));
159 static void record_function_format PARAMS ((tree, tree, enum format_type,
160 int, int));
161 static void record_international_format PARAMS ((tree, tree, int));
162 static int default_valid_lang_attribute PARAMS ((tree, tree, tree, tree));
164 /* Keep a stack of if statements. We record the number of compound
165 statements seen up to the if keyword, as well as the line number
166 and file of the if. If a potentially ambiguous else is seen, that
167 fact is recorded; the warning is issued when we can be sure that
168 the enclosing if statement does not have an else branch. */
169 typedef struct
171 int compstmt_count;
172 int line;
173 const char *file;
174 int needs_warning;
175 } if_elt;
176 static void tfaff PARAMS ((void));
178 static if_elt *if_stack;
180 /* Amount of space in the if statement stack. */
181 static int if_stack_space = 0;
183 /* Stack pointer. */
184 static int if_stack_pointer = 0;
186 /* Generate RTL for the start of an if-then, and record the start of it
187 for ambiguous else detection. */
189 void
190 c_expand_start_cond (cond, exitflag, compstmt_count)
191 tree cond;
192 int exitflag;
193 int compstmt_count;
195 /* Make sure there is enough space on the stack. */
196 if (if_stack_space == 0)
198 if_stack_space = 10;
199 if_stack = (if_elt *)xmalloc (10 * sizeof (if_elt));
201 else if (if_stack_space == if_stack_pointer)
203 if_stack_space += 10;
204 if_stack = (if_elt *)xrealloc (if_stack, if_stack_space * sizeof (if_elt));
207 /* Record this if statement. */
208 if_stack[if_stack_pointer].compstmt_count = compstmt_count;
209 if_stack[if_stack_pointer].file = input_filename;
210 if_stack[if_stack_pointer].line = lineno;
211 if_stack[if_stack_pointer].needs_warning = 0;
212 if_stack_pointer++;
214 expand_start_cond (cond, exitflag);
217 /* Generate RTL for the end of an if-then. Optionally warn if a nested
218 if statement had an ambiguous else clause. */
220 void
221 c_expand_end_cond ()
223 if_stack_pointer--;
224 if (if_stack[if_stack_pointer].needs_warning)
225 warning_with_file_and_line (if_stack[if_stack_pointer].file,
226 if_stack[if_stack_pointer].line,
227 "suggest explicit braces to avoid ambiguous `else'");
228 expand_end_cond ();
231 /* Generate RTL between the then-clause and the else-clause
232 of an if-then-else. */
234 void
235 c_expand_start_else ()
237 /* An ambiguous else warning must be generated for the enclosing if
238 statement, unless we see an else branch for that one, too. */
239 if (warn_parentheses
240 && if_stack_pointer > 1
241 && (if_stack[if_stack_pointer - 1].compstmt_count
242 == if_stack[if_stack_pointer - 2].compstmt_count))
243 if_stack[if_stack_pointer - 2].needs_warning = 1;
245 /* Even if a nested if statement had an else branch, it can't be
246 ambiguous if this one also has an else. So don't warn in that
247 case. Also don't warn for any if statements nested in this else. */
248 if_stack[if_stack_pointer - 1].needs_warning = 0;
249 if_stack[if_stack_pointer - 1].compstmt_count--;
251 expand_start_else ();
254 /* Make bindings for __FUNCTION__, __PRETTY_FUNCTION__, and __func__. */
256 void
257 declare_function_name ()
259 const char *name, *printable_name;
261 if (current_function_decl == NULL)
263 name = "";
264 printable_name = "top level";
266 else
268 /* Allow functions to be nameless (such as artificial ones). */
269 if (DECL_NAME (current_function_decl))
270 name = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
271 else
272 name = "";
273 printable_name = (*decl_printable_name) (current_function_decl, 2);
276 (*make_fname_decl) (get_identifier ("__FUNCTION__"), name, 0);
277 (*make_fname_decl) (get_identifier ("__PRETTY_FUNCTION__"), printable_name, 1);
278 /* The ISO C people "of course" couldn't use __FUNCTION__ in the
279 ISO C 99 standard; instead a new variable is invented. */
280 (*make_fname_decl) (get_identifier ("__func__"), name, 0);
283 /* Given a chain of STRING_CST nodes,
284 concatenate them into one STRING_CST
285 and give it a suitable array-of-chars data type. */
287 tree
288 combine_strings (strings)
289 tree strings;
291 register tree value, t;
292 register int length = 1;
293 int wide_length = 0;
294 int wide_flag = 0;
295 int wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
296 int nchars;
297 const int nchars_max = flag_isoc99 ? 4095 : 509;
299 if (TREE_CHAIN (strings))
301 /* More than one in the chain, so concatenate. */
302 register char *p, *q;
304 /* Don't include the \0 at the end of each substring,
305 except for the last one.
306 Count wide strings and ordinary strings separately. */
307 for (t = strings; t; t = TREE_CHAIN (t))
309 if (TREE_TYPE (t) == wchar_array_type_node)
311 wide_length += (TREE_STRING_LENGTH (t) - wchar_bytes);
312 wide_flag = 1;
314 else
315 length += (TREE_STRING_LENGTH (t) - 1);
318 /* If anything is wide, the non-wides will be converted,
319 which makes them take more space. */
320 if (wide_flag)
321 length = length * wchar_bytes + wide_length;
323 p = ggc_alloc_string (NULL, length);
325 /* Copy the individual strings into the new combined string.
326 If the combined string is wide, convert the chars to ints
327 for any individual strings that are not wide. */
329 q = p;
330 for (t = strings; t; t = TREE_CHAIN (t))
332 int len = (TREE_STRING_LENGTH (t)
333 - ((TREE_TYPE (t) == wchar_array_type_node)
334 ? wchar_bytes : 1));
335 if ((TREE_TYPE (t) == wchar_array_type_node) == wide_flag)
337 memcpy (q, TREE_STRING_POINTER (t), len);
338 q += len;
340 else
342 int i;
343 for (i = 0; i < len; i++)
345 if (WCHAR_TYPE_SIZE == HOST_BITS_PER_SHORT)
346 ((short *) q)[i] = TREE_STRING_POINTER (t)[i];
347 else
348 ((int *) q)[i] = TREE_STRING_POINTER (t)[i];
350 q += len * wchar_bytes;
353 if (wide_flag)
355 int i;
356 for (i = 0; i < wchar_bytes; i++)
357 *q++ = 0;
359 else
360 *q = 0;
362 value = make_node (STRING_CST);
363 TREE_STRING_POINTER (value) = p;
364 TREE_STRING_LENGTH (value) = length;
366 else
368 value = strings;
369 length = TREE_STRING_LENGTH (value);
370 if (TREE_TYPE (value) == wchar_array_type_node)
371 wide_flag = 1;
374 /* Compute the number of elements, for the array type. */
375 nchars = wide_flag ? length / wchar_bytes : length;
377 if (pedantic && nchars > nchars_max)
378 pedwarn ("string length `%d' is greater than the minimum length `%d' ISO C%d is required to support",
379 nchars, nchars_max, flag_isoc99 ? 99 : 89);
381 /* Create the array type for the string constant.
382 -Wwrite-strings says make the string constant an array of const char
383 so that copying it to a non-const pointer will get a warning.
384 For C++, this is the standard behavior. */
385 if (flag_const_strings
386 && (! flag_traditional && ! flag_writable_strings))
388 tree elements
389 = build_type_variant (wide_flag ? wchar_type_node : char_type_node,
390 1, 0);
391 TREE_TYPE (value)
392 = build_array_type (elements,
393 build_index_type (build_int_2 (nchars - 1, 0)));
395 else
396 TREE_TYPE (value)
397 = build_array_type (wide_flag ? wchar_type_node : char_type_node,
398 build_index_type (build_int_2 (nchars - 1, 0)));
400 TREE_CONSTANT (value) = 1;
401 TREE_READONLY (value) = ! flag_writable_strings;
402 TREE_STATIC (value) = 1;
403 return value;
406 /* To speed up processing of attributes, we maintain an array of
407 IDENTIFIER_NODES and the corresponding attribute types. */
409 /* Array to hold attribute information. */
411 static struct {enum attrs id; tree name; int min, max, decl_req;} attrtab[50];
413 static int attrtab_idx = 0;
415 /* Add an entry to the attribute table above. */
417 static void
418 add_attribute (id, string, min_len, max_len, decl_req)
419 enum attrs id;
420 const char *string;
421 int min_len, max_len;
422 int decl_req;
424 char buf[100];
426 attrtab[attrtab_idx].id = id;
427 attrtab[attrtab_idx].name = get_identifier (string);
428 attrtab[attrtab_idx].min = min_len;
429 attrtab[attrtab_idx].max = max_len;
430 attrtab[attrtab_idx++].decl_req = decl_req;
432 sprintf (buf, "__%s__", string);
434 attrtab[attrtab_idx].id = id;
435 attrtab[attrtab_idx].name = get_identifier (buf);
436 attrtab[attrtab_idx].min = min_len;
437 attrtab[attrtab_idx].max = max_len;
438 attrtab[attrtab_idx++].decl_req = decl_req;
441 /* Initialize attribute table. */
443 static void
444 init_attributes ()
446 add_attribute (A_PACKED, "packed", 0, 0, 0);
447 add_attribute (A_NOCOMMON, "nocommon", 0, 0, 1);
448 add_attribute (A_COMMON, "common", 0, 0, 1);
449 add_attribute (A_NORETURN, "noreturn", 0, 0, 1);
450 add_attribute (A_NORETURN, "volatile", 0, 0, 1);
451 add_attribute (A_UNUSED, "unused", 0, 0, 0);
452 add_attribute (A_CONST, "const", 0, 0, 1);
453 add_attribute (A_T_UNION, "transparent_union", 0, 0, 0);
454 add_attribute (A_CONSTRUCTOR, "constructor", 0, 0, 1);
455 add_attribute (A_DESTRUCTOR, "destructor", 0, 0, 1);
456 add_attribute (A_MODE, "mode", 1, 1, 1);
457 add_attribute (A_SECTION, "section", 1, 1, 1);
458 add_attribute (A_ALIGNED, "aligned", 0, 1, 0);
459 add_attribute (A_FORMAT, "format", 3, 3, 1);
460 add_attribute (A_FORMAT_ARG, "format_arg", 1, 1, 1);
461 add_attribute (A_WEAK, "weak", 0, 0, 1);
462 add_attribute (A_ALIAS, "alias", 1, 1, 1);
463 add_attribute (A_NO_INSTRUMENT_FUNCTION, "no_instrument_function", 0, 0, 1);
464 add_attribute (A_NO_CHECK_MEMORY_USAGE, "no_check_memory_usage", 0, 0, 1);
465 add_attribute (A_MALLOC, "malloc", 0, 0, 1);
466 add_attribute (A_NO_LIMIT_STACK, "no_stack_limit", 0, 0, 1);
467 add_attribute (A_PURE, "pure", 0, 0, 1);
470 /* Default implementation of valid_lang_attribute, below. By default, there
471 are no language-specific attributes. */
473 static int
474 default_valid_lang_attribute (attr_name, attr_args, decl, type)
475 tree attr_name ATTRIBUTE_UNUSED;
476 tree attr_args ATTRIBUTE_UNUSED;
477 tree decl ATTRIBUTE_UNUSED;
478 tree type ATTRIBUTE_UNUSED;
480 return 0;
483 /* Return a 1 if ATTR_NAME and ATTR_ARGS denote a valid language-specific
484 attribute for either declaration DECL or type TYPE and 0 otherwise. */
486 int (*valid_lang_attribute) PARAMS ((tree, tree, tree, tree))
487 = default_valid_lang_attribute;
489 /* Process the attributes listed in ATTRIBUTES and PREFIX_ATTRIBUTES
490 and install them in NODE, which is either a DECL (including a TYPE_DECL)
491 or a TYPE. PREFIX_ATTRIBUTES can appear after the declaration specifiers
492 and declaration modifiers but before the declaration proper. */
494 void
495 decl_attributes (node, attributes, prefix_attributes)
496 tree node, attributes, prefix_attributes;
498 tree decl = 0, type = 0;
499 int is_type = 0;
500 tree a;
502 if (attrtab_idx == 0)
503 init_attributes ();
505 if (DECL_P (node))
507 decl = node;
508 type = TREE_TYPE (decl);
509 is_type = TREE_CODE (node) == TYPE_DECL;
511 else if (TYPE_P (node))
512 type = node, is_type = 1;
514 #ifdef PRAGMA_INSERT_ATTRIBUTES
515 /* If the code in c-pragma.c wants to insert some attributes then
516 allow it to do so. Do this before allowing machine back ends to
517 insert attributes, so that they have the opportunity to override
518 anything done here. */
519 PRAGMA_INSERT_ATTRIBUTES (node, & attributes, & prefix_attributes);
520 #endif
522 #ifdef INSERT_ATTRIBUTES
523 INSERT_ATTRIBUTES (node, & attributes, & prefix_attributes);
524 #endif
526 attributes = chainon (prefix_attributes, attributes);
528 for (a = attributes; a; a = TREE_CHAIN (a))
530 tree name = TREE_PURPOSE (a);
531 tree args = TREE_VALUE (a);
532 int i;
533 enum attrs id;
535 for (i = 0; i < attrtab_idx; i++)
536 if (attrtab[i].name == name)
537 break;
539 if (i == attrtab_idx)
541 if (! valid_machine_attribute (name, args, decl, type)
542 && ! (* valid_lang_attribute) (name, args, decl, type))
543 warning ("`%s' attribute directive ignored",
544 IDENTIFIER_POINTER (name));
545 else if (decl != 0)
546 type = TREE_TYPE (decl);
547 continue;
549 else if (attrtab[i].decl_req && decl == 0)
551 warning ("`%s' attribute does not apply to types",
552 IDENTIFIER_POINTER (name));
553 continue;
555 else if (list_length (args) < attrtab[i].min
556 || list_length (args) > attrtab[i].max)
558 error ("wrong number of arguments specified for `%s' attribute",
559 IDENTIFIER_POINTER (name));
560 continue;
563 id = attrtab[i].id;
564 switch (id)
566 case A_PACKED:
567 if (is_type)
568 TYPE_PACKED (type) = 1;
569 else if (TREE_CODE (decl) == FIELD_DECL)
570 DECL_PACKED (decl) = 1;
571 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
572 used for DECL_REGISTER. It wouldn't mean anything anyway. */
573 else
574 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
575 break;
577 case A_NOCOMMON:
578 if (TREE_CODE (decl) == VAR_DECL)
579 DECL_COMMON (decl) = 0;
580 else
581 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
582 break;
584 case A_COMMON:
585 if (TREE_CODE (decl) == VAR_DECL)
586 DECL_COMMON (decl) = 1;
587 else
588 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
589 break;
591 case A_NORETURN:
592 if (TREE_CODE (decl) == FUNCTION_DECL)
593 TREE_THIS_VOLATILE (decl) = 1;
594 else if (TREE_CODE (type) == POINTER_TYPE
595 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
596 TREE_TYPE (decl) = type
597 = build_pointer_type
598 (build_type_variant (TREE_TYPE (type),
599 TREE_READONLY (TREE_TYPE (type)), 1));
600 else
601 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
602 break;
604 case A_MALLOC:
605 if (TREE_CODE (decl) == FUNCTION_DECL)
606 DECL_IS_MALLOC (decl) = 1;
607 /* ??? TODO: Support types. */
608 else
609 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
610 break;
612 case A_UNUSED:
613 if (is_type)
614 if (decl)
615 TREE_USED (decl) = 1;
616 else
617 TREE_USED (type) = 1;
618 else if (TREE_CODE (decl) == PARM_DECL
619 || TREE_CODE (decl) == VAR_DECL
620 || TREE_CODE (decl) == FUNCTION_DECL
621 || TREE_CODE (decl) == LABEL_DECL)
622 TREE_USED (decl) = 1;
623 else
624 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
625 break;
627 case A_CONST:
628 if (TREE_CODE (decl) == FUNCTION_DECL)
629 TREE_READONLY (decl) = 1;
630 else if (TREE_CODE (type) == POINTER_TYPE
631 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
632 TREE_TYPE (decl) = type
633 = build_pointer_type
634 (build_type_variant (TREE_TYPE (type), 1,
635 TREE_THIS_VOLATILE (TREE_TYPE (type))));
636 else
637 warning ( "`%s' attribute ignored", IDENTIFIER_POINTER (name));
638 break;
640 case A_PURE:
641 if (TREE_CODE (decl) == FUNCTION_DECL)
642 DECL_IS_PURE (decl) = 1;
643 /* ??? TODO: Support types. */
644 else
645 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
646 break;
649 case A_T_UNION:
650 if (is_type
651 && TREE_CODE (type) == UNION_TYPE
652 && (decl == 0
653 || (TYPE_FIELDS (type) != 0
654 && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))))
655 TYPE_TRANSPARENT_UNION (type) = 1;
656 else if (decl != 0 && TREE_CODE (decl) == PARM_DECL
657 && TREE_CODE (type) == UNION_TYPE
658 && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))
659 DECL_TRANSPARENT_UNION (decl) = 1;
660 else
661 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
662 break;
664 case A_CONSTRUCTOR:
665 if (TREE_CODE (decl) == FUNCTION_DECL
666 && TREE_CODE (type) == FUNCTION_TYPE
667 && decl_function_context (decl) == 0)
669 DECL_STATIC_CONSTRUCTOR (decl) = 1;
670 TREE_USED (decl) = 1;
672 else
673 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
674 break;
676 case A_DESTRUCTOR:
677 if (TREE_CODE (decl) == FUNCTION_DECL
678 && TREE_CODE (type) == FUNCTION_TYPE
679 && decl_function_context (decl) == 0)
681 DECL_STATIC_DESTRUCTOR (decl) = 1;
682 TREE_USED (decl) = 1;
684 else
685 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
686 break;
688 case A_MODE:
689 if (TREE_CODE (TREE_VALUE (args)) != IDENTIFIER_NODE)
690 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
691 else
693 int j;
694 const char *p = IDENTIFIER_POINTER (TREE_VALUE (args));
695 int len = strlen (p);
696 enum machine_mode mode = VOIDmode;
697 tree typefm;
699 if (len > 4 && p[0] == '_' && p[1] == '_'
700 && p[len - 1] == '_' && p[len - 2] == '_')
702 char *newp = (char *) alloca (len - 1);
704 strcpy (newp, &p[2]);
705 newp[len - 4] = '\0';
706 p = newp;
709 /* Give this decl a type with the specified mode.
710 First check for the special modes. */
711 if (! strcmp (p, "byte"))
712 mode = byte_mode;
713 else if (!strcmp (p, "word"))
714 mode = word_mode;
715 else if (! strcmp (p, "pointer"))
716 mode = ptr_mode;
717 else
718 for (j = 0; j < NUM_MACHINE_MODES; j++)
719 if (!strcmp (p, GET_MODE_NAME (j)))
720 mode = (enum machine_mode) j;
722 if (mode == VOIDmode)
723 error ("unknown machine mode `%s'", p);
724 else if (0 == (typefm = type_for_mode (mode,
725 TREE_UNSIGNED (type))))
726 error ("no data type for mode `%s'", p);
727 else
729 TREE_TYPE (decl) = type = typefm;
730 DECL_SIZE (decl) = DECL_SIZE_UNIT (decl) = 0;
731 layout_decl (decl, 0);
734 break;
736 case A_SECTION:
737 #ifdef ASM_OUTPUT_SECTION_NAME
738 if ((TREE_CODE (decl) == FUNCTION_DECL
739 || TREE_CODE (decl) == VAR_DECL)
740 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
742 if (TREE_CODE (decl) == VAR_DECL
743 && current_function_decl != NULL_TREE
744 && ! TREE_STATIC (decl))
745 error_with_decl (decl,
746 "section attribute cannot be specified for local variables");
747 /* The decl may have already been given a section attribute from
748 a previous declaration. Ensure they match. */
749 else if (DECL_SECTION_NAME (decl) != NULL_TREE
750 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
751 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
752 error_with_decl (node,
753 "section of `%s' conflicts with previous declaration");
754 else
755 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
757 else
758 error_with_decl (node,
759 "section attribute not allowed for `%s'");
760 #else
761 error_with_decl (node,
762 "section attributes are not supported for this target");
763 #endif
764 break;
766 case A_ALIGNED:
768 tree align_expr
769 = (args ? TREE_VALUE (args)
770 : size_int (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
771 int i;
773 /* Strip any NOPs of any kind. */
774 while (TREE_CODE (align_expr) == NOP_EXPR
775 || TREE_CODE (align_expr) == CONVERT_EXPR
776 || TREE_CODE (align_expr) == NON_LVALUE_EXPR)
777 align_expr = TREE_OPERAND (align_expr, 0);
779 if (TREE_CODE (align_expr) != INTEGER_CST)
781 error ("requested alignment is not a constant");
782 continue;
785 if ((i = tree_log2 (align_expr)) == -1)
786 error ("requested alignment is not a power of 2");
787 else if (i > HOST_BITS_PER_INT - 2)
788 error ("requested alignment is too large");
789 else if (is_type)
791 if (decl)
793 DECL_ALIGN (decl) = (1 << i) * BITS_PER_UNIT;
794 DECL_USER_ALIGN (decl) = 1;
796 else
798 TYPE_ALIGN (type) = (1 << i) * BITS_PER_UNIT;
799 TYPE_USER_ALIGN (type) = 1;
802 else if (TREE_CODE (decl) != VAR_DECL
803 && TREE_CODE (decl) != FIELD_DECL)
804 error_with_decl (decl,
805 "alignment may not be specified for `%s'");
806 else
808 DECL_ALIGN (decl) = (1 << i) * BITS_PER_UNIT;
809 DECL_USER_ALIGN (decl) = 1;
812 break;
814 case A_FORMAT:
816 tree format_type_id = TREE_VALUE (args);
817 tree format_num_expr = TREE_VALUE (TREE_CHAIN (args));
818 tree first_arg_num_expr
819 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)));
820 unsigned HOST_WIDE_INT format_num, first_arg_num;
821 enum format_type format_type;
822 tree argument;
823 unsigned int arg_num;
825 if (TREE_CODE (decl) != FUNCTION_DECL)
827 error_with_decl (decl,
828 "argument format specified for non-function `%s'");
829 continue;
832 if (TREE_CODE (format_type_id) != IDENTIFIER_NODE)
834 error ("unrecognized format specifier");
835 continue;
837 else
839 const char *p = IDENTIFIER_POINTER (format_type_id);
841 if (!strcmp (p, "printf") || !strcmp (p, "__printf__"))
842 format_type = printf_format_type;
843 else if (!strcmp (p, "scanf") || !strcmp (p, "__scanf__"))
844 format_type = scanf_format_type;
845 else if (!strcmp (p, "strftime")
846 || !strcmp (p, "__strftime__"))
847 format_type = strftime_format_type;
848 else
850 warning ("`%s' is an unrecognized format function type", p);
851 continue;
855 /* Strip any conversions from the string index and first arg number
856 and verify they are constants. */
857 while (TREE_CODE (format_num_expr) == NOP_EXPR
858 || TREE_CODE (format_num_expr) == CONVERT_EXPR
859 || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
860 format_num_expr = TREE_OPERAND (format_num_expr, 0);
862 while (TREE_CODE (first_arg_num_expr) == NOP_EXPR
863 || TREE_CODE (first_arg_num_expr) == CONVERT_EXPR
864 || TREE_CODE (first_arg_num_expr) == NON_LVALUE_EXPR)
865 first_arg_num_expr = TREE_OPERAND (first_arg_num_expr, 0);
867 if (TREE_CODE (format_num_expr) != INTEGER_CST
868 || TREE_INT_CST_HIGH (format_num_expr) != 0
869 || TREE_CODE (first_arg_num_expr) != INTEGER_CST
870 || TREE_INT_CST_HIGH (first_arg_num_expr) != 0)
872 error ("format string has invalid operand number");
873 continue;
876 format_num = TREE_INT_CST_LOW (format_num_expr);
877 first_arg_num = TREE_INT_CST_LOW (first_arg_num_expr);
878 if (first_arg_num != 0 && first_arg_num <= format_num)
880 error ("format string arg follows the args to be formatted");
881 continue;
884 /* If a parameter list is specified, verify that the format_num
885 argument is actually a string, in case the format attribute
886 is in error. */
887 argument = TYPE_ARG_TYPES (type);
888 if (argument)
890 for (arg_num = 1; argument != 0 && arg_num != format_num;
891 ++arg_num, argument = TREE_CHAIN (argument))
894 if (! argument
895 || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
896 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
897 != char_type_node))
899 error ("format string arg not a string type");
900 continue;
903 else if (first_arg_num != 0)
905 /* Verify that first_arg_num points to the last arg,
906 the ... */
907 while (argument)
908 arg_num++, argument = TREE_CHAIN (argument);
910 if (arg_num != first_arg_num)
912 error ("args to be formatted is not '...'");
913 continue;
918 record_function_format (DECL_NAME (decl),
919 DECL_ASSEMBLER_NAME (decl),
920 format_type, format_num, first_arg_num);
921 break;
924 case A_FORMAT_ARG:
926 tree format_num_expr = TREE_VALUE (args);
927 unsigned HOST_WIDE_INT format_num;
928 unsigned int arg_num;
929 tree argument;
931 if (TREE_CODE (decl) != FUNCTION_DECL)
933 error_with_decl (decl,
934 "argument format specified for non-function `%s'");
935 continue;
938 /* Strip any conversions from the first arg number and verify it
939 is a constant. */
940 while (TREE_CODE (format_num_expr) == NOP_EXPR
941 || TREE_CODE (format_num_expr) == CONVERT_EXPR
942 || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
943 format_num_expr = TREE_OPERAND (format_num_expr, 0);
945 if (TREE_CODE (format_num_expr) != INTEGER_CST
946 || TREE_INT_CST_HIGH (format_num_expr) != 0)
948 error ("format string has invalid operand number");
949 continue;
952 format_num = TREE_INT_CST_LOW (format_num_expr);
954 /* If a parameter list is specified, verify that the format_num
955 argument is actually a string, in case the format attribute
956 is in error. */
957 argument = TYPE_ARG_TYPES (type);
958 if (argument)
960 for (arg_num = 1; argument != 0 && arg_num != format_num;
961 ++arg_num, argument = TREE_CHAIN (argument))
964 if (! argument
965 || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
966 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
967 != char_type_node))
969 error ("format string arg not a string type");
970 continue;
974 if (TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) != POINTER_TYPE
975 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (TREE_TYPE (decl))))
976 != char_type_node))
978 error ("function does not return string type");
979 continue;
982 record_international_format (DECL_NAME (decl),
983 DECL_ASSEMBLER_NAME (decl),
984 format_num);
985 break;
988 case A_WEAK:
989 declare_weak (decl);
990 break;
992 case A_ALIAS:
993 if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
994 || (TREE_CODE (decl) != FUNCTION_DECL && ! DECL_EXTERNAL (decl)))
995 error_with_decl (decl,
996 "`%s' defined both normally and as an alias");
997 else if (decl_function_context (decl) == 0)
999 tree id;
1001 id = TREE_VALUE (args);
1002 if (TREE_CODE (id) != STRING_CST)
1004 error ("alias arg not a string");
1005 break;
1007 id = get_identifier (TREE_STRING_POINTER (id));
1008 /* This counts as a use of the object pointed to. */
1009 TREE_USED (id) = 1;
1011 if (TREE_CODE (decl) == FUNCTION_DECL)
1012 DECL_INITIAL (decl) = error_mark_node;
1013 else
1014 DECL_EXTERNAL (decl) = 0;
1015 assemble_alias (decl, id);
1017 else
1018 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
1019 break;
1021 case A_NO_CHECK_MEMORY_USAGE:
1022 if (TREE_CODE (decl) != FUNCTION_DECL)
1024 error_with_decl (decl,
1025 "`%s' attribute applies only to functions",
1026 IDENTIFIER_POINTER (name));
1028 else if (DECL_INITIAL (decl))
1030 error_with_decl (decl,
1031 "can't set `%s' attribute after definition",
1032 IDENTIFIER_POINTER (name));
1034 else
1035 DECL_NO_CHECK_MEMORY_USAGE (decl) = 1;
1036 break;
1038 case A_NO_INSTRUMENT_FUNCTION:
1039 if (TREE_CODE (decl) != FUNCTION_DECL)
1041 error_with_decl (decl,
1042 "`%s' attribute applies only to functions",
1043 IDENTIFIER_POINTER (name));
1045 else if (DECL_INITIAL (decl))
1047 error_with_decl (decl,
1048 "can't set `%s' attribute after definition",
1049 IDENTIFIER_POINTER (name));
1051 else
1052 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
1053 break;
1055 case A_NO_LIMIT_STACK:
1056 if (TREE_CODE (decl) != FUNCTION_DECL)
1058 error_with_decl (decl,
1059 "`%s' attribute applies only to functions",
1060 IDENTIFIER_POINTER (name));
1062 else if (DECL_INITIAL (decl))
1064 error_with_decl (decl,
1065 "can't set `%s' attribute after definition",
1066 IDENTIFIER_POINTER (name));
1068 else
1069 DECL_NO_LIMIT_STACK (decl) = 1;
1070 break;
1075 /* Split SPECS_ATTRS, a list of declspecs and prefix attributes, into two
1076 lists. SPECS_ATTRS may also be just a typespec (eg: RECORD_TYPE).
1078 The head of the declspec list is stored in DECLSPECS.
1079 The head of the attribute list is stored in PREFIX_ATTRIBUTES.
1081 Note that attributes in SPECS_ATTRS are stored in the TREE_PURPOSE of
1082 the list elements. We drop the containing TREE_LIST nodes and link the
1083 resulting attributes together the way decl_attributes expects them. */
1085 void
1086 split_specs_attrs (specs_attrs, declspecs, prefix_attributes)
1087 tree specs_attrs;
1088 tree *declspecs, *prefix_attributes;
1090 tree t, s, a, next, specs, attrs;
1092 /* This can happen after an __extension__ in pedantic mode. */
1093 if (specs_attrs != NULL_TREE
1094 && TREE_CODE (specs_attrs) == INTEGER_CST)
1096 *declspecs = NULL_TREE;
1097 *prefix_attributes = NULL_TREE;
1098 return;
1101 /* This can happen in c++ (eg: decl: typespec initdecls ';'). */
1102 if (specs_attrs != NULL_TREE
1103 && TREE_CODE (specs_attrs) != TREE_LIST)
1105 *declspecs = specs_attrs;
1106 *prefix_attributes = NULL_TREE;
1107 return;
1110 /* Remember to keep the lists in the same order, element-wise. */
1112 specs = s = NULL_TREE;
1113 attrs = a = NULL_TREE;
1114 for (t = specs_attrs; t; t = next)
1116 next = TREE_CHAIN (t);
1117 /* Declspecs have a non-NULL TREE_VALUE. */
1118 if (TREE_VALUE (t) != NULL_TREE)
1120 if (specs == NULL_TREE)
1121 specs = s = t;
1122 else
1124 TREE_CHAIN (s) = t;
1125 s = t;
1128 else
1130 if (attrs == NULL_TREE)
1131 attrs = a = TREE_PURPOSE (t);
1132 else
1134 TREE_CHAIN (a) = TREE_PURPOSE (t);
1135 a = TREE_PURPOSE (t);
1137 /* More attrs can be linked here, move A to the end. */
1138 while (TREE_CHAIN (a) != NULL_TREE)
1139 a = TREE_CHAIN (a);
1143 /* Terminate the lists. */
1144 if (s != NULL_TREE)
1145 TREE_CHAIN (s) = NULL_TREE;
1146 if (a != NULL_TREE)
1147 TREE_CHAIN (a) = NULL_TREE;
1149 /* All done. */
1150 *declspecs = specs;
1151 *prefix_attributes = attrs;
1154 /* Strip attributes from SPECS_ATTRS, a list of declspecs and attributes.
1155 This function is used by the parser when a rule will accept attributes
1156 in a particular position, but we don't want to support that just yet.
1158 A warning is issued for every ignored attribute. */
1160 tree
1161 strip_attrs (specs_attrs)
1162 tree specs_attrs;
1164 tree specs, attrs;
1166 split_specs_attrs (specs_attrs, &specs, &attrs);
1168 while (attrs)
1170 warning ("`%s' attribute ignored",
1171 IDENTIFIER_POINTER (TREE_PURPOSE (attrs)));
1172 attrs = TREE_CHAIN (attrs);
1175 return specs;
1178 /* Check a printf/fprintf/sprintf/scanf/fscanf/sscanf format against
1179 a parameter list. */
1181 #define T_I &integer_type_node
1182 #define T_L &long_integer_type_node
1183 #define T_LL &long_long_integer_type_node
1184 #define T_S &short_integer_type_node
1185 #define T_UI &unsigned_type_node
1186 #define T_UL &long_unsigned_type_node
1187 #define T_ULL &long_long_unsigned_type_node
1188 #define T_US &short_unsigned_type_node
1189 #define T_F &float_type_node
1190 #define T_D &double_type_node
1191 #define T_LD &long_double_type_node
1192 #define T_C &char_type_node
1193 #define T_SC &signed_char_type_node
1194 #define T_UC &unsigned_char_type_node
1195 #define T_V &void_type_node
1196 #define T_W &wchar_type_node
1197 #define T_WI &wint_type_node
1198 #define T_ST &sizetype
1199 #define T_SST &signed_size_type_node
1200 #define T_PD &ptrdiff_type_node
1201 #define T_UPD &unsigned_ptrdiff_type_node
1202 #define T_IM NULL /* intmax_t not yet implemented. */
1203 #define T_UIM NULL /* uintmax_t not yet implemented. */
1205 typedef struct {
1206 const char *format_chars;
1207 int pointer_count;
1208 /* Type of argument if no length modifier is used. */
1209 tree *nolen;
1210 /* Type of argument if length modifier for shortening to byte is used.
1211 If NULL, then this modifier is not allowed. */
1212 tree *hhlen;
1213 /* Type of argument if length modifier for shortening is used.
1214 If NULL, then this modifier is not allowed. */
1215 tree *hlen;
1216 /* Type of argument if length modifier `l' is used.
1217 If NULL, then this modifier is not allowed. */
1218 tree *llen;
1219 /* Type of argument if length modifier `q' or `ll' is used.
1220 If NULL, then this modifier is not allowed. */
1221 tree *qlen;
1222 /* Type of argument if length modifier `L' is used.
1223 If NULL, then this modifier is not allowed. */
1224 tree *bigllen;
1225 /* Type of argument if length modifiers 'z' or `Z' is used.
1226 If NULL, then this modifier is not allowed. */
1227 tree *zlen;
1228 /* Type of argument if length modifier 't' is used.
1229 If NULL, then this modifier is not allowed. */
1230 tree *tlen;
1231 /* Type of argument if length modifier 'j' is used.
1232 If NULL, then this modifier is not allowed. */
1233 tree *jlen;
1234 /* List of other modifier characters allowed with these options. */
1235 const char *flag_chars;
1236 } format_char_info;
1238 static format_char_info print_char_table[] = {
1239 { "di", 0, T_I, T_I, T_I, T_L, T_LL, T_LL, T_SST, T_PD, T_IM, "-wp0 +'" },
1240 { "oxX", 0, T_UI, T_UI, T_UI, T_UL, T_ULL, T_ULL, T_ST, T_UPD, T_UIM, "-wp0#" },
1241 { "u", 0, T_UI, T_UI, T_UI, T_UL, T_ULL, T_ULL, T_ST, T_UPD, T_UIM, "-wp0'" },
1242 /* A GNU extension. */
1243 { "m", 0, T_V, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-wp" },
1244 { "fFgG", 0, T_D, NULL, NULL, T_D, NULL, T_LD, NULL, NULL, NULL, "-wp0 +#'" },
1245 { "eEaA", 0, T_D, NULL, NULL, T_D, NULL, T_LD, NULL, NULL, NULL, "-wp0 +#" },
1246 { "c", 0, T_I, NULL, NULL, T_WI, NULL, NULL, NULL, NULL, NULL, "-w" },
1247 { "C", 0, T_WI, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-w" },
1248 { "s", 1, T_C, NULL, NULL, T_W, NULL, NULL, NULL, NULL, NULL, "-wpc" },
1249 { "S", 1, T_W, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-wp" },
1250 { "p", 1, T_V, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-wc" },
1251 { "n", 1, T_I, T_SC, T_S, T_L, T_LL, NULL, T_SST, T_PD, T_IM, "" },
1252 { NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
1255 static format_char_info scan_char_table[] = {
1256 { "di", 1, T_I, T_SC, T_S, T_L, T_LL, T_LL, T_SST, T_PD, T_IM, "*" },
1257 { "ouxX", 1, T_UI, T_UC, T_US, T_UL, T_ULL, T_ULL, T_ST, T_UPD, T_UIM, "*" },
1258 { "efFgEGaA", 1, T_F, NULL, NULL, T_D, NULL, T_LD, NULL, NULL, NULL, "*" },
1259 { "c", 1, T_C, NULL, NULL, T_W, NULL, NULL, NULL, NULL, NULL, "*c" },
1260 { "s", 1, T_C, NULL, NULL, T_W, NULL, NULL, NULL, NULL, NULL, "*ac" },
1261 { "[", 1, T_C, NULL, NULL, T_W, NULL, NULL, NULL, NULL, NULL, "*ac" },
1262 { "C", 1, T_W, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "*" },
1263 { "S", 1, T_W, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "*a" },
1264 { "p", 2, T_V, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "*" },
1265 { "n", 1, T_I, T_SC, T_S, T_L, T_LL, NULL, T_SST, T_PD, T_IM, "" },
1266 { NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
1269 /* Handle format characters recognized by glibc's strftime.c.
1270 '2' - MUST do years as only two digits
1271 '3' - MAY do years as only two digits (depending on locale)
1272 'E' - E modifier is acceptable
1273 'O' - O modifier is acceptable to Standard C
1274 'o' - O modifier is acceptable as a GNU extension
1275 '9' - added to the C standard in C99
1276 'G' - other GNU extensions */
1278 static format_char_info time_char_table[] = {
1279 { "y", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "2EO-_0w" },
1280 { "D", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "29" },
1281 { "g", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "2Oo-_0w9" },
1282 { "cx", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "3E" },
1283 { "%", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "" },
1284 { "X", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "E" },
1285 { "RTnrt", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "9" },
1286 { "P", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "G" },
1287 { "HIMSUWdmw", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0Ow" },
1288 { "e", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0Ow9" },
1289 { "j", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0Oow" },
1290 { "Vu", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0Ow9" },
1291 { "Gz", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0Oow9" },
1292 { "kls", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0OGw" },
1293 { "ABZa", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "^#" },
1294 { "p", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "#" },
1295 { "b", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "^" },
1296 { "h", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "^9" },
1297 { "Y", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0EOow" },
1298 { "F", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "w9" },
1299 { "C", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0EOow9" },
1300 { NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
1303 typedef struct function_format_info
1305 struct function_format_info *next; /* next structure on the list */
1306 tree name; /* identifier such as "printf" */
1307 tree assembler_name; /* optional mangled identifier (for C++) */
1308 enum format_type format_type; /* type of format (printf, scanf, etc.) */
1309 int format_num; /* number of format argument */
1310 int first_arg_num; /* number of first arg (zero for varargs) */
1311 } function_format_info;
1313 static function_format_info *function_format_list = NULL;
1315 typedef struct international_format_info
1317 struct international_format_info *next; /* next structure on the list */
1318 tree name; /* identifier such as "gettext" */
1319 tree assembler_name; /* optional mangled identifier (for C++) */
1320 int format_num; /* number of format argument */
1321 } international_format_info;
1323 static international_format_info *international_format_list = NULL;
1325 static void check_format_info PARAMS ((function_format_info *, tree));
1327 /* Initialize the table of functions to perform format checking on.
1328 The ANSI functions are always checked (whether <stdio.h> is
1329 included or not), since it is common to call printf without
1330 including <stdio.h>. There shouldn't be a problem with this,
1331 since ANSI reserves these function names whether you include the
1332 header file or not. In any case, the checking is harmless.
1334 Also initialize the name of function that modify the format string for
1335 internationalization purposes. */
1337 void
1338 init_function_format_info ()
1340 record_function_format (get_identifier ("printf"), NULL_TREE,
1341 printf_format_type, 1, 2);
1342 record_function_format (get_identifier ("fprintf"), NULL_TREE,
1343 printf_format_type, 2, 3);
1344 record_function_format (get_identifier ("sprintf"), NULL_TREE,
1345 printf_format_type, 2, 3);
1346 record_function_format (get_identifier ("scanf"), NULL_TREE,
1347 scanf_format_type, 1, 2);
1348 record_function_format (get_identifier ("fscanf"), NULL_TREE,
1349 scanf_format_type, 2, 3);
1350 record_function_format (get_identifier ("sscanf"), NULL_TREE,
1351 scanf_format_type, 2, 3);
1352 record_function_format (get_identifier ("vprintf"), NULL_TREE,
1353 printf_format_type, 1, 0);
1354 record_function_format (get_identifier ("vfprintf"), NULL_TREE,
1355 printf_format_type, 2, 0);
1356 record_function_format (get_identifier ("vsprintf"), NULL_TREE,
1357 printf_format_type, 2, 0);
1358 record_function_format (get_identifier ("strftime"), NULL_TREE,
1359 strftime_format_type, 3, 0);
1361 if (flag_isoc99)
1363 /* ISO C99 adds the snprintf and vscanf family functions. */
1364 record_function_format (get_identifier ("snprintf"), NULL_TREE,
1365 printf_format_type, 3, 4);
1366 record_function_format (get_identifier ("vsnprintf"), NULL_TREE,
1367 printf_format_type, 3, 0);
1368 record_function_format (get_identifier ("vscanf"), NULL_TREE,
1369 scanf_format_type, 1, 0);
1370 record_function_format (get_identifier ("vfscanf"), NULL_TREE,
1371 scanf_format_type, 2, 0);
1372 record_function_format (get_identifier ("vsscanf"), NULL_TREE,
1373 scanf_format_type, 2, 0);
1376 record_international_format (get_identifier ("gettext"), NULL_TREE, 1);
1377 record_international_format (get_identifier ("dgettext"), NULL_TREE, 2);
1378 record_international_format (get_identifier ("dcgettext"), NULL_TREE, 2);
1381 /* Record information for argument format checking. FUNCTION_IDENT is
1382 the identifier node for the name of the function to check (its decl
1383 need not exist yet).
1384 FORMAT_TYPE specifies the type of format checking. FORMAT_NUM is the number
1385 of the argument which is the format control string (starting from 1).
1386 FIRST_ARG_NUM is the number of the first actual argument to check
1387 against the format string, or zero if no checking is not be done
1388 (e.g. for varargs such as vfprintf). */
1390 static void
1391 record_function_format (name, assembler_name, format_type,
1392 format_num, first_arg_num)
1393 tree name;
1394 tree assembler_name;
1395 enum format_type format_type;
1396 int format_num;
1397 int first_arg_num;
1399 function_format_info *info;
1401 /* Re-use existing structure if it's there. */
1403 for (info = function_format_list; info; info = info->next)
1405 if (info->name == name && info->assembler_name == assembler_name)
1406 break;
1408 if (! info)
1410 info = (function_format_info *) xmalloc (sizeof (function_format_info));
1411 info->next = function_format_list;
1412 function_format_list = info;
1414 info->name = name;
1415 info->assembler_name = assembler_name;
1418 info->format_type = format_type;
1419 info->format_num = format_num;
1420 info->first_arg_num = first_arg_num;
1423 /* Record information for the names of function that modify the format
1424 argument to format functions. FUNCTION_IDENT is the identifier node for
1425 the name of the function (its decl need not exist yet) and FORMAT_NUM is
1426 the number of the argument which is the format control string (starting
1427 from 1). */
1429 static void
1430 record_international_format (name, assembler_name, format_num)
1431 tree name;
1432 tree assembler_name;
1433 int format_num;
1435 international_format_info *info;
1437 /* Re-use existing structure if it's there. */
1439 for (info = international_format_list; info; info = info->next)
1441 if (info->name == name && info->assembler_name == assembler_name)
1442 break;
1445 if (! info)
1447 info
1448 = (international_format_info *)
1449 xmalloc (sizeof (international_format_info));
1450 info->next = international_format_list;
1451 international_format_list = info;
1453 info->name = name;
1454 info->assembler_name = assembler_name;
1457 info->format_num = format_num;
1460 static void
1461 tfaff ()
1463 warning ("too few arguments for format");
1466 /* Check the argument list of a call to printf, scanf, etc.
1467 NAME is the function identifier.
1468 ASSEMBLER_NAME is the function's assembler identifier.
1469 (Either NAME or ASSEMBLER_NAME, but not both, may be NULL_TREE.)
1470 PARAMS is the list of argument values. */
1472 void
1473 check_function_format (name, assembler_name, params)
1474 tree name;
1475 tree assembler_name;
1476 tree params;
1478 function_format_info *info;
1480 /* See if this function is a format function. */
1481 for (info = function_format_list; info; info = info->next)
1483 if (info->assembler_name
1484 ? (info->assembler_name == assembler_name)
1485 : (info->name == name))
1487 /* Yup; check it. */
1488 check_format_info (info, params);
1489 break;
1494 /* Check the argument list of a call to printf, scanf, etc.
1495 INFO points to the function_format_info structure.
1496 PARAMS is the list of argument values. */
1498 static void
1499 check_format_info (info, params)
1500 function_format_info *info;
1501 tree params;
1503 int i;
1504 int arg_num;
1505 int suppressed, wide, precise;
1506 int length_char = 0;
1507 int format_char;
1508 int format_length;
1509 tree format_tree;
1510 tree cur_param;
1511 tree cur_type;
1512 tree wanted_type;
1513 tree first_fillin_param;
1514 const char *format_chars;
1515 format_char_info *fci = NULL;
1516 char flag_chars[8];
1517 int has_operand_number = 0;
1519 /* Skip to format argument. If the argument isn't available, there's
1520 no work for us to do; prototype checking will catch the problem. */
1521 for (arg_num = 1; ; ++arg_num)
1523 if (params == 0)
1524 return;
1525 if (arg_num == info->format_num)
1526 break;
1527 params = TREE_CHAIN (params);
1529 format_tree = TREE_VALUE (params);
1530 params = TREE_CHAIN (params);
1531 if (format_tree == 0)
1532 return;
1534 /* We can only check the format if it's a string constant. */
1535 while (TREE_CODE (format_tree) == NOP_EXPR)
1536 format_tree = TREE_OPERAND (format_tree, 0); /* strip coercion */
1538 if (TREE_CODE (format_tree) == CALL_EXPR
1539 && TREE_CODE (TREE_OPERAND (format_tree, 0)) == ADDR_EXPR
1540 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (format_tree, 0), 0))
1541 == FUNCTION_DECL))
1543 tree function = TREE_OPERAND (TREE_OPERAND (format_tree, 0), 0);
1545 /* See if this is a call to a known internationalization function
1546 that modifies the format arg. */
1547 international_format_info *info;
1549 for (info = international_format_list; info; info = info->next)
1550 if (info->assembler_name
1551 ? (info->assembler_name == DECL_ASSEMBLER_NAME (function))
1552 : (info->name == DECL_NAME (function)))
1554 tree inner_args;
1555 int i;
1557 for (inner_args = TREE_OPERAND (format_tree, 1), i = 1;
1558 inner_args != 0;
1559 inner_args = TREE_CHAIN (inner_args), i++)
1560 if (i == info->format_num)
1562 format_tree = TREE_VALUE (inner_args);
1564 while (TREE_CODE (format_tree) == NOP_EXPR)
1565 format_tree = TREE_OPERAND (format_tree, 0);
1570 if (integer_zerop (format_tree))
1572 warning ("null format string");
1573 return;
1575 if (TREE_CODE (format_tree) != ADDR_EXPR)
1577 /* The user may get multiple warnings if the supplied argument
1578 isn't even a string pointer. */
1579 /* Functions taking a va_list normally pass a non-literal format
1580 string. These functions typically are declared with
1581 first_arg_num == 0, so avoid warning in those cases. */
1582 if (info->first_arg_num != 0 && warn_format > 1)
1583 warning ("format not a string literal, argument types not checked");
1584 return;
1586 format_tree = TREE_OPERAND (format_tree, 0);
1587 if (TREE_CODE (format_tree) != STRING_CST)
1589 /* The user may get multiple warnings if the supplied argument
1590 isn't even a string pointer. */
1591 /* Functions taking a va_list normally pass a non-literal format
1592 string. These functions typically are declared with
1593 first_arg_num == 0, so avoid warning in those cases. */
1594 if (info->first_arg_num != 0 && warn_format > 1)
1595 warning ("format not a string literal, argument types not checked");
1596 return;
1598 format_chars = TREE_STRING_POINTER (format_tree);
1599 format_length = TREE_STRING_LENGTH (format_tree);
1600 if (format_length <= 1)
1601 warning ("zero-length format string");
1602 if (format_chars[--format_length] != 0)
1604 warning ("unterminated format string");
1605 return;
1607 /* Skip to first argument to check. */
1608 while (arg_num + 1 < info->first_arg_num)
1610 if (params == 0)
1611 return;
1612 params = TREE_CHAIN (params);
1613 ++arg_num;
1616 first_fillin_param = params;
1617 while (1)
1619 int aflag;
1620 int char_type_flag = 0;
1621 if (*format_chars == 0)
1623 if (format_chars - TREE_STRING_POINTER (format_tree) != format_length)
1624 warning ("embedded `\\0' in format");
1625 if (info->first_arg_num != 0 && params != 0 && ! has_operand_number)
1626 warning ("too many arguments for format");
1627 return;
1629 if (*format_chars++ != '%')
1630 continue;
1631 if (*format_chars == 0)
1633 warning ("spurious trailing `%%' in format");
1634 continue;
1636 if (*format_chars == '%')
1638 ++format_chars;
1639 continue;
1641 flag_chars[0] = 0;
1642 suppressed = wide = precise = FALSE;
1643 if (info->format_type == scanf_format_type)
1645 suppressed = *format_chars == '*';
1646 if (suppressed)
1647 ++format_chars;
1648 while (ISDIGIT (*format_chars))
1649 ++format_chars;
1651 else if (info->format_type == strftime_format_type)
1653 while (*format_chars != 0 && index ("_-0^#", *format_chars) != 0)
1655 if (pedantic)
1656 warning ("ISO C does not support the strftime `%c' flag",
1657 *format_chars);
1658 if (index (flag_chars, *format_chars) != 0)
1660 warning ("repeated `%c' flag in format",
1661 *format_chars);
1662 ++format_chars;
1664 else
1666 i = strlen (flag_chars);
1667 flag_chars[i++] = *format_chars++;
1668 flag_chars[i] = 0;
1671 while (ISDIGIT ((unsigned char) *format_chars))
1673 wide = TRUE;
1674 ++format_chars;
1676 if (wide && pedantic)
1677 warning ("ISO C does not support strftime format width");
1678 if (*format_chars == 'E' || *format_chars == 'O')
1680 i = strlen (flag_chars);
1681 flag_chars[i++] = *format_chars++;
1682 flag_chars[i] = 0;
1683 if (*format_chars == 'E' || *format_chars == 'O')
1685 warning ("multiple E/O modifiers in format");
1686 while (*format_chars == 'E' || *format_chars == 'O')
1687 ++format_chars;
1691 else if (info->format_type == printf_format_type)
1693 /* See if we have a number followed by a dollar sign. If we do,
1694 it is an operand number, so set PARAMS to that operand. */
1695 if (*format_chars >= '0' && *format_chars <= '9')
1697 const char *p = format_chars;
1699 while (*p >= '0' && *p++ <= '9')
1702 if (*p == '$')
1704 int opnum = atoi (format_chars);
1706 if (pedantic)
1707 warning ("ISO C does not support printf %%n$");
1709 params = first_fillin_param;
1710 format_chars = p + 1;
1711 has_operand_number = 1;
1713 for (i = 1; i < opnum && params != 0; i++)
1714 params = TREE_CHAIN (params);
1716 if (opnum == 0 || params == 0)
1718 warning ("operand number out of range in format");
1719 return;
1724 while (*format_chars != 0 && index (" +#0-'", *format_chars) != 0)
1726 if (index (flag_chars, *format_chars) != 0)
1727 warning ("repeated `%c' flag in format", *format_chars++);
1728 else
1730 i = strlen (flag_chars);
1731 flag_chars[i++] = *format_chars++;
1732 flag_chars[i] = 0;
1735 /* "If the space and + flags both appear,
1736 the space flag will be ignored." */
1737 if (index (flag_chars, ' ') != 0
1738 && index (flag_chars, '+') != 0)
1739 warning ("use of both ` ' and `+' flags in format");
1740 /* "If the 0 and - flags both appear,
1741 the 0 flag will be ignored." */
1742 if (index (flag_chars, '0') != 0
1743 && index (flag_chars, '-') != 0)
1744 warning ("use of both `0' and `-' flags in format");
1745 if (index (flag_chars, '\'') && pedantic)
1746 warning ("ISO C does not support the `'' format flag");
1747 if (*format_chars == '*')
1749 wide = TRUE;
1750 /* "...a field width...may be indicated by an asterisk.
1751 In this case, an int argument supplies the field width..." */
1752 ++format_chars;
1753 if (params == 0)
1755 tfaff ();
1756 return;
1758 if (info->first_arg_num != 0)
1760 cur_param = TREE_VALUE (params);
1761 params = TREE_CHAIN (params);
1762 ++arg_num;
1763 /* size_t is generally not valid here.
1764 It will work on most machines, because size_t and int
1765 have the same mode. But might as well warn anyway,
1766 since it will fail on other machines. */
1767 if ((TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1768 != integer_type_node)
1770 (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1771 != unsigned_type_node))
1772 warning ("field width is not type int (arg %d)", arg_num);
1775 else
1777 while (ISDIGIT (*format_chars))
1779 wide = TRUE;
1780 ++format_chars;
1783 if (*format_chars == '.')
1785 precise = TRUE;
1786 ++format_chars;
1787 /* "...a...precision...may be indicated by an asterisk.
1788 In this case, an int argument supplies the...precision." */
1789 if (*format_chars == '*')
1791 if (info->first_arg_num != 0)
1793 ++format_chars;
1794 if (params == 0)
1796 tfaff ();
1797 return;
1799 cur_param = TREE_VALUE (params);
1800 params = TREE_CHAIN (params);
1801 ++arg_num;
1802 if ((TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1803 != integer_type_node)
1805 (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1806 != unsigned_type_node))
1807 warning ("field precision is not type int (arg %d)",
1808 arg_num);
1811 else
1813 while (ISDIGIT (*format_chars))
1814 ++format_chars;
1819 aflag = 0;
1821 if (info->format_type != strftime_format_type)
1823 if (*format_chars == 'h' || *format_chars == 'l')
1824 length_char = *format_chars++;
1825 else if (*format_chars == 'q' || *format_chars == 'L')
1827 length_char = *format_chars++;
1828 if (length_char == 'q' && pedantic)
1829 warning ("ISO C does not support the `%c' length modifier",
1830 length_char);
1832 else if (*format_chars == 'Z' || *format_chars == 'z')
1834 length_char = *format_chars++;
1835 if (pedantic)
1837 if (length_char == 'Z')
1838 warning ("ISO C does not support the `%c' length modifier",
1839 length_char);
1840 else if (!flag_isoc99)
1841 warning ("ISO C89 does not support the `%c' length modifier",
1842 length_char);
1845 else if (*format_chars == 't' || *format_chars == 'j')
1847 length_char = *format_chars++;
1848 if (pedantic && !flag_isoc99)
1849 warning ("ISO C89 does not support the `%c' length modifier",
1850 length_char);
1852 else
1853 length_char = 0;
1854 if (length_char == 'l' && *format_chars == 'l')
1856 length_char = 'q', format_chars++;
1857 if (pedantic && !flag_isoc99)
1858 warning ("ISO C89 does not support the `ll' length modifier");
1860 else if (length_char == 'h' && *format_chars == 'h')
1862 length_char = 'H', format_chars++;
1863 if (pedantic && !flag_isoc99)
1864 warning ("ISO C89 does not support the `hh' length modifier");
1866 if (*format_chars == 'a' && info->format_type == scanf_format_type)
1868 if (format_chars[1] == 's' || format_chars[1] == 'S'
1869 || format_chars[1] == '[')
1871 /* `a' is used as a flag. */
1872 aflag = 1;
1873 format_chars++;
1876 if (suppressed && length_char != 0)
1877 warning ("use of `*' and `%c' together in format", length_char);
1879 format_char = *format_chars;
1880 if (format_char == 0
1881 || (info->format_type != strftime_format_type && format_char == '%'))
1883 warning ("conversion lacks type at end of format");
1884 continue;
1886 /* The m, C, and S formats are GNU extensions. */
1887 if (pedantic && info->format_type != strftime_format_type
1888 && (format_char == 'm' || format_char == 'C' || format_char == 'S'))
1889 warning ("ISO C does not support the `%c' format", format_char);
1890 /* The a, A and F formats are C99 extensions. */
1891 if (pedantic && info->format_type != strftime_format_type
1892 && (format_char == 'a' || format_char == 'A' || format_char == 'F')
1893 && !flag_isoc99)
1894 warning ("ISO C89 does not support the `%c' format", format_char);
1895 format_chars++;
1896 switch (info->format_type)
1898 case printf_format_type:
1899 fci = print_char_table;
1900 break;
1901 case scanf_format_type:
1902 fci = scan_char_table;
1903 break;
1904 case strftime_format_type:
1905 fci = time_char_table;
1906 break;
1907 default:
1908 abort ();
1910 while (fci->format_chars != 0
1911 && index (fci->format_chars, format_char) == 0)
1912 ++fci;
1913 if (fci->format_chars == 0)
1915 if (ISGRAPH(format_char))
1916 warning ("unknown conversion type character `%c' in format",
1917 format_char);
1918 else
1919 warning ("unknown conversion type character 0x%x in format",
1920 format_char);
1921 continue;
1923 if (pedantic)
1925 if (index (fci->flag_chars, 'G') != 0)
1926 warning ("ISO C does not support `%%%c'", format_char);
1927 if (index (fci->flag_chars, '9') != 0 && !flag_isoc99)
1928 warning ("ISO C89 does not support `%%%c'", format_char);
1929 if (index (flag_chars, 'O') != 0)
1931 if (index (fci->flag_chars, 'o') != 0)
1932 warning ("ISO C does not support `%%O%c'", format_char);
1933 else if (!flag_isoc99 && index (fci->flag_chars, 'O') != 0)
1934 warning ("ISO C89 does not support `%%O%c'", format_char);
1936 if (!flag_isoc99 && index (flag_chars, 'E'))
1937 warning ("ISO C89 does not support `%%E%c'", format_char);
1939 if (wide && index (fci->flag_chars, 'w') == 0)
1940 warning ("width used with `%c' format", format_char);
1941 if (index (fci->flag_chars, '2') != 0)
1942 warning ("`%%%c' yields only last 2 digits of year", format_char);
1943 else if (index (fci->flag_chars, '3') != 0)
1944 warning ("`%%%c' yields only last 2 digits of year in some locales",
1945 format_char);
1946 if (precise && index (fci->flag_chars, 'p') == 0)
1947 warning ("precision used with `%c' format", format_char);
1948 if (aflag && index (fci->flag_chars, 'a') == 0)
1950 warning ("`a' flag used with `%c' format", format_char);
1951 /* To simplify the following code. */
1952 aflag = 0;
1954 /* The a flag is a GNU extension. */
1955 else if (pedantic && aflag)
1956 warning ("ISO C does not support the `a' flag");
1957 if (info->format_type == scanf_format_type && format_char == '[')
1959 /* Skip over scan set, in case it happens to have '%' in it. */
1960 if (*format_chars == '^')
1961 ++format_chars;
1962 /* Find closing bracket; if one is hit immediately, then
1963 it's part of the scan set rather than a terminator. */
1964 if (*format_chars == ']')
1965 ++format_chars;
1966 while (*format_chars && *format_chars != ']')
1967 ++format_chars;
1968 if (*format_chars != ']')
1969 /* The end of the format string was reached. */
1970 warning ("no closing `]' for `%%[' format");
1972 if (suppressed)
1974 if (index (fci->flag_chars, '*') == 0)
1975 warning ("suppression of `%c' conversion in format", format_char);
1976 continue;
1978 for (i = 0; flag_chars[i] != 0; ++i)
1980 if (index (fci->flag_chars, flag_chars[i]) == 0)
1981 warning ("flag `%c' used with type `%c'",
1982 flag_chars[i], format_char);
1984 if (info->format_type == strftime_format_type)
1985 continue;
1986 if (precise && index (flag_chars, '0') != 0
1987 && (format_char == 'd' || format_char == 'i'
1988 || format_char == 'o' || format_char == 'u'
1989 || format_char == 'x' || format_char == 'X'))
1990 warning ("`0' flag ignored with precision specifier and `%c' format",
1991 format_char);
1992 switch (length_char)
1994 default: wanted_type = fci->nolen ? *(fci->nolen) : 0; break;
1995 case 'H': wanted_type = fci->hhlen ? *(fci->hhlen) : 0; break;
1996 case 'h': wanted_type = fci->hlen ? *(fci->hlen) : 0; break;
1997 case 'l': wanted_type = fci->llen ? *(fci->llen) : 0; break;
1998 case 'q': wanted_type = fci->qlen ? *(fci->qlen) : 0; break;
1999 case 'L': wanted_type = fci->bigllen ? *(fci->bigllen) : 0; break;
2000 case 'z': case 'Z': wanted_type = (fci->zlen
2001 ? (TYPE_DOMAIN (*fci->zlen)
2002 ? TYPE_DOMAIN (*fci->zlen)
2003 : *fci->zlen)
2004 : 0); break;
2005 case 't': wanted_type = fci->tlen ? *(fci->tlen) : 0; break;
2006 case 'j': wanted_type = fci->jlen ? *(fci->jlen) : 0; break;
2008 if (wanted_type == 0)
2009 warning ("use of `%c' length character with `%c' type character",
2010 length_char, format_char);
2011 else if (length_char == 'L' && pedantic
2012 && !(format_char == 'a' || format_char == 'A'
2013 || format_char == 'e' || format_char == 'E'
2014 || format_char == 'f' || format_char == 'F'
2015 || format_char == 'g' || format_char == 'G'))
2016 warning ("ISO C does not support the `L' length modifier with the `%c' type character",
2017 format_char);
2018 else if (length_char == 'l'
2019 && (format_char == 'c' || format_char == 's'
2020 || format_char == '[')
2021 && pedantic && !flag_isoc94)
2022 warning ("ISO C89 does not support the `l' length modifier with the `%c' type character",
2023 format_char);
2024 else if (info->format_type == printf_format_type && pedantic
2025 && !flag_isoc99 && length_char == 'l'
2026 && (format_char == 'f' || format_char == 'e'
2027 || format_char == 'E' || format_char == 'g'
2028 || format_char == 'G'))
2029 warning ("ISO C89 does not support the `l' length modifier with the `%c' type character",
2030 format_char);
2032 /* Finally. . .check type of argument against desired type! */
2033 if (info->first_arg_num == 0)
2034 continue;
2035 if (fci->pointer_count == 0 && wanted_type == void_type_node)
2036 /* This specifier takes no argument. */
2037 continue;
2038 if (params == 0)
2040 tfaff ();
2041 return;
2043 cur_param = TREE_VALUE (params);
2044 params = TREE_CHAIN (params);
2045 ++arg_num;
2046 cur_type = TREE_TYPE (cur_param);
2048 STRIP_NOPS (cur_param);
2050 /* Check the types of any additional pointer arguments
2051 that precede the "real" argument. */
2052 for (i = 0; i < fci->pointer_count + aflag; ++i)
2054 if (TREE_CODE (cur_type) == POINTER_TYPE)
2056 cur_type = TREE_TYPE (cur_type);
2058 if (cur_param != 0 && TREE_CODE (cur_param) == ADDR_EXPR)
2059 cur_param = TREE_OPERAND (cur_param, 0);
2060 else
2061 cur_param = 0;
2063 continue;
2065 if (TREE_CODE (cur_type) != ERROR_MARK)
2067 if (fci->pointer_count + aflag == 1)
2068 warning ("format argument is not a pointer (arg %d)", arg_num);
2069 else
2070 warning ("format argument is not a pointer to a pointer (arg %d)", arg_num);
2072 break;
2075 /* See if this is an attempt to write into a const type with
2076 scanf or with printf "%n". */
2077 if ((info->format_type == scanf_format_type
2078 || (info->format_type == printf_format_type
2079 && format_char == 'n'))
2080 && i == fci->pointer_count + aflag
2081 && wanted_type != 0
2082 && TREE_CODE (cur_type) != ERROR_MARK
2083 && (TYPE_READONLY (cur_type)
2084 || (cur_param != 0
2085 && (TREE_CODE_CLASS (TREE_CODE (cur_param)) == 'c'
2086 || (DECL_P (cur_param) && TREE_READONLY (cur_param))))))
2087 warning ("writing into constant object (arg %d)", arg_num);
2089 /* Check whether the argument type is a character type. This leniency
2090 only applies to certain formats, flagged with 'c'.
2092 if (TREE_CODE (cur_type) != ERROR_MARK && index (fci->flag_chars, 'c') != 0)
2093 char_type_flag = (TYPE_MAIN_VARIANT (cur_type) == char_type_node
2094 || TYPE_MAIN_VARIANT (cur_type) == signed_char_type_node
2095 || TYPE_MAIN_VARIANT (cur_type) == unsigned_char_type_node);
2097 /* Check the type of the "real" argument, if there's a type we want. */
2098 if (i == fci->pointer_count + aflag && wanted_type != 0
2099 && TREE_CODE (cur_type) != ERROR_MARK
2100 && wanted_type != TYPE_MAIN_VARIANT (cur_type)
2101 /* If we want `void *', allow any pointer type.
2102 (Anything else would already have got a warning.)
2103 With -pedantic, only allow pointers to void and to character
2104 types.
2106 && ! (wanted_type == void_type_node
2107 && fci->pointer_count > 0
2108 && (! pedantic
2109 || TYPE_MAIN_VARIANT (cur_type) == void_type_node
2110 || (i == 1 && char_type_flag)))
2111 /* Don't warn about differences merely in signedness, unless
2112 -pedantic. With -pedantic, warn if the type is a pointer
2113 target and not a character type, and for character types at
2114 a second level of indirection.
2116 && !(TREE_CODE (wanted_type) == INTEGER_TYPE
2117 && TREE_CODE (TYPE_MAIN_VARIANT (cur_type)) == INTEGER_TYPE
2118 && (! pedantic || i == 0 || (i == 1 && char_type_flag))
2119 && (TREE_UNSIGNED (wanted_type)
2120 ? wanted_type == (cur_type = unsigned_type (cur_type))
2121 : wanted_type == (cur_type = signed_type (cur_type))))
2122 /* Likewise, "signed char", "unsigned char" and "char" are
2123 equivalent but the above test won't consider them equivalent. */
2124 && ! (wanted_type == char_type_node
2125 && (! pedantic || i < 2)
2126 && char_type_flag))
2128 register const char *this;
2129 register const char *that;
2131 this = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (wanted_type)));
2132 that = 0;
2133 if (TREE_CODE (cur_type) != ERROR_MARK
2134 && TYPE_NAME (cur_type) != 0
2135 && TREE_CODE (cur_type) != INTEGER_TYPE
2136 && !(TREE_CODE (cur_type) == POINTER_TYPE
2137 && TREE_CODE (TREE_TYPE (cur_type)) == INTEGER_TYPE))
2139 if (TREE_CODE (TYPE_NAME (cur_type)) == TYPE_DECL
2140 && DECL_NAME (TYPE_NAME (cur_type)) != 0)
2141 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type)));
2142 else
2143 that = IDENTIFIER_POINTER (TYPE_NAME (cur_type));
2146 /* A nameless type can't possibly match what the format wants.
2147 So there will be a warning for it.
2148 Make up a string to describe vaguely what it is. */
2149 if (that == 0)
2151 if (TREE_CODE (cur_type) == POINTER_TYPE)
2152 that = "pointer";
2153 else
2154 that = "different type";
2157 /* Make the warning better in case of mismatch of int vs long. */
2158 if (TREE_CODE (cur_type) == INTEGER_TYPE
2159 && TREE_CODE (wanted_type) == INTEGER_TYPE
2160 && TYPE_PRECISION (cur_type) == TYPE_PRECISION (wanted_type)
2161 && TYPE_NAME (cur_type) != 0
2162 && TREE_CODE (TYPE_NAME (cur_type)) == TYPE_DECL)
2163 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type)));
2165 if (strcmp (this, that) != 0)
2166 warning ("%s format, %s arg (arg %d)", this, that, arg_num);
2171 /* Print a warning if a constant expression had overflow in folding.
2172 Invoke this function on every expression that the language
2173 requires to be a constant expression.
2174 Note the ANSI C standard says it is erroneous for a
2175 constant expression to overflow. */
2177 void
2178 constant_expression_warning (value)
2179 tree value;
2181 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
2182 || TREE_CODE (value) == COMPLEX_CST)
2183 && TREE_CONSTANT_OVERFLOW (value) && pedantic)
2184 pedwarn ("overflow in constant expression");
2187 /* Print a warning if an expression had overflow in folding.
2188 Invoke this function on every expression that
2189 (1) appears in the source code, and
2190 (2) might be a constant expression that overflowed, and
2191 (3) is not already checked by convert_and_check;
2192 however, do not invoke this function on operands of explicit casts. */
2194 void
2195 overflow_warning (value)
2196 tree value;
2198 if ((TREE_CODE (value) == INTEGER_CST
2199 || (TREE_CODE (value) == COMPLEX_CST
2200 && TREE_CODE (TREE_REALPART (value)) == INTEGER_CST))
2201 && TREE_OVERFLOW (value))
2203 TREE_OVERFLOW (value) = 0;
2204 if (skip_evaluation == 0)
2205 warning ("integer overflow in expression");
2207 else if ((TREE_CODE (value) == REAL_CST
2208 || (TREE_CODE (value) == COMPLEX_CST
2209 && TREE_CODE (TREE_REALPART (value)) == REAL_CST))
2210 && TREE_OVERFLOW (value))
2212 TREE_OVERFLOW (value) = 0;
2213 if (skip_evaluation == 0)
2214 warning ("floating point overflow in expression");
2218 /* Print a warning if a large constant is truncated to unsigned,
2219 or if -Wconversion is used and a constant < 0 is converted to unsigned.
2220 Invoke this function on every expression that might be implicitly
2221 converted to an unsigned type. */
2223 void
2224 unsigned_conversion_warning (result, operand)
2225 tree result, operand;
2227 if (TREE_CODE (operand) == INTEGER_CST
2228 && TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
2229 && TREE_UNSIGNED (TREE_TYPE (result))
2230 && skip_evaluation == 0
2231 && !int_fits_type_p (operand, TREE_TYPE (result)))
2233 if (!int_fits_type_p (operand, signed_type (TREE_TYPE (result))))
2234 /* This detects cases like converting -129 or 256 to unsigned char. */
2235 warning ("large integer implicitly truncated to unsigned type");
2236 else if (warn_conversion)
2237 warning ("negative integer implicitly converted to unsigned type");
2241 /* Convert EXPR to TYPE, warning about conversion problems with constants.
2242 Invoke this function on every expression that is converted implicitly,
2243 i.e. because of language rules and not because of an explicit cast. */
2245 tree
2246 convert_and_check (type, expr)
2247 tree type, expr;
2249 tree t = convert (type, expr);
2250 if (TREE_CODE (t) == INTEGER_CST)
2252 if (TREE_OVERFLOW (t))
2254 TREE_OVERFLOW (t) = 0;
2256 /* Do not diagnose overflow in a constant expression merely
2257 because a conversion overflowed. */
2258 TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (expr);
2260 /* No warning for converting 0x80000000 to int. */
2261 if (!(TREE_UNSIGNED (type) < TREE_UNSIGNED (TREE_TYPE (expr))
2262 && TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
2263 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (expr))))
2264 /* If EXPR fits in the unsigned version of TYPE,
2265 don't warn unless pedantic. */
2266 if ((pedantic
2267 || TREE_UNSIGNED (type)
2268 || ! int_fits_type_p (expr, unsigned_type (type)))
2269 && skip_evaluation == 0)
2270 warning ("overflow in implicit constant conversion");
2272 else
2273 unsigned_conversion_warning (t, expr);
2275 return t;
2278 void
2279 c_expand_expr_stmt (expr)
2280 tree expr;
2282 /* Do default conversion if safe and possibly important,
2283 in case within ({...}). */
2284 if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE && lvalue_p (expr))
2285 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
2286 expr = default_conversion (expr);
2288 if (TREE_TYPE (expr) != error_mark_node
2289 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
2290 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
2291 error ("expression statement has incomplete type");
2293 expand_expr_stmt (expr);
2296 /* Validate the expression after `case' and apply default promotions. */
2298 tree
2299 check_case_value (value)
2300 tree value;
2302 if (value == NULL_TREE)
2303 return value;
2305 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
2306 STRIP_TYPE_NOPS (value);
2308 if (TREE_CODE (value) != INTEGER_CST
2309 && value != error_mark_node)
2311 error ("case label does not reduce to an integer constant");
2312 value = error_mark_node;
2314 else
2315 /* Promote char or short to int. */
2316 value = default_conversion (value);
2318 constant_expression_warning (value);
2320 return value;
2323 /* Return an integer type with BITS bits of precision,
2324 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
2326 tree
2327 type_for_size (bits, unsignedp)
2328 unsigned bits;
2329 int unsignedp;
2331 if (bits == TYPE_PRECISION (integer_type_node))
2332 return unsignedp ? unsigned_type_node : integer_type_node;
2334 if (bits == TYPE_PRECISION (signed_char_type_node))
2335 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2337 if (bits == TYPE_PRECISION (short_integer_type_node))
2338 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2340 if (bits == TYPE_PRECISION (long_integer_type_node))
2341 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2343 if (bits == TYPE_PRECISION (long_long_integer_type_node))
2344 return (unsignedp ? long_long_unsigned_type_node
2345 : long_long_integer_type_node);
2347 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
2348 return (unsignedp ? widest_unsigned_literal_type_node
2349 : widest_integer_literal_type_node);
2351 if (bits <= TYPE_PRECISION (intQI_type_node))
2352 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2354 if (bits <= TYPE_PRECISION (intHI_type_node))
2355 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2357 if (bits <= TYPE_PRECISION (intSI_type_node))
2358 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2360 if (bits <= TYPE_PRECISION (intDI_type_node))
2361 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2363 return 0;
2366 /* Return a data type that has machine mode MODE.
2367 If the mode is an integer,
2368 then UNSIGNEDP selects between signed and unsigned types. */
2370 tree
2371 type_for_mode (mode, unsignedp)
2372 enum machine_mode mode;
2373 int unsignedp;
2375 if (mode == TYPE_MODE (integer_type_node))
2376 return unsignedp ? unsigned_type_node : integer_type_node;
2378 if (mode == TYPE_MODE (signed_char_type_node))
2379 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2381 if (mode == TYPE_MODE (short_integer_type_node))
2382 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2384 if (mode == TYPE_MODE (long_integer_type_node))
2385 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2387 if (mode == TYPE_MODE (long_long_integer_type_node))
2388 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
2390 if (mode == TYPE_MODE (widest_integer_literal_type_node))
2391 return unsignedp ? widest_unsigned_literal_type_node
2392 : widest_integer_literal_type_node;
2394 if (mode == TYPE_MODE (intQI_type_node))
2395 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2397 if (mode == TYPE_MODE (intHI_type_node))
2398 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2400 if (mode == TYPE_MODE (intSI_type_node))
2401 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2403 if (mode == TYPE_MODE (intDI_type_node))
2404 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2406 #if HOST_BITS_PER_WIDE_INT >= 64
2407 if (mode == TYPE_MODE (intTI_type_node))
2408 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2409 #endif
2411 if (mode == TYPE_MODE (float_type_node))
2412 return float_type_node;
2414 if (mode == TYPE_MODE (double_type_node))
2415 return double_type_node;
2417 if (mode == TYPE_MODE (long_double_type_node))
2418 return long_double_type_node;
2420 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
2421 return build_pointer_type (char_type_node);
2423 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
2424 return build_pointer_type (integer_type_node);
2426 #ifdef VECTOR_MODE_SUPPORTED_P
2427 if (mode == TYPE_MODE (V4SF_type_node) && VECTOR_MODE_SUPPORTED_P (mode))
2428 return V4SF_type_node;
2429 if (mode == TYPE_MODE (V4SI_type_node) && VECTOR_MODE_SUPPORTED_P (mode))
2430 return V4SI_type_node;
2431 if (mode == TYPE_MODE (V2SI_type_node) && VECTOR_MODE_SUPPORTED_P (mode))
2432 return V2SI_type_node;
2433 if (mode == TYPE_MODE (V4HI_type_node) && VECTOR_MODE_SUPPORTED_P (mode))
2434 return V4HI_type_node;
2435 if (mode == TYPE_MODE (V8QI_type_node) && VECTOR_MODE_SUPPORTED_P (mode))
2436 return V8QI_type_node;
2437 #endif
2439 return 0;
2442 /* Return an unsigned type the same as TYPE in other respects. */
2443 tree
2444 unsigned_type (type)
2445 tree type;
2447 tree type1 = TYPE_MAIN_VARIANT (type);
2448 if (type1 == signed_char_type_node || type1 == char_type_node)
2449 return unsigned_char_type_node;
2450 if (type1 == integer_type_node)
2451 return unsigned_type_node;
2452 if (type1 == short_integer_type_node)
2453 return short_unsigned_type_node;
2454 if (type1 == long_integer_type_node)
2455 return long_unsigned_type_node;
2456 if (type1 == long_long_integer_type_node)
2457 return long_long_unsigned_type_node;
2458 if (type1 == widest_integer_literal_type_node)
2459 return widest_unsigned_literal_type_node;
2460 #if HOST_BITS_PER_WIDE_INT >= 64
2461 if (type1 == intTI_type_node)
2462 return unsigned_intTI_type_node;
2463 #endif
2464 if (type1 == intDI_type_node)
2465 return unsigned_intDI_type_node;
2466 if (type1 == intSI_type_node)
2467 return unsigned_intSI_type_node;
2468 if (type1 == intHI_type_node)
2469 return unsigned_intHI_type_node;
2470 if (type1 == intQI_type_node)
2471 return unsigned_intQI_type_node;
2473 return signed_or_unsigned_type (1, type);
2476 /* Return a signed type the same as TYPE in other respects. */
2478 tree
2479 signed_type (type)
2480 tree type;
2482 tree type1 = TYPE_MAIN_VARIANT (type);
2483 if (type1 == unsigned_char_type_node || type1 == char_type_node)
2484 return signed_char_type_node;
2485 if (type1 == unsigned_type_node)
2486 return integer_type_node;
2487 if (type1 == short_unsigned_type_node)
2488 return short_integer_type_node;
2489 if (type1 == long_unsigned_type_node)
2490 return long_integer_type_node;
2491 if (type1 == long_long_unsigned_type_node)
2492 return long_long_integer_type_node;
2493 if (type1 == widest_unsigned_literal_type_node)
2494 return widest_integer_literal_type_node;
2495 #if HOST_BITS_PER_WIDE_INT >= 64
2496 if (type1 == unsigned_intTI_type_node)
2497 return intTI_type_node;
2498 #endif
2499 if (type1 == unsigned_intDI_type_node)
2500 return intDI_type_node;
2501 if (type1 == unsigned_intSI_type_node)
2502 return intSI_type_node;
2503 if (type1 == unsigned_intHI_type_node)
2504 return intHI_type_node;
2505 if (type1 == unsigned_intQI_type_node)
2506 return intQI_type_node;
2508 return signed_or_unsigned_type (0, type);
2511 /* Return a type the same as TYPE except unsigned or
2512 signed according to UNSIGNEDP. */
2514 tree
2515 signed_or_unsigned_type (unsignedp, type)
2516 int unsignedp;
2517 tree type;
2519 if (! INTEGRAL_TYPE_P (type)
2520 || TREE_UNSIGNED (type) == unsignedp)
2521 return type;
2523 if (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node))
2524 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2525 if (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2526 return unsignedp ? unsigned_type_node : integer_type_node;
2527 if (TYPE_PRECISION (type) == TYPE_PRECISION (short_integer_type_node))
2528 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2529 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_integer_type_node))
2530 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2531 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_long_integer_type_node))
2532 return (unsignedp ? long_long_unsigned_type_node
2533 : long_long_integer_type_node);
2534 if (TYPE_PRECISION (type) == TYPE_PRECISION (widest_integer_literal_type_node))
2535 return (unsignedp ? widest_unsigned_literal_type_node
2536 : widest_integer_literal_type_node);
2537 return type;
2540 /* Return the minimum number of bits needed to represent VALUE in a
2541 signed or unsigned type, UNSIGNEDP says which. */
2543 unsigned int
2544 min_precision (value, unsignedp)
2545 tree value;
2546 int unsignedp;
2548 int log;
2550 /* If the value is negative, compute its negative minus 1. The latter
2551 adjustment is because the absolute value of the largest negative value
2552 is one larger than the largest positive value. This is equivalent to
2553 a bit-wise negation, so use that operation instead. */
2555 if (tree_int_cst_sgn (value) < 0)
2556 value = fold (build1 (BIT_NOT_EXPR, TREE_TYPE (value), value));
2558 /* Return the number of bits needed, taking into account the fact
2559 that we need one more bit for a signed than unsigned type. */
2561 if (integer_zerop (value))
2562 log = 0;
2563 else
2564 log = tree_floor_log2 (value);
2566 return log + 1 + ! unsignedp;
2569 /* Print an error message for invalid operands to arith operation CODE.
2570 NOP_EXPR is used as a special case (see truthvalue_conversion). */
2572 void
2573 binary_op_error (code)
2574 enum tree_code code;
2576 register const char *opname;
2578 switch (code)
2580 case NOP_EXPR:
2581 error ("invalid truth-value expression");
2582 return;
2584 case PLUS_EXPR:
2585 opname = "+"; break;
2586 case MINUS_EXPR:
2587 opname = "-"; break;
2588 case MULT_EXPR:
2589 opname = "*"; break;
2590 case MAX_EXPR:
2591 opname = "max"; break;
2592 case MIN_EXPR:
2593 opname = "min"; break;
2594 case EQ_EXPR:
2595 opname = "=="; break;
2596 case NE_EXPR:
2597 opname = "!="; break;
2598 case LE_EXPR:
2599 opname = "<="; break;
2600 case GE_EXPR:
2601 opname = ">="; break;
2602 case LT_EXPR:
2603 opname = "<"; break;
2604 case GT_EXPR:
2605 opname = ">"; break;
2606 case LSHIFT_EXPR:
2607 opname = "<<"; break;
2608 case RSHIFT_EXPR:
2609 opname = ">>"; break;
2610 case TRUNC_MOD_EXPR:
2611 case FLOOR_MOD_EXPR:
2612 opname = "%"; break;
2613 case TRUNC_DIV_EXPR:
2614 case FLOOR_DIV_EXPR:
2615 opname = "/"; break;
2616 case BIT_AND_EXPR:
2617 opname = "&"; break;
2618 case BIT_IOR_EXPR:
2619 opname = "|"; break;
2620 case TRUTH_ANDIF_EXPR:
2621 opname = "&&"; break;
2622 case TRUTH_ORIF_EXPR:
2623 opname = "||"; break;
2624 case BIT_XOR_EXPR:
2625 opname = "^"; break;
2626 case LROTATE_EXPR:
2627 case RROTATE_EXPR:
2628 opname = "rotate"; break;
2629 default:
2630 opname = "unknown"; break;
2632 error ("invalid operands to binary %s", opname);
2635 /* Subroutine of build_binary_op, used for comparison operations.
2636 See if the operands have both been converted from subword integer types
2637 and, if so, perhaps change them both back to their original type.
2638 This function is also responsible for converting the two operands
2639 to the proper common type for comparison.
2641 The arguments of this function are all pointers to local variables
2642 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
2643 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
2645 If this function returns nonzero, it means that the comparison has
2646 a constant value. What this function returns is an expression for
2647 that value. */
2649 tree
2650 shorten_compare (op0_ptr, op1_ptr, restype_ptr, rescode_ptr)
2651 tree *op0_ptr, *op1_ptr;
2652 tree *restype_ptr;
2653 enum tree_code *rescode_ptr;
2655 register tree type;
2656 tree op0 = *op0_ptr;
2657 tree op1 = *op1_ptr;
2658 int unsignedp0, unsignedp1;
2659 int real1, real2;
2660 tree primop0, primop1;
2661 enum tree_code code = *rescode_ptr;
2663 /* Throw away any conversions to wider types
2664 already present in the operands. */
2666 primop0 = get_narrower (op0, &unsignedp0);
2667 primop1 = get_narrower (op1, &unsignedp1);
2669 /* Handle the case that OP0 does not *contain* a conversion
2670 but it *requires* conversion to FINAL_TYPE. */
2672 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
2673 unsignedp0 = TREE_UNSIGNED (TREE_TYPE (op0));
2674 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
2675 unsignedp1 = TREE_UNSIGNED (TREE_TYPE (op1));
2677 /* If one of the operands must be floated, we cannot optimize. */
2678 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
2679 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
2681 /* If first arg is constant, swap the args (changing operation
2682 so value is preserved), for canonicalization. Don't do this if
2683 the second arg is 0. */
2685 if (TREE_CONSTANT (primop0)
2686 && ! integer_zerop (primop1) && ! real_zerop (primop1))
2688 register tree tem = primop0;
2689 register int temi = unsignedp0;
2690 primop0 = primop1;
2691 primop1 = tem;
2692 tem = op0;
2693 op0 = op1;
2694 op1 = tem;
2695 *op0_ptr = op0;
2696 *op1_ptr = op1;
2697 unsignedp0 = unsignedp1;
2698 unsignedp1 = temi;
2699 temi = real1;
2700 real1 = real2;
2701 real2 = temi;
2703 switch (code)
2705 case LT_EXPR:
2706 code = GT_EXPR;
2707 break;
2708 case GT_EXPR:
2709 code = LT_EXPR;
2710 break;
2711 case LE_EXPR:
2712 code = GE_EXPR;
2713 break;
2714 case GE_EXPR:
2715 code = LE_EXPR;
2716 break;
2717 default:
2718 break;
2720 *rescode_ptr = code;
2723 /* If comparing an integer against a constant more bits wide,
2724 maybe we can deduce a value of 1 or 0 independent of the data.
2725 Or else truncate the constant now
2726 rather than extend the variable at run time.
2728 This is only interesting if the constant is the wider arg.
2729 Also, it is not safe if the constant is unsigned and the
2730 variable arg is signed, since in this case the variable
2731 would be sign-extended and then regarded as unsigned.
2732 Our technique fails in this case because the lowest/highest
2733 possible unsigned results don't follow naturally from the
2734 lowest/highest possible values of the variable operand.
2735 For just EQ_EXPR and NE_EXPR there is another technique that
2736 could be used: see if the constant can be faithfully represented
2737 in the other operand's type, by truncating it and reextending it
2738 and see if that preserves the constant's value. */
2740 if (!real1 && !real2
2741 && TREE_CODE (primop1) == INTEGER_CST
2742 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
2744 int min_gt, max_gt, min_lt, max_lt;
2745 tree maxval, minval;
2746 /* 1 if comparison is nominally unsigned. */
2747 int unsignedp = TREE_UNSIGNED (*restype_ptr);
2748 tree val;
2750 type = signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0));
2752 /* If TYPE is an enumeration, then we need to get its min/max
2753 values from it's underlying integral type, not the enumerated
2754 type itself. */
2755 if (TREE_CODE (type) == ENUMERAL_TYPE)
2756 type = type_for_size (TYPE_PRECISION (type), unsignedp0);
2758 maxval = TYPE_MAX_VALUE (type);
2759 minval = TYPE_MIN_VALUE (type);
2761 if (unsignedp && !unsignedp0)
2762 *restype_ptr = signed_type (*restype_ptr);
2764 if (TREE_TYPE (primop1) != *restype_ptr)
2765 primop1 = convert (*restype_ptr, primop1);
2766 if (type != *restype_ptr)
2768 minval = convert (*restype_ptr, minval);
2769 maxval = convert (*restype_ptr, maxval);
2772 if (unsignedp && unsignedp0)
2774 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
2775 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
2776 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
2777 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
2779 else
2781 min_gt = INT_CST_LT (primop1, minval);
2782 max_gt = INT_CST_LT (primop1, maxval);
2783 min_lt = INT_CST_LT (minval, primop1);
2784 max_lt = INT_CST_LT (maxval, primop1);
2787 val = 0;
2788 /* This used to be a switch, but Genix compiler can't handle that. */
2789 if (code == NE_EXPR)
2791 if (max_lt || min_gt)
2792 val = boolean_true_node;
2794 else if (code == EQ_EXPR)
2796 if (max_lt || min_gt)
2797 val = boolean_false_node;
2799 else if (code == LT_EXPR)
2801 if (max_lt)
2802 val = boolean_true_node;
2803 if (!min_lt)
2804 val = boolean_false_node;
2806 else if (code == GT_EXPR)
2808 if (min_gt)
2809 val = boolean_true_node;
2810 if (!max_gt)
2811 val = boolean_false_node;
2813 else if (code == LE_EXPR)
2815 if (!max_gt)
2816 val = boolean_true_node;
2817 if (min_gt)
2818 val = boolean_false_node;
2820 else if (code == GE_EXPR)
2822 if (!min_lt)
2823 val = boolean_true_node;
2824 if (max_lt)
2825 val = boolean_false_node;
2828 /* If primop0 was sign-extended and unsigned comparison specd,
2829 we did a signed comparison above using the signed type bounds.
2830 But the comparison we output must be unsigned.
2832 Also, for inequalities, VAL is no good; but if the signed
2833 comparison had *any* fixed result, it follows that the
2834 unsigned comparison just tests the sign in reverse
2835 (positive values are LE, negative ones GE).
2836 So we can generate an unsigned comparison
2837 against an extreme value of the signed type. */
2839 if (unsignedp && !unsignedp0)
2841 if (val != 0)
2842 switch (code)
2844 case LT_EXPR:
2845 case GE_EXPR:
2846 primop1 = TYPE_MIN_VALUE (type);
2847 val = 0;
2848 break;
2850 case LE_EXPR:
2851 case GT_EXPR:
2852 primop1 = TYPE_MAX_VALUE (type);
2853 val = 0;
2854 break;
2856 default:
2857 break;
2859 type = unsigned_type (type);
2862 if (!max_gt && !unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
2864 /* This is the case of (char)x >?< 0x80, which people used to use
2865 expecting old C compilers to change the 0x80 into -0x80. */
2866 if (val == boolean_false_node)
2867 warning ("comparison is always false due to limited range of data type");
2868 if (val == boolean_true_node)
2869 warning ("comparison is always true due to limited range of data type");
2872 if (!min_lt && unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
2874 /* This is the case of (unsigned char)x >?< -1 or < 0. */
2875 if (val == boolean_false_node)
2876 warning ("comparison is always false due to limited range of data type");
2877 if (val == boolean_true_node)
2878 warning ("comparison is always true due to limited range of data type");
2881 if (val != 0)
2883 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2884 if (TREE_SIDE_EFFECTS (primop0))
2885 return build (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
2886 return val;
2889 /* Value is not predetermined, but do the comparison
2890 in the type of the operand that is not constant.
2891 TYPE is already properly set. */
2893 else if (real1 && real2
2894 && (TYPE_PRECISION (TREE_TYPE (primop0))
2895 == TYPE_PRECISION (TREE_TYPE (primop1))))
2896 type = TREE_TYPE (primop0);
2898 /* If args' natural types are both narrower than nominal type
2899 and both extend in the same manner, compare them
2900 in the type of the wider arg.
2901 Otherwise must actually extend both to the nominal
2902 common type lest different ways of extending
2903 alter the result.
2904 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
2906 else if (unsignedp0 == unsignedp1 && real1 == real2
2907 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
2908 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
2910 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
2911 type = signed_or_unsigned_type (unsignedp0
2912 || TREE_UNSIGNED (*restype_ptr),
2913 type);
2914 /* Make sure shorter operand is extended the right way
2915 to match the longer operand. */
2916 primop0 = convert (signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0)),
2917 primop0);
2918 primop1 = convert (signed_or_unsigned_type (unsignedp1, TREE_TYPE (primop1)),
2919 primop1);
2921 else
2923 /* Here we must do the comparison on the nominal type
2924 using the args exactly as we received them. */
2925 type = *restype_ptr;
2926 primop0 = op0;
2927 primop1 = op1;
2929 if (!real1 && !real2 && integer_zerop (primop1)
2930 && TREE_UNSIGNED (*restype_ptr))
2932 tree value = 0;
2933 switch (code)
2935 case GE_EXPR:
2936 /* All unsigned values are >= 0, so we warn if extra warnings
2937 are requested. However, if OP0 is a constant that is
2938 >= 0, the signedness of the comparison isn't an issue,
2939 so suppress the warning. */
2940 if (extra_warnings && !in_system_header
2941 && ! (TREE_CODE (primop0) == INTEGER_CST
2942 && ! TREE_OVERFLOW (convert (signed_type (type),
2943 primop0))))
2944 warning ("comparison of unsigned expression >= 0 is always true");
2945 value = boolean_true_node;
2946 break;
2948 case LT_EXPR:
2949 if (extra_warnings && !in_system_header
2950 && ! (TREE_CODE (primop0) == INTEGER_CST
2951 && ! TREE_OVERFLOW (convert (signed_type (type),
2952 primop0))))
2953 warning ("comparison of unsigned expression < 0 is always false");
2954 value = boolean_false_node;
2955 break;
2957 default:
2958 break;
2961 if (value != 0)
2963 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2964 if (TREE_SIDE_EFFECTS (primop0))
2965 return build (COMPOUND_EXPR, TREE_TYPE (value),
2966 primop0, value);
2967 return value;
2972 *op0_ptr = convert (type, primop0);
2973 *op1_ptr = convert (type, primop1);
2975 *restype_ptr = boolean_type_node;
2977 return 0;
2980 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
2981 or validate its data type for an `if' or `while' statement or ?..: exp.
2983 This preparation consists of taking the ordinary
2984 representation of an expression expr and producing a valid tree
2985 boolean expression describing whether expr is nonzero. We could
2986 simply always do build_binary_op (NE_EXPR, expr, boolean_false_node, 1),
2987 but we optimize comparisons, &&, ||, and !.
2989 The resulting type should always be `boolean_type_node'. */
2991 tree
2992 truthvalue_conversion (expr)
2993 tree expr;
2995 if (TREE_CODE (expr) == ERROR_MARK)
2996 return expr;
2998 #if 0 /* This appears to be wrong for C++. */
2999 /* These really should return error_mark_node after 2.4 is stable.
3000 But not all callers handle ERROR_MARK properly. */
3001 switch (TREE_CODE (TREE_TYPE (expr)))
3003 case RECORD_TYPE:
3004 error ("struct type value used where scalar is required");
3005 return boolean_false_node;
3007 case UNION_TYPE:
3008 error ("union type value used where scalar is required");
3009 return boolean_false_node;
3011 case ARRAY_TYPE:
3012 error ("array type value used where scalar is required");
3013 return boolean_false_node;
3015 default:
3016 break;
3018 #endif /* 0 */
3020 switch (TREE_CODE (expr))
3022 case EQ_EXPR:
3023 case NE_EXPR: case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
3024 case TRUTH_ANDIF_EXPR:
3025 case TRUTH_ORIF_EXPR:
3026 case TRUTH_AND_EXPR:
3027 case TRUTH_OR_EXPR:
3028 case TRUTH_XOR_EXPR:
3029 case TRUTH_NOT_EXPR:
3030 TREE_TYPE (expr) = boolean_type_node;
3031 return expr;
3033 case ERROR_MARK:
3034 return expr;
3036 case INTEGER_CST:
3037 return integer_zerop (expr) ? boolean_false_node : boolean_true_node;
3039 case REAL_CST:
3040 return real_zerop (expr) ? boolean_false_node : boolean_true_node;
3042 case ADDR_EXPR:
3043 /* If we are taking the address of a external decl, it might be zero
3044 if it is weak, so we cannot optimize. */
3045 if (DECL_P (TREE_OPERAND (expr, 0))
3046 && DECL_EXTERNAL (TREE_OPERAND (expr, 0)))
3047 break;
3049 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 0)))
3050 return build (COMPOUND_EXPR, boolean_type_node,
3051 TREE_OPERAND (expr, 0), boolean_true_node);
3052 else
3053 return boolean_true_node;
3055 case COMPLEX_EXPR:
3056 return build_binary_op ((TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
3057 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
3058 truthvalue_conversion (TREE_OPERAND (expr, 0)),
3059 truthvalue_conversion (TREE_OPERAND (expr, 1)),
3062 case NEGATE_EXPR:
3063 case ABS_EXPR:
3064 case FLOAT_EXPR:
3065 case FFS_EXPR:
3066 /* These don't change whether an object is non-zero or zero. */
3067 return truthvalue_conversion (TREE_OPERAND (expr, 0));
3069 case LROTATE_EXPR:
3070 case RROTATE_EXPR:
3071 /* These don't change whether an object is zero or non-zero, but
3072 we can't ignore them if their second arg has side-effects. */
3073 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
3074 return build (COMPOUND_EXPR, boolean_type_node, TREE_OPERAND (expr, 1),
3075 truthvalue_conversion (TREE_OPERAND (expr, 0)));
3076 else
3077 return truthvalue_conversion (TREE_OPERAND (expr, 0));
3079 case COND_EXPR:
3080 /* Distribute the conversion into the arms of a COND_EXPR. */
3081 return fold (build (COND_EXPR, boolean_type_node, TREE_OPERAND (expr, 0),
3082 truthvalue_conversion (TREE_OPERAND (expr, 1)),
3083 truthvalue_conversion (TREE_OPERAND (expr, 2))));
3085 case CONVERT_EXPR:
3086 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
3087 since that affects how `default_conversion' will behave. */
3088 if (TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE
3089 || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
3090 break;
3091 /* fall through... */
3092 case NOP_EXPR:
3093 /* If this is widening the argument, we can ignore it. */
3094 if (TYPE_PRECISION (TREE_TYPE (expr))
3095 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
3096 return truthvalue_conversion (TREE_OPERAND (expr, 0));
3097 break;
3099 case MINUS_EXPR:
3100 /* With IEEE arithmetic, x - x may not equal 0, so we can't optimize
3101 this case. */
3102 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
3103 && TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE)
3104 break;
3105 /* fall through... */
3106 case BIT_XOR_EXPR:
3107 /* This and MINUS_EXPR can be changed into a comparison of the
3108 two objects. */
3109 if (TREE_TYPE (TREE_OPERAND (expr, 0))
3110 == TREE_TYPE (TREE_OPERAND (expr, 1)))
3111 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
3112 TREE_OPERAND (expr, 1), 1);
3113 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
3114 fold (build1 (NOP_EXPR,
3115 TREE_TYPE (TREE_OPERAND (expr, 0)),
3116 TREE_OPERAND (expr, 1))), 1);
3118 case BIT_AND_EXPR:
3119 if (integer_onep (TREE_OPERAND (expr, 1))
3120 && TREE_TYPE (expr) != boolean_type_node)
3121 /* Using convert here would cause infinite recursion. */
3122 return build1 (NOP_EXPR, boolean_type_node, expr);
3123 break;
3125 case MODIFY_EXPR:
3126 if (warn_parentheses && C_EXP_ORIGINAL_CODE (expr) == MODIFY_EXPR)
3127 warning ("suggest parentheses around assignment used as truth value");
3128 break;
3130 default:
3131 break;
3134 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
3136 tree tem = save_expr (expr);
3137 return (build_binary_op
3138 ((TREE_SIDE_EFFECTS (expr)
3139 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
3140 truthvalue_conversion (build_unary_op (REALPART_EXPR, tem, 0)),
3141 truthvalue_conversion (build_unary_op (IMAGPART_EXPR, tem, 0)),
3142 0));
3145 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
3148 #if USE_CPPLIB
3149 /* Read the rest of a #-directive from input stream FINPUT.
3150 In normal use, the directive name and the white space after it
3151 have already been read, so they won't be included in the result.
3152 We allow for the fact that the directive line may contain
3153 a newline embedded within a character or string literal which forms
3154 a part of the directive.
3156 The value is a string in a reusable buffer. It remains valid
3157 only until the next time this function is called. */
3158 unsigned char *yy_cur, *yy_lim;
3160 #define GETC() (yy_cur < yy_lim ? *yy_cur++ : yy_get_token ())
3161 #define UNGETC(c) ((c) == EOF ? 0 : yy_cur--)
3164 yy_get_token ()
3166 for (;;)
3168 parse_in.limit = parse_in.token_buffer;
3169 cpp_token = cpp_get_token (&parse_in);
3170 if (cpp_token == CPP_EOF)
3171 return -1;
3172 yy_lim = CPP_PWRITTEN (&parse_in);
3173 yy_cur = parse_in.token_buffer;
3174 if (yy_cur < yy_lim)
3175 return *yy_cur++;
3179 char *
3180 get_directive_line ()
3182 static char *directive_buffer = NULL;
3183 static unsigned buffer_length = 0;
3184 register char *p;
3185 register char *buffer_limit;
3186 register int looking_for = 0;
3187 register int char_escaped = 0;
3189 if (buffer_length == 0)
3191 directive_buffer = (char *)xmalloc (128);
3192 buffer_length = 128;
3195 buffer_limit = &directive_buffer[buffer_length];
3197 for (p = directive_buffer; ; )
3199 int c;
3201 /* Make buffer bigger if it is full. */
3202 if (p >= buffer_limit)
3204 register unsigned bytes_used = (p - directive_buffer);
3206 buffer_length *= 2;
3207 directive_buffer
3208 = (char *)xrealloc (directive_buffer, buffer_length);
3209 p = &directive_buffer[bytes_used];
3210 buffer_limit = &directive_buffer[buffer_length];
3213 c = GETC ();
3215 /* Discard initial whitespace. */
3216 if ((c == ' ' || c == '\t') && p == directive_buffer)
3217 continue;
3219 /* Detect the end of the directive. */
3220 if (c == '\n' && looking_for == 0)
3222 UNGETC (c);
3223 c = '\0';
3226 *p++ = c;
3228 if (c == 0)
3229 return directive_buffer;
3231 /* Handle string and character constant syntax. */
3232 if (looking_for)
3234 if (looking_for == c && !char_escaped)
3235 looking_for = 0; /* Found terminator... stop looking. */
3237 else
3238 if (c == '\'' || c == '"')
3239 looking_for = c; /* Don't stop buffering until we see another
3240 another one of these (or an EOF). */
3242 /* Handle backslash. */
3243 char_escaped = (c == '\\' && ! char_escaped);
3246 #else
3247 /* Read the rest of a #-directive from input stream FINPUT.
3248 In normal use, the directive name and the white space after it
3249 have already been read, so they won't be included in the result.
3250 We allow for the fact that the directive line may contain
3251 a newline embedded within a character or string literal which forms
3252 a part of the directive.
3254 The value is a string in a reusable buffer. It remains valid
3255 only until the next time this function is called.
3257 The terminating character ('\n' or EOF) is left in FINPUT for the
3258 caller to re-read. */
3260 char *
3261 get_directive_line (finput)
3262 register FILE *finput;
3264 static char *directive_buffer = NULL;
3265 static unsigned buffer_length = 0;
3266 register char *p;
3267 register char *buffer_limit;
3268 register int looking_for = 0;
3269 register int char_escaped = 0;
3271 if (buffer_length == 0)
3273 directive_buffer = (char *)xmalloc (128);
3274 buffer_length = 128;
3277 buffer_limit = &directive_buffer[buffer_length];
3279 for (p = directive_buffer; ; )
3281 int c;
3283 /* Make buffer bigger if it is full. */
3284 if (p >= buffer_limit)
3286 register unsigned bytes_used = (p - directive_buffer);
3288 buffer_length *= 2;
3289 directive_buffer
3290 = (char *)xrealloc (directive_buffer, buffer_length);
3291 p = &directive_buffer[bytes_used];
3292 buffer_limit = &directive_buffer[buffer_length];
3295 c = getc (finput);
3297 /* Discard initial whitespace. */
3298 if ((c == ' ' || c == '\t') && p == directive_buffer)
3299 continue;
3301 /* Detect the end of the directive. */
3302 if (looking_for == 0
3303 && (c == '\n' || c == EOF))
3305 ungetc (c, finput);
3306 c = '\0';
3309 *p++ = c;
3311 if (c == 0)
3312 return directive_buffer;
3314 /* Handle string and character constant syntax. */
3315 if (looking_for)
3317 if (looking_for == c && !char_escaped)
3318 looking_for = 0; /* Found terminator... stop looking. */
3320 else
3321 if (c == '\'' || c == '"')
3322 looking_for = c; /* Don't stop buffering until we see another
3323 one of these (or an EOF). */
3325 /* Handle backslash. */
3326 char_escaped = (c == '\\' && ! char_escaped);
3329 #endif /* !USE_CPPLIB */
3331 /* Make a variant type in the proper way for C/C++, propagating qualifiers
3332 down to the element type of an array. */
3334 tree
3335 c_build_qualified_type (type, type_quals)
3336 tree type;
3337 int type_quals;
3339 /* A restrict-qualified pointer type must be a pointer to object or
3340 incomplete type. Note that the use of POINTER_TYPE_P also allows
3341 REFERENCE_TYPEs, which is appropriate for C++. Unfortunately,
3342 the C++ front-end also use POINTER_TYPE for pointer-to-member
3343 values, so even though it should be illegal to use `restrict'
3344 with such an entity we don't flag that here. Thus, special case
3345 code for that case is required in the C++ front-end. */
3346 if ((type_quals & TYPE_QUAL_RESTRICT)
3347 && (!POINTER_TYPE_P (type)
3348 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
3350 error ("invalid use of `restrict'");
3351 type_quals &= ~TYPE_QUAL_RESTRICT;
3354 if (TREE_CODE (type) == ARRAY_TYPE)
3355 return build_array_type (c_build_qualified_type (TREE_TYPE (type),
3356 type_quals),
3357 TYPE_DOMAIN (type));
3358 return build_qualified_type (type, type_quals);
3361 /* Apply the TYPE_QUALS to the new DECL. */
3363 void
3364 c_apply_type_quals_to_decl (type_quals, decl)
3365 int type_quals;
3366 tree decl;
3368 if ((type_quals & TYPE_QUAL_CONST)
3369 || (TREE_TYPE (decl)
3370 && TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE))
3371 TREE_READONLY (decl) = 1;
3372 if (type_quals & TYPE_QUAL_VOLATILE)
3374 TREE_SIDE_EFFECTS (decl) = 1;
3375 TREE_THIS_VOLATILE (decl) = 1;
3377 if (type_quals & TYPE_QUAL_RESTRICT)
3379 if (!TREE_TYPE (decl)
3380 || !POINTER_TYPE_P (TREE_TYPE (decl))
3381 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (TREE_TYPE (decl))))
3382 error ("invalid use of `restrict'");
3383 else if (flag_strict_aliasing)
3385 /* No two restricted pointers can point at the same thing.
3386 However, a restricted pointer can point at the same thing
3387 as an unrestricted pointer, if that unrestricted pointer
3388 is based on the restricted pointer. So, we make the
3389 alias set for the restricted pointer a subset of the
3390 alias set for the type pointed to by the type of the
3391 decl. */
3393 HOST_WIDE_INT pointed_to_alias_set
3394 = get_alias_set (TREE_TYPE (TREE_TYPE (decl)));
3396 if (pointed_to_alias_set == 0)
3397 /* It's not legal to make a subset of alias set zero. */
3399 else
3401 DECL_POINTER_ALIAS_SET (decl) = new_alias_set ();
3402 record_alias_subset (pointed_to_alias_set,
3403 DECL_POINTER_ALIAS_SET (decl));
3410 /* Return the typed-based alias set for T, which may be an expression
3411 or a type. Return -1 if we don't do anything special. */
3413 HOST_WIDE_INT
3414 lang_get_alias_set (t)
3415 tree t;
3417 tree u;
3419 /* Permit type-punning when accessing a union, provided the access
3420 is directly through the union. For example, this code does not
3421 permit taking the address of a union member and then storing
3422 through it. Even the type-punning allowed here is a GCC
3423 extension, albeit a common and useful one; the C standard says
3424 that such accesses have implementation-defined behavior. */
3425 for (u = t;
3426 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
3427 u = TREE_OPERAND (u, 0))
3428 if (TREE_CODE (u) == COMPONENT_REF
3429 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
3430 return 0;
3432 /* If this is a char *, the ANSI C standard says it can alias
3433 anything. Note that all references need do this. */
3434 if (TREE_CODE_CLASS (TREE_CODE (t)) == 'r'
3435 && TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
3436 && TYPE_PRECISION (TREE_TYPE (t)) == TYPE_PRECISION (char_type_node))
3437 return 0;
3439 /* That's all the expressions we handle specially. */
3440 if (! TYPE_P (t))
3441 return -1;
3443 /* The C standard specifically allows aliasing between signed and
3444 unsigned variants of the same type. We treat the signed
3445 variant as canonical. */
3446 if (TREE_CODE (t) == INTEGER_TYPE && TREE_UNSIGNED (t))
3448 tree t1 = signed_type (t);
3450 /* t1 == t can happen for boolean nodes which are always unsigned. */
3451 if (t1 != t)
3452 return get_alias_set (t1);
3454 else if (POINTER_TYPE_P (t))
3456 tree t1;
3458 /* Unfortunately, there is no canonical form of a pointer type.
3459 In particular, if we have `typedef int I', then `int *', and
3460 `I *' are different types. So, we have to pick a canonical
3461 representative. We do this below.
3463 Technically, this approach is actually more conservative that
3464 it needs to be. In particular, `const int *' and `int *'
3465 chould be in different alias sets, according to the C and C++
3466 standard, since their types are not the same, and so,
3467 technically, an `int **' and `const int **' cannot point at
3468 the same thing.
3470 But, the standard is wrong. In particular, this code is
3471 legal C++:
3473 int *ip;
3474 int **ipp = &ip;
3475 const int* const* cipp = &ip;
3477 And, it doesn't make sense for that to be legal unless you
3478 can dereference IPP and CIPP. So, we ignore cv-qualifiers on
3479 the pointed-to types. This issue has been reported to the
3480 C++ committee. */
3481 t1 = TYPE_MAIN_VARIANT (TREE_TYPE (t));
3482 t1 = ((TREE_CODE (t) == POINTER_TYPE)
3483 ? build_pointer_type (t1) : build_reference_type (t1));
3484 if (t1 != t)
3485 return get_alias_set (t1);
3487 /* It's not yet safe to use alias sets for classes in C++ because
3488 the TYPE_FIELDs list for a class doesn't mention base classes. */
3489 else if (c_language == clk_cplusplus && AGGREGATE_TYPE_P (t))
3490 return 0;
3492 return -1;
3495 /* Build tree nodes and builtin functions common to both C and C++ language
3496 frontends.
3497 CPLUS_MODE is nonzero if we are called from the C++ frontend, we generate
3498 some stricter prototypes in that case.
3499 NO_BUILTINS and NO_NONANSI_BUILTINS contain the respective values of
3500 the language frontend flags flag_no_builtin and
3501 flag_no_nonansi_builtin. */
3503 void
3504 c_common_nodes_and_builtins (cplus_mode, no_builtins, no_nonansi_builtins)
3505 int cplus_mode, no_builtins, no_nonansi_builtins;
3507 tree temp;
3508 tree memcpy_ftype, memset_ftype, strlen_ftype;
3509 tree bzero_ftype, bcmp_ftype;
3510 tree endlink, int_endlink, double_endlink, unsigned_endlink;
3511 tree sizetype_endlink;
3512 tree ptr_ftype, ptr_ftype_unsigned;
3513 tree void_ftype_any, void_ftype_int, int_ftype_any;
3514 tree double_ftype_double, double_ftype_double_double;
3515 tree float_ftype_float, ldouble_ftype_ldouble;
3516 tree int_ftype_cptr_cptr_sizet;
3517 tree int_ftype_string_string, string_ftype_ptr_ptr;
3518 tree long_ftype_long;
3519 /* Either char* or void*. */
3520 tree traditional_ptr_type_node;
3521 /* Either const char* or const void*. */
3522 tree traditional_cptr_type_node;
3523 tree traditional_len_type_node;
3524 tree traditional_len_endlink;
3525 tree va_list_ref_type_node;
3526 tree va_list_arg_type_node;
3528 pushdecl (build_decl (TYPE_DECL, get_identifier ("__builtin_va_list"),
3529 va_list_type_node));
3531 pushdecl (build_decl (TYPE_DECL, get_identifier ("__builtin_ptrdiff_t"),
3532 ptrdiff_type_node));
3534 pushdecl (build_decl (TYPE_DECL, get_identifier ("__builtin_size_t"),
3535 sizetype));
3537 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
3539 va_list_arg_type_node = va_list_ref_type_node =
3540 build_pointer_type (TREE_TYPE (va_list_type_node));
3542 else
3544 va_list_arg_type_node = va_list_type_node;
3545 va_list_ref_type_node = build_reference_type (va_list_type_node);
3548 endlink = void_list_node;
3549 int_endlink = tree_cons (NULL_TREE, integer_type_node, endlink);
3550 double_endlink = tree_cons (NULL_TREE, double_type_node, endlink);
3551 unsigned_endlink = tree_cons (NULL_TREE, unsigned_type_node, endlink);
3553 ptr_ftype = build_function_type (ptr_type_node, NULL_TREE);
3554 ptr_ftype_unsigned = build_function_type (ptr_type_node, unsigned_endlink);
3555 sizetype_endlink = tree_cons (NULL_TREE, TYPE_DOMAIN (sizetype), endlink);
3556 /* We realloc here because sizetype could be int or unsigned. S'ok. */
3557 ptr_ftype_sizetype = build_function_type (ptr_type_node, sizetype_endlink);
3559 int_ftype_any = build_function_type (integer_type_node, NULL_TREE);
3560 void_ftype_any = build_function_type (void_type_node, NULL_TREE);
3561 void_ftype = build_function_type (void_type_node, endlink);
3562 void_ftype_int = build_function_type (void_type_node, int_endlink);
3563 void_ftype_ptr
3564 = build_function_type (void_type_node,
3565 tree_cons (NULL_TREE, ptr_type_node, endlink));
3567 float_ftype_float
3568 = build_function_type (float_type_node,
3569 tree_cons (NULL_TREE, float_type_node, endlink));
3571 double_ftype_double
3572 = build_function_type (double_type_node, double_endlink);
3574 ldouble_ftype_ldouble
3575 = build_function_type (long_double_type_node,
3576 tree_cons (NULL_TREE, long_double_type_node,
3577 endlink));
3579 double_ftype_double_double
3580 = build_function_type (double_type_node,
3581 tree_cons (NULL_TREE, double_type_node,
3582 double_endlink));
3584 int_ftype_int
3585 = build_function_type (integer_type_node, int_endlink);
3587 long_ftype_long
3588 = build_function_type (long_integer_type_node,
3589 tree_cons (NULL_TREE, long_integer_type_node,
3590 endlink));
3592 int_ftype_cptr_cptr_sizet
3593 = build_function_type (integer_type_node,
3594 tree_cons (NULL_TREE, const_ptr_type_node,
3595 tree_cons (NULL_TREE, const_ptr_type_node,
3596 tree_cons (NULL_TREE,
3597 sizetype,
3598 endlink))));
3600 /* Prototype for strcpy. */
3601 string_ftype_ptr_ptr
3602 = build_function_type (string_type_node,
3603 tree_cons (NULL_TREE, string_type_node,
3604 tree_cons (NULL_TREE,
3605 const_string_type_node,
3606 endlink)));
3608 traditional_len_type_node = (flag_traditional && ! cplus_mode
3609 ? integer_type_node : sizetype);
3610 traditional_len_endlink = tree_cons (NULL_TREE, traditional_len_type_node,
3611 endlink);
3613 /* Prototype for strcmp. */
3614 int_ftype_string_string
3615 = build_function_type (integer_type_node,
3616 tree_cons (NULL_TREE, const_string_type_node,
3617 tree_cons (NULL_TREE,
3618 const_string_type_node,
3619 endlink)));
3621 /* Prototype for strlen. */
3622 strlen_ftype
3623 = build_function_type (traditional_len_type_node,
3624 tree_cons (NULL_TREE, const_string_type_node,
3625 endlink));
3627 traditional_ptr_type_node = (flag_traditional && ! cplus_mode
3628 ? string_type_node : ptr_type_node);
3629 traditional_cptr_type_node = (flag_traditional && ! cplus_mode
3630 ? const_string_type_node : const_ptr_type_node);
3632 /* Prototype for memcpy. */
3633 memcpy_ftype
3634 = build_function_type (traditional_ptr_type_node,
3635 tree_cons (NULL_TREE, ptr_type_node,
3636 tree_cons (NULL_TREE, const_ptr_type_node,
3637 sizetype_endlink)));
3639 /* Prototype for memset. */
3640 memset_ftype
3641 = build_function_type (traditional_ptr_type_node,
3642 tree_cons (NULL_TREE, ptr_type_node,
3643 tree_cons (NULL_TREE, integer_type_node,
3644 tree_cons (NULL_TREE,
3645 sizetype,
3646 endlink))));
3648 /* Prototype for bzero. */
3649 bzero_ftype
3650 = build_function_type (void_type_node,
3651 tree_cons (NULL_TREE, traditional_ptr_type_node,
3652 traditional_len_endlink));
3654 /* Prototype for bcmp. */
3655 bcmp_ftype
3656 = build_function_type (integer_type_node,
3657 tree_cons (NULL_TREE, traditional_cptr_type_node,
3658 tree_cons (NULL_TREE,
3659 traditional_cptr_type_node,
3660 traditional_len_endlink)));
3662 builtin_function ("__builtin_constant_p", default_function_type,
3663 BUILT_IN_CONSTANT_P, BUILT_IN_NORMAL, NULL_PTR);
3665 builtin_function ("__builtin_return_address", ptr_ftype_unsigned,
3666 BUILT_IN_RETURN_ADDRESS, BUILT_IN_NORMAL, NULL_PTR);
3668 builtin_function ("__builtin_frame_address", ptr_ftype_unsigned,
3669 BUILT_IN_FRAME_ADDRESS, BUILT_IN_NORMAL, NULL_PTR);
3671 builtin_function ("__builtin_alloca", ptr_ftype_sizetype,
3672 BUILT_IN_ALLOCA, BUILT_IN_NORMAL, "alloca");
3673 builtin_function ("__builtin_ffs", int_ftype_int, BUILT_IN_FFS,
3674 BUILT_IN_NORMAL, NULL_PTR);
3675 /* Define alloca, ffs as builtins.
3676 Declare _exit just to mark it as volatile. */
3677 if (! no_builtins && ! no_nonansi_builtins)
3679 #ifndef SMALL_STACK
3680 temp = builtin_function ("alloca", ptr_ftype_sizetype,
3681 BUILT_IN_ALLOCA, BUILT_IN_NORMAL, NULL_PTR);
3682 /* Suppress error if redefined as a non-function. */
3683 DECL_BUILT_IN_NONANSI (temp) = 1;
3684 #endif
3685 temp = builtin_function ("ffs", int_ftype_int, BUILT_IN_FFS,
3686 BUILT_IN_NORMAL, NULL_PTR);
3687 /* Suppress error if redefined as a non-function. */
3688 DECL_BUILT_IN_NONANSI (temp) = 1;
3689 temp = builtin_function ("_exit", void_ftype_int,
3690 0, NOT_BUILT_IN, NULL_PTR);
3691 TREE_THIS_VOLATILE (temp) = 1;
3692 TREE_SIDE_EFFECTS (temp) = 1;
3693 /* Suppress error if redefined as a non-function. */
3694 DECL_BUILT_IN_NONANSI (temp) = 1;
3696 /* The system prototypes for these functions have many
3697 variations, so don't specify parameters to avoid conflicts.
3698 The expand_* functions check the argument types anyway. */
3699 temp = builtin_function ("bzero", void_ftype_any,
3700 BUILT_IN_BZERO, BUILT_IN_NORMAL, NULL_PTR);
3701 DECL_BUILT_IN_NONANSI (temp) = 1;
3702 temp = builtin_function ("bcmp", int_ftype_any,
3703 BUILT_IN_BCMP, BUILT_IN_NORMAL, NULL_PTR);
3704 DECL_BUILT_IN_NONANSI (temp) = 1;
3707 builtin_function ("__builtin_abs", int_ftype_int, BUILT_IN_ABS,
3708 BUILT_IN_NORMAL, NULL_PTR);
3709 builtin_function ("__builtin_fabsf", float_ftype_float, BUILT_IN_FABS,
3710 BUILT_IN_NORMAL, NULL_PTR);
3711 builtin_function ("__builtin_fabs", double_ftype_double, BUILT_IN_FABS,
3712 BUILT_IN_NORMAL, NULL_PTR);
3713 builtin_function ("__builtin_fabsl", ldouble_ftype_ldouble, BUILT_IN_FABS,
3714 BUILT_IN_NORMAL, NULL_PTR);
3715 builtin_function ("__builtin_labs", long_ftype_long, BUILT_IN_LABS,
3716 BUILT_IN_NORMAL, NULL_PTR);
3717 builtin_function ("__builtin_saveregs", ptr_ftype, BUILT_IN_SAVEREGS,
3718 BUILT_IN_NORMAL, NULL_PTR);
3719 builtin_function ("__builtin_classify_type", default_function_type,
3720 BUILT_IN_CLASSIFY_TYPE, BUILT_IN_NORMAL, NULL_PTR);
3721 builtin_function ("__builtin_next_arg", ptr_ftype, BUILT_IN_NEXT_ARG,
3722 BUILT_IN_NORMAL, NULL_PTR);
3723 builtin_function ("__builtin_args_info", int_ftype_int, BUILT_IN_ARGS_INFO,
3724 BUILT_IN_NORMAL, NULL_PTR);
3725 builtin_function ("__builtin_setjmp",
3726 build_function_type (integer_type_node,
3727 tree_cons (NULL_TREE, ptr_type_node,
3728 endlink)),
3729 BUILT_IN_SETJMP, BUILT_IN_NORMAL, NULL_PTR);
3730 builtin_function ("__builtin_longjmp",
3731 build_function_type (void_type_node,
3732 tree_cons (NULL_TREE, ptr_type_node,
3733 tree_cons (NULL_TREE,
3734 integer_type_node,
3735 endlink))),
3736 BUILT_IN_LONGJMP, BUILT_IN_NORMAL, NULL_PTR);
3737 builtin_function ("__builtin_trap", void_ftype, BUILT_IN_TRAP,
3738 BUILT_IN_NORMAL, NULL_PTR);
3740 /* ISO C99 IEEE Unordered compares. */
3741 builtin_function ("__builtin_isgreater", default_function_type,
3742 BUILT_IN_ISGREATER, BUILT_IN_NORMAL, NULL_PTR);
3743 builtin_function ("__builtin_isgreaterequal", default_function_type,
3744 BUILT_IN_ISGREATEREQUAL, BUILT_IN_NORMAL, NULL_PTR);
3745 builtin_function ("__builtin_isless", default_function_type,
3746 BUILT_IN_ISLESS, BUILT_IN_NORMAL, NULL_PTR);
3747 builtin_function ("__builtin_islessequal", default_function_type,
3748 BUILT_IN_ISLESSEQUAL, BUILT_IN_NORMAL, NULL_PTR);
3749 builtin_function ("__builtin_islessgreater", default_function_type,
3750 BUILT_IN_ISLESSGREATER, BUILT_IN_NORMAL, NULL_PTR);
3751 builtin_function ("__builtin_isunordered", default_function_type,
3752 BUILT_IN_ISUNORDERED, BUILT_IN_NORMAL, NULL_PTR);
3754 /* Untyped call and return. */
3755 builtin_function ("__builtin_apply_args", ptr_ftype,
3756 BUILT_IN_APPLY_ARGS, BUILT_IN_NORMAL, NULL_PTR);
3758 temp = tree_cons (NULL_TREE,
3759 build_pointer_type (build_function_type (void_type_node,
3760 NULL_TREE)),
3761 tree_cons (NULL_TREE,
3762 ptr_type_node,
3763 tree_cons (NULL_TREE,
3764 sizetype,
3765 endlink)));
3766 builtin_function ("__builtin_apply",
3767 build_function_type (ptr_type_node, temp),
3768 BUILT_IN_APPLY, BUILT_IN_NORMAL, NULL_PTR);
3769 builtin_function ("__builtin_return", void_ftype_ptr,
3770 BUILT_IN_RETURN, BUILT_IN_NORMAL, NULL_PTR);
3772 /* Support for varargs.h and stdarg.h. */
3773 builtin_function ("__builtin_varargs_start",
3774 build_function_type (void_type_node,
3775 tree_cons (NULL_TREE,
3776 va_list_ref_type_node,
3777 endlink)),
3778 BUILT_IN_VARARGS_START, BUILT_IN_NORMAL, NULL_PTR);
3780 builtin_function ("__builtin_stdarg_start",
3781 build_function_type (void_type_node,
3782 tree_cons (NULL_TREE,
3783 va_list_ref_type_node,
3784 NULL_TREE)),
3785 BUILT_IN_STDARG_START, BUILT_IN_NORMAL, NULL_PTR);
3787 builtin_function ("__builtin_va_end",
3788 build_function_type (void_type_node,
3789 tree_cons (NULL_TREE,
3790 va_list_ref_type_node,
3791 endlink)),
3792 BUILT_IN_VA_END, BUILT_IN_NORMAL, NULL_PTR);
3794 builtin_function ("__builtin_va_copy",
3795 build_function_type (void_type_node,
3796 tree_cons (NULL_TREE,
3797 va_list_ref_type_node,
3798 tree_cons (NULL_TREE,
3799 va_list_arg_type_node,
3800 endlink))),
3801 BUILT_IN_VA_COPY, BUILT_IN_NORMAL, NULL_PTR);
3803 /* ??? Ought to be `T __builtin_expect(T, T)' for any type T. */
3804 builtin_function ("__builtin_expect",
3805 build_function_type (long_integer_type_node,
3806 tree_cons (NULL_TREE,
3807 long_integer_type_node,
3808 tree_cons (NULL_TREE,
3809 long_integer_type_node,
3810 endlink))),
3811 BUILT_IN_EXPECT, BUILT_IN_NORMAL, NULL_PTR);
3813 /* Currently under experimentation. */
3814 builtin_function ("__builtin_memcpy", memcpy_ftype, BUILT_IN_MEMCPY,
3815 BUILT_IN_NORMAL, "memcpy");
3816 builtin_function ("__builtin_memcmp", int_ftype_cptr_cptr_sizet,
3817 BUILT_IN_MEMCMP, BUILT_IN_NORMAL, "memcmp");
3818 builtin_function ("__builtin_memset", memset_ftype,
3819 BUILT_IN_MEMSET, BUILT_IN_NORMAL, "memset");
3820 builtin_function ("__builtin_bzero", bzero_ftype,
3821 BUILT_IN_BZERO, BUILT_IN_NORMAL, "bzero");
3822 builtin_function ("__builtin_bcmp", bcmp_ftype,
3823 BUILT_IN_BCMP, BUILT_IN_NORMAL, "bcmp");
3824 builtin_function ("__builtin_strcmp", int_ftype_string_string,
3825 BUILT_IN_STRCMP, BUILT_IN_NORMAL, "strcmp");
3826 builtin_function ("__builtin_strcpy", string_ftype_ptr_ptr,
3827 BUILT_IN_STRCPY, BUILT_IN_NORMAL, "strcpy");
3828 builtin_function ("__builtin_strlen", strlen_ftype,
3829 BUILT_IN_STRLEN, BUILT_IN_NORMAL, "strlen");
3830 builtin_function ("__builtin_sqrtf", float_ftype_float,
3831 BUILT_IN_FSQRT, BUILT_IN_NORMAL, "sqrtf");
3832 builtin_function ("__builtin_fsqrt", double_ftype_double,
3833 BUILT_IN_FSQRT, BUILT_IN_NORMAL, "sqrt");
3834 builtin_function ("__builtin_sqrtl", ldouble_ftype_ldouble,
3835 BUILT_IN_FSQRT, BUILT_IN_NORMAL, "sqrtl");
3836 builtin_function ("__builtin_sinf", float_ftype_float,
3837 BUILT_IN_SIN, BUILT_IN_NORMAL, "sinf");
3838 builtin_function ("__builtin_sin", double_ftype_double,
3839 BUILT_IN_SIN, BUILT_IN_NORMAL, "sin");
3840 builtin_function ("__builtin_sinl", ldouble_ftype_ldouble,
3841 BUILT_IN_SIN, BUILT_IN_NORMAL, "sinl");
3842 builtin_function ("__builtin_cosf", float_ftype_float,
3843 BUILT_IN_COS, BUILT_IN_NORMAL, "cosf");
3844 builtin_function ("__builtin_cos", double_ftype_double,
3845 BUILT_IN_COS, BUILT_IN_NORMAL, "cos");
3846 builtin_function ("__builtin_cosl", ldouble_ftype_ldouble,
3847 BUILT_IN_COS, BUILT_IN_NORMAL, "cosl");
3849 if (! no_builtins)
3851 builtin_function ("abs", int_ftype_int, BUILT_IN_ABS,
3852 BUILT_IN_NORMAL, NULL_PTR);
3853 builtin_function ("fabsf", float_ftype_float, BUILT_IN_FABS,
3854 BUILT_IN_NORMAL, NULL_PTR);
3855 builtin_function ("fabs", double_ftype_double, BUILT_IN_FABS,
3856 BUILT_IN_NORMAL, NULL_PTR);
3857 builtin_function ("fabsl", ldouble_ftype_ldouble, BUILT_IN_FABS,
3858 BUILT_IN_NORMAL, NULL_PTR);
3859 builtin_function ("labs", long_ftype_long, BUILT_IN_LABS,
3860 BUILT_IN_NORMAL, NULL_PTR);
3861 builtin_function ("memcpy", memcpy_ftype, BUILT_IN_MEMCPY,
3862 BUILT_IN_NORMAL, NULL_PTR);
3863 builtin_function ("memcmp", int_ftype_cptr_cptr_sizet, BUILT_IN_MEMCMP,
3864 BUILT_IN_NORMAL, NULL_PTR);
3865 builtin_function ("memset", memset_ftype, BUILT_IN_MEMSET,
3866 BUILT_IN_NORMAL, NULL_PTR);
3867 builtin_function ("strcmp", int_ftype_string_string, BUILT_IN_STRCMP,
3868 BUILT_IN_NORMAL, NULL_PTR);
3869 builtin_function ("strcpy", string_ftype_ptr_ptr, BUILT_IN_STRCPY,
3870 BUILT_IN_NORMAL, NULL_PTR);
3871 builtin_function ("strlen", strlen_ftype, BUILT_IN_STRLEN,
3872 BUILT_IN_NORMAL, NULL_PTR);
3873 builtin_function ("sqrtf", float_ftype_float, BUILT_IN_FSQRT,
3874 BUILT_IN_NORMAL, NULL_PTR);
3875 builtin_function ("sqrt", double_ftype_double, BUILT_IN_FSQRT,
3876 BUILT_IN_NORMAL, NULL_PTR);
3877 builtin_function ("sqrtl", ldouble_ftype_ldouble, BUILT_IN_FSQRT,
3878 BUILT_IN_NORMAL, NULL_PTR);
3879 builtin_function ("sinf", float_ftype_float, BUILT_IN_SIN,
3880 BUILT_IN_NORMAL, NULL_PTR);
3881 builtin_function ("sin", double_ftype_double, BUILT_IN_SIN,
3882 BUILT_IN_NORMAL, NULL_PTR);
3883 builtin_function ("sinl", ldouble_ftype_ldouble, BUILT_IN_SIN,
3884 BUILT_IN_NORMAL, NULL_PTR);
3885 builtin_function ("cosf", float_ftype_float, BUILT_IN_COS,
3886 BUILT_IN_NORMAL, NULL_PTR);
3887 builtin_function ("cos", double_ftype_double, BUILT_IN_COS,
3888 BUILT_IN_NORMAL, NULL_PTR);
3889 builtin_function ("cosl", ldouble_ftype_ldouble, BUILT_IN_COS,
3890 BUILT_IN_NORMAL, NULL_PTR);
3892 /* Declare these functions volatile
3893 to avoid spurious "control drops through" warnings. */
3894 temp = builtin_function ("abort", cplus_mode ? void_ftype : void_ftype_any,
3895 0, NOT_BUILT_IN, NULL_PTR);
3896 TREE_THIS_VOLATILE (temp) = 1;
3897 TREE_SIDE_EFFECTS (temp) = 1;
3899 #if 0 /* ??? The C++ frontend used to do this. */
3900 /* Well, these are actually ANSI, but we can't set DECL_BUILT_IN on
3901 them... */
3902 DECL_BUILT_IN_NONANSI (temp) = 1;
3903 #endif
3904 temp = builtin_function ("exit",
3905 cplus_mode ? void_ftype_int : void_ftype_any,
3906 0, NOT_BUILT_IN, NULL_PTR);
3907 TREE_THIS_VOLATILE (temp) = 1;
3908 TREE_SIDE_EFFECTS (temp) = 1;
3910 #if 0 /* ??? The C++ frontend used to do this. */
3911 /* Well, these are actually ANSI, but we can't set DECL_BUILT_IN on
3912 them... */
3913 DECL_BUILT_IN_NONANSI (temp) = 1;
3914 #endif
3917 #if 0
3918 /* Support for these has not been written in either expand_builtin
3919 or build_function_call. */
3920 builtin_function ("__builtin_div", default_ftype, BUILT_IN_DIV,
3921 BUILT_IN_NORMAL, NULL_PTR);
3922 builtin_function ("__builtin_ldiv", default_ftype, BUILT_IN_LDIV,
3923 BUILT_IN_NORMAL, NULL_PTR);
3924 builtin_function ("__builtin_ffloor", double_ftype_double, BUILT_IN_FFLOOR,
3925 BUILT_IN_NORMAL, NULL_PTR);
3926 builtin_function ("__builtin_fceil", double_ftype_double, BUILT_IN_FCEIL,
3927 BUILT_IN_NORMAL, NULL_PTR);
3928 builtin_function ("__builtin_fmod", double_ftype_double_double,
3929 BUILT_IN_FMOD, BUILT_IN_NORMAL, NULL_PTR);
3930 builtin_function ("__builtin_frem", double_ftype_double_double,
3931 BUILT_IN_FREM, BUILT_IN_NORMAL, NULL_PTR);
3932 builtin_function ("__builtin_getexp", double_ftype_double, BUILT_IN_GETEXP,
3933 BUILT_IN_NORMAL, NULL_PTR);
3934 builtin_function ("__builtin_getman", double_ftype_double, BUILT_IN_GETMAN,
3935 BUILT_IN_NORMAL, NULL_PTR);
3936 #endif
3938 /* ??? Perhaps there's a better place to do this. But it is related
3939 to __builtin_va_arg, so it isn't that off-the-wall. */
3940 lang_type_promotes_to = simple_type_promotes_to;
3943 tree
3944 build_va_arg (expr, type)
3945 tree expr, type;
3947 return build1 (VA_ARG_EXPR, type, expr);
3950 /* Given a type, apply default promotions wrt unnamed function arguments
3951 and return the new type. Return NULL_TREE if no change. */
3952 /* ??? There is a function of the same name in the C++ front end that
3953 does something similar, but is more thorough and does not return NULL
3954 if no change. We could perhaps share code, but it would make the
3955 self_promoting_type property harder to identify. */
3957 tree
3958 simple_type_promotes_to (type)
3959 tree type;
3961 if (TYPE_MAIN_VARIANT (type) == float_type_node)
3962 return double_type_node;
3964 if (C_PROMOTING_INTEGER_TYPE_P (type))
3966 /* Traditionally, unsignedness is preserved in default promotions.
3967 Also preserve unsignedness if not really getting any wider. */
3968 if (TREE_UNSIGNED (type)
3969 && (flag_traditional
3970 || TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
3971 return unsigned_type_node;
3972 return integer_type_node;
3975 return NULL_TREE;
3978 /* Return 1 if PARMS specifies a fixed number of parameters
3979 and none of their types is affected by default promotions. */
3982 self_promoting_args_p (parms)
3983 tree parms;
3985 register tree t;
3986 for (t = parms; t; t = TREE_CHAIN (t))
3988 register tree type = TREE_VALUE (t);
3990 if (TREE_CHAIN (t) == 0 && type != void_type_node)
3991 return 0;
3993 if (type == 0)
3994 return 0;
3996 if (TYPE_MAIN_VARIANT (type) == float_type_node)
3997 return 0;
3999 if (C_PROMOTING_INTEGER_TYPE_P (type))
4000 return 0;
4002 return 1;
4005 /* Recognize certain built-in functions so we can make tree-codes
4006 other than CALL_EXPR. We do this when it enables fold-const.c
4007 to do something useful. */
4008 /* ??? By rights this should go in builtins.c, but only C and C++
4009 implement build_{binary,unary}_op. Not exactly sure what bits
4010 of functionality are actually needed from those functions, or
4011 where the similar functionality exists in the other front ends. */
4013 tree
4014 expand_tree_builtin (function, params, coerced_params)
4015 tree function, params, coerced_params;
4017 enum tree_code code;
4019 if (DECL_BUILT_IN_CLASS (function) != BUILT_IN_NORMAL)
4020 return NULL_TREE;
4022 switch (DECL_FUNCTION_CODE (function))
4024 case BUILT_IN_ABS:
4025 case BUILT_IN_LABS:
4026 case BUILT_IN_FABS:
4027 if (coerced_params == 0)
4028 return integer_zero_node;
4029 return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0);
4031 case BUILT_IN_ISGREATER:
4032 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
4033 code = UNLE_EXPR;
4034 else
4035 code = LE_EXPR;
4036 goto unordered_cmp;
4038 case BUILT_IN_ISGREATEREQUAL:
4039 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
4040 code = UNLT_EXPR;
4041 else
4042 code = LT_EXPR;
4043 goto unordered_cmp;
4045 case BUILT_IN_ISLESS:
4046 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
4047 code = UNGE_EXPR;
4048 else
4049 code = GE_EXPR;
4050 goto unordered_cmp;
4052 case BUILT_IN_ISLESSEQUAL:
4053 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
4054 code = UNGT_EXPR;
4055 else
4056 code = GT_EXPR;
4057 goto unordered_cmp;
4059 case BUILT_IN_ISLESSGREATER:
4060 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
4061 code = UNEQ_EXPR;
4062 else
4063 code = EQ_EXPR;
4064 goto unordered_cmp;
4066 case BUILT_IN_ISUNORDERED:
4067 if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT)
4068 return integer_zero_node;
4069 code = UNORDERED_EXPR;
4070 goto unordered_cmp;
4072 unordered_cmp:
4074 tree arg0, arg1;
4076 if (params == 0
4077 || TREE_CHAIN (params) == 0)
4079 error ("too few arguments to function `%s'",
4080 IDENTIFIER_POINTER (DECL_NAME (function)));
4081 return error_mark_node;
4083 else if (TREE_CHAIN (TREE_CHAIN (params)) != 0)
4085 error ("too many arguments to function `%s'",
4086 IDENTIFIER_POINTER (DECL_NAME (function)));
4087 return error_mark_node;
4090 arg0 = TREE_VALUE (params);
4091 arg1 = TREE_VALUE (TREE_CHAIN (params));
4092 arg0 = build_binary_op (code, arg0, arg1, 0);
4093 if (code != UNORDERED_EXPR)
4094 arg0 = build_unary_op (TRUTH_NOT_EXPR, arg0, 0);
4095 return arg0;
4097 break;
4099 default:
4100 break;
4103 return NULL_TREE;
4106 /* Tree code classes. */
4108 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
4110 static char c_tree_code_type[] = {
4111 'x',
4112 #include "c-common.def"
4114 #undef DEFTREECODE
4116 /* Table indexed by tree code giving number of expression
4117 operands beyond the fixed part of the node structure.
4118 Not used for types or decls. */
4120 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
4122 static int c_tree_code_length[] = {
4124 #include "c-common.def"
4126 #undef DEFTREECODE
4128 /* Names of tree components.
4129 Used for printing out the tree and error messages. */
4130 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
4132 static const char *c_tree_code_name[] = {
4133 "@@dummy",
4134 #include "c-common.def"
4136 #undef DEFTREECODE
4138 /* Adds the tree codes specific to the C front end to the list of all
4139 tree codes. */
4141 void
4142 add_c_tree_codes ()
4144 memcpy (tree_code_type + (int) LAST_AND_UNUSED_TREE_CODE,
4145 c_tree_code_type,
4146 (int)LAST_C_TREE_CODE - (int)LAST_AND_UNUSED_TREE_CODE);
4147 memcpy (tree_code_length + (int) LAST_AND_UNUSED_TREE_CODE,
4148 c_tree_code_length,
4149 (LAST_C_TREE_CODE - (int)LAST_AND_UNUSED_TREE_CODE) * sizeof (int));
4150 memcpy (tree_code_name + (int) LAST_AND_UNUSED_TREE_CODE,
4151 c_tree_code_name,
4152 (LAST_C_TREE_CODE - (int)LAST_AND_UNUSED_TREE_CODE) * sizeof (char *));