]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c-common.c
[thirdparty/gcc.git] / gcc / c-common.c
CommitLineData
b0fc3e72 1/* Subroutines shared by all languages that are variants of C.
997d68fe 2 Copyright (C) 1992, 93, 94, 95, 96, 97, 1998 Free Software Foundation, Inc.
b0fc3e72 3
4This file is part of GNU CC.
5
6GNU CC is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU CC is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU CC; see the file COPYING. If not, write to
8d62a21c 18the Free Software Foundation, 59 Temple Place - Suite 330,
19Boston, MA 02111-1307, USA. */
b0fc3e72 20
21#include "config.h"
405711de 22#include "system.h"
b0fc3e72 23#include "tree.h"
24#include "c-lex.h"
25#include "c-tree.h"
26#include "flags.h"
5da0b0ab 27#include "obstack.h"
9cdfa0b0 28#include "toplev.h"
cd03a192 29#include "output.h"
3ef9782d 30
c83bcd28 31#ifndef WCHAR_TYPE_SIZE
32#ifdef INT_TYPE_SIZE
33#define WCHAR_TYPE_SIZE INT_TYPE_SIZE
34#else
35#define WCHAR_TYPE_SIZE BITS_PER_WORD
36#endif
37#endif
38
ceee5ef4 39extern struct obstack permanent_obstack;
40
e78703c1 41/* Nonzero means the expression being parsed will never be evaluated.
42 This is a count, since unevaluated expressions can nest. */
43int skip_evaluation;
44
9493f142 45enum attrs {A_PACKED, A_NOCOMMON, A_COMMON, A_NORETURN, A_CONST, A_T_UNION,
1f57d1e5 46 A_CONSTRUCTOR, A_DESTRUCTOR, A_MODE, A_SECTION, A_ALIGNED,
b91b108f 47 A_UNUSED, A_FORMAT, A_FORMAT_ARG, A_WEAK, A_ALIAS};
550e135c 48
d1f11193 49enum format_type { printf_format_type, scanf_format_type,
50 strftime_format_type };
51
550e135c 52static void declare_hidden_char_array PROTO((char *, char *));
53static void add_attribute PROTO((enum attrs, char *,
54 int, int, int));
55static void init_attributes PROTO((void));
d1f11193 56static void record_function_format PROTO((tree, tree, enum format_type,
57 int, int));
b91b108f 58static void record_international_format PROTO((tree, tree, int));
be43ff5a 59
9fc84d48 60/* Keep a stack of if statements. We record the number of compound
61 statements seen up to the if keyword, as well as the line number
62 and file of the if. If a potentially ambiguous else is seen, that
63 fact is recorded; the warning is issued when we can be sure that
64 the enclosing if statement does not have an else branch. */
65typedef struct
66{
67 int compstmt_count;
68 int line;
69 char *file;
70 int needs_warning;
71} if_elt;
72
73static if_elt *if_stack;
31f820d2 74
75/* Amount of space in the if statement stack. */
76static int if_stack_space = 0;
77
78/* Stack pointer. */
79static int if_stack_pointer = 0;
80
9fc84d48 81/* Generate RTL for the start of an if-then, and record the start of it
82 for ambiguous else detection. */
83
31f820d2 84void
85c_expand_start_cond (cond, exitflag, compstmt_count)
86 tree cond;
87 int exitflag;
88 int compstmt_count;
89{
90 /* Make sure there is enough space on the stack. */
91 if (if_stack_space == 0)
92 {
93 if_stack_space = 10;
9fc84d48 94 if_stack = (if_elt *)xmalloc (10 * sizeof (if_elt));
31f820d2 95 }
96 else if (if_stack_space == if_stack_pointer)
97 {
98 if_stack_space += 10;
9fc84d48 99 if_stack = (if_elt *)xrealloc (if_stack, if_stack_space * sizeof (if_elt));
31f820d2 100 }
9fc84d48 101
31f820d2 102 /* Record this if statement. */
9fc84d48 103 if_stack[if_stack_pointer].compstmt_count = compstmt_count;
104 if_stack[if_stack_pointer].file = input_filename;
105 if_stack[if_stack_pointer].line = lineno;
106 if_stack[if_stack_pointer].needs_warning = 0;
107 if_stack_pointer++;
31f820d2 108
109 expand_start_cond (cond, exitflag);
110}
111
9fc84d48 112/* Generate RTL for the end of an if-then. Optionally warn if a nested
113 if statement had an ambiguous else clause. */
114
31f820d2 115void
116c_expand_end_cond ()
117{
118 if_stack_pointer--;
9fc84d48 119 if (if_stack[if_stack_pointer].needs_warning)
120 warning_with_file_and_line (if_stack[if_stack_pointer].file,
121 if_stack[if_stack_pointer].line,
122 "suggest explicit braces to avoid ambiguous `else'");
31f820d2 123 expand_end_cond ();
124}
125
9fc84d48 126/* Generate RTL between the then-clause and the else-clause
127 of an if-then-else. */
128
31f820d2 129void
130c_expand_start_else ()
131{
9fc84d48 132 /* An ambiguous else warning must be generated for the enclosing if
133 statement, unless we see an else branch for that one, too. */
31f820d2 134 if (warn_parentheses
135 && if_stack_pointer > 1
9fc84d48 136 && (if_stack[if_stack_pointer - 1].compstmt_count
137 == if_stack[if_stack_pointer - 2].compstmt_count))
138 if_stack[if_stack_pointer - 2].needs_warning = 1;
139
140 /* Even if a nested if statement had an else branch, it can't be
141 ambiguous if this one also has an else. So don't warn in that
142 case. Also don't warn for any if statements nested in this else. */
143 if_stack[if_stack_pointer - 1].needs_warning = 0;
144 if_stack[if_stack_pointer - 1].compstmt_count--;
31f820d2 145
146 expand_start_else ();
147}
148
b1497296 149/* Make bindings for __FUNCTION__ and __PRETTY_FUNCTION__. */
f4e3c278 150
151void
152declare_function_name ()
153{
f4e3c278 154 char *name, *printable_name;
155
156 if (current_function_decl == NULL)
157 {
158 name = "";
159 printable_name = "top level";
160 }
161 else
162 {
3f3680c7 163 /* Allow functions to be nameless (such as artificial ones). */
164 if (DECL_NAME (current_function_decl))
165 name = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
166 else
167 name = "";
59561872 168 printable_name = (*decl_printable_name) (current_function_decl, 2);
f4e3c278 169 }
170
be43ff5a 171 declare_hidden_char_array ("__FUNCTION__", name);
172 declare_hidden_char_array ("__PRETTY_FUNCTION__", printable_name);
173}
f4c9036d 174
be43ff5a 175static void
176declare_hidden_char_array (name, value)
177 char *name, *value;
178{
179 tree decl, type, init;
180 int vlen;
f4e3c278 181
be43ff5a 182 /* If the default size of char arrays isn't big enough for the name,
3227fe11 183 or if we want to give warnings for large objects, make a bigger one. */
be43ff5a 184 vlen = strlen (value) + 1;
f4c9036d 185 type = char_array_type_node;
2695dec0 186 if (TREE_INT_CST_LOW (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) < vlen
3227fe11 187 || warn_larger_than)
f4c9036d 188 type = build_array_type (char_type_node,
be43ff5a 189 build_index_type (build_int_2 (vlen, 0)));
f4e3c278 190 push_obstacks_nochange ();
be43ff5a 191 decl = build_decl (VAR_DECL, get_identifier (name), type);
f4e3c278 192 TREE_STATIC (decl) = 1;
193 TREE_READONLY (decl) = 1;
be43ff5a 194 TREE_ASM_WRITTEN (decl) = 1;
8658b58d 195 DECL_SOURCE_LINE (decl) = 0;
f5d94a98 196 DECL_ARTIFICIAL (decl) = 1;
776899d6 197 DECL_IN_SYSTEM_HEADER (decl) = 1;
f4e3c278 198 DECL_IGNORED_P (decl) = 1;
be43ff5a 199 init = build_string (vlen, value);
f4c9036d 200 TREE_TYPE (init) = type;
f4e3c278 201 DECL_INITIAL (decl) = init;
202 finish_decl (pushdecl (decl), init, NULL_TREE);
203}
204
b0fc3e72 205/* Given a chain of STRING_CST nodes,
206 concatenate them into one STRING_CST
207 and give it a suitable array-of-chars data type. */
208
209tree
210combine_strings (strings)
211 tree strings;
212{
213 register tree value, t;
214 register int length = 1;
215 int wide_length = 0;
216 int wide_flag = 0;
217 int wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
218 int nchars;
219
220 if (TREE_CHAIN (strings))
221 {
222 /* More than one in the chain, so concatenate. */
223 register char *p, *q;
224
225 /* Don't include the \0 at the end of each substring,
226 except for the last one.
227 Count wide strings and ordinary strings separately. */
228 for (t = strings; t; t = TREE_CHAIN (t))
229 {
230 if (TREE_TYPE (t) == wchar_array_type_node)
231 {
232 wide_length += (TREE_STRING_LENGTH (t) - wchar_bytes);
233 wide_flag = 1;
234 }
235 else
236 length += (TREE_STRING_LENGTH (t) - 1);
237 }
238
239 /* If anything is wide, the non-wides will be converted,
240 which makes them take more space. */
241 if (wide_flag)
242 length = length * wchar_bytes + wide_length;
243
244 p = savealloc (length);
245
246 /* Copy the individual strings into the new combined string.
247 If the combined string is wide, convert the chars to ints
248 for any individual strings that are not wide. */
249
250 q = p;
251 for (t = strings; t; t = TREE_CHAIN (t))
252 {
253 int len = (TREE_STRING_LENGTH (t)
254 - ((TREE_TYPE (t) == wchar_array_type_node)
255 ? wchar_bytes : 1));
256 if ((TREE_TYPE (t) == wchar_array_type_node) == wide_flag)
257 {
258 bcopy (TREE_STRING_POINTER (t), q, len);
259 q += len;
260 }
261 else
262 {
263 int i;
264 for (i = 0; i < len; i++)
c9dcb798 265 {
266 if (WCHAR_TYPE_SIZE == HOST_BITS_PER_SHORT)
267 ((short *) q)[i] = TREE_STRING_POINTER (t)[i];
268 else
269 ((int *) q)[i] = TREE_STRING_POINTER (t)[i];
270 }
b0fc3e72 271 q += len * wchar_bytes;
272 }
273 }
274 if (wide_flag)
275 {
276 int i;
277 for (i = 0; i < wchar_bytes; i++)
278 *q++ = 0;
279 }
280 else
281 *q = 0;
282
283 value = make_node (STRING_CST);
284 TREE_STRING_POINTER (value) = p;
285 TREE_STRING_LENGTH (value) = length;
286 TREE_CONSTANT (value) = 1;
287 }
288 else
289 {
290 value = strings;
291 length = TREE_STRING_LENGTH (value);
292 if (TREE_TYPE (value) == wchar_array_type_node)
293 wide_flag = 1;
294 }
295
73be5127 296 /* Compute the number of elements, for the array type. */
b0fc3e72 297 nchars = wide_flag ? length / wchar_bytes : length;
298
299 /* Create the array type for the string constant.
300 -Wwrite-strings says make the string constant an array of const char
301 so that copying it to a non-const pointer will get a warning. */
302 if (warn_write_strings
303 && (! flag_traditional && ! flag_writable_strings))
304 {
305 tree elements
306 = build_type_variant (wide_flag ? wchar_type_node : char_type_node,
307 1, 0);
308 TREE_TYPE (value)
309 = build_array_type (elements,
310 build_index_type (build_int_2 (nchars - 1, 0)));
311 }
312 else
313 TREE_TYPE (value)
314 = build_array_type (wide_flag ? wchar_type_node : char_type_node,
315 build_index_type (build_int_2 (nchars - 1, 0)));
316 TREE_CONSTANT (value) = 1;
317 TREE_STATIC (value) = 1;
318 return value;
319}
320\f
550e135c 321/* To speed up processing of attributes, we maintain an array of
322 IDENTIFIER_NODES and the corresponding attribute types. */
323
324/* Array to hold attribute information. */
325
326static struct {enum attrs id; tree name; int min, max, decl_req;} attrtab[50];
327
328static int attrtab_idx = 0;
329
330/* Add an entry to the attribute table above. */
331
332static void
333add_attribute (id, string, min_len, max_len, decl_req)
334 enum attrs id;
335 char *string;
336 int min_len, max_len;
337 int decl_req;
338{
339 char buf[100];
340
341 attrtab[attrtab_idx].id = id;
342 attrtab[attrtab_idx].name = get_identifier (string);
343 attrtab[attrtab_idx].min = min_len;
344 attrtab[attrtab_idx].max = max_len;
345 attrtab[attrtab_idx++].decl_req = decl_req;
346
347 sprintf (buf, "__%s__", string);
348
349 attrtab[attrtab_idx].id = id;
350 attrtab[attrtab_idx].name = get_identifier (buf);
351 attrtab[attrtab_idx].min = min_len;
352 attrtab[attrtab_idx].max = max_len;
353 attrtab[attrtab_idx++].decl_req = decl_req;
354}
355
356/* Initialize attribute table. */
357
358static void
359init_attributes ()
360{
cf333338 361 add_attribute (A_PACKED, "packed", 0, 0, 0);
1f57d1e5 362 add_attribute (A_NOCOMMON, "nocommon", 0, 0, 1);
9493f142 363 add_attribute (A_COMMON, "common", 0, 0, 1);
550e135c 364 add_attribute (A_NORETURN, "noreturn", 0, 0, 1);
365 add_attribute (A_NORETURN, "volatile", 0, 0, 1);
31f820d2 366 add_attribute (A_UNUSED, "unused", 0, 0, 0);
550e135c 367 add_attribute (A_CONST, "const", 0, 0, 1);
368 add_attribute (A_T_UNION, "transparent_union", 0, 0, 0);
369 add_attribute (A_CONSTRUCTOR, "constructor", 0, 0, 1);
370 add_attribute (A_DESTRUCTOR, "destructor", 0, 0, 1);
371 add_attribute (A_MODE, "mode", 1, 1, 1);
372 add_attribute (A_SECTION, "section", 1, 1, 1);
373 add_attribute (A_ALIGNED, "aligned", 0, 1, 0);
24f0e20d 374 add_attribute (A_FORMAT, "format", 3, 3, 1);
b91b108f 375 add_attribute (A_FORMAT_ARG, "format_arg", 1, 1, 1);
df1c8607 376 add_attribute (A_WEAK, "weak", 0, 0, 1);
377 add_attribute (A_ALIAS, "alias", 1, 1, 1);
550e135c 378}
379\f
528adc5a 380/* Process the attributes listed in ATTRIBUTES and PREFIX_ATTRIBUTES
550e135c 381 and install them in NODE, which is either a DECL (including a TYPE_DECL)
382 or a TYPE. PREFIX_ATTRIBUTES can appear after the declaration specifiers
a92771b8 383 and declaration modifiers but before the declaration proper. */
b0fc3e72 384
385void
550e135c 386decl_attributes (node, attributes, prefix_attributes)
387 tree node, attributes, prefix_attributes;
b0fc3e72 388{
7ad3893d 389 tree decl = 0, type = 0;
390 int is_type = 0;
550e135c 391 tree a;
392
393 if (attrtab_idx == 0)
394 init_attributes ();
395
396 if (TREE_CODE_CLASS (TREE_CODE (node)) == 'd')
397 {
398 decl = node;
399 type = TREE_TYPE (decl);
400 is_type = TREE_CODE (node) == TYPE_DECL;
401 }
402 else if (TREE_CODE_CLASS (TREE_CODE (node)) == 't')
403 type = node, is_type = 1;
252b13bb 404
d9df9e36 405 attributes = chainon (prefix_attributes, attributes);
e532650d 406
b0fc3e72 407 for (a = attributes; a; a = TREE_CHAIN (a))
550e135c 408 {
409 tree name = TREE_PURPOSE (a);
410 tree args = TREE_VALUE (a);
411 int i;
412 enum attrs id;
73be5127 413
550e135c 414 for (i = 0; i < attrtab_idx; i++)
415 if (attrtab[i].name == name)
416 break;
417
606926f8 418 if (i == attrtab_idx)
550e135c 419 {
606926f8 420 if (! valid_machine_attribute (name, args, decl, type))
421 warning ("`%s' attribute directive ignored",
422 IDENTIFIER_POINTER (name));
4b708a9e 423 else if (decl != 0)
424 type = TREE_TYPE (decl);
550e135c 425 continue;
426 }
427 else if (attrtab[i].decl_req && decl == 0)
428 {
429 warning ("`%s' attribute does not apply to types",
430 IDENTIFIER_POINTER (name));
431 continue;
432 }
433 else if (list_length (args) < attrtab[i].min
434 || list_length (args) > attrtab[i].max)
435 {
436 error ("wrong number of arguments specified for `%s' attribute",
437 IDENTIFIER_POINTER (name));
438 continue;
439 }
440
441 id = attrtab[i].id;
442 switch (id)
443 {
444 case A_PACKED:
4f21bc40 445 if (is_type)
cf333338 446 TYPE_PACKED (type) = 1;
447 else if (TREE_CODE (decl) == FIELD_DECL)
550e135c 448 DECL_PACKED (decl) = 1;
449 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
450 used for DECL_REGISTER. It wouldn't mean anything anyway. */
451 else
452 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
453 break;
454
1f57d1e5 455 case A_NOCOMMON:
456 if (TREE_CODE (decl) == VAR_DECL)
457 DECL_COMMON (decl) = 0;
458 else
459 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
460 break;
461
9493f142 462 case A_COMMON:
463 if (TREE_CODE (decl) == VAR_DECL)
464 DECL_COMMON (decl) = 1;
465 else
466 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
467 break;
468
550e135c 469 case A_NORETURN:
470 if (TREE_CODE (decl) == FUNCTION_DECL)
471 TREE_THIS_VOLATILE (decl) = 1;
472 else if (TREE_CODE (type) == POINTER_TYPE
473 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
73be5127 474 TREE_TYPE (decl) = type
550e135c 475 = build_pointer_type
476 (build_type_variant (TREE_TYPE (type),
477 TREE_READONLY (TREE_TYPE (type)), 1));
478 else
479 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
480 break;
481
3cf4cf22 482 case A_UNUSED:
31f820d2 483 if (is_type)
484 TREE_USED (type) = 1;
73be5127 485 else if (TREE_CODE (decl) == PARM_DECL
31f820d2 486 || TREE_CODE (decl) == VAR_DECL
487 || TREE_CODE (decl) == FUNCTION_DECL)
3cf4cf22 488 TREE_USED (decl) = 1;
489 else
490 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
491 break;
492
550e135c 493 case A_CONST:
494 if (TREE_CODE (decl) == FUNCTION_DECL)
495 TREE_READONLY (decl) = 1;
496 else if (TREE_CODE (type) == POINTER_TYPE
497 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
498 TREE_TYPE (decl) = type
499 = build_pointer_type
500 (build_type_variant (TREE_TYPE (type), 1,
501 TREE_THIS_VOLATILE (TREE_TYPE (type))));
502 else
503 warning ( "`%s' attribute ignored", IDENTIFIER_POINTER (name));
504 break;
505
506 case A_T_UNION:
4f21bc40 507 if (is_type
550e135c 508 && TREE_CODE (type) == UNION_TYPE
4f21bc40 509 && (decl == 0
207bece5 510 || (TYPE_FIELDS (type) != 0
511 && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))))
4f21bc40 512 TYPE_TRANSPARENT_UNION (type) = 1;
513 else if (decl != 0 && TREE_CODE (decl) == PARM_DECL
550e135c 514 && TREE_CODE (type) == UNION_TYPE
515 && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))
4f21bc40 516 DECL_TRANSPARENT_UNION (decl) = 1;
550e135c 517 else
518 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
519 break;
520
521 case A_CONSTRUCTOR:
522 if (TREE_CODE (decl) == FUNCTION_DECL
523 && TREE_CODE (type) == FUNCTION_TYPE
524 && decl_function_context (decl) == 0)
6d3e85e7 525 {
526 DECL_STATIC_CONSTRUCTOR (decl) = 1;
527 TREE_USED (decl) = 1;
528 }
550e135c 529 else
530 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
531 break;
532
533 case A_DESTRUCTOR:
534 if (TREE_CODE (decl) == FUNCTION_DECL
535 && TREE_CODE (type) == FUNCTION_TYPE
536 && decl_function_context (decl) == 0)
6d3e85e7 537 {
538 DECL_STATIC_DESTRUCTOR (decl) = 1;
539 TREE_USED (decl) = 1;
540 }
550e135c 541 else
542 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
543 break;
544
545 case A_MODE:
546 if (TREE_CODE (TREE_VALUE (args)) != IDENTIFIER_NODE)
547 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
548 else
549 {
550 int j;
551 char *p = IDENTIFIER_POINTER (TREE_VALUE (args));
552 int len = strlen (p);
553 enum machine_mode mode = VOIDmode;
554 tree typefm;
555
556 if (len > 4 && p[0] == '_' && p[1] == '_'
557 && p[len - 1] == '_' && p[len - 2] == '_')
558 {
22573d05 559 char *newp = (char *) alloca (len - 1);
550e135c 560
561 strcpy (newp, &p[2]);
562 newp[len - 4] = '\0';
563 p = newp;
564 }
565
566 /* Give this decl a type with the specified mode.
567 First check for the special modes. */
568 if (! strcmp (p, "byte"))
569 mode = byte_mode;
570 else if (!strcmp (p, "word"))
571 mode = word_mode;
572 else if (! strcmp (p, "pointer"))
573 mode = ptr_mode;
574 else
575 for (j = 0; j < NUM_MACHINE_MODES; j++)
576 if (!strcmp (p, GET_MODE_NAME (j)))
577 mode = (enum machine_mode) j;
578
579 if (mode == VOIDmode)
580 error ("unknown machine mode `%s'", p);
581 else if (0 == (typefm = type_for_mode (mode,
582 TREE_UNSIGNED (type))))
583 error ("no data type for mode `%s'", p);
584 else
585 {
586 TREE_TYPE (decl) = type = typefm;
587 DECL_SIZE (decl) = 0;
588 layout_decl (decl, 0);
589 }
590 }
591 break;
592
593 case A_SECTION:
e532650d 594#ifdef ASM_OUTPUT_SECTION_NAME
550e135c 595 if ((TREE_CODE (decl) == FUNCTION_DECL
596 || TREE_CODE (decl) == VAR_DECL)
597 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
598 {
73be5127 599 if (TREE_CODE (decl) == VAR_DECL
28f4825b 600 && current_function_decl != NULL_TREE
601 && ! TREE_STATIC (decl))
550e135c 602 error_with_decl (decl,
603 "section attribute cannot be specified for local variables");
604 /* The decl may have already been given a section attribute from
605 a previous declaration. Ensure they match. */
606 else if (DECL_SECTION_NAME (decl) != NULL_TREE
607 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
608 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
609 error_with_decl (node,
610 "section of `%s' conflicts with previous declaration");
611 else
612 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
613 }
614 else
615 error_with_decl (node,
e532650d 616 "section attribute not allowed for `%s'");
617#else
550e135c 618 error_with_decl (node,
619 "section attributes are not supported for this target");
e532650d 620#endif
550e135c 621 break;
622
623 case A_ALIGNED:
245de75a 624 {
550e135c 625 tree align_expr
21e3a120 626 = (args ? TREE_VALUE (args)
627 : size_int (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
550e135c 628 int align;
629
630 /* Strip any NOPs of any kind. */
631 while (TREE_CODE (align_expr) == NOP_EXPR
632 || TREE_CODE (align_expr) == CONVERT_EXPR
633 || TREE_CODE (align_expr) == NON_LVALUE_EXPR)
634 align_expr = TREE_OPERAND (align_expr, 0);
73be5127 635
550e135c 636 if (TREE_CODE (align_expr) != INTEGER_CST)
637 {
638 error ("requested alignment is not a constant");
639 continue;
640 }
641
642 align = TREE_INT_CST_LOW (align_expr) * BITS_PER_UNIT;
643
644 if (exact_log2 (align) == -1)
645 error ("requested alignment is not a power of 2");
646 else if (is_type)
482cae09 647 TYPE_ALIGN (type) = align;
550e135c 648 else if (TREE_CODE (decl) != VAR_DECL
649 && TREE_CODE (decl) != FIELD_DECL)
650 error_with_decl (decl,
651 "alignment may not be specified for `%s'");
652 else
653 DECL_ALIGN (decl) = align;
245de75a 654 }
550e135c 655 break;
245de75a 656
550e135c 657 case A_FORMAT:
b0fc3e72 658 {
d1f11193 659 tree format_type_id = TREE_VALUE (args);
550e135c 660 tree format_num_expr = TREE_VALUE (TREE_CHAIN (args));
661 tree first_arg_num_expr
662 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)));
663 int format_num;
664 int first_arg_num;
d1f11193 665 enum format_type format_type;
550e135c 666 tree argument;
667 int arg_num;
73be5127 668
550e135c 669 if (TREE_CODE (decl) != FUNCTION_DECL)
670 {
671 error_with_decl (decl,
672 "argument format specified for non-function `%s'");
673 continue;
674 }
c5aa1e92 675
d1f11193 676 if (TREE_CODE (format_type_id) != IDENTIFIER_NODE)
b91b108f 677 {
d1f11193 678 error ("unrecognized format specifier");
b91b108f 679 continue;
680 }
550e135c 681 else
682 {
d1f11193 683 char *p = IDENTIFIER_POINTER (format_type_id);
c5aa1e92 684
d1f11193 685 if (!strcmp (p, "printf") || !strcmp (p, "__printf__"))
686 format_type = printf_format_type;
687 else if (!strcmp (p, "scanf") || !strcmp (p, "__scanf__"))
688 format_type = scanf_format_type;
689 else if (!strcmp (p, "strftime")
690 || !strcmp (p, "__strftime__"))
691 format_type = strftime_format_type;
692 else
693 {
694 error ("`%s' is an unrecognized format function type", p);
695 continue;
696 }
550e135c 697 }
245de75a 698
550e135c 699 /* Strip any conversions from the string index and first arg number
700 and verify they are constants. */
701 while (TREE_CODE (format_num_expr) == NOP_EXPR
702 || TREE_CODE (format_num_expr) == CONVERT_EXPR
703 || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
704 format_num_expr = TREE_OPERAND (format_num_expr, 0);
d6cd1111 705
550e135c 706 while (TREE_CODE (first_arg_num_expr) == NOP_EXPR
707 || TREE_CODE (first_arg_num_expr) == CONVERT_EXPR
708 || TREE_CODE (first_arg_num_expr) == NON_LVALUE_EXPR)
709 first_arg_num_expr = TREE_OPERAND (first_arg_num_expr, 0);
710
711 if (TREE_CODE (format_num_expr) != INTEGER_CST
712 || TREE_CODE (first_arg_num_expr) != INTEGER_CST)
557b092e 713 {
550e135c 714 error ("format string has non-constant operand number");
715 continue;
557b092e 716 }
550e135c 717
718 format_num = TREE_INT_CST_LOW (format_num_expr);
719 first_arg_num = TREE_INT_CST_LOW (first_arg_num_expr);
720 if (first_arg_num != 0 && first_arg_num <= format_num)
5d730828 721 {
550e135c 722 error ("format string arg follows the args to be formatted");
245de75a 723 continue;
5d730828 724 }
550e135c 725
726 /* If a parameter list is specified, verify that the format_num
727 argument is actually a string, in case the format attribute
728 is in error. */
729 argument = TYPE_ARG_TYPES (type);
730 if (argument)
557b092e 731 {
550e135c 732 for (arg_num = 1; ; ++arg_num)
557b092e 733 {
550e135c 734 if (argument == 0 || arg_num == format_num)
735 break;
736 argument = TREE_CHAIN (argument);
737 }
738 if (! argument
739 || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
740 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
741 != char_type_node))
742 {
743 error ("format string arg not a string type");
557b092e 744 continue;
745 }
550e135c 746 if (first_arg_num != 0)
747 {
748 /* Verify that first_arg_num points to the last arg,
a92771b8 749 the ... */
550e135c 750 while (argument)
751 arg_num++, argument = TREE_CHAIN (argument);
752 if (arg_num != first_arg_num)
753 {
754 error ("args to be formatted is not ...");
755 continue;
756 }
757 }
557b092e 758 }
252b13bb 759
550e135c 760 record_function_format (DECL_NAME (decl),
761 DECL_ASSEMBLER_NAME (decl),
d1f11193 762 format_type, format_num, first_arg_num);
550e135c 763 break;
764 }
df1c8607 765
b91b108f 766 case A_FORMAT_ARG:
767 {
768 tree format_num_expr = TREE_VALUE (args);
769 int format_num, arg_num;
770 tree argument;
73be5127 771
b91b108f 772 if (TREE_CODE (decl) != FUNCTION_DECL)
773 {
774 error_with_decl (decl,
775 "argument format specified for non-function `%s'");
776 continue;
777 }
73be5127 778
b91b108f 779 /* Strip any conversions from the first arg number and verify it
780 is a constant. */
781 while (TREE_CODE (format_num_expr) == NOP_EXPR
782 || TREE_CODE (format_num_expr) == CONVERT_EXPR
783 || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
784 format_num_expr = TREE_OPERAND (format_num_expr, 0);
785
786 if (TREE_CODE (format_num_expr) != INTEGER_CST)
787 {
788 error ("format string has non-constant operand number");
789 continue;
790 }
791
792 format_num = TREE_INT_CST_LOW (format_num_expr);
793
794 /* If a parameter list is specified, verify that the format_num
795 argument is actually a string, in case the format attribute
796 is in error. */
797 argument = TYPE_ARG_TYPES (type);
798 if (argument)
799 {
800 for (arg_num = 1; ; ++arg_num)
801 {
802 if (argument == 0 || arg_num == format_num)
803 break;
804 argument = TREE_CHAIN (argument);
805 }
806 if (! argument
807 || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
808 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
809 != char_type_node))
810 {
811 error ("format string arg not a string type");
812 continue;
813 }
814 }
815
816 if (TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) != POINTER_TYPE
817 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (TREE_TYPE (decl))))
818 != char_type_node))
819 {
820 error ("function does not return string type");
821 continue;
822 }
823
824 record_international_format (DECL_NAME (decl),
825 DECL_ASSEMBLER_NAME (decl),
826 format_num);
827 break;
828 }
829
df1c8607 830 case A_WEAK:
831 declare_weak (decl);
832 break;
833
834 case A_ALIAS:
835 if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
65dab9aa 836 || (TREE_CODE (decl) != FUNCTION_DECL && ! DECL_EXTERNAL (decl)))
df1c8607 837 error_with_decl (decl,
838 "`%s' defined both normally and as an alias");
839 else if (decl_function_context (decl) == 0)
840 {
841 tree id = get_identifier (TREE_STRING_POINTER
842 (TREE_VALUE (args)));
843 if (TREE_CODE (decl) == FUNCTION_DECL)
844 DECL_INITIAL (decl) = error_mark_node;
845 else
846 DECL_EXTERNAL (decl) = 0;
847 assemble_alias (decl, id);
848 }
849 else
850 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
851 break;
550e135c 852 }
853 }
b0fc3e72 854}
d53e9dc0 855
856/* Split SPECS_ATTRS, a list of declspecs and prefix attributes, into two
857 lists. SPECS_ATTRS may also be just a typespec (eg: RECORD_TYPE).
858
859 The head of the declspec list is stored in DECLSPECS.
860 The head of the attribute list is stored in PREFIX_ATTRIBUTES.
861
862 Note that attributes in SPECS_ATTRS are stored in the TREE_PURPOSE of
863 the list elements. We drop the containing TREE_LIST nodes and link the
864 resulting attributes together the way decl_attributes expects them. */
865
866void
867split_specs_attrs (specs_attrs, declspecs, prefix_attributes)
868 tree specs_attrs;
869 tree *declspecs, *prefix_attributes;
870{
871 tree t, s, a, next, specs, attrs;
872
873 /* This can happen in c++ (eg: decl: typespec initdecls ';'). */
874 if (specs_attrs != NULL_TREE
875 && TREE_CODE (specs_attrs) != TREE_LIST)
876 {
877 *declspecs = specs_attrs;
878 *prefix_attributes = NULL_TREE;
879 return;
880 }
881
882 /* Remember to keep the lists in the same order, element-wise. */
883
884 specs = s = NULL_TREE;
885 attrs = a = NULL_TREE;
886 for (t = specs_attrs; t; t = next)
887 {
888 next = TREE_CHAIN (t);
889 /* Declspecs have a non-NULL TREE_VALUE. */
890 if (TREE_VALUE (t) != NULL_TREE)
891 {
892 if (specs == NULL_TREE)
893 specs = s = t;
894 else
895 {
896 TREE_CHAIN (s) = t;
897 s = t;
898 }
899 }
900 else
901 {
902 if (attrs == NULL_TREE)
903 attrs = a = TREE_PURPOSE (t);
904 else
905 {
906 TREE_CHAIN (a) = TREE_PURPOSE (t);
907 a = TREE_PURPOSE (t);
908 }
909 /* More attrs can be linked here, move A to the end. */
910 while (TREE_CHAIN (a) != NULL_TREE)
911 a = TREE_CHAIN (a);
912 }
913 }
914
915 /* Terminate the lists. */
916 if (s != NULL_TREE)
917 TREE_CHAIN (s) = NULL_TREE;
918 if (a != NULL_TREE)
919 TREE_CHAIN (a) = NULL_TREE;
920
921 /* All done. */
922 *declspecs = specs;
923 *prefix_attributes = attrs;
924}
0bf60c2b 925
926/* Strip attributes from SPECS_ATTRS, a list of declspecs and attributes.
927 This function is used by the parser when a rule will accept attributes
928 in a particular position, but we don't want to support that just yet.
929
930 A warning is issued for every ignored attribute. */
931
932tree
933strip_attrs (specs_attrs)
934 tree specs_attrs;
935{
936 tree specs, attrs;
937
938 split_specs_attrs (specs_attrs, &specs, &attrs);
939
940 while (attrs)
941 {
942 warning ("`%s' attribute ignored",
943 IDENTIFIER_POINTER (TREE_PURPOSE (attrs)));
944 attrs = TREE_CHAIN (attrs);
945 }
946
947 return specs;
948}
b0fc3e72 949\f
fce9faf9 950/* Check a printf/fprintf/sprintf/scanf/fscanf/sscanf format against
951 a parameter list. */
952
953#define T_I &integer_type_node
954#define T_L &long_integer_type_node
93fccc0a 955#define T_LL &long_long_integer_type_node
fce9faf9 956#define T_S &short_integer_type_node
957#define T_UI &unsigned_type_node
958#define T_UL &long_unsigned_type_node
93fccc0a 959#define T_ULL &long_long_unsigned_type_node
fce9faf9 960#define T_US &short_unsigned_type_node
961#define T_F &float_type_node
962#define T_D &double_type_node
963#define T_LD &long_double_type_node
964#define T_C &char_type_node
73be5127 965#define T_UC &unsigned_char_type_node
fce9faf9 966#define T_V &void_type_node
967#define T_W &wchar_type_node
7710f895 968#define T_ST &sizetype
fce9faf9 969
970typedef struct {
971 char *format_chars;
972 int pointer_count;
973 /* Type of argument if no length modifier is used. */
974 tree *nolen;
975 /* Type of argument if length modifier for shortening is used.
976 If NULL, then this modifier is not allowed. */
73be5127 977 tree *hhlen;
978 /* Type of argument if length modifier for shortening to byte if used.
979 If NULL, then this modifier is not allowed. */
fce9faf9 980 tree *hlen;
981 /* Type of argument if length modifier `l' is used.
982 If NULL, then this modifier is not allowed. */
983 tree *llen;
4a1640ed 984 /* Type of argument if length modifier `q' or `ll' is used.
93fccc0a 985 If NULL, then this modifier is not allowed. */
986 tree *qlen;
fce9faf9 987 /* Type of argument if length modifier `L' is used.
988 If NULL, then this modifier is not allowed. */
989 tree *bigllen;
997d68fe 990 /* Type of argument if length modifier `Z' is used.
991 If NULL, then this modifier is not allowed. */
992 tree *zlen;
fce9faf9 993 /* List of other modifier characters allowed with these options. */
994 char *flag_chars;
995} format_char_info;
996
997static format_char_info print_char_table[] = {
73be5127 998 { "di", 0, T_I, T_I, T_I, T_L, T_LL, T_LL, T_ST, "-wp0 +" },
999 { "oxX", 0, T_UI, T_UI, T_UI, T_UL, T_ULL, T_ULL, T_ST, "-wp0#" },
1000 { "u", 0, T_UI, T_UI, T_UI, T_UL, T_ULL, T_ULL, T_ST, "-wp0" },
997d68fe 1001/* A GNU extension. */
73be5127 1002 { "m", 0, T_V, NULL, NULL, NULL, NULL, NULL, NULL, "-wp" },
1003 { "feEgGaA", 0, T_D, NULL, NULL, NULL, NULL, T_LD, NULL, "-wp0 +#" },
1004 { "c", 0, T_I, NULL, NULL, T_W, NULL, NULL, NULL, "-w" },
1005 { "C", 0, T_W, NULL, NULL, NULL, NULL, NULL, NULL, "-w" },
1006 { "s", 1, T_C, NULL, NULL, T_W, NULL, NULL, NULL, "-wp" },
1007 { "S", 1, T_W, NULL, NULL, NULL, NULL, NULL, NULL, "-wp" },
1008 { "p", 1, T_V, NULL, NULL, NULL, NULL, NULL, NULL, "-w" },
1009 { "n", 1, T_I, NULL, T_S, T_L, T_LL, NULL, NULL, "" },
fce9faf9 1010 { NULL }
1011};
1012
1013static format_char_info scan_char_table[] = {
73be5127 1014 { "di", 1, T_I, T_C, T_S, T_L, T_LL, T_LL, NULL, "*" },
1015 { "ouxX", 1, T_UI, T_UC, T_US, T_UL, T_ULL, T_ULL, NULL, "*" },
1016 { "efgEGaA", 1, T_F, NULL, NULL, T_D, NULL, T_LD, NULL, "*" },
2d47efb6 1017 { "c", 1, T_C, NULL, NULL, T_W, NULL, NULL, NULL, "*" },
1018 { "s", 1, T_C, NULL, NULL, T_W, NULL, NULL, NULL, "*a" },
73be5127 1019 { "[", 1, T_C, NULL, NULL, NULL, NULL, NULL, NULL, "*a" },
1020 { "C", 1, T_W, NULL, NULL, NULL, NULL, NULL, NULL, "*" },
2d47efb6 1021 { "S", 1, T_W, NULL, NULL, NULL, NULL, NULL, NULL, "*a" },
73be5127 1022 { "p", 2, T_V, NULL, NULL, NULL, NULL, NULL, NULL, "*" },
1023 { "n", 1, T_I, T_C, T_S, T_L, T_LL, NULL, NULL, "" },
fce9faf9 1024 { NULL }
1025};
1026
d1f11193 1027/* Handle format characters recognized by glibc's strftime.c.
1028 '2' - MUST do years as only two digits
1029 '3' - MAY do years as only two digits (depending on locale)
1030 'E' - E modifier is acceptable
1031 'O' - O modifier is acceptable to Standard C
1032 'o' - O modifier is acceptable as a GNU extension
1033 'G' - other GNU extensions */
1034
1035static format_char_info time_char_table[] = {
73be5127 1036 { "y", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "2EO-_0w" },
1037 { "D", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "2" },
1038 { "g", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "2O-_0w" },
1039 { "cx", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "3E" },
1040 { "%RTXnrt", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "" },
1041 { "P", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "G" },
1042 { "HIMSUWdemw", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0Ow" },
1043 { "Vju", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0Oow" },
1044 { "Gklsz", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0OGw" },
1045 { "ABZa", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "^#" },
1046 { "p", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "#" },
1047 { "bh", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "^" },
1048 { "CY", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0EOw" },
d1f11193 1049 { NULL }
1050};
1051
b91b108f 1052typedef struct function_format_info
1053{
fce9faf9 1054 struct function_format_info *next; /* next structure on the list */
1055 tree name; /* identifier such as "printf" */
1056 tree assembler_name; /* optional mangled identifier (for C++) */
d1f11193 1057 enum format_type format_type; /* type of format (printf, scanf, etc.) */
fce9faf9 1058 int format_num; /* number of format argument */
1059 int first_arg_num; /* number of first arg (zero for varargs) */
1060} function_format_info;
1061
1062static function_format_info *function_format_list = NULL;
1063
b91b108f 1064typedef struct international_format_info
1065{
1066 struct international_format_info *next; /* next structure on the list */
1067 tree name; /* identifier such as "gettext" */
1068 tree assembler_name; /* optional mangled identifier (for C++) */
1069 int format_num; /* number of format argument */
1070} international_format_info;
1071
1072static international_format_info *international_format_list = NULL;
1073
1074static void check_format_info PROTO((function_format_info *, tree));
fce9faf9 1075
1076/* Initialize the table of functions to perform format checking on.
1077 The ANSI functions are always checked (whether <stdio.h> is
1078 included or not), since it is common to call printf without
1079 including <stdio.h>. There shouldn't be a problem with this,
1080 since ANSI reserves these function names whether you include the
73be5127 1081 header file or not. In any case, the checking is harmless.
b91b108f 1082
1083 Also initialize the name of function that modify the format string for
1084 internationalization purposes. */
fce9faf9 1085
1086void
1087init_function_format_info ()
1088{
d1f11193 1089 record_function_format (get_identifier ("printf"), NULL_TREE,
1090 printf_format_type, 1, 2);
1091 record_function_format (get_identifier ("fprintf"), NULL_TREE,
1092 printf_format_type, 2, 3);
1093 record_function_format (get_identifier ("sprintf"), NULL_TREE,
1094 printf_format_type, 2, 3);
1095 record_function_format (get_identifier ("scanf"), NULL_TREE,
1096 scanf_format_type, 1, 2);
1097 record_function_format (get_identifier ("fscanf"), NULL_TREE,
1098 scanf_format_type, 2, 3);
1099 record_function_format (get_identifier ("sscanf"), NULL_TREE,
1100 scanf_format_type, 2, 3);
1101 record_function_format (get_identifier ("vprintf"), NULL_TREE,
1102 printf_format_type, 1, 0);
1103 record_function_format (get_identifier ("vfprintf"), NULL_TREE,
1104 printf_format_type, 2, 0);
1105 record_function_format (get_identifier ("vsprintf"), NULL_TREE,
1106 printf_format_type, 2, 0);
1107 record_function_format (get_identifier ("strftime"), NULL_TREE,
1108 strftime_format_type, 3, 0);
b91b108f 1109
1110 record_international_format (get_identifier ("gettext"), NULL_TREE, 1);
1111 record_international_format (get_identifier ("dgettext"), NULL_TREE, 2);
1112 record_international_format (get_identifier ("dcgettext"), NULL_TREE, 2);
fce9faf9 1113}
1114
1115/* Record information for argument format checking. FUNCTION_IDENT is
1116 the identifier node for the name of the function to check (its decl
d1f11193 1117 need not exist yet).
1118 FORMAT_TYPE specifies the type of format checking. FORMAT_NUM is the number
fce9faf9 1119 of the argument which is the format control string (starting from 1).
1120 FIRST_ARG_NUM is the number of the first actual argument to check
b91b108f 1121 against the format string, or zero if no checking is not be done
fce9faf9 1122 (e.g. for varargs such as vfprintf). */
1123
d1f11193 1124static void
1125record_function_format (name, assembler_name, format_type,
fce9faf9 1126 format_num, first_arg_num)
1127 tree name;
1128 tree assembler_name;
d1f11193 1129 enum format_type format_type;
fce9faf9 1130 int format_num;
1131 int first_arg_num;
1132{
1133 function_format_info *info;
1134
1135 /* Re-use existing structure if it's there. */
1136
1137 for (info = function_format_list; info; info = info->next)
1138 {
1139 if (info->name == name && info->assembler_name == assembler_name)
1140 break;
1141 }
1142 if (! info)
1143 {
1144 info = (function_format_info *) xmalloc (sizeof (function_format_info));
1145 info->next = function_format_list;
1146 function_format_list = info;
1147
1148 info->name = name;
1149 info->assembler_name = assembler_name;
1150 }
1151
d1f11193 1152 info->format_type = format_type;
fce9faf9 1153 info->format_num = format_num;
1154 info->first_arg_num = first_arg_num;
1155}
1156
b91b108f 1157/* Record information for the names of function that modify the format
1158 argument to format functions. FUNCTION_IDENT is the identifier node for
1159 the name of the function (its decl need not exist yet) and FORMAT_NUM is
1160 the number of the argument which is the format control string (starting
1161 from 1). */
1162
a5c74c6a 1163static void
b91b108f 1164record_international_format (name, assembler_name, format_num)
1165 tree name;
1166 tree assembler_name;
1167 int format_num;
1168{
1169 international_format_info *info;
1170
1171 /* Re-use existing structure if it's there. */
1172
1173 for (info = international_format_list; info; info = info->next)
1174 {
1175 if (info->name == name && info->assembler_name == assembler_name)
1176 break;
1177 }
1178
1179 if (! info)
1180 {
1181 info
1182 = (international_format_info *)
1183 xmalloc (sizeof (international_format_info));
1184 info->next = international_format_list;
1185 international_format_list = info;
1186
1187 info->name = name;
1188 info->assembler_name = assembler_name;
1189 }
1190
1191 info->format_num = format_num;
1192}
1193
fce9faf9 1194static char tfaff[] = "too few arguments for format";
1195\f
1196/* Check the argument list of a call to printf, scanf, etc.
1197 NAME is the function identifier.
1198 ASSEMBLER_NAME is the function's assembler identifier.
1199 (Either NAME or ASSEMBLER_NAME, but not both, may be NULL_TREE.)
1200 PARAMS is the list of argument values. */
1201
1202void
1203check_function_format (name, assembler_name, params)
1204 tree name;
1205 tree assembler_name;
1206 tree params;
1207{
1208 function_format_info *info;
1209
1210 /* See if this function is a format function. */
1211 for (info = function_format_list; info; info = info->next)
1212 {
236236da 1213 if (info->assembler_name
fce9faf9 1214 ? (info->assembler_name == assembler_name)
1215 : (info->name == name))
1216 {
1217 /* Yup; check it. */
1218 check_format_info (info, params);
1219 break;
1220 }
1221 }
1222}
1223
1224/* Check the argument list of a call to printf, scanf, etc.
1225 INFO points to the function_format_info structure.
1226 PARAMS is the list of argument values. */
1227
1228static void
1229check_format_info (info, params)
1230 function_format_info *info;
1231 tree params;
1232{
1233 int i;
1234 int arg_num;
1235 int suppressed, wide, precise;
1236 int length_char;
1237 int format_char;
1238 int format_length;
1239 tree format_tree;
1240 tree cur_param;
1241 tree cur_type;
1242 tree wanted_type;
f19cab4a 1243 tree first_fillin_param;
fce9faf9 1244 char *format_chars;
1245 format_char_info *fci;
fce9faf9 1246 char flag_chars[8];
f19cab4a 1247 int has_operand_number = 0;
fce9faf9 1248
1249 /* Skip to format argument. If the argument isn't available, there's
1250 no work for us to do; prototype checking will catch the problem. */
1251 for (arg_num = 1; ; ++arg_num)
1252 {
1253 if (params == 0)
1254 return;
1255 if (arg_num == info->format_num)
1256 break;
1257 params = TREE_CHAIN (params);
1258 }
1259 format_tree = TREE_VALUE (params);
1260 params = TREE_CHAIN (params);
1261 if (format_tree == 0)
1262 return;
b91b108f 1263
fce9faf9 1264 /* We can only check the format if it's a string constant. */
1265 while (TREE_CODE (format_tree) == NOP_EXPR)
1266 format_tree = TREE_OPERAND (format_tree, 0); /* strip coercion */
b91b108f 1267
1268 if (TREE_CODE (format_tree) == CALL_EXPR
1269 && TREE_CODE (TREE_OPERAND (format_tree, 0)) == ADDR_EXPR
1270 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (format_tree, 0), 0))
1271 == FUNCTION_DECL))
1272 {
1273 tree function = TREE_OPERAND (TREE_OPERAND (format_tree, 0), 0);
1274
1275 /* See if this is a call to a known internationalization function
1276 that modifies the format arg. */
1277 international_format_info *info;
1278
1279 for (info = international_format_list; info; info = info->next)
1280 if (info->assembler_name
1281 ? (info->assembler_name == DECL_ASSEMBLER_NAME (function))
1282 : (info->name == DECL_NAME (function)))
1283 {
1284 tree inner_args;
1285 int i;
1286
1287 for (inner_args = TREE_OPERAND (format_tree, 1), i = 1;
1288 inner_args != 0;
1289 inner_args = TREE_CHAIN (inner_args), i++)
1290 if (i == info->format_num)
1291 {
1292 format_tree = TREE_VALUE (inner_args);
1293
1294 while (TREE_CODE (format_tree) == NOP_EXPR)
1295 format_tree = TREE_OPERAND (format_tree, 0);
1296 }
1297 }
1298 }
1299
a8a713e9 1300 if (integer_zerop (format_tree))
fce9faf9 1301 {
1302 warning ("null format string");
1303 return;
1304 }
1305 if (TREE_CODE (format_tree) != ADDR_EXPR)
1306 return;
1307 format_tree = TREE_OPERAND (format_tree, 0);
1308 if (TREE_CODE (format_tree) != STRING_CST)
1309 return;
1310 format_chars = TREE_STRING_POINTER (format_tree);
1311 format_length = TREE_STRING_LENGTH (format_tree);
1312 if (format_length <= 1)
1313 warning ("zero-length format string");
1314 if (format_chars[--format_length] != 0)
1315 {
1316 warning ("unterminated format string");
1317 return;
1318 }
1319 /* Skip to first argument to check. */
1320 while (arg_num + 1 < info->first_arg_num)
1321 {
1322 if (params == 0)
1323 return;
1324 params = TREE_CHAIN (params);
1325 ++arg_num;
1326 }
f19cab4a 1327
1328 first_fillin_param = params;
fce9faf9 1329 while (1)
1330 {
63b97047 1331 int aflag;
fce9faf9 1332 if (*format_chars == 0)
1333 {
1334 if (format_chars - TREE_STRING_POINTER (format_tree) != format_length)
1335 warning ("embedded `\\0' in format");
f19cab4a 1336 if (info->first_arg_num != 0 && params != 0 && ! has_operand_number)
fce9faf9 1337 warning ("too many arguments for format");
1338 return;
1339 }
1340 if (*format_chars++ != '%')
1341 continue;
1342 if (*format_chars == 0)
1343 {
1344 warning ("spurious trailing `%%' in format");
1345 continue;
1346 }
1347 if (*format_chars == '%')
1348 {
1349 ++format_chars;
1350 continue;
1351 }
1352 flag_chars[0] = 0;
1353 suppressed = wide = precise = FALSE;
d1f11193 1354 if (info->format_type == scanf_format_type)
fce9faf9 1355 {
1356 suppressed = *format_chars == '*';
1357 if (suppressed)
1358 ++format_chars;
cc404a47 1359 while (ISDIGIT (*format_chars))
fce9faf9 1360 ++format_chars;
1361 }
d1f11193 1362 else if (info->format_type == strftime_format_type)
1363 {
1364 while (*format_chars != 0 && index ("_-0^#", *format_chars) != 0)
1365 {
1366 if (pedantic)
1367 warning ("ANSI C does not support the strftime `%c' flag",
1368 *format_chars);
1369 if (index (flag_chars, *format_chars) != 0)
1370 {
1371 warning ("repeated `%c' flag in format",
1372 *format_chars);
1373 ++format_chars;
1374 }
1375 else
1376 {
1377 i = strlen (flag_chars);
1378 flag_chars[i++] = *format_chars++;
1379 flag_chars[i] = 0;
1380 }
1381 }
cc404a47 1382 while (ISDIGIT ((unsigned char) *format_chars))
d1f11193 1383 {
1384 wide = TRUE;
1385 ++format_chars;
1386 }
1387 if (wide && pedantic)
1388 warning ("ANSI C does not support strftime format width");
1389 if (*format_chars == 'E' || *format_chars == 'O')
1390 {
1391 i = strlen (flag_chars);
1392 flag_chars[i++] = *format_chars++;
1393 flag_chars[i] = 0;
1394 if (*format_chars == 'E' || *format_chars == 'O')
1395 {
1396 warning ("multiple E/O modifiers in format");
1397 while (*format_chars == 'E' || *format_chars == 'O')
1398 ++format_chars;
1399 }
1400 }
1401 }
1402 else if (info->format_type == printf_format_type)
fce9faf9 1403 {
f19cab4a 1404 /* See if we have a number followed by a dollar sign. If we do,
1405 it is an operand number, so set PARAMS to that operand. */
1406 if (*format_chars >= '0' && *format_chars <= '9')
1407 {
1408 char *p = format_chars;
1409
1410 while (*p >= '0' && *p++ <= '9')
1411 ;
1412
1413 if (*p == '$')
1414 {
1415 int opnum = atoi (format_chars);
1416
1417 params = first_fillin_param;
1418 format_chars = p + 1;
1419 has_operand_number = 1;
1420
1421 for (i = 1; i < opnum && params != 0; i++)
1422 params = TREE_CHAIN (params);
1423
1424 if (opnum == 0 || params == 0)
1425 {
1426 warning ("operand number out of range in format");
1427 return;
1428 }
1429 }
1430 }
1431
fce9faf9 1432 while (*format_chars != 0 && index (" +#0-", *format_chars) != 0)
1433 {
1434 if (index (flag_chars, *format_chars) != 0)
d1f11193 1435 warning ("repeated `%c' flag in format", *format_chars++);
31f820d2 1436 else
1437 {
1438 i = strlen (flag_chars);
1439 flag_chars[i++] = *format_chars++;
1440 flag_chars[i] = 0;
1441 }
fce9faf9 1442 }
73be5127 1443 /* "If the space and + flags both appear,
fce9faf9 1444 the space flag will be ignored." */
1445 if (index (flag_chars, ' ') != 0
1446 && index (flag_chars, '+') != 0)
1447 warning ("use of both ` ' and `+' flags in format");
1448 /* "If the 0 and - flags both appear,
1449 the 0 flag will be ignored." */
1450 if (index (flag_chars, '0') != 0
1451 && index (flag_chars, '-') != 0)
1452 warning ("use of both `0' and `-' flags in format");
1453 if (*format_chars == '*')
1454 {
1455 wide = TRUE;
1456 /* "...a field width...may be indicated by an asterisk.
1457 In this case, an int argument supplies the field width..." */
1458 ++format_chars;
1459 if (params == 0)
1460 {
1461 warning (tfaff);
1462 return;
1463 }
1464 if (info->first_arg_num != 0)
1465 {
1466 cur_param = TREE_VALUE (params);
1467 params = TREE_CHAIN (params);
1468 ++arg_num;
1469 /* size_t is generally not valid here.
1470 It will work on most machines, because size_t and int
1471 have the same mode. But might as well warn anyway,
1472 since it will fail on other machines. */
cbc5b7d9 1473 if ((TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1474 != integer_type_node)
1475 &&
1476 (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1477 != unsigned_type_node))
d1f11193 1478 warning ("field width is not type int (arg %d)", arg_num);
fce9faf9 1479 }
1480 }
1481 else
1482 {
cc404a47 1483 while (ISDIGIT (*format_chars))
fce9faf9 1484 {
1485 wide = TRUE;
1486 ++format_chars;
1487 }
1488 }
1489 if (*format_chars == '.')
1490 {
1491 precise = TRUE;
1492 ++format_chars;
cc404a47 1493 if (*format_chars != '*' && !ISDIGIT (*format_chars))
fce9faf9 1494 warning ("`.' not followed by `*' or digit in format");
1495 /* "...a...precision...may be indicated by an asterisk.
1496 In this case, an int argument supplies the...precision." */
1497 if (*format_chars == '*')
1498 {
1499 if (info->first_arg_num != 0)
1500 {
1501 ++format_chars;
1502 if (params == 0)
1503 {
1504 warning (tfaff);
1505 return;
1506 }
1507 cur_param = TREE_VALUE (params);
1508 params = TREE_CHAIN (params);
1509 ++arg_num;
1510 if (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1511 != integer_type_node)
d1f11193 1512 warning ("field width is not type int (arg %d)",
1513 arg_num);
fce9faf9 1514 }
1515 }
1516 else
1517 {
cc404a47 1518 while (ISDIGIT (*format_chars))
fce9faf9 1519 ++format_chars;
1520 }
1521 }
1522 }
d1f11193 1523
63b97047 1524 aflag = 0;
d1f11193 1525
1526 if (info->format_type != strftime_format_type)
63b97047 1527 {
d1f11193 1528 if (*format_chars == 'h' || *format_chars == 'l')
1529 length_char = *format_chars++;
1530 else if (*format_chars == 'q' || *format_chars == 'L')
31f820d2 1531 {
d1f11193 1532 length_char = *format_chars++;
c5aa1e92 1533 if (pedantic)
d1f11193 1534 warning ("ANSI C does not support the `%c' length modifier",
1535 length_char);
31f820d2 1536 }
d1f11193 1537 else if (*format_chars == 'Z')
1538 {
1539 length_char = *format_chars++;
1540 if (pedantic)
1541 warning ("ANSI C does not support the `Z' length modifier");
1542 }
1543 else
1544 length_char = 0;
1545 if (length_char == 'l' && *format_chars == 'l')
1546 {
1547 length_char = 'q', format_chars++;
73be5127 1548 /* FIXME: Is allowed in ISO C 9x. */
d1f11193 1549 if (pedantic)
1550 warning ("ANSI C does not support the `ll' length modifier");
1551 }
1552 if (*format_chars == 'a' && info->format_type == scanf_format_type)
1553 {
1554 if (format_chars[1] == 's' || format_chars[1] == 'S'
1555 || format_chars[1] == '[')
1556 {
1557 /* `a' is used as a flag. */
1558 aflag = 1;
1559 format_chars++;
1560 }
1561 }
1562 if (suppressed && length_char != 0)
1563 warning ("use of `*' and `%c' together in format", length_char);
fce9faf9 1564 }
1565 format_char = *format_chars;
d1f11193 1566 if (format_char == 0
1567 || (info->format_type != strftime_format_type && format_char == '%'))
fce9faf9 1568 {
1569 warning ("conversion lacks type at end of format");
1570 continue;
1571 }
2d47efb6 1572 /* The m, C, and S formats are GNU extensions. */
1573 if (pedantic && info->format_type != strftime_format_type
1574 && (format_char == 'm' || format_char == 'C' || format_char == 'S'))
1575 warning ("ANSI C does not support the `%c' format", format_char);
1576 /* ??? The a and A formats are C9X extensions, and should be allowed
1577 when a C9X option is added. */
1578 if (pedantic && info->format_type != strftime_format_type
1579 && (format_char == 'a' || format_char == 'A'))
1580 warning ("ANSI C does not support the `%c' format", format_char);
fce9faf9 1581 format_chars++;
d1f11193 1582 switch (info->format_type)
1583 {
1584 case printf_format_type:
1585 fci = print_char_table;
1586 break;
1587 case scanf_format_type:
1588 fci = scan_char_table;
1589 break;
1590 case strftime_format_type:
1591 fci = time_char_table;
1592 break;
1593 default:
1594 abort ();
1595 }
fce9faf9 1596 while (fci->format_chars != 0
1597 && index (fci->format_chars, format_char) == 0)
1598 ++fci;
1599 if (fci->format_chars == 0)
1600 {
1601 if (format_char >= 040 && format_char < 0177)
d1f11193 1602 warning ("unknown conversion type character `%c' in format",
fce9faf9 1603 format_char);
1604 else
d1f11193 1605 warning ("unknown conversion type character 0x%x in format",
fce9faf9 1606 format_char);
fce9faf9 1607 continue;
1608 }
d1f11193 1609 if (pedantic)
fce9faf9 1610 {
d1f11193 1611 if (index (fci->flag_chars, 'G') != 0)
1612 warning ("ANSI C does not support `%%%c'", format_char);
1613 if (index (fci->flag_chars, 'o') != 0
1614 && index (flag_chars, 'O') != 0)
1615 warning ("ANSI C does not support `%%O%c'", format_char);
fce9faf9 1616 }
d1f11193 1617 if (wide && index (fci->flag_chars, 'w') == 0)
1618 warning ("width used with `%c' format", format_char);
1619 if (index (fci->flag_chars, '2') != 0)
1620 warning ("`%%%c' yields only last 2 digits of year", format_char);
1621 else if (index (fci->flag_chars, '3') != 0)
1622 warning ("`%%%c' yields only last 2 digits of year in some locales",
1623 format_char);
fce9faf9 1624 if (precise && index (fci->flag_chars, 'p') == 0)
d1f11193 1625 warning ("precision used with `%c' format", format_char);
63b97047 1626 if (aflag && index (fci->flag_chars, 'a') == 0)
1627 {
d1f11193 1628 warning ("`a' flag used with `%c' format", format_char);
d6abc4a6 1629 /* To simplify the following code. */
1630 aflag = 0;
fce9faf9 1631 }
2d47efb6 1632 /* The a flag is a GNU extension. */
1633 else if (pedantic && aflag)
1634 warning ("ANSI C does not support the `a' flag");
d1f11193 1635 if (info->format_type == scanf_format_type && format_char == '[')
fce9faf9 1636 {
1637 /* Skip over scan set, in case it happens to have '%' in it. */
1638 if (*format_chars == '^')
1639 ++format_chars;
1640 /* Find closing bracket; if one is hit immediately, then
1641 it's part of the scan set rather than a terminator. */
1642 if (*format_chars == ']')
1643 ++format_chars;
1644 while (*format_chars && *format_chars != ']')
1645 ++format_chars;
1646 if (*format_chars != ']')
d1f11193 1647 /* The end of the format string was reached. */
1648 warning ("no closing `]' for `%%[' format");
fce9faf9 1649 }
1650 if (suppressed)
1651 {
1652 if (index (fci->flag_chars, '*') == 0)
d1f11193 1653 warning ("suppression of `%c' conversion in format", format_char);
fce9faf9 1654 continue;
1655 }
1656 for (i = 0; flag_chars[i] != 0; ++i)
1657 {
1658 if (index (fci->flag_chars, flag_chars[i]) == 0)
d1f11193 1659 warning ("flag `%c' used with type `%c'",
1660 flag_chars[i], format_char);
fce9faf9 1661 }
d1f11193 1662 if (info->format_type == strftime_format_type)
1663 continue;
c5aa1e92 1664 if (precise && index (flag_chars, '0') != 0
1665 && (format_char == 'd' || format_char == 'i'
1666 || format_char == 'o' || format_char == 'u'
1667 || format_char == 'x' || format_char == 'x'))
d1f11193 1668 warning ("`0' flag ignored with precision specifier and `%c' format",
1669 format_char);
fce9faf9 1670 switch (length_char)
1671 {
1672 default: wanted_type = fci->nolen ? *(fci->nolen) : 0; break;
73be5127 1673 case 'H': wanted_type = fci->hhlen ? *(fci->hhlen) : 0; break;
fce9faf9 1674 case 'h': wanted_type = fci->hlen ? *(fci->hlen) : 0; break;
1675 case 'l': wanted_type = fci->llen ? *(fci->llen) : 0; break;
93fccc0a 1676 case 'q': wanted_type = fci->qlen ? *(fci->qlen) : 0; break;
fce9faf9 1677 case 'L': wanted_type = fci->bigllen ? *(fci->bigllen) : 0; break;
997d68fe 1678 case 'Z': wanted_type = fci->zlen ? *fci->zlen : 0; break;
fce9faf9 1679 }
c5aa1e92 1680 if (wanted_type == 0)
047a721c 1681 warning ("use of `%c' length character with `%c' type character",
1682 length_char, format_char);
fce9faf9 1683
1684 /*
1685 ** XXX -- should kvetch about stuff such as
1686 ** {
1687 ** const int i;
1688 **
1689 ** scanf ("%d", &i);
1690 ** }
1691 */
1692
1693 /* Finally. . .check type of argument against desired type! */
1694 if (info->first_arg_num == 0)
1695 continue;
2a287891 1696 if (fci->pointer_count == 0 && wanted_type == void_type_node)
1697 /* This specifier takes no argument. */
1698 continue;
fce9faf9 1699 if (params == 0)
1700 {
1701 warning (tfaff);
1702 return;
1703 }
1704 cur_param = TREE_VALUE (params);
1705 params = TREE_CHAIN (params);
1706 ++arg_num;
1707 cur_type = TREE_TYPE (cur_param);
1708
31f820d2 1709 STRIP_NOPS (cur_param);
1710
fce9faf9 1711 /* Check the types of any additional pointer arguments
1712 that precede the "real" argument. */
d6abc4a6 1713 for (i = 0; i < fci->pointer_count + aflag; ++i)
fce9faf9 1714 {
1715 if (TREE_CODE (cur_type) == POINTER_TYPE)
1716 {
1717 cur_type = TREE_TYPE (cur_type);
31f820d2 1718
c5aa1e92 1719 if (cur_param != 0 && TREE_CODE (cur_param) == ADDR_EXPR)
31f820d2 1720 cur_param = TREE_OPERAND (cur_param, 0);
1721 else
1722 cur_param = 0;
1723
fce9faf9 1724 continue;
1725 }
309f04fa 1726 if (TREE_CODE (cur_type) != ERROR_MARK)
d1f11193 1727 warning ("format argument is not a %s (arg %d)",
1728 ((fci->pointer_count + aflag == 1)
1729 ? "pointer" : "pointer to a pointer"),
1730 arg_num);
fce9faf9 1731 break;
1732 }
1733
31f820d2 1734 /* See if this is an attempt to write into a const type with
1735 scanf. */
d1f11193 1736 if (info->format_type == scanf_format_type
1737 && i == fci->pointer_count + aflag
31f820d2 1738 && wanted_type != 0
1739 && TREE_CODE (cur_type) != ERROR_MARK
1740 && (TYPE_READONLY (cur_type)
1741 || (cur_param != 0
1742 && (TREE_CODE_CLASS (TREE_CODE (cur_param)) == 'c'
1743 || (TREE_CODE_CLASS (TREE_CODE (cur_param)) == 'd'
1744 && TREE_READONLY (cur_param))))))
d1f11193 1745 warning ("writing into constant object (arg %d)", arg_num);
31f820d2 1746
fce9faf9 1747 /* Check the type of the "real" argument, if there's a type we want. */
d6abc4a6 1748 if (i == fci->pointer_count + aflag && wanted_type != 0
309f04fa 1749 && TREE_CODE (cur_type) != ERROR_MARK
fce9faf9 1750 && wanted_type != TYPE_MAIN_VARIANT (cur_type)
1751 /* If we want `void *', allow any pointer type.
1752 (Anything else would already have got a warning.) */
1753 && ! (wanted_type == void_type_node
1754 && fci->pointer_count > 0)
1755 /* Don't warn about differences merely in signedness. */
1756 && !(TREE_CODE (wanted_type) == INTEGER_TYPE
78dafd61 1757 && TREE_CODE (TYPE_MAIN_VARIANT (cur_type)) == INTEGER_TYPE
8200cbb2 1758 && (TREE_UNSIGNED (wanted_type)
4ef20085 1759 ? wanted_type == (cur_type = unsigned_type (cur_type))
1760 : wanted_type == (cur_type = signed_type (cur_type))))
641dd458 1761 /* Likewise, "signed char", "unsigned char" and "char" are
1762 equivalent but the above test won't consider them equivalent. */
1763 && ! (wanted_type == char_type_node
78dafd61 1764 && (TYPE_MAIN_VARIANT (cur_type) == signed_char_type_node
1765 || TYPE_MAIN_VARIANT (cur_type) == unsigned_char_type_node)))
fce9faf9 1766 {
1767 register char *this;
1768 register char *that;
73be5127 1769
fce9faf9 1770 this = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (wanted_type)));
1771 that = 0;
1772 if (TREE_CODE (cur_type) != ERROR_MARK
1773 && TYPE_NAME (cur_type) != 0
1774 && TREE_CODE (cur_type) != INTEGER_TYPE
1775 && !(TREE_CODE (cur_type) == POINTER_TYPE
1776 && TREE_CODE (TREE_TYPE (cur_type)) == INTEGER_TYPE))
1777 {
1778 if (TREE_CODE (TYPE_NAME (cur_type)) == TYPE_DECL
1779 && DECL_NAME (TYPE_NAME (cur_type)) != 0)
1780 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type)));
1781 else
1782 that = IDENTIFIER_POINTER (TYPE_NAME (cur_type));
1783 }
1784
1785 /* A nameless type can't possibly match what the format wants.
1786 So there will be a warning for it.
1787 Make up a string to describe vaguely what it is. */
1788 if (that == 0)
1789 {
1790 if (TREE_CODE (cur_type) == POINTER_TYPE)
1791 that = "pointer";
1792 else
1793 that = "different type";
1794 }
1795
cbc5b7d9 1796 /* Make the warning better in case of mismatch of int vs long. */
1797 if (TREE_CODE (cur_type) == INTEGER_TYPE
1798 && TREE_CODE (wanted_type) == INTEGER_TYPE
1799 && TYPE_PRECISION (cur_type) == TYPE_PRECISION (wanted_type)
1800 && TYPE_NAME (cur_type) != 0
1801 && TREE_CODE (TYPE_NAME (cur_type)) == TYPE_DECL)
1802 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type)));
1803
fce9faf9 1804 if (strcmp (this, that) != 0)
d1f11193 1805 warning ("%s format, %s arg (arg %d)", this, that, arg_num);
fce9faf9 1806 }
1807 }
1808}
1809\f
2a1736ed 1810/* Print a warning if a constant expression had overflow in folding.
1811 Invoke this function on every expression that the language
1812 requires to be a constant expression.
1813 Note the ANSI C standard says it is erroneous for a
1814 constant expression to overflow. */
b2806639 1815
1816void
1817constant_expression_warning (value)
1818 tree value;
1819{
837e1122 1820 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1821 || TREE_CODE (value) == COMPLEX_CST)
1822 && TREE_CONSTANT_OVERFLOW (value) && pedantic)
1823 pedwarn ("overflow in constant expression");
2a1736ed 1824}
1825
1826/* Print a warning if an expression had overflow in folding.
1827 Invoke this function on every expression that
1828 (1) appears in the source code, and
1829 (2) might be a constant expression that overflowed, and
1830 (3) is not already checked by convert_and_check;
1831 however, do not invoke this function on operands of explicit casts. */
1832
1833void
1834overflow_warning (value)
1835 tree value;
1836{
837e1122 1837 if ((TREE_CODE (value) == INTEGER_CST
1838 || (TREE_CODE (value) == COMPLEX_CST
1839 && TREE_CODE (TREE_REALPART (value)) == INTEGER_CST))
1840 && TREE_OVERFLOW (value))
2a1736ed 1841 {
b04da9b5 1842 TREE_OVERFLOW (value) = 0;
e78703c1 1843 if (skip_evaluation == 0)
1844 warning ("integer overflow in expression");
2a1736ed 1845 }
837e1122 1846 else if ((TREE_CODE (value) == REAL_CST
1847 || (TREE_CODE (value) == COMPLEX_CST
1848 && TREE_CODE (TREE_REALPART (value)) == REAL_CST))
1849 && TREE_OVERFLOW (value))
1850 {
1851 TREE_OVERFLOW (value) = 0;
e78703c1 1852 if (skip_evaluation == 0)
1853 warning ("floating point overflow in expression");
837e1122 1854 }
2a1736ed 1855}
1856
1857/* Print a warning if a large constant is truncated to unsigned,
1858 or if -Wconversion is used and a constant < 0 is converted to unsigned.
1859 Invoke this function on every expression that might be implicitly
1860 converted to an unsigned type. */
1861
1862void
1863unsigned_conversion_warning (result, operand)
1864 tree result, operand;
1865{
1866 if (TREE_CODE (operand) == INTEGER_CST
1867 && TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
1868 && TREE_UNSIGNED (TREE_TYPE (result))
e78703c1 1869 && skip_evaluation == 0
2a1736ed 1870 && !int_fits_type_p (operand, TREE_TYPE (result)))
1871 {
1872 if (!int_fits_type_p (operand, signed_type (TREE_TYPE (result))))
1873 /* This detects cases like converting -129 or 256 to unsigned char. */
454b5afb 1874 warning ("large integer implicitly truncated to unsigned type");
2a1736ed 1875 else if (warn_conversion)
454b5afb 1876 warning ("negative integer implicitly converted to unsigned type");
2a1736ed 1877 }
1878}
1879
1880/* Convert EXPR to TYPE, warning about conversion problems with constants.
1881 Invoke this function on every expression that is converted implicitly,
1882 i.e. because of language rules and not because of an explicit cast. */
1883
1884tree
1885convert_and_check (type, expr)
1886 tree type, expr;
1887{
1888 tree t = convert (type, expr);
1889 if (TREE_CODE (t) == INTEGER_CST)
1890 {
b04da9b5 1891 if (TREE_OVERFLOW (t))
2a1736ed 1892 {
b04da9b5 1893 TREE_OVERFLOW (t) = 0;
1894
2c4232f8 1895 /* Do not diagnose overflow in a constant expression merely
1896 because a conversion overflowed. */
1897 TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (expr);
1898
b04da9b5 1899 /* No warning for converting 0x80000000 to int. */
1900 if (!(TREE_UNSIGNED (type) < TREE_UNSIGNED (TREE_TYPE (expr))
1901 && TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
1902 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (expr))))
96730c20 1903 /* If EXPR fits in the unsigned version of TYPE,
1904 don't warn unless pedantic. */
e78703c1 1905 if ((pedantic
1906 || TREE_UNSIGNED (type)
1907 || ! int_fits_type_p (expr, unsigned_type (type)))
1908 && skip_evaluation == 0)
d1f11193 1909 warning ("overflow in implicit constant conversion");
2a1736ed 1910 }
1911 else
1912 unsigned_conversion_warning (t, expr);
1913 }
1914 return t;
b2806639 1915}
1916\f
b0fc3e72 1917void
1918c_expand_expr_stmt (expr)
1919 tree expr;
1920{
1921 /* Do default conversion if safe and possibly important,
1922 in case within ({...}). */
1923 if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE && lvalue_p (expr))
1924 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
1925 expr = default_conversion (expr);
1926
1927 if (TREE_TYPE (expr) != error_mark_node
1928 && TYPE_SIZE (TREE_TYPE (expr)) == 0
1929 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
1930 error ("expression statement has incomplete type");
1931
1932 expand_expr_stmt (expr);
1933}
1934\f
1935/* Validate the expression after `case' and apply default promotions. */
1936
1937tree
1938check_case_value (value)
1939 tree value;
1940{
1941 if (value == NULL_TREE)
1942 return value;
1943
1944 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
fce1d6af 1945 STRIP_TYPE_NOPS (value);
b0fc3e72 1946
1947 if (TREE_CODE (value) != INTEGER_CST
1948 && value != error_mark_node)
1949 {
1950 error ("case label does not reduce to an integer constant");
1951 value = error_mark_node;
1952 }
1953 else
1954 /* Promote char or short to int. */
1955 value = default_conversion (value);
1956
6433f1c2 1957 constant_expression_warning (value);
1958
b0fc3e72 1959 return value;
1960}
1961\f
1962/* Return an integer type with BITS bits of precision,
1963 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
1964
1965tree
1966type_for_size (bits, unsignedp)
1967 unsigned bits;
1968 int unsignedp;
1969{
46375237 1970 if (bits == TYPE_PRECISION (integer_type_node))
1971 return unsignedp ? unsigned_type_node : integer_type_node;
1972
bacde65a 1973 if (bits == TYPE_PRECISION (signed_char_type_node))
b0fc3e72 1974 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
1975
bacde65a 1976 if (bits == TYPE_PRECISION (short_integer_type_node))
b0fc3e72 1977 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
1978
bacde65a 1979 if (bits == TYPE_PRECISION (long_integer_type_node))
b0fc3e72 1980 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
1981
bacde65a 1982 if (bits == TYPE_PRECISION (long_long_integer_type_node))
b0fc3e72 1983 return (unsignedp ? long_long_unsigned_type_node
1984 : long_long_integer_type_node);
1985
bacde65a 1986 if (bits <= TYPE_PRECISION (intQI_type_node))
1987 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
1988
1989 if (bits <= TYPE_PRECISION (intHI_type_node))
1990 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
1991
1992 if (bits <= TYPE_PRECISION (intSI_type_node))
1993 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
1994
1995 if (bits <= TYPE_PRECISION (intDI_type_node))
1996 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
1997
b0fc3e72 1998 return 0;
1999}
2000
2001/* Return a data type that has machine mode MODE.
2002 If the mode is an integer,
2003 then UNSIGNEDP selects between signed and unsigned types. */
2004
2005tree
2006type_for_mode (mode, unsignedp)
2007 enum machine_mode mode;
2008 int unsignedp;
2009{
46375237 2010 if (mode == TYPE_MODE (integer_type_node))
2011 return unsignedp ? unsigned_type_node : integer_type_node;
2012
b0fc3e72 2013 if (mode == TYPE_MODE (signed_char_type_node))
2014 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2015
2016 if (mode == TYPE_MODE (short_integer_type_node))
2017 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2018
b0fc3e72 2019 if (mode == TYPE_MODE (long_integer_type_node))
2020 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2021
2022 if (mode == TYPE_MODE (long_long_integer_type_node))
2023 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
2024
bacde65a 2025 if (mode == TYPE_MODE (intQI_type_node))
2026 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2027
2028 if (mode == TYPE_MODE (intHI_type_node))
2029 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2030
2031 if (mode == TYPE_MODE (intSI_type_node))
2032 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2033
2034 if (mode == TYPE_MODE (intDI_type_node))
2035 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2036
6274009c 2037 if (mode == TYPE_MODE (intTI_type_node))
2038 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2039
b0fc3e72 2040 if (mode == TYPE_MODE (float_type_node))
2041 return float_type_node;
2042
2043 if (mode == TYPE_MODE (double_type_node))
2044 return double_type_node;
2045
2046 if (mode == TYPE_MODE (long_double_type_node))
2047 return long_double_type_node;
2048
2049 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
2050 return build_pointer_type (char_type_node);
2051
2052 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
2053 return build_pointer_type (integer_type_node);
2054
2055 return 0;
2056}
2057\f
a9b9d10c 2058/* Return the minimum number of bits needed to represent VALUE in a
2059 signed or unsigned type, UNSIGNEDP says which. */
2060
2061int
2062min_precision (value, unsignedp)
2063 tree value;
2064 int unsignedp;
2065{
2066 int log;
2067
2068 /* If the value is negative, compute its negative minus 1. The latter
2069 adjustment is because the absolute value of the largest negative value
2070 is one larger than the largest positive value. This is equivalent to
2071 a bit-wise negation, so use that operation instead. */
2072
2073 if (tree_int_cst_sgn (value) < 0)
2074 value = fold (build1 (BIT_NOT_EXPR, TREE_TYPE (value), value));
2075
2076 /* Return the number of bits needed, taking into account the fact
2077 that we need one more bit for a signed than unsigned type. */
2078
2079 if (integer_zerop (value))
2080 log = 0;
2081 else if (TREE_INT_CST_HIGH (value) != 0)
2082 log = HOST_BITS_PER_WIDE_INT + floor_log2 (TREE_INT_CST_HIGH (value));
2083 else
2084 log = floor_log2 (TREE_INT_CST_LOW (value));
2085
2086 return log + 1 + ! unsignedp;
2087}
2088\f
b0fc3e72 2089/* Print an error message for invalid operands to arith operation CODE.
2090 NOP_EXPR is used as a special case (see truthvalue_conversion). */
2091
2092void
2093binary_op_error (code)
2094 enum tree_code code;
2095{
31f820d2 2096 register char *opname;
f03946e4 2097
b0fc3e72 2098 switch (code)
2099 {
2100 case NOP_EXPR:
2101 error ("invalid truth-value expression");
2102 return;
2103
2104 case PLUS_EXPR:
2105 opname = "+"; break;
2106 case MINUS_EXPR:
2107 opname = "-"; break;
2108 case MULT_EXPR:
2109 opname = "*"; break;
2110 case MAX_EXPR:
2111 opname = "max"; break;
2112 case MIN_EXPR:
2113 opname = "min"; break;
2114 case EQ_EXPR:
2115 opname = "=="; break;
2116 case NE_EXPR:
2117 opname = "!="; break;
2118 case LE_EXPR:
2119 opname = "<="; break;
2120 case GE_EXPR:
2121 opname = ">="; break;
2122 case LT_EXPR:
2123 opname = "<"; break;
2124 case GT_EXPR:
2125 opname = ">"; break;
2126 case LSHIFT_EXPR:
2127 opname = "<<"; break;
2128 case RSHIFT_EXPR:
2129 opname = ">>"; break;
2130 case TRUNC_MOD_EXPR:
66618a1e 2131 case FLOOR_MOD_EXPR:
b0fc3e72 2132 opname = "%"; break;
2133 case TRUNC_DIV_EXPR:
66618a1e 2134 case FLOOR_DIV_EXPR:
b0fc3e72 2135 opname = "/"; break;
2136 case BIT_AND_EXPR:
2137 opname = "&"; break;
2138 case BIT_IOR_EXPR:
2139 opname = "|"; break;
2140 case TRUTH_ANDIF_EXPR:
2141 opname = "&&"; break;
2142 case TRUTH_ORIF_EXPR:
2143 opname = "||"; break;
2144 case BIT_XOR_EXPR:
2145 opname = "^"; break;
66618a1e 2146 case LROTATE_EXPR:
2147 case RROTATE_EXPR:
2148 opname = "rotate"; break;
31f820d2 2149 default:
2150 opname = "unknown"; break;
b0fc3e72 2151 }
2152 error ("invalid operands to binary %s", opname);
2153}
2154\f
2155/* Subroutine of build_binary_op, used for comparison operations.
2156 See if the operands have both been converted from subword integer types
2157 and, if so, perhaps change them both back to their original type.
5b511807 2158 This function is also responsible for converting the two operands
2159 to the proper common type for comparison.
b0fc3e72 2160
2161 The arguments of this function are all pointers to local variables
2162 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
2163 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
2164
2165 If this function returns nonzero, it means that the comparison has
2166 a constant value. What this function returns is an expression for
2167 that value. */
2168
2169tree
2170shorten_compare (op0_ptr, op1_ptr, restype_ptr, rescode_ptr)
2171 tree *op0_ptr, *op1_ptr;
2172 tree *restype_ptr;
2173 enum tree_code *rescode_ptr;
2174{
2175 register tree type;
2176 tree op0 = *op0_ptr;
2177 tree op1 = *op1_ptr;
2178 int unsignedp0, unsignedp1;
2179 int real1, real2;
2180 tree primop0, primop1;
2181 enum tree_code code = *rescode_ptr;
2182
2183 /* Throw away any conversions to wider types
2184 already present in the operands. */
2185
2186 primop0 = get_narrower (op0, &unsignedp0);
2187 primop1 = get_narrower (op1, &unsignedp1);
2188
2189 /* Handle the case that OP0 does not *contain* a conversion
2190 but it *requires* conversion to FINAL_TYPE. */
2191
2192 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
2193 unsignedp0 = TREE_UNSIGNED (TREE_TYPE (op0));
2194 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
2195 unsignedp1 = TREE_UNSIGNED (TREE_TYPE (op1));
2196
2197 /* If one of the operands must be floated, we cannot optimize. */
2198 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
2199 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
2200
2201 /* If first arg is constant, swap the args (changing operation
2bd278cc 2202 so value is preserved), for canonicalization. Don't do this if
2203 the second arg is 0. */
b0fc3e72 2204
2bd278cc 2205 if (TREE_CONSTANT (primop0)
2206 && ! integer_zerop (primop1) && ! real_zerop (primop1))
b0fc3e72 2207 {
2208 register tree tem = primop0;
2209 register int temi = unsignedp0;
2210 primop0 = primop1;
2211 primop1 = tem;
2212 tem = op0;
2213 op0 = op1;
2214 op1 = tem;
2215 *op0_ptr = op0;
2216 *op1_ptr = op1;
2217 unsignedp0 = unsignedp1;
2218 unsignedp1 = temi;
2219 temi = real1;
2220 real1 = real2;
2221 real2 = temi;
2222
2223 switch (code)
2224 {
2225 case LT_EXPR:
2226 code = GT_EXPR;
2227 break;
2228 case GT_EXPR:
2229 code = LT_EXPR;
2230 break;
2231 case LE_EXPR:
2232 code = GE_EXPR;
2233 break;
2234 case GE_EXPR:
2235 code = LE_EXPR;
2236 break;
31f820d2 2237 default:
2238 break;
b0fc3e72 2239 }
2240 *rescode_ptr = code;
2241 }
2242
2243 /* If comparing an integer against a constant more bits wide,
2244 maybe we can deduce a value of 1 or 0 independent of the data.
2245 Or else truncate the constant now
2246 rather than extend the variable at run time.
2247
2248 This is only interesting if the constant is the wider arg.
2249 Also, it is not safe if the constant is unsigned and the
2250 variable arg is signed, since in this case the variable
2251 would be sign-extended and then regarded as unsigned.
2252 Our technique fails in this case because the lowest/highest
2253 possible unsigned results don't follow naturally from the
2254 lowest/highest possible values of the variable operand.
2255 For just EQ_EXPR and NE_EXPR there is another technique that
2256 could be used: see if the constant can be faithfully represented
2257 in the other operand's type, by truncating it and reextending it
2258 and see if that preserves the constant's value. */
2259
2260 if (!real1 && !real2
2261 && TREE_CODE (primop1) == INTEGER_CST
2262 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
2263 {
2264 int min_gt, max_gt, min_lt, max_lt;
2265 tree maxval, minval;
2266 /* 1 if comparison is nominally unsigned. */
2267 int unsignedp = TREE_UNSIGNED (*restype_ptr);
2268 tree val;
2269
2270 type = signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0));
2271
2272 maxval = TYPE_MAX_VALUE (type);
2273 minval = TYPE_MIN_VALUE (type);
2274
2275 if (unsignedp && !unsignedp0)
2276 *restype_ptr = signed_type (*restype_ptr);
2277
2278 if (TREE_TYPE (primop1) != *restype_ptr)
2279 primop1 = convert (*restype_ptr, primop1);
2280 if (type != *restype_ptr)
2281 {
2282 minval = convert (*restype_ptr, minval);
2283 maxval = convert (*restype_ptr, maxval);
2284 }
2285
2286 if (unsignedp && unsignedp0)
2287 {
2288 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
2289 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
2290 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
2291 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
2292 }
2293 else
2294 {
2295 min_gt = INT_CST_LT (primop1, minval);
2296 max_gt = INT_CST_LT (primop1, maxval);
2297 min_lt = INT_CST_LT (minval, primop1);
2298 max_lt = INT_CST_LT (maxval, primop1);
2299 }
2300
2301 val = 0;
2302 /* This used to be a switch, but Genix compiler can't handle that. */
2303 if (code == NE_EXPR)
2304 {
2305 if (max_lt || min_gt)
8a7e5d0d 2306 val = boolean_true_node;
b0fc3e72 2307 }
2308 else if (code == EQ_EXPR)
2309 {
2310 if (max_lt || min_gt)
8a7e5d0d 2311 val = boolean_false_node;
b0fc3e72 2312 }
2313 else if (code == LT_EXPR)
2314 {
2315 if (max_lt)
8a7e5d0d 2316 val = boolean_true_node;
b0fc3e72 2317 if (!min_lt)
8a7e5d0d 2318 val = boolean_false_node;
b0fc3e72 2319 }
2320 else if (code == GT_EXPR)
2321 {
2322 if (min_gt)
8a7e5d0d 2323 val = boolean_true_node;
b0fc3e72 2324 if (!max_gt)
8a7e5d0d 2325 val = boolean_false_node;
b0fc3e72 2326 }
2327 else if (code == LE_EXPR)
2328 {
2329 if (!max_gt)
8a7e5d0d 2330 val = boolean_true_node;
b0fc3e72 2331 if (min_gt)
8a7e5d0d 2332 val = boolean_false_node;
b0fc3e72 2333 }
2334 else if (code == GE_EXPR)
2335 {
2336 if (!min_lt)
8a7e5d0d 2337 val = boolean_true_node;
b0fc3e72 2338 if (max_lt)
8a7e5d0d 2339 val = boolean_false_node;
b0fc3e72 2340 }
2341
2342 /* If primop0 was sign-extended and unsigned comparison specd,
2343 we did a signed comparison above using the signed type bounds.
2344 But the comparison we output must be unsigned.
2345
2346 Also, for inequalities, VAL is no good; but if the signed
2347 comparison had *any* fixed result, it follows that the
2348 unsigned comparison just tests the sign in reverse
2349 (positive values are LE, negative ones GE).
2350 So we can generate an unsigned comparison
2351 against an extreme value of the signed type. */
2352
2353 if (unsignedp && !unsignedp0)
2354 {
2355 if (val != 0)
2356 switch (code)
2357 {
2358 case LT_EXPR:
2359 case GE_EXPR:
2360 primop1 = TYPE_MIN_VALUE (type);
2361 val = 0;
2362 break;
2363
2364 case LE_EXPR:
2365 case GT_EXPR:
2366 primop1 = TYPE_MAX_VALUE (type);
2367 val = 0;
2368 break;
31f820d2 2369
2370 default:
2371 break;
b0fc3e72 2372 }
2373 type = unsigned_type (type);
2374 }
2375
78dafd61 2376 if (!max_gt && !unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
b0fc3e72 2377 {
2378 /* This is the case of (char)x >?< 0x80, which people used to use
2379 expecting old C compilers to change the 0x80 into -0x80. */
8a7e5d0d 2380 if (val == boolean_false_node)
b0fc3e72 2381 warning ("comparison is always 0 due to limited range of data type");
8a7e5d0d 2382 if (val == boolean_true_node)
b0fc3e72 2383 warning ("comparison is always 1 due to limited range of data type");
2384 }
2385
78dafd61 2386 if (!min_lt && unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
b0fc3e72 2387 {
a693ee7d 2388 /* This is the case of (unsigned char)x >?< -1 or < 0. */
8a7e5d0d 2389 if (val == boolean_false_node)
b0fc3e72 2390 warning ("comparison is always 0 due to limited range of data type");
8a7e5d0d 2391 if (val == boolean_true_node)
b0fc3e72 2392 warning ("comparison is always 1 due to limited range of data type");
2393 }
2394
2395 if (val != 0)
2396 {
2397 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2398 if (TREE_SIDE_EFFECTS (primop0))
2399 return build (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
2400 return val;
2401 }
2402
2403 /* Value is not predetermined, but do the comparison
2404 in the type of the operand that is not constant.
2405 TYPE is already properly set. */
2406 }
2407 else if (real1 && real2
2203bd5c 2408 && (TYPE_PRECISION (TREE_TYPE (primop0))
2409 == TYPE_PRECISION (TREE_TYPE (primop1))))
b0fc3e72 2410 type = TREE_TYPE (primop0);
2411
2412 /* If args' natural types are both narrower than nominal type
2413 and both extend in the same manner, compare them
2414 in the type of the wider arg.
2415 Otherwise must actually extend both to the nominal
2416 common type lest different ways of extending
2417 alter the result.
2418 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
2419
2420 else if (unsignedp0 == unsignedp1 && real1 == real2
2421 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
2422 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
2423 {
2424 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
2425 type = signed_or_unsigned_type (unsignedp0
2426 || TREE_UNSIGNED (*restype_ptr),
2427 type);
2428 /* Make sure shorter operand is extended the right way
2429 to match the longer operand. */
2430 primop0 = convert (signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0)),
2431 primop0);
2432 primop1 = convert (signed_or_unsigned_type (unsignedp1, TREE_TYPE (primop1)),
2433 primop1);
2434 }
2435 else
2436 {
2437 /* Here we must do the comparison on the nominal type
2438 using the args exactly as we received them. */
2439 type = *restype_ptr;
2440 primop0 = op0;
2441 primop1 = op1;
2442
2443 if (!real1 && !real2 && integer_zerop (primop1)
32ab5039 2444 && TREE_UNSIGNED (*restype_ptr))
b0fc3e72 2445 {
2446 tree value = 0;
2447 switch (code)
2448 {
2449 case GE_EXPR:
2bd278cc 2450 /* All unsigned values are >= 0, so we warn if extra warnings
2451 are requested. However, if OP0 is a constant that is
2452 >= 0, the signedness of the comparison isn't an issue,
2453 so suppress the warning. */
2454 if (extra_warnings
2455 && ! (TREE_CODE (primop0) == INTEGER_CST
2456 && ! TREE_OVERFLOW (convert (signed_type (type),
2457 primop0))))
b0fc3e72 2458 warning ("unsigned value >= 0 is always 1");
8a7e5d0d 2459 value = boolean_true_node;
b0fc3e72 2460 break;
2461
2462 case LT_EXPR:
2bd278cc 2463 if (extra_warnings
2464 && ! (TREE_CODE (primop0) == INTEGER_CST
2465 && ! TREE_OVERFLOW (convert (signed_type (type),
2466 primop0))))
b0fc3e72 2467 warning ("unsigned value < 0 is always 0");
8a7e5d0d 2468 value = boolean_false_node;
31f820d2 2469 break;
2470
2471 default:
2472 break;
b0fc3e72 2473 }
2474
2475 if (value != 0)
2476 {
2477 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2478 if (TREE_SIDE_EFFECTS (primop0))
2479 return build (COMPOUND_EXPR, TREE_TYPE (value),
2480 primop0, value);
2481 return value;
2482 }
2483 }
2484 }
2485
2486 *op0_ptr = convert (type, primop0);
2487 *op1_ptr = convert (type, primop1);
2488
8a7e5d0d 2489 *restype_ptr = boolean_type_node;
b0fc3e72 2490
2491 return 0;
2492}
2493\f
2494/* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
2495 or validate its data type for an `if' or `while' statement or ?..: exp.
2496
2497 This preparation consists of taking the ordinary
2498 representation of an expression expr and producing a valid tree
2499 boolean expression describing whether expr is nonzero. We could
8a7e5d0d 2500 simply always do build_binary_op (NE_EXPR, expr, boolean_false_node, 1),
b0fc3e72 2501 but we optimize comparisons, &&, ||, and !.
2502
8a7e5d0d 2503 The resulting type should always be `boolean_type_node'. */
b0fc3e72 2504
2505tree
2506truthvalue_conversion (expr)
2507 tree expr;
2508{
baa1f4f5 2509 if (TREE_CODE (expr) == ERROR_MARK)
2510 return expr;
2511
a70adbe5 2512#if 0 /* This appears to be wrong for C++. */
baa1f4f5 2513 /* These really should return error_mark_node after 2.4 is stable.
2514 But not all callers handle ERROR_MARK properly. */
2515 switch (TREE_CODE (TREE_TYPE (expr)))
2516 {
2517 case RECORD_TYPE:
2518 error ("struct type value used where scalar is required");
8a7e5d0d 2519 return boolean_false_node;
baa1f4f5 2520
2521 case UNION_TYPE:
2522 error ("union type value used where scalar is required");
8a7e5d0d 2523 return boolean_false_node;
baa1f4f5 2524
2525 case ARRAY_TYPE:
2526 error ("array type value used where scalar is required");
8a7e5d0d 2527 return boolean_false_node;
baa1f4f5 2528
2529 default:
2530 break;
2531 }
a70adbe5 2532#endif /* 0 */
baa1f4f5 2533
b0fc3e72 2534 switch (TREE_CODE (expr))
2535 {
2536 /* It is simpler and generates better code to have only TRUTH_*_EXPR
2537 or comparison expressions as truth values at this level. */
2538#if 0
2539 case COMPONENT_REF:
2540 /* A one-bit unsigned bit-field is already acceptable. */
2541 if (1 == TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (expr, 1)))
2542 && TREE_UNSIGNED (TREE_OPERAND (expr, 1)))
2543 return expr;
2544 break;
2545#endif
2546
2547 case EQ_EXPR:
2548 /* It is simpler and generates better code to have only TRUTH_*_EXPR
2549 or comparison expressions as truth values at this level. */
2550#if 0
2551 if (integer_zerop (TREE_OPERAND (expr, 1)))
2552 return build_unary_op (TRUTH_NOT_EXPR, TREE_OPERAND (expr, 0), 0);
2553#endif
2554 case NE_EXPR: case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
2555 case TRUTH_ANDIF_EXPR:
2556 case TRUTH_ORIF_EXPR:
2557 case TRUTH_AND_EXPR:
2558 case TRUTH_OR_EXPR:
31f6e93c 2559 case TRUTH_XOR_EXPR:
7bbc42b5 2560 case TRUTH_NOT_EXPR:
8a7e5d0d 2561 TREE_TYPE (expr) = boolean_type_node;
2562 return expr;
3e851b85 2563
b0fc3e72 2564 case ERROR_MARK:
2565 return expr;
2566
2567 case INTEGER_CST:
8a7e5d0d 2568 return integer_zerop (expr) ? boolean_false_node : boolean_true_node;
b0fc3e72 2569
2570 case REAL_CST:
8a7e5d0d 2571 return real_zerop (expr) ? boolean_false_node : boolean_true_node;
b0fc3e72 2572
2573 case ADDR_EXPR:
65b5e6a6 2574 /* If we are taking the address of a external decl, it might be zero
2575 if it is weak, so we cannot optimize. */
2576 if (TREE_CODE_CLASS (TREE_CODE (TREE_OPERAND (expr, 0))) == 'd'
2577 && DECL_EXTERNAL (TREE_OPERAND (expr, 0)))
2578 break;
2579
b0fc3e72 2580 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 0)))
8a7e5d0d 2581 return build (COMPOUND_EXPR, boolean_type_node,
2582 TREE_OPERAND (expr, 0), boolean_true_node);
b0fc3e72 2583 else
8a7e5d0d 2584 return boolean_true_node;
b0fc3e72 2585
2203bd5c 2586 case COMPLEX_EXPR:
2ba726d2 2587 return build_binary_op ((TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
95809de4 2588 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
2ba726d2 2589 truthvalue_conversion (TREE_OPERAND (expr, 0)),
2590 truthvalue_conversion (TREE_OPERAND (expr, 1)),
2203bd5c 2591 0);
2592
b0fc3e72 2593 case NEGATE_EXPR:
2594 case ABS_EXPR:
2595 case FLOAT_EXPR:
2596 case FFS_EXPR:
2597 /* These don't change whether an object is non-zero or zero. */
2598 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2599
2600 case LROTATE_EXPR:
2601 case RROTATE_EXPR:
2602 /* These don't change whether an object is zero or non-zero, but
2603 we can't ignore them if their second arg has side-effects. */
2604 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
8a7e5d0d 2605 return build (COMPOUND_EXPR, boolean_type_node, TREE_OPERAND (expr, 1),
b0fc3e72 2606 truthvalue_conversion (TREE_OPERAND (expr, 0)));
2607 else
2608 return truthvalue_conversion (TREE_OPERAND (expr, 0));
73be5127 2609
b0fc3e72 2610 case COND_EXPR:
2611 /* Distribute the conversion into the arms of a COND_EXPR. */
8a7e5d0d 2612 return fold (build (COND_EXPR, boolean_type_node, TREE_OPERAND (expr, 0),
b0fc3e72 2613 truthvalue_conversion (TREE_OPERAND (expr, 1)),
2614 truthvalue_conversion (TREE_OPERAND (expr, 2))));
2615
2616 case CONVERT_EXPR:
2617 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
2618 since that affects how `default_conversion' will behave. */
2619 if (TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE
2620 || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
2621 break;
a92771b8 2622 /* fall through... */
b0fc3e72 2623 case NOP_EXPR:
2624 /* If this is widening the argument, we can ignore it. */
2625 if (TYPE_PRECISION (TREE_TYPE (expr))
2626 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
2627 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2628 break;
2629
b0fc3e72 2630 case MINUS_EXPR:
fe0a0255 2631 /* With IEEE arithmetic, x - x may not equal 0, so we can't optimize
2632 this case. */
2633 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
2634 && TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE)
2635 break;
a92771b8 2636 /* fall through... */
fe0a0255 2637 case BIT_XOR_EXPR:
a70adbe5 2638 /* This and MINUS_EXPR can be changed into a comparison of the
fe0a0255 2639 two objects. */
b0fc3e72 2640 if (TREE_TYPE (TREE_OPERAND (expr, 0))
2641 == TREE_TYPE (TREE_OPERAND (expr, 1)))
2642 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2643 TREE_OPERAND (expr, 1), 1);
2644 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2645 fold (build1 (NOP_EXPR,
2646 TREE_TYPE (TREE_OPERAND (expr, 0)),
2647 TREE_OPERAND (expr, 1))), 1);
16837b18 2648
c2edbca4 2649 case BIT_AND_EXPR:
cc4c7eaf 2650 if (integer_onep (TREE_OPERAND (expr, 1))
2651 && TREE_TYPE (expr) != boolean_type_node)
2652 /* Using convert here would cause infinite recursion. */
2653 return build1 (NOP_EXPR, boolean_type_node, expr);
2654 break;
c2edbca4 2655
16837b18 2656 case MODIFY_EXPR:
2657 if (warn_parentheses && C_EXP_ORIGINAL_CODE (expr) == MODIFY_EXPR)
2658 warning ("suggest parentheses around assignment used as truth value");
2659 break;
73be5127 2660
31f820d2 2661 default:
2662 break;
b0fc3e72 2663 }
2664
2ba726d2 2665 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
2666 return (build_binary_op
2667 ((TREE_SIDE_EFFECTS (expr)
95809de4 2668 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
2ba726d2 2669 truthvalue_conversion (build_unary_op (REALPART_EXPR, expr, 0)),
2670 truthvalue_conversion (build_unary_op (IMAGPART_EXPR, expr, 0)),
2671 0));
2672
b0fc3e72 2673 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
2674}
2675\f
2676/* Read the rest of a #-directive from input stream FINPUT.
2677 In normal use, the directive name and the white space after it
2678 have already been read, so they won't be included in the result.
2679 We allow for the fact that the directive line may contain
2680 a newline embedded within a character or string literal which forms
2681 a part of the directive.
2682
2683 The value is a string in a reusable buffer. It remains valid
b97b38c0 2684 only until the next time this function is called.
2685
2686 The terminating character ('\n' or EOF) is left in FINPUT for the
2687 caller to re-read. */
b0fc3e72 2688
2689char *
2690get_directive_line (finput)
2691 register FILE *finput;
2692{
2693 static char *directive_buffer = NULL;
2694 static unsigned buffer_length = 0;
2695 register char *p;
2696 register char *buffer_limit;
2697 register int looking_for = 0;
2698 register int char_escaped = 0;
2699
2700 if (buffer_length == 0)
2701 {
2702 directive_buffer = (char *)xmalloc (128);
2703 buffer_length = 128;
2704 }
2705
2706 buffer_limit = &directive_buffer[buffer_length];
2707
2708 for (p = directive_buffer; ; )
2709 {
2710 int c;
2711
2712 /* Make buffer bigger if it is full. */
2713 if (p >= buffer_limit)
2714 {
2715 register unsigned bytes_used = (p - directive_buffer);
2716
2717 buffer_length *= 2;
2718 directive_buffer
2719 = (char *)xrealloc (directive_buffer, buffer_length);
2720 p = &directive_buffer[bytes_used];
2721 buffer_limit = &directive_buffer[buffer_length];
2722 }
2723
2724 c = getc (finput);
2725
2726 /* Discard initial whitespace. */
2727 if ((c == ' ' || c == '\t') && p == directive_buffer)
2728 continue;
2729
2730 /* Detect the end of the directive. */
b97b38c0 2731 if (looking_for == 0
2732 && (c == '\n' || c == EOF))
b0fc3e72 2733 {
2734 ungetc (c, finput);
2735 c = '\0';
2736 }
2737
2738 *p++ = c;
2739
2740 if (c == 0)
2741 return directive_buffer;
2742
2743 /* Handle string and character constant syntax. */
2744 if (looking_for)
2745 {
2746 if (looking_for == c && !char_escaped)
2747 looking_for = 0; /* Found terminator... stop looking. */
2748 }
2749 else
2750 if (c == '\'' || c == '"')
2751 looking_for = c; /* Don't stop buffering until we see another
3398e91d 2752 one of these (or an EOF). */
b0fc3e72 2753
2754 /* Handle backslash. */
2755 char_escaped = (c == '\\' && ! char_escaped);
2756 }
2757}
ceee5ef4 2758\f
2759/* Make a variant type in the proper way for C/C++, propagating qualifiers
2760 down to the element type of an array. */
2761
2762tree
2763c_build_type_variant (type, constp, volatilep)
2764 tree type;
2765 int constp, volatilep;
2766{
2767 if (TREE_CODE (type) == ARRAY_TYPE)
754d8048 2768 return build_array_type (c_build_type_variant (TREE_TYPE (type),
2769 constp, volatilep),
2770 TYPE_DOMAIN (type));
ceee5ef4 2771 return build_type_variant (type, constp, volatilep);
2772}