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