1 /* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996 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. */
30 #ifndef WCHAR_TYPE_SIZE
32 #define WCHAR_TYPE_SIZE INT_TYPE_SIZE
34 #define WCHAR_TYPE_SIZE BITS_PER_WORD
38 extern struct obstack permanent_obstack;
40 /* Nonzero means the expression being parsed will never be evaluated.
41 This is a count, since unevaluated expressions can nest. */
44 enum attrs {A_PACKED, A_NOCOMMON, A_COMMON, A_NORETURN, A_CONST, A_T_UNION,
45 A_CONSTRUCTOR, A_DESTRUCTOR, A_MODE, A_SECTION, A_ALIGNED,
46 A_UNUSED, A_FORMAT, A_FORMAT_ARG, A_WEAK, A_ALIAS};
48 static void declare_hidden_char_array PROTO((char *, char *));
49 static void add_attribute PROTO((enum attrs, char *,
51 static void init_attributes PROTO((void));
52 static void record_international_format PROTO((tree, tree, int));
54 /* Make bindings for __FUNCTION__ and __PRETTY_FUNCTION__. */
57 declare_function_name ()
59 char *name, *printable_name;
61 if (current_function_decl == NULL)
64 printable_name = "top level";
68 char *kind = "function";
69 if (TREE_CODE (TREE_TYPE (current_function_decl)) == METHOD_TYPE)
71 /* Allow functions to be nameless (such as artificial ones). */
72 if (DECL_NAME (current_function_decl))
73 name = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
76 printable_name = (*decl_printable_name) (current_function_decl, &kind);
79 declare_hidden_char_array ("__FUNCTION__", name);
80 declare_hidden_char_array ("__PRETTY_FUNCTION__", printable_name);
84 declare_hidden_char_array (name, value)
87 tree decl, type, init;
90 /* If the default size of char arrays isn't big enough for the name,
91 or if we want to give warnings for large objects, make a bigger one. */
92 vlen = strlen (value) + 1;
93 type = char_array_type_node;
94 if (TREE_INT_CST_LOW (TYPE_MAX_VALUE (TREE_TYPE (type))) < vlen
96 type = build_array_type (char_type_node,
97 build_index_type (build_int_2 (vlen, 0)));
98 push_obstacks_nochange ();
99 decl = build_decl (VAR_DECL, get_identifier (name), type);
100 TREE_STATIC (decl) = 1;
101 TREE_READONLY (decl) = 1;
102 TREE_ASM_WRITTEN (decl) = 1;
103 DECL_SOURCE_LINE (decl) = 0;
104 DECL_ARTIFICIAL (decl) = 1;
105 DECL_IN_SYSTEM_HEADER (decl) = 1;
106 DECL_IGNORED_P (decl) = 1;
107 init = build_string (vlen, value);
108 TREE_TYPE (init) = type;
109 DECL_INITIAL (decl) = init;
110 finish_decl (pushdecl (decl), init, NULL_TREE);
113 /* Given a chain of STRING_CST nodes,
114 concatenate them into one STRING_CST
115 and give it a suitable array-of-chars data type. */
118 combine_strings (strings)
121 register tree value, t;
122 register int length = 1;
125 int wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
128 if (TREE_CHAIN (strings))
130 /* More than one in the chain, so concatenate. */
131 register char *p, *q;
133 /* Don't include the \0 at the end of each substring,
134 except for the last one.
135 Count wide strings and ordinary strings separately. */
136 for (t = strings; t; t = TREE_CHAIN (t))
138 if (TREE_TYPE (t) == wchar_array_type_node)
140 wide_length += (TREE_STRING_LENGTH (t) - wchar_bytes);
144 length += (TREE_STRING_LENGTH (t) - 1);
147 /* If anything is wide, the non-wides will be converted,
148 which makes them take more space. */
150 length = length * wchar_bytes + wide_length;
152 p = savealloc (length);
154 /* Copy the individual strings into the new combined string.
155 If the combined string is wide, convert the chars to ints
156 for any individual strings that are not wide. */
159 for (t = strings; t; t = TREE_CHAIN (t))
161 int len = (TREE_STRING_LENGTH (t)
162 - ((TREE_TYPE (t) == wchar_array_type_node)
164 if ((TREE_TYPE (t) == wchar_array_type_node) == wide_flag)
166 bcopy (TREE_STRING_POINTER (t), q, len);
172 for (i = 0; i < len; i++)
174 if (WCHAR_TYPE_SIZE == HOST_BITS_PER_SHORT)
175 ((short *) q)[i] = TREE_STRING_POINTER (t)[i];
177 ((int *) q)[i] = TREE_STRING_POINTER (t)[i];
179 q += len * wchar_bytes;
185 for (i = 0; i < wchar_bytes; i++)
191 value = make_node (STRING_CST);
192 TREE_STRING_POINTER (value) = p;
193 TREE_STRING_LENGTH (value) = length;
194 TREE_CONSTANT (value) = 1;
199 length = TREE_STRING_LENGTH (value);
200 if (TREE_TYPE (value) == wchar_array_type_node)
204 /* Compute the number of elements, for the array type. */
205 nchars = wide_flag ? length / wchar_bytes : length;
207 /* Create the array type for the string constant.
208 -Wwrite-strings says make the string constant an array of const char
209 so that copying it to a non-const pointer will get a warning. */
210 if (warn_write_strings
211 && (! flag_traditional && ! flag_writable_strings))
214 = build_type_variant (wide_flag ? wchar_type_node : char_type_node,
217 = build_array_type (elements,
218 build_index_type (build_int_2 (nchars - 1, 0)));
222 = build_array_type (wide_flag ? wchar_type_node : char_type_node,
223 build_index_type (build_int_2 (nchars - 1, 0)));
224 TREE_CONSTANT (value) = 1;
225 TREE_STATIC (value) = 1;
229 /* To speed up processing of attributes, we maintain an array of
230 IDENTIFIER_NODES and the corresponding attribute types. */
232 /* Array to hold attribute information. */
234 static struct {enum attrs id; tree name; int min, max, decl_req;} attrtab[50];
236 static int attrtab_idx = 0;
238 /* Add an entry to the attribute table above. */
241 add_attribute (id, string, min_len, max_len, decl_req)
244 int min_len, max_len;
249 attrtab[attrtab_idx].id = id;
250 attrtab[attrtab_idx].name = get_identifier (string);
251 attrtab[attrtab_idx].min = min_len;
252 attrtab[attrtab_idx].max = max_len;
253 attrtab[attrtab_idx++].decl_req = decl_req;
255 sprintf (buf, "__%s__", string);
257 attrtab[attrtab_idx].id = id;
258 attrtab[attrtab_idx].name = get_identifier (buf);
259 attrtab[attrtab_idx].min = min_len;
260 attrtab[attrtab_idx].max = max_len;
261 attrtab[attrtab_idx++].decl_req = decl_req;
264 /* Initialize attribute table. */
269 add_attribute (A_PACKED, "packed", 0, 0, 0);
270 add_attribute (A_NOCOMMON, "nocommon", 0, 0, 1);
271 add_attribute (A_COMMON, "common", 0, 0, 1);
272 add_attribute (A_NORETURN, "noreturn", 0, 0, 1);
273 add_attribute (A_NORETURN, "volatile", 0, 0, 1);
274 add_attribute (A_UNUSED, "unused", 0, 0, 1);
275 add_attribute (A_CONST, "const", 0, 0, 1);
276 add_attribute (A_T_UNION, "transparent_union", 0, 0, 0);
277 add_attribute (A_CONSTRUCTOR, "constructor", 0, 0, 1);
278 add_attribute (A_DESTRUCTOR, "destructor", 0, 0, 1);
279 add_attribute (A_MODE, "mode", 1, 1, 1);
280 add_attribute (A_SECTION, "section", 1, 1, 1);
281 add_attribute (A_ALIGNED, "aligned", 0, 1, 0);
282 add_attribute (A_FORMAT, "format", 3, 3, 1);
283 add_attribute (A_FORMAT_ARG, "format_arg", 1, 1, 1);
284 add_attribute (A_WEAK, "weak", 0, 0, 1);
285 add_attribute (A_ALIAS, "alias", 1, 1, 1);
288 /* Process the attributes listed in ATTRIBUTES and PREFIX_ATTRIBUTES
289 and install them in NODE, which is either a DECL (including a TYPE_DECL)
290 or a TYPE. PREFIX_ATTRIBUTES can appear after the declaration specifiers
291 and declaration modifiers but before the declaration proper. */
294 decl_attributes (node, attributes, prefix_attributes)
295 tree node, attributes, prefix_attributes;
301 if (attrtab_idx == 0)
304 if (TREE_CODE_CLASS (TREE_CODE (node)) == 'd')
307 type = TREE_TYPE (decl);
308 is_type = TREE_CODE (node) == TYPE_DECL;
310 else if (TREE_CODE_CLASS (TREE_CODE (node)) == 't')
311 type = node, is_type = 1;
313 attributes = chainon (prefix_attributes, attributes);
315 for (a = attributes; a; a = TREE_CHAIN (a))
317 tree name = TREE_PURPOSE (a);
318 tree args = TREE_VALUE (a);
322 for (i = 0; i < attrtab_idx; i++)
323 if (attrtab[i].name == name)
326 if (i == attrtab_idx)
328 if (! valid_machine_attribute (name, args, decl, type))
329 warning ("`%s' attribute directive ignored",
330 IDENTIFIER_POINTER (name));
332 type = TREE_TYPE (decl);
335 else if (attrtab[i].decl_req && decl == 0)
337 warning ("`%s' attribute does not apply to types",
338 IDENTIFIER_POINTER (name));
341 else if (list_length (args) < attrtab[i].min
342 || list_length (args) > attrtab[i].max)
344 error ("wrong number of arguments specified for `%s' attribute",
345 IDENTIFIER_POINTER (name));
354 TYPE_PACKED (type) = 1;
355 else if (TREE_CODE (decl) == FIELD_DECL)
356 DECL_PACKED (decl) = 1;
357 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
358 used for DECL_REGISTER. It wouldn't mean anything anyway. */
360 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
364 if (TREE_CODE (decl) == VAR_DECL)
365 DECL_COMMON (decl) = 0;
367 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
371 if (TREE_CODE (decl) == VAR_DECL)
372 DECL_COMMON (decl) = 1;
374 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
378 if (TREE_CODE (decl) == FUNCTION_DECL)
379 TREE_THIS_VOLATILE (decl) = 1;
380 else if (TREE_CODE (type) == POINTER_TYPE
381 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
382 TREE_TYPE (decl) = type
384 (build_type_variant (TREE_TYPE (type),
385 TREE_READONLY (TREE_TYPE (type)), 1));
387 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
391 if (TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == VAR_DECL
392 || TREE_CODE (decl) == FUNCTION_DECL)
393 TREE_USED (decl) = 1;
395 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
399 if (TREE_CODE (decl) == FUNCTION_DECL)
400 TREE_READONLY (decl) = 1;
401 else if (TREE_CODE (type) == POINTER_TYPE
402 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
403 TREE_TYPE (decl) = type
405 (build_type_variant (TREE_TYPE (type), 1,
406 TREE_THIS_VOLATILE (TREE_TYPE (type))));
408 warning ( "`%s' attribute ignored", IDENTIFIER_POINTER (name));
413 && TREE_CODE (type) == UNION_TYPE
415 || (TYPE_FIELDS (type) != 0
416 && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))))
417 TYPE_TRANSPARENT_UNION (type) = 1;
418 else if (decl != 0 && TREE_CODE (decl) == PARM_DECL
419 && TREE_CODE (type) == UNION_TYPE
420 && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))
421 DECL_TRANSPARENT_UNION (decl) = 1;
423 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
427 if (TREE_CODE (decl) == FUNCTION_DECL
428 && TREE_CODE (type) == FUNCTION_TYPE
429 && decl_function_context (decl) == 0)
431 DECL_STATIC_CONSTRUCTOR (decl) = 1;
432 TREE_USED (decl) = 1;
435 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
439 if (TREE_CODE (decl) == FUNCTION_DECL
440 && TREE_CODE (type) == FUNCTION_TYPE
441 && decl_function_context (decl) == 0)
443 DECL_STATIC_DESTRUCTOR (decl) = 1;
444 TREE_USED (decl) = 1;
447 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
451 if (TREE_CODE (TREE_VALUE (args)) != IDENTIFIER_NODE)
452 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
456 char *p = IDENTIFIER_POINTER (TREE_VALUE (args));
457 int len = strlen (p);
458 enum machine_mode mode = VOIDmode;
461 if (len > 4 && p[0] == '_' && p[1] == '_'
462 && p[len - 1] == '_' && p[len - 2] == '_')
464 char *newp = (char *) alloca (len - 1);
466 strcpy (newp, &p[2]);
467 newp[len - 4] = '\0';
471 /* Give this decl a type with the specified mode.
472 First check for the special modes. */
473 if (! strcmp (p, "byte"))
475 else if (!strcmp (p, "word"))
477 else if (! strcmp (p, "pointer"))
480 for (j = 0; j < NUM_MACHINE_MODES; j++)
481 if (!strcmp (p, GET_MODE_NAME (j)))
482 mode = (enum machine_mode) j;
484 if (mode == VOIDmode)
485 error ("unknown machine mode `%s'", p);
486 else if (0 == (typefm = type_for_mode (mode,
487 TREE_UNSIGNED (type))))
488 error ("no data type for mode `%s'", p);
491 TREE_TYPE (decl) = type = typefm;
492 DECL_SIZE (decl) = 0;
493 layout_decl (decl, 0);
499 #ifdef ASM_OUTPUT_SECTION_NAME
500 if ((TREE_CODE (decl) == FUNCTION_DECL
501 || TREE_CODE (decl) == VAR_DECL)
502 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
504 if (TREE_CODE (decl) == VAR_DECL
505 && current_function_decl != NULL_TREE
506 && ! TREE_STATIC (decl))
507 error_with_decl (decl,
508 "section attribute cannot be specified for local variables");
509 /* The decl may have already been given a section attribute from
510 a previous declaration. Ensure they match. */
511 else if (DECL_SECTION_NAME (decl) != NULL_TREE
512 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
513 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
514 error_with_decl (node,
515 "section of `%s' conflicts with previous declaration");
517 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
520 error_with_decl (node,
521 "section attribute not allowed for `%s'");
523 error_with_decl (node,
524 "section attributes are not supported for this target");
531 = (args ? TREE_VALUE (args)
532 : size_int (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
535 /* Strip any NOPs of any kind. */
536 while (TREE_CODE (align_expr) == NOP_EXPR
537 || TREE_CODE (align_expr) == CONVERT_EXPR
538 || TREE_CODE (align_expr) == NON_LVALUE_EXPR)
539 align_expr = TREE_OPERAND (align_expr, 0);
541 if (TREE_CODE (align_expr) != INTEGER_CST)
543 error ("requested alignment is not a constant");
547 align = TREE_INT_CST_LOW (align_expr) * BITS_PER_UNIT;
549 if (exact_log2 (align) == -1)
550 error ("requested alignment is not a power of 2");
552 TYPE_ALIGN (type) = align;
553 else if (TREE_CODE (decl) != VAR_DECL
554 && TREE_CODE (decl) != FIELD_DECL)
555 error_with_decl (decl,
556 "alignment may not be specified for `%s'");
558 DECL_ALIGN (decl) = align;
564 tree format_type = TREE_VALUE (args);
565 tree format_num_expr = TREE_VALUE (TREE_CHAIN (args));
566 tree first_arg_num_expr
567 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)));
574 if (TREE_CODE (decl) != FUNCTION_DECL)
576 error_with_decl (decl,
577 "argument format specified for non-function `%s'");
581 if (TREE_CODE (format_type) == IDENTIFIER_NODE
582 && (!strcmp (IDENTIFIER_POINTER (format_type), "printf")
583 || !strcmp (IDENTIFIER_POINTER (format_type),
586 else if (TREE_CODE (format_type) == IDENTIFIER_NODE
587 && (!strcmp (IDENTIFIER_POINTER (format_type), "scanf")
588 || !strcmp (IDENTIFIER_POINTER (format_type),
591 else if (TREE_CODE (format_type) == IDENTIFIER_NODE)
593 error ("`%s' is an unrecognized format function type",
594 IDENTIFIER_POINTER (format_type));
599 error ("unrecognized format specifier");
603 /* Strip any conversions from the string index and first arg number
604 and verify they are constants. */
605 while (TREE_CODE (format_num_expr) == NOP_EXPR
606 || TREE_CODE (format_num_expr) == CONVERT_EXPR
607 || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
608 format_num_expr = TREE_OPERAND (format_num_expr, 0);
610 while (TREE_CODE (first_arg_num_expr) == NOP_EXPR
611 || TREE_CODE (first_arg_num_expr) == CONVERT_EXPR
612 || TREE_CODE (first_arg_num_expr) == NON_LVALUE_EXPR)
613 first_arg_num_expr = TREE_OPERAND (first_arg_num_expr, 0);
615 if (TREE_CODE (format_num_expr) != INTEGER_CST
616 || TREE_CODE (first_arg_num_expr) != INTEGER_CST)
618 error ("format string has non-constant operand number");
622 format_num = TREE_INT_CST_LOW (format_num_expr);
623 first_arg_num = TREE_INT_CST_LOW (first_arg_num_expr);
624 if (first_arg_num != 0 && first_arg_num <= format_num)
626 error ("format string arg follows the args to be formatted");
630 /* If a parameter list is specified, verify that the format_num
631 argument is actually a string, in case the format attribute
633 argument = TYPE_ARG_TYPES (type);
636 for (arg_num = 1; ; ++arg_num)
638 if (argument == 0 || arg_num == format_num)
640 argument = TREE_CHAIN (argument);
643 || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
644 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
647 error ("format string arg not a string type");
650 if (first_arg_num != 0)
652 /* Verify that first_arg_num points to the last arg,
655 arg_num++, argument = TREE_CHAIN (argument);
656 if (arg_num != first_arg_num)
658 error ("args to be formatted is not ...");
664 record_function_format (DECL_NAME (decl),
665 DECL_ASSEMBLER_NAME (decl),
666 is_scan, format_num, first_arg_num);
672 tree format_num_expr = TREE_VALUE (args);
673 int format_num, arg_num;
676 if (TREE_CODE (decl) != FUNCTION_DECL)
678 error_with_decl (decl,
679 "argument format specified for non-function `%s'");
683 /* Strip any conversions from the first arg number and verify it
685 while (TREE_CODE (format_num_expr) == NOP_EXPR
686 || TREE_CODE (format_num_expr) == CONVERT_EXPR
687 || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
688 format_num_expr = TREE_OPERAND (format_num_expr, 0);
690 if (TREE_CODE (format_num_expr) != INTEGER_CST)
692 error ("format string has non-constant operand number");
696 format_num = TREE_INT_CST_LOW (format_num_expr);
698 /* If a parameter list is specified, verify that the format_num
699 argument is actually a string, in case the format attribute
701 argument = TYPE_ARG_TYPES (type);
704 for (arg_num = 1; ; ++arg_num)
706 if (argument == 0 || arg_num == format_num)
708 argument = TREE_CHAIN (argument);
711 || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
712 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
715 error ("format string arg not a string type");
720 if (TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) != POINTER_TYPE
721 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (TREE_TYPE (decl))))
724 error ("function does not return string type");
728 record_international_format (DECL_NAME (decl),
729 DECL_ASSEMBLER_NAME (decl),
739 if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
740 || (TREE_CODE (decl) != FUNCTION_DECL && ! DECL_EXTERNAL (decl)))
741 error_with_decl (decl,
742 "`%s' defined both normally and as an alias");
743 else if (decl_function_context (decl) == 0)
745 tree id = get_identifier (TREE_STRING_POINTER
746 (TREE_VALUE (args)));
747 if (TREE_CODE (decl) == FUNCTION_DECL)
748 DECL_INITIAL (decl) = error_mark_node;
750 DECL_EXTERNAL (decl) = 0;
751 assemble_alias (decl, id);
754 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
760 /* Split SPECS_ATTRS, a list of declspecs and prefix attributes, into two
761 lists. SPECS_ATTRS may also be just a typespec (eg: RECORD_TYPE).
763 The head of the declspec list is stored in DECLSPECS.
764 The head of the attribute list is stored in PREFIX_ATTRIBUTES.
766 Note that attributes in SPECS_ATTRS are stored in the TREE_PURPOSE of
767 the list elements. We drop the containing TREE_LIST nodes and link the
768 resulting attributes together the way decl_attributes expects them. */
771 split_specs_attrs (specs_attrs, declspecs, prefix_attributes)
773 tree *declspecs, *prefix_attributes;
775 tree t, s, a, next, specs, attrs;
777 /* This can happen in c++ (eg: decl: typespec initdecls ';'). */
778 if (specs_attrs != NULL_TREE
779 && TREE_CODE (specs_attrs) != TREE_LIST)
781 *declspecs = specs_attrs;
782 *prefix_attributes = NULL_TREE;
786 /* Remember to keep the lists in the same order, element-wise. */
788 specs = s = NULL_TREE;
789 attrs = a = NULL_TREE;
790 for (t = specs_attrs; t; t = next)
792 next = TREE_CHAIN (t);
793 /* Declspecs have a non-NULL TREE_VALUE. */
794 if (TREE_VALUE (t) != NULL_TREE)
796 if (specs == NULL_TREE)
806 if (attrs == NULL_TREE)
807 attrs = a = TREE_PURPOSE (t);
810 TREE_CHAIN (a) = TREE_PURPOSE (t);
811 a = TREE_PURPOSE (t);
813 /* More attrs can be linked here, move A to the end. */
814 while (TREE_CHAIN (a) != NULL_TREE)
819 /* Terminate the lists. */
821 TREE_CHAIN (s) = NULL_TREE;
823 TREE_CHAIN (a) = NULL_TREE;
827 *prefix_attributes = attrs;
830 /* Check a printf/fprintf/sprintf/scanf/fscanf/sscanf format against
833 #define T_I &integer_type_node
834 #define T_L &long_integer_type_node
835 #define T_LL &long_long_integer_type_node
836 #define T_S &short_integer_type_node
837 #define T_UI &unsigned_type_node
838 #define T_UL &long_unsigned_type_node
839 #define T_ULL &long_long_unsigned_type_node
840 #define T_US &short_unsigned_type_node
841 #define T_F &float_type_node
842 #define T_D &double_type_node
843 #define T_LD &long_double_type_node
844 #define T_C &char_type_node
845 #define T_V &void_type_node
846 #define T_W &wchar_type_node
847 #define T_ST &sizetype
852 /* Type of argument if no length modifier is used. */
854 /* Type of argument if length modifier for shortening is used.
855 If NULL, then this modifier is not allowed. */
857 /* Type of argument if length modifier `l' is used.
858 If NULL, then this modifier is not allowed. */
860 /* Type of argument if length modifier `q' or `ll' is used.
861 If NULL, then this modifier is not allowed. */
863 /* Type of argument if length modifier `L' is used.
864 If NULL, then this modifier is not allowed. */
866 /* List of other modifier characters allowed with these options. */
870 static format_char_info print_char_table[] = {
871 { "di", 0, T_I, T_I, T_L, T_LL, T_LL, "-wp0 +" },
872 { "oxX", 0, T_UI, T_UI, T_UL, T_ULL, T_ULL, "-wp0#" },
873 { "u", 0, T_UI, T_UI, T_UL, T_ULL, T_ULL, "-wp0" },
874 /* Two GNU extensions. */
875 { "Z", 0, T_ST, NULL, NULL, NULL, NULL, "-wp0" },
876 { "m", 0, T_V, NULL, NULL, NULL, NULL, "-wp" },
877 { "feEgG", 0, T_D, NULL, NULL, NULL, T_LD, "-wp0 +#" },
878 { "c", 0, T_I, NULL, T_W, NULL, NULL, "-w" },
879 { "C", 0, T_W, NULL, NULL, NULL, NULL, "-w" },
880 { "s", 1, T_C, NULL, T_W, NULL, NULL, "-wp" },
881 { "S", 1, T_W, NULL, NULL, NULL, NULL, "-wp" },
882 { "p", 1, T_V, NULL, NULL, NULL, NULL, "-w" },
883 { "n", 1, T_I, T_S, T_L, T_LL, NULL, "" },
887 static format_char_info scan_char_table[] = {
888 { "di", 1, T_I, T_S, T_L, T_LL, T_LL, "*" },
889 { "ouxX", 1, T_UI, T_US, T_UL, T_ULL, T_ULL, "*" },
890 { "efgEG", 1, T_F, NULL, T_D, NULL, T_LD, "*" },
891 { "sc", 1, T_C, NULL, T_W, NULL, NULL, "*a" },
892 { "[", 1, T_C, NULL, NULL, NULL, NULL, "*a" },
893 { "C", 1, T_W, NULL, NULL, NULL, NULL, "*" },
894 { "S", 1, T_W, NULL, NULL, NULL, NULL, "*" },
895 { "p", 2, T_V, NULL, NULL, NULL, NULL, "*" },
896 { "n", 1, T_I, T_S, T_L, T_LL, NULL, "" },
900 typedef struct function_format_info
902 struct function_format_info *next; /* next structure on the list */
903 tree name; /* identifier such as "printf" */
904 tree assembler_name; /* optional mangled identifier (for C++) */
905 int is_scan; /* TRUE if *scanf */
906 int format_num; /* number of format argument */
907 int first_arg_num; /* number of first arg (zero for varargs) */
908 } function_format_info;
910 static function_format_info *function_format_list = NULL;
912 typedef struct international_format_info
914 struct international_format_info *next; /* next structure on the list */
915 tree name; /* identifier such as "gettext" */
916 tree assembler_name; /* optional mangled identifier (for C++) */
917 int format_num; /* number of format argument */
918 } international_format_info;
920 static international_format_info *international_format_list = NULL;
922 static void check_format_info PROTO((function_format_info *, tree));
924 /* Initialize the table of functions to perform format checking on.
925 The ANSI functions are always checked (whether <stdio.h> is
926 included or not), since it is common to call printf without
927 including <stdio.h>. There shouldn't be a problem with this,
928 since ANSI reserves these function names whether you include the
929 header file or not. In any case, the checking is harmless.
931 Also initialize the name of function that modify the format string for
932 internationalization purposes. */
935 init_function_format_info ()
937 record_function_format (get_identifier ("printf"), NULL_TREE, 0, 1, 2);
938 record_function_format (get_identifier ("fprintf"), NULL_TREE, 0, 2, 3);
939 record_function_format (get_identifier ("sprintf"), NULL_TREE, 0, 2, 3);
940 record_function_format (get_identifier ("scanf"), NULL_TREE, 1, 1, 2);
941 record_function_format (get_identifier ("fscanf"), NULL_TREE, 1, 2, 3);
942 record_function_format (get_identifier ("sscanf"), NULL_TREE, 1, 2, 3);
943 record_function_format (get_identifier ("vprintf"), NULL_TREE, 0, 1, 0);
944 record_function_format (get_identifier ("vfprintf"), NULL_TREE, 0, 2, 0);
945 record_function_format (get_identifier ("vsprintf"), NULL_TREE, 0, 2, 0);
947 record_international_format (get_identifier ("gettext"), NULL_TREE, 1);
948 record_international_format (get_identifier ("dgettext"), NULL_TREE, 2);
949 record_international_format (get_identifier ("dcgettext"), NULL_TREE, 2);
952 /* Record information for argument format checking. FUNCTION_IDENT is
953 the identifier node for the name of the function to check (its decl
954 need not exist yet). IS_SCAN is true for scanf-type format checking;
955 false indicates printf-style format checking. FORMAT_NUM is the number
956 of the argument which is the format control string (starting from 1).
957 FIRST_ARG_NUM is the number of the first actual argument to check
958 against the format string, or zero if no checking is not be done
959 (e.g. for varargs such as vfprintf). */
962 record_function_format (name, assembler_name, is_scan,
963 format_num, first_arg_num)
970 function_format_info *info;
972 /* Re-use existing structure if it's there. */
974 for (info = function_format_list; info; info = info->next)
976 if (info->name == name && info->assembler_name == assembler_name)
981 info = (function_format_info *) xmalloc (sizeof (function_format_info));
982 info->next = function_format_list;
983 function_format_list = info;
986 info->assembler_name = assembler_name;
989 info->is_scan = is_scan;
990 info->format_num = format_num;
991 info->first_arg_num = first_arg_num;
994 /* Record information for the names of function that modify the format
995 argument to format functions. FUNCTION_IDENT is the identifier node for
996 the name of the function (its decl need not exist yet) and FORMAT_NUM is
997 the number of the argument which is the format control string (starting
1001 record_international_format (name, assembler_name, format_num)
1003 tree assembler_name;
1006 international_format_info *info;
1008 /* Re-use existing structure if it's there. */
1010 for (info = international_format_list; info; info = info->next)
1012 if (info->name == name && info->assembler_name == assembler_name)
1019 = (international_format_info *)
1020 xmalloc (sizeof (international_format_info));
1021 info->next = international_format_list;
1022 international_format_list = info;
1025 info->assembler_name = assembler_name;
1028 info->format_num = format_num;
1031 static char tfaff[] = "too few arguments for format";
1033 /* Check the argument list of a call to printf, scanf, etc.
1034 NAME is the function identifier.
1035 ASSEMBLER_NAME is the function's assembler identifier.
1036 (Either NAME or ASSEMBLER_NAME, but not both, may be NULL_TREE.)
1037 PARAMS is the list of argument values. */
1040 check_function_format (name, assembler_name, params)
1042 tree assembler_name;
1045 function_format_info *info;
1047 /* See if this function is a format function. */
1048 for (info = function_format_list; info; info = info->next)
1050 if (info->assembler_name
1051 ? (info->assembler_name == assembler_name)
1052 : (info->name == name))
1054 /* Yup; check it. */
1055 check_format_info (info, params);
1061 /* Check the argument list of a call to printf, scanf, etc.
1062 INFO points to the function_format_info structure.
1063 PARAMS is the list of argument values. */
1066 check_format_info (info, params)
1067 function_format_info *info;
1072 int suppressed, wide, precise;
1080 tree first_fillin_param;
1082 format_char_info *fci;
1083 static char message[132];
1085 int has_operand_number = 0;
1087 /* Skip to format argument. If the argument isn't available, there's
1088 no work for us to do; prototype checking will catch the problem. */
1089 for (arg_num = 1; ; ++arg_num)
1093 if (arg_num == info->format_num)
1095 params = TREE_CHAIN (params);
1097 format_tree = TREE_VALUE (params);
1098 params = TREE_CHAIN (params);
1099 if (format_tree == 0)
1102 /* We can only check the format if it's a string constant. */
1103 while (TREE_CODE (format_tree) == NOP_EXPR)
1104 format_tree = TREE_OPERAND (format_tree, 0); /* strip coercion */
1106 if (TREE_CODE (format_tree) == CALL_EXPR
1107 && TREE_CODE (TREE_OPERAND (format_tree, 0)) == ADDR_EXPR
1108 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (format_tree, 0), 0))
1111 tree function = TREE_OPERAND (TREE_OPERAND (format_tree, 0), 0);
1113 /* See if this is a call to a known internationalization function
1114 that modifies the format arg. */
1115 international_format_info *info;
1117 for (info = international_format_list; info; info = info->next)
1118 if (info->assembler_name
1119 ? (info->assembler_name == DECL_ASSEMBLER_NAME (function))
1120 : (info->name == DECL_NAME (function)))
1125 for (inner_args = TREE_OPERAND (format_tree, 1), i = 1;
1127 inner_args = TREE_CHAIN (inner_args), i++)
1128 if (i == info->format_num)
1130 format_tree = TREE_VALUE (inner_args);
1132 while (TREE_CODE (format_tree) == NOP_EXPR)
1133 format_tree = TREE_OPERAND (format_tree, 0);
1138 if (integer_zerop (format_tree))
1140 warning ("null format string");
1143 if (TREE_CODE (format_tree) != ADDR_EXPR)
1145 format_tree = TREE_OPERAND (format_tree, 0);
1146 if (TREE_CODE (format_tree) != STRING_CST)
1148 format_chars = TREE_STRING_POINTER (format_tree);
1149 format_length = TREE_STRING_LENGTH (format_tree);
1150 if (format_length <= 1)
1151 warning ("zero-length format string");
1152 if (format_chars[--format_length] != 0)
1154 warning ("unterminated format string");
1157 /* Skip to first argument to check. */
1158 while (arg_num + 1 < info->first_arg_num)
1162 params = TREE_CHAIN (params);
1166 first_fillin_param = params;
1170 if (*format_chars == 0)
1172 if (format_chars - TREE_STRING_POINTER (format_tree) != format_length)
1173 warning ("embedded `\\0' in format");
1174 if (info->first_arg_num != 0 && params != 0 && ! has_operand_number)
1175 warning ("too many arguments for format");
1178 if (*format_chars++ != '%')
1180 if (*format_chars == 0)
1182 warning ("spurious trailing `%%' in format");
1185 if (*format_chars == '%')
1191 suppressed = wide = precise = FALSE;
1194 suppressed = *format_chars == '*';
1197 while (isdigit (*format_chars))
1202 /* See if we have a number followed by a dollar sign. If we do,
1203 it is an operand number, so set PARAMS to that operand. */
1204 if (*format_chars >= '0' && *format_chars <= '9')
1206 char *p = format_chars;
1208 while (*p >= '0' && *p++ <= '9')
1213 int opnum = atoi (format_chars);
1215 params = first_fillin_param;
1216 format_chars = p + 1;
1217 has_operand_number = 1;
1219 for (i = 1; i < opnum && params != 0; i++)
1220 params = TREE_CHAIN (params);
1222 if (opnum == 0 || params == 0)
1224 warning ("operand number out of range in format");
1230 while (*format_chars != 0 && index (" +#0-", *format_chars) != 0)
1232 if (index (flag_chars, *format_chars) != 0)
1234 sprintf (message, "repeated `%c' flag in format",
1238 i = strlen (flag_chars);
1239 flag_chars[i++] = *format_chars++;
1242 /* "If the space and + flags both appear,
1243 the space flag will be ignored." */
1244 if (index (flag_chars, ' ') != 0
1245 && index (flag_chars, '+') != 0)
1246 warning ("use of both ` ' and `+' flags in format");
1247 /* "If the 0 and - flags both appear,
1248 the 0 flag will be ignored." */
1249 if (index (flag_chars, '0') != 0
1250 && index (flag_chars, '-') != 0)
1251 warning ("use of both `0' and `-' flags in format");
1252 if (*format_chars == '*')
1255 /* "...a field width...may be indicated by an asterisk.
1256 In this case, an int argument supplies the field width..." */
1263 if (info->first_arg_num != 0)
1265 cur_param = TREE_VALUE (params);
1266 params = TREE_CHAIN (params);
1268 /* size_t is generally not valid here.
1269 It will work on most machines, because size_t and int
1270 have the same mode. But might as well warn anyway,
1271 since it will fail on other machines. */
1272 if ((TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1273 != integer_type_node)
1275 (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1276 != unsigned_type_node))
1279 "field width is not type int (arg %d)",
1287 while (isdigit (*format_chars))
1293 if (*format_chars == '.')
1297 if (*format_chars != '*' && !isdigit (*format_chars))
1298 warning ("`.' not followed by `*' or digit in format");
1299 /* "...a...precision...may be indicated by an asterisk.
1300 In this case, an int argument supplies the...precision." */
1301 if (*format_chars == '*')
1303 if (info->first_arg_num != 0)
1311 cur_param = TREE_VALUE (params);
1312 params = TREE_CHAIN (params);
1314 if (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1315 != integer_type_node)
1318 "field width is not type int (arg %d)",
1326 while (isdigit (*format_chars))
1331 if (*format_chars == 'h' || *format_chars == 'l')
1332 length_char = *format_chars++;
1333 else if (*format_chars == 'q' || *format_chars == 'L')
1335 length_char = *format_chars++;
1337 pedwarn ("ANSI C does not support the `%c' length modifier",
1342 if (length_char == 'l' && *format_chars == 'l')
1344 length_char = 'q', format_chars++;
1346 pedwarn ("ANSI C does not support the `ll' length modifier");
1349 if (*format_chars == 'a')
1354 if (suppressed && length_char != 0)
1357 "use of `*' and `%c' together in format",
1361 format_char = *format_chars;
1362 if (format_char == 0 || format_char == '%')
1364 warning ("conversion lacks type at end of format");
1368 fci = info->is_scan ? scan_char_table : print_char_table;
1369 while (fci->format_chars != 0
1370 && index (fci->format_chars, format_char) == 0)
1372 if (fci->format_chars == 0)
1374 if (format_char >= 040 && format_char < 0177)
1376 "unknown conversion type character `%c' in format",
1380 "unknown conversion type character 0x%x in format",
1385 if (wide && index (fci->flag_chars, 'w') == 0)
1387 sprintf (message, "width used with `%c' format",
1391 if (precise && index (fci->flag_chars, 'p') == 0)
1393 sprintf (message, "precision used with `%c' format",
1397 if (aflag && index (fci->flag_chars, 'a') == 0)
1399 sprintf (message, "`a' flag used with `%c' format",
1403 if (info->is_scan && format_char == '[')
1405 /* Skip over scan set, in case it happens to have '%' in it. */
1406 if (*format_chars == '^')
1408 /* Find closing bracket; if one is hit immediately, then
1409 it's part of the scan set rather than a terminator. */
1410 if (*format_chars == ']')
1412 while (*format_chars && *format_chars != ']')
1414 if (*format_chars != ']')
1415 /* The end of the format string was reached. */
1416 warning ("no closing `]' for `%%[' format");
1420 if (index (fci->flag_chars, '*') == 0)
1423 "suppression of `%c' conversion in format",
1429 for (i = 0; flag_chars[i] != 0; ++i)
1431 if (index (fci->flag_chars, flag_chars[i]) == 0)
1433 sprintf (message, "flag `%c' used with type `%c'",
1434 flag_chars[i], format_char);
1438 if (precise && index (flag_chars, '0') != 0
1439 && (format_char == 'd' || format_char == 'i'
1440 || format_char == 'o' || format_char == 'u'
1441 || format_char == 'x' || format_char == 'x'))
1444 "`0' flag ignored with precision specifier and `%c' format",
1448 switch (length_char)
1450 default: wanted_type = fci->nolen ? *(fci->nolen) : 0; break;
1451 case 'h': wanted_type = fci->hlen ? *(fci->hlen) : 0; break;
1452 case 'l': wanted_type = fci->llen ? *(fci->llen) : 0; break;
1453 case 'q': wanted_type = fci->qlen ? *(fci->qlen) : 0; break;
1454 case 'L': wanted_type = fci->bigllen ? *(fci->bigllen) : 0; break;
1456 if (wanted_type == 0)
1459 "use of `%c' length character with `%c' type character",
1460 length_char, format_char);
1465 ** XXX -- should kvetch about stuff such as
1469 ** scanf ("%d", &i);
1473 /* Finally. . .check type of argument against desired type! */
1474 if (info->first_arg_num == 0)
1476 if (fci->pointer_count == 0 && wanted_type == void_type_node)
1477 /* This specifier takes no argument. */
1484 cur_param = TREE_VALUE (params);
1485 params = TREE_CHAIN (params);
1487 cur_type = TREE_TYPE (cur_param);
1489 /* Check the types of any additional pointer arguments
1490 that precede the "real" argument. */
1491 for (i = 0; i < fci->pointer_count; ++i)
1493 if (TREE_CODE (cur_type) == POINTER_TYPE)
1495 cur_type = TREE_TYPE (cur_type);
1498 if (TREE_CODE (cur_type) != ERROR_MARK)
1501 "format argument is not a %s (arg %d)",
1502 ((fci->pointer_count == 1) ? "pointer" : "pointer to a pointer"),
1509 /* Check the type of the "real" argument, if there's a type we want. */
1510 if (i == fci->pointer_count && wanted_type != 0
1511 && TREE_CODE (cur_type) != ERROR_MARK
1512 && wanted_type != TYPE_MAIN_VARIANT (cur_type)
1513 /* If we want `void *', allow any pointer type.
1514 (Anything else would already have got a warning.) */
1515 && ! (wanted_type == void_type_node
1516 && fci->pointer_count > 0)
1517 /* Don't warn about differences merely in signedness. */
1518 && !(TREE_CODE (wanted_type) == INTEGER_TYPE
1519 && TREE_CODE (TYPE_MAIN_VARIANT (cur_type)) == INTEGER_TYPE
1520 && (TREE_UNSIGNED (wanted_type)
1521 ? wanted_type == (cur_type = unsigned_type (cur_type))
1522 : wanted_type == (cur_type = signed_type (cur_type))))
1523 /* Likewise, "signed char", "unsigned char" and "char" are
1524 equivalent but the above test won't consider them equivalent. */
1525 && ! (wanted_type == char_type_node
1526 && (TYPE_MAIN_VARIANT (cur_type) == signed_char_type_node
1527 || TYPE_MAIN_VARIANT (cur_type) == unsigned_char_type_node)))
1529 register char *this;
1530 register char *that;
1532 this = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (wanted_type)));
1534 if (TREE_CODE (cur_type) != ERROR_MARK
1535 && TYPE_NAME (cur_type) != 0
1536 && TREE_CODE (cur_type) != INTEGER_TYPE
1537 && !(TREE_CODE (cur_type) == POINTER_TYPE
1538 && TREE_CODE (TREE_TYPE (cur_type)) == INTEGER_TYPE))
1540 if (TREE_CODE (TYPE_NAME (cur_type)) == TYPE_DECL
1541 && DECL_NAME (TYPE_NAME (cur_type)) != 0)
1542 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type)));
1544 that = IDENTIFIER_POINTER (TYPE_NAME (cur_type));
1547 /* A nameless type can't possibly match what the format wants.
1548 So there will be a warning for it.
1549 Make up a string to describe vaguely what it is. */
1552 if (TREE_CODE (cur_type) == POINTER_TYPE)
1555 that = "different type";
1558 /* Make the warning better in case of mismatch of int vs long. */
1559 if (TREE_CODE (cur_type) == INTEGER_TYPE
1560 && TREE_CODE (wanted_type) == INTEGER_TYPE
1561 && TYPE_PRECISION (cur_type) == TYPE_PRECISION (wanted_type)
1562 && TYPE_NAME (cur_type) != 0
1563 && TREE_CODE (TYPE_NAME (cur_type)) == TYPE_DECL)
1564 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type)));
1566 if (strcmp (this, that) != 0)
1568 sprintf (message, "%s format, %s arg (arg %d)",
1569 this, that, arg_num);
1576 /* Print a warning if a constant expression had overflow in folding.
1577 Invoke this function on every expression that the language
1578 requires to be a constant expression.
1579 Note the ANSI C standard says it is erroneous for a
1580 constant expression to overflow. */
1583 constant_expression_warning (value)
1586 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1587 || TREE_CODE (value) == COMPLEX_CST)
1588 && TREE_CONSTANT_OVERFLOW (value) && pedantic)
1589 pedwarn ("overflow in constant expression");
1592 /* Print a warning if an expression had overflow in folding.
1593 Invoke this function on every expression that
1594 (1) appears in the source code, and
1595 (2) might be a constant expression that overflowed, and
1596 (3) is not already checked by convert_and_check;
1597 however, do not invoke this function on operands of explicit casts. */
1600 overflow_warning (value)
1603 if ((TREE_CODE (value) == INTEGER_CST
1604 || (TREE_CODE (value) == COMPLEX_CST
1605 && TREE_CODE (TREE_REALPART (value)) == INTEGER_CST))
1606 && TREE_OVERFLOW (value))
1608 TREE_OVERFLOW (value) = 0;
1609 if (skip_evaluation == 0)
1610 warning ("integer overflow in expression");
1612 else if ((TREE_CODE (value) == REAL_CST
1613 || (TREE_CODE (value) == COMPLEX_CST
1614 && TREE_CODE (TREE_REALPART (value)) == REAL_CST))
1615 && TREE_OVERFLOW (value))
1617 TREE_OVERFLOW (value) = 0;
1618 if (skip_evaluation == 0)
1619 warning ("floating point overflow in expression");
1623 /* Print a warning if a large constant is truncated to unsigned,
1624 or if -Wconversion is used and a constant < 0 is converted to unsigned.
1625 Invoke this function on every expression that might be implicitly
1626 converted to an unsigned type. */
1629 unsigned_conversion_warning (result, operand)
1630 tree result, operand;
1632 if (TREE_CODE (operand) == INTEGER_CST
1633 && TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
1634 && TREE_UNSIGNED (TREE_TYPE (result))
1635 && skip_evaluation == 0
1636 && !int_fits_type_p (operand, TREE_TYPE (result)))
1638 if (!int_fits_type_p (operand, signed_type (TREE_TYPE (result))))
1639 /* This detects cases like converting -129 or 256 to unsigned char. */
1640 warning ("large integer implicitly truncated to unsigned type");
1641 else if (warn_conversion)
1642 warning ("negative integer implicitly converted to unsigned type");
1646 /* Convert EXPR to TYPE, warning about conversion problems with constants.
1647 Invoke this function on every expression that is converted implicitly,
1648 i.e. because of language rules and not because of an explicit cast. */
1651 convert_and_check (type, expr)
1654 tree t = convert (type, expr);
1655 if (TREE_CODE (t) == INTEGER_CST)
1657 if (TREE_OVERFLOW (t))
1659 TREE_OVERFLOW (t) = 0;
1661 /* Do not diagnose overflow in a constant expression merely
1662 because a conversion overflowed. */
1663 TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (expr);
1665 /* No warning for converting 0x80000000 to int. */
1666 if (!(TREE_UNSIGNED (type) < TREE_UNSIGNED (TREE_TYPE (expr))
1667 && TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
1668 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (expr))))
1669 /* If EXPR fits in the unsigned version of TYPE,
1670 don't warn unless pedantic. */
1672 || TREE_UNSIGNED (type)
1673 || ! int_fits_type_p (expr, unsigned_type (type)))
1674 && skip_evaluation == 0)
1675 warning ("overflow in implicit constant conversion");
1678 unsigned_conversion_warning (t, expr);
1684 c_expand_expr_stmt (expr)
1687 /* Do default conversion if safe and possibly important,
1688 in case within ({...}). */
1689 if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE && lvalue_p (expr))
1690 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
1691 expr = default_conversion (expr);
1693 if (TREE_TYPE (expr) != error_mark_node
1694 && TYPE_SIZE (TREE_TYPE (expr)) == 0
1695 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
1696 error ("expression statement has incomplete type");
1698 expand_expr_stmt (expr);
1701 /* Validate the expression after `case' and apply default promotions. */
1704 check_case_value (value)
1707 if (value == NULL_TREE)
1710 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1711 STRIP_TYPE_NOPS (value);
1713 if (TREE_CODE (value) != INTEGER_CST
1714 && value != error_mark_node)
1716 error ("case label does not reduce to an integer constant");
1717 value = error_mark_node;
1720 /* Promote char or short to int. */
1721 value = default_conversion (value);
1723 constant_expression_warning (value);
1728 /* Return an integer type with BITS bits of precision,
1729 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
1732 type_for_size (bits, unsignedp)
1736 if (bits == TYPE_PRECISION (integer_type_node))
1737 return unsignedp ? unsigned_type_node : integer_type_node;
1739 if (bits == TYPE_PRECISION (signed_char_type_node))
1740 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
1742 if (bits == TYPE_PRECISION (short_integer_type_node))
1743 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
1745 if (bits == TYPE_PRECISION (long_integer_type_node))
1746 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
1748 if (bits == TYPE_PRECISION (long_long_integer_type_node))
1749 return (unsignedp ? long_long_unsigned_type_node
1750 : long_long_integer_type_node);
1752 if (bits <= TYPE_PRECISION (intQI_type_node))
1753 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
1755 if (bits <= TYPE_PRECISION (intHI_type_node))
1756 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
1758 if (bits <= TYPE_PRECISION (intSI_type_node))
1759 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
1761 if (bits <= TYPE_PRECISION (intDI_type_node))
1762 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
1767 /* Return a data type that has machine mode MODE.
1768 If the mode is an integer,
1769 then UNSIGNEDP selects between signed and unsigned types. */
1772 type_for_mode (mode, unsignedp)
1773 enum machine_mode mode;
1776 if (mode == TYPE_MODE (integer_type_node))
1777 return unsignedp ? unsigned_type_node : integer_type_node;
1779 if (mode == TYPE_MODE (signed_char_type_node))
1780 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
1782 if (mode == TYPE_MODE (short_integer_type_node))
1783 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
1785 if (mode == TYPE_MODE (long_integer_type_node))
1786 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
1788 if (mode == TYPE_MODE (long_long_integer_type_node))
1789 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
1791 if (mode == TYPE_MODE (intQI_type_node))
1792 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
1794 if (mode == TYPE_MODE (intHI_type_node))
1795 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
1797 if (mode == TYPE_MODE (intSI_type_node))
1798 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
1800 if (mode == TYPE_MODE (intDI_type_node))
1801 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
1803 if (mode == TYPE_MODE (float_type_node))
1804 return float_type_node;
1806 if (mode == TYPE_MODE (double_type_node))
1807 return double_type_node;
1809 if (mode == TYPE_MODE (long_double_type_node))
1810 return long_double_type_node;
1812 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
1813 return build_pointer_type (char_type_node);
1815 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
1816 return build_pointer_type (integer_type_node);
1821 /* Return the minimum number of bits needed to represent VALUE in a
1822 signed or unsigned type, UNSIGNEDP says which. */
1825 min_precision (value, unsignedp)
1831 /* If the value is negative, compute its negative minus 1. The latter
1832 adjustment is because the absolute value of the largest negative value
1833 is one larger than the largest positive value. This is equivalent to
1834 a bit-wise negation, so use that operation instead. */
1836 if (tree_int_cst_sgn (value) < 0)
1837 value = fold (build1 (BIT_NOT_EXPR, TREE_TYPE (value), value));
1839 /* Return the number of bits needed, taking into account the fact
1840 that we need one more bit for a signed than unsigned type. */
1842 if (integer_zerop (value))
1844 else if (TREE_INT_CST_HIGH (value) != 0)
1845 log = HOST_BITS_PER_WIDE_INT + floor_log2 (TREE_INT_CST_HIGH (value));
1847 log = floor_log2 (TREE_INT_CST_LOW (value));
1849 return log + 1 + ! unsignedp;
1852 /* Print an error message for invalid operands to arith operation CODE.
1853 NOP_EXPR is used as a special case (see truthvalue_conversion). */
1856 binary_op_error (code)
1857 enum tree_code code;
1859 register char *opname = "unknown";
1864 error ("invalid truth-value expression");
1868 opname = "+"; break;
1870 opname = "-"; break;
1872 opname = "*"; break;
1874 opname = "max"; break;
1876 opname = "min"; break;
1878 opname = "=="; break;
1880 opname = "!="; break;
1882 opname = "<="; break;
1884 opname = ">="; break;
1886 opname = "<"; break;
1888 opname = ">"; break;
1890 opname = "<<"; break;
1892 opname = ">>"; break;
1893 case TRUNC_MOD_EXPR:
1894 case FLOOR_MOD_EXPR:
1895 opname = "%"; break;
1896 case TRUNC_DIV_EXPR:
1897 case FLOOR_DIV_EXPR:
1898 opname = "/"; break;
1900 opname = "&"; break;
1902 opname = "|"; break;
1903 case TRUTH_ANDIF_EXPR:
1904 opname = "&&"; break;
1905 case TRUTH_ORIF_EXPR:
1906 opname = "||"; break;
1908 opname = "^"; break;
1911 opname = "rotate"; break;
1913 error ("invalid operands to binary %s", opname);
1916 /* Subroutine of build_binary_op, used for comparison operations.
1917 See if the operands have both been converted from subword integer types
1918 and, if so, perhaps change them both back to their original type.
1919 This function is also responsible for converting the two operands
1920 to the proper common type for comparison.
1922 The arguments of this function are all pointers to local variables
1923 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
1924 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
1926 If this function returns nonzero, it means that the comparison has
1927 a constant value. What this function returns is an expression for
1931 shorten_compare (op0_ptr, op1_ptr, restype_ptr, rescode_ptr)
1932 tree *op0_ptr, *op1_ptr;
1934 enum tree_code *rescode_ptr;
1937 tree op0 = *op0_ptr;
1938 tree op1 = *op1_ptr;
1939 int unsignedp0, unsignedp1;
1941 tree primop0, primop1;
1942 enum tree_code code = *rescode_ptr;
1944 /* Throw away any conversions to wider types
1945 already present in the operands. */
1947 primop0 = get_narrower (op0, &unsignedp0);
1948 primop1 = get_narrower (op1, &unsignedp1);
1950 /* Handle the case that OP0 does not *contain* a conversion
1951 but it *requires* conversion to FINAL_TYPE. */
1953 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
1954 unsignedp0 = TREE_UNSIGNED (TREE_TYPE (op0));
1955 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
1956 unsignedp1 = TREE_UNSIGNED (TREE_TYPE (op1));
1958 /* If one of the operands must be floated, we cannot optimize. */
1959 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
1960 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
1962 /* If first arg is constant, swap the args (changing operation
1963 so value is preserved), for canonicalization. Don't do this if
1964 the second arg is 0. */
1966 if (TREE_CONSTANT (primop0)
1967 && ! integer_zerop (primop1) && ! real_zerop (primop1))
1969 register tree tem = primop0;
1970 register int temi = unsignedp0;
1978 unsignedp0 = unsignedp1;
1999 *rescode_ptr = code;
2002 /* If comparing an integer against a constant more bits wide,
2003 maybe we can deduce a value of 1 or 0 independent of the data.
2004 Or else truncate the constant now
2005 rather than extend the variable at run time.
2007 This is only interesting if the constant is the wider arg.
2008 Also, it is not safe if the constant is unsigned and the
2009 variable arg is signed, since in this case the variable
2010 would be sign-extended and then regarded as unsigned.
2011 Our technique fails in this case because the lowest/highest
2012 possible unsigned results don't follow naturally from the
2013 lowest/highest possible values of the variable operand.
2014 For just EQ_EXPR and NE_EXPR there is another technique that
2015 could be used: see if the constant can be faithfully represented
2016 in the other operand's type, by truncating it and reextending it
2017 and see if that preserves the constant's value. */
2019 if (!real1 && !real2
2020 && TREE_CODE (primop1) == INTEGER_CST
2021 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
2023 int min_gt, max_gt, min_lt, max_lt;
2024 tree maxval, minval;
2025 /* 1 if comparison is nominally unsigned. */
2026 int unsignedp = TREE_UNSIGNED (*restype_ptr);
2029 type = signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0));
2031 maxval = TYPE_MAX_VALUE (type);
2032 minval = TYPE_MIN_VALUE (type);
2034 if (unsignedp && !unsignedp0)
2035 *restype_ptr = signed_type (*restype_ptr);
2037 if (TREE_TYPE (primop1) != *restype_ptr)
2038 primop1 = convert (*restype_ptr, primop1);
2039 if (type != *restype_ptr)
2041 minval = convert (*restype_ptr, minval);
2042 maxval = convert (*restype_ptr, maxval);
2045 if (unsignedp && unsignedp0)
2047 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
2048 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
2049 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
2050 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
2054 min_gt = INT_CST_LT (primop1, minval);
2055 max_gt = INT_CST_LT (primop1, maxval);
2056 min_lt = INT_CST_LT (minval, primop1);
2057 max_lt = INT_CST_LT (maxval, primop1);
2061 /* This used to be a switch, but Genix compiler can't handle that. */
2062 if (code == NE_EXPR)
2064 if (max_lt || min_gt)
2065 val = boolean_true_node;
2067 else if (code == EQ_EXPR)
2069 if (max_lt || min_gt)
2070 val = boolean_false_node;
2072 else if (code == LT_EXPR)
2075 val = boolean_true_node;
2077 val = boolean_false_node;
2079 else if (code == GT_EXPR)
2082 val = boolean_true_node;
2084 val = boolean_false_node;
2086 else if (code == LE_EXPR)
2089 val = boolean_true_node;
2091 val = boolean_false_node;
2093 else if (code == GE_EXPR)
2096 val = boolean_true_node;
2098 val = boolean_false_node;
2101 /* If primop0 was sign-extended and unsigned comparison specd,
2102 we did a signed comparison above using the signed type bounds.
2103 But the comparison we output must be unsigned.
2105 Also, for inequalities, VAL is no good; but if the signed
2106 comparison had *any* fixed result, it follows that the
2107 unsigned comparison just tests the sign in reverse
2108 (positive values are LE, negative ones GE).
2109 So we can generate an unsigned comparison
2110 against an extreme value of the signed type. */
2112 if (unsignedp && !unsignedp0)
2119 primop1 = TYPE_MIN_VALUE (type);
2125 primop1 = TYPE_MAX_VALUE (type);
2129 type = unsigned_type (type);
2132 if (!max_gt && !unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
2134 /* This is the case of (char)x >?< 0x80, which people used to use
2135 expecting old C compilers to change the 0x80 into -0x80. */
2136 if (val == boolean_false_node)
2137 warning ("comparison is always 0 due to limited range of data type");
2138 if (val == boolean_true_node)
2139 warning ("comparison is always 1 due to limited range of data type");
2142 if (!min_lt && unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
2144 /* This is the case of (unsigned char)x >?< -1 or < 0. */
2145 if (val == boolean_false_node)
2146 warning ("comparison is always 0 due to limited range of data type");
2147 if (val == boolean_true_node)
2148 warning ("comparison is always 1 due to limited range of data type");
2153 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2154 if (TREE_SIDE_EFFECTS (primop0))
2155 return build (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
2159 /* Value is not predetermined, but do the comparison
2160 in the type of the operand that is not constant.
2161 TYPE is already properly set. */
2163 else if (real1 && real2
2164 && (TYPE_PRECISION (TREE_TYPE (primop0))
2165 == TYPE_PRECISION (TREE_TYPE (primop1))))
2166 type = TREE_TYPE (primop0);
2168 /* If args' natural types are both narrower than nominal type
2169 and both extend in the same manner, compare them
2170 in the type of the wider arg.
2171 Otherwise must actually extend both to the nominal
2172 common type lest different ways of extending
2174 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
2176 else if (unsignedp0 == unsignedp1 && real1 == real2
2177 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
2178 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
2180 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
2181 type = signed_or_unsigned_type (unsignedp0
2182 || TREE_UNSIGNED (*restype_ptr),
2184 /* Make sure shorter operand is extended the right way
2185 to match the longer operand. */
2186 primop0 = convert (signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0)),
2188 primop1 = convert (signed_or_unsigned_type (unsignedp1, TREE_TYPE (primop1)),
2193 /* Here we must do the comparison on the nominal type
2194 using the args exactly as we received them. */
2195 type = *restype_ptr;
2199 if (!real1 && !real2 && integer_zerop (primop1)
2200 && TREE_UNSIGNED (*restype_ptr))
2206 /* All unsigned values are >= 0, so we warn if extra warnings
2207 are requested. However, if OP0 is a constant that is
2208 >= 0, the signedness of the comparison isn't an issue,
2209 so suppress the warning. */
2211 && ! (TREE_CODE (primop0) == INTEGER_CST
2212 && ! TREE_OVERFLOW (convert (signed_type (type),
2214 warning ("unsigned value >= 0 is always 1");
2215 value = boolean_true_node;
2220 && ! (TREE_CODE (primop0) == INTEGER_CST
2221 && ! TREE_OVERFLOW (convert (signed_type (type),
2223 warning ("unsigned value < 0 is always 0");
2224 value = boolean_false_node;
2229 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2230 if (TREE_SIDE_EFFECTS (primop0))
2231 return build (COMPOUND_EXPR, TREE_TYPE (value),
2238 *op0_ptr = convert (type, primop0);
2239 *op1_ptr = convert (type, primop1);
2241 *restype_ptr = boolean_type_node;
2246 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
2247 or validate its data type for an `if' or `while' statement or ?..: exp.
2249 This preparation consists of taking the ordinary
2250 representation of an expression expr and producing a valid tree
2251 boolean expression describing whether expr is nonzero. We could
2252 simply always do build_binary_op (NE_EXPR, expr, boolean_false_node, 1),
2253 but we optimize comparisons, &&, ||, and !.
2255 The resulting type should always be `boolean_type_node'. */
2258 truthvalue_conversion (expr)
2261 if (TREE_CODE (expr) == ERROR_MARK)
2264 #if 0 /* This appears to be wrong for C++. */
2265 /* These really should return error_mark_node after 2.4 is stable.
2266 But not all callers handle ERROR_MARK properly. */
2267 switch (TREE_CODE (TREE_TYPE (expr)))
2270 error ("struct type value used where scalar is required");
2271 return boolean_false_node;
2274 error ("union type value used where scalar is required");
2275 return boolean_false_node;
2278 error ("array type value used where scalar is required");
2279 return boolean_false_node;
2286 switch (TREE_CODE (expr))
2288 /* It is simpler and generates better code to have only TRUTH_*_EXPR
2289 or comparison expressions as truth values at this level. */
2292 /* A one-bit unsigned bit-field is already acceptable. */
2293 if (1 == TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (expr, 1)))
2294 && TREE_UNSIGNED (TREE_OPERAND (expr, 1)))
2300 /* It is simpler and generates better code to have only TRUTH_*_EXPR
2301 or comparison expressions as truth values at this level. */
2303 if (integer_zerop (TREE_OPERAND (expr, 1)))
2304 return build_unary_op (TRUTH_NOT_EXPR, TREE_OPERAND (expr, 0), 0);
2306 case NE_EXPR: case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
2307 case TRUTH_ANDIF_EXPR:
2308 case TRUTH_ORIF_EXPR:
2309 case TRUTH_AND_EXPR:
2311 case TRUTH_XOR_EXPR:
2312 case TRUTH_NOT_EXPR:
2313 TREE_TYPE (expr) = boolean_type_node;
2320 return integer_zerop (expr) ? boolean_false_node : boolean_true_node;
2323 return real_zerop (expr) ? boolean_false_node : boolean_true_node;
2326 /* If we are taking the address of a external decl, it might be zero
2327 if it is weak, so we cannot optimize. */
2328 if (TREE_CODE_CLASS (TREE_CODE (TREE_OPERAND (expr, 0))) == 'd'
2329 && DECL_EXTERNAL (TREE_OPERAND (expr, 0)))
2332 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 0)))
2333 return build (COMPOUND_EXPR, boolean_type_node,
2334 TREE_OPERAND (expr, 0), boolean_true_node);
2336 return boolean_true_node;
2339 return build_binary_op ((TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
2340 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
2341 truthvalue_conversion (TREE_OPERAND (expr, 0)),
2342 truthvalue_conversion (TREE_OPERAND (expr, 1)),
2349 /* These don't change whether an object is non-zero or zero. */
2350 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2354 /* These don't change whether an object is zero or non-zero, but
2355 we can't ignore them if their second arg has side-effects. */
2356 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
2357 return build (COMPOUND_EXPR, boolean_type_node, TREE_OPERAND (expr, 1),
2358 truthvalue_conversion (TREE_OPERAND (expr, 0)));
2360 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2363 /* Distribute the conversion into the arms of a COND_EXPR. */
2364 return fold (build (COND_EXPR, boolean_type_node, TREE_OPERAND (expr, 0),
2365 truthvalue_conversion (TREE_OPERAND (expr, 1)),
2366 truthvalue_conversion (TREE_OPERAND (expr, 2))));
2369 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
2370 since that affects how `default_conversion' will behave. */
2371 if (TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE
2372 || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
2374 /* fall through... */
2376 /* If this is widening the argument, we can ignore it. */
2377 if (TYPE_PRECISION (TREE_TYPE (expr))
2378 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
2379 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2383 /* With IEEE arithmetic, x - x may not equal 0, so we can't optimize
2385 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
2386 && TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE)
2388 /* fall through... */
2390 /* This and MINUS_EXPR can be changed into a comparison of the
2392 if (TREE_TYPE (TREE_OPERAND (expr, 0))
2393 == TREE_TYPE (TREE_OPERAND (expr, 1)))
2394 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2395 TREE_OPERAND (expr, 1), 1);
2396 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2397 fold (build1 (NOP_EXPR,
2398 TREE_TYPE (TREE_OPERAND (expr, 0)),
2399 TREE_OPERAND (expr, 1))), 1);
2402 if (integer_onep (TREE_OPERAND (expr, 1))
2403 && TREE_TYPE (expr) != boolean_type_node)
2404 /* Using convert here would cause infinite recursion. */
2405 return build1 (NOP_EXPR, boolean_type_node, expr);
2409 if (warn_parentheses && C_EXP_ORIGINAL_CODE (expr) == MODIFY_EXPR)
2410 warning ("suggest parentheses around assignment used as truth value");
2414 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
2415 return (build_binary_op
2416 ((TREE_SIDE_EFFECTS (expr)
2417 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
2418 truthvalue_conversion (build_unary_op (REALPART_EXPR, expr, 0)),
2419 truthvalue_conversion (build_unary_op (IMAGPART_EXPR, expr, 0)),
2422 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
2425 /* Read the rest of a #-directive from input stream FINPUT.
2426 In normal use, the directive name and the white space after it
2427 have already been read, so they won't be included in the result.
2428 We allow for the fact that the directive line may contain
2429 a newline embedded within a character or string literal which forms
2430 a part of the directive.
2432 The value is a string in a reusable buffer. It remains valid
2433 only until the next time this function is called.
2435 The terminating character ('\n' or EOF) is left in FINPUT for the
2436 caller to re-read. */
2439 get_directive_line (finput)
2440 register FILE *finput;
2442 static char *directive_buffer = NULL;
2443 static unsigned buffer_length = 0;
2445 register char *buffer_limit;
2446 register int looking_for = 0;
2447 register int char_escaped = 0;
2449 if (buffer_length == 0)
2451 directive_buffer = (char *)xmalloc (128);
2452 buffer_length = 128;
2455 buffer_limit = &directive_buffer[buffer_length];
2457 for (p = directive_buffer; ; )
2461 /* Make buffer bigger if it is full. */
2462 if (p >= buffer_limit)
2464 register unsigned bytes_used = (p - directive_buffer);
2468 = (char *)xrealloc (directive_buffer, buffer_length);
2469 p = &directive_buffer[bytes_used];
2470 buffer_limit = &directive_buffer[buffer_length];
2475 /* Discard initial whitespace. */
2476 if ((c == ' ' || c == '\t') && p == directive_buffer)
2479 /* Detect the end of the directive. */
2480 if (looking_for == 0
2481 && (c == '\n' || c == EOF))
2490 return directive_buffer;
2492 /* Handle string and character constant syntax. */
2495 if (looking_for == c && !char_escaped)
2496 looking_for = 0; /* Found terminator... stop looking. */
2499 if (c == '\'' || c == '"')
2500 looking_for = c; /* Don't stop buffering until we see another
2501 another one of these (or an EOF). */
2503 /* Handle backslash. */
2504 char_escaped = (c == '\\' && ! char_escaped);
2508 /* Make a variant type in the proper way for C/C++, propagating qualifiers
2509 down to the element type of an array. */
2512 c_build_type_variant (type, constp, volatilep)
2514 int constp, volatilep;
2516 if (TREE_CODE (type) == ARRAY_TYPE)
2518 tree real_main_variant = TYPE_MAIN_VARIANT (type);
2520 push_obstacks (TYPE_OBSTACK (real_main_variant),
2521 TYPE_OBSTACK (real_main_variant));
2522 type = build_array_type (c_build_type_variant (TREE_TYPE (type),
2524 TYPE_DOMAIN (type));
2526 /* TYPE must be on same obstack as REAL_MAIN_VARIANT. If not,
2527 make a copy. (TYPE might have come from the hash table and
2528 REAL_MAIN_VARIANT might be in some function's obstack.) */
2530 if (TYPE_OBSTACK (type) != TYPE_OBSTACK (real_main_variant))
2532 type = copy_node (type);
2533 TYPE_POINTER_TO (type) = TYPE_REFERENCE_TO (type) = 0;
2536 TYPE_MAIN_VARIANT (type) = real_main_variant;
2539 return build_type_variant (type, constp, volatilep);