oops - fixed typo in previous delta
[official-gcc.git] / gcc / c-common.c
blob815c8e1a601c9d53806627fd497ee1010d074464
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 /* Nonzero means the expression being parsed will never be evaluated.
139 This is a count, since unevaluated expressions can nest. */
140 int skip_evaluation;
142 enum attrs {A_PACKED, A_NOCOMMON, A_COMMON, A_NORETURN, A_CONST, A_T_UNION,
143 A_NO_CHECK_MEMORY_USAGE, A_NO_INSTRUMENT_FUNCTION,
144 A_CONSTRUCTOR, A_DESTRUCTOR, A_MODE, A_SECTION, A_ALIGNED,
145 A_UNUSED, A_FORMAT, A_FORMAT_ARG, A_WEAK, A_ALIAS, A_MALLOC,
146 A_NO_LIMIT_STACK};
148 enum format_type { printf_format_type, scanf_format_type,
149 strftime_format_type };
151 static void declare_hidden_char_array PARAMS ((const char *, const char *));
152 static void add_attribute PARAMS ((enum attrs, const char *,
153 int, int, int));
154 static void init_attributes PARAMS ((void));
155 static void record_function_format PARAMS ((tree, tree, enum format_type,
156 int, int));
157 static void record_international_format PARAMS ((tree, tree, int));
158 static tree c_find_base_decl PARAMS ((tree));
159 static int default_valid_lang_attribute PARAMS ((tree, tree, tree, tree));
161 /* Keep a stack of if statements. We record the number of compound
162 statements seen up to the if keyword, as well as the line number
163 and file of the if. If a potentially ambiguous else is seen, that
164 fact is recorded; the warning is issued when we can be sure that
165 the enclosing if statement does not have an else branch. */
166 typedef struct
168 int compstmt_count;
169 int line;
170 const char *file;
171 int needs_warning;
172 } if_elt;
173 static void tfaff PARAMS ((void));
175 static if_elt *if_stack;
177 /* Amount of space in the if statement stack. */
178 static int if_stack_space = 0;
180 /* Stack pointer. */
181 static int if_stack_pointer = 0;
183 /* Generate RTL for the start of an if-then, and record the start of it
184 for ambiguous else detection. */
186 void
187 c_expand_start_cond (cond, exitflag, compstmt_count)
188 tree cond;
189 int exitflag;
190 int compstmt_count;
192 /* Make sure there is enough space on the stack. */
193 if (if_stack_space == 0)
195 if_stack_space = 10;
196 if_stack = (if_elt *)xmalloc (10 * sizeof (if_elt));
198 else if (if_stack_space == if_stack_pointer)
200 if_stack_space += 10;
201 if_stack = (if_elt *)xrealloc (if_stack, if_stack_space * sizeof (if_elt));
204 /* Record this if statement. */
205 if_stack[if_stack_pointer].compstmt_count = compstmt_count;
206 if_stack[if_stack_pointer].file = input_filename;
207 if_stack[if_stack_pointer].line = lineno;
208 if_stack[if_stack_pointer].needs_warning = 0;
209 if_stack_pointer++;
211 expand_start_cond (cond, exitflag);
214 /* Generate RTL for the end of an if-then. Optionally warn if a nested
215 if statement had an ambiguous else clause. */
217 void
218 c_expand_end_cond ()
220 if_stack_pointer--;
221 if (if_stack[if_stack_pointer].needs_warning)
222 warning_with_file_and_line (if_stack[if_stack_pointer].file,
223 if_stack[if_stack_pointer].line,
224 "suggest explicit braces to avoid ambiguous `else'");
225 expand_end_cond ();
228 /* Generate RTL between the then-clause and the else-clause
229 of an if-then-else. */
231 void
232 c_expand_start_else ()
234 /* An ambiguous else warning must be generated for the enclosing if
235 statement, unless we see an else branch for that one, too. */
236 if (warn_parentheses
237 && if_stack_pointer > 1
238 && (if_stack[if_stack_pointer - 1].compstmt_count
239 == if_stack[if_stack_pointer - 2].compstmt_count))
240 if_stack[if_stack_pointer - 2].needs_warning = 1;
242 /* Even if a nested if statement had an else branch, it can't be
243 ambiguous if this one also has an else. So don't warn in that
244 case. Also don't warn for any if statements nested in this else. */
245 if_stack[if_stack_pointer - 1].needs_warning = 0;
246 if_stack[if_stack_pointer - 1].compstmt_count--;
248 expand_start_else ();
251 /* Make bindings for __FUNCTION__, __PRETTY_FUNCTION__, and __func__. */
253 void
254 declare_function_name ()
256 const char *name, *printable_name;
258 if (current_function_decl == NULL)
260 name = "";
261 printable_name = "top level";
263 else
265 /* Allow functions to be nameless (such as artificial ones). */
266 if (DECL_NAME (current_function_decl))
267 name = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
268 else
269 name = "";
270 printable_name = (*decl_printable_name) (current_function_decl, 2);
273 declare_hidden_char_array ("__FUNCTION__", name);
274 declare_hidden_char_array ("__PRETTY_FUNCTION__", printable_name);
275 /* The ISO C people "of course" couldn't use __FUNCTION__ in the
276 ISO C 99 standard; instead a new variable is invented. */
277 declare_hidden_char_array ("__func__", name);
280 static void
281 declare_hidden_char_array (name, value)
282 const char *name, *value;
284 tree decl, type, init;
285 int vlen;
287 /* If the default size of char arrays isn't big enough for the name,
288 or if we want to give warnings for large objects, make a bigger one. */
289 vlen = strlen (value) + 1;
290 type = char_array_type_node;
291 if (TREE_INT_CST_LOW (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) < vlen
292 || warn_larger_than)
293 type = build_array_type (char_type_node,
294 build_index_type (build_int_2 (vlen, 0)));
295 decl = build_decl (VAR_DECL, get_identifier (name), type);
296 TREE_STATIC (decl) = 1;
297 TREE_READONLY (decl) = 1;
298 TREE_ASM_WRITTEN (decl) = 1;
299 DECL_SOURCE_LINE (decl) = 0;
300 DECL_ARTIFICIAL (decl) = 1;
301 DECL_IN_SYSTEM_HEADER (decl) = 1;
302 DECL_IGNORED_P (decl) = 1;
303 init = build_string (vlen, value);
304 TREE_TYPE (init) = type;
305 DECL_INITIAL (decl) = init;
306 finish_decl (pushdecl (decl), init, NULL_TREE);
309 /* Given a chain of STRING_CST nodes,
310 concatenate them into one STRING_CST
311 and give it a suitable array-of-chars data type. */
313 tree
314 combine_strings (strings)
315 tree strings;
317 register tree value, t;
318 register int length = 1;
319 int wide_length = 0;
320 int wide_flag = 0;
321 int wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
322 int nchars;
324 if (TREE_CHAIN (strings))
326 /* More than one in the chain, so concatenate. */
327 register char *p, *q;
329 /* Don't include the \0 at the end of each substring,
330 except for the last one.
331 Count wide strings and ordinary strings separately. */
332 for (t = strings; t; t = TREE_CHAIN (t))
334 if (TREE_TYPE (t) == wchar_array_type_node)
336 wide_length += (TREE_STRING_LENGTH (t) - wchar_bytes);
337 wide_flag = 1;
339 else
340 length += (TREE_STRING_LENGTH (t) - 1);
343 /* If anything is wide, the non-wides will be converted,
344 which makes them take more space. */
345 if (wide_flag)
346 length = length * wchar_bytes + wide_length;
348 p = ggc_alloc_string (NULL, length);
350 /* Copy the individual strings into the new combined string.
351 If the combined string is wide, convert the chars to ints
352 for any individual strings that are not wide. */
354 q = p;
355 for (t = strings; t; t = TREE_CHAIN (t))
357 int len = (TREE_STRING_LENGTH (t)
358 - ((TREE_TYPE (t) == wchar_array_type_node)
359 ? wchar_bytes : 1));
360 if ((TREE_TYPE (t) == wchar_array_type_node) == wide_flag)
362 memcpy (q, TREE_STRING_POINTER (t), len);
363 q += len;
365 else
367 int i;
368 for (i = 0; i < len; i++)
370 if (WCHAR_TYPE_SIZE == HOST_BITS_PER_SHORT)
371 ((short *) q)[i] = TREE_STRING_POINTER (t)[i];
372 else
373 ((int *) q)[i] = TREE_STRING_POINTER (t)[i];
375 q += len * wchar_bytes;
378 if (wide_flag)
380 int i;
381 for (i = 0; i < wchar_bytes; i++)
382 *q++ = 0;
384 else
385 *q = 0;
387 value = make_node (STRING_CST);
388 TREE_STRING_POINTER (value) = p;
389 TREE_STRING_LENGTH (value) = length;
391 else
393 value = strings;
394 length = TREE_STRING_LENGTH (value);
395 if (TREE_TYPE (value) == wchar_array_type_node)
396 wide_flag = 1;
399 /* Compute the number of elements, for the array type. */
400 nchars = wide_flag ? length / wchar_bytes : length;
402 /* Create the array type for the string constant.
403 -Wwrite-strings says make the string constant an array of const char
404 so that copying it to a non-const pointer will get a warning.
405 For C++, this is the standard behavior. */
406 if (flag_const_strings
407 && (! flag_traditional && ! flag_writable_strings))
409 tree elements
410 = build_type_variant (wide_flag ? wchar_type_node : char_type_node,
411 1, 0);
412 TREE_TYPE (value)
413 = build_array_type (elements,
414 build_index_type (build_int_2 (nchars - 1, 0)));
416 else
417 TREE_TYPE (value)
418 = build_array_type (wide_flag ? wchar_type_node : char_type_node,
419 build_index_type (build_int_2 (nchars - 1, 0)));
421 TREE_CONSTANT (value) = 1;
422 TREE_READONLY (value) = ! flag_writable_strings;
423 TREE_STATIC (value) = 1;
424 return value;
427 /* To speed up processing of attributes, we maintain an array of
428 IDENTIFIER_NODES and the corresponding attribute types. */
430 /* Array to hold attribute information. */
432 static struct {enum attrs id; tree name; int min, max, decl_req;} attrtab[50];
434 static int attrtab_idx = 0;
436 /* Add an entry to the attribute table above. */
438 static void
439 add_attribute (id, string, min_len, max_len, decl_req)
440 enum attrs id;
441 const char *string;
442 int min_len, max_len;
443 int decl_req;
445 char buf[100];
447 attrtab[attrtab_idx].id = id;
448 attrtab[attrtab_idx].name = get_identifier (string);
449 attrtab[attrtab_idx].min = min_len;
450 attrtab[attrtab_idx].max = max_len;
451 attrtab[attrtab_idx++].decl_req = decl_req;
453 sprintf (buf, "__%s__", string);
455 attrtab[attrtab_idx].id = id;
456 attrtab[attrtab_idx].name = get_identifier (buf);
457 attrtab[attrtab_idx].min = min_len;
458 attrtab[attrtab_idx].max = max_len;
459 attrtab[attrtab_idx++].decl_req = decl_req;
462 /* Initialize attribute table. */
464 static void
465 init_attributes ()
467 add_attribute (A_PACKED, "packed", 0, 0, 0);
468 add_attribute (A_NOCOMMON, "nocommon", 0, 0, 1);
469 add_attribute (A_COMMON, "common", 0, 0, 1);
470 add_attribute (A_NORETURN, "noreturn", 0, 0, 1);
471 add_attribute (A_NORETURN, "volatile", 0, 0, 1);
472 add_attribute (A_UNUSED, "unused", 0, 0, 0);
473 add_attribute (A_CONST, "const", 0, 0, 1);
474 add_attribute (A_T_UNION, "transparent_union", 0, 0, 0);
475 add_attribute (A_CONSTRUCTOR, "constructor", 0, 0, 1);
476 add_attribute (A_DESTRUCTOR, "destructor", 0, 0, 1);
477 add_attribute (A_MODE, "mode", 1, 1, 1);
478 add_attribute (A_SECTION, "section", 1, 1, 1);
479 add_attribute (A_ALIGNED, "aligned", 0, 1, 0);
480 add_attribute (A_FORMAT, "format", 3, 3, 1);
481 add_attribute (A_FORMAT_ARG, "format_arg", 1, 1, 1);
482 add_attribute (A_WEAK, "weak", 0, 0, 1);
483 add_attribute (A_ALIAS, "alias", 1, 1, 1);
484 add_attribute (A_NO_INSTRUMENT_FUNCTION, "no_instrument_function", 0, 0, 1);
485 add_attribute (A_NO_CHECK_MEMORY_USAGE, "no_check_memory_usage", 0, 0, 1);
486 add_attribute (A_MALLOC, "malloc", 0, 0, 1);
487 add_attribute (A_NO_LIMIT_STACK, "no_stack_limit", 0, 0, 1);
490 /* Default implementation of valid_lang_attribute, below. By default, there
491 are no language-specific attributes. */
493 static int
494 default_valid_lang_attribute (attr_name, attr_args, decl, type)
495 tree attr_name ATTRIBUTE_UNUSED;
496 tree attr_args ATTRIBUTE_UNUSED;
497 tree decl ATTRIBUTE_UNUSED;
498 tree type ATTRIBUTE_UNUSED;
500 return 0;
503 /* Return a 1 if ATTR_NAME and ATTR_ARGS denote a valid language-specific
504 attribute for either declaration DECL or type TYPE and 0 otherwise. */
506 int (*valid_lang_attribute) PARAMS ((tree, tree, tree, tree))
507 = default_valid_lang_attribute;
509 /* Process the attributes listed in ATTRIBUTES and PREFIX_ATTRIBUTES
510 and install them in NODE, which is either a DECL (including a TYPE_DECL)
511 or a TYPE. PREFIX_ATTRIBUTES can appear after the declaration specifiers
512 and declaration modifiers but before the declaration proper. */
514 void
515 decl_attributes (node, attributes, prefix_attributes)
516 tree node, attributes, prefix_attributes;
518 tree decl = 0, type = 0;
519 int is_type = 0;
520 tree a;
522 if (attrtab_idx == 0)
523 init_attributes ();
525 if (TREE_CODE_CLASS (TREE_CODE (node)) == 'd')
527 decl = node;
528 type = TREE_TYPE (decl);
529 is_type = TREE_CODE (node) == TYPE_DECL;
531 else if (TREE_CODE_CLASS (TREE_CODE (node)) == 't')
532 type = node, is_type = 1;
534 #ifdef PRAGMA_INSERT_ATTRIBUTES
535 /* If the code in c-pragma.c wants to insert some attributes then
536 allow it to do so. Do this before allowing machine back ends to
537 insert attributes, so that they have the opportunity to override
538 anything done here. */
539 PRAGMA_INSERT_ATTRIBUTES (node, & attributes, & prefix_attributes);
540 #endif
542 #ifdef INSERT_ATTRIBUTES
543 INSERT_ATTRIBUTES (node, & attributes, & prefix_attributes);
544 #endif
546 attributes = chainon (prefix_attributes, attributes);
548 for (a = attributes; a; a = TREE_CHAIN (a))
550 tree name = TREE_PURPOSE (a);
551 tree args = TREE_VALUE (a);
552 int i;
553 enum attrs id;
555 for (i = 0; i < attrtab_idx; i++)
556 if (attrtab[i].name == name)
557 break;
559 if (i == attrtab_idx)
561 if (! valid_machine_attribute (name, args, decl, type)
562 && ! (* valid_lang_attribute) (name, args, decl, type))
563 warning ("`%s' attribute directive ignored",
564 IDENTIFIER_POINTER (name));
565 else if (decl != 0)
566 type = TREE_TYPE (decl);
567 continue;
569 else if (attrtab[i].decl_req && decl == 0)
571 warning ("`%s' attribute does not apply to types",
572 IDENTIFIER_POINTER (name));
573 continue;
575 else if (list_length (args) < attrtab[i].min
576 || list_length (args) > attrtab[i].max)
578 error ("wrong number of arguments specified for `%s' attribute",
579 IDENTIFIER_POINTER (name));
580 continue;
583 id = attrtab[i].id;
584 switch (id)
586 case A_PACKED:
587 if (is_type)
588 TYPE_PACKED (type) = 1;
589 else if (TREE_CODE (decl) == FIELD_DECL)
590 DECL_PACKED (decl) = 1;
591 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
592 used for DECL_REGISTER. It wouldn't mean anything anyway. */
593 else
594 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
595 break;
597 case A_NOCOMMON:
598 if (TREE_CODE (decl) == VAR_DECL)
599 DECL_COMMON (decl) = 0;
600 else
601 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
602 break;
604 case A_COMMON:
605 if (TREE_CODE (decl) == VAR_DECL)
606 DECL_COMMON (decl) = 1;
607 else
608 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
609 break;
611 case A_NORETURN:
612 if (TREE_CODE (decl) == FUNCTION_DECL)
613 TREE_THIS_VOLATILE (decl) = 1;
614 else if (TREE_CODE (type) == POINTER_TYPE
615 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
616 TREE_TYPE (decl) = type
617 = build_pointer_type
618 (build_type_variant (TREE_TYPE (type),
619 TREE_READONLY (TREE_TYPE (type)), 1));
620 else
621 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
622 break;
624 case A_MALLOC:
625 if (TREE_CODE (decl) == FUNCTION_DECL)
626 DECL_IS_MALLOC (decl) = 1;
627 else
628 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
629 break;
631 case A_UNUSED:
632 if (is_type)
633 TREE_USED (type) = 1;
634 else if (TREE_CODE (decl) == PARM_DECL
635 || TREE_CODE (decl) == VAR_DECL
636 || TREE_CODE (decl) == FUNCTION_DECL
637 || TREE_CODE (decl) == LABEL_DECL)
638 TREE_USED (decl) = 1;
639 else
640 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
641 break;
643 case A_CONST:
644 if (TREE_CODE (decl) == FUNCTION_DECL)
645 TREE_READONLY (decl) = 1;
646 else if (TREE_CODE (type) == POINTER_TYPE
647 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
648 TREE_TYPE (decl) = type
649 = build_pointer_type
650 (build_type_variant (TREE_TYPE (type), 1,
651 TREE_THIS_VOLATILE (TREE_TYPE (type))));
652 else
653 warning ( "`%s' attribute ignored", IDENTIFIER_POINTER (name));
654 break;
656 case A_T_UNION:
657 if (is_type
658 && TREE_CODE (type) == UNION_TYPE
659 && (decl == 0
660 || (TYPE_FIELDS (type) != 0
661 && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))))
662 TYPE_TRANSPARENT_UNION (type) = 1;
663 else if (decl != 0 && TREE_CODE (decl) == PARM_DECL
664 && TREE_CODE (type) == UNION_TYPE
665 && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))
666 DECL_TRANSPARENT_UNION (decl) = 1;
667 else
668 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
669 break;
671 case A_CONSTRUCTOR:
672 if (TREE_CODE (decl) == FUNCTION_DECL
673 && TREE_CODE (type) == FUNCTION_TYPE
674 && decl_function_context (decl) == 0)
676 DECL_STATIC_CONSTRUCTOR (decl) = 1;
677 TREE_USED (decl) = 1;
679 else
680 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
681 break;
683 case A_DESTRUCTOR:
684 if (TREE_CODE (decl) == FUNCTION_DECL
685 && TREE_CODE (type) == FUNCTION_TYPE
686 && decl_function_context (decl) == 0)
688 DECL_STATIC_DESTRUCTOR (decl) = 1;
689 TREE_USED (decl) = 1;
691 else
692 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
693 break;
695 case A_MODE:
696 if (TREE_CODE (TREE_VALUE (args)) != IDENTIFIER_NODE)
697 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
698 else
700 int j;
701 const char *p = IDENTIFIER_POINTER (TREE_VALUE (args));
702 int len = strlen (p);
703 enum machine_mode mode = VOIDmode;
704 tree typefm;
706 if (len > 4 && p[0] == '_' && p[1] == '_'
707 && p[len - 1] == '_' && p[len - 2] == '_')
709 char *newp = (char *) alloca (len - 1);
711 strcpy (newp, &p[2]);
712 newp[len - 4] = '\0';
713 p = newp;
716 /* Give this decl a type with the specified mode.
717 First check for the special modes. */
718 if (! strcmp (p, "byte"))
719 mode = byte_mode;
720 else if (!strcmp (p, "word"))
721 mode = word_mode;
722 else if (! strcmp (p, "pointer"))
723 mode = ptr_mode;
724 else
725 for (j = 0; j < NUM_MACHINE_MODES; j++)
726 if (!strcmp (p, GET_MODE_NAME (j)))
727 mode = (enum machine_mode) j;
729 if (mode == VOIDmode)
730 error ("unknown machine mode `%s'", p);
731 else if (0 == (typefm = type_for_mode (mode,
732 TREE_UNSIGNED (type))))
733 error ("no data type for mode `%s'", p);
734 else
736 TREE_TYPE (decl) = type = typefm;
737 DECL_SIZE (decl) = DECL_SIZE_UNIT (decl) = 0;
738 layout_decl (decl, 0);
741 break;
743 case A_SECTION:
744 #ifdef ASM_OUTPUT_SECTION_NAME
745 if ((TREE_CODE (decl) == FUNCTION_DECL
746 || TREE_CODE (decl) == VAR_DECL)
747 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
749 if (TREE_CODE (decl) == VAR_DECL
750 && current_function_decl != NULL_TREE
751 && ! TREE_STATIC (decl))
752 error_with_decl (decl,
753 "section attribute cannot be specified for local variables");
754 /* The decl may have already been given a section attribute from
755 a previous declaration. Ensure they match. */
756 else if (DECL_SECTION_NAME (decl) != NULL_TREE
757 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
758 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
759 error_with_decl (node,
760 "section of `%s' conflicts with previous declaration");
761 else
762 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
764 else
765 error_with_decl (node,
766 "section attribute not allowed for `%s'");
767 #else
768 error_with_decl (node,
769 "section attributes are not supported for this target");
770 #endif
771 break;
773 case A_ALIGNED:
775 tree align_expr
776 = (args ? TREE_VALUE (args)
777 : size_int (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
778 int align;
780 /* Strip any NOPs of any kind. */
781 while (TREE_CODE (align_expr) == NOP_EXPR
782 || TREE_CODE (align_expr) == CONVERT_EXPR
783 || TREE_CODE (align_expr) == NON_LVALUE_EXPR)
784 align_expr = TREE_OPERAND (align_expr, 0);
786 if (TREE_CODE (align_expr) != INTEGER_CST)
788 error ("requested alignment is not a constant");
789 continue;
792 align = TREE_INT_CST_LOW (align_expr) * BITS_PER_UNIT;
794 if (exact_log2 (align) == -1)
795 error ("requested alignment is not a power of 2");
796 else if (is_type)
797 TYPE_ALIGN (type) = align;
798 else if (TREE_CODE (decl) != VAR_DECL
799 && TREE_CODE (decl) != FIELD_DECL)
800 error_with_decl (decl,
801 "alignment may not be specified for `%s'");
802 else
803 DECL_ALIGN (decl) = align;
805 break;
807 case A_FORMAT:
809 tree format_type_id = TREE_VALUE (args);
810 tree format_num_expr = TREE_VALUE (TREE_CHAIN (args));
811 tree first_arg_num_expr
812 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)));
813 int format_num;
814 int first_arg_num;
815 enum format_type format_type;
816 tree argument;
817 int arg_num;
819 if (TREE_CODE (decl) != FUNCTION_DECL)
821 error_with_decl (decl,
822 "argument format specified for non-function `%s'");
823 continue;
826 if (TREE_CODE (format_type_id) != IDENTIFIER_NODE)
828 error ("unrecognized format specifier");
829 continue;
831 else
833 const char *p = IDENTIFIER_POINTER (format_type_id);
835 if (!strcmp (p, "printf") || !strcmp (p, "__printf__"))
836 format_type = printf_format_type;
837 else if (!strcmp (p, "scanf") || !strcmp (p, "__scanf__"))
838 format_type = scanf_format_type;
839 else if (!strcmp (p, "strftime")
840 || !strcmp (p, "__strftime__"))
841 format_type = strftime_format_type;
842 else
844 warning ("`%s' is an unrecognized format function type", p);
845 continue;
849 /* Strip any conversions from the string index and first arg number
850 and verify they are constants. */
851 while (TREE_CODE (format_num_expr) == NOP_EXPR
852 || TREE_CODE (format_num_expr) == CONVERT_EXPR
853 || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
854 format_num_expr = TREE_OPERAND (format_num_expr, 0);
856 while (TREE_CODE (first_arg_num_expr) == NOP_EXPR
857 || TREE_CODE (first_arg_num_expr) == CONVERT_EXPR
858 || TREE_CODE (first_arg_num_expr) == NON_LVALUE_EXPR)
859 first_arg_num_expr = TREE_OPERAND (first_arg_num_expr, 0);
861 if (TREE_CODE (format_num_expr) != INTEGER_CST
862 || TREE_CODE (first_arg_num_expr) != INTEGER_CST)
864 error ("format string has non-constant operand number");
865 continue;
868 format_num = TREE_INT_CST_LOW (format_num_expr);
869 first_arg_num = TREE_INT_CST_LOW (first_arg_num_expr);
870 if (first_arg_num != 0 && first_arg_num <= format_num)
872 error ("format string arg follows the args to be formatted");
873 continue;
876 /* If a parameter list is specified, verify that the format_num
877 argument is actually a string, in case the format attribute
878 is in error. */
879 argument = TYPE_ARG_TYPES (type);
880 if (argument)
882 for (arg_num = 1; ; ++arg_num)
884 if (argument == 0 || arg_num == format_num)
885 break;
886 argument = TREE_CHAIN (argument);
888 if (! argument
889 || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
890 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
891 != char_type_node))
893 error ("format string arg not a string type");
894 continue;
896 if (first_arg_num != 0)
898 /* Verify that first_arg_num points to the last arg,
899 the ... */
900 while (argument)
901 arg_num++, argument = TREE_CHAIN (argument);
902 if (arg_num != first_arg_num)
904 error ("args to be formatted is not ...");
905 continue;
910 record_function_format (DECL_NAME (decl),
911 DECL_ASSEMBLER_NAME (decl),
912 format_type, format_num, first_arg_num);
913 break;
916 case A_FORMAT_ARG:
918 tree format_num_expr = TREE_VALUE (args);
919 int format_num, arg_num;
920 tree argument;
922 if (TREE_CODE (decl) != FUNCTION_DECL)
924 error_with_decl (decl,
925 "argument format specified for non-function `%s'");
926 continue;
929 /* Strip any conversions from the first arg number and verify it
930 is a constant. */
931 while (TREE_CODE (format_num_expr) == NOP_EXPR
932 || TREE_CODE (format_num_expr) == CONVERT_EXPR
933 || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
934 format_num_expr = TREE_OPERAND (format_num_expr, 0);
936 if (TREE_CODE (format_num_expr) != INTEGER_CST)
938 error ("format string has non-constant operand number");
939 continue;
942 format_num = TREE_INT_CST_LOW (format_num_expr);
944 /* If a parameter list is specified, verify that the format_num
945 argument is actually a string, in case the format attribute
946 is in error. */
947 argument = TYPE_ARG_TYPES (type);
948 if (argument)
950 for (arg_num = 1; ; ++arg_num)
952 if (argument == 0 || arg_num == format_num)
953 break;
954 argument = TREE_CHAIN (argument);
956 if (! argument
957 || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
958 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
959 != char_type_node))
961 error ("format string arg not a string type");
962 continue;
966 if (TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) != POINTER_TYPE
967 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (TREE_TYPE (decl))))
968 != char_type_node))
970 error ("function does not return string type");
971 continue;
974 record_international_format (DECL_NAME (decl),
975 DECL_ASSEMBLER_NAME (decl),
976 format_num);
977 break;
980 case A_WEAK:
981 declare_weak (decl);
982 break;
984 case A_ALIAS:
985 if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
986 || (TREE_CODE (decl) != FUNCTION_DECL && ! DECL_EXTERNAL (decl)))
987 error_with_decl (decl,
988 "`%s' defined both normally and as an alias");
989 else if (decl_function_context (decl) == 0)
991 tree id;
993 id = TREE_VALUE (args);
994 if (TREE_CODE (id) != STRING_CST)
996 error ("alias arg not a string");
997 break;
999 id = get_identifier (TREE_STRING_POINTER (id));
1001 if (TREE_CODE (decl) == FUNCTION_DECL)
1002 DECL_INITIAL (decl) = error_mark_node;
1003 else
1004 DECL_EXTERNAL (decl) = 0;
1005 assemble_alias (decl, id);
1007 else
1008 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
1009 break;
1011 case A_NO_CHECK_MEMORY_USAGE:
1012 if (TREE_CODE (decl) != FUNCTION_DECL)
1014 error_with_decl (decl,
1015 "`%s' attribute applies only to functions",
1016 IDENTIFIER_POINTER (name));
1018 else if (DECL_INITIAL (decl))
1020 error_with_decl (decl,
1021 "can't set `%s' attribute after definition",
1022 IDENTIFIER_POINTER (name));
1024 else
1025 DECL_NO_CHECK_MEMORY_USAGE (decl) = 1;
1026 break;
1028 case A_NO_INSTRUMENT_FUNCTION:
1029 if (TREE_CODE (decl) != FUNCTION_DECL)
1031 error_with_decl (decl,
1032 "`%s' attribute applies only to functions",
1033 IDENTIFIER_POINTER (name));
1035 else if (DECL_INITIAL (decl))
1037 error_with_decl (decl,
1038 "can't set `%s' attribute after definition",
1039 IDENTIFIER_POINTER (name));
1041 else
1042 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
1043 break;
1045 case A_NO_LIMIT_STACK:
1046 if (TREE_CODE (decl) != FUNCTION_DECL)
1048 error_with_decl (decl,
1049 "`%s' attribute applies only to functions",
1050 IDENTIFIER_POINTER (name));
1052 else if (DECL_INITIAL (decl))
1054 error_with_decl (decl,
1055 "can't set `%s' attribute after definition",
1056 IDENTIFIER_POINTER (name));
1058 else
1059 DECL_NO_LIMIT_STACK (decl) = 1;
1060 break;
1065 /* Split SPECS_ATTRS, a list of declspecs and prefix attributes, into two
1066 lists. SPECS_ATTRS may also be just a typespec (eg: RECORD_TYPE).
1068 The head of the declspec list is stored in DECLSPECS.
1069 The head of the attribute list is stored in PREFIX_ATTRIBUTES.
1071 Note that attributes in SPECS_ATTRS are stored in the TREE_PURPOSE of
1072 the list elements. We drop the containing TREE_LIST nodes and link the
1073 resulting attributes together the way decl_attributes expects them. */
1075 void
1076 split_specs_attrs (specs_attrs, declspecs, prefix_attributes)
1077 tree specs_attrs;
1078 tree *declspecs, *prefix_attributes;
1080 tree t, s, a, next, specs, attrs;
1082 /* This can happen after an __extension__ in pedantic mode. */
1083 if (specs_attrs != NULL_TREE
1084 && TREE_CODE (specs_attrs) == INTEGER_CST)
1086 *declspecs = NULL_TREE;
1087 *prefix_attributes = NULL_TREE;
1088 return;
1091 /* This can happen in c++ (eg: decl: typespec initdecls ';'). */
1092 if (specs_attrs != NULL_TREE
1093 && TREE_CODE (specs_attrs) != TREE_LIST)
1095 *declspecs = specs_attrs;
1096 *prefix_attributes = NULL_TREE;
1097 return;
1100 /* Remember to keep the lists in the same order, element-wise. */
1102 specs = s = NULL_TREE;
1103 attrs = a = NULL_TREE;
1104 for (t = specs_attrs; t; t = next)
1106 next = TREE_CHAIN (t);
1107 /* Declspecs have a non-NULL TREE_VALUE. */
1108 if (TREE_VALUE (t) != NULL_TREE)
1110 if (specs == NULL_TREE)
1111 specs = s = t;
1112 else
1114 TREE_CHAIN (s) = t;
1115 s = t;
1118 else
1120 if (attrs == NULL_TREE)
1121 attrs = a = TREE_PURPOSE (t);
1122 else
1124 TREE_CHAIN (a) = TREE_PURPOSE (t);
1125 a = TREE_PURPOSE (t);
1127 /* More attrs can be linked here, move A to the end. */
1128 while (TREE_CHAIN (a) != NULL_TREE)
1129 a = TREE_CHAIN (a);
1133 /* Terminate the lists. */
1134 if (s != NULL_TREE)
1135 TREE_CHAIN (s) = NULL_TREE;
1136 if (a != NULL_TREE)
1137 TREE_CHAIN (a) = NULL_TREE;
1139 /* All done. */
1140 *declspecs = specs;
1141 *prefix_attributes = attrs;
1144 /* Strip attributes from SPECS_ATTRS, a list of declspecs and attributes.
1145 This function is used by the parser when a rule will accept attributes
1146 in a particular position, but we don't want to support that just yet.
1148 A warning is issued for every ignored attribute. */
1150 tree
1151 strip_attrs (specs_attrs)
1152 tree specs_attrs;
1154 tree specs, attrs;
1156 split_specs_attrs (specs_attrs, &specs, &attrs);
1158 while (attrs)
1160 warning ("`%s' attribute ignored",
1161 IDENTIFIER_POINTER (TREE_PURPOSE (attrs)));
1162 attrs = TREE_CHAIN (attrs);
1165 return specs;
1168 /* Check a printf/fprintf/sprintf/scanf/fscanf/sscanf format against
1169 a parameter list. */
1171 #define T_I &integer_type_node
1172 #define T_L &long_integer_type_node
1173 #define T_LL &long_long_integer_type_node
1174 #define T_S &short_integer_type_node
1175 #define T_UI &unsigned_type_node
1176 #define T_UL &long_unsigned_type_node
1177 #define T_ULL &long_long_unsigned_type_node
1178 #define T_US &short_unsigned_type_node
1179 #define T_F &float_type_node
1180 #define T_D &double_type_node
1181 #define T_LD &long_double_type_node
1182 #define T_C &char_type_node
1183 #define T_UC &unsigned_char_type_node
1184 #define T_V &void_type_node
1185 #define T_W &wchar_type_node
1186 #define T_ST &sizetype
1188 typedef struct {
1189 const char *format_chars;
1190 int pointer_count;
1191 /* Type of argument if no length modifier is used. */
1192 tree *nolen;
1193 /* Type of argument if length modifier for shortening to byte is used.
1194 If NULL, then this modifier is not allowed. */
1195 tree *hhlen;
1196 /* Type of argument if length modifier for shortening is used.
1197 If NULL, then this modifier is not allowed. */
1198 tree *hlen;
1199 /* Type of argument if length modifier `l' is used.
1200 If NULL, then this modifier is not allowed. */
1201 tree *llen;
1202 /* Type of argument if length modifier `q' or `ll' is used.
1203 If NULL, then this modifier is not allowed. */
1204 tree *qlen;
1205 /* Type of argument if length modifier `L' is used.
1206 If NULL, then this modifier is not allowed. */
1207 tree *bigllen;
1208 /* Type of argument if length modifiers 'z' or `Z' is used.
1209 If NULL, then this modifier is not allowed. */
1210 tree *zlen;
1211 /* List of other modifier characters allowed with these options. */
1212 const char *flag_chars;
1213 } format_char_info;
1215 static format_char_info print_char_table[] = {
1216 { "di", 0, T_I, T_I, T_I, T_L, T_LL, T_LL, T_ST, "-wp0 +" },
1217 { "oxX", 0, T_UI, T_UI, T_UI, T_UL, T_ULL, T_ULL, T_ST, "-wp0#" },
1218 { "u", 0, T_UI, T_UI, T_UI, T_UL, T_ULL, T_ULL, T_ST, "-wp0" },
1219 /* A GNU extension. */
1220 { "m", 0, T_V, NULL, NULL, NULL, NULL, NULL, NULL, "-wp" },
1221 { "feEgGaA", 0, T_D, NULL, NULL, NULL, NULL, T_LD, NULL, "-wp0 +#" },
1222 { "c", 0, T_I, NULL, NULL, T_W, NULL, NULL, NULL, "-w" },
1223 { "C", 0, T_W, NULL, NULL, NULL, NULL, NULL, NULL, "-w" },
1224 { "s", 1, T_C, NULL, NULL, T_W, NULL, NULL, NULL, "-wp" },
1225 { "S", 1, T_W, NULL, NULL, NULL, NULL, NULL, NULL, "-wp" },
1226 { "p", 1, T_V, NULL, NULL, NULL, NULL, NULL, NULL, "-w" },
1227 { "n", 1, T_I, NULL, T_S, T_L, T_LL, NULL, NULL, "" },
1228 { NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
1231 static format_char_info scan_char_table[] = {
1232 { "di", 1, T_I, T_C, T_S, T_L, T_LL, T_LL, NULL, "*" },
1233 { "ouxX", 1, T_UI, T_UC, T_US, T_UL, T_ULL, T_ULL, NULL, "*" },
1234 { "efgEGaA", 1, T_F, NULL, NULL, T_D, NULL, T_LD, NULL, "*" },
1235 { "c", 1, T_C, NULL, NULL, T_W, NULL, NULL, NULL, "*" },
1236 { "s", 1, T_C, NULL, NULL, T_W, NULL, NULL, NULL, "*a" },
1237 { "[", 1, T_C, NULL, NULL, NULL, NULL, NULL, NULL, "*a" },
1238 { "C", 1, T_W, NULL, NULL, NULL, NULL, NULL, NULL, "*" },
1239 { "S", 1, T_W, NULL, NULL, NULL, NULL, NULL, NULL, "*a" },
1240 { "p", 2, T_V, NULL, NULL, NULL, NULL, NULL, NULL, "*" },
1241 { "n", 1, T_I, T_C, T_S, T_L, T_LL, NULL, NULL, "" },
1242 { NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
1245 /* Handle format characters recognized by glibc's strftime.c.
1246 '2' - MUST do years as only two digits
1247 '3' - MAY do years as only two digits (depending on locale)
1248 'E' - E modifier is acceptable
1249 'O' - O modifier is acceptable to Standard C
1250 'o' - O modifier is acceptable as a GNU extension
1251 'G' - other GNU extensions */
1253 static format_char_info time_char_table[] = {
1254 { "y", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "2EO-_0w" },
1255 { "D", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "2" },
1256 { "g", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "2O-_0w" },
1257 { "cx", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "3E" },
1258 { "%RTXnrt", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "" },
1259 { "P", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "G" },
1260 { "HIMSUWdemw", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0Ow" },
1261 { "Vju", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0Oow" },
1262 { "Gklsz", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0OGw" },
1263 { "ABZa", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "^#" },
1264 { "p", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "#" },
1265 { "bh", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "^" },
1266 { "CY", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0EOw" },
1267 { NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
1270 typedef struct function_format_info
1272 struct function_format_info *next; /* next structure on the list */
1273 tree name; /* identifier such as "printf" */
1274 tree assembler_name; /* optional mangled identifier (for C++) */
1275 enum format_type format_type; /* type of format (printf, scanf, etc.) */
1276 int format_num; /* number of format argument */
1277 int first_arg_num; /* number of first arg (zero for varargs) */
1278 } function_format_info;
1280 static function_format_info *function_format_list = NULL;
1282 typedef struct international_format_info
1284 struct international_format_info *next; /* next structure on the list */
1285 tree name; /* identifier such as "gettext" */
1286 tree assembler_name; /* optional mangled identifier (for C++) */
1287 int format_num; /* number of format argument */
1288 } international_format_info;
1290 static international_format_info *international_format_list = NULL;
1292 static void check_format_info PARAMS ((function_format_info *, tree));
1294 /* Initialize the table of functions to perform format checking on.
1295 The ANSI functions are always checked (whether <stdio.h> is
1296 included or not), since it is common to call printf without
1297 including <stdio.h>. There shouldn't be a problem with this,
1298 since ANSI reserves these function names whether you include the
1299 header file or not. In any case, the checking is harmless.
1301 Also initialize the name of function that modify the format string for
1302 internationalization purposes. */
1304 void
1305 init_function_format_info ()
1307 record_function_format (get_identifier ("printf"), NULL_TREE,
1308 printf_format_type, 1, 2);
1309 record_function_format (get_identifier ("fprintf"), NULL_TREE,
1310 printf_format_type, 2, 3);
1311 record_function_format (get_identifier ("sprintf"), NULL_TREE,
1312 printf_format_type, 2, 3);
1313 record_function_format (get_identifier ("scanf"), NULL_TREE,
1314 scanf_format_type, 1, 2);
1315 record_function_format (get_identifier ("fscanf"), NULL_TREE,
1316 scanf_format_type, 2, 3);
1317 record_function_format (get_identifier ("sscanf"), NULL_TREE,
1318 scanf_format_type, 2, 3);
1319 record_function_format (get_identifier ("vprintf"), NULL_TREE,
1320 printf_format_type, 1, 0);
1321 record_function_format (get_identifier ("vfprintf"), NULL_TREE,
1322 printf_format_type, 2, 0);
1323 record_function_format (get_identifier ("vsprintf"), NULL_TREE,
1324 printf_format_type, 2, 0);
1325 record_function_format (get_identifier ("strftime"), NULL_TREE,
1326 strftime_format_type, 3, 0);
1328 record_international_format (get_identifier ("gettext"), NULL_TREE, 1);
1329 record_international_format (get_identifier ("dgettext"), NULL_TREE, 2);
1330 record_international_format (get_identifier ("dcgettext"), NULL_TREE, 2);
1333 /* Record information for argument format checking. FUNCTION_IDENT is
1334 the identifier node for the name of the function to check (its decl
1335 need not exist yet).
1336 FORMAT_TYPE specifies the type of format checking. FORMAT_NUM is the number
1337 of the argument which is the format control string (starting from 1).
1338 FIRST_ARG_NUM is the number of the first actual argument to check
1339 against the format string, or zero if no checking is not be done
1340 (e.g. for varargs such as vfprintf). */
1342 static void
1343 record_function_format (name, assembler_name, format_type,
1344 format_num, first_arg_num)
1345 tree name;
1346 tree assembler_name;
1347 enum format_type format_type;
1348 int format_num;
1349 int first_arg_num;
1351 function_format_info *info;
1353 /* Re-use existing structure if it's there. */
1355 for (info = function_format_list; info; info = info->next)
1357 if (info->name == name && info->assembler_name == assembler_name)
1358 break;
1360 if (! info)
1362 info = (function_format_info *) xmalloc (sizeof (function_format_info));
1363 info->next = function_format_list;
1364 function_format_list = info;
1366 info->name = name;
1367 info->assembler_name = assembler_name;
1370 info->format_type = format_type;
1371 info->format_num = format_num;
1372 info->first_arg_num = first_arg_num;
1375 /* Record information for the names of function that modify the format
1376 argument to format functions. FUNCTION_IDENT is the identifier node for
1377 the name of the function (its decl need not exist yet) and FORMAT_NUM is
1378 the number of the argument which is the format control string (starting
1379 from 1). */
1381 static void
1382 record_international_format (name, assembler_name, format_num)
1383 tree name;
1384 tree assembler_name;
1385 int format_num;
1387 international_format_info *info;
1389 /* Re-use existing structure if it's there. */
1391 for (info = international_format_list; info; info = info->next)
1393 if (info->name == name && info->assembler_name == assembler_name)
1394 break;
1397 if (! info)
1399 info
1400 = (international_format_info *)
1401 xmalloc (sizeof (international_format_info));
1402 info->next = international_format_list;
1403 international_format_list = info;
1405 info->name = name;
1406 info->assembler_name = assembler_name;
1409 info->format_num = format_num;
1412 static void
1413 tfaff ()
1415 warning ("too few arguments for format");
1418 /* Check the argument list of a call to printf, scanf, etc.
1419 NAME is the function identifier.
1420 ASSEMBLER_NAME is the function's assembler identifier.
1421 (Either NAME or ASSEMBLER_NAME, but not both, may be NULL_TREE.)
1422 PARAMS is the list of argument values. */
1424 void
1425 check_function_format (name, assembler_name, params)
1426 tree name;
1427 tree assembler_name;
1428 tree params;
1430 function_format_info *info;
1432 /* See if this function is a format function. */
1433 for (info = function_format_list; info; info = info->next)
1435 if (info->assembler_name
1436 ? (info->assembler_name == assembler_name)
1437 : (info->name == name))
1439 /* Yup; check it. */
1440 check_format_info (info, params);
1441 break;
1446 /* Check the argument list of a call to printf, scanf, etc.
1447 INFO points to the function_format_info structure.
1448 PARAMS is the list of argument values. */
1450 static void
1451 check_format_info (info, params)
1452 function_format_info *info;
1453 tree params;
1455 int i;
1456 int arg_num;
1457 int suppressed, wide, precise;
1458 int length_char = 0;
1459 int format_char;
1460 int format_length;
1461 tree format_tree;
1462 tree cur_param;
1463 tree cur_type;
1464 tree wanted_type;
1465 tree first_fillin_param;
1466 const char *format_chars;
1467 format_char_info *fci = NULL;
1468 char flag_chars[8];
1469 int has_operand_number = 0;
1471 /* Skip to format argument. If the argument isn't available, there's
1472 no work for us to do; prototype checking will catch the problem. */
1473 for (arg_num = 1; ; ++arg_num)
1475 if (params == 0)
1476 return;
1477 if (arg_num == info->format_num)
1478 break;
1479 params = TREE_CHAIN (params);
1481 format_tree = TREE_VALUE (params);
1482 params = TREE_CHAIN (params);
1483 if (format_tree == 0)
1484 return;
1486 /* We can only check the format if it's a string constant. */
1487 while (TREE_CODE (format_tree) == NOP_EXPR)
1488 format_tree = TREE_OPERAND (format_tree, 0); /* strip coercion */
1490 if (TREE_CODE (format_tree) == CALL_EXPR
1491 && TREE_CODE (TREE_OPERAND (format_tree, 0)) == ADDR_EXPR
1492 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (format_tree, 0), 0))
1493 == FUNCTION_DECL))
1495 tree function = TREE_OPERAND (TREE_OPERAND (format_tree, 0), 0);
1497 /* See if this is a call to a known internationalization function
1498 that modifies the format arg. */
1499 international_format_info *info;
1501 for (info = international_format_list; info; info = info->next)
1502 if (info->assembler_name
1503 ? (info->assembler_name == DECL_ASSEMBLER_NAME (function))
1504 : (info->name == DECL_NAME (function)))
1506 tree inner_args;
1507 int i;
1509 for (inner_args = TREE_OPERAND (format_tree, 1), i = 1;
1510 inner_args != 0;
1511 inner_args = TREE_CHAIN (inner_args), i++)
1512 if (i == info->format_num)
1514 format_tree = TREE_VALUE (inner_args);
1516 while (TREE_CODE (format_tree) == NOP_EXPR)
1517 format_tree = TREE_OPERAND (format_tree, 0);
1522 if (integer_zerop (format_tree))
1524 warning ("null format string");
1525 return;
1527 if (TREE_CODE (format_tree) != ADDR_EXPR)
1529 /* The user may get multiple warnings if the supplied argument
1530 isn't even a string pointer. */
1531 /* Functions taking a va_list normally pass a non-literal format
1532 string. These functions typically are declared with
1533 first_arg_num == 0, so avoid warning in those cases. */
1534 if (info->first_arg_num != 0 && warn_format > 1)
1535 warning ("format not a string literal, argument types not checked");
1536 return;
1538 format_tree = TREE_OPERAND (format_tree, 0);
1539 if (TREE_CODE (format_tree) != STRING_CST)
1541 /* The user may get multiple warnings if the supplied argument
1542 isn't even a string pointer. */
1543 /* Functions taking a va_list normally pass a non-literal format
1544 string. These functions typically are declared with
1545 first_arg_num == 0, so avoid warning in those cases. */
1546 if (info->first_arg_num != 0 && warn_format > 1)
1547 warning ("format not a string literal, argument types not checked");
1548 return;
1550 format_chars = TREE_STRING_POINTER (format_tree);
1551 format_length = TREE_STRING_LENGTH (format_tree);
1552 if (format_length <= 1)
1553 warning ("zero-length format string");
1554 if (format_chars[--format_length] != 0)
1556 warning ("unterminated format string");
1557 return;
1559 /* Skip to first argument to check. */
1560 while (arg_num + 1 < info->first_arg_num)
1562 if (params == 0)
1563 return;
1564 params = TREE_CHAIN (params);
1565 ++arg_num;
1568 first_fillin_param = params;
1569 while (1)
1571 int aflag;
1572 if (*format_chars == 0)
1574 if (format_chars - TREE_STRING_POINTER (format_tree) != format_length)
1575 warning ("embedded `\\0' in format");
1576 if (info->first_arg_num != 0 && params != 0 && ! has_operand_number)
1577 warning ("too many arguments for format");
1578 return;
1580 if (*format_chars++ != '%')
1581 continue;
1582 if (*format_chars == 0)
1584 warning ("spurious trailing `%%' in format");
1585 continue;
1587 if (*format_chars == '%')
1589 ++format_chars;
1590 continue;
1592 flag_chars[0] = 0;
1593 suppressed = wide = precise = FALSE;
1594 if (info->format_type == scanf_format_type)
1596 suppressed = *format_chars == '*';
1597 if (suppressed)
1598 ++format_chars;
1599 while (ISDIGIT (*format_chars))
1600 ++format_chars;
1602 else if (info->format_type == strftime_format_type)
1604 while (*format_chars != 0 && index ("_-0^#", *format_chars) != 0)
1606 if (pedantic)
1607 warning ("ANSI C does not support the strftime `%c' flag",
1608 *format_chars);
1609 if (index (flag_chars, *format_chars) != 0)
1611 warning ("repeated `%c' flag in format",
1612 *format_chars);
1613 ++format_chars;
1615 else
1617 i = strlen (flag_chars);
1618 flag_chars[i++] = *format_chars++;
1619 flag_chars[i] = 0;
1622 while (ISDIGIT ((unsigned char) *format_chars))
1624 wide = TRUE;
1625 ++format_chars;
1627 if (wide && pedantic)
1628 warning ("ANSI C does not support strftime format width");
1629 if (*format_chars == 'E' || *format_chars == 'O')
1631 i = strlen (flag_chars);
1632 flag_chars[i++] = *format_chars++;
1633 flag_chars[i] = 0;
1634 if (*format_chars == 'E' || *format_chars == 'O')
1636 warning ("multiple E/O modifiers in format");
1637 while (*format_chars == 'E' || *format_chars == 'O')
1638 ++format_chars;
1642 else if (info->format_type == printf_format_type)
1644 /* See if we have a number followed by a dollar sign. If we do,
1645 it is an operand number, so set PARAMS to that operand. */
1646 if (*format_chars >= '0' && *format_chars <= '9')
1648 const char *p = format_chars;
1650 while (*p >= '0' && *p++ <= '9')
1653 if (*p == '$')
1655 int opnum = atoi (format_chars);
1657 params = first_fillin_param;
1658 format_chars = p + 1;
1659 has_operand_number = 1;
1661 for (i = 1; i < opnum && params != 0; i++)
1662 params = TREE_CHAIN (params);
1664 if (opnum == 0 || params == 0)
1666 warning ("operand number out of range in format");
1667 return;
1672 while (*format_chars != 0 && index (" +#0-", *format_chars) != 0)
1674 if (index (flag_chars, *format_chars) != 0)
1675 warning ("repeated `%c' flag in format", *format_chars++);
1676 else
1678 i = strlen (flag_chars);
1679 flag_chars[i++] = *format_chars++;
1680 flag_chars[i] = 0;
1683 /* "If the space and + flags both appear,
1684 the space flag will be ignored." */
1685 if (index (flag_chars, ' ') != 0
1686 && index (flag_chars, '+') != 0)
1687 warning ("use of both ` ' and `+' flags in format");
1688 /* "If the 0 and - flags both appear,
1689 the 0 flag will be ignored." */
1690 if (index (flag_chars, '0') != 0
1691 && index (flag_chars, '-') != 0)
1692 warning ("use of both `0' and `-' flags in format");
1693 if (*format_chars == '*')
1695 wide = TRUE;
1696 /* "...a field width...may be indicated by an asterisk.
1697 In this case, an int argument supplies the field width..." */
1698 ++format_chars;
1699 if (params == 0)
1701 tfaff ();
1702 return;
1704 if (info->first_arg_num != 0)
1706 cur_param = TREE_VALUE (params);
1707 params = TREE_CHAIN (params);
1708 ++arg_num;
1709 /* size_t is generally not valid here.
1710 It will work on most machines, because size_t and int
1711 have the same mode. But might as well warn anyway,
1712 since it will fail on other machines. */
1713 if ((TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1714 != integer_type_node)
1716 (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1717 != unsigned_type_node))
1718 warning ("field width is not type int (arg %d)", arg_num);
1721 else
1723 while (ISDIGIT (*format_chars))
1725 wide = TRUE;
1726 ++format_chars;
1729 if (*format_chars == '.')
1731 precise = TRUE;
1732 ++format_chars;
1733 if (*format_chars != '*' && !ISDIGIT (*format_chars))
1734 warning ("`.' not followed by `*' or digit in format");
1735 /* "...a...precision...may be indicated by an asterisk.
1736 In this case, an int argument supplies the...precision." */
1737 if (*format_chars == '*')
1739 if (info->first_arg_num != 0)
1741 ++format_chars;
1742 if (params == 0)
1744 tfaff ();
1745 return;
1747 cur_param = TREE_VALUE (params);
1748 params = TREE_CHAIN (params);
1749 ++arg_num;
1750 if (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1751 != integer_type_node)
1752 warning ("field width is not type int (arg %d)",
1753 arg_num);
1756 else
1758 while (ISDIGIT (*format_chars))
1759 ++format_chars;
1764 aflag = 0;
1766 if (info->format_type != strftime_format_type)
1768 if (*format_chars == 'h' || *format_chars == 'l')
1769 length_char = *format_chars++;
1770 else if (*format_chars == 'q' || *format_chars == 'L')
1772 length_char = *format_chars++;
1773 if (pedantic)
1774 warning ("ANSI C does not support the `%c' length modifier",
1775 length_char);
1777 else if (*format_chars == 'Z' || *format_chars == 'z')
1779 length_char = *format_chars++;
1780 if (pedantic && (length_char == 'Z' || !flag_isoc99))
1781 warning ("ANSI C does not support the `%c' length modifier",
1782 length_char);
1784 else
1785 length_char = 0;
1786 if (length_char == 'l' && *format_chars == 'l')
1788 length_char = 'q', format_chars++;
1789 if (pedantic && !flag_isoc99)
1790 warning ("ANSI C does not support the `ll' length modifier");
1792 else if (length_char == 'h' && *format_chars == 'h')
1794 length_char = 'H', format_chars++;
1795 if (pedantic && !flag_isoc99)
1796 warning ("ANSI C does not support the `hh' length modifier");
1798 if (*format_chars == 'a' && info->format_type == scanf_format_type)
1800 if (format_chars[1] == 's' || format_chars[1] == 'S'
1801 || format_chars[1] == '[')
1803 /* `a' is used as a flag. */
1804 aflag = 1;
1805 format_chars++;
1808 if (suppressed && length_char != 0)
1809 warning ("use of `*' and `%c' together in format", length_char);
1811 format_char = *format_chars;
1812 if (format_char == 0
1813 || (info->format_type != strftime_format_type && format_char == '%'))
1815 warning ("conversion lacks type at end of format");
1816 continue;
1818 /* The m, C, and S formats are GNU extensions. */
1819 if (pedantic && info->format_type != strftime_format_type
1820 && (format_char == 'm' || format_char == 'C' || format_char == 'S'))
1821 warning ("ANSI C does not support the `%c' format", format_char);
1822 /* The a and A formats are C99 extensions. */
1823 if (pedantic && info->format_type != strftime_format_type
1824 && (format_char == 'a' || format_char == 'A')
1825 && !flag_isoc99)
1826 warning ("ANSI C does not support the `%c' format", format_char);
1827 format_chars++;
1828 switch (info->format_type)
1830 case printf_format_type:
1831 fci = print_char_table;
1832 break;
1833 case scanf_format_type:
1834 fci = scan_char_table;
1835 break;
1836 case strftime_format_type:
1837 fci = time_char_table;
1838 break;
1839 default:
1840 abort ();
1842 while (fci->format_chars != 0
1843 && index (fci->format_chars, format_char) == 0)
1844 ++fci;
1845 if (fci->format_chars == 0)
1847 if (ISGRAPH(format_char))
1848 warning ("unknown conversion type character `%c' in format",
1849 format_char);
1850 else
1851 warning ("unknown conversion type character 0x%x in format",
1852 format_char);
1853 continue;
1855 if (pedantic)
1857 if (index (fci->flag_chars, 'G') != 0)
1858 warning ("ANSI C does not support `%%%c'", format_char);
1859 if (index (fci->flag_chars, 'o') != 0
1860 && index (flag_chars, 'O') != 0)
1861 warning ("ANSI C does not support `%%O%c'", format_char);
1863 if (wide && index (fci->flag_chars, 'w') == 0)
1864 warning ("width used with `%c' format", format_char);
1865 if (index (fci->flag_chars, '2') != 0)
1866 warning ("`%%%c' yields only last 2 digits of year", format_char);
1867 else if (index (fci->flag_chars, '3') != 0)
1868 warning ("`%%%c' yields only last 2 digits of year in some locales",
1869 format_char);
1870 if (precise && index (fci->flag_chars, 'p') == 0)
1871 warning ("precision used with `%c' format", format_char);
1872 if (aflag && index (fci->flag_chars, 'a') == 0)
1874 warning ("`a' flag used with `%c' format", format_char);
1875 /* To simplify the following code. */
1876 aflag = 0;
1878 /* The a flag is a GNU extension. */
1879 else if (pedantic && aflag)
1880 warning ("ANSI C does not support the `a' flag");
1881 if (info->format_type == scanf_format_type && format_char == '[')
1883 /* Skip over scan set, in case it happens to have '%' in it. */
1884 if (*format_chars == '^')
1885 ++format_chars;
1886 /* Find closing bracket; if one is hit immediately, then
1887 it's part of the scan set rather than a terminator. */
1888 if (*format_chars == ']')
1889 ++format_chars;
1890 while (*format_chars && *format_chars != ']')
1891 ++format_chars;
1892 if (*format_chars != ']')
1893 /* The end of the format string was reached. */
1894 warning ("no closing `]' for `%%[' format");
1896 if (suppressed)
1898 if (index (fci->flag_chars, '*') == 0)
1899 warning ("suppression of `%c' conversion in format", format_char);
1900 continue;
1902 for (i = 0; flag_chars[i] != 0; ++i)
1904 if (index (fci->flag_chars, flag_chars[i]) == 0)
1905 warning ("flag `%c' used with type `%c'",
1906 flag_chars[i], format_char);
1908 if (info->format_type == strftime_format_type)
1909 continue;
1910 if (precise && index (flag_chars, '0') != 0
1911 && (format_char == 'd' || format_char == 'i'
1912 || format_char == 'o' || format_char == 'u'
1913 || format_char == 'x' || format_char == 'X'))
1914 warning ("`0' flag ignored with precision specifier and `%c' format",
1915 format_char);
1916 switch (length_char)
1918 default: wanted_type = fci->nolen ? *(fci->nolen) : 0; break;
1919 case 'H': wanted_type = fci->hhlen ? *(fci->hhlen) : 0; break;
1920 case 'h': wanted_type = fci->hlen ? *(fci->hlen) : 0; break;
1921 case 'l': wanted_type = fci->llen ? *(fci->llen) : 0; break;
1922 case 'q': wanted_type = fci->qlen ? *(fci->qlen) : 0; break;
1923 case 'L': wanted_type = fci->bigllen ? *(fci->bigllen) : 0; break;
1924 case 'z': case 'Z': wanted_type = fci->zlen ? *fci->zlen : 0; break;
1926 if (wanted_type == 0)
1927 warning ("use of `%c' length character with `%c' type character",
1928 length_char, format_char);
1930 /* Finally. . .check type of argument against desired type! */
1931 if (info->first_arg_num == 0)
1932 continue;
1933 if (fci->pointer_count == 0 && wanted_type == void_type_node)
1934 /* This specifier takes no argument. */
1935 continue;
1936 if (params == 0)
1938 tfaff ();
1939 return;
1941 cur_param = TREE_VALUE (params);
1942 params = TREE_CHAIN (params);
1943 ++arg_num;
1944 cur_type = TREE_TYPE (cur_param);
1946 STRIP_NOPS (cur_param);
1948 /* Check the types of any additional pointer arguments
1949 that precede the "real" argument. */
1950 for (i = 0; i < fci->pointer_count + aflag; ++i)
1952 if (TREE_CODE (cur_type) == POINTER_TYPE)
1954 cur_type = TREE_TYPE (cur_type);
1956 if (cur_param != 0 && TREE_CODE (cur_param) == ADDR_EXPR)
1957 cur_param = TREE_OPERAND (cur_param, 0);
1958 else
1959 cur_param = 0;
1961 continue;
1963 if (TREE_CODE (cur_type) != ERROR_MARK)
1965 if (fci->pointer_count + aflag == 1)
1966 warning ("format argument is not a pointer (arg %d)", arg_num);
1967 else
1968 warning ("format argument is not a pointer to a pointer (arg %d)", arg_num);
1970 break;
1973 /* See if this is an attempt to write into a const type with
1974 scanf or with printf "%n". */
1975 if ((info->format_type == scanf_format_type
1976 || (info->format_type == printf_format_type
1977 && format_char == 'n'))
1978 && i == fci->pointer_count + aflag
1979 && wanted_type != 0
1980 && TREE_CODE (cur_type) != ERROR_MARK
1981 && (TYPE_READONLY (cur_type)
1982 || (cur_param != 0
1983 && (TREE_CODE_CLASS (TREE_CODE (cur_param)) == 'c'
1984 || (TREE_CODE_CLASS (TREE_CODE (cur_param)) == 'd'
1985 && TREE_READONLY (cur_param))))))
1986 warning ("writing into constant object (arg %d)", arg_num);
1988 /* Check the type of the "real" argument, if there's a type we want. */
1989 if (i == fci->pointer_count + aflag && wanted_type != 0
1990 && TREE_CODE (cur_type) != ERROR_MARK
1991 && wanted_type != TYPE_MAIN_VARIANT (cur_type)
1992 /* If we want `void *', allow any pointer type.
1993 (Anything else would already have got a warning.) */
1994 && ! (wanted_type == void_type_node
1995 && fci->pointer_count > 0)
1996 /* Don't warn about differences merely in signedness. */
1997 && !(TREE_CODE (wanted_type) == INTEGER_TYPE
1998 && TREE_CODE (TYPE_MAIN_VARIANT (cur_type)) == INTEGER_TYPE
1999 && (TREE_UNSIGNED (wanted_type)
2000 ? wanted_type == (cur_type = unsigned_type (cur_type))
2001 : wanted_type == (cur_type = signed_type (cur_type))))
2002 /* Likewise, "signed char", "unsigned char" and "char" are
2003 equivalent but the above test won't consider them equivalent. */
2004 && ! (wanted_type == char_type_node
2005 && (TYPE_MAIN_VARIANT (cur_type) == signed_char_type_node
2006 || TYPE_MAIN_VARIANT (cur_type) == unsigned_char_type_node)))
2008 register const char *this;
2009 register const char *that;
2011 this = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (wanted_type)));
2012 that = 0;
2013 if (TREE_CODE (cur_type) != ERROR_MARK
2014 && TYPE_NAME (cur_type) != 0
2015 && TREE_CODE (cur_type) != INTEGER_TYPE
2016 && !(TREE_CODE (cur_type) == POINTER_TYPE
2017 && TREE_CODE (TREE_TYPE (cur_type)) == INTEGER_TYPE))
2019 if (TREE_CODE (TYPE_NAME (cur_type)) == TYPE_DECL
2020 && DECL_NAME (TYPE_NAME (cur_type)) != 0)
2021 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type)));
2022 else
2023 that = IDENTIFIER_POINTER (TYPE_NAME (cur_type));
2026 /* A nameless type can't possibly match what the format wants.
2027 So there will be a warning for it.
2028 Make up a string to describe vaguely what it is. */
2029 if (that == 0)
2031 if (TREE_CODE (cur_type) == POINTER_TYPE)
2032 that = "pointer";
2033 else
2034 that = "different type";
2037 /* Make the warning better in case of mismatch of int vs long. */
2038 if (TREE_CODE (cur_type) == INTEGER_TYPE
2039 && TREE_CODE (wanted_type) == INTEGER_TYPE
2040 && TYPE_PRECISION (cur_type) == TYPE_PRECISION (wanted_type)
2041 && TYPE_NAME (cur_type) != 0
2042 && TREE_CODE (TYPE_NAME (cur_type)) == TYPE_DECL)
2043 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type)));
2045 if (strcmp (this, that) != 0)
2046 warning ("%s format, %s arg (arg %d)", this, that, arg_num);
2051 /* Print a warning if a constant expression had overflow in folding.
2052 Invoke this function on every expression that the language
2053 requires to be a constant expression.
2054 Note the ANSI C standard says it is erroneous for a
2055 constant expression to overflow. */
2057 void
2058 constant_expression_warning (value)
2059 tree value;
2061 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
2062 || TREE_CODE (value) == COMPLEX_CST)
2063 && TREE_CONSTANT_OVERFLOW (value) && pedantic)
2064 pedwarn ("overflow in constant expression");
2067 /* Print a warning if an expression had overflow in folding.
2068 Invoke this function on every expression that
2069 (1) appears in the source code, and
2070 (2) might be a constant expression that overflowed, and
2071 (3) is not already checked by convert_and_check;
2072 however, do not invoke this function on operands of explicit casts. */
2074 void
2075 overflow_warning (value)
2076 tree value;
2078 if ((TREE_CODE (value) == INTEGER_CST
2079 || (TREE_CODE (value) == COMPLEX_CST
2080 && TREE_CODE (TREE_REALPART (value)) == INTEGER_CST))
2081 && TREE_OVERFLOW (value))
2083 TREE_OVERFLOW (value) = 0;
2084 if (skip_evaluation == 0)
2085 warning ("integer overflow in expression");
2087 else if ((TREE_CODE (value) == REAL_CST
2088 || (TREE_CODE (value) == COMPLEX_CST
2089 && TREE_CODE (TREE_REALPART (value)) == REAL_CST))
2090 && TREE_OVERFLOW (value))
2092 TREE_OVERFLOW (value) = 0;
2093 if (skip_evaluation == 0)
2094 warning ("floating point overflow in expression");
2098 /* Print a warning if a large constant is truncated to unsigned,
2099 or if -Wconversion is used and a constant < 0 is converted to unsigned.
2100 Invoke this function on every expression that might be implicitly
2101 converted to an unsigned type. */
2103 void
2104 unsigned_conversion_warning (result, operand)
2105 tree result, operand;
2107 if (TREE_CODE (operand) == INTEGER_CST
2108 && TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
2109 && TREE_UNSIGNED (TREE_TYPE (result))
2110 && skip_evaluation == 0
2111 && !int_fits_type_p (operand, TREE_TYPE (result)))
2113 if (!int_fits_type_p (operand, signed_type (TREE_TYPE (result))))
2114 /* This detects cases like converting -129 or 256 to unsigned char. */
2115 warning ("large integer implicitly truncated to unsigned type");
2116 else if (warn_conversion)
2117 warning ("negative integer implicitly converted to unsigned type");
2121 /* Convert EXPR to TYPE, warning about conversion problems with constants.
2122 Invoke this function on every expression that is converted implicitly,
2123 i.e. because of language rules and not because of an explicit cast. */
2125 tree
2126 convert_and_check (type, expr)
2127 tree type, expr;
2129 tree t = convert (type, expr);
2130 if (TREE_CODE (t) == INTEGER_CST)
2132 if (TREE_OVERFLOW (t))
2134 TREE_OVERFLOW (t) = 0;
2136 /* Do not diagnose overflow in a constant expression merely
2137 because a conversion overflowed. */
2138 TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (expr);
2140 /* No warning for converting 0x80000000 to int. */
2141 if (!(TREE_UNSIGNED (type) < TREE_UNSIGNED (TREE_TYPE (expr))
2142 && TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
2143 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (expr))))
2144 /* If EXPR fits in the unsigned version of TYPE,
2145 don't warn unless pedantic. */
2146 if ((pedantic
2147 || TREE_UNSIGNED (type)
2148 || ! int_fits_type_p (expr, unsigned_type (type)))
2149 && skip_evaluation == 0)
2150 warning ("overflow in implicit constant conversion");
2152 else
2153 unsigned_conversion_warning (t, expr);
2155 return t;
2158 void
2159 c_expand_expr_stmt (expr)
2160 tree expr;
2162 /* Do default conversion if safe and possibly important,
2163 in case within ({...}). */
2164 if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE && lvalue_p (expr))
2165 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
2166 expr = default_conversion (expr);
2168 if (TREE_TYPE (expr) != error_mark_node
2169 && TYPE_SIZE (TREE_TYPE (expr)) == 0
2170 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
2171 error ("expression statement has incomplete type");
2173 expand_expr_stmt (expr);
2176 /* Validate the expression after `case' and apply default promotions. */
2178 tree
2179 check_case_value (value)
2180 tree value;
2182 if (value == NULL_TREE)
2183 return value;
2185 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
2186 STRIP_TYPE_NOPS (value);
2188 if (TREE_CODE (value) != INTEGER_CST
2189 && value != error_mark_node)
2191 error ("case label does not reduce to an integer constant");
2192 value = error_mark_node;
2194 else
2195 /* Promote char or short to int. */
2196 value = default_conversion (value);
2198 constant_expression_warning (value);
2200 return value;
2203 /* Return an integer type with BITS bits of precision,
2204 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
2206 tree
2207 type_for_size (bits, unsignedp)
2208 unsigned bits;
2209 int unsignedp;
2211 if (bits == TYPE_PRECISION (integer_type_node))
2212 return unsignedp ? unsigned_type_node : integer_type_node;
2214 if (bits == TYPE_PRECISION (signed_char_type_node))
2215 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2217 if (bits == TYPE_PRECISION (short_integer_type_node))
2218 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2220 if (bits == TYPE_PRECISION (long_integer_type_node))
2221 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2223 if (bits == TYPE_PRECISION (long_long_integer_type_node))
2224 return (unsignedp ? long_long_unsigned_type_node
2225 : long_long_integer_type_node);
2227 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
2228 return (unsignedp ? widest_unsigned_literal_type_node
2229 : widest_integer_literal_type_node);
2231 if (bits <= TYPE_PRECISION (intQI_type_node))
2232 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2234 if (bits <= TYPE_PRECISION (intHI_type_node))
2235 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2237 if (bits <= TYPE_PRECISION (intSI_type_node))
2238 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2240 if (bits <= TYPE_PRECISION (intDI_type_node))
2241 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2243 return 0;
2246 /* Return a data type that has machine mode MODE.
2247 If the mode is an integer,
2248 then UNSIGNEDP selects between signed and unsigned types. */
2250 tree
2251 type_for_mode (mode, unsignedp)
2252 enum machine_mode mode;
2253 int unsignedp;
2255 if (mode == TYPE_MODE (integer_type_node))
2256 return unsignedp ? unsigned_type_node : integer_type_node;
2258 if (mode == TYPE_MODE (signed_char_type_node))
2259 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2261 if (mode == TYPE_MODE (short_integer_type_node))
2262 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2264 if (mode == TYPE_MODE (long_integer_type_node))
2265 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2267 if (mode == TYPE_MODE (long_long_integer_type_node))
2268 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
2270 if (mode == TYPE_MODE (widest_integer_literal_type_node))
2271 return unsignedp ? widest_unsigned_literal_type_node
2272 : widest_integer_literal_type_node;
2274 if (mode == TYPE_MODE (intQI_type_node))
2275 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2277 if (mode == TYPE_MODE (intHI_type_node))
2278 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2280 if (mode == TYPE_MODE (intSI_type_node))
2281 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2283 if (mode == TYPE_MODE (intDI_type_node))
2284 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2286 #if HOST_BITS_PER_WIDE_INT >= 64
2287 if (mode == TYPE_MODE (intTI_type_node))
2288 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2289 #endif
2291 if (mode == TYPE_MODE (float_type_node))
2292 return float_type_node;
2294 if (mode == TYPE_MODE (double_type_node))
2295 return double_type_node;
2297 if (mode == TYPE_MODE (long_double_type_node))
2298 return long_double_type_node;
2300 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
2301 return build_pointer_type (char_type_node);
2303 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
2304 return build_pointer_type (integer_type_node);
2306 return 0;
2309 /* Return an unsigned type the same as TYPE in other respects. */
2310 tree
2311 unsigned_type (type)
2312 tree type;
2314 tree type1 = TYPE_MAIN_VARIANT (type);
2315 if (type1 == signed_char_type_node || type1 == char_type_node)
2316 return unsigned_char_type_node;
2317 if (type1 == integer_type_node)
2318 return unsigned_type_node;
2319 if (type1 == short_integer_type_node)
2320 return short_unsigned_type_node;
2321 if (type1 == long_integer_type_node)
2322 return long_unsigned_type_node;
2323 if (type1 == long_long_integer_type_node)
2324 return long_long_unsigned_type_node;
2325 if (type1 == widest_integer_literal_type_node)
2326 return widest_unsigned_literal_type_node;
2327 #if HOST_BITS_PER_WIDE_INT >= 64
2328 if (type1 == intTI_type_node)
2329 return unsigned_intTI_type_node;
2330 #endif
2331 if (type1 == intDI_type_node)
2332 return unsigned_intDI_type_node;
2333 if (type1 == intSI_type_node)
2334 return unsigned_intSI_type_node;
2335 if (type1 == intHI_type_node)
2336 return unsigned_intHI_type_node;
2337 if (type1 == intQI_type_node)
2338 return unsigned_intQI_type_node;
2340 return signed_or_unsigned_type (1, type);
2343 /* Return a signed type the same as TYPE in other respects. */
2345 tree
2346 signed_type (type)
2347 tree type;
2349 tree type1 = TYPE_MAIN_VARIANT (type);
2350 if (type1 == unsigned_char_type_node || type1 == char_type_node)
2351 return signed_char_type_node;
2352 if (type1 == unsigned_type_node)
2353 return integer_type_node;
2354 if (type1 == short_unsigned_type_node)
2355 return short_integer_type_node;
2356 if (type1 == long_unsigned_type_node)
2357 return long_integer_type_node;
2358 if (type1 == long_long_unsigned_type_node)
2359 return long_long_integer_type_node;
2360 if (type1 == widest_unsigned_literal_type_node)
2361 return widest_integer_literal_type_node;
2362 #if HOST_BITS_PER_WIDE_INT >= 64
2363 if (type1 == unsigned_intTI_type_node)
2364 return intTI_type_node;
2365 #endif
2366 if (type1 == unsigned_intDI_type_node)
2367 return intDI_type_node;
2368 if (type1 == unsigned_intSI_type_node)
2369 return intSI_type_node;
2370 if (type1 == unsigned_intHI_type_node)
2371 return intHI_type_node;
2372 if (type1 == unsigned_intQI_type_node)
2373 return intQI_type_node;
2375 return signed_or_unsigned_type (0, type);
2378 /* Return a type the same as TYPE except unsigned or
2379 signed according to UNSIGNEDP. */
2381 tree
2382 signed_or_unsigned_type (unsignedp, type)
2383 int unsignedp;
2384 tree type;
2386 if (! INTEGRAL_TYPE_P (type)
2387 || TREE_UNSIGNED (type) == unsignedp)
2388 return type;
2390 if (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node))
2391 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2392 if (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2393 return unsignedp ? unsigned_type_node : integer_type_node;
2394 if (TYPE_PRECISION (type) == TYPE_PRECISION (short_integer_type_node))
2395 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2396 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_integer_type_node))
2397 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2398 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_long_integer_type_node))
2399 return (unsignedp ? long_long_unsigned_type_node
2400 : long_long_integer_type_node);
2401 if (TYPE_PRECISION (type) == TYPE_PRECISION (widest_integer_literal_type_node))
2402 return (unsignedp ? widest_unsigned_literal_type_node
2403 : widest_integer_literal_type_node);
2404 return type;
2407 /* Return the minimum number of bits needed to represent VALUE in a
2408 signed or unsigned type, UNSIGNEDP says which. */
2411 min_precision (value, unsignedp)
2412 tree value;
2413 int unsignedp;
2415 int log;
2417 /* If the value is negative, compute its negative minus 1. The latter
2418 adjustment is because the absolute value of the largest negative value
2419 is one larger than the largest positive value. This is equivalent to
2420 a bit-wise negation, so use that operation instead. */
2422 if (tree_int_cst_sgn (value) < 0)
2423 value = fold (build1 (BIT_NOT_EXPR, TREE_TYPE (value), value));
2425 /* Return the number of bits needed, taking into account the fact
2426 that we need one more bit for a signed than unsigned type. */
2428 if (integer_zerop (value))
2429 log = 0;
2430 else if (TREE_INT_CST_HIGH (value) != 0)
2431 log = HOST_BITS_PER_WIDE_INT + floor_log2 (TREE_INT_CST_HIGH (value));
2432 else
2433 log = floor_log2 (TREE_INT_CST_LOW (value));
2435 return log + 1 + ! unsignedp;
2438 /* Print an error message for invalid operands to arith operation CODE.
2439 NOP_EXPR is used as a special case (see truthvalue_conversion). */
2441 void
2442 binary_op_error (code)
2443 enum tree_code code;
2445 register const char *opname;
2447 switch (code)
2449 case NOP_EXPR:
2450 error ("invalid truth-value expression");
2451 return;
2453 case PLUS_EXPR:
2454 opname = "+"; break;
2455 case MINUS_EXPR:
2456 opname = "-"; break;
2457 case MULT_EXPR:
2458 opname = "*"; break;
2459 case MAX_EXPR:
2460 opname = "max"; break;
2461 case MIN_EXPR:
2462 opname = "min"; break;
2463 case EQ_EXPR:
2464 opname = "=="; break;
2465 case NE_EXPR:
2466 opname = "!="; break;
2467 case LE_EXPR:
2468 opname = "<="; break;
2469 case GE_EXPR:
2470 opname = ">="; break;
2471 case LT_EXPR:
2472 opname = "<"; break;
2473 case GT_EXPR:
2474 opname = ">"; break;
2475 case LSHIFT_EXPR:
2476 opname = "<<"; break;
2477 case RSHIFT_EXPR:
2478 opname = ">>"; break;
2479 case TRUNC_MOD_EXPR:
2480 case FLOOR_MOD_EXPR:
2481 opname = "%"; break;
2482 case TRUNC_DIV_EXPR:
2483 case FLOOR_DIV_EXPR:
2484 opname = "/"; break;
2485 case BIT_AND_EXPR:
2486 opname = "&"; break;
2487 case BIT_IOR_EXPR:
2488 opname = "|"; break;
2489 case TRUTH_ANDIF_EXPR:
2490 opname = "&&"; break;
2491 case TRUTH_ORIF_EXPR:
2492 opname = "||"; break;
2493 case BIT_XOR_EXPR:
2494 opname = "^"; break;
2495 case LROTATE_EXPR:
2496 case RROTATE_EXPR:
2497 opname = "rotate"; break;
2498 default:
2499 opname = "unknown"; break;
2501 error ("invalid operands to binary %s", opname);
2504 /* Subroutine of build_binary_op, used for comparison operations.
2505 See if the operands have both been converted from subword integer types
2506 and, if so, perhaps change them both back to their original type.
2507 This function is also responsible for converting the two operands
2508 to the proper common type for comparison.
2510 The arguments of this function are all pointers to local variables
2511 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
2512 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
2514 If this function returns nonzero, it means that the comparison has
2515 a constant value. What this function returns is an expression for
2516 that value. */
2518 tree
2519 shorten_compare (op0_ptr, op1_ptr, restype_ptr, rescode_ptr)
2520 tree *op0_ptr, *op1_ptr;
2521 tree *restype_ptr;
2522 enum tree_code *rescode_ptr;
2524 register tree type;
2525 tree op0 = *op0_ptr;
2526 tree op1 = *op1_ptr;
2527 int unsignedp0, unsignedp1;
2528 int real1, real2;
2529 tree primop0, primop1;
2530 enum tree_code code = *rescode_ptr;
2532 /* Throw away any conversions to wider types
2533 already present in the operands. */
2535 primop0 = get_narrower (op0, &unsignedp0);
2536 primop1 = get_narrower (op1, &unsignedp1);
2538 /* Handle the case that OP0 does not *contain* a conversion
2539 but it *requires* conversion to FINAL_TYPE. */
2541 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
2542 unsignedp0 = TREE_UNSIGNED (TREE_TYPE (op0));
2543 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
2544 unsignedp1 = TREE_UNSIGNED (TREE_TYPE (op1));
2546 /* If one of the operands must be floated, we cannot optimize. */
2547 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
2548 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
2550 /* If first arg is constant, swap the args (changing operation
2551 so value is preserved), for canonicalization. Don't do this if
2552 the second arg is 0. */
2554 if (TREE_CONSTANT (primop0)
2555 && ! integer_zerop (primop1) && ! real_zerop (primop1))
2557 register tree tem = primop0;
2558 register int temi = unsignedp0;
2559 primop0 = primop1;
2560 primop1 = tem;
2561 tem = op0;
2562 op0 = op1;
2563 op1 = tem;
2564 *op0_ptr = op0;
2565 *op1_ptr = op1;
2566 unsignedp0 = unsignedp1;
2567 unsignedp1 = temi;
2568 temi = real1;
2569 real1 = real2;
2570 real2 = temi;
2572 switch (code)
2574 case LT_EXPR:
2575 code = GT_EXPR;
2576 break;
2577 case GT_EXPR:
2578 code = LT_EXPR;
2579 break;
2580 case LE_EXPR:
2581 code = GE_EXPR;
2582 break;
2583 case GE_EXPR:
2584 code = LE_EXPR;
2585 break;
2586 default:
2587 break;
2589 *rescode_ptr = code;
2592 /* If comparing an integer against a constant more bits wide,
2593 maybe we can deduce a value of 1 or 0 independent of the data.
2594 Or else truncate the constant now
2595 rather than extend the variable at run time.
2597 This is only interesting if the constant is the wider arg.
2598 Also, it is not safe if the constant is unsigned and the
2599 variable arg is signed, since in this case the variable
2600 would be sign-extended and then regarded as unsigned.
2601 Our technique fails in this case because the lowest/highest
2602 possible unsigned results don't follow naturally from the
2603 lowest/highest possible values of the variable operand.
2604 For just EQ_EXPR and NE_EXPR there is another technique that
2605 could be used: see if the constant can be faithfully represented
2606 in the other operand's type, by truncating it and reextending it
2607 and see if that preserves the constant's value. */
2609 if (!real1 && !real2
2610 && TREE_CODE (primop1) == INTEGER_CST
2611 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
2613 int min_gt, max_gt, min_lt, max_lt;
2614 tree maxval, minval;
2615 /* 1 if comparison is nominally unsigned. */
2616 int unsignedp = TREE_UNSIGNED (*restype_ptr);
2617 tree val;
2619 type = signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0));
2621 /* If TYPE is an enumeration, then we need to get its min/max
2622 values from it's underlying integral type, not the enumerated
2623 type itself. */
2624 if (TREE_CODE (type) == ENUMERAL_TYPE)
2625 type = type_for_size (TYPE_PRECISION (type), unsignedp0);
2627 maxval = TYPE_MAX_VALUE (type);
2628 minval = TYPE_MIN_VALUE (type);
2630 if (unsignedp && !unsignedp0)
2631 *restype_ptr = signed_type (*restype_ptr);
2633 if (TREE_TYPE (primop1) != *restype_ptr)
2634 primop1 = convert (*restype_ptr, primop1);
2635 if (type != *restype_ptr)
2637 minval = convert (*restype_ptr, minval);
2638 maxval = convert (*restype_ptr, maxval);
2641 if (unsignedp && unsignedp0)
2643 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
2644 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
2645 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
2646 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
2648 else
2650 min_gt = INT_CST_LT (primop1, minval);
2651 max_gt = INT_CST_LT (primop1, maxval);
2652 min_lt = INT_CST_LT (minval, primop1);
2653 max_lt = INT_CST_LT (maxval, primop1);
2656 val = 0;
2657 /* This used to be a switch, but Genix compiler can't handle that. */
2658 if (code == NE_EXPR)
2660 if (max_lt || min_gt)
2661 val = boolean_true_node;
2663 else if (code == EQ_EXPR)
2665 if (max_lt || min_gt)
2666 val = boolean_false_node;
2668 else if (code == LT_EXPR)
2670 if (max_lt)
2671 val = boolean_true_node;
2672 if (!min_lt)
2673 val = boolean_false_node;
2675 else if (code == GT_EXPR)
2677 if (min_gt)
2678 val = boolean_true_node;
2679 if (!max_gt)
2680 val = boolean_false_node;
2682 else if (code == LE_EXPR)
2684 if (!max_gt)
2685 val = boolean_true_node;
2686 if (min_gt)
2687 val = boolean_false_node;
2689 else if (code == GE_EXPR)
2691 if (!min_lt)
2692 val = boolean_true_node;
2693 if (max_lt)
2694 val = boolean_false_node;
2697 /* If primop0 was sign-extended and unsigned comparison specd,
2698 we did a signed comparison above using the signed type bounds.
2699 But the comparison we output must be unsigned.
2701 Also, for inequalities, VAL is no good; but if the signed
2702 comparison had *any* fixed result, it follows that the
2703 unsigned comparison just tests the sign in reverse
2704 (positive values are LE, negative ones GE).
2705 So we can generate an unsigned comparison
2706 against an extreme value of the signed type. */
2708 if (unsignedp && !unsignedp0)
2710 if (val != 0)
2711 switch (code)
2713 case LT_EXPR:
2714 case GE_EXPR:
2715 primop1 = TYPE_MIN_VALUE (type);
2716 val = 0;
2717 break;
2719 case LE_EXPR:
2720 case GT_EXPR:
2721 primop1 = TYPE_MAX_VALUE (type);
2722 val = 0;
2723 break;
2725 default:
2726 break;
2728 type = unsigned_type (type);
2731 if (!max_gt && !unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
2733 /* This is the case of (char)x >?< 0x80, which people used to use
2734 expecting old C compilers to change the 0x80 into -0x80. */
2735 if (val == boolean_false_node)
2736 warning ("comparison is always false due to limited range of data type");
2737 if (val == boolean_true_node)
2738 warning ("comparison is always true due to limited range of data type");
2741 if (!min_lt && unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
2743 /* This is the case of (unsigned char)x >?< -1 or < 0. */
2744 if (val == boolean_false_node)
2745 warning ("comparison is always false due to limited range of data type");
2746 if (val == boolean_true_node)
2747 warning ("comparison is always true due to limited range of data type");
2750 if (val != 0)
2752 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2753 if (TREE_SIDE_EFFECTS (primop0))
2754 return build (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
2755 return val;
2758 /* Value is not predetermined, but do the comparison
2759 in the type of the operand that is not constant.
2760 TYPE is already properly set. */
2762 else if (real1 && real2
2763 && (TYPE_PRECISION (TREE_TYPE (primop0))
2764 == TYPE_PRECISION (TREE_TYPE (primop1))))
2765 type = TREE_TYPE (primop0);
2767 /* If args' natural types are both narrower than nominal type
2768 and both extend in the same manner, compare them
2769 in the type of the wider arg.
2770 Otherwise must actually extend both to the nominal
2771 common type lest different ways of extending
2772 alter the result.
2773 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
2775 else if (unsignedp0 == unsignedp1 && real1 == real2
2776 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
2777 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
2779 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
2780 type = signed_or_unsigned_type (unsignedp0
2781 || TREE_UNSIGNED (*restype_ptr),
2782 type);
2783 /* Make sure shorter operand is extended the right way
2784 to match the longer operand. */
2785 primop0 = convert (signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0)),
2786 primop0);
2787 primop1 = convert (signed_or_unsigned_type (unsignedp1, TREE_TYPE (primop1)),
2788 primop1);
2790 else
2792 /* Here we must do the comparison on the nominal type
2793 using the args exactly as we received them. */
2794 type = *restype_ptr;
2795 primop0 = op0;
2796 primop1 = op1;
2798 if (!real1 && !real2 && integer_zerop (primop1)
2799 && TREE_UNSIGNED (*restype_ptr))
2801 tree value = 0;
2802 switch (code)
2804 case GE_EXPR:
2805 /* All unsigned values are >= 0, so we warn if extra warnings
2806 are requested. However, if OP0 is a constant that is
2807 >= 0, the signedness of the comparison isn't an issue,
2808 so suppress the warning. */
2809 if (extra_warnings
2810 && ! (TREE_CODE (primop0) == INTEGER_CST
2811 && ! TREE_OVERFLOW (convert (signed_type (type),
2812 primop0))))
2813 warning ("comparison of unsigned expression >= 0 is always true");
2814 value = boolean_true_node;
2815 break;
2817 case LT_EXPR:
2818 if (extra_warnings
2819 && ! (TREE_CODE (primop0) == INTEGER_CST
2820 && ! TREE_OVERFLOW (convert (signed_type (type),
2821 primop0))))
2822 warning ("comparison of unsigned expression < 0 is always false");
2823 value = boolean_false_node;
2824 break;
2826 default:
2827 break;
2830 if (value != 0)
2832 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2833 if (TREE_SIDE_EFFECTS (primop0))
2834 return build (COMPOUND_EXPR, TREE_TYPE (value),
2835 primop0, value);
2836 return value;
2841 *op0_ptr = convert (type, primop0);
2842 *op1_ptr = convert (type, primop1);
2844 *restype_ptr = boolean_type_node;
2846 return 0;
2849 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
2850 or validate its data type for an `if' or `while' statement or ?..: exp.
2852 This preparation consists of taking the ordinary
2853 representation of an expression expr and producing a valid tree
2854 boolean expression describing whether expr is nonzero. We could
2855 simply always do build_binary_op (NE_EXPR, expr, boolean_false_node, 1),
2856 but we optimize comparisons, &&, ||, and !.
2858 The resulting type should always be `boolean_type_node'. */
2860 tree
2861 truthvalue_conversion (expr)
2862 tree expr;
2864 if (TREE_CODE (expr) == ERROR_MARK)
2865 return expr;
2867 #if 0 /* This appears to be wrong for C++. */
2868 /* These really should return error_mark_node after 2.4 is stable.
2869 But not all callers handle ERROR_MARK properly. */
2870 switch (TREE_CODE (TREE_TYPE (expr)))
2872 case RECORD_TYPE:
2873 error ("struct type value used where scalar is required");
2874 return boolean_false_node;
2876 case UNION_TYPE:
2877 error ("union type value used where scalar is required");
2878 return boolean_false_node;
2880 case ARRAY_TYPE:
2881 error ("array type value used where scalar is required");
2882 return boolean_false_node;
2884 default:
2885 break;
2887 #endif /* 0 */
2889 switch (TREE_CODE (expr))
2891 /* It is simpler and generates better code to have only TRUTH_*_EXPR
2892 or comparison expressions as truth values at this level. */
2893 #if 0
2894 case COMPONENT_REF:
2895 /* A one-bit unsigned bit-field is already acceptable. */
2896 if (1 == TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (expr, 1)))
2897 && TREE_UNSIGNED (TREE_OPERAND (expr, 1)))
2898 return expr;
2899 break;
2900 #endif
2902 case EQ_EXPR:
2903 /* It is simpler and generates better code to have only TRUTH_*_EXPR
2904 or comparison expressions as truth values at this level. */
2905 #if 0
2906 if (integer_zerop (TREE_OPERAND (expr, 1)))
2907 return build_unary_op (TRUTH_NOT_EXPR, TREE_OPERAND (expr, 0), 0);
2908 #endif
2909 case NE_EXPR: case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
2910 case TRUTH_ANDIF_EXPR:
2911 case TRUTH_ORIF_EXPR:
2912 case TRUTH_AND_EXPR:
2913 case TRUTH_OR_EXPR:
2914 case TRUTH_XOR_EXPR:
2915 case TRUTH_NOT_EXPR:
2916 TREE_TYPE (expr) = boolean_type_node;
2917 return expr;
2919 case ERROR_MARK:
2920 return expr;
2922 case INTEGER_CST:
2923 return integer_zerop (expr) ? boolean_false_node : boolean_true_node;
2925 case REAL_CST:
2926 return real_zerop (expr) ? boolean_false_node : boolean_true_node;
2928 case ADDR_EXPR:
2929 /* If we are taking the address of a external decl, it might be zero
2930 if it is weak, so we cannot optimize. */
2931 if (TREE_CODE_CLASS (TREE_CODE (TREE_OPERAND (expr, 0))) == 'd'
2932 && DECL_EXTERNAL (TREE_OPERAND (expr, 0)))
2933 break;
2935 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 0)))
2936 return build (COMPOUND_EXPR, boolean_type_node,
2937 TREE_OPERAND (expr, 0), boolean_true_node);
2938 else
2939 return boolean_true_node;
2941 case COMPLEX_EXPR:
2942 return build_binary_op ((TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
2943 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
2944 truthvalue_conversion (TREE_OPERAND (expr, 0)),
2945 truthvalue_conversion (TREE_OPERAND (expr, 1)),
2948 case NEGATE_EXPR:
2949 case ABS_EXPR:
2950 case FLOAT_EXPR:
2951 case FFS_EXPR:
2952 /* These don't change whether an object is non-zero or zero. */
2953 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2955 case LROTATE_EXPR:
2956 case RROTATE_EXPR:
2957 /* These don't change whether an object is zero or non-zero, but
2958 we can't ignore them if their second arg has side-effects. */
2959 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
2960 return build (COMPOUND_EXPR, boolean_type_node, TREE_OPERAND (expr, 1),
2961 truthvalue_conversion (TREE_OPERAND (expr, 0)));
2962 else
2963 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2965 case COND_EXPR:
2966 /* Distribute the conversion into the arms of a COND_EXPR. */
2967 return fold (build (COND_EXPR, boolean_type_node, TREE_OPERAND (expr, 0),
2968 truthvalue_conversion (TREE_OPERAND (expr, 1)),
2969 truthvalue_conversion (TREE_OPERAND (expr, 2))));
2971 case CONVERT_EXPR:
2972 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
2973 since that affects how `default_conversion' will behave. */
2974 if (TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE
2975 || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
2976 break;
2977 /* fall through... */
2978 case NOP_EXPR:
2979 /* If this is widening the argument, we can ignore it. */
2980 if (TYPE_PRECISION (TREE_TYPE (expr))
2981 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
2982 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2983 break;
2985 case MINUS_EXPR:
2986 /* With IEEE arithmetic, x - x may not equal 0, so we can't optimize
2987 this case. */
2988 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
2989 && TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE)
2990 break;
2991 /* fall through... */
2992 case BIT_XOR_EXPR:
2993 /* This and MINUS_EXPR can be changed into a comparison of the
2994 two objects. */
2995 if (TREE_TYPE (TREE_OPERAND (expr, 0))
2996 == TREE_TYPE (TREE_OPERAND (expr, 1)))
2997 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2998 TREE_OPERAND (expr, 1), 1);
2999 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
3000 fold (build1 (NOP_EXPR,
3001 TREE_TYPE (TREE_OPERAND (expr, 0)),
3002 TREE_OPERAND (expr, 1))), 1);
3004 case BIT_AND_EXPR:
3005 if (integer_onep (TREE_OPERAND (expr, 1))
3006 && TREE_TYPE (expr) != boolean_type_node)
3007 /* Using convert here would cause infinite recursion. */
3008 return build1 (NOP_EXPR, boolean_type_node, expr);
3009 break;
3011 case MODIFY_EXPR:
3012 if (warn_parentheses && C_EXP_ORIGINAL_CODE (expr) == MODIFY_EXPR)
3013 warning ("suggest parentheses around assignment used as truth value");
3014 break;
3016 default:
3017 break;
3020 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
3022 tree tem = save_expr (expr);
3023 return (build_binary_op
3024 ((TREE_SIDE_EFFECTS (expr)
3025 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
3026 truthvalue_conversion (build_unary_op (REALPART_EXPR, tem, 0)),
3027 truthvalue_conversion (build_unary_op (IMAGPART_EXPR, tem, 0)),
3028 0));
3031 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
3034 #if USE_CPPLIB
3035 /* Read the rest of a #-directive from input stream FINPUT.
3036 In normal use, the directive name and the white space after it
3037 have already been read, so they won't be included in the result.
3038 We allow for the fact that the directive line may contain
3039 a newline embedded within a character or string literal which forms
3040 a part of the directive.
3042 The value is a string in a reusable buffer. It remains valid
3043 only until the next time this function is called. */
3044 unsigned char *yy_cur, *yy_lim;
3046 #define GETC() (yy_cur < yy_lim ? *yy_cur++ : yy_get_token ())
3047 #define UNGETC(c) ((c) == EOF ? 0 : yy_cur--)
3050 yy_get_token ()
3052 for (;;)
3054 parse_in.limit = parse_in.token_buffer;
3055 cpp_token = cpp_get_token (&parse_in);
3056 if (cpp_token == CPP_EOF)
3057 return -1;
3058 yy_lim = CPP_PWRITTEN (&parse_in);
3059 yy_cur = parse_in.token_buffer;
3060 if (yy_cur < yy_lim)
3061 return *yy_cur++;
3065 char *
3066 get_directive_line ()
3068 static char *directive_buffer = NULL;
3069 static unsigned buffer_length = 0;
3070 register char *p;
3071 register char *buffer_limit;
3072 register int looking_for = 0;
3073 register int char_escaped = 0;
3075 if (buffer_length == 0)
3077 directive_buffer = (char *)xmalloc (128);
3078 buffer_length = 128;
3081 buffer_limit = &directive_buffer[buffer_length];
3083 for (p = directive_buffer; ; )
3085 int c;
3087 /* Make buffer bigger if it is full. */
3088 if (p >= buffer_limit)
3090 register unsigned bytes_used = (p - directive_buffer);
3092 buffer_length *= 2;
3093 directive_buffer
3094 = (char *)xrealloc (directive_buffer, buffer_length);
3095 p = &directive_buffer[bytes_used];
3096 buffer_limit = &directive_buffer[buffer_length];
3099 c = GETC ();
3101 /* Discard initial whitespace. */
3102 if ((c == ' ' || c == '\t') && p == directive_buffer)
3103 continue;
3105 /* Detect the end of the directive. */
3106 if (c == '\n' && looking_for == 0)
3108 UNGETC (c);
3109 c = '\0';
3112 *p++ = c;
3114 if (c == 0)
3115 return directive_buffer;
3117 /* Handle string and character constant syntax. */
3118 if (looking_for)
3120 if (looking_for == c && !char_escaped)
3121 looking_for = 0; /* Found terminator... stop looking. */
3123 else
3124 if (c == '\'' || c == '"')
3125 looking_for = c; /* Don't stop buffering until we see another
3126 another one of these (or an EOF). */
3128 /* Handle backslash. */
3129 char_escaped = (c == '\\' && ! char_escaped);
3132 #else
3133 /* Read the rest of a #-directive from input stream FINPUT.
3134 In normal use, the directive name and the white space after it
3135 have already been read, so they won't be included in the result.
3136 We allow for the fact that the directive line may contain
3137 a newline embedded within a character or string literal which forms
3138 a part of the directive.
3140 The value is a string in a reusable buffer. It remains valid
3141 only until the next time this function is called.
3143 The terminating character ('\n' or EOF) is left in FINPUT for the
3144 caller to re-read. */
3146 char *
3147 get_directive_line (finput)
3148 register FILE *finput;
3150 static char *directive_buffer = NULL;
3151 static unsigned buffer_length = 0;
3152 register char *p;
3153 register char *buffer_limit;
3154 register int looking_for = 0;
3155 register int char_escaped = 0;
3157 if (buffer_length == 0)
3159 directive_buffer = (char *)xmalloc (128);
3160 buffer_length = 128;
3163 buffer_limit = &directive_buffer[buffer_length];
3165 for (p = directive_buffer; ; )
3167 int c;
3169 /* Make buffer bigger if it is full. */
3170 if (p >= buffer_limit)
3172 register unsigned bytes_used = (p - directive_buffer);
3174 buffer_length *= 2;
3175 directive_buffer
3176 = (char *)xrealloc (directive_buffer, buffer_length);
3177 p = &directive_buffer[bytes_used];
3178 buffer_limit = &directive_buffer[buffer_length];
3181 c = getc (finput);
3183 /* Discard initial whitespace. */
3184 if ((c == ' ' || c == '\t') && p == directive_buffer)
3185 continue;
3187 /* Detect the end of the directive. */
3188 if (looking_for == 0
3189 && (c == '\n' || c == EOF))
3191 ungetc (c, finput);
3192 c = '\0';
3195 *p++ = c;
3197 if (c == 0)
3198 return directive_buffer;
3200 /* Handle string and character constant syntax. */
3201 if (looking_for)
3203 if (looking_for == c && !char_escaped)
3204 looking_for = 0; /* Found terminator... stop looking. */
3206 else
3207 if (c == '\'' || c == '"')
3208 looking_for = c; /* Don't stop buffering until we see another
3209 one of these (or an EOF). */
3211 /* Handle backslash. */
3212 char_escaped = (c == '\\' && ! char_escaped);
3215 #endif /* !USE_CPPLIB */
3217 /* Make a variant type in the proper way for C/C++, propagating qualifiers
3218 down to the element type of an array. */
3220 tree
3221 c_build_qualified_type (type, type_quals)
3222 tree type;
3223 int type_quals;
3225 /* A restrict-qualified pointer type must be a pointer to object or
3226 incomplete type. Note that the use of POINTER_TYPE_P also allows
3227 REFERENCE_TYPEs, which is appropriate for C++. Unfortunately,
3228 the C++ front-end also use POINTER_TYPE for pointer-to-member
3229 values, so even though it should be illegal to use `restrict'
3230 with such an entity we don't flag that here. Thus, special case
3231 code for that case is required in the C++ front-end. */
3232 if ((type_quals & TYPE_QUAL_RESTRICT)
3233 && (!POINTER_TYPE_P (type)
3234 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
3236 error ("invalid use of `restrict'");
3237 type_quals &= ~TYPE_QUAL_RESTRICT;
3240 if (TREE_CODE (type) == ARRAY_TYPE)
3241 return build_array_type (c_build_qualified_type (TREE_TYPE (type),
3242 type_quals),
3243 TYPE_DOMAIN (type));
3244 return build_qualified_type (type, type_quals);
3247 /* Apply the TYPE_QUALS to the new DECL. */
3249 void
3250 c_apply_type_quals_to_decl (type_quals, decl)
3251 int type_quals;
3252 tree decl;
3254 if (type_quals & TYPE_QUAL_CONST)
3255 TREE_READONLY (decl) = 1;
3256 if (type_quals & TYPE_QUAL_VOLATILE)
3258 TREE_SIDE_EFFECTS (decl) = 1;
3259 TREE_THIS_VOLATILE (decl) = 1;
3261 if (type_quals & TYPE_QUAL_RESTRICT)
3263 if (!TREE_TYPE (decl)
3264 || !POINTER_TYPE_P (TREE_TYPE (decl))
3265 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (TREE_TYPE (decl))))
3266 error ("invalid use of `restrict'");
3267 else if (flag_strict_aliasing)
3269 /* No two restricted pointers can point at the same thing.
3270 However, a restricted pointer can point at the same thing
3271 as an unrestricted pointer, if that unrestricted pointer
3272 is based on the restricted pointer. So, we make the
3273 alias set for the restricted pointer a subset of the
3274 alias set for the type pointed to by the type of the
3275 decl. */
3277 int pointed_to_alias_set
3278 = get_alias_set (TREE_TYPE (TREE_TYPE (decl)));
3280 if (!pointed_to_alias_set)
3281 /* It's not legal to make a subset of alias set zero. */
3283 else
3285 DECL_POINTER_ALIAS_SET (decl) = new_alias_set ();
3286 record_alias_subset (pointed_to_alias_set,
3287 DECL_POINTER_ALIAS_SET (decl));
3293 /* T is an expression with pointer type. Find the DECL on which this
3294 expression is based. (For example, in `a[i]' this would be `a'.)
3295 If there is no such DECL, or a unique decl cannot be determined,
3296 NULL_TREE is retured. */
3298 static tree
3299 c_find_base_decl (t)
3300 tree t;
3302 int i;
3303 tree decl;
3305 if (t == NULL_TREE || t == error_mark_node)
3306 return NULL_TREE;
3308 if (!POINTER_TYPE_P (TREE_TYPE (t)))
3309 return NULL_TREE;
3311 decl = NULL_TREE;
3313 if (TREE_CODE (t) == FIELD_DECL
3314 || TREE_CODE (t) == PARM_DECL
3315 || TREE_CODE (t) == VAR_DECL)
3316 /* Aha, we found a pointer-typed declaration. */
3317 return t;
3319 /* It would be nice to deal with COMPONENT_REFs here. If we could
3320 tell that `a' and `b' were the same, then `a->f' and `b->f' are
3321 also the same. */
3323 /* Handle general expressions. */
3324 switch (TREE_CODE_CLASS (TREE_CODE (t)))
3326 case '1':
3327 case '2':
3328 case '3':
3329 for (i = tree_code_length [(int) TREE_CODE (t)]; --i >= 0;)
3331 tree d = c_find_base_decl (TREE_OPERAND (t, i));
3332 if (d)
3334 if (!decl)
3335 decl = d;
3336 else if (d && d != decl)
3337 /* Two different declarations. That's confusing; let's
3338 just assume we don't know what's going on. */
3339 decl = NULL_TREE;
3342 break;
3344 default:
3345 break;
3348 return decl;
3351 /* Return the typed-based alias set for T, which may be an expression
3352 or a type. */
3355 c_get_alias_set (t)
3356 tree t;
3358 tree type;
3359 tree u;
3361 if (t == error_mark_node)
3362 return 0;
3364 type = (TREE_CODE_CLASS (TREE_CODE (t)) == 't')
3365 ? t : TREE_TYPE (t);
3367 if (type == error_mark_node)
3368 return 0;
3370 /* Deal with special cases first; for certain kinds of references
3371 we're interested in more than just the type. */
3373 if (TREE_CODE (t) == BIT_FIELD_REF)
3374 /* Perhaps reads and writes to this piece of data alias fields
3375 neighboring the bitfield. Perhaps that's impossible. For now,
3376 let's just assume that bitfields can alias everything, which is
3377 the conservative assumption. */
3378 return 0;
3380 /* Permit type-punning when accessing a union, provided the access
3381 is directly through the union. For example, this code does not
3382 permit taking the address of a union member and then storing
3383 through it. Even the type-punning allowed here is a GCC
3384 extension, albeit a common and useful one; the C standard says
3385 that such accesses have implementation-defined behavior. */
3386 for (u = t;
3387 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
3388 u = TREE_OPERAND (u, 0))
3389 if (TREE_CODE (u) == COMPONENT_REF
3390 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
3391 return 0;
3393 if (TREE_CODE (t) == INDIRECT_REF)
3395 /* Check for accesses through restrict-qualified pointers. */
3396 tree decl = c_find_base_decl (TREE_OPERAND (t, 0));
3398 if (decl && DECL_POINTER_ALIAS_SET_KNOWN_P (decl))
3399 /* We use the alias set indicated in the declaration. */
3400 return DECL_POINTER_ALIAS_SET (decl);
3403 /* From here on, only the type matters. */
3405 if (TREE_CODE (t) == COMPONENT_REF
3406 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (t, 1)))
3407 /* Since build_modify_expr calls get_unwidened for stores to
3408 component references, the type of a bit field can be changed
3409 from (say) `unsigned int : 16' to `unsigned short' or from
3410 `enum E : 16' to `short'. We want the real type of the
3411 bit-field in this case, not some the integral equivalent. */
3412 type = DECL_BIT_FIELD_TYPE (TREE_OPERAND (t, 1));
3414 if (TYPE_ALIAS_SET_KNOWN_P (type))
3415 /* If we've already calculated the value, just return it. */
3416 return TYPE_ALIAS_SET (type);
3417 else if (TYPE_MAIN_VARIANT (type) != type)
3418 /* The C standard specifically allows aliasing between
3419 cv-qualified variants of types. */
3420 TYPE_ALIAS_SET (type) = c_get_alias_set (TYPE_MAIN_VARIANT (type));
3421 else if (TREE_CODE (type) == INTEGER_TYPE)
3423 tree signed_variant;
3425 /* The C standard specifically allows aliasing between signed and
3426 unsigned variants of the same type. We treat the signed
3427 variant as canonical. */
3428 signed_variant = signed_type (type);
3430 if (signed_variant != type)
3431 TYPE_ALIAS_SET (type) = c_get_alias_set (signed_variant);
3432 else if (signed_variant == signed_char_type_node)
3433 /* The C standard guarantess that any object may be accessed
3434 via an lvalue that has character type. We don't have to
3435 check for unsigned_char_type_node or char_type_node because
3436 we are specifically looking at the signed variant. */
3437 TYPE_ALIAS_SET (type) = 0;
3439 else if (TREE_CODE (type) == ARRAY_TYPE)
3440 /* Anything that can alias one of the array elements can alias
3441 the entire array as well. */
3442 TYPE_ALIAS_SET (type) = c_get_alias_set (TREE_TYPE (type));
3443 else if (TREE_CODE (type) == FUNCTION_TYPE)
3444 /* There are no objects of FUNCTION_TYPE, so there's no point in
3445 using up an alias set for them. (There are, of course,
3446 pointers and references to functions, but that's
3447 different.) */
3448 TYPE_ALIAS_SET (type) = 0;
3449 else if (TREE_CODE (type) == RECORD_TYPE
3450 || TREE_CODE (type) == UNION_TYPE)
3451 /* If TYPE is a struct or union type then we're reading or
3452 writing an entire struct. Thus, we don't know anything about
3453 aliasing. (In theory, such an access can only alias objects
3454 whose type is the same as one of the fields, recursively, but
3455 we don't yet make any use of that information.) */
3456 TYPE_ALIAS_SET (type) = 0;
3457 else if (TREE_CODE (type) == POINTER_TYPE
3458 || TREE_CODE (type) == REFERENCE_TYPE)
3460 tree t;
3462 /* Unfortunately, there is no canonical form of a pointer type.
3463 In particular, if we have `typedef int I', then `int *', and
3464 `I *' are different types. So, we have to pick a canonical
3465 representative. We do this below.
3467 Technically, this approach is actually more conservative that
3468 it needs to be. In particular, `const int *' and `int *'
3469 chould be in different alias sets, according to the C and C++
3470 standard, since their types are not the same, and so,
3471 technically, an `int **' and `const int **' cannot point at
3472 the same thing.
3474 But, the standard is wrong. In particular, this code is
3475 legal C++:
3477 int *ip;
3478 int **ipp = &ip;
3479 const int* const* cipp = &ip;
3481 And, it doesn't make sense for that to be legal unless you
3482 can dereference IPP and CIPP. So, we ignore cv-qualifiers on
3483 the pointed-to types. This issue has been reported to the
3484 C++ committee. */
3485 t = TYPE_MAIN_VARIANT (TREE_TYPE (type));
3486 t = ((TREE_CODE (type) == POINTER_TYPE)
3487 ? build_pointer_type (t) : build_reference_type (t));
3488 if (t != type)
3489 TYPE_ALIAS_SET (type) = c_get_alias_set (t);
3492 if (!TYPE_ALIAS_SET_KNOWN_P (type))
3494 /* Types that are not global ('permanent') are not
3495 placed in the type hash table. Thus, there can be multiple
3496 copies of identical types in local scopes. In the long run,
3497 all types should be permanent. */
3498 if (! TREE_PERMANENT (type))
3499 TYPE_ALIAS_SET (type) = 0;
3500 else
3501 /* TYPE is something we haven't seen before. Put it in a new
3502 alias set. */
3503 TYPE_ALIAS_SET (type) = new_alias_set ();
3506 return TYPE_ALIAS_SET (type);
3509 /* Build tree nodes and builtin functions common to both C and C++ language
3510 frontends.
3511 CPLUS_MODE is nonzero if we are called from the C++ frontend, we generate
3512 some stricter prototypes in that case.
3513 NO_BUILTINS and NO_NONANSI_BUILTINS contain the respective values of
3514 the language frontend flags flag_no_builtin and
3515 flag_no_nonansi_builtin. */
3516 void
3517 c_common_nodes_and_builtins (cplus_mode, no_builtins, no_nonansi_builtins)
3518 int cplus_mode, no_builtins, no_nonansi_builtins;
3520 tree temp;
3521 tree memcpy_ftype, memset_ftype, strlen_ftype;
3522 tree endlink, int_endlink, double_endlink, unsigned_endlink;
3523 tree sizetype_endlink;
3524 tree ptr_ftype, ptr_ftype_unsigned;
3525 tree void_ftype_any, void_ftype_int;
3526 tree double_ftype_double, double_ftype_double_double;
3527 tree float_ftype_float, ldouble_ftype_ldouble;
3528 tree int_ftype_cptr_cptr_sizet;
3529 tree int_ftype_string_string, string_ftype_ptr_ptr;
3530 tree long_ftype_long;
3531 /* Either char* or void*. */
3532 tree traditional_ptr_type_node;
3533 tree va_list_ptr_type_node;
3534 tree va_list_arg_type_node;
3536 pushdecl (build_decl (TYPE_DECL, get_identifier ("__builtin_va_list"),
3537 va_list_type_node));
3539 pushdecl (build_decl (TYPE_DECL, get_identifier ("__builtin_ptrdiff_t"),
3540 ptrdiff_type_node));
3542 pushdecl (build_decl (TYPE_DECL, get_identifier ("__builtin_size_t"),
3543 sizetype));
3545 va_list_ptr_type_node = build_pointer_type (va_list_type_node);
3547 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
3548 va_list_arg_type_node = build_pointer_type (TREE_TYPE (va_list_type_node));
3549 else
3550 va_list_arg_type_node = va_list_type_node;
3552 endlink = void_list_node;
3553 int_endlink = tree_cons (NULL_TREE, integer_type_node, endlink);
3554 double_endlink = tree_cons (NULL_TREE, double_type_node, endlink);
3555 unsigned_endlink = tree_cons (NULL_TREE, unsigned_type_node, endlink);
3557 ptr_ftype = build_function_type (ptr_type_node, NULL_TREE);
3558 ptr_ftype_unsigned = build_function_type (ptr_type_node, unsigned_endlink);
3559 sizetype_endlink = tree_cons (NULL_TREE, sizetype, endlink);
3560 /* We realloc here because sizetype could be int or unsigned. S'ok. */
3561 ptr_ftype_sizetype = build_function_type (ptr_type_node, sizetype_endlink);
3563 void_ftype_any = build_function_type (void_type_node, NULL_TREE);
3564 void_ftype = build_function_type (void_type_node, endlink);
3565 void_ftype_int = build_function_type (void_type_node, int_endlink);
3566 void_ftype_ptr
3567 = build_function_type (void_type_node,
3568 tree_cons (NULL_TREE, ptr_type_node, endlink));
3570 float_ftype_float
3571 = build_function_type (float_type_node,
3572 tree_cons (NULL_TREE, float_type_node, endlink));
3574 double_ftype_double
3575 = build_function_type (double_type_node, double_endlink);
3577 ldouble_ftype_ldouble
3578 = build_function_type (long_double_type_node,
3579 tree_cons (NULL_TREE, long_double_type_node,
3580 endlink));
3582 double_ftype_double_double
3583 = build_function_type (double_type_node,
3584 tree_cons (NULL_TREE, double_type_node,
3585 double_endlink));
3587 int_ftype_int
3588 = build_function_type (integer_type_node, int_endlink);
3590 long_ftype_long
3591 = build_function_type (long_integer_type_node,
3592 tree_cons (NULL_TREE, long_integer_type_node,
3593 endlink));
3595 int_ftype_cptr_cptr_sizet
3596 = build_function_type (integer_type_node,
3597 tree_cons (NULL_TREE, const_ptr_type_node,
3598 tree_cons (NULL_TREE, const_ptr_type_node,
3599 tree_cons (NULL_TREE,
3600 sizetype,
3601 endlink))));
3603 /* Prototype for strcpy. */
3604 string_ftype_ptr_ptr
3605 = build_function_type (string_type_node,
3606 tree_cons (NULL_TREE, string_type_node,
3607 tree_cons (NULL_TREE,
3608 const_string_type_node,
3609 endlink)));
3611 /* Prototype for strcmp. */
3612 int_ftype_string_string
3613 = build_function_type (integer_type_node,
3614 tree_cons (NULL_TREE, const_string_type_node,
3615 tree_cons (NULL_TREE,
3616 const_string_type_node,
3617 endlink)));
3619 /* Prototype for strlen. */
3620 strlen_ftype
3621 = build_function_type ((flag_traditional && ! cplus_mode
3622 ? integer_type_node : sizetype),
3623 tree_cons (NULL_TREE, const_string_type_node,
3624 endlink));
3626 traditional_ptr_type_node = (flag_traditional && ! cplus_mode
3627 ? string_type_node : ptr_type_node);
3629 /* Prototype for memcpy. */
3630 memcpy_ftype
3631 = build_function_type (traditional_ptr_type_node,
3632 tree_cons (NULL_TREE, ptr_type_node,
3633 tree_cons (NULL_TREE, const_ptr_type_node,
3634 sizetype_endlink)));
3636 /* Prototype for memset. */
3637 memset_ftype
3638 = build_function_type (traditional_ptr_type_node,
3639 tree_cons (NULL_TREE, ptr_type_node,
3640 tree_cons (NULL_TREE, integer_type_node,
3641 tree_cons (NULL_TREE,
3642 sizetype,
3643 endlink))));
3645 builtin_function ("__builtin_constant_p", default_function_type,
3646 BUILT_IN_CONSTANT_P, BUILT_IN_NORMAL, NULL_PTR);
3648 builtin_function ("__builtin_return_address", ptr_ftype_unsigned,
3649 BUILT_IN_RETURN_ADDRESS, BUILT_IN_NORMAL, NULL_PTR);
3651 builtin_function ("__builtin_frame_address", ptr_ftype_unsigned,
3652 BUILT_IN_FRAME_ADDRESS, BUILT_IN_NORMAL, NULL_PTR);
3654 builtin_function ("__builtin_alloca", ptr_ftype_sizetype,
3655 BUILT_IN_ALLOCA, BUILT_IN_NORMAL, "alloca");
3656 builtin_function ("__builtin_ffs", int_ftype_int, BUILT_IN_FFS,
3657 BUILT_IN_NORMAL, NULL_PTR);
3658 /* Define alloca, ffs as builtins.
3659 Declare _exit just to mark it as volatile. */
3660 if (! no_builtins && ! no_nonansi_builtins)
3662 #ifndef SMALL_STACK
3663 temp = builtin_function ("alloca", ptr_ftype_sizetype,
3664 BUILT_IN_ALLOCA, BUILT_IN_NORMAL, NULL_PTR);
3665 /* Suppress error if redefined as a non-function. */
3666 DECL_BUILT_IN_NONANSI (temp) = 1;
3667 #endif
3668 temp = builtin_function ("ffs", int_ftype_int, BUILT_IN_FFS,
3669 BUILT_IN_NORMAL, NULL_PTR);
3670 /* Suppress error if redefined as a non-function. */
3671 DECL_BUILT_IN_NONANSI (temp) = 1;
3672 temp = builtin_function ("_exit", void_ftype_int,
3673 0, NOT_BUILT_IN, NULL_PTR);
3674 TREE_THIS_VOLATILE (temp) = 1;
3675 TREE_SIDE_EFFECTS (temp) = 1;
3676 /* Suppress error if redefined as a non-function. */
3677 DECL_BUILT_IN_NONANSI (temp) = 1;
3680 builtin_function ("__builtin_abs", int_ftype_int, BUILT_IN_ABS,
3681 BUILT_IN_NORMAL, NULL_PTR);
3682 builtin_function ("__builtin_fabsf", float_ftype_float, BUILT_IN_FABS,
3683 BUILT_IN_NORMAL, NULL_PTR);
3684 builtin_function ("__builtin_fabs", double_ftype_double, BUILT_IN_FABS,
3685 BUILT_IN_NORMAL, NULL_PTR);
3686 builtin_function ("__builtin_fabsl", ldouble_ftype_ldouble, BUILT_IN_FABS,
3687 BUILT_IN_NORMAL, NULL_PTR);
3688 builtin_function ("__builtin_labs", long_ftype_long, BUILT_IN_LABS,
3689 BUILT_IN_NORMAL, NULL_PTR);
3690 builtin_function ("__builtin_saveregs", ptr_ftype, BUILT_IN_SAVEREGS,
3691 BUILT_IN_NORMAL, NULL_PTR);
3692 builtin_function ("__builtin_classify_type", default_function_type,
3693 BUILT_IN_CLASSIFY_TYPE, BUILT_IN_NORMAL, NULL_PTR);
3694 builtin_function ("__builtin_next_arg", ptr_ftype, BUILT_IN_NEXT_ARG,
3695 BUILT_IN_NORMAL, NULL_PTR);
3696 builtin_function ("__builtin_args_info", int_ftype_int, BUILT_IN_ARGS_INFO,
3697 BUILT_IN_NORMAL, NULL_PTR);
3698 builtin_function ("__builtin_setjmp",
3699 build_function_type (integer_type_node,
3700 tree_cons (NULL_TREE, ptr_type_node,
3701 endlink)),
3702 BUILT_IN_SETJMP, BUILT_IN_NORMAL, NULL_PTR);
3703 builtin_function ("__builtin_longjmp",
3704 build_function_type (void_type_node,
3705 tree_cons (NULL_TREE, ptr_type_node,
3706 tree_cons (NULL_TREE,
3707 integer_type_node,
3708 endlink))),
3709 BUILT_IN_LONGJMP, BUILT_IN_NORMAL, NULL_PTR);
3710 builtin_function ("__builtin_trap", void_ftype, BUILT_IN_TRAP,
3711 BUILT_IN_NORMAL, NULL_PTR);
3713 /* ISO C99 IEEE Unordered compares. */
3714 builtin_function ("__builtin_isgreater", default_function_type,
3715 BUILT_IN_ISGREATER, BUILT_IN_NORMAL, NULL_PTR);
3716 builtin_function ("__builtin_isgreaterequal", default_function_type,
3717 BUILT_IN_ISGREATEREQUAL, BUILT_IN_NORMAL, NULL_PTR);
3718 builtin_function ("__builtin_isless", default_function_type,
3719 BUILT_IN_ISLESS, BUILT_IN_NORMAL, NULL_PTR);
3720 builtin_function ("__builtin_islessequal", default_function_type,
3721 BUILT_IN_ISLESSEQUAL, BUILT_IN_NORMAL, NULL_PTR);
3722 builtin_function ("__builtin_islessgreater", default_function_type,
3723 BUILT_IN_ISLESSGREATER, BUILT_IN_NORMAL, NULL_PTR);
3724 builtin_function ("__builtin_isunordered", default_function_type,
3725 BUILT_IN_ISUNORDERED, BUILT_IN_NORMAL, NULL_PTR);
3727 /* Untyped call and return. */
3728 builtin_function ("__builtin_apply_args", ptr_ftype,
3729 BUILT_IN_APPLY_ARGS, BUILT_IN_NORMAL, NULL_PTR);
3731 temp = tree_cons (NULL_TREE,
3732 build_pointer_type (build_function_type (void_type_node,
3733 NULL_TREE)),
3734 tree_cons (NULL_TREE,
3735 ptr_type_node,
3736 tree_cons (NULL_TREE,
3737 sizetype,
3738 endlink)));
3739 builtin_function ("__builtin_apply",
3740 build_function_type (ptr_type_node, temp),
3741 BUILT_IN_APPLY, BUILT_IN_NORMAL, NULL_PTR);
3742 builtin_function ("__builtin_return", void_ftype_ptr,
3743 BUILT_IN_RETURN, BUILT_IN_NORMAL, NULL_PTR);
3745 /* Support for varargs.h and stdarg.h. */
3746 builtin_function ("__builtin_varargs_start",
3747 build_function_type (void_type_node,
3748 tree_cons (NULL_TREE,
3749 va_list_ptr_type_node,
3750 endlink)),
3751 BUILT_IN_VARARGS_START, BUILT_IN_NORMAL, NULL_PTR);
3753 builtin_function ("__builtin_stdarg_start",
3754 build_function_type (void_type_node,
3755 tree_cons (NULL_TREE,
3756 va_list_ptr_type_node,
3757 NULL_TREE)),
3758 BUILT_IN_STDARG_START, BUILT_IN_NORMAL, NULL_PTR);
3760 builtin_function ("__builtin_va_end",
3761 build_function_type (void_type_node,
3762 tree_cons (NULL_TREE,
3763 va_list_arg_type_node,
3764 endlink)),
3765 BUILT_IN_VA_END, BUILT_IN_NORMAL, NULL_PTR);
3767 builtin_function ("__builtin_va_copy",
3768 build_function_type (void_type_node,
3769 tree_cons (NULL_TREE,
3770 va_list_ptr_type_node,
3771 tree_cons (NULL_TREE,
3772 va_list_arg_type_node,
3773 endlink))),
3774 BUILT_IN_VA_COPY, BUILT_IN_NORMAL, NULL_PTR);
3776 /* Currently under experimentation. */
3777 builtin_function ("__builtin_memcpy", memcpy_ftype, BUILT_IN_MEMCPY,
3778 BUILT_IN_NORMAL, "memcpy");
3779 builtin_function ("__builtin_memcmp", int_ftype_cptr_cptr_sizet,
3780 BUILT_IN_MEMCMP, BUILT_IN_NORMAL, "memcmp");
3781 builtin_function ("__builtin_memset", memset_ftype, BUILT_IN_MEMSET,
3782 BUILT_IN_NORMAL, "memset");
3783 builtin_function ("__builtin_strcmp", int_ftype_string_string,
3784 BUILT_IN_STRCMP, BUILT_IN_NORMAL, "strcmp");
3785 builtin_function ("__builtin_strcpy", string_ftype_ptr_ptr,
3786 BUILT_IN_STRCPY, BUILT_IN_NORMAL, "strcpy");
3787 builtin_function ("__builtin_strlen", strlen_ftype,
3788 BUILT_IN_STRLEN, BUILT_IN_NORMAL, "strlen");
3789 builtin_function ("__builtin_sqrtf", float_ftype_float,
3790 BUILT_IN_FSQRT, BUILT_IN_NORMAL, "sqrtf");
3791 builtin_function ("__builtin_fsqrt", double_ftype_double,
3792 BUILT_IN_FSQRT, BUILT_IN_NORMAL, "sqrt");
3793 builtin_function ("__builtin_sqrtl", ldouble_ftype_ldouble,
3794 BUILT_IN_FSQRT, BUILT_IN_NORMAL, "sqrtl");
3795 builtin_function ("__builtin_sinf", float_ftype_float,
3796 BUILT_IN_SIN, BUILT_IN_NORMAL, "sinf");
3797 builtin_function ("__builtin_sin", double_ftype_double,
3798 BUILT_IN_SIN, BUILT_IN_NORMAL, "sin");
3799 builtin_function ("__builtin_sinl", ldouble_ftype_ldouble,
3800 BUILT_IN_SIN, BUILT_IN_NORMAL, "sinl");
3801 builtin_function ("__builtin_cosf", float_ftype_float,
3802 BUILT_IN_COS, BUILT_IN_NORMAL, "cosf");
3803 builtin_function ("__builtin_cos", double_ftype_double,
3804 BUILT_IN_COS, BUILT_IN_NORMAL, "cos");
3805 builtin_function ("__builtin_cosl", ldouble_ftype_ldouble,
3806 BUILT_IN_COS, BUILT_IN_NORMAL, "cosl");
3808 if (! no_builtins)
3810 builtin_function ("abs", int_ftype_int, BUILT_IN_ABS,
3811 BUILT_IN_NORMAL, NULL_PTR);
3812 builtin_function ("fabsf", float_ftype_float, BUILT_IN_FABS,
3813 BUILT_IN_NORMAL, NULL_PTR);
3814 builtin_function ("fabs", double_ftype_double, BUILT_IN_FABS,
3815 BUILT_IN_NORMAL, NULL_PTR);
3816 builtin_function ("fabsl", ldouble_ftype_ldouble, BUILT_IN_FABS,
3817 BUILT_IN_NORMAL, NULL_PTR);
3818 builtin_function ("labs", long_ftype_long, BUILT_IN_LABS,
3819 BUILT_IN_NORMAL, NULL_PTR);
3820 builtin_function ("memcpy", memcpy_ftype, BUILT_IN_MEMCPY,
3821 BUILT_IN_NORMAL, NULL_PTR);
3822 builtin_function ("memcmp", int_ftype_cptr_cptr_sizet, BUILT_IN_MEMCMP,
3823 BUILT_IN_NORMAL, NULL_PTR);
3824 builtin_function ("memset", memset_ftype, BUILT_IN_MEMSET,
3825 BUILT_IN_NORMAL, NULL_PTR);
3826 builtin_function ("strcmp", int_ftype_string_string, BUILT_IN_STRCMP,
3827 BUILT_IN_NORMAL, NULL_PTR);
3828 builtin_function ("strcpy", string_ftype_ptr_ptr, BUILT_IN_STRCPY,
3829 BUILT_IN_NORMAL, NULL_PTR);
3830 builtin_function ("strlen", strlen_ftype, BUILT_IN_STRLEN,
3831 BUILT_IN_NORMAL, NULL_PTR);
3832 builtin_function ("sqrtf", float_ftype_float, BUILT_IN_FSQRT,
3833 BUILT_IN_NORMAL, NULL_PTR);
3834 builtin_function ("sqrt", double_ftype_double, BUILT_IN_FSQRT,
3835 BUILT_IN_NORMAL, NULL_PTR);
3836 builtin_function ("sqrtl", ldouble_ftype_ldouble, BUILT_IN_FSQRT,
3837 BUILT_IN_NORMAL, NULL_PTR);
3838 builtin_function ("sinf", float_ftype_float, BUILT_IN_SIN,
3839 BUILT_IN_NORMAL, NULL_PTR);
3840 builtin_function ("sin", double_ftype_double, BUILT_IN_SIN,
3841 BUILT_IN_NORMAL, NULL_PTR);
3842 builtin_function ("sinl", ldouble_ftype_ldouble, BUILT_IN_SIN,
3843 BUILT_IN_NORMAL, NULL_PTR);
3844 builtin_function ("cosf", float_ftype_float, BUILT_IN_COS,
3845 BUILT_IN_NORMAL, NULL_PTR);
3846 builtin_function ("cos", double_ftype_double, BUILT_IN_COS,
3847 BUILT_IN_NORMAL, NULL_PTR);
3848 builtin_function ("cosl", ldouble_ftype_ldouble, BUILT_IN_COS,
3849 BUILT_IN_NORMAL, NULL_PTR);
3851 /* Declare these functions volatile
3852 to avoid spurious "control drops through" warnings. */
3853 temp = builtin_function ("abort", cplus_mode ? void_ftype : void_ftype_any,
3854 0, NOT_BUILT_IN, NULL_PTR);
3855 TREE_THIS_VOLATILE (temp) = 1;
3856 TREE_SIDE_EFFECTS (temp) = 1;
3858 #if 0 /* ??? The C++ frontend used to do this. */
3859 /* Well, these are actually ANSI, but we can't set DECL_BUILT_IN on
3860 them... */
3861 DECL_BUILT_IN_NONANSI (temp) = 1;
3862 #endif
3863 temp = builtin_function ("exit",
3864 cplus_mode ? void_ftype_int : void_ftype_any,
3865 0, NOT_BUILT_IN, NULL_PTR);
3866 TREE_THIS_VOLATILE (temp) = 1;
3867 TREE_SIDE_EFFECTS (temp) = 1;
3869 #if 0 /* ??? The C++ frontend used to do this. */
3870 /* Well, these are actually ANSI, but we can't set DECL_BUILT_IN on
3871 them... */
3872 DECL_BUILT_IN_NONANSI (temp) = 1;
3873 #endif
3876 #if 0
3877 /* Support for these has not been written in either expand_builtin
3878 or build_function_call. */
3879 builtin_function ("__builtin_div", default_ftype, BUILT_IN_DIV,
3880 BUILT_IN_NORMAL, NULL_PTR);
3881 builtin_function ("__builtin_ldiv", default_ftype, BUILT_IN_LDIV,
3882 BUILT_IN_NORMAL, NULL_PTR);
3883 builtin_function ("__builtin_ffloor", double_ftype_double, BUILT_IN_FFLOOR,
3884 BUILT_IN_NORMAL, NULL_PTR);
3885 builtin_function ("__builtin_fceil", double_ftype_double, BUILT_IN_FCEIL,
3886 BUILT_IN_NORMAL, NULL_PTR);
3887 builtin_function ("__builtin_fmod", double_ftype_double_double,
3888 BUILT_IN_FMOD, BUILT_IN_NORMAL, NULL_PTR);
3889 builtin_function ("__builtin_frem", double_ftype_double_double,
3890 BUILT_IN_FREM, BUILT_IN_NORMAL, NULL_PTR);
3891 builtin_function ("__builtin_getexp", double_ftype_double, BUILT_IN_GETEXP,
3892 BUILT_IN_NORMAL, NULL_PTR);
3893 builtin_function ("__builtin_getman", double_ftype_double, BUILT_IN_GETMAN,
3894 BUILT_IN_NORMAL, NULL_PTR);
3895 #endif
3897 /* ??? Perhaps there's a better place to do this. But it is related
3898 to __builtin_va_arg, so it isn't that off-the-wall. */
3899 lang_type_promotes_to = simple_type_promotes_to;
3902 tree
3903 build_va_arg (expr, type)
3904 tree expr, type;
3906 return build1 (VA_ARG_EXPR, type, expr);
3909 /* Given a type, apply default promotions wrt unnamed function arguments
3910 and return the new type. Return NULL_TREE if no change. */
3911 /* ??? There is a function of the same name in the C++ front end that
3912 does something similar, but is more thorough and does not return NULL
3913 if no change. We could perhaps share code, but it would make the
3914 self_promoting_type property harder to identify. */
3916 tree
3917 simple_type_promotes_to (type)
3918 tree type;
3920 if (TYPE_MAIN_VARIANT (type) == float_type_node)
3921 return double_type_node;
3923 if (C_PROMOTING_INTEGER_TYPE_P (type))
3925 /* Traditionally, unsignedness is preserved in default promotions.
3926 Also preserve unsignedness if not really getting any wider. */
3927 if (TREE_UNSIGNED (type)
3928 && (flag_traditional
3929 || TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
3930 return unsigned_type_node;
3931 return integer_type_node;
3934 return NULL_TREE;
3937 /* Return 1 if PARMS specifies a fixed number of parameters
3938 and none of their types is affected by default promotions. */
3941 self_promoting_args_p (parms)
3942 tree parms;
3944 register tree t;
3945 for (t = parms; t; t = TREE_CHAIN (t))
3947 register tree type = TREE_VALUE (t);
3949 if (TREE_CHAIN (t) == 0 && type != void_type_node)
3950 return 0;
3952 if (type == 0)
3953 return 0;
3955 if (TYPE_MAIN_VARIANT (type) == float_type_node)
3956 return 0;
3958 if (C_PROMOTING_INTEGER_TYPE_P (type))
3959 return 0;
3961 return 1;
3964 /* Recognize certain built-in functions so we can make tree-codes
3965 other than CALL_EXPR. We do this when it enables fold-const.c
3966 to do something useful. */
3967 /* ??? By rights this should go in builtins.c, but only C and C++
3968 implement build_{binary,unary}_op. Not exactly sure what bits
3969 of functionality are actually needed from those functions, or
3970 where the similar functionality exists in the other front ends. */
3972 tree
3973 expand_tree_builtin (function, params, coerced_params)
3974 tree function, params, coerced_params;
3976 enum tree_code code;
3978 if (DECL_BUILT_IN_CLASS (function) != BUILT_IN_NORMAL)
3979 return NULL_TREE;
3981 switch (DECL_FUNCTION_CODE (function))
3983 case BUILT_IN_ABS:
3984 case BUILT_IN_LABS:
3985 case BUILT_IN_FABS:
3986 if (coerced_params == 0)
3987 return integer_zero_node;
3988 return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0);
3990 case BUILT_IN_ISGREATER:
3991 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
3992 code = UNLE_EXPR;
3993 else
3994 code = LE_EXPR;
3995 goto unordered_cmp;
3997 case BUILT_IN_ISGREATEREQUAL:
3998 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
3999 code = UNLT_EXPR;
4000 else
4001 code = LT_EXPR;
4002 goto unordered_cmp;
4004 case BUILT_IN_ISLESS:
4005 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
4006 code = UNGE_EXPR;
4007 else
4008 code = GE_EXPR;
4009 goto unordered_cmp;
4011 case BUILT_IN_ISLESSEQUAL:
4012 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
4013 code = UNGT_EXPR;
4014 else
4015 code = GT_EXPR;
4016 goto unordered_cmp;
4018 case BUILT_IN_ISLESSGREATER:
4019 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
4020 code = UNEQ_EXPR;
4021 else
4022 code = EQ_EXPR;
4023 goto unordered_cmp;
4025 case BUILT_IN_ISUNORDERED:
4026 if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT)
4027 return integer_zero_node;
4028 code = UNORDERED_EXPR;
4029 goto unordered_cmp;
4031 unordered_cmp:
4033 tree arg0, arg1;
4035 if (params == 0
4036 || TREE_CHAIN (params) == 0)
4038 error ("too few arguments to function `%s'",
4039 IDENTIFIER_POINTER (DECL_NAME (function)));
4040 return error_mark_node;
4042 else if (TREE_CHAIN (TREE_CHAIN (params)) != 0)
4044 error ("too many arguments to function `%s'",
4045 IDENTIFIER_POINTER (DECL_NAME (function)));
4046 return error_mark_node;
4049 arg0 = TREE_VALUE (params);
4050 arg1 = TREE_VALUE (TREE_CHAIN (params));
4051 arg0 = build_binary_op (code, arg0, arg1, 0);
4052 if (code != UNORDERED_EXPR)
4053 arg0 = build_unary_op (TRUTH_NOT_EXPR, arg0, 0);
4054 return arg0;
4056 break;
4058 default:
4059 break;
4062 return NULL_TREE;