Use new tail-calling mechanism on ARM.
[official-gcc.git] / gcc / c-common.c
blob30985b560f8e05d28f5a0bec8dc94886a648de78
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-lex.h"
26 #include "c-tree.h"
27 #include "flags.h"
28 #include "toplev.h"
29 #include "output.h"
30 #include "c-pragma.h"
31 #include "rtl.h"
32 #include "ggc.h"
33 #include "expr.h"
34 #include "tm_p.h"
36 #if USE_CPPLIB
37 #include "cpplib.h"
38 cpp_reader parse_in;
39 cpp_options parse_options;
40 enum cpp_token cpp_token;
41 #endif
43 #undef WCHAR_TYPE_SIZE
44 #define WCHAR_TYPE_SIZE TYPE_PRECISION (wchar_type_node)
46 /* The following symbols are subsumed in the c_global_trees array, and
47 listed here individually for documentation purposes.
49 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
51 tree short_integer_type_node;
52 tree long_integer_type_node;
53 tree long_long_integer_type_node;
55 tree short_unsigned_type_node;
56 tree long_unsigned_type_node;
57 tree long_long_unsigned_type_node;
59 tree boolean_type_node;
60 tree boolean_false_node;
61 tree boolean_true_node;
63 tree ptrdiff_type_node;
65 tree unsigned_char_type_node;
66 tree signed_char_type_node;
67 tree wchar_type_node;
68 tree signed_wchar_type_node;
69 tree unsigned_wchar_type_node;
71 tree float_type_node;
72 tree double_type_node;
73 tree long_double_type_node;
75 tree complex_integer_type_node;
76 tree complex_float_type_node;
77 tree complex_double_type_node;
78 tree complex_long_double_type_node;
80 tree intQI_type_node;
81 tree intHI_type_node;
82 tree intSI_type_node;
83 tree intDI_type_node;
84 tree intTI_type_node;
86 tree unsigned_intQI_type_node;
87 tree unsigned_intHI_type_node;
88 tree unsigned_intSI_type_node;
89 tree unsigned_intDI_type_node;
90 tree unsigned_intTI_type_node;
92 tree widest_integer_literal_type_node;
93 tree widest_unsigned_literal_type_node;
95 Nodes for types `void *' and `const void *'.
97 tree ptr_type_node, const_ptr_type_node;
99 Nodes for types `char *' and `const char *'.
101 tree string_type_node, const_string_type_node;
103 Type `char[SOMENUMBER]'.
104 Used when an array of char is needed and the size is irrelevant.
106 tree char_array_type_node;
108 Type `int[SOMENUMBER]' or something like it.
109 Used when an array of int needed and the size is irrelevant.
111 tree int_array_type_node;
113 Type `wchar_t[SOMENUMBER]' or something like it.
114 Used when a wide string literal is created.
116 tree wchar_array_type_node;
118 Type `int ()' -- used for implicit declaration of functions.
120 tree default_function_type;
122 Function types `int (int)', etc.
124 tree int_ftype_int;
125 tree void_ftype;
126 tree void_ftype_ptr;
127 tree int_ftype_int;
128 tree ptr_ftype_sizetype;
130 A VOID_TYPE node, packaged in a TREE_LIST.
132 tree void_list_node;
136 tree c_global_trees[CTI_MAX];
138 tree (*make_fname_decl) PARAMS ((tree, const char *, int));
140 /* Nonzero means the expression being parsed will never be evaluated.
141 This is a count, since unevaluated expressions can nest. */
142 int skip_evaluation;
144 enum attrs {A_PACKED, A_NOCOMMON, A_COMMON, A_NORETURN, A_CONST, A_T_UNION,
145 A_NO_CHECK_MEMORY_USAGE, A_NO_INSTRUMENT_FUNCTION,
146 A_CONSTRUCTOR, A_DESTRUCTOR, A_MODE, A_SECTION, A_ALIGNED,
147 A_UNUSED, A_FORMAT, A_FORMAT_ARG, A_WEAK, A_ALIAS, A_MALLOC,
148 A_NO_LIMIT_STACK, A_PURE};
150 enum format_type { printf_format_type, scanf_format_type,
151 strftime_format_type };
153 static void add_attribute PARAMS ((enum attrs, const char *,
154 int, int, int));
155 static void init_attributes PARAMS ((void));
156 static void record_function_format PARAMS ((tree, tree, enum format_type,
157 int, int));
158 static void record_international_format PARAMS ((tree, tree, int));
159 static tree c_find_base_decl PARAMS ((tree));
160 static int default_valid_lang_attribute PARAMS ((tree, tree, tree, tree));
162 /* Keep a stack of if statements. We record the number of compound
163 statements seen up to the if keyword, as well as the line number
164 and file of the if. If a potentially ambiguous else is seen, that
165 fact is recorded; the warning is issued when we can be sure that
166 the enclosing if statement does not have an else branch. */
167 typedef struct
169 int compstmt_count;
170 int line;
171 const char *file;
172 int needs_warning;
173 } if_elt;
174 static void tfaff PARAMS ((void));
176 static if_elt *if_stack;
178 /* Amount of space in the if statement stack. */
179 static int if_stack_space = 0;
181 /* Stack pointer. */
182 static int if_stack_pointer = 0;
184 /* Generate RTL for the start of an if-then, and record the start of it
185 for ambiguous else detection. */
187 void
188 c_expand_start_cond (cond, exitflag, compstmt_count)
189 tree cond;
190 int exitflag;
191 int compstmt_count;
193 /* Make sure there is enough space on the stack. */
194 if (if_stack_space == 0)
196 if_stack_space = 10;
197 if_stack = (if_elt *)xmalloc (10 * sizeof (if_elt));
199 else if (if_stack_space == if_stack_pointer)
201 if_stack_space += 10;
202 if_stack = (if_elt *)xrealloc (if_stack, if_stack_space * sizeof (if_elt));
205 /* Record this if statement. */
206 if_stack[if_stack_pointer].compstmt_count = compstmt_count;
207 if_stack[if_stack_pointer].file = input_filename;
208 if_stack[if_stack_pointer].line = lineno;
209 if_stack[if_stack_pointer].needs_warning = 0;
210 if_stack_pointer++;
212 expand_start_cond (cond, exitflag);
215 /* Generate RTL for the end of an if-then. Optionally warn if a nested
216 if statement had an ambiguous else clause. */
218 void
219 c_expand_end_cond ()
221 if_stack_pointer--;
222 if (if_stack[if_stack_pointer].needs_warning)
223 warning_with_file_and_line (if_stack[if_stack_pointer].file,
224 if_stack[if_stack_pointer].line,
225 "suggest explicit braces to avoid ambiguous `else'");
226 expand_end_cond ();
229 /* Generate RTL between the then-clause and the else-clause
230 of an if-then-else. */
232 void
233 c_expand_start_else ()
235 /* An ambiguous else warning must be generated for the enclosing if
236 statement, unless we see an else branch for that one, too. */
237 if (warn_parentheses
238 && if_stack_pointer > 1
239 && (if_stack[if_stack_pointer - 1].compstmt_count
240 == if_stack[if_stack_pointer - 2].compstmt_count))
241 if_stack[if_stack_pointer - 2].needs_warning = 1;
243 /* Even if a nested if statement had an else branch, it can't be
244 ambiguous if this one also has an else. So don't warn in that
245 case. Also don't warn for any if statements nested in this else. */
246 if_stack[if_stack_pointer - 1].needs_warning = 0;
247 if_stack[if_stack_pointer - 1].compstmt_count--;
249 expand_start_else ();
252 /* Make bindings for __FUNCTION__, __PRETTY_FUNCTION__, and __func__. */
254 void
255 declare_function_name ()
257 const char *name, *printable_name;
259 if (current_function_decl == NULL)
261 name = "";
262 printable_name = "top level";
264 else
266 /* Allow functions to be nameless (such as artificial ones). */
267 if (DECL_NAME (current_function_decl))
268 name = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
269 else
270 name = "";
271 printable_name = (*decl_printable_name) (current_function_decl, 2);
274 (*make_fname_decl) (get_identifier ("__FUNCTION__"), name, 0);
275 (*make_fname_decl) (get_identifier ("__PRETTY_FUNCTION__"), printable_name, 1);
276 /* The ISO C people "of course" couldn't use __FUNCTION__ in the
277 ISO C 99 standard; instead a new variable is invented. */
278 (*make_fname_decl) (get_identifier ("__func__"), name, 0);
281 /* Given a chain of STRING_CST nodes,
282 concatenate them into one STRING_CST
283 and give it a suitable array-of-chars data type. */
285 tree
286 combine_strings (strings)
287 tree strings;
289 register tree value, t;
290 register int length = 1;
291 int wide_length = 0;
292 int wide_flag = 0;
293 int wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
294 int nchars;
296 if (TREE_CHAIN (strings))
298 /* More than one in the chain, so concatenate. */
299 register char *p, *q;
301 /* Don't include the \0 at the end of each substring,
302 except for the last one.
303 Count wide strings and ordinary strings separately. */
304 for (t = strings; t; t = TREE_CHAIN (t))
306 if (TREE_TYPE (t) == wchar_array_type_node)
308 wide_length += (TREE_STRING_LENGTH (t) - wchar_bytes);
309 wide_flag = 1;
311 else
312 length += (TREE_STRING_LENGTH (t) - 1);
315 /* If anything is wide, the non-wides will be converted,
316 which makes them take more space. */
317 if (wide_flag)
318 length = length * wchar_bytes + wide_length;
320 p = ggc_alloc_string (NULL, length);
322 /* Copy the individual strings into the new combined string.
323 If the combined string is wide, convert the chars to ints
324 for any individual strings that are not wide. */
326 q = p;
327 for (t = strings; t; t = TREE_CHAIN (t))
329 int len = (TREE_STRING_LENGTH (t)
330 - ((TREE_TYPE (t) == wchar_array_type_node)
331 ? wchar_bytes : 1));
332 if ((TREE_TYPE (t) == wchar_array_type_node) == wide_flag)
334 memcpy (q, TREE_STRING_POINTER (t), len);
335 q += len;
337 else
339 int i;
340 for (i = 0; i < len; i++)
342 if (WCHAR_TYPE_SIZE == HOST_BITS_PER_SHORT)
343 ((short *) q)[i] = TREE_STRING_POINTER (t)[i];
344 else
345 ((int *) q)[i] = TREE_STRING_POINTER (t)[i];
347 q += len * wchar_bytes;
350 if (wide_flag)
352 int i;
353 for (i = 0; i < wchar_bytes; i++)
354 *q++ = 0;
356 else
357 *q = 0;
359 value = make_node (STRING_CST);
360 TREE_STRING_POINTER (value) = p;
361 TREE_STRING_LENGTH (value) = length;
363 else
365 value = strings;
366 length = TREE_STRING_LENGTH (value);
367 if (TREE_TYPE (value) == wchar_array_type_node)
368 wide_flag = 1;
371 /* Compute the number of elements, for the array type. */
372 nchars = wide_flag ? length / wchar_bytes : length;
374 /* Create the array type for the string constant.
375 -Wwrite-strings says make the string constant an array of const char
376 so that copying it to a non-const pointer will get a warning.
377 For C++, this is the standard behavior. */
378 if (flag_const_strings
379 && (! flag_traditional && ! flag_writable_strings))
381 tree elements
382 = build_type_variant (wide_flag ? wchar_type_node : char_type_node,
383 1, 0);
384 TREE_TYPE (value)
385 = build_array_type (elements,
386 build_index_type (build_int_2 (nchars - 1, 0)));
388 else
389 TREE_TYPE (value)
390 = build_array_type (wide_flag ? wchar_type_node : char_type_node,
391 build_index_type (build_int_2 (nchars - 1, 0)));
393 TREE_CONSTANT (value) = 1;
394 TREE_READONLY (value) = ! flag_writable_strings;
395 TREE_STATIC (value) = 1;
396 return value;
399 /* To speed up processing of attributes, we maintain an array of
400 IDENTIFIER_NODES and the corresponding attribute types. */
402 /* Array to hold attribute information. */
404 static struct {enum attrs id; tree name; int min, max, decl_req;} attrtab[50];
406 static int attrtab_idx = 0;
408 /* Add an entry to the attribute table above. */
410 static void
411 add_attribute (id, string, min_len, max_len, decl_req)
412 enum attrs id;
413 const char *string;
414 int min_len, max_len;
415 int decl_req;
417 char buf[100];
419 attrtab[attrtab_idx].id = id;
420 attrtab[attrtab_idx].name = get_identifier (string);
421 attrtab[attrtab_idx].min = min_len;
422 attrtab[attrtab_idx].max = max_len;
423 attrtab[attrtab_idx++].decl_req = decl_req;
425 sprintf (buf, "__%s__", string);
427 attrtab[attrtab_idx].id = id;
428 attrtab[attrtab_idx].name = get_identifier (buf);
429 attrtab[attrtab_idx].min = min_len;
430 attrtab[attrtab_idx].max = max_len;
431 attrtab[attrtab_idx++].decl_req = decl_req;
434 /* Initialize attribute table. */
436 static void
437 init_attributes ()
439 add_attribute (A_PACKED, "packed", 0, 0, 0);
440 add_attribute (A_NOCOMMON, "nocommon", 0, 0, 1);
441 add_attribute (A_COMMON, "common", 0, 0, 1);
442 add_attribute (A_NORETURN, "noreturn", 0, 0, 1);
443 add_attribute (A_NORETURN, "volatile", 0, 0, 1);
444 add_attribute (A_UNUSED, "unused", 0, 0, 0);
445 add_attribute (A_CONST, "const", 0, 0, 1);
446 add_attribute (A_T_UNION, "transparent_union", 0, 0, 0);
447 add_attribute (A_CONSTRUCTOR, "constructor", 0, 0, 1);
448 add_attribute (A_DESTRUCTOR, "destructor", 0, 0, 1);
449 add_attribute (A_MODE, "mode", 1, 1, 1);
450 add_attribute (A_SECTION, "section", 1, 1, 1);
451 add_attribute (A_ALIGNED, "aligned", 0, 1, 0);
452 add_attribute (A_FORMAT, "format", 3, 3, 1);
453 add_attribute (A_FORMAT_ARG, "format_arg", 1, 1, 1);
454 add_attribute (A_WEAK, "weak", 0, 0, 1);
455 add_attribute (A_ALIAS, "alias", 1, 1, 1);
456 add_attribute (A_NO_INSTRUMENT_FUNCTION, "no_instrument_function", 0, 0, 1);
457 add_attribute (A_NO_CHECK_MEMORY_USAGE, "no_check_memory_usage", 0, 0, 1);
458 add_attribute (A_MALLOC, "malloc", 0, 0, 1);
459 add_attribute (A_NO_LIMIT_STACK, "no_stack_limit", 0, 0, 1);
460 add_attribute (A_PURE, "pure", 0, 0, 1);
463 /* Default implementation of valid_lang_attribute, below. By default, there
464 are no language-specific attributes. */
466 static int
467 default_valid_lang_attribute (attr_name, attr_args, decl, type)
468 tree attr_name ATTRIBUTE_UNUSED;
469 tree attr_args ATTRIBUTE_UNUSED;
470 tree decl ATTRIBUTE_UNUSED;
471 tree type ATTRIBUTE_UNUSED;
473 return 0;
476 /* Return a 1 if ATTR_NAME and ATTR_ARGS denote a valid language-specific
477 attribute for either declaration DECL or type TYPE and 0 otherwise. */
479 int (*valid_lang_attribute) PARAMS ((tree, tree, tree, tree))
480 = default_valid_lang_attribute;
482 /* Process the attributes listed in ATTRIBUTES and PREFIX_ATTRIBUTES
483 and install them in NODE, which is either a DECL (including a TYPE_DECL)
484 or a TYPE. PREFIX_ATTRIBUTES can appear after the declaration specifiers
485 and declaration modifiers but before the declaration proper. */
487 void
488 decl_attributes (node, attributes, prefix_attributes)
489 tree node, attributes, prefix_attributes;
491 tree decl = 0, type = 0;
492 int is_type = 0;
493 tree a;
495 if (attrtab_idx == 0)
496 init_attributes ();
498 if (DECL_P (node))
500 decl = node;
501 type = TREE_TYPE (decl);
502 is_type = TREE_CODE (node) == TYPE_DECL;
504 else if (TYPE_P (node))
505 type = node, is_type = 1;
507 #ifdef PRAGMA_INSERT_ATTRIBUTES
508 /* If the code in c-pragma.c wants to insert some attributes then
509 allow it to do so. Do this before allowing machine back ends to
510 insert attributes, so that they have the opportunity to override
511 anything done here. */
512 PRAGMA_INSERT_ATTRIBUTES (node, & attributes, & prefix_attributes);
513 #endif
515 #ifdef INSERT_ATTRIBUTES
516 INSERT_ATTRIBUTES (node, & attributes, & prefix_attributes);
517 #endif
519 attributes = chainon (prefix_attributes, attributes);
521 for (a = attributes; a; a = TREE_CHAIN (a))
523 tree name = TREE_PURPOSE (a);
524 tree args = TREE_VALUE (a);
525 int i;
526 enum attrs id;
528 for (i = 0; i < attrtab_idx; i++)
529 if (attrtab[i].name == name)
530 break;
532 if (i == attrtab_idx)
534 if (! valid_machine_attribute (name, args, decl, type)
535 && ! (* valid_lang_attribute) (name, args, decl, type))
536 warning ("`%s' attribute directive ignored",
537 IDENTIFIER_POINTER (name));
538 else if (decl != 0)
539 type = TREE_TYPE (decl);
540 continue;
542 else if (attrtab[i].decl_req && decl == 0)
544 warning ("`%s' attribute does not apply to types",
545 IDENTIFIER_POINTER (name));
546 continue;
548 else if (list_length (args) < attrtab[i].min
549 || list_length (args) > attrtab[i].max)
551 error ("wrong number of arguments specified for `%s' attribute",
552 IDENTIFIER_POINTER (name));
553 continue;
556 id = attrtab[i].id;
557 switch (id)
559 case A_PACKED:
560 if (is_type)
561 TYPE_PACKED (type) = 1;
562 else if (TREE_CODE (decl) == FIELD_DECL)
563 DECL_PACKED (decl) = 1;
564 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
565 used for DECL_REGISTER. It wouldn't mean anything anyway. */
566 else
567 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
568 break;
570 case A_NOCOMMON:
571 if (TREE_CODE (decl) == VAR_DECL)
572 DECL_COMMON (decl) = 0;
573 else
574 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
575 break;
577 case A_COMMON:
578 if (TREE_CODE (decl) == VAR_DECL)
579 DECL_COMMON (decl) = 1;
580 else
581 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
582 break;
584 case A_NORETURN:
585 if (TREE_CODE (decl) == FUNCTION_DECL)
586 TREE_THIS_VOLATILE (decl) = 1;
587 else if (TREE_CODE (type) == POINTER_TYPE
588 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
589 TREE_TYPE (decl) = type
590 = build_pointer_type
591 (build_type_variant (TREE_TYPE (type),
592 TREE_READONLY (TREE_TYPE (type)), 1));
593 else
594 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
595 break;
597 case A_MALLOC:
598 if (TREE_CODE (decl) == FUNCTION_DECL)
599 DECL_IS_MALLOC (decl) = 1;
600 /* ??? TODO: Support types. */
601 else
602 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
603 break;
605 case A_UNUSED:
606 if (is_type)
607 if (decl)
608 TREE_USED (decl) = 1;
609 else
610 TREE_USED (type) = 1;
611 else if (TREE_CODE (decl) == PARM_DECL
612 || TREE_CODE (decl) == VAR_DECL
613 || TREE_CODE (decl) == FUNCTION_DECL
614 || TREE_CODE (decl) == LABEL_DECL)
615 TREE_USED (decl) = 1;
616 else
617 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
618 break;
620 case A_CONST:
621 if (TREE_CODE (decl) == FUNCTION_DECL)
622 TREE_READONLY (decl) = 1;
623 else if (TREE_CODE (type) == POINTER_TYPE
624 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
625 TREE_TYPE (decl) = type
626 = build_pointer_type
627 (build_type_variant (TREE_TYPE (type), 1,
628 TREE_THIS_VOLATILE (TREE_TYPE (type))));
629 else
630 warning ( "`%s' attribute ignored", IDENTIFIER_POINTER (name));
631 break;
633 case A_PURE:
634 if (TREE_CODE (decl) == FUNCTION_DECL)
635 DECL_IS_PURE (decl) = 1;
636 /* ??? TODO: Support types. */
637 else
638 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
639 break;
642 case A_T_UNION:
643 if (is_type
644 && TREE_CODE (type) == UNION_TYPE
645 && (decl == 0
646 || (TYPE_FIELDS (type) != 0
647 && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))))
648 TYPE_TRANSPARENT_UNION (type) = 1;
649 else if (decl != 0 && TREE_CODE (decl) == PARM_DECL
650 && TREE_CODE (type) == UNION_TYPE
651 && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))
652 DECL_TRANSPARENT_UNION (decl) = 1;
653 else
654 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
655 break;
657 case A_CONSTRUCTOR:
658 if (TREE_CODE (decl) == FUNCTION_DECL
659 && TREE_CODE (type) == FUNCTION_TYPE
660 && decl_function_context (decl) == 0)
662 DECL_STATIC_CONSTRUCTOR (decl) = 1;
663 TREE_USED (decl) = 1;
665 else
666 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
667 break;
669 case A_DESTRUCTOR:
670 if (TREE_CODE (decl) == FUNCTION_DECL
671 && TREE_CODE (type) == FUNCTION_TYPE
672 && decl_function_context (decl) == 0)
674 DECL_STATIC_DESTRUCTOR (decl) = 1;
675 TREE_USED (decl) = 1;
677 else
678 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
679 break;
681 case A_MODE:
682 if (TREE_CODE (TREE_VALUE (args)) != IDENTIFIER_NODE)
683 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
684 else
686 int j;
687 const char *p = IDENTIFIER_POINTER (TREE_VALUE (args));
688 int len = strlen (p);
689 enum machine_mode mode = VOIDmode;
690 tree typefm;
692 if (len > 4 && p[0] == '_' && p[1] == '_'
693 && p[len - 1] == '_' && p[len - 2] == '_')
695 char *newp = (char *) alloca (len - 1);
697 strcpy (newp, &p[2]);
698 newp[len - 4] = '\0';
699 p = newp;
702 /* Give this decl a type with the specified mode.
703 First check for the special modes. */
704 if (! strcmp (p, "byte"))
705 mode = byte_mode;
706 else if (!strcmp (p, "word"))
707 mode = word_mode;
708 else if (! strcmp (p, "pointer"))
709 mode = ptr_mode;
710 else
711 for (j = 0; j < NUM_MACHINE_MODES; j++)
712 if (!strcmp (p, GET_MODE_NAME (j)))
713 mode = (enum machine_mode) j;
715 if (mode == VOIDmode)
716 error ("unknown machine mode `%s'", p);
717 else if (0 == (typefm = type_for_mode (mode,
718 TREE_UNSIGNED (type))))
719 error ("no data type for mode `%s'", p);
720 else
722 TREE_TYPE (decl) = type = typefm;
723 DECL_SIZE (decl) = DECL_SIZE_UNIT (decl) = 0;
724 layout_decl (decl, 0);
727 break;
729 case A_SECTION:
730 #ifdef ASM_OUTPUT_SECTION_NAME
731 if ((TREE_CODE (decl) == FUNCTION_DECL
732 || TREE_CODE (decl) == VAR_DECL)
733 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
735 if (TREE_CODE (decl) == VAR_DECL
736 && current_function_decl != NULL_TREE
737 && ! TREE_STATIC (decl))
738 error_with_decl (decl,
739 "section attribute cannot be specified for local variables");
740 /* The decl may have already been given a section attribute from
741 a previous declaration. Ensure they match. */
742 else if (DECL_SECTION_NAME (decl) != NULL_TREE
743 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
744 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
745 error_with_decl (node,
746 "section of `%s' conflicts with previous declaration");
747 else
748 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
750 else
751 error_with_decl (node,
752 "section attribute not allowed for `%s'");
753 #else
754 error_with_decl (node,
755 "section attributes are not supported for this target");
756 #endif
757 break;
759 case A_ALIGNED:
761 tree align_expr
762 = (args ? TREE_VALUE (args)
763 : size_int (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
764 int i;
766 /* Strip any NOPs of any kind. */
767 while (TREE_CODE (align_expr) == NOP_EXPR
768 || TREE_CODE (align_expr) == CONVERT_EXPR
769 || TREE_CODE (align_expr) == NON_LVALUE_EXPR)
770 align_expr = TREE_OPERAND (align_expr, 0);
772 if (TREE_CODE (align_expr) != INTEGER_CST)
774 error ("requested alignment is not a constant");
775 continue;
778 if ((i = tree_log2 (align_expr)) == -1)
779 error ("requested alignment is not a power of 2");
780 else if (i > HOST_BITS_PER_INT - 2)
781 error ("requested alignment is too large");
782 else if (is_type)
783 TYPE_ALIGN (type) = (1 << i) * BITS_PER_UNIT;
784 else if (TREE_CODE (decl) != VAR_DECL
785 && TREE_CODE (decl) != FIELD_DECL)
786 error_with_decl (decl,
787 "alignment may not be specified for `%s'");
788 else
789 DECL_ALIGN (decl) = (1 << i) * BITS_PER_UNIT;
791 break;
793 case A_FORMAT:
795 tree format_type_id = TREE_VALUE (args);
796 tree format_num_expr = TREE_VALUE (TREE_CHAIN (args));
797 tree first_arg_num_expr
798 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)));
799 unsigned HOST_WIDE_INT format_num, first_arg_num;
800 enum format_type format_type;
801 tree argument;
802 unsigned int arg_num;
804 if (TREE_CODE (decl) != FUNCTION_DECL)
806 error_with_decl (decl,
807 "argument format specified for non-function `%s'");
808 continue;
811 if (TREE_CODE (format_type_id) != IDENTIFIER_NODE)
813 error ("unrecognized format specifier");
814 continue;
816 else
818 const char *p = IDENTIFIER_POINTER (format_type_id);
820 if (!strcmp (p, "printf") || !strcmp (p, "__printf__"))
821 format_type = printf_format_type;
822 else if (!strcmp (p, "scanf") || !strcmp (p, "__scanf__"))
823 format_type = scanf_format_type;
824 else if (!strcmp (p, "strftime")
825 || !strcmp (p, "__strftime__"))
826 format_type = strftime_format_type;
827 else
829 warning ("`%s' is an unrecognized format function type", p);
830 continue;
834 /* Strip any conversions from the string index and first arg number
835 and verify they are constants. */
836 while (TREE_CODE (format_num_expr) == NOP_EXPR
837 || TREE_CODE (format_num_expr) == CONVERT_EXPR
838 || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
839 format_num_expr = TREE_OPERAND (format_num_expr, 0);
841 while (TREE_CODE (first_arg_num_expr) == NOP_EXPR
842 || TREE_CODE (first_arg_num_expr) == CONVERT_EXPR
843 || TREE_CODE (first_arg_num_expr) == NON_LVALUE_EXPR)
844 first_arg_num_expr = TREE_OPERAND (first_arg_num_expr, 0);
846 if (TREE_CODE (format_num_expr) != INTEGER_CST
847 || TREE_INT_CST_HIGH (format_num_expr) != 0
848 || TREE_CODE (first_arg_num_expr) != INTEGER_CST
849 || TREE_INT_CST_HIGH (first_arg_num_expr) != 0)
851 error ("format string has invalid operand number");
852 continue;
855 format_num = TREE_INT_CST_LOW (format_num_expr);
856 first_arg_num = TREE_INT_CST_LOW (first_arg_num_expr);
857 if (first_arg_num != 0 && first_arg_num <= format_num)
859 error ("format string arg follows the args to be formatted");
860 continue;
863 /* If a parameter list is specified, verify that the format_num
864 argument is actually a string, in case the format attribute
865 is in error. */
866 argument = TYPE_ARG_TYPES (type);
867 if (argument)
869 for (arg_num = 1; argument != 0 && arg_num != format_num;
870 ++arg_num, argument = TREE_CHAIN (argument))
873 if (! argument
874 || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
875 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
876 != char_type_node))
878 error ("format string arg not a string type");
879 continue;
882 else if (first_arg_num != 0)
884 /* Verify that first_arg_num points to the last arg,
885 the ... */
886 while (argument)
887 arg_num++, argument = TREE_CHAIN (argument);
889 if (arg_num != first_arg_num)
891 error ("args to be formatted is not '...'");
892 continue;
897 record_function_format (DECL_NAME (decl),
898 DECL_ASSEMBLER_NAME (decl),
899 format_type, format_num, first_arg_num);
900 break;
903 case A_FORMAT_ARG:
905 tree format_num_expr = TREE_VALUE (args);
906 unsigned HOST_WIDE_INT format_num;
907 unsigned int arg_num;
908 tree argument;
910 if (TREE_CODE (decl) != FUNCTION_DECL)
912 error_with_decl (decl,
913 "argument format specified for non-function `%s'");
914 continue;
917 /* Strip any conversions from the first arg number and verify it
918 is a constant. */
919 while (TREE_CODE (format_num_expr) == NOP_EXPR
920 || TREE_CODE (format_num_expr) == CONVERT_EXPR
921 || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
922 format_num_expr = TREE_OPERAND (format_num_expr, 0);
924 if (TREE_CODE (format_num_expr) != INTEGER_CST
925 || TREE_INT_CST_HIGH (format_num_expr) != 0)
927 error ("format string has invalid operand number");
928 continue;
931 format_num = TREE_INT_CST_LOW (format_num_expr);
933 /* If a parameter list is specified, verify that the format_num
934 argument is actually a string, in case the format attribute
935 is in error. */
936 argument = TYPE_ARG_TYPES (type);
937 if (argument)
939 for (arg_num = 1; argument != 0 && arg_num != format_num;
940 ++arg_num, argument = TREE_CHAIN (argument))
943 if (! argument
944 || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
945 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
946 != char_type_node))
948 error ("format string arg not a string type");
949 continue;
953 if (TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) != POINTER_TYPE
954 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (TREE_TYPE (decl))))
955 != char_type_node))
957 error ("function does not return string type");
958 continue;
961 record_international_format (DECL_NAME (decl),
962 DECL_ASSEMBLER_NAME (decl),
963 format_num);
964 break;
967 case A_WEAK:
968 declare_weak (decl);
969 break;
971 case A_ALIAS:
972 if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
973 || (TREE_CODE (decl) != FUNCTION_DECL && ! DECL_EXTERNAL (decl)))
974 error_with_decl (decl,
975 "`%s' defined both normally and as an alias");
976 else if (decl_function_context (decl) == 0)
978 tree id;
980 id = TREE_VALUE (args);
981 if (TREE_CODE (id) != STRING_CST)
983 error ("alias arg not a string");
984 break;
986 id = get_identifier (TREE_STRING_POINTER (id));
987 /* This counts as a use of the object pointed to. */
988 TREE_USED (id) = 1;
990 if (TREE_CODE (decl) == FUNCTION_DECL)
991 DECL_INITIAL (decl) = error_mark_node;
992 else
993 DECL_EXTERNAL (decl) = 0;
994 assemble_alias (decl, id);
996 else
997 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
998 break;
1000 case A_NO_CHECK_MEMORY_USAGE:
1001 if (TREE_CODE (decl) != FUNCTION_DECL)
1003 error_with_decl (decl,
1004 "`%s' attribute applies only to functions",
1005 IDENTIFIER_POINTER (name));
1007 else if (DECL_INITIAL (decl))
1009 error_with_decl (decl,
1010 "can't set `%s' attribute after definition",
1011 IDENTIFIER_POINTER (name));
1013 else
1014 DECL_NO_CHECK_MEMORY_USAGE (decl) = 1;
1015 break;
1017 case A_NO_INSTRUMENT_FUNCTION:
1018 if (TREE_CODE (decl) != FUNCTION_DECL)
1020 error_with_decl (decl,
1021 "`%s' attribute applies only to functions",
1022 IDENTIFIER_POINTER (name));
1024 else if (DECL_INITIAL (decl))
1026 error_with_decl (decl,
1027 "can't set `%s' attribute after definition",
1028 IDENTIFIER_POINTER (name));
1030 else
1031 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
1032 break;
1034 case A_NO_LIMIT_STACK:
1035 if (TREE_CODE (decl) != FUNCTION_DECL)
1037 error_with_decl (decl,
1038 "`%s' attribute applies only to functions",
1039 IDENTIFIER_POINTER (name));
1041 else if (DECL_INITIAL (decl))
1043 error_with_decl (decl,
1044 "can't set `%s' attribute after definition",
1045 IDENTIFIER_POINTER (name));
1047 else
1048 DECL_NO_LIMIT_STACK (decl) = 1;
1049 break;
1054 /* Split SPECS_ATTRS, a list of declspecs and prefix attributes, into two
1055 lists. SPECS_ATTRS may also be just a typespec (eg: RECORD_TYPE).
1057 The head of the declspec list is stored in DECLSPECS.
1058 The head of the attribute list is stored in PREFIX_ATTRIBUTES.
1060 Note that attributes in SPECS_ATTRS are stored in the TREE_PURPOSE of
1061 the list elements. We drop the containing TREE_LIST nodes and link the
1062 resulting attributes together the way decl_attributes expects them. */
1064 void
1065 split_specs_attrs (specs_attrs, declspecs, prefix_attributes)
1066 tree specs_attrs;
1067 tree *declspecs, *prefix_attributes;
1069 tree t, s, a, next, specs, attrs;
1071 /* This can happen after an __extension__ in pedantic mode. */
1072 if (specs_attrs != NULL_TREE
1073 && TREE_CODE (specs_attrs) == INTEGER_CST)
1075 *declspecs = NULL_TREE;
1076 *prefix_attributes = NULL_TREE;
1077 return;
1080 /* This can happen in c++ (eg: decl: typespec initdecls ';'). */
1081 if (specs_attrs != NULL_TREE
1082 && TREE_CODE (specs_attrs) != TREE_LIST)
1084 *declspecs = specs_attrs;
1085 *prefix_attributes = NULL_TREE;
1086 return;
1089 /* Remember to keep the lists in the same order, element-wise. */
1091 specs = s = NULL_TREE;
1092 attrs = a = NULL_TREE;
1093 for (t = specs_attrs; t; t = next)
1095 next = TREE_CHAIN (t);
1096 /* Declspecs have a non-NULL TREE_VALUE. */
1097 if (TREE_VALUE (t) != NULL_TREE)
1099 if (specs == NULL_TREE)
1100 specs = s = t;
1101 else
1103 TREE_CHAIN (s) = t;
1104 s = t;
1107 else
1109 if (attrs == NULL_TREE)
1110 attrs = a = TREE_PURPOSE (t);
1111 else
1113 TREE_CHAIN (a) = TREE_PURPOSE (t);
1114 a = TREE_PURPOSE (t);
1116 /* More attrs can be linked here, move A to the end. */
1117 while (TREE_CHAIN (a) != NULL_TREE)
1118 a = TREE_CHAIN (a);
1122 /* Terminate the lists. */
1123 if (s != NULL_TREE)
1124 TREE_CHAIN (s) = NULL_TREE;
1125 if (a != NULL_TREE)
1126 TREE_CHAIN (a) = NULL_TREE;
1128 /* All done. */
1129 *declspecs = specs;
1130 *prefix_attributes = attrs;
1133 /* Strip attributes from SPECS_ATTRS, a list of declspecs and attributes.
1134 This function is used by the parser when a rule will accept attributes
1135 in a particular position, but we don't want to support that just yet.
1137 A warning is issued for every ignored attribute. */
1139 tree
1140 strip_attrs (specs_attrs)
1141 tree specs_attrs;
1143 tree specs, attrs;
1145 split_specs_attrs (specs_attrs, &specs, &attrs);
1147 while (attrs)
1149 warning ("`%s' attribute ignored",
1150 IDENTIFIER_POINTER (TREE_PURPOSE (attrs)));
1151 attrs = TREE_CHAIN (attrs);
1154 return specs;
1157 /* Check a printf/fprintf/sprintf/scanf/fscanf/sscanf format against
1158 a parameter list. */
1160 #define T_I &integer_type_node
1161 #define T_L &long_integer_type_node
1162 #define T_LL &long_long_integer_type_node
1163 #define T_S &short_integer_type_node
1164 #define T_UI &unsigned_type_node
1165 #define T_UL &long_unsigned_type_node
1166 #define T_ULL &long_long_unsigned_type_node
1167 #define T_US &short_unsigned_type_node
1168 #define T_F &float_type_node
1169 #define T_D &double_type_node
1170 #define T_LD &long_double_type_node
1171 #define T_C &char_type_node
1172 #define T_UC &unsigned_char_type_node
1173 #define T_V &void_type_node
1174 #define T_W &wchar_type_node
1175 #define T_ST &sizetype
1177 typedef struct {
1178 const char *format_chars;
1179 int pointer_count;
1180 /* Type of argument if no length modifier is used. */
1181 tree *nolen;
1182 /* Type of argument if length modifier for shortening to byte is used.
1183 If NULL, then this modifier is not allowed. */
1184 tree *hhlen;
1185 /* Type of argument if length modifier for shortening is used.
1186 If NULL, then this modifier is not allowed. */
1187 tree *hlen;
1188 /* Type of argument if length modifier `l' is used.
1189 If NULL, then this modifier is not allowed. */
1190 tree *llen;
1191 /* Type of argument if length modifier `q' or `ll' is used.
1192 If NULL, then this modifier is not allowed. */
1193 tree *qlen;
1194 /* Type of argument if length modifier `L' is used.
1195 If NULL, then this modifier is not allowed. */
1196 tree *bigllen;
1197 /* Type of argument if length modifiers 'z' or `Z' is used.
1198 If NULL, then this modifier is not allowed. */
1199 tree *zlen;
1200 /* List of other modifier characters allowed with these options. */
1201 const char *flag_chars;
1202 } format_char_info;
1204 static format_char_info print_char_table[] = {
1205 { "di", 0, T_I, T_I, T_I, T_L, T_LL, T_LL, T_ST, "-wp0 +" },
1206 { "oxX", 0, T_UI, T_UI, T_UI, T_UL, T_ULL, T_ULL, T_ST, "-wp0#" },
1207 { "u", 0, T_UI, T_UI, T_UI, T_UL, T_ULL, T_ULL, T_ST, "-wp0" },
1208 /* A GNU extension. */
1209 { "m", 0, T_V, NULL, NULL, NULL, NULL, NULL, NULL, "-wp" },
1210 { "feEgGaA", 0, T_D, NULL, NULL, NULL, NULL, T_LD, NULL, "-wp0 +#" },
1211 { "c", 0, T_I, NULL, NULL, T_W, NULL, NULL, NULL, "-w" },
1212 { "C", 0, T_W, NULL, NULL, NULL, NULL, NULL, NULL, "-w" },
1213 { "s", 1, T_C, NULL, NULL, T_W, NULL, NULL, NULL, "-wp" },
1214 { "S", 1, T_W, NULL, NULL, NULL, NULL, NULL, NULL, "-wp" },
1215 { "p", 1, T_V, NULL, NULL, NULL, NULL, NULL, NULL, "-w" },
1216 { "n", 1, T_I, NULL, T_S, T_L, T_LL, NULL, NULL, "" },
1217 { NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
1220 static format_char_info scan_char_table[] = {
1221 { "di", 1, T_I, T_C, T_S, T_L, T_LL, T_LL, NULL, "*" },
1222 { "ouxX", 1, T_UI, T_UC, T_US, T_UL, T_ULL, T_ULL, NULL, "*" },
1223 { "efgEGaA", 1, T_F, NULL, NULL, T_D, NULL, T_LD, NULL, "*" },
1224 { "c", 1, T_C, NULL, NULL, T_W, NULL, NULL, NULL, "*" },
1225 { "s", 1, T_C, NULL, NULL, T_W, NULL, NULL, NULL, "*a" },
1226 { "[", 1, T_C, NULL, NULL, NULL, NULL, NULL, NULL, "*a" },
1227 { "C", 1, T_W, NULL, NULL, NULL, NULL, NULL, NULL, "*" },
1228 { "S", 1, T_W, NULL, NULL, NULL, NULL, NULL, NULL, "*a" },
1229 { "p", 2, T_V, NULL, NULL, NULL, NULL, NULL, NULL, "*" },
1230 { "n", 1, T_I, T_C, T_S, T_L, T_LL, NULL, NULL, "" },
1231 { NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
1234 /* Handle format characters recognized by glibc's strftime.c.
1235 '2' - MUST do years as only two digits
1236 '3' - MAY do years as only two digits (depending on locale)
1237 'E' - E modifier is acceptable
1238 'O' - O modifier is acceptable to Standard C
1239 'o' - O modifier is acceptable as a GNU extension
1240 'G' - other GNU extensions */
1242 static format_char_info time_char_table[] = {
1243 { "y", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "2EO-_0w" },
1244 { "D", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "2" },
1245 { "g", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "2O-_0w" },
1246 { "cx", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "3E" },
1247 { "%RTXnrt", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "" },
1248 { "P", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "G" },
1249 { "HIMSUWdemw", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0Ow" },
1250 { "Vju", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0Oow" },
1251 { "Gklsz", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0OGw" },
1252 { "ABZa", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "^#" },
1253 { "p", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "#" },
1254 { "bh", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "^" },
1255 { "CY", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0EOw" },
1256 { NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
1259 typedef struct function_format_info
1261 struct function_format_info *next; /* next structure on the list */
1262 tree name; /* identifier such as "printf" */
1263 tree assembler_name; /* optional mangled identifier (for C++) */
1264 enum format_type format_type; /* type of format (printf, scanf, etc.) */
1265 int format_num; /* number of format argument */
1266 int first_arg_num; /* number of first arg (zero for varargs) */
1267 } function_format_info;
1269 static function_format_info *function_format_list = NULL;
1271 typedef struct international_format_info
1273 struct international_format_info *next; /* next structure on the list */
1274 tree name; /* identifier such as "gettext" */
1275 tree assembler_name; /* optional mangled identifier (for C++) */
1276 int format_num; /* number of format argument */
1277 } international_format_info;
1279 static international_format_info *international_format_list = NULL;
1281 static void check_format_info PARAMS ((function_format_info *, tree));
1283 /* Initialize the table of functions to perform format checking on.
1284 The ANSI functions are always checked (whether <stdio.h> is
1285 included or not), since it is common to call printf without
1286 including <stdio.h>. There shouldn't be a problem with this,
1287 since ANSI reserves these function names whether you include the
1288 header file or not. In any case, the checking is harmless.
1290 Also initialize the name of function that modify the format string for
1291 internationalization purposes. */
1293 void
1294 init_function_format_info ()
1296 record_function_format (get_identifier ("printf"), NULL_TREE,
1297 printf_format_type, 1, 2);
1298 record_function_format (get_identifier ("fprintf"), NULL_TREE,
1299 printf_format_type, 2, 3);
1300 record_function_format (get_identifier ("sprintf"), NULL_TREE,
1301 printf_format_type, 2, 3);
1302 record_function_format (get_identifier ("scanf"), NULL_TREE,
1303 scanf_format_type, 1, 2);
1304 record_function_format (get_identifier ("fscanf"), NULL_TREE,
1305 scanf_format_type, 2, 3);
1306 record_function_format (get_identifier ("sscanf"), NULL_TREE,
1307 scanf_format_type, 2, 3);
1308 record_function_format (get_identifier ("vprintf"), NULL_TREE,
1309 printf_format_type, 1, 0);
1310 record_function_format (get_identifier ("vfprintf"), NULL_TREE,
1311 printf_format_type, 2, 0);
1312 record_function_format (get_identifier ("vsprintf"), NULL_TREE,
1313 printf_format_type, 2, 0);
1314 record_function_format (get_identifier ("strftime"), NULL_TREE,
1315 strftime_format_type, 3, 0);
1317 record_international_format (get_identifier ("gettext"), NULL_TREE, 1);
1318 record_international_format (get_identifier ("dgettext"), NULL_TREE, 2);
1319 record_international_format (get_identifier ("dcgettext"), NULL_TREE, 2);
1322 /* Record information for argument format checking. FUNCTION_IDENT is
1323 the identifier node for the name of the function to check (its decl
1324 need not exist yet).
1325 FORMAT_TYPE specifies the type of format checking. FORMAT_NUM is the number
1326 of the argument which is the format control string (starting from 1).
1327 FIRST_ARG_NUM is the number of the first actual argument to check
1328 against the format string, or zero if no checking is not be done
1329 (e.g. for varargs such as vfprintf). */
1331 static void
1332 record_function_format (name, assembler_name, format_type,
1333 format_num, first_arg_num)
1334 tree name;
1335 tree assembler_name;
1336 enum format_type format_type;
1337 int format_num;
1338 int first_arg_num;
1340 function_format_info *info;
1342 /* Re-use existing structure if it's there. */
1344 for (info = function_format_list; info; info = info->next)
1346 if (info->name == name && info->assembler_name == assembler_name)
1347 break;
1349 if (! info)
1351 info = (function_format_info *) xmalloc (sizeof (function_format_info));
1352 info->next = function_format_list;
1353 function_format_list = info;
1355 info->name = name;
1356 info->assembler_name = assembler_name;
1359 info->format_type = format_type;
1360 info->format_num = format_num;
1361 info->first_arg_num = first_arg_num;
1364 /* Record information for the names of function that modify the format
1365 argument to format functions. FUNCTION_IDENT is the identifier node for
1366 the name of the function (its decl need not exist yet) and FORMAT_NUM is
1367 the number of the argument which is the format control string (starting
1368 from 1). */
1370 static void
1371 record_international_format (name, assembler_name, format_num)
1372 tree name;
1373 tree assembler_name;
1374 int format_num;
1376 international_format_info *info;
1378 /* Re-use existing structure if it's there. */
1380 for (info = international_format_list; info; info = info->next)
1382 if (info->name == name && info->assembler_name == assembler_name)
1383 break;
1386 if (! info)
1388 info
1389 = (international_format_info *)
1390 xmalloc (sizeof (international_format_info));
1391 info->next = international_format_list;
1392 international_format_list = info;
1394 info->name = name;
1395 info->assembler_name = assembler_name;
1398 info->format_num = format_num;
1401 static void
1402 tfaff ()
1404 warning ("too few arguments for format");
1407 /* Check the argument list of a call to printf, scanf, etc.
1408 NAME is the function identifier.
1409 ASSEMBLER_NAME is the function's assembler identifier.
1410 (Either NAME or ASSEMBLER_NAME, but not both, may be NULL_TREE.)
1411 PARAMS is the list of argument values. */
1413 void
1414 check_function_format (name, assembler_name, params)
1415 tree name;
1416 tree assembler_name;
1417 tree params;
1419 function_format_info *info;
1421 /* See if this function is a format function. */
1422 for (info = function_format_list; info; info = info->next)
1424 if (info->assembler_name
1425 ? (info->assembler_name == assembler_name)
1426 : (info->name == name))
1428 /* Yup; check it. */
1429 check_format_info (info, params);
1430 break;
1435 /* Check the argument list of a call to printf, scanf, etc.
1436 INFO points to the function_format_info structure.
1437 PARAMS is the list of argument values. */
1439 static void
1440 check_format_info (info, params)
1441 function_format_info *info;
1442 tree params;
1444 int i;
1445 int arg_num;
1446 int suppressed, wide, precise;
1447 int length_char = 0;
1448 int format_char;
1449 int format_length;
1450 tree format_tree;
1451 tree cur_param;
1452 tree cur_type;
1453 tree wanted_type;
1454 tree first_fillin_param;
1455 const char *format_chars;
1456 format_char_info *fci = NULL;
1457 char flag_chars[8];
1458 int has_operand_number = 0;
1460 /* Skip to format argument. If the argument isn't available, there's
1461 no work for us to do; prototype checking will catch the problem. */
1462 for (arg_num = 1; ; ++arg_num)
1464 if (params == 0)
1465 return;
1466 if (arg_num == info->format_num)
1467 break;
1468 params = TREE_CHAIN (params);
1470 format_tree = TREE_VALUE (params);
1471 params = TREE_CHAIN (params);
1472 if (format_tree == 0)
1473 return;
1475 /* We can only check the format if it's a string constant. */
1476 while (TREE_CODE (format_tree) == NOP_EXPR)
1477 format_tree = TREE_OPERAND (format_tree, 0); /* strip coercion */
1479 if (TREE_CODE (format_tree) == CALL_EXPR
1480 && TREE_CODE (TREE_OPERAND (format_tree, 0)) == ADDR_EXPR
1481 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (format_tree, 0), 0))
1482 == FUNCTION_DECL))
1484 tree function = TREE_OPERAND (TREE_OPERAND (format_tree, 0), 0);
1486 /* See if this is a call to a known internationalization function
1487 that modifies the format arg. */
1488 international_format_info *info;
1490 for (info = international_format_list; info; info = info->next)
1491 if (info->assembler_name
1492 ? (info->assembler_name == DECL_ASSEMBLER_NAME (function))
1493 : (info->name == DECL_NAME (function)))
1495 tree inner_args;
1496 int i;
1498 for (inner_args = TREE_OPERAND (format_tree, 1), i = 1;
1499 inner_args != 0;
1500 inner_args = TREE_CHAIN (inner_args), i++)
1501 if (i == info->format_num)
1503 format_tree = TREE_VALUE (inner_args);
1505 while (TREE_CODE (format_tree) == NOP_EXPR)
1506 format_tree = TREE_OPERAND (format_tree, 0);
1511 if (integer_zerop (format_tree))
1513 warning ("null format string");
1514 return;
1516 if (TREE_CODE (format_tree) != ADDR_EXPR)
1518 /* The user may get multiple warnings if the supplied argument
1519 isn't even a string pointer. */
1520 /* Functions taking a va_list normally pass a non-literal format
1521 string. These functions typically are declared with
1522 first_arg_num == 0, so avoid warning in those cases. */
1523 if (info->first_arg_num != 0 && warn_format > 1)
1524 warning ("format not a string literal, argument types not checked");
1525 return;
1527 format_tree = TREE_OPERAND (format_tree, 0);
1528 if (TREE_CODE (format_tree) != STRING_CST)
1530 /* The user may get multiple warnings if the supplied argument
1531 isn't even a string pointer. */
1532 /* Functions taking a va_list normally pass a non-literal format
1533 string. These functions typically are declared with
1534 first_arg_num == 0, so avoid warning in those cases. */
1535 if (info->first_arg_num != 0 && warn_format > 1)
1536 warning ("format not a string literal, argument types not checked");
1537 return;
1539 format_chars = TREE_STRING_POINTER (format_tree);
1540 format_length = TREE_STRING_LENGTH (format_tree);
1541 if (format_length <= 1)
1542 warning ("zero-length format string");
1543 if (format_chars[--format_length] != 0)
1545 warning ("unterminated format string");
1546 return;
1548 /* Skip to first argument to check. */
1549 while (arg_num + 1 < info->first_arg_num)
1551 if (params == 0)
1552 return;
1553 params = TREE_CHAIN (params);
1554 ++arg_num;
1557 first_fillin_param = params;
1558 while (1)
1560 int aflag;
1561 if (*format_chars == 0)
1563 if (format_chars - TREE_STRING_POINTER (format_tree) != format_length)
1564 warning ("embedded `\\0' in format");
1565 if (info->first_arg_num != 0 && params != 0 && ! has_operand_number)
1566 warning ("too many arguments for format");
1567 return;
1569 if (*format_chars++ != '%')
1570 continue;
1571 if (*format_chars == 0)
1573 warning ("spurious trailing `%%' in format");
1574 continue;
1576 if (*format_chars == '%')
1578 ++format_chars;
1579 continue;
1581 flag_chars[0] = 0;
1582 suppressed = wide = precise = FALSE;
1583 if (info->format_type == scanf_format_type)
1585 suppressed = *format_chars == '*';
1586 if (suppressed)
1587 ++format_chars;
1588 while (ISDIGIT (*format_chars))
1589 ++format_chars;
1591 else if (info->format_type == strftime_format_type)
1593 while (*format_chars != 0 && index ("_-0^#", *format_chars) != 0)
1595 if (pedantic)
1596 warning ("ANSI C does not support the strftime `%c' flag",
1597 *format_chars);
1598 if (index (flag_chars, *format_chars) != 0)
1600 warning ("repeated `%c' flag in format",
1601 *format_chars);
1602 ++format_chars;
1604 else
1606 i = strlen (flag_chars);
1607 flag_chars[i++] = *format_chars++;
1608 flag_chars[i] = 0;
1611 while (ISDIGIT ((unsigned char) *format_chars))
1613 wide = TRUE;
1614 ++format_chars;
1616 if (wide && pedantic)
1617 warning ("ANSI C does not support strftime format width");
1618 if (*format_chars == 'E' || *format_chars == 'O')
1620 i = strlen (flag_chars);
1621 flag_chars[i++] = *format_chars++;
1622 flag_chars[i] = 0;
1623 if (*format_chars == 'E' || *format_chars == 'O')
1625 warning ("multiple E/O modifiers in format");
1626 while (*format_chars == 'E' || *format_chars == 'O')
1627 ++format_chars;
1631 else if (info->format_type == printf_format_type)
1633 /* See if we have a number followed by a dollar sign. If we do,
1634 it is an operand number, so set PARAMS to that operand. */
1635 if (*format_chars >= '0' && *format_chars <= '9')
1637 const char *p = format_chars;
1639 while (*p >= '0' && *p++ <= '9')
1642 if (*p == '$')
1644 int opnum = atoi (format_chars);
1646 params = first_fillin_param;
1647 format_chars = p + 1;
1648 has_operand_number = 1;
1650 for (i = 1; i < opnum && params != 0; i++)
1651 params = TREE_CHAIN (params);
1653 if (opnum == 0 || params == 0)
1655 warning ("operand number out of range in format");
1656 return;
1661 while (*format_chars != 0 && index (" +#0-", *format_chars) != 0)
1663 if (index (flag_chars, *format_chars) != 0)
1664 warning ("repeated `%c' flag in format", *format_chars++);
1665 else
1667 i = strlen (flag_chars);
1668 flag_chars[i++] = *format_chars++;
1669 flag_chars[i] = 0;
1672 /* "If the space and + flags both appear,
1673 the space flag will be ignored." */
1674 if (index (flag_chars, ' ') != 0
1675 && index (flag_chars, '+') != 0)
1676 warning ("use of both ` ' and `+' flags in format");
1677 /* "If the 0 and - flags both appear,
1678 the 0 flag will be ignored." */
1679 if (index (flag_chars, '0') != 0
1680 && index (flag_chars, '-') != 0)
1681 warning ("use of both `0' and `-' flags in format");
1682 if (*format_chars == '*')
1684 wide = TRUE;
1685 /* "...a field width...may be indicated by an asterisk.
1686 In this case, an int argument supplies the field width..." */
1687 ++format_chars;
1688 if (params == 0)
1690 tfaff ();
1691 return;
1693 if (info->first_arg_num != 0)
1695 cur_param = TREE_VALUE (params);
1696 params = TREE_CHAIN (params);
1697 ++arg_num;
1698 /* size_t is generally not valid here.
1699 It will work on most machines, because size_t and int
1700 have the same mode. But might as well warn anyway,
1701 since it will fail on other machines. */
1702 if ((TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1703 != integer_type_node)
1705 (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1706 != unsigned_type_node))
1707 warning ("field width is not type int (arg %d)", arg_num);
1710 else
1712 while (ISDIGIT (*format_chars))
1714 wide = TRUE;
1715 ++format_chars;
1718 if (*format_chars == '.')
1720 precise = TRUE;
1721 ++format_chars;
1722 if (*format_chars != '*' && !ISDIGIT (*format_chars))
1723 warning ("`.' not followed by `*' or digit in format");
1724 /* "...a...precision...may be indicated by an asterisk.
1725 In this case, an int argument supplies the...precision." */
1726 if (*format_chars == '*')
1728 if (info->first_arg_num != 0)
1730 ++format_chars;
1731 if (params == 0)
1733 tfaff ();
1734 return;
1736 cur_param = TREE_VALUE (params);
1737 params = TREE_CHAIN (params);
1738 ++arg_num;
1739 if (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1740 != integer_type_node)
1741 warning ("field width is not type int (arg %d)",
1742 arg_num);
1745 else
1747 while (ISDIGIT (*format_chars))
1748 ++format_chars;
1753 aflag = 0;
1755 if (info->format_type != strftime_format_type)
1757 if (*format_chars == 'h' || *format_chars == 'l')
1758 length_char = *format_chars++;
1759 else if (*format_chars == 'q' || *format_chars == 'L')
1761 length_char = *format_chars++;
1762 if (pedantic)
1763 warning ("ANSI C does not support the `%c' length modifier",
1764 length_char);
1766 else if (*format_chars == 'Z' || *format_chars == 'z')
1768 length_char = *format_chars++;
1769 if (pedantic && (length_char == 'Z' || !flag_isoc99))
1770 warning ("ANSI C does not support the `%c' length modifier",
1771 length_char);
1773 else
1774 length_char = 0;
1775 if (length_char == 'l' && *format_chars == 'l')
1777 length_char = 'q', format_chars++;
1778 if (pedantic && !flag_isoc99)
1779 warning ("ANSI C does not support the `ll' length modifier");
1781 else if (length_char == 'h' && *format_chars == 'h')
1783 length_char = 'H', format_chars++;
1784 if (pedantic && !flag_isoc99)
1785 warning ("ANSI C does not support the `hh' length modifier");
1787 if (*format_chars == 'a' && info->format_type == scanf_format_type)
1789 if (format_chars[1] == 's' || format_chars[1] == 'S'
1790 || format_chars[1] == '[')
1792 /* `a' is used as a flag. */
1793 aflag = 1;
1794 format_chars++;
1797 if (suppressed && length_char != 0)
1798 warning ("use of `*' and `%c' together in format", length_char);
1800 format_char = *format_chars;
1801 if (format_char == 0
1802 || (info->format_type != strftime_format_type && format_char == '%'))
1804 warning ("conversion lacks type at end of format");
1805 continue;
1807 /* The m, C, and S formats are GNU extensions. */
1808 if (pedantic && info->format_type != strftime_format_type
1809 && (format_char == 'm' || format_char == 'C' || format_char == 'S'))
1810 warning ("ANSI C does not support the `%c' format", format_char);
1811 /* The a and A formats are C99 extensions. */
1812 if (pedantic && info->format_type != strftime_format_type
1813 && (format_char == 'a' || format_char == 'A')
1814 && !flag_isoc99)
1815 warning ("ANSI C does not support the `%c' format", format_char);
1816 format_chars++;
1817 switch (info->format_type)
1819 case printf_format_type:
1820 fci = print_char_table;
1821 break;
1822 case scanf_format_type:
1823 fci = scan_char_table;
1824 break;
1825 case strftime_format_type:
1826 fci = time_char_table;
1827 break;
1828 default:
1829 abort ();
1831 while (fci->format_chars != 0
1832 && index (fci->format_chars, format_char) == 0)
1833 ++fci;
1834 if (fci->format_chars == 0)
1836 if (ISGRAPH(format_char))
1837 warning ("unknown conversion type character `%c' in format",
1838 format_char);
1839 else
1840 warning ("unknown conversion type character 0x%x in format",
1841 format_char);
1842 continue;
1844 if (pedantic)
1846 if (index (fci->flag_chars, 'G') != 0)
1847 warning ("ANSI C does not support `%%%c'", format_char);
1848 if (index (fci->flag_chars, 'o') != 0
1849 && index (flag_chars, 'O') != 0)
1850 warning ("ANSI C does not support `%%O%c'", format_char);
1852 if (wide && index (fci->flag_chars, 'w') == 0)
1853 warning ("width used with `%c' format", format_char);
1854 if (index (fci->flag_chars, '2') != 0)
1855 warning ("`%%%c' yields only last 2 digits of year", format_char);
1856 else if (index (fci->flag_chars, '3') != 0)
1857 warning ("`%%%c' yields only last 2 digits of year in some locales",
1858 format_char);
1859 if (precise && index (fci->flag_chars, 'p') == 0)
1860 warning ("precision used with `%c' format", format_char);
1861 if (aflag && index (fci->flag_chars, 'a') == 0)
1863 warning ("`a' flag used with `%c' format", format_char);
1864 /* To simplify the following code. */
1865 aflag = 0;
1867 /* The a flag is a GNU extension. */
1868 else if (pedantic && aflag)
1869 warning ("ANSI C does not support the `a' flag");
1870 if (info->format_type == scanf_format_type && format_char == '[')
1872 /* Skip over scan set, in case it happens to have '%' in it. */
1873 if (*format_chars == '^')
1874 ++format_chars;
1875 /* Find closing bracket; if one is hit immediately, then
1876 it's part of the scan set rather than a terminator. */
1877 if (*format_chars == ']')
1878 ++format_chars;
1879 while (*format_chars && *format_chars != ']')
1880 ++format_chars;
1881 if (*format_chars != ']')
1882 /* The end of the format string was reached. */
1883 warning ("no closing `]' for `%%[' format");
1885 if (suppressed)
1887 if (index (fci->flag_chars, '*') == 0)
1888 warning ("suppression of `%c' conversion in format", format_char);
1889 continue;
1891 for (i = 0; flag_chars[i] != 0; ++i)
1893 if (index (fci->flag_chars, flag_chars[i]) == 0)
1894 warning ("flag `%c' used with type `%c'",
1895 flag_chars[i], format_char);
1897 if (info->format_type == strftime_format_type)
1898 continue;
1899 if (precise && index (flag_chars, '0') != 0
1900 && (format_char == 'd' || format_char == 'i'
1901 || format_char == 'o' || format_char == 'u'
1902 || format_char == 'x' || format_char == 'X'))
1903 warning ("`0' flag ignored with precision specifier and `%c' format",
1904 format_char);
1905 switch (length_char)
1907 default: wanted_type = fci->nolen ? *(fci->nolen) : 0; break;
1908 case 'H': wanted_type = fci->hhlen ? *(fci->hhlen) : 0; break;
1909 case 'h': wanted_type = fci->hlen ? *(fci->hlen) : 0; break;
1910 case 'l': wanted_type = fci->llen ? *(fci->llen) : 0; break;
1911 case 'q': wanted_type = fci->qlen ? *(fci->qlen) : 0; break;
1912 case 'L': wanted_type = fci->bigllen ? *(fci->bigllen) : 0; break;
1913 case 'z': case 'Z': wanted_type = fci->zlen ? *fci->zlen : 0; break;
1915 if (wanted_type == 0)
1916 warning ("use of `%c' length character with `%c' type character",
1917 length_char, format_char);
1919 /* Finally. . .check type of argument against desired type! */
1920 if (info->first_arg_num == 0)
1921 continue;
1922 if (fci->pointer_count == 0 && wanted_type == void_type_node)
1923 /* This specifier takes no argument. */
1924 continue;
1925 if (params == 0)
1927 tfaff ();
1928 return;
1930 cur_param = TREE_VALUE (params);
1931 params = TREE_CHAIN (params);
1932 ++arg_num;
1933 cur_type = TREE_TYPE (cur_param);
1935 STRIP_NOPS (cur_param);
1937 /* Check the types of any additional pointer arguments
1938 that precede the "real" argument. */
1939 for (i = 0; i < fci->pointer_count + aflag; ++i)
1941 if (TREE_CODE (cur_type) == POINTER_TYPE)
1943 cur_type = TREE_TYPE (cur_type);
1945 if (cur_param != 0 && TREE_CODE (cur_param) == ADDR_EXPR)
1946 cur_param = TREE_OPERAND (cur_param, 0);
1947 else
1948 cur_param = 0;
1950 continue;
1952 if (TREE_CODE (cur_type) != ERROR_MARK)
1954 if (fci->pointer_count + aflag == 1)
1955 warning ("format argument is not a pointer (arg %d)", arg_num);
1956 else
1957 warning ("format argument is not a pointer to a pointer (arg %d)", arg_num);
1959 break;
1962 /* See if this is an attempt to write into a const type with
1963 scanf or with printf "%n". */
1964 if ((info->format_type == scanf_format_type
1965 || (info->format_type == printf_format_type
1966 && format_char == 'n'))
1967 && i == fci->pointer_count + aflag
1968 && wanted_type != 0
1969 && TREE_CODE (cur_type) != ERROR_MARK
1970 && (TYPE_READONLY (cur_type)
1971 || (cur_param != 0
1972 && (TREE_CODE_CLASS (TREE_CODE (cur_param)) == 'c'
1973 || (DECL_P (cur_param) && TREE_READONLY (cur_param))))))
1974 warning ("writing into constant object (arg %d)", arg_num);
1976 /* Check the type of the "real" argument, if there's a type we want. */
1977 if (i == fci->pointer_count + aflag && wanted_type != 0
1978 && TREE_CODE (cur_type) != ERROR_MARK
1979 && wanted_type != TYPE_MAIN_VARIANT (cur_type)
1980 /* If we want `void *', allow any pointer type.
1981 (Anything else would already have got a warning.) */
1982 && ! (wanted_type == void_type_node
1983 && fci->pointer_count > 0)
1984 /* Don't warn about differences merely in signedness. */
1985 && !(TREE_CODE (wanted_type) == INTEGER_TYPE
1986 && TREE_CODE (TYPE_MAIN_VARIANT (cur_type)) == INTEGER_TYPE
1987 && (TREE_UNSIGNED (wanted_type)
1988 ? wanted_type == (cur_type = unsigned_type (cur_type))
1989 : wanted_type == (cur_type = signed_type (cur_type))))
1990 /* Likewise, "signed char", "unsigned char" and "char" are
1991 equivalent but the above test won't consider them equivalent. */
1992 && ! (wanted_type == char_type_node
1993 && (TYPE_MAIN_VARIANT (cur_type) == signed_char_type_node
1994 || TYPE_MAIN_VARIANT (cur_type) == unsigned_char_type_node)))
1996 register const char *this;
1997 register const char *that;
1999 this = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (wanted_type)));
2000 that = 0;
2001 if (TREE_CODE (cur_type) != ERROR_MARK
2002 && TYPE_NAME (cur_type) != 0
2003 && TREE_CODE (cur_type) != INTEGER_TYPE
2004 && !(TREE_CODE (cur_type) == POINTER_TYPE
2005 && TREE_CODE (TREE_TYPE (cur_type)) == INTEGER_TYPE))
2007 if (TREE_CODE (TYPE_NAME (cur_type)) == TYPE_DECL
2008 && DECL_NAME (TYPE_NAME (cur_type)) != 0)
2009 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type)));
2010 else
2011 that = IDENTIFIER_POINTER (TYPE_NAME (cur_type));
2014 /* A nameless type can't possibly match what the format wants.
2015 So there will be a warning for it.
2016 Make up a string to describe vaguely what it is. */
2017 if (that == 0)
2019 if (TREE_CODE (cur_type) == POINTER_TYPE)
2020 that = "pointer";
2021 else
2022 that = "different type";
2025 /* Make the warning better in case of mismatch of int vs long. */
2026 if (TREE_CODE (cur_type) == INTEGER_TYPE
2027 && TREE_CODE (wanted_type) == INTEGER_TYPE
2028 && TYPE_PRECISION (cur_type) == TYPE_PRECISION (wanted_type)
2029 && TYPE_NAME (cur_type) != 0
2030 && TREE_CODE (TYPE_NAME (cur_type)) == TYPE_DECL)
2031 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type)));
2033 if (strcmp (this, that) != 0)
2034 warning ("%s format, %s arg (arg %d)", this, that, arg_num);
2039 /* Print a warning if a constant expression had overflow in folding.
2040 Invoke this function on every expression that the language
2041 requires to be a constant expression.
2042 Note the ANSI C standard says it is erroneous for a
2043 constant expression to overflow. */
2045 void
2046 constant_expression_warning (value)
2047 tree value;
2049 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
2050 || TREE_CODE (value) == COMPLEX_CST)
2051 && TREE_CONSTANT_OVERFLOW (value) && pedantic)
2052 pedwarn ("overflow in constant expression");
2055 /* Print a warning if an expression had overflow in folding.
2056 Invoke this function on every expression that
2057 (1) appears in the source code, and
2058 (2) might be a constant expression that overflowed, and
2059 (3) is not already checked by convert_and_check;
2060 however, do not invoke this function on operands of explicit casts. */
2062 void
2063 overflow_warning (value)
2064 tree value;
2066 if ((TREE_CODE (value) == INTEGER_CST
2067 || (TREE_CODE (value) == COMPLEX_CST
2068 && TREE_CODE (TREE_REALPART (value)) == INTEGER_CST))
2069 && TREE_OVERFLOW (value))
2071 TREE_OVERFLOW (value) = 0;
2072 if (skip_evaluation == 0)
2073 warning ("integer overflow in expression");
2075 else if ((TREE_CODE (value) == REAL_CST
2076 || (TREE_CODE (value) == COMPLEX_CST
2077 && TREE_CODE (TREE_REALPART (value)) == REAL_CST))
2078 && TREE_OVERFLOW (value))
2080 TREE_OVERFLOW (value) = 0;
2081 if (skip_evaluation == 0)
2082 warning ("floating point overflow in expression");
2086 /* Print a warning if a large constant is truncated to unsigned,
2087 or if -Wconversion is used and a constant < 0 is converted to unsigned.
2088 Invoke this function on every expression that might be implicitly
2089 converted to an unsigned type. */
2091 void
2092 unsigned_conversion_warning (result, operand)
2093 tree result, operand;
2095 if (TREE_CODE (operand) == INTEGER_CST
2096 && TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
2097 && TREE_UNSIGNED (TREE_TYPE (result))
2098 && skip_evaluation == 0
2099 && !int_fits_type_p (operand, TREE_TYPE (result)))
2101 if (!int_fits_type_p (operand, signed_type (TREE_TYPE (result))))
2102 /* This detects cases like converting -129 or 256 to unsigned char. */
2103 warning ("large integer implicitly truncated to unsigned type");
2104 else if (warn_conversion)
2105 warning ("negative integer implicitly converted to unsigned type");
2109 /* Convert EXPR to TYPE, warning about conversion problems with constants.
2110 Invoke this function on every expression that is converted implicitly,
2111 i.e. because of language rules and not because of an explicit cast. */
2113 tree
2114 convert_and_check (type, expr)
2115 tree type, expr;
2117 tree t = convert (type, expr);
2118 if (TREE_CODE (t) == INTEGER_CST)
2120 if (TREE_OVERFLOW (t))
2122 TREE_OVERFLOW (t) = 0;
2124 /* Do not diagnose overflow in a constant expression merely
2125 because a conversion overflowed. */
2126 TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (expr);
2128 /* No warning for converting 0x80000000 to int. */
2129 if (!(TREE_UNSIGNED (type) < TREE_UNSIGNED (TREE_TYPE (expr))
2130 && TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
2131 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (expr))))
2132 /* If EXPR fits in the unsigned version of TYPE,
2133 don't warn unless pedantic. */
2134 if ((pedantic
2135 || TREE_UNSIGNED (type)
2136 || ! int_fits_type_p (expr, unsigned_type (type)))
2137 && skip_evaluation == 0)
2138 warning ("overflow in implicit constant conversion");
2140 else
2141 unsigned_conversion_warning (t, expr);
2143 return t;
2146 void
2147 c_expand_expr_stmt (expr)
2148 tree expr;
2150 /* Do default conversion if safe and possibly important,
2151 in case within ({...}). */
2152 if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE && lvalue_p (expr))
2153 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
2154 expr = default_conversion (expr);
2156 if (TREE_TYPE (expr) != error_mark_node
2157 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
2158 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
2159 error ("expression statement has incomplete type");
2161 expand_expr_stmt (expr);
2164 /* Validate the expression after `case' and apply default promotions. */
2166 tree
2167 check_case_value (value)
2168 tree value;
2170 if (value == NULL_TREE)
2171 return value;
2173 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
2174 STRIP_TYPE_NOPS (value);
2176 if (TREE_CODE (value) != INTEGER_CST
2177 && value != error_mark_node)
2179 error ("case label does not reduce to an integer constant");
2180 value = error_mark_node;
2182 else
2183 /* Promote char or short to int. */
2184 value = default_conversion (value);
2186 constant_expression_warning (value);
2188 return value;
2191 /* Return an integer type with BITS bits of precision,
2192 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
2194 tree
2195 type_for_size (bits, unsignedp)
2196 unsigned bits;
2197 int unsignedp;
2199 if (bits == TYPE_PRECISION (integer_type_node))
2200 return unsignedp ? unsigned_type_node : integer_type_node;
2202 if (bits == TYPE_PRECISION (signed_char_type_node))
2203 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2205 if (bits == TYPE_PRECISION (short_integer_type_node))
2206 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2208 if (bits == TYPE_PRECISION (long_integer_type_node))
2209 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2211 if (bits == TYPE_PRECISION (long_long_integer_type_node))
2212 return (unsignedp ? long_long_unsigned_type_node
2213 : long_long_integer_type_node);
2215 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
2216 return (unsignedp ? widest_unsigned_literal_type_node
2217 : widest_integer_literal_type_node);
2219 if (bits <= TYPE_PRECISION (intQI_type_node))
2220 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2222 if (bits <= TYPE_PRECISION (intHI_type_node))
2223 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2225 if (bits <= TYPE_PRECISION (intSI_type_node))
2226 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2228 if (bits <= TYPE_PRECISION (intDI_type_node))
2229 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2231 return 0;
2234 /* Return a data type that has machine mode MODE.
2235 If the mode is an integer,
2236 then UNSIGNEDP selects between signed and unsigned types. */
2238 tree
2239 type_for_mode (mode, unsignedp)
2240 enum machine_mode mode;
2241 int unsignedp;
2243 if (mode == TYPE_MODE (integer_type_node))
2244 return unsignedp ? unsigned_type_node : integer_type_node;
2246 if (mode == TYPE_MODE (signed_char_type_node))
2247 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2249 if (mode == TYPE_MODE (short_integer_type_node))
2250 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2252 if (mode == TYPE_MODE (long_integer_type_node))
2253 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2255 if (mode == TYPE_MODE (long_long_integer_type_node))
2256 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
2258 if (mode == TYPE_MODE (widest_integer_literal_type_node))
2259 return unsignedp ? widest_unsigned_literal_type_node
2260 : widest_integer_literal_type_node;
2262 if (mode == TYPE_MODE (intQI_type_node))
2263 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2265 if (mode == TYPE_MODE (intHI_type_node))
2266 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2268 if (mode == TYPE_MODE (intSI_type_node))
2269 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2271 if (mode == TYPE_MODE (intDI_type_node))
2272 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2274 #if HOST_BITS_PER_WIDE_INT >= 64
2275 if (mode == TYPE_MODE (intTI_type_node))
2276 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2277 #endif
2279 if (mode == TYPE_MODE (float_type_node))
2280 return float_type_node;
2282 if (mode == TYPE_MODE (double_type_node))
2283 return double_type_node;
2285 if (mode == TYPE_MODE (long_double_type_node))
2286 return long_double_type_node;
2288 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
2289 return build_pointer_type (char_type_node);
2291 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
2292 return build_pointer_type (integer_type_node);
2294 return 0;
2297 /* Return an unsigned type the same as TYPE in other respects. */
2298 tree
2299 unsigned_type (type)
2300 tree type;
2302 tree type1 = TYPE_MAIN_VARIANT (type);
2303 if (type1 == signed_char_type_node || type1 == char_type_node)
2304 return unsigned_char_type_node;
2305 if (type1 == integer_type_node)
2306 return unsigned_type_node;
2307 if (type1 == short_integer_type_node)
2308 return short_unsigned_type_node;
2309 if (type1 == long_integer_type_node)
2310 return long_unsigned_type_node;
2311 if (type1 == long_long_integer_type_node)
2312 return long_long_unsigned_type_node;
2313 if (type1 == widest_integer_literal_type_node)
2314 return widest_unsigned_literal_type_node;
2315 #if HOST_BITS_PER_WIDE_INT >= 64
2316 if (type1 == intTI_type_node)
2317 return unsigned_intTI_type_node;
2318 #endif
2319 if (type1 == intDI_type_node)
2320 return unsigned_intDI_type_node;
2321 if (type1 == intSI_type_node)
2322 return unsigned_intSI_type_node;
2323 if (type1 == intHI_type_node)
2324 return unsigned_intHI_type_node;
2325 if (type1 == intQI_type_node)
2326 return unsigned_intQI_type_node;
2328 return signed_or_unsigned_type (1, type);
2331 /* Return a signed type the same as TYPE in other respects. */
2333 tree
2334 signed_type (type)
2335 tree type;
2337 tree type1 = TYPE_MAIN_VARIANT (type);
2338 if (type1 == unsigned_char_type_node || type1 == char_type_node)
2339 return signed_char_type_node;
2340 if (type1 == unsigned_type_node)
2341 return integer_type_node;
2342 if (type1 == short_unsigned_type_node)
2343 return short_integer_type_node;
2344 if (type1 == long_unsigned_type_node)
2345 return long_integer_type_node;
2346 if (type1 == long_long_unsigned_type_node)
2347 return long_long_integer_type_node;
2348 if (type1 == widest_unsigned_literal_type_node)
2349 return widest_integer_literal_type_node;
2350 #if HOST_BITS_PER_WIDE_INT >= 64
2351 if (type1 == unsigned_intTI_type_node)
2352 return intTI_type_node;
2353 #endif
2354 if (type1 == unsigned_intDI_type_node)
2355 return intDI_type_node;
2356 if (type1 == unsigned_intSI_type_node)
2357 return intSI_type_node;
2358 if (type1 == unsigned_intHI_type_node)
2359 return intHI_type_node;
2360 if (type1 == unsigned_intQI_type_node)
2361 return intQI_type_node;
2363 return signed_or_unsigned_type (0, type);
2366 /* Return a type the same as TYPE except unsigned or
2367 signed according to UNSIGNEDP. */
2369 tree
2370 signed_or_unsigned_type (unsignedp, type)
2371 int unsignedp;
2372 tree type;
2374 if (! INTEGRAL_TYPE_P (type)
2375 || TREE_UNSIGNED (type) == unsignedp)
2376 return type;
2378 if (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node))
2379 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2380 if (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2381 return unsignedp ? unsigned_type_node : integer_type_node;
2382 if (TYPE_PRECISION (type) == TYPE_PRECISION (short_integer_type_node))
2383 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2384 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_integer_type_node))
2385 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2386 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_long_integer_type_node))
2387 return (unsignedp ? long_long_unsigned_type_node
2388 : long_long_integer_type_node);
2389 if (TYPE_PRECISION (type) == TYPE_PRECISION (widest_integer_literal_type_node))
2390 return (unsignedp ? widest_unsigned_literal_type_node
2391 : widest_integer_literal_type_node);
2392 return type;
2395 /* Return the minimum number of bits needed to represent VALUE in a
2396 signed or unsigned type, UNSIGNEDP says which. */
2398 unsigned int
2399 min_precision (value, unsignedp)
2400 tree value;
2401 int unsignedp;
2403 int log;
2405 /* If the value is negative, compute its negative minus 1. The latter
2406 adjustment is because the absolute value of the largest negative value
2407 is one larger than the largest positive value. This is equivalent to
2408 a bit-wise negation, so use that operation instead. */
2410 if (tree_int_cst_sgn (value) < 0)
2411 value = fold (build1 (BIT_NOT_EXPR, TREE_TYPE (value), value));
2413 /* Return the number of bits needed, taking into account the fact
2414 that we need one more bit for a signed than unsigned type. */
2416 if (integer_zerop (value))
2417 log = 0;
2418 else
2419 log = tree_floor_log2 (value);
2421 return log + 1 + ! unsignedp;
2424 /* Print an error message for invalid operands to arith operation CODE.
2425 NOP_EXPR is used as a special case (see truthvalue_conversion). */
2427 void
2428 binary_op_error (code)
2429 enum tree_code code;
2431 register const char *opname;
2433 switch (code)
2435 case NOP_EXPR:
2436 error ("invalid truth-value expression");
2437 return;
2439 case PLUS_EXPR:
2440 opname = "+"; break;
2441 case MINUS_EXPR:
2442 opname = "-"; break;
2443 case MULT_EXPR:
2444 opname = "*"; break;
2445 case MAX_EXPR:
2446 opname = "max"; break;
2447 case MIN_EXPR:
2448 opname = "min"; break;
2449 case EQ_EXPR:
2450 opname = "=="; break;
2451 case NE_EXPR:
2452 opname = "!="; break;
2453 case LE_EXPR:
2454 opname = "<="; break;
2455 case GE_EXPR:
2456 opname = ">="; break;
2457 case LT_EXPR:
2458 opname = "<"; break;
2459 case GT_EXPR:
2460 opname = ">"; break;
2461 case LSHIFT_EXPR:
2462 opname = "<<"; break;
2463 case RSHIFT_EXPR:
2464 opname = ">>"; break;
2465 case TRUNC_MOD_EXPR:
2466 case FLOOR_MOD_EXPR:
2467 opname = "%"; break;
2468 case TRUNC_DIV_EXPR:
2469 case FLOOR_DIV_EXPR:
2470 opname = "/"; break;
2471 case BIT_AND_EXPR:
2472 opname = "&"; break;
2473 case BIT_IOR_EXPR:
2474 opname = "|"; break;
2475 case TRUTH_ANDIF_EXPR:
2476 opname = "&&"; break;
2477 case TRUTH_ORIF_EXPR:
2478 opname = "||"; break;
2479 case BIT_XOR_EXPR:
2480 opname = "^"; break;
2481 case LROTATE_EXPR:
2482 case RROTATE_EXPR:
2483 opname = "rotate"; break;
2484 default:
2485 opname = "unknown"; break;
2487 error ("invalid operands to binary %s", opname);
2490 /* Subroutine of build_binary_op, used for comparison operations.
2491 See if the operands have both been converted from subword integer types
2492 and, if so, perhaps change them both back to their original type.
2493 This function is also responsible for converting the two operands
2494 to the proper common type for comparison.
2496 The arguments of this function are all pointers to local variables
2497 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
2498 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
2500 If this function returns nonzero, it means that the comparison has
2501 a constant value. What this function returns is an expression for
2502 that value. */
2504 tree
2505 shorten_compare (op0_ptr, op1_ptr, restype_ptr, rescode_ptr)
2506 tree *op0_ptr, *op1_ptr;
2507 tree *restype_ptr;
2508 enum tree_code *rescode_ptr;
2510 register tree type;
2511 tree op0 = *op0_ptr;
2512 tree op1 = *op1_ptr;
2513 int unsignedp0, unsignedp1;
2514 int real1, real2;
2515 tree primop0, primop1;
2516 enum tree_code code = *rescode_ptr;
2518 /* Throw away any conversions to wider types
2519 already present in the operands. */
2521 primop0 = get_narrower (op0, &unsignedp0);
2522 primop1 = get_narrower (op1, &unsignedp1);
2524 /* Handle the case that OP0 does not *contain* a conversion
2525 but it *requires* conversion to FINAL_TYPE. */
2527 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
2528 unsignedp0 = TREE_UNSIGNED (TREE_TYPE (op0));
2529 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
2530 unsignedp1 = TREE_UNSIGNED (TREE_TYPE (op1));
2532 /* If one of the operands must be floated, we cannot optimize. */
2533 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
2534 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
2536 /* If first arg is constant, swap the args (changing operation
2537 so value is preserved), for canonicalization. Don't do this if
2538 the second arg is 0. */
2540 if (TREE_CONSTANT (primop0)
2541 && ! integer_zerop (primop1) && ! real_zerop (primop1))
2543 register tree tem = primop0;
2544 register int temi = unsignedp0;
2545 primop0 = primop1;
2546 primop1 = tem;
2547 tem = op0;
2548 op0 = op1;
2549 op1 = tem;
2550 *op0_ptr = op0;
2551 *op1_ptr = op1;
2552 unsignedp0 = unsignedp1;
2553 unsignedp1 = temi;
2554 temi = real1;
2555 real1 = real2;
2556 real2 = temi;
2558 switch (code)
2560 case LT_EXPR:
2561 code = GT_EXPR;
2562 break;
2563 case GT_EXPR:
2564 code = LT_EXPR;
2565 break;
2566 case LE_EXPR:
2567 code = GE_EXPR;
2568 break;
2569 case GE_EXPR:
2570 code = LE_EXPR;
2571 break;
2572 default:
2573 break;
2575 *rescode_ptr = code;
2578 /* If comparing an integer against a constant more bits wide,
2579 maybe we can deduce a value of 1 or 0 independent of the data.
2580 Or else truncate the constant now
2581 rather than extend the variable at run time.
2583 This is only interesting if the constant is the wider arg.
2584 Also, it is not safe if the constant is unsigned and the
2585 variable arg is signed, since in this case the variable
2586 would be sign-extended and then regarded as unsigned.
2587 Our technique fails in this case because the lowest/highest
2588 possible unsigned results don't follow naturally from the
2589 lowest/highest possible values of the variable operand.
2590 For just EQ_EXPR and NE_EXPR there is another technique that
2591 could be used: see if the constant can be faithfully represented
2592 in the other operand's type, by truncating it and reextending it
2593 and see if that preserves the constant's value. */
2595 if (!real1 && !real2
2596 && TREE_CODE (primop1) == INTEGER_CST
2597 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
2599 int min_gt, max_gt, min_lt, max_lt;
2600 tree maxval, minval;
2601 /* 1 if comparison is nominally unsigned. */
2602 int unsignedp = TREE_UNSIGNED (*restype_ptr);
2603 tree val;
2605 type = signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0));
2607 /* If TYPE is an enumeration, then we need to get its min/max
2608 values from it's underlying integral type, not the enumerated
2609 type itself. */
2610 if (TREE_CODE (type) == ENUMERAL_TYPE)
2611 type = type_for_size (TYPE_PRECISION (type), unsignedp0);
2613 maxval = TYPE_MAX_VALUE (type);
2614 minval = TYPE_MIN_VALUE (type);
2616 if (unsignedp && !unsignedp0)
2617 *restype_ptr = signed_type (*restype_ptr);
2619 if (TREE_TYPE (primop1) != *restype_ptr)
2620 primop1 = convert (*restype_ptr, primop1);
2621 if (type != *restype_ptr)
2623 minval = convert (*restype_ptr, minval);
2624 maxval = convert (*restype_ptr, maxval);
2627 if (unsignedp && unsignedp0)
2629 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
2630 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
2631 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
2632 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
2634 else
2636 min_gt = INT_CST_LT (primop1, minval);
2637 max_gt = INT_CST_LT (primop1, maxval);
2638 min_lt = INT_CST_LT (minval, primop1);
2639 max_lt = INT_CST_LT (maxval, primop1);
2642 val = 0;
2643 /* This used to be a switch, but Genix compiler can't handle that. */
2644 if (code == NE_EXPR)
2646 if (max_lt || min_gt)
2647 val = boolean_true_node;
2649 else if (code == EQ_EXPR)
2651 if (max_lt || min_gt)
2652 val = boolean_false_node;
2654 else if (code == LT_EXPR)
2656 if (max_lt)
2657 val = boolean_true_node;
2658 if (!min_lt)
2659 val = boolean_false_node;
2661 else if (code == GT_EXPR)
2663 if (min_gt)
2664 val = boolean_true_node;
2665 if (!max_gt)
2666 val = boolean_false_node;
2668 else if (code == LE_EXPR)
2670 if (!max_gt)
2671 val = boolean_true_node;
2672 if (min_gt)
2673 val = boolean_false_node;
2675 else if (code == GE_EXPR)
2677 if (!min_lt)
2678 val = boolean_true_node;
2679 if (max_lt)
2680 val = boolean_false_node;
2683 /* If primop0 was sign-extended and unsigned comparison specd,
2684 we did a signed comparison above using the signed type bounds.
2685 But the comparison we output must be unsigned.
2687 Also, for inequalities, VAL is no good; but if the signed
2688 comparison had *any* fixed result, it follows that the
2689 unsigned comparison just tests the sign in reverse
2690 (positive values are LE, negative ones GE).
2691 So we can generate an unsigned comparison
2692 against an extreme value of the signed type. */
2694 if (unsignedp && !unsignedp0)
2696 if (val != 0)
2697 switch (code)
2699 case LT_EXPR:
2700 case GE_EXPR:
2701 primop1 = TYPE_MIN_VALUE (type);
2702 val = 0;
2703 break;
2705 case LE_EXPR:
2706 case GT_EXPR:
2707 primop1 = TYPE_MAX_VALUE (type);
2708 val = 0;
2709 break;
2711 default:
2712 break;
2714 type = unsigned_type (type);
2717 if (!max_gt && !unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
2719 /* This is the case of (char)x >?< 0x80, which people used to use
2720 expecting old C compilers to change the 0x80 into -0x80. */
2721 if (val == boolean_false_node)
2722 warning ("comparison is always false due to limited range of data type");
2723 if (val == boolean_true_node)
2724 warning ("comparison is always true due to limited range of data type");
2727 if (!min_lt && unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
2729 /* This is the case of (unsigned char)x >?< -1 or < 0. */
2730 if (val == boolean_false_node)
2731 warning ("comparison is always false due to limited range of data type");
2732 if (val == boolean_true_node)
2733 warning ("comparison is always true due to limited range of data type");
2736 if (val != 0)
2738 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2739 if (TREE_SIDE_EFFECTS (primop0))
2740 return build (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
2741 return val;
2744 /* Value is not predetermined, but do the comparison
2745 in the type of the operand that is not constant.
2746 TYPE is already properly set. */
2748 else if (real1 && real2
2749 && (TYPE_PRECISION (TREE_TYPE (primop0))
2750 == TYPE_PRECISION (TREE_TYPE (primop1))))
2751 type = TREE_TYPE (primop0);
2753 /* If args' natural types are both narrower than nominal type
2754 and both extend in the same manner, compare them
2755 in the type of the wider arg.
2756 Otherwise must actually extend both to the nominal
2757 common type lest different ways of extending
2758 alter the result.
2759 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
2761 else if (unsignedp0 == unsignedp1 && real1 == real2
2762 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
2763 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
2765 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
2766 type = signed_or_unsigned_type (unsignedp0
2767 || TREE_UNSIGNED (*restype_ptr),
2768 type);
2769 /* Make sure shorter operand is extended the right way
2770 to match the longer operand. */
2771 primop0 = convert (signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0)),
2772 primop0);
2773 primop1 = convert (signed_or_unsigned_type (unsignedp1, TREE_TYPE (primop1)),
2774 primop1);
2776 else
2778 /* Here we must do the comparison on the nominal type
2779 using the args exactly as we received them. */
2780 type = *restype_ptr;
2781 primop0 = op0;
2782 primop1 = op1;
2784 if (!real1 && !real2 && integer_zerop (primop1)
2785 && TREE_UNSIGNED (*restype_ptr))
2787 tree value = 0;
2788 switch (code)
2790 case GE_EXPR:
2791 /* All unsigned values are >= 0, so we warn if extra warnings
2792 are requested. However, if OP0 is a constant that is
2793 >= 0, the signedness of the comparison isn't an issue,
2794 so suppress the warning. */
2795 if (extra_warnings
2796 && ! (TREE_CODE (primop0) == INTEGER_CST
2797 && ! TREE_OVERFLOW (convert (signed_type (type),
2798 primop0))))
2799 warning ("comparison of unsigned expression >= 0 is always true");
2800 value = boolean_true_node;
2801 break;
2803 case LT_EXPR:
2804 if (extra_warnings
2805 && ! (TREE_CODE (primop0) == INTEGER_CST
2806 && ! TREE_OVERFLOW (convert (signed_type (type),
2807 primop0))))
2808 warning ("comparison of unsigned expression < 0 is always false");
2809 value = boolean_false_node;
2810 break;
2812 default:
2813 break;
2816 if (value != 0)
2818 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2819 if (TREE_SIDE_EFFECTS (primop0))
2820 return build (COMPOUND_EXPR, TREE_TYPE (value),
2821 primop0, value);
2822 return value;
2827 *op0_ptr = convert (type, primop0);
2828 *op1_ptr = convert (type, primop1);
2830 *restype_ptr = boolean_type_node;
2832 return 0;
2835 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
2836 or validate its data type for an `if' or `while' statement or ?..: exp.
2838 This preparation consists of taking the ordinary
2839 representation of an expression expr and producing a valid tree
2840 boolean expression describing whether expr is nonzero. We could
2841 simply always do build_binary_op (NE_EXPR, expr, boolean_false_node, 1),
2842 but we optimize comparisons, &&, ||, and !.
2844 The resulting type should always be `boolean_type_node'. */
2846 tree
2847 truthvalue_conversion (expr)
2848 tree expr;
2850 if (TREE_CODE (expr) == ERROR_MARK)
2851 return expr;
2853 #if 0 /* This appears to be wrong for C++. */
2854 /* These really should return error_mark_node after 2.4 is stable.
2855 But not all callers handle ERROR_MARK properly. */
2856 switch (TREE_CODE (TREE_TYPE (expr)))
2858 case RECORD_TYPE:
2859 error ("struct type value used where scalar is required");
2860 return boolean_false_node;
2862 case UNION_TYPE:
2863 error ("union type value used where scalar is required");
2864 return boolean_false_node;
2866 case ARRAY_TYPE:
2867 error ("array type value used where scalar is required");
2868 return boolean_false_node;
2870 default:
2871 break;
2873 #endif /* 0 */
2875 switch (TREE_CODE (expr))
2877 case EQ_EXPR:
2878 case NE_EXPR: case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
2879 case TRUTH_ANDIF_EXPR:
2880 case TRUTH_ORIF_EXPR:
2881 case TRUTH_AND_EXPR:
2882 case TRUTH_OR_EXPR:
2883 case TRUTH_XOR_EXPR:
2884 case TRUTH_NOT_EXPR:
2885 TREE_TYPE (expr) = boolean_type_node;
2886 return expr;
2888 case ERROR_MARK:
2889 return expr;
2891 case INTEGER_CST:
2892 return integer_zerop (expr) ? boolean_false_node : boolean_true_node;
2894 case REAL_CST:
2895 return real_zerop (expr) ? boolean_false_node : boolean_true_node;
2897 case ADDR_EXPR:
2898 /* If we are taking the address of a external decl, it might be zero
2899 if it is weak, so we cannot optimize. */
2900 if (DECL_P (TREE_OPERAND (expr, 0))
2901 && DECL_EXTERNAL (TREE_OPERAND (expr, 0)))
2902 break;
2904 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 0)))
2905 return build (COMPOUND_EXPR, boolean_type_node,
2906 TREE_OPERAND (expr, 0), boolean_true_node);
2907 else
2908 return boolean_true_node;
2910 case COMPLEX_EXPR:
2911 return build_binary_op ((TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
2912 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
2913 truthvalue_conversion (TREE_OPERAND (expr, 0)),
2914 truthvalue_conversion (TREE_OPERAND (expr, 1)),
2917 case NEGATE_EXPR:
2918 case ABS_EXPR:
2919 case FLOAT_EXPR:
2920 case FFS_EXPR:
2921 /* These don't change whether an object is non-zero or zero. */
2922 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2924 case LROTATE_EXPR:
2925 case RROTATE_EXPR:
2926 /* These don't change whether an object is zero or non-zero, but
2927 we can't ignore them if their second arg has side-effects. */
2928 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
2929 return build (COMPOUND_EXPR, boolean_type_node, TREE_OPERAND (expr, 1),
2930 truthvalue_conversion (TREE_OPERAND (expr, 0)));
2931 else
2932 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2934 case COND_EXPR:
2935 /* Distribute the conversion into the arms of a COND_EXPR. */
2936 return fold (build (COND_EXPR, boolean_type_node, TREE_OPERAND (expr, 0),
2937 truthvalue_conversion (TREE_OPERAND (expr, 1)),
2938 truthvalue_conversion (TREE_OPERAND (expr, 2))));
2940 case CONVERT_EXPR:
2941 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
2942 since that affects how `default_conversion' will behave. */
2943 if (TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE
2944 || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
2945 break;
2946 /* fall through... */
2947 case NOP_EXPR:
2948 /* If this is widening the argument, we can ignore it. */
2949 if (TYPE_PRECISION (TREE_TYPE (expr))
2950 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
2951 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2952 break;
2954 case MINUS_EXPR:
2955 /* With IEEE arithmetic, x - x may not equal 0, so we can't optimize
2956 this case. */
2957 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
2958 && TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE)
2959 break;
2960 /* fall through... */
2961 case BIT_XOR_EXPR:
2962 /* This and MINUS_EXPR can be changed into a comparison of the
2963 two objects. */
2964 if (TREE_TYPE (TREE_OPERAND (expr, 0))
2965 == TREE_TYPE (TREE_OPERAND (expr, 1)))
2966 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2967 TREE_OPERAND (expr, 1), 1);
2968 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2969 fold (build1 (NOP_EXPR,
2970 TREE_TYPE (TREE_OPERAND (expr, 0)),
2971 TREE_OPERAND (expr, 1))), 1);
2973 case BIT_AND_EXPR:
2974 if (integer_onep (TREE_OPERAND (expr, 1))
2975 && TREE_TYPE (expr) != boolean_type_node)
2976 /* Using convert here would cause infinite recursion. */
2977 return build1 (NOP_EXPR, boolean_type_node, expr);
2978 break;
2980 case MODIFY_EXPR:
2981 if (warn_parentheses && C_EXP_ORIGINAL_CODE (expr) == MODIFY_EXPR)
2982 warning ("suggest parentheses around assignment used as truth value");
2983 break;
2985 default:
2986 break;
2989 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
2991 tree tem = save_expr (expr);
2992 return (build_binary_op
2993 ((TREE_SIDE_EFFECTS (expr)
2994 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
2995 truthvalue_conversion (build_unary_op (REALPART_EXPR, tem, 0)),
2996 truthvalue_conversion (build_unary_op (IMAGPART_EXPR, tem, 0)),
2997 0));
3000 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
3003 #if USE_CPPLIB
3004 /* Read the rest of a #-directive from input stream FINPUT.
3005 In normal use, the directive name and the white space after it
3006 have already been read, so they won't be included in the result.
3007 We allow for the fact that the directive line may contain
3008 a newline embedded within a character or string literal which forms
3009 a part of the directive.
3011 The value is a string in a reusable buffer. It remains valid
3012 only until the next time this function is called. */
3013 unsigned char *yy_cur, *yy_lim;
3015 #define GETC() (yy_cur < yy_lim ? *yy_cur++ : yy_get_token ())
3016 #define UNGETC(c) ((c) == EOF ? 0 : yy_cur--)
3019 yy_get_token ()
3021 for (;;)
3023 parse_in.limit = parse_in.token_buffer;
3024 cpp_token = cpp_get_token (&parse_in);
3025 if (cpp_token == CPP_EOF)
3026 return -1;
3027 yy_lim = CPP_PWRITTEN (&parse_in);
3028 yy_cur = parse_in.token_buffer;
3029 if (yy_cur < yy_lim)
3030 return *yy_cur++;
3034 char *
3035 get_directive_line ()
3037 static char *directive_buffer = NULL;
3038 static unsigned buffer_length = 0;
3039 register char *p;
3040 register char *buffer_limit;
3041 register int looking_for = 0;
3042 register int char_escaped = 0;
3044 if (buffer_length == 0)
3046 directive_buffer = (char *)xmalloc (128);
3047 buffer_length = 128;
3050 buffer_limit = &directive_buffer[buffer_length];
3052 for (p = directive_buffer; ; )
3054 int c;
3056 /* Make buffer bigger if it is full. */
3057 if (p >= buffer_limit)
3059 register unsigned bytes_used = (p - directive_buffer);
3061 buffer_length *= 2;
3062 directive_buffer
3063 = (char *)xrealloc (directive_buffer, buffer_length);
3064 p = &directive_buffer[bytes_used];
3065 buffer_limit = &directive_buffer[buffer_length];
3068 c = GETC ();
3070 /* Discard initial whitespace. */
3071 if ((c == ' ' || c == '\t') && p == directive_buffer)
3072 continue;
3074 /* Detect the end of the directive. */
3075 if (c == '\n' && looking_for == 0)
3077 UNGETC (c);
3078 c = '\0';
3081 *p++ = c;
3083 if (c == 0)
3084 return directive_buffer;
3086 /* Handle string and character constant syntax. */
3087 if (looking_for)
3089 if (looking_for == c && !char_escaped)
3090 looking_for = 0; /* Found terminator... stop looking. */
3092 else
3093 if (c == '\'' || c == '"')
3094 looking_for = c; /* Don't stop buffering until we see another
3095 another one of these (or an EOF). */
3097 /* Handle backslash. */
3098 char_escaped = (c == '\\' && ! char_escaped);
3101 #else
3102 /* Read the rest of a #-directive from input stream FINPUT.
3103 In normal use, the directive name and the white space after it
3104 have already been read, so they won't be included in the result.
3105 We allow for the fact that the directive line may contain
3106 a newline embedded within a character or string literal which forms
3107 a part of the directive.
3109 The value is a string in a reusable buffer. It remains valid
3110 only until the next time this function is called.
3112 The terminating character ('\n' or EOF) is left in FINPUT for the
3113 caller to re-read. */
3115 char *
3116 get_directive_line (finput)
3117 register FILE *finput;
3119 static char *directive_buffer = NULL;
3120 static unsigned buffer_length = 0;
3121 register char *p;
3122 register char *buffer_limit;
3123 register int looking_for = 0;
3124 register int char_escaped = 0;
3126 if (buffer_length == 0)
3128 directive_buffer = (char *)xmalloc (128);
3129 buffer_length = 128;
3132 buffer_limit = &directive_buffer[buffer_length];
3134 for (p = directive_buffer; ; )
3136 int c;
3138 /* Make buffer bigger if it is full. */
3139 if (p >= buffer_limit)
3141 register unsigned bytes_used = (p - directive_buffer);
3143 buffer_length *= 2;
3144 directive_buffer
3145 = (char *)xrealloc (directive_buffer, buffer_length);
3146 p = &directive_buffer[bytes_used];
3147 buffer_limit = &directive_buffer[buffer_length];
3150 c = getc (finput);
3152 /* Discard initial whitespace. */
3153 if ((c == ' ' || c == '\t') && p == directive_buffer)
3154 continue;
3156 /* Detect the end of the directive. */
3157 if (looking_for == 0
3158 && (c == '\n' || c == EOF))
3160 ungetc (c, finput);
3161 c = '\0';
3164 *p++ = c;
3166 if (c == 0)
3167 return directive_buffer;
3169 /* Handle string and character constant syntax. */
3170 if (looking_for)
3172 if (looking_for == c && !char_escaped)
3173 looking_for = 0; /* Found terminator... stop looking. */
3175 else
3176 if (c == '\'' || c == '"')
3177 looking_for = c; /* Don't stop buffering until we see another
3178 one of these (or an EOF). */
3180 /* Handle backslash. */
3181 char_escaped = (c == '\\' && ! char_escaped);
3184 #endif /* !USE_CPPLIB */
3186 /* Make a variant type in the proper way for C/C++, propagating qualifiers
3187 down to the element type of an array. */
3189 tree
3190 c_build_qualified_type (type, type_quals)
3191 tree type;
3192 int type_quals;
3194 /* A restrict-qualified pointer type must be a pointer to object or
3195 incomplete type. Note that the use of POINTER_TYPE_P also allows
3196 REFERENCE_TYPEs, which is appropriate for C++. Unfortunately,
3197 the C++ front-end also use POINTER_TYPE for pointer-to-member
3198 values, so even though it should be illegal to use `restrict'
3199 with such an entity we don't flag that here. Thus, special case
3200 code for that case is required in the C++ front-end. */
3201 if ((type_quals & TYPE_QUAL_RESTRICT)
3202 && (!POINTER_TYPE_P (type)
3203 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
3205 error ("invalid use of `restrict'");
3206 type_quals &= ~TYPE_QUAL_RESTRICT;
3209 if (TREE_CODE (type) == ARRAY_TYPE)
3210 return build_array_type (c_build_qualified_type (TREE_TYPE (type),
3211 type_quals),
3212 TYPE_DOMAIN (type));
3213 return build_qualified_type (type, type_quals);
3216 /* Apply the TYPE_QUALS to the new DECL. */
3218 void
3219 c_apply_type_quals_to_decl (type_quals, decl)
3220 int type_quals;
3221 tree decl;
3223 if ((type_quals & TYPE_QUAL_CONST)
3224 || (TREE_TYPE (decl)
3225 && TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE))
3226 TREE_READONLY (decl) = 1;
3227 if (type_quals & TYPE_QUAL_VOLATILE)
3229 TREE_SIDE_EFFECTS (decl) = 1;
3230 TREE_THIS_VOLATILE (decl) = 1;
3232 if (type_quals & TYPE_QUAL_RESTRICT)
3234 if (!TREE_TYPE (decl)
3235 || !POINTER_TYPE_P (TREE_TYPE (decl))
3236 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (TREE_TYPE (decl))))
3237 error ("invalid use of `restrict'");
3238 else if (flag_strict_aliasing)
3240 /* No two restricted pointers can point at the same thing.
3241 However, a restricted pointer can point at the same thing
3242 as an unrestricted pointer, if that unrestricted pointer
3243 is based on the restricted pointer. So, we make the
3244 alias set for the restricted pointer a subset of the
3245 alias set for the type pointed to by the type of the
3246 decl. */
3248 int pointed_to_alias_set
3249 = get_alias_set (TREE_TYPE (TREE_TYPE (decl)));
3251 if (!pointed_to_alias_set)
3252 /* It's not legal to make a subset of alias set zero. */
3254 else
3256 DECL_POINTER_ALIAS_SET (decl) = new_alias_set ();
3257 record_alias_subset (pointed_to_alias_set,
3258 DECL_POINTER_ALIAS_SET (decl));
3264 /* T is an expression with pointer type. Find the DECL on which this
3265 expression is based. (For example, in `a[i]' this would be `a'.)
3266 If there is no such DECL, or a unique decl cannot be determined,
3267 NULL_TREE is retured. */
3269 static tree
3270 c_find_base_decl (t)
3271 tree t;
3273 int i;
3274 tree decl;
3276 if (t == NULL_TREE || t == error_mark_node)
3277 return NULL_TREE;
3279 if (!POINTER_TYPE_P (TREE_TYPE (t)))
3280 return NULL_TREE;
3282 decl = NULL_TREE;
3284 if (TREE_CODE (t) == FIELD_DECL
3285 || TREE_CODE (t) == PARM_DECL
3286 || TREE_CODE (t) == VAR_DECL)
3287 /* Aha, we found a pointer-typed declaration. */
3288 return t;
3290 /* It would be nice to deal with COMPONENT_REFs here. If we could
3291 tell that `a' and `b' were the same, then `a->f' and `b->f' are
3292 also the same. */
3294 /* Handle general expressions. */
3295 switch (TREE_CODE_CLASS (TREE_CODE (t)))
3297 case '1':
3298 case '2':
3299 case '3':
3300 for (i = tree_code_length [(int) TREE_CODE (t)]; --i >= 0;)
3302 tree d = c_find_base_decl (TREE_OPERAND (t, i));
3303 if (d)
3305 if (!decl)
3306 decl = d;
3307 else if (d && d != decl)
3308 /* Two different declarations. That's confusing; let's
3309 just assume we don't know what's going on. */
3310 decl = NULL_TREE;
3313 break;
3315 default:
3316 break;
3319 return decl;
3322 /* Return the typed-based alias set for T, which may be an expression
3323 or a type. */
3326 c_get_alias_set (t)
3327 tree t;
3329 tree type;
3330 tree u;
3332 if (t == error_mark_node)
3333 return 0;
3335 type = (TYPE_P (t)) ? t : TREE_TYPE (t);
3337 if (type == error_mark_node)
3338 return 0;
3340 /* Deal with special cases first; for certain kinds of references
3341 we're interested in more than just the type. */
3343 if (TREE_CODE (t) == BIT_FIELD_REF)
3344 /* Perhaps reads and writes to this piece of data alias fields
3345 neighboring the bitfield. Perhaps that's impossible. For now,
3346 let's just assume that bitfields can alias everything, which is
3347 the conservative assumption. */
3348 return 0;
3350 /* Permit type-punning when accessing a union, provided the access
3351 is directly through the union. For example, this code does not
3352 permit taking the address of a union member and then storing
3353 through it. Even the type-punning allowed here is a GCC
3354 extension, albeit a common and useful one; the C standard says
3355 that such accesses have implementation-defined behavior. */
3356 for (u = t;
3357 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
3358 u = TREE_OPERAND (u, 0))
3359 if (TREE_CODE (u) == COMPONENT_REF
3360 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
3361 return 0;
3363 if (TREE_CODE (t) == INDIRECT_REF)
3365 /* Check for accesses through restrict-qualified pointers. */
3366 tree decl = c_find_base_decl (TREE_OPERAND (t, 0));
3368 if (decl && DECL_POINTER_ALIAS_SET_KNOWN_P (decl))
3369 /* We use the alias set indicated in the declaration. */
3370 return DECL_POINTER_ALIAS_SET (decl);
3373 /* From here on, only the type matters. */
3375 if (TREE_CODE (t) == COMPONENT_REF
3376 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (t, 1)))
3377 /* Since build_modify_expr calls get_unwidened for stores to
3378 component references, the type of a bit field can be changed
3379 from (say) `unsigned int : 16' to `unsigned short' or from
3380 `enum E : 16' to `short'. We want the real type of the
3381 bit-field in this case, not some the integral equivalent. */
3382 type = DECL_BIT_FIELD_TYPE (TREE_OPERAND (t, 1));
3384 if (TYPE_ALIAS_SET_KNOWN_P (type))
3385 /* If we've already calculated the value, just return it. */
3386 return TYPE_ALIAS_SET (type);
3387 else if (TYPE_MAIN_VARIANT (type) != type)
3388 /* The C standard specifically allows aliasing between
3389 cv-qualified variants of types. */
3390 TYPE_ALIAS_SET (type) = c_get_alias_set (TYPE_MAIN_VARIANT (type));
3391 else if (TREE_CODE (type) == INTEGER_TYPE)
3393 tree signed_variant;
3395 /* The C standard specifically allows aliasing between signed and
3396 unsigned variants of the same type. We treat the signed
3397 variant as canonical. */
3398 signed_variant = signed_type (type);
3400 if (signed_variant != type)
3401 TYPE_ALIAS_SET (type) = c_get_alias_set (signed_variant);
3402 else if (signed_variant == signed_char_type_node)
3403 /* The C standard guarantess that any object may be accessed
3404 via an lvalue that has character type. We don't have to
3405 check for unsigned_char_type_node or char_type_node because
3406 we are specifically looking at the signed variant. */
3407 TYPE_ALIAS_SET (type) = 0;
3409 else if (TREE_CODE (type) == ARRAY_TYPE)
3410 /* Anything that can alias one of the array elements can alias
3411 the entire array as well. */
3412 TYPE_ALIAS_SET (type) = c_get_alias_set (TREE_TYPE (type));
3413 else if (TREE_CODE (type) == FUNCTION_TYPE)
3414 /* There are no objects of FUNCTION_TYPE, so there's no point in
3415 using up an alias set for them. (There are, of course,
3416 pointers and references to functions, but that's
3417 different.) */
3418 TYPE_ALIAS_SET (type) = 0;
3419 else if (TREE_CODE (type) == RECORD_TYPE
3420 || TREE_CODE (type) == UNION_TYPE)
3421 /* If TYPE is a struct or union type then we're reading or
3422 writing an entire struct. Thus, we don't know anything about
3423 aliasing. (In theory, such an access can only alias objects
3424 whose type is the same as one of the fields, recursively, but
3425 we don't yet make any use of that information.) */
3426 TYPE_ALIAS_SET (type) = 0;
3427 else if (TREE_CODE (type) == POINTER_TYPE
3428 || TREE_CODE (type) == REFERENCE_TYPE)
3430 tree t;
3432 /* Unfortunately, there is no canonical form of a pointer type.
3433 In particular, if we have `typedef int I', then `int *', and
3434 `I *' are different types. So, we have to pick a canonical
3435 representative. We do this below.
3437 Technically, this approach is actually more conservative that
3438 it needs to be. In particular, `const int *' and `int *'
3439 chould be in different alias sets, according to the C and C++
3440 standard, since their types are not the same, and so,
3441 technically, an `int **' and `const int **' cannot point at
3442 the same thing.
3444 But, the standard is wrong. In particular, this code is
3445 legal C++:
3447 int *ip;
3448 int **ipp = &ip;
3449 const int* const* cipp = &ip;
3451 And, it doesn't make sense for that to be legal unless you
3452 can dereference IPP and CIPP. So, we ignore cv-qualifiers on
3453 the pointed-to types. This issue has been reported to the
3454 C++ committee. */
3455 t = TYPE_MAIN_VARIANT (TREE_TYPE (type));
3456 t = ((TREE_CODE (type) == POINTER_TYPE)
3457 ? build_pointer_type (t) : build_reference_type (t));
3458 if (t != type)
3459 TYPE_ALIAS_SET (type) = c_get_alias_set (t);
3462 if (!TYPE_ALIAS_SET_KNOWN_P (type))
3463 /* TYPE is something we haven't seen before. Put it in a new
3464 alias set. */
3465 TYPE_ALIAS_SET (type) = new_alias_set ();
3467 return TYPE_ALIAS_SET (type);
3470 /* Build tree nodes and builtin functions common to both C and C++ language
3471 frontends.
3472 CPLUS_MODE is nonzero if we are called from the C++ frontend, we generate
3473 some stricter prototypes in that case.
3474 NO_BUILTINS and NO_NONANSI_BUILTINS contain the respective values of
3475 the language frontend flags flag_no_builtin and
3476 flag_no_nonansi_builtin. */
3477 void
3478 c_common_nodes_and_builtins (cplus_mode, no_builtins, no_nonansi_builtins)
3479 int cplus_mode, no_builtins, no_nonansi_builtins;
3481 tree temp;
3482 tree memcpy_ftype, memset_ftype, strlen_ftype;
3483 tree bzero_ftype, bcmp_ftype;
3484 tree endlink, int_endlink, double_endlink, unsigned_endlink;
3485 tree sizetype_endlink;
3486 tree ptr_ftype, ptr_ftype_unsigned;
3487 tree void_ftype_any, void_ftype_int, int_ftype_any;
3488 tree double_ftype_double, double_ftype_double_double;
3489 tree float_ftype_float, ldouble_ftype_ldouble;
3490 tree int_ftype_cptr_cptr_sizet;
3491 tree int_ftype_string_string, string_ftype_ptr_ptr;
3492 tree long_ftype_long;
3493 /* Either char* or void*. */
3494 tree traditional_ptr_type_node;
3495 /* Either const char* or const void*. */
3496 tree traditional_cptr_type_node;
3497 tree traditional_len_type_node;
3498 tree traditional_len_endlink;
3499 tree va_list_ref_type_node;
3500 tree va_list_arg_type_node;
3502 pushdecl (build_decl (TYPE_DECL, get_identifier ("__builtin_va_list"),
3503 va_list_type_node));
3505 pushdecl (build_decl (TYPE_DECL, get_identifier ("__builtin_ptrdiff_t"),
3506 ptrdiff_type_node));
3508 pushdecl (build_decl (TYPE_DECL, get_identifier ("__builtin_size_t"),
3509 sizetype));
3511 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
3513 va_list_arg_type_node = va_list_ref_type_node =
3514 build_pointer_type (TREE_TYPE (va_list_type_node));
3516 else
3518 va_list_arg_type_node = va_list_type_node;
3519 va_list_ref_type_node = build_reference_type (va_list_type_node);
3522 endlink = void_list_node;
3523 int_endlink = tree_cons (NULL_TREE, integer_type_node, endlink);
3524 double_endlink = tree_cons (NULL_TREE, double_type_node, endlink);
3525 unsigned_endlink = tree_cons (NULL_TREE, unsigned_type_node, endlink);
3527 ptr_ftype = build_function_type (ptr_type_node, NULL_TREE);
3528 ptr_ftype_unsigned = build_function_type (ptr_type_node, unsigned_endlink);
3529 sizetype_endlink = tree_cons (NULL_TREE, TYPE_DOMAIN (sizetype), endlink);
3530 /* We realloc here because sizetype could be int or unsigned. S'ok. */
3531 ptr_ftype_sizetype = build_function_type (ptr_type_node, sizetype_endlink);
3533 int_ftype_any = build_function_type (integer_type_node, NULL_TREE);
3534 void_ftype_any = build_function_type (void_type_node, NULL_TREE);
3535 void_ftype = build_function_type (void_type_node, endlink);
3536 void_ftype_int = build_function_type (void_type_node, int_endlink);
3537 void_ftype_ptr
3538 = build_function_type (void_type_node,
3539 tree_cons (NULL_TREE, ptr_type_node, endlink));
3541 float_ftype_float
3542 = build_function_type (float_type_node,
3543 tree_cons (NULL_TREE, float_type_node, endlink));
3545 double_ftype_double
3546 = build_function_type (double_type_node, double_endlink);
3548 ldouble_ftype_ldouble
3549 = build_function_type (long_double_type_node,
3550 tree_cons (NULL_TREE, long_double_type_node,
3551 endlink));
3553 double_ftype_double_double
3554 = build_function_type (double_type_node,
3555 tree_cons (NULL_TREE, double_type_node,
3556 double_endlink));
3558 int_ftype_int
3559 = build_function_type (integer_type_node, int_endlink);
3561 long_ftype_long
3562 = build_function_type (long_integer_type_node,
3563 tree_cons (NULL_TREE, long_integer_type_node,
3564 endlink));
3566 int_ftype_cptr_cptr_sizet
3567 = build_function_type (integer_type_node,
3568 tree_cons (NULL_TREE, const_ptr_type_node,
3569 tree_cons (NULL_TREE, const_ptr_type_node,
3570 tree_cons (NULL_TREE,
3571 sizetype,
3572 endlink))));
3574 /* Prototype for strcpy. */
3575 string_ftype_ptr_ptr
3576 = build_function_type (string_type_node,
3577 tree_cons (NULL_TREE, string_type_node,
3578 tree_cons (NULL_TREE,
3579 const_string_type_node,
3580 endlink)));
3582 traditional_len_type_node = (flag_traditional && ! cplus_mode
3583 ? integer_type_node : sizetype);
3584 traditional_len_endlink = tree_cons (NULL_TREE, traditional_len_type_node,
3585 endlink);
3587 /* Prototype for strcmp. */
3588 int_ftype_string_string
3589 = build_function_type (integer_type_node,
3590 tree_cons (NULL_TREE, const_string_type_node,
3591 tree_cons (NULL_TREE,
3592 const_string_type_node,
3593 endlink)));
3595 /* Prototype for strlen. */
3596 strlen_ftype
3597 = build_function_type (traditional_len_type_node,
3598 tree_cons (NULL_TREE, const_string_type_node,
3599 endlink));
3601 traditional_ptr_type_node = (flag_traditional && ! cplus_mode
3602 ? string_type_node : ptr_type_node);
3603 traditional_cptr_type_node = (flag_traditional && ! cplus_mode
3604 ? const_string_type_node : const_ptr_type_node);
3606 /* Prototype for memcpy. */
3607 memcpy_ftype
3608 = build_function_type (traditional_ptr_type_node,
3609 tree_cons (NULL_TREE, ptr_type_node,
3610 tree_cons (NULL_TREE, const_ptr_type_node,
3611 sizetype_endlink)));
3613 /* Prototype for memset. */
3614 memset_ftype
3615 = build_function_type (traditional_ptr_type_node,
3616 tree_cons (NULL_TREE, ptr_type_node,
3617 tree_cons (NULL_TREE, integer_type_node,
3618 tree_cons (NULL_TREE,
3619 sizetype,
3620 endlink))));
3622 /* Prototype for bzero. */
3623 bzero_ftype
3624 = build_function_type (void_type_node,
3625 tree_cons (NULL_TREE, traditional_ptr_type_node,
3626 traditional_len_endlink));
3628 /* Prototype for bcmp. */
3629 bcmp_ftype
3630 = build_function_type (integer_type_node,
3631 tree_cons (NULL_TREE, traditional_cptr_type_node,
3632 tree_cons (NULL_TREE,
3633 traditional_cptr_type_node,
3634 traditional_len_endlink)));
3636 builtin_function ("__builtin_constant_p", default_function_type,
3637 BUILT_IN_CONSTANT_P, BUILT_IN_NORMAL, NULL_PTR);
3639 builtin_function ("__builtin_return_address", ptr_ftype_unsigned,
3640 BUILT_IN_RETURN_ADDRESS, BUILT_IN_NORMAL, NULL_PTR);
3642 builtin_function ("__builtin_frame_address", ptr_ftype_unsigned,
3643 BUILT_IN_FRAME_ADDRESS, BUILT_IN_NORMAL, NULL_PTR);
3645 builtin_function ("__builtin_alloca", ptr_ftype_sizetype,
3646 BUILT_IN_ALLOCA, BUILT_IN_NORMAL, "alloca");
3647 builtin_function ("__builtin_ffs", int_ftype_int, BUILT_IN_FFS,
3648 BUILT_IN_NORMAL, NULL_PTR);
3649 /* Define alloca, ffs as builtins.
3650 Declare _exit just to mark it as volatile. */
3651 if (! no_builtins && ! no_nonansi_builtins)
3653 #ifndef SMALL_STACK
3654 temp = builtin_function ("alloca", ptr_ftype_sizetype,
3655 BUILT_IN_ALLOCA, BUILT_IN_NORMAL, NULL_PTR);
3656 /* Suppress error if redefined as a non-function. */
3657 DECL_BUILT_IN_NONANSI (temp) = 1;
3658 #endif
3659 temp = builtin_function ("ffs", int_ftype_int, BUILT_IN_FFS,
3660 BUILT_IN_NORMAL, NULL_PTR);
3661 /* Suppress error if redefined as a non-function. */
3662 DECL_BUILT_IN_NONANSI (temp) = 1;
3663 temp = builtin_function ("_exit", void_ftype_int,
3664 0, NOT_BUILT_IN, NULL_PTR);
3665 TREE_THIS_VOLATILE (temp) = 1;
3666 TREE_SIDE_EFFECTS (temp) = 1;
3667 /* Suppress error if redefined as a non-function. */
3668 DECL_BUILT_IN_NONANSI (temp) = 1;
3670 /* The system prototypes for these functions have many
3671 variations, so don't specify parameters to avoid conflicts.
3672 The expand_* functions check the argument types anyway. */
3673 temp = builtin_function ("bzero", void_ftype_any,
3674 BUILT_IN_BZERO, BUILT_IN_NORMAL, NULL_PTR);
3675 DECL_BUILT_IN_NONANSI (temp) = 1;
3676 temp = builtin_function ("bcmp", int_ftype_any,
3677 BUILT_IN_BCMP, BUILT_IN_NORMAL, NULL_PTR);
3678 DECL_BUILT_IN_NONANSI (temp) = 1;
3681 builtin_function ("__builtin_abs", int_ftype_int, BUILT_IN_ABS,
3682 BUILT_IN_NORMAL, NULL_PTR);
3683 builtin_function ("__builtin_fabsf", float_ftype_float, BUILT_IN_FABS,
3684 BUILT_IN_NORMAL, NULL_PTR);
3685 builtin_function ("__builtin_fabs", double_ftype_double, BUILT_IN_FABS,
3686 BUILT_IN_NORMAL, NULL_PTR);
3687 builtin_function ("__builtin_fabsl", ldouble_ftype_ldouble, BUILT_IN_FABS,
3688 BUILT_IN_NORMAL, NULL_PTR);
3689 builtin_function ("__builtin_labs", long_ftype_long, BUILT_IN_LABS,
3690 BUILT_IN_NORMAL, NULL_PTR);
3691 builtin_function ("__builtin_saveregs", ptr_ftype, BUILT_IN_SAVEREGS,
3692 BUILT_IN_NORMAL, NULL_PTR);
3693 builtin_function ("__builtin_classify_type", default_function_type,
3694 BUILT_IN_CLASSIFY_TYPE, BUILT_IN_NORMAL, NULL_PTR);
3695 builtin_function ("__builtin_next_arg", ptr_ftype, BUILT_IN_NEXT_ARG,
3696 BUILT_IN_NORMAL, NULL_PTR);
3697 builtin_function ("__builtin_args_info", int_ftype_int, BUILT_IN_ARGS_INFO,
3698 BUILT_IN_NORMAL, NULL_PTR);
3699 builtin_function ("__builtin_setjmp",
3700 build_function_type (integer_type_node,
3701 tree_cons (NULL_TREE, ptr_type_node,
3702 endlink)),
3703 BUILT_IN_SETJMP, BUILT_IN_NORMAL, NULL_PTR);
3704 builtin_function ("__builtin_longjmp",
3705 build_function_type (void_type_node,
3706 tree_cons (NULL_TREE, ptr_type_node,
3707 tree_cons (NULL_TREE,
3708 integer_type_node,
3709 endlink))),
3710 BUILT_IN_LONGJMP, BUILT_IN_NORMAL, NULL_PTR);
3711 builtin_function ("__builtin_trap", void_ftype, BUILT_IN_TRAP,
3712 BUILT_IN_NORMAL, NULL_PTR);
3714 /* ISO C99 IEEE Unordered compares. */
3715 builtin_function ("__builtin_isgreater", default_function_type,
3716 BUILT_IN_ISGREATER, BUILT_IN_NORMAL, NULL_PTR);
3717 builtin_function ("__builtin_isgreaterequal", default_function_type,
3718 BUILT_IN_ISGREATEREQUAL, BUILT_IN_NORMAL, NULL_PTR);
3719 builtin_function ("__builtin_isless", default_function_type,
3720 BUILT_IN_ISLESS, BUILT_IN_NORMAL, NULL_PTR);
3721 builtin_function ("__builtin_islessequal", default_function_type,
3722 BUILT_IN_ISLESSEQUAL, BUILT_IN_NORMAL, NULL_PTR);
3723 builtin_function ("__builtin_islessgreater", default_function_type,
3724 BUILT_IN_ISLESSGREATER, BUILT_IN_NORMAL, NULL_PTR);
3725 builtin_function ("__builtin_isunordered", default_function_type,
3726 BUILT_IN_ISUNORDERED, BUILT_IN_NORMAL, NULL_PTR);
3728 /* Untyped call and return. */
3729 builtin_function ("__builtin_apply_args", ptr_ftype,
3730 BUILT_IN_APPLY_ARGS, BUILT_IN_NORMAL, NULL_PTR);
3732 temp = tree_cons (NULL_TREE,
3733 build_pointer_type (build_function_type (void_type_node,
3734 NULL_TREE)),
3735 tree_cons (NULL_TREE,
3736 ptr_type_node,
3737 tree_cons (NULL_TREE,
3738 sizetype,
3739 endlink)));
3740 builtin_function ("__builtin_apply",
3741 build_function_type (ptr_type_node, temp),
3742 BUILT_IN_APPLY, BUILT_IN_NORMAL, NULL_PTR);
3743 builtin_function ("__builtin_return", void_ftype_ptr,
3744 BUILT_IN_RETURN, BUILT_IN_NORMAL, NULL_PTR);
3746 /* Support for varargs.h and stdarg.h. */
3747 builtin_function ("__builtin_varargs_start",
3748 build_function_type (void_type_node,
3749 tree_cons (NULL_TREE,
3750 va_list_ref_type_node,
3751 endlink)),
3752 BUILT_IN_VARARGS_START, BUILT_IN_NORMAL, NULL_PTR);
3754 builtin_function ("__builtin_stdarg_start",
3755 build_function_type (void_type_node,
3756 tree_cons (NULL_TREE,
3757 va_list_ref_type_node,
3758 NULL_TREE)),
3759 BUILT_IN_STDARG_START, BUILT_IN_NORMAL, NULL_PTR);
3761 builtin_function ("__builtin_va_end",
3762 build_function_type (void_type_node,
3763 tree_cons (NULL_TREE,
3764 va_list_ref_type_node,
3765 endlink)),
3766 BUILT_IN_VA_END, BUILT_IN_NORMAL, NULL_PTR);
3768 builtin_function ("__builtin_va_copy",
3769 build_function_type (void_type_node,
3770 tree_cons (NULL_TREE,
3771 va_list_ref_type_node,
3772 tree_cons (NULL_TREE,
3773 va_list_arg_type_node,
3774 endlink))),
3775 BUILT_IN_VA_COPY, BUILT_IN_NORMAL, NULL_PTR);
3777 /* ??? Ought to be `T __builtin_expect(T, T)' for any type T. */
3778 builtin_function ("__builtin_expect",
3779 build_function_type (long_integer_type_node,
3780 tree_cons (NULL_TREE,
3781 long_integer_type_node,
3782 tree_cons (NULL_TREE,
3783 long_integer_type_node,
3784 endlink))),
3785 BUILT_IN_EXPECT, BUILT_IN_NORMAL, NULL_PTR);
3787 /* Currently under experimentation. */
3788 builtin_function ("__builtin_memcpy", memcpy_ftype, BUILT_IN_MEMCPY,
3789 BUILT_IN_NORMAL, "memcpy");
3790 builtin_function ("__builtin_memcmp", int_ftype_cptr_cptr_sizet,
3791 BUILT_IN_MEMCMP, BUILT_IN_NORMAL, "memcmp");
3792 builtin_function ("__builtin_memset", memset_ftype,
3793 BUILT_IN_MEMSET, BUILT_IN_NORMAL, "memset");
3794 builtin_function ("__builtin_bzero", bzero_ftype,
3795 BUILT_IN_BZERO, BUILT_IN_NORMAL, "bzero");
3796 builtin_function ("__builtin_bcmp", bcmp_ftype,
3797 BUILT_IN_BCMP, BUILT_IN_NORMAL, "bcmp");
3798 builtin_function ("__builtin_strcmp", int_ftype_string_string,
3799 BUILT_IN_STRCMP, BUILT_IN_NORMAL, "strcmp");
3800 builtin_function ("__builtin_strcpy", string_ftype_ptr_ptr,
3801 BUILT_IN_STRCPY, BUILT_IN_NORMAL, "strcpy");
3802 builtin_function ("__builtin_strlen", strlen_ftype,
3803 BUILT_IN_STRLEN, BUILT_IN_NORMAL, "strlen");
3804 builtin_function ("__builtin_sqrtf", float_ftype_float,
3805 BUILT_IN_FSQRT, BUILT_IN_NORMAL, "sqrtf");
3806 builtin_function ("__builtin_fsqrt", double_ftype_double,
3807 BUILT_IN_FSQRT, BUILT_IN_NORMAL, "sqrt");
3808 builtin_function ("__builtin_sqrtl", ldouble_ftype_ldouble,
3809 BUILT_IN_FSQRT, BUILT_IN_NORMAL, "sqrtl");
3810 builtin_function ("__builtin_sinf", float_ftype_float,
3811 BUILT_IN_SIN, BUILT_IN_NORMAL, "sinf");
3812 builtin_function ("__builtin_sin", double_ftype_double,
3813 BUILT_IN_SIN, BUILT_IN_NORMAL, "sin");
3814 builtin_function ("__builtin_sinl", ldouble_ftype_ldouble,
3815 BUILT_IN_SIN, BUILT_IN_NORMAL, "sinl");
3816 builtin_function ("__builtin_cosf", float_ftype_float,
3817 BUILT_IN_COS, BUILT_IN_NORMAL, "cosf");
3818 builtin_function ("__builtin_cos", double_ftype_double,
3819 BUILT_IN_COS, BUILT_IN_NORMAL, "cos");
3820 builtin_function ("__builtin_cosl", ldouble_ftype_ldouble,
3821 BUILT_IN_COS, BUILT_IN_NORMAL, "cosl");
3823 if (! no_builtins)
3825 builtin_function ("abs", int_ftype_int, BUILT_IN_ABS,
3826 BUILT_IN_NORMAL, NULL_PTR);
3827 builtin_function ("fabsf", float_ftype_float, BUILT_IN_FABS,
3828 BUILT_IN_NORMAL, NULL_PTR);
3829 builtin_function ("fabs", double_ftype_double, BUILT_IN_FABS,
3830 BUILT_IN_NORMAL, NULL_PTR);
3831 builtin_function ("fabsl", ldouble_ftype_ldouble, BUILT_IN_FABS,
3832 BUILT_IN_NORMAL, NULL_PTR);
3833 builtin_function ("labs", long_ftype_long, BUILT_IN_LABS,
3834 BUILT_IN_NORMAL, NULL_PTR);
3835 builtin_function ("memcpy", memcpy_ftype, BUILT_IN_MEMCPY,
3836 BUILT_IN_NORMAL, NULL_PTR);
3837 builtin_function ("memcmp", int_ftype_cptr_cptr_sizet, BUILT_IN_MEMCMP,
3838 BUILT_IN_NORMAL, NULL_PTR);
3839 builtin_function ("memset", memset_ftype, BUILT_IN_MEMSET,
3840 BUILT_IN_NORMAL, NULL_PTR);
3841 builtin_function ("strcmp", int_ftype_string_string, BUILT_IN_STRCMP,
3842 BUILT_IN_NORMAL, NULL_PTR);
3843 builtin_function ("strcpy", string_ftype_ptr_ptr, BUILT_IN_STRCPY,
3844 BUILT_IN_NORMAL, NULL_PTR);
3845 builtin_function ("strlen", strlen_ftype, BUILT_IN_STRLEN,
3846 BUILT_IN_NORMAL, NULL_PTR);
3847 builtin_function ("sqrtf", float_ftype_float, BUILT_IN_FSQRT,
3848 BUILT_IN_NORMAL, NULL_PTR);
3849 builtin_function ("sqrt", double_ftype_double, BUILT_IN_FSQRT,
3850 BUILT_IN_NORMAL, NULL_PTR);
3851 builtin_function ("sqrtl", ldouble_ftype_ldouble, BUILT_IN_FSQRT,
3852 BUILT_IN_NORMAL, NULL_PTR);
3853 builtin_function ("sinf", float_ftype_float, BUILT_IN_SIN,
3854 BUILT_IN_NORMAL, NULL_PTR);
3855 builtin_function ("sin", double_ftype_double, BUILT_IN_SIN,
3856 BUILT_IN_NORMAL, NULL_PTR);
3857 builtin_function ("sinl", ldouble_ftype_ldouble, BUILT_IN_SIN,
3858 BUILT_IN_NORMAL, NULL_PTR);
3859 builtin_function ("cosf", float_ftype_float, BUILT_IN_COS,
3860 BUILT_IN_NORMAL, NULL_PTR);
3861 builtin_function ("cos", double_ftype_double, BUILT_IN_COS,
3862 BUILT_IN_NORMAL, NULL_PTR);
3863 builtin_function ("cosl", ldouble_ftype_ldouble, BUILT_IN_COS,
3864 BUILT_IN_NORMAL, NULL_PTR);
3866 /* Declare these functions volatile
3867 to avoid spurious "control drops through" warnings. */
3868 temp = builtin_function ("abort", cplus_mode ? void_ftype : void_ftype_any,
3869 0, NOT_BUILT_IN, NULL_PTR);
3870 TREE_THIS_VOLATILE (temp) = 1;
3871 TREE_SIDE_EFFECTS (temp) = 1;
3873 #if 0 /* ??? The C++ frontend used to do this. */
3874 /* Well, these are actually ANSI, but we can't set DECL_BUILT_IN on
3875 them... */
3876 DECL_BUILT_IN_NONANSI (temp) = 1;
3877 #endif
3878 temp = builtin_function ("exit",
3879 cplus_mode ? void_ftype_int : void_ftype_any,
3880 0, NOT_BUILT_IN, NULL_PTR);
3881 TREE_THIS_VOLATILE (temp) = 1;
3882 TREE_SIDE_EFFECTS (temp) = 1;
3884 #if 0 /* ??? The C++ frontend used to do this. */
3885 /* Well, these are actually ANSI, but we can't set DECL_BUILT_IN on
3886 them... */
3887 DECL_BUILT_IN_NONANSI (temp) = 1;
3888 #endif
3891 #if 0
3892 /* Support for these has not been written in either expand_builtin
3893 or build_function_call. */
3894 builtin_function ("__builtin_div", default_ftype, BUILT_IN_DIV,
3895 BUILT_IN_NORMAL, NULL_PTR);
3896 builtin_function ("__builtin_ldiv", default_ftype, BUILT_IN_LDIV,
3897 BUILT_IN_NORMAL, NULL_PTR);
3898 builtin_function ("__builtin_ffloor", double_ftype_double, BUILT_IN_FFLOOR,
3899 BUILT_IN_NORMAL, NULL_PTR);
3900 builtin_function ("__builtin_fceil", double_ftype_double, BUILT_IN_FCEIL,
3901 BUILT_IN_NORMAL, NULL_PTR);
3902 builtin_function ("__builtin_fmod", double_ftype_double_double,
3903 BUILT_IN_FMOD, BUILT_IN_NORMAL, NULL_PTR);
3904 builtin_function ("__builtin_frem", double_ftype_double_double,
3905 BUILT_IN_FREM, BUILT_IN_NORMAL, NULL_PTR);
3906 builtin_function ("__builtin_getexp", double_ftype_double, BUILT_IN_GETEXP,
3907 BUILT_IN_NORMAL, NULL_PTR);
3908 builtin_function ("__builtin_getman", double_ftype_double, BUILT_IN_GETMAN,
3909 BUILT_IN_NORMAL, NULL_PTR);
3910 #endif
3912 /* ??? Perhaps there's a better place to do this. But it is related
3913 to __builtin_va_arg, so it isn't that off-the-wall. */
3914 lang_type_promotes_to = simple_type_promotes_to;
3917 tree
3918 build_va_arg (expr, type)
3919 tree expr, type;
3921 return build1 (VA_ARG_EXPR, type, expr);
3924 /* Given a type, apply default promotions wrt unnamed function arguments
3925 and return the new type. Return NULL_TREE if no change. */
3926 /* ??? There is a function of the same name in the C++ front end that
3927 does something similar, but is more thorough and does not return NULL
3928 if no change. We could perhaps share code, but it would make the
3929 self_promoting_type property harder to identify. */
3931 tree
3932 simple_type_promotes_to (type)
3933 tree type;
3935 if (TYPE_MAIN_VARIANT (type) == float_type_node)
3936 return double_type_node;
3938 if (C_PROMOTING_INTEGER_TYPE_P (type))
3940 /* Traditionally, unsignedness is preserved in default promotions.
3941 Also preserve unsignedness if not really getting any wider. */
3942 if (TREE_UNSIGNED (type)
3943 && (flag_traditional
3944 || TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
3945 return unsigned_type_node;
3946 return integer_type_node;
3949 return NULL_TREE;
3952 /* Return 1 if PARMS specifies a fixed number of parameters
3953 and none of their types is affected by default promotions. */
3956 self_promoting_args_p (parms)
3957 tree parms;
3959 register tree t;
3960 for (t = parms; t; t = TREE_CHAIN (t))
3962 register tree type = TREE_VALUE (t);
3964 if (TREE_CHAIN (t) == 0 && type != void_type_node)
3965 return 0;
3967 if (type == 0)
3968 return 0;
3970 if (TYPE_MAIN_VARIANT (type) == float_type_node)
3971 return 0;
3973 if (C_PROMOTING_INTEGER_TYPE_P (type))
3974 return 0;
3976 return 1;
3979 /* Recognize certain built-in functions so we can make tree-codes
3980 other than CALL_EXPR. We do this when it enables fold-const.c
3981 to do something useful. */
3982 /* ??? By rights this should go in builtins.c, but only C and C++
3983 implement build_{binary,unary}_op. Not exactly sure what bits
3984 of functionality are actually needed from those functions, or
3985 where the similar functionality exists in the other front ends. */
3987 tree
3988 expand_tree_builtin (function, params, coerced_params)
3989 tree function, params, coerced_params;
3991 enum tree_code code;
3993 if (DECL_BUILT_IN_CLASS (function) != BUILT_IN_NORMAL)
3994 return NULL_TREE;
3996 switch (DECL_FUNCTION_CODE (function))
3998 case BUILT_IN_ABS:
3999 case BUILT_IN_LABS:
4000 case BUILT_IN_FABS:
4001 if (coerced_params == 0)
4002 return integer_zero_node;
4003 return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0);
4005 case BUILT_IN_ISGREATER:
4006 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
4007 code = UNLE_EXPR;
4008 else
4009 code = LE_EXPR;
4010 goto unordered_cmp;
4012 case BUILT_IN_ISGREATEREQUAL:
4013 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
4014 code = UNLT_EXPR;
4015 else
4016 code = LT_EXPR;
4017 goto unordered_cmp;
4019 case BUILT_IN_ISLESS:
4020 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
4021 code = UNGE_EXPR;
4022 else
4023 code = GE_EXPR;
4024 goto unordered_cmp;
4026 case BUILT_IN_ISLESSEQUAL:
4027 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
4028 code = UNGT_EXPR;
4029 else
4030 code = GT_EXPR;
4031 goto unordered_cmp;
4033 case BUILT_IN_ISLESSGREATER:
4034 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
4035 code = UNEQ_EXPR;
4036 else
4037 code = EQ_EXPR;
4038 goto unordered_cmp;
4040 case BUILT_IN_ISUNORDERED:
4041 if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT)
4042 return integer_zero_node;
4043 code = UNORDERED_EXPR;
4044 goto unordered_cmp;
4046 unordered_cmp:
4048 tree arg0, arg1;
4050 if (params == 0
4051 || TREE_CHAIN (params) == 0)
4053 error ("too few arguments to function `%s'",
4054 IDENTIFIER_POINTER (DECL_NAME (function)));
4055 return error_mark_node;
4057 else if (TREE_CHAIN (TREE_CHAIN (params)) != 0)
4059 error ("too many arguments to function `%s'",
4060 IDENTIFIER_POINTER (DECL_NAME (function)));
4061 return error_mark_node;
4064 arg0 = TREE_VALUE (params);
4065 arg1 = TREE_VALUE (TREE_CHAIN (params));
4066 arg0 = build_binary_op (code, arg0, arg1, 0);
4067 if (code != UNORDERED_EXPR)
4068 arg0 = build_unary_op (TRUTH_NOT_EXPR, arg0, 0);
4069 return arg0;
4071 break;
4073 default:
4074 break;
4077 return NULL_TREE;