Do not do src->dest copy if register would not be allocated a normal register
[official-gcc.git] / gcc / c-common.c
blob9983cdbc5efad5cf163d8d0f5028021afc1ce6fb
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)
9 any later version.
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. */
21 #include "config.h"
22 #include "system.h"
23 #include "tree.h"
24 #include "c-lex.h"
25 #include "c-tree.h"
26 #include "flags.h"
27 #include "obstack.h"
28 #include "toplev.h"
29 #include "output.h"
31 #if USE_CPPLIB
32 #include "cpplib.h"
33 cpp_reader parse_in;
34 cpp_options parse_options;
35 static enum cpp_token cpp_token;
36 #endif
38 #ifndef WCHAR_TYPE_SIZE
39 #ifdef INT_TYPE_SIZE
40 #define WCHAR_TYPE_SIZE INT_TYPE_SIZE
41 #else
42 #define WCHAR_TYPE_SIZE BITS_PER_WORD
43 #endif
44 #endif
46 extern struct obstack permanent_obstack;
48 /* Nonzero means the expression being parsed will never be evaluated.
49 This is a count, since unevaluated expressions can nest. */
50 int skip_evaluation;
52 enum attrs {A_PACKED, A_NOCOMMON, A_COMMON, A_NORETURN, A_CONST, A_T_UNION,
53 A_CONSTRUCTOR, A_DESTRUCTOR, A_MODE, A_SECTION, A_ALIGNED,
54 A_UNUSED, A_FORMAT, A_FORMAT_ARG, A_WEAK, A_ALIAS};
56 enum format_type { printf_format_type, scanf_format_type,
57 strftime_format_type };
59 static void declare_hidden_char_array PROTO((char *, char *));
60 static void add_attribute PROTO((enum attrs, char *,
61 int, int, int));
62 static void init_attributes PROTO((void));
63 static void record_function_format PROTO((tree, tree, enum format_type,
64 int, int));
65 static void record_international_format PROTO((tree, tree, int));
67 /* Keep a stack of if statements. We record the number of compound
68 statements seen up to the if keyword, as well as the line number
69 and file of the if. If a potentially ambiguous else is seen, that
70 fact is recorded; the warning is issued when we can be sure that
71 the enclosing if statement does not have an else branch. */
72 typedef struct
74 int compstmt_count;
75 int line;
76 char *file;
77 int needs_warning;
78 } if_elt;
80 static if_elt *if_stack;
82 /* Amount of space in the if statement stack. */
83 static int if_stack_space = 0;
85 /* Stack pointer. */
86 static int if_stack_pointer = 0;
88 /* Generate RTL for the start of an if-then, and record the start of it
89 for ambiguous else detection. */
91 void
92 c_expand_start_cond (cond, exitflag, compstmt_count)
93 tree cond;
94 int exitflag;
95 int compstmt_count;
97 /* Make sure there is enough space on the stack. */
98 if (if_stack_space == 0)
100 if_stack_space = 10;
101 if_stack = (if_elt *)xmalloc (10 * sizeof (if_elt));
103 else if (if_stack_space == if_stack_pointer)
105 if_stack_space += 10;
106 if_stack = (if_elt *)xrealloc (if_stack, if_stack_space * sizeof (if_elt));
109 /* Record this if statement. */
110 if_stack[if_stack_pointer].compstmt_count = compstmt_count;
111 if_stack[if_stack_pointer].file = input_filename;
112 if_stack[if_stack_pointer].line = lineno;
113 if_stack[if_stack_pointer].needs_warning = 0;
114 if_stack_pointer++;
116 expand_start_cond (cond, exitflag);
119 /* Generate RTL for the end of an if-then. Optionally warn if a nested
120 if statement had an ambiguous else clause. */
122 void
123 c_expand_end_cond ()
125 if_stack_pointer--;
126 if (if_stack[if_stack_pointer].needs_warning)
127 warning_with_file_and_line (if_stack[if_stack_pointer].file,
128 if_stack[if_stack_pointer].line,
129 "suggest explicit braces to avoid ambiguous `else'");
130 expand_end_cond ();
133 /* Generate RTL between the then-clause and the else-clause
134 of an if-then-else. */
136 void
137 c_expand_start_else ()
139 /* An ambiguous else warning must be generated for the enclosing if
140 statement, unless we see an else branch for that one, too. */
141 if (warn_parentheses
142 && if_stack_pointer > 1
143 && (if_stack[if_stack_pointer - 1].compstmt_count
144 == if_stack[if_stack_pointer - 2].compstmt_count))
145 if_stack[if_stack_pointer - 2].needs_warning = 1;
147 /* Even if a nested if statement had an else branch, it can't be
148 ambiguous if this one also has an else. So don't warn in that
149 case. Also don't warn for any if statements nested in this else. */
150 if_stack[if_stack_pointer - 1].needs_warning = 0;
151 if_stack[if_stack_pointer - 1].compstmt_count--;
153 expand_start_else ();
156 /* Make bindings for __FUNCTION__ and __PRETTY_FUNCTION__. */
158 void
159 declare_function_name ()
161 char *name, *printable_name;
163 if (current_function_decl == NULL)
165 name = "";
166 printable_name = "top level";
168 else
170 /* Allow functions to be nameless (such as artificial ones). */
171 if (DECL_NAME (current_function_decl))
172 name = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
173 else
174 name = "";
175 printable_name = (*decl_printable_name) (current_function_decl, 2);
178 declare_hidden_char_array ("__FUNCTION__", name);
179 declare_hidden_char_array ("__PRETTY_FUNCTION__", printable_name);
182 static void
183 declare_hidden_char_array (name, value)
184 char *name, *value;
186 tree decl, type, init;
187 int vlen;
189 /* If the default size of char arrays isn't big enough for the name,
190 or if we want to give warnings for large objects, make a bigger one. */
191 vlen = strlen (value) + 1;
192 type = char_array_type_node;
193 if (TREE_INT_CST_LOW (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) < vlen
194 || warn_larger_than)
195 type = build_array_type (char_type_node,
196 build_index_type (build_int_2 (vlen, 0)));
197 push_obstacks_nochange ();
198 decl = build_decl (VAR_DECL, get_identifier (name), type);
199 TREE_STATIC (decl) = 1;
200 TREE_READONLY (decl) = 1;
201 TREE_ASM_WRITTEN (decl) = 1;
202 DECL_SOURCE_LINE (decl) = 0;
203 DECL_ARTIFICIAL (decl) = 1;
204 DECL_IN_SYSTEM_HEADER (decl) = 1;
205 DECL_IGNORED_P (decl) = 1;
206 init = build_string (vlen, value);
207 TREE_TYPE (init) = type;
208 DECL_INITIAL (decl) = init;
209 finish_decl (pushdecl (decl), init, NULL_TREE);
212 /* Given a chain of STRING_CST nodes,
213 concatenate them into one STRING_CST
214 and give it a suitable array-of-chars data type. */
216 tree
217 combine_strings (strings)
218 tree strings;
220 register tree value, t;
221 register int length = 1;
222 int wide_length = 0;
223 int wide_flag = 0;
224 int wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
225 int nchars;
227 if (TREE_CHAIN (strings))
229 /* More than one in the chain, so concatenate. */
230 register char *p, *q;
232 /* Don't include the \0 at the end of each substring,
233 except for the last one.
234 Count wide strings and ordinary strings separately. */
235 for (t = strings; t; t = TREE_CHAIN (t))
237 if (TREE_TYPE (t) == wchar_array_type_node)
239 wide_length += (TREE_STRING_LENGTH (t) - wchar_bytes);
240 wide_flag = 1;
242 else
243 length += (TREE_STRING_LENGTH (t) - 1);
246 /* If anything is wide, the non-wides will be converted,
247 which makes them take more space. */
248 if (wide_flag)
249 length = length * wchar_bytes + wide_length;
251 p = savealloc (length);
253 /* Copy the individual strings into the new combined string.
254 If the combined string is wide, convert the chars to ints
255 for any individual strings that are not wide. */
257 q = p;
258 for (t = strings; t; t = TREE_CHAIN (t))
260 int len = (TREE_STRING_LENGTH (t)
261 - ((TREE_TYPE (t) == wchar_array_type_node)
262 ? wchar_bytes : 1));
263 if ((TREE_TYPE (t) == wchar_array_type_node) == wide_flag)
265 bcopy (TREE_STRING_POINTER (t), q, len);
266 q += len;
268 else
270 int i;
271 for (i = 0; i < len; i++)
273 if (WCHAR_TYPE_SIZE == HOST_BITS_PER_SHORT)
274 ((short *) q)[i] = TREE_STRING_POINTER (t)[i];
275 else
276 ((int *) q)[i] = TREE_STRING_POINTER (t)[i];
278 q += len * wchar_bytes;
281 if (wide_flag)
283 int i;
284 for (i = 0; i < wchar_bytes; i++)
285 *q++ = 0;
287 else
288 *q = 0;
290 value = make_node (STRING_CST);
291 TREE_STRING_POINTER (value) = p;
292 TREE_STRING_LENGTH (value) = length;
293 TREE_CONSTANT (value) = 1;
295 else
297 value = strings;
298 length = TREE_STRING_LENGTH (value);
299 if (TREE_TYPE (value) == wchar_array_type_node)
300 wide_flag = 1;
303 /* Compute the number of elements, for the array type. */
304 nchars = wide_flag ? length / wchar_bytes : length;
306 /* Create the array type for the string constant.
307 -Wwrite-strings says make the string constant an array of const char
308 so that copying it to a non-const pointer will get a warning. */
309 if (warn_write_strings
310 && (! flag_traditional && ! flag_writable_strings))
312 tree elements
313 = build_type_variant (wide_flag ? wchar_type_node : char_type_node,
314 1, 0);
315 TREE_TYPE (value)
316 = build_array_type (elements,
317 build_index_type (build_int_2 (nchars - 1, 0)));
319 else
320 TREE_TYPE (value)
321 = build_array_type (wide_flag ? wchar_type_node : char_type_node,
322 build_index_type (build_int_2 (nchars - 1, 0)));
323 TREE_CONSTANT (value) = 1;
324 TREE_STATIC (value) = 1;
325 return value;
328 /* To speed up processing of attributes, we maintain an array of
329 IDENTIFIER_NODES and the corresponding attribute types. */
331 /* Array to hold attribute information. */
333 static struct {enum attrs id; tree name; int min, max, decl_req;} attrtab[50];
335 static int attrtab_idx = 0;
337 /* Add an entry to the attribute table above. */
339 static void
340 add_attribute (id, string, min_len, max_len, decl_req)
341 enum attrs id;
342 char *string;
343 int min_len, max_len;
344 int decl_req;
346 char buf[100];
348 attrtab[attrtab_idx].id = id;
349 attrtab[attrtab_idx].name = get_identifier (string);
350 attrtab[attrtab_idx].min = min_len;
351 attrtab[attrtab_idx].max = max_len;
352 attrtab[attrtab_idx++].decl_req = decl_req;
354 sprintf (buf, "__%s__", string);
356 attrtab[attrtab_idx].id = id;
357 attrtab[attrtab_idx].name = get_identifier (buf);
358 attrtab[attrtab_idx].min = min_len;
359 attrtab[attrtab_idx].max = max_len;
360 attrtab[attrtab_idx++].decl_req = decl_req;
363 /* Initialize attribute table. */
365 static void
366 init_attributes ()
368 add_attribute (A_PACKED, "packed", 0, 0, 0);
369 add_attribute (A_NOCOMMON, "nocommon", 0, 0, 1);
370 add_attribute (A_COMMON, "common", 0, 0, 1);
371 add_attribute (A_NORETURN, "noreturn", 0, 0, 1);
372 add_attribute (A_NORETURN, "volatile", 0, 0, 1);
373 add_attribute (A_UNUSED, "unused", 0, 0, 0);
374 add_attribute (A_CONST, "const", 0, 0, 1);
375 add_attribute (A_T_UNION, "transparent_union", 0, 0, 0);
376 add_attribute (A_CONSTRUCTOR, "constructor", 0, 0, 1);
377 add_attribute (A_DESTRUCTOR, "destructor", 0, 0, 1);
378 add_attribute (A_MODE, "mode", 1, 1, 1);
379 add_attribute (A_SECTION, "section", 1, 1, 1);
380 add_attribute (A_ALIGNED, "aligned", 0, 1, 0);
381 add_attribute (A_FORMAT, "format", 3, 3, 1);
382 add_attribute (A_FORMAT_ARG, "format_arg", 1, 1, 1);
383 add_attribute (A_WEAK, "weak", 0, 0, 1);
384 add_attribute (A_ALIAS, "alias", 1, 1, 1);
387 /* Process the attributes listed in ATTRIBUTES and PREFIX_ATTRIBUTES
388 and install them in NODE, which is either a DECL (including a TYPE_DECL)
389 or a TYPE. PREFIX_ATTRIBUTES can appear after the declaration specifiers
390 and declaration modifiers but before the declaration proper. */
392 void
393 decl_attributes (node, attributes, prefix_attributes)
394 tree node, attributes, prefix_attributes;
396 tree decl = 0, type = 0;
397 int is_type = 0;
398 tree a;
400 if (attrtab_idx == 0)
401 init_attributes ();
403 if (TREE_CODE_CLASS (TREE_CODE (node)) == 'd')
405 decl = node;
406 type = TREE_TYPE (decl);
407 is_type = TREE_CODE (node) == TYPE_DECL;
409 else if (TREE_CODE_CLASS (TREE_CODE (node)) == 't')
410 type = node, is_type = 1;
412 attributes = chainon (prefix_attributes, attributes);
414 for (a = attributes; a; a = TREE_CHAIN (a))
416 tree name = TREE_PURPOSE (a);
417 tree args = TREE_VALUE (a);
418 int i;
419 enum attrs id;
421 for (i = 0; i < attrtab_idx; i++)
422 if (attrtab[i].name == name)
423 break;
425 if (i == attrtab_idx)
427 if (! valid_machine_attribute (name, args, decl, type))
428 warning ("`%s' attribute directive ignored",
429 IDENTIFIER_POINTER (name));
430 else if (decl != 0)
431 type = TREE_TYPE (decl);
432 continue;
434 else if (attrtab[i].decl_req && decl == 0)
436 warning ("`%s' attribute does not apply to types",
437 IDENTIFIER_POINTER (name));
438 continue;
440 else if (list_length (args) < attrtab[i].min
441 || list_length (args) > attrtab[i].max)
443 error ("wrong number of arguments specified for `%s' attribute",
444 IDENTIFIER_POINTER (name));
445 continue;
448 id = attrtab[i].id;
449 switch (id)
451 case A_PACKED:
452 if (is_type)
453 TYPE_PACKED (type) = 1;
454 else if (TREE_CODE (decl) == FIELD_DECL)
455 DECL_PACKED (decl) = 1;
456 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
457 used for DECL_REGISTER. It wouldn't mean anything anyway. */
458 else
459 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
460 break;
462 case A_NOCOMMON:
463 if (TREE_CODE (decl) == VAR_DECL)
464 DECL_COMMON (decl) = 0;
465 else
466 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
467 break;
469 case A_COMMON:
470 if (TREE_CODE (decl) == VAR_DECL)
471 DECL_COMMON (decl) = 1;
472 else
473 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
474 break;
476 case A_NORETURN:
477 if (TREE_CODE (decl) == FUNCTION_DECL)
478 TREE_THIS_VOLATILE (decl) = 1;
479 else if (TREE_CODE (type) == POINTER_TYPE
480 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
481 TREE_TYPE (decl) = type
482 = build_pointer_type
483 (build_type_variant (TREE_TYPE (type),
484 TREE_READONLY (TREE_TYPE (type)), 1));
485 else
486 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
487 break;
489 case A_UNUSED:
490 if (is_type)
491 TREE_USED (type) = 1;
492 else if (TREE_CODE (decl) == PARM_DECL
493 || TREE_CODE (decl) == VAR_DECL
494 || TREE_CODE (decl) == FUNCTION_DECL)
495 TREE_USED (decl) = 1;
496 else
497 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
498 break;
500 case A_CONST:
501 if (TREE_CODE (decl) == FUNCTION_DECL)
502 TREE_READONLY (decl) = 1;
503 else if (TREE_CODE (type) == POINTER_TYPE
504 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
505 TREE_TYPE (decl) = type
506 = build_pointer_type
507 (build_type_variant (TREE_TYPE (type), 1,
508 TREE_THIS_VOLATILE (TREE_TYPE (type))));
509 else
510 warning ( "`%s' attribute ignored", IDENTIFIER_POINTER (name));
511 break;
513 case A_T_UNION:
514 if (is_type
515 && TREE_CODE (type) == UNION_TYPE
516 && (decl == 0
517 || (TYPE_FIELDS (type) != 0
518 && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))))
519 TYPE_TRANSPARENT_UNION (type) = 1;
520 else if (decl != 0 && TREE_CODE (decl) == PARM_DECL
521 && TREE_CODE (type) == UNION_TYPE
522 && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))
523 DECL_TRANSPARENT_UNION (decl) = 1;
524 else
525 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
526 break;
528 case A_CONSTRUCTOR:
529 if (TREE_CODE (decl) == FUNCTION_DECL
530 && TREE_CODE (type) == FUNCTION_TYPE
531 && decl_function_context (decl) == 0)
533 DECL_STATIC_CONSTRUCTOR (decl) = 1;
534 TREE_USED (decl) = 1;
536 else
537 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
538 break;
540 case A_DESTRUCTOR:
541 if (TREE_CODE (decl) == FUNCTION_DECL
542 && TREE_CODE (type) == FUNCTION_TYPE
543 && decl_function_context (decl) == 0)
545 DECL_STATIC_DESTRUCTOR (decl) = 1;
546 TREE_USED (decl) = 1;
548 else
549 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
550 break;
552 case A_MODE:
553 if (TREE_CODE (TREE_VALUE (args)) != IDENTIFIER_NODE)
554 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
555 else
557 int j;
558 char *p = IDENTIFIER_POINTER (TREE_VALUE (args));
559 int len = strlen (p);
560 enum machine_mode mode = VOIDmode;
561 tree typefm;
563 if (len > 4 && p[0] == '_' && p[1] == '_'
564 && p[len - 1] == '_' && p[len - 2] == '_')
566 char *newp = (char *) alloca (len - 1);
568 strcpy (newp, &p[2]);
569 newp[len - 4] = '\0';
570 p = newp;
573 /* Give this decl a type with the specified mode.
574 First check for the special modes. */
575 if (! strcmp (p, "byte"))
576 mode = byte_mode;
577 else if (!strcmp (p, "word"))
578 mode = word_mode;
579 else if (! strcmp (p, "pointer"))
580 mode = ptr_mode;
581 else
582 for (j = 0; j < NUM_MACHINE_MODES; j++)
583 if (!strcmp (p, GET_MODE_NAME (j)))
584 mode = (enum machine_mode) j;
586 if (mode == VOIDmode)
587 error ("unknown machine mode `%s'", p);
588 else if (0 == (typefm = type_for_mode (mode,
589 TREE_UNSIGNED (type))))
590 error ("no data type for mode `%s'", p);
591 else
593 TREE_TYPE (decl) = type = typefm;
594 DECL_SIZE (decl) = 0;
595 layout_decl (decl, 0);
598 break;
600 case A_SECTION:
601 #ifdef ASM_OUTPUT_SECTION_NAME
602 if ((TREE_CODE (decl) == FUNCTION_DECL
603 || TREE_CODE (decl) == VAR_DECL)
604 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
606 if (TREE_CODE (decl) == VAR_DECL
607 && current_function_decl != NULL_TREE
608 && ! TREE_STATIC (decl))
609 error_with_decl (decl,
610 "section attribute cannot be specified for local variables");
611 /* The decl may have already been given a section attribute from
612 a previous declaration. Ensure they match. */
613 else if (DECL_SECTION_NAME (decl) != NULL_TREE
614 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
615 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
616 error_with_decl (node,
617 "section of `%s' conflicts with previous declaration");
618 else
619 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
621 else
622 error_with_decl (node,
623 "section attribute not allowed for `%s'");
624 #else
625 error_with_decl (node,
626 "section attributes are not supported for this target");
627 #endif
628 break;
630 case A_ALIGNED:
632 tree align_expr
633 = (args ? TREE_VALUE (args)
634 : size_int (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
635 int align;
637 /* Strip any NOPs of any kind. */
638 while (TREE_CODE (align_expr) == NOP_EXPR
639 || TREE_CODE (align_expr) == CONVERT_EXPR
640 || TREE_CODE (align_expr) == NON_LVALUE_EXPR)
641 align_expr = TREE_OPERAND (align_expr, 0);
643 if (TREE_CODE (align_expr) != INTEGER_CST)
645 error ("requested alignment is not a constant");
646 continue;
649 align = TREE_INT_CST_LOW (align_expr) * BITS_PER_UNIT;
651 if (exact_log2 (align) == -1)
652 error ("requested alignment is not a power of 2");
653 else if (is_type)
654 TYPE_ALIGN (type) = align;
655 else if (TREE_CODE (decl) != VAR_DECL
656 && TREE_CODE (decl) != FIELD_DECL)
657 error_with_decl (decl,
658 "alignment may not be specified for `%s'");
659 else
660 DECL_ALIGN (decl) = align;
662 break;
664 case A_FORMAT:
666 tree format_type_id = TREE_VALUE (args);
667 tree format_num_expr = TREE_VALUE (TREE_CHAIN (args));
668 tree first_arg_num_expr
669 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)));
670 int format_num;
671 int first_arg_num;
672 enum format_type format_type;
673 tree argument;
674 int arg_num;
676 if (TREE_CODE (decl) != FUNCTION_DECL)
678 error_with_decl (decl,
679 "argument format specified for non-function `%s'");
680 continue;
683 if (TREE_CODE (format_type_id) != IDENTIFIER_NODE)
685 error ("unrecognized format specifier");
686 continue;
688 else
690 char *p = IDENTIFIER_POINTER (format_type_id);
692 if (!strcmp (p, "printf") || !strcmp (p, "__printf__"))
693 format_type = printf_format_type;
694 else if (!strcmp (p, "scanf") || !strcmp (p, "__scanf__"))
695 format_type = scanf_format_type;
696 else if (!strcmp (p, "strftime")
697 || !strcmp (p, "__strftime__"))
698 format_type = strftime_format_type;
699 else
701 error ("`%s' is an unrecognized format function type", p);
702 continue;
706 /* Strip any conversions from the string index and first arg number
707 and verify they are constants. */
708 while (TREE_CODE (format_num_expr) == NOP_EXPR
709 || TREE_CODE (format_num_expr) == CONVERT_EXPR
710 || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
711 format_num_expr = TREE_OPERAND (format_num_expr, 0);
713 while (TREE_CODE (first_arg_num_expr) == NOP_EXPR
714 || TREE_CODE (first_arg_num_expr) == CONVERT_EXPR
715 || TREE_CODE (first_arg_num_expr) == NON_LVALUE_EXPR)
716 first_arg_num_expr = TREE_OPERAND (first_arg_num_expr, 0);
718 if (TREE_CODE (format_num_expr) != INTEGER_CST
719 || TREE_CODE (first_arg_num_expr) != INTEGER_CST)
721 error ("format string has non-constant operand number");
722 continue;
725 format_num = TREE_INT_CST_LOW (format_num_expr);
726 first_arg_num = TREE_INT_CST_LOW (first_arg_num_expr);
727 if (first_arg_num != 0 && first_arg_num <= format_num)
729 error ("format string arg follows the args to be formatted");
730 continue;
733 /* If a parameter list is specified, verify that the format_num
734 argument is actually a string, in case the format attribute
735 is in error. */
736 argument = TYPE_ARG_TYPES (type);
737 if (argument)
739 for (arg_num = 1; ; ++arg_num)
741 if (argument == 0 || arg_num == format_num)
742 break;
743 argument = TREE_CHAIN (argument);
745 if (! argument
746 || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
747 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
748 != char_type_node))
750 error ("format string arg not a string type");
751 continue;
753 if (first_arg_num != 0)
755 /* Verify that first_arg_num points to the last arg,
756 the ... */
757 while (argument)
758 arg_num++, argument = TREE_CHAIN (argument);
759 if (arg_num != first_arg_num)
761 error ("args to be formatted is not ...");
762 continue;
767 record_function_format (DECL_NAME (decl),
768 DECL_ASSEMBLER_NAME (decl),
769 format_type, format_num, first_arg_num);
770 break;
773 case A_FORMAT_ARG:
775 tree format_num_expr = TREE_VALUE (args);
776 int format_num, arg_num;
777 tree argument;
779 if (TREE_CODE (decl) != FUNCTION_DECL)
781 error_with_decl (decl,
782 "argument format specified for non-function `%s'");
783 continue;
786 /* Strip any conversions from the first arg number and verify it
787 is a constant. */
788 while (TREE_CODE (format_num_expr) == NOP_EXPR
789 || TREE_CODE (format_num_expr) == CONVERT_EXPR
790 || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
791 format_num_expr = TREE_OPERAND (format_num_expr, 0);
793 if (TREE_CODE (format_num_expr) != INTEGER_CST)
795 error ("format string has non-constant operand number");
796 continue;
799 format_num = TREE_INT_CST_LOW (format_num_expr);
801 /* If a parameter list is specified, verify that the format_num
802 argument is actually a string, in case the format attribute
803 is in error. */
804 argument = TYPE_ARG_TYPES (type);
805 if (argument)
807 for (arg_num = 1; ; ++arg_num)
809 if (argument == 0 || arg_num == format_num)
810 break;
811 argument = TREE_CHAIN (argument);
813 if (! argument
814 || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
815 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
816 != char_type_node))
818 error ("format string arg not a string type");
819 continue;
823 if (TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) != POINTER_TYPE
824 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (TREE_TYPE (decl))))
825 != char_type_node))
827 error ("function does not return string type");
828 continue;
831 record_international_format (DECL_NAME (decl),
832 DECL_ASSEMBLER_NAME (decl),
833 format_num);
834 break;
837 case A_WEAK:
838 declare_weak (decl);
839 break;
841 case A_ALIAS:
842 if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
843 || (TREE_CODE (decl) != FUNCTION_DECL && ! DECL_EXTERNAL (decl)))
844 error_with_decl (decl,
845 "`%s' defined both normally and as an alias");
846 else if (decl_function_context (decl) == 0)
848 tree id = get_identifier (TREE_STRING_POINTER
849 (TREE_VALUE (args)));
850 if (TREE_CODE (decl) == FUNCTION_DECL)
851 DECL_INITIAL (decl) = error_mark_node;
852 else
853 DECL_EXTERNAL (decl) = 0;
854 assemble_alias (decl, id);
856 else
857 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
858 break;
863 /* Split SPECS_ATTRS, a list of declspecs and prefix attributes, into two
864 lists. SPECS_ATTRS may also be just a typespec (eg: RECORD_TYPE).
866 The head of the declspec list is stored in DECLSPECS.
867 The head of the attribute list is stored in PREFIX_ATTRIBUTES.
869 Note that attributes in SPECS_ATTRS are stored in the TREE_PURPOSE of
870 the list elements. We drop the containing TREE_LIST nodes and link the
871 resulting attributes together the way decl_attributes expects them. */
873 void
874 split_specs_attrs (specs_attrs, declspecs, prefix_attributes)
875 tree specs_attrs;
876 tree *declspecs, *prefix_attributes;
878 tree t, s, a, next, specs, attrs;
880 /* This can happen in c++ (eg: decl: typespec initdecls ';'). */
881 if (specs_attrs != NULL_TREE
882 && TREE_CODE (specs_attrs) != TREE_LIST)
884 *declspecs = specs_attrs;
885 *prefix_attributes = NULL_TREE;
886 return;
889 /* Remember to keep the lists in the same order, element-wise. */
891 specs = s = NULL_TREE;
892 attrs = a = NULL_TREE;
893 for (t = specs_attrs; t; t = next)
895 next = TREE_CHAIN (t);
896 /* Declspecs have a non-NULL TREE_VALUE. */
897 if (TREE_VALUE (t) != NULL_TREE)
899 if (specs == NULL_TREE)
900 specs = s = t;
901 else
903 TREE_CHAIN (s) = t;
904 s = t;
907 else
909 if (attrs == NULL_TREE)
910 attrs = a = TREE_PURPOSE (t);
911 else
913 TREE_CHAIN (a) = TREE_PURPOSE (t);
914 a = TREE_PURPOSE (t);
916 /* More attrs can be linked here, move A to the end. */
917 while (TREE_CHAIN (a) != NULL_TREE)
918 a = TREE_CHAIN (a);
922 /* Terminate the lists. */
923 if (s != NULL_TREE)
924 TREE_CHAIN (s) = NULL_TREE;
925 if (a != NULL_TREE)
926 TREE_CHAIN (a) = NULL_TREE;
928 /* All done. */
929 *declspecs = specs;
930 *prefix_attributes = attrs;
933 /* Strip attributes from SPECS_ATTRS, a list of declspecs and attributes.
934 This function is used by the parser when a rule will accept attributes
935 in a particular position, but we don't want to support that just yet.
937 A warning is issued for every ignored attribute. */
939 tree
940 strip_attrs (specs_attrs)
941 tree specs_attrs;
943 tree specs, attrs;
945 split_specs_attrs (specs_attrs, &specs, &attrs);
947 while (attrs)
949 warning ("`%s' attribute ignored",
950 IDENTIFIER_POINTER (TREE_PURPOSE (attrs)));
951 attrs = TREE_CHAIN (attrs);
954 return specs;
957 /* Check a printf/fprintf/sprintf/scanf/fscanf/sscanf format against
958 a parameter list. */
960 #define T_I &integer_type_node
961 #define T_L &long_integer_type_node
962 #define T_LL &long_long_integer_type_node
963 #define T_S &short_integer_type_node
964 #define T_UI &unsigned_type_node
965 #define T_UL &long_unsigned_type_node
966 #define T_ULL &long_long_unsigned_type_node
967 #define T_US &short_unsigned_type_node
968 #define T_F &float_type_node
969 #define T_D &double_type_node
970 #define T_LD &long_double_type_node
971 #define T_C &char_type_node
972 #define T_UC &unsigned_char_type_node
973 #define T_V &void_type_node
974 #define T_W &wchar_type_node
975 #define T_ST &sizetype
977 typedef struct {
978 char *format_chars;
979 int pointer_count;
980 /* Type of argument if no length modifier is used. */
981 tree *nolen;
982 /* Type of argument if length modifier for shortening to byte is used.
983 If NULL, then this modifier is not allowed. */
984 tree *hhlen;
985 /* Type of argument if length modifier for shortening is used.
986 If NULL, then this modifier is not allowed. */
987 tree *hlen;
988 /* Type of argument if length modifier `l' is used.
989 If NULL, then this modifier is not allowed. */
990 tree *llen;
991 /* Type of argument if length modifier `q' or `ll' is used.
992 If NULL, then this modifier is not allowed. */
993 tree *qlen;
994 /* Type of argument if length modifier `L' is used.
995 If NULL, then this modifier is not allowed. */
996 tree *bigllen;
997 /* Type of argument if length modifier `Z' is used.
998 If NULL, then this modifier is not allowed. */
999 tree *zlen;
1000 /* List of other modifier characters allowed with these options. */
1001 char *flag_chars;
1002 } format_char_info;
1004 static format_char_info print_char_table[] = {
1005 { "di", 0, T_I, T_I, T_I, T_L, T_LL, T_LL, T_ST, "-wp0 +" },
1006 { "oxX", 0, T_UI, T_UI, T_UI, T_UL, T_ULL, T_ULL, T_ST, "-wp0#" },
1007 { "u", 0, T_UI, T_UI, T_UI, T_UL, T_ULL, T_ULL, T_ST, "-wp0" },
1008 /* A GNU extension. */
1009 { "m", 0, T_V, NULL, NULL, NULL, NULL, NULL, NULL, "-wp" },
1010 { "feEgGaA", 0, T_D, NULL, NULL, NULL, NULL, T_LD, NULL, "-wp0 +#" },
1011 { "c", 0, T_I, NULL, NULL, T_W, NULL, NULL, NULL, "-w" },
1012 { "C", 0, T_W, NULL, NULL, NULL, NULL, NULL, NULL, "-w" },
1013 { "s", 1, T_C, NULL, NULL, T_W, NULL, NULL, NULL, "-wp" },
1014 { "S", 1, T_W, NULL, NULL, NULL, NULL, NULL, NULL, "-wp" },
1015 { "p", 1, T_V, NULL, NULL, NULL, NULL, NULL, NULL, "-w" },
1016 { "n", 1, T_I, NULL, T_S, T_L, T_LL, NULL, NULL, "" },
1017 { NULL }
1020 static format_char_info scan_char_table[] = {
1021 { "di", 1, T_I, T_C, T_S, T_L, T_LL, T_LL, NULL, "*" },
1022 { "ouxX", 1, T_UI, T_UC, T_US, T_UL, T_ULL, T_ULL, NULL, "*" },
1023 { "efgEGaA", 1, T_F, NULL, NULL, T_D, NULL, T_LD, NULL, "*" },
1024 { "c", 1, T_C, NULL, NULL, T_W, NULL, NULL, NULL, "*" },
1025 { "s", 1, T_C, NULL, NULL, T_W, NULL, NULL, NULL, "*a" },
1026 { "[", 1, T_C, NULL, NULL, NULL, NULL, NULL, NULL, "*a" },
1027 { "C", 1, T_W, NULL, NULL, NULL, NULL, NULL, NULL, "*" },
1028 { "S", 1, T_W, NULL, NULL, NULL, NULL, NULL, NULL, "*a" },
1029 { "p", 2, T_V, NULL, NULL, NULL, NULL, NULL, NULL, "*" },
1030 { "n", 1, T_I, T_C, T_S, T_L, T_LL, NULL, NULL, "" },
1031 { NULL }
1034 /* Handle format characters recognized by glibc's strftime.c.
1035 '2' - MUST do years as only two digits
1036 '3' - MAY do years as only two digits (depending on locale)
1037 'E' - E modifier is acceptable
1038 'O' - O modifier is acceptable to Standard C
1039 'o' - O modifier is acceptable as a GNU extension
1040 'G' - other GNU extensions */
1042 static format_char_info time_char_table[] = {
1043 { "y", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "2EO-_0w" },
1044 { "D", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "2" },
1045 { "g", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "2O-_0w" },
1046 { "cx", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "3E" },
1047 { "%RTXnrt", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "" },
1048 { "P", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "G" },
1049 { "HIMSUWdemw", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0Ow" },
1050 { "Vju", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0Oow" },
1051 { "Gklsz", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0OGw" },
1052 { "ABZa", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "^#" },
1053 { "p", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "#" },
1054 { "bh", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "^" },
1055 { "CY", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0EOw" },
1056 { NULL }
1059 typedef struct function_format_info
1061 struct function_format_info *next; /* next structure on the list */
1062 tree name; /* identifier such as "printf" */
1063 tree assembler_name; /* optional mangled identifier (for C++) */
1064 enum format_type format_type; /* type of format (printf, scanf, etc.) */
1065 int format_num; /* number of format argument */
1066 int first_arg_num; /* number of first arg (zero for varargs) */
1067 } function_format_info;
1069 static function_format_info *function_format_list = NULL;
1071 typedef struct international_format_info
1073 struct international_format_info *next; /* next structure on the list */
1074 tree name; /* identifier such as "gettext" */
1075 tree assembler_name; /* optional mangled identifier (for C++) */
1076 int format_num; /* number of format argument */
1077 } international_format_info;
1079 static international_format_info *international_format_list = NULL;
1081 static void check_format_info PROTO((function_format_info *, tree));
1083 /* Initialize the table of functions to perform format checking on.
1084 The ANSI functions are always checked (whether <stdio.h> is
1085 included or not), since it is common to call printf without
1086 including <stdio.h>. There shouldn't be a problem with this,
1087 since ANSI reserves these function names whether you include the
1088 header file or not. In any case, the checking is harmless.
1090 Also initialize the name of function that modify the format string for
1091 internationalization purposes. */
1093 void
1094 init_function_format_info ()
1096 record_function_format (get_identifier ("printf"), NULL_TREE,
1097 printf_format_type, 1, 2);
1098 record_function_format (get_identifier ("fprintf"), NULL_TREE,
1099 printf_format_type, 2, 3);
1100 record_function_format (get_identifier ("sprintf"), NULL_TREE,
1101 printf_format_type, 2, 3);
1102 record_function_format (get_identifier ("scanf"), NULL_TREE,
1103 scanf_format_type, 1, 2);
1104 record_function_format (get_identifier ("fscanf"), NULL_TREE,
1105 scanf_format_type, 2, 3);
1106 record_function_format (get_identifier ("sscanf"), NULL_TREE,
1107 scanf_format_type, 2, 3);
1108 record_function_format (get_identifier ("vprintf"), NULL_TREE,
1109 printf_format_type, 1, 0);
1110 record_function_format (get_identifier ("vfprintf"), NULL_TREE,
1111 printf_format_type, 2, 0);
1112 record_function_format (get_identifier ("vsprintf"), NULL_TREE,
1113 printf_format_type, 2, 0);
1114 record_function_format (get_identifier ("strftime"), NULL_TREE,
1115 strftime_format_type, 3, 0);
1117 record_international_format (get_identifier ("gettext"), NULL_TREE, 1);
1118 record_international_format (get_identifier ("dgettext"), NULL_TREE, 2);
1119 record_international_format (get_identifier ("dcgettext"), NULL_TREE, 2);
1122 /* Record information for argument format checking. FUNCTION_IDENT is
1123 the identifier node for the name of the function to check (its decl
1124 need not exist yet).
1125 FORMAT_TYPE specifies the type of format checking. FORMAT_NUM is the number
1126 of the argument which is the format control string (starting from 1).
1127 FIRST_ARG_NUM is the number of the first actual argument to check
1128 against the format string, or zero if no checking is not be done
1129 (e.g. for varargs such as vfprintf). */
1131 static void
1132 record_function_format (name, assembler_name, format_type,
1133 format_num, first_arg_num)
1134 tree name;
1135 tree assembler_name;
1136 enum format_type format_type;
1137 int format_num;
1138 int first_arg_num;
1140 function_format_info *info;
1142 /* Re-use existing structure if it's there. */
1144 for (info = function_format_list; info; info = info->next)
1146 if (info->name == name && info->assembler_name == assembler_name)
1147 break;
1149 if (! info)
1151 info = (function_format_info *) xmalloc (sizeof (function_format_info));
1152 info->next = function_format_list;
1153 function_format_list = info;
1155 info->name = name;
1156 info->assembler_name = assembler_name;
1159 info->format_type = format_type;
1160 info->format_num = format_num;
1161 info->first_arg_num = first_arg_num;
1164 /* Record information for the names of function that modify the format
1165 argument to format functions. FUNCTION_IDENT is the identifier node for
1166 the name of the function (its decl need not exist yet) and FORMAT_NUM is
1167 the number of the argument which is the format control string (starting
1168 from 1). */
1170 static void
1171 record_international_format (name, assembler_name, format_num)
1172 tree name;
1173 tree assembler_name;
1174 int format_num;
1176 international_format_info *info;
1178 /* Re-use existing structure if it's there. */
1180 for (info = international_format_list; info; info = info->next)
1182 if (info->name == name && info->assembler_name == assembler_name)
1183 break;
1186 if (! info)
1188 info
1189 = (international_format_info *)
1190 xmalloc (sizeof (international_format_info));
1191 info->next = international_format_list;
1192 international_format_list = info;
1194 info->name = name;
1195 info->assembler_name = assembler_name;
1198 info->format_num = format_num;
1201 static char tfaff[] = "too few arguments for format";
1203 /* Check the argument list of a call to printf, scanf, etc.
1204 NAME is the function identifier.
1205 ASSEMBLER_NAME is the function's assembler identifier.
1206 (Either NAME or ASSEMBLER_NAME, but not both, may be NULL_TREE.)
1207 PARAMS is the list of argument values. */
1209 void
1210 check_function_format (name, assembler_name, params)
1211 tree name;
1212 tree assembler_name;
1213 tree params;
1215 function_format_info *info;
1217 /* See if this function is a format function. */
1218 for (info = function_format_list; info; info = info->next)
1220 if (info->assembler_name
1221 ? (info->assembler_name == assembler_name)
1222 : (info->name == name))
1224 /* Yup; check it. */
1225 check_format_info (info, params);
1226 break;
1231 /* Check the argument list of a call to printf, scanf, etc.
1232 INFO points to the function_format_info structure.
1233 PARAMS is the list of argument values. */
1235 static void
1236 check_format_info (info, params)
1237 function_format_info *info;
1238 tree params;
1240 int i;
1241 int arg_num;
1242 int suppressed, wide, precise;
1243 int length_char;
1244 int format_char;
1245 int format_length;
1246 tree format_tree;
1247 tree cur_param;
1248 tree cur_type;
1249 tree wanted_type;
1250 tree first_fillin_param;
1251 char *format_chars;
1252 format_char_info *fci;
1253 char flag_chars[8];
1254 int has_operand_number = 0;
1256 /* Skip to format argument. If the argument isn't available, there's
1257 no work for us to do; prototype checking will catch the problem. */
1258 for (arg_num = 1; ; ++arg_num)
1260 if (params == 0)
1261 return;
1262 if (arg_num == info->format_num)
1263 break;
1264 params = TREE_CHAIN (params);
1266 format_tree = TREE_VALUE (params);
1267 params = TREE_CHAIN (params);
1268 if (format_tree == 0)
1269 return;
1271 /* We can only check the format if it's a string constant. */
1272 while (TREE_CODE (format_tree) == NOP_EXPR)
1273 format_tree = TREE_OPERAND (format_tree, 0); /* strip coercion */
1275 if (TREE_CODE (format_tree) == CALL_EXPR
1276 && TREE_CODE (TREE_OPERAND (format_tree, 0)) == ADDR_EXPR
1277 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (format_tree, 0), 0))
1278 == FUNCTION_DECL))
1280 tree function = TREE_OPERAND (TREE_OPERAND (format_tree, 0), 0);
1282 /* See if this is a call to a known internationalization function
1283 that modifies the format arg. */
1284 international_format_info *info;
1286 for (info = international_format_list; info; info = info->next)
1287 if (info->assembler_name
1288 ? (info->assembler_name == DECL_ASSEMBLER_NAME (function))
1289 : (info->name == DECL_NAME (function)))
1291 tree inner_args;
1292 int i;
1294 for (inner_args = TREE_OPERAND (format_tree, 1), i = 1;
1295 inner_args != 0;
1296 inner_args = TREE_CHAIN (inner_args), i++)
1297 if (i == info->format_num)
1299 format_tree = TREE_VALUE (inner_args);
1301 while (TREE_CODE (format_tree) == NOP_EXPR)
1302 format_tree = TREE_OPERAND (format_tree, 0);
1307 if (integer_zerop (format_tree))
1309 warning ("null format string");
1310 return;
1312 if (TREE_CODE (format_tree) != ADDR_EXPR)
1313 return;
1314 format_tree = TREE_OPERAND (format_tree, 0);
1315 if (TREE_CODE (format_tree) != STRING_CST)
1316 return;
1317 format_chars = TREE_STRING_POINTER (format_tree);
1318 format_length = TREE_STRING_LENGTH (format_tree);
1319 if (format_length <= 1)
1320 warning ("zero-length format string");
1321 if (format_chars[--format_length] != 0)
1323 warning ("unterminated format string");
1324 return;
1326 /* Skip to first argument to check. */
1327 while (arg_num + 1 < info->first_arg_num)
1329 if (params == 0)
1330 return;
1331 params = TREE_CHAIN (params);
1332 ++arg_num;
1335 first_fillin_param = params;
1336 while (1)
1338 int aflag;
1339 if (*format_chars == 0)
1341 if (format_chars - TREE_STRING_POINTER (format_tree) != format_length)
1342 warning ("embedded `\\0' in format");
1343 if (info->first_arg_num != 0 && params != 0 && ! has_operand_number)
1344 warning ("too many arguments for format");
1345 return;
1347 if (*format_chars++ != '%')
1348 continue;
1349 if (*format_chars == 0)
1351 warning ("spurious trailing `%%' in format");
1352 continue;
1354 if (*format_chars == '%')
1356 ++format_chars;
1357 continue;
1359 flag_chars[0] = 0;
1360 suppressed = wide = precise = FALSE;
1361 if (info->format_type == scanf_format_type)
1363 suppressed = *format_chars == '*';
1364 if (suppressed)
1365 ++format_chars;
1366 while (ISDIGIT (*format_chars))
1367 ++format_chars;
1369 else if (info->format_type == strftime_format_type)
1371 while (*format_chars != 0 && index ("_-0^#", *format_chars) != 0)
1373 if (pedantic)
1374 warning ("ANSI C does not support the strftime `%c' flag",
1375 *format_chars);
1376 if (index (flag_chars, *format_chars) != 0)
1378 warning ("repeated `%c' flag in format",
1379 *format_chars);
1380 ++format_chars;
1382 else
1384 i = strlen (flag_chars);
1385 flag_chars[i++] = *format_chars++;
1386 flag_chars[i] = 0;
1389 while (ISDIGIT ((unsigned char) *format_chars))
1391 wide = TRUE;
1392 ++format_chars;
1394 if (wide && pedantic)
1395 warning ("ANSI C does not support strftime format width");
1396 if (*format_chars == 'E' || *format_chars == 'O')
1398 i = strlen (flag_chars);
1399 flag_chars[i++] = *format_chars++;
1400 flag_chars[i] = 0;
1401 if (*format_chars == 'E' || *format_chars == 'O')
1403 warning ("multiple E/O modifiers in format");
1404 while (*format_chars == 'E' || *format_chars == 'O')
1405 ++format_chars;
1409 else if (info->format_type == printf_format_type)
1411 /* See if we have a number followed by a dollar sign. If we do,
1412 it is an operand number, so set PARAMS to that operand. */
1413 if (*format_chars >= '0' && *format_chars <= '9')
1415 char *p = format_chars;
1417 while (*p >= '0' && *p++ <= '9')
1420 if (*p == '$')
1422 int opnum = atoi (format_chars);
1424 params = first_fillin_param;
1425 format_chars = p + 1;
1426 has_operand_number = 1;
1428 for (i = 1; i < opnum && params != 0; i++)
1429 params = TREE_CHAIN (params);
1431 if (opnum == 0 || params == 0)
1433 warning ("operand number out of range in format");
1434 return;
1439 while (*format_chars != 0 && index (" +#0-", *format_chars) != 0)
1441 if (index (flag_chars, *format_chars) != 0)
1442 warning ("repeated `%c' flag in format", *format_chars++);
1443 else
1445 i = strlen (flag_chars);
1446 flag_chars[i++] = *format_chars++;
1447 flag_chars[i] = 0;
1450 /* "If the space and + flags both appear,
1451 the space flag will be ignored." */
1452 if (index (flag_chars, ' ') != 0
1453 && index (flag_chars, '+') != 0)
1454 warning ("use of both ` ' and `+' flags in format");
1455 /* "If the 0 and - flags both appear,
1456 the 0 flag will be ignored." */
1457 if (index (flag_chars, '0') != 0
1458 && index (flag_chars, '-') != 0)
1459 warning ("use of both `0' and `-' flags in format");
1460 if (*format_chars == '*')
1462 wide = TRUE;
1463 /* "...a field width...may be indicated by an asterisk.
1464 In this case, an int argument supplies the field width..." */
1465 ++format_chars;
1466 if (params == 0)
1468 warning (tfaff);
1469 return;
1471 if (info->first_arg_num != 0)
1473 cur_param = TREE_VALUE (params);
1474 params = TREE_CHAIN (params);
1475 ++arg_num;
1476 /* size_t is generally not valid here.
1477 It will work on most machines, because size_t and int
1478 have the same mode. But might as well warn anyway,
1479 since it will fail on other machines. */
1480 if ((TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1481 != integer_type_node)
1483 (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1484 != unsigned_type_node))
1485 warning ("field width is not type int (arg %d)", arg_num);
1488 else
1490 while (ISDIGIT (*format_chars))
1492 wide = TRUE;
1493 ++format_chars;
1496 if (*format_chars == '.')
1498 precise = TRUE;
1499 ++format_chars;
1500 if (*format_chars != '*' && !ISDIGIT (*format_chars))
1501 warning ("`.' not followed by `*' or digit in format");
1502 /* "...a...precision...may be indicated by an asterisk.
1503 In this case, an int argument supplies the...precision." */
1504 if (*format_chars == '*')
1506 if (info->first_arg_num != 0)
1508 ++format_chars;
1509 if (params == 0)
1511 warning (tfaff);
1512 return;
1514 cur_param = TREE_VALUE (params);
1515 params = TREE_CHAIN (params);
1516 ++arg_num;
1517 if (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1518 != integer_type_node)
1519 warning ("field width is not type int (arg %d)",
1520 arg_num);
1523 else
1525 while (ISDIGIT (*format_chars))
1526 ++format_chars;
1531 aflag = 0;
1533 if (info->format_type != strftime_format_type)
1535 if (*format_chars == 'h' || *format_chars == 'l')
1536 length_char = *format_chars++;
1537 else if (*format_chars == 'q' || *format_chars == 'L')
1539 length_char = *format_chars++;
1540 if (pedantic)
1541 warning ("ANSI C does not support the `%c' length modifier",
1542 length_char);
1544 else if (*format_chars == 'Z')
1546 length_char = *format_chars++;
1547 if (pedantic)
1548 warning ("ANSI C does not support the `Z' length modifier");
1550 else
1551 length_char = 0;
1552 if (length_char == 'l' && *format_chars == 'l')
1554 length_char = 'q', format_chars++;
1555 /* FIXME: Is allowed in ISO C 9x. */
1556 if (pedantic)
1557 warning ("ANSI C does not support the `ll' length modifier");
1559 else if (length_char == 'h' && *format_chars == 'h')
1561 length_char = 'H', format_chars++;
1562 /* FIXME: Is allowed in ISO C 9x. */
1563 if (pedantic)
1564 warning ("ANSI C does not support the `hh' length modifier");
1566 if (*format_chars == 'a' && info->format_type == scanf_format_type)
1568 if (format_chars[1] == 's' || format_chars[1] == 'S'
1569 || format_chars[1] == '[')
1571 /* `a' is used as a flag. */
1572 aflag = 1;
1573 format_chars++;
1576 if (suppressed && length_char != 0)
1577 warning ("use of `*' and `%c' together in format", length_char);
1579 format_char = *format_chars;
1580 if (format_char == 0
1581 || (info->format_type != strftime_format_type && format_char == '%'))
1583 warning ("conversion lacks type at end of format");
1584 continue;
1586 /* The m, C, and S formats are GNU extensions. */
1587 if (pedantic && info->format_type != strftime_format_type
1588 && (format_char == 'm' || format_char == 'C' || format_char == 'S'))
1589 warning ("ANSI C does not support the `%c' format", format_char);
1590 /* ??? The a and A formats are C9X extensions, and should be allowed
1591 when a C9X option is added. */
1592 if (pedantic && info->format_type != strftime_format_type
1593 && (format_char == 'a' || format_char == 'A'))
1594 warning ("ANSI C does not support the `%c' format", format_char);
1595 format_chars++;
1596 switch (info->format_type)
1598 case printf_format_type:
1599 fci = print_char_table;
1600 break;
1601 case scanf_format_type:
1602 fci = scan_char_table;
1603 break;
1604 case strftime_format_type:
1605 fci = time_char_table;
1606 break;
1607 default:
1608 abort ();
1610 while (fci->format_chars != 0
1611 && index (fci->format_chars, format_char) == 0)
1612 ++fci;
1613 if (fci->format_chars == 0)
1615 if (format_char >= 040 && format_char < 0177)
1616 warning ("unknown conversion type character `%c' in format",
1617 format_char);
1618 else
1619 warning ("unknown conversion type character 0x%x in format",
1620 format_char);
1621 continue;
1623 if (pedantic)
1625 if (index (fci->flag_chars, 'G') != 0)
1626 warning ("ANSI C does not support `%%%c'", format_char);
1627 if (index (fci->flag_chars, 'o') != 0
1628 && index (flag_chars, 'O') != 0)
1629 warning ("ANSI C does not support `%%O%c'", format_char);
1631 if (wide && index (fci->flag_chars, 'w') == 0)
1632 warning ("width used with `%c' format", format_char);
1633 if (index (fci->flag_chars, '2') != 0)
1634 warning ("`%%%c' yields only last 2 digits of year", format_char);
1635 else if (index (fci->flag_chars, '3') != 0)
1636 warning ("`%%%c' yields only last 2 digits of year in some locales",
1637 format_char);
1638 if (precise && index (fci->flag_chars, 'p') == 0)
1639 warning ("precision used with `%c' format", format_char);
1640 if (aflag && index (fci->flag_chars, 'a') == 0)
1642 warning ("`a' flag used with `%c' format", format_char);
1643 /* To simplify the following code. */
1644 aflag = 0;
1646 /* The a flag is a GNU extension. */
1647 else if (pedantic && aflag)
1648 warning ("ANSI C does not support the `a' flag");
1649 if (info->format_type == scanf_format_type && format_char == '[')
1651 /* Skip over scan set, in case it happens to have '%' in it. */
1652 if (*format_chars == '^')
1653 ++format_chars;
1654 /* Find closing bracket; if one is hit immediately, then
1655 it's part of the scan set rather than a terminator. */
1656 if (*format_chars == ']')
1657 ++format_chars;
1658 while (*format_chars && *format_chars != ']')
1659 ++format_chars;
1660 if (*format_chars != ']')
1661 /* The end of the format string was reached. */
1662 warning ("no closing `]' for `%%[' format");
1664 if (suppressed)
1666 if (index (fci->flag_chars, '*') == 0)
1667 warning ("suppression of `%c' conversion in format", format_char);
1668 continue;
1670 for (i = 0; flag_chars[i] != 0; ++i)
1672 if (index (fci->flag_chars, flag_chars[i]) == 0)
1673 warning ("flag `%c' used with type `%c'",
1674 flag_chars[i], format_char);
1676 if (info->format_type == strftime_format_type)
1677 continue;
1678 if (precise && index (flag_chars, '0') != 0
1679 && (format_char == 'd' || format_char == 'i'
1680 || format_char == 'o' || format_char == 'u'
1681 || format_char == 'x' || format_char == 'x'))
1682 warning ("`0' flag ignored with precision specifier and `%c' format",
1683 format_char);
1684 switch (length_char)
1686 default: wanted_type = fci->nolen ? *(fci->nolen) : 0; break;
1687 case 'H': wanted_type = fci->hhlen ? *(fci->hhlen) : 0; break;
1688 case 'h': wanted_type = fci->hlen ? *(fci->hlen) : 0; break;
1689 case 'l': wanted_type = fci->llen ? *(fci->llen) : 0; break;
1690 case 'q': wanted_type = fci->qlen ? *(fci->qlen) : 0; break;
1691 case 'L': wanted_type = fci->bigllen ? *(fci->bigllen) : 0; break;
1692 case 'Z': wanted_type = fci->zlen ? *fci->zlen : 0; break;
1694 if (wanted_type == 0)
1695 warning ("use of `%c' length character with `%c' type character",
1696 length_char, format_char);
1698 /* Finally. . .check type of argument against desired type! */
1699 if (info->first_arg_num == 0)
1700 continue;
1701 if (fci->pointer_count == 0 && wanted_type == void_type_node)
1702 /* This specifier takes no argument. */
1703 continue;
1704 if (params == 0)
1706 warning (tfaff);
1707 return;
1709 cur_param = TREE_VALUE (params);
1710 params = TREE_CHAIN (params);
1711 ++arg_num;
1712 cur_type = TREE_TYPE (cur_param);
1714 STRIP_NOPS (cur_param);
1716 /* Check the types of any additional pointer arguments
1717 that precede the "real" argument. */
1718 for (i = 0; i < fci->pointer_count + aflag; ++i)
1720 if (TREE_CODE (cur_type) == POINTER_TYPE)
1722 cur_type = TREE_TYPE (cur_type);
1724 if (cur_param != 0 && TREE_CODE (cur_param) == ADDR_EXPR)
1725 cur_param = TREE_OPERAND (cur_param, 0);
1726 else
1727 cur_param = 0;
1729 continue;
1731 if (TREE_CODE (cur_type) != ERROR_MARK)
1732 warning ("format argument is not a %s (arg %d)",
1733 ((fci->pointer_count + aflag == 1)
1734 ? "pointer" : "pointer to a pointer"),
1735 arg_num);
1736 break;
1739 /* See if this is an attempt to write into a const type with
1740 scanf or with printf "%n". */
1741 if ((info->format_type == scanf_format_type
1742 || (info->format_type == printf_format_type
1743 && format_char == 'n'))
1744 && i == fci->pointer_count + aflag
1745 && wanted_type != 0
1746 && TREE_CODE (cur_type) != ERROR_MARK
1747 && (TYPE_READONLY (cur_type)
1748 || (cur_param != 0
1749 && (TREE_CODE_CLASS (TREE_CODE (cur_param)) == 'c'
1750 || (TREE_CODE_CLASS (TREE_CODE (cur_param)) == 'd'
1751 && TREE_READONLY (cur_param))))))
1752 warning ("writing into constant object (arg %d)", arg_num);
1754 /* Check the type of the "real" argument, if there's a type we want. */
1755 if (i == fci->pointer_count + aflag && wanted_type != 0
1756 && TREE_CODE (cur_type) != ERROR_MARK
1757 && wanted_type != TYPE_MAIN_VARIANT (cur_type)
1758 /* If we want `void *', allow any pointer type.
1759 (Anything else would already have got a warning.) */
1760 && ! (wanted_type == void_type_node
1761 && fci->pointer_count > 0)
1762 /* Don't warn about differences merely in signedness. */
1763 && !(TREE_CODE (wanted_type) == INTEGER_TYPE
1764 && TREE_CODE (TYPE_MAIN_VARIANT (cur_type)) == INTEGER_TYPE
1765 && (TREE_UNSIGNED (wanted_type)
1766 ? wanted_type == (cur_type = unsigned_type (cur_type))
1767 : wanted_type == (cur_type = signed_type (cur_type))))
1768 /* Likewise, "signed char", "unsigned char" and "char" are
1769 equivalent but the above test won't consider them equivalent. */
1770 && ! (wanted_type == char_type_node
1771 && (TYPE_MAIN_VARIANT (cur_type) == signed_char_type_node
1772 || TYPE_MAIN_VARIANT (cur_type) == unsigned_char_type_node)))
1774 register char *this;
1775 register char *that;
1777 this = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (wanted_type)));
1778 that = 0;
1779 if (TREE_CODE (cur_type) != ERROR_MARK
1780 && TYPE_NAME (cur_type) != 0
1781 && TREE_CODE (cur_type) != INTEGER_TYPE
1782 && !(TREE_CODE (cur_type) == POINTER_TYPE
1783 && TREE_CODE (TREE_TYPE (cur_type)) == INTEGER_TYPE))
1785 if (TREE_CODE (TYPE_NAME (cur_type)) == TYPE_DECL
1786 && DECL_NAME (TYPE_NAME (cur_type)) != 0)
1787 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type)));
1788 else
1789 that = IDENTIFIER_POINTER (TYPE_NAME (cur_type));
1792 /* A nameless type can't possibly match what the format wants.
1793 So there will be a warning for it.
1794 Make up a string to describe vaguely what it is. */
1795 if (that == 0)
1797 if (TREE_CODE (cur_type) == POINTER_TYPE)
1798 that = "pointer";
1799 else
1800 that = "different type";
1803 /* Make the warning better in case of mismatch of int vs long. */
1804 if (TREE_CODE (cur_type) == INTEGER_TYPE
1805 && TREE_CODE (wanted_type) == INTEGER_TYPE
1806 && TYPE_PRECISION (cur_type) == TYPE_PRECISION (wanted_type)
1807 && TYPE_NAME (cur_type) != 0
1808 && TREE_CODE (TYPE_NAME (cur_type)) == TYPE_DECL)
1809 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type)));
1811 if (strcmp (this, that) != 0)
1812 warning ("%s format, %s arg (arg %d)", this, that, arg_num);
1817 /* Print a warning if a constant expression had overflow in folding.
1818 Invoke this function on every expression that the language
1819 requires to be a constant expression.
1820 Note the ANSI C standard says it is erroneous for a
1821 constant expression to overflow. */
1823 void
1824 constant_expression_warning (value)
1825 tree value;
1827 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1828 || TREE_CODE (value) == COMPLEX_CST)
1829 && TREE_CONSTANT_OVERFLOW (value) && pedantic)
1830 pedwarn ("overflow in constant expression");
1833 /* Print a warning if an expression had overflow in folding.
1834 Invoke this function on every expression that
1835 (1) appears in the source code, and
1836 (2) might be a constant expression that overflowed, and
1837 (3) is not already checked by convert_and_check;
1838 however, do not invoke this function on operands of explicit casts. */
1840 void
1841 overflow_warning (value)
1842 tree value;
1844 if ((TREE_CODE (value) == INTEGER_CST
1845 || (TREE_CODE (value) == COMPLEX_CST
1846 && TREE_CODE (TREE_REALPART (value)) == INTEGER_CST))
1847 && TREE_OVERFLOW (value))
1849 TREE_OVERFLOW (value) = 0;
1850 if (skip_evaluation == 0)
1851 warning ("integer overflow in expression");
1853 else if ((TREE_CODE (value) == REAL_CST
1854 || (TREE_CODE (value) == COMPLEX_CST
1855 && TREE_CODE (TREE_REALPART (value)) == REAL_CST))
1856 && TREE_OVERFLOW (value))
1858 TREE_OVERFLOW (value) = 0;
1859 if (skip_evaluation == 0)
1860 warning ("floating point overflow in expression");
1864 /* Print a warning if a large constant is truncated to unsigned,
1865 or if -Wconversion is used and a constant < 0 is converted to unsigned.
1866 Invoke this function on every expression that might be implicitly
1867 converted to an unsigned type. */
1869 void
1870 unsigned_conversion_warning (result, operand)
1871 tree result, operand;
1873 if (TREE_CODE (operand) == INTEGER_CST
1874 && TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
1875 && TREE_UNSIGNED (TREE_TYPE (result))
1876 && skip_evaluation == 0
1877 && !int_fits_type_p (operand, TREE_TYPE (result)))
1879 if (!int_fits_type_p (operand, signed_type (TREE_TYPE (result))))
1880 /* This detects cases like converting -129 or 256 to unsigned char. */
1881 warning ("large integer implicitly truncated to unsigned type");
1882 else if (warn_conversion)
1883 warning ("negative integer implicitly converted to unsigned type");
1887 /* Convert EXPR to TYPE, warning about conversion problems with constants.
1888 Invoke this function on every expression that is converted implicitly,
1889 i.e. because of language rules and not because of an explicit cast. */
1891 tree
1892 convert_and_check (type, expr)
1893 tree type, expr;
1895 tree t = convert (type, expr);
1896 if (TREE_CODE (t) == INTEGER_CST)
1898 if (TREE_OVERFLOW (t))
1900 TREE_OVERFLOW (t) = 0;
1902 /* Do not diagnose overflow in a constant expression merely
1903 because a conversion overflowed. */
1904 TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (expr);
1906 /* No warning for converting 0x80000000 to int. */
1907 if (!(TREE_UNSIGNED (type) < TREE_UNSIGNED (TREE_TYPE (expr))
1908 && TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
1909 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (expr))))
1910 /* If EXPR fits in the unsigned version of TYPE,
1911 don't warn unless pedantic. */
1912 if ((pedantic
1913 || TREE_UNSIGNED (type)
1914 || ! int_fits_type_p (expr, unsigned_type (type)))
1915 && skip_evaluation == 0)
1916 warning ("overflow in implicit constant conversion");
1918 else
1919 unsigned_conversion_warning (t, expr);
1921 return t;
1924 void
1925 c_expand_expr_stmt (expr)
1926 tree expr;
1928 /* Do default conversion if safe and possibly important,
1929 in case within ({...}). */
1930 if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE && lvalue_p (expr))
1931 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
1932 expr = default_conversion (expr);
1934 if (TREE_TYPE (expr) != error_mark_node
1935 && TYPE_SIZE (TREE_TYPE (expr)) == 0
1936 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
1937 error ("expression statement has incomplete type");
1939 expand_expr_stmt (expr);
1942 /* Validate the expression after `case' and apply default promotions. */
1944 tree
1945 check_case_value (value)
1946 tree value;
1948 if (value == NULL_TREE)
1949 return value;
1951 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1952 STRIP_TYPE_NOPS (value);
1954 if (TREE_CODE (value) != INTEGER_CST
1955 && value != error_mark_node)
1957 error ("case label does not reduce to an integer constant");
1958 value = error_mark_node;
1960 else
1961 /* Promote char or short to int. */
1962 value = default_conversion (value);
1964 constant_expression_warning (value);
1966 return value;
1969 /* Return an integer type with BITS bits of precision,
1970 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
1972 tree
1973 type_for_size (bits, unsignedp)
1974 unsigned bits;
1975 int unsignedp;
1977 if (bits == TYPE_PRECISION (integer_type_node))
1978 return unsignedp ? unsigned_type_node : integer_type_node;
1980 if (bits == TYPE_PRECISION (signed_char_type_node))
1981 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
1983 if (bits == TYPE_PRECISION (short_integer_type_node))
1984 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
1986 if (bits == TYPE_PRECISION (long_integer_type_node))
1987 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
1989 if (bits == TYPE_PRECISION (long_long_integer_type_node))
1990 return (unsignedp ? long_long_unsigned_type_node
1991 : long_long_integer_type_node);
1993 if (bits <= TYPE_PRECISION (intQI_type_node))
1994 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
1996 if (bits <= TYPE_PRECISION (intHI_type_node))
1997 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
1999 if (bits <= TYPE_PRECISION (intSI_type_node))
2000 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2002 if (bits <= TYPE_PRECISION (intDI_type_node))
2003 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2005 return 0;
2008 /* Return a data type that has machine mode MODE.
2009 If the mode is an integer,
2010 then UNSIGNEDP selects between signed and unsigned types. */
2012 tree
2013 type_for_mode (mode, unsignedp)
2014 enum machine_mode mode;
2015 int unsignedp;
2017 if (mode == TYPE_MODE (integer_type_node))
2018 return unsignedp ? unsigned_type_node : integer_type_node;
2020 if (mode == TYPE_MODE (signed_char_type_node))
2021 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2023 if (mode == TYPE_MODE (short_integer_type_node))
2024 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2026 if (mode == TYPE_MODE (long_integer_type_node))
2027 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2029 if (mode == TYPE_MODE (long_long_integer_type_node))
2030 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
2032 if (mode == TYPE_MODE (intQI_type_node))
2033 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2035 if (mode == TYPE_MODE (intHI_type_node))
2036 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2038 if (mode == TYPE_MODE (intSI_type_node))
2039 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2041 if (mode == TYPE_MODE (intDI_type_node))
2042 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2044 if (mode == TYPE_MODE (intTI_type_node))
2045 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2047 if (mode == TYPE_MODE (float_type_node))
2048 return float_type_node;
2050 if (mode == TYPE_MODE (double_type_node))
2051 return double_type_node;
2053 if (mode == TYPE_MODE (long_double_type_node))
2054 return long_double_type_node;
2056 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
2057 return build_pointer_type (char_type_node);
2059 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
2060 return build_pointer_type (integer_type_node);
2062 return 0;
2065 /* Return the minimum number of bits needed to represent VALUE in a
2066 signed or unsigned type, UNSIGNEDP says which. */
2069 min_precision (value, unsignedp)
2070 tree value;
2071 int unsignedp;
2073 int log;
2075 /* If the value is negative, compute its negative minus 1. The latter
2076 adjustment is because the absolute value of the largest negative value
2077 is one larger than the largest positive value. This is equivalent to
2078 a bit-wise negation, so use that operation instead. */
2080 if (tree_int_cst_sgn (value) < 0)
2081 value = fold (build1 (BIT_NOT_EXPR, TREE_TYPE (value), value));
2083 /* Return the number of bits needed, taking into account the fact
2084 that we need one more bit for a signed than unsigned type. */
2086 if (integer_zerop (value))
2087 log = 0;
2088 else if (TREE_INT_CST_HIGH (value) != 0)
2089 log = HOST_BITS_PER_WIDE_INT + floor_log2 (TREE_INT_CST_HIGH (value));
2090 else
2091 log = floor_log2 (TREE_INT_CST_LOW (value));
2093 return log + 1 + ! unsignedp;
2096 /* Print an error message for invalid operands to arith operation CODE.
2097 NOP_EXPR is used as a special case (see truthvalue_conversion). */
2099 void
2100 binary_op_error (code)
2101 enum tree_code code;
2103 register char *opname;
2105 switch (code)
2107 case NOP_EXPR:
2108 error ("invalid truth-value expression");
2109 return;
2111 case PLUS_EXPR:
2112 opname = "+"; break;
2113 case MINUS_EXPR:
2114 opname = "-"; break;
2115 case MULT_EXPR:
2116 opname = "*"; break;
2117 case MAX_EXPR:
2118 opname = "max"; break;
2119 case MIN_EXPR:
2120 opname = "min"; break;
2121 case EQ_EXPR:
2122 opname = "=="; break;
2123 case NE_EXPR:
2124 opname = "!="; break;
2125 case LE_EXPR:
2126 opname = "<="; break;
2127 case GE_EXPR:
2128 opname = ">="; break;
2129 case LT_EXPR:
2130 opname = "<"; break;
2131 case GT_EXPR:
2132 opname = ">"; break;
2133 case LSHIFT_EXPR:
2134 opname = "<<"; break;
2135 case RSHIFT_EXPR:
2136 opname = ">>"; break;
2137 case TRUNC_MOD_EXPR:
2138 case FLOOR_MOD_EXPR:
2139 opname = "%"; break;
2140 case TRUNC_DIV_EXPR:
2141 case FLOOR_DIV_EXPR:
2142 opname = "/"; break;
2143 case BIT_AND_EXPR:
2144 opname = "&"; break;
2145 case BIT_IOR_EXPR:
2146 opname = "|"; break;
2147 case TRUTH_ANDIF_EXPR:
2148 opname = "&&"; break;
2149 case TRUTH_ORIF_EXPR:
2150 opname = "||"; break;
2151 case BIT_XOR_EXPR:
2152 opname = "^"; break;
2153 case LROTATE_EXPR:
2154 case RROTATE_EXPR:
2155 opname = "rotate"; break;
2156 default:
2157 opname = "unknown"; break;
2159 error ("invalid operands to binary %s", opname);
2162 /* Subroutine of build_binary_op, used for comparison operations.
2163 See if the operands have both been converted from subword integer types
2164 and, if so, perhaps change them both back to their original type.
2165 This function is also responsible for converting the two operands
2166 to the proper common type for comparison.
2168 The arguments of this function are all pointers to local variables
2169 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
2170 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
2172 If this function returns nonzero, it means that the comparison has
2173 a constant value. What this function returns is an expression for
2174 that value. */
2176 tree
2177 shorten_compare (op0_ptr, op1_ptr, restype_ptr, rescode_ptr)
2178 tree *op0_ptr, *op1_ptr;
2179 tree *restype_ptr;
2180 enum tree_code *rescode_ptr;
2182 register tree type;
2183 tree op0 = *op0_ptr;
2184 tree op1 = *op1_ptr;
2185 int unsignedp0, unsignedp1;
2186 int real1, real2;
2187 tree primop0, primop1;
2188 enum tree_code code = *rescode_ptr;
2190 /* Throw away any conversions to wider types
2191 already present in the operands. */
2193 primop0 = get_narrower (op0, &unsignedp0);
2194 primop1 = get_narrower (op1, &unsignedp1);
2196 /* Handle the case that OP0 does not *contain* a conversion
2197 but it *requires* conversion to FINAL_TYPE. */
2199 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
2200 unsignedp0 = TREE_UNSIGNED (TREE_TYPE (op0));
2201 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
2202 unsignedp1 = TREE_UNSIGNED (TREE_TYPE (op1));
2204 /* If one of the operands must be floated, we cannot optimize. */
2205 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
2206 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
2208 /* If first arg is constant, swap the args (changing operation
2209 so value is preserved), for canonicalization. Don't do this if
2210 the second arg is 0. */
2212 if (TREE_CONSTANT (primop0)
2213 && ! integer_zerop (primop1) && ! real_zerop (primop1))
2215 register tree tem = primop0;
2216 register int temi = unsignedp0;
2217 primop0 = primop1;
2218 primop1 = tem;
2219 tem = op0;
2220 op0 = op1;
2221 op1 = tem;
2222 *op0_ptr = op0;
2223 *op1_ptr = op1;
2224 unsignedp0 = unsignedp1;
2225 unsignedp1 = temi;
2226 temi = real1;
2227 real1 = real2;
2228 real2 = temi;
2230 switch (code)
2232 case LT_EXPR:
2233 code = GT_EXPR;
2234 break;
2235 case GT_EXPR:
2236 code = LT_EXPR;
2237 break;
2238 case LE_EXPR:
2239 code = GE_EXPR;
2240 break;
2241 case GE_EXPR:
2242 code = LE_EXPR;
2243 break;
2244 default:
2245 break;
2247 *rescode_ptr = code;
2250 /* If comparing an integer against a constant more bits wide,
2251 maybe we can deduce a value of 1 or 0 independent of the data.
2252 Or else truncate the constant now
2253 rather than extend the variable at run time.
2255 This is only interesting if the constant is the wider arg.
2256 Also, it is not safe if the constant is unsigned and the
2257 variable arg is signed, since in this case the variable
2258 would be sign-extended and then regarded as unsigned.
2259 Our technique fails in this case because the lowest/highest
2260 possible unsigned results don't follow naturally from the
2261 lowest/highest possible values of the variable operand.
2262 For just EQ_EXPR and NE_EXPR there is another technique that
2263 could be used: see if the constant can be faithfully represented
2264 in the other operand's type, by truncating it and reextending it
2265 and see if that preserves the constant's value. */
2267 if (!real1 && !real2
2268 && TREE_CODE (primop1) == INTEGER_CST
2269 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
2271 int min_gt, max_gt, min_lt, max_lt;
2272 tree maxval, minval;
2273 /* 1 if comparison is nominally unsigned. */
2274 int unsignedp = TREE_UNSIGNED (*restype_ptr);
2275 tree val;
2277 type = signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0));
2279 maxval = TYPE_MAX_VALUE (type);
2280 minval = TYPE_MIN_VALUE (type);
2282 if (unsignedp && !unsignedp0)
2283 *restype_ptr = signed_type (*restype_ptr);
2285 if (TREE_TYPE (primop1) != *restype_ptr)
2286 primop1 = convert (*restype_ptr, primop1);
2287 if (type != *restype_ptr)
2289 minval = convert (*restype_ptr, minval);
2290 maxval = convert (*restype_ptr, maxval);
2293 if (unsignedp && unsignedp0)
2295 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
2296 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
2297 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
2298 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
2300 else
2302 min_gt = INT_CST_LT (primop1, minval);
2303 max_gt = INT_CST_LT (primop1, maxval);
2304 min_lt = INT_CST_LT (minval, primop1);
2305 max_lt = INT_CST_LT (maxval, primop1);
2308 val = 0;
2309 /* This used to be a switch, but Genix compiler can't handle that. */
2310 if (code == NE_EXPR)
2312 if (max_lt || min_gt)
2313 val = boolean_true_node;
2315 else if (code == EQ_EXPR)
2317 if (max_lt || min_gt)
2318 val = boolean_false_node;
2320 else if (code == LT_EXPR)
2322 if (max_lt)
2323 val = boolean_true_node;
2324 if (!min_lt)
2325 val = boolean_false_node;
2327 else if (code == GT_EXPR)
2329 if (min_gt)
2330 val = boolean_true_node;
2331 if (!max_gt)
2332 val = boolean_false_node;
2334 else if (code == LE_EXPR)
2336 if (!max_gt)
2337 val = boolean_true_node;
2338 if (min_gt)
2339 val = boolean_false_node;
2341 else if (code == GE_EXPR)
2343 if (!min_lt)
2344 val = boolean_true_node;
2345 if (max_lt)
2346 val = boolean_false_node;
2349 /* If primop0 was sign-extended and unsigned comparison specd,
2350 we did a signed comparison above using the signed type bounds.
2351 But the comparison we output must be unsigned.
2353 Also, for inequalities, VAL is no good; but if the signed
2354 comparison had *any* fixed result, it follows that the
2355 unsigned comparison just tests the sign in reverse
2356 (positive values are LE, negative ones GE).
2357 So we can generate an unsigned comparison
2358 against an extreme value of the signed type. */
2360 if (unsignedp && !unsignedp0)
2362 if (val != 0)
2363 switch (code)
2365 case LT_EXPR:
2366 case GE_EXPR:
2367 primop1 = TYPE_MIN_VALUE (type);
2368 val = 0;
2369 break;
2371 case LE_EXPR:
2372 case GT_EXPR:
2373 primop1 = TYPE_MAX_VALUE (type);
2374 val = 0;
2375 break;
2377 default:
2378 break;
2380 type = unsigned_type (type);
2383 if (!max_gt && !unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
2385 /* This is the case of (char)x >?< 0x80, which people used to use
2386 expecting old C compilers to change the 0x80 into -0x80. */
2387 if (val == boolean_false_node)
2388 warning ("comparison is always 0 due to limited range of data type");
2389 if (val == boolean_true_node)
2390 warning ("comparison is always 1 due to limited range of data type");
2393 if (!min_lt && unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
2395 /* This is the case of (unsigned char)x >?< -1 or < 0. */
2396 if (val == boolean_false_node)
2397 warning ("comparison is always 0 due to limited range of data type");
2398 if (val == boolean_true_node)
2399 warning ("comparison is always 1 due to limited range of data type");
2402 if (val != 0)
2404 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2405 if (TREE_SIDE_EFFECTS (primop0))
2406 return build (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
2407 return val;
2410 /* Value is not predetermined, but do the comparison
2411 in the type of the operand that is not constant.
2412 TYPE is already properly set. */
2414 else if (real1 && real2
2415 && (TYPE_PRECISION (TREE_TYPE (primop0))
2416 == TYPE_PRECISION (TREE_TYPE (primop1))))
2417 type = TREE_TYPE (primop0);
2419 /* If args' natural types are both narrower than nominal type
2420 and both extend in the same manner, compare them
2421 in the type of the wider arg.
2422 Otherwise must actually extend both to the nominal
2423 common type lest different ways of extending
2424 alter the result.
2425 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
2427 else if (unsignedp0 == unsignedp1 && real1 == real2
2428 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
2429 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
2431 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
2432 type = signed_or_unsigned_type (unsignedp0
2433 || TREE_UNSIGNED (*restype_ptr),
2434 type);
2435 /* Make sure shorter operand is extended the right way
2436 to match the longer operand. */
2437 primop0 = convert (signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0)),
2438 primop0);
2439 primop1 = convert (signed_or_unsigned_type (unsignedp1, TREE_TYPE (primop1)),
2440 primop1);
2442 else
2444 /* Here we must do the comparison on the nominal type
2445 using the args exactly as we received them. */
2446 type = *restype_ptr;
2447 primop0 = op0;
2448 primop1 = op1;
2450 if (!real1 && !real2 && integer_zerop (primop1)
2451 && TREE_UNSIGNED (*restype_ptr))
2453 tree value = 0;
2454 switch (code)
2456 case GE_EXPR:
2457 /* All unsigned values are >= 0, so we warn if extra warnings
2458 are requested. However, if OP0 is a constant that is
2459 >= 0, the signedness of the comparison isn't an issue,
2460 so suppress the warning. */
2461 if (extra_warnings
2462 && ! (TREE_CODE (primop0) == INTEGER_CST
2463 && ! TREE_OVERFLOW (convert (signed_type (type),
2464 primop0))))
2465 warning ("unsigned value >= 0 is always 1");
2466 value = boolean_true_node;
2467 break;
2469 case LT_EXPR:
2470 if (extra_warnings
2471 && ! (TREE_CODE (primop0) == INTEGER_CST
2472 && ! TREE_OVERFLOW (convert (signed_type (type),
2473 primop0))))
2474 warning ("unsigned value < 0 is always 0");
2475 value = boolean_false_node;
2476 break;
2478 default:
2479 break;
2482 if (value != 0)
2484 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2485 if (TREE_SIDE_EFFECTS (primop0))
2486 return build (COMPOUND_EXPR, TREE_TYPE (value),
2487 primop0, value);
2488 return value;
2493 *op0_ptr = convert (type, primop0);
2494 *op1_ptr = convert (type, primop1);
2496 *restype_ptr = boolean_type_node;
2498 return 0;
2501 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
2502 or validate its data type for an `if' or `while' statement or ?..: exp.
2504 This preparation consists of taking the ordinary
2505 representation of an expression expr and producing a valid tree
2506 boolean expression describing whether expr is nonzero. We could
2507 simply always do build_binary_op (NE_EXPR, expr, boolean_false_node, 1),
2508 but we optimize comparisons, &&, ||, and !.
2510 The resulting type should always be `boolean_type_node'. */
2512 tree
2513 truthvalue_conversion (expr)
2514 tree expr;
2516 if (TREE_CODE (expr) == ERROR_MARK)
2517 return expr;
2519 #if 0 /* This appears to be wrong for C++. */
2520 /* These really should return error_mark_node after 2.4 is stable.
2521 But not all callers handle ERROR_MARK properly. */
2522 switch (TREE_CODE (TREE_TYPE (expr)))
2524 case RECORD_TYPE:
2525 error ("struct type value used where scalar is required");
2526 return boolean_false_node;
2528 case UNION_TYPE:
2529 error ("union type value used where scalar is required");
2530 return boolean_false_node;
2532 case ARRAY_TYPE:
2533 error ("array type value used where scalar is required");
2534 return boolean_false_node;
2536 default:
2537 break;
2539 #endif /* 0 */
2541 switch (TREE_CODE (expr))
2543 /* It is simpler and generates better code to have only TRUTH_*_EXPR
2544 or comparison expressions as truth values at this level. */
2545 #if 0
2546 case COMPONENT_REF:
2547 /* A one-bit unsigned bit-field is already acceptable. */
2548 if (1 == TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (expr, 1)))
2549 && TREE_UNSIGNED (TREE_OPERAND (expr, 1)))
2550 return expr;
2551 break;
2552 #endif
2554 case EQ_EXPR:
2555 /* It is simpler and generates better code to have only TRUTH_*_EXPR
2556 or comparison expressions as truth values at this level. */
2557 #if 0
2558 if (integer_zerop (TREE_OPERAND (expr, 1)))
2559 return build_unary_op (TRUTH_NOT_EXPR, TREE_OPERAND (expr, 0), 0);
2560 #endif
2561 case NE_EXPR: case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
2562 case TRUTH_ANDIF_EXPR:
2563 case TRUTH_ORIF_EXPR:
2564 case TRUTH_AND_EXPR:
2565 case TRUTH_OR_EXPR:
2566 case TRUTH_XOR_EXPR:
2567 case TRUTH_NOT_EXPR:
2568 TREE_TYPE (expr) = boolean_type_node;
2569 return expr;
2571 case ERROR_MARK:
2572 return expr;
2574 case INTEGER_CST:
2575 return integer_zerop (expr) ? boolean_false_node : boolean_true_node;
2577 case REAL_CST:
2578 return real_zerop (expr) ? boolean_false_node : boolean_true_node;
2580 case ADDR_EXPR:
2581 /* If we are taking the address of a external decl, it might be zero
2582 if it is weak, so we cannot optimize. */
2583 if (TREE_CODE_CLASS (TREE_CODE (TREE_OPERAND (expr, 0))) == 'd'
2584 && DECL_EXTERNAL (TREE_OPERAND (expr, 0)))
2585 break;
2587 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 0)))
2588 return build (COMPOUND_EXPR, boolean_type_node,
2589 TREE_OPERAND (expr, 0), boolean_true_node);
2590 else
2591 return boolean_true_node;
2593 case COMPLEX_EXPR:
2594 return build_binary_op ((TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
2595 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
2596 truthvalue_conversion (TREE_OPERAND (expr, 0)),
2597 truthvalue_conversion (TREE_OPERAND (expr, 1)),
2600 case NEGATE_EXPR:
2601 case ABS_EXPR:
2602 case FLOAT_EXPR:
2603 case FFS_EXPR:
2604 /* These don't change whether an object is non-zero or zero. */
2605 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2607 case LROTATE_EXPR:
2608 case RROTATE_EXPR:
2609 /* These don't change whether an object is zero or non-zero, but
2610 we can't ignore them if their second arg has side-effects. */
2611 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
2612 return build (COMPOUND_EXPR, boolean_type_node, TREE_OPERAND (expr, 1),
2613 truthvalue_conversion (TREE_OPERAND (expr, 0)));
2614 else
2615 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2617 case COND_EXPR:
2618 /* Distribute the conversion into the arms of a COND_EXPR. */
2619 return fold (build (COND_EXPR, boolean_type_node, TREE_OPERAND (expr, 0),
2620 truthvalue_conversion (TREE_OPERAND (expr, 1)),
2621 truthvalue_conversion (TREE_OPERAND (expr, 2))));
2623 case CONVERT_EXPR:
2624 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
2625 since that affects how `default_conversion' will behave. */
2626 if (TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE
2627 || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
2628 break;
2629 /* fall through... */
2630 case NOP_EXPR:
2631 /* If this is widening the argument, we can ignore it. */
2632 if (TYPE_PRECISION (TREE_TYPE (expr))
2633 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
2634 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2635 break;
2637 case MINUS_EXPR:
2638 /* With IEEE arithmetic, x - x may not equal 0, so we can't optimize
2639 this case. */
2640 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
2641 && TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE)
2642 break;
2643 /* fall through... */
2644 case BIT_XOR_EXPR:
2645 /* This and MINUS_EXPR can be changed into a comparison of the
2646 two objects. */
2647 if (TREE_TYPE (TREE_OPERAND (expr, 0))
2648 == TREE_TYPE (TREE_OPERAND (expr, 1)))
2649 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2650 TREE_OPERAND (expr, 1), 1);
2651 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2652 fold (build1 (NOP_EXPR,
2653 TREE_TYPE (TREE_OPERAND (expr, 0)),
2654 TREE_OPERAND (expr, 1))), 1);
2656 case BIT_AND_EXPR:
2657 if (integer_onep (TREE_OPERAND (expr, 1))
2658 && TREE_TYPE (expr) != boolean_type_node)
2659 /* Using convert here would cause infinite recursion. */
2660 return build1 (NOP_EXPR, boolean_type_node, expr);
2661 break;
2663 case MODIFY_EXPR:
2664 if (warn_parentheses && C_EXP_ORIGINAL_CODE (expr) == MODIFY_EXPR)
2665 warning ("suggest parentheses around assignment used as truth value");
2666 break;
2668 default:
2669 break;
2672 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
2673 return (build_binary_op
2674 ((TREE_SIDE_EFFECTS (expr)
2675 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
2676 truthvalue_conversion (build_unary_op (REALPART_EXPR, expr, 0)),
2677 truthvalue_conversion (build_unary_op (IMAGPART_EXPR, expr, 0)),
2678 0));
2680 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
2683 #if USE_CPPLIB
2684 /* Read the rest of a #-directive from input stream FINPUT.
2685 In normal use, the directive name and the white space after it
2686 have already been read, so they won't be included in the result.
2687 We allow for the fact that the directive line may contain
2688 a newline embedded within a character or string literal which forms
2689 a part of the directive.
2691 The value is a string in a reusable buffer. It remains valid
2692 only until the next time this function is called. */
2693 unsigned char *yy_cur, *yy_lim;
2695 #define GETC() (yy_cur < yy_lim ? *yy_cur++ : yy_get_token ())
2696 #define UNGETC(c) ((c), yy_cur--)
2699 yy_get_token ()
2701 for (;;)
2703 parse_in.limit = parse_in.token_buffer;
2704 cpp_token = cpp_get_token (&parse_in);
2705 if (cpp_token == CPP_EOF)
2706 return -1;
2707 yy_lim = CPP_PWRITTEN (&parse_in);
2708 yy_cur = parse_in.token_buffer;
2709 if (yy_cur < yy_lim)
2710 return *yy_cur++;
2714 char *
2715 get_directive_line ()
2717 static char *directive_buffer = NULL;
2718 static unsigned buffer_length = 0;
2719 register char *p;
2720 register char *buffer_limit;
2721 register int looking_for = 0;
2722 register int char_escaped = 0;
2724 if (buffer_length == 0)
2726 directive_buffer = (char *)xmalloc (128);
2727 buffer_length = 128;
2730 buffer_limit = &directive_buffer[buffer_length];
2732 for (p = directive_buffer; ; )
2734 int c;
2736 /* Make buffer bigger if it is full. */
2737 if (p >= buffer_limit)
2739 register unsigned bytes_used = (p - directive_buffer);
2741 buffer_length *= 2;
2742 directive_buffer
2743 = (char *)xrealloc (directive_buffer, buffer_length);
2744 p = &directive_buffer[bytes_used];
2745 buffer_limit = &directive_buffer[buffer_length];
2748 c = GETC ();
2750 /* Discard initial whitespace. */
2751 if ((c == ' ' || c == '\t') && p == directive_buffer)
2752 continue;
2754 /* Detect the end of the directive. */
2755 if (c == '\n' && looking_for == 0)
2757 UNGETC (c);
2758 c = '\0';
2761 *p++ = c;
2763 if (c == 0)
2764 return directive_buffer;
2766 /* Handle string and character constant syntax. */
2767 if (looking_for)
2769 if (looking_for == c && !char_escaped)
2770 looking_for = 0; /* Found terminator... stop looking. */
2772 else
2773 if (c == '\'' || c == '"')
2774 looking_for = c; /* Don't stop buffering until we see another
2775 another one of these (or an EOF). */
2777 /* Handle backslash. */
2778 char_escaped = (c == '\\' && ! char_escaped);
2781 #else
2782 /* Read the rest of a #-directive from input stream FINPUT.
2783 In normal use, the directive name and the white space after it
2784 have already been read, so they won't be included in the result.
2785 We allow for the fact that the directive line may contain
2786 a newline embedded within a character or string literal which forms
2787 a part of the directive.
2789 The value is a string in a reusable buffer. It remains valid
2790 only until the next time this function is called.
2792 The terminating character ('\n' or EOF) is left in FINPUT for the
2793 caller to re-read. */
2795 char *
2796 get_directive_line (finput)
2797 register FILE *finput;
2799 static char *directive_buffer = NULL;
2800 static unsigned buffer_length = 0;
2801 register char *p;
2802 register char *buffer_limit;
2803 register int looking_for = 0;
2804 register int char_escaped = 0;
2806 if (buffer_length == 0)
2808 directive_buffer = (char *)xmalloc (128);
2809 buffer_length = 128;
2812 buffer_limit = &directive_buffer[buffer_length];
2814 for (p = directive_buffer; ; )
2816 int c;
2818 /* Make buffer bigger if it is full. */
2819 if (p >= buffer_limit)
2821 register unsigned bytes_used = (p - directive_buffer);
2823 buffer_length *= 2;
2824 directive_buffer
2825 = (char *)xrealloc (directive_buffer, buffer_length);
2826 p = &directive_buffer[bytes_used];
2827 buffer_limit = &directive_buffer[buffer_length];
2830 c = getc (finput);
2832 /* Discard initial whitespace. */
2833 if ((c == ' ' || c == '\t') && p == directive_buffer)
2834 continue;
2836 /* Detect the end of the directive. */
2837 if (looking_for == 0
2838 && (c == '\n' || c == EOF))
2840 ungetc (c, finput);
2841 c = '\0';
2844 *p++ = c;
2846 if (c == 0)
2847 return directive_buffer;
2849 /* Handle string and character constant syntax. */
2850 if (looking_for)
2852 if (looking_for == c && !char_escaped)
2853 looking_for = 0; /* Found terminator... stop looking. */
2855 else
2856 if (c == '\'' || c == '"')
2857 looking_for = c; /* Don't stop buffering until we see another
2858 one of these (or an EOF). */
2860 /* Handle backslash. */
2861 char_escaped = (c == '\\' && ! char_escaped);
2864 #endif /* !USE_CPPLIB */
2866 /* Make a variant type in the proper way for C/C++, propagating qualifiers
2867 down to the element type of an array. */
2869 tree
2870 c_build_type_variant (type, constp, volatilep)
2871 tree type;
2872 int constp, volatilep;
2874 if (TREE_CODE (type) == ARRAY_TYPE)
2875 return build_array_type (c_build_type_variant (TREE_TYPE (type),
2876 constp, volatilep),
2877 TYPE_DOMAIN (type));
2878 return build_type_variant (type, constp, volatilep);