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