1 /* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992, 93, 94, 95, 96, 97, 1998 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
36 cpp_options parse_options;
37 static enum cpp_token cpp_token;
40 #ifndef WCHAR_TYPE_SIZE
42 #define WCHAR_TYPE_SIZE INT_TYPE_SIZE
44 #define WCHAR_TYPE_SIZE BITS_PER_WORD
48 extern struct obstack permanent_obstack;
50 /* Nonzero means the expression being parsed will never be evaluated.
51 This is a count, since unevaluated expressions can nest. */
54 enum attrs {A_PACKED, A_NOCOMMON, A_COMMON, A_NORETURN, A_CONST, A_T_UNION,
55 A_NO_CHECK_MEMORY_USAGE, A_NO_INSTRUMENT_FUNCTION,
56 A_CONSTRUCTOR, A_DESTRUCTOR, A_MODE, A_SECTION, A_ALIGNED,
57 A_UNUSED, A_FORMAT, A_FORMAT_ARG, A_WEAK, A_ALIAS,
60 enum format_type { printf_format_type, scanf_format_type,
61 strftime_format_type };
63 static void declare_hidden_char_array PROTO((char *, char *));
64 static void add_attribute PROTO((enum attrs, char *,
66 static void init_attributes PROTO((void));
67 static void record_function_format PROTO((tree, tree, enum format_type,
69 static void record_international_format PROTO((tree, tree, int));
70 static tree c_find_base_decl PROTO((tree));
72 /* Keep a stack of if statements. We record the number of compound
73 statements seen up to the if keyword, as well as the line number
74 and file of the if. If a potentially ambiguous else is seen, that
75 fact is recorded; the warning is issued when we can be sure that
76 the enclosing if statement does not have an else branch. */
85 static if_elt *if_stack;
87 /* Amount of space in the if statement stack. */
88 static int if_stack_space = 0;
91 static int if_stack_pointer = 0;
93 /* Generate RTL for the start of an if-then, and record the start of it
94 for ambiguous else detection. */
96 /* A list of objects which have constructors or destructors which
97 reside in the global scope, and have an init_priority attribute
98 associated with them. The decl is stored in the TREE_VALUE slot
99 and the priority number is stored in the TREE_PURPOSE slot. */
100 tree static_aggregates_initp;
103 c_expand_start_cond (cond, exitflag, compstmt_count)
108 /* Make sure there is enough space on the stack. */
109 if (if_stack_space == 0)
112 if_stack = (if_elt *)xmalloc (10 * sizeof (if_elt));
114 else if (if_stack_space == if_stack_pointer)
116 if_stack_space += 10;
117 if_stack = (if_elt *)xrealloc (if_stack, if_stack_space * sizeof (if_elt));
120 /* Record this if statement. */
121 if_stack[if_stack_pointer].compstmt_count = compstmt_count;
122 if_stack[if_stack_pointer].file = input_filename;
123 if_stack[if_stack_pointer].line = lineno;
124 if_stack[if_stack_pointer].needs_warning = 0;
127 expand_start_cond (cond, exitflag);
130 /* Generate RTL for the end of an if-then. Optionally warn if a nested
131 if statement had an ambiguous else clause. */
137 if (if_stack[if_stack_pointer].needs_warning)
138 warning_with_file_and_line (if_stack[if_stack_pointer].file,
139 if_stack[if_stack_pointer].line,
140 "suggest explicit braces to avoid ambiguous `else'");
144 /* Generate RTL between the then-clause and the else-clause
145 of an if-then-else. */
148 c_expand_start_else ()
150 /* An ambiguous else warning must be generated for the enclosing if
151 statement, unless we see an else branch for that one, too. */
153 && if_stack_pointer > 1
154 && (if_stack[if_stack_pointer - 1].compstmt_count
155 == if_stack[if_stack_pointer - 2].compstmt_count))
156 if_stack[if_stack_pointer - 2].needs_warning = 1;
158 /* Even if a nested if statement had an else branch, it can't be
159 ambiguous if this one also has an else. So don't warn in that
160 case. Also don't warn for any if statements nested in this else. */
161 if_stack[if_stack_pointer - 1].needs_warning = 0;
162 if_stack[if_stack_pointer - 1].compstmt_count--;
164 expand_start_else ();
167 /* Make bindings for __FUNCTION__ and __PRETTY_FUNCTION__. */
170 declare_function_name ()
172 char *name, *printable_name;
174 if (current_function_decl == NULL)
177 printable_name = "top level";
181 /* Allow functions to be nameless (such as artificial ones). */
182 if (DECL_NAME (current_function_decl))
183 name = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
186 printable_name = (*decl_printable_name) (current_function_decl, 2);
189 declare_hidden_char_array ("__FUNCTION__", name);
190 declare_hidden_char_array ("__PRETTY_FUNCTION__", printable_name);
194 declare_hidden_char_array (name, value)
197 tree decl, type, init;
200 /* If the default size of char arrays isn't big enough for the name,
201 or if we want to give warnings for large objects, make a bigger one. */
202 vlen = strlen (value) + 1;
203 type = char_array_type_node;
204 if (TREE_INT_CST_LOW (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) < vlen
206 type = build_array_type (char_type_node,
207 build_index_type (build_int_2 (vlen, 0)));
208 push_obstacks_nochange ();
209 decl = build_decl (VAR_DECL, get_identifier (name), type);
210 TREE_STATIC (decl) = 1;
211 TREE_READONLY (decl) = 1;
212 TREE_ASM_WRITTEN (decl) = 1;
213 DECL_SOURCE_LINE (decl) = 0;
214 DECL_ARTIFICIAL (decl) = 1;
215 DECL_IN_SYSTEM_HEADER (decl) = 1;
216 DECL_IGNORED_P (decl) = 1;
217 init = build_string (vlen, value);
218 TREE_TYPE (init) = type;
219 DECL_INITIAL (decl) = init;
220 finish_decl (pushdecl (decl), init, NULL_TREE);
223 /* Given a chain of STRING_CST nodes,
224 concatenate them into one STRING_CST
225 and give it a suitable array-of-chars data type. */
228 combine_strings (strings)
231 register tree value, t;
232 register int length = 1;
235 int wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
238 if (TREE_CHAIN (strings))
240 /* More than one in the chain, so concatenate. */
241 register char *p, *q;
243 /* Don't include the \0 at the end of each substring,
244 except for the last one.
245 Count wide strings and ordinary strings separately. */
246 for (t = strings; t; t = TREE_CHAIN (t))
248 if (TREE_TYPE (t) == wchar_array_type_node)
250 wide_length += (TREE_STRING_LENGTH (t) - wchar_bytes);
254 length += (TREE_STRING_LENGTH (t) - 1);
257 /* If anything is wide, the non-wides will be converted,
258 which makes them take more space. */
260 length = length * wchar_bytes + wide_length;
262 p = savealloc (length);
264 /* Copy the individual strings into the new combined string.
265 If the combined string is wide, convert the chars to ints
266 for any individual strings that are not wide. */
269 for (t = strings; t; t = TREE_CHAIN (t))
271 int len = (TREE_STRING_LENGTH (t)
272 - ((TREE_TYPE (t) == wchar_array_type_node)
274 if ((TREE_TYPE (t) == wchar_array_type_node) == wide_flag)
276 memcpy (q, TREE_STRING_POINTER (t), len);
282 for (i = 0; i < len; i++)
284 if (WCHAR_TYPE_SIZE == HOST_BITS_PER_SHORT)
285 ((short *) q)[i] = TREE_STRING_POINTER (t)[i];
287 ((int *) q)[i] = TREE_STRING_POINTER (t)[i];
289 q += len * wchar_bytes;
295 for (i = 0; i < wchar_bytes; i++)
301 value = make_node (STRING_CST);
302 TREE_STRING_POINTER (value) = p;
303 TREE_STRING_LENGTH (value) = length;
308 length = TREE_STRING_LENGTH (value);
309 if (TREE_TYPE (value) == wchar_array_type_node)
313 /* Compute the number of elements, for the array type. */
314 nchars = wide_flag ? length / wchar_bytes : length;
316 /* Create the array type for the string constant.
317 -Wwrite-strings says make the string constant an array of const char
318 so that copying it to a non-const pointer will get a warning.
319 For C++, this is the standard behavior. */
320 if (flag_const_strings
321 && (! flag_traditional && ! flag_writable_strings))
324 = build_type_variant (wide_flag ? wchar_type_node : char_type_node,
327 = build_array_type (elements,
328 build_index_type (build_int_2 (nchars - 1, 0)));
332 = build_array_type (wide_flag ? wchar_type_node : char_type_node,
333 build_index_type (build_int_2 (nchars - 1, 0)));
335 TREE_READONLY (value) = TREE_CONSTANT (value) = ! flag_writable_strings;
336 TREE_STATIC (value) = 1;
340 /* To speed up processing of attributes, we maintain an array of
341 IDENTIFIER_NODES and the corresponding attribute types. */
343 /* Array to hold attribute information. */
345 static struct {enum attrs id; tree name; int min, max, decl_req;} attrtab[50];
347 static int attrtab_idx = 0;
349 /* Add an entry to the attribute table above. */
352 add_attribute (id, string, min_len, max_len, decl_req)
355 int min_len, max_len;
360 attrtab[attrtab_idx].id = id;
361 attrtab[attrtab_idx].name = get_identifier (string);
362 attrtab[attrtab_idx].min = min_len;
363 attrtab[attrtab_idx].max = max_len;
364 attrtab[attrtab_idx++].decl_req = decl_req;
366 sprintf (buf, "__%s__", string);
368 attrtab[attrtab_idx].id = id;
369 attrtab[attrtab_idx].name = get_identifier (buf);
370 attrtab[attrtab_idx].min = min_len;
371 attrtab[attrtab_idx].max = max_len;
372 attrtab[attrtab_idx++].decl_req = decl_req;
375 /* Initialize attribute table. */
380 add_attribute (A_PACKED, "packed", 0, 0, 0);
381 add_attribute (A_NOCOMMON, "nocommon", 0, 0, 1);
382 add_attribute (A_COMMON, "common", 0, 0, 1);
383 add_attribute (A_NORETURN, "noreturn", 0, 0, 1);
384 add_attribute (A_NORETURN, "volatile", 0, 0, 1);
385 add_attribute (A_UNUSED, "unused", 0, 0, 0);
386 add_attribute (A_CONST, "const", 0, 0, 1);
387 add_attribute (A_T_UNION, "transparent_union", 0, 0, 0);
388 add_attribute (A_CONSTRUCTOR, "constructor", 0, 0, 1);
389 add_attribute (A_DESTRUCTOR, "destructor", 0, 0, 1);
390 add_attribute (A_MODE, "mode", 1, 1, 1);
391 add_attribute (A_SECTION, "section", 1, 1, 1);
392 add_attribute (A_ALIGNED, "aligned", 0, 1, 0);
393 add_attribute (A_FORMAT, "format", 3, 3, 1);
394 add_attribute (A_FORMAT_ARG, "format_arg", 1, 1, 1);
395 add_attribute (A_WEAK, "weak", 0, 0, 1);
396 add_attribute (A_ALIAS, "alias", 1, 1, 1);
397 add_attribute (A_INIT_PRIORITY, "init_priority", 0, 1, 0);
398 add_attribute (A_NO_INSTRUMENT_FUNCTION, "no_instrument_function", 0, 0, 1);
399 add_attribute (A_NO_CHECK_MEMORY_USAGE, "no_check_memory_usage", 0, 0, 1);
402 /* Process the attributes listed in ATTRIBUTES and PREFIX_ATTRIBUTES
403 and install them in NODE, which is either a DECL (including a TYPE_DECL)
404 or a TYPE. PREFIX_ATTRIBUTES can appear after the declaration specifiers
405 and declaration modifiers but before the declaration proper. */
408 decl_attributes (node, attributes, prefix_attributes)
409 tree node, attributes, prefix_attributes;
411 tree decl = 0, type = 0;
415 if (attrtab_idx == 0)
418 if (TREE_CODE_CLASS (TREE_CODE (node)) == 'd')
421 type = TREE_TYPE (decl);
422 is_type = TREE_CODE (node) == TYPE_DECL;
424 else if (TREE_CODE_CLASS (TREE_CODE (node)) == 't')
425 type = node, is_type = 1;
427 #ifdef PRAGMA_INSERT_ATTRIBUTES
428 /* If the code in c-pragma.c wants to insert some attributes then
429 allow it to do so. Do this before allowing machine back ends to
430 insert attributes, so that they have the opportunity to override
431 anything done here. */
432 PRAGMA_INSERT_ATTRIBUTES (node, & attributes, & prefix_attributes);
435 #ifdef INSERT_ATTRIBUTES
436 INSERT_ATTRIBUTES (node, & attributes, & prefix_attributes);
439 attributes = chainon (prefix_attributes, attributes);
441 for (a = attributes; a; a = TREE_CHAIN (a))
443 tree name = TREE_PURPOSE (a);
444 tree args = TREE_VALUE (a);
448 for (i = 0; i < attrtab_idx; i++)
449 if (attrtab[i].name == name)
452 if (i == attrtab_idx)
454 if (! valid_machine_attribute (name, args, decl, type))
455 warning ("`%s' attribute directive ignored",
456 IDENTIFIER_POINTER (name));
458 type = TREE_TYPE (decl);
461 else if (attrtab[i].decl_req && decl == 0)
463 warning ("`%s' attribute does not apply to types",
464 IDENTIFIER_POINTER (name));
467 else if (list_length (args) < attrtab[i].min
468 || list_length (args) > attrtab[i].max)
470 error ("wrong number of arguments specified for `%s' attribute",
471 IDENTIFIER_POINTER (name));
480 TYPE_PACKED (type) = 1;
481 else if (TREE_CODE (decl) == FIELD_DECL)
482 DECL_PACKED (decl) = 1;
483 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
484 used for DECL_REGISTER. It wouldn't mean anything anyway. */
486 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
490 if (TREE_CODE (decl) == VAR_DECL)
491 DECL_COMMON (decl) = 0;
493 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
497 if (TREE_CODE (decl) == VAR_DECL)
498 DECL_COMMON (decl) = 1;
500 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
504 if (TREE_CODE (decl) == FUNCTION_DECL)
505 TREE_THIS_VOLATILE (decl) = 1;
506 else if (TREE_CODE (type) == POINTER_TYPE
507 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
508 TREE_TYPE (decl) = type
510 (build_type_variant (TREE_TYPE (type),
511 TREE_READONLY (TREE_TYPE (type)), 1));
513 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
518 TREE_USED (type) = 1;
519 else if (TREE_CODE (decl) == PARM_DECL
520 || TREE_CODE (decl) == VAR_DECL
521 || TREE_CODE (decl) == FUNCTION_DECL)
522 TREE_USED (decl) = 1;
524 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
528 if (TREE_CODE (decl) == FUNCTION_DECL)
529 TREE_READONLY (decl) = 1;
530 else if (TREE_CODE (type) == POINTER_TYPE
531 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
532 TREE_TYPE (decl) = type
534 (build_type_variant (TREE_TYPE (type), 1,
535 TREE_THIS_VOLATILE (TREE_TYPE (type))));
537 warning ( "`%s' attribute ignored", IDENTIFIER_POINTER (name));
542 && TREE_CODE (type) == UNION_TYPE
544 || (TYPE_FIELDS (type) != 0
545 && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))))
546 TYPE_TRANSPARENT_UNION (type) = 1;
547 else if (decl != 0 && TREE_CODE (decl) == PARM_DECL
548 && TREE_CODE (type) == UNION_TYPE
549 && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))
550 DECL_TRANSPARENT_UNION (decl) = 1;
552 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
556 if (TREE_CODE (decl) == FUNCTION_DECL
557 && TREE_CODE (type) == FUNCTION_TYPE
558 && decl_function_context (decl) == 0)
560 DECL_STATIC_CONSTRUCTOR (decl) = 1;
561 TREE_USED (decl) = 1;
564 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
568 if (TREE_CODE (decl) == FUNCTION_DECL
569 && TREE_CODE (type) == FUNCTION_TYPE
570 && decl_function_context (decl) == 0)
572 DECL_STATIC_DESTRUCTOR (decl) = 1;
573 TREE_USED (decl) = 1;
576 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
580 if (TREE_CODE (TREE_VALUE (args)) != IDENTIFIER_NODE)
581 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
585 char *p = IDENTIFIER_POINTER (TREE_VALUE (args));
586 int len = strlen (p);
587 enum machine_mode mode = VOIDmode;
590 if (len > 4 && p[0] == '_' && p[1] == '_'
591 && p[len - 1] == '_' && p[len - 2] == '_')
593 char *newp = (char *) alloca (len - 1);
595 strcpy (newp, &p[2]);
596 newp[len - 4] = '\0';
600 /* Give this decl a type with the specified mode.
601 First check for the special modes. */
602 if (! strcmp (p, "byte"))
604 else if (!strcmp (p, "word"))
606 else if (! strcmp (p, "pointer"))
609 for (j = 0; j < NUM_MACHINE_MODES; j++)
610 if (!strcmp (p, GET_MODE_NAME (j)))
611 mode = (enum machine_mode) j;
613 if (mode == VOIDmode)
614 error ("unknown machine mode `%s'", p);
615 else if (0 == (typefm = type_for_mode (mode,
616 TREE_UNSIGNED (type))))
617 error ("no data type for mode `%s'", p);
620 TREE_TYPE (decl) = type = typefm;
621 DECL_SIZE (decl) = 0;
622 layout_decl (decl, 0);
628 #ifdef ASM_OUTPUT_SECTION_NAME
629 if ((TREE_CODE (decl) == FUNCTION_DECL
630 || TREE_CODE (decl) == VAR_DECL)
631 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
633 if (TREE_CODE (decl) == VAR_DECL
634 && current_function_decl != NULL_TREE
635 && ! TREE_STATIC (decl))
636 error_with_decl (decl,
637 "section attribute cannot be specified for local variables");
638 /* The decl may have already been given a section attribute from
639 a previous declaration. Ensure they match. */
640 else if (DECL_SECTION_NAME (decl) != NULL_TREE
641 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
642 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
643 error_with_decl (node,
644 "section of `%s' conflicts with previous declaration");
646 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
649 error_with_decl (node,
650 "section attribute not allowed for `%s'");
652 error_with_decl (node,
653 "section attributes are not supported for this target");
660 = (args ? TREE_VALUE (args)
661 : size_int (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
664 /* Strip any NOPs of any kind. */
665 while (TREE_CODE (align_expr) == NOP_EXPR
666 || TREE_CODE (align_expr) == CONVERT_EXPR
667 || TREE_CODE (align_expr) == NON_LVALUE_EXPR)
668 align_expr = TREE_OPERAND (align_expr, 0);
670 if (TREE_CODE (align_expr) != INTEGER_CST)
672 error ("requested alignment is not a constant");
676 align = TREE_INT_CST_LOW (align_expr) * BITS_PER_UNIT;
678 if (exact_log2 (align) == -1)
679 error ("requested alignment is not a power of 2");
681 TYPE_ALIGN (type) = align;
682 else if (TREE_CODE (decl) != VAR_DECL
683 && TREE_CODE (decl) != FIELD_DECL)
684 error_with_decl (decl,
685 "alignment may not be specified for `%s'");
687 DECL_ALIGN (decl) = align;
693 tree format_type_id = TREE_VALUE (args);
694 tree format_num_expr = TREE_VALUE (TREE_CHAIN (args));
695 tree first_arg_num_expr
696 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)));
699 enum format_type format_type;
703 if (TREE_CODE (decl) != FUNCTION_DECL)
705 error_with_decl (decl,
706 "argument format specified for non-function `%s'");
710 if (TREE_CODE (format_type_id) != IDENTIFIER_NODE)
712 error ("unrecognized format specifier");
717 char *p = IDENTIFIER_POINTER (format_type_id);
719 if (!strcmp (p, "printf") || !strcmp (p, "__printf__"))
720 format_type = printf_format_type;
721 else if (!strcmp (p, "scanf") || !strcmp (p, "__scanf__"))
722 format_type = scanf_format_type;
723 else if (!strcmp (p, "strftime")
724 || !strcmp (p, "__strftime__"))
725 format_type = strftime_format_type;
728 error ("`%s' is an unrecognized format function type", p);
733 /* Strip any conversions from the string index and first arg number
734 and verify they are constants. */
735 while (TREE_CODE (format_num_expr) == NOP_EXPR
736 || TREE_CODE (format_num_expr) == CONVERT_EXPR
737 || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
738 format_num_expr = TREE_OPERAND (format_num_expr, 0);
740 while (TREE_CODE (first_arg_num_expr) == NOP_EXPR
741 || TREE_CODE (first_arg_num_expr) == CONVERT_EXPR
742 || TREE_CODE (first_arg_num_expr) == NON_LVALUE_EXPR)
743 first_arg_num_expr = TREE_OPERAND (first_arg_num_expr, 0);
745 if (TREE_CODE (format_num_expr) != INTEGER_CST
746 || TREE_CODE (first_arg_num_expr) != INTEGER_CST)
748 error ("format string has non-constant operand number");
752 format_num = TREE_INT_CST_LOW (format_num_expr);
753 first_arg_num = TREE_INT_CST_LOW (first_arg_num_expr);
754 if (first_arg_num != 0 && first_arg_num <= format_num)
756 error ("format string arg follows the args to be formatted");
760 /* If a parameter list is specified, verify that the format_num
761 argument is actually a string, in case the format attribute
763 argument = TYPE_ARG_TYPES (type);
766 for (arg_num = 1; ; ++arg_num)
768 if (argument == 0 || arg_num == format_num)
770 argument = TREE_CHAIN (argument);
773 || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
774 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
777 error ("format string arg not a string type");
780 if (first_arg_num != 0)
782 /* Verify that first_arg_num points to the last arg,
785 arg_num++, argument = TREE_CHAIN (argument);
786 if (arg_num != first_arg_num)
788 error ("args to be formatted is not ...");
794 record_function_format (DECL_NAME (decl),
795 DECL_ASSEMBLER_NAME (decl),
796 format_type, format_num, first_arg_num);
802 tree format_num_expr = TREE_VALUE (args);
803 int format_num, arg_num;
806 if (TREE_CODE (decl) != FUNCTION_DECL)
808 error_with_decl (decl,
809 "argument format specified for non-function `%s'");
813 /* Strip any conversions from the first arg number and verify it
815 while (TREE_CODE (format_num_expr) == NOP_EXPR
816 || TREE_CODE (format_num_expr) == CONVERT_EXPR
817 || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
818 format_num_expr = TREE_OPERAND (format_num_expr, 0);
820 if (TREE_CODE (format_num_expr) != INTEGER_CST)
822 error ("format string has non-constant operand number");
826 format_num = TREE_INT_CST_LOW (format_num_expr);
828 /* If a parameter list is specified, verify that the format_num
829 argument is actually a string, in case the format attribute
831 argument = TYPE_ARG_TYPES (type);
834 for (arg_num = 1; ; ++arg_num)
836 if (argument == 0 || arg_num == format_num)
838 argument = TREE_CHAIN (argument);
841 || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
842 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
845 error ("format string arg not a string type");
850 if (TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) != POINTER_TYPE
851 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (TREE_TYPE (decl))))
854 error ("function does not return string type");
858 record_international_format (DECL_NAME (decl),
859 DECL_ASSEMBLER_NAME (decl),
869 if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
870 || (TREE_CODE (decl) != FUNCTION_DECL && ! DECL_EXTERNAL (decl)))
871 error_with_decl (decl,
872 "`%s' defined both normally and as an alias");
873 else if (decl_function_context (decl) == 0)
877 id = TREE_VALUE (args);
878 if (TREE_CODE (id) != STRING_CST)
880 error ("alias arg not a string");
883 id = get_identifier (TREE_STRING_POINTER (id));
885 if (TREE_CODE (decl) == FUNCTION_DECL)
886 DECL_INITIAL (decl) = error_mark_node;
888 DECL_EXTERNAL (decl) = 0;
889 assemble_alias (decl, id);
892 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
895 case A_NO_CHECK_MEMORY_USAGE:
896 if (TREE_CODE (decl) != FUNCTION_DECL)
898 error_with_decl (decl,
899 "`%s' attribute applies only to functions",
900 IDENTIFIER_POINTER (name));
902 else if (DECL_INITIAL (decl))
904 error_with_decl (decl,
905 "can't set `%s' attribute after definition",
906 IDENTIFIER_POINTER (name));
909 DECL_NO_CHECK_MEMORY_USAGE (decl) = 1;
912 case A_INIT_PRIORITY:
914 tree initp_expr = (args ? TREE_VALUE (args): NULL_TREE);
918 STRIP_NOPS (initp_expr);
920 if (!initp_expr || TREE_CODE (initp_expr) != INTEGER_CST)
922 error ("requested init_priority is not an integer constant");
926 pri = TREE_INT_CST_LOW (initp_expr);
928 if (is_type || TREE_CODE (decl) != VAR_DECL
929 || ! TREE_STATIC (decl)
930 || DECL_EXTERNAL (decl)
931 || (TREE_CODE (TREE_TYPE (decl)) != RECORD_TYPE
932 && TREE_CODE (TREE_TYPE (decl)) != UNION_TYPE)
933 /* Static objects in functions are initialized the
934 first time control passes through that
935 function. This is not precise enough to pin down an
936 init_priority value, so don't allow it. */
937 || current_function_decl)
939 error ("can only use init_priority attribute on file-scope definitions of objects of class type");
943 /* Check for init_priorities that are reserved for
944 implementation. Reserved for language and runtime
945 support implementations.*/
946 if ((10 <= pri && pri <= 99)
947 /* Reserved for standard library implementations. */
948 || (500 <= pri && pri <= 999)
949 /* Reserved for objects with no attributes. */
950 || pri > (MAX_INIT_PRIORITY - 50))
953 ("requested init_priority is reserved for internal use");
957 if (pri > MAX_INIT_PRIORITY || pri <= 0)
959 error ("requested init_priority is out of range");
963 static_aggregates_initp
964 = perm_tree_cons (initp_expr, decl, static_aggregates_initp);
968 case A_NO_INSTRUMENT_FUNCTION:
969 if (TREE_CODE (decl) != FUNCTION_DECL)
971 error_with_decl (decl,
972 "`%s' attribute applies only to functions",
973 IDENTIFIER_POINTER (name));
975 else if (DECL_INITIAL (decl))
977 error_with_decl (decl,
978 "can't set `%s' attribute after definition",
979 IDENTIFIER_POINTER (name));
982 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
988 /* Split SPECS_ATTRS, a list of declspecs and prefix attributes, into two
989 lists. SPECS_ATTRS may also be just a typespec (eg: RECORD_TYPE).
991 The head of the declspec list is stored in DECLSPECS.
992 The head of the attribute list is stored in PREFIX_ATTRIBUTES.
994 Note that attributes in SPECS_ATTRS are stored in the TREE_PURPOSE of
995 the list elements. We drop the containing TREE_LIST nodes and link the
996 resulting attributes together the way decl_attributes expects them. */
999 split_specs_attrs (specs_attrs, declspecs, prefix_attributes)
1001 tree *declspecs, *prefix_attributes;
1003 tree t, s, a, next, specs, attrs;
1005 /* This can happen in c++ (eg: decl: typespec initdecls ';'). */
1006 if (specs_attrs != NULL_TREE
1007 && TREE_CODE (specs_attrs) != TREE_LIST)
1009 *declspecs = specs_attrs;
1010 *prefix_attributes = NULL_TREE;
1014 /* Remember to keep the lists in the same order, element-wise. */
1016 specs = s = NULL_TREE;
1017 attrs = a = NULL_TREE;
1018 for (t = specs_attrs; t; t = next)
1020 next = TREE_CHAIN (t);
1021 /* Declspecs have a non-NULL TREE_VALUE. */
1022 if (TREE_VALUE (t) != NULL_TREE)
1024 if (specs == NULL_TREE)
1034 if (attrs == NULL_TREE)
1035 attrs = a = TREE_PURPOSE (t);
1038 TREE_CHAIN (a) = TREE_PURPOSE (t);
1039 a = TREE_PURPOSE (t);
1041 /* More attrs can be linked here, move A to the end. */
1042 while (TREE_CHAIN (a) != NULL_TREE)
1047 /* Terminate the lists. */
1049 TREE_CHAIN (s) = NULL_TREE;
1051 TREE_CHAIN (a) = NULL_TREE;
1055 *prefix_attributes = attrs;
1058 /* Strip attributes from SPECS_ATTRS, a list of declspecs and attributes.
1059 This function is used by the parser when a rule will accept attributes
1060 in a particular position, but we don't want to support that just yet.
1062 A warning is issued for every ignored attribute. */
1065 strip_attrs (specs_attrs)
1070 split_specs_attrs (specs_attrs, &specs, &attrs);
1074 warning ("`%s' attribute ignored",
1075 IDENTIFIER_POINTER (TREE_PURPOSE (attrs)));
1076 attrs = TREE_CHAIN (attrs);
1082 /* Check a printf/fprintf/sprintf/scanf/fscanf/sscanf format against
1083 a parameter list. */
1085 #define T_I &integer_type_node
1086 #define T_L &long_integer_type_node
1087 #define T_LL &long_long_integer_type_node
1088 #define T_S &short_integer_type_node
1089 #define T_UI &unsigned_type_node
1090 #define T_UL &long_unsigned_type_node
1091 #define T_ULL &long_long_unsigned_type_node
1092 #define T_US &short_unsigned_type_node
1093 #define T_F &float_type_node
1094 #define T_D &double_type_node
1095 #define T_LD &long_double_type_node
1096 #define T_C &char_type_node
1097 #define T_UC &unsigned_char_type_node
1098 #define T_V &void_type_node
1099 #define T_W &wchar_type_node
1100 #define T_ST &sizetype
1105 /* Type of argument if no length modifier is used. */
1107 /* Type of argument if length modifier for shortening to byte is used.
1108 If NULL, then this modifier is not allowed. */
1110 /* Type of argument if length modifier for shortening is used.
1111 If NULL, then this modifier is not allowed. */
1113 /* Type of argument if length modifier `l' is used.
1114 If NULL, then this modifier is not allowed. */
1116 /* Type of argument if length modifier `q' or `ll' is used.
1117 If NULL, then this modifier is not allowed. */
1119 /* Type of argument if length modifier `L' is used.
1120 If NULL, then this modifier is not allowed. */
1122 /* Type of argument if length modifier `Z' is used.
1123 If NULL, then this modifier is not allowed. */
1125 /* List of other modifier characters allowed with these options. */
1129 static format_char_info print_char_table[] = {
1130 { "di", 0, T_I, T_I, T_I, T_L, T_LL, T_LL, T_ST, "-wp0 +" },
1131 { "oxX", 0, T_UI, T_UI, T_UI, T_UL, T_ULL, T_ULL, T_ST, "-wp0#" },
1132 { "u", 0, T_UI, T_UI, T_UI, T_UL, T_ULL, T_ULL, T_ST, "-wp0" },
1133 /* A GNU extension. */
1134 { "m", 0, T_V, NULL, NULL, NULL, NULL, NULL, NULL, "-wp" },
1135 { "feEgGaA", 0, T_D, NULL, NULL, NULL, NULL, T_LD, NULL, "-wp0 +#" },
1136 { "c", 0, T_I, NULL, NULL, T_W, NULL, NULL, NULL, "-w" },
1137 { "C", 0, T_W, NULL, NULL, NULL, NULL, NULL, NULL, "-w" },
1138 { "s", 1, T_C, NULL, NULL, T_W, NULL, NULL, NULL, "-wp" },
1139 { "S", 1, T_W, NULL, NULL, NULL, NULL, NULL, NULL, "-wp" },
1140 { "p", 1, T_V, NULL, NULL, NULL, NULL, NULL, NULL, "-w" },
1141 { "n", 1, T_I, NULL, T_S, T_L, T_LL, NULL, NULL, "" },
1142 { NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
1145 static format_char_info scan_char_table[] = {
1146 { "di", 1, T_I, T_C, T_S, T_L, T_LL, T_LL, NULL, "*" },
1147 { "ouxX", 1, T_UI, T_UC, T_US, T_UL, T_ULL, T_ULL, NULL, "*" },
1148 { "efgEGaA", 1, T_F, NULL, NULL, T_D, NULL, T_LD, NULL, "*" },
1149 { "c", 1, T_C, NULL, NULL, T_W, NULL, NULL, NULL, "*" },
1150 { "s", 1, T_C, NULL, NULL, T_W, NULL, NULL, NULL, "*a" },
1151 { "[", 1, T_C, NULL, NULL, NULL, NULL, NULL, NULL, "*a" },
1152 { "C", 1, T_W, NULL, NULL, NULL, NULL, NULL, NULL, "*" },
1153 { "S", 1, T_W, NULL, NULL, NULL, NULL, NULL, NULL, "*a" },
1154 { "p", 2, T_V, NULL, NULL, NULL, NULL, NULL, NULL, "*" },
1155 { "n", 1, T_I, T_C, T_S, T_L, T_LL, NULL, NULL, "" },
1156 { NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
1159 /* Handle format characters recognized by glibc's strftime.c.
1160 '2' - MUST do years as only two digits
1161 '3' - MAY do years as only two digits (depending on locale)
1162 'E' - E modifier is acceptable
1163 'O' - O modifier is acceptable to Standard C
1164 'o' - O modifier is acceptable as a GNU extension
1165 'G' - other GNU extensions */
1167 static format_char_info time_char_table[] = {
1168 { "y", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "2EO-_0w" },
1169 { "D", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "2" },
1170 { "g", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "2O-_0w" },
1171 { "cx", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "3E" },
1172 { "%RTXnrt", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "" },
1173 { "P", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "G" },
1174 { "HIMSUWdemw", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0Ow" },
1175 { "Vju", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0Oow" },
1176 { "Gklsz", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0OGw" },
1177 { "ABZa", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "^#" },
1178 { "p", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "#" },
1179 { "bh", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "^" },
1180 { "CY", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0EOw" },
1181 { NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
1184 typedef struct function_format_info
1186 struct function_format_info *next; /* next structure on the list */
1187 tree name; /* identifier such as "printf" */
1188 tree assembler_name; /* optional mangled identifier (for C++) */
1189 enum format_type format_type; /* type of format (printf, scanf, etc.) */
1190 int format_num; /* number of format argument */
1191 int first_arg_num; /* number of first arg (zero for varargs) */
1192 } function_format_info;
1194 static function_format_info *function_format_list = NULL;
1196 typedef struct international_format_info
1198 struct international_format_info *next; /* next structure on the list */
1199 tree name; /* identifier such as "gettext" */
1200 tree assembler_name; /* optional mangled identifier (for C++) */
1201 int format_num; /* number of format argument */
1202 } international_format_info;
1204 static international_format_info *international_format_list = NULL;
1206 static void check_format_info PROTO((function_format_info *, tree));
1208 /* Initialize the table of functions to perform format checking on.
1209 The ANSI functions are always checked (whether <stdio.h> is
1210 included or not), since it is common to call printf without
1211 including <stdio.h>. There shouldn't be a problem with this,
1212 since ANSI reserves these function names whether you include the
1213 header file or not. In any case, the checking is harmless.
1215 Also initialize the name of function that modify the format string for
1216 internationalization purposes. */
1219 init_function_format_info ()
1221 record_function_format (get_identifier ("printf"), NULL_TREE,
1222 printf_format_type, 1, 2);
1223 record_function_format (get_identifier ("fprintf"), NULL_TREE,
1224 printf_format_type, 2, 3);
1225 record_function_format (get_identifier ("sprintf"), NULL_TREE,
1226 printf_format_type, 2, 3);
1227 record_function_format (get_identifier ("scanf"), NULL_TREE,
1228 scanf_format_type, 1, 2);
1229 record_function_format (get_identifier ("fscanf"), NULL_TREE,
1230 scanf_format_type, 2, 3);
1231 record_function_format (get_identifier ("sscanf"), NULL_TREE,
1232 scanf_format_type, 2, 3);
1233 record_function_format (get_identifier ("vprintf"), NULL_TREE,
1234 printf_format_type, 1, 0);
1235 record_function_format (get_identifier ("vfprintf"), NULL_TREE,
1236 printf_format_type, 2, 0);
1237 record_function_format (get_identifier ("vsprintf"), NULL_TREE,
1238 printf_format_type, 2, 0);
1239 record_function_format (get_identifier ("strftime"), NULL_TREE,
1240 strftime_format_type, 3, 0);
1242 record_international_format (get_identifier ("gettext"), NULL_TREE, 1);
1243 record_international_format (get_identifier ("dgettext"), NULL_TREE, 2);
1244 record_international_format (get_identifier ("dcgettext"), NULL_TREE, 2);
1247 /* Record information for argument format checking. FUNCTION_IDENT is
1248 the identifier node for the name of the function to check (its decl
1249 need not exist yet).
1250 FORMAT_TYPE specifies the type of format checking. FORMAT_NUM is the number
1251 of the argument which is the format control string (starting from 1).
1252 FIRST_ARG_NUM is the number of the first actual argument to check
1253 against the format string, or zero if no checking is not be done
1254 (e.g. for varargs such as vfprintf). */
1257 record_function_format (name, assembler_name, format_type,
1258 format_num, first_arg_num)
1260 tree assembler_name;
1261 enum format_type format_type;
1265 function_format_info *info;
1267 /* Re-use existing structure if it's there. */
1269 for (info = function_format_list; info; info = info->next)
1271 if (info->name == name && info->assembler_name == assembler_name)
1276 info = (function_format_info *) xmalloc (sizeof (function_format_info));
1277 info->next = function_format_list;
1278 function_format_list = info;
1281 info->assembler_name = assembler_name;
1284 info->format_type = format_type;
1285 info->format_num = format_num;
1286 info->first_arg_num = first_arg_num;
1289 /* Record information for the names of function that modify the format
1290 argument to format functions. FUNCTION_IDENT is the identifier node for
1291 the name of the function (its decl need not exist yet) and FORMAT_NUM is
1292 the number of the argument which is the format control string (starting
1296 record_international_format (name, assembler_name, format_num)
1298 tree assembler_name;
1301 international_format_info *info;
1303 /* Re-use existing structure if it's there. */
1305 for (info = international_format_list; info; info = info->next)
1307 if (info->name == name && info->assembler_name == assembler_name)
1314 = (international_format_info *)
1315 xmalloc (sizeof (international_format_info));
1316 info->next = international_format_list;
1317 international_format_list = info;
1320 info->assembler_name = assembler_name;
1323 info->format_num = format_num;
1326 static char tfaff[] = "too few arguments for format";
1328 /* Check the argument list of a call to printf, scanf, etc.
1329 NAME is the function identifier.
1330 ASSEMBLER_NAME is the function's assembler identifier.
1331 (Either NAME or ASSEMBLER_NAME, but not both, may be NULL_TREE.)
1332 PARAMS is the list of argument values. */
1335 check_function_format (name, assembler_name, params)
1337 tree assembler_name;
1340 function_format_info *info;
1342 /* See if this function is a format function. */
1343 for (info = function_format_list; info; info = info->next)
1345 if (info->assembler_name
1346 ? (info->assembler_name == assembler_name)
1347 : (info->name == name))
1349 /* Yup; check it. */
1350 check_format_info (info, params);
1356 /* Check the argument list of a call to printf, scanf, etc.
1357 INFO points to the function_format_info structure.
1358 PARAMS is the list of argument values. */
1361 check_format_info (info, params)
1362 function_format_info *info;
1367 int suppressed, wide, precise;
1368 int length_char = 0;
1375 tree first_fillin_param;
1377 format_char_info *fci = NULL;
1379 int has_operand_number = 0;
1381 /* Skip to format argument. If the argument isn't available, there's
1382 no work for us to do; prototype checking will catch the problem. */
1383 for (arg_num = 1; ; ++arg_num)
1387 if (arg_num == info->format_num)
1389 params = TREE_CHAIN (params);
1391 format_tree = TREE_VALUE (params);
1392 params = TREE_CHAIN (params);
1393 if (format_tree == 0)
1396 /* We can only check the format if it's a string constant. */
1397 while (TREE_CODE (format_tree) == NOP_EXPR)
1398 format_tree = TREE_OPERAND (format_tree, 0); /* strip coercion */
1400 if (TREE_CODE (format_tree) == CALL_EXPR
1401 && TREE_CODE (TREE_OPERAND (format_tree, 0)) == ADDR_EXPR
1402 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (format_tree, 0), 0))
1405 tree function = TREE_OPERAND (TREE_OPERAND (format_tree, 0), 0);
1407 /* See if this is a call to a known internationalization function
1408 that modifies the format arg. */
1409 international_format_info *info;
1411 for (info = international_format_list; info; info = info->next)
1412 if (info->assembler_name
1413 ? (info->assembler_name == DECL_ASSEMBLER_NAME (function))
1414 : (info->name == DECL_NAME (function)))
1419 for (inner_args = TREE_OPERAND (format_tree, 1), i = 1;
1421 inner_args = TREE_CHAIN (inner_args), i++)
1422 if (i == info->format_num)
1424 format_tree = TREE_VALUE (inner_args);
1426 while (TREE_CODE (format_tree) == NOP_EXPR)
1427 format_tree = TREE_OPERAND (format_tree, 0);
1432 if (integer_zerop (format_tree))
1434 warning ("null format string");
1437 if (TREE_CODE (format_tree) != ADDR_EXPR)
1439 format_tree = TREE_OPERAND (format_tree, 0);
1440 if (TREE_CODE (format_tree) != STRING_CST)
1442 format_chars = TREE_STRING_POINTER (format_tree);
1443 format_length = TREE_STRING_LENGTH (format_tree);
1444 if (format_length <= 1)
1445 warning ("zero-length format string");
1446 if (format_chars[--format_length] != 0)
1448 warning ("unterminated format string");
1451 /* Skip to first argument to check. */
1452 while (arg_num + 1 < info->first_arg_num)
1456 params = TREE_CHAIN (params);
1460 first_fillin_param = params;
1464 if (*format_chars == 0)
1466 if (format_chars - TREE_STRING_POINTER (format_tree) != format_length)
1467 warning ("embedded `\\0' in format");
1468 if (info->first_arg_num != 0 && params != 0 && ! has_operand_number)
1469 warning ("too many arguments for format");
1472 if (*format_chars++ != '%')
1474 if (*format_chars == 0)
1476 warning ("spurious trailing `%%' in format");
1479 if (*format_chars == '%')
1485 suppressed = wide = precise = FALSE;
1486 if (info->format_type == scanf_format_type)
1488 suppressed = *format_chars == '*';
1491 while (ISDIGIT (*format_chars))
1494 else if (info->format_type == strftime_format_type)
1496 while (*format_chars != 0 && index ("_-0^#", *format_chars) != 0)
1499 warning ("ANSI C does not support the strftime `%c' flag",
1501 if (index (flag_chars, *format_chars) != 0)
1503 warning ("repeated `%c' flag in format",
1509 i = strlen (flag_chars);
1510 flag_chars[i++] = *format_chars++;
1514 while (ISDIGIT ((unsigned char) *format_chars))
1519 if (wide && pedantic)
1520 warning ("ANSI C does not support strftime format width");
1521 if (*format_chars == 'E' || *format_chars == 'O')
1523 i = strlen (flag_chars);
1524 flag_chars[i++] = *format_chars++;
1526 if (*format_chars == 'E' || *format_chars == 'O')
1528 warning ("multiple E/O modifiers in format");
1529 while (*format_chars == 'E' || *format_chars == 'O')
1534 else if (info->format_type == printf_format_type)
1536 /* See if we have a number followed by a dollar sign. If we do,
1537 it is an operand number, so set PARAMS to that operand. */
1538 if (*format_chars >= '0' && *format_chars <= '9')
1540 char *p = format_chars;
1542 while (*p >= '0' && *p++ <= '9')
1547 int opnum = atoi (format_chars);
1549 params = first_fillin_param;
1550 format_chars = p + 1;
1551 has_operand_number = 1;
1553 for (i = 1; i < opnum && params != 0; i++)
1554 params = TREE_CHAIN (params);
1556 if (opnum == 0 || params == 0)
1558 warning ("operand number out of range in format");
1564 while (*format_chars != 0 && index (" +#0-", *format_chars) != 0)
1566 if (index (flag_chars, *format_chars) != 0)
1567 warning ("repeated `%c' flag in format", *format_chars++);
1570 i = strlen (flag_chars);
1571 flag_chars[i++] = *format_chars++;
1575 /* "If the space and + flags both appear,
1576 the space flag will be ignored." */
1577 if (index (flag_chars, ' ') != 0
1578 && index (flag_chars, '+') != 0)
1579 warning ("use of both ` ' and `+' flags in format");
1580 /* "If the 0 and - flags both appear,
1581 the 0 flag will be ignored." */
1582 if (index (flag_chars, '0') != 0
1583 && index (flag_chars, '-') != 0)
1584 warning ("use of both `0' and `-' flags in format");
1585 if (*format_chars == '*')
1588 /* "...a field width...may be indicated by an asterisk.
1589 In this case, an int argument supplies the field width..." */
1596 if (info->first_arg_num != 0)
1598 cur_param = TREE_VALUE (params);
1599 params = TREE_CHAIN (params);
1601 /* size_t is generally not valid here.
1602 It will work on most machines, because size_t and int
1603 have the same mode. But might as well warn anyway,
1604 since it will fail on other machines. */
1605 if ((TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1606 != integer_type_node)
1608 (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1609 != unsigned_type_node))
1610 warning ("field width is not type int (arg %d)", arg_num);
1615 while (ISDIGIT (*format_chars))
1621 if (*format_chars == '.')
1625 if (*format_chars != '*' && !ISDIGIT (*format_chars))
1626 warning ("`.' not followed by `*' or digit in format");
1627 /* "...a...precision...may be indicated by an asterisk.
1628 In this case, an int argument supplies the...precision." */
1629 if (*format_chars == '*')
1631 if (info->first_arg_num != 0)
1639 cur_param = TREE_VALUE (params);
1640 params = TREE_CHAIN (params);
1642 if (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1643 != integer_type_node)
1644 warning ("field width is not type int (arg %d)",
1650 while (ISDIGIT (*format_chars))
1658 if (info->format_type != strftime_format_type)
1660 if (*format_chars == 'h' || *format_chars == 'l')
1661 length_char = *format_chars++;
1662 else if (*format_chars == 'q' || *format_chars == 'L')
1664 length_char = *format_chars++;
1666 warning ("ANSI C does not support the `%c' length modifier",
1669 else if (*format_chars == 'Z')
1671 length_char = *format_chars++;
1673 warning ("ANSI C does not support the `Z' length modifier");
1677 if (length_char == 'l' && *format_chars == 'l')
1679 length_char = 'q', format_chars++;
1680 /* FIXME: Is allowed in ISO C 9x. */
1682 warning ("ANSI C does not support the `ll' length modifier");
1684 else if (length_char == 'h' && *format_chars == 'h')
1686 length_char = 'H', format_chars++;
1687 /* FIXME: Is allowed in ISO C 9x. */
1689 warning ("ANSI C does not support the `hh' length modifier");
1691 if (*format_chars == 'a' && info->format_type == scanf_format_type)
1693 if (format_chars[1] == 's' || format_chars[1] == 'S'
1694 || format_chars[1] == '[')
1696 /* `a' is used as a flag. */
1701 if (suppressed && length_char != 0)
1702 warning ("use of `*' and `%c' together in format", length_char);
1704 format_char = *format_chars;
1705 if (format_char == 0
1706 || (info->format_type != strftime_format_type && format_char == '%'))
1708 warning ("conversion lacks type at end of format");
1711 /* The m, C, and S formats are GNU extensions. */
1712 if (pedantic && info->format_type != strftime_format_type
1713 && (format_char == 'm' || format_char == 'C' || format_char == 'S'))
1714 warning ("ANSI C does not support the `%c' format", format_char);
1715 /* ??? The a and A formats are C9X extensions, and should be allowed
1716 when a C9X option is added. */
1717 if (pedantic && info->format_type != strftime_format_type
1718 && (format_char == 'a' || format_char == 'A'))
1719 warning ("ANSI C does not support the `%c' format", format_char);
1721 switch (info->format_type)
1723 case printf_format_type:
1724 fci = print_char_table;
1726 case scanf_format_type:
1727 fci = scan_char_table;
1729 case strftime_format_type:
1730 fci = time_char_table;
1735 while (fci->format_chars != 0
1736 && index (fci->format_chars, format_char) == 0)
1738 if (fci->format_chars == 0)
1740 if (format_char >= 040 && format_char < 0177)
1741 warning ("unknown conversion type character `%c' in format",
1744 warning ("unknown conversion type character 0x%x in format",
1750 if (index (fci->flag_chars, 'G') != 0)
1751 warning ("ANSI C does not support `%%%c'", format_char);
1752 if (index (fci->flag_chars, 'o') != 0
1753 && index (flag_chars, 'O') != 0)
1754 warning ("ANSI C does not support `%%O%c'", format_char);
1756 if (wide && index (fci->flag_chars, 'w') == 0)
1757 warning ("width used with `%c' format", format_char);
1758 if (index (fci->flag_chars, '2') != 0)
1759 warning ("`%%%c' yields only last 2 digits of year", format_char);
1760 else if (index (fci->flag_chars, '3') != 0)
1761 warning ("`%%%c' yields only last 2 digits of year in some locales",
1763 if (precise && index (fci->flag_chars, 'p') == 0)
1764 warning ("precision used with `%c' format", format_char);
1765 if (aflag && index (fci->flag_chars, 'a') == 0)
1767 warning ("`a' flag used with `%c' format", format_char);
1768 /* To simplify the following code. */
1771 /* The a flag is a GNU extension. */
1772 else if (pedantic && aflag)
1773 warning ("ANSI C does not support the `a' flag");
1774 if (info->format_type == scanf_format_type && format_char == '[')
1776 /* Skip over scan set, in case it happens to have '%' in it. */
1777 if (*format_chars == '^')
1779 /* Find closing bracket; if one is hit immediately, then
1780 it's part of the scan set rather than a terminator. */
1781 if (*format_chars == ']')
1783 while (*format_chars && *format_chars != ']')
1785 if (*format_chars != ']')
1786 /* The end of the format string was reached. */
1787 warning ("no closing `]' for `%%[' format");
1791 if (index (fci->flag_chars, '*') == 0)
1792 warning ("suppression of `%c' conversion in format", format_char);
1795 for (i = 0; flag_chars[i] != 0; ++i)
1797 if (index (fci->flag_chars, flag_chars[i]) == 0)
1798 warning ("flag `%c' used with type `%c'",
1799 flag_chars[i], format_char);
1801 if (info->format_type == strftime_format_type)
1803 if (precise && index (flag_chars, '0') != 0
1804 && (format_char == 'd' || format_char == 'i'
1805 || format_char == 'o' || format_char == 'u'
1806 || format_char == 'x' || format_char == 'X'))
1807 warning ("`0' flag ignored with precision specifier and `%c' format",
1809 switch (length_char)
1811 default: wanted_type = fci->nolen ? *(fci->nolen) : 0; break;
1812 case 'H': wanted_type = fci->hhlen ? *(fci->hhlen) : 0; break;
1813 case 'h': wanted_type = fci->hlen ? *(fci->hlen) : 0; break;
1814 case 'l': wanted_type = fci->llen ? *(fci->llen) : 0; break;
1815 case 'q': wanted_type = fci->qlen ? *(fci->qlen) : 0; break;
1816 case 'L': wanted_type = fci->bigllen ? *(fci->bigllen) : 0; break;
1817 case 'Z': wanted_type = fci->zlen ? *fci->zlen : 0; break;
1819 if (wanted_type == 0)
1820 warning ("use of `%c' length character with `%c' type character",
1821 length_char, format_char);
1823 /* Finally. . .check type of argument against desired type! */
1824 if (info->first_arg_num == 0)
1826 if (fci->pointer_count == 0 && wanted_type == void_type_node)
1827 /* This specifier takes no argument. */
1834 cur_param = TREE_VALUE (params);
1835 params = TREE_CHAIN (params);
1837 cur_type = TREE_TYPE (cur_param);
1839 STRIP_NOPS (cur_param);
1841 /* Check the types of any additional pointer arguments
1842 that precede the "real" argument. */
1843 for (i = 0; i < fci->pointer_count + aflag; ++i)
1845 if (TREE_CODE (cur_type) == POINTER_TYPE)
1847 cur_type = TREE_TYPE (cur_type);
1849 if (cur_param != 0 && TREE_CODE (cur_param) == ADDR_EXPR)
1850 cur_param = TREE_OPERAND (cur_param, 0);
1856 if (TREE_CODE (cur_type) != ERROR_MARK)
1857 warning ("format argument is not a %s (arg %d)",
1858 ((fci->pointer_count + aflag == 1)
1859 ? "pointer" : "pointer to a pointer"),
1864 /* See if this is an attempt to write into a const type with
1865 scanf or with printf "%n". */
1866 if ((info->format_type == scanf_format_type
1867 || (info->format_type == printf_format_type
1868 && format_char == 'n'))
1869 && i == fci->pointer_count + aflag
1871 && TREE_CODE (cur_type) != ERROR_MARK
1872 && (TYPE_READONLY (cur_type)
1874 && (TREE_CODE_CLASS (TREE_CODE (cur_param)) == 'c'
1875 || (TREE_CODE_CLASS (TREE_CODE (cur_param)) == 'd'
1876 && TREE_READONLY (cur_param))))))
1877 warning ("writing into constant object (arg %d)", arg_num);
1879 /* Check the type of the "real" argument, if there's a type we want. */
1880 if (i == fci->pointer_count + aflag && wanted_type != 0
1881 && TREE_CODE (cur_type) != ERROR_MARK
1882 && wanted_type != TYPE_MAIN_VARIANT (cur_type)
1883 /* If we want `void *', allow any pointer type.
1884 (Anything else would already have got a warning.) */
1885 && ! (wanted_type == void_type_node
1886 && fci->pointer_count > 0)
1887 /* Don't warn about differences merely in signedness. */
1888 && !(TREE_CODE (wanted_type) == INTEGER_TYPE
1889 && TREE_CODE (TYPE_MAIN_VARIANT (cur_type)) == INTEGER_TYPE
1890 && (TREE_UNSIGNED (wanted_type)
1891 ? wanted_type == (cur_type = unsigned_type (cur_type))
1892 : wanted_type == (cur_type = signed_type (cur_type))))
1893 /* Likewise, "signed char", "unsigned char" and "char" are
1894 equivalent but the above test won't consider them equivalent. */
1895 && ! (wanted_type == char_type_node
1896 && (TYPE_MAIN_VARIANT (cur_type) == signed_char_type_node
1897 || TYPE_MAIN_VARIANT (cur_type) == unsigned_char_type_node)))
1899 register char *this;
1900 register char *that;
1902 this = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (wanted_type)));
1904 if (TREE_CODE (cur_type) != ERROR_MARK
1905 && TYPE_NAME (cur_type) != 0
1906 && TREE_CODE (cur_type) != INTEGER_TYPE
1907 && !(TREE_CODE (cur_type) == POINTER_TYPE
1908 && TREE_CODE (TREE_TYPE (cur_type)) == INTEGER_TYPE))
1910 if (TREE_CODE (TYPE_NAME (cur_type)) == TYPE_DECL
1911 && DECL_NAME (TYPE_NAME (cur_type)) != 0)
1912 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type)));
1914 that = IDENTIFIER_POINTER (TYPE_NAME (cur_type));
1917 /* A nameless type can't possibly match what the format wants.
1918 So there will be a warning for it.
1919 Make up a string to describe vaguely what it is. */
1922 if (TREE_CODE (cur_type) == POINTER_TYPE)
1925 that = "different type";
1928 /* Make the warning better in case of mismatch of int vs long. */
1929 if (TREE_CODE (cur_type) == INTEGER_TYPE
1930 && TREE_CODE (wanted_type) == INTEGER_TYPE
1931 && TYPE_PRECISION (cur_type) == TYPE_PRECISION (wanted_type)
1932 && TYPE_NAME (cur_type) != 0
1933 && TREE_CODE (TYPE_NAME (cur_type)) == TYPE_DECL)
1934 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type)));
1936 if (strcmp (this, that) != 0)
1937 warning ("%s format, %s arg (arg %d)", this, that, arg_num);
1942 /* Print a warning if a constant expression had overflow in folding.
1943 Invoke this function on every expression that the language
1944 requires to be a constant expression.
1945 Note the ANSI C standard says it is erroneous for a
1946 constant expression to overflow. */
1949 constant_expression_warning (value)
1952 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1953 || TREE_CODE (value) == COMPLEX_CST)
1954 && TREE_CONSTANT_OVERFLOW (value) && pedantic)
1955 pedwarn ("overflow in constant expression");
1958 /* Print a warning if an expression had overflow in folding.
1959 Invoke this function on every expression that
1960 (1) appears in the source code, and
1961 (2) might be a constant expression that overflowed, and
1962 (3) is not already checked by convert_and_check;
1963 however, do not invoke this function on operands of explicit casts. */
1966 overflow_warning (value)
1969 if ((TREE_CODE (value) == INTEGER_CST
1970 || (TREE_CODE (value) == COMPLEX_CST
1971 && TREE_CODE (TREE_REALPART (value)) == INTEGER_CST))
1972 && TREE_OVERFLOW (value))
1974 TREE_OVERFLOW (value) = 0;
1975 if (skip_evaluation == 0)
1976 warning ("integer overflow in expression");
1978 else if ((TREE_CODE (value) == REAL_CST
1979 || (TREE_CODE (value) == COMPLEX_CST
1980 && TREE_CODE (TREE_REALPART (value)) == REAL_CST))
1981 && TREE_OVERFLOW (value))
1983 TREE_OVERFLOW (value) = 0;
1984 if (skip_evaluation == 0)
1985 warning ("floating point overflow in expression");
1989 /* Print a warning if a large constant is truncated to unsigned,
1990 or if -Wconversion is used and a constant < 0 is converted to unsigned.
1991 Invoke this function on every expression that might be implicitly
1992 converted to an unsigned type. */
1995 unsigned_conversion_warning (result, operand)
1996 tree result, operand;
1998 if (TREE_CODE (operand) == INTEGER_CST
1999 && TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
2000 && TREE_UNSIGNED (TREE_TYPE (result))
2001 && skip_evaluation == 0
2002 && !int_fits_type_p (operand, TREE_TYPE (result)))
2004 if (!int_fits_type_p (operand, signed_type (TREE_TYPE (result))))
2005 /* This detects cases like converting -129 or 256 to unsigned char. */
2006 warning ("large integer implicitly truncated to unsigned type");
2007 else if (warn_conversion)
2008 warning ("negative integer implicitly converted to unsigned type");
2012 /* Convert EXPR to TYPE, warning about conversion problems with constants.
2013 Invoke this function on every expression that is converted implicitly,
2014 i.e. because of language rules and not because of an explicit cast. */
2017 convert_and_check (type, expr)
2020 tree t = convert (type, expr);
2021 if (TREE_CODE (t) == INTEGER_CST)
2023 if (TREE_OVERFLOW (t))
2025 TREE_OVERFLOW (t) = 0;
2027 /* Do not diagnose overflow in a constant expression merely
2028 because a conversion overflowed. */
2029 TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (expr);
2031 /* No warning for converting 0x80000000 to int. */
2032 if (!(TREE_UNSIGNED (type) < TREE_UNSIGNED (TREE_TYPE (expr))
2033 && TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
2034 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (expr))))
2035 /* If EXPR fits in the unsigned version of TYPE,
2036 don't warn unless pedantic. */
2038 || TREE_UNSIGNED (type)
2039 || ! int_fits_type_p (expr, unsigned_type (type)))
2040 && skip_evaluation == 0)
2041 warning ("overflow in implicit constant conversion");
2044 unsigned_conversion_warning (t, expr);
2050 c_expand_expr_stmt (expr)
2053 /* Do default conversion if safe and possibly important,
2054 in case within ({...}). */
2055 if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE && lvalue_p (expr))
2056 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
2057 expr = default_conversion (expr);
2059 if (TREE_TYPE (expr) != error_mark_node
2060 && TYPE_SIZE (TREE_TYPE (expr)) == 0
2061 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
2062 error ("expression statement has incomplete type");
2064 expand_expr_stmt (expr);
2067 /* Validate the expression after `case' and apply default promotions. */
2070 check_case_value (value)
2073 if (value == NULL_TREE)
2076 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
2077 STRIP_TYPE_NOPS (value);
2079 if (TREE_CODE (value) != INTEGER_CST
2080 && value != error_mark_node)
2082 error ("case label does not reduce to an integer constant");
2083 value = error_mark_node;
2086 /* Promote char or short to int. */
2087 value = default_conversion (value);
2089 constant_expression_warning (value);
2094 /* Return an integer type with BITS bits of precision,
2095 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
2098 type_for_size (bits, unsignedp)
2102 if (bits == TYPE_PRECISION (integer_type_node))
2103 return unsignedp ? unsigned_type_node : integer_type_node;
2105 if (bits == TYPE_PRECISION (signed_char_type_node))
2106 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2108 if (bits == TYPE_PRECISION (short_integer_type_node))
2109 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2111 if (bits == TYPE_PRECISION (long_integer_type_node))
2112 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2114 if (bits == TYPE_PRECISION (long_long_integer_type_node))
2115 return (unsignedp ? long_long_unsigned_type_node
2116 : long_long_integer_type_node);
2118 if (bits <= TYPE_PRECISION (intQI_type_node))
2119 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2121 if (bits <= TYPE_PRECISION (intHI_type_node))
2122 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2124 if (bits <= TYPE_PRECISION (intSI_type_node))
2125 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2127 if (bits <= TYPE_PRECISION (intDI_type_node))
2128 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2133 /* Return a data type that has machine mode MODE.
2134 If the mode is an integer,
2135 then UNSIGNEDP selects between signed and unsigned types. */
2138 type_for_mode (mode, unsignedp)
2139 enum machine_mode mode;
2142 if (mode == TYPE_MODE (integer_type_node))
2143 return unsignedp ? unsigned_type_node : integer_type_node;
2145 if (mode == TYPE_MODE (signed_char_type_node))
2146 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2148 if (mode == TYPE_MODE (short_integer_type_node))
2149 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2151 if (mode == TYPE_MODE (long_integer_type_node))
2152 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2154 if (mode == TYPE_MODE (long_long_integer_type_node))
2155 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
2157 if (mode == TYPE_MODE (intQI_type_node))
2158 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2160 if (mode == TYPE_MODE (intHI_type_node))
2161 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2163 if (mode == TYPE_MODE (intSI_type_node))
2164 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2166 if (mode == TYPE_MODE (intDI_type_node))
2167 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2169 #if HOST_BITS_PER_WIDE_INT >= 64
2170 if (mode == TYPE_MODE (intTI_type_node))
2171 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2174 if (mode == TYPE_MODE (float_type_node))
2175 return float_type_node;
2177 if (mode == TYPE_MODE (double_type_node))
2178 return double_type_node;
2180 if (mode == TYPE_MODE (long_double_type_node))
2181 return long_double_type_node;
2183 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
2184 return build_pointer_type (char_type_node);
2186 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
2187 return build_pointer_type (integer_type_node);
2192 /* Return the minimum number of bits needed to represent VALUE in a
2193 signed or unsigned type, UNSIGNEDP says which. */
2196 min_precision (value, unsignedp)
2202 /* If the value is negative, compute its negative minus 1. The latter
2203 adjustment is because the absolute value of the largest negative value
2204 is one larger than the largest positive value. This is equivalent to
2205 a bit-wise negation, so use that operation instead. */
2207 if (tree_int_cst_sgn (value) < 0)
2208 value = fold (build1 (BIT_NOT_EXPR, TREE_TYPE (value), value));
2210 /* Return the number of bits needed, taking into account the fact
2211 that we need one more bit for a signed than unsigned type. */
2213 if (integer_zerop (value))
2215 else if (TREE_INT_CST_HIGH (value) != 0)
2216 log = HOST_BITS_PER_WIDE_INT + floor_log2 (TREE_INT_CST_HIGH (value));
2218 log = floor_log2 (TREE_INT_CST_LOW (value));
2220 return log + 1 + ! unsignedp;
2223 /* Print an error message for invalid operands to arith operation CODE.
2224 NOP_EXPR is used as a special case (see truthvalue_conversion). */
2227 binary_op_error (code)
2228 enum tree_code code;
2230 register char *opname;
2235 error ("invalid truth-value expression");
2239 opname = "+"; break;
2241 opname = "-"; break;
2243 opname = "*"; break;
2245 opname = "max"; break;
2247 opname = "min"; break;
2249 opname = "=="; break;
2251 opname = "!="; break;
2253 opname = "<="; break;
2255 opname = ">="; break;
2257 opname = "<"; break;
2259 opname = ">"; break;
2261 opname = "<<"; break;
2263 opname = ">>"; break;
2264 case TRUNC_MOD_EXPR:
2265 case FLOOR_MOD_EXPR:
2266 opname = "%"; break;
2267 case TRUNC_DIV_EXPR:
2268 case FLOOR_DIV_EXPR:
2269 opname = "/"; break;
2271 opname = "&"; break;
2273 opname = "|"; break;
2274 case TRUTH_ANDIF_EXPR:
2275 opname = "&&"; break;
2276 case TRUTH_ORIF_EXPR:
2277 opname = "||"; break;
2279 opname = "^"; break;
2282 opname = "rotate"; break;
2284 opname = "unknown"; break;
2286 error ("invalid operands to binary %s", opname);
2289 /* Subroutine of build_binary_op, used for comparison operations.
2290 See if the operands have both been converted from subword integer types
2291 and, if so, perhaps change them both back to their original type.
2292 This function is also responsible for converting the two operands
2293 to the proper common type for comparison.
2295 The arguments of this function are all pointers to local variables
2296 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
2297 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
2299 If this function returns nonzero, it means that the comparison has
2300 a constant value. What this function returns is an expression for
2304 shorten_compare (op0_ptr, op1_ptr, restype_ptr, rescode_ptr)
2305 tree *op0_ptr, *op1_ptr;
2307 enum tree_code *rescode_ptr;
2310 tree op0 = *op0_ptr;
2311 tree op1 = *op1_ptr;
2312 int unsignedp0, unsignedp1;
2314 tree primop0, primop1;
2315 enum tree_code code = *rescode_ptr;
2317 /* Throw away any conversions to wider types
2318 already present in the operands. */
2320 primop0 = get_narrower (op0, &unsignedp0);
2321 primop1 = get_narrower (op1, &unsignedp1);
2323 /* Handle the case that OP0 does not *contain* a conversion
2324 but it *requires* conversion to FINAL_TYPE. */
2326 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
2327 unsignedp0 = TREE_UNSIGNED (TREE_TYPE (op0));
2328 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
2329 unsignedp1 = TREE_UNSIGNED (TREE_TYPE (op1));
2331 /* If one of the operands must be floated, we cannot optimize. */
2332 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
2333 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
2335 /* If first arg is constant, swap the args (changing operation
2336 so value is preserved), for canonicalization. Don't do this if
2337 the second arg is 0. */
2339 if (TREE_CONSTANT (primop0)
2340 && ! integer_zerop (primop1) && ! real_zerop (primop1))
2342 register tree tem = primop0;
2343 register int temi = unsignedp0;
2351 unsignedp0 = unsignedp1;
2374 *rescode_ptr = code;
2377 /* If comparing an integer against a constant more bits wide,
2378 maybe we can deduce a value of 1 or 0 independent of the data.
2379 Or else truncate the constant now
2380 rather than extend the variable at run time.
2382 This is only interesting if the constant is the wider arg.
2383 Also, it is not safe if the constant is unsigned and the
2384 variable arg is signed, since in this case the variable
2385 would be sign-extended and then regarded as unsigned.
2386 Our technique fails in this case because the lowest/highest
2387 possible unsigned results don't follow naturally from the
2388 lowest/highest possible values of the variable operand.
2389 For just EQ_EXPR and NE_EXPR there is another technique that
2390 could be used: see if the constant can be faithfully represented
2391 in the other operand's type, by truncating it and reextending it
2392 and see if that preserves the constant's value. */
2394 if (!real1 && !real2
2395 && TREE_CODE (primop1) == INTEGER_CST
2396 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
2398 int min_gt, max_gt, min_lt, max_lt;
2399 tree maxval, minval;
2400 /* 1 if comparison is nominally unsigned. */
2401 int unsignedp = TREE_UNSIGNED (*restype_ptr);
2404 type = signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0));
2406 maxval = TYPE_MAX_VALUE (type);
2407 minval = TYPE_MIN_VALUE (type);
2409 if (unsignedp && !unsignedp0)
2410 *restype_ptr = signed_type (*restype_ptr);
2412 if (TREE_TYPE (primop1) != *restype_ptr)
2413 primop1 = convert (*restype_ptr, primop1);
2414 if (type != *restype_ptr)
2416 minval = convert (*restype_ptr, minval);
2417 maxval = convert (*restype_ptr, maxval);
2420 if (unsignedp && unsignedp0)
2422 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
2423 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
2424 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
2425 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
2429 min_gt = INT_CST_LT (primop1, minval);
2430 max_gt = INT_CST_LT (primop1, maxval);
2431 min_lt = INT_CST_LT (minval, primop1);
2432 max_lt = INT_CST_LT (maxval, primop1);
2436 /* This used to be a switch, but Genix compiler can't handle that. */
2437 if (code == NE_EXPR)
2439 if (max_lt || min_gt)
2440 val = boolean_true_node;
2442 else if (code == EQ_EXPR)
2444 if (max_lt || min_gt)
2445 val = boolean_false_node;
2447 else if (code == LT_EXPR)
2450 val = boolean_true_node;
2452 val = boolean_false_node;
2454 else if (code == GT_EXPR)
2457 val = boolean_true_node;
2459 val = boolean_false_node;
2461 else if (code == LE_EXPR)
2464 val = boolean_true_node;
2466 val = boolean_false_node;
2468 else if (code == GE_EXPR)
2471 val = boolean_true_node;
2473 val = boolean_false_node;
2476 /* If primop0 was sign-extended and unsigned comparison specd,
2477 we did a signed comparison above using the signed type bounds.
2478 But the comparison we output must be unsigned.
2480 Also, for inequalities, VAL is no good; but if the signed
2481 comparison had *any* fixed result, it follows that the
2482 unsigned comparison just tests the sign in reverse
2483 (positive values are LE, negative ones GE).
2484 So we can generate an unsigned comparison
2485 against an extreme value of the signed type. */
2487 if (unsignedp && !unsignedp0)
2494 primop1 = TYPE_MIN_VALUE (type);
2500 primop1 = TYPE_MAX_VALUE (type);
2507 type = unsigned_type (type);
2510 if (!max_gt && !unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
2512 /* This is the case of (char)x >?< 0x80, which people used to use
2513 expecting old C compilers to change the 0x80 into -0x80. */
2514 if (val == boolean_false_node)
2515 warning ("comparison is always 0 due to limited range of data type");
2516 if (val == boolean_true_node)
2517 warning ("comparison is always 1 due to limited range of data type");
2520 if (!min_lt && unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
2522 /* This is the case of (unsigned char)x >?< -1 or < 0. */
2523 if (val == boolean_false_node)
2524 warning ("comparison is always 0 due to limited range of data type");
2525 if (val == boolean_true_node)
2526 warning ("comparison is always 1 due to limited range of data type");
2531 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2532 if (TREE_SIDE_EFFECTS (primop0))
2533 return build (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
2537 /* Value is not predetermined, but do the comparison
2538 in the type of the operand that is not constant.
2539 TYPE is already properly set. */
2541 else if (real1 && real2
2542 && (TYPE_PRECISION (TREE_TYPE (primop0))
2543 == TYPE_PRECISION (TREE_TYPE (primop1))))
2544 type = TREE_TYPE (primop0);
2546 /* If args' natural types are both narrower than nominal type
2547 and both extend in the same manner, compare them
2548 in the type of the wider arg.
2549 Otherwise must actually extend both to the nominal
2550 common type lest different ways of extending
2552 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
2554 else if (unsignedp0 == unsignedp1 && real1 == real2
2555 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
2556 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
2558 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
2559 type = signed_or_unsigned_type (unsignedp0
2560 || TREE_UNSIGNED (*restype_ptr),
2562 /* Make sure shorter operand is extended the right way
2563 to match the longer operand. */
2564 primop0 = convert (signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0)),
2566 primop1 = convert (signed_or_unsigned_type (unsignedp1, TREE_TYPE (primop1)),
2571 /* Here we must do the comparison on the nominal type
2572 using the args exactly as we received them. */
2573 type = *restype_ptr;
2577 if (!real1 && !real2 && integer_zerop (primop1)
2578 && TREE_UNSIGNED (*restype_ptr))
2584 /* All unsigned values are >= 0, so we warn if extra warnings
2585 are requested. However, if OP0 is a constant that is
2586 >= 0, the signedness of the comparison isn't an issue,
2587 so suppress the warning. */
2589 && ! (TREE_CODE (primop0) == INTEGER_CST
2590 && ! TREE_OVERFLOW (convert (signed_type (type),
2592 warning ("unsigned value >= 0 is always 1");
2593 value = boolean_true_node;
2598 && ! (TREE_CODE (primop0) == INTEGER_CST
2599 && ! TREE_OVERFLOW (convert (signed_type (type),
2601 warning ("unsigned value < 0 is always 0");
2602 value = boolean_false_node;
2611 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2612 if (TREE_SIDE_EFFECTS (primop0))
2613 return build (COMPOUND_EXPR, TREE_TYPE (value),
2620 *op0_ptr = convert (type, primop0);
2621 *op1_ptr = convert (type, primop1);
2623 *restype_ptr = boolean_type_node;
2628 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
2629 or validate its data type for an `if' or `while' statement or ?..: exp.
2631 This preparation consists of taking the ordinary
2632 representation of an expression expr and producing a valid tree
2633 boolean expression describing whether expr is nonzero. We could
2634 simply always do build_binary_op (NE_EXPR, expr, boolean_false_node, 1),
2635 but we optimize comparisons, &&, ||, and !.
2637 The resulting type should always be `boolean_type_node'. */
2640 truthvalue_conversion (expr)
2643 if (TREE_CODE (expr) == ERROR_MARK)
2646 #if 0 /* This appears to be wrong for C++. */
2647 /* These really should return error_mark_node after 2.4 is stable.
2648 But not all callers handle ERROR_MARK properly. */
2649 switch (TREE_CODE (TREE_TYPE (expr)))
2652 error ("struct type value used where scalar is required");
2653 return boolean_false_node;
2656 error ("union type value used where scalar is required");
2657 return boolean_false_node;
2660 error ("array type value used where scalar is required");
2661 return boolean_false_node;
2668 switch (TREE_CODE (expr))
2670 /* It is simpler and generates better code to have only TRUTH_*_EXPR
2671 or comparison expressions as truth values at this level. */
2674 /* A one-bit unsigned bit-field is already acceptable. */
2675 if (1 == TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (expr, 1)))
2676 && TREE_UNSIGNED (TREE_OPERAND (expr, 1)))
2682 /* It is simpler and generates better code to have only TRUTH_*_EXPR
2683 or comparison expressions as truth values at this level. */
2685 if (integer_zerop (TREE_OPERAND (expr, 1)))
2686 return build_unary_op (TRUTH_NOT_EXPR, TREE_OPERAND (expr, 0), 0);
2688 case NE_EXPR: case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
2689 case TRUTH_ANDIF_EXPR:
2690 case TRUTH_ORIF_EXPR:
2691 case TRUTH_AND_EXPR:
2693 case TRUTH_XOR_EXPR:
2694 case TRUTH_NOT_EXPR:
2695 TREE_TYPE (expr) = boolean_type_node;
2702 return integer_zerop (expr) ? boolean_false_node : boolean_true_node;
2705 return real_zerop (expr) ? boolean_false_node : boolean_true_node;
2708 /* If we are taking the address of a external decl, it might be zero
2709 if it is weak, so we cannot optimize. */
2710 if (TREE_CODE_CLASS (TREE_CODE (TREE_OPERAND (expr, 0))) == 'd'
2711 && DECL_EXTERNAL (TREE_OPERAND (expr, 0)))
2714 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 0)))
2715 return build (COMPOUND_EXPR, boolean_type_node,
2716 TREE_OPERAND (expr, 0), boolean_true_node);
2718 return boolean_true_node;
2721 return build_binary_op ((TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
2722 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
2723 truthvalue_conversion (TREE_OPERAND (expr, 0)),
2724 truthvalue_conversion (TREE_OPERAND (expr, 1)),
2731 /* These don't change whether an object is non-zero or zero. */
2732 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2736 /* These don't change whether an object is zero or non-zero, but
2737 we can't ignore them if their second arg has side-effects. */
2738 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
2739 return build (COMPOUND_EXPR, boolean_type_node, TREE_OPERAND (expr, 1),
2740 truthvalue_conversion (TREE_OPERAND (expr, 0)));
2742 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2745 /* Distribute the conversion into the arms of a COND_EXPR. */
2746 return fold (build (COND_EXPR, boolean_type_node, TREE_OPERAND (expr, 0),
2747 truthvalue_conversion (TREE_OPERAND (expr, 1)),
2748 truthvalue_conversion (TREE_OPERAND (expr, 2))));
2751 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
2752 since that affects how `default_conversion' will behave. */
2753 if (TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE
2754 || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
2756 /* fall through... */
2758 /* If this is widening the argument, we can ignore it. */
2759 if (TYPE_PRECISION (TREE_TYPE (expr))
2760 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
2761 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2765 /* With IEEE arithmetic, x - x may not equal 0, so we can't optimize
2767 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
2768 && TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE)
2770 /* fall through... */
2772 /* This and MINUS_EXPR can be changed into a comparison of the
2774 if (TREE_TYPE (TREE_OPERAND (expr, 0))
2775 == TREE_TYPE (TREE_OPERAND (expr, 1)))
2776 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2777 TREE_OPERAND (expr, 1), 1);
2778 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2779 fold (build1 (NOP_EXPR,
2780 TREE_TYPE (TREE_OPERAND (expr, 0)),
2781 TREE_OPERAND (expr, 1))), 1);
2784 if (integer_onep (TREE_OPERAND (expr, 1))
2785 && TREE_TYPE (expr) != boolean_type_node)
2786 /* Using convert here would cause infinite recursion. */
2787 return build1 (NOP_EXPR, boolean_type_node, expr);
2791 if (warn_parentheses && C_EXP_ORIGINAL_CODE (expr) == MODIFY_EXPR)
2792 warning ("suggest parentheses around assignment used as truth value");
2799 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
2801 tree tem = save_expr (expr);
2802 return (build_binary_op
2803 ((TREE_SIDE_EFFECTS (expr)
2804 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
2805 truthvalue_conversion (build_unary_op (REALPART_EXPR, tem, 0)),
2806 truthvalue_conversion (build_unary_op (IMAGPART_EXPR, tem, 0)),
2810 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
2814 /* Read the rest of a #-directive from input stream FINPUT.
2815 In normal use, the directive name and the white space after it
2816 have already been read, so they won't be included in the result.
2817 We allow for the fact that the directive line may contain
2818 a newline embedded within a character or string literal which forms
2819 a part of the directive.
2821 The value is a string in a reusable buffer. It remains valid
2822 only until the next time this function is called. */
2823 unsigned char *yy_cur, *yy_lim;
2825 #define GETC() (yy_cur < yy_lim ? *yy_cur++ : yy_get_token ())
2826 #define UNGETC(c) ((c), yy_cur--)
2833 parse_in.limit = parse_in.token_buffer;
2834 cpp_token = cpp_get_token (&parse_in);
2835 if (cpp_token == CPP_EOF)
2837 yy_lim = CPP_PWRITTEN (&parse_in);
2838 yy_cur = parse_in.token_buffer;
2839 if (yy_cur < yy_lim)
2845 get_directive_line ()
2847 static char *directive_buffer = NULL;
2848 static unsigned buffer_length = 0;
2850 register char *buffer_limit;
2851 register int looking_for = 0;
2852 register int char_escaped = 0;
2854 if (buffer_length == 0)
2856 directive_buffer = (char *)xmalloc (128);
2857 buffer_length = 128;
2860 buffer_limit = &directive_buffer[buffer_length];
2862 for (p = directive_buffer; ; )
2866 /* Make buffer bigger if it is full. */
2867 if (p >= buffer_limit)
2869 register unsigned bytes_used = (p - directive_buffer);
2873 = (char *)xrealloc (directive_buffer, buffer_length);
2874 p = &directive_buffer[bytes_used];
2875 buffer_limit = &directive_buffer[buffer_length];
2880 /* Discard initial whitespace. */
2881 if ((c == ' ' || c == '\t') && p == directive_buffer)
2884 /* Detect the end of the directive. */
2885 if (c == '\n' && looking_for == 0)
2894 return directive_buffer;
2896 /* Handle string and character constant syntax. */
2899 if (looking_for == c && !char_escaped)
2900 looking_for = 0; /* Found terminator... stop looking. */
2903 if (c == '\'' || c == '"')
2904 looking_for = c; /* Don't stop buffering until we see another
2905 another one of these (or an EOF). */
2907 /* Handle backslash. */
2908 char_escaped = (c == '\\' && ! char_escaped);
2912 /* Read the rest of a #-directive from input stream FINPUT.
2913 In normal use, the directive name and the white space after it
2914 have already been read, so they won't be included in the result.
2915 We allow for the fact that the directive line may contain
2916 a newline embedded within a character or string literal which forms
2917 a part of the directive.
2919 The value is a string in a reusable buffer. It remains valid
2920 only until the next time this function is called.
2922 The terminating character ('\n' or EOF) is left in FINPUT for the
2923 caller to re-read. */
2926 get_directive_line (finput)
2927 register FILE *finput;
2929 static char *directive_buffer = NULL;
2930 static unsigned buffer_length = 0;
2932 register char *buffer_limit;
2933 register int looking_for = 0;
2934 register int char_escaped = 0;
2936 if (buffer_length == 0)
2938 directive_buffer = (char *)xmalloc (128);
2939 buffer_length = 128;
2942 buffer_limit = &directive_buffer[buffer_length];
2944 for (p = directive_buffer; ; )
2948 /* Make buffer bigger if it is full. */
2949 if (p >= buffer_limit)
2951 register unsigned bytes_used = (p - directive_buffer);
2955 = (char *)xrealloc (directive_buffer, buffer_length);
2956 p = &directive_buffer[bytes_used];
2957 buffer_limit = &directive_buffer[buffer_length];
2962 /* Discard initial whitespace. */
2963 if ((c == ' ' || c == '\t') && p == directive_buffer)
2966 /* Detect the end of the directive. */
2967 if (looking_for == 0
2968 && (c == '\n' || c == EOF))
2977 return directive_buffer;
2979 /* Handle string and character constant syntax. */
2982 if (looking_for == c && !char_escaped)
2983 looking_for = 0; /* Found terminator... stop looking. */
2986 if (c == '\'' || c == '"')
2987 looking_for = c; /* Don't stop buffering until we see another
2988 one of these (or an EOF). */
2990 /* Handle backslash. */
2991 char_escaped = (c == '\\' && ! char_escaped);
2994 #endif /* !USE_CPPLIB */
2996 /* Make a variant type in the proper way for C/C++, propagating qualifiers
2997 down to the element type of an array. */
3000 c_build_qualified_type (type, type_quals)
3004 /* A restrict-qualified pointer type must be a pointer to object or
3005 incomplete type. Note that the use of POINTER_TYPE_P also allows
3006 REFERENCE_TYPEs, which is appropriate for C++. Unfortunately,
3007 the C++ front-end also use POINTER_TYPE for pointer-to-member
3008 values, so even though it should be illegal to use `restrict'
3009 with such an entity we don't flag that here. Thus, special case
3010 code for that case is required in the C++ front-end. */
3011 if ((type_quals & TYPE_QUAL_RESTRICT)
3012 && (!POINTER_TYPE_P (type)
3013 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
3015 error ("invalid use of `restrict'");
3016 type_quals &= ~TYPE_QUAL_RESTRICT;
3019 if (TREE_CODE (type) == ARRAY_TYPE)
3020 return build_array_type (c_build_qualified_type (TREE_TYPE (type),
3022 TYPE_DOMAIN (type));
3023 return build_qualified_type (type, type_quals);
3026 /* Apply the TYPE_QUALS to the new DECL. */
3029 c_apply_type_quals_to_decl (type_quals, decl)
3033 if (type_quals & TYPE_QUAL_CONST)
3034 TREE_READONLY (decl) = 1;
3035 if (type_quals & TYPE_QUAL_VOLATILE)
3037 TREE_SIDE_EFFECTS (decl) = 1;
3038 TREE_THIS_VOLATILE (decl) = 1;
3040 if (type_quals & TYPE_QUAL_RESTRICT)
3042 if (!TREE_TYPE (decl)
3043 || !POINTER_TYPE_P (TREE_TYPE (decl))
3044 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (TREE_TYPE (decl))))
3045 error ("invalid use of `restrict'");
3046 else if (flag_strict_aliasing)
3048 /* No two restricted pointers can point at the same thing.
3049 However, a restricted pointer can point at the same thing
3050 as an unrestricted pointer, if that unrestricted pointer
3051 is based on the restricted pointer. So, we make the
3052 alias set for the restricted pointer a subset of the
3053 alias set for the type pointed to by the type of the
3056 int pointed_to_alias_set
3057 = get_alias_set (TREE_TYPE (TREE_TYPE (decl)));
3059 if (!pointed_to_alias_set)
3060 /* It's not legal to make a subset of alias set zero. */
3064 DECL_POINTER_ALIAS_SET (decl) = new_alias_set ();
3065 record_alias_subset (pointed_to_alias_set,
3066 DECL_POINTER_ALIAS_SET (decl));
3072 /* T is an expression with pointer type. Find the DECL on which this
3073 expression is based. (For example, in `a[i]' this would be `a'.)
3074 If there is no such DECL, or a unique decl cannot be determined,
3075 NULL_TREE is retured. */
3078 c_find_base_decl (t)
3084 if (t == NULL_TREE || t == error_mark_node)
3087 if (!POINTER_TYPE_P (TREE_TYPE (t)))
3092 if (TREE_CODE (t) == FIELD_DECL
3093 || TREE_CODE (t) == PARM_DECL
3094 || TREE_CODE (t) == VAR_DECL)
3095 /* Aha, we found a pointer-typed declaration. */
3098 /* It would be nice to deal with COMPONENT_REFs here. If we could
3099 tell that `a' and `b' were the same, then `a->f' and `b->f' are
3102 /* Handle general expressions. */
3103 switch (TREE_CODE_CLASS (TREE_CODE (t)))
3108 for (i = tree_code_length [(int) TREE_CODE (t)]; --i >= 0;)
3110 tree d = c_find_base_decl (TREE_OPERAND (t, i));
3115 else if (d && d != decl)
3116 /* Two different declarations. That's confusing; let's
3117 just assume we don't know what's going on. */
3130 /* Return the typed-based alias set for T, which may be an expression
3139 if (t == error_mark_node)
3142 type = (TREE_CODE_CLASS (TREE_CODE (t)) == 't')
3143 ? t : TREE_TYPE (t);
3145 if (type == error_mark_node)
3148 /* Deal with special cases first; for certain kinds of references
3149 we're interested in more than just the type. */
3151 if (TREE_CODE (t) == BIT_FIELD_REF)
3152 /* Perhaps reads and writes to this piece of data alias fields
3153 neighboring the bitfield. Perhaps that's impossible. For now,
3154 let's just assume that bitfields can alias everything, which is
3155 the conservative assumption. */
3158 if ((TREE_CODE (t) == COMPONENT_REF
3159 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
3160 /* Also permit punning when accessing an array which is a union
3161 member. This makes the current sparc va_arg macro work, but may
3162 not be otherwise necessary. */
3163 || (TREE_CODE (t) == ARRAY_REF
3164 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF
3165 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
3167 /* Permit type-punning when accessing a union, provided the
3168 access is directly through the union. For example, this code does
3169 not permit taking the address of a union member and then
3170 storing through it. Even the type-punning allowed here is a
3171 GCC extension, albeit a common and useful one; the C standard
3172 says that such accesses have implementation-defined behavior. */
3175 if (TREE_CODE (t) == INDIRECT_REF)
3177 /* Check for accesses through restrict-qualified pointers. */
3178 tree decl = c_find_base_decl (TREE_OPERAND (t, 0));
3180 if (decl && DECL_POINTER_ALIAS_SET_KNOWN_P (decl))
3181 /* We use the alias set indicated in the declaration. */
3182 return DECL_POINTER_ALIAS_SET (decl);
3185 /* From here on, only the type matters. */
3187 if (TYPE_ALIAS_SET_KNOWN_P (type))
3188 /* If we've already calculated the value, just return it. */
3189 return TYPE_ALIAS_SET (type);
3190 else if (TYPE_MAIN_VARIANT (type) != type)
3191 /* The C standard specifically allows aliasing between
3192 cv-qualified variants of types. */
3193 TYPE_ALIAS_SET (type) = c_get_alias_set (TYPE_MAIN_VARIANT (type));
3194 else if (TREE_CODE (type) == INTEGER_TYPE)
3196 tree signed_variant;
3198 /* The C standard specifically allows aliasing between signed and
3199 unsigned variants of the same type. We treat the signed
3200 variant as canonical. */
3201 signed_variant = signed_type (type);
3203 if (signed_variant != type)
3204 TYPE_ALIAS_SET (type) = c_get_alias_set (signed_variant);
3205 else if (signed_variant == signed_char_type_node)
3206 /* The C standard guarantess that any object may be accessed
3207 via an lvalue that has character type. We don't have to
3208 check for unsigned_char_type_node or char_type_node because
3209 we are specifically looking at the signed variant. */
3210 TYPE_ALIAS_SET (type) = 0;
3212 else if (TREE_CODE (type) == ARRAY_TYPE)
3213 /* Anything that can alias one of the array elements can alias
3214 the entire array as well. */
3215 TYPE_ALIAS_SET (type) = c_get_alias_set (TREE_TYPE (type));
3216 else if (TREE_CODE (type) == FUNCTION_TYPE)
3217 /* There are no objects of FUNCTION_TYPE, so there's no point in
3218 using up an alias set for them. (There are, of course,
3219 pointers and references to functions, but that's
3221 TYPE_ALIAS_SET (type) = 0;
3222 else if (TREE_CODE (type) == RECORD_TYPE
3223 || TREE_CODE (type) == UNION_TYPE)
3224 /* If TYPE is a struct or union type then we're reading or
3225 writing an entire struct. Thus, we don't know anything about
3226 aliasing. (In theory, such an access can only alias objects
3227 whose type is the same as one of the fields, recursively, but
3228 we don't yet make any use of that information.) */
3229 TYPE_ALIAS_SET (type) = 0;
3231 if (!TYPE_ALIAS_SET_KNOWN_P (type))
3232 /* TYPE is something we haven't seen before. Put it in a new
3234 TYPE_ALIAS_SET (type) = new_alias_set ();
3236 return TYPE_ALIAS_SET (type);